В случае, если кому-то нужен POSIX-совместимый способ сделать это, не требуя git
исполняемого файла:
git-root
#$1: Path to child directory
git_root_recurse_parent() {
# Check if cwd is a git root directory
if [ -d .git/objects -a -d .git/refs -a -f .git/HEAD ] ; then
pwd
return 0
fi
# Check if recursion should end (typically if cwd is /)
if [ "${1}" = "$(pwd)" ] ; then
return 1
fi
# Check parent directory in the same way
local cwd=$(pwd)
cd ..
git_root_recurse_parent "${cwd}"
}
git_root_recurse_parent
Если вы просто хотите, чтобы функциональность была частью сценария, удалите шебанг и замените последнюю строку git_root_recurse_parent
на:
git_root() {
(git_root_recurse_parent)
}