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
13 use CGI qw(:standard :escapeHTML -nosticky);
14 use CGI::Util qw(unescape);
15 use CGI::Carp qw(fatalsToBrowser);
19 use File::Basename qw(basename);
21 binmode STDOUT, ':utf8';
23 use Gitalist::Util qw(to_utf8);
30 $cgi $version $my_url $my_uri $base_url $path_info $GIT $projectroot
31 $project_maxdepth $home_link $home_link_str $site_header
32 $home_text $site_footer @stylesheets
33 $logo_url $logo_label $logo_url $logo_label $projects_list
34 $projects_list_description_width $default_projects_order
35 $export_ok $export_auth_hook $strict_export @git_base_url_list
36 $default_blob_plain_mimetype $default_text_plain_charset
37 $mimetypes_file $fallback_encoding @diff_opts $prevent_xss
38 %known_snapshot_formats %known_snapshot_format_aliases %feature
39 $GITWEB_CONFIG $GITWEB_CONFIG $GITWEB_CONFIG_SYSTEM $git_version
40 %input_params @cgi_param_mapping %cgi_param_mapping %actions
41 %allowed_options $action $project $file_name $file_parent $hash
42 $hash_parent $hash_base @extra_options $hash_parent_base $page
43 $searchtype $search_use_regexp $searchtext $search_regexp $git_dir
53 our $version = "1.6.3.3";
54 our $my_url = $cgi->url();
55 our $my_uri = $cgi->url(-absolute => 1);
57 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
58 # needed and used only for URLs with nonempty PATH_INFO
59 our $base_url = $my_url;
61 # When the script is used as DirectoryIndex, the URL does not contain the name
62 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
63 # have to do it ourselves. We make $path_info global because it's also used
66 # Another issue with the script being the DirectoryIndex is that the resulting
67 # $my_url data is not the full script URL: this is good, because we want
68 # generated links to keep implying the script name if it wasn't explicitly
69 # indicated in the URL we're handling, but it means that $my_url cannot be used
71 # Therefore, if we needed to strip PATH_INFO, then we know that we have
72 # to build the base URL ourselves:
73 our $path_info = $ENV{"PATH_INFO"};
75 if ($my_url =~ s,\Q$path_info\E$,, &&
76 $my_uri =~ s,\Q$path_info\E$,, &&
77 defined $ENV{'SCRIPT_NAME'}) {
78 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
82 # core git executable to use
83 # this can just be "git" if your webserver has a sensible PATH
84 our $GIT = `which git`;
87 # absolute fs-path which will be prepended to the project path
88 #our $projectroot = "/pub/scm";
90 # target of the home link on top of all pages
91 our $home_link = $my_uri || "/";
93 # string of the home link on top of all pages
94 our $home_link_str = "Project Gitalist";
96 # filename of html text to include at top of each page
97 our $site_header = "";
98 # html text to include at home page
99 our $home_text = "indextext.html";
100 # filename of html text to include at bottom of each page
101 our $site_footer = "";
104 our @stylesheets = ("gitweb.css");
106 # URI and label (title) of GIT logo link
107 our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
108 our $logo_label = "git documentation";
110 # source of projects list
111 our $projectroot = our $projects_list = $c->config->{projectroot};
113 # the width (in characters) of the projects list "Description" column
114 our $projects_list_description_width = 25;
116 # default order of projects list
117 # valid values are none, project, descr, owner, and age
118 our $default_projects_order = "project";
120 # show repository only if this file exists
121 # (only effective if this variable evaluates to true)
124 # show repository only if this subroutine returns true
125 # when given the path to the project, for example:
126 # sub { return -e "$_[0]/git-daemon-export-ok"; }
127 our $export_auth_hook = undef;
129 # only allow viewing of repositories also shown on the overview page
130 our $strict_export = "";
132 # list of git base URLs used for URL to where fetch project from,
133 # i.e. full URL is "$git_base_url/$project"
134 our @git_base_url_list = grep { $_ ne '' } ("");
136 # default blob_plain mimetype and default charset for text/plain blob
137 our $default_blob_plain_mimetype = 'text/plain';
138 our $default_text_plain_charset = undef;
140 # file to use for guessing MIME types before trying /etc/mime.types
141 # (relative to the current git repository)
142 our $mimetypes_file = undef;
144 # assume this charset if line contains non-UTF-8 characters;
145 # it should be valid encoding (see Encoding::Supported(3pm) for list),
146 # for which encoding all byte sequences are valid, for example
147 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
148 # could be even 'utf-8' for the old behavior)
149 our $fallback_encoding = 'latin1';
151 # rename detection options for git-diff and git-diff-tree
152 # - default is '-M', with the cost proportional to
153 # (number of removed files) * (number of new files).
154 # - more costly is '-C' (which implies '-M'), with the cost proportional to
155 # (number of changed files + number of removed files) * (number of new files)
156 # - even more costly is '-C', '--find-copies-harder' with cost
157 # (number of files in the original tree) * (number of new files)
158 # - one might want to include '-B' option, e.g. '-B', '-M'
159 our @diff_opts = ('-M'); # taken from git_commit
161 # Disables features that would allow repository owners to inject script into
163 our $prevent_xss = 0;
165 # information about snapshot formats that gitweb is capable of serving
166 our %known_snapshot_formats = (
168 # 'display' => display name,
169 # 'type' => mime type,
170 # 'suffix' => filename suffix,
171 # 'format' => --format for git-archive,
172 # 'compressor' => [compressor command and arguments]
173 # (array reference, optional)}
176 'display' => 'tar.gz',
177 'type' => 'application/x-gzip',
178 'suffix' => '.tar.gz',
180 'compressor' => ['gzip']},
183 'display' => 'tar.bz2',
184 'type' => 'application/x-bzip2',
185 'suffix' => '.tar.bz2',
187 'compressor' => ['bzip2']},
191 'type' => 'application/x-zip',
196 # Aliases so we understand old gitweb.snapshot values in repository
198 our %known_snapshot_format_aliases = (
202 # backward compatibility: legacy gitweb config support
203 'x-gzip' => undef, 'gz' => undef,
204 'x-bzip2' => undef, 'bz2' => undef,
205 'x-zip' => undef, '' => undef,
208 my $feature_bool = sub {
210 my ($val) = git_get_project_config($key, '--bool');
214 } elsif ($val eq 'true') {
216 } elsif ($val eq 'false') {
221 my $feature_snapshot = sub {
224 my ($val) = git_get_project_config('snapshot');
227 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
233 my $feature_patches = sub {
234 my @val = (git_get_project_config('patches', '--int'));
244 # You define site-wide feature defaults here; override them with
245 # $GITWEB_CONFIG as necessary.
248 # 'sub' => feature-sub (subroutine),
249 # 'override' => allow-override (boolean),
250 # 'default' => [ default options...] (array reference)}
252 # if feature is overridable (it means that allow-override has true value),
253 # then feature-sub will be called with default options as parameters;
254 # return value of feature-sub indicates if to enable specified feature
256 # if there is no 'sub' key (no feature-sub), then feature cannot be
259 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
260 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
263 # Enable the 'blame' blob view, showing the last commit that modified
264 # each line in the file. This can be very CPU-intensive.
266 # To enable system wide have in $GITWEB_CONFIG
267 # $feature{'blame'}{'default'} = [1];
268 # To have project specific config enable override in $GITWEB_CONFIG
269 # $feature{'blame'}{'override'} = 1;
270 # and in project config gitweb.blame = 0|1;
272 'sub' => sub { &$feature_bool('blame', @_) },
276 # Enable the 'snapshot' link, providing a compressed archive of any
277 # tree. This can potentially generate high traffic if you have large
280 # Value is a list of formats defined in %known_snapshot_formats that
282 # To disable system wide have in $GITWEB_CONFIG
283 # $feature{'snapshot'}{'default'} = [];
284 # To have project specific config enable override in $GITWEB_CONFIG
285 # $feature{'snapshot'}{'override'} = 1;
286 # and in project config, a comma-separated list of formats or "none"
287 # to disable. Example: gitweb.snapshot = tbz2,zip;
289 'sub' => $feature_snapshot,
291 'default' => ['tgz']},
293 # Enable text search, which will list the commits which match author,
294 # committer or commit text to a given string. Enabled by default.
295 # Project specific override is not supported.
300 # Enable grep search, which will list the files in currently selected
301 # tree containing the given string. Enabled by default. This can be
302 # potentially CPU-intensive, of course.
304 # To enable system wide have in $GITWEB_CONFIG
305 # $feature{'grep'}{'default'} = [1];
306 # To have project specific config enable override in $GITWEB_CONFIG
307 # $feature{'grep'}{'override'} = 1;
308 # and in project config gitweb.grep = 0|1;
310 'sub' => sub { &$feature_bool('grep', @_) },
314 # Enable the pickaxe search, which will list the commits that modified
315 # a given string in a file. This can be practical and quite faster
316 # alternative to 'blame', but still potentially CPU-intensive.
318 # To enable system wide have in $GITWEB_CONFIG
319 # $feature{'pickaxe'}{'default'} = [1];
320 # To have project specific config enable override in $GITWEB_CONFIG
321 # $feature{'pickaxe'}{'override'} = 1;
322 # and in project config gitweb.pickaxe = 0|1;
324 'sub' => sub { &$feature_bool('pickaxe', @_) },
328 # Make gitweb use an alternative format of the URLs which can be
329 # more readable and natural-looking: project name is embedded
330 # directly in the path and the query string contains other
331 # auxiliary information. All gitweb installations recognize
332 # URL in either format; this configures in which formats gitweb
335 # To enable system wide have in $GITWEB_CONFIG
336 # $feature{'pathinfo'}{'default'} = [1];
337 # Project specific override is not supported.
339 # Note that you will need to change the default location of CSS,
340 # favicon, logo and possibly other files to an absolute URL. Also,
341 # if gitweb.cgi serves as your indexfile, you will need to force
342 # $my_uri to contain the script name in your $GITWEB_CONFIG.
347 # Make gitweb consider projects in project root subdirectories
348 # to be forks of existing projects. Given project $projname.git,
349 # projects matching $projname/*.git will not be shown in the main
350 # projects list, instead a '+' mark will be added to $projname
351 # there and a 'forks' view will be enabled for the project, listing
352 # all the forks. If project list is taken from a file, forks have
353 # to be listed after the main project.
355 # To enable system wide have in $GITWEB_CONFIG
356 # $feature{'forks'}{'default'} = [1];
357 # Project specific override is not supported.
362 # Insert custom links to the action bar of all project pages.
363 # This enables you mainly to link to third-party scripts integrating
364 # into gitweb; e.g. git-browser for graphical history representation
365 # or custom web-based repository administration interface.
367 # The 'default' value consists of a list of triplets in the form
368 # (label, link, position) where position is the label after which
369 # to insert the link and link is a format string where %n expands
370 # to the project name, %f to the project path within the filesystem,
371 # %h to the current hash (h gitweb parameter) and %b to the current
372 # hash base (hb gitweb parameter); %% expands to %.
374 # To enable system wide have in $GITWEB_CONFIG e.g.
375 # $feature{'actions'}{'default'} = [('graphiclog',
376 # '/git-browser/by-commit.html?r=%n', 'summary')];
377 # Project specific override is not supported.
382 # Allow gitweb scan project content tags described in ctags/
383 # of project repository, and display the popular Web 2.0-ish
384 # "tag cloud" near the project list. Note that this is something
385 # COMPLETELY different from the normal Git tags.
387 # gitweb by itself can show existing tags, but it does not handle
388 # tagging itself; you need an external application for that.
389 # For an example script, check Girocco's cgi/tagproj.cgi.
390 # You may want to install the HTML::TagCloud Perl module to get
391 # a pretty tag cloud instead of just a list of tags.
393 # To enable system wide have in $GITWEB_CONFIG
394 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
395 # Project specific override is not supported.
400 # The maximum number of patches in a patchset generated in patch
401 # view. Set this to 0 or undef to disable patch view, or to a
402 # negative number to remove any limit.
404 # To disable system wide have in $GITWEB_CONFIG
405 # $feature{'patches'}{'default'} = [0];
406 # To have project specific config enable override in $GITWEB_CONFIG
407 # $feature{'patches'}{'override'} = 1;
408 # and in project config gitweb.patches = 0|n;
409 # where n is the maximum number of patches allowed in a patchset.
411 'sub' => $feature_patches,
416 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "gitweb_config.perl";
417 if (-e $GITWEB_CONFIG) {
420 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "$FindBin::Bin/../gitweb.conf";
421 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
424 # version of the core git binary
425 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
427 # ======================================================================
428 # input validation and dispatch
430 # input parameters can be collected from a variety of sources (presently, CGI
431 # and PATH_INFO), so we define an %input_params hash that collects them all
432 # together during validation: this allows subsequent uses (e.g. href()) to be
433 # agnostic of the parameter origin
435 our %input_params = ();
437 # input parameters are stored with the long parameter name as key. This will
438 # also be used in the href subroutine to convert parameters to their CGI
439 # equivalent, and since the href() usage is the most frequent one, we store
440 # the name -> CGI key mapping here, instead of the reverse.
442 # XXX: Warning: If you touch this, check the search form for updating,
445 our @cgi_param_mapping = (
453 hash_parent_base => "hpb",
458 snapshot_format => "sf",
459 extra_options => "opt",
460 search_use_regexp => "sr",
462 our %cgi_param_mapping = @cgi_param_mapping;
464 # we will also need to know the possible actions, for validation
466 "blame" => \&git_blame,
467 "blobdiff" => \&git_blobdiff,
468 "blobdiff_plain" => \&git_blobdiff_plain,
469 "blob" => \&git_blob,
470 "blob_plain" => \&git_blob_plain,
471 "commitdiff" => \&git_commitdiff,
472 "commitdiff_plain" => \&git_commitdiff_plain,
473 "commit" => \&git_commit,
474 "forks" => \&git_forks,
475 "heads" => \&git_heads,
476 "history" => \&git_history,
478 "patch" => \&git_patch,
479 "patches" => \&git_patches,
481 "atom" => \&git_atom,
482 "search" => \&git_search,
483 "search_help" => \&git_search_help,
484 "shortlog" => \&git_shortlog,
485 "summary" => \&git_summary,
487 "tags" => \&git_tags,
488 "tree" => \&git_tree,
489 "snapshot" => \&git_snapshot,
490 "object" => \&git_object,
491 # those below don't need $project
492 "opml" => \&git_opml,
493 "project_list" => \&git_project_list,
494 "project_index" => \&git_project_index,
497 # finally, we have the hash of allowed extra_options for the commands that
499 our %allowed_options = (
500 "--no-merges" => [ qw(rss atom log shortlog history) ],
503 # fill %input_params with the CGI parameters. All values except for 'opt'
504 # should be single values, but opt can be an array. We should probably
505 # build an array of parameters that can be multi-valued, but since for the time
506 # being it's only this one, we just single it out
507 while (my ($name, $symbol) = each %cgi_param_mapping) {
508 if ($symbol eq 'opt') {
509 $input_params{$name} = [ $c->req->param($symbol) ];
511 $input_params{$name} = $c->req->param($symbol);
515 # now read PATH_INFO and update the parameter list for missing parameters
516 my $evaluate_path_info = sub {
517 return if defined $input_params{'project'};
518 return if !$path_info;
519 $path_info =~ s,^/+,,;
520 return if !$path_info;
522 # find which part of PATH_INFO is project
523 my $project = $path_info;
525 while ($project && !check_head_link("$projectroot/$project")) {
526 $project =~ s,/*[^/]*$,,;
528 return unless $project;
529 $input_params{'project'} = $project;
531 # do not change any parameters if an action is given using the query string
532 return if $input_params{'action'};
533 $path_info =~ s,^\Q$project\E/*,,;
535 # next, check if we have an action
536 my $action = $path_info;
538 if (exists $actions{$action}) {
539 $path_info =~ s,^$action/*,,;
540 $input_params{'action'} = $action;
543 # list of actions that want hash_base instead of hash, but can have no
544 # pathname (f) parameter
551 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
552 my ($parentrefname, $parentpathname, $refname, $pathname) =
553 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
555 # first, analyze the 'current' part
556 if (defined $pathname) {
557 # we got "branch:filename" or "branch:dir/"
558 # we could use git_get_type(branch:pathname), but:
559 # - it needs $git_dir
560 # - it does a git() call
561 # - the convention of terminating directories with a slash
562 # makes it superfluous
563 # - embedding the action in the PATH_INFO would make it even
565 $pathname =~ s,^/+,,;
566 if (!$pathname || substr($pathname, -1) eq "/") {
567 $input_params{'action'} ||= "tree";
570 # the default action depends on whether we had parent info
572 if ($parentrefname) {
573 $input_params{'action'} ||= "blobdiff_plain";
575 $input_params{'action'} ||= "blob_plain";
578 $input_params{'hash_base'} ||= $refname;
579 $input_params{'file_name'} ||= $pathname;
580 } elsif (defined $refname) {
581 # we got "branch". In this case we have to choose if we have to
582 # set hash or hash_base.
584 # Most of the actions without a pathname only want hash to be
585 # set, except for the ones specified in @wants_base that want
586 # hash_base instead. It should also be noted that hand-crafted
587 # links having 'history' as an action and no pathname or hash
588 # set will fail, but that happens regardless of PATH_INFO.
589 $input_params{'action'} ||= "shortlog";
590 if (grep { $_ eq $input_params{'action'} } @wants_base) {
591 $input_params{'hash_base'} ||= $refname;
593 $input_params{'hash'} ||= $refname;
597 # next, handle the 'parent' part, if present
598 if (defined $parentrefname) {
599 # a missing pathspec defaults to the 'current' filename, allowing e.g.
600 # someproject/blobdiff/oldrev..newrev:/filename
601 if ($parentpathname) {
602 $parentpathname =~ s,^/+,,;
603 $parentpathname =~ s,/$,,;
604 $input_params{'file_parent'} ||= $parentpathname;
606 $input_params{'file_parent'} ||= $input_params{'file_name'};
608 # we assume that hash_parent_base is wanted if a path was specified,
609 # or if the action wants hash_base instead of hash
610 if (defined $input_params{'file_parent'} ||
611 grep { $_ eq $input_params{'action'} } @wants_base) {
612 $input_params{'hash_parent_base'} ||= $parentrefname;
614 $input_params{'hash_parent'} ||= $parentrefname;
618 # for the snapshot action, we allow URLs in the form
619 # $project/snapshot/$hash.ext
620 # where .ext determines the snapshot and gets removed from the
621 # passed $refname to provide the $hash.
623 # To be able to tell that $refname includes the format extension, we
624 # require the following two conditions to be satisfied:
625 # - the hash input parameter MUST have been set from the $refname part
626 # of the URL (i.e. they must be equal)
627 # - the snapshot format MUST NOT have been defined already (e.g. from
629 # It's also useless to try any matching unless $refname has a dot,
630 # so we check for that too
631 if (defined $input_params{'action'} &&
632 $input_params{'action'} eq 'snapshot' &&
633 defined $refname && index($refname, '.') != -1 &&
634 $refname eq $input_params{'hash'} &&
635 !defined $input_params{'snapshot_format'}) {
636 # We loop over the known snapshot formats, checking for
637 # extensions. Allowed extensions are both the defined suffix
638 # (which includes the initial dot already) and the snapshot
639 # format key itself, with a prepended dot
640 while (my ($fmt, $opt) = each %known_snapshot_formats) {
643 $hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//;
644 next unless $sfx = $1;
645 # a valid suffix was found, so set the snapshot format
646 # and reset the hash parameter
647 $input_params{'snapshot_format'} = $fmt;
648 $input_params{'hash'} = $hash;
649 # we also set the format suffix to the one requested
650 # in the URL: this way a request for e.g. .tgz returns
651 # a .tgz instead of a .tar.gz
652 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
658 &$evaluate_path_info();
660 gitweb_validate_setup();
662 return $actions{$action};
665 sub gitweb_validate_setup {
666 our $action = $input_params{'action'};
667 if (defined $action) {
668 if (!validate_action($action)) {
669 die_error(400, "Invalid action parameter");
673 # parameters which are pathnames
674 our $project = $input_params{'project'};
675 if (defined $project) {
676 if (!validate_project($project)) {
678 die_error(404, "No such project");
682 our $file_name = $input_params{'file_name'};
683 if (defined $file_name) {
684 if (!validate_pathname($file_name)) {
685 die_error(400, "Invalid file parameter");
689 our $file_parent = $input_params{'file_parent'};
690 if (defined $file_parent) {
691 if (!validate_pathname($file_parent)) {
692 die_error(400, "Invalid file parent parameter");
696 # parameters which are refnames
697 our $hash = $input_params{'hash'};
699 if (!validate_refname($hash)) {
700 die_error(400, "Invalid hash parameter");
704 our $hash_parent = $input_params{'hash_parent'};
705 if (defined $hash_parent) {
706 if (!validate_refname($hash_parent)) {
707 die_error(400, "Invalid hash parent parameter");
711 our $hash_base = $input_params{'hash_base'};
712 if (defined $hash_base) {
713 if (!validate_refname($hash_base)) {
714 die_error(400, "Invalid hash base parameter");
718 our @extra_options = @{$input_params{'extra_options'}};
719 # @extra_options is always defined, since it can only be (currently) set from
720 # CGI, and $c->req->param() returns the empty array in array context if the param
722 foreach my $opt (@extra_options) {
723 if (not exists $allowed_options{$opt}) {
724 die_error(400, "Invalid option parameter");
726 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
727 die_error(400, "Invalid option parameter for this action");
731 our $hash_parent_base = $input_params{'hash_parent_base'};
732 if (defined $hash_parent_base) {
733 if (!validate_refname($hash_parent_base)) {
734 die_error(400, "Invalid hash parent base parameter");
739 our $page = $input_params{'page'};
741 if ($page =~ m/[^0-9]/) {
742 die_error(400, "Invalid page parameter");
746 our $searchtype = $input_params{'searchtype'};
747 if (defined $searchtype) {
748 if ($searchtype =~ m/[^a-z]/) {
749 die_error(400, "Invalid searchtype parameter");
753 our $search_use_regexp = $input_params{'search_use_regexp'};
755 our $searchtext = $input_params{'searchtext'};
757 if (defined $searchtext) {
758 if (length($searchtext) < 2) {
759 die_error(403, "At least two characters are required for search parameter");
761 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
764 # path to the current git repository
766 $git_dir = "$projectroot/$project" if $project;
768 # process alternate names for backward compatibility
769 # filter out unsupported (unknown) snapshot formats
770 my $filter_snapshot_fmts = sub {
774 exists $known_snapshot_format_aliases{$_} ?
775 $known_snapshot_format_aliases{$_} : $_} @fmts;
776 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
779 # list of supported snapshot formats
780 our @snapshot_fmts = gitweb_get_feature('snapshot');
781 @snapshot_fmts = &$filter_snapshot_fmts(@snapshot_fmts);
784 if (!defined $action) {
786 $action = git_get_type($hash);
787 } elsif (defined $hash_base && defined $file_name) {
788 $action = git_get_type("$hash_base:$file_name");
789 } elsif (defined $project) {
792 $action = 'project_list';
795 if (!defined($actions{$action})) {
796 die_error(400, "Unknown action");
798 if ($action !~ m/^(opml|project_list|project_index)$/ &&
800 die_error(400, "Project needed");
804 sub gitweb_get_feature {
806 return unless exists $feature{$name};
807 my ($sub, $override, @defaults) = (
808 $feature{$name}{'sub'},
809 $feature{$name}{'override'},
810 @{$feature{$name}{'default'}});
811 if (!$override) { return @defaults; }
813 warn "feature $name is not overrideable";
816 return $sub->(@defaults);
819 # A wrapper to check if a given feature is enabled.
820 # With this, you can say
822 # my $bool_feat = gitweb_check_feature('bool_feat');
823 # gitweb_check_feature('bool_feat') or somecode;
827 # my ($bool_feat) = gitweb_get_feature('bool_feat');
828 # (gitweb_get_feature('bool_feat'))[0] or somecode;
830 sub gitweb_check_feature {
831 return (gitweb_get_feature(@_))[0];
834 # checking HEAD file with -e is fragile if the repository was
835 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
837 sub check_head_link {
839 my $headfile = "$dir/HEAD";
840 return ((-e $headfile) ||
841 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
844 sub check_export_ok {
846 return (check_head_link($dir) &&
847 (!$export_ok || -e "$dir/$export_ok") &&
848 (!$export_auth_hook || $export_auth_hook->($dir)));
852 ## ======================================================================
857 # default is to use -absolute url() i.e. $my_uri
858 my $href = $params{-full} ? $my_url : $my_uri;
860 $params{'project'} = $project unless exists $params{'project'};
862 if ($params{-replay}) {
863 while (my ($name, $symbol) = each %cgi_param_mapping) {
864 if (!exists $params{$name}) {
865 $params{$name} = $input_params{$name};
870 my $use_pathinfo = gitweb_check_feature('pathinfo');
871 if ($use_pathinfo and defined $params{'project'}) {
872 # try to put as many parameters as possible in PATH_INFO:
875 # - hash_parent or hash_parent_base:/file_parent
876 # - hash or hash_base:/filename
877 # - the snapshot_format as an appropriate suffix
879 # When the script is the root DirectoryIndex for the domain,
880 # $href here would be something like http://gitweb.example.com/
881 # Thus, we strip any trailing / from $href, to spare us double
882 # slashes in the final URL
885 # Then add the project name, if present
886 $href .= "/".esc_url($params{'project'});
887 delete $params{'project'};
889 # since we destructively absorb parameters, we keep this
890 # boolean that remembers if we're handling a snapshot
891 my $is_snapshot = $params{'action'} eq 'snapshot';
893 # Summary just uses the project path URL, any other action is
895 if (defined $params{'action'}) {
896 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
897 delete $params{'action'};
900 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
901 # stripping nonexistent or useless pieces
902 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
903 || $params{'hash_parent'} || $params{'hash'});
904 if (defined $params{'hash_base'}) {
905 if (defined $params{'hash_parent_base'}) {
906 $href .= esc_url($params{'hash_parent_base'});
907 # skip the file_parent if it's the same as the file_name
908 delete $params{'file_parent'} if $params{'file_parent'} eq $params{'file_name'};
909 if (defined $params{'file_parent'} && $params{'file_parent'} !~ /\.\./) {
910 $href .= ":/".esc_url($params{'file_parent'});
911 delete $params{'file_parent'};
914 delete $params{'hash_parent'};
915 delete $params{'hash_parent_base'};
916 } elsif (defined $params{'hash_parent'}) {
917 $href .= esc_url($params{'hash_parent'}). "..";
918 delete $params{'hash_parent'};
921 $href .= esc_url($params{'hash_base'});
922 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
923 $href .= ":/".esc_url($params{'file_name'});
924 delete $params{'file_name'};
926 delete $params{'hash'};
927 delete $params{'hash_base'};
928 } elsif (defined $params{'hash'}) {
929 $href .= esc_url($params{'hash'});
930 delete $params{'hash'};
933 # If the action was a snapshot, we can absorb the
934 # snapshot_format parameter too
936 my $fmt = $params{'snapshot_format'};
937 # snapshot_format should always be defined when href()
938 # is called, but just in case some code forgets, we
939 # fall back to the default
940 $fmt ||= $snapshot_fmts[0];
941 $href .= $known_snapshot_formats{$fmt}{'suffix'};
942 delete $params{'snapshot_format'};
946 # now encode the parameters explicitly
948 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
949 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
950 if (defined $params{$name}) {
951 if (ref($params{$name}) eq "ARRAY") {
952 foreach my $par (@{$params{$name}}) {
953 push @result, $symbol . "=" . esc_param($par);
956 push @result, $symbol . "=" . esc_param($params{$name});
960 $href .= "?" . join(';', @result) if scalar @result;
966 ## ======================================================================
967 ## validation, quoting/unquoting and escaping
969 sub validate_action {
970 my $input = shift || return undef;
971 return undef unless exists $actions{$input};
975 sub validate_project {
976 my $input = shift || return undef;
977 if (!validate_pathname($input) ||
978 !(-d "$projectroot/$input") ||
979 !check_export_ok("$projectroot/$input") ||
980 ($strict_export && !project_in_list($input))) {
987 sub validate_pathname {
988 my $input = shift || return undef;
990 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
991 # at the beginning, at the end, and between slashes.
992 # also this catches doubled slashes
993 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
997 if ($input =~ m!\0!) {
1003 sub validate_refname {
1004 my $input = shift || return undef;
1006 # textual hashes are O.K.
1007 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1010 # it must be correct pathname
1011 $input = validate_pathname($input)
1013 # restrictions on ref name according to git-check-ref-format
1014 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1020 # quote unsafe chars, but keep the slash, even when it's not
1021 # correct, but quoted slashes look too horrible in bookmarks
1024 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
1030 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
1033 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1039 # replace invalid utf8 character with SUBSTITUTION sequence
1040 sub esc_html ($;%) {
1044 $str = to_utf8($str);
1045 $str = $cgi->escapeHTML($str);
1046 if ($opts{'-nbsp'}) {
1047 $str =~ s/ / /g;
1049 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1053 # quote control characters and escape filename to HTML
1058 $str = to_utf8($str);
1059 $str = $cgi->escapeHTML($str);
1060 if ($opts{'-nbsp'}) {
1061 $str =~ s/ / /g;
1063 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1067 # Make control characters "printable", using character escape codes (CEC)
1071 my %es = ( # character escape codes, aka escape sequences
1072 "\t" => '\t', # tab (HT)
1073 "\n" => '\n', # line feed (LF)
1074 "\r" => '\r', # carrige return (CR)
1075 "\f" => '\f', # form feed (FF)
1076 "\b" => '\b', # backspace (BS)
1077 "\a" => '\a', # alarm (bell) (BEL)
1078 "\e" => '\e', # escape (ESC)
1079 "\013" => '\v', # vertical tab (VT)
1080 "\000" => '\0', # nul character (NUL)
1082 my $chr = ( (exists $es{$cntrl})
1084 : sprintf('\%2x', ord($cntrl)) );
1085 if ($opts{-nohtml}) {
1088 return "<span class=\"cntrl\">$chr</span>";
1092 # Alternatively use unicode control pictures codepoints,
1093 # Unicode "printable representation" (PR)
1098 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1099 if ($opts{-nohtml}) {
1102 return "<span class=\"cntrl\">$chr</span>";
1106 # git may return quoted and escaped filenames
1112 my %es = ( # character escape codes, aka escape sequences
1113 't' => "\t", # tab (HT, TAB)
1114 'n' => "\n", # newline (NL)
1115 'r' => "\r", # return (CR)
1116 'f' => "\f", # form feed (FF)
1117 'b' => "\b", # backspace (BS)
1118 'a' => "\a", # alarm (bell) (BEL)
1119 'e' => "\e", # escape (ESC)
1120 'v' => "\013", # vertical tab (VT)
1123 if ($seq =~ m/^[0-7]{1,3}$/) {
1124 # octal char sequence
1125 return chr(oct($seq));
1126 } elsif (exists $es{$seq}) {
1127 # C escape sequence, aka character escape code
1130 # quoted ordinary character
1134 if ($str =~ m/^"(.*)"$/) {
1137 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1142 # escape tabs (convert tabs to spaces)
1146 while ((my $pos = index($line, "\t")) != -1) {
1147 if (my $count = (8 - ($pos % 8))) {
1148 my $spaces = ' ' x $count;
1149 $line =~ s/\t/$spaces/;
1156 sub project_in_list {
1157 my $project = shift;
1158 my @list = git_get_projects_list();
1159 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1162 ## ----------------------------------------------------------------------
1163 ## HTML aware string manipulation
1165 # Try to chop given string on a word boundary between position
1166 # $len and $len+$add_len. If there is no word boundary there,
1167 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1168 # (marking chopped part) would be longer than given string.
1172 my $add_len = shift || 10;
1173 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1175 # Make sure perl knows it is utf8 encoded so we don't
1176 # cut in the middle of a utf8 multibyte char.
1177 $str = to_utf8($str);
1179 # allow only $len chars, but don't cut a word if it would fit in $add_len
1180 # if it doesn't fit, cut it if it's still longer than the dots we would add
1181 # remove chopped character entities entirely
1183 # when chopping in the middle, distribute $len into left and right part
1184 # return early if chopping wouldn't make string shorter
1185 if ($where eq 'center') {
1186 return $str if ($len + 5 >= length($str)); # filler is length 5
1189 return $str if ($len + 4 >= length($str)); # filler is length 4
1192 # regexps: ending and beginning with word part up to $add_len
1193 my $endre = qr/.{$len}\w{0,$add_len}/;
1194 my $begre = qr/\w{0,$add_len}.{$len}/;
1196 if ($where eq 'left') {
1197 $str =~ m/^(.*?)($begre)$/;
1198 my ($lead, $body) = ($1, $2);
1199 if (length($lead) > 4) {
1200 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1203 return "$lead$body";
1205 } elsif ($where eq 'center') {
1206 $str =~ m/^($endre)(.*)$/;
1207 my ($left, $str) = ($1, $2);
1208 $str =~ m/^(.*?)($begre)$/;
1209 my ($mid, $right) = ($1, $2);
1210 if (length($mid) > 5) {
1211 $left =~ s/&[^;]*$//;
1212 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1215 return "$left$mid$right";
1218 $str =~ m/^($endre)(.*)$/;
1221 if (length($tail) > 4) {
1222 $body =~ s/&[^;]*$//;
1225 return "$body$tail";
1229 # takes the same arguments as chop_str, but also wraps a <span> around the
1230 # result with a title attribute if it does get chopped. Additionally, the
1231 # string is HTML-escaped.
1232 sub chop_and_escape_str {
1235 my $chopped = chop_str(@_);
1236 if ($chopped eq $str) {
1237 return esc_html($chopped);
1239 $str =~ s/([[:cntrl:]])/?/g;
1240 return $cgi->span({-title=>$str}, esc_html($chopped));
1244 ## ----------------------------------------------------------------------
1245 ## functions returning short strings
1247 # CSS class for given age value (in seconds)
1251 if (!defined $age) {
1253 } elsif ($age < 60*60*2) {
1255 } elsif ($age < 60*60*24*2) {
1262 # convert age in seconds to "nn units ago" string
1267 if ($age > 60*60*24*365*2) {
1268 $age_str = (int $age/60/60/24/365);
1269 $age_str .= " years ago";
1270 } elsif ($age > 60*60*24*(365/12)*2) {
1271 $age_str = int $age/60/60/24/(365/12);
1272 $age_str .= " months ago";
1273 } elsif ($age > 60*60*24*7*2) {
1274 $age_str = int $age/60/60/24/7;
1275 $age_str .= " weeks ago";
1276 } elsif ($age > 60*60*24*2) {
1277 $age_str = int $age/60/60/24;
1278 $age_str .= " days ago";
1279 } elsif ($age > 60*60*2) {
1280 $age_str = int $age/60/60;
1281 $age_str .= " hours ago";
1282 } elsif ($age > 60*2) {
1283 $age_str = int $age/60;
1284 $age_str .= " min ago";
1285 } elsif ($age > 2) {
1286 $age_str = int $age;
1287 $age_str .= " sec ago";
1289 $age_str .= " right now";
1295 S_IFINVALID => 0030000,
1296 S_IFGITLINK => 0160000,
1299 # submodule/subproject, a commit object reference
1300 sub S_ISGITLINK($) {
1303 return (($mode & S_IFMT) == S_IFGITLINK)
1306 # convert file mode in octal to symbolic file mode string
1308 my $mode = oct shift;
1310 if (S_ISGITLINK($mode)) {
1311 return 'm---------';
1312 } elsif (S_ISDIR($mode & S_IFMT)) {
1313 return 'drwxr-xr-x';
1314 } elsif (S_ISLNK($mode)) {
1315 return 'lrwxrwxrwx';
1316 } elsif (S_ISREG($mode)) {
1317 # git cares only about the executable bit
1318 if ($mode & S_IXUSR) {
1319 return '-rwxr-xr-x';
1321 return '-rw-r--r--';
1324 return '----------';
1328 # convert file mode in octal to file type string
1332 if ($mode !~ m/^[0-7]+$/) {
1338 if (S_ISGITLINK($mode)) {
1340 } elsif (S_ISDIR($mode & S_IFMT)) {
1342 } elsif (S_ISLNK($mode)) {
1344 } elsif (S_ISREG($mode)) {
1351 # convert file mode in octal to file type description string
1352 sub file_type_long {
1355 if ($mode !~ m/^[0-7]+$/) {
1361 if (S_ISGITLINK($mode)) {
1363 } elsif (S_ISDIR($mode & S_IFMT)) {
1365 } elsif (S_ISLNK($mode)) {
1367 } elsif (S_ISREG($mode)) {
1368 if ($mode & S_IXUSR) {
1369 return "executable";
1379 ## ----------------------------------------------------------------------
1380 ## functions returning short HTML fragments, or transforming HTML fragments
1381 ## which don't belong to other sections
1383 # format line of commit message.
1384 sub format_log_line_html {
1387 $line = esc_html($line, -nbsp=>1);
1388 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1389 $cgi->a({-href => href(action=>"object", hash=>$1),
1390 -class => "text"}, $1);
1396 # format marker of refs pointing to given object
1398 # the destination action is chosen based on object type and current context:
1399 # - for annotated tags, we choose the tag view unless it's the current view
1400 # already, in which case we go to shortlog view
1401 # - for other refs, we keep the current view if we're in history, shortlog or
1402 # log view, and select shortlog otherwise
1403 sub format_ref_marker {
1404 my ($refs, $id) = @_;
1407 if (defined $refs->{$id}) {
1408 foreach my $ref (@{$refs->{$id}}) {
1409 # this code exploits the fact that non-lightweight tags are the
1410 # only indirect objects, and that they are the only objects for which
1411 # we want to use tag instead of shortlog as action
1412 my ($type, $name) = qw();
1413 my $indirect = ($ref =~ s/\^\{\}$//);
1414 # e.g. tags/v2.6.11 or heads/next
1415 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1424 $class .= " indirect" if $indirect;
1426 my $dest_action = "shortlog";
1429 $dest_action = "tag" unless $action eq "tag";
1430 } elsif ($action =~ /^(history|(short)?log)$/) {
1431 $dest_action = $action;
1435 $dest .= "refs/" unless $ref =~ m!^refs/!;
1438 my $link = $cgi->a({
1440 action=>$dest_action,
1444 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1450 return ' <span class="refs">'. $markers . '</span>';
1456 # format, perhaps shortened and with markers, title line
1457 sub format_subject_html {
1458 my ($long, $short, $href, $extra) = @_;
1459 $extra = '' unless defined($extra);
1461 if (length($short) < length($long)) {
1462 return $cgi->a({-href => $href, -class => "list subject",
1463 -title => to_utf8($long)},
1464 esc_html($short) . $extra);
1466 return $cgi->a({-href => $href, -class => "list subject"},
1467 esc_html($long) . $extra);
1471 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1472 sub format_git_diff_header_line {
1474 my $diffinfo = shift;
1475 my ($from, $to) = @_;
1477 if ($diffinfo->{'nparents'}) {
1479 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1480 if ($to->{'href'}) {
1481 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1482 esc_path($to->{'file'}));
1483 } else { # file was deleted (no href)
1484 $line .= esc_path($to->{'file'});
1488 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1489 if ($from->{'href'}) {
1490 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1491 'a/' . esc_path($from->{'file'}));
1492 } else { # file was added (no href)
1493 $line .= 'a/' . esc_path($from->{'file'});
1496 if ($to->{'href'}) {
1497 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1498 'b/' . esc_path($to->{'file'}));
1499 } else { # file was deleted
1500 $line .= 'b/' . esc_path($to->{'file'});
1504 return "<div class=\"diff header\">$line</div>\n";
1507 # format extended diff header line, before patch itself
1508 sub format_extended_diff_header_line {
1510 my $diffinfo = shift;
1511 my ($from, $to) = @_;
1514 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1515 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1516 esc_path($from->{'file'}));
1518 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1519 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1520 esc_path($to->{'file'}));
1522 # match single <mode>
1523 if ($line =~ m/\s(\d{6})$/) {
1524 $line .= '<span class="info"> (' .
1525 file_type_long($1) .
1529 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1530 # can match only for combined diff
1532 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1533 if ($from->{'href'}[$i]) {
1534 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1536 substr($diffinfo->{'from_id'}[$i],0,7));
1541 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1544 if ($to->{'href'}) {
1545 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1546 substr($diffinfo->{'to_id'},0,7));
1551 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1552 # can match only for ordinary diff
1553 my ($from_link, $to_link);
1554 if ($from->{'href'}) {
1555 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1556 substr($diffinfo->{'from_id'},0,7));
1558 $from_link = '0' x 7;
1560 if ($to->{'href'}) {
1561 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1562 substr($diffinfo->{'to_id'},0,7));
1566 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1567 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1570 return $line . "<br/>\n";
1573 # format from-file/to-file diff header
1574 sub format_diff_from_to_header {
1575 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1580 #assert($line =~ m/^---/) if DEBUG;
1581 # no extra formatting for "^--- /dev/null"
1582 if (! $diffinfo->{'nparents'}) {
1583 # ordinary (single parent) diff
1584 if ($line =~ m!^--- "?a/!) {
1585 if ($from->{'href'}) {
1587 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1588 esc_path($from->{'file'}));
1591 esc_path($from->{'file'});
1594 $result .= qq!<div class="diff from_file">$line</div>\n!;
1597 # combined diff (merge commit)
1598 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1599 if ($from->{'href'}[$i]) {
1601 $cgi->a({-href=>href(action=>"blobdiff",
1602 hash_parent=>$diffinfo->{'from_id'}[$i],
1603 hash_parent_base=>$parents[$i],
1604 file_parent=>$from->{'file'}[$i],
1605 hash=>$diffinfo->{'to_id'},
1607 file_name=>$to->{'file'}),
1609 -title=>"diff" . ($i+1)},
1612 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1613 esc_path($from->{'file'}[$i]));
1615 $line = '--- /dev/null';
1617 $result .= qq!<div class="diff from_file">$line</div>\n!;
1622 #assert($line =~ m/^\+\+\+/) if DEBUG;
1623 # no extra formatting for "^+++ /dev/null"
1624 if ($line =~ m!^\+\+\+ "?b/!) {
1625 if ($to->{'href'}) {
1627 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1628 esc_path($to->{'file'}));
1631 esc_path($to->{'file'});
1634 $result .= qq!<div class="diff to_file">$line</div>\n!;
1639 # create note for patch simplified by combined diff
1640 sub format_diff_cc_simplified {
1641 my ($diffinfo, @parents) = @_;
1644 $result .= "<div class=\"diff header\">" .
1646 if (!is_deleted($diffinfo)) {
1647 $result .= $cgi->a({-href => href(action=>"blob",
1649 hash=>$diffinfo->{'to_id'},
1650 file_name=>$diffinfo->{'to_file'}),
1652 esc_path($diffinfo->{'to_file'}));
1654 $result .= esc_path($diffinfo->{'to_file'});
1656 $result .= "</div>\n" . # class="diff header"
1657 "<div class=\"diff nodifferences\">" .
1659 "</div>\n"; # class="diff nodifferences"
1664 # format patch (diff) line (not to be used for diff headers)
1665 sub format_diff_line {
1667 my ($from, $to) = @_;
1668 my $diff_class = "";
1672 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1674 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1675 if ($line =~ m/^\@{3}/) {
1676 $diff_class = " chunk_header";
1677 } elsif ($line =~ m/^\\/) {
1678 $diff_class = " incomplete";
1679 } elsif ($prefix =~ tr/+/+/) {
1680 $diff_class = " add";
1681 } elsif ($prefix =~ tr/-/-/) {
1682 $diff_class = " rem";
1685 # assume ordinary diff
1686 my $char = substr($line, 0, 1);
1688 $diff_class = " add";
1689 } elsif ($char eq '-') {
1690 $diff_class = " rem";
1691 } elsif ($char eq '@') {
1692 $diff_class = " chunk_header";
1693 } elsif ($char eq "\\") {
1694 $diff_class = " incomplete";
1697 $line = untabify($line);
1698 if ($from && $to && $line =~ m/^\@{2} /) {
1699 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1700 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1702 $from_lines = 0 unless defined $from_lines;
1703 $to_lines = 0 unless defined $to_lines;
1705 if ($from->{'href'}) {
1706 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1707 -class=>"list"}, $from_text);
1709 if ($to->{'href'}) {
1710 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1711 -class=>"list"}, $to_text);
1713 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1714 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1715 return "<div class=\"diff$diff_class\">$line</div>\n";
1716 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1717 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1718 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1720 @from_text = split(' ', $ranges);
1721 for (my $i = 0; $i < @from_text; ++$i) {
1722 ($from_start[$i], $from_nlines[$i]) =
1723 (split(',', substr($from_text[$i], 1)), 0);
1726 $to_text = pop @from_text;
1727 $to_start = pop @from_start;
1728 $to_nlines = pop @from_nlines;
1730 $line = "<span class=\"chunk_info\">$prefix ";
1731 for (my $i = 0; $i < @from_text; ++$i) {
1732 if ($from->{'href'}[$i]) {
1733 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1734 -class=>"list"}, $from_text[$i]);
1736 $line .= $from_text[$i];
1740 if ($to->{'href'}) {
1741 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1742 -class=>"list"}, $to_text);
1746 $line .= " $prefix</span>" .
1747 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1748 return "<div class=\"diff$diff_class\">$line</div>\n";
1750 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1753 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1754 # linked. Pass the hash of the tree/commit to snapshot.
1755 sub format_snapshot_links {
1757 my $num_fmts = @snapshot_fmts;
1758 if ($num_fmts > 1) {
1759 # A parenthesized list of links bearing format names.
1760 # e.g. "snapshot (_tar.gz_ _zip_)"
1761 return "snapshot (" . join(' ', map
1768 }, $known_snapshot_formats{$_}{'display'})
1769 , @snapshot_fmts) . ")";
1770 } elsif ($num_fmts == 1) {
1771 # A single "snapshot" link whose tooltip bears the format name.
1773 my ($fmt) = @snapshot_fmts;
1779 snapshot_format=>$fmt
1781 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1783 } else { # $num_fmts == 0
1788 ## ......................................................................
1789 ## functions returning values to be passed, perhaps after some
1790 ## transformation, to other functions; e.g. returning arguments to href()
1792 # returns hash to be passed to href to generate gitweb URL
1793 # in -title key it returns description of link
1795 my $format = shift || 'Atom';
1796 my %res = (action => lc($format));
1798 # feed links are possible only for project views
1799 return unless (defined $project);
1800 # some views should link to OPML, or to generic project feed,
1801 # or don't have specific feed yet (so they should use generic)
1802 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1805 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1806 # from tag links; this also makes possible to detect branch links
1807 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1808 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1811 # find log type for feed description (title)
1813 if (defined $file_name) {
1814 $type = "history of $file_name";
1815 $type .= "/" if ($action eq 'tree');
1816 $type .= " on '$branch'" if (defined $branch);
1818 $type = "log of $branch" if (defined $branch);
1821 $res{-title} = $type;
1822 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1823 $res{'file_name'} = $file_name;
1828 ## ----------------------------------------------------------------------
1829 ## git utility subroutines, invoking git commands
1831 # returns path to the core git executable and the --git-dir parameter as list
1833 return $GIT, '--git-dir='.$git_dir;
1836 # quote the given arguments for passing them to the shell
1837 # quote_command("command", "arg 1", "arg with ' and ! characters")
1838 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1839 # Try to avoid using this function wherever possible.
1842 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1845 # get HEAD ref of given project as hash
1846 sub git_get_head_hash {
1847 my $project = shift;
1848 my $o_git_dir = $git_dir;
1850 $git_dir = "$projectroot/$project";
1851 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1854 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1858 if (defined $o_git_dir) {
1859 $git_dir = $o_git_dir;
1864 # get type of given object
1868 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1870 close $fd or return;
1875 # repository configuration
1876 our $config_file = '';
1879 # store multiple values for single key as anonymous array reference
1880 # single values stored directly in the hash, not as [ <value> ]
1881 sub hash_set_multi {
1882 my ($hash, $key, $value) = @_;
1884 if (!exists $hash->{$key}) {
1885 $hash->{$key} = $value;
1886 } elsif (!ref $hash->{$key}) {
1887 $hash->{$key} = [ $hash->{$key}, $value ];
1889 push @{$hash->{$key}}, $value;
1893 # return hash of git project configuration
1894 # optionally limited to some section, e.g. 'gitweb'
1895 sub git_parse_project_config {
1896 my $section_regexp = shift;
1901 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1904 while (my $keyval = <$fh>) {
1906 my ($key, $value) = split(/\n/, $keyval, 2);
1908 hash_set_multi(\%config, $key, $value)
1909 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1916 # convert config value to boolean: 'true' or 'false'
1917 # no value, number > 0, 'true' and 'yes' values are true
1918 # rest of values are treated as false (never as error)
1919 sub config_to_bool {
1922 return 1 if !defined $val; # section.key
1924 # strip leading and trailing whitespace
1928 return (($val =~ /^\d+$/ && $val) || # section.key = 1
1929 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1932 # convert config value to simple decimal number
1933 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1934 # to be multiplied by 1024, 1048576, or 1073741824
1938 # strip leading and trailing whitespace
1942 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1944 # unknown unit is treated as 1
1945 return $num * ($unit eq 'g' ? 1073741824 :
1946 $unit eq 'm' ? 1048576 :
1947 $unit eq 'k' ? 1024 : 1);
1952 # convert config value to array reference, if needed
1953 sub config_to_multi {
1956 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1959 sub git_get_project_config {
1960 my ($key, $type) = @_;
1963 return unless ($key);
1964 $key =~ s/^gitweb\.//;
1965 return if ($key =~ m/\W/);
1968 if (defined $type) {
1971 unless ($type eq 'bool' || $type eq 'int');
1975 if (!defined $config_file ||
1976 $config_file ne "$git_dir/config") {
1977 %config = git_parse_project_config('gitweb');
1978 $config_file = "$git_dir/config";
1981 # check if config variable (key) exists
1982 return unless exists $config{"gitweb.$key"};
1985 if (!defined $type) {
1986 return $config{"gitweb.$key"};
1987 } elsif ($type eq 'bool') {
1988 # backward compatibility: 'git config --bool' returns true/false
1989 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1990 } elsif ($type eq 'int') {
1991 return config_to_int($config{"gitweb.$key"});
1993 return $config{"gitweb.$key"};
1996 # get hash of given path at given ref
1997 sub git_get_hash_by_path {
1999 my $path = shift || return undef;
2004 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2005 or die_error(500, "Open git-ls-tree failed");
2007 close $fd or return undef;
2009 if (!defined $line) {
2010 # there is no tree or hash given by $path at $base
2014 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2015 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2016 if (defined $type && $type ne $2) {
2017 # type doesn't match
2023 # get path of entry with given hash at given tree-ish (ref)
2024 # used to get 'from' filename for combined diff (merge commit) for renames
2025 sub git_get_path_by_hash {
2026 my $base = shift || return;
2027 my $hash = shift || return;
2031 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2033 while (my $line = <$fd>) {
2036 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2037 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2038 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2047 ## ......................................................................
2048 ## git utility functions, directly accessing git repository
2050 sub git_get_project_description {
2053 $git_dir = "$projectroot/$path";
2054 open my $fd, "$git_dir/description"
2055 or return git_get_project_config('description');
2058 if (defined $descr) {
2064 sub git_get_project_ctags {
2068 $git_dir = "$projectroot/$path";
2069 unless (opendir D, "$git_dir/ctags") {
2072 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir(D)) {
2073 open CT, $_ or next;
2077 my $ctag = $_; $ctag =~ s#.*/##;
2078 $ctags->{$ctag} = $val;
2084 sub git_populate_project_tagcloud {
2087 # First, merge different-cased tags; tags vote on casing
2089 foreach (keys %$ctags) {
2090 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2091 if (not $ctags_lc{lc $_}->{topcount}
2092 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2093 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2094 $ctags_lc{lc $_}->{topname} = $_;
2099 if (eval { require HTML::TagCloud; 1; }) {
2100 $cloud = HTML::TagCloud->new;
2101 foreach (sort keys %ctags_lc) {
2102 # Pad the title with spaces so that the cloud looks
2104 my $title = $ctags_lc{$_}->{topname};
2105 $title =~ s/ / /g;
2106 $title =~ s/^/ /g;
2107 $title =~ s/$/ /g;
2108 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2111 $cloud = \%ctags_lc;
2116 sub git_show_project_tagcloud {
2117 my ($cloud, $count) = @_;
2118 #print STDERR ref($cloud)."..\n";
2119 if (ref $cloud eq 'HTML::TagCloud') {
2120 return $cloud->html_and_css($count);
2122 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2123 return '<p align="center">' . join (', ', map {
2124 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2125 } splice(@tags, 0, $count)) . '</p>';
2129 sub git_get_project_url_list {
2132 $git_dir = "$projectroot/$path";
2133 open my $fd, "$git_dir/cloneurl"
2134 or return wantarray ?
2135 @{ config_to_multi(git_get_project_config('url')) } :
2136 config_to_multi(git_get_project_config('url'));
2137 my @git_project_url_list = map { chomp; $_ } <$fd>;
2140 return wantarray ? @git_project_url_list : \@git_project_url_list;
2143 our $gitweb_project_owner = undef;
2144 sub git_get_project_list_from_file {
2146 return if (defined $gitweb_project_owner);
2148 $gitweb_project_owner = {};
2149 # read from file (url-encoded):
2150 # 'git%2Fgit.git Linus+Torvalds'
2151 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2152 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2153 if (-f $projects_list) {
2154 open (my $fd , $projects_list);
2155 while (my $line = <$fd>) {
2157 my ($pr, $ow) = split ' ', $line;
2158 $pr = unescape($pr);
2159 $ow = unescape($ow);
2160 $gitweb_project_owner->{$pr} = to_utf8($ow);
2166 sub git_get_project_owner {
2167 my $project = shift;
2170 return undef unless $project;
2171 $git_dir = "$projectroot/$project";
2173 if (!defined $gitweb_project_owner) {
2174 git_get_project_list_from_file();
2177 if (exists $gitweb_project_owner->{$project}) {
2178 $owner = $gitweb_project_owner->{$project};
2180 if (!defined $owner){
2181 $owner = git_get_project_config('owner');
2183 if (!defined $owner) {
2184 $owner = get_file_owner("$git_dir");
2190 sub git_get_last_activity {
2194 $git_dir = "$projectroot/$path";
2195 open($fd, "-|", git_cmd(), 'for-each-ref',
2196 '--format=%(committer)',
2197 '--sort=-committerdate',
2199 'refs/heads') or return;
2200 my $most_recent = <$fd>;
2201 close $fd or return;
2202 if (defined $most_recent &&
2203 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2205 my $age = time - $timestamp;
2206 return ($age, age_string($age));
2208 return (undef, undef);
2211 sub git_get_references {
2212 my $type = shift || "";
2214 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2215 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2216 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2217 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2220 while (my $line = <$fd>) {
2222 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2223 if (defined $refs{$1}) {
2224 push @{$refs{$1}}, $2;
2230 close $fd or return;
2234 sub git_get_rev_name_tags {
2235 my $hash = shift || return undef;
2237 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2239 my $name_rev = <$fd>;
2242 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2245 # catches also '$hash undefined' output
2250 ## ----------------------------------------------------------------------
2251 ## parse to hash functions
2255 my $tz = shift || "-0000";
2258 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2259 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2260 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2261 $date{'hour'} = $hour;
2262 $date{'minute'} = $min;
2263 $date{'mday'} = $mday;
2264 $date{'day'} = $days[$wday];
2265 $date{'month'} = $months[$mon];
2266 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2267 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2268 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2269 $mday, $months[$mon], $hour ,$min;
2270 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2271 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2273 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2274 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2275 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2276 $date{'hour_local'} = $hour;
2277 $date{'minute_local'} = $min;
2278 $date{'tz_local'} = $tz;
2279 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2280 1900+$year, $mon+1, $mday,
2281 $hour, $min, $sec, $tz);
2290 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2291 $tag{'id'} = $tag_id;
2292 while (my $line = <$fd>) {
2294 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2295 $tag{'object'} = $1;
2296 } elsif ($line =~ m/^type (.+)$/) {
2298 } elsif ($line =~ m/^tag (.+)$/) {
2300 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2301 $tag{'author'} = $1;
2304 } elsif ($line =~ m/--BEGIN/) {
2305 push @comment, $line;
2307 } elsif ($line eq "") {
2311 push @comment, <$fd>;
2312 $tag{'comment'} = \@comment;
2313 close $fd or return;
2314 if (!defined $tag{'name'}) {
2320 sub parse_commit_text {
2321 my ($commit_text, $withparents) = @_;
2322 my @commit_lines = split '\n', $commit_text;
2325 pop @commit_lines; # Remove '\0'
2327 if (! @commit_lines) {
2331 my $header = shift @commit_lines;
2332 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2335 ($co{'id'}, my @parents) = split ' ', $header;
2336 while (my $line = shift @commit_lines) {
2337 last if $line eq "\n";
2338 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2340 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2342 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2344 $co{'author_epoch'} = $2;
2345 $co{'author_tz'} = $3;
2346 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2347 $co{'author_name'} = $1;
2348 $co{'author_email'} = $2;
2350 $co{'author_name'} = $co{'author'};
2352 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2353 $co{'committer'} = $1;
2354 $co{'committer_epoch'} = $2;
2355 $co{'committer_tz'} = $3;
2356 $co{'committer_name'} = $co{'committer'};
2357 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2358 $co{'committer_name'} = $1;
2359 $co{'committer_email'} = $2;
2361 $co{'committer_name'} = $co{'committer'};
2365 if (!defined $co{'tree'}) {
2368 $co{'parents'} = \@parents;
2369 $co{'parent'} = $parents[0];
2371 foreach my $title (@commit_lines) {
2374 $co{'title'} = chop_str($title, 80, 5);
2375 # remove leading stuff of merges to make the interesting part visible
2376 if (length($title) > 50) {
2377 $title =~ s/^Automatic //;
2378 $title =~ s/^merge (of|with) /Merge ... /i;
2379 if (length($title) > 50) {
2380 $title =~ s/(http|rsync):\/\///;
2382 if (length($title) > 50) {
2383 $title =~ s/(master|www|rsync)\.//;
2385 if (length($title) > 50) {
2386 $title =~ s/kernel.org:?//;
2388 if (length($title) > 50) {
2389 $title =~ s/\/pub\/scm//;
2392 $co{'title_short'} = chop_str($title, 50, 5);
2396 if (! defined $co{'title'} || $co{'title'} eq "") {
2397 $co{'title'} = $co{'title_short'} = '(no commit message)';
2399 # remove added spaces
2400 foreach my $line (@commit_lines) {
2403 $co{'comment'} = \@commit_lines;
2405 my $age = time - $co{'committer_epoch'};
2407 $co{'age_string'} = age_string($age);
2408 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2409 if ($age > 60*60*24*7*2) {
2410 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2411 $co{'age_string_age'} = $co{'age_string'};
2413 $co{'age_string_date'} = $co{'age_string'};
2414 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2420 my ($commit_id) = @_;
2425 open my $fd, "-|", git_cmd(), "rev-list",
2431 or die_error(500, "Open git-rev-list failed");
2432 %co = parse_commit_text(<$fd>, 1);
2439 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2447 open my $fd, "-|", git_cmd(), "rev-list",
2450 ("--max-count=" . $maxcount),
2451 ("--skip=" . $skip),
2455 ($filename ? ($filename) : ())
2456 or die_error(500, "Open git-rev-list failed");
2457 while (my $line = <$fd>) {
2458 my %co = parse_commit_text($line);
2463 return wantarray ? @cos : \@cos;
2466 # parse line of git-diff-tree "raw" output
2467 sub parse_difftree_raw_line {
2471 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2472 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2473 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2474 $res{'from_mode'} = $1;
2475 $res{'to_mode'} = $2;
2476 $res{'from_id'} = $3;
2478 $res{'status'} = $5;
2479 $res{'similarity'} = $6;
2480 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2481 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2483 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2486 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2487 # combined diff (for merge commit)
2488 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2489 $res{'nparents'} = length($1);
2490 $res{'from_mode'} = [ split(' ', $2) ];
2491 $res{'to_mode'} = pop @{$res{'from_mode'}};
2492 $res{'from_id'} = [ split(' ', $3) ];
2493 $res{'to_id'} = pop @{$res{'from_id'}};
2494 $res{'status'} = [ split('', $4) ];
2495 $res{'to_file'} = unquote($5);
2497 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2498 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2499 $res{'commit'} = $1;
2502 return wantarray ? %res : \%res;
2505 # wrapper: return parsed line of git-diff-tree "raw" output
2506 # (the argument might be raw line, or parsed info)
2507 sub parsed_difftree_line {
2508 my $line_or_ref = shift;
2510 if (ref($line_or_ref) eq "HASH") {
2511 # pre-parsed (or generated by hand)
2512 return $line_or_ref;
2514 return parse_difftree_raw_line($line_or_ref);
2518 # parse line of git-ls-tree output
2519 sub parse_ls_tree_line ($;%) {
2524 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2525 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2533 $res{'name'} = unquote($4);
2536 return wantarray ? %res : \%res;
2539 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2540 sub parse_from_to_diffinfo {
2541 my ($diffinfo, $from, $to, @parents) = @_;
2543 if ($diffinfo->{'nparents'}) {
2545 $from->{'file'} = [];
2546 $from->{'href'} = [];
2547 fill_from_file_info($diffinfo, @parents)
2548 unless exists $diffinfo->{'from_file'};
2549 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2550 $from->{'file'}[$i] =
2551 defined $diffinfo->{'from_file'}[$i] ?
2552 $diffinfo->{'from_file'}[$i] :
2553 $diffinfo->{'to_file'};
2554 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2555 $from->{'href'}[$i] = href(action=>"blob",
2556 hash_base=>$parents[$i],
2557 hash=>$diffinfo->{'from_id'}[$i],
2558 file_name=>$from->{'file'}[$i]);
2560 $from->{'href'}[$i] = undef;
2564 # ordinary (not combined) diff
2565 $from->{'file'} = $diffinfo->{'from_file'};
2566 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2567 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2568 hash=>$diffinfo->{'from_id'},
2569 file_name=>$from->{'file'});
2571 delete $from->{'href'};
2575 $to->{'file'} = $diffinfo->{'to_file'};
2576 if (!is_deleted($diffinfo)) { # file exists in result
2577 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2578 hash=>$diffinfo->{'to_id'},
2579 file_name=>$to->{'file'});
2581 delete $to->{'href'};
2585 ## ......................................................................
2586 ## parse to array of hashes functions
2588 sub git_get_heads_list {
2592 open my $fd, '-|', git_cmd(), 'for-each-ref',
2593 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2594 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2597 while (my $line = <$fd>) {
2601 my ($refinfo, $committerinfo) = split(/\0/, $line);
2602 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2603 my ($committer, $epoch, $tz) =
2604 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2605 $ref_item{'fullname'} = $name;
2606 $name =~ s!^refs/heads/!!;
2608 $ref_item{'name'} = $name;
2609 $ref_item{'id'} = $hash;
2610 $ref_item{'title'} = $title || '(no commit message)';
2611 $ref_item{'epoch'} = $epoch;
2613 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2615 $ref_item{'age'} = "unknown";
2618 push @headslist, \%ref_item;
2622 return wantarray ? @headslist : \@headslist;
2625 sub git_get_tags_list {
2629 open my $fd, '-|', git_cmd(), 'for-each-ref',
2630 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2631 '--format=%(objectname) %(objecttype) %(refname) '.
2632 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2635 while (my $line = <$fd>) {
2639 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2640 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2641 my ($creator, $epoch, $tz) =
2642 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2643 $ref_item{'fullname'} = $name;
2644 $name =~ s!^refs/tags/!!;
2646 $ref_item{'type'} = $type;
2647 $ref_item{'id'} = $id;
2648 $ref_item{'name'} = $name;
2649 if ($type eq "tag") {
2650 $ref_item{'subject'} = $title;
2651 $ref_item{'reftype'} = $reftype;
2652 $ref_item{'refid'} = $refid;
2654 $ref_item{'reftype'} = $type;
2655 $ref_item{'refid'} = $id;
2658 if ($type eq "tag" || $type eq "commit") {
2659 $ref_item{'epoch'} = $epoch;
2661 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2663 $ref_item{'age'} = "unknown";
2667 push @tagslist, \%ref_item;
2671 return wantarray ? @tagslist : \@tagslist;
2674 ## ----------------------------------------------------------------------
2675 ## filesystem-related functions
2677 sub get_file_owner {
2680 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2681 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2682 if (!defined $gcos) {
2686 $owner =~ s/[,;].*$//;
2687 return to_utf8($owner);
2690 # assume that file exists
2692 my $filename = shift;
2694 open my $fd, '<', $filename;
2695 return join '', map to_utf8($_), <$fd>;
2698 ## ......................................................................
2699 ## mimetype related functions
2701 sub mimetype_guess_file {
2702 my $filename = shift;
2703 my $mimemap = shift;
2704 -r $mimemap or return undef;
2707 open(MIME, $mimemap) or return undef;
2709 next if m/^#/; # skip comments
2710 my ($mime, $exts) = split(/\t+/);
2711 if (defined $exts) {
2712 my @exts = split(/\s+/, $exts);
2713 foreach my $ext (@exts) {
2714 $mimemap{$ext} = $mime;
2720 $filename =~ /\.([^.]*)$/;
2721 return $mimemap{$1};
2724 sub mimetype_guess {
2725 my $filename = shift;
2727 $filename =~ /\./ or return undef;
2729 if ($mimetypes_file) {
2730 my $file = $mimetypes_file;
2731 if ($file !~ m!^/!) { # if it is relative path
2732 # it is relative to project
2733 $file = "$projectroot/$project/$file";
2735 $mime = mimetype_guess_file($filename, $file);
2737 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2743 my $filename = shift;
2746 my $mime = mimetype_guess($filename);
2747 $mime and return $mime;
2751 return $default_blob_plain_mimetype unless $fd;
2754 return 'text/plain';
2755 } elsif (! $filename) {
2756 return 'application/octet-stream';
2757 } elsif ($filename =~ m/\.png$/i) {
2759 } elsif ($filename =~ m/\.gif$/i) {
2761 } elsif ($filename =~ m/\.jpe?g$/i) {
2762 return 'image/jpeg';
2764 return 'application/octet-stream';
2768 sub blob_contenttype {
2769 my ($fd, $file_name, $type) = @_;
2771 $type ||= blob_mimetype($fd, $file_name);
2772 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2773 $type .= "; charset=$default_text_plain_charset";
2779 # die_error(<http_status_code>, <error_message>)
2780 # Example: die_error(404, 'Hash not found')
2781 # By convention, use the following status codes (as defined in RFC 2616):
2782 # 400: Invalid or missing CGI parameters, or
2783 # requested object exists but has wrong type.
2784 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
2785 # this server or project.
2786 # 404: Requested object/revision/project doesn't exist.
2787 # 500: The server isn't configured properly, or
2788 # an internal error occurred (e.g. failed assertions caused by bugs), or
2789 # an unknown error occurred (e.g. the git binary died unexpectedly).
2791 my $status = shift || 500;
2792 my $error = shift || "Internal server error";
2794 my %http_responses = (400 => '400 Bad Request',
2795 403 => '403 Forbidden',
2796 404 => '404 Not Found',
2797 500 => '500 Internal Server Error');
2798 $c->response->status($http_responses{$status});
2800 $c->stash->{content} = <<EOF;
2801 <div class="page_body">
2807 die bless { $status => $http_responses{$status}, err => $error };
2810 ## ----------------------------------------------------------------------
2811 ## functions printing or outputting HTML: navigation
2813 sub git_print_page_nav {
2814 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2815 $extra = '' if !defined $extra; # pager or formats
2817 my @navs = qw(summary shortlog log commit commitdiff tree);
2819 @navs = grep { $_ ne $suppress } @navs;
2822 my %arg = map { $_ => {action=>$_} } @navs;
2823 if (defined $head) {
2824 for (qw(commit commitdiff)) {
2825 $arg{$_}{'hash'} = $head;
2827 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2828 for (qw(shortlog log)) {
2829 $arg{$_}{'hash'} = $head;
2834 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2835 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2837 my @actions = gitweb_get_feature('actions');
2840 'n' => $project, # project name
2841 'f' => $git_dir, # project path within filesystem
2842 'h' => $treehead || '', # current hash ('h' parameter)
2843 'b' => $treebase || '', # hash base ('hb' parameter)
2846 my ($label, $link, $pos) = splice(@actions,0,3);
2848 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
2850 $link =~ s/%([%nfhb])/$repl{$1}/g;
2851 $arg{$label}{'_href'} = $link;
2854 $c->stash->{page_nav} = 1;
2855 $c->stash->{nav_links} =
2857 map { $_ eq $current ?
2858 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
2860 $c->stash->{extra} = $extra;
2863 sub format_paging_nav {
2864 my ($action, $hash, $head, $page, $has_next_link) = @_;
2868 if ($hash ne $head || $page) {
2869 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2871 $paging_nav .= "HEAD";
2875 $paging_nav .= " ⋅ " .
2876 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2877 -accesskey => "p", -title => "Alt-p"}, "prev");
2879 $paging_nav .= " ⋅ prev";
2882 if ($has_next_link) {
2883 $paging_nav .= " ⋅ " .
2884 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2885 -accesskey => "n", -title => "Alt-n"}, "next");
2887 $paging_nav .= " ⋅ next";
2893 ## ......................................................................
2894 ## functions printing or outputting HTML: div
2896 sub git_print_header_div {
2897 my ($action, $title, $hash, $hash_base) = @_;
2900 $args{'action'} = $action;
2901 $args{'hash'} = $hash if $hash;
2902 $args{'hash_base'} = $hash_base if $hash_base;
2904 print q[<div class="header">],
2905 $cgi->a({-href => href(%args), -class => "title"},
2906 $title ? $title : $action),
2910 sub git_print_authorship {
2913 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2914 print "<div class=\"author_date\">" .
2915 esc_html($co->{'author_name'}) .
2917 if ($ad{'hour_local'} < 6) {
2918 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2919 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2921 printf(" (%02d:%02d %s)",
2922 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2927 sub git_print_page_path {
2933 print "<div class=\"page_path\">";
2934 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2935 -title => 'tree root'}, to_utf8("[$project]"));
2937 if (defined $name) {
2938 my @dirname = split '/', $name;
2939 my $basename = pop @dirname;
2942 foreach my $dir (@dirname) {
2943 $fullname .= ($fullname ? '/' : '') . $dir;
2944 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2946 -title => $fullname}, esc_path($dir));
2949 if (defined $type && $type eq 'blob') {
2950 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2952 -title => $name}, esc_path($basename));
2953 } elsif (defined $type && $type eq 'tree') {
2954 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2956 -title => $name}, esc_path($basename));
2959 print esc_path($basename);
2962 print "<br/></div>\n";
2965 # sub git_print_log (\@;%) {
2966 sub git_print_log ($;%) {
2970 if ($opts{'-remove_title'}) {
2971 # remove title, i.e. first line of log
2974 # remove leading empty lines
2975 while (defined $log->[0] && $log->[0] eq "") {
2982 foreach my $line (@$log) {
2983 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2986 if (! $opts{'-remove_signoff'}) {
2987 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2990 # remove signoff lines
2997 # print only one empty line
2998 # do not print empty line after signoff
3000 next if ($empty || $signoff);
3006 print format_log_line_html($line) . "<br/>\n";
3009 if ($opts{'-final_empty_line'}) {
3010 # end with single empty line
3011 print "<br/>\n" unless $empty;
3015 # return link target (what link points to)
3016 sub git_get_link_target {
3021 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3025 $link_target = <$fd>;
3030 return $link_target;
3033 # given link target, and the directory (basedir) the link is in,
3034 # return target of link relative to top directory (top tree);
3035 # return undef if it is not possible (including absolute links).
3036 sub normalize_link_target {
3037 my ($link_target, $basedir, $hash_base) = @_;
3039 # we can normalize symlink target only if $hash_base is provided
3040 return unless $hash_base;
3042 # absolute symlinks (beginning with '/') cannot be normalized
3043 return if (substr($link_target, 0, 1) eq '/');
3045 # normalize link target to path from top (root) tree (dir)
3048 $path = $basedir . '/' . $link_target;
3050 # we are in top (root) tree (dir)
3051 $path = $link_target;
3054 # remove //, /./, and /../
3056 foreach my $part (split('/', $path)) {
3057 # discard '.' and ''
3058 next if (!$part || $part eq '.');
3060 if ($part eq '..') {
3064 # link leads outside repository (outside top dir)
3068 push @path_parts, $part;
3071 $path = join('/', @path_parts);
3076 # print tree entry (row of git_tree), but without encompassing <tr> element
3077 sub git_print_tree_entry {
3078 my ($t, $basedir, $hash_base, $have_blame) = @_;
3081 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3083 # The format of a table row is: mode list link. Where mode is
3084 # the mode of the entry, list is the name of the entry, an href,
3085 # and link is the action links of the entry.
3087 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3088 if ($t->{'type'} eq "blob") {
3089 print "<td class=\"list\">" .
3090 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3091 file_name=>"$basedir$t->{'name'}", %base_key),
3092 -class => "list"}, esc_path($t->{'name'}));
3093 if (S_ISLNK(oct $t->{'mode'})) {
3094 my $link_target = git_get_link_target($t->{'hash'});
3096 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3097 if (defined $norm_target) {
3099 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3100 file_name=>$norm_target),
3101 -title => $norm_target}, esc_path($link_target));
3103 print " -> " . esc_path($link_target);
3108 print "<td class=\"link\">";
3109 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3110 file_name=>"$basedir$t->{'name'}", %base_key)},
3114 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3115 file_name=>"$basedir$t->{'name'}", %base_key)},
3118 if (defined $hash_base) {
3120 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3121 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3125 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3126 file_name=>"$basedir$t->{'name'}")},
3130 } elsif ($t->{'type'} eq "tree") {
3131 print "<td class=\"list\">";
3132 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3133 file_name=>"$basedir$t->{'name'}", %base_key)},
3134 esc_path($t->{'name'}));
3136 print "<td class=\"link\">";
3137 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3138 file_name=>"$basedir$t->{'name'}", %base_key)},
3140 if (defined $hash_base) {
3142 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3143 file_name=>"$basedir$t->{'name'}")},
3148 # unknown object: we can only present history for it
3149 # (this includes 'commit' object, i.e. submodule support)
3150 print "<td class=\"list\">" .
3151 esc_path($t->{'name'}) .
3153 print "<td class=\"link\">";
3154 if (defined $hash_base) {
3155 print $cgi->a({-href => href(action=>"history",
3156 hash_base=>$hash_base,
3157 file_name=>"$basedir$t->{'name'}")},
3164 ## ......................................................................
3165 ## functions printing large fragments of HTML
3167 # get pre-image filenames for merge (combined) diff
3168 sub fill_from_file_info {
3169 my ($diff, @parents) = @_;
3171 $diff->{'from_file'} = [ ];
3172 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3173 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3174 if ($diff->{'status'}[$i] eq 'R' ||
3175 $diff->{'status'}[$i] eq 'C') {
3176 $diff->{'from_file'}[$i] =
3177 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3184 # is current raw difftree line of file deletion
3186 my $diffinfo = shift;
3188 return $diffinfo->{'to_id'} eq ('0' x 40);
3191 # does patch correspond to [previous] difftree raw line
3192 # $diffinfo - hashref of parsed raw diff format
3193 # $patchinfo - hashref of parsed patch diff format
3194 # (the same keys as in $diffinfo)
3195 sub is_patch_split {
3196 my ($diffinfo, $patchinfo) = @_;
3198 return defined $diffinfo && defined $patchinfo
3199 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3203 sub git_difftree_body {
3204 my ($difftree, $hash, @parents) = @_;
3205 my ($parent) = $parents[0];
3206 my $have_blame = gitweb_check_feature('blame');
3207 print "<div class=\"list_head\">\n";
3208 if ($#{$difftree} > 10) {
3209 print(($#{$difftree} + 1) . " files changed:\n");
3213 print "<table class=\"" .
3214 (@parents > 1 ? "combined " : "") .
3217 # header only for combined diff in 'commitdiff' view
3218 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3221 print "<thead><tr>\n" .
3222 "<th></th><th></th>\n"; # filename, patchN link
3223 for (my $i = 0; $i < @parents; $i++) {
3224 my $par = $parents[$i];
3226 $cgi->a({-href => href(action=>"commitdiff",
3227 hash=>$hash, hash_parent=>$par),
3228 -title => 'commitdiff to parent number ' .
3229 ($i+1) . ': ' . substr($par,0,7)},
3233 print "</tr></thead>\n<tbody>\n";
3238 foreach my $line (@{$difftree}) {
3239 my $diff = parsed_difftree_line($line);
3242 print "<tr class=\"dark\">\n";
3244 print "<tr class=\"light\">\n";
3248 if (exists $diff->{'nparents'}) { # combined diff
3250 fill_from_file_info($diff, @parents)
3251 unless exists $diff->{'from_file'};
3253 if (!is_deleted($diff)) {
3254 # file exists in the result (child) commit
3256 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3257 file_name=>$diff->{'to_file'},
3259 -class => "list"}, esc_path($diff->{'to_file'})) .
3263 esc_path($diff->{'to_file'}) .
3267 if ($action eq 'commitdiff') {
3270 print "<td class=\"link\">" .
3271 $cgi->a({-href => "#patch$patchno"}, "patch") .
3276 my $has_history = 0;
3277 my $not_deleted = 0;
3278 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3279 my $hash_parent = $parents[$i];
3280 my $from_hash = $diff->{'from_id'}[$i];
3281 my $from_path = $diff->{'from_file'}[$i];
3282 my $status = $diff->{'status'}[$i];
3284 $has_history ||= ($status ne 'A');
3285 $not_deleted ||= ($status ne 'D');
3287 if ($status eq 'A') {
3288 print "<td class=\"link\" align=\"right\"> | </td>\n";
3289 } elsif ($status eq 'D') {
3290 print "<td class=\"link\">" .
3291 $cgi->a({-href => href(action=>"blob",
3294 file_name=>$from_path)},
3298 if ($diff->{'to_id'} eq $from_hash) {
3299 print "<td class=\"link nochange\">";
3301 print "<td class=\"link\">";
3303 print $cgi->a({-href => href(action=>"blobdiff",
3304 hash=>$diff->{'to_id'},
3305 hash_parent=>$from_hash,
3307 hash_parent_base=>$hash_parent,
3308 file_name=>$diff->{'to_file'},
3309 file_parent=>$from_path)},
3315 print "<td class=\"link\">";
3317 print $cgi->a({-href => href(action=>"blob",
3318 hash=>$diff->{'to_id'},
3319 file_name=>$diff->{'to_file'},
3322 print " | " if ($has_history);
3325 print $cgi->a({-href => href(action=>"history",
3326 file_name=>$diff->{'to_file'},
3333 next; # instead of 'else' clause, to avoid extra indent
3335 # else ordinary diff
3337 my ($to_mode_oct, $to_mode_str, $to_file_type);
3338 my ($from_mode_oct, $from_mode_str, $from_file_type);
3339 if ($diff->{'to_mode'} ne ('0' x 6)) {
3340 $to_mode_oct = oct $diff->{'to_mode'};
3341 if (S_ISREG($to_mode_oct)) { # only for regular file
3342 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3344 $to_file_type = file_type($diff->{'to_mode'});
3346 if ($diff->{'from_mode'} ne ('0' x 6)) {
3347 $from_mode_oct = oct $diff->{'from_mode'};
3348 if (S_ISREG($to_mode_oct)) { # only for regular file
3349 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3351 $from_file_type = file_type($diff->{'from_mode'});
3354 if ($diff->{'status'} eq "A") { # created
3355 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3356 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3357 $mode_chng .= "]</span>";
3359 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3360 hash_base=>$hash, file_name=>$diff->{'file'}),
3361 -class => "list"}, esc_path($diff->{'file'}));
3363 print "<td>$mode_chng</td>\n";
3364 print "<td class=\"link\">";
3365 if ($action eq 'commitdiff') {
3368 print $cgi->a({-href => "#patch$patchno"}, "patch");
3371 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3372 hash_base=>$hash, file_name=>$diff->{'file'})},
3376 } elsif ($diff->{'status'} eq "D") { # deleted
3377 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3379 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3380 hash_base=>$parent, file_name=>$diff->{'file'}),
3381 -class => "list"}, esc_path($diff->{'file'}));
3383 print "<td>$mode_chng</td>\n";
3384 print "<td class=\"link\">";
3385 if ($action eq 'commitdiff') {
3388 print $cgi->a({-href => "#patch$patchno"}, "patch");
3391 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3392 hash_base=>$parent, file_name=>$diff->{'file'})},
3395 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3396 file_name=>$diff->{'file'})},
3399 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3400 file_name=>$diff->{'file'})},
3404 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3405 my $mode_chnge = "";
3406 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3407 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3408 if ($from_file_type ne $to_file_type) {
3409 $mode_chnge .= " from $from_file_type to $to_file_type";
3411 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3412 if ($from_mode_str && $to_mode_str) {
3413 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3414 } elsif ($to_mode_str) {
3415 $mode_chnge .= " mode: $to_mode_str";
3418 $mode_chnge .= "]</span>\n";
3421 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3422 hash_base=>$hash, file_name=>$diff->{'file'}),
3423 -class => "list"}, esc_path($diff->{'file'}));
3425 print "<td>$mode_chnge</td>\n";
3426 print "<td class=\"link\">";
3427 if ($action eq 'commitdiff') {
3430 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3432 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3433 # "commit" view and modified file (not onlu mode changed)
3434 print $cgi->a({-href => href(action=>"blobdiff",
3435 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3436 hash_base=>$hash, hash_parent_base=>$parent,
3437 file_name=>$diff->{'file'})},
3441 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3442 hash_base=>$hash, file_name=>$diff->{'file'})},
3445 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3446 file_name=>$diff->{'file'})},
3449 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3450 file_name=>$diff->{'file'})},
3454 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3455 my %status_name = ('R' => 'moved', 'C' => 'copied');
3456 my $nstatus = $status_name{$diff->{'status'}};
3458 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3459 # mode also for directories, so we cannot use $to_mode_str
3460 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3463 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3464 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3465 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3466 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3467 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3468 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3469 -class => "list"}, esc_path($diff->{'from_file'})) .
3470 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3471 "<td class=\"link\">";
3472 if ($action eq 'commitdiff') {
3475 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3477 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3478 # "commit" view and modified file (not only pure rename or copy)
3479 print $cgi->a({-href => href(action=>"blobdiff",
3480 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3481 hash_base=>$hash, hash_parent_base=>$parent,
3482 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3486 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3487 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3490 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3491 file_name=>$diff->{'to_file'})},
3494 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3495 file_name=>$diff->{'to_file'})},
3499 } # we should not encounter Unmerged (U) or Unknown (X) status
3502 print "</tbody>" if $has_header;
3506 sub git_patchset_body {
3507 my ($fd, $difftree, $hash, @hash_parents) = @_;
3508 my ($hash_parent) = $hash_parents[0];
3510 my $is_combined = (@hash_parents > 1);
3512 my $patch_number = 0;
3518 print "<div class=\"patchset\">\n";
3520 # skip to first patch
3521 while ($patch_line = <$fd>) {
3524 last if ($patch_line =~ m/^diff /);
3528 while ($patch_line) {
3530 # parse "git diff" header line
3531 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3532 # $1 is from_name, which we do not use
3533 $to_name = unquote($2);
3534 $to_name =~ s!^b/!!;
3535 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3536 # $1 is 'cc' or 'combined', which we do not use
3537 $to_name = unquote($2);
3542 # check if current patch belong to current raw line
3543 # and parse raw git-diff line if needed
3544 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3545 # this is continuation of a split patch
3546 print "<div class=\"patch cont\">\n";
3548 # advance raw git-diff output if needed
3549 $patch_idx++ if defined $diffinfo;
3551 # read and prepare patch information
3552 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3554 # compact combined diff output can have some patches skipped
3555 # find which patch (using pathname of result) we are at now;
3557 while ($to_name ne $diffinfo->{'to_file'}) {
3558 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3559 format_diff_cc_simplified($diffinfo, @hash_parents) .
3560 "</div>\n"; # class="patch"
3565 last if $patch_idx > $#$difftree;
3566 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3570 # modifies %from, %to hashes
3571 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3573 # this is first patch for raw difftree line with $patch_idx index
3574 # we index @$difftree array from 0, but number patches from 1
3575 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3579 #assert($patch_line =~ m/^diff /) if DEBUG;
3580 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3582 # print "git diff" header
3583 print format_git_diff_header_line($patch_line, $diffinfo,
3586 # print extended diff header
3587 print "<div class=\"diff extended_header\">\n";
3589 while ($patch_line = <$fd>) {
3592 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3594 print format_extended_diff_header_line($patch_line, $diffinfo,
3597 print "</div>\n"; # class="diff extended_header"
3599 # from-file/to-file diff header
3600 if (! $patch_line) {
3601 print "</div>\n"; # class="patch"
3604 next PATCH if ($patch_line =~ m/^diff /);
3605 #assert($patch_line =~ m/^---/) if DEBUG;
3607 my $last_patch_line = $patch_line;
3608 $patch_line = <$fd>;
3610 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3612 print format_diff_from_to_header($last_patch_line, $patch_line,
3613 $diffinfo, \%from, \%to,
3618 while ($patch_line = <$fd>) {
3621 next PATCH if ($patch_line =~ m/^diff /);
3623 print format_diff_line($patch_line, \%from, \%to);
3627 print "</div>\n"; # class="patch"
3630 # for compact combined (--cc) format, with chunk and patch simpliciaction
3631 # patchset might be empty, but there might be unprocessed raw lines
3632 for (++$patch_idx if $patch_number > 0;
3633 $patch_idx < @$difftree;
3635 # read and prepare patch information
3636 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3638 # generate anchor for "patch" links in difftree / whatchanged part
3639 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3640 format_diff_cc_simplified($diffinfo, @hash_parents) .
3641 "</div>\n"; # class="patch"
3646 if ($patch_number == 0) {
3647 if (@hash_parents > 1) {
3648 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3650 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3654 print "</div>\n"; # class="patchset"
3657 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3659 # fills project list info (age, description, owner, forks) for each
3660 # project in the list, removing invalid projects from returned list
3661 # NOTE: modifies $projlist, but does not remove entries from it
3662 sub fill_project_list_info {
3663 my ($projlist, $check_forks) = @_;
3666 my $show_ctags = gitweb_check_feature('ctags');
3668 foreach my $pr (@$projlist) {
3669 my (@activity) = git_get_last_activity($pr->{'path'});
3670 unless (@activity) {
3673 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3674 if (!defined $pr->{'descr'}) {
3675 my $descr = git_get_project_description($pr->{'path'}) || "";
3676 $descr = to_utf8($descr);
3677 $pr->{'descr_long'} = $descr;
3678 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3680 if (!defined $pr->{'owner'}) {
3681 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3684 my $pname = $pr->{'path'};
3685 if (($pname =~ s/\.git$//) &&
3686 ($pname !~ /\/$/) &&
3687 (-d "$projectroot/$pname")) {
3688 $pr->{'forks'} = "-d $projectroot/$pname";
3693 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
3694 push @projects, $pr;
3700 # print 'sort by' <th> element, generating 'sort by $name' replay link
3701 # if that order is not selected
3703 my ($name, $order, $header) = @_;
3704 $header ||= ucfirst($name);
3706 if ($order eq $name) {
3707 print "<th>$header</th>\n";
3710 $cgi->a({-href => href(-replay=>1, order=>$name),
3711 -class => "header"}, $header) .
3716 sub git_project_list_body {
3717 # actually uses global variable $project
3718 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3720 my $check_forks = gitweb_check_feature('forks');
3721 my @projects = fill_project_list_info($projlist, $check_forks);
3723 $order ||= $default_projects_order;
3724 $from = 0 unless defined $from;
3725 $to = $#projects if (!defined $to || $#projects < $to);
3728 project => { key => 'path', type => 'str' },
3729 descr => { key => 'descr_long', type => 'str' },
3730 owner => { key => 'owner', type => 'str' },
3731 age => { key => 'age', type => 'num' }
3733 my $oi = $order_info{$order};
3734 if ($oi->{'type'} eq 'str') {
3735 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
3737 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
3740 my $show_ctags = gitweb_check_feature('ctags');
3743 foreach my $p (@projects) {
3744 foreach my $ct (keys %{$p->{'ctags'}}) {
3745 $ctags{$ct} += $p->{'ctags'}->{$ct};
3748 my $cloud = git_populate_project_tagcloud(\%ctags);
3749 print git_show_project_tagcloud($cloud, 64);
3752 print "<table class=\"project_list\">\n";
3753 unless ($no_header) {
3756 print "<th></th>\n";
3758 print_sort_th('project', $order, 'Project');
3759 print_sort_th('descr', $order, 'Description');
3760 print_sort_th('owner', $order, 'Owner');
3761 print_sort_th('age', $order, 'Last Change');
3762 print "<th></th>\n" . # for links
3766 my $tagfilter = $c->req->param('by_tag');
3767 for (my $i = $from; $i <= $to; $i++) {
3768 my $pr = $projects[$i];
3770 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
3771 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
3772 and not $pr->{'descr_long'} =~ /$searchtext/;
3773 # Weed out forks or non-matching entries of search
3775 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
3776 $forkbase="^$forkbase" if $forkbase;
3777 next if not $searchtext and not $tagfilter and $show_ctags
3778 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
3782 print "<tr class=\"dark\">\n";
3784 print "<tr class=\"light\">\n";
3789 if ($pr->{'forks'}) {
3790 print "<!-- $pr->{'forks'} -->\n";
3791 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3795 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3796 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3797 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3798 -class => "list", -title => $pr->{'descr_long'}},
3799 esc_html($pr->{'descr'})) . "</td>\n" .
3800 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3801 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3802 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3803 "<td class=\"link\">" .
3804 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3805 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3806 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3807 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3808 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3812 if (defined $extra) {
3815 print "<td></td>\n";
3817 print "<td colspan=\"5\">$extra</td>\n" .
3823 sub git_shortlog_body {
3824 # uses global variable $project
3825 my ($commitlist, $from, $to, $refs, $extra) = @_;
3827 $from = 0 unless defined $from;
3828 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3830 print "<table class=\"shortlog\">\n";
3832 for (my $i = $from; $i <= $to; $i++) {
3833 my %co = %{$commitlist->[$i]};
3834 my $commit = $co{'id'};
3835 my $ref = format_ref_marker($refs, $commit);
3837 print "<tr class=\"dark\">\n";
3839 print "<tr class=\"light\">\n";
3842 my $author = chop_and_escape_str($co{'author_name'}, 10);
3843 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3844 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3845 "<td><i>" . $author . "</i></td>\n" .
3847 print format_subject_html($co{'title'}, $co{'title_short'},
3848 href(action=>"commit", hash=>$commit), $ref);
3850 "<td class=\"link\">" .
3851 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3852 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3853 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3854 my $snapshot_links = format_snapshot_links($commit);
3855 if (defined $snapshot_links) {
3856 print " | " . $snapshot_links;
3861 if (defined $extra) {
3863 "<td colspan=\"4\">$extra</td>\n" .
3869 sub git_history_body {
3870 # Warning: assumes constant type (blob or tree) during history
3871 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3873 $from = 0 unless defined $from;
3874 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3876 print "<table class=\"history\">\n";
3878 for (my $i = $from; $i <= $to; $i++) {
3879 my %co = %{$commitlist->[$i]};
3883 my $commit = $co{'id'};
3885 my $ref = format_ref_marker($refs, $commit);
3888 print "<tr class=\"dark\">\n";
3890 print "<tr class=\"light\">\n";
3893 # shortlog uses chop_str($co{'author_name'}, 10)
3894 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3895 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3896 "<td><i>" . $author . "</i></td>\n" .
3898 # originally git_history used chop_str($co{'title'}, 50)
3899 print format_subject_html($co{'title'}, $co{'title_short'},
3900 href(action=>"commit", hash=>$commit), $ref);
3902 "<td class=\"link\">" .
3903 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3904 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3906 if ($ftype eq 'blob') {
3907 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3908 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3909 if (defined $blob_current && defined $blob_parent &&
3910 $blob_current ne $blob_parent) {
3912 $cgi->a({-href => href(action=>"blobdiff",
3913 hash=>$blob_current, hash_parent=>$blob_parent,
3914 hash_base=>$hash_base, hash_parent_base=>$commit,
3915 file_name=>$file_name)},
3922 if (defined $extra) {
3924 "<td colspan=\"4\">$extra</td>\n" .
3931 # uses global variable $project
3932 my ($taglist, $from, $to, $extra) = @_;
3933 $from = 0 unless defined $from;
3934 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3936 print "<table class=\"tags\">\n";
3938 for (my $i = $from; $i <= $to; $i++) {
3939 my $entry = $taglist->[$i];
3941 my $comment = $tag{'subject'};
3943 if (defined $comment) {
3944 $comment_short = chop_str($comment, 30, 5);
3947 print "<tr class=\"dark\">\n";
3949 print "<tr class=\"light\">\n";
3952 if (defined $tag{'age'}) {
3953 print "<td><i>$tag{'age'}</i></td>\n";
3955 print "<td></td>\n";
3958 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3959 -class => "list name"}, esc_html($tag{'name'})) .
3962 if (defined $comment) {
3963 print format_subject_html($comment, $comment_short,
3964 href(action=>"tag", hash=>$tag{'id'}));
3967 "<td class=\"selflink\">";
3968 if ($tag{'type'} eq "tag") {
3969 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3974 "<td class=\"link\">" . " | " .
3975 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3976 if ($tag{'reftype'} eq "commit") {
3977 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
3978 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
3979 } elsif ($tag{'reftype'} eq "blob") {
3980 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3985 if (defined $extra) {
3987 "<td colspan=\"5\">$extra</td>\n" .
3993 sub git_heads_body {
3994 # uses global variable $project
3995 my ($headlist, $head, $from, $to, $extra) = @_;
3996 $from = 0 unless defined $from;
3997 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3999 print "<table class=\"heads\">\n";
4001 for (my $i = $from; $i <= $to; $i++) {
4002 my $entry = $headlist->[$i];
4004 my $curr = $ref{'id'} eq $head;
4006 print "<tr class=\"dark\">\n";
4008 print "<tr class=\"light\">\n";
4011 print "<td><i>$ref{'age'}</i></td>\n" .
4012 ($curr ? "<td class=\"current_head\">" : "<td>") .
4013 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4014 -class => "list name"},esc_html($ref{'name'})) .
4016 "<td class=\"link\">" .
4017 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4018 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4019 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4023 if (defined $extra) {
4025 "<td colspan=\"3\">$extra</td>\n" .
4031 sub git_search_grep_body {
4032 my ($commitlist, $from, $to, $extra) = @_;
4033 $from = 0 unless defined $from;
4034 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4036 print "<table class=\"commit_search\">\n";
4038 for (my $i = $from; $i <= $to; $i++) {
4039 my %co = %{$commitlist->[$i]};
4043 my $commit = $co{'id'};
4045 print "<tr class=\"dark\">\n";
4047 print "<tr class=\"light\">\n";
4050 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
4051 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4052 "<td><i>" . $author . "</i></td>\n" .
4054 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4055 -class => "list subject"},
4056 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4057 my $comment = $co{'comment'};
4058 foreach my $line (@$comment) {
4059 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4060 my ($lead, $match, $trail) = ($1, $2, $3);
4061 $match = chop_str($match, 70, 5, 'center');
4062 my $contextlen = int((80 - length($match))/2);
4063 $contextlen = 30 if ($contextlen > 30);
4064 $lead = chop_str($lead, $contextlen, 10, 'left');
4065 $trail = chop_str($trail, $contextlen, 10, 'right');
4067 $lead = esc_html($lead);
4068 $match = esc_html($match);
4069 $trail = esc_html($trail);
4071 print "$lead<span class=\"match\">$match</span>$trail<br />";
4075 "<td class=\"link\">" .
4076 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4078 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4080 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4084 if (defined $extra) {
4086 "<td colspan=\"3\">$extra</td>\n" .
4092 ## ======================================================================
4093 ## ======================================================================
4096 sub git_project_list {
4097 my $order = $input_params{'order'};
4098 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4099 die_error(400, "Unknown order parameter");
4102 my @list = git_get_projects_list();
4104 die_error(404, "No projects found");
4107 if (-f $home_text) {
4108 print "<div class=\"index_include\">\n";
4109 print insert_file($home_text);
4112 print $cgi->startform(-method => "get") .
4113 "<p class=\"projsearch\">Search:\n" .
4114 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4116 $cgi->end_form() . "\n";
4117 git_project_list_body(\@list, $order);
4121 my $order = $input_params{'order'};
4122 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4123 die_error(400, "Unknown order parameter");
4126 my @list = git_get_projects_list($project);
4128 die_error(404, "No forks found");
4131 git_print_page_nav('','');
4132 git_print_header_div('summary', "$project forks");
4133 git_project_list_body(\@list, $order);
4136 sub git_project_index {
4137 my @projects = git_get_projects_list($project);
4140 -type => 'text/plain',
4141 -charset => 'utf-8',
4142 -content_disposition => 'inline; filename="index.aux"');
4144 foreach my $pr (@projects) {
4145 if (!exists $pr->{'owner'}) {
4146 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4149 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4150 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4151 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4152 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4156 print "$path $owner\n";
4161 my $descr = git_get_project_description($project) || "none";
4162 my %co = parse_commit("HEAD");
4163 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4164 my $head = $co{'id'};
4166 my $owner = git_get_project_owner($project);
4168 my $refs = git_get_references();
4169 # These get_*_list functions return one more to allow us to see if
4170 # there are more ...
4171 my @taglist = git_get_tags_list(16);
4172 my @headlist = git_get_heads_list(16);
4174 my $check_forks = gitweb_check_feature('forks');
4177 @forklist = git_get_projects_list($project);
4180 git_print_page_nav('summary','', $head);
4182 print "<div class=\"title\"> </div>\n";
4183 print "<table class=\"projects_list\">\n" .
4184 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4185 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4186 if (defined $cd{'rfc2822'}) {
4187 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4190 # use per project git URL list in $projectroot/$project/cloneurl
4191 # or make project git URL from git base URL and project name
4192 my $url_tag = "URL";
4193 my @url_list = git_get_project_url_list($project);
4194 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4195 foreach my $git_url (@url_list) {
4196 next unless $git_url;
4197 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4202 my $show_ctags = gitweb_check_feature('ctags');
4204 my $ctags = git_get_project_ctags($project);
4205 my $cloud = git_populate_project_tagcloud($ctags);
4206 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4207 print "</td>\n<td>" unless %$ctags;
4208 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4209 print "</td>\n<td>" if %$ctags;
4210 print git_show_project_tagcloud($cloud, 48);
4216 # If XSS prevention is on, we don't include README.html.
4217 # TODO: Allow a readme in some safe format.
4218 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
4219 print "<div class=\"title\">readme</div>\n" .
4220 "<div class=\"readme\">\n";
4221 print insert_file("$projectroot/$project/README.html");
4222 print "\n</div>\n"; # class="readme"
4225 # we need to request one more than 16 (0..15) to check if
4227 my @commitlist = $head ? parse_commits($head, 17) : ();
4229 git_print_header_div('shortlog');
4230 git_shortlog_body(\@commitlist, 0, 15, $refs,
4231 $#commitlist <= 15 ? undef :
4232 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4236 git_print_header_div('tags');
4237 git_tags_body(\@taglist, 0, 15,
4238 $#taglist <= 15 ? undef :
4239 $cgi->a({-href => href(action=>"tags")}, "..."));
4243 git_print_header_div('heads');
4244 git_heads_body(\@headlist, $head, 0, 15,
4245 $#headlist <= 15 ? undef :
4246 $cgi->a({-href => href(action=>"heads")}, "..."));
4250 git_print_header_div('forks');
4251 git_project_list_body(\@forklist, 'age', 0, 15,
4252 $#forklist <= 15 ? undef :
4253 $cgi->a({-href => href(action=>"forks")}, "..."),
4260 my $head = git_get_head_hash($project);
4261 git_print_page_nav('','', $head,undef,$head);
4262 my %tag = parse_tag($hash);
4265 die_error(404, "Unknown tag object");
4268 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4269 print "<div class=\"title_text\">\n" .
4270 "<table class=\"object_header\">\n" .
4272 "<td>object</td>\n" .
4273 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4274 $tag{'object'}) . "</td>\n" .
4275 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4276 $tag{'type'}) . "</td>\n" .
4278 if (defined($tag{'author'})) {
4279 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4280 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4281 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4282 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4285 print "</table>\n\n" .
4287 print "<div class=\"page_body\">";
4288 my $comment = $tag{'comment'};
4289 foreach my $line (@$comment) {
4291 print esc_html($line, -nbsp=>1) . "<br/>\n";
4298 gitweb_check_feature('blame')
4299 or die_error(403, "Blame view not allowed");
4302 die_error(400, "No file name given") unless $file_name;
4303 $hash_base ||= git_get_head_hash($project);
4304 die_error(404, "Couldn't find base commit") unless $hash_base;
4305 my %co = parse_commit($hash_base)
4306 or die_error(404, "Commit not found");
4308 if (!defined $hash) {
4309 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4310 or die_error(404, "Error looking up file");
4312 $ftype = git_get_type($hash);
4313 if ($ftype !~ "blob") {
4314 die_error(400, "Object is not a blob - $hash");
4318 # run git-blame --porcelain
4319 open my $fd, "-|", git_cmd(), "blame", '-p',
4320 $hash_base, '--', $file_name
4321 or die_error(500, "Open git-blame failed");
4325 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4328 $cgi->a({-href => href(action=>"history", -replay=>1)},
4331 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4333 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4334 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4335 git_print_page_path($file_name, $ftype, $hash_base);
4338 my @rev_color = qw(light2 dark2);
4339 my $num_colors = scalar(@rev_color);
4340 my $current_color = 0;
4344 <div class="page_body">
4345 <table class="blame">
4346 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4349 while (my $line = <$fd>) {
4351 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
4352 # no <lines in group> for subsequent lines in group of lines
4353 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4354 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
4355 if (!exists $metainfo{$full_rev}) {
4356 $metainfo{$full_rev} = {};
4358 my $meta = $metainfo{$full_rev};
4360 while ($data = <$fd>) {
4362 last if ($data =~ s/^\t//); # contents of line
4363 if ($data =~ /^(\S+) (.*)$/) {
4367 my $short_rev = substr($full_rev, 0, 8);
4368 my $author = $meta->{'author'};
4370 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
4371 my $date = $date{'iso-tz'};
4373 $current_color = ($current_color + 1) % $num_colors;
4375 print "<tr id=\"l$lineno\" class=\"$rev_color[$current_color]\">\n";
4377 print "<td class=\"sha1\"";
4378 print " title=\"". esc_html($author) . ", $date\"";
4379 print " rowspan=\"$group_size\"" if ($group_size > 1);
4381 print $cgi->a({-href => href(action=>"commit",
4383 file_name=>$file_name)},
4384 esc_html($short_rev));
4388 if (!exists $meta->{'parent'}) {
4389 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4390 or die_error(500, "Open git-rev-parse failed");
4391 $parent_commit = <$dd>;
4393 chomp($parent_commit);
4394 $meta->{'parent'} = $parent_commit;
4396 $parent_commit = $meta->{'parent'};
4398 my $blamed = href(action => 'blame',
4399 file_name => $meta->{'filename'},
4400 hash_base => $parent_commit);
4401 print "<td class=\"linenr\">";
4402 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4403 -class => "linenr" },
4406 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4412 or print "Reading blob failed\n";
4418 my $head = git_get_head_hash($project);
4419 git_print_page_nav('','', $head,undef,$head);
4420 git_print_header_div('summary', $project);
4422 my @tagslist = git_get_tags_list();
4424 git_tags_body(\@tagslist);
4429 my $head = git_get_head_hash($project);
4430 git_print_page_nav('','', $head,undef,$head);
4431 git_print_header_div('summary', $project);
4433 my @headslist = git_get_heads_list();
4435 git_heads_body(\@headslist, $head);
4439 sub git_blob_plain {
4443 if (!defined $hash) {
4444 if (defined $file_name) {
4445 my $base = $hash_base || git_get_head_hash($project);
4446 $hash = git_get_hash_by_path($base, $file_name, "blob")
4447 or die_error(404, "Cannot find file");
4449 die_error(400, "No file name defined");
4451 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4452 # blobs defined by non-textual hash id's can be cached
4456 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4457 or die_error(500, "Open git-cat-file blob '$hash' failed");
4459 # content-type (can include charset)
4460 $type = blob_contenttype($fd, $file_name, $type);
4462 # "save as" filename, even when no $file_name is given
4463 my $save_as = "$hash";
4464 if (defined $file_name) {
4465 $save_as = $file_name;
4466 } elsif ($type =~ m/^text\//) {
4470 # With XSS prevention on, blobs of all types except a few known safe
4471 # ones are served with "Content-Disposition: attachment" to make sure
4472 # they don't run in our security domain. For certain image types,
4473 # blob view writes an <img> tag referring to blob_plain view, and we
4474 # want to be sure not to break that by serving the image as an
4475 # attachment (though Firefox 3 doesn't seem to care).
4476 my $sandbox = $prevent_xss &&
4477 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
4481 -expires => $expires,
4482 -content_disposition =>
4483 ($sandbox ? 'attachment' : 'inline')
4484 . '; filename="' . $save_as . '"');
4486 binmode STDOUT, ':raw';
4488 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4496 if (!defined $hash) {
4497 if (defined $file_name) {
4498 my $base = $hash_base || git_get_head_hash($project);
4499 $hash = git_get_hash_by_path($base, $file_name, "blob")
4500 or die_error(404, "Cannot find file");
4502 die_error(400, "No file name defined");
4504 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4505 # blobs defined by non-textual hash id's can be cached
4509 my $have_blame = gitweb_check_feature('blame');
4510 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4511 or die_error(500, "Couldn't cat $file_name, $hash");
4512 my $mimetype = blob_mimetype($fd, $file_name);
4513 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4515 return git_blob_plain($mimetype);
4517 # we can have blame only for text/* mimetype
4518 $have_blame &&= ($mimetype =~ m!^text/!);
4520 my $formats_nav = '';
4521 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4522 if (defined $file_name) {
4525 $cgi->a({-href => href(action=>"blame", hash => $hash, -replay=>1)},
4530 $cgi->a({-href => href(action=>"history", -replay=>1)},
4533 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4536 $cgi->a({-href => href(action=>"blob",
4537 hash_base=>"HEAD", file_name=>$file_name)},
4541 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4544 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4545 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4547 print "<div class=\"page_nav\">\n" .
4548 "<br/><br/></div>\n" .
4549 "<div class=\"title\">$hash</div>\n";
4551 git_print_page_path($file_name, "blob", $hash_base);
4552 print "<div class=\"page_body\">\n";
4553 if ($mimetype =~ m!^image/!) {
4554 print qq!<img type="$mimetype"!;
4556 print qq! alt="$file_name" title="$file_name"!;
4559 href(action=>"blob_plain", hash=>$hash,
4560 hash_base=>$hash_base, file_name=>$file_name) .
4564 while (my $line = <$fd>) {
4567 $line = untabify($line);
4568 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4569 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4573 or print "Reading blob failed.\n";
4578 if (!defined $hash_base) {
4579 $hash_base = "HEAD";
4581 if (!defined $hash) {
4582 if (defined $file_name) {
4583 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4588 die_error(404, "No such tree") unless defined($hash);
4590 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4591 or die_error(500, "Open git-ls-tree failed");
4592 my @entries = map { chomp; $_ } <$fd>;
4593 close $fd or die_error(404, "Reading tree failed");
4596 my $refs = git_get_references();
4597 my $ref = format_ref_marker($refs, $hash_base);
4599 my $have_blame = gitweb_check_feature('blame');
4600 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4602 if (defined $file_name) {
4604 $cgi->a({-href => href(action=>"history", -replay=>1)},
4606 $cgi->a({-href => href(action=>"tree",
4607 hash_base=>"HEAD", file_name=>$file_name)},
4610 my $snapshot_links = format_snapshot_links($hash);
4611 if (defined $snapshot_links) {
4612 # FIXME: Should be available when we have no hash base as well.
4613 push @views_nav, $snapshot_links;
4615 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4616 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4619 print "<div class=\"page_nav\">\n";
4620 print "<br/><br/></div>\n";
4621 print "<div class=\"title\">$hash</div>\n";
4623 if (defined $file_name) {
4624 $basedir = $file_name;
4625 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4628 git_print_page_path($file_name, 'tree', $hash_base);
4630 print "<div class=\"page_body\">\n";
4631 print "<table class=\"tree\">\n";
4633 # '..' (top directory) link if possible
4634 if (defined $hash_base &&
4635 defined $file_name && $file_name =~ m![^/]+$!) {
4637 print "<tr class=\"dark\">\n";
4639 print "<tr class=\"light\">\n";
4643 my $up = $file_name;
4644 $up =~ s!/?[^/]+$!!;
4645 undef $up unless $up;
4646 # based on git_print_tree_entry
4647 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4648 print '<td class="list">';
4649 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4653 print "<td class=\"link\"></td>\n";
4657 foreach my $line (@entries) {
4658 my %t = parse_ls_tree_line($line, -z => 1);
4661 print "<tr class=\"dark\">\n";
4663 print "<tr class=\"light\">\n";
4667 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4671 print "</table>\n" .
4676 my $format = $input_params{'snapshot_format'};
4677 if (!@snapshot_fmts) {
4678 die_error(403, "Snapshots not allowed");
4680 # default to first supported snapshot format
4681 $format ||= $snapshot_fmts[0];
4682 if ($format !~ m/^[a-z0-9]+$/) {
4683 die_error(400, "Invalid snapshot format parameter");
4684 } elsif (!exists($known_snapshot_formats{$format})) {
4685 die_error(400, "Unknown snapshot format");
4686 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
4687 die_error(403, "Unsupported snapshot format");
4690 if (!defined $hash) {
4691 $hash = git_get_head_hash($project);
4694 my $name = $project;
4695 $name =~ s,([^/])/*\.git$,$1,;
4696 $name = basename($name);
4697 my $filename = to_utf8($name);
4698 $name =~ s/\047/\047\\\047\047/g;
4700 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4701 $cmd = quote_command(
4702 git_cmd(), 'archive',
4703 "--format=$known_snapshot_formats{$format}{'format'}",
4704 "--prefix=$name/", $hash);
4705 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4706 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
4710 -type => $known_snapshot_formats{$format}{'type'},
4711 -content_disposition => 'inline; filename="' . "$filename" . '"',
4712 -status => '200 OK');
4714 open my $fd, "-|", $cmd
4715 or die_error(500, "Execute git-archive failed");
4716 binmode STDOUT, ':raw';
4718 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4723 my $head = git_get_head_hash($project);
4724 if (!defined $hash) {
4727 if (!defined $page) {
4730 my $refs = git_get_references();
4732 my @commitlist = parse_commits($hash, 101, (100 * $page));
4734 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
4736 my ($patch_max) = gitweb_get_feature('patches');
4738 if ($patch_max < 0 || @commitlist <= $patch_max) {
4739 $paging_nav .= " ⋅ " .
4740 $cgi->a({-href => href(action=>"patches", -replay=>1)},
4745 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4748 my %co = parse_commit($hash);
4750 git_print_header_div('summary', $project);
4751 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4753 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4754 for (my $i = 0; $i <= $to; $i++) {
4755 my %co = %{$commitlist[$i]};
4757 my $commit = $co{'id'};
4758 my $ref = format_ref_marker($refs, $commit);
4759 my %ad = parse_date($co{'author_epoch'});
4760 git_print_header_div('commit',
4761 "<span class=\"age\">$co{'age_string'}</span>" .
4762 esc_html($co{'title'}) . $ref,
4764 print "<div class=\"title_text\">\n" .
4765 "<div class=\"log_link\">\n" .
4766 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4768 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4770 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4773 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4776 print "<div class=\"log_body\">\n";
4777 git_print_log($co{'comment'}, -final_empty_line=> 1);
4780 if ($#commitlist >= 100) {
4781 print "<div class=\"page_nav\">\n";
4782 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4783 -accesskey => "n", -title => "Alt-n"}, "next");
4789 $hash ||= $hash_base || "HEAD";
4790 my %co = parse_commit($hash)
4791 or die_error(404, "Unknown commit object");
4792 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4793 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4795 my $parent = $co{'parent'};
4796 my $parents = $co{'parents'}; # listref
4798 # we need to prepare $formats_nav before any parameter munging
4800 if (!defined $parent) {
4802 $formats_nav .= '(initial)';
4803 } elsif (@$parents == 1) {
4804 # single parent commit
4807 $cgi->a({-href => href(action=>"commit",
4809 esc_html(substr($parent, 0, 7))) .
4816 $cgi->a({-href => href(action=>"commit",
4818 esc_html(substr($_, 0, 7)));
4822 if (gitweb_check_feature('patches')) {
4823 $formats_nav .= " | " .
4824 $cgi->a({-href => href(action=>"patch", -replay=>1)},
4828 if (!defined $parent) {
4832 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4834 (@$parents <= 1 ? $parent : '-c'),
4836 or die_error(500, "Open git-diff-tree failed");
4837 @difftree = map { chomp; $_ } <$fd>;
4838 close $fd or die_error(404, "Reading git-diff-tree failed");
4840 # non-textual hash id's can be cached
4842 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4845 my $refs = git_get_references();
4846 my $ref = format_ref_marker($refs, $co{'id'});
4848 git_print_page_nav('commit', '',
4849 $hash, $co{'tree'}, $hash,
4852 if (defined $co{'parent'}) {
4853 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4855 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4857 print "<div class=\"title_text\">\n" .
4858 "<table class=\"object_header\">\n";
4859 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4861 "<td></td><td> $ad{'rfc2822'}";
4862 if ($ad{'hour_local'} < 6) {
4863 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4864 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4866 printf(" (%02d:%02d %s)",
4867 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4871 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4872 print "<tr><td></td><td> $cd{'rfc2822'}" .
4873 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4875 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4878 "<td class=\"sha1\">" .
4879 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4880 class => "list"}, $co{'tree'}) .
4882 "<td class=\"link\">" .
4883 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4885 my $snapshot_links = format_snapshot_links($hash);
4886 if (defined $snapshot_links) {
4887 print " | " . $snapshot_links;
4892 foreach my $par (@$parents) {
4895 "<td class=\"sha1\">" .
4896 $cgi->a({-href => href(action=>"commit", hash=>$par),
4897 class => "list"}, $par) .
4899 "<td class=\"link\">" .
4900 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4902 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4909 print "<div class=\"page_body\">\n";
4910 git_print_log($co{'comment'});
4913 git_difftree_body(\@difftree, $hash, @$parents);
4918 # object is defined by:
4919 # - hash or hash_base alone
4920 # - hash_base and file_name
4923 # - hash or hash_base alone
4924 if ($hash || ($hash_base && !defined $file_name)) {
4925 my $object_id = $hash || $hash_base;
4927 open my $fd, "-|", quote_command(
4928 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
4929 or die_error(404, "Object does not exist");
4933 or die_error(404, "Object does not exist");
4935 # - hash_base and file_name
4936 } elsif ($hash_base && defined $file_name) {
4937 $file_name =~ s,/+$,,;
4939 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4940 or die_error(404, "Base object does not exist");
4942 # here errors should not hapen
4943 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4944 or die_error(500, "Open git-ls-tree failed");
4948 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4949 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4950 die_error(404, "File or directory for given base does not exist");
4955 die_error(400, "Not enough information to find object");
4958 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4959 hash=>$hash, hash_base=>$hash_base,
4960 file_name=>$file_name),
4961 -status => '302 Found');
4965 my $format = shift || 'html';
4972 # preparing $fd and %diffinfo for git_patchset_body
4974 if (defined $hash_base && defined $hash_parent_base) {
4975 if (defined $file_name) {
4977 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4978 $hash_parent_base, $hash_base,
4979 "--", (defined $file_parent ? $file_parent : ()), $file_name
4980 or die_error(500, "Open git-diff-tree failed");
4981 @difftree = map { chomp; $_ } <$fd>;
4983 or die_error(404, "Reading git-diff-tree failed");
4985 or die_error(404, "Blob diff not found");
4987 } elsif (defined $hash &&
4988 $hash =~ /[0-9a-fA-F]{40}/) {
4989 # try to find filename from $hash
4991 # read filtered raw output
4992 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4993 $hash_parent_base, $hash_base, "--"
4994 or die_error(500, "Open git-diff-tree failed");
4996 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4998 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4999 map { chomp; $_ } <$fd>;
5001 or die_error(404, "Reading git-diff-tree failed");
5003 or die_error(404, "Blob diff not found");
5006 die_error(400, "Missing one of the blob diff parameters");
5009 if (@difftree > 1) {
5010 die_error(400, "Ambiguous blob diff specification");
5013 %diffinfo = parse_difftree_raw_line($difftree[0]);
5014 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5015 $file_name ||= $diffinfo{'to_file'};
5017 $hash_parent ||= $diffinfo{'from_id'};
5018 $hash ||= $diffinfo{'to_id'};
5020 # non-textual hash id's can be cached
5021 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5022 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5027 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5028 '-p', ($format eq 'html' ? "--full-index" : ()),
5029 $hash_parent_base, $hash_base,
5030 "--", (defined $file_parent ? $file_parent : ()), $file_name
5031 or die_error(500, "Open git-diff-tree failed");
5034 # old/legacy style URI -- not generated anymore since 1.4.3.
5036 die_error('404 Not Found', "Missing one of the blob diff parameters")
5040 if ($format eq 'html') {
5042 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5044 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5045 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5046 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5048 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5049 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5051 if (defined $file_name) {
5052 git_print_page_path($file_name, "blob", $hash_base);
5054 print "<div class=\"page_path\"></div>\n";
5057 } elsif ($format eq 'plain') {
5059 -type => 'text/plain',
5060 -charset => 'utf-8',
5061 -expires => $expires,
5062 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5064 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5067 die_error(400, "Unknown blobdiff format");
5071 if ($format eq 'html') {
5072 print "<div class=\"page_body\">\n";
5074 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5077 print "</div>\n"; # class="page_body"
5080 while (my $line = <$fd>) {
5081 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5082 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5086 last if $line =~ m!^\+\+\+!;
5094 sub git_blobdiff_plain {
5095 git_blobdiff('plain');
5098 sub git_commitdiff {
5100 my $format = $params{-format} || 'html';
5102 my ($patch_max) = gitweb_get_feature('patches');
5103 if ($format eq 'patch') {
5104 die_error(403, "Patch view not allowed") unless $patch_max;
5107 $hash ||= $hash_base || "HEAD";
5108 my %co = parse_commit($hash)
5109 or die_error(404, "Unknown commit object");
5111 # choose format for commitdiff for merge
5112 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5113 $hash_parent = '--cc';
5115 # we need to prepare $formats_nav before almost any parameter munging
5117 if ($format eq 'html') {
5119 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5122 $formats_nav .= " | " .
5123 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5127 if (defined $hash_parent &&
5128 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5129 # commitdiff with two commits given
5130 my $hash_parent_short = $hash_parent;
5131 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5132 $hash_parent_short = substr($hash_parent, 0, 7);
5136 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5137 if ($co{'parents'}[$i] eq $hash_parent) {
5138 $formats_nav .= ' parent ' . ($i+1);
5142 $formats_nav .= ': ' .
5143 $cgi->a({-href => href(action=>"commitdiff",
5144 hash=>$hash_parent)},
5145 esc_html($hash_parent_short)) .
5147 } elsif (!$co{'parent'}) {
5149 $formats_nav .= ' (initial)';
5150 } elsif (scalar @{$co{'parents'}} == 1) {
5151 # single parent commit
5154 $cgi->a({-href => href(action=>"commitdiff",
5155 hash=>$co{'parent'})},
5156 esc_html(substr($co{'parent'}, 0, 7))) .
5160 if ($hash_parent eq '--cc') {
5161 $formats_nav .= ' | ' .
5162 $cgi->a({-href => href(action=>"commitdiff",
5163 hash=>$hash, hash_parent=>'-c')},
5165 } else { # $hash_parent eq '-c'
5166 $formats_nav .= ' | ' .
5167 $cgi->a({-href => href(action=>"commitdiff",
5168 hash=>$hash, hash_parent=>'--cc')},
5174 $cgi->a({-href => href(action=>"commitdiff",
5176 esc_html(substr($_, 0, 7)));
5177 } @{$co{'parents'}} ) .
5182 my $hash_parent_param = $hash_parent;
5183 if (!defined $hash_parent_param) {
5184 # --cc for multiple parents, --root for parentless
5185 $hash_parent_param =
5186 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5192 if ($format eq 'html') {
5193 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5194 "--no-commit-id", "--patch-with-raw", "--full-index",
5195 $hash_parent_param, $hash, "--"
5196 or die_error(500, "Open git-diff-tree failed");
5198 while (my $line = <$fd>) {
5200 # empty line ends raw part of diff-tree output
5202 push @difftree, scalar parse_difftree_raw_line($line);
5205 } elsif ($format eq 'plain') {
5206 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5207 '-p', $hash_parent_param, $hash, "--"
5208 or die_error(500, "Open git-diff-tree failed");
5209 } elsif ($format eq 'patch') {
5210 # For commit ranges, we limit the output to the number of
5211 # patches specified in the 'patches' feature.
5212 # For single commits, we limit the output to a single patch,
5213 # diverging from the git-format-patch default.
5214 my @commit_spec = ();
5216 if ($patch_max > 0) {
5217 push @commit_spec, "-$patch_max";
5219 push @commit_spec, '-n', "$hash_parent..$hash";
5221 if ($params{-single}) {
5222 push @commit_spec, '-1';
5224 if ($patch_max > 0) {
5225 push @commit_spec, "-$patch_max";
5227 push @commit_spec, "-n";
5229 push @commit_spec, '--root', $hash;
5231 open $fd, "-|", git_cmd(), "format-patch", '--encoding=utf8',
5232 '--stdout', @commit_spec
5233 or die_error(500, "Open git-format-patch failed");
5235 die_error(400, "Unknown commitdiff format");
5238 # non-textual hash id's can be cached
5240 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5244 # write commit message
5245 if ($format eq 'html') {
5246 my $refs = git_get_references();
5247 my $ref = format_ref_marker($refs, $co{'id'});
5249 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5250 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5251 git_print_authorship(\%co);
5252 print "<div class=\"page_body\">\n";
5253 if (@{$co{'comment'}} > 1) {
5254 print "<div class=\"log\">\n";
5255 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5256 print "</div>\n"; # class="log"
5259 } elsif ($format eq 'plain') {
5260 my $refs = git_get_references("tags");
5261 my $tagname = git_get_rev_name_tags($hash);
5262 my $filename = basename($project) . "-$hash.patch";
5265 -type => 'text/plain',
5266 -charset => 'utf-8',
5267 -expires => $expires,
5268 -content_disposition => 'inline; filename="' . "$filename" . '"');
5269 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5270 print "From: " . to_utf8($co{'author'}) . "\n";
5271 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5272 print "Subject: " . to_utf8($co{'title'}) . "\n";
5274 print "X-Git-Tag: $tagname\n" if $tagname;
5275 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5277 foreach my $line (@{$co{'comment'}}) {
5278 print to_utf8($line) . "\n";
5281 } elsif ($format eq 'patch') {
5282 my $filename = basename($project) . "-$hash.patch";
5285 -type => 'text/plain',
5286 -charset => 'utf-8',
5287 -expires => $expires,
5288 -content_disposition => 'inline; filename="' . "$filename" . '"');
5292 if ($format eq 'html') {
5293 my $use_parents = !defined $hash_parent ||
5294 $hash_parent eq '-c' || $hash_parent eq '--cc';
5295 git_difftree_body(\@difftree, $hash,
5296 $use_parents ? @{$co{'parents'}} : $hash_parent);
5299 git_patchset_body($fd, \@difftree, $hash,
5300 $use_parents ? @{$co{'parents'}} : $hash_parent);
5302 print "</div>\n"; # class="page_body"
5304 } elsif ($format eq 'plain') {
5308 or print "Reading git-diff-tree failed\n";
5309 } elsif ($format eq 'patch') {
5313 or print "Reading git-format-patch failed\n";
5317 sub git_commitdiff_plain {
5318 git_commitdiff(-format => 'plain');
5321 # format-patch-style patches
5323 git_commitdiff(-format => 'patch', -single=> 1);
5327 git_commitdiff(-format => 'patch');
5331 if (!defined $hash_base) {
5332 $hash_base = git_get_head_hash($project);
5334 if (!defined $page) {
5338 my %co = parse_commit($hash_base)
5339 or die_error(404, "Unknown commit object");
5341 my $refs = git_get_references();
5342 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5344 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5345 $file_name, "--full-history")
5346 or die_error(404, "No such file or directory on given branch");
5348 if (!defined $hash && defined $file_name) {
5349 # some commits could have deleted file in question,
5350 # and not have it in tree, but one of them has to have it
5351 for (my $i = 0; $i <= @commitlist; $i++) {
5352 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5353 last if defined $hash;
5356 if (defined $hash) {
5357 $ftype = git_get_type($hash);
5359 if (!defined $ftype) {
5360 die_error(500, "Unknown type of object");
5363 my $paging_nav = '';
5366 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5367 file_name=>$file_name)},
5369 $paging_nav .= " ⋅ " .
5370 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5371 -accesskey => "p", -title => "Alt-p"}, "prev");
5373 $paging_nav .= "first";
5374 $paging_nav .= " ⋅ prev";
5377 if ($#commitlist >= 100) {
5379 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5380 -accesskey => "n", -title => "Alt-n"}, "next");
5381 $paging_nav .= " ⋅ $next_link";
5383 $paging_nav .= " ⋅ next";
5386 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5387 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5388 git_print_page_path($file_name, $ftype, $hash_base);
5390 git_history_body(\@commitlist, 0, 99,
5391 $refs, $hash_base, $ftype, $next_link);
5396 gitweb_check_feature('search') or die_error(403, "Search is disabled");
5397 if (!defined $searchtext) {
5398 die_error(400, "Text field is empty");
5400 if (!defined $hash) {
5401 $hash = git_get_head_hash($project);
5403 my %co = parse_commit($hash);
5405 die_error(404, "Unknown commit object");
5407 if (!defined $page) {
5411 $searchtype ||= 'commit';
5412 if ($searchtype eq 'pickaxe') {
5413 # pickaxe may take all resources of your box and run for several minutes
5414 # with every query - so decide by yourself how public you make this feature
5415 gitweb_check_feature('pickaxe')
5416 or die_error(403, "Pickaxe is disabled");
5418 if ($searchtype eq 'grep') {
5419 gitweb_check_feature('grep')
5420 or die_error(403, "Grep is disabled");
5424 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5426 if ($searchtype eq 'commit') {
5427 $greptype = "--grep=";
5428 } elsif ($searchtype eq 'author') {
5429 $greptype = "--author=";
5430 } elsif ($searchtype eq 'committer') {
5431 $greptype = "--committer=";
5433 $greptype .= $searchtext;
5434 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5435 $greptype, '--regexp-ignore-case',
5436 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5438 my $paging_nav = '';
5441 $cgi->a({-href => href(action=>"search", hash=>$hash,
5442 searchtext=>$searchtext,
5443 searchtype=>$searchtype)},
5445 $paging_nav .= " ⋅ " .
5446 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5447 -accesskey => "p", -title => "Alt-p"}, "prev");
5449 $paging_nav .= "first";
5450 $paging_nav .= " ⋅ prev";
5453 if ($#commitlist >= 100) {
5455 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5456 -accesskey => "n", -title => "Alt-n"}, "next");
5457 $paging_nav .= " ⋅ $next_link";
5459 $paging_nav .= " ⋅ next";
5462 if ($#commitlist >= 100) {
5465 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5466 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5467 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5470 if ($searchtype eq 'pickaxe') {
5471 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5472 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5474 print "<table class=\"pickaxe search\">\n";
5477 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5478 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5479 ($search_use_regexp ? '--pickaxe-regex' : ());
5482 while (my $line = <$fd>) {
5486 my %set = parse_difftree_raw_line($line);
5487 if (defined $set{'commit'}) {
5488 # finish previous commit
5491 "<td class=\"link\">" .
5492 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5494 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5500 print "<tr class=\"dark\">\n";
5502 print "<tr class=\"light\">\n";
5505 %co = parse_commit($set{'commit'});
5506 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5507 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5508 "<td><i>$author</i></td>\n" .
5510 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5511 -class => "list subject"},
5512 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5513 } elsif (defined $set{'to_id'}) {
5514 next if ($set{'to_id'} =~ m/^0{40}$/);
5516 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5517 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5519 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5525 # finish last commit (warning: repetition!)
5528 "<td class=\"link\">" .
5529 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5531 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5539 if ($searchtype eq 'grep') {
5540 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5541 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5543 print "<table class=\"grep_search\">\n";
5547 open my $fd, "-|", git_cmd(), 'grep', '-n',
5548 $search_use_regexp ? ('-E', '-i') : '-F',
5549 $searchtext, $co{'tree'};
5551 while (my $line = <$fd>) {
5553 my ($file, $lno, $ltext, $binary);
5554 last if ($matches++ > 1000);
5555 if ($line =~ /^Binary file (.+) matches$/) {
5559 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5561 if ($file ne $lastfile) {
5562 $lastfile and print "</td></tr>\n";
5564 print "<tr class=\"dark\">\n";
5566 print "<tr class=\"light\">\n";
5568 print "<td class=\"list\">".
5569 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5570 file_name=>"$file"),
5571 -class => "list"}, esc_path($file));
5572 print "</td><td>\n";
5576 print "<div class=\"binary\">Binary file</div>\n";
5578 $ltext = untabify($ltext);
5579 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5580 $ltext = esc_html($1, -nbsp=>1);
5581 $ltext .= '<span class="match">';
5582 $ltext .= esc_html($2, -nbsp=>1);
5583 $ltext .= '</span>';
5584 $ltext .= esc_html($3, -nbsp=>1);
5586 $ltext = esc_html($ltext, -nbsp=>1);
5588 print "<div class=\"pre\">" .
5589 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5590 file_name=>"$file").'#l'.$lno,
5591 -class => "linenr"}, sprintf('%4i', $lno))
5592 . ' ' . $ltext . "</div>\n";
5596 print "</td></tr>\n";
5597 if ($matches > 1000) {
5598 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5601 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5609 sub git_search_help {
5610 git_print_page_nav('','', $hash,$hash,$hash);
5612 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5613 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5614 the pattern entered is recognized as the POSIX extended
5615 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5618 <dt><b>commit</b></dt>
5619 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5621 my $have_grep = gitweb_check_feature('grep');
5624 <dt><b>grep</b></dt>
5625 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5626 a different one) are searched for the given pattern. On large trees, this search can take
5627 a while and put some strain on the server, so please use it with some consideration. Note that
5628 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5629 case-sensitive.</dd>
5633 <dt><b>author</b></dt>
5634 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5635 <dt><b>committer</b></dt>
5636 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5638 my $have_pickaxe = gitweb_check_feature('pickaxe');
5639 if ($have_pickaxe) {
5641 <dt><b>pickaxe</b></dt>
5642 <dd>All commits that caused the string to appear or disappear from any file (changes that
5643 added, removed or "modified" the string) will be listed. This search can take a while and
5644 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5645 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5652 my $head = git_get_head_hash($project);
5653 if (!defined $hash) {
5656 if (!defined $page) {
5659 my $refs = git_get_references();
5661 my $commit_hash = $hash;
5662 if (defined $hash_parent) {
5663 $commit_hash = "$hash_parent..$hash";
5665 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
5667 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5669 if ($#commitlist >= 100) {
5671 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5672 -accesskey => "n", -title => "Alt-n"}, "next");
5674 my $patch_max = gitweb_check_feature('patches');
5676 if ($patch_max < 0 || @commitlist <= $patch_max) {
5677 $paging_nav .= " ⋅ " .
5678 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5683 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5684 git_print_header_div('summary', $project);
5686 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5690 ## ......................................................................
5691 ## feeds (RSS, Atom; OPML)
5693 # XXX This does header stuff which may not play nice with Catalyst, so likely
5694 # broken in some/many ways.
5696 my $format = shift || 'atom';
5697 my $have_blame = gitweb_check_feature('blame');
5699 # Atom: http://www.atomenabled.org/developers/syndication/
5700 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5701 if ($format ne 'rss' && $format ne 'atom') {
5702 die_error(400, "Unknown web feed format");
5705 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5706 my $head = $hash || 'HEAD';
5707 my @commitlist = parse_commits($head, 150, 0, $file_name);
5711 my $content_type = "application/$format+xml";
5712 if (defined $cgi->http('HTTP_ACCEPT') &&
5713 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5714 # browser (feed reader) prefers text/xml
5715 $content_type = 'text/xml';
5717 if (defined($commitlist[0])) {
5718 %latest_commit = %{$commitlist[0]};
5719 my $latest_epoch = $latest_commit{'committer_epoch'};
5720 %latest_date = parse_date($latest_epoch);
5721 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
5722 if (defined $if_modified) {
5724 if (eval { require HTTP::Date; 1; }) {
5725 $since = HTTP::Date::str2time($if_modified);
5726 } elsif (eval { require Time::ParseDate; 1; }) {
5727 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
5729 if (defined $since && $latest_epoch <= $since) {
5731 -type => $content_type,
5732 -charset => 'utf-8',
5733 -last_modified => $latest_date{'rfc2822'},
5734 -status => '304 Not Modified');
5739 -type => $content_type,
5740 -charset => 'utf-8',
5741 -last_modified => $latest_date{'rfc2822'});
5744 -type => $content_type,
5745 -charset => 'utf-8');
5748 # Optimization: skip generating the body if client asks only
5749 # for Last-Modified date.
5750 return if ($cgi->request_method() eq 'HEAD');
5753 my $title = $c->config->{sitename} . " - $project/$action";
5754 my $feed_type = 'log';
5755 if (defined $hash) {
5756 $title .= " - '$hash'";
5757 $feed_type = 'branch log';
5758 if (defined $file_name) {
5759 $title .= " :: $file_name";
5760 $feed_type = 'history';
5762 } elsif (defined $file_name) {
5763 $title .= " - $file_name";
5764 $feed_type = 'history';
5766 $title .= " $feed_type";
5767 my $descr = git_get_project_description($project);
5768 if (defined $descr) {
5769 $descr = esc_html($descr);
5771 $descr = "$project " .
5772 ($format eq 'rss' ? 'RSS' : 'Atom') .
5775 my $owner = git_get_project_owner($project);
5776 $owner = esc_html($owner);
5780 if (defined $file_name) {
5781 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5782 } elsif (defined $hash) {
5783 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5785 $alt_url = href(-full=>1, action=>"summary");
5787 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5788 if ($format eq 'rss') {
5790 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5793 print "<title>$title</title>\n" .
5794 "<link>$alt_url</link>\n" .
5795 "<description>$descr</description>\n" .
5796 "<language>en</language>\n" .
5797 # project owner is responsible for 'editorial' content
5798 "<managingEditor>$owner</managingEditor>\n";
5799 if ($c->config->{logo} || $c->config->{favicon}) {
5800 # prefer the logo to the favicon, since RSS
5801 # doesn't allow both
5802 my $img = esc_url($c->config->{logo} || $c->config->{favicon});
5804 "<url>$img</url>\n" .
5805 "<title>$title</title>\n" .
5806 "<link>$alt_url</link>\n" .
5810 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
5811 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
5813 print "<generator>gitweb v.$version/$git_version</generator>\n";
5814 } elsif ($format eq 'atom') {
5816 <feed xmlns="http://www.w3.org/2005/Atom">
5818 print "<title>$title</title>\n" .
5819 "<subtitle>$descr</subtitle>\n" .
5820 '<link rel="alternate" type="text/html" href="' .
5821 $alt_url . '" />' . "\n" .
5822 '<link rel="self" type="' . $content_type . '" href="' .
5823 $cgi->self_url() . '" />' . "\n" .
5824 "<id>" . href(-full=>1) . "</id>\n" .
5825 # use project owner for feed author
5826 "<author><name>$owner</name></author>\n";
5827 if ($c->config->{favicon}) {
5828 print "<icon>" . esc_url($c->config->{favicon}) . "</icon>\n";
5830 if (defined $logo_url) {
5831 # not twice as wide as tall: 72 x 27 pixels
5832 print "<logo>" . esc_url($c->config->{logo}) . "</logo>\n";
5834 if (! %latest_date) {
5835 # dummy date to keep the feed valid until commits trickle in:
5836 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5838 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5840 print "<generator version='$version/$git_version'>gitweb</generator>\n";
5844 for (my $i = 0; $i <= $#commitlist; $i++) {
5845 my %co = %{$commitlist[$i]};
5846 my $commit = $co{'id'};
5847 # we read 150, we always show 30 and the ones more recent than 48 hours
5848 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5851 my %cd = parse_date($co{'author_epoch'});
5853 # get list of changed files
5854 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5855 $co{'parent'} || "--root",
5856 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5858 my @difftree = map { chomp; $_ } <$fd>;
5862 # print element (entry, item)
5863 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5864 if ($format eq 'rss') {
5866 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5867 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5868 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5869 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5870 "<link>$co_url</link>\n" .
5871 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5872 "<content:encoded>" .
5874 } elsif ($format eq 'atom') {
5876 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5877 "<updated>$cd{'iso-8601'}</updated>\n" .
5879 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5880 if ($co{'author_email'}) {
5881 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5883 print "</author>\n" .
5884 # use committer for contributor
5886 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5887 if ($co{'committer_email'}) {
5888 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5890 print "</contributor>\n" .
5891 "<published>$cd{'iso-8601'}</published>\n" .
5892 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5893 "<id>$co_url</id>\n" .
5894 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5895 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5897 my $comment = $co{'comment'};
5899 foreach my $line (@$comment) {
5900 $line = esc_html($line);
5903 print "</pre><ul>\n";
5904 foreach my $difftree_line (@difftree) {
5905 my %difftree = parse_difftree_raw_line($difftree_line);
5906 next if !$difftree{'from_id'};
5908 my $file = $difftree{'file'} || $difftree{'to_file'};
5912 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5913 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5914 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5915 file_name=>$file, file_parent=>$difftree{'from_file'}),
5916 -title => "diff"}, 'D');
5918 print $cgi->a({-href => href(-full=>1, action=>"blame",
5919 file_name=>$file, hash_base=>$commit),
5920 -title => "blame"}, 'B');
5922 # if this is not a feed of a file history
5923 if (!defined $file_name || $file_name ne $file) {
5924 print $cgi->a({-href => href(-full=>1, action=>"history",
5925 file_name=>$file, hash=>$commit),
5926 -title => "history"}, 'H');
5928 $file = esc_path($file);
5932 if ($format eq 'rss') {
5933 print "</ul>]]>\n" .
5934 "</content:encoded>\n" .
5936 } elsif ($format eq 'atom') {
5937 print "</ul>\n</div>\n" .
5944 if ($format eq 'rss') {
5945 print "</channel>\n</rss>\n";
5946 } elsif ($format eq 'atom') {
5960 my @list = git_get_projects_list();
5963 -type => 'text/xml',
5964 -charset => 'utf-8',
5965 -content_disposition => 'inline; filename="opml.xml"');
5967 my $sitename = $c->config->{sitename};
5969 <?xml version="1.0" encoding="utf-8"?>
5970 <opml version="1.0">
5972 <title>$sitename OPML Export</title>
5975 <outline text="git RSS feeds">
5978 foreach my $pr (@list) {
5980 my $head = git_get_head_hash($proj{'path'});
5981 if (!defined $head) {
5984 $git_dir = "$projectroot/$proj{'path'}";
5985 my %co = parse_commit($head);
5990 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5991 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
5992 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
5993 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";