From ab40cb1dfc47dbff1234539bd52a3b6fae10fda2 Mon Sep 17 00:00:00 2001 From: Max Mehl <6170081+mxmehl@users.noreply.github.com> Date: Fri, 24 Jul 2020 17:20:14 +0200 Subject: [PATCH] Suggestions for git-sed I use this great script stand-alone and added some features. Perhaps not all feasible for upstream: * Add [REUSE Software](https://reuse.software) identifiers to ease code reuse * Add a link to the manpage, useful if used stand-alone * Replace git_extra_mktemp by built-in function * Add extended regex to grep and sed --- bin/git-sed | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/bin/git-sed b/bin/git-sed index 741015966..b1b9bec9d 100755 --- a/bin/git-sed +++ b/bin/git-sed @@ -1,5 +1,12 @@ #!/usr/bin/env bash +# SPDX-License-Identifier: MIT +# SPDX-FileCopyrightText: spacewander +# SPDX-FileCopyrightText: Paul Wise +# SPDX-FileCopyrightText: Richard Fearn +# SPDX-FileCopyrightText: anarcat +# SPDX-FileCopyrightText: 2020 Max Mehl + usage() { cat < ] [ ] @@ -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 } @@ -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