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