3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
22 if (eval { require Time::HiRes; 1; }) {
23 $t0 = [Time::HiRes::gettimeofday()];
25 our $number_of_git_cmds = 0;
28 CGI->compile() if $ENV{'MOD_PERL'};
32 our $version = "++GIT_VERSION++";
33 our $my_url = $cgi->url();
34 our $my_uri = $cgi->url(-absolute => 1);
36 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
37 # needed and used only for URLs with nonempty PATH_INFO
38 our $base_url = $my_url;
40 # When the script is used as DirectoryIndex, the URL does not contain the name
41 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
42 # have to do it ourselves. We make $path_info global because it's also used
45 # Another issue with the script being the DirectoryIndex is that the resulting
46 # $my_url data is not the full script URL: this is good, because we want
47 # generated links to keep implying the script name if it wasn't explicitly
48 # indicated in the URL we're handling, but it means that $my_url cannot be used
50 # Therefore, if we needed to strip PATH_INFO, then we know that we have
51 # to build the base URL ourselves:
52 our $path_info = $ENV{"PATH_INFO"};
54 if ($my_url =~ s,\Q$path_info\E$,, &&
55 $my_uri =~ s,\Q$path_info\E$,, &&
56 defined $ENV{'SCRIPT_NAME'}) {
57 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
61 # core git executable to use
62 # this can just be "git" if your webserver has a sensible PATH
63 our $GIT = "++GIT_BINDIR++/git";
65 # absolute fs-path which will be prepended to the project path
66 #our $projectroot = "/pub/scm";
67 our $projectroot = "++GITWEB_PROJECTROOT++";
69 # fs traversing limit for getting project list
70 # the number is relative to the projectroot
71 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
73 # target of the home link on top of all pages
74 our $home_link = $my_uri || "/";
76 # string of the home link on top of all pages
77 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
79 # name of your site or organization to appear in page titles
80 # replace this with something more descriptive for clearer bookmarks
81 our $site_name = "++GITWEB_SITENAME++"
82 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
84 # filename of html text to include at top of each page
85 our $site_header = "++GITWEB_SITE_HEADER++";
86 # html text to include at home page
87 our $home_text = "++GITWEB_HOMETEXT++";
88 # filename of html text to include at bottom of each page
89 our $site_footer = "++GITWEB_SITE_FOOTER++";
92 our @stylesheets = ("++GITWEB_CSS++");
93 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
94 our $stylesheet = undef;
95 # URI of GIT logo (72x27 size)
96 our $logo = "++GITWEB_LOGO++";
97 # URI of GIT favicon, assumed to be image/png type
98 our $favicon = "++GITWEB_FAVICON++";
99 # URI of gitweb.js (JavaScript code for gitweb)
100 our $javascript = "++GITWEB_JS++";
102 # URI and label (title) of GIT logo link
103 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
104 #our $logo_label = "git documentation";
105 our $logo_url = "http://git-scm.com/";
106 our $logo_label = "git homepage";
108 # source of projects list
109 our $projects_list = "++GITWEB_LIST++";
111 # the width (in characters) of the projects list "Description" column
112 our $projects_list_description_width = 25;
114 # default order of projects list
115 # valid values are none, project, descr, owner, and age
116 our $default_projects_order = "project";
118 # show repository only if this file exists
119 # (only effective if this variable evaluates to true)
120 our $export_ok = "++GITWEB_EXPORT_OK++";
122 # show repository only if this subroutine returns true
123 # when given the path to the project, for example:
124 # sub { return -e "$_[0]/git-daemon-export-ok"; }
125 our $export_auth_hook = undef;
127 # only allow viewing of repositories also shown on the overview page
128 our $strict_export = "++GITWEB_STRICT_EXPORT++";
130 # list of git base URLs used for URL to where fetch project from,
131 # i.e. full URL is "$git_base_url/$project"
132 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
134 # default blob_plain mimetype and default charset for text/plain blob
135 our $default_blob_plain_mimetype = 'text/plain';
136 our $default_text_plain_charset = undef;
138 # file to use for guessing MIME types before trying /etc/mime.types
139 # (relative to the current git repository)
140 our $mimetypes_file = undef;
142 # assume this charset if line contains non-UTF-8 characters;
143 # it should be valid encoding (see Encoding::Supported(3pm) for list),
144 # for which encoding all byte sequences are valid, for example
145 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
146 # could be even 'utf-8' for the old behavior)
147 our $fallback_encoding = 'latin1';
149 # rename detection options for git-diff and git-diff-tree
150 # - default is '-M', with the cost proportional to
151 # (number of removed files) * (number of new files).
152 # - more costly is '-C' (which implies '-M'), with the cost proportional to
153 # (number of changed files + number of removed files) * (number of new files)
154 # - even more costly is '-C', '--find-copies-harder' with cost
155 # (number of files in the original tree) * (number of new files)
156 # - one might want to include '-B' option, e.g. '-B', '-M'
157 our @diff_opts = ('-M'); # taken from git_commit
159 # Disables features that would allow repository owners to inject script into
161 our $prevent_xss = 0;
163 # information about snapshot formats that gitweb is capable of serving
164 our %known_snapshot_formats = (
166 # 'display' => display name,
167 # 'type' => mime type,
168 # 'suffix' => filename suffix,
169 # 'format' => --format for git-archive,
170 # 'compressor' => [compressor command and arguments]
171 # (array reference, optional)
172 # 'disabled' => boolean (optional)}
175 'display' => 'tar.gz',
176 'type' => 'application/x-gzip',
177 'suffix' => '.tar.gz',
179 'compressor' => ['gzip']},
182 'display' => 'tar.bz2',
183 'type' => 'application/x-bzip2',
184 'suffix' => '.tar.bz2',
186 'compressor' => ['bzip2']},
189 'display' => 'tar.xz',
190 'type' => 'application/x-xz',
191 'suffix' => '.tar.xz',
193 'compressor' => ['xz'],
198 'type' => 'application/x-zip',
203 # Aliases so we understand old gitweb.snapshot values in repository
205 our %known_snapshot_format_aliases = (
210 # backward compatibility: legacy gitweb config support
211 'x-gzip' => undef, 'gz' => undef,
212 'x-bzip2' => undef, 'bz2' => undef,
213 'x-zip' => undef, '' => undef,
216 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
217 # are changed, it may be appropriate to change these values too via
224 # Used to set the maximum load that we will still respond to gitweb queries.
225 # If server load exceed this value then return "503 server busy" error.
226 # If gitweb cannot determined server load, it is taken to be 0.
227 # Leave it undefined (or set to 'undef') to turn off load checking.
230 # You define site-wide feature defaults here; override them with
231 # $GITWEB_CONFIG as necessary.
234 # 'sub' => feature-sub (subroutine),
235 # 'override' => allow-override (boolean),
236 # 'default' => [ default options...] (array reference)}
238 # if feature is overridable (it means that allow-override has true value),
239 # then feature-sub will be called with default options as parameters;
240 # return value of feature-sub indicates if to enable specified feature
242 # if there is no 'sub' key (no feature-sub), then feature cannot be
245 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
246 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
249 # Enable the 'blame' blob view, showing the last commit that modified
250 # each line in the file. This can be very CPU-intensive.
252 # To enable system wide have in $GITWEB_CONFIG
253 # $feature{'blame'}{'default'} = [1];
254 # To have project specific config enable override in $GITWEB_CONFIG
255 # $feature{'blame'}{'override'} = 1;
256 # and in project config gitweb.blame = 0|1;
258 'sub' => sub { feature_bool('blame', @_) },
262 # Enable the 'snapshot' link, providing a compressed archive of any
263 # tree. This can potentially generate high traffic if you have large
266 # Value is a list of formats defined in %known_snapshot_formats that
268 # To disable system wide have in $GITWEB_CONFIG
269 # $feature{'snapshot'}{'default'} = [];
270 # To have project specific config enable override in $GITWEB_CONFIG
271 # $feature{'snapshot'}{'override'} = 1;
272 # and in project config, a comma-separated list of formats or "none"
273 # to disable. Example: gitweb.snapshot = tbz2,zip;
275 'sub' => \&feature_snapshot,
277 'default' => ['tgz']},
279 # Enable text search, which will list the commits which match author,
280 # committer or commit text to a given string. Enabled by default.
281 # Project specific override is not supported.
286 # Enable grep search, which will list the files in currently selected
287 # tree containing the given string. Enabled by default. This can be
288 # potentially CPU-intensive, of course.
290 # To enable system wide have in $GITWEB_CONFIG
291 # $feature{'grep'}{'default'} = [1];
292 # To have project specific config enable override in $GITWEB_CONFIG
293 # $feature{'grep'}{'override'} = 1;
294 # and in project config gitweb.grep = 0|1;
296 'sub' => sub { feature_bool('grep', @_) },
300 # Enable the pickaxe search, which will list the commits that modified
301 # a given string in a file. This can be practical and quite faster
302 # alternative to 'blame', but still potentially CPU-intensive.
304 # To enable system wide have in $GITWEB_CONFIG
305 # $feature{'pickaxe'}{'default'} = [1];
306 # To have project specific config enable override in $GITWEB_CONFIG
307 # $feature{'pickaxe'}{'override'} = 1;
308 # and in project config gitweb.pickaxe = 0|1;
310 'sub' => sub { feature_bool('pickaxe', @_) },
314 # Enable showing size of blobs in a 'tree' view, in a separate
315 # column, similar to what 'ls -l' does. This cost a bit of IO.
317 # To disable system wide have in $GITWEB_CONFIG
318 # $feature{'show-sizes'}{'default'} = [0];
319 # To have project specific config enable override in $GITWEB_CONFIG
320 # $feature{'show-sizes'}{'override'} = 1;
321 # and in project config gitweb.showsizes = 0|1;
323 'sub' => sub { feature_bool('showsizes', @_) },
327 # Make gitweb use an alternative format of the URLs which can be
328 # more readable and natural-looking: project name is embedded
329 # directly in the path and the query string contains other
330 # auxiliary information. All gitweb installations recognize
331 # URL in either format; this configures in which formats gitweb
334 # To enable system wide have in $GITWEB_CONFIG
335 # $feature{'pathinfo'}{'default'} = [1];
336 # Project specific override is not supported.
338 # Note that you will need to change the default location of CSS,
339 # favicon, logo and possibly other files to an absolute URL. Also,
340 # if gitweb.cgi serves as your indexfile, you will need to force
341 # $my_uri to contain the script name in your $GITWEB_CONFIG.
346 # Make gitweb consider projects in project root subdirectories
347 # to be forks of existing projects. Given project $projname.git,
348 # projects matching $projname/*.git will not be shown in the main
349 # projects list, instead a '+' mark will be added to $projname
350 # there and a 'forks' view will be enabled for the project, listing
351 # all the forks. If project list is taken from a file, forks have
352 # to be listed after the main project.
354 # To enable system wide have in $GITWEB_CONFIG
355 # $feature{'forks'}{'default'} = [1];
356 # Project specific override is not supported.
361 # Insert custom links to the action bar of all project pages.
362 # This enables you mainly to link to third-party scripts integrating
363 # into gitweb; e.g. git-browser for graphical history representation
364 # or custom web-based repository administration interface.
366 # The 'default' value consists of a list of triplets in the form
367 # (label, link, position) where position is the label after which
368 # to insert the link and link is a format string where %n expands
369 # to the project name, %f to the project path within the filesystem,
370 # %h to the current hash (h gitweb parameter) and %b to the current
371 # hash base (hb gitweb parameter); %% expands to %.
373 # To enable system wide have in $GITWEB_CONFIG e.g.
374 # $feature{'actions'}{'default'} = [('graphiclog',
375 # '/git-browser/by-commit.html?r=%n', 'summary')];
376 # Project specific override is not supported.
381 # Allow gitweb scan project content tags described in ctags/
382 # of project repository, and display the popular Web 2.0-ish
383 # "tag cloud" near the project list. Note that this is something
384 # COMPLETELY different from the normal Git tags.
386 # gitweb by itself can show existing tags, but it does not handle
387 # tagging itself; you need an external application for that.
388 # For an example script, check Girocco's cgi/tagproj.cgi.
389 # You may want to install the HTML::TagCloud Perl module to get
390 # a pretty tag cloud instead of just a list of tags.
392 # To enable system wide have in $GITWEB_CONFIG
393 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
394 # Project specific override is not supported.
399 # The maximum number of patches in a patchset generated in patch
400 # view. Set this to 0 or undef to disable patch view, or to a
401 # negative number to remove any limit.
403 # To disable system wide have in $GITWEB_CONFIG
404 # $feature{'patches'}{'default'} = [0];
405 # To have project specific config enable override in $GITWEB_CONFIG
406 # $feature{'patches'}{'override'} = 1;
407 # and in project config gitweb.patches = 0|n;
408 # where n is the maximum number of patches allowed in a patchset.
410 'sub' => \&feature_patches,
414 # Avatar support. When this feature is enabled, views such as
415 # shortlog or commit will display an avatar associated with
416 # the email of the committer(s) and/or author(s).
418 # Currently available providers are gravatar and picon.
419 # If an unknown provider is specified, the feature is disabled.
421 # Gravatar depends on Digest::MD5.
422 # Picon currently relies on the indiana.edu database.
424 # To enable system wide have in $GITWEB_CONFIG
425 # $feature{'avatar'}{'default'} = ['<provider>'];
426 # where <provider> is either gravatar or picon.
427 # To have project specific config enable override in $GITWEB_CONFIG
428 # $feature{'avatar'}{'override'} = 1;
429 # and in project config gitweb.avatar = <provider>;
431 'sub' => \&feature_avatar,
435 # Enable displaying how much time and how many git commands
436 # it took to generate and display page. Disabled by default.
437 # Project specific override is not supported.
442 # Enable turning some links into links to actions which require
443 # JavaScript to run (like 'blame_incremental'). Not enabled by
444 # default. Project specific override is currently not supported.
445 'javascript-actions' => {
450 sub gitweb_get_feature {
452 return unless exists $feature{$name};
453 my ($sub, $override, @defaults) = (
454 $feature{$name}{'sub'},
455 $feature{$name}{'override'},
456 @{$feature{$name}{'default'}});
457 if (!$override) { return @defaults; }
459 warn "feature $name is not overridable";
462 return $sub->(@defaults);
465 # A wrapper to check if a given feature is enabled.
466 # With this, you can say
468 # my $bool_feat = gitweb_check_feature('bool_feat');
469 # gitweb_check_feature('bool_feat') or somecode;
473 # my ($bool_feat) = gitweb_get_feature('bool_feat');
474 # (gitweb_get_feature('bool_feat'))[0] or somecode;
476 sub gitweb_check_feature {
477 return (gitweb_get_feature(@_))[0];
483 my ($val) = git_get_project_config($key, '--bool');
487 } elsif ($val eq 'true') {
489 } elsif ($val eq 'false') {
494 sub feature_snapshot {
497 my ($val) = git_get_project_config('snapshot');
500 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
506 sub feature_patches {
507 my @val = (git_get_project_config('patches', '--int'));
517 my @val = (git_get_project_config('avatar'));
519 return @val ? @val : @_;
522 # checking HEAD file with -e is fragile if the repository was
523 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
525 sub check_head_link {
527 my $headfile = "$dir/HEAD";
528 return ((-e $headfile) ||
529 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
532 sub check_export_ok {
534 return (check_head_link($dir) &&
535 (!$export_ok || -e "$dir/$export_ok") &&
536 (!$export_auth_hook || $export_auth_hook->($dir)));
539 # process alternate names for backward compatibility
540 # filter out unsupported (unknown) snapshot formats
541 sub filter_snapshot_fmts {
545 exists $known_snapshot_format_aliases{$_} ?
546 $known_snapshot_format_aliases{$_} : $_} @fmts;
548 exists $known_snapshot_formats{$_} &&
549 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
552 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
553 if (-e $GITWEB_CONFIG) {
556 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
557 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
560 # Get loadavg of system, to compare against $maxload.
561 # Currently it requires '/proc/loadavg' present to get loadavg;
562 # if it is not present it returns 0, which means no load checking.
564 if( -e '/proc/loadavg' ){
565 open my $fd, '<', '/proc/loadavg'
567 my @load = split(/\s+/, scalar <$fd>);
570 # The first three columns measure CPU and IO utilization of the last one,
571 # five, and 10 minute periods. The fourth column shows the number of
572 # currently running processes and the total number of processes in the m/n
573 # format. The last column displays the last process ID used.
574 return $load[0] || 0;
576 # additional checks for load average should go here for things that don't export
582 # version of the core git binary
583 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
584 $number_of_git_cmds++;
586 $projects_list ||= $projectroot;
588 if (defined $maxload && get_loadavg() > $maxload) {
589 die_error(503, "The load average on the server is too high");
592 # ======================================================================
593 # input validation and dispatch
595 # input parameters can be collected from a variety of sources (presently, CGI
596 # and PATH_INFO), so we define an %input_params hash that collects them all
597 # together during validation: this allows subsequent uses (e.g. href()) to be
598 # agnostic of the parameter origin
600 our %input_params = ();
602 # input parameters are stored with the long parameter name as key. This will
603 # also be used in the href subroutine to convert parameters to their CGI
604 # equivalent, and since the href() usage is the most frequent one, we store
605 # the name -> CGI key mapping here, instead of the reverse.
607 # XXX: Warning: If you touch this, check the search form for updating,
610 our @cgi_param_mapping = (
618 hash_parent_base => "hpb",
623 snapshot_format => "sf",
624 extra_options => "opt",
625 search_use_regexp => "sr",
626 # this must be last entry (for manipulation from JavaScript)
629 our %cgi_param_mapping = @cgi_param_mapping;
631 # we will also need to know the possible actions, for validation
633 "blame" => \&git_blame,
634 "blame_incremental" => \&git_blame_incremental,
635 "blame_data" => \&git_blame_data,
636 "blobdiff" => \&git_blobdiff,
637 "blobdiff_plain" => \&git_blobdiff_plain,
638 "blob" => \&git_blob,
639 "blob_plain" => \&git_blob_plain,
640 "commitdiff" => \&git_commitdiff,
641 "commitdiff_plain" => \&git_commitdiff_plain,
642 "commit" => \&git_commit,
643 "forks" => \&git_forks,
644 "heads" => \&git_heads,
645 "history" => \&git_history,
647 "patch" => \&git_patch,
648 "patches" => \&git_patches,
650 "atom" => \&git_atom,
651 "search" => \&git_search,
652 "search_help" => \&git_search_help,
653 "shortlog" => \&git_shortlog,
654 "summary" => \&git_summary,
656 "tags" => \&git_tags,
657 "tree" => \&git_tree,
658 "snapshot" => \&git_snapshot,
659 "object" => \&git_object,
660 # those below don't need $project
661 "opml" => \&git_opml,
662 "project_list" => \&git_project_list,
663 "project_index" => \&git_project_index,
666 # finally, we have the hash of allowed extra_options for the commands that
668 our %allowed_options = (
669 "--no-merges" => [ qw(rss atom log shortlog history) ],
672 # fill %input_params with the CGI parameters. All values except for 'opt'
673 # should be single values, but opt can be an array. We should probably
674 # build an array of parameters that can be multi-valued, but since for the time
675 # being it's only this one, we just single it out
676 while (my ($name, $symbol) = each %cgi_param_mapping) {
677 if ($symbol eq 'opt') {
678 $input_params{$name} = [ $cgi->param($symbol) ];
680 $input_params{$name} = $cgi->param($symbol);
684 # now read PATH_INFO and update the parameter list for missing parameters
685 sub evaluate_path_info {
686 return if defined $input_params{'project'};
687 return if !$path_info;
688 $path_info =~ s,^/+,,;
689 return if !$path_info;
691 # find which part of PATH_INFO is project
692 my $project = $path_info;
694 while ($project && !check_head_link("$projectroot/$project")) {
695 $project =~ s,/*[^/]*$,,;
697 return unless $project;
698 $input_params{'project'} = $project;
700 # do not change any parameters if an action is given using the query string
701 return if $input_params{'action'};
702 $path_info =~ s,^\Q$project\E/*,,;
704 # next, check if we have an action
705 my $action = $path_info;
707 if (exists $actions{$action}) {
708 $path_info =~ s,^$action/*,,;
709 $input_params{'action'} = $action;
712 # list of actions that want hash_base instead of hash, but can have no
713 # pathname (f) parameter
720 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
721 my ($parentrefname, $parentpathname, $refname, $pathname) =
722 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
724 # first, analyze the 'current' part
725 if (defined $pathname) {
726 # we got "branch:filename" or "branch:dir/"
727 # we could use git_get_type(branch:pathname), but:
728 # - it needs $git_dir
729 # - it does a git() call
730 # - the convention of terminating directories with a slash
731 # makes it superfluous
732 # - embedding the action in the PATH_INFO would make it even
734 $pathname =~ s,^/+,,;
735 if (!$pathname || substr($pathname, -1) eq "/") {
736 $input_params{'action'} ||= "tree";
739 # the default action depends on whether we had parent info
741 if ($parentrefname) {
742 $input_params{'action'} ||= "blobdiff_plain";
744 $input_params{'action'} ||= "blob_plain";
747 $input_params{'hash_base'} ||= $refname;
748 $input_params{'file_name'} ||= $pathname;
749 } elsif (defined $refname) {
750 # we got "branch". In this case we have to choose if we have to
751 # set hash or hash_base.
753 # Most of the actions without a pathname only want hash to be
754 # set, except for the ones specified in @wants_base that want
755 # hash_base instead. It should also be noted that hand-crafted
756 # links having 'history' as an action and no pathname or hash
757 # set will fail, but that happens regardless of PATH_INFO.
758 $input_params{'action'} ||= "shortlog";
759 if (grep { $_ eq $input_params{'action'} } @wants_base) {
760 $input_params{'hash_base'} ||= $refname;
762 $input_params{'hash'} ||= $refname;
766 # next, handle the 'parent' part, if present
767 if (defined $parentrefname) {
768 # a missing pathspec defaults to the 'current' filename, allowing e.g.
769 # someproject/blobdiff/oldrev..newrev:/filename
770 if ($parentpathname) {
771 $parentpathname =~ s,^/+,,;
772 $parentpathname =~ s,/$,,;
773 $input_params{'file_parent'} ||= $parentpathname;
775 $input_params{'file_parent'} ||= $input_params{'file_name'};
777 # we assume that hash_parent_base is wanted if a path was specified,
778 # or if the action wants hash_base instead of hash
779 if (defined $input_params{'file_parent'} ||
780 grep { $_ eq $input_params{'action'} } @wants_base) {
781 $input_params{'hash_parent_base'} ||= $parentrefname;
783 $input_params{'hash_parent'} ||= $parentrefname;
787 # for the snapshot action, we allow URLs in the form
788 # $project/snapshot/$hash.ext
789 # where .ext determines the snapshot and gets removed from the
790 # passed $refname to provide the $hash.
792 # To be able to tell that $refname includes the format extension, we
793 # require the following two conditions to be satisfied:
794 # - the hash input parameter MUST have been set from the $refname part
795 # of the URL (i.e. they must be equal)
796 # - the snapshot format MUST NOT have been defined already (e.g. from
798 # It's also useless to try any matching unless $refname has a dot,
799 # so we check for that too
800 if (defined $input_params{'action'} &&
801 $input_params{'action'} eq 'snapshot' &&
802 defined $refname && index($refname, '.') != -1 &&
803 $refname eq $input_params{'hash'} &&
804 !defined $input_params{'snapshot_format'}) {
805 # We loop over the known snapshot formats, checking for
806 # extensions. Allowed extensions are both the defined suffix
807 # (which includes the initial dot already) and the snapshot
808 # format key itself, with a prepended dot
809 while (my ($fmt, $opt) = each %known_snapshot_formats) {
811 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
815 # a valid suffix was found, so set the snapshot format
816 # and reset the hash parameter
817 $input_params{'snapshot_format'} = $fmt;
818 $input_params{'hash'} = $hash;
819 # we also set the format suffix to the one requested
820 # in the URL: this way a request for e.g. .tgz returns
821 # a .tgz instead of a .tar.gz
822 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
827 evaluate_path_info();
829 our $action = $input_params{'action'};
830 if (defined $action) {
831 if (!validate_action($action)) {
832 die_error(400, "Invalid action parameter");
836 # parameters which are pathnames
837 our $project = $input_params{'project'};
838 if (defined $project) {
839 if (!validate_project($project)) {
841 die_error(404, "No such project");
845 our $file_name = $input_params{'file_name'};
846 if (defined $file_name) {
847 if (!validate_pathname($file_name)) {
848 die_error(400, "Invalid file parameter");
852 our $file_parent = $input_params{'file_parent'};
853 if (defined $file_parent) {
854 if (!validate_pathname($file_parent)) {
855 die_error(400, "Invalid file parent parameter");
859 # parameters which are refnames
860 our $hash = $input_params{'hash'};
862 if (!validate_refname($hash)) {
863 die_error(400, "Invalid hash parameter");
867 our $hash_parent = $input_params{'hash_parent'};
868 if (defined $hash_parent) {
869 if (!validate_refname($hash_parent)) {
870 die_error(400, "Invalid hash parent parameter");
874 our $hash_base = $input_params{'hash_base'};
875 if (defined $hash_base) {
876 if (!validate_refname($hash_base)) {
877 die_error(400, "Invalid hash base parameter");
881 our @extra_options = @{$input_params{'extra_options'}};
882 # @extra_options is always defined, since it can only be (currently) set from
883 # CGI, and $cgi->param() returns the empty array in array context if the param
885 foreach my $opt (@extra_options) {
886 if (not exists $allowed_options{$opt}) {
887 die_error(400, "Invalid option parameter");
889 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
890 die_error(400, "Invalid option parameter for this action");
894 our $hash_parent_base = $input_params{'hash_parent_base'};
895 if (defined $hash_parent_base) {
896 if (!validate_refname($hash_parent_base)) {
897 die_error(400, "Invalid hash parent base parameter");
902 our $page = $input_params{'page'};
904 if ($page =~ m/[^0-9]/) {
905 die_error(400, "Invalid page parameter");
909 our $searchtype = $input_params{'searchtype'};
910 if (defined $searchtype) {
911 if ($searchtype =~ m/[^a-z]/) {
912 die_error(400, "Invalid searchtype parameter");
916 our $search_use_regexp = $input_params{'search_use_regexp'};
918 our $searchtext = $input_params{'searchtext'};
920 if (defined $searchtext) {
921 if (length($searchtext) < 2) {
922 die_error(403, "At least two characters are required for search parameter");
924 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
927 # path to the current git repository
929 $git_dir = "$projectroot/$project" if $project;
931 # list of supported snapshot formats
932 our @snapshot_fmts = gitweb_get_feature('snapshot');
933 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
935 # check that the avatar feature is set to a known provider name,
936 # and for each provider check if the dependencies are satisfied.
937 # if the provider name is invalid or the dependencies are not met,
938 # reset $git_avatar to the empty string.
939 our ($git_avatar) = gitweb_get_feature('avatar');
940 if ($git_avatar eq 'gravatar') {
941 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
942 } elsif ($git_avatar eq 'picon') {
949 if (!defined $action) {
951 $action = git_get_type($hash);
952 } elsif (defined $hash_base && defined $file_name) {
953 $action = git_get_type("$hash_base:$file_name");
954 } elsif (defined $project) {
957 $action = 'project_list';
960 if (!defined($actions{$action})) {
961 die_error(400, "Unknown action");
963 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
965 die_error(400, "Project needed");
967 $actions{$action}->();
970 ## ======================================================================
975 # default is to use -absolute url() i.e. $my_uri
976 my $href = $params{-full} ? $my_url : $my_uri;
978 $params{'project'} = $project unless exists $params{'project'};
980 if ($params{-replay}) {
981 while (my ($name, $symbol) = each %cgi_param_mapping) {
982 if (!exists $params{$name}) {
983 $params{$name} = $input_params{$name};
988 my $use_pathinfo = gitweb_check_feature('pathinfo');
989 if ($use_pathinfo and defined $params{'project'}) {
990 # try to put as many parameters as possible in PATH_INFO:
993 # - hash_parent or hash_parent_base:/file_parent
994 # - hash or hash_base:/filename
995 # - the snapshot_format as an appropriate suffix
997 # When the script is the root DirectoryIndex for the domain,
998 # $href here would be something like http://gitweb.example.com/
999 # Thus, we strip any trailing / from $href, to spare us double
1000 # slashes in the final URL
1003 # Then add the project name, if present
1004 $href .= "/".esc_url($params{'project'});
1005 delete $params{'project'};
1007 # since we destructively absorb parameters, we keep this
1008 # boolean that remembers if we're handling a snapshot
1009 my $is_snapshot = $params{'action'} eq 'snapshot';
1011 # Summary just uses the project path URL, any other action is
1013 if (defined $params{'action'}) {
1014 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
1015 delete $params{'action'};
1018 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1019 # stripping nonexistent or useless pieces
1020 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1021 || $params{'hash_parent'} || $params{'hash'});
1022 if (defined $params{'hash_base'}) {
1023 if (defined $params{'hash_parent_base'}) {
1024 $href .= esc_url($params{'hash_parent_base'});
1025 # skip the file_parent if it's the same as the file_name
1026 if (defined $params{'file_parent'}) {
1027 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1028 delete $params{'file_parent'};
1029 } elsif ($params{'file_parent'} !~ /\.\./) {
1030 $href .= ":/".esc_url($params{'file_parent'});
1031 delete $params{'file_parent'};
1035 delete $params{'hash_parent'};
1036 delete $params{'hash_parent_base'};
1037 } elsif (defined $params{'hash_parent'}) {
1038 $href .= esc_url($params{'hash_parent'}). "..";
1039 delete $params{'hash_parent'};
1042 $href .= esc_url($params{'hash_base'});
1043 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1044 $href .= ":/".esc_url($params{'file_name'});
1045 delete $params{'file_name'};
1047 delete $params{'hash'};
1048 delete $params{'hash_base'};
1049 } elsif (defined $params{'hash'}) {
1050 $href .= esc_url($params{'hash'});
1051 delete $params{'hash'};
1054 # If the action was a snapshot, we can absorb the
1055 # snapshot_format parameter too
1057 my $fmt = $params{'snapshot_format'};
1058 # snapshot_format should always be defined when href()
1059 # is called, but just in case some code forgets, we
1060 # fall back to the default
1061 $fmt ||= $snapshot_fmts[0];
1062 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1063 delete $params{'snapshot_format'};
1067 # now encode the parameters explicitly
1069 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1070 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1071 if (defined $params{$name}) {
1072 if (ref($params{$name}) eq "ARRAY") {
1073 foreach my $par (@{$params{$name}}) {
1074 push @result, $symbol . "=" . esc_param($par);
1077 push @result, $symbol . "=" . esc_param($params{$name});
1081 $href .= "?" . join(';', @result) if scalar @result;
1087 ## ======================================================================
1088 ## validation, quoting/unquoting and escaping
1090 sub validate_action {
1091 my $input = shift || return undef;
1092 return undef unless exists $actions{$input};
1096 sub validate_project {
1097 my $input = shift || return undef;
1098 if (!validate_pathname($input) ||
1099 !(-d "$projectroot/$input") ||
1100 !check_export_ok("$projectroot/$input") ||
1101 ($strict_export && !project_in_list($input))) {
1108 sub validate_pathname {
1109 my $input = shift || return undef;
1111 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1112 # at the beginning, at the end, and between slashes.
1113 # also this catches doubled slashes
1114 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1117 # no null characters
1118 if ($input =~ m!\0!) {
1124 sub validate_refname {
1125 my $input = shift || return undef;
1127 # textual hashes are O.K.
1128 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1131 # it must be correct pathname
1132 $input = validate_pathname($input)
1134 # restrictions on ref name according to git-check-ref-format
1135 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1141 # decode sequences of octets in utf8 into Perl's internal form,
1142 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1143 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1146 if (utf8::valid($str)) {
1150 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1154 # quote unsafe chars, but keep the slash, even when it's not
1155 # correct, but quoted slashes look too horrible in bookmarks
1158 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1163 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
1166 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1172 # replace invalid utf8 character with SUBSTITUTION sequence
1177 $str = to_utf8($str);
1178 $str = $cgi->escapeHTML($str);
1179 if ($opts{'-nbsp'}) {
1180 $str =~ s/ / /g;
1182 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1186 # quote control characters and escape filename to HTML
1191 $str = to_utf8($str);
1192 $str = $cgi->escapeHTML($str);
1193 if ($opts{'-nbsp'}) {
1194 $str =~ s/ / /g;
1196 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1200 # Make control characters "printable", using character escape codes (CEC)
1204 my %es = ( # character escape codes, aka escape sequences
1205 "\t" => '\t', # tab (HT)
1206 "\n" => '\n', # line feed (LF)
1207 "\r" => '\r', # carrige return (CR)
1208 "\f" => '\f', # form feed (FF)
1209 "\b" => '\b', # backspace (BS)
1210 "\a" => '\a', # alarm (bell) (BEL)
1211 "\e" => '\e', # escape (ESC)
1212 "\013" => '\v', # vertical tab (VT)
1213 "\000" => '\0', # nul character (NUL)
1215 my $chr = ( (exists $es{$cntrl})
1217 : sprintf('\%2x', ord($cntrl)) );
1218 if ($opts{-nohtml}) {
1221 return "<span class=\"cntrl\">$chr</span>";
1225 # Alternatively use unicode control pictures codepoints,
1226 # Unicode "printable representation" (PR)
1231 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1232 if ($opts{-nohtml}) {
1235 return "<span class=\"cntrl\">$chr</span>";
1239 # git may return quoted and escaped filenames
1245 my %es = ( # character escape codes, aka escape sequences
1246 't' => "\t", # tab (HT, TAB)
1247 'n' => "\n", # newline (NL)
1248 'r' => "\r", # return (CR)
1249 'f' => "\f", # form feed (FF)
1250 'b' => "\b", # backspace (BS)
1251 'a' => "\a", # alarm (bell) (BEL)
1252 'e' => "\e", # escape (ESC)
1253 'v' => "\013", # vertical tab (VT)
1256 if ($seq =~ m/^[0-7]{1,3}$/) {
1257 # octal char sequence
1258 return chr(oct($seq));
1259 } elsif (exists $es{$seq}) {
1260 # C escape sequence, aka character escape code
1263 # quoted ordinary character
1267 if ($str =~ m/^"(.*)"$/) {
1270 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1275 # escape tabs (convert tabs to spaces)
1279 while ((my $pos = index($line, "\t")) != -1) {
1280 if (my $count = (8 - ($pos % 8))) {
1281 my $spaces = ' ' x $count;
1282 $line =~ s/\t/$spaces/;
1289 sub project_in_list {
1290 my $project = shift;
1291 my @list = git_get_projects_list();
1292 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1295 ## ----------------------------------------------------------------------
1296 ## HTML aware string manipulation
1298 # Try to chop given string on a word boundary between position
1299 # $len and $len+$add_len. If there is no word boundary there,
1300 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1301 # (marking chopped part) would be longer than given string.
1305 my $add_len = shift || 10;
1306 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1308 # Make sure perl knows it is utf8 encoded so we don't
1309 # cut in the middle of a utf8 multibyte char.
1310 $str = to_utf8($str);
1312 # allow only $len chars, but don't cut a word if it would fit in $add_len
1313 # if it doesn't fit, cut it if it's still longer than the dots we would add
1314 # remove chopped character entities entirely
1316 # when chopping in the middle, distribute $len into left and right part
1317 # return early if chopping wouldn't make string shorter
1318 if ($where eq 'center') {
1319 return $str if ($len + 5 >= length($str)); # filler is length 5
1322 return $str if ($len + 4 >= length($str)); # filler is length 4
1325 # regexps: ending and beginning with word part up to $add_len
1326 my $endre = qr/.{$len}\w{0,$add_len}/;
1327 my $begre = qr/\w{0,$add_len}.{$len}/;
1329 if ($where eq 'left') {
1330 $str =~ m/^(.*?)($begre)$/;
1331 my ($lead, $body) = ($1, $2);
1332 if (length($lead) > 4) {
1333 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1336 return "$lead$body";
1338 } elsif ($where eq 'center') {
1339 $str =~ m/^($endre)(.*)$/;
1340 my ($left, $str) = ($1, $2);
1341 $str =~ m/^(.*?)($begre)$/;
1342 my ($mid, $right) = ($1, $2);
1343 if (length($mid) > 5) {
1344 $left =~ s/&[^;]*$//;
1345 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1348 return "$left$mid$right";
1351 $str =~ m/^($endre)(.*)$/;
1354 if (length($tail) > 4) {
1355 $body =~ s/&[^;]*$//;
1358 return "$body$tail";
1362 # takes the same arguments as chop_str, but also wraps a <span> around the
1363 # result with a title attribute if it does get chopped. Additionally, the
1364 # string is HTML-escaped.
1365 sub chop_and_escape_str {
1368 my $chopped = chop_str(@_);
1369 if ($chopped eq $str) {
1370 return esc_html($chopped);
1372 $str =~ s/[[:cntrl:]]/?/g;
1373 return $cgi->span({-title=>$str}, esc_html($chopped));
1377 ## ----------------------------------------------------------------------
1378 ## functions returning short strings
1380 # CSS class for given age value (in seconds)
1384 if (!defined $age) {
1386 } elsif ($age < 60*60*2) {
1388 } elsif ($age < 60*60*24*2) {
1395 # convert age in seconds to "nn units ago" string
1400 if ($age > 60*60*24*365*2) {
1401 $age_str = (int $age/60/60/24/365);
1402 $age_str .= " years ago";
1403 } elsif ($age > 60*60*24*(365/12)*2) {
1404 $age_str = int $age/60/60/24/(365/12);
1405 $age_str .= " months ago";
1406 } elsif ($age > 60*60*24*7*2) {
1407 $age_str = int $age/60/60/24/7;
1408 $age_str .= " weeks ago";
1409 } elsif ($age > 60*60*24*2) {
1410 $age_str = int $age/60/60/24;
1411 $age_str .= " days ago";
1412 } elsif ($age > 60*60*2) {
1413 $age_str = int $age/60/60;
1414 $age_str .= " hours ago";
1415 } elsif ($age > 60*2) {
1416 $age_str = int $age/60;
1417 $age_str .= " min ago";
1418 } elsif ($age > 2) {
1419 $age_str = int $age;
1420 $age_str .= " sec ago";
1422 $age_str .= " right now";
1428 S_IFINVALID => 0030000,
1429 S_IFGITLINK => 0160000,
1432 # submodule/subproject, a commit object reference
1436 return (($mode & S_IFMT) == S_IFGITLINK)
1439 # convert file mode in octal to symbolic file mode string
1441 my $mode = oct shift;
1443 if (S_ISGITLINK($mode)) {
1444 return 'm---------';
1445 } elsif (S_ISDIR($mode & S_IFMT)) {
1446 return 'drwxr-xr-x';
1447 } elsif (S_ISLNK($mode)) {
1448 return 'lrwxrwxrwx';
1449 } elsif (S_ISREG($mode)) {
1450 # git cares only about the executable bit
1451 if ($mode & S_IXUSR) {
1452 return '-rwxr-xr-x';
1454 return '-rw-r--r--';
1457 return '----------';
1461 # convert file mode in octal to file type string
1465 if ($mode !~ m/^[0-7]+$/) {
1471 if (S_ISGITLINK($mode)) {
1473 } elsif (S_ISDIR($mode & S_IFMT)) {
1475 } elsif (S_ISLNK($mode)) {
1477 } elsif (S_ISREG($mode)) {
1484 # convert file mode in octal to file type description string
1485 sub file_type_long {
1488 if ($mode !~ m/^[0-7]+$/) {
1494 if (S_ISGITLINK($mode)) {
1496 } elsif (S_ISDIR($mode & S_IFMT)) {
1498 } elsif (S_ISLNK($mode)) {
1500 } elsif (S_ISREG($mode)) {
1501 if ($mode & S_IXUSR) {
1502 return "executable";
1512 ## ----------------------------------------------------------------------
1513 ## functions returning short HTML fragments, or transforming HTML fragments
1514 ## which don't belong to other sections
1516 # format line of commit message.
1517 sub format_log_line_html {
1520 $line = esc_html($line, -nbsp=>1);
1521 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1522 $cgi->a({-href => href(action=>"object", hash=>$1),
1523 -class => "text"}, $1);
1529 # format marker of refs pointing to given object
1531 # the destination action is chosen based on object type and current context:
1532 # - for annotated tags, we choose the tag view unless it's the current view
1533 # already, in which case we go to shortlog view
1534 # - for other refs, we keep the current view if we're in history, shortlog or
1535 # log view, and select shortlog otherwise
1536 sub format_ref_marker {
1537 my ($refs, $id) = @_;
1540 if (defined $refs->{$id}) {
1541 foreach my $ref (@{$refs->{$id}}) {
1542 # this code exploits the fact that non-lightweight tags are the
1543 # only indirect objects, and that they are the only objects for which
1544 # we want to use tag instead of shortlog as action
1545 my ($type, $name) = qw();
1546 my $indirect = ($ref =~ s/\^\{\}$//);
1547 # e.g. tags/v2.6.11 or heads/next
1548 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1557 $class .= " indirect" if $indirect;
1559 my $dest_action = "shortlog";
1562 $dest_action = "tag" unless $action eq "tag";
1563 } elsif ($action =~ /^(history|(short)?log)$/) {
1564 $dest_action = $action;
1568 $dest .= "refs/" unless $ref =~ m!^refs/!;
1571 my $link = $cgi->a({
1573 action=>$dest_action,
1577 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1583 return ' <span class="refs">'. $markers . '</span>';
1589 # format, perhaps shortened and with markers, title line
1590 sub format_subject_html {
1591 my ($long, $short, $href, $extra) = @_;
1592 $extra = '' unless defined($extra);
1594 if (length($short) < length($long)) {
1595 $long =~ s/[[:cntrl:]]/?/g;
1596 return $cgi->a({-href => $href, -class => "list subject",
1597 -title => to_utf8($long)},
1598 esc_html($short)) . $extra;
1600 return $cgi->a({-href => $href, -class => "list subject"},
1601 esc_html($long)) . $extra;
1605 # Rather than recomputing the url for an email multiple times, we cache it
1606 # after the first hit. This gives a visible benefit in views where the avatar
1607 # for the same email is used repeatedly (e.g. shortlog).
1608 # The cache is shared by all avatar engines (currently gravatar only), which
1609 # are free to use it as preferred. Since only one avatar engine is used for any
1610 # given page, there's no risk for cache conflicts.
1611 our %avatar_cache = ();
1613 # Compute the picon url for a given email, by using the picon search service over at
1614 # http://www.cs.indiana.edu/picons/search.html
1616 my $email = lc shift;
1617 if (!$avatar_cache{$email}) {
1618 my ($user, $domain) = split('@', $email);
1619 $avatar_cache{$email} =
1620 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1622 "users+domains+unknown/up/single";
1624 return $avatar_cache{$email};
1627 # Compute the gravatar url for a given email, if it's not in the cache already.
1628 # Gravatar stores only the part of the URL before the size, since that's the
1629 # one computationally more expensive. This also allows reuse of the cache for
1630 # different sizes (for this particular engine).
1632 my $email = lc shift;
1634 $avatar_cache{$email} ||=
1635 "http://www.gravatar.com/avatar/" .
1636 Digest::MD5::md5_hex($email) . "?s=";
1637 return $avatar_cache{$email} . $size;
1640 # Insert an avatar for the given $email at the given $size if the feature
1642 sub git_get_avatar {
1643 my ($email, %opts) = @_;
1644 my $pre_white = ($opts{-pad_before} ? " " : "");
1645 my $post_white = ($opts{-pad_after} ? " " : "");
1646 $opts{-size} ||= 'default';
1647 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1649 if ($git_avatar eq 'gravatar') {
1650 $url = gravatar_url($email, $size);
1651 } elsif ($git_avatar eq 'picon') {
1652 $url = picon_url($email);
1654 # Other providers can be added by extending the if chain, defining $url
1655 # as needed. If no variant puts something in $url, we assume avatars
1656 # are completely disabled/unavailable.
1659 "<img width=\"$size\" " .
1660 "class=\"avatar\" " .
1669 sub format_search_author {
1670 my ($author, $searchtype, $displaytext) = @_;
1671 my $have_search = gitweb_check_feature('search');
1675 if ($searchtype eq 'author') {
1676 $performed = "authored";
1677 } elsif ($searchtype eq 'committer') {
1678 $performed = "committed";
1681 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1682 searchtext=>$author,
1683 searchtype=>$searchtype), class=>"list",
1684 title=>"Search for commits $performed by $author"},
1688 return $displaytext;
1692 # format the author name of the given commit with the given tag
1693 # the author name is chopped and escaped according to the other
1694 # optional parameters (see chop_str).
1695 sub format_author_html {
1698 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1699 return "<$tag class=\"author\">" .
1700 format_search_author($co->{'author_name'}, "author",
1701 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1706 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1707 sub format_git_diff_header_line {
1709 my $diffinfo = shift;
1710 my ($from, $to) = @_;
1712 if ($diffinfo->{'nparents'}) {
1714 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1715 if ($to->{'href'}) {
1716 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1717 esc_path($to->{'file'}));
1718 } else { # file was deleted (no href)
1719 $line .= esc_path($to->{'file'});
1723 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1724 if ($from->{'href'}) {
1725 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1726 'a/' . esc_path($from->{'file'}));
1727 } else { # file was added (no href)
1728 $line .= 'a/' . esc_path($from->{'file'});
1731 if ($to->{'href'}) {
1732 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1733 'b/' . esc_path($to->{'file'}));
1734 } else { # file was deleted
1735 $line .= 'b/' . esc_path($to->{'file'});
1739 return "<div class=\"diff header\">$line</div>\n";
1742 # format extended diff header line, before patch itself
1743 sub format_extended_diff_header_line {
1745 my $diffinfo = shift;
1746 my ($from, $to) = @_;
1749 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1750 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1751 esc_path($from->{'file'}));
1753 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1754 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1755 esc_path($to->{'file'}));
1757 # match single <mode>
1758 if ($line =~ m/\s(\d{6})$/) {
1759 $line .= '<span class="info"> (' .
1760 file_type_long($1) .
1764 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1765 # can match only for combined diff
1767 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1768 if ($from->{'href'}[$i]) {
1769 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1771 substr($diffinfo->{'from_id'}[$i],0,7));
1776 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1779 if ($to->{'href'}) {
1780 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1781 substr($diffinfo->{'to_id'},0,7));
1786 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1787 # can match only for ordinary diff
1788 my ($from_link, $to_link);
1789 if ($from->{'href'}) {
1790 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1791 substr($diffinfo->{'from_id'},0,7));
1793 $from_link = '0' x 7;
1795 if ($to->{'href'}) {
1796 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1797 substr($diffinfo->{'to_id'},0,7));
1801 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1802 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1805 return $line . "<br/>\n";
1808 # format from-file/to-file diff header
1809 sub format_diff_from_to_header {
1810 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1815 #assert($line =~ m/^---/) if DEBUG;
1816 # no extra formatting for "^--- /dev/null"
1817 if (! $diffinfo->{'nparents'}) {
1818 # ordinary (single parent) diff
1819 if ($line =~ m!^--- "?a/!) {
1820 if ($from->{'href'}) {
1822 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1823 esc_path($from->{'file'}));
1826 esc_path($from->{'file'});
1829 $result .= qq!<div class="diff from_file">$line</div>\n!;
1832 # combined diff (merge commit)
1833 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1834 if ($from->{'href'}[$i]) {
1836 $cgi->a({-href=>href(action=>"blobdiff",
1837 hash_parent=>$diffinfo->{'from_id'}[$i],
1838 hash_parent_base=>$parents[$i],
1839 file_parent=>$from->{'file'}[$i],
1840 hash=>$diffinfo->{'to_id'},
1842 file_name=>$to->{'file'}),
1844 -title=>"diff" . ($i+1)},
1847 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1848 esc_path($from->{'file'}[$i]));
1850 $line = '--- /dev/null';
1852 $result .= qq!<div class="diff from_file">$line</div>\n!;
1857 #assert($line =~ m/^\+\+\+/) if DEBUG;
1858 # no extra formatting for "^+++ /dev/null"
1859 if ($line =~ m!^\+\+\+ "?b/!) {
1860 if ($to->{'href'}) {
1862 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1863 esc_path($to->{'file'}));
1866 esc_path($to->{'file'});
1869 $result .= qq!<div class="diff to_file">$line</div>\n!;
1874 # create note for patch simplified by combined diff
1875 sub format_diff_cc_simplified {
1876 my ($diffinfo, @parents) = @_;
1879 $result .= "<div class=\"diff header\">" .
1881 if (!is_deleted($diffinfo)) {
1882 $result .= $cgi->a({-href => href(action=>"blob",
1884 hash=>$diffinfo->{'to_id'},
1885 file_name=>$diffinfo->{'to_file'}),
1887 esc_path($diffinfo->{'to_file'}));
1889 $result .= esc_path($diffinfo->{'to_file'});
1891 $result .= "</div>\n" . # class="diff header"
1892 "<div class=\"diff nodifferences\">" .
1894 "</div>\n"; # class="diff nodifferences"
1899 # format patch (diff) line (not to be used for diff headers)
1900 sub format_diff_line {
1902 my ($from, $to) = @_;
1903 my $diff_class = "";
1907 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1909 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1910 if ($line =~ m/^\@{3}/) {
1911 $diff_class = " chunk_header";
1912 } elsif ($line =~ m/^\\/) {
1913 $diff_class = " incomplete";
1914 } elsif ($prefix =~ tr/+/+/) {
1915 $diff_class = " add";
1916 } elsif ($prefix =~ tr/-/-/) {
1917 $diff_class = " rem";
1920 # assume ordinary diff
1921 my $char = substr($line, 0, 1);
1923 $diff_class = " add";
1924 } elsif ($char eq '-') {
1925 $diff_class = " rem";
1926 } elsif ($char eq '@') {
1927 $diff_class = " chunk_header";
1928 } elsif ($char eq "\\") {
1929 $diff_class = " incomplete";
1932 $line = untabify($line);
1933 if ($from && $to && $line =~ m/^\@{2} /) {
1934 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1935 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1937 $from_lines = 0 unless defined $from_lines;
1938 $to_lines = 0 unless defined $to_lines;
1940 if ($from->{'href'}) {
1941 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1942 -class=>"list"}, $from_text);
1944 if ($to->{'href'}) {
1945 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1946 -class=>"list"}, $to_text);
1948 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1949 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1950 return "<div class=\"diff$diff_class\">$line</div>\n";
1951 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1952 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1953 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1955 @from_text = split(' ', $ranges);
1956 for (my $i = 0; $i < @from_text; ++$i) {
1957 ($from_start[$i], $from_nlines[$i]) =
1958 (split(',', substr($from_text[$i], 1)), 0);
1961 $to_text = pop @from_text;
1962 $to_start = pop @from_start;
1963 $to_nlines = pop @from_nlines;
1965 $line = "<span class=\"chunk_info\">$prefix ";
1966 for (my $i = 0; $i < @from_text; ++$i) {
1967 if ($from->{'href'}[$i]) {
1968 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1969 -class=>"list"}, $from_text[$i]);
1971 $line .= $from_text[$i];
1975 if ($to->{'href'}) {
1976 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1977 -class=>"list"}, $to_text);
1981 $line .= " $prefix</span>" .
1982 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1983 return "<div class=\"diff$diff_class\">$line</div>\n";
1985 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1988 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1989 # linked. Pass the hash of the tree/commit to snapshot.
1990 sub format_snapshot_links {
1992 my $num_fmts = @snapshot_fmts;
1993 if ($num_fmts > 1) {
1994 # A parenthesized list of links bearing format names.
1995 # e.g. "snapshot (_tar.gz_ _zip_)"
1996 return "snapshot (" . join(' ', map
2003 }, $known_snapshot_formats{$_}{'display'})
2004 , @snapshot_fmts) . ")";
2005 } elsif ($num_fmts == 1) {
2006 # A single "snapshot" link whose tooltip bears the format name.
2008 my ($fmt) = @snapshot_fmts;
2014 snapshot_format=>$fmt
2016 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2018 } else { # $num_fmts == 0
2023 ## ......................................................................
2024 ## functions returning values to be passed, perhaps after some
2025 ## transformation, to other functions; e.g. returning arguments to href()
2027 # returns hash to be passed to href to generate gitweb URL
2028 # in -title key it returns description of link
2030 my $format = shift || 'Atom';
2031 my %res = (action => lc($format));
2033 # feed links are possible only for project views
2034 return unless (defined $project);
2035 # some views should link to OPML, or to generic project feed,
2036 # or don't have specific feed yet (so they should use generic)
2037 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2040 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2041 # from tag links; this also makes possible to detect branch links
2042 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2043 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2046 # find log type for feed description (title)
2048 if (defined $file_name) {
2049 $type = "history of $file_name";
2050 $type .= "/" if ($action eq 'tree');
2051 $type .= " on '$branch'" if (defined $branch);
2053 $type = "log of $branch" if (defined $branch);
2056 $res{-title} = $type;
2057 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2058 $res{'file_name'} = $file_name;
2063 ## ----------------------------------------------------------------------
2064 ## git utility subroutines, invoking git commands
2066 # returns path to the core git executable and the --git-dir parameter as list
2068 $number_of_git_cmds++;
2069 return $GIT, '--git-dir='.$git_dir;
2072 # quote the given arguments for passing them to the shell
2073 # quote_command("command", "arg 1", "arg with ' and ! characters")
2074 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2075 # Try to avoid using this function wherever possible.
2078 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2081 # get HEAD ref of given project as hash
2082 sub git_get_head_hash {
2083 return git_get_full_hash(shift, 'HEAD');
2086 sub git_get_full_hash {
2087 return git_get_hash(@_);
2090 sub git_get_short_hash {
2091 return git_get_hash(@_, '--short=7');
2095 my ($project, $hash, @options) = @_;
2096 my $o_git_dir = $git_dir;
2098 $git_dir = "$projectroot/$project";
2099 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2100 '--verify', '-q', @options, $hash) {
2102 chomp $retval if defined $retval;
2105 if (defined $o_git_dir) {
2106 $git_dir = $o_git_dir;
2111 # get type of given object
2115 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2117 close $fd or return;
2122 # repository configuration
2123 our $config_file = '';
2126 # store multiple values for single key as anonymous array reference
2127 # single values stored directly in the hash, not as [ <value> ]
2128 sub hash_set_multi {
2129 my ($hash, $key, $value) = @_;
2131 if (!exists $hash->{$key}) {
2132 $hash->{$key} = $value;
2133 } elsif (!ref $hash->{$key}) {
2134 $hash->{$key} = [ $hash->{$key}, $value ];
2136 push @{$hash->{$key}}, $value;
2140 # return hash of git project configuration
2141 # optionally limited to some section, e.g. 'gitweb'
2142 sub git_parse_project_config {
2143 my $section_regexp = shift;
2148 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2151 while (my $keyval = <$fh>) {
2153 my ($key, $value) = split(/\n/, $keyval, 2);
2155 hash_set_multi(\%config, $key, $value)
2156 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2163 # convert config value to boolean: 'true' or 'false'
2164 # no value, number > 0, 'true' and 'yes' values are true
2165 # rest of values are treated as false (never as error)
2166 sub config_to_bool {
2169 return 1 if !defined $val; # section.key
2171 # strip leading and trailing whitespace
2175 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2176 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2179 # convert config value to simple decimal number
2180 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2181 # to be multiplied by 1024, 1048576, or 1073741824
2185 # strip leading and trailing whitespace
2189 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2191 # unknown unit is treated as 1
2192 return $num * ($unit eq 'g' ? 1073741824 :
2193 $unit eq 'm' ? 1048576 :
2194 $unit eq 'k' ? 1024 : 1);
2199 # convert config value to array reference, if needed
2200 sub config_to_multi {
2203 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2206 sub git_get_project_config {
2207 my ($key, $type) = @_;
2210 return unless ($key);
2211 $key =~ s/^gitweb\.//;
2212 return if ($key =~ m/\W/);
2215 if (defined $type) {
2218 unless ($type eq 'bool' || $type eq 'int');
2222 if (!defined $config_file ||
2223 $config_file ne "$git_dir/config") {
2224 %config = git_parse_project_config('gitweb');
2225 $config_file = "$git_dir/config";
2228 # check if config variable (key) exists
2229 return unless exists $config{"gitweb.$key"};
2232 if (!defined $type) {
2233 return $config{"gitweb.$key"};
2234 } elsif ($type eq 'bool') {
2235 # backward compatibility: 'git config --bool' returns true/false
2236 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2237 } elsif ($type eq 'int') {
2238 return config_to_int($config{"gitweb.$key"});
2240 return $config{"gitweb.$key"};
2243 # get hash of given path at given ref
2244 sub git_get_hash_by_path {
2246 my $path = shift || return undef;
2251 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2252 or die_error(500, "Open git-ls-tree failed");
2254 close $fd or return undef;
2256 if (!defined $line) {
2257 # there is no tree or hash given by $path at $base
2261 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2262 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2263 if (defined $type && $type ne $2) {
2264 # type doesn't match
2270 # get path of entry with given hash at given tree-ish (ref)
2271 # used to get 'from' filename for combined diff (merge commit) for renames
2272 sub git_get_path_by_hash {
2273 my $base = shift || return;
2274 my $hash = shift || return;
2278 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2280 while (my $line = <$fd>) {
2283 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2284 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2285 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2294 ## ......................................................................
2295 ## git utility functions, directly accessing git repository
2297 sub git_get_project_description {
2300 $git_dir = "$projectroot/$path";
2301 open my $fd, '<', "$git_dir/description"
2302 or return git_get_project_config('description');
2305 if (defined $descr) {
2311 sub git_get_project_ctags {
2315 $git_dir = "$projectroot/$path";
2316 opendir my $dh, "$git_dir/ctags"
2318 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2319 open my $ct, '<', $_ or next;
2323 my $ctag = $_; $ctag =~ s#.*/##;
2324 $ctags->{$ctag} = $val;
2330 sub git_populate_project_tagcloud {
2333 # First, merge different-cased tags; tags vote on casing
2335 foreach (keys %$ctags) {
2336 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2337 if (not $ctags_lc{lc $_}->{topcount}
2338 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2339 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2340 $ctags_lc{lc $_}->{topname} = $_;
2345 if (eval { require HTML::TagCloud; 1; }) {
2346 $cloud = HTML::TagCloud->new;
2347 foreach (sort keys %ctags_lc) {
2348 # Pad the title with spaces so that the cloud looks
2350 my $title = $ctags_lc{$_}->{topname};
2351 $title =~ s/ / /g;
2352 $title =~ s/^/ /g;
2353 $title =~ s/$/ /g;
2354 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2357 $cloud = \%ctags_lc;
2362 sub git_show_project_tagcloud {
2363 my ($cloud, $count) = @_;
2364 print STDERR ref($cloud)."..\n";
2365 if (ref $cloud eq 'HTML::TagCloud') {
2366 return $cloud->html_and_css($count);
2368 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2369 return '<p align="center">' . join (', ', map {
2370 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2371 } splice(@tags, 0, $count)) . '</p>';
2375 sub git_get_project_url_list {
2378 $git_dir = "$projectroot/$path";
2379 open my $fd, '<', "$git_dir/cloneurl"
2380 or return wantarray ?
2381 @{ config_to_multi(git_get_project_config('url')) } :
2382 config_to_multi(git_get_project_config('url'));
2383 my @git_project_url_list = map { chomp; $_ } <$fd>;
2386 return wantarray ? @git_project_url_list : \@git_project_url_list;
2389 sub git_get_projects_list {
2394 $filter =~ s/\.git$//;
2396 my $check_forks = gitweb_check_feature('forks');
2398 if (-d $projects_list) {
2399 # search in directory
2400 my $dir = $projects_list . ($filter ? "/$filter" : '');
2401 # remove the trailing "/"
2403 my $pfxlen = length("$dir");
2404 my $pfxdepth = ($dir =~ tr!/!!);
2407 follow_fast => 1, # follow symbolic links
2408 follow_skip => 2, # ignore duplicates
2409 dangling_symlinks => 0, # ignore dangling symlinks, silently
2411 # skip project-list toplevel, if we get it.
2412 return if (m!^[/.]$!);
2413 # only directories can be git repositories
2414 return unless (-d $_);
2415 # don't traverse too deep (Find is super slow on os x)
2416 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2417 $File::Find::prune = 1;
2421 my $subdir = substr($File::Find::name, $pfxlen + 1);
2422 # we check related file in $projectroot
2423 my $path = ($filter ? "$filter/" : '') . $subdir;
2424 if (check_export_ok("$projectroot/$path")) {
2425 push @list, { path => $path };
2426 $File::Find::prune = 1;
2431 } elsif (-f $projects_list) {
2432 # read from file(url-encoded):
2433 # 'git%2Fgit.git Linus+Torvalds'
2434 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2435 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2437 open my $fd, '<', $projects_list or return;
2439 while (my $line = <$fd>) {
2441 my ($path, $owner) = split ' ', $line;
2442 $path = unescape($path);
2443 $owner = unescape($owner);
2444 if (!defined $path) {
2447 if ($filter ne '') {
2448 # looking for forks;
2449 my $pfx = substr($path, 0, length($filter));
2450 if ($pfx ne $filter) {
2453 my $sfx = substr($path, length($filter));
2454 if ($sfx !~ /^\/.*\.git$/) {
2457 } elsif ($check_forks) {
2459 foreach my $filter (keys %paths) {
2460 # looking for forks;
2461 my $pfx = substr($path, 0, length($filter));
2462 if ($pfx ne $filter) {
2465 my $sfx = substr($path, length($filter));
2466 if ($sfx !~ /^\/.*\.git$/) {
2469 # is a fork, don't include it in
2474 if (check_export_ok("$projectroot/$path")) {
2477 owner => to_utf8($owner),
2480 (my $forks_path = $path) =~ s/\.git$//;
2481 $paths{$forks_path}++;
2489 our $gitweb_project_owner = undef;
2490 sub git_get_project_list_from_file {
2492 return if (defined $gitweb_project_owner);
2494 $gitweb_project_owner = {};
2495 # read from file (url-encoded):
2496 # 'git%2Fgit.git Linus+Torvalds'
2497 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2498 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2499 if (-f $projects_list) {
2500 open(my $fd, '<', $projects_list);
2501 while (my $line = <$fd>) {
2503 my ($pr, $ow) = split ' ', $line;
2504 $pr = unescape($pr);
2505 $ow = unescape($ow);
2506 $gitweb_project_owner->{$pr} = to_utf8($ow);
2512 sub git_get_project_owner {
2513 my $project = shift;
2516 return undef unless $project;
2517 $git_dir = "$projectroot/$project";
2519 if (!defined $gitweb_project_owner) {
2520 git_get_project_list_from_file();
2523 if (exists $gitweb_project_owner->{$project}) {
2524 $owner = $gitweb_project_owner->{$project};
2526 if (!defined $owner){
2527 $owner = git_get_project_config('owner');
2529 if (!defined $owner) {
2530 $owner = get_file_owner("$git_dir");
2536 sub git_get_last_activity {
2540 $git_dir = "$projectroot/$path";
2541 open($fd, "-|", git_cmd(), 'for-each-ref',
2542 '--format=%(committer)',
2543 '--sort=-committerdate',
2545 'refs/heads') or return;
2546 my $most_recent = <$fd>;
2547 close $fd or return;
2548 if (defined $most_recent &&
2549 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2551 my $age = time - $timestamp;
2552 return ($age, age_string($age));
2554 return (undef, undef);
2557 sub git_get_references {
2558 my $type = shift || "";
2560 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2561 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2562 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2563 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2566 while (my $line = <$fd>) {
2568 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2569 if (defined $refs{$1}) {
2570 push @{$refs{$1}}, $2;
2576 close $fd or return;
2580 sub git_get_rev_name_tags {
2581 my $hash = shift || return undef;
2583 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2585 my $name_rev = <$fd>;
2588 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2591 # catches also '$hash undefined' output
2596 ## ----------------------------------------------------------------------
2597 ## parse to hash functions
2601 my $tz = shift || "-0000";
2604 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2605 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2606 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2607 $date{'hour'} = $hour;
2608 $date{'minute'} = $min;
2609 $date{'mday'} = $mday;
2610 $date{'day'} = $days[$wday];
2611 $date{'month'} = $months[$mon];
2612 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2613 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2614 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2615 $mday, $months[$mon], $hour ,$min;
2616 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2617 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2619 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2620 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2621 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2622 $date{'hour_local'} = $hour;
2623 $date{'minute_local'} = $min;
2624 $date{'tz_local'} = $tz;
2625 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2626 1900+$year, $mon+1, $mday,
2627 $hour, $min, $sec, $tz);
2636 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2637 $tag{'id'} = $tag_id;
2638 while (my $line = <$fd>) {
2640 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2641 $tag{'object'} = $1;
2642 } elsif ($line =~ m/^type (.+)$/) {
2644 } elsif ($line =~ m/^tag (.+)$/) {
2646 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2647 $tag{'author'} = $1;
2648 $tag{'author_epoch'} = $2;
2649 $tag{'author_tz'} = $3;
2650 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2651 $tag{'author_name'} = $1;
2652 $tag{'author_email'} = $2;
2654 $tag{'author_name'} = $tag{'author'};
2656 } elsif ($line =~ m/--BEGIN/) {
2657 push @comment, $line;
2659 } elsif ($line eq "") {
2663 push @comment, <$fd>;
2664 $tag{'comment'} = \@comment;
2665 close $fd or return;
2666 if (!defined $tag{'name'}) {
2672 sub parse_commit_text {
2673 my ($commit_text, $withparents) = @_;
2674 my @commit_lines = split '\n', $commit_text;
2677 pop @commit_lines; # Remove '\0'
2679 if (! @commit_lines) {
2683 my $header = shift @commit_lines;
2684 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2687 ($co{'id'}, my @parents) = split ' ', $header;
2688 while (my $line = shift @commit_lines) {
2689 last if $line eq "\n";
2690 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2692 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2694 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2695 $co{'author'} = to_utf8($1);
2696 $co{'author_epoch'} = $2;
2697 $co{'author_tz'} = $3;
2698 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2699 $co{'author_name'} = $1;
2700 $co{'author_email'} = $2;
2702 $co{'author_name'} = $co{'author'};
2704 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2705 $co{'committer'} = to_utf8($1);
2706 $co{'committer_epoch'} = $2;
2707 $co{'committer_tz'} = $3;
2708 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2709 $co{'committer_name'} = $1;
2710 $co{'committer_email'} = $2;
2712 $co{'committer_name'} = $co{'committer'};
2716 if (!defined $co{'tree'}) {
2719 $co{'parents'} = \@parents;
2720 $co{'parent'} = $parents[0];
2722 foreach my $title (@commit_lines) {
2725 $co{'title'} = chop_str($title, 80, 5);
2726 # remove leading stuff of merges to make the interesting part visible
2727 if (length($title) > 50) {
2728 $title =~ s/^Automatic //;
2729 $title =~ s/^merge (of|with) /Merge ... /i;
2730 if (length($title) > 50) {
2731 $title =~ s/(http|rsync):\/\///;
2733 if (length($title) > 50) {
2734 $title =~ s/(master|www|rsync)\.//;
2736 if (length($title) > 50) {
2737 $title =~ s/kernel.org:?//;
2739 if (length($title) > 50) {
2740 $title =~ s/\/pub\/scm//;
2743 $co{'title_short'} = chop_str($title, 50, 5);
2747 if (! defined $co{'title'} || $co{'title'} eq "") {
2748 $co{'title'} = $co{'title_short'} = '(no commit message)';
2750 # remove added spaces
2751 foreach my $line (@commit_lines) {
2754 $co{'comment'} = \@commit_lines;
2756 my $age = time - $co{'committer_epoch'};
2758 $co{'age_string'} = age_string($age);
2759 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2760 if ($age > 60*60*24*7*2) {
2761 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2762 $co{'age_string_age'} = $co{'age_string'};
2764 $co{'age_string_date'} = $co{'age_string'};
2765 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2771 my ($commit_id) = @_;
2776 open my $fd, "-|", git_cmd(), "rev-list",
2782 or die_error(500, "Open git-rev-list failed");
2783 %co = parse_commit_text(<$fd>, 1);
2790 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2798 open my $fd, "-|", git_cmd(), "rev-list",
2801 ("--max-count=" . $maxcount),
2802 ("--skip=" . $skip),
2806 ($filename ? ($filename) : ())
2807 or die_error(500, "Open git-rev-list failed");
2808 while (my $line = <$fd>) {
2809 my %co = parse_commit_text($line);
2814 return wantarray ? @cos : \@cos;
2817 # parse line of git-diff-tree "raw" output
2818 sub parse_difftree_raw_line {
2822 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2823 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2824 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2825 $res{'from_mode'} = $1;
2826 $res{'to_mode'} = $2;
2827 $res{'from_id'} = $3;
2829 $res{'status'} = $5;
2830 $res{'similarity'} = $6;
2831 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2832 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2834 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2837 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2838 # combined diff (for merge commit)
2839 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2840 $res{'nparents'} = length($1);
2841 $res{'from_mode'} = [ split(' ', $2) ];
2842 $res{'to_mode'} = pop @{$res{'from_mode'}};
2843 $res{'from_id'} = [ split(' ', $3) ];
2844 $res{'to_id'} = pop @{$res{'from_id'}};
2845 $res{'status'} = [ split('', $4) ];
2846 $res{'to_file'} = unquote($5);
2848 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2849 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2850 $res{'commit'} = $1;
2853 return wantarray ? %res : \%res;
2856 # wrapper: return parsed line of git-diff-tree "raw" output
2857 # (the argument might be raw line, or parsed info)
2858 sub parsed_difftree_line {
2859 my $line_or_ref = shift;
2861 if (ref($line_or_ref) eq "HASH") {
2862 # pre-parsed (or generated by hand)
2863 return $line_or_ref;
2865 return parse_difftree_raw_line($line_or_ref);
2869 # parse line of git-ls-tree output
2870 sub parse_ls_tree_line {
2876 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
2877 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
2886 $res{'name'} = unquote($5);
2889 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2890 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2898 $res{'name'} = unquote($4);
2902 return wantarray ? %res : \%res;
2905 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2906 sub parse_from_to_diffinfo {
2907 my ($diffinfo, $from, $to, @parents) = @_;
2909 if ($diffinfo->{'nparents'}) {
2911 $from->{'file'} = [];
2912 $from->{'href'} = [];
2913 fill_from_file_info($diffinfo, @parents)
2914 unless exists $diffinfo->{'from_file'};
2915 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2916 $from->{'file'}[$i] =
2917 defined $diffinfo->{'from_file'}[$i] ?
2918 $diffinfo->{'from_file'}[$i] :
2919 $diffinfo->{'to_file'};
2920 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2921 $from->{'href'}[$i] = href(action=>"blob",
2922 hash_base=>$parents[$i],
2923 hash=>$diffinfo->{'from_id'}[$i],
2924 file_name=>$from->{'file'}[$i]);
2926 $from->{'href'}[$i] = undef;
2930 # ordinary (not combined) diff
2931 $from->{'file'} = $diffinfo->{'from_file'};
2932 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2933 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2934 hash=>$diffinfo->{'from_id'},
2935 file_name=>$from->{'file'});
2937 delete $from->{'href'};
2941 $to->{'file'} = $diffinfo->{'to_file'};
2942 if (!is_deleted($diffinfo)) { # file exists in result
2943 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2944 hash=>$diffinfo->{'to_id'},
2945 file_name=>$to->{'file'});
2947 delete $to->{'href'};
2951 ## ......................................................................
2952 ## parse to array of hashes functions
2954 sub git_get_heads_list {
2958 open my $fd, '-|', git_cmd(), 'for-each-ref',
2959 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2960 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2963 while (my $line = <$fd>) {
2967 my ($refinfo, $committerinfo) = split(/\0/, $line);
2968 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2969 my ($committer, $epoch, $tz) =
2970 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2971 $ref_item{'fullname'} = $name;
2972 $name =~ s!^refs/heads/!!;
2974 $ref_item{'name'} = $name;
2975 $ref_item{'id'} = $hash;
2976 $ref_item{'title'} = $title || '(no commit message)';
2977 $ref_item{'epoch'} = $epoch;
2979 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2981 $ref_item{'age'} = "unknown";
2984 push @headslist, \%ref_item;
2988 return wantarray ? @headslist : \@headslist;
2991 sub git_get_tags_list {
2995 open my $fd, '-|', git_cmd(), 'for-each-ref',
2996 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2997 '--format=%(objectname) %(objecttype) %(refname) '.
2998 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3001 while (my $line = <$fd>) {
3005 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3006 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3007 my ($creator, $epoch, $tz) =
3008 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3009 $ref_item{'fullname'} = $name;
3010 $name =~ s!^refs/tags/!!;
3012 $ref_item{'type'} = $type;
3013 $ref_item{'id'} = $id;
3014 $ref_item{'name'} = $name;
3015 if ($type eq "tag") {
3016 $ref_item{'subject'} = $title;
3017 $ref_item{'reftype'} = $reftype;
3018 $ref_item{'refid'} = $refid;
3020 $ref_item{'reftype'} = $type;
3021 $ref_item{'refid'} = $id;
3024 if ($type eq "tag" || $type eq "commit") {
3025 $ref_item{'epoch'} = $epoch;
3027 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3029 $ref_item{'age'} = "unknown";
3033 push @tagslist, \%ref_item;
3037 return wantarray ? @tagslist : \@tagslist;
3040 ## ----------------------------------------------------------------------
3041 ## filesystem-related functions
3043 sub get_file_owner {
3046 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3047 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3048 if (!defined $gcos) {
3052 $owner =~ s/[,;].*$//;
3053 return to_utf8($owner);
3056 # assume that file exists
3058 my $filename = shift;
3060 open my $fd, '<', $filename;
3061 print map { to_utf8($_) } <$fd>;
3065 ## ......................................................................
3066 ## mimetype related functions
3068 sub mimetype_guess_file {
3069 my $filename = shift;
3070 my $mimemap = shift;
3071 -r $mimemap or return undef;
3074 open(my $mh, '<', $mimemap) or return undef;
3076 next if m/^#/; # skip comments
3077 my ($mimetype, $exts) = split(/\t+/);
3078 if (defined $exts) {
3079 my @exts = split(/\s+/, $exts);
3080 foreach my $ext (@exts) {
3081 $mimemap{$ext} = $mimetype;
3087 $filename =~ /\.([^.]*)$/;
3088 return $mimemap{$1};
3091 sub mimetype_guess {
3092 my $filename = shift;
3094 $filename =~ /\./ or return undef;
3096 if ($mimetypes_file) {
3097 my $file = $mimetypes_file;
3098 if ($file !~ m!^/!) { # if it is relative path
3099 # it is relative to project
3100 $file = "$projectroot/$project/$file";
3102 $mime = mimetype_guess_file($filename, $file);
3104 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3110 my $filename = shift;
3113 my $mime = mimetype_guess($filename);
3114 $mime and return $mime;
3118 return $default_blob_plain_mimetype unless $fd;
3121 return 'text/plain';
3122 } elsif (! $filename) {
3123 return 'application/octet-stream';
3124 } elsif ($filename =~ m/\.png$/i) {
3126 } elsif ($filename =~ m/\.gif$/i) {
3128 } elsif ($filename =~ m/\.jpe?g$/i) {
3129 return 'image/jpeg';
3131 return 'application/octet-stream';
3135 sub blob_contenttype {
3136 my ($fd, $file_name, $type) = @_;
3138 $type ||= blob_mimetype($fd, $file_name);
3139 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3140 $type .= "; charset=$default_text_plain_charset";
3146 ## ======================================================================
3147 ## functions printing HTML: header, footer, error page
3149 sub git_header_html {
3150 my $status = shift || "200 OK";
3151 my $expires = shift;
3153 my $title = "$site_name";
3154 if (defined $project) {
3155 $title .= " - " . to_utf8($project);
3156 if (defined $action) {
3157 $title .= "/$action";
3158 if (defined $file_name) {
3159 $title .= " - " . esc_path($file_name);
3160 if ($action eq "tree" && $file_name !~ m|/$|) {
3167 # require explicit support from the UA if we are to send the page as
3168 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3169 # we have to do this because MSIE sometimes globs '*/*', pretending to
3170 # support xhtml+xml but choking when it gets what it asked for.
3171 if (defined $cgi->http('HTTP_ACCEPT') &&
3172 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3173 $cgi->Accept('application/xhtml+xml') != 0) {
3174 $content_type = 'application/xhtml+xml';
3176 $content_type = 'text/html';
3178 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3179 -status=> $status, -expires => $expires);
3180 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3182 <?xml version="1.0" encoding="utf-8"?>
3183 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3184 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3185 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3186 <!-- git core binaries version $git_version -->
3188 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3189 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3190 <meta name="robots" content="index, nofollow"/>
3191 <title>$title</title>
3193 # the stylesheet, favicon etc urls won't work correctly with path_info
3194 # unless we set the appropriate base URL
3195 if ($ENV{'PATH_INFO'}) {
3196 print "<base href=\"".esc_url($base_url)."\" />\n";
3198 # print out each stylesheet that exist, providing backwards capability
3199 # for those people who defined $stylesheet in a config file
3200 if (defined $stylesheet) {
3201 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3203 foreach my $stylesheet (@stylesheets) {
3204 next unless $stylesheet;
3205 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3208 if (defined $project) {
3209 my %href_params = get_feed_info();
3210 if (!exists $href_params{'-title'}) {
3211 $href_params{'-title'} = 'log';
3214 foreach my $format qw(RSS Atom) {
3215 my $type = lc($format);
3217 '-rel' => 'alternate',
3218 '-title' => "$project - $href_params{'-title'} - $format feed",
3219 '-type' => "application/$type+xml"
3222 $href_params{'action'} = $type;
3223 $link_attr{'-href'} = href(%href_params);
3225 "rel=\"$link_attr{'-rel'}\" ".
3226 "title=\"$link_attr{'-title'}\" ".
3227 "href=\"$link_attr{'-href'}\" ".
3228 "type=\"$link_attr{'-type'}\" ".
3231 $href_params{'extra_options'} = '--no-merges';
3232 $link_attr{'-href'} = href(%href_params);
3233 $link_attr{'-title'} .= ' (no merges)';
3235 "rel=\"$link_attr{'-rel'}\" ".
3236 "title=\"$link_attr{'-title'}\" ".
3237 "href=\"$link_attr{'-href'}\" ".
3238 "type=\"$link_attr{'-type'}\" ".
3243 printf('<link rel="alternate" title="%s projects list" '.
3244 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3245 $site_name, href(project=>undef, action=>"project_index"));
3246 printf('<link rel="alternate" title="%s projects feeds" '.
3247 'href="%s" type="text/x-opml" />'."\n",
3248 $site_name, href(project=>undef, action=>"opml"));
3250 if (defined $favicon) {
3251 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
3257 if (defined $site_header && -f $site_header) {
3258 insert_file($site_header);
3261 print "<div class=\"page_header\">\n" .
3262 $cgi->a({-href => esc_url($logo_url),
3263 -title => $logo_label},
3264 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
3265 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3266 if (defined $project) {
3267 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3268 if (defined $action) {
3275 my $have_search = gitweb_check_feature('search');
3276 if (defined $project && $have_search) {
3277 if (!defined $searchtext) {
3281 if (defined $hash_base) {
3282 $search_hash = $hash_base;
3283 } elsif (defined $hash) {
3284 $search_hash = $hash;
3286 $search_hash = "HEAD";
3288 my $action = $my_uri;
3289 my $use_pathinfo = gitweb_check_feature('pathinfo');
3290 if ($use_pathinfo) {
3291 $action .= "/".esc_url($project);
3293 print $cgi->startform(-method => "get", -action => $action) .
3294 "<div class=\"search\">\n" .
3296 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3297 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3298 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3299 $cgi->popup_menu(-name => 'st', -default => 'commit',
3300 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3301 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3303 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3304 "<span title=\"Extended regular expression\">" .
3305 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3306 -checked => $search_use_regexp) .
3309 $cgi->end_form() . "\n";
3313 sub git_footer_html {
3314 my $feed_class = 'rss_logo';
3316 print "<div class=\"page_footer\">\n";
3317 if (defined $project) {
3318 my $descr = git_get_project_description($project);
3319 if (defined $descr) {
3320 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3323 my %href_params = get_feed_info();
3324 if (!%href_params) {
3325 $feed_class .= ' generic';
3327 $href_params{'-title'} ||= 'log';
3329 foreach my $format qw(RSS Atom) {
3330 $href_params{'action'} = lc($format);
3331 print $cgi->a({-href => href(%href_params),
3332 -title => "$href_params{'-title'} $format feed",
3333 -class => $feed_class}, $format)."\n";
3337 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3338 -class => $feed_class}, "OPML") . " ";
3339 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3340 -class => $feed_class}, "TXT") . "\n";
3342 print "</div>\n"; # class="page_footer"
3344 if (defined $t0 && gitweb_check_feature('timed')) {
3345 print "<div id=\"generating_info\">\n";
3346 print 'This page took '.
3347 '<span id="generating_time" class="time_span">'.
3348 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
3351 '<span id="generating_cmd">'.
3352 $number_of_git_cmds.
3353 '</span> git commands '.
3355 print "</div>\n"; # class="page_footer"
3358 if (defined $site_footer && -f $site_footer) {
3359 insert_file($site_footer);
3362 print qq!<script type="text/javascript" src="$javascript"></script>\n!;
3363 if (defined $action &&
3364 $action eq 'blame_incremental') {
3365 print qq!<script type="text/javascript">\n!.
3366 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3367 qq! "!. href() .qq!");\n!.
3369 } elsif (gitweb_check_feature('javascript-actions')) {
3370 print qq!<script type="text/javascript">\n!.
3371 qq!window.onload = fixLinks;\n!.
3379 # die_error(<http_status_code>, <error_message>)
3380 # Example: die_error(404, 'Hash not found')
3381 # By convention, use the following status codes (as defined in RFC 2616):
3382 # 400: Invalid or missing CGI parameters, or
3383 # requested object exists but has wrong type.
3384 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3385 # this server or project.
3386 # 404: Requested object/revision/project doesn't exist.
3387 # 500: The server isn't configured properly, or
3388 # an internal error occurred (e.g. failed assertions caused by bugs), or
3389 # an unknown error occurred (e.g. the git binary died unexpectedly).
3390 # 503: The server is currently unavailable (because it is overloaded,
3391 # or down for maintenance). Generally, this is a temporary state.
3393 my $status = shift || 500;
3394 my $error = shift || "Internal server error";
3396 my %http_responses = (
3397 400 => '400 Bad Request',
3398 403 => '403 Forbidden',
3399 404 => '404 Not Found',
3400 500 => '500 Internal Server Error',
3401 503 => '503 Service Unavailable',
3403 git_header_html($http_responses{$status});
3405 <div class="page_body">
3415 ## ----------------------------------------------------------------------
3416 ## functions printing or outputting HTML: navigation
3418 sub git_print_page_nav {
3419 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3420 $extra = '' if !defined $extra; # pager or formats
3422 my @navs = qw(summary shortlog log commit commitdiff tree);
3424 @navs = grep { $_ ne $suppress } @navs;
3427 my %arg = map { $_ => {action=>$_} } @navs;
3428 if (defined $head) {
3429 for (qw(commit commitdiff)) {
3430 $arg{$_}{'hash'} = $head;
3432 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3433 for (qw(shortlog log)) {
3434 $arg{$_}{'hash'} = $head;
3439 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3440 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3442 my @actions = gitweb_get_feature('actions');
3445 'n' => $project, # project name
3446 'f' => $git_dir, # project path within filesystem
3447 'h' => $treehead || '', # current hash ('h' parameter)
3448 'b' => $treebase || '', # hash base ('hb' parameter)
3451 my ($label, $link, $pos) = splice(@actions,0,3);
3453 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3455 $link =~ s/%([%nfhb])/$repl{$1}/g;
3456 $arg{$label}{'_href'} = $link;
3459 print "<div class=\"page_nav\">\n" .
3461 map { $_ eq $current ?
3462 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3464 print "<br/>\n$extra<br/>\n" .
3468 sub format_paging_nav {
3469 my ($action, $page, $has_next_link) = @_;
3475 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
3477 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3478 -accesskey => "p", -title => "Alt-p"}, "prev");
3480 $paging_nav .= "first ⋅ prev";
3483 if ($has_next_link) {
3484 $paging_nav .= " ⋅ " .
3485 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3486 -accesskey => "n", -title => "Alt-n"}, "next");
3488 $paging_nav .= " ⋅ next";
3494 ## ......................................................................
3495 ## functions printing or outputting HTML: div
3497 sub git_print_header_div {
3498 my ($action, $title, $hash, $hash_base) = @_;
3501 $args{'action'} = $action;
3502 $args{'hash'} = $hash if $hash;
3503 $args{'hash_base'} = $hash_base if $hash_base;
3505 print "<div class=\"header\">\n" .
3506 $cgi->a({-href => href(%args), -class => "title"},
3507 $title ? $title : $action) .
3511 sub print_local_time {
3512 print format_local_time(@_);
3515 sub format_local_time {
3518 if ($date{'hour_local'} < 6) {
3519 $localtime .= sprintf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3520 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3522 $localtime .= sprintf(" (%02d:%02d %s)",
3523 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3529 # Outputs the author name and date in long form
3530 sub git_print_authorship {
3533 my $tag = $opts{-tag} || 'div';
3534 my $author = $co->{'author_name'};
3536 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3537 print "<$tag class=\"author_date\">" .
3538 format_search_author($author, "author", esc_html($author)) .
3540 print_local_time(%ad) if ($opts{-localtime});
3541 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3545 # Outputs table rows containing the full author or committer information,
3546 # in the format expected for 'commit' view (& similia).
3547 # Parameters are a commit hash reference, followed by the list of people
3548 # to output information for. If the list is empty it defalts to both
3549 # author and committer.
3550 sub git_print_authorship_rows {
3552 # too bad we can't use @people = @_ || ('author', 'committer')
3554 @people = ('author', 'committer') unless @people;
3555 foreach my $who (@people) {
3556 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3557 print "<tr><td>$who</td><td>" .
3558 format_search_author($co->{"${who}_name"}, $who,
3559 esc_html($co->{"${who}_name"})) . " " .
3560 format_search_author($co->{"${who}_email"}, $who,
3561 esc_html("<" . $co->{"${who}_email"} . ">")) .
3562 "</td><td rowspan=\"2\">" .
3563 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3566 "<td></td><td> $wd{'rfc2822'}";
3567 print_local_time(%wd);
3573 sub git_print_page_path {
3579 print "<div class=\"page_path\">";
3580 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3581 -title => 'tree root'}, to_utf8("[$project]"));
3583 if (defined $name) {
3584 my @dirname = split '/', $name;
3585 my $basename = pop @dirname;
3588 foreach my $dir (@dirname) {
3589 $fullname .= ($fullname ? '/' : '') . $dir;
3590 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3592 -title => $fullname}, esc_path($dir));
3595 if (defined $type && $type eq 'blob') {
3596 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3598 -title => $name}, esc_path($basename));
3599 } elsif (defined $type && $type eq 'tree') {
3600 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3602 -title => $name}, esc_path($basename));
3605 print esc_path($basename);
3608 print "<br/></div>\n";
3615 if ($opts{'-remove_title'}) {
3616 # remove title, i.e. first line of log
3619 # remove leading empty lines
3620 while (defined $log->[0] && $log->[0] eq "") {
3627 foreach my $line (@$log) {
3628 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3631 if (! $opts{'-remove_signoff'}) {
3632 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3635 # remove signoff lines
3642 # print only one empty line
3643 # do not print empty line after signoff
3645 next if ($empty || $signoff);
3651 print format_log_line_html($line) . "<br/>\n";
3654 if ($opts{'-final_empty_line'}) {
3655 # end with single empty line
3656 print "<br/>\n" unless $empty;
3660 # return link target (what link points to)
3661 sub git_get_link_target {
3666 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3670 $link_target = <$fd>;
3675 return $link_target;
3678 # given link target, and the directory (basedir) the link is in,
3679 # return target of link relative to top directory (top tree);
3680 # return undef if it is not possible (including absolute links).
3681 sub normalize_link_target {
3682 my ($link_target, $basedir) = @_;
3684 # absolute symlinks (beginning with '/') cannot be normalized
3685 return if (substr($link_target, 0, 1) eq '/');
3687 # normalize link target to path from top (root) tree (dir)
3690 $path = $basedir . '/' . $link_target;
3692 # we are in top (root) tree (dir)
3693 $path = $link_target;
3696 # remove //, /./, and /../
3698 foreach my $part (split('/', $path)) {
3699 # discard '.' and ''
3700 next if (!$part || $part eq '.');
3702 if ($part eq '..') {
3706 # link leads outside repository (outside top dir)
3710 push @path_parts, $part;
3713 $path = join('/', @path_parts);
3718 # print tree entry (row of git_tree), but without encompassing <tr> element
3719 sub git_print_tree_entry {
3720 my ($t, $basedir, $hash_base, $have_blame) = @_;
3723 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3725 # The format of a table row is: mode list link. Where mode is
3726 # the mode of the entry, list is the name of the entry, an href,
3727 # and link is the action links of the entry.
3729 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3730 if (exists $t->{'size'}) {
3731 print "<td class=\"size\">$t->{'size'}</td>\n";
3733 if ($t->{'type'} eq "blob") {
3734 print "<td class=\"list\">" .
3735 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3736 file_name=>"$basedir$t->{'name'}", %base_key),
3737 -class => "list"}, esc_path($t->{'name'}));
3738 if (S_ISLNK(oct $t->{'mode'})) {
3739 my $link_target = git_get_link_target($t->{'hash'});
3741 my $norm_target = normalize_link_target($link_target, $basedir);
3742 if (defined $norm_target) {
3744 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3745 file_name=>$norm_target),
3746 -title => $norm_target}, esc_path($link_target));
3748 print " -> " . esc_path($link_target);
3753 print "<td class=\"link\">";
3754 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3755 file_name=>"$basedir$t->{'name'}", %base_key)},
3759 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3760 file_name=>"$basedir$t->{'name'}", %base_key)},
3763 if (defined $hash_base) {
3765 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3766 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3770 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3771 file_name=>"$basedir$t->{'name'}")},
3775 } elsif ($t->{'type'} eq "tree") {
3776 print "<td class=\"list\">";
3777 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3778 file_name=>"$basedir$t->{'name'}",
3780 esc_path($t->{'name'}));
3782 print "<td class=\"link\">";
3783 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3784 file_name=>"$basedir$t->{'name'}",
3787 if (defined $hash_base) {
3789 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3790 file_name=>"$basedir$t->{'name'}")},
3795 # unknown object: we can only present history for it
3796 # (this includes 'commit' object, i.e. submodule support)
3797 print "<td class=\"list\">" .
3798 esc_path($t->{'name'}) .
3800 print "<td class=\"link\">";
3801 if (defined $hash_base) {
3802 print $cgi->a({-href => href(action=>"history",
3803 hash_base=>$hash_base,
3804 file_name=>"$basedir$t->{'name'}")},
3811 ## ......................................................................
3812 ## functions printing large fragments of HTML
3814 # get pre-image filenames for merge (combined) diff
3815 sub fill_from_file_info {
3816 my ($diff, @parents) = @_;
3818 $diff->{'from_file'} = [ ];
3819 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3820 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3821 if ($diff->{'status'}[$i] eq 'R' ||
3822 $diff->{'status'}[$i] eq 'C') {
3823 $diff->{'from_file'}[$i] =
3824 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3831 # is current raw difftree line of file deletion
3833 my $diffinfo = shift;
3835 return $diffinfo->{'to_id'} eq ('0' x 40);
3838 # does patch correspond to [previous] difftree raw line
3839 # $diffinfo - hashref of parsed raw diff format
3840 # $patchinfo - hashref of parsed patch diff format
3841 # (the same keys as in $diffinfo)
3842 sub is_patch_split {
3843 my ($diffinfo, $patchinfo) = @_;
3845 return defined $diffinfo && defined $patchinfo
3846 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3850 sub git_difftree_body {
3851 my ($difftree, $hash, @parents) = @_;
3852 my ($parent) = $parents[0];
3853 my $have_blame = gitweb_check_feature('blame');
3854 print "<div class=\"list_head\">\n";
3855 if ($#{$difftree} > 10) {
3856 print(($#{$difftree} + 1) . " files changed:\n");
3860 print "<table class=\"" .
3861 (@parents > 1 ? "combined " : "") .
3864 # header only for combined diff in 'commitdiff' view
3865 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3868 print "<thead><tr>\n" .
3869 "<th></th><th></th>\n"; # filename, patchN link
3870 for (my $i = 0; $i < @parents; $i++) {
3871 my $par = $parents[$i];
3873 $cgi->a({-href => href(action=>"commitdiff",
3874 hash=>$hash, hash_parent=>$par),
3875 -title => 'commitdiff to parent number ' .
3876 ($i+1) . ': ' . substr($par,0,7)},
3880 print "</tr></thead>\n<tbody>\n";
3885 foreach my $line (@{$difftree}) {
3886 my $diff = parsed_difftree_line($line);
3889 print "<tr class=\"dark\">\n";
3891 print "<tr class=\"light\">\n";
3895 if (exists $diff->{'nparents'}) { # combined diff
3897 fill_from_file_info($diff, @parents)
3898 unless exists $diff->{'from_file'};
3900 if (!is_deleted($diff)) {
3901 # file exists in the result (child) commit
3903 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3904 file_name=>$diff->{'to_file'},
3906 -class => "list"}, esc_path($diff->{'to_file'})) .
3910 esc_path($diff->{'to_file'}) .
3914 if ($action eq 'commitdiff') {
3917 print "<td class=\"link\">" .
3918 $cgi->a({-href => "#patch$patchno"}, "patch") .
3923 my $has_history = 0;
3924 my $not_deleted = 0;
3925 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3926 my $hash_parent = $parents[$i];
3927 my $from_hash = $diff->{'from_id'}[$i];
3928 my $from_path = $diff->{'from_file'}[$i];
3929 my $status = $diff->{'status'}[$i];
3931 $has_history ||= ($status ne 'A');
3932 $not_deleted ||= ($status ne 'D');
3934 if ($status eq 'A') {
3935 print "<td class=\"link\" align=\"right\"> | </td>\n";
3936 } elsif ($status eq 'D') {
3937 print "<td class=\"link\">" .
3938 $cgi->a({-href => href(action=>"blob",
3941 file_name=>$from_path)},
3945 if ($diff->{'to_id'} eq $from_hash) {
3946 print "<td class=\"link nochange\">";
3948 print "<td class=\"link\">";
3950 print $cgi->a({-href => href(action=>"blobdiff",
3951 hash=>$diff->{'to_id'},
3952 hash_parent=>$from_hash,
3954 hash_parent_base=>$hash_parent,
3955 file_name=>$diff->{'to_file'},
3956 file_parent=>$from_path)},
3962 print "<td class=\"link\">";
3964 print $cgi->a({-href => href(action=>"blob",
3965 hash=>$diff->{'to_id'},
3966 file_name=>$diff->{'to_file'},
3969 print " | " if ($has_history);
3972 print $cgi->a({-href => href(action=>"history",
3973 file_name=>$diff->{'to_file'},
3980 next; # instead of 'else' clause, to avoid extra indent
3982 # else ordinary diff
3984 my ($to_mode_oct, $to_mode_str, $to_file_type);
3985 my ($from_mode_oct, $from_mode_str, $from_file_type);
3986 if ($diff->{'to_mode'} ne ('0' x 6)) {
3987 $to_mode_oct = oct $diff->{'to_mode'};
3988 if (S_ISREG($to_mode_oct)) { # only for regular file
3989 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3991 $to_file_type = file_type($diff->{'to_mode'});
3993 if ($diff->{'from_mode'} ne ('0' x 6)) {
3994 $from_mode_oct = oct $diff->{'from_mode'};
3995 if (S_ISREG($to_mode_oct)) { # only for regular file
3996 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3998 $from_file_type = file_type($diff->{'from_mode'});
4001 if ($diff->{'status'} eq "A") { # created
4002 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4003 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4004 $mode_chng .= "]</span>";
4006 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4007 hash_base=>$hash, file_name=>$diff->{'file'}),
4008 -class => "list"}, esc_path($diff->{'file'}));
4010 print "<td>$mode_chng</td>\n";
4011 print "<td class=\"link\">";
4012 if ($action eq 'commitdiff') {
4015 print $cgi->a({-href => "#patch$patchno"}, "patch");
4018 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4019 hash_base=>$hash, file_name=>$diff->{'file'})},
4023 } elsif ($diff->{'status'} eq "D") { # deleted
4024 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4026 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4027 hash_base=>$parent, file_name=>$diff->{'file'}),
4028 -class => "list"}, esc_path($diff->{'file'}));
4030 print "<td>$mode_chng</td>\n";
4031 print "<td class=\"link\">";
4032 if ($action eq 'commitdiff') {
4035 print $cgi->a({-href => "#patch$patchno"}, "patch");
4038 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4039 hash_base=>$parent, file_name=>$diff->{'file'})},
4042 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4043 file_name=>$diff->{'file'})},
4046 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4047 file_name=>$diff->{'file'})},
4051 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4052 my $mode_chnge = "";
4053 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4054 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4055 if ($from_file_type ne $to_file_type) {
4056 $mode_chnge .= " from $from_file_type to $to_file_type";
4058 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4059 if ($from_mode_str && $to_mode_str) {
4060 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4061 } elsif ($to_mode_str) {
4062 $mode_chnge .= " mode: $to_mode_str";
4065 $mode_chnge .= "]</span>\n";
4068 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4069 hash_base=>$hash, file_name=>$diff->{'file'}),
4070 -class => "list"}, esc_path($diff->{'file'}));
4072 print "<td>$mode_chnge</td>\n";
4073 print "<td class=\"link\">";
4074 if ($action eq 'commitdiff') {
4077 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4079 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4080 # "commit" view and modified file (not onlu mode changed)
4081 print $cgi->a({-href => href(action=>"blobdiff",
4082 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4083 hash_base=>$hash, hash_parent_base=>$parent,
4084 file_name=>$diff->{'file'})},
4088 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4089 hash_base=>$hash, file_name=>$diff->{'file'})},
4092 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4093 file_name=>$diff->{'file'})},
4096 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4097 file_name=>$diff->{'file'})},
4101 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4102 my %status_name = ('R' => 'moved', 'C' => 'copied');
4103 my $nstatus = $status_name{$diff->{'status'}};
4105 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4106 # mode also for directories, so we cannot use $to_mode_str
4107 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4110 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4111 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4112 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4113 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4114 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4115 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4116 -class => "list"}, esc_path($diff->{'from_file'})) .
4117 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4118 "<td class=\"link\">";
4119 if ($action eq 'commitdiff') {
4122 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4124 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4125 # "commit" view and modified file (not only pure rename or copy)
4126 print $cgi->a({-href => href(action=>"blobdiff",
4127 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4128 hash_base=>$hash, hash_parent_base=>$parent,
4129 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4133 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4134 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4137 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4138 file_name=>$diff->{'to_file'})},
4141 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4142 file_name=>$diff->{'to_file'})},
4146 } # we should not encounter Unmerged (U) or Unknown (X) status
4149 print "</tbody>" if $has_header;
4153 sub git_patchset_body {
4154 my ($fd, $difftree, $hash, @hash_parents) = @_;
4155 my ($hash_parent) = $hash_parents[0];
4157 my $is_combined = (@hash_parents > 1);
4159 my $patch_number = 0;
4165 print "<div class=\"patchset\">\n";
4167 # skip to first patch
4168 while ($patch_line = <$fd>) {
4171 last if ($patch_line =~ m/^diff /);
4175 while ($patch_line) {
4177 # parse "git diff" header line
4178 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4179 # $1 is from_name, which we do not use
4180 $to_name = unquote($2);
4181 $to_name =~ s!^b/!!;
4182 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4183 # $1 is 'cc' or 'combined', which we do not use
4184 $to_name = unquote($2);
4189 # check if current patch belong to current raw line
4190 # and parse raw git-diff line if needed
4191 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4192 # this is continuation of a split patch
4193 print "<div class=\"patch cont\">\n";
4195 # advance raw git-diff output if needed
4196 $patch_idx++ if defined $diffinfo;
4198 # read and prepare patch information
4199 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4201 # compact combined diff output can have some patches skipped
4202 # find which patch (using pathname of result) we are at now;
4204 while ($to_name ne $diffinfo->{'to_file'}) {
4205 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4206 format_diff_cc_simplified($diffinfo, @hash_parents) .
4207 "</div>\n"; # class="patch"
4212 last if $patch_idx > $#$difftree;
4213 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4217 # modifies %from, %to hashes
4218 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4220 # this is first patch for raw difftree line with $patch_idx index
4221 # we index @$difftree array from 0, but number patches from 1
4222 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4226 #assert($patch_line =~ m/^diff /) if DEBUG;
4227 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4229 # print "git diff" header
4230 print format_git_diff_header_line($patch_line, $diffinfo,
4233 # print extended diff header
4234 print "<div class=\"diff extended_header\">\n";
4236 while ($patch_line = <$fd>) {
4239 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4241 print format_extended_diff_header_line($patch_line, $diffinfo,
4244 print "</div>\n"; # class="diff extended_header"
4246 # from-file/to-file diff header
4247 if (! $patch_line) {
4248 print "</div>\n"; # class="patch"
4251 next PATCH if ($patch_line =~ m/^diff /);
4252 #assert($patch_line =~ m/^---/) if DEBUG;
4254 my $last_patch_line = $patch_line;
4255 $patch_line = <$fd>;
4257 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4259 print format_diff_from_to_header($last_patch_line, $patch_line,
4260 $diffinfo, \%from, \%to,
4265 while ($patch_line = <$fd>) {
4268 next PATCH if ($patch_line =~ m/^diff /);
4270 print format_diff_line($patch_line, \%from, \%to);
4274 print "</div>\n"; # class="patch"
4277 # for compact combined (--cc) format, with chunk and patch simpliciaction
4278 # patchset might be empty, but there might be unprocessed raw lines
4279 for (++$patch_idx if $patch_number > 0;
4280 $patch_idx < @$difftree;
4282 # read and prepare patch information
4283 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4285 # generate anchor for "patch" links in difftree / whatchanged part
4286 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4287 format_diff_cc_simplified($diffinfo, @hash_parents) .
4288 "</div>\n"; # class="patch"
4293 if ($patch_number == 0) {
4294 if (@hash_parents > 1) {
4295 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4297 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4301 print "</div>\n"; # class="patchset"
4304 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4306 # fills project list info (age, description, owner, forks) for each
4307 # project in the list, removing invalid projects from returned list
4308 # NOTE: modifies $projlist, but does not remove entries from it
4309 sub fill_project_list_info {
4310 my ($projlist, $check_forks) = @_;
4313 my $show_ctags = gitweb_check_feature('ctags');
4315 foreach my $pr (@$projlist) {
4316 my (@activity) = git_get_last_activity($pr->{'path'});
4317 unless (@activity) {
4320 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4321 if (!defined $pr->{'descr'}) {
4322 my $descr = git_get_project_description($pr->{'path'}) || "";
4323 $descr = to_utf8($descr);
4324 $pr->{'descr_long'} = $descr;
4325 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4327 if (!defined $pr->{'owner'}) {
4328 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4331 my $pname = $pr->{'path'};
4332 if (($pname =~ s/\.git$//) &&
4333 ($pname !~ /\/$/) &&
4334 (-d "$projectroot/$pname")) {
4335 $pr->{'forks'} = "-d $projectroot/$pname";
4340 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4341 push @projects, $pr;
4347 # print 'sort by' <th> element, generating 'sort by $name' replay link
4348 # if that order is not selected
4350 print format_sort_th(@_);
4353 sub format_sort_th {
4354 my ($name, $order, $header) = @_;
4356 $header ||= ucfirst($name);
4358 if ($order eq $name) {
4359 $sort_th .= "<th>$header</th>\n";
4361 $sort_th .= "<th>" .
4362 $cgi->a({-href => href(-replay=>1, order=>$name),
4363 -class => "header"}, $header) .
4370 sub git_project_list_body {
4371 # actually uses global variable $project
4372 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4374 my $check_forks = gitweb_check_feature('forks');
4375 my @projects = fill_project_list_info($projlist, $check_forks);
4377 $order ||= $default_projects_order;
4378 $from = 0 unless defined $from;
4379 $to = $#projects if (!defined $to || $#projects < $to);
4382 project => { key => 'path', type => 'str' },
4383 descr => { key => 'descr_long', type => 'str' },
4384 owner => { key => 'owner', type => 'str' },
4385 age => { key => 'age', type => 'num' }
4387 my $oi = $order_info{$order};
4388 if ($oi->{'type'} eq 'str') {
4389 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4391 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4394 my $show_ctags = gitweb_check_feature('ctags');
4397 foreach my $p (@projects) {
4398 foreach my $ct (keys %{$p->{'ctags'}}) {
4399 $ctags{$ct} += $p->{'ctags'}->{$ct};
4402 my $cloud = git_populate_project_tagcloud(\%ctags);
4403 print git_show_project_tagcloud($cloud, 64);
4406 print "<table class=\"project_list\">\n";
4407 unless ($no_header) {
4410 print "<th></th>\n";
4412 print_sort_th('project', $order, 'Project');
4413 print_sort_th('descr', $order, 'Description');
4414 print_sort_th('owner', $order, 'Owner');
4415 print_sort_th('age', $order, 'Last Change');
4416 print "<th></th>\n" . # for links
4420 my $tagfilter = $cgi->param('by_tag');
4421 for (my $i = $from; $i <= $to; $i++) {
4422 my $pr = $projects[$i];
4424 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4425 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4426 and not $pr->{'descr_long'} =~ /$searchtext/;
4427 # Weed out forks or non-matching entries of search
4429 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4430 $forkbase="^$forkbase" if $forkbase;
4431 next if not $searchtext and not $tagfilter and $show_ctags
4432 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4436 print "<tr class=\"dark\">\n";
4438 print "<tr class=\"light\">\n";
4443 if ($pr->{'forks'}) {
4444 print "<!-- $pr->{'forks'} -->\n";
4445 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4449 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4450 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4451 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4452 -class => "list", -title => $pr->{'descr_long'}},
4453 esc_html($pr->{'descr'})) . "</td>\n" .
4454 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4455 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4456 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4457 "<td class=\"link\">" .
4458 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4459 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
4460 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4461 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4462 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4466 if (defined $extra) {
4469 print "<td></td>\n";
4471 print "<td colspan=\"5\">$extra</td>\n" .
4478 # uses global variable $project
4479 my ($commitlist, $from, $to, $refs, $extra) = @_;
4481 $from = 0 unless defined $from;
4482 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4484 for (my $i = 0; $i <= $to; $i++) {
4485 my %co = %{$commitlist->[$i]};
4487 my $commit = $co{'id'};
4488 my $ref = format_ref_marker($refs, $commit);
4489 my %ad = parse_date($co{'author_epoch'});
4490 git_print_header_div('commit',
4491 "<span class=\"age\">$co{'age_string'}</span>" .
4492 esc_html($co{'title'}) . $ref,
4494 print "<div class=\"title_text\">\n" .
4495 "<div class=\"log_link\">\n" .
4496 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4498 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4500 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4503 git_print_authorship(\%co, -tag => 'span');
4504 print "<br/>\n</div>\n";
4506 print "<div class=\"log_body\">\n";
4507 git_print_log($co{'comment'}, -final_empty_line=> 1);
4511 print "<div class=\"page_nav\">\n";
4517 sub git_shortlog_body {
4518 # uses global variable $project
4519 my ($commitlist, $from, $to, $refs, $extra) = @_;
4521 $from = 0 unless defined $from;
4522 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4524 print "<table class=\"shortlog\">\n";
4526 for (my $i = $from; $i <= $to; $i++) {
4527 my %co = %{$commitlist->[$i]};
4528 my $commit = $co{'id'};
4529 my $ref = format_ref_marker($refs, $commit);
4531 print "<tr class=\"dark\">\n";
4533 print "<tr class=\"light\">\n";
4536 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4537 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4538 format_author_html('td', \%co, 10) . "<td>";
4539 print format_subject_html($co{'title'}, $co{'title_short'},
4540 href(action=>"commit", hash=>$commit), $ref);
4542 "<td class=\"link\">" .
4543 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4544 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4545 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4546 my $snapshot_links = format_snapshot_links($commit);
4547 if (defined $snapshot_links) {
4548 print " | " . $snapshot_links;
4553 if (defined $extra) {
4555 "<td colspan=\"4\">$extra</td>\n" .
4561 sub git_history_body {
4562 # Warning: assumes constant type (blob or tree) during history
4563 my ($commitlist, $from, $to, $refs, $extra,
4564 $file_name, $file_hash, $ftype) = @_;
4566 $from = 0 unless defined $from;
4567 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4569 print "<table class=\"history\">\n";
4571 for (my $i = $from; $i <= $to; $i++) {
4572 my %co = %{$commitlist->[$i]};
4576 my $commit = $co{'id'};
4578 my $ref = format_ref_marker($refs, $commit);
4581 print "<tr class=\"dark\">\n";
4583 print "<tr class=\"light\">\n";
4586 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4587 # shortlog: format_author_html('td', \%co, 10)
4588 format_author_html('td', \%co, 15, 3) . "<td>";
4589 # originally git_history used chop_str($co{'title'}, 50)
4590 print format_subject_html($co{'title'}, $co{'title_short'},
4591 href(action=>"commit", hash=>$commit), $ref);
4593 "<td class=\"link\">" .
4594 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4595 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4597 if ($ftype eq 'blob') {
4598 my $blob_current = $file_hash;
4599 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4600 if (defined $blob_current && defined $blob_parent &&
4601 $blob_current ne $blob_parent) {
4603 $cgi->a({-href => href(action=>"blobdiff",
4604 hash=>$blob_current, hash_parent=>$blob_parent,
4605 hash_base=>$hash_base, hash_parent_base=>$commit,
4606 file_name=>$file_name)},
4613 if (defined $extra) {
4615 "<td colspan=\"4\">$extra</td>\n" .
4622 # uses global variable $project
4623 my ($taglist, $from, $to, $extra) = @_;
4624 $from = 0 unless defined $from;
4625 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4627 print "<table class=\"tags\">\n";
4629 for (my $i = $from; $i <= $to; $i++) {
4630 my $entry = $taglist->[$i];
4632 my $comment = $tag{'subject'};
4634 if (defined $comment) {
4635 $comment_short = chop_str($comment, 30, 5);
4638 print "<tr class=\"dark\">\n";
4640 print "<tr class=\"light\">\n";
4643 if (defined $tag{'age'}) {
4644 print "<td><i>$tag{'age'}</i></td>\n";
4646 print "<td></td>\n";
4649 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4650 -class => "list name"}, esc_html($tag{'name'})) .
4653 if (defined $comment) {
4654 print format_subject_html($comment, $comment_short,
4655 href(action=>"tag", hash=>$tag{'id'}));
4658 "<td class=\"selflink\">";
4659 if ($tag{'type'} eq "tag") {
4660 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4665 "<td class=\"link\">" . " | " .
4666 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4667 if ($tag{'reftype'} eq "commit") {
4668 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4669 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
4670 } elsif ($tag{'reftype'} eq "blob") {
4671 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4676 if (defined $extra) {
4678 "<td colspan=\"5\">$extra</td>\n" .
4684 sub git_heads_body {
4685 # uses global variable $project
4686 my ($headlist, $head, $from, $to, $extra) = @_;
4687 $from = 0 unless defined $from;
4688 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4690 print "<table class=\"heads\">\n";
4692 for (my $i = $from; $i <= $to; $i++) {
4693 my $entry = $headlist->[$i];
4695 my $curr = $ref{'id'} eq $head;
4697 print "<tr class=\"dark\">\n";
4699 print "<tr class=\"light\">\n";
4702 print "<td><i>$ref{'age'}</i></td>\n" .
4703 ($curr ? "<td class=\"current_head\">" : "<td>") .
4704 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4705 -class => "list name"},esc_html($ref{'name'})) .
4707 "<td class=\"link\">" .
4708 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4709 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4710 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4714 if (defined $extra) {
4716 "<td colspan=\"3\">$extra</td>\n" .
4722 sub git_search_grep_body {
4723 my ($commitlist, $from, $to, $extra) = @_;
4724 $from = 0 unless defined $from;
4725 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4727 print "<table class=\"commit_search\">\n";
4729 for (my $i = $from; $i <= $to; $i++) {
4730 my %co = %{$commitlist->[$i]};
4734 my $commit = $co{'id'};
4736 print "<tr class=\"dark\">\n";
4738 print "<tr class=\"light\">\n";
4741 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4742 format_author_html('td', \%co, 15, 5) .
4744 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4745 -class => "list subject"},
4746 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4747 my $comment = $co{'comment'};
4748 foreach my $line (@$comment) {
4749 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4750 my ($lead, $match, $trail) = ($1, $2, $3);
4751 $match = chop_str($match, 70, 5, 'center');
4752 my $contextlen = int((80 - length($match))/2);
4753 $contextlen = 30 if ($contextlen > 30);
4754 $lead = chop_str($lead, $contextlen, 10, 'left');
4755 $trail = chop_str($trail, $contextlen, 10, 'right');
4757 $lead = esc_html($lead);
4758 $match = esc_html($match);
4759 $trail = esc_html($trail);
4761 print "$lead<span class=\"match\">$match</span>$trail<br />";
4765 "<td class=\"link\">" .
4766 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4768 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4770 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4774 if (defined $extra) {
4776 "<td colspan=\"3\">$extra</td>\n" .
4782 ## ======================================================================
4783 ## ======================================================================
4786 sub git_project_list {
4787 my $order = $input_params{'order'};
4788 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4789 die_error(400, "Unknown order parameter");
4792 my @list = git_get_projects_list();
4794 die_error(404, "No projects found");
4798 if (defined $home_text && -f $home_text) {
4799 print "<div class=\"index_include\">\n";
4800 insert_file($home_text);
4803 print $cgi->startform(-method => "get") .
4804 "<p class=\"projsearch\">Search:\n" .
4805 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4807 $cgi->end_form() . "\n";
4808 git_project_list_body(\@list, $order);
4813 my $order = $input_params{'order'};
4814 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4815 die_error(400, "Unknown order parameter");
4818 my @list = git_get_projects_list($project);
4820 die_error(404, "No forks found");
4824 git_print_page_nav('','');
4825 git_print_header_div('summary', "$project forks");
4826 git_project_list_body(\@list, $order);
4830 sub git_project_index {
4831 my @projects = git_get_projects_list($project);
4834 -type => 'text/plain',
4835 -charset => 'utf-8',
4836 -content_disposition => 'inline; filename="index.aux"');
4838 foreach my $pr (@projects) {
4839 if (!exists $pr->{'owner'}) {
4840 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4843 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4844 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4845 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4846 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4850 print "$path $owner\n";
4855 my $descr = git_get_project_description($project) || "none";
4856 my %co = parse_commit("HEAD");
4857 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4858 my $head = $co{'id'};
4860 my $owner = git_get_project_owner($project);
4862 my $refs = git_get_references();
4863 # These get_*_list functions return one more to allow us to see if
4864 # there are more ...
4865 my @taglist = git_get_tags_list(16);
4866 my @headlist = git_get_heads_list(16);
4868 my $check_forks = gitweb_check_feature('forks');
4871 @forklist = git_get_projects_list($project);
4875 git_print_page_nav('summary','', $head);
4877 print "<div class=\"title\"> </div>\n";
4878 print "<table class=\"projects_list\">\n" .
4879 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4880 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4881 if (defined $cd{'rfc2822'}) {
4882 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4885 # use per project git URL list in $projectroot/$project/cloneurl
4886 # or make project git URL from git base URL and project name
4887 my $url_tag = "URL";
4888 my @url_list = git_get_project_url_list($project);
4889 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4890 foreach my $git_url (@url_list) {
4891 next unless $git_url;
4892 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4897 my $show_ctags = gitweb_check_feature('ctags');
4899 my $ctags = git_get_project_ctags($project);
4900 my $cloud = git_populate_project_tagcloud($ctags);
4901 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4902 print "</td>\n<td>" unless %$ctags;
4903 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4904 print "</td>\n<td>" if %$ctags;
4905 print git_show_project_tagcloud($cloud, 48);
4911 # If XSS prevention is on, we don't include README.html.
4912 # TODO: Allow a readme in some safe format.
4913 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
4914 print "<div class=\"title\">readme</div>\n" .
4915 "<div class=\"readme\">\n";
4916 insert_file("$projectroot/$project/README.html");
4917 print "\n</div>\n"; # class="readme"
4920 # we need to request one more than 16 (0..15) to check if
4922 my @commitlist = $head ? parse_commits($head, 17) : ();
4924 git_print_header_div('shortlog');
4925 git_shortlog_body(\@commitlist, 0, 15, $refs,
4926 $#commitlist <= 15 ? undef :
4927 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4931 git_print_header_div('tags');
4932 git_tags_body(\@taglist, 0, 15,
4933 $#taglist <= 15 ? undef :
4934 $cgi->a({-href => href(action=>"tags")}, "..."));
4938 git_print_header_div('heads');
4939 git_heads_body(\@headlist, $head, 0, 15,
4940 $#headlist <= 15 ? undef :
4941 $cgi->a({-href => href(action=>"heads")}, "..."));
4945 git_print_header_div('forks');
4946 git_project_list_body(\@forklist, 'age', 0, 15,
4947 $#forklist <= 15 ? undef :
4948 $cgi->a({-href => href(action=>"forks")}, "..."),
4956 my $head = git_get_head_hash($project);
4958 git_print_page_nav('','', $head,undef,$head);
4959 my %tag = parse_tag($hash);
4962 die_error(404, "Unknown tag object");
4965 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4966 print "<div class=\"title_text\">\n" .
4967 "<table class=\"object_header\">\n" .
4969 "<td>object</td>\n" .
4970 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4971 $tag{'object'}) . "</td>\n" .
4972 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4973 $tag{'type'}) . "</td>\n" .
4975 if (defined($tag{'author'})) {
4976 git_print_authorship_rows(\%tag, 'author');
4978 print "</table>\n\n" .
4980 print "<div class=\"page_body\">";
4981 my $comment = $tag{'comment'};
4982 foreach my $line (@$comment) {
4984 print esc_html($line, -nbsp=>1) . "<br/>\n";
4990 sub git_blame_common {
4991 my $format = shift || 'porcelain';
4992 if ($format eq 'porcelain' && $cgi->param('js')) {
4993 $format = 'incremental';
4994 $action = 'blame_incremental'; # for page title etc
4998 gitweb_check_feature('blame')
4999 or die_error(403, "Blame view not allowed");
5002 die_error(400, "No file name given") unless $file_name;
5003 $hash_base ||= git_get_head_hash($project);
5004 die_error(404, "Couldn't find base commit") unless $hash_base;
5005 my %co = parse_commit($hash_base)
5006 or die_error(404, "Commit not found");
5008 if (!defined $hash) {
5009 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5010 or die_error(404, "Error looking up file");
5012 $ftype = git_get_type($hash);
5013 if ($ftype !~ "blob") {
5014 die_error(400, "Object is not a blob");
5019 if ($format eq 'incremental') {
5020 # get file contents (as base)
5021 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5022 or die_error(500, "Open git-cat-file failed");
5023 } elsif ($format eq 'data') {
5024 # run git-blame --incremental
5025 open $fd, "-|", git_cmd(), "blame", "--incremental",
5026 $hash_base, "--", $file_name
5027 or die_error(500, "Open git-blame --incremental failed");
5029 # run git-blame --porcelain
5030 open $fd, "-|", git_cmd(), "blame", '-p',
5031 $hash_base, '--', $file_name
5032 or die_error(500, "Open git-blame --porcelain failed");
5035 # incremental blame data returns early
5036 if ($format eq 'data') {
5038 -type=>"text/plain", -charset => "utf-8",
5039 -status=> "200 OK");
5040 local $| = 1; # output autoflush
5043 or print "ERROR $!\n";
5046 if (defined $t0 && gitweb_check_feature('timed')) {
5048 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
5049 ' '.$number_of_git_cmds;
5059 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5062 if ($format eq 'incremental') {
5064 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
5065 "blame") . " (non-incremental)";
5068 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
5069 "blame") . " (incremental)";
5073 $cgi->a({-href => href(action=>"history", -replay=>1)},
5076 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
5078 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5079 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5080 git_print_page_path($file_name, $ftype, $hash_base);
5083 if ($format eq 'incremental') {
5084 print "<noscript>\n<div class=\"error\"><center><b>\n".
5085 "This page requires JavaScript to run.\n Use ".
5086 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
5089 "</b></center></div>\n</noscript>\n";
5091 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
5094 print qq!<div class="page_body">\n!;
5095 print qq!<div id="progress_info">... / ...</div>\n!
5096 if ($format eq 'incremental');
5097 print qq!<table id="blame_table" class="blame" width="100%">\n!.
5098 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5100 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
5104 my @rev_color = qw(light dark);
5105 my $num_colors = scalar(@rev_color);
5106 my $current_color = 0;
5108 if ($format eq 'incremental') {
5109 my $color_class = $rev_color[$current_color];
5114 while (my $line = <$fd>) {
5118 print qq!<tr id="l$linenr" class="$color_class">!.
5119 qq!<td class="sha1"><a href=""> </a></td>!.
5120 qq!<td class="linenr">!.
5121 qq!<a class="linenr" href="">$linenr</a></td>!;
5122 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
5126 } else { # porcelain, i.e. ordinary blame
5127 my %metainfo = (); # saves information about commits
5131 while (my $line = <$fd>) {
5133 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5134 # no <lines in group> for subsequent lines in group of lines
5135 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5136 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5137 if (!exists $metainfo{$full_rev}) {
5138 $metainfo{$full_rev} = { 'nprevious' => 0 };
5140 my $meta = $metainfo{$full_rev};
5142 while ($data = <$fd>) {
5144 last if ($data =~ s/^\t//); # contents of line
5145 if ($data =~ /^(\S+)(?: (.*))?$/) {
5146 $meta->{$1} = $2 unless exists $meta->{$1};
5148 if ($data =~ /^previous /) {
5149 $meta->{'nprevious'}++;
5152 my $short_rev = substr($full_rev, 0, 8);
5153 my $author = $meta->{'author'};
5155 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5156 my $date = $date{'iso-tz'};
5158 $current_color = ($current_color + 1) % $num_colors;
5160 my $tr_class = $rev_color[$current_color];
5161 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5162 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5163 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5164 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5166 print "<td class=\"sha1\"";
5167 print " title=\"". esc_html($author) . ", $date\"";
5168 print " rowspan=\"$group_size\"" if ($group_size > 1);
5170 print $cgi->a({-href => href(action=>"commit",
5172 file_name=>$file_name)},
5173 esc_html($short_rev));
5174 if ($group_size >= 2) {
5175 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5176 if (@author_initials) {
5178 esc_html(join('', @author_initials));
5184 # 'previous' <sha1 of parent commit> <filename at commit>
5185 if (exists $meta->{'previous'} &&
5186 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5187 $meta->{'parent'} = $1;
5188 $meta->{'file_parent'} = unquote($2);
5191 exists($meta->{'parent'}) ?
5192 $meta->{'parent'} : $full_rev;
5193 my $linenr_filename =
5194 exists($meta->{'file_parent'}) ?
5195 $meta->{'file_parent'} : unquote($meta->{'filename'});
5196 my $blamed = href(action => 'blame',
5197 file_name => $linenr_filename,
5198 hash_base => $linenr_commit);
5199 print "<td class=\"linenr\">";
5200 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5201 -class => "linenr" },
5204 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5212 "</table>\n"; # class="blame"
5213 print "</div>\n"; # class="blame_body"
5215 or print "Reading blob failed\n";
5224 sub git_blame_incremental {
5225 git_blame_common('incremental');
5228 sub git_blame_data {
5229 git_blame_common('data');
5233 my $head = git_get_head_hash($project);
5235 git_print_page_nav('','', $head,undef,$head);
5236 git_print_header_div('summary', $project);
5238 my @tagslist = git_get_tags_list();
5240 git_tags_body(\@tagslist);
5246 my $head = git_get_head_hash($project);
5248 git_print_page_nav('','', $head,undef,$head);
5249 git_print_header_div('summary', $project);
5251 my @headslist = git_get_heads_list();
5253 git_heads_body(\@headslist, $head);
5258 sub git_blob_plain {
5262 if (!defined $hash) {
5263 if (defined $file_name) {
5264 my $base = $hash_base || git_get_head_hash($project);
5265 $hash = git_get_hash_by_path($base, $file_name, "blob")
5266 or die_error(404, "Cannot find file");
5268 die_error(400, "No file name defined");
5270 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5271 # blobs defined by non-textual hash id's can be cached
5275 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5276 or die_error(500, "Open git-cat-file blob '$hash' failed");
5278 # content-type (can include charset)
5279 $type = blob_contenttype($fd, $file_name, $type);
5281 # "save as" filename, even when no $file_name is given
5282 my $save_as = "$hash";
5283 if (defined $file_name) {
5284 $save_as = $file_name;
5285 } elsif ($type =~ m/^text\//) {
5289 # With XSS prevention on, blobs of all types except a few known safe
5290 # ones are served with "Content-Disposition: attachment" to make sure
5291 # they don't run in our security domain. For certain image types,
5292 # blob view writes an <img> tag referring to blob_plain view, and we
5293 # want to be sure not to break that by serving the image as an
5294 # attachment (though Firefox 3 doesn't seem to care).
5295 my $sandbox = $prevent_xss &&
5296 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5300 -expires => $expires,
5301 -content_disposition =>
5302 ($sandbox ? 'attachment' : 'inline')
5303 . '; filename="' . $save_as . '"');
5305 binmode STDOUT, ':raw';
5307 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5314 if (!defined $hash) {
5315 if (defined $file_name) {
5316 my $base = $hash_base || git_get_head_hash($project);
5317 $hash = git_get_hash_by_path($base, $file_name, "blob")
5318 or die_error(404, "Cannot find file");
5320 die_error(400, "No file name defined");
5322 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5323 # blobs defined by non-textual hash id's can be cached
5327 my $have_blame = gitweb_check_feature('blame');
5328 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5329 or die_error(500, "Couldn't cat $file_name, $hash");
5330 my $mimetype = blob_mimetype($fd, $file_name);
5331 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5333 return git_blob_plain($mimetype);
5335 # we can have blame only for text/* mimetype
5336 $have_blame &&= ($mimetype =~ m!^text/!);
5338 git_header_html(undef, $expires);
5339 my $formats_nav = '';
5340 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5341 if (defined $file_name) {
5344 $cgi->a({-href => href(action=>"blame", -replay=>1)},
5349 $cgi->a({-href => href(action=>"history", -replay=>1)},
5352 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5355 $cgi->a({-href => href(action=>"blob",
5356 hash_base=>"HEAD", file_name=>$file_name)},
5360 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5363 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5364 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5366 print "<div class=\"page_nav\">\n" .
5367 "<br/><br/></div>\n" .
5368 "<div class=\"title\">$hash</div>\n";
5370 git_print_page_path($file_name, "blob", $hash_base);
5371 print "<div class=\"page_body\">\n";
5372 if ($mimetype =~ m!^image/!) {
5373 print qq!<img type="$mimetype"!;
5375 print qq! alt="$file_name" title="$file_name"!;
5378 href(action=>"blob_plain", hash=>$hash,
5379 hash_base=>$hash_base, file_name=>$file_name) .
5383 while (my $line = <$fd>) {
5386 $line = untabify($line);
5387 printf "<div class=\"pre\"><a id=\"l%i\" href=\"" . href(-replay => 1)
5388 . "#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
5389 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
5393 or print "Reading blob failed.\n";
5399 if (!defined $hash_base) {
5400 $hash_base = "HEAD";
5402 if (!defined $hash) {
5403 if (defined $file_name) {
5404 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5409 die_error(404, "No such tree") unless defined($hash);
5411 my $show_sizes = gitweb_check_feature('show-sizes');
5412 my $have_blame = gitweb_check_feature('blame');
5417 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5418 ($show_sizes ? '-l' : ()), @extra_options, $hash
5419 or die_error(500, "Open git-ls-tree failed");
5420 @entries = map { chomp; $_ } <$fd>;
5422 or die_error(404, "Reading tree failed");
5425 my $refs = git_get_references();
5426 my $ref = format_ref_marker($refs, $hash_base);
5429 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5431 if (defined $file_name) {
5433 $cgi->a({-href => href(action=>"history", -replay=>1)},
5435 $cgi->a({-href => href(action=>"tree",
5436 hash_base=>"HEAD", file_name=>$file_name)},
5439 my $snapshot_links = format_snapshot_links($hash);
5440 if (defined $snapshot_links) {
5441 # FIXME: Should be available when we have no hash base as well.
5442 push @views_nav, $snapshot_links;
5444 git_print_page_nav('tree','', $hash_base, undef, undef,
5445 join(' | ', @views_nav));
5446 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5449 print "<div class=\"page_nav\">\n";
5450 print "<br/><br/></div>\n";
5451 print "<div class=\"title\">$hash</div>\n";
5453 if (defined $file_name) {
5454 $basedir = $file_name;
5455 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5458 git_print_page_path($file_name, 'tree', $hash_base);
5460 print "<div class=\"page_body\">\n";
5461 print "<table class=\"tree\">\n";
5463 # '..' (top directory) link if possible
5464 if (defined $hash_base &&
5465 defined $file_name && $file_name =~ m![^/]+$!) {
5467 print "<tr class=\"dark\">\n";
5469 print "<tr class=\"light\">\n";
5473 my $up = $file_name;
5474 $up =~ s!/?[^/]+$!!;
5475 undef $up unless $up;
5476 # based on git_print_tree_entry
5477 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5478 print '<td class="size"> </td>'."\n" if $show_sizes;
5479 print '<td class="list">';
5480 print $cgi->a({-href => href(action=>"tree",
5481 hash_base=>$hash_base,
5485 print "<td class=\"link\"></td>\n";
5489 foreach my $line (@entries) {
5490 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
5493 print "<tr class=\"dark\">\n";
5495 print "<tr class=\"light\">\n";
5499 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
5503 print "</table>\n" .
5509 my ($project, $hash) = @_;
5511 # path/to/project.git -> project
5512 # path/to/project/.git -> project
5513 my $name = to_utf8($project);
5514 $name =~ s,([^/])/*\.git$,$1,;
5515 $name = basename($name);
5517 $name =~ s/[[:cntrl:]]/?/g;
5520 if ($hash =~ /^[0-9a-fA-F]+$/) {
5521 # shorten SHA-1 hash
5522 my $full_hash = git_get_full_hash($project, $hash);
5523 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
5524 $ver = git_get_short_hash($project, $hash);
5526 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
5527 # tags don't need shortened SHA-1 hash
5530 # branches and other need shortened SHA-1 hash
5531 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
5534 $ver .= '-' . git_get_short_hash($project, $hash);
5536 # in case of hierarchical branch names
5539 # name = project-version_string
5540 $name = "$name-$ver";
5542 return wantarray ? ($name, $name) : $name;
5546 my $format = $input_params{'snapshot_format'};
5547 if (!@snapshot_fmts) {
5548 die_error(403, "Snapshots not allowed");
5550 # default to first supported snapshot format
5551 $format ||= $snapshot_fmts[0];
5552 if ($format !~ m/^[a-z0-9]+$/) {
5553 die_error(400, "Invalid snapshot format parameter");
5554 } elsif (!exists($known_snapshot_formats{$format})) {
5555 die_error(400, "Unknown snapshot format");
5556 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
5557 die_error(403, "Snapshot format not allowed");
5558 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5559 die_error(403, "Unsupported snapshot format");
5562 my $type = git_get_type("$hash^{}");
5564 die_error(404, 'Object does not exist');
5565 } elsif ($type eq 'blob') {
5566 die_error(400, 'Object is not a tree-ish');
5569 my ($name, $prefix) = snapshot_name($project, $hash);
5570 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
5571 my $cmd = quote_command(
5572 git_cmd(), 'archive',
5573 "--format=$known_snapshot_formats{$format}{'format'}",
5574 "--prefix=$prefix/", $hash);
5575 if (exists $known_snapshot_formats{$format}{'compressor'}) {
5576 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
5579 $filename =~ s/(["\\])/\\$1/g;
5581 -type => $known_snapshot_formats{$format}{'type'},
5582 -content_disposition => 'inline; filename="' . $filename . '"',
5583 -status => '200 OK');
5585 open my $fd, "-|", $cmd
5586 or die_error(500, "Execute git-archive failed");
5587 binmode STDOUT, ':raw';
5589 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5593 sub git_log_generic {
5594 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
5596 my $head = git_get_head_hash($project);
5597 if (!defined $base) {
5600 if (!defined $page) {
5603 my $refs = git_get_references();
5605 my $commit_hash = $base;
5606 if (defined $parent) {
5607 $commit_hash = "$parent..$base";
5610 parse_commits($commit_hash, 101, (100 * $page),
5611 defined $file_name ? ($file_name, "--full-history") : ());
5614 if (!defined $file_hash && defined $file_name) {
5615 # some commits could have deleted file in question,
5616 # and not have it in tree, but one of them has to have it
5617 for (my $i = 0; $i < @commitlist; $i++) {
5618 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5619 last if defined $file_hash;
5622 if (defined $file_hash) {
5623 $ftype = git_get_type($file_hash);
5625 if (defined $file_name && !defined $ftype) {
5626 die_error(500, "Unknown type of object");
5629 if (defined $file_name) {
5630 %co = parse_commit($base)
5631 or die_error(404, "Unknown commit object");
5635 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
5637 if ($#commitlist >= 100) {
5639 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5640 -accesskey => "n", -title => "Alt-n"}, "next");
5642 my $patch_max = gitweb_get_feature('patches');
5643 if ($patch_max && !defined $file_name) {
5644 if ($patch_max < 0 || @commitlist <= $patch_max) {
5645 $paging_nav .= " ⋅ " .
5646 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5652 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
5653 if (defined $file_name) {
5654 git_print_header_div('commit', esc_html($co{'title'}), $base);
5656 git_print_header_div('summary', $project)
5658 git_print_page_path($file_name, $ftype, $hash_base)
5659 if (defined $file_name);
5661 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
5662 $file_name, $file_hash, $ftype);
5668 git_log_generic('log', \&git_log_body,
5669 $hash, $hash_parent);
5673 $hash ||= $hash_base || "HEAD";
5674 my %co = parse_commit($hash)
5675 or die_error(404, "Unknown commit object");
5677 my $parent = $co{'parent'};
5678 my $parents = $co{'parents'}; # listref
5680 # we need to prepare $formats_nav before any parameter munging
5682 if (!defined $parent) {
5684 $formats_nav .= '(initial)';
5685 } elsif (@$parents == 1) {
5686 # single parent commit
5689 $cgi->a({-href => href(action=>"commit",
5691 esc_html(substr($parent, 0, 7))) .
5698 $cgi->a({-href => href(action=>"commit",
5700 esc_html(substr($_, 0, 7)));
5704 if (gitweb_check_feature('patches') && @$parents <= 1) {
5705 $formats_nav .= " | " .
5706 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5710 if (!defined $parent) {
5714 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5716 (@$parents <= 1 ? $parent : '-c'),
5718 or die_error(500, "Open git-diff-tree failed");
5719 @difftree = map { chomp; $_ } <$fd>;
5720 close $fd or die_error(404, "Reading git-diff-tree failed");
5722 # non-textual hash id's can be cached
5724 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5727 my $refs = git_get_references();
5728 my $ref = format_ref_marker($refs, $co{'id'});
5730 git_header_html(undef, $expires);
5731 git_print_page_nav('commit', '',
5732 $hash, $co{'tree'}, $hash,
5735 if (defined $co{'parent'}) {
5736 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5738 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5740 print "<div class=\"title_text\">\n" .
5741 "<table class=\"object_header\">\n";
5742 git_print_authorship_rows(\%co);
5743 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5746 "<td class=\"sha1\">" .
5747 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5748 class => "list"}, $co{'tree'}) .
5750 "<td class=\"link\">" .
5751 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5753 my $snapshot_links = format_snapshot_links($hash);
5754 if (defined $snapshot_links) {
5755 print " | " . $snapshot_links;
5760 foreach my $par (@$parents) {
5763 "<td class=\"sha1\">" .
5764 $cgi->a({-href => href(action=>"commit", hash=>$par),
5765 class => "list"}, $par) .
5767 "<td class=\"link\">" .
5768 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
5770 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
5777 print "<div class=\"page_body\">\n";
5778 git_print_log($co{'comment'});
5781 git_difftree_body(\@difftree, $hash, @$parents);
5787 # object is defined by:
5788 # - hash or hash_base alone
5789 # - hash_base and file_name
5792 # - hash or hash_base alone
5793 if ($hash || ($hash_base && !defined $file_name)) {
5794 my $object_id = $hash || $hash_base;
5796 open my $fd, "-|", quote_command(
5797 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
5798 or die_error(404, "Object does not exist");
5802 or die_error(404, "Object does not exist");
5804 # - hash_base and file_name
5805 } elsif ($hash_base && defined $file_name) {
5806 $file_name =~ s,/+$,,;
5808 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
5809 or die_error(404, "Base object does not exist");
5811 # here errors should not hapen
5812 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
5813 or die_error(500, "Open git-ls-tree failed");
5817 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5818 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
5819 die_error(404, "File or directory for given base does not exist");
5824 die_error(400, "Not enough information to find object");
5827 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5828 hash=>$hash, hash_base=>$hash_base,
5829 file_name=>$file_name),
5830 -status => '302 Found');
5834 my $format = shift || 'html';
5841 # preparing $fd and %diffinfo for git_patchset_body
5843 if (defined $hash_base && defined $hash_parent_base) {
5844 if (defined $file_name) {
5846 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5847 $hash_parent_base, $hash_base,
5848 "--", (defined $file_parent ? $file_parent : ()), $file_name
5849 or die_error(500, "Open git-diff-tree failed");
5850 @difftree = map { chomp; $_ } <$fd>;
5852 or die_error(404, "Reading git-diff-tree failed");
5854 or die_error(404, "Blob diff not found");
5856 } elsif (defined $hash &&
5857 $hash =~ /[0-9a-fA-F]{40}/) {
5858 # try to find filename from $hash
5860 # read filtered raw output
5861 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5862 $hash_parent_base, $hash_base, "--"
5863 or die_error(500, "Open git-diff-tree failed");
5865 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5867 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5868 map { chomp; $_ } <$fd>;
5870 or die_error(404, "Reading git-diff-tree failed");
5872 or die_error(404, "Blob diff not found");
5875 die_error(400, "Missing one of the blob diff parameters");
5878 if (@difftree > 1) {
5879 die_error(400, "Ambiguous blob diff specification");
5882 %diffinfo = parse_difftree_raw_line($difftree[0]);
5883 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5884 $file_name ||= $diffinfo{'to_file'};
5886 $hash_parent ||= $diffinfo{'from_id'};
5887 $hash ||= $diffinfo{'to_id'};
5889 # non-textual hash id's can be cached
5890 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5891 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5896 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5897 '-p', ($format eq 'html' ? "--full-index" : ()),
5898 $hash_parent_base, $hash_base,
5899 "--", (defined $file_parent ? $file_parent : ()), $file_name
5900 or die_error(500, "Open git-diff-tree failed");
5903 # old/legacy style URI -- not generated anymore since 1.4.3.
5905 die_error('404 Not Found', "Missing one of the blob diff parameters")
5909 if ($format eq 'html') {
5911 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5913 git_header_html(undef, $expires);
5914 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5915 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5916 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5918 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5919 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5921 if (defined $file_name) {
5922 git_print_page_path($file_name, "blob", $hash_base);
5924 print "<div class=\"page_path\"></div>\n";
5927 } elsif ($format eq 'plain') {
5929 -type => 'text/plain',
5930 -charset => 'utf-8',
5931 -expires => $expires,
5932 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5934 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5937 die_error(400, "Unknown blobdiff format");
5941 if ($format eq 'html') {
5942 print "<div class=\"page_body\">\n";
5944 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5947 print "</div>\n"; # class="page_body"
5951 while (my $line = <$fd>) {
5952 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5953 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5957 last if $line =~ m!^\+\+\+!;
5965 sub git_blobdiff_plain {
5966 git_blobdiff('plain');
5969 sub git_commitdiff {
5971 my $format = $params{-format} || 'html';
5973 my ($patch_max) = gitweb_get_feature('patches');
5974 if ($format eq 'patch') {
5975 die_error(403, "Patch view not allowed") unless $patch_max;
5978 $hash ||= $hash_base || "HEAD";
5979 my %co = parse_commit($hash)
5980 or die_error(404, "Unknown commit object");
5982 # choose format for commitdiff for merge
5983 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5984 $hash_parent = '--cc';
5986 # we need to prepare $formats_nav before almost any parameter munging
5988 if ($format eq 'html') {
5990 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5992 if ($patch_max && @{$co{'parents'}} <= 1) {
5993 $formats_nav .= " | " .
5994 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5998 if (defined $hash_parent &&
5999 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6000 # commitdiff with two commits given
6001 my $hash_parent_short = $hash_parent;
6002 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6003 $hash_parent_short = substr($hash_parent, 0, 7);
6007 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6008 if ($co{'parents'}[$i] eq $hash_parent) {
6009 $formats_nav .= ' parent ' . ($i+1);
6013 $formats_nav .= ': ' .
6014 $cgi->a({-href => href(action=>"commitdiff",
6015 hash=>$hash_parent)},
6016 esc_html($hash_parent_short)) .
6018 } elsif (!$co{'parent'}) {
6020 $formats_nav .= ' (initial)';
6021 } elsif (scalar @{$co{'parents'}} == 1) {
6022 # single parent commit
6025 $cgi->a({-href => href(action=>"commitdiff",
6026 hash=>$co{'parent'})},
6027 esc_html(substr($co{'parent'}, 0, 7))) .
6031 if ($hash_parent eq '--cc') {
6032 $formats_nav .= ' | ' .
6033 $cgi->a({-href => href(action=>"commitdiff",
6034 hash=>$hash, hash_parent=>'-c')},
6036 } else { # $hash_parent eq '-c'
6037 $formats_nav .= ' | ' .
6038 $cgi->a({-href => href(action=>"commitdiff",
6039 hash=>$hash, hash_parent=>'--cc')},
6045 $cgi->a({-href => href(action=>"commitdiff",
6047 esc_html(substr($_, 0, 7)));
6048 } @{$co{'parents'}} ) .
6053 my $hash_parent_param = $hash_parent;
6054 if (!defined $hash_parent_param) {
6055 # --cc for multiple parents, --root for parentless
6056 $hash_parent_param =
6057 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6063 if ($format eq 'html') {
6064 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6065 "--no-commit-id", "--patch-with-raw", "--full-index",
6066 $hash_parent_param, $hash, "--"
6067 or die_error(500, "Open git-diff-tree failed");
6069 while (my $line = <$fd>) {
6071 # empty line ends raw part of diff-tree output
6073 push @difftree, scalar parse_difftree_raw_line($line);
6076 } elsif ($format eq 'plain') {
6077 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6078 '-p', $hash_parent_param, $hash, "--"
6079 or die_error(500, "Open git-diff-tree failed");
6080 } elsif ($format eq 'patch') {
6081 # For commit ranges, we limit the output to the number of
6082 # patches specified in the 'patches' feature.
6083 # For single commits, we limit the output to a single patch,
6084 # diverging from the git-format-patch default.
6085 my @commit_spec = ();
6087 if ($patch_max > 0) {
6088 push @commit_spec, "-$patch_max";
6090 push @commit_spec, '-n', "$hash_parent..$hash";
6092 if ($params{-single}) {
6093 push @commit_spec, '-1';
6095 if ($patch_max > 0) {
6096 push @commit_spec, "-$patch_max";
6098 push @commit_spec, "-n";
6100 push @commit_spec, '--root', $hash;
6102 open $fd, "-|", git_cmd(), "format-patch", '--encoding=utf8',
6103 '--stdout', @commit_spec
6104 or die_error(500, "Open git-format-patch failed");
6106 die_error(400, "Unknown commitdiff format");
6109 # non-textual hash id's can be cached
6111 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6115 # write commit message
6116 if ($format eq 'html') {
6117 my $refs = git_get_references();
6118 my $ref = format_ref_marker($refs, $co{'id'});
6120 git_header_html(undef, $expires);
6121 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6122 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6123 print "<div class=\"title_text\">\n" .
6124 "<table class=\"object_header\">\n";
6125 git_print_authorship_rows(\%co);
6128 print "<div class=\"page_body\">\n";
6129 if (@{$co{'comment'}} > 1) {
6130 print "<div class=\"log\">\n";
6131 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6132 print "</div>\n"; # class="log"
6135 } elsif ($format eq 'plain') {
6136 my $refs = git_get_references("tags");
6137 my $tagname = git_get_rev_name_tags($hash);
6138 my $filename = basename($project) . "-$hash.patch";
6141 -type => 'text/plain',
6142 -charset => 'utf-8',
6143 -expires => $expires,
6144 -content_disposition => 'inline; filename="' . "$filename" . '"');
6145 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
6146 print "From: " . to_utf8($co{'author'}) . "\n";
6147 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6148 print "Subject: " . to_utf8($co{'title'}) . "\n";
6150 print "X-Git-Tag: $tagname\n" if $tagname;
6151 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6153 foreach my $line (@{$co{'comment'}}) {
6154 print to_utf8($line) . "\n";
6157 } elsif ($format eq 'patch') {
6158 my $filename = basename($project) . "-$hash.patch";
6161 -type => 'text/plain',
6162 -charset => 'utf-8',
6163 -expires => $expires,
6164 -content_disposition => 'inline; filename="' . "$filename" . '"');
6168 if ($format eq 'html') {
6169 my $use_parents = !defined $hash_parent ||
6170 $hash_parent eq '-c' || $hash_parent eq '--cc';
6171 git_difftree_body(\@difftree, $hash,
6172 $use_parents ? @{$co{'parents'}} : $hash_parent);
6175 git_patchset_body($fd, \@difftree, $hash,
6176 $use_parents ? @{$co{'parents'}} : $hash_parent);
6178 print "</div>\n"; # class="page_body"
6181 } elsif ($format eq 'plain') {
6185 or print "Reading git-diff-tree failed\n";
6186 } elsif ($format eq 'patch') {
6190 or print "Reading git-format-patch failed\n";
6194 sub git_commitdiff_plain {
6195 git_commitdiff(-format => 'plain');
6198 # format-patch-style patches
6200 git_commitdiff(-format => 'patch', -single => 1);
6204 git_commitdiff(-format => 'patch');
6208 git_log_generic('history', \&git_history_body,
6209 $hash_base, $hash_parent_base,
6214 gitweb_check_feature('search') or die_error(403, "Search is disabled");
6215 if (!defined $searchtext) {
6216 die_error(400, "Text field is empty");
6218 if (!defined $hash) {
6219 $hash = git_get_head_hash($project);
6221 my %co = parse_commit($hash);
6223 die_error(404, "Unknown commit object");
6225 if (!defined $page) {
6229 $searchtype ||= 'commit';
6230 if ($searchtype eq 'pickaxe') {
6231 # pickaxe may take all resources of your box and run for several minutes
6232 # with every query - so decide by yourself how public you make this feature
6233 gitweb_check_feature('pickaxe')
6234 or die_error(403, "Pickaxe is disabled");
6236 if ($searchtype eq 'grep') {
6237 gitweb_check_feature('grep')
6238 or die_error(403, "Grep is disabled");
6243 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6245 if ($searchtype eq 'commit') {
6246 $greptype = "--grep=";
6247 } elsif ($searchtype eq 'author') {
6248 $greptype = "--author=";
6249 } elsif ($searchtype eq 'committer') {
6250 $greptype = "--committer=";
6252 $greptype .= $searchtext;
6253 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6254 $greptype, '--regexp-ignore-case',
6255 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6257 my $paging_nav = '';
6260 $cgi->a({-href => href(action=>"search", hash=>$hash,
6261 searchtext=>$searchtext,
6262 searchtype=>$searchtype)},
6264 $paging_nav .= " ⋅ " .
6265 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6266 -accesskey => "p", -title => "Alt-p"}, "prev");
6268 $paging_nav .= "first";
6269 $paging_nav .= " ⋅ prev";
6272 if ($#commitlist >= 100) {
6274 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6275 -accesskey => "n", -title => "Alt-n"}, "next");
6276 $paging_nav .= " ⋅ $next_link";
6278 $paging_nav .= " ⋅ next";
6281 if ($#commitlist >= 100) {
6284 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6285 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6286 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6289 if ($searchtype eq 'pickaxe') {
6290 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6291 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6293 print "<table class=\"pickaxe search\">\n";
6296 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6297 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6298 ($search_use_regexp ? '--pickaxe-regex' : ());
6301 while (my $line = <$fd>) {
6305 my %set = parse_difftree_raw_line($line);
6306 if (defined $set{'commit'}) {
6307 # finish previous commit
6310 "<td class=\"link\">" .
6311 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6313 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6319 print "<tr class=\"dark\">\n";
6321 print "<tr class=\"light\">\n";
6324 %co = parse_commit($set{'commit'});
6325 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6326 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6327 "<td><i>$author</i></td>\n" .
6329 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6330 -class => "list subject"},
6331 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6332 } elsif (defined $set{'to_id'}) {
6333 next if ($set{'to_id'} =~ m/^0{40}$/);
6335 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6336 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6338 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6344 # finish last commit (warning: repetition!)
6347 "<td class=\"link\">" .
6348 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6350 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6358 if ($searchtype eq 'grep') {
6359 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6360 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6362 print "<table class=\"grep_search\">\n";
6366 open my $fd, "-|", git_cmd(), 'grep', '-n',
6367 $search_use_regexp ? ('-E', '-i') : '-F',
6368 $searchtext, $co{'tree'};
6370 while (my $line = <$fd>) {
6372 my ($file, $lno, $ltext, $binary);
6373 last if ($matches++ > 1000);
6374 if ($line =~ /^Binary file (.+) matches$/) {
6378 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6380 if ($file ne $lastfile) {
6381 $lastfile and print "</td></tr>\n";
6383 print "<tr class=\"dark\">\n";
6385 print "<tr class=\"light\">\n";
6387 print "<td class=\"list\">".
6388 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6389 file_name=>"$file"),
6390 -class => "list"}, esc_path($file));
6391 print "</td><td>\n";
6395 print "<div class=\"binary\">Binary file</div>\n";
6397 $ltext = untabify($ltext);
6398 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6399 $ltext = esc_html($1, -nbsp=>1);
6400 $ltext .= '<span class="match">';
6401 $ltext .= esc_html($2, -nbsp=>1);
6402 $ltext .= '</span>';
6403 $ltext .= esc_html($3, -nbsp=>1);
6405 $ltext = esc_html($ltext, -nbsp=>1);
6407 print "<div class=\"pre\">" .
6408 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6409 file_name=>"$file").'#l'.$lno,
6410 -class => "linenr"}, sprintf('%4i', $lno))
6411 . ' ' . $ltext . "</div>\n";
6415 print "</td></tr>\n";
6416 if ($matches > 1000) {
6417 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6420 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6429 sub git_search_help {
6431 git_print_page_nav('','', $hash,$hash,$hash);
6433 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6434 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6435 the pattern entered is recognized as the POSIX extended
6436 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6439 <dt><b>commit</b></dt>
6440 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6442 my $have_grep = gitweb_check_feature('grep');
6445 <dt><b>grep</b></dt>
6446 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6447 a different one) are searched for the given pattern. On large trees, this search can take
6448 a while and put some strain on the server, so please use it with some consideration. Note that
6449 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6450 case-sensitive.</dd>
6454 <dt><b>author</b></dt>
6455 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6456 <dt><b>committer</b></dt>
6457 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6459 my $have_pickaxe = gitweb_check_feature('pickaxe');
6460 if ($have_pickaxe) {
6462 <dt><b>pickaxe</b></dt>
6463 <dd>All commits that caused the string to appear or disappear from any file (changes that
6464 added, removed or "modified" the string) will be listed. This search can take a while and
6465 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6466 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6474 git_log_generic('shortlog', \&git_shortlog_body,
6475 $hash, $hash_parent);
6478 ## ......................................................................
6479 ## feeds (RSS, Atom; OPML)
6482 my $format = shift || 'atom';
6483 my $have_blame = gitweb_check_feature('blame');
6485 # Atom: http://www.atomenabled.org/developers/syndication/
6486 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6487 if ($format ne 'rss' && $format ne 'atom') {
6488 die_error(400, "Unknown web feed format");
6491 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6492 my $head = $hash || 'HEAD';
6493 my @commitlist = parse_commits($head, 150, 0, $file_name);
6497 my $content_type = "application/$format+xml";
6498 if (defined $cgi->http('HTTP_ACCEPT') &&
6499 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6500 # browser (feed reader) prefers text/xml
6501 $content_type = 'text/xml';
6503 if (defined($commitlist[0])) {
6504 %latest_commit = %{$commitlist[0]};
6505 my $latest_epoch = $latest_commit{'committer_epoch'};
6506 %latest_date = parse_date($latest_epoch);
6507 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6508 if (defined $if_modified) {
6510 if (eval { require HTTP::Date; 1; }) {
6511 $since = HTTP::Date::str2time($if_modified);
6512 } elsif (eval { require Time::ParseDate; 1; }) {
6513 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6515 if (defined $since && $latest_epoch <= $since) {
6517 -type => $content_type,
6518 -charset => 'utf-8',
6519 -last_modified => $latest_date{'rfc2822'},
6520 -status => '304 Not Modified');
6525 -type => $content_type,
6526 -charset => 'utf-8',
6527 -last_modified => $latest_date{'rfc2822'});
6530 -type => $content_type,
6531 -charset => 'utf-8');
6534 # Optimization: skip generating the body if client asks only
6535 # for Last-Modified date.
6536 return if ($cgi->request_method() eq 'HEAD');
6539 my $title = "$site_name - $project/$action";
6540 my $feed_type = 'log';
6541 if (defined $hash) {
6542 $title .= " - '$hash'";
6543 $feed_type = 'branch log';
6544 if (defined $file_name) {
6545 $title .= " :: $file_name";
6546 $feed_type = 'history';
6548 } elsif (defined $file_name) {
6549 $title .= " - $file_name";
6550 $feed_type = 'history';
6552 $title .= " $feed_type";
6553 my $descr = git_get_project_description($project);
6554 if (defined $descr) {
6555 $descr = esc_html($descr);
6557 $descr = "$project " .
6558 ($format eq 'rss' ? 'RSS' : 'Atom') .
6561 my $owner = git_get_project_owner($project);
6562 $owner = esc_html($owner);
6566 if (defined $file_name) {
6567 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6568 } elsif (defined $hash) {
6569 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6571 $alt_url = href(-full=>1, action=>"summary");
6573 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6574 if ($format eq 'rss') {
6576 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6579 print "<title>$title</title>\n" .
6580 "<link>$alt_url</link>\n" .
6581 "<description>$descr</description>\n" .
6582 "<language>en</language>\n" .
6583 # project owner is responsible for 'editorial' content
6584 "<managingEditor>$owner</managingEditor>\n";
6585 if (defined $logo || defined $favicon) {
6586 # prefer the logo to the favicon, since RSS
6587 # doesn't allow both
6588 my $img = esc_url($logo || $favicon);
6590 "<url>$img</url>\n" .
6591 "<title>$title</title>\n" .
6592 "<link>$alt_url</link>\n" .
6596 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6597 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6599 print "<generator>gitweb v.$version/$git_version</generator>\n";
6600 } elsif ($format eq 'atom') {
6602 <feed xmlns="http://www.w3.org/2005/Atom">
6604 print "<title>$title</title>\n" .
6605 "<subtitle>$descr</subtitle>\n" .
6606 '<link rel="alternate" type="text/html" href="' .
6607 $alt_url . '" />' . "\n" .
6608 '<link rel="self" type="' . $content_type . '" href="' .
6609 $cgi->self_url() . '" />' . "\n" .
6610 "<id>" . href(-full=>1) . "</id>\n" .
6611 # use project owner for feed author
6612 "<author><name>$owner</name></author>\n";
6613 if (defined $favicon) {
6614 print "<icon>" . esc_url($favicon) . "</icon>\n";
6616 if (defined $logo_url) {
6617 # not twice as wide as tall: 72 x 27 pixels
6618 print "<logo>" . esc_url($logo) . "</logo>\n";
6620 if (! %latest_date) {
6621 # dummy date to keep the feed valid until commits trickle in:
6622 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6624 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6626 print "<generator version='$version/$git_version'>gitweb</generator>\n";
6630 for (my $i = 0; $i <= $#commitlist; $i++) {
6631 my %co = %{$commitlist[$i]};
6632 my $commit = $co{'id'};
6633 # we read 150, we always show 30 and the ones more recent than 48 hours
6634 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6637 my %cd = parse_date($co{'author_epoch'});
6639 # get list of changed files
6640 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6641 $co{'parent'} || "--root",
6642 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6644 my @difftree = map { chomp; $_ } <$fd>;
6648 # print element (entry, item)
6649 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6650 if ($format eq 'rss') {
6652 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6653 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6654 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6655 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6656 "<link>$co_url</link>\n" .
6657 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6658 "<content:encoded>" .
6660 } elsif ($format eq 'atom') {
6662 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6663 "<updated>$cd{'iso-8601'}</updated>\n" .
6665 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6666 if ($co{'author_email'}) {
6667 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6669 print "</author>\n" .
6670 # use committer for contributor
6672 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6673 if ($co{'committer_email'}) {
6674 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6676 print "</contributor>\n" .
6677 "<published>$cd{'iso-8601'}</published>\n" .
6678 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6679 "<id>$co_url</id>\n" .
6680 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6681 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6683 my $comment = $co{'comment'};
6685 foreach my $line (@$comment) {
6686 $line = esc_html($line);
6689 print "</pre><ul>\n";
6690 foreach my $difftree_line (@difftree) {
6691 my %difftree = parse_difftree_raw_line($difftree_line);
6692 next if !$difftree{'from_id'};
6694 my $file = $difftree{'file'} || $difftree{'to_file'};
6698 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6699 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6700 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6701 file_name=>$file, file_parent=>$difftree{'from_file'}),
6702 -title => "diff"}, 'D');
6704 print $cgi->a({-href => href(-full=>1, action=>"blame",
6705 file_name=>$file, hash_base=>$commit),
6706 -title => "blame"}, 'B');
6708 # if this is not a feed of a file history
6709 if (!defined $file_name || $file_name ne $file) {
6710 print $cgi->a({-href => href(-full=>1, action=>"history",
6711 file_name=>$file, hash=>$commit),
6712 -title => "history"}, 'H');
6714 $file = esc_path($file);
6718 if ($format eq 'rss') {
6719 print "</ul>]]>\n" .
6720 "</content:encoded>\n" .
6722 } elsif ($format eq 'atom') {
6723 print "</ul>\n</div>\n" .
6730 if ($format eq 'rss') {
6731 print "</channel>\n</rss>\n";
6732 } elsif ($format eq 'atom') {
6746 my @list = git_get_projects_list();
6749 -type => 'text/xml',
6750 -charset => 'utf-8',
6751 -content_disposition => 'inline; filename="opml.xml"');
6754 <?xml version="1.0" encoding="utf-8"?>
6755 <opml version="1.0">
6757 <title>$site_name OPML Export</title>
6760 <outline text="git RSS feeds">
6763 foreach my $pr (@list) {
6765 my $head = git_get_head_hash($proj{'path'});
6766 if (!defined $head) {
6769 $git_dir = "$projectroot/$proj{'path'}";
6770 my %co = parse_commit($head);
6775 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6776 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
6777 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
6778 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";