Skip to content

Instantly share code, notes, and snippets.

@pvdb
Last active June 14, 2020 07:55
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 pvdb/19301203c466b26fbe14b32ba56ac3ed to your computer and use it in GitHub Desktop.
Save pvdb/19301203c466b26fbe14b32ba56ac3ed to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# INSTALLATION
#
# ln -s ${PWD}/git-subst $(brew --prefix)/bin/
#
FROM="$1" ;
TO="$2" ;
[ -z "${FROM}" ] && { # check command-line options
>&2 printf 'Replace <from> with <to>:\n' ;
>&2 printf ' git subst <from> <to> [<file>...]\n' ;
>&2 printf '\n' ;
>&2 printf 'Delete <pattern>:\n' ;
>&2 printf ' git subst <pattern> '"''"' [<file>...]\n' ;
>&2 printf '\n' ;
exit 1 ;
}
shift ; # exclude ${FROM} from [<file>...] list
shift ; # exclude ${TO} from [<file>...] list
DELIM=$'\a' ; # use 'BEL' as delimiter in sed addresses and commands
MATCH="\\${DELIM}${FROM}${DELIM}" ; # /<from>/
SUBST="s${DELIM}${FROM}${DELIM}${TO}${DELIM}g" ; # s/<from>/<to>/g
git grep -l "${FROM}" "$@" | while read -r FILE ; do
sed -i "" -e "${MATCH}{ ${SUBST}; }" "${FILE}" ;
done
# That's all Folks!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment