-
Notifications
You must be signed in to change notification settings - Fork 116
/
git2gv
executable file
·167 lines (145 loc) · 4.91 KB
/
git2gv
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
#!/usr/bin/env ruby
# Copyright (C) 2010 Gregoire Lejeune <gregoire.lejeune@free.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
require 'rubygems'
require 'getoptlong'
require 'graphviz'
class Git2Gv
def initialize( xGVPath, xOutFile, xOutFormat, bNothugly )
@xGVPath, @xOutFile, @xOutFormat, @bNothugly = xGVPath, xOutFile, xOutFormat.to_sym, bNothugly
end
def run
git = GraphViz.new( :G, :path => @xGVPath )
git.node[:shape => "record"]
branches.each do |branch|
git.add_nodes( branch, :style => "filled", :fillcolor => :lightgrey )
end
commits.each do |commit|
git.add_nodes(
commit[:short],
:style => "filled",
:fillcolor => :lightblue,
:label => "{ #{commit[:long]} |{ #{commit[:comment].gsub(/\{/, "\\{").gsub(/\}/, "\\}").gsub(/\|/, "\\|").gsub(/</, "\\<").gsub(/>/, "\\>")}}| { #{commit[:commiter]} | #{commit[:date]} } }"
)
end
refs.each do |from, to|
git.add_edges( from, to )
end
relations.each do |from, to|
git.add_edges( to, from, :dir => "back" )
end
git.output( @xOutFormat => @xOutFile, :nothugly => @bNothugly )
end
# private
def cmd(c, &blk)
`#{c}`.split($/).map(&blk || proc {|a| a })
end
def commits
@commits ||= begin
data = []
cmd('git log --pretty=format:"%h - %H - %s - %cn - %cd" --date=short').each do |commit|
x = commit.split( " - " )
data << {
:short => x[0],
:long => x[1],
:comment => x[2],
:commiter => x[3],
:date => x[4]
}
end
data
end
end
def relations
@rels ||= cmd("git log --pretty=format:\"%h %p\"") { |l|
c, *parents = l.split
parents.map {|p| [p, c] }
}.flatten(1)
end
def branches
@branches ||= cmd("git branch") {|b| b[2..-1] }
end
def refs
branches.inject({}) {|h,b|
h.tap { h[b] = `git log -1 #{b} --pretty=format:"%h"` }
}
end
def current_branch
cmd("git branch").select {|l| l =~ /^\*/ }.first.strip[2..-1]
end
end
def usage
puts "usage: git2gv [-Tformat] [-ofile] [-h] [-V]"
puts "-T, --output-format format Output format (default: png)"
puts " --nothugly Use nothugly if SVG output"
puts "-o, --output-file file Output file (default: STDOUT)"
puts "-p, --path Graphviz path"
puts "-V, --version Show version"
puts "-h, --help Show this usage message"
end
def version
puts "Git2GraphViz v#{GraphViz::Constants::RGV_VERSION}, (c)2010 Gregoire Lejeune <gregoire.lejeune@free.fr>"
puts ""
puts "This program is free software; you can redistribute it and/or modify"
puts "it under the terms of the GNU General Public License as published by"
puts "the Free Software Foundation; either version 2 of the License, or"
puts "(at your option) any later version."
puts ""
puts "This program is distributed in the hope that it will be useful,"
puts "but WITHOUT ANY WARRANTY; without even the implied warranty of"
puts "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
puts "GNU General Public License for more details."
puts ""
puts "You should have received a copy of the GNU General Public License"
puts "along with this program; if not, write to the Free Software"
puts "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"
end
oOpt = GetoptLong.new(
['--output-format', '-T', GetoptLong::REQUIRED_ARGUMENT],
['--output-file', '-o', GetoptLong::REQUIRED_ARGUMENT],
['--path', '-p', GetoptLong::REQUIRED_ARGUMENT],
['--nothugly', GetoptLong::NO_ARGUMENT],
['--help', '-h', GetoptLong::NO_ARGUMENT],
['--version', '-V', GetoptLong::NO_ARGUMENT]
)
xOutFormat = "png"
xOutFile = nil
xGVPath = ""
bNothugly = false
begin
oOpt.each_option do |xOpt, xValue|
case xOpt
when '--output-format'
xOutFormat = xValue
when '--output-file'
xOutFile = xValue
when '--path'
xGVPath = xValue
when '--nothugly'
bNothugly = true
when '--help'
usage( )
exit
when '--version'
version( )
exit
end
end
rescue GetoptLong::InvalidOption
usage( )
exit
end
Git2Gv.new( xGVPath, xOutFile, xOutFormat, bNothugly ).run