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