mirror of
https://github.com/tj/git-extras.git
synced 2026-09-10 07:26:17 -04:00
Merge pull request #816 from spacewander/git-sed-pathspec
git-sed: limit paths via pathspec
This commit is contained in:
commit
265b03e400
|
|
@ -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.
|
||||
|
|
|
|||
15
bin/git-sed
15
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
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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> <flags>
|
||||
|
|
@ -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> -- <pathspec></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é <<a href="mailto:anarcat@debian.org" data-bare-link="true">anarcat@debian.org</a>> from
|
||||
<p>Written by Antoine Beaupré <<a href="mailto:anarcat@debian.org" data-bare-link="true">anarcat@debian.org</a>> 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>
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
<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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue