Skip to content

Instantly share code, notes, and snippets.

@gwerbin
Created August 27, 2020 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwerbin/e957b352aaf9a2f9981a03bf586fde21 to your computer and use it in GitHub Desktop.
Save gwerbin/e957b352aaf9a2f9981a03bf586fde21 to your computer and use it in GitHub Desktop.
Gitpath
# TODO: should this have a --flag for dispatch to system/global/local? Currently only supports local.
function gitpath() {
local git_dir output_path
if [[ -z "$GIT_DIR" ]]; then
read -r -d '' git_dir < <(git rev-parse --show-toplevel)
git_dir="${git_dir}/.git"
else
git_dir="$GIT_DIR"
fi
if [[ -z "$git_dir" ]]; then
# Rely on Git to print its own error message to stderr, and use its exit code
return $?
fi
if [[ -n "$1" ]]; then
case "$1" in
attr|attrib|attribute|attributes)
output_path="info/attributes" ;;
config)
# also .gitconfig and $XDG_CONFIG_HOME/git/config
output_path="config" ;;
desc|descr|description)
# Used for 3rd party tools
# https://git-scm.com/docs/gitweb#Documentation/gitweb.txt-descriptionorcodegitwebdescriptioncode
output_path="description" ;;
excl|exclude|ign|ignore)
# also .gitignore and $XDG_CONFIG_HOME/git/ignore
hook|hooks)
output_path="hooks" ;;
output_path="info/exclude" ;;
info)
output_path="info" ;;
*)
print -u2 -- "Unknown Git directory: $1"
return 1
esac
output_path="${git_dir%/}/${output_path}"
else
output_path="${git_dir}"
fi
echo "$output_path"
}
gitpath "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment