#!/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"