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