v259
[git.git] / gitweb.cgi
1 #!/usr/bin/perl
2
3 # gitweb - simple web interface to track changes in git repositories
4 #
5 # (C) 2005, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke <ch@gierke.de>
7 #
8 # This program is licensed under the GPLv2
9
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 binmode STDOUT, ':utf8';
18
19 my $cgi = new CGI;
20 my $version =           "259";
21 my $my_url =            $cgi->url();
22 my $my_uri =            $cgi->url(-absolute => 1);
23 my $rss_link =          "";
24
25 # absolute fs-path which will be prepended to the project path
26 #my $projectroot =      "/pub/scm";
27 my $projectroot =       "/home/kay/public_html/pub/scm";
28
29 # location of the git-core binaries
30 my $gitbin =            "/usr/bin";
31
32 # location for temporary files needed for diffs
33 my $git_temp =          "/tmp/gitweb";
34
35 # target of the home link on top of all pages
36 my $home_link =         $my_uri;
37
38 # html text to include at home page
39 my $home_text =         "indextext.html";
40
41 # source of projects list
42 #my $projects_list =    $projectroot;
43 my $projects_list =     "index/index.aux";
44
45 # input validation and dispatch
46 my $action = $cgi->param('a');
47 if (defined $action) {
48         if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
49                 undef $action;
50                 die_error(undef, "Invalid action parameter.");
51         }
52         if ($action eq "git-logo.png") {
53                 git_logo();
54                 exit;
55         } elsif ($action eq "opml") {
56                 git_opml();
57                 exit;
58         }
59 }
60
61 my $order = $cgi->param('o');
62 if (defined $order) {
63         if ($order =~ m/[^0-9a-zA-Z_]/) {
64                 undef $order;
65                 die_error(undef, "Invalid order parameter.");
66         }
67 }
68
69 my $project = $cgi->param('p');
70 if (defined $project) {
71         $project = validate_input($project);
72         if (!defined($project)) {
73                 die_error(undef, "Invalid project parameter.");
74         }
75         if (!(-d "$projectroot/$project")) {
76                 undef $project;
77                 die_error(undef, "No such directory.");
78         }
79         if (!(-e "$projectroot/$project/HEAD")) {
80                 undef $project;
81                 die_error(undef, "No such project.");
82         }
83         $rss_link = "<link rel=\"alternate\" title=\"" . esc_param($project) . " log\" href=\"" .
84                     "$my_uri?" . esc_param("p=$project;a=rss") . "\" type=\"application/rss+xml\"/>";
85         $ENV{'GIT_DIR'} = "$projectroot/$project";
86 } else {
87         git_project_list();
88         exit;
89 }
90
91 my $file_name = $cgi->param('f');
92 if (defined $file_name) {
93         $file_name = validate_input($file_name);
94         if (!defined($file_name)) {
95                 die_error(undef, "Invalid file parameter.");
96         }
97 }
98
99 my $hash = $cgi->param('h');
100 if (defined $hash) {
101         $hash = validate_input($hash);
102         if (!defined($hash)) {
103                 die_error(undef, "Invalid hash parameter.");
104         }
105 }
106
107 my $hash_parent = $cgi->param('hp');
108 if (defined $hash_parent) {
109         $hash_parent = validate_input($hash_parent);
110         if (!defined($hash_parent)) {
111                 die_error(undef, "Invalid hash parent parameter.");
112         }
113 }
114
115 my $hash_base = $cgi->param('hb');
116 if (defined $hash_base) {
117         $hash_base = validate_input($hash_base);
118         if (!defined($hash_base)) {
119                 die_error(undef, "Invalid hash base parameter.");
120         }
121 }
122
123 my $page = $cgi->param('pg');
124 if (defined $page) {
125         if ($page =~ m/[^0-9]$/) {
126                 undef $page;
127                 die_error(undef, "Invalid page parameter.");
128         }
129 }
130
131 my $searchtext = $cgi->param('s');
132 if (defined $searchtext) {
133         if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
134                 undef $searchtext;
135                 die_error(undef, "Invalid search parameter.");
136         }
137         $searchtext = quotemeta $searchtext;
138 }
139
140 sub validate_input {
141         my $input = shift;
142
143         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
144                 return $input;
145         }
146         if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
147                 return undef;
148         }
149         if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
150                 return undef;
151         }
152         return $input;
153 }
154
155 if (!defined $action || $action eq "summary") {
156         git_summary();
157         exit;
158 } elsif ($action eq "heads") {
159         git_heads();
160         exit;
161 } elsif ($action eq "tags") {
162         git_tags();
163         exit;
164 } elsif ($action eq "blob") {
165         git_blob();
166         exit;
167 } elsif ($action eq "blob_plain") {
168         git_blob_plain();
169         exit;
170 } elsif ($action eq "tree") {
171         git_tree();
172         exit;
173 } elsif ($action eq "rss") {
174         git_rss();
175         exit;
176 } elsif ($action eq "commit") {
177         git_commit();
178         exit;
179 } elsif ($action eq "log") {
180         git_log();
181         exit;
182 } elsif ($action eq "blobdiff") {
183         git_blobdiff();
184         exit;
185 } elsif ($action eq "blobdiff_plain") {
186         git_blobdiff_plain();
187         exit;
188 } elsif ($action eq "commitdiff") {
189         git_commitdiff();
190         exit;
191 } elsif ($action eq "commitdiff_plain") {
192         git_commitdiff_plain();
193         exit;
194 } elsif ($action eq "history") {
195         git_history();
196         exit;
197 } elsif ($action eq "search") {
198         git_search();
199         exit;
200 } elsif ($action eq "shortlog") {
201         git_shortlog();
202         exit;
203 } elsif ($action eq "tag") {
204         git_tag();
205         exit;
206 } else {
207         undef $action;
208         die_error(undef, "Unknown action.");
209         exit;
210 }
211
212 # quote unsafe chars, but keep the slash, even when it's not
213 # correct, but quoted slashes look too horrible in bookmarks
214 sub esc_param {
215         my $str = shift;
216         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
217         $str =~ s/\+/%2B/g;
218         $str =~ s/ /\+/g;
219         return $str;
220 }
221
222 # replace invalid utf8 character with SUBSTITUTION sequence
223 sub esc_html {
224         my $str = shift;
225         $str = decode("utf8", $str, Encode::FB_DEFAULT);
226         $str = escapeHTML($str);
227         return $str;
228 }
229
230 # git may return quoted and escaped filenames
231 sub unquote {
232         my $str = shift;
233         if ($str =~ m/^"(.*)"$/) {
234                 $str = $1;
235                 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
236         }
237         return $str;
238 }
239
240 sub git_header_html {
241         my $status = shift || "200 OK";
242         my $expires = shift;
243
244         my $title = "git";
245         if (defined $project) {
246                 $title .= " - $project";
247                 if (defined $action) {
248                         $title .= "/$action";
249                 }
250         }
251         print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status, -expires => $expires);
252         print <<EOF;
253 <?xml version="1.0" encoding="utf-8"?>
254 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
255 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
256 <!-- git web interface v$version, (C) 2005, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke <ch\@gierke.de> -->
257 <head>
258 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
259 <meta name="robots" content="index, nofollow"/>
260 <title>$title</title>
261 $rss_link
262 <style type="text/css">
263 body { font-family: sans-serif; font-size: 12px; margin:0px; border:solid #d9d8d1; border-width:1px; margin:10px; }
264 a { color:#0000cc; }
265 a:hover, a:visited, a:active { color:#880000; }
266 div.page_header { height:25px; padding:8px; font-size:18px; font-weight:bold; background-color:#d9d8d1; }
267 div.page_header a:visited, a.header { color:#0000cc; }
268 div.page_header a:hover { color:#880000; }
269 div.page_nav { padding:8px; }
270 div.page_nav a:visited { color:#0000cc; }
271 div.page_path { padding:8px; border:solid #d9d8d1; border-width:0px 0px 1px}
272 div.page_footer { height:17px; padding:4px 8px; background-color: #d9d8d1; }
273 div.page_footer_text { float:left; color:#555555; font-style:italic; }
274 div.page_body { padding:8px; }
275 div.title, a.title {
276         display:block; padding:6px 8px;
277         font-weight:bold; background-color:#edece6; text-decoration:none; color:#000000;
278 }
279 a.title:hover { background-color: #d9d8d1; }
280 div.title_text { padding:6px 0px; border: solid #d9d8d1; border-width:0px 0px 1px; }
281 div.log_body { padding:8px 8px 8px 150px; }
282 span.age { position:relative; float:left; width:142px; font-style:italic; }
283 div.log_link {
284         padding:0px 8px;
285         font-size:10px; font-family:sans-serif; font-style:normal;
286         position:relative; float:left; width:136px;
287 }
288 div.list_head { padding:6px 8px 4px; border:solid #d9d8d1; border-width:1px 0px 0px; font-style:italic; }
289 a.list { text-decoration:none; color:#000000; }
290 a.list:hover { text-decoration:underline; color:#880000; }
291 a.text { text-decoration:none; color:#0000cc; }
292 a.text:visited { text-decoration:none; color:#880000; }
293 a.text:hover { text-decoration:underline; color:#880000; }
294 table { padding:8px 4px; }
295 th { padding:2px 5px; font-size:12px; text-align:left; }
296 tr.light:hover { background-color:#edece6; }
297 tr.dark { background-color:#f6f6f0; }
298 tr.dark:hover { background-color:#edece6; }
299 td { padding:2px 5px; font-size:12px; vertical-align:top; }
300 td.link { padding:2px 5px; font-family:sans-serif; font-size:10px; }
301 div.pre { font-family:monospace; font-size:12px; white-space:pre; }
302 div.diff_info { font-family:monospace; color:#000099; background-color:#edece6; font-style:italic; }
303 div.index_include { border:solid #d9d8d1; border-width:0px 0px 1px; padding:12px 8px; }
304 div.search { margin:4px 8px; position:absolute; top:56px; right:12px }
305 a.linenr { color:#999999; text-decoration:none }
306 a.rss_logo {
307         float:right; padding:3px 0px; width:35px; line-height:10px;
308         border:1px solid; border-color:#fcc7a5 #7d3302 #3e1a01 #ff954e;
309         color:#ffffff; background-color:#ff6600;
310         font-weight:bold; font-family:sans-serif; font-size:10px;
311         text-align:center; text-decoration:none;
312 }
313 a.rss_logo:hover { background-color:#ee5500; }
314 span.tag {
315         padding:0px 4px; font-size:10px;
316         background-color:#ffffaa; border:1px solid; border-color:#ffffcc #ffee00 #ffee00 #ffffcc;
317 }
318 </style>
319 </head>
320 <body>
321 EOF
322         print "<div class=\"page_header\">\n" .
323               "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
324               "<img src=\"$my_uri?" . esc_param("a=git-logo.png") . "\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
325               "</a>\n";
326         print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
327         if (defined $project) {
328                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
329                 if (defined $action) {
330                         print " / $action";
331                 }
332                 print "\n";
333                 if (!defined $searchtext) {
334                         $searchtext = "";
335                 }
336                 my $search_hash;
337                 if (defined $hash) {
338                         $search_hash = $hash;
339                 } else {
340                         $search_hash  = "HEAD";
341                 }
342                 $cgi->param("a", "search");
343                 $cgi->param("h", $search_hash);
344                 print $cgi->startform(-method => "get", -action => $my_uri) .
345                       "<div class=\"search\">\n" .
346                       $cgi->hidden(-name => "p") . "\n" .
347                       $cgi->hidden(-name => "a") . "\n" .
348                       $cgi->hidden(-name => "h") . "\n" .
349                       $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
350                       "</div>" .
351                       $cgi->end_form() . "\n";
352         }
353         print "</div>\n";
354 }
355
356 sub git_footer_html {
357         print "<div class=\"page_footer\">\n";
358         if (defined $project) {
359                 my $descr = git_read_description($project);
360                 if (defined $descr) {
361                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
362                 }
363                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
364         } else {
365                 print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
366         }
367         print "</div>\n" .
368               "</body>\n" .
369               "</html>";
370 }
371
372 sub die_error {
373         my $status = shift || "403 Forbidden";
374         my $error = shift || "Malformed query, file missing or permission denied"; 
375
376         git_header_html($status);
377         print "<div class=\"page_body\">\n" .
378               "<br/><br/>\n" .
379               "$status - $error\n" .
380               "<br/>\n" .
381               "</div>\n";
382         git_footer_html();
383         exit;
384 }
385
386 sub git_get_type {
387         my $hash = shift;
388
389         open my $fd, "-|", "$gitbin/git-cat-file -t $hash" or return;
390         my $type = <$fd>;
391         close $fd or return;
392         chomp $type;
393         return $type;
394 }
395
396 sub git_read_hash {
397         my $path = shift;
398
399         open my $fd, "$projectroot/$path" or return undef;
400         my $head = <$fd>;
401         close $fd;
402         chomp $head;
403         if ($head =~ m/^[0-9a-fA-F]{40}$/) {
404                 return $head;
405         }
406 }
407
408 sub git_read_description {
409         my $path = shift;
410
411         open my $fd, "$projectroot/$path/description" or return undef;
412         my $descr = <$fd>;
413         close $fd;
414         chomp $descr;
415         return $descr;
416 }
417
418 sub git_read_tag {
419         my $tag_id = shift;
420         my %tag;
421         my @comment;
422
423         open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" or return;
424         $tag{'id'} = $tag_id;
425         while (my $line = <$fd>) {
426                 chomp $line;
427                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
428                         $tag{'object'} = $1;
429                 } elsif ($line =~ m/^type (.+)$/) {
430                         $tag{'type'} = $1;
431                 } elsif ($line =~ m/^tag (.+)$/) {
432                         $tag{'name'} = $1;
433                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
434                         $tag{'author'} = $1;
435                         $tag{'epoch'} = $2;
436                         $tag{'tz'} = $3;
437                 } elsif ($line =~ m/--BEGIN/) {
438                         push @comment, $line;
439                         last;
440                 } elsif ($line eq "") {
441                         last;
442                 }
443         }
444         push @comment, <$fd>;
445         $tag{'comment'} = \@comment;
446         close $fd or return;
447         if (!defined $tag{'name'}) {
448                 return
449         };
450         return %tag
451 }
452
453 sub age_string {
454         my $age = shift;
455         my $age_str;
456
457         if ($age > 60*60*24*365*2) {
458                 $age_str = (int $age/60/60/24/365);
459                 $age_str .= " years ago";
460         } elsif ($age > 60*60*24*(365/12)*2) {
461                 $age_str = int $age/60/60/24/(365/12);
462                 $age_str .= " months ago";
463         } elsif ($age > 60*60*24*7*2) {
464                 $age_str = int $age/60/60/24/7;
465                 $age_str .= " weeks ago";
466         } elsif ($age > 60*60*24*2) {
467                 $age_str = int $age/60/60/24;
468                 $age_str .= " days ago";
469         } elsif ($age > 60*60*2) {
470                 $age_str = int $age/60/60;
471                 $age_str .= " hours ago";
472         } elsif ($age > 60*2) {
473                 $age_str = int $age/60;
474                 $age_str .= " min ago";
475         } elsif ($age > 2) {
476                 $age_str = int $age;
477                 $age_str .= " sec ago";
478         } else {
479                 $age_str .= " right now";
480         }
481         return $age_str;
482 }
483
484 sub git_read_commit {
485         my $commit_id = shift;
486         my $commit_text = shift;
487
488         my @commit_lines;
489         my %co;
490
491         if (defined $commit_text) {
492                 @commit_lines = @$commit_text;
493         } else {
494                 $/ = "\0";
495                 open my $fd, "-|", "$gitbin/git-rev-list --header --parents --max-count=1 $commit_id" or return;
496                 @commit_lines = split '\n', <$fd>;
497                 close $fd or return;
498                 $/ = "\n";
499                 pop @commit_lines;
500         }
501         my $header = shift @commit_lines;
502         if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
503                 return;
504         }
505         ($co{'id'}, my @parents) = split ' ', $header;
506         $co{'parents'} = \@parents;
507         $co{'parent'} = $parents[0];
508         while (my $line = shift @commit_lines) {
509                 last if $line eq "\n";
510                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
511                         $co{'tree'} = $1;
512                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
513                         $co{'author'} = $1;
514                         $co{'author_epoch'} = $2;
515                         $co{'author_tz'} = $3;
516                         if ($co{'author'} =~ m/^([^<]+) </) {
517                                 $co{'author_name'} = $1;
518                         } else {
519                                 $co{'author_name'} = $co{'author'};
520                         }
521                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
522                         $co{'committer'} = $1;
523                         $co{'committer_epoch'} = $2;
524                         $co{'committer_tz'} = $3;
525                         $co{'committer_name'} = $co{'committer'};
526                         $co{'committer_name'} =~ s/ <.*//;
527                 }
528         }
529         if (!defined $co{'tree'}) {
530                 return;
531         };
532
533         foreach my $title (@commit_lines) {
534                 if ($title ne "") {
535                         $co{'title'} = chop_str($title, 80, 5);
536                         # remove leading stuff of merges to make the interesting part visible
537                         if (length($title) > 50) {
538                                 $title =~ s/^Automatic //;
539                                 $title =~ s/^merge (of|with) /Merge ... /i;
540                                 if (length($title) > 50) {
541                                         $title =~ s/(http|rsync):\/\///;
542                                 }
543                                 if (length($title) > 50) {
544                                         $title =~ s/(master|www|rsync)\.//;
545                                 }
546                                 if (length($title) > 50) {
547                                         $title =~ s/kernel.org:?//;
548                                 }
549                                 if (length($title) > 50) {
550                                         $title =~ s/\/pub\/scm//;
551                                 }
552                         }
553                         $co{'title_short'} = chop_str($title, 50, 5);
554                         last;
555                 }
556         }
557         # remove added spaces
558         foreach my $line (@commit_lines) {
559                 $line =~ s/^    //;
560         }
561         $co{'comment'} = \@commit_lines;
562
563         my $age = time - $co{'committer_epoch'};
564         $co{'age'} = $age;
565         $co{'age_string'} = age_string($age);
566         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
567         if ($age > 60*60*24*7*2) {
568                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
569                 $co{'age_string_age'} = $co{'age_string'};
570         } else {
571                 $co{'age_string_date'} = $co{'age_string'};
572                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
573         }
574         return %co;
575 }
576
577 sub git_diff_print {
578         my $from = shift;
579         my $from_name = shift;
580         my $to = shift;
581         my $to_name = shift;
582         my $format = shift || "html";
583
584         my $from_tmp = "/dev/null";
585         my $to_tmp = "/dev/null";
586         my $pid = $$;
587
588         # create tmp from-file
589         if (defined $from) {
590                 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
591                 open my $fd2, "> $from_tmp";
592                 open my $fd, "-|", "$gitbin/git-cat-file blob $from";
593                 my @file = <$fd>;
594                 print $fd2 @file;
595                 close $fd2;
596                 close $fd;
597         }
598
599         # create tmp to-file
600         if (defined $to) {
601                 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
602                 open my $fd2, "> $to_tmp";
603                 open my $fd, "-|", "$gitbin/git-cat-file blob $to";
604                 my @file = <$fd>;
605                 print $fd2 @file;
606                 close $fd2;
607                 close $fd;
608         }
609
610         open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
611         if ($format eq "plain") {
612                 undef $/;
613                 print <$fd>;
614                 $/ = "\n";
615         } else {
616                 while (my $line = <$fd>) {
617                         chomp($line);
618                         my $char = substr($line, 0, 1);
619                         my $color = "";
620                         if ($char eq '+') {
621                                 $color = " style=\"color:#008800;\"";
622                         } elsif ($char eq "-") {
623                                 $color = " style=\"color:#cc0000;\"";
624                         } elsif ($char eq "@") {
625                                 $color = " style=\"color:#990099;\"";
626                         } elsif ($char eq "\\") {
627                                 # skip errors
628                                 next;
629                         }
630                         while ((my $pos = index($line, "\t")) != -1) {
631                                 if (my $count = (8 - (($pos-1) % 8))) {
632                                         my $spaces = ' ' x $count;
633                                         $line =~ s/\t/$spaces/;
634                                 }
635                         }
636                         print "<div class=\"pre\"$color>" . esc_html($line) . "</div>\n";
637                 }
638         }
639         close $fd;
640
641         if (defined $from) {
642                 unlink($from_tmp);
643         }
644         if (defined $to) {
645                 unlink($to_tmp);
646         }
647 }
648
649 sub mode_str {
650         my $mode = oct shift;
651
652         if (S_ISDIR($mode & S_IFMT)) {
653                 return 'drwxr-xr-x';
654         } elsif (S_ISLNK($mode)) {
655                 return 'lrwxrwxrwx';
656         } elsif (S_ISREG($mode)) {
657                 # git cares only about the executable bit
658                 if ($mode & S_IXUSR) {
659                         return '-rwxr-xr-x';
660                 } else {
661                         return '-rw-r--r--';
662                 };
663         } else {
664                 return '----------';
665         }
666 }
667
668 sub chop_str {
669         my $str = shift;
670         my $len = shift;
671         my $add_len = shift || 10;
672
673         # allow only $len chars, but don't cut a word if it would fit in $add_len
674         # if it doesn't fit, cut it if it's still longer than the dots we would add
675         $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
676         my $body = $1;
677         my $tail = $2;
678         if (length($tail) > 4) {
679                 $tail = " ...";
680         }
681         return "$body$tail";
682 }
683
684 sub file_type {
685         my $mode = oct shift;
686
687         if (S_ISDIR($mode & S_IFMT)) {
688                 return "directory";
689         } elsif (S_ISLNK($mode)) {
690                 return "symlink";
691         } elsif (S_ISREG($mode)) {
692                 return "file";
693         } else {
694                 return "unknown";
695         }
696 }
697
698 sub format_log_line_html {
699         my $line = shift;
700
701         $line = esc_html($line);
702         $line =~ s/ /&nbsp;/g;
703         if ($line =~ m/([0-9a-fA-F]{40})/) {
704                 my $hash_text = $1;
705                 if (git_get_type($hash_text) eq "commit") {
706                         my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
707                         $line =~ s/$hash_text/$link/;
708                 }
709         }
710         return $line;
711 }
712
713 sub date_str {
714         my $epoch = shift;
715         my $tz = shift || "-0000";
716
717         my %date;
718         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
719         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
720         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
721         $date{'hour'} = $hour;
722         $date{'minute'} = $min;
723         $date{'mday'} = $mday;
724         $date{'day'} = $days[$wday];
725         $date{'month'} = $months[$mon];
726         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
727         $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
728
729         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
730         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
731         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
732         $date{'hour_local'} = $hour;
733         $date{'minute_local'} = $min;
734         $date{'tz_local'} = $tz;
735         return %date;
736 }
737
738 # git-logo (cached in browser for one day)
739 sub git_logo {
740         binmode STDOUT, ':raw';
741         print $cgi->header(-type => 'image/png', -expires => '+1d');
742         # cat git-logo.png | hexdump -e '16/1 " %02x"  "\n"' | sed 's/ /\\x/g'
743         print   "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
744                 "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
745                 "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
746                 "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
747                 "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
748                 "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
749                 "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
750                 "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
751                 "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
752                 "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
753                 "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
754                 "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
755                 "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
756 }
757
758 sub get_file_owner {
759         my $path = shift;
760
761         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
762         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
763         if (!defined $gcos) {
764                 return undef;
765         }
766         my $owner = $gcos;
767         $owner =~ s/[,;].*$//;
768         return $owner;
769 }
770
771 sub git_read_projects {
772         my @list;
773
774         if (-d $projects_list) {
775                 # search in directory
776                 my $dir = $projects_list;
777                 opendir my $dh, $dir or return undef;
778                 while (my $dir = readdir($dh)) {
779                         if (-e "$projectroot/$dir/HEAD") {
780                                 my $pr = {
781                                         path => $dir,
782                                 };
783                                 push @list, $pr
784                         }
785                 }
786                 closedir($dh);
787         } elsif (-f $projects_list) {
788                 # read from file(url-encoded):
789                 # 'git%2Fgit.git Linus+Torvalds'
790                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
791                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
792                 open my $fd , $projects_list or return undef;
793                 while (my $line = <$fd>) {
794                         chomp $line;
795                         my ($path, $owner) = split ' ', $line;
796                         $path = unescape($path);
797                         $owner = unescape($owner);
798                         if (!defined $path) {
799                                 next;
800                         }
801                         if (-e "$projectroot/$path/HEAD") {
802                                 my $pr = {
803                                         path => $path,
804                                         owner => $owner,
805                                 };
806                                 push @list, $pr
807                         }
808                 }
809                 close $fd;
810         }
811         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
812         return @list;
813 }
814
815 sub git_project_list {
816         my @list = git_read_projects();
817         my @projects;
818         if (!@list) {
819                 die_error(undef, "No project found.");
820         }
821         foreach my $pr (@list) {
822                 my $head = git_read_hash("$pr->{'path'}/HEAD");
823                 if (!defined $head) {
824                         next;
825                 }
826                 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
827                 my %co = git_read_commit($head);
828                 if (!%co) {
829                         next;
830                 }
831                 $pr->{'commit'} = \%co;
832                 if (!defined $pr->{'descr'}) {
833                         my $descr = git_read_description($pr->{'path'}) || "";
834                         $pr->{'descr'} = chop_str($descr, 25, 5);
835                 }
836                 if (!defined $pr->{'owner'}) {
837                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
838                 }
839                 push @projects, $pr;
840         }
841         git_header_html();
842         if (-f $home_text) {
843                 print "<div class=\"index_include\">\n";
844                 open (my $fd, $home_text);
845                 print <$fd>;
846                 close $fd;
847                 print "</div>\n";
848         }
849         print "<table cellspacing=\"0\">\n" .
850               "<tr>\n";
851         if (!defined($order) || (defined($order) && ($order eq "project"))) {
852                 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
853                 print "<th>Project</th>\n";
854         } else {
855                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=project")}, "Project") . "</th>\n";
856         }
857         if (defined($order) && ($order eq "descr")) {
858                 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
859                 print "<th>Description</th>\n";
860         } else {
861                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=descr")}, "Description") . "</th>\n";
862         }
863         if (defined($order) && ($order eq "owner")) {
864                 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
865                 print "<th>Owner</th>\n";
866         } else {
867                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=owner")}, "Owner") . "</th>\n";
868         }
869         if (defined($order) && ($order eq "age")) {
870                 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
871                 print "<th>Last Change</th>\n";
872         } else {
873                 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=age")}, "Last Change") . "</th>\n";
874         }
875         print "<th></th>\n" .
876               "</tr>\n";
877         my $alternate = 0;
878         foreach my $pr (@projects) {
879                 if ($alternate) {
880                         print "<tr class=\"dark\">\n";
881                 } else {
882                         print "<tr class=\"light\">\n";
883                 }
884                 $alternate ^= 1;
885                 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"), -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
886                       "<td>$pr->{'descr'}</td>\n" .
887                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
888                 my $colored_age;
889                 if ($pr->{'commit'}{'age'} < 60*60*2) {
890                         $colored_age = "<span style =\"color: #009900;\"><b><i>$pr->{'commit'}{'age_string'}</i></b></span>";
891                 } elsif ($pr->{'commit'}{'age'} < 60*60*24*2) {
892                         $colored_age = "<span style =\"color: #009900;\"><i>$pr->{'commit'}{'age_string'}</i></span>";
893                 } else {
894                         $colored_age = "<i>$pr->{'commit'}{'age_string'}</i>";
895                 }
896                 print "<td>$colored_age</td>\n" .
897                       "<td class=\"link\">" .
898                       $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") .
899                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") .
900                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
901                       "</td>\n" .
902                       "</tr>\n";
903         }
904         print "</table>\n";
905         git_footer_html();
906 }
907
908 sub read_info_ref {
909         my %refs;
910         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
911         # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
912         open my $fd, "$projectroot/$project/info/refs" or return;
913         while (my $line = <$fd>) {
914                 if ($line =~ m/^([0-9a-fA-F]{40})\t.*\/([^\^]+)/) {
915                         if (defined $refs{$1}) {
916                                 $refs{$1} .= " / $2";
917                         } else {
918                                 $refs{$1} = $2;
919                         }
920                 }
921         }
922         close $fd or return;
923         return \%refs;
924 }
925
926 sub git_read_refs {
927         my $ref_dir = shift;
928         my @reflist;
929
930         my @refs;
931         opendir my $dh, "$projectroot/$project/$ref_dir";
932         while (my $dir = readdir($dh)) {
933                 if ($dir =~ m/^\./) {
934                         next;
935                 }
936                 if (-d "$projectroot/$project/$ref_dir/$dir") {
937                         opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
938                         my @subdirs = grep !m/^\./, readdir $dh2;
939                         closedir($dh2);
940                         foreach my $subdir (@subdirs) {
941                                 push @refs, "$dir/$subdir"
942                         }
943                         next;
944                 }
945                 push @refs, $dir;
946         }
947         closedir($dh);
948         foreach my $ref_file (@refs) {
949                 my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
950                 my $type = git_get_type($ref_id) || next;
951                 my %ref_item;
952                 my %co;
953                 $ref_item{'type'} = $type;
954                 $ref_item{'id'} = $ref_id;
955                 $ref_item{'epoch'} = 0;
956                 $ref_item{'age'} = "unknown";
957                 if ($type eq "tag") {
958                         my %tag = git_read_tag($ref_id);
959                         $ref_item{'comment'} = $tag{'comment'};
960                         if ($tag{'type'} eq "commit") {
961                                 %co = git_read_commit($tag{'object'});
962                                 $ref_item{'epoch'} = $co{'committer_epoch'};
963                                 $ref_item{'age'} = $co{'age_string'};
964                         } elsif (defined($tag{'epoch'})) {
965                                 my $age = time - $tag{'epoch'};
966                                 $ref_item{'epoch'} = $tag{'epoch'};
967                                 $ref_item{'age'} = age_string($age);
968                         }
969                         $ref_item{'reftype'} = $tag{'type'};
970                         $ref_item{'name'} = $tag{'name'};
971                         $ref_item{'refid'} = $tag{'object'};
972                 } elsif ($type eq "commit"){
973                         %co = git_read_commit($ref_id);
974                         $ref_item{'reftype'} = "commit";
975                         $ref_item{'name'} = $ref_file;
976                         $ref_item{'title'} = $co{'title'};
977                         $ref_item{'refid'} = $ref_id;
978                         $ref_item{'epoch'} = $co{'committer_epoch'};
979                         $ref_item{'age'} = $co{'age_string'};
980                 }
981
982                 push @reflist, \%ref_item;
983         }
984         # sort tags by age
985         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
986         return \@reflist;
987 }
988
989 sub git_summary {
990         my $descr = git_read_description($project) || "none";
991         my $head = git_read_hash("$project/HEAD");
992         my %co = git_read_commit($head);
993         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
994
995         my $owner;
996         if (-f $projects_list) {
997                 open (my $fd , $projects_list);
998                 while (my $line = <$fd>) {
999                         chomp $line;
1000                         my ($pr, $ow) = split ' ', $line;
1001                         $pr = unescape($pr);
1002                         $ow = unescape($ow);
1003                         if ($pr eq $project) {
1004                                 $owner = $ow;
1005                                 last;
1006                         }
1007                 }
1008                 close $fd;
1009         }
1010         if (!defined $owner) {
1011                 $owner = get_file_owner("$projectroot/$project");
1012         }
1013
1014         my $refs = read_info_ref();
1015         git_header_html();
1016         print "<div class=\"page_nav\">\n" .
1017               "summary".
1018               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1019               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1020               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1021               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1022               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree")}, "tree") .
1023               "<br/><br/>\n" .
1024               "</div>\n";
1025         print "<div class=\"title\">&nbsp;</div>\n";
1026         print "<table cellspacing=\"0\">\n" .
1027               "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1028               "<tr><td>owner</td><td>$owner</td></tr>\n" .
1029               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
1030               "</table>\n";
1031         open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_hash("$project/HEAD") or die_error(undef, "Open failed.");
1032         my (@revlist) = map { chomp; $_ } <$fd>;
1033         close $fd;
1034         print "<div>\n" .
1035               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog"), -class => "title"}, "shortlog") .
1036               "</div>\n";
1037         my $i = 16;
1038         print "<table cellspacing=\"0\">\n";
1039         my $alternate = 0;
1040         foreach my $commit (@revlist) {
1041                 my %co = git_read_commit($commit);
1042                 my %ad = date_str($co{'author_epoch'});
1043                 if ($alternate) {
1044                         print "<tr class=\"dark\">\n";
1045                 } else {
1046                         print "<tr class=\"light\">\n";
1047                 }
1048                 $alternate ^= 1;
1049                 if ($i-- > 0) {
1050                         print "<td><i>$co{'age_string'}</i></td>\n" .
1051                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1052                               "<td>";
1053                         if (length($co{'title_short'}) < length($co{'title'})) {
1054                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
1055                                       "<b>" . esc_html($co{'title_short'}) . "</b>");
1056                         } else {
1057                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
1058                                       "<b>" . esc_html($co{'title'}) . "</b>");
1059                         }
1060                         if (defined $refs->{$commit}) {
1061                                 print " <span class=\"tag\">$refs->{$commit}</span>";
1062                         }
1063                         print "</td>\n" .
1064                               "<td class=\"link\">" .
1065                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1066                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1067                               "</td>\n" .
1068                               "</tr>";
1069                 } else {
1070                         print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "...") . "</td>\n" .
1071                         "</tr>";
1072                         last;
1073                 }
1074         }
1075         print "</table\n>";
1076
1077         my $taglist = git_read_refs("refs/tags");
1078         if (defined @$taglist) {
1079                 print "<div>\n" .
1080                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags"), -class => "title"}, "tags") .
1081                       "</div>\n";
1082                 my $i = 16;
1083                 print "<table cellspacing=\"0\">\n";
1084                 my $alternate = 0;
1085                 foreach my $entry (@$taglist) {
1086                         my %tag = %$entry;
1087                         my $comment_lines = $tag{'comment'};
1088                         my $comment = shift @$comment_lines;
1089                         if (defined($comment)) {
1090                                 $comment = chop_str($comment, 30, 5);
1091                         }
1092                         if ($alternate) {
1093                                 print "<tr class=\"dark\">\n";
1094                         } else {
1095                                 print "<tr class=\"light\">\n";
1096                         }
1097                         $alternate ^= 1;
1098                         if ($i-- > 0) {
1099                                 print "<td><i>$tag{'age'}</i></td>\n" .
1100                                       "<td>" .
1101                                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
1102                                       "<b>" . esc_html($tag{'name'}) . "</b>") .
1103                                       "</td>\n" .
1104                                       "<td>";
1105                                 if (defined($comment)) {
1106                                       print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
1107                                 }
1108                                 print "</td>\n" .
1109                                       "<td class=\"link\">";
1110                                 if ($tag{'type'} eq "tag") {
1111                                       print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
1112                                 }
1113                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1114                                 if ($tag{'reftype'} eq "commit") {
1115                                       print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1116                                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1117                                 }
1118                                 print "</td>\n" .
1119                                       "</tr>";
1120                         } else {
1121                                 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "...") . "</td>\n" .
1122                                 "</tr>";
1123                                 last;
1124                         }
1125                 }
1126                 print "</table\n>";
1127         }
1128
1129         my $headlist = git_read_refs("refs/heads");
1130         if (defined @$headlist) {
1131                 print "<div>\n" .
1132                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads"), -class => "title"}, "heads") .
1133                       "</div>\n";
1134                 my $i = 16;
1135                 print "<table cellspacing=\"0\">\n";
1136                 my $alternate = 0;
1137                 foreach my $entry (@$headlist) {
1138                         my %tag = %$entry;
1139                         if ($alternate) {
1140                                 print "<tr class=\"dark\">\n";
1141                         } else {
1142                                 print "<tr class=\"light\">\n";
1143                         }
1144                         $alternate ^= 1;
1145                         if ($i-- > 0) {
1146                                 print "<td><i>$tag{'age'}</i></td>\n" .
1147                                       "<td>" .
1148                                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"},
1149                                       "<b>" . esc_html($tag{'name'}) . "</b>") .
1150                                       "</td>\n" .
1151                                       "<td class=\"link\">" .
1152                                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1153                                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1154                                       "</td>\n" .
1155                                       "</tr>";
1156                         } else {
1157                                 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "...") . "</td>\n" .
1158                                 "</tr>";
1159                                 last;
1160                         }
1161                 }
1162                 print "</table\n>";
1163         }
1164         git_footer_html();
1165 }
1166
1167 sub git_tag {
1168         my $head = git_read_hash("$project/HEAD");
1169         git_header_html();
1170         print "<div class=\"page_nav\">\n" .
1171               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1172               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1173               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1174               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1175               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1176               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
1177               "<br/>\n" .
1178               "</div>\n";
1179         my %tag = git_read_tag($hash);
1180         print "<div>\n" .
1181               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($tag{'name'})) . "\n" .
1182               "</div>\n";
1183         print "<div class=\"title_text\">\n" .
1184               "<table cellspacing=\"0\">\n" .
1185               "<tr>\n" .
1186               "<td>object</td>\n" .
1187               "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
1188               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
1189               "</tr>\n";
1190         if (defined($tag{'author'})) {
1191                 my %ad = date_str($tag{'epoch'}, $tag{'tz'});
1192                 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1193                 print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1194         }
1195         print "</table>\n\n" .
1196               "</div>\n";
1197         print "<div class=\"page_body\">";
1198         my $comment = $tag{'comment'};
1199         foreach my $line (@$comment) {
1200                 print esc_html($line) . "<br/>\n";
1201         }
1202         print "</div>\n";
1203         git_footer_html();
1204 }
1205
1206 sub git_tags {
1207         my $head = git_read_hash("$project/HEAD");
1208         git_header_html();
1209         print "<div class=\"page_nav\">\n" .
1210               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1211               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1212               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1213               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1214               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1215               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
1216               "<br/>\n" .
1217               "</div>\n";
1218         my $taglist = git_read_refs("refs/tags");
1219         print "<div>\n" .
1220               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
1221               "</div>\n";
1222         print "<table cellspacing=\"0\">\n";
1223         my $alternate = 0;
1224         if (defined @$taglist) {
1225                 foreach my $entry (@$taglist) {
1226                         my %tag = %$entry;
1227                         my $comment_lines = $tag{'comment'};
1228                         my $comment = shift @$comment_lines;
1229                         if (defined($comment)) {
1230                                 $comment = chop_str($comment, 30, 5);
1231                         }
1232                         if ($alternate) {
1233                                 print "<tr class=\"dark\">\n";
1234                         } else {
1235                                 print "<tr class=\"light\">\n";
1236                         }
1237                         $alternate ^= 1;
1238                         print "<td><i>$tag{'age'}</i></td>\n" .
1239                               "<td>" .
1240                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
1241                               "<b>" . esc_html($tag{'name'}) . "</b>") .
1242                               "</td>\n" .
1243                               "<td>";
1244                         if (defined($comment)) {
1245                               print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
1246                         }
1247                         print "</td>\n" .
1248                               "<td class=\"link\">";
1249                         if ($tag{'type'} eq "tag") {
1250                               print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
1251                         }
1252                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1253                         if ($tag{'reftype'} eq "commit") {
1254                               print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1255                                     " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1256                         }
1257                         print "</td>\n" .
1258                               "</tr>";
1259                 }
1260         }
1261         print "</table\n>";
1262         git_footer_html();
1263 }
1264
1265 sub git_heads {
1266         my $head = git_read_hash("$project/HEAD");
1267         git_header_html();
1268         print "<div class=\"page_nav\">\n" .
1269               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1270               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1271               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1272               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1273               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1274               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
1275               "<br/>\n" .
1276               "</div>\n";
1277         my $taglist = git_read_refs("refs/heads");
1278         print "<div>\n" .
1279               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
1280               "</div>\n";
1281         print "<table cellspacing=\"0\">\n";
1282         my $alternate = 0;
1283         if (defined @$taglist) {
1284                 foreach my $entry (@$taglist) {
1285                         my %tag = %$entry;
1286                         if ($alternate) {
1287                                 print "<tr class=\"dark\">\n";
1288                         } else {
1289                                 print "<tr class=\"light\">\n";
1290                         }
1291                         $alternate ^= 1;
1292                         print "<td><i>$tag{'age'}</i></td>\n" .
1293                               "<td>" .
1294                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1295                               "</td>\n" .
1296                               "<td class=\"link\">" .
1297                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1298                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1299                               "</td>\n" .
1300                               "</tr>";
1301                 }
1302         }
1303         print "</table\n>";
1304         git_footer_html();
1305 }
1306
1307 sub git_get_hash_by_path {
1308         my $base = shift;
1309         my $path = shift || return undef;
1310
1311         my $tree = $base;
1312         my @parts = split '/', $path;
1313         while (my $part = shift @parts) {
1314                 open my $fd, "-|", "$gitbin/git-ls-tree $tree" or die_error(undef, "Open git-ls-tree failed.");
1315                 my (@entries) = map { chomp; $_ } <$fd>;
1316                 close $fd or return undef;
1317                 foreach my $line (@entries) {
1318                         #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1319                         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1320                         my $t_mode = $1;
1321                         my $t_type = $2;
1322                         my $t_hash = $3;
1323                         my $t_name = validate_input(unquote($4));
1324                         if ($t_name eq $part) {
1325                                 if (!(@parts)) {
1326                                         return $t_hash;
1327                                 }
1328                                 if ($t_type eq "tree") {
1329                                         $tree = $t_hash;
1330                                 }
1331                                 last;
1332                         }
1333                 }
1334         }
1335 }
1336
1337 sub git_blob {
1338         if (!defined $hash && defined $file_name) {
1339                 my $base = $hash_base || git_read_hash("$project/HEAD");
1340                 $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file.");
1341         }
1342         open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
1343         git_header_html();
1344         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1345                 print "<div class=\"page_nav\">\n" .
1346                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1347                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1348                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1349                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1350                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1351                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1352                 if (defined $file_name) {
1353                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1354                         " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "<br/>\n";
1355                 } else {
1356                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "<br/>\n";
1357                 }
1358                 print "</div>\n".
1359                        "<div>" .
1360                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1361                       "</div>\n";
1362         } else {
1363                 print "<div class=\"page_nav\">\n" .
1364                       "<br/><br/></div>\n" .
1365                       "<div class=\"title\">$hash</div>\n";
1366         }
1367         if (defined $file_name) {
1368                 print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
1369         }
1370         print "<div class=\"page_body\">\n";
1371         my $nr;
1372         while (my $line = <$fd>) {
1373                 chomp $line;
1374                 $nr++;
1375                 while ((my $pos = index($line, "\t")) != -1) {
1376                         if (my $count = (8 - ($pos % 8))) {
1377                                 my $spaces = ' ' x $count;
1378                                 $line =~ s/\t/$spaces/;
1379                         }
1380                 }
1381                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1382         }
1383         close $fd or print "Reading blob failed.\n";
1384         print "</div>";
1385         git_footer_html();
1386 }
1387
1388 sub git_blob_plain {
1389         my $save_as = "$hash.txt";
1390         if (defined $file_name) {
1391                 $save_as = $file_name;
1392         }
1393         print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"$save_as\"");
1394         open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
1395         undef $/;
1396         print <$fd>;
1397         $/ = "\n";
1398         close $fd;
1399 }
1400
1401 sub git_tree {
1402         if (!defined $hash) {
1403                 $hash = git_read_hash("$project/HEAD");
1404                 if (defined $file_name) {
1405                         my $base = $hash_base || git_read_hash("$project/HEAD");
1406                         $hash = git_get_hash_by_path($base, $file_name, "tree");
1407                 }
1408                 if (!defined $hash_base) {
1409                         $hash_base = git_read_hash("$project/HEAD");
1410                 }
1411         }
1412         $/ = "\0";
1413         open my $fd, "-|", "$gitbin/git-ls-tree -z $hash" or die_error(undef, "Open git-ls-tree failed.");
1414         chomp (my (@entries) = <$fd>);
1415         close $fd or die_error(undef, "Reading tree failed.");
1416         $/ = "\n";
1417
1418         git_header_html();
1419         my $base_key = "";
1420         my $base = "";
1421         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1422                 $base_key = ";hb=$hash_base";
1423                 print "<div class=\"page_nav\">\n" .
1424                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1425                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash_base")}, "shortlog") .
1426                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash_base")}, "log") .
1427                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1428                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1429                       " | tree" .
1430                       "<br/><br/>\n" .
1431                       "</div>\n";
1432                 print "<div>\n" .
1433                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" .
1434                       "</div>\n";
1435         } else {
1436                 print "<div class=\"page_nav\">\n";
1437                 print "<br/><br/></div>\n";
1438                 print "<div class=\"title\">$hash</div>\n";
1439         }
1440         if (defined $file_name) {
1441                 $base = esc_html("$file_name/");
1442                 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
1443         } else {
1444                 print "<div class=\"page_path\"><b>/</b></div>\n";
1445         }
1446         print "<div class=\"page_body\">\n";
1447         print "<table cellspacing=\"0\">\n";
1448         my $alternate = 0;
1449         foreach my $line (@entries) {
1450                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1451                 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1452                 my $t_mode = $1;
1453                 my $t_type = $2;
1454                 my $t_hash = $3;
1455                 my $t_name = validate_input($4);
1456                 if ($alternate) {
1457                         print "<tr class=\"dark\">\n";
1458                 } else {
1459                         print "<tr class=\"light\">\n";
1460                 }
1461                 $alternate ^= 1;
1462                 print "<td style=\"font-family:monospace\">" . mode_str($t_mode) . "</td>\n";
1463                 if ($t_type eq "blob") {
1464                         print "<td class=\"list\">" .
1465                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name"), -class => "list"}, esc_html($t_name)) .
1466                               "</td>\n" .
1467                               "<td class=\"link\">" .
1468                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
1469                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") .
1470                               "</td>\n";
1471                 } elsif ($t_type eq "tree") {
1472                         print "<td class=\"list\">" .
1473                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
1474                               "</td>\n" .
1475                               "<td class=\"link\">" .
1476                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
1477                               "</td>\n";
1478                 }
1479                 print "</tr>\n";
1480         }
1481         print "</table>\n" .
1482               "</div>";
1483         git_footer_html();
1484 }
1485
1486 sub git_rss {
1487         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1488         open my $fd, "-|", "$gitbin/git-rev-list --max-count=150 " . git_read_hash("$project/HEAD") or die_error(undef, "Open failed.");
1489         my (@revlist) = map { chomp; $_ } <$fd>;
1490         close $fd or die_error(undef, "Reading rev-list failed.");
1491         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1492         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1493               "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
1494         print "<channel>\n";
1495         print "<title>$project</title>\n".
1496               "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
1497               "<description>$project log</description>\n".
1498               "<language>en</language>\n";
1499
1500         for (my $i = 0; $i <= $#revlist; $i++) {
1501                 my $commit = $revlist[$i];
1502                 my %co = git_read_commit($commit);
1503                 # we read 150, we always show 30 and the ones more recent than 48 hours
1504                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
1505                         last;
1506                 }
1507                 my %cd = date_str($co{'committer_epoch'});
1508                 open $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $co{'id'}" or next;
1509                 my @difftree = map { chomp; $_ } <$fd>;
1510                 close $fd or next;
1511                 print "<item>\n" .
1512                       "<title>" .
1513                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
1514                       "</title>\n" .
1515                       "<author>" . esc_html($co{'author'}) . "</author>\n" .
1516                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
1517                       "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
1518                       "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
1519                       "<description>" . esc_html($co{'title'}) . "</description>\n" .
1520                       "<content:encoded>" .
1521                       "<![CDATA[\n";
1522                 my $comment = $co{'comment'};
1523                 foreach my $line (@$comment) {
1524                         $line = decode("utf8", $line, Encode::FB_DEFAULT);
1525                         print "$line<br/>\n";
1526                 }
1527                 print "<br/>\n";
1528                 foreach my $line (@difftree) {
1529                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1530                                 next;
1531                         }
1532                         my $file = validate_input(unquote($7));
1533                         $file = decode("utf8", $file, Encode::FB_DEFAULT);
1534                         print "$file<br/>\n";
1535                 }
1536                 print "]]>\n" .
1537                       "</content:encoded>\n" .
1538                       "</item>\n";
1539         }
1540         print "</channel></rss>";
1541 }
1542
1543 sub git_opml {
1544         my @list = git_read_projects();
1545
1546         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1547         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1548               "<opml version=\"1.0\">\n".
1549               "<head>".
1550               "  <title>Git OPML Export</title>\n".
1551               "</head>\n".
1552               "<body>\n".
1553               "<outline text=\"git RSS feeds\">\n";
1554
1555         foreach my $pr (@list) {
1556                 my %proj = %$pr;
1557                 my $head = git_read_hash("$proj{'path'}/HEAD");
1558                 if (!defined $head) {
1559                         next;
1560                 }
1561                 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
1562                 my %co = git_read_commit($head);
1563                 if (!%co) {
1564                         next;
1565                 }
1566
1567                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
1568                 my $rss =  "$my_url?p=$proj{'path'};a=rss";
1569                 my $html =  "$my_url?p=$proj{'path'};a=summary";
1570                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
1571         }
1572         print "</outline>\n".
1573               "</body>\n".
1574               "</opml>\n";
1575 }
1576
1577 sub git_log {
1578         my $head = git_read_hash("$project/HEAD");
1579         if (!defined $hash) {
1580                 $hash = $head;
1581         }
1582         if (!defined $page) {
1583                 $page = 0;
1584         }
1585         my $refs = read_info_ref();
1586         git_header_html();
1587         print "<div class=\"page_nav\">\n";
1588         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1589               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
1590               " | log" .
1591               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
1592               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
1593               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
1594
1595         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1596         open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
1597         my (@revlist) = map { chomp; $_ } <$fd>;
1598         close $fd;
1599
1600         if ($hash ne $head || $page) {
1601                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "HEAD");
1602         } else {
1603                 print "HEAD";
1604         }
1605         if ($page > 0) {
1606                 print " &sdot; " .
1607                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
1608         } else {
1609                 print " &sdot; prev";
1610         }
1611         if ($#revlist >= (100 * ($page+1)-1)) {
1612                 print " &sdot; " .
1613                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
1614         } else {
1615                 print " &sdot; next";
1616         }
1617         print "<br/>\n" .
1618               "</div>\n";
1619         if (!@revlist) {
1620                 print "<div>\n" .
1621                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
1622                       "</div>\n";
1623                 my %co = git_read_commit($hash);
1624                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1625         }
1626         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1627                 my $commit = $revlist[$i];
1628                 my $ref = "";
1629                 if (defined $refs->{$commit}) {
1630                         $ref = " <span class=\"tag\">$refs->{$commit}</span>";
1631                 }
1632                 my %co = git_read_commit($commit);
1633                 next if !%co;
1634                 my %ad = date_str($co{'author_epoch'});
1635                 print "<div>\n" .
1636                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "title"},
1637                       "<span class=\"age\">$co{'age_string'}</span>" . esc_html($co{'title'}) . $ref) . "\n";
1638                 print "</div>\n";
1639                 print "<div class=\"title_text\">\n" .
1640                       "<div class=\"log_link\">\n" .
1641                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1642                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1643                       "<br/>\n" .
1644                       "</div>\n" .
1645                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1646                       "</div>\n" .
1647                       "<div class=\"log_body\">\n";
1648                 my $comment = $co{'comment'};
1649                 my $empty = 0;
1650                 foreach my $line (@$comment) {
1651                         if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1652                                 next;
1653                         }
1654                         if ($line eq "") {
1655                                 if ($empty) {
1656                                         next;
1657                                 }
1658                                 $empty = 1;
1659                         } else {
1660                                 $empty = 0;
1661                         }
1662                         print format_log_line_html($line) . "<br/>\n";
1663                 }
1664                 if (!$empty) {
1665                         print "<br/>\n";
1666                 }
1667                 print "</div>\n";
1668         }
1669         git_footer_html();
1670 }
1671
1672 sub git_commit {
1673         my %co = git_read_commit($hash);
1674         if (!%co) {
1675                 die_error(undef, "Unknown commit object.");
1676         }
1677         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1678         my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1679
1680         my @difftree;
1681         my $root = "";
1682         my $parent = $co{'parent'};
1683         if (!defined $parent) {
1684                 $root = " --root";
1685                 $parent = "";
1686         }
1687         open my $fd, "-|", "$gitbin/git-diff-tree -r -M $root $parent $hash" or die_error(undef, "Open failed.");
1688         @difftree = map { chomp; $_ } <$fd>;
1689         close $fd or die_error(undef, "Reading diff-tree failed.");
1690
1691         # non-textual hash id's can be cached
1692         my $expires;
1693         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1694                 $expires = "+1d";
1695         }
1696         git_header_html(undef, $expires);
1697         print "<div class=\"page_nav\">\n" .
1698               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1699               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
1700               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
1701               " | commit";
1702         if (defined $co{'parent'}) {
1703                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff");
1704         }
1705         print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . "\n" .
1706               "<br/><br/></div>\n";
1707         if (defined $co{'parent'}) {
1708                 print "<div>\n" .
1709                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
1710                       "</div>\n";
1711         } else {
1712                 print "<div>\n" .
1713                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
1714                       "</div>\n";
1715         }
1716         print "<div class=\"title_text\">\n" .
1717               "<table cellspacing=\"0\">\n";
1718         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
1719               "<tr>" .
1720               "<td></td><td> $ad{'rfc2822'}";
1721         if ($ad{'hour_local'} < 6) {
1722                 printf(" (<span style=\"color: #cc0000;\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1723         } else {
1724                 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1725         }
1726         print "</td>" .
1727               "</tr>\n";
1728         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
1729         print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1730         print "<tr><td>commit</td><td style=\"font-family:monospace\">$co{'id'}</td></tr>\n";
1731         print "<tr>" .
1732               "<td>tree</td>" .
1733               "<td style=\"font-family:monospace\">" .
1734               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
1735               "</td>" .
1736               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
1737               "</td>" .
1738               "</tr>\n";
1739         my $parents  = $co{'parents'};
1740         foreach my $par (@$parents) {
1741                 print "<tr>" .
1742                       "<td>parent</td>" .
1743                       "<td style=\"font-family:monospace\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
1744                       "<td class=\"link\">" .
1745                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
1746                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
1747                       "</td>" .
1748                       "</tr>\n";
1749         }
1750         print "</table>". 
1751               "</div>\n";
1752         print "<div class=\"page_body\">\n";
1753         my $comment = $co{'comment'};
1754         my $empty = 0;
1755         my $signed = 0;
1756         foreach my $line (@$comment) {
1757                 # print only one empty line
1758                 if ($line eq "") {
1759                         if ($empty || $signed) {
1760                                 next;
1761                         }
1762                         $empty = 1;
1763                 } else {
1764                         $empty = 0;
1765                 }
1766                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1767                         $signed = 1;
1768                         print "<span style=\"color: #888888\">" . esc_html($line) . "</span><br/>\n";
1769                 } else {
1770                         $signed = 0;
1771                         print format_log_line_html($line) . "<br/>\n";
1772                 }
1773         }
1774         print "</div>\n";
1775         print "<div class=\"list_head\">\n";
1776         if ($#difftree > 10) {
1777                 print(($#difftree + 1) . " files changed:\n");
1778         }
1779         print "</div>\n";
1780         print "<table cellspacing=\"0\">\n";
1781         my $alternate = 0;
1782         foreach my $line (@difftree) {
1783                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
1784                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
1785                 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1786                         next;
1787                 }
1788                 my $from_mode = $1;
1789                 my $to_mode = $2;
1790                 my $from_id = $3;
1791                 my $to_id = $4;
1792                 my $status = $5;
1793                 my $similarity = $6;
1794                 my $file = validate_input(unquote($7));
1795                 if ($alternate) {
1796                         print "<tr class=\"dark\">\n";
1797                 } else {
1798                         print "<tr class=\"light\">\n";
1799                 }
1800                 $alternate ^= 1;
1801                 if ($status eq "A") {
1802                         my $mode_chng = "";
1803                         if (S_ISREG(oct $to_mode)) {
1804                                 $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
1805                         }
1806                         print "<td>" .
1807                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
1808                               "<td><span style=\"color: #008000;\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
1809                               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") . "</td>\n";
1810                 } elsif ($status eq "D") {
1811                         print "<td>" .
1812                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
1813                               "<td><span style=\"color: #c00000;\">[deleted " . file_type($from_mode). "]</span></td>\n" .
1814                               "<td class=\"link\">" .
1815                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") .
1816                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") .
1817                               "</td>\n"
1818                 } elsif ($status eq "M" || $status eq "T") {
1819                         my $mode_chnge = "";
1820                         if ($from_mode != $to_mode) {
1821                                 $mode_chnge = " <span style=\"color: #777777;\">[changed";
1822                                 if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
1823                                         $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
1824                                 }
1825                                 if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
1826                                         if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
1827                                                 $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
1828                                         } elsif (S_ISREG($to_mode)) {
1829                                                 $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
1830                                         }
1831                                 }
1832                                 $mode_chnge .= "]</span>\n";
1833                         }
1834                         print "<td>";
1835                         if ($to_id ne $from_id) {
1836                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
1837                         } else {
1838                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
1839                         }
1840                         print "</td>\n" .
1841                               "<td>$mode_chnge</td>\n" .
1842                               "<td class=\"link\">";
1843                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob");
1844                         if ($to_id ne $from_id) {
1845                                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff");
1846                         }
1847                         print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") . "\n";
1848                         print "</td>\n";
1849                 } elsif ($status eq "R") {
1850                         my ($from_file, $to_file) = split "\t", $file;
1851                         my $mode_chng = "";
1852                         if ($from_mode != $to_mode) {
1853                                 $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
1854                         }
1855                         print "<td>" .
1856                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file"), -class => "list"}, esc_html($to_file)) . "</td>\n" .
1857                               "<td><span style=\"color: #777777;\">[moved from " .
1858                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$from_file"), -class => "list"}, esc_html($from_file)) .
1859                               " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
1860                               "<td class=\"link\">" .
1861                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
1862                         if ($to_id ne $from_id) {
1863                                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file")}, "diff");
1864                         }
1865                         print "</td>\n";
1866                 }
1867                 print "</tr>\n";
1868         }
1869         print "</table>\n";
1870         git_footer_html();
1871 }
1872
1873 sub git_blobdiff {
1874         mkdir($git_temp, 0700);
1875         git_header_html();
1876         if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1877                 print "<div class=\"page_nav\">\n" .
1878                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1879                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1880                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1881                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1882                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1883                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") .
1884                       "<br/>\n";
1885                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain") .
1886                       "</div>\n";
1887                 print "<div>\n" .
1888                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" .
1889                       "</div>\n";
1890         } else {
1891                 print "<div class=\"page_nav\">\n" .
1892                       "<br/><br/></div>\n" .
1893                       "<div class=\"title\">$hash vs $hash_parent</div>\n";
1894         }
1895         if (defined $file_name) {
1896                 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
1897         }
1898         print "<div class=\"page_body\">\n" .
1899               "<div class=\"diff_info\">blob:" .
1900               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
1901               " -> blob:" .
1902               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
1903               "</div>\n";
1904         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
1905         print "</div>";
1906         git_footer_html();
1907 }
1908
1909 sub git_blobdiff_plain {
1910         mkdir($git_temp, 0700);
1911         print $cgi->header(-type => "text/plain", -charset => 'utf-8');
1912         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
1913 }
1914
1915 sub git_commitdiff {
1916         mkdir($git_temp, 0700);
1917         my %co = git_read_commit($hash);
1918         if (!%co) {
1919                 die_error(undef, "Unknown commit object.");
1920         }
1921         if (!defined $hash_parent) {
1922                 $hash_parent = $co{'parent'};
1923         }
1924         open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
1925         my (@difftree) = map { chomp; $_ } <$fd>;
1926         close $fd or die_error(undef, "Reading diff-tree failed.");
1927
1928         # non-textual hash id's can be cached
1929         my $expires;
1930         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1931                 $expires = "+1d";
1932         }
1933         git_header_html(undef, $expires);
1934         print "<div class=\"page_nav\">\n" .
1935               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1936               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
1937               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
1938               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
1939               " | commitdiff" .
1940               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . "<br/>\n";
1941         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain") . "\n" .
1942               "</div>\n";
1943         print "<div>\n" .
1944               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
1945               "</div>\n";
1946         print "<div class=\"page_body\">\n";
1947         my $comment = $co{'comment'};
1948         my $empty = 0;
1949         my $signed = 0;
1950         my @log = @$comment;
1951         # remove first and empty lines after that
1952         shift @log;
1953         while (defined $log[0] && $log[0] eq "") {
1954                 shift @log;
1955         }
1956         foreach my $line (@log) {
1957                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1958                         next;
1959                 }
1960                 if ($line eq "") {
1961                         if ($empty) {
1962                                 next;
1963                         }
1964                         $empty = 1;
1965                 } else {
1966                         $empty = 0;
1967                 }
1968                 print format_log_line_html($line) . "<br/>\n";
1969         }
1970         print "<br/>\n";
1971         foreach my $line (@difftree) {
1972                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
1973                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
1974                 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
1975                 my $from_mode = $1;
1976                 my $to_mode = $2;
1977                 my $from_id = $3;
1978                 my $to_id = $4;
1979                 my $status = $5;
1980                 my $file = validate_input(unquote($6));
1981                 if ($status eq "A") {
1982                         print "<div class=\"diff_info\">" .  file_type($to_mode) . ":" .
1983                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
1984                               "</div>\n";
1985                         git_diff_print(undef, "/dev/null", $to_id, "b/$file");
1986                 } elsif ($status eq "D") {
1987                         print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
1988                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) . "(deleted)" .
1989                               "</div>\n";
1990                         git_diff_print($from_id, "a/$file", undef, "/dev/null");
1991                 } elsif ($status eq "M") {
1992                         if ($from_id ne $to_id) {
1993                                 print "<div class=\"diff_info\">" .
1994                                       file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) .
1995                                       " -> " .
1996                                       file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
1997                                 print "</div>\n";
1998                                 git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
1999                         }
2000                 }
2001         }
2002         print "<br/>\n" .
2003               "</div>";
2004         git_footer_html();
2005 }
2006
2007 sub git_commitdiff_plain {
2008         mkdir($git_temp, 0700);
2009         open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
2010         my (@difftree) = map { chomp; $_ } <$fd>;
2011         close $fd or die_error(undef, "Reading diff-tree failed.");
2012
2013         # try to figure out the next tag after this commit
2014         my $tagname;
2015         my $refs = read_info_ref();
2016         open $fd, "-|", "$gitbin/git-rev-list HEAD";
2017         chomp (my (@commits) = <$fd>);
2018         close $fd;
2019         foreach my $commit (@commits) {
2020                 if (defined $refs->{$commit}) {
2021                         $tagname = $refs->{$commit}
2022                 }
2023                 if ($commit eq $hash) {
2024                         last;
2025                 }
2026         }
2027
2028         print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
2029         my %co = git_read_commit($hash);
2030         my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
2031         my $comment = $co{'comment'};
2032         print "From: $co{'author'}\n" .
2033               "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
2034               "Subject: $co{'title'}\n";
2035         if (defined $tagname) {
2036               print "X-Git-Tag: $tagname\n";
2037         }
2038         print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2039               "\n";
2040
2041         foreach my $line (@$comment) {;
2042                 print "  $line\n";
2043         }
2044         print "---\n\n";
2045
2046         foreach my $line (@difftree) {
2047                 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2048                 my $from_id = $3;
2049                 my $to_id = $4;
2050                 my $status = $5;
2051                 my $file = $6;
2052                 if ($status eq "A") {
2053                         git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2054                 } elsif ($status eq "D") {
2055                         git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2056                 } elsif ($status eq "M") {
2057                         git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
2058                 }
2059         }
2060 }
2061
2062 sub git_history {
2063         if (!defined $hash) {
2064                 $hash = git_read_hash("$project/HEAD");
2065         }
2066         my %co = git_read_commit($hash);
2067         if (!%co) {
2068                 die_error(undef, "Unknown commit object.");
2069         }
2070         my $refs = read_info_ref();
2071         git_header_html();
2072         print "<div class=\"page_nav\">\n" .
2073               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2074               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2075               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
2076               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2077               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2078               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
2079               "<br/><br/>\n" .
2080               "</div>\n";
2081         print "<div>\n" .
2082               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2083               "</div>\n";
2084         print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b><br/></div>\n";
2085
2086         open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin \'$file_name\'";
2087         my $commit;
2088         print "<table cellspacing=\"0\">\n";
2089         my $alternate = 0;
2090         while (my $line = <$fd>) {
2091                 if ($line =~ m/^([0-9a-fA-F]{40})/){
2092                         $commit = $1;
2093                         next;
2094                 }
2095                 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/ && (defined $commit)) {
2096                         my %co = git_read_commit($commit);
2097                         if (!%co) {
2098                                 next;
2099                         }
2100                         my $ref = "";
2101                         if (defined $refs->{$commit}) {
2102                                 $ref = " <span class=\"tag\">$refs->{$commit}</span>";
2103                         }
2104                         if ($alternate) {
2105                                 print "<tr class=\"dark\">\n";
2106                         } else {
2107                                 print "<tr class=\"light\">\n";
2108                         }
2109                         $alternate ^= 1;
2110                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2111                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2112                               "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
2113                               esc_html(chop_str($co{'title'}, 50)) . "</b>") . "$ref</td>\n" .
2114                               "<td class=\"link\">" .
2115                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2116                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2117                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=$commit;f=$file_name")}, "blob");
2118                         my $blob = git_get_hash_by_path($hash, $file_name);
2119                         my $blob_parent = git_get_hash_by_path($commit, $file_name);
2120                         if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
2121                                 print " | " .
2122                                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
2123                                 "diff to current");
2124                         }
2125                         print "</td>\n" .
2126                               "</tr>\n";
2127                         undef $commit;
2128                 }
2129         }
2130         print "</table>\n";
2131         close $fd;
2132         git_footer_html();
2133 }
2134
2135 sub git_search {
2136         if (!defined $searchtext) {
2137                 die_error("", "Text field empty.");
2138         }
2139         if (!defined $hash) {
2140                 $hash = git_read_hash("$project/HEAD");
2141         }
2142         my %co = git_read_commit($hash);
2143         if (!%co) {
2144                 die_error(undef, "Unknown commit object.");
2145         }
2146         # pickaxe may take all resources of your box and run for several minutes
2147         # with every query - so decide by yourself how public you make this feature :)
2148         my $commit_search = 1;
2149         my $author_search = 0;
2150         my $committer_search = 0;
2151         my $pickaxe_search = 0;
2152         if ($searchtext =~ s/^author\\://i) {
2153                 $author_search = 1;
2154         } elsif ($searchtext =~ s/^committer\\://i) {
2155                 $committer_search = 1;
2156         } elsif ($searchtext =~ s/^pickaxe\\://i) {
2157                 $commit_search = 0;
2158                 $pickaxe_search = 1;
2159         }
2160         git_header_html();
2161         print "<div class=\"page_nav\">\n" .
2162               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary;h=$hash")}, "summary") .
2163               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2164               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2165               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2166               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2167               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
2168               "<br/><br/>\n" .
2169               "</div>\n";
2170
2171         print "<div>\n" .
2172               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2173               "</div>\n";
2174         print "<table cellspacing=\"0\">\n";
2175         my $alternate = 0;
2176         if ($commit_search) {
2177                 $/ = "\0";
2178                 open my $fd, "-|", "$gitbin/git-rev-list --header --parents $hash" or next;
2179                 while (my $commit_text = <$fd>) {
2180                         if (!grep m/$searchtext/i, $commit_text) {
2181                                 next;
2182                         }
2183                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2184                                 next;
2185                         }
2186                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2187                                 next;
2188                         }
2189                         my @commit_lines = split "\n", $commit_text;
2190                         my %co = git_read_commit(undef, \@commit_lines);
2191                         if (!%co) {
2192                                 next;
2193                         }
2194                         if ($alternate) {
2195                                 print "<tr class=\"dark\">\n";
2196                         } else {
2197                                 print "<tr class=\"light\">\n";
2198                         }
2199                         $alternate ^= 1;
2200                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2201                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2202                               "<td>" .
2203                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" . esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2204                         my $comment = $co{'comment'};
2205                         foreach my $line (@$comment) {
2206                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2207                                         my $lead = esc_html($1) || "";
2208                                         $lead = chop_str($lead, 30, 10);
2209                                         my $match = esc_html($2) || "";
2210                                         my $trail = esc_html($3) || "";
2211                                         $trail = chop_str($trail, 30, 10);
2212                                         my $text = "$lead<span style=\"color:#e00000\">$match</span>$trail";
2213                                         print chop_str($text, 80, 5) . "<br/>\n";
2214                                 }
2215                         }
2216                         print "</td>\n" .
2217                               "<td class=\"link\">" .
2218                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2219                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2220                         print "</td>\n" .
2221                               "</tr>\n";
2222                 }
2223                 close $fd;
2224         }
2225
2226         if ($pickaxe_search) {
2227                 $/ = "\n";
2228                 open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -S\'$searchtext\'";
2229                 undef %co;
2230                 my @files;
2231                 while (my $line = <$fd>) {
2232                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2233                                 my %set;
2234                                 $set{'file'} = $6;
2235                                 $set{'from_id'} = $3;
2236                                 $set{'to_id'} = $4;
2237                                 $set{'id'} = $set{'to_id'};
2238                                 if ($set{'id'} =~ m/0{40}/) {
2239                                         $set{'id'} = $set{'from_id'};
2240                                 }
2241                                 if ($set{'id'} =~ m/0{40}/) {
2242                                         next;
2243                                 }
2244                                 push @files, \%set;
2245                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2246                                 if (%co) {
2247                                         if ($alternate) {
2248                                                 print "<tr class=\"dark\">\n";
2249                                         } else {
2250                                                 print "<tr class=\"light\">\n";
2251                                         }
2252                                         $alternate ^= 1;
2253                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2254                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2255                                               "<td>" .
2256                                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
2257                                               esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2258                                         while (my $setref = shift @files) {
2259                                                 my %set = %$setref;
2260                                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
2261                                                       "<span style=\"color:#e00000\">" . esc_html($set{'file'}) . "</span>") .
2262                                                       "<br/>\n";
2263                                         }
2264                                         print "</td>\n" .
2265                                               "<td class=\"link\">" .
2266                                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2267                                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2268                                         print "</td>\n" .
2269                                               "</tr>\n";
2270                                 }
2271                                 %co = git_read_commit($1);
2272                         }
2273                 }
2274                 close $fd;
2275         }
2276         print "</table>\n";
2277         git_footer_html();
2278 }
2279
2280 sub git_shortlog {
2281         my $head = git_read_hash("$project/HEAD");
2282         if (!defined $hash) {
2283                 $hash = $head;
2284         }
2285         if (!defined $page) {
2286                 $page = 0;
2287         }
2288         my $refs = read_info_ref();
2289         git_header_html();
2290         print "<div class=\"page_nav\">\n" .
2291               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2292               " | shortlog" .
2293               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2294               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2295               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2296               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
2297
2298         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2299         open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
2300         my (@revlist) = map { chomp; $_ } <$fd>;
2301         close $fd;
2302
2303         if ($hash ne $head || $page) {
2304                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "HEAD");
2305         } else {
2306                 print "HEAD";
2307         }
2308         if ($page > 0) {
2309                 print " &sdot; " .
2310                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
2311         } else {
2312                 print " &sdot; prev";
2313         }
2314         if ($#revlist >= (100 * ($page+1)-1)) {
2315                 print " &sdot; " .
2316                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
2317         } else {
2318                 print " &sdot; next";
2319         }
2320         print "<br/>\n" .
2321               "</div>\n";
2322         print "<div>\n" .
2323               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
2324               "</div>\n";
2325         print "<table cellspacing=\"0\">\n";
2326         my $alternate = 0;
2327         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2328                 my $commit = $revlist[$i];
2329                 my %co = git_read_commit($commit);
2330                 my %ad = date_str($co{'author_epoch'});
2331                 if ($alternate) {
2332                         print "<tr class=\"dark\">\n";
2333                 } else {
2334                         print "<tr class=\"light\">\n";
2335                 }
2336                 $alternate ^= 1;
2337                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2338                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2339                       "<td>";
2340                 if (length($co{'title_short'}) < length($co{'title'})) {
2341                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
2342                               "<b>" . esc_html($co{'title_short'}) . "</b>");
2343                 } else {
2344                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
2345                               "<b>" . esc_html($co{'title_short'}) . "</b>");
2346                 }
2347                 if (defined $refs->{$commit}) {
2348                         print " <span class=\"tag\">$refs->{$commit}</span>";
2349                 }
2350                 print "</td>\n" .
2351                       "<td class=\"link\">" .
2352                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2353                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2354                       "</td>\n" .
2355                       "</tr>";
2356         }
2357         if ($#revlist >= (100 * ($page+1)-1)) {
2358                 print "<tr>\n" .
2359                       "<td>" .
2360                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -title => "Alt-n"}, "next") .
2361                       "</td>\n" .
2362                       "</tr>\n";
2363         }
2364         print "</table\n>";
2365         git_footer_html();
2366 }