-
Notifications
You must be signed in to change notification settings - Fork 2
/
wibget.rb
305 lines (267 loc) · 7.69 KB
/
wibget.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
# = WibGet -- a minimalist, but convenient Git web frontend
#
# Copyright (C) 2008 Christian Neukirchen <purl.org/net/chneukirchen>
# Licensed under the terms of the MIT license.
require 'rack'
require 'coset'
require "grit"
class WibRepos
def initialize(repos)
@repos = repos.map { |name, dir|
[name, dir, WibGet.new(File.expand_path(dir))]
}
@map = Rack::URLMap.new(
@repos.map { |name, dir, wib|
["/" + name, Rack::Cascade.new([Rack::File.new(wib.repo.git.git_dir),
wib])]
} << ["/", method(:index)])
end
def call(env)
@map.call(env)
end
def index(env)
req = Rack::Request.new(env)
res = Rack::Response.new
res.write WibGet::HEADER % "Index of WibGet repositories"
res.write "<h1>WibGet repositories:</h1>"
@repos.map { |name, dir, wib|
res.write %{<li><a href="#{name}/">#{name}</a>, #{wib.repo.description}</li>}
}
res.write "</body></html>"
res.finish
end
end
class WibGet < Coset
PER_PAGE = 10
HEADER = <<'EOF'
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>%s</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script>
jQuery(function($){
$(".entry h2").click(function() {
$(this).parent().find("pre").toggleClass("hidden")
})
$(".entry h2 a").click(function(e) {
e.stopPropagation()
})
$(".tree dt").click(function() {
$(this).next("dd").toggleClass("hidden")
})
$("#files").click(function() {
if ($(".tree dd.hidden").length > 1)
$(".tree dd").removeClass("hidden")
else
$(".tree dd").addClass("hidden")
})
$("#logs").click(function() {
if ($(".entry pre.hidden").length > 1)
$(".entry pre").removeClass("hidden")
else
$(".entry pre").addClass("hidden")
})
})
</script>
<style>
body { font: 10px monospace; }
.entry h2 {
font-weight: normal;
white-space: pre;
}
h2 { clear: left; }
pre { background-color: #eee; }
dl, dt {
margin: 0;
padding: 0;
}
dd {
margin: 0;
padding: 0 0 0 4em;
}
.info { color: #777; }
.del { color: red; }
.ins { color: green; }
.entry h2, .tree dt, #logs, #files {
cursor: pointer;
}
.hidden {
display: none;
}
</style>
</head>
<body>
EOF
attr_accessor :repo
def initialize(repo)
@repo = Grit::Repo.new(repo)
@dir = repo
@name = File.basename(@dir)
end
def traverse(res, tree)
tree.contents.sort_by { |c| c.name.downcase }.each { |c|
case c
when Grit::Blob
res.write "<div><a href='#{c.id[0..6]}'>#{c.name}</a></div>"
when Grit::Tree
res.write "<dl>"
res.write "<dt>#{c.name}/</dt>"
res.write "<dd class='hidden'>"
traverse(res, c)
res.write "</dd>"
res.write "</dl>"
else
raise TypeError, "Unknown tree element: #{c.inspect}"
end
}
end
def rev2url(rev)
rev.gsub("/", "--")
end
def url2rev(url)
url.gsub("--", "/")
end
GET("/") {
run("/master", "GET")
}
GET("/{id}") {
@id = url2rev @id
if @id =~ /\A\(([\w\/^~-]+)\)/
topic = url2rev $1
@id = $'
end
id = @repo.git.rev_parse({:verify => true}, @id)
begin
tree = @repo.tree(id)
rescue RuntimeError
blob = @repo.blob(id)
res['Content-Type'] = blob.mime_type
res.write blob.data
return
end
if tree.contents.empty?
res.status = 404
res.write "<h1>Not found: #{Rack::Utils.escape_html id}</h1>"
return
end
@offset = [(req["offset"] || 0).to_i, 0].max
log = @repo.git.log({ :cc => true,
:p => true,
:pretty => 'format:%s (%aN, %ar) %ai %h%n%b',
:shortstat => true,
:z => true,
:skip => @offset,
:max_count => PER_PAGE},
topic ? "^" + topic : "", id)
niceid = @repo.git.describe({ :contains => true,
:always => true,
:all => true,
:abbrev => 7},
id)
if @id != niceid
title = "#{@name}: #{@id} (#{niceid} = #{id})"
else
title = "#{@name}: #{@id} (#{id})"
end
res.write HEADER % title
res.write "<h1>#{title}</h1>"
unless @repo.description =~ /^Unnamed repository/
res.write "<p>#{Rack::Utils.escape_html @repo.description}</p>"
end
res.write '<h2>Heads: '
heads = {}
@repo.heads.sort_by { |head| head.name }.each { |head|
commit = head.commit
heads[head.name] = commit.id
res.write %{<a href="#{rev2url head.name}" title="#{commit.authored_date.xmlschema} by #{commit.author}">#{head.name}</a> } rescue nil
}
res.write "</h2>"
unless @repo.tags.empty?
res.write '<h2>Tags: '
@repo.tags.sort_by { |tag| tag.name }.each { |tag|
commit = tag.commit
res.write %{<a href="#{rev2url tag.name}" title="#{commit.authored_date.xmlschema} by #{commit.author}">#{tag.name}</a> } rescue nil
}
res.write "</h2>"
end
unless @repo.remotes.empty?
res.write '<h2>Remotes: '
@repo.remotes.sort_by { |remote| remote.name }.each { |remote|
commit = remote.commit
# Don't show remotes that are mere copie
next if heads[remote.name.split("/").last] == commit.id
res.write %{<a href="#{rev2url remote.name}" title="#{commit.authored_date.xmlschema} by #{commit.author}">#{remote.name}</a> } rescue nil
}
res.write "</h2>"
end
res.write '<h2 id="files">Files:</h2>'
res.write '<div class="tree">'
traverse(res, tree)
res.write '</div>'
res.write '<div class="log">'
res.write '<h2 id="logs">Change log '
if topic
res.write 'without ' + topic + ' '
res.write %{[<a href="#{rev2url @id}">full log</a>]}
else
res.write %{[<a href="(master)#{rev2url @id}">topic log</a>]}
end
res.write ':</h2>'
c = 0
entries = log.split("\0")
entries.each_with_index { |desc, i|
next if desc =~ /\Adiff --/
if entries[i+1] =~ /\Adiff --/
diff = entries[i+1] || ""
end
c += 1
desc.gsub!(/\A(.*) \((.*), (.*)\) (.*?) (\w+)$/, '<a href="\5">\5</a>: <strong>\1</strong> (\2, <span title="\4">\3</span>)')
title, desc = desc.split("\n", 2)
diff = Rack::Utils.escape_html diff
diff.gsub!(/^-.*/, '<span class="del">\&</span>')
diff.gsub!(/^\+.*/, '<span class="ins">\&</span>')
if diff =~ /\Adiff --cc/
puts "CC"
diff.gsub!(/^ -.*/, '<span class="del">\&</span>')
diff.gsub!(/^ \+.*/, '<span class="ins">\&</span>')
end
diff.gsub!(/^(diff|index|@@).*/, '<span class="info">\&</span>')
res.write <<EOF
<div class="entry">
<h2>#{title}</h2>
<pre class="hidden">#{desc}</pre>
<pre class="hidden diff">#{diff}</pre>
</div>
EOF
}
res.write '</div>'
if @offset > PER_PAGE
res.write %{<a href="?offset=#{@offset-PER_PAGE}"><<</a>}
elsif @offset > 0
res.write %{<a href="?"><<</a>}
else
res.write "<<"
end
res.write " "
if c >= PER_PAGE
res.write %{<a href="?offset=#{@offset+PER_PAGE}">>></a>}
else
res.write ">>"
end
res.write " </body>"
res.write "</html>"
}
GET("/{rev}/{path:all}") {
@rev = url2rev @rev
blob = @repo.tree(@rev) / @path
unless blob
res.status = 404
res.write "<h1>Not found: #{Rack::Utils.escape_html "#@rev:#@path"}</h1>"
else
res['Content-Type'] = blob.mime_type
res.write blob.data
end
}
end