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
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.
+$ 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/.
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