3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser set_message);
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
22 if (eval { require Time::HiRes; 1; }) {
23 $t0 = [Time::HiRes::gettimeofday()];
25 our $number_of_git_cmds = 0;
28 CGI->compile() if $ENV{'MOD_PERL'};
32 our $version = "++GIT_VERSION++";
33 our $my_url = $cgi->url();
34 our $my_uri = $cgi->url(-absolute => 1);
36 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
37 # needed and used only for URLs with nonempty PATH_INFO
38 our $base_url = $my_url;
40 # When the script is used as DirectoryIndex, the URL does not contain the name
41 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
42 # have to do it ourselves. We make $path_info global because it's also used
45 # Another issue with the script being the DirectoryIndex is that the resulting
46 # $my_url data is not the full script URL: this is good, because we want
47 # generated links to keep implying the script name if it wasn't explicitly
48 # indicated in the URL we're handling, but it means that $my_url cannot be used
50 # Therefore, if we needed to strip PATH_INFO, then we know that we have
51 # to build the base URL ourselves:
52 our $path_info = $ENV{"PATH_INFO"};
54 if ($my_url =~ s,\Q$path_info\E$,, &&
55 $my_uri =~ s,\Q$path_info\E$,, &&
56 defined $ENV{'SCRIPT_NAME'}) {
57 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
61 # core git executable to use
62 # this can just be "git" if your webserver has a sensible PATH
63 our $GIT = "++GIT_BINDIR++/git";
65 # absolute fs-path which will be prepended to the project path
66 #our $projectroot = "/pub/scm";
67 our $projectroot = "++GITWEB_PROJECTROOT++";
69 # fs traversing limit for getting project list
70 # the number is relative to the projectroot
71 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
73 # target of the home link on top of all pages
74 our $home_link = $my_uri || "/";
76 # string of the home link on top of all pages
77 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
79 # name of your site or organization to appear in page titles
80 # replace this with something more descriptive for clearer bookmarks
81 our $site_name = "++GITWEB_SITENAME++"
82 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
84 # filename of html text to include at top of each page
85 our $site_header = "++GITWEB_SITE_HEADER++";
86 # html text to include at home page
87 our $home_text = "++GITWEB_HOMETEXT++";
88 # filename of html text to include at bottom of each page
89 our $site_footer = "++GITWEB_SITE_FOOTER++";
92 our @stylesheets = ("++GITWEB_CSS++");
93 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
94 our $stylesheet = undef;
95 # URI of GIT logo (72x27 size)
96 our $logo = "++GITWEB_LOGO++";
97 # URI of GIT favicon, assumed to be image/png type
98 our $favicon = "++GITWEB_FAVICON++";
99 # URI of gitweb.js (JavaScript code for gitweb)
100 our $javascript = "++GITWEB_JS++";
102 # URI and label (title) of GIT logo link
103 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
104 #our $logo_label = "git documentation";
105 our $logo_url = "http://git-scm.com/";
106 our $logo_label = "git homepage";
108 # source of projects list
109 our $projects_list = "++GITWEB_LIST++";
111 # the width (in characters) of the projects list "Description" column
112 our $projects_list_description_width = 25;
114 # default order of projects list
115 # valid values are none, project, descr, owner, and age
116 our $default_projects_order = "project";
118 # show repository only if this file exists
119 # (only effective if this variable evaluates to true)
120 our $export_ok = "++GITWEB_EXPORT_OK++";
122 # show repository only if this subroutine returns true
123 # when given the path to the project, for example:
124 # sub { return -e "$_[0]/git-daemon-export-ok"; }
125 our $export_auth_hook = undef;
127 # only allow viewing of repositories also shown on the overview page
128 our $strict_export = "++GITWEB_STRICT_EXPORT++";
130 # list of git base URLs used for URL to where fetch project from,
131 # i.e. full URL is "$git_base_url/$project"
132 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
134 # default blob_plain mimetype and default charset for text/plain blob
135 our $default_blob_plain_mimetype = 'text/plain';
136 our $default_text_plain_charset = undef;
138 # file to use for guessing MIME types before trying /etc/mime.types
139 # (relative to the current git repository)
140 our $mimetypes_file = undef;
142 # assume this charset if line contains non-UTF-8 characters;
143 # it should be valid encoding (see Encoding::Supported(3pm) for list),
144 # for which encoding all byte sequences are valid, for example
145 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
146 # could be even 'utf-8' for the old behavior)
147 our $fallback_encoding = 'latin1';
149 # rename detection options for git-diff and git-diff-tree
150 # - default is '-M', with the cost proportional to
151 # (number of removed files) * (number of new files).
152 # - more costly is '-C' (which implies '-M'), with the cost proportional to
153 # (number of changed files + number of removed files) * (number of new files)
154 # - even more costly is '-C', '--find-copies-harder' with cost
155 # (number of files in the original tree) * (number of new files)
156 # - one might want to include '-B' option, e.g. '-B', '-M'
157 our @diff_opts = ('-M'); # taken from git_commit
159 # Disables features that would allow repository owners to inject script into
161 our $prevent_xss = 0;
163 # information about snapshot formats that gitweb is capable of serving
164 our %known_snapshot_formats = (
166 # 'display' => display name,
167 # 'type' => mime type,
168 # 'suffix' => filename suffix,
169 # 'format' => --format for git-archive,
170 # 'compressor' => [compressor command and arguments]
171 # (array reference, optional)
172 # 'disabled' => boolean (optional)}
175 'display' => 'tar.gz',
176 'type' => 'application/x-gzip',
177 'suffix' => '.tar.gz',
179 'compressor' => ['gzip']},
182 'display' => 'tar.bz2',
183 'type' => 'application/x-bzip2',
184 'suffix' => '.tar.bz2',
186 'compressor' => ['bzip2']},
189 'display' => 'tar.xz',
190 'type' => 'application/x-xz',
191 'suffix' => '.tar.xz',
193 'compressor' => ['xz'],
198 'type' => 'application/x-zip',
203 # Aliases so we understand old gitweb.snapshot values in repository
205 our %known_snapshot_format_aliases = (
210 # backward compatibility: legacy gitweb config support
211 'x-gzip' => undef, 'gz' => undef,
212 'x-bzip2' => undef, 'bz2' => undef,
213 'x-zip' => undef, '' => undef,
216 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
217 # are changed, it may be appropriate to change these values too via
224 # Used to set the maximum load that we will still respond to gitweb queries.
225 # If server load exceed this value then return "503 server busy" error.
226 # If gitweb cannot determined server load, it is taken to be 0.
227 # Leave it undefined (or set to 'undef') to turn off load checking.
230 # You define site-wide feature defaults here; override them with
231 # $GITWEB_CONFIG as necessary.
234 # 'sub' => feature-sub (subroutine),
235 # 'override' => allow-override (boolean),
236 # 'default' => [ default options...] (array reference)}
238 # if feature is overridable (it means that allow-override has true value),
239 # then feature-sub will be called with default options as parameters;
240 # return value of feature-sub indicates if to enable specified feature
242 # if there is no 'sub' key (no feature-sub), then feature cannot be
245 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
246 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
249 # Enable the 'blame' blob view, showing the last commit that modified
250 # each line in the file. This can be very CPU-intensive.
252 # To enable system wide have in $GITWEB_CONFIG
253 # $feature{'blame'}{'default'} = [1];
254 # To have project specific config enable override in $GITWEB_CONFIG
255 # $feature{'blame'}{'override'} = 1;
256 # and in project config gitweb.blame = 0|1;
258 'sub' => sub { feature_bool('blame', @_) },
262 # Enable the 'snapshot' link, providing a compressed archive of any
263 # tree. This can potentially generate high traffic if you have large
266 # Value is a list of formats defined in %known_snapshot_formats that
268 # To disable system wide have in $GITWEB_CONFIG
269 # $feature{'snapshot'}{'default'} = [];
270 # To have project specific config enable override in $GITWEB_CONFIG
271 # $feature{'snapshot'}{'override'} = 1;
272 # and in project config, a comma-separated list of formats or "none"
273 # to disable. Example: gitweb.snapshot = tbz2,zip;
275 'sub' => \&feature_snapshot,
277 'default' => ['tgz']},
279 # Enable text search, which will list the commits which match author,
280 # committer or commit text to a given string. Enabled by default.
281 # Project specific override is not supported.
286 # Enable grep search, which will list the files in currently selected
287 # tree containing the given string. Enabled by default. This can be
288 # potentially CPU-intensive, of course.
290 # To enable system wide have in $GITWEB_CONFIG
291 # $feature{'grep'}{'default'} = [1];
292 # To have project specific config enable override in $GITWEB_CONFIG
293 # $feature{'grep'}{'override'} = 1;
294 # and in project config gitweb.grep = 0|1;
296 'sub' => sub { feature_bool('grep', @_) },
300 # Enable the pickaxe search, which will list the commits that modified
301 # a given string in a file. This can be practical and quite faster
302 # alternative to 'blame', but still potentially CPU-intensive.
304 # To enable system wide have in $GITWEB_CONFIG
305 # $feature{'pickaxe'}{'default'} = [1];
306 # To have project specific config enable override in $GITWEB_CONFIG
307 # $feature{'pickaxe'}{'override'} = 1;
308 # and in project config gitweb.pickaxe = 0|1;
310 'sub' => sub { feature_bool('pickaxe', @_) },
314 # Enable showing size of blobs in a 'tree' view, in a separate
315 # column, similar to what 'ls -l' does. This cost a bit of IO.
317 # To disable system wide have in $GITWEB_CONFIG
318 # $feature{'show-sizes'}{'default'} = [0];
319 # To have project specific config enable override in $GITWEB_CONFIG
320 # $feature{'show-sizes'}{'override'} = 1;
321 # and in project config gitweb.showsizes = 0|1;
323 'sub' => sub { feature_bool('showsizes', @_) },
327 # Make gitweb use an alternative format of the URLs which can be
328 # more readable and natural-looking: project name is embedded
329 # directly in the path and the query string contains other
330 # auxiliary information. All gitweb installations recognize
331 # URL in either format; this configures in which formats gitweb
334 # To enable system wide have in $GITWEB_CONFIG
335 # $feature{'pathinfo'}{'default'} = [1];
336 # Project specific override is not supported.
338 # Note that you will need to change the default location of CSS,
339 # favicon, logo and possibly other files to an absolute URL. Also,
340 # if gitweb.cgi serves as your indexfile, you will need to force
341 # $my_uri to contain the script name in your $GITWEB_CONFIG.
346 # Make gitweb consider projects in project root subdirectories
347 # to be forks of existing projects. Given project $projname.git,
348 # projects matching $projname/*.git will not be shown in the main
349 # projects list, instead a '+' mark will be added to $projname
350 # there and a 'forks' view will be enabled for the project, listing
351 # all the forks. If project list is taken from a file, forks have
352 # to be listed after the main project.
354 # To enable system wide have in $GITWEB_CONFIG
355 # $feature{'forks'}{'default'} = [1];
356 # Project specific override is not supported.
361 # Insert custom links to the action bar of all project pages.
362 # This enables you mainly to link to third-party scripts integrating
363 # into gitweb; e.g. git-browser for graphical history representation
364 # or custom web-based repository administration interface.
366 # The 'default' value consists of a list of triplets in the form
367 # (label, link, position) where position is the label after which
368 # to insert the link and link is a format string where %n expands
369 # to the project name, %f to the project path within the filesystem,
370 # %h to the current hash (h gitweb parameter) and %b to the current
371 # hash base (hb gitweb parameter); %% expands to %.
373 # To enable system wide have in $GITWEB_CONFIG e.g.
374 # $feature{'actions'}{'default'} = [('graphiclog',
375 # '/git-browser/by-commit.html?r=%n', 'summary')];
376 # Project specific override is not supported.
381 # Allow gitweb scan project content tags described in ctags/
382 # of project repository, and display the popular Web 2.0-ish
383 # "tag cloud" near the project list. Note that this is something
384 # COMPLETELY different from the normal Git tags.
386 # gitweb by itself can show existing tags, but it does not handle
387 # tagging itself; you need an external application for that.
388 # For an example script, check Girocco's cgi/tagproj.cgi.
389 # You may want to install the HTML::TagCloud Perl module to get
390 # a pretty tag cloud instead of just a list of tags.
392 # To enable system wide have in $GITWEB_CONFIG
393 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
394 # Project specific override is not supported.
399 # The maximum number of patches in a patchset generated in patch
400 # view. Set this to 0 or undef to disable patch view, or to a
401 # negative number to remove any limit.
403 # To disable system wide have in $GITWEB_CONFIG
404 # $feature{'patches'}{'default'} = [0];
405 # To have project specific config enable override in $GITWEB_CONFIG
406 # $feature{'patches'}{'override'} = 1;
407 # and in project config gitweb.patches = 0|n;
408 # where n is the maximum number of patches allowed in a patchset.
410 'sub' => \&feature_patches,
414 # Avatar support. When this feature is enabled, views such as
415 # shortlog or commit will display an avatar associated with
416 # the email of the committer(s) and/or author(s).
418 # Currently available providers are gravatar and picon.
419 # If an unknown provider is specified, the feature is disabled.
421 # Gravatar depends on Digest::MD5.
422 # Picon currently relies on the indiana.edu database.
424 # To enable system wide have in $GITWEB_CONFIG
425 # $feature{'avatar'}{'default'} = ['<provider>'];
426 # where <provider> is either gravatar or picon.
427 # To have project specific config enable override in $GITWEB_CONFIG
428 # $feature{'avatar'}{'override'} = 1;
429 # and in project config gitweb.avatar = <provider>;
431 'sub' => \&feature_avatar,
435 # Enable displaying how much time and how many git commands
436 # it took to generate and display page. Disabled by default.
437 # Project specific override is not supported.
442 # Enable turning some links into links to actions which require
443 # JavaScript to run (like 'blame_incremental'). Not enabled by
444 # default. Project specific override is currently not supported.
445 'javascript-actions' => {
450 sub gitweb_get_feature {
452 return unless exists $feature{$name};
453 my ($sub, $override, @defaults) = (
454 $feature{$name}{'sub'},
455 $feature{$name}{'override'},
456 @{$feature{$name}{'default'}});
457 # project specific override is possible only if we have project
458 our $git_dir; # global variable, declared later
459 if (!$override || !defined $git_dir) {
463 warn "feature $name is not overridable";
466 return $sub->(@defaults);
469 # A wrapper to check if a given feature is enabled.
470 # With this, you can say
472 # my $bool_feat = gitweb_check_feature('bool_feat');
473 # gitweb_check_feature('bool_feat') or somecode;
477 # my ($bool_feat) = gitweb_get_feature('bool_feat');
478 # (gitweb_get_feature('bool_feat'))[0] or somecode;
480 sub gitweb_check_feature {
481 return (gitweb_get_feature(@_))[0];
487 my ($val) = git_get_project_config($key, '--bool');
491 } elsif ($val eq 'true') {
493 } elsif ($val eq 'false') {
498 sub feature_snapshot {
501 my ($val) = git_get_project_config('snapshot');
504 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
510 sub feature_patches {
511 my @val = (git_get_project_config('patches', '--int'));
521 my @val = (git_get_project_config('avatar'));
523 return @val ? @val : @_;
526 # checking HEAD file with -e is fragile if the repository was
527 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
529 sub check_head_link {
531 my $headfile = "$dir/HEAD";
532 return ((-e $headfile) ||
533 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
536 sub check_export_ok {
538 return (check_head_link($dir) &&
539 (!$export_ok || -e "$dir/$export_ok") &&
540 (!$export_auth_hook || $export_auth_hook->($dir)));
543 # process alternate names for backward compatibility
544 # filter out unsupported (unknown) snapshot formats
545 sub filter_snapshot_fmts {
549 exists $known_snapshot_format_aliases{$_} ?
550 $known_snapshot_format_aliases{$_} : $_} @fmts;
552 exists $known_snapshot_formats{$_} &&
553 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
556 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
557 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
558 # die if there are errors parsing config file
559 if (-e $GITWEB_CONFIG) {
562 } elsif (-e $GITWEB_CONFIG_SYSTEM) {
563 do $GITWEB_CONFIG_SYSTEM;
567 # Get loadavg of system, to compare against $maxload.
568 # Currently it requires '/proc/loadavg' present to get loadavg;
569 # if it is not present it returns 0, which means no load checking.
571 if( -e '/proc/loadavg' ){
572 open my $fd, '<', '/proc/loadavg'
574 my @load = split(/\s+/, scalar <$fd>);
577 # The first three columns measure CPU and IO utilization of the last one,
578 # five, and 10 minute periods. The fourth column shows the number of
579 # currently running processes and the total number of processes in the m/n
580 # format. The last column displays the last process ID used.
581 return $load[0] || 0;
583 # additional checks for load average should go here for things that don't export
589 # version of the core git binary
590 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
591 $number_of_git_cmds++;
593 $projects_list ||= $projectroot;
595 if (defined $maxload && get_loadavg() > $maxload) {
596 die_error(503, "The load average on the server is too high");
599 # ======================================================================
600 # input validation and dispatch
602 # input parameters can be collected from a variety of sources (presently, CGI
603 # and PATH_INFO), so we define an %input_params hash that collects them all
604 # together during validation: this allows subsequent uses (e.g. href()) to be
605 # agnostic of the parameter origin
607 our %input_params = ();
609 # input parameters are stored with the long parameter name as key. This will
610 # also be used in the href subroutine to convert parameters to their CGI
611 # equivalent, and since the href() usage is the most frequent one, we store
612 # the name -> CGI key mapping here, instead of the reverse.
614 # XXX: Warning: If you touch this, check the search form for updating,
617 our @cgi_param_mapping = (
625 hash_parent_base => "hpb",
630 snapshot_format => "sf",
631 extra_options => "opt",
632 search_use_regexp => "sr",
633 # this must be last entry (for manipulation from JavaScript)
636 our %cgi_param_mapping = @cgi_param_mapping;
638 # we will also need to know the possible actions, for validation
640 "blame" => \&git_blame,
641 "blame_incremental" => \&git_blame_incremental,
642 "blame_data" => \&git_blame_data,
643 "blobdiff" => \&git_blobdiff,
644 "blobdiff_plain" => \&git_blobdiff_plain,
645 "blob" => \&git_blob,
646 "blob_plain" => \&git_blob_plain,
647 "commitdiff" => \&git_commitdiff,
648 "commitdiff_plain" => \&git_commitdiff_plain,
649 "commit" => \&git_commit,
650 "forks" => \&git_forks,
651 "heads" => \&git_heads,
652 "history" => \&git_history,
654 "patch" => \&git_patch,
655 "patches" => \&git_patches,
657 "atom" => \&git_atom,
658 "search" => \&git_search,
659 "search_help" => \&git_search_help,
660 "shortlog" => \&git_shortlog,
661 "summary" => \&git_summary,
663 "tags" => \&git_tags,
664 "tree" => \&git_tree,
665 "snapshot" => \&git_snapshot,
666 "object" => \&git_object,
667 # those below don't need $project
668 "opml" => \&git_opml,
669 "project_list" => \&git_project_list,
670 "project_index" => \&git_project_index,
673 # finally, we have the hash of allowed extra_options for the commands that
675 our %allowed_options = (
676 "--no-merges" => [ qw(rss atom log shortlog history) ],
679 # fill %input_params with the CGI parameters. All values except for 'opt'
680 # should be single values, but opt can be an array. We should probably
681 # build an array of parameters that can be multi-valued, but since for the time
682 # being it's only this one, we just single it out
683 while (my ($name, $symbol) = each %cgi_param_mapping) {
684 if ($symbol eq 'opt') {
685 $input_params{$name} = [ $cgi->param($symbol) ];
687 $input_params{$name} = $cgi->param($symbol);
691 # now read PATH_INFO and update the parameter list for missing parameters
692 sub evaluate_path_info {
693 return if defined $input_params{'project'};
694 return if !$path_info;
695 $path_info =~ s,^/+,,;
696 return if !$path_info;
698 # find which part of PATH_INFO is project
699 my $project = $path_info;
701 while ($project && !check_head_link("$projectroot/$project")) {
702 $project =~ s,/*[^/]*$,,;
704 return unless $project;
705 $input_params{'project'} = $project;
707 # do not change any parameters if an action is given using the query string
708 return if $input_params{'action'};
709 $path_info =~ s,^\Q$project\E/*,,;
711 # next, check if we have an action
712 my $action = $path_info;
714 if (exists $actions{$action}) {
715 $path_info =~ s,^$action/*,,;
716 $input_params{'action'} = $action;
719 # list of actions that want hash_base instead of hash, but can have no
720 # pathname (f) parameter
727 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
728 my ($parentrefname, $parentpathname, $refname, $pathname) =
729 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
731 # first, analyze the 'current' part
732 if (defined $pathname) {
733 # we got "branch:filename" or "branch:dir/"
734 # we could use git_get_type(branch:pathname), but:
735 # - it needs $git_dir
736 # - it does a git() call
737 # - the convention of terminating directories with a slash
738 # makes it superfluous
739 # - embedding the action in the PATH_INFO would make it even
741 $pathname =~ s,^/+,,;
742 if (!$pathname || substr($pathname, -1) eq "/") {
743 $input_params{'action'} ||= "tree";
746 # the default action depends on whether we had parent info
748 if ($parentrefname) {
749 $input_params{'action'} ||= "blobdiff_plain";
751 $input_params{'action'} ||= "blob_plain";
754 $input_params{'hash_base'} ||= $refname;
755 $input_params{'file_name'} ||= $pathname;
756 } elsif (defined $refname) {
757 # we got "branch". In this case we have to choose if we have to
758 # set hash or hash_base.
760 # Most of the actions without a pathname only want hash to be
761 # set, except for the ones specified in @wants_base that want
762 # hash_base instead. It should also be noted that hand-crafted
763 # links having 'history' as an action and no pathname or hash
764 # set will fail, but that happens regardless of PATH_INFO.
765 $input_params{'action'} ||= "shortlog";
766 if (grep { $_ eq $input_params{'action'} } @wants_base) {
767 $input_params{'hash_base'} ||= $refname;
769 $input_params{'hash'} ||= $refname;
773 # next, handle the 'parent' part, if present
774 if (defined $parentrefname) {
775 # a missing pathspec defaults to the 'current' filename, allowing e.g.
776 # someproject/blobdiff/oldrev..newrev:/filename
777 if ($parentpathname) {
778 $parentpathname =~ s,^/+,,;
779 $parentpathname =~ s,/$,,;
780 $input_params{'file_parent'} ||= $parentpathname;
782 $input_params{'file_parent'} ||= $input_params{'file_name'};
784 # we assume that hash_parent_base is wanted if a path was specified,
785 # or if the action wants hash_base instead of hash
786 if (defined $input_params{'file_parent'} ||
787 grep { $_ eq $input_params{'action'} } @wants_base) {
788 $input_params{'hash_parent_base'} ||= $parentrefname;
790 $input_params{'hash_parent'} ||= $parentrefname;
794 # for the snapshot action, we allow URLs in the form
795 # $project/snapshot/$hash.ext
796 # where .ext determines the snapshot and gets removed from the
797 # passed $refname to provide the $hash.
799 # To be able to tell that $refname includes the format extension, we
800 # require the following two conditions to be satisfied:
801 # - the hash input parameter MUST have been set from the $refname part
802 # of the URL (i.e. they must be equal)
803 # - the snapshot format MUST NOT have been defined already (e.g. from
805 # It's also useless to try any matching unless $refname has a dot,
806 # so we check for that too
807 if (defined $input_params{'action'} &&
808 $input_params{'action'} eq 'snapshot' &&
809 defined $refname && index($refname, '.') != -1 &&
810 $refname eq $input_params{'hash'} &&
811 !defined $input_params{'snapshot_format'}) {
812 # We loop over the known snapshot formats, checking for
813 # extensions. Allowed extensions are both the defined suffix
814 # (which includes the initial dot already) and the snapshot
815 # format key itself, with a prepended dot
816 while (my ($fmt, $opt) = each %known_snapshot_formats) {
818 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
822 # a valid suffix was found, so set the snapshot format
823 # and reset the hash parameter
824 $input_params{'snapshot_format'} = $fmt;
825 $input_params{'hash'} = $hash;
826 # we also set the format suffix to the one requested
827 # in the URL: this way a request for e.g. .tgz returns
828 # a .tgz instead of a .tar.gz
829 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
834 evaluate_path_info();
836 our $action = $input_params{'action'};
837 if (defined $action) {
838 if (!validate_action($action)) {
839 die_error(400, "Invalid action parameter");
843 # parameters which are pathnames
844 our $project = $input_params{'project'};
845 if (defined $project) {
846 if (!validate_project($project)) {
848 die_error(404, "No such project");
852 our $file_name = $input_params{'file_name'};
853 if (defined $file_name) {
854 if (!validate_pathname($file_name)) {
855 die_error(400, "Invalid file parameter");
859 our $file_parent = $input_params{'file_parent'};
860 if (defined $file_parent) {
861 if (!validate_pathname($file_parent)) {
862 die_error(400, "Invalid file parent parameter");
866 # parameters which are refnames
867 our $hash = $input_params{'hash'};
869 if (!validate_refname($hash)) {
870 die_error(400, "Invalid hash parameter");
874 our $hash_parent = $input_params{'hash_parent'};
875 if (defined $hash_parent) {
876 if (!validate_refname($hash_parent)) {
877 die_error(400, "Invalid hash parent parameter");
881 our $hash_base = $input_params{'hash_base'};
882 if (defined $hash_base) {
883 if (!validate_refname($hash_base)) {
884 die_error(400, "Invalid hash base parameter");
888 our @extra_options = @{$input_params{'extra_options'}};
889 # @extra_options is always defined, since it can only be (currently) set from
890 # CGI, and $cgi->param() returns the empty array in array context if the param
892 foreach my $opt (@extra_options) {
893 if (not exists $allowed_options{$opt}) {
894 die_error(400, "Invalid option parameter");
896 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
897 die_error(400, "Invalid option parameter for this action");
901 our $hash_parent_base = $input_params{'hash_parent_base'};
902 if (defined $hash_parent_base) {
903 if (!validate_refname($hash_parent_base)) {
904 die_error(400, "Invalid hash parent base parameter");
909 our $page = $input_params{'page'};
911 if ($page =~ m/[^0-9]/) {
912 die_error(400, "Invalid page parameter");
916 our $searchtype = $input_params{'searchtype'};
917 if (defined $searchtype) {
918 if ($searchtype =~ m/[^a-z]/) {
919 die_error(400, "Invalid searchtype parameter");
923 our $search_use_regexp = $input_params{'search_use_regexp'};
925 our $searchtext = $input_params{'searchtext'};
927 if (defined $searchtext) {
928 if (length($searchtext) < 2) {
929 die_error(403, "At least two characters are required for search parameter");
931 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
934 # path to the current git repository
936 $git_dir = "$projectroot/$project" if $project;
938 # list of supported snapshot formats
939 our @snapshot_fmts = gitweb_get_feature('snapshot');
940 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
942 # check that the avatar feature is set to a known provider name,
943 # and for each provider check if the dependencies are satisfied.
944 # if the provider name is invalid or the dependencies are not met,
945 # reset $git_avatar to the empty string.
946 our ($git_avatar) = gitweb_get_feature('avatar');
947 if ($git_avatar eq 'gravatar') {
948 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
949 } elsif ($git_avatar eq 'picon') {
955 # custom error handler: 'die <message>' is Internal Server Error
956 sub handle_errors_html {
957 my $msg = shift; # it is already HTML escaped
959 # to avoid infinite loop where error occurs in die_error,
960 # change handler to default handler, disabling handle_errors_html
961 set_message("Error occured when inside die_error:\n$msg");
963 # you cannot jump out of die_error when called as error handler;
964 # the subroutine set via CGI::Carp::set_message is called _after_
965 # HTTP headers are already written, so it cannot write them itself
966 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
968 set_message(\&handle_errors_html);
971 if (!defined $action) {
973 $action = git_get_type($hash);
974 } elsif (defined $hash_base && defined $file_name) {
975 $action = git_get_type("$hash_base:$file_name");
976 } elsif (defined $project) {
979 $action = 'project_list';
982 if (!defined($actions{$action})) {
983 die_error(400, "Unknown action");
985 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
987 die_error(400, "Project needed");
989 $actions{$action}->();
993 ## ======================================================================
996 # possible values of extra options
997 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
998 # -replay => 1 - start from a current view (replay with modifications)
999 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1002 # default is to use -absolute url() i.e. $my_uri
1003 my $href = $params{-full} ? $my_url : $my_uri;
1005 $params{'project'} = $project unless exists $params{'project'};
1007 if ($params{-replay}) {
1008 while (my ($name, $symbol) = each %cgi_param_mapping) {
1009 if (!exists $params{$name}) {
1010 $params{$name} = $input_params{$name};
1015 my $use_pathinfo = gitweb_check_feature('pathinfo');
1016 if (defined $params{'project'} &&
1017 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1018 # try to put as many parameters as possible in PATH_INFO:
1021 # - hash_parent or hash_parent_base:/file_parent
1022 # - hash or hash_base:/filename
1023 # - the snapshot_format as an appropriate suffix
1025 # When the script is the root DirectoryIndex for the domain,
1026 # $href here would be something like http://gitweb.example.com/
1027 # Thus, we strip any trailing / from $href, to spare us double
1028 # slashes in the final URL
1031 # Then add the project name, if present
1032 $href .= "/".esc_url($params{'project'});
1033 delete $params{'project'};
1035 # since we destructively absorb parameters, we keep this
1036 # boolean that remembers if we're handling a snapshot
1037 my $is_snapshot = $params{'action'} eq 'snapshot';
1039 # Summary just uses the project path URL, any other action is
1041 if (defined $params{'action'}) {
1042 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
1043 delete $params{'action'};
1046 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1047 # stripping nonexistent or useless pieces
1048 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1049 || $params{'hash_parent'} || $params{'hash'});
1050 if (defined $params{'hash_base'}) {
1051 if (defined $params{'hash_parent_base'}) {
1052 $href .= esc_url($params{'hash_parent_base'});
1053 # skip the file_parent if it's the same as the file_name
1054 if (defined $params{'file_parent'}) {
1055 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1056 delete $params{'file_parent'};
1057 } elsif ($params{'file_parent'} !~ /\.\./) {
1058 $href .= ":/".esc_url($params{'file_parent'});
1059 delete $params{'file_parent'};
1063 delete $params{'hash_parent'};
1064 delete $params{'hash_parent_base'};
1065 } elsif (defined $params{'hash_parent'}) {
1066 $href .= esc_url($params{'hash_parent'}). "..";
1067 delete $params{'hash_parent'};
1070 $href .= esc_url($params{'hash_base'});
1071 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1072 $href .= ":/".esc_url($params{'file_name'});
1073 delete $params{'file_name'};
1075 delete $params{'hash'};
1076 delete $params{'hash_base'};
1077 } elsif (defined $params{'hash'}) {
1078 $href .= esc_url($params{'hash'});
1079 delete $params{'hash'};
1082 # If the action was a snapshot, we can absorb the
1083 # snapshot_format parameter too
1085 my $fmt = $params{'snapshot_format'};
1086 # snapshot_format should always be defined when href()
1087 # is called, but just in case some code forgets, we
1088 # fall back to the default
1089 $fmt ||= $snapshot_fmts[0];
1090 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1091 delete $params{'snapshot_format'};
1095 # now encode the parameters explicitly
1097 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1098 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1099 if (defined $params{$name}) {
1100 if (ref($params{$name}) eq "ARRAY") {
1101 foreach my $par (@{$params{$name}}) {
1102 push @result, $symbol . "=" . esc_param($par);
1105 push @result, $symbol . "=" . esc_param($params{$name});
1109 $href .= "?" . join(';', @result) if scalar @result;
1115 ## ======================================================================
1116 ## validation, quoting/unquoting and escaping
1118 sub validate_action {
1119 my $input = shift || return undef;
1120 return undef unless exists $actions{$input};
1124 sub validate_project {
1125 my $input = shift || return undef;
1126 if (!validate_pathname($input) ||
1127 !(-d "$projectroot/$input") ||
1128 !check_export_ok("$projectroot/$input") ||
1129 ($strict_export && !project_in_list($input))) {
1136 sub validate_pathname {
1137 my $input = shift || return undef;
1139 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1140 # at the beginning, at the end, and between slashes.
1141 # also this catches doubled slashes
1142 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1145 # no null characters
1146 if ($input =~ m!\0!) {
1152 sub validate_refname {
1153 my $input = shift || return undef;
1155 # textual hashes are O.K.
1156 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1159 # it must be correct pathname
1160 $input = validate_pathname($input)
1162 # restrictions on ref name according to git-check-ref-format
1163 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1169 # decode sequences of octets in utf8 into Perl's internal form,
1170 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1171 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1174 return undef unless defined $str;
1175 if (utf8::valid($str)) {
1179 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1183 # quote unsafe chars, but keep the slash, even when it's not
1184 # correct, but quoted slashes look too horrible in bookmarks
1187 return undef unless defined $str;
1188 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1193 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
1196 return undef unless defined $str;
1197 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1203 # replace invalid utf8 character with SUBSTITUTION sequence
1208 return undef unless defined $str;
1210 $str = to_utf8($str);
1211 $str = $cgi->escapeHTML($str);
1212 if ($opts{'-nbsp'}) {
1213 $str =~ s/ / /g;
1215 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1219 # quote control characters and escape filename to HTML
1224 return undef unless defined $str;
1226 $str = to_utf8($str);
1227 $str = $cgi->escapeHTML($str);
1228 if ($opts{'-nbsp'}) {
1229 $str =~ s/ / /g;
1231 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1235 # Make control characters "printable", using character escape codes (CEC)
1239 my %es = ( # character escape codes, aka escape sequences
1240 "\t" => '\t', # tab (HT)
1241 "\n" => '\n', # line feed (LF)
1242 "\r" => '\r', # carrige return (CR)
1243 "\f" => '\f', # form feed (FF)
1244 "\b" => '\b', # backspace (BS)
1245 "\a" => '\a', # alarm (bell) (BEL)
1246 "\e" => '\e', # escape (ESC)
1247 "\013" => '\v', # vertical tab (VT)
1248 "\000" => '\0', # nul character (NUL)
1250 my $chr = ( (exists $es{$cntrl})
1252 : sprintf('\%2x', ord($cntrl)) );
1253 if ($opts{-nohtml}) {
1256 return "<span class=\"cntrl\">$chr</span>";
1260 # Alternatively use unicode control pictures codepoints,
1261 # Unicode "printable representation" (PR)
1266 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1267 if ($opts{-nohtml}) {
1270 return "<span class=\"cntrl\">$chr</span>";
1274 # git may return quoted and escaped filenames
1280 my %es = ( # character escape codes, aka escape sequences
1281 't' => "\t", # tab (HT, TAB)
1282 'n' => "\n", # newline (NL)
1283 'r' => "\r", # return (CR)
1284 'f' => "\f", # form feed (FF)
1285 'b' => "\b", # backspace (BS)
1286 'a' => "\a", # alarm (bell) (BEL)
1287 'e' => "\e", # escape (ESC)
1288 'v' => "\013", # vertical tab (VT)
1291 if ($seq =~ m/^[0-7]{1,3}$/) {
1292 # octal char sequence
1293 return chr(oct($seq));
1294 } elsif (exists $es{$seq}) {
1295 # C escape sequence, aka character escape code
1298 # quoted ordinary character
1302 if ($str =~ m/^"(.*)"$/) {
1305 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1310 # escape tabs (convert tabs to spaces)
1314 while ((my $pos = index($line, "\t")) != -1) {
1315 if (my $count = (8 - ($pos % 8))) {
1316 my $spaces = ' ' x $count;
1317 $line =~ s/\t/$spaces/;
1324 sub project_in_list {
1325 my $project = shift;
1326 my @list = git_get_projects_list();
1327 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1330 ## ----------------------------------------------------------------------
1331 ## HTML aware string manipulation
1333 # Try to chop given string on a word boundary between position
1334 # $len and $len+$add_len. If there is no word boundary there,
1335 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1336 # (marking chopped part) would be longer than given string.
1340 my $add_len = shift || 10;
1341 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1343 # Make sure perl knows it is utf8 encoded so we don't
1344 # cut in the middle of a utf8 multibyte char.
1345 $str = to_utf8($str);
1347 # allow only $len chars, but don't cut a word if it would fit in $add_len
1348 # if it doesn't fit, cut it if it's still longer than the dots we would add
1349 # remove chopped character entities entirely
1351 # when chopping in the middle, distribute $len into left and right part
1352 # return early if chopping wouldn't make string shorter
1353 if ($where eq 'center') {
1354 return $str if ($len + 5 >= length($str)); # filler is length 5
1357 return $str if ($len + 4 >= length($str)); # filler is length 4
1360 # regexps: ending and beginning with word part up to $add_len
1361 my $endre = qr/.{$len}\w{0,$add_len}/;
1362 my $begre = qr/\w{0,$add_len}.{$len}/;
1364 if ($where eq 'left') {
1365 $str =~ m/^(.*?)($begre)$/;
1366 my ($lead, $body) = ($1, $2);
1367 if (length($lead) > 4) {
1370 return "$lead$body";
1372 } elsif ($where eq 'center') {
1373 $str =~ m/^($endre)(.*)$/;
1374 my ($left, $str) = ($1, $2);
1375 $str =~ m/^(.*?)($begre)$/;
1376 my ($mid, $right) = ($1, $2);
1377 if (length($mid) > 5) {
1380 return "$left$mid$right";
1383 $str =~ m/^($endre)(.*)$/;
1386 if (length($tail) > 4) {
1389 return "$body$tail";
1393 # takes the same arguments as chop_str, but also wraps a <span> around the
1394 # result with a title attribute if it does get chopped. Additionally, the
1395 # string is HTML-escaped.
1396 sub chop_and_escape_str {
1399 my $chopped = chop_str(@_);
1400 if ($chopped eq $str) {
1401 return esc_html($chopped);
1403 $str =~ s/[[:cntrl:]]/?/g;
1404 return $cgi->span({-title=>$str}, esc_html($chopped));
1408 ## ----------------------------------------------------------------------
1409 ## functions returning short strings
1411 # CSS class for given age value (in seconds)
1415 if (!defined $age) {
1417 } elsif ($age < 60*60*2) {
1419 } elsif ($age < 60*60*24*2) {
1426 # convert age in seconds to "nn units ago" string
1431 if ($age > 60*60*24*365*2) {
1432 $age_str = (int $age/60/60/24/365);
1433 $age_str .= " years ago";
1434 } elsif ($age > 60*60*24*(365/12)*2) {
1435 $age_str = int $age/60/60/24/(365/12);
1436 $age_str .= " months ago";
1437 } elsif ($age > 60*60*24*7*2) {
1438 $age_str = int $age/60/60/24/7;
1439 $age_str .= " weeks ago";
1440 } elsif ($age > 60*60*24*2) {
1441 $age_str = int $age/60/60/24;
1442 $age_str .= " days ago";
1443 } elsif ($age > 60*60*2) {
1444 $age_str = int $age/60/60;
1445 $age_str .= " hours ago";
1446 } elsif ($age > 60*2) {
1447 $age_str = int $age/60;
1448 $age_str .= " min ago";
1449 } elsif ($age > 2) {
1450 $age_str = int $age;
1451 $age_str .= " sec ago";
1453 $age_str .= " right now";
1459 S_IFINVALID => 0030000,
1460 S_IFGITLINK => 0160000,
1463 # submodule/subproject, a commit object reference
1467 return (($mode & S_IFMT) == S_IFGITLINK)
1470 # convert file mode in octal to symbolic file mode string
1472 my $mode = oct shift;
1474 if (S_ISGITLINK($mode)) {
1475 return 'm---------';
1476 } elsif (S_ISDIR($mode & S_IFMT)) {
1477 return 'drwxr-xr-x';
1478 } elsif (S_ISLNK($mode)) {
1479 return 'lrwxrwxrwx';
1480 } elsif (S_ISREG($mode)) {
1481 # git cares only about the executable bit
1482 if ($mode & S_IXUSR) {
1483 return '-rwxr-xr-x';
1485 return '-rw-r--r--';
1488 return '----------';
1492 # convert file mode in octal to file type string
1496 if ($mode !~ m/^[0-7]+$/) {
1502 if (S_ISGITLINK($mode)) {
1504 } elsif (S_ISDIR($mode & S_IFMT)) {
1506 } elsif (S_ISLNK($mode)) {
1508 } elsif (S_ISREG($mode)) {
1515 # convert file mode in octal to file type description string
1516 sub file_type_long {
1519 if ($mode !~ m/^[0-7]+$/) {
1525 if (S_ISGITLINK($mode)) {
1527 } elsif (S_ISDIR($mode & S_IFMT)) {
1529 } elsif (S_ISLNK($mode)) {
1531 } elsif (S_ISREG($mode)) {
1532 if ($mode & S_IXUSR) {
1533 return "executable";
1543 ## ----------------------------------------------------------------------
1544 ## functions returning short HTML fragments, or transforming HTML fragments
1545 ## which don't belong to other sections
1547 # format line of commit message.
1548 sub format_log_line_html {
1551 $line = esc_html($line, -nbsp=>1);
1552 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1553 $cgi->a({-href => href(action=>"object", hash=>$1),
1554 -class => "text"}, $1);
1560 # format marker of refs pointing to given object
1562 # the destination action is chosen based on object type and current context:
1563 # - for annotated tags, we choose the tag view unless it's the current view
1564 # already, in which case we go to shortlog view
1565 # - for other refs, we keep the current view if we're in history, shortlog or
1566 # log view, and select shortlog otherwise
1567 sub format_ref_marker {
1568 my ($refs, $id) = @_;
1571 if (defined $refs->{$id}) {
1572 foreach my $ref (@{$refs->{$id}}) {
1573 # this code exploits the fact that non-lightweight tags are the
1574 # only indirect objects, and that they are the only objects for which
1575 # we want to use tag instead of shortlog as action
1576 my ($type, $name) = qw();
1577 my $indirect = ($ref =~ s/\^\{\}$//);
1578 # e.g. tags/v2.6.11 or heads/next
1579 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1588 $class .= " indirect" if $indirect;
1590 my $dest_action = "shortlog";
1593 $dest_action = "tag" unless $action eq "tag";
1594 } elsif ($action =~ /^(history|(short)?log)$/) {
1595 $dest_action = $action;
1599 $dest .= "refs/" unless $ref =~ m!^refs/!;
1602 my $link = $cgi->a({
1604 action=>$dest_action,
1608 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1614 return ' <span class="refs">'. $markers . '</span>';
1620 # format, perhaps shortened and with markers, title line
1621 sub format_subject_html {
1622 my ($long, $short, $href, $extra) = @_;
1623 $extra = '' unless defined($extra);
1625 if (length($short) < length($long)) {
1626 $long =~ s/[[:cntrl:]]/?/g;
1627 return $cgi->a({-href => $href, -class => "list subject",
1628 -title => to_utf8($long)},
1629 esc_html($short)) . $extra;
1631 return $cgi->a({-href => $href, -class => "list subject"},
1632 esc_html($long)) . $extra;
1636 # Rather than recomputing the url for an email multiple times, we cache it
1637 # after the first hit. This gives a visible benefit in views where the avatar
1638 # for the same email is used repeatedly (e.g. shortlog).
1639 # The cache is shared by all avatar engines (currently gravatar only), which
1640 # are free to use it as preferred. Since only one avatar engine is used for any
1641 # given page, there's no risk for cache conflicts.
1642 our %avatar_cache = ();
1644 # Compute the picon url for a given email, by using the picon search service over at
1645 # http://www.cs.indiana.edu/picons/search.html
1647 my $email = lc shift;
1648 if (!$avatar_cache{$email}) {
1649 my ($user, $domain) = split('@', $email);
1650 $avatar_cache{$email} =
1651 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1653 "users+domains+unknown/up/single";
1655 return $avatar_cache{$email};
1658 # Compute the gravatar url for a given email, if it's not in the cache already.
1659 # Gravatar stores only the part of the URL before the size, since that's the
1660 # one computationally more expensive. This also allows reuse of the cache for
1661 # different sizes (for this particular engine).
1663 my $email = lc shift;
1665 $avatar_cache{$email} ||=
1666 "http://www.gravatar.com/avatar/" .
1667 Digest::MD5::md5_hex($email) . "?s=";
1668 return $avatar_cache{$email} . $size;
1671 # Insert an avatar for the given $email at the given $size if the feature
1673 sub git_get_avatar {
1674 my ($email, %opts) = @_;
1675 my $pre_white = ($opts{-pad_before} ? " " : "");
1676 my $post_white = ($opts{-pad_after} ? " " : "");
1677 $opts{-size} ||= 'default';
1678 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1680 if ($git_avatar eq 'gravatar') {
1681 $url = gravatar_url($email, $size);
1682 } elsif ($git_avatar eq 'picon') {
1683 $url = picon_url($email);
1685 # Other providers can be added by extending the if chain, defining $url
1686 # as needed. If no variant puts something in $url, we assume avatars
1687 # are completely disabled/unavailable.
1690 "<img width=\"$size\" " .
1691 "class=\"avatar\" " .
1700 sub format_search_author {
1701 my ($author, $searchtype, $displaytext) = @_;
1702 my $have_search = gitweb_check_feature('search');
1706 if ($searchtype eq 'author') {
1707 $performed = "authored";
1708 } elsif ($searchtype eq 'committer') {
1709 $performed = "committed";
1712 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1713 searchtext=>$author,
1714 searchtype=>$searchtype), class=>"list",
1715 title=>"Search for commits $performed by $author"},
1719 return $displaytext;
1723 # format the author name of the given commit with the given tag
1724 # the author name is chopped and escaped according to the other
1725 # optional parameters (see chop_str).
1726 sub format_author_html {
1729 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1730 return "<$tag class=\"author\">" .
1731 format_search_author($co->{'author_name'}, "author",
1732 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1737 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1738 sub format_git_diff_header_line {
1740 my $diffinfo = shift;
1741 my ($from, $to) = @_;
1743 if ($diffinfo->{'nparents'}) {
1745 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1746 if ($to->{'href'}) {
1747 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1748 esc_path($to->{'file'}));
1749 } else { # file was deleted (no href)
1750 $line .= esc_path($to->{'file'});
1754 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1755 if ($from->{'href'}) {
1756 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1757 'a/' . esc_path($from->{'file'}));
1758 } else { # file was added (no href)
1759 $line .= 'a/' . esc_path($from->{'file'});
1762 if ($to->{'href'}) {
1763 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1764 'b/' . esc_path($to->{'file'}));
1765 } else { # file was deleted
1766 $line .= 'b/' . esc_path($to->{'file'});
1770 return "<div class=\"diff header\">$line</div>\n";
1773 # format extended diff header line, before patch itself
1774 sub format_extended_diff_header_line {
1776 my $diffinfo = shift;
1777 my ($from, $to) = @_;
1780 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1781 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1782 esc_path($from->{'file'}));
1784 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1785 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1786 esc_path($to->{'file'}));
1788 # match single <mode>
1789 if ($line =~ m/\s(\d{6})$/) {
1790 $line .= '<span class="info"> (' .
1791 file_type_long($1) .
1795 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1796 # can match only for combined diff
1798 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1799 if ($from->{'href'}[$i]) {
1800 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1802 substr($diffinfo->{'from_id'}[$i],0,7));
1807 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1810 if ($to->{'href'}) {
1811 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1812 substr($diffinfo->{'to_id'},0,7));
1817 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1818 # can match only for ordinary diff
1819 my ($from_link, $to_link);
1820 if ($from->{'href'}) {
1821 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1822 substr($diffinfo->{'from_id'},0,7));
1824 $from_link = '0' x 7;
1826 if ($to->{'href'}) {
1827 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1828 substr($diffinfo->{'to_id'},0,7));
1832 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1833 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1836 return $line . "<br/>\n";
1839 # format from-file/to-file diff header
1840 sub format_diff_from_to_header {
1841 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1846 #assert($line =~ m/^---/) if DEBUG;
1847 # no extra formatting for "^--- /dev/null"
1848 if (! $diffinfo->{'nparents'}) {
1849 # ordinary (single parent) diff
1850 if ($line =~ m!^--- "?a/!) {
1851 if ($from->{'href'}) {
1853 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1854 esc_path($from->{'file'}));
1857 esc_path($from->{'file'});
1860 $result .= qq!<div class="diff from_file">$line</div>\n!;
1863 # combined diff (merge commit)
1864 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1865 if ($from->{'href'}[$i]) {
1867 $cgi->a({-href=>href(action=>"blobdiff",
1868 hash_parent=>$diffinfo->{'from_id'}[$i],
1869 hash_parent_base=>$parents[$i],
1870 file_parent=>$from->{'file'}[$i],
1871 hash=>$diffinfo->{'to_id'},
1873 file_name=>$to->{'file'}),
1875 -title=>"diff" . ($i+1)},
1878 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1879 esc_path($from->{'file'}[$i]));
1881 $line = '--- /dev/null';
1883 $result .= qq!<div class="diff from_file">$line</div>\n!;
1888 #assert($line =~ m/^\+\+\+/) if DEBUG;
1889 # no extra formatting for "^+++ /dev/null"
1890 if ($line =~ m!^\+\+\+ "?b/!) {
1891 if ($to->{'href'}) {
1893 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1894 esc_path($to->{'file'}));
1897 esc_path($to->{'file'});
1900 $result .= qq!<div class="diff to_file">$line</div>\n!;
1905 # create note for patch simplified by combined diff
1906 sub format_diff_cc_simplified {
1907 my ($diffinfo, @parents) = @_;
1910 $result .= "<div class=\"diff header\">" .
1912 if (!is_deleted($diffinfo)) {
1913 $result .= $cgi->a({-href => href(action=>"blob",
1915 hash=>$diffinfo->{'to_id'},
1916 file_name=>$diffinfo->{'to_file'}),
1918 esc_path($diffinfo->{'to_file'}));
1920 $result .= esc_path($diffinfo->{'to_file'});
1922 $result .= "</div>\n" . # class="diff header"
1923 "<div class=\"diff nodifferences\">" .
1925 "</div>\n"; # class="diff nodifferences"
1930 # format patch (diff) line (not to be used for diff headers)
1931 sub format_diff_line {
1933 my ($from, $to) = @_;
1934 my $diff_class = "";
1938 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1940 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1941 if ($line =~ m/^\@{3}/) {
1942 $diff_class = " chunk_header";
1943 } elsif ($line =~ m/^\\/) {
1944 $diff_class = " incomplete";
1945 } elsif ($prefix =~ tr/+/+/) {
1946 $diff_class = " add";
1947 } elsif ($prefix =~ tr/-/-/) {
1948 $diff_class = " rem";
1951 # assume ordinary diff
1952 my $char = substr($line, 0, 1);
1954 $diff_class = " add";
1955 } elsif ($char eq '-') {
1956 $diff_class = " rem";
1957 } elsif ($char eq '@') {
1958 $diff_class = " chunk_header";
1959 } elsif ($char eq "\\") {
1960 $diff_class = " incomplete";
1963 $line = untabify($line);
1964 if ($from && $to && $line =~ m/^\@{2} /) {
1965 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1966 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1968 $from_lines = 0 unless defined $from_lines;
1969 $to_lines = 0 unless defined $to_lines;
1971 if ($from->{'href'}) {
1972 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1973 -class=>"list"}, $from_text);
1975 if ($to->{'href'}) {
1976 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1977 -class=>"list"}, $to_text);
1979 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1980 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1981 return "<div class=\"diff$diff_class\">$line</div>\n";
1982 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1983 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1984 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1986 @from_text = split(' ', $ranges);
1987 for (my $i = 0; $i < @from_text; ++$i) {
1988 ($from_start[$i], $from_nlines[$i]) =
1989 (split(',', substr($from_text[$i], 1)), 0);
1992 $to_text = pop @from_text;
1993 $to_start = pop @from_start;
1994 $to_nlines = pop @from_nlines;
1996 $line = "<span class=\"chunk_info\">$prefix ";
1997 for (my $i = 0; $i < @from_text; ++$i) {
1998 if ($from->{'href'}[$i]) {
1999 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2000 -class=>"list"}, $from_text[$i]);
2002 $line .= $from_text[$i];
2006 if ($to->{'href'}) {
2007 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2008 -class=>"list"}, $to_text);
2012 $line .= " $prefix</span>" .
2013 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2014 return "<div class=\"diff$diff_class\">$line</div>\n";
2016 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
2019 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2020 # linked. Pass the hash of the tree/commit to snapshot.
2021 sub format_snapshot_links {
2023 my $num_fmts = @snapshot_fmts;
2024 if ($num_fmts > 1) {
2025 # A parenthesized list of links bearing format names.
2026 # e.g. "snapshot (_tar.gz_ _zip_)"
2027 return "snapshot (" . join(' ', map
2034 }, $known_snapshot_formats{$_}{'display'})
2035 , @snapshot_fmts) . ")";
2036 } elsif ($num_fmts == 1) {
2037 # A single "snapshot" link whose tooltip bears the format name.
2039 my ($fmt) = @snapshot_fmts;
2045 snapshot_format=>$fmt
2047 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2049 } else { # $num_fmts == 0
2054 ## ......................................................................
2055 ## functions returning values to be passed, perhaps after some
2056 ## transformation, to other functions; e.g. returning arguments to href()
2058 # returns hash to be passed to href to generate gitweb URL
2059 # in -title key it returns description of link
2061 my $format = shift || 'Atom';
2062 my %res = (action => lc($format));
2064 # feed links are possible only for project views
2065 return unless (defined $project);
2066 # some views should link to OPML, or to generic project feed,
2067 # or don't have specific feed yet (so they should use generic)
2068 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2071 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2072 # from tag links; this also makes possible to detect branch links
2073 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2074 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2077 # find log type for feed description (title)
2079 if (defined $file_name) {
2080 $type = "history of $file_name";
2081 $type .= "/" if ($action eq 'tree');
2082 $type .= " on '$branch'" if (defined $branch);
2084 $type = "log of $branch" if (defined $branch);
2087 $res{-title} = $type;
2088 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2089 $res{'file_name'} = $file_name;
2094 ## ----------------------------------------------------------------------
2095 ## git utility subroutines, invoking git commands
2097 # returns path to the core git executable and the --git-dir parameter as list
2099 $number_of_git_cmds++;
2100 return $GIT, '--git-dir='.$git_dir;
2103 # quote the given arguments for passing them to the shell
2104 # quote_command("command", "arg 1", "arg with ' and ! characters")
2105 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2106 # Try to avoid using this function wherever possible.
2109 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2112 # get HEAD ref of given project as hash
2113 sub git_get_head_hash {
2114 return git_get_full_hash(shift, 'HEAD');
2117 sub git_get_full_hash {
2118 return git_get_hash(@_);
2121 sub git_get_short_hash {
2122 return git_get_hash(@_, '--short=7');
2126 my ($project, $hash, @options) = @_;
2127 my $o_git_dir = $git_dir;
2129 $git_dir = "$projectroot/$project";
2130 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2131 '--verify', '-q', @options, $hash) {
2133 chomp $retval if defined $retval;
2136 if (defined $o_git_dir) {
2137 $git_dir = $o_git_dir;
2142 # get type of given object
2146 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2148 close $fd or return;
2153 # repository configuration
2154 our $config_file = '';
2157 # store multiple values for single key as anonymous array reference
2158 # single values stored directly in the hash, not as [ <value> ]
2159 sub hash_set_multi {
2160 my ($hash, $key, $value) = @_;
2162 if (!exists $hash->{$key}) {
2163 $hash->{$key} = $value;
2164 } elsif (!ref $hash->{$key}) {
2165 $hash->{$key} = [ $hash->{$key}, $value ];
2167 push @{$hash->{$key}}, $value;
2171 # return hash of git project configuration
2172 # optionally limited to some section, e.g. 'gitweb'
2173 sub git_parse_project_config {
2174 my $section_regexp = shift;
2179 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2182 while (my $keyval = <$fh>) {
2184 my ($key, $value) = split(/\n/, $keyval, 2);
2186 hash_set_multi(\%config, $key, $value)
2187 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2194 # convert config value to boolean: 'true' or 'false'
2195 # no value, number > 0, 'true' and 'yes' values are true
2196 # rest of values are treated as false (never as error)
2197 sub config_to_bool {
2200 return 1 if !defined $val; # section.key
2202 # strip leading and trailing whitespace
2206 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2207 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2210 # convert config value to simple decimal number
2211 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2212 # to be multiplied by 1024, 1048576, or 1073741824
2216 # strip leading and trailing whitespace
2220 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2222 # unknown unit is treated as 1
2223 return $num * ($unit eq 'g' ? 1073741824 :
2224 $unit eq 'm' ? 1048576 :
2225 $unit eq 'k' ? 1024 : 1);
2230 # convert config value to array reference, if needed
2231 sub config_to_multi {
2234 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2237 sub git_get_project_config {
2238 my ($key, $type) = @_;
2240 return unless defined $git_dir;
2243 return unless ($key);
2244 $key =~ s/^gitweb\.//;
2245 return if ($key =~ m/\W/);
2248 if (defined $type) {
2251 unless ($type eq 'bool' || $type eq 'int');
2255 if (!defined $config_file ||
2256 $config_file ne "$git_dir/config") {
2257 %config = git_parse_project_config('gitweb');
2258 $config_file = "$git_dir/config";
2261 # check if config variable (key) exists
2262 return unless exists $config{"gitweb.$key"};
2265 if (!defined $type) {
2266 return $config{"gitweb.$key"};
2267 } elsif ($type eq 'bool') {
2268 # backward compatibility: 'git config --bool' returns true/false
2269 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2270 } elsif ($type eq 'int') {
2271 return config_to_int($config{"gitweb.$key"});
2273 return $config{"gitweb.$key"};
2276 # get hash of given path at given ref
2277 sub git_get_hash_by_path {
2279 my $path = shift || return undef;
2284 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2285 or die_error(500, "Open git-ls-tree failed");
2287 close $fd or return undef;
2289 if (!defined $line) {
2290 # there is no tree or hash given by $path at $base
2294 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2295 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2296 if (defined $type && $type ne $2) {
2297 # type doesn't match
2303 # get path of entry with given hash at given tree-ish (ref)
2304 # used to get 'from' filename for combined diff (merge commit) for renames
2305 sub git_get_path_by_hash {
2306 my $base = shift || return;
2307 my $hash = shift || return;
2311 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2313 while (my $line = <$fd>) {
2316 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2317 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2318 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2327 ## ......................................................................
2328 ## git utility functions, directly accessing git repository
2330 sub git_get_project_description {
2333 $git_dir = "$projectroot/$path";
2334 open my $fd, '<', "$git_dir/description"
2335 or return git_get_project_config('description');
2338 if (defined $descr) {
2344 sub git_get_project_ctags {
2348 $git_dir = "$projectroot/$path";
2349 opendir my $dh, "$git_dir/ctags"
2351 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2352 open my $ct, '<', $_ or next;
2356 my $ctag = $_; $ctag =~ s#.*/##;
2357 $ctags->{$ctag} = $val;
2363 sub git_populate_project_tagcloud {
2366 # First, merge different-cased tags; tags vote on casing
2368 foreach (keys %$ctags) {
2369 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2370 if (not $ctags_lc{lc $_}->{topcount}
2371 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2372 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2373 $ctags_lc{lc $_}->{topname} = $_;
2378 if (eval { require HTML::TagCloud; 1; }) {
2379 $cloud = HTML::TagCloud->new;
2380 foreach (sort keys %ctags_lc) {
2381 # Pad the title with spaces so that the cloud looks
2383 my $title = $ctags_lc{$_}->{topname};
2384 $title =~ s/ / /g;
2385 $title =~ s/^/ /g;
2386 $title =~ s/$/ /g;
2387 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2390 $cloud = \%ctags_lc;
2395 sub git_show_project_tagcloud {
2396 my ($cloud, $count) = @_;
2397 print STDERR ref($cloud)."..\n";
2398 if (ref $cloud eq 'HTML::TagCloud') {
2399 return $cloud->html_and_css($count);
2401 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2402 return '<p align="center">' . join (', ', map {
2403 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2404 } splice(@tags, 0, $count)) . '</p>';
2408 sub git_get_project_url_list {
2411 $git_dir = "$projectroot/$path";
2412 open my $fd, '<', "$git_dir/cloneurl"
2413 or return wantarray ?
2414 @{ config_to_multi(git_get_project_config('url')) } :
2415 config_to_multi(git_get_project_config('url'));
2416 my @git_project_url_list = map { chomp; $_ } <$fd>;
2419 return wantarray ? @git_project_url_list : \@git_project_url_list;
2422 sub git_get_projects_list {
2427 $filter =~ s/\.git$//;
2429 my $check_forks = gitweb_check_feature('forks');
2431 if (-d $projects_list) {
2432 # search in directory
2433 my $dir = $projects_list . ($filter ? "/$filter" : '');
2434 # remove the trailing "/"
2436 my $pfxlen = length("$dir");
2437 my $pfxdepth = ($dir =~ tr!/!!);
2440 follow_fast => 1, # follow symbolic links
2441 follow_skip => 2, # ignore duplicates
2442 dangling_symlinks => 0, # ignore dangling symlinks, silently
2444 # skip project-list toplevel, if we get it.
2445 return if (m!^[/.]$!);
2446 # only directories can be git repositories
2447 return unless (-d $_);
2448 # don't traverse too deep (Find is super slow on os x)
2449 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2450 $File::Find::prune = 1;
2454 my $subdir = substr($File::Find::name, $pfxlen + 1);
2455 # we check related file in $projectroot
2456 my $path = ($filter ? "$filter/" : '') . $subdir;
2457 if (check_export_ok("$projectroot/$path")) {
2458 push @list, { path => $path };
2459 $File::Find::prune = 1;
2464 } elsif (-f $projects_list) {
2465 # read from file(url-encoded):
2466 # 'git%2Fgit.git Linus+Torvalds'
2467 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2468 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2470 open my $fd, '<', $projects_list or return;
2472 while (my $line = <$fd>) {
2474 my ($path, $owner) = split ' ', $line;
2475 $path = unescape($path);
2476 $owner = unescape($owner);
2477 if (!defined $path) {
2480 if ($filter ne '') {
2481 # looking for forks;
2482 my $pfx = substr($path, 0, length($filter));
2483 if ($pfx ne $filter) {
2486 my $sfx = substr($path, length($filter));
2487 if ($sfx !~ /^\/.*\.git$/) {
2490 } elsif ($check_forks) {
2492 foreach my $filter (keys %paths) {
2493 # looking for forks;
2494 my $pfx = substr($path, 0, length($filter));
2495 if ($pfx ne $filter) {
2498 my $sfx = substr($path, length($filter));
2499 if ($sfx !~ /^\/.*\.git$/) {
2502 # is a fork, don't include it in
2507 if (check_export_ok("$projectroot/$path")) {
2510 owner => to_utf8($owner),
2513 (my $forks_path = $path) =~ s/\.git$//;
2514 $paths{$forks_path}++;
2522 our $gitweb_project_owner = undef;
2523 sub git_get_project_list_from_file {
2525 return if (defined $gitweb_project_owner);
2527 $gitweb_project_owner = {};
2528 # read from file (url-encoded):
2529 # 'git%2Fgit.git Linus+Torvalds'
2530 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2531 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2532 if (-f $projects_list) {
2533 open(my $fd, '<', $projects_list);
2534 while (my $line = <$fd>) {
2536 my ($pr, $ow) = split ' ', $line;
2537 $pr = unescape($pr);
2538 $ow = unescape($ow);
2539 $gitweb_project_owner->{$pr} = to_utf8($ow);
2545 sub git_get_project_owner {
2546 my $project = shift;
2549 return undef unless $project;
2550 $git_dir = "$projectroot/$project";
2552 if (!defined $gitweb_project_owner) {
2553 git_get_project_list_from_file();
2556 if (exists $gitweb_project_owner->{$project}) {
2557 $owner = $gitweb_project_owner->{$project};
2559 if (!defined $owner){
2560 $owner = git_get_project_config('owner');
2562 if (!defined $owner) {
2563 $owner = get_file_owner("$git_dir");
2569 sub git_get_last_activity {
2573 $git_dir = "$projectroot/$path";
2574 open($fd, "-|", git_cmd(), 'for-each-ref',
2575 '--format=%(committer)',
2576 '--sort=-committerdate',
2578 'refs/heads') or return;
2579 my $most_recent = <$fd>;
2580 close $fd or return;
2581 if (defined $most_recent &&
2582 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2584 my $age = time - $timestamp;
2585 return ($age, age_string($age));
2587 return (undef, undef);
2590 sub git_get_references {
2591 my $type = shift || "";
2593 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2594 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2595 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2596 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2599 while (my $line = <$fd>) {
2601 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2602 if (defined $refs{$1}) {
2603 push @{$refs{$1}}, $2;
2609 close $fd or return;
2613 sub git_get_rev_name_tags {
2614 my $hash = shift || return undef;
2616 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2618 my $name_rev = <$fd>;
2621 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2624 # catches also '$hash undefined' output
2629 ## ----------------------------------------------------------------------
2630 ## parse to hash functions
2634 my $tz = shift || "-0000";
2637 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2638 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2639 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2640 $date{'hour'} = $hour;
2641 $date{'minute'} = $min;
2642 $date{'mday'} = $mday;
2643 $date{'day'} = $days[$wday];
2644 $date{'month'} = $months[$mon];
2645 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2646 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2647 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2648 $mday, $months[$mon], $hour ,$min;
2649 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2650 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2652 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2653 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2654 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2655 $date{'hour_local'} = $hour;
2656 $date{'minute_local'} = $min;
2657 $date{'tz_local'} = $tz;
2658 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2659 1900+$year, $mon+1, $mday,
2660 $hour, $min, $sec, $tz);
2669 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2670 $tag{'id'} = $tag_id;
2671 while (my $line = <$fd>) {
2673 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2674 $tag{'object'} = $1;
2675 } elsif ($line =~ m/^type (.+)$/) {
2677 } elsif ($line =~ m/^tag (.+)$/) {
2679 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2680 $tag{'author'} = $1;
2681 $tag{'author_epoch'} = $2;
2682 $tag{'author_tz'} = $3;
2683 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2684 $tag{'author_name'} = $1;
2685 $tag{'author_email'} = $2;
2687 $tag{'author_name'} = $tag{'author'};
2689 } elsif ($line =~ m/--BEGIN/) {
2690 push @comment, $line;
2692 } elsif ($line eq "") {
2696 push @comment, <$fd>;
2697 $tag{'comment'} = \@comment;
2698 close $fd or return;
2699 if (!defined $tag{'name'}) {
2705 sub parse_commit_text {
2706 my ($commit_text, $withparents) = @_;
2707 my @commit_lines = split '\n', $commit_text;
2710 pop @commit_lines; # Remove '\0'
2712 if (! @commit_lines) {
2716 my $header = shift @commit_lines;
2717 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2720 ($co{'id'}, my @parents) = split ' ', $header;
2721 while (my $line = shift @commit_lines) {
2722 last if $line eq "\n";
2723 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2725 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2727 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2728 $co{'author'} = to_utf8($1);
2729 $co{'author_epoch'} = $2;
2730 $co{'author_tz'} = $3;
2731 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2732 $co{'author_name'} = $1;
2733 $co{'author_email'} = $2;
2735 $co{'author_name'} = $co{'author'};
2737 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2738 $co{'committer'} = to_utf8($1);
2739 $co{'committer_epoch'} = $2;
2740 $co{'committer_tz'} = $3;
2741 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2742 $co{'committer_name'} = $1;
2743 $co{'committer_email'} = $2;
2745 $co{'committer_name'} = $co{'committer'};
2749 if (!defined $co{'tree'}) {
2752 $co{'parents'} = \@parents;
2753 $co{'parent'} = $parents[0];
2755 foreach my $title (@commit_lines) {
2758 $co{'title'} = chop_str($title, 80, 5);
2759 # remove leading stuff of merges to make the interesting part visible
2760 if (length($title) > 50) {
2761 $title =~ s/^Automatic //;
2762 $title =~ s/^merge (of|with) /Merge ... /i;
2763 if (length($title) > 50) {
2764 $title =~ s/(http|rsync):\/\///;
2766 if (length($title) > 50) {
2767 $title =~ s/(master|www|rsync)\.//;
2769 if (length($title) > 50) {
2770 $title =~ s/kernel.org:?//;
2772 if (length($title) > 50) {
2773 $title =~ s/\/pub\/scm//;
2776 $co{'title_short'} = chop_str($title, 50, 5);
2780 if (! defined $co{'title'} || $co{'title'} eq "") {
2781 $co{'title'} = $co{'title_short'} = '(no commit message)';
2783 # remove added spaces
2784 foreach my $line (@commit_lines) {
2787 $co{'comment'} = \@commit_lines;
2789 my $age = time - $co{'committer_epoch'};
2791 $co{'age_string'} = age_string($age);
2792 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2793 if ($age > 60*60*24*7*2) {
2794 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2795 $co{'age_string_age'} = $co{'age_string'};
2797 $co{'age_string_date'} = $co{'age_string'};
2798 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2804 my ($commit_id) = @_;
2809 open my $fd, "-|", git_cmd(), "rev-list",
2815 or die_error(500, "Open git-rev-list failed");
2816 %co = parse_commit_text(<$fd>, 1);
2823 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2831 open my $fd, "-|", git_cmd(), "rev-list",
2834 ("--max-count=" . $maxcount),
2835 ("--skip=" . $skip),
2839 ($filename ? ($filename) : ())
2840 or die_error(500, "Open git-rev-list failed");
2841 while (my $line = <$fd>) {
2842 my %co = parse_commit_text($line);
2847 return wantarray ? @cos : \@cos;
2850 # parse line of git-diff-tree "raw" output
2851 sub parse_difftree_raw_line {
2855 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2856 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2857 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2858 $res{'from_mode'} = $1;
2859 $res{'to_mode'} = $2;
2860 $res{'from_id'} = $3;
2862 $res{'status'} = $5;
2863 $res{'similarity'} = $6;
2864 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2865 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2867 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2870 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2871 # combined diff (for merge commit)
2872 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2873 $res{'nparents'} = length($1);
2874 $res{'from_mode'} = [ split(' ', $2) ];
2875 $res{'to_mode'} = pop @{$res{'from_mode'}};
2876 $res{'from_id'} = [ split(' ', $3) ];
2877 $res{'to_id'} = pop @{$res{'from_id'}};
2878 $res{'status'} = [ split('', $4) ];
2879 $res{'to_file'} = unquote($5);
2881 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2882 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2883 $res{'commit'} = $1;
2886 return wantarray ? %res : \%res;
2889 # wrapper: return parsed line of git-diff-tree "raw" output
2890 # (the argument might be raw line, or parsed info)
2891 sub parsed_difftree_line {
2892 my $line_or_ref = shift;
2894 if (ref($line_or_ref) eq "HASH") {
2895 # pre-parsed (or generated by hand)
2896 return $line_or_ref;
2898 return parse_difftree_raw_line($line_or_ref);
2902 # parse line of git-ls-tree output
2903 sub parse_ls_tree_line {
2909 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
2910 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
2919 $res{'name'} = unquote($5);
2922 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2923 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2931 $res{'name'} = unquote($4);
2935 return wantarray ? %res : \%res;
2938 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2939 sub parse_from_to_diffinfo {
2940 my ($diffinfo, $from, $to, @parents) = @_;
2942 if ($diffinfo->{'nparents'}) {
2944 $from->{'file'} = [];
2945 $from->{'href'} = [];
2946 fill_from_file_info($diffinfo, @parents)
2947 unless exists $diffinfo->{'from_file'};
2948 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2949 $from->{'file'}[$i] =
2950 defined $diffinfo->{'from_file'}[$i] ?
2951 $diffinfo->{'from_file'}[$i] :
2952 $diffinfo->{'to_file'};
2953 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2954 $from->{'href'}[$i] = href(action=>"blob",
2955 hash_base=>$parents[$i],
2956 hash=>$diffinfo->{'from_id'}[$i],
2957 file_name=>$from->{'file'}[$i]);
2959 $from->{'href'}[$i] = undef;
2963 # ordinary (not combined) diff
2964 $from->{'file'} = $diffinfo->{'from_file'};
2965 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2966 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2967 hash=>$diffinfo->{'from_id'},
2968 file_name=>$from->{'file'});
2970 delete $from->{'href'};
2974 $to->{'file'} = $diffinfo->{'to_file'};
2975 if (!is_deleted($diffinfo)) { # file exists in result
2976 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2977 hash=>$diffinfo->{'to_id'},
2978 file_name=>$to->{'file'});
2980 delete $to->{'href'};
2984 ## ......................................................................
2985 ## parse to array of hashes functions
2987 sub git_get_heads_list {
2991 open my $fd, '-|', git_cmd(), 'for-each-ref',
2992 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2993 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2996 while (my $line = <$fd>) {
3000 my ($refinfo, $committerinfo) = split(/\0/, $line);
3001 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3002 my ($committer, $epoch, $tz) =
3003 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3004 $ref_item{'fullname'} = $name;
3005 $name =~ s!^refs/heads/!!;
3007 $ref_item{'name'} = $name;
3008 $ref_item{'id'} = $hash;
3009 $ref_item{'title'} = $title || '(no commit message)';
3010 $ref_item{'epoch'} = $epoch;
3012 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3014 $ref_item{'age'} = "unknown";
3017 push @headslist, \%ref_item;
3021 return wantarray ? @headslist : \@headslist;
3024 sub git_get_tags_list {
3028 open my $fd, '-|', git_cmd(), 'for-each-ref',
3029 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3030 '--format=%(objectname) %(objecttype) %(refname) '.
3031 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3034 while (my $line = <$fd>) {
3038 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3039 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3040 my ($creator, $epoch, $tz) =
3041 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3042 $ref_item{'fullname'} = $name;
3043 $name =~ s!^refs/tags/!!;
3045 $ref_item{'type'} = $type;
3046 $ref_item{'id'} = $id;
3047 $ref_item{'name'} = $name;
3048 if ($type eq "tag") {
3049 $ref_item{'subject'} = $title;
3050 $ref_item{'reftype'} = $reftype;
3051 $ref_item{'refid'} = $refid;
3053 $ref_item{'reftype'} = $type;
3054 $ref_item{'refid'} = $id;
3057 if ($type eq "tag" || $type eq "commit") {
3058 $ref_item{'epoch'} = $epoch;
3060 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3062 $ref_item{'age'} = "unknown";
3066 push @tagslist, \%ref_item;
3070 return wantarray ? @tagslist : \@tagslist;
3073 ## ----------------------------------------------------------------------
3074 ## filesystem-related functions
3076 sub get_file_owner {
3079 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3080 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3081 if (!defined $gcos) {
3085 $owner =~ s/[,;].*$//;
3086 return to_utf8($owner);
3089 # assume that file exists
3091 my $filename = shift;
3093 open my $fd, '<', $filename;
3094 print map { to_utf8($_) } <$fd>;
3098 ## ......................................................................
3099 ## mimetype related functions
3101 sub mimetype_guess_file {
3102 my $filename = shift;
3103 my $mimemap = shift;
3104 -r $mimemap or return undef;
3107 open(my $mh, '<', $mimemap) or return undef;
3109 next if m/^#/; # skip comments
3110 my ($mimetype, $exts) = split(/\t+/);
3111 if (defined $exts) {
3112 my @exts = split(/\s+/, $exts);
3113 foreach my $ext (@exts) {
3114 $mimemap{$ext} = $mimetype;
3120 $filename =~ /\.([^.]*)$/;
3121 return $mimemap{$1};
3124 sub mimetype_guess {
3125 my $filename = shift;
3127 $filename =~ /\./ or return undef;
3129 if ($mimetypes_file) {
3130 my $file = $mimetypes_file;
3131 if ($file !~ m!^/!) { # if it is relative path
3132 # it is relative to project
3133 $file = "$projectroot/$project/$file";
3135 $mime = mimetype_guess_file($filename, $file);
3137 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3143 my $filename = shift;
3146 my $mime = mimetype_guess($filename);
3147 $mime and return $mime;
3151 return $default_blob_plain_mimetype unless $fd;
3154 return 'text/plain';
3155 } elsif (! $filename) {
3156 return 'application/octet-stream';
3157 } elsif ($filename =~ m/\.png$/i) {
3159 } elsif ($filename =~ m/\.gif$/i) {
3161 } elsif ($filename =~ m/\.jpe?g$/i) {
3162 return 'image/jpeg';
3164 return 'application/octet-stream';
3168 sub blob_contenttype {
3169 my ($fd, $file_name, $type) = @_;
3171 $type ||= blob_mimetype($fd, $file_name);
3172 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3173 $type .= "; charset=$default_text_plain_charset";
3179 ## ======================================================================
3180 ## functions printing HTML: header, footer, error page
3182 sub get_page_title {
3183 my $title = to_utf8($site_name);
3185 return $title unless (defined $project);
3186 $title .= " - " . to_utf8($project);
3188 return $title unless (defined $action);
3189 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3191 return $title unless (defined $file_name);
3192 $title .= " - " . esc_path($file_name);
3193 if ($action eq "tree" && $file_name !~ m|/$|) {
3200 sub git_header_html {
3201 my $status = shift || "200 OK";
3202 my $expires = shift;
3205 my $title = get_page_title();
3207 # require explicit support from the UA if we are to send the page as
3208 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3209 # we have to do this because MSIE sometimes globs '*/*', pretending to
3210 # support xhtml+xml but choking when it gets what it asked for.
3211 if (defined $cgi->http('HTTP_ACCEPT') &&
3212 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3213 $cgi->Accept('application/xhtml+xml') != 0) {
3214 $content_type = 'application/xhtml+xml';
3216 $content_type = 'text/html';
3218 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3219 -status=> $status, -expires => $expires)
3220 unless ($opts{'-no_http_headers'});
3221 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3223 <?xml version="1.0" encoding="utf-8"?>
3224 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3225 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3226 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3227 <!-- git core binaries version $git_version -->
3229 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3230 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3231 <meta name="robots" content="index, nofollow"/>
3232 <title>$title</title>
3234 # the stylesheet, favicon etc urls won't work correctly with path_info
3235 # unless we set the appropriate base URL
3236 if ($ENV{'PATH_INFO'}) {
3237 print "<base href=\"".esc_url($base_url)."\" />\n";
3239 # print out each stylesheet that exist, providing backwards capability
3240 # for those people who defined $stylesheet in a config file
3241 if (defined $stylesheet) {
3242 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3244 foreach my $stylesheet (@stylesheets) {
3245 next unless $stylesheet;
3246 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3249 if (defined $project) {
3250 my %href_params = get_feed_info();
3251 if (!exists $href_params{'-title'}) {
3252 $href_params{'-title'} = 'log';
3255 foreach my $format qw(RSS Atom) {
3256 my $type = lc($format);
3258 '-rel' => 'alternate',
3259 '-title' => "$project - $href_params{'-title'} - $format feed",
3260 '-type' => "application/$type+xml"
3263 $href_params{'action'} = $type;
3264 $link_attr{'-href'} = href(%href_params);
3266 "rel=\"$link_attr{'-rel'}\" ".
3267 "title=\"$link_attr{'-title'}\" ".
3268 "href=\"$link_attr{'-href'}\" ".
3269 "type=\"$link_attr{'-type'}\" ".
3272 $href_params{'extra_options'} = '--no-merges';
3273 $link_attr{'-href'} = href(%href_params);
3274 $link_attr{'-title'} .= ' (no merges)';
3276 "rel=\"$link_attr{'-rel'}\" ".
3277 "title=\"$link_attr{'-title'}\" ".
3278 "href=\"$link_attr{'-href'}\" ".
3279 "type=\"$link_attr{'-type'}\" ".
3284 printf('<link rel="alternate" title="%s projects list" '.
3285 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3286 $site_name, href(project=>undef, action=>"project_index"));
3287 printf('<link rel="alternate" title="%s projects feeds" '.
3288 'href="%s" type="text/x-opml" />'."\n",
3289 $site_name, href(project=>undef, action=>"opml"));
3291 if (defined $favicon) {
3292 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
3298 if (defined $site_header && -f $site_header) {
3299 insert_file($site_header);
3302 print "<div class=\"page_header\">\n" .
3303 $cgi->a({-href => esc_url($logo_url),
3304 -title => $logo_label},
3305 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
3306 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3307 if (defined $project) {
3308 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3309 if (defined $action) {
3316 my $have_search = gitweb_check_feature('search');
3317 if (defined $project && $have_search) {
3318 if (!defined $searchtext) {
3322 if (defined $hash_base) {
3323 $search_hash = $hash_base;
3324 } elsif (defined $hash) {
3325 $search_hash = $hash;
3327 $search_hash = "HEAD";
3329 my $action = $my_uri;
3330 my $use_pathinfo = gitweb_check_feature('pathinfo');
3331 if ($use_pathinfo) {
3332 $action .= "/".esc_url($project);
3334 print $cgi->startform(-method => "get", -action => $action) .
3335 "<div class=\"search\">\n" .
3337 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3338 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3339 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3340 $cgi->popup_menu(-name => 'st', -default => 'commit',
3341 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3342 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3344 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3345 "<span title=\"Extended regular expression\">" .
3346 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3347 -checked => $search_use_regexp) .
3350 $cgi->end_form() . "\n";
3354 sub git_footer_html {
3355 my $feed_class = 'rss_logo';
3357 print "<div class=\"page_footer\">\n";
3358 if (defined $project) {
3359 my $descr = git_get_project_description($project);
3360 if (defined $descr) {
3361 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3364 my %href_params = get_feed_info();
3365 if (!%href_params) {
3366 $feed_class .= ' generic';
3368 $href_params{'-title'} ||= 'log';
3370 foreach my $format qw(RSS Atom) {
3371 $href_params{'action'} = lc($format);
3372 print $cgi->a({-href => href(%href_params),
3373 -title => "$href_params{'-title'} $format feed",
3374 -class => $feed_class}, $format)."\n";
3378 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3379 -class => $feed_class}, "OPML") . " ";
3380 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3381 -class => $feed_class}, "TXT") . "\n";
3383 print "</div>\n"; # class="page_footer"
3385 if (defined $t0 && gitweb_check_feature('timed')) {
3386 print "<div id=\"generating_info\">\n";
3387 print 'This page took '.
3388 '<span id="generating_time" class="time_span">'.
3389 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
3392 '<span id="generating_cmd">'.
3393 $number_of_git_cmds.
3394 '</span> git commands '.
3396 print "</div>\n"; # class="page_footer"
3399 if (defined $site_footer && -f $site_footer) {
3400 insert_file($site_footer);
3403 print qq!<script type="text/javascript" src="$javascript"></script>\n!;
3404 if (defined $action &&
3405 $action eq 'blame_incremental') {
3406 print qq!<script type="text/javascript">\n!.
3407 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3408 qq! "!. href() .qq!");\n!.
3410 } elsif (gitweb_check_feature('javascript-actions')) {
3411 print qq!<script type="text/javascript">\n!.
3412 qq!window.onload = fixLinks;\n!.
3420 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3421 # Example: die_error(404, 'Hash not found')
3422 # By convention, use the following status codes (as defined in RFC 2616):
3423 # 400: Invalid or missing CGI parameters, or
3424 # requested object exists but has wrong type.
3425 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3426 # this server or project.
3427 # 404: Requested object/revision/project doesn't exist.
3428 # 500: The server isn't configured properly, or
3429 # an internal error occurred (e.g. failed assertions caused by bugs), or
3430 # an unknown error occurred (e.g. the git binary died unexpectedly).
3431 # 503: The server is currently unavailable (because it is overloaded,
3432 # or down for maintenance). Generally, this is a temporary state.
3434 my $status = shift || 500;
3435 my $error = esc_html(shift) || "Internal Server Error";
3439 my %http_responses = (
3440 400 => '400 Bad Request',
3441 403 => '403 Forbidden',
3442 404 => '404 Not Found',
3443 500 => '500 Internal Server Error',
3444 503 => '503 Service Unavailable',
3446 git_header_html($http_responses{$status}, undef, %opts);
3448 <div class="page_body">
3453 if (defined $extra) {
3461 unless ($opts{'-error_handler'});
3464 ## ----------------------------------------------------------------------
3465 ## functions printing or outputting HTML: navigation
3467 sub git_print_page_nav {
3468 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3469 $extra = '' if !defined $extra; # pager or formats
3471 my @navs = qw(summary shortlog log commit commitdiff tree);
3473 @navs = grep { $_ ne $suppress } @navs;
3476 my %arg = map { $_ => {action=>$_} } @navs;
3477 if (defined $head) {
3478 for (qw(commit commitdiff)) {
3479 $arg{$_}{'hash'} = $head;
3481 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3482 for (qw(shortlog log)) {
3483 $arg{$_}{'hash'} = $head;
3488 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3489 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3491 my @actions = gitweb_get_feature('actions');
3494 'n' => $project, # project name
3495 'f' => $git_dir, # project path within filesystem
3496 'h' => $treehead || '', # current hash ('h' parameter)
3497 'b' => $treebase || '', # hash base ('hb' parameter)
3500 my ($label, $link, $pos) = splice(@actions,0,3);
3502 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3504 $link =~ s/%([%nfhb])/$repl{$1}/g;
3505 $arg{$label}{'_href'} = $link;
3508 print "<div class=\"page_nav\">\n" .
3510 map { $_ eq $current ?
3511 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3513 print "<br/>\n$extra<br/>\n" .
3517 sub format_paging_nav {
3518 my ($action, $page, $has_next_link) = @_;
3524 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
3526 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3527 -accesskey => "p", -title => "Alt-p"}, "prev");
3529 $paging_nav .= "first ⋅ prev";
3532 if ($has_next_link) {
3533 $paging_nav .= " ⋅ " .
3534 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3535 -accesskey => "n", -title => "Alt-n"}, "next");
3537 $paging_nav .= " ⋅ next";
3543 ## ......................................................................
3544 ## functions printing or outputting HTML: div
3546 sub git_print_header_div {
3547 my ($action, $title, $hash, $hash_base) = @_;
3550 $args{'action'} = $action;
3551 $args{'hash'} = $hash if $hash;
3552 $args{'hash_base'} = $hash_base if $hash_base;
3554 print "<div class=\"header\">\n" .
3555 $cgi->a({-href => href(%args), -class => "title"},
3556 $title ? $title : $action) .
3560 sub print_local_time {
3561 print format_local_time(@_);
3564 sub format_local_time {
3567 if ($date{'hour_local'} < 6) {
3568 $localtime .= sprintf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3569 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3571 $localtime .= sprintf(" (%02d:%02d %s)",
3572 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3578 # Outputs the author name and date in long form
3579 sub git_print_authorship {
3582 my $tag = $opts{-tag} || 'div';
3583 my $author = $co->{'author_name'};
3585 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3586 print "<$tag class=\"author_date\">" .
3587 format_search_author($author, "author", esc_html($author)) .
3589 print_local_time(%ad) if ($opts{-localtime});
3590 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3594 # Outputs table rows containing the full author or committer information,
3595 # in the format expected for 'commit' view (& similia).
3596 # Parameters are a commit hash reference, followed by the list of people
3597 # to output information for. If the list is empty it defalts to both
3598 # author and committer.
3599 sub git_print_authorship_rows {
3601 # too bad we can't use @people = @_ || ('author', 'committer')
3603 @people = ('author', 'committer') unless @people;
3604 foreach my $who (@people) {
3605 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3606 print "<tr><td>$who</td><td>" .
3607 format_search_author($co->{"${who}_name"}, $who,
3608 esc_html($co->{"${who}_name"})) . " " .
3609 format_search_author($co->{"${who}_email"}, $who,
3610 esc_html("<" . $co->{"${who}_email"} . ">")) .
3611 "</td><td rowspan=\"2\">" .
3612 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3615 "<td></td><td> $wd{'rfc2822'}";
3616 print_local_time(%wd);
3622 sub git_print_page_path {
3628 print "<div class=\"page_path\">";
3629 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3630 -title => 'tree root'}, to_utf8("[$project]"));
3632 if (defined $name) {
3633 my @dirname = split '/', $name;
3634 my $basename = pop @dirname;
3637 foreach my $dir (@dirname) {
3638 $fullname .= ($fullname ? '/' : '') . $dir;
3639 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3641 -title => $fullname}, esc_path($dir));
3644 if (defined $type && $type eq 'blob') {
3645 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3647 -title => $name}, esc_path($basename));
3648 } elsif (defined $type && $type eq 'tree') {
3649 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3651 -title => $name}, esc_path($basename));
3654 print esc_path($basename);
3657 print "<br/></div>\n";
3664 if ($opts{'-remove_title'}) {
3665 # remove title, i.e. first line of log
3668 # remove leading empty lines
3669 while (defined $log->[0] && $log->[0] eq "") {
3676 foreach my $line (@$log) {
3677 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3680 if (! $opts{'-remove_signoff'}) {
3681 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3684 # remove signoff lines
3691 # print only one empty line
3692 # do not print empty line after signoff
3694 next if ($empty || $signoff);
3700 print format_log_line_html($line) . "<br/>\n";
3703 if ($opts{'-final_empty_line'}) {
3704 # end with single empty line
3705 print "<br/>\n" unless $empty;
3709 # return link target (what link points to)
3710 sub git_get_link_target {
3715 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3719 $link_target = <$fd>;
3724 return $link_target;
3727 # given link target, and the directory (basedir) the link is in,
3728 # return target of link relative to top directory (top tree);
3729 # return undef if it is not possible (including absolute links).
3730 sub normalize_link_target {
3731 my ($link_target, $basedir) = @_;
3733 # absolute symlinks (beginning with '/') cannot be normalized
3734 return if (substr($link_target, 0, 1) eq '/');
3736 # normalize link target to path from top (root) tree (dir)
3739 $path = $basedir . '/' . $link_target;
3741 # we are in top (root) tree (dir)
3742 $path = $link_target;
3745 # remove //, /./, and /../
3747 foreach my $part (split('/', $path)) {
3748 # discard '.' and ''
3749 next if (!$part || $part eq '.');
3751 if ($part eq '..') {
3755 # link leads outside repository (outside top dir)
3759 push @path_parts, $part;
3762 $path = join('/', @path_parts);
3767 # print tree entry (row of git_tree), but without encompassing <tr> element
3768 sub git_print_tree_entry {
3769 my ($t, $basedir, $hash_base, $have_blame) = @_;
3772 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3774 # The format of a table row is: mode list link. Where mode is
3775 # the mode of the entry, list is the name of the entry, an href,
3776 # and link is the action links of the entry.
3778 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3779 if (exists $t->{'size'}) {
3780 print "<td class=\"size\">$t->{'size'}</td>\n";
3782 if ($t->{'type'} eq "blob") {
3783 print "<td class=\"list\">" .
3784 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3785 file_name=>"$basedir$t->{'name'}", %base_key),
3786 -class => "list"}, esc_path($t->{'name'}));
3787 if (S_ISLNK(oct $t->{'mode'})) {
3788 my $link_target = git_get_link_target($t->{'hash'});
3790 my $norm_target = normalize_link_target($link_target, $basedir);
3791 if (defined $norm_target) {
3793 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3794 file_name=>$norm_target),
3795 -title => $norm_target}, esc_path($link_target));
3797 print " -> " . esc_path($link_target);
3802 print "<td class=\"link\">";
3803 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3804 file_name=>"$basedir$t->{'name'}", %base_key)},
3808 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3809 file_name=>"$basedir$t->{'name'}", %base_key)},
3812 if (defined $hash_base) {
3814 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3815 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3819 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3820 file_name=>"$basedir$t->{'name'}")},
3824 } elsif ($t->{'type'} eq "tree") {
3825 print "<td class=\"list\">";
3826 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3827 file_name=>"$basedir$t->{'name'}",
3829 esc_path($t->{'name'}));
3831 print "<td class=\"link\">";
3832 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3833 file_name=>"$basedir$t->{'name'}",
3836 if (defined $hash_base) {
3838 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3839 file_name=>"$basedir$t->{'name'}")},
3844 # unknown object: we can only present history for it
3845 # (this includes 'commit' object, i.e. submodule support)
3846 print "<td class=\"list\">" .
3847 esc_path($t->{'name'}) .
3849 print "<td class=\"link\">";
3850 if (defined $hash_base) {
3851 print $cgi->a({-href => href(action=>"history",
3852 hash_base=>$hash_base,
3853 file_name=>"$basedir$t->{'name'}")},
3860 ## ......................................................................
3861 ## functions printing large fragments of HTML
3863 # get pre-image filenames for merge (combined) diff
3864 sub fill_from_file_info {
3865 my ($diff, @parents) = @_;
3867 $diff->{'from_file'} = [ ];
3868 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3869 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3870 if ($diff->{'status'}[$i] eq 'R' ||
3871 $diff->{'status'}[$i] eq 'C') {
3872 $diff->{'from_file'}[$i] =
3873 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3880 # is current raw difftree line of file deletion
3882 my $diffinfo = shift;
3884 return $diffinfo->{'to_id'} eq ('0' x 40);
3887 # does patch correspond to [previous] difftree raw line
3888 # $diffinfo - hashref of parsed raw diff format
3889 # $patchinfo - hashref of parsed patch diff format
3890 # (the same keys as in $diffinfo)
3891 sub is_patch_split {
3892 my ($diffinfo, $patchinfo) = @_;
3894 return defined $diffinfo && defined $patchinfo
3895 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3899 sub git_difftree_body {
3900 my ($difftree, $hash, @parents) = @_;
3901 my ($parent) = $parents[0];
3902 my $have_blame = gitweb_check_feature('blame');
3903 print "<div class=\"list_head\">\n";
3904 if ($#{$difftree} > 10) {
3905 print(($#{$difftree} + 1) . " files changed:\n");
3909 print "<table class=\"" .
3910 (@parents > 1 ? "combined " : "") .
3913 # header only for combined diff in 'commitdiff' view
3914 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3917 print "<thead><tr>\n" .
3918 "<th></th><th></th>\n"; # filename, patchN link
3919 for (my $i = 0; $i < @parents; $i++) {
3920 my $par = $parents[$i];
3922 $cgi->a({-href => href(action=>"commitdiff",
3923 hash=>$hash, hash_parent=>$par),
3924 -title => 'commitdiff to parent number ' .
3925 ($i+1) . ': ' . substr($par,0,7)},
3929 print "</tr></thead>\n<tbody>\n";
3934 foreach my $line (@{$difftree}) {
3935 my $diff = parsed_difftree_line($line);
3938 print "<tr class=\"dark\">\n";
3940 print "<tr class=\"light\">\n";
3944 if (exists $diff->{'nparents'}) { # combined diff
3946 fill_from_file_info($diff, @parents)
3947 unless exists $diff->{'from_file'};
3949 if (!is_deleted($diff)) {
3950 # file exists in the result (child) commit
3952 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3953 file_name=>$diff->{'to_file'},
3955 -class => "list"}, esc_path($diff->{'to_file'})) .
3959 esc_path($diff->{'to_file'}) .
3963 if ($action eq 'commitdiff') {
3966 print "<td class=\"link\">" .
3967 $cgi->a({-href => "#patch$patchno"}, "patch") .
3972 my $has_history = 0;
3973 my $not_deleted = 0;
3974 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3975 my $hash_parent = $parents[$i];
3976 my $from_hash = $diff->{'from_id'}[$i];
3977 my $from_path = $diff->{'from_file'}[$i];
3978 my $status = $diff->{'status'}[$i];
3980 $has_history ||= ($status ne 'A');
3981 $not_deleted ||= ($status ne 'D');
3983 if ($status eq 'A') {
3984 print "<td class=\"link\" align=\"right\"> | </td>\n";
3985 } elsif ($status eq 'D') {
3986 print "<td class=\"link\">" .
3987 $cgi->a({-href => href(action=>"blob",
3990 file_name=>$from_path)},
3994 if ($diff->{'to_id'} eq $from_hash) {
3995 print "<td class=\"link nochange\">";
3997 print "<td class=\"link\">";
3999 print $cgi->a({-href => href(action=>"blobdiff",
4000 hash=>$diff->{'to_id'},
4001 hash_parent=>$from_hash,
4003 hash_parent_base=>$hash_parent,
4004 file_name=>$diff->{'to_file'},
4005 file_parent=>$from_path)},
4011 print "<td class=\"link\">";
4013 print $cgi->a({-href => href(action=>"blob",
4014 hash=>$diff->{'to_id'},
4015 file_name=>$diff->{'to_file'},
4018 print " | " if ($has_history);
4021 print $cgi->a({-href => href(action=>"history",
4022 file_name=>$diff->{'to_file'},
4029 next; # instead of 'else' clause, to avoid extra indent
4031 # else ordinary diff
4033 my ($to_mode_oct, $to_mode_str, $to_file_type);
4034 my ($from_mode_oct, $from_mode_str, $from_file_type);
4035 if ($diff->{'to_mode'} ne ('0' x 6)) {
4036 $to_mode_oct = oct $diff->{'to_mode'};
4037 if (S_ISREG($to_mode_oct)) { # only for regular file
4038 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4040 $to_file_type = file_type($diff->{'to_mode'});
4042 if ($diff->{'from_mode'} ne ('0' x 6)) {
4043 $from_mode_oct = oct $diff->{'from_mode'};
4044 if (S_ISREG($to_mode_oct)) { # only for regular file
4045 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4047 $from_file_type = file_type($diff->{'from_mode'});
4050 if ($diff->{'status'} eq "A") { # created
4051 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4052 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4053 $mode_chng .= "]</span>";
4055 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4056 hash_base=>$hash, file_name=>$diff->{'file'}),
4057 -class => "list"}, esc_path($diff->{'file'}));
4059 print "<td>$mode_chng</td>\n";
4060 print "<td class=\"link\">";
4061 if ($action eq 'commitdiff') {
4064 print $cgi->a({-href => "#patch$patchno"}, "patch");
4067 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4068 hash_base=>$hash, file_name=>$diff->{'file'})},
4072 } elsif ($diff->{'status'} eq "D") { # deleted
4073 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4075 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4076 hash_base=>$parent, file_name=>$diff->{'file'}),
4077 -class => "list"}, esc_path($diff->{'file'}));
4079 print "<td>$mode_chng</td>\n";
4080 print "<td class=\"link\">";
4081 if ($action eq 'commitdiff') {
4084 print $cgi->a({-href => "#patch$patchno"}, "patch");
4087 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4088 hash_base=>$parent, file_name=>$diff->{'file'})},
4091 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4092 file_name=>$diff->{'file'})},
4095 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4096 file_name=>$diff->{'file'})},
4100 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4101 my $mode_chnge = "";
4102 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4103 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4104 if ($from_file_type ne $to_file_type) {
4105 $mode_chnge .= " from $from_file_type to $to_file_type";
4107 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4108 if ($from_mode_str && $to_mode_str) {
4109 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4110 } elsif ($to_mode_str) {
4111 $mode_chnge .= " mode: $to_mode_str";
4114 $mode_chnge .= "]</span>\n";
4117 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4118 hash_base=>$hash, file_name=>$diff->{'file'}),
4119 -class => "list"}, esc_path($diff->{'file'}));
4121 print "<td>$mode_chnge</td>\n";
4122 print "<td class=\"link\">";
4123 if ($action eq 'commitdiff') {
4126 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4128 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4129 # "commit" view and modified file (not onlu mode changed)
4130 print $cgi->a({-href => href(action=>"blobdiff",
4131 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4132 hash_base=>$hash, hash_parent_base=>$parent,
4133 file_name=>$diff->{'file'})},
4137 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4138 hash_base=>$hash, file_name=>$diff->{'file'})},
4141 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4142 file_name=>$diff->{'file'})},
4145 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4146 file_name=>$diff->{'file'})},
4150 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4151 my %status_name = ('R' => 'moved', 'C' => 'copied');
4152 my $nstatus = $status_name{$diff->{'status'}};
4154 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4155 # mode also for directories, so we cannot use $to_mode_str
4156 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4159 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4160 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4161 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4162 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4163 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4164 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4165 -class => "list"}, esc_path($diff->{'from_file'})) .
4166 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4167 "<td class=\"link\">";
4168 if ($action eq 'commitdiff') {
4171 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4173 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4174 # "commit" view and modified file (not only pure rename or copy)
4175 print $cgi->a({-href => href(action=>"blobdiff",
4176 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4177 hash_base=>$hash, hash_parent_base=>$parent,
4178 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4182 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4183 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4186 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4187 file_name=>$diff->{'to_file'})},
4190 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4191 file_name=>$diff->{'to_file'})},
4195 } # we should not encounter Unmerged (U) or Unknown (X) status
4198 print "</tbody>" if $has_header;
4202 sub git_patchset_body {
4203 my ($fd, $difftree, $hash, @hash_parents) = @_;
4204 my ($hash_parent) = $hash_parents[0];
4206 my $is_combined = (@hash_parents > 1);
4208 my $patch_number = 0;
4214 print "<div class=\"patchset\">\n";
4216 # skip to first patch
4217 while ($patch_line = <$fd>) {
4220 last if ($patch_line =~ m/^diff /);
4224 while ($patch_line) {
4226 # parse "git diff" header line
4227 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4228 # $1 is from_name, which we do not use
4229 $to_name = unquote($2);
4230 $to_name =~ s!^b/!!;
4231 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4232 # $1 is 'cc' or 'combined', which we do not use
4233 $to_name = unquote($2);
4238 # check if current patch belong to current raw line
4239 # and parse raw git-diff line if needed
4240 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4241 # this is continuation of a split patch
4242 print "<div class=\"patch cont\">\n";
4244 # advance raw git-diff output if needed
4245 $patch_idx++ if defined $diffinfo;
4247 # read and prepare patch information
4248 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4250 # compact combined diff output can have some patches skipped
4251 # find which patch (using pathname of result) we are at now;
4253 while ($to_name ne $diffinfo->{'to_file'}) {
4254 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4255 format_diff_cc_simplified($diffinfo, @hash_parents) .
4256 "</div>\n"; # class="patch"
4261 last if $patch_idx > $#$difftree;
4262 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4266 # modifies %from, %to hashes
4267 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4269 # this is first patch for raw difftree line with $patch_idx index
4270 # we index @$difftree array from 0, but number patches from 1
4271 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4275 #assert($patch_line =~ m/^diff /) if DEBUG;
4276 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4278 # print "git diff" header
4279 print format_git_diff_header_line($patch_line, $diffinfo,
4282 # print extended diff header
4283 print "<div class=\"diff extended_header\">\n";
4285 while ($patch_line = <$fd>) {
4288 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4290 print format_extended_diff_header_line($patch_line, $diffinfo,
4293 print "</div>\n"; # class="diff extended_header"
4295 # from-file/to-file diff header
4296 if (! $patch_line) {
4297 print "</div>\n"; # class="patch"
4300 next PATCH if ($patch_line =~ m/^diff /);
4301 #assert($patch_line =~ m/^---/) if DEBUG;
4303 my $last_patch_line = $patch_line;
4304 $patch_line = <$fd>;
4306 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4308 print format_diff_from_to_header($last_patch_line, $patch_line,
4309 $diffinfo, \%from, \%to,
4314 while ($patch_line = <$fd>) {
4317 next PATCH if ($patch_line =~ m/^diff /);
4319 print format_diff_line($patch_line, \%from, \%to);
4323 print "</div>\n"; # class="patch"
4326 # for compact combined (--cc) format, with chunk and patch simpliciaction
4327 # patchset might be empty, but there might be unprocessed raw lines
4328 for (++$patch_idx if $patch_number > 0;
4329 $patch_idx < @$difftree;
4331 # read and prepare patch information
4332 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4334 # generate anchor for "patch" links in difftree / whatchanged part
4335 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4336 format_diff_cc_simplified($diffinfo, @hash_parents) .
4337 "</div>\n"; # class="patch"
4342 if ($patch_number == 0) {
4343 if (@hash_parents > 1) {
4344 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4346 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4350 print "</div>\n"; # class="patchset"
4353 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4355 # fills project list info (age, description, owner, forks) for each
4356 # project in the list, removing invalid projects from returned list
4357 # NOTE: modifies $projlist, but does not remove entries from it
4358 sub fill_project_list_info {
4359 my ($projlist, $check_forks) = @_;
4362 my $show_ctags = gitweb_check_feature('ctags');
4364 foreach my $pr (@$projlist) {
4365 my (@activity) = git_get_last_activity($pr->{'path'});
4366 unless (@activity) {
4369 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4370 if (!defined $pr->{'descr'}) {
4371 my $descr = git_get_project_description($pr->{'path'}) || "";
4372 $descr = to_utf8($descr);
4373 $pr->{'descr_long'} = $descr;
4374 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4376 if (!defined $pr->{'owner'}) {
4377 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4380 my $pname = $pr->{'path'};
4381 if (($pname =~ s/\.git$//) &&
4382 ($pname !~ /\/$/) &&
4383 (-d "$projectroot/$pname")) {
4384 $pr->{'forks'} = "-d $projectroot/$pname";
4389 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4390 push @projects, $pr;
4396 # print 'sort by' <th> element, generating 'sort by $name' replay link
4397 # if that order is not selected
4399 print format_sort_th(@_);
4402 sub format_sort_th {
4403 my ($name, $order, $header) = @_;
4405 $header ||= ucfirst($name);
4407 if ($order eq $name) {
4408 $sort_th .= "<th>$header</th>\n";
4410 $sort_th .= "<th>" .
4411 $cgi->a({-href => href(-replay=>1, order=>$name),
4412 -class => "header"}, $header) .
4419 sub git_project_list_body {
4420 # actually uses global variable $project
4421 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4423 my $check_forks = gitweb_check_feature('forks');
4424 my @projects = fill_project_list_info($projlist, $check_forks);
4426 $order ||= $default_projects_order;
4427 $from = 0 unless defined $from;
4428 $to = $#projects if (!defined $to || $#projects < $to);
4431 project => { key => 'path', type => 'str' },
4432 descr => { key => 'descr_long', type => 'str' },
4433 owner => { key => 'owner', type => 'str' },
4434 age => { key => 'age', type => 'num' }
4436 my $oi = $order_info{$order};
4437 if ($oi->{'type'} eq 'str') {
4438 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4440 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4443 my $show_ctags = gitweb_check_feature('ctags');
4446 foreach my $p (@projects) {
4447 foreach my $ct (keys %{$p->{'ctags'}}) {
4448 $ctags{$ct} += $p->{'ctags'}->{$ct};
4451 my $cloud = git_populate_project_tagcloud(\%ctags);
4452 print git_show_project_tagcloud($cloud, 64);
4455 print "<table class=\"project_list\">\n";
4456 unless ($no_header) {
4459 print "<th></th>\n";
4461 print_sort_th('project', $order, 'Project');
4462 print_sort_th('descr', $order, 'Description');
4463 print_sort_th('owner', $order, 'Owner');
4464 print_sort_th('age', $order, 'Last Change');
4465 print "<th></th>\n" . # for links
4469 my $tagfilter = $cgi->param('by_tag');
4470 for (my $i = $from; $i <= $to; $i++) {
4471 my $pr = $projects[$i];
4473 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4474 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4475 and not $pr->{'descr_long'} =~ /$searchtext/;
4476 # Weed out forks or non-matching entries of search
4478 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4479 $forkbase="^$forkbase" if $forkbase;
4480 next if not $searchtext and not $tagfilter and $show_ctags
4481 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4485 print "<tr class=\"dark\">\n";
4487 print "<tr class=\"light\">\n";
4492 if ($pr->{'forks'}) {
4493 print "<!-- $pr->{'forks'} -->\n";
4494 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4498 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4499 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4500 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4501 -class => "list", -title => $pr->{'descr_long'}},
4502 esc_html($pr->{'descr'})) . "</td>\n" .
4503 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4504 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4505 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4506 "<td class=\"link\">" .
4507 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4508 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
4509 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4510 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4511 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4515 if (defined $extra) {
4518 print "<td></td>\n";
4520 print "<td colspan=\"5\">$extra</td>\n" .
4527 # uses global variable $project
4528 my ($commitlist, $from, $to, $refs, $extra) = @_;
4530 $from = 0 unless defined $from;
4531 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4533 for (my $i = 0; $i <= $to; $i++) {
4534 my %co = %{$commitlist->[$i]};
4536 my $commit = $co{'id'};
4537 my $ref = format_ref_marker($refs, $commit);
4538 my %ad = parse_date($co{'author_epoch'});
4539 git_print_header_div('commit',
4540 "<span class=\"age\">$co{'age_string'}</span>" .
4541 esc_html($co{'title'}) . $ref,
4543 print "<div class=\"title_text\">\n" .
4544 "<div class=\"log_link\">\n" .
4545 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4547 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4549 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4552 git_print_authorship(\%co, -tag => 'span');
4553 print "<br/>\n</div>\n";
4555 print "<div class=\"log_body\">\n";
4556 git_print_log($co{'comment'}, -final_empty_line=> 1);
4560 print "<div class=\"page_nav\">\n";
4566 sub git_shortlog_body {
4567 # uses global variable $project
4568 my ($commitlist, $from, $to, $refs, $extra) = @_;
4570 $from = 0 unless defined $from;
4571 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4573 print "<table class=\"shortlog\">\n";
4575 for (my $i = $from; $i <= $to; $i++) {
4576 my %co = %{$commitlist->[$i]};
4577 my $commit = $co{'id'};
4578 my $ref = format_ref_marker($refs, $commit);
4580 print "<tr class=\"dark\">\n";
4582 print "<tr class=\"light\">\n";
4585 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4586 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4587 format_author_html('td', \%co, 10) . "<td>";
4588 print format_subject_html($co{'title'}, $co{'title_short'},
4589 href(action=>"commit", hash=>$commit), $ref);
4591 "<td class=\"link\">" .
4592 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4593 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4594 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4595 my $snapshot_links = format_snapshot_links($commit);
4596 if (defined $snapshot_links) {
4597 print " | " . $snapshot_links;
4602 if (defined $extra) {
4604 "<td colspan=\"4\">$extra</td>\n" .
4610 sub git_history_body {
4611 # Warning: assumes constant type (blob or tree) during history
4612 my ($commitlist, $from, $to, $refs, $extra,
4613 $file_name, $file_hash, $ftype) = @_;
4615 $from = 0 unless defined $from;
4616 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4618 print "<table class=\"history\">\n";
4620 for (my $i = $from; $i <= $to; $i++) {
4621 my %co = %{$commitlist->[$i]};
4625 my $commit = $co{'id'};
4627 my $ref = format_ref_marker($refs, $commit);
4630 print "<tr class=\"dark\">\n";
4632 print "<tr class=\"light\">\n";
4635 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4636 # shortlog: format_author_html('td', \%co, 10)
4637 format_author_html('td', \%co, 15, 3) . "<td>";
4638 # originally git_history used chop_str($co{'title'}, 50)
4639 print format_subject_html($co{'title'}, $co{'title_short'},
4640 href(action=>"commit", hash=>$commit), $ref);
4642 "<td class=\"link\">" .
4643 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4644 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4646 if ($ftype eq 'blob') {
4647 my $blob_current = $file_hash;
4648 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4649 if (defined $blob_current && defined $blob_parent &&
4650 $blob_current ne $blob_parent) {
4652 $cgi->a({-href => href(action=>"blobdiff",
4653 hash=>$blob_current, hash_parent=>$blob_parent,
4654 hash_base=>$hash_base, hash_parent_base=>$commit,
4655 file_name=>$file_name)},
4662 if (defined $extra) {
4664 "<td colspan=\"4\">$extra</td>\n" .
4671 # uses global variable $project
4672 my ($taglist, $from, $to, $extra) = @_;
4673 $from = 0 unless defined $from;
4674 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4676 print "<table class=\"tags\">\n";
4678 for (my $i = $from; $i <= $to; $i++) {
4679 my $entry = $taglist->[$i];
4681 my $comment = $tag{'subject'};
4683 if (defined $comment) {
4684 $comment_short = chop_str($comment, 30, 5);
4687 print "<tr class=\"dark\">\n";
4689 print "<tr class=\"light\">\n";
4692 if (defined $tag{'age'}) {
4693 print "<td><i>$tag{'age'}</i></td>\n";
4695 print "<td></td>\n";
4698 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4699 -class => "list name"}, esc_html($tag{'name'})) .
4702 if (defined $comment) {
4703 print format_subject_html($comment, $comment_short,
4704 href(action=>"tag", hash=>$tag{'id'}));
4707 "<td class=\"selflink\">";
4708 if ($tag{'type'} eq "tag") {
4709 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4714 "<td class=\"link\">" . " | " .
4715 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4716 if ($tag{'reftype'} eq "commit") {
4717 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4718 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
4719 } elsif ($tag{'reftype'} eq "blob") {
4720 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4725 if (defined $extra) {
4727 "<td colspan=\"5\">$extra</td>\n" .
4733 sub git_heads_body {
4734 # uses global variable $project
4735 my ($headlist, $head, $from, $to, $extra) = @_;
4736 $from = 0 unless defined $from;
4737 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4739 print "<table class=\"heads\">\n";
4741 for (my $i = $from; $i <= $to; $i++) {
4742 my $entry = $headlist->[$i];
4744 my $curr = $ref{'id'} eq $head;
4746 print "<tr class=\"dark\">\n";
4748 print "<tr class=\"light\">\n";
4751 print "<td><i>$ref{'age'}</i></td>\n" .
4752 ($curr ? "<td class=\"current_head\">" : "<td>") .
4753 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4754 -class => "list name"},esc_html($ref{'name'})) .
4756 "<td class=\"link\">" .
4757 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4758 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4759 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4763 if (defined $extra) {
4765 "<td colspan=\"3\">$extra</td>\n" .
4771 sub git_search_grep_body {
4772 my ($commitlist, $from, $to, $extra) = @_;
4773 $from = 0 unless defined $from;
4774 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4776 print "<table class=\"commit_search\">\n";
4778 for (my $i = $from; $i <= $to; $i++) {
4779 my %co = %{$commitlist->[$i]};
4783 my $commit = $co{'id'};
4785 print "<tr class=\"dark\">\n";
4787 print "<tr class=\"light\">\n";
4790 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4791 format_author_html('td', \%co, 15, 5) .
4793 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4794 -class => "list subject"},
4795 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4796 my $comment = $co{'comment'};
4797 foreach my $line (@$comment) {
4798 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4799 my ($lead, $match, $trail) = ($1, $2, $3);
4800 $match = chop_str($match, 70, 5, 'center');
4801 my $contextlen = int((80 - length($match))/2);
4802 $contextlen = 30 if ($contextlen > 30);
4803 $lead = chop_str($lead, $contextlen, 10, 'left');
4804 $trail = chop_str($trail, $contextlen, 10, 'right');
4806 $lead = esc_html($lead);
4807 $match = esc_html($match);
4808 $trail = esc_html($trail);
4810 print "$lead<span class=\"match\">$match</span>$trail<br />";
4814 "<td class=\"link\">" .
4815 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4817 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4819 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4823 if (defined $extra) {
4825 "<td colspan=\"3\">$extra</td>\n" .
4831 ## ======================================================================
4832 ## ======================================================================
4835 sub git_project_list {
4836 my $order = $input_params{'order'};
4837 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4838 die_error(400, "Unknown order parameter");
4841 my @list = git_get_projects_list();
4843 die_error(404, "No projects found");
4847 if (defined $home_text && -f $home_text) {
4848 print "<div class=\"index_include\">\n";
4849 insert_file($home_text);
4852 print $cgi->startform(-method => "get") .
4853 "<p class=\"projsearch\">Search:\n" .
4854 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4856 $cgi->end_form() . "\n";
4857 git_project_list_body(\@list, $order);
4862 my $order = $input_params{'order'};
4863 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4864 die_error(400, "Unknown order parameter");
4867 my @list = git_get_projects_list($project);
4869 die_error(404, "No forks found");
4873 git_print_page_nav('','');
4874 git_print_header_div('summary', "$project forks");
4875 git_project_list_body(\@list, $order);
4879 sub git_project_index {
4880 my @projects = git_get_projects_list($project);
4883 -type => 'text/plain',
4884 -charset => 'utf-8',
4885 -content_disposition => 'inline; filename="index.aux"');
4887 foreach my $pr (@projects) {
4888 if (!exists $pr->{'owner'}) {
4889 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4892 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4893 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4894 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4895 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4899 print "$path $owner\n";
4904 my $descr = git_get_project_description($project) || "none";
4905 my %co = parse_commit("HEAD");
4906 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4907 my $head = $co{'id'};
4909 my $owner = git_get_project_owner($project);
4911 my $refs = git_get_references();
4912 # These get_*_list functions return one more to allow us to see if
4913 # there are more ...
4914 my @taglist = git_get_tags_list(16);
4915 my @headlist = git_get_heads_list(16);
4917 my $check_forks = gitweb_check_feature('forks');
4920 @forklist = git_get_projects_list($project);
4924 git_print_page_nav('summary','', $head);
4926 print "<div class=\"title\"> </div>\n";
4927 print "<table class=\"projects_list\">\n" .
4928 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4929 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4930 if (defined $cd{'rfc2822'}) {
4931 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4934 # use per project git URL list in $projectroot/$project/cloneurl
4935 # or make project git URL from git base URL and project name
4936 my $url_tag = "URL";
4937 my @url_list = git_get_project_url_list($project);
4938 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4939 foreach my $git_url (@url_list) {
4940 next unless $git_url;
4941 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4946 my $show_ctags = gitweb_check_feature('ctags');
4948 my $ctags = git_get_project_ctags($project);
4949 my $cloud = git_populate_project_tagcloud($ctags);
4950 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4951 print "</td>\n<td>" unless %$ctags;
4952 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4953 print "</td>\n<td>" if %$ctags;
4954 print git_show_project_tagcloud($cloud, 48);
4960 # If XSS prevention is on, we don't include README.html.
4961 # TODO: Allow a readme in some safe format.
4962 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
4963 print "<div class=\"title\">readme</div>\n" .
4964 "<div class=\"readme\">\n";
4965 insert_file("$projectroot/$project/README.html");
4966 print "\n</div>\n"; # class="readme"
4969 # we need to request one more than 16 (0..15) to check if
4971 my @commitlist = $head ? parse_commits($head, 17) : ();
4973 git_print_header_div('shortlog');
4974 git_shortlog_body(\@commitlist, 0, 15, $refs,
4975 $#commitlist <= 15 ? undef :
4976 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4980 git_print_header_div('tags');
4981 git_tags_body(\@taglist, 0, 15,
4982 $#taglist <= 15 ? undef :
4983 $cgi->a({-href => href(action=>"tags")}, "..."));
4987 git_print_header_div('heads');
4988 git_heads_body(\@headlist, $head, 0, 15,
4989 $#headlist <= 15 ? undef :
4990 $cgi->a({-href => href(action=>"heads")}, "..."));
4994 git_print_header_div('forks');
4995 git_project_list_body(\@forklist, 'age', 0, 15,
4996 $#forklist <= 15 ? undef :
4997 $cgi->a({-href => href(action=>"forks")}, "..."),
5005 my $head = git_get_head_hash($project);
5007 git_print_page_nav('','', $head,undef,$head);
5008 my %tag = parse_tag($hash);
5011 die_error(404, "Unknown tag object");
5014 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
5015 print "<div class=\"title_text\">\n" .
5016 "<table class=\"object_header\">\n" .
5018 "<td>object</td>\n" .
5019 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5020 $tag{'object'}) . "</td>\n" .
5021 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5022 $tag{'type'}) . "</td>\n" .
5024 if (defined($tag{'author'})) {
5025 git_print_authorship_rows(\%tag, 'author');
5027 print "</table>\n\n" .
5029 print "<div class=\"page_body\">";
5030 my $comment = $tag{'comment'};
5031 foreach my $line (@$comment) {
5033 print esc_html($line, -nbsp=>1) . "<br/>\n";
5039 sub git_blame_common {
5040 my $format = shift || 'porcelain';
5041 if ($format eq 'porcelain' && $cgi->param('js')) {
5042 $format = 'incremental';
5043 $action = 'blame_incremental'; # for page title etc
5047 gitweb_check_feature('blame')
5048 or die_error(403, "Blame view not allowed");
5051 die_error(400, "No file name given") unless $file_name;
5052 $hash_base ||= git_get_head_hash($project);
5053 die_error(404, "Couldn't find base commit") unless $hash_base;
5054 my %co = parse_commit($hash_base)
5055 or die_error(404, "Commit not found");
5057 if (!defined $hash) {
5058 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5059 or die_error(404, "Error looking up file");
5061 $ftype = git_get_type($hash);
5062 if ($ftype !~ "blob") {
5063 die_error(400, "Object is not a blob");
5068 if ($format eq 'incremental') {
5069 # get file contents (as base)
5070 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5071 or die_error(500, "Open git-cat-file failed");
5072 } elsif ($format eq 'data') {
5073 # run git-blame --incremental
5074 open $fd, "-|", git_cmd(), "blame", "--incremental",
5075 $hash_base, "--", $file_name
5076 or die_error(500, "Open git-blame --incremental failed");
5078 # run git-blame --porcelain
5079 open $fd, "-|", git_cmd(), "blame", '-p',
5080 $hash_base, '--', $file_name
5081 or die_error(500, "Open git-blame --porcelain failed");
5084 # incremental blame data returns early
5085 if ($format eq 'data') {
5087 -type=>"text/plain", -charset => "utf-8",
5088 -status=> "200 OK");
5089 local $| = 1; # output autoflush
5092 or print "ERROR $!\n";
5095 if (defined $t0 && gitweb_check_feature('timed')) {
5097 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
5098 ' '.$number_of_git_cmds;
5108 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5111 if ($format eq 'incremental') {
5113 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
5114 "blame") . " (non-incremental)";
5117 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
5118 "blame") . " (incremental)";
5122 $cgi->a({-href => href(action=>"history", -replay=>1)},
5125 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
5127 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5128 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5129 git_print_page_path($file_name, $ftype, $hash_base);
5132 if ($format eq 'incremental') {
5133 print "<noscript>\n<div class=\"error\"><center><b>\n".
5134 "This page requires JavaScript to run.\n Use ".
5135 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
5138 "</b></center></div>\n</noscript>\n";
5140 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
5143 print qq!<div class="page_body">\n!;
5144 print qq!<div id="progress_info">... / ...</div>\n!
5145 if ($format eq 'incremental');
5146 print qq!<table id="blame_table" class="blame" width="100%">\n!.
5147 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5149 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
5153 my @rev_color = qw(light dark);
5154 my $num_colors = scalar(@rev_color);
5155 my $current_color = 0;
5157 if ($format eq 'incremental') {
5158 my $color_class = $rev_color[$current_color];
5163 while (my $line = <$fd>) {
5167 print qq!<tr id="l$linenr" class="$color_class">!.
5168 qq!<td class="sha1"><a href=""> </a></td>!.
5169 qq!<td class="linenr">!.
5170 qq!<a class="linenr" href="">$linenr</a></td>!;
5171 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
5175 } else { # porcelain, i.e. ordinary blame
5176 my %metainfo = (); # saves information about commits
5180 while (my $line = <$fd>) {
5182 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5183 # no <lines in group> for subsequent lines in group of lines
5184 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5185 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5186 if (!exists $metainfo{$full_rev}) {
5187 $metainfo{$full_rev} = { 'nprevious' => 0 };
5189 my $meta = $metainfo{$full_rev};
5191 while ($data = <$fd>) {
5193 last if ($data =~ s/^\t//); # contents of line
5194 if ($data =~ /^(\S+)(?: (.*))?$/) {
5195 $meta->{$1} = $2 unless exists $meta->{$1};
5197 if ($data =~ /^previous /) {
5198 $meta->{'nprevious'}++;
5201 my $short_rev = substr($full_rev, 0, 8);
5202 my $author = $meta->{'author'};
5204 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5205 my $date = $date{'iso-tz'};
5207 $current_color = ($current_color + 1) % $num_colors;
5209 my $tr_class = $rev_color[$current_color];
5210 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5211 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5212 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5213 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5215 print "<td class=\"sha1\"";
5216 print " title=\"". esc_html($author) . ", $date\"";
5217 print " rowspan=\"$group_size\"" if ($group_size > 1);
5219 print $cgi->a({-href => href(action=>"commit",
5221 file_name=>$file_name)},
5222 esc_html($short_rev));
5223 if ($group_size >= 2) {
5224 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5225 if (@author_initials) {
5227 esc_html(join('', @author_initials));
5233 # 'previous' <sha1 of parent commit> <filename at commit>
5234 if (exists $meta->{'previous'} &&
5235 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5236 $meta->{'parent'} = $1;
5237 $meta->{'file_parent'} = unquote($2);
5240 exists($meta->{'parent'}) ?
5241 $meta->{'parent'} : $full_rev;
5242 my $linenr_filename =
5243 exists($meta->{'file_parent'}) ?
5244 $meta->{'file_parent'} : unquote($meta->{'filename'});
5245 my $blamed = href(action => 'blame',
5246 file_name => $linenr_filename,
5247 hash_base => $linenr_commit);
5248 print "<td class=\"linenr\">";
5249 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5250 -class => "linenr" },
5253 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5261 "</table>\n"; # class="blame"
5262 print "</div>\n"; # class="blame_body"
5264 or print "Reading blob failed\n";
5273 sub git_blame_incremental {
5274 git_blame_common('incremental');
5277 sub git_blame_data {
5278 git_blame_common('data');
5282 my $head = git_get_head_hash($project);
5284 git_print_page_nav('','', $head,undef,$head);
5285 git_print_header_div('summary', $project);
5287 my @tagslist = git_get_tags_list();
5289 git_tags_body(\@tagslist);
5295 my $head = git_get_head_hash($project);
5297 git_print_page_nav('','', $head,undef,$head);
5298 git_print_header_div('summary', $project);
5300 my @headslist = git_get_heads_list();
5302 git_heads_body(\@headslist, $head);
5307 sub git_blob_plain {
5311 if (!defined $hash) {
5312 if (defined $file_name) {
5313 my $base = $hash_base || git_get_head_hash($project);
5314 $hash = git_get_hash_by_path($base, $file_name, "blob")
5315 or die_error(404, "Cannot find file");
5317 die_error(400, "No file name defined");
5319 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5320 # blobs defined by non-textual hash id's can be cached
5324 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5325 or die_error(500, "Open git-cat-file blob '$hash' failed");
5327 # content-type (can include charset)
5328 $type = blob_contenttype($fd, $file_name, $type);
5330 # "save as" filename, even when no $file_name is given
5331 my $save_as = "$hash";
5332 if (defined $file_name) {
5333 $save_as = $file_name;
5334 } elsif ($type =~ m/^text\//) {
5338 # With XSS prevention on, blobs of all types except a few known safe
5339 # ones are served with "Content-Disposition: attachment" to make sure
5340 # they don't run in our security domain. For certain image types,
5341 # blob view writes an <img> tag referring to blob_plain view, and we
5342 # want to be sure not to break that by serving the image as an
5343 # attachment (though Firefox 3 doesn't seem to care).
5344 my $sandbox = $prevent_xss &&
5345 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5349 -expires => $expires,
5350 -content_disposition =>
5351 ($sandbox ? 'attachment' : 'inline')
5352 . '; filename="' . $save_as . '"');
5354 binmode STDOUT, ':raw';
5356 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5363 if (!defined $hash) {
5364 if (defined $file_name) {
5365 my $base = $hash_base || git_get_head_hash($project);
5366 $hash = git_get_hash_by_path($base, $file_name, "blob")
5367 or die_error(404, "Cannot find file");
5369 die_error(400, "No file name defined");
5371 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5372 # blobs defined by non-textual hash id's can be cached
5376 my $have_blame = gitweb_check_feature('blame');
5377 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5378 or die_error(500, "Couldn't cat $file_name, $hash");
5379 my $mimetype = blob_mimetype($fd, $file_name);
5380 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5382 return git_blob_plain($mimetype);
5384 # we can have blame only for text/* mimetype
5385 $have_blame &&= ($mimetype =~ m!^text/!);
5387 git_header_html(undef, $expires);
5388 my $formats_nav = '';
5389 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5390 if (defined $file_name) {
5393 $cgi->a({-href => href(action=>"blame", -replay=>1)},
5398 $cgi->a({-href => href(action=>"history", -replay=>1)},
5401 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5404 $cgi->a({-href => href(action=>"blob",
5405 hash_base=>"HEAD", file_name=>$file_name)},
5409 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5412 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5413 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5415 print "<div class=\"page_nav\">\n" .
5416 "<br/><br/></div>\n" .
5417 "<div class=\"title\">$hash</div>\n";
5419 git_print_page_path($file_name, "blob", $hash_base);
5420 print "<div class=\"page_body\">\n";
5421 if ($mimetype =~ m!^image/!) {
5422 print qq!<img type="$mimetype"!;
5424 print qq! alt="$file_name" title="$file_name"!;
5427 href(action=>"blob_plain", hash=>$hash,
5428 hash_base=>$hash_base, file_name=>$file_name) .
5432 while (my $line = <$fd>) {
5435 $line = untabify($line);
5436 printf "<div class=\"pre\"><a id=\"l%i\" href=\"" . href(-replay => 1)
5437 . "#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
5438 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
5442 or print "Reading blob failed.\n";
5448 if (!defined $hash_base) {
5449 $hash_base = "HEAD";
5451 if (!defined $hash) {
5452 if (defined $file_name) {
5453 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5458 die_error(404, "No such tree") unless defined($hash);
5460 my $show_sizes = gitweb_check_feature('show-sizes');
5461 my $have_blame = gitweb_check_feature('blame');
5466 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5467 ($show_sizes ? '-l' : ()), @extra_options, $hash
5468 or die_error(500, "Open git-ls-tree failed");
5469 @entries = map { chomp; $_ } <$fd>;
5471 or die_error(404, "Reading tree failed");
5474 my $refs = git_get_references();
5475 my $ref = format_ref_marker($refs, $hash_base);
5478 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5480 if (defined $file_name) {
5482 $cgi->a({-href => href(action=>"history", -replay=>1)},
5484 $cgi->a({-href => href(action=>"tree",
5485 hash_base=>"HEAD", file_name=>$file_name)},
5488 my $snapshot_links = format_snapshot_links($hash);
5489 if (defined $snapshot_links) {
5490 # FIXME: Should be available when we have no hash base as well.
5491 push @views_nav, $snapshot_links;
5493 git_print_page_nav('tree','', $hash_base, undef, undef,
5494 join(' | ', @views_nav));
5495 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5498 print "<div class=\"page_nav\">\n";
5499 print "<br/><br/></div>\n";
5500 print "<div class=\"title\">$hash</div>\n";
5502 if (defined $file_name) {
5503 $basedir = $file_name;
5504 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5507 git_print_page_path($file_name, 'tree', $hash_base);
5509 print "<div class=\"page_body\">\n";
5510 print "<table class=\"tree\">\n";
5512 # '..' (top directory) link if possible
5513 if (defined $hash_base &&
5514 defined $file_name && $file_name =~ m![^/]+$!) {
5516 print "<tr class=\"dark\">\n";
5518 print "<tr class=\"light\">\n";
5522 my $up = $file_name;
5523 $up =~ s!/?[^/]+$!!;
5524 undef $up unless $up;
5525 # based on git_print_tree_entry
5526 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5527 print '<td class="size"> </td>'."\n" if $show_sizes;
5528 print '<td class="list">';
5529 print $cgi->a({-href => href(action=>"tree",
5530 hash_base=>$hash_base,
5534 print "<td class=\"link\"></td>\n";
5538 foreach my $line (@entries) {
5539 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
5542 print "<tr class=\"dark\">\n";
5544 print "<tr class=\"light\">\n";
5548 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
5552 print "</table>\n" .
5558 my ($project, $hash) = @_;
5560 # path/to/project.git -> project
5561 # path/to/project/.git -> project
5562 my $name = to_utf8($project);
5563 $name =~ s,([^/])/*\.git$,$1,;
5564 $name = basename($name);
5566 $name =~ s/[[:cntrl:]]/?/g;
5569 if ($hash =~ /^[0-9a-fA-F]+$/) {
5570 # shorten SHA-1 hash
5571 my $full_hash = git_get_full_hash($project, $hash);
5572 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
5573 $ver = git_get_short_hash($project, $hash);
5575 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
5576 # tags don't need shortened SHA-1 hash
5579 # branches and other need shortened SHA-1 hash
5580 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
5583 $ver .= '-' . git_get_short_hash($project, $hash);
5585 # in case of hierarchical branch names
5588 # name = project-version_string
5589 $name = "$name-$ver";
5591 return wantarray ? ($name, $name) : $name;
5595 my $format = $input_params{'snapshot_format'};
5596 if (!@snapshot_fmts) {
5597 die_error(403, "Snapshots not allowed");
5599 # default to first supported snapshot format
5600 $format ||= $snapshot_fmts[0];
5601 if ($format !~ m/^[a-z0-9]+$/) {
5602 die_error(400, "Invalid snapshot format parameter");
5603 } elsif (!exists($known_snapshot_formats{$format})) {
5604 die_error(400, "Unknown snapshot format");
5605 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
5606 die_error(403, "Snapshot format not allowed");
5607 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5608 die_error(403, "Unsupported snapshot format");
5611 my $type = git_get_type("$hash^{}");
5613 die_error(404, 'Object does not exist');
5614 } elsif ($type eq 'blob') {
5615 die_error(400, 'Object is not a tree-ish');
5618 my ($name, $prefix) = snapshot_name($project, $hash);
5619 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
5620 my $cmd = quote_command(
5621 git_cmd(), 'archive',
5622 "--format=$known_snapshot_formats{$format}{'format'}",
5623 "--prefix=$prefix/", $hash);
5624 if (exists $known_snapshot_formats{$format}{'compressor'}) {
5625 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
5628 $filename =~ s/(["\\])/\\$1/g;
5630 -type => $known_snapshot_formats{$format}{'type'},
5631 -content_disposition => 'inline; filename="' . $filename . '"',
5632 -status => '200 OK');
5634 open my $fd, "-|", $cmd
5635 or die_error(500, "Execute git-archive failed");
5636 binmode STDOUT, ':raw';
5638 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5642 sub git_log_generic {
5643 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
5645 my $head = git_get_head_hash($project);
5646 if (!defined $base) {
5649 if (!defined $page) {
5652 my $refs = git_get_references();
5654 my $commit_hash = $base;
5655 if (defined $parent) {
5656 $commit_hash = "$parent..$base";
5659 parse_commits($commit_hash, 101, (100 * $page),
5660 defined $file_name ? ($file_name, "--full-history") : ());
5663 if (!defined $file_hash && defined $file_name) {
5664 # some commits could have deleted file in question,
5665 # and not have it in tree, but one of them has to have it
5666 for (my $i = 0; $i < @commitlist; $i++) {
5667 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5668 last if defined $file_hash;
5671 if (defined $file_hash) {
5672 $ftype = git_get_type($file_hash);
5674 if (defined $file_name && !defined $ftype) {
5675 die_error(500, "Unknown type of object");
5678 if (defined $file_name) {
5679 %co = parse_commit($base)
5680 or die_error(404, "Unknown commit object");
5684 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
5686 if ($#commitlist >= 100) {
5688 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5689 -accesskey => "n", -title => "Alt-n"}, "next");
5691 my $patch_max = gitweb_get_feature('patches');
5692 if ($patch_max && !defined $file_name) {
5693 if ($patch_max < 0 || @commitlist <= $patch_max) {
5694 $paging_nav .= " ⋅ " .
5695 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5701 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
5702 if (defined $file_name) {
5703 git_print_header_div('commit', esc_html($co{'title'}), $base);
5705 git_print_header_div('summary', $project)
5707 git_print_page_path($file_name, $ftype, $hash_base)
5708 if (defined $file_name);
5710 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
5711 $file_name, $file_hash, $ftype);
5717 git_log_generic('log', \&git_log_body,
5718 $hash, $hash_parent);
5722 $hash ||= $hash_base || "HEAD";
5723 my %co = parse_commit($hash)
5724 or die_error(404, "Unknown commit object");
5726 my $parent = $co{'parent'};
5727 my $parents = $co{'parents'}; # listref
5729 # we need to prepare $formats_nav before any parameter munging
5731 if (!defined $parent) {
5733 $formats_nav .= '(initial)';
5734 } elsif (@$parents == 1) {
5735 # single parent commit
5738 $cgi->a({-href => href(action=>"commit",
5740 esc_html(substr($parent, 0, 7))) .
5747 $cgi->a({-href => href(action=>"commit",
5749 esc_html(substr($_, 0, 7)));
5753 if (gitweb_check_feature('patches') && @$parents <= 1) {
5754 $formats_nav .= " | " .
5755 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5759 if (!defined $parent) {
5763 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5765 (@$parents <= 1 ? $parent : '-c'),
5767 or die_error(500, "Open git-diff-tree failed");
5768 @difftree = map { chomp; $_ } <$fd>;
5769 close $fd or die_error(404, "Reading git-diff-tree failed");
5771 # non-textual hash id's can be cached
5773 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5776 my $refs = git_get_references();
5777 my $ref = format_ref_marker($refs, $co{'id'});
5779 git_header_html(undef, $expires);
5780 git_print_page_nav('commit', '',
5781 $hash, $co{'tree'}, $hash,
5784 if (defined $co{'parent'}) {
5785 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5787 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5789 print "<div class=\"title_text\">\n" .
5790 "<table class=\"object_header\">\n";
5791 git_print_authorship_rows(\%co);
5792 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5795 "<td class=\"sha1\">" .
5796 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5797 class => "list"}, $co{'tree'}) .
5799 "<td class=\"link\">" .
5800 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5802 my $snapshot_links = format_snapshot_links($hash);
5803 if (defined $snapshot_links) {
5804 print " | " . $snapshot_links;
5809 foreach my $par (@$parents) {
5812 "<td class=\"sha1\">" .
5813 $cgi->a({-href => href(action=>"commit", hash=>$par),
5814 class => "list"}, $par) .
5816 "<td class=\"link\">" .
5817 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
5819 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
5826 print "<div class=\"page_body\">\n";
5827 git_print_log($co{'comment'});
5830 git_difftree_body(\@difftree, $hash, @$parents);
5836 # object is defined by:
5837 # - hash or hash_base alone
5838 # - hash_base and file_name
5841 # - hash or hash_base alone
5842 if ($hash || ($hash_base && !defined $file_name)) {
5843 my $object_id = $hash || $hash_base;
5845 open my $fd, "-|", quote_command(
5846 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
5847 or die_error(404, "Object does not exist");
5851 or die_error(404, "Object does not exist");
5853 # - hash_base and file_name
5854 } elsif ($hash_base && defined $file_name) {
5855 $file_name =~ s,/+$,,;
5857 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
5858 or die_error(404, "Base object does not exist");
5860 # here errors should not hapen
5861 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
5862 or die_error(500, "Open git-ls-tree failed");
5866 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5867 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
5868 die_error(404, "File or directory for given base does not exist");
5873 die_error(400, "Not enough information to find object");
5876 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5877 hash=>$hash, hash_base=>$hash_base,
5878 file_name=>$file_name),
5879 -status => '302 Found');
5883 my $format = shift || 'html';
5890 # preparing $fd and %diffinfo for git_patchset_body
5892 if (defined $hash_base && defined $hash_parent_base) {
5893 if (defined $file_name) {
5895 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5896 $hash_parent_base, $hash_base,
5897 "--", (defined $file_parent ? $file_parent : ()), $file_name
5898 or die_error(500, "Open git-diff-tree failed");
5899 @difftree = map { chomp; $_ } <$fd>;
5901 or die_error(404, "Reading git-diff-tree failed");
5903 or die_error(404, "Blob diff not found");
5905 } elsif (defined $hash &&
5906 $hash =~ /[0-9a-fA-F]{40}/) {
5907 # try to find filename from $hash
5909 # read filtered raw output
5910 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5911 $hash_parent_base, $hash_base, "--"
5912 or die_error(500, "Open git-diff-tree failed");
5914 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5916 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5917 map { chomp; $_ } <$fd>;
5919 or die_error(404, "Reading git-diff-tree failed");
5921 or die_error(404, "Blob diff not found");
5924 die_error(400, "Missing one of the blob diff parameters");
5927 if (@difftree > 1) {
5928 die_error(400, "Ambiguous blob diff specification");
5931 %diffinfo = parse_difftree_raw_line($difftree[0]);
5932 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5933 $file_name ||= $diffinfo{'to_file'};
5935 $hash_parent ||= $diffinfo{'from_id'};
5936 $hash ||= $diffinfo{'to_id'};
5938 # non-textual hash id's can be cached
5939 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5940 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5945 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5946 '-p', ($format eq 'html' ? "--full-index" : ()),
5947 $hash_parent_base, $hash_base,
5948 "--", (defined $file_parent ? $file_parent : ()), $file_name
5949 or die_error(500, "Open git-diff-tree failed");
5952 # old/legacy style URI -- not generated anymore since 1.4.3.
5954 die_error('404 Not Found', "Missing one of the blob diff parameters")
5958 if ($format eq 'html') {
5960 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5962 git_header_html(undef, $expires);
5963 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5964 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5965 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5967 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5968 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5970 if (defined $file_name) {
5971 git_print_page_path($file_name, "blob", $hash_base);
5973 print "<div class=\"page_path\"></div>\n";
5976 } elsif ($format eq 'plain') {
5978 -type => 'text/plain',
5979 -charset => 'utf-8',
5980 -expires => $expires,
5981 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5983 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5986 die_error(400, "Unknown blobdiff format");
5990 if ($format eq 'html') {
5991 print "<div class=\"page_body\">\n";
5993 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5996 print "</div>\n"; # class="page_body"
6000 while (my $line = <$fd>) {
6001 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6002 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6006 last if $line =~ m!^\+\+\+!;
6014 sub git_blobdiff_plain {
6015 git_blobdiff('plain');
6018 sub git_commitdiff {
6020 my $format = $params{-format} || 'html';
6022 my ($patch_max) = gitweb_get_feature('patches');
6023 if ($format eq 'patch') {
6024 die_error(403, "Patch view not allowed") unless $patch_max;
6027 $hash ||= $hash_base || "HEAD";
6028 my %co = parse_commit($hash)
6029 or die_error(404, "Unknown commit object");
6031 # choose format for commitdiff for merge
6032 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
6033 $hash_parent = '--cc';
6035 # we need to prepare $formats_nav before almost any parameter munging
6037 if ($format eq 'html') {
6039 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
6041 if ($patch_max && @{$co{'parents'}} <= 1) {
6042 $formats_nav .= " | " .
6043 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6047 if (defined $hash_parent &&
6048 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6049 # commitdiff with two commits given
6050 my $hash_parent_short = $hash_parent;
6051 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6052 $hash_parent_short = substr($hash_parent, 0, 7);
6056 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6057 if ($co{'parents'}[$i] eq $hash_parent) {
6058 $formats_nav .= ' parent ' . ($i+1);
6062 $formats_nav .= ': ' .
6063 $cgi->a({-href => href(action=>"commitdiff",
6064 hash=>$hash_parent)},
6065 esc_html($hash_parent_short)) .
6067 } elsif (!$co{'parent'}) {
6069 $formats_nav .= ' (initial)';
6070 } elsif (scalar @{$co{'parents'}} == 1) {
6071 # single parent commit
6074 $cgi->a({-href => href(action=>"commitdiff",
6075 hash=>$co{'parent'})},
6076 esc_html(substr($co{'parent'}, 0, 7))) .
6080 if ($hash_parent eq '--cc') {
6081 $formats_nav .= ' | ' .
6082 $cgi->a({-href => href(action=>"commitdiff",
6083 hash=>$hash, hash_parent=>'-c')},
6085 } else { # $hash_parent eq '-c'
6086 $formats_nav .= ' | ' .
6087 $cgi->a({-href => href(action=>"commitdiff",
6088 hash=>$hash, hash_parent=>'--cc')},
6094 $cgi->a({-href => href(action=>"commitdiff",
6096 esc_html(substr($_, 0, 7)));
6097 } @{$co{'parents'}} ) .
6102 my $hash_parent_param = $hash_parent;
6103 if (!defined $hash_parent_param) {
6104 # --cc for multiple parents, --root for parentless
6105 $hash_parent_param =
6106 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6112 if ($format eq 'html') {
6113 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6114 "--no-commit-id", "--patch-with-raw", "--full-index",
6115 $hash_parent_param, $hash, "--"
6116 or die_error(500, "Open git-diff-tree failed");
6118 while (my $line = <$fd>) {
6120 # empty line ends raw part of diff-tree output
6122 push @difftree, scalar parse_difftree_raw_line($line);
6125 } elsif ($format eq 'plain') {
6126 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6127 '-p', $hash_parent_param, $hash, "--"
6128 or die_error(500, "Open git-diff-tree failed");
6129 } elsif ($format eq 'patch') {
6130 # For commit ranges, we limit the output to the number of
6131 # patches specified in the 'patches' feature.
6132 # For single commits, we limit the output to a single patch,
6133 # diverging from the git-format-patch default.
6134 my @commit_spec = ();
6136 if ($patch_max > 0) {
6137 push @commit_spec, "-$patch_max";
6139 push @commit_spec, '-n', "$hash_parent..$hash";
6141 if ($params{-single}) {
6142 push @commit_spec, '-1';
6144 if ($patch_max > 0) {
6145 push @commit_spec, "-$patch_max";
6147 push @commit_spec, "-n";
6149 push @commit_spec, '--root', $hash;
6151 open $fd, "-|", git_cmd(), "format-patch", '--encoding=utf8',
6152 '--stdout', @commit_spec
6153 or die_error(500, "Open git-format-patch failed");
6155 die_error(400, "Unknown commitdiff format");
6158 # non-textual hash id's can be cached
6160 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6164 # write commit message
6165 if ($format eq 'html') {
6166 my $refs = git_get_references();
6167 my $ref = format_ref_marker($refs, $co{'id'});
6169 git_header_html(undef, $expires);
6170 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6171 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6172 print "<div class=\"title_text\">\n" .
6173 "<table class=\"object_header\">\n";
6174 git_print_authorship_rows(\%co);
6177 print "<div class=\"page_body\">\n";
6178 if (@{$co{'comment'}} > 1) {
6179 print "<div class=\"log\">\n";
6180 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6181 print "</div>\n"; # class="log"
6184 } elsif ($format eq 'plain') {
6185 my $refs = git_get_references("tags");
6186 my $tagname = git_get_rev_name_tags($hash);
6187 my $filename = basename($project) . "-$hash.patch";
6190 -type => 'text/plain',
6191 -charset => 'utf-8',
6192 -expires => $expires,
6193 -content_disposition => 'inline; filename="' . "$filename" . '"');
6194 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
6195 print "From: " . to_utf8($co{'author'}) . "\n";
6196 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6197 print "Subject: " . to_utf8($co{'title'}) . "\n";
6199 print "X-Git-Tag: $tagname\n" if $tagname;
6200 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6202 foreach my $line (@{$co{'comment'}}) {
6203 print to_utf8($line) . "\n";
6206 } elsif ($format eq 'patch') {
6207 my $filename = basename($project) . "-$hash.patch";
6210 -type => 'text/plain',
6211 -charset => 'utf-8',
6212 -expires => $expires,
6213 -content_disposition => 'inline; filename="' . "$filename" . '"');
6217 if ($format eq 'html') {
6218 my $use_parents = !defined $hash_parent ||
6219 $hash_parent eq '-c' || $hash_parent eq '--cc';
6220 git_difftree_body(\@difftree, $hash,
6221 $use_parents ? @{$co{'parents'}} : $hash_parent);
6224 git_patchset_body($fd, \@difftree, $hash,
6225 $use_parents ? @{$co{'parents'}} : $hash_parent);
6227 print "</div>\n"; # class="page_body"
6230 } elsif ($format eq 'plain') {
6234 or print "Reading git-diff-tree failed\n";
6235 } elsif ($format eq 'patch') {
6239 or print "Reading git-format-patch failed\n";
6243 sub git_commitdiff_plain {
6244 git_commitdiff(-format => 'plain');
6247 # format-patch-style patches
6249 git_commitdiff(-format => 'patch', -single => 1);
6253 git_commitdiff(-format => 'patch');
6257 git_log_generic('history', \&git_history_body,
6258 $hash_base, $hash_parent_base,
6263 gitweb_check_feature('search') or die_error(403, "Search is disabled");
6264 if (!defined $searchtext) {
6265 die_error(400, "Text field is empty");
6267 if (!defined $hash) {
6268 $hash = git_get_head_hash($project);
6270 my %co = parse_commit($hash);
6272 die_error(404, "Unknown commit object");
6274 if (!defined $page) {
6278 $searchtype ||= 'commit';
6279 if ($searchtype eq 'pickaxe') {
6280 # pickaxe may take all resources of your box and run for several minutes
6281 # with every query - so decide by yourself how public you make this feature
6282 gitweb_check_feature('pickaxe')
6283 or die_error(403, "Pickaxe is disabled");
6285 if ($searchtype eq 'grep') {
6286 gitweb_check_feature('grep')
6287 or die_error(403, "Grep is disabled");
6292 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6294 if ($searchtype eq 'commit') {
6295 $greptype = "--grep=";
6296 } elsif ($searchtype eq 'author') {
6297 $greptype = "--author=";
6298 } elsif ($searchtype eq 'committer') {
6299 $greptype = "--committer=";
6301 $greptype .= $searchtext;
6302 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6303 $greptype, '--regexp-ignore-case',
6304 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6306 my $paging_nav = '';
6309 $cgi->a({-href => href(action=>"search", hash=>$hash,
6310 searchtext=>$searchtext,
6311 searchtype=>$searchtype)},
6313 $paging_nav .= " ⋅ " .
6314 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6315 -accesskey => "p", -title => "Alt-p"}, "prev");
6317 $paging_nav .= "first";
6318 $paging_nav .= " ⋅ prev";
6321 if ($#commitlist >= 100) {
6323 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6324 -accesskey => "n", -title => "Alt-n"}, "next");
6325 $paging_nav .= " ⋅ $next_link";
6327 $paging_nav .= " ⋅ next";
6330 if ($#commitlist >= 100) {
6333 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6334 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6335 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6338 if ($searchtype eq 'pickaxe') {
6339 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6340 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6342 print "<table class=\"pickaxe search\">\n";
6345 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6346 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6347 ($search_use_regexp ? '--pickaxe-regex' : ());
6350 while (my $line = <$fd>) {
6354 my %set = parse_difftree_raw_line($line);
6355 if (defined $set{'commit'}) {
6356 # finish previous commit
6359 "<td class=\"link\">" .
6360 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6362 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6368 print "<tr class=\"dark\">\n";
6370 print "<tr class=\"light\">\n";
6373 %co = parse_commit($set{'commit'});
6374 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6375 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6376 "<td><i>$author</i></td>\n" .
6378 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6379 -class => "list subject"},
6380 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6381 } elsif (defined $set{'to_id'}) {
6382 next if ($set{'to_id'} =~ m/^0{40}$/);
6384 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6385 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6387 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6393 # finish last commit (warning: repetition!)
6396 "<td class=\"link\">" .
6397 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6399 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6407 if ($searchtype eq 'grep') {
6408 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6409 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6411 print "<table class=\"grep_search\">\n";
6415 open my $fd, "-|", git_cmd(), 'grep', '-n',
6416 $search_use_regexp ? ('-E', '-i') : '-F',
6417 $searchtext, $co{'tree'};
6419 while (my $line = <$fd>) {
6421 my ($file, $lno, $ltext, $binary);
6422 last if ($matches++ > 1000);
6423 if ($line =~ /^Binary file (.+) matches$/) {
6427 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6429 if ($file ne $lastfile) {
6430 $lastfile and print "</td></tr>\n";
6432 print "<tr class=\"dark\">\n";
6434 print "<tr class=\"light\">\n";
6436 print "<td class=\"list\">".
6437 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6438 file_name=>"$file"),
6439 -class => "list"}, esc_path($file));
6440 print "</td><td>\n";
6444 print "<div class=\"binary\">Binary file</div>\n";
6446 $ltext = untabify($ltext);
6447 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6448 $ltext = esc_html($1, -nbsp=>1);
6449 $ltext .= '<span class="match">';
6450 $ltext .= esc_html($2, -nbsp=>1);
6451 $ltext .= '</span>';
6452 $ltext .= esc_html($3, -nbsp=>1);
6454 $ltext = esc_html($ltext, -nbsp=>1);
6456 print "<div class=\"pre\">" .
6457 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6458 file_name=>"$file").'#l'.$lno,
6459 -class => "linenr"}, sprintf('%4i', $lno))
6460 . ' ' . $ltext . "</div>\n";
6464 print "</td></tr>\n";
6465 if ($matches > 1000) {
6466 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6469 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6478 sub git_search_help {
6480 git_print_page_nav('','', $hash,$hash,$hash);
6482 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6483 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6484 the pattern entered is recognized as the POSIX extended
6485 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6488 <dt><b>commit</b></dt>
6489 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6491 my $have_grep = gitweb_check_feature('grep');
6494 <dt><b>grep</b></dt>
6495 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6496 a different one) are searched for the given pattern. On large trees, this search can take
6497 a while and put some strain on the server, so please use it with some consideration. Note that
6498 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6499 case-sensitive.</dd>
6503 <dt><b>author</b></dt>
6504 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6505 <dt><b>committer</b></dt>
6506 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6508 my $have_pickaxe = gitweb_check_feature('pickaxe');
6509 if ($have_pickaxe) {
6511 <dt><b>pickaxe</b></dt>
6512 <dd>All commits that caused the string to appear or disappear from any file (changes that
6513 added, removed or "modified" the string) will be listed. This search can take a while and
6514 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6515 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6523 git_log_generic('shortlog', \&git_shortlog_body,
6524 $hash, $hash_parent);
6527 ## ......................................................................
6528 ## feeds (RSS, Atom; OPML)
6531 my $format = shift || 'atom';
6532 my $have_blame = gitweb_check_feature('blame');
6534 # Atom: http://www.atomenabled.org/developers/syndication/
6535 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6536 if ($format ne 'rss' && $format ne 'atom') {
6537 die_error(400, "Unknown web feed format");
6540 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6541 my $head = $hash || 'HEAD';
6542 my @commitlist = parse_commits($head, 150, 0, $file_name);
6546 my $content_type = "application/$format+xml";
6547 if (defined $cgi->http('HTTP_ACCEPT') &&
6548 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6549 # browser (feed reader) prefers text/xml
6550 $content_type = 'text/xml';
6552 if (defined($commitlist[0])) {
6553 %latest_commit = %{$commitlist[0]};
6554 my $latest_epoch = $latest_commit{'committer_epoch'};
6555 %latest_date = parse_date($latest_epoch);
6556 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6557 if (defined $if_modified) {
6559 if (eval { require HTTP::Date; 1; }) {
6560 $since = HTTP::Date::str2time($if_modified);
6561 } elsif (eval { require Time::ParseDate; 1; }) {
6562 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6564 if (defined $since && $latest_epoch <= $since) {
6566 -type => $content_type,
6567 -charset => 'utf-8',
6568 -last_modified => $latest_date{'rfc2822'},
6569 -status => '304 Not Modified');
6574 -type => $content_type,
6575 -charset => 'utf-8',
6576 -last_modified => $latest_date{'rfc2822'});
6579 -type => $content_type,
6580 -charset => 'utf-8');
6583 # Optimization: skip generating the body if client asks only
6584 # for Last-Modified date.
6585 return if ($cgi->request_method() eq 'HEAD');
6588 my $title = "$site_name - $project/$action";
6589 my $feed_type = 'log';
6590 if (defined $hash) {
6591 $title .= " - '$hash'";
6592 $feed_type = 'branch log';
6593 if (defined $file_name) {
6594 $title .= " :: $file_name";
6595 $feed_type = 'history';
6597 } elsif (defined $file_name) {
6598 $title .= " - $file_name";
6599 $feed_type = 'history';
6601 $title .= " $feed_type";
6602 my $descr = git_get_project_description($project);
6603 if (defined $descr) {
6604 $descr = esc_html($descr);
6606 $descr = "$project " .
6607 ($format eq 'rss' ? 'RSS' : 'Atom') .
6610 my $owner = git_get_project_owner($project);
6611 $owner = esc_html($owner);
6615 if (defined $file_name) {
6616 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6617 } elsif (defined $hash) {
6618 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6620 $alt_url = href(-full=>1, action=>"summary");
6622 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6623 if ($format eq 'rss') {
6625 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6628 print "<title>$title</title>\n" .
6629 "<link>$alt_url</link>\n" .
6630 "<description>$descr</description>\n" .
6631 "<language>en</language>\n" .
6632 # project owner is responsible for 'editorial' content
6633 "<managingEditor>$owner</managingEditor>\n";
6634 if (defined $logo || defined $favicon) {
6635 # prefer the logo to the favicon, since RSS
6636 # doesn't allow both
6637 my $img = esc_url($logo || $favicon);
6639 "<url>$img</url>\n" .
6640 "<title>$title</title>\n" .
6641 "<link>$alt_url</link>\n" .
6645 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6646 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6648 print "<generator>gitweb v.$version/$git_version</generator>\n";
6649 } elsif ($format eq 'atom') {
6651 <feed xmlns="http://www.w3.org/2005/Atom">
6653 print "<title>$title</title>\n" .
6654 "<subtitle>$descr</subtitle>\n" .
6655 '<link rel="alternate" type="text/html" href="' .
6656 $alt_url . '" />' . "\n" .
6657 '<link rel="self" type="' . $content_type . '" href="' .
6658 $cgi->self_url() . '" />' . "\n" .
6659 "<id>" . href(-full=>1) . "</id>\n" .
6660 # use project owner for feed author
6661 "<author><name>$owner</name></author>\n";
6662 if (defined $favicon) {
6663 print "<icon>" . esc_url($favicon) . "</icon>\n";
6665 if (defined $logo_url) {
6666 # not twice as wide as tall: 72 x 27 pixels
6667 print "<logo>" . esc_url($logo) . "</logo>\n";
6669 if (! %latest_date) {
6670 # dummy date to keep the feed valid until commits trickle in:
6671 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6673 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6675 print "<generator version='$version/$git_version'>gitweb</generator>\n";
6679 for (my $i = 0; $i <= $#commitlist; $i++) {
6680 my %co = %{$commitlist[$i]};
6681 my $commit = $co{'id'};
6682 # we read 150, we always show 30 and the ones more recent than 48 hours
6683 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6686 my %cd = parse_date($co{'author_epoch'});
6688 # get list of changed files
6689 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6690 $co{'parent'} || "--root",
6691 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6693 my @difftree = map { chomp; $_ } <$fd>;
6697 # print element (entry, item)
6698 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6699 if ($format eq 'rss') {
6701 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6702 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6703 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6704 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6705 "<link>$co_url</link>\n" .
6706 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6707 "<content:encoded>" .
6709 } elsif ($format eq 'atom') {
6711 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6712 "<updated>$cd{'iso-8601'}</updated>\n" .
6714 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6715 if ($co{'author_email'}) {
6716 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6718 print "</author>\n" .
6719 # use committer for contributor
6721 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6722 if ($co{'committer_email'}) {
6723 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6725 print "</contributor>\n" .
6726 "<published>$cd{'iso-8601'}</published>\n" .
6727 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6728 "<id>$co_url</id>\n" .
6729 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6730 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6732 my $comment = $co{'comment'};
6734 foreach my $line (@$comment) {
6735 $line = esc_html($line);
6738 print "</pre><ul>\n";
6739 foreach my $difftree_line (@difftree) {
6740 my %difftree = parse_difftree_raw_line($difftree_line);
6741 next if !$difftree{'from_id'};
6743 my $file = $difftree{'file'} || $difftree{'to_file'};
6747 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6748 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6749 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6750 file_name=>$file, file_parent=>$difftree{'from_file'}),
6751 -title => "diff"}, 'D');
6753 print $cgi->a({-href => href(-full=>1, action=>"blame",
6754 file_name=>$file, hash_base=>$commit),
6755 -title => "blame"}, 'B');
6757 # if this is not a feed of a file history
6758 if (!defined $file_name || $file_name ne $file) {
6759 print $cgi->a({-href => href(-full=>1, action=>"history",
6760 file_name=>$file, hash=>$commit),
6761 -title => "history"}, 'H');
6763 $file = esc_path($file);
6767 if ($format eq 'rss') {
6768 print "</ul>]]>\n" .
6769 "</content:encoded>\n" .
6771 } elsif ($format eq 'atom') {
6772 print "</ul>\n</div>\n" .
6779 if ($format eq 'rss') {
6780 print "</channel>\n</rss>\n";
6781 } elsif ($format eq 'atom') {
6795 my @list = git_get_projects_list();
6798 -type => 'text/xml',
6799 -charset => 'utf-8',
6800 -content_disposition => 'inline; filename="opml.xml"');
6803 <?xml version="1.0" encoding="utf-8"?>
6804 <opml version="1.0">
6806 <title>$site_name OPML Export</title>
6809 <outline text="git RSS feeds">
6812 foreach my $pr (@list) {
6814 my $head = git_get_head_hash($proj{'path'});
6815 if (!defined $head) {
6818 $git_dir = "$projectroot/$proj{'path'}";
6819 my %co = parse_commit($head);
6824 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6825 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
6826 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
6827 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";