Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git-sed: limit paths via pathspec #816

Merged
merged 1 commit into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 11 additions & 4 deletions bin/git-sed
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ do_commit() {
true
}

pathspec=
while [ "X$1" != "X" ]; do
case "$1" in
-c|--commit)
Expand Down Expand Up @@ -44,6 +45,10 @@ actual command:
usage
exit
;;
--)
pathspec="$*"
break
;;
-*)
usage
echo "unknown flag: $1"
Expand Down Expand Up @@ -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
16 changes: 12 additions & 4 deletions man/git-sed.1
Original file line number Diff line number Diff line change
@@ -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\.
Expand All @@ -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>
Expand All @@ -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
Expand All @@ -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>
Expand Down
18 changes: 13 additions & 5 deletions man/git-sed.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions man/git-sed.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
Expand All @@ -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'
Expand All @@ -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

Expand Down