v006
[git.git] / gitweb.pl
blob8bd8127343bd503cc3de5cb75e84fa77d7142cfd
1 #!/usr/bin/perl
3 # gitweb.pl - simple web interface to track changes in git repositories
5 # Version 006
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;
18 my $gitbin = "/home/kay/bin/git";
19 my $gitroot = "/home/kay/public_html";
20 my $gittmp = "/tmp";
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]+)$#) {
31 $project = $1;
32 $action = "commit";
33 $hash = $2;
34 } elsif ($url_path =~ m#/([^/]+)/treediff/([0-9a-fA-F]+)$#) {
35 $project = $1;
36 $action = "treediff";
37 $hash = $2;
38 } elsif ($url_path =~ m#/([^/]+)/diff/([0-9a-fA-F]+)/([0-9a-fA-F]+)$#) {
39 $project = $1;
40 $action = "treediff";
41 $hash = $2;
42 $hash_parent = $3;
43 } elsif ($url_path =~ m#/([^/]+)/log/([0-9]+)$#) {
44 $project = $1;
45 $action = "log";
46 $view_back = $2;
47 } elsif ($url_path =~ m#/([^/]+)/log#) {
48 $project = $1;
49 $action = "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";
60 sub git_header {
61 print $cgi->header(-type => 'text/html; charset: utf-8');
62 print <<EOF;
63 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
64 <html>
65 <head>
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; }
93 a { color:#0000cc; }
94 a:hover { color:#880000; }
95 a:visited { color:#880000; }
96 a:active { color:#880000; }
97 </style>
98 </head>
99 <body>
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);
108 if ($action ne "") {
109 print " / $action";
111 print "</div>\n";
114 sub git_footer {
115 print "</div>";
116 print $cgi->end_html();
119 sub git_diff {
120 my $old_name = shift || "/dev/null";
121 my $new_name = shift || "/dev/null";
122 my $old = shift;
123 my $new = shift;
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
131 if ($old ne "") {
132 open my $fd2, "> $gittmp/$old";
133 open my $fd, "-|", "$gitbin/cat-file", "blob", $old;
134 while (my $line = <$fd>) {
135 print $fd2 $line;
137 close $fd2;
138 close $fd;
139 $tmp_old = "$gittmp/$old";
140 $old_label = "a/$old_name";
143 # create tmp to-file
144 if ($new ne "") {
145 open my $fd2, "> $gittmp/$new";
146 open my $fd, "-|", "$gitbin/cat-file", "blob", $new;
147 while (my $line = <$fd>) {
148 print $fd2 $line;
150 close $fd2;
151 close $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">===== ';
158 if ($old ne "") {
159 print $cgi->a({-href => "$myself?project=$project;action=blob;hash=$old"}, $old);
160 } else {
161 print $old_name;
163 print " vs ";
164 if ($new ne "") {
165 print $cgi->a({-href => "$myself?project=$project;action=blob;hash=$new"}, $new);
166 } else {
167 print $new_name;
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 '@';
178 close $fd;
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>;
186 close $fd;
187 git_header();
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";
194 print "<br/></div>";
195 git_footer();
196 exit;
199 if ($action eq "") {
200 print $cgi->redirect("$myself?project=$project;action=log;view_back=$view_back");
201 exit;
204 if ($action eq "blob") {
205 git_header();
206 print "<br/><br/><div class=\"main\">\n";
207 print "<pre>\n";
208 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
209 my $nr;
210 while (my $line = <$fd>) {
211 $nr++;
212 print "$nr\t" . escapeHTML($line);;
214 close $fd;
215 print "</pre>\n";
216 print "<br/></div>";
217 git_footer();
218 } elsif ($action eq "tree") {
219 if ($hash eq "") {
220 open my $fd, "$projectroot/.git/HEAD";
221 my $head = <$fd>;
222 chomp $head;
223 close $fd;
224 $hash = $head;
226 open my $fd, "-|", "$gitbin/ls-tree", $hash;
227 my (@entries) = map { chomp; $_ } <$fd>;
228 close $fd;
229 git_header();
230 print "<br/><br/><div class=\"main\">\n";
231 print "<pre>\n";
232 foreach my $line (@entries) {
233 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
234 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
235 my $t_type = $2;
236 my $t_hash = $3;
237 my $t_name = $4;
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";
244 print "</pre>\n";
245 print "<br/></div>";
246 git_footer();
247 } elsif ($action eq "log" || $action eq "show_log" ) {
248 open my $fd, "$projectroot/.git/HEAD";
249 my $head = <$fd>;
250 chomp $head;
251 close $fd;
252 open $fd, "-|", "$gitbin/rev-tree", $head;
253 my (@revtree) = map { chomp; $_ } <$fd>;
254 close $fd;
255 git_header();
256 print "<div class=\"head2\">\n";
257 print "view ";
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";
264 print "</div>\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]+)/);
269 my $time = $1;
270 my $commit = $2;
271 my $parent = $3;
272 my @parents;
273 my $author;
274 my $author_name;
275 my $author_time;
276 my $author_timezone;
277 my $committer;
278 my $committer_time;
279 my $committer_timezone;
280 my $tree;
281 my $comment;
282 my $shortlog;
283 open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
284 while (my $line = <$fd>) {
285 chomp($line);
286 last if $line eq "";
287 if ($line =~ m/^tree (.*)$/) {
288 $tree = $1;
289 } elsif ($line =~ m/^parent (.*)$/) {
290 push @parents, $1;
291 } elsif ($line =~ m/^committer (.*>) ([0-9]+) (.*)$/) {
292 $committer = $1;
293 $committer_time = $2;
294 $committer_timezone = $3;
295 } elsif ($line =~ m/^author (.*>) ([0-9]+) (.*)$/) {
296 $author = $1;
297 $author_time = $2;
298 $author_timezone = $3;
301 $shortlog = <$fd>;
302 $shortlog = escapeHTML($shortlog);
303 $comment = $shortlog . "<br/>";
304 while (my $line = <$fd>) {
305 chomp($line);
306 $comment .= escapeHTML($line) . "<br/>\n";
308 close $fd;
309 my $age = time-$committer_time;
310 last if ($view_back > 0 && $age > $view_back);
312 my $age_string;
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";
332 print "<tr>\n";
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>";
335 print "</tr>\n";
336 print "<tr>\n";
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";
341 print "</td>\n";
342 print "<td class=\"head2\">\n";
343 print "author &nbsp; &nbsp;" . escapeHTML($author) . " [" . gmtime($author_time) . " " . $author_timezone . "]<br/>\n";
344 print "committer " . escapeHTML($committer) . " [" . gmtime($committer_time) . " " . $committer_timezone . "]<br/>\n";
345 print "commit &nbsp; &nbsp;$commit<br/>\n";
346 print "tree &nbsp; &nbsp; &nbsp;$tree<br/>\n";
347 foreach my $par (@parents) {
348 print "parent &nbsp; &nbsp;$par<br/>\n";
350 print "</td>";
351 print "</tr>\n";
352 print "<tr>\n";
353 print "<td></td>\n";
354 print "<td>\n";
355 print "$comment<br/><br/>\n";
356 print "</td>";
357 print "</tr>\n";
359 print "</table>\n";
360 git_footer();
361 } elsif ($action eq "commit") {
362 my $parent = "";
363 open my $fd, "-|", "$gitbin/cat-file", "commit", $hash;
364 while (my $line = <$fd>) {
365 chomp($line);
366 last if $line eq "";
367 if ($line =~ m/^parent (.*)$/ && $parent eq "") {
368 $parent = $1;
371 my $shortlog = <$fd>;
372 $shortlog = escapeHTML($shortlog);
373 close $fd;
375 open $fd, "-|", "$gitbin/diff-tree", "-r", $parent, $hash;
376 my (@difftree) = map { chomp; $_ } <$fd>;
377 close $fd;
379 git_header();
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";
383 print "<pre>\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(.*)$/;
388 my $op = $1;
389 my $mode = $2;
390 my $type = $3;
391 my $id = $4;
392 my $file = $5;
393 if ($type eq "blob") {
394 if ($op eq "+") {
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]+)/;
400 my $old = $1;
401 my $new = $2;
402 print "CHANGED\t" . $cgi->a({-href => "$myself?project=$project;action=diff;hash=$old;hash_parent=$new"}, $file) . "\n";
406 print "</pre>\n";
407 print "<br/></div>";
408 git_footer();
409 } elsif ($action eq "diff") {
410 git_header();
411 print "<br/><br/><div class=\"main\">\n";
412 print "<pre>\n";
413 git_diff($hash, $hash_parent, $hash, $hash_parent);
414 print "</pre>\n";
415 print "<br/></div>";
416 git_footer();
417 } elsif ($action eq "treediff") {
418 my $parent = "";
419 open my $fd, "-|", "$gitbin/cat-file", "commit", $hash;
420 while (my $line = <$fd>) {
421 chomp($line);
422 last if $line eq "";
423 if ($line =~ m/^parent (.*)$/ && $parent eq "") {
424 $parent = $1;
427 my $shortlog = <$fd>;
428 $shortlog = escapeHTML($shortlog);
429 close $fd;
431 open $fd, "-|", "$gitbin/diff-tree", "-r", $parent, $hash;
432 my (@difftree) = map { chomp; $_ } <$fd>;
433 close $fd;
435 git_header();
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";
439 print "<pre>\n";
440 foreach my $line (@difftree) {
441 # '*100644->100644 blob 8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154 Makefile'
442 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
443 my $op = $1;
444 my $mode = $2;
445 my $type = $3;
446 my $id = $4;
447 my $file = $5;
448 if ($type eq "blob") {
449 if ($op eq "+") {
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);
459 print "</pre>\n";
460 print "<br/></div>";
461 git_footer();
462 } else {
463 git_header();
464 print "<br/><br/><div class=\"main\">\n";
465 print "unknown action\n";
466 print "<br/></div>";
467 git_footer();