-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcommit-email.rb
executable file
·399 lines (340 loc) · 9.98 KB
/
commit-email.rb
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env ruby
require "optparse"
require "ostruct"
require "nkf"
CommitEmailInfo = Struct.new(
:author,
:author_email,
:revision,
:entire_sha256,
:date,
:log,
:branch,
:diffs,
:added_files, :deleted_files, :updated_files,
:added_dirs, :deleted_dirs, :updated_dirs,
)
class GitInfoBuilder
GitCommandFailure = Class.new(RuntimeError)
def initialize(repo_path)
@repo_path = repo_path
end
def build(oldrev, newrev, refname)
diffs = build_diffs(oldrev, newrev)
info = CommitEmailInfo.new
info.author = git_show(newrev, format: '%an')
info.author_email = normalize_email(git_show(newrev, format: '%aE'))
info.revision = newrev[0...10]
info.entire_sha256 = newrev
info.date = Time.at(Integer(git_show(newrev, format: '%at')))
info.log = git_show(newrev, format: '%B')
info.branch = git('rev-parse', '--symbolic', '--abbrev-ref', refname).strip
info.diffs = diffs
info.added_files = find_files(diffs, status: :added)
info.deleted_files = find_files(diffs, status: :deleted)
info.updated_files = find_files(diffs, status: :modified)
info.added_dirs = [] # git does not deal with directory
info.deleted_dirs = [] # git does not deal with directory
info.updated_dirs = [] # git does not deal with directory
info
end
private
# Force git-svn email address to @ruby-lang.org to avoid email bounce by invalid email address.
def normalize_email(email)
if email.match(/\A[^@]+@\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/) # git-svn
svn_user, _ = email.split('@', 2)
"#{svn_user}@ruby-lang.org"
else
email
end
end
def find_files(diffs, status:)
files = []
diffs.each do |path, values|
if values.keys.first == status
files << path
end
end
files
end
# SVN version:
# {
# "filename" => {
# "[modified|added|deleted|copied|property_changed]" => {
# type: "[modified|added|deleted|copied|property_changed]",
# body: "diff body", # not implemented because not used
# added: Integer,
# deleted: Integer,
# }
# }
# }
def build_diffs(oldrev, newrev)
diffs = {}
numstats = git('diff', '--numstat', oldrev, newrev).lines.map { |l| l.strip.split("\t", 3) }
git('diff', '--name-status', oldrev, newrev).each_line do |line|
status, path, _newpath = line.strip.split("\t", 3)
diff = build_diff(path, numstats)
case status
when 'A'
diffs[path] = { added: { type: :added, **diff } }
when 'M'
diffs[path] = { modified: { type: :modified, **diff } }
when 'C'
diffs[path] = { copied: { type: :copied, **diff } }
when 'D'
diffs[path] = { deleted: { type: :deleted, **diff } }
when /\AR/ # R100 (which does not exist in git.ruby-lang.org's git 2.1.4)
# TODO: implement something
else
$stderr.puts "unexpected git diff status: #{status}"
end
end
diffs
end
def build_diff(path, numstats)
diff = { added: 0, deleted: 0 } # :body not implemented because not used
line = numstats.find { |(_added, _deleted, file, *)| file == path }
return diff if line.nil?
added, deleted, _ = line
if added
diff[:added] = Integer(added)
end
if deleted
diff[:deleted] = Integer(deleted)
end
diff
end
def git_show(revision, format:)
git('show', "--pretty=#{format}", '--no-patch', revision).strip
end
def git(*args)
command = ['git', '-C', @repo_path, *args]
output = with_gitenv { IO.popen(command, &:read) }
unless $?.success?
raise GitCommandFailure, "failed to execute '#{command.join(' ')}':\n#{output}"
end
output
end
def with_gitenv
orig = ENV.to_h.dup
begin
ENV['LANG'] = 'C'
ENV.delete('GIT_DIR')
yield
ensure
ENV.replace(orig)
end
end
end
CommitEmail = Module.new
class << CommitEmail
SENDMAIL = ENV.fetch('SENDMAIL', '/usr/sbin/sendmail')
private_constant :SENDMAIL
def parse(args)
options = OpenStruct.new
options.error_to = nil
options.viewvc_uri = nil
opts = OptionParser.new do |opts|
opts.separator('')
opts.on('-e', '--error-to [TO]',
'Add [TO] to to address when error is occurred') do |to|
options.error_to = to
end
opts.on('--viewer-uri [URI]',
'Use [URI] as URI of revision viewer') do |uri|
options.viewer_uri = uri
end
opts.on_tail('--help', 'Show this message') do
puts opts
exit
end
end
return opts.parse!(args), options
end
def main(repo_path, to, rest)
args, options = parse(rest)
infos = args.each_slice(3).flat_map do |oldrev, newrev, refname|
revisions = IO.popen(['git', 'log', '--reverse', '--pretty=%H', "#{oldrev}^..#{newrev}"], &:read).lines.map(&:strip)
revisions[0..-2].zip(revisions[1..-1]).map do |old, new|
GitInfoBuilder.new(repo_path).build(old, new, refname)
end
end
infos.each do |info|
next if info.branch.start_with?('notes/')
puts "#{info.branch}: #{info.revision} (#{info.author})"
from = make_from(name: info.author, email: "[email protected]")
sendmail(to, from, make_mail(to, from, info, viewer_uri: options.viewer_uri))
end
end
def sendmail(to, from, mail)
IO.popen([SENDMAIL, to], 'w') do |f|
f.print(mail)
end
unless $?.success?
raise "Failed to run `#{SENDMAIL} #{to}` with: '#{mail}'"
end
end
private
def b_encode(str)
NKF.nkf('-WwM', str)
end
def make_body(info, viewer_uri:)
body = ''
body << "#{info.author}\t#{format_time(info.date)}\n"
body << "\n"
body << " New Revision: #{info.revision}\n"
body << "\n"
body << " #{viewer_uri}#{info.revision}\n"
body << "\n"
body << " Log:\n"
body << info.log.lstrip.gsub(/^\t*/, ' ').rstrip
body << "\n\n"
body << added_dirs(info)
body << added_files(info)
body << deleted_dirs(info)
body << deleted_files(info)
body << modified_dirs(info)
body << modified_files(info)
[body.rstrip].pack('M')
end
def format_time(time)
time.strftime('%Y-%m-%d %X %z (%a, %d %b %Y)')
end
def changed_items(title, type, items)
rv = ''
unless items.empty?
rv << " #{title} #{type}:\n"
rv << items.collect {|item| " #{item}\n"}.join('')
end
rv
end
def changed_files(title, files)
changed_items(title, 'files', files)
end
def added_files(info)
changed_files('Added', info.added_files)
end
def deleted_files(info)
changed_files('Removed', info.deleted_files)
end
def modified_files(info)
changed_files('Modified', info.updated_files)
end
def changed_dirs(title, files)
changed_items(title, 'directories', files)
end
def added_dirs(info)
changed_dirs('Added', info.added_dirs)
end
def deleted_dirs(info)
changed_dirs('Removed', info.deleted_dirs)
end
def modified_dirs(info)
changed_dirs('Modified', info.updated_dirs)
end
def changed_dirs_info(info, uri)
rev = info.revision
(info.added_dirs.collect do |dir|
" Added: #{dir}\n"
end + info.deleted_dirs.collect do |dir|
" Deleted: #{dir}\n"
end + info.updated_dirs.collect do |dir|
" Modified: #{dir}\n"
end).join("\n")
end
def diff_info(info, uri)
info.diffs.collect do |key, values|
[
key,
values.collect do |type, value|
case type
when :added
command = 'cat'
rev = "?revision=#{info.revision}&view=markup"
when :modified, :property_changed
command = 'diff'
prev_revision = (info.revision.is_a?(Integer) ? info.revision - 1 : "#{info.revision}^")
rev = "?r1=#{info.revision}&r2=#{prev_revision}&diff_format=u"
when :deleted, :copied
command = 'cat'
rev = ''
else
raise "unknown diff type: #{value[:type]}"
end
link = [uri, key.sub(/ .+/, '') || ''].join('/') + rev
desc = ''
[desc, link]
end
]
end
end
def make_header(to, from, info)
headers = []
headers << x_author(info)
headers << x_repository(info)
headers << x_revision(info)
headers << x_id(info)
headers << 'Mime-Version: 1.0'
headers << 'Content-Type: text/plain; charset=utf-8'
headers << 'Content-Transfer-Encoding: quoted-printable'
headers << "From: #{from}"
headers << "To: #{to}"
headers << "Subject: #{make_subject(info)}"
headers.find_all do |header|
/\A\s*\z/ !~ header
end.join("\n")
end
def make_subject(info)
subject = ''
subject << "#{info.revision}"
subject << " (#{info.branch})"
subject << ': '
subject << info.log.lstrip.lines.first.to_s.strip
b_encode(subject)
end
# https://tools.ietf.org/html/rfc822#section-4.1
# https://tools.ietf.org/html/rfc822#section-6.1
# https://tools.ietf.org/html/rfc822#appendix-D
# https://tools.ietf.org/html/rfc2047
def make_from(name:, email:)
if name.ascii_only?
escaped_name = name.gsub(/["\\\n]/) { |c| "\\#{c}" }
%Q["#{escaped_name}" <#{email}>]
else
escaped_name = "=?UTF-8?B?#{NKF.nkf('-WwMB', name)}?="
%Q[#{escaped_name} <#{email}>]
end
end
def x_author(info)
"X-SVN-Author: #{b_encode(info.author)}"
end
def x_repository(info)
'X-SVN-Repository: XXX'
end
def x_id(info)
"X-SVN-Commit-Id: #{info.entire_sha256}"
end
def x_revision(info)
"X-SVN-Revision: #{info.revision}"
end
def make_mail(to, from, info, viewer_uri:)
"#{make_header(to, from, info)}\n#{make_body(info, viewer_uri: viewer_uri)}"
end
end
repo_path, to, *rest = ARGV
begin
CommitEmail.main(repo_path, to, rest)
rescue StandardError => e
$stderr.puts "#{e.class}: #{e.message}"
$stderr.puts e.backtrace
_, options = CommitEmail.parse(rest)
to = options.error_to
CommitEmail.sendmail(to, to, <<-MAIL)
From: #{to}
To: #{to}
Subject: Error
#{$!.class}: #{$!.message}
#{[email protected]("\n")}
MAIL
exit 1
end