3 # gitweb.pl - simple web interface to track changes in git repositories
7 # (C) 2005, Kay Sievers <kay.sievers@vrfy.org>
8 # (C) 2005, Christian Gierke <ch@gierke.de>
10 # This file is licensed under the GPL v2, or a later version
14 use CGI
qw(:standard :escapeHTML);
15 use CGI
::Carp
qw(fatalsToBrowser);
19 my $projectroot = "/home/kay/public_html";
20 my $defaultprojects = ".";
21 my $gitbin = "/home/kay/bin/git";
23 my $my_url = $cgi->url();
24 my $my_uri = $cgi->url(-absolute
=> 1);
25 my $my_url_parm = $cgi->url(-path
=> 1);
26 $my_url_parm =~ s/.*$my_uri//;
35 if ($my_url_parm =~ m
#/(.+)/commit/([0-9a-fA-F]+)$#) {
39 } elsif ($my_url_parm =~ m
#/(.+)/commitdiff/([0-9a-fA-F]+)$#) {
41 $action = "commitdiff";
43 } elsif ($my_url_parm =~ m
#/(.+)/blobdiff/([0-9a-fA-F]+)/([0-9a-fA-F]+)$#) {
48 } elsif ($my_url_parm =~ m
#/(.+)/blob/([0-9a-fA-F]+)$#) {
52 } elsif ($my_url_parm =~ m
#/(.+)/tree/([0-9a-fA-F]+)$#) {
56 } elsif ($my_url_parm =~ m
#/(.+)/log/([0-9]+)$#) {
60 } elsif ($my_url_parm =~ m
#/(.+)/log$#) {
64 } elsif ($my_url_parm =~ m
#/(.+)/rss$#) {
68 } elsif ($my_url_parm =~ m
#/git-logo.png$#) {
69 print $cgi->header(-type
=> 'image/png', -expires
=> '+1d');
70 print "\211\120\116\107\015\012\032\012\000\000\000\015\111\110\104\122".
71 "\000\000\000\110\000\000\000\033\004\003\000\000\000\055\331\324".
72 "\055\000\000\000\030\120\114\124\105\377\377\377\140\140\135\260".
73 "\257\252\000\200\000\316\315\307\300\000\000\350\350\346\367\367".
74 "\366\225\014\247\107\000\000\000\163\111\104\101\124\050\317\143".
75 "\110\147\040\004\112\134\030\012\010\052\142\123\141\040\002\010".
76 "\015\151\105\254\241\241\001\060\014\223\140\066\046\122\221\261".
77 "\001\021\326\341\125\144\154\154\314\154\154\014\242\014\160\052".
78 "\142\006\052\301\142\035\263\001\002\123\244\010\350\000\003\030".
79 "\046\126\021\324\341\040\227\033\340\264\016\065\044\161\051\202".
80 "\231\060\270\223\012\021\271\105\210\301\215\240\242\104\041\006".
81 "\047\101\202\100\205\301\105\211\040\160\001\000\244\075\041\305".
82 "\022\034\232\376\000\000\000\000\111\105\116\104\256\102\140\202";
84 } elsif ($my_url_parm =~ m
#/(.+)$#) {
91 $project =~ s
#\/\.+##g;
93 $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/.git/objects";
96 print $cgi->header(-type
=> 'text/html', -charset
=> 'utf-8');
98 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
101 <title>git - $project $action</title>
102 <link rel="alternate" title="$project log" href="$my_uri/$project/rss" type="application/rss+xml"/>
103 <style type="text/css">
104 body { font-family: sans-serif; font-size: 12px; margin:25px; }
105 div.body { border-width:1px; border-style:solid; border-color:#D9D8D1; }
106 div.head1 { font-size:20px; padding:8px; background-color: #D9D8D1; font-weight:bold; }
107 div.head1 a:visited { color:#0000cc; }
108 div.head1 a:hover { color:#880000; }
109 div.head1 a:active { color:#880000; }
110 div.head2 { padding:8px; }
111 div.head2 a:visited { color:#0000cc; }
112 div.head2 a:hover { color:#880000; }
113 div.head2 a:active { color:#880000; }
114 div.title { padding:8px; background-color: #D9D8D1; font-weight:bold; }
115 div.title a { color:#000000; text-decoration:none; }
116 div.title a:hover { color:#880000; text-decoration:underline; }
117 div.title a:visited { color:#000000; }
118 table { padding:0px; margin:0px; width:100%; }
119 tr { vertical-align:top; }
120 td { padding:8px; margin:0px; font-family: sans-serif; font-size: 12px; }
121 td.head1 { background-color: #D9D8D1; font-weight:bold; }
122 td.head1 a { color:#000000; text-decoration:none; }
123 td.head1 a:hover { color:#880000; text-decoration:underline; }
124 td.head1 a:visited { color:#000000; }
125 td.head2 { background-color: #EDECE6; font-family: monospace; font-size:12px; }
126 td.head3 { background-color: #EDECE6; font-size:10px; }
127 div.signed_off { color: #a9a8a1; }
129 a:hover { color:#880000; }
130 a:visited { color:#880000; }
131 a:active { color:#880000; }
137 print "<div class=\"body\">\n";
138 print "<div class=\"head1\">";
139 print "<a href=\"http://kernel.org/pub/software/scm/git/\">" .
140 "<img src=\"$my_uri/git-logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/></a>";
141 if ($defaultprojects ne "") {
142 print $cgi->a({-href
=> "$my_uri"}, "projects") . " / ";
144 if ($project ne "") {
145 print $cgi->a({-href
=> "$my_uri/$project/log"}, $project);
153 sub git_footer_html
{
155 print $cgi->end_html();
159 open my $fd, "$projectroot/$project/.git/HEAD";
171 open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
172 while (my $line = <$fd>) {
175 if ($line =~ m/^tree (.*)$/) {
177 } elsif ($line =~ m/^parent (.*)$/) {
179 } elsif ($line =~ m/^committer (.*>) ([0-9]+) (.*)$/) {
180 $co{'committer'} = $1;
181 $co{'committer_time'} = $2;
182 $co{'committer_timezone'} = $3;
183 } elsif ($line =~ m/^author (.*>) ([0-9]+) (.*)$/) {
185 $co{'author_time'} = $2;
186 $co{'author_timezone'} = $3;
189 $co{'parents'} = \
@parents;
190 $co{'parent'} = $parents[0];
191 my (@comment) = map { chomp; $_ } <$fd>;
192 $co{'comment'} = \
@comment;
193 $co{'title'} = $comment[0];
199 my $old_name = shift || "/dev/null";
200 my $new_name = shift || "/dev/null";
204 my $tmp_old = "/dev/null";
205 my $tmp_new = "/dev/null";
206 my $old_label = "/dev/null";
207 my $new_label = "/dev/null";
209 # create temp from-file
211 open my $fd2, "> $gittmp/$old";
212 open my $fd, "-|", "$gitbin/cat-file", "blob", $old;
213 while (my $line = <$fd>) {
218 $tmp_old = "$gittmp/$old";
219 $old_label = "a/$old_name";
224 open my $fd2, "> $gittmp/$new";
225 open my $fd, "-|", "$gitbin/cat-file", "blob", $new;
226 while (my $line = <$fd>) {
231 $tmp_new = "$gittmp/$new";
232 $new_label = "b/$new_name";
235 open my $fd, "-|", "/usr/bin/diff", "-L", $old_label, "-L", $new_label, "-u", "-p", $tmp_old, $tmp_new;
236 print "<span style =\"color: #000099;\">===== ";
238 print $cgi->a({-href
=> "$my_uri/$project/blob/$old"}, $old);
244 print $cgi->a({-href
=> "$my_uri/$project/blob/$new"}, $new);
248 print " =====</span>\n";
249 while (my $line = <$fd>) {
250 my $char = substr($line,0,1);
251 print '<span style ="color: #008800;">' if $char eq '+';
252 print '<span style ="color: #CC0000;">' if $char eq '-';
253 print '<span style ="color: #990099;">' if $char eq '@';
254 print escapeHTML
($line);
255 print '</span>' if $char eq '+' or $char eq '-' or $char eq '@';
258 unlink("$gittmp/$new");
259 unlink("$gittmp/$old");
262 if ($project eq "") {
263 opendir(my $fd, "$projectroot/$defaultprojects");
264 my (@path) = grep(!/^\./, readdir($fd));
267 print "<div class=\"head2\">\n";
268 print "<br/><br/>\n";
269 foreach my $line (@path) {
270 if (-e
"$projectroot/$defaultprojects/$line/.git/HEAD") {
271 print $cgi->a({-href
=> "$my_uri/$defaultprojects/$line/log"}, $line) . "<br/>\n";
279 if ($action eq "blob") {
281 print "<br/><br/>\n";
283 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
285 while (my $line = <$fd>) {
287 print "$nr\t" . escapeHTML
($line);;
293 } elsif ($action eq "tree") {
297 open my $fd, "-|", "$gitbin/ls-tree", $hash;
298 my (@entries) = map { chomp; $_ } <$fd>;
301 print "<br/><br/>\n";
303 foreach my $line (@entries) {
304 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
305 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
309 if ($t_type eq "blob") {
310 print "BLOB\t" . $cgi->a({-href
=> "$my_uri/$project/blob/$3"}, $4) . "\n";
311 } elsif ($t_type eq "tree") {
312 print "TREE\t" . $cgi->a({-href
=> "$my_uri/$project/tree/$3"}, $4) . "\n";
318 } elsif ($action eq "log" || $action eq "rss") {
319 open my $fd, "-|", "$gitbin/rev-tree", git_head
();
320 my (@revtree) = reverse sort map { chomp; $_ } <$fd>;
323 if ($action eq "log") {
325 print "<div class=\"head2\">\n";
327 print $cgi->a({-href
=> "$my_uri/$project/log"}, "last day") . " | ";
328 print $cgi->a({-href
=> "$my_uri/$project/log/7"}, "week") . " | ";
329 print $cgi->a({-href
=> "$my_uri/$project/log/31"}, "month") . " | ";
330 print $cgi->a({-href
=> "$my_uri/$project/log/365"}, "year") . " | ";
331 print $cgi->a({-href
=> "$my_uri/$project/log/0"}, "all") . "<br/>\n";
332 print "<br/><br/>\n";
334 print "<table cellspacing=\"0\" class=\"log\">\n";
335 } elsif ($action eq "rss") {
336 print $cgi->header(-type
=> 'text/xml', -charset
=> 'utf-8');
337 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
338 "<rss version=\"0.91\">\n";
340 print "<title>$project</title>\n".
341 "<link> " . $my_url . "/$project/log</link>\n".
342 "<description>$project log</description>\n".
343 "<language>en</language>\n";
346 for (my $i = 0; $i <= $#revtree; $i++) {
347 my $rev = $revtree[$i];
348 # '1114106118 755e3010ee10dadf42a8a80770e1b115fb038d9b:1 2af17b4854036a1c2ec6c101d93c8dd1ed80d24e:1'
349 last if !($rev =~ m/^([0-9]+) ([0-9a-fA-F]+).* ([0-9a-fA-F]+)/);
354 my %co = git_commit
($commit);
355 my $age = time - $co{'committer_time'};
357 if ($age > 60*60*24*365*2) {
358 $age_string = int $age/60/60/24/365;
359 $age_string .= " years ago";
360 } elsif ($age > 60*60*24*365/12*2) {
361 $age_string = int $age/60/60/24/365/12;
362 $age_string .= " months ago";
363 } elsif ($age > 60*60*24*7*2) {
364 $age_string = int $age/60/60/24/7;
365 $age_string .= " weeks ago";
366 } elsif ($age > 60*60*24*2) {
367 $age_string = int $age/60/60/24;
368 $age_string .= " days ago";
369 } elsif ($age > 60*60*2) {
370 $age_string = int $age/60/60;
371 $age_string .= " hours ago";
372 } elsif ($age > 60*2) {
373 $age_string = int $age/60;
374 $age_string .= " minutes ago";
376 if ($action eq "log") {
377 if ($view_back > 0 && $age > $view_back*60*60*24) {
380 print "<td class=\"head1\"> Last change $age_string. </td>\n";
386 print "<td class=\"head1\">" . $age_string . "</td>\n";
387 print "<td class=\"head1\">" . $cgi->a({-href
=> "$my_uri/$project/commit/$commit"}, $co{'title'}) . "</td>";
390 print "<td class=\"head3\">";
391 print $cgi->a({-href
=> "$my_uri/$project/commit/$commit"}, "view commit") . "<br/>\n";
392 print $cgi->a({-href
=> "$my_uri/$project/commitdiff/$commit"}, "view diff") . "<br/>\n";
394 print "<td class=\"head2\">\n";
395 print "author " . escapeHTML
($co{'author'}) . " [" . gmtime($co{'author_time'}) . " " . $co{'author_timezone'} . "]<br/>\n";
396 print "committer " . escapeHTML
($co{'committer'}) . " [" . gmtime($co{'committer_time'}) . " " . $co{'committer_timezone'} . "]<br/>\n";
402 my $comment = $co{'comment'};
403 foreach my $line (@
$comment) {
404 if ($line =~ m/signed-off-by:/i) {
405 print '<div class="signed_off">' . escapeHTML
($line) . "<br/></div>\n";
407 print escapeHTML
($line) . "<br/>\n";
410 print "<br/><br/>\n";
413 } elsif ($action eq "rss") {
415 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($co{'author_time'});
416 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
417 print "<item>\n\t<title>";
418 printf("%s %02d, %02d:%02d - ", $months[$mon], $mday, $hour, $min);
419 print escapeHTML
($co{'title'}) . "</title>\n";
420 print "\t<link> " . $my_url . "/$project/commit/$commit</link>\n";
421 print "\t<description>";
422 my $comment = $co{'comment'};
423 foreach my $line (@
$comment) {
424 print escapeHTML
($line) . "\n";
426 print "\t</description>\n";
430 if ($action eq "log") {
433 } elsif ($action eq "rss") {
434 print "</channel></rss>";
436 } elsif ($action eq "commit") {
437 my %co = git_commit
($hash);
438 open my $fd, "-|", "$gitbin/diff-tree", "-r", $co{'parent'}, $hash;
439 my (@difftree) = map { chomp; $_ } <$fd>;
443 print "<div class=\"head2\"> view\n";
444 print $cgi->a({-href
=> "$my_uri/$project/commit/$hash"}, "commit") . " | ";
445 print $cgi->a({-href
=> "$my_uri/$project/commitdiff/$hash"}, "diff");
446 print "</div><br/><br/>\n";
447 print "<div class=\"title\">" . $cgi->a({-href
=> "$my_uri/$project/commitdiff/$hash"}, $co{'title'}) . "<br/></div>\n";
448 print "<table cellspacing=\"0\" class=\"log\">\n";
450 print "<td class=\"head2\">";
451 print "author " . escapeHTML
($co{'author'}) . " [" . gmtime($co{'author_time'}) . " " . $co{'author_timezone'} . "]<br/>\n";
452 print "committer " . escapeHTML
($co{'committer'}) . " [" . gmtime($co{'committer_time'}) . " " . $co{'committer_timezone'} . "]<br/>\n";
453 print "commit $hash<br/>\n";
454 print "tree " . $cgi->a({-href
=> "$my_uri/$project/tree/$co{'tree'}"}, $co{'tree'}) . "<br/>\n";
455 my $parents = $co{'parents'};
456 foreach my $par (@
$parents) {
457 print "parent " . $cgi->a({-href
=> "$my_uri/$project/tree/$par"}, $par) . "<br/>\n";
463 my $comment = $co{'comment'};
464 foreach my $line (@
$comment) {
465 if ($line =~ m/signed-off-by:/i) {
466 print '<div class="signed_off">' . escapeHTML
($line) . "<br/></div>\n";
468 print escapeHTML
($line) . "<br/>\n";
471 print "<br/><br/>\n";
477 foreach my $line (@difftree) {
478 # '*100644->100644 blob 9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596 net/ipv4/route.c'
479 # '+100644 blob 4a83ab6cd565d21ab0385bac6643826b83c2fcd4 arch/arm/lib/bitops.h'
480 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
486 if ($type eq "blob") {
488 print "added\t" . $cgi->a({-href
=> "$my_uri/$project/blob/$id"}, $file) . "\n";
489 } elsif ($op eq "-") {
490 print "removed\t" . $cgi->a({-href
=> "$my_uri/$project/blob/$id"}, $file) . "\n";
491 } elsif ($op eq "*") {
492 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
495 print "changed\t" . $cgi->a({-href
=> "$my_uri/$project/blobdiff/$old/$new"}, $file) . "\n";
502 } elsif ($action eq "blobdiff") {
504 print "<br/><br/>\n";
506 git_diff
($hash, $hash_parent, $hash, $hash_parent);
510 } elsif ($action eq "commitdiff") {
511 my %co = git_commit
($hash);
512 open my $fd, "-|", "$gitbin/diff-tree", "-r", $co{'parent'}, $hash;
513 my (@difftree) = map { chomp; $_ } <$fd>;
517 print "<div class=\"head2\"> view\n";
518 print $cgi->a({-href
=> "$my_uri/$project/commit/$hash"}, "commit") . " | ";
519 print $cgi->a({-href
=> "$my_uri/$project/commitdiff/$hash"}, "diff");
520 print "</div><br/><br/>\n";
521 print "<div class=\"title\">" . $cgi->a({-href
=> "$my_uri/$project/commit/$hash"}, $co{'title'}) . "<br/></div>\n";
523 foreach my $line (@difftree) {
524 # '*100644->100644 blob 8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154 Makefile'
525 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
531 if ($type eq "blob") {
533 git_diff
("", $file, "", $id);
534 } elsif ($op eq "-") {
535 git_diff
($file, "", $id, "");
536 } elsif ($op eq "*") {
537 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
538 git_diff
($file, $file, $1, $2);