summaryrefslogtreecommitdiffstats
path: root/src/subgit-relpath
blob: f22f11957ed8e07f73009bce7e63807875175e6b (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
37
38
39
#!/bin/bash

# Finds the relative path at which tracking for a target path
# starts within its subgit parent.

usage() {
	die $(cat <<-EOF
	Usage: subgit relpath ROOT PATH

	  Determines the relative path (from ROOT) where tracking begins
	  for the given PATH. Walks up from PATH until finding a tracked
	  entry in ROOT's .subgitrc.
	EOF
	)
}

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

[ $# -ne 2 ] && usage
root=$1
path=$2

source "$root/.subgitrc"

relpath=$(realpath -m --relative-to="$root" "$path")
while [ 1 ]; do
	[ ! -z "${subgit[$relpath]}" ] && break
	[ "$relpath" = "." ] && die "Not tracked"
	relpath=$(dirname "$relpath")
done

echo "$relpath"