Last active
August 20, 2024 15:01
-
-
Save pvdb/19301203c466b26fbe14b32ba56ac3ed to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env bash | |
# | |
# INSTALLATION | |
# | |
# ln -s ${PWD}/git-subst $(brew --prefix)/bin/ | |
# sudo ln -s ${PWD}/git-subst /usr/local/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