Code

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