summaryrefslogtreecommitdiffstats
path: root/src/subgit-parent
blob: c3999a85b3aa5ca8029b302ecff0ea6ece3a0e3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash

# Returns the longest subpath of the given path, which
# tracks the current path in its .subgit folder.

usage() {
	die $(cat <<-EOF
	Usage: subgit parent [PATH]

	  Finds the subgit container (root directory with .subgitrc) that
	  tracks the given PATH as a subrepository. Walks up the directory
	  tree to find the appropriate parent.
	EOF
	)
}

while [ "$1" ]; do
	case $1 in
	-h) usage;;
	--) shift; break;;
	-*) die "Invalid option $1";;
	*) break;;
	esac
done
[ $# -gt 1 ] && usage
path=$(realpath "${1:-.}")

root="$path"
while [ 1 ]; do
	root=$(subgit-sub root "$(dirname "$root")")
	if subgit-sub relpath "$root" "$path" &>/dev/null; then
		break
	fi
done

echo "$root"