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