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