-
Notifications
You must be signed in to change notification settings - Fork 11
/
git-icing
executable file
·287 lines (251 loc) · 7.58 KB
/
git-icing
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/ruby
#
# Some extra icing on top of git cherry's tasty cake ...
#
# - Allow blacklisting of commits which should never be upstreamed,
# via the git-notes(1) mechanism. To blacklist a commit from being
# upstreamed to any branch:
#
# git notes --ref=upstreaming add -m'skip: all' $sha
#
# or to blacklist from being upstreamed to specific branches (currently
# only works when the upstream branch is explicitly provided):
#
# git notes --ref=upstreaming add -m'skip: upstream-branch' $sha
# git notes --ref=upstreaming append -m'skip: another-branch' $sha
# git notes --ref=upstreaming append -m'skip: /branch-regexp/' $sha
#
# It is strongly recommended that you also include justification for
# why this commit should not be upstreamed. You can place free-form
# text in the note, as long as the 'skip: ' line is preserved, with
# no indentation. To edit the note:
#
# git notes --ref=upstreaming edit $sha
#
# To remove from the blacklist:
#
# git notes --ref=upstreaming remove $sha
#
# To push / pull the blacklist notes between git repositories, see:
#
# http://stackoverflow.com/questions/12055303/merging-git-notes-when-there-are-merge-conflicts-in-them/
#
# - Categorise and colour-code commits. The first field of each
# line output by `git cherry' is extended to show more than just
# `+' and `-'. Run `git icing --help' to show all possibilities.
require 'optparse'
require 'open3'
require 'shellwords'
# This would make the code nicer, but let's minimise dependencies instead ...
# require 'term/ansicolor'
# include Term::ANSIColor
CATEGORIES = [
[ '+', 1, '31', 'not yet upstream' ],
[ '!', 1, '35', 'still needs to be upstreamed, tracked on a TODO list' ],
[ '.?', 1, '1;30', 'upstream *and* blacklisted?!' ],
[ '!?', 1, '1;30', 'upstream *and* marked as a TODO?' ],
[ '.', 2, '1;30', 'blacklisted - should not be pushed to this upstream' ],
[ '?', 2, '33', 'not yet upstream, with unparseable note' ],
[ '-', 3, '32', 'already upstream' ],
[ '#', 3, '36', 'already upstream with an annotation note' ],
]
CATEGORIES_BY_PREFIX = CATEGORIES.inject({}) { |h, new| h[new[0]] = new[1, 3]; h }
$verbosity = 1
$summary = false
def output(level, colour, msg)
return unless level <= $verbosity
if STDOUT.tty? and colour
msg = "\033[#{colour}m#{msg}\033[0m"
end
puts msg
end
def upstreaming_note(sha)
ENV['LANG'] = 'C'
cmd = [ 'git', 'notes', '--ref=upstreaming', 'show', sha ]
out = nil
Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread|
out = stdout.readlines().join('')
err = stderr.readlines().join('')
status = wait_thread.value
return nil if err =~ /No note found for object/
unless status.success?
raise cmd.shelljoin + " exited with status #{status.exitstatus}: #{err}"
end
unless err.empty?
raise cmd.shelljoin + " outputted to STDERR: #{err}"
end
end
out
end
def blacklisted(note, upstream_branch)
skippers = note.split("\n").grep(/^skip: (.+?)\s*$/m) { |x| $1 }
for skipper in skippers
return true if skipper == 'all'
if upstream_branch
skipper = Regexp.new($1) if skipper =~ %r{^/(.+)/$}
return true if skipper === upstream_branch
end
end
false
end
def parse_options
parser = OptionParser.new do |opts|
opts.banner = "usage: git icing [options] [git cherry args]\n\n"
opts.on("-v", "--verbosity [N]", Integer, "Set verbosity level") do |verbosity|
$verbosity = verbosity || 2
end
opts.on("-s", "--summary", Integer, "Show summary") do
$summary = true
end
end
def parser.help
rows = CATEGORIES.map do |field, level, colour, description|
field = "%-2s" % field
if STDOUT.tty?
" \e[#{colour}m#{field}\e[0m | #{level} | \e[#{colour}m#{description}\e[0m"
else
" #{field} | #{level} | #{description}"
end
end
super + <<EOF + rows.join("\n")
first | verbosity | description of
field | level | classification
------+-----------+-----------------------------------------------------
EOF
end
parser.parse!
end
def classify_line(upstream_branch, line)
case line
when /^- ([0-9a-f]{32})/
# An equivalent change with the same patch-id is already upstream
sha = $1
note = upstreaming_note(sha)
if note
if blacklisted(note, upstream_branch)
# Upstream *and* blacklisted?!
return '.?'
elsif note =~ /\bTODO\b/
# Upstream *and* marked as a TODO?!
return '!?'
else
# Upstream and has an annotation note.
return '#'
end
else
# Already upstream.
return '-'
end
when /^\+ ([0-9a-f]{32})/
# Not upstream yet
sha = $1
note = upstreaming_note(sha)
if note
if blacklisted(note, upstream_branch)
# We never want to upstream this
return '.'
elsif note =~ /\bTODO\b/
# Not upstream but tracked on a TODO list elsewhere
return '!'
else
# Note had unrecognised format
return '?'
end
else
# Not upstream and no note
return '+'
end
else
# Unrecognised output; leave alone
return nil
end
end
def category_colour(category)
matched = CATEGORIES_BY_PREFIX[category]
abort "Couldn't understand category '#{category}'" unless matched
return matched[1]
end
def category_level(category)
matched = CATEGORIES_BY_PREFIX[category]
return 1 unless matched
return matched[0]
end
def cherry(upstream_branch)
totals = Hash.new(0)
totals[:all] = 0
IO.popen(["git", "cherry", "-v"] + ARGV) do |out|
out.each_line do |line|
line.chomp!
category = classify_line(upstream_branch, line)
colour = nil
if category
line[0] = category
if STDOUT.tty?
colour = category_colour(category)
end
end
level = category_level(category)
output level, colour, line
totals[line[0..1].strip] += 1
totals[:all] += 1
end
end
exitcode = $?.exitstatus
exit(exitcode) unless exitcode == 0
return totals
end
def status_description(status)
CATEGORIES_BY_PREFIX[status][2] or raise "unrecognised status '#{status}'"
end
def needs_work?(status)
case status
when '-', '.'
false
when '+', '!', /^.\?$/
true
else
raise "unrecognised status '#{status}'"
end
end
def show_summary(totals)
all = totals.delete(:all)
if all == 0
puts "No commits found."
exit 0
end
remaining, done = totals.sort_by { |status, count| -count }.partition {|t| needs_work?(t[0])}
totals[:done] = done .inject(0) { |acc, new| acc += new[1] }
totals[:remaining] = remaining.inject(0) { |acc, new| acc += new[1] }
total = totals[:done] + totals[:remaining]
percent = totals[:done].to_f / total * 100
puts unless $verbosity == 0
puts <<EOF
Summary
=======
#{totals[:done]} commits processed#{done.empty? ? '' : ':'}
EOF
done.each do |status, count|
puts " %4d %2s" % [count, status_description(status)]
end
puts <<EOF
#{totals[:remaining]} commits remaining#{remaining.empty? ? '' : ':'}
EOF
remaining.each do |status, count|
puts " %4d %2s" % [count, status_description(status)]
end
puts <<EOF % [totals[:done], total, percent]
Progress: %d / %d commits (%d%%)
EOF
end
def main
parse_options
upstream_branch = ARGV[0]
trap "PIPE" do
exit 1
end
totals = cherry(upstream_branch)
if $summary
show_summary(totals)
end
end
main