2dca8618325a503ba18ba670445152fdd70b6f22
1#!/usr/bin/perl
2
3# gitweb.pl - simple web interface to track changes in git repositories
4#
5# Version 031
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 = "/home/kay/public_html";
20my $defaultprojects = ".";
21my $gitbin = "/home/kay/bin/git";
22my $gittmp = "/tmp";
23my $my_url = $cgi->url();
24my $my_uri = $cgi->url(-absolute => 1);
25my $my_url_parm = $cgi->url(-path => 1);
26$my_url_parm =~ s/.*$my_uri//;
27
28my $project = "";
29my $action = "";
30my $hash = "";
31my $hash_parent = "";
32my $view_back = 1;
33
34# get values from url
35if ($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;
88}
89
90# sanitize input
91$project =~ s#\/\.+##g;
92
93$ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/$project/.git/objects";
94
95sub git_header_html {
96 print $cgi->header(-type => 'text/html', -charset => 'utf-8');
97print <<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>
136EOF
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") . " / ";
143 }
144 if ($project ne "") {
145 print $cgi->a({-href => "$my_uri/$project/log"}, $project);
146 }
147 if ($action ne "") {
148 print " / $action";
149 }
150 print "</div>\n";
151}
152
153sub git_footer_html {
154 print "</div>\n";
155 print "</body>\n</html>";
156}
157
158sub git_head {
159 open my $fd, "$projectroot/$project/.git/HEAD";
160 my $head = <$fd>;
161 close $fd;
162 chomp $head;
163 return $head;
164}
165
166sub git_commit {
167 my $commit = shift;
168 my %co;
169 my @parents;
170
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;
187 }
188 }
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;
196}
197
198sub git_diff_html {
199 my $from_name = shift || "/dev/null";
200 my $to_name = shift || "/dev/null";
201 my $from = shift;
202 my $to = shift;
203
204 my $from_tmp = "/dev/null";
205 my $to_tmp = "/dev/null";
206 my $from_label = "/dev/null";
207 my $to_label = "/dev/null";
208 my $pid = $$;
209
210 # create temp from-file
211 if ($from ne "") {
212 $from_tmp = "$gittmp/gitweb_" . $$ . "_from";
213 open my $fd2, "> $from_tmp";
214 open my $fd, "-|", "$gitbin/cat-file", "blob", $from;
215 my @file = <$fd>;
216 print $fd2 @file;
217 close $fd2;
218 close $fd;
219 $from_label = "a/$from_name";
220 }
221
222 # create tmp to-file
223 if ($to ne "") {
224 $to_tmp = "$gittmp/gitweb_" . $$ . "_to";
225 open my $fd2, "> $to_tmp";
226 open my $fd, "-|", "$gitbin/cat-file", "blob", $to;
227 my @file = <$fd>;
228 print $fd2 @file;
229 close $fd2;
230 close $fd;
231 $to_label = "b/$to_name";
232 }
233
234 open my $fd, "-|", "/usr/bin/diff", "-L", $from_label, "-L", $to_label, "-u", "-p", $from_tmp, $to_tmp;
235 print "<span style =\"color: #000099;\">===== ";
236 if ($from ne "") {
237 print $cgi->a({-href => "$my_uri/$project/blob/$from"}, $from);
238 } else {
239 print $from_name;
240 }
241 print " vs ";
242 if ($to ne "") {
243 print $cgi->a({-href => "$my_uri/$project/blob/$to"}, $to);
244 } else {
245 print $to_name;
246 }
247 print " =====</span>\n";
248 while (my $line = <$fd>) {
249 my $char = substr($line,0,1);
250 print '<span style ="color: #008800;">' if $char eq '+';
251 print '<span style ="color: #CC0000;">' if $char eq '-';
252 print '<span style ="color: #990099;">' if $char eq '@';
253 print escapeHTML($line);
254 print '</span>' if $char eq '+' or $char eq '-' or $char eq '@';
255 }
256 close $fd;
257
258 if ($from ne "") {
259 unlink("$from_tmp");
260 }
261 if ($to ne "") {
262 unlink("$to_tmp");
263 }
264}
265
266sub mode_str {
267 my $perms = oct shift;
268 my $modestr;
269 if ($perms & 040000) { $modestr .= 'd' } else { $modestr .= '-' };
270 for (my $i = 0; $i < 3; $i++) {
271 if ($perms & 0400) { $modestr .= 'r' } else { $modestr .= '-' };
272 if ($perms & 0200) { $modestr .= 'w' } else { $modestr .= '-' };
273 if ($perms & 0100) { $modestr .= 'x' } else { $modestr .= '-' };
274 $perms <<= 3;
275 }
276 return $modestr;
277}
278
279# show list of default projects
280if ($project eq "") {
281 opendir(my $fd, "$projectroot/$defaultprojects");
282 my (@path) = grep(!/^\./, readdir($fd));
283 closedir($fd);
284 git_header_html();
285 print "<div class=\"head2\">\n";
286 print "<br/><br/>\n";
287 foreach my $line (@path) {
288 if (-e "$projectroot/$defaultprojects/$line/.git/HEAD") {
289 print $cgi->a({-href => "$my_uri/$defaultprojects/$line/log"}, $line) . "<br/>\n";
290 }
291 }
292 print "</div><br/>";
293 git_footer_html();
294 exit;
295}
296
297if ($action eq "blob") {
298 git_header_html();
299 print "<br/><br/>\n";
300 print "<pre>\n";
301 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
302 my $nr;
303 while (my $line = <$fd>) {
304 $nr++;
305 print "$nr\t" . escapeHTML($line);;
306 }
307 close $fd;
308 print "</pre>\n";
309 print "<br/>";
310 git_footer_html();
311} elsif ($action eq "tree") {
312 if ($hash eq "") {
313 $hash = git_head();
314 }
315 open my $fd, "-|", "$gitbin/ls-tree", $hash;
316 my (@entries) = map { chomp; $_ } <$fd>;
317 close $fd;
318 git_header_html();
319 print "<br/><br/>\n";
320 print "<pre>\n";
321 foreach my $line (@entries) {
322 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
323 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
324 my $t_mode = $1;
325 my $t_type = $2;
326 my $t_hash = $3;
327 my $t_name = $4;
328 if ($t_type eq "blob") {
329 print mode_str($t_mode). " " . $cgi->a({-href => "$my_uri/$project/blob/$t_hash"}, $t_name) . "\n";
330 } elsif ($t_type eq "tree") {
331 print mode_str($t_mode). " " . $cgi->a({-href => "$my_uri/$project/tree/$t_hash"}, $t_name) . "\n";
332 }
333 }
334 print "</pre>\n";
335 print "<br/>";
336 git_footer_html();
337} elsif ($action eq "log" || $action eq "rss") {
338 open my $fd, "-|", "$gitbin/rev-list", git_head();
339 my (@revtree) = map { chomp; $_ } <$fd>;
340 close $fd;
341
342 if ($action eq "log") {
343 git_header_html();
344 print "<div class=\"head2\">\n";
345 print "view ";
346 print $cgi->a({-href => "$my_uri/$project/log"}, "last day") . " | ";
347 print $cgi->a({-href => "$my_uri/$project/log/7"}, "week") . " | ";
348 print $cgi->a({-href => "$my_uri/$project/log/31"}, "month") . " | ";
349 print $cgi->a({-href => "$my_uri/$project/log/365"}, "year") . " | ";
350 print $cgi->a({-href => "$my_uri/$project/log/0"}, "all") . "<br/>\n";
351 print "<br/><br/>\n";
352 print "</div>\n";
353 print "<table cellspacing=\"0\" class=\"log\">\n";
354 } elsif ($action eq "rss") {
355 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
356 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
357 "<rss version=\"0.91\">\n";
358 print "<channel>\n";
359 print "<title>$project</title>\n".
360 "<link> " . $my_url . "/$project/log</link>\n".
361 "<description>$project log</description>\n".
362 "<language>en</language>\n";
363 }
364
365 for (my $i = 0; $i <= $#revtree; $i++) {
366 my $commit = $revtree[$i];
367
368 my %co = git_commit($commit);
369 my $age = time - $co{'committer_time'};
370 my $age_string;
371 if ($age > 60*60*24*365*2) {
372 $age_string = int $age/60/60/24/365;
373 $age_string .= " years ago";
374 } elsif ($age > 60*60*24*365/12*2) {
375 $age_string = int $age/60/60/24/365/12;
376 $age_string .= " months ago";
377 } elsif ($age > 60*60*24*7*2) {
378 $age_string = int $age/60/60/24/7;
379 $age_string .= " weeks ago";
380 } elsif ($age > 60*60*24*2) {
381 $age_string = int $age/60/60/24;
382 $age_string .= " days ago";
383 } elsif ($age > 60*60*2) {
384 $age_string = int $age/60/60;
385 $age_string .= " hours ago";
386 } elsif ($age > 60*2) {
387 $age_string = int $age/60;
388 $age_string .= " minutes ago";
389 }
390 if ($action eq "log") {
391 if ($view_back > 0 && $age > $view_back*60*60*24) {
392 if ($i == 0) {
393 print "<tr>\n";
394 print "<td class=\"head1\"> Last change $age_string. </td>\n";
395 print "</tr>\n";
396 }
397 last;
398 }
399 print "<tr>\n";
400 print "<td class=\"head1\">" . $age_string . "</td>\n";
401 print "<td class=\"head1\">" . $cgi->a({-href => "$my_uri/$project/commit/$commit"}, $co{'title'}) . "</td>";
402 print "</tr>\n";
403 print "<tr>\n";
404 print "<td class=\"head3\">";
405 print $cgi->a({-href => "$my_uri/$project/commit/$commit"}, "view commit") . "<br/>\n";
406 print $cgi->a({-href => "$my_uri/$project/commitdiff/$commit"}, "view diff") . "<br/>\n";
407 print "</td>\n";
408 print "<td class=\"head2\">\n";
409 print "author " . escapeHTML($co{'author'}) . " [" . gmtime($co{'author_time'}) . " " . $co{'author_timezone'} . "]<br/>\n";
410 print "committer " . escapeHTML($co{'committer'}) . " [" . gmtime($co{'committer_time'}) . " " . $co{'committer_timezone'} . "]<br/>\n";
411 print "</td>";
412 print "</tr>\n";
413 print "<tr>\n";
414 print "<td></td>\n";
415 print "<td>\n";
416 my $comment = $co{'comment'};
417 foreach my $line (@$comment) {
418 if ($line =~ m/^(signed-off|acked)-by:/i) {
419 print '<div class="signed_off">' . escapeHTML($line) . "<br/></div>\n";
420 } else {
421 print escapeHTML($line) . "<br/>\n";
422 }
423 }
424 print "<br/><br/>\n";
425 print "</td>";
426 print "</tr>\n";
427 } elsif ($action eq "rss") {
428 last if ($i >= 20);
429 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($co{'author_time'});
430 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
431 print "<item>\n\t<title>";
432 printf("%s %02d, %02d:%02d - ", $months[$mon], $mday, $hour, $min);
433 print escapeHTML($co{'title'}) . "</title>\n";
434 print "\t<link> " . $my_url . "/$project/commit/$commit</link>\n";
435 print "\t<description>";
436 my $comment = $co{'comment'};
437 foreach my $line (@$comment) {
438 print escapeHTML($line) . "\n";
439 }
440 print "\t</description>\n";
441 print "</item>\n";
442 }
443 }
444 if ($action eq "log") {
445 print "</table>\n";
446 git_footer_html();
447 } elsif ($action eq "rss") {
448 print "</channel></rss>";
449 }
450} elsif ($action eq "commit") {
451 my %co = git_commit($hash);
452 open my $fd, "-|", "$gitbin/diff-tree", "-r", $co{'parent'}, $hash;
453 my (@difftree) = map { chomp; $_ } <$fd>;
454 close $fd;
455
456 git_header_html();
457 print "<div class=\"head2\"> view\n";
458 print $cgi->a({-href => "$my_uri/$project/commit/$hash"}, "commit") . " | ";
459 print $cgi->a({-href => "$my_uri/$project/commitdiff/$hash"}, "diff");
460 print "</div><br/><br/>\n";
461 print "<div class=\"title\">" . $cgi->a({-href => "$my_uri/$project/commitdiff/$hash"}, $co{'title'}) . "<br/></div>\n";
462 print "<table cellspacing=\"0\" class=\"log\">\n";
463 print "<tr>\n";
464 print "<td class=\"head2\">";
465 print "author " . escapeHTML($co{'author'}) . " [" . gmtime($co{'author_time'}) . " " . $co{'author_timezone'} . "]<br/>\n";
466 print "committer " . escapeHTML($co{'committer'}) . " [" . gmtime($co{'committer_time'}) . " " . $co{'committer_timezone'} . "]<br/>\n";
467 print "commit $hash<br/>\n";
468 print "tree " . $cgi->a({-href => "$my_uri/$project/tree/$co{'tree'}"}, $co{'tree'}) . "<br/>\n";
469 my $parents = $co{'parents'};
470 foreach my $par (@$parents) {
471 print "parent " . $cgi->a({-href => "$my_uri/$project/tree/$par"}, $par) . "<br/>\n";
472 }
473 print "</td>";
474 print "</tr>\n";
475 print "<tr>\n";
476 print "<td>\n";
477 my $comment = $co{'comment'};
478 foreach my $line (@$comment) {
479 if ($line =~ m/(signed-off|acked)-by:/i) {
480 print '<div class="signed_off">' . escapeHTML($line) . "<br/></div>\n";
481 } else {
482 print escapeHTML($line) . "<br/>\n";
483 }
484 }
485 print "<br/><br/>\n";
486 print "</td>";
487 print "</tr>\n";
488 print "</table>";
489
490 print "<pre>\n";
491 foreach my $line (@difftree) {
492 # '*100644->100644 blob 9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596 net/ipv4/route.c'
493 # '+100644 blob 4a83ab6cd565d21ab0385bac6643826b83c2fcd4 arch/arm/lib/bitops.h'
494 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
495 my $op = $1;
496 my $mode = $2;
497 my $type = $3;
498 my $id = $4;
499 my $file = $5;
500 $mode =~ m/^([0-7]{6})/;
501 my $modestr = mode_str($1);
502 if ($type eq "blob") {
503 if ($op eq "+") {
504 print "added\t$modestr " . $cgi->a({-href => "$my_uri/$project/blob/$id"}, $file) . "\n";
505 } elsif ($op eq "-") {
506 print "removed\t$modestr " . $cgi->a({-href => "$my_uri/$project/blob/$id"}, $file) . "\n";
507 } elsif ($op eq "*") {
508 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
509 my $old = $1;
510 my $new = $2;
511 print "changed\t$modestr " . $cgi->a({-href => "$my_uri/$project/blobdiff/$old/$new"}, $file) . "\n";
512 }
513 }
514 }
515 print "</pre>\n";
516 print "<br/>";
517 git_footer_html();
518} elsif ($action eq "blobdiff") {
519 git_header_html();
520 print "<br/><br/>\n";
521 print "<pre>\n";
522 git_diff_html($hash, $hash_parent, $hash, $hash_parent);
523 print "</pre>\n";
524 print "<br/>";
525 git_footer_html();
526} elsif ($action eq "commitdiff") {
527 my %co = git_commit($hash);
528 open my $fd, "-|", "$gitbin/diff-tree", "-r", $co{'parent'}, $hash;
529 my (@difftree) = map { chomp; $_ } <$fd>;
530 close $fd;
531
532 git_header_html();
533 print "<div class=\"head2\"> view\n";
534 print $cgi->a({-href => "$my_uri/$project/commit/$hash"}, "commit") . " | ";
535 print $cgi->a({-href => "$my_uri/$project/commitdiff/$hash"}, "diff");
536 print "</div><br/><br/>\n";
537 print "<div class=\"title\">" . $cgi->a({-href => "$my_uri/$project/commit/$hash"}, $co{'title'}) . "<br/></div>\n";
538 print "<pre>\n";
539 foreach my $line (@difftree) {
540 # '*100644->100644 blob 8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154 Makefile'
541 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
542 my $op = $1;
543 my $mode = $2;
544 my $type = $3;
545 my $id = $4;
546 my $file = $5;
547 if ($type eq "blob") {
548 if ($op eq "+") {
549 git_diff_html("", $file, "", $id);
550 } elsif ($op eq "-") {
551 git_diff_html($file, "", $id, "");
552 } elsif ($op eq "*") {
553 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
554 git_diff_html($file, $file, $1, $2);
555 }
556 }
557 }
558 print "</pre>\n";
559 print "<br/>";
560 git_footer_html();
561}