From 4b9f1619f6b272a22fa23ef23428483ef5a897d6 Mon Sep 17 00:00:00 2001
From: spacewander
Date: Fri, 3 Jan 2020 18:22:34 +0800
Subject: [PATCH] git-sed: limit paths via pathspec
---
Commands.md | 7 +++++++
bin/git-sed | 15 +++++++++++----
man/git-sed.1 | 16 ++++++++++++----
man/git-sed.html | 18 +++++++++++++-----
man/git-sed.md | 12 ++++++++++--
5 files changed, 53 insertions(+), 15 deletions(-)
diff --git a/Commands.md b/Commands.md
index d9d501e..b78bb19 100644
--- a/Commands.md
+++ b/Commands.md
@@ -1044,6 +1044,13 @@ git undo 3
Run grep as directed but replace the given files with the pattern.
+For example,
+```bash
+$ git sed 'this' 'that'
+$ git sed 'this' 'that' g
+$ git sed 'this' 'that' -- path/ path2/
+```
+
## git setup
Set up a git repository (if one doesn't exist), add all files, and make an initial commit. `dir` defaults to the current working directory.
diff --git a/bin/git-sed b/bin/git-sed
index c9137bd..7410159 100755
--- a/bin/git-sed
+++ b/bin/git-sed
@@ -16,6 +16,7 @@ do_commit() {
true
}
+pathspec=
while [ "X$1" != "X" ]; do
case "$1" in
-c|--commit)
@@ -44,6 +45,10 @@ actual command:
usage
exit
;;
+ --)
+ pathspec="$*"
+ break
+ ;;
-*)
usage
echo "unknown flag: $1"
@@ -88,10 +93,12 @@ r=$(xargs -r false < /dev/null > /dev/null 2>&1 && echo r)
need_bak=$(sed -i s/hello/world/ "$(git_extra_mktemp)" > /dev/null 2>&1 || echo true)
if [ "$need_bak" ]; then
- 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"
+ command="git grep -lz '$search' $pathspec | xargs -0$r sed -i '' 's$sep$search$sep$replacement$sep$flags'"
+ # shellcheck disable=SC2086
+ git grep -lz "$search" $pathspec | xargs -0"$r" sed -i '' "s$sep$search$sep$replacement$sep$flags"
else
- 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"
+ command="git grep -lz '$search' $pathspec | xargs -0$r sed -i 's$sep$search$sep$replacement$sep$flags'"
+ # shellcheck disable=SC2086
+ git grep -lz "$search" $pathspec | xargs -0"$r" sed -i "s$sep$search$sep$replacement$sep$flags"
fi
do_commit
diff --git a/man/git-sed.1 b/man/git-sed.1
index 9084569..552eda8 100644
--- a/man/git-sed.1
+++ b/man/git-sed.1
@@ -1,13 +1,13 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
-.TH "GIT\-SED" "1" "May 2018" "" "Git Extras"
+.TH "GIT\-SED" "1" "January 2020" "" "Git Extras"
.
.SH "NAME"
\fBgit\-sed\fR \- replace patterns in git\-controlled files
.
.SH "SYNOPSIS"
-\fBgit\-sed\fR [ \-c ] [ \-f \fIflags\fR ] \fIsearch\fR \fIreplacement\fR [ \fIflags\fR ]
+\fBgit\-sed\fR [ \-c ] [ \-f \fIflags\fR ] \fIsearch\fR \fIreplacement\fR [ \fIflags\fR ] [ \-\- \fIpathspec\fR ]
.
.SH "DESCRIPTION"
Run git grep and then send results to sed for replacement with the given flags, if they are provided via \-f or as the third argument\.
@@ -19,7 +19,7 @@ Also runs git commit if \-c is provided\.
\-c
.
.P
-commit the resulting changes with a standard commit message detailing the exact command ran\. will fail if there are unstaged changes\.
+commit the resulting changes with a standard commit message detailing the exact command ran\. It will fail if there are unstaged changes\.
.
.P
\-f
@@ -39,6 +39,12 @@ the pattern passed to grep and to the first part of the sed expression\.
.P
the replacement passed to sed, the second part of the sed expression\.
.
+.P
+\-\-
+.
+.P
+limit the paths which will be applied on\. Read https://git\-scm\.com/docs/gitglossary\.html#Documentation/gitglossary\.txt\-aiddefpathspecapathspec for the supported patterns of pathspec\.
+.
.SH "EXAMPLES"
.
.nf
@@ -51,11 +57,13 @@ $ git sed \-c \'do_stuff\' \'stuff\'
$ git sed \-f g do_stuff stuff
# \.\. g is actually pretty important, otherwise you will miss some
# stuff!
+$ git sed \'my_method\' \'do_stuff\' \-\- lake/
+# \.\.\. only replace \'my_method\' occurs under lake/\.
.
.fi
.
.SH "AUTHOR"
-Written by Antoine Beaupré <\fIanarcat@debian\.org\fR> from inspiration by https://github\.com/da\-x/git\-search\-replace and http://stackoverflow\.com/questions/9651898/is\-there\-a\-git\-sed\-or\-equivalent
+Written by Antoine Beaupré <\fIanarcat@debian\.org\fR> from inspiration by https://github\.com/da\-x/git\-search\-replace and https://stackoverflow\.com/questions/9651898/is\-there\-a\-git\-sed\-or\-equivalent
.
.SH "REPORTING BUGS"
<\fIhttps://github\.com/tj/git\-extras/issues\fR>
diff --git a/man/git-sed.html b/man/git-sed.html
index 0ce0a8a..f2694a1 100644
--- a/man/git-sed.html
+++ b/man/git-sed.html
@@ -76,7 +76,7 @@
SYNOPSIS
-git-sed [ -c ] [ -f flags ] search replacement [ flags ]
+git-sed [ -c ] [ -f flags ] search replacement [ flags ] [ -- pathspec ]
DESCRIPTION
@@ -90,7 +90,7 @@ given flags, if they are provided via -f or as the third argument.
-c
commit the resulting changes with a standard commit message
- detailing the exact command ran. will fail if there are unstaged
+ detailing the exact command ran. It will fail if there are unstaged
changes.
<flags>
@@ -107,6 +107,12 @@ given flags, if they are provided via -f or as the third argument.
the replacement passed to sed, the second part of the sed expression.
+ -- <pathspec>
+
+ limit the paths which will be applied on.
+ Read https://git-scm.com/docs/gitglossary.html#Documentation/gitglossary.txt-aiddefpathspecapathspec
+ for the supported patterns of pathspec.
+
EXAMPLES
$ git sed 'my_function' 'do_stuff'
@@ -117,13 +123,15 @@ $ git sed -c 'do_stuff' 'stuff'
$ git sed -f g do_stuff stuff
# .. g is actually pretty important, otherwise you will miss some
# stuff!
+$ git sed 'my_method' 'do_stuff' -- lake/
+# ... only replace 'my_method' occurs under lake/.
AUTHOR
-Written by Antoine Beaupré <anarcat@debian.org> from
+
Written by Antoine Beaupré <anarcat@debian.org> from
inspiration by https://github.com/da-x/git-search-replace and
-http://stackoverflow.com/questions/9651898/is-there-a-git-sed-or-equivalent
+https://stackoverflow.com/questions/9651898/is-there-a-git-sed-or-equivalent
REPORTING BUGS
@@ -136,7 +144,7 @@ http://stackoverflow.com/questions/9651898/is-there-a-git-sed-or-equivalent
diff --git a/man/git-sed.md b/man/git-sed.md
index 6096d9e..eaaced7 100644
--- a/man/git-sed.md
+++ b/man/git-sed.md
@@ -3,7 +3,7 @@ git-sed(1) -- replace patterns in git-controlled files
## SYNOPSIS
-`git-sed` [ -c ] [ -f ] [ ]
+`git-sed` [ -c ] [ -f ] [ ] [ -- ]
## DESCRIPTION
@@ -17,7 +17,7 @@ Also runs git commit if -c is provided.
-c
commit the resulting changes with a standard commit message
- detailing the exact command ran. will fail if there are unstaged
+ detailing the exact command ran. It will fail if there are unstaged
changes.
<flags>
@@ -34,6 +34,12 @@ Also runs git commit if -c is provided.
the replacement passed to sed, the second part of the sed expression.
+ -- <pathspec>
+
+ limit the paths which will be applied on.
+ Read https://git-scm.com/docs/gitglossary.html#Documentation/gitglossary.txt-aiddefpathspecapathspec
+ for the supported patterns of pathspec.
+
## EXAMPLES
$ git sed 'my_function' 'do_stuff'
@@ -44,6 +50,8 @@ Also runs git commit if -c is provided.
$ git sed -f g do_stuff stuff
# .. g is actually pretty important, otherwise you will miss some
# stuff!
+ $ git sed 'my_method' 'do_stuff' -- lake/
+ # ... only replace 'my_method' occurs under lake/.
## AUTHOR