Skip to content

Instantly share code, notes, and snippets.

@thepaul
Created August 25, 2011 17:54
Show Gist options
  • Save thepaul/1171296 to your computer and use it in GitHub Desktop.
Save thepaul/1171296 to your computer and use it in GitHub Desktop.
"git find" command
[alias]
; "git find" should work exactly like your system find (BSD or GNU), except for
; two things: one, it will always ignore the current git directory; and two, if
; you don't supply any root paths to search, it defaults to searching from your
; git project toplevel.
;
; paul cannon <p@thepaul.org> 2011
find = "!_() { \
declare -a preargs; \
declare -a postargs; \
declare -a finddirs; \
action=-print; \
sec=pre; \
gitdir=$(git rev-parse --git-dir); \
while [ \"${#@}\" -gt 0 ]; do \
case \"$1\" in \
-exec|-ls|-print|-print0|-ok|-delete|-execdir|-fls|-fprint|-fprint0|-fprintf|-okdir|-printf) \
sec=expr; \
action=''; \
postargs=( \"${postargs[@]}\" \"$1\" ); \
;; \
-*) \
if [ \"$sec\" = pre ]; then \
if [ \"$1\" = -D ] || [ \"$1\" = -O ]; then \
preargs=( \"${preargs[@]}\" \"$1\" \"$2\" ); \
shift; \
elif echo \"$1\" | grep -qE '^-([HLPEXdsxf]+|[DO].*)$'; then \
preargs=( \"${preargs[@]}\" \"$1\" ); \
else \
sec=expr; \
fi; \
fi; \
if [ \"$sec\" = dirs ] || [ \"$sec\" = expr ]; then \
sec=expr; \
postargs=( \"${postargs[@]}\" \"$1\" ); \
fi; \
;; \
*) \
[ \"$sec\" = pre ] && sec=dirs; \
if [ \"$sec\" = dirs ]; then \
finddirs=( \"${finddirs[@]}\" \"$1\" ); \
else \
postargs=( \"${postargs[@]}\" \"$1\" ); \
fi; \
esac; \
shift; \
done; \
[ \"${#finddirs[@]}\" -eq 0 ] && finddirs=( \"$(dirname \"$gitdir\")\" ); \
[ \"${#postargs[@]}\" -eq 0 ] && postargs=( -true ); \
find \"${preargs[@]}\" \"${finddirs[@]}\" \\( -samefile \"$gitdir\" -prune \\) -o \\( \"${postargs[@]}\" \\) $action; \
}; _"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment