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