3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
13 use CGI qw(:standard :escapeHTML -nosticky);
14 use CGI::Util qw(unescape);
15 use CGI::Carp qw(fatalsToBrowser set_message);
19 use File::Basename qw(basename);
20 use Time::HiRes qw(gettimeofday tv_interval);
21 binmode STDOUT, ':utf8';
23 our $t0 = [ gettimeofday() ];
24 our $number_of_git_cmds = 0;
27 CGI->compile() if $ENV{'MOD_PERL'};
30 our $version = "++GIT_VERSION++";
32 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
36 our $my_url = $cgi->url();
37 our $my_uri = $cgi->url(-absolute => 1);
39 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
40 # needed and used only for URLs with nonempty PATH_INFO
41 our $base_url = $my_url;
43 # When the script is used as DirectoryIndex, the URL does not contain the name
44 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
45 # have to do it ourselves. We make $path_info global because it's also used
48 # Another issue with the script being the DirectoryIndex is that the resulting
49 # $my_url data is not the full script URL: this is good, because we want
50 # generated links to keep implying the script name if it wasn't explicitly
51 # indicated in the URL we're handling, but it means that $my_url cannot be used
53 # Therefore, if we needed to strip PATH_INFO, then we know that we have
54 # to build the base URL ourselves:
55 our $path_info = $ENV{"PATH_INFO"};
57 if ($my_url =~ s,\Q$path_info\E$,, &&
58 $my_uri =~ s,\Q$path_info\E$,, &&
59 defined $ENV{'SCRIPT_NAME'}) {
60 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
64 # target of the home link on top of all pages
65 our $home_link = $my_uri || "/";
68 # core git executable to use
69 # this can just be "git" if your webserver has a sensible PATH
70 our $GIT = "++GIT_BINDIR++/git";
72 # absolute fs-path which will be prepended to the project path
73 #our $projectroot = "/pub/scm";
74 our $projectroot = "++GITWEB_PROJECTROOT++";
76 # fs traversing limit for getting project list
77 # the number is relative to the projectroot
78 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
80 # string of the home link on top of all pages
81 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
83 # name of your site or organization to appear in page titles
84 # replace this with something more descriptive for clearer bookmarks
85 our $site_name = "++GITWEB_SITENAME++"
86 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
88 # filename of html text to include at top of each page
89 our $site_header = "++GITWEB_SITE_HEADER++";
90 # html text to include at home page
91 our $home_text = "++GITWEB_HOMETEXT++";
92 # filename of html text to include at bottom of each page
93 our $site_footer = "++GITWEB_SITE_FOOTER++";
96 our @stylesheets = ("++GITWEB_CSS++");
97 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
98 our $stylesheet = undef;
99 # URI of GIT logo (72x27 size)
100 our $logo = "++GITWEB_LOGO++";
101 # URI of GIT favicon, assumed to be image/png type
102 our $favicon = "++GITWEB_FAVICON++";
103 # URI of gitweb.js (JavaScript code for gitweb)
104 our $javascript = "++GITWEB_JS++";
106 # URI and label (title) of GIT logo link
107 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
108 #our $logo_label = "git documentation";
109 our $logo_url = "http://git-scm.com/";
110 our $logo_label = "git homepage";
112 # source of projects list
113 our $projects_list = "++GITWEB_LIST++";
115 # the width (in characters) of the projects list "Description" column
116 our $projects_list_description_width = 25;
118 # group projects by category on the projects list
119 # (enabled if this variable evaluates to true)
120 our $projects_list_group_categories = 0;
122 # default category if none specified
123 # (leave the empty string for no category)
124 our $project_list_default_category = "";
126 # default order of projects list
127 # valid values are none, project, descr, owner, and age
128 our $default_projects_order = "project";
130 # show repository only if this file exists
131 # (only effective if this variable evaluates to true)
132 our $export_ok = "++GITWEB_EXPORT_OK++";
134 # show repository only if this subroutine returns true
135 # when given the path to the project, for example:
136 # sub { return -e "$_[0]/git-daemon-export-ok"; }
137 our $export_auth_hook = undef;
139 # only allow viewing of repositories also shown on the overview page
140 our $strict_export = "++GITWEB_STRICT_EXPORT++";
142 # list of git base URLs used for URL to where fetch project from,
143 # i.e. full URL is "$git_base_url/$project"
144 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
146 # default blob_plain mimetype and default charset for text/plain blob
147 our $default_blob_plain_mimetype = 'text/plain';
148 our $default_text_plain_charset = undef;
150 # file to use for guessing MIME types before trying /etc/mime.types
151 # (relative to the current git repository)
152 our $mimetypes_file = undef;
154 # assume this charset if line contains non-UTF-8 characters;
155 # it should be valid encoding (see Encoding::Supported(3pm) for list),
156 # for which encoding all byte sequences are valid, for example
157 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
158 # could be even 'utf-8' for the old behavior)
159 our $fallback_encoding = 'latin1';
161 # rename detection options for git-diff and git-diff-tree
162 # - default is '-M', with the cost proportional to
163 # (number of removed files) * (number of new files).
164 # - more costly is '-C' (which implies '-M'), with the cost proportional to
165 # (number of changed files + number of removed files) * (number of new files)
166 # - even more costly is '-C', '--find-copies-harder' with cost
167 # (number of files in the original tree) * (number of new files)
168 # - one might want to include '-B' option, e.g. '-B', '-M'
169 our @diff_opts = ('-M'); # taken from git_commit
171 # Disables features that would allow repository owners to inject script into
173 our $prevent_xss = 0;
175 # Path to the highlight executable to use (must be the one from
176 # http://www.andre-simon.de due to assumptions about parameters and output).
177 # Useful if highlight is not installed on your webserver's PATH.
178 # [Default: highlight]
179 our $highlight_bin = "++HIGHLIGHT_BIN++";
181 # information about snapshot formats that gitweb is capable of serving
182 our %known_snapshot_formats = (
184 # 'display' => display name,
185 # 'type' => mime type,
186 # 'suffix' => filename suffix,
187 # 'format' => --format for git-archive,
188 # 'compressor' => [compressor command and arguments]
189 # (array reference, optional)
190 # 'disabled' => boolean (optional)}
193 'display' => 'tar.gz',
194 'type' => 'application/x-gzip',
195 'suffix' => '.tar.gz',
197 'compressor' => ['gzip', '-n']},
200 'display' => 'tar.bz2',
201 'type' => 'application/x-bzip2',
202 'suffix' => '.tar.bz2',
204 'compressor' => ['bzip2']},
207 'display' => 'tar.xz',
208 'type' => 'application/x-xz',
209 'suffix' => '.tar.xz',
211 'compressor' => ['xz'],
216 'type' => 'application/x-zip',
221 # Aliases so we understand old gitweb.snapshot values in repository
223 our %known_snapshot_format_aliases = (
228 # backward compatibility: legacy gitweb config support
229 'x-gzip' => undef, 'gz' => undef,
230 'x-bzip2' => undef, 'bz2' => undef,
231 'x-zip' => undef, '' => undef,
234 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
235 # are changed, it may be appropriate to change these values too via
242 # Used to set the maximum load that we will still respond to gitweb queries.
243 # If server load exceed this value then return "503 server busy" error.
244 # If gitweb cannot determined server load, it is taken to be 0.
245 # Leave it undefined (or set to 'undef') to turn off load checking.
248 # configuration for 'highlight' (http://www.andre-simon.de/)
250 our %highlight_basename = (
253 'SConstruct' => 'py', # SCons equivalent of Makefile
254 'Makefile' => 'make',
257 our %highlight_ext = (
258 # main extensions, defining name of syntax;
259 # see files in /usr/share/highlight/langDefs/ directory
261 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make),
262 # alternate extensions, see /etc/highlight/filetypes.conf
264 map { $_ => 'sh' } qw(bash zsh ksh),
265 map { $_ => 'cpp' } qw(cxx c++ cc),
266 map { $_ => 'php' } qw(php3 php4 php5 phps),
267 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
268 map { $_ => 'make'} qw(mak mk),
269 map { $_ => 'xml' } qw(xhtml html htm),
272 # You define site-wide feature defaults here; override them with
273 # $GITWEB_CONFIG as necessary.
276 # 'sub' => feature-sub (subroutine),
277 # 'override' => allow-override (boolean),
278 # 'default' => [ default options...] (array reference)}
280 # if feature is overridable (it means that allow-override has true value),
281 # then feature-sub will be called with default options as parameters;
282 # return value of feature-sub indicates if to enable specified feature
284 # if there is no 'sub' key (no feature-sub), then feature cannot be
287 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
288 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
291 # Enable the 'blame' blob view, showing the last commit that modified
292 # each line in the file. This can be very CPU-intensive.
294 # To enable system wide have in $GITWEB_CONFIG
295 # $feature{'blame'}{'default'} = [1];
296 # To have project specific config enable override in $GITWEB_CONFIG
297 # $feature{'blame'}{'override'} = 1;
298 # and in project config gitweb.blame = 0|1;
300 'sub' => sub { feature_bool('blame', @_) },
304 # Enable the 'snapshot' link, providing a compressed archive of any
305 # tree. This can potentially generate high traffic if you have large
308 # Value is a list of formats defined in %known_snapshot_formats that
310 # To disable system wide have in $GITWEB_CONFIG
311 # $feature{'snapshot'}{'default'} = [];
312 # To have project specific config enable override in $GITWEB_CONFIG
313 # $feature{'snapshot'}{'override'} = 1;
314 # and in project config, a comma-separated list of formats or "none"
315 # to disable. Example: gitweb.snapshot = tbz2,zip;
317 'sub' => \&feature_snapshot,
319 'default' => ['tgz']},
321 # Enable text search, which will list the commits which match author,
322 # committer or commit text to a given string. Enabled by default.
323 # Project specific override is not supported.
328 # Enable grep search, which will list the files in currently selected
329 # tree containing the given string. Enabled by default. This can be
330 # potentially CPU-intensive, of course.
331 # Note that you need to have 'search' feature enabled too.
333 # To enable system wide have in $GITWEB_CONFIG
334 # $feature{'grep'}{'default'} = [1];
335 # To have project specific config enable override in $GITWEB_CONFIG
336 # $feature{'grep'}{'override'} = 1;
337 # and in project config gitweb.grep = 0|1;
339 'sub' => sub { feature_bool('grep', @_) },
343 # Enable the pickaxe search, which will list the commits that modified
344 # a given string in a file. This can be practical and quite faster
345 # alternative to 'blame', but still potentially CPU-intensive.
346 # Note that you need to have 'search' feature enabled too.
348 # To enable system wide have in $GITWEB_CONFIG
349 # $feature{'pickaxe'}{'default'} = [1];
350 # To have project specific config enable override in $GITWEB_CONFIG
351 # $feature{'pickaxe'}{'override'} = 1;
352 # and in project config gitweb.pickaxe = 0|1;
354 'sub' => sub { feature_bool('pickaxe', @_) },
358 # Enable showing size of blobs in a 'tree' view, in a separate
359 # column, similar to what 'ls -l' does. This cost a bit of IO.
361 # To disable system wide have in $GITWEB_CONFIG
362 # $feature{'show-sizes'}{'default'} = [0];
363 # To have project specific config enable override in $GITWEB_CONFIG
364 # $feature{'show-sizes'}{'override'} = 1;
365 # and in project config gitweb.showsizes = 0|1;
367 'sub' => sub { feature_bool('showsizes', @_) },
371 # Make gitweb use an alternative format of the URLs which can be
372 # more readable and natural-looking: project name is embedded
373 # directly in the path and the query string contains other
374 # auxiliary information. All gitweb installations recognize
375 # URL in either format; this configures in which formats gitweb
378 # To enable system wide have in $GITWEB_CONFIG
379 # $feature{'pathinfo'}{'default'} = [1];
380 # Project specific override is not supported.
382 # Note that you will need to change the default location of CSS,
383 # favicon, logo and possibly other files to an absolute URL. Also,
384 # if gitweb.cgi serves as your indexfile, you will need to force
385 # $my_uri to contain the script name in your $GITWEB_CONFIG.
390 # Make gitweb consider projects in project root subdirectories
391 # to be forks of existing projects. Given project $projname.git,
392 # projects matching $projname/*.git will not be shown in the main
393 # projects list, instead a '+' mark will be added to $projname
394 # there and a 'forks' view will be enabled for the project, listing
395 # all the forks. If project list is taken from a file, forks have
396 # to be listed after the main project.
398 # To enable system wide have in $GITWEB_CONFIG
399 # $feature{'forks'}{'default'} = [1];
400 # Project specific override is not supported.
405 # Insert custom links to the action bar of all project pages.
406 # This enables you mainly to link to third-party scripts integrating
407 # into gitweb; e.g. git-browser for graphical history representation
408 # or custom web-based repository administration interface.
410 # The 'default' value consists of a list of triplets in the form
411 # (label, link, position) where position is the label after which
412 # to insert the link and link is a format string where %n expands
413 # to the project name, %f to the project path within the filesystem,
414 # %h to the current hash (h gitweb parameter) and %b to the current
415 # hash base (hb gitweb parameter); %% expands to %.
417 # To enable system wide have in $GITWEB_CONFIG e.g.
418 # $feature{'actions'}{'default'} = [('graphiclog',
419 # '/git-browser/by-commit.html?r=%n', 'summary')];
420 # Project specific override is not supported.
425 # Allow gitweb scan project content tags of project repository,
426 # and display the popular Web 2.0-ish "tag cloud" near the projects
427 # list. Note that this is something COMPLETELY different from the
430 # gitweb by itself can show existing tags, but it does not handle
431 # tagging itself; you need to do it externally, outside gitweb.
432 # The format is described in git_get_project_ctags() subroutine.
433 # You may want to install the HTML::TagCloud Perl module to get
434 # a pretty tag cloud instead of just a list of tags.
436 # To enable system wide have in $GITWEB_CONFIG
437 # $feature{'ctags'}{'default'} = [1];
438 # Project specific override is not supported.
440 # In the future whether ctags editing is enabled might depend
441 # on the value, but using 1 should always mean no editing of ctags.
446 # The maximum number of patches in a patchset generated in patch
447 # view. Set this to 0 or undef to disable patch view, or to a
448 # negative number to remove any limit.
450 # To disable system wide have in $GITWEB_CONFIG
451 # $feature{'patches'}{'default'} = [0];
452 # To have project specific config enable override in $GITWEB_CONFIG
453 # $feature{'patches'}{'override'} = 1;
454 # and in project config gitweb.patches = 0|n;
455 # where n is the maximum number of patches allowed in a patchset.
457 'sub' => \&feature_patches,
461 # Avatar support. When this feature is enabled, views such as
462 # shortlog or commit will display an avatar associated with
463 # the email of the committer(s) and/or author(s).
465 # Currently available providers are gravatar and picon.
466 # If an unknown provider is specified, the feature is disabled.
468 # Gravatar depends on Digest::MD5.
469 # Picon currently relies on the indiana.edu database.
471 # To enable system wide have in $GITWEB_CONFIG
472 # $feature{'avatar'}{'default'} = ['<provider>'];
473 # where <provider> is either gravatar or picon.
474 # To have project specific config enable override in $GITWEB_CONFIG
475 # $feature{'avatar'}{'override'} = 1;
476 # and in project config gitweb.avatar = <provider>;
478 'sub' => \&feature_avatar,
482 # Enable displaying how much time and how many git commands
483 # it took to generate and display page. Disabled by default.
484 # Project specific override is not supported.
489 # Enable turning some links into links to actions which require
490 # JavaScript to run (like 'blame_incremental'). Not enabled by
491 # default. Project specific override is currently not supported.
492 'javascript-actions' => {
496 # Enable and configure ability to change common timezone for dates
497 # in gitweb output via JavaScript. Enabled by default.
498 # Project specific override is not supported.
499 'javascript-timezone' => {
502 'local', # default timezone: 'utc', 'local', or '(-|+)HHMM' format,
503 # or undef to turn off this feature
504 'gitweb_tz', # name of cookie where to store selected timezone
505 'datetime', # CSS class used to mark up dates for manipulation
508 # Syntax highlighting support. This is based on Daniel Svensson's
509 # and Sham Chukoury's work in gitweb-xmms2.git.
510 # It requires the 'highlight' program present in $PATH,
511 # and therefore is disabled by default.
513 # To enable system wide have in $GITWEB_CONFIG
514 # $feature{'highlight'}{'default'} = [1];
517 'sub' => sub { feature_bool('highlight', @_) },
521 # Enable displaying of remote heads in the heads list
523 # To enable system wide have in $GITWEB_CONFIG
524 # $feature{'remote_heads'}{'default'} = [1];
525 # To have project specific config enable override in $GITWEB_CONFIG
526 # $feature{'remote_heads'}{'override'} = 1;
527 # and in project config gitweb.remote_heads = 0|1;
529 'sub' => sub { feature_bool('remote_heads', @_) },
534 sub gitweb_get_feature {
536 return unless exists $feature{$name};
537 my ($sub, $override, @defaults) = (
538 $feature{$name}{'sub'},
539 $feature{$name}{'override'},
540 @{$feature{$name}{'default'}});
541 # project specific override is possible only if we have project
542 our $git_dir; # global variable, declared later
543 if (!$override || !defined $git_dir) {
547 warn "feature $name is not overridable";
550 return $sub->(@defaults);
553 # A wrapper to check if a given feature is enabled.
554 # With this, you can say
556 # my $bool_feat = gitweb_check_feature('bool_feat');
557 # gitweb_check_feature('bool_feat') or somecode;
561 # my ($bool_feat) = gitweb_get_feature('bool_feat');
562 # (gitweb_get_feature('bool_feat'))[0] or somecode;
564 sub gitweb_check_feature {
565 return (gitweb_get_feature(@_))[0];
571 my ($val) = git_get_project_config($key, '--bool');
575 } elsif ($val eq 'true') {
577 } elsif ($val eq 'false') {
582 sub feature_snapshot {
585 my ($val) = git_get_project_config('snapshot');
588 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
594 sub feature_patches {
595 my @val = (git_get_project_config('patches', '--int'));
605 my @val = (git_get_project_config('avatar'));
607 return @val ? @val : @_;
610 # checking HEAD file with -e is fragile if the repository was
611 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
613 sub check_head_link {
615 my $headfile = "$dir/HEAD";
616 return ((-e $headfile) ||
617 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
620 sub check_export_ok {
622 return (check_head_link($dir) &&
623 (!$export_ok || -e "$dir/$export_ok") &&
624 (!$export_auth_hook || $export_auth_hook->($dir)));
627 # process alternate names for backward compatibility
628 # filter out unsupported (unknown) snapshot formats
629 sub filter_snapshot_fmts {
633 exists $known_snapshot_format_aliases{$_} ?
634 $known_snapshot_format_aliases{$_} : $_} @fmts;
636 exists $known_snapshot_formats{$_} &&
637 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
640 # If it is set to code reference, it is code that it is to be run once per
641 # request, allowing updating configurations that change with each request,
642 # while running other code in config file only once.
644 # Otherwise, if it is false then gitweb would process config file only once;
645 # if it is true then gitweb config would be run for each request.
646 our $per_request_config = 1;
648 # read and parse gitweb config file given by its parameter.
649 # returns true on success, false on recoverable error, allowing
650 # to chain this subroutine, using first file that exists.
651 # dies on errors during parsing config file, as it is unrecoverable.
652 sub read_config_file {
653 my $filename = shift;
654 return unless defined $filename;
655 # die if there are errors parsing config file
664 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM);
665 sub evaluate_gitweb_config {
666 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
667 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
669 # use first config file that exists
670 read_config_file($GITWEB_CONFIG) or
671 read_config_file($GITWEB_CONFIG_SYSTEM);
674 # Get loadavg of system, to compare against $maxload.
675 # Currently it requires '/proc/loadavg' present to get loadavg;
676 # if it is not present it returns 0, which means no load checking.
678 if( -e '/proc/loadavg' ){
679 open my $fd, '<', '/proc/loadavg'
681 my @load = split(/\s+/, scalar <$fd>);
684 # The first three columns measure CPU and IO utilization of the last one,
685 # five, and 10 minute periods. The fourth column shows the number of
686 # currently running processes and the total number of processes in the m/n
687 # format. The last column displays the last process ID used.
688 return $load[0] || 0;
690 # additional checks for load average should go here for things that don't export
696 # version of the core git binary
698 sub evaluate_git_version {
699 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
700 $number_of_git_cmds++;
704 if (defined $maxload && get_loadavg() > $maxload) {
705 die_error(503, "The load average on the server is too high");
709 # ======================================================================
710 # input validation and dispatch
712 # input parameters can be collected from a variety of sources (presently, CGI
713 # and PATH_INFO), so we define an %input_params hash that collects them all
714 # together during validation: this allows subsequent uses (e.g. href()) to be
715 # agnostic of the parameter origin
717 our %input_params = ();
719 # input parameters are stored with the long parameter name as key. This will
720 # also be used in the href subroutine to convert parameters to their CGI
721 # equivalent, and since the href() usage is the most frequent one, we store
722 # the name -> CGI key mapping here, instead of the reverse.
724 # XXX: Warning: If you touch this, check the search form for updating,
727 our @cgi_param_mapping = (
735 hash_parent_base => "hpb",
740 snapshot_format => "sf",
741 extra_options => "opt",
742 search_use_regexp => "sr",
744 # this must be last entry (for manipulation from JavaScript)
747 our %cgi_param_mapping = @cgi_param_mapping;
749 # we will also need to know the possible actions, for validation
751 "blame" => \&git_blame,
752 "blame_incremental" => \&git_blame_incremental,
753 "blame_data" => \&git_blame_data,
754 "blobdiff" => \&git_blobdiff,
755 "blobdiff_plain" => \&git_blobdiff_plain,
756 "blob" => \&git_blob,
757 "blob_plain" => \&git_blob_plain,
758 "commitdiff" => \&git_commitdiff,
759 "commitdiff_plain" => \&git_commitdiff_plain,
760 "commit" => \&git_commit,
761 "forks" => \&git_forks,
762 "heads" => \&git_heads,
763 "history" => \&git_history,
765 "patch" => \&git_patch,
766 "patches" => \&git_patches,
767 "remotes" => \&git_remotes,
769 "atom" => \&git_atom,
770 "search" => \&git_search,
771 "search_help" => \&git_search_help,
772 "shortlog" => \&git_shortlog,
773 "summary" => \&git_summary,
775 "tags" => \&git_tags,
776 "tree" => \&git_tree,
777 "snapshot" => \&git_snapshot,
778 "object" => \&git_object,
779 # those below don't need $project
780 "opml" => \&git_opml,
781 "project_list" => \&git_project_list,
782 "project_index" => \&git_project_index,
785 # finally, we have the hash of allowed extra_options for the commands that
787 our %allowed_options = (
788 "--no-merges" => [ qw(rss atom log shortlog history) ],
791 # fill %input_params with the CGI parameters. All values except for 'opt'
792 # should be single values, but opt can be an array. We should probably
793 # build an array of parameters that can be multi-valued, but since for the time
794 # being it's only this one, we just single it out
795 sub evaluate_query_params {
798 while (my ($name, $symbol) = each %cgi_param_mapping) {
799 if ($symbol eq 'opt') {
800 $input_params{$name} = [ $cgi->param($symbol) ];
802 $input_params{$name} = $cgi->param($symbol);
807 # now read PATH_INFO and update the parameter list for missing parameters
808 sub evaluate_path_info {
809 return if defined $input_params{'project'};
810 return if !$path_info;
811 $path_info =~ s,^/+,,;
812 return if !$path_info;
814 # find which part of PATH_INFO is project
815 my $project = $path_info;
817 while ($project && !check_head_link("$projectroot/$project")) {
818 $project =~ s,/*[^/]*$,,;
820 return unless $project;
821 $input_params{'project'} = $project;
823 # do not change any parameters if an action is given using the query string
824 return if $input_params{'action'};
825 $path_info =~ s,^\Q$project\E/*,,;
827 # next, check if we have an action
828 my $action = $path_info;
830 if (exists $actions{$action}) {
831 $path_info =~ s,^$action/*,,;
832 $input_params{'action'} = $action;
835 # list of actions that want hash_base instead of hash, but can have no
836 # pathname (f) parameter
842 # we want to catch, among others
843 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
844 my ($parentrefname, $parentpathname, $refname, $pathname) =
845 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/);
847 # first, analyze the 'current' part
848 if (defined $pathname) {
849 # we got "branch:filename" or "branch:dir/"
850 # we could use git_get_type(branch:pathname), but:
851 # - it needs $git_dir
852 # - it does a git() call
853 # - the convention of terminating directories with a slash
854 # makes it superfluous
855 # - embedding the action in the PATH_INFO would make it even
857 $pathname =~ s,^/+,,;
858 if (!$pathname || substr($pathname, -1) eq "/") {
859 $input_params{'action'} ||= "tree";
862 # the default action depends on whether we had parent info
864 if ($parentrefname) {
865 $input_params{'action'} ||= "blobdiff_plain";
867 $input_params{'action'} ||= "blob_plain";
870 $input_params{'hash_base'} ||= $refname;
871 $input_params{'file_name'} ||= $pathname;
872 } elsif (defined $refname) {
873 # we got "branch". In this case we have to choose if we have to
874 # set hash or hash_base.
876 # Most of the actions without a pathname only want hash to be
877 # set, except for the ones specified in @wants_base that want
878 # hash_base instead. It should also be noted that hand-crafted
879 # links having 'history' as an action and no pathname or hash
880 # set will fail, but that happens regardless of PATH_INFO.
881 if (defined $parentrefname) {
882 # if there is parent let the default be 'shortlog' action
883 # (for http://git.example.com/repo.git/A..B links); if there
884 # is no parent, dispatch will detect type of object and set
885 # action appropriately if required (if action is not set)
886 $input_params{'action'} ||= "shortlog";
888 if ($input_params{'action'} &&
889 grep { $_ eq $input_params{'action'} } @wants_base) {
890 $input_params{'hash_base'} ||= $refname;
892 $input_params{'hash'} ||= $refname;
896 # next, handle the 'parent' part, if present
897 if (defined $parentrefname) {
898 # a missing pathspec defaults to the 'current' filename, allowing e.g.
899 # someproject/blobdiff/oldrev..newrev:/filename
900 if ($parentpathname) {
901 $parentpathname =~ s,^/+,,;
902 $parentpathname =~ s,/$,,;
903 $input_params{'file_parent'} ||= $parentpathname;
905 $input_params{'file_parent'} ||= $input_params{'file_name'};
907 # we assume that hash_parent_base is wanted if a path was specified,
908 # or if the action wants hash_base instead of hash
909 if (defined $input_params{'file_parent'} ||
910 grep { $_ eq $input_params{'action'} } @wants_base) {
911 $input_params{'hash_parent_base'} ||= $parentrefname;
913 $input_params{'hash_parent'} ||= $parentrefname;
917 # for the snapshot action, we allow URLs in the form
918 # $project/snapshot/$hash.ext
919 # where .ext determines the snapshot and gets removed from the
920 # passed $refname to provide the $hash.
922 # To be able to tell that $refname includes the format extension, we
923 # require the following two conditions to be satisfied:
924 # - the hash input parameter MUST have been set from the $refname part
925 # of the URL (i.e. they must be equal)
926 # - the snapshot format MUST NOT have been defined already (e.g. from
928 # It's also useless to try any matching unless $refname has a dot,
929 # so we check for that too
930 if (defined $input_params{'action'} &&
931 $input_params{'action'} eq 'snapshot' &&
932 defined $refname && index($refname, '.') != -1 &&
933 $refname eq $input_params{'hash'} &&
934 !defined $input_params{'snapshot_format'}) {
935 # We loop over the known snapshot formats, checking for
936 # extensions. Allowed extensions are both the defined suffix
937 # (which includes the initial dot already) and the snapshot
938 # format key itself, with a prepended dot
939 while (my ($fmt, $opt) = each %known_snapshot_formats) {
941 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
945 # a valid suffix was found, so set the snapshot format
946 # and reset the hash parameter
947 $input_params{'snapshot_format'} = $fmt;
948 $input_params{'hash'} = $hash;
949 # we also set the format suffix to the one requested
950 # in the URL: this way a request for e.g. .tgz returns
951 # a .tgz instead of a .tar.gz
952 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
958 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
959 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
960 $searchtext, $search_regexp);
961 sub evaluate_and_validate_params {
962 our $action = $input_params{'action'};
963 if (defined $action) {
964 if (!validate_action($action)) {
965 die_error(400, "Invalid action parameter");
969 # parameters which are pathnames
970 our $project = $input_params{'project'};
971 if (defined $project) {
972 if (!validate_project($project)) {
974 die_error(404, "No such project");
978 our $file_name = $input_params{'file_name'};
979 if (defined $file_name) {
980 if (!validate_pathname($file_name)) {
981 die_error(400, "Invalid file parameter");
985 our $file_parent = $input_params{'file_parent'};
986 if (defined $file_parent) {
987 if (!validate_pathname($file_parent)) {
988 die_error(400, "Invalid file parent parameter");
992 # parameters which are refnames
993 our $hash = $input_params{'hash'};
995 if (!validate_refname($hash)) {
996 die_error(400, "Invalid hash parameter");
1000 our $hash_parent = $input_params{'hash_parent'};
1001 if (defined $hash_parent) {
1002 if (!validate_refname($hash_parent)) {
1003 die_error(400, "Invalid hash parent parameter");
1007 our $hash_base = $input_params{'hash_base'};
1008 if (defined $hash_base) {
1009 if (!validate_refname($hash_base)) {
1010 die_error(400, "Invalid hash base parameter");
1014 our @extra_options = @{$input_params{'extra_options'}};
1015 # @extra_options is always defined, since it can only be (currently) set from
1016 # CGI, and $cgi->param() returns the empty array in array context if the param
1018 foreach my $opt (@extra_options) {
1019 if (not exists $allowed_options{$opt}) {
1020 die_error(400, "Invalid option parameter");
1022 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
1023 die_error(400, "Invalid option parameter for this action");
1027 our $hash_parent_base = $input_params{'hash_parent_base'};
1028 if (defined $hash_parent_base) {
1029 if (!validate_refname($hash_parent_base)) {
1030 die_error(400, "Invalid hash parent base parameter");
1035 our $page = $input_params{'page'};
1036 if (defined $page) {
1037 if ($page =~ m/[^0-9]/) {
1038 die_error(400, "Invalid page parameter");
1042 our $searchtype = $input_params{'searchtype'};
1043 if (defined $searchtype) {
1044 if ($searchtype =~ m/[^a-z]/) {
1045 die_error(400, "Invalid searchtype parameter");
1049 our $search_use_regexp = $input_params{'search_use_regexp'};
1051 our $searchtext = $input_params{'searchtext'};
1053 if (defined $searchtext) {
1054 if (length($searchtext) < 2) {
1055 die_error(403, "At least two characters are required for search parameter");
1057 if ($search_use_regexp) {
1058 $search_regexp = $searchtext;
1059 if (!eval { qr/$search_regexp/; 1; }) {
1060 (my $error = $@) =~ s/ at \S+ line \d+.*\n?//;
1061 die_error(400, "Invalid search regexp '$search_regexp'",
1065 $search_regexp = quotemeta $searchtext;
1070 # path to the current git repository
1072 sub evaluate_git_dir {
1073 our $git_dir = "$projectroot/$project" if $project;
1076 our (@snapshot_fmts, $git_avatar);
1077 sub configure_gitweb_features {
1078 # list of supported snapshot formats
1079 our @snapshot_fmts = gitweb_get_feature('snapshot');
1080 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1082 # check that the avatar feature is set to a known provider name,
1083 # and for each provider check if the dependencies are satisfied.
1084 # if the provider name is invalid or the dependencies are not met,
1085 # reset $git_avatar to the empty string.
1086 our ($git_avatar) = gitweb_get_feature('avatar');
1087 if ($git_avatar eq 'gravatar') {
1088 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1089 } elsif ($git_avatar eq 'picon') {
1096 # custom error handler: 'die <message>' is Internal Server Error
1097 sub handle_errors_html {
1098 my $msg = shift; # it is already HTML escaped
1100 # to avoid infinite loop where error occurs in die_error,
1101 # change handler to default handler, disabling handle_errors_html
1102 set_message("Error occured when inside die_error:\n$msg");
1104 # you cannot jump out of die_error when called as error handler;
1105 # the subroutine set via CGI::Carp::set_message is called _after_
1106 # HTTP headers are already written, so it cannot write them itself
1107 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1109 set_message(\&handle_errors_html);
1113 if (!defined $action) {
1114 if (defined $hash) {
1115 $action = git_get_type($hash);
1116 } elsif (defined $hash_base && defined $file_name) {
1117 $action = git_get_type("$hash_base:$file_name");
1118 } elsif (defined $project) {
1119 $action = 'summary';
1121 $action = 'project_list';
1124 if (!defined($actions{$action})) {
1125 die_error(400, "Unknown action");
1127 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1129 die_error(400, "Project needed");
1131 $actions{$action}->();
1135 our $t0 = [ gettimeofday() ]
1137 our $number_of_git_cmds = 0;
1140 our $first_request = 1;
1145 if ($first_request) {
1146 evaluate_gitweb_config();
1147 evaluate_git_version();
1149 if ($per_request_config) {
1150 if (ref($per_request_config) eq 'CODE') {
1151 $per_request_config->();
1152 } elsif (!$first_request) {
1153 evaluate_gitweb_config();
1158 # $projectroot and $projects_list might be set in gitweb config file
1159 $projects_list ||= $projectroot;
1161 evaluate_query_params();
1162 evaluate_path_info();
1163 evaluate_and_validate_params();
1166 configure_gitweb_features();
1171 our $is_last_request = sub { 1 };
1172 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1175 sub configure_as_fcgi {
1177 our $CGI = 'CGI::Fast';
1179 my $request_number = 0;
1180 # let each child service 100 requests
1181 our $is_last_request = sub { ++$request_number > 100 };
1184 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1186 if $script_name =~ /\.fcgi$/;
1188 return unless (@ARGV);
1190 require Getopt::Long;
1191 Getopt::Long::GetOptions(
1192 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1193 'nproc|n=i' => sub {
1194 my ($arg, $val) = @_;
1195 return unless eval { require FCGI::ProcManager; 1; };
1196 my $proc_manager = FCGI::ProcManager->new({
1197 n_processes => $val,
1199 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1200 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1201 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1210 $pre_listen_hook->()
1211 if $pre_listen_hook;
1214 while ($cgi = $CGI->new()) {
1215 $pre_dispatch_hook->()
1216 if $pre_dispatch_hook;
1220 $post_dispatch_hook->()
1221 if $post_dispatch_hook;
1224 last REQUEST if ($is_last_request->());
1233 if (defined caller) {
1234 # wrapped in a subroutine processing requests,
1235 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1238 # pure CGI script, serving single request
1242 ## ======================================================================
1245 # possible values of extra options
1246 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1247 # -replay => 1 - start from a current view (replay with modifications)
1248 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1249 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1252 # default is to use -absolute url() i.e. $my_uri
1253 my $href = $params{-full} ? $my_url : $my_uri;
1255 # implicit -replay, must be first of implicit params
1256 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1258 $params{'project'} = $project unless exists $params{'project'};
1260 if ($params{-replay}) {
1261 while (my ($name, $symbol) = each %cgi_param_mapping) {
1262 if (!exists $params{$name}) {
1263 $params{$name} = $input_params{$name};
1268 my $use_pathinfo = gitweb_check_feature('pathinfo');
1269 if (defined $params{'project'} &&
1270 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1271 # try to put as many parameters as possible in PATH_INFO:
1274 # - hash_parent or hash_parent_base:/file_parent
1275 # - hash or hash_base:/filename
1276 # - the snapshot_format as an appropriate suffix
1278 # When the script is the root DirectoryIndex for the domain,
1279 # $href here would be something like http://gitweb.example.com/
1280 # Thus, we strip any trailing / from $href, to spare us double
1281 # slashes in the final URL
1284 # Then add the project name, if present
1285 $href .= "/".esc_path_info($params{'project'});
1286 delete $params{'project'};
1288 # since we destructively absorb parameters, we keep this
1289 # boolean that remembers if we're handling a snapshot
1290 my $is_snapshot = $params{'action'} eq 'snapshot';
1292 # Summary just uses the project path URL, any other action is
1294 if (defined $params{'action'}) {
1295 $href .= "/".esc_path_info($params{'action'})
1296 unless $params{'action'} eq 'summary';
1297 delete $params{'action'};
1300 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1301 # stripping nonexistent or useless pieces
1302 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1303 || $params{'hash_parent'} || $params{'hash'});
1304 if (defined $params{'hash_base'}) {
1305 if (defined $params{'hash_parent_base'}) {
1306 $href .= esc_path_info($params{'hash_parent_base'});
1307 # skip the file_parent if it's the same as the file_name
1308 if (defined $params{'file_parent'}) {
1309 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1310 delete $params{'file_parent'};
1311 } elsif ($params{'file_parent'} !~ /\.\./) {
1312 $href .= ":/".esc_path_info($params{'file_parent'});
1313 delete $params{'file_parent'};
1317 delete $params{'hash_parent'};
1318 delete $params{'hash_parent_base'};
1319 } elsif (defined $params{'hash_parent'}) {
1320 $href .= esc_path_info($params{'hash_parent'}). "..";
1321 delete $params{'hash_parent'};
1324 $href .= esc_path_info($params{'hash_base'});
1325 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1326 $href .= ":/".esc_path_info($params{'file_name'});
1327 delete $params{'file_name'};
1329 delete $params{'hash'};
1330 delete $params{'hash_base'};
1331 } elsif (defined $params{'hash'}) {
1332 $href .= esc_path_info($params{'hash'});
1333 delete $params{'hash'};
1336 # If the action was a snapshot, we can absorb the
1337 # snapshot_format parameter too
1339 my $fmt = $params{'snapshot_format'};
1340 # snapshot_format should always be defined when href()
1341 # is called, but just in case some code forgets, we
1342 # fall back to the default
1343 $fmt ||= $snapshot_fmts[0];
1344 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1345 delete $params{'snapshot_format'};
1349 # now encode the parameters explicitly
1351 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1352 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1353 if (defined $params{$name}) {
1354 if (ref($params{$name}) eq "ARRAY") {
1355 foreach my $par (@{$params{$name}}) {
1356 push @result, $symbol . "=" . esc_param($par);
1359 push @result, $symbol . "=" . esc_param($params{$name});
1363 $href .= "?" . join(';', @result) if scalar @result;
1365 # final transformation: trailing spaces must be escaped (URI-encoded)
1366 $href =~ s/(\s+)$/CGI::escape($1)/e;
1368 if ($params{-anchor}) {
1369 $href .= "#".esc_param($params{-anchor});
1376 ## ======================================================================
1377 ## validation, quoting/unquoting and escaping
1379 sub validate_action {
1380 my $input = shift || return undef;
1381 return undef unless exists $actions{$input};
1385 sub validate_project {
1386 my $input = shift || return undef;
1387 if (!validate_pathname($input) ||
1388 !(-d "$projectroot/$input") ||
1389 !check_export_ok("$projectroot/$input") ||
1390 ($strict_export && !project_in_list($input))) {
1397 sub validate_pathname {
1398 my $input = shift || return undef;
1400 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1401 # at the beginning, at the end, and between slashes.
1402 # also this catches doubled slashes
1403 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1406 # no null characters
1407 if ($input =~ m!\0!) {
1413 sub validate_refname {
1414 my $input = shift || return undef;
1416 # textual hashes are O.K.
1417 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1420 # it must be correct pathname
1421 $input = validate_pathname($input)
1423 # restrictions on ref name according to git-check-ref-format
1424 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1430 # decode sequences of octets in utf8 into Perl's internal form,
1431 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1432 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1435 return undef unless defined $str;
1436 if (utf8::valid($str)) {
1440 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1444 # quote unsafe chars, but keep the slash, even when it's not
1445 # correct, but quoted slashes look too horrible in bookmarks
1448 return undef unless defined $str;
1449 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1454 # the quoting rules for path_info fragment are slightly different
1457 return undef unless defined $str;
1459 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1460 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1465 # quote unsafe chars in whole URL, so some characters cannot be quoted
1468 return undef unless defined $str;
1469 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1474 # quote unsafe characters in HTML attributes
1477 # for XHTML conformance escaping '"' to '"' is not enough
1478 return esc_html(@_);
1481 # replace invalid utf8 character with SUBSTITUTION sequence
1486 return undef unless defined $str;
1488 $str = to_utf8($str);
1489 $str = $cgi->escapeHTML($str);
1490 if ($opts{'-nbsp'}) {
1491 $str =~ s/ / /g;
1493 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1497 # quote control characters and escape filename to HTML
1502 return undef unless defined $str;
1504 $str = to_utf8($str);
1505 $str = $cgi->escapeHTML($str);
1506 if ($opts{'-nbsp'}) {
1507 $str =~ s/ / /g;
1509 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1513 # Make control characters "printable", using character escape codes (CEC)
1517 my %es = ( # character escape codes, aka escape sequences
1518 "\t" => '\t', # tab (HT)
1519 "\n" => '\n', # line feed (LF)
1520 "\r" => '\r', # carrige return (CR)
1521 "\f" => '\f', # form feed (FF)
1522 "\b" => '\b', # backspace (BS)
1523 "\a" => '\a', # alarm (bell) (BEL)
1524 "\e" => '\e', # escape (ESC)
1525 "\013" => '\v', # vertical tab (VT)
1526 "\000" => '\0', # nul character (NUL)
1528 my $chr = ( (exists $es{$cntrl})
1530 : sprintf('\%2x', ord($cntrl)) );
1531 if ($opts{-nohtml}) {
1534 return "<span class=\"cntrl\">$chr</span>";
1538 # Alternatively use unicode control pictures codepoints,
1539 # Unicode "printable representation" (PR)
1544 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1545 if ($opts{-nohtml}) {
1548 return "<span class=\"cntrl\">$chr</span>";
1552 # git may return quoted and escaped filenames
1558 my %es = ( # character escape codes, aka escape sequences
1559 't' => "\t", # tab (HT, TAB)
1560 'n' => "\n", # newline (NL)
1561 'r' => "\r", # return (CR)
1562 'f' => "\f", # form feed (FF)
1563 'b' => "\b", # backspace (BS)
1564 'a' => "\a", # alarm (bell) (BEL)
1565 'e' => "\e", # escape (ESC)
1566 'v' => "\013", # vertical tab (VT)
1569 if ($seq =~ m/^[0-7]{1,3}$/) {
1570 # octal char sequence
1571 return chr(oct($seq));
1572 } elsif (exists $es{$seq}) {
1573 # C escape sequence, aka character escape code
1576 # quoted ordinary character
1580 if ($str =~ m/^"(.*)"$/) {
1583 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1588 # escape tabs (convert tabs to spaces)
1592 while ((my $pos = index($line, "\t")) != -1) {
1593 if (my $count = (8 - ($pos % 8))) {
1594 my $spaces = ' ' x $count;
1595 $line =~ s/\t/$spaces/;
1602 sub project_in_list {
1603 my $project = shift;
1604 my @list = git_get_projects_list();
1605 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1608 ## ----------------------------------------------------------------------
1609 ## HTML aware string manipulation
1611 # Try to chop given string on a word boundary between position
1612 # $len and $len+$add_len. If there is no word boundary there,
1613 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1614 # (marking chopped part) would be longer than given string.
1618 my $add_len = shift || 10;
1619 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1621 # Make sure perl knows it is utf8 encoded so we don't
1622 # cut in the middle of a utf8 multibyte char.
1623 $str = to_utf8($str);
1625 # allow only $len chars, but don't cut a word if it would fit in $add_len
1626 # if it doesn't fit, cut it if it's still longer than the dots we would add
1627 # remove chopped character entities entirely
1629 # when chopping in the middle, distribute $len into left and right part
1630 # return early if chopping wouldn't make string shorter
1631 if ($where eq 'center') {
1632 return $str if ($len + 5 >= length($str)); # filler is length 5
1635 return $str if ($len + 4 >= length($str)); # filler is length 4
1638 # regexps: ending and beginning with word part up to $add_len
1639 my $endre = qr/.{$len}\w{0,$add_len}/;
1640 my $begre = qr/\w{0,$add_len}.{$len}/;
1642 if ($where eq 'left') {
1643 $str =~ m/^(.*?)($begre)$/;
1644 my ($lead, $body) = ($1, $2);
1645 if (length($lead) > 4) {
1648 return "$lead$body";
1650 } elsif ($where eq 'center') {
1651 $str =~ m/^($endre)(.*)$/;
1652 my ($left, $str) = ($1, $2);
1653 $str =~ m/^(.*?)($begre)$/;
1654 my ($mid, $right) = ($1, $2);
1655 if (length($mid) > 5) {
1658 return "$left$mid$right";
1661 $str =~ m/^($endre)(.*)$/;
1664 if (length($tail) > 4) {
1667 return "$body$tail";
1671 # takes the same arguments as chop_str, but also wraps a <span> around the
1672 # result with a title attribute if it does get chopped. Additionally, the
1673 # string is HTML-escaped.
1674 sub chop_and_escape_str {
1677 my $chopped = chop_str(@_);
1678 if ($chopped eq $str) {
1679 return esc_html($chopped);
1681 $str =~ s/[[:cntrl:]]/?/g;
1682 return $cgi->span({-title=>$str}, esc_html($chopped));
1686 ## ----------------------------------------------------------------------
1687 ## functions returning short strings
1689 # CSS class for given age value (in seconds)
1693 if (!defined $age) {
1695 } elsif ($age < 60*60*2) {
1697 } elsif ($age < 60*60*24*2) {
1704 # convert age in seconds to "nn units ago" string
1709 if ($age > 60*60*24*365*2) {
1710 $age_str = (int $age/60/60/24/365);
1711 $age_str .= " years ago";
1712 } elsif ($age > 60*60*24*(365/12)*2) {
1713 $age_str = int $age/60/60/24/(365/12);
1714 $age_str .= " months ago";
1715 } elsif ($age > 60*60*24*7*2) {
1716 $age_str = int $age/60/60/24/7;
1717 $age_str .= " weeks ago";
1718 } elsif ($age > 60*60*24*2) {
1719 $age_str = int $age/60/60/24;
1720 $age_str .= " days ago";
1721 } elsif ($age > 60*60*2) {
1722 $age_str = int $age/60/60;
1723 $age_str .= " hours ago";
1724 } elsif ($age > 60*2) {
1725 $age_str = int $age/60;
1726 $age_str .= " min ago";
1727 } elsif ($age > 2) {
1728 $age_str = int $age;
1729 $age_str .= " sec ago";
1731 $age_str .= " right now";
1737 S_IFINVALID => 0030000,
1738 S_IFGITLINK => 0160000,
1741 # submodule/subproject, a commit object reference
1745 return (($mode & S_IFMT) == S_IFGITLINK)
1748 # convert file mode in octal to symbolic file mode string
1750 my $mode = oct shift;
1752 if (S_ISGITLINK($mode)) {
1753 return 'm---------';
1754 } elsif (S_ISDIR($mode & S_IFMT)) {
1755 return 'drwxr-xr-x';
1756 } elsif (S_ISLNK($mode)) {
1757 return 'lrwxrwxrwx';
1758 } elsif (S_ISREG($mode)) {
1759 # git cares only about the executable bit
1760 if ($mode & S_IXUSR) {
1761 return '-rwxr-xr-x';
1763 return '-rw-r--r--';
1766 return '----------';
1770 # convert file mode in octal to file type string
1774 if ($mode !~ m/^[0-7]+$/) {
1780 if (S_ISGITLINK($mode)) {
1782 } elsif (S_ISDIR($mode & S_IFMT)) {
1784 } elsif (S_ISLNK($mode)) {
1786 } elsif (S_ISREG($mode)) {
1793 # convert file mode in octal to file type description string
1794 sub file_type_long {
1797 if ($mode !~ m/^[0-7]+$/) {
1803 if (S_ISGITLINK($mode)) {
1805 } elsif (S_ISDIR($mode & S_IFMT)) {
1807 } elsif (S_ISLNK($mode)) {
1809 } elsif (S_ISREG($mode)) {
1810 if ($mode & S_IXUSR) {
1811 return "executable";
1821 ## ----------------------------------------------------------------------
1822 ## functions returning short HTML fragments, or transforming HTML fragments
1823 ## which don't belong to other sections
1825 # format line of commit message.
1826 sub format_log_line_html {
1829 $line = esc_html($line, -nbsp=>1);
1830 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1831 $cgi->a({-href => href(action=>"object", hash=>$1),
1832 -class => "text"}, $1);
1838 # format marker of refs pointing to given object
1840 # the destination action is chosen based on object type and current context:
1841 # - for annotated tags, we choose the tag view unless it's the current view
1842 # already, in which case we go to shortlog view
1843 # - for other refs, we keep the current view if we're in history, shortlog or
1844 # log view, and select shortlog otherwise
1845 sub format_ref_marker {
1846 my ($refs, $id) = @_;
1849 if (defined $refs->{$id}) {
1850 foreach my $ref (@{$refs->{$id}}) {
1851 # this code exploits the fact that non-lightweight tags are the
1852 # only indirect objects, and that they are the only objects for which
1853 # we want to use tag instead of shortlog as action
1854 my ($type, $name) = qw();
1855 my $indirect = ($ref =~ s/\^\{\}$//);
1856 # e.g. tags/v2.6.11 or heads/next
1857 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1866 $class .= " indirect" if $indirect;
1868 my $dest_action = "shortlog";
1871 $dest_action = "tag" unless $action eq "tag";
1872 } elsif ($action =~ /^(history|(short)?log)$/) {
1873 $dest_action = $action;
1877 $dest .= "refs/" unless $ref =~ m!^refs/!;
1880 my $link = $cgi->a({
1882 action=>$dest_action,
1886 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
1892 return ' <span class="refs">'. $markers . '</span>';
1898 # format, perhaps shortened and with markers, title line
1899 sub format_subject_html {
1900 my ($long, $short, $href, $extra) = @_;
1901 $extra = '' unless defined($extra);
1903 if (length($short) < length($long)) {
1904 $long =~ s/[[:cntrl:]]/?/g;
1905 return $cgi->a({-href => $href, -class => "list subject",
1906 -title => to_utf8($long)},
1907 esc_html($short)) . $extra;
1909 return $cgi->a({-href => $href, -class => "list subject"},
1910 esc_html($long)) . $extra;
1914 # Rather than recomputing the url for an email multiple times, we cache it
1915 # after the first hit. This gives a visible benefit in views where the avatar
1916 # for the same email is used repeatedly (e.g. shortlog).
1917 # The cache is shared by all avatar engines (currently gravatar only), which
1918 # are free to use it as preferred. Since only one avatar engine is used for any
1919 # given page, there's no risk for cache conflicts.
1920 our %avatar_cache = ();
1922 # Compute the picon url for a given email, by using the picon search service over at
1923 # http://www.cs.indiana.edu/picons/search.html
1925 my $email = lc shift;
1926 if (!$avatar_cache{$email}) {
1927 my ($user, $domain) = split('@', $email);
1928 $avatar_cache{$email} =
1929 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1931 "users+domains+unknown/up/single";
1933 return $avatar_cache{$email};
1936 # Compute the gravatar url for a given email, if it's not in the cache already.
1937 # Gravatar stores only the part of the URL before the size, since that's the
1938 # one computationally more expensive. This also allows reuse of the cache for
1939 # different sizes (for this particular engine).
1941 my $email = lc shift;
1943 $avatar_cache{$email} ||=
1944 "http://www.gravatar.com/avatar/" .
1945 Digest::MD5::md5_hex($email) . "?s=";
1946 return $avatar_cache{$email} . $size;
1949 # Insert an avatar for the given $email at the given $size if the feature
1951 sub git_get_avatar {
1952 my ($email, %opts) = @_;
1953 my $pre_white = ($opts{-pad_before} ? " " : "");
1954 my $post_white = ($opts{-pad_after} ? " " : "");
1955 $opts{-size} ||= 'default';
1956 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1958 if ($git_avatar eq 'gravatar') {
1959 $url = gravatar_url($email, $size);
1960 } elsif ($git_avatar eq 'picon') {
1961 $url = picon_url($email);
1963 # Other providers can be added by extending the if chain, defining $url
1964 # as needed. If no variant puts something in $url, we assume avatars
1965 # are completely disabled/unavailable.
1968 "<img width=\"$size\" " .
1969 "class=\"avatar\" " .
1970 "src=\"".esc_url($url)."\" " .
1978 sub format_search_author {
1979 my ($author, $searchtype, $displaytext) = @_;
1980 my $have_search = gitweb_check_feature('search');
1984 if ($searchtype eq 'author') {
1985 $performed = "authored";
1986 } elsif ($searchtype eq 'committer') {
1987 $performed = "committed";
1990 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1991 searchtext=>$author,
1992 searchtype=>$searchtype), class=>"list",
1993 title=>"Search for commits $performed by $author"},
1997 return $displaytext;
2001 # format the author name of the given commit with the given tag
2002 # the author name is chopped and escaped according to the other
2003 # optional parameters (see chop_str).
2004 sub format_author_html {
2007 my $author = chop_and_escape_str($co->{'author_name'}, @_);
2008 return "<$tag class=\"author\">" .
2009 format_search_author($co->{'author_name'}, "author",
2010 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2015 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2016 sub format_git_diff_header_line {
2018 my $diffinfo = shift;
2019 my ($from, $to) = @_;
2021 if ($diffinfo->{'nparents'}) {
2023 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2024 if ($to->{'href'}) {
2025 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2026 esc_path($to->{'file'}));
2027 } else { # file was deleted (no href)
2028 $line .= esc_path($to->{'file'});
2032 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2033 if ($from->{'href'}) {
2034 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2035 'a/' . esc_path($from->{'file'}));
2036 } else { # file was added (no href)
2037 $line .= 'a/' . esc_path($from->{'file'});
2040 if ($to->{'href'}) {
2041 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2042 'b/' . esc_path($to->{'file'}));
2043 } else { # file was deleted
2044 $line .= 'b/' . esc_path($to->{'file'});
2048 return "<div class=\"diff header\">$line</div>\n";
2051 # format extended diff header line, before patch itself
2052 sub format_extended_diff_header_line {
2054 my $diffinfo = shift;
2055 my ($from, $to) = @_;
2058 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2059 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2060 esc_path($from->{'file'}));
2062 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2063 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2064 esc_path($to->{'file'}));
2066 # match single <mode>
2067 if ($line =~ m/\s(\d{6})$/) {
2068 $line .= '<span class="info"> (' .
2069 file_type_long($1) .
2073 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2074 # can match only for combined diff
2076 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2077 if ($from->{'href'}[$i]) {
2078 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2080 substr($diffinfo->{'from_id'}[$i],0,7));
2085 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2088 if ($to->{'href'}) {
2089 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2090 substr($diffinfo->{'to_id'},0,7));
2095 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2096 # can match only for ordinary diff
2097 my ($from_link, $to_link);
2098 if ($from->{'href'}) {
2099 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2100 substr($diffinfo->{'from_id'},0,7));
2102 $from_link = '0' x 7;
2104 if ($to->{'href'}) {
2105 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2106 substr($diffinfo->{'to_id'},0,7));
2110 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2111 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2114 return $line . "<br/>\n";
2117 # format from-file/to-file diff header
2118 sub format_diff_from_to_header {
2119 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2124 #assert($line =~ m/^---/) if DEBUG;
2125 # no extra formatting for "^--- /dev/null"
2126 if (! $diffinfo->{'nparents'}) {
2127 # ordinary (single parent) diff
2128 if ($line =~ m!^--- "?a/!) {
2129 if ($from->{'href'}) {
2131 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2132 esc_path($from->{'file'}));
2135 esc_path($from->{'file'});
2138 $result .= qq!<div class="diff from_file">$line</div>\n!;
2141 # combined diff (merge commit)
2142 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2143 if ($from->{'href'}[$i]) {
2145 $cgi->a({-href=>href(action=>"blobdiff",
2146 hash_parent=>$diffinfo->{'from_id'}[$i],
2147 hash_parent_base=>$parents[$i],
2148 file_parent=>$from->{'file'}[$i],
2149 hash=>$diffinfo->{'to_id'},
2151 file_name=>$to->{'file'}),
2153 -title=>"diff" . ($i+1)},
2156 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2157 esc_path($from->{'file'}[$i]));
2159 $line = '--- /dev/null';
2161 $result .= qq!<div class="diff from_file">$line</div>\n!;
2166 #assert($line =~ m/^\+\+\+/) if DEBUG;
2167 # no extra formatting for "^+++ /dev/null"
2168 if ($line =~ m!^\+\+\+ "?b/!) {
2169 if ($to->{'href'}) {
2171 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2172 esc_path($to->{'file'}));
2175 esc_path($to->{'file'});
2178 $result .= qq!<div class="diff to_file">$line</div>\n!;
2183 # create note for patch simplified by combined diff
2184 sub format_diff_cc_simplified {
2185 my ($diffinfo, @parents) = @_;
2188 $result .= "<div class=\"diff header\">" .
2190 if (!is_deleted($diffinfo)) {
2191 $result .= $cgi->a({-href => href(action=>"blob",
2193 hash=>$diffinfo->{'to_id'},
2194 file_name=>$diffinfo->{'to_file'}),
2196 esc_path($diffinfo->{'to_file'}));
2198 $result .= esc_path($diffinfo->{'to_file'});
2200 $result .= "</div>\n" . # class="diff header"
2201 "<div class=\"diff nodifferences\">" .
2203 "</div>\n"; # class="diff nodifferences"
2208 # format patch (diff) line (not to be used for diff headers)
2209 sub format_diff_line {
2211 my ($from, $to) = @_;
2212 my $diff_class = "";
2216 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2218 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2219 if ($line =~ m/^\@{3}/) {
2220 $diff_class = " chunk_header";
2221 } elsif ($line =~ m/^\\/) {
2222 $diff_class = " incomplete";
2223 } elsif ($prefix =~ tr/+/+/) {
2224 $diff_class = " add";
2225 } elsif ($prefix =~ tr/-/-/) {
2226 $diff_class = " rem";
2229 # assume ordinary diff
2230 my $char = substr($line, 0, 1);
2232 $diff_class = " add";
2233 } elsif ($char eq '-') {
2234 $diff_class = " rem";
2235 } elsif ($char eq '@') {
2236 $diff_class = " chunk_header";
2237 } elsif ($char eq "\\") {
2238 $diff_class = " incomplete";
2241 $line = untabify($line);
2242 if ($from && $to && $line =~ m/^\@{2} /) {
2243 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2244 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2246 $from_lines = 0 unless defined $from_lines;
2247 $to_lines = 0 unless defined $to_lines;
2249 if ($from->{'href'}) {
2250 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2251 -class=>"list"}, $from_text);
2253 if ($to->{'href'}) {
2254 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2255 -class=>"list"}, $to_text);
2257 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2258 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2259 return "<div class=\"diff$diff_class\">$line</div>\n";
2260 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2261 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2262 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2264 @from_text = split(' ', $ranges);
2265 for (my $i = 0; $i < @from_text; ++$i) {
2266 ($from_start[$i], $from_nlines[$i]) =
2267 (split(',', substr($from_text[$i], 1)), 0);
2270 $to_text = pop @from_text;
2271 $to_start = pop @from_start;
2272 $to_nlines = pop @from_nlines;
2274 $line = "<span class=\"chunk_info\">$prefix ";
2275 for (my $i = 0; $i < @from_text; ++$i) {
2276 if ($from->{'href'}[$i]) {
2277 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2278 -class=>"list"}, $from_text[$i]);
2280 $line .= $from_text[$i];
2284 if ($to->{'href'}) {
2285 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2286 -class=>"list"}, $to_text);
2290 $line .= " $prefix</span>" .
2291 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2292 return "<div class=\"diff$diff_class\">$line</div>\n";
2294 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
2297 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2298 # linked. Pass the hash of the tree/commit to snapshot.
2299 sub format_snapshot_links {
2301 my $num_fmts = @snapshot_fmts;
2302 if ($num_fmts > 1) {
2303 # A parenthesized list of links bearing format names.
2304 # e.g. "snapshot (_tar.gz_ _zip_)"
2305 return "snapshot (" . join(' ', map
2312 }, $known_snapshot_formats{$_}{'display'})
2313 , @snapshot_fmts) . ")";
2314 } elsif ($num_fmts == 1) {
2315 # A single "snapshot" link whose tooltip bears the format name.
2317 my ($fmt) = @snapshot_fmts;
2323 snapshot_format=>$fmt
2325 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2327 } else { # $num_fmts == 0
2332 ## ......................................................................
2333 ## functions returning values to be passed, perhaps after some
2334 ## transformation, to other functions; e.g. returning arguments to href()
2336 # returns hash to be passed to href to generate gitweb URL
2337 # in -title key it returns description of link
2339 my $format = shift || 'Atom';
2340 my %res = (action => lc($format));
2342 # feed links are possible only for project views
2343 return unless (defined $project);
2344 # some views should link to OPML, or to generic project feed,
2345 # or don't have specific feed yet (so they should use generic)
2346 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2349 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2350 # from tag links; this also makes possible to detect branch links
2351 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2352 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2355 # find log type for feed description (title)
2357 if (defined $file_name) {
2358 $type = "history of $file_name";
2359 $type .= "/" if ($action eq 'tree');
2360 $type .= " on '$branch'" if (defined $branch);
2362 $type = "log of $branch" if (defined $branch);
2365 $res{-title} = $type;
2366 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2367 $res{'file_name'} = $file_name;
2372 ## ----------------------------------------------------------------------
2373 ## git utility subroutines, invoking git commands
2375 # returns path to the core git executable and the --git-dir parameter as list
2377 $number_of_git_cmds++;
2378 return $GIT, '--git-dir='.$git_dir;
2381 # quote the given arguments for passing them to the shell
2382 # quote_command("command", "arg 1", "arg with ' and ! characters")
2383 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2384 # Try to avoid using this function wherever possible.
2387 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2390 # get HEAD ref of given project as hash
2391 sub git_get_head_hash {
2392 return git_get_full_hash(shift, 'HEAD');
2395 sub git_get_full_hash {
2396 return git_get_hash(@_);
2399 sub git_get_short_hash {
2400 return git_get_hash(@_, '--short=7');
2404 my ($project, $hash, @options) = @_;
2405 my $o_git_dir = $git_dir;
2407 $git_dir = "$projectroot/$project";
2408 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2409 '--verify', '-q', @options, $hash) {
2411 chomp $retval if defined $retval;
2414 if (defined $o_git_dir) {
2415 $git_dir = $o_git_dir;
2420 # get type of given object
2424 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2426 close $fd or return;
2431 # repository configuration
2432 our $config_file = '';
2435 # store multiple values for single key as anonymous array reference
2436 # single values stored directly in the hash, not as [ <value> ]
2437 sub hash_set_multi {
2438 my ($hash, $key, $value) = @_;
2440 if (!exists $hash->{$key}) {
2441 $hash->{$key} = $value;
2442 } elsif (!ref $hash->{$key}) {
2443 $hash->{$key} = [ $hash->{$key}, $value ];
2445 push @{$hash->{$key}}, $value;
2449 # return hash of git project configuration
2450 # optionally limited to some section, e.g. 'gitweb'
2451 sub git_parse_project_config {
2452 my $section_regexp = shift;
2457 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2460 while (my $keyval = <$fh>) {
2462 my ($key, $value) = split(/\n/, $keyval, 2);
2464 hash_set_multi(\%config, $key, $value)
2465 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2472 # convert config value to boolean: 'true' or 'false'
2473 # no value, number > 0, 'true' and 'yes' values are true
2474 # rest of values are treated as false (never as error)
2475 sub config_to_bool {
2478 return 1 if !defined $val; # section.key
2480 # strip leading and trailing whitespace
2484 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2485 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2488 # convert config value to simple decimal number
2489 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2490 # to be multiplied by 1024, 1048576, or 1073741824
2494 # strip leading and trailing whitespace
2498 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2500 # unknown unit is treated as 1
2501 return $num * ($unit eq 'g' ? 1073741824 :
2502 $unit eq 'm' ? 1048576 :
2503 $unit eq 'k' ? 1024 : 1);
2508 # convert config value to array reference, if needed
2509 sub config_to_multi {
2512 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2515 sub git_get_project_config {
2516 my ($key, $type) = @_;
2518 return unless defined $git_dir;
2521 return unless ($key);
2522 $key =~ s/^gitweb\.//;
2523 return if ($key =~ m/\W/);
2526 if (defined $type) {
2529 unless ($type eq 'bool' || $type eq 'int');
2533 if (!defined $config_file ||
2534 $config_file ne "$git_dir/config") {
2535 %config = git_parse_project_config('gitweb');
2536 $config_file = "$git_dir/config";
2539 # check if config variable (key) exists
2540 return unless exists $config{"gitweb.$key"};
2543 if (!defined $type) {
2544 return $config{"gitweb.$key"};
2545 } elsif ($type eq 'bool') {
2546 # backward compatibility: 'git config --bool' returns true/false
2547 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2548 } elsif ($type eq 'int') {
2549 return config_to_int($config{"gitweb.$key"});
2551 return $config{"gitweb.$key"};
2554 # get hash of given path at given ref
2555 sub git_get_hash_by_path {
2557 my $path = shift || return undef;
2562 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2563 or die_error(500, "Open git-ls-tree failed");
2565 close $fd or return undef;
2567 if (!defined $line) {
2568 # there is no tree or hash given by $path at $base
2572 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2573 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2574 if (defined $type && $type ne $2) {
2575 # type doesn't match
2581 # get path of entry with given hash at given tree-ish (ref)
2582 # used to get 'from' filename for combined diff (merge commit) for renames
2583 sub git_get_path_by_hash {
2584 my $base = shift || return;
2585 my $hash = shift || return;
2589 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2591 while (my $line = <$fd>) {
2594 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2595 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2596 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2605 ## ......................................................................
2606 ## git utility functions, directly accessing git repository
2608 # get the value of config variable either from file named as the variable
2609 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2610 # configuration variable in the repository config file.
2611 sub git_get_file_or_project_config {
2612 my ($path, $name) = @_;
2614 $git_dir = "$projectroot/$path";
2615 open my $fd, '<', "$git_dir/$name"
2616 or return git_get_project_config($name);
2619 if (defined $conf) {
2625 sub git_get_project_description {
2627 return git_get_file_or_project_config($path, 'description');
2630 sub git_get_project_category {
2632 return git_get_file_or_project_config($path, 'category');
2636 # supported formats:
2637 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2638 # - if its contents is a number, use it as tag weight,
2639 # - otherwise add a tag with weight 1
2640 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2641 # the same value multiple times increases tag weight
2642 # * `gitweb.ctag' multi-valued repo config variable
2643 sub git_get_project_ctags {
2644 my $project = shift;
2647 $git_dir = "$projectroot/$project";
2648 if (opendir my $dh, "$git_dir/ctags") {
2649 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2650 foreach my $tagfile (@files) {
2651 open my $ct, '<', $tagfile
2657 (my $ctag = $tagfile) =~ s#.*/##;
2658 if ($val =~ /^\d+$/) {
2659 $ctags->{$ctag} = $val;
2661 $ctags->{$ctag} = 1;
2666 } elsif (open my $fh, '<', "$git_dir/ctags") {
2667 while (my $line = <$fh>) {
2669 $ctags->{$line}++ if $line;
2674 my $taglist = config_to_multi(git_get_project_config('ctag'));
2675 foreach my $tag (@$taglist) {
2683 # return hash, where keys are content tags ('ctags'),
2684 # and values are sum of weights of given tag in every project
2685 sub git_gather_all_ctags {
2686 my $projects = shift;
2689 foreach my $p (@$projects) {
2690 foreach my $ct (keys %{$p->{'ctags'}}) {
2691 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2698 sub git_populate_project_tagcloud {
2701 # First, merge different-cased tags; tags vote on casing
2703 foreach (keys %$ctags) {
2704 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2705 if (not $ctags_lc{lc $_}->{topcount}
2706 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2707 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2708 $ctags_lc{lc $_}->{topname} = $_;
2713 my $matched = $cgi->param('by_tag');
2714 if (eval { require HTML::TagCloud; 1; }) {
2715 $cloud = HTML::TagCloud->new;
2716 foreach my $ctag (sort keys %ctags_lc) {
2717 # Pad the title with spaces so that the cloud looks
2719 my $title = esc_html($ctags_lc{$ctag}->{topname});
2720 $title =~ s/ / /g;
2721 $title =~ s/^/ /g;
2722 $title =~ s/$/ /g;
2723 if (defined $matched && $matched eq $ctag) {
2724 $title = qq(<span class="match">$title</span>);
2726 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2727 $ctags_lc{$ctag}->{count});
2731 foreach my $ctag (keys %ctags_lc) {
2732 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2733 if (defined $matched && $matched eq $ctag) {
2734 $title = qq(<span class="match">$title</span>);
2736 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2737 $cloud->{$ctag}{ctag} =
2738 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
2744 sub git_show_project_tagcloud {
2745 my ($cloud, $count) = @_;
2746 if (ref $cloud eq 'HTML::TagCloud') {
2747 return $cloud->html_and_css($count);
2749 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2751 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2753 $cloud->{$_}->{'ctag'}
2754 } splice(@tags, 0, $count)) .
2759 sub git_get_project_url_list {
2762 $git_dir = "$projectroot/$path";
2763 open my $fd, '<', "$git_dir/cloneurl"
2764 or return wantarray ?
2765 @{ config_to_multi(git_get_project_config('url')) } :
2766 config_to_multi(git_get_project_config('url'));
2767 my @git_project_url_list = map { chomp; $_ } <$fd>;
2770 return wantarray ? @git_project_url_list : \@git_project_url_list;
2773 sub git_get_projects_list {
2774 my $filter = shift || '';
2777 $filter =~ s/\.git$//;
2779 if (-d $projects_list) {
2780 # search in directory
2781 my $dir = $projects_list;
2782 # remove the trailing "/"
2784 my $pfxlen = length("$projects_list");
2785 my $pfxdepth = ($projects_list =~ tr!/!!);
2786 # when filtering, search only given subdirectory
2793 follow_fast => 1, # follow symbolic links
2794 follow_skip => 2, # ignore duplicates
2795 dangling_symlinks => 0, # ignore dangling symlinks, silently
2798 our $project_maxdepth;
2800 # skip project-list toplevel, if we get it.
2801 return if (m!^[/.]$!);
2802 # only directories can be git repositories
2803 return unless (-d $_);
2804 # don't traverse too deep (Find is super slow on os x)
2805 # $project_maxdepth excludes depth of $projectroot
2806 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2807 $File::Find::prune = 1;
2811 my $path = substr($File::Find::name, $pfxlen + 1);
2812 # we check related file in $projectroot
2813 if (check_export_ok("$projectroot/$path")) {
2814 push @list, { path => $path };
2815 $File::Find::prune = 1;
2820 } elsif (-f $projects_list) {
2821 # read from file(url-encoded):
2822 # 'git%2Fgit.git Linus+Torvalds'
2823 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2824 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2825 open my $fd, '<', $projects_list or return;
2827 while (my $line = <$fd>) {
2829 my ($path, $owner) = split ' ', $line;
2830 $path = unescape($path);
2831 $owner = unescape($owner);
2832 if (!defined $path) {
2835 # if $filter is rpovided, check if $path begins with $filter
2836 if ($filter && $path !~ m!^\Q$filter\E/!) {
2839 if (check_export_ok("$projectroot/$path")) {
2842 owner => to_utf8($owner),
2852 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
2853 # as side effects it sets 'forks' field to list of forks for forked projects
2854 sub filter_forks_from_projects_list {
2855 my $projects = shift;
2857 my %trie; # prefix tree of directories (path components)
2858 # generate trie out of those directories that might contain forks
2859 foreach my $pr (@$projects) {
2860 my $path = $pr->{'path'};
2861 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
2862 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
2863 next unless ($path); # skip '.git' repository: tests, git-instaweb
2864 next unless (-d $path); # containing directory exists
2865 $pr->{'forks'} = []; # there can be 0 or more forks of project
2868 my @dirs = split('/', $path);
2869 # walk the trie, until either runs out of components or out of trie
2871 while (scalar @dirs &&
2872 exists($ref->{$dirs[0]})) {
2873 $ref = $ref->{shift @dirs};
2875 # create rest of trie structure from rest of components
2876 foreach my $dir (@dirs) {
2877 $ref = $ref->{$dir} = {};
2879 # create end marker, store $pr as a data
2880 $ref->{''} = $pr if (!exists $ref->{''});
2883 # filter out forks, by finding shortest prefix match for paths
2886 foreach my $pr (@$projects) {
2890 foreach my $dir (split('/', $pr->{'path'})) {
2891 if (exists $ref->{''}) {
2892 # found [shortest] prefix, is a fork - skip it
2893 push @{$ref->{''}{'forks'}}, $pr;
2896 if (!exists $ref->{$dir}) {
2897 # not in trie, cannot have prefix, not a fork
2898 push @filtered, $pr;
2901 # If the dir is there, we just walk one step down the trie.
2902 $ref = $ref->{$dir};
2904 # we ran out of trie
2905 # (shouldn't happen: it's either no match, or end marker)
2906 push @filtered, $pr;
2912 # note: fill_project_list_info must be run first,
2913 # for 'descr_long' and 'ctags' to be filled
2914 sub search_projects_list {
2915 my ($projlist, %opts) = @_;
2916 my $tagfilter = $opts{'tagfilter'};
2917 my $searchtext = $opts{'searchtext'};
2920 unless ($tagfilter || $searchtext);
2924 foreach my $pr (@$projlist) {
2927 next unless ref($pr->{'ctags'}) eq 'HASH';
2929 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
2934 $pr->{'path'} =~ /$searchtext/ ||
2935 $pr->{'descr_long'} =~ /$searchtext/;
2938 push @projects, $pr;
2944 our $gitweb_project_owner = undef;
2945 sub git_get_project_list_from_file {
2947 return if (defined $gitweb_project_owner);
2949 $gitweb_project_owner = {};
2950 # read from file (url-encoded):
2951 # 'git%2Fgit.git Linus+Torvalds'
2952 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2953 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2954 if (-f $projects_list) {
2955 open(my $fd, '<', $projects_list);
2956 while (my $line = <$fd>) {
2958 my ($pr, $ow) = split ' ', $line;
2959 $pr = unescape($pr);
2960 $ow = unescape($ow);
2961 $gitweb_project_owner->{$pr} = to_utf8($ow);
2967 sub git_get_project_owner {
2968 my $project = shift;
2971 return undef unless $project;
2972 $git_dir = "$projectroot/$project";
2974 if (!defined $gitweb_project_owner) {
2975 git_get_project_list_from_file();
2978 if (exists $gitweb_project_owner->{$project}) {
2979 $owner = $gitweb_project_owner->{$project};
2981 if (!defined $owner){
2982 $owner = git_get_project_config('owner');
2984 if (!defined $owner) {
2985 $owner = get_file_owner("$git_dir");
2991 sub git_get_last_activity {
2995 $git_dir = "$projectroot/$path";
2996 open($fd, "-|", git_cmd(), 'for-each-ref',
2997 '--format=%(committer)',
2998 '--sort=-committerdate',
3000 'refs/heads') or return;
3001 my $most_recent = <$fd>;
3002 close $fd or return;
3003 if (defined $most_recent &&
3004 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
3006 my $age = time - $timestamp;
3007 return ($age, age_string($age));
3009 return (undef, undef);
3012 # Implementation note: when a single remote is wanted, we cannot use 'git
3013 # remote show -n' because that command always work (assuming it's a remote URL
3014 # if it's not defined), and we cannot use 'git remote show' because that would
3015 # try to make a network roundtrip. So the only way to find if that particular
3016 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3017 # and when we find what we want.
3018 sub git_get_remotes_list {
3022 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3024 while (my $remote = <$fd>) {
3026 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3027 next if $wanted and not $remote eq $wanted;
3028 my ($url, $key) = ($1, $2);
3030 $remotes{$remote} ||= { 'heads' => () };
3031 $remotes{$remote}{$key} = $url;
3033 close $fd or return;
3034 return wantarray ? %remotes : \%remotes;
3037 # Takes a hash of remotes as first parameter and fills it by adding the
3038 # available remote heads for each of the indicated remotes.
3039 sub fill_remote_heads {
3040 my $remotes = shift;
3041 my @heads = map { "remotes/$_" } keys %$remotes;
3042 my @remoteheads = git_get_heads_list(undef, @heads);
3043 foreach my $remote (keys %$remotes) {
3044 $remotes->{$remote}{'heads'} = [ grep {
3045 $_->{'name'} =~ s!^$remote/!!
3050 sub git_get_references {
3051 my $type = shift || "";
3053 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3054 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3055 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3056 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3059 while (my $line = <$fd>) {
3061 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3062 if (defined $refs{$1}) {
3063 push @{$refs{$1}}, $2;
3069 close $fd or return;
3073 sub git_get_rev_name_tags {
3074 my $hash = shift || return undef;
3076 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
3078 my $name_rev = <$fd>;
3081 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3084 # catches also '$hash undefined' output
3089 ## ----------------------------------------------------------------------
3090 ## parse to hash functions
3094 my $tz = shift || "-0000";
3097 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3098 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3099 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3100 $date{'hour'} = $hour;
3101 $date{'minute'} = $min;
3102 $date{'mday'} = $mday;
3103 $date{'day'} = $days[$wday];
3104 $date{'month'} = $months[$mon];
3105 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3106 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3107 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3108 $mday, $months[$mon], $hour ,$min;
3109 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3110 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3112 my ($tz_sign, $tz_hour, $tz_min) =
3113 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3114 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3115 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3116 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3117 $date{'hour_local'} = $hour;
3118 $date{'minute_local'} = $min;
3119 $date{'tz_local'} = $tz;
3120 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3121 1900+$year, $mon+1, $mday,
3122 $hour, $min, $sec, $tz);
3131 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
3132 $tag{'id'} = $tag_id;
3133 while (my $line = <$fd>) {
3135 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3136 $tag{'object'} = $1;
3137 } elsif ($line =~ m/^type (.+)$/) {
3139 } elsif ($line =~ m/^tag (.+)$/) {
3141 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3142 $tag{'author'} = $1;
3143 $tag{'author_epoch'} = $2;
3144 $tag{'author_tz'} = $3;
3145 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3146 $tag{'author_name'} = $1;
3147 $tag{'author_email'} = $2;
3149 $tag{'author_name'} = $tag{'author'};
3151 } elsif ($line =~ m/--BEGIN/) {
3152 push @comment, $line;
3154 } elsif ($line eq "") {
3158 push @comment, <$fd>;
3159 $tag{'comment'} = \@comment;
3160 close $fd or return;
3161 if (!defined $tag{'name'}) {
3167 sub parse_commit_text {
3168 my ($commit_text, $withparents) = @_;
3169 my @commit_lines = split '\n', $commit_text;
3172 pop @commit_lines; # Remove '\0'
3174 if (! @commit_lines) {
3178 my $header = shift @commit_lines;
3179 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3182 ($co{'id'}, my @parents) = split ' ', $header;
3183 while (my $line = shift @commit_lines) {
3184 last if $line eq "\n";
3185 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3187 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3189 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3190 $co{'author'} = to_utf8($1);
3191 $co{'author_epoch'} = $2;
3192 $co{'author_tz'} = $3;
3193 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3194 $co{'author_name'} = $1;
3195 $co{'author_email'} = $2;
3197 $co{'author_name'} = $co{'author'};
3199 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3200 $co{'committer'} = to_utf8($1);
3201 $co{'committer_epoch'} = $2;
3202 $co{'committer_tz'} = $3;
3203 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3204 $co{'committer_name'} = $1;
3205 $co{'committer_email'} = $2;
3207 $co{'committer_name'} = $co{'committer'};
3211 if (!defined $co{'tree'}) {
3214 $co{'parents'} = \@parents;
3215 $co{'parent'} = $parents[0];
3217 foreach my $title (@commit_lines) {
3220 $co{'title'} = chop_str($title, 80, 5);
3221 # remove leading stuff of merges to make the interesting part visible
3222 if (length($title) > 50) {
3223 $title =~ s/^Automatic //;
3224 $title =~ s/^merge (of|with) /Merge ... /i;
3225 if (length($title) > 50) {
3226 $title =~ s/(http|rsync):\/\///;
3228 if (length($title) > 50) {
3229 $title =~ s/(master|www|rsync)\.//;
3231 if (length($title) > 50) {
3232 $title =~ s/kernel.org:?//;
3234 if (length($title) > 50) {
3235 $title =~ s/\/pub\/scm//;
3238 $co{'title_short'} = chop_str($title, 50, 5);
3242 if (! defined $co{'title'} || $co{'title'} eq "") {
3243 $co{'title'} = $co{'title_short'} = '(no commit message)';
3245 # remove added spaces
3246 foreach my $line (@commit_lines) {
3249 $co{'comment'} = \@commit_lines;
3251 my $age = time - $co{'committer_epoch'};
3253 $co{'age_string'} = age_string($age);
3254 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3255 if ($age > 60*60*24*7*2) {
3256 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3257 $co{'age_string_age'} = $co{'age_string'};
3259 $co{'age_string_date'} = $co{'age_string'};
3260 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3266 my ($commit_id) = @_;
3271 open my $fd, "-|", git_cmd(), "rev-list",
3277 or die_error(500, "Open git-rev-list failed");
3278 %co = parse_commit_text(<$fd>, 1);
3285 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3293 open my $fd, "-|", git_cmd(), "rev-list",
3296 ("--max-count=" . $maxcount),
3297 ("--skip=" . $skip),
3301 ($filename ? ($filename) : ())
3302 or die_error(500, "Open git-rev-list failed");
3303 while (my $line = <$fd>) {
3304 my %co = parse_commit_text($line);
3309 return wantarray ? @cos : \@cos;
3312 # parse line of git-diff-tree "raw" output
3313 sub parse_difftree_raw_line {
3317 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3318 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3319 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3320 $res{'from_mode'} = $1;
3321 $res{'to_mode'} = $2;
3322 $res{'from_id'} = $3;
3324 $res{'status'} = $5;
3325 $res{'similarity'} = $6;
3326 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3327 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3329 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3332 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3333 # combined diff (for merge commit)
3334 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3335 $res{'nparents'} = length($1);
3336 $res{'from_mode'} = [ split(' ', $2) ];
3337 $res{'to_mode'} = pop @{$res{'from_mode'}};
3338 $res{'from_id'} = [ split(' ', $3) ];
3339 $res{'to_id'} = pop @{$res{'from_id'}};
3340 $res{'status'} = [ split('', $4) ];
3341 $res{'to_file'} = unquote($5);
3343 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3344 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3345 $res{'commit'} = $1;
3348 return wantarray ? %res : \%res;
3351 # wrapper: return parsed line of git-diff-tree "raw" output
3352 # (the argument might be raw line, or parsed info)
3353 sub parsed_difftree_line {
3354 my $line_or_ref = shift;
3356 if (ref($line_or_ref) eq "HASH") {
3357 # pre-parsed (or generated by hand)
3358 return $line_or_ref;
3360 return parse_difftree_raw_line($line_or_ref);
3364 # parse line of git-ls-tree output
3365 sub parse_ls_tree_line {
3371 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3372 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3381 $res{'name'} = unquote($5);
3384 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3385 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3393 $res{'name'} = unquote($4);
3397 return wantarray ? %res : \%res;
3400 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3401 sub parse_from_to_diffinfo {
3402 my ($diffinfo, $from, $to, @parents) = @_;
3404 if ($diffinfo->{'nparents'}) {
3406 $from->{'file'} = [];
3407 $from->{'href'} = [];
3408 fill_from_file_info($diffinfo, @parents)
3409 unless exists $diffinfo->{'from_file'};
3410 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3411 $from->{'file'}[$i] =
3412 defined $diffinfo->{'from_file'}[$i] ?
3413 $diffinfo->{'from_file'}[$i] :
3414 $diffinfo->{'to_file'};
3415 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3416 $from->{'href'}[$i] = href(action=>"blob",
3417 hash_base=>$parents[$i],
3418 hash=>$diffinfo->{'from_id'}[$i],
3419 file_name=>$from->{'file'}[$i]);
3421 $from->{'href'}[$i] = undef;
3425 # ordinary (not combined) diff
3426 $from->{'file'} = $diffinfo->{'from_file'};
3427 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3428 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3429 hash=>$diffinfo->{'from_id'},
3430 file_name=>$from->{'file'});
3432 delete $from->{'href'};
3436 $to->{'file'} = $diffinfo->{'to_file'};
3437 if (!is_deleted($diffinfo)) { # file exists in result
3438 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3439 hash=>$diffinfo->{'to_id'},
3440 file_name=>$to->{'file'});
3442 delete $to->{'href'};
3446 ## ......................................................................
3447 ## parse to array of hashes functions
3449 sub git_get_heads_list {
3450 my ($limit, @classes) = @_;
3451 @classes = ('heads') unless @classes;
3452 my @patterns = map { "refs/$_" } @classes;
3455 open my $fd, '-|', git_cmd(), 'for-each-ref',
3456 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3457 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3460 while (my $line = <$fd>) {
3464 my ($refinfo, $committerinfo) = split(/\0/, $line);
3465 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3466 my ($committer, $epoch, $tz) =
3467 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3468 $ref_item{'fullname'} = $name;
3469 $name =~ s!^refs/(?:head|remote)s/!!;
3471 $ref_item{'name'} = $name;
3472 $ref_item{'id'} = $hash;
3473 $ref_item{'title'} = $title || '(no commit message)';
3474 $ref_item{'epoch'} = $epoch;
3476 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3478 $ref_item{'age'} = "unknown";
3481 push @headslist, \%ref_item;
3485 return wantarray ? @headslist : \@headslist;
3488 sub git_get_tags_list {
3492 open my $fd, '-|', git_cmd(), 'for-each-ref',
3493 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3494 '--format=%(objectname) %(objecttype) %(refname) '.
3495 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3498 while (my $line = <$fd>) {
3502 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3503 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3504 my ($creator, $epoch, $tz) =
3505 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3506 $ref_item{'fullname'} = $name;
3507 $name =~ s!^refs/tags/!!;
3509 $ref_item{'type'} = $type;
3510 $ref_item{'id'} = $id;
3511 $ref_item{'name'} = $name;
3512 if ($type eq "tag") {
3513 $ref_item{'subject'} = $title;
3514 $ref_item{'reftype'} = $reftype;
3515 $ref_item{'refid'} = $refid;
3517 $ref_item{'reftype'} = $type;
3518 $ref_item{'refid'} = $id;
3521 if ($type eq "tag" || $type eq "commit") {
3522 $ref_item{'epoch'} = $epoch;
3524 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3526 $ref_item{'age'} = "unknown";
3530 push @tagslist, \%ref_item;
3534 return wantarray ? @tagslist : \@tagslist;
3537 ## ----------------------------------------------------------------------
3538 ## filesystem-related functions
3540 sub get_file_owner {
3543 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3544 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3545 if (!defined $gcos) {
3549 $owner =~ s/[,;].*$//;
3550 return to_utf8($owner);
3553 # assume that file exists
3555 my $filename = shift;
3557 open my $fd, '<', $filename;
3558 print map { to_utf8($_) } <$fd>;
3562 ## ......................................................................
3563 ## mimetype related functions
3565 sub mimetype_guess_file {
3566 my $filename = shift;
3567 my $mimemap = shift;
3568 -r $mimemap or return undef;
3571 open(my $mh, '<', $mimemap) or return undef;
3573 next if m/^#/; # skip comments
3574 my ($mimetype, $exts) = split(/\t+/);
3575 if (defined $exts) {
3576 my @exts = split(/\s+/, $exts);
3577 foreach my $ext (@exts) {
3578 $mimemap{$ext} = $mimetype;
3584 $filename =~ /\.([^.]*)$/;
3585 return $mimemap{$1};
3588 sub mimetype_guess {
3589 my $filename = shift;
3591 $filename =~ /\./ or return undef;
3593 if ($mimetypes_file) {
3594 my $file = $mimetypes_file;
3595 if ($file !~ m!^/!) { # if it is relative path
3596 # it is relative to project
3597 $file = "$projectroot/$project/$file";
3599 $mime = mimetype_guess_file($filename, $file);
3601 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3607 my $filename = shift;
3610 my $mime = mimetype_guess($filename);
3611 $mime and return $mime;
3615 return $default_blob_plain_mimetype unless $fd;
3618 return 'text/plain';
3619 } elsif (! $filename) {
3620 return 'application/octet-stream';
3621 } elsif ($filename =~ m/\.png$/i) {
3623 } elsif ($filename =~ m/\.gif$/i) {
3625 } elsif ($filename =~ m/\.jpe?g$/i) {
3626 return 'image/jpeg';
3628 return 'application/octet-stream';
3632 sub blob_contenttype {
3633 my ($fd, $file_name, $type) = @_;
3635 $type ||= blob_mimetype($fd, $file_name);
3636 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3637 $type .= "; charset=$default_text_plain_charset";
3643 # guess file syntax for syntax highlighting; return undef if no highlighting
3644 # the name of syntax can (in the future) depend on syntax highlighter used
3645 sub guess_file_syntax {
3646 my ($highlight, $mimetype, $file_name) = @_;
3647 return undef unless ($highlight && defined $file_name);
3648 my $basename = basename($file_name, '.in');
3649 return $highlight_basename{$basename}
3650 if exists $highlight_basename{$basename};
3652 $basename =~ /\.([^.]*)$/;
3653 my $ext = $1 or return undef;
3654 return $highlight_ext{$ext}
3655 if exists $highlight_ext{$ext};
3660 # run highlighter and return FD of its output,
3661 # or return original FD if no highlighting
3662 sub run_highlighter {
3663 my ($fd, $highlight, $syntax) = @_;
3664 return $fd unless ($highlight && defined $syntax);
3667 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3668 quote_command($highlight_bin).
3669 " --replace-tabs=8 --fragment --syntax $syntax |"
3670 or die_error(500, "Couldn't open file or run syntax highlighter");
3674 ## ======================================================================
3675 ## functions printing HTML: header, footer, error page
3677 sub get_page_title {
3678 my $title = to_utf8($site_name);
3680 return $title unless (defined $project);
3681 $title .= " - " . to_utf8($project);
3683 return $title unless (defined $action);
3684 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3686 return $title unless (defined $file_name);
3687 $title .= " - " . esc_path($file_name);
3688 if ($action eq "tree" && $file_name !~ m|/$|) {
3695 sub print_feed_meta {
3696 if (defined $project) {
3697 my %href_params = get_feed_info();
3698 if (!exists $href_params{'-title'}) {
3699 $href_params{'-title'} = 'log';
3702 foreach my $format (qw(RSS Atom)) {
3703 my $type = lc($format);
3705 '-rel' => 'alternate',
3706 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3707 '-type' => "application/$type+xml"
3710 $href_params{'action'} = $type;
3711 $link_attr{'-href'} = href(%href_params);
3713 "rel=\"$link_attr{'-rel'}\" ".
3714 "title=\"$link_attr{'-title'}\" ".
3715 "href=\"$link_attr{'-href'}\" ".
3716 "type=\"$link_attr{'-type'}\" ".
3719 $href_params{'extra_options'} = '--no-merges';
3720 $link_attr{'-href'} = href(%href_params);
3721 $link_attr{'-title'} .= ' (no merges)';
3723 "rel=\"$link_attr{'-rel'}\" ".
3724 "title=\"$link_attr{'-title'}\" ".
3725 "href=\"$link_attr{'-href'}\" ".
3726 "type=\"$link_attr{'-type'}\" ".
3731 printf('<link rel="alternate" title="%s projects list" '.
3732 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3733 esc_attr($site_name), href(project=>undef, action=>"project_index"));
3734 printf('<link rel="alternate" title="%s projects feeds" '.
3735 'href="%s" type="text/x-opml" />'."\n",
3736 esc_attr($site_name), href(project=>undef, action=>"opml"));
3740 sub git_header_html {
3741 my $status = shift || "200 OK";
3742 my $expires = shift;
3745 my $title = get_page_title();
3747 # require explicit support from the UA if we are to send the page as
3748 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3749 # we have to do this because MSIE sometimes globs '*/*', pretending to
3750 # support xhtml+xml but choking when it gets what it asked for.
3751 if (defined $cgi->http('HTTP_ACCEPT') &&
3752 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3753 $cgi->Accept('application/xhtml+xml') != 0) {
3754 $content_type = 'application/xhtml+xml';
3756 $content_type = 'text/html';
3758 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3759 -status=> $status, -expires => $expires)
3760 unless ($opts{'-no_http_header'});
3761 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3763 <?xml version="1.0" encoding="utf-8"?>
3764 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3765 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3766 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3767 <!-- git core binaries version $git_version -->
3769 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3770 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3771 <meta name="robots" content="index, nofollow"/>
3772 <title>$title</title>
3774 # the stylesheet, favicon etc urls won't work correctly with path_info
3775 # unless we set the appropriate base URL
3776 if ($ENV{'PATH_INFO'}) {
3777 print "<base href=\"".esc_url($base_url)."\" />\n";
3779 # print out each stylesheet that exist, providing backwards capability
3780 # for those people who defined $stylesheet in a config file
3781 if (defined $stylesheet) {
3782 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3784 foreach my $stylesheet (@stylesheets) {
3785 next unless $stylesheet;
3786 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3790 if ($status eq '200 OK');
3791 if (defined $favicon) {
3792 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3798 if (defined $site_header && -f $site_header) {
3799 insert_file($site_header);
3802 print "<div class=\"page_header\">\n";
3803 if (defined $logo) {
3804 print $cgi->a({-href => esc_url($logo_url),
3805 -title => $logo_label},
3806 $cgi->img({-src => esc_url($logo),
3807 -width => 72, -height => 27,
3809 -class => "logo"}));
3811 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3812 if (defined $project) {
3813 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3814 if (defined $action) {
3815 my $action_print = $action ;
3816 if (defined $opts{-action_extra}) {
3817 $action_print = $cgi->a({-href => href(action=>$action)},
3820 print " / $action_print";
3822 if (defined $opts{-action_extra}) {
3823 print " / $opts{-action_extra}";
3829 my $have_search = gitweb_check_feature('search');
3830 if (defined $project && $have_search) {
3831 if (!defined $searchtext) {
3835 if (defined $hash_base) {
3836 $search_hash = $hash_base;
3837 } elsif (defined $hash) {
3838 $search_hash = $hash;
3840 $search_hash = "HEAD";
3842 my $action = $my_uri;
3843 my $use_pathinfo = gitweb_check_feature('pathinfo');
3844 if ($use_pathinfo) {
3845 $action .= "/".esc_url($project);
3847 print $cgi->startform(-method => "get", -action => $action) .
3848 "<div class=\"search\">\n" .
3850 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3851 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3852 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3853 $cgi->popup_menu(-name => 'st', -default => 'commit',
3854 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3855 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3857 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3858 "<span title=\"Extended regular expression\">" .
3859 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3860 -checked => $search_use_regexp) .
3863 $cgi->end_form() . "\n";
3867 sub git_footer_html {
3868 my $feed_class = 'rss_logo';
3870 print "<div class=\"page_footer\">\n";
3871 if (defined $project) {
3872 my $descr = git_get_project_description($project);
3873 if (defined $descr) {
3874 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3877 my %href_params = get_feed_info();
3878 if (!%href_params) {
3879 $feed_class .= ' generic';
3881 $href_params{'-title'} ||= 'log';
3883 foreach my $format (qw(RSS Atom)) {
3884 $href_params{'action'} = lc($format);
3885 print $cgi->a({-href => href(%href_params),
3886 -title => "$href_params{'-title'} $format feed",
3887 -class => $feed_class}, $format)."\n";
3891 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3892 -class => $feed_class}, "OPML") . " ";
3893 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3894 -class => $feed_class}, "TXT") . "\n";
3896 print "</div>\n"; # class="page_footer"
3898 if (defined $t0 && gitweb_check_feature('timed')) {
3899 print "<div id=\"generating_info\">\n";
3900 print 'This page took '.
3901 '<span id="generating_time" class="time_span">'.
3902 tv_interval($t0, [ gettimeofday() ]).
3905 '<span id="generating_cmd">'.
3906 $number_of_git_cmds.
3907 '</span> git commands '.
3909 print "</div>\n"; # class="page_footer"
3912 if (defined $site_footer && -f $site_footer) {
3913 insert_file($site_footer);
3916 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
3917 if (defined $action &&
3918 $action eq 'blame_incremental') {
3919 print qq!<script type="text/javascript">\n!.
3920 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3921 qq! "!. href() .qq!");\n!.
3924 my ($jstimezone, $tz_cookie, $datetime_class) =
3925 gitweb_get_feature('javascript-timezone');
3927 print qq!<script type="text/javascript">\n!.
3928 qq!window.onload = function () {\n!;
3929 if (gitweb_check_feature('javascript-actions')) {
3930 print qq! fixLinks();\n!;
3932 if ($jstimezone && $tz_cookie && $datetime_class) {
3933 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
3934 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
3944 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3945 # Example: die_error(404, 'Hash not found')
3946 # By convention, use the following status codes (as defined in RFC 2616):
3947 # 400: Invalid or missing CGI parameters, or
3948 # requested object exists but has wrong type.
3949 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3950 # this server or project.
3951 # 404: Requested object/revision/project doesn't exist.
3952 # 500: The server isn't configured properly, or
3953 # an internal error occurred (e.g. failed assertions caused by bugs), or
3954 # an unknown error occurred (e.g. the git binary died unexpectedly).
3955 # 503: The server is currently unavailable (because it is overloaded,
3956 # or down for maintenance). Generally, this is a temporary state.
3958 my $status = shift || 500;
3959 my $error = esc_html(shift) || "Internal Server Error";
3963 my %http_responses = (
3964 400 => '400 Bad Request',
3965 403 => '403 Forbidden',
3966 404 => '404 Not Found',
3967 500 => '500 Internal Server Error',
3968 503 => '503 Service Unavailable',
3970 git_header_html($http_responses{$status}, undef, %opts);
3972 <div class="page_body">
3977 if (defined $extra) {
3985 unless ($opts{'-error_handler'});
3988 ## ----------------------------------------------------------------------
3989 ## functions printing or outputting HTML: navigation
3991 sub git_print_page_nav {
3992 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3993 $extra = '' if !defined $extra; # pager or formats
3995 my @navs = qw(summary shortlog log commit commitdiff tree);
3997 @navs = grep { $_ ne $suppress } @navs;
4000 my %arg = map { $_ => {action=>$_} } @navs;
4001 if (defined $head) {
4002 for (qw(commit commitdiff)) {
4003 $arg{$_}{'hash'} = $head;
4005 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4006 for (qw(shortlog log)) {
4007 $arg{$_}{'hash'} = $head;
4012 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4013 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4015 my @actions = gitweb_get_feature('actions');
4018 'n' => $project, # project name
4019 'f' => $git_dir, # project path within filesystem
4020 'h' => $treehead || '', # current hash ('h' parameter)
4021 'b' => $treebase || '', # hash base ('hb' parameter)
4024 my ($label, $link, $pos) = splice(@actions,0,3);
4026 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4028 $link =~ s/%([%nfhb])/$repl{$1}/g;
4029 $arg{$label}{'_href'} = $link;
4032 print "<div class=\"page_nav\">\n" .
4034 map { $_ eq $current ?
4035 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
4037 print "<br/>\n$extra<br/>\n" .
4041 # returns a submenu for the nagivation of the refs views (tags, heads,
4042 # remotes) with the current view disabled and the remotes view only
4043 # available if the feature is enabled
4044 sub format_ref_views {
4046 my @ref_views = qw{tags heads};
4047 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4048 return join " | ", map {
4049 $_ eq $current ? $_ :
4050 $cgi->a({-href => href(action=>$_)}, $_)
4054 sub format_paging_nav {
4055 my ($action, $page, $has_next_link) = @_;
4061 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4063 $cgi->a({-href => href(-replay=>1, page=>$page-1),
4064 -accesskey => "p", -title => "Alt-p"}, "prev");
4066 $paging_nav .= "first ⋅ prev";
4069 if ($has_next_link) {
4070 $paging_nav .= " ⋅ " .
4071 $cgi->a({-href => href(-replay=>1, page=>$page+1),
4072 -accesskey => "n", -title => "Alt-n"}, "next");
4074 $paging_nav .= " ⋅ next";
4080 ## ......................................................................
4081 ## functions printing or outputting HTML: div
4083 sub git_print_header_div {
4084 my ($action, $title, $hash, $hash_base) = @_;
4087 $args{'action'} = $action;
4088 $args{'hash'} = $hash if $hash;
4089 $args{'hash_base'} = $hash_base if $hash_base;
4091 print "<div class=\"header\">\n" .
4092 $cgi->a({-href => href(%args), -class => "title"},
4093 $title ? $title : $action) .
4097 sub format_repo_url {
4098 my ($name, $url) = @_;
4099 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4102 # Group output by placing it in a DIV element and adding a header.
4103 # Options for start_div() can be provided by passing a hash reference as the
4104 # first parameter to the function.
4105 # Options to git_print_header_div() can be provided by passing an array
4106 # reference. This must follow the options to start_div if they are present.
4107 # The content can be a scalar, which is output as-is, a scalar reference, which
4108 # is output after html escaping, an IO handle passed either as *handle or
4109 # *handle{IO}, or a function reference. In the latter case all following
4110 # parameters will be taken as argument to the content function call.
4111 sub git_print_section {
4112 my ($div_args, $header_args, $content);
4114 if (ref($arg) eq 'HASH') {
4118 if (ref($arg) eq 'ARRAY') {
4119 $header_args = $arg;
4124 print $cgi->start_div($div_args);
4125 git_print_header_div(@$header_args);
4127 if (ref($content) eq 'CODE') {
4129 } elsif (ref($content) eq 'SCALAR') {
4130 print esc_html($$content);
4131 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4133 } elsif (!ref($content) && defined($content)) {
4137 print $cgi->end_div;
4140 sub format_timestamp_html {
4142 my $strtime = $date->{'rfc2822'};
4144 my (undef, undef, $datetime_class) =
4145 gitweb_get_feature('javascript-timezone');
4146 if ($datetime_class) {
4147 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
4150 my $localtime_format = '(%02d:%02d %s)';
4151 if ($date->{'hour_local'} < 6) {
4152 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4155 sprintf($localtime_format,
4156 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4161 # Outputs the author name and date in long form
4162 sub git_print_authorship {
4165 my $tag = $opts{-tag} || 'div';
4166 my $author = $co->{'author_name'};
4168 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4169 print "<$tag class=\"author_date\">" .
4170 format_search_author($author, "author", esc_html($author)) .
4171 " [".format_timestamp_html(\%ad)."]".
4172 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4176 # Outputs table rows containing the full author or committer information,
4177 # in the format expected for 'commit' view (& similar).
4178 # Parameters are a commit hash reference, followed by the list of people
4179 # to output information for. If the list is empty it defaults to both
4180 # author and committer.
4181 sub git_print_authorship_rows {
4183 # too bad we can't use @people = @_ || ('author', 'committer')
4185 @people = ('author', 'committer') unless @people;
4186 foreach my $who (@people) {
4187 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4188 print "<tr><td>$who</td><td>" .
4189 format_search_author($co->{"${who}_name"}, $who,
4190 esc_html($co->{"${who}_name"})) . " " .
4191 format_search_author($co->{"${who}_email"}, $who,
4192 esc_html("<" . $co->{"${who}_email"} . ">")) .
4193 "</td><td rowspan=\"2\">" .
4194 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4198 format_timestamp_html(\%wd) .
4204 sub git_print_page_path {
4210 print "<div class=\"page_path\">";
4211 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
4212 -title => 'tree root'}, to_utf8("[$project]"));
4214 if (defined $name) {
4215 my @dirname = split '/', $name;
4216 my $basename = pop @dirname;
4219 foreach my $dir (@dirname) {
4220 $fullname .= ($fullname ? '/' : '') . $dir;
4221 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4223 -title => $fullname}, esc_path($dir));
4226 if (defined $type && $type eq 'blob') {
4227 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
4229 -title => $name}, esc_path($basename));
4230 } elsif (defined $type && $type eq 'tree') {
4231 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4233 -title => $name}, esc_path($basename));
4236 print esc_path($basename);
4239 print "<br/></div>\n";
4246 if ($opts{'-remove_title'}) {
4247 # remove title, i.e. first line of log
4250 # remove leading empty lines
4251 while (defined $log->[0] && $log->[0] eq "") {
4258 foreach my $line (@$log) {
4259 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
4262 if (! $opts{'-remove_signoff'}) {
4263 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
4266 # remove signoff lines
4273 # print only one empty line
4274 # do not print empty line after signoff
4276 next if ($empty || $signoff);
4282 print format_log_line_html($line) . "<br/>\n";
4285 if ($opts{'-final_empty_line'}) {
4286 # end with single empty line
4287 print "<br/>\n" unless $empty;
4291 # return link target (what link points to)
4292 sub git_get_link_target {
4297 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4301 $link_target = <$fd>;
4306 return $link_target;
4309 # given link target, and the directory (basedir) the link is in,
4310 # return target of link relative to top directory (top tree);
4311 # return undef if it is not possible (including absolute links).
4312 sub normalize_link_target {
4313 my ($link_target, $basedir) = @_;
4315 # absolute symlinks (beginning with '/') cannot be normalized
4316 return if (substr($link_target, 0, 1) eq '/');
4318 # normalize link target to path from top (root) tree (dir)
4321 $path = $basedir . '/' . $link_target;
4323 # we are in top (root) tree (dir)
4324 $path = $link_target;
4327 # remove //, /./, and /../
4329 foreach my $part (split('/', $path)) {
4330 # discard '.' and ''
4331 next if (!$part || $part eq '.');
4333 if ($part eq '..') {
4337 # link leads outside repository (outside top dir)
4341 push @path_parts, $part;
4344 $path = join('/', @path_parts);
4349 # print tree entry (row of git_tree), but without encompassing <tr> element
4350 sub git_print_tree_entry {
4351 my ($t, $basedir, $hash_base, $have_blame) = @_;
4354 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4356 # The format of a table row is: mode list link. Where mode is
4357 # the mode of the entry, list is the name of the entry, an href,
4358 # and link is the action links of the entry.
4360 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
4361 if (exists $t->{'size'}) {
4362 print "<td class=\"size\">$t->{'size'}</td>\n";
4364 if ($t->{'type'} eq "blob") {
4365 print "<td class=\"list\">" .
4366 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4367 file_name=>"$basedir$t->{'name'}", %base_key),
4368 -class => "list"}, esc_path($t->{'name'}));
4369 if (S_ISLNK(oct $t->{'mode'})) {
4370 my $link_target = git_get_link_target($t->{'hash'});
4372 my $norm_target = normalize_link_target($link_target, $basedir);
4373 if (defined $norm_target) {
4375 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4376 file_name=>$norm_target),
4377 -title => $norm_target}, esc_path($link_target));
4379 print " -> " . esc_path($link_target);
4384 print "<td class=\"link\">";
4385 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4386 file_name=>"$basedir$t->{'name'}", %base_key)},
4390 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4391 file_name=>"$basedir$t->{'name'}", %base_key)},
4394 if (defined $hash_base) {
4396 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4397 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4401 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4402 file_name=>"$basedir$t->{'name'}")},
4406 } elsif ($t->{'type'} eq "tree") {
4407 print "<td class=\"list\">";
4408 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4409 file_name=>"$basedir$t->{'name'}",
4411 esc_path($t->{'name'}));
4413 print "<td class=\"link\">";
4414 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4415 file_name=>"$basedir$t->{'name'}",
4418 if (defined $hash_base) {
4420 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4421 file_name=>"$basedir$t->{'name'}")},
4426 # unknown object: we can only present history for it
4427 # (this includes 'commit' object, i.e. submodule support)
4428 print "<td class=\"list\">" .
4429 esc_path($t->{'name'}) .
4431 print "<td class=\"link\">";
4432 if (defined $hash_base) {
4433 print $cgi->a({-href => href(action=>"history",
4434 hash_base=>$hash_base,
4435 file_name=>"$basedir$t->{'name'}")},
4442 ## ......................................................................
4443 ## functions printing large fragments of HTML
4445 # get pre-image filenames for merge (combined) diff
4446 sub fill_from_file_info {
4447 my ($diff, @parents) = @_;
4449 $diff->{'from_file'} = [ ];
4450 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4451 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4452 if ($diff->{'status'}[$i] eq 'R' ||
4453 $diff->{'status'}[$i] eq 'C') {
4454 $diff->{'from_file'}[$i] =
4455 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4462 # is current raw difftree line of file deletion
4464 my $diffinfo = shift;
4466 return $diffinfo->{'to_id'} eq ('0' x 40);
4469 # does patch correspond to [previous] difftree raw line
4470 # $diffinfo - hashref of parsed raw diff format
4471 # $patchinfo - hashref of parsed patch diff format
4472 # (the same keys as in $diffinfo)
4473 sub is_patch_split {
4474 my ($diffinfo, $patchinfo) = @_;
4476 return defined $diffinfo && defined $patchinfo
4477 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4481 sub git_difftree_body {
4482 my ($difftree, $hash, @parents) = @_;
4483 my ($parent) = $parents[0];
4484 my $have_blame = gitweb_check_feature('blame');
4485 print "<div class=\"list_head\">\n";
4486 if ($#{$difftree} > 10) {
4487 print(($#{$difftree} + 1) . " files changed:\n");
4491 print "<table class=\"" .
4492 (@parents > 1 ? "combined " : "") .
4495 # header only for combined diff in 'commitdiff' view
4496 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4499 print "<thead><tr>\n" .
4500 "<th></th><th></th>\n"; # filename, patchN link
4501 for (my $i = 0; $i < @parents; $i++) {
4502 my $par = $parents[$i];
4504 $cgi->a({-href => href(action=>"commitdiff",
4505 hash=>$hash, hash_parent=>$par),
4506 -title => 'commitdiff to parent number ' .
4507 ($i+1) . ': ' . substr($par,0,7)},
4511 print "</tr></thead>\n<tbody>\n";
4516 foreach my $line (@{$difftree}) {
4517 my $diff = parsed_difftree_line($line);
4520 print "<tr class=\"dark\">\n";
4522 print "<tr class=\"light\">\n";
4526 if (exists $diff->{'nparents'}) { # combined diff
4528 fill_from_file_info($diff, @parents)
4529 unless exists $diff->{'from_file'};
4531 if (!is_deleted($diff)) {
4532 # file exists in the result (child) commit
4534 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4535 file_name=>$diff->{'to_file'},
4537 -class => "list"}, esc_path($diff->{'to_file'})) .
4541 esc_path($diff->{'to_file'}) .
4545 if ($action eq 'commitdiff') {
4548 print "<td class=\"link\">" .
4549 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4555 my $has_history = 0;
4556 my $not_deleted = 0;
4557 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4558 my $hash_parent = $parents[$i];
4559 my $from_hash = $diff->{'from_id'}[$i];
4560 my $from_path = $diff->{'from_file'}[$i];
4561 my $status = $diff->{'status'}[$i];
4563 $has_history ||= ($status ne 'A');
4564 $not_deleted ||= ($status ne 'D');
4566 if ($status eq 'A') {
4567 print "<td class=\"link\" align=\"right\"> | </td>\n";
4568 } elsif ($status eq 'D') {
4569 print "<td class=\"link\">" .
4570 $cgi->a({-href => href(action=>"blob",
4573 file_name=>$from_path)},
4577 if ($diff->{'to_id'} eq $from_hash) {
4578 print "<td class=\"link nochange\">";
4580 print "<td class=\"link\">";
4582 print $cgi->a({-href => href(action=>"blobdiff",
4583 hash=>$diff->{'to_id'},
4584 hash_parent=>$from_hash,
4586 hash_parent_base=>$hash_parent,
4587 file_name=>$diff->{'to_file'},
4588 file_parent=>$from_path)},
4594 print "<td class=\"link\">";
4596 print $cgi->a({-href => href(action=>"blob",
4597 hash=>$diff->{'to_id'},
4598 file_name=>$diff->{'to_file'},
4601 print " | " if ($has_history);
4604 print $cgi->a({-href => href(action=>"history",
4605 file_name=>$diff->{'to_file'},
4612 next; # instead of 'else' clause, to avoid extra indent
4614 # else ordinary diff
4616 my ($to_mode_oct, $to_mode_str, $to_file_type);
4617 my ($from_mode_oct, $from_mode_str, $from_file_type);
4618 if ($diff->{'to_mode'} ne ('0' x 6)) {
4619 $to_mode_oct = oct $diff->{'to_mode'};
4620 if (S_ISREG($to_mode_oct)) { # only for regular file
4621 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4623 $to_file_type = file_type($diff->{'to_mode'});
4625 if ($diff->{'from_mode'} ne ('0' x 6)) {
4626 $from_mode_oct = oct $diff->{'from_mode'};
4627 if (S_ISREG($from_mode_oct)) { # only for regular file
4628 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4630 $from_file_type = file_type($diff->{'from_mode'});
4633 if ($diff->{'status'} eq "A") { # created
4634 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4635 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4636 $mode_chng .= "]</span>";
4638 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4639 hash_base=>$hash, file_name=>$diff->{'file'}),
4640 -class => "list"}, esc_path($diff->{'file'}));
4642 print "<td>$mode_chng</td>\n";
4643 print "<td class=\"link\">";
4644 if ($action eq 'commitdiff') {
4647 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4651 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4652 hash_base=>$hash, file_name=>$diff->{'file'})},
4656 } elsif ($diff->{'status'} eq "D") { # deleted
4657 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4659 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4660 hash_base=>$parent, file_name=>$diff->{'file'}),
4661 -class => "list"}, esc_path($diff->{'file'}));
4663 print "<td>$mode_chng</td>\n";
4664 print "<td class=\"link\">";
4665 if ($action eq 'commitdiff') {
4668 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4672 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4673 hash_base=>$parent, file_name=>$diff->{'file'})},
4676 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4677 file_name=>$diff->{'file'})},
4680 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4681 file_name=>$diff->{'file'})},
4685 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4686 my $mode_chnge = "";
4687 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4688 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4689 if ($from_file_type ne $to_file_type) {
4690 $mode_chnge .= " from $from_file_type to $to_file_type";
4692 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4693 if ($from_mode_str && $to_mode_str) {
4694 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4695 } elsif ($to_mode_str) {
4696 $mode_chnge .= " mode: $to_mode_str";
4699 $mode_chnge .= "]</span>\n";
4702 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4703 hash_base=>$hash, file_name=>$diff->{'file'}),
4704 -class => "list"}, esc_path($diff->{'file'}));
4706 print "<td>$mode_chnge</td>\n";
4707 print "<td class=\"link\">";
4708 if ($action eq 'commitdiff') {
4711 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4714 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4715 # "commit" view and modified file (not onlu mode changed)
4716 print $cgi->a({-href => href(action=>"blobdiff",
4717 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4718 hash_base=>$hash, hash_parent_base=>$parent,
4719 file_name=>$diff->{'file'})},
4723 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4724 hash_base=>$hash, file_name=>$diff->{'file'})},
4727 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4728 file_name=>$diff->{'file'})},
4731 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4732 file_name=>$diff->{'file'})},
4736 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4737 my %status_name = ('R' => 'moved', 'C' => 'copied');
4738 my $nstatus = $status_name{$diff->{'status'}};
4740 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4741 # mode also for directories, so we cannot use $to_mode_str
4742 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4745 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4746 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4747 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4748 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4749 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4750 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4751 -class => "list"}, esc_path($diff->{'from_file'})) .
4752 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4753 "<td class=\"link\">";
4754 if ($action eq 'commitdiff') {
4757 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4760 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4761 # "commit" view and modified file (not only pure rename or copy)
4762 print $cgi->a({-href => href(action=>"blobdiff",
4763 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4764 hash_base=>$hash, hash_parent_base=>$parent,
4765 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4769 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4770 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4773 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4774 file_name=>$diff->{'to_file'})},
4777 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4778 file_name=>$diff->{'to_file'})},
4782 } # we should not encounter Unmerged (U) or Unknown (X) status
4785 print "</tbody>" if $has_header;
4789 sub git_patchset_body {
4790 my ($fd, $difftree, $hash, @hash_parents) = @_;
4791 my ($hash_parent) = $hash_parents[0];
4793 my $is_combined = (@hash_parents > 1);
4795 my $patch_number = 0;
4801 print "<div class=\"patchset\">\n";
4803 # skip to first patch
4804 while ($patch_line = <$fd>) {
4807 last if ($patch_line =~ m/^diff /);
4811 while ($patch_line) {
4813 # parse "git diff" header line
4814 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4815 # $1 is from_name, which we do not use
4816 $to_name = unquote($2);
4817 $to_name =~ s!^b/!!;
4818 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4819 # $1 is 'cc' or 'combined', which we do not use
4820 $to_name = unquote($2);
4825 # check if current patch belong to current raw line
4826 # and parse raw git-diff line if needed
4827 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4828 # this is continuation of a split patch
4829 print "<div class=\"patch cont\">\n";
4831 # advance raw git-diff output if needed
4832 $patch_idx++ if defined $diffinfo;
4834 # read and prepare patch information
4835 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4837 # compact combined diff output can have some patches skipped
4838 # find which patch (using pathname of result) we are at now;
4840 while ($to_name ne $diffinfo->{'to_file'}) {
4841 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4842 format_diff_cc_simplified($diffinfo, @hash_parents) .
4843 "</div>\n"; # class="patch"
4848 last if $patch_idx > $#$difftree;
4849 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4853 # modifies %from, %to hashes
4854 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4856 # this is first patch for raw difftree line with $patch_idx index
4857 # we index @$difftree array from 0, but number patches from 1
4858 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4862 #assert($patch_line =~ m/^diff /) if DEBUG;
4863 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4865 # print "git diff" header
4866 print format_git_diff_header_line($patch_line, $diffinfo,
4869 # print extended diff header
4870 print "<div class=\"diff extended_header\">\n";
4872 while ($patch_line = <$fd>) {
4875 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4877 print format_extended_diff_header_line($patch_line, $diffinfo,
4880 print "</div>\n"; # class="diff extended_header"
4882 # from-file/to-file diff header
4883 if (! $patch_line) {
4884 print "</div>\n"; # class="patch"
4887 next PATCH if ($patch_line =~ m/^diff /);
4888 #assert($patch_line =~ m/^---/) if DEBUG;
4890 my $last_patch_line = $patch_line;
4891 $patch_line = <$fd>;
4893 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4895 print format_diff_from_to_header($last_patch_line, $patch_line,
4896 $diffinfo, \%from, \%to,
4901 while ($patch_line = <$fd>) {
4904 next PATCH if ($patch_line =~ m/^diff /);
4906 print format_diff_line($patch_line, \%from, \%to);
4910 print "</div>\n"; # class="patch"
4913 # for compact combined (--cc) format, with chunk and patch simplification
4914 # the patchset might be empty, but there might be unprocessed raw lines
4915 for (++$patch_idx if $patch_number > 0;
4916 $patch_idx < @$difftree;
4918 # read and prepare patch information
4919 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4921 # generate anchor for "patch" links in difftree / whatchanged part
4922 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4923 format_diff_cc_simplified($diffinfo, @hash_parents) .
4924 "</div>\n"; # class="patch"
4929 if ($patch_number == 0) {
4930 if (@hash_parents > 1) {
4931 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4933 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4937 print "</div>\n"; # class="patchset"
4940 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4942 # fills project list info (age, description, owner, category, forks)
4943 # for each project in the list, removing invalid projects from
4945 # NOTE: modifies $projlist, but does not remove entries from it
4946 sub fill_project_list_info {
4947 my $projlist = shift;
4950 my $show_ctags = gitweb_check_feature('ctags');
4952 foreach my $pr (@$projlist) {
4953 my (@activity) = git_get_last_activity($pr->{'path'});
4954 unless (@activity) {
4957 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4958 if (!defined $pr->{'descr'}) {
4959 my $descr = git_get_project_description($pr->{'path'}) || "";
4960 $descr = to_utf8($descr);
4961 $pr->{'descr_long'} = $descr;
4962 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4964 if (!defined $pr->{'owner'}) {
4965 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4968 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4970 if ($projects_list_group_categories && !defined $pr->{'category'}) {
4971 my $cat = git_get_project_category($pr->{'path'}) ||
4972 $project_list_default_category;
4973 $pr->{'category'} = to_utf8($cat);
4976 push @projects, $pr;
4982 sub sort_projects_list {
4983 my ($projlist, $order) = @_;
4987 project => { key => 'path', type => 'str' },
4988 descr => { key => 'descr_long', type => 'str' },
4989 owner => { key => 'owner', type => 'str' },
4990 age => { key => 'age', type => 'num' }
4992 my $oi = $order_info{$order};
4993 return @$projlist unless defined $oi;
4994 if ($oi->{'type'} eq 'str') {
4995 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @$projlist;
4997 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @$projlist;
5003 # returns a hash of categories, containing the list of project
5004 # belonging to each category
5005 sub build_projlist_by_category {
5006 my ($projlist, $from, $to) = @_;
5009 $from = 0 unless defined $from;
5010 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5012 for (my $i = $from; $i <= $to; $i++) {
5013 my $pr = $projlist->[$i];
5014 push @{$categories{ $pr->{'category'} }}, $pr;
5017 return wantarray ? %categories : \%categories;
5020 # print 'sort by' <th> element, generating 'sort by $name' replay link
5021 # if that order is not selected
5023 print format_sort_th(@_);
5026 sub format_sort_th {
5027 my ($name, $order, $header) = @_;
5029 $header ||= ucfirst($name);
5031 if ($order eq $name) {
5032 $sort_th .= "<th>$header</th>\n";
5034 $sort_th .= "<th>" .
5035 $cgi->a({-href => href(-replay=>1, order=>$name),
5036 -class => "header"}, $header) .
5043 sub git_project_list_rows {
5044 my ($projlist, $from, $to, $check_forks) = @_;
5046 $from = 0 unless defined $from;
5047 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5050 for (my $i = $from; $i <= $to; $i++) {
5051 my $pr = $projlist->[$i];
5054 print "<tr class=\"dark\">\n";
5056 print "<tr class=\"light\">\n";
5062 if ($pr->{'forks'}) {
5063 my $nforks = scalar @{$pr->{'forks'}};
5065 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5066 -title => "$nforks forks"}, "+");
5068 print $cgi->span({-title => "$nforks forks"}, "+");
5073 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5074 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
5075 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5076 -class => "list", -title => $pr->{'descr_long'}},
5077 esc_html($pr->{'descr'})) . "</td>\n" .
5078 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5079 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5080 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
5081 "<td class=\"link\">" .
5082 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5083 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5084 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5085 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5086 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5092 sub git_project_list_body {
5093 # actually uses global variable $project
5094 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5095 my @projects = @$projlist;
5097 my $check_forks = gitweb_check_feature('forks');
5098 my $show_ctags = gitweb_check_feature('ctags');
5099 my $tagfilter = $show_ctags ? $cgi->param('by_tag') : undef;
5100 $check_forks = undef
5101 if ($tagfilter || $searchtext);
5103 # filtering out forks before filling info allows to do less work
5104 @projects = filter_forks_from_projects_list(\@projects)
5106 @projects = fill_project_list_info(\@projects);
5107 # searching projects require filling to be run before it
5108 @projects = search_projects_list(\@projects,
5109 'searchtext' => $searchtext,
5110 'tagfilter' => $tagfilter)
5111 if ($tagfilter || $searchtext);
5113 $order ||= $default_projects_order;
5114 $from = 0 unless defined $from;
5115 $to = $#projects if (!defined $to || $#projects < $to);
5120 "<b>No such projects found</b><br />\n".
5121 "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5122 "</center>\n<br />\n";
5126 @projects = sort_projects_list(\@projects, $order);
5129 my $ctags = git_gather_all_ctags(\@projects);
5130 my $cloud = git_populate_project_tagcloud($ctags);
5131 print git_show_project_tagcloud($cloud, 64);
5134 print "<table class=\"project_list\">\n";
5135 unless ($no_header) {
5138 print "<th></th>\n";
5140 print_sort_th('project', $order, 'Project');
5141 print_sort_th('descr', $order, 'Description');
5142 print_sort_th('owner', $order, 'Owner');
5143 print_sort_th('age', $order, 'Last Change');
5144 print "<th></th>\n" . # for links
5148 if ($projects_list_group_categories) {
5149 # only display categories with projects in the $from-$to window
5150 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5151 my %categories = build_projlist_by_category(\@projects, $from, $to);
5152 foreach my $cat (sort keys %categories) {
5153 unless ($cat eq "") {
5156 print "<td></td>\n";
5158 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5162 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
5165 git_project_list_rows(\@projects, $from, $to, $check_forks);
5168 if (defined $extra) {
5171 print "<td></td>\n";
5173 print "<td colspan=\"5\">$extra</td>\n" .
5180 # uses global variable $project
5181 my ($commitlist, $from, $to, $refs, $extra) = @_;
5183 $from = 0 unless defined $from;
5184 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5186 for (my $i = 0; $i <= $to; $i++) {
5187 my %co = %{$commitlist->[$i]};
5189 my $commit = $co{'id'};
5190 my $ref = format_ref_marker($refs, $commit);
5191 git_print_header_div('commit',
5192 "<span class=\"age\">$co{'age_string'}</span>" .
5193 esc_html($co{'title'}) . $ref,
5195 print "<div class=\"title_text\">\n" .
5196 "<div class=\"log_link\">\n" .
5197 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5199 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5201 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5204 git_print_authorship(\%co, -tag => 'span');
5205 print "<br/>\n</div>\n";
5207 print "<div class=\"log_body\">\n";
5208 git_print_log($co{'comment'}, -final_empty_line=> 1);
5212 print "<div class=\"page_nav\">\n";
5218 sub git_shortlog_body {
5219 # uses global variable $project
5220 my ($commitlist, $from, $to, $refs, $extra) = @_;
5222 $from = 0 unless defined $from;
5223 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5225 print "<table class=\"shortlog\">\n";
5227 for (my $i = $from; $i <= $to; $i++) {
5228 my %co = %{$commitlist->[$i]};
5229 my $commit = $co{'id'};
5230 my $ref = format_ref_marker($refs, $commit);
5232 print "<tr class=\"dark\">\n";
5234 print "<tr class=\"light\">\n";
5237 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5238 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5239 format_author_html('td', \%co, 10) . "<td>";
5240 print format_subject_html($co{'title'}, $co{'title_short'},
5241 href(action=>"commit", hash=>$commit), $ref);
5243 "<td class=\"link\">" .
5244 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
5245 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
5246 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
5247 my $snapshot_links = format_snapshot_links($commit);
5248 if (defined $snapshot_links) {
5249 print " | " . $snapshot_links;
5254 if (defined $extra) {
5256 "<td colspan=\"4\">$extra</td>\n" .
5262 sub git_history_body {
5263 # Warning: assumes constant type (blob or tree) during history
5264 my ($commitlist, $from, $to, $refs, $extra,
5265 $file_name, $file_hash, $ftype) = @_;
5267 $from = 0 unless defined $from;
5268 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5270 print "<table class=\"history\">\n";
5272 for (my $i = $from; $i <= $to; $i++) {
5273 my %co = %{$commitlist->[$i]};
5277 my $commit = $co{'id'};
5279 my $ref = format_ref_marker($refs, $commit);
5282 print "<tr class=\"dark\">\n";
5284 print "<tr class=\"light\">\n";
5287 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5288 # shortlog: format_author_html('td', \%co, 10)
5289 format_author_html('td', \%co, 15, 3) . "<td>";
5290 # originally git_history used chop_str($co{'title'}, 50)
5291 print format_subject_html($co{'title'}, $co{'title_short'},
5292 href(action=>"commit", hash=>$commit), $ref);
5294 "<td class=\"link\">" .
5295 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5296 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
5298 if ($ftype eq 'blob') {
5299 my $blob_current = $file_hash;
5300 my $blob_parent = git_get_hash_by_path($commit, $file_name);
5301 if (defined $blob_current && defined $blob_parent &&
5302 $blob_current ne $blob_parent) {
5304 $cgi->a({-href => href(action=>"blobdiff",
5305 hash=>$blob_current, hash_parent=>$blob_parent,
5306 hash_base=>$hash_base, hash_parent_base=>$commit,
5307 file_name=>$file_name)},
5314 if (defined $extra) {
5316 "<td colspan=\"4\">$extra</td>\n" .
5323 # uses global variable $project
5324 my ($taglist, $from, $to, $extra) = @_;
5325 $from = 0 unless defined $from;
5326 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5328 print "<table class=\"tags\">\n";
5330 for (my $i = $from; $i <= $to; $i++) {
5331 my $entry = $taglist->[$i];
5333 my $comment = $tag{'subject'};
5335 if (defined $comment) {
5336 $comment_short = chop_str($comment, 30, 5);
5339 print "<tr class=\"dark\">\n";
5341 print "<tr class=\"light\">\n";
5344 if (defined $tag{'age'}) {
5345 print "<td><i>$tag{'age'}</i></td>\n";
5347 print "<td></td>\n";
5350 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
5351 -class => "list name"}, esc_html($tag{'name'})) .
5354 if (defined $comment) {
5355 print format_subject_html($comment, $comment_short,
5356 href(action=>"tag", hash=>$tag{'id'}));
5359 "<td class=\"selflink\">";
5360 if ($tag{'type'} eq "tag") {
5361 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
5366 "<td class=\"link\">" . " | " .
5367 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
5368 if ($tag{'reftype'} eq "commit") {
5369 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
5370 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
5371 } elsif ($tag{'reftype'} eq "blob") {
5372 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
5377 if (defined $extra) {
5379 "<td colspan=\"5\">$extra</td>\n" .
5385 sub git_heads_body {
5386 # uses global variable $project
5387 my ($headlist, $head, $from, $to, $extra) = @_;
5388 $from = 0 unless defined $from;
5389 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5391 print "<table class=\"heads\">\n";
5393 for (my $i = $from; $i <= $to; $i++) {
5394 my $entry = $headlist->[$i];
5396 my $curr = $ref{'id'} eq $head;
5398 print "<tr class=\"dark\">\n";
5400 print "<tr class=\"light\">\n";
5403 print "<td><i>$ref{'age'}</i></td>\n" .
5404 ($curr ? "<td class=\"current_head\">" : "<td>") .
5405 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
5406 -class => "list name"},esc_html($ref{'name'})) .
5408 "<td class=\"link\">" .
5409 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
5410 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
5411 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
5415 if (defined $extra) {
5417 "<td colspan=\"3\">$extra</td>\n" .
5423 # Display a single remote block
5424 sub git_remote_block {
5425 my ($remote, $rdata, $limit, $head) = @_;
5427 my $heads = $rdata->{'heads'};
5428 my $fetch = $rdata->{'fetch'};
5429 my $push = $rdata->{'push'};
5431 my $urls_table = "<table class=\"projects_list\">\n" ;
5433 if (defined $fetch) {
5434 if ($fetch eq $push) {
5435 $urls_table .= format_repo_url("URL", $fetch);
5437 $urls_table .= format_repo_url("Fetch URL", $fetch);
5438 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
5440 } elsif (defined $push) {
5441 $urls_table .= format_repo_url("Push URL", $push);
5443 $urls_table .= format_repo_url("", "No remote URL");
5446 $urls_table .= "</table>\n";
5449 if (defined $limit && $limit < @$heads) {
5450 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
5454 git_heads_body($heads, $head, 0, $limit, $dots);
5457 # Display a list of remote names with the respective fetch and push URLs
5458 sub git_remotes_list {
5459 my ($remotedata, $limit) = @_;
5460 print "<table class=\"heads\">\n";
5462 my @remotes = sort keys %$remotedata;
5464 my $limited = $limit && $limit < @remotes;
5466 $#remotes = $limit - 1 if $limited;
5468 while (my $remote = shift @remotes) {
5469 my $rdata = $remotedata->{$remote};
5470 my $fetch = $rdata->{'fetch'};
5471 my $push = $rdata->{'push'};
5473 print "<tr class=\"dark\">\n";
5475 print "<tr class=\"light\">\n";
5479 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
5480 -class=> "list name"},esc_html($remote)) .
5482 print "<td class=\"link\">" .
5483 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
5485 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
5493 "<td colspan=\"3\">" .
5494 $cgi->a({-href => href(action=>"remotes")}, "...") .
5495 "</td>\n" . "</tr>\n";
5501 # Display remote heads grouped by remote, unless there are too many
5502 # remotes, in which case we only display the remote names
5503 sub git_remotes_body {
5504 my ($remotedata, $limit, $head) = @_;
5505 if ($limit and $limit < keys %$remotedata) {
5506 git_remotes_list($remotedata, $limit);
5508 fill_remote_heads($remotedata);
5509 while (my ($remote, $rdata) = each %$remotedata) {
5510 git_print_section({-class=>"remote", -id=>$remote},
5511 ["remotes", $remote, $remote], sub {
5512 git_remote_block($remote, $rdata, $limit, $head);
5518 sub git_search_grep_body {
5519 my ($commitlist, $from, $to, $extra) = @_;
5520 $from = 0 unless defined $from;
5521 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5523 print "<table class=\"commit_search\">\n";
5525 for (my $i = $from; $i <= $to; $i++) {
5526 my %co = %{$commitlist->[$i]};
5530 my $commit = $co{'id'};
5532 print "<tr class=\"dark\">\n";
5534 print "<tr class=\"light\">\n";
5537 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5538 format_author_html('td', \%co, 15, 5) .
5540 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5541 -class => "list subject"},
5542 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5543 my $comment = $co{'comment'};
5544 foreach my $line (@$comment) {
5545 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5546 my ($lead, $match, $trail) = ($1, $2, $3);
5547 $match = chop_str($match, 70, 5, 'center');
5548 my $contextlen = int((80 - length($match))/2);
5549 $contextlen = 30 if ($contextlen > 30);
5550 $lead = chop_str($lead, $contextlen, 10, 'left');
5551 $trail = chop_str($trail, $contextlen, 10, 'right');
5553 $lead = esc_html($lead);
5554 $match = esc_html($match);
5555 $trail = esc_html($trail);
5557 print "$lead<span class=\"match\">$match</span>$trail<br />";
5561 "<td class=\"link\">" .
5562 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5564 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
5566 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5570 if (defined $extra) {
5572 "<td colspan=\"3\">$extra</td>\n" .
5578 ## ======================================================================
5579 ## ======================================================================
5582 sub git_project_list {
5583 my $order = $input_params{'order'};
5584 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5585 die_error(400, "Unknown order parameter");
5588 my @list = git_get_projects_list();
5590 die_error(404, "No projects found");
5594 if (defined $home_text && -f $home_text) {
5595 print "<div class=\"index_include\">\n";
5596 insert_file($home_text);
5599 print $cgi->startform(-method => "get") .
5600 "<p class=\"projsearch\">Search:\n" .
5601 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
5603 $cgi->end_form() . "\n";
5604 git_project_list_body(\@list, $order);
5609 my $order = $input_params{'order'};
5610 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5611 die_error(400, "Unknown order parameter");
5614 my @list = git_get_projects_list($project);
5616 die_error(404, "No forks found");
5620 git_print_page_nav('','');
5621 git_print_header_div('summary', "$project forks");
5622 git_project_list_body(\@list, $order);
5626 sub git_project_index {
5627 my @projects = git_get_projects_list();
5629 die_error(404, "No projects found");
5633 -type => 'text/plain',
5634 -charset => 'utf-8',
5635 -content_disposition => 'inline; filename="index.aux"');
5637 foreach my $pr (@projects) {
5638 if (!exists $pr->{'owner'}) {
5639 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
5642 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5643 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5644 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5645 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5649 print "$path $owner\n";
5654 my $descr = git_get_project_description($project) || "none";
5655 my %co = parse_commit("HEAD");
5656 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5657 my $head = $co{'id'};
5658 my $remote_heads = gitweb_check_feature('remote_heads');
5660 my $owner = git_get_project_owner($project);
5662 my $refs = git_get_references();
5663 # These get_*_list functions return one more to allow us to see if
5664 # there are more ...
5665 my @taglist = git_get_tags_list(16);
5666 my @headlist = git_get_heads_list(16);
5667 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
5669 my $check_forks = gitweb_check_feature('forks');
5672 # find forks of a project
5673 @forklist = git_get_projects_list($project);
5674 # filter out forks of forks
5675 @forklist = filter_forks_from_projects_list(\@forklist)
5680 git_print_page_nav('summary','', $head);
5682 print "<div class=\"title\"> </div>\n";
5683 print "<table class=\"projects_list\">\n" .
5684 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
5685 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
5686 if (defined $cd{'rfc2822'}) {
5687 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
5688 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
5691 # use per project git URL list in $projectroot/$project/cloneurl
5692 # or make project git URL from git base URL and project name
5693 my $url_tag = "URL";
5694 my @url_list = git_get_project_url_list($project);
5695 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5696 foreach my $git_url (@url_list) {
5697 next unless $git_url;
5698 print format_repo_url($url_tag, $git_url);
5703 my $show_ctags = gitweb_check_feature('ctags');
5705 my $ctags = git_get_project_ctags($project);
5707 # without ability to add tags, don't show if there are none
5708 my $cloud = git_populate_project_tagcloud($ctags);
5709 print "<tr id=\"metadata_ctags\">" .
5710 "<td>content tags</td>" .
5711 "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
5718 # If XSS prevention is on, we don't include README.html.
5719 # TODO: Allow a readme in some safe format.
5720 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
5721 print "<div class=\"title\">readme</div>\n" .
5722 "<div class=\"readme\">\n";
5723 insert_file("$projectroot/$project/README.html");
5724 print "\n</div>\n"; # class="readme"
5727 # we need to request one more than 16 (0..15) to check if
5729 my @commitlist = $head ? parse_commits($head, 17) : ();
5731 git_print_header_div('shortlog');
5732 git_shortlog_body(\@commitlist, 0, 15, $refs,
5733 $#commitlist <= 15 ? undef :
5734 $cgi->a({-href => href(action=>"shortlog")}, "..."));
5738 git_print_header_div('tags');
5739 git_tags_body(\@taglist, 0, 15,
5740 $#taglist <= 15 ? undef :
5741 $cgi->a({-href => href(action=>"tags")}, "..."));
5745 git_print_header_div('heads');
5746 git_heads_body(\@headlist, $head, 0, 15,
5747 $#headlist <= 15 ? undef :
5748 $cgi->a({-href => href(action=>"heads")}, "..."));
5752 git_print_header_div('remotes');
5753 git_remotes_body(\%remotedata, 15, $head);
5757 git_print_header_div('forks');
5758 git_project_list_body(\@forklist, 'age', 0, 15,
5759 $#forklist <= 15 ? undef :
5760 $cgi->a({-href => href(action=>"forks")}, "..."),
5768 my %tag = parse_tag($hash);
5771 die_error(404, "Unknown tag object");
5774 my $head = git_get_head_hash($project);
5776 git_print_page_nav('','', $head,undef,$head);
5777 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
5778 print "<div class=\"title_text\">\n" .
5779 "<table class=\"object_header\">\n" .
5781 "<td>object</td>\n" .
5782 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5783 $tag{'object'}) . "</td>\n" .
5784 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5785 $tag{'type'}) . "</td>\n" .
5787 if (defined($tag{'author'})) {
5788 git_print_authorship_rows(\%tag, 'author');
5790 print "</table>\n\n" .
5792 print "<div class=\"page_body\">";
5793 my $comment = $tag{'comment'};
5794 foreach my $line (@$comment) {
5796 print esc_html($line, -nbsp=>1) . "<br/>\n";
5802 sub git_blame_common {
5803 my $format = shift || 'porcelain';
5804 if ($format eq 'porcelain' && $cgi->param('js')) {
5805 $format = 'incremental';
5806 $action = 'blame_incremental'; # for page title etc
5810 gitweb_check_feature('blame')
5811 or die_error(403, "Blame view not allowed");
5814 die_error(400, "No file name given") unless $file_name;
5815 $hash_base ||= git_get_head_hash($project);
5816 die_error(404, "Couldn't find base commit") unless $hash_base;
5817 my %co = parse_commit($hash_base)
5818 or die_error(404, "Commit not found");
5820 if (!defined $hash) {
5821 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5822 or die_error(404, "Error looking up file");
5824 $ftype = git_get_type($hash);
5825 if ($ftype !~ "blob") {
5826 die_error(400, "Object is not a blob");
5831 if ($format eq 'incremental') {
5832 # get file contents (as base)
5833 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5834 or die_error(500, "Open git-cat-file failed");
5835 } elsif ($format eq 'data') {
5836 # run git-blame --incremental
5837 open $fd, "-|", git_cmd(), "blame", "--incremental",
5838 $hash_base, "--", $file_name
5839 or die_error(500, "Open git-blame --incremental failed");
5841 # run git-blame --porcelain
5842 open $fd, "-|", git_cmd(), "blame", '-p',
5843 $hash_base, '--', $file_name
5844 or die_error(500, "Open git-blame --porcelain failed");
5847 # incremental blame data returns early
5848 if ($format eq 'data') {
5850 -type=>"text/plain", -charset => "utf-8",
5851 -status=> "200 OK");
5852 local $| = 1; # output autoflush
5855 or print "ERROR $!\n";
5858 if (defined $t0 && gitweb_check_feature('timed')) {
5860 tv_interval($t0, [ gettimeofday() ]).
5861 ' '.$number_of_git_cmds;
5871 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5874 if ($format eq 'incremental') {
5876 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
5877 "blame") . " (non-incremental)";
5880 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
5881 "blame") . " (incremental)";
5885 $cgi->a({-href => href(action=>"history", -replay=>1)},
5888 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
5890 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5891 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5892 git_print_page_path($file_name, $ftype, $hash_base);
5895 if ($format eq 'incremental') {
5896 print "<noscript>\n<div class=\"error\"><center><b>\n".
5897 "This page requires JavaScript to run.\n Use ".
5898 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
5901 "</b></center></div>\n</noscript>\n";
5903 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
5906 print qq!<div class="page_body">\n!;
5907 print qq!<div id="progress_info">... / ...</div>\n!
5908 if ($format eq 'incremental');
5909 print qq!<table id="blame_table" class="blame" width="100%">\n!.
5910 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5912 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
5916 my @rev_color = qw(light dark);
5917 my $num_colors = scalar(@rev_color);
5918 my $current_color = 0;
5920 if ($format eq 'incremental') {
5921 my $color_class = $rev_color[$current_color];
5926 while (my $line = <$fd>) {
5930 print qq!<tr id="l$linenr" class="$color_class">!.
5931 qq!<td class="sha1"><a href=""> </a></td>!.
5932 qq!<td class="linenr">!.
5933 qq!<a class="linenr" href="">$linenr</a></td>!;
5934 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
5938 } else { # porcelain, i.e. ordinary blame
5939 my %metainfo = (); # saves information about commits
5943 while (my $line = <$fd>) {
5945 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5946 # no <lines in group> for subsequent lines in group of lines
5947 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5948 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5949 if (!exists $metainfo{$full_rev}) {
5950 $metainfo{$full_rev} = { 'nprevious' => 0 };
5952 my $meta = $metainfo{$full_rev};
5954 while ($data = <$fd>) {
5956 last if ($data =~ s/^\t//); # contents of line
5957 if ($data =~ /^(\S+)(?: (.*))?$/) {
5958 $meta->{$1} = $2 unless exists $meta->{$1};
5960 if ($data =~ /^previous /) {
5961 $meta->{'nprevious'}++;
5964 my $short_rev = substr($full_rev, 0, 8);
5965 my $author = $meta->{'author'};
5967 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5968 my $date = $date{'iso-tz'};
5970 $current_color = ($current_color + 1) % $num_colors;
5972 my $tr_class = $rev_color[$current_color];
5973 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5974 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5975 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5976 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5978 print "<td class=\"sha1\"";
5979 print " title=\"". esc_html($author) . ", $date\"";
5980 print " rowspan=\"$group_size\"" if ($group_size > 1);
5982 print $cgi->a({-href => href(action=>"commit",
5984 file_name=>$file_name)},
5985 esc_html($short_rev));
5986 if ($group_size >= 2) {
5987 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5988 if (@author_initials) {
5990 esc_html(join('', @author_initials));
5996 # 'previous' <sha1 of parent commit> <filename at commit>
5997 if (exists $meta->{'previous'} &&
5998 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5999 $meta->{'parent'} = $1;
6000 $meta->{'file_parent'} = unquote($2);
6003 exists($meta->{'parent'}) ?
6004 $meta->{'parent'} : $full_rev;
6005 my $linenr_filename =
6006 exists($meta->{'file_parent'}) ?
6007 $meta->{'file_parent'} : unquote($meta->{'filename'});
6008 my $blamed = href(action => 'blame',
6009 file_name => $linenr_filename,
6010 hash_base => $linenr_commit);
6011 print "<td class=\"linenr\">";
6012 print $cgi->a({ -href => "$blamed#l$orig_lineno",
6013 -class => "linenr" },
6016 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6024 "</table>\n"; # class="blame"
6025 print "</div>\n"; # class="blame_body"
6027 or print "Reading blob failed\n";
6036 sub git_blame_incremental {
6037 git_blame_common('incremental');
6040 sub git_blame_data {
6041 git_blame_common('data');
6045 my $head = git_get_head_hash($project);
6047 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
6048 git_print_header_div('summary', $project);
6050 my @tagslist = git_get_tags_list();
6052 git_tags_body(\@tagslist);
6058 my $head = git_get_head_hash($project);
6060 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
6061 git_print_header_div('summary', $project);
6063 my @headslist = git_get_heads_list();
6065 git_heads_body(\@headslist, $head);
6070 # used both for single remote view and for list of all the remotes
6072 gitweb_check_feature('remote_heads')
6073 or die_error(403, "Remote heads view is disabled");
6075 my $head = git_get_head_hash($project);
6076 my $remote = $input_params{'hash'};
6078 my $remotedata = git_get_remotes_list($remote);
6079 die_error(500, "Unable to get remote information") unless defined $remotedata;
6081 unless (%$remotedata) {
6082 die_error(404, defined $remote ?
6083 "Remote $remote not found" :
6084 "No remotes found");
6087 git_header_html(undef, undef, -action_extra => $remote);
6088 git_print_page_nav('', '', $head, undef, $head,
6089 format_ref_views($remote ? '' : 'remotes'));
6091 fill_remote_heads($remotedata);
6092 if (defined $remote) {
6093 git_print_header_div('remotes', "$remote remote for $project");
6094 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
6096 git_print_header_div('summary', "$project remotes");
6097 git_remotes_body($remotedata, undef, $head);
6103 sub git_blob_plain {
6107 if (!defined $hash) {
6108 if (defined $file_name) {
6109 my $base = $hash_base || git_get_head_hash($project);
6110 $hash = git_get_hash_by_path($base, $file_name, "blob")
6111 or die_error(404, "Cannot find file");
6113 die_error(400, "No file name defined");
6115 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6116 # blobs defined by non-textual hash id's can be cached
6120 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6121 or die_error(500, "Open git-cat-file blob '$hash' failed");
6123 # content-type (can include charset)
6124 $type = blob_contenttype($fd, $file_name, $type);
6126 # "save as" filename, even when no $file_name is given
6127 my $save_as = "$hash";
6128 if (defined $file_name) {
6129 $save_as = $file_name;
6130 } elsif ($type =~ m/^text\//) {
6134 # With XSS prevention on, blobs of all types except a few known safe
6135 # ones are served with "Content-Disposition: attachment" to make sure
6136 # they don't run in our security domain. For certain image types,
6137 # blob view writes an <img> tag referring to blob_plain view, and we
6138 # want to be sure not to break that by serving the image as an
6139 # attachment (though Firefox 3 doesn't seem to care).
6140 my $sandbox = $prevent_xss &&
6141 $type !~ m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6143 # serve text/* as text/plain
6145 ($type =~ m!^text/[a-z]+\b(.*)$! ||
6146 ($type =~ m!^[a-z]+/[a-z]\+xml\b(.*)$! && -T $fd))) {
6148 $rest = defined $rest ? $rest : '';
6149 $type = "text/plain$rest";
6154 -expires => $expires,
6155 -content_disposition =>
6156 ($sandbox ? 'attachment' : 'inline')
6157 . '; filename="' . $save_as . '"');
6159 binmode STDOUT, ':raw';
6161 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6168 if (!defined $hash) {
6169 if (defined $file_name) {
6170 my $base = $hash_base || git_get_head_hash($project);
6171 $hash = git_get_hash_by_path($base, $file_name, "blob")
6172 or die_error(404, "Cannot find file");
6174 die_error(400, "No file name defined");
6176 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6177 # blobs defined by non-textual hash id's can be cached
6181 my $have_blame = gitweb_check_feature('blame');
6182 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6183 or die_error(500, "Couldn't cat $file_name, $hash");
6184 my $mimetype = blob_mimetype($fd, $file_name);
6185 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6186 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
6188 return git_blob_plain($mimetype);
6190 # we can have blame only for text/* mimetype
6191 $have_blame &&= ($mimetype =~ m!^text/!);
6193 my $highlight = gitweb_check_feature('highlight');
6194 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
6195 $fd = run_highlighter($fd, $highlight, $syntax)
6198 git_header_html(undef, $expires);
6199 my $formats_nav = '';
6200 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6201 if (defined $file_name) {
6204 $cgi->a({-href => href(action=>"blame", -replay=>1)},
6209 $cgi->a({-href => href(action=>"history", -replay=>1)},
6212 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6215 $cgi->a({-href => href(action=>"blob",
6216 hash_base=>"HEAD", file_name=>$file_name)},
6220 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6223 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6224 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6226 print "<div class=\"page_nav\">\n" .
6227 "<br/><br/></div>\n" .
6228 "<div class=\"title\">".esc_html($hash)."</div>\n";
6230 git_print_page_path($file_name, "blob", $hash_base);
6231 print "<div class=\"page_body\">\n";
6232 if ($mimetype =~ m!^image/!) {
6233 print qq!<img type="!.esc_attr($mimetype).qq!"!;
6235 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
6238 href(action=>"blob_plain", hash=>$hash,
6239 hash_base=>$hash_base, file_name=>$file_name) .
6243 while (my $line = <$fd>) {
6246 $line = untabify($line);
6247 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
6248 $nr, esc_attr(href(-replay => 1)), $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);
6252 or print "Reading blob failed.\n";
6258 if (!defined $hash_base) {
6259 $hash_base = "HEAD";
6261 if (!defined $hash) {
6262 if (defined $file_name) {
6263 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
6268 die_error(404, "No such tree") unless defined($hash);
6270 my $show_sizes = gitweb_check_feature('show-sizes');
6271 my $have_blame = gitweb_check_feature('blame');
6276 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
6277 ($show_sizes ? '-l' : ()), @extra_options, $hash
6278 or die_error(500, "Open git-ls-tree failed");
6279 @entries = map { chomp; $_ } <$fd>;
6281 or die_error(404, "Reading tree failed");
6284 my $refs = git_get_references();
6285 my $ref = format_ref_marker($refs, $hash_base);
6288 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6290 if (defined $file_name) {
6292 $cgi->a({-href => href(action=>"history", -replay=>1)},
6294 $cgi->a({-href => href(action=>"tree",
6295 hash_base=>"HEAD", file_name=>$file_name)},
6298 my $snapshot_links = format_snapshot_links($hash);
6299 if (defined $snapshot_links) {
6300 # FIXME: Should be available when we have no hash base as well.
6301 push @views_nav, $snapshot_links;
6303 git_print_page_nav('tree','', $hash_base, undef, undef,
6304 join(' | ', @views_nav));
6305 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
6308 print "<div class=\"page_nav\">\n";
6309 print "<br/><br/></div>\n";
6310 print "<div class=\"title\">".esc_html($hash)."</div>\n";
6312 if (defined $file_name) {
6313 $basedir = $file_name;
6314 if ($basedir ne '' && substr($basedir, -1) ne '/') {
6317 git_print_page_path($file_name, 'tree', $hash_base);
6319 print "<div class=\"page_body\">\n";
6320 print "<table class=\"tree\">\n";
6322 # '..' (top directory) link if possible
6323 if (defined $hash_base &&
6324 defined $file_name && $file_name =~ m![^/]+$!) {
6326 print "<tr class=\"dark\">\n";
6328 print "<tr class=\"light\">\n";
6332 my $up = $file_name;
6333 $up =~ s!/?[^/]+$!!;
6334 undef $up unless $up;
6335 # based on git_print_tree_entry
6336 print '<td class="mode">' . mode_str('040000') . "</td>\n";
6337 print '<td class="size"> </td>'."\n" if $show_sizes;
6338 print '<td class="list">';
6339 print $cgi->a({-href => href(action=>"tree",
6340 hash_base=>$hash_base,
6344 print "<td class=\"link\"></td>\n";
6348 foreach my $line (@entries) {
6349 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
6352 print "<tr class=\"dark\">\n";
6354 print "<tr class=\"light\">\n";
6358 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
6362 print "</table>\n" .
6368 my ($project, $hash) = @_;
6370 # path/to/project.git -> project
6371 # path/to/project/.git -> project
6372 my $name = to_utf8($project);
6373 $name =~ s,([^/])/*\.git$,$1,;
6374 $name = basename($name);
6376 $name =~ s/[[:cntrl:]]/?/g;
6379 if ($hash =~ /^[0-9a-fA-F]+$/) {
6380 # shorten SHA-1 hash
6381 my $full_hash = git_get_full_hash($project, $hash);
6382 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
6383 $ver = git_get_short_hash($project, $hash);
6385 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
6386 # tags don't need shortened SHA-1 hash
6389 # branches and other need shortened SHA-1 hash
6390 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
6393 $ver .= '-' . git_get_short_hash($project, $hash);
6395 # in case of hierarchical branch names
6398 # name = project-version_string
6399 $name = "$name-$ver";
6401 return wantarray ? ($name, $name) : $name;
6405 my $format = $input_params{'snapshot_format'};
6406 if (!@snapshot_fmts) {
6407 die_error(403, "Snapshots not allowed");
6409 # default to first supported snapshot format
6410 $format ||= $snapshot_fmts[0];
6411 if ($format !~ m/^[a-z0-9]+$/) {
6412 die_error(400, "Invalid snapshot format parameter");
6413 } elsif (!exists($known_snapshot_formats{$format})) {
6414 die_error(400, "Unknown snapshot format");
6415 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
6416 die_error(403, "Snapshot format not allowed");
6417 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
6418 die_error(403, "Unsupported snapshot format");
6421 my $type = git_get_type("$hash^{}");
6423 die_error(404, 'Object does not exist');
6424 } elsif ($type eq 'blob') {
6425 die_error(400, 'Object is not a tree-ish');
6428 my ($name, $prefix) = snapshot_name($project, $hash);
6429 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
6430 my $cmd = quote_command(
6431 git_cmd(), 'archive',
6432 "--format=$known_snapshot_formats{$format}{'format'}",
6433 "--prefix=$prefix/", $hash);
6434 if (exists $known_snapshot_formats{$format}{'compressor'}) {
6435 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
6438 $filename =~ s/(["\\])/\\$1/g;
6440 -type => $known_snapshot_formats{$format}{'type'},
6441 -content_disposition => 'inline; filename="' . $filename . '"',
6442 -status => '200 OK');
6444 open my $fd, "-|", $cmd
6445 or die_error(500, "Execute git-archive failed");
6446 binmode STDOUT, ':raw';
6448 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6452 sub git_log_generic {
6453 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
6455 my $head = git_get_head_hash($project);
6456 if (!defined $base) {
6459 if (!defined $page) {
6462 my $refs = git_get_references();
6464 my $commit_hash = $base;
6465 if (defined $parent) {
6466 $commit_hash = "$parent..$base";
6469 parse_commits($commit_hash, 101, (100 * $page),
6470 defined $file_name ? ($file_name, "--full-history") : ());
6473 if (!defined $file_hash && defined $file_name) {
6474 # some commits could have deleted file in question,
6475 # and not have it in tree, but one of them has to have it
6476 for (my $i = 0; $i < @commitlist; $i++) {
6477 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
6478 last if defined $file_hash;
6481 if (defined $file_hash) {
6482 $ftype = git_get_type($file_hash);
6484 if (defined $file_name && !defined $ftype) {
6485 die_error(500, "Unknown type of object");
6488 if (defined $file_name) {
6489 %co = parse_commit($base)
6490 or die_error(404, "Unknown commit object");
6494 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
6496 if ($#commitlist >= 100) {
6498 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6499 -accesskey => "n", -title => "Alt-n"}, "next");
6501 my $patch_max = gitweb_get_feature('patches');
6502 if ($patch_max && !defined $file_name) {
6503 if ($patch_max < 0 || @commitlist <= $patch_max) {
6504 $paging_nav .= " ⋅ " .
6505 $cgi->a({-href => href(action=>"patches", -replay=>1)},
6511 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
6512 if (defined $file_name) {
6513 git_print_header_div('commit', esc_html($co{'title'}), $base);
6515 git_print_header_div('summary', $project)
6517 git_print_page_path($file_name, $ftype, $hash_base)
6518 if (defined $file_name);
6520 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
6521 $file_name, $file_hash, $ftype);
6527 git_log_generic('log', \&git_log_body,
6528 $hash, $hash_parent);
6532 $hash ||= $hash_base || "HEAD";
6533 my %co = parse_commit($hash)
6534 or die_error(404, "Unknown commit object");
6536 my $parent = $co{'parent'};
6537 my $parents = $co{'parents'}; # listref
6539 # we need to prepare $formats_nav before any parameter munging
6541 if (!defined $parent) {
6543 $formats_nav .= '(initial)';
6544 } elsif (@$parents == 1) {
6545 # single parent commit
6548 $cgi->a({-href => href(action=>"commit",
6550 esc_html(substr($parent, 0, 7))) .
6557 $cgi->a({-href => href(action=>"commit",
6559 esc_html(substr($_, 0, 7)));
6563 if (gitweb_check_feature('patches') && @$parents <= 1) {
6564 $formats_nav .= " | " .
6565 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6569 if (!defined $parent) {
6573 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
6575 (@$parents <= 1 ? $parent : '-c'),
6577 or die_error(500, "Open git-diff-tree failed");
6578 @difftree = map { chomp; $_ } <$fd>;
6579 close $fd or die_error(404, "Reading git-diff-tree failed");
6581 # non-textual hash id's can be cached
6583 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6586 my $refs = git_get_references();
6587 my $ref = format_ref_marker($refs, $co{'id'});
6589 git_header_html(undef, $expires);
6590 git_print_page_nav('commit', '',
6591 $hash, $co{'tree'}, $hash,
6594 if (defined $co{'parent'}) {
6595 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
6597 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
6599 print "<div class=\"title_text\">\n" .
6600 "<table class=\"object_header\">\n";
6601 git_print_authorship_rows(\%co);
6602 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6605 "<td class=\"sha1\">" .
6606 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
6607 class => "list"}, $co{'tree'}) .
6609 "<td class=\"link\">" .
6610 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
6612 my $snapshot_links = format_snapshot_links($hash);
6613 if (defined $snapshot_links) {
6614 print " | " . $snapshot_links;
6619 foreach my $par (@$parents) {
6622 "<td class=\"sha1\">" .
6623 $cgi->a({-href => href(action=>"commit", hash=>$par),
6624 class => "list"}, $par) .
6626 "<td class=\"link\">" .
6627 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
6629 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
6636 print "<div class=\"page_body\">\n";
6637 git_print_log($co{'comment'});
6640 git_difftree_body(\@difftree, $hash, @$parents);
6646 # object is defined by:
6647 # - hash or hash_base alone
6648 # - hash_base and file_name
6651 # - hash or hash_base alone
6652 if ($hash || ($hash_base && !defined $file_name)) {
6653 my $object_id = $hash || $hash_base;
6655 open my $fd, "-|", quote_command(
6656 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6657 or die_error(404, "Object does not exist");
6661 or die_error(404, "Object does not exist");
6663 # - hash_base and file_name
6664 } elsif ($hash_base && defined $file_name) {
6665 $file_name =~ s,/+$,,;
6667 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
6668 or die_error(404, "Base object does not exist");
6670 # here errors should not hapen
6671 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
6672 or die_error(500, "Open git-ls-tree failed");
6676 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6677 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6678 die_error(404, "File or directory for given base does not exist");
6683 die_error(400, "Not enough information to find object");
6686 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
6687 hash=>$hash, hash_base=>$hash_base,
6688 file_name=>$file_name),
6689 -status => '302 Found');
6693 my $format = shift || 'html';
6700 # preparing $fd and %diffinfo for git_patchset_body
6702 if (defined $hash_base && defined $hash_parent_base) {
6703 if (defined $file_name) {
6705 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6706 $hash_parent_base, $hash_base,
6707 "--", (defined $file_parent ? $file_parent : ()), $file_name
6708 or die_error(500, "Open git-diff-tree failed");
6709 @difftree = map { chomp; $_ } <$fd>;
6711 or die_error(404, "Reading git-diff-tree failed");
6713 or die_error(404, "Blob diff not found");
6715 } elsif (defined $hash &&
6716 $hash =~ /[0-9a-fA-F]{40}/) {
6717 # try to find filename from $hash
6719 # read filtered raw output
6720 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6721 $hash_parent_base, $hash_base, "--"
6722 or die_error(500, "Open git-diff-tree failed");
6724 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6726 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6727 map { chomp; $_ } <$fd>;
6729 or die_error(404, "Reading git-diff-tree failed");
6731 or die_error(404, "Blob diff not found");
6734 die_error(400, "Missing one of the blob diff parameters");
6737 if (@difftree > 1) {
6738 die_error(400, "Ambiguous blob diff specification");
6741 %diffinfo = parse_difftree_raw_line($difftree[0]);
6742 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6743 $file_name ||= $diffinfo{'to_file'};
6745 $hash_parent ||= $diffinfo{'from_id'};
6746 $hash ||= $diffinfo{'to_id'};
6748 # non-textual hash id's can be cached
6749 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6750 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6755 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6756 '-p', ($format eq 'html' ? "--full-index" : ()),
6757 $hash_parent_base, $hash_base,
6758 "--", (defined $file_parent ? $file_parent : ()), $file_name
6759 or die_error(500, "Open git-diff-tree failed");
6762 # old/legacy style URI -- not generated anymore since 1.4.3.
6764 die_error('404 Not Found', "Missing one of the blob diff parameters")
6768 if ($format eq 'html') {
6770 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
6772 git_header_html(undef, $expires);
6773 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6774 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6775 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6777 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6778 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
6780 if (defined $file_name) {
6781 git_print_page_path($file_name, "blob", $hash_base);
6783 print "<div class=\"page_path\"></div>\n";
6786 } elsif ($format eq 'plain') {
6788 -type => 'text/plain',
6789 -charset => 'utf-8',
6790 -expires => $expires,
6791 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
6793 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6796 die_error(400, "Unknown blobdiff format");
6800 if ($format eq 'html') {
6801 print "<div class=\"page_body\">\n";
6803 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
6806 print "</div>\n"; # class="page_body"
6810 while (my $line = <$fd>) {
6811 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6812 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6816 last if $line =~ m!^\+\+\+!;
6824 sub git_blobdiff_plain {
6825 git_blobdiff('plain');
6828 sub git_commitdiff {
6830 my $format = $params{-format} || 'html';
6832 my ($patch_max) = gitweb_get_feature('patches');
6833 if ($format eq 'patch') {
6834 die_error(403, "Patch view not allowed") unless $patch_max;
6837 $hash ||= $hash_base || "HEAD";
6838 my %co = parse_commit($hash)
6839 or die_error(404, "Unknown commit object");
6841 # choose format for commitdiff for merge
6842 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
6843 $hash_parent = '--cc';
6845 # we need to prepare $formats_nav before almost any parameter munging
6847 if ($format eq 'html') {
6849 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
6851 if ($patch_max && @{$co{'parents'}} <= 1) {
6852 $formats_nav .= " | " .
6853 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6857 if (defined $hash_parent &&
6858 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6859 # commitdiff with two commits given
6860 my $hash_parent_short = $hash_parent;
6861 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6862 $hash_parent_short = substr($hash_parent, 0, 7);
6866 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6867 if ($co{'parents'}[$i] eq $hash_parent) {
6868 $formats_nav .= ' parent ' . ($i+1);
6872 $formats_nav .= ': ' .
6873 $cgi->a({-href => href(action=>"commitdiff",
6874 hash=>$hash_parent)},
6875 esc_html($hash_parent_short)) .
6877 } elsif (!$co{'parent'}) {
6879 $formats_nav .= ' (initial)';
6880 } elsif (scalar @{$co{'parents'}} == 1) {
6881 # single parent commit
6884 $cgi->a({-href => href(action=>"commitdiff",
6885 hash=>$co{'parent'})},
6886 esc_html(substr($co{'parent'}, 0, 7))) .
6890 if ($hash_parent eq '--cc') {
6891 $formats_nav .= ' | ' .
6892 $cgi->a({-href => href(action=>"commitdiff",
6893 hash=>$hash, hash_parent=>'-c')},
6895 } else { # $hash_parent eq '-c'
6896 $formats_nav .= ' | ' .
6897 $cgi->a({-href => href(action=>"commitdiff",
6898 hash=>$hash, hash_parent=>'--cc')},
6904 $cgi->a({-href => href(action=>"commitdiff",
6906 esc_html(substr($_, 0, 7)));
6907 } @{$co{'parents'}} ) .
6912 my $hash_parent_param = $hash_parent;
6913 if (!defined $hash_parent_param) {
6914 # --cc for multiple parents, --root for parentless
6915 $hash_parent_param =
6916 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6922 if ($format eq 'html') {
6923 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6924 "--no-commit-id", "--patch-with-raw", "--full-index",
6925 $hash_parent_param, $hash, "--"
6926 or die_error(500, "Open git-diff-tree failed");
6928 while (my $line = <$fd>) {
6930 # empty line ends raw part of diff-tree output
6932 push @difftree, scalar parse_difftree_raw_line($line);
6935 } elsif ($format eq 'plain') {
6936 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6937 '-p', $hash_parent_param, $hash, "--"
6938 or die_error(500, "Open git-diff-tree failed");
6939 } elsif ($format eq 'patch') {
6940 # For commit ranges, we limit the output to the number of
6941 # patches specified in the 'patches' feature.
6942 # For single commits, we limit the output to a single patch,
6943 # diverging from the git-format-patch default.
6944 my @commit_spec = ();
6946 if ($patch_max > 0) {
6947 push @commit_spec, "-$patch_max";
6949 push @commit_spec, '-n', "$hash_parent..$hash";
6951 if ($params{-single}) {
6952 push @commit_spec, '-1';
6954 if ($patch_max > 0) {
6955 push @commit_spec, "-$patch_max";
6957 push @commit_spec, "-n";
6959 push @commit_spec, '--root', $hash;
6961 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
6962 '--encoding=utf8', '--stdout', @commit_spec
6963 or die_error(500, "Open git-format-patch failed");
6965 die_error(400, "Unknown commitdiff format");
6968 # non-textual hash id's can be cached
6970 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6974 # write commit message
6975 if ($format eq 'html') {
6976 my $refs = git_get_references();
6977 my $ref = format_ref_marker($refs, $co{'id'});
6979 git_header_html(undef, $expires);
6980 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6981 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6982 print "<div class=\"title_text\">\n" .
6983 "<table class=\"object_header\">\n";
6984 git_print_authorship_rows(\%co);
6987 print "<div class=\"page_body\">\n";
6988 if (@{$co{'comment'}} > 1) {
6989 print "<div class=\"log\">\n";
6990 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6991 print "</div>\n"; # class="log"
6994 } elsif ($format eq 'plain') {
6995 my $refs = git_get_references("tags");
6996 my $tagname = git_get_rev_name_tags($hash);
6997 my $filename = basename($project) . "-$hash.patch";
7000 -type => 'text/plain',
7001 -charset => 'utf-8',
7002 -expires => $expires,
7003 -content_disposition => 'inline; filename="' . "$filename" . '"');
7004 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
7005 print "From: " . to_utf8($co{'author'}) . "\n";
7006 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7007 print "Subject: " . to_utf8($co{'title'}) . "\n";
7009 print "X-Git-Tag: $tagname\n" if $tagname;
7010 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7012 foreach my $line (@{$co{'comment'}}) {
7013 print to_utf8($line) . "\n";
7016 } elsif ($format eq 'patch') {
7017 my $filename = basename($project) . "-$hash.patch";
7020 -type => 'text/plain',
7021 -charset => 'utf-8',
7022 -expires => $expires,
7023 -content_disposition => 'inline; filename="' . "$filename" . '"');
7027 if ($format eq 'html') {
7028 my $use_parents = !defined $hash_parent ||
7029 $hash_parent eq '-c' || $hash_parent eq '--cc';
7030 git_difftree_body(\@difftree, $hash,
7031 $use_parents ? @{$co{'parents'}} : $hash_parent);
7034 git_patchset_body($fd, \@difftree, $hash,
7035 $use_parents ? @{$co{'parents'}} : $hash_parent);
7037 print "</div>\n"; # class="page_body"
7040 } elsif ($format eq 'plain') {
7044 or print "Reading git-diff-tree failed\n";
7045 } elsif ($format eq 'patch') {
7049 or print "Reading git-format-patch failed\n";
7053 sub git_commitdiff_plain {
7054 git_commitdiff(-format => 'plain');
7057 # format-patch-style patches
7059 git_commitdiff(-format => 'patch', -single => 1);
7063 git_commitdiff(-format => 'patch');
7067 git_log_generic('history', \&git_history_body,
7068 $hash_base, $hash_parent_base,
7073 gitweb_check_feature('search') or die_error(403, "Search is disabled");
7074 if (!defined $searchtext) {
7075 die_error(400, "Text field is empty");
7077 if (!defined $hash) {
7078 $hash = git_get_head_hash($project);
7080 my %co = parse_commit($hash);
7082 die_error(404, "Unknown commit object");
7084 if (!defined $page) {
7088 $searchtype ||= 'commit';
7089 if ($searchtype eq 'pickaxe') {
7090 # pickaxe may take all resources of your box and run for several minutes
7091 # with every query - so decide by yourself how public you make this feature
7092 gitweb_check_feature('pickaxe')
7093 or die_error(403, "Pickaxe is disabled");
7095 if ($searchtype eq 'grep') {
7096 gitweb_check_feature('grep')
7097 or die_error(403, "Grep is disabled");
7102 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
7104 if ($searchtype eq 'commit') {
7105 $greptype = "--grep=";
7106 } elsif ($searchtype eq 'author') {
7107 $greptype = "--author=";
7108 } elsif ($searchtype eq 'committer') {
7109 $greptype = "--committer=";
7111 $greptype .= $searchtext;
7112 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
7113 $greptype, '--regexp-ignore-case',
7114 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
7116 my $paging_nav = '';
7119 $cgi->a({-href => href(action=>"search", hash=>$hash,
7120 searchtext=>$searchtext,
7121 searchtype=>$searchtype)},
7123 $paging_nav .= " ⋅ " .
7124 $cgi->a({-href => href(-replay=>1, page=>$page-1),
7125 -accesskey => "p", -title => "Alt-p"}, "prev");
7127 $paging_nav .= "first";
7128 $paging_nav .= " ⋅ prev";
7131 if ($#commitlist >= 100) {
7133 $cgi->a({-href => href(-replay=>1, page=>$page+1),
7134 -accesskey => "n", -title => "Alt-n"}, "next");
7135 $paging_nav .= " ⋅ $next_link";
7137 $paging_nav .= " ⋅ next";
7140 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
7141 git_print_header_div('commit', esc_html($co{'title'}), $hash);
7142 if ($page == 0 && !@commitlist) {
7143 print "<p>No match.</p>\n";
7145 git_search_grep_body(\@commitlist, 0, 99, $next_link);
7149 if ($searchtype eq 'pickaxe') {
7150 git_print_page_nav('','', $hash,$co{'tree'},$hash);
7151 git_print_header_div('commit', esc_html($co{'title'}), $hash);
7153 print "<table class=\"pickaxe search\">\n";
7156 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
7157 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
7158 ($search_use_regexp ? '--pickaxe-regex' : ());
7161 while (my $line = <$fd>) {
7165 my %set = parse_difftree_raw_line($line);
7166 if (defined $set{'commit'}) {
7167 # finish previous commit
7170 "<td class=\"link\">" .
7171 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
7173 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
7179 print "<tr class=\"dark\">\n";
7181 print "<tr class=\"light\">\n";
7184 %co = parse_commit($set{'commit'});
7185 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
7186 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
7187 "<td><i>$author</i></td>\n" .
7189 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
7190 -class => "list subject"},
7191 chop_and_escape_str($co{'title'}, 50) . "<br/>");
7192 } elsif (defined $set{'to_id'}) {
7193 next if ($set{'to_id'} =~ m/^0{40}$/);
7195 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
7196 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
7198 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
7204 # finish last commit (warning: repetition!)
7207 "<td class=\"link\">" .
7208 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
7210 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
7218 if ($searchtype eq 'grep') {
7219 git_print_page_nav('','', $hash,$co{'tree'},$hash);
7220 git_print_header_div('commit', esc_html($co{'title'}), $hash);
7222 print "<table class=\"grep_search\">\n";
7226 open my $fd, "-|", git_cmd(), 'grep', '-n',
7227 $search_use_regexp ? ('-E', '-i') : '-F',
7228 $searchtext, $co{'tree'};
7230 while (my $line = <$fd>) {
7232 my ($file, $lno, $ltext, $binary);
7233 last if ($matches++ > 1000);
7234 if ($line =~ /^Binary file (.+) matches$/) {
7238 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
7240 if ($file ne $lastfile) {
7241 $lastfile and print "</td></tr>\n";
7243 print "<tr class=\"dark\">\n";
7245 print "<tr class=\"light\">\n";
7247 print "<td class=\"list\">".
7248 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
7249 file_name=>"$file"),
7250 -class => "list"}, esc_path($file));
7251 print "</td><td>\n";
7255 print "<div class=\"binary\">Binary file</div>\n";
7257 $ltext = untabify($ltext);
7258 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
7259 $ltext = esc_html($1, -nbsp=>1);
7260 $ltext .= '<span class="match">';
7261 $ltext .= esc_html($2, -nbsp=>1);
7262 $ltext .= '</span>';
7263 $ltext .= esc_html($3, -nbsp=>1);
7265 $ltext = esc_html($ltext, -nbsp=>1);
7267 print "<div class=\"pre\">" .
7268 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
7269 file_name=>"$file").'#l'.$lno,
7270 -class => "linenr"}, sprintf('%4i', $lno))
7271 . ' ' . $ltext . "</div>\n";
7275 print "</td></tr>\n";
7276 if ($matches > 1000) {
7277 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
7280 print "<div class=\"diff nodifferences\">No matches found</div>\n";
7289 sub git_search_help {
7291 git_print_page_nav('','', $hash,$hash,$hash);
7293 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7294 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7295 the pattern entered is recognized as the POSIX extended
7296 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7299 <dt><b>commit</b></dt>
7300 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7302 my $have_grep = gitweb_check_feature('grep');
7305 <dt><b>grep</b></dt>
7306 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7307 a different one) are searched for the given pattern. On large trees, this search can take
7308 a while and put some strain on the server, so please use it with some consideration. Note that
7309 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7310 case-sensitive.</dd>
7314 <dt><b>author</b></dt>
7315 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7316 <dt><b>committer</b></dt>
7317 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7319 my $have_pickaxe = gitweb_check_feature('pickaxe');
7320 if ($have_pickaxe) {
7322 <dt><b>pickaxe</b></dt>
7323 <dd>All commits that caused the string to appear or disappear from any file (changes that
7324 added, removed or "modified" the string) will be listed. This search can take a while and
7325 takes a lot of strain on the server, so please use it wisely. Note that since you may be
7326 interested even in changes just changing the case as well, this search is case sensitive.</dd>
7334 git_log_generic('shortlog', \&git_shortlog_body,
7335 $hash, $hash_parent);
7338 ## ......................................................................
7339 ## feeds (RSS, Atom; OPML)
7342 my $format = shift || 'atom';
7343 my $have_blame = gitweb_check_feature('blame');
7345 # Atom: http://www.atomenabled.org/developers/syndication/
7346 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
7347 if ($format ne 'rss' && $format ne 'atom') {
7348 die_error(400, "Unknown web feed format");
7351 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7352 my $head = $hash || 'HEAD';
7353 my @commitlist = parse_commits($head, 150, 0, $file_name);
7357 my $content_type = "application/$format+xml";
7358 if (defined $cgi->http('HTTP_ACCEPT') &&
7359 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7360 # browser (feed reader) prefers text/xml
7361 $content_type = 'text/xml';
7363 if (defined($commitlist[0])) {
7364 %latest_commit = %{$commitlist[0]};
7365 my $latest_epoch = $latest_commit{'committer_epoch'};
7366 %latest_date = parse_date($latest_epoch, $latest_commit{'comitter_tz'});
7367 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7368 if (defined $if_modified) {
7370 if (eval { require HTTP::Date; 1; }) {
7371 $since = HTTP::Date::str2time($if_modified);
7372 } elsif (eval { require Time::ParseDate; 1; }) {
7373 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7375 if (defined $since && $latest_epoch <= $since) {
7377 -type => $content_type,
7378 -charset => 'utf-8',
7379 -last_modified => $latest_date{'rfc2822'},
7380 -status => '304 Not Modified');
7385 -type => $content_type,
7386 -charset => 'utf-8',
7387 -last_modified => $latest_date{'rfc2822'});
7390 -type => $content_type,
7391 -charset => 'utf-8');
7394 # Optimization: skip generating the body if client asks only
7395 # for Last-Modified date.
7396 return if ($cgi->request_method() eq 'HEAD');
7399 my $title = "$site_name - $project/$action";
7400 my $feed_type = 'log';
7401 if (defined $hash) {
7402 $title .= " - '$hash'";
7403 $feed_type = 'branch log';
7404 if (defined $file_name) {
7405 $title .= " :: $file_name";
7406 $feed_type = 'history';
7408 } elsif (defined $file_name) {
7409 $title .= " - $file_name";
7410 $feed_type = 'history';
7412 $title .= " $feed_type";
7413 my $descr = git_get_project_description($project);
7414 if (defined $descr) {
7415 $descr = esc_html($descr);
7417 $descr = "$project " .
7418 ($format eq 'rss' ? 'RSS' : 'Atom') .
7421 my $owner = git_get_project_owner($project);
7422 $owner = esc_html($owner);
7426 if (defined $file_name) {
7427 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
7428 } elsif (defined $hash) {
7429 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
7431 $alt_url = href(-full=>1, action=>"summary");
7433 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
7434 if ($format eq 'rss') {
7436 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7439 print "<title>$title</title>\n" .
7440 "<link>$alt_url</link>\n" .
7441 "<description>$descr</description>\n" .
7442 "<language>en</language>\n" .
7443 # project owner is responsible for 'editorial' content
7444 "<managingEditor>$owner</managingEditor>\n";
7445 if (defined $logo || defined $favicon) {
7446 # prefer the logo to the favicon, since RSS
7447 # doesn't allow both
7448 my $img = esc_url($logo || $favicon);
7450 "<url>$img</url>\n" .
7451 "<title>$title</title>\n" .
7452 "<link>$alt_url</link>\n" .
7456 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7457 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7459 print "<generator>gitweb v.$version/$git_version</generator>\n";
7460 } elsif ($format eq 'atom') {
7462 <feed xmlns="http://www.w3.org/2005/Atom">
7464 print "<title>$title</title>\n" .
7465 "<subtitle>$descr</subtitle>\n" .
7466 '<link rel="alternate" type="text/html" href="' .
7467 $alt_url . '" />' . "\n" .
7468 '<link rel="self" type="' . $content_type . '" href="' .
7469 $cgi->self_url() . '" />' . "\n" .
7470 "<id>" . href(-full=>1) . "</id>\n" .
7471 # use project owner for feed author
7472 "<author><name>$owner</name></author>\n";
7473 if (defined $favicon) {
7474 print "<icon>" . esc_url($favicon) . "</icon>\n";
7476 if (defined $logo) {
7477 # not twice as wide as tall: 72 x 27 pixels
7478 print "<logo>" . esc_url($logo) . "</logo>\n";
7480 if (! %latest_date) {
7481 # dummy date to keep the feed valid until commits trickle in:
7482 print "<updated>1970-01-01T00:00:00Z</updated>\n";
7484 print "<updated>$latest_date{'iso-8601'}</updated>\n";
7486 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7490 for (my $i = 0; $i <= $#commitlist; $i++) {
7491 my %co = %{$commitlist[$i]};
7492 my $commit = $co{'id'};
7493 # we read 150, we always show 30 and the ones more recent than 48 hours
7494 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7497 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
7499 # get list of changed files
7500 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7501 $co{'parent'} || "--root",
7502 $co{'id'}, "--", (defined $file_name ? $file_name : ())
7504 my @difftree = map { chomp; $_ } <$fd>;
7508 # print element (entry, item)
7509 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
7510 if ($format eq 'rss') {
7512 "<title>" . esc_html($co{'title'}) . "</title>\n" .
7513 "<author>" . esc_html($co{'author'}) . "</author>\n" .
7514 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7515 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7516 "<link>$co_url</link>\n" .
7517 "<description>" . esc_html($co{'title'}) . "</description>\n" .
7518 "<content:encoded>" .
7520 } elsif ($format eq 'atom') {
7522 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
7523 "<updated>$cd{'iso-8601'}</updated>\n" .
7525 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
7526 if ($co{'author_email'}) {
7527 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
7529 print "</author>\n" .
7530 # use committer for contributor
7532 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
7533 if ($co{'committer_email'}) {
7534 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
7536 print "</contributor>\n" .
7537 "<published>$cd{'iso-8601'}</published>\n" .
7538 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
7539 "<id>$co_url</id>\n" .
7540 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
7541 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
7543 my $comment = $co{'comment'};
7545 foreach my $line (@$comment) {
7546 $line = esc_html($line);
7549 print "</pre><ul>\n";
7550 foreach my $difftree_line (@difftree) {
7551 my %difftree = parse_difftree_raw_line($difftree_line);
7552 next if !$difftree{'from_id'};
7554 my $file = $difftree{'file'} || $difftree{'to_file'};
7558 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
7559 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
7560 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
7561 file_name=>$file, file_parent=>$difftree{'from_file'}),
7562 -title => "diff"}, 'D');
7564 print $cgi->a({-href => href(-full=>1, action=>"blame",
7565 file_name=>$file, hash_base=>$commit),
7566 -title => "blame"}, 'B');
7568 # if this is not a feed of a file history
7569 if (!defined $file_name || $file_name ne $file) {
7570 print $cgi->a({-href => href(-full=>1, action=>"history",
7571 file_name=>$file, hash=>$commit),
7572 -title => "history"}, 'H');
7574 $file = esc_path($file);
7578 if ($format eq 'rss') {
7579 print "</ul>]]>\n" .
7580 "</content:encoded>\n" .
7582 } elsif ($format eq 'atom') {
7583 print "</ul>\n</div>\n" .
7590 if ($format eq 'rss') {
7591 print "</channel>\n</rss>\n";
7592 } elsif ($format eq 'atom') {
7606 my @list = git_get_projects_list();
7608 die_error(404, "No projects found");
7612 -type => 'text/xml',
7613 -charset => 'utf-8',
7614 -content_disposition => 'inline; filename="opml.xml"');
7617 <?xml version="1.0" encoding="utf-8"?>
7618 <opml version="1.0">
7620 <title>$site_name OPML Export</title>
7623 <outline text="git RSS feeds">
7626 foreach my $pr (@list) {
7628 my $head = git_get_head_hash($proj{'path'});
7629 if (!defined $head) {
7632 $git_dir = "$projectroot/$proj{'path'}";
7633 my %co = parse_commit($head);
7638 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
7639 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
7640 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
7641 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";