Mit dem Beispiel von @KYNAN als Basis habe ich dieses Skript gemacht und es in meinen Pfad (~/bin/
) als gg
. Es verwendet git grep
, vermeidet jedoch einige angegebene Dateitypen.
In unserem Repo gibt es eine Menge Bilder, also habe ich die Bilddateien ausgeschlossen, und das dauert die Serchtime auf 1/3 runter, wenn ich das ganze Repo suche. Aber das Skript könnte leicht modifiziert werden, um andere Dateitypen oder Geleralmuster auszuschließen.
#!/bin/bash
#
# Wrapper of git-grep that excludes certain filetypes.
# NOTE: The filetypes to exclude is hardcoded for my specific needs.
#
# The basic setup of this script is from here:
# https://stackoverflow.com/a/14226610/42580
# But there is issues with giving extra path information to the script
# therefor I crafted the while-thing that moves path-parts to the other side
# of the '--'.
# Declare the filetypes to ignore here
EXCLUDES="png xcf jpg jpeg pdf ps"
# Rebuild the list of fileendings to a good regexp
EXCLUDES=`echo $EXCLUDES | sed -e 's/ /\\\|/g' -e 's/.*/\\\.\\\(\0\\\)/'`
# Store the stuff that is moved from the arguments.
moved=
# If git-grep returns this "fatal..." then move the last element of the
# arg-list to the list of files to search.
err="fatal: bad flag '--' used after filename"
while [ "$err" = "fatal: bad flag '--' used after filename" ]; do
{
err=$(git grep "[email protected]" -- `git ls-files $moved | grep -iv "$EXCLUDES"` \
2>&1 1>&3-)
} 3>&1
# The rest of the code in this loop is here to move the last argument in
# the arglist to a separate list $moved. I had issues with whitespace in
# the search-string, so this is loosely based on:
# http://www.linuxjournal.com/content/bash-preserving-whitespace-using-set-and-eval
x=1
items=
for i in "[email protected]"; do
if [ $x -lt $# ]; then
items="$items \"$i\""
else
moved="$i $moved"
fi
x=$(($x+1))
done
eval set -- $items
done
# Show the error if there was any
echo $err
Anmerkung 1
Nach this soll es möglich sein, das Ding git-gg
zu nennen und es nennen zu können, als reguläres git Kommando:
$ git gg searchstring
Aber ich kann Lass das nicht funktionieren. Ich erstellte das Skript in meinem ~/bin/
und machte einen git-gg
Symlink in /usr/lib/git-core/
.
Anmerkung 2
Der Befehl kann nicht in einen regulären sh
git-Alias gemacht werden, da es dann an der Wurzel des Repo aufgerufen werden. Und das will ich nicht!
Doing es auf Bash wäre eine mögliche Problemumgehung: http://StackOverflow.com/Questions/216995/How-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a -unix-linu –
Diese Funktion wurde in 1.9.0 hinzugefügt [siehe meine Antwort unten] (/ a/30084612/436287) – onlynone