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);
18 my $gitbin = "/home/kay/bin/git";
19 my $gitroot = "/home/kay/public_html";
21 my $myself = $cgi->url(-relative
=> 1);
22 my $url_path = $cgi->url(-path
=> 1);
24 my $project = $cgi->param("project") || "";
25 my $action = $cgi->param("action") || "";
26 my $hash = $cgi->param("hash") || "";
27 my $hash_parent = $cgi->param("hash_parent") || "";
28 my $view_back = $cgi->param("view_back") || 60*60*24;
30 if ($url_path =~ m
#/([^/]+)/commit/([0-9a-fA-F]+)$#) {
34 } elsif ($url_path =~ m
#/([^/]+)/treediff/([0-9a-fA-F]+)$#) {
38 } elsif ($url_path =~ m
#/([^/]+)/diff/([0-9a-fA-F]+)/([0-9a-fA-F]+)$#) {
43 } elsif ($url_path =~ m
#/([^/]+)/log/([0-9]+)$#) {
47 } elsif ($url_path =~ m
#/([^/]+)/log#) {
50 $view_back = 60*60*24;
52 my $projectroot = "$gitroot/$project";
54 $hash =~ s/[^0-9a-fA-F]//g;
55 $hash_parent =~ s/[^0-9a-fA-F]//g;
56 $project =~ s/[^0-9a-zA-Z\-\._]//g;
58 $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/.git/objects";
61 print $cgi->header(-type
=> 'text/html; charset: utf-8');
63 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
66 <title>git - $project $action</title>
67 <style type="text/css">
68 body { font-family: sans-serif; font-size: 12px; margin:25px; }
69 div.body { border-width:1px; border-style:solid; border-color:#D9D8D1; }
70 div.head1 { font-size:20px; padding:8px; background-color: #D9D8D1; font-weight:bold; }
71 div.head1 a:visited { color:#0000cc; }
72 div.head1 a:hover { color:#880000; }
73 div.head1 a:active { color:#880000; }
74 div.head2 { padding:8px; }
75 div.head2 a:visited { color:#0000cc; }
76 div.head2 a:hover { color:#880000; }
77 div.head2 a:active { color:#880000; }
78 div.main { padding:8px; font-family: sans-serif; font-size: 12px; }
79 table { padding:0px; margin:0px; width:100%; }
80 tr { vertical-align:top; }
81 td { padding:8px; margin:0px; font-family: sans-serif; font-size: 12px; }
82 td.head1 { background-color: #D9D8D1; font-weight:bold; }
83 td.head1 a { color:#000000; text-decoration:none; }
84 td.head1 a:hover { color:#880000; text-decoration:underline; }
85 td.head1 a:visited { color:#000000; }
86 td.head2 { background-color: #EDECE6; font-family: monospace; font-size:12px; }
87 td.head3 { background-color: #EDECE6; font-size:10px; }
88 div.add { color: #008800; }
89 div.subtract { color: #CC0000; }
90 div.diff_head { color: #000099; }
91 div.diff_head a:visited { color:#0000cc; }
92 div.diff_line { color: #990099; }
94 a:hover { color:#880000; }
95 a:visited { color:#880000; }
96 a:active { color:#880000; }
101 print "<div class=\"body\">\n";
102 print "<div class=\"head1\">";
103 print "<a href=\"http://kernel.org/pub/software/scm/git/\"><img src=\"git_logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/></a>";
104 print $cgi->a({-href
=> "$myself"}, "projects");
105 if ($project ne "") {
106 print " / " . $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=" . 60*60*24}, $project);
116 print $cgi->end_html();
120 my $old_name = shift || "/dev/null";
121 my $new_name = shift || "/dev/null";
125 my $tmp_old = "/dev/null";
126 my $tmp_new = "/dev/null";
127 my $old_label = "/dev/null";
128 my $new_label = "/dev/null";
130 # create temp from-file
132 open my $fd2, "> $gittmp/$old";
133 open my $fd, "-|", "$gitbin/cat-file", "blob", $old;
134 while (my $line = <$fd>) {
139 $tmp_old = "$gittmp/$old";
140 $old_label = "a/$old_name";
145 open my $fd2, "> $gittmp/$new";
146 open my $fd, "-|", "$gitbin/cat-file", "blob", $new;
147 while (my $line = <$fd>) {
152 $tmp_new = "$gittmp/$new";
153 $new_label = "b/$new_name";
156 open my $fd, "-|", "/usr/bin/diff", "-L", $old_label, "-L", $new_label, "-u", "-p", $tmp_old, $tmp_new;
157 print '<div class="diff_head">===== ';
159 print $cgi->a({-href
=> "$myself?project=$project;action=blob;hash=$old"}, $old);
165 print $cgi->a({-href
=> "$myself?project=$project;action=blob;hash=$new"}, $new);
169 print ' =====</div>';
170 while (my $line = <$fd>) {
171 my $char = substr($line,0,1);
172 print '<div class="add">' if $char eq '+';
173 print '<div class="subtract">' if $char eq '-';
174 print '<div class="diff_line">' if $char eq '@';
175 print escapeHTML
($line);
176 print '</div>' if $char eq '+' or $char eq '-' or $char eq '@';
179 unlink("$gittmp/$new");
180 unlink("$gittmp/$old");
183 if ($project eq "") {
184 open my $fd, "-|", "ls", "-1", $gitroot;
185 my (@path) = map { chomp; $_ } <$fd>;
188 print "<br/><br/><div class=\"main\">\n";
189 foreach my $line (@path) {
190 if (-e
"$gitroot/$line/.git/HEAD") {
191 print $cgi->a({-href
=> "$myself?project=$line"}, $line) . "<br/>\n";
200 print $cgi->redirect("$myself?project=$project;action=log;view_back=$view_back");
204 if ($action eq "blob") {
206 print "<br/><br/><div class=\"main\">\n";
208 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
210 while (my $line = <$fd>) {
212 print "$nr\t" . escapeHTML
($line);;
218 } elsif ($action eq "tree") {
220 open my $fd, "$projectroot/.git/HEAD";
226 open my $fd, "-|", "$gitbin/ls-tree", $hash;
227 my (@entries) = map { chomp; $_ } <$fd>;
230 print "<br/><br/><div class=\"main\">\n";
232 foreach my $line (@entries) {
233 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
234 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
238 if ($t_type eq "blob") {
239 print "BLOB\t" . $cgi->a({-href
=> "$myself?project=$project;action=blob;hash=$3"}, $4) . "\n";
240 } elsif ($t_type eq "tree") {
241 print "TREE\t" . $cgi->a({-href
=> "$myself?project=$project;action=tree;hash=$3"}, $4) . "\n";
247 } elsif ($action eq "log" || $action eq "show_log" ) {
248 open my $fd, "$projectroot/.git/HEAD";
252 open $fd, "-|", "$gitbin/rev-tree", $head;
253 my (@revtree) = map { chomp; $_ } <$fd>;
256 print "<div class=\"head2\">\n";
258 print $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=" . 60*60*24}, "last day") . " | ";
259 print $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=" . 60*60*24*7}, "week") . " | ";
260 print $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=" . 60*60*24*30}, "month") . " | ";
261 print $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=" . 60*60*24*365}, "year") . " | ";
262 print $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=-1"}, "all") . "<br/>\n";
263 print "<br/><br/>\n";
265 print "<table cellspacing=\"0\" class=\"log\">\n";
266 foreach my $rev (reverse sort @revtree) {
267 # '1114106118 755e3010ee10dadf42a8a80770e1b115fb038d9b:1 2af17b4854036a1c2ec6c101d93c8dd1ed80d24e:1'
268 last if !($rev =~ m/^([0-9]+) ([0-9a-fA-F]+).* ([0-9a-fA-F]+)/);
279 my $committer_timezone;
283 open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
284 while (my $line = <$fd>) {
287 if ($line =~ m/^tree (.*)$/) {
289 } elsif ($line =~ m/^parent (.*)$/) {
291 } elsif ($line =~ m/^committer (.*>) ([0-9]+) (.*)$/) {
293 $committer_time = $2;
294 $committer_timezone = $3;
295 } elsif ($line =~ m/^author (.*>) ([0-9]+) (.*)$/) {
298 $author_timezone = $3;
302 $shortlog = escapeHTML
($shortlog);
303 $comment = $shortlog . "<br/>";
304 while (my $line = <$fd>) {
306 $comment .= escapeHTML
($line) . "<br/>\n";
309 my $age = time-$committer_time;
310 last if ($view_back > 0 && $age > $view_back);
313 if ($age > 60*60*24*365*2) {
314 $age_string = int $age/60/60/24/365;
315 $age_string .= " years ago";
316 } elsif ($age > 60*60*24*365/12*2) {
317 $age_string = int $age/60/60/24/365/12;
318 $age_string .= " months ago";
319 } elsif ($age > 60*60*24*7*2) {
320 $age_string = int $age/60/60/24/7;
321 $age_string .= " weeks ago";
322 } elsif ($age > 60*60*24*2) {
323 $age_string = int $age/60/60/24;
324 $age_string .= " days ago";
325 } elsif ($age > 60*60*2) {
326 $age_string = int $age/60/60;
327 $age_string .= " hours ago";
328 } elsif ($age > 60*2) {
329 $age_string = int $age/60;
330 $age_string .= " minutes ago";
333 print "<td class=\"head1\">" . $age_string . "</td>\n";
334 print "<td class=\"head1\">" . $cgi->a({-href
=> "$myself?project=$project;action=commit;hash=$commit"}, $shortlog) . "</td>";
337 print "<td class=\"head3\">";
338 print $cgi->a({-href
=> "$myself?project=$project;action=treediff;hash=$commit"}, "view diff") . "<br/>\n";
339 print $cgi->a({-href
=> "$myself?project=$project;action=commit;hash=$commit"}, "view commit") . "<br/>\n";
340 print $cgi->a({-href
=> "$myself?project=$project;action=tree;hash=$tree"}, "view tree") . "<br/>\n";
342 print "<td class=\"head2\">\n";
343 print "author " . escapeHTML
($author) . " [" . gmtime($author_time) . " " . $author_timezone . "]<br/>\n";
344 print "committer " . escapeHTML
($committer) . " [" . gmtime($committer_time) . " " . $committer_timezone . "]<br/>\n";
345 print "commit $commit<br/>\n";
346 print "tree $tree<br/>\n";
347 foreach my $par (@parents) {
348 print "parent $par<br/>\n";
355 print "$comment<br/><br/>\n";
361 } elsif ($action eq "commit") {
363 open my $fd, "-|", "$gitbin/cat-file", "commit", $hash;
364 while (my $line = <$fd>) {
367 if ($line =~ m/^parent (.*)$/ && $parent eq "") {
371 my $shortlog = <$fd>;
372 $shortlog = escapeHTML
($shortlog);
375 open $fd, "-|", "$gitbin/diff-tree", "-r", $parent, $hash;
376 my (@difftree) = map { chomp; $_ } <$fd>;
380 print "<div class=\"main\">\n";
381 print "view " . $cgi->a({-href
=> "$myself?project=$project;action=treediff;hash=$hash"}, "diff") . "<br/><br/><br/>\n";
382 print "$shortlog<br/>\n";
384 foreach my $line (@difftree) {
385 # '*100644->100644 blob 9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596 net/ipv4/route.c'
386 # '+100644 blob 4a83ab6cd565d21ab0385bac6643826b83c2fcd4 arch/arm/lib/bitops.h'
387 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
393 if ($type eq "blob") {
395 print "NEW\t" . $cgi->a({-href
=> "$myself?project=$project;action=blob;hash=$id"}, $file) . "\n";
396 } elsif ($op eq "-") {
397 print "DEL\t" . $cgi->a({-href
=> "$myself?project=$project;action=blob;hash=$id"}, $file) . "\n";
398 } elsif ($op eq "*") {
399 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
402 print "CHANGED\t" . $cgi->a({-href
=> "$myself?project=$project;action=diff;hash=$old;hash_parent=$new"}, $file) . "\n";
409 } elsif ($action eq "diff") {
411 print "<br/><br/><div class=\"main\">\n";
413 git_diff
($hash, $hash_parent, $hash, $hash_parent);
417 } elsif ($action eq "treediff") {
419 open my $fd, "-|", "$gitbin/cat-file", "commit", $hash;
420 while (my $line = <$fd>) {
423 if ($line =~ m/^parent (.*)$/ && $parent eq "") {
427 my $shortlog = <$fd>;
428 $shortlog = escapeHTML
($shortlog);
431 open $fd, "-|", "$gitbin/diff-tree", "-r", $parent, $hash;
432 my (@difftree) = map { chomp; $_ } <$fd>;
436 print "<div class=\"main\">\n";
437 print "view " . $cgi->a({-href
=> "$myself?project=$project;action=commit;hash=$hash"}, "commit") . "<br/><br/><br/>\n";
438 print "$shortlog<br/>\n";
440 foreach my $line (@difftree) {
441 # '*100644->100644 blob 8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154 Makefile'
442 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
448 if ($type eq "blob") {
450 git_diff
("", $file, "", $id);
451 } elsif ($op eq "-") {
452 git_diff
($file, "", $id, "");
453 } elsif ($op eq "*") {
454 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
455 git_diff
($file, $file, $1, $2);
464 print "<br/><br/><div class=\"main\">\n";
465 print "unknown action\n";