v025
[git.git] / gitweb.pl
bloba2ec30db108f65c187f2d749a52743ef743f8d00
1 #!/usr/bin/perl
3 # gitweb.pl - simple web interface to track changes in git repositories
5 # Version 025
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 = "/home/kay/public_html";
20 my $defaultprojects = ".";
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);
25 my $my_url_parm = $cgi->url(-path => 1);
26 $my_url_parm =~ s/.*$my_uri//;
28 my $project = "";
29 my $action = "";
30 my $hash = "";
31 my $hash_parent = "";
32 my $view_back = 1;
34 # get values from url
35 if ($my_url_parm =~ m#/(.+)/commit/([0-9a-fA-F]+)$#) {
36 $project = $1;
37 $action = "commit";
38 $hash = $2;
39 } elsif ($my_url_parm =~ m#/(.+)/commitdiff/([0-9a-fA-F]+)$#) {
40 $project = $1;
41 $action = "commitdiff";
42 $hash = $2;
43 } elsif ($my_url_parm =~ m#/(.+)/blobdiff/([0-9a-fA-F]+)/([0-9a-fA-F]+)$#) {
44 $project = $1;
45 $action = "blobdiff";
46 $hash = $2;
47 $hash_parent = $3;
48 } elsif ($my_url_parm =~ m#/(.+)/blob/([0-9a-fA-F]+)$#) {
49 $project = $1;
50 $action = "blob";
51 $hash = $2;
52 } elsif ($my_url_parm =~ m#/(.+)/tree/([0-9a-fA-F]+)$#) {
53 $project = $1;
54 $action = "tree";
55 $hash = $2;
56 } elsif ($my_url_parm =~ m#/(.+)/log/([0-9]+)$#) {
57 $project = $1;
58 $action = "log";
59 $view_back = $2;
60 } elsif ($my_url_parm =~ m#/(.+)/log$#) {
61 $project = $1;
62 $action = "log";
63 $view_back = 1;
64 } elsif ($my_url_parm =~ m#/(.+)/rss$#) {
65 $project = $1;
66 $action = "rss";
67 $view_back = 1;
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";
83 exit;
84 } elsif ($my_url_parm =~ m#/(.+)$#) {
85 $project = $1;
86 $action = "log";
87 $view_back = 1;
90 # sanitize input
91 $project =~ s#\/\.+##g;
93 $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/.git/objects";
95 sub git_header_html {
96 print $cgi->header(-type => 'text/html', -charset => 'utf-8');
97 print <<EOF;
98 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
99 <html>
100 <head>
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; }
128 a { color:#0000cc; }
129 a:hover { color:#880000; }
130 a:visited { color:#880000; }
131 a:active { color:#880000; }
132 pre { padding:8px; }
133 </style>
134 </head>
135 <body>
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);
147 if ($action ne "") {
148 print " / $action";
150 print "</div>\n";
153 sub git_footer_html {
154 print "</div>";
155 print $cgi->end_html();
158 sub git_head {
159 open my $fd, "$projectroot/$project/.git/HEAD";
160 my $head = <$fd>;
161 close $fd;
162 chomp $head;
163 return $head;
166 sub git_commit {
167 my $commit = shift;
168 my %co;
169 my @parents;
171 open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
172 while (my $line = <$fd>) {
173 chomp($line);
174 last if $line eq "";
175 if ($line =~ m/^tree (.*)$/) {
176 $co{'tree'} = $1;
177 } elsif ($line =~ m/^parent (.*)$/) {
178 push @parents, $1;
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]+) (.*)$/) {
184 $co{'author'} = $1;
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];
194 close $fd;
195 return %co;
198 sub git_diff {
199 my $old_name = shift || "/dev/null";
200 my $new_name = shift || "/dev/null";
201 my $old = shift;
202 my $new = shift;
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
210 if ($old ne "") {
211 open my $fd2, "> $gittmp/$old";
212 open my $fd, "-|", "$gitbin/cat-file", "blob", $old;
213 while (my $line = <$fd>) {
214 print $fd2 $line;
216 close $fd2;
217 close $fd;
218 $tmp_old = "$gittmp/$old";
219 $old_label = "a/$old_name";
222 # create tmp to-file
223 if ($new ne "") {
224 open my $fd2, "> $gittmp/$new";
225 open my $fd, "-|", "$gitbin/cat-file", "blob", $new;
226 while (my $line = <$fd>) {
227 print $fd2 $line;
229 close $fd2;
230 close $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;\">===== ";
237 if ($old ne "") {
238 print $cgi->a({-href => "$my_uri/$project/blob/$old"}, $old);
239 } else {
240 print $old_name;
242 print " vs ";
243 if ($new ne "") {
244 print $cgi->a({-href => "$my_uri/$project/blob/$new"}, $new);
245 } else {
246 print $new_name;
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 '@';
257 close $fd;
258 unlink("$gittmp/$new");
259 unlink("$gittmp/$old");
262 if ($project eq "") {
263 opendir(my $fd, "$projectroot/$defaultprojects");
264 my (@path) = grep(!/^\./, readdir($fd));
265 closedir($fd);
266 git_header_html();
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";
274 print "</div><br/>";
275 git_footer_html();
276 exit;
279 if ($action eq "blob") {
280 git_header_html();
281 print "<br/><br/>\n";
282 print "<pre>\n";
283 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
284 my $nr;
285 while (my $line = <$fd>) {
286 $nr++;
287 print "$nr\t" . escapeHTML($line);;
289 close $fd;
290 print "</pre>\n";
291 print "<br/>";
292 git_footer_html();
293 } elsif ($action eq "tree") {
294 if ($hash eq "") {
295 $hash = git_head();
297 open my $fd, "-|", "$gitbin/ls-tree", $hash;
298 my (@entries) = map { chomp; $_ } <$fd>;
299 close $fd;
300 git_header_html();
301 print "<br/><br/>\n";
302 print "<pre>\n";
303 foreach my $line (@entries) {
304 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
305 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
306 my $t_type = $2;
307 my $t_hash = $3;
308 my $t_name = $4;
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";
315 print "</pre>\n";
316 print "<br/>";
317 git_footer_html();
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>;
321 close $fd;
323 if ($action eq "log") {
324 git_header_html();
325 print "<div class=\"head2\">\n";
326 print "view ";
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";
333 print "</div>\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";
339 print "<channel>\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]+)/);
350 my $time = $1;
351 my $commit = $2;
352 my $parent = $3;
354 my %co = git_commit($commit);
355 my $age = time - $co{'committer_time'};
356 my $age_string;
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) {
378 if ($i == 0) {
379 print "<tr>\n";
380 print "<td class=\"head1\"> Last change $age_string. </td>\n";
381 print "</tr>\n";
383 last;
385 print "<tr>\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>";
388 print "</tr>\n";
389 print "<tr>\n";
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";
393 print "</td>\n";
394 print "<td class=\"head2\">\n";
395 print "author &nbsp; &nbsp;" . 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";
397 print "</td>";
398 print "</tr>\n";
399 print "<tr>\n";
400 print "<td></td>\n";
401 print "<td>\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";
406 } else {
407 print escapeHTML($line) . "<br/>\n";
410 print "<br/><br/>\n";
411 print "</td>";
412 print "</tr>\n";
413 } elsif ($action eq "rss") {
414 last if ($i >= 20);
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";
427 print "</item>\n";
430 if ($action eq "log") {
431 print "</table>\n";
432 git_footer_html();
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>;
440 close $fd;
442 git_header_html();
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";
449 print "<tr>\n";
450 print "<td class=\"head2\">";
451 print "author &nbsp; &nbsp;" . 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 &nbsp; &nbsp;$hash<br/>\n";
454 print "tree &nbsp; &nbsp; &nbsp;" . $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 &nbsp; &nbsp;" . $cgi->a({-href => "$my_uri/$project/tree/$par"}, $par) . "<br/>\n";
459 print "</td>";
460 print "</tr>\n";
461 print "<tr>\n";
462 print "<td>\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";
467 } else {
468 print escapeHTML($line) . "<br/>\n";
471 print "<br/><br/>\n";
472 print "</td>";
473 print "</tr>\n";
474 print "</table>";
476 print "<pre>\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(.*)$/;
481 my $op = $1;
482 my $mode = $2;
483 my $type = $3;
484 my $id = $4;
485 my $file = $5;
486 if ($type eq "blob") {
487 if ($op eq "+") {
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]+)/;
493 my $old = $1;
494 my $new = $2;
495 print "changed\t" . $cgi->a({-href => "$my_uri/$project/blobdiff/$old/$new"}, $file) . "\n";
499 print "</pre>\n";
500 print "<br/>";
501 git_footer_html();
502 } elsif ($action eq "blobdiff") {
503 git_header_html();
504 print "<br/><br/>\n";
505 print "<pre>\n";
506 git_diff($hash, $hash_parent, $hash, $hash_parent);
507 print "</pre>\n";
508 print "<br/>";
509 git_footer_html();
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>;
514 close $fd;
516 git_header_html();
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";
522 print "<pre>\n";
523 foreach my $line (@difftree) {
524 # '*100644->100644 blob 8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154 Makefile'
525 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
526 my $op = $1;
527 my $mode = $2;
528 my $type = $3;
529 my $id = $4;
530 my $file = $5;
531 if ($type eq "blob") {
532 if ($op eq "+") {
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);
542 print "</pre>\n";
543 print "<br/>";
544 git_footer_html();