Merge branch 'pu' into refs/top-bases/girocco
[git/gitweb.git] / gitweb / gitweb.perl
blob56e1b71aa1067e81c41a61768885dd2757bfc9c6
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 # projects list cache for busy sites with many projects;
162 # if you set this to non-zero, it will be used as the cached
163 # index lifetime in minutes
165 # the cached list version is stored in $cache_dir/$cache_name and can
166 # be tweaked by other scripts running with the same uid as gitweb -
167 # use this ONLY at secure installations; only single gitweb project
168 # root per system is supported, unless you tweak configuration!
169 our $projlist_cache_lifetime = 0; # in minutes
170 # FHS compliant $cache_dir would be "/var/cache/gitweb"
171 our $cache_dir =
172 (defined $ENV{'TMPDIR'} ? $ENV{'TMPDIR'} : '/tmp').'/gitweb';
173 our $projlist_cache_name = 'gitweb.index.cache';
175 # information about snapshot formats that gitweb is capable of serving
176 our %known_snapshot_formats = (
177 # name => {
178 # 'display' => display name,
179 # 'type' => mime type,
180 # 'suffix' => filename suffix,
181 # 'format' => --format for git-archive,
182 # 'compressor' => [compressor command and arguments]
183 # (array reference, optional)
184 # 'disabled' => boolean (optional)}
186 'tgz' => {
187 'display' => 'tar.gz',
188 'type' => 'application/x-gzip',
189 'suffix' => '.tar.gz',
190 'format' => 'tar',
191 'compressor' => ['gzip']},
193 'tbz2' => {
194 'display' => 'tar.bz2',
195 'type' => 'application/x-bzip2',
196 'suffix' => '.tar.bz2',
197 'format' => 'tar',
198 'compressor' => ['bzip2']},
200 'txz' => {
201 'display' => 'tar.xz',
202 'type' => 'application/x-xz',
203 'suffix' => '.tar.xz',
204 'format' => 'tar',
205 'compressor' => ['xz'],
206 'disabled' => 1},
208 'zip' => {
209 'display' => 'zip',
210 'type' => 'application/x-zip',
211 'suffix' => '.zip',
212 'format' => 'zip'},
215 # Aliases so we understand old gitweb.snapshot values in repository
216 # configuration.
217 our %known_snapshot_format_aliases = (
218 'gzip' => 'tgz',
219 'bzip2' => 'tbz2',
220 'xz' => 'txz',
222 # backward compatibility: legacy gitweb config support
223 'x-gzip' => undef, 'gz' => undef,
224 'x-bzip2' => undef, 'bz2' => undef,
225 'x-zip' => undef, '' => undef,
228 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
229 # are changed, it may be appropriate to change these values too via
230 # $GITWEB_CONFIG.
231 our %avatar_size = (
232 'default' => 16,
233 'double' => 32
236 # You define site-wide feature defaults here; override them with
237 # $GITWEB_CONFIG as necessary.
238 our %feature = (
239 # feature => {
240 # 'sub' => feature-sub (subroutine),
241 # 'override' => allow-override (boolean),
242 # 'default' => [ default options...] (array reference)}
244 # if feature is overridable (it means that allow-override has true value),
245 # then feature-sub will be called with default options as parameters;
246 # return value of feature-sub indicates if to enable specified feature
248 # if there is no 'sub' key (no feature-sub), then feature cannot be
249 # overriden
251 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
252 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
253 # is enabled
255 # Enable the 'blame' blob view, showing the last commit that modified
256 # each line in the file. This can be very CPU-intensive.
258 # To enable system wide have in $GITWEB_CONFIG
259 # $feature{'blame'}{'default'} = [1];
260 # To have project specific config enable override in $GITWEB_CONFIG
261 # $feature{'blame'}{'override'} = 1;
262 # and in project config gitweb.blame = 0|1;
263 'blame' => {
264 'sub' => sub { feature_bool('blame', @_) },
265 'override' => 0,
266 'default' => [0]},
268 # Enable the 'incremental blame' blob view, which uses javascript to
269 # incrementally show the revisions of lines as they are discovered
270 # in the history. It is better for large histories, files and slow
271 # servers, but requires javascript in the client, can slow down the
272 # browser on large files and does not show author initials.
274 # To enable system wide have in $GITWEB_CONFIG
275 # $feature{'blame_incremental'}{'default'} = [1];
276 # To have project specific config enable override in $GITWEB_CONFIG
277 # $feature{'blame_incremental'}{'override'} = 1;
278 # and in project config gitweb.blame_incremental = 0|1;
279 'blame_incremental' => {
280 'sub' => sub { feature_bool('blame_incremental', @_) },
281 'override' => 0,
282 'default' => [0]},
284 # Enable the 'snapshot' link, providing a compressed archive of any
285 # tree. This can potentially generate high traffic if you have large
286 # project.
288 # Value is a list of formats defined in %known_snapshot_formats that
289 # you wish to offer.
290 # To disable system wide have in $GITWEB_CONFIG
291 # $feature{'snapshot'}{'default'} = [];
292 # To have project specific config enable override in $GITWEB_CONFIG
293 # $feature{'snapshot'}{'override'} = 1;
294 # and in project config, a comma-separated list of formats or "none"
295 # to disable. Example: gitweb.snapshot = tbz2,zip;
296 'snapshot' => {
297 'sub' => \&feature_snapshot,
298 'override' => 0,
299 'default' => ['tgz']},
301 # Enable text search, which will list the commits which match author,
302 # committer or commit text to a given string. Enabled by default.
303 # Project specific override is not supported.
304 'search' => {
305 'override' => 0,
306 'default' => [1]},
308 # Enable grep search, which will list the files in currently selected
309 # tree containing the given string. Enabled by default. This can be
310 # potentially CPU-intensive, of course.
312 # To enable system wide have in $GITWEB_CONFIG
313 # $feature{'grep'}{'default'} = [1];
314 # To have project specific config enable override in $GITWEB_CONFIG
315 # $feature{'grep'}{'override'} = 1;
316 # and in project config gitweb.grep = 0|1;
317 'grep' => {
318 'sub' => sub { feature_bool('grep', @_) },
319 'override' => 0,
320 'default' => [1]},
322 # Enable the pickaxe search, which will list the commits that modified
323 # a given string in a file. This can be practical and quite faster
324 # alternative to 'blame', but still potentially CPU-intensive.
326 # To enable system wide have in $GITWEB_CONFIG
327 # $feature{'pickaxe'}{'default'} = [1];
328 # To have project specific config enable override in $GITWEB_CONFIG
329 # $feature{'pickaxe'}{'override'} = 1;
330 # and in project config gitweb.pickaxe = 0|1;
331 'pickaxe' => {
332 'sub' => sub { feature_bool('pickaxe', @_) },
333 'override' => 0,
334 'default' => [1]},
336 # Enable showing size of blobs in a 'tree' view, in a separate
337 # column, similar to what 'ls -l' does. This cost a bit of IO.
339 # To disable system wide have in $GITWEB_CONFIG
340 # $feature{'show-sizes'}{'default'} = [0];
341 # To have project specific config enable override in $GITWEB_CONFIG
342 # $feature{'show-sizes'}{'override'} = 1;
343 # and in project config gitweb.showsizes = 0|1;
344 'show-sizes' => {
345 'sub' => sub { feature_bool('showsizes', @_) },
346 'override' => 0,
347 'default' => [1]},
349 # Make gitweb use an alternative format of the URLs which can be
350 # more readable and natural-looking: project name is embedded
351 # directly in the path and the query string contains other
352 # auxiliary information. All gitweb installations recognize
353 # URL in either format; this configures in which formats gitweb
354 # generates links.
356 # To enable system wide have in $GITWEB_CONFIG
357 # $feature{'pathinfo'}{'default'} = [1];
358 # Project specific override is not supported.
360 # Note that you will need to change the default location of CSS,
361 # favicon, logo and possibly other files to an absolute URL. Also,
362 # if gitweb.cgi serves as your indexfile, you will need to force
363 # $my_uri to contain the script name in your $GITWEB_CONFIG.
364 'pathinfo' => {
365 'override' => 0,
366 'default' => [0]},
368 # Make gitweb consider projects in project root subdirectories
369 # to be forks of existing projects. Given project $projname.git,
370 # projects matching $projname/*.git will not be shown in the main
371 # projects list, instead a '+' mark will be added to $projname
372 # there and a 'forks' view will be enabled for the project, listing
373 # all the forks. If project list is taken from a file, forks have
374 # to be listed after the main project.
376 # To enable system wide have in $GITWEB_CONFIG
377 # $feature{'forks'}{'default'} = [1];
378 # Project specific override is not supported.
379 'forks' => {
380 'override' => 0,
381 'default' => [0]},
383 # Insert custom links to the action bar of all project pages.
384 # This enables you mainly to link to third-party scripts integrating
385 # into gitweb; e.g. git-browser for graphical history representation
386 # or custom web-based repository administration interface.
388 # The 'default' value consists of a list of triplets in the form
389 # (label, link, position) where position is the label after which
390 # to insert the link and link is a format string where %n expands
391 # to the project name, %f to the project path within the filesystem,
392 # %h to the current hash (h gitweb parameter) and %b to the current
393 # hash base (hb gitweb parameter); %% expands to %.
395 # To enable system wide have in $GITWEB_CONFIG e.g.
396 # $feature{'actions'}{'default'} = [('graphiclog',
397 # '/git-browser/by-commit.html?r=%n', 'summary')];
398 # Project specific override is not supported.
399 'actions' => {
400 'override' => 0,
401 'default' => []},
403 # Allow gitweb scan project content tags described in ctags/
404 # of project repository, and display the popular Web 2.0-ish
405 # "tag cloud" near the project list. Note that this is something
406 # COMPLETELY different from the normal Git tags.
408 # gitweb by itself can show existing tags, but it does not handle
409 # tagging itself; you need an external application for that.
410 # For an example script, check Girocco's cgi/tagproj.cgi.
411 # You may want to install the HTML::TagCloud Perl module to get
412 # a pretty tag cloud instead of just a list of tags.
414 # To enable system wide have in $GITWEB_CONFIG
415 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
416 # Project specific override is not supported.
417 'ctags' => {
418 'override' => 0,
419 'default' => [0]},
421 # The maximum number of patches in a patchset generated in patch
422 # view. Set this to 0 or undef to disable patch view, or to a
423 # negative number to remove any limit.
425 # To disable system wide have in $GITWEB_CONFIG
426 # $feature{'patches'}{'default'} = [0];
427 # To have project specific config enable override in $GITWEB_CONFIG
428 # $feature{'patches'}{'override'} = 1;
429 # and in project config gitweb.patches = 0|n;
430 # where n is the maximum number of patches allowed in a patchset.
431 'patches' => {
432 'sub' => \&feature_patches,
433 'override' => 0,
434 'default' => [16]},
436 # Avatar support. When this feature is enabled, views such as
437 # shortlog or commit will display an avatar associated with
438 # the email of the committer(s) and/or author(s).
440 # Currently available providers are gravatar and picon.
441 # If an unknown provider is specified, the feature is disabled.
443 # Gravatar depends on Digest::MD5.
444 # Picon currently relies on the indiana.edu database.
446 # To enable system wide have in $GITWEB_CONFIG
447 # $feature{'avatar'}{'default'} = ['<provider>'];
448 # where <provider> is either gravatar or picon.
449 # To have project specific config enable override in $GITWEB_CONFIG
450 # $feature{'avatar'}{'override'} = 1;
451 # and in project config gitweb.avatar = <provider>;
452 'avatar' => {
453 'sub' => \&feature_avatar,
454 'override' => 0,
455 'default' => ['']},
458 # email obfuscation
459 our $email;
460 if (eval { require HTML::Email::Obfuscate; 1 }) {
461 $email = HTML::Email::Obfuscate->new(lite => 1);
464 sub gitweb_get_feature {
465 my ($name) = @_;
466 return unless exists $feature{$name};
467 my ($sub, $override, @defaults) = (
468 $feature{$name}{'sub'},
469 $feature{$name}{'override'},
470 @{$feature{$name}{'default'}});
471 if (!$override) { return @defaults; }
472 if (!defined $sub) {
473 warn "feature $name is not overridable";
474 return @defaults;
476 return $sub->(@defaults);
479 # A wrapper to check if a given feature is enabled.
480 # With this, you can say
482 # my $bool_feat = gitweb_check_feature('bool_feat');
483 # gitweb_check_feature('bool_feat') or somecode;
485 # instead of
487 # my ($bool_feat) = gitweb_get_feature('bool_feat');
488 # (gitweb_get_feature('bool_feat'))[0] or somecode;
490 sub gitweb_check_feature {
491 return (gitweb_get_feature(@_))[0];
495 sub feature_bool {
496 my $key = shift;
497 my ($val) = git_get_project_config($key, '--bool');
499 if (!defined $val) {
500 return ($_[0]);
501 } elsif ($val eq 'true') {
502 return (1);
503 } elsif ($val eq 'false') {
504 return (0);
508 sub feature_snapshot {
509 my (@fmts) = @_;
511 my ($val) = git_get_project_config('snapshot');
513 if ($val) {
514 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
517 return @fmts;
520 sub feature_patches {
521 my @val = (git_get_project_config('patches', '--int'));
523 if (@val) {
524 return @val;
527 return ($_[0]);
530 sub feature_avatar {
531 my @val = (git_get_project_config('avatar'));
533 return @val ? @val : @_;
536 # checking HEAD file with -e is fragile if the repository was
537 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
538 # and then pruned.
539 sub check_head_link {
540 my ($dir) = @_;
541 my $headfile = "$dir/HEAD";
542 return ((-e $headfile) ||
543 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
546 sub check_export_ok {
547 my ($dir) = @_;
548 return (check_head_link($dir) &&
549 (!$export_ok || -e "$dir/$export_ok") &&
550 (!$export_auth_hook || $export_auth_hook->($dir)));
553 # process alternate names for backward compatibility
554 # filter out unsupported (unknown) snapshot formats
555 sub filter_snapshot_fmts {
556 my @fmts = @_;
558 @fmts = map {
559 exists $known_snapshot_format_aliases{$_} ?
560 $known_snapshot_format_aliases{$_} : $_} @fmts;
561 @fmts = grep {
562 exists $known_snapshot_formats{$_} &&
563 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
566 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
567 if (-e $GITWEB_CONFIG) {
568 do $GITWEB_CONFIG;
569 } else {
570 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
571 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
574 # version of the core git binary
575 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
577 $projects_list ||= $projectroot;
579 # ======================================================================
580 # input validation and dispatch
582 # input parameters can be collected from a variety of sources (presently, CGI
583 # and PATH_INFO), so we define an %input_params hash that collects them all
584 # together during validation: this allows subsequent uses (e.g. href()) to be
585 # agnostic of the parameter origin
587 our %input_params = ();
589 # input parameters are stored with the long parameter name as key. This will
590 # also be used in the href subroutine to convert parameters to their CGI
591 # equivalent, and since the href() usage is the most frequent one, we store
592 # the name -> CGI key mapping here, instead of the reverse.
594 # XXX: Warning: If you touch this, check the search form for updating,
595 # too.
597 our @cgi_param_mapping = (
598 project => "p",
599 action => "a",
600 file_name => "f",
601 file_parent => "fp",
602 hash => "h",
603 hash_parent => "hp",
604 hash_base => "hb",
605 hash_parent_base => "hpb",
606 page => "pg",
607 order => "o",
608 searchtext => "s",
609 searchtype => "st",
610 snapshot_format => "sf",
611 extra_options => "opt",
612 search_use_regexp => "sr",
614 our %cgi_param_mapping = @cgi_param_mapping;
616 # we will also need to know the possible actions, for validation
617 our %actions = (
618 "blame" => \&git_blame,
619 "blame_incremental" => \&git_blame_incremental,
620 "blame_data" => \&git_blame_data,
621 "blobdiff" => \&git_blobdiff,
622 "blobdiff_plain" => \&git_blobdiff_plain,
623 "blob" => \&git_blob,
624 "blob_plain" => \&git_blob_plain,
625 "commitdiff" => \&git_commitdiff,
626 "commitdiff_plain" => \&git_commitdiff_plain,
627 "commit" => \&git_commit,
628 "forks" => \&git_forks,
629 "heads" => \&git_heads,
630 "history" => \&git_history,
631 "log" => \&git_log,
632 "patch" => \&git_patch,
633 "patches" => \&git_patches,
634 "rss" => \&git_rss,
635 "atom" => \&git_atom,
636 "search" => \&git_search,
637 "search_help" => \&git_search_help,
638 "shortlog" => \&git_shortlog,
639 "summary" => \&git_summary,
640 "tag" => \&git_tag,
641 "tags" => \&git_tags,
642 "tree" => \&git_tree,
643 "snapshot" => \&git_snapshot,
644 "object" => \&git_object,
645 # those below don't need $project
646 "opml" => \&git_opml,
647 "project_list" => \&git_project_list,
648 "project_index" => \&git_project_index,
651 # finally, we have the hash of allowed extra_options for the commands that
652 # allow them
653 our %allowed_options = (
654 "--no-merges" => [ qw(rss atom log shortlog history) ],
657 # fill %input_params with the CGI parameters. All values except for 'opt'
658 # should be single values, but opt can be an array. We should probably
659 # build an array of parameters that can be multi-valued, but since for the time
660 # being it's only this one, we just single it out
661 while (my ($name, $symbol) = each %cgi_param_mapping) {
662 if ($symbol eq 'opt') {
663 $input_params{$name} = [ $cgi->param($symbol) ];
664 } else {
665 $input_params{$name} = $cgi->param($symbol);
669 # now read PATH_INFO and update the parameter list for missing parameters
670 sub evaluate_path_info {
671 return if defined $input_params{'project'};
672 return if !$path_info;
673 $path_info =~ s,^/+,,;
674 return if !$path_info;
676 # find which part of PATH_INFO is project
677 my $project = $path_info;
678 $project =~ s,/+$,,;
679 while ($project && !check_head_link("$projectroot/$project")) {
680 $project =~ s,/*[^/]*$,,;
682 return unless $project;
683 $input_params{'project'} = $project;
685 # do not change any parameters if an action is given using the query string
686 return if $input_params{'action'};
687 $path_info =~ s,^\Q$project\E/*,,;
689 # next, check if we have an action
690 my $action = $path_info;
691 $action =~ s,/.*$,,;
692 if (exists $actions{$action}) {
693 $path_info =~ s,^$action/*,,;
694 $input_params{'action'} = $action;
697 # list of actions that want hash_base instead of hash, but can have no
698 # pathname (f) parameter
699 my @wants_base = (
700 'tree',
701 'history',
704 # we want to catch
705 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
706 my ($parentrefname, $parentpathname, $refname, $pathname) =
707 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
709 # first, analyze the 'current' part
710 if (defined $pathname) {
711 # we got "branch:filename" or "branch:dir/"
712 # we could use git_get_type(branch:pathname), but:
713 # - it needs $git_dir
714 # - it does a git() call
715 # - the convention of terminating directories with a slash
716 # makes it superfluous
717 # - embedding the action in the PATH_INFO would make it even
718 # more superfluous
719 $pathname =~ s,^/+,,;
720 if (!$pathname || substr($pathname, -1) eq "/") {
721 $input_params{'action'} ||= "tree";
722 $pathname =~ s,/$,,;
723 } else {
724 # the default action depends on whether we had parent info
725 # or not
726 if ($parentrefname) {
727 $input_params{'action'} ||= "blobdiff_plain";
728 } else {
729 $input_params{'action'} ||= "blob_plain";
732 $input_params{'hash_base'} ||= $refname;
733 $input_params{'file_name'} ||= $pathname;
734 } elsif (defined $refname) {
735 # we got "branch". In this case we have to choose if we have to
736 # set hash or hash_base.
738 # Most of the actions without a pathname only want hash to be
739 # set, except for the ones specified in @wants_base that want
740 # hash_base instead. It should also be noted that hand-crafted
741 # links having 'history' as an action and no pathname or hash
742 # set will fail, but that happens regardless of PATH_INFO.
743 $input_params{'action'} ||= "shortlog";
744 if (grep { $_ eq $input_params{'action'} } @wants_base) {
745 $input_params{'hash_base'} ||= $refname;
746 } else {
747 $input_params{'hash'} ||= $refname;
751 # next, handle the 'parent' part, if present
752 if (defined $parentrefname) {
753 # a missing pathspec defaults to the 'current' filename, allowing e.g.
754 # someproject/blobdiff/oldrev..newrev:/filename
755 if ($parentpathname) {
756 $parentpathname =~ s,^/+,,;
757 $parentpathname =~ s,/$,,;
758 $input_params{'file_parent'} ||= $parentpathname;
759 } else {
760 $input_params{'file_parent'} ||= $input_params{'file_name'};
762 # we assume that hash_parent_base is wanted if a path was specified,
763 # or if the action wants hash_base instead of hash
764 if (defined $input_params{'file_parent'} ||
765 grep { $_ eq $input_params{'action'} } @wants_base) {
766 $input_params{'hash_parent_base'} ||= $parentrefname;
767 } else {
768 $input_params{'hash_parent'} ||= $parentrefname;
772 # for the snapshot action, we allow URLs in the form
773 # $project/snapshot/$hash.ext
774 # where .ext determines the snapshot and gets removed from the
775 # passed $refname to provide the $hash.
777 # To be able to tell that $refname includes the format extension, we
778 # require the following two conditions to be satisfied:
779 # - the hash input parameter MUST have been set from the $refname part
780 # of the URL (i.e. they must be equal)
781 # - the snapshot format MUST NOT have been defined already (e.g. from
782 # CGI parameter sf)
783 # It's also useless to try any matching unless $refname has a dot,
784 # so we check for that too
785 if (defined $input_params{'action'} &&
786 $input_params{'action'} eq 'snapshot' &&
787 defined $refname && index($refname, '.') != -1 &&
788 $refname eq $input_params{'hash'} &&
789 !defined $input_params{'snapshot_format'}) {
790 # We loop over the known snapshot formats, checking for
791 # extensions. Allowed extensions are both the defined suffix
792 # (which includes the initial dot already) and the snapshot
793 # format key itself, with a prepended dot
794 while (my ($fmt, $opt) = each %known_snapshot_formats) {
795 my $hash = $refname;
796 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
797 next;
799 my $sfx = $1;
800 # a valid suffix was found, so set the snapshot format
801 # and reset the hash parameter
802 $input_params{'snapshot_format'} = $fmt;
803 $input_params{'hash'} = $hash;
804 # we also set the format suffix to the one requested
805 # in the URL: this way a request for e.g. .tgz returns
806 # a .tgz instead of a .tar.gz
807 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
808 last;
812 evaluate_path_info();
814 our $action = $input_params{'action'};
815 if (defined $action) {
816 if (!validate_action($action)) {
817 die_error(400, "Invalid action parameter");
821 # parameters which are pathnames
822 our $project = $input_params{'project'};
823 if (defined $project) {
824 if (!validate_project($project)) {
825 undef $project;
826 die_error(404, "No such project");
830 our $file_name = $input_params{'file_name'};
831 if (defined $file_name) {
832 if (!validate_pathname($file_name)) {
833 die_error(400, "Invalid file parameter");
837 our $file_parent = $input_params{'file_parent'};
838 if (defined $file_parent) {
839 if (!validate_pathname($file_parent)) {
840 die_error(400, "Invalid file parent parameter");
844 # parameters which are refnames
845 our $hash = $input_params{'hash'};
846 if (defined $hash) {
847 if (!validate_refname($hash)) {
848 die_error(400, "Invalid hash parameter");
852 our $hash_parent = $input_params{'hash_parent'};
853 if (defined $hash_parent) {
854 if (!validate_refname($hash_parent)) {
855 die_error(400, "Invalid hash parent parameter");
859 our $hash_base = $input_params{'hash_base'};
860 if (defined $hash_base) {
861 if (!validate_refname($hash_base)) {
862 die_error(400, "Invalid hash base parameter");
866 our @extra_options = @{$input_params{'extra_options'}};
867 # @extra_options is always defined, since it can only be (currently) set from
868 # CGI, and $cgi->param() returns the empty array in array context if the param
869 # is not set
870 foreach my $opt (@extra_options) {
871 if (not exists $allowed_options{$opt}) {
872 die_error(400, "Invalid option parameter");
874 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
875 die_error(400, "Invalid option parameter for this action");
879 our $hash_parent_base = $input_params{'hash_parent_base'};
880 if (defined $hash_parent_base) {
881 if (!validate_refname($hash_parent_base)) {
882 die_error(400, "Invalid hash parent base parameter");
886 # other parameters
887 our $page = $input_params{'page'};
888 if (defined $page) {
889 if ($page =~ m/[^0-9]/) {
890 die_error(400, "Invalid page parameter");
894 our $searchtype = $input_params{'searchtype'};
895 if (defined $searchtype) {
896 if ($searchtype =~ m/[^a-z]/) {
897 die_error(400, "Invalid searchtype parameter");
901 our $search_use_regexp = $input_params{'search_use_regexp'};
903 our $searchtext = $input_params{'searchtext'};
904 our $search_regexp;
905 if (defined $searchtext) {
906 if (length($searchtext) < 2) {
907 die_error(403, "At least two characters are required for search parameter");
909 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
912 # path to the current git repository
913 our $git_dir;
914 $git_dir = "$projectroot/$project" if $project;
916 # list of supported snapshot formats
917 our @snapshot_fmts = gitweb_get_feature('snapshot');
918 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
920 # check that the avatar feature is set to a known provider name,
921 # and for each provider check if the dependencies are satisfied.
922 # if the provider name is invalid or the dependencies are not met,
923 # reset $git_avatar to the empty string.
924 our ($git_avatar) = gitweb_get_feature('avatar');
925 if ($git_avatar eq 'gravatar') {
926 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
927 } elsif ($git_avatar eq 'picon') {
928 # no dependencies
929 } else {
930 $git_avatar = '';
933 # dispatch
934 if (!defined $action) {
935 if (defined $hash) {
936 $action = git_get_type($hash);
937 } elsif (defined $hash_base && defined $file_name) {
938 $action = git_get_type("$hash_base:$file_name");
939 } elsif (defined $project) {
940 $action = 'summary';
941 } else {
942 $action = 'project_list';
945 if (!defined($actions{$action})) {
946 die_error(400, "Unknown action");
948 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
949 !$project) {
950 die_error(400, "Project needed");
952 $actions{$action}->();
953 exit;
955 ## ======================================================================
956 ## action links
958 sub href {
959 my %params = @_;
960 # default is to use -absolute url() i.e. $my_uri
961 my $href = $params{-full} ? $my_url : $my_uri;
963 $params{'project'} = $project unless exists $params{'project'};
965 if ($params{-replay}) {
966 while (my ($name, $symbol) = each %cgi_param_mapping) {
967 if (!exists $params{$name}) {
968 $params{$name} = $input_params{$name};
973 my $use_pathinfo = gitweb_check_feature('pathinfo');
974 if ($use_pathinfo and defined $params{'project'}) {
975 # try to put as many parameters as possible in PATH_INFO:
976 # - project name
977 # - action
978 # - hash_parent or hash_parent_base:/file_parent
979 # - hash or hash_base:/filename
980 # - the snapshot_format as an appropriate suffix
982 # When the script is the root DirectoryIndex for the domain,
983 # $href here would be something like http://gitweb.example.com/
984 # Thus, we strip any trailing / from $href, to spare us double
985 # slashes in the final URL
986 $href =~ s,/$,,;
988 # Then add the project name, if present
989 $href .= "/".esc_url($params{'project'});
990 delete $params{'project'};
992 # since we destructively absorb parameters, we keep this
993 # boolean that remembers if we're handling a snapshot
994 my $is_snapshot = $params{'action'} eq 'snapshot';
996 # Summary just uses the project path URL, any other action is
997 # added to the URL
998 if (defined $params{'action'}) {
999 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
1000 delete $params{'action'};
1003 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1004 # stripping nonexistent or useless pieces
1005 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1006 || $params{'hash_parent'} || $params{'hash'});
1007 if (defined $params{'hash_base'}) {
1008 if (defined $params{'hash_parent_base'}) {
1009 $href .= esc_url($params{'hash_parent_base'});
1010 # skip the file_parent if it's the same as the file_name
1011 if (defined $params{'file_parent'}) {
1012 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1013 delete $params{'file_parent'};
1014 } elsif ($params{'file_parent'} !~ /\.\./) {
1015 $href .= ":/".esc_url($params{'file_parent'});
1016 delete $params{'file_parent'};
1019 $href .= "..";
1020 delete $params{'hash_parent'};
1021 delete $params{'hash_parent_base'};
1022 } elsif (defined $params{'hash_parent'}) {
1023 $href .= esc_url($params{'hash_parent'}). "..";
1024 delete $params{'hash_parent'};
1027 $href .= esc_url($params{'hash_base'});
1028 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1029 $href .= ":/".esc_url($params{'file_name'});
1030 delete $params{'file_name'};
1032 delete $params{'hash'};
1033 delete $params{'hash_base'};
1034 } elsif (defined $params{'hash'}) {
1035 $href .= esc_url($params{'hash'});
1036 delete $params{'hash'};
1039 # If the action was a snapshot, we can absorb the
1040 # snapshot_format parameter too
1041 if ($is_snapshot) {
1042 my $fmt = $params{'snapshot_format'};
1043 # snapshot_format should always be defined when href()
1044 # is called, but just in case some code forgets, we
1045 # fall back to the default
1046 $fmt ||= $snapshot_fmts[0];
1047 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1048 delete $params{'snapshot_format'};
1052 # now encode the parameters explicitly
1053 my @result = ();
1054 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1055 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1056 if (defined $params{$name}) {
1057 if (ref($params{$name}) eq "ARRAY") {
1058 foreach my $par (@{$params{$name}}) {
1059 push @result, $symbol . "=" . esc_param($par);
1061 } else {
1062 push @result, $symbol . "=" . esc_param($params{$name});
1066 $href .= "?" . join(';', @result) if $params{-partial_query} or scalar @result;
1068 return $href;
1072 ## ======================================================================
1073 ## validation, quoting/unquoting and escaping
1075 sub validate_action {
1076 my $input = shift || return undef;
1077 return undef unless exists $actions{$input};
1078 return $input;
1081 sub validate_project {
1082 my $input = shift || return undef;
1083 if (!validate_pathname($input) ||
1084 !(-d "$projectroot/$input") ||
1085 !check_export_ok("$projectroot/$input") ||
1086 ($strict_export && !project_in_list($input))) {
1087 return undef;
1088 } else {
1089 return $input;
1093 sub validate_pathname {
1094 my $input = shift || return undef;
1096 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1097 # at the beginning, at the end, and between slashes.
1098 # also this catches doubled slashes
1099 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1100 return undef;
1102 # no null characters
1103 if ($input =~ m!\0!) {
1104 return undef;
1106 return $input;
1109 sub validate_refname {
1110 my $input = shift || return undef;
1112 # textual hashes are O.K.
1113 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1114 return $input;
1116 # it must be correct pathname
1117 $input = validate_pathname($input)
1118 or return undef;
1119 # restrictions on ref name according to git-check-ref-format
1120 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1121 return undef;
1123 return $input;
1126 # decode sequences of octets in utf8 into Perl's internal form,
1127 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1128 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1129 sub to_utf8 {
1130 my $str = shift;
1131 if (utf8::valid($str)) {
1132 utf8::decode($str);
1133 return $str;
1134 } else {
1135 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1139 # quote unsafe chars, but keep the slash, even when it's not
1140 # correct, but quoted slashes look too horrible in bookmarks
1141 sub esc_param {
1142 my $str = shift;
1143 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1144 $str =~ s/ /\+/g;
1145 return $str;
1148 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
1149 sub esc_url {
1150 my $str = shift;
1151 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1152 $str =~ s/\+/%2B/g;
1153 $str =~ s/ /\+/g;
1154 return $str;
1157 # replace invalid utf8 character with SUBSTITUTION sequence
1158 sub esc_html {
1159 my $str = shift;
1160 my %opts = @_;
1162 $str = to_utf8($str);
1163 $str = $cgi->escapeHTML($str);
1164 if ($opts{'-nbsp'}) {
1165 $str =~ s/ /&nbsp;/g;
1167 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1168 return $str;
1171 # quote control characters and escape filename to HTML
1172 sub esc_path {
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:]])|quot_cec($1)|eg;
1182 return $str;
1185 # Make control characters "printable", using character escape codes (CEC)
1186 sub quot_cec {
1187 my $cntrl = shift;
1188 my %opts = @_;
1189 my %es = ( # character escape codes, aka escape sequences
1190 "\t" => '\t', # tab (HT)
1191 "\n" => '\n', # line feed (LF)
1192 "\r" => '\r', # carrige return (CR)
1193 "\f" => '\f', # form feed (FF)
1194 "\b" => '\b', # backspace (BS)
1195 "\a" => '\a', # alarm (bell) (BEL)
1196 "\e" => '\e', # escape (ESC)
1197 "\013" => '\v', # vertical tab (VT)
1198 "\000" => '\0', # nul character (NUL)
1200 my $chr = ( (exists $es{$cntrl})
1201 ? $es{$cntrl}
1202 : sprintf('\%2x', ord($cntrl)) );
1203 if ($opts{-nohtml}) {
1204 return $chr;
1205 } else {
1206 return "<span class=\"cntrl\">$chr</span>";
1210 # Alternatively use unicode control pictures codepoints,
1211 # Unicode "printable representation" (PR)
1212 sub quot_upr {
1213 my $cntrl = shift;
1214 my %opts = @_;
1216 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1217 if ($opts{-nohtml}) {
1218 return $chr;
1219 } else {
1220 return "<span class=\"cntrl\">$chr</span>";
1224 # git may return quoted and escaped filenames
1225 sub unquote {
1226 my $str = shift;
1228 sub unq {
1229 my $seq = shift;
1230 my %es = ( # character escape codes, aka escape sequences
1231 't' => "\t", # tab (HT, TAB)
1232 'n' => "\n", # newline (NL)
1233 'r' => "\r", # return (CR)
1234 'f' => "\f", # form feed (FF)
1235 'b' => "\b", # backspace (BS)
1236 'a' => "\a", # alarm (bell) (BEL)
1237 'e' => "\e", # escape (ESC)
1238 'v' => "\013", # vertical tab (VT)
1241 if ($seq =~ m/^[0-7]{1,3}$/) {
1242 # octal char sequence
1243 return chr(oct($seq));
1244 } elsif (exists $es{$seq}) {
1245 # C escape sequence, aka character escape code
1246 return $es{$seq};
1248 # quoted ordinary character
1249 return $seq;
1252 if ($str =~ m/^"(.*)"$/) {
1253 # needs unquoting
1254 $str = $1;
1255 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1257 return $str;
1260 # escape tabs (convert tabs to spaces)
1261 sub untabify {
1262 my $line = shift;
1264 while ((my $pos = index($line, "\t")) != -1) {
1265 if (my $count = (8 - ($pos % 8))) {
1266 my $spaces = ' ' x $count;
1267 $line =~ s/\t/$spaces/;
1271 return $line;
1274 sub project_in_list {
1275 my $project = shift;
1276 my @list = git_get_projects_list();
1277 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1280 ## ----------------------------------------------------------------------
1281 ## HTML aware string manipulation
1283 # Try to chop given string on a word boundary between position
1284 # $len and $len+$add_len. If there is no word boundary there,
1285 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1286 # (marking chopped part) would be longer than given string.
1287 sub chop_str {
1288 my $str = shift;
1289 my $len = shift;
1290 my $add_len = shift || 10;
1291 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1293 # Make sure perl knows it is utf8 encoded so we don't
1294 # cut in the middle of a utf8 multibyte char.
1295 $str = to_utf8($str);
1297 # allow only $len chars, but don't cut a word if it would fit in $add_len
1298 # if it doesn't fit, cut it if it's still longer than the dots we would add
1299 # remove chopped character entities entirely
1301 # when chopping in the middle, distribute $len into left and right part
1302 # return early if chopping wouldn't make string shorter
1303 if ($where eq 'center') {
1304 return $str if ($len + 5 >= length($str)); # filler is length 5
1305 $len = int($len/2);
1306 } else {
1307 return $str if ($len + 4 >= length($str)); # filler is length 4
1310 # regexps: ending and beginning with word part up to $add_len
1311 my $endre = qr/.{$len}\w{0,$add_len}/;
1312 my $begre = qr/\w{0,$add_len}.{$len}/;
1314 if ($where eq 'left') {
1315 $str =~ m/^(.*?)($begre)$/;
1316 my ($lead, $body) = ($1, $2);
1317 if (length($lead) > 4) {
1318 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1319 $lead = " ...";
1321 return "$lead$body";
1323 } elsif ($where eq 'center') {
1324 $str =~ m/^($endre)(.*)$/;
1325 my ($left, $str) = ($1, $2);
1326 $str =~ m/^(.*?)($begre)$/;
1327 my ($mid, $right) = ($1, $2);
1328 if (length($mid) > 5) {
1329 $left =~ s/&[^;]*$//;
1330 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1331 $mid = " ... ";
1333 return "$left$mid$right";
1335 } else {
1336 $str =~ m/^($endre)(.*)$/;
1337 my $body = $1;
1338 my $tail = $2;
1339 if (length($tail) > 4) {
1340 $body =~ s/&[^;]*$//;
1341 $tail = "... ";
1343 return "$body$tail";
1347 # pass-through email filter, obfuscating it when possible
1348 sub email_obfuscate {
1349 my ($str) = @_;
1350 if ($email) {
1351 $str = $email->escape_html($str);
1352 # Stock HTML::Email::Obfuscate version likes to produce
1353 # invalid XHTML...
1354 $str =~ s#<(/?)B>#<$1b>#g;
1355 return $str;
1356 } else {
1357 $str = esc_html($str);
1358 $str =~ s/@/&#x40;/;
1359 return $str;
1363 # takes the same arguments as chop_str, but also wraps a <span> around the
1364 # result with a title attribute if it does get chopped. Additionally, the
1365 # string is HTML-escaped.
1366 sub chop_and_escape_str {
1367 my ($str) = @_;
1369 my $chopped = chop_str(@_);
1370 if ($chopped eq $str) {
1371 return email_obfuscate($chopped);
1372 } else {
1373 $str =~ s/[[:cntrl:]]/?/g;
1374 return $cgi->span({-title=>$str}, email_obfuscate($chopped));
1378 ## ----------------------------------------------------------------------
1379 ## functions returning short strings
1381 # CSS class for given age value (in seconds)
1382 sub age_class {
1383 my $age = shift;
1385 if (!defined $age) {
1386 return "noage";
1387 } elsif ($age < 60*60*2) {
1388 return "age0";
1389 } elsif ($age < 60*60*24*2) {
1390 return "age1";
1391 } else {
1392 return "age2";
1396 # convert age in seconds to "nn units ago" string
1397 sub age_string {
1398 my $age = shift;
1399 my $age_str;
1401 if ($age > 60*60*24*365*2) {
1402 $age_str = (int $age/60/60/24/365);
1403 $age_str .= " years ago";
1404 } elsif ($age > 60*60*24*(365/12)*2) {
1405 $age_str = int $age/60/60/24/(365/12);
1406 $age_str .= " months ago";
1407 } elsif ($age > 60*60*24*7*2) {
1408 $age_str = int $age/60/60/24/7;
1409 $age_str .= " weeks ago";
1410 } elsif ($age > 60*60*24*2) {
1411 $age_str = int $age/60/60/24;
1412 $age_str .= " days ago";
1413 } elsif ($age > 60*60*2) {
1414 $age_str = int $age/60/60;
1415 $age_str .= " hours ago";
1416 } elsif ($age > 60*2) {
1417 $age_str = int $age/60;
1418 $age_str .= " min ago";
1419 } elsif ($age > 2) {
1420 $age_str = int $age;
1421 $age_str .= " sec ago";
1422 } else {
1423 $age_str .= " right now";
1425 return $age_str;
1428 use constant {
1429 S_IFINVALID => 0030000,
1430 S_IFGITLINK => 0160000,
1433 # submodule/subproject, a commit object reference
1434 sub S_ISGITLINK {
1435 my $mode = shift;
1437 return (($mode & S_IFMT) == S_IFGITLINK)
1440 # convert file mode in octal to symbolic file mode string
1441 sub mode_str {
1442 my $mode = oct shift;
1444 if (S_ISGITLINK($mode)) {
1445 return 'm---------';
1446 } elsif (S_ISDIR($mode & S_IFMT)) {
1447 return 'drwxr-xr-x';
1448 } elsif (S_ISLNK($mode)) {
1449 return 'lrwxrwxrwx';
1450 } elsif (S_ISREG($mode)) {
1451 # git cares only about the executable bit
1452 if ($mode & S_IXUSR) {
1453 return '-rwxr-xr-x';
1454 } else {
1455 return '-rw-r--r--';
1457 } else {
1458 return '----------';
1462 # convert file mode in octal to file type string
1463 sub file_type {
1464 my $mode = shift;
1466 if ($mode !~ m/^[0-7]+$/) {
1467 return $mode;
1468 } else {
1469 $mode = oct $mode;
1472 if (S_ISGITLINK($mode)) {
1473 return "submodule";
1474 } elsif (S_ISDIR($mode & S_IFMT)) {
1475 return "directory";
1476 } elsif (S_ISLNK($mode)) {
1477 return "symlink";
1478 } elsif (S_ISREG($mode)) {
1479 return "file";
1480 } else {
1481 return "unknown";
1485 # convert file mode in octal to file type description string
1486 sub file_type_long {
1487 my $mode = shift;
1489 if ($mode !~ m/^[0-7]+$/) {
1490 return $mode;
1491 } else {
1492 $mode = oct $mode;
1495 if (S_ISGITLINK($mode)) {
1496 return "submodule";
1497 } elsif (S_ISDIR($mode & S_IFMT)) {
1498 return "directory";
1499 } elsif (S_ISLNK($mode)) {
1500 return "symlink";
1501 } elsif (S_ISREG($mode)) {
1502 if ($mode & S_IXUSR) {
1503 return "executable";
1504 } else {
1505 return "file";
1507 } else {
1508 return "unknown";
1513 ## ----------------------------------------------------------------------
1514 ## functions returning short HTML fragments, or transforming HTML fragments
1515 ## which don't belong to other sections
1517 # format line of commit message.
1518 sub format_log_line_html {
1519 my $line = shift;
1521 $line = esc_html($line, -nbsp=>1);
1522 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1523 $cgi->a({-href => href(action=>"object", hash=>$1),
1524 -class => "text"}, $1);
1525 }eg;
1527 return $line;
1530 # format marker of refs pointing to given object
1532 # the destination action is chosen based on object type and current context:
1533 # - for annotated tags, we choose the tag view unless it's the current view
1534 # already, in which case we go to shortlog view
1535 # - for other refs, we keep the current view if we're in history, shortlog or
1536 # log view, and select shortlog otherwise
1537 sub format_ref_marker {
1538 my ($refs, $id) = @_;
1539 my $markers = '';
1541 if (defined $refs->{$id}) {
1542 foreach my $ref (@{$refs->{$id}}) {
1543 # this code exploits the fact that non-lightweight tags are the
1544 # only indirect objects, and that they are the only objects for which
1545 # we want to use tag instead of shortlog as action
1546 my ($type, $name) = qw();
1547 my $indirect = ($ref =~ s/\^\{\}$//);
1548 # e.g. tags/v2.6.11 or heads/next
1549 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1550 $type = $1;
1551 $name = $2;
1552 } else {
1553 $type = "ref";
1554 $name = $ref;
1557 my $class = $type;
1558 $class .= " indirect" if $indirect;
1560 my $dest_action = "shortlog";
1562 if ($indirect) {
1563 $dest_action = "tag" unless $action eq "tag";
1564 } elsif ($action =~ /^(history|(short)?log)$/) {
1565 $dest_action = $action;
1568 my $dest = "";
1569 $dest .= "refs/" unless $ref =~ m!^refs/!;
1570 $dest .= $ref;
1572 my $link = $cgi->a({
1573 -href => href(
1574 action=>$dest_action,
1575 hash=>$dest
1576 )}, $name);
1578 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1579 $link . "</span>";
1583 if ($markers) {
1584 return ' <span class="refs">'. $markers . '</span>';
1585 } else {
1586 return "";
1590 # format, perhaps shortened and with markers, title line
1591 sub format_subject_html {
1592 my ($long, $short, $href, $extra) = @_;
1593 $extra = '' unless defined($extra);
1595 if (length($short) < length($long)) {
1596 $long =~ s/[[:cntrl:]]/?/g;
1597 return $cgi->a({-href => $href, -class => "list subject",
1598 -title => to_utf8($long)},
1599 esc_html($short)) . $extra;
1600 } else {
1601 return $cgi->a({-href => $href, -class => "list subject"},
1602 esc_html($long)) . $extra;
1606 # Rather than recomputing the url for an email multiple times, we cache it
1607 # after the first hit. This gives a visible benefit in views where the avatar
1608 # for the same email is used repeatedly (e.g. shortlog).
1609 # The cache is shared by all avatar engines (currently gravatar only), which
1610 # are free to use it as preferred. Since only one avatar engine is used for any
1611 # given page, there's no risk for cache conflicts.
1612 our %avatar_cache = ();
1614 # Compute the picon url for a given email, by using the picon search service over at
1615 # http://www.cs.indiana.edu/picons/search.html
1616 sub picon_url {
1617 my $email = lc shift;
1618 if (!$avatar_cache{$email}) {
1619 my ($user, $domain) = split('@', $email);
1620 $avatar_cache{$email} =
1621 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1622 "$domain/$user/" .
1623 "users+domains+unknown/up/single";
1625 return $avatar_cache{$email};
1628 # Compute the gravatar url for a given email, if it's not in the cache already.
1629 # Gravatar stores only the part of the URL before the size, since that's the
1630 # one computationally more expensive. This also allows reuse of the cache for
1631 # different sizes (for this particular engine).
1632 sub gravatar_url {
1633 my $email = lc shift;
1634 my $size = shift;
1635 $avatar_cache{$email} ||=
1636 "http://www.gravatar.com/avatar/" .
1637 Digest::MD5::md5_hex($email) . "?s=";
1638 return $avatar_cache{$email} . $size;
1641 # Insert an avatar for the given $email at the given $size if the feature
1642 # is enabled.
1643 sub git_get_avatar {
1644 my ($email, %opts) = @_;
1645 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1646 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1647 $opts{-size} ||= 'default';
1648 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1649 my $url = "";
1650 if ($git_avatar eq 'gravatar') {
1651 $url = gravatar_url($email, $size);
1652 } elsif ($git_avatar eq 'picon') {
1653 $url = picon_url($email);
1655 # Other providers can be added by extending the if chain, defining $url
1656 # as needed. If no variant puts something in $url, we assume avatars
1657 # are completely disabled/unavailable.
1658 if ($url) {
1659 return $pre_white .
1660 "<img width=\"$size\" " .
1661 "class=\"avatar\" " .
1662 "src=\"$url\" " .
1663 "alt=\"\" " .
1664 "/>" . $post_white;
1665 } else {
1666 return "";
1670 sub format_search_author {
1671 my ($author, $searchtype, $displaytext) = @_;
1672 my $have_search = gitweb_check_feature('search');
1674 if ($have_search) {
1675 my $performed = "";
1676 if ($searchtype eq 'author') {
1677 $performed = "authored";
1678 } elsif ($searchtype eq 'committer') {
1679 $performed = "committed";
1682 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1683 searchtext=>$author,
1684 searchtype=>$searchtype), class=>"list",
1685 title=>"Search for commits $performed by $author"},
1686 $displaytext);
1688 } else {
1689 return $displaytext;
1693 # format the author name of the given commit with the given tag
1694 # the author name is chopped and escaped according to the other
1695 # optional parameters (see chop_str).
1696 sub format_author_html {
1697 my $tag = shift;
1698 my $co = shift;
1699 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1700 return "<$tag class=\"author\">" .
1701 format_search_author($co->{'author_name'}, "author",
1702 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1703 $author) .
1704 "</$tag>";
1707 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1708 sub format_git_diff_header_line {
1709 my $line = shift;
1710 my $diffinfo = shift;
1711 my ($from, $to) = @_;
1713 if ($diffinfo->{'nparents'}) {
1714 # combined diff
1715 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1716 if ($to->{'href'}) {
1717 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1718 esc_path($to->{'file'}));
1719 } else { # file was deleted (no href)
1720 $line .= esc_path($to->{'file'});
1722 } else {
1723 # "ordinary" diff
1724 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1725 if ($from->{'href'}) {
1726 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1727 'a/' . esc_path($from->{'file'}));
1728 } else { # file was added (no href)
1729 $line .= 'a/' . esc_path($from->{'file'});
1731 $line .= ' ';
1732 if ($to->{'href'}) {
1733 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1734 'b/' . esc_path($to->{'file'}));
1735 } else { # file was deleted
1736 $line .= 'b/' . esc_path($to->{'file'});
1740 return "<div class=\"diff header\">$line</div>\n";
1743 # format extended diff header line, before patch itself
1744 sub format_extended_diff_header_line {
1745 my $line = shift;
1746 my $diffinfo = shift;
1747 my ($from, $to) = @_;
1749 # match <path>
1750 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1751 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1752 esc_path($from->{'file'}));
1754 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1755 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1756 esc_path($to->{'file'}));
1758 # match single <mode>
1759 if ($line =~ m/\s(\d{6})$/) {
1760 $line .= '<span class="info"> (' .
1761 file_type_long($1) .
1762 ')</span>';
1764 # match <hash>
1765 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1766 # can match only for combined diff
1767 $line = 'index ';
1768 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1769 if ($from->{'href'}[$i]) {
1770 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1771 -class=>"hash"},
1772 substr($diffinfo->{'from_id'}[$i],0,7));
1773 } else {
1774 $line .= '0' x 7;
1776 # separator
1777 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1779 $line .= '..';
1780 if ($to->{'href'}) {
1781 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1782 substr($diffinfo->{'to_id'},0,7));
1783 } else {
1784 $line .= '0' x 7;
1787 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1788 # can match only for ordinary diff
1789 my ($from_link, $to_link);
1790 if ($from->{'href'}) {
1791 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1792 substr($diffinfo->{'from_id'},0,7));
1793 } else {
1794 $from_link = '0' x 7;
1796 if ($to->{'href'}) {
1797 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1798 substr($diffinfo->{'to_id'},0,7));
1799 } else {
1800 $to_link = '0' x 7;
1802 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1803 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1806 return $line . "<br/>\n";
1809 # format from-file/to-file diff header
1810 sub format_diff_from_to_header {
1811 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1812 my $line;
1813 my $result = '';
1815 $line = $from_line;
1816 #assert($line =~ m/^---/) if DEBUG;
1817 # no extra formatting for "^--- /dev/null"
1818 if (! $diffinfo->{'nparents'}) {
1819 # ordinary (single parent) diff
1820 if ($line =~ m!^--- "?a/!) {
1821 if ($from->{'href'}) {
1822 $line = '--- a/' .
1823 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1824 esc_path($from->{'file'}));
1825 } else {
1826 $line = '--- a/' .
1827 esc_path($from->{'file'});
1830 $result .= qq!<div class="diff from_file">$line</div>\n!;
1832 } else {
1833 # combined diff (merge commit)
1834 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1835 if ($from->{'href'}[$i]) {
1836 $line = '--- ' .
1837 $cgi->a({-href=>href(action=>"blobdiff",
1838 hash_parent=>$diffinfo->{'from_id'}[$i],
1839 hash_parent_base=>$parents[$i],
1840 file_parent=>$from->{'file'}[$i],
1841 hash=>$diffinfo->{'to_id'},
1842 hash_base=>$hash,
1843 file_name=>$to->{'file'}),
1844 -class=>"path",
1845 -title=>"diff" . ($i+1)},
1846 $i+1) .
1847 '/' .
1848 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1849 esc_path($from->{'file'}[$i]));
1850 } else {
1851 $line = '--- /dev/null';
1853 $result .= qq!<div class="diff from_file">$line</div>\n!;
1857 $line = $to_line;
1858 #assert($line =~ m/^\+\+\+/) if DEBUG;
1859 # no extra formatting for "^+++ /dev/null"
1860 if ($line =~ m!^\+\+\+ "?b/!) {
1861 if ($to->{'href'}) {
1862 $line = '+++ b/' .
1863 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1864 esc_path($to->{'file'}));
1865 } else {
1866 $line = '+++ b/' .
1867 esc_path($to->{'file'});
1870 $result .= qq!<div class="diff to_file">$line</div>\n!;
1872 return $result;
1875 # create note for patch simplified by combined diff
1876 sub format_diff_cc_simplified {
1877 my ($diffinfo, @parents) = @_;
1878 my $result = '';
1880 $result .= "<div class=\"diff header\">" .
1881 "diff --cc ";
1882 if (!is_deleted($diffinfo)) {
1883 $result .= $cgi->a({-href => href(action=>"blob",
1884 hash_base=>$hash,
1885 hash=>$diffinfo->{'to_id'},
1886 file_name=>$diffinfo->{'to_file'}),
1887 -class => "path"},
1888 esc_path($diffinfo->{'to_file'}));
1889 } else {
1890 $result .= esc_path($diffinfo->{'to_file'});
1892 $result .= "</div>\n" . # class="diff header"
1893 "<div class=\"diff nodifferences\">" .
1894 "Simple merge" .
1895 "</div>\n"; # class="diff nodifferences"
1897 return $result;
1900 # format patch (diff) line (not to be used for diff headers)
1901 sub format_diff_line {
1902 my $line = shift;
1903 my ($from, $to) = @_;
1904 my $diff_class = "";
1906 chomp $line;
1908 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1909 # combined diff
1910 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1911 if ($line =~ m/^\@{3}/) {
1912 $diff_class = " chunk_header";
1913 } elsif ($line =~ m/^\\/) {
1914 $diff_class = " incomplete";
1915 } elsif ($prefix =~ tr/+/+/) {
1916 $diff_class = " add";
1917 } elsif ($prefix =~ tr/-/-/) {
1918 $diff_class = " rem";
1920 } else {
1921 # assume ordinary diff
1922 my $char = substr($line, 0, 1);
1923 if ($char eq '+') {
1924 $diff_class = " add";
1925 } elsif ($char eq '-') {
1926 $diff_class = " rem";
1927 } elsif ($char eq '@') {
1928 $diff_class = " chunk_header";
1929 } elsif ($char eq "\\") {
1930 $diff_class = " incomplete";
1933 $line = untabify($line);
1934 if ($from && $to && $line =~ m/^\@{2} /) {
1935 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1936 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1938 $from_lines = 0 unless defined $from_lines;
1939 $to_lines = 0 unless defined $to_lines;
1941 if ($from->{'href'}) {
1942 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1943 -class=>"list"}, $from_text);
1945 if ($to->{'href'}) {
1946 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1947 -class=>"list"}, $to_text);
1949 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1950 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1951 return "<div class=\"diff$diff_class\">$line</div>\n";
1952 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1953 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1954 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1956 @from_text = split(' ', $ranges);
1957 for (my $i = 0; $i < @from_text; ++$i) {
1958 ($from_start[$i], $from_nlines[$i]) =
1959 (split(',', substr($from_text[$i], 1)), 0);
1962 $to_text = pop @from_text;
1963 $to_start = pop @from_start;
1964 $to_nlines = pop @from_nlines;
1966 $line = "<span class=\"chunk_info\">$prefix ";
1967 for (my $i = 0; $i < @from_text; ++$i) {
1968 if ($from->{'href'}[$i]) {
1969 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1970 -class=>"list"}, $from_text[$i]);
1971 } else {
1972 $line .= $from_text[$i];
1974 $line .= " ";
1976 if ($to->{'href'}) {
1977 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1978 -class=>"list"}, $to_text);
1979 } else {
1980 $line .= $to_text;
1982 $line .= " $prefix</span>" .
1983 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1984 return "<div class=\"diff$diff_class\">$line</div>\n";
1986 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1989 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1990 # linked. Pass the hash of the tree/commit to snapshot.
1991 sub format_snapshot_links {
1992 my ($hash) = @_;
1993 my $num_fmts = @snapshot_fmts;
1994 if ($num_fmts > 1) {
1995 # A parenthesized list of links bearing format names.
1996 # e.g. "snapshot (_tar.gz_ _zip_)"
1997 return "snapshot (" . join(' ', map
1998 $cgi->a({
1999 -href => href(
2000 action=>"snapshot",
2001 hash=>$hash,
2002 snapshot_format=>$_
2004 }, $known_snapshot_formats{$_}{'display'})
2005 , @snapshot_fmts) . ")";
2006 } elsif ($num_fmts == 1) {
2007 # A single "snapshot" link whose tooltip bears the format name.
2008 # i.e. "_snapshot_"
2009 my ($fmt) = @snapshot_fmts;
2010 return
2011 $cgi->a({
2012 -href => href(
2013 action=>"snapshot",
2014 hash=>$hash,
2015 snapshot_format=>$fmt
2017 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2018 }, "snapshot");
2019 } else { # $num_fmts == 0
2020 return undef;
2024 ## ......................................................................
2025 ## functions returning values to be passed, perhaps after some
2026 ## transformation, to other functions; e.g. returning arguments to href()
2028 # returns hash to be passed to href to generate gitweb URL
2029 # in -title key it returns description of link
2030 sub get_feed_info {
2031 my $format = shift || 'Atom';
2032 my %res = (action => lc($format));
2034 # feed links are possible only for project views
2035 return unless (defined $project);
2036 # some views should link to OPML, or to generic project feed,
2037 # or don't have specific feed yet (so they should use generic)
2038 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2040 my $branch;
2041 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2042 # from tag links; this also makes possible to detect branch links
2043 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2044 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2045 $branch = $1;
2047 # find log type for feed description (title)
2048 my $type = 'log';
2049 if (defined $file_name) {
2050 $type = "history of $file_name";
2051 $type .= "/" if ($action eq 'tree');
2052 $type .= " on '$branch'" if (defined $branch);
2053 } else {
2054 $type = "log of $branch" if (defined $branch);
2057 $res{-title} = $type;
2058 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2059 $res{'file_name'} = $file_name;
2061 return %res;
2064 ## ----------------------------------------------------------------------
2065 ## git utility subroutines, invoking git commands
2067 # returns path to the core git executable and the --git-dir parameter as list
2068 sub git_cmd {
2069 return $GIT, '--git-dir='.$git_dir;
2072 # quote the given arguments for passing them to the shell
2073 # quote_command("command", "arg 1", "arg with ' and ! characters")
2074 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2075 # Try to avoid using this function wherever possible.
2076 sub quote_command {
2077 return join(' ',
2078 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2081 # get HEAD ref of given project as hash
2082 sub git_get_head_hash {
2083 my $project = shift;
2084 my $o_git_dir = $git_dir;
2085 my $retval = undef;
2086 $git_dir = "$projectroot/$project";
2087 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
2088 my $head = <$fd>;
2089 close $fd;
2090 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
2091 $retval = $1;
2094 if (defined $o_git_dir) {
2095 $git_dir = $o_git_dir;
2097 return $retval;
2100 # get type of given object
2101 sub git_get_type {
2102 my $hash = shift;
2104 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2105 my $type = <$fd>;
2106 close $fd or return;
2107 chomp $type;
2108 return $type;
2111 # repository configuration
2112 our $config_file = '';
2113 our %config;
2115 # store multiple values for single key as anonymous array reference
2116 # single values stored directly in the hash, not as [ <value> ]
2117 sub hash_set_multi {
2118 my ($hash, $key, $value) = @_;
2120 if (!exists $hash->{$key}) {
2121 $hash->{$key} = $value;
2122 } elsif (!ref $hash->{$key}) {
2123 $hash->{$key} = [ $hash->{$key}, $value ];
2124 } else {
2125 push @{$hash->{$key}}, $value;
2129 # return hash of git project configuration
2130 # optionally limited to some section, e.g. 'gitweb'
2131 sub git_parse_project_config {
2132 my $section_regexp = shift;
2133 my %config;
2135 local $/ = "\0";
2137 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2138 or return;
2140 while (my $keyval = <$fh>) {
2141 chomp $keyval;
2142 my ($key, $value) = split(/\n/, $keyval, 2);
2144 hash_set_multi(\%config, $key, $value)
2145 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2147 close $fh;
2149 return %config;
2152 # convert config value to boolean: 'true' or 'false'
2153 # no value, number > 0, 'true' and 'yes' values are true
2154 # rest of values are treated as false (never as error)
2155 sub config_to_bool {
2156 my $val = shift;
2158 return 1 if !defined $val; # section.key
2160 # strip leading and trailing whitespace
2161 $val =~ s/^\s+//;
2162 $val =~ s/\s+$//;
2164 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2165 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2168 # convert config value to simple decimal number
2169 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2170 # to be multiplied by 1024, 1048576, or 1073741824
2171 sub config_to_int {
2172 my $val = shift;
2174 # strip leading and trailing whitespace
2175 $val =~ s/^\s+//;
2176 $val =~ s/\s+$//;
2178 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2179 $unit = lc($unit);
2180 # unknown unit is treated as 1
2181 return $num * ($unit eq 'g' ? 1073741824 :
2182 $unit eq 'm' ? 1048576 :
2183 $unit eq 'k' ? 1024 : 1);
2185 return $val;
2188 # convert config value to array reference, if needed
2189 sub config_to_multi {
2190 my $val = shift;
2192 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2195 sub git_get_project_config {
2196 my ($key, $type) = @_;
2198 # key sanity check
2199 return unless ($key);
2200 $key =~ s/^gitweb\.//;
2201 return if ($key =~ m/\W/);
2203 # type sanity check
2204 if (defined $type) {
2205 $type =~ s/^--//;
2206 $type = undef
2207 unless ($type eq 'bool' || $type eq 'int');
2210 # get config
2211 if (!defined $config_file ||
2212 $config_file ne "$git_dir/config") {
2213 %config = git_parse_project_config('gitweb');
2214 $config_file = "$git_dir/config";
2217 # check if config variable (key) exists
2218 return unless exists $config{"gitweb.$key"};
2220 # ensure given type
2221 if (!defined $type) {
2222 return $config{"gitweb.$key"};
2223 } elsif ($type eq 'bool') {
2224 # backward compatibility: 'git config --bool' returns true/false
2225 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2226 } elsif ($type eq 'int') {
2227 return config_to_int($config{"gitweb.$key"});
2229 return $config{"gitweb.$key"};
2232 # get hash of given path at given ref
2233 sub git_get_hash_by_path {
2234 my $base = shift;
2235 my $path = shift || return undef;
2236 my $type = shift;
2238 $path =~ s,/+$,,;
2240 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2241 or die_error(500, "Open git-ls-tree failed");
2242 my $line = <$fd>;
2243 close $fd or return undef;
2245 if (!defined $line) {
2246 # there is no tree or hash given by $path at $base
2247 return undef;
2250 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2251 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2252 if (defined $type && $type ne $2) {
2253 # type doesn't match
2254 return undef;
2256 return $3;
2259 # get path of entry with given hash at given tree-ish (ref)
2260 # used to get 'from' filename for combined diff (merge commit) for renames
2261 sub git_get_path_by_hash {
2262 my $base = shift || return;
2263 my $hash = shift || return;
2265 local $/ = "\0";
2267 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2268 or return undef;
2269 while (my $line = <$fd>) {
2270 chomp $line;
2272 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2273 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2274 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2275 close $fd;
2276 return $1;
2279 close $fd;
2280 return undef;
2283 ## ......................................................................
2284 ## git utility functions, directly accessing git repository
2286 sub git_get_project_description {
2287 my $path = shift;
2289 $git_dir = "$projectroot/$path";
2290 open my $fd, '<', "$git_dir/description"
2291 or return git_get_project_config('description');
2292 my $descr = <$fd>;
2293 close $fd;
2294 if (defined $descr) {
2295 chomp $descr;
2297 return $descr;
2300 sub git_get_project_ctags {
2301 my $path = shift;
2302 my $ctags = {};
2304 $git_dir = "$projectroot/$path";
2305 opendir my $dh, "$git_dir/ctags"
2306 or return $ctags;
2307 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2308 open my $ct, '<', $_ or next;
2309 my $val = <$ct>;
2310 chomp $val;
2311 close $ct;
2312 my $ctag = $_; $ctag =~ s#.*/##;
2313 $ctags->{$ctag} = $val;
2315 closedir $dh;
2316 $ctags;
2319 sub git_populate_project_tagcloud {
2320 my $ctags = shift;
2322 # First, merge different-cased tags; tags vote on casing
2323 my %ctags_lc;
2324 foreach (keys %$ctags) {
2325 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2326 if (not $ctags_lc{lc $_}->{topcount}
2327 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2328 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2329 $ctags_lc{lc $_}->{topname} = $_;
2333 my $cloud;
2334 if (eval { require HTML::TagCloud; 1; }) {
2335 $cloud = HTML::TagCloud->new;
2336 foreach (sort keys %ctags_lc) {
2337 # Pad the title with spaces so that the cloud looks
2338 # less crammed.
2339 my $title = $ctags_lc{$_}->{topname};
2340 $title =~ s/ /&nbsp;/g;
2341 $title =~ s/^/&nbsp;/g;
2342 $title =~ s/$/&nbsp;/g;
2343 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2345 } else {
2346 $cloud = \%ctags_lc;
2348 $cloud;
2351 sub git_show_project_tagcloud {
2352 my ($cloud, $count) = @_;
2353 print STDERR ref($cloud)."..\n";
2354 if (ref $cloud eq 'HTML::TagCloud') {
2355 return $cloud->html_and_css($count);
2356 } else {
2357 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2358 return '<p align="center">' . join (', ', map {
2359 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2360 } splice(@tags, 0, $count)) . '</p>';
2364 sub git_get_project_url_list {
2365 my $path = shift;
2367 $git_dir = "$projectroot/$path";
2368 open my $fd, '<', "$git_dir/cloneurl"
2369 or return wantarray ?
2370 @{ config_to_multi(git_get_project_config('url')) } :
2371 config_to_multi(git_get_project_config('url'));
2372 my @git_project_url_list = map { chomp; $_ } <$fd>;
2373 close $fd;
2375 return wantarray ? @git_project_url_list : \@git_project_url_list;
2378 sub git_get_projects_list {
2379 my ($filter) = @_;
2380 my @list;
2382 $filter ||= '';
2383 $filter =~ s/\.git$//;
2385 my $check_forks = gitweb_check_feature('forks');
2387 if (-d $projects_list) {
2388 # search in directory
2389 my $dir = $projects_list . ($filter ? "/$filter" : '');
2390 # remove the trailing "/"
2391 $dir =~ s!/+$!!;
2392 my $pfxlen = length("$dir");
2393 my $pfxdepth = ($dir =~ tr!/!!);
2395 File::Find::find({
2396 follow_fast => 1, # follow symbolic links
2397 follow_skip => 2, # ignore duplicates
2398 dangling_symlinks => 0, # ignore dangling symlinks, silently
2399 wanted => sub {
2400 # skip project-list toplevel, if we get it.
2401 return if (m!^[/.]$!);
2402 # only directories can be git repositories
2403 return unless (-d $_);
2404 # don't traverse too deep (Find is super slow on os x)
2405 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2406 $File::Find::prune = 1;
2407 return;
2410 my $subdir = substr($File::Find::name, $pfxlen + 1);
2411 # we check related file in $projectroot
2412 my $path = ($filter ? "$filter/" : '') . $subdir;
2413 if (check_export_ok("$projectroot/$path")) {
2414 push @list, { path => $path };
2415 $File::Find::prune = 1;
2418 }, "$dir");
2420 } elsif (-f $projects_list) {
2421 # read from file(url-encoded):
2422 # 'git%2Fgit.git Linus+Torvalds'
2423 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2424 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2425 my %paths;
2426 open my $fd, '<', $projects_list or return;
2427 PROJECT:
2428 while (my $line = <$fd>) {
2429 chomp $line;
2430 my ($path, $owner) = split ' ', $line;
2431 $path = unescape($path);
2432 $owner = unescape($owner);
2433 if (!defined $path) {
2434 next;
2436 if ($filter ne '') {
2437 # looking for forks;
2438 my $pfx = substr($path, 0, length($filter));
2439 if ($pfx ne $filter) {
2440 next PROJECT;
2442 my $sfx = substr($path, length($filter));
2443 if ($sfx !~ /^\/.*\.git$/) {
2444 next PROJECT;
2446 } elsif ($check_forks) {
2447 PATH:
2448 foreach my $filter (keys %paths) {
2449 # looking for forks;
2450 my $pfx = substr($path, 0, length($filter));
2451 if ($pfx ne $filter) {
2452 next PATH;
2454 my $sfx = substr($path, length($filter));
2455 if ($sfx !~ /^\/.*\.git$/) {
2456 next PATH;
2458 # is a fork, don't include it in
2459 # the list
2460 next PROJECT;
2463 if (check_export_ok("$projectroot/$path")) {
2464 my $pr = {
2465 path => $path,
2466 owner => to_utf8($owner),
2468 push @list, $pr;
2469 (my $forks_path = $path) =~ s/\.git$//;
2470 $paths{$forks_path}++;
2473 close $fd;
2475 return @list;
2478 our $gitweb_project_owner = undef;
2479 sub git_get_project_list_from_file {
2481 return if (defined $gitweb_project_owner);
2483 $gitweb_project_owner = {};
2484 # read from file (url-encoded):
2485 # 'git%2Fgit.git Linus+Torvalds'
2486 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2487 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2488 if (-f $projects_list) {
2489 open(my $fd, '<', $projects_list);
2490 while (my $line = <$fd>) {
2491 chomp $line;
2492 my ($pr, $ow) = split ' ', $line;
2493 $pr = unescape($pr);
2494 $ow = unescape($ow);
2495 $gitweb_project_owner->{$pr} = to_utf8($ow);
2497 close $fd;
2501 sub git_get_project_owner {
2502 my $project = shift;
2503 my $owner;
2505 return undef unless $project;
2506 $git_dir = "$projectroot/$project";
2508 if (!defined $gitweb_project_owner) {
2509 git_get_project_list_from_file();
2512 if (exists $gitweb_project_owner->{$project}) {
2513 $owner = $gitweb_project_owner->{$project};
2515 if (!defined $owner){
2516 $owner = git_get_project_config('owner');
2518 if (!defined $owner) {
2519 $owner = get_file_owner("$git_dir");
2522 return $owner;
2525 sub git_get_last_activity {
2526 my ($path) = @_;
2527 my $fd;
2529 $git_dir = "$projectroot/$path";
2530 open($fd, "-|", git_cmd(), 'for-each-ref',
2531 '--format=%(committer)',
2532 '--sort=-committerdate',
2533 '--count=1',
2534 'refs/heads') or return;
2535 my $most_recent = <$fd>;
2536 close $fd or return;
2537 if (defined $most_recent &&
2538 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2539 my $timestamp = $1;
2540 my $age = time - $timestamp;
2541 return ($age, age_string($age));
2543 return (undef, undef);
2546 sub git_get_references {
2547 my $type = shift || "";
2548 my %refs;
2549 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2550 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2551 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2552 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2553 or return;
2555 while (my $line = <$fd>) {
2556 chomp $line;
2557 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2558 if (defined $refs{$1}) {
2559 push @{$refs{$1}}, $2;
2560 } else {
2561 $refs{$1} = [ $2 ];
2565 close $fd or return;
2566 return \%refs;
2569 sub git_get_rev_name_tags {
2570 my $hash = shift || return undef;
2572 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2573 or return;
2574 my $name_rev = <$fd>;
2575 close $fd;
2577 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2578 return $1;
2579 } else {
2580 # catches also '$hash undefined' output
2581 return undef;
2585 ## ----------------------------------------------------------------------
2586 ## parse to hash functions
2588 sub parse_date {
2589 my $epoch = shift;
2590 my $tz = shift || "-0000";
2592 my %date;
2593 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2594 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2595 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2596 $date{'hour'} = $hour;
2597 $date{'minute'} = $min;
2598 $date{'mday'} = $mday;
2599 $date{'day'} = $days[$wday];
2600 $date{'month'} = $months[$mon];
2601 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2602 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2603 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2604 $mday, $months[$mon], $hour ,$min;
2605 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2606 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2608 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2609 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2610 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2611 $date{'hour_local'} = $hour;
2612 $date{'minute_local'} = $min;
2613 $date{'tz_local'} = $tz;
2614 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2615 1900+$year, $mon+1, $mday,
2616 $hour, $min, $sec, $tz);
2617 return %date;
2620 sub parse_tag {
2621 my $tag_id = shift;
2622 my %tag;
2623 my @comment;
2625 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2626 $tag{'id'} = $tag_id;
2627 while (my $line = <$fd>) {
2628 chomp $line;
2629 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2630 $tag{'object'} = $1;
2631 } elsif ($line =~ m/^type (.+)$/) {
2632 $tag{'type'} = $1;
2633 } elsif ($line =~ m/^tag (.+)$/) {
2634 $tag{'name'} = $1;
2635 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2636 $tag{'author'} = $1;
2637 $tag{'author_epoch'} = $2;
2638 $tag{'author_tz'} = $3;
2639 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2640 $tag{'author_name'} = $1;
2641 $tag{'author_email'} = $2;
2642 } else {
2643 $tag{'author_name'} = $tag{'author'};
2645 } elsif ($line =~ m/--BEGIN/) {
2646 push @comment, $line;
2647 last;
2648 } elsif ($line eq "") {
2649 last;
2652 push @comment, <$fd>;
2653 $tag{'comment'} = \@comment;
2654 close $fd or return;
2655 if (!defined $tag{'name'}) {
2656 return
2658 return %tag
2661 sub parse_commit_text {
2662 my ($commit_text, $withparents) = @_;
2663 my @commit_lines = split '\n', $commit_text;
2664 my %co;
2666 pop @commit_lines; # Remove '\0'
2668 if (! @commit_lines) {
2669 return;
2672 my $header = shift @commit_lines;
2673 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2674 return;
2676 ($co{'id'}, my @parents) = split ' ', $header;
2677 while (my $line = shift @commit_lines) {
2678 last if $line eq "\n";
2679 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2680 $co{'tree'} = $1;
2681 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2682 push @parents, $1;
2683 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2684 $co{'author'} = to_utf8($1);
2685 $co{'author_epoch'} = $2;
2686 $co{'author_tz'} = $3;
2687 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2688 $co{'author_name'} = $1;
2689 $co{'author_email'} = $2;
2690 } else {
2691 $co{'author_name'} = $co{'author'};
2693 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2694 $co{'committer'} = to_utf8($1);
2695 $co{'committer_epoch'} = $2;
2696 $co{'committer_tz'} = $3;
2697 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2698 $co{'committer_name'} = $1;
2699 $co{'committer_email'} = $2;
2700 } else {
2701 $co{'committer_name'} = $co{'committer'};
2705 if (!defined $co{'tree'}) {
2706 return;
2708 $co{'parents'} = \@parents;
2709 $co{'parent'} = $parents[0];
2711 foreach my $title (@commit_lines) {
2712 $title =~ s/^ //;
2713 if ($title ne "") {
2714 $co{'title'} = chop_str($title, 80, 5);
2715 # remove leading stuff of merges to make the interesting part visible
2716 if (length($title) > 50) {
2717 $title =~ s/^Automatic //;
2718 $title =~ s/^merge (of|with) /Merge ... /i;
2719 if (length($title) > 50) {
2720 $title =~ s/(http|rsync):\/\///;
2722 if (length($title) > 50) {
2723 $title =~ s/(master|www|rsync)\.//;
2725 if (length($title) > 50) {
2726 $title =~ s/kernel.org:?//;
2728 if (length($title) > 50) {
2729 $title =~ s/\/pub\/scm//;
2732 $co{'title_short'} = chop_str($title, 50, 5);
2733 last;
2736 if (! defined $co{'title'} || $co{'title'} eq "") {
2737 $co{'title'} = $co{'title_short'} = '(no commit message)';
2739 # remove added spaces
2740 foreach my $line (@commit_lines) {
2741 $line =~ s/^ //;
2743 $co{'comment'} = \@commit_lines;
2745 my $age = time - $co{'committer_epoch'};
2746 $co{'age'} = $age;
2747 $co{'age_string'} = age_string($age);
2748 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2749 if ($age > 60*60*24*7*2) {
2750 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2751 $co{'age_string_age'} = $co{'age_string'};
2752 } else {
2753 $co{'age_string_date'} = $co{'age_string'};
2754 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2756 return %co;
2759 sub parse_commit {
2760 my ($commit_id) = @_;
2761 my %co;
2763 local $/ = "\0";
2765 open my $fd, "-|", git_cmd(), "rev-list",
2766 "--parents",
2767 "--header",
2768 "--max-count=1",
2769 $commit_id,
2770 "--",
2771 or die_error(500, "Open git-rev-list failed");
2772 %co = parse_commit_text(<$fd>, 1);
2773 close $fd;
2775 return %co;
2778 sub parse_commits {
2779 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2780 my @cos;
2782 $maxcount ||= 1;
2783 $skip ||= 0;
2785 local $/ = "\0";
2787 open my $fd, "-|", git_cmd(), "rev-list",
2788 "--header",
2789 @args,
2790 ("--max-count=" . $maxcount),
2791 ("--skip=" . $skip),
2792 @extra_options,
2793 $commit_id,
2794 "--",
2795 ($filename ? ($filename) : ())
2796 or die_error(500, "Open git-rev-list failed");
2797 while (my $line = <$fd>) {
2798 my %co = parse_commit_text($line);
2799 push @cos, \%co;
2801 close $fd;
2803 return wantarray ? @cos : \@cos;
2806 # parse line of git-diff-tree "raw" output
2807 sub parse_difftree_raw_line {
2808 my $line = shift;
2809 my %res;
2811 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2812 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2813 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2814 $res{'from_mode'} = $1;
2815 $res{'to_mode'} = $2;
2816 $res{'from_id'} = $3;
2817 $res{'to_id'} = $4;
2818 $res{'status'} = $5;
2819 $res{'similarity'} = $6;
2820 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2821 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2822 } else {
2823 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2826 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2827 # combined diff (for merge commit)
2828 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2829 $res{'nparents'} = length($1);
2830 $res{'from_mode'} = [ split(' ', $2) ];
2831 $res{'to_mode'} = pop @{$res{'from_mode'}};
2832 $res{'from_id'} = [ split(' ', $3) ];
2833 $res{'to_id'} = pop @{$res{'from_id'}};
2834 $res{'status'} = [ split('', $4) ];
2835 $res{'to_file'} = unquote($5);
2837 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2838 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2839 $res{'commit'} = $1;
2842 return wantarray ? %res : \%res;
2845 # wrapper: return parsed line of git-diff-tree "raw" output
2846 # (the argument might be raw line, or parsed info)
2847 sub parsed_difftree_line {
2848 my $line_or_ref = shift;
2850 if (ref($line_or_ref) eq "HASH") {
2851 # pre-parsed (or generated by hand)
2852 return $line_or_ref;
2853 } else {
2854 return parse_difftree_raw_line($line_or_ref);
2858 # parse line of git-ls-tree output
2859 sub parse_ls_tree_line {
2860 my $line = shift;
2861 my %opts = @_;
2862 my %res;
2864 if ($opts{'-l'}) {
2865 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
2866 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
2868 $res{'mode'} = $1;
2869 $res{'type'} = $2;
2870 $res{'hash'} = $3;
2871 $res{'size'} = $4;
2872 if ($opts{'-z'}) {
2873 $res{'name'} = $5;
2874 } else {
2875 $res{'name'} = unquote($5);
2877 } else {
2878 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2879 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2881 $res{'mode'} = $1;
2882 $res{'type'} = $2;
2883 $res{'hash'} = $3;
2884 if ($opts{'-z'}) {
2885 $res{'name'} = $4;
2886 } else {
2887 $res{'name'} = unquote($4);
2891 return wantarray ? %res : \%res;
2894 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2895 sub parse_from_to_diffinfo {
2896 my ($diffinfo, $from, $to, @parents) = @_;
2898 if ($diffinfo->{'nparents'}) {
2899 # combined diff
2900 $from->{'file'} = [];
2901 $from->{'href'} = [];
2902 fill_from_file_info($diffinfo, @parents)
2903 unless exists $diffinfo->{'from_file'};
2904 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2905 $from->{'file'}[$i] =
2906 defined $diffinfo->{'from_file'}[$i] ?
2907 $diffinfo->{'from_file'}[$i] :
2908 $diffinfo->{'to_file'};
2909 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2910 $from->{'href'}[$i] = href(action=>"blob",
2911 hash_base=>$parents[$i],
2912 hash=>$diffinfo->{'from_id'}[$i],
2913 file_name=>$from->{'file'}[$i]);
2914 } else {
2915 $from->{'href'}[$i] = undef;
2918 } else {
2919 # ordinary (not combined) diff
2920 $from->{'file'} = $diffinfo->{'from_file'};
2921 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2922 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2923 hash=>$diffinfo->{'from_id'},
2924 file_name=>$from->{'file'});
2925 } else {
2926 delete $from->{'href'};
2930 $to->{'file'} = $diffinfo->{'to_file'};
2931 if (!is_deleted($diffinfo)) { # file exists in result
2932 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2933 hash=>$diffinfo->{'to_id'},
2934 file_name=>$to->{'file'});
2935 } else {
2936 delete $to->{'href'};
2940 ## ......................................................................
2941 ## parse to array of hashes functions
2943 sub git_get_heads_list {
2944 my $limit = shift;
2945 my @headslist;
2947 open my $fd, '-|', git_cmd(), 'for-each-ref',
2948 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2949 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2950 'refs/heads'
2951 or return;
2952 while (my $line = <$fd>) {
2953 my %ref_item;
2955 chomp $line;
2956 my ($refinfo, $committerinfo) = split(/\0/, $line);
2957 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2958 my ($committer, $epoch, $tz) =
2959 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2960 $ref_item{'fullname'} = $name;
2961 $name =~ s!^refs/heads/!!;
2963 $ref_item{'name'} = $name;
2964 $ref_item{'id'} = $hash;
2965 $ref_item{'title'} = $title || '(no commit message)';
2966 $ref_item{'epoch'} = $epoch;
2967 if ($epoch) {
2968 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2969 } else {
2970 $ref_item{'age'} = "unknown";
2973 push @headslist, \%ref_item;
2975 close $fd;
2977 return wantarray ? @headslist : \@headslist;
2980 sub git_get_tags_list {
2981 my $limit = shift;
2982 my @tagslist;
2984 open my $fd, '-|', git_cmd(), 'for-each-ref',
2985 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2986 '--format=%(objectname) %(objecttype) %(refname) '.
2987 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2988 'refs/tags'
2989 or return;
2990 while (my $line = <$fd>) {
2991 my %ref_item;
2993 chomp $line;
2994 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2995 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2996 my ($creator, $epoch, $tz) =
2997 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2998 $ref_item{'fullname'} = $name;
2999 $name =~ s!^refs/tags/!!;
3001 $ref_item{'type'} = $type;
3002 $ref_item{'id'} = $id;
3003 $ref_item{'name'} = $name;
3004 if ($type eq "tag") {
3005 $ref_item{'subject'} = $title;
3006 $ref_item{'reftype'} = $reftype;
3007 $ref_item{'refid'} = $refid;
3008 } else {
3009 $ref_item{'reftype'} = $type;
3010 $ref_item{'refid'} = $id;
3013 if ($type eq "tag" || $type eq "commit") {
3014 $ref_item{'epoch'} = $epoch;
3015 if ($epoch) {
3016 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3017 } else {
3018 $ref_item{'age'} = "unknown";
3022 push @tagslist, \%ref_item;
3024 close $fd;
3026 return wantarray ? @tagslist : \@tagslist;
3029 ## ----------------------------------------------------------------------
3030 ## filesystem-related functions
3032 sub get_file_owner {
3033 my $path = shift;
3035 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3036 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3037 if (!defined $gcos) {
3038 return undef;
3040 my $owner = $gcos;
3041 $owner =~ s/[,;].*$//;
3042 return to_utf8($owner);
3045 # assume that file exists
3046 sub insert_file {
3047 my $filename = shift;
3049 open my $fd, '<', $filename;
3050 print map { to_utf8($_) } <$fd>;
3051 close $fd;
3054 ## ......................................................................
3055 ## mimetype related functions
3057 sub mimetype_guess_file {
3058 my $filename = shift;
3059 my $mimemap = shift;
3060 -r $mimemap or return undef;
3062 my %mimemap;
3063 open(my $mh, '<', $mimemap) or return undef;
3064 while (<$mh>) {
3065 next if m/^#/; # skip comments
3066 my ($mimetype, $exts) = split(/\t+/);
3067 if (defined $exts) {
3068 my @exts = split(/\s+/, $exts);
3069 foreach my $ext (@exts) {
3070 $mimemap{$ext} = $mimetype;
3074 close($mh);
3076 $filename =~ /\.([^.]*)$/;
3077 return $mimemap{$1};
3080 sub mimetype_guess {
3081 my $filename = shift;
3082 my $mime;
3083 $filename =~ /\./ or return undef;
3085 if ($mimetypes_file) {
3086 my $file = $mimetypes_file;
3087 if ($file !~ m!^/!) { # if it is relative path
3088 # it is relative to project
3089 $file = "$projectroot/$project/$file";
3091 $mime = mimetype_guess_file($filename, $file);
3093 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3094 return $mime;
3097 sub blob_mimetype {
3098 my $fd = shift;
3099 my $filename = shift;
3101 if ($filename) {
3102 my $mime = mimetype_guess($filename);
3103 $mime and return $mime;
3106 # just in case
3107 return $default_blob_plain_mimetype unless $fd;
3109 if (-T $fd) {
3110 return 'text/plain';
3111 } elsif (! $filename) {
3112 return 'application/octet-stream';
3113 } elsif ($filename =~ m/\.png$/i) {
3114 return 'image/png';
3115 } elsif ($filename =~ m/\.gif$/i) {
3116 return 'image/gif';
3117 } elsif ($filename =~ m/\.jpe?g$/i) {
3118 return 'image/jpeg';
3119 } else {
3120 return 'application/octet-stream';
3124 sub blob_contenttype {
3125 my ($fd, $file_name, $type) = @_;
3127 $type ||= blob_mimetype($fd, $file_name);
3128 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3129 $type .= "; charset=$default_text_plain_charset";
3132 return $type;
3135 ## ======================================================================
3136 ## functions printing HTML: header, footer, error page
3138 sub git_header_html {
3139 my $status = shift || "200 OK";
3140 my $expires = shift;
3142 my $title = "$site_name";
3143 if (defined $project) {
3144 $title .= " - " . to_utf8($project);
3145 if (defined $action) {
3146 $title .= "/$action";
3147 if (defined $file_name) {
3148 $title .= " - " . esc_path($file_name);
3149 if ($action eq "tree" && $file_name !~ m|/$|) {
3150 $title .= "/";
3155 # We do not ever emit application/xhtml+xml since that gives us
3156 # no benefits and it makes many browsers (e.g. Firefox) exceedingly
3157 # strict, which is troublesome for example when showing user-supplied
3158 # README.html files.
3159 my $content_type = 'text/html';
3160 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3161 -status=> $status, -expires => $expires);
3162 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3163 print <<EOF;
3164 <?xml version="1.0" encoding="utf-8"?>
3165 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3166 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3167 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3168 <!-- git core binaries version $git_version -->
3169 <head>
3170 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3171 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3172 <meta name="robots" content="index, nofollow"/>
3173 <title>$title</title>
3174 <script type="text/javascript">/* <![CDATA[ */
3175 function fixBlameLinks() {
3176 var allLinks = document.getElementsByTagName("a");
3177 for (var i = 0; i < allLinks.length; i++) {
3178 var link = allLinks.item(i);
3179 if (link.className == 'blamelink')
3180 link.href = link.href.replace("/blame/", "/blame_incremental/");
3183 /* ]]> */</script>
3185 # the stylesheet, favicon etc urls won't work correctly with path_info
3186 # unless we set the appropriate base URL
3187 if ($ENV{'PATH_INFO'}) {
3188 print "<base href=\"".esc_url($base_url)."\" />\n";
3190 # print out each stylesheet that exist, providing backwards capability
3191 # for those people who defined $stylesheet in a config file
3192 if (defined $stylesheet) {
3193 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3194 } else {
3195 foreach my $stylesheet (@stylesheets) {
3196 next unless $stylesheet;
3197 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3200 if (defined $project) {
3201 my %href_params = get_feed_info();
3202 if (!exists $href_params{'-title'}) {
3203 $href_params{'-title'} = 'log';
3206 foreach my $format qw(RSS Atom) {
3207 my $type = lc($format);
3208 my %link_attr = (
3209 '-rel' => 'alternate',
3210 '-title' => "$project - $href_params{'-title'} - $format feed",
3211 '-type' => "application/$type+xml"
3214 $href_params{'action'} = $type;
3215 $link_attr{'-href'} = href(%href_params);
3216 print "<link ".
3217 "rel=\"$link_attr{'-rel'}\" ".
3218 "title=\"$link_attr{'-title'}\" ".
3219 "href=\"$link_attr{'-href'}\" ".
3220 "type=\"$link_attr{'-type'}\" ".
3221 "/>\n";
3223 $href_params{'extra_options'} = '--no-merges';
3224 $link_attr{'-href'} = href(%href_params);
3225 $link_attr{'-title'} .= ' (no merges)';
3226 print "<link ".
3227 "rel=\"$link_attr{'-rel'}\" ".
3228 "title=\"$link_attr{'-title'}\" ".
3229 "href=\"$link_attr{'-href'}\" ".
3230 "type=\"$link_attr{'-type'}\" ".
3231 "/>\n";
3234 } else {
3235 printf('<link rel="alternate" title="%s projects list" '.
3236 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3237 $site_name, href(project=>undef, action=>"project_index"));
3238 printf('<link rel="alternate" title="%s projects feeds" '.
3239 'href="%s" type="text/x-opml" />'."\n",
3240 $site_name, href(project=>undef, action=>"opml"));
3242 if (defined $favicon) {
3243 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
3246 if (defined $gitwebjs) {
3247 print qq(<script src="$gitwebjs" type="text/javascript"></script>\n);
3250 print "</head>\n";
3251 if (gitweb_check_feature('blame_incremental')) {
3252 print "<body onload=\"fixBlameLinks();\">\n";
3253 } else {
3254 print "<body>\n";
3257 if (-f $site_header) {
3258 insert_file($site_header);
3261 print "<div class=\"page_header\">\n" .
3262 $cgi->a({-href => esc_url($logo_url),
3263 -title => $logo_label},
3264 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
3265 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3266 if (defined $project) {
3267 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3268 if (defined $action) {
3269 print " / $action";
3271 print "\n";
3273 print "</div>\n";
3275 my $have_search = gitweb_check_feature('search');
3276 if (defined $project && $have_search) {
3277 if (!defined $searchtext) {
3278 $searchtext = "";
3280 my $search_hash;
3281 if (defined $hash_base) {
3282 $search_hash = $hash_base;
3283 } elsif (defined $hash) {
3284 $search_hash = $hash;
3285 } else {
3286 $search_hash = "HEAD";
3288 my $action = $my_uri;
3289 my $use_pathinfo = gitweb_check_feature('pathinfo');
3290 if ($use_pathinfo) {
3291 $action .= "/".esc_url($project);
3293 print $cgi->startform(-method => "get", -action => $action) .
3294 "<div class=\"search\">\n" .
3295 (!$use_pathinfo &&
3296 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3297 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3298 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3299 $cgi->popup_menu(-name => 'st', -default => 'commit',
3300 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3301 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3302 " search:\n",
3303 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3304 "<span title=\"Extended regular expression\">" .
3305 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3306 -checked => $search_use_regexp) .
3307 "</span>" .
3308 "</div>" .
3309 $cgi->end_form() . "\n";
3313 sub git_footer_html {
3314 my $feed_class = 'rss_logo';
3316 print "<div class=\"page_footer\">\n";
3317 if (defined $project) {
3318 my $descr = git_get_project_description($project);
3319 if (defined $descr) {
3320 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3323 my %href_params = get_feed_info();
3324 if (!%href_params) {
3325 $feed_class .= ' generic';
3327 $href_params{'-title'} ||= 'log';
3329 foreach my $format qw(RSS Atom) {
3330 $href_params{'action'} = lc($format);
3331 print $cgi->a({-href => href(%href_params),
3332 -title => "$href_params{'-title'} $format feed",
3333 -class => $feed_class}, $format)."\n";
3336 } else {
3337 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3338 -class => $feed_class}, "OPML") . " ";
3339 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3340 -class => $feed_class}, "TXT") . "\n";
3342 print "</div>\n"; # class="page_footer"
3344 if (-f $site_footer) {
3345 insert_file($site_footer);
3348 print "</body>\n" .
3349 "</html>";
3352 # die_error(<http_status_code>, <error_message>)
3353 # Example: die_error(404, 'Hash not found')
3354 # By convention, use the following status codes (as defined in RFC 2616):
3355 # 400: Invalid or missing CGI parameters, or
3356 # requested object exists but has wrong type.
3357 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3358 # this server or project.
3359 # 404: Requested object/revision/project doesn't exist.
3360 # 500: The server isn't configured properly, or
3361 # an internal error occurred (e.g. failed assertions caused by bugs), or
3362 # an unknown error occurred (e.g. the git binary died unexpectedly).
3363 sub die_error {
3364 my $status = shift || 500;
3365 my $error = shift || "Internal server error";
3367 my %http_responses = (400 => '400 Bad Request',
3368 403 => '403 Forbidden',
3369 404 => '404 Not Found',
3370 500 => '500 Internal Server Error');
3371 git_header_html($http_responses{$status});
3372 print <<EOF;
3373 <div class="page_body">
3374 <br /><br />
3375 $status - $error
3376 <br />
3377 </div>
3379 git_footer_html();
3380 exit;
3383 ## ----------------------------------------------------------------------
3384 ## functions printing or outputting HTML: navigation
3386 sub git_print_page_nav {
3387 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3388 $extra = '' if !defined $extra; # pager or formats
3390 my @navs = qw(summary log commit commitdiff tree);
3391 if ($suppress) {
3392 @navs = grep { $_ ne $suppress } @navs;
3395 my %arg = map { $_ => {action=>$_} } @navs;
3396 if (defined $head) {
3397 for (qw(commit commitdiff)) {
3398 $arg{$_}{'hash'} = $head;
3400 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3401 $arg{'log'}{'hash'} = $head;
3405 $arg{'log'}{'action'} = 'shortlog';
3406 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3407 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3409 my @actions = gitweb_get_feature('actions');
3410 my %repl = (
3411 '%' => '%',
3412 'n' => $project, # project name
3413 'f' => $git_dir, # project path within filesystem
3414 'h' => $treehead || '', # current hash ('h' parameter)
3415 'b' => $treebase || '', # hash base ('hb' parameter)
3417 while (@actions) {
3418 my ($label, $link, $pos) = splice(@actions,0,3);
3419 # insert
3420 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3421 # munch munch
3422 $link =~ s/%([%nfhb])/$repl{$1}/g;
3423 $arg{$label}{'_href'} = $link;
3426 print "<div class=\"page_nav\">\n" .
3427 (join " | ",
3428 map { $_ eq $current ?
3429 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3430 } @navs);
3431 print "<br/>\n$extra<br/>\n" .
3432 "</div>\n";
3435 sub format_paging_nav {
3436 my ($action, $hash, $head, $page, $has_next_link) = @_;
3437 my $paging_nav;
3440 if ($hash ne $head || $page) {
3441 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
3442 } else {
3443 $paging_nav .= "HEAD";
3446 if ($page > 0) {
3447 $paging_nav .= " &sdot; " .
3448 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3449 -accesskey => "p", -title => "Alt-p"}, "prev");
3450 } else {
3451 $paging_nav .= " &sdot; prev";
3454 if ($has_next_link) {
3455 $paging_nav .= " &sdot; " .
3456 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3457 -accesskey => "n", -title => "Alt-n"}, "next");
3458 } else {
3459 $paging_nav .= " &sdot; next";
3462 return $paging_nav;
3465 sub format_log_nav {
3466 my ($action, $hash, $head, $page, $has_next_link) = @_;
3467 my $paging_nav;
3469 if ($action eq 'shortlog') {
3470 $paging_nav .= 'shortlog';
3471 } else {
3472 $paging_nav .= $cgi->a({-href => href(action=>'shortlog', -replay=>1)}, 'shortlog');
3474 $paging_nav .= ' | ';
3475 if ($action eq 'log') {
3476 $paging_nav .= 'fulllog';
3477 } else {
3478 $paging_nav .= $cgi->a({-href => href(action=>'log', -replay=>1)}, 'fulllog');
3481 $paging_nav .= " | " . format_paging_nav($action, $hash, $head, $page, $has_next_link);
3482 return $paging_nav;
3485 ## ......................................................................
3486 ## functions printing or outputting HTML: div
3488 sub git_print_header_div {
3489 my ($action, $title, $hash, $hash_base) = @_;
3490 my %args = ();
3492 $args{'action'} = $action;
3493 $args{'hash'} = $hash if $hash;
3494 $args{'hash_base'} = $hash_base if $hash_base;
3496 print "<div class=\"header\">\n" .
3497 $cgi->a({-href => href(%args), -class => "title"},
3498 $title ? $title : $action) .
3499 "\n</div>\n";
3502 sub print_local_time {
3503 my %date = @_;
3504 if ($date{'hour_local'} < 6) {
3505 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3506 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3507 } else {
3508 printf(" (%02d:%02d %s)",
3509 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3513 # Outputs the author name and date in long form
3514 sub git_print_authorship {
3515 my $co = shift;
3516 my %opts = @_;
3517 my $tag = $opts{-tag} || 'div';
3518 my $author = $co->{'author_name'};
3520 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3521 print "<$tag class=\"author_date\">" .
3522 format_search_author($author, "author", esc_html($author)) .
3523 " [$ad{'rfc2822'}";
3524 print_local_time(%ad) if ($opts{-localtime});
3525 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3526 . "</$tag>\n";
3529 # Outputs table rows containing the full author or committer information,
3530 # in the format expected for 'commit' view (& similia).
3531 # Parameters are a commit hash reference, followed by the list of people
3532 # to output information for. If the list is empty it defalts to both
3533 # author and committer.
3534 sub git_print_authorship_rows {
3535 my $co = shift;
3536 # too bad we can't use @people = @_ || ('author', 'committer')
3537 my @people = @_;
3538 @people = ('author', 'committer') unless @people;
3539 foreach my $who (@people) {
3540 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3541 print "<tr><td>$who</td><td>" .
3542 format_search_author($co->{"${who}_name"}, $who,
3543 esc_html($co->{"${who}_name"})) . " " .
3544 format_search_author($co->{"${who}_email"}, $who,
3545 esc_html("<" . $co->{"${who}_email"} . ">")) .
3546 "</td><td rowspan=\"2\">" .
3547 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3548 "</td></tr>\n" .
3549 "<tr>" .
3550 "<td></td><td> $wd{'rfc2822'}";
3551 print_local_time(%wd);
3552 print "</td>" .
3553 "</tr>\n";
3557 sub git_print_page_path {
3558 my $name = shift;
3559 my $type = shift;
3560 my $hb = shift;
3563 print "<div class=\"page_path\">";
3564 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3565 -title => 'tree root'}, to_utf8("[$project]"));
3566 print " / ";
3567 if (defined $name) {
3568 my @dirname = split '/', $name;
3569 my $basename = pop @dirname;
3570 my $fullname = '';
3572 foreach my $dir (@dirname) {
3573 $fullname .= ($fullname ? '/' : '') . $dir;
3574 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3575 hash_base=>$hb),
3576 -title => $fullname}, esc_path($dir));
3577 print " / ";
3579 if (defined $type && $type eq 'blob') {
3580 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3581 hash_base=>$hb),
3582 -title => $name}, esc_path($basename));
3583 } elsif (defined $type && $type eq 'tree') {
3584 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3585 hash_base=>$hb),
3586 -title => $name}, esc_path($basename));
3587 print " / ";
3588 } else {
3589 print esc_path($basename);
3592 print "<br/></div>\n";
3595 sub git_print_log {
3596 my $log = shift;
3597 my %opts = @_;
3599 if ($opts{'-remove_title'}) {
3600 # remove title, i.e. first line of log
3601 shift @$log;
3603 # remove leading empty lines
3604 while (defined $log->[0] && $log->[0] eq "") {
3605 shift @$log;
3608 # print log
3609 my $signoff = 0;
3610 my $empty = 0;
3611 foreach my $line (@$log) {
3612 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3613 $signoff = 1;
3614 $empty = 0;
3615 if (! $opts{'-remove_signoff'}) {
3616 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3617 next;
3618 } else {
3619 # remove signoff lines
3620 next;
3622 } else {
3623 $signoff = 0;
3626 # print only one empty line
3627 # do not print empty line after signoff
3628 if ($line eq "") {
3629 next if ($empty || $signoff);
3630 $empty = 1;
3631 } else {
3632 $empty = 0;
3635 print format_log_line_html($line) . "<br/>\n";
3638 if ($opts{'-final_empty_line'}) {
3639 # end with single empty line
3640 print "<br/>\n" unless $empty;
3644 # return link target (what link points to)
3645 sub git_get_link_target {
3646 my $hash = shift;
3647 my $link_target;
3649 # read link
3650 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3651 or return;
3653 local $/ = undef;
3654 $link_target = <$fd>;
3656 close $fd
3657 or return;
3659 return $link_target;
3662 # given link target, and the directory (basedir) the link is in,
3663 # return target of link relative to top directory (top tree);
3664 # return undef if it is not possible (including absolute links).
3665 sub normalize_link_target {
3666 my ($link_target, $basedir) = @_;
3668 # absolute symlinks (beginning with '/') cannot be normalized
3669 return if (substr($link_target, 0, 1) eq '/');
3671 # normalize link target to path from top (root) tree (dir)
3672 my $path;
3673 if ($basedir) {
3674 $path = $basedir . '/' . $link_target;
3675 } else {
3676 # we are in top (root) tree (dir)
3677 $path = $link_target;
3680 # remove //, /./, and /../
3681 my @path_parts;
3682 foreach my $part (split('/', $path)) {
3683 # discard '.' and ''
3684 next if (!$part || $part eq '.');
3685 # handle '..'
3686 if ($part eq '..') {
3687 if (@path_parts) {
3688 pop @path_parts;
3689 } else {
3690 # link leads outside repository (outside top dir)
3691 return;
3693 } else {
3694 push @path_parts, $part;
3697 $path = join('/', @path_parts);
3699 return $path;
3702 # print tree entry (row of git_tree), but without encompassing <tr> element
3703 sub git_print_tree_entry {
3704 my ($t, $basedir, $hash_base, $have_blame) = @_;
3706 my %base_key = ();
3707 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3709 # The format of a table row is: mode list link. Where mode is
3710 # the mode of the entry, list is the name of the entry, an href,
3711 # and link is the action links of the entry.
3713 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3714 if (exists $t->{'size'}) {
3715 print "<td class=\"size\">$t->{'size'}</td>\n";
3717 if ($t->{'type'} eq "blob") {
3718 print "<td class=\"list\">" .
3719 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3720 file_name=>"$basedir$t->{'name'}", %base_key),
3721 -class => "list"}, esc_path($t->{'name'}));
3722 if (S_ISLNK(oct $t->{'mode'})) {
3723 my $link_target = git_get_link_target($t->{'hash'});
3724 if ($link_target) {
3725 my $norm_target = normalize_link_target($link_target, $basedir);
3726 if (defined $norm_target) {
3727 print " -> " .
3728 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3729 file_name=>$norm_target),
3730 -title => $norm_target}, esc_path($link_target));
3731 } else {
3732 print " -> " . esc_path($link_target);
3736 print "</td>\n";
3737 print "<td class=\"link\">";
3738 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3739 file_name=>"$basedir$t->{'name'}", %base_key)},
3740 "blob");
3741 if ($have_blame) {
3742 print " | " .
3743 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3744 file_name=>"$basedir$t->{'name'}", %base_key), -class => "blamelink"},
3745 "blame");
3747 if (defined $hash_base) {
3748 print " | " .
3749 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3750 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3751 "history");
3753 print " | " .
3754 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3755 file_name=>"$basedir$t->{'name'}")},
3756 "raw");
3757 print "</td>\n";
3759 } elsif ($t->{'type'} eq "tree") {
3760 print "<td class=\"list\">";
3761 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3762 file_name=>"$basedir$t->{'name'}",
3763 %base_key)},
3764 esc_path($t->{'name'}));
3765 print "</td>\n";
3766 print "<td class=\"link\">";
3767 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3768 file_name=>"$basedir$t->{'name'}",
3769 %base_key)},
3770 "tree");
3771 if (defined $hash_base) {
3772 print " | " .
3773 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3774 file_name=>"$basedir$t->{'name'}")},
3775 "history");
3777 print "</td>\n";
3778 } else {
3779 # unknown object: we can only present history for it
3780 # (this includes 'commit' object, i.e. submodule support)
3781 print "<td class=\"list\">" .
3782 esc_path($t->{'name'}) .
3783 "</td>\n";
3784 print "<td class=\"link\">";
3785 if (defined $hash_base) {
3786 print $cgi->a({-href => href(action=>"history",
3787 hash_base=>$hash_base,
3788 file_name=>"$basedir$t->{'name'}")},
3789 "history");
3791 print "</td>\n";
3795 ## ......................................................................
3796 ## functions printing large fragments of HTML
3798 # get pre-image filenames for merge (combined) diff
3799 sub fill_from_file_info {
3800 my ($diff, @parents) = @_;
3802 $diff->{'from_file'} = [ ];
3803 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3804 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3805 if ($diff->{'status'}[$i] eq 'R' ||
3806 $diff->{'status'}[$i] eq 'C') {
3807 $diff->{'from_file'}[$i] =
3808 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3812 return $diff;
3815 # is current raw difftree line of file deletion
3816 sub is_deleted {
3817 my $diffinfo = shift;
3819 return $diffinfo->{'to_id'} eq ('0' x 40);
3822 # does patch correspond to [previous] difftree raw line
3823 # $diffinfo - hashref of parsed raw diff format
3824 # $patchinfo - hashref of parsed patch diff format
3825 # (the same keys as in $diffinfo)
3826 sub is_patch_split {
3827 my ($diffinfo, $patchinfo) = @_;
3829 return defined $diffinfo && defined $patchinfo
3830 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3834 sub git_difftree_body {
3835 my ($difftree, $hash, @parents) = @_;
3836 my ($parent) = $parents[0];
3837 my $have_blame = gitweb_check_feature('blame');
3838 print "<div class=\"list_head\">\n";
3839 if ($#{$difftree} > 10) {
3840 print(($#{$difftree} + 1) . " files changed:\n");
3842 print "</div>\n";
3844 print "<table class=\"" .
3845 (@parents > 1 ? "combined " : "") .
3846 "diff_tree\">\n";
3848 # header only for combined diff in 'commitdiff' view
3849 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3850 if ($has_header) {
3851 # table header
3852 print "<thead><tr>\n" .
3853 "<th></th><th></th>\n"; # filename, patchN link
3854 for (my $i = 0; $i < @parents; $i++) {
3855 my $par = $parents[$i];
3856 print "<th>" .
3857 $cgi->a({-href => href(action=>"commitdiff",
3858 hash=>$hash, hash_parent=>$par),
3859 -title => 'commitdiff to parent number ' .
3860 ($i+1) . ': ' . substr($par,0,7)},
3861 $i+1) .
3862 "&nbsp;</th>\n";
3864 print "</tr></thead>\n<tbody>\n";
3867 my $alternate = 1;
3868 my $patchno = 0;
3869 foreach my $line (@{$difftree}) {
3870 my $diff = parsed_difftree_line($line);
3872 if ($alternate) {
3873 print "<tr class=\"dark\">\n";
3874 } else {
3875 print "<tr class=\"light\">\n";
3877 $alternate ^= 1;
3879 if (exists $diff->{'nparents'}) { # combined diff
3881 fill_from_file_info($diff, @parents)
3882 unless exists $diff->{'from_file'};
3884 if (!is_deleted($diff)) {
3885 # file exists in the result (child) commit
3886 print "<td>" .
3887 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3888 file_name=>$diff->{'to_file'},
3889 hash_base=>$hash),
3890 -class => "list"}, esc_path($diff->{'to_file'})) .
3891 "</td>\n";
3892 } else {
3893 print "<td>" .
3894 esc_path($diff->{'to_file'}) .
3895 "</td>\n";
3898 if ($action eq 'commitdiff') {
3899 # link to patch
3900 $patchno++;
3901 print "<td class=\"link\">" .
3902 $cgi->a({-href => "#patch$patchno"}, "patch") .
3903 " | " .
3904 "</td>\n";
3907 my $has_history = 0;
3908 my $not_deleted = 0;
3909 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3910 my $hash_parent = $parents[$i];
3911 my $from_hash = $diff->{'from_id'}[$i];
3912 my $from_path = $diff->{'from_file'}[$i];
3913 my $status = $diff->{'status'}[$i];
3915 $has_history ||= ($status ne 'A');
3916 $not_deleted ||= ($status ne 'D');
3918 if ($status eq 'A') {
3919 print "<td class=\"link\" align=\"right\"> | </td>\n";
3920 } elsif ($status eq 'D') {
3921 print "<td class=\"link\">" .
3922 $cgi->a({-href => href(action=>"blob",
3923 hash_base=>$hash,
3924 hash=>$from_hash,
3925 file_name=>$from_path)},
3926 "blob" . ($i+1)) .
3927 " | </td>\n";
3928 } else {
3929 if ($diff->{'to_id'} eq $from_hash) {
3930 print "<td class=\"link nochange\">";
3931 } else {
3932 print "<td class=\"link\">";
3934 print $cgi->a({-href => href(action=>"blobdiff",
3935 hash=>$diff->{'to_id'},
3936 hash_parent=>$from_hash,
3937 hash_base=>$hash,
3938 hash_parent_base=>$hash_parent,
3939 file_name=>$diff->{'to_file'},
3940 file_parent=>$from_path)},
3941 "diff" . ($i+1)) .
3942 " | </td>\n";
3946 print "<td class=\"link\">";
3947 if ($not_deleted) {
3948 print $cgi->a({-href => href(action=>"blob",
3949 hash=>$diff->{'to_id'},
3950 file_name=>$diff->{'to_file'},
3951 hash_base=>$hash)},
3952 "blob");
3953 print " | " if ($has_history);
3955 if ($has_history) {
3956 print $cgi->a({-href => href(action=>"history",
3957 file_name=>$diff->{'to_file'},
3958 hash_base=>$hash)},
3959 "history");
3961 print "</td>\n";
3963 print "</tr>\n";
3964 next; # instead of 'else' clause, to avoid extra indent
3966 # else ordinary diff
3968 my ($to_mode_oct, $to_mode_str, $to_file_type);
3969 my ($from_mode_oct, $from_mode_str, $from_file_type);
3970 if ($diff->{'to_mode'} ne ('0' x 6)) {
3971 $to_mode_oct = oct $diff->{'to_mode'};
3972 if (S_ISREG($to_mode_oct)) { # only for regular file
3973 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3975 $to_file_type = file_type($diff->{'to_mode'});
3977 if ($diff->{'from_mode'} ne ('0' x 6)) {
3978 $from_mode_oct = oct $diff->{'from_mode'};
3979 if (S_ISREG($to_mode_oct)) { # only for regular file
3980 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3982 $from_file_type = file_type($diff->{'from_mode'});
3985 if ($diff->{'status'} eq "A") { # created
3986 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3987 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3988 $mode_chng .= "]</span>";
3989 print "<td>";
3990 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3991 hash_base=>$hash, file_name=>$diff->{'file'}),
3992 -class => "list"}, esc_path($diff->{'file'}));
3993 print "</td>\n";
3994 print "<td>$mode_chng</td>\n";
3995 print "<td class=\"link\">";
3996 if ($action eq 'commitdiff') {
3997 # link to patch
3998 $patchno++;
3999 print $cgi->a({-href => "#patch$patchno"}, "patch");
4000 print " | ";
4002 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4003 hash_base=>$hash, file_name=>$diff->{'file'})},
4004 "blob");
4005 print "</td>\n";
4007 } elsif ($diff->{'status'} eq "D") { # deleted
4008 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4009 print "<td>";
4010 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4011 hash_base=>$parent, file_name=>$diff->{'file'}),
4012 -class => "list"}, esc_path($diff->{'file'}));
4013 print "</td>\n";
4014 print "<td>$mode_chng</td>\n";
4015 print "<td class=\"link\">";
4016 if ($action eq 'commitdiff') {
4017 # link to patch
4018 $patchno++;
4019 print $cgi->a({-href => "#patch$patchno"}, "patch");
4020 print " | ";
4022 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4023 hash_base=>$parent, file_name=>$diff->{'file'})},
4024 "blob") . " | ";
4025 if ($have_blame) {
4026 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4027 file_name=>$diff->{'file'})},
4028 "blame") . " | ";
4030 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4031 file_name=>$diff->{'file'})},
4032 "history");
4033 print "</td>\n";
4035 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4036 my $mode_chnge = "";
4037 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4038 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4039 if ($from_file_type ne $to_file_type) {
4040 $mode_chnge .= " from $from_file_type to $to_file_type";
4042 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4043 if ($from_mode_str && $to_mode_str) {
4044 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4045 } elsif ($to_mode_str) {
4046 $mode_chnge .= " mode: $to_mode_str";
4049 $mode_chnge .= "]</span>\n";
4051 print "<td>";
4052 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4053 hash_base=>$hash, file_name=>$diff->{'file'}),
4054 -class => "list"}, esc_path($diff->{'file'}));
4055 print "</td>\n";
4056 print "<td>$mode_chnge</td>\n";
4057 print "<td class=\"link\">";
4058 if ($action eq 'commitdiff') {
4059 # link to patch
4060 $patchno++;
4061 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4062 " | ";
4063 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4064 # "commit" view and modified file (not onlu mode changed)
4065 print $cgi->a({-href => href(action=>"blobdiff",
4066 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4067 hash_base=>$hash, hash_parent_base=>$parent,
4068 file_name=>$diff->{'file'})},
4069 "diff") .
4070 " | ";
4072 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4073 hash_base=>$hash, file_name=>$diff->{'file'})},
4074 "blob") . " | ";
4075 if ($have_blame) {
4076 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4077 file_name=>$diff->{'file'})},
4078 "blame") . " | ";
4080 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4081 file_name=>$diff->{'file'})},
4082 "history");
4083 print "</td>\n";
4085 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4086 my %status_name = ('R' => 'moved', 'C' => 'copied');
4087 my $nstatus = $status_name{$diff->{'status'}};
4088 my $mode_chng = "";
4089 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4090 # mode also for directories, so we cannot use $to_mode_str
4091 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4093 print "<td>" .
4094 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4095 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4096 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4097 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4098 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4099 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4100 -class => "list"}, esc_path($diff->{'from_file'})) .
4101 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4102 "<td class=\"link\">";
4103 if ($action eq 'commitdiff') {
4104 # link to patch
4105 $patchno++;
4106 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4107 " | ";
4108 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4109 # "commit" view and modified file (not only pure rename or copy)
4110 print $cgi->a({-href => href(action=>"blobdiff",
4111 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4112 hash_base=>$hash, hash_parent_base=>$parent,
4113 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4114 "diff") .
4115 " | ";
4117 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4118 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4119 "blob") . " | ";
4120 if ($have_blame) {
4121 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4122 file_name=>$diff->{'to_file'})},
4123 "blame") . " | ";
4125 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4126 file_name=>$diff->{'to_file'})},
4127 "history");
4128 print "</td>\n";
4130 } # we should not encounter Unmerged (U) or Unknown (X) status
4131 print "</tr>\n";
4133 print "</tbody>" if $has_header;
4134 print "</table>\n";
4137 sub git_patchset_body {
4138 my ($fd, $difftree, $hash, @hash_parents) = @_;
4139 my ($hash_parent) = $hash_parents[0];
4141 my $is_combined = (@hash_parents > 1);
4142 my $patch_idx = 0;
4143 my $patch_number = 0;
4144 my $patch_line;
4145 my $diffinfo;
4146 my $to_name;
4147 my (%from, %to);
4149 print "<div class=\"patchset\">\n";
4151 # skip to first patch
4152 while ($patch_line = <$fd>) {
4153 chomp $patch_line;
4155 last if ($patch_line =~ m/^diff /);
4158 PATCH:
4159 while ($patch_line) {
4161 # parse "git diff" header line
4162 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4163 # $1 is from_name, which we do not use
4164 $to_name = unquote($2);
4165 $to_name =~ s!^b/!!;
4166 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4167 # $1 is 'cc' or 'combined', which we do not use
4168 $to_name = unquote($2);
4169 } else {
4170 $to_name = undef;
4173 # check if current patch belong to current raw line
4174 # and parse raw git-diff line if needed
4175 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4176 # this is continuation of a split patch
4177 print "<div class=\"patch cont\">\n";
4178 } else {
4179 # advance raw git-diff output if needed
4180 $patch_idx++ if defined $diffinfo;
4182 # read and prepare patch information
4183 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4185 # compact combined diff output can have some patches skipped
4186 # find which patch (using pathname of result) we are at now;
4187 if ($is_combined) {
4188 while ($to_name ne $diffinfo->{'to_file'}) {
4189 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4190 format_diff_cc_simplified($diffinfo, @hash_parents) .
4191 "</div>\n"; # class="patch"
4193 $patch_idx++;
4194 $patch_number++;
4196 last if $patch_idx > $#$difftree;
4197 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4201 # modifies %from, %to hashes
4202 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4204 # this is first patch for raw difftree line with $patch_idx index
4205 # we index @$difftree array from 0, but number patches from 1
4206 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4209 # git diff header
4210 #assert($patch_line =~ m/^diff /) if DEBUG;
4211 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4212 $patch_number++;
4213 # print "git diff" header
4214 print format_git_diff_header_line($patch_line, $diffinfo,
4215 \%from, \%to);
4217 # print extended diff header
4218 print "<div class=\"diff extended_header\">\n";
4219 EXTENDED_HEADER:
4220 while ($patch_line = <$fd>) {
4221 chomp $patch_line;
4223 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4225 print format_extended_diff_header_line($patch_line, $diffinfo,
4226 \%from, \%to);
4228 print "</div>\n"; # class="diff extended_header"
4230 # from-file/to-file diff header
4231 if (! $patch_line) {
4232 print "</div>\n"; # class="patch"
4233 last PATCH;
4235 next PATCH if ($patch_line =~ m/^diff /);
4236 #assert($patch_line =~ m/^---/) if DEBUG;
4238 my $last_patch_line = $patch_line;
4239 $patch_line = <$fd>;
4240 chomp $patch_line;
4241 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4243 print format_diff_from_to_header($last_patch_line, $patch_line,
4244 $diffinfo, \%from, \%to,
4245 @hash_parents);
4247 # the patch itself
4248 LINE:
4249 while ($patch_line = <$fd>) {
4250 chomp $patch_line;
4252 next PATCH if ($patch_line =~ m/^diff /);
4254 print format_diff_line($patch_line, \%from, \%to);
4257 } continue {
4258 print "</div>\n"; # class="patch"
4261 # for compact combined (--cc) format, with chunk and patch simpliciaction
4262 # patchset might be empty, but there might be unprocessed raw lines
4263 for (++$patch_idx if $patch_number > 0;
4264 $patch_idx < @$difftree;
4265 ++$patch_idx) {
4266 # read and prepare patch information
4267 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4269 # generate anchor for "patch" links in difftree / whatchanged part
4270 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4271 format_diff_cc_simplified($diffinfo, @hash_parents) .
4272 "</div>\n"; # class="patch"
4274 $patch_number++;
4277 if ($patch_number == 0) {
4278 if (@hash_parents > 1) {
4279 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4280 } else {
4281 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4285 print "</div>\n"; # class="patchset"
4288 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4290 # fills project list info (age, description, owner, forks) for each
4291 # project in the list, removing invalid projects from returned list
4292 # NOTE: modifies $projlist, but does not remove entries from it
4293 sub fill_project_list_info {
4294 my ($projlist, $check_forks) = @_;
4295 my @projects;
4297 my $show_ctags = gitweb_check_feature('ctags');
4298 PROJECT:
4299 foreach my $pr (@$projlist) {
4300 my (@activity) = git_get_last_activity($pr->{'path'});
4301 unless (@activity) {
4302 next PROJECT;
4304 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4305 if (!defined $pr->{'descr'}) {
4306 my $descr = git_get_project_description($pr->{'path'}) || "";
4307 $descr = to_utf8($descr);
4308 $pr->{'descr_long'} = $descr;
4309 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4311 if (!defined $pr->{'owner'}) {
4312 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4314 if ($check_forks) {
4315 my $pname = $pr->{'path'};
4316 if (($pname =~ s/\.git$//) &&
4317 ($pname !~ /\/$/) &&
4318 (-d "$projectroot/$pname")) {
4319 $pr->{'forks'} = "-d $projectroot/$pname";
4320 } else {
4321 $pr->{'forks'} = 0;
4324 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4325 push @projects, $pr;
4328 return @projects;
4331 # print 'sort by' <th> element, generating 'sort by $name' replay link
4332 # if that order is not selected
4333 sub print_sort_th {
4334 my ($name, $order, $header) = @_;
4335 $header ||= ucfirst($name);
4337 if ($order eq $name) {
4338 print "<th>$header</th>\n";
4339 } else {
4340 print "<th>" .
4341 $cgi->a({-href => href(-replay=>1, order=>$name),
4342 -class => "header"}, $header) .
4343 "</th>\n";
4347 sub git_project_list_body {
4348 # actually uses global variable $project
4349 my ($projlist, $order, $from, $to, $extra, $no_header, $cache_lifetime) = @_;
4351 my $check_forks = gitweb_check_feature('forks');
4353 use File::stat;
4354 use POSIX qw(:fcntl_h);
4355 use Storable qw(store_fd retrieve);
4357 my $cache_file = "$cache_dir/$projlist_cache_name";
4359 my @projects;
4360 my $stale = 0;
4361 my $now = time();
4362 my $cache_mtime;
4363 if ($cache_lifetime && -f $cache_file) {
4364 $cache_mtime = stat($cache_file)->mtime;
4366 if (defined $cache_mtime && # caching is on and $cache_file exists
4367 $cache_mtime + $cache_lifetime*60 > $now &&
4368 (my $dump = retrieve($cache_file))) {
4369 $stale = $now - $cache_mtime;
4370 @projects = @$dump;
4371 } else {
4372 if (defined $cache_mtime) {
4373 # Postpone timeout by two minutes so that we get
4374 # enough time to do our job, or to be more exact
4375 # make cache expire after two minutes from now.
4376 my $time = $now - $cache_lifetime*60 + 120;
4377 utime $time, $time, $cache_file;
4379 @projects = fill_project_list_info($projlist, $check_forks);
4380 if ($cache_lifetime &&
4381 (-d $cache_dir || mkdir($cache_dir, 0700)) &&
4382 sysopen(my $fd, "$cache_file.lock", O_WRONLY|O_CREAT|O_EXCL, 0600)) {
4383 store_fd(\@projects, $fd);
4384 close $fd;
4385 rename "$cache_file.lock", $cache_file;
4389 $order ||= $default_projects_order;
4390 $from = 0 unless defined $from;
4391 $to = $#projects if (!defined $to || $#projects < $to);
4393 if ($cache_lifetime && $stale > 0) {
4394 print "<div class=\"stale_info\">Cached version (${stale}s old)</div>\n";
4397 my %order_info = (
4398 project => { key => 'path', type => 'str' },
4399 descr => { key => 'descr_long', type => 'str' },
4400 owner => { key => 'owner', type => 'str' },
4401 age => { key => 'age', type => 'num' }
4403 my $oi = $order_info{$order};
4404 if ($oi->{'type'} eq 'str') {
4405 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4406 } else {
4407 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4410 if ($cache_lifetime && $stale > 0) {
4411 print "<div class=\"stale_info\">Cached version (${stale}s old)</div>\n";
4414 my $show_ctags = gitweb_check_feature('ctags');
4415 if ($show_ctags) {
4416 my %ctags;
4417 foreach my $p (@projects) {
4418 foreach my $ct (keys %{$p->{'ctags'}}) {
4419 $ctags{$ct} += $p->{'ctags'}->{$ct};
4422 my $cloud = git_populate_project_tagcloud(\%ctags);
4423 print git_show_project_tagcloud($cloud, 64);
4426 print "<table class=\"project_list\">\n";
4427 unless ($no_header) {
4428 print "<tr>\n";
4429 if ($check_forks) {
4430 print "<th></th>\n";
4432 print_sort_th('project', $order, 'Project');
4433 print_sort_th('descr', $order, 'Description');
4434 print_sort_th('owner', $order, 'Owner');
4435 print_sort_th('age', $order, 'Last Change');
4436 print "<th></th>\n" . # for links
4437 "</tr>\n";
4439 my $alternate = 1;
4440 my $tagfilter = $cgi->param('by_tag');
4441 for (my $i = $from; $i <= $to; $i++) {
4442 my $pr = $projects[$i];
4444 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4445 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4446 and not $pr->{'descr_long'} =~ /$searchtext/;
4447 # Weed out forks or non-matching entries of search
4448 if ($check_forks) {
4449 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4450 $forkbase="^$forkbase" if $forkbase;
4451 next if not $searchtext and not $tagfilter and $show_ctags
4452 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4455 if ($alternate) {
4456 print "<tr class=\"dark\">\n";
4457 } else {
4458 print "<tr class=\"light\">\n";
4460 $alternate ^= 1;
4461 if ($check_forks) {
4462 print "<td>";
4463 if ($pr->{'forks'}) {
4464 print "<!-- $pr->{'forks'} -->\n";
4465 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4467 print "</td>\n";
4469 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4470 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4471 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4472 -class => "list", -title => $pr->{'descr_long'}},
4473 esc_html($pr->{'descr'})) . "</td>\n" .
4474 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4475 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4476 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4477 "<td class=\"link\">" .
4478 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4479 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "log") . " | " .
4480 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4481 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4482 "</td>\n" .
4483 "</tr>\n";
4485 if (defined $extra) {
4486 print "<tr>\n";
4487 if ($check_forks) {
4488 print "<td></td>\n";
4490 print "<td colspan=\"5\">$extra</td>\n" .
4491 "</tr>\n";
4493 print "</table>\n";
4496 sub git_shortlog_body {
4497 # uses global variable $project
4498 my ($commitlist, $from, $to, $refs, $extra) = @_;
4500 $from = 0 unless defined $from;
4501 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4503 print "<table class=\"shortlog\">\n";
4504 my $alternate = 1;
4505 for (my $i = $from; $i <= $to; $i++) {
4506 my %co = %{$commitlist->[$i]};
4507 my $commit = $co{'id'};
4508 my $ref = format_ref_marker($refs, $commit);
4509 if ($alternate) {
4510 print "<tr class=\"dark\">\n";
4511 } else {
4512 print "<tr class=\"light\">\n";
4514 $alternate ^= 1;
4515 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4516 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4517 format_author_html('td', \%co, 10) . "<td>";
4518 print format_subject_html($co{'title'}, $co{'title_short'},
4519 href(action=>"commit", hash=>$commit), $ref);
4520 print "</td>\n" .
4521 "<td class=\"link\">" .
4522 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4523 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4524 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4525 my $snapshot_links = format_snapshot_links($commit);
4526 if (defined $snapshot_links) {
4527 print " | " . $snapshot_links;
4529 print "</td>\n" .
4530 "</tr>\n";
4532 if (defined $extra) {
4533 print "<tr>\n" .
4534 "<td colspan=\"4\">$extra</td>\n" .
4535 "</tr>\n";
4537 print "</table>\n";
4540 sub git_history_body {
4541 # Warning: assumes constant type (blob or tree) during history
4542 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
4544 $from = 0 unless defined $from;
4545 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4547 print "<table class=\"history\">\n";
4548 my $alternate = 1;
4549 for (my $i = $from; $i <= $to; $i++) {
4550 my %co = %{$commitlist->[$i]};
4551 if (!%co) {
4552 next;
4554 my $commit = $co{'id'};
4556 my $ref = format_ref_marker($refs, $commit);
4558 if ($alternate) {
4559 print "<tr class=\"dark\">\n";
4560 } else {
4561 print "<tr class=\"light\">\n";
4563 $alternate ^= 1;
4564 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4565 # shortlog: format_author_html('td', \%co, 10)
4566 format_author_html('td', \%co, 15, 3) . "<td>";
4567 # originally git_history used chop_str($co{'title'}, 50)
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=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4573 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4575 if ($ftype eq 'blob') {
4576 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
4577 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4578 if (defined $blob_current && defined $blob_parent &&
4579 $blob_current ne $blob_parent) {
4580 print " | " .
4581 $cgi->a({-href => href(action=>"blobdiff",
4582 hash=>$blob_current, hash_parent=>$blob_parent,
4583 hash_base=>$hash_base, hash_parent_base=>$commit,
4584 file_name=>$file_name)},
4585 "diff to current");
4588 print "</td>\n" .
4589 "</tr>\n";
4591 if (defined $extra) {
4592 print "<tr>\n" .
4593 "<td colspan=\"4\">$extra</td>\n" .
4594 "</tr>\n";
4596 print "</table>\n";
4599 sub git_tags_body {
4600 # uses global variable $project
4601 my ($taglist, $from, $to, $extra) = @_;
4602 $from = 0 unless defined $from;
4603 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4605 print "<table class=\"tags\">\n";
4606 my $alternate = 1;
4607 for (my $i = $from; $i <= $to; $i++) {
4608 my $entry = $taglist->[$i];
4609 my %tag = %$entry;
4610 my $comment = $tag{'subject'};
4611 my $comment_short;
4612 if (defined $comment) {
4613 $comment_short = chop_str($comment, 30, 5);
4615 if ($alternate) {
4616 print "<tr class=\"dark\">\n";
4617 } else {
4618 print "<tr class=\"light\">\n";
4620 $alternate ^= 1;
4621 if (defined $tag{'age'}) {
4622 print "<td><i>$tag{'age'}</i></td>\n";
4623 } else {
4624 print "<td></td>\n";
4626 print "<td>" .
4627 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4628 -class => "list name"}, esc_html($tag{'name'})) .
4629 "</td>\n" .
4630 "<td>";
4631 if (defined $comment) {
4632 print format_subject_html($comment, $comment_short,
4633 href(action=>"tag", hash=>$tag{'id'}));
4635 print "</td>\n" .
4636 "<td class=\"selflink\">";
4637 if ($tag{'type'} eq "tag") {
4638 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4639 } else {
4640 print "&nbsp;";
4642 print "</td>\n" .
4643 "<td class=\"link\">" . " | " .
4644 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4645 if ($tag{'reftype'} eq "commit") {
4646 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "log");
4647 } elsif ($tag{'reftype'} eq "blob") {
4648 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4650 print "</td>\n" .
4651 "</tr>";
4653 if (defined $extra) {
4654 print "<tr>\n" .
4655 "<td colspan=\"5\">$extra</td>\n" .
4656 "</tr>\n";
4658 print "</table>\n";
4661 sub git_heads_body {
4662 # uses global variable $project
4663 my ($headlist, $head, $from, $to, $extra) = @_;
4664 $from = 0 unless defined $from;
4665 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4667 print "<table class=\"heads\">\n";
4668 my $alternate = 1;
4669 for (my $i = $from; $i <= $to; $i++) {
4670 my $entry = $headlist->[$i];
4671 my %ref = %$entry;
4672 my $curr = $ref{'id'} eq $head;
4673 if ($alternate) {
4674 print "<tr class=\"dark\">\n";
4675 } else {
4676 print "<tr class=\"light\">\n";
4678 $alternate ^= 1;
4679 print "<td><i>$ref{'age'}</i></td>\n" .
4680 ($curr ? "<td class=\"current_head\">" : "<td>") .
4681 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4682 -class => "list name"},esc_html($ref{'name'})) .
4683 "</td>\n" .
4684 "<td class=\"link\">" .
4685 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "log") . " | " .
4686 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4687 "</td>\n" .
4688 "</tr>";
4690 if (defined $extra) {
4691 print "<tr>\n" .
4692 "<td colspan=\"3\">$extra</td>\n" .
4693 "</tr>\n";
4695 print "</table>\n";
4698 sub git_search_grep_body {
4699 my ($commitlist, $from, $to, $extra) = @_;
4700 $from = 0 unless defined $from;
4701 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4703 print "<table class=\"commit_search\">\n";
4704 my $alternate = 1;
4705 for (my $i = $from; $i <= $to; $i++) {
4706 my %co = %{$commitlist->[$i]};
4707 if (!%co) {
4708 next;
4710 my $commit = $co{'id'};
4711 if ($alternate) {
4712 print "<tr class=\"dark\">\n";
4713 } else {
4714 print "<tr class=\"light\">\n";
4716 $alternate ^= 1;
4717 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4718 format_author_html('td', \%co, 15, 5) .
4719 "<td>" .
4720 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4721 -class => "list subject"},
4722 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4723 my $comment = $co{'comment'};
4724 foreach my $line (@$comment) {
4725 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4726 my ($lead, $match, $trail) = ($1, $2, $3);
4727 $match = chop_str($match, 70, 5, 'center');
4728 my $contextlen = int((80 - length($match))/2);
4729 $contextlen = 30 if ($contextlen > 30);
4730 $lead = chop_str($lead, $contextlen, 10, 'left');
4731 $trail = chop_str($trail, $contextlen, 10, 'right');
4733 $lead = esc_html($lead);
4734 $match = esc_html($match);
4735 $trail = esc_html($trail);
4737 print "$lead<span class=\"match\">$match</span>$trail<br />";
4740 print "</td>\n" .
4741 "<td class=\"link\">" .
4742 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4743 " | " .
4744 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4745 " | " .
4746 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4747 print "</td>\n" .
4748 "</tr>\n";
4750 if (defined $extra) {
4751 print "<tr>\n" .
4752 "<td colspan=\"3\">$extra</td>\n" .
4753 "</tr>\n";
4755 print "</table>\n";
4758 ## ======================================================================
4759 ## ======================================================================
4760 ## actions
4762 sub git_project_list {
4763 my $order = $input_params{'order'};
4764 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4765 die_error(400, "Unknown order parameter");
4768 my @list = git_get_projects_list();
4769 if (!@list) {
4770 die_error(404, "No projects found");
4773 git_header_html();
4774 if (-f $home_text) {
4775 print "<div class=\"index_include\">\n";
4776 insert_file($home_text);
4777 print "</div>\n";
4779 print $cgi->startform(-method => "get") .
4780 "<p class=\"projsearch\">Search:\n" .
4781 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4782 "</p>" .
4783 $cgi->end_form() . "\n";
4784 git_project_list_body(\@list, $order, undef, undef, undef, undef, $projlist_cache_lifetime);
4785 git_footer_html();
4788 sub git_forks {
4789 my $order = $input_params{'order'};
4790 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4791 die_error(400, "Unknown order parameter");
4794 my @list = git_get_projects_list($project);
4795 if (!@list) {
4796 die_error(404, "No forks found");
4799 git_header_html();
4800 git_print_page_nav('','');
4801 git_print_header_div('summary', "$project forks");
4802 git_project_list_body(\@list, $order);
4803 git_footer_html();
4806 sub git_project_index {
4807 my @projects = git_get_projects_list($project);
4809 print $cgi->header(
4810 -type => 'text/plain',
4811 -charset => 'utf-8',
4812 -content_disposition => 'inline; filename="index.aux"');
4814 foreach my $pr (@projects) {
4815 if (!exists $pr->{'owner'}) {
4816 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4819 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4820 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4821 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4822 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4823 $path =~ s/ /\+/g;
4824 $owner =~ s/ /\+/g;
4826 print "$path $owner\n";
4830 sub git_summary {
4831 my $descr = git_get_project_description($project) || "none";
4832 my %co = parse_commit("HEAD");
4833 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4834 my $head = $co{'id'};
4836 my $owner = git_get_project_owner($project);
4837 my $homepage = git_get_project_config('homepage');
4838 my $base_url = git_get_project_config('baseurl');
4839 my $last_refresh = git_get_project_config("lastrefresh");
4841 my $refs = git_get_references();
4842 # These get_*_list functions return one more to allow us to see if
4843 # there are more ...
4844 my @taglist = git_get_tags_list(16);
4845 my @headlist = git_get_heads_list(16);
4846 my @forklist;
4847 my $check_forks = gitweb_check_feature('forks');
4849 if ($check_forks) {
4850 @forklist = git_get_projects_list($project);
4853 git_header_html();
4854 git_print_page_nav('summary','', $head);
4856 if ($check_forks and $project =~ m#/#) {
4857 my $xproject = $project; $xproject =~ s#/.+?$#.git#; #
4858 my $r = $cgi->a({-href=> href(project => $xproject, action => 'summary')}, $xproject);
4859 print <<EOT;
4860 <div class="forkinfo">
4861 This project is a fork of the $r project. If you have that one
4862 already cloned locally, you can use
4863 <pre>git clone --reference /path/to/your/$xproject/incarnation mirror_URL</pre>
4864 to save bandwidth during cloning.
4865 </div>
4869 print "<div class=\"title\">&nbsp;</div>\n";
4870 print "<table class=\"projects_list\">\n";
4871 print "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n";
4872 $homepage and print "<tr id=\"metadata_homepage\"><td>homepage URL</td><td>" . $cgi->a({-href => $homepage}, $homepage) . "</td></tr>\n";
4873 $base_url and print "<tr id=\"metadata_baseurl\"><td>repository URL</td><td>" . esc_html($base_url) . "</td></tr>\n";
4874 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . email_obfuscate($owner) . "</td></tr>\n";
4875 if (defined $cd{'rfc2822'}) {
4876 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4878 $last_refresh and print "<tr id=\"metadata_lrefresh\"><td>last refresh</td><td>$last_refresh</td></tr>\n";
4880 # use per project git URL list in $projectroot/$project/cloneurl
4881 # or make project git URL from git base URL and project name
4882 my $url_tag = $base_url ? "mirror URL" : "URL";
4883 my @url_list = git_get_project_url_list($project);
4884 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4885 foreach my $git_url (@url_list) {
4886 next unless $git_url;
4887 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4888 $url_tag = "";
4890 -f "$projectroot/$project/.nofetch" and $git_base_push_url and
4891 print "<tr id=\"metadata_pushurl\"><td>Push URL</td><td>$git_base_push_url/$project</td></tr>\n";
4893 # Tag cloud
4894 my $show_ctags = gitweb_check_feature('ctags');
4895 if ($show_ctags) {
4896 my $ctags = git_get_project_ctags($project);
4897 my $cloud = git_populate_project_tagcloud($ctags);
4898 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4899 print "</td>\n<td>" unless %$ctags;
4900 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4901 print "</td>\n<td>" if %$ctags;
4902 print git_show_project_tagcloud($cloud, 48);
4903 print "</td></tr>";
4906 print "</table>\n";
4908 # If XSS prevention is on, we don't include README.html.
4909 # TODO: Allow a readme in some safe format.
4910 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
4911 print "<div class=\"title\">readme</div>\n" .
4912 "<div class=\"readme\">\n";
4913 insert_file("$projectroot/$project/README.html");
4914 print "\n</div>\n"; # class="readme"
4917 # we need to request one more than 16 (0..15) to check if
4918 # those 16 are all
4919 my @commitlist = $head ? parse_commits($head, 17) : ();
4920 if (@commitlist) {
4921 git_print_header_div('shortlog');
4922 git_shortlog_body(\@commitlist, 0, 15, $refs,
4923 $#commitlist <= 15 ? undef :
4924 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4927 if (@taglist) {
4928 git_print_header_div('tags');
4929 git_tags_body(\@taglist, 0, 15,
4930 $#taglist <= 15 ? undef :
4931 $cgi->a({-href => href(action=>"tags")}, "..."));
4934 if (@headlist) {
4935 git_print_header_div('heads');
4936 git_heads_body(\@headlist, $head, 0, 15,
4937 $#headlist <= 15 ? undef :
4938 $cgi->a({-href => href(action=>"heads")}, "..."));
4941 if (@forklist) {
4942 git_print_header_div('forks');
4943 git_project_list_body(\@forklist, 'age', 0, 15,
4944 $#forklist <= 15 ? undef :
4945 $cgi->a({-href => href(action=>"forks")}, "..."),
4946 'no_header');
4949 git_footer_html();
4952 sub git_tag {
4953 my $head = git_get_head_hash($project);
4954 git_header_html();
4955 git_print_page_nav('','', $head,undef,$head);
4956 my %tag = parse_tag($hash);
4958 if (! %tag) {
4959 die_error(404, "Unknown tag object");
4962 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4963 print "<div class=\"title_text\">\n" .
4964 "<table class=\"object_header\">\n" .
4965 "<tr>\n" .
4966 "<td>object</td>\n" .
4967 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4968 $tag{'object'}) . "</td>\n" .
4969 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4970 $tag{'type'}) . "</td>\n" .
4971 "</tr>\n";
4972 if (defined($tag{'author'})) {
4973 git_print_authorship_rows(\%tag, 'author');
4975 print "</table>\n\n" .
4976 "</div>\n";
4977 print "<div class=\"page_body\">";
4978 my $comment = $tag{'comment'};
4979 foreach my $line (@$comment) {
4980 chomp $line;
4981 print esc_html($line, -nbsp=>1) . "<br/>\n";
4983 print "</div>\n";
4984 git_footer_html();
4987 sub git_blame_data {
4988 my $ftype;
4990 my ($have_blame) = gitweb_check_feature('blame');
4991 if (!$have_blame) {
4992 die_error('403 Permission denied', "Permission denied");
4994 die_error('404 Not Found', "File name not defined") if (!$file_name);
4995 $hash_base ||= git_get_head_hash($project);
4996 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4997 my %co = parse_commit($hash_base)
4998 or die_error(undef, "Reading commit failed");
4999 if (!defined $hash) {
5000 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5001 or die_error(undef, "Error looking up file");
5003 $ftype = git_get_type($hash);
5004 if ($ftype !~ "blob") {
5005 die_error("400 Bad Request", "Object is not a blob");
5007 open my $fd, "-|", git_cmd(), "blame", '--incremental',
5008 $hash_base, '--', $file_name
5009 or die_error(undef, "Open git-blame --incremental failed");
5011 print $cgi->header(-type=>"text/plain", -charset => 'utf-8',
5012 -status=> "200 OK");
5014 while(<$fd>) {
5015 if (/^([0-9a-f]{40}) ([0-9]+) ([0-9]+) ([0-9]+)/ or
5016 /^author-time |^author |^filename /) {
5017 print;
5021 close $fd or print "Reading blame data failed\n";
5024 sub git_blame_common {
5025 my ($type) = @_;
5027 # permissions
5028 gitweb_check_feature('blame')
5029 or die_error(403, "Blame view not allowed");
5031 # error checking
5032 die_error(400, "No file name given") unless $file_name;
5033 $hash_base ||= git_get_head_hash($project);
5034 die_error(404, "Couldn't find base commit") unless $hash_base;
5035 my %co = parse_commit($hash_base)
5036 or die_error(404, "Commit not found");
5037 my $ftype = "blob";
5038 if (!defined $hash) {
5039 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5040 or die_error(404, "Error looking up file");
5041 } else {
5042 $ftype = git_get_type($hash);
5043 if ($ftype !~ "blob") {
5044 die_error(400, "Object is not a blob");
5047 $ftype = git_get_type($hash);
5048 if ($ftype !~ "blob") {
5049 die_error(400, "Object is not a blob");
5051 my $fd;
5052 if ($type eq 'incremental') {
5053 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5054 or die_error(undef, "Open git-cat-file failed");
5055 } else {
5056 # run git-blame --porcelain
5057 open $fd, "-|", git_cmd(), "blame", '-p',
5058 $hash_base, '--', $file_name
5059 or die_error(500, "Open git-blame failed");
5062 # page header
5063 git_header_html();
5064 my $formats_nav =
5065 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5066 "blob") .
5067 " | " .
5068 $cgi->a({-href => href(action=>"history", -replay=>1)},
5069 "history") .
5070 " | " .
5071 $cgi->a({-href => href(action=>"blame", file_name=>$file_name), -class => "blamelink"},
5072 "HEAD");
5073 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5074 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5075 git_print_page_path($file_name, $ftype, $hash_base);
5077 # page body
5078 my @rev_color = qw(light dark);
5079 my $num_colors = scalar(@rev_color);
5080 my $current_color = 0;
5081 my %metainfo = ();
5083 print <<HTML;
5085 <div class="page_body">
5086 <table class="blame">
5087 <tr><th>Commit&nbsp;<a href="javascript:extra_blame_columns()" id="columns_expander">[+]</a></th>
5088 <th class="extra_column">Author</th>
5089 <th class="extra_column">Date</th>
5090 <th>Line</th>
5091 <th>Data</th></tr>
5092 HTML
5093 LINE:
5094 my $linenr = 0;
5095 while (my $line = <$fd>) {
5096 chomp $line;
5097 if ($type eq 'incremental') {
5098 # Empty stage with just the file contents
5099 $linenr += 1;
5100 print "<tr id=\"l$linenr\" class=\"light2\">";
5101 print '<td class="sha1"><a href=""></a></td>';
5102 print "<td class=\"extra_column\"></td>";
5103 print "<td class=\"extra_column\"></td>";
5104 print "<td class=\"linenr\"><a class=\"linenr\" href=\"\">$linenr</a></td><td class=\"pre\">" . esc_html($line) . "</td>\n";
5105 print "</tr>\n";
5106 next;
5109 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5110 # no <lines in group> for subsequent lines in group of lines
5111 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5112 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5113 if (!exists $metainfo{$full_rev}) {
5114 $metainfo{$full_rev} = { 'nprevious' => 0 };
5116 my $meta = $metainfo{$full_rev};
5117 my $data;
5118 while ($data = <$fd>) {
5119 chomp $data;
5120 last if ($data =~ s/^\t//); # contents of line
5121 if ($data =~ /^(\S+)(?: (.*))?$/) {
5122 $meta->{$1} = $2 unless exists $meta->{$1};
5124 if ($data =~ /^previous /) {
5125 $meta->{'nprevious'}++;
5128 my $short_rev = substr($full_rev, 0, 8);
5129 my $author = $meta->{'author'};
5130 my %date =
5131 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5132 my $date = $date{'iso-tz'};
5133 if ($group_size) {
5134 $current_color = ($current_color + 1) % $num_colors;
5136 my $tr_class = $rev_color[$current_color];
5137 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5138 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5139 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5140 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5141 if ($group_size) {
5142 my $rowspan = $group_size > 1 ? " rowspan=\"$group_size\"" : "";
5143 print "<td class=\"sha1\"";
5144 print " title=\"". esc_html($author) . ", $date\"";
5145 print "$rowspan>";
5146 print $cgi->a({-href => href(action=>"commit",
5147 hash=>$full_rev,
5148 file_name=>$file_name)},
5149 esc_html($short_rev));
5150 if ($group_size >= 2) {
5151 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5152 if (@author_initials) {
5153 print "<br />" .
5154 esc_html(join('', @author_initials));
5155 # or join('.', ...)
5158 print "</td>\n";
5159 print "<td class=\"extra_column\" $rowspan>". esc_html($author) . "</td>";
5160 print "<td class=\"extra_column\" $rowspan>". $date . "</td>";
5162 # 'previous' <sha1 of parent commit> <filename at commit>
5163 if (exists $meta->{'previous'} &&
5164 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5165 $meta->{'parent'} = $1;
5166 $meta->{'file_parent'} = unquote($2);
5168 my $linenr_commit =
5169 exists($meta->{'parent'}) ?
5170 $meta->{'parent'} : $full_rev;
5171 my $linenr_filename =
5172 exists($meta->{'file_parent'}) ?
5173 $meta->{'file_parent'} : unquote($meta->{'filename'});
5174 my $blamed = href(action => 'blame',
5175 file_name => $linenr_filename,
5176 hash_base => $linenr_commit);
5177 print "<td class=\"linenr\">";
5178 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5179 -class => "linenr" },
5180 esc_html($lineno));
5181 print "</td>";
5182 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5183 print "</tr>\n";
5186 print "</table>\n";
5187 print "</div>";
5188 close $fd
5189 or print "Reading blob failed\n";
5191 if ($type eq 'incremental') {
5192 print "<script type=\"text/javascript\">\n";
5193 print "startBlame(\"" . href(action=>"blame_data", hash_base=>$hash_base, file_name=>$file_name) . "\", \"" .
5194 href(-partial_query=>1) . "\");\n";
5195 print "</script>\n";
5198 # page footer
5199 git_footer_html();
5202 sub git_blame_incremental {
5203 git_blame_common('incremental');
5206 sub git_blame {
5207 git_blame_common('oneshot');
5210 sub git_tags {
5211 my $head = git_get_head_hash($project);
5212 git_header_html();
5213 git_print_page_nav('','', $head,undef,$head);
5214 git_print_header_div('summary', $project);
5216 my @tagslist = git_get_tags_list();
5217 if (@tagslist) {
5218 git_tags_body(\@tagslist);
5220 git_footer_html();
5223 sub git_heads {
5224 my $head = git_get_head_hash($project);
5225 git_header_html();
5226 git_print_page_nav('','', $head,undef,$head);
5227 git_print_header_div('summary', $project);
5229 my @headslist = git_get_heads_list();
5230 if (@headslist) {
5231 git_heads_body(\@headslist, $head);
5233 git_footer_html();
5236 sub git_blob_plain {
5237 my $type = shift;
5238 my $expires;
5240 if (!defined $hash) {
5241 if (defined $file_name) {
5242 my $base = $hash_base || git_get_head_hash($project);
5243 $hash = git_get_hash_by_path($base, $file_name, "blob")
5244 or die_error(404, "Cannot find file");
5245 } else {
5246 die_error(400, "No file name defined");
5248 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5249 # blobs defined by non-textual hash id's can be cached
5250 $expires = "+1d";
5253 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5254 or die_error(500, "Open git-cat-file blob '$hash' failed");
5256 # content-type (can include charset)
5257 $type = blob_contenttype($fd, $file_name, $type);
5259 # "save as" filename, even when no $file_name is given
5260 my $save_as = "$hash";
5261 if (defined $file_name) {
5262 $save_as = $file_name;
5263 } elsif ($type =~ m/^text\//) {
5264 $save_as .= '.txt';
5267 # With XSS prevention on, blobs of all types except a few known safe
5268 # ones are served with "Content-Disposition: attachment" to make sure
5269 # they don't run in our security domain. For certain image types,
5270 # blob view writes an <img> tag referring to blob_plain view, and we
5271 # want to be sure not to break that by serving the image as an
5272 # attachment (though Firefox 3 doesn't seem to care).
5273 my $sandbox = $prevent_xss &&
5274 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5276 print $cgi->header(
5277 -type => $type,
5278 -expires => $expires,
5279 -content_disposition =>
5280 ($sandbox ? 'attachment' : 'inline')
5281 . '; filename="' . $save_as . '"');
5282 local $/ = undef;
5283 binmode STDOUT, ':raw';
5284 print <$fd>;
5285 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5286 close $fd;
5289 sub git_blob {
5290 my $expires;
5292 if (!defined $hash) {
5293 if (defined $file_name) {
5294 my $base = $hash_base || git_get_head_hash($project);
5295 $hash = git_get_hash_by_path($base, $file_name, "blob")
5296 or die_error(404, "Cannot find file");
5297 } else {
5298 die_error(400, "No file name defined");
5300 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5301 # blobs defined by non-textual hash id's can be cached
5302 $expires = "+1d";
5305 my $have_blame = gitweb_check_feature('blame');
5306 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5307 or die_error(500, "Couldn't cat $file_name, $hash");
5308 my $mimetype = blob_mimetype($fd, $file_name);
5309 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5310 close $fd;
5311 return git_blob_plain($mimetype);
5313 # we can have blame only for text/* mimetype
5314 $have_blame &&= ($mimetype =~ m!^text/!);
5316 git_header_html(undef, $expires);
5317 my $formats_nav = '';
5318 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5319 if (defined $file_name) {
5320 if ($have_blame) {
5321 $formats_nav .=
5322 $cgi->a({-href => href(action=>"blame", -replay=>1,
5323 -class => "blamelink")},
5324 "blame") .
5325 " | ";
5327 $formats_nav .=
5328 $cgi->a({-href => href(action=>"history", -replay=>1)},
5329 "history") .
5330 " | " .
5331 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5332 "raw") .
5333 " | " .
5334 $cgi->a({-href => href(action=>"blob",
5335 hash_base=>"HEAD", file_name=>$file_name)},
5336 "HEAD");
5337 } else {
5338 $formats_nav .=
5339 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5340 "raw");
5342 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5343 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5344 } else {
5345 print "<div class=\"page_nav\">\n" .
5346 "<br/><br/></div>\n" .
5347 "<div class=\"title\">$hash</div>\n";
5349 git_print_page_path($file_name, "blob", $hash_base);
5350 print "<div class=\"page_body\">\n";
5351 if ($mimetype =~ m!^image/!) {
5352 print qq!<img type="$mimetype"!;
5353 if ($file_name) {
5354 print qq! alt="$file_name" title="$file_name"!;
5356 print qq! src="! .
5357 href(action=>"blob_plain", hash=>$hash,
5358 hash_base=>$hash_base, file_name=>$file_name) .
5359 qq!" />\n!;
5360 } else {
5361 my $nr;
5362 while (my $line = <$fd>) {
5363 chomp $line;
5364 $nr++;
5365 $line = untabify($line);
5366 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
5367 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
5370 close $fd
5371 or print "Reading blob failed.\n";
5372 print "</div>";
5373 git_footer_html();
5376 sub git_tree {
5377 if (!defined $hash_base) {
5378 $hash_base = "HEAD";
5380 if (!defined $hash) {
5381 if (defined $file_name) {
5382 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5383 } else {
5384 $hash = $hash_base;
5387 die_error(404, "No such tree") unless defined($hash);
5389 my $show_sizes = gitweb_check_feature('show-sizes');
5390 my $have_blame = gitweb_check_feature('blame');
5392 my @entries = ();
5394 local $/ = "\0";
5395 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5396 ($show_sizes ? '-l' : ()), @extra_options, $hash
5397 or die_error(500, "Open git-ls-tree failed");
5398 @entries = map { chomp; $_ } <$fd>;
5399 close $fd
5400 or die_error(404, "Reading tree failed");
5403 my $refs = git_get_references();
5404 my $ref = format_ref_marker($refs, $hash_base);
5405 git_header_html();
5406 my $basedir = '';
5407 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5408 my @views_nav = ();
5409 if (defined $file_name) {
5410 push @views_nav,
5411 $cgi->a({-href => href(action=>"history", -replay=>1)},
5412 "history"),
5413 $cgi->a({-href => href(action=>"tree",
5414 hash_base=>"HEAD", file_name=>$file_name)},
5415 "HEAD"),
5417 my $snapshot_links = format_snapshot_links($hash);
5418 if (defined $snapshot_links) {
5419 # FIXME: Should be available when we have no hash base as well.
5420 push @views_nav, $snapshot_links;
5422 git_print_page_nav('tree','', $hash_base, undef, undef,
5423 join(' | ', @views_nav));
5424 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5425 } else {
5426 undef $hash_base;
5427 print "<div class=\"page_nav\">\n";
5428 print "<br/><br/></div>\n";
5429 print "<div class=\"title\">$hash</div>\n";
5431 if (defined $file_name) {
5432 $basedir = $file_name;
5433 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5434 $basedir .= '/';
5436 git_print_page_path($file_name, 'tree', $hash_base);
5438 print "<div class=\"page_body\">\n";
5439 print "<table class=\"tree\">\n";
5440 my $alternate = 1;
5441 # '..' (top directory) link if possible
5442 if (defined $hash_base &&
5443 defined $file_name && $file_name =~ m![^/]+$!) {
5444 if ($alternate) {
5445 print "<tr class=\"dark\">\n";
5446 } else {
5447 print "<tr class=\"light\">\n";
5449 $alternate ^= 1;
5451 my $up = $file_name;
5452 $up =~ s!/?[^/]+$!!;
5453 undef $up unless $up;
5454 # based on git_print_tree_entry
5455 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5456 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
5457 print '<td class="list">';
5458 print $cgi->a({-href => href(action=>"tree",
5459 hash_base=>$hash_base,
5460 file_name=>$up)},
5461 "..");
5462 print "</td>\n";
5463 print "<td class=\"link\"></td>\n";
5465 print "</tr>\n";
5467 foreach my $line (@entries) {
5468 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
5470 if ($alternate) {
5471 print "<tr class=\"dark\">\n";
5472 } else {
5473 print "<tr class=\"light\">\n";
5475 $alternate ^= 1;
5477 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
5479 print "</tr>\n";
5481 print "</table>\n" .
5482 "</div>";
5483 git_footer_html();
5486 sub git_snapshot {
5487 my $format = $input_params{'snapshot_format'};
5488 if (!@snapshot_fmts) {
5489 die_error(403, "Snapshots not allowed");
5491 # default to first supported snapshot format
5492 $format ||= $snapshot_fmts[0];
5493 if ($format !~ m/^[a-z0-9]+$/) {
5494 die_error(400, "Invalid snapshot format parameter");
5495 } elsif (!exists($known_snapshot_formats{$format})) {
5496 die_error(400, "Unknown snapshot format");
5497 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
5498 die_error(403, "Snapshot format not allowed");
5499 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5500 die_error(403, "Unsupported snapshot format");
5503 if (!defined $hash) {
5504 $hash = git_get_head_hash($project);
5507 my $name = $project;
5508 $name =~ s,([^/])/*\.git$,$1,;
5509 $name = basename($name);
5510 my $filename = to_utf8($name);
5511 $name =~ s/\047/\047\\\047\047/g;
5512 my $cmd;
5513 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
5514 $cmd = quote_command(
5515 git_cmd(), 'archive',
5516 "--format=$known_snapshot_formats{$format}{'format'}",
5517 "--prefix=$name/", $hash);
5518 if (exists $known_snapshot_formats{$format}{'compressor'}) {
5519 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
5522 print $cgi->header(
5523 -type => $known_snapshot_formats{$format}{'type'},
5524 -content_disposition => 'inline; filename="' . "$filename" . '"',
5525 -status => '200 OK');
5527 open my $fd, "-|", $cmd
5528 or die_error(500, "Execute git-archive failed");
5529 binmode STDOUT, ':raw';
5530 print <$fd>;
5531 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5532 close $fd;
5535 sub git_log {
5536 my $head = git_get_head_hash($project);
5537 if (!defined $hash) {
5538 $hash = $head;
5540 if (!defined $page) {
5541 $page = 0;
5543 my $refs = git_get_references();
5545 my @commitlist = parse_commits($hash, 101, (100 * $page));
5547 my $paging_nav = format_log_nav('log', $hash, $head, $page, $#commitlist >= 100);
5549 my ($patch_max) = gitweb_get_feature('patches');
5550 if ($patch_max) {
5551 if ($patch_max < 0 || @commitlist <= $patch_max) {
5552 $paging_nav .= " &sdot; " .
5553 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5554 "patches");
5559 local $action = 'fulllog';
5560 git_header_html();
5562 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
5564 if (!@commitlist) {
5565 my %co = parse_commit($hash);
5567 git_print_header_div('summary', $project);
5568 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
5570 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
5571 for (my $i = 0; $i <= $to; $i++) {
5572 my %co = %{$commitlist[$i]};
5573 next if !%co;
5574 my $commit = $co{'id'};
5575 my $ref = format_ref_marker($refs, $commit);
5576 my %ad = parse_date($co{'author_epoch'});
5577 git_print_header_div('commit',
5578 "<span class=\"age\">$co{'age_string'}</span>" .
5579 esc_html($co{'title'}) . $ref,
5580 $commit);
5581 print "<div class=\"title_text\">\n" .
5582 "<div class=\"log_link\">\n" .
5583 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5584 " | " .
5585 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5586 " | " .
5587 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5588 "<br/>\n" .
5589 "</div>\n";
5590 git_print_authorship(\%co, -tag => 'span');
5591 print "<br/>\n</div>\n";
5593 print "<div class=\"log_body\">\n";
5594 git_print_log($co{'comment'}, -final_empty_line=> 1);
5595 print "</div>\n";
5597 if ($#commitlist >= 100) {
5598 print "<div class=\"page_nav\">\n";
5599 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
5600 -accesskey => "n", -title => "Alt-n"}, "next");
5601 print "</div>\n";
5603 git_footer_html();
5606 sub git_commit {
5607 $hash ||= $hash_base || "HEAD";
5608 my %co = parse_commit($hash)
5609 or die_error(404, "Unknown commit object");
5611 my $parent = $co{'parent'};
5612 my $parents = $co{'parents'}; # listref
5614 # we need to prepare $formats_nav before any parameter munging
5615 my $formats_nav;
5616 if (!defined $parent) {
5617 # --root commitdiff
5618 $formats_nav .= '(initial)';
5619 } elsif (@$parents == 1) {
5620 # single parent commit
5621 $formats_nav .=
5622 '(parent: ' .
5623 $cgi->a({-href => href(action=>"commit",
5624 hash=>$parent)},
5625 esc_html(substr($parent, 0, 7))) .
5626 ')';
5627 } else {
5628 # merge commit
5629 $formats_nav .=
5630 '(merge: ' .
5631 join(' ', map {
5632 $cgi->a({-href => href(action=>"commit",
5633 hash=>$_)},
5634 esc_html(substr($_, 0, 7)));
5635 } @$parents ) .
5636 ')';
5638 if (gitweb_check_feature('patches') && @$parents <= 1) {
5639 $formats_nav .= " | " .
5640 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5641 "patch");
5644 if (!defined $parent) {
5645 $parent = "--root";
5647 my @difftree;
5648 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5649 @diff_opts,
5650 (@$parents <= 1 ? $parent : '-c'),
5651 $hash, "--"
5652 or die_error(500, "Open git-diff-tree failed");
5653 @difftree = map { chomp; $_ } <$fd>;
5654 close $fd or die_error(404, "Reading git-diff-tree failed");
5656 # non-textual hash id's can be cached
5657 my $expires;
5658 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5659 $expires = "+1d";
5661 my $refs = git_get_references();
5662 my $ref = format_ref_marker($refs, $co{'id'});
5664 git_header_html(undef, $expires);
5665 git_print_page_nav('commit', '',
5666 $hash, $co{'tree'}, $hash,
5667 $formats_nav);
5669 if (defined $co{'parent'}) {
5670 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5671 } else {
5672 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5674 print "<div class=\"title_text\">\n" .
5675 "<table class=\"object_header\">\n";
5676 git_print_authorship_rows(\%co);
5677 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5678 print "<tr>" .
5679 "<td>tree</td>" .
5680 "<td class=\"sha1\">" .
5681 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5682 class => "list"}, $co{'tree'}) .
5683 "</td>" .
5684 "<td class=\"link\">" .
5685 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5686 "tree");
5687 my $snapshot_links = format_snapshot_links($hash);
5688 if (defined $snapshot_links) {
5689 print " | " . $snapshot_links;
5691 print "</td>" .
5692 "</tr>\n";
5694 foreach my $par (@$parents) {
5695 print "<tr>" .
5696 "<td>parent</td>" .
5697 "<td class=\"sha1\">" .
5698 $cgi->a({-href => href(action=>"commit", hash=>$par),
5699 class => "list"}, $par) .
5700 "</td>" .
5701 "<td class=\"link\">" .
5702 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
5703 " | " .
5704 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
5705 "</td>" .
5706 "</tr>\n";
5708 print "</table>".
5709 "</div>\n";
5711 print "<div class=\"page_body\">\n";
5712 git_print_log($co{'comment'});
5713 print "</div>\n";
5715 git_difftree_body(\@difftree, $hash, @$parents);
5717 git_footer_html();
5720 sub git_object {
5721 # object is defined by:
5722 # - hash or hash_base alone
5723 # - hash_base and file_name
5724 my $type;
5726 # - hash or hash_base alone
5727 if ($hash || ($hash_base && !defined $file_name)) {
5728 my $object_id = $hash || $hash_base;
5730 open my $fd, "-|", quote_command(
5731 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
5732 or die_error(404, "Object does not exist");
5733 $type = <$fd>;
5734 chomp $type;
5735 close $fd
5736 or die_error(404, "Object does not exist");
5738 # - hash_base and file_name
5739 } elsif ($hash_base && defined $file_name) {
5740 $file_name =~ s,/+$,,;
5742 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
5743 or die_error(404, "Base object does not exist");
5745 # here errors should not hapen
5746 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
5747 or die_error(500, "Open git-ls-tree failed");
5748 my $line = <$fd>;
5749 close $fd;
5751 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5752 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
5753 die_error(404, "File or directory for given base does not exist");
5755 $type = $2;
5756 $hash = $3;
5757 } else {
5758 die_error(400, "Not enough information to find object");
5761 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5762 hash=>$hash, hash_base=>$hash_base,
5763 file_name=>$file_name),
5764 -status => '302 Found');
5767 sub git_blobdiff {
5768 my $format = shift || 'html';
5770 my $fd;
5771 my @difftree;
5772 my %diffinfo;
5773 my $expires;
5775 # preparing $fd and %diffinfo for git_patchset_body
5776 # new style URI
5777 if (defined $hash_base && defined $hash_parent_base) {
5778 if (defined $file_name) {
5779 # read raw output
5780 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5781 $hash_parent_base, $hash_base,
5782 "--", (defined $file_parent ? $file_parent : ()), $file_name
5783 or die_error(500, "Open git-diff-tree failed");
5784 @difftree = map { chomp; $_ } <$fd>;
5785 close $fd
5786 or die_error(404, "Reading git-diff-tree failed");
5787 @difftree
5788 or die_error(404, "Blob diff not found");
5790 } elsif (defined $hash &&
5791 $hash =~ /[0-9a-fA-F]{40}/) {
5792 # try to find filename from $hash
5794 # read filtered raw output
5795 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5796 $hash_parent_base, $hash_base, "--"
5797 or die_error(500, "Open git-diff-tree failed");
5798 @difftree =
5799 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5800 # $hash == to_id
5801 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5802 map { chomp; $_ } <$fd>;
5803 close $fd
5804 or die_error(404, "Reading git-diff-tree failed");
5805 @difftree
5806 or die_error(404, "Blob diff not found");
5808 } else {
5809 die_error(400, "Missing one of the blob diff parameters");
5812 if (@difftree > 1) {
5813 die_error(400, "Ambiguous blob diff specification");
5816 %diffinfo = parse_difftree_raw_line($difftree[0]);
5817 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5818 $file_name ||= $diffinfo{'to_file'};
5820 $hash_parent ||= $diffinfo{'from_id'};
5821 $hash ||= $diffinfo{'to_id'};
5823 # non-textual hash id's can be cached
5824 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5825 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5826 $expires = '+1d';
5829 # open patch output
5830 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5831 '-p', ($format eq 'html' ? "--full-index" : ()),
5832 $hash_parent_base, $hash_base,
5833 "--", (defined $file_parent ? $file_parent : ()), $file_name
5834 or die_error(500, "Open git-diff-tree failed");
5837 # old/legacy style URI -- not generated anymore since 1.4.3.
5838 if (!%diffinfo) {
5839 die_error('404 Not Found', "Missing one of the blob diff parameters")
5842 # header
5843 if ($format eq 'html') {
5844 my $formats_nav =
5845 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5846 "raw");
5847 git_header_html(undef, $expires);
5848 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5849 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5850 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5851 } else {
5852 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5853 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5855 if (defined $file_name) {
5856 git_print_page_path($file_name, "blob", $hash_base);
5857 } else {
5858 print "<div class=\"page_path\"></div>\n";
5861 } elsif ($format eq 'plain') {
5862 print $cgi->header(
5863 -type => 'text/plain',
5864 -charset => 'utf-8',
5865 -expires => $expires,
5866 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5868 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5870 } else {
5871 die_error(400, "Unknown blobdiff format");
5874 # patch
5875 if ($format eq 'html') {
5876 print "<div class=\"page_body\">\n";
5878 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5879 close $fd;
5881 print "</div>\n"; # class="page_body"
5882 git_footer_html();
5884 } else {
5885 while (my $line = <$fd>) {
5886 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5887 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5889 print $line;
5891 last if $line =~ m!^\+\+\+!;
5893 local $/ = undef;
5894 print <$fd>;
5895 close $fd;
5899 sub git_blobdiff_plain {
5900 git_blobdiff('plain');
5903 sub git_commitdiff {
5904 my %params = @_;
5905 my $format = $params{-format} || 'html';
5907 my ($patch_max) = gitweb_get_feature('patches');
5908 if ($format eq 'patch') {
5909 die_error(403, "Patch view not allowed") unless $patch_max;
5912 $hash ||= $hash_base || "HEAD";
5913 my %co = parse_commit($hash)
5914 or die_error(404, "Unknown commit object");
5916 # choose format for commitdiff for merge
5917 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5918 $hash_parent = '--cc';
5920 # we need to prepare $formats_nav before almost any parameter munging
5921 my $formats_nav;
5922 if ($format eq 'html') {
5923 $formats_nav =
5924 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5925 "raw");
5926 if ($patch_max && @{$co{'parents'}} <= 1) {
5927 $formats_nav .= " | " .
5928 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5929 "patch");
5932 if (defined $hash_parent &&
5933 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5934 # commitdiff with two commits given
5935 my $hash_parent_short = $hash_parent;
5936 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5937 $hash_parent_short = substr($hash_parent, 0, 7);
5939 $formats_nav .=
5940 ' (from';
5941 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5942 if ($co{'parents'}[$i] eq $hash_parent) {
5943 $formats_nav .= ' parent ' . ($i+1);
5944 last;
5947 $formats_nav .= ': ' .
5948 $cgi->a({-href => href(action=>"commitdiff",
5949 hash=>$hash_parent)},
5950 esc_html($hash_parent_short)) .
5951 ')';
5952 } elsif (!$co{'parent'}) {
5953 # --root commitdiff
5954 $formats_nav .= ' (initial)';
5955 } elsif (scalar @{$co{'parents'}} == 1) {
5956 # single parent commit
5957 $formats_nav .=
5958 ' (parent: ' .
5959 $cgi->a({-href => href(action=>"commitdiff",
5960 hash=>$co{'parent'})},
5961 esc_html(substr($co{'parent'}, 0, 7))) .
5962 ')';
5963 } else {
5964 # merge commit
5965 if ($hash_parent eq '--cc') {
5966 $formats_nav .= ' | ' .
5967 $cgi->a({-href => href(action=>"commitdiff",
5968 hash=>$hash, hash_parent=>'-c')},
5969 'combined');
5970 } else { # $hash_parent eq '-c'
5971 $formats_nav .= ' | ' .
5972 $cgi->a({-href => href(action=>"commitdiff",
5973 hash=>$hash, hash_parent=>'--cc')},
5974 'compact');
5976 $formats_nav .=
5977 ' (merge: ' .
5978 join(' ', map {
5979 $cgi->a({-href => href(action=>"commitdiff",
5980 hash=>$_)},
5981 esc_html(substr($_, 0, 7)));
5982 } @{$co{'parents'}} ) .
5983 ')';
5987 my $hash_parent_param = $hash_parent;
5988 if (!defined $hash_parent_param) {
5989 # --cc for multiple parents, --root for parentless
5990 $hash_parent_param =
5991 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5994 # read commitdiff
5995 my $fd;
5996 my @difftree;
5997 if ($format eq 'html') {
5998 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5999 "--no-commit-id", "--patch-with-raw", "--full-index",
6000 $hash_parent_param, $hash, "--"
6001 or die_error(500, "Open git-diff-tree failed");
6003 while (my $line = <$fd>) {
6004 chomp $line;
6005 # empty line ends raw part of diff-tree output
6006 last unless $line;
6007 push @difftree, scalar parse_difftree_raw_line($line);
6010 } elsif ($format eq 'plain') {
6011 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6012 '-p', $hash_parent_param, $hash, "--"
6013 or die_error(500, "Open git-diff-tree failed");
6014 } elsif ($format eq 'patch') {
6015 # For commit ranges, we limit the output to the number of
6016 # patches specified in the 'patches' feature.
6017 # For single commits, we limit the output to a single patch,
6018 # diverging from the git-format-patch default.
6019 my @commit_spec = ();
6020 if ($hash_parent) {
6021 if ($patch_max > 0) {
6022 push @commit_spec, "-$patch_max";
6024 push @commit_spec, '-n', "$hash_parent..$hash";
6025 } else {
6026 if ($params{-single}) {
6027 push @commit_spec, '-1';
6028 } else {
6029 if ($patch_max > 0) {
6030 push @commit_spec, "-$patch_max";
6032 push @commit_spec, "-n";
6034 push @commit_spec, '--root', $hash;
6036 open $fd, "-|", git_cmd(), "format-patch", '--encoding=utf8',
6037 '--stdout', @commit_spec
6038 or die_error(500, "Open git-format-patch failed");
6039 } else {
6040 die_error(400, "Unknown commitdiff format");
6043 # non-textual hash id's can be cached
6044 my $expires;
6045 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6046 $expires = "+1d";
6049 # write commit message
6050 if ($format eq 'html') {
6051 my $refs = git_get_references();
6052 my $ref = format_ref_marker($refs, $co{'id'});
6054 git_header_html(undef, $expires);
6055 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6056 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6057 print "<div class=\"title_text\">\n" .
6058 "<table class=\"object_header\">\n";
6059 git_print_authorship_rows(\%co);
6060 print "</table>".
6061 "</div>\n";
6062 print "<div class=\"page_body\">\n";
6063 if (@{$co{'comment'}} > 1) {
6064 print "<div class=\"log\">\n";
6065 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6066 print "</div>\n"; # class="log"
6069 } elsif ($format eq 'plain') {
6070 my $refs = git_get_references("tags");
6071 my $tagname = git_get_rev_name_tags($hash);
6072 my $filename = basename($project) . "-$hash.patch";
6074 print $cgi->header(
6075 -type => 'text/plain',
6076 -charset => 'utf-8',
6077 -expires => $expires,
6078 -content_disposition => 'inline; filename="' . "$filename" . '"');
6079 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
6080 print "From: " . to_utf8($co{'author'}) . "\n";
6081 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6082 print "Subject: " . to_utf8($co{'title'}) . "\n";
6084 print "X-Git-Tag: $tagname\n" if $tagname;
6085 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6087 foreach my $line (@{$co{'comment'}}) {
6088 print to_utf8($line) . "\n";
6090 print "---\n\n";
6091 } elsif ($format eq 'patch') {
6092 my $filename = basename($project) . "-$hash.patch";
6094 print $cgi->header(
6095 -type => 'text/plain',
6096 -charset => 'utf-8',
6097 -expires => $expires,
6098 -content_disposition => 'inline; filename="' . "$filename" . '"');
6101 # write patch
6102 if ($format eq 'html') {
6103 my $use_parents = !defined $hash_parent ||
6104 $hash_parent eq '-c' || $hash_parent eq '--cc';
6105 git_difftree_body(\@difftree, $hash,
6106 $use_parents ? @{$co{'parents'}} : $hash_parent);
6107 print "<br/>\n";
6109 git_patchset_body($fd, \@difftree, $hash,
6110 $use_parents ? @{$co{'parents'}} : $hash_parent);
6111 close $fd;
6112 print "</div>\n"; # class="page_body"
6113 git_footer_html();
6115 } elsif ($format eq 'plain') {
6116 local $/ = undef;
6117 print <$fd>;
6118 close $fd
6119 or print "Reading git-diff-tree failed\n";
6120 } elsif ($format eq 'patch') {
6121 local $/ = undef;
6122 print <$fd>;
6123 close $fd
6124 or print "Reading git-format-patch failed\n";
6128 sub git_commitdiff_plain {
6129 git_commitdiff(-format => 'plain');
6132 # format-patch-style patches
6133 sub git_patch {
6134 git_commitdiff(-format => 'patch', -single => 1);
6137 sub git_patches {
6138 git_commitdiff(-format => 'patch');
6141 sub git_history {
6142 if (!defined $hash_base) {
6143 $hash_base = git_get_head_hash($project);
6145 if (!defined $page) {
6146 $page = 0;
6148 my $ftype;
6149 my %co = parse_commit($hash_base)
6150 or die_error(404, "Unknown commit object");
6152 my $refs = git_get_references();
6153 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
6155 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
6156 $file_name, "--full-history")
6157 or die_error(404, "No such file or directory on given branch");
6159 if (!defined $hash && defined $file_name) {
6160 # some commits could have deleted file in question,
6161 # and not have it in tree, but one of them has to have it
6162 for (my $i = 0; $i <= @commitlist; $i++) {
6163 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
6164 last if defined $hash;
6167 if (defined $hash) {
6168 $ftype = git_get_type($hash);
6170 if (!defined $ftype) {
6171 die_error(500, "Unknown type of object");
6174 my $paging_nav = '';
6175 if ($page > 0) {
6176 $paging_nav .=
6177 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
6178 file_name=>$file_name)},
6179 "first");
6180 $paging_nav .= " &sdot; " .
6181 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6182 -accesskey => "p", -title => "Alt-p"}, "prev");
6183 } else {
6184 $paging_nav .= "first";
6185 $paging_nav .= " &sdot; prev";
6187 my $next_link = '';
6188 if ($#commitlist >= 100) {
6189 $next_link =
6190 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6191 -accesskey => "n", -title => "Alt-n"}, "next");
6192 $paging_nav .= " &sdot; $next_link";
6193 } else {
6194 $paging_nav .= " &sdot; next";
6197 git_header_html();
6198 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
6199 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6200 git_print_page_path($file_name, $ftype, $hash_base);
6202 git_history_body(\@commitlist, 0, 99,
6203 $refs, $hash_base, $ftype, $next_link);
6205 git_footer_html();
6208 sub git_search {
6209 gitweb_check_feature('search') or die_error(403, "Search is disabled");
6210 if (!defined $searchtext) {
6211 die_error(400, "Text field is empty");
6213 if (!defined $hash) {
6214 $hash = git_get_head_hash($project);
6216 my %co = parse_commit($hash);
6217 if (!%co) {
6218 die_error(404, "Unknown commit object");
6220 if (!defined $page) {
6221 $page = 0;
6224 $searchtype ||= 'commit';
6225 if ($searchtype eq 'pickaxe') {
6226 # pickaxe may take all resources of your box and run for several minutes
6227 # with every query - so decide by yourself how public you make this feature
6228 gitweb_check_feature('pickaxe')
6229 or die_error(403, "Pickaxe is disabled");
6231 if ($searchtype eq 'grep') {
6232 gitweb_check_feature('grep')
6233 or die_error(403, "Grep is disabled");
6236 git_header_html();
6238 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6239 my $greptype;
6240 if ($searchtype eq 'commit') {
6241 $greptype = "--grep=";
6242 } elsif ($searchtype eq 'author') {
6243 $greptype = "--author=";
6244 } elsif ($searchtype eq 'committer') {
6245 $greptype = "--committer=";
6247 $greptype .= $searchtext;
6248 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6249 $greptype, '--regexp-ignore-case',
6250 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6252 my $paging_nav = '';
6253 if ($page > 0) {
6254 $paging_nav .=
6255 $cgi->a({-href => href(action=>"search", hash=>$hash,
6256 searchtext=>$searchtext,
6257 searchtype=>$searchtype)},
6258 "first");
6259 $paging_nav .= " &sdot; " .
6260 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6261 -accesskey => "p", -title => "Alt-p"}, "prev");
6262 } else {
6263 $paging_nav .= "first";
6264 $paging_nav .= " &sdot; prev";
6266 my $next_link = '';
6267 if ($#commitlist >= 100) {
6268 $next_link =
6269 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6270 -accesskey => "n", -title => "Alt-n"}, "next");
6271 $paging_nav .= " &sdot; $next_link";
6272 } else {
6273 $paging_nav .= " &sdot; next";
6276 if ($#commitlist >= 100) {
6279 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6280 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6281 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6284 if ($searchtype eq 'pickaxe') {
6285 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6286 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6288 print "<table class=\"pickaxe search\">\n";
6289 my $alternate = 1;
6290 local $/ = "\n";
6291 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6292 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6293 ($search_use_regexp ? '--pickaxe-regex' : ());
6294 undef %co;
6295 my @files;
6296 while (my $line = <$fd>) {
6297 chomp $line;
6298 next unless $line;
6300 my %set = parse_difftree_raw_line($line);
6301 if (defined $set{'commit'}) {
6302 # finish previous commit
6303 if (%co) {
6304 print "</td>\n" .
6305 "<td class=\"link\">" .
6306 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6307 " | " .
6308 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6309 print "</td>\n" .
6310 "</tr>\n";
6313 if ($alternate) {
6314 print "<tr class=\"dark\">\n";
6315 } else {
6316 print "<tr class=\"light\">\n";
6318 $alternate ^= 1;
6319 %co = parse_commit($set{'commit'});
6320 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6321 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6322 "<td><i>$author</i></td>\n" .
6323 "<td>" .
6324 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6325 -class => "list subject"},
6326 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6327 } elsif (defined $set{'to_id'}) {
6328 next if ($set{'to_id'} =~ m/^0{40}$/);
6330 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6331 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6332 -class => "list"},
6333 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6334 "<br/>\n";
6337 close $fd;
6339 # finish last commit (warning: repetition!)
6340 if (%co) {
6341 print "</td>\n" .
6342 "<td class=\"link\">" .
6343 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6344 " | " .
6345 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6346 print "</td>\n" .
6347 "</tr>\n";
6350 print "</table>\n";
6353 if ($searchtype eq 'grep') {
6354 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6355 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6357 print "<table class=\"grep_search\">\n";
6358 my $alternate = 1;
6359 my $matches = 0;
6360 local $/ = "\n";
6361 open my $fd, "-|", git_cmd(), 'grep', '-n',
6362 $search_use_regexp ? ('-E', '-i') : '-F',
6363 $searchtext, $co{'tree'};
6364 my $lastfile = '';
6365 while (my $line = <$fd>) {
6366 chomp $line;
6367 my ($file, $lno, $ltext, $binary);
6368 last if ($matches++ > 1000);
6369 if ($line =~ /^Binary file (.+) matches$/) {
6370 $file = $1;
6371 $binary = 1;
6372 } else {
6373 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6375 if ($file ne $lastfile) {
6376 $lastfile and print "</td></tr>\n";
6377 if ($alternate++) {
6378 print "<tr class=\"dark\">\n";
6379 } else {
6380 print "<tr class=\"light\">\n";
6382 print "<td class=\"list\">".
6383 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6384 file_name=>"$file"),
6385 -class => "list"}, esc_path($file));
6386 print "</td><td>\n";
6387 $lastfile = $file;
6389 if ($binary) {
6390 print "<div class=\"binary\">Binary file</div>\n";
6391 } else {
6392 $ltext = untabify($ltext);
6393 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6394 $ltext = esc_html($1, -nbsp=>1);
6395 $ltext .= '<span class="match">';
6396 $ltext .= esc_html($2, -nbsp=>1);
6397 $ltext .= '</span>';
6398 $ltext .= esc_html($3, -nbsp=>1);
6399 } else {
6400 $ltext = esc_html($ltext, -nbsp=>1);
6402 print "<div class=\"pre\">" .
6403 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6404 file_name=>"$file").'#l'.$lno,
6405 -class => "linenr"}, sprintf('%4i', $lno))
6406 . ' ' . $ltext . "</div>\n";
6409 if ($lastfile) {
6410 print "</td></tr>\n";
6411 if ($matches > 1000) {
6412 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6414 } else {
6415 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6417 close $fd;
6419 print "</table>\n";
6421 git_footer_html();
6424 sub git_search_help {
6425 git_header_html();
6426 git_print_page_nav('','', $hash,$hash,$hash);
6427 print <<EOT;
6428 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6429 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6430 the pattern entered is recognized as the POSIX extended
6431 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6432 insensitive).</p>
6433 <dl>
6434 <dt><b>commit</b></dt>
6435 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6437 my $have_grep = gitweb_check_feature('grep');
6438 if ($have_grep) {
6439 print <<EOT;
6440 <dt><b>grep</b></dt>
6441 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6442 a different one) are searched for the given pattern. On large trees, this search can take
6443 a while and put some strain on the server, so please use it with some consideration. Note that
6444 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6445 case-sensitive.</dd>
6448 print <<EOT;
6449 <dt><b>author</b></dt>
6450 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6451 <dt><b>committer</b></dt>
6452 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6454 my $have_pickaxe = gitweb_check_feature('pickaxe');
6455 if ($have_pickaxe) {
6456 print <<EOT;
6457 <dt><b>pickaxe</b></dt>
6458 <dd>All commits that caused the string to appear or disappear from any file (changes that
6459 added, removed or "modified" the string) will be listed. This search can take a while and
6460 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6461 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6464 print "</dl>\n";
6465 git_footer_html();
6468 sub git_shortlog {
6469 my $head = git_get_head_hash($project);
6470 if (!defined $hash) {
6471 $hash = $head;
6473 if (!defined $page) {
6474 $page = 0;
6476 my $refs = git_get_references();
6478 my $commit_hash = $hash;
6479 if (defined $hash_parent) {
6480 $commit_hash = "$hash_parent..$hash";
6482 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
6484 my $paging_nav = format_log_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
6486 my $next_link = '';
6487 if ($#commitlist >= 100) {
6488 $next_link =
6489 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6490 -accesskey => "n", -title => "Alt-n"}, "next");
6492 my $patch_max = gitweb_check_feature('patches');
6493 if ($patch_max) {
6494 if ($patch_max < 0 || @commitlist <= $patch_max) {
6495 $paging_nav .= " &sdot; " .
6496 $cgi->a({-href => href(action=>"patches", -replay=>1)},
6497 "patches");
6501 git_header_html();
6502 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
6503 git_print_header_div('summary', $project);
6505 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
6507 git_footer_html();
6510 ## ......................................................................
6511 ## feeds (RSS, Atom; OPML)
6513 sub git_feed {
6514 my $format = shift || 'atom';
6515 my $have_blame = gitweb_check_feature('blame');
6517 # Atom: http://www.atomenabled.org/developers/syndication/
6518 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6519 if ($format ne 'rss' && $format ne 'atom') {
6520 die_error(400, "Unknown web feed format");
6523 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6524 my $head = $hash || 'HEAD';
6525 my @commitlist = parse_commits($head, 150, 0, $file_name);
6527 my %latest_commit;
6528 my %latest_date;
6529 my $content_type = "application/$format+xml";
6530 if (defined $cgi->http('HTTP_ACCEPT') &&
6531 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6532 # browser (feed reader) prefers text/xml
6533 $content_type = 'text/xml';
6535 if (defined($commitlist[0])) {
6536 %latest_commit = %{$commitlist[0]};
6537 my $latest_epoch = $latest_commit{'committer_epoch'};
6538 %latest_date = parse_date($latest_epoch);
6539 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6540 if (defined $if_modified) {
6541 my $since;
6542 if (eval { require HTTP::Date; 1; }) {
6543 $since = HTTP::Date::str2time($if_modified);
6544 } elsif (eval { require Time::ParseDate; 1; }) {
6545 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6547 if (defined $since && $latest_epoch <= $since) {
6548 print $cgi->header(
6549 -type => $content_type,
6550 -charset => 'utf-8',
6551 -last_modified => $latest_date{'rfc2822'},
6552 -status => '304 Not Modified');
6553 return;
6556 print $cgi->header(
6557 -type => $content_type,
6558 -charset => 'utf-8',
6559 -last_modified => $latest_date{'rfc2822'});
6560 } else {
6561 print $cgi->header(
6562 -type => $content_type,
6563 -charset => 'utf-8');
6566 # Optimization: skip generating the body if client asks only
6567 # for Last-Modified date.
6568 return if ($cgi->request_method() eq 'HEAD');
6570 # header variables
6571 my $title = "$site_name - $project/$action";
6572 my $feed_type = 'log';
6573 if (defined $hash) {
6574 $title .= " - '$hash'";
6575 $feed_type = 'branch log';
6576 if (defined $file_name) {
6577 $title .= " :: $file_name";
6578 $feed_type = 'history';
6580 } elsif (defined $file_name) {
6581 $title .= " - $file_name";
6582 $feed_type = 'history';
6584 $title .= " $feed_type";
6585 my $descr = git_get_project_description($project);
6586 if (defined $descr) {
6587 $descr = esc_html($descr);
6588 } else {
6589 $descr = "$project " .
6590 ($format eq 'rss' ? 'RSS' : 'Atom') .
6591 " feed";
6593 my $owner = git_get_project_owner($project);
6594 $owner = esc_html($owner);
6596 #header
6597 my $alt_url;
6598 if (defined $file_name) {
6599 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6600 } elsif (defined $hash) {
6601 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6602 } else {
6603 $alt_url = href(-full=>1, action=>"summary");
6605 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6606 if ($format eq 'rss') {
6607 print <<XML;
6608 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6609 <channel>
6611 print "<title>$title</title>\n" .
6612 "<link>$alt_url</link>\n" .
6613 "<description>$descr</description>\n" .
6614 "<language>en</language>\n" .
6615 # project owner is responsible for 'editorial' content
6616 "<managingEditor>$owner</managingEditor>\n";
6617 if (defined $logo || defined $favicon) {
6618 # prefer the logo to the favicon, since RSS
6619 # doesn't allow both
6620 my $img = esc_url($logo || $favicon);
6621 print "<image>\n" .
6622 "<url>$img</url>\n" .
6623 "<title>$title</title>\n" .
6624 "<link>$alt_url</link>\n" .
6625 "</image>\n";
6627 if (%latest_date) {
6628 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6629 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6631 print "<generator>gitweb v.$version/$git_version</generator>\n";
6632 } elsif ($format eq 'atom') {
6633 print <<XML;
6634 <feed xmlns="http://www.w3.org/2005/Atom">
6636 print "<title>$title</title>\n" .
6637 "<subtitle>$descr</subtitle>\n" .
6638 '<link rel="alternate" type="text/html" href="' .
6639 $alt_url . '" />' . "\n" .
6640 '<link rel="self" type="' . $content_type . '" href="' .
6641 $cgi->self_url() . '" />' . "\n" .
6642 "<id>" . href(-full=>1) . "</id>\n" .
6643 # use project owner for feed author
6644 '<author><name>'. email_obfuscate($owner) . '</name></author>\n';
6645 if (defined $favicon) {
6646 print "<icon>" . esc_url($favicon) . "</icon>\n";
6648 if (defined $logo_url) {
6649 # not twice as wide as tall: 72 x 27 pixels
6650 print "<logo>" . esc_url($logo) . "</logo>\n";
6652 if (! %latest_date) {
6653 # dummy date to keep the feed valid until commits trickle in:
6654 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6655 } else {
6656 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6658 print "<generator version='$version/$git_version'>gitweb</generator>\n";
6661 # contents
6662 for (my $i = 0; $i <= $#commitlist; $i++) {
6663 my %co = %{$commitlist[$i]};
6664 my $commit = $co{'id'};
6665 # we read 150, we always show 30 and the ones more recent than 48 hours
6666 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6667 last;
6669 my %cd = parse_date($co{'author_epoch'});
6671 # get list of changed files
6672 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6673 $co{'parent'} || "--root",
6674 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6675 or next;
6676 my @difftree = map { chomp; $_ } <$fd>;
6677 close $fd
6678 or next;
6680 # print element (entry, item)
6681 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6682 if ($format eq 'rss') {
6683 print "<item>\n" .
6684 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6685 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6686 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6687 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6688 "<link>$co_url</link>\n" .
6689 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6690 "<content:encoded>" .
6691 "<![CDATA[\n";
6692 } elsif ($format eq 'atom') {
6693 print "<entry>\n" .
6694 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6695 "<updated>$cd{'iso-8601'}</updated>\n" .
6696 "<author>\n" .
6697 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6698 if ($co{'author_email'}) {
6699 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6701 print "</author>\n" .
6702 # use committer for contributor
6703 "<contributor>\n" .
6704 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6705 if ($co{'committer_email'}) {
6706 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6708 print "</contributor>\n" .
6709 "<published>$cd{'iso-8601'}</published>\n" .
6710 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6711 "<id>$co_url</id>\n" .
6712 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6713 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6715 my $comment = $co{'comment'};
6716 print "<pre>\n";
6717 foreach my $line (@$comment) {
6718 $line = esc_html($line);
6719 print "$line\n";
6721 print "</pre><ul>\n";
6722 foreach my $difftree_line (@difftree) {
6723 my %difftree = parse_difftree_raw_line($difftree_line);
6724 next if !$difftree{'from_id'};
6726 my $file = $difftree{'file'} || $difftree{'to_file'};
6728 print "<li>" .
6729 "[" .
6730 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6731 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6732 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6733 file_name=>$file, file_parent=>$difftree{'from_file'}),
6734 -title => "diff"}, 'D');
6735 if ($have_blame) {
6736 print $cgi->a({-href => href(-full=>1, action=>"blame",
6737 file_name=>$file, hash_base=>$commit), -class => "blamelink",
6738 -title => "blame"}, 'B');
6740 # if this is not a feed of a file history
6741 if (!defined $file_name || $file_name ne $file) {
6742 print $cgi->a({-href => href(-full=>1, action=>"history",
6743 file_name=>$file, hash=>$commit),
6744 -title => "history"}, 'H');
6746 $file = esc_path($file);
6747 print "] ".
6748 "$file</li>\n";
6750 if ($format eq 'rss') {
6751 print "</ul>]]>\n" .
6752 "</content:encoded>\n" .
6753 "</item>\n";
6754 } elsif ($format eq 'atom') {
6755 print "</ul>\n</div>\n" .
6756 "</content>\n" .
6757 "</entry>\n";
6761 # end of feed
6762 if ($format eq 'rss') {
6763 print "</channel>\n</rss>\n";
6764 } elsif ($format eq 'atom') {
6765 print "</feed>\n";
6769 sub git_rss {
6770 git_feed('rss');
6773 sub git_atom {
6774 git_feed('atom');
6777 sub git_opml {
6778 my @list = git_get_projects_list();
6780 print $cgi->header(
6781 -type => 'text/xml',
6782 -charset => 'utf-8',
6783 -content_disposition => 'inline; filename="opml.xml"');
6785 print <<XML;
6786 <?xml version="1.0" encoding="utf-8"?>
6787 <opml version="1.0">
6788 <head>
6789 <title>$site_name OPML Export</title>
6790 </head>
6791 <body>
6792 <outline text="git RSS feeds">
6795 foreach my $pr (@list) {
6796 my %proj = %$pr;
6797 my $head = git_get_head_hash($proj{'path'});
6798 if (!defined $head) {
6799 next;
6801 $git_dir = "$projectroot/$proj{'path'}";
6802 my %co = parse_commit($head);
6803 if (!%co) {
6804 next;
6807 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6808 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
6809 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
6810 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6812 print <<XML;
6813 </outline>
6814 </body>
6815 </opml>