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