Merge branch 'pu' into refs/top-bases/girocco
[git/gitweb.git] / gitweb / gitweb.perl
blob7e7c68e050a9cced0b5e38e60b6c8dc05d15bcf6
1 #!/usr/bin/perl
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
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 BEGIN {
22 CGI->compile() if $ENV{'MOD_PERL'};
25 our $cgi = new CGI;
26 our $version = "++GIT_VERSION++";
27 our $my_url = $cgi->url();
28 our $my_uri = $cgi->url(-absolute => 1);
30 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
31 # needed and used only for URLs with nonempty PATH_INFO
32 our $base_url = $my_url;
34 # When the script is used as DirectoryIndex, the URL does not contain the name
35 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
36 # have to do it ourselves. We make $path_info global because it's also used
37 # later on.
39 # Another issue with the script being the DirectoryIndex is that the resulting
40 # $my_url data is not the full script URL: this is good, because we want
41 # generated links to keep implying the script name if it wasn't explicitly
42 # indicated in the URL we're handling, but it means that $my_url cannot be used
43 # as base URL.
44 # Therefore, if we needed to strip PATH_INFO, then we know that we have
45 # to build the base URL ourselves:
46 our $path_info = $ENV{"PATH_INFO"};
47 if ($path_info) {
48 if ($my_url =~ s,\Q$path_info\E$,, &&
49 $my_uri =~ s,\Q$path_info\E$,, &&
50 defined $ENV{'SCRIPT_NAME'}) {
51 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
55 # core git executable to use
56 # this can just be "git" if your webserver has a sensible PATH
57 our $GIT = "++GIT_BINDIR++/git";
59 # absolute fs-path which will be prepended to the project path
60 #our $projectroot = "/pub/scm";
61 our $projectroot = "++GITWEB_PROJECTROOT++";
63 # fs traversing limit for getting project list
64 # the number is relative to the projectroot
65 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
67 # target of the home link on top of all pages
68 our $home_link = $my_uri || "/";
70 # string of the home link on top of all pages
71 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
73 # name of your site or organization to appear in page titles
74 # replace this with something more descriptive for clearer bookmarks
75 our $site_name = "++GITWEB_SITENAME++"
76 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
78 # filename of html text to include at top of each page
79 our $site_header = "++GITWEB_SITE_HEADER++";
80 # html text to include at home page
81 our $home_text = "++GITWEB_HOMETEXT++";
82 # filename of html text to include at bottom of each page
83 our $site_footer = "++GITWEB_SITE_FOOTER++";
85 # URI of stylesheets
86 our @stylesheets = ("++GITWEB_CSS++");
87 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
88 our $stylesheet = undef;
89 # URI of GIT logo (72x27 size)
90 our $logo = "++GITWEB_LOGO++";
91 # URI of GIT favicon, assumed to be image/png type
92 our $favicon = "++GITWEB_FAVICON++";
93 # URI of gitweb.js
94 our $gitwebjs = "++GITWEB_GITWEBJS++";
96 # URI and label (title) of GIT logo link
97 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
98 #our $logo_label = "git documentation";
99 our $logo_url = "http://git-scm.com/";
100 our $logo_label = "git homepage";
102 # source of projects list
103 our $projects_list = "++GITWEB_LIST++";
105 # the width (in characters) of the projects list "Description" column
106 our $projects_list_description_width = 25;
108 # default order of projects list
109 # valid values are none, project, descr, owner, and age
110 our $default_projects_order = "project";
112 # show repository only if this file exists
113 # (only effective if this variable evaluates to true)
114 our $export_ok = "++GITWEB_EXPORT_OK++";
116 # show repository only if this subroutine returns true
117 # when given the path to the project, for example:
118 # sub { return -e "$_[0]/git-daemon-export-ok"; }
119 our $export_auth_hook = undef;
121 # only allow viewing of repositories also shown on the overview page
122 our $strict_export = "++GITWEB_STRICT_EXPORT++";
124 # list of git base URLs used for URL to where fetch project from,
125 # i.e. full URL is "$git_base_url/$project"
126 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
128 # an URL designated for pushing new changes, extended by the
129 # project name (i.e. "$git_base_push_url/$project")
130 our $git_base_push_url = '';
132 # default blob_plain mimetype and default charset for text/plain blob
133 our $default_blob_plain_mimetype = 'text/plain';
134 our $default_text_plain_charset = undef;
136 # file to use for guessing MIME types before trying /etc/mime.types
137 # (relative to the current git repository)
138 our $mimetypes_file = undef;
140 # assume this charset if line contains non-UTF-8 characters;
141 # it should be valid encoding (see Encoding::Supported(3pm) for list),
142 # for which encoding all byte sequences are valid, for example
143 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
144 # could be even 'utf-8' for the old behavior)
145 our $fallback_encoding = 'latin1';
147 # rename detection options for git-diff and git-diff-tree
148 # - default is '-M', with the cost proportional to
149 # (number of removed files) * (number of new files).
150 # - more costly is '-C' (which implies '-M'), with the cost proportional to
151 # (number of changed files + number of removed files) * (number of new files)
152 # - even more costly is '-C', '--find-copies-harder' with cost
153 # (number of files in the original tree) * (number of new files)
154 # - one might want to include '-B' option, e.g. '-B', '-M'
155 our @diff_opts = ('-M'); # taken from git_commit
157 # Disables features that would allow repository owners to inject script into
158 # the gitweb domain.
159 our $prevent_xss = 0;
161 # Whether to include project list on the gitweb front page; 0 means yes,
162 # 1 means no list but show tag cloud if enabled (all projects still need
163 # to be scanned, unless the info is cached), 2 means no list and no tag cloud
164 # (very fast)
165 our $frontpage_no_project_list = 0;
167 # projects list cache for busy sites with many projects;
168 # if you set this to non-zero, it will be used as the cached
169 # index lifetime in minutes
171 # the cached list version is stored in $cache_dir/$cache_name and can
172 # be tweaked by other scripts running with the same uid as gitweb -
173 # use this ONLY at secure installations; only single gitweb project
174 # root per system is supported, unless you tweak configuration!
175 our $projlist_cache_lifetime = 0; # in minutes
176 # FHS compliant $cache_dir would be "/var/cache/gitweb"
177 our $cache_dir =
178 (defined $ENV{'TMPDIR'} ? $ENV{'TMPDIR'} : '/tmp').'/gitweb';
179 our $projlist_cache_name = 'gitweb.index.cache';
180 our $cache_grpshared = 0;
182 # information about snapshot formats that gitweb is capable of serving
183 our %known_snapshot_formats = (
184 # name => {
185 # 'display' => display name,
186 # 'type' => mime type,
187 # 'suffix' => filename suffix,
188 # 'format' => --format for git-archive,
189 # 'compressor' => [compressor command and arguments]
190 # (array reference, optional)
191 # 'disabled' => boolean (optional)}
193 'tgz' => {
194 'display' => 'tar.gz',
195 'type' => 'application/x-gzip',
196 'suffix' => '.tar.gz',
197 'format' => 'tar',
198 'compressor' => ['gzip']},
200 'tbz2' => {
201 'display' => 'tar.bz2',
202 'type' => 'application/x-bzip2',
203 'suffix' => '.tar.bz2',
204 'format' => 'tar',
205 'compressor' => ['bzip2']},
207 'txz' => {
208 'display' => 'tar.xz',
209 'type' => 'application/x-xz',
210 'suffix' => '.tar.xz',
211 'format' => 'tar',
212 'compressor' => ['xz'],
213 'disabled' => 1},
215 'zip' => {
216 'display' => 'zip',
217 'type' => 'application/x-zip',
218 'suffix' => '.zip',
219 'format' => 'zip'},
222 # Aliases so we understand old gitweb.snapshot values in repository
223 # configuration.
224 our %known_snapshot_format_aliases = (
225 'gzip' => 'tgz',
226 'bzip2' => 'tbz2',
227 'xz' => 'txz',
229 # backward compatibility: legacy gitweb config support
230 'x-gzip' => undef, 'gz' => undef,
231 'x-bzip2' => undef, 'bz2' => undef,
232 'x-zip' => undef, '' => undef,
235 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
236 # are changed, it may be appropriate to change these values too via
237 # $GITWEB_CONFIG.
238 our %avatar_size = (
239 'default' => 16,
240 'double' => 32
243 # You define site-wide feature defaults here; override them with
244 # $GITWEB_CONFIG as necessary.
245 our %feature = (
246 # feature => {
247 # 'sub' => feature-sub (subroutine),
248 # 'override' => allow-override (boolean),
249 # 'default' => [ default options...] (array reference)}
251 # if feature is overridable (it means that allow-override has true value),
252 # then feature-sub will be called with default options as parameters;
253 # return value of feature-sub indicates if to enable specified feature
255 # if there is no 'sub' key (no feature-sub), then feature cannot be
256 # overriden
258 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
259 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
260 # is enabled
262 # Enable the 'blame' blob view, showing the last commit that modified
263 # each line in the file. This can be very CPU-intensive.
265 # To enable system wide have in $GITWEB_CONFIG
266 # $feature{'blame'}{'default'} = [1];
267 # To have project specific config enable override in $GITWEB_CONFIG
268 # $feature{'blame'}{'override'} = 1;
269 # and in project config gitweb.blame = 0|1;
270 'blame' => {
271 'sub' => sub { feature_bool('blame', @_) },
272 'override' => 0,
273 'default' => [0]},
275 # Enable the 'incremental blame' blob view, which uses javascript to
276 # incrementally show the revisions of lines as they are discovered
277 # in the history. It is better for large histories, files and slow
278 # servers, but requires javascript in the client, can slow down the
279 # browser on large files and does not show author initials.
281 # To enable system wide have in $GITWEB_CONFIG
282 # $feature{'blame_incremental'}{'default'} = [1];
283 # To have project specific config enable override in $GITWEB_CONFIG
284 # $feature{'blame_incremental'}{'override'} = 1;
285 # and in project config gitweb.blame_incremental = 0|1;
286 'blame_incremental' => {
287 'sub' => sub { feature_bool('blame_incremental', @_) },
288 'override' => 0,
289 'default' => [0]},
291 # Enable the 'snapshot' link, providing a compressed archive of any
292 # tree. This can potentially generate high traffic if you have large
293 # project.
295 # Value is a list of formats defined in %known_snapshot_formats that
296 # you wish to offer.
297 # To disable system wide have in $GITWEB_CONFIG
298 # $feature{'snapshot'}{'default'} = [];
299 # To have project specific config enable override in $GITWEB_CONFIG
300 # $feature{'snapshot'}{'override'} = 1;
301 # and in project config, a comma-separated list of formats or "none"
302 # to disable. Example: gitweb.snapshot = tbz2,zip;
303 'snapshot' => {
304 'sub' => \&feature_snapshot,
305 'override' => 0,
306 'default' => ['tgz']},
308 # Enable text search, which will list the commits which match author,
309 # committer or commit text to a given string. Enabled by default.
310 # Project specific override is not supported.
311 'search' => {
312 'override' => 0,
313 'default' => [1]},
315 # Enable grep search, which will list the files in currently selected
316 # tree containing the given string. Enabled by default. This can be
317 # potentially CPU-intensive, of course.
319 # To enable system wide have in $GITWEB_CONFIG
320 # $feature{'grep'}{'default'} = [1];
321 # To have project specific config enable override in $GITWEB_CONFIG
322 # $feature{'grep'}{'override'} = 1;
323 # and in project config gitweb.grep = 0|1;
324 'grep' => {
325 'sub' => sub { feature_bool('grep', @_) },
326 'override' => 0,
327 'default' => [1]},
329 # Enable the pickaxe search, which will list the commits that modified
330 # a given string in a file. This can be practical and quite faster
331 # alternative to 'blame', but still potentially CPU-intensive.
333 # To enable system wide have in $GITWEB_CONFIG
334 # $feature{'pickaxe'}{'default'} = [1];
335 # To have project specific config enable override in $GITWEB_CONFIG
336 # $feature{'pickaxe'}{'override'} = 1;
337 # and in project config gitweb.pickaxe = 0|1;
338 'pickaxe' => {
339 'sub' => sub { feature_bool('pickaxe', @_) },
340 'override' => 0,
341 'default' => [1]},
343 # Enable showing size of blobs in a 'tree' view, in a separate
344 # column, similar to what 'ls -l' does. This cost a bit of IO.
346 # To disable system wide have in $GITWEB_CONFIG
347 # $feature{'show-sizes'}{'default'} = [0];
348 # To have project specific config enable override in $GITWEB_CONFIG
349 # $feature{'show-sizes'}{'override'} = 1;
350 # and in project config gitweb.showsizes = 0|1;
351 'show-sizes' => {
352 'sub' => sub { feature_bool('showsizes', @_) },
353 'override' => 0,
354 'default' => [1]},
356 # Make gitweb use an alternative format of the URLs which can be
357 # more readable and natural-looking: project name is embedded
358 # directly in the path and the query string contains other
359 # auxiliary information. All gitweb installations recognize
360 # URL in either format; this configures in which formats gitweb
361 # generates links.
363 # To enable system wide have in $GITWEB_CONFIG
364 # $feature{'pathinfo'}{'default'} = [1];
365 # Project specific override is not supported.
367 # Note that you will need to change the default location of CSS,
368 # favicon, logo and possibly other files to an absolute URL. Also,
369 # if gitweb.cgi serves as your indexfile, you will need to force
370 # $my_uri to contain the script name in your $GITWEB_CONFIG.
371 'pathinfo' => {
372 'override' => 0,
373 'default' => [0]},
375 # Make gitweb consider projects in project root subdirectories
376 # to be forks of existing projects. Given project $projname.git,
377 # projects matching $projname/*.git will not be shown in the main
378 # projects list, instead a '+' mark will be added to $projname
379 # there and a 'forks' view will be enabled for the project, listing
380 # all the forks. If project list is taken from a file, forks have
381 # to be listed after the main project.
383 # To enable system wide have in $GITWEB_CONFIG
384 # $feature{'forks'}{'default'} = [1];
385 # Project specific override is not supported.
386 'forks' => {
387 'override' => 0,
388 'default' => [0]},
390 # Insert custom links to the action bar of all project pages.
391 # This enables you mainly to link to third-party scripts integrating
392 # into gitweb; e.g. git-browser for graphical history representation
393 # or custom web-based repository administration interface.
395 # The 'default' value consists of a list of triplets in the form
396 # (label, link, position) where position is the label after which
397 # to insert the link and link is a format string where %n expands
398 # to the project name, %f to the project path within the filesystem,
399 # %h to the current hash (h gitweb parameter) and %b to the current
400 # hash base (hb gitweb parameter); %% expands to %.
402 # To enable system wide have in $GITWEB_CONFIG e.g.
403 # $feature{'actions'}{'default'} = [('graphiclog',
404 # '/git-browser/by-commit.html?r=%n', 'summary')];
405 # Project specific override is not supported.
406 'actions' => {
407 'override' => 0,
408 'default' => []},
410 # Allow gitweb scan project content tags described in ctags/
411 # of project repository, and display the popular Web 2.0-ish
412 # "tag cloud" near the project list. Note that this is something
413 # COMPLETELY different from the normal Git tags.
415 # gitweb by itself can show existing tags, but it does not handle
416 # tagging itself; you need an external application for that.
417 # For an example script, check Girocco's cgi/tagproj.cgi.
418 # You may want to install the HTML::TagCloud Perl module to get
419 # a pretty tag cloud instead of just a list of tags.
421 # To enable system wide have in $GITWEB_CONFIG
422 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
423 # Project specific override is not supported.
424 'ctags' => {
425 'override' => 0,
426 'default' => [0]},
428 # The maximum number of patches in a patchset generated in patch
429 # view. Set this to 0 or undef to disable patch view, or to a
430 # negative number to remove any limit.
432 # To disable system wide have in $GITWEB_CONFIG
433 # $feature{'patches'}{'default'} = [0];
434 # To have project specific config enable override in $GITWEB_CONFIG
435 # $feature{'patches'}{'override'} = 1;
436 # and in project config gitweb.patches = 0|n;
437 # where n is the maximum number of patches allowed in a patchset.
438 'patches' => {
439 'sub' => \&feature_patches,
440 'override' => 0,
441 'default' => [16]},
443 # Avatar support. When this feature is enabled, views such as
444 # shortlog or commit will display an avatar associated with
445 # the email of the committer(s) and/or author(s).
447 # Currently available providers are gravatar and picon.
448 # If an unknown provider is specified, the feature is disabled.
450 # Gravatar depends on Digest::MD5.
451 # Picon currently relies on the indiana.edu database.
453 # To enable system wide have in $GITWEB_CONFIG
454 # $feature{'avatar'}{'default'} = ['<provider>'];
455 # where <provider> is either gravatar or picon.
456 # To have project specific config enable override in $GITWEB_CONFIG
457 # $feature{'avatar'}{'override'} = 1;
458 # and in project config gitweb.avatar = <provider>;
459 'avatar' => {
460 'sub' => \&feature_avatar,
461 'override' => 0,
462 'default' => ['']},
465 # email obfuscation
466 our $email;
467 if (eval { require HTML::Email::Obfuscate; 1 }) {
468 $email = HTML::Email::Obfuscate->new(lite => 1);
471 sub gitweb_get_feature {
472 my ($name) = @_;
473 return unless exists $feature{$name};
474 my ($sub, $override, @defaults) = (
475 $feature{$name}{'sub'},
476 $feature{$name}{'override'},
477 @{$feature{$name}{'default'}});
478 if (!$override) { return @defaults; }
479 if (!defined $sub) {
480 warn "feature $name is not overridable";
481 return @defaults;
483 return $sub->(@defaults);
486 # A wrapper to check if a given feature is enabled.
487 # With this, you can say
489 # my $bool_feat = gitweb_check_feature('bool_feat');
490 # gitweb_check_feature('bool_feat') or somecode;
492 # instead of
494 # my ($bool_feat) = gitweb_get_feature('bool_feat');
495 # (gitweb_get_feature('bool_feat'))[0] or somecode;
497 sub gitweb_check_feature {
498 return (gitweb_get_feature(@_))[0];
502 sub feature_bool {
503 my $key = shift;
504 my ($val) = git_get_project_config($key, '--bool');
506 if (!defined $val) {
507 return ($_[0]);
508 } elsif ($val eq 'true') {
509 return (1);
510 } elsif ($val eq 'false') {
511 return (0);
515 sub feature_snapshot {
516 my (@fmts) = @_;
518 my ($val) = git_get_project_config('snapshot');
520 if ($val) {
521 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
524 return @fmts;
527 sub feature_patches {
528 my @val = (git_get_project_config('patches', '--int'));
530 if (@val) {
531 return @val;
534 return ($_[0]);
537 sub feature_avatar {
538 my @val = (git_get_project_config('avatar'));
540 return @val ? @val : @_;
543 # checking HEAD file with -e is fragile if the repository was
544 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
545 # and then pruned.
546 sub check_head_link {
547 my ($dir) = @_;
548 my $headfile = "$dir/HEAD";
549 return ((-e $headfile) ||
550 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
553 sub check_export_ok {
554 my ($dir) = @_;
555 return (check_head_link($dir) &&
556 (!$export_ok || -e "$dir/$export_ok") &&
557 (!$export_auth_hook || $export_auth_hook->($dir)));
560 # process alternate names for backward compatibility
561 # filter out unsupported (unknown) snapshot formats
562 sub filter_snapshot_fmts {
563 my @fmts = @_;
565 @fmts = map {
566 exists $known_snapshot_format_aliases{$_} ?
567 $known_snapshot_format_aliases{$_} : $_} @fmts;
568 @fmts = grep {
569 exists $known_snapshot_formats{$_} &&
570 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
573 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
574 if (-e $GITWEB_CONFIG) {
575 do $GITWEB_CONFIG;
576 } else {
577 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
578 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
581 # version of the core git binary
582 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
584 $projects_list ||= $projectroot;
586 # ======================================================================
587 # input validation and dispatch
589 # input parameters can be collected from a variety of sources (presently, CGI
590 # and PATH_INFO), so we define an %input_params hash that collects them all
591 # together during validation: this allows subsequent uses (e.g. href()) to be
592 # agnostic of the parameter origin
594 our %input_params = ();
596 # input parameters are stored with the long parameter name as key. This will
597 # also be used in the href subroutine to convert parameters to their CGI
598 # equivalent, and since the href() usage is the most frequent one, we store
599 # the name -> CGI key mapping here, instead of the reverse.
601 # XXX: Warning: If you touch this, check the search form for updating,
602 # too.
604 our @cgi_param_mapping = (
605 project => "p",
606 action => "a",
607 file_name => "f",
608 file_parent => "fp",
609 hash => "h",
610 hash_parent => "hp",
611 hash_base => "hb",
612 hash_parent_base => "hpb",
613 page => "pg",
614 order => "o",
615 searchtext => "s",
616 searchtype => "st",
617 snapshot_format => "sf",
618 ctag_filter => 't',
619 extra_options => "opt",
620 search_use_regexp => "sr",
622 our %cgi_param_mapping = @cgi_param_mapping;
624 # we will also need to know the possible actions, for validation
625 our %actions = (
626 "blame" => \&git_blame,
627 "blame_incremental" => \&git_blame_incremental,
628 "blame_data" => \&git_blame_data,
629 "blobdiff" => \&git_blobdiff,
630 "blobdiff_plain" => \&git_blobdiff_plain,
631 "blob" => \&git_blob,
632 "blob_plain" => \&git_blob_plain,
633 "commitdiff" => \&git_commitdiff,
634 "commitdiff_plain" => \&git_commitdiff_plain,
635 "commit" => \&git_commit,
636 "forks" => \&git_forks,
637 "heads" => \&git_heads,
638 "history" => \&git_history,
639 "log" => \&git_log,
640 "patch" => \&git_patch,
641 "patches" => \&git_patches,
642 "rss" => \&git_rss,
643 "atom" => \&git_atom,
644 "search" => \&git_search,
645 "search_help" => \&git_search_help,
646 "shortlog" => \&git_shortlog,
647 "summary" => \&git_summary,
648 "tag" => \&git_tag,
649 "tags" => \&git_tags,
650 "tree" => \&git_tree,
651 "snapshot" => \&git_snapshot,
652 "object" => \&git_object,
653 # those below don't need $project
654 "opml" => \&git_opml,
655 "frontpage" => \&git_frontpage,
656 "project_list" => \&git_project_list,
657 "project_index" => \&git_project_index,
660 # finally, we have the hash of allowed extra_options for the commands that
661 # allow them
662 our %allowed_options = (
663 "--no-merges" => [ qw(rss atom log shortlog history) ],
666 # fill %input_params with the CGI parameters. All values except for 'opt'
667 # should be single values, but opt can be an array. We should probably
668 # build an array of parameters that can be multi-valued, but since for the time
669 # being it's only this one, we just single it out
670 while (my ($name, $symbol) = each %cgi_param_mapping) {
671 if ($symbol eq 'opt') {
672 $input_params{$name} = [ $cgi->param($symbol) ];
673 } else {
674 $input_params{$name} = $cgi->param($symbol);
678 # Backwards compatibility - by_tag= <=> t=
679 if ($cgi->param('by_tag')) {
680 $input_params{'ctag_filter'} = $cgi->param('by_tag');
683 # now read PATH_INFO and update the parameter list for missing parameters
684 sub evaluate_path_info {
685 return if defined $input_params{'project'};
686 return if !$path_info;
687 $path_info =~ s,^/+,,;
688 return if !$path_info;
690 # find which part of PATH_INFO is project
691 my $project = $path_info;
692 $project =~ s,/+$,,;
693 while ($project && !check_head_link("$projectroot/$project")) {
694 $project =~ s,/*[^/]*$,,;
696 return unless $project;
697 $input_params{'project'} = $project;
699 # do not change any parameters if an action is given using the query string
700 return if $input_params{'action'};
701 $path_info =~ s,^\Q$project\E/*,,;
703 # next, check if we have an action
704 my $action = $path_info;
705 $action =~ s,/.*$,,;
706 if (exists $actions{$action}) {
707 $path_info =~ s,^$action/*,,;
708 $input_params{'action'} = $action;
711 # list of actions that want hash_base instead of hash, but can have no
712 # pathname (f) parameter
713 my @wants_base = (
714 'tree',
715 'history',
718 # we want to catch
719 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
720 my ($parentrefname, $parentpathname, $refname, $pathname) =
721 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
723 # first, analyze the 'current' part
724 if (defined $pathname) {
725 # we got "branch:filename" or "branch:dir/"
726 # we could use git_get_type(branch:pathname), but:
727 # - it needs $git_dir
728 # - it does a git() call
729 # - the convention of terminating directories with a slash
730 # makes it superfluous
731 # - embedding the action in the PATH_INFO would make it even
732 # more superfluous
733 $pathname =~ s,^/+,,;
734 if (!$pathname || substr($pathname, -1) eq "/") {
735 $input_params{'action'} ||= "tree";
736 $pathname =~ s,/$,,;
737 } else {
738 # the default action depends on whether we had parent info
739 # or not
740 if ($parentrefname) {
741 $input_params{'action'} ||= "blobdiff_plain";
742 } else {
743 $input_params{'action'} ||= "blob_plain";
746 $input_params{'hash_base'} ||= $refname;
747 $input_params{'file_name'} ||= $pathname;
748 } elsif (defined $refname) {
749 # we got "branch". In this case we have to choose if we have to
750 # set hash or hash_base.
752 # Most of the actions without a pathname only want hash to be
753 # set, except for the ones specified in @wants_base that want
754 # hash_base instead. It should also be noted that hand-crafted
755 # links having 'history' as an action and no pathname or hash
756 # set will fail, but that happens regardless of PATH_INFO.
757 $input_params{'action'} ||= "shortlog";
758 if (grep { $_ eq $input_params{'action'} } @wants_base) {
759 $input_params{'hash_base'} ||= $refname;
760 } else {
761 $input_params{'hash'} ||= $refname;
765 # next, handle the 'parent' part, if present
766 if (defined $parentrefname) {
767 # a missing pathspec defaults to the 'current' filename, allowing e.g.
768 # someproject/blobdiff/oldrev..newrev:/filename
769 if ($parentpathname) {
770 $parentpathname =~ s,^/+,,;
771 $parentpathname =~ s,/$,,;
772 $input_params{'file_parent'} ||= $parentpathname;
773 } else {
774 $input_params{'file_parent'} ||= $input_params{'file_name'};
776 # we assume that hash_parent_base is wanted if a path was specified,
777 # or if the action wants hash_base instead of hash
778 if (defined $input_params{'file_parent'} ||
779 grep { $_ eq $input_params{'action'} } @wants_base) {
780 $input_params{'hash_parent_base'} ||= $parentrefname;
781 } else {
782 $input_params{'hash_parent'} ||= $parentrefname;
786 # for the snapshot action, we allow URLs in the form
787 # $project/snapshot/$hash.ext
788 # where .ext determines the snapshot and gets removed from the
789 # passed $refname to provide the $hash.
791 # To be able to tell that $refname includes the format extension, we
792 # require the following two conditions to be satisfied:
793 # - the hash input parameter MUST have been set from the $refname part
794 # of the URL (i.e. they must be equal)
795 # - the snapshot format MUST NOT have been defined already (e.g. from
796 # CGI parameter sf)
797 # It's also useless to try any matching unless $refname has a dot,
798 # so we check for that too
799 if (defined $input_params{'action'} &&
800 $input_params{'action'} eq 'snapshot' &&
801 defined $refname && index($refname, '.') != -1 &&
802 $refname eq $input_params{'hash'} &&
803 !defined $input_params{'snapshot_format'}) {
804 # We loop over the known snapshot formats, checking for
805 # extensions. Allowed extensions are both the defined suffix
806 # (which includes the initial dot already) and the snapshot
807 # format key itself, with a prepended dot
808 while (my ($fmt, $opt) = each %known_snapshot_formats) {
809 my $hash = $refname;
810 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
811 next;
813 my $sfx = $1;
814 # a valid suffix was found, so set the snapshot format
815 # and reset the hash parameter
816 $input_params{'snapshot_format'} = $fmt;
817 $input_params{'hash'} = $hash;
818 # we also set the format suffix to the one requested
819 # in the URL: this way a request for e.g. .tgz returns
820 # a .tgz instead of a .tar.gz
821 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
822 last;
826 evaluate_path_info();
828 our $action = $input_params{'action'};
829 if (defined $action) {
830 if (!validate_action($action)) {
831 die_error(400, "Invalid action parameter");
835 # parameters which are pathnames
836 our $project = $input_params{'project'};
837 if (defined $project) {
838 if (!validate_project($project)) {
839 undef $project;
840 die_error(404, "No such project");
844 our $file_name = $input_params{'file_name'};
845 if (defined $file_name) {
846 if (!validate_pathname($file_name)) {
847 die_error(400, "Invalid file parameter");
851 our $file_parent = $input_params{'file_parent'};
852 if (defined $file_parent) {
853 if (!validate_pathname($file_parent)) {
854 die_error(400, "Invalid file parent parameter");
858 # parameters which are refnames
859 our $hash = $input_params{'hash'};
860 if (defined $hash) {
861 if (!validate_refname($hash)) {
862 die_error(400, "Invalid hash parameter");
866 our $hash_parent = $input_params{'hash_parent'};
867 if (defined $hash_parent) {
868 if (!validate_refname($hash_parent)) {
869 die_error(400, "Invalid hash parent parameter");
873 our $hash_base = $input_params{'hash_base'};
874 if (defined $hash_base) {
875 if (!validate_refname($hash_base)) {
876 die_error(400, "Invalid hash base parameter");
880 our @extra_options = @{$input_params{'extra_options'}};
881 # @extra_options is always defined, since it can only be (currently) set from
882 # CGI, and $cgi->param() returns the empty array in array context if the param
883 # is not set
884 foreach my $opt (@extra_options) {
885 if (not exists $allowed_options{$opt}) {
886 die_error(400, "Invalid option parameter");
888 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
889 die_error(400, "Invalid option parameter for this action");
893 our $hash_parent_base = $input_params{'hash_parent_base'};
894 if (defined $hash_parent_base) {
895 if (!validate_refname($hash_parent_base)) {
896 die_error(400, "Invalid hash parent base parameter");
900 # other parameters
901 our $page = $input_params{'page'};
902 if (defined $page) {
903 if ($page =~ m/[^0-9]/) {
904 die_error(400, "Invalid page parameter");
908 our $searchtype = $input_params{'searchtype'};
909 if (defined $searchtype) {
910 if ($searchtype =~ m/[^a-z]/) {
911 die_error(400, "Invalid searchtype parameter");
915 our $search_use_regexp = $input_params{'search_use_regexp'};
917 our $searchtext = $input_params{'searchtext'};
918 our $search_regexp;
919 if (defined $searchtext) {
920 if (length($searchtext) < 2) {
921 die_error(403, "At least two characters are required for search parameter");
923 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
926 # path to the current git repository
927 our $git_dir;
928 $git_dir = "$projectroot/$project" if $project;
930 # list of supported snapshot formats
931 our @snapshot_fmts = gitweb_get_feature('snapshot');
932 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
934 # check that the avatar feature is set to a known provider name,
935 # and for each provider check if the dependencies are satisfied.
936 # if the provider name is invalid or the dependencies are not met,
937 # reset $git_avatar to the empty string.
938 our ($git_avatar) = gitweb_get_feature('avatar');
939 if ($git_avatar eq 'gravatar') {
940 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
941 } elsif ($git_avatar eq 'picon') {
942 # no dependencies
943 } else {
944 $git_avatar = '';
947 # dispatch
948 if (!defined $action) {
949 if (defined $hash) {
950 $action = git_get_type($hash);
951 } elsif (defined $hash_base && defined $file_name) {
952 $action = git_get_type("$hash_base:$file_name");
953 } elsif (defined $project) {
954 $action = 'summary';
955 } else {
956 $action = 'frontpage';
959 if (!defined($actions{$action})) {
960 die_error(400, "Unknown action");
962 if ($action !~ m/^(?:opml|frontpage|project_list|project_index)$/ &&
963 !$project) {
964 die_error(400, "Project needed");
966 $actions{$action}->();
967 exit;
969 ## ======================================================================
970 ## action links
972 sub href {
973 my %params = @_;
974 # default is to use -absolute url() i.e. $my_uri
975 my $href = $params{-full} ? $my_url : $my_uri;
977 $params{'project'} = $project unless exists $params{'project'};
979 if ($params{-replay}) {
980 while (my ($name, $symbol) = each %cgi_param_mapping) {
981 if (!exists $params{$name}) {
982 $params{$name} = $input_params{$name};
987 my $use_pathinfo = gitweb_check_feature('pathinfo');
988 if ($use_pathinfo and defined $params{'project'}) {
989 # try to put as many parameters as possible in PATH_INFO:
990 # - project name
991 # - action
992 # - hash_parent or hash_parent_base:/file_parent
993 # - hash or hash_base:/filename
994 # - the snapshot_format as an appropriate suffix
996 # When the script is the root DirectoryIndex for the domain,
997 # $href here would be something like http://gitweb.example.com/
998 # Thus, we strip any trailing / from $href, to spare us double
999 # slashes in the final URL
1000 $href =~ s,/$,,;
1002 # Then add the project name, if present
1003 $href .= "/".esc_url($params{'project'});
1004 delete $params{'project'};
1006 # since we destructively absorb parameters, we keep this
1007 # boolean that remembers if we're handling a snapshot
1008 my $is_snapshot = $params{'action'} eq 'snapshot';
1010 # Summary just uses the project path URL, any other action is
1011 # added to the URL
1012 if (defined $params{'action'}) {
1013 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
1014 delete $params{'action'};
1017 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1018 # stripping nonexistent or useless pieces
1019 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1020 || $params{'hash_parent'} || $params{'hash'});
1021 if (defined $params{'hash_base'}) {
1022 if (defined $params{'hash_parent_base'}) {
1023 $href .= esc_url($params{'hash_parent_base'});
1024 # skip the file_parent if it's the same as the file_name
1025 if (defined $params{'file_parent'}) {
1026 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1027 delete $params{'file_parent'};
1028 } elsif ($params{'file_parent'} !~ /\.\./) {
1029 $href .= ":/".esc_url($params{'file_parent'});
1030 delete $params{'file_parent'};
1033 $href .= "..";
1034 delete $params{'hash_parent'};
1035 delete $params{'hash_parent_base'};
1036 } elsif (defined $params{'hash_parent'}) {
1037 $href .= esc_url($params{'hash_parent'}). "..";
1038 delete $params{'hash_parent'};
1041 $href .= esc_url($params{'hash_base'});
1042 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1043 $href .= ":/".esc_url($params{'file_name'});
1044 delete $params{'file_name'};
1046 delete $params{'hash'};
1047 delete $params{'hash_base'};
1048 } elsif (defined $params{'hash'}) {
1049 $href .= esc_url($params{'hash'});
1050 delete $params{'hash'};
1053 # If the action was a snapshot, we can absorb the
1054 # snapshot_format parameter too
1055 if ($is_snapshot) {
1056 my $fmt = $params{'snapshot_format'};
1057 # snapshot_format should always be defined when href()
1058 # is called, but just in case some code forgets, we
1059 # fall back to the default
1060 $fmt ||= $snapshot_fmts[0];
1061 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1062 delete $params{'snapshot_format'};
1066 # now encode the parameters explicitly
1067 my @result = ();
1068 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1069 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1070 if (defined $params{$name}) {
1071 if (ref($params{$name}) eq "ARRAY") {
1072 foreach my $par (@{$params{$name}}) {
1073 push @result, $symbol . "=" . esc_param($par);
1075 } else {
1076 push @result, $symbol . "=" . esc_param($params{$name});
1080 $href .= "?" . join(';', @result) if $params{-partial_query} or scalar @result;
1082 return $href;
1086 ## ======================================================================
1087 ## validation, quoting/unquoting and escaping
1089 sub validate_action {
1090 my $input = shift || return undef;
1091 return undef unless exists $actions{$input};
1092 return $input;
1095 sub validate_project {
1096 my $input = shift || return undef;
1097 if (!validate_pathname($input) ||
1098 !(-d "$projectroot/$input") ||
1099 !check_export_ok("$projectroot/$input") ||
1100 ($strict_export && !project_in_list($input))) {
1101 return undef;
1102 } else {
1103 return $input;
1107 sub validate_pathname {
1108 my $input = shift || return undef;
1110 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1111 # at the beginning, at the end, and between slashes.
1112 # also this catches doubled slashes
1113 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1114 return undef;
1116 # no null characters
1117 if ($input =~ m!\0!) {
1118 return undef;
1120 return $input;
1123 sub validate_refname {
1124 my $input = shift || return undef;
1126 # textual hashes are O.K.
1127 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1128 return $input;
1130 # it must be correct pathname
1131 $input = validate_pathname($input)
1132 or return undef;
1133 # restrictions on ref name according to git-check-ref-format
1134 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1135 return undef;
1137 return $input;
1140 # decode sequences of octets in utf8 into Perl's internal form,
1141 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1142 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1143 sub to_utf8 {
1144 my $str = shift;
1145 if (utf8::valid($str)) {
1146 utf8::decode($str);
1147 return $str;
1148 } else {
1149 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1153 # quote unsafe chars, but keep the slash, even when it's not
1154 # correct, but quoted slashes look too horrible in bookmarks
1155 sub esc_param {
1156 my $str = shift;
1157 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1158 $str =~ s/ /\+/g;
1159 return $str;
1162 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
1163 sub esc_url {
1164 my $str = shift;
1165 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1166 $str =~ s/\+/%2B/g;
1167 $str =~ s/ /\+/g;
1168 return $str;
1171 # replace invalid utf8 character with SUBSTITUTION sequence
1172 sub esc_html {
1173 my $str = shift;
1174 my %opts = @_;
1176 $str = to_utf8($str);
1177 $str = $cgi->escapeHTML($str);
1178 if ($opts{'-nbsp'}) {
1179 $str =~ s/ /&nbsp;/g;
1181 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1182 return $str;
1185 # quote control characters and escape filename to HTML
1186 sub esc_path {
1187 my $str = shift;
1188 my %opts = @_;
1190 $str = to_utf8($str);
1191 $str = $cgi->escapeHTML($str);
1192 if ($opts{'-nbsp'}) {
1193 $str =~ s/ /&nbsp;/g;
1195 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1196 return $str;
1199 # Make control characters "printable", using character escape codes (CEC)
1200 sub quot_cec {
1201 my $cntrl = shift;
1202 my %opts = @_;
1203 my %es = ( # character escape codes, aka escape sequences
1204 "\t" => '\t', # tab (HT)
1205 "\n" => '\n', # line feed (LF)
1206 "\r" => '\r', # carrige return (CR)
1207 "\f" => '\f', # form feed (FF)
1208 "\b" => '\b', # backspace (BS)
1209 "\a" => '\a', # alarm (bell) (BEL)
1210 "\e" => '\e', # escape (ESC)
1211 "\013" => '\v', # vertical tab (VT)
1212 "\000" => '\0', # nul character (NUL)
1214 my $chr = ( (exists $es{$cntrl})
1215 ? $es{$cntrl}
1216 : sprintf('\%2x', ord($cntrl)) );
1217 if ($opts{-nohtml}) {
1218 return $chr;
1219 } else {
1220 return "<span class=\"cntrl\">$chr</span>";
1224 # Alternatively use unicode control pictures codepoints,
1225 # Unicode "printable representation" (PR)
1226 sub quot_upr {
1227 my $cntrl = shift;
1228 my %opts = @_;
1230 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1231 if ($opts{-nohtml}) {
1232 return $chr;
1233 } else {
1234 return "<span class=\"cntrl\">$chr</span>";
1238 # git may return quoted and escaped filenames
1239 sub unquote {
1240 my $str = shift;
1242 sub unq {
1243 my $seq = shift;
1244 my %es = ( # character escape codes, aka escape sequences
1245 't' => "\t", # tab (HT, TAB)
1246 'n' => "\n", # newline (NL)
1247 'r' => "\r", # return (CR)
1248 'f' => "\f", # form feed (FF)
1249 'b' => "\b", # backspace (BS)
1250 'a' => "\a", # alarm (bell) (BEL)
1251 'e' => "\e", # escape (ESC)
1252 'v' => "\013", # vertical tab (VT)
1255 if ($seq =~ m/^[0-7]{1,3}$/) {
1256 # octal char sequence
1257 return chr(oct($seq));
1258 } elsif (exists $es{$seq}) {
1259 # C escape sequence, aka character escape code
1260 return $es{$seq};
1262 # quoted ordinary character
1263 return $seq;
1266 if ($str =~ m/^"(.*)"$/) {
1267 # needs unquoting
1268 $str = $1;
1269 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1271 return $str;
1274 # escape tabs (convert tabs to spaces)
1275 sub untabify {
1276 my $line = shift;
1278 while ((my $pos = index($line, "\t")) != -1) {
1279 if (my $count = (8 - ($pos % 8))) {
1280 my $spaces = ' ' x $count;
1281 $line =~ s/\t/$spaces/;
1285 return $line;
1288 sub project_in_list {
1289 my $project = shift;
1290 my @list = git_get_projects_list();
1291 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1294 ## ----------------------------------------------------------------------
1295 ## HTML aware string manipulation
1297 # Try to chop given string on a word boundary between position
1298 # $len and $len+$add_len. If there is no word boundary there,
1299 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1300 # (marking chopped part) would be longer than given string.
1301 sub chop_str {
1302 my $str = shift;
1303 my $len = shift;
1304 my $add_len = shift || 10;
1305 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1307 # Make sure perl knows it is utf8 encoded so we don't
1308 # cut in the middle of a utf8 multibyte char.
1309 $str = to_utf8($str);
1311 # allow only $len chars, but don't cut a word if it would fit in $add_len
1312 # if it doesn't fit, cut it if it's still longer than the dots we would add
1313 # remove chopped character entities entirely
1315 # when chopping in the middle, distribute $len into left and right part
1316 # return early if chopping wouldn't make string shorter
1317 if ($where eq 'center') {
1318 return $str if ($len + 5 >= length($str)); # filler is length 5
1319 $len = int($len/2);
1320 } else {
1321 return $str if ($len + 4 >= length($str)); # filler is length 4
1324 # regexps: ending and beginning with word part up to $add_len
1325 my $endre = qr/.{$len}\w{0,$add_len}/;
1326 my $begre = qr/\w{0,$add_len}.{$len}/;
1328 if ($where eq 'left') {
1329 $str =~ m/^(.*?)($begre)$/;
1330 my ($lead, $body) = ($1, $2);
1331 if (length($lead) > 4) {
1332 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1333 $lead = " ...";
1335 return "$lead$body";
1337 } elsif ($where eq 'center') {
1338 $str =~ m/^($endre)(.*)$/;
1339 my ($left, $str) = ($1, $2);
1340 $str =~ m/^(.*?)($begre)$/;
1341 my ($mid, $right) = ($1, $2);
1342 if (length($mid) > 5) {
1343 $left =~ s/&[^;]*$//;
1344 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1345 $mid = " ... ";
1347 return "$left$mid$right";
1349 } else {
1350 $str =~ m/^($endre)(.*)$/;
1351 my $body = $1;
1352 my $tail = $2;
1353 if (length($tail) > 4) {
1354 $body =~ s/&[^;]*$//;
1355 $tail = "... ";
1357 return "$body$tail";
1361 # pass-through email filter, obfuscating it when possible
1362 sub email_obfuscate {
1363 my ($str) = @_;
1364 if ($email) {
1365 $str = $email->escape_html($str);
1366 # Stock HTML::Email::Obfuscate version likes to produce
1367 # invalid XHTML...
1368 $str =~ s#<(/?)B>#<$1b>#g;
1369 return $str;
1370 } else {
1371 $str = esc_html($str);
1372 $str =~ s/@/&#x40;/;
1373 return $str;
1377 # takes the same arguments as chop_str, but also wraps a <span> around the
1378 # result with a title attribute if it does get chopped. Additionally, the
1379 # string is HTML-escaped.
1380 sub chop_and_escape_str {
1381 my ($str) = @_;
1383 my $chopped = chop_str(@_);
1384 if ($chopped eq $str) {
1385 return email_obfuscate($chopped);
1386 } else {
1387 $str =~ s/[[:cntrl:]]/?/g;
1388 return $cgi->span({-title=>$str}, email_obfuscate($chopped));
1392 ## ----------------------------------------------------------------------
1393 ## functions returning short strings
1395 # CSS class for given age value (in seconds)
1396 sub age_class {
1397 my $age = shift;
1399 if (!defined $age) {
1400 return "noage";
1401 } elsif ($age < 60*60*2) {
1402 return "age0";
1403 } elsif ($age < 60*60*24*2) {
1404 return "age1";
1405 } else {
1406 return "age2";
1410 # convert age in seconds to "nn units ago" string
1411 sub age_string {
1412 my $age = shift;
1413 my $age_str;
1415 if ($age > 60*60*24*365*2) {
1416 $age_str = (int $age/60/60/24/365);
1417 $age_str .= " years ago";
1418 } elsif ($age > 60*60*24*(365/12)*2) {
1419 $age_str = int $age/60/60/24/(365/12);
1420 $age_str .= " months ago";
1421 } elsif ($age > 60*60*24*7*2) {
1422 $age_str = int $age/60/60/24/7;
1423 $age_str .= " weeks ago";
1424 } elsif ($age > 60*60*24*2) {
1425 $age_str = int $age/60/60/24;
1426 $age_str .= " days ago";
1427 } elsif ($age > 60*60*2) {
1428 $age_str = int $age/60/60;
1429 $age_str .= " hours ago";
1430 } elsif ($age > 60*2) {
1431 $age_str = int $age/60;
1432 $age_str .= " min ago";
1433 } elsif ($age > 2) {
1434 $age_str = int $age;
1435 $age_str .= " sec ago";
1436 } else {
1437 $age_str .= " right now";
1439 return $age_str;
1442 use constant {
1443 S_IFINVALID => 0030000,
1444 S_IFGITLINK => 0160000,
1447 # submodule/subproject, a commit object reference
1448 sub S_ISGITLINK {
1449 my $mode = shift;
1451 return (($mode & S_IFMT) == S_IFGITLINK)
1454 # convert file mode in octal to symbolic file mode string
1455 sub mode_str {
1456 my $mode = oct shift;
1458 if (S_ISGITLINK($mode)) {
1459 return 'm---------';
1460 } elsif (S_ISDIR($mode & S_IFMT)) {
1461 return 'drwxr-xr-x';
1462 } elsif (S_ISLNK($mode)) {
1463 return 'lrwxrwxrwx';
1464 } elsif (S_ISREG($mode)) {
1465 # git cares only about the executable bit
1466 if ($mode & S_IXUSR) {
1467 return '-rwxr-xr-x';
1468 } else {
1469 return '-rw-r--r--';
1471 } else {
1472 return '----------';
1476 # convert file mode in octal to file type string
1477 sub file_type {
1478 my $mode = shift;
1480 if ($mode !~ m/^[0-7]+$/) {
1481 return $mode;
1482 } else {
1483 $mode = oct $mode;
1486 if (S_ISGITLINK($mode)) {
1487 return "submodule";
1488 } elsif (S_ISDIR($mode & S_IFMT)) {
1489 return "directory";
1490 } elsif (S_ISLNK($mode)) {
1491 return "symlink";
1492 } elsif (S_ISREG($mode)) {
1493 return "file";
1494 } else {
1495 return "unknown";
1499 # convert file mode in octal to file type description string
1500 sub file_type_long {
1501 my $mode = shift;
1503 if ($mode !~ m/^[0-7]+$/) {
1504 return $mode;
1505 } else {
1506 $mode = oct $mode;
1509 if (S_ISGITLINK($mode)) {
1510 return "submodule";
1511 } elsif (S_ISDIR($mode & S_IFMT)) {
1512 return "directory";
1513 } elsif (S_ISLNK($mode)) {
1514 return "symlink";
1515 } elsif (S_ISREG($mode)) {
1516 if ($mode & S_IXUSR) {
1517 return "executable";
1518 } else {
1519 return "file";
1521 } else {
1522 return "unknown";
1527 ## ----------------------------------------------------------------------
1528 ## functions returning short HTML fragments, or transforming HTML fragments
1529 ## which don't belong to other sections
1531 # format line of commit message.
1532 sub format_log_line_html {
1533 my $line = shift;
1535 $line = esc_html($line, -nbsp=>1);
1536 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1537 $cgi->a({-href => href(action=>"object", hash=>$1),
1538 -class => "text"}, $1);
1539 }eg;
1541 return $line;
1544 # format marker of refs pointing to given object
1546 # the destination action is chosen based on object type and current context:
1547 # - for annotated tags, we choose the tag view unless it's the current view
1548 # already, in which case we go to shortlog view
1549 # - for other refs, we keep the current view if we're in history, shortlog or
1550 # log view, and select shortlog otherwise
1551 sub format_ref_marker {
1552 my ($refs, $id) = @_;
1553 my $markers = '';
1555 if (defined $refs->{$id}) {
1556 foreach my $ref (@{$refs->{$id}}) {
1557 # this code exploits the fact that non-lightweight tags are the
1558 # only indirect objects, and that they are the only objects for which
1559 # we want to use tag instead of shortlog as action
1560 my ($type, $name) = qw();
1561 my $indirect = ($ref =~ s/\^\{\}$//);
1562 # e.g. tags/v2.6.11 or heads/next
1563 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1564 $type = $1;
1565 $name = $2;
1566 } else {
1567 $type = "ref";
1568 $name = $ref;
1571 my $class = $type;
1572 $class .= " indirect" if $indirect;
1574 my $dest_action = "shortlog";
1576 if ($indirect) {
1577 $dest_action = "tag" unless $action eq "tag";
1578 } elsif ($action =~ /^(history|(short)?log)$/) {
1579 $dest_action = $action;
1582 my $dest = "";
1583 $dest .= "refs/" unless $ref =~ m!^refs/!;
1584 $dest .= $ref;
1586 my $link = $cgi->a({
1587 -href => href(
1588 action=>$dest_action,
1589 hash=>$dest
1590 )}, $name);
1592 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1593 $link . "</span>";
1597 if ($markers) {
1598 return ' <span class="refs">'. $markers . '</span>';
1599 } else {
1600 return "";
1604 # format, perhaps shortened and with markers, title line
1605 sub format_subject_html {
1606 my ($long, $short, $href, $extra) = @_;
1607 $extra = '' unless defined($extra);
1609 if (length($short) < length($long)) {
1610 $long =~ s/[[:cntrl:]]/?/g;
1611 return $cgi->a({-href => $href, -class => "list subject",
1612 -title => to_utf8($long)},
1613 esc_html($short)) . $extra;
1614 } else {
1615 return $cgi->a({-href => $href, -class => "list subject"},
1616 esc_html($long)) . $extra;
1620 # Rather than recomputing the url for an email multiple times, we cache it
1621 # after the first hit. This gives a visible benefit in views where the avatar
1622 # for the same email is used repeatedly (e.g. shortlog).
1623 # The cache is shared by all avatar engines (currently gravatar only), which
1624 # are free to use it as preferred. Since only one avatar engine is used for any
1625 # given page, there's no risk for cache conflicts.
1626 our %avatar_cache = ();
1628 # Compute the picon url for a given email, by using the picon search service over at
1629 # http://www.cs.indiana.edu/picons/search.html
1630 sub picon_url {
1631 my $email = lc shift;
1632 if (!$avatar_cache{$email}) {
1633 my ($user, $domain) = split('@', $email);
1634 $avatar_cache{$email} =
1635 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1636 "$domain/$user/" .
1637 "users+domains+unknown/up/single";
1639 return $avatar_cache{$email};
1642 # Compute the gravatar url for a given email, if it's not in the cache already.
1643 # Gravatar stores only the part of the URL before the size, since that's the
1644 # one computationally more expensive. This also allows reuse of the cache for
1645 # different sizes (for this particular engine).
1646 sub gravatar_url {
1647 my $email = lc shift;
1648 my $size = shift;
1649 $avatar_cache{$email} ||=
1650 "http://www.gravatar.com/avatar/" .
1651 Digest::MD5::md5_hex($email) . "?s=";
1652 return $avatar_cache{$email} . $size;
1655 # Insert an avatar for the given $email at the given $size if the feature
1656 # is enabled.
1657 sub git_get_avatar {
1658 my ($email, %opts) = @_;
1659 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1660 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1661 $opts{-size} ||= 'default';
1662 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1663 my $url = "";
1664 if ($git_avatar eq 'gravatar') {
1665 $url = gravatar_url($email, $size);
1666 } elsif ($git_avatar eq 'picon') {
1667 $url = picon_url($email);
1669 # Other providers can be added by extending the if chain, defining $url
1670 # as needed. If no variant puts something in $url, we assume avatars
1671 # are completely disabled/unavailable.
1672 if ($url) {
1673 return $pre_white .
1674 "<img width=\"$size\" " .
1675 "class=\"avatar\" " .
1676 "src=\"$url\" " .
1677 "alt=\"\" " .
1678 "/>" . $post_white;
1679 } else {
1680 return "";
1684 sub format_search_author {
1685 my ($author, $searchtype, $displaytext) = @_;
1686 my $have_search = gitweb_check_feature('search');
1688 if ($have_search) {
1689 my $performed = "";
1690 if ($searchtype eq 'author') {
1691 $performed = "authored";
1692 } elsif ($searchtype eq 'committer') {
1693 $performed = "committed";
1696 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1697 searchtext=>$author,
1698 searchtype=>$searchtype), class=>"list",
1699 title=>"Search for commits $performed by $author"},
1700 $displaytext);
1702 } else {
1703 return $displaytext;
1707 # format the author name of the given commit with the given tag
1708 # the author name is chopped and escaped according to the other
1709 # optional parameters (see chop_str).
1710 sub format_author_html {
1711 my $tag = shift;
1712 my $co = shift;
1713 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1714 return "<$tag class=\"author\">" .
1715 format_search_author($co->{'author_name'}, "author",
1716 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1717 $author) .
1718 "</$tag>";
1721 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1722 sub format_git_diff_header_line {
1723 my $line = shift;
1724 my $diffinfo = shift;
1725 my ($from, $to) = @_;
1727 if ($diffinfo->{'nparents'}) {
1728 # combined diff
1729 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1730 if ($to->{'href'}) {
1731 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1732 esc_path($to->{'file'}));
1733 } else { # file was deleted (no href)
1734 $line .= esc_path($to->{'file'});
1736 } else {
1737 # "ordinary" diff
1738 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1739 if ($from->{'href'}) {
1740 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1741 'a/' . esc_path($from->{'file'}));
1742 } else { # file was added (no href)
1743 $line .= 'a/' . esc_path($from->{'file'});
1745 $line .= ' ';
1746 if ($to->{'href'}) {
1747 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1748 'b/' . esc_path($to->{'file'}));
1749 } else { # file was deleted
1750 $line .= 'b/' . esc_path($to->{'file'});
1754 return "<div class=\"diff header\">$line</div>\n";
1757 # format extended diff header line, before patch itself
1758 sub format_extended_diff_header_line {
1759 my $line = shift;
1760 my $diffinfo = shift;
1761 my ($from, $to) = @_;
1763 # match <path>
1764 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1765 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1766 esc_path($from->{'file'}));
1768 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1769 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1770 esc_path($to->{'file'}));
1772 # match single <mode>
1773 if ($line =~ m/\s(\d{6})$/) {
1774 $line .= '<span class="info"> (' .
1775 file_type_long($1) .
1776 ')</span>';
1778 # match <hash>
1779 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1780 # can match only for combined diff
1781 $line = 'index ';
1782 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1783 if ($from->{'href'}[$i]) {
1784 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1785 -class=>"hash"},
1786 substr($diffinfo->{'from_id'}[$i],0,7));
1787 } else {
1788 $line .= '0' x 7;
1790 # separator
1791 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1793 $line .= '..';
1794 if ($to->{'href'}) {
1795 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1796 substr($diffinfo->{'to_id'},0,7));
1797 } else {
1798 $line .= '0' x 7;
1801 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1802 # can match only for ordinary diff
1803 my ($from_link, $to_link);
1804 if ($from->{'href'}) {
1805 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1806 substr($diffinfo->{'from_id'},0,7));
1807 } else {
1808 $from_link = '0' x 7;
1810 if ($to->{'href'}) {
1811 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1812 substr($diffinfo->{'to_id'},0,7));
1813 } else {
1814 $to_link = '0' x 7;
1816 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1817 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1820 return $line . "<br/>\n";
1823 # format from-file/to-file diff header
1824 sub format_diff_from_to_header {
1825 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1826 my $line;
1827 my $result = '';
1829 $line = $from_line;
1830 #assert($line =~ m/^---/) if DEBUG;
1831 # no extra formatting for "^--- /dev/null"
1832 if (! $diffinfo->{'nparents'}) {
1833 # ordinary (single parent) diff
1834 if ($line =~ m!^--- "?a/!) {
1835 if ($from->{'href'}) {
1836 $line = '--- a/' .
1837 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1838 esc_path($from->{'file'}));
1839 } else {
1840 $line = '--- a/' .
1841 esc_path($from->{'file'});
1844 $result .= qq!<div class="diff from_file">$line</div>\n!;
1846 } else {
1847 # combined diff (merge commit)
1848 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1849 if ($from->{'href'}[$i]) {
1850 $line = '--- ' .
1851 $cgi->a({-href=>href(action=>"blobdiff",
1852 hash_parent=>$diffinfo->{'from_id'}[$i],
1853 hash_parent_base=>$parents[$i],
1854 file_parent=>$from->{'file'}[$i],
1855 hash=>$diffinfo->{'to_id'},
1856 hash_base=>$hash,
1857 file_name=>$to->{'file'}),
1858 -class=>"path",
1859 -title=>"diff" . ($i+1)},
1860 $i+1) .
1861 '/' .
1862 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1863 esc_path($from->{'file'}[$i]));
1864 } else {
1865 $line = '--- /dev/null';
1867 $result .= qq!<div class="diff from_file">$line</div>\n!;
1871 $line = $to_line;
1872 #assert($line =~ m/^\+\+\+/) if DEBUG;
1873 # no extra formatting for "^+++ /dev/null"
1874 if ($line =~ m!^\+\+\+ "?b/!) {
1875 if ($to->{'href'}) {
1876 $line = '+++ b/' .
1877 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1878 esc_path($to->{'file'}));
1879 } else {
1880 $line = '+++ b/' .
1881 esc_path($to->{'file'});
1884 $result .= qq!<div class="diff to_file">$line</div>\n!;
1886 return $result;
1889 # create note for patch simplified by combined diff
1890 sub format_diff_cc_simplified {
1891 my ($diffinfo, @parents) = @_;
1892 my $result = '';
1894 $result .= "<div class=\"diff header\">" .
1895 "diff --cc ";
1896 if (!is_deleted($diffinfo)) {
1897 $result .= $cgi->a({-href => href(action=>"blob",
1898 hash_base=>$hash,
1899 hash=>$diffinfo->{'to_id'},
1900 file_name=>$diffinfo->{'to_file'}),
1901 -class => "path"},
1902 esc_path($diffinfo->{'to_file'}));
1903 } else {
1904 $result .= esc_path($diffinfo->{'to_file'});
1906 $result .= "</div>\n" . # class="diff header"
1907 "<div class=\"diff nodifferences\">" .
1908 "Simple merge" .
1909 "</div>\n"; # class="diff nodifferences"
1911 return $result;
1914 # format patch (diff) line (not to be used for diff headers)
1915 sub format_diff_line {
1916 my $line = shift;
1917 my ($from, $to) = @_;
1918 my $diff_class = "";
1920 chomp $line;
1922 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1923 # combined diff
1924 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1925 if ($line =~ m/^\@{3}/) {
1926 $diff_class = " chunk_header";
1927 } elsif ($line =~ m/^\\/) {
1928 $diff_class = " incomplete";
1929 } elsif ($prefix =~ tr/+/+/) {
1930 $diff_class = " add";
1931 } elsif ($prefix =~ tr/-/-/) {
1932 $diff_class = " rem";
1934 } else {
1935 # assume ordinary diff
1936 my $char = substr($line, 0, 1);
1937 if ($char eq '+') {
1938 $diff_class = " add";
1939 } elsif ($char eq '-') {
1940 $diff_class = " rem";
1941 } elsif ($char eq '@') {
1942 $diff_class = " chunk_header";
1943 } elsif ($char eq "\\") {
1944 $diff_class = " incomplete";
1947 $line = untabify($line);
1948 if ($from && $to && $line =~ m/^\@{2} /) {
1949 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1950 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1952 $from_lines = 0 unless defined $from_lines;
1953 $to_lines = 0 unless defined $to_lines;
1955 if ($from->{'href'}) {
1956 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1957 -class=>"list"}, $from_text);
1959 if ($to->{'href'}) {
1960 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1961 -class=>"list"}, $to_text);
1963 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1964 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1965 return "<div class=\"diff$diff_class\">$line</div>\n";
1966 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1967 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1968 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1970 @from_text = split(' ', $ranges);
1971 for (my $i = 0; $i < @from_text; ++$i) {
1972 ($from_start[$i], $from_nlines[$i]) =
1973 (split(',', substr($from_text[$i], 1)), 0);
1976 $to_text = pop @from_text;
1977 $to_start = pop @from_start;
1978 $to_nlines = pop @from_nlines;
1980 $line = "<span class=\"chunk_info\">$prefix ";
1981 for (my $i = 0; $i < @from_text; ++$i) {
1982 if ($from->{'href'}[$i]) {
1983 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1984 -class=>"list"}, $from_text[$i]);
1985 } else {
1986 $line .= $from_text[$i];
1988 $line .= " ";
1990 if ($to->{'href'}) {
1991 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1992 -class=>"list"}, $to_text);
1993 } else {
1994 $line .= $to_text;
1996 $line .= " $prefix</span>" .
1997 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1998 return "<div class=\"diff$diff_class\">$line</div>\n";
2000 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
2003 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2004 # linked. Pass the hash of the tree/commit to snapshot.
2005 sub format_snapshot_links {
2006 my ($hash) = @_;
2007 my $num_fmts = @snapshot_fmts;
2008 if ($num_fmts > 1) {
2009 # A parenthesized list of links bearing format names.
2010 # e.g. "snapshot (_tar.gz_ _zip_)"
2011 return "snapshot (" . join(' ', map
2012 $cgi->a({
2013 -href => href(
2014 action=>"snapshot",
2015 hash=>$hash,
2016 snapshot_format=>$_
2018 }, $known_snapshot_formats{$_}{'display'})
2019 , @snapshot_fmts) . ")";
2020 } elsif ($num_fmts == 1) {
2021 # A single "snapshot" link whose tooltip bears the format name.
2022 # i.e. "_snapshot_"
2023 my ($fmt) = @snapshot_fmts;
2024 return
2025 $cgi->a({
2026 -href => href(
2027 action=>"snapshot",
2028 hash=>$hash,
2029 snapshot_format=>$fmt
2031 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2032 }, "snapshot");
2033 } else { # $num_fmts == 0
2034 return undef;
2038 ## ......................................................................
2039 ## functions returning values to be passed, perhaps after some
2040 ## transformation, to other functions; e.g. returning arguments to href()
2042 # returns hash to be passed to href to generate gitweb URL
2043 # in -title key it returns description of link
2044 sub get_feed_info {
2045 my $format = shift || 'Atom';
2046 my %res = (action => lc($format));
2048 # feed links are possible only for project views
2049 return unless (defined $project);
2050 # some views should link to OPML, or to generic project feed,
2051 # or don't have specific feed yet (so they should use generic)
2052 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2054 my $branch;
2055 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2056 # from tag links; this also makes possible to detect branch links
2057 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2058 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2059 $branch = $1;
2061 # find log type for feed description (title)
2062 my $type = 'log';
2063 if (defined $file_name) {
2064 $type = "history of $file_name";
2065 $type .= "/" if ($action eq 'tree');
2066 $type .= " on '$branch'" if (defined $branch);
2067 } else {
2068 $type = "log of $branch" if (defined $branch);
2071 $res{-title} = $type;
2072 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2073 $res{'file_name'} = $file_name;
2075 return %res;
2078 ## ----------------------------------------------------------------------
2079 ## git utility subroutines, invoking git commands
2081 # returns path to the core git executable and the --git-dir parameter as list
2082 sub git_cmd {
2083 return $GIT, '--git-dir='.$git_dir;
2086 # quote the given arguments for passing them to the shell
2087 # quote_command("command", "arg 1", "arg with ' and ! characters")
2088 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2089 # Try to avoid using this function wherever possible.
2090 sub quote_command {
2091 return join(' ',
2092 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2095 # get HEAD ref of given project as hash
2096 sub git_get_head_hash {
2097 my $project = shift;
2098 my $o_git_dir = $git_dir;
2099 my $retval = undef;
2100 $git_dir = "$projectroot/$project";
2101 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
2102 my $head = <$fd>;
2103 close $fd;
2104 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
2105 $retval = $1;
2108 if (defined $o_git_dir) {
2109 $git_dir = $o_git_dir;
2111 return $retval;
2114 # get type of given object
2115 sub git_get_type {
2116 my $hash = shift;
2118 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2119 my $type = <$fd>;
2120 close $fd or return;
2121 chomp $type;
2122 return $type;
2125 # repository configuration
2126 our $config_file = '';
2127 our %config;
2129 # store multiple values for single key as anonymous array reference
2130 # single values stored directly in the hash, not as [ <value> ]
2131 sub hash_set_multi {
2132 my ($hash, $key, $value) = @_;
2134 if (!exists $hash->{$key}) {
2135 $hash->{$key} = $value;
2136 } elsif (!ref $hash->{$key}) {
2137 $hash->{$key} = [ $hash->{$key}, $value ];
2138 } else {
2139 push @{$hash->{$key}}, $value;
2143 # return hash of git project configuration
2144 # optionally limited to some section, e.g. 'gitweb'
2145 sub git_parse_project_config {
2146 my $section_regexp = shift;
2147 my %config;
2149 local $/ = "\0";
2151 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2152 or return;
2154 while (my $keyval = <$fh>) {
2155 chomp $keyval;
2156 my ($key, $value) = split(/\n/, $keyval, 2);
2158 hash_set_multi(\%config, $key, $value)
2159 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2161 close $fh;
2163 return %config;
2166 # convert config value to boolean: 'true' or 'false'
2167 # no value, number > 0, 'true' and 'yes' values are true
2168 # rest of values are treated as false (never as error)
2169 sub config_to_bool {
2170 my $val = shift;
2172 return 1 if !defined $val; # section.key
2174 # strip leading and trailing whitespace
2175 $val =~ s/^\s+//;
2176 $val =~ s/\s+$//;
2178 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2179 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2182 # convert config value to simple decimal number
2183 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2184 # to be multiplied by 1024, 1048576, or 1073741824
2185 sub config_to_int {
2186 my $val = shift;
2188 # strip leading and trailing whitespace
2189 $val =~ s/^\s+//;
2190 $val =~ s/\s+$//;
2192 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2193 $unit = lc($unit);
2194 # unknown unit is treated as 1
2195 return $num * ($unit eq 'g' ? 1073741824 :
2196 $unit eq 'm' ? 1048576 :
2197 $unit eq 'k' ? 1024 : 1);
2199 return $val;
2202 # convert config value to array reference, if needed
2203 sub config_to_multi {
2204 my $val = shift;
2206 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2209 sub git_get_project_config {
2210 my ($key, $type) = @_;
2212 # key sanity check
2213 return unless ($key);
2214 $key =~ s/^gitweb\.//;
2215 return if ($key =~ m/\W/);
2217 # type sanity check
2218 if (defined $type) {
2219 $type =~ s/^--//;
2220 $type = undef
2221 unless ($type eq 'bool' || $type eq 'int');
2224 # get config
2225 if (!defined $config_file ||
2226 $config_file ne "$git_dir/config") {
2227 %config = git_parse_project_config('gitweb');
2228 $config_file = "$git_dir/config";
2231 # check if config variable (key) exists
2232 return unless exists $config{"gitweb.$key"};
2234 # ensure given type
2235 if (!defined $type) {
2236 return $config{"gitweb.$key"};
2237 } elsif ($type eq 'bool') {
2238 # backward compatibility: 'git config --bool' returns true/false
2239 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2240 } elsif ($type eq 'int') {
2241 return config_to_int($config{"gitweb.$key"});
2243 return $config{"gitweb.$key"};
2246 # get hash of given path at given ref
2247 sub git_get_hash_by_path {
2248 my $base = shift;
2249 my $path = shift || return undef;
2250 my $type = shift;
2252 $path =~ s,/+$,,;
2254 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2255 or die_error(500, "Open git-ls-tree failed");
2256 my $line = <$fd>;
2257 close $fd or return undef;
2259 if (!defined $line) {
2260 # there is no tree or hash given by $path at $base
2261 return undef;
2264 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2265 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2266 if (defined $type && $type ne $2) {
2267 # type doesn't match
2268 return undef;
2270 return $3;
2273 # get path of entry with given hash at given tree-ish (ref)
2274 # used to get 'from' filename for combined diff (merge commit) for renames
2275 sub git_get_path_by_hash {
2276 my $base = shift || return;
2277 my $hash = shift || return;
2279 local $/ = "\0";
2281 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2282 or return undef;
2283 while (my $line = <$fd>) {
2284 chomp $line;
2286 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2287 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2288 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2289 close $fd;
2290 return $1;
2293 close $fd;
2294 return undef;
2297 ## ......................................................................
2298 ## git utility functions, directly accessing git repository
2300 sub git_get_project_description {
2301 my $path = shift;
2303 $git_dir = "$projectroot/$path";
2304 open my $fd, '<', "$git_dir/description"
2305 or return git_get_project_config('description');
2306 my $descr = <$fd>;
2307 close $fd;
2308 if (defined $descr) {
2309 chomp $descr;
2311 return $descr;
2314 sub git_get_project_ctags {
2315 my $path = shift;
2316 my $ctags = {};
2318 $git_dir = "$projectroot/$path";
2319 opendir my $dh, "$git_dir/ctags"
2320 or return $ctags;
2321 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2322 open my $ct, '<', $_ or next;
2323 my $val = <$ct>;
2324 chomp $val;
2325 close $ct;
2326 my $ctag = $_; $ctag =~ s#.*/##;
2327 $ctags->{$ctag} = $val;
2329 closedir $dh;
2330 $ctags;
2333 sub git_populate_project_tagcloud {
2334 my ($ctags, $action) = @_;
2336 # First, merge different-cased tags; tags vote on casing
2337 my %ctags_lc;
2338 foreach (keys %$ctags) {
2339 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2340 if (not $ctags_lc{lc $_}->{topcount}
2341 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2342 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2343 $ctags_lc{lc $_}->{topname} = $_;
2347 my $cloud;
2348 if (eval { require HTML::TagCloud; 1; }) {
2349 $cloud = HTML::TagCloud->new;
2350 foreach (sort keys %ctags_lc) {
2351 # Pad the title with spaces so that the cloud looks
2352 # less crammed.
2353 my $title = $ctags_lc{$_}->{topname};
2354 $title =~ s/ /&nbsp;/g;
2355 $title =~ s/^/&nbsp;/g;
2356 $title =~ s/$/&nbsp;/g;
2357 $cloud->add($title, href(-replay=>1, action=>$action, ctag_filter=>$_),
2358 $ctags_lc{$_}->{count});
2360 } else {
2361 $cloud = \%ctags_lc;
2363 $cloud;
2366 sub git_show_project_tagcloud {
2367 my ($cloud, $count, $action) = @_;
2368 print STDERR ref($cloud)."..\n";
2369 if (ref $cloud eq 'HTML::TagCloud') {
2370 return $cloud->html_and_css($count);
2371 } else {
2372 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2373 return '<p align="center">' . join (', ', map {
2374 $cgi->a({-href => href(-replay=>1, action=>$action, ctag_filter=>$_)},
2375 $cloud->{$_}->{topname});
2376 } splice(@tags, 0, $count)) . '</p>';
2380 sub git_get_project_url_list {
2381 my $path = shift;
2383 $git_dir = "$projectroot/$path";
2384 open my $fd, '<', "$git_dir/cloneurl"
2385 or return wantarray ?
2386 @{ config_to_multi(git_get_project_config('url')) } :
2387 config_to_multi(git_get_project_config('url'));
2388 my @git_project_url_list = map { chomp; $_ } <$fd>;
2389 close $fd;
2391 return wantarray ? @git_project_url_list : \@git_project_url_list;
2394 sub git_get_projects_list {
2395 my ($filter) = @_;
2396 my @list;
2398 $filter ||= '';
2399 $filter =~ s/\.git$//;
2401 my $check_forks = gitweb_check_feature('forks');
2403 if (-d $projects_list) {
2404 # search in directory
2405 my $dir = $projects_list . ($filter ? "/$filter" : '');
2406 # remove the trailing "/"
2407 $dir =~ s!/+$!!;
2408 my $pfxlen = length("$dir");
2409 my $pfxdepth = ($dir =~ tr!/!!);
2411 File::Find::find({
2412 follow_fast => 1, # follow symbolic links
2413 follow_skip => 2, # ignore duplicates
2414 dangling_symlinks => 0, # ignore dangling symlinks, silently
2415 wanted => sub {
2416 # skip project-list toplevel, if we get it.
2417 return if (m!^[/.]$!);
2418 # only directories can be git repositories
2419 return unless (-d $_);
2420 # don't traverse too deep (Find is super slow on os x)
2421 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2422 $File::Find::prune = 1;
2423 return;
2426 my $subdir = substr($File::Find::name, $pfxlen + 1);
2427 # we check related file in $projectroot
2428 my $path = ($filter ? "$filter/" : '') . $subdir;
2429 if (check_export_ok("$projectroot/$path")) {
2430 push @list, { path => $path };
2431 $File::Find::prune = 1;
2434 }, "$dir");
2436 } elsif (-f $projects_list) {
2437 # read from file(url-encoded):
2438 # 'git%2Fgit.git Linus+Torvalds'
2439 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2440 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2441 my %paths;
2442 open my $fd, '<', $projects_list or return;
2443 PROJECT:
2444 while (my $line = <$fd>) {
2445 chomp $line;
2446 my ($path, $owner) = split ' ', $line;
2447 $path = unescape($path);
2448 $owner = unescape($owner);
2449 if (!defined $path) {
2450 next;
2452 if ($filter ne '') {
2453 # looking for forks;
2454 my $pfx = substr($path, 0, length($filter));
2455 if ($pfx ne $filter) {
2456 next PROJECT;
2458 my $sfx = substr($path, length($filter));
2459 if ($sfx !~ /^\/.*\.git$/) {
2460 next PROJECT;
2462 } elsif ($check_forks) {
2463 PATH:
2464 foreach my $filter (keys %paths) {
2465 # looking for forks;
2466 my $pfx = substr($path, 0, length($filter));
2467 if ($pfx ne $filter) {
2468 next PATH;
2470 my $sfx = substr($path, length($filter));
2471 if ($sfx !~ /^\/.*\.git$/) {
2472 next PATH;
2474 # is a fork, don't include it in
2475 # the list
2476 next PROJECT;
2479 if (check_export_ok("$projectroot/$path")) {
2480 my $pr = {
2481 path => $path,
2482 owner => to_utf8($owner),
2484 push @list, $pr;
2485 (my $forks_path = $path) =~ s/\.git$//;
2486 $paths{$forks_path}++;
2489 close $fd;
2491 return @list;
2494 our $gitweb_project_owner = undef;
2495 sub git_get_project_list_from_file {
2497 return if (defined $gitweb_project_owner);
2499 $gitweb_project_owner = {};
2500 # read from file (url-encoded):
2501 # 'git%2Fgit.git Linus+Torvalds'
2502 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2503 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2504 if (-f $projects_list) {
2505 open(my $fd, '<', $projects_list);
2506 while (my $line = <$fd>) {
2507 chomp $line;
2508 my ($pr, $ow) = split ' ', $line;
2509 $pr = unescape($pr);
2510 $ow = unescape($ow);
2511 $gitweb_project_owner->{$pr} = to_utf8($ow);
2513 close $fd;
2517 sub git_get_project_owner {
2518 my $project = shift;
2519 my $owner;
2521 return undef unless $project;
2522 $git_dir = "$projectroot/$project";
2524 if (!defined $gitweb_project_owner) {
2525 git_get_project_list_from_file();
2528 if (exists $gitweb_project_owner->{$project}) {
2529 $owner = $gitweb_project_owner->{$project};
2531 if (!defined $owner){
2532 $owner = git_get_project_config('owner');
2534 if (!defined $owner) {
2535 $owner = get_file_owner("$git_dir");
2538 return $owner;
2541 sub git_get_last_activity {
2542 my ($path) = @_;
2543 my $fd;
2545 $git_dir = "$projectroot/$path";
2546 open($fd, "-|", git_cmd(), 'for-each-ref',
2547 '--format=%(committer)',
2548 '--sort=-committerdate',
2549 '--count=1',
2550 'refs/heads') or return;
2551 my $most_recent = <$fd>;
2552 close $fd or return;
2553 if (defined $most_recent &&
2554 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2555 my $timestamp = $1;
2556 my $age = time - $timestamp;
2557 return ($age, age_string($age));
2559 return (undef, undef);
2562 sub git_get_references {
2563 my $type = shift || "";
2564 my %refs;
2565 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2566 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2567 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2568 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2569 or return;
2571 while (my $line = <$fd>) {
2572 chomp $line;
2573 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2574 if (defined $refs{$1}) {
2575 push @{$refs{$1}}, $2;
2576 } else {
2577 $refs{$1} = [ $2 ];
2581 close $fd or return;
2582 return \%refs;
2585 sub git_get_rev_name_tags {
2586 my $hash = shift || return undef;
2588 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2589 or return;
2590 my $name_rev = <$fd>;
2591 close $fd;
2593 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2594 return $1;
2595 } else {
2596 # catches also '$hash undefined' output
2597 return undef;
2601 ## ----------------------------------------------------------------------
2602 ## parse to hash functions
2604 sub parse_date {
2605 my $epoch = shift;
2606 my $tz = shift || "-0000";
2608 my %date;
2609 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2610 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2611 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2612 $date{'hour'} = $hour;
2613 $date{'minute'} = $min;
2614 $date{'mday'} = $mday;
2615 $date{'day'} = $days[$wday];
2616 $date{'month'} = $months[$mon];
2617 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2618 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2619 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2620 $mday, $months[$mon], $hour ,$min;
2621 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2622 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2624 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2625 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2626 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2627 $date{'hour_local'} = $hour;
2628 $date{'minute_local'} = $min;
2629 $date{'tz_local'} = $tz;
2630 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2631 1900+$year, $mon+1, $mday,
2632 $hour, $min, $sec, $tz);
2633 return %date;
2636 sub parse_tag {
2637 my $tag_id = shift;
2638 my %tag;
2639 my @comment;
2641 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2642 $tag{'id'} = $tag_id;
2643 while (my $line = <$fd>) {
2644 chomp $line;
2645 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2646 $tag{'object'} = $1;
2647 } elsif ($line =~ m/^type (.+)$/) {
2648 $tag{'type'} = $1;
2649 } elsif ($line =~ m/^tag (.+)$/) {
2650 $tag{'name'} = $1;
2651 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2652 $tag{'author'} = $1;
2653 $tag{'author_epoch'} = $2;
2654 $tag{'author_tz'} = $3;
2655 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2656 $tag{'author_name'} = $1;
2657 $tag{'author_email'} = $2;
2658 } else {
2659 $tag{'author_name'} = $tag{'author'};
2661 } elsif ($line =~ m/--BEGIN/) {
2662 push @comment, $line;
2663 last;
2664 } elsif ($line eq "") {
2665 last;
2668 push @comment, <$fd>;
2669 $tag{'comment'} = \@comment;
2670 close $fd or return;
2671 if (!defined $tag{'name'}) {
2672 return
2674 return %tag
2677 sub parse_commit_text {
2678 my ($commit_text, $withparents) = @_;
2679 my @commit_lines = split '\n', $commit_text;
2680 my %co;
2682 pop @commit_lines; # Remove '\0'
2684 if (! @commit_lines) {
2685 return;
2688 my $header = shift @commit_lines;
2689 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2690 return;
2692 ($co{'id'}, my @parents) = split ' ', $header;
2693 while (my $line = shift @commit_lines) {
2694 last if $line eq "\n";
2695 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2696 $co{'tree'} = $1;
2697 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2698 push @parents, $1;
2699 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2700 $co{'author'} = to_utf8($1);
2701 $co{'author_epoch'} = $2;
2702 $co{'author_tz'} = $3;
2703 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2704 $co{'author_name'} = $1;
2705 $co{'author_email'} = $2;
2706 } else {
2707 $co{'author_name'} = $co{'author'};
2709 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2710 $co{'committer'} = to_utf8($1);
2711 $co{'committer_epoch'} = $2;
2712 $co{'committer_tz'} = $3;
2713 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2714 $co{'committer_name'} = $1;
2715 $co{'committer_email'} = $2;
2716 } else {
2717 $co{'committer_name'} = $co{'committer'};
2721 if (!defined $co{'tree'}) {
2722 return;
2724 $co{'parents'} = \@parents;
2725 $co{'parent'} = $parents[0];
2727 foreach my $title (@commit_lines) {
2728 $title =~ s/^ //;
2729 if ($title ne "") {
2730 $co{'title'} = chop_str($title, 80, 5);
2731 # remove leading stuff of merges to make the interesting part visible
2732 if (length($title) > 50) {
2733 $title =~ s/^Automatic //;
2734 $title =~ s/^merge (of|with) /Merge ... /i;
2735 if (length($title) > 50) {
2736 $title =~ s/(http|rsync):\/\///;
2738 if (length($title) > 50) {
2739 $title =~ s/(master|www|rsync)\.//;
2741 if (length($title) > 50) {
2742 $title =~ s/kernel.org:?//;
2744 if (length($title) > 50) {
2745 $title =~ s/\/pub\/scm//;
2748 $co{'title_short'} = chop_str($title, 50, 5);
2749 last;
2752 if (! defined $co{'title'} || $co{'title'} eq "") {
2753 $co{'title'} = $co{'title_short'} = '(no commit message)';
2755 # remove added spaces
2756 foreach my $line (@commit_lines) {
2757 $line =~ s/^ //;
2759 $co{'comment'} = \@commit_lines;
2761 my $age = time - $co{'committer_epoch'};
2762 $co{'age'} = $age;
2763 $co{'age_string'} = age_string($age);
2764 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2765 if ($age > 60*60*24*7*2) {
2766 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2767 $co{'age_string_age'} = $co{'age_string'};
2768 } else {
2769 $co{'age_string_date'} = $co{'age_string'};
2770 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2772 return %co;
2775 sub parse_commit {
2776 my ($commit_id) = @_;
2777 my %co;
2779 local $/ = "\0";
2781 open my $fd, "-|", git_cmd(), "rev-list",
2782 "--parents",
2783 "--header",
2784 "--max-count=1",
2785 $commit_id,
2786 "--",
2787 or die_error(500, "Open git-rev-list failed");
2788 %co = parse_commit_text(<$fd>, 1);
2789 close $fd;
2791 return %co;
2794 sub parse_commits {
2795 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2796 my @cos;
2798 $maxcount ||= 1;
2799 $skip ||= 0;
2801 local $/ = "\0";
2803 open my $fd, "-|", git_cmd(), "rev-list",
2804 "--header",
2805 @args,
2806 ("--max-count=" . $maxcount),
2807 ("--skip=" . $skip),
2808 @extra_options,
2809 $commit_id,
2810 "--",
2811 ($filename ? ($filename) : ())
2812 or die_error(500, "Open git-rev-list failed");
2813 while (my $line = <$fd>) {
2814 my %co = parse_commit_text($line);
2815 push @cos, \%co;
2817 close $fd;
2819 return wantarray ? @cos : \@cos;
2822 # parse line of git-diff-tree "raw" output
2823 sub parse_difftree_raw_line {
2824 my $line = shift;
2825 my %res;
2827 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2828 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2829 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2830 $res{'from_mode'} = $1;
2831 $res{'to_mode'} = $2;
2832 $res{'from_id'} = $3;
2833 $res{'to_id'} = $4;
2834 $res{'status'} = $5;
2835 $res{'similarity'} = $6;
2836 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2837 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2838 } else {
2839 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2842 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2843 # combined diff (for merge commit)
2844 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2845 $res{'nparents'} = length($1);
2846 $res{'from_mode'} = [ split(' ', $2) ];
2847 $res{'to_mode'} = pop @{$res{'from_mode'}};
2848 $res{'from_id'} = [ split(' ', $3) ];
2849 $res{'to_id'} = pop @{$res{'from_id'}};
2850 $res{'status'} = [ split('', $4) ];
2851 $res{'to_file'} = unquote($5);
2853 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2854 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2855 $res{'commit'} = $1;
2858 return wantarray ? %res : \%res;
2861 # wrapper: return parsed line of git-diff-tree "raw" output
2862 # (the argument might be raw line, or parsed info)
2863 sub parsed_difftree_line {
2864 my $line_or_ref = shift;
2866 if (ref($line_or_ref) eq "HASH") {
2867 # pre-parsed (or generated by hand)
2868 return $line_or_ref;
2869 } else {
2870 return parse_difftree_raw_line($line_or_ref);
2874 # parse line of git-ls-tree output
2875 sub parse_ls_tree_line {
2876 my $line = shift;
2877 my %opts = @_;
2878 my %res;
2880 if ($opts{'-l'}) {
2881 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
2882 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
2884 $res{'mode'} = $1;
2885 $res{'type'} = $2;
2886 $res{'hash'} = $3;
2887 $res{'size'} = $4;
2888 if ($opts{'-z'}) {
2889 $res{'name'} = $5;
2890 } else {
2891 $res{'name'} = unquote($5);
2893 } else {
2894 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2895 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2897 $res{'mode'} = $1;
2898 $res{'type'} = $2;
2899 $res{'hash'} = $3;
2900 if ($opts{'-z'}) {
2901 $res{'name'} = $4;
2902 } else {
2903 $res{'name'} = unquote($4);
2907 return wantarray ? %res : \%res;
2910 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2911 sub parse_from_to_diffinfo {
2912 my ($diffinfo, $from, $to, @parents) = @_;
2914 if ($diffinfo->{'nparents'}) {
2915 # combined diff
2916 $from->{'file'} = [];
2917 $from->{'href'} = [];
2918 fill_from_file_info($diffinfo, @parents)
2919 unless exists $diffinfo->{'from_file'};
2920 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2921 $from->{'file'}[$i] =
2922 defined $diffinfo->{'from_file'}[$i] ?
2923 $diffinfo->{'from_file'}[$i] :
2924 $diffinfo->{'to_file'};
2925 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2926 $from->{'href'}[$i] = href(action=>"blob",
2927 hash_base=>$parents[$i],
2928 hash=>$diffinfo->{'from_id'}[$i],
2929 file_name=>$from->{'file'}[$i]);
2930 } else {
2931 $from->{'href'}[$i] = undef;
2934 } else {
2935 # ordinary (not combined) diff
2936 $from->{'file'} = $diffinfo->{'from_file'};
2937 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2938 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2939 hash=>$diffinfo->{'from_id'},
2940 file_name=>$from->{'file'});
2941 } else {
2942 delete $from->{'href'};
2946 $to->{'file'} = $diffinfo->{'to_file'};
2947 if (!is_deleted($diffinfo)) { # file exists in result
2948 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2949 hash=>$diffinfo->{'to_id'},
2950 file_name=>$to->{'file'});
2951 } else {
2952 delete $to->{'href'};
2956 ## ......................................................................
2957 ## parse to array of hashes functions
2959 sub git_get_heads_list {
2960 my $limit = shift;
2961 my @headslist;
2963 open my $fd, '-|', git_cmd(), 'for-each-ref',
2964 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2965 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2966 'refs/heads'
2967 or return;
2968 while (my $line = <$fd>) {
2969 my %ref_item;
2971 chomp $line;
2972 my ($refinfo, $committerinfo) = split(/\0/, $line);
2973 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2974 my ($committer, $epoch, $tz) =
2975 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2976 $ref_item{'fullname'} = $name;
2977 $name =~ s!^refs/heads/!!;
2979 $ref_item{'name'} = $name;
2980 $ref_item{'id'} = $hash;
2981 $ref_item{'title'} = $title || '(no commit message)';
2982 $ref_item{'epoch'} = $epoch;
2983 if ($epoch) {
2984 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2985 } else {
2986 $ref_item{'age'} = "unknown";
2989 push @headslist, \%ref_item;
2991 close $fd;
2993 return wantarray ? @headslist : \@headslist;
2996 sub git_get_tags_list {
2997 my $limit = shift;
2998 my @tagslist;
3000 open my $fd, '-|', git_cmd(), 'for-each-ref',
3001 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3002 '--format=%(objectname) %(objecttype) %(refname) '.
3003 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3004 'refs/tags'
3005 or return;
3006 while (my $line = <$fd>) {
3007 my %ref_item;
3009 chomp $line;
3010 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3011 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3012 my ($creator, $epoch, $tz) =
3013 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3014 $ref_item{'fullname'} = $name;
3015 $name =~ s!^refs/tags/!!;
3017 $ref_item{'type'} = $type;
3018 $ref_item{'id'} = $id;
3019 $ref_item{'name'} = $name;
3020 if ($type eq "tag") {
3021 $ref_item{'subject'} = $title;
3022 $ref_item{'reftype'} = $reftype;
3023 $ref_item{'refid'} = $refid;
3024 } else {
3025 $ref_item{'reftype'} = $type;
3026 $ref_item{'refid'} = $id;
3029 if ($type eq "tag" || $type eq "commit") {
3030 $ref_item{'epoch'} = $epoch;
3031 if ($epoch) {
3032 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3033 } else {
3034 $ref_item{'age'} = "unknown";
3038 push @tagslist, \%ref_item;
3040 close $fd;
3042 return wantarray ? @tagslist : \@tagslist;
3045 ## ----------------------------------------------------------------------
3046 ## filesystem-related functions
3048 sub get_file_owner {
3049 my $path = shift;
3051 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3052 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3053 if (!defined $gcos) {
3054 return undef;
3056 my $owner = $gcos;
3057 $owner =~ s/[,;].*$//;
3058 return to_utf8($owner);
3061 # assume that file exists
3062 sub insert_file {
3063 my $filename = shift;
3065 open my $fd, '<', $filename;
3066 print map { to_utf8($_) } <$fd>;
3067 close $fd;
3070 ## ......................................................................
3071 ## mimetype related functions
3073 sub mimetype_guess_file {
3074 my $filename = shift;
3075 my $mimemap = shift;
3076 -r $mimemap or return undef;
3078 my %mimemap;
3079 open(my $mh, '<', $mimemap) or return undef;
3080 while (<$mh>) {
3081 next if m/^#/; # skip comments
3082 my ($mimetype, $exts) = split(/\t+/);
3083 if (defined $exts) {
3084 my @exts = split(/\s+/, $exts);
3085 foreach my $ext (@exts) {
3086 $mimemap{$ext} = $mimetype;
3090 close($mh);
3092 $filename =~ /\.([^.]*)$/;
3093 return $mimemap{$1};
3096 sub mimetype_guess {
3097 my $filename = shift;
3098 my $mime;
3099 $filename =~ /\./ or return undef;
3101 if ($mimetypes_file) {
3102 my $file = $mimetypes_file;
3103 if ($file !~ m!^/!) { # if it is relative path
3104 # it is relative to project
3105 $file = "$projectroot/$project/$file";
3107 $mime = mimetype_guess_file($filename, $file);
3109 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3110 return $mime;
3113 sub blob_mimetype {
3114 my $fd = shift;
3115 my $filename = shift;
3117 if ($filename) {
3118 my $mime = mimetype_guess($filename);
3119 $mime and return $mime;
3122 # just in case
3123 return $default_blob_plain_mimetype unless $fd;
3125 if (-T $fd) {
3126 return 'text/plain';
3127 } elsif (! $filename) {
3128 return 'application/octet-stream';
3129 } elsif ($filename =~ m/\.png$/i) {
3130 return 'image/png';
3131 } elsif ($filename =~ m/\.gif$/i) {
3132 return 'image/gif';
3133 } elsif ($filename =~ m/\.jpe?g$/i) {
3134 return 'image/jpeg';
3135 } else {
3136 return 'application/octet-stream';
3140 sub blob_contenttype {
3141 my ($fd, $file_name, $type) = @_;
3143 $type ||= blob_mimetype($fd, $file_name);
3144 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3145 $type .= "; charset=$default_text_plain_charset";
3148 return $type;
3151 ## ======================================================================
3152 ## functions printing HTML: header, footer, error page
3154 sub git_header_html {
3155 my $status = shift || "200 OK";
3156 my $expires = shift;
3158 my $title = "$site_name";
3159 if (defined $project) {
3160 $title .= " - " . to_utf8($project);
3161 if (defined $action) {
3162 $title .= "/$action";
3163 if (defined $file_name) {
3164 $title .= " - " . esc_path($file_name);
3165 if ($action eq "tree" && $file_name !~ m|/$|) {
3166 $title .= "/";
3171 # We do not ever emit application/xhtml+xml since that gives us
3172 # no benefits and it makes many browsers (e.g. Firefox) exceedingly
3173 # strict, which is troublesome for example when showing user-supplied
3174 # README.html files.
3175 my $content_type = 'text/html';
3176 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3177 -status=> $status, -expires => $expires);
3178 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3179 print <<EOF;
3180 <?xml version="1.0" encoding="utf-8"?>
3181 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3182 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3183 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3184 <!-- git core binaries version $git_version -->
3185 <head>
3186 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3187 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3188 <meta name="robots" content="index, nofollow"/>
3189 <title>$title</title>
3190 <script type="text/javascript">/* <![CDATA[ */
3191 function fixBlameLinks() {
3192 var allLinks = document.getElementsByTagName("a");
3193 for (var i = 0; i < allLinks.length; i++) {
3194 var link = allLinks.item(i);
3195 if (link.className == 'blamelink')
3196 link.href = link.href.replace("/blame/", "/blame_incremental/");
3199 /* ]]> */</script>
3201 # the stylesheet, favicon etc urls won't work correctly with path_info
3202 # unless we set the appropriate base URL
3203 if ($ENV{'PATH_INFO'}) {
3204 print "<base href=\"".esc_url($base_url)."\" />\n";
3206 # print out each stylesheet that exist, providing backwards capability
3207 # for those people who defined $stylesheet in a config file
3208 if (defined $stylesheet) {
3209 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3210 } else {
3211 foreach my $stylesheet (@stylesheets) {
3212 next unless $stylesheet;
3213 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3216 if (defined $project) {
3217 my %href_params = get_feed_info();
3218 if (!exists $href_params{'-title'}) {
3219 $href_params{'-title'} = 'log';
3222 foreach my $format qw(RSS Atom) {
3223 my $type = lc($format);
3224 my %link_attr = (
3225 '-rel' => 'alternate',
3226 '-title' => "$project - $href_params{'-title'} - $format feed",
3227 '-type' => "application/$type+xml"
3230 $href_params{'action'} = $type;
3231 $link_attr{'-href'} = href(%href_params);
3232 print "<link ".
3233 "rel=\"$link_attr{'-rel'}\" ".
3234 "title=\"$link_attr{'-title'}\" ".
3235 "href=\"$link_attr{'-href'}\" ".
3236 "type=\"$link_attr{'-type'}\" ".
3237 "/>\n";
3239 $href_params{'extra_options'} = '--no-merges';
3240 $link_attr{'-href'} = href(%href_params);
3241 $link_attr{'-title'} .= ' (no merges)';
3242 print "<link ".
3243 "rel=\"$link_attr{'-rel'}\" ".
3244 "title=\"$link_attr{'-title'}\" ".
3245 "href=\"$link_attr{'-href'}\" ".
3246 "type=\"$link_attr{'-type'}\" ".
3247 "/>\n";
3250 } else {
3251 printf('<link rel="alternate" title="%s projects list" '.
3252 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3253 $site_name, href(project=>undef, action=>"project_index"));
3254 printf('<link rel="alternate" title="%s projects feeds" '.
3255 'href="%s" type="text/x-opml" />'."\n",
3256 $site_name, href(project=>undef, action=>"opml"));
3258 if (defined $favicon) {
3259 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
3262 if (defined $gitwebjs) {
3263 print qq(<script src="$gitwebjs" type="text/javascript"></script>\n);
3266 print "</head>\n";
3267 if (gitweb_check_feature('blame_incremental')) {
3268 print "<body onload=\"fixBlameLinks();\">\n";
3269 } else {
3270 print "<body>\n";
3273 if (-f $site_header) {
3274 insert_file($site_header);
3277 print "<div class=\"page_header\">\n" .
3278 $cgi->a({-href => esc_url($logo_url),
3279 -title => $logo_label},
3280 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
3281 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3282 if (defined $project) {
3283 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3284 if (defined $action) {
3285 print " / $action";
3287 print "\n";
3289 print "</div>\n";
3291 my $have_search = gitweb_check_feature('search');
3292 if (defined $project && $have_search) {
3293 if (!defined $searchtext) {
3294 $searchtext = "";
3296 my $search_hash;
3297 if (defined $hash_base) {
3298 $search_hash = $hash_base;
3299 } elsif (defined $hash) {
3300 $search_hash = $hash;
3301 } else {
3302 $search_hash = "HEAD";
3304 my $action = $my_uri;
3305 my $use_pathinfo = gitweb_check_feature('pathinfo');
3306 if ($use_pathinfo) {
3307 $action .= "/".esc_url($project);
3309 print $cgi->startform(-method => "get", -action => $action) .
3310 "<div class=\"search\">\n" .
3311 (!$use_pathinfo &&
3312 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3313 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3314 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3315 $cgi->popup_menu(-name => 'st', -default => 'commit',
3316 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3317 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3318 " search:\n",
3319 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3320 "<span title=\"Extended regular expression\">" .
3321 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3322 -checked => $search_use_regexp) .
3323 "</span>" .
3324 "</div>" .
3325 $cgi->end_form() . "\n";
3329 sub git_footer_html {
3330 my $feed_class = 'rss_logo';
3332 print "<div class=\"page_footer\">\n";
3333 if (defined $project) {
3334 my $descr = git_get_project_description($project);
3335 if (defined $descr) {
3336 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3339 my %href_params = get_feed_info();
3340 if (!%href_params) {
3341 $feed_class .= ' generic';
3343 $href_params{'-title'} ||= 'log';
3345 foreach my $format qw(RSS Atom) {
3346 $href_params{'action'} = lc($format);
3347 print $cgi->a({-href => href(%href_params),
3348 -title => "$href_params{'-title'} $format feed",
3349 -class => $feed_class}, $format)."\n";
3352 } else {
3353 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3354 -class => $feed_class}, "OPML") . " ";
3355 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3356 -class => $feed_class}, "TXT") . "\n";
3358 print "</div>\n"; # class="page_footer"
3360 if (-f $site_footer) {
3361 insert_file($site_footer);
3364 print "</body>\n" .
3365 "</html>";
3368 # die_error(<http_status_code>, <error_message>)
3369 # Example: die_error(404, 'Hash not found')
3370 # By convention, use the following status codes (as defined in RFC 2616):
3371 # 400: Invalid or missing CGI parameters, or
3372 # requested object exists but has wrong type.
3373 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3374 # this server or project.
3375 # 404: Requested object/revision/project doesn't exist.
3376 # 500: The server isn't configured properly, or
3377 # an internal error occurred (e.g. failed assertions caused by bugs), or
3378 # an unknown error occurred (e.g. the git binary died unexpectedly).
3379 sub die_error {
3380 my $status = shift || 500;
3381 my $error = shift || "Internal server error";
3383 my %http_responses = (400 => '400 Bad Request',
3384 403 => '403 Forbidden',
3385 404 => '404 Not Found',
3386 500 => '500 Internal Server Error');
3387 git_header_html($http_responses{$status});
3388 print <<EOF;
3389 <div class="page_body">
3390 <br /><br />
3391 $status - $error
3392 <br />
3393 </div>
3395 git_footer_html();
3396 exit;
3399 ## ----------------------------------------------------------------------
3400 ## functions printing or outputting HTML: navigation
3402 sub git_print_page_nav {
3403 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3404 $extra = '' if !defined $extra; # pager or formats
3406 my @navs = qw(summary log commit commitdiff tree);
3407 if ($suppress) {
3408 @navs = grep { $_ ne $suppress } @navs;
3411 my %arg = map { $_ => {action=>$_} } @navs;
3412 if (defined $head) {
3413 for (qw(commit commitdiff)) {
3414 $arg{$_}{'hash'} = $head;
3416 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3417 $arg{'log'}{'hash'} = $head;
3421 $arg{'log'}{'action'} = 'shortlog';
3422 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3423 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3425 my @actions = gitweb_get_feature('actions');
3426 my %repl = (
3427 '%' => '%',
3428 'n' => $project, # project name
3429 'f' => $git_dir, # project path within filesystem
3430 'h' => $treehead || '', # current hash ('h' parameter)
3431 'b' => $treebase || '', # hash base ('hb' parameter)
3433 while (@actions) {
3434 my ($label, $link, $pos) = splice(@actions,0,3);
3435 # insert
3436 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3437 # munch munch
3438 $link =~ s/%([%nfhb])/$repl{$1}/g;
3439 $arg{$label}{'_href'} = $link;
3442 print "<div class=\"page_nav\">\n" .
3443 (join " | ",
3444 map { $_ eq $current ?
3445 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3446 } @navs);
3447 print "<br/>\n$extra<br/>\n" .
3448 "</div>\n";
3451 sub format_paging_nav {
3452 my ($action, $hash, $head, $page, $has_next_link) = @_;
3453 my $paging_nav;
3456 if ($hash ne $head || $page) {
3457 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
3458 } else {
3459 $paging_nav .= "HEAD";
3462 if ($page > 0) {
3463 $paging_nav .= " &sdot; " .
3464 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3465 -accesskey => "p", -title => "Alt-p"}, "prev");
3466 } else {
3467 $paging_nav .= " &sdot; prev";
3470 if ($has_next_link) {
3471 $paging_nav .= " &sdot; " .
3472 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3473 -accesskey => "n", -title => "Alt-n"}, "next");
3474 } else {
3475 $paging_nav .= " &sdot; next";
3478 return $paging_nav;
3481 sub format_log_nav {
3482 my ($action, $hash, $head, $page, $has_next_link) = @_;
3483 my $paging_nav;
3485 if ($action eq 'shortlog') {
3486 $paging_nav .= 'shortlog';
3487 } else {
3488 $paging_nav .= $cgi->a({-href => href(action=>'shortlog', -replay=>1)}, 'shortlog');
3490 $paging_nav .= ' | ';
3491 if ($action eq 'log') {
3492 $paging_nav .= 'fulllog';
3493 } else {
3494 $paging_nav .= $cgi->a({-href => href(action=>'log', -replay=>1)}, 'fulllog');
3497 $paging_nav .= " | " . format_paging_nav($action, $hash, $head, $page, $has_next_link);
3498 return $paging_nav;
3501 ## ......................................................................
3502 ## functions printing or outputting HTML: div
3504 sub git_print_header_div {
3505 my ($action, $title, $hash, $hash_base) = @_;
3506 my %args = ();
3508 $args{'action'} = $action;
3509 $args{'hash'} = $hash if $hash;
3510 $args{'hash_base'} = $hash_base if $hash_base;
3512 print "<div class=\"header\">\n" .
3513 $cgi->a({-href => href(%args), -class => "title"},
3514 $title ? $title : $action) .
3515 "\n</div>\n";
3518 sub print_local_time {
3519 my %date = @_;
3520 if ($date{'hour_local'} < 6) {
3521 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3522 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3523 } else {
3524 printf(" (%02d:%02d %s)",
3525 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3529 # Outputs the author name and date in long form
3530 sub git_print_authorship {
3531 my $co = shift;
3532 my %opts = @_;
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)) .
3539 " [$ad{'rfc2822'}";
3540 print_local_time(%ad) if ($opts{-localtime});
3541 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3542 . "</$tag>\n";
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 {
3551 my $co = shift;
3552 # too bad we can't use @people = @_ || ('author', 'committer')
3553 my @people = @_;
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') .
3564 "</td></tr>\n" .
3565 "<tr>" .
3566 "<td></td><td> $wd{'rfc2822'}";
3567 print_local_time(%wd);
3568 print "</td>" .
3569 "</tr>\n";
3573 sub git_print_page_path {
3574 my $name = shift;
3575 my $type = shift;
3576 my $hb = shift;
3579 print "<div class=\"page_path\">";
3580 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3581 -title => 'tree root'}, to_utf8("[$project]"));
3582 print " / ";
3583 if (defined $name) {
3584 my @dirname = split '/', $name;
3585 my $basename = pop @dirname;
3586 my $fullname = '';
3588 foreach my $dir (@dirname) {
3589 $fullname .= ($fullname ? '/' : '') . $dir;
3590 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3591 hash_base=>$hb),
3592 -title => $fullname}, esc_path($dir));
3593 print " / ";
3595 if (defined $type && $type eq 'blob') {
3596 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3597 hash_base=>$hb),
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,
3601 hash_base=>$hb),
3602 -title => $name}, esc_path($basename));
3603 print " / ";
3604 } else {
3605 print esc_path($basename);
3608 print "<br/></div>\n";
3611 sub git_print_log {
3612 my $log = shift;
3613 my %opts = @_;
3615 if ($opts{'-remove_title'}) {
3616 # remove title, i.e. first line of log
3617 shift @$log;
3619 # remove leading empty lines
3620 while (defined $log->[0] && $log->[0] eq "") {
3621 shift @$log;
3624 # print log
3625 my $signoff = 0;
3626 my $empty = 0;
3627 foreach my $line (@$log) {
3628 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3629 $signoff = 1;
3630 $empty = 0;
3631 if (! $opts{'-remove_signoff'}) {
3632 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3633 next;
3634 } else {
3635 # remove signoff lines
3636 next;
3638 } else {
3639 $signoff = 0;
3642 # print only one empty line
3643 # do not print empty line after signoff
3644 if ($line eq "") {
3645 next if ($empty || $signoff);
3646 $empty = 1;
3647 } else {
3648 $empty = 0;
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 {
3662 my $hash = shift;
3663 my $link_target;
3665 # read link
3666 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3667 or return;
3669 local $/ = undef;
3670 $link_target = <$fd>;
3672 close $fd
3673 or return;
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)
3688 my $path;
3689 if ($basedir) {
3690 $path = $basedir . '/' . $link_target;
3691 } else {
3692 # we are in top (root) tree (dir)
3693 $path = $link_target;
3696 # remove //, /./, and /../
3697 my @path_parts;
3698 foreach my $part (split('/', $path)) {
3699 # discard '.' and ''
3700 next if (!$part || $part eq '.');
3701 # handle '..'
3702 if ($part eq '..') {
3703 if (@path_parts) {
3704 pop @path_parts;
3705 } else {
3706 # link leads outside repository (outside top dir)
3707 return;
3709 } else {
3710 push @path_parts, $part;
3713 $path = join('/', @path_parts);
3715 return $path;
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) = @_;
3722 my %base_key = ();
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'});
3740 if ($link_target) {
3741 my $norm_target = normalize_link_target($link_target, $basedir);
3742 if (defined $norm_target) {
3743 print " -> " .
3744 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3745 file_name=>$norm_target),
3746 -title => $norm_target}, esc_path($link_target));
3747 } else {
3748 print " -> " . esc_path($link_target);
3752 print "</td>\n";
3753 print "<td class=\"link\">";
3754 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3755 file_name=>"$basedir$t->{'name'}", %base_key)},
3756 "blob");
3757 if ($have_blame) {
3758 print " | " .
3759 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3760 file_name=>"$basedir$t->{'name'}", %base_key), -class => "blamelink"},
3761 "blame");
3763 if (defined $hash_base) {
3764 print " | " .
3765 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3766 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3767 "history");
3769 print " | " .
3770 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3771 file_name=>"$basedir$t->{'name'}")},
3772 "raw");
3773 print "</td>\n";
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'}",
3779 %base_key)},
3780 esc_path($t->{'name'}));
3781 print "</td>\n";
3782 print "<td class=\"link\">";
3783 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3784 file_name=>"$basedir$t->{'name'}",
3785 %base_key)},
3786 "tree");
3787 if (defined $hash_base) {
3788 print " | " .
3789 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3790 file_name=>"$basedir$t->{'name'}")},
3791 "history");
3793 print "</td>\n";
3794 } else {
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'}) .
3799 "</td>\n";
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'}")},
3805 "history");
3807 print "</td>\n";
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]);
3828 return $diff;
3831 # is current raw difftree line of file deletion
3832 sub is_deleted {
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");
3858 print "</div>\n";
3860 print "<table class=\"" .
3861 (@parents > 1 ? "combined " : "") .
3862 "diff_tree\">\n";
3864 # header only for combined diff in 'commitdiff' view
3865 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3866 if ($has_header) {
3867 # table header
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];
3872 print "<th>" .
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)},
3877 $i+1) .
3878 "&nbsp;</th>\n";
3880 print "</tr></thead>\n<tbody>\n";
3883 my $alternate = 1;
3884 my $patchno = 0;
3885 foreach my $line (@{$difftree}) {
3886 my $diff = parsed_difftree_line($line);
3888 if ($alternate) {
3889 print "<tr class=\"dark\">\n";
3890 } else {
3891 print "<tr class=\"light\">\n";
3893 $alternate ^= 1;
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
3902 print "<td>" .
3903 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3904 file_name=>$diff->{'to_file'},
3905 hash_base=>$hash),
3906 -class => "list"}, esc_path($diff->{'to_file'})) .
3907 "</td>\n";
3908 } else {
3909 print "<td>" .
3910 esc_path($diff->{'to_file'}) .
3911 "</td>\n";
3914 if ($action eq 'commitdiff') {
3915 # link to patch
3916 $patchno++;
3917 print "<td class=\"link\">" .
3918 $cgi->a({-href => "#patch$patchno"}, "patch") .
3919 " | " .
3920 "</td>\n";
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",
3939 hash_base=>$hash,
3940 hash=>$from_hash,
3941 file_name=>$from_path)},
3942 "blob" . ($i+1)) .
3943 " | </td>\n";
3944 } else {
3945 if ($diff->{'to_id'} eq $from_hash) {
3946 print "<td class=\"link nochange\">";
3947 } else {
3948 print "<td class=\"link\">";
3950 print $cgi->a({-href => href(action=>"blobdiff",
3951 hash=>$diff->{'to_id'},
3952 hash_parent=>$from_hash,
3953 hash_base=>$hash,
3954 hash_parent_base=>$hash_parent,
3955 file_name=>$diff->{'to_file'},
3956 file_parent=>$from_path)},
3957 "diff" . ($i+1)) .
3958 " | </td>\n";
3962 print "<td class=\"link\">";
3963 if ($not_deleted) {
3964 print $cgi->a({-href => href(action=>"blob",
3965 hash=>$diff->{'to_id'},
3966 file_name=>$diff->{'to_file'},
3967 hash_base=>$hash)},
3968 "blob");
3969 print " | " if ($has_history);
3971 if ($has_history) {
3972 print $cgi->a({-href => href(action=>"history",
3973 file_name=>$diff->{'to_file'},
3974 hash_base=>$hash)},
3975 "history");
3977 print "</td>\n";
3979 print "</tr>\n";
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>";
4005 print "<td>";
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'}));
4009 print "</td>\n";
4010 print "<td>$mode_chng</td>\n";
4011 print "<td class=\"link\">";
4012 if ($action eq 'commitdiff') {
4013 # link to patch
4014 $patchno++;
4015 print $cgi->a({-href => "#patch$patchno"}, "patch");
4016 print " | ";
4018 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4019 hash_base=>$hash, file_name=>$diff->{'file'})},
4020 "blob");
4021 print "</td>\n";
4023 } elsif ($diff->{'status'} eq "D") { # deleted
4024 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4025 print "<td>";
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'}));
4029 print "</td>\n";
4030 print "<td>$mode_chng</td>\n";
4031 print "<td class=\"link\">";
4032 if ($action eq 'commitdiff') {
4033 # link to patch
4034 $patchno++;
4035 print $cgi->a({-href => "#patch$patchno"}, "patch");
4036 print " | ";
4038 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4039 hash_base=>$parent, file_name=>$diff->{'file'})},
4040 "blob") . " | ";
4041 if ($have_blame) {
4042 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4043 file_name=>$diff->{'file'})},
4044 "blame") . " | ";
4046 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4047 file_name=>$diff->{'file'})},
4048 "history");
4049 print "</td>\n";
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";
4067 print "<td>";
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'}));
4071 print "</td>\n";
4072 print "<td>$mode_chnge</td>\n";
4073 print "<td class=\"link\">";
4074 if ($action eq 'commitdiff') {
4075 # link to patch
4076 $patchno++;
4077 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4078 " | ";
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'})},
4085 "diff") .
4086 " | ";
4088 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4089 hash_base=>$hash, file_name=>$diff->{'file'})},
4090 "blob") . " | ";
4091 if ($have_blame) {
4092 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4093 file_name=>$diff->{'file'})},
4094 "blame") . " | ";
4096 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4097 file_name=>$diff->{'file'})},
4098 "history");
4099 print "</td>\n";
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'}};
4104 my $mode_chng = "";
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);
4109 print "<td>" .
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') {
4120 # link to patch
4121 $patchno++;
4122 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4123 " | ";
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'})},
4130 "diff") .
4131 " | ";
4133 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4134 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4135 "blob") . " | ";
4136 if ($have_blame) {
4137 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4138 file_name=>$diff->{'to_file'})},
4139 "blame") . " | ";
4141 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4142 file_name=>$diff->{'to_file'})},
4143 "history");
4144 print "</td>\n";
4146 } # we should not encounter Unmerged (U) or Unknown (X) status
4147 print "</tr>\n";
4149 print "</tbody>" if $has_header;
4150 print "</table>\n";
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);
4158 my $patch_idx = 0;
4159 my $patch_number = 0;
4160 my $patch_line;
4161 my $diffinfo;
4162 my $to_name;
4163 my (%from, %to);
4165 print "<div class=\"patchset\">\n";
4167 # skip to first patch
4168 while ($patch_line = <$fd>) {
4169 chomp $patch_line;
4171 last if ($patch_line =~ m/^diff /);
4174 PATCH:
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);
4185 } else {
4186 $to_name = undef;
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";
4194 } else {
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;
4203 if ($is_combined) {
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"
4209 $patch_idx++;
4210 $patch_number++;
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";
4225 # git diff header
4226 #assert($patch_line =~ m/^diff /) if DEBUG;
4227 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4228 $patch_number++;
4229 # print "git diff" header
4230 print format_git_diff_header_line($patch_line, $diffinfo,
4231 \%from, \%to);
4233 # print extended diff header
4234 print "<div class=\"diff extended_header\">\n";
4235 EXTENDED_HEADER:
4236 while ($patch_line = <$fd>) {
4237 chomp $patch_line;
4239 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4241 print format_extended_diff_header_line($patch_line, $diffinfo,
4242 \%from, \%to);
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"
4249 last 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>;
4256 chomp $patch_line;
4257 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4259 print format_diff_from_to_header($last_patch_line, $patch_line,
4260 $diffinfo, \%from, \%to,
4261 @hash_parents);
4263 # the patch itself
4264 LINE:
4265 while ($patch_line = <$fd>) {
4266 chomp $patch_line;
4268 next PATCH if ($patch_line =~ m/^diff /);
4270 print format_diff_line($patch_line, \%from, \%to);
4273 } continue {
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;
4281 ++$patch_idx) {
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"
4290 $patch_number++;
4293 if ($patch_number == 0) {
4294 if (@hash_parents > 1) {
4295 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4296 } else {
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, $show_ctags) = @_;
4311 my @projects;
4313 PROJECT:
4314 foreach my $pr (@$projlist) {
4315 my (@activity) = git_get_last_activity($pr->{'path'});
4316 unless (@activity) {
4317 next PROJECT;
4319 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4320 if (!defined $pr->{'descr'}) {
4321 my $descr = git_get_project_description($pr->{'path'}) || "";
4322 $descr = to_utf8($descr);
4323 $pr->{'descr_long'} = $descr;
4324 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4326 if (!defined $pr->{'owner'}) {
4327 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4329 if ($check_forks) {
4330 my $pname = $pr->{'path'};
4331 if (($pname =~ s/\.git$//) &&
4332 ($pname !~ /\/$/) &&
4333 (-d "$projectroot/$pname")) {
4334 $pr->{'forks'} = "-d $projectroot/$pname";
4335 } else {
4336 $pr->{'forks'} = 0;
4339 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4340 push @projects, $pr;
4343 return @projects;
4346 sub cached_project_list_info {
4347 my ($projlist, $check_forks, $show_ctags, $cache_lifetime) = @_;
4349 use File::stat;
4350 use POSIX qw(:fcntl_h);
4351 use Storable qw(store_fd retrieve);
4353 my $cache_file = "$cache_dir/$projlist_cache_name";
4355 my @projects;
4356 my $stale = 0;
4357 my $now = time();
4358 my $cache_mtime;
4359 if ($cache_lifetime && -f $cache_file) {
4360 $cache_mtime = stat($cache_file)->mtime;
4362 if (defined $cache_mtime && # caching is on and $cache_file exists
4363 $cache_mtime + $cache_lifetime*60 > $now &&
4364 (my $dump = retrieve($cache_file))) {
4365 # Cache hit.
4366 $stale = $now - $cache_mtime;
4367 @projects = @$dump;
4369 } else { # Cache miss.
4370 if (defined $cache_mtime) {
4371 # Postpone timeout by two minutes so that we get
4372 # enough time to do our job, or to be more exact
4373 # make cache expire after two minutes from now.
4374 my $time = $now - $cache_lifetime*60 + 120;
4375 utime $time, $time, $cache_file;
4377 @projects = fill_project_list_info($projlist, $check_forks, $show_ctags);
4378 if ($cache_lifetime &&
4379 (-d $cache_dir || mkdir($cache_dir, $cache_grpshared ? 0770 : 0700)) &&
4380 sysopen(my $fd, "$cache_file.lock", O_WRONLY|O_CREAT|O_EXCL, $cache_grpshared ? 0660 : 0600)) {
4381 store_fd(\@projects, $fd);
4382 close $fd;
4383 rename "$cache_file.lock", $cache_file;
4387 if ($cache_lifetime && $stale > 0) {
4388 print "<div class=\"stale_info\">Cached version (${stale}s old)</div>\n";
4391 return @projects;
4394 # print 'sort by' <th> element, generating 'sort by $name' replay link
4395 # if that order is not selected
4396 sub print_sort_th {
4397 my ($name, $order, $header) = @_;
4398 $header ||= ucfirst($name);
4400 if ($order eq $name) {
4401 print "<th>$header</th>\n";
4402 } else {
4403 print "<th>" .
4404 $cgi->a({-href => href(-replay=>1, order=>$name),
4405 -class => "header"}, $header) .
4406 "</th>\n";
4410 sub git_project_list_ctags {
4411 my ($projects, $action) = @_;
4412 $action ||= 'project_list';
4414 my %ctags;
4415 foreach my $p (@$projects) {
4416 foreach my $ct (keys %{$p->{'ctags'}}) {
4417 $ctags{$ct} += $p->{'ctags'}->{$ct};
4420 my $cloud = git_populate_project_tagcloud(\%ctags, $action);
4421 print git_show_project_tagcloud($cloud, 64, $action);
4424 sub git_project_list_body {
4425 # actually uses global variable $project
4426 my ($projlist, $order, $from, $to, $extra, $no_header, $ctags_action, $cache_lifetime) = @_;
4428 my $check_forks = gitweb_check_feature('forks');
4429 my $show_ctags = gitweb_check_feature('ctags');
4430 my @projects = cached_project_list_info($projlist, $check_forks, $show_ctags, $cache_lifetime);
4432 $order ||= $default_projects_order;
4433 $from = 0 unless defined $from;
4434 $to = $#projects if (!defined $to || $#projects < $to);
4436 my %order_info = (
4437 project => { key => 'path', type => 'str' },
4438 descr => { key => 'descr_long', type => 'str' },
4439 owner => { key => 'owner', type => 'str' },
4440 age => { key => 'age', type => 'num' }
4442 my $oi = $order_info{$order};
4443 if ($oi->{'type'} eq 'str') {
4444 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4445 } else {
4446 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4449 if ($show_ctags) {
4450 git_project_list_ctags(\@projects, $ctags_action);
4453 print "<table class=\"project_list\">\n";
4454 unless ($no_header) {
4455 print "<tr>\n";
4456 if ($check_forks) {
4457 print "<th></th>\n";
4459 print_sort_th('project', $order, 'Project');
4460 print_sort_th('descr', $order, 'Description');
4461 print_sort_th('owner', $order, 'Owner');
4462 print_sort_th('age', $order, 'Last Change');
4463 print "<th></th>\n" . # for links
4464 "</tr>\n";
4466 my $alternate = 1;
4467 my $tagfilter = $input_params{'ctag_filter'};
4468 for (my $i = $from; $i <= $to; $i++) {
4469 my $pr = $projects[$i];
4471 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4472 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4473 and not $pr->{'descr_long'} =~ /$searchtext/;
4474 # Weed out forks or non-matching entries of search
4475 if ($check_forks) {
4476 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4477 $forkbase="^$forkbase" if $forkbase;
4478 next if not $searchtext and not $tagfilter and $show_ctags
4479 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4482 if ($alternate) {
4483 print "<tr class=\"dark\">\n";
4484 } else {
4485 print "<tr class=\"light\">\n";
4487 $alternate ^= 1;
4488 if ($check_forks) {
4489 print "<td>";
4490 if ($pr->{'forks'}) {
4491 print "<!-- $pr->{'forks'} -->\n";
4492 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4494 print "</td>\n";
4496 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4497 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4498 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4499 -class => "list", -title => $pr->{'descr_long'}},
4500 esc_html($pr->{'descr'})) . "</td>\n" .
4501 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4502 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4503 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4504 "<td class=\"link\">" .
4505 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4506 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "log") . " | " .
4507 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4508 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4509 "</td>\n" .
4510 "</tr>\n";
4512 if (defined $extra) {
4513 print "<tr>\n";
4514 if ($check_forks) {
4515 print "<td></td>\n";
4517 print "<td colspan=\"5\">$extra</td>\n" .
4518 "</tr>\n";
4520 print "</table>\n";
4523 sub git_project_search_form {
4524 print $cgi->startform(-method => "get") .
4525 $cgi->hidden({-name=>"a", -value=>"project_list"}) . "\n" .
4526 "<p class=\"projsearch\">Search:\n" .
4527 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4528 "</p>" .
4529 $cgi->end_form() . "\n";
4532 sub git_project_list_all {
4533 my $order = $input_params{'order'};
4534 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4535 die_error(400, "Unknown order parameter");
4538 my @list = git_get_projects_list();
4539 if (!@list) {
4540 die_error(404, "No projects found");
4543 git_project_list_body(\@list, $order, undef, undef, undef, undef, undef, $projlist_cache_lifetime);
4546 sub git_shortlog_body {
4547 # uses global variable $project
4548 my ($commitlist, $from, $to, $refs, $extra) = @_;
4550 $from = 0 unless defined $from;
4551 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4553 print "<table class=\"shortlog\">\n";
4554 my $alternate = 1;
4555 for (my $i = $from; $i <= $to; $i++) {
4556 my %co = %{$commitlist->[$i]};
4557 my $commit = $co{'id'};
4558 my $ref = format_ref_marker($refs, $commit);
4559 if ($alternate) {
4560 print "<tr class=\"dark\">\n";
4561 } else {
4562 print "<tr class=\"light\">\n";
4564 $alternate ^= 1;
4565 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4566 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4567 format_author_html('td', \%co, 10) . "<td>";
4568 print format_subject_html($co{'title'}, $co{'title_short'},
4569 href(action=>"commit", hash=>$commit), $ref);
4570 print "</td>\n" .
4571 "<td class=\"link\">" .
4572 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4573 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4574 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4575 my $snapshot_links = format_snapshot_links($commit);
4576 if (defined $snapshot_links) {
4577 print " | " . $snapshot_links;
4579 print "</td>\n" .
4580 "</tr>\n";
4582 if (defined $extra) {
4583 print "<tr>\n" .
4584 "<td colspan=\"4\">$extra</td>\n" .
4585 "</tr>\n";
4587 print "</table>\n";
4590 sub git_history_body {
4591 # Warning: assumes constant type (blob or tree) during history
4592 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
4594 $from = 0 unless defined $from;
4595 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4597 print "<table class=\"history\">\n";
4598 my $alternate = 1;
4599 for (my $i = $from; $i <= $to; $i++) {
4600 my %co = %{$commitlist->[$i]};
4601 if (!%co) {
4602 next;
4604 my $commit = $co{'id'};
4606 my $ref = format_ref_marker($refs, $commit);
4608 if ($alternate) {
4609 print "<tr class=\"dark\">\n";
4610 } else {
4611 print "<tr class=\"light\">\n";
4613 $alternate ^= 1;
4614 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4615 # shortlog: format_author_html('td', \%co, 10)
4616 format_author_html('td', \%co, 15, 3) . "<td>";
4617 # originally git_history used chop_str($co{'title'}, 50)
4618 print format_subject_html($co{'title'}, $co{'title_short'},
4619 href(action=>"commit", hash=>$commit), $ref);
4620 print "</td>\n" .
4621 "<td class=\"link\">" .
4622 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4623 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4625 if ($ftype eq 'blob') {
4626 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
4627 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4628 if (defined $blob_current && defined $blob_parent &&
4629 $blob_current ne $blob_parent) {
4630 print " | " .
4631 $cgi->a({-href => href(action=>"blobdiff",
4632 hash=>$blob_current, hash_parent=>$blob_parent,
4633 hash_base=>$hash_base, hash_parent_base=>$commit,
4634 file_name=>$file_name)},
4635 "diff to current");
4638 print "</td>\n" .
4639 "</tr>\n";
4641 if (defined $extra) {
4642 print "<tr>\n" .
4643 "<td colspan=\"4\">$extra</td>\n" .
4644 "</tr>\n";
4646 print "</table>\n";
4649 sub git_tags_body {
4650 # uses global variable $project
4651 my ($taglist, $from, $to, $extra) = @_;
4652 $from = 0 unless defined $from;
4653 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4655 print "<table class=\"tags\">\n";
4656 my $alternate = 1;
4657 for (my $i = $from; $i <= $to; $i++) {
4658 my $entry = $taglist->[$i];
4659 my %tag = %$entry;
4660 my $comment = $tag{'subject'};
4661 my $comment_short;
4662 if (defined $comment) {
4663 $comment_short = chop_str($comment, 30, 5);
4665 if ($alternate) {
4666 print "<tr class=\"dark\">\n";
4667 } else {
4668 print "<tr class=\"light\">\n";
4670 $alternate ^= 1;
4671 if (defined $tag{'age'}) {
4672 print "<td><i>$tag{'age'}</i></td>\n";
4673 } else {
4674 print "<td></td>\n";
4676 print "<td>" .
4677 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4678 -class => "list name"}, esc_html($tag{'name'})) .
4679 "</td>\n" .
4680 "<td>";
4681 if (defined $comment) {
4682 print format_subject_html($comment, $comment_short,
4683 href(action=>"tag", hash=>$tag{'id'}));
4685 print "</td>\n" .
4686 "<td class=\"selflink\">";
4687 if ($tag{'type'} eq "tag") {
4688 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4689 } else {
4690 print "&nbsp;";
4692 print "</td>\n" .
4693 "<td class=\"link\">" . " | " .
4694 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4695 if ($tag{'reftype'} eq "commit") {
4696 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "log");
4697 } elsif ($tag{'reftype'} eq "blob") {
4698 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4700 print "</td>\n" .
4701 "</tr>";
4703 if (defined $extra) {
4704 print "<tr>\n" .
4705 "<td colspan=\"5\">$extra</td>\n" .
4706 "</tr>\n";
4708 print "</table>\n";
4711 sub git_heads_body {
4712 # uses global variable $project
4713 my ($headlist, $head, $from, $to, $extra) = @_;
4714 $from = 0 unless defined $from;
4715 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4717 print "<table class=\"heads\">\n";
4718 my $alternate = 1;
4719 for (my $i = $from; $i <= $to; $i++) {
4720 my $entry = $headlist->[$i];
4721 my %ref = %$entry;
4722 my $curr = $ref{'id'} eq $head;
4723 if ($alternate) {
4724 print "<tr class=\"dark\">\n";
4725 } else {
4726 print "<tr class=\"light\">\n";
4728 $alternate ^= 1;
4729 print "<td><i>$ref{'age'}</i></td>\n" .
4730 ($curr ? "<td class=\"current_head\">" : "<td>") .
4731 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4732 -class => "list name"},esc_html($ref{'name'})) .
4733 "</td>\n" .
4734 "<td class=\"link\">" .
4735 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "log") . " | " .
4736 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4737 "</td>\n" .
4738 "</tr>";
4740 if (defined $extra) {
4741 print "<tr>\n" .
4742 "<td colspan=\"3\">$extra</td>\n" .
4743 "</tr>\n";
4745 print "</table>\n";
4748 sub git_search_grep_body {
4749 my ($commitlist, $from, $to, $extra) = @_;
4750 $from = 0 unless defined $from;
4751 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4753 print "<table class=\"commit_search\">\n";
4754 my $alternate = 1;
4755 for (my $i = $from; $i <= $to; $i++) {
4756 my %co = %{$commitlist->[$i]};
4757 if (!%co) {
4758 next;
4760 my $commit = $co{'id'};
4761 if ($alternate) {
4762 print "<tr class=\"dark\">\n";
4763 } else {
4764 print "<tr class=\"light\">\n";
4766 $alternate ^= 1;
4767 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4768 format_author_html('td', \%co, 15, 5) .
4769 "<td>" .
4770 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4771 -class => "list subject"},
4772 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4773 my $comment = $co{'comment'};
4774 foreach my $line (@$comment) {
4775 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4776 my ($lead, $match, $trail) = ($1, $2, $3);
4777 $match = chop_str($match, 70, 5, 'center');
4778 my $contextlen = int((80 - length($match))/2);
4779 $contextlen = 30 if ($contextlen > 30);
4780 $lead = chop_str($lead, $contextlen, 10, 'left');
4781 $trail = chop_str($trail, $contextlen, 10, 'right');
4783 $lead = esc_html($lead);
4784 $match = esc_html($match);
4785 $trail = esc_html($trail);
4787 print "$lead<span class=\"match\">$match</span>$trail<br />";
4790 print "</td>\n" .
4791 "<td class=\"link\">" .
4792 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4793 " | " .
4794 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4795 " | " .
4796 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4797 print "</td>\n" .
4798 "</tr>\n";
4800 if (defined $extra) {
4801 print "<tr>\n" .
4802 "<td colspan=\"3\">$extra</td>\n" .
4803 "</tr>\n";
4805 print "</table>\n";
4808 ## ======================================================================
4809 ## ======================================================================
4810 ## actions
4812 sub git_frontpage {
4813 git_header_html();
4814 if (-f $home_text) {
4815 print "<div class=\"index_include\">\n";
4816 insert_file($home_text);
4817 print "</div>\n";
4819 git_project_search_form();
4820 if (not $frontpage_no_project_list) {
4821 git_project_list_all();
4822 } else {
4823 my $show_ctags = gitweb_check_feature('ctags');
4824 if ($frontpage_no_project_list == 1 and $show_ctags) {
4825 my @list = git_get_projects_list();
4826 my @projects = cached_project_list_info(\@list,
4827 gitweb_check_feature('forks'),
4828 $show_ctags, $projlist_cache_lifetime);
4829 git_project_list_ctags(\@projects);
4831 print "<p class=\"projectlist_link\">" .
4832 $cgi->a({-href => href(action=>'project_list')}, "Browse all projects") .
4833 "</p>\n";
4835 git_footer_html();
4838 sub git_project_list {
4839 git_header_html();
4840 git_project_search_form();
4841 git_project_list_all();
4842 git_footer_html();
4845 sub git_forks {
4846 my $order = $input_params{'order'};
4847 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4848 die_error(400, "Unknown order parameter");
4851 my @list = git_get_projects_list($project);
4852 if (!@list) {
4853 die_error(404, "No forks found");
4856 git_header_html();
4857 git_print_page_nav('','');
4858 git_print_header_div('summary', "$project forks");
4859 git_project_list_body(\@list, $order, undef, undef, undef, undef, 'forks');
4860 git_footer_html();
4863 sub git_project_index {
4864 my @projects = git_get_projects_list($project);
4866 print $cgi->header(
4867 -type => 'text/plain',
4868 -charset => 'utf-8',
4869 -content_disposition => 'inline; filename="index.aux"');
4871 foreach my $pr (@projects) {
4872 if (!exists $pr->{'owner'}) {
4873 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4876 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4877 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4878 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4879 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4880 $path =~ s/ /\+/g;
4881 $owner =~ s/ /\+/g;
4883 print "$path $owner\n";
4887 sub git_summary {
4888 my $descr = git_get_project_description($project) || "none";
4889 my %co = parse_commit("HEAD");
4890 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4891 my $head = $co{'id'};
4893 my $owner = git_get_project_owner($project);
4894 my $homepage = git_get_project_config('homepage');
4895 my $base_url = git_get_project_config('baseurl');
4896 my $last_refresh = git_get_project_config("lastrefresh");
4898 my $refs = git_get_references();
4899 # These get_*_list functions return one more to allow us to see if
4900 # there are more ...
4901 my @taglist = git_get_tags_list(16);
4902 my @headlist = git_get_heads_list(16);
4903 my @forklist;
4904 my $check_forks = gitweb_check_feature('forks');
4906 if ($check_forks) {
4907 @forklist = git_get_projects_list($project);
4910 git_header_html();
4911 git_print_page_nav('summary','', $head);
4913 if ($check_forks and $project =~ m#/#) {
4914 my $xproject = $project; $xproject =~ s#/.+?$#.git#; #
4915 my $r = $cgi->a({-href=> href(project => $xproject, action => 'summary')}, $xproject);
4916 print <<EOT;
4917 <div class="forkinfo">
4918 This project is a fork of the $r project. If you have that one
4919 already cloned locally, you can use
4920 <pre>git clone --reference /path/to/your/$xproject/incarnation mirror_URL</pre>
4921 to save bandwidth during cloning.
4922 </div>
4926 print "<div class=\"title\">&nbsp;</div>\n";
4927 print "<table class=\"projects_list\">\n";
4928 print "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
4929 $homepage and print "<tr id=\"metadata_homepage\"><td>homepage URL</td><td>" . $cgi->a({-href => $homepage}, $homepage) . "</td></tr>\n";
4930 $base_url and print "<tr id=\"metadata_baseurl\"><td>repository URL</td><td>" . esc_html($base_url) . "</td></tr>\n";
4931 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . email_obfuscate($owner) . "</td></tr>\n";
4932 if (defined $cd{'rfc2822'}) {
4933 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4935 $last_refresh and print "<tr id=\"metadata_lrefresh\"><td>last refresh</td><td>$last_refresh</td></tr>\n";
4937 # use per project git URL list in $projectroot/$project/cloneurl
4938 # or make project git URL from git base URL and project name
4939 my $url_tag = $base_url ? "mirror URL" : "URL";
4940 my @url_list = git_get_project_url_list($project);
4941 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4942 foreach my $git_url (@url_list) {
4943 next unless $git_url;
4944 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4945 $url_tag = "";
4947 -f "$projectroot/$project/.nofetch" and $git_base_push_url and
4948 print "<tr id=\"metadata_pushurl\"><td>Push URL</td><td>$git_base_push_url/$project</td></tr>\n";
4950 # Tag cloud
4951 my $show_ctags = gitweb_check_feature('ctags');
4952 if ($show_ctags) {
4953 my $ctags = git_get_project_ctags($project);
4954 my $cloud = git_populate_project_tagcloud($ctags, 'project_list');
4955 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4956 print "</td>\n<td>" unless %$ctags;
4957 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4958 print "</td>\n<td>" if %$ctags;
4959 print git_show_project_tagcloud($cloud, 48, 'project_list');
4960 print "</td></tr>";
4963 print "</table>\n";
4965 # If XSS prevention is on, we don't include README.html.
4966 # TODO: Allow a readme in some safe format.
4967 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
4968 print "<div class=\"title\">readme</div>\n" .
4969 "<div class=\"readme\">\n";
4970 insert_file("$projectroot/$project/README.html");
4971 print "\n</div>\n"; # class="readme"
4974 # we need to request one more than 16 (0..15) to check if
4975 # those 16 are all
4976 my @commitlist = $head ? parse_commits($head, 17) : ();
4977 if (@commitlist) {
4978 git_print_header_div('shortlog');
4979 git_shortlog_body(\@commitlist, 0, 15, $refs,
4980 $#commitlist <= 15 ? undef :
4981 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4984 if (@taglist) {
4985 git_print_header_div('tags');
4986 git_tags_body(\@taglist, 0, 15,
4987 $#taglist <= 15 ? undef :
4988 $cgi->a({-href => href(action=>"tags")}, "..."));
4991 if (@headlist) {
4992 git_print_header_div('heads');
4993 git_heads_body(\@headlist, $head, 0, 15,
4994 $#headlist <= 15 ? undef :
4995 $cgi->a({-href => href(action=>"heads")}, "..."));
4998 if (@forklist) {
4999 git_print_header_div('forks');
5000 git_project_list_body(\@forklist, 'age', 0, 15,
5001 $#forklist <= 15 ? undef :
5002 $cgi->a({-href => href(action=>"forks")}, "..."),
5003 'no_header', 'forks');
5006 git_footer_html();
5009 sub git_tag {
5010 my $head = git_get_head_hash($project);
5011 git_header_html();
5012 git_print_page_nav('','', $head,undef,$head);
5013 my %tag = parse_tag($hash);
5015 if (! %tag) {
5016 die_error(404, "Unknown tag object");
5019 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
5020 print "<div class=\"title_text\">\n" .
5021 "<table class=\"object_header\">\n" .
5022 "<tr>\n" .
5023 "<td>object</td>\n" .
5024 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5025 $tag{'object'}) . "</td>\n" .
5026 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5027 $tag{'type'}) . "</td>\n" .
5028 "</tr>\n";
5029 if (defined($tag{'author'})) {
5030 git_print_authorship_rows(\%tag, 'author');
5032 print "</table>\n\n" .
5033 "</div>\n";
5034 print "<div class=\"page_body\">";
5035 my $comment = $tag{'comment'};
5036 foreach my $line (@$comment) {
5037 chomp $line;
5038 print esc_html($line, -nbsp=>1) . "<br/>\n";
5040 print "</div>\n";
5041 git_footer_html();
5044 sub git_blame_data {
5045 my $ftype;
5047 my ($have_blame) = gitweb_check_feature('blame');
5048 if (!$have_blame) {
5049 die_error('403 Permission denied', "Permission denied");
5051 die_error('404 Not Found', "File name not defined") if (!$file_name);
5052 $hash_base ||= git_get_head_hash($project);
5053 die_error(undef, "Couldn't find base commit") unless ($hash_base);
5054 my %co = parse_commit($hash_base)
5055 or die_error(undef, "Reading commit failed");
5056 if (!defined $hash) {
5057 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5058 or die_error(undef, "Error looking up file");
5060 $ftype = git_get_type($hash);
5061 if ($ftype !~ "blob") {
5062 die_error("400 Bad Request", "Object is not a blob");
5064 open my $fd, "-|", git_cmd(), "blame", '--incremental',
5065 $hash_base, '--', $file_name
5066 or die_error(undef, "Open git-blame --incremental failed");
5068 print $cgi->header(-type=>"text/plain", -charset => 'utf-8',
5069 -status=> "200 OK");
5071 while(<$fd>) {
5072 if (/^([0-9a-f]{40}) ([0-9]+) ([0-9]+) ([0-9]+)/ or
5073 /^author-time |^author |^filename /) {
5074 print;
5078 close $fd or print "Reading blame data failed\n";
5081 sub git_blame_common {
5082 my ($type) = @_;
5084 # permissions
5085 gitweb_check_feature('blame')
5086 or die_error(403, "Blame view not allowed");
5088 # error checking
5089 die_error(400, "No file name given") unless $file_name;
5090 $hash_base ||= git_get_head_hash($project);
5091 die_error(404, "Couldn't find base commit") unless $hash_base;
5092 my %co = parse_commit($hash_base)
5093 or die_error(404, "Commit not found");
5094 my $ftype = "blob";
5095 if (!defined $hash) {
5096 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5097 or die_error(404, "Error looking up file");
5098 } else {
5099 $ftype = git_get_type($hash);
5100 if ($ftype !~ "blob") {
5101 die_error(400, "Object is not a blob");
5104 $ftype = git_get_type($hash);
5105 if ($ftype !~ "blob") {
5106 die_error(400, "Object is not a blob");
5108 my $fd;
5109 if ($type eq 'incremental') {
5110 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5111 or die_error(undef, "Open git-cat-file failed");
5112 } else {
5113 # run git-blame --porcelain
5114 open $fd, "-|", git_cmd(), "blame", '-p',
5115 $hash_base, '--', $file_name
5116 or die_error(500, "Open git-blame failed");
5119 # page header
5120 git_header_html();
5121 my $formats_nav =
5122 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5123 "blob") .
5124 " | " .
5125 $cgi->a({-href => href(action=>"history", -replay=>1)},
5126 "history") .
5127 " | " .
5128 $cgi->a({-href => href(action=>"blame", file_name=>$file_name), -class => "blamelink"},
5129 "HEAD");
5130 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5131 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5132 git_print_page_path($file_name, $ftype, $hash_base);
5134 # page body
5135 my @rev_color = qw(light dark);
5136 my $num_colors = scalar(@rev_color);
5137 my $current_color = 0;
5138 my %metainfo = ();
5140 print <<HTML;
5142 <div class="page_body">
5143 <table class="blame">
5144 <tr><th>Commit&nbsp;<a href="javascript:extra_blame_columns()" id="columns_expander">[+]</a></th>
5145 <th class="extra_column">Author</th>
5146 <th class="extra_column">Date</th>
5147 <th>Line</th>
5148 <th>Data</th></tr>
5149 HTML
5150 LINE:
5151 my $linenr = 0;
5152 while (my $line = <$fd>) {
5153 chomp $line;
5154 if ($type eq 'incremental') {
5155 # Empty stage with just the file contents
5156 $linenr += 1;
5157 print "<tr id=\"l$linenr\" class=\"light2\">";
5158 print '<td class="sha1"><a href=""></a></td>';
5159 print "<td class=\"extra_column\"></td>";
5160 print "<td class=\"extra_column\"></td>";
5161 print "<td class=\"linenr\"><a class=\"linenr\" href=\"\">$linenr</a></td><td class=\"pre\">" . esc_html($line) . "</td>\n";
5162 print "</tr>\n";
5163 next;
5166 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5167 # no <lines in group> for subsequent lines in group of lines
5168 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5169 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5170 if (!exists $metainfo{$full_rev}) {
5171 $metainfo{$full_rev} = { 'nprevious' => 0 };
5173 my $meta = $metainfo{$full_rev};
5174 my $data;
5175 while ($data = <$fd>) {
5176 chomp $data;
5177 last if ($data =~ s/^\t//); # contents of line
5178 if ($data =~ /^(\S+)(?: (.*))?$/) {
5179 $meta->{$1} = $2 unless exists $meta->{$1};
5181 if ($data =~ /^previous /) {
5182 $meta->{'nprevious'}++;
5185 my $short_rev = substr($full_rev, 0, 8);
5186 my $author = $meta->{'author'};
5187 my %date =
5188 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5189 my $date = $date{'iso-tz'};
5190 if ($group_size) {
5191 $current_color = ($current_color + 1) % $num_colors;
5193 my $tr_class = $rev_color[$current_color];
5194 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5195 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5196 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5197 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5198 if ($group_size) {
5199 my $rowspan = $group_size > 1 ? " rowspan=\"$group_size\"" : "";
5200 print "<td class=\"sha1\"";
5201 print " title=\"". esc_html($author) . ", $date\"";
5202 print "$rowspan>";
5203 print $cgi->a({-href => href(action=>"commit",
5204 hash=>$full_rev,
5205 file_name=>$file_name)},
5206 esc_html($short_rev));
5207 if ($group_size >= 2) {
5208 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5209 if (@author_initials) {
5210 print "<br />" .
5211 esc_html(join('', @author_initials));
5212 # or join('.', ...)
5215 print "</td>\n";
5216 print "<td class=\"extra_column\" $rowspan>". esc_html($author) . "</td>";
5217 print "<td class=\"extra_column\" $rowspan>". $date . "</td>";
5219 # 'previous' <sha1 of parent commit> <filename at commit>
5220 if (exists $meta->{'previous'} &&
5221 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5222 $meta->{'parent'} = $1;
5223 $meta->{'file_parent'} = unquote($2);
5225 my $linenr_commit =
5226 exists($meta->{'parent'}) ?
5227 $meta->{'parent'} : $full_rev;
5228 my $linenr_filename =
5229 exists($meta->{'file_parent'}) ?
5230 $meta->{'file_parent'} : unquote($meta->{'filename'});
5231 my $blamed = href(action => 'blame',
5232 file_name => $linenr_filename,
5233 hash_base => $linenr_commit);
5234 print "<td class=\"linenr\">";
5235 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5236 -class => "linenr" },
5237 esc_html($lineno));
5238 print "</td>";
5239 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5240 print "</tr>\n";
5243 print "</table>\n";
5244 print "</div>";
5245 close $fd
5246 or print "Reading blob failed\n";
5248 if ($type eq 'incremental') {
5249 print "<script type=\"text/javascript\">\n";
5250 print "startBlame(\"" . href(action=>"blame_data", hash_base=>$hash_base, file_name=>$file_name) . "\", \"" .
5251 href(-partial_query=>1) . "\");\n";
5252 print "</script>\n";
5255 # page footer
5256 git_footer_html();
5259 sub git_blame_incremental {
5260 git_blame_common('incremental');
5263 sub git_blame {
5264 git_blame_common('oneshot');
5267 sub git_tags {
5268 my $head = git_get_head_hash($project);
5269 git_header_html();
5270 git_print_page_nav('','', $head,undef,$head);
5271 git_print_header_div('summary', $project);
5273 my @tagslist = git_get_tags_list();
5274 if (@tagslist) {
5275 git_tags_body(\@tagslist);
5277 git_footer_html();
5280 sub git_heads {
5281 my $head = git_get_head_hash($project);
5282 git_header_html();
5283 git_print_page_nav('','', $head,undef,$head);
5284 git_print_header_div('summary', $project);
5286 my @headslist = git_get_heads_list();
5287 if (@headslist) {
5288 git_heads_body(\@headslist, $head);
5290 git_footer_html();
5293 sub git_blob_plain {
5294 my $type = shift;
5295 my $expires;
5297 if (!defined $hash) {
5298 if (defined $file_name) {
5299 my $base = $hash_base || git_get_head_hash($project);
5300 $hash = git_get_hash_by_path($base, $file_name, "blob")
5301 or die_error(404, "Cannot find file");
5302 } else {
5303 die_error(400, "No file name defined");
5305 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5306 # blobs defined by non-textual hash id's can be cached
5307 $expires = "+1d";
5310 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5311 or die_error(500, "Open git-cat-file blob '$hash' failed");
5313 # content-type (can include charset)
5314 $type = blob_contenttype($fd, $file_name, $type);
5316 # "save as" filename, even when no $file_name is given
5317 my $save_as = "$hash";
5318 if (defined $file_name) {
5319 $save_as = $file_name;
5320 } elsif ($type =~ m/^text\//) {
5321 $save_as .= '.txt';
5324 # With XSS prevention on, blobs of all types except a few known safe
5325 # ones are served with "Content-Disposition: attachment" to make sure
5326 # they don't run in our security domain. For certain image types,
5327 # blob view writes an <img> tag referring to blob_plain view, and we
5328 # want to be sure not to break that by serving the image as an
5329 # attachment (though Firefox 3 doesn't seem to care).
5330 my $sandbox = $prevent_xss &&
5331 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5333 print $cgi->header(
5334 -type => $type,
5335 -expires => $expires,
5336 -content_disposition =>
5337 ($sandbox ? 'attachment' : 'inline')
5338 . '; filename="' . $save_as . '"');
5339 local $/ = undef;
5340 binmode STDOUT, ':raw';
5341 print <$fd>;
5342 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5343 close $fd;
5346 sub git_blob {
5347 my $expires;
5349 if (!defined $hash) {
5350 if (defined $file_name) {
5351 my $base = $hash_base || git_get_head_hash($project);
5352 $hash = git_get_hash_by_path($base, $file_name, "blob")
5353 or die_error(404, "Cannot find file");
5354 } else {
5355 die_error(400, "No file name defined");
5357 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5358 # blobs defined by non-textual hash id's can be cached
5359 $expires = "+1d";
5362 my $have_blame = gitweb_check_feature('blame');
5363 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5364 or die_error(500, "Couldn't cat $file_name, $hash");
5365 my $mimetype = blob_mimetype($fd, $file_name);
5366 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5367 close $fd;
5368 return git_blob_plain($mimetype);
5370 # we can have blame only for text/* mimetype
5371 $have_blame &&= ($mimetype =~ m!^text/!);
5373 git_header_html(undef, $expires);
5374 my $formats_nav = '';
5375 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5376 if (defined $file_name) {
5377 if ($have_blame) {
5378 $formats_nav .=
5379 $cgi->a({-href => href(action=>"blame", -replay=>1,
5380 -class => "blamelink")},
5381 "blame") .
5382 " | ";
5384 $formats_nav .=
5385 $cgi->a({-href => href(action=>"history", -replay=>1)},
5386 "history") .
5387 " | " .
5388 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5389 "raw") .
5390 " | " .
5391 $cgi->a({-href => href(action=>"blob",
5392 hash_base=>"HEAD", file_name=>$file_name)},
5393 "HEAD");
5394 } else {
5395 $formats_nav .=
5396 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5397 "raw");
5399 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5400 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5401 } else {
5402 print "<div class=\"page_nav\">\n" .
5403 "<br/><br/></div>\n" .
5404 "<div class=\"title\">$hash</div>\n";
5406 git_print_page_path($file_name, "blob", $hash_base);
5407 print "<div class=\"page_body\">\n";
5408 if ($mimetype =~ m!^image/!) {
5409 print qq!<img type="$mimetype"!;
5410 if ($file_name) {
5411 print qq! alt="$file_name" title="$file_name"!;
5413 print qq! src="! .
5414 href(action=>"blob_plain", hash=>$hash,
5415 hash_base=>$hash_base, file_name=>$file_name) .
5416 qq!" />\n!;
5417 } else {
5418 my $nr;
5419 while (my $line = <$fd>) {
5420 chomp $line;
5421 $nr++;
5422 $line = untabify($line);
5423 printf "<div class=\"pre\"><a id=\"l%i\" href=\"" . href(-replay => 1)
5424 . "#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
5425 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
5428 close $fd
5429 or print "Reading blob failed.\n";
5430 print "</div>";
5431 git_footer_html();
5434 sub git_tree {
5435 if (!defined $hash_base) {
5436 $hash_base = "HEAD";
5438 if (!defined $hash) {
5439 if (defined $file_name) {
5440 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5441 } else {
5442 $hash = $hash_base;
5445 die_error(404, "No such tree") unless defined($hash);
5447 my $show_sizes = gitweb_check_feature('show-sizes');
5448 my $have_blame = gitweb_check_feature('blame');
5450 my @entries = ();
5452 local $/ = "\0";
5453 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5454 ($show_sizes ? '-l' : ()), @extra_options, $hash
5455 or die_error(500, "Open git-ls-tree failed");
5456 @entries = map { chomp; $_ } <$fd>;
5457 close $fd
5458 or die_error(404, "Reading tree failed");
5461 my $refs = git_get_references();
5462 my $ref = format_ref_marker($refs, $hash_base);
5463 git_header_html();
5464 my $basedir = '';
5465 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5466 my @views_nav = ();
5467 if (defined $file_name) {
5468 push @views_nav,
5469 $cgi->a({-href => href(action=>"history", -replay=>1)},
5470 "history"),
5471 $cgi->a({-href => href(action=>"tree",
5472 hash_base=>"HEAD", file_name=>$file_name)},
5473 "HEAD"),
5475 my $snapshot_links = format_snapshot_links($hash);
5476 if (defined $snapshot_links) {
5477 # FIXME: Should be available when we have no hash base as well.
5478 push @views_nav, $snapshot_links;
5480 git_print_page_nav('tree','', $hash_base, undef, undef,
5481 join(' | ', @views_nav));
5482 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5483 } else {
5484 undef $hash_base;
5485 print "<div class=\"page_nav\">\n";
5486 print "<br/><br/></div>\n";
5487 print "<div class=\"title\">$hash</div>\n";
5489 if (defined $file_name) {
5490 $basedir = $file_name;
5491 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5492 $basedir .= '/';
5494 git_print_page_path($file_name, 'tree', $hash_base);
5496 print "<div class=\"page_body\">\n";
5497 print "<table class=\"tree\">\n";
5498 my $alternate = 1;
5499 # '..' (top directory) link if possible
5500 if (defined $hash_base &&
5501 defined $file_name && $file_name =~ m![^/]+$!) {
5502 if ($alternate) {
5503 print "<tr class=\"dark\">\n";
5504 } else {
5505 print "<tr class=\"light\">\n";
5507 $alternate ^= 1;
5509 my $up = $file_name;
5510 $up =~ s!/?[^/]+$!!;
5511 undef $up unless $up;
5512 # based on git_print_tree_entry
5513 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5514 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
5515 print '<td class="list">';
5516 print $cgi->a({-href => href(action=>"tree",
5517 hash_base=>$hash_base,
5518 file_name=>$up)},
5519 "..");
5520 print "</td>\n";
5521 print "<td class=\"link\"></td>\n";
5523 print "</tr>\n";
5525 foreach my $line (@entries) {
5526 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
5528 if ($alternate) {
5529 print "<tr class=\"dark\">\n";
5530 } else {
5531 print "<tr class=\"light\">\n";
5533 $alternate ^= 1;
5535 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
5537 print "</tr>\n";
5539 print "</table>\n" .
5540 "</div>";
5541 git_footer_html();
5544 sub git_snapshot {
5545 my $format = $input_params{'snapshot_format'};
5546 if (!@snapshot_fmts) {
5547 die_error(403, "Snapshots not allowed");
5549 # default to first supported snapshot format
5550 $format ||= $snapshot_fmts[0];
5551 if ($format !~ m/^[a-z0-9]+$/) {
5552 die_error(400, "Invalid snapshot format parameter");
5553 } elsif (!exists($known_snapshot_formats{$format})) {
5554 die_error(400, "Unknown snapshot format");
5555 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
5556 die_error(403, "Snapshot format not allowed");
5557 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5558 die_error(403, "Unsupported snapshot format");
5561 if (!defined $hash) {
5562 $hash = git_get_head_hash($project);
5565 my $name = $project;
5566 $name =~ s,([^/])/*\.git$,$1,;
5567 $name = basename($name);
5568 my $filename = to_utf8($name);
5569 $name =~ s/\047/\047\\\047\047/g;
5570 my $cmd;
5571 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
5572 $cmd = quote_command(
5573 git_cmd(), 'archive',
5574 "--format=$known_snapshot_formats{$format}{'format'}",
5575 "--prefix=$name/", $hash);
5576 if (exists $known_snapshot_formats{$format}{'compressor'}) {
5577 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
5580 print $cgi->header(
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';
5588 print <$fd>;
5589 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5590 close $fd;
5593 sub git_log {
5594 my $head = git_get_head_hash($project);
5595 if (!defined $hash) {
5596 $hash = $head;
5598 if (!defined $page) {
5599 $page = 0;
5601 my $refs = git_get_references();
5603 my @commitlist = parse_commits($hash, 101, (100 * $page));
5605 my $paging_nav = format_log_nav('log', $hash, $head, $page, $#commitlist >= 100);
5607 my ($patch_max) = gitweb_get_feature('patches');
5608 if ($patch_max) {
5609 if ($patch_max < 0 || @commitlist <= $patch_max) {
5610 $paging_nav .= " &sdot; " .
5611 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5612 "patches");
5617 local $action = 'fulllog';
5618 git_header_html();
5620 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
5622 if (!@commitlist) {
5623 my %co = parse_commit($hash);
5625 git_print_header_div('summary', $project);
5626 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
5628 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
5629 for (my $i = 0; $i <= $to; $i++) {
5630 my %co = %{$commitlist[$i]};
5631 next if !%co;
5632 my $commit = $co{'id'};
5633 my $ref = format_ref_marker($refs, $commit);
5634 my %ad = parse_date($co{'author_epoch'});
5635 git_print_header_div('commit',
5636 "<span class=\"age\">$co{'age_string'}</span>" .
5637 esc_html($co{'title'}) . $ref,
5638 $commit);
5639 print "<div class=\"title_text\">\n" .
5640 "<div class=\"log_link\">\n" .
5641 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5642 " | " .
5643 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5644 " | " .
5645 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5646 "<br/>\n" .
5647 "</div>\n";
5648 git_print_authorship(\%co, -tag => 'span');
5649 print "<br/>\n</div>\n";
5651 print "<div class=\"log_body\">\n";
5652 git_print_log($co{'comment'}, -final_empty_line=> 1);
5653 print "</div>\n";
5655 if ($#commitlist >= 100) {
5656 print "<div class=\"page_nav\">\n";
5657 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
5658 -accesskey => "n", -title => "Alt-n"}, "next");
5659 print "</div>\n";
5661 git_footer_html();
5664 sub git_commit {
5665 $hash ||= $hash_base || "HEAD";
5666 my %co = parse_commit($hash)
5667 or die_error(404, "Unknown commit object");
5669 my $parent = $co{'parent'};
5670 my $parents = $co{'parents'}; # listref
5672 # we need to prepare $formats_nav before any parameter munging
5673 my $formats_nav;
5674 if (!defined $parent) {
5675 # --root commitdiff
5676 $formats_nav .= '(initial)';
5677 } elsif (@$parents == 1) {
5678 # single parent commit
5679 $formats_nav .=
5680 '(parent: ' .
5681 $cgi->a({-href => href(action=>"commit",
5682 hash=>$parent)},
5683 esc_html(substr($parent, 0, 7))) .
5684 ')';
5685 } else {
5686 # merge commit
5687 $formats_nav .=
5688 '(merge: ' .
5689 join(' ', map {
5690 $cgi->a({-href => href(action=>"commit",
5691 hash=>$_)},
5692 esc_html(substr($_, 0, 7)));
5693 } @$parents ) .
5694 ')';
5696 if (gitweb_check_feature('patches') && @$parents <= 1) {
5697 $formats_nav .= " | " .
5698 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5699 "patch");
5702 if (!defined $parent) {
5703 $parent = "--root";
5705 my @difftree;
5706 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5707 @diff_opts,
5708 (@$parents <= 1 ? $parent : '-c'),
5709 $hash, "--"
5710 or die_error(500, "Open git-diff-tree failed");
5711 @difftree = map { chomp; $_ } <$fd>;
5712 close $fd or die_error(404, "Reading git-diff-tree failed");
5714 # non-textual hash id's can be cached
5715 my $expires;
5716 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5717 $expires = "+1d";
5719 my $refs = git_get_references();
5720 my $ref = format_ref_marker($refs, $co{'id'});
5722 git_header_html(undef, $expires);
5723 git_print_page_nav('commit', '',
5724 $hash, $co{'tree'}, $hash,
5725 $formats_nav);
5727 if (defined $co{'parent'}) {
5728 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5729 } else {
5730 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5732 print "<div class=\"title_text\">\n" .
5733 "<table class=\"object_header\">\n";
5734 git_print_authorship_rows(\%co);
5735 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5736 print "<tr>" .
5737 "<td>tree</td>" .
5738 "<td class=\"sha1\">" .
5739 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5740 class => "list"}, $co{'tree'}) .
5741 "</td>" .
5742 "<td class=\"link\">" .
5743 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5744 "tree");
5745 my $snapshot_links = format_snapshot_links($hash);
5746 if (defined $snapshot_links) {
5747 print " | " . $snapshot_links;
5749 print "</td>" .
5750 "</tr>\n";
5752 foreach my $par (@$parents) {
5753 print "<tr>" .
5754 "<td>parent</td>" .
5755 "<td class=\"sha1\">" .
5756 $cgi->a({-href => href(action=>"commit", hash=>$par),
5757 class => "list"}, $par) .
5758 "</td>" .
5759 "<td class=\"link\">" .
5760 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
5761 " | " .
5762 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
5763 "</td>" .
5764 "</tr>\n";
5766 print "</table>".
5767 "</div>\n";
5769 print "<div class=\"page_body\">\n";
5770 git_print_log($co{'comment'});
5771 print "</div>\n";
5773 git_difftree_body(\@difftree, $hash, @$parents);
5775 git_footer_html();
5778 sub git_object {
5779 # object is defined by:
5780 # - hash or hash_base alone
5781 # - hash_base and file_name
5782 my $type;
5784 # - hash or hash_base alone
5785 if ($hash || ($hash_base && !defined $file_name)) {
5786 my $object_id = $hash || $hash_base;
5788 open my $fd, "-|", quote_command(
5789 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
5790 or die_error(404, "Object does not exist");
5791 $type = <$fd>;
5792 chomp $type;
5793 close $fd
5794 or die_error(404, "Object does not exist");
5796 # - hash_base and file_name
5797 } elsif ($hash_base && defined $file_name) {
5798 $file_name =~ s,/+$,,;
5800 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
5801 or die_error(404, "Base object does not exist");
5803 # here errors should not hapen
5804 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
5805 or die_error(500, "Open git-ls-tree failed");
5806 my $line = <$fd>;
5807 close $fd;
5809 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5810 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
5811 die_error(404, "File or directory for given base does not exist");
5813 $type = $2;
5814 $hash = $3;
5815 } else {
5816 die_error(400, "Not enough information to find object");
5819 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5820 hash=>$hash, hash_base=>$hash_base,
5821 file_name=>$file_name),
5822 -status => '302 Found');
5825 sub git_blobdiff {
5826 my $format = shift || 'html';
5828 my $fd;
5829 my @difftree;
5830 my %diffinfo;
5831 my $expires;
5833 # preparing $fd and %diffinfo for git_patchset_body
5834 # new style URI
5835 if (defined $hash_base && defined $hash_parent_base) {
5836 if (defined $file_name) {
5837 # read raw output
5838 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5839 $hash_parent_base, $hash_base,
5840 "--", (defined $file_parent ? $file_parent : ()), $file_name
5841 or die_error(500, "Open git-diff-tree failed");
5842 @difftree = map { chomp; $_ } <$fd>;
5843 close $fd
5844 or die_error(404, "Reading git-diff-tree failed");
5845 @difftree
5846 or die_error(404, "Blob diff not found");
5848 } elsif (defined $hash &&
5849 $hash =~ /[0-9a-fA-F]{40}/) {
5850 # try to find filename from $hash
5852 # read filtered raw output
5853 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5854 $hash_parent_base, $hash_base, "--"
5855 or die_error(500, "Open git-diff-tree failed");
5856 @difftree =
5857 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5858 # $hash == to_id
5859 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5860 map { chomp; $_ } <$fd>;
5861 close $fd
5862 or die_error(404, "Reading git-diff-tree failed");
5863 @difftree
5864 or die_error(404, "Blob diff not found");
5866 } else {
5867 die_error(400, "Missing one of the blob diff parameters");
5870 if (@difftree > 1) {
5871 die_error(400, "Ambiguous blob diff specification");
5874 %diffinfo = parse_difftree_raw_line($difftree[0]);
5875 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5876 $file_name ||= $diffinfo{'to_file'};
5878 $hash_parent ||= $diffinfo{'from_id'};
5879 $hash ||= $diffinfo{'to_id'};
5881 # non-textual hash id's can be cached
5882 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5883 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5884 $expires = '+1d';
5887 # open patch output
5888 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5889 '-p', ($format eq 'html' ? "--full-index" : ()),
5890 $hash_parent_base, $hash_base,
5891 "--", (defined $file_parent ? $file_parent : ()), $file_name
5892 or die_error(500, "Open git-diff-tree failed");
5895 # old/legacy style URI -- not generated anymore since 1.4.3.
5896 if (!%diffinfo) {
5897 die_error('404 Not Found', "Missing one of the blob diff parameters")
5900 # header
5901 if ($format eq 'html') {
5902 my $formats_nav =
5903 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5904 "raw");
5905 git_header_html(undef, $expires);
5906 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5907 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5908 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5909 } else {
5910 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5911 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5913 if (defined $file_name) {
5914 git_print_page_path($file_name, "blob", $hash_base);
5915 } else {
5916 print "<div class=\"page_path\"></div>\n";
5919 } elsif ($format eq 'plain') {
5920 print $cgi->header(
5921 -type => 'text/plain',
5922 -charset => 'utf-8',
5923 -expires => $expires,
5924 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5926 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5928 } else {
5929 die_error(400, "Unknown blobdiff format");
5932 # patch
5933 if ($format eq 'html') {
5934 print "<div class=\"page_body\">\n";
5936 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5937 close $fd;
5939 print "</div>\n"; # class="page_body"
5940 git_footer_html();
5942 } else {
5943 while (my $line = <$fd>) {
5944 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5945 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5947 print $line;
5949 last if $line =~ m!^\+\+\+!;
5951 local $/ = undef;
5952 print <$fd>;
5953 close $fd;
5957 sub git_blobdiff_plain {
5958 git_blobdiff('plain');
5961 sub git_commitdiff {
5962 my %params = @_;
5963 my $format = $params{-format} || 'html';
5965 my ($patch_max) = gitweb_get_feature('patches');
5966 if ($format eq 'patch') {
5967 die_error(403, "Patch view not allowed") unless $patch_max;
5970 $hash ||= $hash_base || "HEAD";
5971 my %co = parse_commit($hash)
5972 or die_error(404, "Unknown commit object");
5974 # choose format for commitdiff for merge
5975 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5976 $hash_parent = '--cc';
5978 # we need to prepare $formats_nav before almost any parameter munging
5979 my $formats_nav;
5980 if ($format eq 'html') {
5981 $formats_nav =
5982 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5983 "raw");
5984 if ($patch_max && @{$co{'parents'}} <= 1) {
5985 $formats_nav .= " | " .
5986 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5987 "patch");
5990 if (defined $hash_parent &&
5991 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5992 # commitdiff with two commits given
5993 my $hash_parent_short = $hash_parent;
5994 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5995 $hash_parent_short = substr($hash_parent, 0, 7);
5997 $formats_nav .=
5998 ' (from';
5999 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6000 if ($co{'parents'}[$i] eq $hash_parent) {
6001 $formats_nav .= ' parent ' . ($i+1);
6002 last;
6005 $formats_nav .= ': ' .
6006 $cgi->a({-href => href(action=>"commitdiff",
6007 hash=>$hash_parent)},
6008 esc_html($hash_parent_short)) .
6009 ')';
6010 } elsif (!$co{'parent'}) {
6011 # --root commitdiff
6012 $formats_nav .= ' (initial)';
6013 } elsif (scalar @{$co{'parents'}} == 1) {
6014 # single parent commit
6015 $formats_nav .=
6016 ' (parent: ' .
6017 $cgi->a({-href => href(action=>"commitdiff",
6018 hash=>$co{'parent'})},
6019 esc_html(substr($co{'parent'}, 0, 7))) .
6020 ')';
6021 } else {
6022 # merge commit
6023 if ($hash_parent eq '--cc') {
6024 $formats_nav .= ' | ' .
6025 $cgi->a({-href => href(action=>"commitdiff",
6026 hash=>$hash, hash_parent=>'-c')},
6027 'combined');
6028 } else { # $hash_parent eq '-c'
6029 $formats_nav .= ' | ' .
6030 $cgi->a({-href => href(action=>"commitdiff",
6031 hash=>$hash, hash_parent=>'--cc')},
6032 'compact');
6034 $formats_nav .=
6035 ' (merge: ' .
6036 join(' ', map {
6037 $cgi->a({-href => href(action=>"commitdiff",
6038 hash=>$_)},
6039 esc_html(substr($_, 0, 7)));
6040 } @{$co{'parents'}} ) .
6041 ')';
6045 my $hash_parent_param = $hash_parent;
6046 if (!defined $hash_parent_param) {
6047 # --cc for multiple parents, --root for parentless
6048 $hash_parent_param =
6049 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6052 # read commitdiff
6053 my $fd;
6054 my @difftree;
6055 if ($format eq 'html') {
6056 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6057 "--no-commit-id", "--patch-with-raw", "--full-index",
6058 $hash_parent_param, $hash, "--"
6059 or die_error(500, "Open git-diff-tree failed");
6061 while (my $line = <$fd>) {
6062 chomp $line;
6063 # empty line ends raw part of diff-tree output
6064 last unless $line;
6065 push @difftree, scalar parse_difftree_raw_line($line);
6068 } elsif ($format eq 'plain') {
6069 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6070 '-p', $hash_parent_param, $hash, "--"
6071 or die_error(500, "Open git-diff-tree failed");
6072 } elsif ($format eq 'patch') {
6073 # For commit ranges, we limit the output to the number of
6074 # patches specified in the 'patches' feature.
6075 # For single commits, we limit the output to a single patch,
6076 # diverging from the git-format-patch default.
6077 my @commit_spec = ();
6078 if ($hash_parent) {
6079 if ($patch_max > 0) {
6080 push @commit_spec, "-$patch_max";
6082 push @commit_spec, '-n', "$hash_parent..$hash";
6083 } else {
6084 if ($params{-single}) {
6085 push @commit_spec, '-1';
6086 } else {
6087 if ($patch_max > 0) {
6088 push @commit_spec, "-$patch_max";
6090 push @commit_spec, "-n";
6092 push @commit_spec, '--root', $hash;
6094 open $fd, "-|", git_cmd(), "format-patch", '--encoding=utf8',
6095 '--stdout', @commit_spec
6096 or die_error(500, "Open git-format-patch failed");
6097 } else {
6098 die_error(400, "Unknown commitdiff format");
6101 # non-textual hash id's can be cached
6102 my $expires;
6103 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6104 $expires = "+1d";
6107 # write commit message
6108 if ($format eq 'html') {
6109 my $refs = git_get_references();
6110 my $ref = format_ref_marker($refs, $co{'id'});
6112 git_header_html(undef, $expires);
6113 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6114 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6115 print "<div class=\"title_text\">\n" .
6116 "<table class=\"object_header\">\n";
6117 git_print_authorship_rows(\%co);
6118 print "</table>".
6119 "</div>\n";
6120 print "<div class=\"page_body\">\n";
6121 if (@{$co{'comment'}} > 1) {
6122 print "<div class=\"log\">\n";
6123 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6124 print "</div>\n"; # class="log"
6127 } elsif ($format eq 'plain') {
6128 my $refs = git_get_references("tags");
6129 my $tagname = git_get_rev_name_tags($hash);
6130 my $filename = basename($project) . "-$hash.patch";
6132 print $cgi->header(
6133 -type => 'text/plain',
6134 -charset => 'utf-8',
6135 -expires => $expires,
6136 -content_disposition => 'inline; filename="' . "$filename" . '"');
6137 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
6138 print "From: " . to_utf8($co{'author'}) . "\n";
6139 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6140 print "Subject: " . to_utf8($co{'title'}) . "\n";
6142 print "X-Git-Tag: $tagname\n" if $tagname;
6143 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6145 foreach my $line (@{$co{'comment'}}) {
6146 print to_utf8($line) . "\n";
6148 print "---\n\n";
6149 } elsif ($format eq 'patch') {
6150 my $filename = basename($project) . "-$hash.patch";
6152 print $cgi->header(
6153 -type => 'text/plain',
6154 -charset => 'utf-8',
6155 -expires => $expires,
6156 -content_disposition => 'inline; filename="' . "$filename" . '"');
6159 # write patch
6160 if ($format eq 'html') {
6161 my $use_parents = !defined $hash_parent ||
6162 $hash_parent eq '-c' || $hash_parent eq '--cc';
6163 git_difftree_body(\@difftree, $hash,
6164 $use_parents ? @{$co{'parents'}} : $hash_parent);
6165 print "<br/>\n";
6167 git_patchset_body($fd, \@difftree, $hash,
6168 $use_parents ? @{$co{'parents'}} : $hash_parent);
6169 close $fd;
6170 print "</div>\n"; # class="page_body"
6171 git_footer_html();
6173 } elsif ($format eq 'plain') {
6174 local $/ = undef;
6175 print <$fd>;
6176 close $fd
6177 or print "Reading git-diff-tree failed\n";
6178 } elsif ($format eq 'patch') {
6179 local $/ = undef;
6180 print <$fd>;
6181 close $fd
6182 or print "Reading git-format-patch failed\n";
6186 sub git_commitdiff_plain {
6187 git_commitdiff(-format => 'plain');
6190 # format-patch-style patches
6191 sub git_patch {
6192 git_commitdiff(-format => 'patch', -single => 1);
6195 sub git_patches {
6196 git_commitdiff(-format => 'patch');
6199 sub git_history {
6200 if (!defined $hash_base) {
6201 $hash_base = git_get_head_hash($project);
6203 if (!defined $page) {
6204 $page = 0;
6206 my $ftype;
6207 my %co = parse_commit($hash_base)
6208 or die_error(404, "Unknown commit object");
6210 my $refs = git_get_references();
6211 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
6213 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
6214 $file_name, "--full-history")
6215 or die_error(404, "No such file or directory on given branch");
6217 if (!defined $hash && defined $file_name) {
6218 # some commits could have deleted file in question,
6219 # and not have it in tree, but one of them has to have it
6220 for (my $i = 0; $i <= @commitlist; $i++) {
6221 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
6222 last if defined $hash;
6225 if (defined $hash) {
6226 $ftype = git_get_type($hash);
6228 if (!defined $ftype) {
6229 die_error(500, "Unknown type of object");
6232 my $paging_nav = '';
6233 if ($page > 0) {
6234 $paging_nav .=
6235 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
6236 file_name=>$file_name)},
6237 "first");
6238 $paging_nav .= " &sdot; " .
6239 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6240 -accesskey => "p", -title => "Alt-p"}, "prev");
6241 } else {
6242 $paging_nav .= "first";
6243 $paging_nav .= " &sdot; prev";
6245 my $next_link = '';
6246 if ($#commitlist >= 100) {
6247 $next_link =
6248 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6249 -accesskey => "n", -title => "Alt-n"}, "next");
6250 $paging_nav .= " &sdot; $next_link";
6251 } else {
6252 $paging_nav .= " &sdot; next";
6255 git_header_html();
6256 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
6257 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6258 git_print_page_path($file_name, $ftype, $hash_base);
6260 git_history_body(\@commitlist, 0, 99,
6261 $refs, $hash_base, $ftype, $next_link);
6263 git_footer_html();
6266 sub git_search {
6267 gitweb_check_feature('search') or die_error(403, "Search is disabled");
6268 if (!defined $searchtext) {
6269 die_error(400, "Text field is empty");
6271 if (!defined $hash) {
6272 $hash = git_get_head_hash($project);
6274 my %co = parse_commit($hash);
6275 if (!%co) {
6276 die_error(404, "Unknown commit object");
6278 if (!defined $page) {
6279 $page = 0;
6282 $searchtype ||= 'commit';
6283 if ($searchtype eq 'pickaxe') {
6284 # pickaxe may take all resources of your box and run for several minutes
6285 # with every query - so decide by yourself how public you make this feature
6286 gitweb_check_feature('pickaxe')
6287 or die_error(403, "Pickaxe is disabled");
6289 if ($searchtype eq 'grep') {
6290 gitweb_check_feature('grep')
6291 or die_error(403, "Grep is disabled");
6294 git_header_html();
6296 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6297 my $greptype;
6298 if ($searchtype eq 'commit') {
6299 $greptype = "--grep=";
6300 } elsif ($searchtype eq 'author') {
6301 $greptype = "--author=";
6302 } elsif ($searchtype eq 'committer') {
6303 $greptype = "--committer=";
6305 $greptype .= $searchtext;
6306 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6307 $greptype, '--regexp-ignore-case',
6308 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6310 my $paging_nav = '';
6311 if ($page > 0) {
6312 $paging_nav .=
6313 $cgi->a({-href => href(action=>"search", hash=>$hash,
6314 searchtext=>$searchtext,
6315 searchtype=>$searchtype)},
6316 "first");
6317 $paging_nav .= " &sdot; " .
6318 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6319 -accesskey => "p", -title => "Alt-p"}, "prev");
6320 } else {
6321 $paging_nav .= "first";
6322 $paging_nav .= " &sdot; prev";
6324 my $next_link = '';
6325 if ($#commitlist >= 100) {
6326 $next_link =
6327 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6328 -accesskey => "n", -title => "Alt-n"}, "next");
6329 $paging_nav .= " &sdot; $next_link";
6330 } else {
6331 $paging_nav .= " &sdot; next";
6334 if ($#commitlist >= 100) {
6337 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6338 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6339 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6342 if ($searchtype eq 'pickaxe') {
6343 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6344 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6346 print "<table class=\"pickaxe search\">\n";
6347 my $alternate = 1;
6348 local $/ = "\n";
6349 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6350 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6351 ($search_use_regexp ? '--pickaxe-regex' : ());
6352 undef %co;
6353 my @files;
6354 while (my $line = <$fd>) {
6355 chomp $line;
6356 next unless $line;
6358 my %set = parse_difftree_raw_line($line);
6359 if (defined $set{'commit'}) {
6360 # finish previous commit
6361 if (%co) {
6362 print "</td>\n" .
6363 "<td class=\"link\">" .
6364 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6365 " | " .
6366 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6367 print "</td>\n" .
6368 "</tr>\n";
6371 if ($alternate) {
6372 print "<tr class=\"dark\">\n";
6373 } else {
6374 print "<tr class=\"light\">\n";
6376 $alternate ^= 1;
6377 %co = parse_commit($set{'commit'});
6378 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6379 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6380 "<td><i>$author</i></td>\n" .
6381 "<td>" .
6382 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6383 -class => "list subject"},
6384 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6385 } elsif (defined $set{'to_id'}) {
6386 next if ($set{'to_id'} =~ m/^0{40}$/);
6388 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6389 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6390 -class => "list"},
6391 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6392 "<br/>\n";
6395 close $fd;
6397 # finish last commit (warning: repetition!)
6398 if (%co) {
6399 print "</td>\n" .
6400 "<td class=\"link\">" .
6401 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6402 " | " .
6403 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6404 print "</td>\n" .
6405 "</tr>\n";
6408 print "</table>\n";
6411 if ($searchtype eq 'grep') {
6412 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6413 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6415 print "<table class=\"grep_search\">\n";
6416 my $alternate = 1;
6417 my $matches = 0;
6418 local $/ = "\n";
6419 open my $fd, "-|", git_cmd(), 'grep', '-n',
6420 $search_use_regexp ? ('-E', '-i') : '-F',
6421 $searchtext, $co{'tree'};
6422 my $lastfile = '';
6423 while (my $line = <$fd>) {
6424 chomp $line;
6425 my ($file, $lno, $ltext, $binary);
6426 last if ($matches++ > 1000);
6427 if ($line =~ /^Binary file (.+) matches$/) {
6428 $file = $1;
6429 $binary = 1;
6430 } else {
6431 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6433 if ($file ne $lastfile) {
6434 $lastfile and print "</td></tr>\n";
6435 if ($alternate++) {
6436 print "<tr class=\"dark\">\n";
6437 } else {
6438 print "<tr class=\"light\">\n";
6440 print "<td class=\"list\">".
6441 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6442 file_name=>"$file"),
6443 -class => "list"}, esc_path($file));
6444 print "</td><td>\n";
6445 $lastfile = $file;
6447 if ($binary) {
6448 print "<div class=\"binary\">Binary file</div>\n";
6449 } else {
6450 $ltext = untabify($ltext);
6451 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6452 $ltext = esc_html($1, -nbsp=>1);
6453 $ltext .= '<span class="match">';
6454 $ltext .= esc_html($2, -nbsp=>1);
6455 $ltext .= '</span>';
6456 $ltext .= esc_html($3, -nbsp=>1);
6457 } else {
6458 $ltext = esc_html($ltext, -nbsp=>1);
6460 print "<div class=\"pre\">" .
6461 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6462 file_name=>"$file").'#l'.$lno,
6463 -class => "linenr"}, sprintf('%4i', $lno))
6464 . ' ' . $ltext . "</div>\n";
6467 if ($lastfile) {
6468 print "</td></tr>\n";
6469 if ($matches > 1000) {
6470 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6472 } else {
6473 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6475 close $fd;
6477 print "</table>\n";
6479 git_footer_html();
6482 sub git_search_help {
6483 git_header_html();
6484 git_print_page_nav('','', $hash,$hash,$hash);
6485 print <<EOT;
6486 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6487 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6488 the pattern entered is recognized as the POSIX extended
6489 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6490 insensitive).</p>
6491 <dl>
6492 <dt><b>commit</b></dt>
6493 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6495 my $have_grep = gitweb_check_feature('grep');
6496 if ($have_grep) {
6497 print <<EOT;
6498 <dt><b>grep</b></dt>
6499 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6500 a different one) are searched for the given pattern. On large trees, this search can take
6501 a while and put some strain on the server, so please use it with some consideration. Note that
6502 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6503 case-sensitive.</dd>
6506 print <<EOT;
6507 <dt><b>author</b></dt>
6508 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6509 <dt><b>committer</b></dt>
6510 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6512 my $have_pickaxe = gitweb_check_feature('pickaxe');
6513 if ($have_pickaxe) {
6514 print <<EOT;
6515 <dt><b>pickaxe</b></dt>
6516 <dd>All commits that caused the string to appear or disappear from any file (changes that
6517 added, removed or "modified" the string) will be listed. This search can take a while and
6518 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6519 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6522 print "</dl>\n";
6523 git_footer_html();
6526 sub git_shortlog {
6527 my $head = git_get_head_hash($project);
6528 if (!defined $hash) {
6529 $hash = $head;
6531 if (!defined $page) {
6532 $page = 0;
6534 my $refs = git_get_references();
6536 my $commit_hash = $hash;
6537 if (defined $hash_parent) {
6538 $commit_hash = "$hash_parent..$hash";
6540 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
6542 my $paging_nav = format_log_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
6544 my $next_link = '';
6545 if ($#commitlist >= 100) {
6546 $next_link =
6547 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6548 -accesskey => "n", -title => "Alt-n"}, "next");
6550 my $patch_max = gitweb_check_feature('patches');
6551 if ($patch_max) {
6552 if ($patch_max < 0 || @commitlist <= $patch_max) {
6553 $paging_nav .= " &sdot; " .
6554 $cgi->a({-href => href(action=>"patches", -replay=>1)},
6555 "patches");
6559 git_header_html();
6560 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
6561 git_print_header_div('summary', $project);
6563 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
6565 git_footer_html();
6568 ## ......................................................................
6569 ## feeds (RSS, Atom; OPML)
6571 sub git_feed {
6572 my $format = shift || 'atom';
6573 my $have_blame = gitweb_check_feature('blame');
6575 # Atom: http://www.atomenabled.org/developers/syndication/
6576 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6577 if ($format ne 'rss' && $format ne 'atom') {
6578 die_error(400, "Unknown web feed format");
6581 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6582 my $head = $hash || 'HEAD';
6583 my @commitlist = parse_commits($head, 150, 0, $file_name);
6585 my %latest_commit;
6586 my %latest_date;
6587 my $content_type = "application/$format+xml";
6588 if (defined $cgi->http('HTTP_ACCEPT') &&
6589 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6590 # browser (feed reader) prefers text/xml
6591 $content_type = 'text/xml';
6593 if (defined($commitlist[0])) {
6594 %latest_commit = %{$commitlist[0]};
6595 my $latest_epoch = $latest_commit{'committer_epoch'};
6596 %latest_date = parse_date($latest_epoch);
6597 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6598 if (defined $if_modified) {
6599 my $since;
6600 if (eval { require HTTP::Date; 1; }) {
6601 $since = HTTP::Date::str2time($if_modified);
6602 } elsif (eval { require Time::ParseDate; 1; }) {
6603 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6605 if (defined $since && $latest_epoch <= $since) {
6606 print $cgi->header(
6607 -type => $content_type,
6608 -charset => 'utf-8',
6609 -last_modified => $latest_date{'rfc2822'},
6610 -status => '304 Not Modified');
6611 return;
6614 print $cgi->header(
6615 -type => $content_type,
6616 -charset => 'utf-8',
6617 -last_modified => $latest_date{'rfc2822'});
6618 } else {
6619 print $cgi->header(
6620 -type => $content_type,
6621 -charset => 'utf-8');
6624 # Optimization: skip generating the body if client asks only
6625 # for Last-Modified date.
6626 return if ($cgi->request_method() eq 'HEAD');
6628 # header variables
6629 my $title = "$site_name - $project/$action";
6630 my $feed_type = 'log';
6631 if (defined $hash) {
6632 $title .= " - '$hash'";
6633 $feed_type = 'branch log';
6634 if (defined $file_name) {
6635 $title .= " :: $file_name";
6636 $feed_type = 'history';
6638 } elsif (defined $file_name) {
6639 $title .= " - $file_name";
6640 $feed_type = 'history';
6642 $title .= " $feed_type";
6643 my $descr = git_get_project_description($project);
6644 if (defined $descr) {
6645 $descr = esc_html($descr);
6646 } else {
6647 $descr = "$project " .
6648 ($format eq 'rss' ? 'RSS' : 'Atom') .
6649 " feed";
6651 my $owner = git_get_project_owner($project);
6652 $owner = esc_html($owner);
6654 #header
6655 my $alt_url;
6656 if (defined $file_name) {
6657 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6658 } elsif (defined $hash) {
6659 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6660 } else {
6661 $alt_url = href(-full=>1, action=>"summary");
6663 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6664 if ($format eq 'rss') {
6665 print <<XML;
6666 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6667 <channel>
6669 print "<title>$title</title>\n" .
6670 "<link>$alt_url</link>\n" .
6671 "<description>$descr</description>\n" .
6672 "<language>en</language>\n" .
6673 # project owner is responsible for 'editorial' content
6674 "<managingEditor>$owner</managingEditor>\n";
6675 if (defined $logo || defined $favicon) {
6676 # prefer the logo to the favicon, since RSS
6677 # doesn't allow both
6678 my $img = esc_url($logo || $favicon);
6679 print "<image>\n" .
6680 "<url>$img</url>\n" .
6681 "<title>$title</title>\n" .
6682 "<link>$alt_url</link>\n" .
6683 "</image>\n";
6685 if (%latest_date) {
6686 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6687 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6689 print "<generator>gitweb v.$version/$git_version</generator>\n";
6690 } elsif ($format eq 'atom') {
6691 print <<XML;
6692 <feed xmlns="http://www.w3.org/2005/Atom">
6694 print "<title>$title</title>\n" .
6695 "<subtitle>$descr</subtitle>\n" .
6696 '<link rel="alternate" type="text/html" href="' .
6697 $alt_url . '" />' . "\n" .
6698 '<link rel="self" type="' . $content_type . '" href="' .
6699 $cgi->self_url() . '" />' . "\n" .
6700 "<id>" . href(-full=>1) . "</id>\n" .
6701 # use project owner for feed author
6702 '<author><name>'. email_obfuscate($owner) . '</name></author>\n';
6703 if (defined $favicon) {
6704 print "<icon>" . esc_url($favicon) . "</icon>\n";
6706 if (defined $logo_url) {
6707 # not twice as wide as tall: 72 x 27 pixels
6708 print "<logo>" . esc_url($logo) . "</logo>\n";
6710 if (! %latest_date) {
6711 # dummy date to keep the feed valid until commits trickle in:
6712 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6713 } else {
6714 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6716 print "<generator version='$version/$git_version'>gitweb</generator>\n";
6719 # contents
6720 for (my $i = 0; $i <= $#commitlist; $i++) {
6721 my %co = %{$commitlist[$i]};
6722 my $commit = $co{'id'};
6723 # we read 150, we always show 30 and the ones more recent than 48 hours
6724 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6725 last;
6727 my %cd = parse_date($co{'author_epoch'});
6729 # get list of changed files
6730 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6731 $co{'parent'} || "--root",
6732 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6733 or next;
6734 my @difftree = map { chomp; $_ } <$fd>;
6735 close $fd
6736 or next;
6738 # print element (entry, item)
6739 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6740 if ($format eq 'rss') {
6741 print "<item>\n" .
6742 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6743 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6744 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6745 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6746 "<link>$co_url</link>\n" .
6747 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6748 "<content:encoded>" .
6749 "<![CDATA[\n";
6750 } elsif ($format eq 'atom') {
6751 print "<entry>\n" .
6752 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6753 "<updated>$cd{'iso-8601'}</updated>\n" .
6754 "<author>\n" .
6755 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6756 if ($co{'author_email'}) {
6757 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6759 print "</author>\n" .
6760 # use committer for contributor
6761 "<contributor>\n" .
6762 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6763 if ($co{'committer_email'}) {
6764 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6766 print "</contributor>\n" .
6767 "<published>$cd{'iso-8601'}</published>\n" .
6768 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6769 "<id>$co_url</id>\n" .
6770 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6771 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6773 my $comment = $co{'comment'};
6774 print "<pre>\n";
6775 foreach my $line (@$comment) {
6776 $line = esc_html($line);
6777 print "$line\n";
6779 print "</pre><ul>\n";
6780 foreach my $difftree_line (@difftree) {
6781 my %difftree = parse_difftree_raw_line($difftree_line);
6782 next if !$difftree{'from_id'};
6784 my $file = $difftree{'file'} || $difftree{'to_file'};
6786 print "<li>" .
6787 "[" .
6788 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6789 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6790 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6791 file_name=>$file, file_parent=>$difftree{'from_file'}),
6792 -title => "diff"}, 'D');
6793 if ($have_blame) {
6794 print $cgi->a({-href => href(-full=>1, action=>"blame",
6795 file_name=>$file, hash_base=>$commit), -class => "blamelink",
6796 -title => "blame"}, 'B');
6798 # if this is not a feed of a file history
6799 if (!defined $file_name || $file_name ne $file) {
6800 print $cgi->a({-href => href(-full=>1, action=>"history",
6801 file_name=>$file, hash=>$commit),
6802 -title => "history"}, 'H');
6804 $file = esc_path($file);
6805 print "] ".
6806 "$file</li>\n";
6808 if ($format eq 'rss') {
6809 print "</ul>]]>\n" .
6810 "</content:encoded>\n" .
6811 "</item>\n";
6812 } elsif ($format eq 'atom') {
6813 print "</ul>\n</div>\n" .
6814 "</content>\n" .
6815 "</entry>\n";
6819 # end of feed
6820 if ($format eq 'rss') {
6821 print "</channel>\n</rss>\n";
6822 } elsif ($format eq 'atom') {
6823 print "</feed>\n";
6827 sub git_rss {
6828 git_feed('rss');
6831 sub git_atom {
6832 git_feed('atom');
6835 sub git_opml {
6836 my @list = git_get_projects_list();
6838 print $cgi->header(
6839 -type => 'text/xml',
6840 -charset => 'utf-8',
6841 -content_disposition => 'inline; filename="opml.xml"');
6843 print <<XML;
6844 <?xml version="1.0" encoding="utf-8"?>
6845 <opml version="1.0">
6846 <head>
6847 <title>$site_name OPML Export</title>
6848 </head>
6849 <body>
6850 <outline text="git RSS feeds">
6853 foreach my $pr (@list) {
6854 my %proj = %$pr;
6855 my $head = git_get_head_hash($proj{'path'});
6856 if (!defined $head) {
6857 next;
6859 $git_dir = "$projectroot/$proj{'path'}";
6860 my %co = parse_commit($head);
6861 if (!%co) {
6862 next;
6865 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6866 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
6867 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
6868 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6870 print <<XML;
6871 </outline>
6872 </body>
6873 </opml>