Merge pull request #816 from spacewander/git-sed-pathspec

git-sed: limit paths via pathspec
This commit is contained in:
罗泽轩 2020-01-08 10:08:37 +08:00 committed by GitHub
commit 265b03e400
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 15 deletions

View file

@ -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.

View file

@ -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

View file

@ -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
<flags> \-f <flags>
@ -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
\-\- <pathspec>
.
.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>

View file

@ -76,7 +76,7 @@
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p><code>git-sed</code> [ -c ] [ -f <var>flags</var> ] <var>search</var> <var>replacement</var> [ <var>flags</var> ]</p>
<p><code>git-sed</code> [ -c ] [ -f <var>flags</var> ] <var>search</var> <var>replacement</var> [ <var>flags</var> ] [ -- <var>pathspec</var> ]</p>
<h2 id="DESCRIPTION">DESCRIPTION</h2>
@ -90,7 +90,7 @@ given flags, if they are provided via -f or as the third argument.</p>
<p> -c</p>
<p> 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.</p>
<p> &lt;flags&gt;
@ -107,6 +107,12 @@ given flags, if they are provided via -f or as the third argument.</p>
<p> the replacement passed to sed, the second part of the sed expression.</p>
<p> -- &lt;pathspec&gt;</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.</p>
<h2 id="EXAMPLES">EXAMPLES</h2>
<pre><code>$ 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/.
</code></pre>
<h2 id="AUTHOR">AUTHOR</h2>
<p>Written by Antoine Beaupré &lt;<a href="&#109;&#97;&#105;&#108;&#x74;&#x6f;&#x3a;&#x61;&#110;&#x61;&#x72;&#99;&#97;&#x74;&#x40;&#100;&#x65;&#x62;&#105;&#97;&#110;&#46;&#111;&#x72;&#103;" data-bare-link="true">&#97;&#110;&#x61;&#x72;&#99;&#x61;&#x74;&#x40;&#x64;&#x65;&#98;&#105;&#97;&#x6e;&#x2e;&#111;&#114;&#x67;</a>&gt; from
<p>Written by Antoine Beaupré &lt;<a href="&#x6d;&#x61;&#105;&#108;&#116;&#111;&#58;&#x61;&#x6e;&#97;&#x72;&#99;&#97;&#x74;&#x40;&#x64;&#101;&#98;&#x69;&#x61;&#110;&#46;&#x6f;&#x72;&#x67;" data-bare-link="true">&#97;&#110;&#97;&#x72;&#99;&#97;&#x74;&#64;&#100;&#x65;&#98;&#105;&#97;&#x6e;&#x2e;&#111;&#x72;&#103;</a>&gt; from
inspiration by https://github.com/da-x/git-search-replace and
http://stackoverflow.com/questions/9651898/is-there-a-git-sed-or-equivalent</p>
https://stackoverflow.com/questions/9651898/is-there-a-git-sed-or-equivalent</p>
<h2 id="REPORTING-BUGS">REPORTING BUGS</h2>
@ -136,7 +144,7 @@ http://stackoverflow.com/questions/9651898/is-there-a-git-sed-or-equivalent</p>
<ol class='man-decor man-foot man foot'>
<li class='tl'></li>
<li class='tc'>May 2018</li>
<li class='tc'>January 2020</li>
<li class='tr'>git-sed(1)</li>
</ol>

View file

@ -3,7 +3,7 @@ git-sed(1) -- replace patterns in git-controlled files
## SYNOPSIS
`git-sed` [ -c ] [ -f <flags> ] <search> <replacement> [ <flags> ]
`git-sed` [ -c ] [ -f <flags> ] <search> <replacement> [ <flags> ] [ -- <pathspec> ]
## 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.
&lt;flags&gt;
@ -34,6 +34,12 @@ Also runs git commit if -c is provided.
the replacement passed to sed, the second part of the sed expression.
-- &lt;pathspec&gt;
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