This repository has been archived by the owner on Sep 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changemailer.rb
114 lines (99 loc) · 2.6 KB
/
changemailer.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
require 'rugged'
require 'mail'
require 'mustache'
require 'yaml'
config = YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'config.yml'))
directory = ARGV[0]
html_mail_template =<<EOT
<html>
<head>
<style>
table.diff {
width: 100%;
border-bottom: 1px solid rgb(200, 200, 200);
margin-bottom: 20px;
}
h1 {
font-size: 12pt;
}
ins {
color: rgb(0, 70, 0);
background-color: rgb(240, 255, 250);
}
del {
color: rgb(70, 0, 0);
background-color: rgb(255, 240, 250);
}
</style>
</head>
<body>
{{#changes}}
<details>
<summary>{{status}}: {{old_filename}}{{#new_filename}}<br>to {{.}}{{/new_filename}}</summary>
<table class="diff">
<tr>
<td width="33%"><pre>{{left}}</pre></td>
<td width="33%"><pre>{{right}}</pre></td>
<td width="33%"><pre>{{{patch}}}</pre></td>
</tr>
</table>
</details>
{{/changes}}
</body>
</html>
EOT
text_mail_template =<<EOT
Changes to the following items:
{{#changes}}
- {{status}} {{old_filename}}{{#new_filename}} => {{.}}{{/new_filename}}
{{/changes}}
EOT
repo = Rugged::Repository.new(directory)
head_commit = repo.head.target
diff = head_commit.parents[0].diff(head_commit)
diff.find_similar!(renames: true)
changes = diff.patches.map do |patch|
delta = patch.delta
left = repo.lookup(delta.old_file[:oid]).read_raw.data rescue ""
right = repo.lookup(delta.new_file[:oid]).read_raw.data rescue ""
patch = patch.hunks.map do |h|
lines = h.lines.map do |l|
tag = case l.line_origin
when :addition
"ins"
when :deletion
"del"
else
"span"
end
"<#{tag}>#{l.content}</#{tag}>"
end
[h.header, lines]
end
{
status: delta.status,
old_filename: delta.old_file[:path],
new_filename: delta.old_file[:path] != delta.new_file[:path] ? delta.new_file[:path] : nil,
left: left,
right: right,
patch: patch.flatten.join
}
end
mail = Mail.new do
from config["mailer"]["from"]
to config["mailer"]["to"]
subject "#{diff.deltas.length} items changed in #{directory}"
end
mail.part :content_type => 'multipart/alternative' do |p|
p.text_part = Mail::Part.new do
content_type 'text/plain; charset="UTF-8'
body Mustache.render(text_mail_template, {changes: changes})
end
p.html_part = Mail::Part.new do
content_type 'text/html; charset=UTF-8'
body Mustache.render(html_mail_template, {changes: changes})
end
end
mail.attachments['changes.diff'] = {content: diff.patch, mime_type: 'text/x-diff'}
mail.delivery_method :sendmail
mail.deliver