Merge branch 'next' into refs/top-bases/pu
[git/gitweb.git] / gitweb / gitweb.perl
blob2a8cedca41b7bdbfd0543b5c332856591faa44a0
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 # default blob_plain mimetype and default charset for text/plain blob
129 our $default_blob_plain_mimetype = 'text/plain';
130 our $default_text_plain_charset = undef;
132 # file to use for guessing MIME types before trying /etc/mime.types
133 # (relative to the current git repository)
134 our $mimetypes_file = undef;
136 # assume this charset if line contains non-UTF-8 characters;
137 # it should be valid encoding (see Encoding::Supported(3pm) for list),
138 # for which encoding all byte sequences are valid, for example
139 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
140 # could be even 'utf-8' for the old behavior)
141 our $fallback_encoding = 'latin1';
143 # rename detection options for git-diff and git-diff-tree
144 # - default is '-M', with the cost proportional to
145 # (number of removed files) * (number of new files).
146 # - more costly is '-C' (which implies '-M'), with the cost proportional to
147 # (number of changed files + number of removed files) * (number of new files)
148 # - even more costly is '-C', '--find-copies-harder' with cost
149 # (number of files in the original tree) * (number of new files)
150 # - one might want to include '-B' option, e.g. '-B', '-M'
151 our @diff_opts = ('-M'); # taken from git_commit
153 # Disables features that would allow repository owners to inject script into
154 # the gitweb domain.
155 our $prevent_xss = 0;
157 # projects list cache for busy sites with many projects;
158 # if you set this to non-zero, it will be used as the cached
159 # index lifetime in minutes
161 # the cached list version is stored in $cache_dir/$cache_name and can
162 # be tweaked by other scripts running with the same uid as gitweb -
163 # use this ONLY at secure installations; only single gitweb project
164 # root per system is supported, unless you tweak configuration!
165 our $projlist_cache_lifetime = 0; # in minutes
166 # FHS compliant $cache_dir would be "/var/cache/gitweb"
167 our $cache_dir =
168 (defined $ENV{'TMPDIR'} ? $ENV{'TMPDIR'} : '/tmp').'/gitweb';
169 our $projlist_cache_name = 'gitweb.index.cache';
171 # information about snapshot formats that gitweb is capable of serving
172 our %known_snapshot_formats = (
173 # name => {
174 # 'display' => display name,
175 # 'type' => mime type,
176 # 'suffix' => filename suffix,
177 # 'format' => --format for git-archive,
178 # 'compressor' => [compressor command and arguments]
179 # (array reference, optional)
180 # 'disabled' => boolean (optional)}
182 'tgz' => {
183 'display' => 'tar.gz',
184 'type' => 'application/x-gzip',
185 'suffix' => '.tar.gz',
186 'format' => 'tar',
187 'compressor' => ['gzip']},
189 'tbz2' => {
190 'display' => 'tar.bz2',
191 'type' => 'application/x-bzip2',
192 'suffix' => '.tar.bz2',
193 'format' => 'tar',
194 'compressor' => ['bzip2']},
196 'txz' => {
197 'display' => 'tar.xz',
198 'type' => 'application/x-xz',
199 'suffix' => '.tar.xz',
200 'format' => 'tar',
201 'compressor' => ['xz'],
202 'disabled' => 1},
204 'zip' => {
205 'display' => 'zip',
206 'type' => 'application/x-zip',
207 'suffix' => '.zip',
208 'format' => 'zip'},
211 # Aliases so we understand old gitweb.snapshot values in repository
212 # configuration.
213 our %known_snapshot_format_aliases = (
214 'gzip' => 'tgz',
215 'bzip2' => 'tbz2',
216 'xz' => 'txz',
218 # backward compatibility: legacy gitweb config support
219 'x-gzip' => undef, 'gz' => undef,
220 'x-bzip2' => undef, 'bz2' => undef,
221 'x-zip' => undef, '' => undef,
224 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
225 # are changed, it may be appropriate to change these values too via
226 # $GITWEB_CONFIG.
227 our %avatar_size = (
228 'default' => 16,
229 'double' => 32
232 # You define site-wide feature defaults here; override them with
233 # $GITWEB_CONFIG as necessary.
234 our %feature = (
235 # feature => {
236 # 'sub' => feature-sub (subroutine),
237 # 'override' => allow-override (boolean),
238 # 'default' => [ default options...] (array reference)}
240 # if feature is overridable (it means that allow-override has true value),
241 # then feature-sub will be called with default options as parameters;
242 # return value of feature-sub indicates if to enable specified feature
244 # if there is no 'sub' key (no feature-sub), then feature cannot be
245 # overriden
247 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
248 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
249 # is enabled
251 # Enable the 'blame' blob view, showing the last commit that modified
252 # each line in the file. This can be very CPU-intensive.
254 # To enable system wide have in $GITWEB_CONFIG
255 # $feature{'blame'}{'default'} = [1];
256 # To have project specific config enable override in $GITWEB_CONFIG
257 # $feature{'blame'}{'override'} = 1;
258 # and in project config gitweb.blame = 0|1;
259 'blame' => {
260 'sub' => sub { feature_bool('blame', @_) },
261 'override' => 0,
262 'default' => [0]},
264 # Enable the 'snapshot' link, providing a compressed archive of any
265 # tree. This can potentially generate high traffic if you have large
266 # project.
268 # Value is a list of formats defined in %known_snapshot_formats that
269 # you wish to offer.
270 # To disable system wide have in $GITWEB_CONFIG
271 # $feature{'snapshot'}{'default'} = [];
272 # To have project specific config enable override in $GITWEB_CONFIG
273 # $feature{'snapshot'}{'override'} = 1;
274 # and in project config, a comma-separated list of formats or "none"
275 # to disable. Example: gitweb.snapshot = tbz2,zip;
276 'snapshot' => {
277 'sub' => \&feature_snapshot,
278 'override' => 0,
279 'default' => ['tgz']},
281 # Enable text search, which will list the commits which match author,
282 # committer or commit text to a given string. Enabled by default.
283 # Project specific override is not supported.
284 'search' => {
285 'override' => 0,
286 'default' => [1]},
288 # Enable grep search, which will list the files in currently selected
289 # tree containing the given string. Enabled by default. This can be
290 # potentially CPU-intensive, of course.
292 # To enable system wide have in $GITWEB_CONFIG
293 # $feature{'grep'}{'default'} = [1];
294 # To have project specific config enable override in $GITWEB_CONFIG
295 # $feature{'grep'}{'override'} = 1;
296 # and in project config gitweb.grep = 0|1;
297 'grep' => {
298 'sub' => sub { feature_bool('grep', @_) },
299 'override' => 0,
300 'default' => [1]},
302 # Enable the pickaxe search, which will list the commits that modified
303 # a given string in a file. This can be practical and quite faster
304 # alternative to 'blame', but still potentially CPU-intensive.
306 # To enable system wide have in $GITWEB_CONFIG
307 # $feature{'pickaxe'}{'default'} = [1];
308 # To have project specific config enable override in $GITWEB_CONFIG
309 # $feature{'pickaxe'}{'override'} = 1;
310 # and in project config gitweb.pickaxe = 0|1;
311 'pickaxe' => {
312 'sub' => sub { feature_bool('pickaxe', @_) },
313 'override' => 0,
314 'default' => [1]},
316 # Enable showing size of blobs in a 'tree' view, in a separate
317 # column, similar to what 'ls -l' does. This cost a bit of IO.
319 # To disable system wide have in $GITWEB_CONFIG
320 # $feature{'show-sizes'}{'default'} = [0];
321 # To have project specific config enable override in $GITWEB_CONFIG
322 # $feature{'show-sizes'}{'override'} = 1;
323 # and in project config gitweb.showsizes = 0|1;
324 'show-sizes' => {
325 'sub' => sub { feature_bool('showsizes', @_) },
326 'override' => 0,
327 'default' => [1]},
329 # Make gitweb use an alternative format of the URLs which can be
330 # more readable and natural-looking: project name is embedded
331 # directly in the path and the query string contains other
332 # auxiliary information. All gitweb installations recognize
333 # URL in either format; this configures in which formats gitweb
334 # generates links.
336 # To enable system wide have in $GITWEB_CONFIG
337 # $feature{'pathinfo'}{'default'} = [1];
338 # Project specific override is not supported.
340 # Note that you will need to change the default location of CSS,
341 # favicon, logo and possibly other files to an absolute URL. Also,
342 # if gitweb.cgi serves as your indexfile, you will need to force
343 # $my_uri to contain the script name in your $GITWEB_CONFIG.
344 'pathinfo' => {
345 'override' => 0,
346 'default' => [0]},
348 # Make gitweb consider projects in project root subdirectories
349 # to be forks of existing projects. Given project $projname.git,
350 # projects matching $projname/*.git will not be shown in the main
351 # projects list, instead a '+' mark will be added to $projname
352 # there and a 'forks' view will be enabled for the project, listing
353 # all the forks. If project list is taken from a file, forks have
354 # to be listed after the main project.
356 # To enable system wide have in $GITWEB_CONFIG
357 # $feature{'forks'}{'default'} = [1];
358 # Project specific override is not supported.
359 'forks' => {
360 'override' => 0,
361 'default' => [0]},
363 # Insert custom links to the action bar of all project pages.
364 # This enables you mainly to link to third-party scripts integrating
365 # into gitweb; e.g. git-browser for graphical history representation
366 # or custom web-based repository administration interface.
368 # The 'default' value consists of a list of triplets in the form
369 # (label, link, position) where position is the label after which
370 # to insert the link and link is a format string where %n expands
371 # to the project name, %f to the project path within the filesystem,
372 # %h to the current hash (h gitweb parameter) and %b to the current
373 # hash base (hb gitweb parameter); %% expands to %.
375 # To enable system wide have in $GITWEB_CONFIG e.g.
376 # $feature{'actions'}{'default'} = [('graphiclog',
377 # '/git-browser/by-commit.html?r=%n', 'summary')];
378 # Project specific override is not supported.
379 'actions' => {
380 'override' => 0,
381 'default' => []},
383 # Allow gitweb scan project content tags described in ctags/
384 # of project repository, and display the popular Web 2.0-ish
385 # "tag cloud" near the project list. Note that this is something
386 # COMPLETELY different from the normal Git tags.
388 # gitweb by itself can show existing tags, but it does not handle
389 # tagging itself; you need an external application for that.
390 # For an example script, check Girocco's cgi/tagproj.cgi.
391 # You may want to install the HTML::TagCloud Perl module to get
392 # a pretty tag cloud instead of just a list of tags.
394 # To enable system wide have in $GITWEB_CONFIG
395 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
396 # Project specific override is not supported.
397 'ctags' => {
398 'override' => 0,
399 'default' => [0]},
401 # The maximum number of patches in a patchset generated in patch
402 # view. Set this to 0 or undef to disable patch view, or to a
403 # negative number to remove any limit.
405 # To disable system wide have in $GITWEB_CONFIG
406 # $feature{'patches'}{'default'} = [0];
407 # To have project specific config enable override in $GITWEB_CONFIG
408 # $feature{'patches'}{'override'} = 1;
409 # and in project config gitweb.patches = 0|n;
410 # where n is the maximum number of patches allowed in a patchset.
411 'patches' => {
412 'sub' => \&feature_patches,
413 'override' => 0,
414 'default' => [16]},
416 # Avatar support. When this feature is enabled, views such as
417 # shortlog or commit will display an avatar associated with
418 # the email of the committer(s) and/or author(s).
420 # Currently available providers are gravatar and picon.
421 # If an unknown provider is specified, the feature is disabled.
423 # Gravatar depends on Digest::MD5.
424 # Picon currently relies on the indiana.edu database.
426 # To enable system wide have in $GITWEB_CONFIG
427 # $feature{'avatar'}{'default'} = ['<provider>'];
428 # where <provider> is either gravatar or picon.
429 # To have project specific config enable override in $GITWEB_CONFIG
430 # $feature{'avatar'}{'override'} = 1;
431 # and in project config gitweb.avatar = <provider>;
432 'avatar' => {
433 'sub' => \&feature_avatar,
434 'override' => 0,
435 'default' => ['']},
438 sub gitweb_get_feature {
439 my ($name) = @_;
440 return unless exists $feature{$name};
441 my ($sub, $override, @defaults) = (
442 $feature{$name}{'sub'},
443 $feature{$name}{'override'},
444 @{$feature{$name}{'default'}});
445 if (!$override) { return @defaults; }
446 if (!defined $sub) {
447 warn "feature $name is not overridable";
448 return @defaults;
450 return $sub->(@defaults);
453 # A wrapper to check if a given feature is enabled.
454 # With this, you can say
456 # my $bool_feat = gitweb_check_feature('bool_feat');
457 # gitweb_check_feature('bool_feat') or somecode;
459 # instead of
461 # my ($bool_feat) = gitweb_get_feature('bool_feat');
462 # (gitweb_get_feature('bool_feat'))[0] or somecode;
464 sub gitweb_check_feature {
465 return (gitweb_get_feature(@_))[0];
469 sub feature_bool {
470 my $key = shift;
471 my ($val) = git_get_project_config($key, '--bool');
473 if (!defined $val) {
474 return ($_[0]);
475 } elsif ($val eq 'true') {
476 return (1);
477 } elsif ($val eq 'false') {
478 return (0);
482 sub feature_snapshot {
483 my (@fmts) = @_;
485 my ($val) = git_get_project_config('snapshot');
487 if ($val) {
488 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
491 return @fmts;
494 sub feature_patches {
495 my @val = (git_get_project_config('patches', '--int'));
497 if (@val) {
498 return @val;
501 return ($_[0]);
504 sub feature_avatar {
505 my @val = (git_get_project_config('avatar'));
507 return @val ? @val : @_;
510 # checking HEAD file with -e is fragile if the repository was
511 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
512 # and then pruned.
513 sub check_head_link {
514 my ($dir) = @_;
515 my $headfile = "$dir/HEAD";
516 return ((-e $headfile) ||
517 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
520 sub check_export_ok {
521 my ($dir) = @_;
522 return (check_head_link($dir) &&
523 (!$export_ok || -e "$dir/$export_ok") &&
524 (!$export_auth_hook || $export_auth_hook->($dir)));
527 # process alternate names for backward compatibility
528 # filter out unsupported (unknown) snapshot formats
529 sub filter_snapshot_fmts {
530 my @fmts = @_;
532 @fmts = map {
533 exists $known_snapshot_format_aliases{$_} ?
534 $known_snapshot_format_aliases{$_} : $_} @fmts;
535 @fmts = grep {
536 exists $known_snapshot_formats{$_} &&
537 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
540 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
541 if (-e $GITWEB_CONFIG) {
542 do $GITWEB_CONFIG;
543 } else {
544 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
545 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
548 # version of the core git binary
549 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
551 $projects_list ||= $projectroot;
553 # ======================================================================
554 # input validation and dispatch
556 # input parameters can be collected from a variety of sources (presently, CGI
557 # and PATH_INFO), so we define an %input_params hash that collects them all
558 # together during validation: this allows subsequent uses (e.g. href()) to be
559 # agnostic of the parameter origin
561 our %input_params = ();
563 # input parameters are stored with the long parameter name as key. This will
564 # also be used in the href subroutine to convert parameters to their CGI
565 # equivalent, and since the href() usage is the most frequent one, we store
566 # the name -> CGI key mapping here, instead of the reverse.
568 # XXX: Warning: If you touch this, check the search form for updating,
569 # too.
571 our @cgi_param_mapping = (
572 project => "p",
573 action => "a",
574 file_name => "f",
575 file_parent => "fp",
576 hash => "h",
577 hash_parent => "hp",
578 hash_base => "hb",
579 hash_parent_base => "hpb",
580 page => "pg",
581 order => "o",
582 searchtext => "s",
583 searchtype => "st",
584 snapshot_format => "sf",
585 extra_options => "opt",
586 search_use_regexp => "sr",
588 our %cgi_param_mapping = @cgi_param_mapping;
590 # we will also need to know the possible actions, for validation
591 our %actions = (
592 "blame" => \&git_blame,
593 "blame_incremental" => \&git_blame_incremental,
594 "blame_data" => \&git_blame_data,
595 "blobdiff" => \&git_blobdiff,
596 "blobdiff_plain" => \&git_blobdiff_plain,
597 "blob" => \&git_blob,
598 "blob_plain" => \&git_blob_plain,
599 "commitdiff" => \&git_commitdiff,
600 "commitdiff_plain" => \&git_commitdiff_plain,
601 "commit" => \&git_commit,
602 "forks" => \&git_forks,
603 "heads" => \&git_heads,
604 "history" => \&git_history,
605 "log" => \&git_log,
606 "patch" => \&git_patch,
607 "patches" => \&git_patches,
608 "rss" => \&git_rss,
609 "atom" => \&git_atom,
610 "search" => \&git_search,
611 "search_help" => \&git_search_help,
612 "shortlog" => \&git_shortlog,
613 "summary" => \&git_summary,
614 "tag" => \&git_tag,
615 "tags" => \&git_tags,
616 "tree" => \&git_tree,
617 "snapshot" => \&git_snapshot,
618 "object" => \&git_object,
619 # those below don't need $project
620 "opml" => \&git_opml,
621 "project_list" => \&git_project_list,
622 "project_index" => \&git_project_index,
625 # finally, we have the hash of allowed extra_options for the commands that
626 # allow them
627 our %allowed_options = (
628 "--no-merges" => [ qw(rss atom log shortlog history) ],
631 # fill %input_params with the CGI parameters. All values except for 'opt'
632 # should be single values, but opt can be an array. We should probably
633 # build an array of parameters that can be multi-valued, but since for the time
634 # being it's only this one, we just single it out
635 while (my ($name, $symbol) = each %cgi_param_mapping) {
636 if ($symbol eq 'opt') {
637 $input_params{$name} = [ $cgi->param($symbol) ];
638 } else {
639 $input_params{$name} = $cgi->param($symbol);
643 # now read PATH_INFO and update the parameter list for missing parameters
644 sub evaluate_path_info {
645 return if defined $input_params{'project'};
646 return if !$path_info;
647 $path_info =~ s,^/+,,;
648 return if !$path_info;
650 # find which part of PATH_INFO is project
651 my $project = $path_info;
652 $project =~ s,/+$,,;
653 while ($project && !check_head_link("$projectroot/$project")) {
654 $project =~ s,/*[^/]*$,,;
656 return unless $project;
657 $input_params{'project'} = $project;
659 # do not change any parameters if an action is given using the query string
660 return if $input_params{'action'};
661 $path_info =~ s,^\Q$project\E/*,,;
663 # next, check if we have an action
664 my $action = $path_info;
665 $action =~ s,/.*$,,;
666 if (exists $actions{$action}) {
667 $path_info =~ s,^$action/*,,;
668 $input_params{'action'} = $action;
671 # list of actions that want hash_base instead of hash, but can have no
672 # pathname (f) parameter
673 my @wants_base = (
674 'tree',
675 'history',
678 # we want to catch
679 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
680 my ($parentrefname, $parentpathname, $refname, $pathname) =
681 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
683 # first, analyze the 'current' part
684 if (defined $pathname) {
685 # we got "branch:filename" or "branch:dir/"
686 # we could use git_get_type(branch:pathname), but:
687 # - it needs $git_dir
688 # - it does a git() call
689 # - the convention of terminating directories with a slash
690 # makes it superfluous
691 # - embedding the action in the PATH_INFO would make it even
692 # more superfluous
693 $pathname =~ s,^/+,,;
694 if (!$pathname || substr($pathname, -1) eq "/") {
695 $input_params{'action'} ||= "tree";
696 $pathname =~ s,/$,,;
697 } else {
698 # the default action depends on whether we had parent info
699 # or not
700 if ($parentrefname) {
701 $input_params{'action'} ||= "blobdiff_plain";
702 } else {
703 $input_params{'action'} ||= "blob_plain";
706 $input_params{'hash_base'} ||= $refname;
707 $input_params{'file_name'} ||= $pathname;
708 } elsif (defined $refname) {
709 # we got "branch". In this case we have to choose if we have to
710 # set hash or hash_base.
712 # Most of the actions without a pathname only want hash to be
713 # set, except for the ones specified in @wants_base that want
714 # hash_base instead. It should also be noted that hand-crafted
715 # links having 'history' as an action and no pathname or hash
716 # set will fail, but that happens regardless of PATH_INFO.
717 $input_params{'action'} ||= "shortlog";
718 if (grep { $_ eq $input_params{'action'} } @wants_base) {
719 $input_params{'hash_base'} ||= $refname;
720 } else {
721 $input_params{'hash'} ||= $refname;
725 # next, handle the 'parent' part, if present
726 if (defined $parentrefname) {
727 # a missing pathspec defaults to the 'current' filename, allowing e.g.
728 # someproject/blobdiff/oldrev..newrev:/filename
729 if ($parentpathname) {
730 $parentpathname =~ s,^/+,,;
731 $parentpathname =~ s,/$,,;
732 $input_params{'file_parent'} ||= $parentpathname;
733 } else {
734 $input_params{'file_parent'} ||= $input_params{'file_name'};
736 # we assume that hash_parent_base is wanted if a path was specified,
737 # or if the action wants hash_base instead of hash
738 if (defined $input_params{'file_parent'} ||
739 grep { $_ eq $input_params{'action'} } @wants_base) {
740 $input_params{'hash_parent_base'} ||= $parentrefname;
741 } else {
742 $input_params{'hash_parent'} ||= $parentrefname;
746 # for the snapshot action, we allow URLs in the form
747 # $project/snapshot/$hash.ext
748 # where .ext determines the snapshot and gets removed from the
749 # passed $refname to provide the $hash.
751 # To be able to tell that $refname includes the format extension, we
752 # require the following two conditions to be satisfied:
753 # - the hash input parameter MUST have been set from the $refname part
754 # of the URL (i.e. they must be equal)
755 # - the snapshot format MUST NOT have been defined already (e.g. from
756 # CGI parameter sf)
757 # It's also useless to try any matching unless $refname has a dot,
758 # so we check for that too
759 if (defined $input_params{'action'} &&
760 $input_params{'action'} eq 'snapshot' &&
761 defined $refname && index($refname, '.') != -1 &&
762 $refname eq $input_params{'hash'} &&
763 !defined $input_params{'snapshot_format'}) {
764 # We loop over the known snapshot formats, checking for
765 # extensions. Allowed extensions are both the defined suffix
766 # (which includes the initial dot already) and the snapshot
767 # format key itself, with a prepended dot
768 while (my ($fmt, $opt) = each %known_snapshot_formats) {
769 my $hash = $refname;
770 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
771 next;
773 my $sfx = $1;
774 # a valid suffix was found, so set the snapshot format
775 # and reset the hash parameter
776 $input_params{'snapshot_format'} = $fmt;
777 $input_params{'hash'} = $hash;
778 # we also set the format suffix to the one requested
779 # in the URL: this way a request for e.g. .tgz returns
780 # a .tgz instead of a .tar.gz
781 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
782 last;
786 evaluate_path_info();
788 our $action = $input_params{'action'};
789 if (defined $action) {
790 if (!validate_action($action)) {
791 die_error(400, "Invalid action parameter");
795 # parameters which are pathnames
796 our $project = $input_params{'project'};
797 if (defined $project) {
798 if (!validate_project($project)) {
799 undef $project;
800 die_error(404, "No such project");
804 our $file_name = $input_params{'file_name'};
805 if (defined $file_name) {
806 if (!validate_pathname($file_name)) {
807 die_error(400, "Invalid file parameter");
811 our $file_parent = $input_params{'file_parent'};
812 if (defined $file_parent) {
813 if (!validate_pathname($file_parent)) {
814 die_error(400, "Invalid file parent parameter");
818 # parameters which are refnames
819 our $hash = $input_params{'hash'};
820 if (defined $hash) {
821 if (!validate_refname($hash)) {
822 die_error(400, "Invalid hash parameter");
826 our $hash_parent = $input_params{'hash_parent'};
827 if (defined $hash_parent) {
828 if (!validate_refname($hash_parent)) {
829 die_error(400, "Invalid hash parent parameter");
833 our $hash_base = $input_params{'hash_base'};
834 if (defined $hash_base) {
835 if (!validate_refname($hash_base)) {
836 die_error(400, "Invalid hash base parameter");
840 our @extra_options = @{$input_params{'extra_options'}};
841 # @extra_options is always defined, since it can only be (currently) set from
842 # CGI, and $cgi->param() returns the empty array in array context if the param
843 # is not set
844 foreach my $opt (@extra_options) {
845 if (not exists $allowed_options{$opt}) {
846 die_error(400, "Invalid option parameter");
848 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
849 die_error(400, "Invalid option parameter for this action");
853 our $hash_parent_base = $input_params{'hash_parent_base'};
854 if (defined $hash_parent_base) {
855 if (!validate_refname($hash_parent_base)) {
856 die_error(400, "Invalid hash parent base parameter");
860 # other parameters
861 our $page = $input_params{'page'};
862 if (defined $page) {
863 if ($page =~ m/[^0-9]/) {
864 die_error(400, "Invalid page parameter");
868 our $searchtype = $input_params{'searchtype'};
869 if (defined $searchtype) {
870 if ($searchtype =~ m/[^a-z]/) {
871 die_error(400, "Invalid searchtype parameter");
875 our $search_use_regexp = $input_params{'search_use_regexp'};
877 our $searchtext = $input_params{'searchtext'};
878 our $search_regexp;
879 if (defined $searchtext) {
880 if (length($searchtext) < 2) {
881 die_error(403, "At least two characters are required for search parameter");
883 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
886 # path to the current git repository
887 our $git_dir;
888 $git_dir = "$projectroot/$project" if $project;
890 # list of supported snapshot formats
891 our @snapshot_fmts = gitweb_get_feature('snapshot');
892 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
894 # check that the avatar feature is set to a known provider name,
895 # and for each provider check if the dependencies are satisfied.
896 # if the provider name is invalid or the dependencies are not met,
897 # reset $git_avatar to the empty string.
898 our ($git_avatar) = gitweb_get_feature('avatar');
899 if ($git_avatar eq 'gravatar') {
900 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
901 } elsif ($git_avatar eq 'picon') {
902 # no dependencies
903 } else {
904 $git_avatar = '';
907 # dispatch
908 if (!defined $action) {
909 if (defined $hash) {
910 $action = git_get_type($hash);
911 } elsif (defined $hash_base && defined $file_name) {
912 $action = git_get_type("$hash_base:$file_name");
913 } elsif (defined $project) {
914 $action = 'summary';
915 } else {
916 $action = 'project_list';
919 if (!defined($actions{$action})) {
920 die_error(400, "Unknown action");
922 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
923 !$project) {
924 die_error(400, "Project needed");
926 $actions{$action}->();
927 exit;
929 ## ======================================================================
930 ## action links
932 sub href {
933 my %params = @_;
934 # default is to use -absolute url() i.e. $my_uri
935 my $href = $params{-full} ? $my_url : $my_uri;
937 $params{'project'} = $project unless exists $params{'project'};
939 if ($params{-replay}) {
940 while (my ($name, $symbol) = each %cgi_param_mapping) {
941 if (!exists $params{$name}) {
942 $params{$name} = $input_params{$name};
947 my $use_pathinfo = gitweb_check_feature('pathinfo');
948 if ($use_pathinfo and defined $params{'project'}) {
949 # try to put as many parameters as possible in PATH_INFO:
950 # - project name
951 # - action
952 # - hash_parent or hash_parent_base:/file_parent
953 # - hash or hash_base:/filename
954 # - the snapshot_format as an appropriate suffix
956 # When the script is the root DirectoryIndex for the domain,
957 # $href here would be something like http://gitweb.example.com/
958 # Thus, we strip any trailing / from $href, to spare us double
959 # slashes in the final URL
960 $href =~ s,/$,,;
962 # Then add the project name, if present
963 $href .= "/".esc_url($params{'project'});
964 delete $params{'project'};
966 # since we destructively absorb parameters, we keep this
967 # boolean that remembers if we're handling a snapshot
968 my $is_snapshot = $params{'action'} eq 'snapshot';
970 # Summary just uses the project path URL, any other action is
971 # added to the URL
972 if (defined $params{'action'}) {
973 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
974 delete $params{'action'};
977 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
978 # stripping nonexistent or useless pieces
979 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
980 || $params{'hash_parent'} || $params{'hash'});
981 if (defined $params{'hash_base'}) {
982 if (defined $params{'hash_parent_base'}) {
983 $href .= esc_url($params{'hash_parent_base'});
984 # skip the file_parent if it's the same as the file_name
985 if (defined $params{'file_parent'}) {
986 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
987 delete $params{'file_parent'};
988 } elsif ($params{'file_parent'} !~ /\.\./) {
989 $href .= ":/".esc_url($params{'file_parent'});
990 delete $params{'file_parent'};
993 $href .= "..";
994 delete $params{'hash_parent'};
995 delete $params{'hash_parent_base'};
996 } elsif (defined $params{'hash_parent'}) {
997 $href .= esc_url($params{'hash_parent'}). "..";
998 delete $params{'hash_parent'};
1001 $href .= esc_url($params{'hash_base'});
1002 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1003 $href .= ":/".esc_url($params{'file_name'});
1004 delete $params{'file_name'};
1006 delete $params{'hash'};
1007 delete $params{'hash_base'};
1008 } elsif (defined $params{'hash'}) {
1009 $href .= esc_url($params{'hash'});
1010 delete $params{'hash'};
1013 # If the action was a snapshot, we can absorb the
1014 # snapshot_format parameter too
1015 if ($is_snapshot) {
1016 my $fmt = $params{'snapshot_format'};
1017 # snapshot_format should always be defined when href()
1018 # is called, but just in case some code forgets, we
1019 # fall back to the default
1020 $fmt ||= $snapshot_fmts[0];
1021 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1022 delete $params{'snapshot_format'};
1026 # now encode the parameters explicitly
1027 my @result = ();
1028 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1029 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1030 if (defined $params{$name}) {
1031 if (ref($params{$name}) eq "ARRAY") {
1032 foreach my $par (@{$params{$name}}) {
1033 push @result, $symbol . "=" . esc_param($par);
1035 } else {
1036 push @result, $symbol . "=" . esc_param($params{$name});
1040 $href .= "?" . join(';', @result) if $params{-partial_query} or scalar @result;
1042 return $href;
1046 ## ======================================================================
1047 ## validation, quoting/unquoting and escaping
1049 sub validate_action {
1050 my $input = shift || return undef;
1051 return undef unless exists $actions{$input};
1052 return $input;
1055 sub validate_project {
1056 my $input = shift || return undef;
1057 if (!validate_pathname($input) ||
1058 !(-d "$projectroot/$input") ||
1059 !check_export_ok("$projectroot/$input") ||
1060 ($strict_export && !project_in_list($input))) {
1061 return undef;
1062 } else {
1063 return $input;
1067 sub validate_pathname {
1068 my $input = shift || return undef;
1070 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1071 # at the beginning, at the end, and between slashes.
1072 # also this catches doubled slashes
1073 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1074 return undef;
1076 # no null characters
1077 if ($input =~ m!\0!) {
1078 return undef;
1080 return $input;
1083 sub validate_refname {
1084 my $input = shift || return undef;
1086 # textual hashes are O.K.
1087 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1088 return $input;
1090 # it must be correct pathname
1091 $input = validate_pathname($input)
1092 or return undef;
1093 # restrictions on ref name according to git-check-ref-format
1094 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1095 return undef;
1097 return $input;
1100 # decode sequences of octets in utf8 into Perl's internal form,
1101 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1102 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1103 sub to_utf8 {
1104 my $str = shift;
1105 if (utf8::valid($str)) {
1106 utf8::decode($str);
1107 return $str;
1108 } else {
1109 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1113 # quote unsafe chars, but keep the slash, even when it's not
1114 # correct, but quoted slashes look too horrible in bookmarks
1115 sub esc_param {
1116 my $str = shift;
1117 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1118 $str =~ s/ /\+/g;
1119 return $str;
1122 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
1123 sub esc_url {
1124 my $str = shift;
1125 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1126 $str =~ s/\+/%2B/g;
1127 $str =~ s/ /\+/g;
1128 return $str;
1131 # replace invalid utf8 character with SUBSTITUTION sequence
1132 sub esc_html {
1133 my $str = shift;
1134 my %opts = @_;
1136 $str = to_utf8($str);
1137 $str = $cgi->escapeHTML($str);
1138 if ($opts{'-nbsp'}) {
1139 $str =~ s/ /&nbsp;/g;
1141 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1142 return $str;
1145 # quote control characters and escape filename to HTML
1146 sub esc_path {
1147 my $str = shift;
1148 my %opts = @_;
1150 $str = to_utf8($str);
1151 $str = $cgi->escapeHTML($str);
1152 if ($opts{'-nbsp'}) {
1153 $str =~ s/ /&nbsp;/g;
1155 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1156 return $str;
1159 # Make control characters "printable", using character escape codes (CEC)
1160 sub quot_cec {
1161 my $cntrl = shift;
1162 my %opts = @_;
1163 my %es = ( # character escape codes, aka escape sequences
1164 "\t" => '\t', # tab (HT)
1165 "\n" => '\n', # line feed (LF)
1166 "\r" => '\r', # carrige return (CR)
1167 "\f" => '\f', # form feed (FF)
1168 "\b" => '\b', # backspace (BS)
1169 "\a" => '\a', # alarm (bell) (BEL)
1170 "\e" => '\e', # escape (ESC)
1171 "\013" => '\v', # vertical tab (VT)
1172 "\000" => '\0', # nul character (NUL)
1174 my $chr = ( (exists $es{$cntrl})
1175 ? $es{$cntrl}
1176 : sprintf('\%2x', ord($cntrl)) );
1177 if ($opts{-nohtml}) {
1178 return $chr;
1179 } else {
1180 return "<span class=\"cntrl\">$chr</span>";
1184 # Alternatively use unicode control pictures codepoints,
1185 # Unicode "printable representation" (PR)
1186 sub quot_upr {
1187 my $cntrl = shift;
1188 my %opts = @_;
1190 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1191 if ($opts{-nohtml}) {
1192 return $chr;
1193 } else {
1194 return "<span class=\"cntrl\">$chr</span>";
1198 # git may return quoted and escaped filenames
1199 sub unquote {
1200 my $str = shift;
1202 sub unq {
1203 my $seq = shift;
1204 my %es = ( # character escape codes, aka escape sequences
1205 't' => "\t", # tab (HT, TAB)
1206 'n' => "\n", # newline (NL)
1207 'r' => "\r", # return (CR)
1208 'f' => "\f", # form feed (FF)
1209 'b' => "\b", # backspace (BS)
1210 'a' => "\a", # alarm (bell) (BEL)
1211 'e' => "\e", # escape (ESC)
1212 'v' => "\013", # vertical tab (VT)
1215 if ($seq =~ m/^[0-7]{1,3}$/) {
1216 # octal char sequence
1217 return chr(oct($seq));
1218 } elsif (exists $es{$seq}) {
1219 # C escape sequence, aka character escape code
1220 return $es{$seq};
1222 # quoted ordinary character
1223 return $seq;
1226 if ($str =~ m/^"(.*)"$/) {
1227 # needs unquoting
1228 $str = $1;
1229 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1231 return $str;
1234 # escape tabs (convert tabs to spaces)
1235 sub untabify {
1236 my $line = shift;
1238 while ((my $pos = index($line, "\t")) != -1) {
1239 if (my $count = (8 - ($pos % 8))) {
1240 my $spaces = ' ' x $count;
1241 $line =~ s/\t/$spaces/;
1245 return $line;
1248 sub project_in_list {
1249 my $project = shift;
1250 my @list = git_get_projects_list();
1251 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1254 ## ----------------------------------------------------------------------
1255 ## HTML aware string manipulation
1257 # Try to chop given string on a word boundary between position
1258 # $len and $len+$add_len. If there is no word boundary there,
1259 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1260 # (marking chopped part) would be longer than given string.
1261 sub chop_str {
1262 my $str = shift;
1263 my $len = shift;
1264 my $add_len = shift || 10;
1265 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1267 # Make sure perl knows it is utf8 encoded so we don't
1268 # cut in the middle of a utf8 multibyte char.
1269 $str = to_utf8($str);
1271 # allow only $len chars, but don't cut a word if it would fit in $add_len
1272 # if it doesn't fit, cut it if it's still longer than the dots we would add
1273 # remove chopped character entities entirely
1275 # when chopping in the middle, distribute $len into left and right part
1276 # return early if chopping wouldn't make string shorter
1277 if ($where eq 'center') {
1278 return $str if ($len + 5 >= length($str)); # filler is length 5
1279 $len = int($len/2);
1280 } else {
1281 return $str if ($len + 4 >= length($str)); # filler is length 4
1284 # regexps: ending and beginning with word part up to $add_len
1285 my $endre = qr/.{$len}\w{0,$add_len}/;
1286 my $begre = qr/\w{0,$add_len}.{$len}/;
1288 if ($where eq 'left') {
1289 $str =~ m/^(.*?)($begre)$/;
1290 my ($lead, $body) = ($1, $2);
1291 if (length($lead) > 4) {
1292 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1293 $lead = " ...";
1295 return "$lead$body";
1297 } elsif ($where eq 'center') {
1298 $str =~ m/^($endre)(.*)$/;
1299 my ($left, $str) = ($1, $2);
1300 $str =~ m/^(.*?)($begre)$/;
1301 my ($mid, $right) = ($1, $2);
1302 if (length($mid) > 5) {
1303 $left =~ s/&[^;]*$//;
1304 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1305 $mid = " ... ";
1307 return "$left$mid$right";
1309 } else {
1310 $str =~ m/^($endre)(.*)$/;
1311 my $body = $1;
1312 my $tail = $2;
1313 if (length($tail) > 4) {
1314 $body =~ s/&[^;]*$//;
1315 $tail = "... ";
1317 return "$body$tail";
1321 # takes the same arguments as chop_str, but also wraps a <span> around the
1322 # result with a title attribute if it does get chopped. Additionally, the
1323 # string is HTML-escaped.
1324 sub chop_and_escape_str {
1325 my ($str) = @_;
1327 my $chopped = chop_str(@_);
1328 if ($chopped eq $str) {
1329 return esc_html($chopped);
1330 } else {
1331 $str =~ s/[[:cntrl:]]/?/g;
1332 return $cgi->span({-title=>$str}, esc_html($chopped));
1336 ## ----------------------------------------------------------------------
1337 ## functions returning short strings
1339 # CSS class for given age value (in seconds)
1340 sub age_class {
1341 my $age = shift;
1343 if (!defined $age) {
1344 return "noage";
1345 } elsif ($age < 60*60*2) {
1346 return "age0";
1347 } elsif ($age < 60*60*24*2) {
1348 return "age1";
1349 } else {
1350 return "age2";
1354 # convert age in seconds to "nn units ago" string
1355 sub age_string {
1356 my $age = shift;
1357 my $age_str;
1359 if ($age > 60*60*24*365*2) {
1360 $age_str = (int $age/60/60/24/365);
1361 $age_str .= " years ago";
1362 } elsif ($age > 60*60*24*(365/12)*2) {
1363 $age_str = int $age/60/60/24/(365/12);
1364 $age_str .= " months ago";
1365 } elsif ($age > 60*60*24*7*2) {
1366 $age_str = int $age/60/60/24/7;
1367 $age_str .= " weeks ago";
1368 } elsif ($age > 60*60*24*2) {
1369 $age_str = int $age/60/60/24;
1370 $age_str .= " days ago";
1371 } elsif ($age > 60*60*2) {
1372 $age_str = int $age/60/60;
1373 $age_str .= " hours ago";
1374 } elsif ($age > 60*2) {
1375 $age_str = int $age/60;
1376 $age_str .= " min ago";
1377 } elsif ($age > 2) {
1378 $age_str = int $age;
1379 $age_str .= " sec ago";
1380 } else {
1381 $age_str .= " right now";
1383 return $age_str;
1386 use constant {
1387 S_IFINVALID => 0030000,
1388 S_IFGITLINK => 0160000,
1391 # submodule/subproject, a commit object reference
1392 sub S_ISGITLINK {
1393 my $mode = shift;
1395 return (($mode & S_IFMT) == S_IFGITLINK)
1398 # convert file mode in octal to symbolic file mode string
1399 sub mode_str {
1400 my $mode = oct shift;
1402 if (S_ISGITLINK($mode)) {
1403 return 'm---------';
1404 } elsif (S_ISDIR($mode & S_IFMT)) {
1405 return 'drwxr-xr-x';
1406 } elsif (S_ISLNK($mode)) {
1407 return 'lrwxrwxrwx';
1408 } elsif (S_ISREG($mode)) {
1409 # git cares only about the executable bit
1410 if ($mode & S_IXUSR) {
1411 return '-rwxr-xr-x';
1412 } else {
1413 return '-rw-r--r--';
1415 } else {
1416 return '----------';
1420 # convert file mode in octal to file type string
1421 sub file_type {
1422 my $mode = shift;
1424 if ($mode !~ m/^[0-7]+$/) {
1425 return $mode;
1426 } else {
1427 $mode = oct $mode;
1430 if (S_ISGITLINK($mode)) {
1431 return "submodule";
1432 } elsif (S_ISDIR($mode & S_IFMT)) {
1433 return "directory";
1434 } elsif (S_ISLNK($mode)) {
1435 return "symlink";
1436 } elsif (S_ISREG($mode)) {
1437 return "file";
1438 } else {
1439 return "unknown";
1443 # convert file mode in octal to file type description string
1444 sub file_type_long {
1445 my $mode = shift;
1447 if ($mode !~ m/^[0-7]+$/) {
1448 return $mode;
1449 } else {
1450 $mode = oct $mode;
1453 if (S_ISGITLINK($mode)) {
1454 return "submodule";
1455 } elsif (S_ISDIR($mode & S_IFMT)) {
1456 return "directory";
1457 } elsif (S_ISLNK($mode)) {
1458 return "symlink";
1459 } elsif (S_ISREG($mode)) {
1460 if ($mode & S_IXUSR) {
1461 return "executable";
1462 } else {
1463 return "file";
1465 } else {
1466 return "unknown";
1471 ## ----------------------------------------------------------------------
1472 ## functions returning short HTML fragments, or transforming HTML fragments
1473 ## which don't belong to other sections
1475 # format line of commit message.
1476 sub format_log_line_html {
1477 my $line = shift;
1479 $line = esc_html($line, -nbsp=>1);
1480 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1481 $cgi->a({-href => href(action=>"object", hash=>$1),
1482 -class => "text"}, $1);
1483 }eg;
1485 return $line;
1488 # format marker of refs pointing to given object
1490 # the destination action is chosen based on object type and current context:
1491 # - for annotated tags, we choose the tag view unless it's the current view
1492 # already, in which case we go to shortlog view
1493 # - for other refs, we keep the current view if we're in history, shortlog or
1494 # log view, and select shortlog otherwise
1495 sub format_ref_marker {
1496 my ($refs, $id) = @_;
1497 my $markers = '';
1499 if (defined $refs->{$id}) {
1500 foreach my $ref (@{$refs->{$id}}) {
1501 # this code exploits the fact that non-lightweight tags are the
1502 # only indirect objects, and that they are the only objects for which
1503 # we want to use tag instead of shortlog as action
1504 my ($type, $name) = qw();
1505 my $indirect = ($ref =~ s/\^\{\}$//);
1506 # e.g. tags/v2.6.11 or heads/next
1507 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1508 $type = $1;
1509 $name = $2;
1510 } else {
1511 $type = "ref";
1512 $name = $ref;
1515 my $class = $type;
1516 $class .= " indirect" if $indirect;
1518 my $dest_action = "shortlog";
1520 if ($indirect) {
1521 $dest_action = "tag" unless $action eq "tag";
1522 } elsif ($action =~ /^(history|(short)?log)$/) {
1523 $dest_action = $action;
1526 my $dest = "";
1527 $dest .= "refs/" unless $ref =~ m!^refs/!;
1528 $dest .= $ref;
1530 my $link = $cgi->a({
1531 -href => href(
1532 action=>$dest_action,
1533 hash=>$dest
1534 )}, $name);
1536 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1537 $link . "</span>";
1541 if ($markers) {
1542 return ' <span class="refs">'. $markers . '</span>';
1543 } else {
1544 return "";
1548 # format, perhaps shortened and with markers, title line
1549 sub format_subject_html {
1550 my ($long, $short, $href, $extra) = @_;
1551 $extra = '' unless defined($extra);
1553 if (length($short) < length($long)) {
1554 $long =~ s/[[:cntrl:]]/?/g;
1555 return $cgi->a({-href => $href, -class => "list subject",
1556 -title => to_utf8($long)},
1557 esc_html($short)) . $extra;
1558 } else {
1559 return $cgi->a({-href => $href, -class => "list subject"},
1560 esc_html($long)) . $extra;
1564 # Rather than recomputing the url for an email multiple times, we cache it
1565 # after the first hit. This gives a visible benefit in views where the avatar
1566 # for the same email is used repeatedly (e.g. shortlog).
1567 # The cache is shared by all avatar engines (currently gravatar only), which
1568 # are free to use it as preferred. Since only one avatar engine is used for any
1569 # given page, there's no risk for cache conflicts.
1570 our %avatar_cache = ();
1572 # Compute the picon url for a given email, by using the picon search service over at
1573 # http://www.cs.indiana.edu/picons/search.html
1574 sub picon_url {
1575 my $email = lc shift;
1576 if (!$avatar_cache{$email}) {
1577 my ($user, $domain) = split('@', $email);
1578 $avatar_cache{$email} =
1579 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1580 "$domain/$user/" .
1581 "users+domains+unknown/up/single";
1583 return $avatar_cache{$email};
1586 # Compute the gravatar url for a given email, if it's not in the cache already.
1587 # Gravatar stores only the part of the URL before the size, since that's the
1588 # one computationally more expensive. This also allows reuse of the cache for
1589 # different sizes (for this particular engine).
1590 sub gravatar_url {
1591 my $email = lc shift;
1592 my $size = shift;
1593 $avatar_cache{$email} ||=
1594 "http://www.gravatar.com/avatar/" .
1595 Digest::MD5::md5_hex($email) . "?s=";
1596 return $avatar_cache{$email} . $size;
1599 # Insert an avatar for the given $email at the given $size if the feature
1600 # is enabled.
1601 sub git_get_avatar {
1602 my ($email, %opts) = @_;
1603 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1604 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1605 $opts{-size} ||= 'default';
1606 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1607 my $url = "";
1608 if ($git_avatar eq 'gravatar') {
1609 $url = gravatar_url($email, $size);
1610 } elsif ($git_avatar eq 'picon') {
1611 $url = picon_url($email);
1613 # Other providers can be added by extending the if chain, defining $url
1614 # as needed. If no variant puts something in $url, we assume avatars
1615 # are completely disabled/unavailable.
1616 if ($url) {
1617 return $pre_white .
1618 "<img width=\"$size\" " .
1619 "class=\"avatar\" " .
1620 "src=\"$url\" " .
1621 "alt=\"\" " .
1622 "/>" . $post_white;
1623 } else {
1624 return "";
1628 sub format_search_author {
1629 my ($author, $searchtype, $displaytext) = @_;
1630 my $have_search = gitweb_check_feature('search');
1632 if ($have_search) {
1633 my $performed = "";
1634 if ($searchtype eq 'author') {
1635 $performed = "authored";
1636 } elsif ($searchtype eq 'committer') {
1637 $performed = "committed";
1640 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1641 searchtext=>$author,
1642 searchtype=>$searchtype), class=>"list",
1643 title=>"Search for commits $performed by $author"},
1644 $displaytext);
1646 } else {
1647 return $displaytext;
1651 # format the author name of the given commit with the given tag
1652 # the author name is chopped and escaped according to the other
1653 # optional parameters (see chop_str).
1654 sub format_author_html {
1655 my $tag = shift;
1656 my $co = shift;
1657 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1658 return "<$tag class=\"author\">" .
1659 format_search_author($co->{'author_name'}, "author",
1660 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1661 $author) .
1662 "</$tag>";
1665 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1666 sub format_git_diff_header_line {
1667 my $line = shift;
1668 my $diffinfo = shift;
1669 my ($from, $to) = @_;
1671 if ($diffinfo->{'nparents'}) {
1672 # combined diff
1673 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1674 if ($to->{'href'}) {
1675 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1676 esc_path($to->{'file'}));
1677 } else { # file was deleted (no href)
1678 $line .= esc_path($to->{'file'});
1680 } else {
1681 # "ordinary" diff
1682 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1683 if ($from->{'href'}) {
1684 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1685 'a/' . esc_path($from->{'file'}));
1686 } else { # file was added (no href)
1687 $line .= 'a/' . esc_path($from->{'file'});
1689 $line .= ' ';
1690 if ($to->{'href'}) {
1691 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1692 'b/' . esc_path($to->{'file'}));
1693 } else { # file was deleted
1694 $line .= 'b/' . esc_path($to->{'file'});
1698 return "<div class=\"diff header\">$line</div>\n";
1701 # format extended diff header line, before patch itself
1702 sub format_extended_diff_header_line {
1703 my $line = shift;
1704 my $diffinfo = shift;
1705 my ($from, $to) = @_;
1707 # match <path>
1708 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1709 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1710 esc_path($from->{'file'}));
1712 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1713 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1714 esc_path($to->{'file'}));
1716 # match single <mode>
1717 if ($line =~ m/\s(\d{6})$/) {
1718 $line .= '<span class="info"> (' .
1719 file_type_long($1) .
1720 ')</span>';
1722 # match <hash>
1723 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1724 # can match only for combined diff
1725 $line = 'index ';
1726 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1727 if ($from->{'href'}[$i]) {
1728 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1729 -class=>"hash"},
1730 substr($diffinfo->{'from_id'}[$i],0,7));
1731 } else {
1732 $line .= '0' x 7;
1734 # separator
1735 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1737 $line .= '..';
1738 if ($to->{'href'}) {
1739 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1740 substr($diffinfo->{'to_id'},0,7));
1741 } else {
1742 $line .= '0' x 7;
1745 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1746 # can match only for ordinary diff
1747 my ($from_link, $to_link);
1748 if ($from->{'href'}) {
1749 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1750 substr($diffinfo->{'from_id'},0,7));
1751 } else {
1752 $from_link = '0' x 7;
1754 if ($to->{'href'}) {
1755 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1756 substr($diffinfo->{'to_id'},0,7));
1757 } else {
1758 $to_link = '0' x 7;
1760 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1761 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1764 return $line . "<br/>\n";
1767 # format from-file/to-file diff header
1768 sub format_diff_from_to_header {
1769 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1770 my $line;
1771 my $result = '';
1773 $line = $from_line;
1774 #assert($line =~ m/^---/) if DEBUG;
1775 # no extra formatting for "^--- /dev/null"
1776 if (! $diffinfo->{'nparents'}) {
1777 # ordinary (single parent) diff
1778 if ($line =~ m!^--- "?a/!) {
1779 if ($from->{'href'}) {
1780 $line = '--- a/' .
1781 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1782 esc_path($from->{'file'}));
1783 } else {
1784 $line = '--- a/' .
1785 esc_path($from->{'file'});
1788 $result .= qq!<div class="diff from_file">$line</div>\n!;
1790 } else {
1791 # combined diff (merge commit)
1792 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1793 if ($from->{'href'}[$i]) {
1794 $line = '--- ' .
1795 $cgi->a({-href=>href(action=>"blobdiff",
1796 hash_parent=>$diffinfo->{'from_id'}[$i],
1797 hash_parent_base=>$parents[$i],
1798 file_parent=>$from->{'file'}[$i],
1799 hash=>$diffinfo->{'to_id'},
1800 hash_base=>$hash,
1801 file_name=>$to->{'file'}),
1802 -class=>"path",
1803 -title=>"diff" . ($i+1)},
1804 $i+1) .
1805 '/' .
1806 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1807 esc_path($from->{'file'}[$i]));
1808 } else {
1809 $line = '--- /dev/null';
1811 $result .= qq!<div class="diff from_file">$line</div>\n!;
1815 $line = $to_line;
1816 #assert($line =~ m/^\+\+\+/) if DEBUG;
1817 # no extra formatting for "^+++ /dev/null"
1818 if ($line =~ m!^\+\+\+ "?b/!) {
1819 if ($to->{'href'}) {
1820 $line = '+++ b/' .
1821 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1822 esc_path($to->{'file'}));
1823 } else {
1824 $line = '+++ b/' .
1825 esc_path($to->{'file'});
1828 $result .= qq!<div class="diff to_file">$line</div>\n!;
1830 return $result;
1833 # create note for patch simplified by combined diff
1834 sub format_diff_cc_simplified {
1835 my ($diffinfo, @parents) = @_;
1836 my $result = '';
1838 $result .= "<div class=\"diff header\">" .
1839 "diff --cc ";
1840 if (!is_deleted($diffinfo)) {
1841 $result .= $cgi->a({-href => href(action=>"blob",
1842 hash_base=>$hash,
1843 hash=>$diffinfo->{'to_id'},
1844 file_name=>$diffinfo->{'to_file'}),
1845 -class => "path"},
1846 esc_path($diffinfo->{'to_file'}));
1847 } else {
1848 $result .= esc_path($diffinfo->{'to_file'});
1850 $result .= "</div>\n" . # class="diff header"
1851 "<div class=\"diff nodifferences\">" .
1852 "Simple merge" .
1853 "</div>\n"; # class="diff nodifferences"
1855 return $result;
1858 # format patch (diff) line (not to be used for diff headers)
1859 sub format_diff_line {
1860 my $line = shift;
1861 my ($from, $to) = @_;
1862 my $diff_class = "";
1864 chomp $line;
1866 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1867 # combined diff
1868 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1869 if ($line =~ m/^\@{3}/) {
1870 $diff_class = " chunk_header";
1871 } elsif ($line =~ m/^\\/) {
1872 $diff_class = " incomplete";
1873 } elsif ($prefix =~ tr/+/+/) {
1874 $diff_class = " add";
1875 } elsif ($prefix =~ tr/-/-/) {
1876 $diff_class = " rem";
1878 } else {
1879 # assume ordinary diff
1880 my $char = substr($line, 0, 1);
1881 if ($char eq '+') {
1882 $diff_class = " add";
1883 } elsif ($char eq '-') {
1884 $diff_class = " rem";
1885 } elsif ($char eq '@') {
1886 $diff_class = " chunk_header";
1887 } elsif ($char eq "\\") {
1888 $diff_class = " incomplete";
1891 $line = untabify($line);
1892 if ($from && $to && $line =~ m/^\@{2} /) {
1893 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1894 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1896 $from_lines = 0 unless defined $from_lines;
1897 $to_lines = 0 unless defined $to_lines;
1899 if ($from->{'href'}) {
1900 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1901 -class=>"list"}, $from_text);
1903 if ($to->{'href'}) {
1904 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1905 -class=>"list"}, $to_text);
1907 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1908 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1909 return "<div class=\"diff$diff_class\">$line</div>\n";
1910 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1911 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1912 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1914 @from_text = split(' ', $ranges);
1915 for (my $i = 0; $i < @from_text; ++$i) {
1916 ($from_start[$i], $from_nlines[$i]) =
1917 (split(',', substr($from_text[$i], 1)), 0);
1920 $to_text = pop @from_text;
1921 $to_start = pop @from_start;
1922 $to_nlines = pop @from_nlines;
1924 $line = "<span class=\"chunk_info\">$prefix ";
1925 for (my $i = 0; $i < @from_text; ++$i) {
1926 if ($from->{'href'}[$i]) {
1927 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1928 -class=>"list"}, $from_text[$i]);
1929 } else {
1930 $line .= $from_text[$i];
1932 $line .= " ";
1934 if ($to->{'href'}) {
1935 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1936 -class=>"list"}, $to_text);
1937 } else {
1938 $line .= $to_text;
1940 $line .= " $prefix</span>" .
1941 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1942 return "<div class=\"diff$diff_class\">$line</div>\n";
1944 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1947 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1948 # linked. Pass the hash of the tree/commit to snapshot.
1949 sub format_snapshot_links {
1950 my ($hash) = @_;
1951 my $num_fmts = @snapshot_fmts;
1952 if ($num_fmts > 1) {
1953 # A parenthesized list of links bearing format names.
1954 # e.g. "snapshot (_tar.gz_ _zip_)"
1955 return "snapshot (" . join(' ', map
1956 $cgi->a({
1957 -href => href(
1958 action=>"snapshot",
1959 hash=>$hash,
1960 snapshot_format=>$_
1962 }, $known_snapshot_formats{$_}{'display'})
1963 , @snapshot_fmts) . ")";
1964 } elsif ($num_fmts == 1) {
1965 # A single "snapshot" link whose tooltip bears the format name.
1966 # i.e. "_snapshot_"
1967 my ($fmt) = @snapshot_fmts;
1968 return
1969 $cgi->a({
1970 -href => href(
1971 action=>"snapshot",
1972 hash=>$hash,
1973 snapshot_format=>$fmt
1975 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1976 }, "snapshot");
1977 } else { # $num_fmts == 0
1978 return undef;
1982 ## ......................................................................
1983 ## functions returning values to be passed, perhaps after some
1984 ## transformation, to other functions; e.g. returning arguments to href()
1986 # returns hash to be passed to href to generate gitweb URL
1987 # in -title key it returns description of link
1988 sub get_feed_info {
1989 my $format = shift || 'Atom';
1990 my %res = (action => lc($format));
1992 # feed links are possible only for project views
1993 return unless (defined $project);
1994 # some views should link to OPML, or to generic project feed,
1995 # or don't have specific feed yet (so they should use generic)
1996 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1998 my $branch;
1999 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2000 # from tag links; this also makes possible to detect branch links
2001 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2002 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2003 $branch = $1;
2005 # find log type for feed description (title)
2006 my $type = 'log';
2007 if (defined $file_name) {
2008 $type = "history of $file_name";
2009 $type .= "/" if ($action eq 'tree');
2010 $type .= " on '$branch'" if (defined $branch);
2011 } else {
2012 $type = "log of $branch" if (defined $branch);
2015 $res{-title} = $type;
2016 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2017 $res{'file_name'} = $file_name;
2019 return %res;
2022 ## ----------------------------------------------------------------------
2023 ## git utility subroutines, invoking git commands
2025 # returns path to the core git executable and the --git-dir parameter as list
2026 sub git_cmd {
2027 return $GIT, '--git-dir='.$git_dir;
2030 # quote the given arguments for passing them to the shell
2031 # quote_command("command", "arg 1", "arg with ' and ! characters")
2032 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2033 # Try to avoid using this function wherever possible.
2034 sub quote_command {
2035 return join(' ',
2036 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2039 # get HEAD ref of given project as hash
2040 sub git_get_head_hash {
2041 my $project = shift;
2042 my $o_git_dir = $git_dir;
2043 my $retval = undef;
2044 $git_dir = "$projectroot/$project";
2045 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
2046 my $head = <$fd>;
2047 close $fd;
2048 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
2049 $retval = $1;
2052 if (defined $o_git_dir) {
2053 $git_dir = $o_git_dir;
2055 return $retval;
2058 # get type of given object
2059 sub git_get_type {
2060 my $hash = shift;
2062 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2063 my $type = <$fd>;
2064 close $fd or return;
2065 chomp $type;
2066 return $type;
2069 # repository configuration
2070 our $config_file = '';
2071 our %config;
2073 # store multiple values for single key as anonymous array reference
2074 # single values stored directly in the hash, not as [ <value> ]
2075 sub hash_set_multi {
2076 my ($hash, $key, $value) = @_;
2078 if (!exists $hash->{$key}) {
2079 $hash->{$key} = $value;
2080 } elsif (!ref $hash->{$key}) {
2081 $hash->{$key} = [ $hash->{$key}, $value ];
2082 } else {
2083 push @{$hash->{$key}}, $value;
2087 # return hash of git project configuration
2088 # optionally limited to some section, e.g. 'gitweb'
2089 sub git_parse_project_config {
2090 my $section_regexp = shift;
2091 my %config;
2093 local $/ = "\0";
2095 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2096 or return;
2098 while (my $keyval = <$fh>) {
2099 chomp $keyval;
2100 my ($key, $value) = split(/\n/, $keyval, 2);
2102 hash_set_multi(\%config, $key, $value)
2103 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2105 close $fh;
2107 return %config;
2110 # convert config value to boolean: 'true' or 'false'
2111 # no value, number > 0, 'true' and 'yes' values are true
2112 # rest of values are treated as false (never as error)
2113 sub config_to_bool {
2114 my $val = shift;
2116 return 1 if !defined $val; # section.key
2118 # strip leading and trailing whitespace
2119 $val =~ s/^\s+//;
2120 $val =~ s/\s+$//;
2122 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2123 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2126 # convert config value to simple decimal number
2127 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2128 # to be multiplied by 1024, 1048576, or 1073741824
2129 sub config_to_int {
2130 my $val = shift;
2132 # strip leading and trailing whitespace
2133 $val =~ s/^\s+//;
2134 $val =~ s/\s+$//;
2136 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2137 $unit = lc($unit);
2138 # unknown unit is treated as 1
2139 return $num * ($unit eq 'g' ? 1073741824 :
2140 $unit eq 'm' ? 1048576 :
2141 $unit eq 'k' ? 1024 : 1);
2143 return $val;
2146 # convert config value to array reference, if needed
2147 sub config_to_multi {
2148 my $val = shift;
2150 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2153 sub git_get_project_config {
2154 my ($key, $type) = @_;
2156 # key sanity check
2157 return unless ($key);
2158 $key =~ s/^gitweb\.//;
2159 return if ($key =~ m/\W/);
2161 # type sanity check
2162 if (defined $type) {
2163 $type =~ s/^--//;
2164 $type = undef
2165 unless ($type eq 'bool' || $type eq 'int');
2168 # get config
2169 if (!defined $config_file ||
2170 $config_file ne "$git_dir/config") {
2171 %config = git_parse_project_config('gitweb');
2172 $config_file = "$git_dir/config";
2175 # check if config variable (key) exists
2176 return unless exists $config{"gitweb.$key"};
2178 # ensure given type
2179 if (!defined $type) {
2180 return $config{"gitweb.$key"};
2181 } elsif ($type eq 'bool') {
2182 # backward compatibility: 'git config --bool' returns true/false
2183 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2184 } elsif ($type eq 'int') {
2185 return config_to_int($config{"gitweb.$key"});
2187 return $config{"gitweb.$key"};
2190 # get hash of given path at given ref
2191 sub git_get_hash_by_path {
2192 my $base = shift;
2193 my $path = shift || return undef;
2194 my $type = shift;
2196 $path =~ s,/+$,,;
2198 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2199 or die_error(500, "Open git-ls-tree failed");
2200 my $line = <$fd>;
2201 close $fd or return undef;
2203 if (!defined $line) {
2204 # there is no tree or hash given by $path at $base
2205 return undef;
2208 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2209 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2210 if (defined $type && $type ne $2) {
2211 # type doesn't match
2212 return undef;
2214 return $3;
2217 # get path of entry with given hash at given tree-ish (ref)
2218 # used to get 'from' filename for combined diff (merge commit) for renames
2219 sub git_get_path_by_hash {
2220 my $base = shift || return;
2221 my $hash = shift || return;
2223 local $/ = "\0";
2225 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2226 or return undef;
2227 while (my $line = <$fd>) {
2228 chomp $line;
2230 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2231 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2232 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2233 close $fd;
2234 return $1;
2237 close $fd;
2238 return undef;
2241 ## ......................................................................
2242 ## git utility functions, directly accessing git repository
2244 sub git_get_project_description {
2245 my $path = shift;
2247 $git_dir = "$projectroot/$path";
2248 open my $fd, '<', "$git_dir/description"
2249 or return git_get_project_config('description');
2250 my $descr = <$fd>;
2251 close $fd;
2252 if (defined $descr) {
2253 chomp $descr;
2255 return $descr;
2258 sub git_get_project_ctags {
2259 my $path = shift;
2260 my $ctags = {};
2262 $git_dir = "$projectroot/$path";
2263 opendir my $dh, "$git_dir/ctags"
2264 or return $ctags;
2265 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2266 open my $ct, '<', $_ or next;
2267 my $val = <$ct>;
2268 chomp $val;
2269 close $ct;
2270 my $ctag = $_; $ctag =~ s#.*/##;
2271 $ctags->{$ctag} = $val;
2273 closedir $dh;
2274 $ctags;
2277 sub git_populate_project_tagcloud {
2278 my $ctags = shift;
2280 # First, merge different-cased tags; tags vote on casing
2281 my %ctags_lc;
2282 foreach (keys %$ctags) {
2283 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2284 if (not $ctags_lc{lc $_}->{topcount}
2285 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2286 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2287 $ctags_lc{lc $_}->{topname} = $_;
2291 my $cloud;
2292 if (eval { require HTML::TagCloud; 1; }) {
2293 $cloud = HTML::TagCloud->new;
2294 foreach (sort keys %ctags_lc) {
2295 # Pad the title with spaces so that the cloud looks
2296 # less crammed.
2297 my $title = $ctags_lc{$_}->{topname};
2298 $title =~ s/ /&nbsp;/g;
2299 $title =~ s/^/&nbsp;/g;
2300 $title =~ s/$/&nbsp;/g;
2301 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2303 } else {
2304 $cloud = \%ctags_lc;
2306 $cloud;
2309 sub git_show_project_tagcloud {
2310 my ($cloud, $count) = @_;
2311 print STDERR ref($cloud)."..\n";
2312 if (ref $cloud eq 'HTML::TagCloud') {
2313 return $cloud->html_and_css($count);
2314 } else {
2315 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2316 return '<p align="center">' . join (', ', map {
2317 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2318 } splice(@tags, 0, $count)) . '</p>';
2322 sub git_get_project_url_list {
2323 my $path = shift;
2325 $git_dir = "$projectroot/$path";
2326 open my $fd, '<', "$git_dir/cloneurl"
2327 or return wantarray ?
2328 @{ config_to_multi(git_get_project_config('url')) } :
2329 config_to_multi(git_get_project_config('url'));
2330 my @git_project_url_list = map { chomp; $_ } <$fd>;
2331 close $fd;
2333 return wantarray ? @git_project_url_list : \@git_project_url_list;
2336 sub git_get_projects_list {
2337 my ($filter) = @_;
2338 my @list;
2340 $filter ||= '';
2341 $filter =~ s/\.git$//;
2343 my $check_forks = gitweb_check_feature('forks');
2345 if (-d $projects_list) {
2346 # search in directory
2347 my $dir = $projects_list . ($filter ? "/$filter" : '');
2348 # remove the trailing "/"
2349 $dir =~ s!/+$!!;
2350 my $pfxlen = length("$dir");
2351 my $pfxdepth = ($dir =~ tr!/!!);
2353 File::Find::find({
2354 follow_fast => 1, # follow symbolic links
2355 follow_skip => 2, # ignore duplicates
2356 dangling_symlinks => 0, # ignore dangling symlinks, silently
2357 wanted => sub {
2358 # skip project-list toplevel, if we get it.
2359 return if (m!^[/.]$!);
2360 # only directories can be git repositories
2361 return unless (-d $_);
2362 # don't traverse too deep (Find is super slow on os x)
2363 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2364 $File::Find::prune = 1;
2365 return;
2368 my $subdir = substr($File::Find::name, $pfxlen + 1);
2369 # we check related file in $projectroot
2370 my $path = ($filter ? "$filter/" : '') . $subdir;
2371 if (check_export_ok("$projectroot/$path")) {
2372 push @list, { path => $path };
2373 $File::Find::prune = 1;
2376 }, "$dir");
2378 } elsif (-f $projects_list) {
2379 # read from file(url-encoded):
2380 # 'git%2Fgit.git Linus+Torvalds'
2381 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2382 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2383 my %paths;
2384 open my $fd, '<', $projects_list or return;
2385 PROJECT:
2386 while (my $line = <$fd>) {
2387 chomp $line;
2388 my ($path, $owner) = split ' ', $line;
2389 $path = unescape($path);
2390 $owner = unescape($owner);
2391 if (!defined $path) {
2392 next;
2394 if ($filter ne '') {
2395 # looking for forks;
2396 my $pfx = substr($path, 0, length($filter));
2397 if ($pfx ne $filter) {
2398 next PROJECT;
2400 my $sfx = substr($path, length($filter));
2401 if ($sfx !~ /^\/.*\.git$/) {
2402 next PROJECT;
2404 } elsif ($check_forks) {
2405 PATH:
2406 foreach my $filter (keys %paths) {
2407 # looking for forks;
2408 my $pfx = substr($path, 0, length($filter));
2409 if ($pfx ne $filter) {
2410 next PATH;
2412 my $sfx = substr($path, length($filter));
2413 if ($sfx !~ /^\/.*\.git$/) {
2414 next PATH;
2416 # is a fork, don't include it in
2417 # the list
2418 next PROJECT;
2421 if (check_export_ok("$projectroot/$path")) {
2422 my $pr = {
2423 path => $path,
2424 owner => to_utf8($owner),
2426 push @list, $pr;
2427 (my $forks_path = $path) =~ s/\.git$//;
2428 $paths{$forks_path}++;
2431 close $fd;
2433 return @list;
2436 our $gitweb_project_owner = undef;
2437 sub git_get_project_list_from_file {
2439 return if (defined $gitweb_project_owner);
2441 $gitweb_project_owner = {};
2442 # read from file (url-encoded):
2443 # 'git%2Fgit.git Linus+Torvalds'
2444 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2445 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2446 if (-f $projects_list) {
2447 open(my $fd, '<', $projects_list);
2448 while (my $line = <$fd>) {
2449 chomp $line;
2450 my ($pr, $ow) = split ' ', $line;
2451 $pr = unescape($pr);
2452 $ow = unescape($ow);
2453 $gitweb_project_owner->{$pr} = to_utf8($ow);
2455 close $fd;
2459 sub git_get_project_owner {
2460 my $project = shift;
2461 my $owner;
2463 return undef unless $project;
2464 $git_dir = "$projectroot/$project";
2466 if (!defined $gitweb_project_owner) {
2467 git_get_project_list_from_file();
2470 if (exists $gitweb_project_owner->{$project}) {
2471 $owner = $gitweb_project_owner->{$project};
2473 if (!defined $owner){
2474 $owner = git_get_project_config('owner');
2476 if (!defined $owner) {
2477 $owner = get_file_owner("$git_dir");
2480 return $owner;
2483 sub git_get_last_activity {
2484 my ($path) = @_;
2485 my $fd;
2487 $git_dir = "$projectroot/$path";
2488 open($fd, "-|", git_cmd(), 'for-each-ref',
2489 '--format=%(committer)',
2490 '--sort=-committerdate',
2491 '--count=1',
2492 'refs/heads') or return;
2493 my $most_recent = <$fd>;
2494 close $fd or return;
2495 if (defined $most_recent &&
2496 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2497 my $timestamp = $1;
2498 my $age = time - $timestamp;
2499 return ($age, age_string($age));
2501 return (undef, undef);
2504 sub git_get_references {
2505 my $type = shift || "";
2506 my %refs;
2507 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2508 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2509 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2510 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2511 or return;
2513 while (my $line = <$fd>) {
2514 chomp $line;
2515 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2516 if (defined $refs{$1}) {
2517 push @{$refs{$1}}, $2;
2518 } else {
2519 $refs{$1} = [ $2 ];
2523 close $fd or return;
2524 return \%refs;
2527 sub git_get_rev_name_tags {
2528 my $hash = shift || return undef;
2530 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2531 or return;
2532 my $name_rev = <$fd>;
2533 close $fd;
2535 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2536 return $1;
2537 } else {
2538 # catches also '$hash undefined' output
2539 return undef;
2543 ## ----------------------------------------------------------------------
2544 ## parse to hash functions
2546 sub parse_date {
2547 my $epoch = shift;
2548 my $tz = shift || "-0000";
2550 my %date;
2551 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2552 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2553 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2554 $date{'hour'} = $hour;
2555 $date{'minute'} = $min;
2556 $date{'mday'} = $mday;
2557 $date{'day'} = $days[$wday];
2558 $date{'month'} = $months[$mon];
2559 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2560 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2561 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2562 $mday, $months[$mon], $hour ,$min;
2563 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2564 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2566 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2567 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2568 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2569 $date{'hour_local'} = $hour;
2570 $date{'minute_local'} = $min;
2571 $date{'tz_local'} = $tz;
2572 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2573 1900+$year, $mon+1, $mday,
2574 $hour, $min, $sec, $tz);
2575 return %date;
2578 sub parse_tag {
2579 my $tag_id = shift;
2580 my %tag;
2581 my @comment;
2583 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2584 $tag{'id'} = $tag_id;
2585 while (my $line = <$fd>) {
2586 chomp $line;
2587 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2588 $tag{'object'} = $1;
2589 } elsif ($line =~ m/^type (.+)$/) {
2590 $tag{'type'} = $1;
2591 } elsif ($line =~ m/^tag (.+)$/) {
2592 $tag{'name'} = $1;
2593 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2594 $tag{'author'} = $1;
2595 $tag{'author_epoch'} = $2;
2596 $tag{'author_tz'} = $3;
2597 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2598 $tag{'author_name'} = $1;
2599 $tag{'author_email'} = $2;
2600 } else {
2601 $tag{'author_name'} = $tag{'author'};
2603 } elsif ($line =~ m/--BEGIN/) {
2604 push @comment, $line;
2605 last;
2606 } elsif ($line eq "") {
2607 last;
2610 push @comment, <$fd>;
2611 $tag{'comment'} = \@comment;
2612 close $fd or return;
2613 if (!defined $tag{'name'}) {
2614 return
2616 return %tag
2619 sub parse_commit_text {
2620 my ($commit_text, $withparents) = @_;
2621 my @commit_lines = split '\n', $commit_text;
2622 my %co;
2624 pop @commit_lines; # Remove '\0'
2626 if (! @commit_lines) {
2627 return;
2630 my $header = shift @commit_lines;
2631 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2632 return;
2634 ($co{'id'}, my @parents) = split ' ', $header;
2635 while (my $line = shift @commit_lines) {
2636 last if $line eq "\n";
2637 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2638 $co{'tree'} = $1;
2639 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2640 push @parents, $1;
2641 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2642 $co{'author'} = to_utf8($1);
2643 $co{'author_epoch'} = $2;
2644 $co{'author_tz'} = $3;
2645 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2646 $co{'author_name'} = $1;
2647 $co{'author_email'} = $2;
2648 } else {
2649 $co{'author_name'} = $co{'author'};
2651 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2652 $co{'committer'} = to_utf8($1);
2653 $co{'committer_epoch'} = $2;
2654 $co{'committer_tz'} = $3;
2655 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2656 $co{'committer_name'} = $1;
2657 $co{'committer_email'} = $2;
2658 } else {
2659 $co{'committer_name'} = $co{'committer'};
2663 if (!defined $co{'tree'}) {
2664 return;
2666 $co{'parents'} = \@parents;
2667 $co{'parent'} = $parents[0];
2669 foreach my $title (@commit_lines) {
2670 $title =~ s/^ //;
2671 if ($title ne "") {
2672 $co{'title'} = chop_str($title, 80, 5);
2673 # remove leading stuff of merges to make the interesting part visible
2674 if (length($title) > 50) {
2675 $title =~ s/^Automatic //;
2676 $title =~ s/^merge (of|with) /Merge ... /i;
2677 if (length($title) > 50) {
2678 $title =~ s/(http|rsync):\/\///;
2680 if (length($title) > 50) {
2681 $title =~ s/(master|www|rsync)\.//;
2683 if (length($title) > 50) {
2684 $title =~ s/kernel.org:?//;
2686 if (length($title) > 50) {
2687 $title =~ s/\/pub\/scm//;
2690 $co{'title_short'} = chop_str($title, 50, 5);
2691 last;
2694 if (! defined $co{'title'} || $co{'title'} eq "") {
2695 $co{'title'} = $co{'title_short'} = '(no commit message)';
2697 # remove added spaces
2698 foreach my $line (@commit_lines) {
2699 $line =~ s/^ //;
2701 $co{'comment'} = \@commit_lines;
2703 my $age = time - $co{'committer_epoch'};
2704 $co{'age'} = $age;
2705 $co{'age_string'} = age_string($age);
2706 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2707 if ($age > 60*60*24*7*2) {
2708 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2709 $co{'age_string_age'} = $co{'age_string'};
2710 } else {
2711 $co{'age_string_date'} = $co{'age_string'};
2712 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2714 return %co;
2717 sub parse_commit {
2718 my ($commit_id) = @_;
2719 my %co;
2721 local $/ = "\0";
2723 open my $fd, "-|", git_cmd(), "rev-list",
2724 "--parents",
2725 "--header",
2726 "--max-count=1",
2727 $commit_id,
2728 "--",
2729 or die_error(500, "Open git-rev-list failed");
2730 %co = parse_commit_text(<$fd>, 1);
2731 close $fd;
2733 return %co;
2736 sub parse_commits {
2737 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2738 my @cos;
2740 $maxcount ||= 1;
2741 $skip ||= 0;
2743 local $/ = "\0";
2745 open my $fd, "-|", git_cmd(), "rev-list",
2746 "--header",
2747 @args,
2748 ("--max-count=" . $maxcount),
2749 ("--skip=" . $skip),
2750 @extra_options,
2751 $commit_id,
2752 "--",
2753 ($filename ? ($filename) : ())
2754 or die_error(500, "Open git-rev-list failed");
2755 while (my $line = <$fd>) {
2756 my %co = parse_commit_text($line);
2757 push @cos, \%co;
2759 close $fd;
2761 return wantarray ? @cos : \@cos;
2764 # parse line of git-diff-tree "raw" output
2765 sub parse_difftree_raw_line {
2766 my $line = shift;
2767 my %res;
2769 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2770 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2771 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2772 $res{'from_mode'} = $1;
2773 $res{'to_mode'} = $2;
2774 $res{'from_id'} = $3;
2775 $res{'to_id'} = $4;
2776 $res{'status'} = $5;
2777 $res{'similarity'} = $6;
2778 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2779 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2780 } else {
2781 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2784 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2785 # combined diff (for merge commit)
2786 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2787 $res{'nparents'} = length($1);
2788 $res{'from_mode'} = [ split(' ', $2) ];
2789 $res{'to_mode'} = pop @{$res{'from_mode'}};
2790 $res{'from_id'} = [ split(' ', $3) ];
2791 $res{'to_id'} = pop @{$res{'from_id'}};
2792 $res{'status'} = [ split('', $4) ];
2793 $res{'to_file'} = unquote($5);
2795 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2796 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2797 $res{'commit'} = $1;
2800 return wantarray ? %res : \%res;
2803 # wrapper: return parsed line of git-diff-tree "raw" output
2804 # (the argument might be raw line, or parsed info)
2805 sub parsed_difftree_line {
2806 my $line_or_ref = shift;
2808 if (ref($line_or_ref) eq "HASH") {
2809 # pre-parsed (or generated by hand)
2810 return $line_or_ref;
2811 } else {
2812 return parse_difftree_raw_line($line_or_ref);
2816 # parse line of git-ls-tree output
2817 sub parse_ls_tree_line {
2818 my $line = shift;
2819 my %opts = @_;
2820 my %res;
2822 if ($opts{'-l'}) {
2823 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
2824 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
2826 $res{'mode'} = $1;
2827 $res{'type'} = $2;
2828 $res{'hash'} = $3;
2829 $res{'size'} = $4;
2830 if ($opts{'-z'}) {
2831 $res{'name'} = $5;
2832 } else {
2833 $res{'name'} = unquote($5);
2835 } else {
2836 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2837 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2839 $res{'mode'} = $1;
2840 $res{'type'} = $2;
2841 $res{'hash'} = $3;
2842 if ($opts{'-z'}) {
2843 $res{'name'} = $4;
2844 } else {
2845 $res{'name'} = unquote($4);
2849 return wantarray ? %res : \%res;
2852 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2853 sub parse_from_to_diffinfo {
2854 my ($diffinfo, $from, $to, @parents) = @_;
2856 if ($diffinfo->{'nparents'}) {
2857 # combined diff
2858 $from->{'file'} = [];
2859 $from->{'href'} = [];
2860 fill_from_file_info($diffinfo, @parents)
2861 unless exists $diffinfo->{'from_file'};
2862 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2863 $from->{'file'}[$i] =
2864 defined $diffinfo->{'from_file'}[$i] ?
2865 $diffinfo->{'from_file'}[$i] :
2866 $diffinfo->{'to_file'};
2867 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2868 $from->{'href'}[$i] = href(action=>"blob",
2869 hash_base=>$parents[$i],
2870 hash=>$diffinfo->{'from_id'}[$i],
2871 file_name=>$from->{'file'}[$i]);
2872 } else {
2873 $from->{'href'}[$i] = undef;
2876 } else {
2877 # ordinary (not combined) diff
2878 $from->{'file'} = $diffinfo->{'from_file'};
2879 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2880 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2881 hash=>$diffinfo->{'from_id'},
2882 file_name=>$from->{'file'});
2883 } else {
2884 delete $from->{'href'};
2888 $to->{'file'} = $diffinfo->{'to_file'};
2889 if (!is_deleted($diffinfo)) { # file exists in result
2890 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2891 hash=>$diffinfo->{'to_id'},
2892 file_name=>$to->{'file'});
2893 } else {
2894 delete $to->{'href'};
2898 ## ......................................................................
2899 ## parse to array of hashes functions
2901 sub git_get_heads_list {
2902 my $limit = shift;
2903 my @headslist;
2905 open my $fd, '-|', git_cmd(), 'for-each-ref',
2906 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2907 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2908 'refs/heads'
2909 or return;
2910 while (my $line = <$fd>) {
2911 my %ref_item;
2913 chomp $line;
2914 my ($refinfo, $committerinfo) = split(/\0/, $line);
2915 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2916 my ($committer, $epoch, $tz) =
2917 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2918 $ref_item{'fullname'} = $name;
2919 $name =~ s!^refs/heads/!!;
2921 $ref_item{'name'} = $name;
2922 $ref_item{'id'} = $hash;
2923 $ref_item{'title'} = $title || '(no commit message)';
2924 $ref_item{'epoch'} = $epoch;
2925 if ($epoch) {
2926 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2927 } else {
2928 $ref_item{'age'} = "unknown";
2931 push @headslist, \%ref_item;
2933 close $fd;
2935 return wantarray ? @headslist : \@headslist;
2938 sub git_get_tags_list {
2939 my $limit = shift;
2940 my @tagslist;
2942 open my $fd, '-|', git_cmd(), 'for-each-ref',
2943 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2944 '--format=%(objectname) %(objecttype) %(refname) '.
2945 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2946 'refs/tags'
2947 or return;
2948 while (my $line = <$fd>) {
2949 my %ref_item;
2951 chomp $line;
2952 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2953 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2954 my ($creator, $epoch, $tz) =
2955 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2956 $ref_item{'fullname'} = $name;
2957 $name =~ s!^refs/tags/!!;
2959 $ref_item{'type'} = $type;
2960 $ref_item{'id'} = $id;
2961 $ref_item{'name'} = $name;
2962 if ($type eq "tag") {
2963 $ref_item{'subject'} = $title;
2964 $ref_item{'reftype'} = $reftype;
2965 $ref_item{'refid'} = $refid;
2966 } else {
2967 $ref_item{'reftype'} = $type;
2968 $ref_item{'refid'} = $id;
2971 if ($type eq "tag" || $type eq "commit") {
2972 $ref_item{'epoch'} = $epoch;
2973 if ($epoch) {
2974 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2975 } else {
2976 $ref_item{'age'} = "unknown";
2980 push @tagslist, \%ref_item;
2982 close $fd;
2984 return wantarray ? @tagslist : \@tagslist;
2987 ## ----------------------------------------------------------------------
2988 ## filesystem-related functions
2990 sub get_file_owner {
2991 my $path = shift;
2993 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2994 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2995 if (!defined $gcos) {
2996 return undef;
2998 my $owner = $gcos;
2999 $owner =~ s/[,;].*$//;
3000 return to_utf8($owner);
3003 # assume that file exists
3004 sub insert_file {
3005 my $filename = shift;
3007 open my $fd, '<', $filename;
3008 print map { to_utf8($_) } <$fd>;
3009 close $fd;
3012 ## ......................................................................
3013 ## mimetype related functions
3015 sub mimetype_guess_file {
3016 my $filename = shift;
3017 my $mimemap = shift;
3018 -r $mimemap or return undef;
3020 my %mimemap;
3021 open(my $mh, '<', $mimemap) or return undef;
3022 while (<$mh>) {
3023 next if m/^#/; # skip comments
3024 my ($mimetype, $exts) = split(/\t+/);
3025 if (defined $exts) {
3026 my @exts = split(/\s+/, $exts);
3027 foreach my $ext (@exts) {
3028 $mimemap{$ext} = $mimetype;
3032 close($mh);
3034 $filename =~ /\.([^.]*)$/;
3035 return $mimemap{$1};
3038 sub mimetype_guess {
3039 my $filename = shift;
3040 my $mime;
3041 $filename =~ /\./ or return undef;
3043 if ($mimetypes_file) {
3044 my $file = $mimetypes_file;
3045 if ($file !~ m!^/!) { # if it is relative path
3046 # it is relative to project
3047 $file = "$projectroot/$project/$file";
3049 $mime = mimetype_guess_file($filename, $file);
3051 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3052 return $mime;
3055 sub blob_mimetype {
3056 my $fd = shift;
3057 my $filename = shift;
3059 if ($filename) {
3060 my $mime = mimetype_guess($filename);
3061 $mime and return $mime;
3064 # just in case
3065 return $default_blob_plain_mimetype unless $fd;
3067 if (-T $fd) {
3068 return 'text/plain';
3069 } elsif (! $filename) {
3070 return 'application/octet-stream';
3071 } elsif ($filename =~ m/\.png$/i) {
3072 return 'image/png';
3073 } elsif ($filename =~ m/\.gif$/i) {
3074 return 'image/gif';
3075 } elsif ($filename =~ m/\.jpe?g$/i) {
3076 return 'image/jpeg';
3077 } else {
3078 return 'application/octet-stream';
3082 sub blob_contenttype {
3083 my ($fd, $file_name, $type) = @_;
3085 $type ||= blob_mimetype($fd, $file_name);
3086 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3087 $type .= "; charset=$default_text_plain_charset";
3090 return $type;
3093 ## ======================================================================
3094 ## functions printing HTML: header, footer, error page
3096 sub git_header_html {
3097 my $status = shift || "200 OK";
3098 my $expires = shift;
3100 my $title = "$site_name";
3101 if (defined $project) {
3102 $title .= " - " . to_utf8($project);
3103 if (defined $action) {
3104 $title .= "/$action";
3105 if (defined $file_name) {
3106 $title .= " - " . esc_path($file_name);
3107 if ($action eq "tree" && $file_name !~ m|/$|) {
3108 $title .= "/";
3113 my $content_type;
3114 # require explicit support from the UA if we are to send the page as
3115 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3116 # we have to do this because MSIE sometimes globs '*/*', pretending to
3117 # support xhtml+xml but choking when it gets what it asked for.
3118 if (defined $cgi->http('HTTP_ACCEPT') &&
3119 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3120 $cgi->Accept('application/xhtml+xml') != 0) {
3121 $content_type = 'application/xhtml+xml';
3122 } else {
3123 $content_type = 'text/html';
3125 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3126 -status=> $status, -expires => $expires);
3127 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3128 print <<EOF;
3129 <?xml version="1.0" encoding="utf-8"?>
3130 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3131 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3132 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3133 <!-- git core binaries version $git_version -->
3134 <head>
3135 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3136 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3137 <meta name="robots" content="index, nofollow"/>
3138 <title>$title</title>
3139 <script type="text/javascript">/* <![CDATA[ */
3140 function fixBlameLinks() {
3141 var allLinks = document.getElementsByTagName("a");
3142 for (var i = 0; i < allLinks.length; i++) {
3143 var link = allLinks.item(i);
3144 if (link.className == 'blamelink')
3145 link.href = link.href.replace("a=blame", "a=blame_incremental");
3148 /* ]]> */</script>
3150 # the stylesheet, favicon etc urls won't work correctly with path_info
3151 # unless we set the appropriate base URL
3152 if ($ENV{'PATH_INFO'}) {
3153 print "<base href=\"".esc_url($base_url)."\" />\n";
3155 # print out each stylesheet that exist, providing backwards capability
3156 # for those people who defined $stylesheet in a config file
3157 if (defined $stylesheet) {
3158 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3159 } else {
3160 foreach my $stylesheet (@stylesheets) {
3161 next unless $stylesheet;
3162 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3165 if (defined $project) {
3166 my %href_params = get_feed_info();
3167 if (!exists $href_params{'-title'}) {
3168 $href_params{'-title'} = 'log';
3171 foreach my $format qw(RSS Atom) {
3172 my $type = lc($format);
3173 my %link_attr = (
3174 '-rel' => 'alternate',
3175 '-title' => "$project - $href_params{'-title'} - $format feed",
3176 '-type' => "application/$type+xml"
3179 $href_params{'action'} = $type;
3180 $link_attr{'-href'} = href(%href_params);
3181 print "<link ".
3182 "rel=\"$link_attr{'-rel'}\" ".
3183 "title=\"$link_attr{'-title'}\" ".
3184 "href=\"$link_attr{'-href'}\" ".
3185 "type=\"$link_attr{'-type'}\" ".
3186 "/>\n";
3188 $href_params{'extra_options'} = '--no-merges';
3189 $link_attr{'-href'} = href(%href_params);
3190 $link_attr{'-title'} .= ' (no merges)';
3191 print "<link ".
3192 "rel=\"$link_attr{'-rel'}\" ".
3193 "title=\"$link_attr{'-title'}\" ".
3194 "href=\"$link_attr{'-href'}\" ".
3195 "type=\"$link_attr{'-type'}\" ".
3196 "/>\n";
3199 } else {
3200 printf('<link rel="alternate" title="%s projects list" '.
3201 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3202 $site_name, href(project=>undef, action=>"project_index"));
3203 printf('<link rel="alternate" title="%s projects feeds" '.
3204 'href="%s" type="text/x-opml" />'."\n",
3205 $site_name, href(project=>undef, action=>"opml"));
3207 if (defined $favicon) {
3208 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
3211 if (defined $gitwebjs) {
3212 print qq(<script src="$gitwebjs" type="text/javascript"></script>\n);
3215 print "</head>\n" .
3216 "<body onload=\"fixBlameLinks();\">\n";
3218 if (-f $site_header) {
3219 insert_file($site_header);
3222 print "<div class=\"page_header\">\n" .
3223 $cgi->a({-href => esc_url($logo_url),
3224 -title => $logo_label},
3225 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
3226 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3227 if (defined $project) {
3228 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3229 if (defined $action) {
3230 print " / $action";
3232 print "\n";
3234 print "</div>\n";
3236 my $have_search = gitweb_check_feature('search');
3237 if (defined $project && $have_search) {
3238 if (!defined $searchtext) {
3239 $searchtext = "";
3241 my $search_hash;
3242 if (defined $hash_base) {
3243 $search_hash = $hash_base;
3244 } elsif (defined $hash) {
3245 $search_hash = $hash;
3246 } else {
3247 $search_hash = "HEAD";
3249 my $action = $my_uri;
3250 my $use_pathinfo = gitweb_check_feature('pathinfo');
3251 if ($use_pathinfo) {
3252 $action .= "/".esc_url($project);
3254 print $cgi->startform(-method => "get", -action => $action) .
3255 "<div class=\"search\">\n" .
3256 (!$use_pathinfo &&
3257 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3258 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3259 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3260 $cgi->popup_menu(-name => 'st', -default => 'commit',
3261 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3262 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3263 " search:\n",
3264 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3265 "<span title=\"Extended regular expression\">" .
3266 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3267 -checked => $search_use_regexp) .
3268 "</span>" .
3269 "</div>" .
3270 $cgi->end_form() . "\n";
3274 sub git_footer_html {
3275 my $feed_class = 'rss_logo';
3277 print "<div class=\"page_footer\">\n";
3278 if (defined $project) {
3279 my $descr = git_get_project_description($project);
3280 if (defined $descr) {
3281 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3284 my %href_params = get_feed_info();
3285 if (!%href_params) {
3286 $feed_class .= ' generic';
3288 $href_params{'-title'} ||= 'log';
3290 foreach my $format qw(RSS Atom) {
3291 $href_params{'action'} = lc($format);
3292 print $cgi->a({-href => href(%href_params),
3293 -title => "$href_params{'-title'} $format feed",
3294 -class => $feed_class}, $format)."\n";
3297 } else {
3298 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3299 -class => $feed_class}, "OPML") . " ";
3300 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3301 -class => $feed_class}, "TXT") . "\n";
3303 print "</div>\n"; # class="page_footer"
3305 if (-f $site_footer) {
3306 insert_file($site_footer);
3309 print "</body>\n" .
3310 "</html>";
3313 # die_error(<http_status_code>, <error_message>)
3314 # Example: die_error(404, 'Hash not found')
3315 # By convention, use the following status codes (as defined in RFC 2616):
3316 # 400: Invalid or missing CGI parameters, or
3317 # requested object exists but has wrong type.
3318 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3319 # this server or project.
3320 # 404: Requested object/revision/project doesn't exist.
3321 # 500: The server isn't configured properly, or
3322 # an internal error occurred (e.g. failed assertions caused by bugs), or
3323 # an unknown error occurred (e.g. the git binary died unexpectedly).
3324 sub die_error {
3325 my $status = shift || 500;
3326 my $error = shift || "Internal server error";
3328 my %http_responses = (400 => '400 Bad Request',
3329 403 => '403 Forbidden',
3330 404 => '404 Not Found',
3331 500 => '500 Internal Server Error');
3332 git_header_html($http_responses{$status});
3333 print <<EOF;
3334 <div class="page_body">
3335 <br /><br />
3336 $status - $error
3337 <br />
3338 </div>
3340 git_footer_html();
3341 exit;
3344 ## ----------------------------------------------------------------------
3345 ## functions printing or outputting HTML: navigation
3347 sub git_print_page_nav {
3348 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3349 $extra = '' if !defined $extra; # pager or formats
3351 my @navs = qw(summary log commit commitdiff tree);
3352 if ($suppress) {
3353 @navs = grep { $_ ne $suppress } @navs;
3356 my %arg = map { $_ => {action=>$_} } @navs;
3357 if (defined $head) {
3358 for (qw(commit commitdiff)) {
3359 $arg{$_}{'hash'} = $head;
3361 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3362 $arg{'log'}{'hash'} = $head;
3366 $arg{'log'}{'action'} = 'shortlog';
3367 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3368 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3370 my @actions = gitweb_get_feature('actions');
3371 my %repl = (
3372 '%' => '%',
3373 'n' => $project, # project name
3374 'f' => $git_dir, # project path within filesystem
3375 'h' => $treehead || '', # current hash ('h' parameter)
3376 'b' => $treebase || '', # hash base ('hb' parameter)
3378 while (@actions) {
3379 my ($label, $link, $pos) = splice(@actions,0,3);
3380 # insert
3381 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3382 # munch munch
3383 $link =~ s/%([%nfhb])/$repl{$1}/g;
3384 $arg{$label}{'_href'} = $link;
3387 print "<div class=\"page_nav\">\n" .
3388 (join " | ",
3389 map { $_ eq $current ?
3390 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3391 } @navs);
3392 print "<br/>\n$extra<br/>\n" .
3393 "</div>\n";
3396 sub format_paging_nav {
3397 my ($action, $hash, $head, $page, $has_next_link) = @_;
3398 my $paging_nav;
3401 if ($hash ne $head || $page) {
3402 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
3403 } else {
3404 $paging_nav .= "HEAD";
3407 if ($page > 0) {
3408 $paging_nav .= " &sdot; " .
3409 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3410 -accesskey => "p", -title => "Alt-p"}, "prev");
3411 } else {
3412 $paging_nav .= " &sdot; prev";
3415 if ($has_next_link) {
3416 $paging_nav .= " &sdot; " .
3417 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3418 -accesskey => "n", -title => "Alt-n"}, "next");
3419 } else {
3420 $paging_nav .= " &sdot; next";
3423 return $paging_nav;
3426 sub format_log_nav {
3427 my ($action, $hash, $head, $page, $has_next_link) = @_;
3428 my $paging_nav;
3430 if ($action eq 'shortlog') {
3431 $paging_nav .= 'shortlog';
3432 } else {
3433 $paging_nav .= $cgi->a({-href => href(action=>'shortlog', -replay=>1)}, 'shortlog');
3435 $paging_nav .= ' | ';
3436 if ($action eq 'log') {
3437 $paging_nav .= 'fulllog';
3438 } else {
3439 $paging_nav .= $cgi->a({-href => href(action=>'log', -replay=>1)}, 'fulllog');
3442 $paging_nav .= " | " . format_paging_nav($action, $hash, $head, $page, $has_next_link);
3443 return $paging_nav;
3446 ## ......................................................................
3447 ## functions printing or outputting HTML: div
3449 sub git_print_header_div {
3450 my ($action, $title, $hash, $hash_base) = @_;
3451 my %args = ();
3453 $args{'action'} = $action;
3454 $args{'hash'} = $hash if $hash;
3455 $args{'hash_base'} = $hash_base if $hash_base;
3457 print "<div class=\"header\">\n" .
3458 $cgi->a({-href => href(%args), -class => "title"},
3459 $title ? $title : $action) .
3460 "\n</div>\n";
3463 sub print_local_time {
3464 my %date = @_;
3465 if ($date{'hour_local'} < 6) {
3466 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3467 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3468 } else {
3469 printf(" (%02d:%02d %s)",
3470 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3474 # Outputs the author name and date in long form
3475 sub git_print_authorship {
3476 my $co = shift;
3477 my %opts = @_;
3478 my $tag = $opts{-tag} || 'div';
3479 my $author = $co->{'author_name'};
3481 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3482 print "<$tag class=\"author_date\">" .
3483 format_search_author($author, "author", esc_html($author)) .
3484 " [$ad{'rfc2822'}";
3485 print_local_time(%ad) if ($opts{-localtime});
3486 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3487 . "</$tag>\n";
3490 # Outputs table rows containing the full author or committer information,
3491 # in the format expected for 'commit' view (& similia).
3492 # Parameters are a commit hash reference, followed by the list of people
3493 # to output information for. If the list is empty it defalts to both
3494 # author and committer.
3495 sub git_print_authorship_rows {
3496 my $co = shift;
3497 # too bad we can't use @people = @_ || ('author', 'committer')
3498 my @people = @_;
3499 @people = ('author', 'committer') unless @people;
3500 foreach my $who (@people) {
3501 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3502 print "<tr><td>$who</td><td>" .
3503 format_search_author($co->{"${who}_name"}, $who,
3504 esc_html($co->{"${who}_name"})) . " " .
3505 format_search_author($co->{"${who}_email"}, $who,
3506 esc_html("<" . $co->{"${who}_email"} . ">")) .
3507 "</td><td rowspan=\"2\">" .
3508 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3509 "</td></tr>\n" .
3510 "<tr>" .
3511 "<td></td><td> $wd{'rfc2822'}";
3512 print_local_time(%wd);
3513 print "</td>" .
3514 "</tr>\n";
3518 sub git_print_page_path {
3519 my $name = shift;
3520 my $type = shift;
3521 my $hb = shift;
3524 print "<div class=\"page_path\">";
3525 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3526 -title => 'tree root'}, to_utf8("[$project]"));
3527 print " / ";
3528 if (defined $name) {
3529 my @dirname = split '/', $name;
3530 my $basename = pop @dirname;
3531 my $fullname = '';
3533 foreach my $dir (@dirname) {
3534 $fullname .= ($fullname ? '/' : '') . $dir;
3535 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3536 hash_base=>$hb),
3537 -title => $fullname}, esc_path($dir));
3538 print " / ";
3540 if (defined $type && $type eq 'blob') {
3541 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3542 hash_base=>$hb),
3543 -title => $name}, esc_path($basename));
3544 } elsif (defined $type && $type eq 'tree') {
3545 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3546 hash_base=>$hb),
3547 -title => $name}, esc_path($basename));
3548 print " / ";
3549 } else {
3550 print esc_path($basename);
3553 print "<br/></div>\n";
3556 sub git_print_log {
3557 my $log = shift;
3558 my %opts = @_;
3560 if ($opts{'-remove_title'}) {
3561 # remove title, i.e. first line of log
3562 shift @$log;
3564 # remove leading empty lines
3565 while (defined $log->[0] && $log->[0] eq "") {
3566 shift @$log;
3569 # print log
3570 my $signoff = 0;
3571 my $empty = 0;
3572 foreach my $line (@$log) {
3573 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3574 $signoff = 1;
3575 $empty = 0;
3576 if (! $opts{'-remove_signoff'}) {
3577 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3578 next;
3579 } else {
3580 # remove signoff lines
3581 next;
3583 } else {
3584 $signoff = 0;
3587 # print only one empty line
3588 # do not print empty line after signoff
3589 if ($line eq "") {
3590 next if ($empty || $signoff);
3591 $empty = 1;
3592 } else {
3593 $empty = 0;
3596 print format_log_line_html($line) . "<br/>\n";
3599 if ($opts{'-final_empty_line'}) {
3600 # end with single empty line
3601 print "<br/>\n" unless $empty;
3605 # return link target (what link points to)
3606 sub git_get_link_target {
3607 my $hash = shift;
3608 my $link_target;
3610 # read link
3611 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3612 or return;
3614 local $/ = undef;
3615 $link_target = <$fd>;
3617 close $fd
3618 or return;
3620 return $link_target;
3623 # given link target, and the directory (basedir) the link is in,
3624 # return target of link relative to top directory (top tree);
3625 # return undef if it is not possible (including absolute links).
3626 sub normalize_link_target {
3627 my ($link_target, $basedir) = @_;
3629 # absolute symlinks (beginning with '/') cannot be normalized
3630 return if (substr($link_target, 0, 1) eq '/');
3632 # normalize link target to path from top (root) tree (dir)
3633 my $path;
3634 if ($basedir) {
3635 $path = $basedir . '/' . $link_target;
3636 } else {
3637 # we are in top (root) tree (dir)
3638 $path = $link_target;
3641 # remove //, /./, and /../
3642 my @path_parts;
3643 foreach my $part (split('/', $path)) {
3644 # discard '.' and ''
3645 next if (!$part || $part eq '.');
3646 # handle '..'
3647 if ($part eq '..') {
3648 if (@path_parts) {
3649 pop @path_parts;
3650 } else {
3651 # link leads outside repository (outside top dir)
3652 return;
3654 } else {
3655 push @path_parts, $part;
3658 $path = join('/', @path_parts);
3660 return $path;
3663 # print tree entry (row of git_tree), but without encompassing <tr> element
3664 sub git_print_tree_entry {
3665 my ($t, $basedir, $hash_base, $have_blame) = @_;
3667 my %base_key = ();
3668 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3670 # The format of a table row is: mode list link. Where mode is
3671 # the mode of the entry, list is the name of the entry, an href,
3672 # and link is the action links of the entry.
3674 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3675 if (exists $t->{'size'}) {
3676 print "<td class=\"size\">$t->{'size'}</td>\n";
3678 if ($t->{'type'} eq "blob") {
3679 print "<td class=\"list\">" .
3680 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3681 file_name=>"$basedir$t->{'name'}", %base_key),
3682 -class => "list"}, esc_path($t->{'name'}));
3683 if (S_ISLNK(oct $t->{'mode'})) {
3684 my $link_target = git_get_link_target($t->{'hash'});
3685 if ($link_target) {
3686 my $norm_target = normalize_link_target($link_target, $basedir);
3687 if (defined $norm_target) {
3688 print " -> " .
3689 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3690 file_name=>$norm_target),
3691 -title => $norm_target}, esc_path($link_target));
3692 } else {
3693 print " -> " . esc_path($link_target);
3697 print "</td>\n";
3698 print "<td class=\"link\">";
3699 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3700 file_name=>"$basedir$t->{'name'}", %base_key)},
3701 "blob");
3702 if ($have_blame) {
3703 print " | " .
3704 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3705 file_name=>"$basedir$t->{'name'}", %base_key), -class => "blamelink"},
3706 "blame");
3708 if (defined $hash_base) {
3709 print " | " .
3710 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3711 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3712 "history");
3714 print " | " .
3715 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3716 file_name=>"$basedir$t->{'name'}")},
3717 "raw");
3718 print "</td>\n";
3720 } elsif ($t->{'type'} eq "tree") {
3721 print "<td class=\"list\">";
3722 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3723 file_name=>"$basedir$t->{'name'}",
3724 %base_key)},
3725 esc_path($t->{'name'}));
3726 print "</td>\n";
3727 print "<td class=\"link\">";
3728 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3729 file_name=>"$basedir$t->{'name'}",
3730 %base_key)},
3731 "tree");
3732 if (defined $hash_base) {
3733 print " | " .
3734 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3735 file_name=>"$basedir$t->{'name'}")},
3736 "history");
3738 print "</td>\n";
3739 } else {
3740 # unknown object: we can only present history for it
3741 # (this includes 'commit' object, i.e. submodule support)
3742 print "<td class=\"list\">" .
3743 esc_path($t->{'name'}) .
3744 "</td>\n";
3745 print "<td class=\"link\">";
3746 if (defined $hash_base) {
3747 print $cgi->a({-href => href(action=>"history",
3748 hash_base=>$hash_base,
3749 file_name=>"$basedir$t->{'name'}")},
3750 "history");
3752 print "</td>\n";
3756 ## ......................................................................
3757 ## functions printing large fragments of HTML
3759 # get pre-image filenames for merge (combined) diff
3760 sub fill_from_file_info {
3761 my ($diff, @parents) = @_;
3763 $diff->{'from_file'} = [ ];
3764 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3765 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3766 if ($diff->{'status'}[$i] eq 'R' ||
3767 $diff->{'status'}[$i] eq 'C') {
3768 $diff->{'from_file'}[$i] =
3769 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3773 return $diff;
3776 # is current raw difftree line of file deletion
3777 sub is_deleted {
3778 my $diffinfo = shift;
3780 return $diffinfo->{'to_id'} eq ('0' x 40);
3783 # does patch correspond to [previous] difftree raw line
3784 # $diffinfo - hashref of parsed raw diff format
3785 # $patchinfo - hashref of parsed patch diff format
3786 # (the same keys as in $diffinfo)
3787 sub is_patch_split {
3788 my ($diffinfo, $patchinfo) = @_;
3790 return defined $diffinfo && defined $patchinfo
3791 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3795 sub git_difftree_body {
3796 my ($difftree, $hash, @parents) = @_;
3797 my ($parent) = $parents[0];
3798 my $have_blame = gitweb_check_feature('blame');
3799 print "<div class=\"list_head\">\n";
3800 if ($#{$difftree} > 10) {
3801 print(($#{$difftree} + 1) . " files changed:\n");
3803 print "</div>\n";
3805 print "<table class=\"" .
3806 (@parents > 1 ? "combined " : "") .
3807 "diff_tree\">\n";
3809 # header only for combined diff in 'commitdiff' view
3810 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3811 if ($has_header) {
3812 # table header
3813 print "<thead><tr>\n" .
3814 "<th></th><th></th>\n"; # filename, patchN link
3815 for (my $i = 0; $i < @parents; $i++) {
3816 my $par = $parents[$i];
3817 print "<th>" .
3818 $cgi->a({-href => href(action=>"commitdiff",
3819 hash=>$hash, hash_parent=>$par),
3820 -title => 'commitdiff to parent number ' .
3821 ($i+1) . ': ' . substr($par,0,7)},
3822 $i+1) .
3823 "&nbsp;</th>\n";
3825 print "</tr></thead>\n<tbody>\n";
3828 my $alternate = 1;
3829 my $patchno = 0;
3830 foreach my $line (@{$difftree}) {
3831 my $diff = parsed_difftree_line($line);
3833 if ($alternate) {
3834 print "<tr class=\"dark\">\n";
3835 } else {
3836 print "<tr class=\"light\">\n";
3838 $alternate ^= 1;
3840 if (exists $diff->{'nparents'}) { # combined diff
3842 fill_from_file_info($diff, @parents)
3843 unless exists $diff->{'from_file'};
3845 if (!is_deleted($diff)) {
3846 # file exists in the result (child) commit
3847 print "<td>" .
3848 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3849 file_name=>$diff->{'to_file'},
3850 hash_base=>$hash),
3851 -class => "list"}, esc_path($diff->{'to_file'})) .
3852 "</td>\n";
3853 } else {
3854 print "<td>" .
3855 esc_path($diff->{'to_file'}) .
3856 "</td>\n";
3859 if ($action eq 'commitdiff') {
3860 # link to patch
3861 $patchno++;
3862 print "<td class=\"link\">" .
3863 $cgi->a({-href => "#patch$patchno"}, "patch") .
3864 " | " .
3865 "</td>\n";
3868 my $has_history = 0;
3869 my $not_deleted = 0;
3870 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3871 my $hash_parent = $parents[$i];
3872 my $from_hash = $diff->{'from_id'}[$i];
3873 my $from_path = $diff->{'from_file'}[$i];
3874 my $status = $diff->{'status'}[$i];
3876 $has_history ||= ($status ne 'A');
3877 $not_deleted ||= ($status ne 'D');
3879 if ($status eq 'A') {
3880 print "<td class=\"link\" align=\"right\"> | </td>\n";
3881 } elsif ($status eq 'D') {
3882 print "<td class=\"link\">" .
3883 $cgi->a({-href => href(action=>"blob",
3884 hash_base=>$hash,
3885 hash=>$from_hash,
3886 file_name=>$from_path)},
3887 "blob" . ($i+1)) .
3888 " | </td>\n";
3889 } else {
3890 if ($diff->{'to_id'} eq $from_hash) {
3891 print "<td class=\"link nochange\">";
3892 } else {
3893 print "<td class=\"link\">";
3895 print $cgi->a({-href => href(action=>"blobdiff",
3896 hash=>$diff->{'to_id'},
3897 hash_parent=>$from_hash,
3898 hash_base=>$hash,
3899 hash_parent_base=>$hash_parent,
3900 file_name=>$diff->{'to_file'},
3901 file_parent=>$from_path)},
3902 "diff" . ($i+1)) .
3903 " | </td>\n";
3907 print "<td class=\"link\">";
3908 if ($not_deleted) {
3909 print $cgi->a({-href => href(action=>"blob",
3910 hash=>$diff->{'to_id'},
3911 file_name=>$diff->{'to_file'},
3912 hash_base=>$hash)},
3913 "blob");
3914 print " | " if ($has_history);
3916 if ($has_history) {
3917 print $cgi->a({-href => href(action=>"history",
3918 file_name=>$diff->{'to_file'},
3919 hash_base=>$hash)},
3920 "history");
3922 print "</td>\n";
3924 print "</tr>\n";
3925 next; # instead of 'else' clause, to avoid extra indent
3927 # else ordinary diff
3929 my ($to_mode_oct, $to_mode_str, $to_file_type);
3930 my ($from_mode_oct, $from_mode_str, $from_file_type);
3931 if ($diff->{'to_mode'} ne ('0' x 6)) {
3932 $to_mode_oct = oct $diff->{'to_mode'};
3933 if (S_ISREG($to_mode_oct)) { # only for regular file
3934 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3936 $to_file_type = file_type($diff->{'to_mode'});
3938 if ($diff->{'from_mode'} ne ('0' x 6)) {
3939 $from_mode_oct = oct $diff->{'from_mode'};
3940 if (S_ISREG($to_mode_oct)) { # only for regular file
3941 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3943 $from_file_type = file_type($diff->{'from_mode'});
3946 if ($diff->{'status'} eq "A") { # created
3947 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3948 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3949 $mode_chng .= "]</span>";
3950 print "<td>";
3951 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3952 hash_base=>$hash, file_name=>$diff->{'file'}),
3953 -class => "list"}, esc_path($diff->{'file'}));
3954 print "</td>\n";
3955 print "<td>$mode_chng</td>\n";
3956 print "<td class=\"link\">";
3957 if ($action eq 'commitdiff') {
3958 # link to patch
3959 $patchno++;
3960 print $cgi->a({-href => "#patch$patchno"}, "patch");
3961 print " | ";
3963 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3964 hash_base=>$hash, file_name=>$diff->{'file'})},
3965 "blob");
3966 print "</td>\n";
3968 } elsif ($diff->{'status'} eq "D") { # deleted
3969 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3970 print "<td>";
3971 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3972 hash_base=>$parent, file_name=>$diff->{'file'}),
3973 -class => "list"}, esc_path($diff->{'file'}));
3974 print "</td>\n";
3975 print "<td>$mode_chng</td>\n";
3976 print "<td class=\"link\">";
3977 if ($action eq 'commitdiff') {
3978 # link to patch
3979 $patchno++;
3980 print $cgi->a({-href => "#patch$patchno"}, "patch");
3981 print " | ";
3983 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3984 hash_base=>$parent, file_name=>$diff->{'file'})},
3985 "blob") . " | ";
3986 if ($have_blame) {
3987 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3988 file_name=>$diff->{'file'})},
3989 "blame") . " | ";
3991 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3992 file_name=>$diff->{'file'})},
3993 "history");
3994 print "</td>\n";
3996 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3997 my $mode_chnge = "";
3998 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3999 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4000 if ($from_file_type ne $to_file_type) {
4001 $mode_chnge .= " from $from_file_type to $to_file_type";
4003 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4004 if ($from_mode_str && $to_mode_str) {
4005 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4006 } elsif ($to_mode_str) {
4007 $mode_chnge .= " mode: $to_mode_str";
4010 $mode_chnge .= "]</span>\n";
4012 print "<td>";
4013 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4014 hash_base=>$hash, file_name=>$diff->{'file'}),
4015 -class => "list"}, esc_path($diff->{'file'}));
4016 print "</td>\n";
4017 print "<td>$mode_chnge</td>\n";
4018 print "<td class=\"link\">";
4019 if ($action eq 'commitdiff') {
4020 # link to patch
4021 $patchno++;
4022 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4023 " | ";
4024 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4025 # "commit" view and modified file (not onlu mode changed)
4026 print $cgi->a({-href => href(action=>"blobdiff",
4027 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4028 hash_base=>$hash, hash_parent_base=>$parent,
4029 file_name=>$diff->{'file'})},
4030 "diff") .
4031 " | ";
4033 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4034 hash_base=>$hash, file_name=>$diff->{'file'})},
4035 "blob") . " | ";
4036 if ($have_blame) {
4037 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4038 file_name=>$diff->{'file'})},
4039 "blame") . " | ";
4041 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4042 file_name=>$diff->{'file'})},
4043 "history");
4044 print "</td>\n";
4046 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4047 my %status_name = ('R' => 'moved', 'C' => 'copied');
4048 my $nstatus = $status_name{$diff->{'status'}};
4049 my $mode_chng = "";
4050 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4051 # mode also for directories, so we cannot use $to_mode_str
4052 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4054 print "<td>" .
4055 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4056 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4057 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4058 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4059 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4060 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4061 -class => "list"}, esc_path($diff->{'from_file'})) .
4062 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4063 "<td class=\"link\">";
4064 if ($action eq 'commitdiff') {
4065 # link to patch
4066 $patchno++;
4067 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4068 " | ";
4069 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4070 # "commit" view and modified file (not only pure rename or copy)
4071 print $cgi->a({-href => href(action=>"blobdiff",
4072 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4073 hash_base=>$hash, hash_parent_base=>$parent,
4074 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4075 "diff") .
4076 " | ";
4078 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4079 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4080 "blob") . " | ";
4081 if ($have_blame) {
4082 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4083 file_name=>$diff->{'to_file'})},
4084 "blame") . " | ";
4086 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4087 file_name=>$diff->{'to_file'})},
4088 "history");
4089 print "</td>\n";
4091 } # we should not encounter Unmerged (U) or Unknown (X) status
4092 print "</tr>\n";
4094 print "</tbody>" if $has_header;
4095 print "</table>\n";
4098 sub git_patchset_body {
4099 my ($fd, $difftree, $hash, @hash_parents) = @_;
4100 my ($hash_parent) = $hash_parents[0];
4102 my $is_combined = (@hash_parents > 1);
4103 my $patch_idx = 0;
4104 my $patch_number = 0;
4105 my $patch_line;
4106 my $diffinfo;
4107 my $to_name;
4108 my (%from, %to);
4110 print "<div class=\"patchset\">\n";
4112 # skip to first patch
4113 while ($patch_line = <$fd>) {
4114 chomp $patch_line;
4116 last if ($patch_line =~ m/^diff /);
4119 PATCH:
4120 while ($patch_line) {
4122 # parse "git diff" header line
4123 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4124 # $1 is from_name, which we do not use
4125 $to_name = unquote($2);
4126 $to_name =~ s!^b/!!;
4127 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4128 # $1 is 'cc' or 'combined', which we do not use
4129 $to_name = unquote($2);
4130 } else {
4131 $to_name = undef;
4134 # check if current patch belong to current raw line
4135 # and parse raw git-diff line if needed
4136 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4137 # this is continuation of a split patch
4138 print "<div class=\"patch cont\">\n";
4139 } else {
4140 # advance raw git-diff output if needed
4141 $patch_idx++ if defined $diffinfo;
4143 # read and prepare patch information
4144 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4146 # compact combined diff output can have some patches skipped
4147 # find which patch (using pathname of result) we are at now;
4148 if ($is_combined) {
4149 while ($to_name ne $diffinfo->{'to_file'}) {
4150 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4151 format_diff_cc_simplified($diffinfo, @hash_parents) .
4152 "</div>\n"; # class="patch"
4154 $patch_idx++;
4155 $patch_number++;
4157 last if $patch_idx > $#$difftree;
4158 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4162 # modifies %from, %to hashes
4163 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4165 # this is first patch for raw difftree line with $patch_idx index
4166 # we index @$difftree array from 0, but number patches from 1
4167 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4170 # git diff header
4171 #assert($patch_line =~ m/^diff /) if DEBUG;
4172 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4173 $patch_number++;
4174 # print "git diff" header
4175 print format_git_diff_header_line($patch_line, $diffinfo,
4176 \%from, \%to);
4178 # print extended diff header
4179 print "<div class=\"diff extended_header\">\n";
4180 EXTENDED_HEADER:
4181 while ($patch_line = <$fd>) {
4182 chomp $patch_line;
4184 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4186 print format_extended_diff_header_line($patch_line, $diffinfo,
4187 \%from, \%to);
4189 print "</div>\n"; # class="diff extended_header"
4191 # from-file/to-file diff header
4192 if (! $patch_line) {
4193 print "</div>\n"; # class="patch"
4194 last PATCH;
4196 next PATCH if ($patch_line =~ m/^diff /);
4197 #assert($patch_line =~ m/^---/) if DEBUG;
4199 my $last_patch_line = $patch_line;
4200 $patch_line = <$fd>;
4201 chomp $patch_line;
4202 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4204 print format_diff_from_to_header($last_patch_line, $patch_line,
4205 $diffinfo, \%from, \%to,
4206 @hash_parents);
4208 # the patch itself
4209 LINE:
4210 while ($patch_line = <$fd>) {
4211 chomp $patch_line;
4213 next PATCH if ($patch_line =~ m/^diff /);
4215 print format_diff_line($patch_line, \%from, \%to);
4218 } continue {
4219 print "</div>\n"; # class="patch"
4222 # for compact combined (--cc) format, with chunk and patch simpliciaction
4223 # patchset might be empty, but there might be unprocessed raw lines
4224 for (++$patch_idx if $patch_number > 0;
4225 $patch_idx < @$difftree;
4226 ++$patch_idx) {
4227 # read and prepare patch information
4228 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4230 # generate anchor for "patch" links in difftree / whatchanged part
4231 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4232 format_diff_cc_simplified($diffinfo, @hash_parents) .
4233 "</div>\n"; # class="patch"
4235 $patch_number++;
4238 if ($patch_number == 0) {
4239 if (@hash_parents > 1) {
4240 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4241 } else {
4242 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4246 print "</div>\n"; # class="patchset"
4249 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4251 # fills project list info (age, description, owner, forks) for each
4252 # project in the list, removing invalid projects from returned list
4253 # NOTE: modifies $projlist, but does not remove entries from it
4254 sub fill_project_list_info {
4255 my ($projlist, $check_forks) = @_;
4256 my @projects;
4258 my $show_ctags = gitweb_check_feature('ctags');
4259 PROJECT:
4260 foreach my $pr (@$projlist) {
4261 my (@activity) = git_get_last_activity($pr->{'path'});
4262 unless (@activity) {
4263 next PROJECT;
4265 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4266 if (!defined $pr->{'descr'}) {
4267 my $descr = git_get_project_description($pr->{'path'}) || "";
4268 $descr = to_utf8($descr);
4269 $pr->{'descr_long'} = $descr;
4270 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4272 if (!defined $pr->{'owner'}) {
4273 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4275 if ($check_forks) {
4276 my $pname = $pr->{'path'};
4277 if (($pname =~ s/\.git$//) &&
4278 ($pname !~ /\/$/) &&
4279 (-d "$projectroot/$pname")) {
4280 $pr->{'forks'} = "-d $projectroot/$pname";
4281 } else {
4282 $pr->{'forks'} = 0;
4285 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4286 push @projects, $pr;
4289 return @projects;
4292 # print 'sort by' <th> element, generating 'sort by $name' replay link
4293 # if that order is not selected
4294 sub print_sort_th {
4295 my ($name, $order, $header) = @_;
4296 $header ||= ucfirst($name);
4298 if ($order eq $name) {
4299 print "<th>$header</th>\n";
4300 } else {
4301 print "<th>" .
4302 $cgi->a({-href => href(-replay=>1, order=>$name),
4303 -class => "header"}, $header) .
4304 "</th>\n";
4308 sub git_project_list_body {
4309 # actually uses global variable $project
4310 my ($projlist, $order, $from, $to, $extra, $no_header, $cache_lifetime) = @_;
4312 my $check_forks = gitweb_check_feature('forks');
4314 use File::stat;
4315 use POSIX qw(:fcntl_h);
4316 use Storable qw(store_fd retrieve);
4318 my $cache_file = "$cache_dir/$projlist_cache_name";
4320 my @projects;
4321 my $stale = 0;
4322 my $now = time();
4323 my $cache_mtime;
4324 if ($cache_lifetime && -f $cache_file) {
4325 $cache_mtime = stat($cache_file)->mtime;
4327 if (defined $cache_mtime && # caching is on and $cache_file exists
4328 $cache_mtime + $cache_lifetime*60 > $now &&
4329 (my $dump = retrieve($cache_file))) {
4330 $stale = $now - $cache_mtime;
4331 @projects = @$dump;
4332 } else {
4333 if (defined $cache_mtime) {
4334 # Postpone timeout by two minutes so that we get
4335 # enough time to do our job, or to be more exact
4336 # make cache expire after two minutes from now.
4337 my $time = $now - $cache_lifetime*60 + 120;
4338 utime $time, $time, $cache_file;
4340 @projects = fill_project_list_info($projlist, $check_forks);
4341 if ($cache_lifetime &&
4342 (-d $cache_dir || mkdir($cache_dir, 0700)) &&
4343 sysopen(my $fd, "$cache_file.lock", O_WRONLY|O_CREAT|O_EXCL, 0600)) {
4344 store_fd(\@projects, $fd);
4345 close $fd;
4346 rename "$cache_file.lock", $cache_file;
4350 $order ||= $default_projects_order;
4351 $from = 0 unless defined $from;
4352 $to = $#projects if (!defined $to || $#projects < $to);
4354 if ($cache_lifetime && $stale > 0) {
4355 print "<div class=\"stale_info\">Cached version (${stale}s old)</div>\n";
4358 my %order_info = (
4359 project => { key => 'path', type => 'str' },
4360 descr => { key => 'descr_long', type => 'str' },
4361 owner => { key => 'owner', type => 'str' },
4362 age => { key => 'age', type => 'num' }
4364 my $oi = $order_info{$order};
4365 if ($oi->{'type'} eq 'str') {
4366 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4367 } else {
4368 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4371 if ($cache_lifetime && $stale > 0) {
4372 print "<div class=\"stale_info\">Cached version (${stale}s old)</div>\n";
4375 my $show_ctags = gitweb_check_feature('ctags');
4376 if ($show_ctags) {
4377 my %ctags;
4378 foreach my $p (@projects) {
4379 foreach my $ct (keys %{$p->{'ctags'}}) {
4380 $ctags{$ct} += $p->{'ctags'}->{$ct};
4383 my $cloud = git_populate_project_tagcloud(\%ctags);
4384 print git_show_project_tagcloud($cloud, 64);
4387 print "<table class=\"project_list\">\n";
4388 unless ($no_header) {
4389 print "<tr>\n";
4390 if ($check_forks) {
4391 print "<th></th>\n";
4393 print_sort_th('project', $order, 'Project');
4394 print_sort_th('descr', $order, 'Description');
4395 print_sort_th('owner', $order, 'Owner');
4396 print_sort_th('age', $order, 'Last Change');
4397 print "<th></th>\n" . # for links
4398 "</tr>\n";
4400 my $alternate = 1;
4401 my $tagfilter = $cgi->param('by_tag');
4402 for (my $i = $from; $i <= $to; $i++) {
4403 my $pr = $projects[$i];
4405 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4406 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4407 and not $pr->{'descr_long'} =~ /$searchtext/;
4408 # Weed out forks or non-matching entries of search
4409 if ($check_forks) {
4410 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4411 $forkbase="^$forkbase" if $forkbase;
4412 next if not $searchtext and not $tagfilter and $show_ctags
4413 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4416 if ($alternate) {
4417 print "<tr class=\"dark\">\n";
4418 } else {
4419 print "<tr class=\"light\">\n";
4421 $alternate ^= 1;
4422 if ($check_forks) {
4423 print "<td>";
4424 if ($pr->{'forks'}) {
4425 print "<!-- $pr->{'forks'} -->\n";
4426 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4428 print "</td>\n";
4430 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4431 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4432 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4433 -class => "list", -title => $pr->{'descr_long'}},
4434 esc_html($pr->{'descr'})) . "</td>\n" .
4435 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4436 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4437 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4438 "<td class=\"link\">" .
4439 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4440 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "log") . " | " .
4441 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4442 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4443 "</td>\n" .
4444 "</tr>\n";
4446 if (defined $extra) {
4447 print "<tr>\n";
4448 if ($check_forks) {
4449 print "<td></td>\n";
4451 print "<td colspan=\"5\">$extra</td>\n" .
4452 "</tr>\n";
4454 print "</table>\n";
4457 sub git_shortlog_body {
4458 # uses global variable $project
4459 my ($commitlist, $from, $to, $refs, $extra) = @_;
4461 $from = 0 unless defined $from;
4462 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4464 print "<table class=\"shortlog\">\n";
4465 my $alternate = 1;
4466 for (my $i = $from; $i <= $to; $i++) {
4467 my %co = %{$commitlist->[$i]};
4468 my $commit = $co{'id'};
4469 my $ref = format_ref_marker($refs, $commit);
4470 if ($alternate) {
4471 print "<tr class=\"dark\">\n";
4472 } else {
4473 print "<tr class=\"light\">\n";
4475 $alternate ^= 1;
4476 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4477 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4478 format_author_html('td', \%co, 10) . "<td>";
4479 print format_subject_html($co{'title'}, $co{'title_short'},
4480 href(action=>"commit", hash=>$commit), $ref);
4481 print "</td>\n" .
4482 "<td class=\"link\">" .
4483 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4484 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4485 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4486 my $snapshot_links = format_snapshot_links($commit);
4487 if (defined $snapshot_links) {
4488 print " | " . $snapshot_links;
4490 print "</td>\n" .
4491 "</tr>\n";
4493 if (defined $extra) {
4494 print "<tr>\n" .
4495 "<td colspan=\"4\">$extra</td>\n" .
4496 "</tr>\n";
4498 print "</table>\n";
4501 sub git_history_body {
4502 # Warning: assumes constant type (blob or tree) during history
4503 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
4505 $from = 0 unless defined $from;
4506 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4508 print "<table class=\"history\">\n";
4509 my $alternate = 1;
4510 for (my $i = $from; $i <= $to; $i++) {
4511 my %co = %{$commitlist->[$i]};
4512 if (!%co) {
4513 next;
4515 my $commit = $co{'id'};
4517 my $ref = format_ref_marker($refs, $commit);
4519 if ($alternate) {
4520 print "<tr class=\"dark\">\n";
4521 } else {
4522 print "<tr class=\"light\">\n";
4524 $alternate ^= 1;
4525 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4526 # shortlog: format_author_html('td', \%co, 10)
4527 format_author_html('td', \%co, 15, 3) . "<td>";
4528 # originally git_history used chop_str($co{'title'}, 50)
4529 print format_subject_html($co{'title'}, $co{'title_short'},
4530 href(action=>"commit", hash=>$commit), $ref);
4531 print "</td>\n" .
4532 "<td class=\"link\">" .
4533 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4534 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4536 if ($ftype eq 'blob') {
4537 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
4538 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4539 if (defined $blob_current && defined $blob_parent &&
4540 $blob_current ne $blob_parent) {
4541 print " | " .
4542 $cgi->a({-href => href(action=>"blobdiff",
4543 hash=>$blob_current, hash_parent=>$blob_parent,
4544 hash_base=>$hash_base, hash_parent_base=>$commit,
4545 file_name=>$file_name)},
4546 "diff to current");
4549 print "</td>\n" .
4550 "</tr>\n";
4552 if (defined $extra) {
4553 print "<tr>\n" .
4554 "<td colspan=\"4\">$extra</td>\n" .
4555 "</tr>\n";
4557 print "</table>\n";
4560 sub git_tags_body {
4561 # uses global variable $project
4562 my ($taglist, $from, $to, $extra) = @_;
4563 $from = 0 unless defined $from;
4564 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4566 print "<table class=\"tags\">\n";
4567 my $alternate = 1;
4568 for (my $i = $from; $i <= $to; $i++) {
4569 my $entry = $taglist->[$i];
4570 my %tag = %$entry;
4571 my $comment = $tag{'subject'};
4572 my $comment_short;
4573 if (defined $comment) {
4574 $comment_short = chop_str($comment, 30, 5);
4576 if ($alternate) {
4577 print "<tr class=\"dark\">\n";
4578 } else {
4579 print "<tr class=\"light\">\n";
4581 $alternate ^= 1;
4582 if (defined $tag{'age'}) {
4583 print "<td><i>$tag{'age'}</i></td>\n";
4584 } else {
4585 print "<td></td>\n";
4587 print "<td>" .
4588 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4589 -class => "list name"}, esc_html($tag{'name'})) .
4590 "</td>\n" .
4591 "<td>";
4592 if (defined $comment) {
4593 print format_subject_html($comment, $comment_short,
4594 href(action=>"tag", hash=>$tag{'id'}));
4596 print "</td>\n" .
4597 "<td class=\"selflink\">";
4598 if ($tag{'type'} eq "tag") {
4599 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4600 } else {
4601 print "&nbsp;";
4603 print "</td>\n" .
4604 "<td class=\"link\">" . " | " .
4605 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4606 if ($tag{'reftype'} eq "commit") {
4607 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "log");
4608 } elsif ($tag{'reftype'} eq "blob") {
4609 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4611 print "</td>\n" .
4612 "</tr>";
4614 if (defined $extra) {
4615 print "<tr>\n" .
4616 "<td colspan=\"5\">$extra</td>\n" .
4617 "</tr>\n";
4619 print "</table>\n";
4622 sub git_heads_body {
4623 # uses global variable $project
4624 my ($headlist, $head, $from, $to, $extra) = @_;
4625 $from = 0 unless defined $from;
4626 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4628 print "<table class=\"heads\">\n";
4629 my $alternate = 1;
4630 for (my $i = $from; $i <= $to; $i++) {
4631 my $entry = $headlist->[$i];
4632 my %ref = %$entry;
4633 my $curr = $ref{'id'} eq $head;
4634 if ($alternate) {
4635 print "<tr class=\"dark\">\n";
4636 } else {
4637 print "<tr class=\"light\">\n";
4639 $alternate ^= 1;
4640 print "<td><i>$ref{'age'}</i></td>\n" .
4641 ($curr ? "<td class=\"current_head\">" : "<td>") .
4642 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4643 -class => "list name"},esc_html($ref{'name'})) .
4644 "</td>\n" .
4645 "<td class=\"link\">" .
4646 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "log") . " | " .
4647 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4648 "</td>\n" .
4649 "</tr>";
4651 if (defined $extra) {
4652 print "<tr>\n" .
4653 "<td colspan=\"3\">$extra</td>\n" .
4654 "</tr>\n";
4656 print "</table>\n";
4659 sub git_search_grep_body {
4660 my ($commitlist, $from, $to, $extra) = @_;
4661 $from = 0 unless defined $from;
4662 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4664 print "<table class=\"commit_search\">\n";
4665 my $alternate = 1;
4666 for (my $i = $from; $i <= $to; $i++) {
4667 my %co = %{$commitlist->[$i]};
4668 if (!%co) {
4669 next;
4671 my $commit = $co{'id'};
4672 if ($alternate) {
4673 print "<tr class=\"dark\">\n";
4674 } else {
4675 print "<tr class=\"light\">\n";
4677 $alternate ^= 1;
4678 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4679 format_author_html('td', \%co, 15, 5) .
4680 "<td>" .
4681 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4682 -class => "list subject"},
4683 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4684 my $comment = $co{'comment'};
4685 foreach my $line (@$comment) {
4686 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4687 my ($lead, $match, $trail) = ($1, $2, $3);
4688 $match = chop_str($match, 70, 5, 'center');
4689 my $contextlen = int((80 - length($match))/2);
4690 $contextlen = 30 if ($contextlen > 30);
4691 $lead = chop_str($lead, $contextlen, 10, 'left');
4692 $trail = chop_str($trail, $contextlen, 10, 'right');
4694 $lead = esc_html($lead);
4695 $match = esc_html($match);
4696 $trail = esc_html($trail);
4698 print "$lead<span class=\"match\">$match</span>$trail<br />";
4701 print "</td>\n" .
4702 "<td class=\"link\">" .
4703 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4704 " | " .
4705 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4706 " | " .
4707 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4708 print "</td>\n" .
4709 "</tr>\n";
4711 if (defined $extra) {
4712 print "<tr>\n" .
4713 "<td colspan=\"3\">$extra</td>\n" .
4714 "</tr>\n";
4716 print "</table>\n";
4719 ## ======================================================================
4720 ## ======================================================================
4721 ## actions
4723 sub git_project_list {
4724 my $order = $input_params{'order'};
4725 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4726 die_error(400, "Unknown order parameter");
4729 my @list = git_get_projects_list();
4730 if (!@list) {
4731 die_error(404, "No projects found");
4734 git_header_html();
4735 if (-f $home_text) {
4736 print "<div class=\"index_include\">\n";
4737 insert_file($home_text);
4738 print "</div>\n";
4740 print $cgi->startform(-method => "get") .
4741 "<p class=\"projsearch\">Search:\n" .
4742 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4743 "</p>" .
4744 $cgi->end_form() . "\n";
4745 git_project_list_body(\@list, $order, undef, undef, undef, undef, $projlist_cache_lifetime);
4746 git_footer_html();
4749 sub git_forks {
4750 my $order = $input_params{'order'};
4751 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4752 die_error(400, "Unknown order parameter");
4755 my @list = git_get_projects_list($project);
4756 if (!@list) {
4757 die_error(404, "No forks found");
4760 git_header_html();
4761 git_print_page_nav('','');
4762 git_print_header_div('summary', "$project forks");
4763 git_project_list_body(\@list, $order);
4764 git_footer_html();
4767 sub git_project_index {
4768 my @projects = git_get_projects_list($project);
4770 print $cgi->header(
4771 -type => 'text/plain',
4772 -charset => 'utf-8',
4773 -content_disposition => 'inline; filename="index.aux"');
4775 foreach my $pr (@projects) {
4776 if (!exists $pr->{'owner'}) {
4777 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4780 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4781 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4782 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4783 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4784 $path =~ s/ /\+/g;
4785 $owner =~ s/ /\+/g;
4787 print "$path $owner\n";
4791 sub git_summary {
4792 my $descr = git_get_project_description($project) || "none";
4793 my %co = parse_commit("HEAD");
4794 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4795 my $head = $co{'id'};
4797 my $owner = git_get_project_owner($project);
4799 my $refs = git_get_references();
4800 # These get_*_list functions return one more to allow us to see if
4801 # there are more ...
4802 my @taglist = git_get_tags_list(16);
4803 my @headlist = git_get_heads_list(16);
4804 my @forklist;
4805 my $check_forks = gitweb_check_feature('forks');
4807 if ($check_forks) {
4808 @forklist = git_get_projects_list($project);
4811 git_header_html();
4812 git_print_page_nav('summary','', $head);
4814 print "<div class=\"title\">&nbsp;</div>\n";
4815 print "<table class=\"projects_list\">\n" .
4816 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4817 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4818 if (defined $cd{'rfc2822'}) {
4819 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4822 # use per project git URL list in $projectroot/$project/cloneurl
4823 # or make project git URL from git base URL and project name
4824 my $url_tag = "URL";
4825 my @url_list = git_get_project_url_list($project);
4826 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4827 foreach my $git_url (@url_list) {
4828 next unless $git_url;
4829 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4830 $url_tag = "";
4833 # Tag cloud
4834 my $show_ctags = gitweb_check_feature('ctags');
4835 if ($show_ctags) {
4836 my $ctags = git_get_project_ctags($project);
4837 my $cloud = git_populate_project_tagcloud($ctags);
4838 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4839 print "</td>\n<td>" unless %$ctags;
4840 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4841 print "</td>\n<td>" if %$ctags;
4842 print git_show_project_tagcloud($cloud, 48);
4843 print "</td></tr>";
4846 print "</table>\n";
4848 # If XSS prevention is on, we don't include README.html.
4849 # TODO: Allow a readme in some safe format.
4850 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
4851 print "<div class=\"title\">readme</div>\n" .
4852 "<div class=\"readme\">\n";
4853 insert_file("$projectroot/$project/README.html");
4854 print "\n</div>\n"; # class="readme"
4857 # we need to request one more than 16 (0..15) to check if
4858 # those 16 are all
4859 my @commitlist = $head ? parse_commits($head, 17) : ();
4860 if (@commitlist) {
4861 git_print_header_div('shortlog');
4862 git_shortlog_body(\@commitlist, 0, 15, $refs,
4863 $#commitlist <= 15 ? undef :
4864 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4867 if (@taglist) {
4868 git_print_header_div('tags');
4869 git_tags_body(\@taglist, 0, 15,
4870 $#taglist <= 15 ? undef :
4871 $cgi->a({-href => href(action=>"tags")}, "..."));
4874 if (@headlist) {
4875 git_print_header_div('heads');
4876 git_heads_body(\@headlist, $head, 0, 15,
4877 $#headlist <= 15 ? undef :
4878 $cgi->a({-href => href(action=>"heads")}, "..."));
4881 if (@forklist) {
4882 git_print_header_div('forks');
4883 git_project_list_body(\@forklist, 'age', 0, 15,
4884 $#forklist <= 15 ? undef :
4885 $cgi->a({-href => href(action=>"forks")}, "..."),
4886 'no_header');
4889 git_footer_html();
4892 sub git_tag {
4893 my $head = git_get_head_hash($project);
4894 git_header_html();
4895 git_print_page_nav('','', $head,undef,$head);
4896 my %tag = parse_tag($hash);
4898 if (! %tag) {
4899 die_error(404, "Unknown tag object");
4902 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4903 print "<div class=\"title_text\">\n" .
4904 "<table class=\"object_header\">\n" .
4905 "<tr>\n" .
4906 "<td>object</td>\n" .
4907 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4908 $tag{'object'}) . "</td>\n" .
4909 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4910 $tag{'type'}) . "</td>\n" .
4911 "</tr>\n";
4912 if (defined($tag{'author'})) {
4913 git_print_authorship_rows(\%tag, 'author');
4915 print "</table>\n\n" .
4916 "</div>\n";
4917 print "<div class=\"page_body\">";
4918 my $comment = $tag{'comment'};
4919 foreach my $line (@$comment) {
4920 chomp $line;
4921 print esc_html($line, -nbsp=>1) . "<br/>\n";
4923 print "</div>\n";
4924 git_footer_html();
4927 sub git_blame_data {
4928 my $ftype;
4930 my ($have_blame) = gitweb_check_feature('blame');
4931 if (!$have_blame) {
4932 die_error('403 Permission denied', "Permission denied");
4934 die_error('404 Not Found', "File name not defined") if (!$file_name);
4935 $hash_base ||= git_get_head_hash($project);
4936 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4937 my %co = parse_commit($hash_base)
4938 or die_error(undef, "Reading commit failed");
4939 if (!defined $hash) {
4940 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4941 or die_error(undef, "Error looking up file");
4943 $ftype = git_get_type($hash);
4944 if ($ftype !~ "blob") {
4945 die_error("400 Bad Request", "Object is not a blob");
4947 open my $fd, "-|", git_cmd(), "blame", '--incremental',
4948 $hash_base, '--', $file_name
4949 or die_error(undef, "Open git-blame --incremental failed");
4951 print $cgi->header(-type=>"text/plain", -charset => 'utf-8',
4952 -status=> "200 OK");
4954 while(<$fd>) {
4955 if (/^([0-9a-f]{40}) ([0-9]+) ([0-9]+) ([0-9]+)/ or
4956 /^author-time |^author |^filename /) {
4957 print;
4961 close $fd or print "Reading blame data failed\n";
4964 sub git_blame_common {
4965 my ($type) = @_;
4967 my $ftype;
4969 # permissions
4970 gitweb_check_feature('blame')
4971 or die_error(403, "Blame view not allowed");
4973 # error checking
4974 die_error(400, "No file name given") unless $file_name;
4975 $hash_base ||= git_get_head_hash($project);
4976 die_error(404, "Couldn't find base commit") unless $hash_base;
4977 my %co = parse_commit($hash_base)
4978 or die_error(404, "Commit not found");
4979 my $ftype = "blob";
4980 if (!defined $hash) {
4981 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4982 or die_error(404, "Error looking up file");
4983 } else {
4984 $ftype = git_get_type($hash);
4985 if ($ftype !~ "blob") {
4986 die_error(400, "Object is not a blob");
4989 $ftype = git_get_type($hash);
4990 if ($ftype !~ "blob") {
4991 die_error(400, "Object is not a blob");
4993 if ($type eq 'incremental') {
4994 open my $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
4995 or die_error(undef, "Open git-cat-file failed");
4996 } else {
4997 # run git-blame --porcelain
4998 open my $fd, "-|", git_cmd(), "blame", '-p',
4999 $hash_base, '--', $file_name
5000 or die_error(500, "Open git-blame failed");
5003 # page header
5004 git_header_html();
5005 my $formats_nav =
5006 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5007 "blob") .
5008 " | " .
5009 $cgi->a({-href => href(action=>"history", -replay=>1)},
5010 "history") .
5011 " | " .
5012 $cgi->a({-href => href(action=>"blame", file_name=>$file_name), -class => "blamelink"},
5013 "HEAD");
5014 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5015 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5016 git_print_page_path($file_name, $ftype, $hash_base);
5018 # page body
5019 my @rev_color = qw(light dark);
5020 my $num_colors = scalar(@rev_color);
5021 my $current_color = 0;
5022 my %metainfo = ();
5024 print <<HTML;
5026 <div class="page_body">
5027 <table class="blame">
5028 <tr><th>Commit&nbsp;<a href="javascript:extra_blame_columns()" id="columns_expander">[+]</a></th>
5029 <th class="extra_column">Author</th>
5030 <th class="extra_column">Date</th>
5031 <th>Line</th>
5032 <th>Data</th></tr>
5033 HTML
5034 LINE:
5035 my $linenr = 0;
5036 while (my $line = <$fd>) {
5037 chomp $line;
5038 if ($type eq 'incremental') {
5039 # Empty stage with just the file contents
5040 $linenr += 1;
5041 print "<tr id=\"l$linenr\" class=\"light2\">";
5042 print '<td class="sha1"><a href=""></a></td>';
5043 print "<td class=\"extra_column\"></td>";
5044 print "<td class=\"extra_column\"></td>";
5045 print "<td class=\"linenr\"><a class=\"linenr\" href=\"\">$linenr</a></td><td class=\"pre\">" . esc_html($line) . "</td>\n";
5046 print "</tr>\n";
5047 next;
5050 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5051 # no <lines in group> for subsequent lines in group of lines
5052 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5053 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5054 if (!exists $metainfo{$full_rev}) {
5055 $metainfo{$full_rev} = { 'nprevious' => 0 };
5057 my $meta = $metainfo{$full_rev};
5058 my $data;
5059 while ($data = <$fd>) {
5060 chomp $data;
5061 last if ($data =~ s/^\t//); # contents of line
5062 if ($data =~ /^(\S+)(?: (.*))?$/) {
5063 $meta->{$1} = $2 unless exists $meta->{$1};
5065 if ($data =~ /^previous /) {
5066 $meta->{'nprevious'}++;
5069 my $short_rev = substr($full_rev, 0, 8);
5070 my $author = $meta->{'author'};
5071 my %date =
5072 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5073 my $date = $date{'iso-tz'};
5074 if ($group_size) {
5075 $current_color = ($current_color + 1) % $num_colors;
5077 my $tr_class = $rev_color[$current_color];
5078 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5079 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5080 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5081 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5082 if ($group_size) {
5083 my $rowspan = $group_size > 1 ? " rowspan=\"$group_size\"" : "";
5084 print "<td class=\"sha1\"";
5085 print " title=\"". esc_html($author) . ", $date\"";
5086 print "$rowspan>";
5087 print $cgi->a({-href => href(action=>"commit",
5088 hash=>$full_rev,
5089 file_name=>$file_name)},
5090 esc_html($short_rev));
5091 if ($group_size >= 2) {
5092 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5093 if (@author_initials) {
5094 print "<br />" .
5095 esc_html(join('', @author_initials));
5096 # or join('.', ...)
5099 print "</td>\n";
5100 print "<td class=\"extra_column\" $rowspan>". esc_html($author) . "</td>";
5101 print "<td class=\"extra_column\" $rowspan>". $date . "</td>";
5103 # 'previous' <sha1 of parent commit> <filename at commit>
5104 if (exists $meta->{'previous'} &&
5105 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5106 $meta->{'parent'} = $1;
5107 $meta->{'file_parent'} = unquote($2);
5109 my $linenr_commit =
5110 exists($meta->{'parent'}) ?
5111 $meta->{'parent'} : $full_rev;
5112 my $linenr_filename =
5113 exists($meta->{'file_parent'}) ?
5114 $meta->{'file_parent'} : unquote($meta->{'filename'});
5115 my $blamed = href(action => 'blame',
5116 file_name => $linenr_filename,
5117 hash_base => $linenr_commit);
5118 print "<td class=\"linenr\">";
5119 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5120 -class => "linenr" },
5121 esc_html($lineno));
5122 print "</td>";
5123 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5124 print "</tr>\n";
5127 print "</table>\n";
5128 print "</div>";
5129 close $fd
5130 or print "Reading blob failed\n";
5132 if ($type eq 'incremental') {
5133 print "<script type=\"text/javascript\">\n";
5134 print "startBlame(\"" . href(action=>"blame_data", hash_base=>$hash_base, file_name=>$file_name) . "\", \"" .
5135 href(-partial_query=>1) . "\");\n";
5136 print "</script>\n";
5139 # page footer
5140 git_footer_html();
5143 sub git_blame_incremental {
5144 git_blame_common('incremental');
5147 sub git_blame {
5148 git_blame_common('oneshot');
5151 sub git_tags {
5152 my $head = git_get_head_hash($project);
5153 git_header_html();
5154 git_print_page_nav('','', $head,undef,$head);
5155 git_print_header_div('summary', $project);
5157 my @tagslist = git_get_tags_list();
5158 if (@tagslist) {
5159 git_tags_body(\@tagslist);
5161 git_footer_html();
5164 sub git_heads {
5165 my $head = git_get_head_hash($project);
5166 git_header_html();
5167 git_print_page_nav('','', $head,undef,$head);
5168 git_print_header_div('summary', $project);
5170 my @headslist = git_get_heads_list();
5171 if (@headslist) {
5172 git_heads_body(\@headslist, $head);
5174 git_footer_html();
5177 sub git_blob_plain {
5178 my $type = shift;
5179 my $expires;
5181 if (!defined $hash) {
5182 if (defined $file_name) {
5183 my $base = $hash_base || git_get_head_hash($project);
5184 $hash = git_get_hash_by_path($base, $file_name, "blob")
5185 or die_error(404, "Cannot find file");
5186 } else {
5187 die_error(400, "No file name defined");
5189 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5190 # blobs defined by non-textual hash id's can be cached
5191 $expires = "+1d";
5194 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5195 or die_error(500, "Open git-cat-file blob '$hash' failed");
5197 # content-type (can include charset)
5198 $type = blob_contenttype($fd, $file_name, $type);
5200 # "save as" filename, even when no $file_name is given
5201 my $save_as = "$hash";
5202 if (defined $file_name) {
5203 $save_as = $file_name;
5204 } elsif ($type =~ m/^text\//) {
5205 $save_as .= '.txt';
5208 # With XSS prevention on, blobs of all types except a few known safe
5209 # ones are served with "Content-Disposition: attachment" to make sure
5210 # they don't run in our security domain. For certain image types,
5211 # blob view writes an <img> tag referring to blob_plain view, and we
5212 # want to be sure not to break that by serving the image as an
5213 # attachment (though Firefox 3 doesn't seem to care).
5214 my $sandbox = $prevent_xss &&
5215 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5217 print $cgi->header(
5218 -type => $type,
5219 -expires => $expires,
5220 -content_disposition =>
5221 ($sandbox ? 'attachment' : 'inline')
5222 . '; filename="' . $save_as . '"');
5223 local $/ = undef;
5224 binmode STDOUT, ':raw';
5225 print <$fd>;
5226 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5227 close $fd;
5230 sub git_blob {
5231 my $expires;
5233 if (!defined $hash) {
5234 if (defined $file_name) {
5235 my $base = $hash_base || git_get_head_hash($project);
5236 $hash = git_get_hash_by_path($base, $file_name, "blob")
5237 or die_error(404, "Cannot find file");
5238 } else {
5239 die_error(400, "No file name defined");
5241 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5242 # blobs defined by non-textual hash id's can be cached
5243 $expires = "+1d";
5246 my $have_blame = gitweb_check_feature('blame');
5247 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5248 or die_error(500, "Couldn't cat $file_name, $hash");
5249 my $mimetype = blob_mimetype($fd, $file_name);
5250 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5251 close $fd;
5252 return git_blob_plain($mimetype);
5254 # we can have blame only for text/* mimetype
5255 $have_blame &&= ($mimetype =~ m!^text/!);
5257 git_header_html(undef, $expires);
5258 my $formats_nav = '';
5259 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5260 if (defined $file_name) {
5261 if ($have_blame) {
5262 $formats_nav .=
5263 $cgi->a({-href => href(action=>"blame", -replay=>1,
5264 -class => "blamelink")},
5265 "blame") .
5266 " | ";
5268 $formats_nav .=
5269 $cgi->a({-href => href(action=>"history", -replay=>1)},
5270 "history") .
5271 " | " .
5272 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5273 "raw") .
5274 " | " .
5275 $cgi->a({-href => href(action=>"blob",
5276 hash_base=>"HEAD", file_name=>$file_name)},
5277 "HEAD");
5278 } else {
5279 $formats_nav .=
5280 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5281 "raw");
5283 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5284 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5285 } else {
5286 print "<div class=\"page_nav\">\n" .
5287 "<br/><br/></div>\n" .
5288 "<div class=\"title\">$hash</div>\n";
5290 git_print_page_path($file_name, "blob", $hash_base);
5291 print "<div class=\"page_body\">\n";
5292 if ($mimetype =~ m!^image/!) {
5293 print qq!<img type="$mimetype"!;
5294 if ($file_name) {
5295 print qq! alt="$file_name" title="$file_name"!;
5297 print qq! src="! .
5298 href(action=>"blob_plain", hash=>$hash,
5299 hash_base=>$hash_base, file_name=>$file_name) .
5300 qq!" />\n!;
5301 } else {
5302 my $nr;
5303 while (my $line = <$fd>) {
5304 chomp $line;
5305 $nr++;
5306 $line = untabify($line);
5307 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
5308 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
5311 close $fd
5312 or print "Reading blob failed.\n";
5313 print "</div>";
5314 git_footer_html();
5317 sub git_tree {
5318 if (!defined $hash_base) {
5319 $hash_base = "HEAD";
5321 if (!defined $hash) {
5322 if (defined $file_name) {
5323 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5324 } else {
5325 $hash = $hash_base;
5328 die_error(404, "No such tree") unless defined($hash);
5330 my $show_sizes = gitweb_check_feature('show-sizes');
5331 my $have_blame = gitweb_check_feature('blame');
5333 my @entries = ();
5335 local $/ = "\0";
5336 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5337 ($show_sizes ? '-l' : ()), @extra_options, $hash
5338 or die_error(500, "Open git-ls-tree failed");
5339 @entries = map { chomp; $_ } <$fd>;
5340 close $fd
5341 or die_error(404, "Reading tree failed");
5344 my $refs = git_get_references();
5345 my $ref = format_ref_marker($refs, $hash_base);
5346 git_header_html();
5347 my $basedir = '';
5348 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5349 my @views_nav = ();
5350 if (defined $file_name) {
5351 push @views_nav,
5352 $cgi->a({-href => href(action=>"history", -replay=>1)},
5353 "history"),
5354 $cgi->a({-href => href(action=>"tree",
5355 hash_base=>"HEAD", file_name=>$file_name)},
5356 "HEAD"),
5358 my $snapshot_links = format_snapshot_links($hash);
5359 if (defined $snapshot_links) {
5360 # FIXME: Should be available when we have no hash base as well.
5361 push @views_nav, $snapshot_links;
5363 git_print_page_nav('tree','', $hash_base, undef, undef,
5364 join(' | ', @views_nav));
5365 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5366 } else {
5367 undef $hash_base;
5368 print "<div class=\"page_nav\">\n";
5369 print "<br/><br/></div>\n";
5370 print "<div class=\"title\">$hash</div>\n";
5372 if (defined $file_name) {
5373 $basedir = $file_name;
5374 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5375 $basedir .= '/';
5377 git_print_page_path($file_name, 'tree', $hash_base);
5379 print "<div class=\"page_body\">\n";
5380 print "<table class=\"tree\">\n";
5381 my $alternate = 1;
5382 # '..' (top directory) link if possible
5383 if (defined $hash_base &&
5384 defined $file_name && $file_name =~ m![^/]+$!) {
5385 if ($alternate) {
5386 print "<tr class=\"dark\">\n";
5387 } else {
5388 print "<tr class=\"light\">\n";
5390 $alternate ^= 1;
5392 my $up = $file_name;
5393 $up =~ s!/?[^/]+$!!;
5394 undef $up unless $up;
5395 # based on git_print_tree_entry
5396 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5397 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
5398 print '<td class="list">';
5399 print $cgi->a({-href => href(action=>"tree",
5400 hash_base=>$hash_base,
5401 file_name=>$up)},
5402 "..");
5403 print "</td>\n";
5404 print "<td class=\"link\"></td>\n";
5406 print "</tr>\n";
5408 foreach my $line (@entries) {
5409 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
5411 if ($alternate) {
5412 print "<tr class=\"dark\">\n";
5413 } else {
5414 print "<tr class=\"light\">\n";
5416 $alternate ^= 1;
5418 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
5420 print "</tr>\n";
5422 print "</table>\n" .
5423 "</div>";
5424 git_footer_html();
5427 sub git_snapshot {
5428 my $format = $input_params{'snapshot_format'};
5429 if (!@snapshot_fmts) {
5430 die_error(403, "Snapshots not allowed");
5432 # default to first supported snapshot format
5433 $format ||= $snapshot_fmts[0];
5434 if ($format !~ m/^[a-z0-9]+$/) {
5435 die_error(400, "Invalid snapshot format parameter");
5436 } elsif (!exists($known_snapshot_formats{$format})) {
5437 die_error(400, "Unknown snapshot format");
5438 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
5439 die_error(403, "Snapshot format not allowed");
5440 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5441 die_error(403, "Unsupported snapshot format");
5444 if (!defined $hash) {
5445 $hash = git_get_head_hash($project);
5448 my $name = $project;
5449 $name =~ s,([^/])/*\.git$,$1,;
5450 $name = basename($name);
5451 my $filename = to_utf8($name);
5452 $name =~ s/\047/\047\\\047\047/g;
5453 my $cmd;
5454 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
5455 $cmd = quote_command(
5456 git_cmd(), 'archive',
5457 "--format=$known_snapshot_formats{$format}{'format'}",
5458 "--prefix=$name/", $hash);
5459 if (exists $known_snapshot_formats{$format}{'compressor'}) {
5460 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
5463 print $cgi->header(
5464 -type => $known_snapshot_formats{$format}{'type'},
5465 -content_disposition => 'inline; filename="' . "$filename" . '"',
5466 -status => '200 OK');
5468 open my $fd, "-|", $cmd
5469 or die_error(500, "Execute git-archive failed");
5470 binmode STDOUT, ':raw';
5471 print <$fd>;
5472 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5473 close $fd;
5476 sub git_log {
5477 my $head = git_get_head_hash($project);
5478 if (!defined $hash) {
5479 $hash = $head;
5481 if (!defined $page) {
5482 $page = 0;
5484 my $refs = git_get_references();
5486 my @commitlist = parse_commits($hash, 101, (100 * $page));
5488 my $paging_nav = format_log_nav('log', $hash, $head, $page, $#commitlist >= 100);
5490 my ($patch_max) = gitweb_get_feature('patches');
5491 if ($patch_max) {
5492 if ($patch_max < 0 || @commitlist <= $patch_max) {
5493 $paging_nav .= " &sdot; " .
5494 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5495 "patches");
5500 local $action = 'fulllog';
5501 git_header_html();
5503 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
5505 if (!@commitlist) {
5506 my %co = parse_commit($hash);
5508 git_print_header_div('summary', $project);
5509 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
5511 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
5512 for (my $i = 0; $i <= $to; $i++) {
5513 my %co = %{$commitlist[$i]};
5514 next if !%co;
5515 my $commit = $co{'id'};
5516 my $ref = format_ref_marker($refs, $commit);
5517 my %ad = parse_date($co{'author_epoch'});
5518 git_print_header_div('commit',
5519 "<span class=\"age\">$co{'age_string'}</span>" .
5520 esc_html($co{'title'}) . $ref,
5521 $commit);
5522 print "<div class=\"title_text\">\n" .
5523 "<div class=\"log_link\">\n" .
5524 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5525 " | " .
5526 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5527 " | " .
5528 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5529 "<br/>\n" .
5530 "</div>\n";
5531 git_print_authorship(\%co, -tag => 'span');
5532 print "<br/>\n</div>\n";
5534 print "<div class=\"log_body\">\n";
5535 git_print_log($co{'comment'}, -final_empty_line=> 1);
5536 print "</div>\n";
5538 if ($#commitlist >= 100) {
5539 print "<div class=\"page_nav\">\n";
5540 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
5541 -accesskey => "n", -title => "Alt-n"}, "next");
5542 print "</div>\n";
5544 git_footer_html();
5547 sub git_commit {
5548 $hash ||= $hash_base || "HEAD";
5549 my %co = parse_commit($hash)
5550 or die_error(404, "Unknown commit object");
5552 my $parent = $co{'parent'};
5553 my $parents = $co{'parents'}; # listref
5555 # we need to prepare $formats_nav before any parameter munging
5556 my $formats_nav;
5557 if (!defined $parent) {
5558 # --root commitdiff
5559 $formats_nav .= '(initial)';
5560 } elsif (@$parents == 1) {
5561 # single parent commit
5562 $formats_nav .=
5563 '(parent: ' .
5564 $cgi->a({-href => href(action=>"commit",
5565 hash=>$parent)},
5566 esc_html(substr($parent, 0, 7))) .
5567 ')';
5568 } else {
5569 # merge commit
5570 $formats_nav .=
5571 '(merge: ' .
5572 join(' ', map {
5573 $cgi->a({-href => href(action=>"commit",
5574 hash=>$_)},
5575 esc_html(substr($_, 0, 7)));
5576 } @$parents ) .
5577 ')';
5579 if (gitweb_check_feature('patches') && @$parents <= 1) {
5580 $formats_nav .= " | " .
5581 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5582 "patch");
5585 if (!defined $parent) {
5586 $parent = "--root";
5588 my @difftree;
5589 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5590 @diff_opts,
5591 (@$parents <= 1 ? $parent : '-c'),
5592 $hash, "--"
5593 or die_error(500, "Open git-diff-tree failed");
5594 @difftree = map { chomp; $_ } <$fd>;
5595 close $fd or die_error(404, "Reading git-diff-tree failed");
5597 # non-textual hash id's can be cached
5598 my $expires;
5599 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5600 $expires = "+1d";
5602 my $refs = git_get_references();
5603 my $ref = format_ref_marker($refs, $co{'id'});
5605 git_header_html(undef, $expires);
5606 git_print_page_nav('commit', '',
5607 $hash, $co{'tree'}, $hash,
5608 $formats_nav);
5610 if (defined $co{'parent'}) {
5611 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5612 } else {
5613 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5615 print "<div class=\"title_text\">\n" .
5616 "<table class=\"object_header\">\n";
5617 git_print_authorship_rows(\%co);
5618 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5619 print "<tr>" .
5620 "<td>tree</td>" .
5621 "<td class=\"sha1\">" .
5622 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5623 class => "list"}, $co{'tree'}) .
5624 "</td>" .
5625 "<td class=\"link\">" .
5626 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5627 "tree");
5628 my $snapshot_links = format_snapshot_links($hash);
5629 if (defined $snapshot_links) {
5630 print " | " . $snapshot_links;
5632 print "</td>" .
5633 "</tr>\n";
5635 foreach my $par (@$parents) {
5636 print "<tr>" .
5637 "<td>parent</td>" .
5638 "<td class=\"sha1\">" .
5639 $cgi->a({-href => href(action=>"commit", hash=>$par),
5640 class => "list"}, $par) .
5641 "</td>" .
5642 "<td class=\"link\">" .
5643 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
5644 " | " .
5645 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
5646 "</td>" .
5647 "</tr>\n";
5649 print "</table>".
5650 "</div>\n";
5652 print "<div class=\"page_body\">\n";
5653 git_print_log($co{'comment'});
5654 print "</div>\n";
5656 git_difftree_body(\@difftree, $hash, @$parents);
5658 git_footer_html();
5661 sub git_object {
5662 # object is defined by:
5663 # - hash or hash_base alone
5664 # - hash_base and file_name
5665 my $type;
5667 # - hash or hash_base alone
5668 if ($hash || ($hash_base && !defined $file_name)) {
5669 my $object_id = $hash || $hash_base;
5671 open my $fd, "-|", quote_command(
5672 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
5673 or die_error(404, "Object does not exist");
5674 $type = <$fd>;
5675 chomp $type;
5676 close $fd
5677 or die_error(404, "Object does not exist");
5679 # - hash_base and file_name
5680 } elsif ($hash_base && defined $file_name) {
5681 $file_name =~ s,/+$,,;
5683 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
5684 or die_error(404, "Base object does not exist");
5686 # here errors should not hapen
5687 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
5688 or die_error(500, "Open git-ls-tree failed");
5689 my $line = <$fd>;
5690 close $fd;
5692 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5693 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
5694 die_error(404, "File or directory for given base does not exist");
5696 $type = $2;
5697 $hash = $3;
5698 } else {
5699 die_error(400, "Not enough information to find object");
5702 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5703 hash=>$hash, hash_base=>$hash_base,
5704 file_name=>$file_name),
5705 -status => '302 Found');
5708 sub git_blobdiff {
5709 my $format = shift || 'html';
5711 my $fd;
5712 my @difftree;
5713 my %diffinfo;
5714 my $expires;
5716 # preparing $fd and %diffinfo for git_patchset_body
5717 # new style URI
5718 if (defined $hash_base && defined $hash_parent_base) {
5719 if (defined $file_name) {
5720 # read raw output
5721 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5722 $hash_parent_base, $hash_base,
5723 "--", (defined $file_parent ? $file_parent : ()), $file_name
5724 or die_error(500, "Open git-diff-tree failed");
5725 @difftree = map { chomp; $_ } <$fd>;
5726 close $fd
5727 or die_error(404, "Reading git-diff-tree failed");
5728 @difftree
5729 or die_error(404, "Blob diff not found");
5731 } elsif (defined $hash &&
5732 $hash =~ /[0-9a-fA-F]{40}/) {
5733 # try to find filename from $hash
5735 # read filtered raw output
5736 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5737 $hash_parent_base, $hash_base, "--"
5738 or die_error(500, "Open git-diff-tree failed");
5739 @difftree =
5740 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5741 # $hash == to_id
5742 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5743 map { chomp; $_ } <$fd>;
5744 close $fd
5745 or die_error(404, "Reading git-diff-tree failed");
5746 @difftree
5747 or die_error(404, "Blob diff not found");
5749 } else {
5750 die_error(400, "Missing one of the blob diff parameters");
5753 if (@difftree > 1) {
5754 die_error(400, "Ambiguous blob diff specification");
5757 %diffinfo = parse_difftree_raw_line($difftree[0]);
5758 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5759 $file_name ||= $diffinfo{'to_file'};
5761 $hash_parent ||= $diffinfo{'from_id'};
5762 $hash ||= $diffinfo{'to_id'};
5764 # non-textual hash id's can be cached
5765 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5766 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5767 $expires = '+1d';
5770 # open patch output
5771 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5772 '-p', ($format eq 'html' ? "--full-index" : ()),
5773 $hash_parent_base, $hash_base,
5774 "--", (defined $file_parent ? $file_parent : ()), $file_name
5775 or die_error(500, "Open git-diff-tree failed");
5778 # old/legacy style URI -- not generated anymore since 1.4.3.
5779 if (!%diffinfo) {
5780 die_error('404 Not Found', "Missing one of the blob diff parameters")
5783 # header
5784 if ($format eq 'html') {
5785 my $formats_nav =
5786 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5787 "raw");
5788 git_header_html(undef, $expires);
5789 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5790 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5791 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5792 } else {
5793 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5794 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5796 if (defined $file_name) {
5797 git_print_page_path($file_name, "blob", $hash_base);
5798 } else {
5799 print "<div class=\"page_path\"></div>\n";
5802 } elsif ($format eq 'plain') {
5803 print $cgi->header(
5804 -type => 'text/plain',
5805 -charset => 'utf-8',
5806 -expires => $expires,
5807 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5809 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5811 } else {
5812 die_error(400, "Unknown blobdiff format");
5815 # patch
5816 if ($format eq 'html') {
5817 print "<div class=\"page_body\">\n";
5819 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5820 close $fd;
5822 print "</div>\n"; # class="page_body"
5823 git_footer_html();
5825 } else {
5826 while (my $line = <$fd>) {
5827 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5828 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5830 print $line;
5832 last if $line =~ m!^\+\+\+!;
5834 local $/ = undef;
5835 print <$fd>;
5836 close $fd;
5840 sub git_blobdiff_plain {
5841 git_blobdiff('plain');
5844 sub git_commitdiff {
5845 my %params = @_;
5846 my $format = $params{-format} || 'html';
5848 my ($patch_max) = gitweb_get_feature('patches');
5849 if ($format eq 'patch') {
5850 die_error(403, "Patch view not allowed") unless $patch_max;
5853 $hash ||= $hash_base || "HEAD";
5854 my %co = parse_commit($hash)
5855 or die_error(404, "Unknown commit object");
5857 # choose format for commitdiff for merge
5858 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5859 $hash_parent = '--cc';
5861 # we need to prepare $formats_nav before almost any parameter munging
5862 my $formats_nav;
5863 if ($format eq 'html') {
5864 $formats_nav =
5865 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5866 "raw");
5867 if ($patch_max && @{$co{'parents'}} <= 1) {
5868 $formats_nav .= " | " .
5869 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5870 "patch");
5873 if (defined $hash_parent &&
5874 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5875 # commitdiff with two commits given
5876 my $hash_parent_short = $hash_parent;
5877 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5878 $hash_parent_short = substr($hash_parent, 0, 7);
5880 $formats_nav .=
5881 ' (from';
5882 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5883 if ($co{'parents'}[$i] eq $hash_parent) {
5884 $formats_nav .= ' parent ' . ($i+1);
5885 last;
5888 $formats_nav .= ': ' .
5889 $cgi->a({-href => href(action=>"commitdiff",
5890 hash=>$hash_parent)},
5891 esc_html($hash_parent_short)) .
5892 ')';
5893 } elsif (!$co{'parent'}) {
5894 # --root commitdiff
5895 $formats_nav .= ' (initial)';
5896 } elsif (scalar @{$co{'parents'}} == 1) {
5897 # single parent commit
5898 $formats_nav .=
5899 ' (parent: ' .
5900 $cgi->a({-href => href(action=>"commitdiff",
5901 hash=>$co{'parent'})},
5902 esc_html(substr($co{'parent'}, 0, 7))) .
5903 ')';
5904 } else {
5905 # merge commit
5906 if ($hash_parent eq '--cc') {
5907 $formats_nav .= ' | ' .
5908 $cgi->a({-href => href(action=>"commitdiff",
5909 hash=>$hash, hash_parent=>'-c')},
5910 'combined');
5911 } else { # $hash_parent eq '-c'
5912 $formats_nav .= ' | ' .
5913 $cgi->a({-href => href(action=>"commitdiff",
5914 hash=>$hash, hash_parent=>'--cc')},
5915 'compact');
5917 $formats_nav .=
5918 ' (merge: ' .
5919 join(' ', map {
5920 $cgi->a({-href => href(action=>"commitdiff",
5921 hash=>$_)},
5922 esc_html(substr($_, 0, 7)));
5923 } @{$co{'parents'}} ) .
5924 ')';
5928 my $hash_parent_param = $hash_parent;
5929 if (!defined $hash_parent_param) {
5930 # --cc for multiple parents, --root for parentless
5931 $hash_parent_param =
5932 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5935 # read commitdiff
5936 my $fd;
5937 my @difftree;
5938 if ($format eq 'html') {
5939 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5940 "--no-commit-id", "--patch-with-raw", "--full-index",
5941 $hash_parent_param, $hash, "--"
5942 or die_error(500, "Open git-diff-tree failed");
5944 while (my $line = <$fd>) {
5945 chomp $line;
5946 # empty line ends raw part of diff-tree output
5947 last unless $line;
5948 push @difftree, scalar parse_difftree_raw_line($line);
5951 } elsif ($format eq 'plain') {
5952 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5953 '-p', $hash_parent_param, $hash, "--"
5954 or die_error(500, "Open git-diff-tree failed");
5955 } elsif ($format eq 'patch') {
5956 # For commit ranges, we limit the output to the number of
5957 # patches specified in the 'patches' feature.
5958 # For single commits, we limit the output to a single patch,
5959 # diverging from the git-format-patch default.
5960 my @commit_spec = ();
5961 if ($hash_parent) {
5962 if ($patch_max > 0) {
5963 push @commit_spec, "-$patch_max";
5965 push @commit_spec, '-n', "$hash_parent..$hash";
5966 } else {
5967 if ($params{-single}) {
5968 push @commit_spec, '-1';
5969 } else {
5970 if ($patch_max > 0) {
5971 push @commit_spec, "-$patch_max";
5973 push @commit_spec, "-n";
5975 push @commit_spec, '--root', $hash;
5977 open $fd, "-|", git_cmd(), "format-patch", '--encoding=utf8',
5978 '--stdout', @commit_spec
5979 or die_error(500, "Open git-format-patch failed");
5980 } else {
5981 die_error(400, "Unknown commitdiff format");
5984 # non-textual hash id's can be cached
5985 my $expires;
5986 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5987 $expires = "+1d";
5990 # write commit message
5991 if ($format eq 'html') {
5992 my $refs = git_get_references();
5993 my $ref = format_ref_marker($refs, $co{'id'});
5995 git_header_html(undef, $expires);
5996 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5997 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5998 print "<div class=\"title_text\">\n" .
5999 "<table class=\"object_header\">\n";
6000 git_print_authorship_rows(\%co);
6001 print "</table>".
6002 "</div>\n";
6003 print "<div class=\"page_body\">\n";
6004 if (@{$co{'comment'}} > 1) {
6005 print "<div class=\"log\">\n";
6006 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6007 print "</div>\n"; # class="log"
6010 } elsif ($format eq 'plain') {
6011 my $refs = git_get_references("tags");
6012 my $tagname = git_get_rev_name_tags($hash);
6013 my $filename = basename($project) . "-$hash.patch";
6015 print $cgi->header(
6016 -type => 'text/plain',
6017 -charset => 'utf-8',
6018 -expires => $expires,
6019 -content_disposition => 'inline; filename="' . "$filename" . '"');
6020 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
6021 print "From: " . to_utf8($co{'author'}) . "\n";
6022 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6023 print "Subject: " . to_utf8($co{'title'}) . "\n";
6025 print "X-Git-Tag: $tagname\n" if $tagname;
6026 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6028 foreach my $line (@{$co{'comment'}}) {
6029 print to_utf8($line) . "\n";
6031 print "---\n\n";
6032 } elsif ($format eq 'patch') {
6033 my $filename = basename($project) . "-$hash.patch";
6035 print $cgi->header(
6036 -type => 'text/plain',
6037 -charset => 'utf-8',
6038 -expires => $expires,
6039 -content_disposition => 'inline; filename="' . "$filename" . '"');
6042 # write patch
6043 if ($format eq 'html') {
6044 my $use_parents = !defined $hash_parent ||
6045 $hash_parent eq '-c' || $hash_parent eq '--cc';
6046 git_difftree_body(\@difftree, $hash,
6047 $use_parents ? @{$co{'parents'}} : $hash_parent);
6048 print "<br/>\n";
6050 git_patchset_body($fd, \@difftree, $hash,
6051 $use_parents ? @{$co{'parents'}} : $hash_parent);
6052 close $fd;
6053 print "</div>\n"; # class="page_body"
6054 git_footer_html();
6056 } elsif ($format eq 'plain') {
6057 local $/ = undef;
6058 print <$fd>;
6059 close $fd
6060 or print "Reading git-diff-tree failed\n";
6061 } elsif ($format eq 'patch') {
6062 local $/ = undef;
6063 print <$fd>;
6064 close $fd
6065 or print "Reading git-format-patch failed\n";
6069 sub git_commitdiff_plain {
6070 git_commitdiff(-format => 'plain');
6073 # format-patch-style patches
6074 sub git_patch {
6075 git_commitdiff(-format => 'patch', -single => 1);
6078 sub git_patches {
6079 git_commitdiff(-format => 'patch');
6082 sub git_history {
6083 if (!defined $hash_base) {
6084 $hash_base = git_get_head_hash($project);
6086 if (!defined $page) {
6087 $page = 0;
6089 my $ftype;
6090 my %co = parse_commit($hash_base)
6091 or die_error(404, "Unknown commit object");
6093 my $refs = git_get_references();
6094 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
6096 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
6097 $file_name, "--full-history")
6098 or die_error(404, "No such file or directory on given branch");
6100 if (!defined $hash && defined $file_name) {
6101 # some commits could have deleted file in question,
6102 # and not have it in tree, but one of them has to have it
6103 for (my $i = 0; $i <= @commitlist; $i++) {
6104 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
6105 last if defined $hash;
6108 if (defined $hash) {
6109 $ftype = git_get_type($hash);
6111 if (!defined $ftype) {
6112 die_error(500, "Unknown type of object");
6115 my $paging_nav = '';
6116 if ($page > 0) {
6117 $paging_nav .=
6118 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
6119 file_name=>$file_name)},
6120 "first");
6121 $paging_nav .= " &sdot; " .
6122 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6123 -accesskey => "p", -title => "Alt-p"}, "prev");
6124 } else {
6125 $paging_nav .= "first";
6126 $paging_nav .= " &sdot; prev";
6128 my $next_link = '';
6129 if ($#commitlist >= 100) {
6130 $next_link =
6131 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6132 -accesskey => "n", -title => "Alt-n"}, "next");
6133 $paging_nav .= " &sdot; $next_link";
6134 } else {
6135 $paging_nav .= " &sdot; next";
6138 git_header_html();
6139 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
6140 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6141 git_print_page_path($file_name, $ftype, $hash_base);
6143 git_history_body(\@commitlist, 0, 99,
6144 $refs, $hash_base, $ftype, $next_link);
6146 git_footer_html();
6149 sub git_search {
6150 gitweb_check_feature('search') or die_error(403, "Search is disabled");
6151 if (!defined $searchtext) {
6152 die_error(400, "Text field is empty");
6154 if (!defined $hash) {
6155 $hash = git_get_head_hash($project);
6157 my %co = parse_commit($hash);
6158 if (!%co) {
6159 die_error(404, "Unknown commit object");
6161 if (!defined $page) {
6162 $page = 0;
6165 $searchtype ||= 'commit';
6166 if ($searchtype eq 'pickaxe') {
6167 # pickaxe may take all resources of your box and run for several minutes
6168 # with every query - so decide by yourself how public you make this feature
6169 gitweb_check_feature('pickaxe')
6170 or die_error(403, "Pickaxe is disabled");
6172 if ($searchtype eq 'grep') {
6173 gitweb_check_feature('grep')
6174 or die_error(403, "Grep is disabled");
6177 git_header_html();
6179 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6180 my $greptype;
6181 if ($searchtype eq 'commit') {
6182 $greptype = "--grep=";
6183 } elsif ($searchtype eq 'author') {
6184 $greptype = "--author=";
6185 } elsif ($searchtype eq 'committer') {
6186 $greptype = "--committer=";
6188 $greptype .= $searchtext;
6189 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6190 $greptype, '--regexp-ignore-case',
6191 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6193 my $paging_nav = '';
6194 if ($page > 0) {
6195 $paging_nav .=
6196 $cgi->a({-href => href(action=>"search", hash=>$hash,
6197 searchtext=>$searchtext,
6198 searchtype=>$searchtype)},
6199 "first");
6200 $paging_nav .= " &sdot; " .
6201 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6202 -accesskey => "p", -title => "Alt-p"}, "prev");
6203 } else {
6204 $paging_nav .= "first";
6205 $paging_nav .= " &sdot; prev";
6207 my $next_link = '';
6208 if ($#commitlist >= 100) {
6209 $next_link =
6210 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6211 -accesskey => "n", -title => "Alt-n"}, "next");
6212 $paging_nav .= " &sdot; $next_link";
6213 } else {
6214 $paging_nav .= " &sdot; next";
6217 if ($#commitlist >= 100) {
6220 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6221 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6222 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6225 if ($searchtype eq 'pickaxe') {
6226 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6227 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6229 print "<table class=\"pickaxe search\">\n";
6230 my $alternate = 1;
6231 local $/ = "\n";
6232 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6233 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6234 ($search_use_regexp ? '--pickaxe-regex' : ());
6235 undef %co;
6236 my @files;
6237 while (my $line = <$fd>) {
6238 chomp $line;
6239 next unless $line;
6241 my %set = parse_difftree_raw_line($line);
6242 if (defined $set{'commit'}) {
6243 # finish previous commit
6244 if (%co) {
6245 print "</td>\n" .
6246 "<td class=\"link\">" .
6247 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6248 " | " .
6249 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6250 print "</td>\n" .
6251 "</tr>\n";
6254 if ($alternate) {
6255 print "<tr class=\"dark\">\n";
6256 } else {
6257 print "<tr class=\"light\">\n";
6259 $alternate ^= 1;
6260 %co = parse_commit($set{'commit'});
6261 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6262 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6263 "<td><i>$author</i></td>\n" .
6264 "<td>" .
6265 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6266 -class => "list subject"},
6267 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6268 } elsif (defined $set{'to_id'}) {
6269 next if ($set{'to_id'} =~ m/^0{40}$/);
6271 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6272 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6273 -class => "list"},
6274 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6275 "<br/>\n";
6278 close $fd;
6280 # finish last commit (warning: repetition!)
6281 if (%co) {
6282 print "</td>\n" .
6283 "<td class=\"link\">" .
6284 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6285 " | " .
6286 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6287 print "</td>\n" .
6288 "</tr>\n";
6291 print "</table>\n";
6294 if ($searchtype eq 'grep') {
6295 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6296 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6298 print "<table class=\"grep_search\">\n";
6299 my $alternate = 1;
6300 my $matches = 0;
6301 local $/ = "\n";
6302 open my $fd, "-|", git_cmd(), 'grep', '-n',
6303 $search_use_regexp ? ('-E', '-i') : '-F',
6304 $searchtext, $co{'tree'};
6305 my $lastfile = '';
6306 while (my $line = <$fd>) {
6307 chomp $line;
6308 my ($file, $lno, $ltext, $binary);
6309 last if ($matches++ > 1000);
6310 if ($line =~ /^Binary file (.+) matches$/) {
6311 $file = $1;
6312 $binary = 1;
6313 } else {
6314 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6316 if ($file ne $lastfile) {
6317 $lastfile and print "</td></tr>\n";
6318 if ($alternate++) {
6319 print "<tr class=\"dark\">\n";
6320 } else {
6321 print "<tr class=\"light\">\n";
6323 print "<td class=\"list\">".
6324 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6325 file_name=>"$file"),
6326 -class => "list"}, esc_path($file));
6327 print "</td><td>\n";
6328 $lastfile = $file;
6330 if ($binary) {
6331 print "<div class=\"binary\">Binary file</div>\n";
6332 } else {
6333 $ltext = untabify($ltext);
6334 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6335 $ltext = esc_html($1, -nbsp=>1);
6336 $ltext .= '<span class="match">';
6337 $ltext .= esc_html($2, -nbsp=>1);
6338 $ltext .= '</span>';
6339 $ltext .= esc_html($3, -nbsp=>1);
6340 } else {
6341 $ltext = esc_html($ltext, -nbsp=>1);
6343 print "<div class=\"pre\">" .
6344 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6345 file_name=>"$file").'#l'.$lno,
6346 -class => "linenr"}, sprintf('%4i', $lno))
6347 . ' ' . $ltext . "</div>\n";
6350 if ($lastfile) {
6351 print "</td></tr>\n";
6352 if ($matches > 1000) {
6353 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6355 } else {
6356 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6358 close $fd;
6360 print "</table>\n";
6362 git_footer_html();
6365 sub git_search_help {
6366 git_header_html();
6367 git_print_page_nav('','', $hash,$hash,$hash);
6368 print <<EOT;
6369 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6370 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6371 the pattern entered is recognized as the POSIX extended
6372 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6373 insensitive).</p>
6374 <dl>
6375 <dt><b>commit</b></dt>
6376 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6378 my $have_grep = gitweb_check_feature('grep');
6379 if ($have_grep) {
6380 print <<EOT;
6381 <dt><b>grep</b></dt>
6382 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6383 a different one) are searched for the given pattern. On large trees, this search can take
6384 a while and put some strain on the server, so please use it with some consideration. Note that
6385 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6386 case-sensitive.</dd>
6389 print <<EOT;
6390 <dt><b>author</b></dt>
6391 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6392 <dt><b>committer</b></dt>
6393 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6395 my $have_pickaxe = gitweb_check_feature('pickaxe');
6396 if ($have_pickaxe) {
6397 print <<EOT;
6398 <dt><b>pickaxe</b></dt>
6399 <dd>All commits that caused the string to appear or disappear from any file (changes that
6400 added, removed or "modified" the string) will be listed. This search can take a while and
6401 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6402 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6405 print "</dl>\n";
6406 git_footer_html();
6409 sub git_shortlog {
6410 my $head = git_get_head_hash($project);
6411 if (!defined $hash) {
6412 $hash = $head;
6414 if (!defined $page) {
6415 $page = 0;
6417 my $refs = git_get_references();
6419 my $commit_hash = $hash;
6420 if (defined $hash_parent) {
6421 $commit_hash = "$hash_parent..$hash";
6423 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
6425 my $paging_nav = format_log_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
6427 my $next_link = '';
6428 if ($#commitlist >= 100) {
6429 $next_link =
6430 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6431 -accesskey => "n", -title => "Alt-n"}, "next");
6433 my $patch_max = gitweb_check_feature('patches');
6434 if ($patch_max) {
6435 if ($patch_max < 0 || @commitlist <= $patch_max) {
6436 $paging_nav .= " &sdot; " .
6437 $cgi->a({-href => href(action=>"patches", -replay=>1)},
6438 "patches");
6442 git_header_html();
6443 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
6444 git_print_header_div('summary', $project);
6446 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
6448 git_footer_html();
6451 ## ......................................................................
6452 ## feeds (RSS, Atom; OPML)
6454 sub git_feed {
6455 my $format = shift || 'atom';
6456 my $have_blame = gitweb_check_feature('blame');
6458 # Atom: http://www.atomenabled.org/developers/syndication/
6459 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6460 if ($format ne 'rss' && $format ne 'atom') {
6461 die_error(400, "Unknown web feed format");
6464 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6465 my $head = $hash || 'HEAD';
6466 my @commitlist = parse_commits($head, 150, 0, $file_name);
6468 my %latest_commit;
6469 my %latest_date;
6470 my $content_type = "application/$format+xml";
6471 if (defined $cgi->http('HTTP_ACCEPT') &&
6472 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6473 # browser (feed reader) prefers text/xml
6474 $content_type = 'text/xml';
6476 if (defined($commitlist[0])) {
6477 %latest_commit = %{$commitlist[0]};
6478 my $latest_epoch = $latest_commit{'committer_epoch'};
6479 %latest_date = parse_date($latest_epoch);
6480 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6481 if (defined $if_modified) {
6482 my $since;
6483 if (eval { require HTTP::Date; 1; }) {
6484 $since = HTTP::Date::str2time($if_modified);
6485 } elsif (eval { require Time::ParseDate; 1; }) {
6486 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6488 if (defined $since && $latest_epoch <= $since) {
6489 print $cgi->header(
6490 -type => $content_type,
6491 -charset => 'utf-8',
6492 -last_modified => $latest_date{'rfc2822'},
6493 -status => '304 Not Modified');
6494 return;
6497 print $cgi->header(
6498 -type => $content_type,
6499 -charset => 'utf-8',
6500 -last_modified => $latest_date{'rfc2822'});
6501 } else {
6502 print $cgi->header(
6503 -type => $content_type,
6504 -charset => 'utf-8');
6507 # Optimization: skip generating the body if client asks only
6508 # for Last-Modified date.
6509 return if ($cgi->request_method() eq 'HEAD');
6511 # header variables
6512 my $title = "$site_name - $project/$action";
6513 my $feed_type = 'log';
6514 if (defined $hash) {
6515 $title .= " - '$hash'";
6516 $feed_type = 'branch log';
6517 if (defined $file_name) {
6518 $title .= " :: $file_name";
6519 $feed_type = 'history';
6521 } elsif (defined $file_name) {
6522 $title .= " - $file_name";
6523 $feed_type = 'history';
6525 $title .= " $feed_type";
6526 my $descr = git_get_project_description($project);
6527 if (defined $descr) {
6528 $descr = esc_html($descr);
6529 } else {
6530 $descr = "$project " .
6531 ($format eq 'rss' ? 'RSS' : 'Atom') .
6532 " feed";
6534 my $owner = git_get_project_owner($project);
6535 $owner = esc_html($owner);
6537 #header
6538 my $alt_url;
6539 if (defined $file_name) {
6540 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6541 } elsif (defined $hash) {
6542 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6543 } else {
6544 $alt_url = href(-full=>1, action=>"summary");
6546 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6547 if ($format eq 'rss') {
6548 print <<XML;
6549 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6550 <channel>
6552 print "<title>$title</title>\n" .
6553 "<link>$alt_url</link>\n" .
6554 "<description>$descr</description>\n" .
6555 "<language>en</language>\n" .
6556 # project owner is responsible for 'editorial' content
6557 "<managingEditor>$owner</managingEditor>\n";
6558 if (defined $logo || defined $favicon) {
6559 # prefer the logo to the favicon, since RSS
6560 # doesn't allow both
6561 my $img = esc_url($logo || $favicon);
6562 print "<image>\n" .
6563 "<url>$img</url>\n" .
6564 "<title>$title</title>\n" .
6565 "<link>$alt_url</link>\n" .
6566 "</image>\n";
6568 if (%latest_date) {
6569 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6570 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6572 print "<generator>gitweb v.$version/$git_version</generator>\n";
6573 } elsif ($format eq 'atom') {
6574 print <<XML;
6575 <feed xmlns="http://www.w3.org/2005/Atom">
6577 print "<title>$title</title>\n" .
6578 "<subtitle>$descr</subtitle>\n" .
6579 '<link rel="alternate" type="text/html" href="' .
6580 $alt_url . '" />' . "\n" .
6581 '<link rel="self" type="' . $content_type . '" href="' .
6582 $cgi->self_url() . '" />' . "\n" .
6583 "<id>" . href(-full=>1) . "</id>\n" .
6584 # use project owner for feed author
6585 "<author><name>$owner</name></author>\n";
6586 if (defined $favicon) {
6587 print "<icon>" . esc_url($favicon) . "</icon>\n";
6589 if (defined $logo_url) {
6590 # not twice as wide as tall: 72 x 27 pixels
6591 print "<logo>" . esc_url($logo) . "</logo>\n";
6593 if (! %latest_date) {
6594 # dummy date to keep the feed valid until commits trickle in:
6595 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6596 } else {
6597 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6599 print "<generator version='$version/$git_version'>gitweb</generator>\n";
6602 # contents
6603 for (my $i = 0; $i <= $#commitlist; $i++) {
6604 my %co = %{$commitlist[$i]};
6605 my $commit = $co{'id'};
6606 # we read 150, we always show 30 and the ones more recent than 48 hours
6607 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6608 last;
6610 my %cd = parse_date($co{'author_epoch'});
6612 # get list of changed files
6613 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6614 $co{'parent'} || "--root",
6615 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6616 or next;
6617 my @difftree = map { chomp; $_ } <$fd>;
6618 close $fd
6619 or next;
6621 # print element (entry, item)
6622 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6623 if ($format eq 'rss') {
6624 print "<item>\n" .
6625 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6626 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6627 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6628 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6629 "<link>$co_url</link>\n" .
6630 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6631 "<content:encoded>" .
6632 "<![CDATA[\n";
6633 } elsif ($format eq 'atom') {
6634 print "<entry>\n" .
6635 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6636 "<updated>$cd{'iso-8601'}</updated>\n" .
6637 "<author>\n" .
6638 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6639 if ($co{'author_email'}) {
6640 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6642 print "</author>\n" .
6643 # use committer for contributor
6644 "<contributor>\n" .
6645 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6646 if ($co{'committer_email'}) {
6647 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6649 print "</contributor>\n" .
6650 "<published>$cd{'iso-8601'}</published>\n" .
6651 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6652 "<id>$co_url</id>\n" .
6653 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6654 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6656 my $comment = $co{'comment'};
6657 print "<pre>\n";
6658 foreach my $line (@$comment) {
6659 $line = esc_html($line);
6660 print "$line\n";
6662 print "</pre><ul>\n";
6663 foreach my $difftree_line (@difftree) {
6664 my %difftree = parse_difftree_raw_line($difftree_line);
6665 next if !$difftree{'from_id'};
6667 my $file = $difftree{'file'} || $difftree{'to_file'};
6669 print "<li>" .
6670 "[" .
6671 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6672 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6673 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6674 file_name=>$file, file_parent=>$difftree{'from_file'}),
6675 -title => "diff"}, 'D');
6676 if ($have_blame) {
6677 print $cgi->a({-href => href(-full=>1, action=>"blame",
6678 file_name=>$file, hash_base=>$commit), -class => "blamelink",
6679 -title => "blame"}, 'B');
6681 # if this is not a feed of a file history
6682 if (!defined $file_name || $file_name ne $file) {
6683 print $cgi->a({-href => href(-full=>1, action=>"history",
6684 file_name=>$file, hash=>$commit),
6685 -title => "history"}, 'H');
6687 $file = esc_path($file);
6688 print "] ".
6689 "$file</li>\n";
6691 if ($format eq 'rss') {
6692 print "</ul>]]>\n" .
6693 "</content:encoded>\n" .
6694 "</item>\n";
6695 } elsif ($format eq 'atom') {
6696 print "</ul>\n</div>\n" .
6697 "</content>\n" .
6698 "</entry>\n";
6702 # end of feed
6703 if ($format eq 'rss') {
6704 print "</channel>\n</rss>\n";
6705 } elsif ($format eq 'atom') {
6706 print "</feed>\n";
6710 sub git_rss {
6711 git_feed('rss');
6714 sub git_atom {
6715 git_feed('atom');
6718 sub git_opml {
6719 my @list = git_get_projects_list();
6721 print $cgi->header(
6722 -type => 'text/xml',
6723 -charset => 'utf-8',
6724 -content_disposition => 'inline; filename="opml.xml"');
6726 print <<XML;
6727 <?xml version="1.0" encoding="utf-8"?>
6728 <opml version="1.0">
6729 <head>
6730 <title>$site_name OPML Export</title>
6731 </head>
6732 <body>
6733 <outline text="git RSS feeds">
6736 foreach my $pr (@list) {
6737 my %proj = %$pr;
6738 my $head = git_get_head_hash($proj{'path'});
6739 if (!defined $head) {
6740 next;
6742 $git_dir = "$projectroot/$proj{'path'}";
6743 my %co = parse_commit($head);
6744 if (!%co) {
6745 next;
6748 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6749 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
6750 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
6751 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6753 print <<XML;
6754 </outline>
6755 </body>
6756 </opml>