From ee14220f664df9f03635d521ebe03c386cd645c9 Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Fri, 20 Apr 2018 11:24:05 +0800 Subject: [PATCH 1/3] git-sed: Allow the flags to be passed as a third argument This is more in-line with how sed itself works. --- bin/git-sed | 6 ++++-- man/git-sed.md | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bin/git-sed b/bin/git-sed index 9ba4f81..a18fc36 100755 --- a/bin/git-sed +++ b/bin/git-sed @@ -2,10 +2,10 @@ usage() { cat < ] +usage: git sed [ -c ] [ -f ] [ ] Run git grep and then send results to sed for replacement with the -given flags, if -f is provided. +given flags, if they are provided via -f or as the third argument. Also runs git commit if -c is provided. EOF @@ -54,6 +54,8 @@ actual command: search="$1" elif [ "X$replacement" = "X" ]; then replacement="$1" + elif [ "X$flags" = "X" ]; then + flags="$1" else usage echo "too many arguments: $1" diff --git a/man/git-sed.md b/man/git-sed.md index 19d1f48..844c2af 100644 --- a/man/git-sed.md +++ b/man/git-sed.md @@ -3,12 +3,12 @@ git-sed(1) -- replace patterns in git-controlled files ## SYNOPSIS -`git-sed` [ -c ] [ -f ] +`git-sed` [ -c ] [ -f ] [ ] ## DESCRIPTION Run git grep and then send results to sed for replacement with the -given flags, if -f is provided. +given flags, if they are provided via -f or as the third argument. Also runs git commit if -c is provided. @@ -20,6 +20,7 @@ Also runs git commit if -c is provided. detailing the exact command ran. will fail if there are unstaged changes. + <flags> -f <flags> will use the given regex flags in the sed command (for example "g" From c27a3505dc46b22fe3c21417a27ec48e12262950 Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Fri, 20 Apr 2018 11:26:45 +0800 Subject: [PATCH 2/3] git-sed: pass the -r option to the xargs command Prevents running sed when the search pattern does not match any files. This results in sed printing an unnessecary warning: $ git sed foo bar sed: no input files --- bin/git-sed | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/git-sed b/bin/git-sed index a18fc36..76c0b67 100755 --- a/bin/git-sed +++ b/bin/git-sed @@ -66,6 +66,7 @@ actual command: 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" +r=$(xargs -r false < /dev/null > /dev/null 2>&1 && echo r) +command="git grep -lz '$search' | xargs -0$r sed -i 's/$search/$replacement/$flags'" +git grep -lz "$search" | xargs -0"$r" sed -i "s/$search/$replacement/$flags" do_commit From 9785199036985f3692847208edcd828df233812d Mon Sep 17 00:00:00 2001 From: Paul Wise Date: Fri, 20 Apr 2018 12:13:21 +0800 Subject: [PATCH 3/3] git-sed: discover a separator when the / character is used in arguments Prevents sed from returning an error for arguments containing filenames: $ git sed src/foo.c src/bar.c sed: -e expression #1, char 13: unknown option to `s' --- bin/git-sed | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/bin/git-sed b/bin/git-sed index 76c0b67..9fcba85 100755 --- a/bin/git-sed +++ b/bin/git-sed @@ -66,7 +66,22 @@ actual command: shift done +all="$search$replacement$flags" +case "$all" in + */*) + ascii="$(for((i=32;i<=127;i++)) do printf '%b' "\\$(printf '%03o' "$i")"; done)" + sep="$(printf '%s' "$ascii" | tr -d "$all")" + sep="$(printf %.1s "$sep")" + if [ "X$sep" = "X" ] ; then + echo 'could not find an unused character for sed separator character' + exit 1 + fi + ;; + *) + sep=/ + ;; +esac r=$(xargs -r false < /dev/null > /dev/null 2>&1 && echo r) -command="git grep -lz '$search' | xargs -0$r sed -i 's/$search/$replacement/$flags'" -git grep -lz "$search" | xargs -0"$r" sed -i "s/$search/$replacement/$flags" +command="git grep -lz '$search' | xargs -0$r sed -i 's$sep$search$sep$replacement$sep$flags'" +git grep -lz "$search" | xargs -0"$r" sed -i "s$sep$search$sep$replacement$sep$flags" do_commit