97ea3ece4b680741bbef8dc1d08dfe08b65aa8e3
[git/jnareb-git.git] / gitweb / gitweb.perl
blob97ea3ece4b680741bbef8dc1d08dfe08b65aa8e3
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 our $t0;
22 if (eval { require Time::HiRes; 1; }) {
23 $t0 = [Time::HiRes::gettimeofday()];
25 our $number_of_git_cmds = 0;
27 BEGIN {
28 CGI->compile() if $ENV{'MOD_PERL'};
31 our $cgi = new CGI;
32 our $version = "++GIT_VERSION++";
33 our $my_url = $cgi->url();
34 our $my_uri = $cgi->url(-absolute => 1);
36 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
37 # needed and used only for URLs with nonempty PATH_INFO
38 our $base_url = $my_url;
40 # When the script is used as DirectoryIndex, the URL does not contain the name
41 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
42 # have to do it ourselves. We make $path_info global because it's also used
43 # later on.
45 # Another issue with the script being the DirectoryIndex is that the resulting
46 # $my_url data is not the full script URL: this is good, because we want
47 # generated links to keep implying the script name if it wasn't explicitly
48 # indicated in the URL we're handling, but it means that $my_url cannot be used
49 # as base URL.
50 # Therefore, if we needed to strip PATH_INFO, then we know that we have
51 # to build the base URL ourselves:
52 our $path_info = $ENV{"PATH_INFO"};
53 if ($path_info) {
54 if ($my_url =~ s,\Q$path_info\E$,, &&
55 $my_uri =~ s,\Q$path_info\E$,, &&
56 defined $ENV{'SCRIPT_NAME'}) {
57 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
61 # core git executable to use
62 # this can just be "git" if your webserver has a sensible PATH
63 our $GIT = "++GIT_BINDIR++/git";
65 # absolute fs-path which will be prepended to the project path
66 #our $projectroot = "/pub/scm";
67 our $projectroot = "++GITWEB_PROJECTROOT++";
69 # fs traversing limit for getting project list
70 # the number is relative to the projectroot
71 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
73 # target of the home link on top of all pages
74 our $home_link = $my_uri || "/";
76 # string of the home link on top of all pages
77 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
79 # name of your site or organization to appear in page titles
80 # replace this with something more descriptive for clearer bookmarks
81 our $site_name = "++GITWEB_SITENAME++"
82 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
84 # filename of html text to include at top of each page
85 our $site_header = "++GITWEB_SITE_HEADER++";
86 # html text to include at home page
87 our $home_text = "++GITWEB_HOMETEXT++";
88 # filename of html text to include at bottom of each page
89 our $site_footer = "++GITWEB_SITE_FOOTER++";
91 # URI of stylesheets
92 our @stylesheets = ("++GITWEB_CSS++");
93 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
94 our $stylesheet = undef;
95 # URI of GIT logo (72x27 size)
96 our $logo = "++GITWEB_LOGO++";
97 # URI of GIT favicon, assumed to be image/png type
98 our $favicon = "++GITWEB_FAVICON++";
99 # URI of gitweb.js (JavaScript code for gitweb)
100 our $javascript = "++GITWEB_JS++";
102 # URI and label (title) of GIT logo link
103 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
104 #our $logo_label = "git documentation";
105 our $logo_url = "http://git-scm.com/";
106 our $logo_label = "git homepage";
108 # source of projects list
109 our $projects_list = "++GITWEB_LIST++";
111 # the width (in characters) of the projects list "Description" column
112 our $projects_list_description_width = 25;
114 # default order of projects list
115 # valid values are none, project, descr, owner, and age
116 our $default_projects_order = "project";
118 # show repository only if this file exists
119 # (only effective if this variable evaluates to true)
120 our $export_ok = "++GITWEB_EXPORT_OK++";
122 # show repository only if this subroutine returns true
123 # when given the path to the project, for example:
124 # sub { return -e "$_[0]/git-daemon-export-ok"; }
125 our $export_auth_hook = undef;
127 # only allow viewing of repositories also shown on the overview page
128 our $strict_export = "++GITWEB_STRICT_EXPORT++";
130 # list of git base URLs used for URL to where fetch project from,
131 # i.e. full URL is "$git_base_url/$project"
132 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
134 # default blob_plain mimetype and default charset for text/plain blob
135 our $default_blob_plain_mimetype = 'text/plain';
136 our $default_text_plain_charset = undef;
138 # file to use for guessing MIME types before trying /etc/mime.types
139 # (relative to the current git repository)
140 our $mimetypes_file = undef;
142 # assume this charset if line contains non-UTF-8 characters;
143 # it should be valid encoding (see Encoding::Supported(3pm) for list),
144 # for which encoding all byte sequences are valid, for example
145 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
146 # could be even 'utf-8' for the old behavior)
147 our $fallback_encoding = 'latin1';
149 # rename detection options for git-diff and git-diff-tree
150 # - default is '-M', with the cost proportional to
151 # (number of removed files) * (number of new files).
152 # - more costly is '-C' (which implies '-M'), with the cost proportional to
153 # (number of changed files + number of removed files) * (number of new files)
154 # - even more costly is '-C', '--find-copies-harder' with cost
155 # (number of files in the original tree) * (number of new files)
156 # - one might want to include '-B' option, e.g. '-B', '-M'
157 our @diff_opts = ('-M'); # taken from git_commit
159 # Disables features that would allow repository owners to inject script into
160 # the gitweb domain.
161 our $prevent_xss = 0;
163 # information about snapshot formats that gitweb is capable of serving
164 our %known_snapshot_formats = (
165 # name => {
166 # 'display' => display name,
167 # 'type' => mime type,
168 # 'suffix' => filename suffix,
169 # 'format' => --format for git-archive,
170 # 'compressor' => [compressor command and arguments]
171 # (array reference, optional)
172 # 'disabled' => boolean (optional)}
174 'tgz' => {
175 'display' => 'tar.gz',
176 'type' => 'application/x-gzip',
177 'suffix' => '.tar.gz',
178 'format' => 'tar',
179 'compressor' => ['gzip']},
181 'tbz2' => {
182 'display' => 'tar.bz2',
183 'type' => 'application/x-bzip2',
184 'suffix' => '.tar.bz2',
185 'format' => 'tar',
186 'compressor' => ['bzip2']},
188 'txz' => {
189 'display' => 'tar.xz',
190 'type' => 'application/x-xz',
191 'suffix' => '.tar.xz',
192 'format' => 'tar',
193 'compressor' => ['xz'],
194 'disabled' => 1},
196 'zip' => {
197 'display' => 'zip',
198 'type' => 'application/x-zip',
199 'suffix' => '.zip',
200 'format' => 'zip'},
203 # Aliases so we understand old gitweb.snapshot values in repository
204 # configuration.
205 our %known_snapshot_format_aliases = (
206 'gzip' => 'tgz',
207 'bzip2' => 'tbz2',
208 'xz' => 'txz',
210 # backward compatibility: legacy gitweb config support
211 'x-gzip' => undef, 'gz' => undef,
212 'x-bzip2' => undef, 'bz2' => undef,
213 'x-zip' => undef, '' => undef,
216 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
217 # are changed, it may be appropriate to change these values too via
218 # $GITWEB_CONFIG.
219 our %avatar_size = (
220 'default' => 16,
221 'double' => 32
224 # Used to set the maximum load that we will still respond to gitweb queries.
225 # If server load exceed this value then return "503 server busy" error.
226 # If gitweb cannot determined server load, it is taken to be 0.
227 # Leave it undefined (or set to 'undef') to turn off load checking.
228 our $maxload = 300;
230 # You define site-wide feature defaults here; override them with
231 # $GITWEB_CONFIG as necessary.
232 our %feature = (
233 # feature => {
234 # 'sub' => feature-sub (subroutine),
235 # 'override' => allow-override (boolean),
236 # 'default' => [ default options...] (array reference)}
238 # if feature is overridable (it means that allow-override has true value),
239 # then feature-sub will be called with default options as parameters;
240 # return value of feature-sub indicates if to enable specified feature
242 # if there is no 'sub' key (no feature-sub), then feature cannot be
243 # overriden
245 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
246 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
247 # is enabled
249 # Enable the 'blame' blob view, showing the last commit that modified
250 # each line in the file. This can be very CPU-intensive.
252 # To enable system wide have in $GITWEB_CONFIG
253 # $feature{'blame'}{'default'} = [1];
254 # To have project specific config enable override in $GITWEB_CONFIG
255 # $feature{'blame'}{'override'} = 1;
256 # and in project config gitweb.blame = 0|1;
257 'blame' => {
258 'sub' => sub { feature_bool('blame', @_) },
259 'override' => 0,
260 'default' => [0]},
262 # Enable the 'snapshot' link, providing a compressed archive of any
263 # tree. This can potentially generate high traffic if you have large
264 # project.
266 # Value is a list of formats defined in %known_snapshot_formats that
267 # you wish to offer.
268 # To disable system wide have in $GITWEB_CONFIG
269 # $feature{'snapshot'}{'default'} = [];
270 # To have project specific config enable override in $GITWEB_CONFIG
271 # $feature{'snapshot'}{'override'} = 1;
272 # and in project config, a comma-separated list of formats or "none"
273 # to disable. Example: gitweb.snapshot = tbz2,zip;
274 'snapshot' => {
275 'sub' => \&feature_snapshot,
276 'override' => 0,
277 'default' => ['tgz']},
279 # Enable text search, which will list the commits which match author,
280 # committer or commit text to a given string. Enabled by default.
281 # Project specific override is not supported.
282 'search' => {
283 'override' => 0,
284 'default' => [1]},
286 # Enable grep search, which will list the files in currently selected
287 # tree containing the given string. Enabled by default. This can be
288 # potentially CPU-intensive, of course.
290 # To enable system wide have in $GITWEB_CONFIG
291 # $feature{'grep'}{'default'} = [1];
292 # To have project specific config enable override in $GITWEB_CONFIG
293 # $feature{'grep'}{'override'} = 1;
294 # and in project config gitweb.grep = 0|1;
295 'grep' => {
296 'sub' => sub { feature_bool('grep', @_) },
297 'override' => 0,
298 'default' => [1]},
300 # Enable the pickaxe search, which will list the commits that modified
301 # a given string in a file. This can be practical and quite faster
302 # alternative to 'blame', but still potentially CPU-intensive.
304 # To enable system wide have in $GITWEB_CONFIG
305 # $feature{'pickaxe'}{'default'} = [1];
306 # To have project specific config enable override in $GITWEB_CONFIG
307 # $feature{'pickaxe'}{'override'} = 1;
308 # and in project config gitweb.pickaxe = 0|1;
309 'pickaxe' => {
310 'sub' => sub { feature_bool('pickaxe', @_) },
311 'override' => 0,
312 'default' => [1]},
314 # Enable showing size of blobs in a 'tree' view, in a separate
315 # column, similar to what 'ls -l' does. This cost a bit of IO.
317 # To disable system wide have in $GITWEB_CONFIG
318 # $feature{'show-sizes'}{'default'} = [0];
319 # To have project specific config enable override in $GITWEB_CONFIG
320 # $feature{'show-sizes'}{'override'} = 1;
321 # and in project config gitweb.showsizes = 0|1;
322 'show-sizes' => {
323 'sub' => sub { feature_bool('showsizes', @_) },
324 'override' => 0,
325 'default' => [1]},
327 # Make gitweb use an alternative format of the URLs which can be
328 # more readable and natural-looking: project name is embedded
329 # directly in the path and the query string contains other
330 # auxiliary information. All gitweb installations recognize
331 # URL in either format; this configures in which formats gitweb
332 # generates links.
334 # To enable system wide have in $GITWEB_CONFIG
335 # $feature{'pathinfo'}{'default'} = [1];
336 # Project specific override is not supported.
338 # Note that you will need to change the default location of CSS,
339 # favicon, logo and possibly other files to an absolute URL. Also,
340 # if gitweb.cgi serves as your indexfile, you will need to force
341 # $my_uri to contain the script name in your $GITWEB_CONFIG.
342 'pathinfo' => {
343 'override' => 0,
344 'default' => [0]},
346 # Make gitweb consider projects in project root subdirectories
347 # to be forks of existing projects. Given project $projname.git,
348 # projects matching $projname/*.git will not be shown in the main
349 # projects list, instead a '+' mark will be added to $projname
350 # there and a 'forks' view will be enabled for the project, listing
351 # all the forks. If project list is taken from a file, forks have
352 # to be listed after the main project.
354 # To enable system wide have in $GITWEB_CONFIG
355 # $feature{'forks'}{'default'} = [1];
356 # Project specific override is not supported.
357 'forks' => {
358 'override' => 0,
359 'default' => [0]},
361 # Insert custom links to the action bar of all project pages.
362 # This enables you mainly to link to third-party scripts integrating
363 # into gitweb; e.g. git-browser for graphical history representation
364 # or custom web-based repository administration interface.
366 # The 'default' value consists of a list of triplets in the form
367 # (label, link, position) where position is the label after which
368 # to insert the link and link is a format string where %n expands
369 # to the project name, %f to the project path within the filesystem,
370 # %h to the current hash (h gitweb parameter) and %b to the current
371 # hash base (hb gitweb parameter); %% expands to %.
373 # To enable system wide have in $GITWEB_CONFIG e.g.
374 # $feature{'actions'}{'default'} = [('graphiclog',
375 # '/git-browser/by-commit.html?r=%n', 'summary')];
376 # Project specific override is not supported.
377 'actions' => {
378 'override' => 0,
379 'default' => []},
381 # Allow gitweb scan project content tags described in ctags/
382 # of project repository, and display the popular Web 2.0-ish
383 # "tag cloud" near the project list. Note that this is something
384 # COMPLETELY different from the normal Git tags.
386 # gitweb by itself can show existing tags, but it does not handle
387 # tagging itself; you need an external application for that.
388 # For an example script, check Girocco's cgi/tagproj.cgi.
389 # You may want to install the HTML::TagCloud Perl module to get
390 # a pretty tag cloud instead of just a list of tags.
392 # To enable system wide have in $GITWEB_CONFIG
393 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
394 # Project specific override is not supported.
395 'ctags' => {
396 'override' => 0,
397 'default' => [0]},
399 # The maximum number of patches in a patchset generated in patch
400 # view. Set this to 0 or undef to disable patch view, or to a
401 # negative number to remove any limit.
403 # To disable system wide have in $GITWEB_CONFIG
404 # $feature{'patches'}{'default'} = [0];
405 # To have project specific config enable override in $GITWEB_CONFIG
406 # $feature{'patches'}{'override'} = 1;
407 # and in project config gitweb.patches = 0|n;
408 # where n is the maximum number of patches allowed in a patchset.
409 'patches' => {
410 'sub' => \&feature_patches,
411 'override' => 0,
412 'default' => [16]},
414 # Avatar support. When this feature is enabled, views such as
415 # shortlog or commit will display an avatar associated with
416 # the email of the committer(s) and/or author(s).
418 # Currently available providers are gravatar and picon.
419 # If an unknown provider is specified, the feature is disabled.
421 # Gravatar depends on Digest::MD5.
422 # Picon currently relies on the indiana.edu database.
424 # To enable system wide have in $GITWEB_CONFIG
425 # $feature{'avatar'}{'default'} = ['<provider>'];
426 # where <provider> is either gravatar or picon.
427 # To have project specific config enable override in $GITWEB_CONFIG
428 # $feature{'avatar'}{'override'} = 1;
429 # and in project config gitweb.avatar = <provider>;
430 'avatar' => {
431 'sub' => \&feature_avatar,
432 'override' => 0,
433 'default' => ['']},
435 # Enable displaying how much time and how many git commands
436 # it took to generate and display page. Disabled by default.
437 # Project specific override is not supported.
438 'timed' => {
439 'override' => 0,
440 'default' => [0]},
442 # Enable turning some links into links to actions which require
443 # JavaScript to run (like 'blame_incremental'). Not enabled by
444 # default. Project specific override is currently not supported.
445 'javascript-actions' => {
446 'override' => 0,
447 'default' => [0]},
450 sub gitweb_get_feature {
451 my ($name) = @_;
452 return unless exists $feature{$name};
453 my ($sub, $override, @defaults) = (
454 $feature{$name}{'sub'},
455 $feature{$name}{'override'},
456 @{$feature{$name}{'default'}});
457 if (!$override) { return @defaults; }
458 if (!defined $sub) {
459 warn "feature $name is not overridable";
460 return @defaults;
462 return $sub->(@defaults);
465 # A wrapper to check if a given feature is enabled.
466 # With this, you can say
468 # my $bool_feat = gitweb_check_feature('bool_feat');
469 # gitweb_check_feature('bool_feat') or somecode;
471 # instead of
473 # my ($bool_feat) = gitweb_get_feature('bool_feat');
474 # (gitweb_get_feature('bool_feat'))[0] or somecode;
476 sub gitweb_check_feature {
477 return (gitweb_get_feature(@_))[0];
481 sub feature_bool {
482 my $key = shift;
483 my ($val) = git_get_project_config($key, '--bool');
485 if (!defined $val) {
486 return ($_[0]);
487 } elsif ($val eq 'true') {
488 return (1);
489 } elsif ($val eq 'false') {
490 return (0);
494 sub feature_snapshot {
495 my (@fmts) = @_;
497 my ($val) = git_get_project_config('snapshot');
499 if ($val) {
500 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
503 return @fmts;
506 sub feature_patches {
507 my @val = (git_get_project_config('patches', '--int'));
509 if (@val) {
510 return @val;
513 return ($_[0]);
516 sub feature_avatar {
517 my @val = (git_get_project_config('avatar'));
519 return @val ? @val : @_;
522 # checking HEAD file with -e is fragile if the repository was
523 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
524 # and then pruned.
525 sub check_head_link {
526 my ($dir) = @_;
527 my $headfile = "$dir/HEAD";
528 return ((-e $headfile) ||
529 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
532 sub check_export_ok {
533 my ($dir) = @_;
534 return (check_head_link($dir) &&
535 (!$export_ok || -e "$dir/$export_ok") &&
536 (!$export_auth_hook || $export_auth_hook->($dir)));
539 # process alternate names for backward compatibility
540 # filter out unsupported (unknown) snapshot formats
541 sub filter_snapshot_fmts {
542 my @fmts = @_;
544 @fmts = map {
545 exists $known_snapshot_format_aliases{$_} ?
546 $known_snapshot_format_aliases{$_} : $_} @fmts;
547 @fmts = grep {
548 exists $known_snapshot_formats{$_} &&
549 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
552 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
553 if (-e $GITWEB_CONFIG) {
554 do $GITWEB_CONFIG;
555 } else {
556 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
557 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
560 # Get loadavg of system, to compare against $maxload.
561 # Currently it requires '/proc/loadavg' present to get loadavg;
562 # if it is not present it returns 0, which means no load checking.
563 sub get_loadavg {
564 if( -e '/proc/loadavg' ){
565 open my $fd, '<', '/proc/loadavg'
566 or return 0;
567 my @load = split(/\s+/, scalar <$fd>);
568 close $fd;
570 # The first three columns measure CPU and IO utilization of the last one,
571 # five, and 10 minute periods. The fourth column shows the number of
572 # currently running processes and the total number of processes in the m/n
573 # format. The last column displays the last process ID used.
574 return $load[0] || 0;
576 # additional checks for load average should go here for things that don't export
577 # /proc/loadavg
579 return 0;
582 # version of the core git binary
583 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
584 $number_of_git_cmds++;
586 $projects_list ||= $projectroot;
588 if (defined $maxload && get_loadavg() > $maxload) {
589 die_error(503, "The load average on the server is too high");
592 # ======================================================================
593 # input validation and dispatch
595 # input parameters can be collected from a variety of sources (presently, CGI
596 # and PATH_INFO), so we define an %input_params hash that collects them all
597 # together during validation: this allows subsequent uses (e.g. href()) to be
598 # agnostic of the parameter origin
600 our %input_params = ();
602 # input parameters are stored with the long parameter name as key. This will
603 # also be used in the href subroutine to convert parameters to their CGI
604 # equivalent, and since the href() usage is the most frequent one, we store
605 # the name -> CGI key mapping here, instead of the reverse.
607 # XXX: Warning: If you touch this, check the search form for updating,
608 # too.
610 our @cgi_param_mapping = (
611 project => "p",
612 action => "a",
613 file_name => "f",
614 file_parent => "fp",
615 hash => "h",
616 hash_parent => "hp",
617 hash_base => "hb",
618 hash_parent_base => "hpb",
619 page => "pg",
620 order => "o",
621 searchtext => "s",
622 searchtype => "st",
623 snapshot_format => "sf",
624 extra_options => "opt",
625 search_use_regexp => "sr",
626 # this must be last entry (for manipulation from JavaScript)
627 javascript => "js"
629 our %cgi_param_mapping = @cgi_param_mapping;
631 # we will also need to know the possible actions, for validation
632 our %actions = (
633 "blame" => \&git_blame,
634 "blame_incremental" => \&git_blame_incremental,
635 "blame_data" => \&git_blame_data,
636 "blobdiff" => \&git_blobdiff,
637 "blobdiff_plain" => \&git_blobdiff_plain,
638 "blob" => \&git_blob,
639 "blob_plain" => \&git_blob_plain,
640 "commitdiff" => \&git_commitdiff,
641 "commitdiff_plain" => \&git_commitdiff_plain,
642 "commit" => \&git_commit,
643 "forks" => \&git_forks,
644 "heads" => \&git_heads,
645 "history" => \&git_history,
646 "log" => \&git_log,
647 "patch" => \&git_patch,
648 "patches" => \&git_patches,
649 "rss" => \&git_rss,
650 "atom" => \&git_atom,
651 "search" => \&git_search,
652 "search_help" => \&git_search_help,
653 "shortlog" => \&git_shortlog,
654 "summary" => \&git_summary,
655 "tag" => \&git_tag,
656 "tags" => \&git_tags,
657 "tree" => \&git_tree,
658 "snapshot" => \&git_snapshot,
659 "object" => \&git_object,
660 # those below don't need $project
661 "opml" => \&git_opml,
662 "project_list" => \&git_project_list,
663 "project_index" => \&git_project_index,
666 # finally, we have the hash of allowed extra_options for the commands that
667 # allow them
668 our %allowed_options = (
669 "--no-merges" => [ qw(rss atom log shortlog history) ],
672 # fill %input_params with the CGI parameters. All values except for 'opt'
673 # should be single values, but opt can be an array. We should probably
674 # build an array of parameters that can be multi-valued, but since for the time
675 # being it's only this one, we just single it out
676 while (my ($name, $symbol) = each %cgi_param_mapping) {
677 if ($symbol eq 'opt') {
678 $input_params{$name} = [ $cgi->param($symbol) ];
679 } else {
680 $input_params{$name} = $cgi->param($symbol);
684 # now read PATH_INFO and update the parameter list for missing parameters
685 sub evaluate_path_info {
686 return if defined $input_params{'project'};
687 return if !$path_info;
688 $path_info =~ s,^/+,,;
689 return if !$path_info;
691 # find which part of PATH_INFO is project
692 my $project = $path_info;
693 $project =~ s,/+$,,;
694 while ($project && !check_head_link("$projectroot/$project")) {
695 $project =~ s,/*[^/]*$,,;
697 return unless $project;
698 $input_params{'project'} = $project;
700 # do not change any parameters if an action is given using the query string
701 return if $input_params{'action'};
702 $path_info =~ s,^\Q$project\E/*,,;
704 # next, check if we have an action
705 my $action = $path_info;
706 $action =~ s,/.*$,,;
707 if (exists $actions{$action}) {
708 $path_info =~ s,^$action/*,,;
709 $input_params{'action'} = $action;
712 # list of actions that want hash_base instead of hash, but can have no
713 # pathname (f) parameter
714 my @wants_base = (
715 'tree',
716 'history',
719 # we want to catch
720 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
721 my ($parentrefname, $parentpathname, $refname, $pathname) =
722 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
724 # first, analyze the 'current' part
725 if (defined $pathname) {
726 # we got "branch:filename" or "branch:dir/"
727 # we could use git_get_type(branch:pathname), but:
728 # - it needs $git_dir
729 # - it does a git() call
730 # - the convention of terminating directories with a slash
731 # makes it superfluous
732 # - embedding the action in the PATH_INFO would make it even
733 # more superfluous
734 $pathname =~ s,^/+,,;
735 if (!$pathname || substr($pathname, -1) eq "/") {
736 $input_params{'action'} ||= "tree";
737 $pathname =~ s,/$,,;
738 } else {
739 # the default action depends on whether we had parent info
740 # or not
741 if ($parentrefname) {
742 $input_params{'action'} ||= "blobdiff_plain";
743 } else {
744 $input_params{'action'} ||= "blob_plain";
747 $input_params{'hash_base'} ||= $refname;
748 $input_params{'file_name'} ||= $pathname;
749 } elsif (defined $refname) {
750 # we got "branch". In this case we have to choose if we have to
751 # set hash or hash_base.
753 # Most of the actions without a pathname only want hash to be
754 # set, except for the ones specified in @wants_base that want
755 # hash_base instead. It should also be noted that hand-crafted
756 # links having 'history' as an action and no pathname or hash
757 # set will fail, but that happens regardless of PATH_INFO.
758 $input_params{'action'} ||= "shortlog";
759 if (grep { $_ eq $input_params{'action'} } @wants_base) {
760 $input_params{'hash_base'} ||= $refname;
761 } else {
762 $input_params{'hash'} ||= $refname;
766 # next, handle the 'parent' part, if present
767 if (defined $parentrefname) {
768 # a missing pathspec defaults to the 'current' filename, allowing e.g.
769 # someproject/blobdiff/oldrev..newrev:/filename
770 if ($parentpathname) {
771 $parentpathname =~ s,^/+,,;
772 $parentpathname =~ s,/$,,;
773 $input_params{'file_parent'} ||= $parentpathname;
774 } else {
775 $input_params{'file_parent'} ||= $input_params{'file_name'};
777 # we assume that hash_parent_base is wanted if a path was specified,
778 # or if the action wants hash_base instead of hash
779 if (defined $input_params{'file_parent'} ||
780 grep { $_ eq $input_params{'action'} } @wants_base) {
781 $input_params{'hash_parent_base'} ||= $parentrefname;
782 } else {
783 $input_params{'hash_parent'} ||= $parentrefname;
787 # for the snapshot action, we allow URLs in the form
788 # $project/snapshot/$hash.ext
789 # where .ext determines the snapshot and gets removed from the
790 # passed $refname to provide the $hash.
792 # To be able to tell that $refname includes the format extension, we
793 # require the following two conditions to be satisfied:
794 # - the hash input parameter MUST have been set from the $refname part
795 # of the URL (i.e. they must be equal)
796 # - the snapshot format MUST NOT have been defined already (e.g. from
797 # CGI parameter sf)
798 # It's also useless to try any matching unless $refname has a dot,
799 # so we check for that too
800 if (defined $input_params{'action'} &&
801 $input_params{'action'} eq 'snapshot' &&
802 defined $refname && index($refname, '.') != -1 &&
803 $refname eq $input_params{'hash'} &&
804 !defined $input_params{'snapshot_format'}) {
805 # We loop over the known snapshot formats, checking for
806 # extensions. Allowed extensions are both the defined suffix
807 # (which includes the initial dot already) and the snapshot
808 # format key itself, with a prepended dot
809 while (my ($fmt, $opt) = each %known_snapshot_formats) {
810 my $hash = $refname;
811 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
812 next;
814 my $sfx = $1;
815 # a valid suffix was found, so set the snapshot format
816 # and reset the hash parameter
817 $input_params{'snapshot_format'} = $fmt;
818 $input_params{'hash'} = $hash;
819 # we also set the format suffix to the one requested
820 # in the URL: this way a request for e.g. .tgz returns
821 # a .tgz instead of a .tar.gz
822 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
823 last;
827 evaluate_path_info();
829 our $action = $input_params{'action'};
830 if (defined $action) {
831 if (!validate_action($action)) {
832 die_error(400, "Invalid action parameter");
836 # parameters which are pathnames
837 our $project = $input_params{'project'};
838 if (defined $project) {
839 if (!validate_project($project)) {
840 undef $project;
841 die_error(404, "No such project");
845 our $file_name = $input_params{'file_name'};
846 if (defined $file_name) {
847 if (!validate_pathname($file_name)) {
848 die_error(400, "Invalid file parameter");
852 our $file_parent = $input_params{'file_parent'};
853 if (defined $file_parent) {
854 if (!validate_pathname($file_parent)) {
855 die_error(400, "Invalid file parent parameter");
859 # parameters which are refnames
860 our $hash = $input_params{'hash'};
861 if (defined $hash) {
862 if (!validate_refname($hash)) {
863 die_error(400, "Invalid hash parameter");
867 our $hash_parent = $input_params{'hash_parent'};
868 if (defined $hash_parent) {
869 if (!validate_refname($hash_parent)) {
870 die_error(400, "Invalid hash parent parameter");
874 our $hash_base = $input_params{'hash_base'};
875 if (defined $hash_base) {
876 if (!validate_refname($hash_base)) {
877 die_error(400, "Invalid hash base parameter");
881 our @extra_options = @{$input_params{'extra_options'}};
882 # @extra_options is always defined, since it can only be (currently) set from
883 # CGI, and $cgi->param() returns the empty array in array context if the param
884 # is not set
885 foreach my $opt (@extra_options) {
886 if (not exists $allowed_options{$opt}) {
887 die_error(400, "Invalid option parameter");
889 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
890 die_error(400, "Invalid option parameter for this action");
894 our $hash_parent_base = $input_params{'hash_parent_base'};
895 if (defined $hash_parent_base) {
896 if (!validate_refname($hash_parent_base)) {
897 die_error(400, "Invalid hash parent base parameter");
901 # other parameters
902 our $page = $input_params{'page'};
903 if (defined $page) {
904 if ($page =~ m/[^0-9]/) {
905 die_error(400, "Invalid page parameter");
909 our $searchtype = $input_params{'searchtype'};
910 if (defined $searchtype) {
911 if ($searchtype =~ m/[^a-z]/) {
912 die_error(400, "Invalid searchtype parameter");
916 our $search_use_regexp = $input_params{'search_use_regexp'};
918 our $searchtext = $input_params{'searchtext'};
919 our $search_regexp;
920 if (defined $searchtext) {
921 if (length($searchtext) < 2) {
922 die_error(403, "At least two characters are required for search parameter");
924 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
927 # path to the current git repository
928 our $git_dir;
929 $git_dir = "$projectroot/$project" if $project;
931 # list of supported snapshot formats
932 our @snapshot_fmts = gitweb_get_feature('snapshot');
933 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
935 # check that the avatar feature is set to a known provider name,
936 # and for each provider check if the dependencies are satisfied.
937 # if the provider name is invalid or the dependencies are not met,
938 # reset $git_avatar to the empty string.
939 our ($git_avatar) = gitweb_get_feature('avatar');
940 if ($git_avatar eq 'gravatar') {
941 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
942 } elsif ($git_avatar eq 'picon') {
943 # no dependencies
944 } else {
945 $git_avatar = '';
948 # dispatch
949 if (!defined $action) {
950 if (defined $hash) {
951 $action = git_get_type($hash);
952 } elsif (defined $hash_base && defined $file_name) {
953 $action = git_get_type("$hash_base:$file_name");
954 } elsif (defined $project) {
955 $action = 'summary';
956 } else {
957 $action = 'project_list';
960 if (!defined($actions{$action})) {
961 die_error(400, "Unknown action");
963 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
964 !$project) {
965 die_error(400, "Project needed");
967 $actions{$action}->();
968 exit;
970 ## ======================================================================
971 ## action links
973 # possible values of extra options
974 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
975 # -replay => 1 - start from a current view (replay with modifications)
976 # -path_info => 0|1 - don't use/use path_info URL (if possible)
977 sub href {
978 my %params = @_;
979 # default is to use -absolute url() i.e. $my_uri
980 my $href = $params{-full} ? $my_url : $my_uri;
982 $params{'project'} = $project unless exists $params{'project'};
984 if ($params{-replay}) {
985 while (my ($name, $symbol) = each %cgi_param_mapping) {
986 if (!exists $params{$name}) {
987 $params{$name} = $input_params{$name};
992 my $use_pathinfo = gitweb_check_feature('pathinfo');
993 if (defined $params{'project'} &&
994 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
995 # try to put as many parameters as possible in PATH_INFO:
996 # - project name
997 # - action
998 # - hash_parent or hash_parent_base:/file_parent
999 # - hash or hash_base:/filename
1000 # - the snapshot_format as an appropriate suffix
1002 # When the script is the root DirectoryIndex for the domain,
1003 # $href here would be something like http://gitweb.example.com/
1004 # Thus, we strip any trailing / from $href, to spare us double
1005 # slashes in the final URL
1006 $href =~ s,/$,,;
1008 # Then add the project name, if present
1009 $href .= "/".esc_url($params{'project'});
1010 delete $params{'project'};
1012 # since we destructively absorb parameters, we keep this
1013 # boolean that remembers if we're handling a snapshot
1014 my $is_snapshot = $params{'action'} eq 'snapshot';
1016 # Summary just uses the project path URL, any other action is
1017 # added to the URL
1018 if (defined $params{'action'}) {
1019 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
1020 delete $params{'action'};
1023 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1024 # stripping nonexistent or useless pieces
1025 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1026 || $params{'hash_parent'} || $params{'hash'});
1027 if (defined $params{'hash_base'}) {
1028 if (defined $params{'hash_parent_base'}) {
1029 $href .= esc_url($params{'hash_parent_base'});
1030 # skip the file_parent if it's the same as the file_name
1031 if (defined $params{'file_parent'}) {
1032 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1033 delete $params{'file_parent'};
1034 } elsif ($params{'file_parent'} !~ /\.\./) {
1035 $href .= ":/".esc_url($params{'file_parent'});
1036 delete $params{'file_parent'};
1039 $href .= "..";
1040 delete $params{'hash_parent'};
1041 delete $params{'hash_parent_base'};
1042 } elsif (defined $params{'hash_parent'}) {
1043 $href .= esc_url($params{'hash_parent'}). "..";
1044 delete $params{'hash_parent'};
1047 $href .= esc_url($params{'hash_base'});
1048 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1049 $href .= ":/".esc_url($params{'file_name'});
1050 delete $params{'file_name'};
1052 delete $params{'hash'};
1053 delete $params{'hash_base'};
1054 } elsif (defined $params{'hash'}) {
1055 $href .= esc_url($params{'hash'});
1056 delete $params{'hash'};
1059 # If the action was a snapshot, we can absorb the
1060 # snapshot_format parameter too
1061 if ($is_snapshot) {
1062 my $fmt = $params{'snapshot_format'};
1063 # snapshot_format should always be defined when href()
1064 # is called, but just in case some code forgets, we
1065 # fall back to the default
1066 $fmt ||= $snapshot_fmts[0];
1067 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1068 delete $params{'snapshot_format'};
1072 # now encode the parameters explicitly
1073 my @result = ();
1074 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1075 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1076 if (defined $params{$name}) {
1077 if (ref($params{$name}) eq "ARRAY") {
1078 foreach my $par (@{$params{$name}}) {
1079 push @result, $symbol . "=" . esc_param($par);
1081 } else {
1082 push @result, $symbol . "=" . esc_param($params{$name});
1086 $href .= "?" . join(';', @result) if scalar @result;
1088 return $href;
1092 ## ======================================================================
1093 ## validation, quoting/unquoting and escaping
1095 sub validate_action {
1096 my $input = shift || return undef;
1097 return undef unless exists $actions{$input};
1098 return $input;
1101 sub validate_project {
1102 my $input = shift || return undef;
1103 if (!validate_pathname($input) ||
1104 !(-d "$projectroot/$input") ||
1105 !check_export_ok("$projectroot/$input") ||
1106 ($strict_export && !project_in_list($input))) {
1107 return undef;
1108 } else {
1109 return $input;
1113 sub validate_pathname {
1114 my $input = shift || return undef;
1116 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1117 # at the beginning, at the end, and between slashes.
1118 # also this catches doubled slashes
1119 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1120 return undef;
1122 # no null characters
1123 if ($input =~ m!\0!) {
1124 return undef;
1126 return $input;
1129 sub validate_refname {
1130 my $input = shift || return undef;
1132 # textual hashes are O.K.
1133 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1134 return $input;
1136 # it must be correct pathname
1137 $input = validate_pathname($input)
1138 or return undef;
1139 # restrictions on ref name according to git-check-ref-format
1140 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1141 return undef;
1143 return $input;
1146 # decode sequences of octets in utf8 into Perl's internal form,
1147 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1148 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1149 sub to_utf8 {
1150 my $str = shift;
1151 if (utf8::valid($str)) {
1152 utf8::decode($str);
1153 return $str;
1154 } else {
1155 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1159 # quote unsafe chars, but keep the slash, even when it's not
1160 # correct, but quoted slashes look too horrible in bookmarks
1161 sub esc_param {
1162 my $str = shift;
1163 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1164 $str =~ s/ /\+/g;
1165 return $str;
1168 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
1169 sub esc_url {
1170 my $str = shift;
1171 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1172 $str =~ s/\+/%2B/g;
1173 $str =~ s/ /\+/g;
1174 return $str;
1177 # replace invalid utf8 character with SUBSTITUTION sequence
1178 sub esc_html {
1179 my $str = shift;
1180 my %opts = @_;
1182 $str = to_utf8($str);
1183 $str = $cgi->escapeHTML($str);
1184 if ($opts{'-nbsp'}) {
1185 $str =~ s/ /&nbsp;/g;
1187 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1188 return $str;
1191 # quote control characters and escape filename to HTML
1192 sub esc_path {
1193 my $str = shift;
1194 my %opts = @_;
1196 $str = to_utf8($str);
1197 $str = $cgi->escapeHTML($str);
1198 if ($opts{'-nbsp'}) {
1199 $str =~ s/ /&nbsp;/g;
1201 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1202 return $str;
1205 # Make control characters "printable", using character escape codes (CEC)
1206 sub quot_cec {
1207 my $cntrl = shift;
1208 my %opts = @_;
1209 my %es = ( # character escape codes, aka escape sequences
1210 "\t" => '\t', # tab (HT)
1211 "\n" => '\n', # line feed (LF)
1212 "\r" => '\r', # carrige return (CR)
1213 "\f" => '\f', # form feed (FF)
1214 "\b" => '\b', # backspace (BS)
1215 "\a" => '\a', # alarm (bell) (BEL)
1216 "\e" => '\e', # escape (ESC)
1217 "\013" => '\v', # vertical tab (VT)
1218 "\000" => '\0', # nul character (NUL)
1220 my $chr = ( (exists $es{$cntrl})
1221 ? $es{$cntrl}
1222 : sprintf('\%2x', ord($cntrl)) );
1223 if ($opts{-nohtml}) {
1224 return $chr;
1225 } else {
1226 return "<span class=\"cntrl\">$chr</span>";
1230 # Alternatively use unicode control pictures codepoints,
1231 # Unicode "printable representation" (PR)
1232 sub quot_upr {
1233 my $cntrl = shift;
1234 my %opts = @_;
1236 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1237 if ($opts{-nohtml}) {
1238 return $chr;
1239 } else {
1240 return "<span class=\"cntrl\">$chr</span>";
1244 # git may return quoted and escaped filenames
1245 sub unquote {
1246 my $str = shift;
1248 sub unq {
1249 my $seq = shift;
1250 my %es = ( # character escape codes, aka escape sequences
1251 't' => "\t", # tab (HT, TAB)
1252 'n' => "\n", # newline (NL)
1253 'r' => "\r", # return (CR)
1254 'f' => "\f", # form feed (FF)
1255 'b' => "\b", # backspace (BS)
1256 'a' => "\a", # alarm (bell) (BEL)
1257 'e' => "\e", # escape (ESC)
1258 'v' => "\013", # vertical tab (VT)
1261 if ($seq =~ m/^[0-7]{1,3}$/) {
1262 # octal char sequence
1263 return chr(oct($seq));
1264 } elsif (exists $es{$seq}) {
1265 # C escape sequence, aka character escape code
1266 return $es{$seq};
1268 # quoted ordinary character
1269 return $seq;
1272 if ($str =~ m/^"(.*)"$/) {
1273 # needs unquoting
1274 $str = $1;
1275 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1277 return $str;
1280 # escape tabs (convert tabs to spaces)
1281 sub untabify {
1282 my $line = shift;
1284 while ((my $pos = index($line, "\t")) != -1) {
1285 if (my $count = (8 - ($pos % 8))) {
1286 my $spaces = ' ' x $count;
1287 $line =~ s/\t/$spaces/;
1291 return $line;
1294 sub project_in_list {
1295 my $project = shift;
1296 my @list = git_get_projects_list();
1297 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1300 ## ----------------------------------------------------------------------
1301 ## HTML aware string manipulation
1303 # Try to chop given string on a word boundary between position
1304 # $len and $len+$add_len. If there is no word boundary there,
1305 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1306 # (marking chopped part) would be longer than given string.
1307 sub chop_str {
1308 my $str = shift;
1309 my $len = shift;
1310 my $add_len = shift || 10;
1311 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1313 # Make sure perl knows it is utf8 encoded so we don't
1314 # cut in the middle of a utf8 multibyte char.
1315 $str = to_utf8($str);
1317 # allow only $len chars, but don't cut a word if it would fit in $add_len
1318 # if it doesn't fit, cut it if it's still longer than the dots we would add
1319 # remove chopped character entities entirely
1321 # when chopping in the middle, distribute $len into left and right part
1322 # return early if chopping wouldn't make string shorter
1323 if ($where eq 'center') {
1324 return $str if ($len + 5 >= length($str)); # filler is length 5
1325 $len = int($len/2);
1326 } else {
1327 return $str if ($len + 4 >= length($str)); # filler is length 4
1330 # regexps: ending and beginning with word part up to $add_len
1331 my $endre = qr/.{$len}\w{0,$add_len}/;
1332 my $begre = qr/\w{0,$add_len}.{$len}/;
1334 if ($where eq 'left') {
1335 $str =~ m/^(.*?)($begre)$/;
1336 my ($lead, $body) = ($1, $2);
1337 if (length($lead) > 4) {
1338 $lead = " ...";
1340 return "$lead$body";
1342 } elsif ($where eq 'center') {
1343 $str =~ m/^($endre)(.*)$/;
1344 my ($left, $str) = ($1, $2);
1345 $str =~ m/^(.*?)($begre)$/;
1346 my ($mid, $right) = ($1, $2);
1347 if (length($mid) > 5) {
1348 $mid = " ... ";
1350 return "$left$mid$right";
1352 } else {
1353 $str =~ m/^($endre)(.*)$/;
1354 my $body = $1;
1355 my $tail = $2;
1356 if (length($tail) > 4) {
1357 $tail = "... ";
1359 return "$body$tail";
1363 # takes the same arguments as chop_str, but also wraps a <span> around the
1364 # result with a title attribute if it does get chopped. Additionally, the
1365 # string is HTML-escaped.
1366 sub chop_and_escape_str {
1367 my ($str) = @_;
1369 my $chopped = chop_str(@_);
1370 if ($chopped eq $str) {
1371 return esc_html($chopped);
1372 } else {
1373 $str =~ s/[[:cntrl:]]/?/g;
1374 return $cgi->span({-title=>$str}, esc_html($chopped));
1378 ## ----------------------------------------------------------------------
1379 ## functions returning short strings
1381 # CSS class for given age value (in seconds)
1382 sub age_class {
1383 my $age = shift;
1385 if (!defined $age) {
1386 return "noage";
1387 } elsif ($age < 60*60*2) {
1388 return "age0";
1389 } elsif ($age < 60*60*24*2) {
1390 return "age1";
1391 } else {
1392 return "age2";
1396 # convert age in seconds to "nn units ago" string
1397 sub age_string {
1398 my $age = shift;
1399 my $age_str;
1401 if ($age > 60*60*24*365*2) {
1402 $age_str = (int $age/60/60/24/365);
1403 $age_str .= " years ago";
1404 } elsif ($age > 60*60*24*(365/12)*2) {
1405 $age_str = int $age/60/60/24/(365/12);
1406 $age_str .= " months ago";
1407 } elsif ($age > 60*60*24*7*2) {
1408 $age_str = int $age/60/60/24/7;
1409 $age_str .= " weeks ago";
1410 } elsif ($age > 60*60*24*2) {
1411 $age_str = int $age/60/60/24;
1412 $age_str .= " days ago";
1413 } elsif ($age > 60*60*2) {
1414 $age_str = int $age/60/60;
1415 $age_str .= " hours ago";
1416 } elsif ($age > 60*2) {
1417 $age_str = int $age/60;
1418 $age_str .= " min ago";
1419 } elsif ($age > 2) {
1420 $age_str = int $age;
1421 $age_str .= " sec ago";
1422 } else {
1423 $age_str .= " right now";
1425 return $age_str;
1428 use constant {
1429 S_IFINVALID => 0030000,
1430 S_IFGITLINK => 0160000,
1433 # submodule/subproject, a commit object reference
1434 sub S_ISGITLINK {
1435 my $mode = shift;
1437 return (($mode & S_IFMT) == S_IFGITLINK)
1440 # convert file mode in octal to symbolic file mode string
1441 sub mode_str {
1442 my $mode = oct shift;
1444 if (S_ISGITLINK($mode)) {
1445 return 'm---------';
1446 } elsif (S_ISDIR($mode & S_IFMT)) {
1447 return 'drwxr-xr-x';
1448 } elsif (S_ISLNK($mode)) {
1449 return 'lrwxrwxrwx';
1450 } elsif (S_ISREG($mode)) {
1451 # git cares only about the executable bit
1452 if ($mode & S_IXUSR) {
1453 return '-rwxr-xr-x';
1454 } else {
1455 return '-rw-r--r--';
1457 } else {
1458 return '----------';
1462 # convert file mode in octal to file type string
1463 sub file_type {
1464 my $mode = shift;
1466 if ($mode !~ m/^[0-7]+$/) {
1467 return $mode;
1468 } else {
1469 $mode = oct $mode;
1472 if (S_ISGITLINK($mode)) {
1473 return "submodule";
1474 } elsif (S_ISDIR($mode & S_IFMT)) {
1475 return "directory";
1476 } elsif (S_ISLNK($mode)) {
1477 return "symlink";
1478 } elsif (S_ISREG($mode)) {
1479 return "file";
1480 } else {
1481 return "unknown";
1485 # convert file mode in octal to file type description string
1486 sub file_type_long {
1487 my $mode = shift;
1489 if ($mode !~ m/^[0-7]+$/) {
1490 return $mode;
1491 } else {
1492 $mode = oct $mode;
1495 if (S_ISGITLINK($mode)) {
1496 return "submodule";
1497 } elsif (S_ISDIR($mode & S_IFMT)) {
1498 return "directory";
1499 } elsif (S_ISLNK($mode)) {
1500 return "symlink";
1501 } elsif (S_ISREG($mode)) {
1502 if ($mode & S_IXUSR) {
1503 return "executable";
1504 } else {
1505 return "file";
1507 } else {
1508 return "unknown";
1513 ## ----------------------------------------------------------------------
1514 ## functions returning short HTML fragments, or transforming HTML fragments
1515 ## which don't belong to other sections
1517 # format line of commit message.
1518 sub format_log_line_html {
1519 my $line = shift;
1521 $line = esc_html($line, -nbsp=>1);
1522 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1523 $cgi->a({-href => href(action=>"object", hash=>$1),
1524 -class => "text"}, $1);
1525 }eg;
1527 return $line;
1530 # format marker of refs pointing to given object
1532 # the destination action is chosen based on object type and current context:
1533 # - for annotated tags, we choose the tag view unless it's the current view
1534 # already, in which case we go to shortlog view
1535 # - for other refs, we keep the current view if we're in history, shortlog or
1536 # log view, and select shortlog otherwise
1537 sub format_ref_marker {
1538 my ($refs, $id) = @_;
1539 my $markers = '';
1541 if (defined $refs->{$id}) {
1542 foreach my $ref (@{$refs->{$id}}) {
1543 # this code exploits the fact that non-lightweight tags are the
1544 # only indirect objects, and that they are the only objects for which
1545 # we want to use tag instead of shortlog as action
1546 my ($type, $name) = qw();
1547 my $indirect = ($ref =~ s/\^\{\}$//);
1548 # e.g. tags/v2.6.11 or heads/next
1549 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1550 $type = $1;
1551 $name = $2;
1552 } else {
1553 $type = "ref";
1554 $name = $ref;
1557 my $class = $type;
1558 $class .= " indirect" if $indirect;
1560 my $dest_action = "shortlog";
1562 if ($indirect) {
1563 $dest_action = "tag" unless $action eq "tag";
1564 } elsif ($action =~ /^(history|(short)?log)$/) {
1565 $dest_action = $action;
1568 my $dest = "";
1569 $dest .= "refs/" unless $ref =~ m!^refs/!;
1570 $dest .= $ref;
1572 my $link = $cgi->a({
1573 -href => href(
1574 action=>$dest_action,
1575 hash=>$dest
1576 )}, $name);
1578 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1579 $link . "</span>";
1583 if ($markers) {
1584 return ' <span class="refs">'. $markers . '</span>';
1585 } else {
1586 return "";
1590 # format, perhaps shortened and with markers, title line
1591 sub format_subject_html {
1592 my ($long, $short, $href, $extra) = @_;
1593 $extra = '' unless defined($extra);
1595 if (length($short) < length($long)) {
1596 $long =~ s/[[:cntrl:]]/?/g;
1597 return $cgi->a({-href => $href, -class => "list subject",
1598 -title => to_utf8($long)},
1599 esc_html($short)) . $extra;
1600 } else {
1601 return $cgi->a({-href => $href, -class => "list subject"},
1602 esc_html($long)) . $extra;
1606 # Rather than recomputing the url for an email multiple times, we cache it
1607 # after the first hit. This gives a visible benefit in views where the avatar
1608 # for the same email is used repeatedly (e.g. shortlog).
1609 # The cache is shared by all avatar engines (currently gravatar only), which
1610 # are free to use it as preferred. Since only one avatar engine is used for any
1611 # given page, there's no risk for cache conflicts.
1612 our %avatar_cache = ();
1614 # Compute the picon url for a given email, by using the picon search service over at
1615 # http://www.cs.indiana.edu/picons/search.html
1616 sub picon_url {
1617 my $email = lc shift;
1618 if (!$avatar_cache{$email}) {
1619 my ($user, $domain) = split('@', $email);
1620 $avatar_cache{$email} =
1621 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1622 "$domain/$user/" .
1623 "users+domains+unknown/up/single";
1625 return $avatar_cache{$email};
1628 # Compute the gravatar url for a given email, if it's not in the cache already.
1629 # Gravatar stores only the part of the URL before the size, since that's the
1630 # one computationally more expensive. This also allows reuse of the cache for
1631 # different sizes (for this particular engine).
1632 sub gravatar_url {
1633 my $email = lc shift;
1634 my $size = shift;
1635 $avatar_cache{$email} ||=
1636 "http://www.gravatar.com/avatar/" .
1637 Digest::MD5::md5_hex($email) . "?s=";
1638 return $avatar_cache{$email} . $size;
1641 # Insert an avatar for the given $email at the given $size if the feature
1642 # is enabled.
1643 sub git_get_avatar {
1644 my ($email, %opts) = @_;
1645 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1646 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1647 $opts{-size} ||= 'default';
1648 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1649 my $url = "";
1650 if ($git_avatar eq 'gravatar') {
1651 $url = gravatar_url($email, $size);
1652 } elsif ($git_avatar eq 'picon') {
1653 $url = picon_url($email);
1655 # Other providers can be added by extending the if chain, defining $url
1656 # as needed. If no variant puts something in $url, we assume avatars
1657 # are completely disabled/unavailable.
1658 if ($url) {
1659 return $pre_white .
1660 "<img width=\"$size\" " .
1661 "class=\"avatar\" " .
1662 "src=\"$url\" " .
1663 "alt=\"\" " .
1664 "/>" . $post_white;
1665 } else {
1666 return "";
1670 sub format_search_author {
1671 my ($author, $searchtype, $displaytext) = @_;
1672 my $have_search = gitweb_check_feature('search');
1674 if ($have_search) {
1675 my $performed = "";
1676 if ($searchtype eq 'author') {
1677 $performed = "authored";
1678 } elsif ($searchtype eq 'committer') {
1679 $performed = "committed";
1682 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1683 searchtext=>$author,
1684 searchtype=>$searchtype), class=>"list",
1685 title=>"Search for commits $performed by $author"},
1686 $displaytext);
1688 } else {
1689 return $displaytext;
1693 # format the author name of the given commit with the given tag
1694 # the author name is chopped and escaped according to the other
1695 # optional parameters (see chop_str).
1696 sub format_author_html {
1697 my $tag = shift;
1698 my $co = shift;
1699 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1700 return "<$tag class=\"author\">" .
1701 format_search_author($co->{'author_name'}, "author",
1702 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1703 $author) .
1704 "</$tag>";
1707 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1708 sub format_git_diff_header_line {
1709 my $line = shift;
1710 my $diffinfo = shift;
1711 my ($from, $to) = @_;
1713 if ($diffinfo->{'nparents'}) {
1714 # combined diff
1715 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1716 if ($to->{'href'}) {
1717 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1718 esc_path($to->{'file'}));
1719 } else { # file was deleted (no href)
1720 $line .= esc_path($to->{'file'});
1722 } else {
1723 # "ordinary" diff
1724 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1725 if ($from->{'href'}) {
1726 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1727 'a/' . esc_path($from->{'file'}));
1728 } else { # file was added (no href)
1729 $line .= 'a/' . esc_path($from->{'file'});
1731 $line .= ' ';
1732 if ($to->{'href'}) {
1733 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1734 'b/' . esc_path($to->{'file'}));
1735 } else { # file was deleted
1736 $line .= 'b/' . esc_path($to->{'file'});
1740 return "<div class=\"diff header\">$line</div>\n";
1743 # format extended diff header line, before patch itself
1744 sub format_extended_diff_header_line {
1745 my $line = shift;
1746 my $diffinfo = shift;
1747 my ($from, $to) = @_;
1749 # match <path>
1750 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1751 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1752 esc_path($from->{'file'}));
1754 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1755 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1756 esc_path($to->{'file'}));
1758 # match single <mode>
1759 if ($line =~ m/\s(\d{6})$/) {
1760 $line .= '<span class="info"> (' .
1761 file_type_long($1) .
1762 ')</span>';
1764 # match <hash>
1765 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1766 # can match only for combined diff
1767 $line = 'index ';
1768 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1769 if ($from->{'href'}[$i]) {
1770 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1771 -class=>"hash"},
1772 substr($diffinfo->{'from_id'}[$i],0,7));
1773 } else {
1774 $line .= '0' x 7;
1776 # separator
1777 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1779 $line .= '..';
1780 if ($to->{'href'}) {
1781 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1782 substr($diffinfo->{'to_id'},0,7));
1783 } else {
1784 $line .= '0' x 7;
1787 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1788 # can match only for ordinary diff
1789 my ($from_link, $to_link);
1790 if ($from->{'href'}) {
1791 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1792 substr($diffinfo->{'from_id'},0,7));
1793 } else {
1794 $from_link = '0' x 7;
1796 if ($to->{'href'}) {
1797 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1798 substr($diffinfo->{'to_id'},0,7));
1799 } else {
1800 $to_link = '0' x 7;
1802 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1803 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1806 return $line . "<br/>\n";
1809 # format from-file/to-file diff header
1810 sub format_diff_from_to_header {
1811 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1812 my $line;
1813 my $result = '';
1815 $line = $from_line;
1816 #assert($line =~ m/^---/) if DEBUG;
1817 # no extra formatting for "^--- /dev/null"
1818 if (! $diffinfo->{'nparents'}) {
1819 # ordinary (single parent) diff
1820 if ($line =~ m!^--- "?a/!) {
1821 if ($from->{'href'}) {
1822 $line = '--- a/' .
1823 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1824 esc_path($from->{'file'}));
1825 } else {
1826 $line = '--- a/' .
1827 esc_path($from->{'file'});
1830 $result .= qq!<div class="diff from_file">$line</div>\n!;
1832 } else {
1833 # combined diff (merge commit)
1834 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1835 if ($from->{'href'}[$i]) {
1836 $line = '--- ' .
1837 $cgi->a({-href=>href(action=>"blobdiff",
1838 hash_parent=>$diffinfo->{'from_id'}[$i],
1839 hash_parent_base=>$parents[$i],
1840 file_parent=>$from->{'file'}[$i],
1841 hash=>$diffinfo->{'to_id'},
1842 hash_base=>$hash,
1843 file_name=>$to->{'file'}),
1844 -class=>"path",
1845 -title=>"diff" . ($i+1)},
1846 $i+1) .
1847 '/' .
1848 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1849 esc_path($from->{'file'}[$i]));
1850 } else {
1851 $line = '--- /dev/null';
1853 $result .= qq!<div class="diff from_file">$line</div>\n!;
1857 $line = $to_line;
1858 #assert($line =~ m/^\+\+\+/) if DEBUG;
1859 # no extra formatting for "^+++ /dev/null"
1860 if ($line =~ m!^\+\+\+ "?b/!) {
1861 if ($to->{'href'}) {
1862 $line = '+++ b/' .
1863 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1864 esc_path($to->{'file'}));
1865 } else {
1866 $line = '+++ b/' .
1867 esc_path($to->{'file'});
1870 $result .= qq!<div class="diff to_file">$line</div>\n!;
1872 return $result;
1875 # create note for patch simplified by combined diff
1876 sub format_diff_cc_simplified {
1877 my ($diffinfo, @parents) = @_;
1878 my $result = '';
1880 $result .= "<div class=\"diff header\">" .
1881 "diff --cc ";
1882 if (!is_deleted($diffinfo)) {
1883 $result .= $cgi->a({-href => href(action=>"blob",
1884 hash_base=>$hash,
1885 hash=>$diffinfo->{'to_id'},
1886 file_name=>$diffinfo->{'to_file'}),
1887 -class => "path"},
1888 esc_path($diffinfo->{'to_file'}));
1889 } else {
1890 $result .= esc_path($diffinfo->{'to_file'});
1892 $result .= "</div>\n" . # class="diff header"
1893 "<div class=\"diff nodifferences\">" .
1894 "Simple merge" .
1895 "</div>\n"; # class="diff nodifferences"
1897 return $result;
1900 # format patch (diff) line (not to be used for diff headers)
1901 sub format_diff_line {
1902 my $line = shift;
1903 my ($from, $to) = @_;
1904 my $diff_class = "";
1906 chomp $line;
1908 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1909 # combined diff
1910 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1911 if ($line =~ m/^\@{3}/) {
1912 $diff_class = " chunk_header";
1913 } elsif ($line =~ m/^\\/) {
1914 $diff_class = " incomplete";
1915 } elsif ($prefix =~ tr/+/+/) {
1916 $diff_class = " add";
1917 } elsif ($prefix =~ tr/-/-/) {
1918 $diff_class = " rem";
1920 } else {
1921 # assume ordinary diff
1922 my $char = substr($line, 0, 1);
1923 if ($char eq '+') {
1924 $diff_class = " add";
1925 } elsif ($char eq '-') {
1926 $diff_class = " rem";
1927 } elsif ($char eq '@') {
1928 $diff_class = " chunk_header";
1929 } elsif ($char eq "\\") {
1930 $diff_class = " incomplete";
1933 $line = untabify($line);
1934 if ($from && $to && $line =~ m/^\@{2} /) {
1935 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1936 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1938 $from_lines = 0 unless defined $from_lines;
1939 $to_lines = 0 unless defined $to_lines;
1941 if ($from->{'href'}) {
1942 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1943 -class=>"list"}, $from_text);
1945 if ($to->{'href'}) {
1946 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1947 -class=>"list"}, $to_text);
1949 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1950 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1951 return "<div class=\"diff$diff_class\">$line</div>\n";
1952 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1953 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1954 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1956 @from_text = split(' ', $ranges);
1957 for (my $i = 0; $i < @from_text; ++$i) {
1958 ($from_start[$i], $from_nlines[$i]) =
1959 (split(',', substr($from_text[$i], 1)), 0);
1962 $to_text = pop @from_text;
1963 $to_start = pop @from_start;
1964 $to_nlines = pop @from_nlines;
1966 $line = "<span class=\"chunk_info\">$prefix ";
1967 for (my $i = 0; $i < @from_text; ++$i) {
1968 if ($from->{'href'}[$i]) {
1969 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1970 -class=>"list"}, $from_text[$i]);
1971 } else {
1972 $line .= $from_text[$i];
1974 $line .= " ";
1976 if ($to->{'href'}) {
1977 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1978 -class=>"list"}, $to_text);
1979 } else {
1980 $line .= $to_text;
1982 $line .= " $prefix</span>" .
1983 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1984 return "<div class=\"diff$diff_class\">$line</div>\n";
1986 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1989 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1990 # linked. Pass the hash of the tree/commit to snapshot.
1991 sub format_snapshot_links {
1992 my ($hash) = @_;
1993 my $num_fmts = @snapshot_fmts;
1994 if ($num_fmts > 1) {
1995 # A parenthesized list of links bearing format names.
1996 # e.g. "snapshot (_tar.gz_ _zip_)"
1997 return "snapshot (" . join(' ', map
1998 $cgi->a({
1999 -href => href(
2000 action=>"snapshot",
2001 hash=>$hash,
2002 snapshot_format=>$_
2004 }, $known_snapshot_formats{$_}{'display'})
2005 , @snapshot_fmts) . ")";
2006 } elsif ($num_fmts == 1) {
2007 # A single "snapshot" link whose tooltip bears the format name.
2008 # i.e. "_snapshot_"
2009 my ($fmt) = @snapshot_fmts;
2010 return
2011 $cgi->a({
2012 -href => href(
2013 action=>"snapshot",
2014 hash=>$hash,
2015 snapshot_format=>$fmt
2017 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2018 }, "snapshot");
2019 } else { # $num_fmts == 0
2020 return undef;
2024 ## ......................................................................
2025 ## functions returning values to be passed, perhaps after some
2026 ## transformation, to other functions; e.g. returning arguments to href()
2028 # returns hash to be passed to href to generate gitweb URL
2029 # in -title key it returns description of link
2030 sub get_feed_info {
2031 my $format = shift || 'Atom';
2032 my %res = (action => lc($format));
2034 # feed links are possible only for project views
2035 return unless (defined $project);
2036 # some views should link to OPML, or to generic project feed,
2037 # or don't have specific feed yet (so they should use generic)
2038 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2040 my $branch;
2041 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2042 # from tag links; this also makes possible to detect branch links
2043 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2044 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2045 $branch = $1;
2047 # find log type for feed description (title)
2048 my $type = 'log';
2049 if (defined $file_name) {
2050 $type = "history of $file_name";
2051 $type .= "/" if ($action eq 'tree');
2052 $type .= " on '$branch'" if (defined $branch);
2053 } else {
2054 $type = "log of $branch" if (defined $branch);
2057 $res{-title} = $type;
2058 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2059 $res{'file_name'} = $file_name;
2061 return %res;
2064 ## ----------------------------------------------------------------------
2065 ## git utility subroutines, invoking git commands
2067 # returns path to the core git executable and the --git-dir parameter as list
2068 sub git_cmd {
2069 $number_of_git_cmds++;
2070 return $GIT, '--git-dir='.$git_dir;
2073 # quote the given arguments for passing them to the shell
2074 # quote_command("command", "arg 1", "arg with ' and ! characters")
2075 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2076 # Try to avoid using this function wherever possible.
2077 sub quote_command {
2078 return join(' ',
2079 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2082 # get HEAD ref of given project as hash
2083 sub git_get_head_hash {
2084 return git_get_full_hash(shift, 'HEAD');
2087 sub git_get_full_hash {
2088 return git_get_hash(@_);
2091 sub git_get_short_hash {
2092 return git_get_hash(@_, '--short=7');
2095 sub git_get_hash {
2096 my ($project, $hash, @options) = @_;
2097 my $o_git_dir = $git_dir;
2098 my $retval = undef;
2099 $git_dir = "$projectroot/$project";
2100 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2101 '--verify', '-q', @options, $hash) {
2102 $retval = <$fd>;
2103 chomp $retval if defined $retval;
2104 close $fd;
2106 if (defined $o_git_dir) {
2107 $git_dir = $o_git_dir;
2109 return $retval;
2112 # get type of given object
2113 sub git_get_type {
2114 my $hash = shift;
2116 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2117 my $type = <$fd>;
2118 close $fd or return;
2119 chomp $type;
2120 return $type;
2123 # repository configuration
2124 our $config_file = '';
2125 our %config;
2127 # store multiple values for single key as anonymous array reference
2128 # single values stored directly in the hash, not as [ <value> ]
2129 sub hash_set_multi {
2130 my ($hash, $key, $value) = @_;
2132 if (!exists $hash->{$key}) {
2133 $hash->{$key} = $value;
2134 } elsif (!ref $hash->{$key}) {
2135 $hash->{$key} = [ $hash->{$key}, $value ];
2136 } else {
2137 push @{$hash->{$key}}, $value;
2141 # return hash of git project configuration
2142 # optionally limited to some section, e.g. 'gitweb'
2143 sub git_parse_project_config {
2144 my $section_regexp = shift;
2145 my %config;
2147 local $/ = "\0";
2149 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2150 or return;
2152 while (my $keyval = <$fh>) {
2153 chomp $keyval;
2154 my ($key, $value) = split(/\n/, $keyval, 2);
2156 hash_set_multi(\%config, $key, $value)
2157 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2159 close $fh;
2161 return %config;
2164 # convert config value to boolean: 'true' or 'false'
2165 # no value, number > 0, 'true' and 'yes' values are true
2166 # rest of values are treated as false (never as error)
2167 sub config_to_bool {
2168 my $val = shift;
2170 return 1 if !defined $val; # section.key
2172 # strip leading and trailing whitespace
2173 $val =~ s/^\s+//;
2174 $val =~ s/\s+$//;
2176 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2177 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2180 # convert config value to simple decimal number
2181 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2182 # to be multiplied by 1024, 1048576, or 1073741824
2183 sub config_to_int {
2184 my $val = shift;
2186 # strip leading and trailing whitespace
2187 $val =~ s/^\s+//;
2188 $val =~ s/\s+$//;
2190 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2191 $unit = lc($unit);
2192 # unknown unit is treated as 1
2193 return $num * ($unit eq 'g' ? 1073741824 :
2194 $unit eq 'm' ? 1048576 :
2195 $unit eq 'k' ? 1024 : 1);
2197 return $val;
2200 # convert config value to array reference, if needed
2201 sub config_to_multi {
2202 my $val = shift;
2204 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2207 sub git_get_project_config {
2208 my ($key, $type) = @_;
2210 # key sanity check
2211 return unless ($key);
2212 $key =~ s/^gitweb\.//;
2213 return if ($key =~ m/\W/);
2215 # type sanity check
2216 if (defined $type) {
2217 $type =~ s/^--//;
2218 $type = undef
2219 unless ($type eq 'bool' || $type eq 'int');
2222 # get config
2223 if (!defined $config_file ||
2224 $config_file ne "$git_dir/config") {
2225 %config = git_parse_project_config('gitweb');
2226 $config_file = "$git_dir/config";
2229 # check if config variable (key) exists
2230 return unless exists $config{"gitweb.$key"};
2232 # ensure given type
2233 if (!defined $type) {
2234 return $config{"gitweb.$key"};
2235 } elsif ($type eq 'bool') {
2236 # backward compatibility: 'git config --bool' returns true/false
2237 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2238 } elsif ($type eq 'int') {
2239 return config_to_int($config{"gitweb.$key"});
2241 return $config{"gitweb.$key"};
2244 # get hash of given path at given ref
2245 sub git_get_hash_by_path {
2246 my $base = shift;
2247 my $path = shift || return undef;
2248 my $type = shift;
2250 $path =~ s,/+$,,;
2252 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2253 or die_error(500, "Open git-ls-tree failed");
2254 my $line = <$fd>;
2255 close $fd or return undef;
2257 if (!defined $line) {
2258 # there is no tree or hash given by $path at $base
2259 return undef;
2262 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2263 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2264 if (defined $type && $type ne $2) {
2265 # type doesn't match
2266 return undef;
2268 return $3;
2271 # get path of entry with given hash at given tree-ish (ref)
2272 # used to get 'from' filename for combined diff (merge commit) for renames
2273 sub git_get_path_by_hash {
2274 my $base = shift || return;
2275 my $hash = shift || return;
2277 local $/ = "\0";
2279 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2280 or return undef;
2281 while (my $line = <$fd>) {
2282 chomp $line;
2284 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2285 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2286 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2287 close $fd;
2288 return $1;
2291 close $fd;
2292 return undef;
2295 ## ......................................................................
2296 ## git utility functions, directly accessing git repository
2298 sub git_get_project_description {
2299 my $path = shift;
2301 $git_dir = "$projectroot/$path";
2302 open my $fd, '<', "$git_dir/description"
2303 or return git_get_project_config('description');
2304 my $descr = <$fd>;
2305 close $fd;
2306 if (defined $descr) {
2307 chomp $descr;
2309 return $descr;
2312 sub git_get_project_ctags {
2313 my $path = shift;
2314 my $ctags = {};
2316 $git_dir = "$projectroot/$path";
2317 opendir my $dh, "$git_dir/ctags"
2318 or return $ctags;
2319 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2320 open my $ct, '<', $_ or next;
2321 my $val = <$ct>;
2322 chomp $val;
2323 close $ct;
2324 my $ctag = $_; $ctag =~ s#.*/##;
2325 $ctags->{$ctag} = $val;
2327 closedir $dh;
2328 $ctags;
2331 sub git_populate_project_tagcloud {
2332 my $ctags = shift;
2334 # First, merge different-cased tags; tags vote on casing
2335 my %ctags_lc;
2336 foreach (keys %$ctags) {
2337 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2338 if (not $ctags_lc{lc $_}->{topcount}
2339 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2340 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2341 $ctags_lc{lc $_}->{topname} = $_;
2345 my $cloud;
2346 if (eval { require HTML::TagCloud; 1; }) {
2347 $cloud = HTML::TagCloud->new;
2348 foreach (sort keys %ctags_lc) {
2349 # Pad the title with spaces so that the cloud looks
2350 # less crammed.
2351 my $title = $ctags_lc{$_}->{topname};
2352 $title =~ s/ /&nbsp;/g;
2353 $title =~ s/^/&nbsp;/g;
2354 $title =~ s/$/&nbsp;/g;
2355 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2357 } else {
2358 $cloud = \%ctags_lc;
2360 $cloud;
2363 sub git_show_project_tagcloud {
2364 my ($cloud, $count) = @_;
2365 print STDERR ref($cloud)."..\n";
2366 if (ref $cloud eq 'HTML::TagCloud') {
2367 return $cloud->html_and_css($count);
2368 } else {
2369 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2370 return '<p align="center">' . join (', ', map {
2371 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
2372 } splice(@tags, 0, $count)) . '</p>';
2376 sub git_get_project_url_list {
2377 my $path = shift;
2379 $git_dir = "$projectroot/$path";
2380 open my $fd, '<', "$git_dir/cloneurl"
2381 or return wantarray ?
2382 @{ config_to_multi(git_get_project_config('url')) } :
2383 config_to_multi(git_get_project_config('url'));
2384 my @git_project_url_list = map { chomp; $_ } <$fd>;
2385 close $fd;
2387 return wantarray ? @git_project_url_list : \@git_project_url_list;
2390 sub git_get_projects_list {
2391 my ($filter) = @_;
2392 my @list;
2394 $filter ||= '';
2395 $filter =~ s/\.git$//;
2397 my $check_forks = gitweb_check_feature('forks');
2399 if (-d $projects_list) {
2400 # search in directory
2401 my $dir = $projects_list . ($filter ? "/$filter" : '');
2402 # remove the trailing "/"
2403 $dir =~ s!/+$!!;
2404 my $pfxlen = length("$dir");
2405 my $pfxdepth = ($dir =~ tr!/!!);
2407 File::Find::find({
2408 follow_fast => 1, # follow symbolic links
2409 follow_skip => 2, # ignore duplicates
2410 dangling_symlinks => 0, # ignore dangling symlinks, silently
2411 wanted => sub {
2412 # skip project-list toplevel, if we get it.
2413 return if (m!^[/.]$!);
2414 # only directories can be git repositories
2415 return unless (-d $_);
2416 # don't traverse too deep (Find is super slow on os x)
2417 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2418 $File::Find::prune = 1;
2419 return;
2422 my $subdir = substr($File::Find::name, $pfxlen + 1);
2423 # we check related file in $projectroot
2424 my $path = ($filter ? "$filter/" : '') . $subdir;
2425 if (check_export_ok("$projectroot/$path")) {
2426 push @list, { path => $path };
2427 $File::Find::prune = 1;
2430 }, "$dir");
2432 } elsif (-f $projects_list) {
2433 # read from file(url-encoded):
2434 # 'git%2Fgit.git Linus+Torvalds'
2435 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2436 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2437 my %paths;
2438 open my $fd, '<', $projects_list or return;
2439 PROJECT:
2440 while (my $line = <$fd>) {
2441 chomp $line;
2442 my ($path, $owner) = split ' ', $line;
2443 $path = unescape($path);
2444 $owner = unescape($owner);
2445 if (!defined $path) {
2446 next;
2448 if ($filter ne '') {
2449 # looking for forks;
2450 my $pfx = substr($path, 0, length($filter));
2451 if ($pfx ne $filter) {
2452 next PROJECT;
2454 my $sfx = substr($path, length($filter));
2455 if ($sfx !~ /^\/.*\.git$/) {
2456 next PROJECT;
2458 } elsif ($check_forks) {
2459 PATH:
2460 foreach my $filter (keys %paths) {
2461 # looking for forks;
2462 my $pfx = substr($path, 0, length($filter));
2463 if ($pfx ne $filter) {
2464 next PATH;
2466 my $sfx = substr($path, length($filter));
2467 if ($sfx !~ /^\/.*\.git$/) {
2468 next PATH;
2470 # is a fork, don't include it in
2471 # the list
2472 next PROJECT;
2475 if (check_export_ok("$projectroot/$path")) {
2476 my $pr = {
2477 path => $path,
2478 owner => to_utf8($owner),
2480 push @list, $pr;
2481 (my $forks_path = $path) =~ s/\.git$//;
2482 $paths{$forks_path}++;
2485 close $fd;
2487 return @list;
2490 our $gitweb_project_owner = undef;
2491 sub git_get_project_list_from_file {
2493 return if (defined $gitweb_project_owner);
2495 $gitweb_project_owner = {};
2496 # read from file (url-encoded):
2497 # 'git%2Fgit.git Linus+Torvalds'
2498 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2499 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2500 if (-f $projects_list) {
2501 open(my $fd, '<', $projects_list);
2502 while (my $line = <$fd>) {
2503 chomp $line;
2504 my ($pr, $ow) = split ' ', $line;
2505 $pr = unescape($pr);
2506 $ow = unescape($ow);
2507 $gitweb_project_owner->{$pr} = to_utf8($ow);
2509 close $fd;
2513 sub git_get_project_owner {
2514 my $project = shift;
2515 my $owner;
2517 return undef unless $project;
2518 $git_dir = "$projectroot/$project";
2520 if (!defined $gitweb_project_owner) {
2521 git_get_project_list_from_file();
2524 if (exists $gitweb_project_owner->{$project}) {
2525 $owner = $gitweb_project_owner->{$project};
2527 if (!defined $owner){
2528 $owner = git_get_project_config('owner');
2530 if (!defined $owner) {
2531 $owner = get_file_owner("$git_dir");
2534 return $owner;
2537 sub git_get_last_activity {
2538 my ($path) = @_;
2539 my $fd;
2541 $git_dir = "$projectroot/$path";
2542 open($fd, "-|", git_cmd(), 'for-each-ref',
2543 '--format=%(committer)',
2544 '--sort=-committerdate',
2545 '--count=1',
2546 'refs/heads') or return;
2547 my $most_recent = <$fd>;
2548 close $fd or return;
2549 if (defined $most_recent &&
2550 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2551 my $timestamp = $1;
2552 my $age = time - $timestamp;
2553 return ($age, age_string($age));
2555 return (undef, undef);
2558 sub git_get_references {
2559 my $type = shift || "";
2560 my %refs;
2561 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2562 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2563 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2564 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2565 or return;
2567 while (my $line = <$fd>) {
2568 chomp $line;
2569 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2570 if (defined $refs{$1}) {
2571 push @{$refs{$1}}, $2;
2572 } else {
2573 $refs{$1} = [ $2 ];
2577 close $fd or return;
2578 return \%refs;
2581 sub git_get_rev_name_tags {
2582 my $hash = shift || return undef;
2584 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2585 or return;
2586 my $name_rev = <$fd>;
2587 close $fd;
2589 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2590 return $1;
2591 } else {
2592 # catches also '$hash undefined' output
2593 return undef;
2597 ## ----------------------------------------------------------------------
2598 ## parse to hash functions
2600 sub parse_date {
2601 my $epoch = shift;
2602 my $tz = shift || "-0000";
2604 my %date;
2605 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2606 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2607 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2608 $date{'hour'} = $hour;
2609 $date{'minute'} = $min;
2610 $date{'mday'} = $mday;
2611 $date{'day'} = $days[$wday];
2612 $date{'month'} = $months[$mon];
2613 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2614 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2615 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2616 $mday, $months[$mon], $hour ,$min;
2617 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2618 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2620 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2621 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2622 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2623 $date{'hour_local'} = $hour;
2624 $date{'minute_local'} = $min;
2625 $date{'tz_local'} = $tz;
2626 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2627 1900+$year, $mon+1, $mday,
2628 $hour, $min, $sec, $tz);
2629 return %date;
2632 sub parse_tag {
2633 my $tag_id = shift;
2634 my %tag;
2635 my @comment;
2637 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2638 $tag{'id'} = $tag_id;
2639 while (my $line = <$fd>) {
2640 chomp $line;
2641 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2642 $tag{'object'} = $1;
2643 } elsif ($line =~ m/^type (.+)$/) {
2644 $tag{'type'} = $1;
2645 } elsif ($line =~ m/^tag (.+)$/) {
2646 $tag{'name'} = $1;
2647 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2648 $tag{'author'} = $1;
2649 $tag{'author_epoch'} = $2;
2650 $tag{'author_tz'} = $3;
2651 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2652 $tag{'author_name'} = $1;
2653 $tag{'author_email'} = $2;
2654 } else {
2655 $tag{'author_name'} = $tag{'author'};
2657 } elsif ($line =~ m/--BEGIN/) {
2658 push @comment, $line;
2659 last;
2660 } elsif ($line eq "") {
2661 last;
2664 push @comment, <$fd>;
2665 $tag{'comment'} = \@comment;
2666 close $fd or return;
2667 if (!defined $tag{'name'}) {
2668 return
2670 return %tag
2673 sub parse_commit_text {
2674 my ($commit_text, $withparents) = @_;
2675 my @commit_lines = split '\n', $commit_text;
2676 my %co;
2678 pop @commit_lines; # Remove '\0'
2680 if (! @commit_lines) {
2681 return;
2684 my $header = shift @commit_lines;
2685 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2686 return;
2688 ($co{'id'}, my @parents) = split ' ', $header;
2689 while (my $line = shift @commit_lines) {
2690 last if $line eq "\n";
2691 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2692 $co{'tree'} = $1;
2693 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2694 push @parents, $1;
2695 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2696 $co{'author'} = to_utf8($1);
2697 $co{'author_epoch'} = $2;
2698 $co{'author_tz'} = $3;
2699 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2700 $co{'author_name'} = $1;
2701 $co{'author_email'} = $2;
2702 } else {
2703 $co{'author_name'} = $co{'author'};
2705 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2706 $co{'committer'} = to_utf8($1);
2707 $co{'committer_epoch'} = $2;
2708 $co{'committer_tz'} = $3;
2709 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2710 $co{'committer_name'} = $1;
2711 $co{'committer_email'} = $2;
2712 } else {
2713 $co{'committer_name'} = $co{'committer'};
2717 if (!defined $co{'tree'}) {
2718 return;
2720 $co{'parents'} = \@parents;
2721 $co{'parent'} = $parents[0];
2723 foreach my $title (@commit_lines) {
2724 $title =~ s/^ //;
2725 if ($title ne "") {
2726 $co{'title'} = chop_str($title, 80, 5);
2727 # remove leading stuff of merges to make the interesting part visible
2728 if (length($title) > 50) {
2729 $title =~ s/^Automatic //;
2730 $title =~ s/^merge (of|with) /Merge ... /i;
2731 if (length($title) > 50) {
2732 $title =~ s/(http|rsync):\/\///;
2734 if (length($title) > 50) {
2735 $title =~ s/(master|www|rsync)\.//;
2737 if (length($title) > 50) {
2738 $title =~ s/kernel.org:?//;
2740 if (length($title) > 50) {
2741 $title =~ s/\/pub\/scm//;
2744 $co{'title_short'} = chop_str($title, 50, 5);
2745 last;
2748 if (! defined $co{'title'} || $co{'title'} eq "") {
2749 $co{'title'} = $co{'title_short'} = '(no commit message)';
2751 # remove added spaces
2752 foreach my $line (@commit_lines) {
2753 $line =~ s/^ //;
2755 $co{'comment'} = \@commit_lines;
2757 my $age = time - $co{'committer_epoch'};
2758 $co{'age'} = $age;
2759 $co{'age_string'} = age_string($age);
2760 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2761 if ($age > 60*60*24*7*2) {
2762 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2763 $co{'age_string_age'} = $co{'age_string'};
2764 } else {
2765 $co{'age_string_date'} = $co{'age_string'};
2766 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2768 return %co;
2771 sub parse_commit {
2772 my ($commit_id) = @_;
2773 my %co;
2775 local $/ = "\0";
2777 open my $fd, "-|", git_cmd(), "rev-list",
2778 "--parents",
2779 "--header",
2780 "--max-count=1",
2781 $commit_id,
2782 "--",
2783 or die_error(500, "Open git-rev-list failed");
2784 %co = parse_commit_text(<$fd>, 1);
2785 close $fd;
2787 return %co;
2790 sub parse_commits {
2791 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2792 my @cos;
2794 $maxcount ||= 1;
2795 $skip ||= 0;
2797 local $/ = "\0";
2799 open my $fd, "-|", git_cmd(), "rev-list",
2800 "--header",
2801 @args,
2802 ("--max-count=" . $maxcount),
2803 ("--skip=" . $skip),
2804 @extra_options,
2805 $commit_id,
2806 "--",
2807 ($filename ? ($filename) : ())
2808 or die_error(500, "Open git-rev-list failed");
2809 while (my $line = <$fd>) {
2810 my %co = parse_commit_text($line);
2811 push @cos, \%co;
2813 close $fd;
2815 return wantarray ? @cos : \@cos;
2818 # parse line of git-diff-tree "raw" output
2819 sub parse_difftree_raw_line {
2820 my $line = shift;
2821 my %res;
2823 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2824 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2825 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2826 $res{'from_mode'} = $1;
2827 $res{'to_mode'} = $2;
2828 $res{'from_id'} = $3;
2829 $res{'to_id'} = $4;
2830 $res{'status'} = $5;
2831 $res{'similarity'} = $6;
2832 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2833 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2834 } else {
2835 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2838 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2839 # combined diff (for merge commit)
2840 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2841 $res{'nparents'} = length($1);
2842 $res{'from_mode'} = [ split(' ', $2) ];
2843 $res{'to_mode'} = pop @{$res{'from_mode'}};
2844 $res{'from_id'} = [ split(' ', $3) ];
2845 $res{'to_id'} = pop @{$res{'from_id'}};
2846 $res{'status'} = [ split('', $4) ];
2847 $res{'to_file'} = unquote($5);
2849 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2850 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2851 $res{'commit'} = $1;
2854 return wantarray ? %res : \%res;
2857 # wrapper: return parsed line of git-diff-tree "raw" output
2858 # (the argument might be raw line, or parsed info)
2859 sub parsed_difftree_line {
2860 my $line_or_ref = shift;
2862 if (ref($line_or_ref) eq "HASH") {
2863 # pre-parsed (or generated by hand)
2864 return $line_or_ref;
2865 } else {
2866 return parse_difftree_raw_line($line_or_ref);
2870 # parse line of git-ls-tree output
2871 sub parse_ls_tree_line {
2872 my $line = shift;
2873 my %opts = @_;
2874 my %res;
2876 if ($opts{'-l'}) {
2877 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
2878 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
2880 $res{'mode'} = $1;
2881 $res{'type'} = $2;
2882 $res{'hash'} = $3;
2883 $res{'size'} = $4;
2884 if ($opts{'-z'}) {
2885 $res{'name'} = $5;
2886 } else {
2887 $res{'name'} = unquote($5);
2889 } else {
2890 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2891 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2893 $res{'mode'} = $1;
2894 $res{'type'} = $2;
2895 $res{'hash'} = $3;
2896 if ($opts{'-z'}) {
2897 $res{'name'} = $4;
2898 } else {
2899 $res{'name'} = unquote($4);
2903 return wantarray ? %res : \%res;
2906 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2907 sub parse_from_to_diffinfo {
2908 my ($diffinfo, $from, $to, @parents) = @_;
2910 if ($diffinfo->{'nparents'}) {
2911 # combined diff
2912 $from->{'file'} = [];
2913 $from->{'href'} = [];
2914 fill_from_file_info($diffinfo, @parents)
2915 unless exists $diffinfo->{'from_file'};
2916 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2917 $from->{'file'}[$i] =
2918 defined $diffinfo->{'from_file'}[$i] ?
2919 $diffinfo->{'from_file'}[$i] :
2920 $diffinfo->{'to_file'};
2921 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2922 $from->{'href'}[$i] = href(action=>"blob",
2923 hash_base=>$parents[$i],
2924 hash=>$diffinfo->{'from_id'}[$i],
2925 file_name=>$from->{'file'}[$i]);
2926 } else {
2927 $from->{'href'}[$i] = undef;
2930 } else {
2931 # ordinary (not combined) diff
2932 $from->{'file'} = $diffinfo->{'from_file'};
2933 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2934 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2935 hash=>$diffinfo->{'from_id'},
2936 file_name=>$from->{'file'});
2937 } else {
2938 delete $from->{'href'};
2942 $to->{'file'} = $diffinfo->{'to_file'};
2943 if (!is_deleted($diffinfo)) { # file exists in result
2944 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2945 hash=>$diffinfo->{'to_id'},
2946 file_name=>$to->{'file'});
2947 } else {
2948 delete $to->{'href'};
2952 ## ......................................................................
2953 ## parse to array of hashes functions
2955 sub git_get_heads_list {
2956 my $limit = shift;
2957 my @headslist;
2959 open my $fd, '-|', git_cmd(), 'for-each-ref',
2960 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2961 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2962 'refs/heads'
2963 or return;
2964 while (my $line = <$fd>) {
2965 my %ref_item;
2967 chomp $line;
2968 my ($refinfo, $committerinfo) = split(/\0/, $line);
2969 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2970 my ($committer, $epoch, $tz) =
2971 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2972 $ref_item{'fullname'} = $name;
2973 $name =~ s!^refs/heads/!!;
2975 $ref_item{'name'} = $name;
2976 $ref_item{'id'} = $hash;
2977 $ref_item{'title'} = $title || '(no commit message)';
2978 $ref_item{'epoch'} = $epoch;
2979 if ($epoch) {
2980 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2981 } else {
2982 $ref_item{'age'} = "unknown";
2985 push @headslist, \%ref_item;
2987 close $fd;
2989 return wantarray ? @headslist : \@headslist;
2992 sub git_get_tags_list {
2993 my $limit = shift;
2994 my @tagslist;
2996 open my $fd, '-|', git_cmd(), 'for-each-ref',
2997 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2998 '--format=%(objectname) %(objecttype) %(refname) '.
2999 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3000 'refs/tags'
3001 or return;
3002 while (my $line = <$fd>) {
3003 my %ref_item;
3005 chomp $line;
3006 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3007 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3008 my ($creator, $epoch, $tz) =
3009 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3010 $ref_item{'fullname'} = $name;
3011 $name =~ s!^refs/tags/!!;
3013 $ref_item{'type'} = $type;
3014 $ref_item{'id'} = $id;
3015 $ref_item{'name'} = $name;
3016 if ($type eq "tag") {
3017 $ref_item{'subject'} = $title;
3018 $ref_item{'reftype'} = $reftype;
3019 $ref_item{'refid'} = $refid;
3020 } else {
3021 $ref_item{'reftype'} = $type;
3022 $ref_item{'refid'} = $id;
3025 if ($type eq "tag" || $type eq "commit") {
3026 $ref_item{'epoch'} = $epoch;
3027 if ($epoch) {
3028 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3029 } else {
3030 $ref_item{'age'} = "unknown";
3034 push @tagslist, \%ref_item;
3036 close $fd;
3038 return wantarray ? @tagslist : \@tagslist;
3041 ## ----------------------------------------------------------------------
3042 ## filesystem-related functions
3044 sub get_file_owner {
3045 my $path = shift;
3047 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3048 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3049 if (!defined $gcos) {
3050 return undef;
3052 my $owner = $gcos;
3053 $owner =~ s/[,;].*$//;
3054 return to_utf8($owner);
3057 # assume that file exists
3058 sub insert_file {
3059 my $filename = shift;
3061 open my $fd, '<', $filename;
3062 print map { to_utf8($_) } <$fd>;
3063 close $fd;
3066 ## ......................................................................
3067 ## mimetype related functions
3069 sub mimetype_guess_file {
3070 my $filename = shift;
3071 my $mimemap = shift;
3072 -r $mimemap or return undef;
3074 my %mimemap;
3075 open(my $mh, '<', $mimemap) or return undef;
3076 while (<$mh>) {
3077 next if m/^#/; # skip comments
3078 my ($mimetype, $exts) = split(/\t+/);
3079 if (defined $exts) {
3080 my @exts = split(/\s+/, $exts);
3081 foreach my $ext (@exts) {
3082 $mimemap{$ext} = $mimetype;
3086 close($mh);
3088 $filename =~ /\.([^.]*)$/;
3089 return $mimemap{$1};
3092 sub mimetype_guess {
3093 my $filename = shift;
3094 my $mime;
3095 $filename =~ /\./ or return undef;
3097 if ($mimetypes_file) {
3098 my $file = $mimetypes_file;
3099 if ($file !~ m!^/!) { # if it is relative path
3100 # it is relative to project
3101 $file = "$projectroot/$project/$file";
3103 $mime = mimetype_guess_file($filename, $file);
3105 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3106 return $mime;
3109 sub blob_mimetype {
3110 my $fd = shift;
3111 my $filename = shift;
3113 if ($filename) {
3114 my $mime = mimetype_guess($filename);
3115 $mime and return $mime;
3118 # just in case
3119 return $default_blob_plain_mimetype unless $fd;
3121 if (-T $fd) {
3122 return 'text/plain';
3123 } elsif (! $filename) {
3124 return 'application/octet-stream';
3125 } elsif ($filename =~ m/\.png$/i) {
3126 return 'image/png';
3127 } elsif ($filename =~ m/\.gif$/i) {
3128 return 'image/gif';
3129 } elsif ($filename =~ m/\.jpe?g$/i) {
3130 return 'image/jpeg';
3131 } else {
3132 return 'application/octet-stream';
3136 sub blob_contenttype {
3137 my ($fd, $file_name, $type) = @_;
3139 $type ||= blob_mimetype($fd, $file_name);
3140 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3141 $type .= "; charset=$default_text_plain_charset";
3144 return $type;
3147 ## ======================================================================
3148 ## functions printing HTML: header, footer, error page
3150 sub git_header_html {
3151 my $status = shift || "200 OK";
3152 my $expires = shift;
3154 my $title = "$site_name";
3155 if (defined $project) {
3156 $title .= " - " . to_utf8($project);
3157 if (defined $action) {
3158 $title .= "/$action";
3159 if (defined $file_name) {
3160 $title .= " - " . esc_path($file_name);
3161 if ($action eq "tree" && $file_name !~ m|/$|) {
3162 $title .= "/";
3167 my $content_type;
3168 # require explicit support from the UA if we are to send the page as
3169 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3170 # we have to do this because MSIE sometimes globs '*/*', pretending to
3171 # support xhtml+xml but choking when it gets what it asked for.
3172 if (defined $cgi->http('HTTP_ACCEPT') &&
3173 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3174 $cgi->Accept('application/xhtml+xml') != 0) {
3175 $content_type = 'application/xhtml+xml';
3176 } else {
3177 $content_type = 'text/html';
3179 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3180 -status=> $status, -expires => $expires);
3181 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3182 print <<EOF;
3183 <?xml version="1.0" encoding="utf-8"?>
3184 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3185 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3186 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3187 <!-- git core binaries version $git_version -->
3188 <head>
3189 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3190 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3191 <meta name="robots" content="index, nofollow"/>
3192 <title>$title</title>
3194 # the stylesheet, favicon etc urls won't work correctly with path_info
3195 # unless we set the appropriate base URL
3196 if ($ENV{'PATH_INFO'}) {
3197 print "<base href=\"".esc_url($base_url)."\" />\n";
3199 # print out each stylesheet that exist, providing backwards capability
3200 # for those people who defined $stylesheet in a config file
3201 if (defined $stylesheet) {
3202 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3203 } else {
3204 foreach my $stylesheet (@stylesheets) {
3205 next unless $stylesheet;
3206 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
3209 if (defined $project) {
3210 my %href_params = get_feed_info();
3211 if (!exists $href_params{'-title'}) {
3212 $href_params{'-title'} = 'log';
3215 foreach my $format qw(RSS Atom) {
3216 my $type = lc($format);
3217 my %link_attr = (
3218 '-rel' => 'alternate',
3219 '-title' => "$project - $href_params{'-title'} - $format feed",
3220 '-type' => "application/$type+xml"
3223 $href_params{'action'} = $type;
3224 $link_attr{'-href'} = href(%href_params);
3225 print "<link ".
3226 "rel=\"$link_attr{'-rel'}\" ".
3227 "title=\"$link_attr{'-title'}\" ".
3228 "href=\"$link_attr{'-href'}\" ".
3229 "type=\"$link_attr{'-type'}\" ".
3230 "/>\n";
3232 $href_params{'extra_options'} = '--no-merges';
3233 $link_attr{'-href'} = href(%href_params);
3234 $link_attr{'-title'} .= ' (no merges)';
3235 print "<link ".
3236 "rel=\"$link_attr{'-rel'}\" ".
3237 "title=\"$link_attr{'-title'}\" ".
3238 "href=\"$link_attr{'-href'}\" ".
3239 "type=\"$link_attr{'-type'}\" ".
3240 "/>\n";
3243 } else {
3244 printf('<link rel="alternate" title="%s projects list" '.
3245 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3246 $site_name, href(project=>undef, action=>"project_index"));
3247 printf('<link rel="alternate" title="%s projects feeds" '.
3248 'href="%s" type="text/x-opml" />'."\n",
3249 $site_name, href(project=>undef, action=>"opml"));
3251 if (defined $favicon) {
3252 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
3255 print "</head>\n" .
3256 "<body>\n";
3258 if (defined $site_header && -f $site_header) {
3259 insert_file($site_header);
3262 print "<div class=\"page_header\">\n" .
3263 $cgi->a({-href => esc_url($logo_url),
3264 -title => $logo_label},
3265 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
3266 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3267 if (defined $project) {
3268 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3269 if (defined $action) {
3270 print " / $action";
3272 print "\n";
3274 print "</div>\n";
3276 my $have_search = gitweb_check_feature('search');
3277 if (defined $project && $have_search) {
3278 if (!defined $searchtext) {
3279 $searchtext = "";
3281 my $search_hash;
3282 if (defined $hash_base) {
3283 $search_hash = $hash_base;
3284 } elsif (defined $hash) {
3285 $search_hash = $hash;
3286 } else {
3287 $search_hash = "HEAD";
3289 my $action = $my_uri;
3290 my $use_pathinfo = gitweb_check_feature('pathinfo');
3291 if ($use_pathinfo) {
3292 $action .= "/".esc_url($project);
3294 print $cgi->startform(-method => "get", -action => $action) .
3295 "<div class=\"search\">\n" .
3296 (!$use_pathinfo &&
3297 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3298 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3299 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3300 $cgi->popup_menu(-name => 'st', -default => 'commit',
3301 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3302 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3303 " search:\n",
3304 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3305 "<span title=\"Extended regular expression\">" .
3306 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3307 -checked => $search_use_regexp) .
3308 "</span>" .
3309 "</div>" .
3310 $cgi->end_form() . "\n";
3314 sub git_footer_html {
3315 my $feed_class = 'rss_logo';
3317 print "<div class=\"page_footer\">\n";
3318 if (defined $project) {
3319 my $descr = git_get_project_description($project);
3320 if (defined $descr) {
3321 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3324 my %href_params = get_feed_info();
3325 if (!%href_params) {
3326 $feed_class .= ' generic';
3328 $href_params{'-title'} ||= 'log';
3330 foreach my $format qw(RSS Atom) {
3331 $href_params{'action'} = lc($format);
3332 print $cgi->a({-href => href(%href_params),
3333 -title => "$href_params{'-title'} $format feed",
3334 -class => $feed_class}, $format)."\n";
3337 } else {
3338 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3339 -class => $feed_class}, "OPML") . " ";
3340 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3341 -class => $feed_class}, "TXT") . "\n";
3343 print "</div>\n"; # class="page_footer"
3345 if (defined $t0 && gitweb_check_feature('timed')) {
3346 print "<div id=\"generating_info\">\n";
3347 print 'This page took '.
3348 '<span id="generating_time" class="time_span">'.
3349 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
3350 ' seconds </span>'.
3351 ' and '.
3352 '<span id="generating_cmd">'.
3353 $number_of_git_cmds.
3354 '</span> git commands '.
3355 " to generate.\n";
3356 print "</div>\n"; # class="page_footer"
3359 if (defined $site_footer && -f $site_footer) {
3360 insert_file($site_footer);
3363 print qq!<script type="text/javascript" src="$javascript"></script>\n!;
3364 if (defined $action &&
3365 $action eq 'blame_incremental') {
3366 print qq!<script type="text/javascript">\n!.
3367 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3368 qq! "!. href() .qq!");\n!.
3369 qq!</script>\n!;
3370 } elsif (gitweb_check_feature('javascript-actions')) {
3371 print qq!<script type="text/javascript">\n!.
3372 qq!window.onload = fixLinks;\n!.
3373 qq!</script>\n!;
3376 print "</body>\n" .
3377 "</html>";
3380 # die_error(<http_status_code>, <error_message>)
3381 # Example: die_error(404, 'Hash not found')
3382 # By convention, use the following status codes (as defined in RFC 2616):
3383 # 400: Invalid or missing CGI parameters, or
3384 # requested object exists but has wrong type.
3385 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3386 # this server or project.
3387 # 404: Requested object/revision/project doesn't exist.
3388 # 500: The server isn't configured properly, or
3389 # an internal error occurred (e.g. failed assertions caused by bugs), or
3390 # an unknown error occurred (e.g. the git binary died unexpectedly).
3391 # 503: The server is currently unavailable (because it is overloaded,
3392 # or down for maintenance). Generally, this is a temporary state.
3393 sub die_error {
3394 my $status = shift || 500;
3395 my $error = shift || "Internal server error";
3396 my $extra = shift;
3398 my %http_responses = (
3399 400 => '400 Bad Request',
3400 403 => '403 Forbidden',
3401 404 => '404 Not Found',
3402 500 => '500 Internal Server Error',
3403 503 => '503 Service Unavailable',
3405 git_header_html($http_responses{$status});
3406 print <<EOF;
3407 <div class="page_body">
3408 <br /><br />
3409 $status - $error
3410 <br />
3412 if (defined $extra) {
3413 print "<hr />\n" .
3414 "$extra\n";
3416 print "</div>\n";
3418 git_footer_html();
3419 exit;
3422 ## ----------------------------------------------------------------------
3423 ## functions printing or outputting HTML: navigation
3425 sub git_print_page_nav {
3426 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3427 $extra = '' if !defined $extra; # pager or formats
3429 my @navs = qw(summary shortlog log commit commitdiff tree);
3430 if ($suppress) {
3431 @navs = grep { $_ ne $suppress } @navs;
3434 my %arg = map { $_ => {action=>$_} } @navs;
3435 if (defined $head) {
3436 for (qw(commit commitdiff)) {
3437 $arg{$_}{'hash'} = $head;
3439 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3440 for (qw(shortlog log)) {
3441 $arg{$_}{'hash'} = $head;
3446 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3447 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3449 my @actions = gitweb_get_feature('actions');
3450 my %repl = (
3451 '%' => '%',
3452 'n' => $project, # project name
3453 'f' => $git_dir, # project path within filesystem
3454 'h' => $treehead || '', # current hash ('h' parameter)
3455 'b' => $treebase || '', # hash base ('hb' parameter)
3457 while (@actions) {
3458 my ($label, $link, $pos) = splice(@actions,0,3);
3459 # insert
3460 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3461 # munch munch
3462 $link =~ s/%([%nfhb])/$repl{$1}/g;
3463 $arg{$label}{'_href'} = $link;
3466 print "<div class=\"page_nav\">\n" .
3467 (join " | ",
3468 map { $_ eq $current ?
3469 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3470 } @navs);
3471 print "<br/>\n$extra<br/>\n" .
3472 "</div>\n";
3475 sub format_paging_nav {
3476 my ($action, $page, $has_next_link) = @_;
3477 my $paging_nav;
3480 if ($page > 0) {
3481 $paging_nav .=
3482 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
3483 " &sdot; " .
3484 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3485 -accesskey => "p", -title => "Alt-p"}, "prev");
3486 } else {
3487 $paging_nav .= "first &sdot; prev";
3490 if ($has_next_link) {
3491 $paging_nav .= " &sdot; " .
3492 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3493 -accesskey => "n", -title => "Alt-n"}, "next");
3494 } else {
3495 $paging_nav .= " &sdot; next";
3498 return $paging_nav;
3501 ## ......................................................................
3502 ## functions printing or outputting HTML: div
3504 sub git_print_header_div {
3505 my ($action, $title, $hash, $hash_base) = @_;
3506 my %args = ();
3508 $args{'action'} = $action;
3509 $args{'hash'} = $hash if $hash;
3510 $args{'hash_base'} = $hash_base if $hash_base;
3512 print "<div class=\"header\">\n" .
3513 $cgi->a({-href => href(%args), -class => "title"},
3514 $title ? $title : $action) .
3515 "\n</div>\n";
3518 sub print_local_time {
3519 print format_local_time(@_);
3522 sub format_local_time {
3523 my $localtime = '';
3524 my %date = @_;
3525 if ($date{'hour_local'} < 6) {
3526 $localtime .= sprintf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3527 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3528 } else {
3529 $localtime .= sprintf(" (%02d:%02d %s)",
3530 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3533 return $localtime;
3536 # Outputs the author name and date in long form
3537 sub git_print_authorship {
3538 my $co = shift;
3539 my %opts = @_;
3540 my $tag = $opts{-tag} || 'div';
3541 my $author = $co->{'author_name'};
3543 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3544 print "<$tag class=\"author_date\">" .
3545 format_search_author($author, "author", esc_html($author)) .
3546 " [$ad{'rfc2822'}";
3547 print_local_time(%ad) if ($opts{-localtime});
3548 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3549 . "</$tag>\n";
3552 # Outputs table rows containing the full author or committer information,
3553 # in the format expected for 'commit' view (& similia).
3554 # Parameters are a commit hash reference, followed by the list of people
3555 # to output information for. If the list is empty it defalts to both
3556 # author and committer.
3557 sub git_print_authorship_rows {
3558 my $co = shift;
3559 # too bad we can't use @people = @_ || ('author', 'committer')
3560 my @people = @_;
3561 @people = ('author', 'committer') unless @people;
3562 foreach my $who (@people) {
3563 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3564 print "<tr><td>$who</td><td>" .
3565 format_search_author($co->{"${who}_name"}, $who,
3566 esc_html($co->{"${who}_name"})) . " " .
3567 format_search_author($co->{"${who}_email"}, $who,
3568 esc_html("<" . $co->{"${who}_email"} . ">")) .
3569 "</td><td rowspan=\"2\">" .
3570 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3571 "</td></tr>\n" .
3572 "<tr>" .
3573 "<td></td><td> $wd{'rfc2822'}";
3574 print_local_time(%wd);
3575 print "</td>" .
3576 "</tr>\n";
3580 sub git_print_page_path {
3581 my $name = shift;
3582 my $type = shift;
3583 my $hb = shift;
3586 print "<div class=\"page_path\">";
3587 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3588 -title => 'tree root'}, to_utf8("[$project]"));
3589 print " / ";
3590 if (defined $name) {
3591 my @dirname = split '/', $name;
3592 my $basename = pop @dirname;
3593 my $fullname = '';
3595 foreach my $dir (@dirname) {
3596 $fullname .= ($fullname ? '/' : '') . $dir;
3597 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3598 hash_base=>$hb),
3599 -title => $fullname}, esc_path($dir));
3600 print " / ";
3602 if (defined $type && $type eq 'blob') {
3603 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3604 hash_base=>$hb),
3605 -title => $name}, esc_path($basename));
3606 } elsif (defined $type && $type eq 'tree') {
3607 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3608 hash_base=>$hb),
3609 -title => $name}, esc_path($basename));
3610 print " / ";
3611 } else {
3612 print esc_path($basename);
3615 print "<br/></div>\n";
3618 sub git_print_log {
3619 my $log = shift;
3620 my %opts = @_;
3622 if ($opts{'-remove_title'}) {
3623 # remove title, i.e. first line of log
3624 shift @$log;
3626 # remove leading empty lines
3627 while (defined $log->[0] && $log->[0] eq "") {
3628 shift @$log;
3631 # print log
3632 my $signoff = 0;
3633 my $empty = 0;
3634 foreach my $line (@$log) {
3635 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3636 $signoff = 1;
3637 $empty = 0;
3638 if (! $opts{'-remove_signoff'}) {
3639 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3640 next;
3641 } else {
3642 # remove signoff lines
3643 next;
3645 } else {
3646 $signoff = 0;
3649 # print only one empty line
3650 # do not print empty line after signoff
3651 if ($line eq "") {
3652 next if ($empty || $signoff);
3653 $empty = 1;
3654 } else {
3655 $empty = 0;
3658 print format_log_line_html($line) . "<br/>\n";
3661 if ($opts{'-final_empty_line'}) {
3662 # end with single empty line
3663 print "<br/>\n" unless $empty;
3667 # return link target (what link points to)
3668 sub git_get_link_target {
3669 my $hash = shift;
3670 my $link_target;
3672 # read link
3673 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3674 or return;
3676 local $/ = undef;
3677 $link_target = <$fd>;
3679 close $fd
3680 or return;
3682 return $link_target;
3685 # given link target, and the directory (basedir) the link is in,
3686 # return target of link relative to top directory (top tree);
3687 # return undef if it is not possible (including absolute links).
3688 sub normalize_link_target {
3689 my ($link_target, $basedir) = @_;
3691 # absolute symlinks (beginning with '/') cannot be normalized
3692 return if (substr($link_target, 0, 1) eq '/');
3694 # normalize link target to path from top (root) tree (dir)
3695 my $path;
3696 if ($basedir) {
3697 $path = $basedir . '/' . $link_target;
3698 } else {
3699 # we are in top (root) tree (dir)
3700 $path = $link_target;
3703 # remove //, /./, and /../
3704 my @path_parts;
3705 foreach my $part (split('/', $path)) {
3706 # discard '.' and ''
3707 next if (!$part || $part eq '.');
3708 # handle '..'
3709 if ($part eq '..') {
3710 if (@path_parts) {
3711 pop @path_parts;
3712 } else {
3713 # link leads outside repository (outside top dir)
3714 return;
3716 } else {
3717 push @path_parts, $part;
3720 $path = join('/', @path_parts);
3722 return $path;
3725 # print tree entry (row of git_tree), but without encompassing <tr> element
3726 sub git_print_tree_entry {
3727 my ($t, $basedir, $hash_base, $have_blame) = @_;
3729 my %base_key = ();
3730 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3732 # The format of a table row is: mode list link. Where mode is
3733 # the mode of the entry, list is the name of the entry, an href,
3734 # and link is the action links of the entry.
3736 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3737 if (exists $t->{'size'}) {
3738 print "<td class=\"size\">$t->{'size'}</td>\n";
3740 if ($t->{'type'} eq "blob") {
3741 print "<td class=\"list\">" .
3742 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3743 file_name=>"$basedir$t->{'name'}", %base_key),
3744 -class => "list"}, esc_path($t->{'name'}));
3745 if (S_ISLNK(oct $t->{'mode'})) {
3746 my $link_target = git_get_link_target($t->{'hash'});
3747 if ($link_target) {
3748 my $norm_target = normalize_link_target($link_target, $basedir);
3749 if (defined $norm_target) {
3750 print " -> " .
3751 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3752 file_name=>$norm_target),
3753 -title => $norm_target}, esc_path($link_target));
3754 } else {
3755 print " -> " . esc_path($link_target);
3759 print "</td>\n";
3760 print "<td class=\"link\">";
3761 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3762 file_name=>"$basedir$t->{'name'}", %base_key)},
3763 "blob");
3764 if ($have_blame) {
3765 print " | " .
3766 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3767 file_name=>"$basedir$t->{'name'}", %base_key)},
3768 "blame");
3770 if (defined $hash_base) {
3771 print " | " .
3772 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3773 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3774 "history");
3776 print " | " .
3777 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3778 file_name=>"$basedir$t->{'name'}")},
3779 "raw");
3780 print "</td>\n";
3782 } elsif ($t->{'type'} eq "tree") {
3783 print "<td class=\"list\">";
3784 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3785 file_name=>"$basedir$t->{'name'}",
3786 %base_key)},
3787 esc_path($t->{'name'}));
3788 print "</td>\n";
3789 print "<td class=\"link\">";
3790 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3791 file_name=>"$basedir$t->{'name'}",
3792 %base_key)},
3793 "tree");
3794 if (defined $hash_base) {
3795 print " | " .
3796 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3797 file_name=>"$basedir$t->{'name'}")},
3798 "history");
3800 print "</td>\n";
3801 } else {
3802 # unknown object: we can only present history for it
3803 # (this includes 'commit' object, i.e. submodule support)
3804 print "<td class=\"list\">" .
3805 esc_path($t->{'name'}) .
3806 "</td>\n";
3807 print "<td class=\"link\">";
3808 if (defined $hash_base) {
3809 print $cgi->a({-href => href(action=>"history",
3810 hash_base=>$hash_base,
3811 file_name=>"$basedir$t->{'name'}")},
3812 "history");
3814 print "</td>\n";
3818 ## ......................................................................
3819 ## functions printing large fragments of HTML
3821 # get pre-image filenames for merge (combined) diff
3822 sub fill_from_file_info {
3823 my ($diff, @parents) = @_;
3825 $diff->{'from_file'} = [ ];
3826 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3827 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3828 if ($diff->{'status'}[$i] eq 'R' ||
3829 $diff->{'status'}[$i] eq 'C') {
3830 $diff->{'from_file'}[$i] =
3831 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3835 return $diff;
3838 # is current raw difftree line of file deletion
3839 sub is_deleted {
3840 my $diffinfo = shift;
3842 return $diffinfo->{'to_id'} eq ('0' x 40);
3845 # does patch correspond to [previous] difftree raw line
3846 # $diffinfo - hashref of parsed raw diff format
3847 # $patchinfo - hashref of parsed patch diff format
3848 # (the same keys as in $diffinfo)
3849 sub is_patch_split {
3850 my ($diffinfo, $patchinfo) = @_;
3852 return defined $diffinfo && defined $patchinfo
3853 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3857 sub git_difftree_body {
3858 my ($difftree, $hash, @parents) = @_;
3859 my ($parent) = $parents[0];
3860 my $have_blame = gitweb_check_feature('blame');
3861 print "<div class=\"list_head\">\n";
3862 if ($#{$difftree} > 10) {
3863 print(($#{$difftree} + 1) . " files changed:\n");
3865 print "</div>\n";
3867 print "<table class=\"" .
3868 (@parents > 1 ? "combined " : "") .
3869 "diff_tree\">\n";
3871 # header only for combined diff in 'commitdiff' view
3872 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3873 if ($has_header) {
3874 # table header
3875 print "<thead><tr>\n" .
3876 "<th></th><th></th>\n"; # filename, patchN link
3877 for (my $i = 0; $i < @parents; $i++) {
3878 my $par = $parents[$i];
3879 print "<th>" .
3880 $cgi->a({-href => href(action=>"commitdiff",
3881 hash=>$hash, hash_parent=>$par),
3882 -title => 'commitdiff to parent number ' .
3883 ($i+1) . ': ' . substr($par,0,7)},
3884 $i+1) .
3885 "&nbsp;</th>\n";
3887 print "</tr></thead>\n<tbody>\n";
3890 my $alternate = 1;
3891 my $patchno = 0;
3892 foreach my $line (@{$difftree}) {
3893 my $diff = parsed_difftree_line($line);
3895 if ($alternate) {
3896 print "<tr class=\"dark\">\n";
3897 } else {
3898 print "<tr class=\"light\">\n";
3900 $alternate ^= 1;
3902 if (exists $diff->{'nparents'}) { # combined diff
3904 fill_from_file_info($diff, @parents)
3905 unless exists $diff->{'from_file'};
3907 if (!is_deleted($diff)) {
3908 # file exists in the result (child) commit
3909 print "<td>" .
3910 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3911 file_name=>$diff->{'to_file'},
3912 hash_base=>$hash),
3913 -class => "list"}, esc_path($diff->{'to_file'})) .
3914 "</td>\n";
3915 } else {
3916 print "<td>" .
3917 esc_path($diff->{'to_file'}) .
3918 "</td>\n";
3921 if ($action eq 'commitdiff') {
3922 # link to patch
3923 $patchno++;
3924 print "<td class=\"link\">" .
3925 $cgi->a({-href => "#patch$patchno"}, "patch") .
3926 " | " .
3927 "</td>\n";
3930 my $has_history = 0;
3931 my $not_deleted = 0;
3932 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3933 my $hash_parent = $parents[$i];
3934 my $from_hash = $diff->{'from_id'}[$i];
3935 my $from_path = $diff->{'from_file'}[$i];
3936 my $status = $diff->{'status'}[$i];
3938 $has_history ||= ($status ne 'A');
3939 $not_deleted ||= ($status ne 'D');
3941 if ($status eq 'A') {
3942 print "<td class=\"link\" align=\"right\"> | </td>\n";
3943 } elsif ($status eq 'D') {
3944 print "<td class=\"link\">" .
3945 $cgi->a({-href => href(action=>"blob",
3946 hash_base=>$hash,
3947 hash=>$from_hash,
3948 file_name=>$from_path)},
3949 "blob" . ($i+1)) .
3950 " | </td>\n";
3951 } else {
3952 if ($diff->{'to_id'} eq $from_hash) {
3953 print "<td class=\"link nochange\">";
3954 } else {
3955 print "<td class=\"link\">";
3957 print $cgi->a({-href => href(action=>"blobdiff",
3958 hash=>$diff->{'to_id'},
3959 hash_parent=>$from_hash,
3960 hash_base=>$hash,
3961 hash_parent_base=>$hash_parent,
3962 file_name=>$diff->{'to_file'},
3963 file_parent=>$from_path)},
3964 "diff" . ($i+1)) .
3965 " | </td>\n";
3969 print "<td class=\"link\">";
3970 if ($not_deleted) {
3971 print $cgi->a({-href => href(action=>"blob",
3972 hash=>$diff->{'to_id'},
3973 file_name=>$diff->{'to_file'},
3974 hash_base=>$hash)},
3975 "blob");
3976 print " | " if ($has_history);
3978 if ($has_history) {
3979 print $cgi->a({-href => href(action=>"history",
3980 file_name=>$diff->{'to_file'},
3981 hash_base=>$hash)},
3982 "history");
3984 print "</td>\n";
3986 print "</tr>\n";
3987 next; # instead of 'else' clause, to avoid extra indent
3989 # else ordinary diff
3991 my ($to_mode_oct, $to_mode_str, $to_file_type);
3992 my ($from_mode_oct, $from_mode_str, $from_file_type);
3993 if ($diff->{'to_mode'} ne ('0' x 6)) {
3994 $to_mode_oct = oct $diff->{'to_mode'};
3995 if (S_ISREG($to_mode_oct)) { # only for regular file
3996 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3998 $to_file_type = file_type($diff->{'to_mode'});
4000 if ($diff->{'from_mode'} ne ('0' x 6)) {
4001 $from_mode_oct = oct $diff->{'from_mode'};
4002 if (S_ISREG($to_mode_oct)) { # only for regular file
4003 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4005 $from_file_type = file_type($diff->{'from_mode'});
4008 if ($diff->{'status'} eq "A") { # created
4009 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4010 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4011 $mode_chng .= "]</span>";
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_chng</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 print " | ";
4025 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4026 hash_base=>$hash, file_name=>$diff->{'file'})},
4027 "blob");
4028 print "</td>\n";
4030 } elsif ($diff->{'status'} eq "D") { # deleted
4031 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4032 print "<td>";
4033 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4034 hash_base=>$parent, file_name=>$diff->{'file'}),
4035 -class => "list"}, esc_path($diff->{'file'}));
4036 print "</td>\n";
4037 print "<td>$mode_chng</td>\n";
4038 print "<td class=\"link\">";
4039 if ($action eq 'commitdiff') {
4040 # link to patch
4041 $patchno++;
4042 print $cgi->a({-href => "#patch$patchno"}, "patch");
4043 print " | ";
4045 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4046 hash_base=>$parent, file_name=>$diff->{'file'})},
4047 "blob") . " | ";
4048 if ($have_blame) {
4049 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4050 file_name=>$diff->{'file'})},
4051 "blame") . " | ";
4053 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4054 file_name=>$diff->{'file'})},
4055 "history");
4056 print "</td>\n";
4058 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4059 my $mode_chnge = "";
4060 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4061 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4062 if ($from_file_type ne $to_file_type) {
4063 $mode_chnge .= " from $from_file_type to $to_file_type";
4065 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4066 if ($from_mode_str && $to_mode_str) {
4067 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4068 } elsif ($to_mode_str) {
4069 $mode_chnge .= " mode: $to_mode_str";
4072 $mode_chnge .= "]</span>\n";
4074 print "<td>";
4075 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4076 hash_base=>$hash, file_name=>$diff->{'file'}),
4077 -class => "list"}, esc_path($diff->{'file'}));
4078 print "</td>\n";
4079 print "<td>$mode_chnge</td>\n";
4080 print "<td class=\"link\">";
4081 if ($action eq 'commitdiff') {
4082 # link to patch
4083 $patchno++;
4084 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4085 " | ";
4086 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4087 # "commit" view and modified file (not onlu mode changed)
4088 print $cgi->a({-href => href(action=>"blobdiff",
4089 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4090 hash_base=>$hash, hash_parent_base=>$parent,
4091 file_name=>$diff->{'file'})},
4092 "diff") .
4093 " | ";
4095 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4096 hash_base=>$hash, file_name=>$diff->{'file'})},
4097 "blob") . " | ";
4098 if ($have_blame) {
4099 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4100 file_name=>$diff->{'file'})},
4101 "blame") . " | ";
4103 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4104 file_name=>$diff->{'file'})},
4105 "history");
4106 print "</td>\n";
4108 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4109 my %status_name = ('R' => 'moved', 'C' => 'copied');
4110 my $nstatus = $status_name{$diff->{'status'}};
4111 my $mode_chng = "";
4112 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4113 # mode also for directories, so we cannot use $to_mode_str
4114 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4116 print "<td>" .
4117 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4118 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4119 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4120 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4121 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4122 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4123 -class => "list"}, esc_path($diff->{'from_file'})) .
4124 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4125 "<td class=\"link\">";
4126 if ($action eq 'commitdiff') {
4127 # link to patch
4128 $patchno++;
4129 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4130 " | ";
4131 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4132 # "commit" view and modified file (not only pure rename or copy)
4133 print $cgi->a({-href => href(action=>"blobdiff",
4134 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4135 hash_base=>$hash, hash_parent_base=>$parent,
4136 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4137 "diff") .
4138 " | ";
4140 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4141 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4142 "blob") . " | ";
4143 if ($have_blame) {
4144 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4145 file_name=>$diff->{'to_file'})},
4146 "blame") . " | ";
4148 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4149 file_name=>$diff->{'to_file'})},
4150 "history");
4151 print "</td>\n";
4153 } # we should not encounter Unmerged (U) or Unknown (X) status
4154 print "</tr>\n";
4156 print "</tbody>" if $has_header;
4157 print "</table>\n";
4160 sub git_patchset_body {
4161 my ($fd, $difftree, $hash, @hash_parents) = @_;
4162 my ($hash_parent) = $hash_parents[0];
4164 my $is_combined = (@hash_parents > 1);
4165 my $patch_idx = 0;
4166 my $patch_number = 0;
4167 my $patch_line;
4168 my $diffinfo;
4169 my $to_name;
4170 my (%from, %to);
4172 print "<div class=\"patchset\">\n";
4174 # skip to first patch
4175 while ($patch_line = <$fd>) {
4176 chomp $patch_line;
4178 last if ($patch_line =~ m/^diff /);
4181 PATCH:
4182 while ($patch_line) {
4184 # parse "git diff" header line
4185 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4186 # $1 is from_name, which we do not use
4187 $to_name = unquote($2);
4188 $to_name =~ s!^b/!!;
4189 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4190 # $1 is 'cc' or 'combined', which we do not use
4191 $to_name = unquote($2);
4192 } else {
4193 $to_name = undef;
4196 # check if current patch belong to current raw line
4197 # and parse raw git-diff line if needed
4198 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4199 # this is continuation of a split patch
4200 print "<div class=\"patch cont\">\n";
4201 } else {
4202 # advance raw git-diff output if needed
4203 $patch_idx++ if defined $diffinfo;
4205 # read and prepare patch information
4206 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4208 # compact combined diff output can have some patches skipped
4209 # find which patch (using pathname of result) we are at now;
4210 if ($is_combined) {
4211 while ($to_name ne $diffinfo->{'to_file'}) {
4212 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4213 format_diff_cc_simplified($diffinfo, @hash_parents) .
4214 "</div>\n"; # class="patch"
4216 $patch_idx++;
4217 $patch_number++;
4219 last if $patch_idx > $#$difftree;
4220 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4224 # modifies %from, %to hashes
4225 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4227 # this is first patch for raw difftree line with $patch_idx index
4228 # we index @$difftree array from 0, but number patches from 1
4229 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4232 # git diff header
4233 #assert($patch_line =~ m/^diff /) if DEBUG;
4234 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4235 $patch_number++;
4236 # print "git diff" header
4237 print format_git_diff_header_line($patch_line, $diffinfo,
4238 \%from, \%to);
4240 # print extended diff header
4241 print "<div class=\"diff extended_header\">\n";
4242 EXTENDED_HEADER:
4243 while ($patch_line = <$fd>) {
4244 chomp $patch_line;
4246 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4248 print format_extended_diff_header_line($patch_line, $diffinfo,
4249 \%from, \%to);
4251 print "</div>\n"; # class="diff extended_header"
4253 # from-file/to-file diff header
4254 if (! $patch_line) {
4255 print "</div>\n"; # class="patch"
4256 last PATCH;
4258 next PATCH if ($patch_line =~ m/^diff /);
4259 #assert($patch_line =~ m/^---/) if DEBUG;
4261 my $last_patch_line = $patch_line;
4262 $patch_line = <$fd>;
4263 chomp $patch_line;
4264 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4266 print format_diff_from_to_header($last_patch_line, $patch_line,
4267 $diffinfo, \%from, \%to,
4268 @hash_parents);
4270 # the patch itself
4271 LINE:
4272 while ($patch_line = <$fd>) {
4273 chomp $patch_line;
4275 next PATCH if ($patch_line =~ m/^diff /);
4277 print format_diff_line($patch_line, \%from, \%to);
4280 } continue {
4281 print "</div>\n"; # class="patch"
4284 # for compact combined (--cc) format, with chunk and patch simpliciaction
4285 # patchset might be empty, but there might be unprocessed raw lines
4286 for (++$patch_idx if $patch_number > 0;
4287 $patch_idx < @$difftree;
4288 ++$patch_idx) {
4289 # read and prepare patch information
4290 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4292 # generate anchor for "patch" links in difftree / whatchanged part
4293 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4294 format_diff_cc_simplified($diffinfo, @hash_parents) .
4295 "</div>\n"; # class="patch"
4297 $patch_number++;
4300 if ($patch_number == 0) {
4301 if (@hash_parents > 1) {
4302 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4303 } else {
4304 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4308 print "</div>\n"; # class="patchset"
4311 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4313 # fills project list info (age, description, owner, forks) for each
4314 # project in the list, removing invalid projects from returned list
4315 # NOTE: modifies $projlist, but does not remove entries from it
4316 sub fill_project_list_info {
4317 my ($projlist, $check_forks) = @_;
4318 my @projects;
4320 my $show_ctags = gitweb_check_feature('ctags');
4321 PROJECT:
4322 foreach my $pr (@$projlist) {
4323 my (@activity) = git_get_last_activity($pr->{'path'});
4324 unless (@activity) {
4325 next PROJECT;
4327 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4328 if (!defined $pr->{'descr'}) {
4329 my $descr = git_get_project_description($pr->{'path'}) || "";
4330 $descr = to_utf8($descr);
4331 $pr->{'descr_long'} = $descr;
4332 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4334 if (!defined $pr->{'owner'}) {
4335 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4337 if ($check_forks) {
4338 my $pname = $pr->{'path'};
4339 if (($pname =~ s/\.git$//) &&
4340 ($pname !~ /\/$/) &&
4341 (-d "$projectroot/$pname")) {
4342 $pr->{'forks'} = "-d $projectroot/$pname";
4343 } else {
4344 $pr->{'forks'} = 0;
4347 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4348 push @projects, $pr;
4351 return @projects;
4354 # print 'sort by' <th> element, generating 'sort by $name' replay link
4355 # if that order is not selected
4356 sub print_sort_th {
4357 print format_sort_th(@_);
4360 sub format_sort_th {
4361 my ($name, $order, $header) = @_;
4362 my $sort_th = "";
4363 $header ||= ucfirst($name);
4365 if ($order eq $name) {
4366 $sort_th .= "<th>$header</th>\n";
4367 } else {
4368 $sort_th .= "<th>" .
4369 $cgi->a({-href => href(-replay=>1, order=>$name),
4370 -class => "header"}, $header) .
4371 "</th>\n";
4374 return $sort_th;
4377 sub git_project_list_body {
4378 # actually uses global variable $project
4379 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4381 my $check_forks = gitweb_check_feature('forks');
4382 my @projects = fill_project_list_info($projlist, $check_forks);
4384 $order ||= $default_projects_order;
4385 $from = 0 unless defined $from;
4386 $to = $#projects if (!defined $to || $#projects < $to);
4388 my %order_info = (
4389 project => { key => 'path', type => 'str' },
4390 descr => { key => 'descr_long', type => 'str' },
4391 owner => { key => 'owner', type => 'str' },
4392 age => { key => 'age', type => 'num' }
4394 my $oi = $order_info{$order};
4395 if ($oi->{'type'} eq 'str') {
4396 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4397 } else {
4398 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4401 my $show_ctags = gitweb_check_feature('ctags');
4402 if ($show_ctags) {
4403 my %ctags;
4404 foreach my $p (@projects) {
4405 foreach my $ct (keys %{$p->{'ctags'}}) {
4406 $ctags{$ct} += $p->{'ctags'}->{$ct};
4409 my $cloud = git_populate_project_tagcloud(\%ctags);
4410 print git_show_project_tagcloud($cloud, 64);
4413 print "<table class=\"project_list\">\n";
4414 unless ($no_header) {
4415 print "<tr>\n";
4416 if ($check_forks) {
4417 print "<th></th>\n";
4419 print_sort_th('project', $order, 'Project');
4420 print_sort_th('descr', $order, 'Description');
4421 print_sort_th('owner', $order, 'Owner');
4422 print_sort_th('age', $order, 'Last Change');
4423 print "<th></th>\n" . # for links
4424 "</tr>\n";
4426 my $alternate = 1;
4427 my $tagfilter = $cgi->param('by_tag');
4428 for (my $i = $from; $i <= $to; $i++) {
4429 my $pr = $projects[$i];
4431 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4432 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4433 and not $pr->{'descr_long'} =~ /$searchtext/;
4434 # Weed out forks or non-matching entries of search
4435 if ($check_forks) {
4436 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4437 $forkbase="^$forkbase" if $forkbase;
4438 next if not $searchtext and not $tagfilter and $show_ctags
4439 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4442 if ($alternate) {
4443 print "<tr class=\"dark\">\n";
4444 } else {
4445 print "<tr class=\"light\">\n";
4447 $alternate ^= 1;
4448 if ($check_forks) {
4449 print "<td>";
4450 if ($pr->{'forks'}) {
4451 print "<!-- $pr->{'forks'} -->\n";
4452 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4454 print "</td>\n";
4456 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4457 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4458 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4459 -class => "list", -title => $pr->{'descr_long'}},
4460 esc_html($pr->{'descr'})) . "</td>\n" .
4461 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4462 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4463 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4464 "<td class=\"link\">" .
4465 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4466 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
4467 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4468 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4469 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4470 "</td>\n" .
4471 "</tr>\n";
4473 if (defined $extra) {
4474 print "<tr>\n";
4475 if ($check_forks) {
4476 print "<td></td>\n";
4478 print "<td colspan=\"5\">$extra</td>\n" .
4479 "</tr>\n";
4481 print "</table>\n";
4484 sub git_log_body {
4485 # uses global variable $project
4486 my ($commitlist, $from, $to, $refs, $extra) = @_;
4488 $from = 0 unless defined $from;
4489 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4491 for (my $i = 0; $i <= $to; $i++) {
4492 my %co = %{$commitlist->[$i]};
4493 next if !%co;
4494 my $commit = $co{'id'};
4495 my $ref = format_ref_marker($refs, $commit);
4496 my %ad = parse_date($co{'author_epoch'});
4497 git_print_header_div('commit',
4498 "<span class=\"age\">$co{'age_string'}</span>" .
4499 esc_html($co{'title'}) . $ref,
4500 $commit);
4501 print "<div class=\"title_text\">\n" .
4502 "<div class=\"log_link\">\n" .
4503 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4504 " | " .
4505 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4506 " | " .
4507 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4508 "<br/>\n" .
4509 "</div>\n";
4510 git_print_authorship(\%co, -tag => 'span');
4511 print "<br/>\n</div>\n";
4513 print "<div class=\"log_body\">\n";
4514 git_print_log($co{'comment'}, -final_empty_line=> 1);
4515 print "</div>\n";
4517 if ($extra) {
4518 print "<div class=\"page_nav\">\n";
4519 print "$extra\n";
4520 print "</div>\n";
4524 sub git_shortlog_body {
4525 # uses global variable $project
4526 my ($commitlist, $from, $to, $refs, $extra) = @_;
4528 $from = 0 unless defined $from;
4529 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4531 print "<table class=\"shortlog\">\n";
4532 my $alternate = 1;
4533 for (my $i = $from; $i <= $to; $i++) {
4534 my %co = %{$commitlist->[$i]};
4535 my $commit = $co{'id'};
4536 my $ref = format_ref_marker($refs, $commit);
4537 if ($alternate) {
4538 print "<tr class=\"dark\">\n";
4539 } else {
4540 print "<tr class=\"light\">\n";
4542 $alternate ^= 1;
4543 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4544 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4545 format_author_html('td', \%co, 10) . "<td>";
4546 print format_subject_html($co{'title'}, $co{'title_short'},
4547 href(action=>"commit", hash=>$commit), $ref);
4548 print "</td>\n" .
4549 "<td class=\"link\">" .
4550 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4551 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4552 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4553 my $snapshot_links = format_snapshot_links($commit);
4554 if (defined $snapshot_links) {
4555 print " | " . $snapshot_links;
4557 print "</td>\n" .
4558 "</tr>\n";
4560 if (defined $extra) {
4561 print "<tr>\n" .
4562 "<td colspan=\"4\">$extra</td>\n" .
4563 "</tr>\n";
4565 print "</table>\n";
4568 sub git_history_body {
4569 # Warning: assumes constant type (blob or tree) during history
4570 my ($commitlist, $from, $to, $refs, $extra,
4571 $file_name, $file_hash, $ftype) = @_;
4573 $from = 0 unless defined $from;
4574 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4576 print "<table class=\"history\">\n";
4577 my $alternate = 1;
4578 for (my $i = $from; $i <= $to; $i++) {
4579 my %co = %{$commitlist->[$i]};
4580 if (!%co) {
4581 next;
4583 my $commit = $co{'id'};
4585 my $ref = format_ref_marker($refs, $commit);
4587 if ($alternate) {
4588 print "<tr class=\"dark\">\n";
4589 } else {
4590 print "<tr class=\"light\">\n";
4592 $alternate ^= 1;
4593 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4594 # shortlog: format_author_html('td', \%co, 10)
4595 format_author_html('td', \%co, 15, 3) . "<td>";
4596 # originally git_history used chop_str($co{'title'}, 50)
4597 print format_subject_html($co{'title'}, $co{'title_short'},
4598 href(action=>"commit", hash=>$commit), $ref);
4599 print "</td>\n" .
4600 "<td class=\"link\">" .
4601 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4602 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4604 if ($ftype eq 'blob') {
4605 my $blob_current = $file_hash;
4606 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4607 if (defined $blob_current && defined $blob_parent &&
4608 $blob_current ne $blob_parent) {
4609 print " | " .
4610 $cgi->a({-href => href(action=>"blobdiff",
4611 hash=>$blob_current, hash_parent=>$blob_parent,
4612 hash_base=>$hash_base, hash_parent_base=>$commit,
4613 file_name=>$file_name)},
4614 "diff to current");
4617 print "</td>\n" .
4618 "</tr>\n";
4620 if (defined $extra) {
4621 print "<tr>\n" .
4622 "<td colspan=\"4\">$extra</td>\n" .
4623 "</tr>\n";
4625 print "</table>\n";
4628 sub git_tags_body {
4629 # uses global variable $project
4630 my ($taglist, $from, $to, $extra) = @_;
4631 $from = 0 unless defined $from;
4632 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4634 print "<table class=\"tags\">\n";
4635 my $alternate = 1;
4636 for (my $i = $from; $i <= $to; $i++) {
4637 my $entry = $taglist->[$i];
4638 my %tag = %$entry;
4639 my $comment = $tag{'subject'};
4640 my $comment_short;
4641 if (defined $comment) {
4642 $comment_short = chop_str($comment, 30, 5);
4644 if ($alternate) {
4645 print "<tr class=\"dark\">\n";
4646 } else {
4647 print "<tr class=\"light\">\n";
4649 $alternate ^= 1;
4650 if (defined $tag{'age'}) {
4651 print "<td><i>$tag{'age'}</i></td>\n";
4652 } else {
4653 print "<td></td>\n";
4655 print "<td>" .
4656 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4657 -class => "list name"}, esc_html($tag{'name'})) .
4658 "</td>\n" .
4659 "<td>";
4660 if (defined $comment) {
4661 print format_subject_html($comment, $comment_short,
4662 href(action=>"tag", hash=>$tag{'id'}));
4664 print "</td>\n" .
4665 "<td class=\"selflink\">";
4666 if ($tag{'type'} eq "tag") {
4667 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4668 } else {
4669 print "&nbsp;";
4671 print "</td>\n" .
4672 "<td class=\"link\">" . " | " .
4673 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4674 if ($tag{'reftype'} eq "commit") {
4675 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4676 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
4677 } elsif ($tag{'reftype'} eq "blob") {
4678 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4680 print "</td>\n" .
4681 "</tr>";
4683 if (defined $extra) {
4684 print "<tr>\n" .
4685 "<td colspan=\"5\">$extra</td>\n" .
4686 "</tr>\n";
4688 print "</table>\n";
4691 sub git_heads_body {
4692 # uses global variable $project
4693 my ($headlist, $head, $from, $to, $extra) = @_;
4694 $from = 0 unless defined $from;
4695 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4697 print "<table class=\"heads\">\n";
4698 my $alternate = 1;
4699 for (my $i = $from; $i <= $to; $i++) {
4700 my $entry = $headlist->[$i];
4701 my %ref = %$entry;
4702 my $curr = $ref{'id'} eq $head;
4703 if ($alternate) {
4704 print "<tr class=\"dark\">\n";
4705 } else {
4706 print "<tr class=\"light\">\n";
4708 $alternate ^= 1;
4709 print "<td><i>$ref{'age'}</i></td>\n" .
4710 ($curr ? "<td class=\"current_head\">" : "<td>") .
4711 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4712 -class => "list name"},esc_html($ref{'name'})) .
4713 "</td>\n" .
4714 "<td class=\"link\">" .
4715 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4716 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4717 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4718 "</td>\n" .
4719 "</tr>";
4721 if (defined $extra) {
4722 print "<tr>\n" .
4723 "<td colspan=\"3\">$extra</td>\n" .
4724 "</tr>\n";
4726 print "</table>\n";
4729 sub git_search_grep_body {
4730 my ($commitlist, $from, $to, $extra) = @_;
4731 $from = 0 unless defined $from;
4732 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4734 print "<table class=\"commit_search\">\n";
4735 my $alternate = 1;
4736 for (my $i = $from; $i <= $to; $i++) {
4737 my %co = %{$commitlist->[$i]};
4738 if (!%co) {
4739 next;
4741 my $commit = $co{'id'};
4742 if ($alternate) {
4743 print "<tr class=\"dark\">\n";
4744 } else {
4745 print "<tr class=\"light\">\n";
4747 $alternate ^= 1;
4748 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4749 format_author_html('td', \%co, 15, 5) .
4750 "<td>" .
4751 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4752 -class => "list subject"},
4753 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4754 my $comment = $co{'comment'};
4755 foreach my $line (@$comment) {
4756 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4757 my ($lead, $match, $trail) = ($1, $2, $3);
4758 $match = chop_str($match, 70, 5, 'center');
4759 my $contextlen = int((80 - length($match))/2);
4760 $contextlen = 30 if ($contextlen > 30);
4761 $lead = chop_str($lead, $contextlen, 10, 'left');
4762 $trail = chop_str($trail, $contextlen, 10, 'right');
4764 $lead = esc_html($lead);
4765 $match = esc_html($match);
4766 $trail = esc_html($trail);
4768 print "$lead<span class=\"match\">$match</span>$trail<br />";
4771 print "</td>\n" .
4772 "<td class=\"link\">" .
4773 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4774 " | " .
4775 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4776 " | " .
4777 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4778 print "</td>\n" .
4779 "</tr>\n";
4781 if (defined $extra) {
4782 print "<tr>\n" .
4783 "<td colspan=\"3\">$extra</td>\n" .
4784 "</tr>\n";
4786 print "</table>\n";
4789 ## ======================================================================
4790 ## ======================================================================
4791 ## actions
4793 sub git_project_list {
4794 my $order = $input_params{'order'};
4795 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4796 die_error(400, "Unknown order parameter");
4799 my @list = git_get_projects_list();
4800 if (!@list) {
4801 die_error(404, "No projects found");
4804 git_header_html();
4805 if (defined $home_text && -f $home_text) {
4806 print "<div class=\"index_include\">\n";
4807 insert_file($home_text);
4808 print "</div>\n";
4810 print $cgi->startform(-method => "get") .
4811 "<p class=\"projsearch\">Search:\n" .
4812 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4813 "</p>" .
4814 $cgi->end_form() . "\n";
4815 git_project_list_body(\@list, $order);
4816 git_footer_html();
4819 sub git_forks {
4820 my $order = $input_params{'order'};
4821 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4822 die_error(400, "Unknown order parameter");
4825 my @list = git_get_projects_list($project);
4826 if (!@list) {
4827 die_error(404, "No forks found");
4830 git_header_html();
4831 git_print_page_nav('','');
4832 git_print_header_div('summary', "$project forks");
4833 git_project_list_body(\@list, $order);
4834 git_footer_html();
4837 sub git_project_index {
4838 my @projects = git_get_projects_list($project);
4840 print $cgi->header(
4841 -type => 'text/plain',
4842 -charset => 'utf-8',
4843 -content_disposition => 'inline; filename="index.aux"');
4845 foreach my $pr (@projects) {
4846 if (!exists $pr->{'owner'}) {
4847 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4850 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4851 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4852 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4853 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4854 $path =~ s/ /\+/g;
4855 $owner =~ s/ /\+/g;
4857 print "$path $owner\n";
4861 sub git_summary {
4862 my $descr = git_get_project_description($project) || "none";
4863 my %co = parse_commit("HEAD");
4864 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4865 my $head = $co{'id'};
4867 my $owner = git_get_project_owner($project);
4869 my $refs = git_get_references();
4870 # These get_*_list functions return one more to allow us to see if
4871 # there are more ...
4872 my @taglist = git_get_tags_list(16);
4873 my @headlist = git_get_heads_list(16);
4874 my @forklist;
4875 my $check_forks = gitweb_check_feature('forks');
4877 if ($check_forks) {
4878 @forklist = git_get_projects_list($project);
4881 git_header_html();
4882 git_print_page_nav('summary','', $head);
4884 print "<div class=\"title\">&nbsp;</div>\n";
4885 print "<table class=\"projects_list\">\n" .
4886 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4887 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4888 if (defined $cd{'rfc2822'}) {
4889 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4892 # use per project git URL list in $projectroot/$project/cloneurl
4893 # or make project git URL from git base URL and project name
4894 my $url_tag = "URL";
4895 my @url_list = git_get_project_url_list($project);
4896 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4897 foreach my $git_url (@url_list) {
4898 next unless $git_url;
4899 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4900 $url_tag = "";
4903 # Tag cloud
4904 my $show_ctags = gitweb_check_feature('ctags');
4905 if ($show_ctags) {
4906 my $ctags = git_get_project_ctags($project);
4907 my $cloud = git_populate_project_tagcloud($ctags);
4908 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4909 print "</td>\n<td>" unless %$ctags;
4910 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4911 print "</td>\n<td>" if %$ctags;
4912 print git_show_project_tagcloud($cloud, 48);
4913 print "</td></tr>";
4916 print "</table>\n";
4918 # If XSS prevention is on, we don't include README.html.
4919 # TODO: Allow a readme in some safe format.
4920 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
4921 print "<div class=\"title\">readme</div>\n" .
4922 "<div class=\"readme\">\n";
4923 insert_file("$projectroot/$project/README.html");
4924 print "\n</div>\n"; # class="readme"
4927 # we need to request one more than 16 (0..15) to check if
4928 # those 16 are all
4929 my @commitlist = $head ? parse_commits($head, 17) : ();
4930 if (@commitlist) {
4931 git_print_header_div('shortlog');
4932 git_shortlog_body(\@commitlist, 0, 15, $refs,
4933 $#commitlist <= 15 ? undef :
4934 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4937 if (@taglist) {
4938 git_print_header_div('tags');
4939 git_tags_body(\@taglist, 0, 15,
4940 $#taglist <= 15 ? undef :
4941 $cgi->a({-href => href(action=>"tags")}, "..."));
4944 if (@headlist) {
4945 git_print_header_div('heads');
4946 git_heads_body(\@headlist, $head, 0, 15,
4947 $#headlist <= 15 ? undef :
4948 $cgi->a({-href => href(action=>"heads")}, "..."));
4951 if (@forklist) {
4952 git_print_header_div('forks');
4953 git_project_list_body(\@forklist, 'age', 0, 15,
4954 $#forklist <= 15 ? undef :
4955 $cgi->a({-href => href(action=>"forks")}, "..."),
4956 'no_header');
4959 git_footer_html();
4962 sub git_tag {
4963 my $head = git_get_head_hash($project);
4964 git_header_html();
4965 git_print_page_nav('','', $head,undef,$head);
4966 my %tag = parse_tag($hash);
4968 if (! %tag) {
4969 die_error(404, "Unknown tag object");
4972 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4973 print "<div class=\"title_text\">\n" .
4974 "<table class=\"object_header\">\n" .
4975 "<tr>\n" .
4976 "<td>object</td>\n" .
4977 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4978 $tag{'object'}) . "</td>\n" .
4979 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4980 $tag{'type'}) . "</td>\n" .
4981 "</tr>\n";
4982 if (defined($tag{'author'})) {
4983 git_print_authorship_rows(\%tag, 'author');
4985 print "</table>\n\n" .
4986 "</div>\n";
4987 print "<div class=\"page_body\">";
4988 my $comment = $tag{'comment'};
4989 foreach my $line (@$comment) {
4990 chomp $line;
4991 print esc_html($line, -nbsp=>1) . "<br/>\n";
4993 print "</div>\n";
4994 git_footer_html();
4997 sub git_blame_common {
4998 my $format = shift || 'porcelain';
4999 if ($format eq 'porcelain' && $cgi->param('js')) {
5000 $format = 'incremental';
5001 $action = 'blame_incremental'; # for page title etc
5004 # permissions
5005 gitweb_check_feature('blame')
5006 or die_error(403, "Blame view not allowed");
5008 # error checking
5009 die_error(400, "No file name given") unless $file_name;
5010 $hash_base ||= git_get_head_hash($project);
5011 die_error(404, "Couldn't find base commit") unless $hash_base;
5012 my %co = parse_commit($hash_base)
5013 or die_error(404, "Commit not found");
5014 my $ftype = "blob";
5015 if (!defined $hash) {
5016 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5017 or die_error(404, "Error looking up file");
5018 } else {
5019 $ftype = git_get_type($hash);
5020 if ($ftype !~ "blob") {
5021 die_error(400, "Object is not a blob");
5025 my $fd;
5026 if ($format eq 'incremental') {
5027 # get file contents (as base)
5028 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5029 or die_error(500, "Open git-cat-file failed");
5030 } elsif ($format eq 'data') {
5031 # run git-blame --incremental
5032 open $fd, "-|", git_cmd(), "blame", "--incremental",
5033 $hash_base, "--", $file_name
5034 or die_error(500, "Open git-blame --incremental failed");
5035 } else {
5036 # run git-blame --porcelain
5037 open $fd, "-|", git_cmd(), "blame", '-p',
5038 $hash_base, '--', $file_name
5039 or die_error(500, "Open git-blame --porcelain failed");
5042 # incremental blame data returns early
5043 if ($format eq 'data') {
5044 print $cgi->header(
5045 -type=>"text/plain", -charset => "utf-8",
5046 -status=> "200 OK");
5047 local $| = 1; # output autoflush
5048 print while <$fd>;
5049 close $fd
5050 or print "ERROR $!\n";
5052 print 'END';
5053 if (defined $t0 && gitweb_check_feature('timed')) {
5054 print ' '.
5055 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
5056 ' '.$number_of_git_cmds;
5058 print "\n";
5060 return;
5063 # page header
5064 git_header_html();
5065 my $formats_nav =
5066 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5067 "blob") .
5068 " | ";
5069 if ($format eq 'incremental') {
5070 $formats_nav .=
5071 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
5072 "blame") . " (non-incremental)";
5073 } else {
5074 $formats_nav .=
5075 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
5076 "blame") . " (incremental)";
5078 $formats_nav .=
5079 " | " .
5080 $cgi->a({-href => href(action=>"history", -replay=>1)},
5081 "history") .
5082 " | " .
5083 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
5084 "HEAD");
5085 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5086 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5087 git_print_page_path($file_name, $ftype, $hash_base);
5089 # page body
5090 if ($format eq 'incremental') {
5091 print "<noscript>\n<div class=\"error\"><center><b>\n".
5092 "This page requires JavaScript to run.\n Use ".
5093 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
5094 'this page').
5095 " instead.\n".
5096 "</b></center></div>\n</noscript>\n";
5098 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
5101 print qq!<div class="page_body">\n!;
5102 print qq!<div id="progress_info">... / ...</div>\n!
5103 if ($format eq 'incremental');
5104 print qq!<table id="blame_table" class="blame" width="100%">\n!.
5105 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5106 qq!<thead>\n!.
5107 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
5108 qq!</thead>\n!.
5109 qq!<tbody>\n!;
5111 my @rev_color = qw(light dark);
5112 my $num_colors = scalar(@rev_color);
5113 my $current_color = 0;
5115 if ($format eq 'incremental') {
5116 my $color_class = $rev_color[$current_color];
5118 #contents of a file
5119 my $linenr = 0;
5120 LINE:
5121 while (my $line = <$fd>) {
5122 chomp $line;
5123 $linenr++;
5125 print qq!<tr id="l$linenr" class="$color_class">!.
5126 qq!<td class="sha1"><a href=""> </a></td>!.
5127 qq!<td class="linenr">!.
5128 qq!<a class="linenr" href="">$linenr</a></td>!;
5129 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
5130 print qq!</tr>\n!;
5133 } else { # porcelain, i.e. ordinary blame
5134 my %metainfo = (); # saves information about commits
5136 # blame data
5137 LINE:
5138 while (my $line = <$fd>) {
5139 chomp $line;
5140 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5141 # no <lines in group> for subsequent lines in group of lines
5142 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5143 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5144 if (!exists $metainfo{$full_rev}) {
5145 $metainfo{$full_rev} = { 'nprevious' => 0 };
5147 my $meta = $metainfo{$full_rev};
5148 my $data;
5149 while ($data = <$fd>) {
5150 chomp $data;
5151 last if ($data =~ s/^\t//); # contents of line
5152 if ($data =~ /^(\S+)(?: (.*))?$/) {
5153 $meta->{$1} = $2 unless exists $meta->{$1};
5155 if ($data =~ /^previous /) {
5156 $meta->{'nprevious'}++;
5159 my $short_rev = substr($full_rev, 0, 8);
5160 my $author = $meta->{'author'};
5161 my %date =
5162 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5163 my $date = $date{'iso-tz'};
5164 if ($group_size) {
5165 $current_color = ($current_color + 1) % $num_colors;
5167 my $tr_class = $rev_color[$current_color];
5168 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5169 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5170 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5171 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5172 if ($group_size) {
5173 print "<td class=\"sha1\"";
5174 print " title=\"". esc_html($author) . ", $date\"";
5175 print " rowspan=\"$group_size\"" if ($group_size > 1);
5176 print ">";
5177 print $cgi->a({-href => href(action=>"commit",
5178 hash=>$full_rev,
5179 file_name=>$file_name)},
5180 esc_html($short_rev));
5181 if ($group_size >= 2) {
5182 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5183 if (@author_initials) {
5184 print "<br />" .
5185 esc_html(join('', @author_initials));
5186 # or join('.', ...)
5189 print "</td>\n";
5191 # 'previous' <sha1 of parent commit> <filename at commit>
5192 if (exists $meta->{'previous'} &&
5193 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5194 $meta->{'parent'} = $1;
5195 $meta->{'file_parent'} = unquote($2);
5197 my $linenr_commit =
5198 exists($meta->{'parent'}) ?
5199 $meta->{'parent'} : $full_rev;
5200 my $linenr_filename =
5201 exists($meta->{'file_parent'}) ?
5202 $meta->{'file_parent'} : unquote($meta->{'filename'});
5203 my $blamed = href(action => 'blame',
5204 file_name => $linenr_filename,
5205 hash_base => $linenr_commit);
5206 print "<td class=\"linenr\">";
5207 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5208 -class => "linenr" },
5209 esc_html($lineno));
5210 print "</td>";
5211 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5212 print "</tr>\n";
5213 } # end while
5217 # footer
5218 print "</tbody>\n".
5219 "</table>\n"; # class="blame"
5220 print "</div>\n"; # class="blame_body"
5221 close $fd
5222 or print "Reading blob failed\n";
5224 git_footer_html();
5227 sub git_blame {
5228 git_blame_common();
5231 sub git_blame_incremental {
5232 git_blame_common('incremental');
5235 sub git_blame_data {
5236 git_blame_common('data');
5239 sub git_tags {
5240 my $head = git_get_head_hash($project);
5241 git_header_html();
5242 git_print_page_nav('','', $head,undef,$head);
5243 git_print_header_div('summary', $project);
5245 my @tagslist = git_get_tags_list();
5246 if (@tagslist) {
5247 git_tags_body(\@tagslist);
5249 git_footer_html();
5252 sub git_heads {
5253 my $head = git_get_head_hash($project);
5254 git_header_html();
5255 git_print_page_nav('','', $head,undef,$head);
5256 git_print_header_div('summary', $project);
5258 my @headslist = git_get_heads_list();
5259 if (@headslist) {
5260 git_heads_body(\@headslist, $head);
5262 git_footer_html();
5265 sub git_blob_plain {
5266 my $type = shift;
5267 my $expires;
5269 if (!defined $hash) {
5270 if (defined $file_name) {
5271 my $base = $hash_base || git_get_head_hash($project);
5272 $hash = git_get_hash_by_path($base, $file_name, "blob")
5273 or die_error(404, "Cannot find file");
5274 } else {
5275 die_error(400, "No file name defined");
5277 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5278 # blobs defined by non-textual hash id's can be cached
5279 $expires = "+1d";
5282 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5283 or die_error(500, "Open git-cat-file blob '$hash' failed");
5285 # content-type (can include charset)
5286 $type = blob_contenttype($fd, $file_name, $type);
5288 # "save as" filename, even when no $file_name is given
5289 my $save_as = "$hash";
5290 if (defined $file_name) {
5291 $save_as = $file_name;
5292 } elsif ($type =~ m/^text\//) {
5293 $save_as .= '.txt';
5296 # With XSS prevention on, blobs of all types except a few known safe
5297 # ones are served with "Content-Disposition: attachment" to make sure
5298 # they don't run in our security domain. For certain image types,
5299 # blob view writes an <img> tag referring to blob_plain view, and we
5300 # want to be sure not to break that by serving the image as an
5301 # attachment (though Firefox 3 doesn't seem to care).
5302 my $sandbox = $prevent_xss &&
5303 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5305 print $cgi->header(
5306 -type => $type,
5307 -expires => $expires,
5308 -content_disposition =>
5309 ($sandbox ? 'attachment' : 'inline')
5310 . '; filename="' . $save_as . '"');
5311 local $/ = undef;
5312 binmode STDOUT, ':raw';
5313 print <$fd>;
5314 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5315 close $fd;
5318 sub git_blob {
5319 my $expires;
5321 if (!defined $hash) {
5322 if (defined $file_name) {
5323 my $base = $hash_base || git_get_head_hash($project);
5324 $hash = git_get_hash_by_path($base, $file_name, "blob")
5325 or die_error(404, "Cannot find file");
5326 } else {
5327 die_error(400, "No file name defined");
5329 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5330 # blobs defined by non-textual hash id's can be cached
5331 $expires = "+1d";
5334 my $have_blame = gitweb_check_feature('blame');
5335 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5336 or die_error(500, "Couldn't cat $file_name, $hash");
5337 my $mimetype = blob_mimetype($fd, $file_name);
5338 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5339 close $fd;
5340 return git_blob_plain($mimetype);
5342 # we can have blame only for text/* mimetype
5343 $have_blame &&= ($mimetype =~ m!^text/!);
5345 git_header_html(undef, $expires);
5346 my $formats_nav = '';
5347 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5348 if (defined $file_name) {
5349 if ($have_blame) {
5350 $formats_nav .=
5351 $cgi->a({-href => href(action=>"blame", -replay=>1)},
5352 "blame") .
5353 " | ";
5355 $formats_nav .=
5356 $cgi->a({-href => href(action=>"history", -replay=>1)},
5357 "history") .
5358 " | " .
5359 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5360 "raw") .
5361 " | " .
5362 $cgi->a({-href => href(action=>"blob",
5363 hash_base=>"HEAD", file_name=>$file_name)},
5364 "HEAD");
5365 } else {
5366 $formats_nav .=
5367 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5368 "raw");
5370 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5371 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5372 } else {
5373 print "<div class=\"page_nav\">\n" .
5374 "<br/><br/></div>\n" .
5375 "<div class=\"title\">$hash</div>\n";
5377 git_print_page_path($file_name, "blob", $hash_base);
5378 print "<div class=\"page_body\">\n";
5379 if ($mimetype =~ m!^image/!) {
5380 print qq!<img type="$mimetype"!;
5381 if ($file_name) {
5382 print qq! alt="$file_name" title="$file_name"!;
5384 print qq! src="! .
5385 href(action=>"blob_plain", hash=>$hash,
5386 hash_base=>$hash_base, file_name=>$file_name) .
5387 qq!" />\n!;
5388 } else {
5389 my $nr;
5390 while (my $line = <$fd>) {
5391 chomp $line;
5392 $nr++;
5393 $line = untabify($line);
5394 printf "<div class=\"pre\"><a id=\"l%i\" href=\"" . href(-replay => 1)
5395 . "#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
5396 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
5399 close $fd
5400 or print "Reading blob failed.\n";
5401 print "</div>";
5402 git_footer_html();
5405 sub git_tree {
5406 if (!defined $hash_base) {
5407 $hash_base = "HEAD";
5409 if (!defined $hash) {
5410 if (defined $file_name) {
5411 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5412 } else {
5413 $hash = $hash_base;
5416 die_error(404, "No such tree") unless defined($hash);
5418 my $show_sizes = gitweb_check_feature('show-sizes');
5419 my $have_blame = gitweb_check_feature('blame');
5421 my @entries = ();
5423 local $/ = "\0";
5424 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5425 ($show_sizes ? '-l' : ()), @extra_options, $hash
5426 or die_error(500, "Open git-ls-tree failed");
5427 @entries = map { chomp; $_ } <$fd>;
5428 close $fd
5429 or die_error(404, "Reading tree failed");
5432 my $refs = git_get_references();
5433 my $ref = format_ref_marker($refs, $hash_base);
5434 git_header_html();
5435 my $basedir = '';
5436 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5437 my @views_nav = ();
5438 if (defined $file_name) {
5439 push @views_nav,
5440 $cgi->a({-href => href(action=>"history", -replay=>1)},
5441 "history"),
5442 $cgi->a({-href => href(action=>"tree",
5443 hash_base=>"HEAD", file_name=>$file_name)},
5444 "HEAD"),
5446 my $snapshot_links = format_snapshot_links($hash);
5447 if (defined $snapshot_links) {
5448 # FIXME: Should be available when we have no hash base as well.
5449 push @views_nav, $snapshot_links;
5451 git_print_page_nav('tree','', $hash_base, undef, undef,
5452 join(' | ', @views_nav));
5453 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5454 } else {
5455 undef $hash_base;
5456 print "<div class=\"page_nav\">\n";
5457 print "<br/><br/></div>\n";
5458 print "<div class=\"title\">$hash</div>\n";
5460 if (defined $file_name) {
5461 $basedir = $file_name;
5462 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5463 $basedir .= '/';
5465 git_print_page_path($file_name, 'tree', $hash_base);
5467 print "<div class=\"page_body\">\n";
5468 print "<table class=\"tree\">\n";
5469 my $alternate = 1;
5470 # '..' (top directory) link if possible
5471 if (defined $hash_base &&
5472 defined $file_name && $file_name =~ m![^/]+$!) {
5473 if ($alternate) {
5474 print "<tr class=\"dark\">\n";
5475 } else {
5476 print "<tr class=\"light\">\n";
5478 $alternate ^= 1;
5480 my $up = $file_name;
5481 $up =~ s!/?[^/]+$!!;
5482 undef $up unless $up;
5483 # based on git_print_tree_entry
5484 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5485 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
5486 print '<td class="list">';
5487 print $cgi->a({-href => href(action=>"tree",
5488 hash_base=>$hash_base,
5489 file_name=>$up)},
5490 "..");
5491 print "</td>\n";
5492 print "<td class=\"link\"></td>\n";
5494 print "</tr>\n";
5496 foreach my $line (@entries) {
5497 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
5499 if ($alternate) {
5500 print "<tr class=\"dark\">\n";
5501 } else {
5502 print "<tr class=\"light\">\n";
5504 $alternate ^= 1;
5506 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
5508 print "</tr>\n";
5510 print "</table>\n" .
5511 "</div>";
5512 git_footer_html();
5515 sub snapshot_name {
5516 my ($project, $hash) = @_;
5518 # path/to/project.git -> project
5519 # path/to/project/.git -> project
5520 my $name = to_utf8($project);
5521 $name =~ s,([^/])/*\.git$,$1,;
5522 $name = basename($name);
5523 # sanitize name
5524 $name =~ s/[[:cntrl:]]/?/g;
5526 my $ver = $hash;
5527 if ($hash =~ /^[0-9a-fA-F]+$/) {
5528 # shorten SHA-1 hash
5529 my $full_hash = git_get_full_hash($project, $hash);
5530 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
5531 $ver = git_get_short_hash($project, $hash);
5533 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
5534 # tags don't need shortened SHA-1 hash
5535 $ver = $1;
5536 } else {
5537 # branches and other need shortened SHA-1 hash
5538 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
5539 $ver = $1;
5541 $ver .= '-' . git_get_short_hash($project, $hash);
5543 # in case of hierarchical branch names
5544 $ver =~ s!/!.!g;
5546 # name = project-version_string
5547 $name = "$name-$ver";
5549 return wantarray ? ($name, $name) : $name;
5552 sub git_snapshot {
5553 my $format = $input_params{'snapshot_format'};
5554 if (!@snapshot_fmts) {
5555 die_error(403, "Snapshots not allowed");
5557 # default to first supported snapshot format
5558 $format ||= $snapshot_fmts[0];
5559 if ($format !~ m/^[a-z0-9]+$/) {
5560 die_error(400, "Invalid snapshot format parameter");
5561 } elsif (!exists($known_snapshot_formats{$format})) {
5562 die_error(400, "Unknown snapshot format");
5563 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
5564 die_error(403, "Snapshot format not allowed");
5565 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5566 die_error(403, "Unsupported snapshot format");
5569 my $type = git_get_type("$hash^{}");
5570 if (!$type) {
5571 die_error(404, 'Object does not exist');
5572 } elsif ($type eq 'blob') {
5573 die_error(400, 'Object is not a tree-ish');
5576 my ($name, $prefix) = snapshot_name($project, $hash);
5577 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
5578 my $cmd = quote_command(
5579 git_cmd(), 'archive',
5580 "--format=$known_snapshot_formats{$format}{'format'}",
5581 "--prefix=$prefix/", $hash);
5582 if (exists $known_snapshot_formats{$format}{'compressor'}) {
5583 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
5586 $filename =~ s/(["\\])/\\$1/g;
5587 print $cgi->header(
5588 -type => $known_snapshot_formats{$format}{'type'},
5589 -content_disposition => 'inline; filename="' . $filename . '"',
5590 -status => '200 OK');
5592 open my $fd, "-|", $cmd
5593 or die_error(500, "Execute git-archive failed");
5594 binmode STDOUT, ':raw';
5595 print <$fd>;
5596 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5597 close $fd;
5600 sub git_log_generic {
5601 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
5603 my $head = git_get_head_hash($project);
5604 if (!defined $base) {
5605 $base = $head;
5607 if (!defined $page) {
5608 $page = 0;
5610 my $refs = git_get_references();
5612 my $commit_hash = $base;
5613 if (defined $parent) {
5614 $commit_hash = "$parent..$base";
5616 my @commitlist =
5617 parse_commits($commit_hash, 101, (100 * $page),
5618 defined $file_name ? ($file_name, "--full-history") : ());
5620 my $ftype;
5621 if (!defined $file_hash && defined $file_name) {
5622 # some commits could have deleted file in question,
5623 # and not have it in tree, but one of them has to have it
5624 for (my $i = 0; $i < @commitlist; $i++) {
5625 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5626 last if defined $file_hash;
5629 if (defined $file_hash) {
5630 $ftype = git_get_type($file_hash);
5632 if (defined $file_name && !defined $ftype) {
5633 die_error(500, "Unknown type of object");
5635 my %co;
5636 if (defined $file_name) {
5637 %co = parse_commit($base)
5638 or die_error(404, "Unknown commit object");
5642 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
5643 my $next_link = '';
5644 if ($#commitlist >= 100) {
5645 $next_link =
5646 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5647 -accesskey => "n", -title => "Alt-n"}, "next");
5649 my $patch_max = gitweb_get_feature('patches');
5650 if ($patch_max && !defined $file_name) {
5651 if ($patch_max < 0 || @commitlist <= $patch_max) {
5652 $paging_nav .= " &sdot; " .
5653 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5654 "patches");
5658 git_header_html();
5659 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
5660 if (defined $file_name) {
5661 git_print_header_div('commit', esc_html($co{'title'}), $base);
5662 } else {
5663 git_print_header_div('summary', $project)
5665 git_print_page_path($file_name, $ftype, $hash_base)
5666 if (defined $file_name);
5668 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
5669 $file_name, $file_hash, $ftype);
5671 git_footer_html();
5674 sub git_log {
5675 git_log_generic('log', \&git_log_body,
5676 $hash, $hash_parent);
5679 sub git_commit {
5680 $hash ||= $hash_base || "HEAD";
5681 my %co = parse_commit($hash)
5682 or die_error(404, "Unknown commit object");
5684 my $parent = $co{'parent'};
5685 my $parents = $co{'parents'}; # listref
5687 # we need to prepare $formats_nav before any parameter munging
5688 my $formats_nav;
5689 if (!defined $parent) {
5690 # --root commitdiff
5691 $formats_nav .= '(initial)';
5692 } elsif (@$parents == 1) {
5693 # single parent commit
5694 $formats_nav .=
5695 '(parent: ' .
5696 $cgi->a({-href => href(action=>"commit",
5697 hash=>$parent)},
5698 esc_html(substr($parent, 0, 7))) .
5699 ')';
5700 } else {
5701 # merge commit
5702 $formats_nav .=
5703 '(merge: ' .
5704 join(' ', map {
5705 $cgi->a({-href => href(action=>"commit",
5706 hash=>$_)},
5707 esc_html(substr($_, 0, 7)));
5708 } @$parents ) .
5709 ')';
5711 if (gitweb_check_feature('patches') && @$parents <= 1) {
5712 $formats_nav .= " | " .
5713 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5714 "patch");
5717 if (!defined $parent) {
5718 $parent = "--root";
5720 my @difftree;
5721 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5722 @diff_opts,
5723 (@$parents <= 1 ? $parent : '-c'),
5724 $hash, "--"
5725 or die_error(500, "Open git-diff-tree failed");
5726 @difftree = map { chomp; $_ } <$fd>;
5727 close $fd or die_error(404, "Reading git-diff-tree failed");
5729 # non-textual hash id's can be cached
5730 my $expires;
5731 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5732 $expires = "+1d";
5734 my $refs = git_get_references();
5735 my $ref = format_ref_marker($refs, $co{'id'});
5737 git_header_html(undef, $expires);
5738 git_print_page_nav('commit', '',
5739 $hash, $co{'tree'}, $hash,
5740 $formats_nav);
5742 if (defined $co{'parent'}) {
5743 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5744 } else {
5745 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5747 print "<div class=\"title_text\">\n" .
5748 "<table class=\"object_header\">\n";
5749 git_print_authorship_rows(\%co);
5750 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5751 print "<tr>" .
5752 "<td>tree</td>" .
5753 "<td class=\"sha1\">" .
5754 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5755 class => "list"}, $co{'tree'}) .
5756 "</td>" .
5757 "<td class=\"link\">" .
5758 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5759 "tree");
5760 my $snapshot_links = format_snapshot_links($hash);
5761 if (defined $snapshot_links) {
5762 print " | " . $snapshot_links;
5764 print "</td>" .
5765 "</tr>\n";
5767 foreach my $par (@$parents) {
5768 print "<tr>" .
5769 "<td>parent</td>" .
5770 "<td class=\"sha1\">" .
5771 $cgi->a({-href => href(action=>"commit", hash=>$par),
5772 class => "list"}, $par) .
5773 "</td>" .
5774 "<td class=\"link\">" .
5775 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
5776 " | " .
5777 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
5778 "</td>" .
5779 "</tr>\n";
5781 print "</table>".
5782 "</div>\n";
5784 print "<div class=\"page_body\">\n";
5785 git_print_log($co{'comment'});
5786 print "</div>\n";
5788 git_difftree_body(\@difftree, $hash, @$parents);
5790 git_footer_html();
5793 sub git_object {
5794 # object is defined by:
5795 # - hash or hash_base alone
5796 # - hash_base and file_name
5797 my $type;
5799 # - hash or hash_base alone
5800 if ($hash || ($hash_base && !defined $file_name)) {
5801 my $object_id = $hash || $hash_base;
5803 open my $fd, "-|", quote_command(
5804 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
5805 or die_error(404, "Object does not exist");
5806 $type = <$fd>;
5807 chomp $type;
5808 close $fd
5809 or die_error(404, "Object does not exist");
5811 # - hash_base and file_name
5812 } elsif ($hash_base && defined $file_name) {
5813 $file_name =~ s,/+$,,;
5815 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
5816 or die_error(404, "Base object does not exist");
5818 # here errors should not hapen
5819 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
5820 or die_error(500, "Open git-ls-tree failed");
5821 my $line = <$fd>;
5822 close $fd;
5824 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5825 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
5826 die_error(404, "File or directory for given base does not exist");
5828 $type = $2;
5829 $hash = $3;
5830 } else {
5831 die_error(400, "Not enough information to find object");
5834 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5835 hash=>$hash, hash_base=>$hash_base,
5836 file_name=>$file_name),
5837 -status => '302 Found');
5840 sub git_blobdiff {
5841 my $format = shift || 'html';
5843 my $fd;
5844 my @difftree;
5845 my %diffinfo;
5846 my $expires;
5848 # preparing $fd and %diffinfo for git_patchset_body
5849 # new style URI
5850 if (defined $hash_base && defined $hash_parent_base) {
5851 if (defined $file_name) {
5852 # read raw output
5853 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5854 $hash_parent_base, $hash_base,
5855 "--", (defined $file_parent ? $file_parent : ()), $file_name
5856 or die_error(500, "Open git-diff-tree failed");
5857 @difftree = map { chomp; $_ } <$fd>;
5858 close $fd
5859 or die_error(404, "Reading git-diff-tree failed");
5860 @difftree
5861 or die_error(404, "Blob diff not found");
5863 } elsif (defined $hash &&
5864 $hash =~ /[0-9a-fA-F]{40}/) {
5865 # try to find filename from $hash
5867 # read filtered raw output
5868 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5869 $hash_parent_base, $hash_base, "--"
5870 or die_error(500, "Open git-diff-tree failed");
5871 @difftree =
5872 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
5873 # $hash == to_id
5874 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
5875 map { chomp; $_ } <$fd>;
5876 close $fd
5877 or die_error(404, "Reading git-diff-tree failed");
5878 @difftree
5879 or die_error(404, "Blob diff not found");
5881 } else {
5882 die_error(400, "Missing one of the blob diff parameters");
5885 if (@difftree > 1) {
5886 die_error(400, "Ambiguous blob diff specification");
5889 %diffinfo = parse_difftree_raw_line($difftree[0]);
5890 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5891 $file_name ||= $diffinfo{'to_file'};
5893 $hash_parent ||= $diffinfo{'from_id'};
5894 $hash ||= $diffinfo{'to_id'};
5896 # non-textual hash id's can be cached
5897 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5898 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5899 $expires = '+1d';
5902 # open patch output
5903 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5904 '-p', ($format eq 'html' ? "--full-index" : ()),
5905 $hash_parent_base, $hash_base,
5906 "--", (defined $file_parent ? $file_parent : ()), $file_name
5907 or die_error(500, "Open git-diff-tree failed");
5910 # old/legacy style URI -- not generated anymore since 1.4.3.
5911 if (!%diffinfo) {
5912 die_error('404 Not Found', "Missing one of the blob diff parameters")
5915 # header
5916 if ($format eq 'html') {
5917 my $formats_nav =
5918 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5919 "raw");
5920 git_header_html(undef, $expires);
5921 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5922 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5923 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5924 } else {
5925 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5926 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5928 if (defined $file_name) {
5929 git_print_page_path($file_name, "blob", $hash_base);
5930 } else {
5931 print "<div class=\"page_path\"></div>\n";
5934 } elsif ($format eq 'plain') {
5935 print $cgi->header(
5936 -type => 'text/plain',
5937 -charset => 'utf-8',
5938 -expires => $expires,
5939 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5941 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5943 } else {
5944 die_error(400, "Unknown blobdiff format");
5947 # patch
5948 if ($format eq 'html') {
5949 print "<div class=\"page_body\">\n";
5951 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5952 close $fd;
5954 print "</div>\n"; # class="page_body"
5955 git_footer_html();
5957 } else {
5958 while (my $line = <$fd>) {
5959 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5960 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5962 print $line;
5964 last if $line =~ m!^\+\+\+!;
5966 local $/ = undef;
5967 print <$fd>;
5968 close $fd;
5972 sub git_blobdiff_plain {
5973 git_blobdiff('plain');
5976 sub git_commitdiff {
5977 my %params = @_;
5978 my $format = $params{-format} || 'html';
5980 my ($patch_max) = gitweb_get_feature('patches');
5981 if ($format eq 'patch') {
5982 die_error(403, "Patch view not allowed") unless $patch_max;
5985 $hash ||= $hash_base || "HEAD";
5986 my %co = parse_commit($hash)
5987 or die_error(404, "Unknown commit object");
5989 # choose format for commitdiff for merge
5990 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5991 $hash_parent = '--cc';
5993 # we need to prepare $formats_nav before almost any parameter munging
5994 my $formats_nav;
5995 if ($format eq 'html') {
5996 $formats_nav =
5997 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5998 "raw");
5999 if ($patch_max && @{$co{'parents'}} <= 1) {
6000 $formats_nav .= " | " .
6001 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6002 "patch");
6005 if (defined $hash_parent &&
6006 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6007 # commitdiff with two commits given
6008 my $hash_parent_short = $hash_parent;
6009 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6010 $hash_parent_short = substr($hash_parent, 0, 7);
6012 $formats_nav .=
6013 ' (from';
6014 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6015 if ($co{'parents'}[$i] eq $hash_parent) {
6016 $formats_nav .= ' parent ' . ($i+1);
6017 last;
6020 $formats_nav .= ': ' .
6021 $cgi->a({-href => href(action=>"commitdiff",
6022 hash=>$hash_parent)},
6023 esc_html($hash_parent_short)) .
6024 ')';
6025 } elsif (!$co{'parent'}) {
6026 # --root commitdiff
6027 $formats_nav .= ' (initial)';
6028 } elsif (scalar @{$co{'parents'}} == 1) {
6029 # single parent commit
6030 $formats_nav .=
6031 ' (parent: ' .
6032 $cgi->a({-href => href(action=>"commitdiff",
6033 hash=>$co{'parent'})},
6034 esc_html(substr($co{'parent'}, 0, 7))) .
6035 ')';
6036 } else {
6037 # merge commit
6038 if ($hash_parent eq '--cc') {
6039 $formats_nav .= ' | ' .
6040 $cgi->a({-href => href(action=>"commitdiff",
6041 hash=>$hash, hash_parent=>'-c')},
6042 'combined');
6043 } else { # $hash_parent eq '-c'
6044 $formats_nav .= ' | ' .
6045 $cgi->a({-href => href(action=>"commitdiff",
6046 hash=>$hash, hash_parent=>'--cc')},
6047 'compact');
6049 $formats_nav .=
6050 ' (merge: ' .
6051 join(' ', map {
6052 $cgi->a({-href => href(action=>"commitdiff",
6053 hash=>$_)},
6054 esc_html(substr($_, 0, 7)));
6055 } @{$co{'parents'}} ) .
6056 ')';
6060 my $hash_parent_param = $hash_parent;
6061 if (!defined $hash_parent_param) {
6062 # --cc for multiple parents, --root for parentless
6063 $hash_parent_param =
6064 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6067 # read commitdiff
6068 my $fd;
6069 my @difftree;
6070 if ($format eq 'html') {
6071 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6072 "--no-commit-id", "--patch-with-raw", "--full-index",
6073 $hash_parent_param, $hash, "--"
6074 or die_error(500, "Open git-diff-tree failed");
6076 while (my $line = <$fd>) {
6077 chomp $line;
6078 # empty line ends raw part of diff-tree output
6079 last unless $line;
6080 push @difftree, scalar parse_difftree_raw_line($line);
6083 } elsif ($format eq 'plain') {
6084 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6085 '-p', $hash_parent_param, $hash, "--"
6086 or die_error(500, "Open git-diff-tree failed");
6087 } elsif ($format eq 'patch') {
6088 # For commit ranges, we limit the output to the number of
6089 # patches specified in the 'patches' feature.
6090 # For single commits, we limit the output to a single patch,
6091 # diverging from the git-format-patch default.
6092 my @commit_spec = ();
6093 if ($hash_parent) {
6094 if ($patch_max > 0) {
6095 push @commit_spec, "-$patch_max";
6097 push @commit_spec, '-n', "$hash_parent..$hash";
6098 } else {
6099 if ($params{-single}) {
6100 push @commit_spec, '-1';
6101 } else {
6102 if ($patch_max > 0) {
6103 push @commit_spec, "-$patch_max";
6105 push @commit_spec, "-n";
6107 push @commit_spec, '--root', $hash;
6109 open $fd, "-|", git_cmd(), "format-patch", '--encoding=utf8',
6110 '--stdout', @commit_spec
6111 or die_error(500, "Open git-format-patch failed");
6112 } else {
6113 die_error(400, "Unknown commitdiff format");
6116 # non-textual hash id's can be cached
6117 my $expires;
6118 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6119 $expires = "+1d";
6122 # write commit message
6123 if ($format eq 'html') {
6124 my $refs = git_get_references();
6125 my $ref = format_ref_marker($refs, $co{'id'});
6127 git_header_html(undef, $expires);
6128 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6129 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6130 print "<div class=\"title_text\">\n" .
6131 "<table class=\"object_header\">\n";
6132 git_print_authorship_rows(\%co);
6133 print "</table>".
6134 "</div>\n";
6135 print "<div class=\"page_body\">\n";
6136 if (@{$co{'comment'}} > 1) {
6137 print "<div class=\"log\">\n";
6138 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6139 print "</div>\n"; # class="log"
6142 } elsif ($format eq 'plain') {
6143 my $refs = git_get_references("tags");
6144 my $tagname = git_get_rev_name_tags($hash);
6145 my $filename = basename($project) . "-$hash.patch";
6147 print $cgi->header(
6148 -type => 'text/plain',
6149 -charset => 'utf-8',
6150 -expires => $expires,
6151 -content_disposition => 'inline; filename="' . "$filename" . '"');
6152 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
6153 print "From: " . to_utf8($co{'author'}) . "\n";
6154 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6155 print "Subject: " . to_utf8($co{'title'}) . "\n";
6157 print "X-Git-Tag: $tagname\n" if $tagname;
6158 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6160 foreach my $line (@{$co{'comment'}}) {
6161 print to_utf8($line) . "\n";
6163 print "---\n\n";
6164 } elsif ($format eq 'patch') {
6165 my $filename = basename($project) . "-$hash.patch";
6167 print $cgi->header(
6168 -type => 'text/plain',
6169 -charset => 'utf-8',
6170 -expires => $expires,
6171 -content_disposition => 'inline; filename="' . "$filename" . '"');
6174 # write patch
6175 if ($format eq 'html') {
6176 my $use_parents = !defined $hash_parent ||
6177 $hash_parent eq '-c' || $hash_parent eq '--cc';
6178 git_difftree_body(\@difftree, $hash,
6179 $use_parents ? @{$co{'parents'}} : $hash_parent);
6180 print "<br/>\n";
6182 git_patchset_body($fd, \@difftree, $hash,
6183 $use_parents ? @{$co{'parents'}} : $hash_parent);
6184 close $fd;
6185 print "</div>\n"; # class="page_body"
6186 git_footer_html();
6188 } elsif ($format eq 'plain') {
6189 local $/ = undef;
6190 print <$fd>;
6191 close $fd
6192 or print "Reading git-diff-tree failed\n";
6193 } elsif ($format eq 'patch') {
6194 local $/ = undef;
6195 print <$fd>;
6196 close $fd
6197 or print "Reading git-format-patch failed\n";
6201 sub git_commitdiff_plain {
6202 git_commitdiff(-format => 'plain');
6205 # format-patch-style patches
6206 sub git_patch {
6207 git_commitdiff(-format => 'patch', -single => 1);
6210 sub git_patches {
6211 git_commitdiff(-format => 'patch');
6214 sub git_history {
6215 git_log_generic('history', \&git_history_body,
6216 $hash_base, $hash_parent_base,
6217 $file_name, $hash);
6220 sub git_search {
6221 gitweb_check_feature('search') or die_error(403, "Search is disabled");
6222 if (!defined $searchtext) {
6223 die_error(400, "Text field is empty");
6225 if (!defined $hash) {
6226 $hash = git_get_head_hash($project);
6228 my %co = parse_commit($hash);
6229 if (!%co) {
6230 die_error(404, "Unknown commit object");
6232 if (!defined $page) {
6233 $page = 0;
6236 $searchtype ||= 'commit';
6237 if ($searchtype eq 'pickaxe') {
6238 # pickaxe may take all resources of your box and run for several minutes
6239 # with every query - so decide by yourself how public you make this feature
6240 gitweb_check_feature('pickaxe')
6241 or die_error(403, "Pickaxe is disabled");
6243 if ($searchtype eq 'grep') {
6244 gitweb_check_feature('grep')
6245 or die_error(403, "Grep is disabled");
6248 git_header_html();
6250 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6251 my $greptype;
6252 if ($searchtype eq 'commit') {
6253 $greptype = "--grep=";
6254 } elsif ($searchtype eq 'author') {
6255 $greptype = "--author=";
6256 } elsif ($searchtype eq 'committer') {
6257 $greptype = "--committer=";
6259 $greptype .= $searchtext;
6260 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6261 $greptype, '--regexp-ignore-case',
6262 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6264 my $paging_nav = '';
6265 if ($page > 0) {
6266 $paging_nav .=
6267 $cgi->a({-href => href(action=>"search", hash=>$hash,
6268 searchtext=>$searchtext,
6269 searchtype=>$searchtype)},
6270 "first");
6271 $paging_nav .= " &sdot; " .
6272 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6273 -accesskey => "p", -title => "Alt-p"}, "prev");
6274 } else {
6275 $paging_nav .= "first";
6276 $paging_nav .= " &sdot; prev";
6278 my $next_link = '';
6279 if ($#commitlist >= 100) {
6280 $next_link =
6281 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6282 -accesskey => "n", -title => "Alt-n"}, "next");
6283 $paging_nav .= " &sdot; $next_link";
6284 } else {
6285 $paging_nav .= " &sdot; next";
6288 if ($#commitlist >= 100) {
6291 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6292 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6293 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6296 if ($searchtype eq 'pickaxe') {
6297 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6298 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6300 print "<table class=\"pickaxe search\">\n";
6301 my $alternate = 1;
6302 local $/ = "\n";
6303 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6304 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6305 ($search_use_regexp ? '--pickaxe-regex' : ());
6306 undef %co;
6307 my @files;
6308 while (my $line = <$fd>) {
6309 chomp $line;
6310 next unless $line;
6312 my %set = parse_difftree_raw_line($line);
6313 if (defined $set{'commit'}) {
6314 # finish previous commit
6315 if (%co) {
6316 print "</td>\n" .
6317 "<td class=\"link\">" .
6318 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6319 " | " .
6320 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6321 print "</td>\n" .
6322 "</tr>\n";
6325 if ($alternate) {
6326 print "<tr class=\"dark\">\n";
6327 } else {
6328 print "<tr class=\"light\">\n";
6330 $alternate ^= 1;
6331 %co = parse_commit($set{'commit'});
6332 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6333 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6334 "<td><i>$author</i></td>\n" .
6335 "<td>" .
6336 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6337 -class => "list subject"},
6338 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6339 } elsif (defined $set{'to_id'}) {
6340 next if ($set{'to_id'} =~ m/^0{40}$/);
6342 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6343 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6344 -class => "list"},
6345 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6346 "<br/>\n";
6349 close $fd;
6351 # finish last commit (warning: repetition!)
6352 if (%co) {
6353 print "</td>\n" .
6354 "<td class=\"link\">" .
6355 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6356 " | " .
6357 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6358 print "</td>\n" .
6359 "</tr>\n";
6362 print "</table>\n";
6365 if ($searchtype eq 'grep') {
6366 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6367 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6369 print "<table class=\"grep_search\">\n";
6370 my $alternate = 1;
6371 my $matches = 0;
6372 local $/ = "\n";
6373 open my $fd, "-|", git_cmd(), 'grep', '-n',
6374 $search_use_regexp ? ('-E', '-i') : '-F',
6375 $searchtext, $co{'tree'};
6376 my $lastfile = '';
6377 while (my $line = <$fd>) {
6378 chomp $line;
6379 my ($file, $lno, $ltext, $binary);
6380 last if ($matches++ > 1000);
6381 if ($line =~ /^Binary file (.+) matches$/) {
6382 $file = $1;
6383 $binary = 1;
6384 } else {
6385 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6387 if ($file ne $lastfile) {
6388 $lastfile and print "</td></tr>\n";
6389 if ($alternate++) {
6390 print "<tr class=\"dark\">\n";
6391 } else {
6392 print "<tr class=\"light\">\n";
6394 print "<td class=\"list\">".
6395 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6396 file_name=>"$file"),
6397 -class => "list"}, esc_path($file));
6398 print "</td><td>\n";
6399 $lastfile = $file;
6401 if ($binary) {
6402 print "<div class=\"binary\">Binary file</div>\n";
6403 } else {
6404 $ltext = untabify($ltext);
6405 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6406 $ltext = esc_html($1, -nbsp=>1);
6407 $ltext .= '<span class="match">';
6408 $ltext .= esc_html($2, -nbsp=>1);
6409 $ltext .= '</span>';
6410 $ltext .= esc_html($3, -nbsp=>1);
6411 } else {
6412 $ltext = esc_html($ltext, -nbsp=>1);
6414 print "<div class=\"pre\">" .
6415 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6416 file_name=>"$file").'#l'.$lno,
6417 -class => "linenr"}, sprintf('%4i', $lno))
6418 . ' ' . $ltext . "</div>\n";
6421 if ($lastfile) {
6422 print "</td></tr>\n";
6423 if ($matches > 1000) {
6424 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6426 } else {
6427 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6429 close $fd;
6431 print "</table>\n";
6433 git_footer_html();
6436 sub git_search_help {
6437 git_header_html();
6438 git_print_page_nav('','', $hash,$hash,$hash);
6439 print <<EOT;
6440 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6441 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6442 the pattern entered is recognized as the POSIX extended
6443 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6444 insensitive).</p>
6445 <dl>
6446 <dt><b>commit</b></dt>
6447 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6449 my $have_grep = gitweb_check_feature('grep');
6450 if ($have_grep) {
6451 print <<EOT;
6452 <dt><b>grep</b></dt>
6453 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6454 a different one) are searched for the given pattern. On large trees, this search can take
6455 a while and put some strain on the server, so please use it with some consideration. Note that
6456 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6457 case-sensitive.</dd>
6460 print <<EOT;
6461 <dt><b>author</b></dt>
6462 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6463 <dt><b>committer</b></dt>
6464 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6466 my $have_pickaxe = gitweb_check_feature('pickaxe');
6467 if ($have_pickaxe) {
6468 print <<EOT;
6469 <dt><b>pickaxe</b></dt>
6470 <dd>All commits that caused the string to appear or disappear from any file (changes that
6471 added, removed or "modified" the string) will be listed. This search can take a while and
6472 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6473 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6476 print "</dl>\n";
6477 git_footer_html();
6480 sub git_shortlog {
6481 git_log_generic('shortlog', \&git_shortlog_body,
6482 $hash, $hash_parent);
6485 ## ......................................................................
6486 ## feeds (RSS, Atom; OPML)
6488 sub git_feed {
6489 my $format = shift || 'atom';
6490 my $have_blame = gitweb_check_feature('blame');
6492 # Atom: http://www.atomenabled.org/developers/syndication/
6493 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6494 if ($format ne 'rss' && $format ne 'atom') {
6495 die_error(400, "Unknown web feed format");
6498 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6499 my $head = $hash || 'HEAD';
6500 my @commitlist = parse_commits($head, 150, 0, $file_name);
6502 my %latest_commit;
6503 my %latest_date;
6504 my $content_type = "application/$format+xml";
6505 if (defined $cgi->http('HTTP_ACCEPT') &&
6506 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6507 # browser (feed reader) prefers text/xml
6508 $content_type = 'text/xml';
6510 if (defined($commitlist[0])) {
6511 %latest_commit = %{$commitlist[0]};
6512 my $latest_epoch = $latest_commit{'committer_epoch'};
6513 %latest_date = parse_date($latest_epoch);
6514 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6515 if (defined $if_modified) {
6516 my $since;
6517 if (eval { require HTTP::Date; 1; }) {
6518 $since = HTTP::Date::str2time($if_modified);
6519 } elsif (eval { require Time::ParseDate; 1; }) {
6520 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6522 if (defined $since && $latest_epoch <= $since) {
6523 print $cgi->header(
6524 -type => $content_type,
6525 -charset => 'utf-8',
6526 -last_modified => $latest_date{'rfc2822'},
6527 -status => '304 Not Modified');
6528 return;
6531 print $cgi->header(
6532 -type => $content_type,
6533 -charset => 'utf-8',
6534 -last_modified => $latest_date{'rfc2822'});
6535 } else {
6536 print $cgi->header(
6537 -type => $content_type,
6538 -charset => 'utf-8');
6541 # Optimization: skip generating the body if client asks only
6542 # for Last-Modified date.
6543 return if ($cgi->request_method() eq 'HEAD');
6545 # header variables
6546 my $title = "$site_name - $project/$action";
6547 my $feed_type = 'log';
6548 if (defined $hash) {
6549 $title .= " - '$hash'";
6550 $feed_type = 'branch log';
6551 if (defined $file_name) {
6552 $title .= " :: $file_name";
6553 $feed_type = 'history';
6555 } elsif (defined $file_name) {
6556 $title .= " - $file_name";
6557 $feed_type = 'history';
6559 $title .= " $feed_type";
6560 my $descr = git_get_project_description($project);
6561 if (defined $descr) {
6562 $descr = esc_html($descr);
6563 } else {
6564 $descr = "$project " .
6565 ($format eq 'rss' ? 'RSS' : 'Atom') .
6566 " feed";
6568 my $owner = git_get_project_owner($project);
6569 $owner = esc_html($owner);
6571 #header
6572 my $alt_url;
6573 if (defined $file_name) {
6574 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6575 } elsif (defined $hash) {
6576 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6577 } else {
6578 $alt_url = href(-full=>1, action=>"summary");
6580 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6581 if ($format eq 'rss') {
6582 print <<XML;
6583 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6584 <channel>
6586 print "<title>$title</title>\n" .
6587 "<link>$alt_url</link>\n" .
6588 "<description>$descr</description>\n" .
6589 "<language>en</language>\n" .
6590 # project owner is responsible for 'editorial' content
6591 "<managingEditor>$owner</managingEditor>\n";
6592 if (defined $logo || defined $favicon) {
6593 # prefer the logo to the favicon, since RSS
6594 # doesn't allow both
6595 my $img = esc_url($logo || $favicon);
6596 print "<image>\n" .
6597 "<url>$img</url>\n" .
6598 "<title>$title</title>\n" .
6599 "<link>$alt_url</link>\n" .
6600 "</image>\n";
6602 if (%latest_date) {
6603 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6604 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6606 print "<generator>gitweb v.$version/$git_version</generator>\n";
6607 } elsif ($format eq 'atom') {
6608 print <<XML;
6609 <feed xmlns="http://www.w3.org/2005/Atom">
6611 print "<title>$title</title>\n" .
6612 "<subtitle>$descr</subtitle>\n" .
6613 '<link rel="alternate" type="text/html" href="' .
6614 $alt_url . '" />' . "\n" .
6615 '<link rel="self" type="' . $content_type . '" href="' .
6616 $cgi->self_url() . '" />' . "\n" .
6617 "<id>" . href(-full=>1) . "</id>\n" .
6618 # use project owner for feed author
6619 "<author><name>$owner</name></author>\n";
6620 if (defined $favicon) {
6621 print "<icon>" . esc_url($favicon) . "</icon>\n";
6623 if (defined $logo_url) {
6624 # not twice as wide as tall: 72 x 27 pixels
6625 print "<logo>" . esc_url($logo) . "</logo>\n";
6627 if (! %latest_date) {
6628 # dummy date to keep the feed valid until commits trickle in:
6629 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6630 } else {
6631 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6633 print "<generator version='$version/$git_version'>gitweb</generator>\n";
6636 # contents
6637 for (my $i = 0; $i <= $#commitlist; $i++) {
6638 my %co = %{$commitlist[$i]};
6639 my $commit = $co{'id'};
6640 # we read 150, we always show 30 and the ones more recent than 48 hours
6641 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6642 last;
6644 my %cd = parse_date($co{'author_epoch'});
6646 # get list of changed files
6647 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6648 $co{'parent'} || "--root",
6649 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6650 or next;
6651 my @difftree = map { chomp; $_ } <$fd>;
6652 close $fd
6653 or next;
6655 # print element (entry, item)
6656 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6657 if ($format eq 'rss') {
6658 print "<item>\n" .
6659 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6660 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6661 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6662 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6663 "<link>$co_url</link>\n" .
6664 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6665 "<content:encoded>" .
6666 "<![CDATA[\n";
6667 } elsif ($format eq 'atom') {
6668 print "<entry>\n" .
6669 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6670 "<updated>$cd{'iso-8601'}</updated>\n" .
6671 "<author>\n" .
6672 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6673 if ($co{'author_email'}) {
6674 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6676 print "</author>\n" .
6677 # use committer for contributor
6678 "<contributor>\n" .
6679 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6680 if ($co{'committer_email'}) {
6681 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6683 print "</contributor>\n" .
6684 "<published>$cd{'iso-8601'}</published>\n" .
6685 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6686 "<id>$co_url</id>\n" .
6687 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6688 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6690 my $comment = $co{'comment'};
6691 print "<pre>\n";
6692 foreach my $line (@$comment) {
6693 $line = esc_html($line);
6694 print "$line\n";
6696 print "</pre><ul>\n";
6697 foreach my $difftree_line (@difftree) {
6698 my %difftree = parse_difftree_raw_line($difftree_line);
6699 next if !$difftree{'from_id'};
6701 my $file = $difftree{'file'} || $difftree{'to_file'};
6703 print "<li>" .
6704 "[" .
6705 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6706 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6707 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6708 file_name=>$file, file_parent=>$difftree{'from_file'}),
6709 -title => "diff"}, 'D');
6710 if ($have_blame) {
6711 print $cgi->a({-href => href(-full=>1, action=>"blame",
6712 file_name=>$file, hash_base=>$commit),
6713 -title => "blame"}, 'B');
6715 # if this is not a feed of a file history
6716 if (!defined $file_name || $file_name ne $file) {
6717 print $cgi->a({-href => href(-full=>1, action=>"history",
6718 file_name=>$file, hash=>$commit),
6719 -title => "history"}, 'H');
6721 $file = esc_path($file);
6722 print "] ".
6723 "$file</li>\n";
6725 if ($format eq 'rss') {
6726 print "</ul>]]>\n" .
6727 "</content:encoded>\n" .
6728 "</item>\n";
6729 } elsif ($format eq 'atom') {
6730 print "</ul>\n</div>\n" .
6731 "</content>\n" .
6732 "</entry>\n";
6736 # end of feed
6737 if ($format eq 'rss') {
6738 print "</channel>\n</rss>\n";
6739 } elsif ($format eq 'atom') {
6740 print "</feed>\n";
6744 sub git_rss {
6745 git_feed('rss');
6748 sub git_atom {
6749 git_feed('atom');
6752 sub git_opml {
6753 my @list = git_get_projects_list();
6755 print $cgi->header(
6756 -type => 'text/xml',
6757 -charset => 'utf-8',
6758 -content_disposition => 'inline; filename="opml.xml"');
6760 print <<XML;
6761 <?xml version="1.0" encoding="utf-8"?>
6762 <opml version="1.0">
6763 <head>
6764 <title>$site_name OPML Export</title>
6765 </head>
6766 <body>
6767 <outline text="git RSS feeds">
6770 foreach my $pr (@list) {
6771 my %proj = %$pr;
6772 my $head = git_get_head_hash($proj{'path'});
6773 if (!defined $head) {
6774 next;
6776 $git_dir = "$projectroot/$proj{'path'}";
6777 my %co = parse_commit($head);
6778 if (!%co) {
6779 next;
6782 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6783 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
6784 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
6785 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6787 print <<XML;
6788 </outline>
6789 </body>
6790 </opml>