fd038355d959a058f82392e3c79369ef8560360b
1#!/usr/bin/perl
2
3# This file is licensed under the GPL v2, or a later version
4# (C) 2005, Kay Sievers <kay.sievers@vrfy.org>
5# (C) 2005, Christian Gierke <ch@gierke.de>
6
7use strict;
8use warnings;
9use CGI qw(:standard :escapeHTML);
10use CGI::Carp qw(fatalsToBrowser);
11
12my $cgi = new CGI;
13my $gitbin = "/home/kay/bin/git";
14my $gitroot = "/home/kay/public_html";
15my $gittmp = "/tmp";
16my $myself = $cgi->url(-relative => 1);
17
18my $project = $cgi->param("project") || "";
19my $action = $cgi->param("action") || "";
20my $hash = $cgi->param("hash") || "";
21my $parent = $cgi->param("parent") || "";
22my $view_back = $cgi->param("view_back") || 60*60*24;
23my $projectroot = "$gitroot/$project";
24$ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/.git/objects";
25
26$hash =~ s/[^0-9a-fA-F]//g;
27$parent =~ s/[^0-9a-fA-F]//g;
28$project =~ s/[^0-9a-zA-Z\-\._]//g;
29
30sub git_header {
31 print $cgi->header(-type => 'text/html; charset: utf-8');
32print <<EOF;
33<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
34<html>
35<head>
36 <title>git - $project $action</title>
37 <style type="text/css">
38 body { font-family: sans-serif; font-size: 12px; margin:25px; }
39 div.body { border-width:1px; border-style:solid; border-color:#D9D8D1; }
40 div.head1 { font-size:20px; padding:8px; background-color: #D9D8D1; font-weight:bold; }
41 div.head1 a:visited { color:#0000cc; }
42 div.head1 a:hover { color:#880000; }
43 div.head1 a:active { color:#880000; }
44 div.head2 { padding:8px; }
45 div.head2 a:visited { color:#0000cc; }
46 div.head2 a:hover { color:#880000; }
47 div.head2 a:active { color:#880000; }
48 div.main { padding:8px; font-family: sans-serif; font-size: 12px; }
49 table { padding:0px; margin:0px; width:100%; }
50 tr { vertical-align:top; }
51 td { padding:8px; margin:0px; font-family: sans-serif; font-size: 12px; }
52 td.head1 { background-color: #D9D8D1; font-weight:bold; }
53 td.head1 a { color:#000000; text-decoration:none; }
54 td.head1 a:hover { color:#880000; text-decoration:underline; }
55 td.head1 a:visited { color:#000000; }
56 td.head2 { background-color: #EDECE6; font-family: monospace; font-size:12px; }
57 td.head3 { background-color: #EDECE6; font-size:10px; }
58 div.add { color: #008800; }
59 div.subtract { color: #CC0000; }
60 div.diff_head { color: #990099; }
61 a { color:#0000cc; }
62 a:hover { color:#880000; }
63 a:visited { color:#880000; }
64 a:active { color:#880000; }
65 </style>
66</head>
67<body>
68EOF
69 print "<div class=\"body\">\n";
70 print "<div class=\"head1\">";
71 print "<a href=\"http://kernel.org/pub/software/scm/git/\"><img src=\"git_logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/></a>";
72 print $cgi->a({-href => "$myself"}, "projects");
73 if ($project ne "") {
74 print " / " . $cgi->a({-href => "$myself?project=$project&action=log&view_back=" . 60*60*24}, $project);
75 }
76 if ($action ne "") {
77 print " / $action";
78 }
79 print "</div>\n";
80}
81
82sub git_footer {
83 print "</div>";
84 print $cgi->end_html();
85}
86
87sub git_diff {
88 my $old_name = shift;
89 my $new_name = shift;
90 my $old = shift;
91 my $new = shift;
92
93 my $label_old = "/dev/null";
94 my $label_new = "/dev/null";
95 my $tmp_old = "/dev/null";
96 my $tmp_new = "/dev/null";
97
98 if ($old ne "") {
99 open my $fd2, "> $gittmp/$old";
100 open my $fd, "-|", "$gitbin/cat-file", "blob", $old;
101 while (my $line = <$fd>) {
102 print $fd2 $line;
103 }
104 close $fd2;
105 close $fd;
106 $tmp_old = "$gittmp/$old";
107 $label_old = "a/$old_name";
108 }
109
110 if ($new ne "") {
111 open my $fd2, "> $gittmp/$new";
112 open my $fd, "-|", "$gitbin/cat-file", "blob", $new;
113 while (my $line = <$fd>) {
114 print $fd2 $line;
115 }
116 close $fd2;
117 close $fd;
118 $tmp_new = "$gittmp/$new";
119 $label_new = "b/$new_name";
120 }
121
122 open my $fd, "-|", "/usr/bin/diff", "-L", $label_old, "-L", $label_new, "-u", "-p", $tmp_old, $tmp_new;
123 while (my $line = <$fd>) {
124 my $char = substr($line,0,1);
125 print '<div class="add">' if $char eq '+';
126 print '<div class="subtract">' if $char eq '-';
127 print '<div class="diff_head">' if $char eq '@';
128 print escapeHTML($line);
129 print '</div>' if $char eq '+' or $char eq '-' or $char eq '@';
130 }
131 close $fd;
132 #unlink("$gittmp/$new");
133 #unlink("$gittmp/$old");
134}
135
136if ($project eq "") {
137 open my $fd, "-|", "ls", "-1", $gitroot;
138 my (@path) = map { chomp; $_ } <$fd>;
139 close $fd;
140 git_header();
141 print "<br/><br/><div class=\"main\">\n";
142 foreach my $line (@path) {
143 if (-e "$gitroot/$line/.git/HEAD") {
144 print $cgi->a({-href => "$myself?project=$line"}, $line) . "<br/>\n";
145 }
146 }
147 print "<br/></div>";
148 git_footer();
149 exit;
150}
151
152if ($action eq "") {
153 print $cgi->redirect("$myself?project=$project&action=log&view_back=$view_back");
154 exit;
155}
156
157if ($action eq "blob") {
158 git_header();
159 print "<br/><br/><div class=\"main\">\n";
160 print "<pre>\n";
161 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
162 my $nr;
163 while (my $line = <$fd>) {
164 $nr++;
165 print "$nr\t" . escapeHTML($line);;
166 }
167 close $fd;
168 print "</pre>\n";
169 print "<br/></div>";
170 git_footer();
171} elsif ($action eq "tree") {
172 if ($hash eq "") {
173 open my $fd, "$projectroot/.git/HEAD";
174 my $head = <$fd>;
175 chomp $head;
176 close $fd;
177 open $fd, "-|", "$gitbin/cat-file", "commit", $head;
178 my $tree = <$fd>;
179 chomp $tree;
180 $tree =~ s/tree //;
181 close $fd;
182 $hash = $tree;
183 }
184 open my $fd, "-|", "$gitbin/ls-tree", $hash;
185 my (@entries) = map { chomp; $_ } <$fd>;
186 close $fd;
187 git_header();
188 print "<br/><br/><div class=\"main\">\n";
189 print "<pre>\n";
190 foreach my $line (@entries) {
191 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
192 my $t_type = $2;
193 my $t_hash = $3;
194 my $t_name = $4;
195 if ($t_type eq "blob") {
196 print "BLOB\t" . $cgi->a({-href => "$myself?project=$project&action=blob&hash=$3"}, $4) . "\n";
197 } elsif ($t_type eq "tree") {
198 print "TREE\t" . $cgi->a({-href => "$myself?project=$project&action=tree&hash=$3"}, $4) . "\n";
199 }
200 }
201 print "</pre>\n";
202 print "<br/></div>";
203 git_footer();
204} elsif ($action eq "log" || $action eq "show_log" ) {
205 open my $fd, "$projectroot/.git/HEAD";
206 my $head = <$fd>;
207 chomp $head;
208 close $fd;
209 open $fd, "-|", "$gitbin/rev-tree", $head;
210 my (@revtree) = map { chomp; $_ } <$fd>;
211 close $fd;
212 git_header();
213 print "<div class=\"head2\">\n";
214 print "view ";
215 print $cgi->a({-href => "$myself?project=$project&action=log&view_back=" . 60*60*24}, "last day") . " | ";
216 print $cgi->a({-href => "$myself?project=$project&action=log&view_back=" . 60*60*24*7}, "week") . " | ";
217 print $cgi->a({-href => "$myself?project=$project&action=log&view_back=" . 60*60*24*30}, "month") . " | ";
218 print $cgi->a({-href => "$myself?project=$project&action=log&view_back=" . 60*60*24*365}, "year") . " | ";
219 print $cgi->a({-href => "$myself?project=$project&action=log&view_back=-1"}, "all") . "<br/>\n";
220 print "<br/><br/>\n";
221 print "</div>\n";
222 print "<table cellspacing=\"0\" class=\"log\">\n";
223 foreach my $rev (reverse sort @revtree) {
224 if (!($rev =~ m/^([0-9]+) ([0-9a-fA-F]+).* ([0-9a-fA-F]+)/)) {
225 last;
226 }
227 my $time = $1;
228 my $commit = $2;
229 my $parent = $3;
230 my @parents;
231 my $author;
232 my $author_name;
233 my $author_time;
234 my $author_timezone;
235 my $committer;
236 my $committer_time;
237 my $committer_timezone;
238 my $tree;
239 my $comment;
240 my $shortlog;
241 open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
242 while (my $line = <$fd>) {
243 chomp($line);
244 if ($line eq "") {
245 last;
246 }
247 if ($line =~ m/^tree (.*)$/) {
248 $tree = $1;
249 } elsif ($line =~ m/^parent (.*)$/) {
250 push @parents, $1;
251 } elsif ($line =~ m/^committer (.*>) ([0-9]+) (.*)$/) {
252 $committer = $1;
253 $committer_time = $2;
254 $committer_timezone = $3;
255 } elsif ($line =~ m/^author (.*>) ([0-9]+) (.*)$/) {
256 $author = $1;
257 $author_time = $2;
258 $author_timezone = $3;
259 $author =~ m/^(.*) </;
260 $author_name = $1;
261 }
262 }
263 $shortlog = <$fd>;
264 $shortlog = escapeHTML($shortlog);
265 $comment = $shortlog . "<br/>";
266 while (my $line = <$fd>) {
267 chomp($line);
268 $comment .= escapeHTML($line) . "<br/>\n";
269 }
270 close $fd;
271 my $age = time-$author_time;
272 if ($view_back > 0 && $age > $view_back) {
273 last;
274 }
275
276 my $age_string;
277 if ($age > 60*60*24*365*2) {
278 $age_string = int $age/60/60/24/365;
279 $age_string .= " years ago";
280 } elsif ($age > 60*60*24*365/12*2) {
281 $age_string = int $age/60/60/24/365/12;
282 $age_string .= " months ago";
283 } elsif ($age > 60*60*24*7*2) {
284 $age_string = int $age/60/60/24/7;
285 $age_string .= " weeks ago";
286 } elsif ($age > 60*60*24*2) {
287 $age_string = int $age/60/60/24;
288 $age_string .= " days ago";
289 } elsif ($age > 60*60*2) {
290 $age_string = int $age/60/60;
291 $age_string .= " hours ago";
292 } elsif ($age > 60*2) {
293 $age_string = int $age/60;
294 $age_string .= " minutes ago";
295 }
296 print "<tr>\n";
297 print "<td class=\"head1\">" . $age_string . "</td>\n";
298 print "<td class=\"head1\"><a href=\"$myself?project=$project&action=commit&hash=$commit&parent=$parent\">" . $shortlog . "</a></td>";
299 print "</tr>\n";
300 print "<tr>\n";
301 print "<td class=\"head3\">";
302 print $cgi->a({-href => "$myself?project=$project&action=diffs&hash=$commit&parent=$parent"}, "view diff") . "<br/>\n";
303 print $cgi->a({-href => "$myself?project=$project&action=commit&hash=$commit&parent=$parent"}, "view commit") . "<br/>\n";
304 print $cgi->a({-href => "$myself?project=$project&action=tree&hash=$tree"}, "view tree") . "<br/>\n";
305 print "</td>\n";
306 print "<td class=\"head2\">\n";
307 print "author " . escapeHTML($author) . " [" . gmtime($author_time) . " " . $author_timezone . "]<br/>\n";
308 print "committer " . escapeHTML($committer) . " [" . gmtime($committer_time) . " " . $committer_timezone . "]<br/>\n";
309 print "commit $commit<br/>\n";
310 print "tree $tree<br/>\n";
311 foreach my $par (@parents) {
312 print "parent $par<br/>\n";
313 }
314 print "</td>";
315 print "</tr>\n";
316 print "<tr>\n";
317 print "<td></td>\n";
318 print "<td>\n";
319 print "$comment<br/><br/>\n";
320 print "</td>";
321 print "</tr>\n";
322 }
323 print "</table>\n";
324 git_footer();
325} elsif ($action eq "commit") {
326 open my $fd, "-|", "$gitbin/cat-file", "commit", $hash;
327 my $tree = <$fd>;
328 chomp $tree;
329 $tree =~ s/tree //;
330 close $fd;
331
332 open $fd, "-|", "$gitbin/cat-file", "commit", $parent;
333 my $parent_tree = <$fd>;
334 chomp $parent_tree;
335 $parent_tree =~ s/tree //;
336 close $fd;
337
338 open $fd, "-|", "$gitbin/diff-tree", "-r", $parent_tree, $tree;
339 my (@difftree) = map { chomp; $_ } <$fd>;
340 close $fd;
341
342 git_header();
343 print "<br/><br/><div class=\"main\">\n";
344 print "<pre>\n";
345 foreach my $line (@difftree) {
346 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
347 my $op = $1;
348 my $mode = $2;
349 my $type = $3;
350 my $id = $4;
351 my $file = $5;
352 if ($type eq "blob") {
353 if ($op eq "+") {
354 print "NEW\t" . $cgi->a({-href => "$myself?project=$project&action=blob&hash=$id"}, $file) . "\n";
355 } elsif ($op eq "-") {
356 print "DEL\t" . $cgi->a({-href => "$myself?project=$project&action=blob&hash=$id"}, $file) . "\n";
357 } elsif ($op eq "*") {
358 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
359 my $old = $1;
360 my $new = $2;
361 print "CHANGED\t" . $cgi->a({-href => "$myself?project=$project&action=diff&hash=$old&parent=$new"}, $file) . "\n";
362 }
363 }
364 }
365 print "</pre>\n";
366 print "<br/></div>";
367 git_footer();
368} elsif ($action eq "diff") {
369 git_header();
370 print "<br/><br/><div class=\"main\">\n";
371 print "<pre>\n";
372 git_diff($hash, $parent, $hash, $parent);
373 print "</pre>\n";
374 print "<br/></div>";
375 git_footer();
376} elsif ($action eq "diffs") {
377 open my $fd, "-|", "$gitbin/cat-file", "commit", $hash;
378 my $tree = <$fd>;
379 chomp $tree;
380 $tree =~ s/tree //;
381 close $fd;
382
383 open $fd, "-|", "$gitbin/cat-file", "commit", $parent;
384 my $parent_tree = <$fd>;
385 chomp $parent_tree;
386 $parent_tree =~ s/tree //;
387 close $fd;
388
389 open $fd, "-|", "$gitbin/diff-tree", "-r", $parent_tree, $tree;
390 my (@difftree) = map { chomp; $_ } <$fd>;
391 close $fd;
392
393 git_header();
394 print "<br/><br/><div class=\"main\">\n";
395 print "<pre>\n";
396 foreach my $line (@difftree) {
397 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
398 my $op = $1;
399 my $mode = $2;
400 my $type = $3;
401 my $id = $4;
402 my $file = $5;
403 if ($type eq "blob") {
404 if ($op eq "+") {
405 git_diff("", $file, "", $id);
406 } elsif ($op eq "-") {
407 git_diff($file, "", $id, "");
408 } elsif ($op eq "*") {
409 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
410 git_diff($file, $file, $1, $2);
411 }
412 }
413 print "<br/>\n";
414 }
415 print "</pre>\n";
416 print "<br/></div>";
417 print "<br/></div>";
418 git_footer();
419} else {
420 git_header();
421 print "<br/><br/><div class=\"main\">\n";
422 print "unknown action\n";
423 print "<br/></div>";
424 git_footer();
425}