-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgit-graph.rb
executable file
·333 lines (281 loc) · 8.32 KB
/
git-graph.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
#!/usr/bin/env ruby
require 'rubygems'
require 'grit'
require 'fast_xs'
$commits = {}
def find_children (head_commit, children, visited)
commits = [head_commit]
while not commits.empty? do
if children.size > 0 and children.size % 1000 == 0 then
puts "#processed child info for #{children.size} commits"
end
commit = commits.slice! 0
if visited.has_key? commit.id then
next
end
visited[commit.id] = true
commit.parents.each do |c|
if not children.has_key? c.id
children[c.id] = []
end
if children[c.id].count{|com| com.id == commit.id} == 0
children[c.id].push commit
end
#find_children(c, children)
commits.push c
end
end
end
def plot_tree (head_commit, children, boring, plotted, decorations)
to_plot = [[head_commit, []]]
while not to_plot.empty? do
nextdata = to_plot.slice! 0
commit = nextdata[0]
boring = nextdata[1]
if plotted.has_key? commit.id
next
end
if is_interesting(commit, children, decorations)
make_node(commit, decorations)
boring = []
else
boring += [commit]
end
parents_to_plot = []
commit.parents.each do |c|
if is_interesting(c, children, decorations)
if is_interesting(commit, children, decorations)
make_edge(commit, c)
else
puts "#boring commit #{commit.id}, interesting parent #{c.id}"
make_elision(boring, c)
end
boring = [] #TODO not quite right ... need sub-boring list within inner loop
else
if is_interesting(commit, children, decorations)
make_edge_to_elision(commit, c)
end
end
parents_to_plot = [[c, boring]] + parents_to_plot
#plot_tree(c, children, boring, plotted, decorations)
end
to_plot = parents_to_plot + to_plot
if commit.parents.length == 0
make_elision(boring, nil)
end
plotted[commit.id] = true
end
end
def is_interesting(commit, children, decorations, neighbor=0)
#merge or branch point
if commit.parents.length > 1
#puts "int true: #{commit.id} mult parents"
return true
end
if children.include? commit.id and children[commit.id].length > 1
#puts "int true: #{commit.id} mult children"
#puts "int true: #{commit.id} child 1: #{children[commit.id][0]}"
#puts "int true: #{commit.id} child 2: #{children[commit.id][2]}"
return true
end
#decorated
if decorations.has_key? commit.id
#puts "int true: #{commit.id} decorated: #{decorations[commit.id]}"
return true
end
if neighbor <= 0
return false
end
#parent is interesting
commit.parents.each do |p|
if is_interesting(p, children, decorations, neighbor - 1)
return true
end
end
#child is interesting
children[commit.id].each do |c|
if is_interesting(c, children, decorations, neighbor - 1)
return true
end
end
return false
end
def nodes_for_interesting(commit, children, shown={})
if shown.has_key? commit.id
return
end
if is_interesting(commit, children)
make_node(commit, {})
shown[commit.id] = true
end
commit.parents.each do |p|
nodes_for_interesting(p, children, shown)
end
end
#def plot_tags(repo)
# repo.tags.each do |tag|
# puts "\"#{tag.name}\" -> \"#{tag.commit.id.slice 0,7}\";"
# puts "\"#{tag.name}\" [shape=box, style=filled, color = yellow];"
# end
#end
# print "\""
# print commit.message.gsub(%r|\n|, "\\n")
# puts "\" -> \"#{commit.id.slice 0,7}\" [arrowhead=dot, color= lightgray, arrowtail=vee];"
# print "\""
# print commit.message.gsub(%r|\n|, "\\n")
# puts "\" [shape=box, fontname=courier, fontsize = 8, color=lightgray, fontcolor=lightgray];"
# puts "\"#{commit.id.slice 0,7}\" -> \"#{c.id.slice 0,7}\";"
def id_for(commit)
commit.id.slice 0,6
end
def wrap_text(txt, col = 25)
txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,
"\\1\\3\n")
end
def log_for(commit)
commit_msg = commit.message.gsub(%r|git-svn-id: .*$|, "")
cutoff = 297
if commit_msg.length > cutoff+3
commit_msg = commit_msg.slice(0,cutoff) + " ... (#{commit_msg.length-cutoff} longer)"
end
esc_log(wrap_text(commit_msg + " [" + id_for(commit) + "]")).gsub(%r|\n|, "<br/>")
end
def fixed(str)
"<font face=\"Courier\">#{str}</font>"
end
def small(str)
"<font point-size=\"9\">#{str}</font>"
end
def smaller(str)
"<font point-size=\"8\">#{str}</font>"
end
def fmt_decor(d)
case
when d.is_a?(Grit::Tag) then color = "gold3"
when d.is_a?(Grit::Head) then color = "forestgreen"
else color = "orange3"
end
"<font color=\"#{color}\">#{d.name}</font>"
end
def color(commit)
r = commit.id.slice(0,2).hex
g = commit.id.slice(2,2).hex
b = commit.id.slice(4,2).hex
col = [r,g,b].collect{|c| [c, 192].min.to_s(16).rjust(2,"0")} * ""
"color=\"##{col}\""
end
def make_node(commit, decorations, prefix="")
label = smaller fixed log_for commit
if decorations.has_key? commit.id
label = decorations[commit.id].collect{|d| fmt_decor d} * "<br/>"
end
fill = ""
if not decorations.has_key? commit.id
fill = "style=filled fillcolor=gray75"
end
puts "\"#{prefix}#{commit.id}\" [label=<<font>#{label}</font>> #{fill}];"
end
def edge_weight(parent, child)
1.0 - child.parents.index{|p| p.id == parent.id}.to_f / child.parents.length
end
def make_edge(c1, c2)
puts "\"#{c2.id}\" -> \"#{c1.id}\" [weight=#{edge_weight(c2, c1)} #{color(c2)}];"
end
def make_edge_to_elision(commit, first_boring)
puts "\"elide.#{first_boring.id}\" -> \"#{commit.id}\" [weight=#{edge_weight(first_boring,commit)} #{color(first_boring)}];"
end
def make_elision(boring_commits, interesting_commit)
if boring_commits.length <= 0
return
end
if boring_commits.length == 1
make_node(boring_commits[0], {}, "elide.")
else
# since we're traversing backwards in time by following parent links,
# the boring_commits list is in reverse chronological order
# (see issue 2)
rangeids = smaller fixed "#{id_for(boring_commits.last)}..#{id_for(boring_commits.first)}"
rangedesc = small "#{boring_commits.size} commits"
fill = "style=filled fillcolor=gray75"
puts "\"elide.#{boring_commits.first.id}\" [label=<<font>#{rangedesc}<br/>#{rangeids}</font>> #{fill}];"
end
if not interesting_commit.nil?
puts "\"#{interesting_commit.id}\" -> \"elide.#{boring_commits.first.id}\" [weight=#{edge_weight(interesting_commit,boring_commits.first)} #{color(interesting_commit)}];"
end
end
if ARGV[1] == "--svg"
#escape twice so that xml entities end up correct in svg files
def esc_log(log)
return log.fast_xs.fast_xs
end
else
def esc_log(log)
return log.fast_xs
end
end
#test command:
#for t in svg png; do
# ./git-graph.rb test --${t} | dot -T${t} -otest.${t} /dev/stdin;
#done
if ARGV[0] == "test"
puts "Digraph test { rankdir=LR;"
class TestCommit
def initialize(message, id)
@message = message
@id = id
end
attr_accessor :message
attr_accessor :id
end
make_node(TestCommit.new("msg", "plain"), {})
make_node(TestCommit.new("msg with entities: & < > "", "ent"), {})
make_node(TestCommit.new("msg with \"quotes\"", "quot"), {})
puts "}"
exit()
end
repo = Grit::Repo.new(ARGV[0]);
decorated = (repo.branches + repo.remotes + repo.tags).collect{|r| r.commit}
decorated.reject!{|ref|
begin
ref.parents
false
rescue NoMethodError
puts "#omitting #{ref} that doesn't know its parents"
true
end}
puts "##{decorated.length} decorations"
children = {}
visited = {}
decorated.each do |c|
puts "#finding children for #{c.id}"
find_children(c, children, visited)
end
decorations = {}
repo.branches.each do |b|
puts "#noting decoration info for branch #{b.name}"
if not decorations.has_key? b.commit.id
decorations[b.commit.id] = []
end
decorations[b.commit.id].push b
end
repo.remotes.each do |r|
puts "#noting decoration info for remote branch #{r.name}"
if not decorations.has_key? r.commit.id
decorations[r.commit.id] = []
end
decorations[r.commit.id].push r
end
repo.tags.each do |t|
puts "#noting decoration info for tag #{t.name}"
if not decorations.has_key? t.commit.id
decorations[t.commit.id] = []
end
decorations[t.commit.id].push t
end
puts "Digraph Git { rankdir=LR;"
plotted={}
decorated.each do |c|
plot_tree(c, children, [], plotted, decorations)
#nodes_for_interesting(c, children)
end
puts "}"