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 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
1061 # path to the current git repository
1063 sub evaluate_git_dir {
1064 our $git_dir = "$projectroot/$project" if $project;
1067 our (@snapshot_fmts, $git_avatar);
1068 sub configure_gitweb_features {
1069 # list of supported snapshot formats
1070 our @snapshot_fmts = gitweb_get_feature('snapshot');
1071 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1073 # check that the avatar feature is set to a known provider name,
1074 # and for each provider check if the dependencies are satisfied.
1075 # if the provider name is invalid or the dependencies are not met,
1076 # reset $git_avatar to the empty string.
1077 our ($git_avatar) = gitweb_get_feature('avatar');
1078 if ($git_avatar eq 'gravatar') {
1079 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1080 } elsif ($git_avatar eq 'picon') {
1087 # custom error handler: 'die <message>' is Internal Server Error
1088 sub handle_errors_html {
1089 my $msg = shift; # it is already HTML escaped
1091 # to avoid infinite loop where error occurs in die_error,
1092 # change handler to default handler, disabling handle_errors_html
1093 set_message("Error occured when inside die_error:\n$msg");
1095 # you cannot jump out of die_error when called as error handler;
1096 # the subroutine set via CGI::Carp::set_message is called _after_
1097 # HTTP headers are already written, so it cannot write them itself
1098 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1100 set_message(\&handle_errors_html);
1104 if (!defined $action) {
1105 if (defined $hash) {
1106 $action = git_get_type($hash);
1107 } elsif (defined $hash_base && defined $file_name) {
1108 $action = git_get_type("$hash_base:$file_name");
1109 } elsif (defined $project) {
1110 $action = 'summary';
1112 $action = 'project_list';
1115 if (!defined($actions{$action})) {
1116 die_error(400, "Unknown action");
1118 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1120 die_error(400, "Project needed");
1122 $actions{$action}->();
1126 our $t0 = [ gettimeofday() ]
1128 our $number_of_git_cmds = 0;
1131 our $first_request = 1;
1136 if ($first_request) {
1137 evaluate_gitweb_config();
1138 evaluate_git_version();
1140 if ($per_request_config) {
1141 if (ref($per_request_config) eq 'CODE') {
1142 $per_request_config->();
1143 } elsif (!$first_request) {
1144 evaluate_gitweb_config();
1149 # $projectroot and $projects_list might be set in gitweb config file
1150 $projects_list ||= $projectroot;
1152 evaluate_query_params();
1153 evaluate_path_info();
1154 evaluate_and_validate_params();
1157 configure_gitweb_features();
1162 our $is_last_request = sub { 1 };
1163 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1166 sub configure_as_fcgi {
1168 our $CGI = 'CGI::Fast';
1170 my $request_number = 0;
1171 # let each child service 100 requests
1172 our $is_last_request = sub { ++$request_number > 100 };
1175 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1177 if $script_name =~ /\.fcgi$/;
1179 return unless (@ARGV);
1181 require Getopt::Long;
1182 Getopt::Long::GetOptions(
1183 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1184 'nproc|n=i' => sub {
1185 my ($arg, $val) = @_;
1186 return unless eval { require FCGI::ProcManager; 1; };
1187 my $proc_manager = FCGI::ProcManager->new({
1188 n_processes => $val,
1190 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1191 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1192 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1201 $pre_listen_hook->()
1202 if $pre_listen_hook;
1205 while ($cgi = $CGI->new()) {
1206 $pre_dispatch_hook->()
1207 if $pre_dispatch_hook;
1211 $post_dispatch_hook->()
1212 if $post_dispatch_hook;
1215 last REQUEST if ($is_last_request->());
1224 if (defined caller) {
1225 # wrapped in a subroutine processing requests,
1226 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1229 # pure CGI script, serving single request
1233 ## ======================================================================
1236 # possible values of extra options
1237 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1238 # -replay => 1 - start from a current view (replay with modifications)
1239 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1240 # -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone
1243 # default is to use -absolute url() i.e. $my_uri
1244 my $href = $params{-full} ? $my_url : $my_uri;
1246 # implicit -replay, must be first of implicit params
1247 $params{-replay} = 1 if (keys %params == 1 && $params{-anchor});
1249 $params{'project'} = $project unless exists $params{'project'};
1251 if ($params{-replay}) {
1252 while (my ($name, $symbol) = each %cgi_param_mapping) {
1253 if (!exists $params{$name}) {
1254 $params{$name} = $input_params{$name};
1259 my $use_pathinfo = gitweb_check_feature('pathinfo');
1260 if (defined $params{'project'} &&
1261 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1262 # try to put as many parameters as possible in PATH_INFO:
1265 # - hash_parent or hash_parent_base:/file_parent
1266 # - hash or hash_base:/filename
1267 # - the snapshot_format as an appropriate suffix
1269 # When the script is the root DirectoryIndex for the domain,
1270 # $href here would be something like http://gitweb.example.com/
1271 # Thus, we strip any trailing / from $href, to spare us double
1272 # slashes in the final URL
1275 # Then add the project name, if present
1276 $href .= "/".esc_path_info($params{'project'});
1277 delete $params{'project'};
1279 # since we destructively absorb parameters, we keep this
1280 # boolean that remembers if we're handling a snapshot
1281 my $is_snapshot = $params{'action'} eq 'snapshot';
1283 # Summary just uses the project path URL, any other action is
1285 if (defined $params{'action'}) {
1286 $href .= "/".esc_path_info($params{'action'})
1287 unless $params{'action'} eq 'summary';
1288 delete $params{'action'};
1291 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1292 # stripping nonexistent or useless pieces
1293 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1294 || $params{'hash_parent'} || $params{'hash'});
1295 if (defined $params{'hash_base'}) {
1296 if (defined $params{'hash_parent_base'}) {
1297 $href .= esc_path_info($params{'hash_parent_base'});
1298 # skip the file_parent if it's the same as the file_name
1299 if (defined $params{'file_parent'}) {
1300 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1301 delete $params{'file_parent'};
1302 } elsif ($params{'file_parent'} !~ /\.\./) {
1303 $href .= ":/".esc_path_info($params{'file_parent'});
1304 delete $params{'file_parent'};
1308 delete $params{'hash_parent'};
1309 delete $params{'hash_parent_base'};
1310 } elsif (defined $params{'hash_parent'}) {
1311 $href .= esc_path_info($params{'hash_parent'}). "..";
1312 delete $params{'hash_parent'};
1315 $href .= esc_path_info($params{'hash_base'});
1316 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1317 $href .= ":/".esc_path_info($params{'file_name'});
1318 delete $params{'file_name'};
1320 delete $params{'hash'};
1321 delete $params{'hash_base'};
1322 } elsif (defined $params{'hash'}) {
1323 $href .= esc_path_info($params{'hash'});
1324 delete $params{'hash'};
1327 # If the action was a snapshot, we can absorb the
1328 # snapshot_format parameter too
1330 my $fmt = $params{'snapshot_format'};
1331 # snapshot_format should always be defined when href()
1332 # is called, but just in case some code forgets, we
1333 # fall back to the default
1334 $fmt ||= $snapshot_fmts[0];
1335 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1336 delete $params{'snapshot_format'};
1340 # now encode the parameters explicitly
1342 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1343 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1344 if (defined $params{$name}) {
1345 if (ref($params{$name}) eq "ARRAY") {
1346 foreach my $par (@{$params{$name}}) {
1347 push @result, $symbol . "=" . esc_param($par);
1350 push @result, $symbol . "=" . esc_param($params{$name});
1354 $href .= "?" . join(';', @result) if scalar @result;
1356 # final transformation: trailing spaces must be escaped (URI-encoded)
1357 $href =~ s/(\s+)$/CGI::escape($1)/e;
1359 if ($params{-anchor}) {
1360 $href .= "#".esc_param($params{-anchor});
1367 ## ======================================================================
1368 ## validation, quoting/unquoting and escaping
1370 sub validate_action {
1371 my $input = shift || return undef;
1372 return undef unless exists $actions{$input};
1376 sub validate_project {
1377 my $input = shift || return undef;
1378 if (!validate_pathname($input) ||
1379 !(-d "$projectroot/$input") ||
1380 !check_export_ok("$projectroot/$input") ||
1381 ($strict_export && !project_in_list($input))) {
1388 sub validate_pathname {
1389 my $input = shift || return undef;
1391 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1392 # at the beginning, at the end, and between slashes.
1393 # also this catches doubled slashes
1394 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1397 # no null characters
1398 if ($input =~ m!\0!) {
1404 sub validate_refname {
1405 my $input = shift || return undef;
1407 # textual hashes are O.K.
1408 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1411 # it must be correct pathname
1412 $input = validate_pathname($input)
1414 # restrictions on ref name according to git-check-ref-format
1415 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1421 # decode sequences of octets in utf8 into Perl's internal form,
1422 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1423 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1426 return undef unless defined $str;
1427 if (utf8::valid($str)) {
1431 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1435 # quote unsafe chars, but keep the slash, even when it's not
1436 # correct, but quoted slashes look too horrible in bookmarks
1439 return undef unless defined $str;
1440 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1445 # the quoting rules for path_info fragment are slightly different
1448 return undef unless defined $str;
1450 # path_info doesn't treat '+' as space (specially), but '?' must be escaped
1451 $str =~ s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;
1456 # quote unsafe chars in whole URL, so some characters cannot be quoted
1459 return undef unless defined $str;
1460 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1465 # quote unsafe characters in HTML attributes
1468 # for XHTML conformance escaping '"' to '"' is not enough
1469 return esc_html(@_);
1472 # replace invalid utf8 character with SUBSTITUTION sequence
1477 return undef unless defined $str;
1479 $str = to_utf8($str);
1480 $str = $cgi->escapeHTML($str);
1481 if ($opts{'-nbsp'}) {
1482 $str =~ s/ / /g;
1484 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1488 # quote control characters and escape filename to HTML
1493 return undef unless defined $str;
1495 $str = to_utf8($str);
1496 $str = $cgi->escapeHTML($str);
1497 if ($opts{'-nbsp'}) {
1498 $str =~ s/ / /g;
1500 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1504 # Make control characters "printable", using character escape codes (CEC)
1508 my %es = ( # character escape codes, aka escape sequences
1509 "\t" => '\t', # tab (HT)
1510 "\n" => '\n', # line feed (LF)
1511 "\r" => '\r', # carrige return (CR)
1512 "\f" => '\f', # form feed (FF)
1513 "\b" => '\b', # backspace (BS)
1514 "\a" => '\a', # alarm (bell) (BEL)
1515 "\e" => '\e', # escape (ESC)
1516 "\013" => '\v', # vertical tab (VT)
1517 "\000" => '\0', # nul character (NUL)
1519 my $chr = ( (exists $es{$cntrl})
1521 : sprintf('\%2x', ord($cntrl)) );
1522 if ($opts{-nohtml}) {
1525 return "<span class=\"cntrl\">$chr</span>";
1529 # Alternatively use unicode control pictures codepoints,
1530 # Unicode "printable representation" (PR)
1535 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1536 if ($opts{-nohtml}) {
1539 return "<span class=\"cntrl\">$chr</span>";
1543 # git may return quoted and escaped filenames
1549 my %es = ( # character escape codes, aka escape sequences
1550 't' => "\t", # tab (HT, TAB)
1551 'n' => "\n", # newline (NL)
1552 'r' => "\r", # return (CR)
1553 'f' => "\f", # form feed (FF)
1554 'b' => "\b", # backspace (BS)
1555 'a' => "\a", # alarm (bell) (BEL)
1556 'e' => "\e", # escape (ESC)
1557 'v' => "\013", # vertical tab (VT)
1560 if ($seq =~ m/^[0-7]{1,3}$/) {
1561 # octal char sequence
1562 return chr(oct($seq));
1563 } elsif (exists $es{$seq}) {
1564 # C escape sequence, aka character escape code
1567 # quoted ordinary character
1571 if ($str =~ m/^"(.*)"$/) {
1574 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1579 # escape tabs (convert tabs to spaces)
1583 while ((my $pos = index($line, "\t")) != -1) {
1584 if (my $count = (8 - ($pos % 8))) {
1585 my $spaces = ' ' x $count;
1586 $line =~ s/\t/$spaces/;
1593 sub project_in_list {
1594 my $project = shift;
1595 my @list = git_get_projects_list();
1596 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1599 ## ----------------------------------------------------------------------
1600 ## HTML aware string manipulation
1602 # Try to chop given string on a word boundary between position
1603 # $len and $len+$add_len. If there is no word boundary there,
1604 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1605 # (marking chopped part) would be longer than given string.
1609 my $add_len = shift || 10;
1610 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1612 # Make sure perl knows it is utf8 encoded so we don't
1613 # cut in the middle of a utf8 multibyte char.
1614 $str = to_utf8($str);
1616 # allow only $len chars, but don't cut a word if it would fit in $add_len
1617 # if it doesn't fit, cut it if it's still longer than the dots we would add
1618 # remove chopped character entities entirely
1620 # when chopping in the middle, distribute $len into left and right part
1621 # return early if chopping wouldn't make string shorter
1622 if ($where eq 'center') {
1623 return $str if ($len + 5 >= length($str)); # filler is length 5
1626 return $str if ($len + 4 >= length($str)); # filler is length 4
1629 # regexps: ending and beginning with word part up to $add_len
1630 my $endre = qr/.{$len}\w{0,$add_len}/;
1631 my $begre = qr/\w{0,$add_len}.{$len}/;
1633 if ($where eq 'left') {
1634 $str =~ m/^(.*?)($begre)$/;
1635 my ($lead, $body) = ($1, $2);
1636 if (length($lead) > 4) {
1639 return "$lead$body";
1641 } elsif ($where eq 'center') {
1642 $str =~ m/^($endre)(.*)$/;
1643 my ($left, $str) = ($1, $2);
1644 $str =~ m/^(.*?)($begre)$/;
1645 my ($mid, $right) = ($1, $2);
1646 if (length($mid) > 5) {
1649 return "$left$mid$right";
1652 $str =~ m/^($endre)(.*)$/;
1655 if (length($tail) > 4) {
1658 return "$body$tail";
1662 # takes the same arguments as chop_str, but also wraps a <span> around the
1663 # result with a title attribute if it does get chopped. Additionally, the
1664 # string is HTML-escaped.
1665 sub chop_and_escape_str {
1668 my $chopped = chop_str(@_);
1669 if ($chopped eq $str) {
1670 return esc_html($chopped);
1672 $str =~ s/[[:cntrl:]]/?/g;
1673 return $cgi->span({-title=>$str}, esc_html($chopped));
1677 ## ----------------------------------------------------------------------
1678 ## functions returning short strings
1680 # CSS class for given age value (in seconds)
1684 if (!defined $age) {
1686 } elsif ($age < 60*60*2) {
1688 } elsif ($age < 60*60*24*2) {
1695 # convert age in seconds to "nn units ago" string
1700 if ($age > 60*60*24*365*2) {
1701 $age_str = (int $age/60/60/24/365);
1702 $age_str .= " years ago";
1703 } elsif ($age > 60*60*24*(365/12)*2) {
1704 $age_str = int $age/60/60/24/(365/12);
1705 $age_str .= " months ago";
1706 } elsif ($age > 60*60*24*7*2) {
1707 $age_str = int $age/60/60/24/7;
1708 $age_str .= " weeks ago";
1709 } elsif ($age > 60*60*24*2) {
1710 $age_str = int $age/60/60/24;
1711 $age_str .= " days ago";
1712 } elsif ($age > 60*60*2) {
1713 $age_str = int $age/60/60;
1714 $age_str .= " hours ago";
1715 } elsif ($age > 60*2) {
1716 $age_str = int $age/60;
1717 $age_str .= " min ago";
1718 } elsif ($age > 2) {
1719 $age_str = int $age;
1720 $age_str .= " sec ago";
1722 $age_str .= " right now";
1728 S_IFINVALID => 0030000,
1729 S_IFGITLINK => 0160000,
1732 # submodule/subproject, a commit object reference
1736 return (($mode & S_IFMT) == S_IFGITLINK)
1739 # convert file mode in octal to symbolic file mode string
1741 my $mode = oct shift;
1743 if (S_ISGITLINK($mode)) {
1744 return 'm---------';
1745 } elsif (S_ISDIR($mode & S_IFMT)) {
1746 return 'drwxr-xr-x';
1747 } elsif (S_ISLNK($mode)) {
1748 return 'lrwxrwxrwx';
1749 } elsif (S_ISREG($mode)) {
1750 # git cares only about the executable bit
1751 if ($mode & S_IXUSR) {
1752 return '-rwxr-xr-x';
1754 return '-rw-r--r--';
1757 return '----------';
1761 # convert file mode in octal to file type string
1765 if ($mode !~ m/^[0-7]+$/) {
1771 if (S_ISGITLINK($mode)) {
1773 } elsif (S_ISDIR($mode & S_IFMT)) {
1775 } elsif (S_ISLNK($mode)) {
1777 } elsif (S_ISREG($mode)) {
1784 # convert file mode in octal to file type description string
1785 sub file_type_long {
1788 if ($mode !~ m/^[0-7]+$/) {
1794 if (S_ISGITLINK($mode)) {
1796 } elsif (S_ISDIR($mode & S_IFMT)) {
1798 } elsif (S_ISLNK($mode)) {
1800 } elsif (S_ISREG($mode)) {
1801 if ($mode & S_IXUSR) {
1802 return "executable";
1812 ## ----------------------------------------------------------------------
1813 ## functions returning short HTML fragments, or transforming HTML fragments
1814 ## which don't belong to other sections
1816 # format line of commit message.
1817 sub format_log_line_html {
1820 $line = esc_html($line, -nbsp=>1);
1821 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1822 $cgi->a({-href => href(action=>"object", hash=>$1),
1823 -class => "text"}, $1);
1829 # format marker of refs pointing to given object
1831 # the destination action is chosen based on object type and current context:
1832 # - for annotated tags, we choose the tag view unless it's the current view
1833 # already, in which case we go to shortlog view
1834 # - for other refs, we keep the current view if we're in history, shortlog or
1835 # log view, and select shortlog otherwise
1836 sub format_ref_marker {
1837 my ($refs, $id) = @_;
1840 if (defined $refs->{$id}) {
1841 foreach my $ref (@{$refs->{$id}}) {
1842 # this code exploits the fact that non-lightweight tags are the
1843 # only indirect objects, and that they are the only objects for which
1844 # we want to use tag instead of shortlog as action
1845 my ($type, $name) = qw();
1846 my $indirect = ($ref =~ s/\^\{\}$//);
1847 # e.g. tags/v2.6.11 or heads/next
1848 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1857 $class .= " indirect" if $indirect;
1859 my $dest_action = "shortlog";
1862 $dest_action = "tag" unless $action eq "tag";
1863 } elsif ($action =~ /^(history|(short)?log)$/) {
1864 $dest_action = $action;
1868 $dest .= "refs/" unless $ref =~ m!^refs/!;
1871 my $link = $cgi->a({
1873 action=>$dest_action,
1877 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
1883 return ' <span class="refs">'. $markers . '</span>';
1889 # format, perhaps shortened and with markers, title line
1890 sub format_subject_html {
1891 my ($long, $short, $href, $extra) = @_;
1892 $extra = '' unless defined($extra);
1894 if (length($short) < length($long)) {
1895 $long =~ s/[[:cntrl:]]/?/g;
1896 return $cgi->a({-href => $href, -class => "list subject",
1897 -title => to_utf8($long)},
1898 esc_html($short)) . $extra;
1900 return $cgi->a({-href => $href, -class => "list subject"},
1901 esc_html($long)) . $extra;
1905 # Rather than recomputing the url for an email multiple times, we cache it
1906 # after the first hit. This gives a visible benefit in views where the avatar
1907 # for the same email is used repeatedly (e.g. shortlog).
1908 # The cache is shared by all avatar engines (currently gravatar only), which
1909 # are free to use it as preferred. Since only one avatar engine is used for any
1910 # given page, there's no risk for cache conflicts.
1911 our %avatar_cache = ();
1913 # Compute the picon url for a given email, by using the picon search service over at
1914 # http://www.cs.indiana.edu/picons/search.html
1916 my $email = lc shift;
1917 if (!$avatar_cache{$email}) {
1918 my ($user, $domain) = split('@', $email);
1919 $avatar_cache{$email} =
1920 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1922 "users+domains+unknown/up/single";
1924 return $avatar_cache{$email};
1927 # Compute the gravatar url for a given email, if it's not in the cache already.
1928 # Gravatar stores only the part of the URL before the size, since that's the
1929 # one computationally more expensive. This also allows reuse of the cache for
1930 # different sizes (for this particular engine).
1932 my $email = lc shift;
1934 $avatar_cache{$email} ||=
1935 "http://www.gravatar.com/avatar/" .
1936 Digest::MD5::md5_hex($email) . "?s=";
1937 return $avatar_cache{$email} . $size;
1940 # Insert an avatar for the given $email at the given $size if the feature
1942 sub git_get_avatar {
1943 my ($email, %opts) = @_;
1944 my $pre_white = ($opts{-pad_before} ? " " : "");
1945 my $post_white = ($opts{-pad_after} ? " " : "");
1946 $opts{-size} ||= 'default';
1947 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1949 if ($git_avatar eq 'gravatar') {
1950 $url = gravatar_url($email, $size);
1951 } elsif ($git_avatar eq 'picon') {
1952 $url = picon_url($email);
1954 # Other providers can be added by extending the if chain, defining $url
1955 # as needed. If no variant puts something in $url, we assume avatars
1956 # are completely disabled/unavailable.
1959 "<img width=\"$size\" " .
1960 "class=\"avatar\" " .
1961 "src=\"".esc_url($url)."\" " .
1969 sub format_search_author {
1970 my ($author, $searchtype, $displaytext) = @_;
1971 my $have_search = gitweb_check_feature('search');
1975 if ($searchtype eq 'author') {
1976 $performed = "authored";
1977 } elsif ($searchtype eq 'committer') {
1978 $performed = "committed";
1981 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1982 searchtext=>$author,
1983 searchtype=>$searchtype), class=>"list",
1984 title=>"Search for commits $performed by $author"},
1988 return $displaytext;
1992 # format the author name of the given commit with the given tag
1993 # the author name is chopped and escaped according to the other
1994 # optional parameters (see chop_str).
1995 sub format_author_html {
1998 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1999 return "<$tag class=\"author\">" .
2000 format_search_author($co->{'author_name'}, "author",
2001 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
2006 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
2007 sub format_git_diff_header_line {
2009 my $diffinfo = shift;
2010 my ($from, $to) = @_;
2012 if ($diffinfo->{'nparents'}) {
2014 $line =~ s!^(diff (.*?) )"?.*$!$1!;
2015 if ($to->{'href'}) {
2016 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2017 esc_path($to->{'file'}));
2018 } else { # file was deleted (no href)
2019 $line .= esc_path($to->{'file'});
2023 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2024 if ($from->{'href'}) {
2025 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
2026 'a/' . esc_path($from->{'file'}));
2027 } else { # file was added (no href)
2028 $line .= 'a/' . esc_path($from->{'file'});
2031 if ($to->{'href'}) {
2032 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
2033 'b/' . esc_path($to->{'file'}));
2034 } else { # file was deleted
2035 $line .= 'b/' . esc_path($to->{'file'});
2039 return "<div class=\"diff header\">$line</div>\n";
2042 # format extended diff header line, before patch itself
2043 sub format_extended_diff_header_line {
2045 my $diffinfo = shift;
2046 my ($from, $to) = @_;
2049 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
2050 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2051 esc_path($from->{'file'}));
2053 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
2054 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2055 esc_path($to->{'file'}));
2057 # match single <mode>
2058 if ($line =~ m/\s(\d{6})$/) {
2059 $line .= '<span class="info"> (' .
2060 file_type_long($1) .
2064 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
2065 # can match only for combined diff
2067 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2068 if ($from->{'href'}[$i]) {
2069 $line .= $cgi->a({-href=>$from->{'href'}[$i],
2071 substr($diffinfo->{'from_id'}[$i],0,7));
2076 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
2079 if ($to->{'href'}) {
2080 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2081 substr($diffinfo->{'to_id'},0,7));
2086 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
2087 # can match only for ordinary diff
2088 my ($from_link, $to_link);
2089 if ($from->{'href'}) {
2090 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
2091 substr($diffinfo->{'from_id'},0,7));
2093 $from_link = '0' x 7;
2095 if ($to->{'href'}) {
2096 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
2097 substr($diffinfo->{'to_id'},0,7));
2101 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2102 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2105 return $line . "<br/>\n";
2108 # format from-file/to-file diff header
2109 sub format_diff_from_to_header {
2110 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2115 #assert($line =~ m/^---/) if DEBUG;
2116 # no extra formatting for "^--- /dev/null"
2117 if (! $diffinfo->{'nparents'}) {
2118 # ordinary (single parent) diff
2119 if ($line =~ m!^--- "?a/!) {
2120 if ($from->{'href'}) {
2122 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2123 esc_path($from->{'file'}));
2126 esc_path($from->{'file'});
2129 $result .= qq!<div class="diff from_file">$line</div>\n!;
2132 # combined diff (merge commit)
2133 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2134 if ($from->{'href'}[$i]) {
2136 $cgi->a({-href=>href(action=>"blobdiff",
2137 hash_parent=>$diffinfo->{'from_id'}[$i],
2138 hash_parent_base=>$parents[$i],
2139 file_parent=>$from->{'file'}[$i],
2140 hash=>$diffinfo->{'to_id'},
2142 file_name=>$to->{'file'}),
2144 -title=>"diff" . ($i+1)},
2147 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2148 esc_path($from->{'file'}[$i]));
2150 $line = '--- /dev/null';
2152 $result .= qq!<div class="diff from_file">$line</div>\n!;
2157 #assert($line =~ m/^\+\+\+/) if DEBUG;
2158 # no extra formatting for "^+++ /dev/null"
2159 if ($line =~ m!^\+\+\+ "?b/!) {
2160 if ($to->{'href'}) {
2162 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2163 esc_path($to->{'file'}));
2166 esc_path($to->{'file'});
2169 $result .= qq!<div class="diff to_file">$line</div>\n!;
2174 # create note for patch simplified by combined diff
2175 sub format_diff_cc_simplified {
2176 my ($diffinfo, @parents) = @_;
2179 $result .= "<div class=\"diff header\">" .
2181 if (!is_deleted($diffinfo)) {
2182 $result .= $cgi->a({-href => href(action=>"blob",
2184 hash=>$diffinfo->{'to_id'},
2185 file_name=>$diffinfo->{'to_file'}),
2187 esc_path($diffinfo->{'to_file'}));
2189 $result .= esc_path($diffinfo->{'to_file'});
2191 $result .= "</div>\n" . # class="diff header"
2192 "<div class=\"diff nodifferences\">" .
2194 "</div>\n"; # class="diff nodifferences"
2199 # format patch (diff) line (not to be used for diff headers)
2200 sub format_diff_line {
2202 my ($from, $to) = @_;
2203 my $diff_class = "";
2207 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2209 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2210 if ($line =~ m/^\@{3}/) {
2211 $diff_class = " chunk_header";
2212 } elsif ($line =~ m/^\\/) {
2213 $diff_class = " incomplete";
2214 } elsif ($prefix =~ tr/+/+/) {
2215 $diff_class = " add";
2216 } elsif ($prefix =~ tr/-/-/) {
2217 $diff_class = " rem";
2220 # assume ordinary diff
2221 my $char = substr($line, 0, 1);
2223 $diff_class = " add";
2224 } elsif ($char eq '-') {
2225 $diff_class = " rem";
2226 } elsif ($char eq '@') {
2227 $diff_class = " chunk_header";
2228 } elsif ($char eq "\\") {
2229 $diff_class = " incomplete";
2232 $line = untabify($line);
2233 if ($from && $to && $line =~ m/^\@{2} /) {
2234 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2235 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2237 $from_lines = 0 unless defined $from_lines;
2238 $to_lines = 0 unless defined $to_lines;
2240 if ($from->{'href'}) {
2241 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2242 -class=>"list"}, $from_text);
2244 if ($to->{'href'}) {
2245 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2246 -class=>"list"}, $to_text);
2248 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2249 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2250 return "<div class=\"diff$diff_class\">$line</div>\n";
2251 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2252 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2253 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2255 @from_text = split(' ', $ranges);
2256 for (my $i = 0; $i < @from_text; ++$i) {
2257 ($from_start[$i], $from_nlines[$i]) =
2258 (split(',', substr($from_text[$i], 1)), 0);
2261 $to_text = pop @from_text;
2262 $to_start = pop @from_start;
2263 $to_nlines = pop @from_nlines;
2265 $line = "<span class=\"chunk_info\">$prefix ";
2266 for (my $i = 0; $i < @from_text; ++$i) {
2267 if ($from->{'href'}[$i]) {
2268 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2269 -class=>"list"}, $from_text[$i]);
2271 $line .= $from_text[$i];
2275 if ($to->{'href'}) {
2276 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2277 -class=>"list"}, $to_text);
2281 $line .= " $prefix</span>" .
2282 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2283 return "<div class=\"diff$diff_class\">$line</div>\n";
2285 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
2288 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2289 # linked. Pass the hash of the tree/commit to snapshot.
2290 sub format_snapshot_links {
2292 my $num_fmts = @snapshot_fmts;
2293 if ($num_fmts > 1) {
2294 # A parenthesized list of links bearing format names.
2295 # e.g. "snapshot (_tar.gz_ _zip_)"
2296 return "snapshot (" . join(' ', map
2303 }, $known_snapshot_formats{$_}{'display'})
2304 , @snapshot_fmts) . ")";
2305 } elsif ($num_fmts == 1) {
2306 # A single "snapshot" link whose tooltip bears the format name.
2308 my ($fmt) = @snapshot_fmts;
2314 snapshot_format=>$fmt
2316 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2318 } else { # $num_fmts == 0
2323 ## ......................................................................
2324 ## functions returning values to be passed, perhaps after some
2325 ## transformation, to other functions; e.g. returning arguments to href()
2327 # returns hash to be passed to href to generate gitweb URL
2328 # in -title key it returns description of link
2330 my $format = shift || 'Atom';
2331 my %res = (action => lc($format));
2333 # feed links are possible only for project views
2334 return unless (defined $project);
2335 # some views should link to OPML, or to generic project feed,
2336 # or don't have specific feed yet (so they should use generic)
2337 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2340 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2341 # from tag links; this also makes possible to detect branch links
2342 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2343 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2346 # find log type for feed description (title)
2348 if (defined $file_name) {
2349 $type = "history of $file_name";
2350 $type .= "/" if ($action eq 'tree');
2351 $type .= " on '$branch'" if (defined $branch);
2353 $type = "log of $branch" if (defined $branch);
2356 $res{-title} = $type;
2357 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2358 $res{'file_name'} = $file_name;
2363 ## ----------------------------------------------------------------------
2364 ## git utility subroutines, invoking git commands
2366 # returns path to the core git executable and the --git-dir parameter as list
2368 $number_of_git_cmds++;
2369 return $GIT, '--git-dir='.$git_dir;
2372 # quote the given arguments for passing them to the shell
2373 # quote_command("command", "arg 1", "arg with ' and ! characters")
2374 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2375 # Try to avoid using this function wherever possible.
2378 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2381 # get HEAD ref of given project as hash
2382 sub git_get_head_hash {
2383 return git_get_full_hash(shift, 'HEAD');
2386 sub git_get_full_hash {
2387 return git_get_hash(@_);
2390 sub git_get_short_hash {
2391 return git_get_hash(@_, '--short=7');
2395 my ($project, $hash, @options) = @_;
2396 my $o_git_dir = $git_dir;
2398 $git_dir = "$projectroot/$project";
2399 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2400 '--verify', '-q', @options, $hash) {
2402 chomp $retval if defined $retval;
2405 if (defined $o_git_dir) {
2406 $git_dir = $o_git_dir;
2411 # get type of given object
2415 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2417 close $fd or return;
2422 # repository configuration
2423 our $config_file = '';
2426 # store multiple values for single key as anonymous array reference
2427 # single values stored directly in the hash, not as [ <value> ]
2428 sub hash_set_multi {
2429 my ($hash, $key, $value) = @_;
2431 if (!exists $hash->{$key}) {
2432 $hash->{$key} = $value;
2433 } elsif (!ref $hash->{$key}) {
2434 $hash->{$key} = [ $hash->{$key}, $value ];
2436 push @{$hash->{$key}}, $value;
2440 # return hash of git project configuration
2441 # optionally limited to some section, e.g. 'gitweb'
2442 sub git_parse_project_config {
2443 my $section_regexp = shift;
2448 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2451 while (my $keyval = <$fh>) {
2453 my ($key, $value) = split(/\n/, $keyval, 2);
2455 hash_set_multi(\%config, $key, $value)
2456 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2463 # convert config value to boolean: 'true' or 'false'
2464 # no value, number > 0, 'true' and 'yes' values are true
2465 # rest of values are treated as false (never as error)
2466 sub config_to_bool {
2469 return 1 if !defined $val; # section.key
2471 # strip leading and trailing whitespace
2475 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2476 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2479 # convert config value to simple decimal number
2480 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2481 # to be multiplied by 1024, 1048576, or 1073741824
2485 # strip leading and trailing whitespace
2489 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2491 # unknown unit is treated as 1
2492 return $num * ($unit eq 'g' ? 1073741824 :
2493 $unit eq 'm' ? 1048576 :
2494 $unit eq 'k' ? 1024 : 1);
2499 # convert config value to array reference, if needed
2500 sub config_to_multi {
2503 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2506 sub git_get_project_config {
2507 my ($key, $type) = @_;
2509 return unless defined $git_dir;
2512 return unless ($key);
2513 $key =~ s/^gitweb\.//;
2514 return if ($key =~ m/\W/);
2517 if (defined $type) {
2520 unless ($type eq 'bool' || $type eq 'int');
2524 if (!defined $config_file ||
2525 $config_file ne "$git_dir/config") {
2526 %config = git_parse_project_config('gitweb');
2527 $config_file = "$git_dir/config";
2530 # check if config variable (key) exists
2531 return unless exists $config{"gitweb.$key"};
2534 if (!defined $type) {
2535 return $config{"gitweb.$key"};
2536 } elsif ($type eq 'bool') {
2537 # backward compatibility: 'git config --bool' returns true/false
2538 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2539 } elsif ($type eq 'int') {
2540 return config_to_int($config{"gitweb.$key"});
2542 return $config{"gitweb.$key"};
2545 # get hash of given path at given ref
2546 sub git_get_hash_by_path {
2548 my $path = shift || return undef;
2553 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2554 or die_error(500, "Open git-ls-tree failed");
2556 close $fd or return undef;
2558 if (!defined $line) {
2559 # there is no tree or hash given by $path at $base
2563 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2564 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2565 if (defined $type && $type ne $2) {
2566 # type doesn't match
2572 # get path of entry with given hash at given tree-ish (ref)
2573 # used to get 'from' filename for combined diff (merge commit) for renames
2574 sub git_get_path_by_hash {
2575 my $base = shift || return;
2576 my $hash = shift || return;
2580 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2582 while (my $line = <$fd>) {
2585 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2586 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2587 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2596 ## ......................................................................
2597 ## git utility functions, directly accessing git repository
2599 # get the value of config variable either from file named as the variable
2600 # itself in the repository ($GIT_DIR/$name file), or from gitweb.$name
2601 # configuration variable in the repository config file.
2602 sub git_get_file_or_project_config {
2603 my ($path, $name) = @_;
2605 $git_dir = "$projectroot/$path";
2606 open my $fd, '<', "$git_dir/$name"
2607 or return git_get_project_config($name);
2610 if (defined $conf) {
2616 sub git_get_project_description {
2618 return git_get_file_or_project_config($path, 'description');
2621 sub git_get_project_category {
2623 return git_get_file_or_project_config($path, 'category');
2627 # supported formats:
2628 # * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)
2629 # - if its contents is a number, use it as tag weight,
2630 # - otherwise add a tag with weight 1
2631 # * $GIT_DIR/ctags file, each line is a tag (with weight 1)
2632 # the same value multiple times increases tag weight
2633 # * `gitweb.ctag' multi-valued repo config variable
2634 sub git_get_project_ctags {
2635 my $project = shift;
2638 $git_dir = "$projectroot/$project";
2639 if (opendir my $dh, "$git_dir/ctags") {
2640 my @files = grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh);
2641 foreach my $tagfile (@files) {
2642 open my $ct, '<', $tagfile
2648 (my $ctag = $tagfile) =~ s#.*/##;
2649 if ($val =~ /^\d+$/) {
2650 $ctags->{$ctag} = $val;
2652 $ctags->{$ctag} = 1;
2657 } elsif (open my $fh, '<', "$git_dir/ctags") {
2658 while (my $line = <$fh>) {
2660 $ctags->{$line}++ if $line;
2665 my $taglist = config_to_multi(git_get_project_config('ctag'));
2666 foreach my $tag (@$taglist) {
2674 # return hash, where keys are content tags ('ctags'),
2675 # and values are sum of weights of given tag in every project
2676 sub git_gather_all_ctags {
2677 my $projects = shift;
2680 foreach my $p (@$projects) {
2681 foreach my $ct (keys %{$p->{'ctags'}}) {
2682 $ctags->{$ct} += $p->{'ctags'}->{$ct};
2689 sub git_populate_project_tagcloud {
2692 # First, merge different-cased tags; tags vote on casing
2694 foreach (keys %$ctags) {
2695 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2696 if (not $ctags_lc{lc $_}->{topcount}
2697 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2698 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2699 $ctags_lc{lc $_}->{topname} = $_;
2704 my $matched = $cgi->param('by_tag');
2705 if (eval { require HTML::TagCloud; 1; }) {
2706 $cloud = HTML::TagCloud->new;
2707 foreach my $ctag (sort keys %ctags_lc) {
2708 # Pad the title with spaces so that the cloud looks
2710 my $title = esc_html($ctags_lc{$ctag}->{topname});
2711 $title =~ s/ / /g;
2712 $title =~ s/^/ /g;
2713 $title =~ s/$/ /g;
2714 if (defined $matched && $matched eq $ctag) {
2715 $title = qq(<span class="match">$title</span>);
2717 $cloud->add($title, href(project=>undef, ctag=>$ctag),
2718 $ctags_lc{$ctag}->{count});
2722 foreach my $ctag (keys %ctags_lc) {
2723 my $title = esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);
2724 if (defined $matched && $matched eq $ctag) {
2725 $title = qq(<span class="match">$title</span>);
2727 $cloud->{$ctag}{count} = $ctags_lc{$ctag}->{count};
2728 $cloud->{$ctag}{ctag} =
2729 $cgi->a({-href=>href(project=>undef, ctag=>$ctag)}, $title);
2735 sub git_show_project_tagcloud {
2736 my ($cloud, $count) = @_;
2737 if (ref $cloud eq 'HTML::TagCloud') {
2738 return $cloud->html_and_css($count);
2740 my @tags = sort { $cloud->{$a}->{'count'} <=> $cloud->{$b}->{'count'} } keys %$cloud;
2742 '<div id="htmltagcloud"'.($project ? '' : ' align="center"').'>' .
2744 $cloud->{$_}->{'ctag'}
2745 } splice(@tags, 0, $count)) .
2750 sub git_get_project_url_list {
2753 $git_dir = "$projectroot/$path";
2754 open my $fd, '<', "$git_dir/cloneurl"
2755 or return wantarray ?
2756 @{ config_to_multi(git_get_project_config('url')) } :
2757 config_to_multi(git_get_project_config('url'));
2758 my @git_project_url_list = map { chomp; $_ } <$fd>;
2761 return wantarray ? @git_project_url_list : \@git_project_url_list;
2764 sub git_get_projects_list {
2765 my $filter = shift || '';
2768 $filter =~ s/\.git$//;
2770 if (-d $projects_list) {
2771 # search in directory
2772 my $dir = $projects_list;
2773 # remove the trailing "/"
2775 my $pfxlen = length("$projects_list");
2776 my $pfxdepth = ($projects_list =~ tr!/!!);
2777 # when filtering, search only given subdirectory
2784 follow_fast => 1, # follow symbolic links
2785 follow_skip => 2, # ignore duplicates
2786 dangling_symlinks => 0, # ignore dangling symlinks, silently
2789 our $project_maxdepth;
2791 # skip project-list toplevel, if we get it.
2792 return if (m!^[/.]$!);
2793 # only directories can be git repositories
2794 return unless (-d $_);
2795 # don't traverse too deep (Find is super slow on os x)
2796 # $project_maxdepth excludes depth of $projectroot
2797 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2798 $File::Find::prune = 1;
2802 my $path = substr($File::Find::name, $pfxlen + 1);
2803 # we check related file in $projectroot
2804 if (check_export_ok("$projectroot/$path")) {
2805 push @list, { path => $path };
2806 $File::Find::prune = 1;
2811 } elsif (-f $projects_list) {
2812 # read from file(url-encoded):
2813 # 'git%2Fgit.git Linus+Torvalds'
2814 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2815 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2816 open my $fd, '<', $projects_list or return;
2818 while (my $line = <$fd>) {
2820 my ($path, $owner) = split ' ', $line;
2821 $path = unescape($path);
2822 $owner = unescape($owner);
2823 if (!defined $path) {
2826 # if $filter is rpovided, check if $path begins with $filter
2827 if ($filter && $path !~ m!^\Q$filter\E/!) {
2830 if (check_export_ok("$projectroot/$path")) {
2833 owner => to_utf8($owner),
2843 # written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)
2844 # as side effects it sets 'forks' field to list of forks for forked projects
2845 sub filter_forks_from_projects_list {
2846 my $projects = shift;
2848 my %trie; # prefix tree of directories (path components)
2849 # generate trie out of those directories that might contain forks
2850 foreach my $pr (@$projects) {
2851 my $path = $pr->{'path'};
2852 $path =~ s/\.git$//; # forks of 'repo.git' are in 'repo/' directory
2853 next if ($path =~ m!/$!); # skip non-bare repositories, e.g. 'repo/.git'
2854 next unless ($path); # skip '.git' repository: tests, git-instaweb
2855 next unless (-d $path); # containing directory exists
2856 $pr->{'forks'} = []; # there can be 0 or more forks of project
2859 my @dirs = split('/', $path);
2860 # walk the trie, until either runs out of components or out of trie
2862 while (scalar @dirs &&
2863 exists($ref->{$dirs[0]})) {
2864 $ref = $ref->{shift @dirs};
2866 # create rest of trie structure from rest of components
2867 foreach my $dir (@dirs) {
2868 $ref = $ref->{$dir} = {};
2870 # create end marker, store $pr as a data
2871 $ref->{''} = $pr if (!exists $ref->{''});
2874 # filter out forks, by finding shortest prefix match for paths
2877 foreach my $pr (@$projects) {
2881 foreach my $dir (split('/', $pr->{'path'})) {
2882 if (exists $ref->{''}) {
2883 # found [shortest] prefix, is a fork - skip it
2884 push @{$ref->{''}{'forks'}}, $pr;
2887 if (!exists $ref->{$dir}) {
2888 # not in trie, cannot have prefix, not a fork
2889 push @filtered, $pr;
2892 # If the dir is there, we just walk one step down the trie.
2893 $ref = $ref->{$dir};
2895 # we ran out of trie
2896 # (shouldn't happen: it's either no match, or end marker)
2897 push @filtered, $pr;
2903 # note: fill_project_list_info must be run first,
2904 # for 'descr_long' and 'ctags' to be filled
2905 sub search_projects_list {
2906 my ($projlist, %opts) = @_;
2907 my $tagfilter = $opts{'tagfilter'};
2908 my $searchtext = $opts{'searchtext'};
2911 unless ($tagfilter || $searchtext);
2915 foreach my $pr (@$projlist) {
2918 next unless ref($pr->{'ctags'}) eq 'HASH';
2920 grep { lc($_) eq lc($tagfilter) } keys %{$pr->{'ctags'}};
2925 $pr->{'path'} =~ /$searchtext/ ||
2926 $pr->{'descr_long'} =~ /$searchtext/;
2929 push @projects, $pr;
2935 our $gitweb_project_owner = undef;
2936 sub git_get_project_list_from_file {
2938 return if (defined $gitweb_project_owner);
2940 $gitweb_project_owner = {};
2941 # read from file (url-encoded):
2942 # 'git%2Fgit.git Linus+Torvalds'
2943 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2944 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2945 if (-f $projects_list) {
2946 open(my $fd, '<', $projects_list);
2947 while (my $line = <$fd>) {
2949 my ($pr, $ow) = split ' ', $line;
2950 $pr = unescape($pr);
2951 $ow = unescape($ow);
2952 $gitweb_project_owner->{$pr} = to_utf8($ow);
2958 sub git_get_project_owner {
2959 my $project = shift;
2962 return undef unless $project;
2963 $git_dir = "$projectroot/$project";
2965 if (!defined $gitweb_project_owner) {
2966 git_get_project_list_from_file();
2969 if (exists $gitweb_project_owner->{$project}) {
2970 $owner = $gitweb_project_owner->{$project};
2972 if (!defined $owner){
2973 $owner = git_get_project_config('owner');
2975 if (!defined $owner) {
2976 $owner = get_file_owner("$git_dir");
2982 sub git_get_last_activity {
2986 $git_dir = "$projectroot/$path";
2987 open($fd, "-|", git_cmd(), 'for-each-ref',
2988 '--format=%(committer)',
2989 '--sort=-committerdate',
2991 'refs/heads') or return;
2992 my $most_recent = <$fd>;
2993 close $fd or return;
2994 if (defined $most_recent &&
2995 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2997 my $age = time - $timestamp;
2998 return ($age, age_string($age));
3000 return (undef, undef);
3003 # Implementation note: when a single remote is wanted, we cannot use 'git
3004 # remote show -n' because that command always work (assuming it's a remote URL
3005 # if it's not defined), and we cannot use 'git remote show' because that would
3006 # try to make a network roundtrip. So the only way to find if that particular
3007 # remote is defined is to walk the list provided by 'git remote -v' and stop if
3008 # and when we find what we want.
3009 sub git_get_remotes_list {
3013 open my $fd, '-|' , git_cmd(), 'remote', '-v';
3015 while (my $remote = <$fd>) {
3017 $remote =~ s!\t(.*?)\s+\((\w+)\)$!!;
3018 next if $wanted and not $remote eq $wanted;
3019 my ($url, $key) = ($1, $2);
3021 $remotes{$remote} ||= { 'heads' => () };
3022 $remotes{$remote}{$key} = $url;
3024 close $fd or return;
3025 return wantarray ? %remotes : \%remotes;
3028 # Takes a hash of remotes as first parameter and fills it by adding the
3029 # available remote heads for each of the indicated remotes.
3030 sub fill_remote_heads {
3031 my $remotes = shift;
3032 my @heads = map { "remotes/$_" } keys %$remotes;
3033 my @remoteheads = git_get_heads_list(undef, @heads);
3034 foreach my $remote (keys %$remotes) {
3035 $remotes->{$remote}{'heads'} = [ grep {
3036 $_->{'name'} =~ s!^$remote/!!
3041 sub git_get_references {
3042 my $type = shift || "";
3044 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
3045 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
3046 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
3047 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
3050 while (my $line = <$fd>) {
3052 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
3053 if (defined $refs{$1}) {
3054 push @{$refs{$1}}, $2;
3060 close $fd or return;
3064 sub git_get_rev_name_tags {
3065 my $hash = shift || return undef;
3067 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
3069 my $name_rev = <$fd>;
3072 if ($name_rev =~ m|^$hash tags/(.*)$|) {
3075 # catches also '$hash undefined' output
3080 ## ----------------------------------------------------------------------
3081 ## parse to hash functions
3085 my $tz = shift || "-0000";
3088 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
3089 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
3090 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
3091 $date{'hour'} = $hour;
3092 $date{'minute'} = $min;
3093 $date{'mday'} = $mday;
3094 $date{'day'} = $days[$wday];
3095 $date{'month'} = $months[$mon];
3096 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
3097 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
3098 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
3099 $mday, $months[$mon], $hour ,$min;
3100 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
3101 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
3103 my ($tz_sign, $tz_hour, $tz_min) =
3104 ($tz =~ m/^([-+])(\d\d)(\d\d)$/);
3105 $tz_sign = ($tz_sign eq '-' ? -1 : +1);
3106 my $local = $epoch + $tz_sign*((($tz_hour*60) + $tz_min)*60);
3107 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
3108 $date{'hour_local'} = $hour;
3109 $date{'minute_local'} = $min;
3110 $date{'tz_local'} = $tz;
3111 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
3112 1900+$year, $mon+1, $mday,
3113 $hour, $min, $sec, $tz);
3122 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
3123 $tag{'id'} = $tag_id;
3124 while (my $line = <$fd>) {
3126 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
3127 $tag{'object'} = $1;
3128 } elsif ($line =~ m/^type (.+)$/) {
3130 } elsif ($line =~ m/^tag (.+)$/) {
3132 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
3133 $tag{'author'} = $1;
3134 $tag{'author_epoch'} = $2;
3135 $tag{'author_tz'} = $3;
3136 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3137 $tag{'author_name'} = $1;
3138 $tag{'author_email'} = $2;
3140 $tag{'author_name'} = $tag{'author'};
3142 } elsif ($line =~ m/--BEGIN/) {
3143 push @comment, $line;
3145 } elsif ($line eq "") {
3149 push @comment, <$fd>;
3150 $tag{'comment'} = \@comment;
3151 close $fd or return;
3152 if (!defined $tag{'name'}) {
3158 sub parse_commit_text {
3159 my ($commit_text, $withparents) = @_;
3160 my @commit_lines = split '\n', $commit_text;
3163 pop @commit_lines; # Remove '\0'
3165 if (! @commit_lines) {
3169 my $header = shift @commit_lines;
3170 if ($header !~ m/^[0-9a-fA-F]{40}/) {
3173 ($co{'id'}, my @parents) = split ' ', $header;
3174 while (my $line = shift @commit_lines) {
3175 last if $line eq "\n";
3176 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
3178 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
3180 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3181 $co{'author'} = to_utf8($1);
3182 $co{'author_epoch'} = $2;
3183 $co{'author_tz'} = $3;
3184 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
3185 $co{'author_name'} = $1;
3186 $co{'author_email'} = $2;
3188 $co{'author_name'} = $co{'author'};
3190 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
3191 $co{'committer'} = to_utf8($1);
3192 $co{'committer_epoch'} = $2;
3193 $co{'committer_tz'} = $3;
3194 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
3195 $co{'committer_name'} = $1;
3196 $co{'committer_email'} = $2;
3198 $co{'committer_name'} = $co{'committer'};
3202 if (!defined $co{'tree'}) {
3205 $co{'parents'} = \@parents;
3206 $co{'parent'} = $parents[0];
3208 foreach my $title (@commit_lines) {
3211 $co{'title'} = chop_str($title, 80, 5);
3212 # remove leading stuff of merges to make the interesting part visible
3213 if (length($title) > 50) {
3214 $title =~ s/^Automatic //;
3215 $title =~ s/^merge (of|with) /Merge ... /i;
3216 if (length($title) > 50) {
3217 $title =~ s/(http|rsync):\/\///;
3219 if (length($title) > 50) {
3220 $title =~ s/(master|www|rsync)\.//;
3222 if (length($title) > 50) {
3223 $title =~ s/kernel.org:?//;
3225 if (length($title) > 50) {
3226 $title =~ s/\/pub\/scm//;
3229 $co{'title_short'} = chop_str($title, 50, 5);
3233 if (! defined $co{'title'} || $co{'title'} eq "") {
3234 $co{'title'} = $co{'title_short'} = '(no commit message)';
3236 # remove added spaces
3237 foreach my $line (@commit_lines) {
3240 $co{'comment'} = \@commit_lines;
3242 my $age = time - $co{'committer_epoch'};
3244 $co{'age_string'} = age_string($age);
3245 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
3246 if ($age > 60*60*24*7*2) {
3247 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3248 $co{'age_string_age'} = $co{'age_string'};
3250 $co{'age_string_date'} = $co{'age_string'};
3251 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
3257 my ($commit_id) = @_;
3262 open my $fd, "-|", git_cmd(), "rev-list",
3268 or die_error(500, "Open git-rev-list failed");
3269 %co = parse_commit_text(<$fd>, 1);
3276 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
3284 open my $fd, "-|", git_cmd(), "rev-list",
3287 ("--max-count=" . $maxcount),
3288 ("--skip=" . $skip),
3292 ($filename ? ($filename) : ())
3293 or die_error(500, "Open git-rev-list failed");
3294 while (my $line = <$fd>) {
3295 my %co = parse_commit_text($line);
3300 return wantarray ? @cos : \@cos;
3303 # parse line of git-diff-tree "raw" output
3304 sub parse_difftree_raw_line {
3308 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3309 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3310 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3311 $res{'from_mode'} = $1;
3312 $res{'to_mode'} = $2;
3313 $res{'from_id'} = $3;
3315 $res{'status'} = $5;
3316 $res{'similarity'} = $6;
3317 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3318 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3320 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3323 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3324 # combined diff (for merge commit)
3325 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3326 $res{'nparents'} = length($1);
3327 $res{'from_mode'} = [ split(' ', $2) ];
3328 $res{'to_mode'} = pop @{$res{'from_mode'}};
3329 $res{'from_id'} = [ split(' ', $3) ];
3330 $res{'to_id'} = pop @{$res{'from_id'}};
3331 $res{'status'} = [ split('', $4) ];
3332 $res{'to_file'} = unquote($5);
3334 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3335 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3336 $res{'commit'} = $1;
3339 return wantarray ? %res : \%res;
3342 # wrapper: return parsed line of git-diff-tree "raw" output
3343 # (the argument might be raw line, or parsed info)
3344 sub parsed_difftree_line {
3345 my $line_or_ref = shift;
3347 if (ref($line_or_ref) eq "HASH") {
3348 # pre-parsed (or generated by hand)
3349 return $line_or_ref;
3351 return parse_difftree_raw_line($line_or_ref);
3355 # parse line of git-ls-tree output
3356 sub parse_ls_tree_line {
3362 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3363 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3372 $res{'name'} = unquote($5);
3375 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3376 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3384 $res{'name'} = unquote($4);
3388 return wantarray ? %res : \%res;
3391 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3392 sub parse_from_to_diffinfo {
3393 my ($diffinfo, $from, $to, @parents) = @_;
3395 if ($diffinfo->{'nparents'}) {
3397 $from->{'file'} = [];
3398 $from->{'href'} = [];
3399 fill_from_file_info($diffinfo, @parents)
3400 unless exists $diffinfo->{'from_file'};
3401 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3402 $from->{'file'}[$i] =
3403 defined $diffinfo->{'from_file'}[$i] ?
3404 $diffinfo->{'from_file'}[$i] :
3405 $diffinfo->{'to_file'};
3406 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3407 $from->{'href'}[$i] = href(action=>"blob",
3408 hash_base=>$parents[$i],
3409 hash=>$diffinfo->{'from_id'}[$i],
3410 file_name=>$from->{'file'}[$i]);
3412 $from->{'href'}[$i] = undef;
3416 # ordinary (not combined) diff
3417 $from->{'file'} = $diffinfo->{'from_file'};
3418 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3419 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3420 hash=>$diffinfo->{'from_id'},
3421 file_name=>$from->{'file'});
3423 delete $from->{'href'};
3427 $to->{'file'} = $diffinfo->{'to_file'};
3428 if (!is_deleted($diffinfo)) { # file exists in result
3429 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3430 hash=>$diffinfo->{'to_id'},
3431 file_name=>$to->{'file'});
3433 delete $to->{'href'};
3437 ## ......................................................................
3438 ## parse to array of hashes functions
3440 sub git_get_heads_list {
3441 my ($limit, @classes) = @_;
3442 @classes = ('heads') unless @classes;
3443 my @patterns = map { "refs/$_" } @classes;
3446 open my $fd, '-|', git_cmd(), 'for-each-ref',
3447 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3448 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3451 while (my $line = <$fd>) {
3455 my ($refinfo, $committerinfo) = split(/\0/, $line);
3456 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3457 my ($committer, $epoch, $tz) =
3458 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3459 $ref_item{'fullname'} = $name;
3460 $name =~ s!^refs/(?:head|remote)s/!!;
3462 $ref_item{'name'} = $name;
3463 $ref_item{'id'} = $hash;
3464 $ref_item{'title'} = $title || '(no commit message)';
3465 $ref_item{'epoch'} = $epoch;
3467 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3469 $ref_item{'age'} = "unknown";
3472 push @headslist, \%ref_item;
3476 return wantarray ? @headslist : \@headslist;
3479 sub git_get_tags_list {
3483 open my $fd, '-|', git_cmd(), 'for-each-ref',
3484 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3485 '--format=%(objectname) %(objecttype) %(refname) '.
3486 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3489 while (my $line = <$fd>) {
3493 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3494 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3495 my ($creator, $epoch, $tz) =
3496 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3497 $ref_item{'fullname'} = $name;
3498 $name =~ s!^refs/tags/!!;
3500 $ref_item{'type'} = $type;
3501 $ref_item{'id'} = $id;
3502 $ref_item{'name'} = $name;
3503 if ($type eq "tag") {
3504 $ref_item{'subject'} = $title;
3505 $ref_item{'reftype'} = $reftype;
3506 $ref_item{'refid'} = $refid;
3508 $ref_item{'reftype'} = $type;
3509 $ref_item{'refid'} = $id;
3512 if ($type eq "tag" || $type eq "commit") {
3513 $ref_item{'epoch'} = $epoch;
3515 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3517 $ref_item{'age'} = "unknown";
3521 push @tagslist, \%ref_item;
3525 return wantarray ? @tagslist : \@tagslist;
3528 ## ----------------------------------------------------------------------
3529 ## filesystem-related functions
3531 sub get_file_owner {
3534 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3535 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3536 if (!defined $gcos) {
3540 $owner =~ s/[,;].*$//;
3541 return to_utf8($owner);
3544 # assume that file exists
3546 my $filename = shift;
3548 open my $fd, '<', $filename;
3549 print map { to_utf8($_) } <$fd>;
3553 ## ......................................................................
3554 ## mimetype related functions
3556 sub mimetype_guess_file {
3557 my $filename = shift;
3558 my $mimemap = shift;
3559 -r $mimemap or return undef;
3562 open(my $mh, '<', $mimemap) or return undef;
3564 next if m/^#/; # skip comments
3565 my ($mimetype, @exts) = split(/\s+/);
3566 foreach my $ext (@exts) {
3567 $mimemap{$ext} = $mimetype;
3572 $filename =~ /\.([^.]*)$/;
3573 return $mimemap{$1};
3576 sub mimetype_guess {
3577 my $filename = shift;
3579 $filename =~ /\./ or return undef;
3581 if ($mimetypes_file) {
3582 my $file = $mimetypes_file;
3583 if ($file !~ m!^/!) { # if it is relative path
3584 # it is relative to project
3585 $file = "$projectroot/$project/$file";
3587 $mime = mimetype_guess_file($filename, $file);
3589 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3595 my $filename = shift;
3598 my $mime = mimetype_guess($filename);
3599 $mime and return $mime;
3603 return $default_blob_plain_mimetype unless $fd;
3606 return 'text/plain';
3607 } elsif (! $filename) {
3608 return 'application/octet-stream';
3609 } elsif ($filename =~ m/\.png$/i) {
3611 } elsif ($filename =~ m/\.gif$/i) {
3613 } elsif ($filename =~ m/\.jpe?g$/i) {
3614 return 'image/jpeg';
3616 return 'application/octet-stream';
3620 sub blob_contenttype {
3621 my ($fd, $file_name, $type) = @_;
3623 $type ||= blob_mimetype($fd, $file_name);
3624 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3625 $type .= "; charset=$default_text_plain_charset";
3631 # guess file syntax for syntax highlighting; return undef if no highlighting
3632 # the name of syntax can (in the future) depend on syntax highlighter used
3633 sub guess_file_syntax {
3634 my ($highlight, $mimetype, $file_name) = @_;
3635 return undef unless ($highlight && defined $file_name);
3636 my $basename = basename($file_name, '.in');
3637 return $highlight_basename{$basename}
3638 if exists $highlight_basename{$basename};
3640 $basename =~ /\.([^.]*)$/;
3641 my $ext = $1 or return undef;
3642 return $highlight_ext{$ext}
3643 if exists $highlight_ext{$ext};
3648 # run highlighter and return FD of its output,
3649 # or return original FD if no highlighting
3650 sub run_highlighter {
3651 my ($fd, $highlight, $syntax) = @_;
3652 return $fd unless ($highlight && defined $syntax);
3655 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3656 quote_command($highlight_bin).
3657 " --replace-tabs=8 --fragment --syntax $syntax |"
3658 or die_error(500, "Couldn't open file or run syntax highlighter");
3662 ## ======================================================================
3663 ## functions printing HTML: header, footer, error page
3665 sub get_page_title {
3666 my $title = to_utf8($site_name);
3668 return $title unless (defined $project);
3669 $title .= " - " . to_utf8($project);
3671 return $title unless (defined $action);
3672 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3674 return $title unless (defined $file_name);
3675 $title .= " - " . esc_path($file_name);
3676 if ($action eq "tree" && $file_name !~ m|/$|) {
3683 sub get_content_type_html {
3684 # require explicit support from the UA if we are to send the page as
3685 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3686 # we have to do this because MSIE sometimes globs '*/*', pretending to
3687 # support xhtml+xml but choking when it gets what it asked for.
3688 if (defined $cgi->http('HTTP_ACCEPT') &&
3689 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3690 $cgi->Accept('application/xhtml+xml') != 0) {
3691 return 'application/xhtml+xml';
3697 sub print_feed_meta {
3698 if (defined $project) {
3699 my %href_params = get_feed_info();
3700 if (!exists $href_params{'-title'}) {
3701 $href_params{'-title'} = 'log';
3704 foreach my $format (qw(RSS Atom)) {
3705 my $type = lc($format);
3707 '-rel' => 'alternate',
3708 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3709 '-type' => "application/$type+xml"
3712 $href_params{'action'} = $type;
3713 $link_attr{'-href'} = href(%href_params);
3715 "rel=\"$link_attr{'-rel'}\" ".
3716 "title=\"$link_attr{'-title'}\" ".
3717 "href=\"$link_attr{'-href'}\" ".
3718 "type=\"$link_attr{'-type'}\" ".
3721 $href_params{'extra_options'} = '--no-merges';
3722 $link_attr{'-href'} = href(%href_params);
3723 $link_attr{'-title'} .= ' (no merges)';
3725 "rel=\"$link_attr{'-rel'}\" ".
3726 "title=\"$link_attr{'-title'}\" ".
3727 "href=\"$link_attr{'-href'}\" ".
3728 "type=\"$link_attr{'-type'}\" ".
3733 printf('<link rel="alternate" title="%s projects list" '.
3734 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3735 esc_attr($site_name), href(project=>undef, action=>"project_index"));
3736 printf('<link rel="alternate" title="%s projects feeds" '.
3737 'href="%s" type="text/x-opml" />'."\n",
3738 esc_attr($site_name), href(project=>undef, action=>"opml"));
3742 sub print_header_links {
3745 # print out each stylesheet that exist, providing backwards capability
3746 # for those people who defined $stylesheet in a config file
3747 if (defined $stylesheet) {
3748 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3750 foreach my $stylesheet (@stylesheets) {
3751 next unless $stylesheet;
3752 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3756 if ($status eq '200 OK');
3757 if (defined $favicon) {
3758 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3762 sub print_nav_breadcrumbs {
3765 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3766 if (defined $project) {
3767 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3768 if (defined $action) {
3769 my $action_print = $action ;
3770 if (defined $opts{-action_extra}) {
3771 $action_print = $cgi->a({-href => href(action=>$action)},
3774 print " / $action_print";
3776 if (defined $opts{-action_extra}) {
3777 print " / $opts{-action_extra}";
3783 sub print_search_form {
3784 if (!defined $searchtext) {
3788 if (defined $hash_base) {
3789 $search_hash = $hash_base;
3790 } elsif (defined $hash) {
3791 $search_hash = $hash;
3793 $search_hash = "HEAD";
3795 my $action = $my_uri;
3796 my $use_pathinfo = gitweb_check_feature('pathinfo');
3797 if ($use_pathinfo) {
3798 $action .= "/".esc_url($project);
3800 print $cgi->startform(-method => "get", -action => $action) .
3801 "<div class=\"search\">\n" .
3803 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3804 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3805 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3806 $cgi->popup_menu(-name => 'st', -default => 'commit',
3807 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3808 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3810 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3811 "<span title=\"Extended regular expression\">" .
3812 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3813 -checked => $search_use_regexp) .
3816 $cgi->end_form() . "\n";
3819 sub git_header_html {
3820 my $status = shift || "200 OK";
3821 my $expires = shift;
3824 my $title = get_page_title();
3825 my $content_type = get_content_type_html();
3826 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3827 -status=> $status, -expires => $expires)
3828 unless ($opts{'-no_http_header'});
3829 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3831 <?xml version="1.0" encoding="utf-8"?>
3832 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3833 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3834 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3835 <!-- git core binaries version $git_version -->
3837 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3838 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3839 <meta name="robots" content="index, nofollow"/>
3840 <title>$title</title>
3842 # the stylesheet, favicon etc urls won't work correctly with path_info
3843 # unless we set the appropriate base URL
3844 if ($ENV{'PATH_INFO'}) {
3845 print "<base href=\"".esc_url($base_url)."\" />\n";
3847 print_header_links($status);
3851 if (defined $site_header && -f $site_header) {
3852 insert_file($site_header);
3855 print "<div class=\"page_header\">\n";
3856 if (defined $logo) {
3857 print $cgi->a({-href => esc_url($logo_url),
3858 -title => $logo_label},
3859 $cgi->img({-src => esc_url($logo),
3860 -width => 72, -height => 27,
3862 -class => "logo"}));
3864 print_nav_breadcrumbs(%opts);
3867 my $have_search = gitweb_check_feature('search');
3868 if (defined $project && $have_search) {
3869 print_search_form();
3873 sub git_footer_html {
3874 my $feed_class = 'rss_logo';
3876 print "<div class=\"page_footer\">\n";
3877 if (defined $project) {
3878 my $descr = git_get_project_description($project);
3879 if (defined $descr) {
3880 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3883 my %href_params = get_feed_info();
3884 if (!%href_params) {
3885 $feed_class .= ' generic';
3887 $href_params{'-title'} ||= 'log';
3889 foreach my $format (qw(RSS Atom)) {
3890 $href_params{'action'} = lc($format);
3891 print $cgi->a({-href => href(%href_params),
3892 -title => "$href_params{'-title'} $format feed",
3893 -class => $feed_class}, $format)."\n";
3897 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3898 -class => $feed_class}, "OPML") . " ";
3899 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3900 -class => $feed_class}, "TXT") . "\n";
3902 print "</div>\n"; # class="page_footer"
3904 if (defined $t0 && gitweb_check_feature('timed')) {
3905 print "<div id=\"generating_info\">\n";
3906 print 'This page took '.
3907 '<span id="generating_time" class="time_span">'.
3908 tv_interval($t0, [ gettimeofday() ]).
3911 '<span id="generating_cmd">'.
3912 $number_of_git_cmds.
3913 '</span> git commands '.
3915 print "</div>\n"; # class="page_footer"
3918 if (defined $site_footer && -f $site_footer) {
3919 insert_file($site_footer);
3922 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
3923 if (defined $action &&
3924 $action eq 'blame_incremental') {
3925 print qq!<script type="text/javascript">\n!.
3926 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3927 qq! "!. href() .qq!");\n!.
3930 my ($jstimezone, $tz_cookie, $datetime_class) =
3931 gitweb_get_feature('javascript-timezone');
3933 print qq!<script type="text/javascript">\n!.
3934 qq!window.onload = function () {\n!;
3935 if (gitweb_check_feature('javascript-actions')) {
3936 print qq! fixLinks();\n!;
3938 if ($jstimezone && $tz_cookie && $datetime_class) {
3939 print qq! var tz_cookie = { name: '$tz_cookie', expires: 14, path: '/' };\n!. # in days
3940 qq! onloadTZSetup('$jstimezone', tz_cookie, '$datetime_class');\n!;
3950 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3951 # Example: die_error(404, 'Hash not found')
3952 # By convention, use the following status codes (as defined in RFC 2616):
3953 # 400: Invalid or missing CGI parameters, or
3954 # requested object exists but has wrong type.
3955 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3956 # this server or project.
3957 # 404: Requested object/revision/project doesn't exist.
3958 # 500: The server isn't configured properly, or
3959 # an internal error occurred (e.g. failed assertions caused by bugs), or
3960 # an unknown error occurred (e.g. the git binary died unexpectedly).
3961 # 503: The server is currently unavailable (because it is overloaded,
3962 # or down for maintenance). Generally, this is a temporary state.
3964 my $status = shift || 500;
3965 my $error = esc_html(shift) || "Internal Server Error";
3969 my %http_responses = (
3970 400 => '400 Bad Request',
3971 403 => '403 Forbidden',
3972 404 => '404 Not Found',
3973 500 => '500 Internal Server Error',
3974 503 => '503 Service Unavailable',
3976 git_header_html($http_responses{$status}, undef, %opts);
3978 <div class="page_body">
3983 if (defined $extra) {
3991 unless ($opts{'-error_handler'});
3994 ## ----------------------------------------------------------------------
3995 ## functions printing or outputting HTML: navigation
3997 sub git_print_page_nav {
3998 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3999 $extra = '' if !defined $extra; # pager or formats
4001 my @navs = qw(summary shortlog log commit commitdiff tree);
4003 @navs = grep { $_ ne $suppress } @navs;
4006 my %arg = map { $_ => {action=>$_} } @navs;
4007 if (defined $head) {
4008 for (qw(commit commitdiff)) {
4009 $arg{$_}{'hash'} = $head;
4011 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
4012 for (qw(shortlog log)) {
4013 $arg{$_}{'hash'} = $head;
4018 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
4019 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
4021 my @actions = gitweb_get_feature('actions');
4024 'n' => $project, # project name
4025 'f' => $git_dir, # project path within filesystem
4026 'h' => $treehead || '', # current hash ('h' parameter)
4027 'b' => $treebase || '', # hash base ('hb' parameter)
4030 my ($label, $link, $pos) = splice(@actions,0,3);
4032 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
4034 $link =~ s/%([%nfhb])/$repl{$1}/g;
4035 $arg{$label}{'_href'} = $link;
4038 print "<div class=\"page_nav\">\n" .
4040 map { $_ eq $current ?
4041 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
4043 print "<br/>\n$extra<br/>\n" .
4047 # returns a submenu for the nagivation of the refs views (tags, heads,
4048 # remotes) with the current view disabled and the remotes view only
4049 # available if the feature is enabled
4050 sub format_ref_views {
4052 my @ref_views = qw{tags heads};
4053 push @ref_views, 'remotes' if gitweb_check_feature('remote_heads');
4054 return join " | ", map {
4055 $_ eq $current ? $_ :
4056 $cgi->a({-href => href(action=>$_)}, $_)
4060 sub format_paging_nav {
4061 my ($action, $page, $has_next_link) = @_;
4067 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
4069 $cgi->a({-href => href(-replay=>1, page=>$page-1),
4070 -accesskey => "p", -title => "Alt-p"}, "prev");
4072 $paging_nav .= "first ⋅ prev";
4075 if ($has_next_link) {
4076 $paging_nav .= " ⋅ " .
4077 $cgi->a({-href => href(-replay=>1, page=>$page+1),
4078 -accesskey => "n", -title => "Alt-n"}, "next");
4080 $paging_nav .= " ⋅ next";
4086 ## ......................................................................
4087 ## functions printing or outputting HTML: div
4089 sub git_print_header_div {
4090 my ($action, $title, $hash, $hash_base) = @_;
4093 $args{'action'} = $action;
4094 $args{'hash'} = $hash if $hash;
4095 $args{'hash_base'} = $hash_base if $hash_base;
4097 print "<div class=\"header\">\n" .
4098 $cgi->a({-href => href(%args), -class => "title"},
4099 $title ? $title : $action) .
4103 sub format_repo_url {
4104 my ($name, $url) = @_;
4105 return "<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";
4108 # Group output by placing it in a DIV element and adding a header.
4109 # Options for start_div() can be provided by passing a hash reference as the
4110 # first parameter to the function.
4111 # Options to git_print_header_div() can be provided by passing an array
4112 # reference. This must follow the options to start_div if they are present.
4113 # The content can be a scalar, which is output as-is, a scalar reference, which
4114 # is output after html escaping, an IO handle passed either as *handle or
4115 # *handle{IO}, or a function reference. In the latter case all following
4116 # parameters will be taken as argument to the content function call.
4117 sub git_print_section {
4118 my ($div_args, $header_args, $content);
4120 if (ref($arg) eq 'HASH') {
4124 if (ref($arg) eq 'ARRAY') {
4125 $header_args = $arg;
4130 print $cgi->start_div($div_args);
4131 git_print_header_div(@$header_args);
4133 if (ref($content) eq 'CODE') {
4135 } elsif (ref($content) eq 'SCALAR') {
4136 print esc_html($$content);
4137 } elsif (ref($content) eq 'GLOB' or ref($content) eq 'IO::Handle') {
4139 } elsif (!ref($content) && defined($content)) {
4143 print $cgi->end_div;
4146 sub format_timestamp_html {
4148 my $strtime = $date->{'rfc2822'};
4150 my (undef, undef, $datetime_class) =
4151 gitweb_get_feature('javascript-timezone');
4152 if ($datetime_class) {
4153 $strtime = qq!<span class="$datetime_class">$strtime</span>!;
4156 my $localtime_format = '(%02d:%02d %s)';
4157 if ($date->{'hour_local'} < 6) {
4158 $localtime_format = '(<span class="atnight">%02d:%02d</span> %s)';
4161 sprintf($localtime_format,
4162 $date->{'hour_local'}, $date->{'minute_local'}, $date->{'tz_local'});
4167 # Outputs the author name and date in long form
4168 sub git_print_authorship {
4171 my $tag = $opts{-tag} || 'div';
4172 my $author = $co->{'author_name'};
4174 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
4175 print "<$tag class=\"author_date\">" .
4176 format_search_author($author, "author", esc_html($author)) .
4177 " [".format_timestamp_html(\%ad)."]".
4178 git_get_avatar($co->{'author_email'}, -pad_before => 1) .
4182 # Outputs table rows containing the full author or committer information,
4183 # in the format expected for 'commit' view (& similar).
4184 # Parameters are a commit hash reference, followed by the list of people
4185 # to output information for. If the list is empty it defaults to both
4186 # author and committer.
4187 sub git_print_authorship_rows {
4189 # too bad we can't use @people = @_ || ('author', 'committer')
4191 @people = ('author', 'committer') unless @people;
4192 foreach my $who (@people) {
4193 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
4194 print "<tr><td>$who</td><td>" .
4195 format_search_author($co->{"${who}_name"}, $who,
4196 esc_html($co->{"${who}_name"})) . " " .
4197 format_search_author($co->{"${who}_email"}, $who,
4198 esc_html("<" . $co->{"${who}_email"} . ">")) .
4199 "</td><td rowspan=\"2\">" .
4200 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
4204 format_timestamp_html(\%wd) .
4210 sub git_print_page_path {
4216 print "<div class=\"page_path\">";
4217 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
4218 -title => 'tree root'}, to_utf8("[$project]"));
4220 if (defined $name) {
4221 my @dirname = split '/', $name;
4222 my $basename = pop @dirname;
4225 foreach my $dir (@dirname) {
4226 $fullname .= ($fullname ? '/' : '') . $dir;
4227 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
4229 -title => $fullname}, esc_path($dir));
4232 if (defined $type && $type eq 'blob') {
4233 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
4235 -title => $name}, esc_path($basename));
4236 } elsif (defined $type && $type eq 'tree') {
4237 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
4239 -title => $name}, esc_path($basename));
4242 print esc_path($basename);
4245 print "<br/></div>\n";
4252 if ($opts{'-remove_title'}) {
4253 # remove title, i.e. first line of log
4256 # remove leading empty lines
4257 while (defined $log->[0] && $log->[0] eq "") {
4264 foreach my $line (@$log) {
4265 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
4268 if (! $opts{'-remove_signoff'}) {
4269 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
4272 # remove signoff lines
4279 # print only one empty line
4280 # do not print empty line after signoff
4282 next if ($empty || $signoff);
4288 print format_log_line_html($line) . "<br/>\n";
4291 if ($opts{'-final_empty_line'}) {
4292 # end with single empty line
4293 print "<br/>\n" unless $empty;
4297 # return link target (what link points to)
4298 sub git_get_link_target {
4303 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4307 $link_target = <$fd>;
4312 return $link_target;
4315 # given link target, and the directory (basedir) the link is in,
4316 # return target of link relative to top directory (top tree);
4317 # return undef if it is not possible (including absolute links).
4318 sub normalize_link_target {
4319 my ($link_target, $basedir) = @_;
4321 # absolute symlinks (beginning with '/') cannot be normalized
4322 return if (substr($link_target, 0, 1) eq '/');
4324 # normalize link target to path from top (root) tree (dir)
4327 $path = $basedir . '/' . $link_target;
4329 # we are in top (root) tree (dir)
4330 $path = $link_target;
4333 # remove //, /./, and /../
4335 foreach my $part (split('/', $path)) {
4336 # discard '.' and ''
4337 next if (!$part || $part eq '.');
4339 if ($part eq '..') {
4343 # link leads outside repository (outside top dir)
4347 push @path_parts, $part;
4350 $path = join('/', @path_parts);
4355 # print tree entry (row of git_tree), but without encompassing <tr> element
4356 sub git_print_tree_entry {
4357 my ($t, $basedir, $hash_base, $have_blame) = @_;
4360 $base_key{'hash_base'} = $hash_base if defined $hash_base;
4362 # The format of a table row is: mode list link. Where mode is
4363 # the mode of the entry, list is the name of the entry, an href,
4364 # and link is the action links of the entry.
4366 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
4367 if (exists $t->{'size'}) {
4368 print "<td class=\"size\">$t->{'size'}</td>\n";
4370 if ($t->{'type'} eq "blob") {
4371 print "<td class=\"list\">" .
4372 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4373 file_name=>"$basedir$t->{'name'}", %base_key),
4374 -class => "list"}, esc_path($t->{'name'}));
4375 if (S_ISLNK(oct $t->{'mode'})) {
4376 my $link_target = git_get_link_target($t->{'hash'});
4378 my $norm_target = normalize_link_target($link_target, $basedir);
4379 if (defined $norm_target) {
4381 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
4382 file_name=>$norm_target),
4383 -title => $norm_target}, esc_path($link_target));
4385 print " -> " . esc_path($link_target);
4390 print "<td class=\"link\">";
4391 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4392 file_name=>"$basedir$t->{'name'}", %base_key)},
4396 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4397 file_name=>"$basedir$t->{'name'}", %base_key)},
4400 if (defined $hash_base) {
4402 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4403 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4407 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4408 file_name=>"$basedir$t->{'name'}")},
4412 } elsif ($t->{'type'} eq "tree") {
4413 print "<td class=\"list\">";
4414 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4415 file_name=>"$basedir$t->{'name'}",
4417 esc_path($t->{'name'}));
4419 print "<td class=\"link\">";
4420 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4421 file_name=>"$basedir$t->{'name'}",
4424 if (defined $hash_base) {
4426 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4427 file_name=>"$basedir$t->{'name'}")},
4432 # unknown object: we can only present history for it
4433 # (this includes 'commit' object, i.e. submodule support)
4434 print "<td class=\"list\">" .
4435 esc_path($t->{'name'}) .
4437 print "<td class=\"link\">";
4438 if (defined $hash_base) {
4439 print $cgi->a({-href => href(action=>"history",
4440 hash_base=>$hash_base,
4441 file_name=>"$basedir$t->{'name'}")},
4448 ## ......................................................................
4449 ## functions printing large fragments of HTML
4451 # get pre-image filenames for merge (combined) diff
4452 sub fill_from_file_info {
4453 my ($diff, @parents) = @_;
4455 $diff->{'from_file'} = [ ];
4456 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4457 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4458 if ($diff->{'status'}[$i] eq 'R' ||
4459 $diff->{'status'}[$i] eq 'C') {
4460 $diff->{'from_file'}[$i] =
4461 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4468 # is current raw difftree line of file deletion
4470 my $diffinfo = shift;
4472 return $diffinfo->{'to_id'} eq ('0' x 40);
4475 # does patch correspond to [previous] difftree raw line
4476 # $diffinfo - hashref of parsed raw diff format
4477 # $patchinfo - hashref of parsed patch diff format
4478 # (the same keys as in $diffinfo)
4479 sub is_patch_split {
4480 my ($diffinfo, $patchinfo) = @_;
4482 return defined $diffinfo && defined $patchinfo
4483 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4487 sub git_difftree_body {
4488 my ($difftree, $hash, @parents) = @_;
4489 my ($parent) = $parents[0];
4490 my $have_blame = gitweb_check_feature('blame');
4491 print "<div class=\"list_head\">\n";
4492 if ($#{$difftree} > 10) {
4493 print(($#{$difftree} + 1) . " files changed:\n");
4497 print "<table class=\"" .
4498 (@parents > 1 ? "combined " : "") .
4501 # header only for combined diff in 'commitdiff' view
4502 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4505 print "<thead><tr>\n" .
4506 "<th></th><th></th>\n"; # filename, patchN link
4507 for (my $i = 0; $i < @parents; $i++) {
4508 my $par = $parents[$i];
4510 $cgi->a({-href => href(action=>"commitdiff",
4511 hash=>$hash, hash_parent=>$par),
4512 -title => 'commitdiff to parent number ' .
4513 ($i+1) . ': ' . substr($par,0,7)},
4517 print "</tr></thead>\n<tbody>\n";
4522 foreach my $line (@{$difftree}) {
4523 my $diff = parsed_difftree_line($line);
4526 print "<tr class=\"dark\">\n";
4528 print "<tr class=\"light\">\n";
4532 if (exists $diff->{'nparents'}) { # combined diff
4534 fill_from_file_info($diff, @parents)
4535 unless exists $diff->{'from_file'};
4537 if (!is_deleted($diff)) {
4538 # file exists in the result (child) commit
4540 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4541 file_name=>$diff->{'to_file'},
4543 -class => "list"}, esc_path($diff->{'to_file'})) .
4547 esc_path($diff->{'to_file'}) .
4551 if ($action eq 'commitdiff') {
4554 print "<td class=\"link\">" .
4555 $cgi->a({-href => href(-anchor=>"patch$patchno")},
4561 my $has_history = 0;
4562 my $not_deleted = 0;
4563 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4564 my $hash_parent = $parents[$i];
4565 my $from_hash = $diff->{'from_id'}[$i];
4566 my $from_path = $diff->{'from_file'}[$i];
4567 my $status = $diff->{'status'}[$i];
4569 $has_history ||= ($status ne 'A');
4570 $not_deleted ||= ($status ne 'D');
4572 if ($status eq 'A') {
4573 print "<td class=\"link\" align=\"right\"> | </td>\n";
4574 } elsif ($status eq 'D') {
4575 print "<td class=\"link\">" .
4576 $cgi->a({-href => href(action=>"blob",
4579 file_name=>$from_path)},
4583 if ($diff->{'to_id'} eq $from_hash) {
4584 print "<td class=\"link nochange\">";
4586 print "<td class=\"link\">";
4588 print $cgi->a({-href => href(action=>"blobdiff",
4589 hash=>$diff->{'to_id'},
4590 hash_parent=>$from_hash,
4592 hash_parent_base=>$hash_parent,
4593 file_name=>$diff->{'to_file'},
4594 file_parent=>$from_path)},
4600 print "<td class=\"link\">";
4602 print $cgi->a({-href => href(action=>"blob",
4603 hash=>$diff->{'to_id'},
4604 file_name=>$diff->{'to_file'},
4607 print " | " if ($has_history);
4610 print $cgi->a({-href => href(action=>"history",
4611 file_name=>$diff->{'to_file'},
4618 next; # instead of 'else' clause, to avoid extra indent
4620 # else ordinary diff
4622 my ($to_mode_oct, $to_mode_str, $to_file_type);
4623 my ($from_mode_oct, $from_mode_str, $from_file_type);
4624 if ($diff->{'to_mode'} ne ('0' x 6)) {
4625 $to_mode_oct = oct $diff->{'to_mode'};
4626 if (S_ISREG($to_mode_oct)) { # only for regular file
4627 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4629 $to_file_type = file_type($diff->{'to_mode'});
4631 if ($diff->{'from_mode'} ne ('0' x 6)) {
4632 $from_mode_oct = oct $diff->{'from_mode'};
4633 if (S_ISREG($from_mode_oct)) { # only for regular file
4634 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4636 $from_file_type = file_type($diff->{'from_mode'});
4639 if ($diff->{'status'} eq "A") { # created
4640 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4641 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4642 $mode_chng .= "]</span>";
4644 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4645 hash_base=>$hash, file_name=>$diff->{'file'}),
4646 -class => "list"}, esc_path($diff->{'file'}));
4648 print "<td>$mode_chng</td>\n";
4649 print "<td class=\"link\">";
4650 if ($action eq 'commitdiff') {
4653 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4657 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4658 hash_base=>$hash, file_name=>$diff->{'file'})},
4662 } elsif ($diff->{'status'} eq "D") { # deleted
4663 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4665 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4666 hash_base=>$parent, file_name=>$diff->{'file'}),
4667 -class => "list"}, esc_path($diff->{'file'}));
4669 print "<td>$mode_chng</td>\n";
4670 print "<td class=\"link\">";
4671 if ($action eq 'commitdiff') {
4674 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4678 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4679 hash_base=>$parent, file_name=>$diff->{'file'})},
4682 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4683 file_name=>$diff->{'file'})},
4686 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4687 file_name=>$diff->{'file'})},
4691 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4692 my $mode_chnge = "";
4693 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4694 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4695 if ($from_file_type ne $to_file_type) {
4696 $mode_chnge .= " from $from_file_type to $to_file_type";
4698 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4699 if ($from_mode_str && $to_mode_str) {
4700 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4701 } elsif ($to_mode_str) {
4702 $mode_chnge .= " mode: $to_mode_str";
4705 $mode_chnge .= "]</span>\n";
4708 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4709 hash_base=>$hash, file_name=>$diff->{'file'}),
4710 -class => "list"}, esc_path($diff->{'file'}));
4712 print "<td>$mode_chnge</td>\n";
4713 print "<td class=\"link\">";
4714 if ($action eq 'commitdiff') {
4717 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4720 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4721 # "commit" view and modified file (not onlu mode changed)
4722 print $cgi->a({-href => href(action=>"blobdiff",
4723 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4724 hash_base=>$hash, hash_parent_base=>$parent,
4725 file_name=>$diff->{'file'})},
4729 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4730 hash_base=>$hash, file_name=>$diff->{'file'})},
4733 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4734 file_name=>$diff->{'file'})},
4737 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4738 file_name=>$diff->{'file'})},
4742 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4743 my %status_name = ('R' => 'moved', 'C' => 'copied');
4744 my $nstatus = $status_name{$diff->{'status'}};
4746 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4747 # mode also for directories, so we cannot use $to_mode_str
4748 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4751 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4752 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4753 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4754 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4755 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4756 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4757 -class => "list"}, esc_path($diff->{'from_file'})) .
4758 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4759 "<td class=\"link\">";
4760 if ($action eq 'commitdiff') {
4763 print $cgi->a({-href => href(-anchor=>"patch$patchno")},
4766 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4767 # "commit" view and modified file (not only pure rename or copy)
4768 print $cgi->a({-href => href(action=>"blobdiff",
4769 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4770 hash_base=>$hash, hash_parent_base=>$parent,
4771 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4775 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4776 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4779 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4780 file_name=>$diff->{'to_file'})},
4783 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4784 file_name=>$diff->{'to_file'})},
4788 } # we should not encounter Unmerged (U) or Unknown (X) status
4791 print "</tbody>" if $has_header;
4795 sub git_patchset_body {
4796 my ($fd, $difftree, $hash, @hash_parents) = @_;
4797 my ($hash_parent) = $hash_parents[0];
4799 my $is_combined = (@hash_parents > 1);
4801 my $patch_number = 0;
4807 print "<div class=\"patchset\">\n";
4809 # skip to first patch
4810 while ($patch_line = <$fd>) {
4813 last if ($patch_line =~ m/^diff /);
4817 while ($patch_line) {
4819 # parse "git diff" header line
4820 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4821 # $1 is from_name, which we do not use
4822 $to_name = unquote($2);
4823 $to_name =~ s!^b/!!;
4824 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4825 # $1 is 'cc' or 'combined', which we do not use
4826 $to_name = unquote($2);
4831 # check if current patch belong to current raw line
4832 # and parse raw git-diff line if needed
4833 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4834 # this is continuation of a split patch
4835 print "<div class=\"patch cont\">\n";
4837 # advance raw git-diff output if needed
4838 $patch_idx++ if defined $diffinfo;
4840 # read and prepare patch information
4841 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4843 # compact combined diff output can have some patches skipped
4844 # find which patch (using pathname of result) we are at now;
4846 while ($to_name ne $diffinfo->{'to_file'}) {
4847 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4848 format_diff_cc_simplified($diffinfo, @hash_parents) .
4849 "</div>\n"; # class="patch"
4854 last if $patch_idx > $#$difftree;
4855 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4859 # modifies %from, %to hashes
4860 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4862 # this is first patch for raw difftree line with $patch_idx index
4863 # we index @$difftree array from 0, but number patches from 1
4864 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4868 #assert($patch_line =~ m/^diff /) if DEBUG;
4869 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4871 # print "git diff" header
4872 print format_git_diff_header_line($patch_line, $diffinfo,
4875 # print extended diff header
4876 print "<div class=\"diff extended_header\">\n";
4878 while ($patch_line = <$fd>) {
4881 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4883 print format_extended_diff_header_line($patch_line, $diffinfo,
4886 print "</div>\n"; # class="diff extended_header"
4888 # from-file/to-file diff header
4889 if (! $patch_line) {
4890 print "</div>\n"; # class="patch"
4893 next PATCH if ($patch_line =~ m/^diff /);
4894 #assert($patch_line =~ m/^---/) if DEBUG;
4896 my $last_patch_line = $patch_line;
4897 $patch_line = <$fd>;
4899 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4901 print format_diff_from_to_header($last_patch_line, $patch_line,
4902 $diffinfo, \%from, \%to,
4907 while ($patch_line = <$fd>) {
4910 next PATCH if ($patch_line =~ m/^diff /);
4912 print format_diff_line($patch_line, \%from, \%to);
4916 print "</div>\n"; # class="patch"
4919 # for compact combined (--cc) format, with chunk and patch simplification
4920 # the patchset might be empty, but there might be unprocessed raw lines
4921 for (++$patch_idx if $patch_number > 0;
4922 $patch_idx < @$difftree;
4924 # read and prepare patch information
4925 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4927 # generate anchor for "patch" links in difftree / whatchanged part
4928 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4929 format_diff_cc_simplified($diffinfo, @hash_parents) .
4930 "</div>\n"; # class="patch"
4935 if ($patch_number == 0) {
4936 if (@hash_parents > 1) {
4937 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4939 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4943 print "</div>\n"; # class="patchset"
4946 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4948 # fills project list info (age, description, owner, category, forks)
4949 # for each project in the list, removing invalid projects from
4951 # NOTE: modifies $projlist, but does not remove entries from it
4952 sub fill_project_list_info {
4953 my $projlist = shift;
4956 my $show_ctags = gitweb_check_feature('ctags');
4958 foreach my $pr (@$projlist) {
4959 my (@activity) = git_get_last_activity($pr->{'path'});
4960 unless (@activity) {
4963 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4964 if (!defined $pr->{'descr'}) {
4965 my $descr = git_get_project_description($pr->{'path'}) || "";
4966 $descr = to_utf8($descr);
4967 $pr->{'descr_long'} = $descr;
4968 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4970 if (!defined $pr->{'owner'}) {
4971 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4974 $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4976 if ($projects_list_group_categories && !defined $pr->{'category'}) {
4977 my $cat = git_get_project_category($pr->{'path'}) ||
4978 $project_list_default_category;
4979 $pr->{'category'} = to_utf8($cat);
4982 push @projects, $pr;
4988 sub sort_projects_list {
4989 my ($projlist, $order) = @_;
4993 project => { key => 'path', type => 'str' },
4994 descr => { key => 'descr_long', type => 'str' },
4995 owner => { key => 'owner', type => 'str' },
4996 age => { key => 'age', type => 'num' }
4998 my $oi = $order_info{$order};
4999 return @$projlist unless defined $oi;
5000 if ($oi->{'type'} eq 'str') {
5001 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @$projlist;
5003 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @$projlist;
5009 # returns a hash of categories, containing the list of project
5010 # belonging to each category
5011 sub build_projlist_by_category {
5012 my ($projlist, $from, $to) = @_;
5015 $from = 0 unless defined $from;
5016 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5018 for (my $i = $from; $i <= $to; $i++) {
5019 my $pr = $projlist->[$i];
5020 push @{$categories{ $pr->{'category'} }}, $pr;
5023 return wantarray ? %categories : \%categories;
5026 # print 'sort by' <th> element, generating 'sort by $name' replay link
5027 # if that order is not selected
5029 print format_sort_th(@_);
5032 sub format_sort_th {
5033 my ($name, $order, $header) = @_;
5035 $header ||= ucfirst($name);
5037 if ($order eq $name) {
5038 $sort_th .= "<th>$header</th>\n";
5040 $sort_th .= "<th>" .
5041 $cgi->a({-href => href(-replay=>1, order=>$name),
5042 -class => "header"}, $header) .
5049 sub git_project_list_rows {
5050 my ($projlist, $from, $to, $check_forks) = @_;
5052 $from = 0 unless defined $from;
5053 $to = $#$projlist if (!defined $to || $#$projlist < $to);
5056 for (my $i = $from; $i <= $to; $i++) {
5057 my $pr = $projlist->[$i];
5060 print "<tr class=\"dark\">\n";
5062 print "<tr class=\"light\">\n";
5068 if ($pr->{'forks'}) {
5069 my $nforks = scalar @{$pr->{'forks'}};
5071 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),
5072 -title => "$nforks forks"}, "+");
5074 print $cgi->span({-title => "$nforks forks"}, "+");
5079 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5080 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
5081 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
5082 -class => "list", -title => $pr->{'descr_long'}},
5083 esc_html($pr->{'descr'})) . "</td>\n" .
5084 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
5085 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
5086 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
5087 "<td class=\"link\">" .
5088 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
5089 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
5090 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
5091 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
5092 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
5098 sub git_project_list_body {
5099 # actually uses global variable $project
5100 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
5101 my @projects = @$projlist;
5103 my $check_forks = gitweb_check_feature('forks');
5104 my $show_ctags = gitweb_check_feature('ctags');
5105 my $tagfilter = $show_ctags ? $cgi->param('by_tag') : undef;
5106 $check_forks = undef
5107 if ($tagfilter || $searchtext);
5109 # filtering out forks before filling info allows to do less work
5110 @projects = filter_forks_from_projects_list(\@projects)
5112 @projects = fill_project_list_info(\@projects);
5113 # searching projects require filling to be run before it
5114 @projects = search_projects_list(\@projects,
5115 'searchtext' => $searchtext,
5116 'tagfilter' => $tagfilter)
5117 if ($tagfilter || $searchtext);
5119 $order ||= $default_projects_order;
5120 $from = 0 unless defined $from;
5121 $to = $#projects if (!defined $to || $#projects < $to);
5126 "<b>No such projects found</b><br />\n".
5127 "Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".
5128 "</center>\n<br />\n";
5132 @projects = sort_projects_list(\@projects, $order);
5135 my $ctags = git_gather_all_ctags(\@projects);
5136 my $cloud = git_populate_project_tagcloud($ctags);
5137 print git_show_project_tagcloud($cloud, 64);
5140 print "<table class=\"project_list\">\n";
5141 unless ($no_header) {
5144 print "<th></th>\n";
5146 print_sort_th('project', $order, 'Project');
5147 print_sort_th('descr', $order, 'Description');
5148 print_sort_th('owner', $order, 'Owner');
5149 print_sort_th('age', $order, 'Last Change');
5150 print "<th></th>\n" . # for links
5154 if ($projects_list_group_categories) {
5155 # only display categories with projects in the $from-$to window
5156 @projects = sort {$a->{'category'} cmp $b->{'category'}} @projects[$from..$to];
5157 my %categories = build_projlist_by_category(\@projects, $from, $to);
5158 foreach my $cat (sort keys %categories) {
5159 unless ($cat eq "") {
5162 print "<td></td>\n";
5164 print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
5168 git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
5171 git_project_list_rows(\@projects, $from, $to, $check_forks);
5174 if (defined $extra) {
5177 print "<td></td>\n";
5179 print "<td colspan=\"5\">$extra</td>\n" .
5186 # uses global variable $project
5187 my ($commitlist, $from, $to, $refs, $extra) = @_;
5189 $from = 0 unless defined $from;
5190 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5192 for (my $i = 0; $i <= $to; $i++) {
5193 my %co = %{$commitlist->[$i]};
5195 my $commit = $co{'id'};
5196 my $ref = format_ref_marker($refs, $commit);
5197 git_print_header_div('commit',
5198 "<span class=\"age\">$co{'age_string'}</span>" .
5199 esc_html($co{'title'}) . $ref,
5201 print "<div class=\"title_text\">\n" .
5202 "<div class=\"log_link\">\n" .
5203 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5205 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5207 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5210 git_print_authorship(\%co, -tag => 'span');
5211 print "<br/>\n</div>\n";
5213 print "<div class=\"log_body\">\n";
5214 git_print_log($co{'comment'}, -final_empty_line=> 1);
5218 print "<div class=\"page_nav\">\n";
5224 sub git_shortlog_body {
5225 # uses global variable $project
5226 my ($commitlist, $from, $to, $refs, $extra) = @_;
5228 $from = 0 unless defined $from;
5229 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5231 print "<table class=\"shortlog\">\n";
5233 for (my $i = $from; $i <= $to; $i++) {
5234 my %co = %{$commitlist->[$i]};
5235 my $commit = $co{'id'};
5236 my $ref = format_ref_marker($refs, $commit);
5238 print "<tr class=\"dark\">\n";
5240 print "<tr class=\"light\">\n";
5243 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
5244 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5245 format_author_html('td', \%co, 10) . "<td>";
5246 print format_subject_html($co{'title'}, $co{'title_short'},
5247 href(action=>"commit", hash=>$commit), $ref);
5249 "<td class=\"link\">" .
5250 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
5251 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
5252 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
5253 my $snapshot_links = format_snapshot_links($commit);
5254 if (defined $snapshot_links) {
5255 print " | " . $snapshot_links;
5260 if (defined $extra) {
5262 "<td colspan=\"4\">$extra</td>\n" .
5268 sub git_history_body {
5269 # Warning: assumes constant type (blob or tree) during history
5270 my ($commitlist, $from, $to, $refs, $extra,
5271 $file_name, $file_hash, $ftype) = @_;
5273 $from = 0 unless defined $from;
5274 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
5276 print "<table class=\"history\">\n";
5278 for (my $i = $from; $i <= $to; $i++) {
5279 my %co = %{$commitlist->[$i]};
5283 my $commit = $co{'id'};
5285 my $ref = format_ref_marker($refs, $commit);
5288 print "<tr class=\"dark\">\n";
5290 print "<tr class=\"light\">\n";
5293 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5294 # shortlog: format_author_html('td', \%co, 10)
5295 format_author_html('td', \%co, 15, 3) . "<td>";
5296 # originally git_history used chop_str($co{'title'}, 50)
5297 print format_subject_html($co{'title'}, $co{'title_short'},
5298 href(action=>"commit", hash=>$commit), $ref);
5300 "<td class=\"link\">" .
5301 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
5302 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
5304 if ($ftype eq 'blob') {
5305 my $blob_current = $file_hash;
5306 my $blob_parent = git_get_hash_by_path($commit, $file_name);
5307 if (defined $blob_current && defined $blob_parent &&
5308 $blob_current ne $blob_parent) {
5310 $cgi->a({-href => href(action=>"blobdiff",
5311 hash=>$blob_current, hash_parent=>$blob_parent,
5312 hash_base=>$hash_base, hash_parent_base=>$commit,
5313 file_name=>$file_name)},
5320 if (defined $extra) {
5322 "<td colspan=\"4\">$extra</td>\n" .
5329 # uses global variable $project
5330 my ($taglist, $from, $to, $extra) = @_;
5331 $from = 0 unless defined $from;
5332 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
5334 print "<table class=\"tags\">\n";
5336 for (my $i = $from; $i <= $to; $i++) {
5337 my $entry = $taglist->[$i];
5339 my $comment = $tag{'subject'};
5341 if (defined $comment) {
5342 $comment_short = chop_str($comment, 30, 5);
5345 print "<tr class=\"dark\">\n";
5347 print "<tr class=\"light\">\n";
5350 if (defined $tag{'age'}) {
5351 print "<td><i>$tag{'age'}</i></td>\n";
5353 print "<td></td>\n";
5356 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
5357 -class => "list name"}, esc_html($tag{'name'})) .
5360 if (defined $comment) {
5361 print format_subject_html($comment, $comment_short,
5362 href(action=>"tag", hash=>$tag{'id'}));
5365 "<td class=\"selflink\">";
5366 if ($tag{'type'} eq "tag") {
5367 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
5372 "<td class=\"link\">" . " | " .
5373 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
5374 if ($tag{'reftype'} eq "commit") {
5375 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
5376 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
5377 } elsif ($tag{'reftype'} eq "blob") {
5378 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
5383 if (defined $extra) {
5385 "<td colspan=\"5\">$extra</td>\n" .
5391 sub git_heads_body {
5392 # uses global variable $project
5393 my ($headlist, $head, $from, $to, $extra) = @_;
5394 $from = 0 unless defined $from;
5395 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
5397 print "<table class=\"heads\">\n";
5399 for (my $i = $from; $i <= $to; $i++) {
5400 my $entry = $headlist->[$i];
5402 my $curr = $ref{'id'} eq $head;
5404 print "<tr class=\"dark\">\n";
5406 print "<tr class=\"light\">\n";
5409 print "<td><i>$ref{'age'}</i></td>\n" .
5410 ($curr ? "<td class=\"current_head\">" : "<td>") .
5411 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
5412 -class => "list name"},esc_html($ref{'name'})) .
5414 "<td class=\"link\">" .
5415 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
5416 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
5417 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})}, "tree") .
5421 if (defined $extra) {
5423 "<td colspan=\"3\">$extra</td>\n" .
5429 # Display a single remote block
5430 sub git_remote_block {
5431 my ($remote, $rdata, $limit, $head) = @_;
5433 my $heads = $rdata->{'heads'};
5434 my $fetch = $rdata->{'fetch'};
5435 my $push = $rdata->{'push'};
5437 my $urls_table = "<table class=\"projects_list\">\n" ;
5439 if (defined $fetch) {
5440 if ($fetch eq $push) {
5441 $urls_table .= format_repo_url("URL", $fetch);
5443 $urls_table .= format_repo_url("Fetch URL", $fetch);
5444 $urls_table .= format_repo_url("Push URL", $push) if defined $push;
5446 } elsif (defined $push) {
5447 $urls_table .= format_repo_url("Push URL", $push);
5449 $urls_table .= format_repo_url("", "No remote URL");
5452 $urls_table .= "</table>\n";
5455 if (defined $limit && $limit < @$heads) {
5456 $dots = $cgi->a({-href => href(action=>"remotes", hash=>$remote)}, "...");
5460 git_heads_body($heads, $head, 0, $limit, $dots);
5463 # Display a list of remote names with the respective fetch and push URLs
5464 sub git_remotes_list {
5465 my ($remotedata, $limit) = @_;
5466 print "<table class=\"heads\">\n";
5468 my @remotes = sort keys %$remotedata;
5470 my $limited = $limit && $limit < @remotes;
5472 $#remotes = $limit - 1 if $limited;
5474 while (my $remote = shift @remotes) {
5475 my $rdata = $remotedata->{$remote};
5476 my $fetch = $rdata->{'fetch'};
5477 my $push = $rdata->{'push'};
5479 print "<tr class=\"dark\">\n";
5481 print "<tr class=\"light\">\n";
5485 $cgi->a({-href=> href(action=>'remotes', hash=>$remote),
5486 -class=> "list name"},esc_html($remote)) .
5488 print "<td class=\"link\">" .
5489 (defined $fetch ? $cgi->a({-href=> $fetch}, "fetch") : "fetch") .
5491 (defined $push ? $cgi->a({-href=> $push}, "push") : "push") .
5499 "<td colspan=\"3\">" .
5500 $cgi->a({-href => href(action=>"remotes")}, "...") .
5501 "</td>\n" . "</tr>\n";
5507 # Display remote heads grouped by remote, unless there are too many
5508 # remotes, in which case we only display the remote names
5509 sub git_remotes_body {
5510 my ($remotedata, $limit, $head) = @_;
5511 if ($limit and $limit < keys %$remotedata) {
5512 git_remotes_list($remotedata, $limit);
5514 fill_remote_heads($remotedata);
5515 while (my ($remote, $rdata) = each %$remotedata) {
5516 git_print_section({-class=>"remote", -id=>$remote},
5517 ["remotes", $remote, $remote], sub {
5518 git_remote_block($remote, $rdata, $limit, $head);
5524 sub git_search_grep_body {
5525 my ($commitlist, $from, $to, $extra) = @_;
5526 $from = 0 unless defined $from;
5527 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
5529 print "<table class=\"commit_search\">\n";
5531 for (my $i = $from; $i <= $to; $i++) {
5532 my %co = %{$commitlist->[$i]};
5536 my $commit = $co{'id'};
5538 print "<tr class=\"dark\">\n";
5540 print "<tr class=\"light\">\n";
5543 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5544 format_author_html('td', \%co, 15, 5) .
5546 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5547 -class => "list subject"},
5548 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5549 my $comment = $co{'comment'};
5550 foreach my $line (@$comment) {
5551 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5552 my ($lead, $match, $trail) = ($1, $2, $3);
5553 $match = chop_str($match, 70, 5, 'center');
5554 my $contextlen = int((80 - length($match))/2);
5555 $contextlen = 30 if ($contextlen > 30);
5556 $lead = chop_str($lead, $contextlen, 10, 'left');
5557 $trail = chop_str($trail, $contextlen, 10, 'right');
5559 $lead = esc_html($lead);
5560 $match = esc_html($match);
5561 $trail = esc_html($trail);
5563 print "$lead<span class=\"match\">$match</span>$trail<br />";
5567 "<td class=\"link\">" .
5568 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5570 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
5572 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5576 if (defined $extra) {
5578 "<td colspan=\"3\">$extra</td>\n" .
5584 ## ======================================================================
5585 ## ======================================================================
5588 sub git_project_list {
5589 my $order = $input_params{'order'};
5590 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5591 die_error(400, "Unknown order parameter");
5594 my @list = git_get_projects_list();
5596 die_error(404, "No projects found");
5600 if (defined $home_text && -f $home_text) {
5601 print "<div class=\"index_include\">\n";
5602 insert_file($home_text);
5605 print $cgi->startform(-method => "get") .
5606 "<p class=\"projsearch\">Search:\n" .
5607 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
5609 $cgi->end_form() . "\n";
5610 git_project_list_body(\@list, $order);
5615 my $order = $input_params{'order'};
5616 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5617 die_error(400, "Unknown order parameter");
5620 my @list = git_get_projects_list($project);
5622 die_error(404, "No forks found");
5626 git_print_page_nav('','');
5627 git_print_header_div('summary', "$project forks");
5628 git_project_list_body(\@list, $order);
5632 sub git_project_index {
5633 my @projects = git_get_projects_list();
5635 die_error(404, "No projects found");
5639 -type => 'text/plain',
5640 -charset => 'utf-8',
5641 -content_disposition => 'inline; filename="index.aux"');
5643 foreach my $pr (@projects) {
5644 if (!exists $pr->{'owner'}) {
5645 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
5648 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5649 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5650 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5651 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5655 print "$path $owner\n";
5660 my $descr = git_get_project_description($project) || "none";
5661 my %co = parse_commit("HEAD");
5662 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5663 my $head = $co{'id'};
5664 my $remote_heads = gitweb_check_feature('remote_heads');
5666 my $owner = git_get_project_owner($project);
5668 my $refs = git_get_references();
5669 # These get_*_list functions return one more to allow us to see if
5670 # there are more ...
5671 my @taglist = git_get_tags_list(16);
5672 my @headlist = git_get_heads_list(16);
5673 my %remotedata = $remote_heads ? git_get_remotes_list() : ();
5675 my $check_forks = gitweb_check_feature('forks');
5678 # find forks of a project
5679 @forklist = git_get_projects_list($project);
5680 # filter out forks of forks
5681 @forklist = filter_forks_from_projects_list(\@forklist)
5686 git_print_page_nav('summary','', $head);
5688 print "<div class=\"title\"> </div>\n";
5689 print "<table class=\"projects_list\">\n" .
5690 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
5691 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
5692 if (defined $cd{'rfc2822'}) {
5693 print "<tr id=\"metadata_lchange\"><td>last change</td>" .
5694 "<td>".format_timestamp_html(\%cd)."</td></tr>\n";
5697 # use per project git URL list in $projectroot/$project/cloneurl
5698 # or make project git URL from git base URL and project name
5699 my $url_tag = "URL";
5700 my @url_list = git_get_project_url_list($project);
5701 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5702 foreach my $git_url (@url_list) {
5703 next unless $git_url;
5704 print format_repo_url($url_tag, $git_url);
5709 my $show_ctags = gitweb_check_feature('ctags');
5711 my $ctags = git_get_project_ctags($project);
5713 # without ability to add tags, don't show if there are none
5714 my $cloud = git_populate_project_tagcloud($ctags);
5715 print "<tr id=\"metadata_ctags\">" .
5716 "<td>content tags</td>" .
5717 "<td>".git_show_project_tagcloud($cloud, 48)."</td>" .
5724 # If XSS prevention is on, we don't include README.html.
5725 # TODO: Allow a readme in some safe format.
5726 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
5727 print "<div class=\"title\">readme</div>\n" .
5728 "<div class=\"readme\">\n";
5729 insert_file("$projectroot/$project/README.html");
5730 print "\n</div>\n"; # class="readme"
5733 # we need to request one more than 16 (0..15) to check if
5735 my @commitlist = $head ? parse_commits($head, 17) : ();
5737 git_print_header_div('shortlog');
5738 git_shortlog_body(\@commitlist, 0, 15, $refs,
5739 $#commitlist <= 15 ? undef :
5740 $cgi->a({-href => href(action=>"shortlog")}, "..."));
5744 git_print_header_div('tags');
5745 git_tags_body(\@taglist, 0, 15,
5746 $#taglist <= 15 ? undef :
5747 $cgi->a({-href => href(action=>"tags")}, "..."));
5751 git_print_header_div('heads');
5752 git_heads_body(\@headlist, $head, 0, 15,
5753 $#headlist <= 15 ? undef :
5754 $cgi->a({-href => href(action=>"heads")}, "..."));
5758 git_print_header_div('remotes');
5759 git_remotes_body(\%remotedata, 15, $head);
5763 git_print_header_div('forks');
5764 git_project_list_body(\@forklist, 'age', 0, 15,
5765 $#forklist <= 15 ? undef :
5766 $cgi->a({-href => href(action=>"forks")}, "..."),
5774 my %tag = parse_tag($hash);
5777 die_error(404, "Unknown tag object");
5780 my $head = git_get_head_hash($project);
5782 git_print_page_nav('','', $head,undef,$head);
5783 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
5784 print "<div class=\"title_text\">\n" .
5785 "<table class=\"object_header\">\n" .
5787 "<td>object</td>\n" .
5788 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5789 $tag{'object'}) . "</td>\n" .
5790 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5791 $tag{'type'}) . "</td>\n" .
5793 if (defined($tag{'author'})) {
5794 git_print_authorship_rows(\%tag, 'author');
5796 print "</table>\n\n" .
5798 print "<div class=\"page_body\">";
5799 my $comment = $tag{'comment'};
5800 foreach my $line (@$comment) {
5802 print esc_html($line, -nbsp=>1) . "<br/>\n";
5808 sub git_blame_common {
5809 my $format = shift || 'porcelain';
5810 if ($format eq 'porcelain' && $cgi->param('js')) {
5811 $format = 'incremental';
5812 $action = 'blame_incremental'; # for page title etc
5816 gitweb_check_feature('blame')
5817 or die_error(403, "Blame view not allowed");
5820 die_error(400, "No file name given") unless $file_name;
5821 $hash_base ||= git_get_head_hash($project);
5822 die_error(404, "Couldn't find base commit") unless $hash_base;
5823 my %co = parse_commit($hash_base)
5824 or die_error(404, "Commit not found");
5826 if (!defined $hash) {
5827 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5828 or die_error(404, "Error looking up file");
5830 $ftype = git_get_type($hash);
5831 if ($ftype !~ "blob") {
5832 die_error(400, "Object is not a blob");
5837 if ($format eq 'incremental') {
5838 # get file contents (as base)
5839 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5840 or die_error(500, "Open git-cat-file failed");
5841 } elsif ($format eq 'data') {
5842 # run git-blame --incremental
5843 open $fd, "-|", git_cmd(), "blame", "--incremental",
5844 $hash_base, "--", $file_name
5845 or die_error(500, "Open git-blame --incremental failed");
5847 # run git-blame --porcelain
5848 open $fd, "-|", git_cmd(), "blame", '-p',
5849 $hash_base, '--', $file_name
5850 or die_error(500, "Open git-blame --porcelain failed");
5853 # incremental blame data returns early
5854 if ($format eq 'data') {
5856 -type=>"text/plain", -charset => "utf-8",
5857 -status=> "200 OK");
5858 local $| = 1; # output autoflush
5861 or print "ERROR $!\n";
5864 if (defined $t0 && gitweb_check_feature('timed')) {
5866 tv_interval($t0, [ gettimeofday() ]).
5867 ' '.$number_of_git_cmds;
5877 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5880 if ($format eq 'incremental') {
5882 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
5883 "blame") . " (non-incremental)";
5886 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
5887 "blame") . " (incremental)";
5891 $cgi->a({-href => href(action=>"history", -replay=>1)},
5894 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
5896 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5897 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5898 git_print_page_path($file_name, $ftype, $hash_base);
5901 if ($format eq 'incremental') {
5902 print "<noscript>\n<div class=\"error\"><center><b>\n".
5903 "This page requires JavaScript to run.\n Use ".
5904 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
5907 "</b></center></div>\n</noscript>\n";
5909 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
5912 print qq!<div class="page_body">\n!;
5913 print qq!<div id="progress_info">... / ...</div>\n!
5914 if ($format eq 'incremental');
5915 print qq!<table id="blame_table" class="blame" width="100%">\n!.
5916 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5918 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
5922 my @rev_color = qw(light dark);
5923 my $num_colors = scalar(@rev_color);
5924 my $current_color = 0;
5926 if ($format eq 'incremental') {
5927 my $color_class = $rev_color[$current_color];
5932 while (my $line = <$fd>) {
5936 print qq!<tr id="l$linenr" class="$color_class">!.
5937 qq!<td class="sha1"><a href=""> </a></td>!.
5938 qq!<td class="linenr">!.
5939 qq!<a class="linenr" href="">$linenr</a></td>!;
5940 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
5944 } else { # porcelain, i.e. ordinary blame
5945 my %metainfo = (); # saves information about commits
5949 while (my $line = <$fd>) {
5951 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5952 # no <lines in group> for subsequent lines in group of lines
5953 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5954 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5955 if (!exists $metainfo{$full_rev}) {
5956 $metainfo{$full_rev} = { 'nprevious' => 0 };
5958 my $meta = $metainfo{$full_rev};
5960 while ($data = <$fd>) {
5962 last if ($data =~ s/^\t//); # contents of line
5963 if ($data =~ /^(\S+)(?: (.*))?$/) {
5964 $meta->{$1} = $2 unless exists $meta->{$1};
5966 if ($data =~ /^previous /) {
5967 $meta->{'nprevious'}++;
5970 my $short_rev = substr($full_rev, 0, 8);
5971 my $author = $meta->{'author'};
5973 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5974 my $date = $date{'iso-tz'};
5976 $current_color = ($current_color + 1) % $num_colors;
5978 my $tr_class = $rev_color[$current_color];
5979 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5980 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5981 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5982 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5984 print "<td class=\"sha1\"";
5985 print " title=\"". esc_html($author) . ", $date\"";
5986 print " rowspan=\"$group_size\"" if ($group_size > 1);
5988 print $cgi->a({-href => href(action=>"commit",
5990 file_name=>$file_name)},
5991 esc_html($short_rev));
5992 if ($group_size >= 2) {
5993 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5994 if (@author_initials) {
5996 esc_html(join('', @author_initials));
6002 # 'previous' <sha1 of parent commit> <filename at commit>
6003 if (exists $meta->{'previous'} &&
6004 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
6005 $meta->{'parent'} = $1;
6006 $meta->{'file_parent'} = unquote($2);
6009 exists($meta->{'parent'}) ?
6010 $meta->{'parent'} : $full_rev;
6011 my $linenr_filename =
6012 exists($meta->{'file_parent'}) ?
6013 $meta->{'file_parent'} : unquote($meta->{'filename'});
6014 my $blamed = href(action => 'blame',
6015 file_name => $linenr_filename,
6016 hash_base => $linenr_commit);
6017 print "<td class=\"linenr\">";
6018 print $cgi->a({ -href => "$blamed#l$orig_lineno",
6019 -class => "linenr" },
6022 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
6030 "</table>\n"; # class="blame"
6031 print "</div>\n"; # class="blame_body"
6033 or print "Reading blob failed\n";
6042 sub git_blame_incremental {
6043 git_blame_common('incremental');
6046 sub git_blame_data {
6047 git_blame_common('data');
6051 my $head = git_get_head_hash($project);
6053 git_print_page_nav('','', $head,undef,$head,format_ref_views('tags'));
6054 git_print_header_div('summary', $project);
6056 my @tagslist = git_get_tags_list();
6058 git_tags_body(\@tagslist);
6064 my $head = git_get_head_hash($project);
6066 git_print_page_nav('','', $head,undef,$head,format_ref_views('heads'));
6067 git_print_header_div('summary', $project);
6069 my @headslist = git_get_heads_list();
6071 git_heads_body(\@headslist, $head);
6076 # used both for single remote view and for list of all the remotes
6078 gitweb_check_feature('remote_heads')
6079 or die_error(403, "Remote heads view is disabled");
6081 my $head = git_get_head_hash($project);
6082 my $remote = $input_params{'hash'};
6084 my $remotedata = git_get_remotes_list($remote);
6085 die_error(500, "Unable to get remote information") unless defined $remotedata;
6087 unless (%$remotedata) {
6088 die_error(404, defined $remote ?
6089 "Remote $remote not found" :
6090 "No remotes found");
6093 git_header_html(undef, undef, -action_extra => $remote);
6094 git_print_page_nav('', '', $head, undef, $head,
6095 format_ref_views($remote ? '' : 'remotes'));
6097 fill_remote_heads($remotedata);
6098 if (defined $remote) {
6099 git_print_header_div('remotes', "$remote remote for $project");
6100 git_remote_block($remote, $remotedata->{$remote}, undef, $head);
6102 git_print_header_div('summary', "$project remotes");
6103 git_remotes_body($remotedata, undef, $head);
6109 sub git_blob_plain {
6113 if (!defined $hash) {
6114 if (defined $file_name) {
6115 my $base = $hash_base || git_get_head_hash($project);
6116 $hash = git_get_hash_by_path($base, $file_name, "blob")
6117 or die_error(404, "Cannot find file");
6119 die_error(400, "No file name defined");
6121 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6122 # blobs defined by non-textual hash id's can be cached
6126 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6127 or die_error(500, "Open git-cat-file blob '$hash' failed");
6129 # content-type (can include charset)
6130 $type = blob_contenttype($fd, $file_name, $type);
6132 # "save as" filename, even when no $file_name is given
6133 my $save_as = "$hash";
6134 if (defined $file_name) {
6135 $save_as = $file_name;
6136 } elsif ($type =~ m/^text\//) {
6140 # With XSS prevention on, blobs of all types except a few known safe
6141 # ones are served with "Content-Disposition: attachment" to make sure
6142 # they don't run in our security domain. For certain image types,
6143 # blob view writes an <img> tag referring to blob_plain view, and we
6144 # want to be sure not to break that by serving the image as an
6145 # attachment (though Firefox 3 doesn't seem to care).
6146 my $sandbox = $prevent_xss &&
6147 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))(?:[ ;]|$)!;
6151 -expires => $expires,
6152 -content_disposition =>
6153 ($sandbox ? 'attachment' : 'inline')
6154 . '; filename="' . $save_as . '"');
6156 binmode STDOUT, ':raw';
6158 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6165 if (!defined $hash) {
6166 if (defined $file_name) {
6167 my $base = $hash_base || git_get_head_hash($project);
6168 $hash = git_get_hash_by_path($base, $file_name, "blob")
6169 or die_error(404, "Cannot find file");
6171 die_error(400, "No file name defined");
6173 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6174 # blobs defined by non-textual hash id's can be cached
6178 my $have_blame = gitweb_check_feature('blame');
6179 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
6180 or die_error(500, "Couldn't cat $file_name, $hash");
6181 my $mimetype = blob_mimetype($fd, $file_name);
6182 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
6183 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
6185 return git_blob_plain($mimetype);
6187 # we can have blame only for text/* mimetype
6188 $have_blame &&= ($mimetype =~ m!^text/!);
6190 my $highlight = gitweb_check_feature('highlight');
6191 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
6192 $fd = run_highlighter($fd, $highlight, $syntax)
6195 git_header_html(undef, $expires);
6196 my $formats_nav = '';
6197 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6198 if (defined $file_name) {
6201 $cgi->a({-href => href(action=>"blame", -replay=>1)},
6206 $cgi->a({-href => href(action=>"history", -replay=>1)},
6209 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6212 $cgi->a({-href => href(action=>"blob",
6213 hash_base=>"HEAD", file_name=>$file_name)},
6217 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
6220 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6221 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6223 print "<div class=\"page_nav\">\n" .
6224 "<br/><br/></div>\n" .
6225 "<div class=\"title\">".esc_html($hash)."</div>\n";
6227 git_print_page_path($file_name, "blob", $hash_base);
6228 print "<div class=\"page_body\">\n";
6229 if ($mimetype =~ m!^image/!) {
6230 print qq!<img type="!.esc_attr($mimetype).qq!"!;
6232 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
6235 href(action=>"blob_plain", hash=>$hash,
6236 hash_base=>$hash_base, file_name=>$file_name) .
6240 while (my $line = <$fd>) {
6243 $line = untabify($line);
6244 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
6245 $nr, esc_attr(href(-replay => 1)), $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);
6249 or print "Reading blob failed.\n";
6255 if (!defined $hash_base) {
6256 $hash_base = "HEAD";
6258 if (!defined $hash) {
6259 if (defined $file_name) {
6260 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
6265 die_error(404, "No such tree") unless defined($hash);
6267 my $show_sizes = gitweb_check_feature('show-sizes');
6268 my $have_blame = gitweb_check_feature('blame');
6273 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
6274 ($show_sizes ? '-l' : ()), @extra_options, $hash
6275 or die_error(500, "Open git-ls-tree failed");
6276 @entries = map { chomp; $_ } <$fd>;
6278 or die_error(404, "Reading tree failed");
6281 my $refs = git_get_references();
6282 my $ref = format_ref_marker($refs, $hash_base);
6285 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6287 if (defined $file_name) {
6289 $cgi->a({-href => href(action=>"history", -replay=>1)},
6291 $cgi->a({-href => href(action=>"tree",
6292 hash_base=>"HEAD", file_name=>$file_name)},
6295 my $snapshot_links = format_snapshot_links($hash);
6296 if (defined $snapshot_links) {
6297 # FIXME: Should be available when we have no hash base as well.
6298 push @views_nav, $snapshot_links;
6300 git_print_page_nav('tree','', $hash_base, undef, undef,
6301 join(' | ', @views_nav));
6302 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
6305 print "<div class=\"page_nav\">\n";
6306 print "<br/><br/></div>\n";
6307 print "<div class=\"title\">".esc_html($hash)."</div>\n";
6309 if (defined $file_name) {
6310 $basedir = $file_name;
6311 if ($basedir ne '' && substr($basedir, -1) ne '/') {
6314 git_print_page_path($file_name, 'tree', $hash_base);
6316 print "<div class=\"page_body\">\n";
6317 print "<table class=\"tree\">\n";
6319 # '..' (top directory) link if possible
6320 if (defined $hash_base &&
6321 defined $file_name && $file_name =~ m![^/]+$!) {
6323 print "<tr class=\"dark\">\n";
6325 print "<tr class=\"light\">\n";
6329 my $up = $file_name;
6330 $up =~ s!/?[^/]+$!!;
6331 undef $up unless $up;
6332 # based on git_print_tree_entry
6333 print '<td class="mode">' . mode_str('040000') . "</td>\n";
6334 print '<td class="size"> </td>'."\n" if $show_sizes;
6335 print '<td class="list">';
6336 print $cgi->a({-href => href(action=>"tree",
6337 hash_base=>$hash_base,
6341 print "<td class=\"link\"></td>\n";
6345 foreach my $line (@entries) {
6346 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
6349 print "<tr class=\"dark\">\n";
6351 print "<tr class=\"light\">\n";
6355 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
6359 print "</table>\n" .
6365 my ($project, $hash) = @_;
6367 # path/to/project.git -> project
6368 # path/to/project/.git -> project
6369 my $name = to_utf8($project);
6370 $name =~ s,([^/])/*\.git$,$1,;
6371 $name = basename($name);
6373 $name =~ s/[[:cntrl:]]/?/g;
6376 if ($hash =~ /^[0-9a-fA-F]+$/) {
6377 # shorten SHA-1 hash
6378 my $full_hash = git_get_full_hash($project, $hash);
6379 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
6380 $ver = git_get_short_hash($project, $hash);
6382 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
6383 # tags don't need shortened SHA-1 hash
6386 # branches and other need shortened SHA-1 hash
6387 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
6390 $ver .= '-' . git_get_short_hash($project, $hash);
6392 # in case of hierarchical branch names
6395 # name = project-version_string
6396 $name = "$name-$ver";
6398 return wantarray ? ($name, $name) : $name;
6402 my $format = $input_params{'snapshot_format'};
6403 if (!@snapshot_fmts) {
6404 die_error(403, "Snapshots not allowed");
6406 # default to first supported snapshot format
6407 $format ||= $snapshot_fmts[0];
6408 if ($format !~ m/^[a-z0-9]+$/) {
6409 die_error(400, "Invalid snapshot format parameter");
6410 } elsif (!exists($known_snapshot_formats{$format})) {
6411 die_error(400, "Unknown snapshot format");
6412 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
6413 die_error(403, "Snapshot format not allowed");
6414 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
6415 die_error(403, "Unsupported snapshot format");
6418 my $type = git_get_type("$hash^{}");
6420 die_error(404, 'Object does not exist');
6421 } elsif ($type eq 'blob') {
6422 die_error(400, 'Object is not a tree-ish');
6425 my ($name, $prefix) = snapshot_name($project, $hash);
6426 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
6427 my $cmd = quote_command(
6428 git_cmd(), 'archive',
6429 "--format=$known_snapshot_formats{$format}{'format'}",
6430 "--prefix=$prefix/", $hash);
6431 if (exists $known_snapshot_formats{$format}{'compressor'}) {
6432 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
6435 $filename =~ s/(["\\])/\\$1/g;
6437 -type => $known_snapshot_formats{$format}{'type'},
6438 -content_disposition => 'inline; filename="' . $filename . '"',
6439 -status => '200 OK');
6441 open my $fd, "-|", $cmd
6442 or die_error(500, "Execute git-archive failed");
6443 binmode STDOUT, ':raw';
6445 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
6449 sub git_log_generic {
6450 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
6452 my $head = git_get_head_hash($project);
6453 if (!defined $base) {
6456 if (!defined $page) {
6459 my $refs = git_get_references();
6461 my $commit_hash = $base;
6462 if (defined $parent) {
6463 $commit_hash = "$parent..$base";
6466 parse_commits($commit_hash, 101, (100 * $page),
6467 defined $file_name ? ($file_name, "--full-history") : ());
6470 if (!defined $file_hash && defined $file_name) {
6471 # some commits could have deleted file in question,
6472 # and not have it in tree, but one of them has to have it
6473 for (my $i = 0; $i < @commitlist; $i++) {
6474 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
6475 last if defined $file_hash;
6478 if (defined $file_hash) {
6479 $ftype = git_get_type($file_hash);
6481 if (defined $file_name && !defined $ftype) {
6482 die_error(500, "Unknown type of object");
6485 if (defined $file_name) {
6486 %co = parse_commit($base)
6487 or die_error(404, "Unknown commit object");
6491 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
6493 if ($#commitlist >= 100) {
6495 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6496 -accesskey => "n", -title => "Alt-n"}, "next");
6498 my $patch_max = gitweb_get_feature('patches');
6499 if ($patch_max && !defined $file_name) {
6500 if ($patch_max < 0 || @commitlist <= $patch_max) {
6501 $paging_nav .= " ⋅ " .
6502 $cgi->a({-href => href(action=>"patches", -replay=>1)},
6508 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
6509 if (defined $file_name) {
6510 git_print_header_div('commit', esc_html($co{'title'}), $base);
6512 git_print_header_div('summary', $project)
6514 git_print_page_path($file_name, $ftype, $hash_base)
6515 if (defined $file_name);
6517 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
6518 $file_name, $file_hash, $ftype);
6524 git_log_generic('log', \&git_log_body,
6525 $hash, $hash_parent);
6529 $hash ||= $hash_base || "HEAD";
6530 my %co = parse_commit($hash)
6531 or die_error(404, "Unknown commit object");
6533 my $parent = $co{'parent'};
6534 my $parents = $co{'parents'}; # listref
6536 # we need to prepare $formats_nav before any parameter munging
6538 if (!defined $parent) {
6540 $formats_nav .= '(initial)';
6541 } elsif (@$parents == 1) {
6542 # single parent commit
6545 $cgi->a({-href => href(action=>"commit",
6547 esc_html(substr($parent, 0, 7))) .
6554 $cgi->a({-href => href(action=>"commit",
6556 esc_html(substr($_, 0, 7)));
6560 if (gitweb_check_feature('patches') && @$parents <= 1) {
6561 $formats_nav .= " | " .
6562 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6566 if (!defined $parent) {
6570 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
6572 (@$parents <= 1 ? $parent : '-c'),
6574 or die_error(500, "Open git-diff-tree failed");
6575 @difftree = map { chomp; $_ } <$fd>;
6576 close $fd or die_error(404, "Reading git-diff-tree failed");
6578 # non-textual hash id's can be cached
6580 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6583 my $refs = git_get_references();
6584 my $ref = format_ref_marker($refs, $co{'id'});
6586 git_header_html(undef, $expires);
6587 git_print_page_nav('commit', '',
6588 $hash, $co{'tree'}, $hash,
6591 if (defined $co{'parent'}) {
6592 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
6594 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
6596 print "<div class=\"title_text\">\n" .
6597 "<table class=\"object_header\">\n";
6598 git_print_authorship_rows(\%co);
6599 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6602 "<td class=\"sha1\">" .
6603 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
6604 class => "list"}, $co{'tree'}) .
6606 "<td class=\"link\">" .
6607 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
6609 my $snapshot_links = format_snapshot_links($hash);
6610 if (defined $snapshot_links) {
6611 print " | " . $snapshot_links;
6616 foreach my $par (@$parents) {
6619 "<td class=\"sha1\">" .
6620 $cgi->a({-href => href(action=>"commit", hash=>$par),
6621 class => "list"}, $par) .
6623 "<td class=\"link\">" .
6624 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
6626 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
6633 print "<div class=\"page_body\">\n";
6634 git_print_log($co{'comment'});
6637 git_difftree_body(\@difftree, $hash, @$parents);
6643 # object is defined by:
6644 # - hash or hash_base alone
6645 # - hash_base and file_name
6648 # - hash or hash_base alone
6649 if ($hash || ($hash_base && !defined $file_name)) {
6650 my $object_id = $hash || $hash_base;
6652 open my $fd, "-|", quote_command(
6653 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6654 or die_error(404, "Object does not exist");
6658 or die_error(404, "Object does not exist");
6660 # - hash_base and file_name
6661 } elsif ($hash_base && defined $file_name) {
6662 $file_name =~ s,/+$,,;
6664 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
6665 or die_error(404, "Base object does not exist");
6667 # here errors should not hapen
6668 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
6669 or die_error(500, "Open git-ls-tree failed");
6673 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6674 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6675 die_error(404, "File or directory for given base does not exist");
6680 die_error(400, "Not enough information to find object");
6683 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
6684 hash=>$hash, hash_base=>$hash_base,
6685 file_name=>$file_name),
6686 -status => '302 Found');
6690 my $format = shift || 'html';
6697 # preparing $fd and %diffinfo for git_patchset_body
6699 if (defined $hash_base && defined $hash_parent_base) {
6700 if (defined $file_name) {
6702 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6703 $hash_parent_base, $hash_base,
6704 "--", (defined $file_parent ? $file_parent : ()), $file_name
6705 or die_error(500, "Open git-diff-tree failed");
6706 @difftree = map { chomp; $_ } <$fd>;
6708 or die_error(404, "Reading git-diff-tree failed");
6710 or die_error(404, "Blob diff not found");
6712 } elsif (defined $hash &&
6713 $hash =~ /[0-9a-fA-F]{40}/) {
6714 # try to find filename from $hash
6716 # read filtered raw output
6717 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6718 $hash_parent_base, $hash_base, "--"
6719 or die_error(500, "Open git-diff-tree failed");
6721 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6723 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6724 map { chomp; $_ } <$fd>;
6726 or die_error(404, "Reading git-diff-tree failed");
6728 or die_error(404, "Blob diff not found");
6731 die_error(400, "Missing one of the blob diff parameters");
6734 if (@difftree > 1) {
6735 die_error(400, "Ambiguous blob diff specification");
6738 %diffinfo = parse_difftree_raw_line($difftree[0]);
6739 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6740 $file_name ||= $diffinfo{'to_file'};
6742 $hash_parent ||= $diffinfo{'from_id'};
6743 $hash ||= $diffinfo{'to_id'};
6745 # non-textual hash id's can be cached
6746 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6747 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6752 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6753 '-p', ($format eq 'html' ? "--full-index" : ()),
6754 $hash_parent_base, $hash_base,
6755 "--", (defined $file_parent ? $file_parent : ()), $file_name
6756 or die_error(500, "Open git-diff-tree failed");
6759 # old/legacy style URI -- not generated anymore since 1.4.3.
6761 die_error('404 Not Found', "Missing one of the blob diff parameters")
6765 if ($format eq 'html') {
6767 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
6769 git_header_html(undef, $expires);
6770 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6771 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6772 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6774 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6775 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
6777 if (defined $file_name) {
6778 git_print_page_path($file_name, "blob", $hash_base);
6780 print "<div class=\"page_path\"></div>\n";
6783 } elsif ($format eq 'plain') {
6785 -type => 'text/plain',
6786 -charset => 'utf-8',
6787 -expires => $expires,
6788 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
6790 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6793 die_error(400, "Unknown blobdiff format");
6797 if ($format eq 'html') {
6798 print "<div class=\"page_body\">\n";
6800 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
6803 print "</div>\n"; # class="page_body"
6807 while (my $line = <$fd>) {
6808 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6809 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6813 last if $line =~ m!^\+\+\+!;
6821 sub git_blobdiff_plain {
6822 git_blobdiff('plain');
6825 sub git_commitdiff {
6827 my $format = $params{-format} || 'html';
6829 my ($patch_max) = gitweb_get_feature('patches');
6830 if ($format eq 'patch') {
6831 die_error(403, "Patch view not allowed") unless $patch_max;
6834 $hash ||= $hash_base || "HEAD";
6835 my %co = parse_commit($hash)
6836 or die_error(404, "Unknown commit object");
6838 # choose format for commitdiff for merge
6839 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
6840 $hash_parent = '--cc';
6842 # we need to prepare $formats_nav before almost any parameter munging
6844 if ($format eq 'html') {
6846 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
6848 if ($patch_max && @{$co{'parents'}} <= 1) {
6849 $formats_nav .= " | " .
6850 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6854 if (defined $hash_parent &&
6855 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6856 # commitdiff with two commits given
6857 my $hash_parent_short = $hash_parent;
6858 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6859 $hash_parent_short = substr($hash_parent, 0, 7);
6863 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6864 if ($co{'parents'}[$i] eq $hash_parent) {
6865 $formats_nav .= ' parent ' . ($i+1);
6869 $formats_nav .= ': ' .
6870 $cgi->a({-href => href(action=>"commitdiff",
6871 hash=>$hash_parent)},
6872 esc_html($hash_parent_short)) .
6874 } elsif (!$co{'parent'}) {
6876 $formats_nav .= ' (initial)';
6877 } elsif (scalar @{$co{'parents'}} == 1) {
6878 # single parent commit
6881 $cgi->a({-href => href(action=>"commitdiff",
6882 hash=>$co{'parent'})},
6883 esc_html(substr($co{'parent'}, 0, 7))) .
6887 if ($hash_parent eq '--cc') {
6888 $formats_nav .= ' | ' .
6889 $cgi->a({-href => href(action=>"commitdiff",
6890 hash=>$hash, hash_parent=>'-c')},
6892 } else { # $hash_parent eq '-c'
6893 $formats_nav .= ' | ' .
6894 $cgi->a({-href => href(action=>"commitdiff",
6895 hash=>$hash, hash_parent=>'--cc')},
6901 $cgi->a({-href => href(action=>"commitdiff",
6903 esc_html(substr($_, 0, 7)));
6904 } @{$co{'parents'}} ) .
6909 my $hash_parent_param = $hash_parent;
6910 if (!defined $hash_parent_param) {
6911 # --cc for multiple parents, --root for parentless
6912 $hash_parent_param =
6913 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6919 if ($format eq 'html') {
6920 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6921 "--no-commit-id", "--patch-with-raw", "--full-index",
6922 $hash_parent_param, $hash, "--"
6923 or die_error(500, "Open git-diff-tree failed");
6925 while (my $line = <$fd>) {
6927 # empty line ends raw part of diff-tree output
6929 push @difftree, scalar parse_difftree_raw_line($line);
6932 } elsif ($format eq 'plain') {
6933 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6934 '-p', $hash_parent_param, $hash, "--"
6935 or die_error(500, "Open git-diff-tree failed");
6936 } elsif ($format eq 'patch') {
6937 # For commit ranges, we limit the output to the number of
6938 # patches specified in the 'patches' feature.
6939 # For single commits, we limit the output to a single patch,
6940 # diverging from the git-format-patch default.
6941 my @commit_spec = ();
6943 if ($patch_max > 0) {
6944 push @commit_spec, "-$patch_max";
6946 push @commit_spec, '-n', "$hash_parent..$hash";
6948 if ($params{-single}) {
6949 push @commit_spec, '-1';
6951 if ($patch_max > 0) {
6952 push @commit_spec, "-$patch_max";
6954 push @commit_spec, "-n";
6956 push @commit_spec, '--root', $hash;
6958 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
6959 '--encoding=utf8', '--stdout', @commit_spec
6960 or die_error(500, "Open git-format-patch failed");
6962 die_error(400, "Unknown commitdiff format");
6965 # non-textual hash id's can be cached
6967 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6971 # write commit message
6972 if ($format eq 'html') {
6973 my $refs = git_get_references();
6974 my $ref = format_ref_marker($refs, $co{'id'});
6976 git_header_html(undef, $expires);
6977 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6978 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6979 print "<div class=\"title_text\">\n" .
6980 "<table class=\"object_header\">\n";
6981 git_print_authorship_rows(\%co);
6984 print "<div class=\"page_body\">\n";
6985 if (@{$co{'comment'}} > 1) {
6986 print "<div class=\"log\">\n";
6987 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6988 print "</div>\n"; # class="log"
6991 } elsif ($format eq 'plain') {
6992 my $refs = git_get_references("tags");
6993 my $tagname = git_get_rev_name_tags($hash);
6994 my $filename = basename($project) . "-$hash.patch";
6997 -type => 'text/plain',
6998 -charset => 'utf-8',
6999 -expires => $expires,
7000 -content_disposition => 'inline; filename="' . "$filename" . '"');
7001 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
7002 print "From: " . to_utf8($co{'author'}) . "\n";
7003 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
7004 print "Subject: " . to_utf8($co{'title'}) . "\n";
7006 print "X-Git-Tag: $tagname\n" if $tagname;
7007 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
7009 foreach my $line (@{$co{'comment'}}) {
7010 print to_utf8($line) . "\n";
7013 } elsif ($format eq 'patch') {
7014 my $filename = basename($project) . "-$hash.patch";
7017 -type => 'text/plain',
7018 -charset => 'utf-8',
7019 -expires => $expires,
7020 -content_disposition => 'inline; filename="' . "$filename" . '"');
7024 if ($format eq 'html') {
7025 my $use_parents = !defined $hash_parent ||
7026 $hash_parent eq '-c' || $hash_parent eq '--cc';
7027 git_difftree_body(\@difftree, $hash,
7028 $use_parents ? @{$co{'parents'}} : $hash_parent);
7031 git_patchset_body($fd, \@difftree, $hash,
7032 $use_parents ? @{$co{'parents'}} : $hash_parent);
7034 print "</div>\n"; # class="page_body"
7037 } elsif ($format eq 'plain') {
7041 or print "Reading git-diff-tree failed\n";
7042 } elsif ($format eq 'patch') {
7046 or print "Reading git-format-patch failed\n";
7050 sub git_commitdiff_plain {
7051 git_commitdiff(-format => 'plain');
7054 # format-patch-style patches
7056 git_commitdiff(-format => 'patch', -single => 1);
7060 git_commitdiff(-format => 'patch');
7064 git_log_generic('history', \&git_history_body,
7065 $hash_base, $hash_parent_base,
7070 gitweb_check_feature('search') or die_error(403, "Search is disabled");
7071 if (!defined $searchtext) {
7072 die_error(400, "Text field is empty");
7074 if (!defined $hash) {
7075 $hash = git_get_head_hash($project);
7077 my %co = parse_commit($hash);
7079 die_error(404, "Unknown commit object");
7081 if (!defined $page) {
7085 $searchtype ||= 'commit';
7086 if ($searchtype eq 'pickaxe') {
7087 # pickaxe may take all resources of your box and run for several minutes
7088 # with every query - so decide by yourself how public you make this feature
7089 gitweb_check_feature('pickaxe')
7090 or die_error(403, "Pickaxe is disabled");
7092 if ($searchtype eq 'grep') {
7093 gitweb_check_feature('grep')
7094 or die_error(403, "Grep is disabled");
7099 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
7101 if ($searchtype eq 'commit') {
7102 $greptype = "--grep=";
7103 } elsif ($searchtype eq 'author') {
7104 $greptype = "--author=";
7105 } elsif ($searchtype eq 'committer') {
7106 $greptype = "--committer=";
7108 $greptype .= $searchtext;
7109 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
7110 $greptype, '--regexp-ignore-case',
7111 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
7113 my $paging_nav = '';
7116 $cgi->a({-href => href(action=>"search", hash=>$hash,
7117 searchtext=>$searchtext,
7118 searchtype=>$searchtype)},
7120 $paging_nav .= " ⋅ " .
7121 $cgi->a({-href => href(-replay=>1, page=>$page-1),
7122 -accesskey => "p", -title => "Alt-p"}, "prev");
7124 $paging_nav .= "first";
7125 $paging_nav .= " ⋅ prev";
7128 if ($#commitlist >= 100) {
7130 $cgi->a({-href => href(-replay=>1, page=>$page+1),
7131 -accesskey => "n", -title => "Alt-n"}, "next");
7132 $paging_nav .= " ⋅ $next_link";
7134 $paging_nav .= " ⋅ next";
7137 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
7138 git_print_header_div('commit', esc_html($co{'title'}), $hash);
7139 if ($page == 0 && !@commitlist) {
7140 print "<p>No match.</p>\n";
7142 git_search_grep_body(\@commitlist, 0, 99, $next_link);
7146 if ($searchtype eq 'pickaxe') {
7147 git_print_page_nav('','', $hash,$co{'tree'},$hash);
7148 git_print_header_div('commit', esc_html($co{'title'}), $hash);
7150 print "<table class=\"pickaxe search\">\n";
7153 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
7154 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
7155 ($search_use_regexp ? '--pickaxe-regex' : ());
7158 while (my $line = <$fd>) {
7162 my %set = parse_difftree_raw_line($line);
7163 if (defined $set{'commit'}) {
7164 # finish previous commit
7167 "<td class=\"link\">" .
7168 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
7170 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
7176 print "<tr class=\"dark\">\n";
7178 print "<tr class=\"light\">\n";
7181 %co = parse_commit($set{'commit'});
7182 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
7183 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
7184 "<td><i>$author</i></td>\n" .
7186 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
7187 -class => "list subject"},
7188 chop_and_escape_str($co{'title'}, 50) . "<br/>");
7189 } elsif (defined $set{'to_id'}) {
7190 next if ($set{'to_id'} =~ m/^0{40}$/);
7192 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
7193 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
7195 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
7201 # finish last commit (warning: repetition!)
7204 "<td class=\"link\">" .
7205 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
7207 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
7215 if ($searchtype eq 'grep') {
7216 git_print_page_nav('','', $hash,$co{'tree'},$hash);
7217 git_print_header_div('commit', esc_html($co{'title'}), $hash);
7219 print "<table class=\"grep_search\">\n";
7223 open my $fd, "-|", git_cmd(), 'grep', '-n',
7224 $search_use_regexp ? ('-E', '-i') : '-F',
7225 $searchtext, $co{'tree'};
7227 while (my $line = <$fd>) {
7229 my ($file, $lno, $ltext, $binary);
7230 last if ($matches++ > 1000);
7231 if ($line =~ /^Binary file (.+) matches$/) {
7235 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
7237 if ($file ne $lastfile) {
7238 $lastfile and print "</td></tr>\n";
7240 print "<tr class=\"dark\">\n";
7242 print "<tr class=\"light\">\n";
7244 print "<td class=\"list\">".
7245 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
7246 file_name=>"$file"),
7247 -class => "list"}, esc_path($file));
7248 print "</td><td>\n";
7252 print "<div class=\"binary\">Binary file</div>\n";
7254 $ltext = untabify($ltext);
7255 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
7256 $ltext = esc_html($1, -nbsp=>1);
7257 $ltext .= '<span class="match">';
7258 $ltext .= esc_html($2, -nbsp=>1);
7259 $ltext .= '</span>';
7260 $ltext .= esc_html($3, -nbsp=>1);
7262 $ltext = esc_html($ltext, -nbsp=>1);
7264 print "<div class=\"pre\">" .
7265 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
7266 file_name=>"$file").'#l'.$lno,
7267 -class => "linenr"}, sprintf('%4i', $lno))
7268 . ' ' . $ltext . "</div>\n";
7272 print "</td></tr>\n";
7273 if ($matches > 1000) {
7274 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
7277 print "<div class=\"diff nodifferences\">No matches found</div>\n";
7286 sub git_search_help {
7288 git_print_page_nav('','', $hash,$hash,$hash);
7290 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
7291 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
7292 the pattern entered is recognized as the POSIX extended
7293 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
7296 <dt><b>commit</b></dt>
7297 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
7299 my $have_grep = gitweb_check_feature('grep');
7302 <dt><b>grep</b></dt>
7303 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
7304 a different one) are searched for the given pattern. On large trees, this search can take
7305 a while and put some strain on the server, so please use it with some consideration. Note that
7306 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
7307 case-sensitive.</dd>
7311 <dt><b>author</b></dt>
7312 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
7313 <dt><b>committer</b></dt>
7314 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
7316 my $have_pickaxe = gitweb_check_feature('pickaxe');
7317 if ($have_pickaxe) {
7319 <dt><b>pickaxe</b></dt>
7320 <dd>All commits that caused the string to appear or disappear from any file (changes that
7321 added, removed or "modified" the string) will be listed. This search can take a while and
7322 takes a lot of strain on the server, so please use it wisely. Note that since you may be
7323 interested even in changes just changing the case as well, this search is case sensitive.</dd>
7331 git_log_generic('shortlog', \&git_shortlog_body,
7332 $hash, $hash_parent);
7335 ## ......................................................................
7336 ## feeds (RSS, Atom; OPML)
7339 my $format = shift || 'atom';
7340 my $have_blame = gitweb_check_feature('blame');
7342 # Atom: http://www.atomenabled.org/developers/syndication/
7343 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
7344 if ($format ne 'rss' && $format ne 'atom') {
7345 die_error(400, "Unknown web feed format");
7348 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
7349 my $head = $hash || 'HEAD';
7350 my @commitlist = parse_commits($head, 150, 0, $file_name);
7354 my $content_type = "application/$format+xml";
7355 if (defined $cgi->http('HTTP_ACCEPT') &&
7356 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
7357 # browser (feed reader) prefers text/xml
7358 $content_type = 'text/xml';
7360 if (defined($commitlist[0])) {
7361 %latest_commit = %{$commitlist[0]};
7362 my $latest_epoch = $latest_commit{'committer_epoch'};
7363 %latest_date = parse_date($latest_epoch, $latest_commit{'comitter_tz'});
7364 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
7365 if (defined $if_modified) {
7367 if (eval { require HTTP::Date; 1; }) {
7368 $since = HTTP::Date::str2time($if_modified);
7369 } elsif (eval { require Time::ParseDate; 1; }) {
7370 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
7372 if (defined $since && $latest_epoch <= $since) {
7374 -type => $content_type,
7375 -charset => 'utf-8',
7376 -last_modified => $latest_date{'rfc2822'},
7377 -status => '304 Not Modified');
7382 -type => $content_type,
7383 -charset => 'utf-8',
7384 -last_modified => $latest_date{'rfc2822'});
7387 -type => $content_type,
7388 -charset => 'utf-8');
7391 # Optimization: skip generating the body if client asks only
7392 # for Last-Modified date.
7393 return if ($cgi->request_method() eq 'HEAD');
7396 my $title = "$site_name - $project/$action";
7397 my $feed_type = 'log';
7398 if (defined $hash) {
7399 $title .= " - '$hash'";
7400 $feed_type = 'branch log';
7401 if (defined $file_name) {
7402 $title .= " :: $file_name";
7403 $feed_type = 'history';
7405 } elsif (defined $file_name) {
7406 $title .= " - $file_name";
7407 $feed_type = 'history';
7409 $title .= " $feed_type";
7410 my $descr = git_get_project_description($project);
7411 if (defined $descr) {
7412 $descr = esc_html($descr);
7414 $descr = "$project " .
7415 ($format eq 'rss' ? 'RSS' : 'Atom') .
7418 my $owner = git_get_project_owner($project);
7419 $owner = esc_html($owner);
7423 if (defined $file_name) {
7424 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
7425 } elsif (defined $hash) {
7426 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
7428 $alt_url = href(-full=>1, action=>"summary");
7430 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
7431 if ($format eq 'rss') {
7433 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
7436 print "<title>$title</title>\n" .
7437 "<link>$alt_url</link>\n" .
7438 "<description>$descr</description>\n" .
7439 "<language>en</language>\n" .
7440 # project owner is responsible for 'editorial' content
7441 "<managingEditor>$owner</managingEditor>\n";
7442 if (defined $logo || defined $favicon) {
7443 # prefer the logo to the favicon, since RSS
7444 # doesn't allow both
7445 my $img = esc_url($logo || $favicon);
7447 "<url>$img</url>\n" .
7448 "<title>$title</title>\n" .
7449 "<link>$alt_url</link>\n" .
7453 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
7454 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
7456 print "<generator>gitweb v.$version/$git_version</generator>\n";
7457 } elsif ($format eq 'atom') {
7459 <feed xmlns="http://www.w3.org/2005/Atom">
7461 print "<title>$title</title>\n" .
7462 "<subtitle>$descr</subtitle>\n" .
7463 '<link rel="alternate" type="text/html" href="' .
7464 $alt_url . '" />' . "\n" .
7465 '<link rel="self" type="' . $content_type . '" href="' .
7466 $cgi->self_url() . '" />' . "\n" .
7467 "<id>" . href(-full=>1) . "</id>\n" .
7468 # use project owner for feed author
7469 "<author><name>$owner</name></author>\n";
7470 if (defined $favicon) {
7471 print "<icon>" . esc_url($favicon) . "</icon>\n";
7473 if (defined $logo) {
7474 # not twice as wide as tall: 72 x 27 pixels
7475 print "<logo>" . esc_url($logo) . "</logo>\n";
7477 if (! %latest_date) {
7478 # dummy date to keep the feed valid until commits trickle in:
7479 print "<updated>1970-01-01T00:00:00Z</updated>\n";
7481 print "<updated>$latest_date{'iso-8601'}</updated>\n";
7483 print "<generator version='$version/$git_version'>gitweb</generator>\n";
7487 for (my $i = 0; $i <= $#commitlist; $i++) {
7488 my %co = %{$commitlist[$i]};
7489 my $commit = $co{'id'};
7490 # we read 150, we always show 30 and the ones more recent than 48 hours
7491 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
7494 my %cd = parse_date($co{'author_epoch'}, $co{'author_tz'});
7496 # get list of changed files
7497 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
7498 $co{'parent'} || "--root",
7499 $co{'id'}, "--", (defined $file_name ? $file_name : ())
7501 my @difftree = map { chomp; $_ } <$fd>;
7505 # print element (entry, item)
7506 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
7507 if ($format eq 'rss') {
7509 "<title>" . esc_html($co{'title'}) . "</title>\n" .
7510 "<author>" . esc_html($co{'author'}) . "</author>\n" .
7511 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
7512 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
7513 "<link>$co_url</link>\n" .
7514 "<description>" . esc_html($co{'title'}) . "</description>\n" .
7515 "<content:encoded>" .
7517 } elsif ($format eq 'atom') {
7519 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
7520 "<updated>$cd{'iso-8601'}</updated>\n" .
7522 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
7523 if ($co{'author_email'}) {
7524 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
7526 print "</author>\n" .
7527 # use committer for contributor
7529 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
7530 if ($co{'committer_email'}) {
7531 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
7533 print "</contributor>\n" .
7534 "<published>$cd{'iso-8601'}</published>\n" .
7535 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
7536 "<id>$co_url</id>\n" .
7537 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
7538 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
7540 my $comment = $co{'comment'};
7542 foreach my $line (@$comment) {
7543 $line = esc_html($line);
7546 print "</pre><ul>\n";
7547 foreach my $difftree_line (@difftree) {
7548 my %difftree = parse_difftree_raw_line($difftree_line);
7549 next if !$difftree{'from_id'};
7551 my $file = $difftree{'file'} || $difftree{'to_file'};
7555 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
7556 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
7557 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
7558 file_name=>$file, file_parent=>$difftree{'from_file'}),
7559 -title => "diff"}, 'D');
7561 print $cgi->a({-href => href(-full=>1, action=>"blame",
7562 file_name=>$file, hash_base=>$commit),
7563 -title => "blame"}, 'B');
7565 # if this is not a feed of a file history
7566 if (!defined $file_name || $file_name ne $file) {
7567 print $cgi->a({-href => href(-full=>1, action=>"history",
7568 file_name=>$file, hash=>$commit),
7569 -title => "history"}, 'H');
7571 $file = esc_path($file);
7575 if ($format eq 'rss') {
7576 print "</ul>]]>\n" .
7577 "</content:encoded>\n" .
7579 } elsif ($format eq 'atom') {
7580 print "</ul>\n</div>\n" .
7587 if ($format eq 'rss') {
7588 print "</channel>\n</rss>\n";
7589 } elsif ($format eq 'atom') {
7603 my @list = git_get_projects_list();
7605 die_error(404, "No projects found");
7609 -type => 'text/xml',
7610 -charset => 'utf-8',
7611 -content_disposition => 'inline; filename="opml.xml"');
7614 <?xml version="1.0" encoding="utf-8"?>
7615 <opml version="1.0">
7617 <title>$site_name OPML Export</title>
7620 <outline text="git RSS feeds">
7623 foreach my $pr (@list) {
7625 my $head = git_get_head_hash($proj{'path'});
7626 if (!defined $head) {
7629 $git_dir = "$projectroot/$proj{'path'}";
7630 my %co = parse_commit($head);
7635 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
7636 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
7637 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
7638 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";