Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Created September 22, 2014 17:13
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 pesterhazy/908e1272ac1e83e5beb4 to your computer and use it in GitHub Desktop.
Save pesterhazy/908e1272ac1e83e5beb4 to your computer and use it in GitHub Desktop.
#!/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