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