Skip to content

Instantly share code, notes, and snippets.

@imaami
Created October 25, 2019 09:05
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 imaami/7961d986487ec2950f925128899c9064 to your computer and use it in GitHub Desktop.
Save imaami/7961d986487ec2950f925128899c9064 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Save this script as $HOME/.local/lib/git/find (make sure it's
# executable), then add a git alias:
#
# $ git config --global alias.find '!$HOME/.local/lib/git/find'
#
T="$(git rev-parse --show-toplevel)"
[[ -z "$T" ]] || T="$T/"
P="$T$GIT_PREFIX"
if (( ${#@} )); then
A=("${@/#/$P}")
else
A=("$P")
fi
git ls-tree -r HEAD -- "${A[@]}" | grep -E '^[0-9]+ blob ' | cut -f2 \
| head -c-1 | tr '\n' '\0' | xargs -0 realpath --relative-to="$P"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment