1#!/usr/bin/perl
2
3# gitweb.pl - simple web interface to track changes in git repositories
4#
5# Version 035
6#
7# (C) 2005, Kay Sievers <kay.sievers@vrfy.org>
8# (C) 2005, Christian Gierke <ch@gierke.de>
9#
10# This file is licensed under the GPL v2, or a later version
11
12use strict;
13use warnings;
14use CGI qw(:standard :escapeHTML);
15use CGI::Carp qw(fatalsToBrowser);
16
17my $cgi = new CGI;
18
19my $projectroot = "/";
20my $defaultprojects = "home/kay/public_html";
21my $gitbin = "/home/kay/bin/git";
22my $gittmp = "/tmp";
23my $my_url = $cgi->url();
24my $my_uri = $cgi->url(-absolute => 1);
25
26my $project = $cgi->param('p');
27my $action = $cgi->param('a');
28my $hash = $cgi->param('h');
29my $hash_parent = $cgi->param('hp');
30my $time_back = $cgi->param('t');
31$ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/.git/objects";
32
33# sanitize input
34$action =~ s/[^0-9a-zA-Z\.\-]//g;
35$project =~ s/\/\.//g;
36$project =~ s/|//g;
37$hash =~ s/[^0-9a-fA-F]//g;
38$hash_parent =~ s/[^0-9a-fA-F]//g;
39$time_back =~ s/[^0-9]+//g;
40
41sub git_header_html {
42 print $cgi->header(-type => 'text/html', -charset => 'utf-8');
43print <<EOF;
44<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
45<html>
46<head>
47 <title>git - $project $action</title>
48 <link rel="alternate" title="$project log" href="$my_uri/$project/rss" type="application/rss+xml"/>
49 <style type="text/css">
50 body { font-family: sans-serif; font-size: 12px; margin:25px; }
51 div.body { border-width:1px; border-style:solid; border-color:#D9D8D1; }
52 div.head1 { font-size:20px; padding:8px; background-color: #D9D8D1; font-weight:bold; }
53 div.head1 a:visited { color:#0000cc; }
54 div.head1 a:hover { color:#880000; }
55 div.head1 a:active { color:#880000; }
56 div.head2 { padding:8px; }
57 div.head2 a:visited { color:#0000cc; }
58 div.head2 a:hover { color:#880000; }
59 div.head2 a:active { color:#880000; }
60 div.title { padding:8px; background-color: #D9D8D1; font-weight:bold; }
61 div.title a { color:#000000; text-decoration:none; }
62 div.title a:hover { color:#880000; text-decoration:underline; }
63 div.title a:visited { color:#000000; }
64 table { padding:0px; margin:0px; width:100%; }
65 tr { vertical-align:top; }
66 td { padding:8px; margin:0px; font-family: sans-serif; font-size: 12px; }
67 td.head1 { background-color: #D9D8D1; font-weight:bold; }
68 td.head1 a { color:#000000; text-decoration:none; }
69 td.head1 a:hover { color:#880000; text-decoration:underline; }
70 td.head1 a:visited { color:#000000; }
71 td.head2 { background-color: #EDECE6; font-family: monospace; font-size:12px; }
72 td.head3 { background-color: #EDECE6; font-size:10px; }
73 div.signed_off { color: #a9a8a1; }
74 a { color:#0000cc; }
75 a:hover { color:#880000; }
76 a:visited { color:#880000; }
77 a:active { color:#880000; }
78 pre { padding:8px; }
79 </style>
80</head>
81<body>
82EOF
83 print "<div class=\"body\">\n";
84 print "<div class=\"head1\">";
85 print "<a href=\"http://kernel.org/pub/software/scm/git/\">" .
86 "<img src=\"$my_uri?a=git-logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/></a>";
87 if ($defaultprojects ne "") {
88 print $cgi->a({-href => "$my_uri"}, "projects") . " / ";
89 }
90 if ($project ne "") {
91 print $cgi->a({-href => "$my_uri?p=$project;a=log"}, $project);
92 }
93 if ($action ne "") {
94 print " / $action";
95 }
96 print "</div>\n";
97}
98
99sub git_footer_html {
100 print "</div>\n";
101 print "</body>\n</html>";
102}
103
104sub git_head {
105 open my $fd, "$projectroot/$project/.git/HEAD";
106 my $head = <$fd>;
107 close $fd;
108 chomp $head;
109 return $head;
110}
111
112sub git_commit {
113 my $commit = shift;
114 my %co;
115 my @parents;
116
117 open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
118 while (my $line = <$fd>) {
119 chomp($line);
120 last if $line eq "";
121 if ($line =~ m/^tree (.*)$/) {
122 $co{'tree'} = $1;
123 } elsif ($line =~ m/^parent (.*)$/) {
124 push @parents, $1;
125 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
126 $co{'committer'} = $1;
127 $co{'committer_time'} = $2;
128 $co{'committer_timezone'} = $3;
129 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
130 $co{'author'} = $1;
131 $co{'author_time'} = $2;
132 $co{'author_timezone'} = $3;
133 }
134 }
135 $co{'parents'} = \@parents;
136 $co{'parent'} = $parents[0];
137 my (@comment) = map { chomp; $_ } <$fd>;
138 $co{'comment'} = \@comment;
139 $co{'title'} = $comment[0];
140 close $fd;
141 return %co;
142}
143
144sub git_diff_html {
145 my $from_name = shift || "/dev/null";
146 my $to_name = shift || "/dev/null";
147 my $from = shift;
148 my $to = shift;
149
150 my $from_tmp = "/dev/null";
151 my $to_tmp = "/dev/null";
152 my $from_label = "/dev/null";
153 my $to_label = "/dev/null";
154 my $pid = $$;
155
156 # create temp from-file
157 if ($from ne "") {
158 $from_tmp = "$gittmp/gitweb_" . $$ . "_from";
159 open my $fd2, "> $from_tmp";
160 open my $fd, "-|", "$gitbin/cat-file", "blob", $from;
161 my @file = <$fd>;
162 print $fd2 @file;
163 close $fd2;
164 close $fd;
165 $from_label = "a/$from_name";
166 }
167
168 # create tmp to-file
169 if ($to ne "") {
170 $to_tmp = "$gittmp/gitweb_" . $$ . "_to";
171 open my $fd2, "> $to_tmp";
172 open my $fd, "-|", "$gitbin/cat-file", "blob", $to;
173 my @file = <$fd>;
174 print $fd2 @file;
175 close $fd2;
176 close $fd;
177 $to_label = "b/$to_name";
178 }
179
180 open my $fd, "-|", "/usr/bin/diff", "-L", $from_label, "-L", $to_label, "-u", "-p", $from_tmp, $to_tmp;
181 print "<span style =\"color: #000099;\">===== ";
182 if ($from ne "") {
183 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$from"}, $from);
184 } else {
185 print $from_name;
186 }
187 print " vs ";
188 if ($to ne "") {
189 print $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$to"}, $to);
190 } else {
191 print $to_name;
192 }
193 print " =====</span>\n";
194 while (my $line = <$fd>) {
195 my $char = substr($line,0,1);
196 print '<span style ="color: #008800;">' if $char eq '+';
197 print '<span style ="color: #CC0000;">' if $char eq '-';
198 print '<span style ="color: #990099;">' if $char eq '@';
199 print escapeHTML($line);
200 print '</span>' if $char eq '+' or $char eq '-' or $char eq '@';
201 }
202 close $fd;
203
204 if ($from ne "") {
205 unlink("$from_tmp");
206 }
207 if ($to ne "") {
208 unlink("$to_tmp");
209 }
210}
211
212sub mode_str {
213 my $perms = oct shift;
214 my $modestr;
215 if ($perms & 040000) { $modestr .= 'd' } else { $modestr .= '-' };
216 for (my $i = 0; $i < 3; $i++) {
217 if ($perms & 0400) { $modestr .= 'r' } else { $modestr .= '-' };
218 if ($perms & 0200) { $modestr .= 'w' } else { $modestr .= '-' };
219 if ($perms & 0100) { $modestr .= 'x' } else { $modestr .= '-' };
220 $perms <<= 3;
221 }
222 return $modestr;
223}
224
225if ($action eq "git-logo.png") {
226 print $cgi->header(-type => 'image/png', -expires => '+1d');
227 print "\211\120\116\107\015\012\032\012\000\000\000\015\111\110\104\122".
228 "\000\000\000\110\000\000\000\033\004\003\000\000\000\055\331\324".
229 "\055\000\000\000\030\120\114\124\105\377\377\377\140\140\135\260".
230 "\257\252\000\200\000\316\315\307\300\000\000\350\350\346\367\367".
231 "\366\225\014\247\107\000\000\000\163\111\104\101\124\050\317\143".
232 "\110\147\040\004\112\134\030\012\010\052\142\123\141\040\002\010".
233 "\015\151\105\254\241\241\001\060\014\223\140\066\046\122\221\261".
234 "\001\021\326\341\125\144\154\154\314\154\154\014\242\014\160\052".
235 "\142\006\052\301\142\035\263\001\002\123\244\010\350\000\003\030".
236 "\046\126\021\324\341\040\227\033\340\264\016\065\044\161\051\202".
237 "\231\060\270\223\012\021\271\105\210\301\215\240\242\104\041\006".
238 "\047\101\202\100\205\301\105\211\040\160\001\000\244\075\041\305".
239 "\022\034\232\376\000\000\000\000\111\105\116\104\256\102\140\202";
240 exit;
241}
242
243# show list of default projects
244if ($project eq "") {
245 opendir(my $fd, "$projectroot/$defaultprojects");
246 my (@path) = grep(!/^\./, readdir($fd));
247 closedir($fd);
248 git_header_html();
249 print "<div class=\"head2\">\n";
250 print "<br/><br/>\n";
251 foreach my $line (@path) {
252 if (-e "$projectroot/$defaultprojects/$line/.git/HEAD") {
253 print $cgi->a({-href => "$my_uri?p=$defaultprojects/$line;a=log"}, "$defaultprojects/$line") . "<br/>\n";
254 }
255 }
256 print "</div><br/>";
257 git_footer_html();
258 exit;
259}
260
261if ($action eq "blob") {
262 git_header_html();
263 print "<br/><br/>\n";
264 print "<pre>\n";
265 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
266 my $nr;
267 while (my $line = <$fd>) {
268 $nr++;
269 printf "<span style =\"color: #999999;\">%3i\t</span>%s", $nr, escapeHTML($line);;
270 }
271 close $fd;
272 print "</pre>\n";
273 print "<br/>";
274 git_footer_html();
275} elsif ($action eq "tree") {
276 if ($hash eq "") {
277 $hash = git_head();
278 }
279 open my $fd, "-|", "$gitbin/ls-tree", $hash;
280 my (@entries) = map { chomp; $_ } <$fd>;
281 close $fd;
282 git_header_html();
283 print "<br/><br/>\n";
284 print "<pre>\n";
285 foreach my $line (@entries) {
286 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
287 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
288 my $t_mode = $1;
289 my $t_type = $2;
290 my $t_hash = $3;
291 my $t_name = $4;
292 if ($t_type eq "blob") {
293 print mode_str($t_mode). " " . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$t_hash"}, $t_name) . "\n";
294 } elsif ($t_type eq "tree") {
295 print mode_str($t_mode). " " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$t_hash"}, $t_name) . "\n";
296 }
297 }
298 print "</pre>\n";
299 print "<br/>";
300 git_footer_html();
301} elsif ($action eq "log" || $action eq "rss") {
302 open my $fd, "-|", "$gitbin/rev-list", git_head();
303 my (@revtree) = map { chomp; $_ } <$fd>;
304 close $fd;
305
306 if ($action eq "log") {
307 git_header_html();
308 print "<div class=\"head2\">\n";
309 print "view ";
310 print $cgi->a({-href => "$my_uri?p=$project;a=log"}, "last day") . " | ";
311 print $cgi->a({-href => "$my_uri?p=$project;a=log;t=7"}, "week") . " | ";
312 print $cgi->a({-href => "$my_uri?p=$project;a=log;t=31"}, "month") . " | ";
313 print $cgi->a({-href => "$my_uri?p=$project;a=log;t=365"}, "year") . " | ";
314 print $cgi->a({-href => "$my_uri?p=$project;a=log;t=0"}, "all") . "<br/>\n";
315 print "<br/><br/>\n";
316 print "</div>\n";
317 print "<table cellspacing=\"0\" class=\"log\">\n";
318 } elsif ($action eq "rss") {
319 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
320 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
321 "<rss version=\"0.91\">\n";
322 print "<channel>\n";
323 print "<title>$project</title>\n".
324 "<link> " . $my_url . "/$project/log</link>\n".
325 "<description>$project log</description>\n".
326 "<language>en</language>\n";
327 }
328
329 for (my $i = 0; $i <= $#revtree; $i++) {
330 my $commit = $revtree[$i];
331 my %co = git_commit($commit);
332 my $age = time - $co{'committer_time'};
333 my $age_string;
334 if ($age > 60*60*24*365*2) {
335 $age_string = int $age/60/60/24/365;
336 $age_string .= " years ago";
337 } elsif ($age > 60*60*24*365/12*2) {
338 $age_string = int $age/60/60/24/365/12;
339 $age_string .= " months ago";
340 } elsif ($age > 60*60*24*7*2) {
341 $age_string = int $age/60/60/24/7;
342 $age_string .= " weeks ago";
343 } elsif ($age > 60*60*24*2) {
344 $age_string = int $age/60/60/24;
345 $age_string .= " days ago";
346 } elsif ($age > 60*60*2) {
347 $age_string = int $age/60/60;
348 $age_string .= " hours ago";
349 } elsif ($age > 60*2) {
350 $age_string = int $age/60;
351 $age_string .= " minutes ago";
352 }
353 if ($action eq "log") {
354 if ($time_back > 0 && $age > $time_back*60*60*24) {
355 if ($i == 0) {
356 print "<tr>\n";
357 print "<td class=\"head1\"> Last change $age_string. </td>\n";
358 print "</tr>\n";
359 }
360 last;
361 }
362 print "<tr>\n";
363 print "<td class=\"head1\">" . $age_string . "</td>\n";
364 print "<td class=\"head1\">" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, $co{'title'}) . "</td>";
365 print "</tr>\n";
366 print "<tr>\n";
367 print "<td class=\"head3\">";
368 print $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$commit"}, "view commit") . "<br/>\n";
369 print $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$commit"}, "view diff") . "<br/>\n";
370 print "</td>\n";
371 print "<td class=\"head2\">\n";
372 print "author " . escapeHTML($co{'author'}) . " [" . gmtime($co{'author_time'}) . " " . $co{'author_timezone'} . "]<br/>\n";
373 print "committer " . escapeHTML($co{'committer'}) . " [" . gmtime($co{'committer_time'}) . " " . $co{'committer_timezone'} . "]<br/>\n";
374 print "</td>";
375 print "</tr>\n";
376 print "<tr>\n";
377 print "<td></td>\n";
378 print "<td>\n";
379 my $comment = $co{'comment'};
380 foreach my $line (@$comment) {
381 if ($line =~ m/^(signed-off|acked)-by:/i) {
382 print '<div class="signed_off">' . escapeHTML($line) . "<br/></div>\n";
383 } else {
384 print escapeHTML($line) . "<br/>\n";
385 }
386 }
387 print "<br/><br/>\n";
388 print "</td>";
389 print "</tr>\n";
390 } elsif ($action eq "rss") {
391 last if ($i >= 20);
392 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($co{'author_time'});
393 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
394 print "<item>\n\t<title>";
395 printf("%s %02d, %02d:%02d - ", $months[$mon], $mday, $hour, $min);
396 print escapeHTML($co{'title'}) . "</title>\n";
397 print "\t<link> " . $my_url . "/$project/commit/$commit</link>\n";
398 print "\t<description>";
399 my $comment = $co{'comment'};
400 foreach my $line (@$comment) {
401 print escapeHTML($line) . "\n";
402 }
403 print "\t</description>\n";
404 print "</item>\n";
405 }
406 }
407 if ($action eq "log") {
408 print "</table>\n";
409 git_footer_html();
410 } elsif ($action eq "rss") {
411 print "</channel></rss>";
412 }
413} elsif ($action eq "commit") {
414 my %co = git_commit($hash);
415 open my $fd, "-|", "$gitbin/diff-tree", "-r", $co{'parent'}, $hash;
416 my (@difftree) = map { chomp; $_ } <$fd>;
417 close $fd;
418
419 git_header_html();
420 print "<div class=\"head2\"> view\n";
421 print $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | ";
422 print $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "diff");
423 print "</div><br/><br/>\n";
424 print "<div class=\"title\">" . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, $co{'title'}) . "<br/></div>\n";
425 print "<table cellspacing=\"0\" class=\"log\">\n";
426 print "<tr>\n";
427 print "<td class=\"head2\">";
428 print "author " . escapeHTML($co{'author'}) . " [" . gmtime($co{'author_time'}) . " " . $co{'author_timezone'} . "]<br/>\n";
429 print "committer " . escapeHTML($co{'committer'}) . " [" . gmtime($co{'committer_time'}) . " " . $co{'committer_timezone'} . "]<br/>\n";
430 print "commit $hash<br/>\n";
431 print "tree " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'}"}, $co{'tree'}) . "<br/>\n";
432 my $parents = $co{'parents'};
433 foreach my $par (@$parents) {
434 print "parent " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$par"}, $par) . "<br/>\n";
435 }
436 print "</td>";
437 print "</tr>\n";
438 print "<tr>\n";
439 print "<td>\n";
440 my $comment = $co{'comment'};
441 foreach my $line (@$comment) {
442 if ($line =~ m/(signed-off|acked)-by:/i) {
443 print '<div class="signed_off">' . escapeHTML($line) . "<br/></div>\n";
444 } else {
445 print escapeHTML($line) . "<br/>\n";
446 }
447 }
448 print "<br/><br/>\n";
449 print "</td>";
450 print "</tr>\n";
451 print "</table>";
452
453 print "<pre>\n";
454 foreach my $line (@difftree) {
455 # '*100644->100644 blob 9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596 net/ipv4/route.c'
456 # '+100644 blob 4a83ab6cd565d21ab0385bac6643826b83c2fcd4 arch/arm/lib/bitops.h'
457 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
458 my $op = $1;
459 my $mode = $2;
460 my $type = $3;
461 my $id = $4;
462 my $file = $5;
463 $mode =~ m/^([0-7]{6})/;
464 my $modestr = mode_str($1);
465 if ($type eq "blob") {
466 if ($op eq "+") {
467 print "added\t$modestr " . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id"}, $file) . "\n";
468 } elsif ($op eq "-") {
469 print "removed\t$modestr " . $cgi->a({-href => "$my_uri?p=$project;a=blob;h=$id"}, $file) . "\n";
470 } elsif ($op eq "*") {
471 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
472 my $old = $1;
473 my $new = $2;
474 print "changed\t$modestr " . $cgi->a({-href => "$my_uri?p=$project;a=blobdiff;h=$old;hp=$new"}, $file) . "\n";
475 }
476 }
477 }
478 print "</pre>\n";
479 print "<br/>";
480 git_footer_html();
481} elsif ($action eq "blobdiff") {
482 git_header_html();
483 print "<br/><br/>\n";
484 print "<pre>\n";
485 git_diff_html($hash, $hash_parent, $hash, $hash_parent);
486 print "</pre>\n";
487 print "<br/>";
488 git_footer_html();
489} elsif ($action eq "commitdiff") {
490 my %co = git_commit($hash);
491 open my $fd, "-|", "$gitbin/diff-tree", "-r", $co{'parent'}, $hash;
492 my (@difftree) = map { chomp; $_ } <$fd>;
493 close $fd;
494
495 git_header_html();
496 print "<div class=\"head2\"> view\n";
497 print $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, "commit") . " | ";
498 print $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash"}, "diff");
499 print "</div><br/><br/>\n";
500 print "<div class=\"title\">" . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash"}, $co{'title'}) . "<br/></div>\n";
501 print "<pre>\n";
502 foreach my $line (@difftree) {
503 # '*100644->100644 blob 8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154 Makefile'
504 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
505 my $op = $1;
506 my $mode = $2;
507 my $type = $3;
508 my $id = $4;
509 my $file = $5;
510 if ($type eq "blob") {
511 if ($op eq "+") {
512 git_diff_html("", $file, "", $id);
513 } elsif ($op eq "-") {
514 git_diff_html($file, "", $id, "");
515 } elsif ($op eq "*") {
516 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
517 git_diff_html($file, $file, $1, $2);
518 }
519 }
520 }
521 print "</pre>\n";
522 print "<br/>";
523 git_footer_html();
524} else {
525 git_header_html();
526 print "<div class=\"head2\">\n";
527 print "unknown action";
528 print "</div><br/><br/>\n";
529 git_footer_html();
530}