Code

Merge branch 'fl/cvsserver'
[git.git] / gitweb / gitweb.perl
1 #!/usr/bin/perl
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
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';
21 BEGIN {
22         CGI->compile() if $ENV{'MOD_PERL'};
23 }
25 our $cgi = new CGI;
26 our $version = "++GIT_VERSION++";
27 our $my_url = $cgi->url();
28 our $my_uri = $cgi->url(-absolute => 1);
30 # core git executable to use
31 # this can just be "git" if your webserver has a sensible PATH
32 our $GIT = "++GIT_BINDIR++/git";
34 # absolute fs-path which will be prepended to the project path
35 #our $projectroot = "/pub/scm";
36 our $projectroot = "++GITWEB_PROJECTROOT++";
38 # target of the home link on top of all pages
39 our $home_link = $my_uri || "/";
41 # string of the home link on top of all pages
42 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
44 # name of your site or organization to appear in page titles
45 # replace this with something more descriptive for clearer bookmarks
46 our $site_name = "++GITWEB_SITENAME++"
47                  || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
49 # filename of html text to include at top of each page
50 our $site_header = "++GITWEB_SITE_HEADER++";
51 # html text to include at home page
52 our $home_text = "++GITWEB_HOMETEXT++";
53 # filename of html text to include at bottom of each page
54 our $site_footer = "++GITWEB_SITE_FOOTER++";
56 # URI of stylesheets
57 our @stylesheets = ("++GITWEB_CSS++");
58 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
59 our $stylesheet = undef;
60 # URI of GIT logo (72x27 size)
61 our $logo = "++GITWEB_LOGO++";
62 # URI of GIT favicon, assumed to be image/png type
63 our $favicon = "++GITWEB_FAVICON++";
65 # URI and label (title) of GIT logo link
66 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
67 #our $logo_label = "git documentation";
68 our $logo_url = "http://git.or.cz/";
69 our $logo_label = "git homepage";
71 # source of projects list
72 our $projects_list = "++GITWEB_LIST++";
74 # default order of projects list
75 # valid values are none, project, descr, owner, and age
76 our $default_projects_order = "project";
78 # show repository only if this file exists
79 # (only effective if this variable evaluates to true)
80 our $export_ok = "++GITWEB_EXPORT_OK++";
82 # only allow viewing of repositories also shown on the overview page
83 our $strict_export = "++GITWEB_STRICT_EXPORT++";
85 # list of git base URLs used for URL to where fetch project from,
86 # i.e. full URL is "$git_base_url/$project"
87 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
89 # default blob_plain mimetype and default charset for text/plain blob
90 our $default_blob_plain_mimetype = 'text/plain';
91 our $default_text_plain_charset  = undef;
93 # file to use for guessing MIME types before trying /etc/mime.types
94 # (relative to the current git repository)
95 our $mimetypes_file = undef;
97 # You define site-wide feature defaults here; override them with
98 # $GITWEB_CONFIG as necessary.
99 our %feature = (
100         # feature => {
101         #       'sub' => feature-sub (subroutine),
102         #       'override' => allow-override (boolean),
103         #       'default' => [ default options...] (array reference)}
104         #
105         # if feature is overridable (it means that allow-override has true value,
106         # then feature-sub will be called with default options as parameters;
107         # return value of feature-sub indicates if to enable specified feature
108         #
109         # use gitweb_check_feature(<feature>) to check if <feature> is enabled
111         # Enable the 'blame' blob view, showing the last commit that modified
112         # each line in the file. This can be very CPU-intensive.
114         # To enable system wide have in $GITWEB_CONFIG
115         # $feature{'blame'}{'default'} = [1];
116         # To have project specific config enable override in $GITWEB_CONFIG
117         # $feature{'blame'}{'override'} = 1;
118         # and in project config gitweb.blame = 0|1;
119         'blame' => {
120                 'sub' => \&feature_blame,
121                 'override' => 0,
122                 'default' => [0]},
124         # Enable the 'snapshot' link, providing a compressed tarball of any
125         # tree. This can potentially generate high traffic if you have large
126         # project.
128         # To disable system wide have in $GITWEB_CONFIG
129         # $feature{'snapshot'}{'default'} = [undef];
130         # To have project specific config enable override in $GITWEB_CONFIG
131         # $feature{'snapshot'}{'override'} = 1;
132         # and in project config gitweb.snapshot = none|gzip|bzip2;
133         'snapshot' => {
134                 'sub' => \&feature_snapshot,
135                 'override' => 0,
136                 #         => [content-encoding, suffix, program]
137                 'default' => ['x-gzip', 'gz', 'gzip']},
139         # Enable text search, which will list the commits which match author,
140         # committer or commit text to a given string.  Enabled by default.
141         'search' => {
142                 'override' => 0,
143                 'default' => [1]},
145         # Enable the pickaxe search, which will list the commits that modified
146         # a given string in a file. This can be practical and quite faster
147         # alternative to 'blame', but still potentially CPU-intensive.
149         # To enable system wide have in $GITWEB_CONFIG
150         # $feature{'pickaxe'}{'default'} = [1];
151         # To have project specific config enable override in $GITWEB_CONFIG
152         # $feature{'pickaxe'}{'override'} = 1;
153         # and in project config gitweb.pickaxe = 0|1;
154         'pickaxe' => {
155                 'sub' => \&feature_pickaxe,
156                 'override' => 0,
157                 'default' => [1]},
159         # Make gitweb use an alternative format of the URLs which can be
160         # more readable and natural-looking: project name is embedded
161         # directly in the path and the query string contains other
162         # auxiliary information. All gitweb installations recognize
163         # URL in either format; this configures in which formats gitweb
164         # generates links.
166         # To enable system wide have in $GITWEB_CONFIG
167         # $feature{'pathinfo'}{'default'} = [1];
168         # Project specific override is not supported.
170         # Note that you will need to change the default location of CSS,
171         # favicon, logo and possibly other files to an absolute URL. Also,
172         # if gitweb.cgi serves as your indexfile, you will need to force
173         # $my_uri to contain the script name in your $GITWEB_CONFIG.
174         'pathinfo' => {
175                 'override' => 0,
176                 'default' => [0]},
178         # Make gitweb consider projects in project root subdirectories
179         # to be forks of existing projects. Given project $projname.git,
180         # projects matching $projname/*.git will not be shown in the main
181         # projects list, instead a '+' mark will be added to $projname
182         # there and a 'forks' view will be enabled for the project, listing
183         # all the forks. If project list is taken from a file, forks have
184         # to be listed after the main project.
186         # To enable system wide have in $GITWEB_CONFIG
187         # $feature{'forks'}{'default'} = [1];
188         # Project specific override is not supported.
189         'forks' => {
190                 'override' => 0,
191                 'default' => [0]},
192 );
194 sub gitweb_check_feature {
195         my ($name) = @_;
196         return unless exists $feature{$name};
197         my ($sub, $override, @defaults) = (
198                 $feature{$name}{'sub'},
199                 $feature{$name}{'override'},
200                 @{$feature{$name}{'default'}});
201         if (!$override) { return @defaults; }
202         if (!defined $sub) {
203                 warn "feature $name is not overrideable";
204                 return @defaults;
205         }
206         return $sub->(@defaults);
209 sub feature_blame {
210         my ($val) = git_get_project_config('blame', '--bool');
212         if ($val eq 'true') {
213                 return 1;
214         } elsif ($val eq 'false') {
215                 return 0;
216         }
218         return $_[0];
221 sub feature_snapshot {
222         my ($ctype, $suffix, $command) = @_;
224         my ($val) = git_get_project_config('snapshot');
226         if ($val eq 'gzip') {
227                 return ('x-gzip', 'gz', 'gzip');
228         } elsif ($val eq 'bzip2') {
229                 return ('x-bzip2', 'bz2', 'bzip2');
230         } elsif ($val eq 'none') {
231                 return ();
232         }
234         return ($ctype, $suffix, $command);
237 sub gitweb_have_snapshot {
238         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
239         my $have_snapshot = (defined $ctype && defined $suffix);
241         return $have_snapshot;
244 sub feature_pickaxe {
245         my ($val) = git_get_project_config('pickaxe', '--bool');
247         if ($val eq 'true') {
248                 return (1);
249         } elsif ($val eq 'false') {
250                 return (0);
251         }
253         return ($_[0]);
256 # checking HEAD file with -e is fragile if the repository was
257 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
258 # and then pruned.
259 sub check_head_link {
260         my ($dir) = @_;
261         my $headfile = "$dir/HEAD";
262         return ((-e $headfile) ||
263                 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
266 sub check_export_ok {
267         my ($dir) = @_;
268         return (check_head_link($dir) &&
269                 (!$export_ok || -e "$dir/$export_ok"));
272 # rename detection options for git-diff and git-diff-tree
273 # - default is '-M', with the cost proportional to
274 #   (number of removed files) * (number of new files).
275 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
276 #   (number of changed files + number of removed files) * (number of new files)
277 # - even more costly is '-C', '--find-copies-harder' with cost
278 #   (number of files in the original tree) * (number of new files)
279 # - one might want to include '-B' option, e.g. '-B', '-M'
280 our @diff_opts = ('-M'); # taken from git_commit
282 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
283 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
285 # version of the core git binary
286 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
288 $projects_list ||= $projectroot;
290 # ======================================================================
291 # input validation and dispatch
292 our $action = $cgi->param('a');
293 if (defined $action) {
294         if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
295                 die_error(undef, "Invalid action parameter");
296         }
299 # parameters which are pathnames
300 our $project = $cgi->param('p');
301 if (defined $project) {
302         if (!validate_pathname($project) ||
303             !(-d "$projectroot/$project") ||
304             !check_head_link("$projectroot/$project") ||
305             ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
306             ($strict_export && !project_in_list($project))) {
307                 undef $project;
308                 die_error(undef, "No such project");
309         }
312 our $file_name = $cgi->param('f');
313 if (defined $file_name) {
314         if (!validate_pathname($file_name)) {
315                 die_error(undef, "Invalid file parameter");
316         }
319 our $file_parent = $cgi->param('fp');
320 if (defined $file_parent) {
321         if (!validate_pathname($file_parent)) {
322                 die_error(undef, "Invalid file parent parameter");
323         }
326 # parameters which are refnames
327 our $hash = $cgi->param('h');
328 if (defined $hash) {
329         if (!validate_refname($hash)) {
330                 die_error(undef, "Invalid hash parameter");
331         }
334 our $hash_parent = $cgi->param('hp');
335 if (defined $hash_parent) {
336         if (!validate_refname($hash_parent)) {
337                 die_error(undef, "Invalid hash parent parameter");
338         }
341 our $hash_base = $cgi->param('hb');
342 if (defined $hash_base) {
343         if (!validate_refname($hash_base)) {
344                 die_error(undef, "Invalid hash base parameter");
345         }
348 our $hash_parent_base = $cgi->param('hpb');
349 if (defined $hash_parent_base) {
350         if (!validate_refname($hash_parent_base)) {
351                 die_error(undef, "Invalid hash parent base parameter");
352         }
355 # other parameters
356 our $page = $cgi->param('pg');
357 if (defined $page) {
358         if ($page =~ m/[^0-9]/) {
359                 die_error(undef, "Invalid page parameter");
360         }
363 our $searchtext = $cgi->param('s');
364 if (defined $searchtext) {
365         if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
366                 die_error(undef, "Invalid search parameter");
367         }
368         if (length($searchtext) < 2) {
369                 die_error(undef, "At least two characters are required for search parameter");
370         }
371         $searchtext = quotemeta $searchtext;
374 our $searchtype = $cgi->param('st');
375 if (defined $searchtype) {
376         if ($searchtype =~ m/[^a-z]/) {
377                 die_error(undef, "Invalid searchtype parameter");
378         }
381 # now read PATH_INFO and use it as alternative to parameters
382 sub evaluate_path_info {
383         return if defined $project;
384         my $path_info = $ENV{"PATH_INFO"};
385         return if !$path_info;
386         $path_info =~ s,^/+,,;
387         return if !$path_info;
388         # find which part of PATH_INFO is project
389         $project = $path_info;
390         $project =~ s,/+$,,;
391         while ($project && !check_head_link("$projectroot/$project")) {
392                 $project =~ s,/*[^/]*$,,;
393         }
394         # validate project
395         $project = validate_pathname($project);
396         if (!$project ||
397             ($export_ok && !-e "$projectroot/$project/$export_ok") ||
398             ($strict_export && !project_in_list($project))) {
399                 undef $project;
400                 return;
401         }
402         # do not change any parameters if an action is given using the query string
403         return if $action;
404         $path_info =~ s,^$project/*,,;
405         my ($refname, $pathname) = split(/:/, $path_info, 2);
406         if (defined $pathname) {
407                 # we got "project.git/branch:filename" or "project.git/branch:dir/"
408                 # we could use git_get_type(branch:pathname), but it needs $git_dir
409                 $pathname =~ s,^/+,,;
410                 if (!$pathname || substr($pathname, -1) eq "/") {
411                         $action  ||= "tree";
412                         $pathname =~ s,/$,,;
413                 } else {
414                         $action  ||= "blob_plain";
415                 }
416                 $hash_base ||= validate_refname($refname);
417                 $file_name ||= validate_pathname($pathname);
418         } elsif (defined $refname) {
419                 # we got "project.git/branch"
420                 $action ||= "shortlog";
421                 $hash   ||= validate_refname($refname);
422         }
424 evaluate_path_info();
426 # path to the current git repository
427 our $git_dir;
428 $git_dir = "$projectroot/$project" if $project;
430 # dispatch
431 my %actions = (
432         "blame" => \&git_blame2,
433         "blobdiff" => \&git_blobdiff,
434         "blobdiff_plain" => \&git_blobdiff_plain,
435         "blob" => \&git_blob,
436         "blob_plain" => \&git_blob_plain,
437         "commitdiff" => \&git_commitdiff,
438         "commitdiff_plain" => \&git_commitdiff_plain,
439         "commit" => \&git_commit,
440         "forks" => \&git_forks,
441         "heads" => \&git_heads,
442         "history" => \&git_history,
443         "log" => \&git_log,
444         "rss" => \&git_rss,
445         "atom" => \&git_atom,
446         "search" => \&git_search,
447         "search_help" => \&git_search_help,
448         "shortlog" => \&git_shortlog,
449         "summary" => \&git_summary,
450         "tag" => \&git_tag,
451         "tags" => \&git_tags,
452         "tree" => \&git_tree,
453         "snapshot" => \&git_snapshot,
454         "object" => \&git_object,
455         # those below don't need $project
456         "opml" => \&git_opml,
457         "project_list" => \&git_project_list,
458         "project_index" => \&git_project_index,
459 );
461 if (defined $project) {
462         $action ||= 'summary';
463 } else {
464         $action ||= 'project_list';
466 if (!defined($actions{$action})) {
467         die_error(undef, "Unknown action");
469 if ($action !~ m/^(opml|project_list|project_index)$/ &&
470     !$project) {
471         die_error(undef, "Project needed");
473 $actions{$action}->();
474 exit;
476 ## ======================================================================
477 ## action links
479 sub href(%) {
480         my %params = @_;
481         # default is to use -absolute url() i.e. $my_uri
482         my $href = $params{-full} ? $my_url : $my_uri;
484         # XXX: Warning: If you touch this, check the search form for updating,
485         # too.
487         my @mapping = (
488                 project => "p",
489                 action => "a",
490                 file_name => "f",
491                 file_parent => "fp",
492                 hash => "h",
493                 hash_parent => "hp",
494                 hash_base => "hb",
495                 hash_parent_base => "hpb",
496                 page => "pg",
497                 order => "o",
498                 searchtext => "s",
499                 searchtype => "st",
500         );
501         my %mapping = @mapping;
503         $params{'project'} = $project unless exists $params{'project'};
505         my ($use_pathinfo) = gitweb_check_feature('pathinfo');
506         if ($use_pathinfo) {
507                 # use PATH_INFO for project name
508                 $href .= "/$params{'project'}" if defined $params{'project'};
509                 delete $params{'project'};
511                 # Summary just uses the project path URL
512                 if (defined $params{'action'} && $params{'action'} eq 'summary') {
513                         delete $params{'action'};
514                 }
515         }
517         # now encode the parameters explicitly
518         my @result = ();
519         for (my $i = 0; $i < @mapping; $i += 2) {
520                 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
521                 if (defined $params{$name}) {
522                         push @result, $symbol . "=" . esc_param($params{$name});
523                 }
524         }
525         $href .= "?" . join(';', @result) if scalar @result;
527         return $href;
531 ## ======================================================================
532 ## validation, quoting/unquoting and escaping
534 sub validate_pathname {
535         my $input = shift || return undef;
537         # no '.' or '..' as elements of path, i.e. no '.' nor '..'
538         # at the beginning, at the end, and between slashes.
539         # also this catches doubled slashes
540         if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
541                 return undef;
542         }
543         # no null characters
544         if ($input =~ m!\0!) {
545                 return undef;
546         }
547         return $input;
550 sub validate_refname {
551         my $input = shift || return undef;
553         # textual hashes are O.K.
554         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
555                 return $input;
556         }
557         # it must be correct pathname
558         $input = validate_pathname($input)
559                 or return undef;
560         # restrictions on ref name according to git-check-ref-format
561         if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
562                 return undef;
563         }
564         return $input;
567 # quote unsafe chars, but keep the slash, even when it's not
568 # correct, but quoted slashes look too horrible in bookmarks
569 sub esc_param {
570         my $str = shift;
571         $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
572         $str =~ s/\+/%2B/g;
573         $str =~ s/ /\+/g;
574         return $str;
577 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
578 sub esc_url {
579         my $str = shift;
580         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
581         $str =~ s/\+/%2B/g;
582         $str =~ s/ /\+/g;
583         return $str;
586 # replace invalid utf8 character with SUBSTITUTION sequence
587 sub esc_html ($;%) {
588         my $str = shift;
589         my %opts = @_;
591         $str = decode_utf8($str);
592         $str = $cgi->escapeHTML($str);
593         if ($opts{'-nbsp'}) {
594                 $str =~ s/ /&nbsp;/g;
595         }
596         $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
597         return $str;
600 # quote control characters and escape filename to HTML
601 sub esc_path {
602         my $str = shift;
603         my %opts = @_;
605         $str = decode_utf8($str);
606         $str = $cgi->escapeHTML($str);
607         if ($opts{'-nbsp'}) {
608                 $str =~ s/ /&nbsp;/g;
609         }
610         $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
611         return $str;
614 # Make control characters "printable", using character escape codes (CEC)
615 sub quot_cec {
616         my $cntrl = shift;
617         my %es = ( # character escape codes, aka escape sequences
618                    "\t" => '\t',   # tab            (HT)
619                    "\n" => '\n',   # line feed      (LF)
620                    "\r" => '\r',   # carrige return (CR)
621                    "\f" => '\f',   # form feed      (FF)
622                    "\b" => '\b',   # backspace      (BS)
623                    "\a" => '\a',   # alarm (bell)   (BEL)
624                    "\e" => '\e',   # escape         (ESC)
625                    "\013" => '\v', # vertical tab   (VT)
626                    "\000" => '\0', # nul character  (NUL)
627                    );
628         my $chr = ( (exists $es{$cntrl})
629                     ? $es{$cntrl}
630                     : sprintf('\%03o', ord($cntrl)) );
631         return "<span class=\"cntrl\">$chr</span>";
634 # Alternatively use unicode control pictures codepoints,
635 # Unicode "printable representation" (PR)
636 sub quot_upr {
637         my $cntrl = shift;
638         my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
639         return "<span class=\"cntrl\">$chr</span>";
642 # git may return quoted and escaped filenames
643 sub unquote {
644         my $str = shift;
646         sub unq {
647                 my $seq = shift;
648                 my %es = ( # character escape codes, aka escape sequences
649                         't' => "\t",   # tab            (HT, TAB)
650                         'n' => "\n",   # newline        (NL)
651                         'r' => "\r",   # return         (CR)
652                         'f' => "\f",   # form feed      (FF)
653                         'b' => "\b",   # backspace      (BS)
654                         'a' => "\a",   # alarm (bell)   (BEL)
655                         'e' => "\e",   # escape         (ESC)
656                         'v' => "\013", # vertical tab   (VT)
657                 );
659                 if ($seq =~ m/^[0-7]{1,3}$/) {
660                         # octal char sequence
661                         return chr(oct($seq));
662                 } elsif (exists $es{$seq}) {
663                         # C escape sequence, aka character escape code
664                         return $es{$seq}
665                 }
666                 # quoted ordinary character
667                 return $seq;
668         }
670         if ($str =~ m/^"(.*)"$/) {
671                 # needs unquoting
672                 $str = $1;
673                 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
674         }
675         return $str;
678 # escape tabs (convert tabs to spaces)
679 sub untabify {
680         my $line = shift;
682         while ((my $pos = index($line, "\t")) != -1) {
683                 if (my $count = (8 - ($pos % 8))) {
684                         my $spaces = ' ' x $count;
685                         $line =~ s/\t/$spaces/;
686                 }
687         }
689         return $line;
692 sub project_in_list {
693         my $project = shift;
694         my @list = git_get_projects_list();
695         return @list && scalar(grep { $_->{'path'} eq $project } @list);
698 ## ----------------------------------------------------------------------
699 ## HTML aware string manipulation
701 sub chop_str {
702         my $str = shift;
703         my $len = shift;
704         my $add_len = shift || 10;
706         # allow only $len chars, but don't cut a word if it would fit in $add_len
707         # if it doesn't fit, cut it if it's still longer than the dots we would add
708         $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
709         my $body = $1;
710         my $tail = $2;
711         if (length($tail) > 4) {
712                 $tail = " ...";
713                 $body =~ s/&[^;]*$//; # remove chopped character entities
714         }
715         return "$body$tail";
718 ## ----------------------------------------------------------------------
719 ## functions returning short strings
721 # CSS class for given age value (in seconds)
722 sub age_class {
723         my $age = shift;
725         if ($age < 60*60*2) {
726                 return "age0";
727         } elsif ($age < 60*60*24*2) {
728                 return "age1";
729         } else {
730                 return "age2";
731         }
734 # convert age in seconds to "nn units ago" string
735 sub age_string {
736         my $age = shift;
737         my $age_str;
739         if ($age > 60*60*24*365*2) {
740                 $age_str = (int $age/60/60/24/365);
741                 $age_str .= " years ago";
742         } elsif ($age > 60*60*24*(365/12)*2) {
743                 $age_str = int $age/60/60/24/(365/12);
744                 $age_str .= " months ago";
745         } elsif ($age > 60*60*24*7*2) {
746                 $age_str = int $age/60/60/24/7;
747                 $age_str .= " weeks ago";
748         } elsif ($age > 60*60*24*2) {
749                 $age_str = int $age/60/60/24;
750                 $age_str .= " days ago";
751         } elsif ($age > 60*60*2) {
752                 $age_str = int $age/60/60;
753                 $age_str .= " hours ago";
754         } elsif ($age > 60*2) {
755                 $age_str = int $age/60;
756                 $age_str .= " min ago";
757         } elsif ($age > 2) {
758                 $age_str = int $age;
759                 $age_str .= " sec ago";
760         } else {
761                 $age_str .= " right now";
762         }
763         return $age_str;
766 # convert file mode in octal to symbolic file mode string
767 sub mode_str {
768         my $mode = oct shift;
770         if (S_ISDIR($mode & S_IFMT)) {
771                 return 'drwxr-xr-x';
772         } elsif (S_ISLNK($mode)) {
773                 return 'lrwxrwxrwx';
774         } elsif (S_ISREG($mode)) {
775                 # git cares only about the executable bit
776                 if ($mode & S_IXUSR) {
777                         return '-rwxr-xr-x';
778                 } else {
779                         return '-rw-r--r--';
780                 };
781         } else {
782                 return '----------';
783         }
786 # convert file mode in octal to file type string
787 sub file_type {
788         my $mode = shift;
790         if ($mode !~ m/^[0-7]+$/) {
791                 return $mode;
792         } else {
793                 $mode = oct $mode;
794         }
796         if (S_ISDIR($mode & S_IFMT)) {
797                 return "directory";
798         } elsif (S_ISLNK($mode)) {
799                 return "symlink";
800         } elsif (S_ISREG($mode)) {
801                 return "file";
802         } else {
803                 return "unknown";
804         }
807 # convert file mode in octal to file type description string
808 sub file_type_long {
809         my $mode = shift;
811         if ($mode !~ m/^[0-7]+$/) {
812                 return $mode;
813         } else {
814                 $mode = oct $mode;
815         }
817         if (S_ISDIR($mode & S_IFMT)) {
818                 return "directory";
819         } elsif (S_ISLNK($mode)) {
820                 return "symlink";
821         } elsif (S_ISREG($mode)) {
822                 if ($mode & S_IXUSR) {
823                         return "executable";
824                 } else {
825                         return "file";
826                 };
827         } else {
828                 return "unknown";
829         }
833 ## ----------------------------------------------------------------------
834 ## functions returning short HTML fragments, or transforming HTML fragments
835 ## which don't belong to other sections
837 # format line of commit message.
838 sub format_log_line_html {
839         my $line = shift;
841         $line = esc_html($line, -nbsp=>1);
842         if ($line =~ m/([0-9a-fA-F]{8,40})/) {
843                 my $hash_text = $1;
844                 my $link =
845                         $cgi->a({-href => href(action=>"object", hash=>$hash_text),
846                                 -class => "text"}, $hash_text);
847                 $line =~ s/$hash_text/$link/;
848         }
849         return $line;
852 # format marker of refs pointing to given object
853 sub format_ref_marker {
854         my ($refs, $id) = @_;
855         my $markers = '';
857         if (defined $refs->{$id}) {
858                 foreach my $ref (@{$refs->{$id}}) {
859                         my ($type, $name) = qw();
860                         # e.g. tags/v2.6.11 or heads/next
861                         if ($ref =~ m!^(.*?)s?/(.*)$!) {
862                                 $type = $1;
863                                 $name = $2;
864                         } else {
865                                 $type = "ref";
866                                 $name = $ref;
867                         }
869                         $markers .= " <span class=\"$type\" title=\"$ref\">" .
870                                     esc_html($name) . "</span>";
871                 }
872         }
874         if ($markers) {
875                 return ' <span class="refs">'. $markers . '</span>';
876         } else {
877                 return "";
878         }
881 # format, perhaps shortened and with markers, title line
882 sub format_subject_html {
883         my ($long, $short, $href, $extra) = @_;
884         $extra = '' unless defined($extra);
886         if (length($short) < length($long)) {
887                 return $cgi->a({-href => $href, -class => "list subject",
888                                 -title => decode_utf8($long)},
889                        esc_html($short) . $extra);
890         } else {
891                 return $cgi->a({-href => $href, -class => "list subject"},
892                        esc_html($long)  . $extra);
893         }
896 # format patch (diff) line (rather not to be used for diff headers)
897 sub format_diff_line {
898         my $line = shift;
899         my ($from, $to) = @_;
900         my $diff_class = "";
902         chomp $line;
904         if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
905                 # combined diff
906                 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
907                 if ($line =~ m/^\@{3}/) {
908                         $diff_class = " chunk_header";
909                 } elsif ($line =~ m/^\\/) {
910                         $diff_class = " incomplete";
911                 } elsif ($prefix =~ tr/+/+/) {
912                         $diff_class = " add";
913                 } elsif ($prefix =~ tr/-/-/) {
914                         $diff_class = " rem";
915                 }
916         } else {
917                 # assume ordinary diff
918                 my $char = substr($line, 0, 1);
919                 if ($char eq '+') {
920                         $diff_class = " add";
921                 } elsif ($char eq '-') {
922                         $diff_class = " rem";
923                 } elsif ($char eq '@') {
924                         $diff_class = " chunk_header";
925                 } elsif ($char eq "\\") {
926                         $diff_class = " incomplete";
927                 }
928         }
929         $line = untabify($line);
930         if ($from && $to && $line =~ m/^\@{2} /) {
931                 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
932                         $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
934                 $from_lines = 0 unless defined $from_lines;
935                 $to_lines   = 0 unless defined $to_lines;
937                 if ($from->{'href'}) {
938                         $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
939                                              -class=>"list"}, $from_text);
940                 }
941                 if ($to->{'href'}) {
942                         $to_text   = $cgi->a({-href=>"$to->{'href'}#l$to_start",
943                                              -class=>"list"}, $to_text);
944                 }
945                 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
946                         "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
947                 return "<div class=\"diff$diff_class\">$line</div>\n";
948         } elsif ($from && $to && $line =~ m/^\@{3}/) {
949                 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
950                 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
952                 @from_text = split(' ', $ranges);
953                 for (my $i = 0; $i < @from_text; ++$i) {
954                         ($from_start[$i], $from_nlines[$i]) =
955                                 (split(',', substr($from_text[$i], 1)), 0);
956                 }
958                 $to_text   = pop @from_text;
959                 $to_start  = pop @from_start;
960                 $to_nlines = pop @from_nlines;
962                 $line = "<span class=\"chunk_info\">$prefix ";
963                 for (my $i = 0; $i < @from_text; ++$i) {
964                         if ($from->{'href'}[$i]) {
965                                 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
966                                                   -class=>"list"}, $from_text[$i]);
967                         } else {
968                                 $line .= $from_text[$i];
969                         }
970                         $line .= " ";
971                 }
972                 if ($to->{'href'}) {
973                         $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
974                                           -class=>"list"}, $to_text);
975                 } else {
976                         $line .= $to_text;
977                 }
978                 $line .= " $prefix</span>" .
979                          "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
980                 return "<div class=\"diff$diff_class\">$line</div>\n";
981         }
982         return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
985 ## ----------------------------------------------------------------------
986 ## git utility subroutines, invoking git commands
988 # returns path to the core git executable and the --git-dir parameter as list
989 sub git_cmd {
990         return $GIT, '--git-dir='.$git_dir;
993 # returns path to the core git executable and the --git-dir parameter as string
994 sub git_cmd_str {
995         return join(' ', git_cmd());
998 # get HEAD ref of given project as hash
999 sub git_get_head_hash {
1000         my $project = shift;
1001         my $o_git_dir = $git_dir;
1002         my $retval = undef;
1003         $git_dir = "$projectroot/$project";
1004         if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1005                 my $head = <$fd>;
1006                 close $fd;
1007                 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1008                         $retval = $1;
1009                 }
1010         }
1011         if (defined $o_git_dir) {
1012                 $git_dir = $o_git_dir;
1013         }
1014         return $retval;
1017 # get type of given object
1018 sub git_get_type {
1019         my $hash = shift;
1021         open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1022         my $type = <$fd>;
1023         close $fd or return;
1024         chomp $type;
1025         return $type;
1028 sub git_get_project_config {
1029         my ($key, $type) = @_;
1031         return unless ($key);
1032         $key =~ s/^gitweb\.//;
1033         return if ($key =~ m/\W/);
1035         my @x = (git_cmd(), 'config');
1036         if (defined $type) { push @x, $type; }
1037         push @x, "--get";
1038         push @x, "gitweb.$key";
1039         my $val = qx(@x);
1040         chomp $val;
1041         return ($val);
1044 # get hash of given path at given ref
1045 sub git_get_hash_by_path {
1046         my $base = shift;
1047         my $path = shift || return undef;
1048         my $type = shift;
1050         $path =~ s,/+$,,;
1052         open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1053                 or die_error(undef, "Open git-ls-tree failed");
1054         my $line = <$fd>;
1055         close $fd or return undef;
1057         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
1058         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1059         if (defined $type && $type ne $2) {
1060                 # type doesn't match
1061                 return undef;
1062         }
1063         return $3;
1066 # get path of entry with given hash at given tree-ish (ref)
1067 # used to get 'from' filename for combined diff (merge commit) for renames
1068 sub git_get_path_by_hash {
1069         my $base = shift || return;
1070         my $hash = shift || return;
1072         local $/ = "\0";
1074         open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1075                 or return undef;
1076         while (my $line = <$fd>) {
1077                 chomp $line;
1079                 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423  gitweb'
1080                 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f  gitweb/README'
1081                 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1082                         close $fd;
1083                         return $1;
1084                 }
1085         }
1086         close $fd;
1087         return undef;
1090 ## ......................................................................
1091 ## git utility functions, directly accessing git repository
1093 sub git_get_project_description {
1094         my $path = shift;
1096         open my $fd, "$projectroot/$path/description" or return undef;
1097         my $descr = <$fd>;
1098         close $fd;
1099         chomp $descr;
1100         return $descr;
1103 sub git_get_project_url_list {
1104         my $path = shift;
1106         open my $fd, "$projectroot/$path/cloneurl" or return;
1107         my @git_project_url_list = map { chomp; $_ } <$fd>;
1108         close $fd;
1110         return wantarray ? @git_project_url_list : \@git_project_url_list;
1113 sub git_get_projects_list {
1114         my ($filter) = @_;
1115         my @list;
1117         $filter ||= '';
1118         $filter =~ s/\.git$//;
1120         my ($check_forks) = gitweb_check_feature('forks');
1122         if (-d $projects_list) {
1123                 # search in directory
1124                 my $dir = $projects_list . ($filter ? "/$filter" : '');
1125                 # remove the trailing "/"
1126                 $dir =~ s!/+$!!;
1127                 my $pfxlen = length("$dir");
1129                 File::Find::find({
1130                         follow_fast => 1, # follow symbolic links
1131                         dangling_symlinks => 0, # ignore dangling symlinks, silently
1132                         wanted => sub {
1133                                 # skip project-list toplevel, if we get it.
1134                                 return if (m!^[/.]$!);
1135                                 # only directories can be git repositories
1136                                 return unless (-d $_);
1138                                 my $subdir = substr($File::Find::name, $pfxlen + 1);
1139                                 # we check related file in $projectroot
1140                                 if ($check_forks and $subdir =~ m#/.#) {
1141                                         $File::Find::prune = 1;
1142                                 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1143                                         push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1144                                         $File::Find::prune = 1;
1145                                 }
1146                         },
1147                 }, "$dir");
1149         } elsif (-f $projects_list) {
1150                 # read from file(url-encoded):
1151                 # 'git%2Fgit.git Linus+Torvalds'
1152                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1153                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1154                 my %paths;
1155                 open my ($fd), $projects_list or return;
1156         PROJECT:
1157                 while (my $line = <$fd>) {
1158                         chomp $line;
1159                         my ($path, $owner) = split ' ', $line;
1160                         $path = unescape($path);
1161                         $owner = unescape($owner);
1162                         if (!defined $path) {
1163                                 next;
1164                         }
1165                         if ($filter ne '') {
1166                                 # looking for forks;
1167                                 my $pfx = substr($path, 0, length($filter));
1168                                 if ($pfx ne $filter) {
1169                                         next PROJECT;
1170                                 }
1171                                 my $sfx = substr($path, length($filter));
1172                                 if ($sfx !~ /^\/.*\.git$/) {
1173                                         next PROJECT;
1174                                 }
1175                         } elsif ($check_forks) {
1176                         PATH:
1177                                 foreach my $filter (keys %paths) {
1178                                         # looking for forks;
1179                                         my $pfx = substr($path, 0, length($filter));
1180                                         if ($pfx ne $filter) {
1181                                                 next PATH;
1182                                         }
1183                                         my $sfx = substr($path, length($filter));
1184                                         if ($sfx !~ /^\/.*\.git$/) {
1185                                                 next PATH;
1186                                         }
1187                                         # is a fork, don't include it in
1188                                         # the list
1189                                         next PROJECT;
1190                                 }
1191                         }
1192                         if (check_export_ok("$projectroot/$path")) {
1193                                 my $pr = {
1194                                         path => $path,
1195                                         owner => decode_utf8($owner),
1196                                 };
1197                                 push @list, $pr;
1198                                 (my $forks_path = $path) =~ s/\.git$//;
1199                                 $paths{$forks_path}++;
1200                         }
1201                 }
1202                 close $fd;
1203         }
1204         return @list;
1207 sub git_get_project_owner {
1208         my $project = shift;
1209         my $owner;
1211         return undef unless $project;
1213         # read from file (url-encoded):
1214         # 'git%2Fgit.git Linus+Torvalds'
1215         # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1216         # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1217         if (-f $projects_list) {
1218                 open (my $fd , $projects_list);
1219                 while (my $line = <$fd>) {
1220                         chomp $line;
1221                         my ($pr, $ow) = split ' ', $line;
1222                         $pr = unescape($pr);
1223                         $ow = unescape($ow);
1224                         if ($pr eq $project) {
1225                                 $owner = decode_utf8($ow);
1226                                 last;
1227                         }
1228                 }
1229                 close $fd;
1230         }
1231         if (!defined $owner) {
1232                 $owner = get_file_owner("$projectroot/$project");
1233         }
1235         return $owner;
1238 sub git_get_last_activity {
1239         my ($path) = @_;
1240         my $fd;
1242         $git_dir = "$projectroot/$path";
1243         open($fd, "-|", git_cmd(), 'for-each-ref',
1244              '--format=%(committer)',
1245              '--sort=-committerdate',
1246              '--count=1',
1247              'refs/heads') or return;
1248         my $most_recent = <$fd>;
1249         close $fd or return;
1250         if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1251                 my $timestamp = $1;
1252                 my $age = time - $timestamp;
1253                 return ($age, age_string($age));
1254         }
1257 sub git_get_references {
1258         my $type = shift || "";
1259         my %refs;
1260         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1261         # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1262         open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1263                 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1264                 or return;
1266         while (my $line = <$fd>) {
1267                 chomp $line;
1268                 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1269                         if (defined $refs{$1}) {
1270                                 push @{$refs{$1}}, $2;
1271                         } else {
1272                                 $refs{$1} = [ $2 ];
1273                         }
1274                 }
1275         }
1276         close $fd or return;
1277         return \%refs;
1280 sub git_get_rev_name_tags {
1281         my $hash = shift || return undef;
1283         open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1284                 or return;
1285         my $name_rev = <$fd>;
1286         close $fd;
1288         if ($name_rev =~ m|^$hash tags/(.*)$|) {
1289                 return $1;
1290         } else {
1291                 # catches also '$hash undefined' output
1292                 return undef;
1293         }
1296 ## ----------------------------------------------------------------------
1297 ## parse to hash functions
1299 sub parse_date {
1300         my $epoch = shift;
1301         my $tz = shift || "-0000";
1303         my %date;
1304         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1305         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1306         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1307         $date{'hour'} = $hour;
1308         $date{'minute'} = $min;
1309         $date{'mday'} = $mday;
1310         $date{'day'} = $days[$wday];
1311         $date{'month'} = $months[$mon];
1312         $date{'rfc2822'}   = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1313                              $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1314         $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1315                              $mday, $months[$mon], $hour ,$min;
1316         $date{'iso-8601'}  = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1317                              1900+$year, $mon, $mday, $hour ,$min, $sec;
1319         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1320         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1321         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1322         $date{'hour_local'} = $hour;
1323         $date{'minute_local'} = $min;
1324         $date{'tz_local'} = $tz;
1325         $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1326                                   1900+$year, $mon+1, $mday,
1327                                   $hour, $min, $sec, $tz);
1328         return %date;
1331 sub parse_tag {
1332         my $tag_id = shift;
1333         my %tag;
1334         my @comment;
1336         open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1337         $tag{'id'} = $tag_id;
1338         while (my $line = <$fd>) {
1339                 chomp $line;
1340                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1341                         $tag{'object'} = $1;
1342                 } elsif ($line =~ m/^type (.+)$/) {
1343                         $tag{'type'} = $1;
1344                 } elsif ($line =~ m/^tag (.+)$/) {
1345                         $tag{'name'} = $1;
1346                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1347                         $tag{'author'} = $1;
1348                         $tag{'epoch'} = $2;
1349                         $tag{'tz'} = $3;
1350                 } elsif ($line =~ m/--BEGIN/) {
1351                         push @comment, $line;
1352                         last;
1353                 } elsif ($line eq "") {
1354                         last;
1355                 }
1356         }
1357         push @comment, <$fd>;
1358         $tag{'comment'} = \@comment;
1359         close $fd or return;
1360         if (!defined $tag{'name'}) {
1361                 return
1362         };
1363         return %tag
1366 sub parse_commit_text {
1367         my ($commit_text, $withparents) = @_;
1368         my @commit_lines = split '\n', $commit_text;
1369         my %co;
1371         pop @commit_lines; # Remove '\0'
1373         my $header = shift @commit_lines;
1374         if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1375                 return;
1376         }
1377         ($co{'id'}, my @parents) = split ' ', $header;
1378         while (my $line = shift @commit_lines) {
1379                 last if $line eq "\n";
1380                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
1381                         $co{'tree'} = $1;
1382                 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
1383                         push @parents, $1;
1384                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
1385                         $co{'author'} = $1;
1386                         $co{'author_epoch'} = $2;
1387                         $co{'author_tz'} = $3;
1388                         if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1389                                 $co{'author_name'}  = $1;
1390                                 $co{'author_email'} = $2;
1391                         } else {
1392                                 $co{'author_name'} = $co{'author'};
1393                         }
1394                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1395                         $co{'committer'} = $1;
1396                         $co{'committer_epoch'} = $2;
1397                         $co{'committer_tz'} = $3;
1398                         $co{'committer_name'} = $co{'committer'};
1399                         if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1400                                 $co{'committer_name'}  = $1;
1401                                 $co{'committer_email'} = $2;
1402                         } else {
1403                                 $co{'committer_name'} = $co{'committer'};
1404                         }
1405                 }
1406         }
1407         if (!defined $co{'tree'}) {
1408                 return;
1409         };
1410         $co{'parents'} = \@parents;
1411         $co{'parent'} = $parents[0];
1413         foreach my $title (@commit_lines) {
1414                 $title =~ s/^    //;
1415                 if ($title ne "") {
1416                         $co{'title'} = chop_str($title, 80, 5);
1417                         # remove leading stuff of merges to make the interesting part visible
1418                         if (length($title) > 50) {
1419                                 $title =~ s/^Automatic //;
1420                                 $title =~ s/^merge (of|with) /Merge ... /i;
1421                                 if (length($title) > 50) {
1422                                         $title =~ s/(http|rsync):\/\///;
1423                                 }
1424                                 if (length($title) > 50) {
1425                                         $title =~ s/(master|www|rsync)\.//;
1426                                 }
1427                                 if (length($title) > 50) {
1428                                         $title =~ s/kernel.org:?//;
1429                                 }
1430                                 if (length($title) > 50) {
1431                                         $title =~ s/\/pub\/scm//;
1432                                 }
1433                         }
1434                         $co{'title_short'} = chop_str($title, 50, 5);
1435                         last;
1436                 }
1437         }
1438         if ($co{'title'} eq "") {
1439                 $co{'title'} = $co{'title_short'} = '(no commit message)';
1440         }
1441         # remove added spaces
1442         foreach my $line (@commit_lines) {
1443                 $line =~ s/^    //;
1444         }
1445         $co{'comment'} = \@commit_lines;
1447         my $age = time - $co{'committer_epoch'};
1448         $co{'age'} = $age;
1449         $co{'age_string'} = age_string($age);
1450         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1451         if ($age > 60*60*24*7*2) {
1452                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1453                 $co{'age_string_age'} = $co{'age_string'};
1454         } else {
1455                 $co{'age_string_date'} = $co{'age_string'};
1456                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
1457         }
1458         return %co;
1461 sub parse_commit {
1462         my ($commit_id) = @_;
1463         my %co;
1465         local $/ = "\0";
1467         open my $fd, "-|", git_cmd(), "rev-list",
1468                 "--parents",
1469                 "--header",
1470                 "--max-count=1",
1471                 $commit_id,
1472                 "--",
1473                 or die_error(undef, "Open git-rev-list failed");
1474         %co = parse_commit_text(<$fd>, 1);
1475         close $fd;
1477         return %co;
1480 sub parse_commits {
1481         my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1482         my @cos;
1484         $maxcount ||= 1;
1485         $skip ||= 0;
1487         local $/ = "\0";
1489         open my $fd, "-|", git_cmd(), "rev-list",
1490                 "--header",
1491                 ($arg ? ($arg) : ()),
1492                 ("--max-count=" . $maxcount),
1493                 ("--skip=" . $skip),
1494                 $commit_id,
1495                 "--",
1496                 ($filename ? ($filename) : ())
1497                 or die_error(undef, "Open git-rev-list failed");
1498         while (my $line = <$fd>) {
1499                 my %co = parse_commit_text($line);
1500                 push @cos, \%co;
1501         }
1502         close $fd;
1504         return wantarray ? @cos : \@cos;
1507 # parse ref from ref_file, given by ref_id, with given type
1508 sub parse_ref {
1509         my $ref_file = shift;
1510         my $ref_id = shift;
1511         my $type = shift || git_get_type($ref_id);
1512         my %ref_item;
1514         $ref_item{'type'} = $type;
1515         $ref_item{'id'} = $ref_id;
1516         $ref_item{'epoch'} = 0;
1517         $ref_item{'age'} = "unknown";
1518         if ($type eq "tag") {
1519                 my %tag = parse_tag($ref_id);
1520                 $ref_item{'comment'} = $tag{'comment'};
1521                 if ($tag{'type'} eq "commit") {
1522                         my %co = parse_commit($tag{'object'});
1523                         $ref_item{'epoch'} = $co{'committer_epoch'};
1524                         $ref_item{'age'} = $co{'age_string'};
1525                 } elsif (defined($tag{'epoch'})) {
1526                         my $age = time - $tag{'epoch'};
1527                         $ref_item{'epoch'} = $tag{'epoch'};
1528                         $ref_item{'age'} = age_string($age);
1529                 }
1530                 $ref_item{'reftype'} = $tag{'type'};
1531                 $ref_item{'name'} = $tag{'name'};
1532                 $ref_item{'refid'} = $tag{'object'};
1533         } elsif ($type eq "commit"){
1534                 my %co = parse_commit($ref_id);
1535                 $ref_item{'reftype'} = "commit";
1536                 $ref_item{'name'} = $ref_file;
1537                 $ref_item{'title'} = $co{'title'};
1538                 $ref_item{'refid'} = $ref_id;
1539                 $ref_item{'epoch'} = $co{'committer_epoch'};
1540                 $ref_item{'age'} = $co{'age_string'};
1541         } else {
1542                 $ref_item{'reftype'} = $type;
1543                 $ref_item{'name'} = $ref_file;
1544                 $ref_item{'refid'} = $ref_id;
1545         }
1547         return %ref_item;
1550 # parse line of git-diff-tree "raw" output
1551 sub parse_difftree_raw_line {
1552         my $line = shift;
1553         my %res;
1555         # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
1556         # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
1557         if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1558                 $res{'from_mode'} = $1;
1559                 $res{'to_mode'} = $2;
1560                 $res{'from_id'} = $3;
1561                 $res{'to_id'} = $4;
1562                 $res{'status'} = $5;
1563                 $res{'similarity'} = $6;
1564                 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1565                         ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1566                 } else {
1567                         $res{'file'} = unquote($7);
1568                 }
1569         }
1570         # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
1571         # combined diff (for merge commit)
1572         elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
1573                 $res{'nparents'}  = length($1);
1574                 $res{'from_mode'} = [ split(' ', $2) ];
1575                 $res{'to_mode'} = pop @{$res{'from_mode'}};
1576                 $res{'from_id'} = [ split(' ', $3) ];
1577                 $res{'to_id'} = pop @{$res{'from_id'}};
1578                 $res{'status'} = [ split('', $4) ];
1579                 $res{'to_file'} = unquote($5);
1580         }
1581         # 'c512b523472485aef4fff9e57b229d9d243c967f'
1582         elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1583                 $res{'commit'} = $1;
1584         }
1586         return wantarray ? %res : \%res;
1589 # parse line of git-ls-tree output
1590 sub parse_ls_tree_line ($;%) {
1591         my $line = shift;
1592         my %opts = @_;
1593         my %res;
1595         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
1596         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
1598         $res{'mode'} = $1;
1599         $res{'type'} = $2;
1600         $res{'hash'} = $3;
1601         if ($opts{'-z'}) {
1602                 $res{'name'} = $4;
1603         } else {
1604                 $res{'name'} = unquote($4);
1605         }
1607         return wantarray ? %res : \%res;
1610 ## ......................................................................
1611 ## parse to array of hashes functions
1613 sub git_get_heads_list {
1614         my $limit = shift;
1615         my @headslist;
1617         open my $fd, '-|', git_cmd(), 'for-each-ref',
1618                 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1619                 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1620                 'refs/heads'
1621                 or return;
1622         while (my $line = <$fd>) {
1623                 my %ref_item;
1625                 chomp $line;
1626                 my ($refinfo, $committerinfo) = split(/\0/, $line);
1627                 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1628                 my ($committer, $epoch, $tz) =
1629                         ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1630                 $name =~ s!^refs/heads/!!;
1632                 $ref_item{'name'}  = $name;
1633                 $ref_item{'id'}    = $hash;
1634                 $ref_item{'title'} = $title || '(no commit message)';
1635                 $ref_item{'epoch'} = $epoch;
1636                 if ($epoch) {
1637                         $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1638                 } else {
1639                         $ref_item{'age'} = "unknown";
1640                 }
1642                 push @headslist, \%ref_item;
1643         }
1644         close $fd;
1646         return wantarray ? @headslist : \@headslist;
1649 sub git_get_tags_list {
1650         my $limit = shift;
1651         my @tagslist;
1653         open my $fd, '-|', git_cmd(), 'for-each-ref',
1654                 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1655                 '--format=%(objectname) %(objecttype) %(refname) '.
1656                 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1657                 'refs/tags'
1658                 or return;
1659         while (my $line = <$fd>) {
1660                 my %ref_item;
1662                 chomp $line;
1663                 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1664                 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1665                 my ($creator, $epoch, $tz) =
1666                         ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1667                 $name =~ s!^refs/tags/!!;
1669                 $ref_item{'type'} = $type;
1670                 $ref_item{'id'} = $id;
1671                 $ref_item{'name'} = $name;
1672                 if ($type eq "tag") {
1673                         $ref_item{'subject'} = $title;
1674                         $ref_item{'reftype'} = $reftype;
1675                         $ref_item{'refid'}   = $refid;
1676                 } else {
1677                         $ref_item{'reftype'} = $type;
1678                         $ref_item{'refid'}   = $id;
1679                 }
1681                 if ($type eq "tag" || $type eq "commit") {
1682                         $ref_item{'epoch'} = $epoch;
1683                         if ($epoch) {
1684                                 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1685                         } else {
1686                                 $ref_item{'age'} = "unknown";
1687                         }
1688                 }
1690                 push @tagslist, \%ref_item;
1691         }
1692         close $fd;
1694         return wantarray ? @tagslist : \@tagslist;
1697 ## ----------------------------------------------------------------------
1698 ## filesystem-related functions
1700 sub get_file_owner {
1701         my $path = shift;
1703         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1704         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1705         if (!defined $gcos) {
1706                 return undef;
1707         }
1708         my $owner = $gcos;
1709         $owner =~ s/[,;].*$//;
1710         return decode_utf8($owner);
1713 ## ......................................................................
1714 ## mimetype related functions
1716 sub mimetype_guess_file {
1717         my $filename = shift;
1718         my $mimemap = shift;
1719         -r $mimemap or return undef;
1721         my %mimemap;
1722         open(MIME, $mimemap) or return undef;
1723         while (<MIME>) {
1724                 next if m/^#/; # skip comments
1725                 my ($mime, $exts) = split(/\t+/);
1726                 if (defined $exts) {
1727                         my @exts = split(/\s+/, $exts);
1728                         foreach my $ext (@exts) {
1729                                 $mimemap{$ext} = $mime;
1730                         }
1731                 }
1732         }
1733         close(MIME);
1735         $filename =~ /\.([^.]*)$/;
1736         return $mimemap{$1};
1739 sub mimetype_guess {
1740         my $filename = shift;
1741         my $mime;
1742         $filename =~ /\./ or return undef;
1744         if ($mimetypes_file) {
1745                 my $file = $mimetypes_file;
1746                 if ($file !~ m!^/!) { # if it is relative path
1747                         # it is relative to project
1748                         $file = "$projectroot/$project/$file";
1749                 }
1750                 $mime = mimetype_guess_file($filename, $file);
1751         }
1752         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1753         return $mime;
1756 sub blob_mimetype {
1757         my $fd = shift;
1758         my $filename = shift;
1760         if ($filename) {
1761                 my $mime = mimetype_guess($filename);
1762                 $mime and return $mime;
1763         }
1765         # just in case
1766         return $default_blob_plain_mimetype unless $fd;
1768         if (-T $fd) {
1769                 return 'text/plain' .
1770                        ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1771         } elsif (! $filename) {
1772                 return 'application/octet-stream';
1773         } elsif ($filename =~ m/\.png$/i) {
1774                 return 'image/png';
1775         } elsif ($filename =~ m/\.gif$/i) {
1776                 return 'image/gif';
1777         } elsif ($filename =~ m/\.jpe?g$/i) {
1778                 return 'image/jpeg';
1779         } else {
1780                 return 'application/octet-stream';
1781         }
1784 ## ======================================================================
1785 ## functions printing HTML: header, footer, error page
1787 sub git_header_html {
1788         my $status = shift || "200 OK";
1789         my $expires = shift;
1791         my $title = "$site_name";
1792         if (defined $project) {
1793                 $title .= " - " . decode_utf8($project);
1794                 if (defined $action) {
1795                         $title .= "/$action";
1796                         if (defined $file_name) {
1797                                 $title .= " - " . esc_path($file_name);
1798                                 if ($action eq "tree" && $file_name !~ m|/$|) {
1799                                         $title .= "/";
1800                                 }
1801                         }
1802                 }
1803         }
1804         my $content_type;
1805         # require explicit support from the UA if we are to send the page as
1806         # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1807         # we have to do this because MSIE sometimes globs '*/*', pretending to
1808         # support xhtml+xml but choking when it gets what it asked for.
1809         if (defined $cgi->http('HTTP_ACCEPT') &&
1810             $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1811             $cgi->Accept('application/xhtml+xml') != 0) {
1812                 $content_type = 'application/xhtml+xml';
1813         } else {
1814                 $content_type = 'text/html';
1815         }
1816         print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1817                            -status=> $status, -expires => $expires);
1818         my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
1819         print <<EOF;
1820 <?xml version="1.0" encoding="utf-8"?>
1821 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1822 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1823 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1824 <!-- git core binaries version $git_version -->
1825 <head>
1826 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1827 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
1828 <meta name="robots" content="index, nofollow"/>
1829 <title>$title</title>
1830 EOF
1831 # print out each stylesheet that exist
1832         if (defined $stylesheet) {
1833 #provides backwards capability for those people who define style sheet in a config file
1834                 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1835         } else {
1836                 foreach my $stylesheet (@stylesheets) {
1837                         next unless $stylesheet;
1838                         print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1839                 }
1840         }
1841         if (defined $project) {
1842                 printf('<link rel="alternate" title="%s log RSS feed" '.
1843                        'href="%s" type="application/rss+xml" />'."\n",
1844                        esc_param($project), href(action=>"rss"));
1845                 printf('<link rel="alternate" title="%s log Atom feed" '.
1846                        'href="%s" type="application/atom+xml" />'."\n",
1847                        esc_param($project), href(action=>"atom"));
1848         } else {
1849                 printf('<link rel="alternate" title="%s projects list" '.
1850                        'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1851                        $site_name, href(project=>undef, action=>"project_index"));
1852                 printf('<link rel="alternate" title="%s projects feeds" '.
1853                        'href="%s" type="text/x-opml"/>'."\n",
1854                        $site_name, href(project=>undef, action=>"opml"));
1855         }
1856         if (defined $favicon) {
1857                 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1858         }
1860         print "</head>\n" .
1861               "<body>\n";
1863         if (-f $site_header) {
1864                 open (my $fd, $site_header);
1865                 print <$fd>;
1866                 close $fd;
1867         }
1869         print "<div class=\"page_header\">\n" .
1870               $cgi->a({-href => esc_url($logo_url),
1871                        -title => $logo_label},
1872                       qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
1873         print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
1874         if (defined $project) {
1875                 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1876                 if (defined $action) {
1877                         print " / $action";
1878                 }
1879                 print "\n";
1880         }
1881         my ($have_search) = gitweb_check_feature('search');
1882         if ((defined $project) && ($have_search)) {
1883                 if (!defined $searchtext) {
1884                         $searchtext = "";
1885                 }
1886                 my $search_hash;
1887                 if (defined $hash_base) {
1888                         $search_hash = $hash_base;
1889                 } elsif (defined $hash) {
1890                         $search_hash = $hash;
1891                 } else {
1892                         $search_hash = "HEAD";
1893                 }
1894                 $cgi->param("a", "search");
1895                 $cgi->param("h", $search_hash);
1896                 $cgi->param("p", $project);
1897                 print $cgi->startform(-method => "get", -action => $my_uri) .
1898                       "<div class=\"search\">\n" .
1899                       $cgi->hidden(-name => "p") . "\n" .
1900                       $cgi->hidden(-name => "a") . "\n" .
1901                       $cgi->hidden(-name => "h") . "\n" .
1902                       $cgi->popup_menu(-name => 'st', -default => 'commit',
1903                                        -values => ['commit', 'author', 'committer', 'pickaxe']) .
1904                       $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1905                       " search:\n",
1906                       $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1907                       "</div>" .
1908                       $cgi->end_form() . "\n";
1909         }
1910         print "</div>\n";
1913 sub git_footer_html {
1914         print "<div class=\"page_footer\">\n";
1915         if (defined $project) {
1916                 my $descr = git_get_project_description($project);
1917                 if (defined $descr) {
1918                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1919                 }
1920                 print $cgi->a({-href => href(action=>"rss"),
1921                               -class => "rss_logo"}, "RSS") . " ";
1922                 print $cgi->a({-href => href(action=>"atom"),
1923                               -class => "rss_logo"}, "Atom") . "\n";
1924         } else {
1925                 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1926                               -class => "rss_logo"}, "OPML") . " ";
1927                 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1928                               -class => "rss_logo"}, "TXT") . "\n";
1929         }
1930         print "</div>\n" ;
1932         if (-f $site_footer) {
1933                 open (my $fd, $site_footer);
1934                 print <$fd>;
1935                 close $fd;
1936         }
1938         print "</body>\n" .
1939               "</html>";
1942 sub die_error {
1943         my $status = shift || "403 Forbidden";
1944         my $error = shift || "Malformed query, file missing or permission denied";
1946         git_header_html($status);
1947         print <<EOF;
1948 <div class="page_body">
1949 <br /><br />
1950 $status - $error
1951 <br />
1952 </div>
1953 EOF
1954         git_footer_html();
1955         exit;
1958 ## ----------------------------------------------------------------------
1959 ## functions printing or outputting HTML: navigation
1961 sub git_print_page_nav {
1962         my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1963         $extra = '' if !defined $extra; # pager or formats
1965         my @navs = qw(summary shortlog log commit commitdiff tree);
1966         if ($suppress) {
1967                 @navs = grep { $_ ne $suppress } @navs;
1968         }
1970         my %arg = map { $_ => {action=>$_} } @navs;
1971         if (defined $head) {
1972                 for (qw(commit commitdiff)) {
1973                         $arg{$_}{'hash'} = $head;
1974                 }
1975                 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1976                         for (qw(shortlog log)) {
1977                                 $arg{$_}{'hash'} = $head;
1978                         }
1979                 }
1980         }
1981         $arg{'tree'}{'hash'} = $treehead if defined $treehead;
1982         $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
1984         print "<div class=\"page_nav\">\n" .
1985                 (join " | ",
1986                  map { $_ eq $current ?
1987                        $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1988                  } @navs);
1989         print "<br/>\n$extra<br/>\n" .
1990               "</div>\n";
1993 sub format_paging_nav {
1994         my ($action, $hash, $head, $page, $nrevs) = @_;
1995         my $paging_nav;
1998         if ($hash ne $head || $page) {
1999                 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2000         } else {
2001                 $paging_nav .= "HEAD";
2002         }
2004         if ($page > 0) {
2005                 $paging_nav .= " &sdot; " .
2006                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
2007                                  -accesskey => "p", -title => "Alt-p"}, "prev");
2008         } else {
2009                 $paging_nav .= " &sdot; prev";
2010         }
2012         if ($nrevs >= (100 * ($page+1)-1)) {
2013                 $paging_nav .= " &sdot; " .
2014                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
2015                                  -accesskey => "n", -title => "Alt-n"}, "next");
2016         } else {
2017                 $paging_nav .= " &sdot; next";
2018         }
2020         return $paging_nav;
2023 ## ......................................................................
2024 ## functions printing or outputting HTML: div
2026 sub git_print_header_div {
2027         my ($action, $title, $hash, $hash_base) = @_;
2028         my %args = ();
2030         $args{'action'} = $action;
2031         $args{'hash'} = $hash if $hash;
2032         $args{'hash_base'} = $hash_base if $hash_base;
2034         print "<div class=\"header\">\n" .
2035               $cgi->a({-href => href(%args), -class => "title"},
2036               $title ? $title : $action) .
2037               "\n</div>\n";
2040 #sub git_print_authorship (\%) {
2041 sub git_print_authorship {
2042         my $co = shift;
2044         my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2045         print "<div class=\"author_date\">" .
2046               esc_html($co->{'author_name'}) .
2047               " [$ad{'rfc2822'}";
2048         if ($ad{'hour_local'} < 6) {
2049                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2050                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2051         } else {
2052                 printf(" (%02d:%02d %s)",
2053                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2054         }
2055         print "]</div>\n";
2058 sub git_print_page_path {
2059         my $name = shift;
2060         my $type = shift;
2061         my $hb = shift;
2064         print "<div class=\"page_path\">";
2065         print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2066                       -title => 'tree root'}, decode_utf8("[$project]"));
2067         print " / ";
2068         if (defined $name) {
2069                 my @dirname = split '/', $name;
2070                 my $basename = pop @dirname;
2071                 my $fullname = '';
2073                 foreach my $dir (@dirname) {
2074                         $fullname .= ($fullname ? '/' : '') . $dir;
2075                         print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2076                                                      hash_base=>$hb),
2077                                       -title => $fullname}, esc_path($dir));
2078                         print " / ";
2079                 }
2080                 if (defined $type && $type eq 'blob') {
2081                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2082                                                      hash_base=>$hb),
2083                                       -title => $name}, esc_path($basename));
2084                 } elsif (defined $type && $type eq 'tree') {
2085                         print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2086                                                      hash_base=>$hb),
2087                                       -title => $name}, esc_path($basename));
2088                         print " / ";
2089                 } else {
2090                         print esc_path($basename);
2091                 }
2092         }
2093         print "<br/></div>\n";
2096 # sub git_print_log (\@;%) {
2097 sub git_print_log ($;%) {
2098         my $log = shift;
2099         my %opts = @_;
2101         if ($opts{'-remove_title'}) {
2102                 # remove title, i.e. first line of log
2103                 shift @$log;
2104         }
2105         # remove leading empty lines
2106         while (defined $log->[0] && $log->[0] eq "") {
2107                 shift @$log;
2108         }
2110         # print log
2111         my $signoff = 0;
2112         my $empty = 0;
2113         foreach my $line (@$log) {
2114                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2115                         $signoff = 1;
2116                         $empty = 0;
2117                         if (! $opts{'-remove_signoff'}) {
2118                                 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2119                                 next;
2120                         } else {
2121                                 # remove signoff lines
2122                                 next;
2123                         }
2124                 } else {
2125                         $signoff = 0;
2126                 }
2128                 # print only one empty line
2129                 # do not print empty line after signoff
2130                 if ($line eq "") {
2131                         next if ($empty || $signoff);
2132                         $empty = 1;
2133                 } else {
2134                         $empty = 0;
2135                 }
2137                 print format_log_line_html($line) . "<br/>\n";
2138         }
2140         if ($opts{'-final_empty_line'}) {
2141                 # end with single empty line
2142                 print "<br/>\n" unless $empty;
2143         }
2146 # return link target (what link points to)
2147 sub git_get_link_target {
2148         my $hash = shift;
2149         my $link_target;
2151         # read link
2152         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2153                 or return;
2154         {
2155                 local $/;
2156                 $link_target = <$fd>;
2157         }
2158         close $fd
2159                 or return;
2161         return $link_target;
2164 # given link target, and the directory (basedir) the link is in,
2165 # return target of link relative to top directory (top tree);
2166 # return undef if it is not possible (including absolute links).
2167 sub normalize_link_target {
2168         my ($link_target, $basedir, $hash_base) = @_;
2170         # we can normalize symlink target only if $hash_base is provided
2171         return unless $hash_base;
2173         # absolute symlinks (beginning with '/') cannot be normalized
2174         return if (substr($link_target, 0, 1) eq '/');
2176         # normalize link target to path from top (root) tree (dir)
2177         my $path;
2178         if ($basedir) {
2179                 $path = $basedir . '/' . $link_target;
2180         } else {
2181                 # we are in top (root) tree (dir)
2182                 $path = $link_target;
2183         }
2185         # remove //, /./, and /../
2186         my @path_parts;
2187         foreach my $part (split('/', $path)) {
2188                 # discard '.' and ''
2189                 next if (!$part || $part eq '.');
2190                 # handle '..'
2191                 if ($part eq '..') {
2192                         if (@path_parts) {
2193                                 pop @path_parts;
2194                         } else {
2195                                 # link leads outside repository (outside top dir)
2196                                 return;
2197                         }
2198                 } else {
2199                         push @path_parts, $part;
2200                 }
2201         }
2202         $path = join('/', @path_parts);
2204         return $path;
2207 # print tree entry (row of git_tree), but without encompassing <tr> element
2208 sub git_print_tree_entry {
2209         my ($t, $basedir, $hash_base, $have_blame) = @_;
2211         my %base_key = ();
2212         $base_key{'hash_base'} = $hash_base if defined $hash_base;
2214         # The format of a table row is: mode list link.  Where mode is
2215         # the mode of the entry, list is the name of the entry, an href,
2216         # and link is the action links of the entry.
2218         print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2219         if ($t->{'type'} eq "blob") {
2220                 print "<td class=\"list\">" .
2221                         $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2222                                                file_name=>"$basedir$t->{'name'}", %base_key),
2223                                 -class => "list"}, esc_path($t->{'name'}));
2224                 if (S_ISLNK(oct $t->{'mode'})) {
2225                         my $link_target = git_get_link_target($t->{'hash'});
2226                         if ($link_target) {
2227                                 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2228                                 if (defined $norm_target) {
2229                                         print " -> " .
2230                                               $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2231                                                                      file_name=>$norm_target),
2232                                                        -title => $norm_target}, esc_path($link_target));
2233                                 } else {
2234                                         print " -> " . esc_path($link_target);
2235                                 }
2236                         }
2237                 }
2238                 print "</td>\n";
2239                 print "<td class=\"link\">";
2240                 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2241                                              file_name=>"$basedir$t->{'name'}", %base_key)},
2242                               "blob");
2243                 if ($have_blame) {
2244                         print " | " .
2245                               $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2246                                                      file_name=>"$basedir$t->{'name'}", %base_key)},
2247                                       "blame");
2248                 }
2249                 if (defined $hash_base) {
2250                         print " | " .
2251                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2252                                                      hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2253                                       "history");
2254                 }
2255                 print " | " .
2256                         $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2257                                                file_name=>"$basedir$t->{'name'}")},
2258                                 "raw");
2259                 print "</td>\n";
2261         } elsif ($t->{'type'} eq "tree") {
2262                 print "<td class=\"list\">";
2263                 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2264                                              file_name=>"$basedir$t->{'name'}", %base_key)},
2265                               esc_path($t->{'name'}));
2266                 print "</td>\n";
2267                 print "<td class=\"link\">";
2268                 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2269                                              file_name=>"$basedir$t->{'name'}", %base_key)},
2270                               "tree");
2271                 if (defined $hash_base) {
2272                         print " | " .
2273                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2274                                                      file_name=>"$basedir$t->{'name'}")},
2275                                       "history");
2276                 }
2277                 print "</td>\n";
2278         }
2281 ## ......................................................................
2282 ## functions printing large fragments of HTML
2284 sub fill_from_file_info {
2285         my ($diff, @parents) = @_;
2287         $diff->{'from_file'} = [ ];
2288         $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
2289         for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2290                 if ($diff->{'status'}[$i] eq 'R' ||
2291                     $diff->{'status'}[$i] eq 'C') {
2292                         $diff->{'from_file'}[$i] =
2293                                 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
2294                 }
2295         }
2297         return $diff;
2300 # parameters can be strings, or references to arrays of strings
2301 sub from_ids_eq {
2302         my ($a, $b) = @_;
2304         if (ref($a) eq "ARRAY" && ref($b) eq "ARRAY" && @$a == @$b) {
2305                 for (my $i = 0; $i < @$a; ++$i) {
2306                         return 0 unless ($a->[$i] eq $b->[$i]);
2307                 }
2308                 return 1;
2309         } elsif (!ref($a) && !ref($b)) {
2310                 return $a eq $b;
2311         } else {
2312                 return 0;
2313         }
2317 sub git_difftree_body {
2318         my ($difftree, $hash, @parents) = @_;
2319         my ($parent) = $parents[0];
2320         my ($have_blame) = gitweb_check_feature('blame');
2321         print "<div class=\"list_head\">\n";
2322         if ($#{$difftree} > 10) {
2323                 print(($#{$difftree} + 1) . " files changed:\n");
2324         }
2325         print "</div>\n";
2327         print "<table class=\"" .
2328               (@parents > 1 ? "combined " : "") .
2329               "diff_tree\">\n";
2330         my $alternate = 1;
2331         my $patchno = 0;
2332         foreach my $line (@{$difftree}) {
2333                 my $diff;
2334                 if (ref($line) eq "HASH") {
2335                         # pre-parsed (or generated by hand)
2336                         $diff = $line;
2337                 } else {
2338                         $diff = parse_difftree_raw_line($line);
2339                 }
2341                 if ($alternate) {
2342                         print "<tr class=\"dark\">\n";
2343                 } else {
2344                         print "<tr class=\"light\">\n";
2345                 }
2346                 $alternate ^= 1;
2348                 if (exists $diff->{'nparents'}) { # combined diff
2350                         fill_from_file_info($diff, @parents)
2351                                 unless exists $diff->{'from_file'};
2353                         if ($diff->{'to_id'} ne ('0' x 40)) {
2354                                 # file exists in the result (child) commit
2355                                 print "<td>" .
2356                                       $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2357                                                              file_name=>$diff->{'to_file'},
2358                                                              hash_base=>$hash),
2359                                               -class => "list"}, esc_path($diff->{'to_file'})) .
2360                                       "</td>\n";
2361                         } else {
2362                                 print "<td>" .
2363                                       esc_path($diff->{'to_file'}) .
2364                                       "</td>\n";
2365                         }
2367                         if ($action eq 'commitdiff') {
2368                                 # link to patch
2369                                 $patchno++;
2370                                 print "<td class=\"link\">" .
2371                                       $cgi->a({-href => "#patch$patchno"}, "patch") .
2372                                       " | " .
2373                                       "</td>\n";
2374                         }
2376                         my $has_history = 0;
2377                         my $not_deleted = 0;
2378                         for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
2379                                 my $hash_parent = $parents[$i];
2380                                 my $from_hash = $diff->{'from_id'}[$i];
2381                                 my $from_path = $diff->{'from_file'}[$i];
2382                                 my $status = $diff->{'status'}[$i];
2384                                 $has_history ||= ($status ne 'A');
2385                                 $not_deleted ||= ($status ne 'D');
2387                                 if ($status eq 'A') {
2388                                         print "<td  class=\"link\" align=\"right\"> | </td>\n";
2389                                 } elsif ($status eq 'D') {
2390                                         print "<td class=\"link\">" .
2391                                               $cgi->a({-href => href(action=>"blob",
2392                                                                      hash_base=>$hash,
2393                                                                      hash=>$from_hash,
2394                                                                      file_name=>$from_path)},
2395                                                       "blob" . ($i+1)) .
2396                                               " | </td>\n";
2397                                 } else {
2398                                         if ($diff->{'to_id'} eq $from_hash) {
2399                                                 print "<td class=\"link nochange\">";
2400                                         } else {
2401                                                 print "<td class=\"link\">";
2402                                         }
2403                                         print $cgi->a({-href => href(action=>"blobdiff",
2404                                                                      hash=>$diff->{'to_id'},
2405                                                                      hash_parent=>$from_hash,
2406                                                                      hash_base=>$hash,
2407                                                                      hash_parent_base=>$hash_parent,
2408                                                                      file_name=>$diff->{'to_file'},
2409                                                                      file_parent=>$from_path)},
2410                                                       "diff" . ($i+1)) .
2411                                               " | </td>\n";
2412                                 }
2413                         }
2415                         print "<td class=\"link\">";
2416                         if ($not_deleted) {
2417                                 print $cgi->a({-href => href(action=>"blob",
2418                                                              hash=>$diff->{'to_id'},
2419                                                              file_name=>$diff->{'to_file'},
2420                                                              hash_base=>$hash)},
2421                                               "blob");
2422                                 print " | " if ($has_history);
2423                         }
2424                         if ($has_history) {
2425                                 print $cgi->a({-href => href(action=>"history",
2426                                                              file_name=>$diff->{'to_file'},
2427                                                              hash_base=>$hash)},
2428                                               "history");
2429                         }
2430                         print "</td>\n";
2432                         print "</tr>\n";
2433                         next; # instead of 'else' clause, to avoid extra indent
2434                 }
2435                 # else ordinary diff
2437                 my ($to_mode_oct, $to_mode_str, $to_file_type);
2438                 my ($from_mode_oct, $from_mode_str, $from_file_type);
2439                 if ($diff->{'to_mode'} ne ('0' x 6)) {
2440                         $to_mode_oct = oct $diff->{'to_mode'};
2441                         if (S_ISREG($to_mode_oct)) { # only for regular file
2442                                 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2443                         }
2444                         $to_file_type = file_type($diff->{'to_mode'});
2445                 }
2446                 if ($diff->{'from_mode'} ne ('0' x 6)) {
2447                         $from_mode_oct = oct $diff->{'from_mode'};
2448                         if (S_ISREG($to_mode_oct)) { # only for regular file
2449                                 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
2450                         }
2451                         $from_file_type = file_type($diff->{'from_mode'});
2452                 }
2454                 if ($diff->{'status'} eq "A") { # created
2455                         my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2456                         $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
2457                         $mode_chng   .= "]</span>";
2458                         print "<td>";
2459                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2460                                                      hash_base=>$hash, file_name=>$diff->{'file'}),
2461                                       -class => "list"}, esc_path($diff->{'file'}));
2462                         print "</td>\n";
2463                         print "<td>$mode_chng</td>\n";
2464                         print "<td class=\"link\">";
2465                         if ($action eq 'commitdiff') {
2466                                 # link to patch
2467                                 $patchno++;
2468                                 print $cgi->a({-href => "#patch$patchno"}, "patch");
2469                                 print " | ";
2470                         }
2471                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2472                                                      hash_base=>$hash, file_name=>$diff->{'file'})},
2473                                       "blob");
2474                         print "</td>\n";
2476                 } elsif ($diff->{'status'} eq "D") { # deleted
2477                         my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
2478                         print "<td>";
2479                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2480                                                      hash_base=>$parent, file_name=>$diff->{'file'}),
2481                                        -class => "list"}, esc_path($diff->{'file'}));
2482                         print "</td>\n";
2483                         print "<td>$mode_chng</td>\n";
2484                         print "<td class=\"link\">";
2485                         if ($action eq 'commitdiff') {
2486                                 # link to patch
2487                                 $patchno++;
2488                                 print $cgi->a({-href => "#patch$patchno"}, "patch");
2489                                 print " | ";
2490                         }
2491                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
2492                                                      hash_base=>$parent, file_name=>$diff->{'file'})},
2493                                       "blob") . " | ";
2494                         if ($have_blame) {
2495                                 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2496                                                              file_name=>$diff->{'file'})},
2497                                               "blame") . " | ";
2498                         }
2499                         print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
2500                                                      file_name=>$diff->{'file'})},
2501                                       "history");
2502                         print "</td>\n";
2504                 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
2505                         my $mode_chnge = "";
2506                         if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2507                                 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
2508                                 if ($from_file_type ne $to_file_type) {
2509                                         $mode_chnge .= " from $from_file_type to $to_file_type";
2510                                 }
2511                                 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2512                                         if ($from_mode_str && $to_mode_str) {
2513                                                 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2514                                         } elsif ($to_mode_str) {
2515                                                 $mode_chnge .= " mode: $to_mode_str";
2516                                         }
2517                                 }
2518                                 $mode_chnge .= "]</span>\n";
2519                         }
2520                         print "<td>";
2521                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2522                                                      hash_base=>$hash, file_name=>$diff->{'file'}),
2523                                       -class => "list"}, esc_path($diff->{'file'}));
2524                         print "</td>\n";
2525                         print "<td>$mode_chnge</td>\n";
2526                         print "<td class=\"link\">";
2527                         if ($action eq 'commitdiff') {
2528                                 # link to patch
2529                                 $patchno++;
2530                                 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2531                                       " | ";
2532                         } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2533                                 # "commit" view and modified file (not onlu mode changed)
2534                                 print $cgi->a({-href => href(action=>"blobdiff",
2535                                                              hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2536                                                              hash_base=>$hash, hash_parent_base=>$parent,
2537                                                              file_name=>$diff->{'file'})},
2538                                               "diff") .
2539                                       " | ";
2540                         }
2541                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2542                                                      hash_base=>$hash, file_name=>$diff->{'file'})},
2543                                        "blob") . " | ";
2544                         if ($have_blame) {
2545                                 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2546                                                              file_name=>$diff->{'file'})},
2547                                               "blame") . " | ";
2548                         }
2549                         print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2550                                                      file_name=>$diff->{'file'})},
2551                                       "history");
2552                         print "</td>\n";
2554                 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
2555                         my %status_name = ('R' => 'moved', 'C' => 'copied');
2556                         my $nstatus = $status_name{$diff->{'status'}};
2557                         my $mode_chng = "";
2558                         if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
2559                                 # mode also for directories, so we cannot use $to_mode_str
2560                                 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
2561                         }
2562                         print "<td>" .
2563                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2564                                                      hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
2565                                       -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
2566                               "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2567                               $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2568                                                      hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
2569                                       -class => "list"}, esc_path($diff->{'from_file'})) .
2570                               " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
2571                               "<td class=\"link\">";
2572                         if ($action eq 'commitdiff') {
2573                                 # link to patch
2574                                 $patchno++;
2575                                 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2576                                       " | ";
2577                         } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
2578                                 # "commit" view and modified file (not only pure rename or copy)
2579                                 print $cgi->a({-href => href(action=>"blobdiff",
2580                                                              hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
2581                                                              hash_base=>$hash, hash_parent_base=>$parent,
2582                                                              file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
2583                                               "diff") .
2584                                       " | ";
2585                         }
2586                         print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
2587                                                      hash_base=>$parent, file_name=>$diff->{'to_file'})},
2588                                       "blob") . " | ";
2589                         if ($have_blame) {
2590                                 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2591                                                              file_name=>$diff->{'to_file'})},
2592                                               "blame") . " | ";
2593                         }
2594                         print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2595                                                     file_name=>$diff->{'to_file'})},
2596                                       "history");
2597                         print "</td>\n";
2599                 } # we should not encounter Unmerged (U) or Unknown (X) status
2600                 print "</tr>\n";
2601         }
2602         print "</table>\n";
2605 sub git_patchset_body {
2606         my ($fd, $difftree, $hash, @hash_parents) = @_;
2607         my ($hash_parent) = $hash_parents[0];
2609         my $patch_idx = 0;
2610         my $patch_number = 0;
2611         my $patch_line;
2612         my $diffinfo;
2613         my (%from, %to);
2615         print "<div class=\"patchset\">\n";
2617         # skip to first patch
2618         while ($patch_line = <$fd>) {
2619                 chomp $patch_line;
2621                 last if ($patch_line =~ m/^diff /);
2622         }
2624  PATCH:
2625         while ($patch_line) {
2626                 my @diff_header;
2627                 my ($from_id, $to_id);
2629                 # git diff header
2630                 #assert($patch_line =~ m/^diff /) if DEBUG;
2631                 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2632                 $patch_number++;
2633                 push @diff_header, $patch_line;
2635                 # extended diff header
2636         EXTENDED_HEADER:
2637                 while ($patch_line = <$fd>) {
2638                         chomp $patch_line;
2640                         last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
2642                         if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2643                                 $from_id = $1;
2644                                 $to_id   = $2;
2645                         } elsif ($patch_line =~ m/^index ((?:[0-9a-fA-F]{40},)+[0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2646                                 $from_id = [ split(',', $1) ];
2647                                 $to_id   = $2;
2648                         }
2650                         push @diff_header, $patch_line;
2651                 }
2652                 my $last_patch_line = $patch_line;
2654                 # check if current patch belong to current raw line
2655                 # and parse raw git-diff line if needed
2656                 if (defined $diffinfo &&
2657                     from_ids_eq($diffinfo->{'from_id'}, $from_id) &&
2658                     $diffinfo->{'to_id'} eq $to_id) {
2659                         # this is split patch
2660                         print "<div class=\"patch cont\">\n";
2661                 } else {
2662                         # advance raw git-diff output if needed
2663                         $patch_idx++ if defined $diffinfo;
2665                         # read and prepare patch information
2666                         if (ref($difftree->[$patch_idx]) eq "HASH") {
2667                                 # pre-parsed (or generated by hand)
2668                                 $diffinfo = $difftree->[$patch_idx];
2669                         } else {
2670                                 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2671                         }
2672                         if ($diffinfo->{'nparents'}) {
2673                                 # combined diff
2674                                 $from{'file'} = [];
2675                                 $from{'href'} = [];
2676                                 fill_from_file_info($diffinfo, @hash_parents)
2677                                         unless exists $diffinfo->{'from_file'};
2678                                 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2679                                         $from{'file'}[$i] = $diffinfo->{'from_file'}[$i] || $diffinfo->{'to_file'};
2680                                         if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2681                                                 $from{'href'}[$i] = href(action=>"blob",
2682                                                                          hash_base=>$hash_parents[$i],
2683                                                                          hash=>$diffinfo->{'from_id'}[$i],
2684                                                                          file_name=>$from{'file'}[$i]);
2685                                         } else {
2686                                                 $from{'href'}[$i] = undef;
2687                                         }
2688                                 }
2689                         } else {
2690                                 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2691                                 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2692                                         $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2693                                                              hash=>$diffinfo->{'from_id'},
2694                                                              file_name=>$from{'file'});
2695                                 } else {
2696                                         delete $from{'href'};
2697                                 }
2698                         }
2699                         $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2700                         if ($diffinfo->{'status'} ne "D") { # not deleted file
2701                                 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2702                                                    hash=>$diffinfo->{'to_id'},
2703                                                    file_name=>$to{'file'});
2704                         } else {
2705                                 delete $to{'href'};
2706                         }
2707                         # this is first patch for raw difftree line with $patch_idx index
2708                         # we index @$difftree array from 0, but number patches from 1
2709                         print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
2710                 }
2712                 # print "git diff" header
2713                 $patch_line = shift @diff_header;
2714                 if ($diffinfo->{'nparents'}) {
2716                         # combined diff
2717                         $patch_line =~ s!^(diff (.*?) )"?.*$!$1!;
2718                         if ($to{'href'}) {
2719                                 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2720                                                        esc_path($to{'file'}));
2721                         } else { # file was deleted
2722                                 $patch_line .= esc_path($to{'file'});
2723                         }
2725                 } else {
2727                         $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2728                         if ($from{'href'}) {
2729                                 $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2730                                                        'a/' . esc_path($from{'file'}));
2731                         } else { # file was added
2732                                 $patch_line .= 'a/' . esc_path($from{'file'});
2733                         }
2734                         $patch_line .= ' ';
2735                         if ($to{'href'}) {
2736                                 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2737                                                        'b/' . esc_path($to{'file'}));
2738                         } else { # file was deleted
2739                                 $patch_line .= 'b/' . esc_path($to{'file'});
2740                         }
2742                 }
2743                 print "<div class=\"diff header\">$patch_line</div>\n";
2745                 # print extended diff header
2746                 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
2747         EXTENDED_HEADER:
2748                 foreach $patch_line (@diff_header) {
2749                         # match <path>
2750                         if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2751                                 $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2752                                                        esc_path($from{'file'}));
2753                         }
2754                         if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2755                                 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"path"},
2756                                                        esc_path($to{'file'}));
2757                         }
2758                         # match single <mode>
2759                         if ($patch_line =~ m/\s(\d{6})$/) {
2760                                 $patch_line .= '<span class="info"> (' .
2761                                                file_type_long($1) .
2762                                                ')</span>';
2763                         }
2764                         # match <hash>
2765                         if ($patch_line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2766                                 # can match only for combined diff
2767                                 $patch_line = 'index ';
2768                                 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2769                                         if ($from{'href'}[$i]) {
2770                                                 $patch_line .= $cgi->a({-href=>$from{'href'}[$i],
2771                                                                         -class=>"hash"},
2772                                                                        substr($diffinfo->{'from_id'}[$i],0,7));
2773                                         } else {
2774                                                 $patch_line .= '0' x 7;
2775                                         }
2776                                         # separator
2777                                         $patch_line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2778                                 }
2779                                 $patch_line .= '..';
2780                                 if ($to{'href'}) {
2781                                         $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2782                                                                substr($diffinfo->{'to_id'},0,7));
2783                                 } else {
2784                                         $patch_line .= '0' x 7;
2785                                 }
2787                         } elsif ($patch_line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2788                                 # can match only for ordinary diff
2789                                 my ($from_link, $to_link);
2790                                 if ($from{'href'}) {
2791                                         $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2792                                                              substr($diffinfo->{'from_id'},0,7));
2793                                 } else {
2794                                         $from_link = '0' x 7;
2795                                 }
2796                                 if ($to{'href'}) {
2797                                         $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2798                                                            substr($diffinfo->{'to_id'},0,7));
2799                                 } else {
2800                                         $to_link = '0' x 7;
2801                                 }
2802                                 #affirm {
2803                                 #       my ($from_hash, $to_hash) =
2804                                 #               ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2805                                 #       my ($from_id, $to_id) =
2806                                 #               ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2807                                 #       ($from_hash eq $from_id) && ($to_hash eq $to_id);
2808                                 #} if DEBUG;
2809                                 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2810                                 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2811                         }
2812                         print $patch_line . "<br/>\n";
2813                 }
2814                 print "</div>\n"  if (@diff_header > 0); # class="diff extended_header"
2816                 # from-file/to-file diff header
2817                 $patch_line = $last_patch_line;
2818                 if (! $patch_line) {
2819                         print "</div>\n"; # class="patch"
2820                         last PATCH;
2821                 }
2822                 next PATCH if ($patch_line =~ m/^diff /);
2823                 #assert($patch_line =~ m/^---/) if DEBUG;
2824                 if (!$diffinfo->{'nparents'} && # not from-file line for combined diff
2825                     $from{'href'} && $patch_line =~ m!^--- "?a/!) {
2826                         $patch_line = '--- a/' .
2827                                       $cgi->a({-href=>$from{'href'}, -class=>"path"},
2828                                               esc_path($from{'file'}));
2829                 }
2830                 print "<div class=\"diff from_file\">$patch_line</div>\n";
2832                 $patch_line = <$fd>;
2833                 chomp $patch_line;
2835                 #assert($patch_line =~ m/^+++/) if DEBUG;
2836                 if ($to{'href'} && $patch_line =~ m!^\+\+\+ "?b/!) {
2837                         $patch_line = '+++ b/' .
2838                                       $cgi->a({-href=>$to{'href'}, -class=>"path"},
2839                                               esc_path($to{'file'}));
2840                 }
2841                 print "<div class=\"diff to_file\">$patch_line</div>\n";
2843                 # the patch itself
2844         LINE:
2845                 while ($patch_line = <$fd>) {
2846                         chomp $patch_line;
2848                         next PATCH if ($patch_line =~ m/^diff /);
2850                         print format_diff_line($patch_line, \%from, \%to);
2851                 }
2853         } continue {
2854                 print "</div>\n"; # class="patch"
2855         }
2856         print "<div class=\"diff nodifferences\">No differences found</div>\n" if (!$patch_number);
2858         print "</div>\n"; # class="patchset"
2861 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2863 sub git_project_list_body {
2864         my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2866         my ($check_forks) = gitweb_check_feature('forks');
2868         my @projects;
2869         foreach my $pr (@$projlist) {
2870                 my (@aa) = git_get_last_activity($pr->{'path'});
2871                 unless (@aa) {
2872                         next;
2873                 }
2874                 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2875                 if (!defined $pr->{'descr'}) {
2876                         my $descr = git_get_project_description($pr->{'path'}) || "";
2877                         $pr->{'descr_long'} = decode_utf8($descr);
2878                         $pr->{'descr'} = chop_str($descr, 25, 5);
2879                 }
2880                 if (!defined $pr->{'owner'}) {
2881                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2882                 }
2883                 if ($check_forks) {
2884                         my $pname = $pr->{'path'};
2885                         if (($pname =~ s/\.git$//) &&
2886                             ($pname !~ /\/$/) &&
2887                             (-d "$projectroot/$pname")) {
2888                                 $pr->{'forks'} = "-d $projectroot/$pname";
2889                         }
2890                         else {
2891                                 $pr->{'forks'} = 0;
2892                         }
2893                 }
2894                 push @projects, $pr;
2895         }
2897         $order ||= $default_projects_order;
2898         $from = 0 unless defined $from;
2899         $to = $#projects if (!defined $to || $#projects < $to);
2901         print "<table class=\"project_list\">\n";
2902         unless ($no_header) {
2903                 print "<tr>\n";
2904                 if ($check_forks) {
2905                         print "<th></th>\n";
2906                 }
2907                 if ($order eq "project") {
2908                         @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2909                         print "<th>Project</th>\n";
2910                 } else {
2911                         print "<th>" .
2912                               $cgi->a({-href => href(project=>undef, order=>'project'),
2913                                        -class => "header"}, "Project") .
2914                               "</th>\n";
2915                 }
2916                 if ($order eq "descr") {
2917                         @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2918                         print "<th>Description</th>\n";
2919                 } else {
2920                         print "<th>" .
2921                               $cgi->a({-href => href(project=>undef, order=>'descr'),
2922                                        -class => "header"}, "Description") .
2923                               "</th>\n";
2924                 }
2925                 if ($order eq "owner") {
2926                         @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2927                         print "<th>Owner</th>\n";
2928                 } else {
2929                         print "<th>" .
2930                               $cgi->a({-href => href(project=>undef, order=>'owner'),
2931                                        -class => "header"}, "Owner") .
2932                               "</th>\n";
2933                 }
2934                 if ($order eq "age") {
2935                         @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2936                         print "<th>Last Change</th>\n";
2937                 } else {
2938                         print "<th>" .
2939                               $cgi->a({-href => href(project=>undef, order=>'age'),
2940                                        -class => "header"}, "Last Change") .
2941                               "</th>\n";
2942                 }
2943                 print "<th></th>\n" .
2944                       "</tr>\n";
2945         }
2946         my $alternate = 1;
2947         for (my $i = $from; $i <= $to; $i++) {
2948                 my $pr = $projects[$i];
2949                 if ($alternate) {
2950                         print "<tr class=\"dark\">\n";
2951                 } else {
2952                         print "<tr class=\"light\">\n";
2953                 }
2954                 $alternate ^= 1;
2955                 if ($check_forks) {
2956                         print "<td>";
2957                         if ($pr->{'forks'}) {
2958                                 print "<!-- $pr->{'forks'} -->\n";
2959                                 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2960                         }
2961                         print "</td>\n";
2962                 }
2963                 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2964                                         -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2965                       "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2966                                         -class => "list", -title => $pr->{'descr_long'}},
2967                                         esc_html($pr->{'descr'})) . "</td>\n" .
2968                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2969                 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2970                       $pr->{'age_string'} . "</td>\n" .
2971                       "<td class=\"link\">" .
2972                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
2973                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2974                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2975                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2976                       ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
2977                       "</td>\n" .
2978                       "</tr>\n";
2979         }
2980         if (defined $extra) {
2981                 print "<tr>\n";
2982                 if ($check_forks) {
2983                         print "<td></td>\n";
2984                 }
2985                 print "<td colspan=\"5\">$extra</td>\n" .
2986                       "</tr>\n";
2987         }
2988         print "</table>\n";
2991 sub git_shortlog_body {
2992         # uses global variable $project
2993         my ($commitlist, $from, $to, $refs, $extra) = @_;
2995         my $have_snapshot = gitweb_have_snapshot();
2997         $from = 0 unless defined $from;
2998         $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3000         print "<table class=\"shortlog\" cellspacing=\"0\">\n";
3001         my $alternate = 1;
3002         for (my $i = $from; $i <= $to; $i++) {
3003                 my %co = %{$commitlist->[$i]};
3004                 my $commit = $co{'id'};
3005                 my $ref = format_ref_marker($refs, $commit);
3006                 if ($alternate) {
3007                         print "<tr class=\"dark\">\n";
3008                 } else {
3009                         print "<tr class=\"light\">\n";
3010                 }
3011                 $alternate ^= 1;
3012                 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3013                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3014                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
3015                       "<td>";
3016                 print format_subject_html($co{'title'}, $co{'title_short'},
3017                                           href(action=>"commit", hash=>$commit), $ref);
3018                 print "</td>\n" .
3019                       "<td class=\"link\">" .
3020                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3021                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3022                       $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3023                 if ($have_snapshot) {
3024                         print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
3025                 }
3026                 print "</td>\n" .
3027                       "</tr>\n";
3028         }
3029         if (defined $extra) {
3030                 print "<tr>\n" .
3031                       "<td colspan=\"4\">$extra</td>\n" .
3032                       "</tr>\n";
3033         }
3034         print "</table>\n";
3037 sub git_history_body {
3038         # Warning: assumes constant type (blob or tree) during history
3039         my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3041         $from = 0 unless defined $from;
3042         $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3044         print "<table class=\"history\" cellspacing=\"0\">\n";
3045         my $alternate = 1;
3046         for (my $i = $from; $i <= $to; $i++) {
3047                 my %co = %{$commitlist->[$i]};
3048                 if (!%co) {
3049                         next;
3050                 }
3051                 my $commit = $co{'id'};
3053                 my $ref = format_ref_marker($refs, $commit);
3055                 if ($alternate) {
3056                         print "<tr class=\"dark\">\n";
3057                 } else {
3058                         print "<tr class=\"light\">\n";
3059                 }
3060                 $alternate ^= 1;
3061                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3062                       # shortlog uses      chop_str($co{'author_name'}, 10)
3063                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
3064                       "<td>";
3065                 # originally git_history used chop_str($co{'title'}, 50)
3066                 print format_subject_html($co{'title'}, $co{'title_short'},
3067                                           href(action=>"commit", hash=>$commit), $ref);
3068                 print "</td>\n" .
3069                       "<td class=\"link\">" .
3070                       $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3071                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3073                 if ($ftype eq 'blob') {
3074                         my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3075                         my $blob_parent  = git_get_hash_by_path($commit, $file_name);
3076                         if (defined $blob_current && defined $blob_parent &&
3077                                         $blob_current ne $blob_parent) {
3078                                 print " | " .
3079                                         $cgi->a({-href => href(action=>"blobdiff",
3080                                                                hash=>$blob_current, hash_parent=>$blob_parent,
3081                                                                hash_base=>$hash_base, hash_parent_base=>$commit,
3082                                                                file_name=>$file_name)},
3083                                                 "diff to current");
3084                         }
3085                 }
3086                 print "</td>\n" .
3087                       "</tr>\n";
3088         }
3089         if (defined $extra) {
3090                 print "<tr>\n" .
3091                       "<td colspan=\"4\">$extra</td>\n" .
3092                       "</tr>\n";
3093         }
3094         print "</table>\n";
3097 sub git_tags_body {
3098         # uses global variable $project
3099         my ($taglist, $from, $to, $extra) = @_;
3100         $from = 0 unless defined $from;
3101         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3103         print "<table class=\"tags\" cellspacing=\"0\">\n";
3104         my $alternate = 1;
3105         for (my $i = $from; $i <= $to; $i++) {
3106                 my $entry = $taglist->[$i];
3107                 my %tag = %$entry;
3108                 my $comment = $tag{'subject'};
3109                 my $comment_short;
3110                 if (defined $comment) {
3111                         $comment_short = chop_str($comment, 30, 5);
3112                 }
3113                 if ($alternate) {
3114                         print "<tr class=\"dark\">\n";
3115                 } else {
3116                         print "<tr class=\"light\">\n";
3117                 }
3118                 $alternate ^= 1;
3119                 if (defined $tag{'age'}) {
3120                         print "<td><i>$tag{'age'}</i></td>\n";
3121                 } else {
3122                         print "<td></td>\n";
3123                 }
3124                 print "<td>" .
3125                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3126                                -class => "list name"}, esc_html($tag{'name'})) .
3127                       "</td>\n" .
3128                       "<td>";
3129                 if (defined $comment) {
3130                         print format_subject_html($comment, $comment_short,
3131                                                   href(action=>"tag", hash=>$tag{'id'}));
3132                 }
3133                 print "</td>\n" .
3134                       "<td class=\"selflink\">";
3135                 if ($tag{'type'} eq "tag") {
3136                         print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3137                 } else {
3138                         print "&nbsp;";
3139                 }
3140                 print "</td>\n" .
3141                       "<td class=\"link\">" . " | " .
3142                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3143                 if ($tag{'reftype'} eq "commit") {
3144                         print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
3145                               " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
3146                 } elsif ($tag{'reftype'} eq "blob") {
3147                         print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3148                 }
3149                 print "</td>\n" .
3150                       "</tr>";
3151         }
3152         if (defined $extra) {
3153                 print "<tr>\n" .
3154                       "<td colspan=\"5\">$extra</td>\n" .
3155                       "</tr>\n";
3156         }
3157         print "</table>\n";
3160 sub git_heads_body {
3161         # uses global variable $project
3162         my ($headlist, $head, $from, $to, $extra) = @_;
3163         $from = 0 unless defined $from;
3164         $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3166         print "<table class=\"heads\" cellspacing=\"0\">\n";
3167         my $alternate = 1;
3168         for (my $i = $from; $i <= $to; $i++) {
3169                 my $entry = $headlist->[$i];
3170                 my %ref = %$entry;
3171                 my $curr = $ref{'id'} eq $head;
3172                 if ($alternate) {
3173                         print "<tr class=\"dark\">\n";
3174                 } else {
3175                         print "<tr class=\"light\">\n";
3176                 }
3177                 $alternate ^= 1;
3178                 print "<td><i>$ref{'age'}</i></td>\n" .
3179                       ($curr ? "<td class=\"current_head\">" : "<td>") .
3180                       $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
3181                                -class => "list name"},esc_html($ref{'name'})) .
3182                       "</td>\n" .
3183                       "<td class=\"link\">" .
3184                       $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
3185                       $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
3186                       $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
3187                       "</td>\n" .
3188                       "</tr>";
3189         }
3190         if (defined $extra) {
3191                 print "<tr>\n" .
3192                       "<td colspan=\"3\">$extra</td>\n" .
3193                       "</tr>\n";
3194         }
3195         print "</table>\n";
3198 sub git_search_grep_body {
3199         my ($commitlist, $from, $to, $extra) = @_;
3200         $from = 0 unless defined $from;
3201         $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3203         print "<table class=\"grep\" cellspacing=\"0\">\n";
3204         my $alternate = 1;
3205         for (my $i = $from; $i <= $to; $i++) {
3206                 my %co = %{$commitlist->[$i]};
3207                 if (!%co) {
3208                         next;
3209                 }
3210                 my $commit = $co{'id'};
3211                 if ($alternate) {
3212                         print "<tr class=\"dark\">\n";
3213                 } else {
3214                         print "<tr class=\"light\">\n";
3215                 }
3216                 $alternate ^= 1;
3217                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3218                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3219                       "<td>" .
3220                       $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3221                                esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3222                 my $comment = $co{'comment'};
3223                 foreach my $line (@$comment) {
3224                         if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3225                                 my $lead = esc_html($1) || "";
3226                                 $lead = chop_str($lead, 30, 10);
3227                                 my $match = esc_html($2) || "";
3228                                 my $trail = esc_html($3) || "";
3229                                 $trail = chop_str($trail, 30, 10);
3230                                 my $text = "$lead<span class=\"match\">$match</span>$trail";
3231                                 print chop_str($text, 80, 5) . "<br/>\n";
3232                         }
3233                 }
3234                 print "</td>\n" .
3235                       "<td class=\"link\">" .
3236                       $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3237                       " | " .
3238                       $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3239                 print "</td>\n" .
3240                       "</tr>\n";
3241         }
3242         if (defined $extra) {
3243                 print "<tr>\n" .
3244                       "<td colspan=\"3\">$extra</td>\n" .
3245                       "</tr>\n";
3246         }
3247         print "</table>\n";
3250 ## ======================================================================
3251 ## ======================================================================
3252 ## actions
3254 sub git_project_list {
3255         my $order = $cgi->param('o');
3256         if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3257                 die_error(undef, "Unknown order parameter");
3258         }
3260         my @list = git_get_projects_list();
3261         if (!@list) {
3262                 die_error(undef, "No projects found");
3263         }
3265         git_header_html();
3266         if (-f $home_text) {
3267                 print "<div class=\"index_include\">\n";
3268                 open (my $fd, $home_text);
3269                 print <$fd>;
3270                 close $fd;
3271                 print "</div>\n";
3272         }
3273         git_project_list_body(\@list, $order);
3274         git_footer_html();
3277 sub git_forks {
3278         my $order = $cgi->param('o');
3279         if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3280                 die_error(undef, "Unknown order parameter");
3281         }
3283         my @list = git_get_projects_list($project);
3284         if (!@list) {
3285                 die_error(undef, "No forks found");
3286         }
3288         git_header_html();
3289         git_print_page_nav('','');
3290         git_print_header_div('summary', "$project forks");
3291         git_project_list_body(\@list, $order);
3292         git_footer_html();
3295 sub git_project_index {
3296         my @projects = git_get_projects_list($project);
3298         print $cgi->header(
3299                 -type => 'text/plain',
3300                 -charset => 'utf-8',
3301                 -content_disposition => 'inline; filename="index.aux"');
3303         foreach my $pr (@projects) {
3304                 if (!exists $pr->{'owner'}) {
3305                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}");
3306                 }
3308                 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3309                 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3310                 $path  =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3311                 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3312                 $path  =~ s/ /\+/g;
3313                 $owner =~ s/ /\+/g;
3315                 print "$path $owner\n";
3316         }
3319 sub git_summary {
3320         my $descr = git_get_project_description($project) || "none";
3321         my %co = parse_commit("HEAD");
3322         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3323         my $head = $co{'id'};
3325         my $owner = git_get_project_owner($project);
3327         my $refs = git_get_references();
3328         # These get_*_list functions return one more to allow us to see if
3329         # there are more ...
3330         my @taglist  = git_get_tags_list(16);
3331         my @headlist = git_get_heads_list(16);
3332         my @forklist;
3333         my ($check_forks) = gitweb_check_feature('forks');
3335         if ($check_forks) {
3336                 @forklist = git_get_projects_list($project);
3337         }
3339         git_header_html();
3340         git_print_page_nav('summary','', $head);
3342         print "<div class=\"title\">&nbsp;</div>\n";
3343         print "<table cellspacing=\"0\">\n" .
3344               "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
3345               "<tr><td>owner</td><td>$owner</td></tr>\n" .
3346               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
3347         # use per project git URL list in $projectroot/$project/cloneurl
3348         # or make project git URL from git base URL and project name
3349         my $url_tag = "URL";
3350         my @url_list = git_get_project_url_list($project);
3351         @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3352         foreach my $git_url (@url_list) {
3353                 next unless $git_url;
3354                 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
3355                 $url_tag = "";
3356         }
3357         print "</table>\n";
3359         if (-s "$projectroot/$project/README.html") {
3360                 if (open my $fd, "$projectroot/$project/README.html") {
3361                         print "<div class=\"title\">readme</div>\n";
3362                         print $_ while (<$fd>);
3363                         close $fd;
3364                 }
3365         }
3367         # we need to request one more than 16 (0..15) to check if
3368         # those 16 are all
3369         my @commitlist = parse_commits($head, 17);
3370         git_print_header_div('shortlog');
3371         git_shortlog_body(\@commitlist, 0, 15, $refs,
3372                           $#commitlist <=  15 ? undef :
3373                           $cgi->a({-href => href(action=>"shortlog")}, "..."));
3375         if (@taglist) {
3376                 git_print_header_div('tags');
3377                 git_tags_body(\@taglist, 0, 15,
3378                               $#taglist <=  15 ? undef :
3379                               $cgi->a({-href => href(action=>"tags")}, "..."));
3380         }
3382         if (@headlist) {
3383                 git_print_header_div('heads');
3384                 git_heads_body(\@headlist, $head, 0, 15,
3385                                $#headlist <= 15 ? undef :
3386                                $cgi->a({-href => href(action=>"heads")}, "..."));
3387         }
3389         if (@forklist) {
3390                 git_print_header_div('forks');
3391                 git_project_list_body(\@forklist, undef, 0, 15,
3392                                       $#forklist <= 15 ? undef :
3393                                       $cgi->a({-href => href(action=>"forks")}, "..."),
3394                                       'noheader');
3395         }
3397         git_footer_html();
3400 sub git_tag {
3401         my $head = git_get_head_hash($project);
3402         git_header_html();
3403         git_print_page_nav('','', $head,undef,$head);
3404         my %tag = parse_tag($hash);
3405         git_print_header_div('commit', esc_html($tag{'name'}), $hash);
3406         print "<div class=\"title_text\">\n" .
3407               "<table cellspacing=\"0\">\n" .
3408               "<tr>\n" .
3409               "<td>object</td>\n" .
3410               "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3411                                $tag{'object'}) . "</td>\n" .
3412               "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3413                                               $tag{'type'}) . "</td>\n" .
3414               "</tr>\n";
3415         if (defined($tag{'author'})) {
3416                 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
3417                 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
3418                 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3419                         sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3420                         "</td></tr>\n";
3421         }
3422         print "</table>\n\n" .
3423               "</div>\n";
3424         print "<div class=\"page_body\">";
3425         my $comment = $tag{'comment'};
3426         foreach my $line (@$comment) {
3427                 chomp $line;
3428                 print esc_html($line, -nbsp=>1) . "<br/>\n";
3429         }
3430         print "</div>\n";
3431         git_footer_html();
3434 sub git_blame2 {
3435         my $fd;
3436         my $ftype;
3438         my ($have_blame) = gitweb_check_feature('blame');
3439         if (!$have_blame) {
3440                 die_error('403 Permission denied', "Permission denied");
3441         }
3442         die_error('404 Not Found', "File name not defined") if (!$file_name);
3443         $hash_base ||= git_get_head_hash($project);
3444         die_error(undef, "Couldn't find base commit") unless ($hash_base);
3445         my %co = parse_commit($hash_base)
3446                 or die_error(undef, "Reading commit failed");
3447         if (!defined $hash) {
3448                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3449                         or die_error(undef, "Error looking up file");
3450         }
3451         $ftype = git_get_type($hash);
3452         if ($ftype !~ "blob") {
3453                 die_error('400 Bad Request', "Object is not a blob");
3454         }
3455         open ($fd, "-|", git_cmd(), "blame", '-p', '--',
3456               $file_name, $hash_base)
3457                 or die_error(undef, "Open git-blame failed");
3458         git_header_html();
3459         my $formats_nav =
3460                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3461                         "blob") .
3462                 " | " .
3463                 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3464                         "history") .
3465                 " | " .
3466                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3467                         "HEAD");
3468         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3469         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3470         git_print_page_path($file_name, $ftype, $hash_base);
3471         my @rev_color = (qw(light2 dark2));
3472         my $num_colors = scalar(@rev_color);
3473         my $current_color = 0;
3474         my $last_rev;
3475         print <<HTML;
3476 <div class="page_body">
3477 <table class="blame">
3478 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3479 HTML
3480         my %metainfo = ();
3481         while (1) {
3482                 $_ = <$fd>;
3483                 last unless defined $_;
3484                 my ($full_rev, $orig_lineno, $lineno, $group_size) =
3485                     /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3486                 if (!exists $metainfo{$full_rev}) {
3487                         $metainfo{$full_rev} = {};
3488                 }
3489                 my $meta = $metainfo{$full_rev};
3490                 while (<$fd>) {
3491                         last if (s/^\t//);
3492                         if (/^(\S+) (.*)$/) {
3493                                 $meta->{$1} = $2;
3494                         }
3495                 }
3496                 my $data = $_;
3497                 chomp $data;
3498                 my $rev = substr($full_rev, 0, 8);
3499                 my $author = $meta->{'author'};
3500                 my %date = parse_date($meta->{'author-time'},
3501                                       $meta->{'author-tz'});
3502                 my $date = $date{'iso-tz'};
3503                 if ($group_size) {
3504                         $current_color = ++$current_color % $num_colors;
3505                 }
3506                 print "<tr class=\"$rev_color[$current_color]\">\n";
3507                 if ($group_size) {
3508                         print "<td class=\"sha1\"";
3509                         print " title=\"". esc_html($author) . ", $date\"";
3510                         print " rowspan=\"$group_size\"" if ($group_size > 1);
3511                         print ">";
3512                         print $cgi->a({-href => href(action=>"commit",
3513                                                      hash=>$full_rev,
3514                                                      file_name=>$file_name)},
3515                                       esc_html($rev));
3516                         print "</td>\n";
3517                 }
3518                 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
3519                         or die_error(undef, "Open git-rev-parse failed");
3520                 my $parent_commit = <$dd>;
3521                 close $dd;
3522                 chomp($parent_commit);
3523                 my $blamed = href(action => 'blame',
3524                                   file_name => $meta->{'filename'},
3525                                   hash_base => $parent_commit);
3526                 print "<td class=\"linenr\">";
3527                 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3528                                 -id => "l$lineno",
3529                                 -class => "linenr" },
3530                               esc_html($lineno));
3531                 print "</td>";
3532                 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3533                 print "</tr>\n";
3534         }
3535         print "</table>\n";
3536         print "</div>";
3537         close $fd
3538                 or print "Reading blob failed\n";
3539         git_footer_html();
3542 sub git_blame {
3543         my $fd;
3545         my ($have_blame) = gitweb_check_feature('blame');
3546         if (!$have_blame) {
3547                 die_error('403 Permission denied', "Permission denied");
3548         }
3549         die_error('404 Not Found', "File name not defined") if (!$file_name);
3550         $hash_base ||= git_get_head_hash($project);
3551         die_error(undef, "Couldn't find base commit") unless ($hash_base);
3552         my %co = parse_commit($hash_base)
3553                 or die_error(undef, "Reading commit failed");
3554         if (!defined $hash) {
3555                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3556                         or die_error(undef, "Error lookup file");
3557         }
3558         open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
3559                 or die_error(undef, "Open git-annotate failed");
3560         git_header_html();
3561         my $formats_nav =
3562                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3563                         "blob") .
3564                 " | " .
3565                 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3566                         "history") .
3567                 " | " .
3568                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
3569                         "HEAD");
3570         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3571         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3572         git_print_page_path($file_name, 'blob', $hash_base);
3573         print "<div class=\"page_body\">\n";
3574         print <<HTML;
3575 <table class="blame">
3576   <tr>
3577     <th>Commit</th>
3578     <th>Age</th>
3579     <th>Author</th>
3580     <th>Line</th>
3581     <th>Data</th>
3582   </tr>
3583 HTML
3584         my @line_class = (qw(light dark));
3585         my $line_class_len = scalar (@line_class);
3586         my $line_class_num = $#line_class;
3587         while (my $line = <$fd>) {
3588                 my $long_rev;
3589                 my $short_rev;
3590                 my $author;
3591                 my $time;
3592                 my $lineno;
3593                 my $data;
3594                 my $age;
3595                 my $age_str;
3596                 my $age_class;
3598                 chomp $line;
3599                 $line_class_num = ($line_class_num + 1) % $line_class_len;
3601                 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
3602                         $long_rev = $1;
3603                         $author   = $2;
3604                         $time     = $3;
3605                         $lineno   = $4;
3606                         $data     = $5;
3607                 } else {
3608                         print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
3609                         next;
3610                 }
3611                 $short_rev  = substr ($long_rev, 0, 8);
3612                 $age        = time () - $time;
3613                 $age_str    = age_string ($age);
3614                 $age_str    =~ s/ /&nbsp;/g;
3615                 $age_class  = age_class($age);
3616                 $author     = esc_html ($author);
3617                 $author     =~ s/ /&nbsp;/g;
3619                 $data = untabify($data);
3620                 $data = esc_html ($data);
3622                 print <<HTML;
3623   <tr class="$line_class[$line_class_num]">
3624     <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
3625     <td class="$age_class">$age_str</td>
3626     <td>$author</td>
3627     <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3628     <td class="pre">$data</td>
3629   </tr>
3630 HTML
3631         } # while (my $line = <$fd>)
3632         print "</table>\n\n";
3633         close $fd
3634                 or print "Reading blob failed.\n";
3635         print "</div>";
3636         git_footer_html();
3639 sub git_tags {
3640         my $head = git_get_head_hash($project);
3641         git_header_html();
3642         git_print_page_nav('','', $head,undef,$head);
3643         git_print_header_div('summary', $project);
3645         my @tagslist = git_get_tags_list();
3646         if (@tagslist) {
3647                 git_tags_body(\@tagslist);
3648         }
3649         git_footer_html();
3652 sub git_heads {
3653         my $head = git_get_head_hash($project);
3654         git_header_html();
3655         git_print_page_nav('','', $head,undef,$head);
3656         git_print_header_div('summary', $project);
3658         my @headslist = git_get_heads_list();
3659         if (@headslist) {
3660                 git_heads_body(\@headslist, $head);
3661         }
3662         git_footer_html();
3665 sub git_blob_plain {
3666         my $expires;
3668         if (!defined $hash) {
3669                 if (defined $file_name) {
3670                         my $base = $hash_base || git_get_head_hash($project);
3671                         $hash = git_get_hash_by_path($base, $file_name, "blob")
3672                                 or die_error(undef, "Error lookup file");
3673                 } else {
3674                         die_error(undef, "No file name defined");
3675                 }
3676         } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3677                 # blobs defined by non-textual hash id's can be cached
3678                 $expires = "+1d";
3679         }
3681         my $type = shift;
3682         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3683                 or die_error(undef, "Couldn't cat $file_name, $hash");
3685         $type ||= blob_mimetype($fd, $file_name);
3687         # save as filename, even when no $file_name is given
3688         my $save_as = "$hash";
3689         if (defined $file_name) {
3690                 $save_as = $file_name;
3691         } elsif ($type =~ m/^text\//) {
3692                 $save_as .= '.txt';
3693         }
3695         print $cgi->header(
3696                 -type => "$type",
3697                 -expires=>$expires,
3698                 -content_disposition => 'inline; filename="' . "$save_as" . '"');
3699         undef $/;
3700         binmode STDOUT, ':raw';
3701         print <$fd>;
3702         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3703         $/ = "\n";
3704         close $fd;
3707 sub git_blob {
3708         my $expires;
3710         if (!defined $hash) {
3711                 if (defined $file_name) {
3712                         my $base = $hash_base || git_get_head_hash($project);
3713                         $hash = git_get_hash_by_path($base, $file_name, "blob")
3714                                 or die_error(undef, "Error lookup file");
3715                 } else {
3716                         die_error(undef, "No file name defined");
3717                 }
3718         } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3719                 # blobs defined by non-textual hash id's can be cached
3720                 $expires = "+1d";
3721         }
3723         my ($have_blame) = gitweb_check_feature('blame');
3724         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3725                 or die_error(undef, "Couldn't cat $file_name, $hash");
3726         my $mimetype = blob_mimetype($fd, $file_name);
3727         if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)!) {
3728                 close $fd;
3729                 return git_blob_plain($mimetype);
3730         }
3731         # we can have blame only for text/* mimetype
3732         $have_blame &&= ($mimetype =~ m!^text/!);
3734         git_header_html(undef, $expires);
3735         my $formats_nav = '';
3736         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3737                 if (defined $file_name) {
3738                         if ($have_blame) {
3739                                 $formats_nav .=
3740                                         $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
3741                                                                hash=>$hash, file_name=>$file_name)},
3742                                                 "blame") .
3743                                         " | ";
3744                         }
3745                         $formats_nav .=
3746                                 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3747                                                        hash=>$hash, file_name=>$file_name)},
3748                                         "history") .
3749                                 " | " .
3750                                 $cgi->a({-href => href(action=>"blob_plain",
3751                                                        hash=>$hash, file_name=>$file_name)},
3752                                         "raw") .
3753                                 " | " .
3754                                 $cgi->a({-href => href(action=>"blob",
3755                                                        hash_base=>"HEAD", file_name=>$file_name)},
3756                                         "HEAD");
3757                 } else {
3758                         $formats_nav .=
3759                                 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
3760                 }
3761                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3762                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3763         } else {
3764                 print "<div class=\"page_nav\">\n" .
3765                       "<br/><br/></div>\n" .
3766                       "<div class=\"title\">$hash</div>\n";
3767         }
3768         git_print_page_path($file_name, "blob", $hash_base);
3769         print "<div class=\"page_body\">\n";
3770         if ($mimetype =~ m!^text/!) {
3771                 my $nr;
3772                 while (my $line = <$fd>) {
3773                         chomp $line;
3774                         $nr++;
3775                         $line = untabify($line);
3776                         printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
3777                                $nr, $nr, $nr, esc_html($line, -nbsp=>1);
3778                 }
3779         } elsif ($mimetype =~ m!^image/!) {
3780                 print qq!<img type="$mimetype"!;
3781                 if ($file_name) {
3782                         print qq! alt="$file_name" title="$file_name"!;
3783                 }
3784                 print qq! src="! .
3785                       href(action=>"blob_plain", hash=>$hash,
3786                            hash_base=>$hash_base, file_name=>$file_name) .
3787                       qq!" />\n!;
3788         }
3789         close $fd
3790                 or print "Reading blob failed.\n";
3791         print "</div>";
3792         git_footer_html();
3795 sub git_tree {
3796         my $have_snapshot = gitweb_have_snapshot();
3798         if (!defined $hash_base) {
3799                 $hash_base = "HEAD";
3800         }
3801         if (!defined $hash) {
3802                 if (defined $file_name) {
3803                         $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
3804                 } else {
3805                         $hash = $hash_base;
3806                 }
3807         }
3808         $/ = "\0";
3809         open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
3810                 or die_error(undef, "Open git-ls-tree failed");
3811         my @entries = map { chomp; $_ } <$fd>;
3812         close $fd or die_error(undef, "Reading tree failed");
3813         $/ = "\n";
3815         my $refs = git_get_references();
3816         my $ref = format_ref_marker($refs, $hash_base);
3817         git_header_html();
3818         my $basedir = '';
3819         my ($have_blame) = gitweb_check_feature('blame');
3820         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3821                 my @views_nav = ();
3822                 if (defined $file_name) {
3823                         push @views_nav,
3824                                 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3825                                                        hash=>$hash, file_name=>$file_name)},
3826                                         "history"),
3827                                 $cgi->a({-href => href(action=>"tree",
3828                                                        hash_base=>"HEAD", file_name=>$file_name)},
3829                                         "HEAD"),
3830                 }
3831                 if ($have_snapshot) {
3832                         # FIXME: Should be available when we have no hash base as well.
3833                         push @views_nav,
3834                                 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
3835                                         "snapshot");
3836                 }
3837                 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
3838                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
3839         } else {
3840                 undef $hash_base;
3841                 print "<div class=\"page_nav\">\n";
3842                 print "<br/><br/></div>\n";
3843                 print "<div class=\"title\">$hash</div>\n";
3844         }
3845         if (defined $file_name) {
3846                 $basedir = $file_name;
3847                 if ($basedir ne '' && substr($basedir, -1) ne '/') {
3848                         $basedir .= '/';
3849                 }
3850         }
3851         git_print_page_path($file_name, 'tree', $hash_base);
3852         print "<div class=\"page_body\">\n";
3853         print "<table cellspacing=\"0\">\n";
3854         my $alternate = 1;
3855         # '..' (top directory) link if possible
3856         if (defined $hash_base &&
3857             defined $file_name && $file_name =~ m![^/]+$!) {
3858                 if ($alternate) {
3859                         print "<tr class=\"dark\">\n";
3860                 } else {
3861                         print "<tr class=\"light\">\n";
3862                 }
3863                 $alternate ^= 1;
3865                 my $up = $file_name;
3866                 $up =~ s!/?[^/]+$!!;
3867                 undef $up unless $up;
3868                 # based on git_print_tree_entry
3869                 print '<td class="mode">' . mode_str('040000') . "</td>\n";
3870                 print '<td class="list">';
3871                 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
3872                                              file_name=>$up)},
3873                               "..");
3874                 print "</td>\n";
3875                 print "<td class=\"link\"></td>\n";
3877                 print "</tr>\n";
3878         }
3879         foreach my $line (@entries) {
3880                 my %t = parse_ls_tree_line($line, -z => 1);
3882                 if ($alternate) {
3883                         print "<tr class=\"dark\">\n";
3884                 } else {
3885                         print "<tr class=\"light\">\n";
3886                 }
3887                 $alternate ^= 1;
3889                 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
3891                 print "</tr>\n";
3892         }
3893         print "</table>\n" .
3894               "</div>";
3895         git_footer_html();
3898 sub git_snapshot {
3899         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
3900         my $have_snapshot = (defined $ctype && defined $suffix);
3901         if (!$have_snapshot) {
3902                 die_error('403 Permission denied', "Permission denied");
3903         }
3905         if (!defined $hash) {
3906                 $hash = git_get_head_hash($project);
3907         }
3909         my $filename = decode_utf8(basename($project)) . "-$hash.tar.$suffix";
3911         print $cgi->header(
3912                 -type => "application/$ctype",
3913                 -content_disposition => 'inline; filename="' . "$filename" . '"',
3914                 -status => '200 OK');
3916         my $git = git_cmd_str();
3917         my $name = $project;
3918         $name =~ s/\047/\047\\\047\047/g;
3919         open my $fd, "-|",
3920                 "$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
3921                 or die_error(undef, "Execute git-tar-tree failed");
3922         binmode STDOUT, ':raw';
3923         print <$fd>;
3924         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
3925         close $fd;
3929 sub git_log {
3930         my $head = git_get_head_hash($project);
3931         if (!defined $hash) {
3932                 $hash = $head;
3933         }
3934         if (!defined $page) {
3935                 $page = 0;
3936         }
3937         my $refs = git_get_references();
3939         my @commitlist = parse_commits($hash, 101, (100 * $page));
3941         my $paging_nav = format_paging_nav('log', $hash, $head, $page, (100 * ($page+1)));
3943         git_header_html();
3944         git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
3946         if (!@commitlist) {
3947                 my %co = parse_commit($hash);
3949                 git_print_header_div('summary', $project);
3950                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
3951         }
3952         my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
3953         for (my $i = 0; $i <= $to; $i++) {
3954                 my %co = %{$commitlist[$i]};
3955                 next if !%co;
3956                 my $commit = $co{'id'};
3957                 my $ref = format_ref_marker($refs, $commit);
3958                 my %ad = parse_date($co{'author_epoch'});
3959                 git_print_header_div('commit',
3960                                "<span class=\"age\">$co{'age_string'}</span>" .
3961                                esc_html($co{'title'}) . $ref,
3962                                $commit);
3963                 print "<div class=\"title_text\">\n" .
3964                       "<div class=\"log_link\">\n" .
3965                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
3966                       " | " .
3967                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
3968                       " | " .
3969                       $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
3970                       "<br/>\n" .
3971                       "</div>\n" .
3972                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
3973                       "</div>\n";
3975                 print "<div class=\"log_body\">\n";
3976                 git_print_log($co{'comment'}, -final_empty_line=> 1);
3977                 print "</div>\n";
3978         }
3979         if ($#commitlist >= 100) {
3980                 print "<div class=\"page_nav\">\n";
3981                 print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
3982                                -accesskey => "n", -title => "Alt-n"}, "next");
3983                 print "</div>\n";
3984         }
3985         git_footer_html();
3988 sub git_commit {
3989         $hash ||= $hash_base || "HEAD";
3990         my %co = parse_commit($hash);
3991         if (!%co) {
3992                 die_error(undef, "Unknown commit object");
3993         }
3994         my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3995         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
3997         my $parent  = $co{'parent'};
3998         my $parents = $co{'parents'}; # listref
4000         # we need to prepare $formats_nav before any parameter munging
4001         my $formats_nav;
4002         if (!defined $parent) {
4003                 # --root commitdiff
4004                 $formats_nav .= '(initial)';
4005         } elsif (@$parents == 1) {
4006                 # single parent commit
4007                 $formats_nav .=
4008                         '(parent: ' .
4009                         $cgi->a({-href => href(action=>"commit",
4010                                                hash=>$parent)},
4011                                 esc_html(substr($parent, 0, 7))) .
4012                         ')';
4013         } else {
4014                 # merge commit
4015                 $formats_nav .=
4016                         '(merge: ' .
4017                         join(' ', map {
4018                                 $cgi->a({-href => href(action=>"commit",
4019                                                        hash=>$_)},
4020                                         esc_html(substr($_, 0, 7)));
4021                         } @$parents ) .
4022                         ')';
4023         }
4025         if (!defined $parent) {
4026                 $parent = "--root";
4027         }
4028         my @difftree;
4029         open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4030                 @diff_opts,
4031                 (@$parents <= 1 ? $parent : '-c'),
4032                 $hash, "--"
4033                 or die_error(undef, "Open git-diff-tree failed");
4034         @difftree = map { chomp; $_ } <$fd>;
4035         close $fd or die_error(undef, "Reading git-diff-tree failed");
4037         # non-textual hash id's can be cached
4038         my $expires;
4039         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4040                 $expires = "+1d";
4041         }
4042         my $refs = git_get_references();
4043         my $ref = format_ref_marker($refs, $co{'id'});
4045         my $have_snapshot = gitweb_have_snapshot();
4047         git_header_html(undef, $expires);
4048         git_print_page_nav('commit', '',
4049                            $hash, $co{'tree'}, $hash,
4050                            $formats_nav);
4052         if (defined $co{'parent'}) {
4053                 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4054         } else {
4055                 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4056         }
4057         print "<div class=\"title_text\">\n" .
4058               "<table cellspacing=\"0\">\n";
4059         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4060               "<tr>" .
4061               "<td></td><td> $ad{'rfc2822'}";
4062         if ($ad{'hour_local'} < 6) {
4063                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4064                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4065         } else {
4066                 printf(" (%02d:%02d %s)",
4067                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4068         }
4069         print "</td>" .
4070               "</tr>\n";
4071         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4072         print "<tr><td></td><td> $cd{'rfc2822'}" .
4073               sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4074               "</td></tr>\n";
4075         print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4076         print "<tr>" .
4077               "<td>tree</td>" .
4078               "<td class=\"sha1\">" .
4079               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4080                        class => "list"}, $co{'tree'}) .
4081               "</td>" .
4082               "<td class=\"link\">" .
4083               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4084                       "tree");
4085         if ($have_snapshot) {
4086                 print " | " .
4087                       $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
4088         }
4089         print "</td>" .
4090               "</tr>\n";
4092         foreach my $par (@$parents) {
4093                 print "<tr>" .
4094                       "<td>parent</td>" .
4095                       "<td class=\"sha1\">" .
4096                       $cgi->a({-href => href(action=>"commit", hash=>$par),
4097                                class => "list"}, $par) .
4098                       "</td>" .
4099                       "<td class=\"link\">" .
4100                       $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4101                       " | " .
4102                       $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4103                       "</td>" .
4104                       "</tr>\n";
4105         }
4106         print "</table>".
4107               "</div>\n";
4109         print "<div class=\"page_body\">\n";
4110         git_print_log($co{'comment'});
4111         print "</div>\n";
4113         git_difftree_body(\@difftree, $hash, @$parents);
4115         git_footer_html();
4118 sub git_object {
4119         # object is defined by:
4120         # - hash or hash_base alone
4121         # - hash_base and file_name
4122         my $type;
4124         # - hash or hash_base alone
4125         if ($hash || ($hash_base && !defined $file_name)) {
4126                 my $object_id = $hash || $hash_base;
4128                 my $git_command = git_cmd_str();
4129                 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4130                         or die_error('404 Not Found', "Object does not exist");
4131                 $type = <$fd>;
4132                 chomp $type;
4133                 close $fd
4134                         or die_error('404 Not Found', "Object does not exist");
4136         # - hash_base and file_name
4137         } elsif ($hash_base && defined $file_name) {
4138                 $file_name =~ s,/+$,,;
4140                 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4141                         or die_error('404 Not Found', "Base object does not exist");
4143                 # here errors should not hapen
4144                 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4145                         or die_error(undef, "Open git-ls-tree failed");
4146                 my $line = <$fd>;
4147                 close $fd;
4149                 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
4150                 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4151                         die_error('404 Not Found', "File or directory for given base does not exist");
4152                 }
4153                 $type = $2;
4154                 $hash = $3;
4155         } else {
4156                 die_error('404 Not Found', "Not enough information to find object");
4157         }
4159         print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4160                                           hash=>$hash, hash_base=>$hash_base,
4161                                           file_name=>$file_name),
4162                              -status => '302 Found');
4165 sub git_blobdiff {
4166         my $format = shift || 'html';
4168         my $fd;
4169         my @difftree;
4170         my %diffinfo;
4171         my $expires;
4173         # preparing $fd and %diffinfo for git_patchset_body
4174         # new style URI
4175         if (defined $hash_base && defined $hash_parent_base) {
4176                 if (defined $file_name) {
4177                         # read raw output
4178                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4179                                 $hash_parent_base, $hash_base,
4180                                 "--", (defined $file_parent ? $file_parent : ()), $file_name
4181                                 or die_error(undef, "Open git-diff-tree failed");
4182                         @difftree = map { chomp; $_ } <$fd>;
4183                         close $fd
4184                                 or die_error(undef, "Reading git-diff-tree failed");
4185                         @difftree
4186                                 or die_error('404 Not Found', "Blob diff not found");
4188                 } elsif (defined $hash &&
4189                          $hash =~ /[0-9a-fA-F]{40}/) {
4190                         # try to find filename from $hash
4192                         # read filtered raw output
4193                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4194                                 $hash_parent_base, $hash_base, "--"
4195                                 or die_error(undef, "Open git-diff-tree failed");
4196                         @difftree =
4197                                 # ':100644 100644 03b21826... 3b93d5e7... M     ls-files.c'
4198                                 # $hash == to_id
4199                                 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4200                                 map { chomp; $_ } <$fd>;
4201                         close $fd
4202                                 or die_error(undef, "Reading git-diff-tree failed");
4203                         @difftree
4204                                 or die_error('404 Not Found', "Blob diff not found");
4206                 } else {
4207                         die_error('404 Not Found', "Missing one of the blob diff parameters");
4208                 }
4210                 if (@difftree > 1) {
4211                         die_error('404 Not Found', "Ambiguous blob diff specification");
4212                 }
4214                 %diffinfo = parse_difftree_raw_line($difftree[0]);
4215                 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
4216                 $file_name   ||= $diffinfo{'to_file'}   || $diffinfo{'file'};
4218                 $hash_parent ||= $diffinfo{'from_id'};
4219                 $hash        ||= $diffinfo{'to_id'};
4221                 # non-textual hash id's can be cached
4222                 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4223                     $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4224                         $expires = '+1d';
4225                 }
4227                 # open patch output
4228                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4229                         '-p', ($format eq 'html' ? "--full-index" : ()),
4230                         $hash_parent_base, $hash_base,
4231                         "--", (defined $file_parent ? $file_parent : ()), $file_name
4232                         or die_error(undef, "Open git-diff-tree failed");
4233         }
4235         # old/legacy style URI
4236         if (!%diffinfo && # if new style URI failed
4237             defined $hash && defined $hash_parent) {
4238                 # fake git-diff-tree raw output
4239                 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4240                 $diffinfo{'from_id'} = $hash_parent;
4241                 $diffinfo{'to_id'}   = $hash;
4242                 if (defined $file_name) {
4243                         if (defined $file_parent) {
4244                                 $diffinfo{'status'} = '2';
4245                                 $diffinfo{'from_file'} = $file_parent;
4246                                 $diffinfo{'to_file'}   = $file_name;
4247                         } else { # assume not renamed
4248                                 $diffinfo{'status'} = '1';
4249                                 $diffinfo{'from_file'} = $file_name;
4250                                 $diffinfo{'to_file'}   = $file_name;
4251                         }
4252                 } else { # no filename given
4253                         $diffinfo{'status'} = '2';
4254                         $diffinfo{'from_file'} = $hash_parent;
4255                         $diffinfo{'to_file'}   = $hash;
4256                 }
4258                 # non-textual hash id's can be cached
4259                 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4260                     $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4261                         $expires = '+1d';
4262                 }
4264                 # open patch output
4265                 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4266                         '-p', ($format eq 'html' ? "--full-index" : ()),
4267                         $hash_parent, $hash, "--"
4268                         or die_error(undef, "Open git-diff failed");
4269         } else  {
4270                 die_error('404 Not Found', "Missing one of the blob diff parameters")
4271                         unless %diffinfo;
4272         }
4274         # header
4275         if ($format eq 'html') {
4276                 my $formats_nav =
4277                         $cgi->a({-href => href(action=>"blobdiff_plain",
4278                                                hash=>$hash, hash_parent=>$hash_parent,
4279                                                hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
4280                                                file_name=>$file_name, file_parent=>$file_parent)},
4281                                 "raw");
4282                 git_header_html(undef, $expires);
4283                 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4284                         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4285                         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4286                 } else {
4287                         print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4288                         print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4289                 }
4290                 if (defined $file_name) {
4291                         git_print_page_path($file_name, "blob", $hash_base);
4292                 } else {
4293                         print "<div class=\"page_path\"></div>\n";
4294                 }
4296         } elsif ($format eq 'plain') {
4297                 print $cgi->header(
4298                         -type => 'text/plain',
4299                         -charset => 'utf-8',
4300                         -expires => $expires,
4301                         -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4303                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4305         } else {
4306                 die_error(undef, "Unknown blobdiff format");
4307         }
4309         # patch
4310         if ($format eq 'html') {
4311                 print "<div class=\"page_body\">\n";
4313                 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4314                 close $fd;
4316                 print "</div>\n"; # class="page_body"
4317                 git_footer_html();
4319         } else {
4320                 while (my $line = <$fd>) {
4321                         $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4322                         $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4324                         print $line;
4326                         last if $line =~ m!^\+\+\+!;
4327                 }
4328                 local $/ = undef;
4329                 print <$fd>;
4330                 close $fd;
4331         }
4334 sub git_blobdiff_plain {
4335         git_blobdiff('plain');
4338 sub git_commitdiff {
4339         my $format = shift || 'html';
4340         $hash ||= $hash_base || "HEAD";
4341         my %co = parse_commit($hash);
4342         if (!%co) {
4343                 die_error(undef, "Unknown commit object");
4344         }
4346         # we need to prepare $formats_nav before any parameter munging
4347         my $formats_nav;
4348         if ($format eq 'html') {
4349                 $formats_nav =
4350                         $cgi->a({-href => href(action=>"commitdiff_plain",
4351                                                hash=>$hash, hash_parent=>$hash_parent)},
4352                                 "raw");
4354                 if (defined $hash_parent) {
4355                         # commitdiff with two commits given
4356                         my $hash_parent_short = $hash_parent;
4357                         if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4358                                 $hash_parent_short = substr($hash_parent, 0, 7);
4359                         }
4360                         $formats_nav .=
4361                                 ' (from: ' .
4362                                 $cgi->a({-href => href(action=>"commitdiff",
4363                                                        hash=>$hash_parent)},
4364                                         esc_html($hash_parent_short)) .
4365                                 ')';
4366                 } elsif (!$co{'parent'}) {
4367                         # --root commitdiff
4368                         $formats_nav .= ' (initial)';
4369                 } elsif (scalar @{$co{'parents'}} == 1) {
4370                         # single parent commit
4371                         $formats_nav .=
4372                                 ' (parent: ' .
4373                                 $cgi->a({-href => href(action=>"commitdiff",
4374                                                        hash=>$co{'parent'})},
4375                                         esc_html(substr($co{'parent'}, 0, 7))) .
4376                                 ')';
4377                 } else {
4378                         # merge commit
4379                         $formats_nav .=
4380                                 ' (merge: ' .
4381                                 join(' ', map {
4382                                         $cgi->a({-href => href(action=>"commitdiff",
4383                                                                hash=>$_)},
4384                                                 esc_html(substr($_, 0, 7)));
4385                                 } @{$co{'parents'}} ) .
4386                                 ')';
4387                 }
4388         }
4390         my $hash_parent_param = $hash_parent;
4391         if (!defined $hash_parent) {
4392                 $hash_parent_param =
4393                         @{$co{'parents'}} > 1 ? '-c' : $co{'parent'} || '--root';
4394         }
4396         # read commitdiff
4397         my $fd;
4398         my @difftree;
4399         if ($format eq 'html') {
4400                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4401                         "--no-commit-id", "--patch-with-raw", "--full-index",
4402                         $hash_parent_param, $hash, "--"
4403                         or die_error(undef, "Open git-diff-tree failed");
4405                 while (my $line = <$fd>) {
4406                         chomp $line;
4407                         # empty line ends raw part of diff-tree output
4408                         last unless $line;
4409                         push @difftree, scalar parse_difftree_raw_line($line);
4410                 }
4412         } elsif ($format eq 'plain') {
4413                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4414                         '-p', $hash_parent_param, $hash, "--"
4415                         or die_error(undef, "Open git-diff-tree failed");
4417         } else {
4418                 die_error(undef, "Unknown commitdiff format");
4419         }
4421         # non-textual hash id's can be cached
4422         my $expires;
4423         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4424                 $expires = "+1d";
4425         }
4427         # write commit message
4428         if ($format eq 'html') {
4429                 my $refs = git_get_references();
4430                 my $ref = format_ref_marker($refs, $co{'id'});
4432                 git_header_html(undef, $expires);
4433                 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
4434                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
4435                 git_print_authorship(\%co);
4436                 print "<div class=\"page_body\">\n";
4437                 if (@{$co{'comment'}} > 1) {
4438                         print "<div class=\"log\">\n";
4439                         git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
4440                         print "</div>\n"; # class="log"
4441                 }
4443         } elsif ($format eq 'plain') {
4444                 my $refs = git_get_references("tags");
4445                 my $tagname = git_get_rev_name_tags($hash);
4446                 my $filename = basename($project) . "-$hash.patch";
4448                 print $cgi->header(
4449                         -type => 'text/plain',
4450                         -charset => 'utf-8',
4451                         -expires => $expires,
4452                         -content_disposition => 'inline; filename="' . "$filename" . '"');
4453                 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4454                 print <<TEXT;
4455 From: $co{'author'}
4456 Date: $ad{'rfc2822'} ($ad{'tz_local'})
4457 Subject: $co{'title'}
4458 TEXT
4459                 print "X-Git-Tag: $tagname\n" if $tagname;
4460                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4462                 foreach my $line (@{$co{'comment'}}) {
4463                         print "$line\n";
4464                 }
4465                 print "---\n\n";
4466         }
4468         # write patch
4469         if ($format eq 'html') {
4470                 git_difftree_body(\@difftree, $hash, $hash_parent || @{$co{'parents'}});
4471                 print "<br/>\n";
4473                 git_patchset_body($fd, \@difftree, $hash, $hash_parent || @{$co{'parents'}});
4474                 close $fd;
4475                 print "</div>\n"; # class="page_body"
4476                 git_footer_html();
4478         } elsif ($format eq 'plain') {
4479                 local $/ = undef;
4480                 print <$fd>;
4481                 close $fd
4482                         or print "Reading git-diff-tree failed\n";
4483         }
4486 sub git_commitdiff_plain {
4487         git_commitdiff('plain');
4490 sub git_history {
4491         if (!defined $hash_base) {
4492                 $hash_base = git_get_head_hash($project);
4493         }
4494         if (!defined $page) {
4495                 $page = 0;
4496         }
4497         my $ftype;
4498         my %co = parse_commit($hash_base);
4499         if (!%co) {
4500                 die_error(undef, "Unknown commit object");
4501         }
4503         my $refs = git_get_references();
4504         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
4506         if (!defined $hash && defined $file_name) {
4507                 $hash = git_get_hash_by_path($hash_base, $file_name);
4508         }
4509         if (defined $hash) {
4510                 $ftype = git_get_type($hash);
4511         }
4513         my @commitlist = parse_commits($hash_base, 101, (100 * $page), "--full-history", $file_name);
4515         my $paging_nav = '';
4516         if ($page > 0) {
4517                 $paging_nav .=
4518                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4519                                                file_name=>$file_name)},
4520                                 "first");
4521                 $paging_nav .= " &sdot; " .
4522                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4523                                                file_name=>$file_name, page=>$page-1),
4524                                  -accesskey => "p", -title => "Alt-p"}, "prev");
4525         } else {
4526                 $paging_nav .= "first";
4527                 $paging_nav .= " &sdot; prev";
4528         }
4529         if ($#commitlist >= 100) {
4530                 $paging_nav .= " &sdot; " .
4531                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4532                                                file_name=>$file_name, page=>$page+1),
4533                                  -accesskey => "n", -title => "Alt-n"}, "next");
4534         } else {
4535                 $paging_nav .= " &sdot; next";
4536         }
4537         my $next_link = '';
4538         if ($#commitlist >= 100) {
4539                 $next_link =
4540                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
4541                                                file_name=>$file_name, page=>$page+1),
4542                                  -accesskey => "n", -title => "Alt-n"}, "next");
4543         }
4545         git_header_html();
4546         git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
4547         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4548         git_print_page_path($file_name, $ftype, $hash_base);
4550         git_history_body(\@commitlist, 0, 99,
4551                          $refs, $hash_base, $ftype, $next_link);
4553         git_footer_html();
4556 sub git_search {
4557         my ($have_search) = gitweb_check_feature('search');
4558         if (!$have_search) {
4559                 die_error('403 Permission denied', "Permission denied");
4560         }
4561         if (!defined $searchtext) {
4562                 die_error(undef, "Text field empty");
4563         }
4564         if (!defined $hash) {
4565                 $hash = git_get_head_hash($project);
4566         }
4567         my %co = parse_commit($hash);
4568         if (!%co) {
4569                 die_error(undef, "Unknown commit object");
4570         }
4571         if (!defined $page) {
4572                 $page = 0;
4573         }
4575         $searchtype ||= 'commit';
4576         if ($searchtype eq 'pickaxe') {
4577                 # pickaxe may take all resources of your box and run for several minutes
4578                 # with every query - so decide by yourself how public you make this feature
4579                 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4580                 if (!$have_pickaxe) {
4581                         die_error('403 Permission denied', "Permission denied");
4582                 }
4583         }
4585         git_header_html();
4587         if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
4588                 my $greptype;
4589                 if ($searchtype eq 'commit') {
4590                         $greptype = "--grep=";
4591                 } elsif ($searchtype eq 'author') {
4592                         $greptype = "--author=";
4593                 } elsif ($searchtype eq 'committer') {
4594                         $greptype = "--committer=";
4595                 }
4596                 $greptype .= $searchtext;
4597                 my @commitlist = parse_commits($hash, 101, (100 * $page), $greptype);
4599                 my $paging_nav = '';
4600                 if ($page > 0) {
4601                         $paging_nav .=
4602                                 $cgi->a({-href => href(action=>"search", hash=>$hash,
4603                                                        searchtext=>$searchtext, searchtype=>$searchtype)},
4604                                         "first");
4605                         $paging_nav .= " &sdot; " .
4606                                 $cgi->a({-href => href(action=>"search", hash=>$hash,
4607                                                        searchtext=>$searchtext, searchtype=>$searchtype,
4608                                                        page=>$page-1),
4609                                          -accesskey => "p", -title => "Alt-p"}, "prev");
4610                 } else {
4611                         $paging_nav .= "first";
4612                         $paging_nav .= " &sdot; prev";
4613                 }
4614                 if ($#commitlist >= 100) {
4615                         $paging_nav .= " &sdot; " .
4616                                 $cgi->a({-href => href(action=>"search", hash=>$hash,
4617                                                        searchtext=>$searchtext, searchtype=>$searchtype,
4618                                                        page=>$page+1),
4619                                          -accesskey => "n", -title => "Alt-n"}, "next");
4620                 } else {
4621                         $paging_nav .= " &sdot; next";
4622                 }
4623                 my $next_link = '';
4624                 if ($#commitlist >= 100) {
4625                         $next_link =
4626                                 $cgi->a({-href => href(action=>"search", hash=>$hash,
4627                                                        searchtext=>$searchtext, searchtype=>$searchtype,
4628                                                        page=>$page+1),
4629                                          -accesskey => "n", -title => "Alt-n"}, "next");
4630                 }
4632                 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
4633                 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4634                 git_search_grep_body(\@commitlist, 0, 99, $next_link);
4635         }
4637         if ($searchtype eq 'pickaxe') {
4638                 git_print_page_nav('','', $hash,$co{'tree'},$hash);
4639                 git_print_header_div('commit', esc_html($co{'title'}), $hash);
4641                 print "<table cellspacing=\"0\">\n";
4642                 my $alternate = 1;
4643                 $/ = "\n";
4644                 my $git_command = git_cmd_str();
4645                 open my $fd, "-|", "$git_command rev-list $hash | " .
4646                         "$git_command diff-tree -r --stdin -S\'$searchtext\'";
4647                 undef %co;
4648                 my @files;
4649                 while (my $line = <$fd>) {
4650                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
4651                                 my %set;
4652                                 $set{'file'} = $6;
4653                                 $set{'from_id'} = $3;
4654                                 $set{'to_id'} = $4;
4655                                 $set{'id'} = $set{'to_id'};
4656                                 if ($set{'id'} =~ m/0{40}/) {
4657                                         $set{'id'} = $set{'from_id'};
4658                                 }
4659                                 if ($set{'id'} =~ m/0{40}/) {
4660                                         next;
4661                                 }
4662                                 push @files, \%set;
4663                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
4664                                 if (%co) {
4665                                         if ($alternate) {
4666                                                 print "<tr class=\"dark\">\n";
4667                                         } else {
4668                                                 print "<tr class=\"light\">\n";
4669                                         }
4670                                         $alternate ^= 1;
4671                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4672                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
4673                                               "<td>" .
4674                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4675                                                       -class => "list subject"},
4676                                                       esc_html(chop_str($co{'title'}, 50)) . "<br/>");
4677                                         while (my $setref = shift @files) {
4678                                                 my %set = %$setref;
4679                                                 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
4680                                                                              hash=>$set{'id'}, file_name=>$set{'file'}),
4681                                                               -class => "list"},
4682                                                               "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
4683                                                       "<br/>\n";
4684                                         }
4685                                         print "</td>\n" .
4686                                               "<td class=\"link\">" .
4687                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4688                                               " | " .
4689                                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4690                                         print "</td>\n" .
4691                                               "</tr>\n";
4692                                 }
4693                                 %co = parse_commit($1);
4694                         }
4695                 }
4696                 close $fd;
4698                 print "</table>\n";
4699         }
4700         git_footer_html();
4703 sub git_search_help {
4704         git_header_html();
4705         git_print_page_nav('','', $hash,$hash,$hash);
4706         print <<EOT;
4707 <dl>
4708 <dt><b>commit</b></dt>
4709 <dd>The commit messages and authorship information will be scanned for the given string.</dd>
4710 <dt><b>author</b></dt>
4711 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given string.</dd>
4712 <dt><b>committer</b></dt>
4713 <dd>Name and e-mail of the committer and date of commit will be scanned for the given string.</dd>
4714 EOT
4715         my ($have_pickaxe) = gitweb_check_feature('pickaxe');
4716         if ($have_pickaxe) {
4717                 print <<EOT;
4718 <dt><b>pickaxe</b></dt>
4719 <dd>All commits that caused the string to appear or disappear from any file (changes that
4720 added, removed or "modified" the string) will be listed. This search can take a while and
4721 takes a lot of strain on the server, so please use it wisely.</dd>
4722 EOT
4723         }
4724         print "</dl>\n";
4725         git_footer_html();
4728 sub git_shortlog {
4729         my $head = git_get_head_hash($project);
4730         if (!defined $hash) {
4731                 $hash = $head;
4732         }
4733         if (!defined $page) {
4734                 $page = 0;
4735         }
4736         my $refs = git_get_references();
4738         my @commitlist = parse_commits($hash, 101, (100 * $page));
4740         my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, (100 * ($page+1)));
4741         my $next_link = '';
4742         if ($#commitlist >= 100) {
4743                 $next_link =
4744                         $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
4745                                  -accesskey => "n", -title => "Alt-n"}, "next");
4746         }
4748         git_header_html();
4749         git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
4750         git_print_header_div('summary', $project);
4752         git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
4754         git_footer_html();
4757 ## ......................................................................
4758 ## feeds (RSS, Atom; OPML)
4760 sub git_feed {
4761         my $format = shift || 'atom';
4762         my ($have_blame) = gitweb_check_feature('blame');
4764         # Atom: http://www.atomenabled.org/developers/syndication/
4765         # RSS:  http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
4766         if ($format ne 'rss' && $format ne 'atom') {
4767                 die_error(undef, "Unknown web feed format");
4768         }
4770         # log/feed of current (HEAD) branch, log of given branch, history of file/directory
4771         my $head = $hash || 'HEAD';
4772         my @commitlist = parse_commits($head, 150);
4774         my %latest_commit;
4775         my %latest_date;
4776         my $content_type = "application/$format+xml";
4777         if (defined $cgi->http('HTTP_ACCEPT') &&
4778                  $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
4779                 # browser (feed reader) prefers text/xml
4780                 $content_type = 'text/xml';
4781         }
4782         if (defined($commitlist[0])) {
4783                 %latest_commit = %{$commitlist[0]};
4784                 %latest_date   = parse_date($latest_commit{'author_epoch'});
4785                 print $cgi->header(
4786                         -type => $content_type,
4787                         -charset => 'utf-8',
4788                         -last_modified => $latest_date{'rfc2822'});
4789         } else {
4790                 print $cgi->header(
4791                         -type => $content_type,
4792                         -charset => 'utf-8');
4793         }
4795         # Optimization: skip generating the body if client asks only
4796         # for Last-Modified date.
4797         return if ($cgi->request_method() eq 'HEAD');
4799         # header variables
4800         my $title = "$site_name - $project/$action";
4801         my $feed_type = 'log';
4802         if (defined $hash) {
4803                 $title .= " - '$hash'";
4804                 $feed_type = 'branch log';
4805                 if (defined $file_name) {
4806                         $title .= " :: $file_name";
4807                         $feed_type = 'history';
4808                 }
4809         } elsif (defined $file_name) {
4810                 $title .= " - $file_name";
4811                 $feed_type = 'history';
4812         }
4813         $title .= " $feed_type";
4814         my $descr = git_get_project_description($project);
4815         if (defined $descr) {
4816                 $descr = esc_html($descr);
4817         } else {
4818                 $descr = "$project " .
4819                          ($format eq 'rss' ? 'RSS' : 'Atom') .
4820                          " feed";
4821         }
4822         my $owner = git_get_project_owner($project);
4823         $owner = esc_html($owner);
4825         #header
4826         my $alt_url;
4827         if (defined $file_name) {
4828                 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
4829         } elsif (defined $hash) {
4830                 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
4831         } else {
4832                 $alt_url = href(-full=>1, action=>"summary");
4833         }
4834         print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
4835         if ($format eq 'rss') {
4836                 print <<XML;
4837 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
4838 <channel>
4839 XML
4840                 print "<title>$title</title>\n" .
4841                       "<link>$alt_url</link>\n" .
4842                       "<description>$descr</description>\n" .
4843                       "<language>en</language>\n";
4844         } elsif ($format eq 'atom') {
4845                 print <<XML;
4846 <feed xmlns="http://www.w3.org/2005/Atom">
4847 XML
4848                 print "<title>$title</title>\n" .
4849                       "<subtitle>$descr</subtitle>\n" .
4850                       '<link rel="alternate" type="text/html" href="' .
4851                       $alt_url . '" />' . "\n" .
4852                       '<link rel="self" type="' . $content_type . '" href="' .
4853                       $cgi->self_url() . '" />' . "\n" .
4854                       "<id>" . href(-full=>1) . "</id>\n" .
4855                       # use project owner for feed author
4856                       "<author><name>$owner</name></author>\n";
4857                 if (defined $favicon) {
4858                         print "<icon>" . esc_url($favicon) . "</icon>\n";
4859                 }
4860                 if (defined $logo_url) {
4861                         # not twice as wide as tall: 72 x 27 pixels
4862                         print "<logo>" . esc_url($logo) . "</logo>\n";
4863                 }
4864                 if (! %latest_date) {
4865                         # dummy date to keep the feed valid until commits trickle in:
4866                         print "<updated>1970-01-01T00:00:00Z</updated>\n";
4867                 } else {
4868                         print "<updated>$latest_date{'iso-8601'}</updated>\n";
4869                 }
4870         }
4872         # contents
4873         for (my $i = 0; $i <= $#commitlist; $i++) {
4874                 my %co = %{$commitlist[$i]};
4875                 my $commit = $co{'id'};
4876                 # we read 150, we always show 30 and the ones more recent than 48 hours
4877                 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
4878                         last;
4879                 }
4880                 my %cd = parse_date($co{'author_epoch'});
4882                 # get list of changed files
4883                 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4884                         $co{'parent'}, $co{'id'}, "--", (defined $file_name ? $file_name : ())
4885                         or next;
4886                 my @difftree = map { chomp; $_ } <$fd>;
4887                 close $fd
4888                         or next;
4890                 # print element (entry, item)
4891                 my $co_url = href(-full=>1, action=>"commit", hash=>$commit);
4892                 if ($format eq 'rss') {
4893                         print "<item>\n" .
4894                               "<title>" . esc_html($co{'title'}) . "</title>\n" .
4895                               "<author>" . esc_html($co{'author'}) . "</author>\n" .
4896                               "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
4897                               "<guid isPermaLink=\"true\">$co_url</guid>\n" .
4898                               "<link>$co_url</link>\n" .
4899                               "<description>" . esc_html($co{'title'}) . "</description>\n" .
4900                               "<content:encoded>" .
4901                               "<![CDATA[\n";
4902                 } elsif ($format eq 'atom') {
4903                         print "<entry>\n" .
4904                               "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
4905                               "<updated>$cd{'iso-8601'}</updated>\n" .
4906                               "<author>\n" .
4907                               "  <name>" . esc_html($co{'author_name'}) . "</name>\n";
4908                         if ($co{'author_email'}) {
4909                                 print "  <email>" . esc_html($co{'author_email'}) . "</email>\n";
4910                         }
4911                         print "</author>\n" .
4912                               # use committer for contributor
4913                               "<contributor>\n" .
4914                               "  <name>" . esc_html($co{'committer_name'}) . "</name>\n";
4915                         if ($co{'committer_email'}) {
4916                                 print "  <email>" . esc_html($co{'committer_email'}) . "</email>\n";
4917                         }
4918                         print "</contributor>\n" .
4919                               "<published>$cd{'iso-8601'}</published>\n" .
4920                               "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
4921                               "<id>$co_url</id>\n" .
4922                               "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
4923                               "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
4924                 }
4925                 my $comment = $co{'comment'};
4926                 print "<pre>\n";
4927                 foreach my $line (@$comment) {
4928                         $line = esc_html($line);
4929                         print "$line\n";
4930                 }
4931                 print "</pre><ul>\n";
4932                 foreach my $difftree_line (@difftree) {
4933                         my %difftree = parse_difftree_raw_line($difftree_line);
4934                         next if !$difftree{'from_id'};
4936                         my $file = $difftree{'file'} || $difftree{'to_file'};
4938                         print "<li>" .
4939                               "[" .
4940                               $cgi->a({-href => href(-full=>1, action=>"blobdiff",
4941                                                      hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
4942                                                      hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
4943                                                      file_name=>$file, file_parent=>$difftree{'from_file'}),
4944                                       -title => "diff"}, 'D');
4945                         if ($have_blame) {
4946                                 print $cgi->a({-href => href(-full=>1, action=>"blame",
4947                                                              file_name=>$file, hash_base=>$commit),
4948                                               -title => "blame"}, 'B');
4949                         }
4950                         # if this is not a feed of a file history
4951                         if (!defined $file_name || $file_name ne $file) {
4952                                 print $cgi->a({-href => href(-full=>1, action=>"history",
4953                                                              file_name=>$file, hash=>$commit),
4954                                               -title => "history"}, 'H');
4955                         }
4956                         $file = esc_path($file);
4957                         print "] ".
4958                               "$file</li>\n";
4959                 }
4960                 if ($format eq 'rss') {
4961                         print "</ul>]]>\n" .
4962                               "</content:encoded>\n" .
4963                               "</item>\n";
4964                 } elsif ($format eq 'atom') {
4965                         print "</ul>\n</div>\n" .
4966                               "</content>\n" .
4967                               "</entry>\n";
4968                 }
4969         }
4971         # end of feed
4972         if ($format eq 'rss') {
4973                 print "</channel>\n</rss>\n";
4974         }       elsif ($format eq 'atom') {
4975                 print "</feed>\n";
4976         }
4979 sub git_rss {
4980         git_feed('rss');
4983 sub git_atom {
4984         git_feed('atom');
4987 sub git_opml {
4988         my @list = git_get_projects_list();
4990         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
4991         print <<XML;
4992 <?xml version="1.0" encoding="utf-8"?>
4993 <opml version="1.0">
4994 <head>
4995   <title>$site_name OPML Export</title>
4996 </head>
4997 <body>
4998 <outline text="git RSS feeds">
4999 XML
5001         foreach my $pr (@list) {
5002                 my %proj = %$pr;
5003                 my $head = git_get_head_hash($proj{'path'});
5004                 if (!defined $head) {
5005                         next;
5006                 }
5007                 $git_dir = "$projectroot/$proj{'path'}";
5008                 my %co = parse_commit($head);
5009                 if (!%co) {
5010                         next;
5011                 }
5013                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5014                 my $rss  = "$my_url?p=$proj{'path'};a=rss";
5015                 my $html = "$my_url?p=$proj{'path'};a=summary";
5016                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5017         }
5018         print <<XML;
5019 </outline>
5020 </body>
5021 </opml>
5022 XML