55fb1005340e4fc483a9280b09f1f59c896eed65
1#!/usr/bin/perl
2
3# gitweb - simple web interface to track changes in git repositories
4#
5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6# (C) 2005, Christian Gierke
7#
8# This program is licensed under the GPLv2
9
10use strict;
11use warnings;
12use CGI qw(:standard :escapeHTML -nosticky);
13use CGI::Util qw(unescape);
14use CGI::Carp qw(fatalsToBrowser);
15use Encode;
16use Fcntl ':mode';
17use File::Find qw();
18use File::Basename qw(basename);
19binmode STDOUT, ':utf8';
20
21BEGIN {
22 CGI->compile() if $ENV{'MOD_PERL'};
23}
24
25our $cgi = new CGI;
26our $version = "++GIT_VERSION++";
27our $my_url = $cgi->url();
28our $my_uri = $cgi->url(-absolute => 1);
29
30# core git executable to use
31# this can just be "git" if your webserver has a sensible PATH
32our $GIT = "++GIT_BINDIR++/git";
33
34# absolute fs-path which will be prepended to the project path
35#our $projectroot = "/pub/scm";
36our $projectroot = "++GITWEB_PROJECTROOT++";
37
38# fs traversing limit for getting project list
39# the number is relative to the projectroot
40our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
41
42# target of the home link on top of all pages
43our $home_link = $my_uri || "/";
44
45# string of the home link on top of all pages
46our $home_link_str = "++GITWEB_HOME_LINK_STR++";
47
48# name of your site or organization to appear in page titles
49# replace this with something more descriptive for clearer bookmarks
50our $site_name = "++GITWEB_SITENAME++"
51 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
52
53# filename of html text to include at top of each page
54our $site_header = "++GITWEB_SITE_HEADER++";
55# html text to include at home page
56our $home_text = "++GITWEB_HOMETEXT++";
57# filename of html text to include at bottom of each page
58our $site_footer = "++GITWEB_SITE_FOOTER++";
59
60# URI of stylesheets
61our @stylesheets = ("++GITWEB_CSS++");
62# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
63our $stylesheet = undef;
64# URI of GIT logo (72x27 size)
65our $logo = "++GITWEB_LOGO++";
66# URI of GIT favicon, assumed to be image/png type
67our $favicon = "++GITWEB_FAVICON++";
68
69# URI and label (title) of GIT logo link
70#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
71#our $logo_label = "git documentation";
72our $logo_url = "http://git.or.cz/";
73our $logo_label = "git homepage";
74
75# source of projects list
76our $projects_list = "++GITWEB_LIST++";
77
78# the width (in characters) of the projects list "Description" column
79our $projects_list_description_width = 25;
80
81# default order of projects list
82# valid values are none, project, descr, owner, and age
83our $default_projects_order = "project";
84
85# show repository only if this file exists
86# (only effective if this variable evaluates to true)
87our $export_ok = "++GITWEB_EXPORT_OK++";
88
89# only allow viewing of repositories also shown on the overview page
90our $strict_export = "++GITWEB_STRICT_EXPORT++";
91
92# list of git base URLs used for URL to where fetch project from,
93# i.e. full URL is "$git_base_url/$project"
94our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
95
96# default blob_plain mimetype and default charset for text/plain blob
97our $default_blob_plain_mimetype = 'text/plain';
98our $default_text_plain_charset = undef;
99
100# file to use for guessing MIME types before trying /etc/mime.types
101# (relative to the current git repository)
102our $mimetypes_file = undef;
103
104# assume this charset if line contains non-UTF-8 characters;
105# it should be valid encoding (see Encoding::Supported(3pm) for list),
106# for which encoding all byte sequences are valid, for example
107# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
108# could be even 'utf-8' for the old behavior)
109our $fallback_encoding = 'latin1';
110
111# rename detection options for git-diff and git-diff-tree
112# - default is '-M', with the cost proportional to
113# (number of removed files) * (number of new files).
114# - more costly is '-C' (which implies '-M'), with the cost proportional to
115# (number of changed files + number of removed files) * (number of new files)
116# - even more costly is '-C', '--find-copies-harder' with cost
117# (number of files in the original tree) * (number of new files)
118# - one might want to include '-B' option, e.g. '-B', '-M'
119our @diff_opts = ('-M'); # taken from git_commit
120
121# information about snapshot formats that gitweb is capable of serving
122our %known_snapshot_formats = (
123 # name => {
124 # 'display' => display name,
125 # 'type' => mime type,
126 # 'suffix' => filename suffix,
127 # 'format' => --format for git-archive,
128 # 'compressor' => [compressor command and arguments]
129 # (array reference, optional)}
130 #
131 'tgz' => {
132 'display' => 'tar.gz',
133 'type' => 'application/x-gzip',
134 'suffix' => '.tar.gz',
135 'format' => 'tar',
136 'compressor' => ['gzip']},
137
138 'tbz2' => {
139 'display' => 'tar.bz2',
140 'type' => 'application/x-bzip2',
141 'suffix' => '.tar.bz2',
142 'format' => 'tar',
143 'compressor' => ['bzip2']},
144
145 'zip' => {
146 'display' => 'zip',
147 'type' => 'application/x-zip',
148 'suffix' => '.zip',
149 'format' => 'zip'},
150);
151
152# Aliases so we understand old gitweb.snapshot values in repository
153# configuration.
154our %known_snapshot_format_aliases = (
155 'gzip' => 'tgz',
156 'bzip2' => 'tbz2',
157
158 # backward compatibility: legacy gitweb config support
159 'x-gzip' => undef, 'gz' => undef,
160 'x-bzip2' => undef, 'bz2' => undef,
161 'x-zip' => undef, '' => undef,
162);
163
164# You define site-wide feature defaults here; override them with
165# $GITWEB_CONFIG as necessary.
166our %feature = (
167 # feature => {
168 # 'sub' => feature-sub (subroutine),
169 # 'override' => allow-override (boolean),
170 # 'default' => [ default options...] (array reference)}
171 #
172 # if feature is overridable (it means that allow-override has true value),
173 # then feature-sub will be called with default options as parameters;
174 # return value of feature-sub indicates if to enable specified feature
175 #
176 # if there is no 'sub' key (no feature-sub), then feature cannot be
177 # overriden
178 #
179 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
180
181 # Enable the 'blame' blob view, showing the last commit that modified
182 # each line in the file. This can be very CPU-intensive.
183
184 # To enable system wide have in $GITWEB_CONFIG
185 # $feature{'blame'}{'default'} = [1];
186 # To have project specific config enable override in $GITWEB_CONFIG
187 # $feature{'blame'}{'override'} = 1;
188 # and in project config gitweb.blame = 0|1;
189 'blame' => {
190 'sub' => \&feature_blame,
191 'override' => 0,
192 'default' => [0]},
193
194 # Enable the 'snapshot' link, providing a compressed archive of any
195 # tree. This can potentially generate high traffic if you have large
196 # project.
197
198 # Value is a list of formats defined in %known_snapshot_formats that
199 # you wish to offer.
200 # To disable system wide have in $GITWEB_CONFIG
201 # $feature{'snapshot'}{'default'} = [];
202 # To have project specific config enable override in $GITWEB_CONFIG
203 # $feature{'snapshot'}{'override'} = 1;
204 # and in project config, a comma-separated list of formats or "none"
205 # to disable. Example: gitweb.snapshot = tbz2,zip;
206 'snapshot' => {
207 'sub' => \&feature_snapshot,
208 'override' => 0,
209 'default' => ['tgz']},
210
211 # Enable text search, which will list the commits which match author,
212 # committer or commit text to a given string. Enabled by default.
213 # Project specific override is not supported.
214 'search' => {
215 'override' => 0,
216 'default' => [1]},
217
218 # Enable grep search, which will list the files in currently selected
219 # tree containing the given string. Enabled by default. This can be
220 # potentially CPU-intensive, of course.
221
222 # To enable system wide have in $GITWEB_CONFIG
223 # $feature{'grep'}{'default'} = [1];
224 # To have project specific config enable override in $GITWEB_CONFIG
225 # $feature{'grep'}{'override'} = 1;
226 # and in project config gitweb.grep = 0|1;
227 'grep' => {
228 'override' => 0,
229 'default' => [1]},
230
231 # Enable the pickaxe search, which will list the commits that modified
232 # a given string in a file. This can be practical and quite faster
233 # alternative to 'blame', but still potentially CPU-intensive.
234
235 # To enable system wide have in $GITWEB_CONFIG
236 # $feature{'pickaxe'}{'default'} = [1];
237 # To have project specific config enable override in $GITWEB_CONFIG
238 # $feature{'pickaxe'}{'override'} = 1;
239 # and in project config gitweb.pickaxe = 0|1;
240 'pickaxe' => {
241 'sub' => \&feature_pickaxe,
242 'override' => 0,
243 'default' => [1]},
244
245 # Make gitweb use an alternative format of the URLs which can be
246 # more readable and natural-looking: project name is embedded
247 # directly in the path and the query string contains other
248 # auxiliary information. All gitweb installations recognize
249 # URL in either format; this configures in which formats gitweb
250 # generates links.
251
252 # To enable system wide have in $GITWEB_CONFIG
253 # $feature{'pathinfo'}{'default'} = [1];
254 # Project specific override is not supported.
255
256 # Note that you will need to change the default location of CSS,
257 # favicon, logo and possibly other files to an absolute URL. Also,
258 # if gitweb.cgi serves as your indexfile, you will need to force
259 # $my_uri to contain the script name in your $GITWEB_CONFIG.
260 'pathinfo' => {
261 'override' => 0,
262 'default' => [0]},
263
264 # Make gitweb consider projects in project root subdirectories
265 # to be forks of existing projects. Given project $projname.git,
266 # projects matching $projname/*.git will not be shown in the main
267 # projects list, instead a '+' mark will be added to $projname
268 # there and a 'forks' view will be enabled for the project, listing
269 # all the forks. If project list is taken from a file, forks have
270 # to be listed after the main project.
271
272 # To enable system wide have in $GITWEB_CONFIG
273 # $feature{'forks'}{'default'} = [1];
274 # Project specific override is not supported.
275 'forks' => {
276 'override' => 0,
277 'default' => [0]},
278);
279
280sub gitweb_check_feature {
281 my ($name) = @_;
282 return unless exists $feature{$name};
283 my ($sub, $override, @defaults) = (
284 $feature{$name}{'sub'},
285 $feature{$name}{'override'},
286 @{$feature{$name}{'default'}});
287 if (!$override) { return @defaults; }
288 if (!defined $sub) {
289 warn "feature $name is not overrideable";
290 return @defaults;
291 }
292 return $sub->(@defaults);
293}
294
295sub feature_blame {
296 my ($val) = git_get_project_config('blame', '--bool');
297
298 if ($val eq 'true') {
299 return 1;
300 } elsif ($val eq 'false') {
301 return 0;
302 }
303
304 return $_[0];
305}
306
307sub feature_snapshot {
308 my (@fmts) = @_;
309
310 my ($val) = git_get_project_config('snapshot');
311
312 if ($val) {
313 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
314 }
315
316 return @fmts;
317}
318
319sub feature_grep {
320 my ($val) = git_get_project_config('grep', '--bool');
321
322 if ($val eq 'true') {
323 return (1);
324 } elsif ($val eq 'false') {
325 return (0);
326 }
327
328 return ($_[0]);
329}
330
331sub feature_pickaxe {
332 my ($val) = git_get_project_config('pickaxe', '--bool');
333
334 if ($val eq 'true') {
335 return (1);
336 } elsif ($val eq 'false') {
337 return (0);
338 }
339
340 return ($_[0]);
341}
342
343# checking HEAD file with -e is fragile if the repository was
344# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
345# and then pruned.
346sub check_head_link {
347 my ($dir) = @_;
348 my $headfile = "$dir/HEAD";
349 return ((-e $headfile) ||
350 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
351}
352
353sub check_export_ok {
354 my ($dir) = @_;
355 return (check_head_link($dir) &&
356 (!$export_ok || -e "$dir/$export_ok"));
357}
358
359# process alternate names for backward compatibility
360# filter out unsupported (unknown) snapshot formats
361sub filter_snapshot_fmts {
362 my @fmts = @_;
363
364 @fmts = map {
365 exists $known_snapshot_format_aliases{$_} ?
366 $known_snapshot_format_aliases{$_} : $_} @fmts;
367 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
368
369}
370
371our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
372if (-e $GITWEB_CONFIG) {
373 do $GITWEB_CONFIG;
374} else {
375 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
376 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
377}
378
379# version of the core git binary
380our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
381
382$projects_list ||= $projectroot;
383
384# ======================================================================
385# input validation and dispatch
386our $action = $cgi->param('a');
387if (defined $action) {
388 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
389 die_error(undef, "Invalid action parameter");
390 }
391}
392
393# parameters which are pathnames
394our $project = $cgi->param('p');
395if (defined $project) {
396 if (!validate_pathname($project) ||
397 !(-d "$projectroot/$project") ||
398 !check_head_link("$projectroot/$project") ||
399 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
400 ($strict_export && !project_in_list($project))) {
401 undef $project;
402 die_error(undef, "No such project");
403 }
404}
405
406our $file_name = $cgi->param('f');
407if (defined $file_name) {
408 if (!validate_pathname($file_name)) {
409 die_error(undef, "Invalid file parameter");
410 }
411}
412
413our $file_parent = $cgi->param('fp');
414if (defined $file_parent) {
415 if (!validate_pathname($file_parent)) {
416 die_error(undef, "Invalid file parent parameter");
417 }
418}
419
420# parameters which are refnames
421our $hash = $cgi->param('h');
422if (defined $hash) {
423 if (!validate_refname($hash)) {
424 die_error(undef, "Invalid hash parameter");
425 }
426}
427
428our $hash_parent = $cgi->param('hp');
429if (defined $hash_parent) {
430 if (!validate_refname($hash_parent)) {
431 die_error(undef, "Invalid hash parent parameter");
432 }
433}
434
435our $hash_base = $cgi->param('hb');
436if (defined $hash_base) {
437 if (!validate_refname($hash_base)) {
438 die_error(undef, "Invalid hash base parameter");
439 }
440}
441
442my %allowed_options = (
443 "--no-merges" => [ qw(rss atom log shortlog history) ],
444);
445
446our @extra_options = $cgi->param('opt');
447if (defined @extra_options) {
448 foreach my $opt (@extra_options) {
449 if (not exists $allowed_options{$opt}) {
450 die_error(undef, "Invalid option parameter");
451 }
452 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
453 die_error(undef, "Invalid option parameter for this action");
454 }
455 }
456}
457
458our $hash_parent_base = $cgi->param('hpb');
459if (defined $hash_parent_base) {
460 if (!validate_refname($hash_parent_base)) {
461 die_error(undef, "Invalid hash parent base parameter");
462 }
463}
464
465# other parameters
466our $page = $cgi->param('pg');
467if (defined $page) {
468 if ($page =~ m/[^0-9]/) {
469 die_error(undef, "Invalid page parameter");
470 }
471}
472
473our $searchtype = $cgi->param('st');
474if (defined $searchtype) {
475 if ($searchtype =~ m/[^a-z]/) {
476 die_error(undef, "Invalid searchtype parameter");
477 }
478}
479
480our $search_use_regexp = $cgi->param('sr');
481
482our $searchtext = $cgi->param('s');
483our $search_regexp;
484if (defined $searchtext) {
485 if (length($searchtext) < 2) {
486 die_error(undef, "At least two characters are required for search parameter");
487 }
488 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
489}
490
491# now read PATH_INFO and use it as alternative to parameters
492sub evaluate_path_info {
493 return if defined $project;
494 my $path_info = $ENV{"PATH_INFO"};
495 return if !$path_info;
496 $path_info =~ s,^/+,,;
497 return if !$path_info;
498 # find which part of PATH_INFO is project
499 $project = $path_info;
500 $project =~ s,/+$,,;
501 while ($project && !check_head_link("$projectroot/$project")) {
502 $project =~ s,/*[^/]*$,,;
503 }
504 # validate project
505 $project = validate_pathname($project);
506 if (!$project ||
507 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
508 ($strict_export && !project_in_list($project))) {
509 undef $project;
510 return;
511 }
512 # do not change any parameters if an action is given using the query string
513 return if $action;
514 $path_info =~ s,^\Q$project\E/*,,;
515 my ($refname, $pathname) = split(/:/, $path_info, 2);
516 if (defined $pathname) {
517 # we got "project.git/branch:filename" or "project.git/branch:dir/"
518 # we could use git_get_type(branch:pathname), but it needs $git_dir
519 $pathname =~ s,^/+,,;
520 if (!$pathname || substr($pathname, -1) eq "/") {
521 $action ||= "tree";
522 $pathname =~ s,/$,,;
523 } else {
524 $action ||= "blob_plain";
525 }
526 $hash_base ||= validate_refname($refname);
527 $file_name ||= validate_pathname($pathname);
528 } elsif (defined $refname) {
529 # we got "project.git/branch"
530 $action ||= "shortlog";
531 $hash ||= validate_refname($refname);
532 }
533}
534evaluate_path_info();
535
536# path to the current git repository
537our $git_dir;
538$git_dir = "$projectroot/$project" if $project;
539
540# dispatch
541my %actions = (
542 "blame" => \&git_blame2,
543 "blobdiff" => \&git_blobdiff,
544 "blobdiff_plain" => \&git_blobdiff_plain,
545 "blob" => \&git_blob,
546 "blob_plain" => \&git_blob_plain,
547 "commitdiff" => \&git_commitdiff,
548 "commitdiff_plain" => \&git_commitdiff_plain,
549 "commit" => \&git_commit,
550 "forks" => \&git_forks,
551 "heads" => \&git_heads,
552 "history" => \&git_history,
553 "log" => \&git_log,
554 "rss" => \&git_rss,
555 "atom" => \&git_atom,
556 "search" => \&git_search,
557 "search_help" => \&git_search_help,
558 "shortlog" => \&git_shortlog,
559 "summary" => \&git_summary,
560 "tag" => \&git_tag,
561 "tags" => \&git_tags,
562 "tree" => \&git_tree,
563 "snapshot" => \&git_snapshot,
564 "object" => \&git_object,
565 # those below don't need $project
566 "opml" => \&git_opml,
567 "project_list" => \&git_project_list,
568 "project_index" => \&git_project_index,
569);
570
571if (!defined $action) {
572 if (defined $hash) {
573 $action = git_get_type($hash);
574 } elsif (defined $hash_base && defined $file_name) {
575 $action = git_get_type("$hash_base:$file_name");
576 } elsif (defined $project) {
577 $action = 'summary';
578 } else {
579 $action = 'project_list';
580 }
581}
582if (!defined($actions{$action})) {
583 die_error(undef, "Unknown action");
584}
585if ($action !~ m/^(opml|project_list|project_index)$/ &&
586 !$project) {
587 die_error(undef, "Project needed");
588}
589$actions{$action}->();
590exit;
591
592## ======================================================================
593## action links
594
595sub href (%) {
596 my %params = @_;
597 # default is to use -absolute url() i.e. $my_uri
598 my $href = $params{-full} ? $my_url : $my_uri;
599
600 # XXX: Warning: If you touch this, check the search form for updating,
601 # too.
602
603 my @mapping = (
604 project => "p",
605 action => "a",
606 file_name => "f",
607 file_parent => "fp",
608 hash => "h",
609 hash_parent => "hp",
610 hash_base => "hb",
611 hash_parent_base => "hpb",
612 page => "pg",
613 order => "o",
614 searchtext => "s",
615 searchtype => "st",
616 snapshot_format => "sf",
617 extra_options => "opt",
618 search_use_regexp => "sr",
619 );
620 my %mapping = @mapping;
621
622 $params{'project'} = $project unless exists $params{'project'};
623
624 if ($params{-replay}) {
625 while (my ($name, $symbol) = each %mapping) {
626 if (!exists $params{$name}) {
627 # to allow for multivalued params we use arrayref form
628 $params{$name} = [ $cgi->param($symbol) ];
629 }
630 }
631 }
632
633 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
634 if ($use_pathinfo) {
635 # use PATH_INFO for project name
636 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
637 delete $params{'project'};
638
639 # Summary just uses the project path URL
640 if (defined $params{'action'} && $params{'action'} eq 'summary') {
641 delete $params{'action'};
642 }
643 }
644
645 # now encode the parameters explicitly
646 my @result = ();
647 for (my $i = 0; $i < @mapping; $i += 2) {
648 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
649 if (defined $params{$name}) {
650 if (ref($params{$name}) eq "ARRAY") {
651 foreach my $par (@{$params{$name}}) {
652 push @result, $symbol . "=" . esc_param($par);
653 }
654 } else {
655 push @result, $symbol . "=" . esc_param($params{$name});
656 }
657 }
658 }
659 $href .= "?" . join(';', @result) if scalar @result;
660
661 return $href;
662}
663
664
665## ======================================================================
666## validation, quoting/unquoting and escaping
667
668sub validate_pathname {
669 my $input = shift || return undef;
670
671 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
672 # at the beginning, at the end, and between slashes.
673 # also this catches doubled slashes
674 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
675 return undef;
676 }
677 # no null characters
678 if ($input =~ m!\0!) {
679 return undef;
680 }
681 return $input;
682}
683
684sub validate_refname {
685 my $input = shift || return undef;
686
687 # textual hashes are O.K.
688 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
689 return $input;
690 }
691 # it must be correct pathname
692 $input = validate_pathname($input)
693 or return undef;
694 # restrictions on ref name according to git-check-ref-format
695 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
696 return undef;
697 }
698 return $input;
699}
700
701# decode sequences of octets in utf8 into Perl's internal form,
702# which is utf-8 with utf8 flag set if needed. gitweb writes out
703# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
704sub to_utf8 {
705 my $str = shift;
706 if (utf8::valid($str)) {
707 utf8::decode($str);
708 return $str;
709 } else {
710 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
711 }
712}
713
714# quote unsafe chars, but keep the slash, even when it's not
715# correct, but quoted slashes look too horrible in bookmarks
716sub esc_param {
717 my $str = shift;
718 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
719 $str =~ s/\+/%2B/g;
720 $str =~ s/ /\+/g;
721 return $str;
722}
723
724# quote unsafe chars in whole URL, so some charactrs cannot be quoted
725sub esc_url {
726 my $str = shift;
727 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
728 $str =~ s/\+/%2B/g;
729 $str =~ s/ /\+/g;
730 return $str;
731}
732
733# replace invalid utf8 character with SUBSTITUTION sequence
734sub esc_html ($;%) {
735 my $str = shift;
736 my %opts = @_;
737
738 $str = to_utf8($str);
739 $str = $cgi->escapeHTML($str);
740 if ($opts{'-nbsp'}) {
741 $str =~ s/ / /g;
742 }
743 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
744 return $str;
745}
746
747# quote control characters and escape filename to HTML
748sub esc_path {
749 my $str = shift;
750 my %opts = @_;
751
752 $str = to_utf8($str);
753 $str = $cgi->escapeHTML($str);
754 if ($opts{'-nbsp'}) {
755 $str =~ s/ / /g;
756 }
757 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
758 return $str;
759}
760
761# Make control characters "printable", using character escape codes (CEC)
762sub quot_cec {
763 my $cntrl = shift;
764 my %opts = @_;
765 my %es = ( # character escape codes, aka escape sequences
766 "\t" => '\t', # tab (HT)
767 "\n" => '\n', # line feed (LF)
768 "\r" => '\r', # carrige return (CR)
769 "\f" => '\f', # form feed (FF)
770 "\b" => '\b', # backspace (BS)
771 "\a" => '\a', # alarm (bell) (BEL)
772 "\e" => '\e', # escape (ESC)
773 "\013" => '\v', # vertical tab (VT)
774 "\000" => '\0', # nul character (NUL)
775 );
776 my $chr = ( (exists $es{$cntrl})
777 ? $es{$cntrl}
778 : sprintf('\%03o', ord($cntrl)) );
779 if ($opts{-nohtml}) {
780 return $chr;
781 } else {
782 return "<span class=\"cntrl\">$chr</span>";
783 }
784}
785
786# Alternatively use unicode control pictures codepoints,
787# Unicode "printable representation" (PR)
788sub quot_upr {
789 my $cntrl = shift;
790 my %opts = @_;
791
792 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
793 if ($opts{-nohtml}) {
794 return $chr;
795 } else {
796 return "<span class=\"cntrl\">$chr</span>";
797 }
798}
799
800# git may return quoted and escaped filenames
801sub unquote {
802 my $str = shift;
803
804 sub unq {
805 my $seq = shift;
806 my %es = ( # character escape codes, aka escape sequences
807 't' => "\t", # tab (HT, TAB)
808 'n' => "\n", # newline (NL)
809 'r' => "\r", # return (CR)
810 'f' => "\f", # form feed (FF)
811 'b' => "\b", # backspace (BS)
812 'a' => "\a", # alarm (bell) (BEL)
813 'e' => "\e", # escape (ESC)
814 'v' => "\013", # vertical tab (VT)
815 );
816
817 if ($seq =~ m/^[0-7]{1,3}$/) {
818 # octal char sequence
819 return chr(oct($seq));
820 } elsif (exists $es{$seq}) {
821 # C escape sequence, aka character escape code
822 return $es{$seq};
823 }
824 # quoted ordinary character
825 return $seq;
826 }
827
828 if ($str =~ m/^"(.*)"$/) {
829 # needs unquoting
830 $str = $1;
831 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
832 }
833 return $str;
834}
835
836# escape tabs (convert tabs to spaces)
837sub untabify {
838 my $line = shift;
839
840 while ((my $pos = index($line, "\t")) != -1) {
841 if (my $count = (8 - ($pos % 8))) {
842 my $spaces = ' ' x $count;
843 $line =~ s/\t/$spaces/;
844 }
845 }
846
847 return $line;
848}
849
850sub project_in_list {
851 my $project = shift;
852 my @list = git_get_projects_list();
853 return @list && scalar(grep { $_->{'path'} eq $project } @list);
854}
855
856## ----------------------------------------------------------------------
857## HTML aware string manipulation
858
859# Try to chop given string on a word boundary between position
860# $len and $len+$add_len. If there is no word boundary there,
861# chop at $len+$add_len. Do not chop if chopped part plus ellipsis
862# (marking chopped part) would be longer than given string.
863sub chop_str {
864 my $str = shift;
865 my $len = shift;
866 my $add_len = shift || 10;
867 my $where = shift || 'right'; # 'left' | 'center' | 'right'
868
869 # Make sure perl knows it is utf8 encoded so we don't
870 # cut in the middle of a utf8 multibyte char.
871 $str = to_utf8($str);
872
873 # allow only $len chars, but don't cut a word if it would fit in $add_len
874 # if it doesn't fit, cut it if it's still longer than the dots we would add
875 # remove chopped character entities entirely
876
877 # when chopping in the middle, distribute $len into left and right part
878 # return early if chopping wouldn't make string shorter
879 if ($where eq 'center') {
880 return $str if ($len + 5 >= length($str)); # filler is length 5
881 $len = int($len/2);
882 } else {
883 return $str if ($len + 4 >= length($str)); # filler is length 4
884 }
885
886 # regexps: ending and beginning with word part up to $add_len
887 my $endre = qr/.{$len}\w{0,$add_len}/;
888 my $begre = qr/\w{0,$add_len}.{$len}/;
889
890 if ($where eq 'left') {
891 $str =~ m/^(.*?)($begre)$/;
892 my ($lead, $body) = ($1, $2);
893 if (length($lead) > 4) {
894 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
895 $lead = " ...";
896 }
897 return "$lead$body";
898
899 } elsif ($where eq 'center') {
900 $str =~ m/^($endre)(.*)$/;
901 my ($left, $str) = ($1, $2);
902 $str =~ m/^(.*?)($begre)$/;
903 my ($mid, $right) = ($1, $2);
904 if (length($mid) > 5) {
905 $left =~ s/&[^;]*$//;
906 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
907 $mid = " ... ";
908 }
909 return "$left$mid$right";
910
911 } else {
912 $str =~ m/^($endre)(.*)$/;
913 my $body = $1;
914 my $tail = $2;
915 if (length($tail) > 4) {
916 $body =~ s/&[^;]*$//;
917 $tail = "... ";
918 }
919 return "$body$tail";
920 }
921}
922
923# takes the same arguments as chop_str, but also wraps a <span> around the
924# result with a title attribute if it does get chopped. Additionally, the
925# string is HTML-escaped.
926sub chop_and_escape_str {
927 my ($str) = @_;
928
929 my $chopped = chop_str(@_);
930 if ($chopped eq $str) {
931 return esc_html($chopped);
932 } else {
933 $str =~ s/([[:cntrl:]])/?/g;
934 return $cgi->span({-title=>$str}, esc_html($chopped));
935 }
936}
937
938## ----------------------------------------------------------------------
939## functions returning short strings
940
941# CSS class for given age value (in seconds)
942sub age_class {
943 my $age = shift;
944
945 if (!defined $age) {
946 return "noage";
947 } elsif ($age < 60*60*2) {
948 return "age0";
949 } elsif ($age < 60*60*24*2) {
950 return "age1";
951 } else {
952 return "age2";
953 }
954}
955
956# convert age in seconds to "nn units ago" string
957sub age_string {
958 my $age = shift;
959 my $age_str;
960
961 if ($age > 60*60*24*365*2) {
962 $age_str = (int $age/60/60/24/365);
963 $age_str .= " years ago";
964 } elsif ($age > 60*60*24*(365/12)*2) {
965 $age_str = int $age/60/60/24/(365/12);
966 $age_str .= " months ago";
967 } elsif ($age > 60*60*24*7*2) {
968 $age_str = int $age/60/60/24/7;
969 $age_str .= " weeks ago";
970 } elsif ($age > 60*60*24*2) {
971 $age_str = int $age/60/60/24;
972 $age_str .= " days ago";
973 } elsif ($age > 60*60*2) {
974 $age_str = int $age/60/60;
975 $age_str .= " hours ago";
976 } elsif ($age > 60*2) {
977 $age_str = int $age/60;
978 $age_str .= " min ago";
979 } elsif ($age > 2) {
980 $age_str = int $age;
981 $age_str .= " sec ago";
982 } else {
983 $age_str .= " right now";
984 }
985 return $age_str;
986}
987
988use constant {
989 S_IFINVALID => 0030000,
990 S_IFGITLINK => 0160000,
991};
992
993# submodule/subproject, a commit object reference
994sub S_ISGITLINK($) {
995 my $mode = shift;
996
997 return (($mode & S_IFMT) == S_IFGITLINK)
998}
999
1000# convert file mode in octal to symbolic file mode string
1001sub mode_str {
1002 my $mode = oct shift;
1003
1004 if (S_ISGITLINK($mode)) {
1005 return 'm---------';
1006 } elsif (S_ISDIR($mode & S_IFMT)) {
1007 return 'drwxr-xr-x';
1008 } elsif (S_ISLNK($mode)) {
1009 return 'lrwxrwxrwx';
1010 } elsif (S_ISREG($mode)) {
1011 # git cares only about the executable bit
1012 if ($mode & S_IXUSR) {
1013 return '-rwxr-xr-x';
1014 } else {
1015 return '-rw-r--r--';
1016 };
1017 } else {
1018 return '----------';
1019 }
1020}
1021
1022# convert file mode in octal to file type string
1023sub file_type {
1024 my $mode = shift;
1025
1026 if ($mode !~ m/^[0-7]+$/) {
1027 return $mode;
1028 } else {
1029 $mode = oct $mode;
1030 }
1031
1032 if (S_ISGITLINK($mode)) {
1033 return "submodule";
1034 } elsif (S_ISDIR($mode & S_IFMT)) {
1035 return "directory";
1036 } elsif (S_ISLNK($mode)) {
1037 return "symlink";
1038 } elsif (S_ISREG($mode)) {
1039 return "file";
1040 } else {
1041 return "unknown";
1042 }
1043}
1044
1045# convert file mode in octal to file type description string
1046sub file_type_long {
1047 my $mode = shift;
1048
1049 if ($mode !~ m/^[0-7]+$/) {
1050 return $mode;
1051 } else {
1052 $mode = oct $mode;
1053 }
1054
1055 if (S_ISGITLINK($mode)) {
1056 return "submodule";
1057 } elsif (S_ISDIR($mode & S_IFMT)) {
1058 return "directory";
1059 } elsif (S_ISLNK($mode)) {
1060 return "symlink";
1061 } elsif (S_ISREG($mode)) {
1062 if ($mode & S_IXUSR) {
1063 return "executable";
1064 } else {
1065 return "file";
1066 };
1067 } else {
1068 return "unknown";
1069 }
1070}
1071
1072
1073## ----------------------------------------------------------------------
1074## functions returning short HTML fragments, or transforming HTML fragments
1075## which don't belong to other sections
1076
1077# format line of commit message.
1078sub format_log_line_html {
1079 my $line = shift;
1080
1081 $line = esc_html($line, -nbsp=>1);
1082 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1083 my $hash_text = $1;
1084 my $link =
1085 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1086 -class => "text"}, $hash_text);
1087 $line =~ s/$hash_text/$link/;
1088 }
1089 return $line;
1090}
1091
1092# format marker of refs pointing to given object
1093sub format_ref_marker {
1094 my ($refs, $id) = @_;
1095 my $markers = '';
1096
1097 if (defined $refs->{$id}) {
1098 foreach my $ref (@{$refs->{$id}}) {
1099 my ($type, $name) = qw();
1100 # e.g. tags/v2.6.11 or heads/next
1101 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1102 $type = $1;
1103 $name = $2;
1104 } else {
1105 $type = "ref";
1106 $name = $ref;
1107 }
1108
1109 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1110 esc_html($name) . "</span>";
1111 }
1112 }
1113
1114 if ($markers) {
1115 return ' <span class="refs">'. $markers . '</span>';
1116 } else {
1117 return "";
1118 }
1119}
1120
1121# format, perhaps shortened and with markers, title line
1122sub format_subject_html {
1123 my ($long, $short, $href, $extra) = @_;
1124 $extra = '' unless defined($extra);
1125
1126 if (length($short) < length($long)) {
1127 return $cgi->a({-href => $href, -class => "list subject",
1128 -title => to_utf8($long)},
1129 esc_html($short) . $extra);
1130 } else {
1131 return $cgi->a({-href => $href, -class => "list subject"},
1132 esc_html($long) . $extra);
1133 }
1134}
1135
1136# format git diff header line, i.e. "diff --(git|combined|cc) ..."
1137sub format_git_diff_header_line {
1138 my $line = shift;
1139 my $diffinfo = shift;
1140 my ($from, $to) = @_;
1141
1142 if ($diffinfo->{'nparents'}) {
1143 # combined diff
1144 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1145 if ($to->{'href'}) {
1146 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1147 esc_path($to->{'file'}));
1148 } else { # file was deleted (no href)
1149 $line .= esc_path($to->{'file'});
1150 }
1151 } else {
1152 # "ordinary" diff
1153 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1154 if ($from->{'href'}) {
1155 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1156 'a/' . esc_path($from->{'file'}));
1157 } else { # file was added (no href)
1158 $line .= 'a/' . esc_path($from->{'file'});
1159 }
1160 $line .= ' ';
1161 if ($to->{'href'}) {
1162 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1163 'b/' . esc_path($to->{'file'}));
1164 } else { # file was deleted
1165 $line .= 'b/' . esc_path($to->{'file'});
1166 }
1167 }
1168
1169 return "<div class=\"diff header\">$line</div>\n";
1170}
1171
1172# format extended diff header line, before patch itself
1173sub format_extended_diff_header_line {
1174 my $line = shift;
1175 my $diffinfo = shift;
1176 my ($from, $to) = @_;
1177
1178 # match <path>
1179 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1180 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1181 esc_path($from->{'file'}));
1182 }
1183 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1184 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1185 esc_path($to->{'file'}));
1186 }
1187 # match single <mode>
1188 if ($line =~ m/\s(\d{6})$/) {
1189 $line .= '<span class="info"> (' .
1190 file_type_long($1) .
1191 ')</span>';
1192 }
1193 # match <hash>
1194 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1195 # can match only for combined diff
1196 $line = 'index ';
1197 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1198 if ($from->{'href'}[$i]) {
1199 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1200 -class=>"hash"},
1201 substr($diffinfo->{'from_id'}[$i],0,7));
1202 } else {
1203 $line .= '0' x 7;
1204 }
1205 # separator
1206 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1207 }
1208 $line .= '..';
1209 if ($to->{'href'}) {
1210 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1211 substr($diffinfo->{'to_id'},0,7));
1212 } else {
1213 $line .= '0' x 7;
1214 }
1215
1216 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1217 # can match only for ordinary diff
1218 my ($from_link, $to_link);
1219 if ($from->{'href'}) {
1220 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1221 substr($diffinfo->{'from_id'},0,7));
1222 } else {
1223 $from_link = '0' x 7;
1224 }
1225 if ($to->{'href'}) {
1226 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1227 substr($diffinfo->{'to_id'},0,7));
1228 } else {
1229 $to_link = '0' x 7;
1230 }
1231 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1232 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1233 }
1234
1235 return $line . "<br/>\n";
1236}
1237
1238# format from-file/to-file diff header
1239sub format_diff_from_to_header {
1240 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1241 my $line;
1242 my $result = '';
1243
1244 $line = $from_line;
1245 #assert($line =~ m/^---/) if DEBUG;
1246 # no extra formatting for "^--- /dev/null"
1247 if (! $diffinfo->{'nparents'}) {
1248 # ordinary (single parent) diff
1249 if ($line =~ m!^--- "?a/!) {
1250 if ($from->{'href'}) {
1251 $line = '--- a/' .
1252 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1253 esc_path($from->{'file'}));
1254 } else {
1255 $line = '--- a/' .
1256 esc_path($from->{'file'});
1257 }
1258 }
1259 $result .= qq!<div class="diff from_file">$line</div>\n!;
1260
1261 } else {
1262 # combined diff (merge commit)
1263 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1264 if ($from->{'href'}[$i]) {
1265 $line = '--- ' .
1266 $cgi->a({-href=>href(action=>"blobdiff",
1267 hash_parent=>$diffinfo->{'from_id'}[$i],
1268 hash_parent_base=>$parents[$i],
1269 file_parent=>$from->{'file'}[$i],
1270 hash=>$diffinfo->{'to_id'},
1271 hash_base=>$hash,
1272 file_name=>$to->{'file'}),
1273 -class=>"path",
1274 -title=>"diff" . ($i+1)},
1275 $i+1) .
1276 '/' .
1277 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1278 esc_path($from->{'file'}[$i]));
1279 } else {
1280 $line = '--- /dev/null';
1281 }
1282 $result .= qq!<div class="diff from_file">$line</div>\n!;
1283 }
1284 }
1285
1286 $line = $to_line;
1287 #assert($line =~ m/^\+\+\+/) if DEBUG;
1288 # no extra formatting for "^+++ /dev/null"
1289 if ($line =~ m!^\+\+\+ "?b/!) {
1290 if ($to->{'href'}) {
1291 $line = '+++ b/' .
1292 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1293 esc_path($to->{'file'}));
1294 } else {
1295 $line = '+++ b/' .
1296 esc_path($to->{'file'});
1297 }
1298 }
1299 $result .= qq!<div class="diff to_file">$line</div>\n!;
1300
1301 return $result;
1302}
1303
1304# create note for patch simplified by combined diff
1305sub format_diff_cc_simplified {
1306 my ($diffinfo, @parents) = @_;
1307 my $result = '';
1308
1309 $result .= "<div class=\"diff header\">" .
1310 "diff --cc ";
1311 if (!is_deleted($diffinfo)) {
1312 $result .= $cgi->a({-href => href(action=>"blob",
1313 hash_base=>$hash,
1314 hash=>$diffinfo->{'to_id'},
1315 file_name=>$diffinfo->{'to_file'}),
1316 -class => "path"},
1317 esc_path($diffinfo->{'to_file'}));
1318 } else {
1319 $result .= esc_path($diffinfo->{'to_file'});
1320 }
1321 $result .= "</div>\n" . # class="diff header"
1322 "<div class=\"diff nodifferences\">" .
1323 "Simple merge" .
1324 "</div>\n"; # class="diff nodifferences"
1325
1326 return $result;
1327}
1328
1329# format patch (diff) line (not to be used for diff headers)
1330sub format_diff_line {
1331 my $line = shift;
1332 my ($from, $to) = @_;
1333 my $diff_class = "";
1334
1335 chomp $line;
1336
1337 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1338 # combined diff
1339 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1340 if ($line =~ m/^\@{3}/) {
1341 $diff_class = " chunk_header";
1342 } elsif ($line =~ m/^\\/) {
1343 $diff_class = " incomplete";
1344 } elsif ($prefix =~ tr/+/+/) {
1345 $diff_class = " add";
1346 } elsif ($prefix =~ tr/-/-/) {
1347 $diff_class = " rem";
1348 }
1349 } else {
1350 # assume ordinary diff
1351 my $char = substr($line, 0, 1);
1352 if ($char eq '+') {
1353 $diff_class = " add";
1354 } elsif ($char eq '-') {
1355 $diff_class = " rem";
1356 } elsif ($char eq '@') {
1357 $diff_class = " chunk_header";
1358 } elsif ($char eq "\\") {
1359 $diff_class = " incomplete";
1360 }
1361 }
1362 $line = untabify($line);
1363 if ($from && $to && $line =~ m/^\@{2} /) {
1364 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1365 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1366
1367 $from_lines = 0 unless defined $from_lines;
1368 $to_lines = 0 unless defined $to_lines;
1369
1370 if ($from->{'href'}) {
1371 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1372 -class=>"list"}, $from_text);
1373 }
1374 if ($to->{'href'}) {
1375 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1376 -class=>"list"}, $to_text);
1377 }
1378 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1379 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1380 return "<div class=\"diff$diff_class\">$line</div>\n";
1381 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1382 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1383 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1384
1385 @from_text = split(' ', $ranges);
1386 for (my $i = 0; $i < @from_text; ++$i) {
1387 ($from_start[$i], $from_nlines[$i]) =
1388 (split(',', substr($from_text[$i], 1)), 0);
1389 }
1390
1391 $to_text = pop @from_text;
1392 $to_start = pop @from_start;
1393 $to_nlines = pop @from_nlines;
1394
1395 $line = "<span class=\"chunk_info\">$prefix ";
1396 for (my $i = 0; $i < @from_text; ++$i) {
1397 if ($from->{'href'}[$i]) {
1398 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1399 -class=>"list"}, $from_text[$i]);
1400 } else {
1401 $line .= $from_text[$i];
1402 }
1403 $line .= " ";
1404 }
1405 if ($to->{'href'}) {
1406 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1407 -class=>"list"}, $to_text);
1408 } else {
1409 $line .= $to_text;
1410 }
1411 $line .= " $prefix</span>" .
1412 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1413 return "<div class=\"diff$diff_class\">$line</div>\n";
1414 }
1415 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1416}
1417
1418# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1419# linked. Pass the hash of the tree/commit to snapshot.
1420sub format_snapshot_links {
1421 my ($hash) = @_;
1422 my @snapshot_fmts = gitweb_check_feature('snapshot');
1423 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1424 my $num_fmts = @snapshot_fmts;
1425 if ($num_fmts > 1) {
1426 # A parenthesized list of links bearing format names.
1427 # e.g. "snapshot (_tar.gz_ _zip_)"
1428 return "snapshot (" . join(' ', map
1429 $cgi->a({
1430 -href => href(
1431 action=>"snapshot",
1432 hash=>$hash,
1433 snapshot_format=>$_
1434 )
1435 }, $known_snapshot_formats{$_}{'display'})
1436 , @snapshot_fmts) . ")";
1437 } elsif ($num_fmts == 1) {
1438 # A single "snapshot" link whose tooltip bears the format name.
1439 # i.e. "_snapshot_"
1440 my ($fmt) = @snapshot_fmts;
1441 return
1442 $cgi->a({
1443 -href => href(
1444 action=>"snapshot",
1445 hash=>$hash,
1446 snapshot_format=>$fmt
1447 ),
1448 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1449 }, "snapshot");
1450 } else { # $num_fmts == 0
1451 return undef;
1452 }
1453}
1454
1455## ......................................................................
1456## functions returning values to be passed, perhaps after some
1457## transformation, to other functions; e.g. returning arguments to href()
1458
1459# returns hash to be passed to href to generate gitweb URL
1460# in -title key it returns description of link
1461sub get_feed_info {
1462 my $format = shift || 'Atom';
1463 my %res = (action => lc($format));
1464
1465 # feed links are possible only for project views
1466 return unless (defined $project);
1467 # some views should link to OPML, or to generic project feed,
1468 # or don't have specific feed yet (so they should use generic)
1469 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1470
1471 my $branch;
1472 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1473 # from tag links; this also makes possible to detect branch links
1474 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1475 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1476 $branch = $1;
1477 }
1478 # find log type for feed description (title)
1479 my $type = 'log';
1480 if (defined $file_name) {
1481 $type = "history of $file_name";
1482 $type .= "/" if ($action eq 'tree');
1483 $type .= " on '$branch'" if (defined $branch);
1484 } else {
1485 $type = "log of $branch" if (defined $branch);
1486 }
1487
1488 $res{-title} = $type;
1489 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1490 $res{'file_name'} = $file_name;
1491
1492 return %res;
1493}
1494
1495## ----------------------------------------------------------------------
1496## git utility subroutines, invoking git commands
1497
1498# returns path to the core git executable and the --git-dir parameter as list
1499sub git_cmd {
1500 return $GIT, '--git-dir='.$git_dir;
1501}
1502
1503# returns path to the core git executable and the --git-dir parameter as string
1504sub git_cmd_str {
1505 return join(' ', git_cmd());
1506}
1507
1508# get HEAD ref of given project as hash
1509sub git_get_head_hash {
1510 my $project = shift;
1511 my $o_git_dir = $git_dir;
1512 my $retval = undef;
1513 $git_dir = "$projectroot/$project";
1514 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1515 my $head = <$fd>;
1516 close $fd;
1517 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1518 $retval = $1;
1519 }
1520 }
1521 if (defined $o_git_dir) {
1522 $git_dir = $o_git_dir;
1523 }
1524 return $retval;
1525}
1526
1527# get type of given object
1528sub git_get_type {
1529 my $hash = shift;
1530
1531 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1532 my $type = <$fd>;
1533 close $fd or return;
1534 chomp $type;
1535 return $type;
1536}
1537
1538# repository configuration
1539our $config_file = '';
1540our %config;
1541
1542# store multiple values for single key as anonymous array reference
1543# single values stored directly in the hash, not as [ <value> ]
1544sub hash_set_multi {
1545 my ($hash, $key, $value) = @_;
1546
1547 if (!exists $hash->{$key}) {
1548 $hash->{$key} = $value;
1549 } elsif (!ref $hash->{$key}) {
1550 $hash->{$key} = [ $hash->{$key}, $value ];
1551 } else {
1552 push @{$hash->{$key}}, $value;
1553 }
1554}
1555
1556# return hash of git project configuration
1557# optionally limited to some section, e.g. 'gitweb'
1558sub git_parse_project_config {
1559 my $section_regexp = shift;
1560 my %config;
1561
1562 local $/ = "\0";
1563
1564 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1565 or return;
1566
1567 while (my $keyval = <$fh>) {
1568 chomp $keyval;
1569 my ($key, $value) = split(/\n/, $keyval, 2);
1570
1571 hash_set_multi(\%config, $key, $value)
1572 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1573 }
1574 close $fh;
1575
1576 return %config;
1577}
1578
1579# convert config value to boolean, 'true' or 'false'
1580# no value, number > 0, 'true' and 'yes' values are true
1581# rest of values are treated as false (never as error)
1582sub config_to_bool {
1583 my $val = shift;
1584
1585 # strip leading and trailing whitespace
1586 $val =~ s/^\s+//;
1587 $val =~ s/\s+$//;
1588
1589 return (!defined $val || # section.key
1590 ($val =~ /^\d+$/ && $val) || # section.key = 1
1591 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1592}
1593
1594# convert config value to simple decimal number
1595# an optional value suffix of 'k', 'm', or 'g' will cause the value
1596# to be multiplied by 1024, 1048576, or 1073741824
1597sub config_to_int {
1598 my $val = shift;
1599
1600 # strip leading and trailing whitespace
1601 $val =~ s/^\s+//;
1602 $val =~ s/\s+$//;
1603
1604 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1605 $unit = lc($unit);
1606 # unknown unit is treated as 1
1607 return $num * ($unit eq 'g' ? 1073741824 :
1608 $unit eq 'm' ? 1048576 :
1609 $unit eq 'k' ? 1024 : 1);
1610 }
1611 return $val;
1612}
1613
1614# convert config value to array reference, if needed
1615sub config_to_multi {
1616 my $val = shift;
1617
1618 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1619}
1620
1621sub git_get_project_config {
1622 my ($key, $type) = @_;
1623
1624 # key sanity check
1625 return unless ($key);
1626 $key =~ s/^gitweb\.//;
1627 return if ($key =~ m/\W/);
1628
1629 # type sanity check
1630 if (defined $type) {
1631 $type =~ s/^--//;
1632 $type = undef
1633 unless ($type eq 'bool' || $type eq 'int');
1634 }
1635
1636 # get config
1637 if (!defined $config_file ||
1638 $config_file ne "$git_dir/config") {
1639 %config = git_parse_project_config('gitweb');
1640 $config_file = "$git_dir/config";
1641 }
1642
1643 # ensure given type
1644 if (!defined $type) {
1645 return $config{"gitweb.$key"};
1646 } elsif ($type eq 'bool') {
1647 # backward compatibility: 'git config --bool' returns true/false
1648 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1649 } elsif ($type eq 'int') {
1650 return config_to_int($config{"gitweb.$key"});
1651 }
1652 return $config{"gitweb.$key"};
1653}
1654
1655# get hash of given path at given ref
1656sub git_get_hash_by_path {
1657 my $base = shift;
1658 my $path = shift || return undef;
1659 my $type = shift;
1660
1661 $path =~ s,/+$,,;
1662
1663 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1664 or die_error(undef, "Open git-ls-tree failed");
1665 my $line = <$fd>;
1666 close $fd or return undef;
1667
1668 if (!defined $line) {
1669 # there is no tree or hash given by $path at $base
1670 return undef;
1671 }
1672
1673 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1674 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1675 if (defined $type && $type ne $2) {
1676 # type doesn't match
1677 return undef;
1678 }
1679 return $3;
1680}
1681
1682# get path of entry with given hash at given tree-ish (ref)
1683# used to get 'from' filename for combined diff (merge commit) for renames
1684sub git_get_path_by_hash {
1685 my $base = shift || return;
1686 my $hash = shift || return;
1687
1688 local $/ = "\0";
1689
1690 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1691 or return undef;
1692 while (my $line = <$fd>) {
1693 chomp $line;
1694
1695 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1696 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1697 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1698 close $fd;
1699 return $1;
1700 }
1701 }
1702 close $fd;
1703 return undef;
1704}
1705
1706## ......................................................................
1707## git utility functions, directly accessing git repository
1708
1709sub git_get_project_description {
1710 my $path = shift;
1711
1712 $git_dir = "$projectroot/$path";
1713 open my $fd, "$git_dir/description"
1714 or return git_get_project_config('description');
1715 my $descr = <$fd>;
1716 close $fd;
1717 if (defined $descr) {
1718 chomp $descr;
1719 }
1720 return $descr;
1721}
1722
1723sub git_get_project_url_list {
1724 my $path = shift;
1725
1726 $git_dir = "$projectroot/$path";
1727 open my $fd, "$git_dir/cloneurl"
1728 or return wantarray ?
1729 @{ config_to_multi(git_get_project_config('url')) } :
1730 config_to_multi(git_get_project_config('url'));
1731 my @git_project_url_list = map { chomp; $_ } <$fd>;
1732 close $fd;
1733
1734 return wantarray ? @git_project_url_list : \@git_project_url_list;
1735}
1736
1737sub git_get_projects_list {
1738 my ($filter) = @_;
1739 my @list;
1740
1741 $filter ||= '';
1742 $filter =~ s/\.git$//;
1743
1744 my ($check_forks) = gitweb_check_feature('forks');
1745
1746 if (-d $projects_list) {
1747 # search in directory
1748 my $dir = $projects_list . ($filter ? "/$filter" : '');
1749 # remove the trailing "/"
1750 $dir =~ s!/+$!!;
1751 my $pfxlen = length("$dir");
1752 my $pfxdepth = ($dir =~ tr!/!!);
1753
1754 File::Find::find({
1755 follow_fast => 1, # follow symbolic links
1756 follow_skip => 2, # ignore duplicates
1757 dangling_symlinks => 0, # ignore dangling symlinks, silently
1758 wanted => sub {
1759 # skip project-list toplevel, if we get it.
1760 return if (m!^[/.]$!);
1761 # only directories can be git repositories
1762 return unless (-d $_);
1763 # don't traverse too deep (Find is super slow on os x)
1764 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1765 $File::Find::prune = 1;
1766 return;
1767 }
1768
1769 my $subdir = substr($File::Find::name, $pfxlen + 1);
1770 # we check related file in $projectroot
1771 if ($check_forks and $subdir =~ m#/.#) {
1772 $File::Find::prune = 1;
1773 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1774 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1775 $File::Find::prune = 1;
1776 }
1777 },
1778 }, "$dir");
1779
1780 } elsif (-f $projects_list) {
1781 # read from file(url-encoded):
1782 # 'git%2Fgit.git Linus+Torvalds'
1783 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1784 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1785 my %paths;
1786 open my ($fd), $projects_list or return;
1787 PROJECT:
1788 while (my $line = <$fd>) {
1789 chomp $line;
1790 my ($path, $owner) = split ' ', $line;
1791 $path = unescape($path);
1792 $owner = unescape($owner);
1793 if (!defined $path) {
1794 next;
1795 }
1796 if ($filter ne '') {
1797 # looking for forks;
1798 my $pfx = substr($path, 0, length($filter));
1799 if ($pfx ne $filter) {
1800 next PROJECT;
1801 }
1802 my $sfx = substr($path, length($filter));
1803 if ($sfx !~ /^\/.*\.git$/) {
1804 next PROJECT;
1805 }
1806 } elsif ($check_forks) {
1807 PATH:
1808 foreach my $filter (keys %paths) {
1809 # looking for forks;
1810 my $pfx = substr($path, 0, length($filter));
1811 if ($pfx ne $filter) {
1812 next PATH;
1813 }
1814 my $sfx = substr($path, length($filter));
1815 if ($sfx !~ /^\/.*\.git$/) {
1816 next PATH;
1817 }
1818 # is a fork, don't include it in
1819 # the list
1820 next PROJECT;
1821 }
1822 }
1823 if (check_export_ok("$projectroot/$path")) {
1824 my $pr = {
1825 path => $path,
1826 owner => to_utf8($owner),
1827 };
1828 push @list, $pr;
1829 (my $forks_path = $path) =~ s/\.git$//;
1830 $paths{$forks_path}++;
1831 }
1832 }
1833 close $fd;
1834 }
1835 return @list;
1836}
1837
1838our $gitweb_project_owner = undef;
1839sub git_get_project_list_from_file {
1840
1841 return if (defined $gitweb_project_owner);
1842
1843 $gitweb_project_owner = {};
1844 # read from file (url-encoded):
1845 # 'git%2Fgit.git Linus+Torvalds'
1846 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1847 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1848 if (-f $projects_list) {
1849 open (my $fd , $projects_list);
1850 while (my $line = <$fd>) {
1851 chomp $line;
1852 my ($pr, $ow) = split ' ', $line;
1853 $pr = unescape($pr);
1854 $ow = unescape($ow);
1855 $gitweb_project_owner->{$pr} = to_utf8($ow);
1856 }
1857 close $fd;
1858 }
1859}
1860
1861sub git_get_project_owner {
1862 my $project = shift;
1863 my $owner;
1864
1865 return undef unless $project;
1866 $git_dir = "$projectroot/$project";
1867
1868 if (!defined $gitweb_project_owner) {
1869 git_get_project_list_from_file();
1870 }
1871
1872 if (exists $gitweb_project_owner->{$project}) {
1873 $owner = $gitweb_project_owner->{$project};
1874 }
1875 if (!defined $owner){
1876 $owner = git_get_project_config('owner');
1877 }
1878 if (!defined $owner) {
1879 $owner = get_file_owner("$git_dir");
1880 }
1881
1882 return $owner;
1883}
1884
1885sub git_get_last_activity {
1886 my ($path) = @_;
1887 my $fd;
1888
1889 $git_dir = "$projectroot/$path";
1890 open($fd, "-|", git_cmd(), 'for-each-ref',
1891 '--format=%(committer)',
1892 '--sort=-committerdate',
1893 '--count=1',
1894 'refs/heads') or return;
1895 my $most_recent = <$fd>;
1896 close $fd or return;
1897 if (defined $most_recent &&
1898 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1899 my $timestamp = $1;
1900 my $age = time - $timestamp;
1901 return ($age, age_string($age));
1902 }
1903 return (undef, undef);
1904}
1905
1906sub git_get_references {
1907 my $type = shift || "";
1908 my %refs;
1909 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1910 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1911 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1912 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1913 or return;
1914
1915 while (my $line = <$fd>) {
1916 chomp $line;
1917 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1918 if (defined $refs{$1}) {
1919 push @{$refs{$1}}, $2;
1920 } else {
1921 $refs{$1} = [ $2 ];
1922 }
1923 }
1924 }
1925 close $fd or return;
1926 return \%refs;
1927}
1928
1929sub git_get_rev_name_tags {
1930 my $hash = shift || return undef;
1931
1932 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1933 or return;
1934 my $name_rev = <$fd>;
1935 close $fd;
1936
1937 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1938 return $1;
1939 } else {
1940 # catches also '$hash undefined' output
1941 return undef;
1942 }
1943}
1944
1945## ----------------------------------------------------------------------
1946## parse to hash functions
1947
1948sub parse_date {
1949 my $epoch = shift;
1950 my $tz = shift || "-0000";
1951
1952 my %date;
1953 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1954 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1955 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1956 $date{'hour'} = $hour;
1957 $date{'minute'} = $min;
1958 $date{'mday'} = $mday;
1959 $date{'day'} = $days[$wday];
1960 $date{'month'} = $months[$mon];
1961 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1962 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1963 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1964 $mday, $months[$mon], $hour ,$min;
1965 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1966 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
1967
1968 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1969 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1970 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1971 $date{'hour_local'} = $hour;
1972 $date{'minute_local'} = $min;
1973 $date{'tz_local'} = $tz;
1974 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1975 1900+$year, $mon+1, $mday,
1976 $hour, $min, $sec, $tz);
1977 return %date;
1978}
1979
1980sub parse_tag {
1981 my $tag_id = shift;
1982 my %tag;
1983 my @comment;
1984
1985 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1986 $tag{'id'} = $tag_id;
1987 while (my $line = <$fd>) {
1988 chomp $line;
1989 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1990 $tag{'object'} = $1;
1991 } elsif ($line =~ m/^type (.+)$/) {
1992 $tag{'type'} = $1;
1993 } elsif ($line =~ m/^tag (.+)$/) {
1994 $tag{'name'} = $1;
1995 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1996 $tag{'author'} = $1;
1997 $tag{'epoch'} = $2;
1998 $tag{'tz'} = $3;
1999 } elsif ($line =~ m/--BEGIN/) {
2000 push @comment, $line;
2001 last;
2002 } elsif ($line eq "") {
2003 last;
2004 }
2005 }
2006 push @comment, <$fd>;
2007 $tag{'comment'} = \@comment;
2008 close $fd or return;
2009 if (!defined $tag{'name'}) {
2010 return
2011 };
2012 return %tag
2013}
2014
2015sub parse_commit_text {
2016 my ($commit_text, $withparents) = @_;
2017 my @commit_lines = split '\n', $commit_text;
2018 my %co;
2019
2020 pop @commit_lines; # Remove '\0'
2021
2022 if (! @commit_lines) {
2023 return;
2024 }
2025
2026 my $header = shift @commit_lines;
2027 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2028 return;
2029 }
2030 ($co{'id'}, my @parents) = split ' ', $header;
2031 while (my $line = shift @commit_lines) {
2032 last if $line eq "\n";
2033 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2034 $co{'tree'} = $1;
2035 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2036 push @parents, $1;
2037 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2038 $co{'author'} = $1;
2039 $co{'author_epoch'} = $2;
2040 $co{'author_tz'} = $3;
2041 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2042 $co{'author_name'} = $1;
2043 $co{'author_email'} = $2;
2044 } else {
2045 $co{'author_name'} = $co{'author'};
2046 }
2047 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2048 $co{'committer'} = $1;
2049 $co{'committer_epoch'} = $2;
2050 $co{'committer_tz'} = $3;
2051 $co{'committer_name'} = $co{'committer'};
2052 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2053 $co{'committer_name'} = $1;
2054 $co{'committer_email'} = $2;
2055 } else {
2056 $co{'committer_name'} = $co{'committer'};
2057 }
2058 }
2059 }
2060 if (!defined $co{'tree'}) {
2061 return;
2062 };
2063 $co{'parents'} = \@parents;
2064 $co{'parent'} = $parents[0];
2065
2066 foreach my $title (@commit_lines) {
2067 $title =~ s/^ //;
2068 if ($title ne "") {
2069 $co{'title'} = chop_str($title, 80, 5);
2070 # remove leading stuff of merges to make the interesting part visible
2071 if (length($title) > 50) {
2072 $title =~ s/^Automatic //;
2073 $title =~ s/^merge (of|with) /Merge ... /i;
2074 if (length($title) > 50) {
2075 $title =~ s/(http|rsync):\/\///;
2076 }
2077 if (length($title) > 50) {
2078 $title =~ s/(master|www|rsync)\.//;
2079 }
2080 if (length($title) > 50) {
2081 $title =~ s/kernel.org:?//;
2082 }
2083 if (length($title) > 50) {
2084 $title =~ s/\/pub\/scm//;
2085 }
2086 }
2087 $co{'title_short'} = chop_str($title, 50, 5);
2088 last;
2089 }
2090 }
2091 if ($co{'title'} eq "") {
2092 $co{'title'} = $co{'title_short'} = '(no commit message)';
2093 }
2094 # remove added spaces
2095 foreach my $line (@commit_lines) {
2096 $line =~ s/^ //;
2097 }
2098 $co{'comment'} = \@commit_lines;
2099
2100 my $age = time - $co{'committer_epoch'};
2101 $co{'age'} = $age;
2102 $co{'age_string'} = age_string($age);
2103 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2104 if ($age > 60*60*24*7*2) {
2105 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2106 $co{'age_string_age'} = $co{'age_string'};
2107 } else {
2108 $co{'age_string_date'} = $co{'age_string'};
2109 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2110 }
2111 return %co;
2112}
2113
2114sub parse_commit {
2115 my ($commit_id) = @_;
2116 my %co;
2117
2118 local $/ = "\0";
2119
2120 open my $fd, "-|", git_cmd(), "rev-list",
2121 "--parents",
2122 "--header",
2123 "--max-count=1",
2124 $commit_id,
2125 "--",
2126 or die_error(undef, "Open git-rev-list failed");
2127 %co = parse_commit_text(<$fd>, 1);
2128 close $fd;
2129
2130 return %co;
2131}
2132
2133sub parse_commits {
2134 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2135 my @cos;
2136
2137 $maxcount ||= 1;
2138 $skip ||= 0;
2139
2140 local $/ = "\0";
2141
2142 open my $fd, "-|", git_cmd(), "rev-list",
2143 "--header",
2144 @args,
2145 ("--max-count=" . $maxcount),
2146 ("--skip=" . $skip),
2147 @extra_options,
2148 $commit_id,
2149 "--",
2150 ($filename ? ($filename) : ())
2151 or die_error(undef, "Open git-rev-list failed");
2152 while (my $line = <$fd>) {
2153 my %co = parse_commit_text($line);
2154 push @cos, \%co;
2155 }
2156 close $fd;
2157
2158 return wantarray ? @cos : \@cos;
2159}
2160
2161# parse ref from ref_file, given by ref_id, with given type
2162sub parse_ref {
2163 my $ref_file = shift;
2164 my $ref_id = shift;
2165 my $type = shift || git_get_type($ref_id);
2166 my %ref_item;
2167
2168 $ref_item{'type'} = $type;
2169 $ref_item{'id'} = $ref_id;
2170 $ref_item{'epoch'} = 0;
2171 $ref_item{'age'} = "unknown";
2172 if ($type eq "tag") {
2173 my %tag = parse_tag($ref_id);
2174 $ref_item{'comment'} = $tag{'comment'};
2175 if ($tag{'type'} eq "commit") {
2176 my %co = parse_commit($tag{'object'});
2177 $ref_item{'epoch'} = $co{'committer_epoch'};
2178 $ref_item{'age'} = $co{'age_string'};
2179 } elsif (defined($tag{'epoch'})) {
2180 my $age = time - $tag{'epoch'};
2181 $ref_item{'epoch'} = $tag{'epoch'};
2182 $ref_item{'age'} = age_string($age);
2183 }
2184 $ref_item{'reftype'} = $tag{'type'};
2185 $ref_item{'name'} = $tag{'name'};
2186 $ref_item{'refid'} = $tag{'object'};
2187 } elsif ($type eq "commit"){
2188 my %co = parse_commit($ref_id);
2189 $ref_item{'reftype'} = "commit";
2190 $ref_item{'name'} = $ref_file;
2191 $ref_item{'title'} = $co{'title'};
2192 $ref_item{'refid'} = $ref_id;
2193 $ref_item{'epoch'} = $co{'committer_epoch'};
2194 $ref_item{'age'} = $co{'age_string'};
2195 } else {
2196 $ref_item{'reftype'} = $type;
2197 $ref_item{'name'} = $ref_file;
2198 $ref_item{'refid'} = $ref_id;
2199 }
2200
2201 return %ref_item;
2202}
2203
2204# parse line of git-diff-tree "raw" output
2205sub parse_difftree_raw_line {
2206 my $line = shift;
2207 my %res;
2208
2209 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2210 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2211 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2212 $res{'from_mode'} = $1;
2213 $res{'to_mode'} = $2;
2214 $res{'from_id'} = $3;
2215 $res{'to_id'} = $4;
2216 $res{'status'} = $5;
2217 $res{'similarity'} = $6;
2218 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2219 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2220 } else {
2221 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2222 }
2223 }
2224 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2225 # combined diff (for merge commit)
2226 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2227 $res{'nparents'} = length($1);
2228 $res{'from_mode'} = [ split(' ', $2) ];
2229 $res{'to_mode'} = pop @{$res{'from_mode'}};
2230 $res{'from_id'} = [ split(' ', $3) ];
2231 $res{'to_id'} = pop @{$res{'from_id'}};
2232 $res{'status'} = [ split('', $4) ];
2233 $res{'to_file'} = unquote($5);
2234 }
2235 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2236 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2237 $res{'commit'} = $1;
2238 }
2239
2240 return wantarray ? %res : \%res;
2241}
2242
2243# wrapper: return parsed line of git-diff-tree "raw" output
2244# (the argument might be raw line, or parsed info)
2245sub parsed_difftree_line {
2246 my $line_or_ref = shift;
2247
2248 if (ref($line_or_ref) eq "HASH") {
2249 # pre-parsed (or generated by hand)
2250 return $line_or_ref;
2251 } else {
2252 return parse_difftree_raw_line($line_or_ref);
2253 }
2254}
2255
2256# parse line of git-ls-tree output
2257sub parse_ls_tree_line ($;%) {
2258 my $line = shift;
2259 my %opts = @_;
2260 my %res;
2261
2262 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2263 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2264
2265 $res{'mode'} = $1;
2266 $res{'type'} = $2;
2267 $res{'hash'} = $3;
2268 if ($opts{'-z'}) {
2269 $res{'name'} = $4;
2270 } else {
2271 $res{'name'} = unquote($4);
2272 }
2273
2274 return wantarray ? %res : \%res;
2275}
2276
2277# generates _two_ hashes, references to which are passed as 2 and 3 argument
2278sub parse_from_to_diffinfo {
2279 my ($diffinfo, $from, $to, @parents) = @_;
2280
2281 if ($diffinfo->{'nparents'}) {
2282 # combined diff
2283 $from->{'file'} = [];
2284 $from->{'href'} = [];
2285 fill_from_file_info($diffinfo, @parents)
2286 unless exists $diffinfo->{'from_file'};
2287 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2288 $from->{'file'}[$i] =
2289 defined $diffinfo->{'from_file'}[$i] ?
2290 $diffinfo->{'from_file'}[$i] :
2291 $diffinfo->{'to_file'};
2292 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2293 $from->{'href'}[$i] = href(action=>"blob",
2294 hash_base=>$parents[$i],
2295 hash=>$diffinfo->{'from_id'}[$i],
2296 file_name=>$from->{'file'}[$i]);
2297 } else {
2298 $from->{'href'}[$i] = undef;
2299 }
2300 }
2301 } else {
2302 # ordinary (not combined) diff
2303 $from->{'file'} = $diffinfo->{'from_file'};
2304 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2305 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2306 hash=>$diffinfo->{'from_id'},
2307 file_name=>$from->{'file'});
2308 } else {
2309 delete $from->{'href'};
2310 }
2311 }
2312
2313 $to->{'file'} = $diffinfo->{'to_file'};
2314 if (!is_deleted($diffinfo)) { # file exists in result
2315 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2316 hash=>$diffinfo->{'to_id'},
2317 file_name=>$to->{'file'});
2318 } else {
2319 delete $to->{'href'};
2320 }
2321}
2322
2323## ......................................................................
2324## parse to array of hashes functions
2325
2326sub git_get_heads_list {
2327 my $limit = shift;
2328 my @headslist;
2329
2330 open my $fd, '-|', git_cmd(), 'for-each-ref',
2331 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2332 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2333 'refs/heads'
2334 or return;
2335 while (my $line = <$fd>) {
2336 my %ref_item;
2337
2338 chomp $line;
2339 my ($refinfo, $committerinfo) = split(/\0/, $line);
2340 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2341 my ($committer, $epoch, $tz) =
2342 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2343 $ref_item{'fullname'} = $name;
2344 $name =~ s!^refs/heads/!!;
2345
2346 $ref_item{'name'} = $name;
2347 $ref_item{'id'} = $hash;
2348 $ref_item{'title'} = $title || '(no commit message)';
2349 $ref_item{'epoch'} = $epoch;
2350 if ($epoch) {
2351 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2352 } else {
2353 $ref_item{'age'} = "unknown";
2354 }
2355
2356 push @headslist, \%ref_item;
2357 }
2358 close $fd;
2359
2360 return wantarray ? @headslist : \@headslist;
2361}
2362
2363sub git_get_tags_list {
2364 my $limit = shift;
2365 my @tagslist;
2366
2367 open my $fd, '-|', git_cmd(), 'for-each-ref',
2368 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2369 '--format=%(objectname) %(objecttype) %(refname) '.
2370 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2371 'refs/tags'
2372 or return;
2373 while (my $line = <$fd>) {
2374 my %ref_item;
2375
2376 chomp $line;
2377 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2378 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2379 my ($creator, $epoch, $tz) =
2380 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2381 $ref_item{'fullname'} = $name;
2382 $name =~ s!^refs/tags/!!;
2383
2384 $ref_item{'type'} = $type;
2385 $ref_item{'id'} = $id;
2386 $ref_item{'name'} = $name;
2387 if ($type eq "tag") {
2388 $ref_item{'subject'} = $title;
2389 $ref_item{'reftype'} = $reftype;
2390 $ref_item{'refid'} = $refid;
2391 } else {
2392 $ref_item{'reftype'} = $type;
2393 $ref_item{'refid'} = $id;
2394 }
2395
2396 if ($type eq "tag" || $type eq "commit") {
2397 $ref_item{'epoch'} = $epoch;
2398 if ($epoch) {
2399 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2400 } else {
2401 $ref_item{'age'} = "unknown";
2402 }
2403 }
2404
2405 push @tagslist, \%ref_item;
2406 }
2407 close $fd;
2408
2409 return wantarray ? @tagslist : \@tagslist;
2410}
2411
2412## ----------------------------------------------------------------------
2413## filesystem-related functions
2414
2415sub get_file_owner {
2416 my $path = shift;
2417
2418 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2419 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2420 if (!defined $gcos) {
2421 return undef;
2422 }
2423 my $owner = $gcos;
2424 $owner =~ s/[,;].*$//;
2425 return to_utf8($owner);
2426}
2427
2428## ......................................................................
2429## mimetype related functions
2430
2431sub mimetype_guess_file {
2432 my $filename = shift;
2433 my $mimemap = shift;
2434 -r $mimemap or return undef;
2435
2436 my %mimemap;
2437 open(MIME, $mimemap) or return undef;
2438 while (<MIME>) {
2439 next if m/^#/; # skip comments
2440 my ($mime, $exts) = split(/\t+/);
2441 if (defined $exts) {
2442 my @exts = split(/\s+/, $exts);
2443 foreach my $ext (@exts) {
2444 $mimemap{$ext} = $mime;
2445 }
2446 }
2447 }
2448 close(MIME);
2449
2450 $filename =~ /\.([^.]*)$/;
2451 return $mimemap{$1};
2452}
2453
2454sub mimetype_guess {
2455 my $filename = shift;
2456 my $mime;
2457 $filename =~ /\./ or return undef;
2458
2459 if ($mimetypes_file) {
2460 my $file = $mimetypes_file;
2461 if ($file !~ m!^/!) { # if it is relative path
2462 # it is relative to project
2463 $file = "$projectroot/$project/$file";
2464 }
2465 $mime = mimetype_guess_file($filename, $file);
2466 }
2467 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2468 return $mime;
2469}
2470
2471sub blob_mimetype {
2472 my $fd = shift;
2473 my $filename = shift;
2474
2475 if ($filename) {
2476 my $mime = mimetype_guess($filename);
2477 $mime and return $mime;
2478 }
2479
2480 # just in case
2481 return $default_blob_plain_mimetype unless $fd;
2482
2483 if (-T $fd) {
2484 return 'text/plain' .
2485 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
2486 } elsif (! $filename) {
2487 return 'application/octet-stream';
2488 } elsif ($filename =~ m/\.png$/i) {
2489 return 'image/png';
2490 } elsif ($filename =~ m/\.gif$/i) {
2491 return 'image/gif';
2492 } elsif ($filename =~ m/\.jpe?g$/i) {
2493 return 'image/jpeg';
2494 } else {
2495 return 'application/octet-stream';
2496 }
2497}
2498
2499## ======================================================================
2500## functions printing HTML: header, footer, error page
2501
2502sub git_header_html {
2503 my $status = shift || "200 OK";
2504 my $expires = shift;
2505
2506 my $title = "$site_name";
2507 if (defined $project) {
2508 $title .= " - " . to_utf8($project);
2509 if (defined $action) {
2510 $title .= "/$action";
2511 if (defined $file_name) {
2512 $title .= " - " . esc_path($file_name);
2513 if ($action eq "tree" && $file_name !~ m|/$|) {
2514 $title .= "/";
2515 }
2516 }
2517 }
2518 }
2519 my $content_type;
2520 # require explicit support from the UA if we are to send the page as
2521 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2522 # we have to do this because MSIE sometimes globs '*/*', pretending to
2523 # support xhtml+xml but choking when it gets what it asked for.
2524 if (defined $cgi->http('HTTP_ACCEPT') &&
2525 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2526 $cgi->Accept('application/xhtml+xml') != 0) {
2527 $content_type = 'application/xhtml+xml';
2528 } else {
2529 $content_type = 'text/html';
2530 }
2531 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2532 -status=> $status, -expires => $expires);
2533 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2534 print <<EOF;
2535<?xml version="1.0" encoding="utf-8"?>
2536<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2537<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2538<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2539<!-- git core binaries version $git_version -->
2540<head>
2541<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2542<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2543<meta name="robots" content="index, nofollow"/>
2544<title>$title</title>
2545EOF
2546# print out each stylesheet that exist
2547 if (defined $stylesheet) {
2548#provides backwards capability for those people who define style sheet in a config file
2549 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2550 } else {
2551 foreach my $stylesheet (@stylesheets) {
2552 next unless $stylesheet;
2553 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2554 }
2555 }
2556 if (defined $project) {
2557 my %href_params = get_feed_info();
2558 if (!exists $href_params{'-title'}) {
2559 $href_params{'-title'} = 'log';
2560 }
2561
2562 foreach my $format qw(RSS Atom) {
2563 my $type = lc($format);
2564 my %link_attr = (
2565 '-rel' => 'alternate',
2566 '-title' => "$project - $href_params{'-title'} - $format feed",
2567 '-type' => "application/$type+xml"
2568 );
2569
2570 $href_params{'action'} = $type;
2571 $link_attr{'-href'} = href(%href_params);
2572 print "<link ".
2573 "rel=\"$link_attr{'-rel'}\" ".
2574 "title=\"$link_attr{'-title'}\" ".
2575 "href=\"$link_attr{'-href'}\" ".
2576 "type=\"$link_attr{'-type'}\" ".
2577 "/>\n";
2578
2579 $href_params{'extra_options'} = '--no-merges';
2580 $link_attr{'-href'} = href(%href_params);
2581 $link_attr{'-title'} .= ' (no merges)';
2582 print "<link ".
2583 "rel=\"$link_attr{'-rel'}\" ".
2584 "title=\"$link_attr{'-title'}\" ".
2585 "href=\"$link_attr{'-href'}\" ".
2586 "type=\"$link_attr{'-type'}\" ".
2587 "/>\n";
2588 }
2589
2590 } else {
2591 printf('<link rel="alternate" title="%s projects list" '.
2592 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2593 $site_name, href(project=>undef, action=>"project_index"));
2594 printf('<link rel="alternate" title="%s projects feeds" '.
2595 'href="%s" type="text/x-opml" />'."\n",
2596 $site_name, href(project=>undef, action=>"opml"));
2597 }
2598 if (defined $favicon) {
2599 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2600 }
2601
2602 print "</head>\n" .
2603 "<body>\n";
2604
2605 if (-f $site_header) {
2606 open (my $fd, $site_header);
2607 print <$fd>;
2608 close $fd;
2609 }
2610
2611 print "<div class=\"page_header\">\n" .
2612 $cgi->a({-href => esc_url($logo_url),
2613 -title => $logo_label},
2614 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2615 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2616 if (defined $project) {
2617 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2618 if (defined $action) {
2619 print " / $action";
2620 }
2621 print "\n";
2622 }
2623 print "</div>\n";
2624
2625 my ($have_search) = gitweb_check_feature('search');
2626 if (defined $project && $have_search) {
2627 if (!defined $searchtext) {
2628 $searchtext = "";
2629 }
2630 my $search_hash;
2631 if (defined $hash_base) {
2632 $search_hash = $hash_base;
2633 } elsif (defined $hash) {
2634 $search_hash = $hash;
2635 } else {
2636 $search_hash = "HEAD";
2637 }
2638 my $action = $my_uri;
2639 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2640 if ($use_pathinfo) {
2641 $action .= "/".esc_url($project);
2642 }
2643 print $cgi->startform(-method => "get", -action => $action) .
2644 "<div class=\"search\">\n" .
2645 (!$use_pathinfo &&
2646 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2647 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2648 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2649 $cgi->popup_menu(-name => 'st', -default => 'commit',
2650 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2651 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2652 " search:\n",
2653 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2654 "<span title=\"Extended regular expression\">" .
2655 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
2656 -checked => $search_use_regexp) .
2657 "</span>" .
2658 "</div>" .
2659 $cgi->end_form() . "\n";
2660 }
2661}
2662
2663sub git_footer_html {
2664 my $feed_class = 'rss_logo';
2665
2666 print "<div class=\"page_footer\">\n";
2667 if (defined $project) {
2668 my $descr = git_get_project_description($project);
2669 if (defined $descr) {
2670 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2671 }
2672
2673 my %href_params = get_feed_info();
2674 if (!%href_params) {
2675 $feed_class .= ' generic';
2676 }
2677 $href_params{'-title'} ||= 'log';
2678
2679 foreach my $format qw(RSS Atom) {
2680 $href_params{'action'} = lc($format);
2681 print $cgi->a({-href => href(%href_params),
2682 -title => "$href_params{'-title'} $format feed",
2683 -class => $feed_class}, $format)."\n";
2684 }
2685
2686 } else {
2687 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2688 -class => $feed_class}, "OPML") . " ";
2689 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2690 -class => $feed_class}, "TXT") . "\n";
2691 }
2692 print "</div>\n"; # class="page_footer"
2693
2694 if (-f $site_footer) {
2695 open (my $fd, $site_footer);
2696 print <$fd>;
2697 close $fd;
2698 }
2699
2700 print "</body>\n" .
2701 "</html>";
2702}
2703
2704sub die_error {
2705 my $status = shift || "403 Forbidden";
2706 my $error = shift || "Malformed query, file missing or permission denied";
2707
2708 git_header_html($status);
2709 print <<EOF;
2710<div class="page_body">
2711<br /><br />
2712$status - $error
2713<br />
2714</div>
2715EOF
2716 git_footer_html();
2717 exit;
2718}
2719
2720## ----------------------------------------------------------------------
2721## functions printing or outputting HTML: navigation
2722
2723sub git_print_page_nav {
2724 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2725 $extra = '' if !defined $extra; # pager or formats
2726
2727 my @navs = qw(summary shortlog log commit commitdiff tree);
2728 if ($suppress) {
2729 @navs = grep { $_ ne $suppress } @navs;
2730 }
2731
2732 my %arg = map { $_ => {action=>$_} } @navs;
2733 if (defined $head) {
2734 for (qw(commit commitdiff)) {
2735 $arg{$_}{'hash'} = $head;
2736 }
2737 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2738 for (qw(shortlog log)) {
2739 $arg{$_}{'hash'} = $head;
2740 }
2741 }
2742 }
2743 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2744 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2745
2746 print "<div class=\"page_nav\">\n" .
2747 (join " | ",
2748 map { $_ eq $current ?
2749 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2750 } @navs);
2751 print "<br/>\n$extra<br/>\n" .
2752 "</div>\n";
2753}
2754
2755sub format_paging_nav {
2756 my ($action, $hash, $head, $page, $has_next_link) = @_;
2757 my $paging_nav;
2758
2759
2760 if ($hash ne $head || $page) {
2761 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2762 } else {
2763 $paging_nav .= "HEAD";
2764 }
2765
2766 if ($page > 0) {
2767 $paging_nav .= " ⋅ " .
2768 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2769 -accesskey => "p", -title => "Alt-p"}, "prev");
2770 } else {
2771 $paging_nav .= " ⋅ prev";
2772 }
2773
2774 if ($has_next_link) {
2775 $paging_nav .= " ⋅ " .
2776 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2777 -accesskey => "n", -title => "Alt-n"}, "next");
2778 } else {
2779 $paging_nav .= " ⋅ next";
2780 }
2781
2782 return $paging_nav;
2783}
2784
2785## ......................................................................
2786## functions printing or outputting HTML: div
2787
2788sub git_print_header_div {
2789 my ($action, $title, $hash, $hash_base) = @_;
2790 my %args = ();
2791
2792 $args{'action'} = $action;
2793 $args{'hash'} = $hash if $hash;
2794 $args{'hash_base'} = $hash_base if $hash_base;
2795
2796 print "<div class=\"header\">\n" .
2797 $cgi->a({-href => href(%args), -class => "title"},
2798 $title ? $title : $action) .
2799 "\n</div>\n";
2800}
2801
2802#sub git_print_authorship (\%) {
2803sub git_print_authorship {
2804 my $co = shift;
2805
2806 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2807 print "<div class=\"author_date\">" .
2808 esc_html($co->{'author_name'}) .
2809 " [$ad{'rfc2822'}";
2810 if ($ad{'hour_local'} < 6) {
2811 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2812 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2813 } else {
2814 printf(" (%02d:%02d %s)",
2815 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2816 }
2817 print "]</div>\n";
2818}
2819
2820sub git_print_page_path {
2821 my $name = shift;
2822 my $type = shift;
2823 my $hb = shift;
2824
2825
2826 print "<div class=\"page_path\">";
2827 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2828 -title => 'tree root'}, to_utf8("[$project]"));
2829 print " / ";
2830 if (defined $name) {
2831 my @dirname = split '/', $name;
2832 my $basename = pop @dirname;
2833 my $fullname = '';
2834
2835 foreach my $dir (@dirname) {
2836 $fullname .= ($fullname ? '/' : '') . $dir;
2837 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2838 hash_base=>$hb),
2839 -title => $fullname}, esc_path($dir));
2840 print " / ";
2841 }
2842 if (defined $type && $type eq 'blob') {
2843 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2844 hash_base=>$hb),
2845 -title => $name}, esc_path($basename));
2846 } elsif (defined $type && $type eq 'tree') {
2847 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2848 hash_base=>$hb),
2849 -title => $name}, esc_path($basename));
2850 print " / ";
2851 } else {
2852 print esc_path($basename);
2853 }
2854 }
2855 print "<br/></div>\n";
2856}
2857
2858# sub git_print_log (\@;%) {
2859sub git_print_log ($;%) {
2860 my $log = shift;
2861 my %opts = @_;
2862
2863 if ($opts{'-remove_title'}) {
2864 # remove title, i.e. first line of log
2865 shift @$log;
2866 }
2867 # remove leading empty lines
2868 while (defined $log->[0] && $log->[0] eq "") {
2869 shift @$log;
2870 }
2871
2872 # print log
2873 my $signoff = 0;
2874 my $empty = 0;
2875 foreach my $line (@$log) {
2876 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2877 $signoff = 1;
2878 $empty = 0;
2879 if (! $opts{'-remove_signoff'}) {
2880 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2881 next;
2882 } else {
2883 # remove signoff lines
2884 next;
2885 }
2886 } else {
2887 $signoff = 0;
2888 }
2889
2890 # print only one empty line
2891 # do not print empty line after signoff
2892 if ($line eq "") {
2893 next if ($empty || $signoff);
2894 $empty = 1;
2895 } else {
2896 $empty = 0;
2897 }
2898
2899 print format_log_line_html($line) . "<br/>\n";
2900 }
2901
2902 if ($opts{'-final_empty_line'}) {
2903 # end with single empty line
2904 print "<br/>\n" unless $empty;
2905 }
2906}
2907
2908# return link target (what link points to)
2909sub git_get_link_target {
2910 my $hash = shift;
2911 my $link_target;
2912
2913 # read link
2914 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2915 or return;
2916 {
2917 local $/;
2918 $link_target = <$fd>;
2919 }
2920 close $fd
2921 or return;
2922
2923 return $link_target;
2924}
2925
2926# given link target, and the directory (basedir) the link is in,
2927# return target of link relative to top directory (top tree);
2928# return undef if it is not possible (including absolute links).
2929sub normalize_link_target {
2930 my ($link_target, $basedir, $hash_base) = @_;
2931
2932 # we can normalize symlink target only if $hash_base is provided
2933 return unless $hash_base;
2934
2935 # absolute symlinks (beginning with '/') cannot be normalized
2936 return if (substr($link_target, 0, 1) eq '/');
2937
2938 # normalize link target to path from top (root) tree (dir)
2939 my $path;
2940 if ($basedir) {
2941 $path = $basedir . '/' . $link_target;
2942 } else {
2943 # we are in top (root) tree (dir)
2944 $path = $link_target;
2945 }
2946
2947 # remove //, /./, and /../
2948 my @path_parts;
2949 foreach my $part (split('/', $path)) {
2950 # discard '.' and ''
2951 next if (!$part || $part eq '.');
2952 # handle '..'
2953 if ($part eq '..') {
2954 if (@path_parts) {
2955 pop @path_parts;
2956 } else {
2957 # link leads outside repository (outside top dir)
2958 return;
2959 }
2960 } else {
2961 push @path_parts, $part;
2962 }
2963 }
2964 $path = join('/', @path_parts);
2965
2966 return $path;
2967}
2968
2969# print tree entry (row of git_tree), but without encompassing <tr> element
2970sub git_print_tree_entry {
2971 my ($t, $basedir, $hash_base, $have_blame) = @_;
2972
2973 my %base_key = ();
2974 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2975
2976 # The format of a table row is: mode list link. Where mode is
2977 # the mode of the entry, list is the name of the entry, an href,
2978 # and link is the action links of the entry.
2979
2980 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2981 if ($t->{'type'} eq "blob") {
2982 print "<td class=\"list\">" .
2983 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2984 file_name=>"$basedir$t->{'name'}", %base_key),
2985 -class => "list"}, esc_path($t->{'name'}));
2986 if (S_ISLNK(oct $t->{'mode'})) {
2987 my $link_target = git_get_link_target($t->{'hash'});
2988 if ($link_target) {
2989 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2990 if (defined $norm_target) {
2991 print " -> " .
2992 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2993 file_name=>$norm_target),
2994 -title => $norm_target}, esc_path($link_target));
2995 } else {
2996 print " -> " . esc_path($link_target);
2997 }
2998 }
2999 }
3000 print "</td>\n";
3001 print "<td class=\"link\">";
3002 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3003 file_name=>"$basedir$t->{'name'}", %base_key)},
3004 "blob");
3005 if ($have_blame) {
3006 print " | " .
3007 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3008 file_name=>"$basedir$t->{'name'}", %base_key)},
3009 "blame");
3010 }
3011 if (defined $hash_base) {
3012 print " | " .
3013 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3014 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3015 "history");
3016 }
3017 print " | " .
3018 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3019 file_name=>"$basedir$t->{'name'}")},
3020 "raw");
3021 print "</td>\n";
3022
3023 } elsif ($t->{'type'} eq "tree") {
3024 print "<td class=\"list\">";
3025 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3026 file_name=>"$basedir$t->{'name'}", %base_key)},
3027 esc_path($t->{'name'}));
3028 print "</td>\n";
3029 print "<td class=\"link\">";
3030 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3031 file_name=>"$basedir$t->{'name'}", %base_key)},
3032 "tree");
3033 if (defined $hash_base) {
3034 print " | " .
3035 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3036 file_name=>"$basedir$t->{'name'}")},
3037 "history");
3038 }
3039 print "</td>\n";
3040 } else {
3041 # unknown object: we can only present history for it
3042 # (this includes 'commit' object, i.e. submodule support)
3043 print "<td class=\"list\">" .
3044 esc_path($t->{'name'}) .
3045 "</td>\n";
3046 print "<td class=\"link\">";
3047 if (defined $hash_base) {
3048 print $cgi->a({-href => href(action=>"history",
3049 hash_base=>$hash_base,
3050 file_name=>"$basedir$t->{'name'}")},
3051 "history");
3052 }
3053 print "</td>\n";
3054 }
3055}
3056
3057## ......................................................................
3058## functions printing large fragments of HTML
3059
3060# get pre-image filenames for merge (combined) diff
3061sub fill_from_file_info {
3062 my ($diff, @parents) = @_;
3063
3064 $diff->{'from_file'} = [ ];
3065 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3066 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3067 if ($diff->{'status'}[$i] eq 'R' ||
3068 $diff->{'status'}[$i] eq 'C') {
3069 $diff->{'from_file'}[$i] =
3070 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3071 }
3072 }
3073
3074 return $diff;
3075}
3076
3077# is current raw difftree line of file deletion
3078sub is_deleted {
3079 my $diffinfo = shift;
3080
3081 return $diffinfo->{'to_id'} eq ('0' x 40);
3082}
3083
3084# does patch correspond to [previous] difftree raw line
3085# $diffinfo - hashref of parsed raw diff format
3086# $patchinfo - hashref of parsed patch diff format
3087# (the same keys as in $diffinfo)
3088sub is_patch_split {
3089 my ($diffinfo, $patchinfo) = @_;
3090
3091 return defined $diffinfo && defined $patchinfo
3092 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3093}
3094
3095
3096sub git_difftree_body {
3097 my ($difftree, $hash, @parents) = @_;
3098 my ($parent) = $parents[0];
3099 my ($have_blame) = gitweb_check_feature('blame');
3100 print "<div class=\"list_head\">\n";
3101 if ($#{$difftree} > 10) {
3102 print(($#{$difftree} + 1) . " files changed:\n");
3103 }
3104 print "</div>\n";
3105
3106 print "<table class=\"" .
3107 (@parents > 1 ? "combined " : "") .
3108 "diff_tree\">\n";
3109
3110 # header only for combined diff in 'commitdiff' view
3111 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3112 if ($has_header) {
3113 # table header
3114 print "<thead><tr>\n" .
3115 "<th></th><th></th>\n"; # filename, patchN link
3116 for (my $i = 0; $i < @parents; $i++) {
3117 my $par = $parents[$i];
3118 print "<th>" .
3119 $cgi->a({-href => href(action=>"commitdiff",
3120 hash=>$hash, hash_parent=>$par),
3121 -title => 'commitdiff to parent number ' .
3122 ($i+1) . ': ' . substr($par,0,7)},
3123 $i+1) .
3124 " </th>\n";
3125 }
3126 print "</tr></thead>\n<tbody>\n";
3127 }
3128
3129 my $alternate = 1;
3130 my $patchno = 0;
3131 foreach my $line (@{$difftree}) {
3132 my $diff = parsed_difftree_line($line);
3133
3134 if ($alternate) {
3135 print "<tr class=\"dark\">\n";
3136 } else {
3137 print "<tr class=\"light\">\n";
3138 }
3139 $alternate ^= 1;
3140
3141 if (exists $diff->{'nparents'}) { # combined diff
3142
3143 fill_from_file_info($diff, @parents)
3144 unless exists $diff->{'from_file'};
3145
3146 if (!is_deleted($diff)) {
3147 # file exists in the result (child) commit
3148 print "<td>" .
3149 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3150 file_name=>$diff->{'to_file'},
3151 hash_base=>$hash),
3152 -class => "list"}, esc_path($diff->{'to_file'})) .
3153 "</td>\n";
3154 } else {
3155 print "<td>" .
3156 esc_path($diff->{'to_file'}) .
3157 "</td>\n";
3158 }
3159
3160 if ($action eq 'commitdiff') {
3161 # link to patch
3162 $patchno++;
3163 print "<td class=\"link\">" .
3164 $cgi->a({-href => "#patch$patchno"}, "patch") .
3165 " | " .
3166 "</td>\n";
3167 }
3168
3169 my $has_history = 0;
3170 my $not_deleted = 0;
3171 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3172 my $hash_parent = $parents[$i];
3173 my $from_hash = $diff->{'from_id'}[$i];
3174 my $from_path = $diff->{'from_file'}[$i];
3175 my $status = $diff->{'status'}[$i];
3176
3177 $has_history ||= ($status ne 'A');
3178 $not_deleted ||= ($status ne 'D');
3179
3180 if ($status eq 'A') {
3181 print "<td class=\"link\" align=\"right\"> | </td>\n";
3182 } elsif ($status eq 'D') {
3183 print "<td class=\"link\">" .
3184 $cgi->a({-href => href(action=>"blob",
3185 hash_base=>$hash,
3186 hash=>$from_hash,
3187 file_name=>$from_path)},
3188 "blob" . ($i+1)) .
3189 " | </td>\n";
3190 } else {
3191 if ($diff->{'to_id'} eq $from_hash) {
3192 print "<td class=\"link nochange\">";
3193 } else {
3194 print "<td class=\"link\">";
3195 }
3196 print $cgi->a({-href => href(action=>"blobdiff",
3197 hash=>$diff->{'to_id'},
3198 hash_parent=>$from_hash,
3199 hash_base=>$hash,
3200 hash_parent_base=>$hash_parent,
3201 file_name=>$diff->{'to_file'},
3202 file_parent=>$from_path)},
3203 "diff" . ($i+1)) .
3204 " | </td>\n";
3205 }
3206 }
3207
3208 print "<td class=\"link\">";
3209 if ($not_deleted) {
3210 print $cgi->a({-href => href(action=>"blob",
3211 hash=>$diff->{'to_id'},
3212 file_name=>$diff->{'to_file'},
3213 hash_base=>$hash)},
3214 "blob");
3215 print " | " if ($has_history);
3216 }
3217 if ($has_history) {
3218 print $cgi->a({-href => href(action=>"history",
3219 file_name=>$diff->{'to_file'},
3220 hash_base=>$hash)},
3221 "history");
3222 }
3223 print "</td>\n";
3224
3225 print "</tr>\n";
3226 next; # instead of 'else' clause, to avoid extra indent
3227 }
3228 # else ordinary diff
3229
3230 my ($to_mode_oct, $to_mode_str, $to_file_type);
3231 my ($from_mode_oct, $from_mode_str, $from_file_type);
3232 if ($diff->{'to_mode'} ne ('0' x 6)) {
3233 $to_mode_oct = oct $diff->{'to_mode'};
3234 if (S_ISREG($to_mode_oct)) { # only for regular file
3235 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3236 }
3237 $to_file_type = file_type($diff->{'to_mode'});
3238 }
3239 if ($diff->{'from_mode'} ne ('0' x 6)) {
3240 $from_mode_oct = oct $diff->{'from_mode'};
3241 if (S_ISREG($to_mode_oct)) { # only for regular file
3242 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3243 }
3244 $from_file_type = file_type($diff->{'from_mode'});
3245 }
3246
3247 if ($diff->{'status'} eq "A") { # created
3248 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3249 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3250 $mode_chng .= "]</span>";
3251 print "<td>";
3252 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3253 hash_base=>$hash, file_name=>$diff->{'file'}),
3254 -class => "list"}, esc_path($diff->{'file'}));
3255 print "</td>\n";
3256 print "<td>$mode_chng</td>\n";
3257 print "<td class=\"link\">";
3258 if ($action eq 'commitdiff') {
3259 # link to patch
3260 $patchno++;
3261 print $cgi->a({-href => "#patch$patchno"}, "patch");
3262 print " | ";
3263 }
3264 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3265 hash_base=>$hash, file_name=>$diff->{'file'})},
3266 "blob");
3267 print "</td>\n";
3268
3269 } elsif ($diff->{'status'} eq "D") { # deleted
3270 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3271 print "<td>";
3272 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3273 hash_base=>$parent, file_name=>$diff->{'file'}),
3274 -class => "list"}, esc_path($diff->{'file'}));
3275 print "</td>\n";
3276 print "<td>$mode_chng</td>\n";
3277 print "<td class=\"link\">";
3278 if ($action eq 'commitdiff') {
3279 # link to patch
3280 $patchno++;
3281 print $cgi->a({-href => "#patch$patchno"}, "patch");
3282 print " | ";
3283 }
3284 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3285 hash_base=>$parent, file_name=>$diff->{'file'})},
3286 "blob") . " | ";
3287 if ($have_blame) {
3288 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3289 file_name=>$diff->{'file'})},
3290 "blame") . " | ";
3291 }
3292 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3293 file_name=>$diff->{'file'})},
3294 "history");
3295 print "</td>\n";
3296
3297 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3298 my $mode_chnge = "";
3299 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3300 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3301 if ($from_file_type ne $to_file_type) {
3302 $mode_chnge .= " from $from_file_type to $to_file_type";
3303 }
3304 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3305 if ($from_mode_str && $to_mode_str) {
3306 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3307 } elsif ($to_mode_str) {
3308 $mode_chnge .= " mode: $to_mode_str";
3309 }
3310 }
3311 $mode_chnge .= "]</span>\n";
3312 }
3313 print "<td>";
3314 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3315 hash_base=>$hash, file_name=>$diff->{'file'}),
3316 -class => "list"}, esc_path($diff->{'file'}));
3317 print "</td>\n";
3318 print "<td>$mode_chnge</td>\n";
3319 print "<td class=\"link\">";
3320 if ($action eq 'commitdiff') {
3321 # link to patch
3322 $patchno++;
3323 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3324 " | ";
3325 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3326 # "commit" view and modified file (not onlu mode changed)
3327 print $cgi->a({-href => href(action=>"blobdiff",
3328 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3329 hash_base=>$hash, hash_parent_base=>$parent,
3330 file_name=>$diff->{'file'})},
3331 "diff") .
3332 " | ";
3333 }
3334 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3335 hash_base=>$hash, file_name=>$diff->{'file'})},
3336 "blob") . " | ";
3337 if ($have_blame) {
3338 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3339 file_name=>$diff->{'file'})},
3340 "blame") . " | ";
3341 }
3342 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3343 file_name=>$diff->{'file'})},
3344 "history");
3345 print "</td>\n";
3346
3347 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3348 my %status_name = ('R' => 'moved', 'C' => 'copied');
3349 my $nstatus = $status_name{$diff->{'status'}};
3350 my $mode_chng = "";
3351 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3352 # mode also for directories, so we cannot use $to_mode_str
3353 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3354 }
3355 print "<td>" .
3356 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3357 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3358 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3359 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3360 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3361 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3362 -class => "list"}, esc_path($diff->{'from_file'})) .
3363 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3364 "<td class=\"link\">";
3365 if ($action eq 'commitdiff') {
3366 # link to patch
3367 $patchno++;
3368 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3369 " | ";
3370 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3371 # "commit" view and modified file (not only pure rename or copy)
3372 print $cgi->a({-href => href(action=>"blobdiff",
3373 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3374 hash_base=>$hash, hash_parent_base=>$parent,
3375 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3376 "diff") .
3377 " | ";
3378 }
3379 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3380 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3381 "blob") . " | ";
3382 if ($have_blame) {
3383 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3384 file_name=>$diff->{'to_file'})},
3385 "blame") . " | ";
3386 }
3387 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3388 file_name=>$diff->{'to_file'})},
3389 "history");
3390 print "</td>\n";
3391
3392 } # we should not encounter Unmerged (U) or Unknown (X) status
3393 print "</tr>\n";
3394 }
3395 print "</tbody>" if $has_header;
3396 print "</table>\n";
3397}
3398
3399sub git_patchset_body {
3400 my ($fd, $difftree, $hash, @hash_parents) = @_;
3401 my ($hash_parent) = $hash_parents[0];
3402
3403 my $is_combined = (@hash_parents > 1);
3404 my $patch_idx = 0;
3405 my $patch_number = 0;
3406 my $patch_line;
3407 my $diffinfo;
3408 my $to_name;
3409 my (%from, %to);
3410
3411 print "<div class=\"patchset\">\n";
3412
3413 # skip to first patch
3414 while ($patch_line = <$fd>) {
3415 chomp $patch_line;
3416
3417 last if ($patch_line =~ m/^diff /);
3418 }
3419
3420 PATCH:
3421 while ($patch_line) {
3422
3423 # parse "git diff" header line
3424 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3425 # $1 is from_name, which we do not use
3426 $to_name = unquote($2);
3427 $to_name =~ s!^b/!!;
3428 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3429 # $1 is 'cc' or 'combined', which we do not use
3430 $to_name = unquote($2);
3431 } else {
3432 $to_name = undef;
3433 }
3434
3435 # check if current patch belong to current raw line
3436 # and parse raw git-diff line if needed
3437 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3438 # this is continuation of a split patch
3439 print "<div class=\"patch cont\">\n";
3440 } else {
3441 # advance raw git-diff output if needed
3442 $patch_idx++ if defined $diffinfo;
3443
3444 # read and prepare patch information
3445 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3446
3447 # compact combined diff output can have some patches skipped
3448 # find which patch (using pathname of result) we are at now;
3449 if ($is_combined) {
3450 while ($to_name ne $diffinfo->{'to_file'}) {
3451 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3452 format_diff_cc_simplified($diffinfo, @hash_parents) .
3453 "</div>\n"; # class="patch"
3454
3455 $patch_idx++;
3456 $patch_number++;
3457
3458 last if $patch_idx > $#$difftree;
3459 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3460 }
3461 }
3462
3463 # modifies %from, %to hashes
3464 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3465
3466 # this is first patch for raw difftree line with $patch_idx index
3467 # we index @$difftree array from 0, but number patches from 1
3468 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3469 }
3470
3471 # git diff header
3472 #assert($patch_line =~ m/^diff /) if DEBUG;
3473 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3474 $patch_number++;
3475 # print "git diff" header
3476 print format_git_diff_header_line($patch_line, $diffinfo,
3477 \%from, \%to);
3478
3479 # print extended diff header
3480 print "<div class=\"diff extended_header\">\n";
3481 EXTENDED_HEADER:
3482 while ($patch_line = <$fd>) {
3483 chomp $patch_line;
3484
3485 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3486
3487 print format_extended_diff_header_line($patch_line, $diffinfo,
3488 \%from, \%to);
3489 }
3490 print "</div>\n"; # class="diff extended_header"
3491
3492 # from-file/to-file diff header
3493 if (! $patch_line) {
3494 print "</div>\n"; # class="patch"
3495 last PATCH;
3496 }
3497 next PATCH if ($patch_line =~ m/^diff /);
3498 #assert($patch_line =~ m/^---/) if DEBUG;
3499
3500 my $last_patch_line = $patch_line;
3501 $patch_line = <$fd>;
3502 chomp $patch_line;
3503 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3504
3505 print format_diff_from_to_header($last_patch_line, $patch_line,
3506 $diffinfo, \%from, \%to,
3507 @hash_parents);
3508
3509 # the patch itself
3510 LINE:
3511 while ($patch_line = <$fd>) {
3512 chomp $patch_line;
3513
3514 next PATCH if ($patch_line =~ m/^diff /);
3515
3516 print format_diff_line($patch_line, \%from, \%to);
3517 }
3518
3519 } continue {
3520 print "</div>\n"; # class="patch"
3521 }
3522
3523 # for compact combined (--cc) format, with chunk and patch simpliciaction
3524 # patchset might be empty, but there might be unprocessed raw lines
3525 for (++$patch_idx if $patch_number > 0;
3526 $patch_idx < @$difftree;
3527 ++$patch_idx) {
3528 # read and prepare patch information
3529 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3530
3531 # generate anchor for "patch" links in difftree / whatchanged part
3532 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3533 format_diff_cc_simplified($diffinfo, @hash_parents) .
3534 "</div>\n"; # class="patch"
3535
3536 $patch_number++;
3537 }
3538
3539 if ($patch_number == 0) {
3540 if (@hash_parents > 1) {
3541 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3542 } else {
3543 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3544 }
3545 }
3546
3547 print "</div>\n"; # class="patchset"
3548}
3549
3550# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3551
3552sub git_project_list_body {
3553 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3554
3555 my ($check_forks) = gitweb_check_feature('forks');
3556
3557 my @projects;
3558 foreach my $pr (@$projlist) {
3559 my (@aa) = git_get_last_activity($pr->{'path'});
3560 unless (@aa) {
3561 next;
3562 }
3563 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3564 if (!defined $pr->{'descr'}) {
3565 my $descr = git_get_project_description($pr->{'path'}) || "";
3566 $pr->{'descr_long'} = to_utf8($descr);
3567 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3568 }
3569 if (!defined $pr->{'owner'}) {
3570 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3571 }
3572 if ($check_forks) {
3573 my $pname = $pr->{'path'};
3574 if (($pname =~ s/\.git$//) &&
3575 ($pname !~ /\/$/) &&
3576 (-d "$projectroot/$pname")) {
3577 $pr->{'forks'} = "-d $projectroot/$pname";
3578 }
3579 else {
3580 $pr->{'forks'} = 0;
3581 }
3582 }
3583 push @projects, $pr;
3584 }
3585
3586 $order ||= $default_projects_order;
3587 $from = 0 unless defined $from;
3588 $to = $#projects if (!defined $to || $#projects < $to);
3589
3590 print "<table class=\"project_list\">\n";
3591 unless ($no_header) {
3592 print "<tr>\n";
3593 if ($check_forks) {
3594 print "<th></th>\n";
3595 }
3596 if ($order eq "project") {
3597 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3598 print "<th>Project</th>\n";
3599 } else {
3600 print "<th>" .
3601 $cgi->a({-href => href(project=>undef, order=>'project'),
3602 -class => "header"}, "Project") .
3603 "</th>\n";
3604 }
3605 if ($order eq "descr") {
3606 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3607 print "<th>Description</th>\n";
3608 } else {
3609 print "<th>" .
3610 $cgi->a({-href => href(project=>undef, order=>'descr'),
3611 -class => "header"}, "Description") .
3612 "</th>\n";
3613 }
3614 if ($order eq "owner") {
3615 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3616 print "<th>Owner</th>\n";
3617 } else {
3618 print "<th>" .
3619 $cgi->a({-href => href(project=>undef, order=>'owner'),
3620 -class => "header"}, "Owner") .
3621 "</th>\n";
3622 }
3623 if ($order eq "age") {
3624 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3625 print "<th>Last Change</th>\n";
3626 } else {
3627 print "<th>" .
3628 $cgi->a({-href => href(project=>undef, order=>'age'),
3629 -class => "header"}, "Last Change") .
3630 "</th>\n";
3631 }
3632 print "<th></th>\n" .
3633 "</tr>\n";
3634 }
3635 my $alternate = 1;
3636 for (my $i = $from; $i <= $to; $i++) {
3637 my $pr = $projects[$i];
3638 if ($alternate) {
3639 print "<tr class=\"dark\">\n";
3640 } else {
3641 print "<tr class=\"light\">\n";
3642 }
3643 $alternate ^= 1;
3644 if ($check_forks) {
3645 print "<td>";
3646 if ($pr->{'forks'}) {
3647 print "<!-- $pr->{'forks'} -->\n";
3648 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3649 }
3650 print "</td>\n";
3651 }
3652 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3653 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3654 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3655 -class => "list", -title => $pr->{'descr_long'}},
3656 esc_html($pr->{'descr'})) . "</td>\n" .
3657 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3658 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3659 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3660 "<td class=\"link\">" .
3661 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3662 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3663 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3664 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3665 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3666 "</td>\n" .
3667 "</tr>\n";
3668 }
3669 if (defined $extra) {
3670 print "<tr>\n";
3671 if ($check_forks) {
3672 print "<td></td>\n";
3673 }
3674 print "<td colspan=\"5\">$extra</td>\n" .
3675 "</tr>\n";
3676 }
3677 print "</table>\n";
3678}
3679
3680sub git_shortlog_body {
3681 # uses global variable $project
3682 my ($commitlist, $from, $to, $refs, $extra) = @_;
3683
3684 $from = 0 unless defined $from;
3685 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3686
3687 print "<table class=\"shortlog\">\n";
3688 my $alternate = 1;
3689 for (my $i = $from; $i <= $to; $i++) {
3690 my %co = %{$commitlist->[$i]};
3691 my $commit = $co{'id'};
3692 my $ref = format_ref_marker($refs, $commit);
3693 if ($alternate) {
3694 print "<tr class=\"dark\">\n";
3695 } else {
3696 print "<tr class=\"light\">\n";
3697 }
3698 $alternate ^= 1;
3699 my $author = chop_and_escape_str($co{'author_name'}, 10);
3700 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3701 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3702 "<td><i>" . $author . "</i></td>\n" .
3703 "<td>";
3704 print format_subject_html($co{'title'}, $co{'title_short'},
3705 href(action=>"commit", hash=>$commit), $ref);
3706 print "</td>\n" .
3707 "<td class=\"link\">" .
3708 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3709 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3710 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3711 my $snapshot_links = format_snapshot_links($commit);
3712 if (defined $snapshot_links) {
3713 print " | " . $snapshot_links;
3714 }
3715 print "</td>\n" .
3716 "</tr>\n";
3717 }
3718 if (defined $extra) {
3719 print "<tr>\n" .
3720 "<td colspan=\"4\">$extra</td>\n" .
3721 "</tr>\n";
3722 }
3723 print "</table>\n";
3724}
3725
3726sub git_history_body {
3727 # Warning: assumes constant type (blob or tree) during history
3728 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3729
3730 $from = 0 unless defined $from;
3731 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3732
3733 print "<table class=\"history\">\n";
3734 my $alternate = 1;
3735 for (my $i = $from; $i <= $to; $i++) {
3736 my %co = %{$commitlist->[$i]};
3737 if (!%co) {
3738 next;
3739 }
3740 my $commit = $co{'id'};
3741
3742 my $ref = format_ref_marker($refs, $commit);
3743
3744 if ($alternate) {
3745 print "<tr class=\"dark\">\n";
3746 } else {
3747 print "<tr class=\"light\">\n";
3748 }
3749 $alternate ^= 1;
3750 # shortlog uses chop_str($co{'author_name'}, 10)
3751 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3752 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3753 "<td><i>" . $author . "</i></td>\n" .
3754 "<td>";
3755 # originally git_history used chop_str($co{'title'}, 50)
3756 print format_subject_html($co{'title'}, $co{'title_short'},
3757 href(action=>"commit", hash=>$commit), $ref);
3758 print "</td>\n" .
3759 "<td class=\"link\">" .
3760 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3761 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3762
3763 if ($ftype eq 'blob') {
3764 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3765 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3766 if (defined $blob_current && defined $blob_parent &&
3767 $blob_current ne $blob_parent) {
3768 print " | " .
3769 $cgi->a({-href => href(action=>"blobdiff",
3770 hash=>$blob_current, hash_parent=>$blob_parent,
3771 hash_base=>$hash_base, hash_parent_base=>$commit,
3772 file_name=>$file_name)},
3773 "diff to current");
3774 }
3775 }
3776 print "</td>\n" .
3777 "</tr>\n";
3778 }
3779 if (defined $extra) {
3780 print "<tr>\n" .
3781 "<td colspan=\"4\">$extra</td>\n" .
3782 "</tr>\n";
3783 }
3784 print "</table>\n";
3785}
3786
3787sub git_tags_body {
3788 # uses global variable $project
3789 my ($taglist, $from, $to, $extra) = @_;
3790 $from = 0 unless defined $from;
3791 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3792
3793 print "<table class=\"tags\">\n";
3794 my $alternate = 1;
3795 for (my $i = $from; $i <= $to; $i++) {
3796 my $entry = $taglist->[$i];
3797 my %tag = %$entry;
3798 my $comment = $tag{'subject'};
3799 my $comment_short;
3800 if (defined $comment) {
3801 $comment_short = chop_str($comment, 30, 5);
3802 }
3803 if ($alternate) {
3804 print "<tr class=\"dark\">\n";
3805 } else {
3806 print "<tr class=\"light\">\n";
3807 }
3808 $alternate ^= 1;
3809 if (defined $tag{'age'}) {
3810 print "<td><i>$tag{'age'}</i></td>\n";
3811 } else {
3812 print "<td></td>\n";
3813 }
3814 print "<td>" .
3815 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3816 -class => "list name"}, esc_html($tag{'name'})) .
3817 "</td>\n" .
3818 "<td>";
3819 if (defined $comment) {
3820 print format_subject_html($comment, $comment_short,
3821 href(action=>"tag", hash=>$tag{'id'}));
3822 }
3823 print "</td>\n" .
3824 "<td class=\"selflink\">";
3825 if ($tag{'type'} eq "tag") {
3826 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3827 } else {
3828 print " ";
3829 }
3830 print "</td>\n" .
3831 "<td class=\"link\">" . " | " .
3832 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3833 if ($tag{'reftype'} eq "commit") {
3834 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
3835 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
3836 } elsif ($tag{'reftype'} eq "blob") {
3837 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3838 }
3839 print "</td>\n" .
3840 "</tr>";
3841 }
3842 if (defined $extra) {
3843 print "<tr>\n" .
3844 "<td colspan=\"5\">$extra</td>\n" .
3845 "</tr>\n";
3846 }
3847 print "</table>\n";
3848}
3849
3850sub git_heads_body {
3851 # uses global variable $project
3852 my ($headlist, $head, $from, $to, $extra) = @_;
3853 $from = 0 unless defined $from;
3854 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3855
3856 print "<table class=\"heads\">\n";
3857 my $alternate = 1;
3858 for (my $i = $from; $i <= $to; $i++) {
3859 my $entry = $headlist->[$i];
3860 my %ref = %$entry;
3861 my $curr = $ref{'id'} eq $head;
3862 if ($alternate) {
3863 print "<tr class=\"dark\">\n";
3864 } else {
3865 print "<tr class=\"light\">\n";
3866 }
3867 $alternate ^= 1;
3868 print "<td><i>$ref{'age'}</i></td>\n" .
3869 ($curr ? "<td class=\"current_head\">" : "<td>") .
3870 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
3871 -class => "list name"},esc_html($ref{'name'})) .
3872 "</td>\n" .
3873 "<td class=\"link\">" .
3874 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
3875 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
3876 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
3877 "</td>\n" .
3878 "</tr>";
3879 }
3880 if (defined $extra) {
3881 print "<tr>\n" .
3882 "<td colspan=\"3\">$extra</td>\n" .
3883 "</tr>\n";
3884 }
3885 print "</table>\n";
3886}
3887
3888sub git_search_grep_body {
3889 my ($commitlist, $from, $to, $extra) = @_;
3890 $from = 0 unless defined $from;
3891 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3892
3893 print "<table class=\"commit_search\">\n";
3894 my $alternate = 1;
3895 for (my $i = $from; $i <= $to; $i++) {
3896 my %co = %{$commitlist->[$i]};
3897 if (!%co) {
3898 next;
3899 }
3900 my $commit = $co{'id'};
3901 if ($alternate) {
3902 print "<tr class=\"dark\">\n";
3903 } else {
3904 print "<tr class=\"light\">\n";
3905 }
3906 $alternate ^= 1;
3907 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3908 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3909 "<td><i>" . $author . "</i></td>\n" .
3910 "<td>" .
3911 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3912 -class => "list subject"},
3913 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3914 my $comment = $co{'comment'};
3915 foreach my $line (@$comment) {
3916 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
3917 my ($lead, $match, $trail) = ($1, $2, $3);
3918 $match = chop_str($match, 70, 5, 'center');
3919 my $contextlen = int((80 - length($match))/2);
3920 $contextlen = 30 if ($contextlen > 30);
3921 $lead = chop_str($lead, $contextlen, 10, 'left');
3922 $trail = chop_str($trail, $contextlen, 10, 'right');
3923
3924 $lead = esc_html($lead);
3925 $match = esc_html($match);
3926 $trail = esc_html($trail);
3927
3928 print "$lead<span class=\"match\">$match</span>$trail<br />";
3929 }
3930 }
3931 print "</td>\n" .
3932 "<td class=\"link\">" .
3933 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3934 " | " .
3935 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
3936 " | " .
3937 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3938 print "</td>\n" .
3939 "</tr>\n";
3940 }
3941 if (defined $extra) {
3942 print "<tr>\n" .
3943 "<td colspan=\"3\">$extra</td>\n" .
3944 "</tr>\n";
3945 }
3946 print "</table>\n";
3947}
3948
3949## ======================================================================
3950## ======================================================================
3951## actions
3952
3953sub git_project_list {
3954 my $order = $cgi->param('o');
3955 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3956 die_error(undef, "Unknown order parameter");
3957 }
3958
3959 my @list = git_get_projects_list();
3960 if (!@list) {
3961 die_error(undef, "No projects found");
3962 }
3963
3964 git_header_html();
3965 if (-f $home_text) {
3966 print "<div class=\"index_include\">\n";
3967 open (my $fd, $home_text);
3968 print <$fd>;
3969 close $fd;
3970 print "</div>\n";
3971 }
3972 git_project_list_body(\@list, $order);
3973 git_footer_html();
3974}
3975
3976sub git_forks {
3977 my $order = $cgi->param('o');
3978 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3979 die_error(undef, "Unknown order parameter");
3980 }
3981
3982 my @list = git_get_projects_list($project);
3983 if (!@list) {
3984 die_error(undef, "No forks found");
3985 }
3986
3987 git_header_html();
3988 git_print_page_nav('','');
3989 git_print_header_div('summary', "$project forks");
3990 git_project_list_body(\@list, $order);
3991 git_footer_html();
3992}
3993
3994sub git_project_index {
3995 my @projects = git_get_projects_list($project);
3996
3997 print $cgi->header(
3998 -type => 'text/plain',
3999 -charset => 'utf-8',
4000 -content_disposition => 'inline; filename="index.aux"');
4001
4002 foreach my $pr (@projects) {
4003 if (!exists $pr->{'owner'}) {
4004 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4005 }
4006
4007 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4008 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4009 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4010 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4011 $path =~ s/ /\+/g;
4012 $owner =~ s/ /\+/g;
4013
4014 print "$path $owner\n";
4015 }
4016}
4017
4018sub git_summary {
4019 my $descr = git_get_project_description($project) || "none";
4020 my %co = parse_commit("HEAD");
4021 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4022 my $head = $co{'id'};
4023
4024 my $owner = git_get_project_owner($project);
4025
4026 my $refs = git_get_references();
4027 # These get_*_list functions return one more to allow us to see if
4028 # there are more ...
4029 my @taglist = git_get_tags_list(16);
4030 my @headlist = git_get_heads_list(16);
4031 my @forklist;
4032 my ($check_forks) = gitweb_check_feature('forks');
4033
4034 if ($check_forks) {
4035 @forklist = git_get_projects_list($project);
4036 }
4037
4038 git_header_html();
4039 git_print_page_nav('summary','', $head);
4040
4041 print "<div class=\"title\"> </div>\n";
4042 print "<table class=\"projects_list\">\n" .
4043 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4044 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4045 if (defined $cd{'rfc2822'}) {
4046 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4047 }
4048
4049 # use per project git URL list in $projectroot/$project/cloneurl
4050 # or make project git URL from git base URL and project name
4051 my $url_tag = "URL";
4052 my @url_list = git_get_project_url_list($project);
4053 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4054 foreach my $git_url (@url_list) {
4055 next unless $git_url;
4056 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
4057 $url_tag = "";
4058 }
4059 print "</table>\n";
4060
4061 if (-s "$projectroot/$project/README.html") {
4062 if (open my $fd, "$projectroot/$project/README.html") {
4063 print "<div class=\"title\">readme</div>\n" .
4064 "<div class=\"readme\">\n";
4065 print $_ while (<$fd>);
4066 print "\n</div>\n"; # class="readme"
4067 close $fd;
4068 }
4069 }
4070
4071 # we need to request one more than 16 (0..15) to check if
4072 # those 16 are all
4073 my @commitlist = $head ? parse_commits($head, 17) : ();
4074 if (@commitlist) {
4075 git_print_header_div('shortlog');
4076 git_shortlog_body(\@commitlist, 0, 15, $refs,
4077 $#commitlist <= 15 ? undef :
4078 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4079 }
4080
4081 if (@taglist) {
4082 git_print_header_div('tags');
4083 git_tags_body(\@taglist, 0, 15,
4084 $#taglist <= 15 ? undef :
4085 $cgi->a({-href => href(action=>"tags")}, "..."));
4086 }
4087
4088 if (@headlist) {
4089 git_print_header_div('heads');
4090 git_heads_body(\@headlist, $head, 0, 15,
4091 $#headlist <= 15 ? undef :
4092 $cgi->a({-href => href(action=>"heads")}, "..."));
4093 }
4094
4095 if (@forklist) {
4096 git_print_header_div('forks');
4097 git_project_list_body(\@forklist, undef, 0, 15,
4098 $#forklist <= 15 ? undef :
4099 $cgi->a({-href => href(action=>"forks")}, "..."),
4100 'noheader');
4101 }
4102
4103 git_footer_html();
4104}
4105
4106sub git_tag {
4107 my $head = git_get_head_hash($project);
4108 git_header_html();
4109 git_print_page_nav('','', $head,undef,$head);
4110 my %tag = parse_tag($hash);
4111
4112 if (! %tag) {
4113 die_error(undef, "Unknown tag object");
4114 }
4115
4116 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4117 print "<div class=\"title_text\">\n" .
4118 "<table class=\"object_header\">\n" .
4119 "<tr>\n" .
4120 "<td>object</td>\n" .
4121 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4122 $tag{'object'}) . "</td>\n" .
4123 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4124 $tag{'type'}) . "</td>\n" .
4125 "</tr>\n";
4126 if (defined($tag{'author'})) {
4127 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4128 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4129 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4130 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4131 "</td></tr>\n";
4132 }
4133 print "</table>\n\n" .
4134 "</div>\n";
4135 print "<div class=\"page_body\">";
4136 my $comment = $tag{'comment'};
4137 foreach my $line (@$comment) {
4138 chomp $line;
4139 print esc_html($line, -nbsp=>1) . "<br/>\n";
4140 }
4141 print "</div>\n";
4142 git_footer_html();
4143}
4144
4145sub git_blame2 {
4146 my $fd;
4147 my $ftype;
4148
4149 my ($have_blame) = gitweb_check_feature('blame');
4150 if (!$have_blame) {
4151 die_error('403 Permission denied', "Permission denied");
4152 }
4153 die_error('404 Not Found', "File name not defined") if (!$file_name);
4154 $hash_base ||= git_get_head_hash($project);
4155 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4156 my %co = parse_commit($hash_base)
4157 or die_error(undef, "Reading commit failed");
4158 if (!defined $hash) {
4159 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4160 or die_error(undef, "Error looking up file");
4161 }
4162 $ftype = git_get_type($hash);
4163 if ($ftype !~ "blob") {
4164 die_error('400 Bad Request', "Object is not a blob");
4165 }
4166 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4167 $file_name, $hash_base)
4168 or die_error(undef, "Open git-blame failed");
4169 git_header_html();
4170 my $formats_nav =
4171 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4172 "blob") .
4173 " | " .
4174 $cgi->a({-href => href(action=>"history", -replay=>1)},
4175 "history") .
4176 " | " .
4177 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4178 "HEAD");
4179 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4180 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4181 git_print_page_path($file_name, $ftype, $hash_base);
4182 my @rev_color = (qw(light2 dark2));
4183 my $num_colors = scalar(@rev_color);
4184 my $current_color = 0;
4185 my $last_rev;
4186 print <<HTML;
4187<div class="page_body">
4188<table class="blame">
4189<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4190HTML
4191 my %metainfo = ();
4192 while (1) {
4193 $_ = <$fd>;
4194 last unless defined $_;
4195 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4196 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4197 if (!exists $metainfo{$full_rev}) {
4198 $metainfo{$full_rev} = {};
4199 }
4200 my $meta = $metainfo{$full_rev};
4201 while (<$fd>) {
4202 last if (s/^\t//);
4203 if (/^(\S+) (.*)$/) {
4204 $meta->{$1} = $2;
4205 }
4206 }
4207 my $data = $_;
4208 chomp $data;
4209 my $rev = substr($full_rev, 0, 8);
4210 my $author = $meta->{'author'};
4211 my %date = parse_date($meta->{'author-time'},
4212 $meta->{'author-tz'});
4213 my $date = $date{'iso-tz'};
4214 if ($group_size) {
4215 $current_color = ++$current_color % $num_colors;
4216 }
4217 print "<tr class=\"$rev_color[$current_color]\">\n";
4218 if ($group_size) {
4219 print "<td class=\"sha1\"";
4220 print " title=\"". esc_html($author) . ", $date\"";
4221 print " rowspan=\"$group_size\"" if ($group_size > 1);
4222 print ">";
4223 print $cgi->a({-href => href(action=>"commit",
4224 hash=>$full_rev,
4225 file_name=>$file_name)},
4226 esc_html($rev));
4227 print "</td>\n";
4228 }
4229 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4230 or die_error(undef, "Open git-rev-parse failed");
4231 my $parent_commit = <$dd>;
4232 close $dd;
4233 chomp($parent_commit);
4234 my $blamed = href(action => 'blame',
4235 file_name => $meta->{'filename'},
4236 hash_base => $parent_commit);
4237 print "<td class=\"linenr\">";
4238 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4239 -id => "l$lineno",
4240 -class => "linenr" },
4241 esc_html($lineno));
4242 print "</td>";
4243 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4244 print "</tr>\n";
4245 }
4246 print "</table>\n";
4247 print "</div>";
4248 close $fd
4249 or print "Reading blob failed\n";
4250 git_footer_html();
4251}
4252
4253sub git_blame {
4254 my $fd;
4255
4256 my ($have_blame) = gitweb_check_feature('blame');
4257 if (!$have_blame) {
4258 die_error('403 Permission denied', "Permission denied");
4259 }
4260 die_error('404 Not Found', "File name not defined") if (!$file_name);
4261 $hash_base ||= git_get_head_hash($project);
4262 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4263 my %co = parse_commit($hash_base)
4264 or die_error(undef, "Reading commit failed");
4265 if (!defined $hash) {
4266 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4267 or die_error(undef, "Error lookup file");
4268 }
4269 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
4270 or die_error(undef, "Open git-annotate failed");
4271 git_header_html();
4272 my $formats_nav =
4273 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4274 "blob") .
4275 " | " .
4276 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4277 "history") .
4278 " | " .
4279 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4280 "HEAD");
4281 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4282 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4283 git_print_page_path($file_name, 'blob', $hash_base);
4284 print "<div class=\"page_body\">\n";
4285 print <<HTML;
4286<table class="blame">
4287 <tr>
4288 <th>Commit</th>
4289 <th>Age</th>
4290 <th>Author</th>
4291 <th>Line</th>
4292 <th>Data</th>
4293 </tr>
4294HTML
4295 my @line_class = (qw(light dark));
4296 my $line_class_len = scalar (@line_class);
4297 my $line_class_num = $#line_class;
4298 while (my $line = <$fd>) {
4299 my $long_rev;
4300 my $short_rev;
4301 my $author;
4302 my $time;
4303 my $lineno;
4304 my $data;
4305 my $age;
4306 my $age_str;
4307 my $age_class;
4308
4309 chomp $line;
4310 $line_class_num = ($line_class_num + 1) % $line_class_len;
4311
4312 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
4313 $long_rev = $1;
4314 $author = $2;
4315 $time = $3;
4316 $lineno = $4;
4317 $data = $5;
4318 } else {
4319 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
4320 next;
4321 }
4322 $short_rev = substr ($long_rev, 0, 8);
4323 $age = time () - $time;
4324 $age_str = age_string ($age);
4325 $age_str =~ s/ / /g;
4326 $age_class = age_class($age);
4327 $author = esc_html ($author);
4328 $author =~ s/ / /g;
4329
4330 $data = untabify($data);
4331 $data = esc_html ($data);
4332
4333 print <<HTML;
4334 <tr class="$line_class[$line_class_num]">
4335 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
4336 <td class="$age_class">$age_str</td>
4337 <td>$author</td>
4338 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4339 <td class="pre">$data</td>
4340 </tr>
4341HTML
4342 } # while (my $line = <$fd>)
4343 print "</table>\n\n";
4344 close $fd
4345 or print "Reading blob failed.\n";
4346 print "</div>";
4347 git_footer_html();
4348}
4349
4350sub git_tags {
4351 my $head = git_get_head_hash($project);
4352 git_header_html();
4353 git_print_page_nav('','', $head,undef,$head);
4354 git_print_header_div('summary', $project);
4355
4356 my @tagslist = git_get_tags_list();
4357 if (@tagslist) {
4358 git_tags_body(\@tagslist);
4359 }
4360 git_footer_html();
4361}
4362
4363sub git_heads {
4364 my $head = git_get_head_hash($project);
4365 git_header_html();
4366 git_print_page_nav('','', $head,undef,$head);
4367 git_print_header_div('summary', $project);
4368
4369 my @headslist = git_get_heads_list();
4370 if (@headslist) {
4371 git_heads_body(\@headslist, $head);
4372 }
4373 git_footer_html();
4374}
4375
4376sub git_blob_plain {
4377 my $expires;
4378
4379 if (!defined $hash) {
4380 if (defined $file_name) {
4381 my $base = $hash_base || git_get_head_hash($project);
4382 $hash = git_get_hash_by_path($base, $file_name, "blob")
4383 or die_error(undef, "Error lookup file");
4384 } else {
4385 die_error(undef, "No file name defined");
4386 }
4387 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4388 # blobs defined by non-textual hash id's can be cached
4389 $expires = "+1d";
4390 }
4391
4392 my $type = shift;
4393 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4394 or die_error(undef, "Couldn't cat $file_name, $hash");
4395
4396 $type ||= blob_mimetype($fd, $file_name);
4397
4398 # save as filename, even when no $file_name is given
4399 my $save_as = "$hash";
4400 if (defined $file_name) {
4401 $save_as = $file_name;
4402 } elsif ($type =~ m/^text\//) {
4403 $save_as .= '.txt';
4404 }
4405
4406 print $cgi->header(
4407 -type => "$type",
4408 -expires=>$expires,
4409 -content_disposition => 'inline; filename="' . "$save_as" . '"');
4410 undef $/;
4411 binmode STDOUT, ':raw';
4412 print <$fd>;
4413 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4414 $/ = "\n";
4415 close $fd;
4416}
4417
4418sub git_blob {
4419 my $expires;
4420
4421 if (!defined $hash) {
4422 if (defined $file_name) {
4423 my $base = $hash_base || git_get_head_hash($project);
4424 $hash = git_get_hash_by_path($base, $file_name, "blob")
4425 or die_error(undef, "Error lookup file");
4426 } else {
4427 die_error(undef, "No file name defined");
4428 }
4429 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4430 # blobs defined by non-textual hash id's can be cached
4431 $expires = "+1d";
4432 }
4433
4434 my ($have_blame) = gitweb_check_feature('blame');
4435 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4436 or die_error(undef, "Couldn't cat $file_name, $hash");
4437 my $mimetype = blob_mimetype($fd, $file_name);
4438 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4439 close $fd;
4440 return git_blob_plain($mimetype);
4441 }
4442 # we can have blame only for text/* mimetype
4443 $have_blame &&= ($mimetype =~ m!^text/!);
4444
4445 git_header_html(undef, $expires);
4446 my $formats_nav = '';
4447 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4448 if (defined $file_name) {
4449 if ($have_blame) {
4450 $formats_nav .=
4451 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4452 "blame") .
4453 " | ";
4454 }
4455 $formats_nav .=
4456 $cgi->a({-href => href(action=>"history", -replay=>1)},
4457 "history") .
4458 " | " .
4459 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4460 "raw") .
4461 " | " .
4462 $cgi->a({-href => href(action=>"blob",
4463 hash_base=>"HEAD", file_name=>$file_name)},
4464 "HEAD");
4465 } else {
4466 $formats_nav .=
4467 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4468 "raw");
4469 }
4470 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4471 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4472 } else {
4473 print "<div class=\"page_nav\">\n" .
4474 "<br/><br/></div>\n" .
4475 "<div class=\"title\">$hash</div>\n";
4476 }
4477 git_print_page_path($file_name, "blob", $hash_base);
4478 print "<div class=\"page_body\">\n";
4479 if ($mimetype =~ m!^image/!) {
4480 print qq!<img type="$mimetype"!;
4481 if ($file_name) {
4482 print qq! alt="$file_name" title="$file_name"!;
4483 }
4484 print qq! src="! .
4485 href(action=>"blob_plain", hash=>$hash,
4486 hash_base=>$hash_base, file_name=>$file_name) .
4487 qq!" />\n!;
4488 } else {
4489 my $nr;
4490 while (my $line = <$fd>) {
4491 chomp $line;
4492 $nr++;
4493 $line = untabify($line);
4494 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4495 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4496 }
4497 }
4498 close $fd
4499 or print "Reading blob failed.\n";
4500 print "</div>";
4501 git_footer_html();
4502}
4503
4504sub git_tree {
4505 if (!defined $hash_base) {
4506 $hash_base = "HEAD";
4507 }
4508 if (!defined $hash) {
4509 if (defined $file_name) {
4510 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4511 } else {
4512 $hash = $hash_base;
4513 }
4514 }
4515 $/ = "\0";
4516 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4517 or die_error(undef, "Open git-ls-tree failed");
4518 my @entries = map { chomp; $_ } <$fd>;
4519 close $fd or die_error(undef, "Reading tree failed");
4520 $/ = "\n";
4521
4522 my $refs = git_get_references();
4523 my $ref = format_ref_marker($refs, $hash_base);
4524 git_header_html();
4525 my $basedir = '';
4526 my ($have_blame) = gitweb_check_feature('blame');
4527 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4528 my @views_nav = ();
4529 if (defined $file_name) {
4530 push @views_nav,
4531 $cgi->a({-href => href(action=>"history", -replay=>1)},
4532 "history"),
4533 $cgi->a({-href => href(action=>"tree",
4534 hash_base=>"HEAD", file_name=>$file_name)},
4535 "HEAD"),
4536 }
4537 my $snapshot_links = format_snapshot_links($hash);
4538 if (defined $snapshot_links) {
4539 # FIXME: Should be available when we have no hash base as well.
4540 push @views_nav, $snapshot_links;
4541 }
4542 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4543 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4544 } else {
4545 undef $hash_base;
4546 print "<div class=\"page_nav\">\n";
4547 print "<br/><br/></div>\n";
4548 print "<div class=\"title\">$hash</div>\n";
4549 }
4550 if (defined $file_name) {
4551 $basedir = $file_name;
4552 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4553 $basedir .= '/';
4554 }
4555 }
4556 git_print_page_path($file_name, 'tree', $hash_base);
4557 print "<div class=\"page_body\">\n";
4558 print "<table class=\"tree\">\n";
4559 my $alternate = 1;
4560 # '..' (top directory) link if possible
4561 if (defined $hash_base &&
4562 defined $file_name && $file_name =~ m![^/]+$!) {
4563 if ($alternate) {
4564 print "<tr class=\"dark\">\n";
4565 } else {
4566 print "<tr class=\"light\">\n";
4567 }
4568 $alternate ^= 1;
4569
4570 my $up = $file_name;
4571 $up =~ s!/?[^/]+$!!;
4572 undef $up unless $up;
4573 # based on git_print_tree_entry
4574 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4575 print '<td class="list">';
4576 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4577 file_name=>$up)},
4578 "..");
4579 print "</td>\n";
4580 print "<td class=\"link\"></td>\n";
4581
4582 print "</tr>\n";
4583 }
4584 foreach my $line (@entries) {
4585 my %t = parse_ls_tree_line($line, -z => 1);
4586
4587 if ($alternate) {
4588 print "<tr class=\"dark\">\n";
4589 } else {
4590 print "<tr class=\"light\">\n";
4591 }
4592 $alternate ^= 1;
4593
4594 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4595
4596 print "</tr>\n";
4597 }
4598 print "</table>\n" .
4599 "</div>";
4600 git_footer_html();
4601}
4602
4603sub git_snapshot {
4604 my @supported_fmts = gitweb_check_feature('snapshot');
4605 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4606
4607 my $format = $cgi->param('sf');
4608 if (!@supported_fmts) {
4609 die_error('403 Permission denied', "Permission denied");
4610 }
4611 # default to first supported snapshot format
4612 $format ||= $supported_fmts[0];
4613 if ($format !~ m/^[a-z0-9]+$/) {
4614 die_error(undef, "Invalid snapshot format parameter");
4615 } elsif (!exists($known_snapshot_formats{$format})) {
4616 die_error(undef, "Unknown snapshot format");
4617 } elsif (!grep($_ eq $format, @supported_fmts)) {
4618 die_error(undef, "Unsupported snapshot format");
4619 }
4620
4621 if (!defined $hash) {
4622 $hash = git_get_head_hash($project);
4623 }
4624
4625 my $git_command = git_cmd_str();
4626 my $name = $project;
4627 $name =~ s,([^/])/*\.git$,$1,;
4628 $name = basename($name);
4629 my $filename = to_utf8($name);
4630 $name =~ s/\047/\047\\\047\047/g;
4631 my $cmd;
4632 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4633 $cmd = "$git_command archive " .
4634 "--format=$known_snapshot_formats{$format}{'format'} " .
4635 "--prefix=\'$name\'/ $hash";
4636 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4637 $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4638 }
4639
4640 print $cgi->header(
4641 -type => $known_snapshot_formats{$format}{'type'},
4642 -content_disposition => 'inline; filename="' . "$filename" . '"',
4643 -status => '200 OK');
4644
4645 open my $fd, "-|", $cmd
4646 or die_error(undef, "Execute git-archive failed");
4647 binmode STDOUT, ':raw';
4648 print <$fd>;
4649 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4650 close $fd;
4651}
4652
4653sub git_log {
4654 my $head = git_get_head_hash($project);
4655 if (!defined $hash) {
4656 $hash = $head;
4657 }
4658 if (!defined $page) {
4659 $page = 0;
4660 }
4661 my $refs = git_get_references();
4662
4663 my @commitlist = parse_commits($hash, 101, (100 * $page));
4664
4665 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
4666
4667 git_header_html();
4668 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4669
4670 if (!@commitlist) {
4671 my %co = parse_commit($hash);
4672
4673 git_print_header_div('summary', $project);
4674 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4675 }
4676 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4677 for (my $i = 0; $i <= $to; $i++) {
4678 my %co = %{$commitlist[$i]};
4679 next if !%co;
4680 my $commit = $co{'id'};
4681 my $ref = format_ref_marker($refs, $commit);
4682 my %ad = parse_date($co{'author_epoch'});
4683 git_print_header_div('commit',
4684 "<span class=\"age\">$co{'age_string'}</span>" .
4685 esc_html($co{'title'}) . $ref,
4686 $commit);
4687 print "<div class=\"title_text\">\n" .
4688 "<div class=\"log_link\">\n" .
4689 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4690 " | " .
4691 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4692 " | " .
4693 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4694 "<br/>\n" .
4695 "</div>\n" .
4696 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4697 "</div>\n";
4698
4699 print "<div class=\"log_body\">\n";
4700 git_print_log($co{'comment'}, -final_empty_line=> 1);
4701 print "</div>\n";
4702 }
4703 if ($#commitlist >= 100) {
4704 print "<div class=\"page_nav\">\n";
4705 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4706 -accesskey => "n", -title => "Alt-n"}, "next");
4707 print "</div>\n";
4708 }
4709 git_footer_html();
4710}
4711
4712sub git_commit {
4713 $hash ||= $hash_base || "HEAD";
4714 my %co = parse_commit($hash);
4715 if (!%co) {
4716 die_error(undef, "Unknown commit object");
4717 }
4718 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4719 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4720
4721 my $parent = $co{'parent'};
4722 my $parents = $co{'parents'}; # listref
4723
4724 # we need to prepare $formats_nav before any parameter munging
4725 my $formats_nav;
4726 if (!defined $parent) {
4727 # --root commitdiff
4728 $formats_nav .= '(initial)';
4729 } elsif (@$parents == 1) {
4730 # single parent commit
4731 $formats_nav .=
4732 '(parent: ' .
4733 $cgi->a({-href => href(action=>"commit",
4734 hash=>$parent)},
4735 esc_html(substr($parent, 0, 7))) .
4736 ')';
4737 } else {
4738 # merge commit
4739 $formats_nav .=
4740 '(merge: ' .
4741 join(' ', map {
4742 $cgi->a({-href => href(action=>"commit",
4743 hash=>$_)},
4744 esc_html(substr($_, 0, 7)));
4745 } @$parents ) .
4746 ')';
4747 }
4748
4749 if (!defined $parent) {
4750 $parent = "--root";
4751 }
4752 my @difftree;
4753 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4754 @diff_opts,
4755 (@$parents <= 1 ? $parent : '-c'),
4756 $hash, "--"
4757 or die_error(undef, "Open git-diff-tree failed");
4758 @difftree = map { chomp; $_ } <$fd>;
4759 close $fd or die_error(undef, "Reading git-diff-tree failed");
4760
4761 # non-textual hash id's can be cached
4762 my $expires;
4763 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4764 $expires = "+1d";
4765 }
4766 my $refs = git_get_references();
4767 my $ref = format_ref_marker($refs, $co{'id'});
4768
4769 git_header_html(undef, $expires);
4770 git_print_page_nav('commit', '',
4771 $hash, $co{'tree'}, $hash,
4772 $formats_nav);
4773
4774 if (defined $co{'parent'}) {
4775 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4776 } else {
4777 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4778 }
4779 print "<div class=\"title_text\">\n" .
4780 "<table class=\"object_header\">\n";
4781 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4782 "<tr>" .
4783 "<td></td><td> $ad{'rfc2822'}";
4784 if ($ad{'hour_local'} < 6) {
4785 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4786 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4787 } else {
4788 printf(" (%02d:%02d %s)",
4789 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4790 }
4791 print "</td>" .
4792 "</tr>\n";
4793 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4794 print "<tr><td></td><td> $cd{'rfc2822'}" .
4795 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4796 "</td></tr>\n";
4797 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4798 print "<tr>" .
4799 "<td>tree</td>" .
4800 "<td class=\"sha1\">" .
4801 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4802 class => "list"}, $co{'tree'}) .
4803 "</td>" .
4804 "<td class=\"link\">" .
4805 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4806 "tree");
4807 my $snapshot_links = format_snapshot_links($hash);
4808 if (defined $snapshot_links) {
4809 print " | " . $snapshot_links;
4810 }
4811 print "</td>" .
4812 "</tr>\n";
4813
4814 foreach my $par (@$parents) {
4815 print "<tr>" .
4816 "<td>parent</td>" .
4817 "<td class=\"sha1\">" .
4818 $cgi->a({-href => href(action=>"commit", hash=>$par),
4819 class => "list"}, $par) .
4820 "</td>" .
4821 "<td class=\"link\">" .
4822 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4823 " | " .
4824 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4825 "</td>" .
4826 "</tr>\n";
4827 }
4828 print "</table>".
4829 "</div>\n";
4830
4831 print "<div class=\"page_body\">\n";
4832 git_print_log($co{'comment'});
4833 print "</div>\n";
4834
4835 git_difftree_body(\@difftree, $hash, @$parents);
4836
4837 git_footer_html();
4838}
4839
4840sub git_object {
4841 # object is defined by:
4842 # - hash or hash_base alone
4843 # - hash_base and file_name
4844 my $type;
4845
4846 # - hash or hash_base alone
4847 if ($hash || ($hash_base && !defined $file_name)) {
4848 my $object_id = $hash || $hash_base;
4849
4850 my $git_command = git_cmd_str();
4851 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4852 or die_error('404 Not Found', "Object does not exist");
4853 $type = <$fd>;
4854 chomp $type;
4855 close $fd
4856 or die_error('404 Not Found', "Object does not exist");
4857
4858 # - hash_base and file_name
4859 } elsif ($hash_base && defined $file_name) {
4860 $file_name =~ s,/+$,,;
4861
4862 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4863 or die_error('404 Not Found', "Base object does not exist");
4864
4865 # here errors should not hapen
4866 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4867 or die_error(undef, "Open git-ls-tree failed");
4868 my $line = <$fd>;
4869 close $fd;
4870
4871 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4872 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4873 die_error('404 Not Found', "File or directory for given base does not exist");
4874 }
4875 $type = $2;
4876 $hash = $3;
4877 } else {
4878 die_error('404 Not Found', "Not enough information to find object");
4879 }
4880
4881 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4882 hash=>$hash, hash_base=>$hash_base,
4883 file_name=>$file_name),
4884 -status => '302 Found');
4885}
4886
4887sub git_blobdiff {
4888 my $format = shift || 'html';
4889
4890 my $fd;
4891 my @difftree;
4892 my %diffinfo;
4893 my $expires;
4894
4895 # preparing $fd and %diffinfo for git_patchset_body
4896 # new style URI
4897 if (defined $hash_base && defined $hash_parent_base) {
4898 if (defined $file_name) {
4899 # read raw output
4900 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4901 $hash_parent_base, $hash_base,
4902 "--", (defined $file_parent ? $file_parent : ()), $file_name
4903 or die_error(undef, "Open git-diff-tree failed");
4904 @difftree = map { chomp; $_ } <$fd>;
4905 close $fd
4906 or die_error(undef, "Reading git-diff-tree failed");
4907 @difftree
4908 or die_error('404 Not Found', "Blob diff not found");
4909
4910 } elsif (defined $hash &&
4911 $hash =~ /[0-9a-fA-F]{40}/) {
4912 # try to find filename from $hash
4913
4914 # read filtered raw output
4915 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4916 $hash_parent_base, $hash_base, "--"
4917 or die_error(undef, "Open git-diff-tree failed");
4918 @difftree =
4919 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4920 # $hash == to_id
4921 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4922 map { chomp; $_ } <$fd>;
4923 close $fd
4924 or die_error(undef, "Reading git-diff-tree failed");
4925 @difftree
4926 or die_error('404 Not Found', "Blob diff not found");
4927
4928 } else {
4929 die_error('404 Not Found', "Missing one of the blob diff parameters");
4930 }
4931
4932 if (@difftree > 1) {
4933 die_error('404 Not Found', "Ambiguous blob diff specification");
4934 }
4935
4936 %diffinfo = parse_difftree_raw_line($difftree[0]);
4937 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4938 $file_name ||= $diffinfo{'to_file'};
4939
4940 $hash_parent ||= $diffinfo{'from_id'};
4941 $hash ||= $diffinfo{'to_id'};
4942
4943 # non-textual hash id's can be cached
4944 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4945 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4946 $expires = '+1d';
4947 }
4948
4949 # open patch output
4950 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4951 '-p', ($format eq 'html' ? "--full-index" : ()),
4952 $hash_parent_base, $hash_base,
4953 "--", (defined $file_parent ? $file_parent : ()), $file_name
4954 or die_error(undef, "Open git-diff-tree failed");
4955 }
4956
4957 # old/legacy style URI
4958 if (!%diffinfo && # if new style URI failed
4959 defined $hash && defined $hash_parent) {
4960 # fake git-diff-tree raw output
4961 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4962 $diffinfo{'from_id'} = $hash_parent;
4963 $diffinfo{'to_id'} = $hash;
4964 if (defined $file_name) {
4965 if (defined $file_parent) {
4966 $diffinfo{'status'} = '2';
4967 $diffinfo{'from_file'} = $file_parent;
4968 $diffinfo{'to_file'} = $file_name;
4969 } else { # assume not renamed
4970 $diffinfo{'status'} = '1';
4971 $diffinfo{'from_file'} = $file_name;
4972 $diffinfo{'to_file'} = $file_name;
4973 }
4974 } else { # no filename given
4975 $diffinfo{'status'} = '2';
4976 $diffinfo{'from_file'} = $hash_parent;
4977 $diffinfo{'to_file'} = $hash;
4978 }
4979
4980 # non-textual hash id's can be cached
4981 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4982 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4983 $expires = '+1d';
4984 }
4985
4986 # open patch output
4987 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4988 '-p', ($format eq 'html' ? "--full-index" : ()),
4989 $hash_parent, $hash, "--"
4990 or die_error(undef, "Open git-diff failed");
4991 } else {
4992 die_error('404 Not Found', "Missing one of the blob diff parameters")
4993 unless %diffinfo;
4994 }
4995
4996 # header
4997 if ($format eq 'html') {
4998 my $formats_nav =
4999 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5000 "raw");
5001 git_header_html(undef, $expires);
5002 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5003 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5004 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5005 } else {
5006 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5007 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5008 }
5009 if (defined $file_name) {
5010 git_print_page_path($file_name, "blob", $hash_base);
5011 } else {
5012 print "<div class=\"page_path\"></div>\n";
5013 }
5014
5015 } elsif ($format eq 'plain') {
5016 print $cgi->header(
5017 -type => 'text/plain',
5018 -charset => 'utf-8',
5019 -expires => $expires,
5020 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5021
5022 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5023
5024 } else {
5025 die_error(undef, "Unknown blobdiff format");
5026 }
5027
5028 # patch
5029 if ($format eq 'html') {
5030 print "<div class=\"page_body\">\n";
5031
5032 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5033 close $fd;
5034
5035 print "</div>\n"; # class="page_body"
5036 git_footer_html();
5037
5038 } else {
5039 while (my $line = <$fd>) {
5040 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5041 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5042
5043 print $line;
5044
5045 last if $line =~ m!^\+\+\+!;
5046 }
5047 local $/ = undef;
5048 print <$fd>;
5049 close $fd;
5050 }
5051}
5052
5053sub git_blobdiff_plain {
5054 git_blobdiff('plain');
5055}
5056
5057sub git_commitdiff {
5058 my $format = shift || 'html';
5059 $hash ||= $hash_base || "HEAD";
5060 my %co = parse_commit($hash);
5061 if (!%co) {
5062 die_error(undef, "Unknown commit object");
5063 }
5064
5065 # choose format for commitdiff for merge
5066 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5067 $hash_parent = '--cc';
5068 }
5069 # we need to prepare $formats_nav before almost any parameter munging
5070 my $formats_nav;
5071 if ($format eq 'html') {
5072 $formats_nav =
5073 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5074 "raw");
5075
5076 if (defined $hash_parent &&
5077 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5078 # commitdiff with two commits given
5079 my $hash_parent_short = $hash_parent;
5080 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5081 $hash_parent_short = substr($hash_parent, 0, 7);
5082 }
5083 $formats_nav .=
5084 ' (from';
5085 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5086 if ($co{'parents'}[$i] eq $hash_parent) {
5087 $formats_nav .= ' parent ' . ($i+1);
5088 last;
5089 }
5090 }
5091 $formats_nav .= ': ' .
5092 $cgi->a({-href => href(action=>"commitdiff",
5093 hash=>$hash_parent)},
5094 esc_html($hash_parent_short)) .
5095 ')';
5096 } elsif (!$co{'parent'}) {
5097 # --root commitdiff
5098 $formats_nav .= ' (initial)';
5099 } elsif (scalar @{$co{'parents'}} == 1) {
5100 # single parent commit
5101 $formats_nav .=
5102 ' (parent: ' .
5103 $cgi->a({-href => href(action=>"commitdiff",
5104 hash=>$co{'parent'})},
5105 esc_html(substr($co{'parent'}, 0, 7))) .
5106 ')';
5107 } else {
5108 # merge commit
5109 if ($hash_parent eq '--cc') {
5110 $formats_nav .= ' | ' .
5111 $cgi->a({-href => href(action=>"commitdiff",
5112 hash=>$hash, hash_parent=>'-c')},
5113 'combined');
5114 } else { # $hash_parent eq '-c'
5115 $formats_nav .= ' | ' .
5116 $cgi->a({-href => href(action=>"commitdiff",
5117 hash=>$hash, hash_parent=>'--cc')},
5118 'compact');
5119 }
5120 $formats_nav .=
5121 ' (merge: ' .
5122 join(' ', map {
5123 $cgi->a({-href => href(action=>"commitdiff",
5124 hash=>$_)},
5125 esc_html(substr($_, 0, 7)));
5126 } @{$co{'parents'}} ) .
5127 ')';
5128 }
5129 }
5130
5131 my $hash_parent_param = $hash_parent;
5132 if (!defined $hash_parent_param) {
5133 # --cc for multiple parents, --root for parentless
5134 $hash_parent_param =
5135 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5136 }
5137
5138 # read commitdiff
5139 my $fd;
5140 my @difftree;
5141 if ($format eq 'html') {
5142 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5143 "--no-commit-id", "--patch-with-raw", "--full-index",
5144 $hash_parent_param, $hash, "--"
5145 or die_error(undef, "Open git-diff-tree failed");
5146
5147 while (my $line = <$fd>) {
5148 chomp $line;
5149 # empty line ends raw part of diff-tree output
5150 last unless $line;
5151 push @difftree, scalar parse_difftree_raw_line($line);
5152 }
5153
5154 } elsif ($format eq 'plain') {
5155 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5156 '-p', $hash_parent_param, $hash, "--"
5157 or die_error(undef, "Open git-diff-tree failed");
5158
5159 } else {
5160 die_error(undef, "Unknown commitdiff format");
5161 }
5162
5163 # non-textual hash id's can be cached
5164 my $expires;
5165 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5166 $expires = "+1d";
5167 }
5168
5169 # write commit message
5170 if ($format eq 'html') {
5171 my $refs = git_get_references();
5172 my $ref = format_ref_marker($refs, $co{'id'});
5173
5174 git_header_html(undef, $expires);
5175 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5176 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5177 git_print_authorship(\%co);
5178 print "<div class=\"page_body\">\n";
5179 if (@{$co{'comment'}} > 1) {
5180 print "<div class=\"log\">\n";
5181 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5182 print "</div>\n"; # class="log"
5183 }
5184
5185 } elsif ($format eq 'plain') {
5186 my $refs = git_get_references("tags");
5187 my $tagname = git_get_rev_name_tags($hash);
5188 my $filename = basename($project) . "-$hash.patch";
5189
5190 print $cgi->header(
5191 -type => 'text/plain',
5192 -charset => 'utf-8',
5193 -expires => $expires,
5194 -content_disposition => 'inline; filename="' . "$filename" . '"');
5195 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5196 print "From: " . to_utf8($co{'author'}) . "\n";
5197 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5198 print "Subject: " . to_utf8($co{'title'}) . "\n";
5199
5200 print "X-Git-Tag: $tagname\n" if $tagname;
5201 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5202
5203 foreach my $line (@{$co{'comment'}}) {
5204 print to_utf8($line) . "\n";
5205 }
5206 print "---\n\n";
5207 }
5208
5209 # write patch
5210 if ($format eq 'html') {
5211 my $use_parents = !defined $hash_parent ||
5212 $hash_parent eq '-c' || $hash_parent eq '--cc';
5213 git_difftree_body(\@difftree, $hash,
5214 $use_parents ? @{$co{'parents'}} : $hash_parent);
5215 print "<br/>\n";
5216
5217 git_patchset_body($fd, \@difftree, $hash,
5218 $use_parents ? @{$co{'parents'}} : $hash_parent);
5219 close $fd;
5220 print "</div>\n"; # class="page_body"
5221 git_footer_html();
5222
5223 } elsif ($format eq 'plain') {
5224 local $/ = undef;
5225 print <$fd>;
5226 close $fd
5227 or print "Reading git-diff-tree failed\n";
5228 }
5229}
5230
5231sub git_commitdiff_plain {
5232 git_commitdiff('plain');
5233}
5234
5235sub git_history {
5236 if (!defined $hash_base) {
5237 $hash_base = git_get_head_hash($project);
5238 }
5239 if (!defined $page) {
5240 $page = 0;
5241 }
5242 my $ftype;
5243 my %co = parse_commit($hash_base);
5244 if (!%co) {
5245 die_error(undef, "Unknown commit object");
5246 }
5247
5248 my $refs = git_get_references();
5249 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5250
5251 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5252 $file_name, "--full-history");
5253 if (!@commitlist) {
5254 die_error('404 Not Found', "No such file or directory on given branch");
5255 }
5256
5257 if (!defined $hash && defined $file_name) {
5258 # some commits could have deleted file in question,
5259 # and not have it in tree, but one of them has to have it
5260 for (my $i = 0; $i <= @commitlist; $i++) {
5261 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5262 last if defined $hash;
5263 }
5264 }
5265 if (defined $hash) {
5266 $ftype = git_get_type($hash);
5267 }
5268 if (!defined $ftype) {
5269 die_error(undef, "Unknown type of object");
5270 }
5271
5272 my $paging_nav = '';
5273 if ($page > 0) {
5274 $paging_nav .=
5275 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5276 file_name=>$file_name)},
5277 "first");
5278 $paging_nav .= " ⋅ " .
5279 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5280 -accesskey => "p", -title => "Alt-p"}, "prev");
5281 } else {
5282 $paging_nav .= "first";
5283 $paging_nav .= " ⋅ prev";
5284 }
5285 my $next_link = '';
5286 if ($#commitlist >= 100) {
5287 $next_link =
5288 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5289 -accesskey => "n", -title => "Alt-n"}, "next");
5290 $paging_nav .= " ⋅ $next_link";
5291 } else {
5292 $paging_nav .= " ⋅ next";
5293 }
5294
5295 git_header_html();
5296 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5297 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5298 git_print_page_path($file_name, $ftype, $hash_base);
5299
5300 git_history_body(\@commitlist, 0, 99,
5301 $refs, $hash_base, $ftype, $next_link);
5302
5303 git_footer_html();
5304}
5305
5306sub git_search {
5307 my ($have_search) = gitweb_check_feature('search');
5308 if (!$have_search) {
5309 die_error('403 Permission denied', "Permission denied");
5310 }
5311 if (!defined $searchtext) {
5312 die_error(undef, "Text field empty");
5313 }
5314 if (!defined $hash) {
5315 $hash = git_get_head_hash($project);
5316 }
5317 my %co = parse_commit($hash);
5318 if (!%co) {
5319 die_error(undef, "Unknown commit object");
5320 }
5321 if (!defined $page) {
5322 $page = 0;
5323 }
5324
5325 $searchtype ||= 'commit';
5326 if ($searchtype eq 'pickaxe') {
5327 # pickaxe may take all resources of your box and run for several minutes
5328 # with every query - so decide by yourself how public you make this feature
5329 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5330 if (!$have_pickaxe) {
5331 die_error('403 Permission denied', "Permission denied");
5332 }
5333 }
5334 if ($searchtype eq 'grep') {
5335 my ($have_grep) = gitweb_check_feature('grep');
5336 if (!$have_grep) {
5337 die_error('403 Permission denied', "Permission denied");
5338 }
5339 }
5340
5341 git_header_html();
5342
5343 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5344 my $greptype;
5345 if ($searchtype eq 'commit') {
5346 $greptype = "--grep=";
5347 } elsif ($searchtype eq 'author') {
5348 $greptype = "--author=";
5349 } elsif ($searchtype eq 'committer') {
5350 $greptype = "--committer=";
5351 }
5352 $greptype .= $searchtext;
5353 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5354 $greptype, '--regexp-ignore-case',
5355 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5356
5357 my $paging_nav = '';
5358 if ($page > 0) {
5359 $paging_nav .=
5360 $cgi->a({-href => href(action=>"search", hash=>$hash,
5361 searchtext=>$searchtext,
5362 searchtype=>$searchtype)},
5363 "first");
5364 $paging_nav .= " ⋅ " .
5365 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5366 -accesskey => "p", -title => "Alt-p"}, "prev");
5367 } else {
5368 $paging_nav .= "first";
5369 $paging_nav .= " ⋅ prev";
5370 }
5371 my $next_link = '';
5372 if ($#commitlist >= 100) {
5373 $next_link =
5374 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5375 -accesskey => "n", -title => "Alt-n"}, "next");
5376 $paging_nav .= " ⋅ $next_link";
5377 } else {
5378 $paging_nav .= " ⋅ next";
5379 }
5380
5381 if ($#commitlist >= 100) {
5382 }
5383
5384 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5385 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5386 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5387 }
5388
5389 if ($searchtype eq 'pickaxe') {
5390 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5391 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5392
5393 print "<table class=\"pickaxe search\">\n";
5394 my $alternate = 1;
5395 $/ = "\n";
5396 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5397 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5398 ($search_use_regexp ? '--pickaxe-regex' : ());
5399 undef %co;
5400 my @files;
5401 while (my $line = <$fd>) {
5402 chomp $line;
5403 next unless $line;
5404
5405 my %set = parse_difftree_raw_line($line);
5406 if (defined $set{'commit'}) {
5407 # finish previous commit
5408 if (%co) {
5409 print "</td>\n" .
5410 "<td class=\"link\">" .
5411 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5412 " | " .
5413 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5414 print "</td>\n" .
5415 "</tr>\n";
5416 }
5417
5418 if ($alternate) {
5419 print "<tr class=\"dark\">\n";
5420 } else {
5421 print "<tr class=\"light\">\n";
5422 }
5423 $alternate ^= 1;
5424 %co = parse_commit($set{'commit'});
5425 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5426 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5427 "<td><i>$author</i></td>\n" .
5428 "<td>" .
5429 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5430 -class => "list subject"},
5431 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5432 } elsif (defined $set{'to_id'}) {
5433 next if ($set{'to_id'} =~ m/^0{40}$/);
5434
5435 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5436 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5437 -class => "list"},
5438 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5439 "<br/>\n";
5440 }
5441 }
5442 close $fd;
5443
5444 # finish last commit (warning: repetition!)
5445 if (%co) {
5446 print "</td>\n" .
5447 "<td class=\"link\">" .
5448 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5449 " | " .
5450 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5451 print "</td>\n" .
5452 "</tr>\n";
5453 }
5454
5455 print "</table>\n";
5456 }
5457
5458 if ($searchtype eq 'grep') {
5459 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5460 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5461
5462 print "<table class=\"grep_search\">\n";
5463 my $alternate = 1;
5464 my $matches = 0;
5465 $/ = "\n";
5466 open my $fd, "-|", git_cmd(), 'grep', '-n',
5467 $search_use_regexp ? ('-E', '-i') : '-F',
5468 $searchtext, $co{'tree'};
5469 my $lastfile = '';
5470 while (my $line = <$fd>) {
5471 chomp $line;
5472 my ($file, $lno, $ltext, $binary);
5473 last if ($matches++ > 1000);
5474 if ($line =~ /^Binary file (.+) matches$/) {
5475 $file = $1;
5476 $binary = 1;
5477 } else {
5478 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5479 }
5480 if ($file ne $lastfile) {
5481 $lastfile and print "</td></tr>\n";
5482 if ($alternate++) {
5483 print "<tr class=\"dark\">\n";
5484 } else {
5485 print "<tr class=\"light\">\n";
5486 }
5487 print "<td class=\"list\">".
5488 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5489 file_name=>"$file"),
5490 -class => "list"}, esc_path($file));
5491 print "</td><td>\n";
5492 $lastfile = $file;
5493 }
5494 if ($binary) {
5495 print "<div class=\"binary\">Binary file</div>\n";
5496 } else {
5497 $ltext = untabify($ltext);
5498 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5499 $ltext = esc_html($1, -nbsp=>1);
5500 $ltext .= '<span class="match">';
5501 $ltext .= esc_html($2, -nbsp=>1);
5502 $ltext .= '</span>';
5503 $ltext .= esc_html($3, -nbsp=>1);
5504 } else {
5505 $ltext = esc_html($ltext, -nbsp=>1);
5506 }
5507 print "<div class=\"pre\">" .
5508 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5509 file_name=>"$file").'#l'.$lno,
5510 -class => "linenr"}, sprintf('%4i', $lno))
5511 . ' ' . $ltext . "</div>\n";
5512 }
5513 }
5514 if ($lastfile) {
5515 print "</td></tr>\n";
5516 if ($matches > 1000) {
5517 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5518 }
5519 } else {
5520 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5521 }
5522 close $fd;
5523
5524 print "</table>\n";
5525 }
5526 git_footer_html();
5527}
5528
5529sub git_search_help {
5530 git_header_html();
5531 git_print_page_nav('','', $hash,$hash,$hash);
5532 print <<EOT;
5533<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5534regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5535the pattern entered is recognized as the POSIX extended
5536<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5537insensitive).</p>
5538<dl>
5539<dt><b>commit</b></dt>
5540<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5541EOT
5542 my ($have_grep) = gitweb_check_feature('grep');
5543 if ($have_grep) {
5544 print <<EOT;
5545<dt><b>grep</b></dt>
5546<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5547 a different one) are searched for the given pattern. On large trees, this search can take
5548a while and put some strain on the server, so please use it with some consideration. Note that
5549due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5550case-sensitive.</dd>
5551EOT
5552 }
5553 print <<EOT;
5554<dt><b>author</b></dt>
5555<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5556<dt><b>committer</b></dt>
5557<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5558EOT
5559 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5560 if ($have_pickaxe) {
5561 print <<EOT;
5562<dt><b>pickaxe</b></dt>
5563<dd>All commits that caused the string to appear or disappear from any file (changes that
5564added, removed or "modified" the string) will be listed. This search can take a while and
5565takes a lot of strain on the server, so please use it wisely. Note that since you may be
5566interested even in changes just changing the case as well, this search is case sensitive.</dd>
5567EOT
5568 }
5569 print "</dl>\n";
5570 git_footer_html();
5571}
5572
5573sub git_shortlog {
5574 my $head = git_get_head_hash($project);
5575 if (!defined $hash) {
5576 $hash = $head;
5577 }
5578 if (!defined $page) {
5579 $page = 0;
5580 }
5581 my $refs = git_get_references();
5582
5583 my @commitlist = parse_commits($hash, 101, (100 * $page));
5584
5585 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5586 my $next_link = '';
5587 if ($#commitlist >= 100) {
5588 $next_link =
5589 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5590 -accesskey => "n", -title => "Alt-n"}, "next");
5591 }
5592
5593 git_header_html();
5594 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5595 git_print_header_div('summary', $project);
5596
5597 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5598
5599 git_footer_html();
5600}
5601
5602## ......................................................................
5603## feeds (RSS, Atom; OPML)
5604
5605sub git_feed {
5606 my $format = shift || 'atom';
5607 my ($have_blame) = gitweb_check_feature('blame');
5608
5609 # Atom: http://www.atomenabled.org/developers/syndication/
5610 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5611 if ($format ne 'rss' && $format ne 'atom') {
5612 die_error(undef, "Unknown web feed format");
5613 }
5614
5615 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5616 my $head = $hash || 'HEAD';
5617 my @commitlist = parse_commits($head, 150, 0, $file_name);
5618
5619 my %latest_commit;
5620 my %latest_date;
5621 my $content_type = "application/$format+xml";
5622 if (defined $cgi->http('HTTP_ACCEPT') &&
5623 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5624 # browser (feed reader) prefers text/xml
5625 $content_type = 'text/xml';
5626 }
5627 if (defined($commitlist[0])) {
5628 %latest_commit = %{$commitlist[0]};
5629 %latest_date = parse_date($latest_commit{'author_epoch'});
5630 print $cgi->header(
5631 -type => $content_type,
5632 -charset => 'utf-8',
5633 -last_modified => $latest_date{'rfc2822'});
5634 } else {
5635 print $cgi->header(
5636 -type => $content_type,
5637 -charset => 'utf-8');
5638 }
5639
5640 # Optimization: skip generating the body if client asks only
5641 # for Last-Modified date.
5642 return if ($cgi->request_method() eq 'HEAD');
5643
5644 # header variables
5645 my $title = "$site_name - $project/$action";
5646 my $feed_type = 'log';
5647 if (defined $hash) {
5648 $title .= " - '$hash'";
5649 $feed_type = 'branch log';
5650 if (defined $file_name) {
5651 $title .= " :: $file_name";
5652 $feed_type = 'history';
5653 }
5654 } elsif (defined $file_name) {
5655 $title .= " - $file_name";
5656 $feed_type = 'history';
5657 }
5658 $title .= " $feed_type";
5659 my $descr = git_get_project_description($project);
5660 if (defined $descr) {
5661 $descr = esc_html($descr);
5662 } else {
5663 $descr = "$project " .
5664 ($format eq 'rss' ? 'RSS' : 'Atom') .
5665 " feed";
5666 }
5667 my $owner = git_get_project_owner($project);
5668 $owner = esc_html($owner);
5669
5670 #header
5671 my $alt_url;
5672 if (defined $file_name) {
5673 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5674 } elsif (defined $hash) {
5675 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5676 } else {
5677 $alt_url = href(-full=>1, action=>"summary");
5678 }
5679 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5680 if ($format eq 'rss') {
5681 print <<XML;
5682<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5683<channel>
5684XML
5685 print "<title>$title</title>\n" .
5686 "<link>$alt_url</link>\n" .
5687 "<description>$descr</description>\n" .
5688 "<language>en</language>\n";
5689 } elsif ($format eq 'atom') {
5690 print <<XML;
5691<feed xmlns="http://www.w3.org/2005/Atom">
5692XML
5693 print "<title>$title</title>\n" .
5694 "<subtitle>$descr</subtitle>\n" .
5695 '<link rel="alternate" type="text/html" href="' .
5696 $alt_url . '" />' . "\n" .
5697 '<link rel="self" type="' . $content_type . '" href="' .
5698 $cgi->self_url() . '" />' . "\n" .
5699 "<id>" . href(-full=>1) . "</id>\n" .
5700 # use project owner for feed author
5701 "<author><name>$owner</name></author>\n";
5702 if (defined $favicon) {
5703 print "<icon>" . esc_url($favicon) . "</icon>\n";
5704 }
5705 if (defined $logo_url) {
5706 # not twice as wide as tall: 72 x 27 pixels
5707 print "<logo>" . esc_url($logo) . "</logo>\n";
5708 }
5709 if (! %latest_date) {
5710 # dummy date to keep the feed valid until commits trickle in:
5711 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5712 } else {
5713 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5714 }
5715 }
5716
5717 # contents
5718 for (my $i = 0; $i <= $#commitlist; $i++) {
5719 my %co = %{$commitlist[$i]};
5720 my $commit = $co{'id'};
5721 # we read 150, we always show 30 and the ones more recent than 48 hours
5722 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5723 last;
5724 }
5725 my %cd = parse_date($co{'author_epoch'});
5726
5727 # get list of changed files
5728 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5729 $co{'parent'} || "--root",
5730 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5731 or next;
5732 my @difftree = map { chomp; $_ } <$fd>;
5733 close $fd
5734 or next;
5735
5736 # print element (entry, item)
5737 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5738 if ($format eq 'rss') {
5739 print "<item>\n" .
5740 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5741 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5742 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5743 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5744 "<link>$co_url</link>\n" .
5745 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5746 "<content:encoded>" .
5747 "<![CDATA[\n";
5748 } elsif ($format eq 'atom') {
5749 print "<entry>\n" .
5750 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5751 "<updated>$cd{'iso-8601'}</updated>\n" .
5752 "<author>\n" .
5753 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5754 if ($co{'author_email'}) {
5755 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5756 }
5757 print "</author>\n" .
5758 # use committer for contributor
5759 "<contributor>\n" .
5760 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5761 if ($co{'committer_email'}) {
5762 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5763 }
5764 print "</contributor>\n" .
5765 "<published>$cd{'iso-8601'}</published>\n" .
5766 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5767 "<id>$co_url</id>\n" .
5768 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5769 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5770 }
5771 my $comment = $co{'comment'};
5772 print "<pre>\n";
5773 foreach my $line (@$comment) {
5774 $line = esc_html($line);
5775 print "$line\n";
5776 }
5777 print "</pre><ul>\n";
5778 foreach my $difftree_line (@difftree) {
5779 my %difftree = parse_difftree_raw_line($difftree_line);
5780 next if !$difftree{'from_id'};
5781
5782 my $file = $difftree{'file'} || $difftree{'to_file'};
5783
5784 print "<li>" .
5785 "[" .
5786 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5787 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5788 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5789 file_name=>$file, file_parent=>$difftree{'from_file'}),
5790 -title => "diff"}, 'D');
5791 if ($have_blame) {
5792 print $cgi->a({-href => href(-full=>1, action=>"blame",
5793 file_name=>$file, hash_base=>$commit),
5794 -title => "blame"}, 'B');
5795 }
5796 # if this is not a feed of a file history
5797 if (!defined $file_name || $file_name ne $file) {
5798 print $cgi->a({-href => href(-full=>1, action=>"history",
5799 file_name=>$file, hash=>$commit),
5800 -title => "history"}, 'H');
5801 }
5802 $file = esc_path($file);
5803 print "] ".
5804 "$file</li>\n";
5805 }
5806 if ($format eq 'rss') {
5807 print "</ul>]]>\n" .
5808 "</content:encoded>\n" .
5809 "</item>\n";
5810 } elsif ($format eq 'atom') {
5811 print "</ul>\n</div>\n" .
5812 "</content>\n" .
5813 "</entry>\n";
5814 }
5815 }
5816
5817 # end of feed
5818 if ($format eq 'rss') {
5819 print "</channel>\n</rss>\n";
5820 } elsif ($format eq 'atom') {
5821 print "</feed>\n";
5822 }
5823}
5824
5825sub git_rss {
5826 git_feed('rss');
5827}
5828
5829sub git_atom {
5830 git_feed('atom');
5831}
5832
5833sub git_opml {
5834 my @list = git_get_projects_list();
5835
5836 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5837 print <<XML;
5838<?xml version="1.0" encoding="utf-8"?>
5839<opml version="1.0">
5840<head>
5841 <title>$site_name OPML Export</title>
5842</head>
5843<body>
5844<outline text="git RSS feeds">
5845XML
5846
5847 foreach my $pr (@list) {
5848 my %proj = %$pr;
5849 my $head = git_get_head_hash($proj{'path'});
5850 if (!defined $head) {
5851 next;
5852 }
5853 $git_dir = "$projectroot/$proj{'path'}";
5854 my %co = parse_commit($head);
5855 if (!%co) {
5856 next;
5857 }
5858
5859 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5860 my $rss = "$my_url?p=$proj{'path'};a=rss";
5861 my $html = "$my_url?p=$proj{'path'};a=summary";
5862 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5863 }
5864 print <<XML;
5865</outline>
5866</body>
5867</opml>
5868XML
5869}