git-root: finds git root directory without using git executable (POSIX compliant shell script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#$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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment