Skip to content

Instantly share code, notes, and snippets.

@jaysoffian
Created May 24, 2017
Embed
What would you like to do?
git gsr
#!/bin/sh
usage () {
cat >&2 <<'__USAGE__'
usage: git gsr [-P | --perl-regexp] <old> <new> [paths...]
replace all occurrances of <old> with <new> optionally limited to
<paths...> (as interpreted by git grep)
-P, --perl-regexp interpret <old> as perl regular expression;
default is to treat it as a fixed string.
__USAGE__
exit 1
}
pattern='-F'
perl='BEGIN {($old, $new) = (shift, shift)} s/\Q$old\E/$new/g'
case "$1" in
-P|--perl-regexp)
shift
pattern='-P'
perl='BEGIN {($old, $new) = (shift, shift)} s/$old/$new/g'
;;
-*) usage
;;
esac
test $# -lt 2 && usage
old=$1; new=$2; shift; shift
git grep -l -z $pattern "$old" -- "$@" |
xargs -0 perl -pi -e "$perl" "$old" "$new"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment