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);
23 my $project = $cgi->param("project") || "";
24 my $action = $cgi->param("action") || "";
25 my $hash = $cgi->param("hash") || "";
26 my $hash_parent = $cgi->param("hash_parent") || "";
27 my $view_back = $cgi->param("view_back") || 60*60*24;
28 my $projectroot = "$gitroot/$project";
29 $hash =~ s/[^0-9a-fA-F]//g;
30 $hash_parent =~ s/[^0-9a-fA-F]//g;
31 $project =~ s/[^0-9a-zA-Z\-\._]//g;
33 $ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/.git/objects";
36 print $cgi->header(-type
=> 'text/html; charset: utf-8');
38 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
41 <title>git - $project $action</title>
42 <style type="text/css">
43 body { font-family: sans-serif; font-size: 12px; margin:25px; }
44 div.body { border-width:1px; border-style:solid; border-color:#D9D8D1; }
45 div.head1 { font-size:20px; padding:8px; background-color: #D9D8D1; font-weight:bold; }
46 div.head1 a:visited { color:#0000cc; }
47 div.head1 a:hover { color:#880000; }
48 div.head1 a:active { color:#880000; }
49 div.head2 { padding:8px; }
50 div.head2 a:visited { color:#0000cc; }
51 div.head2 a:hover { color:#880000; }
52 div.head2 a:active { color:#880000; }
53 div.main { padding:8px; font-family: sans-serif; font-size: 12px; }
54 table { padding:0px; margin:0px; width:100%; }
55 tr { vertical-align:top; }
56 td { padding:8px; margin:0px; font-family: sans-serif; font-size: 12px; }
57 td.head1 { background-color: #D9D8D1; font-weight:bold; }
58 td.head1 a { color:#000000; text-decoration:none; }
59 td.head1 a:hover { color:#880000; text-decoration:underline; }
60 td.head1 a:visited { color:#000000; }
61 td.head2 { background-color: #EDECE6; font-family: monospace; font-size:12px; }
62 td.head3 { background-color: #EDECE6; font-size:10px; }
63 div.add { color: #008800; }
64 div.subtract { color: #CC0000; }
65 div.diff_head { color: #000099; }
66 div.diff_head a:visited { color:#0000cc; }
67 div.diff_line { color: #990099; }
69 a:hover { color:#880000; }
70 a:visited { color:#880000; }
71 a:active { color:#880000; }
76 print "<div class=\"body\">\n";
77 print "<div class=\"head1\">";
78 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>";
79 print $cgi->a({-href
=> "$myself"}, "projects");
81 print " / " . $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=" . 60*60*24}, $project);
91 print $cgi->end_html();
95 my $old_name = shift || "/dev/null";
96 my $new_name = shift || "/dev/null";
100 my $tmp_old = "/dev/null";
101 my $tmp_new = "/dev/null";
102 my $old_label = "/dev/null";
103 my $new_label = "/dev/null";
105 # create temp from-file
107 open my $fd2, "> $gittmp/$old";
108 open my $fd, "-|", "$gitbin/cat-file", "blob", $old;
109 while (my $line = <$fd>) {
114 $tmp_old = "$gittmp/$old";
115 $old_label = "a/$old_name";
120 open my $fd2, "> $gittmp/$new";
121 open my $fd, "-|", "$gitbin/cat-file", "blob", $new;
122 while (my $line = <$fd>) {
127 $tmp_new = "$gittmp/$new";
128 $new_label = "b/$new_name";
131 open my $fd, "-|", "/usr/bin/diff", "-L", $old_label, "-L", $new_label, "-u", "-p", $tmp_old, $tmp_new;
132 print '<div class="diff_head">===== ';
134 print $cgi->a({-href
=> "$myself?project=$project;action=blob;hash=$old"}, $old);
140 print $cgi->a({-href
=> "$myself?project=$project;action=blob;hash=$new"}, $new);
144 print ' =====</div>';
145 while (my $line = <$fd>) {
146 my $char = substr($line,0,1);
147 print '<div class="add">' if $char eq '+';
148 print '<div class="subtract">' if $char eq '-';
149 print '<div class="diff_line">' if $char eq '@';
150 print escapeHTML
($line);
151 print '</div>' if $char eq '+' or $char eq '-' or $char eq '@';
154 unlink("$gittmp/$new");
155 unlink("$gittmp/$old");
158 if ($project eq "") {
159 open my $fd, "-|", "ls", "-1", $gitroot;
160 my (@path) = map { chomp; $_ } <$fd>;
163 print "<br/><br/><div class=\"main\">\n";
164 foreach my $line (@path) {
165 if (-e
"$gitroot/$line/.git/HEAD") {
166 print $cgi->a({-href
=> "$myself?project=$line"}, $line) . "<br/>\n";
175 print $cgi->redirect("$myself?project=$project;action=log;view_back=$view_back");
179 if ($action eq "blob") {
181 print "<br/><br/><div class=\"main\">\n";
183 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
185 while (my $line = <$fd>) {
187 print "$nr\t" . escapeHTML
($line);;
193 } elsif ($action eq "tree") {
195 open my $fd, "$projectroot/.git/HEAD";
201 open my $fd, "-|", "$gitbin/ls-tree", $hash;
202 my (@entries) = map { chomp; $_ } <$fd>;
205 print "<br/><br/><div class=\"main\">\n";
207 foreach my $line (@entries) {
208 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
209 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
213 if ($t_type eq "blob") {
214 print "BLOB\t" . $cgi->a({-href
=> "$myself?project=$project;action=blob;hash=$3"}, $4) . "\n";
215 } elsif ($t_type eq "tree") {
216 print "TREE\t" . $cgi->a({-href
=> "$myself?project=$project;action=tree;hash=$3"}, $4) . "\n";
222 } elsif ($action eq "log" || $action eq "show_log" ) {
223 open my $fd, "$projectroot/.git/HEAD";
227 open $fd, "-|", "$gitbin/rev-tree", $head;
228 my (@revtree) = map { chomp; $_ } <$fd>;
231 print "<div class=\"head2\">\n";
233 print $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=" . 60*60*24}, "last day") . " | ";
234 print $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=" . 60*60*24*7}, "week") . " | ";
235 print $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=" . 60*60*24*30}, "month") . " | ";
236 print $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=" . 60*60*24*365}, "year") . " | ";
237 print $cgi->a({-href
=> "$myself?project=$project;action=log;view_back=-1"}, "all") . "<br/>\n";
238 print "<br/><br/>\n";
240 print "<table cellspacing=\"0\" class=\"log\">\n";
241 foreach my $rev (reverse sort @revtree) {
242 # '1114106118 755e3010ee10dadf42a8a80770e1b115fb038d9b:1 2af17b4854036a1c2ec6c101d93c8dd1ed80d24e:1'
243 last if !($rev =~ m/^([0-9]+) ([0-9a-fA-F]+).* ([0-9a-fA-F]+)/);
254 my $committer_timezone;
258 open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
259 while (my $line = <$fd>) {
262 if ($line =~ m/^tree (.*)$/) {
264 } elsif ($line =~ m/^parent (.*)$/) {
266 } elsif ($line =~ m/^committer (.*>) ([0-9]+) (.*)$/) {
268 $committer_time = $2;
269 $committer_timezone = $3;
270 } elsif ($line =~ m/^author (.*>) ([0-9]+) (.*)$/) {
273 $author_timezone = $3;
274 $author =~ m/^(.*) </;
279 $shortlog = escapeHTML
($shortlog);
280 $comment = $shortlog . "<br/>";
281 while (my $line = <$fd>) {
283 $comment .= escapeHTML
($line) . "<br/>\n";
286 my $age = time-$committer_time;
287 last if ($view_back > 0 && $age > $view_back);
290 if ($age > 60*60*24*365*2) {
291 $age_string = int $age/60/60/24/365;
292 $age_string .= " years ago";
293 } elsif ($age > 60*60*24*365/12*2) {
294 $age_string = int $age/60/60/24/365/12;
295 $age_string .= " months ago";
296 } elsif ($age > 60*60*24*7*2) {
297 $age_string = int $age/60/60/24/7;
298 $age_string .= " weeks ago";
299 } elsif ($age > 60*60*24*2) {
300 $age_string = int $age/60/60/24;
301 $age_string .= " days ago";
302 } elsif ($age > 60*60*2) {
303 $age_string = int $age/60/60;
304 $age_string .= " hours ago";
305 } elsif ($age > 60*2) {
306 $age_string = int $age/60;
307 $age_string .= " minutes ago";
310 print "<td class=\"head1\">" . $age_string . "</td>\n";
311 print "<td class=\"head1\">" . $cgi->a({-href
=> "$myself?project=$project;action=commit;hash=$commit"}, $shortlog) . "</td>";
314 print "<td class=\"head3\">";
315 print $cgi->a({-href
=> "$myself?project=$project;action=treediff;hash=$commit"}, "view diff") . "<br/>\n";
316 print $cgi->a({-href
=> "$myself?project=$project;action=commit;hash=$commit"}, "view commit") . "<br/>\n";
317 print $cgi->a({-href
=> "$myself?project=$project;action=tree;hash=$tree"}, "view tree") . "<br/>\n";
319 print "<td class=\"head2\">\n";
320 print "author " . escapeHTML
($author) . " [" . gmtime($author_time) . " " . $author_timezone . "]<br/>\n";
321 print "committer " . escapeHTML
($committer) . " [" . gmtime($committer_time) . " " . $committer_timezone . "]<br/>\n";
322 print "commit $commit<br/>\n";
323 print "tree $tree<br/>\n";
324 foreach my $par (@parents) {
325 print "parent $par<br/>\n";
332 print "$comment<br/><br/>\n";
338 } elsif ($action eq "commit") {
340 open my $fd, "-|", "$gitbin/cat-file", "commit", $hash;
341 while (my $line = <$fd>) {
344 if ($line =~ m/^parent (.*)$/ && $parent eq "") {
348 my $shortlog = <$fd>;
349 $shortlog = escapeHTML
($shortlog);
352 open $fd, "-|", "$gitbin/diff-tree", "-r", $parent, $hash;
353 my (@difftree) = map { chomp; $_ } <$fd>;
357 print "<div class=\"main\">\n";
358 print "view " . $cgi->a({-href
=> "$myself?project=$project;action=treediff;hash=$hash"}, "diff") . "<br/><br/><br/>\n";
359 print "$shortlog<br/>\n";
361 foreach my $line (@difftree) {
362 # '*100644->100644 blob 9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596 net/ipv4/route.c'
363 # '+100644 blob 4a83ab6cd565d21ab0385bac6643826b83c2fcd4 arch/arm/lib/bitops.h'
364 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
370 if ($type eq "blob") {
372 print "NEW\t" . $cgi->a({-href
=> "$myself?project=$project;action=blob;hash=$id"}, $file) . "\n";
373 } elsif ($op eq "-") {
374 print "DEL\t" . $cgi->a({-href
=> "$myself?project=$project;action=blob;hash=$id"}, $file) . "\n";
375 } elsif ($op eq "*") {
376 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
379 print "CHANGED\t" . $cgi->a({-href
=> "$myself?project=$project;action=diff;hash=$old;hash_parent=$new"}, $file) . "\n";
386 } elsif ($action eq "diff") {
388 print "<br/><br/><div class=\"main\">\n";
390 git_diff
($hash, $hash_parent, $hash, $hash_parent);
394 } elsif ($action eq "treediff") {
396 open my $fd, "-|", "$gitbin/cat-file", "commit", $hash;
397 while (my $line = <$fd>) {
400 if ($line =~ m/^parent (.*)$/ && $parent eq "") {
404 my $shortlog = <$fd>;
405 $shortlog = escapeHTML
($shortlog);
408 open $fd, "-|", "$gitbin/diff-tree", "-r", $parent, $hash;
409 my (@difftree) = map { chomp; $_ } <$fd>;
413 print "<div class=\"main\">\n";
414 print "view " . $cgi->a({-href
=> "$myself?project=$project;action=commit;hash=$hash"}, "commit") . "<br/><br/><br/>\n";
415 print "$shortlog<br/>\n";
417 foreach my $line (@difftree) {
418 # '*100644->100644 blob 8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154 Makefile'
419 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
425 if ($type eq "blob") {
427 git_diff
("", $file, "", $id);
428 } elsif ($op eq "-") {
429 git_diff
($file, "", $id, "");
430 } elsif ($op eq "*") {
431 $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
432 git_diff
($file, $file, $1, $2);
441 print "<br/><br/><div class=\"main\">\n";
442 print "unknown action\n";