mirror of
https://github.com/tj/git-extras.git
synced 2026-09-10 07:26:17 -04:00
Otherwise filenames with spaces result in errors:
$ cat "file with spaces in name"
old
$ git sed old new
sed: can't read file: No such file or directory
sed: can't read with: No such file or directory
sed: can't read spaces: No such file or directory
sed: can't read in: No such file or directory
sed: can't read name: No such file or directory
70 lines
1.5 KiB
Bash
Executable file
70 lines
1.5 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
usage() {
|
|
cat <<EOF
|
|
usage: git sed [ -c ] [ -f <flags> ] <search> <replacement>
|
|
|
|
Run git grep and then send results to sed for replacement with the
|
|
given flags, if -f is provided.
|
|
|
|
Also runs git commit if -c is provided.
|
|
EOF
|
|
}
|
|
|
|
# don't commit by default
|
|
do_commit() {
|
|
true
|
|
}
|
|
|
|
while [ "X$1" != "X" ]; do
|
|
case "$1" in
|
|
-c|--commit)
|
|
if git status --porcelain | grep .; then
|
|
echo "you need to commit your changes before running with --commit"
|
|
exit 1
|
|
fi
|
|
do_commit() {
|
|
git commit -m"replace $search with $replacement
|
|
|
|
actual command:
|
|
|
|
$command" -a
|
|
}
|
|
;;
|
|
-f|--flags)
|
|
if [ "X$2" = "X" ]; then
|
|
usage
|
|
echo "missing argument for $1"
|
|
exit 1
|
|
fi
|
|
shift
|
|
flags=$1
|
|
;;
|
|
-h|--help)
|
|
usage
|
|
exit
|
|
;;
|
|
-*)
|
|
usage
|
|
echo "unknown flag: $1"
|
|
exit 1
|
|
;;
|
|
*)
|
|
if [ "X$search" = "X" ]; then
|
|
search="$1"
|
|
elif [ "X$replacement" = "X" ]; then
|
|
replacement="$1"
|
|
else
|
|
usage
|
|
echo "too many arguments: $1"
|
|
exit 1
|
|
fi
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
command="git grep -lz '$search' | xargs -0 sed -i 's/$search/$replacement/$flags'"
|
|
git grep -lz "$search" | xargs -0 sed -i "s/$search/$replacement/$flags"
|
|
do_commit
|