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