Created
September 22, 2014 17:13
-
-
Save pesterhazy/908e1272ac1e83e5beb4 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env zsh | |
# | |
# Grep in the files of the current git repository *with a list of exclusions*, using `git grep` or `ag`. | |
# | |
# Exclusions are defined in .grepignore. Each line is a regex suitable for grep(2); if the filename | |
# matches any of the regexes, it is excluded from the search. | |
# | |
# Works with zsh (bash not tested). | |
# | |
# Versions for `git grep` and `ag` are provided. | |
# | |
# Usage: | |
# | |
# ggrep word # searches for "word" in | |
grep-with-ignore() { | |
cmd="$1" | |
shift | |
if [[ "$1" == "-a" ]]; then | |
all=1 | |
shift | |
else | |
all=0 | |
fi | |
arlist=() | |
while test ${#} -gt 0; do | |
if [[ "$1" != -* ]]; then | |
break | |
fi | |
arlist+=("$1") | |
shift | |
done | |
pattern="$1" | |
shift | |
if [[ "$all" == 1 ]]; then | |
git ls-files -- "$@" | xargs -d "\n" $=cmd $arlist "$pattern" | |
else | |
git ls-files -- "$@" | grep -vf .grepignore | xargs -d "\n" $=cmd $arlist "$pattern" | |
fi | |
} | |
gag() { | |
grep-with-ignore "ag -S" "$@" | |
} | |
ggrep() { | |
grep-with-ignore "git grep" "$@" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment