3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
12 use CGI
qw(:standard :escapeHTML -nosticky);
13 use CGI
::Util
qw(unescape);
14 use CGI
::Carp
qw(fatalsToBrowser);
18 use File
::Basename
qw(basename);
19 binmode STDOUT
, ':utf8';
22 CGI-
>compile() if $ENV{'MOD_PERL'};
26 our $version = "++GIT_VERSION++";
27 our $my_url = $cgi->url();
28 our $my_uri = $cgi->url(-absolute
=> 1);
30 # if we're called with PATH_INFO, we have to strip that
31 # from the URL to find our real URL
32 # we make $path_info global because it's also used later on
33 our $path_info = $ENV{"PATH_INFO"};
35 $my_url =~ s
,\Q
$path_info\E
$,,;
36 $my_uri =~ s
,\Q
$path_info\E
$,,;
39 # core git executable to use
40 # this can just be "git" if your webserver has a sensible PATH
41 our $GIT = "++GIT_BINDIR++/git";
43 # absolute fs-path which will be prepended to the project path
44 #our $projectroot = "/pub/scm";
45 our $projectroot = "++GITWEB_PROJECTROOT++";
47 # fs traversing limit for getting project list
48 # the number is relative to the projectroot
49 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
51 # target of the home link on top of all pages
52 our $home_link = $my_uri || "/";
54 # string of the home link on top of all pages
55 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
57 # name of your site or organization to appear in page titles
58 # replace this with something more descriptive for clearer bookmarks
59 our $site_name = "++GITWEB_SITENAME++"
60 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
62 # filename of html text to include at top of each page
63 our $site_header = "++GITWEB_SITE_HEADER++";
64 # html text to include at home page
65 our $home_text = "++GITWEB_HOMETEXT++";
66 # filename of html text to include at bottom of each page
67 our $site_footer = "++GITWEB_SITE_FOOTER++";
70 our @stylesheets = ("++GITWEB_CSS++");
71 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
72 our $stylesheet = undef;
74 # URI of GIT logo (72x27 size)
75 our $logo = "++GITWEB_LOGO++";
76 # URI of GIT favicon, assumed to be image/png type
77 our $favicon = "++GITWEB_FAVICON++";
79 # URI and label (title) of GIT logo link
80 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
81 #our $logo_label = "git documentation";
82 our $logo_url = "http://git.or.cz/";
83 our $logo_label = "git homepage";
85 # source of projects list
86 our $projects_list = "++GITWEB_LIST++";
88 # the width (in characters) of the projects list "Description" column
89 our $projects_list_description_width = 25;
91 # default order of projects list
92 # valid values are none, project, descr, owner, and age
93 our $default_projects_order = "project";
95 # show repository only if this file exists
96 # (only effective if this variable evaluates to true)
97 our $export_ok = "++GITWEB_EXPORT_OK++";
99 # show repository only if this subroutine returns true
100 # when given the path to the project, for example:
101 # sub { return -e "$_[0]/git-daemon-export-ok"; }
102 our $export_auth_hook = undef;
104 # only allow viewing of repositories also shown on the overview page
105 our $strict_export = "++GITWEB_STRICT_EXPORT++";
107 # list of git base URLs used for URL to where fetch project from,
108 # i.e. full URL is "$git_base_url/$project"
109 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
111 # default blob_plain mimetype and default charset for text/plain blob
112 our $default_blob_plain_mimetype = 'text/plain';
113 our $default_text_plain_charset = undef;
115 # file to use for guessing MIME types before trying /etc/mime.types
116 # (relative to the current git repository)
117 our $mimetypes_file = undef;
119 # assume this charset if line contains non-UTF-8 characters;
120 # it should be valid encoding (see Encoding::Supported(3pm) for list),
121 # for which encoding all byte sequences are valid, for example
122 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
123 # could be even 'utf-8' for the old behavior)
124 our $fallback_encoding = 'latin1';
126 # rename detection options for git-diff and git-diff-tree
127 # - default is '-M', with the cost proportional to
128 # (number of removed files) * (number of new files).
129 # - more costly is '-C' (which implies '-M'), with the cost proportional to
130 # (number of changed files + number of removed files) * (number of new files)
131 # - even more costly is '-C', '--find-copies-harder' with cost
132 # (number of files in the original tree) * (number of new files)
133 # - one might want to include '-B' option, e.g. '-B', '-M'
134 our @diff_opts = ('-M'); # taken from git_commit
136 # information about snapshot formats that gitweb is capable of serving
137 our %known_snapshot_formats = (
139 # 'display' => display name,
140 # 'type' => mime type,
141 # 'suffix' => filename suffix,
142 # 'format' => --format for git-archive,
143 # 'compressor' => [compressor command and arguments]
144 # (array reference, optional)}
147 'display' => 'tar.gz',
148 'type' => 'application/x-gzip',
149 'suffix' => '.tar.gz',
151 'compressor' => ['gzip']},
154 'display' => 'tar.bz2',
155 'type' => 'application/x-bzip2',
156 'suffix' => '.tar.bz2',
158 'compressor' => ['bzip2']},
162 'type' => 'application/x-zip',
167 # Aliases so we understand old gitweb.snapshot values in repository
169 our %known_snapshot_format_aliases = (
173 # backward compatibility: legacy gitweb config support
174 'x-gzip' => undef, 'gz' => undef,
175 'x-bzip2' => undef, 'bz2' => undef,
176 'x-zip' => undef, '' => undef,
179 # You define site-wide feature defaults here; override them with
180 # $GITWEB_CONFIG as necessary.
183 # 'sub' => feature-sub (subroutine),
184 # 'override' => allow-override (boolean),
185 # 'default' => [ default options...] (array reference)}
187 # if feature is overridable (it means that allow-override has true value),
188 # then feature-sub will be called with default options as parameters;
189 # return value of feature-sub indicates if to enable specified feature
191 # if there is no 'sub' key (no feature-sub), then feature cannot be
194 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
196 # Enable the 'blame' blob view, showing the last commit that modified
197 # each line in the file. This can be very CPU-intensive.
199 # To enable system wide have in $GITWEB_CONFIG
200 # $feature{'blame'}{'default'} = [1];
201 # To have project specific config enable override in $GITWEB_CONFIG
202 # $feature{'blame'}{'override'} = 1;
203 # and in project config gitweb.blame = 0|1;
205 'sub' => \
&feature_blame
,
209 # Enable the 'snapshot' link, providing a compressed archive of any
210 # tree. This can potentially generate high traffic if you have large
213 # Value is a list of formats defined in %known_snapshot_formats that
215 # To disable system wide have in $GITWEB_CONFIG
216 # $feature{'snapshot'}{'default'} = [];
217 # To have project specific config enable override in $GITWEB_CONFIG
218 # $feature{'snapshot'}{'override'} = 1;
219 # and in project config, a comma-separated list of formats or "none"
220 # to disable. Example: gitweb.snapshot = tbz2,zip;
222 'sub' => \
&feature_snapshot
,
224 'default' => ['tgz']},
226 # Enable text search, which will list the commits which match author,
227 # committer or commit text to a given string. Enabled by default.
228 # Project specific override is not supported.
233 # Enable grep search, which will list the files in currently selected
234 # tree containing the given string. Enabled by default. This can be
235 # potentially CPU-intensive, of course.
237 # To enable system wide have in $GITWEB_CONFIG
238 # $feature{'grep'}{'default'} = [1];
239 # To have project specific config enable override in $GITWEB_CONFIG
240 # $feature{'grep'}{'override'} = 1;
241 # and in project config gitweb.grep = 0|1;
246 # Enable the pickaxe search, which will list the commits that modified
247 # a given string in a file. This can be practical and quite faster
248 # alternative to 'blame', but still potentially CPU-intensive.
250 # To enable system wide have in $GITWEB_CONFIG
251 # $feature{'pickaxe'}{'default'} = [1];
252 # To have project specific config enable override in $GITWEB_CONFIG
253 # $feature{'pickaxe'}{'override'} = 1;
254 # and in project config gitweb.pickaxe = 0|1;
256 'sub' => \
&feature_pickaxe
,
260 # Make gitweb use an alternative format of the URLs which can be
261 # more readable and natural-looking: project name is embedded
262 # directly in the path and the query string contains other
263 # auxiliary information. All gitweb installations recognize
264 # URL in either format; this configures in which formats gitweb
267 # To enable system wide have in $GITWEB_CONFIG
268 # $feature{'pathinfo'}{'default'} = [1];
269 # Project specific override is not supported.
271 # Note that you will need to change the default location of CSS,
272 # favicon, logo and possibly other files to an absolute URL. Also,
273 # if gitweb.cgi serves as your indexfile, you will need to force
274 # $my_uri to contain the script name in your $GITWEB_CONFIG.
279 # Make gitweb consider projects in project root subdirectories
280 # to be forks of existing projects. Given project $projname.git,
281 # projects matching $projname/*.git will not be shown in the main
282 # projects list, instead a '+' mark will be added to $projname
283 # there and a 'forks' view will be enabled for the project, listing
284 # all the forks. If project list is taken from a file, forks have
285 # to be listed after the main project.
287 # To enable system wide have in $GITWEB_CONFIG
288 # $feature{'forks'}{'default'} = [1];
289 # Project specific override is not supported.
294 # Insert custom links to the action bar of all project pages.
295 # This enables you mainly to link to third-party scripts integrating
296 # into gitweb; e.g. git-browser for graphical history representation
297 # or custom web-based repository administration interface.
299 # The 'default' value consists of a list of triplets in the form
300 # (label, link, position) where position is the label after which
301 # to insert the link and link is a format string where %n expands
302 # to the project name, %f to the project path within the filesystem,
303 # %h to the current hash (h gitweb parameter) and %b to the current
304 # hash base (hb gitweb parameter); %% expands to %.
306 # To enable system wide have in $GITWEB_CONFIG e.g.
307 # $feature{'actions'}{'default'} = [('graphiclog',
308 # '/git-browser/by-commit.html?r=%n', 'summary')];
309 # Project specific override is not supported.
314 # Allow gitweb scan project content tags described in ctags/
315 # of project repository, and display the popular Web 2.0-ish
316 # "tag cloud" near the project list. Note that this is something
317 # COMPLETELY different from the normal Git tags.
319 # gitweb by itself can show existing tags, but it does not handle
320 # tagging itself; you need an external application for that.
321 # For an example script, check Girocco's cgi/tagproj.cgi.
322 # You may want to install the HTML::TagCloud Perl module to get
323 # a pretty tag cloud instead of just a list of tags.
325 # To enable system wide have in $GITWEB_CONFIG
326 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
327 # Project specific override is not supported.
333 sub gitweb_check_feature
{
335 return unless exists $feature{$name};
336 my ($sub, $override, @defaults) = (
337 $feature{$name}{'sub'},
338 $feature{$name}{'override'},
339 @{$feature{$name}{'default'}});
340 if (!$override) { return @defaults; }
342 warn "feature $name is not overrideable";
345 return $sub->(@defaults);
349 my ($val) = git_get_project_config
('blame', '--bool');
351 if ($val eq 'true') {
353 } elsif ($val eq 'false') {
360 sub feature_snapshot
{
363 my ($val) = git_get_project_config
('snapshot');
366 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
373 my ($val) = git_get_project_config
('grep', '--bool');
375 if ($val eq 'true') {
377 } elsif ($val eq 'false') {
384 sub feature_pickaxe
{
385 my ($val) = git_get_project_config
('pickaxe', '--bool');
387 if ($val eq 'true') {
389 } elsif ($val eq 'false') {
396 # checking HEAD file with -e is fragile if the repository was
397 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
399 sub check_head_link
{
401 my $headfile = "$dir/HEAD";
402 return ((-e
$headfile) ||
403 (-l
$headfile && readlink($headfile) =~ /^refs\/heads\
//));
406 sub check_export_ok
{
408 return (check_head_link
($dir) &&
409 (!$export_ok || -e
"$dir/$export_ok") &&
410 (!$export_auth_hook || $export_auth_hook->($dir)));
413 # process alternate names for backward compatibility
414 # filter out unsupported (unknown) snapshot formats
415 sub filter_snapshot_fmts
{
419 exists $known_snapshot_format_aliases{$_} ?
420 $known_snapshot_format_aliases{$_} : $_} @fmts;
421 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
425 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
426 if (-e
$GITWEB_CONFIG) {
429 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
430 do $GITWEB_CONFIG_SYSTEM if -e
$GITWEB_CONFIG_SYSTEM;
433 # version of the core git binary
434 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
436 $projects_list ||= $projectroot;
438 # ======================================================================
439 # input validation and dispatch
441 # input parameters can be collected from a variety of sources (presently, CGI
442 # and PATH_INFO), so we define an %input_params hash that collects them all
443 # together during validation: this allows subsequent uses (e.g. href()) to be
444 # agnostic of the parameter origin
446 our %input_params = ();
448 # input parameters are stored with the long parameter name as key. This will
449 # also be used in the href subroutine to convert parameters to their CGI
450 # equivalent, and since the href() usage is the most frequent one, we store
451 # the name -> CGI key mapping here, instead of the reverse.
453 # XXX: Warning: If you touch this, check the search form for updating,
456 our @cgi_param_mapping = (
464 hash_parent_base
=> "hpb",
469 snapshot_format
=> "sf",
470 extra_options
=> "opt",
471 search_use_regexp
=> "sr",
473 our %cgi_param_mapping = @cgi_param_mapping;
475 # we will also need to know the possible actions, for validation
477 "blame" => \
&git_blame
,
478 "blobdiff" => \
&git_blobdiff
,
479 "blobdiff_plain" => \
&git_blobdiff_plain
,
480 "blob" => \
&git_blob
,
481 "blob_plain" => \
&git_blob_plain
,
482 "commitdiff" => \
&git_commitdiff
,
483 "commitdiff_plain" => \
&git_commitdiff_plain
,
484 "commit" => \
&git_commit
,
485 "forks" => \
&git_forks
,
486 "heads" => \
&git_heads
,
487 "history" => \
&git_history
,
490 "atom" => \
&git_atom
,
491 "search" => \
&git_search
,
492 "search_help" => \
&git_search_help
,
493 "shortlog" => \
&git_shortlog
,
494 "summary" => \
&git_summary
,
496 "tags" => \
&git_tags
,
497 "tree" => \
&git_tree
,
498 "snapshot" => \
&git_snapshot
,
499 "object" => \
&git_object
,
500 # those below don't need $project
501 "opml" => \
&git_opml
,
502 "project_list" => \
&git_project_list
,
503 "project_index" => \
&git_project_index
,
506 # finally, we have the hash of allowed extra_options for the commands that
508 our %allowed_options = (
509 "--no-merges" => [ qw(rss atom log shortlog history) ],
512 # fill %input_params with the CGI parameters. All values except for 'opt'
513 # should be single values, but opt can be an array. We should probably
514 # build an array of parameters that can be multi-valued, but since for the time
515 # being it's only this one, we just single it out
516 while (my ($name, $symbol) = each %cgi_param_mapping) {
517 if ($symbol eq 'opt') {
518 $input_params{$name} = [ $cgi->param($symbol) ];
520 $input_params{$name} = $cgi->param($symbol);
524 # now read PATH_INFO and update the parameter list for missing parameters
525 sub evaluate_path_info
{
526 return if defined $input_params{'project'};
527 return if !$path_info;
528 $path_info =~ s
,^/+,,;
529 return if !$path_info;
531 # find which part of PATH_INFO is project
532 my $project = $path_info;
534 while ($project && !check_head_link
("$projectroot/$project")) {
535 $project =~ s
,/*[^/]*$,,;
537 return unless $project;
538 $input_params{'project'} = $project;
540 # do not change any parameters if an action is given using the query string
541 return if $input_params{'action'};
542 $path_info =~ s
,^\Q
$project\E
/*,,;
544 # next, check if we have an action
545 my $action = $path_info;
547 if (exists $actions{$action}) {
548 $path_info =~ s
,^$action/*,,;
549 $input_params{'action'} = $action;
552 # list of actions that want hash_base instead of hash, but can have no
553 # pathname (f) parameter
560 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
561 my ($parentrefname, $parentpathname, $refname, $pathname) =
562 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
564 # first, analyze the 'current' part
565 if (defined $pathname) {
566 # we got "branch:filename" or "branch:dir/"
567 # we could use git_get_type(branch:pathname), but:
568 # - it needs $git_dir
569 # - it does a git() call
570 # - the convention of terminating directories with a slash
571 # makes it superfluous
572 # - embedding the action in the PATH_INFO would make it even
574 $pathname =~ s
,^/+,,;
575 if (!$pathname || substr($pathname, -1) eq "/") {
576 $input_params{'action'} ||= "tree";
579 # the default action depends on whether we had parent info
581 if ($parentrefname) {
582 $input_params{'action'} ||= "blobdiff_plain";
584 $input_params{'action'} ||= "blob_plain";
587 $input_params{'hash_base'} ||= $refname;
588 $input_params{'file_name'} ||= $pathname;
589 } elsif (defined $refname) {
590 # we got "branch". In this case we have to choose if we have to
591 # set hash or hash_base.
593 # Most of the actions without a pathname only want hash to be
594 # set, except for the ones specified in @wants_base that want
595 # hash_base instead. It should also be noted that hand-crafted
596 # links having 'history' as an action and no pathname or hash
597 # set will fail, but that happens regardless of PATH_INFO.
598 $input_params{'action'} ||= "shortlog";
599 if (grep { $_ eq $input_params{'action'} } @wants_base) {
600 $input_params{'hash_base'} ||= $refname;
602 $input_params{'hash'} ||= $refname;
606 # next, handle the 'parent' part, if present
607 if (defined $parentrefname) {
608 # a missing pathspec defaults to the 'current' filename, allowing e.g.
609 # someproject/blobdiff/oldrev..newrev:/filename
610 if ($parentpathname) {
611 $parentpathname =~ s
,^/+,,;
612 $parentpathname =~ s
,/$,,;
613 $input_params{'file_parent'} ||= $parentpathname;
615 $input_params{'file_parent'} ||= $input_params{'file_name'};
617 # we assume that hash_parent_base is wanted if a path was specified,
618 # or if the action wants hash_base instead of hash
619 if (defined $input_params{'file_parent'} ||
620 grep { $_ eq $input_params{'action'} } @wants_base) {
621 $input_params{'hash_parent_base'} ||= $parentrefname;
623 $input_params{'hash_parent'} ||= $parentrefname;
627 # for the snapshot action, we allow URLs in the form
628 # $project/snapshot/$hash.ext
629 # where .ext determines the snapshot and gets removed from the
630 # passed $refname to provide the $hash.
632 # To be able to tell that $refname includes the format extension, we
633 # require the following two conditions to be satisfied:
634 # - the hash input parameter MUST have been set from the $refname part
635 # of the URL (i.e. they must be equal)
636 # - the snapshot format MUST NOT have been defined already (e.g. from
638 # It's also useless to try any matching unless $refname has a dot,
639 # so we check for that too
640 if (defined $input_params{'action'} &&
641 $input_params{'action'} eq 'snapshot' &&
642 defined $refname && index($refname, '.') != -1 &&
643 $refname eq $input_params{'hash'} &&
644 !defined $input_params{'snapshot_format'}) {
645 # We loop over the known snapshot formats, checking for
646 # extensions. Allowed extensions are both the defined suffix
647 # (which includes the initial dot already) and the snapshot
648 # format key itself, with a prepended dot
649 while (my ($fmt, %opt) = each %known_snapshot_formats) {
652 $hash =~ s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//;
653 next unless $sfx = $1;
654 # a valid suffix was found, so set the snapshot format
655 # and reset the hash parameter
656 $input_params{'snapshot_format'} = $fmt;
657 $input_params{'hash'} = $hash;
658 # we also set the format suffix to the one requested
659 # in the URL: this way a request for e.g. .tgz returns
660 # a .tgz instead of a .tar.gz
661 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
666 evaluate_path_info
();
668 our $action = $input_params{'action'};
669 if (defined $action) {
670 if (!validate_action
($action)) {
671 die_error
(400, "Invalid action parameter");
675 # parameters which are pathnames
676 our $project = $input_params{'project'};
677 if (defined $project) {
678 if (!validate_project
($project)) {
680 die_error
(404, "No such project");
684 our $file_name = $input_params{'file_name'};
685 if (defined $file_name) {
686 if (!validate_pathname
($file_name)) {
687 die_error
(400, "Invalid file parameter");
691 our $file_parent = $input_params{'file_parent'};
692 if (defined $file_parent) {
693 if (!validate_pathname
($file_parent)) {
694 die_error
(400, "Invalid file parent parameter");
698 # parameters which are refnames
699 our $hash = $input_params{'hash'};
701 if (!validate_refname
($hash)) {
702 die_error
(400, "Invalid hash parameter");
706 our $hash_parent = $input_params{'hash_parent'};
707 if (defined $hash_parent) {
708 if (!validate_refname
($hash_parent)) {
709 die_error
(400, "Invalid hash parent parameter");
713 our $hash_base = $input_params{'hash_base'};
714 if (defined $hash_base) {
715 if (!validate_refname
($hash_base)) {
716 die_error
(400, "Invalid hash base parameter");
720 our @extra_options = @{$input_params{'extra_options'}};
721 # @extra_options is always defined, since it can only be (currently) set from
722 # CGI, and $cgi->param() returns the empty array in array context if the param
724 foreach my $opt (@extra_options) {
725 if (not exists $allowed_options{$opt}) {
726 die_error
(400, "Invalid option parameter");
728 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
729 die_error
(400, "Invalid option parameter for this action");
733 our $hash_parent_base = $input_params{'hash_parent_base'};
734 if (defined $hash_parent_base) {
735 if (!validate_refname
($hash_parent_base)) {
736 die_error
(400, "Invalid hash parent base parameter");
741 our $page = $input_params{'page'};
743 if ($page =~ m/[^0-9]/) {
744 die_error
(400, "Invalid page parameter");
748 our $searchtype = $input_params{'searchtype'};
749 if (defined $searchtype) {
750 if ($searchtype =~ m/[^a-z]/) {
751 die_error
(400, "Invalid searchtype parameter");
755 our $search_use_regexp = $input_params{'search_use_regexp'};
757 our $searchtext = $input_params{'searchtext'};
759 if (defined $searchtext) {
760 if (length($searchtext) < 2) {
761 die_error
(403, "At least two characters are required for search parameter");
763 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
766 # path to the current git repository
768 $git_dir = "$projectroot/$project" if $project;
770 # list of supported snapshot formats
771 our @snapshot_fmts = gitweb_check_feature
('snapshot');
772 @snapshot_fmts = filter_snapshot_fmts
(@snapshot_fmts);
775 if (!defined $action) {
777 $action = git_get_type
($hash);
778 } elsif (defined $hash_base && defined $file_name) {
779 $action = git_get_type
("$hash_base:$file_name");
780 } elsif (defined $project) {
783 $action = 'project_list';
786 if (!defined($actions{$action})) {
787 die_error
(400, "Unknown action");
789 if ($action !~ m/^(opml|project_list|project_index)$/ &&
791 die_error
(400, "Project needed");
793 $actions{$action}->();
796 ## ======================================================================
801 # default is to use -absolute url() i.e. $my_uri
802 my $href = $params{-full
} ? $my_url : $my_uri;
804 $params{'project'} = $project unless exists $params{'project'};
806 if ($params{-replay
}) {
807 while (my ($name, $symbol) = each %cgi_param_mapping) {
808 if (!exists $params{$name}) {
809 $params{$name} = $input_params{$name};
814 my ($use_pathinfo) = gitweb_check_feature
('pathinfo');
816 # try to put as many parameters as possible in PATH_INFO:
819 # - hash_parent or hash_parent_base:/file_parent
820 # - hash or hash_base:/filename
821 # - the snapshot_format as an appropriate suffix
823 # When the script is the root DirectoryIndex for the domain,
824 # $href here would be something like http://gitweb.example.com/
825 # Thus, we strip any trailing / from $href, to spare us double
826 # slashes in the final URL
829 # Then add the project name, if present
830 $href .= "/".esc_url
($params{'project'}) if defined $params{'project'};
831 delete $params{'project'};
833 # since we destructively absorb parameters, we keep this
834 # boolean that remembers if we're handling a snapshot
835 my $is_snapshot = $params{'action'} eq 'snapshot';
837 # Summary just uses the project path URL, any other action is
839 if (defined $params{'action'}) {
840 $href .= "/".esc_url
($params{'action'}) unless $params{'action'} eq 'summary';
841 delete $params{'action'};
844 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
845 # stripping nonexistent or useless pieces
846 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
847 || $params{'hash_parent'} || $params{'hash'});
848 if (defined $params{'hash_base'}) {
849 if (defined $params{'hash_parent_base'}) {
850 $href .= esc_url
($params{'hash_parent_base'});
851 # skip the file_parent if it's the same as the file_name
852 delete $params{'file_parent'} if $params{'file_parent'} eq $params{'file_name'};
853 if (defined $params{'file_parent'} && $params{'file_parent'} !~ /\.\./) {
854 $href .= ":/".esc_url
($params{'file_parent'});
855 delete $params{'file_parent'};
858 delete $params{'hash_parent'};
859 delete $params{'hash_parent_base'};
860 } elsif (defined $params{'hash_parent'}) {
861 $href .= esc_url
($params{'hash_parent'}). "..";
862 delete $params{'hash_parent'};
865 $href .= esc_url
($params{'hash_base'});
866 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
867 $href .= ":/".esc_url
($params{'file_name'});
868 delete $params{'file_name'};
870 delete $params{'hash'};
871 delete $params{'hash_base'};
872 } elsif (defined $params{'hash'}) {
873 $href .= esc_url
($params{'hash'});
874 delete $params{'hash'};
877 # If the action was a snapshot, we can absorb the
878 # snapshot_format parameter too
880 my $fmt = $params{'snapshot_format'};
881 # snapshot_format should always be defined when href()
882 # is called, but just in case some code forgets, we
883 # fall back to the default
884 $fmt ||= $snapshot_fmts[0];
885 $href .= $known_snapshot_formats{$fmt}{'suffix'};
886 delete $params{'snapshot_format'};
890 # now encode the parameters explicitly
892 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
893 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
894 if (defined $params{$name}) {
895 if (ref($params{$name}) eq "ARRAY") {
896 foreach my $par (@{$params{$name}}) {
897 push @result, $symbol . "=" . esc_param
($par);
900 push @result, $symbol . "=" . esc_param
($params{$name});
904 $href .= "?" . join(';', @result) if scalar @result;
910 ## ======================================================================
911 ## validation, quoting/unquoting and escaping
913 sub validate_action
{
914 my $input = shift || return undef;
915 return undef unless exists $actions{$input};
919 sub validate_project
{
920 my $input = shift || return undef;
921 if (!validate_pathname
($input) ||
922 !(-d
"$projectroot/$input") ||
923 !check_export_ok
("$projectroot/$input") ||
924 ($strict_export && !project_in_list
($input))) {
931 sub validate_pathname
{
932 my $input = shift || return undef;
934 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
935 # at the beginning, at the end, and between slashes.
936 # also this catches doubled slashes
937 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
941 if ($input =~ m!\0!) {
947 sub validate_refname
{
948 my $input = shift || return undef;
950 # textual hashes are O.K.
951 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
954 # it must be correct pathname
955 $input = validate_pathname
($input)
957 # restrictions on ref name according to git-check-ref-format
958 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
964 # decode sequences of octets in utf8 into Perl's internal form,
965 # which is utf-8 with utf8 flag set if needed. gitweb writes out
966 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
969 if (utf8
::valid
($str)) {
973 return decode
($fallback_encoding, $str, Encode
::FB_DEFAULT
);
977 # quote unsafe chars, but keep the slash, even when it's not
978 # correct, but quoted slashes look too horrible in bookmarks
981 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf
("%%%02X", ord($1))/eg
;
987 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
990 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf
("%%%02X", ord($1))/eg
;
996 # replace invalid utf8 character with SUBSTITUTION sequence
1001 $str = to_utf8
($str);
1002 $str = $cgi->escapeHTML($str);
1003 if ($opts{'-nbsp'}) {
1004 $str =~ s/ / /g;
1006 $str =~ s
|([[:cntrl
:]])|(($1 ne "\t") ? quot_cec
($1) : $1)|eg
;
1010 # quote control characters and escape filename to HTML
1015 $str = to_utf8
($str);
1016 $str = $cgi->escapeHTML($str);
1017 if ($opts{'-nbsp'}) {
1018 $str =~ s/ / /g;
1020 $str =~ s
|([[:cntrl
:]])|quot_cec
($1)|eg
;
1024 # Make control characters "printable", using character escape codes (CEC)
1028 my %es = ( # character escape codes, aka escape sequences
1029 "\t" => '\t', # tab (HT)
1030 "\n" => '\n', # line feed (LF)
1031 "\r" => '\r', # carrige return (CR)
1032 "\f" => '\f', # form feed (FF)
1033 "\b" => '\b', # backspace (BS)
1034 "\a" => '\a', # alarm (bell) (BEL)
1035 "\e" => '\e', # escape (ESC)
1036 "\013" => '\v', # vertical tab (VT)
1037 "\000" => '\0', # nul character (NUL)
1039 my $chr = ( (exists $es{$cntrl})
1041 : sprintf('\%2x', ord($cntrl)) );
1042 if ($opts{-nohtml
}) {
1045 return "<span class=\"cntrl\">$chr</span>";
1049 # Alternatively use unicode control pictures codepoints,
1050 # Unicode "printable representation" (PR)
1055 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1056 if ($opts{-nohtml
}) {
1059 return "<span class=\"cntrl\">$chr</span>";
1063 # git may return quoted and escaped filenames
1069 my %es = ( # character escape codes, aka escape sequences
1070 't' => "\t", # tab (HT, TAB)
1071 'n' => "\n", # newline (NL)
1072 'r' => "\r", # return (CR)
1073 'f' => "\f", # form feed (FF)
1074 'b' => "\b", # backspace (BS)
1075 'a' => "\a", # alarm (bell) (BEL)
1076 'e' => "\e", # escape (ESC)
1077 'v' => "\013", # vertical tab (VT)
1080 if ($seq =~ m/^[0-7]{1,3}$/) {
1081 # octal char sequence
1082 return chr(oct($seq));
1083 } elsif (exists $es{$seq}) {
1084 # C escape sequence, aka character escape code
1087 # quoted ordinary character
1091 if ($str =~ m/^"(.*)"$/) {
1094 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1099 # escape tabs (convert tabs to spaces)
1103 while ((my $pos = index($line, "\t")) != -1) {
1104 if (my $count = (8 - ($pos % 8))) {
1105 my $spaces = ' ' x
$count;
1106 $line =~ s/\t/$spaces/;
1113 sub project_in_list
{
1114 my $project = shift;
1115 my @list = git_get_projects_list
();
1116 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1119 ## ----------------------------------------------------------------------
1120 ## HTML aware string manipulation
1122 # Try to chop given string on a word boundary between position
1123 # $len and $len+$add_len. If there is no word boundary there,
1124 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1125 # (marking chopped part) would be longer than given string.
1129 my $add_len = shift || 10;
1130 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1132 # Make sure perl knows it is utf8 encoded so we don't
1133 # cut in the middle of a utf8 multibyte char.
1134 $str = to_utf8
($str);
1136 # allow only $len chars, but don't cut a word if it would fit in $add_len
1137 # if it doesn't fit, cut it if it's still longer than the dots we would add
1138 # remove chopped character entities entirely
1140 # when chopping in the middle, distribute $len into left and right part
1141 # return early if chopping wouldn't make string shorter
1142 if ($where eq 'center') {
1143 return $str if ($len + 5 >= length($str)); # filler is length 5
1146 return $str if ($len + 4 >= length($str)); # filler is length 4
1149 # regexps: ending and beginning with word part up to $add_len
1150 my $endre = qr/.{$len}\w{0,$add_len}/;
1151 my $begre = qr/\w{0,$add_len}.{$len}/;
1153 if ($where eq 'left') {
1154 $str =~ m/^(.*?)($begre)$/;
1155 my ($lead, $body) = ($1, $2);
1156 if (length($lead) > 4) {
1157 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1160 return "$lead$body";
1162 } elsif ($where eq 'center') {
1163 $str =~ m/^($endre)(.*)$/;
1164 my ($left, $str) = ($1, $2);
1165 $str =~ m/^(.*?)($begre)$/;
1166 my ($mid, $right) = ($1, $2);
1167 if (length($mid) > 5) {
1168 $left =~ s/&[^;]*$//;
1169 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1172 return "$left$mid$right";
1175 $str =~ m/^($endre)(.*)$/;
1178 if (length($tail) > 4) {
1179 $body =~ s/&[^;]*$//;
1182 return "$body$tail";
1186 # takes the same arguments as chop_str, but also wraps a <span> around the
1187 # result with a title attribute if it does get chopped. Additionally, the
1188 # string is HTML-escaped.
1189 sub chop_and_escape_str
{
1192 my $chopped = chop_str
(@_);
1193 if ($chopped eq $str) {
1194 return esc_html
($chopped);
1196 $str =~ s/([[:cntrl:]])/?/g;
1197 return $cgi->span({-title
=>$str}, esc_html
($chopped));
1201 ## ----------------------------------------------------------------------
1202 ## functions returning short strings
1204 # CSS class for given age value (in seconds)
1208 if (!defined $age) {
1210 } elsif ($age < 60*60*2) {
1212 } elsif ($age < 60*60*24*2) {
1219 # convert age in seconds to "nn units ago" string
1224 if ($age > 60*60*24*365*2) {
1225 $age_str = (int $age/60/60/24/365);
1226 $age_str .= " years ago";
1227 } elsif ($age > 60*60*24*(365/12)*2) {
1228 $age_str = int $age/60/60/24/(365/12);
1229 $age_str .= " months ago";
1230 } elsif ($age > 60*60*24*7*2) {
1231 $age_str = int $age/60/60/24/7;
1232 $age_str .= " weeks ago";
1233 } elsif ($age > 60*60*24*2) {
1234 $age_str = int $age/60/60/24;
1235 $age_str .= " days ago";
1236 } elsif ($age > 60*60*2) {
1237 $age_str = int $age/60/60;
1238 $age_str .= " hours ago";
1239 } elsif ($age > 60*2) {
1240 $age_str = int $age/60;
1241 $age_str .= " min ago";
1242 } elsif ($age > 2) {
1243 $age_str = int $age;
1244 $age_str .= " sec ago";
1246 $age_str .= " right now";
1252 S_IFINVALID
=> 0030000,
1253 S_IFGITLINK
=> 0160000,
1256 # submodule/subproject, a commit object reference
1257 sub S_ISGITLINK
($) {
1260 return (($mode & S_IFMT
) == S_IFGITLINK
)
1263 # convert file mode in octal to symbolic file mode string
1265 my $mode = oct shift;
1267 if (S_ISGITLINK
($mode)) {
1268 return 'm---------';
1269 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1270 return 'drwxr-xr-x';
1271 } elsif (S_ISLNK
($mode)) {
1272 return 'lrwxrwxrwx';
1273 } elsif (S_ISREG
($mode)) {
1274 # git cares only about the executable bit
1275 if ($mode & S_IXUSR
) {
1276 return '-rwxr-xr-x';
1278 return '-rw-r--r--';
1281 return '----------';
1285 # convert file mode in octal to file type string
1289 if ($mode !~ m/^[0-7]+$/) {
1295 if (S_ISGITLINK
($mode)) {
1297 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1299 } elsif (S_ISLNK
($mode)) {
1301 } elsif (S_ISREG
($mode)) {
1308 # convert file mode in octal to file type description string
1309 sub file_type_long
{
1312 if ($mode !~ m/^[0-7]+$/) {
1318 if (S_ISGITLINK
($mode)) {
1320 } elsif (S_ISDIR
($mode & S_IFMT
)) {
1322 } elsif (S_ISLNK
($mode)) {
1324 } elsif (S_ISREG
($mode)) {
1325 if ($mode & S_IXUSR
) {
1326 return "executable";
1336 ## ----------------------------------------------------------------------
1337 ## functions returning short HTML fragments, or transforming HTML fragments
1338 ## which don't belong to other sections
1340 # format line of commit message.
1341 sub format_log_line_html
{
1344 $line = esc_html
($line, -nbsp
=>1);
1345 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1348 $cgi->a({-href
=> href
(action
=>"object", hash
=>$hash_text),
1349 -class => "text"}, $hash_text);
1350 $line =~ s/$hash_text/$link/;
1355 # format marker of refs pointing to given object
1357 # the destination action is chosen based on object type and current context:
1358 # - for annotated tags, we choose the tag view unless it's the current view
1359 # already, in which case we go to shortlog view
1360 # - for other refs, we keep the current view if we're in history, shortlog or
1361 # log view, and select shortlog otherwise
1362 sub format_ref_marker
{
1363 my ($refs, $id) = @_;
1366 if (defined $refs->{$id}) {
1367 foreach my $ref (@{$refs->{$id}}) {
1368 # this code exploits the fact that non-lightweight tags are the
1369 # only indirect objects, and that they are the only objects for which
1370 # we want to use tag instead of shortlog as action
1371 my ($type, $name) = qw();
1372 my $indirect = ($ref =~ s/\^\{\}$//);
1373 # e.g. tags/v2.6.11 or heads/next
1374 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1383 $class .= " indirect" if $indirect;
1385 my $dest_action = "shortlog";
1388 $dest_action = "tag" unless $action eq "tag";
1389 } elsif ($action =~ /^(history|(short)?log)$/) {
1390 $dest_action = $action;
1394 $dest .= "refs/" unless $ref =~ m
!^refs
/!;
1397 my $link = $cgi->a({
1399 action
=>$dest_action,
1403 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1409 return ' <span class="refs">'. $markers . '</span>';
1415 # format, perhaps shortened and with markers, title line
1416 sub format_subject_html
{
1417 my ($long, $short, $href, $extra) = @_;
1418 $extra = '' unless defined($extra);
1420 if (length($short) < length($long)) {
1421 return $cgi->a({-href
=> $href, -class => "list subject",
1422 -title
=> to_utf8
($long)},
1423 esc_html
($short) . $extra);
1425 return $cgi->a({-href
=> $href, -class => "list subject"},
1426 esc_html
($long) . $extra);
1430 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1431 sub format_git_diff_header_line
{
1433 my $diffinfo = shift;
1434 my ($from, $to) = @_;
1436 if ($diffinfo->{'nparents'}) {
1438 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1439 if ($to->{'href'}) {
1440 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
1441 esc_path
($to->{'file'}));
1442 } else { # file was deleted (no href)
1443 $line .= esc_path
($to->{'file'});
1447 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1448 if ($from->{'href'}) {
1449 $line .= $cgi->a({-href
=> $from->{'href'}, -class => "path"},
1450 'a/' . esc_path
($from->{'file'}));
1451 } else { # file was added (no href)
1452 $line .= 'a/' . esc_path
($from->{'file'});
1455 if ($to->{'href'}) {
1456 $line .= $cgi->a({-href
=> $to->{'href'}, -class => "path"},
1457 'b/' . esc_path
($to->{'file'}));
1458 } else { # file was deleted
1459 $line .= 'b/' . esc_path
($to->{'file'});
1463 return "<div class=\"diff header\">$line</div>\n";
1466 # format extended diff header line, before patch itself
1467 sub format_extended_diff_header_line
{
1469 my $diffinfo = shift;
1470 my ($from, $to) = @_;
1473 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1474 $line .= $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
1475 esc_path
($from->{'file'}));
1477 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1478 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
1479 esc_path
($to->{'file'}));
1481 # match single <mode>
1482 if ($line =~ m/\s(\d{6})$/) {
1483 $line .= '<span class="info"> (' .
1484 file_type_long
($1) .
1488 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1489 # can match only for combined diff
1491 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1492 if ($from->{'href'}[$i]) {
1493 $line .= $cgi->a({-href
=>$from->{'href'}[$i],
1495 substr($diffinfo->{'from_id'}[$i],0,7));
1500 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1503 if ($to->{'href'}) {
1504 $line .= $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
1505 substr($diffinfo->{'to_id'},0,7));
1510 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1511 # can match only for ordinary diff
1512 my ($from_link, $to_link);
1513 if ($from->{'href'}) {
1514 $from_link = $cgi->a({-href
=>$from->{'href'}, -class=>"hash"},
1515 substr($diffinfo->{'from_id'},0,7));
1517 $from_link = '0' x
7;
1519 if ($to->{'href'}) {
1520 $to_link = $cgi->a({-href
=>$to->{'href'}, -class=>"hash"},
1521 substr($diffinfo->{'to_id'},0,7));
1525 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1526 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1529 return $line . "<br/>\n";
1532 # format from-file/to-file diff header
1533 sub format_diff_from_to_header
{
1534 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1539 #assert($line =~ m/^---/) if DEBUG;
1540 # no extra formatting for "^--- /dev/null"
1541 if (! $diffinfo->{'nparents'}) {
1542 # ordinary (single parent) diff
1543 if ($line =~ m!^--- "?a/!) {
1544 if ($from->{'href'}) {
1546 $cgi->a({-href
=>$from->{'href'}, -class=>"path"},
1547 esc_path
($from->{'file'}));
1550 esc_path
($from->{'file'});
1553 $result .= qq
!<div
class="diff from_file">$line</div
>\n!;
1556 # combined diff (merge commit)
1557 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1558 if ($from->{'href'}[$i]) {
1560 $cgi->a({-href
=>href
(action
=>"blobdiff",
1561 hash_parent
=>$diffinfo->{'from_id'}[$i],
1562 hash_parent_base
=>$parents[$i],
1563 file_parent
=>$from->{'file'}[$i],
1564 hash
=>$diffinfo->{'to_id'},
1566 file_name
=>$to->{'file'}),
1568 -title
=>"diff" . ($i+1)},
1571 $cgi->a({-href
=>$from->{'href'}[$i], -class=>"path"},
1572 esc_path
($from->{'file'}[$i]));
1574 $line = '--- /dev/null';
1576 $result .= qq
!<div
class="diff from_file">$line</div
>\n!;
1581 #assert($line =~ m/^\+\+\+/) if DEBUG;
1582 # no extra formatting for "^+++ /dev/null"
1583 if ($line =~ m!^\+\+\+ "?b/!) {
1584 if ($to->{'href'}) {
1586 $cgi->a({-href
=>$to->{'href'}, -class=>"path"},
1587 esc_path
($to->{'file'}));
1590 esc_path
($to->{'file'});
1593 $result .= qq
!<div
class="diff to_file">$line</div
>\n!;
1598 # create note for patch simplified by combined diff
1599 sub format_diff_cc_simplified
{
1600 my ($diffinfo, @parents) = @_;
1603 $result .= "<div class=\"diff header\">" .
1605 if (!is_deleted
($diffinfo)) {
1606 $result .= $cgi->a({-href
=> href
(action
=>"blob",
1608 hash
=>$diffinfo->{'to_id'},
1609 file_name
=>$diffinfo->{'to_file'}),
1611 esc_path
($diffinfo->{'to_file'}));
1613 $result .= esc_path
($diffinfo->{'to_file'});
1615 $result .= "</div>\n" . # class="diff header"
1616 "<div class=\"diff nodifferences\">" .
1618 "</div>\n"; # class="diff nodifferences"
1623 # format patch (diff) line (not to be used for diff headers)
1624 sub format_diff_line
{
1626 my ($from, $to) = @_;
1627 my $diff_class = "";
1631 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1633 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1634 if ($line =~ m/^\@{3}/) {
1635 $diff_class = " chunk_header";
1636 } elsif ($line =~ m/^\\/) {
1637 $diff_class = " incomplete";
1638 } elsif ($prefix =~ tr/+/+/) {
1639 $diff_class = " add";
1640 } elsif ($prefix =~ tr/-/-/) {
1641 $diff_class = " rem";
1644 # assume ordinary diff
1645 my $char = substr($line, 0, 1);
1647 $diff_class = " add";
1648 } elsif ($char eq '-') {
1649 $diff_class = " rem";
1650 } elsif ($char eq '@') {
1651 $diff_class = " chunk_header";
1652 } elsif ($char eq "\\") {
1653 $diff_class = " incomplete";
1656 $line = untabify
($line);
1657 if ($from && $to && $line =~ m/^\@{2} /) {
1658 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1659 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1661 $from_lines = 0 unless defined $from_lines;
1662 $to_lines = 0 unless defined $to_lines;
1664 if ($from->{'href'}) {
1665 $from_text = $cgi->a({-href
=>"$from->{'href'}#l$from_start",
1666 -class=>"list"}, $from_text);
1668 if ($to->{'href'}) {
1669 $to_text = $cgi->a({-href
=>"$to->{'href'}#l$to_start",
1670 -class=>"list"}, $to_text);
1672 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1673 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
1674 return "<div class=\"diff$diff_class\">$line</div>\n";
1675 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1676 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1677 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1679 @from_text = split(' ', $ranges);
1680 for (my $i = 0; $i < @from_text; ++$i) {
1681 ($from_start[$i], $from_nlines[$i]) =
1682 (split(',', substr($from_text[$i], 1)), 0);
1685 $to_text = pop @from_text;
1686 $to_start = pop @from_start;
1687 $to_nlines = pop @from_nlines;
1689 $line = "<span class=\"chunk_info\">$prefix ";
1690 for (my $i = 0; $i < @from_text; ++$i) {
1691 if ($from->{'href'}[$i]) {
1692 $line .= $cgi->a({-href
=>"$from->{'href'}[$i]#l$from_start[$i]",
1693 -class=>"list"}, $from_text[$i]);
1695 $line .= $from_text[$i];
1699 if ($to->{'href'}) {
1700 $line .= $cgi->a({-href
=>"$to->{'href'}#l$to_start",
1701 -class=>"list"}, $to_text);
1705 $line .= " $prefix</span>" .
1706 "<span class=\"section\">" . esc_html
($section, -nbsp
=>1) . "</span>";
1707 return "<div class=\"diff$diff_class\">$line</div>\n";
1709 return "<div class=\"diff$diff_class\">" . esc_html
($line, -nbsp
=>1) . "</div>\n";
1712 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1713 # linked. Pass the hash of the tree/commit to snapshot.
1714 sub format_snapshot_links
{
1716 my $num_fmts = @snapshot_fmts;
1717 if ($num_fmts > 1) {
1718 # A parenthesized list of links bearing format names.
1719 # e.g. "snapshot (_tar.gz_ _zip_)"
1720 return "snapshot (" . join(' ', map
1727 }, $known_snapshot_formats{$_}{'display'})
1728 , @snapshot_fmts) . ")";
1729 } elsif ($num_fmts == 1) {
1730 # A single "snapshot" link whose tooltip bears the format name.
1732 my ($fmt) = @snapshot_fmts;
1738 snapshot_format
=>$fmt
1740 -title
=> "in format: $known_snapshot_formats{$fmt}{'display'}"
1742 } else { # $num_fmts == 0
1747 ## ......................................................................
1748 ## functions returning values to be passed, perhaps after some
1749 ## transformation, to other functions; e.g. returning arguments to href()
1751 # returns hash to be passed to href to generate gitweb URL
1752 # in -title key it returns description of link
1754 my $format = shift || 'Atom';
1755 my %res = (action
=> lc($format));
1757 # feed links are possible only for project views
1758 return unless (defined $project);
1759 # some views should link to OPML, or to generic project feed,
1760 # or don't have specific feed yet (so they should use generic)
1761 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1764 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1765 # from tag links; this also makes possible to detect branch links
1766 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1767 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1770 # find log type for feed description (title)
1772 if (defined $file_name) {
1773 $type = "history of $file_name";
1774 $type .= "/" if ($action eq 'tree');
1775 $type .= " on '$branch'" if (defined $branch);
1777 $type = "log of $branch" if (defined $branch);
1780 $res{-title
} = $type;
1781 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1782 $res{'file_name'} = $file_name;
1787 ## ----------------------------------------------------------------------
1788 ## git utility subroutines, invoking git commands
1790 # returns path to the core git executable and the --git-dir parameter as list
1792 return $GIT, '--git-dir='.$git_dir;
1795 # quote the given arguments for passing them to the shell
1796 # quote_command("command", "arg 1", "arg with ' and ! characters")
1797 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1798 # Try to avoid using this function wherever possible.
1801 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1804 # get HEAD ref of given project as hash
1805 sub git_get_head_hash
{
1806 my $project = shift;
1807 my $o_git_dir = $git_dir;
1809 $git_dir = "$projectroot/$project";
1810 if (open my $fd, "-|", git_cmd
(), "rev-parse", "--verify", "HEAD") {
1813 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1817 if (defined $o_git_dir) {
1818 $git_dir = $o_git_dir;
1823 # get type of given object
1827 open my $fd, "-|", git_cmd
(), "cat-file", '-t', $hash or return;
1829 close $fd or return;
1834 # repository configuration
1835 our $config_file = '';
1838 # store multiple values for single key as anonymous array reference
1839 # single values stored directly in the hash, not as [ <value> ]
1840 sub hash_set_multi
{
1841 my ($hash, $key, $value) = @_;
1843 if (!exists $hash->{$key}) {
1844 $hash->{$key} = $value;
1845 } elsif (!ref $hash->{$key}) {
1846 $hash->{$key} = [ $hash->{$key}, $value ];
1848 push @{$hash->{$key}}, $value;
1852 # return hash of git project configuration
1853 # optionally limited to some section, e.g. 'gitweb'
1854 sub git_parse_project_config
{
1855 my $section_regexp = shift;
1860 open my $fh, "-|", git_cmd
(), "config", '-z', '-l',
1863 while (my $keyval = <$fh>) {
1865 my ($key, $value) = split(/\n/, $keyval, 2);
1867 hash_set_multi
(\
%config, $key, $value)
1868 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1875 # convert config value to boolean, 'true' or 'false'
1876 # no value, number > 0, 'true' and 'yes' values are true
1877 # rest of values are treated as false (never as error)
1878 sub config_to_bool
{
1881 # strip leading and trailing whitespace
1885 return (!defined $val || # section.key
1886 ($val =~ /^\d+$/ && $val) || # section.key = 1
1887 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1890 # convert config value to simple decimal number
1891 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1892 # to be multiplied by 1024, 1048576, or 1073741824
1896 # strip leading and trailing whitespace
1900 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1902 # unknown unit is treated as 1
1903 return $num * ($unit eq 'g' ? 1073741824 :
1904 $unit eq 'm' ? 1048576 :
1905 $unit eq 'k' ? 1024 : 1);
1910 # convert config value to array reference, if needed
1911 sub config_to_multi
{
1914 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1917 sub git_get_project_config
{
1918 my ($key, $type) = @_;
1921 return unless ($key);
1922 $key =~ s/^gitweb\.//;
1923 return if ($key =~ m/\W/);
1926 if (defined $type) {
1929 unless ($type eq 'bool' || $type eq 'int');
1933 if (!defined $config_file ||
1934 $config_file ne "$git_dir/config") {
1935 %config = git_parse_project_config
('gitweb');
1936 $config_file = "$git_dir/config";
1940 if (!defined $type) {
1941 return $config{"gitweb.$key"};
1942 } elsif ($type eq 'bool') {
1943 # backward compatibility: 'git config --bool' returns true/false
1944 return config_to_bool
($config{"gitweb.$key"}) ? 'true' : 'false';
1945 } elsif ($type eq 'int') {
1946 return config_to_int
($config{"gitweb.$key"});
1948 return $config{"gitweb.$key"};
1951 # get hash of given path at given ref
1952 sub git_get_hash_by_path
{
1954 my $path = shift || return undef;
1959 open my $fd, "-|", git_cmd
(), "ls-tree", $base, "--", $path
1960 or die_error
(500, "Open git-ls-tree failed");
1962 close $fd or return undef;
1964 if (!defined $line) {
1965 # there is no tree or hash given by $path at $base
1969 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1970 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1971 if (defined $type && $type ne $2) {
1972 # type doesn't match
1978 # get path of entry with given hash at given tree-ish (ref)
1979 # used to get 'from' filename for combined diff (merge commit) for renames
1980 sub git_get_path_by_hash
{
1981 my $base = shift || return;
1982 my $hash = shift || return;
1986 open my $fd, "-|", git_cmd
(), "ls-tree", '-r', '-t', '-z', $base
1988 while (my $line = <$fd>) {
1991 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1992 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1993 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2002 ## ......................................................................
2003 ## git utility functions, directly accessing git repository
2005 sub git_get_project_description
{
2008 $git_dir = "$projectroot/$path";
2009 open my $fd, "$git_dir/description"
2010 or return git_get_project_config
('description');
2013 if (defined $descr) {
2019 sub git_get_project_ctags
{
2023 $git_dir = "$projectroot/$path";
2024 unless (opendir D
, "$git_dir/ctags") {
2027 foreach (grep { -f
$_ } map { "$git_dir/ctags/$_" } readdir(D
)) {
2028 open CT
, $_ or next;
2032 my $ctag = $_; $ctag =~ s
#.*/##;
2033 $ctags->{$ctag} = $val;
2039 sub git_populate_project_tagcloud
{
2042 # First, merge different-cased tags; tags vote on casing
2044 foreach (keys %$ctags) {
2045 $ctags_lc{lc $_}->{count
} += $ctags->{$_};
2046 if (not $ctags_lc{lc $_}->{topcount
}
2047 or $ctags_lc{lc $_}->{topcount
} < $ctags->{$_}) {
2048 $ctags_lc{lc $_}->{topcount
} = $ctags->{$_};
2049 $ctags_lc{lc $_}->{topname
} = $_;
2054 if (eval { require HTML
::TagCloud
; 1; }) {
2055 $cloud = HTML
::TagCloud-
>new;
2056 foreach (sort keys %ctags_lc) {
2057 # Pad the title with spaces so that the cloud looks
2059 my $title = $ctags_lc{$_}->{topname
};
2060 $title =~ s/ / /g;
2061 $title =~ s/^/ /g;
2062 $title =~ s/$/ /g;
2063 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count
});
2066 $cloud = \
%ctags_lc;
2071 sub git_show_project_tagcloud
{
2072 my ($cloud, $count) = @_;
2073 print STDERR
ref($cloud)."..\n";
2074 if (ref $cloud eq 'HTML::TagCloud') {
2075 return $cloud->html_and_css($count);
2077 my @tags = sort { $cloud->{$a}->{count
} <=> $cloud->{$b}->{count
} } keys %$cloud;
2078 return '<p align="center">' . join (', ', map {
2079 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2080 } splice(@tags, 0, $count)) . '</p>';
2084 sub git_get_project_url_list
{
2087 $git_dir = "$projectroot/$path";
2088 open my $fd, "$git_dir/cloneurl"
2089 or return wantarray ?
2090 @{ config_to_multi
(git_get_project_config
('url')) } :
2091 config_to_multi
(git_get_project_config
('url'));
2092 my @git_project_url_list = map { chomp; $_ } <$fd>;
2095 return wantarray ? @git_project_url_list : \
@git_project_url_list;
2098 sub git_get_projects_list
{
2103 $filter =~ s/\.git$//;
2105 my ($check_forks) = gitweb_check_feature
('forks');
2107 if (-d
$projects_list) {
2108 # search in directory
2109 my $dir = $projects_list . ($filter ? "/$filter" : '');
2110 # remove the trailing "/"
2112 my $pfxlen = length("$dir");
2113 my $pfxdepth = ($dir =~ tr!/!!);
2116 follow_fast
=> 1, # follow symbolic links
2117 follow_skip
=> 2, # ignore duplicates
2118 dangling_symlinks
=> 0, # ignore dangling symlinks, silently
2120 # skip project-list toplevel, if we get it.
2121 return if (m!^[/.]$!);
2122 # only directories can be git repositories
2123 return unless (-d
$_);
2124 # don't traverse too deep (Find is super slow on os x)
2125 if (($File::Find
::name
=~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2126 $File::Find
::prune
= 1;
2130 my $subdir = substr($File::Find
::name
, $pfxlen + 1);
2131 # we check related file in $projectroot
2132 if (check_export_ok
("$projectroot/$filter/$subdir")) {
2133 push @list, { path
=> ($filter ? "$filter/" : '') . $subdir };
2134 $File::Find
::prune
= 1;
2139 } elsif (-f
$projects_list) {
2140 # read from file(url-encoded):
2141 # 'git%2Fgit.git Linus+Torvalds'
2142 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2143 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2145 open my ($fd), $projects_list or return;
2147 while (my $line = <$fd>) {
2149 my ($path, $owner) = split ' ', $line;
2150 $path = unescape
($path);
2151 $owner = unescape
($owner);
2152 if (!defined $path) {
2155 if ($filter ne '') {
2156 # looking for forks;
2157 my $pfx = substr($path, 0, length($filter));
2158 if ($pfx ne $filter) {
2161 my $sfx = substr($path, length($filter));
2162 if ($sfx !~ /^\/.*\
.git
$/) {
2165 } elsif ($check_forks) {
2167 foreach my $filter (keys %paths) {
2168 # looking for forks;
2169 my $pfx = substr($path, 0, length($filter));
2170 if ($pfx ne $filter) {
2173 my $sfx = substr($path, length($filter));
2174 if ($sfx !~ /^\/.*\
.git
$/) {
2177 # is a fork, don't include it in
2182 if (check_export_ok
("$projectroot/$path")) {
2185 owner
=> to_utf8
($owner),
2188 (my $forks_path = $path) =~ s/\.git$//;
2189 $paths{$forks_path}++;
2197 our $gitweb_project_owner = undef;
2198 sub git_get_project_list_from_file
{
2200 return if (defined $gitweb_project_owner);
2202 $gitweb_project_owner = {};
2203 # read from file (url-encoded):
2204 # 'git%2Fgit.git Linus+Torvalds'
2205 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2206 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2207 if (-f
$projects_list) {
2208 open (my $fd , $projects_list);
2209 while (my $line = <$fd>) {
2211 my ($pr, $ow) = split ' ', $line;
2212 $pr = unescape
($pr);
2213 $ow = unescape
($ow);
2214 $gitweb_project_owner->{$pr} = to_utf8
($ow);
2220 sub git_get_project_owner
{
2221 my $project = shift;
2224 return undef unless $project;
2225 $git_dir = "$projectroot/$project";
2227 if (!defined $gitweb_project_owner) {
2228 git_get_project_list_from_file
();
2231 if (exists $gitweb_project_owner->{$project}) {
2232 $owner = $gitweb_project_owner->{$project};
2234 if (!defined $owner){
2235 $owner = git_get_project_config
('owner');
2237 if (!defined $owner) {
2238 $owner = get_file_owner
("$git_dir");
2244 sub git_get_last_activity
{
2248 $git_dir = "$projectroot/$path";
2249 open($fd, "-|", git_cmd
(), 'for-each-ref',
2250 '--format=%(committer)',
2251 '--sort=-committerdate',
2253 'refs/heads') or return;
2254 my $most_recent = <$fd>;
2255 close $fd or return;
2256 if (defined $most_recent &&
2257 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2259 my $age = time - $timestamp;
2260 return ($age, age_string
($age));
2262 return (undef, undef);
2265 sub git_get_references
{
2266 my $type = shift || "";
2268 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2269 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2270 open my $fd, "-|", git_cmd
(), "show-ref", "--dereference",
2271 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2274 while (my $line = <$fd>) {
2276 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2277 if (defined $refs{$1}) {
2278 push @{$refs{$1}}, $2;
2284 close $fd or return;
2288 sub git_get_rev_name_tags
{
2289 my $hash = shift || return undef;
2291 open my $fd, "-|", git_cmd
(), "name-rev", "--tags", $hash
2293 my $name_rev = <$fd>;
2296 if ($name_rev =~ m
|^$hash tags
/(.*)$|) {
2299 # catches also '$hash undefined' output
2304 ## ----------------------------------------------------------------------
2305 ## parse to hash functions
2309 my $tz = shift || "-0000";
2312 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2313 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2314 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2315 $date{'hour'} = $hour;
2316 $date{'minute'} = $min;
2317 $date{'mday'} = $mday;
2318 $date{'day'} = $days[$wday];
2319 $date{'month'} = $months[$mon];
2320 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2321 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2322 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2323 $mday, $months[$mon], $hour ,$min;
2324 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2325 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2327 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2328 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2329 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2330 $date{'hour_local'} = $hour;
2331 $date{'minute_local'} = $min;
2332 $date{'tz_local'} = $tz;
2333 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2334 1900+$year, $mon+1, $mday,
2335 $hour, $min, $sec, $tz);
2344 open my $fd, "-|", git_cmd
(), "cat-file", "tag", $tag_id or return;
2345 $tag{'id'} = $tag_id;
2346 while (my $line = <$fd>) {
2348 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2349 $tag{'object'} = $1;
2350 } elsif ($line =~ m/^type (.+)$/) {
2352 } elsif ($line =~ m/^tag (.+)$/) {
2354 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2355 $tag{'author'} = $1;
2358 } elsif ($line =~ m/--BEGIN/) {
2359 push @comment, $line;
2361 } elsif ($line eq "") {
2365 push @comment, <$fd>;
2366 $tag{'comment'} = \
@comment;
2367 close $fd or return;
2368 if (!defined $tag{'name'}) {
2374 sub parse_commit_text
{
2375 my ($commit_text, $withparents) = @_;
2376 my @commit_lines = split '\n', $commit_text;
2379 pop @commit_lines; # Remove '\0'
2381 if (! @commit_lines) {
2385 my $header = shift @commit_lines;
2386 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2389 ($co{'id'}, my @parents) = split ' ', $header;
2390 while (my $line = shift @commit_lines) {
2391 last if $line eq "\n";
2392 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2394 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2396 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2398 $co{'author_epoch'} = $2;
2399 $co{'author_tz'} = $3;
2400 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2401 $co{'author_name'} = $1;
2402 $co{'author_email'} = $2;
2404 $co{'author_name'} = $co{'author'};
2406 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2407 $co{'committer'} = $1;
2408 $co{'committer_epoch'} = $2;
2409 $co{'committer_tz'} = $3;
2410 $co{'committer_name'} = $co{'committer'};
2411 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2412 $co{'committer_name'} = $1;
2413 $co{'committer_email'} = $2;
2415 $co{'committer_name'} = $co{'committer'};
2419 if (!defined $co{'tree'}) {
2422 $co{'parents'} = \
@parents;
2423 $co{'parent'} = $parents[0];
2425 foreach my $title (@commit_lines) {
2428 $co{'title'} = chop_str
($title, 80, 5);
2429 # remove leading stuff of merges to make the interesting part visible
2430 if (length($title) > 50) {
2431 $title =~ s/^Automatic //;
2432 $title =~ s/^merge (of|with) /Merge ... /i;
2433 if (length($title) > 50) {
2434 $title =~ s/(http|rsync):\/\///;
2436 if (length($title) > 50) {
2437 $title =~ s/(master|www|rsync)\.//;
2439 if (length($title) > 50) {
2440 $title =~ s/kernel.org:?//;
2442 if (length($title) > 50) {
2443 $title =~ s/\/pub\/scm//;
2446 $co{'title_short'} = chop_str
($title, 50, 5);
2450 if (! defined $co{'title'} || $co{'title'} eq "") {
2451 $co{'title'} = $co{'title_short'} = '(no commit message)';
2453 # remove added spaces
2454 foreach my $line (@commit_lines) {
2457 $co{'comment'} = \
@commit_lines;
2459 my $age = time - $co{'committer_epoch'};
2461 $co{'age_string'} = age_string
($age);
2462 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2463 if ($age > 60*60*24*7*2) {
2464 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2465 $co{'age_string_age'} = $co{'age_string'};
2467 $co{'age_string_date'} = $co{'age_string'};
2468 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2474 my ($commit_id) = @_;
2479 open my $fd, "-|", git_cmd
(), "rev-list",
2485 or die_error
(500, "Open git-rev-list failed");
2486 %co = parse_commit_text
(<$fd>, 1);
2493 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2501 open my $fd, "-|", git_cmd
(), "rev-list",
2504 ("--max-count=" . $maxcount),
2505 ("--skip=" . $skip),
2509 ($filename ? ($filename) : ())
2510 or die_error
(500, "Open git-rev-list failed");
2511 while (my $line = <$fd>) {
2512 my %co = parse_commit_text
($line);
2517 return wantarray ? @cos : \
@cos;
2520 # parse line of git-diff-tree "raw" output
2521 sub parse_difftree_raw_line
{
2525 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2526 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2527 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2528 $res{'from_mode'} = $1;
2529 $res{'to_mode'} = $2;
2530 $res{'from_id'} = $3;
2532 $res{'status'} = $5;
2533 $res{'similarity'} = $6;
2534 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2535 ($res{'from_file'}, $res{'to_file'}) = map { unquote
($_) } split("\t", $7);
2537 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote
($7);
2540 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2541 # combined diff (for merge commit)
2542 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2543 $res{'nparents'} = length($1);
2544 $res{'from_mode'} = [ split(' ', $2) ];
2545 $res{'to_mode'} = pop @{$res{'from_mode'}};
2546 $res{'from_id'} = [ split(' ', $3) ];
2547 $res{'to_id'} = pop @{$res{'from_id'}};
2548 $res{'status'} = [ split('', $4) ];
2549 $res{'to_file'} = unquote
($5);
2551 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2552 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2553 $res{'commit'} = $1;
2556 return wantarray ? %res : \
%res;
2559 # wrapper: return parsed line of git-diff-tree "raw" output
2560 # (the argument might be raw line, or parsed info)
2561 sub parsed_difftree_line
{
2562 my $line_or_ref = shift;
2564 if (ref($line_or_ref) eq "HASH") {
2565 # pre-parsed (or generated by hand)
2566 return $line_or_ref;
2568 return parse_difftree_raw_line
($line_or_ref);
2572 # parse line of git-ls-tree output
2573 sub parse_ls_tree_line
($;%) {
2578 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2579 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2587 $res{'name'} = unquote
($4);
2590 return wantarray ? %res : \
%res;
2593 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2594 sub parse_from_to_diffinfo
{
2595 my ($diffinfo, $from, $to, @parents) = @_;
2597 if ($diffinfo->{'nparents'}) {
2599 $from->{'file'} = [];
2600 $from->{'href'} = [];
2601 fill_from_file_info
($diffinfo, @parents)
2602 unless exists $diffinfo->{'from_file'};
2603 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2604 $from->{'file'}[$i] =
2605 defined $diffinfo->{'from_file'}[$i] ?
2606 $diffinfo->{'from_file'}[$i] :
2607 $diffinfo->{'to_file'};
2608 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2609 $from->{'href'}[$i] = href
(action
=>"blob",
2610 hash_base
=>$parents[$i],
2611 hash
=>$diffinfo->{'from_id'}[$i],
2612 file_name
=>$from->{'file'}[$i]);
2614 $from->{'href'}[$i] = undef;
2618 # ordinary (not combined) diff
2619 $from->{'file'} = $diffinfo->{'from_file'};
2620 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2621 $from->{'href'} = href
(action
=>"blob", hash_base
=>$hash_parent,
2622 hash
=>$diffinfo->{'from_id'},
2623 file_name
=>$from->{'file'});
2625 delete $from->{'href'};
2629 $to->{'file'} = $diffinfo->{'to_file'};
2630 if (!is_deleted
($diffinfo)) { # file exists in result
2631 $to->{'href'} = href
(action
=>"blob", hash_base
=>$hash,
2632 hash
=>$diffinfo->{'to_id'},
2633 file_name
=>$to->{'file'});
2635 delete $to->{'href'};
2639 ## ......................................................................
2640 ## parse to array of hashes functions
2642 sub git_get_heads_list
{
2646 open my $fd, '-|', git_cmd
(), 'for-each-ref',
2647 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2648 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2651 while (my $line = <$fd>) {
2655 my ($refinfo, $committerinfo) = split(/\0/, $line);
2656 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2657 my ($committer, $epoch, $tz) =
2658 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2659 $ref_item{'fullname'} = $name;
2660 $name =~ s!^refs/heads/!!;
2662 $ref_item{'name'} = $name;
2663 $ref_item{'id'} = $hash;
2664 $ref_item{'title'} = $title || '(no commit message)';
2665 $ref_item{'epoch'} = $epoch;
2667 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
2669 $ref_item{'age'} = "unknown";
2672 push @headslist, \
%ref_item;
2676 return wantarray ? @headslist : \
@headslist;
2679 sub git_get_tags_list
{
2683 open my $fd, '-|', git_cmd
(), 'for-each-ref',
2684 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2685 '--format=%(objectname) %(objecttype) %(refname) '.
2686 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2689 while (my $line = <$fd>) {
2693 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2694 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2695 my ($creator, $epoch, $tz) =
2696 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2697 $ref_item{'fullname'} = $name;
2698 $name =~ s!^refs/tags/!!;
2700 $ref_item{'type'} = $type;
2701 $ref_item{'id'} = $id;
2702 $ref_item{'name'} = $name;
2703 if ($type eq "tag") {
2704 $ref_item{'subject'} = $title;
2705 $ref_item{'reftype'} = $reftype;
2706 $ref_item{'refid'} = $refid;
2708 $ref_item{'reftype'} = $type;
2709 $ref_item{'refid'} = $id;
2712 if ($type eq "tag" || $type eq "commit") {
2713 $ref_item{'epoch'} = $epoch;
2715 $ref_item{'age'} = age_string
(time - $ref_item{'epoch'});
2717 $ref_item{'age'} = "unknown";
2721 push @tagslist, \
%ref_item;
2725 return wantarray ? @tagslist : \
@tagslist;
2728 ## ----------------------------------------------------------------------
2729 ## filesystem-related functions
2731 sub get_file_owner
{
2734 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2735 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2736 if (!defined $gcos) {
2740 $owner =~ s/[,;].*$//;
2741 return to_utf8
($owner);
2744 ## ......................................................................
2745 ## mimetype related functions
2747 sub mimetype_guess_file
{
2748 my $filename = shift;
2749 my $mimemap = shift;
2750 -r
$mimemap or return undef;
2753 open(MIME
, $mimemap) or return undef;
2755 next if m/^#/; # skip comments
2756 my ($mime, $exts) = split(/\t+/);
2757 if (defined $exts) {
2758 my @exts = split(/\s+/, $exts);
2759 foreach my $ext (@exts) {
2760 $mimemap{$ext} = $mime;
2766 $filename =~ /\.([^.]*)$/;
2767 return $mimemap{$1};
2770 sub mimetype_guess
{
2771 my $filename = shift;
2773 $filename =~ /\./ or return undef;
2775 if ($mimetypes_file) {
2776 my $file = $mimetypes_file;
2777 if ($file !~ m!^/!) { # if it is relative path
2778 # it is relative to project
2779 $file = "$projectroot/$project/$file";
2781 $mime = mimetype_guess_file
($filename, $file);
2783 $mime ||= mimetype_guess_file
($filename, '/etc/mime.types');
2789 my $filename = shift;
2792 my $mime = mimetype_guess
($filename);
2793 $mime and return $mime;
2797 return $default_blob_plain_mimetype unless $fd;
2800 return 'text/plain';
2801 } elsif (! $filename) {
2802 return 'application/octet-stream';
2803 } elsif ($filename =~ m/\.png$/i) {
2805 } elsif ($filename =~ m/\.gif$/i) {
2807 } elsif ($filename =~ m/\.jpe?g$/i) {
2808 return 'image/jpeg';
2810 return 'application/octet-stream';
2814 sub blob_contenttype
{
2815 my ($fd, $file_name, $type) = @_;
2817 $type ||= blob_mimetype
($fd, $file_name);
2818 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2819 $type .= "; charset=$default_text_plain_charset";
2825 ## ======================================================================
2826 ## functions printing HTML: header, footer, error page
2828 sub git_header_html
{
2829 my $status = shift || "200 OK";
2830 my $expires = shift;
2832 my $title = "$site_name";
2833 if (defined $project) {
2834 $title .= " - " . to_utf8
($project);
2835 if (defined $action) {
2836 $title .= "/$action";
2837 if (defined $file_name) {
2838 $title .= " - " . esc_path
($file_name);
2839 if ($action eq "tree" && $file_name !~ m
|/$|) {
2846 # require explicit support from the UA if we are to send the page as
2847 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2848 # we have to do this because MSIE sometimes globs '*/*', pretending to
2849 # support xhtml+xml but choking when it gets what it asked for.
2850 if (defined $cgi->http('HTTP_ACCEPT') &&
2851 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\
+xml
(,|;|\s
|$)/ &&
2852 $cgi->Accept('application/xhtml+xml') != 0) {
2853 $content_type = 'application/xhtml+xml';
2855 $content_type = 'text/html';
2857 print $cgi->header(-type
=>$content_type, -charset
=> 'utf-8',
2858 -status
=> $status, -expires
=> $expires);
2859 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2861 <?xml version="1.0" encoding="utf-8"?>
2862 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2863 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2864 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2865 <!-- git core binaries version $git_version -->
2867 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2868 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2869 <meta name="robots" content="index, nofollow"/>
2870 <title>$title</title>
2872 # print out each stylesheet that exist
2873 if (defined $stylesheet) {
2874 #provides backwards capability for those people who define style sheet in a config file
2875 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2877 foreach my $stylesheet (@stylesheets) {
2878 next unless $stylesheet;
2879 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2882 if (defined $project) {
2883 my %href_params = get_feed_info
();
2884 if (!exists $href_params{'-title'}) {
2885 $href_params{'-title'} = 'log';
2888 foreach my $format qw(RSS Atom) {
2889 my $type = lc($format);
2891 '-rel' => 'alternate',
2892 '-title' => "$project - $href_params{'-title'} - $format feed",
2893 '-type' => "application/$type+xml"
2896 $href_params{'action'} = $type;
2897 $link_attr{'-href'} = href
(%href_params);
2899 "rel=\"$link_attr{'-rel'}\" ".
2900 "title=\"$link_attr{'-title'}\" ".
2901 "href=\"$link_attr{'-href'}\" ".
2902 "type=\"$link_attr{'-type'}\" ".
2905 $href_params{'extra_options'} = '--no-merges';
2906 $link_attr{'-href'} = href
(%href_params);
2907 $link_attr{'-title'} .= ' (no merges)';
2909 "rel=\"$link_attr{'-rel'}\" ".
2910 "title=\"$link_attr{'-title'}\" ".
2911 "href=\"$link_attr{'-href'}\" ".
2912 "type=\"$link_attr{'-type'}\" ".
2917 printf('<link rel="alternate" title="%s projects list" '.
2918 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2919 $site_name, href
(project
=>undef, action
=>"project_index"));
2920 printf('<link rel="alternate" title="%s projects feeds" '.
2921 'href="%s" type="text/x-opml" />'."\n",
2922 $site_name, href
(project
=>undef, action
=>"opml"));
2924 if (defined $favicon) {
2925 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2931 if (-f
$site_header) {
2932 open (my $fd, $site_header);
2937 print "<div class=\"page_header\">\n" .
2938 $cgi->a({-href
=> esc_url
($logo_url),
2939 -title
=> $logo_label},
2940 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2941 print $cgi->a({-href
=> esc_url
($home_link)}, $home_link_str) . " / ";
2942 if (defined $project) {
2943 print $cgi->a({-href
=> href
(action
=>"summary")}, esc_html
($project));
2944 if (defined $action) {
2951 my ($have_search) = gitweb_check_feature
('search');
2952 if (defined $project && $have_search) {
2953 if (!defined $searchtext) {
2957 if (defined $hash_base) {
2958 $search_hash = $hash_base;
2959 } elsif (defined $hash) {
2960 $search_hash = $hash;
2962 $search_hash = "HEAD";
2964 my $action = $my_uri;
2965 my ($use_pathinfo) = gitweb_check_feature
('pathinfo');
2966 if ($use_pathinfo) {
2967 $action .= "/".esc_url
($project);
2969 print $cgi->startform(-method => "get", -action
=> $action) .
2970 "<div class=\"search\">\n" .
2972 $cgi->input({-name
=>"p", -value
=>$project, -type
=>"hidden"}) . "\n") .
2973 $cgi->input({-name
=>"a", -value
=>"search", -type
=>"hidden"}) . "\n" .
2974 $cgi->input({-name
=>"h", -value
=>$search_hash, -type
=>"hidden"}) . "\n" .
2975 $cgi->popup_menu(-name
=> 'st', -default => 'commit',
2976 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2977 $cgi->sup($cgi->a({-href
=> href
(action
=>"search_help")}, "?")) .
2979 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
2980 "<span title=\"Extended regular expression\">" .
2981 $cgi->checkbox(-name
=> 'sr', -value
=> 1, -label
=> 're',
2982 -checked
=> $search_use_regexp) .
2985 $cgi->end_form() . "\n";
2989 sub git_footer_html
{
2990 my $feed_class = 'rss_logo';
2992 print "<div class=\"page_footer\">\n";
2993 if (defined $project) {
2994 my $descr = git_get_project_description
($project);
2995 if (defined $descr) {
2996 print "<div class=\"page_footer_text\">" . esc_html
($descr) . "</div>\n";
2999 my %href_params = get_feed_info
();
3000 if (!%href_params) {
3001 $feed_class .= ' generic';
3003 $href_params{'-title'} ||= 'log';
3005 foreach my $format qw(RSS Atom) {
3006 $href_params{'action'} = lc($format);
3007 print $cgi->a({-href
=> href
(%href_params),
3008 -title
=> "$href_params{'-title'} $format feed",
3009 -class => $feed_class}, $format)."\n";
3013 print $cgi->a({-href
=> href
(project
=>undef, action
=>"opml"),
3014 -class => $feed_class}, "OPML") . " ";
3015 print $cgi->a({-href
=> href
(project
=>undef, action
=>"project_index"),
3016 -class => $feed_class}, "TXT") . "\n";
3018 print "</div>\n"; # class="page_footer"
3020 if (-f
$site_footer) {
3021 open (my $fd, $site_footer);
3030 # die_error(<http_status_code>, <error_message>)
3031 # Example: die_error(404, 'Hash not found')
3032 # By convention, use the following status codes (as defined in RFC 2616):
3033 # 400: Invalid or missing CGI parameters, or
3034 # requested object exists but has wrong type.
3035 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3036 # this server or project.
3037 # 404: Requested object/revision/project doesn't exist.
3038 # 500: The server isn't configured properly, or
3039 # an internal error occurred (e.g. failed assertions caused by bugs), or
3040 # an unknown error occurred (e.g. the git binary died unexpectedly).
3042 my $status = shift || 500;
3043 my $error = shift || "Internal server error";
3045 my %http_responses = (400 => '400 Bad Request',
3046 403 => '403 Forbidden',
3047 404 => '404 Not Found',
3048 500 => '500 Internal Server Error');
3049 git_header_html
($http_responses{$status});
3051 <div class="page_body">
3061 ## ----------------------------------------------------------------------
3062 ## functions printing or outputting HTML: navigation
3064 sub git_print_page_nav
{
3065 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3066 $extra = '' if !defined $extra; # pager or formats
3068 my @navs = qw(summary shortlog log commit commitdiff tree);
3070 @navs = grep { $_ ne $suppress } @navs;
3073 my %arg = map { $_ => {action
=>$_} } @navs;
3074 if (defined $head) {
3075 for (qw(commit commitdiff)) {
3076 $arg{$_}{'hash'} = $head;
3078 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3079 for (qw(shortlog log)) {
3080 $arg{$_}{'hash'} = $head;
3085 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3086 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3088 my @actions = gitweb_check_feature
('actions');
3091 'n' => $project, # project name
3092 'f' => $git_dir, # project path within filesystem
3093 'h' => $treehead || '', # current hash ('h' parameter)
3094 'b' => $treebase || '', # hash base ('hb' parameter)
3097 my ($label, $link, $pos) = splice(@actions,0,3);
3099 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3101 $link =~ s/%([%nfhb])/$repl{$1}/g;
3102 $arg{$label}{'_href'} = $link;
3105 print "<div class=\"page_nav\">\n" .
3107 map { $_ eq $current ?
3108 $_ : $cgi->a({-href
=> ($arg{$_}{_href
} ? $arg{$_}{_href
} : href
(%{$arg{$_}}))}, "$_")
3110 print "<br/>\n$extra<br/>\n" .
3114 sub format_paging_nav
{
3115 my ($action, $hash, $head, $page, $has_next_link) = @_;
3119 if ($hash ne $head || $page) {
3120 $paging_nav .= $cgi->a({-href
=> href
(action
=>$action)}, "HEAD");
3122 $paging_nav .= "HEAD";
3126 $paging_nav .= " ⋅ " .
3127 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
3128 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
3130 $paging_nav .= " ⋅ prev";
3133 if ($has_next_link) {
3134 $paging_nav .= " ⋅ " .
3135 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
3136 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
3138 $paging_nav .= " ⋅ next";
3144 ## ......................................................................
3145 ## functions printing or outputting HTML: div
3147 sub git_print_header_div
{
3148 my ($action, $title, $hash, $hash_base) = @_;
3151 $args{'action'} = $action;
3152 $args{'hash'} = $hash if $hash;
3153 $args{'hash_base'} = $hash_base if $hash_base;
3155 print "<div class=\"header\">\n" .
3156 $cgi->a({-href
=> href
(%args), -class => "title"},
3157 $title ? $title : $action) .
3161 #sub git_print_authorship (\%) {
3162 sub git_print_authorship
{
3165 my %ad = parse_date
($co->{'author_epoch'}, $co->{'author_tz'});
3166 print "<div class=\"author_date\">" .
3167 esc_html
($co->{'author_name'}) .
3169 if ($ad{'hour_local'} < 6) {
3170 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3171 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3173 printf(" (%02d:%02d %s)",
3174 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3179 sub git_print_page_path
{
3185 print "<div class=\"page_path\">";
3186 print $cgi->a({-href
=> href
(action
=>"tree", hash_base
=>$hb),
3187 -title
=> 'tree root'}, to_utf8
("[$project]"));
3189 if (defined $name) {
3190 my @dirname = split '/', $name;
3191 my $basename = pop @dirname;
3194 foreach my $dir (@dirname) {
3195 $fullname .= ($fullname ? '/' : '') . $dir;
3196 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$fullname,
3198 -title
=> $fullname}, esc_path
($dir));
3201 if (defined $type && $type eq 'blob') {
3202 print $cgi->a({-href
=> href
(action
=>"blob_plain", file_name
=>$file_name,
3204 -title
=> $name}, esc_path
($basename));
3205 } elsif (defined $type && $type eq 'tree') {
3206 print $cgi->a({-href
=> href
(action
=>"tree", file_name
=>$file_name,
3208 -title
=> $name}, esc_path
($basename));
3211 print esc_path
($basename);
3214 print "<br/></div>\n";
3217 # sub git_print_log (\@;%) {
3218 sub git_print_log
($;%) {
3222 if ($opts{'-remove_title'}) {
3223 # remove title, i.e. first line of log
3226 # remove leading empty lines
3227 while (defined $log->[0] && $log->[0] eq "") {
3234 foreach my $line (@$log) {
3235 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3238 if (! $opts{'-remove_signoff'}) {
3239 print "<span class=\"signoff\">" . esc_html
($line) . "</span><br/>\n";
3242 # remove signoff lines
3249 # print only one empty line
3250 # do not print empty line after signoff
3252 next if ($empty || $signoff);
3258 print format_log_line_html
($line) . "<br/>\n";
3261 if ($opts{'-final_empty_line'}) {
3262 # end with single empty line
3263 print "<br/>\n" unless $empty;
3267 # return link target (what link points to)
3268 sub git_get_link_target
{
3273 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
3277 $link_target = <$fd>;
3282 return $link_target;
3285 # given link target, and the directory (basedir) the link is in,
3286 # return target of link relative to top directory (top tree);
3287 # return undef if it is not possible (including absolute links).
3288 sub normalize_link_target
{
3289 my ($link_target, $basedir, $hash_base) = @_;
3291 # we can normalize symlink target only if $hash_base is provided
3292 return unless $hash_base;
3294 # absolute symlinks (beginning with '/') cannot be normalized
3295 return if (substr($link_target, 0, 1) eq '/');
3297 # normalize link target to path from top (root) tree (dir)
3300 $path = $basedir . '/' . $link_target;
3302 # we are in top (root) tree (dir)
3303 $path = $link_target;
3306 # remove //, /./, and /../
3308 foreach my $part (split('/', $path)) {
3309 # discard '.' and ''
3310 next if (!$part || $part eq '.');
3312 if ($part eq '..') {
3316 # link leads outside repository (outside top dir)
3320 push @path_parts, $part;
3323 $path = join('/', @path_parts);
3328 # print tree entry (row of git_tree), but without encompassing <tr> element
3329 sub git_print_tree_entry
{
3330 my ($t, $basedir, $hash_base, $have_blame) = @_;
3333 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3335 # The format of a table row is: mode list link. Where mode is
3336 # the mode of the entry, list is the name of the entry, an href,
3337 # and link is the action links of the entry.
3339 print "<td class=\"mode\">" . mode_str
($t->{'mode'}) . "</td>\n";
3340 if ($t->{'type'} eq "blob") {
3341 print "<td class=\"list\">" .
3342 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
3343 file_name
=>"$basedir$t->{'name'}", %base_key),
3344 -class => "list"}, esc_path
($t->{'name'}));
3345 if (S_ISLNK
(oct $t->{'mode'})) {
3346 my $link_target = git_get_link_target
($t->{'hash'});
3348 my $norm_target = normalize_link_target
($link_target, $basedir, $hash_base);
3349 if (defined $norm_target) {
3351 $cgi->a({-href
=> href
(action
=>"object", hash_base
=>$hash_base,
3352 file_name
=>$norm_target),
3353 -title
=> $norm_target}, esc_path
($link_target));
3355 print " -> " . esc_path
($link_target);
3360 print "<td class=\"link\">";
3361 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$t->{'hash'},
3362 file_name
=>"$basedir$t->{'name'}", %base_key)},
3366 $cgi->a({-href
=> href
(action
=>"blame", hash
=>$t->{'hash'},
3367 file_name
=>"$basedir$t->{'name'}", %base_key)},
3370 if (defined $hash_base) {
3372 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
3373 hash
=>$t->{'hash'}, file_name
=>"$basedir$t->{'name'}")},
3377 $cgi->a({-href
=> href
(action
=>"blob_plain", hash_base
=>$hash_base,
3378 file_name
=>"$basedir$t->{'name'}")},
3382 } elsif ($t->{'type'} eq "tree") {
3383 print "<td class=\"list\">";
3384 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
3385 file_name
=>"$basedir$t->{'name'}", %base_key)},
3386 esc_path
($t->{'name'}));
3388 print "<td class=\"link\">";
3389 print $cgi->a({-href
=> href
(action
=>"tree", hash
=>$t->{'hash'},
3390 file_name
=>"$basedir$t->{'name'}", %base_key)},
3392 if (defined $hash_base) {
3394 $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash_base,
3395 file_name
=>"$basedir$t->{'name'}")},
3400 # unknown object: we can only present history for it
3401 # (this includes 'commit' object, i.e. submodule support)
3402 print "<td class=\"list\">" .
3403 esc_path
($t->{'name'}) .
3405 print "<td class=\"link\">";
3406 if (defined $hash_base) {
3407 print $cgi->a({-href
=> href
(action
=>"history",
3408 hash_base
=>$hash_base,
3409 file_name
=>"$basedir$t->{'name'}")},
3416 ## ......................................................................
3417 ## functions printing large fragments of HTML
3419 # get pre-image filenames for merge (combined) diff
3420 sub fill_from_file_info
{
3421 my ($diff, @parents) = @_;
3423 $diff->{'from_file'} = [ ];
3424 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3425 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3426 if ($diff->{'status'}[$i] eq 'R' ||
3427 $diff->{'status'}[$i] eq 'C') {
3428 $diff->{'from_file'}[$i] =
3429 git_get_path_by_hash
($parents[$i], $diff->{'from_id'}[$i]);
3436 # is current raw difftree line of file deletion
3438 my $diffinfo = shift;
3440 return $diffinfo->{'to_id'} eq ('0' x
40);
3443 # does patch correspond to [previous] difftree raw line
3444 # $diffinfo - hashref of parsed raw diff format
3445 # $patchinfo - hashref of parsed patch diff format
3446 # (the same keys as in $diffinfo)
3447 sub is_patch_split
{
3448 my ($diffinfo, $patchinfo) = @_;
3450 return defined $diffinfo && defined $patchinfo
3451 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3455 sub git_difftree_body
{
3456 my ($difftree, $hash, @parents) = @_;
3457 my ($parent) = $parents[0];
3458 my ($have_blame) = gitweb_check_feature
('blame');
3459 print "<div class=\"list_head\">\n";
3460 if ($#{$difftree} > 10) {
3461 print(($#{$difftree} + 1) . " files changed:\n");
3465 print "<table class=\"" .
3466 (@parents > 1 ? "combined " : "") .
3469 # header only for combined diff in 'commitdiff' view
3470 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3473 print "<thead><tr>\n" .
3474 "<th></th><th></th>\n"; # filename, patchN link
3475 for (my $i = 0; $i < @parents; $i++) {
3476 my $par = $parents[$i];
3478 $cgi->a({-href
=> href
(action
=>"commitdiff",
3479 hash
=>$hash, hash_parent
=>$par),
3480 -title
=> 'commitdiff to parent number ' .
3481 ($i+1) . ': ' . substr($par,0,7)},
3485 print "</tr></thead>\n<tbody>\n";
3490 foreach my $line (@{$difftree}) {
3491 my $diff = parsed_difftree_line
($line);
3494 print "<tr class=\"dark\">\n";
3496 print "<tr class=\"light\">\n";
3500 if (exists $diff->{'nparents'}) { # combined diff
3502 fill_from_file_info
($diff, @parents)
3503 unless exists $diff->{'from_file'};
3505 if (!is_deleted
($diff)) {
3506 # file exists in the result (child) commit
3508 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
3509 file_name
=>$diff->{'to_file'},
3511 -class => "list"}, esc_path
($diff->{'to_file'})) .
3515 esc_path
($diff->{'to_file'}) .
3519 if ($action eq 'commitdiff') {
3522 print "<td class=\"link\">" .
3523 $cgi->a({-href
=> "#patch$patchno"}, "patch") .
3528 my $has_history = 0;
3529 my $not_deleted = 0;
3530 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3531 my $hash_parent = $parents[$i];
3532 my $from_hash = $diff->{'from_id'}[$i];
3533 my $from_path = $diff->{'from_file'}[$i];
3534 my $status = $diff->{'status'}[$i];
3536 $has_history ||= ($status ne 'A');
3537 $not_deleted ||= ($status ne 'D');
3539 if ($status eq 'A') {
3540 print "<td class=\"link\" align=\"right\"> | </td>\n";
3541 } elsif ($status eq 'D') {
3542 print "<td class=\"link\">" .
3543 $cgi->a({-href
=> href
(action
=>"blob",
3546 file_name
=>$from_path)},
3550 if ($diff->{'to_id'} eq $from_hash) {
3551 print "<td class=\"link nochange\">";
3553 print "<td class=\"link\">";
3555 print $cgi->a({-href
=> href
(action
=>"blobdiff",
3556 hash
=>$diff->{'to_id'},
3557 hash_parent
=>$from_hash,
3559 hash_parent_base
=>$hash_parent,
3560 file_name
=>$diff->{'to_file'},
3561 file_parent
=>$from_path)},
3567 print "<td class=\"link\">";
3569 print $cgi->a({-href
=> href
(action
=>"blob",
3570 hash
=>$diff->{'to_id'},
3571 file_name
=>$diff->{'to_file'},
3574 print " | " if ($has_history);
3577 print $cgi->a({-href
=> href
(action
=>"history",
3578 file_name
=>$diff->{'to_file'},
3585 next; # instead of 'else' clause, to avoid extra indent
3587 # else ordinary diff
3589 my ($to_mode_oct, $to_mode_str, $to_file_type);
3590 my ($from_mode_oct, $from_mode_str, $from_file_type);
3591 if ($diff->{'to_mode'} ne ('0' x
6)) {
3592 $to_mode_oct = oct $diff->{'to_mode'};
3593 if (S_ISREG
($to_mode_oct)) { # only for regular file
3594 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3596 $to_file_type = file_type
($diff->{'to_mode'});
3598 if ($diff->{'from_mode'} ne ('0' x
6)) {
3599 $from_mode_oct = oct $diff->{'from_mode'};
3600 if (S_ISREG
($to_mode_oct)) { # only for regular file
3601 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3603 $from_file_type = file_type
($diff->{'from_mode'});
3606 if ($diff->{'status'} eq "A") { # created
3607 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3608 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3609 $mode_chng .= "]</span>";
3611 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
3612 hash_base
=>$hash, file_name
=>$diff->{'file'}),
3613 -class => "list"}, esc_path
($diff->{'file'}));
3615 print "<td>$mode_chng</td>\n";
3616 print "<td class=\"link\">";
3617 if ($action eq 'commitdiff') {
3620 print $cgi->a({-href
=> "#patch$patchno"}, "patch");
3623 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
3624 hash_base
=>$hash, file_name
=>$diff->{'file'})},
3628 } elsif ($diff->{'status'} eq "D") { # deleted
3629 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3631 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
3632 hash_base
=>$parent, file_name
=>$diff->{'file'}),
3633 -class => "list"}, esc_path
($diff->{'file'}));
3635 print "<td>$mode_chng</td>\n";
3636 print "<td class=\"link\">";
3637 if ($action eq 'commitdiff') {
3640 print $cgi->a({-href
=> "#patch$patchno"}, "patch");
3643 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'from_id'},
3644 hash_base
=>$parent, file_name
=>$diff->{'file'})},
3647 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$parent,
3648 file_name
=>$diff->{'file'})},
3651 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$parent,
3652 file_name
=>$diff->{'file'})},
3656 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3657 my $mode_chnge = "";
3658 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3659 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3660 if ($from_file_type ne $to_file_type) {
3661 $mode_chnge .= " from $from_file_type to $to_file_type";
3663 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3664 if ($from_mode_str && $to_mode_str) {
3665 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3666 } elsif ($to_mode_str) {
3667 $mode_chnge .= " mode: $to_mode_str";
3670 $mode_chnge .= "]</span>\n";
3673 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
3674 hash_base
=>$hash, file_name
=>$diff->{'file'}),
3675 -class => "list"}, esc_path
($diff->{'file'}));
3677 print "<td>$mode_chnge</td>\n";
3678 print "<td class=\"link\">";
3679 if ($action eq 'commitdiff') {
3682 print $cgi->a({-href
=> "#patch$patchno"}, "patch") .
3684 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3685 # "commit" view and modified file (not onlu mode changed)
3686 print $cgi->a({-href
=> href
(action
=>"blobdiff",
3687 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
3688 hash_base
=>$hash, hash_parent_base
=>$parent,
3689 file_name
=>$diff->{'file'})},
3693 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
3694 hash_base
=>$hash, file_name
=>$diff->{'file'})},
3697 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
3698 file_name
=>$diff->{'file'})},
3701 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
3702 file_name
=>$diff->{'file'})},
3706 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3707 my %status_name = ('R' => 'moved', 'C' => 'copied');
3708 my $nstatus = $status_name{$diff->{'status'}};
3710 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3711 # mode also for directories, so we cannot use $to_mode_str
3712 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3715 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$hash,
3716 hash
=>$diff->{'to_id'}, file_name
=>$diff->{'to_file'}),
3717 -class => "list"}, esc_path
($diff->{'to_file'})) . "</td>\n" .
3718 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3719 $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$parent,
3720 hash
=>$diff->{'from_id'}, file_name
=>$diff->{'from_file'}),
3721 -class => "list"}, esc_path
($diff->{'from_file'})) .
3722 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3723 "<td class=\"link\">";
3724 if ($action eq 'commitdiff') {
3727 print $cgi->a({-href
=> "#patch$patchno"}, "patch") .
3729 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3730 # "commit" view and modified file (not only pure rename or copy)
3731 print $cgi->a({-href
=> href
(action
=>"blobdiff",
3732 hash
=>$diff->{'to_id'}, hash_parent
=>$diff->{'from_id'},
3733 hash_base
=>$hash, hash_parent_base
=>$parent,
3734 file_name
=>$diff->{'to_file'}, file_parent
=>$diff->{'from_file'})},
3738 print $cgi->a({-href
=> href
(action
=>"blob", hash
=>$diff->{'to_id'},
3739 hash_base
=>$parent, file_name
=>$diff->{'to_file'})},
3742 print $cgi->a({-href
=> href
(action
=>"blame", hash_base
=>$hash,
3743 file_name
=>$diff->{'to_file'})},
3746 print $cgi->a({-href
=> href
(action
=>"history", hash_base
=>$hash,
3747 file_name
=>$diff->{'to_file'})},
3751 } # we should not encounter Unmerged (U) or Unknown (X) status
3754 print "</tbody>" if $has_header;
3758 sub git_patchset_body
{
3759 my ($fd, $difftree, $hash, @hash_parents) = @_;
3760 my ($hash_parent) = $hash_parents[0];
3762 my $is_combined = (@hash_parents > 1);
3764 my $patch_number = 0;
3770 print "<div class=\"patchset\">\n";
3772 # skip to first patch
3773 while ($patch_line = <$fd>) {
3776 last if ($patch_line =~ m/^diff /);
3780 while ($patch_line) {
3782 # parse "git diff" header line
3783 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3784 # $1 is from_name, which we do not use
3785 $to_name = unquote
($2);
3786 $to_name =~ s!^b/!!;
3787 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3788 # $1 is 'cc' or 'combined', which we do not use
3789 $to_name = unquote
($2);
3794 # check if current patch belong to current raw line
3795 # and parse raw git-diff line if needed
3796 if (is_patch_split
($diffinfo, { 'to_file' => $to_name })) {
3797 # this is continuation of a split patch
3798 print "<div class=\"patch cont\">\n";
3800 # advance raw git-diff output if needed
3801 $patch_idx++ if defined $diffinfo;
3803 # read and prepare patch information
3804 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
3806 # compact combined diff output can have some patches skipped
3807 # find which patch (using pathname of result) we are at now;
3809 while ($to_name ne $diffinfo->{'to_file'}) {
3810 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3811 format_diff_cc_simplified
($diffinfo, @hash_parents) .
3812 "</div>\n"; # class="patch"
3817 last if $patch_idx > $#$difftree;
3818 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
3822 # modifies %from, %to hashes
3823 parse_from_to_diffinfo
($diffinfo, \
%from, \
%to, @hash_parents);
3825 # this is first patch for raw difftree line with $patch_idx index
3826 # we index @$difftree array from 0, but number patches from 1
3827 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3831 #assert($patch_line =~ m/^diff /) if DEBUG;
3832 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3834 # print "git diff" header
3835 print format_git_diff_header_line
($patch_line, $diffinfo,
3838 # print extended diff header
3839 print "<div class=\"diff extended_header\">\n";
3841 while ($patch_line = <$fd>) {
3844 last EXTENDED_HEADER
if ($patch_line =~ m/^--- |^diff /);
3846 print format_extended_diff_header_line
($patch_line, $diffinfo,
3849 print "</div>\n"; # class="diff extended_header"
3851 # from-file/to-file diff header
3852 if (! $patch_line) {
3853 print "</div>\n"; # class="patch"
3856 next PATCH
if ($patch_line =~ m/^diff /);
3857 #assert($patch_line =~ m/^---/) if DEBUG;
3859 my $last_patch_line = $patch_line;
3860 $patch_line = <$fd>;
3862 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3864 print format_diff_from_to_header
($last_patch_line, $patch_line,
3865 $diffinfo, \
%from, \
%to,
3870 while ($patch_line = <$fd>) {
3873 next PATCH
if ($patch_line =~ m/^diff /);
3875 print format_diff_line
($patch_line, \
%from, \
%to);
3879 print "</div>\n"; # class="patch"
3882 # for compact combined (--cc) format, with chunk and patch simpliciaction
3883 # patchset might be empty, but there might be unprocessed raw lines
3884 for (++$patch_idx if $patch_number > 0;
3885 $patch_idx < @$difftree;
3887 # read and prepare patch information
3888 $diffinfo = parsed_difftree_line
($difftree->[$patch_idx]);
3890 # generate anchor for "patch" links in difftree / whatchanged part
3891 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3892 format_diff_cc_simplified
($diffinfo, @hash_parents) .
3893 "</div>\n"; # class="patch"
3898 if ($patch_number == 0) {
3899 if (@hash_parents > 1) {
3900 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3902 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3906 print "</div>\n"; # class="patchset"
3909 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3911 # fills project list info (age, description, owner, forks) for each
3912 # project in the list, removing invalid projects from returned list
3913 # NOTE: modifies $projlist, but does not remove entries from it
3914 sub fill_project_list_info
{
3915 my ($projlist, $check_forks) = @_;
3918 my $show_ctags = gitweb_check_feature
('ctags');
3920 foreach my $pr (@$projlist) {
3921 my (@activity) = git_get_last_activity
($pr->{'path'});
3922 unless (@activity) {
3925 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3926 if (!defined $pr->{'descr'}) {
3927 my $descr = git_get_project_description
($pr->{'path'}) || "";
3928 $descr = to_utf8
($descr);
3929 $pr->{'descr_long'} = $descr;
3930 $pr->{'descr'} = chop_str
($descr, $projects_list_description_width, 5);
3932 if (!defined $pr->{'owner'}) {
3933 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}") || "";
3936 my $pname = $pr->{'path'};
3937 if (($pname =~ s/\.git$//) &&
3938 ($pname !~ /\/$/) &&
3939 (-d
"$projectroot/$pname")) {
3940 $pr->{'forks'} = "-d $projectroot/$pname";
3945 $show_ctags and $pr->{'ctags'} = git_get_project_ctags
($pr->{'path'});
3946 push @projects, $pr;
3952 # print 'sort by' <th> element, generating 'sort by $name' replay link
3953 # if that order is not selected
3955 my ($name, $order, $header) = @_;
3956 $header ||= ucfirst($name);
3958 if ($order eq $name) {
3959 print "<th>$header</th>\n";
3962 $cgi->a({-href
=> href
(-replay
=>1, order
=>$name),
3963 -class => "header"}, $header) .
3968 sub git_project_list_body
{
3969 # actually uses global variable $project
3970 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3972 my ($check_forks) = gitweb_check_feature
('forks');
3973 my @projects = fill_project_list_info
($projlist, $check_forks);
3975 $order ||= $default_projects_order;
3976 $from = 0 unless defined $from;
3977 $to = $#projects if (!defined $to || $#projects < $to);
3980 project
=> { key
=> 'path', type
=> 'str' },
3981 descr
=> { key
=> 'descr_long', type
=> 'str' },
3982 owner
=> { key
=> 'owner', type
=> 'str' },
3983 age
=> { key
=> 'age', type
=> 'num' }
3985 my $oi = $order_info{$order};
3986 if ($oi->{'type'} eq 'str') {
3987 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
3989 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
3992 my $show_ctags = gitweb_check_feature
('ctags');
3995 foreach my $p (@projects) {
3996 foreach my $ct (keys %{$p->{'ctags'}}) {
3997 $ctags{$ct} += $p->{'ctags'}->{$ct};
4000 my $cloud = git_populate_project_tagcloud
(\
%ctags);
4001 print git_show_project_tagcloud
($cloud, 64);
4004 print "<table class=\"project_list\">\n";
4005 unless ($no_header) {
4008 print "<th></th>\n";
4010 print_sort_th
('project', $order, 'Project');
4011 print_sort_th
('descr', $order, 'Description');
4012 print_sort_th
('owner', $order, 'Owner');
4013 print_sort_th
('age', $order, 'Last Change');
4014 print "<th></th>\n" . # for links
4018 my $tagfilter = $cgi->param('by_tag');
4019 for (my $i = $from; $i <= $to; $i++) {
4020 my $pr = $projects[$i];
4022 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4023 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4024 and not $pr->{'descr_long'} =~ /$searchtext/;
4025 # Weed out forks or non-matching entries of search
4027 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s
#\.git$#/#;
4028 $forkbase="^$forkbase" if $forkbase;
4029 next if not $searchtext and not $tagfilter and $show_ctags
4030 and $pr->{'path'} =~ m
#$forkbase.*/.*#; # regexp-safe
4034 print "<tr class=\"dark\">\n";
4036 print "<tr class=\"light\">\n";
4041 if ($pr->{'forks'}) {
4042 print "<!-- $pr->{'forks'} -->\n";
4043 print $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks")}, "+");
4047 print "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
4048 -class => "list"}, esc_html
($pr->{'path'})) . "</td>\n" .
4049 "<td>" . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary"),
4050 -class => "list", -title
=> $pr->{'descr_long'}},
4051 esc_html
($pr->{'descr'})) . "</td>\n" .
4052 "<td><i>" . chop_and_escape_str
($pr->{'owner'}, 15) . "</i></td>\n";
4053 print "<td class=\"". age_class
($pr->{'age'}) . "\">" .
4054 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4055 "<td class=\"link\">" .
4056 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"summary")}, "summary") . " | " .
4057 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"shortlog")}, "shortlog") . " | " .
4058 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"log")}, "log") . " | " .
4059 $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"tree")}, "tree") .
4060 ($pr->{'forks'} ? " | " . $cgi->a({-href
=> href
(project
=>$pr->{'path'}, action
=>"forks")}, "forks") : '') .
4064 if (defined $extra) {
4067 print "<td></td>\n";
4069 print "<td colspan=\"5\">$extra</td>\n" .
4075 sub git_shortlog_body
{
4076 # uses global variable $project
4077 my ($commitlist, $from, $to, $refs, $extra) = @_;
4079 $from = 0 unless defined $from;
4080 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4082 print "<table class=\"shortlog\">\n";
4084 for (my $i = $from; $i <= $to; $i++) {
4085 my %co = %{$commitlist->[$i]};
4086 my $commit = $co{'id'};
4087 my $ref = format_ref_marker
($refs, $commit);
4089 print "<tr class=\"dark\">\n";
4091 print "<tr class=\"light\">\n";
4094 my $author = chop_and_escape_str
($co{'author_name'}, 10);
4095 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4096 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4097 "<td><i>" . $author . "</i></td>\n" .
4099 print format_subject_html
($co{'title'}, $co{'title_short'},
4100 href
(action
=>"commit", hash
=>$commit), $ref);
4102 "<td class=\"link\">" .
4103 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") . " | " .
4104 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") . " | " .
4105 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree");
4106 my $snapshot_links = format_snapshot_links
($commit);
4107 if (defined $snapshot_links) {
4108 print " | " . $snapshot_links;
4113 if (defined $extra) {
4115 "<td colspan=\"4\">$extra</td>\n" .
4121 sub git_history_body
{
4122 # Warning: assumes constant type (blob or tree) during history
4123 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
4125 $from = 0 unless defined $from;
4126 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4128 print "<table class=\"history\">\n";
4130 for (my $i = $from; $i <= $to; $i++) {
4131 my %co = %{$commitlist->[$i]};
4135 my $commit = $co{'id'};
4137 my $ref = format_ref_marker
($refs, $commit);
4140 print "<tr class=\"dark\">\n";
4142 print "<tr class=\"light\">\n";
4145 # shortlog uses chop_str($co{'author_name'}, 10)
4146 my $author = chop_and_escape_str
($co{'author_name'}, 15, 3);
4147 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4148 "<td><i>" . $author . "</i></td>\n" .
4150 # originally git_history used chop_str($co{'title'}, 50)
4151 print format_subject_html
($co{'title'}, $co{'title_short'},
4152 href
(action
=>"commit", hash
=>$commit), $ref);
4154 "<td class=\"link\">" .
4155 $cgi->a({-href
=> href
(action
=>$ftype, hash_base
=>$commit, file_name
=>$file_name)}, $ftype) . " | " .
4156 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff");
4158 if ($ftype eq 'blob') {
4159 my $blob_current = git_get_hash_by_path
($hash_base, $file_name);
4160 my $blob_parent = git_get_hash_by_path
($commit, $file_name);
4161 if (defined $blob_current && defined $blob_parent &&
4162 $blob_current ne $blob_parent) {
4164 $cgi->a({-href
=> href
(action
=>"blobdiff",
4165 hash
=>$blob_current, hash_parent
=>$blob_parent,
4166 hash_base
=>$hash_base, hash_parent_base
=>$commit,
4167 file_name
=>$file_name)},
4174 if (defined $extra) {
4176 "<td colspan=\"4\">$extra</td>\n" .
4183 # uses global variable $project
4184 my ($taglist, $from, $to, $extra) = @_;
4185 $from = 0 unless defined $from;
4186 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4188 print "<table class=\"tags\">\n";
4190 for (my $i = $from; $i <= $to; $i++) {
4191 my $entry = $taglist->[$i];
4193 my $comment = $tag{'subject'};
4195 if (defined $comment) {
4196 $comment_short = chop_str
($comment, 30, 5);
4199 print "<tr class=\"dark\">\n";
4201 print "<tr class=\"light\">\n";
4204 if (defined $tag{'age'}) {
4205 print "<td><i>$tag{'age'}</i></td>\n";
4207 print "<td></td>\n";
4210 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'}),
4211 -class => "list name"}, esc_html
($tag{'name'})) .
4214 if (defined $comment) {
4215 print format_subject_html
($comment, $comment_short,
4216 href
(action
=>"tag", hash
=>$tag{'id'}));
4219 "<td class=\"selflink\">";
4220 if ($tag{'type'} eq "tag") {
4221 print $cgi->a({-href
=> href
(action
=>"tag", hash
=>$tag{'id'})}, "tag");
4226 "<td class=\"link\">" . " | " .
4227 $cgi->a({-href
=> href
(action
=>$tag{'reftype'}, hash
=>$tag{'refid'})}, $tag{'reftype'});
4228 if ($tag{'reftype'} eq "commit") {
4229 print " | " . $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$tag{'fullname'})}, "shortlog") .
4230 " | " . $cgi->a({-href
=> href
(action
=>"log", hash
=>$tag{'fullname'})}, "log");
4231 } elsif ($tag{'reftype'} eq "blob") {
4232 print " | " . $cgi->a({-href
=> href
(action
=>"blob_plain", hash
=>$tag{'refid'})}, "raw");
4237 if (defined $extra) {
4239 "<td colspan=\"5\">$extra</td>\n" .
4245 sub git_heads_body
{
4246 # uses global variable $project
4247 my ($headlist, $head, $from, $to, $extra) = @_;
4248 $from = 0 unless defined $from;
4249 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4251 print "<table class=\"heads\">\n";
4253 for (my $i = $from; $i <= $to; $i++) {
4254 my $entry = $headlist->[$i];
4256 my $curr = $ref{'id'} eq $head;
4258 print "<tr class=\"dark\">\n";
4260 print "<tr class=\"light\">\n";
4263 print "<td><i>$ref{'age'}</i></td>\n" .
4264 ($curr ? "<td class=\"current_head\">" : "<td>") .
4265 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'}),
4266 -class => "list name"},esc_html
($ref{'name'})) .
4268 "<td class=\"link\">" .
4269 $cgi->a({-href
=> href
(action
=>"shortlog", hash
=>$ref{'fullname'})}, "shortlog") . " | " .
4270 $cgi->a({-href
=> href
(action
=>"log", hash
=>$ref{'fullname'})}, "log") . " | " .
4271 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$ref{'fullname'}, hash_base
=>$ref{'name'})}, "tree") .
4275 if (defined $extra) {
4277 "<td colspan=\"3\">$extra</td>\n" .
4283 sub git_search_grep_body
{
4284 my ($commitlist, $from, $to, $extra) = @_;
4285 $from = 0 unless defined $from;
4286 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4288 print "<table class=\"commit_search\">\n";
4290 for (my $i = $from; $i <= $to; $i++) {
4291 my %co = %{$commitlist->[$i]};
4295 my $commit = $co{'id'};
4297 print "<tr class=\"dark\">\n";
4299 print "<tr class=\"light\">\n";
4302 my $author = chop_and_escape_str
($co{'author_name'}, 15, 5);
4303 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4304 "<td><i>" . $author . "</i></td>\n" .
4306 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
4307 -class => "list subject"},
4308 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
4309 my $comment = $co{'comment'};
4310 foreach my $line (@$comment) {
4311 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4312 my ($lead, $match, $trail) = ($1, $2, $3);
4313 $match = chop_str
($match, 70, 5, 'center');
4314 my $contextlen = int((80 - length($match))/2);
4315 $contextlen = 30 if ($contextlen > 30);
4316 $lead = chop_str
($lead, $contextlen, 10, 'left');
4317 $trail = chop_str
($trail, $contextlen, 10, 'right');
4319 $lead = esc_html
($lead);
4320 $match = esc_html
($match);
4321 $trail = esc_html
($trail);
4323 print "$lead<span class=\"match\">$match</span>$trail<br />";
4327 "<td class=\"link\">" .
4328 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
4330 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$co{'id'})}, "commitdiff") .
4332 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
4336 if (defined $extra) {
4338 "<td colspan=\"3\">$extra</td>\n" .
4344 ## ======================================================================
4345 ## ======================================================================
4348 sub git_project_list
{
4349 my $order = $input_params{'order'};
4350 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4351 die_error
(400, "Unknown order parameter");
4354 my @list = git_get_projects_list
();
4356 die_error
(404, "No projects found");
4360 if (-f
$home_text) {
4361 print "<div class=\"index_include\">\n";
4362 open (my $fd, $home_text);
4367 print $cgi->startform(-method => "get") .
4368 "<p class=\"projsearch\">Search:\n" .
4369 $cgi->textfield(-name
=> "s", -value
=> $searchtext) . "\n" .
4371 $cgi->end_form() . "\n";
4372 git_project_list_body
(\
@list, $order);
4377 my $order = $input_params{'order'};
4378 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4379 die_error
(400, "Unknown order parameter");
4382 my @list = git_get_projects_list
($project);
4384 die_error
(404, "No forks found");
4388 git_print_page_nav
('','');
4389 git_print_header_div
('summary', "$project forks");
4390 git_project_list_body
(\
@list, $order);
4394 sub git_project_index
{
4395 my @projects = git_get_projects_list
($project);
4398 -type
=> 'text/plain',
4399 -charset
=> 'utf-8',
4400 -content_disposition
=> 'inline; filename="index.aux"');
4402 foreach my $pr (@projects) {
4403 if (!exists $pr->{'owner'}) {
4404 $pr->{'owner'} = git_get_project_owner
("$pr->{'path'}");
4407 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4408 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4409 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
4410 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf
("%%%02X", ord($1))/eg
;
4414 print "$path $owner\n";
4419 my $descr = git_get_project_description
($project) || "none";
4420 my %co = parse_commit
("HEAD");
4421 my %cd = %co ? parse_date
($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4422 my $head = $co{'id'};
4424 my $owner = git_get_project_owner
($project);
4426 my $refs = git_get_references
();
4427 # These get_*_list functions return one more to allow us to see if
4428 # there are more ...
4429 my @taglist = git_get_tags_list
(16);
4430 my @headlist = git_get_heads_list
(16);
4432 my ($check_forks) = gitweb_check_feature
('forks');
4435 @forklist = git_get_projects_list
($project);
4439 git_print_page_nav
('summary','', $head);
4441 print "<div class=\"title\"> </div>\n";
4442 print "<table class=\"projects_list\">\n" .
4443 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html
($descr) . "</td></tr>\n" .
4444 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html
($owner) . "</td></tr>\n";
4445 if (defined $cd{'rfc2822'}) {
4446 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4449 # use per project git URL list in $projectroot/$project/cloneurl
4450 # or make project git URL from git base URL and project name
4451 my $url_tag = "URL";
4452 my @url_list = git_get_project_url_list
($project);
4453 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4454 foreach my $git_url (@url_list) {
4455 next unless $git_url;
4456 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4461 my $show_ctags = (gitweb_check_feature
('ctags'))[0];
4463 my $ctags = git_get_project_ctags
($project);
4464 my $cloud = git_populate_project_tagcloud
($ctags);
4465 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4466 print "</td>\n<td>" unless %$ctags;
4467 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4468 print "</td>\n<td>" if %$ctags;
4469 print git_show_project_tagcloud
($cloud, 48);
4475 if (-s
"$projectroot/$project/README.html") {
4476 if (open my $fd, "$projectroot/$project/README.html") {
4477 print "<div class=\"title\">readme</div>\n" .
4478 "<div class=\"readme\">\n";
4479 print $_ while (<$fd>);
4480 print "\n</div>\n"; # class="readme"
4485 # we need to request one more than 16 (0..15) to check if
4487 my @commitlist = $head ? parse_commits
($head, 17) : ();
4489 git_print_header_div
('shortlog');
4490 git_shortlog_body
(\
@commitlist, 0, 15, $refs,
4491 $#commitlist <= 15 ? undef :
4492 $cgi->a({-href
=> href
(action
=>"shortlog")}, "..."));
4496 git_print_header_div
('tags');
4497 git_tags_body
(\
@taglist, 0, 15,
4498 $#taglist <= 15 ? undef :
4499 $cgi->a({-href
=> href
(action
=>"tags")}, "..."));
4503 git_print_header_div
('heads');
4504 git_heads_body
(\
@headlist, $head, 0, 15,
4505 $#headlist <= 15 ? undef :
4506 $cgi->a({-href
=> href
(action
=>"heads")}, "..."));
4510 git_print_header_div
('forks');
4511 git_project_list_body
(\
@forklist, 'age', 0, 15,
4512 $#forklist <= 15 ? undef :
4513 $cgi->a({-href
=> href
(action
=>"forks")}, "..."),
4521 my $head = git_get_head_hash
($project);
4523 git_print_page_nav
('','', $head,undef,$head);
4524 my %tag = parse_tag
($hash);
4527 die_error
(404, "Unknown tag object");
4530 git_print_header_div
('commit', esc_html
($tag{'name'}), $hash);
4531 print "<div class=\"title_text\">\n" .
4532 "<table class=\"object_header\">\n" .
4534 "<td>object</td>\n" .
4535 "<td>" . $cgi->a({-class => "list", -href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
4536 $tag{'object'}) . "</td>\n" .
4537 "<td class=\"link\">" . $cgi->a({-href
=> href
(action
=>$tag{'type'}, hash
=>$tag{'object'})},
4538 $tag{'type'}) . "</td>\n" .
4540 if (defined($tag{'author'})) {
4541 my %ad = parse_date
($tag{'epoch'}, $tag{'tz'});
4542 print "<tr><td>author</td><td>" . esc_html
($tag{'author'}) . "</td></tr>\n";
4543 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4544 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4547 print "</table>\n\n" .
4549 print "<div class=\"page_body\">";
4550 my $comment = $tag{'comment'};
4551 foreach my $line (@$comment) {
4553 print esc_html
($line, -nbsp
=>1) . "<br/>\n";
4563 gitweb_check_feature
('blame')
4564 or die_error
(403, "Blame view not allowed");
4566 die_error
(400, "No file name given") unless $file_name;
4567 $hash_base ||= git_get_head_hash
($project);
4568 die_error
(404, "Couldn't find base commit") unless ($hash_base);
4569 my %co = parse_commit
($hash_base)
4570 or die_error
(404, "Commit not found");
4571 if (!defined $hash) {
4572 $hash = git_get_hash_by_path
($hash_base, $file_name, "blob")
4573 or die_error
(404, "Error looking up file");
4575 $ftype = git_get_type
($hash);
4576 if ($ftype !~ "blob") {
4577 die_error
(400, "Object is not a blob");
4579 open ($fd, "-|", git_cmd
(), "blame", '-p', '--',
4580 $file_name, $hash_base)
4581 or die_error
(500, "Open git-blame failed");
4584 $cgi->a({-href
=> href
(action
=>"blob", -replay
=>1)},
4587 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
4590 $cgi->a({-href
=> href
(action
=>"blame", file_name
=>$file_name)},
4592 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4593 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
4594 git_print_page_path
($file_name, $ftype, $hash_base);
4595 my @rev_color = (qw(light2 dark2));
4596 my $num_colors = scalar(@rev_color);
4597 my $current_color = 0;
4600 <div class="page_body">
4601 <table class="blame">
4602 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4607 last unless defined $_;
4608 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4609 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4610 if (!exists $metainfo{$full_rev}) {
4611 $metainfo{$full_rev} = {};
4613 my $meta = $metainfo{$full_rev};
4616 if (/^(\S+) (.*)$/) {
4622 my $rev = substr($full_rev, 0, 8);
4623 my $author = $meta->{'author'};
4624 my %date = parse_date
($meta->{'author-time'},
4625 $meta->{'author-tz'});
4626 my $date = $date{'iso-tz'};
4628 $current_color = ++$current_color % $num_colors;
4630 print "<tr class=\"$rev_color[$current_color]\">\n";
4632 print "<td class=\"sha1\"";
4633 print " title=\"". esc_html
($author) . ", $date\"";
4634 print " rowspan=\"$group_size\"" if ($group_size > 1);
4636 print $cgi->a({-href
=> href
(action
=>"commit",
4638 file_name
=>$file_name)},
4642 open (my $dd, "-|", git_cmd
(), "rev-parse", "$full_rev^")
4643 or die_error
(500, "Open git-rev-parse failed");
4644 my $parent_commit = <$dd>;
4646 chomp($parent_commit);
4647 my $blamed = href
(action
=> 'blame',
4648 file_name
=> $meta->{'filename'},
4649 hash_base
=> $parent_commit);
4650 print "<td class=\"linenr\">";
4651 print $cgi->a({ -href
=> "$blamed#l$orig_lineno",
4653 -class => "linenr" },
4656 print "<td class=\"pre\">" . esc_html
($data) . "</td>\n";
4662 or print "Reading blob failed\n";
4667 my $head = git_get_head_hash
($project);
4669 git_print_page_nav
('','', $head,undef,$head);
4670 git_print_header_div
('summary', $project);
4672 my @tagslist = git_get_tags_list
();
4674 git_tags_body
(\
@tagslist);
4680 my $head = git_get_head_hash
($project);
4682 git_print_page_nav
('','', $head,undef,$head);
4683 git_print_header_div
('summary', $project);
4685 my @headslist = git_get_heads_list
();
4687 git_heads_body
(\
@headslist, $head);
4692 sub git_blob_plain
{
4696 if (!defined $hash) {
4697 if (defined $file_name) {
4698 my $base = $hash_base || git_get_head_hash
($project);
4699 $hash = git_get_hash_by_path
($base, $file_name, "blob")
4700 or die_error
(404, "Cannot find file");
4702 die_error
(400, "No file name defined");
4704 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4705 # blobs defined by non-textual hash id's can be cached
4709 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
4710 or die_error
(500, "Open git-cat-file blob '$hash' failed");
4712 # content-type (can include charset)
4713 $type = blob_contenttype
($fd, $file_name, $type);
4715 # "save as" filename, even when no $file_name is given
4716 my $save_as = "$hash";
4717 if (defined $file_name) {
4718 $save_as = $file_name;
4719 } elsif ($type =~ m/^text\//) {
4725 -expires
=> $expires,
4726 -content_disposition
=> 'inline; filename="' . $save_as . '"');
4728 binmode STDOUT
, ':raw';
4730 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
4738 if (!defined $hash) {
4739 if (defined $file_name) {
4740 my $base = $hash_base || git_get_head_hash
($project);
4741 $hash = git_get_hash_by_path
($base, $file_name, "blob")
4742 or die_error
(404, "Cannot find file");
4744 die_error
(400, "No file name defined");
4746 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4747 # blobs defined by non-textual hash id's can be cached
4751 my ($have_blame) = gitweb_check_feature
('blame');
4752 open my $fd, "-|", git_cmd
(), "cat-file", "blob", $hash
4753 or die_error
(500, "Couldn't cat $file_name, $hash");
4754 my $mimetype = blob_mimetype
($fd, $file_name);
4755 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B
$fd) {
4757 return git_blob_plain
($mimetype);
4759 # we can have blame only for text/* mimetype
4760 $have_blame &&= ($mimetype =~ m!^text/!);
4762 git_header_html
(undef, $expires);
4763 my $formats_nav = '';
4764 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
4765 if (defined $file_name) {
4768 $cgi->a({-href
=> href
(action
=>"blame", -replay
=>1)},
4773 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
4776 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
4779 $cgi->a({-href
=> href
(action
=>"blob",
4780 hash_base
=>"HEAD", file_name
=>$file_name)},
4784 $cgi->a({-href
=> href
(action
=>"blob_plain", -replay
=>1)},
4787 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4788 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
4790 print "<div class=\"page_nav\">\n" .
4791 "<br/><br/></div>\n" .
4792 "<div class=\"title\">$hash</div>\n";
4794 git_print_page_path
($file_name, "blob", $hash_base);
4795 print "<div class=\"page_body\">\n";
4796 if ($mimetype =~ m!^image/!) {
4797 print qq
!<img type
="$mimetype"!;
4799 print qq
! alt
="$file_name" title
="$file_name"!;
4802 href(action=>"blob_plain
", hash=>$hash,
4803 hash_base=>$hash_base, file_name=>$file_name) .
4807 while (my $line = <$fd>) {
4810 $line = untabify
($line);
4811 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4812 $nr, $nr, $nr, esc_html
($line, -nbsp
=>1);
4816 or print "Reading blob failed.\n";
4822 if (!defined $hash_base) {
4823 $hash_base = "HEAD";
4825 if (!defined $hash) {
4826 if (defined $file_name) {
4827 $hash = git_get_hash_by_path
($hash_base, $file_name, "tree");
4832 die_error
(404, "No such tree") unless defined($hash);
4834 open my $fd, "-|", git_cmd
(), "ls-tree", '-z', $hash
4835 or die_error
(500, "Open git-ls-tree failed");
4836 my @entries = map { chomp; $_ } <$fd>;
4837 close $fd or die_error
(404, "Reading tree failed");
4840 my $refs = git_get_references
();
4841 my $ref = format_ref_marker
($refs, $hash_base);
4844 my ($have_blame) = gitweb_check_feature
('blame');
4845 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
4847 if (defined $file_name) {
4849 $cgi->a({-href
=> href
(action
=>"history", -replay
=>1)},
4851 $cgi->a({-href
=> href
(action
=>"tree",
4852 hash_base
=>"HEAD", file_name
=>$file_name)},
4855 my $snapshot_links = format_snapshot_links
($hash);
4856 if (defined $snapshot_links) {
4857 # FIXME: Should be available when we have no hash base as well.
4858 push @views_nav, $snapshot_links;
4860 git_print_page_nav
('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4861 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash_base);
4864 print "<div class=\"page_nav\">\n";
4865 print "<br/><br/></div>\n";
4866 print "<div class=\"title\">$hash</div>\n";
4868 if (defined $file_name) {
4869 $basedir = $file_name;
4870 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4873 git_print_page_path
($file_name, 'tree', $hash_base);
4875 print "<div class=\"page_body\">\n";
4876 print "<table class=\"tree\">\n";
4878 # '..' (top directory) link if possible
4879 if (defined $hash_base &&
4880 defined $file_name && $file_name =~ m![^/]+$!) {
4882 print "<tr class=\"dark\">\n";
4884 print "<tr class=\"light\">\n";
4888 my $up = $file_name;
4889 $up =~ s!/?[^/]+$!!;
4890 undef $up unless $up;
4891 # based on git_print_tree_entry
4892 print '<td class="mode">' . mode_str
('040000') . "</td>\n";
4893 print '<td class="list">';
4894 print $cgi->a({-href
=> href
(action
=>"tree", hash_base
=>$hash_base,
4898 print "<td class=\"link\"></td>\n";
4902 foreach my $line (@entries) {
4903 my %t = parse_ls_tree_line
($line, -z
=> 1);
4906 print "<tr class=\"dark\">\n";
4908 print "<tr class=\"light\">\n";
4912 git_print_tree_entry
(\
%t, $basedir, $hash_base, $have_blame);
4916 print "</table>\n" .
4922 my $format = $input_params{'snapshot_format'};
4923 if (!@snapshot_fmts) {
4924 die_error
(403, "Snapshots not allowed");
4926 # default to first supported snapshot format
4927 $format ||= $snapshot_fmts[0];
4928 if ($format !~ m/^[a-z0-9]+$/) {
4929 die_error
(400, "Invalid snapshot format parameter");
4930 } elsif (!exists($known_snapshot_formats{$format})) {
4931 die_error
(400, "Unknown snapshot format");
4932 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
4933 die_error
(403, "Unsupported snapshot format");
4936 if (!defined $hash) {
4937 $hash = git_get_head_hash
($project);
4940 my $name = $project;
4941 $name =~ s
,([^/])/*\
.git
$,$1,;
4942 $name = basename
($name);
4943 my $filename = to_utf8
($name);
4944 $name =~ s/\047/\047\\\047\047/g;
4946 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4947 $cmd = quote_command
(
4948 git_cmd
(), 'archive',
4949 "--format=$known_snapshot_formats{$format}{'format'}",
4950 "--prefix=$name/", $hash);
4951 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4952 $cmd .= ' | ' . quote_command
(@{$known_snapshot_formats{$format}{'compressor'}});
4956 -type
=> $known_snapshot_formats{$format}{'type'},
4957 -content_disposition
=> 'inline; filename="' . "$filename" . '"',
4958 -status
=> '200 OK');
4960 open my $fd, "-|", $cmd
4961 or die_error
(500, "Execute git-archive failed");
4962 binmode STDOUT
, ':raw';
4964 binmode STDOUT
, ':utf8'; # as set at the beginning of gitweb.cgi
4969 my $head = git_get_head_hash
($project);
4970 if (!defined $hash) {
4973 if (!defined $page) {
4976 my $refs = git_get_references
();
4978 my @commitlist = parse_commits
($hash, 101, (100 * $page));
4980 my $paging_nav = format_paging_nav
('log', $hash, $head, $page, $#commitlist >= 100);
4983 git_print_page_nav
('log','', $hash,undef,undef, $paging_nav);
4986 my %co = parse_commit
($hash);
4988 git_print_header_div
('summary', $project);
4989 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4991 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4992 for (my $i = 0; $i <= $to; $i++) {
4993 my %co = %{$commitlist[$i]};
4995 my $commit = $co{'id'};
4996 my $ref = format_ref_marker
($refs, $commit);
4997 my %ad = parse_date
($co{'author_epoch'});
4998 git_print_header_div
('commit',
4999 "<span class=\"age\">$co{'age_string'}</span>" .
5000 esc_html
($co{'title'}) . $ref,
5002 print "<div class=\"title_text\">\n" .
5003 "<div class=\"log_link\">\n" .
5004 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$commit)}, "commit") .
5006 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$commit)}, "commitdiff") .
5008 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$commit, hash_base
=>$commit)}, "tree") .
5011 "<i>" . esc_html
($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
5014 print "<div class=\"log_body\">\n";
5015 git_print_log
($co{'comment'}, -final_empty_line
=> 1);
5018 if ($#commitlist >= 100) {
5019 print "<div class=\"page_nav\">\n";
5020 print $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
5021 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
5028 $hash ||= $hash_base || "HEAD";
5029 my %co = parse_commit
($hash)
5030 or die_error
(404, "Unknown commit object");
5031 my %ad = parse_date
($co{'author_epoch'}, $co{'author_tz'});
5032 my %cd = parse_date
($co{'committer_epoch'}, $co{'committer_tz'});
5034 my $parent = $co{'parent'};
5035 my $parents = $co{'parents'}; # listref
5037 # we need to prepare $formats_nav before any parameter munging
5039 if (!defined $parent) {
5041 $formats_nav .= '(initial)';
5042 } elsif (@$parents == 1) {
5043 # single parent commit
5046 $cgi->a({-href
=> href
(action
=>"commit",
5048 esc_html
(substr($parent, 0, 7))) .
5055 $cgi->a({-href
=> href
(action
=>"commit",
5057 esc_html
(substr($_, 0, 7)));
5062 if (!defined $parent) {
5066 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', "--no-commit-id",
5068 (@$parents <= 1 ? $parent : '-c'),
5070 or die_error
(500, "Open git-diff-tree failed");
5071 @difftree = map { chomp; $_ } <$fd>;
5072 close $fd or die_error
(404, "Reading git-diff-tree failed");
5074 # non-textual hash id's can be cached
5076 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5079 my $refs = git_get_references
();
5080 my $ref = format_ref_marker
($refs, $co{'id'});
5082 git_header_html
(undef, $expires);
5083 git_print_page_nav
('commit', '',
5084 $hash, $co{'tree'}, $hash,
5087 if (defined $co{'parent'}) {
5088 git_print_header_div
('commitdiff', esc_html
($co{'title'}) . $ref, $hash);
5090 git_print_header_div
('tree', esc_html
($co{'title'}) . $ref, $co{'tree'}, $hash);
5092 print "<div class=\"title_text\">\n" .
5093 "<table class=\"object_header\">\n";
5094 print "<tr><td>author</td><td>" . esc_html
($co{'author'}) . "</td></tr>\n".
5096 "<td></td><td> $ad{'rfc2822'}";
5097 if ($ad{'hour_local'} < 6) {
5098 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
5099 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
5101 printf(" (%02d:%02d %s)",
5102 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
5106 print "<tr><td>committer</td><td>" . esc_html
($co{'committer'}) . "</td></tr>\n";
5107 print "<tr><td></td><td> $cd{'rfc2822'}" .
5108 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
5110 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5113 "<td class=\"sha1\">" .
5114 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash),
5115 class => "list"}, $co{'tree'}) .
5117 "<td class=\"link\">" .
5118 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$hash)},
5120 my $snapshot_links = format_snapshot_links
($hash);
5121 if (defined $snapshot_links) {
5122 print " | " . $snapshot_links;
5127 foreach my $par (@$parents) {
5130 "<td class=\"sha1\">" .
5131 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par),
5132 class => "list"}, $par) .
5134 "<td class=\"link\">" .
5135 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$par)}, "commit") .
5137 $cgi->a({-href
=> href
(action
=>"commitdiff", hash
=>$hash, hash_parent
=>$par)}, "diff") .
5144 print "<div class=\"page_body\">\n";
5145 git_print_log
($co{'comment'});
5148 git_difftree_body
(\
@difftree, $hash, @$parents);
5154 # object is defined by:
5155 # - hash or hash_base alone
5156 # - hash_base and file_name
5159 # - hash or hash_base alone
5160 if ($hash || ($hash_base && !defined $file_name)) {
5161 my $object_id = $hash || $hash_base;
5163 open my $fd, "-|", quote_command
(
5164 git_cmd
(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
5165 or die_error
(404, "Object does not exist");
5169 or die_error
(404, "Object does not exist");
5171 # - hash_base and file_name
5172 } elsif ($hash_base && defined $file_name) {
5173 $file_name =~ s
,/+$,,;
5175 system(git_cmd
(), "cat-file", '-e', $hash_base) == 0
5176 or die_error
(404, "Base object does not exist");
5178 # here errors should not hapen
5179 open my $fd, "-|", git_cmd
(), "ls-tree", $hash_base, "--", $file_name
5180 or die_error
(500, "Open git-ls-tree failed");
5184 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5185 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
5186 die_error
(404, "File or directory for given base does not exist");
5191 die_error
(400, "Not enough information to find object");
5194 print $cgi->redirect(-uri
=> href
(action
=>$type, -full
=>1,
5195 hash
=>$hash, hash_base
=>$hash_base,
5196 file_name
=>$file_name),
5197 -status
=> '302 Found');
5201 my $format = shift || 'html';
5208 # preparing $fd and %diffinfo for git_patchset_body
5210 if (defined $hash_base && defined $hash_parent_base) {
5211 if (defined $file_name) {
5213 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
5214 $hash_parent_base, $hash_base,
5215 "--", (defined $file_parent ? $file_parent : ()), $file_name
5216 or die_error
(500, "Open git-diff-tree failed");
5217 @difftree = map { chomp; $_ } <$fd>;
5219 or die_error
(404, "Reading git-diff-tree failed");
5221 or die_error
(404, "Blob diff not found");
5223 } elsif (defined $hash &&
5224 $hash =~ /[0-9a-fA-F]{40}/) {
5225 # try to find filename from $hash
5227 # read filtered raw output
5228 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
5229 $hash_parent_base, $hash_base, "--"
5230 or die_error
(500, "Open git-diff-tree failed");
5232 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5234 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5235 map { chomp; $_ } <$fd>;
5237 or die_error
(404, "Reading git-diff-tree failed");
5239 or die_error
(404, "Blob diff not found");
5242 die_error
(400, "Missing one of the blob diff parameters");
5245 if (@difftree > 1) {
5246 die_error
(400, "Ambiguous blob diff specification");
5249 %diffinfo = parse_difftree_raw_line
($difftree[0]);
5250 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5251 $file_name ||= $diffinfo{'to_file'};
5253 $hash_parent ||= $diffinfo{'from_id'};
5254 $hash ||= $diffinfo{'to_id'};
5256 # non-textual hash id's can be cached
5257 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5258 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5263 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
5264 '-p', ($format eq 'html' ? "--full-index" : ()),
5265 $hash_parent_base, $hash_base,
5266 "--", (defined $file_parent ? $file_parent : ()), $file_name
5267 or die_error
(500, "Open git-diff-tree failed");
5270 # old/legacy style URI
5271 if (!%diffinfo && # if new style URI failed
5272 defined $hash && defined $hash_parent) {
5273 # fake git-diff-tree raw output
5274 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
5275 $diffinfo{'from_id'} = $hash_parent;
5276 $diffinfo{'to_id'} = $hash;
5277 if (defined $file_name) {
5278 if (defined $file_parent) {
5279 $diffinfo{'status'} = '2';
5280 $diffinfo{'from_file'} = $file_parent;
5281 $diffinfo{'to_file'} = $file_name;
5282 } else { # assume not renamed
5283 $diffinfo{'status'} = '1';
5284 $diffinfo{'from_file'} = $file_name;
5285 $diffinfo{'to_file'} = $file_name;
5287 } else { # no filename given
5288 $diffinfo{'status'} = '2';
5289 $diffinfo{'from_file'} = $hash_parent;
5290 $diffinfo{'to_file'} = $hash;
5293 # non-textual hash id's can be cached
5294 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
5295 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5300 open $fd, "-|", git_cmd
(), "diff", @diff_opts,
5301 '-p', ($format eq 'html' ? "--full-index" : ()),
5302 $hash_parent, $hash, "--"
5303 or die_error
(500, "Open git-diff failed");
5305 die_error
(400, "Missing one of the blob diff parameters")
5310 if ($format eq 'html') {
5312 $cgi->a({-href
=> href
(action
=>"blobdiff_plain", -replay
=>1)},
5314 git_header_html
(undef, $expires);
5315 if (defined $hash_base && (my %co = parse_commit
($hash_base))) {
5316 git_print_page_nav
('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5317 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
5319 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5320 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5322 if (defined $file_name) {
5323 git_print_page_path
($file_name, "blob", $hash_base);
5325 print "<div class=\"page_path\"></div>\n";
5328 } elsif ($format eq 'plain') {
5330 -type
=> 'text/plain',
5331 -charset
=> 'utf-8',
5332 -expires
=> $expires,
5333 -content_disposition
=> 'inline; filename="' . "$file_name" . '.patch"');
5335 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5338 die_error
(400, "Unknown blobdiff format");
5342 if ($format eq 'html') {
5343 print "<div class=\"page_body\">\n";
5345 git_patchset_body
($fd, [ \
%diffinfo ], $hash_base, $hash_parent_base);
5348 print "</div>\n"; # class="page_body"
5352 while (my $line = <$fd>) {
5353 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5354 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5358 last if $line =~ m!^\+\+\+!;
5366 sub git_blobdiff_plain
{
5367 git_blobdiff
('plain');
5370 sub git_commitdiff
{
5371 my $format = shift || 'html';
5372 $hash ||= $hash_base || "HEAD";
5373 my %co = parse_commit
($hash)
5374 or die_error
(404, "Unknown commit object");
5376 # choose format for commitdiff for merge
5377 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5378 $hash_parent = '--cc';
5380 # we need to prepare $formats_nav before almost any parameter munging
5382 if ($format eq 'html') {
5384 $cgi->a({-href
=> href
(action
=>"commitdiff_plain", -replay
=>1)},
5387 if (defined $hash_parent &&
5388 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5389 # commitdiff with two commits given
5390 my $hash_parent_short = $hash_parent;
5391 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5392 $hash_parent_short = substr($hash_parent, 0, 7);
5396 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5397 if ($co{'parents'}[$i] eq $hash_parent) {
5398 $formats_nav .= ' parent ' . ($i+1);
5402 $formats_nav .= ': ' .
5403 $cgi->a({-href
=> href
(action
=>"commitdiff",
5404 hash
=>$hash_parent)},
5405 esc_html
($hash_parent_short)) .
5407 } elsif (!$co{'parent'}) {
5409 $formats_nav .= ' (initial)';
5410 } elsif (scalar @{$co{'parents'}} == 1) {
5411 # single parent commit
5414 $cgi->a({-href
=> href
(action
=>"commitdiff",
5415 hash
=>$co{'parent'})},
5416 esc_html
(substr($co{'parent'}, 0, 7))) .
5420 if ($hash_parent eq '--cc') {
5421 $formats_nav .= ' | ' .
5422 $cgi->a({-href
=> href
(action
=>"commitdiff",
5423 hash
=>$hash, hash_parent
=>'-c')},
5425 } else { # $hash_parent eq '-c'
5426 $formats_nav .= ' | ' .
5427 $cgi->a({-href
=> href
(action
=>"commitdiff",
5428 hash
=>$hash, hash_parent
=>'--cc')},
5434 $cgi->a({-href
=> href
(action
=>"commitdiff",
5436 esc_html
(substr($_, 0, 7)));
5437 } @{$co{'parents'}} ) .
5442 my $hash_parent_param = $hash_parent;
5443 if (!defined $hash_parent_param) {
5444 # --cc for multiple parents, --root for parentless
5445 $hash_parent_param =
5446 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5452 if ($format eq 'html') {
5453 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
5454 "--no-commit-id", "--patch-with-raw", "--full-index",
5455 $hash_parent_param, $hash, "--"
5456 or die_error
(500, "Open git-diff-tree failed");
5458 while (my $line = <$fd>) {
5460 # empty line ends raw part of diff-tree output
5462 push @difftree, scalar parse_difftree_raw_line
($line);
5465 } elsif ($format eq 'plain') {
5466 open $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
5467 '-p', $hash_parent_param, $hash, "--"
5468 or die_error
(500, "Open git-diff-tree failed");
5471 die_error
(400, "Unknown commitdiff format");
5474 # non-textual hash id's can be cached
5476 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5480 # write commit message
5481 if ($format eq 'html') {
5482 my $refs = git_get_references
();
5483 my $ref = format_ref_marker
($refs, $co{'id'});
5485 git_header_html
(undef, $expires);
5486 git_print_page_nav
('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5487 git_print_header_div
('commit', esc_html
($co{'title'}) . $ref, $hash);
5488 git_print_authorship
(\
%co);
5489 print "<div class=\"page_body\">\n";
5490 if (@{$co{'comment'}} > 1) {
5491 print "<div class=\"log\">\n";
5492 git_print_log
($co{'comment'}, -final_empty_line
=> 1, -remove_title
=> 1);
5493 print "</div>\n"; # class="log"
5496 } elsif ($format eq 'plain') {
5497 my $refs = git_get_references
("tags");
5498 my $tagname = git_get_rev_name_tags
($hash);
5499 my $filename = basename
($project) . "-$hash.patch";
5502 -type
=> 'text/plain',
5503 -charset
=> 'utf-8',
5504 -expires
=> $expires,
5505 -content_disposition
=> 'inline; filename="' . "$filename" . '"');
5506 my %ad = parse_date
($co{'author_epoch'}, $co{'author_tz'});
5507 print "From: " . to_utf8
($co{'author'}) . "\n";
5508 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5509 print "Subject: " . to_utf8
($co{'title'}) . "\n";
5511 print "X-Git-Tag: $tagname\n" if $tagname;
5512 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5514 foreach my $line (@{$co{'comment'}}) {
5515 print to_utf8
($line) . "\n";
5521 if ($format eq 'html') {
5522 my $use_parents = !defined $hash_parent ||
5523 $hash_parent eq '-c' || $hash_parent eq '--cc';
5524 git_difftree_body
(\
@difftree, $hash,
5525 $use_parents ? @{$co{'parents'}} : $hash_parent);
5528 git_patchset_body
($fd, \
@difftree, $hash,
5529 $use_parents ? @{$co{'parents'}} : $hash_parent);
5531 print "</div>\n"; # class="page_body"
5534 } elsif ($format eq 'plain') {
5538 or print "Reading git-diff-tree failed\n";
5542 sub git_commitdiff_plain
{
5543 git_commitdiff
('plain');
5547 if (!defined $hash_base) {
5548 $hash_base = git_get_head_hash
($project);
5550 if (!defined $page) {
5554 my %co = parse_commit
($hash_base)
5555 or die_error
(404, "Unknown commit object");
5557 my $refs = git_get_references
();
5558 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5560 my @commitlist = parse_commits
($hash_base, 101, (100 * $page),
5561 $file_name, "--full-history")
5562 or die_error
(404, "No such file or directory on given branch");
5564 if (!defined $hash && defined $file_name) {
5565 # some commits could have deleted file in question,
5566 # and not have it in tree, but one of them has to have it
5567 for (my $i = 0; $i <= @commitlist; $i++) {
5568 $hash = git_get_hash_by_path
($commitlist[$i]{'id'}, $file_name);
5569 last if defined $hash;
5572 if (defined $hash) {
5573 $ftype = git_get_type
($hash);
5575 if (!defined $ftype) {
5576 die_error
(500, "Unknown type of object");
5579 my $paging_nav = '';
5582 $cgi->a({-href
=> href
(action
=>"history", hash
=>$hash, hash_base
=>$hash_base,
5583 file_name
=>$file_name)},
5585 $paging_nav .= " ⋅ " .
5586 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
5587 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
5589 $paging_nav .= "first";
5590 $paging_nav .= " ⋅ prev";
5593 if ($#commitlist >= 100) {
5595 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
5596 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
5597 $paging_nav .= " ⋅ $next_link";
5599 $paging_nav .= " ⋅ next";
5603 git_print_page_nav
('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5604 git_print_header_div
('commit', esc_html
($co{'title'}), $hash_base);
5605 git_print_page_path
($file_name, $ftype, $hash_base);
5607 git_history_body
(\
@commitlist, 0, 99,
5608 $refs, $hash_base, $ftype, $next_link);
5614 gitweb_check_feature
('search') or die_error
(403, "Search is disabled");
5615 if (!defined $searchtext) {
5616 die_error
(400, "Text field is empty");
5618 if (!defined $hash) {
5619 $hash = git_get_head_hash
($project);
5621 my %co = parse_commit
($hash);
5623 die_error
(404, "Unknown commit object");
5625 if (!defined $page) {
5629 $searchtype ||= 'commit';
5630 if ($searchtype eq 'pickaxe') {
5631 # pickaxe may take all resources of your box and run for several minutes
5632 # with every query - so decide by yourself how public you make this feature
5633 gitweb_check_feature
('pickaxe')
5634 or die_error
(403, "Pickaxe is disabled");
5636 if ($searchtype eq 'grep') {
5637 gitweb_check_feature
('grep')
5638 or die_error
(403, "Grep is disabled");
5643 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5645 if ($searchtype eq 'commit') {
5646 $greptype = "--grep=";
5647 } elsif ($searchtype eq 'author') {
5648 $greptype = "--author=";
5649 } elsif ($searchtype eq 'committer') {
5650 $greptype = "--committer=";
5652 $greptype .= $searchtext;
5653 my @commitlist = parse_commits
($hash, 101, (100 * $page), undef,
5654 $greptype, '--regexp-ignore-case',
5655 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5657 my $paging_nav = '';
5660 $cgi->a({-href
=> href
(action
=>"search", hash
=>$hash,
5661 searchtext
=>$searchtext,
5662 searchtype
=>$searchtype)},
5664 $paging_nav .= " ⋅ " .
5665 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page-1),
5666 -accesskey
=> "p", -title
=> "Alt-p"}, "prev");
5668 $paging_nav .= "first";
5669 $paging_nav .= " ⋅ prev";
5672 if ($#commitlist >= 100) {
5674 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
5675 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
5676 $paging_nav .= " ⋅ $next_link";
5678 $paging_nav .= " ⋅ next";
5681 if ($#commitlist >= 100) {
5684 git_print_page_nav
('','', $hash,$co{'tree'},$hash, $paging_nav);
5685 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
5686 git_search_grep_body
(\
@commitlist, 0, 99, $next_link);
5689 if ($searchtype eq 'pickaxe') {
5690 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
5691 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
5693 print "<table class=\"pickaxe search\">\n";
5696 open my $fd, '-|', git_cmd
(), '--no-pager', 'log', @diff_opts,
5697 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5698 ($search_use_regexp ? '--pickaxe-regex' : ());
5701 while (my $line = <$fd>) {
5705 my %set = parse_difftree_raw_line
($line);
5706 if (defined $set{'commit'}) {
5707 # finish previous commit
5710 "<td class=\"link\">" .
5711 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
5713 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
5719 print "<tr class=\"dark\">\n";
5721 print "<tr class=\"light\">\n";
5724 %co = parse_commit
($set{'commit'});
5725 my $author = chop_and_escape_str
($co{'author_name'}, 15, 5);
5726 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5727 "<td><i>$author</i></td>\n" .
5729 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'}),
5730 -class => "list subject"},
5731 chop_and_escape_str
($co{'title'}, 50) . "<br/>");
5732 } elsif (defined $set{'to_id'}) {
5733 next if ($set{'to_id'} =~ m/^0{40}$/);
5735 print $cgi->a({-href
=> href
(action
=>"blob", hash_base
=>$co{'id'},
5736 hash
=>$set{'to_id'}, file_name
=>$set{'to_file'}),
5738 "<span class=\"match\">" . esc_path
($set{'file'}) . "</span>") .
5744 # finish last commit (warning: repetition!)
5747 "<td class=\"link\">" .
5748 $cgi->a({-href
=> href
(action
=>"commit", hash
=>$co{'id'})}, "commit") .
5750 $cgi->a({-href
=> href
(action
=>"tree", hash
=>$co{'tree'}, hash_base
=>$co{'id'})}, "tree");
5758 if ($searchtype eq 'grep') {
5759 git_print_page_nav
('','', $hash,$co{'tree'},$hash);
5760 git_print_header_div
('commit', esc_html
($co{'title'}), $hash);
5762 print "<table class=\"grep_search\">\n";
5766 open my $fd, "-|", git_cmd
(), 'grep', '-n',
5767 $search_use_regexp ? ('-E', '-i') : '-F',
5768 $searchtext, $co{'tree'};
5770 while (my $line = <$fd>) {
5772 my ($file, $lno, $ltext, $binary);
5773 last if ($matches++ > 1000);
5774 if ($line =~ /^Binary file (.+) matches$/) {
5778 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5780 if ($file ne $lastfile) {
5781 $lastfile and print "</td></tr>\n";
5783 print "<tr class=\"dark\">\n";
5785 print "<tr class=\"light\">\n";
5787 print "<td class=\"list\">".
5788 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
5789 file_name
=>"$file"),
5790 -class => "list"}, esc_path
($file));
5791 print "</td><td>\n";
5795 print "<div class=\"binary\">Binary file</div>\n";
5797 $ltext = untabify
($ltext);
5798 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5799 $ltext = esc_html
($1, -nbsp
=>1);
5800 $ltext .= '<span class="match">';
5801 $ltext .= esc_html
($2, -nbsp
=>1);
5802 $ltext .= '</span>';
5803 $ltext .= esc_html
($3, -nbsp
=>1);
5805 $ltext = esc_html
($ltext, -nbsp
=>1);
5807 print "<div class=\"pre\">" .
5808 $cgi->a({-href
=> href
(action
=>"blob", hash
=>$co{'hash'},
5809 file_name
=>"$file").'#l'.$lno,
5810 -class => "linenr"}, sprintf('%4i', $lno))
5811 . ' ' . $ltext . "</div>\n";
5815 print "</td></tr>\n";
5816 if ($matches > 1000) {
5817 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5820 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5829 sub git_search_help
{
5831 git_print_page_nav
('','', $hash,$hash,$hash);
5833 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5834 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5835 the pattern entered is recognized as the POSIX extended
5836 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5839 <dt><b>commit</b></dt>
5840 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5842 my ($have_grep) = gitweb_check_feature
('grep');
5845 <dt><b>grep</b></dt>
5846 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5847 a different one) are searched for the given pattern. On large trees, this search can take
5848 a while and put some strain on the server, so please use it with some consideration. Note that
5849 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5850 case-sensitive.</dd>
5854 <dt><b>author</b></dt>
5855 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5856 <dt><b>committer</b></dt>
5857 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5859 my ($have_pickaxe) = gitweb_check_feature
('pickaxe');
5860 if ($have_pickaxe) {
5862 <dt><b>pickaxe</b></dt>
5863 <dd>All commits that caused the string to appear or disappear from any file (changes that
5864 added, removed or "modified" the string) will be listed. This search can take a while and
5865 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5866 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5874 my $head = git_get_head_hash
($project);
5875 if (!defined $hash) {
5878 if (!defined $page) {
5881 my $refs = git_get_references
();
5883 my $commit_hash = $hash;
5884 if (defined $hash_parent) {
5885 $commit_hash = "$hash_parent..$hash";
5887 my @commitlist = parse_commits
($commit_hash, 101, (100 * $page));
5889 my $paging_nav = format_paging_nav
('shortlog', $hash, $head, $page, $#commitlist >= 100);
5891 if ($#commitlist >= 100) {
5893 $cgi->a({-href
=> href
(-replay
=>1, page
=>$page+1),
5894 -accesskey
=> "n", -title
=> "Alt-n"}, "next");
5898 git_print_page_nav
('shortlog','', $hash,$hash,$hash, $paging_nav);
5899 git_print_header_div
('summary', $project);
5901 git_shortlog_body
(\
@commitlist, 0, 99, $refs, $next_link);
5906 ## ......................................................................
5907 ## feeds (RSS, Atom; OPML)
5910 my $format = shift || 'atom';
5911 my ($have_blame) = gitweb_check_feature
('blame');
5913 # Atom: http://www.atomenabled.org/developers/syndication/
5914 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5915 if ($format ne 'rss' && $format ne 'atom') {
5916 die_error
(400, "Unknown web feed format");
5919 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5920 my $head = $hash || 'HEAD';
5921 my @commitlist = parse_commits
($head, 150, 0, $file_name);
5925 my $content_type = "application/$format+xml";
5926 if (defined $cgi->http('HTTP_ACCEPT') &&
5927 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5928 # browser (feed reader) prefers text/xml
5929 $content_type = 'text/xml';
5931 if (defined($commitlist[0])) {
5932 %latest_commit = %{$commitlist[0]};
5933 %latest_date = parse_date
($latest_commit{'author_epoch'});
5935 -type
=> $content_type,
5936 -charset
=> 'utf-8',
5937 -last_modified
=> $latest_date{'rfc2822'});
5940 -type
=> $content_type,
5941 -charset
=> 'utf-8');
5944 # Optimization: skip generating the body if client asks only
5945 # for Last-Modified date.
5946 return if ($cgi->request_method() eq 'HEAD');
5949 my $title = "$site_name - $project/$action";
5950 my $feed_type = 'log';
5951 if (defined $hash) {
5952 $title .= " - '$hash'";
5953 $feed_type = 'branch log';
5954 if (defined $file_name) {
5955 $title .= " :: $file_name";
5956 $feed_type = 'history';
5958 } elsif (defined $file_name) {
5959 $title .= " - $file_name";
5960 $feed_type = 'history';
5962 $title .= " $feed_type";
5963 my $descr = git_get_project_description
($project);
5964 if (defined $descr) {
5965 $descr = esc_html
($descr);
5967 $descr = "$project " .
5968 ($format eq 'rss' ? 'RSS' : 'Atom') .
5971 my $owner = git_get_project_owner
($project);
5972 $owner = esc_html
($owner);
5976 if (defined $file_name) {
5977 $alt_url = href
(-full
=>1, action
=>"history", hash
=>$hash, file_name
=>$file_name);
5978 } elsif (defined $hash) {
5979 $alt_url = href
(-full
=>1, action
=>"log", hash
=>$hash);
5981 $alt_url = href
(-full
=>1, action
=>"summary");
5983 print qq
!<?xml version
="1.0" encoding
="utf-8"?>\n!;
5984 if ($format eq 'rss') {
5986 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5989 print "<title>$title</title>\n" .
5990 "<link>$alt_url</link>\n" .
5991 "<description>$descr</description>\n" .
5992 "<language>en</language>\n";
5993 } elsif ($format eq 'atom') {
5995 <feed xmlns="http://www.w3.org/2005/Atom">
5997 print "<title>$title</title>\n" .
5998 "<subtitle>$descr</subtitle>\n" .
5999 '<link rel="alternate" type="text/html" href="' .
6000 $alt_url . '" />' . "\n" .
6001 '<link rel="self" type="' . $content_type . '" href="' .
6002 $cgi->self_url() . '" />' . "\n" .
6003 "<id>" . href
(-full
=>1) . "</id>\n" .
6004 # use project owner for feed author
6005 "<author><name>$owner</name></author>\n";
6006 if (defined $favicon) {
6007 print "<icon>" . esc_url
($favicon) . "</icon>\n";
6009 if (defined $logo_url) {
6010 # not twice as wide as tall: 72 x 27 pixels
6011 print "<logo>" . esc_url
($logo) . "</logo>\n";
6013 if (! %latest_date) {
6014 # dummy date to keep the feed valid until commits trickle in:
6015 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6017 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6022 for (my $i = 0; $i <= $#commitlist; $i++) {
6023 my %co = %{$commitlist[$i]};
6024 my $commit = $co{'id'};
6025 # we read 150, we always show 30 and the ones more recent than 48 hours
6026 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6029 my %cd = parse_date
($co{'author_epoch'});
6031 # get list of changed files
6032 open my $fd, "-|", git_cmd
(), "diff-tree", '-r', @diff_opts,
6033 $co{'parent'} || "--root",
6034 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6036 my @difftree = map { chomp; $_ } <$fd>;
6040 # print element (entry, item)
6041 my $co_url = href
(-full
=>1, action
=>"commitdiff", hash
=>$commit);
6042 if ($format eq 'rss') {
6044 "<title>" . esc_html
($co{'title'}) . "</title>\n" .
6045 "<author>" . esc_html
($co{'author'}) . "</author>\n" .
6046 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6047 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6048 "<link>$co_url</link>\n" .
6049 "<description>" . esc_html
($co{'title'}) . "</description>\n" .
6050 "<content:encoded>" .
6052 } elsif ($format eq 'atom') {
6054 "<title type=\"html\">" . esc_html
($co{'title'}) . "</title>\n" .
6055 "<updated>$cd{'iso-8601'}</updated>\n" .
6057 " <name>" . esc_html
($co{'author_name'}) . "</name>\n";
6058 if ($co{'author_email'}) {
6059 print " <email>" . esc_html
($co{'author_email'}) . "</email>\n";
6061 print "</author>\n" .
6062 # use committer for contributor
6064 " <name>" . esc_html
($co{'committer_name'}) . "</name>\n";
6065 if ($co{'committer_email'}) {
6066 print " <email>" . esc_html
($co{'committer_email'}) . "</email>\n";
6068 print "</contributor>\n" .
6069 "<published>$cd{'iso-8601'}</published>\n" .
6070 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6071 "<id>$co_url</id>\n" .
6072 "<content type=\"xhtml\" xml:base=\"" . esc_url
($my_url) . "\">\n" .
6073 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6075 my $comment = $co{'comment'};
6077 foreach my $line (@$comment) {
6078 $line = esc_html
($line);
6081 print "</pre><ul>\n";
6082 foreach my $difftree_line (@difftree) {
6083 my %difftree = parse_difftree_raw_line
($difftree_line);
6084 next if !$difftree{'from_id'};
6086 my $file = $difftree{'file'} || $difftree{'to_file'};
6090 $cgi->a({-href
=> href
(-full
=>1, action
=>"blobdiff",
6091 hash
=>$difftree{'to_id'}, hash_parent
=>$difftree{'from_id'},
6092 hash_base
=>$co{'id'}, hash_parent_base
=>$co{'parent'},
6093 file_name
=>$file, file_parent
=>$difftree{'from_file'}),
6094 -title
=> "diff"}, 'D');
6096 print $cgi->a({-href
=> href
(-full
=>1, action
=>"blame",
6097 file_name
=>$file, hash_base
=>$commit),
6098 -title
=> "blame"}, 'B');
6100 # if this is not a feed of a file history
6101 if (!defined $file_name || $file_name ne $file) {
6102 print $cgi->a({-href
=> href
(-full
=>1, action
=>"history",
6103 file_name
=>$file, hash
=>$commit),
6104 -title
=> "history"}, 'H');
6106 $file = esc_path
($file);
6110 if ($format eq 'rss') {
6111 print "</ul>]]>\n" .
6112 "</content:encoded>\n" .
6114 } elsif ($format eq 'atom') {
6115 print "</ul>\n</div>\n" .
6122 if ($format eq 'rss') {
6123 print "</channel>\n</rss>\n";
6124 } elsif ($format eq 'atom') {
6138 my @list = git_get_projects_list
();
6140 print $cgi->header(-type
=> 'text/xml', -charset
=> 'utf-8');
6142 <?xml version="1.0" encoding="utf-8"?>
6143 <opml version="1.0">
6145 <title>$site_name OPML Export</title>
6148 <outline text="git RSS feeds">
6151 foreach my $pr (@list) {
6153 my $head = git_get_head_hash
($proj{'path'});
6154 if (!defined $head) {
6157 $git_dir = "$projectroot/$proj{'path'}";
6158 my %co = parse_commit
($head);
6163 my $path = esc_html
(chop_str
($proj{'path'}, 25, 5));
6164 my $rss = "$my_url?p=$proj{'path'};a=rss";
6165 my $html = "$my_url?p=$proj{'path'};a=summary";
6166 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";