summaryrefslogtreecommitdiffstats
path: root/src/subgit-root
blob: 9a02512e9bde01e548792d314323aa14045a262d (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 includes a .subgitrc file.

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

	  Finds the root of the subgit container by walking up from PATH
	  until finding a directory with a .subgitrc file. Returns the
	  absolute path to that directory.
	EOF
	)
}

while [ "$1" ]; do
	case $1 in
	-h) usage;;
	--) shift; break;;
	-*) die "Invalid option $1";;
	*) break;;
	esac
done

[ $# -gt 1 ] && usage
path=$(realpath -m "${1:-.}")

root=$path
while [ 1 ]; do
	[ -f "$root/.subgitrc" ] && break
	[ "$root" = "/" ] && die "Not a subgit repository"
	root=$(dirname "$root")
done

echo "$root"