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