v005
[git.git] / gitweb.pl
blob25bd65e5f27f4c058546e16ba13f71cc785a252b
1 #!/usr/bin/perl
3 # gitweb.pl - simple web interface to track changes in git repositories
5 # Version 005
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);
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";
35 sub git_header {
36 print $cgi->header(-type => 'text/html; charset: utf-8');
37 print <<EOF;
38 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
39 <html>
40 <head>
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; }
68 a { color:#0000cc; }
69 a:hover { color:#880000; }
70 a:visited { color:#880000; }
71 a:active { color:#880000; }
72 </style>
73 </head>
74 <body>
75 EOF
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");
80 if ($project ne "") {
81 print " / " . $cgi->a({-href => "$myself?project=$project;action=log;view_back=" . 60*60*24}, $project);
83 if ($action ne "") {
84 print " / $action";
86 print "</div>\n";
89 sub git_footer {
90 print "</div>";
91 print $cgi->end_html();
94 sub git_diff {
95 my $old_name = shift || "/dev/null";
96 my $new_name = shift || "/dev/null";
97 my $old = shift;
98 my $new = shift;
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
106 if ($old ne "") {
107 open my $fd2, "> $gittmp/$old";
108 open my $fd, "-|", "$gitbin/cat-file", "blob", $old;
109 while (my $line = <$fd>) {
110 print $fd2 $line;
112 close $fd2;
113 close $fd;
114 $tmp_old = "$gittmp/$old";
115 $old_label = "a/$old_name";
118 # create tmp to-file
119 if ($new ne "") {
120 open my $fd2, "> $gittmp/$new";
121 open my $fd, "-|", "$gitbin/cat-file", "blob", $new;
122 while (my $line = <$fd>) {
123 print $fd2 $line;
125 close $fd2;
126 close $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">===== ';
133 if ($old ne "") {
134 print $cgi->a({-href => "$myself?project=$project;action=blob;hash=$old"}, $old);
135 } else {
136 print $old_name;
138 print " vs ";
139 if ($new ne "") {
140 print $cgi->a({-href => "$myself?project=$project;action=blob;hash=$new"}, $new);
141 } else {
142 print $new_name;
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 '@';
153 close $fd;
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>;
161 close $fd;
162 git_header();
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";
169 print "<br/></div>";
170 git_footer();
171 exit;
174 if ($action eq "") {
175 print $cgi->redirect("$myself?project=$project;action=log;view_back=$view_back");
176 exit;
179 if ($action eq "blob") {
180 git_header();
181 print "<br/><br/><div class=\"main\">\n";
182 print "<pre>\n";
183 open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
184 my $nr;
185 while (my $line = <$fd>) {
186 $nr++;
187 print "$nr\t" . escapeHTML($line);;
189 close $fd;
190 print "</pre>\n";
191 print "<br/></div>";
192 git_footer();
193 } elsif ($action eq "tree") {
194 if ($hash eq "") {
195 open my $fd, "$projectroot/.git/HEAD";
196 my $head = <$fd>;
197 chomp $head;
198 close $fd;
199 $hash = $head;
201 open my $fd, "-|", "$gitbin/ls-tree", $hash;
202 my (@entries) = map { chomp; $_ } <$fd>;
203 close $fd;
204 git_header();
205 print "<br/><br/><div class=\"main\">\n";
206 print "<pre>\n";
207 foreach my $line (@entries) {
208 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
209 $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
210 my $t_type = $2;
211 my $t_hash = $3;
212 my $t_name = $4;
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";
219 print "</pre>\n";
220 print "<br/></div>";
221 git_footer();
222 } elsif ($action eq "log" || $action eq "show_log" ) {
223 open my $fd, "$projectroot/.git/HEAD";
224 my $head = <$fd>;
225 chomp $head;
226 close $fd;
227 open $fd, "-|", "$gitbin/rev-tree", $head;
228 my (@revtree) = map { chomp; $_ } <$fd>;
229 close $fd;
230 git_header();
231 print "<div class=\"head2\">\n";
232 print "view ";
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";
239 print "</div>\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]+)/);
244 my $time = $1;
245 my $commit = $2;
246 my $parent = $3;
247 my @parents;
248 my $author;
249 my $author_name;
250 my $author_time;
251 my $author_timezone;
252 my $committer;
253 my $committer_time;
254 my $committer_timezone;
255 my $tree;
256 my $comment;
257 my $shortlog;
258 open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
259 while (my $line = <$fd>) {
260 chomp($line);
261 last if $line eq "";
262 if ($line =~ m/^tree (.*)$/) {
263 $tree = $1;
264 } elsif ($line =~ m/^parent (.*)$/) {
265 push @parents, $1;
266 } elsif ($line =~ m/^committer (.*>) ([0-9]+) (.*)$/) {
267 $committer = $1;
268 $committer_time = $2;
269 $committer_timezone = $3;
270 } elsif ($line =~ m/^author (.*>) ([0-9]+) (.*)$/) {
271 $author = $1;
272 $author_time = $2;
273 $author_timezone = $3;
274 $author =~ m/^(.*) </;
275 $author_name = $1;
278 $shortlog = <$fd>;
279 $shortlog = escapeHTML($shortlog);
280 $comment = $shortlog . "<br/>";
281 while (my $line = <$fd>) {
282 chomp($line);
283 $comment .= escapeHTML($line) . "<br/>\n";
285 close $fd;
286 my $age = time-$committer_time;
287 last if ($view_back > 0 && $age > $view_back);
289 my $age_string;
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";
309 print "<tr>\n";
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>";
312 print "</tr>\n";
313 print "<tr>\n";
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";
318 print "</td>\n";
319 print "<td class=\"head2\">\n";
320 print "author &nbsp; &nbsp;" . escapeHTML($author) . " [" . gmtime($author_time) . " " . $author_timezone . "]<br/>\n";
321 print "committer " . escapeHTML($committer) . " [" . gmtime($committer_time) . " " . $committer_timezone . "]<br/>\n";
322 print "commit &nbsp; &nbsp;$commit<br/>\n";
323 print "tree &nbsp; &nbsp; &nbsp;$tree<br/>\n";
324 foreach my $par (@parents) {
325 print "parent &nbsp; &nbsp;$par<br/>\n";
327 print "</td>";
328 print "</tr>\n";
329 print "<tr>\n";
330 print "<td></td>\n";
331 print "<td>\n";
332 print "$comment<br/><br/>\n";
333 print "</td>";
334 print "</tr>\n";
336 print "</table>\n";
337 git_footer();
338 } elsif ($action eq "commit") {
339 my $parent = "";
340 open my $fd, "-|", "$gitbin/cat-file", "commit", $hash;
341 while (my $line = <$fd>) {
342 chomp($line);
343 last if $line eq "";
344 if ($line =~ m/^parent (.*)$/ && $parent eq "") {
345 $parent = $1;
348 my $shortlog = <$fd>;
349 $shortlog = escapeHTML($shortlog);
350 close $fd;
352 open $fd, "-|", "$gitbin/diff-tree", "-r", $parent, $hash;
353 my (@difftree) = map { chomp; $_ } <$fd>;
354 close $fd;
356 git_header();
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";
360 print "<pre>\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(.*)$/;
365 my $op = $1;
366 my $mode = $2;
367 my $type = $3;
368 my $id = $4;
369 my $file = $5;
370 if ($type eq "blob") {
371 if ($op eq "+") {
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]+)/;
377 my $old = $1;
378 my $new = $2;
379 print "CHANGED\t" . $cgi->a({-href => "$myself?project=$project;action=diff;hash=$old;hash_parent=$new"}, $file) . "\n";
383 print "</pre>\n";
384 print "<br/></div>";
385 git_footer();
386 } elsif ($action eq "diff") {
387 git_header();
388 print "<br/><br/><div class=\"main\">\n";
389 print "<pre>\n";
390 git_diff($hash, $hash_parent, $hash, $hash_parent);
391 print "</pre>\n";
392 print "<br/></div>";
393 git_footer();
394 } elsif ($action eq "treediff") {
395 my $parent = "";
396 open my $fd, "-|", "$gitbin/cat-file", "commit", $hash;
397 while (my $line = <$fd>) {
398 chomp($line);
399 last if $line eq "";
400 if ($line =~ m/^parent (.*)$/ && $parent eq "") {
401 $parent = $1;
404 my $shortlog = <$fd>;
405 $shortlog = escapeHTML($shortlog);
406 close $fd;
408 open $fd, "-|", "$gitbin/diff-tree", "-r", $parent, $hash;
409 my (@difftree) = map { chomp; $_ } <$fd>;
410 close $fd;
412 git_header();
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";
416 print "<pre>\n";
417 foreach my $line (@difftree) {
418 # '*100644->100644 blob 8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154 Makefile'
419 $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
420 my $op = $1;
421 my $mode = $2;
422 my $type = $3;
423 my $id = $4;
424 my $file = $5;
425 if ($type eq "blob") {
426 if ($op eq "+") {
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);
436 print "</pre>\n";
437 print "<br/></div>";
438 git_footer();
439 } else {
440 git_header();
441 print "<br/><br/><div class=\"main\">\n";
442 print "unknown action\n";
443 print "<br/></div>";
444 git_footer();