-
Notifications
You must be signed in to change notification settings - Fork 7
/
tg-patch.sh
225 lines (211 loc) · 5.11 KB
/
tg-patch.sh
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/sh
# TopGit - A different patch queue manager
# Copyright (C) Petr Baudis <[email protected]> 2008
# All rights reserved.
# GPLv2
name=
head_from=
binary=
quiet=
fixfrom=
fromaddr=
gec=0
bool="$(git config --get --bool topgit.from 2>/dev/null)" || gec=$?
if [ $gec -eq 128 ]; then
fromaddr="$(git config --get topgit.from 2>/dev/null)" || :
if [ "$fromaddr" = "quiet" ]; then
quiet=1
else
[ -z "$fromaddr" ] || fixfrom=1
fi
elif [ $gec -eq 0 ]; then
[ "$bool" = "false" ] || fixfrom=1
fi
## Parse options
while [ -n "$1" ]; do
arg="$1"
case "$arg" in
--)
case "$2" in
-*)
shift; break;;
*)
break;;
esac;;
-|-h|--help)
echo "Usage: ${tgname:-tg} [...] patch [-q] [-i | -w] [--binary] [<name>] [--] [<git-diff-tree-option>...]" >&2
exit 1;;
-i|-w)
[ -z "$head_from" ] || die "-i and -w are mutually exclusive"
head_from="$arg";;
--binary)
binary=1;;
-q|--quiet)
quiet=1;;
--no-from)
fixfrom= fromaddr=;;
--from)
fixfrom=1;;
--from=*)
fixfrom=1 fromaddr="${1#--from=}";;
-?*)
if v_verify_topgit_branch test "$arg" -f; then
[ -z "$name" ] || die "name already specified ($name)"
name="$arg"
else
break
fi;;
*)
[ -z "$name" ] || die "name already specified ($name)"
name="$arg";;
esac
shift
done
head="$(git symbolic-ref -q HEAD)" || :
head="${head#refs/heads/}"
[ -n "$name" ] ||
name="${head:-HEAD}"
v_verify_topgit_branch name "$name"
base_rev="$(git rev-parse --short --verify "refs/$topbases/$name^0" -- 2>/dev/null)" ||
die "not a TopGit-controlled branch"
if [ -n "$head_from" ] && [ "$name" != "$head" ]; then
die "$head_from makes only sense for the current branch"
fi
[ -z "$head_from" ] || ensure_work_tree
usesob=
[ -z "$fixfrom" ] || [ -n "$fromaddr" ] || {
fromaddr="$(git var GIT_AUTHOR_IDENT)" || exit
usesob=1
}
# We now collect the rest of the code in this file into a function
# so we can redirect the output to the pager.
output()
{
# put out the commit message
# and put an empty line out, if the last one in the message was not an empty line
# and put out "---" if the commit message does not have one yet
result=0
cat_file "refs/heads/$name:.topmsg" $head_from |
awk -v "fixfrom=$fixfrom" -v "fromaddr=$fromaddr" -v "usesob=$usesob" '
function trimfb(s) {
sub(/^[ \t]+/, "", s)
sub(/[ \t]+$/, "", s)
return s
}
function fixident(val, fixmt, _name, _email) {
val = trimfb(val)
if (!fixmt && val == "") return ""
_name=""
_email=""
if ((leftangle = index(val, "<")) > 0) {
_name=trimfb(substr(val, 1, leftangle - 1))
_email=substr(val, leftangle+1)
sub(/>[^>]*$/, "", _email)
_email=trimfb(_email)
} else {
if ((atsign = index(val, "@")) > 0) {
_name=trimfb(substr(val, 1, atsign - 1))
_email=trimfb(val)
} else {
_name=trimfb(val)
if (_name != "") _email="-"
}
}
if (!fixmt && _name == "" && _email == "") return ""
if (_name == "") _name = "-"
if (_email == "") _email = "-"
return _name " <" _email ">"
}
BEGIN {
hdrline = 0
sawfrom = 0
sobname = ""
if (fixfrom) {
fromaddr = fixident(fromaddr)
if (fromaddr == "" && !usesob) fixfrom = 0
}
inhdr = 1
bodyline = 0
}
inhdr && /^[Ff][Rr][Oo][Mm][ \t]*:/ {
val = $0
sub(/^[^:]*:/, "", val)
val = fixident(val)
if (val != "") sawfrom = 1
if (val != "" || !fixfrom) hdrs[++hdrline] = $0
next
}
inhdr && /^[ \t]*$/ {
inhdr = 0
next
}
inhdr { hdrs[++hdrline] = $0; next; }
function writehdrs() {
if (!sawfrom && fixfrom && fromaddr != "") {
print "From: " fromaddr
sawfrom = 1
}
for (i=1; i <= hdrline; ++i) print hdrs[i]
print ""
}
/^---/ { has_3dash=1 }
usesob && /^[Ss][Ii][Gg][Nn][Ee][Dd]-[Oo][Ff][Ff]-[Bb][Yy][ \t]*:[ \t]*[^ \t]/ {
val = $0
sub(/^[^:]*:/, "", val)
val = fixident(val)
if (val != "") fromaddr=val
}
{
need_empty = 1
if ($0 == "") need_empty = 0
body[++bodyline] = $0
}
END {
writehdrs()
for (i = 1; i <= bodyline; ++i) print body[i]
if (need_empty) print ""
if (!has_3dash) print "---"
exit sawfrom ? 0 : 67 # EX_NOUSER
}
' || result=$?
if [ "$result" = "67" ]; then
[ -n "$quiet" ] ||
echo "### tg: missing From: in .topmsg, 'git am' will barf (use --from to add)" >&2
result=0
fi
[ "${result:-0}" = "0" ] || exit "$result"
v_pretty_tree b_tree -t "$name" -b
v_pretty_tree t_tree -t "$name" $head_from
if [ $b_tree = $t_tree ]; then
echo "No changes."
else
hasdd=
for a; do
[ "$a" != "--" ] || { hasdd=1; break; }
done
if [ -z "$hasdd" ]; then
git diff-tree -p --stat --summary ${binary:+--binary} "$@" $b_tree $t_tree
else
cmd="git diff-tree -p --stat --summary ${binary:+--binary}"
while [ $# -gt 0 ] && [ "$1" != "--" ]; do
cmd="$cmd $(quotearg "$1")"
shift
done
cmd="$cmd $(quotearg "$b_tree") $(quotearg "$t_tree")"
while [ $# -gt 0 ]; do
cmd="$cmd $(quotearg "$1")"
shift
done
eval "$cmd"
fi
fi
echo ''
echo '-- '
depon="$(cat_file "refs/heads/$name:.topdeps" $head_from 2>/dev/null | paste -s -d ' ' -)"
echo "$tgname: ($base_rev..) $name${depon:+ (depends on: $depon)}"
branch_contains "refs/heads/$name" "refs/$topbases/$name" ||
echo "$tgname: The patch is out-of-date wrt. the base! Run \`$tgdisplay update\`."
}
USE_PAGER_TYPE=diff
page output "$@"
# ... and then we run it through the pager with the page function