-
Notifications
You must be signed in to change notification settings - Fork 11
/
git-url-rewrite
executable file
·168 lines (142 loc) · 3.96 KB
/
git-url-rewrite
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
export PS4="+\D{%Y/%m/%d %T} \${BASH_SOURCE/\$HOME/\~}@\${LINENO}(\${FUNCNAME[0]}): "
me=`basename $0`
usage () {
# Call as: usage [EXITCODE] [USAGE MESSAGE]
exit_code=1
if [[ "$1" == [0-9] ]]; then
exit_code="$1"
shift
fi
if [ -n "$1" ]; then
echo "$*" >&2
echo
fi
cat <<EOF >&2
Usage:
$me [<file-option>] Lists all rewrite rules.
$me [<file-option>] ABBREV Lists rewrite rule(s) for ABBREV
$me [<file-option>] [-p] ABBREV FULL
Sets rewrite rule for ABBREV to FULL
-p uses pushInsteadOf, not insteadOf.
If FULL is empty string, clears the rule.
<file-option> is as for git-config, i.e. --system, --global, --local,
--file <filename>. Defaults to --global.
EOF
exit "$exit_code"
}
get_rewrites () {
git config $config --list | \
perl -lne '
if (m!^url\.(.+)\.(push)?insteadOf=(.+)$!i) {
$abbrev = sprintf "%-15s", $3;
$full = $1;
$abbrev =~ s/ {0,5}$/ PUSH/ if $2;
print("$abbrev -> $full");
}' | \
sort
}
get_rewrites_by_abbrev () {
abbrev="$1"
git config $config --list | \
perl -lne '
if (m!^url\.(.+)\.(push)?insteadOf='"${abbrev//!/\!}"'$!i) {
print(($2 ? "PUSH " : "") . $1);
}'
}
get_rewrite_by_URL () {
URL="$1"
git config $config url.$URL.$insteadOf
}
get_and_clear_rewrite () {
abbrev="$1"
full="$2"
git config $config --list | \
perl -lne '
if (m!^(url\.(.+)\.'"$insteadOf"')='"${abbrev//!/\!}"'$!i) {
print $2;
next if $2 eq "'"$full"'";
system qw(git config '"$config"' --unset), $1;
}'
}
set_rewrite () {
[ -z "$1" ] && usage "Abbreviation cannot be empty."
abbrev="$1"
full="$2"
if [ -n "$full" ]; then
existing_abbrev=$( get_rewrite_by_URL "$full" )
if [ "$abbrev" = "$existing_abbrev" ]; then
: Nothing to change
return 0
fi
if [ -n "$existing_abbrev" ]; then
echo "$existing_abbrev already points to \"$full\"; git won't"\
"allow pointing something else to it" >&2
exit 1
fi
fi
existing_full=$( get_and_clear_rewrite "$abbrev" "$full" )
if [ -z "$full" ]; then
git config $config --unset "url.$full.$insteadOf"
echo "Cleared ${push:+push }URL rewrite of $abbrev (was $existing_full)"
return
fi
git config $config "url.$full.$insteadOf" "$abbrev"
if [ -z "$existing_full" ]; then
echo "Set ${push:+push }URL rewrite of $abbrev -> $full"
elif [ "$full" != "$existing_full" ]; then
echo "Changed ${push:+push }URL rewrite of $abbrev -> $full (was $existing_full)"
fi
}
parse_opts () {
config=
while [ $# != 0 ]; do
case "$1" in
-h|--help)
usage 0
;;
--system|--global|--local)
config="$1"
shift
break
;;
-f|--file)
config="--file $2"
shift 2
break
;;
*)
break
;;
esac
done
if [ -z "$config" ]; then
#if ! git rev-parse --git-dir 2>/dev/null; then
config=--global
#fi
fi
insteadOf='insteadOf'
if [ "$1" == '-p' ]; then
push=y
insteadOf='pushInsteadOf'
shift
if [ $# != 2 ]; then
usage "-p only valid when setting"
fi
fi
ARGV=( "$@" )
}
main () {
parse_opts "$@"
set -- "${ARGV[@]}"
if [ $# = 0 ]; then
get_rewrites
elif [ $# = 1 ]; then
get_rewrites_by_abbrev "$1"
elif [ $# = 2 ]; then
set_rewrite "$@"
else
usage 1
fi
}
main "$@"