Skip to content

Instantly share code, notes, and snippets.

@swarminglogic
Last active June 14, 2019 08:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swarminglogic/b84dd42441e5a95c6cb9cf1402b49847 to your computer and use it in GitHub Desktop.
Save swarminglogic/b84dd42441e5a95c6cb9cf1402b49847 to your computer and use it in GitHub Desktop.
git-root: finds git root directory without using git executable (POSIX compliant shell script)
#!/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