File: git-sed

package info (click to toggle)
git-extras 7.4.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,120 kB
  • sloc: sh: 4,312; python: 994; makefile: 146
file content (104 lines) | stat: -rwxr-xr-x 2,776 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env bash

usage() {
    cat <<EOF
usage: git sed [ -c ] [ -f <flags> ] <search> <replacement> [ <flags> ]

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.
EOF
}

# don't commit by default
do_commit() {
    true
}

pathspec=
while [ "$1" != "" ]; do
    case "$1" in
        -c|--commit)
            if git status --porcelain | grep .; then
                echo "you need to commit your changes before running with --commit"
                exit 1
            fi
            do_commit() {
                git commit -m"replace $search with $replacement

actual command:

    $command" -a
            }
            ;;
        -f|--flags)
            if [ "$2" = "" ]; then
                usage
                echo "missing argument for $1"
                exit 1
            fi
            shift
            flags=$1
            ;;
        -h|--help)
            usage
            exit
            ;;
        --)
            pathspec="$*"
            break
            ;;
        -*)
            usage
            echo "unknown flag: $1"
            exit 1
            ;;
        *)
            if [ "$search" = "" ]; then
                search="$1"
            elif [ "$replacement" = "" ]; then
                replacement="$1"
            elif [ "$flags" = "" ]; then
                flags="$1"
            else
                usage
                echo "too many arguments: $1"
                exit 1
            fi
            ;;
    esac
    shift
done

all="$search$replacement$flags"
case "$all" in
    */*)
        ascii="$(for((i=32;i<=127;i++)) do printf '%b' "\\$(printf '%03o' "$i")"; done)"
        escaped="${all//-/\\-}"
        escaped="${escaped//[/\\[}"
        sep="$(printf '%s' "$ascii" | tr -d "$escaped")"
        sep="$(printf %.1s "$sep")"
        if [ "$sep" = "" ] ; then
            echo 'could not find an unused character for sed separator character'
            exit 1
        fi
        ;;
    *)
        sep=/
        ;;
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)

if [ "$need_bak" ]; then
    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' $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