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

Suggestions for git-sed #859

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 14 additions & 5 deletions bin/git-sed
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/usr/bin/env bash

# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: spacewander <https://github.com/spacewander>
# SPDX-FileCopyrightText: Paul Wise <https://github.com/pabs3>
# SPDX-FileCopyrightText: Richard Fearn <https://github.com/richardfearn>
# SPDX-FileCopyrightText: anarcat <https://github.com/anarcat>
# SPDX-FileCopyrightText: 2020 Max Mehl <https://mehl.mx>

usage() {
cat <<EOF
usage: git sed [ -c ] [ -f <flags> ] <search> <replacement> [ <flags> ]
Expand All @@ -8,6 +15,8 @@ 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.

Also runs git commit if -c is provided.

Man page: https://github.com/tj/git-extras/blob/master/man/git-sed.md
EOF
}

Expand Down Expand Up @@ -90,15 +99,15 @@ case "$all" in
esac

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)
need_bak=$(sed -i s/hello/world/ "$(mktemp -t "$(basename "$0")".XXXXXXX)" > /dev/null 2>&1 || echo true)

if [ "$need_bak" ]; then
command="git grep -lz '$search' $pathspec | xargs -0$r sed -i '' 's$sep$search$sep$replacement$sep$flags'"
command="git grep -lzE '$search' $pathspec | xargs -0$r sed -i -E '' '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"
git grep -lzE "$search" $pathspec | xargs -0"$r" sed -i -E '' "s$sep$search$sep$replacement$sep$flags"
else
command="git grep -lz '$search' $pathspec | xargs -0$r sed -i 's$sep$search$sep$replacement$sep$flags'"
command="git grep -lzE '$search' $pathspec | xargs -0$r sed -i -E '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"
git grep -lzE "$search" $pathspec | xargs -0"$r" sed -i -E "s$sep$search$sep$replacement$sep$flags"
fi
do_commit