v041
[git.git] / gitweb.pl
blob86c3b79813b5b6a7b11c7d6aa037b85560c43293
1 #!/usr/bin/perl
3 # gitweb.pl - simple web interface to track changes in git repositories
5 # Version 041
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
12 use strict;
13 use warnings;
14 use CGI qw(:standard :escapeHTML);
15 use CGI::Carp qw(fatalsToBrowser);
17 my $cgi = new CGI;
19 my $projectroot = "/";
20 my $defaultprojects = "home/kay/public_html";
21 my $gitbin = "/home/kay/bin/git";
22 my $gittmp = "/tmp";
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";
33 # sanitize input
34 $action =~ s/[^0-9a-zA-Z\.\-]//g;
35 $project =~ s/\/\.//g;
36 $project =~ s/^\/+//g;
37 $project =~ s/\/+$//g;
38 $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;
43 sub git_header_html {
44 print $cgi->header(-type => 'text/html', -charset => 'utf-8');
45 print <<EOF;
46 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
47 <html>
48 <head>
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; }
76 a { color:#0000cc; }
77 a:hover { color:#880000; }
78 a:visited { color:#880000; }
79 a:active { color:#880000; }
80 pre { padding:8px; }
81 </style>
82 </head>
83 <body>
84 EOF
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") . " / ";
92 if ($project ne "") {
93 print $cgi->a({-href => "$my_uri?p=$project;a=log"}, $project);
95 if ($action ne "") {
96 print " / $action";
98 print "</div>\n";
101 sub git_footer_html {
102 print "</div>\n";
103 print "</body>\n</html>";
106 sub git_head {
107 my $path = shift;
108 open my $fd, "$projectroot/$path/.git/HEAD";
109 my $head = <$fd>;
110 close $fd;
111 chomp $head;
112 return $head;
115 sub git_commit {
116 my $commit = shift;
117 my %co;
118 my @parents;
120 open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
121 while (my $line = <$fd>) {
122 chomp($line);
123 last if $line eq "";
124 if ($line =~ m/^tree (.*)$/) {
125 $co{'tree'} = $1;
126 } elsif ($line =~ m/^parent (.*)$/) {
127 push @parents, $1;
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]+) (.*)$/) {
133 $co{'author'} = $1;
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];
143 close $fd;
144 return %co;
147 sub git_diff_html {
148 my $from_name = shift || "/dev/null";
149 my $to_name = shift || "/dev/null";
150 my $from = shift;
151 my $to = shift;
153 my $from_tmp = "/dev/null";
154 my $to_tmp = "/dev/null";
155 my $from_label = "/dev/null";
156 my $to_label = "/dev/null";
157 my $pid = $$;
159 # create temp from-file
160 if ($from ne "") {
161 $from_tmp = "$gittmp/gitweb_" . $$ . "_from";
162 open my $fd2, "> $from_tmp";
163 open my $fd, "-|", "$gitbin/cat-file", "blob", $from;
164 my @file = <$fd>;
165 print $fd2 @file;
166 close $fd2;
167 close $fd;
168 $from_label = "a/$from_name";
171 # create tmp to-file
172 if ($to ne "") {
173 $to_tmp = "$gittmp/gitweb_" . $$ . "_to";
174 open my $fd2, "> $to_tmp";
175 open my $fd, "-|", "$gitbin/cat-file", "blob", $to;
176 my @file = <$fd>;
177 print $fd2 @file;
178 close $fd2;
179 close $fd;
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;\">===== ";
185 if ($from ne "") {
186 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from"}, $from);
187 } else {
188 print $from_name;
190 print " vs ";
191 if ($to ne "") {
192 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to"}, $to);
193 } else {
194 print $to_name;
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 '@';
205 close $fd;
207 if ($from ne "") {
208 unlink("$from_tmp");
210 if ($to ne "") {
211 unlink("$to_tmp");
215 # git cares only about the executable bit
216 sub mode_str {
217 my $perms = oct shift;
218 my $modestr;
219 if ($perms & 040000) {
220 $modestr .= 'drwxrwxr-x';
221 } else {
222 if ($perms & 0100) {
223 $modestr .= '-rwxrwxr-x';
224 } else {
225 $modestr .= '-rw-rw-r--';
228 return $modestr;
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";
246 exit;
249 # show list of default projects
250 if ($project eq "") {
251 opendir(my $fd, "$projectroot/$defaultprojects");
252 my (@path) = sort grep(!/^\./, readdir($fd));
253 closedir($fd);
254 git_header_html();
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";
262 print "</div><br/>";
263 git_footer_html();
264 exit;
267 if ($action eq "blob") {
268 git_header_html();
269 print "<br/><br/>\n";
270 print "<pre>\n";
271 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
272 my $nr;
273 while (my $line = <$fd>) {
274 $nr++;
275 printf "<span style =\"color: #999999;\">%3i\t</span>%s", $nr, escapeHTML($line);;
277 close $fd;
278 print "</pre>\n";
279 print "<br/>";
280 git_footer_html();
281 } elsif ($action eq "tree") {
282 if ($hash eq "") {
283 $hash = git_head($project);
285 open my $fd, "-|", "$gitbin/ls-tree", $hash;
286 my (@entries) = map { chomp; $_ } <$fd>;
287 close $fd;
288 git_header_html();
289 print "<br/><br/>\n";
290 print "<pre>\n";
291 foreach my $line (@entries) {
292 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
293 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
294 my $t_mode = $1;
295 my $t_type = $2;
296 my $t_hash = $3;
297 my $t_name = $4;
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";
304 print "</pre>\n";
305 print "<br/>";
306 git_footer_html();
307 } elsif ($action eq "log" || $action eq "rss") {
308 open my $fd, "-|", "$gitbin/rev-list", git_head($project);
309 my (@revtree) = map { chomp; $_ } <$fd>;
310 close $fd;
312 if ($action eq "log") {
313 git_header_html();
314 print "<div class=\"head2\">\n";
315 print "view ";
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";
322 print "</div>\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";
328 print "<channel>\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'};
339 my $age_string;
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) {
361 if ($i == 0) {
362 print "<tr>\n";
363 print "<td class=\"head1\"> Last change $age_string. </td>\n";
364 print "</tr>\n";
366 last;
368 print "<tr>\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>";
371 print "</tr>\n";
372 print "<tr>\n";
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";
376 print "</td>\n";
377 print "<td class=\"head2\">\n";
378 print "author &nbsp; &nbsp;" . 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";
380 print "</td>";
381 print "</tr>\n";
382 print "<tr>\n";
383 print "<td></td>\n";
384 print "<td>\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";
389 } else {
390 print escapeHTML($line) . "<br/>\n";
393 print "<br/><br/>\n";
394 print "</td>";
395 print "</tr>\n";
396 } elsif ($action eq "rss") {
397 last if ($i >= 20);
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";
410 print "</item>\n";
413 if ($action eq "log") {
414 print "</table>\n";
415 git_footer_html();
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>;
423 close $fd;
425 git_header_html();
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";
432 print "<tr>\n";
433 print "<td class=\"head2\">";
434 print "author &nbsp; &nbsp;" . 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 &nbsp; &nbsp;$hash<br/>\n";
437 print "tree &nbsp; &nbsp; &nbsp;" . $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 &nbsp; &nbsp;" . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$par"}, $par) . "<br/>\n";
442 print "</td>";
443 print "</tr>\n";
444 print "<tr>\n";
445 print "<td>\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";
450 } else {
451 print escapeHTML($line) . "<br/>\n";
454 print "<br/><br/>\n";
455 print "</td>";
456 print "</tr>\n";
457 print "</table>";
459 print "<pre>\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(.*)$/;
464 my $op = $1;
465 my $mode = $2;
466 my $type = $3;
467 my $id = $4;
468 my $file = $5;
469 $mode =~ m/^([0-7]{6})/;
470 my $modestr = mode_str($1);
471 if ($type eq "blob") {
472 if ($op eq "+") {
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]+)/;
478 my $from = $1;
479 my $to = $2;
480 print "$modestr " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$to;hp=$from"}, $file) . "\n";
484 print "</pre>\n";
485 print "<br/>";
486 git_footer_html();
487 } elsif ($action eq "blobdiff") {
488 git_header_html();
489 print "<br/><br/>\n";
490 print "<pre>\n";
491 git_diff_html($hash_parent, $hash, $hash_parent, $hash);
492 print "</pre>\n";
493 print "<br/>";
494 git_footer_html();
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>;
499 close $fd;
501 git_header_html();
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";
507 print "<pre>\n";
508 foreach my $line (@difftree) {
509 # '*100644->100644 blob 8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154 Makefile'
510 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
511 my $op = $1;
512 my $mode = $2;
513 my $type = $3;
514 my $id = $4;
515 my $file = $5;
516 if ($type eq "blob") {
517 if ($op eq "+") {
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);
527 print "</pre>\n";
528 print "<br/>";
529 git_footer_html();
530 } else {
531 git_header_html();
532 print "<div class=\"head2\">\n";
533 print "unknown action";
534 print "</div><br/><br/>\n";
535 git_footer_html();