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