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