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 = "/";
20 my $defaultprojects = "home/kay/public_html";
21 my $gitbin = "/home/kay/bin/git";
23 my $my_url = $cgi->url();
24 my $my_uri = $cgi->url(-absolute
=> 1);
26 my $project = $cgi->param('p');
27 my $action = $cgi->param('a');
28 my $hash = $cgi->param('h');
29 my $hash_parent = $cgi->param('hp');
30 my $time_back = $cgi->param('t') || 1;
31 $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/.git/objects";
34 $action =~ s/[^0-9a-zA-Z\.\-]//g;
35 $project =~ s/\/\.//g
;
36 $project =~ s/^\/+//g
;
37 $project =~ s/\/+$//g
;
39 $hash =~ s/[^0-9a-fA-F]//g;
40 $hash_parent =~ s/[^0-9a-fA-F]//g;
41 $time_back =~ s/[^0-9]+//g;
44 print $cgi->header(-type
=> 'text/html', -charset
=> 'utf-8');
46 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
49 <title>git - $project $action</title>
50 <link rel="alternate" title="$project log" href="$my_uri?p=$project;a=rss" type="application/rss+xml"/>
51 <style type="text/css">
52 body { font-family: sans-serif; font-size: 12px; margin:25px; }
53 div.body { border-width:1px; border-style:solid; border-color:#D9D8D1; }
54 div.head1 { font-size:18px; padding:8px; background-color: #D9D8D1; font-weight:bold; }
55 div.head1 a:visited { color:#0000cc; }
56 div.head1 a:hover { color:#880000; }
57 div.head1 a:active { color:#880000; }
58 div.head2 { padding:8px; }
59 div.head2 a:visited { color:#0000cc; }
60 div.head2 a:hover { color:#880000; }
61 div.head2 a:active { color:#880000; }
62 div.title { padding:8px; background-color: #D9D8D1; font-weight:bold; }
63 div.title a { color:#000000; text-decoration:none; }
64 div.title a:hover { color:#880000; text-decoration:underline; }
65 div.title a:visited { color:#000000; }
66 table { padding:0px; margin:0px; width:100%; }
67 tr { vertical-align:top; }
68 td { padding:8px; margin:0px; font-family: sans-serif; font-size: 12px; }
69 td.head1 { background-color: #D9D8D1; font-weight:bold; }
70 td.head1 a { color:#000000; text-decoration:none; }
71 td.head1 a:hover { color:#880000; text-decoration:underline; }
72 td.head1 a:visited { color:#000000; }
73 td.head2 { background-color: #EDECE6; font-family: monospace; font-size:12px; }
74 td.head3 { background-color: #EDECE6; font-size:10px; }
75 div.signed_off { color: #a9a8a1; }
77 a:hover { color:#880000; }
78 a:visited { color:#880000; }
79 a:active { color:#880000; }
85 print "<div class=\"body\">\n";
86 print "<div class=\"head1\">";
87 print "<a href=\"http://kernel.org/pub/software/scm/git/\">" .
88 "<img src=\"$my_uri?a=git-logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/></a>";
89 if ($defaultprojects ne "") {
90 print $cgi->a({-href
=> "$my_uri"}, "projects") . " / ";
93 print $cgi->a({-href
=> "$my_uri?p=$project;a=log"}, $project);
101 sub git_footer_html
{
103 print "</body>\n</html>";
108 open my $fd, "$projectroot/$path/.git/HEAD";
120 open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
121 while (my $line = <$fd>) {
124 if ($line =~ m/^tree (.*)$/) {
126 } elsif ($line =~ m/^parent (.*)$/) {
128 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
129 $co{'committer'} = $1;
130 $co{'committer_time'} = $2;
131 $co{'committer_timezone'} = $3;
132 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
134 $co{'author_time'} = $2;
135 $co{'author_timezone'} = $3;
138 $co{'parents'} = \
@parents;
139 $co{'parent'} = $parents[0];
140 my (@comment) = map { chomp; $_ } <$fd>;
141 $co{'comment'} = \
@comment;
142 $co{'title'} = $comment[0];
148 my $from_name = shift || "/dev/null";
149 my $to_name = shift || "/dev/null";
153 my $from_tmp = "/dev/null";
154 my $to_tmp = "/dev/null";
155 my $from_label = "/dev/null";
156 my $to_label = "/dev/null";
159 # create temp from-file
161 $from_tmp = "$gittmp/gitweb_" . $$ . "_from";
162 open my $fd2, "> $from_tmp";
163 open my $fd, "-|", "$gitbin/cat-file", "blob", $from;
168 $from_label = "a/$from_name";
173 $to_tmp = "$gittmp/gitweb_" . $$ . "_to";
174 open my $fd2, "> $to_tmp";
175 open my $fd, "-|", "$gitbin/cat-file", "blob", $to;
180 $to_label = "b/$to_name";
183 open my $fd, "-|", "/usr/bin/diff", "-L", $from_label, "-L", $to_label, "-u", "-p", $from_tmp, $to_tmp;
184 print "<span style=\"color: #000099;\">===== ";
186 print $cgi->a({-href
=> "$my_uri?p=$project;a=blob;h=$from"}, $from);
192 print $cgi->a({-href
=> "$my_uri?p=$project;a=blob;h=$to"}, $to);
196 print " =====</span>\n";
197 while (my $line = <$fd>) {
198 my $char = substr($line,0,1);
199 print '<span style="color: #008800;">' if $char eq '+';
200 print '<span style="color: #CC0000;">' if $char eq '-';
201 print '<span style="color: #990099;">' if $char eq '@';
202 print escapeHTML
($line);
203 print '</span>' if $char eq '+' or $char eq '-' or $char eq '@';
215 # git cares only about the executable bit
217 my $perms = oct shift;
219 if ($perms & 040000) {
220 $modestr .= 'drwxrwxr-x';
223 $modestr .= '-rwxrwxr-x';
225 $modestr .= '-rw-rw-r--';
231 if ($action eq "git-logo.png") {
232 print $cgi->header(-type
=> 'image/png', -expires
=> '+1d');
233 print "\211\120\116\107\015\012\032\012\000\000\000\015\111\110\104\122".
234 "\000\000\000\110\000\000\000\033\004\003\000\000\000\055\331\324".
235 "\055\000\000\000\030\120\114\124\105\377\377\377\140\140\135\260".
236 "\257\252\000\200\000\316\315\307\300\000\000\350\350\346\367\367".
237 "\366\225\014\247\107\000\000\000\163\111\104\101\124\050\317\143".
238 "\110\147\040\004\112\134\030\012\010\052\142\123\141\040\002\010".
239 "\015\151\105\254\241\241\001\060\014\223\140\066\046\122\221\261".
240 "\001\021\326\341\125\144\154\154\314\154\154\014\242\014\160\052".
241 "\142\006\052\301\142\035\263\001\002\123\244\010\350\000\003\030".
242 "\046\126\021\324\341\040\227\033\340\264\016\065\044\161\051\202".
243 "\231\060\270\223\012\021\271\105\210\301\215\240\242\104\041\006".
244 "\047\101\202\100\205\301\105\211\040\160\001\000\244\075\041\305".
245 "\022\034\232\376\000\000\000\000\111\105\116\104\256\102\140\202";
249 # show list of default projects
250 if ($project eq "") {
251 opendir(my $fd, "$projectroot/$defaultprojects");
252 my (@path) = sort grep(!/^\./, readdir($fd));
255 print "<div class=\"head2\">\n";
256 print "<br/><br/>\n";
257 foreach my $line (@path) {
258 if (-e
"$projectroot/$defaultprojects/$line/.git/HEAD") {
259 print $cgi->a({-href
=> "$my_uri?p=$defaultprojects/$line;a=log"}, "$defaultprojects/$line") . "<br/>\n";
267 if ($action eq "blob") {
269 print "<br/><br/>\n";
271 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
273 while (my $line = <$fd>) {
275 printf "<span style =\"color: #999999;\">%3i\t</span>%s", $nr, escapeHTML
($line);;
281 } elsif ($action eq "tree") {
283 $hash = git_head
($project);
285 open my $fd, "-|", "$gitbin/ls-tree", $hash;
286 my (@entries) = map { chomp; $_ } <$fd>;
289 print "<br/><br/>\n";
291 foreach my $line (@entries) {
292 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
293 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
298 if ($t_type eq "blob") {
299 print mode_str
($t_mode). " " . $cgi->a({-href
=> "$my_uri?p=$project;a=blob;h=$t_hash"}, $t_name) . "\n";
300 } elsif ($t_type eq "tree") {
301 print mode_str
($t_mode). " " . $cgi->a({-href
=> "$my_uri?p=$project;a=tree;h=$t_hash"}, $t_name) . "\n";
307 } elsif ($action eq "log" || $action eq "rss") {
308 open my $fd, "-|", "$gitbin/rev-list", git_head
($project);
309 my (@revtree) = map { chomp; $_ } <$fd>;
312 if ($action eq "log") {
314 print "<div class=\"head2\">\n";
316 print $cgi->a({-href
=> "$my_uri?p=$project;a=log"}, "last day") . " | ";
317 print $cgi->a({-href
=> "$my_uri?p=$project;a=log;t=7"}, "week") . " | ";
318 print $cgi->a({-href
=> "$my_uri?p=$project;a=log;t=31"}, "month") . " | ";
319 print $cgi->a({-href
=> "$my_uri?p=$project;a=log;t=365"}, "year") . " | ";
320 print $cgi->a({-href
=> "$my_uri?p=$project;a=log;t=0"}, "all") . "<br/>\n";
321 print "<br/><br/>\n";
323 print "<table cellspacing=\"0\" class=\"log\">\n";
324 } elsif ($action eq "rss") {
325 print $cgi->header(-type
=> 'text/xml', -charset
=> 'utf-8');
326 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
327 "<rss version=\"0.91\">\n";
329 print "<title>$project</title>\n".
330 "<link> " . $my_url . "/$project/log</link>\n".
331 "<description>$project log</description>\n".
332 "<language>en</language>\n";
335 for (my $i = 0; $i <= $#revtree; $i++) {
336 my $commit = $revtree[$i];
337 my %co = git_commit
($commit);
338 my $age = time - $co{'committer_time'};
340 if ($age > 60*60*24*365*2) {
341 $age_string = int $age/60/60/24/365;
342 $age_string .= " years ago";
343 } elsif ($age > 60*60*24*365/12*2) {
344 $age_string = int $age/60/60/24/365/12;
345 $age_string .= " months ago";
346 } elsif ($age > 60*60*24*7*2) {
347 $age_string = int $age/60/60/24/7;
348 $age_string .= " weeks ago";
349 } elsif ($age > 60*60*24*2) {
350 $age_string = int $age/60/60/24;
351 $age_string .= " days ago";
352 } elsif ($age > 60*60*2) {
353 $age_string = int $age/60/60;
354 $age_string .= " hours ago";
355 } elsif ($age > 60*2) {
356 $age_string = int $age/60;
357 $age_string .= " minutes ago";
359 if ($action eq "log") {
360 if ($time_back > 0 && $age > $time_back*60*60*24) {
363 print "<td class=\"head1\"> Last change $age_string. </td>\n";
369 print "<td class=\"head1\">" . $age_string . "</td>\n";
370 print "<td class=\"head1\">" . $cgi->a({-href
=> "$my_uri?p=$project;a=commit;h=$commit"}, $co{'title'}) . "</td>";
373 print "<td class=\"head3\">";
374 print $cgi->a({-href
=> "$my_uri?p=$project;a=commit;h=$commit"}, "view commit") . "<br/>\n";
375 print $cgi->a({-href
=> "$my_uri?p=$project;a=commitdiff;h=$commit"}, "view diff") . "<br/>\n";
377 print "<td class=\"head2\">\n";
378 print "author " . escapeHTML
($co{'author'}) . " [" . gmtime($co{'author_time'}) . " " . $co{'author_timezone'} . "]<br/>\n";
379 print "committer " . escapeHTML
($co{'committer'}) . " [" . gmtime($co{'committer_time'}) . " " . $co{'committer_timezone'} . "]<br/>\n";
385 my $comment = $co{'comment'};
386 foreach my $line (@
$comment) {
387 if ($line =~ m/^(signed-off|acked)-by:/i) {
388 print '<div class="signed_off">' . escapeHTML
($line) . "<br/></div>\n";
390 print escapeHTML
($line) . "<br/>\n";
393 print "<br/><br/>\n";
396 } elsif ($action eq "rss") {
398 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($co{'author_time'});
399 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
400 print "<item>\n\t<title>";
401 printf("%s %02d, %02d:%02d - ", $months[$mon], $mday, $hour, $min);
402 print escapeHTML
($co{'title'}) . "</title>\n";
403 print "\t<link> " . $my_url . "/$project/commit/$commit</link>\n";
404 print "\t<description>";
405 my $comment = $co{'comment'};
406 foreach my $line (@
$comment) {
407 print escapeHTML
($line) . "\n";
409 print "\t</description>\n";
413 if ($action eq "log") {
416 } elsif ($action eq "rss") {
417 print "</channel></rss>";
419 } elsif ($action eq "commit") {
420 my %co = git_commit
($hash);
421 open my $fd, "-|", "$gitbin/diff-tree", "-r", $co{'parent'}, $hash;
422 my (@difftree) = map { chomp; $_ } <$fd>;
426 print "<div class=\"head2\"> view\n";
427 print $cgi->a({-href
=> "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | ";
428 print $cgi->a({-href
=> "$my_uri?p=$project;a=commitdiff;h=$hash"}, "diff");
429 print "</div><br/><br/>\n";
430 print "<div class=\"title\">" . $cgi->a({-href
=> "$my_uri?p=$project;a=commitdiff;h=$hash"}, $co{'title'}) . "<br/></div>\n";
431 print "<table cellspacing=\"0\" class=\"log\">\n";
433 print "<td class=\"head2\">";
434 print "author " . escapeHTML
($co{'author'}) . " [" . gmtime($co{'author_time'}) . " " . $co{'author_timezone'} . "]<br/>\n";
435 print "committer " . escapeHTML
($co{'committer'}) . " [" . gmtime($co{'committer_time'}) . " " . $co{'committer_timezone'} . "]<br/>\n";
436 print "commit $hash<br/>\n";
437 print "tree " . $cgi->a({-href
=> "$my_uri?p=$project;a=tree;h=$co{'tree'}"}, $co{'tree'}) . "<br/>\n";
438 my $parents = $co{'parents'};
439 foreach my $par (@
$parents) {
440 print "parent " . $cgi->a({-href
=> "$my_uri?p=$project;a=tree;h=$par"}, $par) . "<br/>\n";
446 my $comment = $co{'comment'};
447 foreach my $line (@
$comment) {
448 if ($line =~ m/(signed-off|acked)-by:/i) {
449 print '<div class="signed_off">' . escapeHTML
($line) . "<br/></div>\n";
451 print escapeHTML
($line) . "<br/>\n";
454 print "<br/><br/>\n";
460 foreach my $line (@difftree) {
461 # '*100644->100644 blob 9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596 net/ipv4/route.c'
462 # '+100644 blob 4a83ab6cd565d21ab0385bac6643826b83c2fcd4 arch/arm/lib/bitops.h'
463 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
469 $mode =~ m/^([0-7]{6})/;
470 my $modestr = mode_str
($1);
471 if ($type eq "blob") {
473 print "$modestr " . $cgi->a({-href
=> "$my_uri?p=$project;a=blobdiff;h=$id"}, $file) . " (new)\n";
474 } elsif ($op eq "-") {
475 print "$modestr " . $cgi->a({-href
=> "$my_uri?p=$project;a=blobdiff;hp=$id"}, $file) . " (removed)\n";
476 } elsif ($op eq "*") {
477 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
480 print "$modestr " . $cgi->a({-href
=> "$my_uri?p=$project;a=blobdiff;h=$to;hp=$from"}, $file) . "\n";
487 } elsif ($action eq "blobdiff") {
489 print "<br/><br/>\n";
491 git_diff_html
($hash_parent, $hash, $hash_parent, $hash);
495 } elsif ($action eq "commitdiff") {
496 my %co = git_commit
($hash);
497 open my $fd, "-|", "$gitbin/diff-tree", "-r", $co{'parent'}, $hash;
498 my (@difftree) = map { chomp; $_ } <$fd>;
502 print "<div class=\"head2\"> view\n";
503 print $cgi->a({-href
=> "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | ";
504 print $cgi->a({-href
=> "$my_uri?p=$project;a=commitdiff;h=$hash"}, "diff");
505 print "</div><br/><br/>\n";
506 print "<div class=\"title\">" . $cgi->a({-href
=> "$my_uri?p=$project;a=commit;h=$hash"}, $co{'title'}) . "<br/></div>\n";
508 foreach my $line (@difftree) {
509 # '*100644->100644 blob 8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154 Makefile'
510 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
516 if ($type eq "blob") {
518 git_diff_html
("", $file, "", $id);
519 } elsif ($op eq "-") {
520 git_diff_html
($file, "", $id, "");
521 } elsif ($op eq "*") {
522 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
523 git_diff_html
($file, $file, $1, $2);
532 print "<div class=\"head2\">\n";
533 print "unknown action";
534 print "</div><br/><br/>\n";