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