Skip to content

Instantly share code, notes, and snippets.

@jaysoffian
Created May 24, 2017 19:08
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 jaysoffian/0eda35a6a41f500ba5c458f02a29865d to your computer and use it in GitHub Desktop.
Save jaysoffian/0eda35a6a41f500ba5c458f02a29865d to your computer and use it in GitHub Desktop.
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