gitweb: use new Git::Repo API, and add optional caching
[git/gitweb-caching.git] / gitweb / gitweb.perl
blob410fe0db96c159ec9114b3931e63beab626664d3
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);
20 use Git::RepoRoot;
22 binmode STDOUT, ':utf8';
24 BEGIN {
25 CGI->compile() if $ENV{'MOD_PERL'};
28 our $cgi = new CGI;
29 our $version = "++GIT_VERSION++";
30 our $my_url = $cgi->url();
31 our $my_uri = $cgi->url(-absolute => 1);
33 # core git executable to use
34 # this can just be "git" if your webserver has a sensible PATH
35 our $GIT = "++GIT_BINDIR++/git";
37 # absolute fs-path which will be prepended to the project path
38 #our $projectroot = "/pub/scm";
39 our $projectroot = "++GITWEB_PROJECTROOT++";
41 # fs traversing limit for getting project list
42 # the number is relative to the projectroot
43 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
45 # target of the home link on top of all pages
46 our $home_link = $my_uri || "/";
48 # string of the home link on top of all pages
49 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
51 # name of your site or organization to appear in page titles
52 # replace this with something more descriptive for clearer bookmarks
53 our $site_name = "++GITWEB_SITENAME++"
54 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
56 # filename of html text to include at top of each page
57 our $site_header = "++GITWEB_SITE_HEADER++";
58 # html text to include at home page
59 our $home_text = "++GITWEB_HOMETEXT++";
60 # filename of html text to include at bottom of each page
61 our $site_footer = "++GITWEB_SITE_FOOTER++";
63 # URI of stylesheets
64 our @stylesheets = ("++GITWEB_CSS++");
65 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
66 our $stylesheet = undef;
67 # URI of GIT logo (72x27 size)
68 our $logo = "++GITWEB_LOGO++";
69 # URI of GIT favicon, assumed to be image/png type
70 our $favicon = "++GITWEB_FAVICON++";
72 # URI and label (title) of GIT logo link
73 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
74 #our $logo_label = "git documentation";
75 our $logo_url = "http://git.or.cz/";
76 our $logo_label = "git homepage";
78 # source of projects list
79 our $projects_list = "++GITWEB_LIST++";
81 # the width (in characters) of the projects list "Description" column
82 our $projects_list_description_width = 25;
84 # default order of projects list
85 # valid values are none, project, descr, owner, and age
86 our $default_projects_order = "project";
88 # show repository only if this file exists
89 # (only effective if this variable evaluates to true)
90 our $export_ok = "++GITWEB_EXPORT_OK++";
92 # only allow viewing of repositories also shown on the overview page
93 our $strict_export = "++GITWEB_STRICT_EXPORT++";
95 # list of git base URLs used for URL to where fetch project from,
96 # i.e. full URL is "$git_base_url/$project"
97 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
99 # default blob_plain mimetype and default charset for text/plain blob
100 our $default_blob_plain_mimetype = 'text/plain';
101 our $default_text_plain_charset = undef;
103 # file to use for guessing MIME types before trying /etc/mime.types
104 # (relative to the current git repository)
105 our $mimetypes_file = undef;
107 # assume this charset if line contains non-UTF-8 characters;
108 # it should be valid encoding (see Encoding::Supported(3pm) for list),
109 # for which encoding all byte sequences are valid, for example
110 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
111 # could be even 'utf-8' for the old behavior)
112 our $fallback_encoding = 'latin1';
114 # rename detection options for git-diff and git-diff-tree
115 # - default is '-M', with the cost proportional to
116 # (number of removed files) * (number of new files).
117 # - more costly is '-C' (which implies '-M'), with the cost proportional to
118 # (number of changed files + number of removed files) * (number of new files)
119 # - even more costly is '-C', '--find-copies-harder' with cost
120 # (number of files in the original tree) * (number of new files)
121 # - one might want to include '-B' option, e.g. '-B', '-M'
122 our @diff_opts = ('-M'); # taken from git_commit
124 # information about snapshot formats that gitweb is capable of serving
125 our %known_snapshot_formats = (
126 # name => {
127 # 'display' => display name,
128 # 'type' => mime type,
129 # 'suffix' => filename suffix,
130 # 'format' => --format for git-archive,
131 # 'compressor' => [compressor command and arguments]
132 # (array reference, optional)}
134 'tgz' => {
135 'display' => 'tar.gz',
136 'type' => 'application/x-gzip',
137 'suffix' => '.tar.gz',
138 'format' => 'tar',
139 'compressor' => ['gzip']},
141 'tbz2' => {
142 'display' => 'tar.bz2',
143 'type' => 'application/x-bzip2',
144 'suffix' => '.tar.bz2',
145 'format' => 'tar',
146 'compressor' => ['bzip2']},
148 'zip' => {
149 'display' => 'zip',
150 'type' => 'application/x-zip',
151 'suffix' => '.zip',
152 'format' => 'zip'},
155 # Aliases so we understand old gitweb.snapshot values in repository
156 # configuration.
157 our %known_snapshot_format_aliases = (
158 'gzip' => 'tgz',
159 'bzip2' => 'tbz2',
161 # backward compatibility: legacy gitweb config support
162 'x-gzip' => undef, 'gz' => undef,
163 'x-bzip2' => undef, 'bz2' => undef,
164 'x-zip' => undef, '' => undef,
167 # Cache::Cache object to cache data from the repository, or undef for
168 # no cache. You would typically use a Cache::Memcached instance here.
169 our $cache = undef;
171 # Expiration time in seconds for transient cache entries, or undef for
172 # no expiration. (Only used if $cache is defined.)
174 # Transient cache entries (like get_sha1('HEAD')) are automatically
175 # invalidated when an mtime of either the repository's root directory
176 # or of the refs directory or any subdirectory changes. This
177 # mechanism *should* detect changes to the repository reliably if you
178 # only use git or rsync to write to it, and hence this expiration time
179 # can be set very high. (This does not default to non-expiring
180 # [undef] just in case a change goes undetected for some reason.) You
181 # might want to set this to a lower time (e.g. a few minutes) if
182 # developers change files in the refs directories on your server by
183 # non-standard means (i.e. manually).
185 # You can usually go with the default here.
186 our $cache_transient_expiration_time = 60 * 60 * 24;
188 # Directory on disk to hold potentially large cache items (in
189 # particular, snapshots, diffs and blobs), or undef for no cache.
190 # Cache files will be created in this directory, but they will not be
191 # expired; you should periodically delete old files yourself. Setting
192 # $large_cache_root but not $cache is possible, but usually not
193 # sensible.
194 our $large_cache_root = undef;
196 # Extra cache key component to use. This should stringify to a string
197 # without null bytes; it is used as a means to discard all cache
198 # entries whenever this key changes. Since gitweb already uses its
199 # own version number as a cache key component, you don't normally need
200 # this unless you're doing development work on gitweb (in which case
201 # you might set it to gitweb's mtime, (stat 'gitweb.cgi')[9]).
202 our $cache_key = undef;
204 # Display information in the footer of each page (currently only cache
205 # statistics): 0 = none, 1 = short, 2 = long.
206 our $page_info = 0;
208 # You define site-wide feature defaults here; override them with
209 # $GITWEB_CONFIG as necessary.
210 our %feature = (
211 # feature => {
212 # 'sub' => feature-sub (subroutine),
213 # 'override' => allow-override (boolean),
214 # 'default' => [ default options...] (array reference)}
216 # if feature is overridable (it means that allow-override has true value),
217 # then feature-sub will be called with default options as parameters;
218 # return value of feature-sub indicates if to enable specified feature
220 # if there is no 'sub' key (no feature-sub), then feature cannot be
221 # overriden
223 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
225 # Enable the 'blame' blob view, showing the last commit that modified
226 # each line in the file. This can be very CPU-intensive.
228 # To enable system wide have in $GITWEB_CONFIG
229 # $feature{'blame'}{'default'} = [1];
230 # To have project specific config enable override in $GITWEB_CONFIG
231 # $feature{'blame'}{'override'} = 1;
232 # and in project config gitweb.blame = 0|1;
233 'blame' => {
234 'sub' => \&feature_blame,
235 'override' => 0,
236 'default' => [0]},
238 # Enable the 'snapshot' link, providing a compressed archive of any
239 # tree. This can potentially generate high traffic if you have large
240 # project.
242 # Value is a list of formats defined in %known_snapshot_formats that
243 # you wish to offer.
244 # To disable system wide have in $GITWEB_CONFIG
245 # $feature{'snapshot'}{'default'} = [];
246 # To have project specific config enable override in $GITWEB_CONFIG
247 # $feature{'snapshot'}{'override'} = 1;
248 # and in project config, a comma-separated list of formats or "none"
249 # to disable. Example: gitweb.snapshot = tbz2,zip;
250 'snapshot' => {
251 'sub' => \&feature_snapshot,
252 'override' => 0,
253 'default' => ['tgz']},
255 # Enable text search, which will list the commits which match author,
256 # committer or commit text to a given string. Enabled by default.
257 # Project specific override is not supported.
258 'search' => {
259 'override' => 0,
260 'default' => [1]},
262 # Enable grep search, which will list the files in currently selected
263 # tree containing the given string. Enabled by default. This can be
264 # potentially CPU-intensive, of course.
266 # To enable system wide have in $GITWEB_CONFIG
267 # $feature{'grep'}{'default'} = [1];
268 # To have project specific config enable override in $GITWEB_CONFIG
269 # $feature{'grep'}{'override'} = 1;
270 # and in project config gitweb.grep = 0|1;
271 'grep' => {
272 'override' => 0,
273 'default' => [1]},
275 # Enable the pickaxe search, which will list the commits that modified
276 # a given string in a file. This can be practical and quite faster
277 # alternative to 'blame', but still potentially CPU-intensive.
279 # To enable system wide have in $GITWEB_CONFIG
280 # $feature{'pickaxe'}{'default'} = [1];
281 # To have project specific config enable override in $GITWEB_CONFIG
282 # $feature{'pickaxe'}{'override'} = 1;
283 # and in project config gitweb.pickaxe = 0|1;
284 'pickaxe' => {
285 'sub' => \&feature_pickaxe,
286 'override' => 0,
287 'default' => [1]},
289 # Make gitweb use an alternative format of the URLs which can be
290 # more readable and natural-looking: project name is embedded
291 # directly in the path and the query string contains other
292 # auxiliary information. All gitweb installations recognize
293 # URL in either format; this configures in which formats gitweb
294 # generates links.
296 # To enable system wide have in $GITWEB_CONFIG
297 # $feature{'pathinfo'}{'default'} = [1];
298 # Project specific override is not supported.
300 # Note that you will need to change the default location of CSS,
301 # favicon, logo and possibly other files to an absolute URL. Also,
302 # if gitweb.cgi serves as your indexfile, you will need to force
303 # $my_uri to contain the script name in your $GITWEB_CONFIG.
304 'pathinfo' => {
305 'override' => 0,
306 'default' => [0]},
308 # Make gitweb consider projects in project root subdirectories
309 # to be forks of existing projects. Given project $projname.git,
310 # projects matching $projname/*.git will not be shown in the main
311 # projects list, instead a '+' mark will be added to $projname
312 # there and a 'forks' view will be enabled for the project, listing
313 # all the forks. If project list is taken from a file, forks have
314 # to be listed after the main project.
316 # To enable system wide have in $GITWEB_CONFIG
317 # $feature{'forks'}{'default'} = [1];
318 # Project specific override is not supported.
319 'forks' => {
320 'override' => 0,
321 'default' => [0]},
324 sub gitweb_check_feature {
325 my ($name) = @_;
326 return unless exists $feature{$name};
327 my ($sub, $override, @defaults) = (
328 $feature{$name}{'sub'},
329 $feature{$name}{'override'},
330 @{$feature{$name}{'default'}});
331 if (!$override) { return @defaults; }
332 if (!defined $sub) {
333 warn "feature $name is not overrideable";
334 return @defaults;
336 return $sub->(@defaults);
339 sub feature_blame {
340 my ($val) = git_get_project_config('blame', '--bool');
342 if ($val eq 'true') {
343 return 1;
344 } elsif ($val eq 'false') {
345 return 0;
348 return $_[0];
351 sub feature_snapshot {
352 my (@fmts) = @_;
354 my ($val) = git_get_project_config('snapshot');
356 if ($val) {
357 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
360 return @fmts;
363 sub feature_grep {
364 my ($val) = git_get_project_config('grep', '--bool');
366 if ($val eq 'true') {
367 return (1);
368 } elsif ($val eq 'false') {
369 return (0);
372 return ($_[0]);
375 sub feature_pickaxe {
376 my ($val) = git_get_project_config('pickaxe', '--bool');
378 if ($val eq 'true') {
379 return (1);
380 } elsif ($val eq 'false') {
381 return (0);
384 return ($_[0]);
387 # checking HEAD file with -e is fragile if the repository was
388 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
389 # and then pruned.
390 sub check_head_link {
391 my ($dir) = @_;
392 my $headfile = "$dir/HEAD";
393 return ((-e $headfile) ||
394 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
397 sub check_export_ok {
398 my ($dir) = @_;
399 return (check_head_link($dir) &&
400 (!$export_ok || -e "$dir/$export_ok"));
403 # process alternate names for backward compatibility
404 # filter out unsupported (unknown) snapshot formats
405 sub filter_snapshot_fmts {
406 my @fmts = @_;
408 @fmts = map {
409 exists $known_snapshot_format_aliases{$_} ?
410 $known_snapshot_format_aliases{$_} : $_} @fmts;
411 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
415 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
416 if (-e $GITWEB_CONFIG) {
417 do $GITWEB_CONFIG;
418 } else {
419 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
420 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
423 # version of the core git binary
424 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
427 # ======================================================================
428 # caching layer
430 package CachedRepo;
432 use Digest::MD5 qw(md5_hex);
433 use List::Util qw(max);
435 use base qw(Git::Repo);
437 # Global statistics, collected across repositories.
438 # Hits, misses, sets, and failed_sets are counters, and get_list is an
439 # arrayref of keys, where a key is an arrayref of key items.
440 our %cache_statistics = (
441 hits => 0, misses => 0, sets => 0, failed_sets => 0, get_list => []);
442 our %large_cache_statistics = (
443 hits => 0, misses => 0, sets => 0, failed_sets => 0, get_list => []);
445 # Options: like Git::Repo->new, and the following:
446 # cache: a Cache::Cache conforming cache instance
447 # transient_expiration_time: expiration time in seconds for transient
448 # cache entries (like get_hash('HEAD')), or undef; do not set to
449 # 30 days or more, since it makes Cache::Memcached hiccup
450 sub new {
451 my ($class, %opts) = @_;
452 my $cache = delete $opts{cache};
453 my $large_cache_root = delete $opts{large_cache_root};
454 my $transient_expiration_time = delete $opts{transient_expiration_time};
455 my $self = $class->SUPER::new(%opts);
456 $self->{cache} = $cache;
457 $self->{large_cache_root} = $large_cache_root;
458 $self->{transient_expiration_time} = $transient_expiration_time;
459 return $self;
462 sub cache { shift->{cache} }
463 sub large_cache_root { shift->{large_cache_root} }
464 sub transient_expiration_time { shift->{transient_expiration_time} }
466 # Fast function to generate a unique (short, hashed) key for the cache
467 # to use. None of the parameters *should* contain null bytes. Example:
468 # $repo->get_key('get_sha1', 'HEAD:file1') eq '05dd723732a45fcac864787ec1897cc1'
469 sub get_key {
470 my $self = shift;
471 # Some caches (like Cache::FileCache) hash keys themselves,
472 # but Cache::Memcached does not like long keys, so we need to
473 # hash them. MD5 is fine here, since (as of July 2008) there
474 # are only collision attacks, but no practical preimage
475 # attacks on MD5. Constructing two colliding keys doesn't
476 # seem to pose much of a threat for the cache. Digest::SHA1
477 # is only in core as of Perl 5.9, so we cannot use it here.
478 return md5_hex(join "\0", $self->_key_items(@_));
481 # Return a list of strings that can be used to generate a key.
482 sub _key_items {
483 my $self = shift;
484 return map { defined $_ ? " $_" : '' }
485 ('gitweb', $version, $cache_key, 'project', $self->repo_dir, @_);
488 # Convenience function: cache_set(\@key, $value, $expire);
489 # $expire is boolean and indicates whether an expiry time should be set.
490 sub cache_set {
491 my ($self, $key, $value, $expire) = @_;
492 return unless $self->cache;
493 my $expiration_token = $expire ? $self->get_last_modification() : undef;
494 my $ok = $self->cache->set(
495 $self->get_key($expiration_token, @$key), $value,
496 $expire ? $self->transient_expiration_time : ());
497 $ok ? $cache_statistics{sets}++ : $cache_statistics{failed_sets}++;
498 return $ok;
501 # Convenience function: cache_get(\@key, $expire)
502 # $expire must be the same that has been used for cache_set or the
503 # lookup will fail.
504 sub cache_get {
505 my ($self, $key, $expire) = @_;
506 return unless $self->cache;
507 my $expiration_token = $expire ? $self->get_last_modification() : undef;
508 my $val = $self->cache->get($self->get_key($expiration_token, @$key));
509 defined $val ? $cache_statistics{hits}++ : $cache_statistics{misses}++;
510 push @{$cache_statistics{get_list}},
511 [$self->repo_dir, $expire ? 1 : 0, @$key];
512 return $val;
515 # Return the output of the given git command as a string.
516 # Valid options are:
517 # cmd: An arrayref of arguments to pass to git (mandatory).
518 # max_exit_code: Die if the exit code of the git binary is greater
519 # than this (default: 0).
520 # cache: If 1, the output is cached but expires when the repo is
521 # modified; if 2, it is cached indefinitely.
522 sub cmd_output {
523 my ($self, %opts) = @_;
524 my $key = ['cmd', $opts{max_exit_code}, @{$opts{cmd}}];
525 my $output;
526 unless ($opts{cache} && defined($output = $self->cache_get(
527 $key, $opts{cache} == 1))) {
528 my @cmd = ($self->_git_cmd, @{$opts{cmd}});
529 my $cmd = join ' ', @cmd;
530 open my $fh, '-|', @cmd or die "cannot open pipe: $cmd";
531 local $/;
532 $output = <$fh>;
533 if (!close $fh) {
534 die "error closing pipe ($!): $cmd" if $!;
535 my $exit_code = $? >> 8;
536 die "Command failed with exit code $exit_code: $cmd"
537 if $exit_code > ($opts{max_exit_code} || 0);
539 $self->cache_set($key, $output, $opts{cache} == 1) if $opts{cache};
541 return $output;
544 # progressive_cmd_output(%opts)
546 # Return a coderef that returns a chunk of the output of the given
547 # command each time it is called, or undef when the output is
548 # exhausted. For the output to be cached, it must be read until the
549 # coderef returns undef, otherwise it will leave a stale temporary
550 # file in the cache.
552 # Options:
553 # cmd: an arrayref or string of arguments to git; if it's a string, it will be
554 # passed to the shell
555 # max_exit_code: die if the command exits with a higher exit code (default: 0)
556 # separator: like $/; if undef, read the output in chunks of arbitrary size
557 # cache: if true, cache the output of the command (without expiration)
559 # Example:
560 # my $diff_read = $repo->progressive_cmd_output(
561 # cmd => ['diff', $from_sha1, $to_sha1], separator => "\n", cache => 1]);
562 # while (my $line = $diff_read->()) {
563 # chomp $line;
564 # ...
566 my $_file_seq = 0;
567 sub progressive_cmd_output {
568 die 'must pass an odd number of arguments' unless @_ % 2;
569 my ($self, %opts) = @_;
570 local $/ = defined $opts{separator} ? $opts{separator} : \32768;
571 my (@cmd, $cmd_str);
572 if (ref($opts{cmd}) eq 'ARRAY') {
573 @cmd = ($self->_git_cmd, @{$opts{cmd}});
574 $cmd_str = join " ", @cmd; # only used for diagnostics
575 } else {
576 $cmd_str = main::quote_command($self->_git_cmd) .
577 " $opts{cmd}"; # this will be passed to the shell
579 # We read from $fh, whether it's a pipe or a cache file. If
580 # it's a pipe, we also progressively cache it to
581 # $tmp_cache_file, and at the end move $tmp_cache_file_name to
582 # $cache_file_name. This avoids having partially written
583 # cache entries.
584 my ($fh, $cache_file_name, $tmp_cache_file_name, $tmp_cache_file);
585 if ($opts{cache} && $self->large_cache_root) {
586 my @key_items = ('cmd', $opts{max_exit_code},
587 ref($opts{cmd}) eq 'ARRAY' ?
588 (@{$opts{cmd}}) : (undef, $opts{cmd}));
589 push @{$large_cache_statistics{get_list}},
590 [$self->repo_dir, @key_items];
591 $cache_file_name = File::Spec->catfile(
592 $self->large_cache_root, $self->get_key(@key_items));
593 $tmp_cache_file_name = File::Spec->catfile(
594 $self->large_cache_root,
595 join('.', $$, $_file_seq++, 'tmp'));
597 unless ($cache_file_name && open $fh, '<', $cache_file_name) {
598 # Not in cache -- open pipe, and open cache file to write to.
599 if (@cmd) {
600 open $fh, '-|', @cmd;
601 } else {
602 open $fh, '-|', $cmd_str;
604 die "cannot open pipe: $cmd_str" unless $fh;
605 if ($tmp_cache_file_name) {
606 open $tmp_cache_file, '>', $tmp_cache_file_name
607 or $tmp_cache_file = undef;
609 # Increment failed_sets; it will be decremented upon
610 # successful finalization of the cache entry.
611 $large_cache_statistics{failed_sets}++;
612 # Record uncached calls as misses.
613 $large_cache_statistics{misses}++;
614 } else {
615 $large_cache_statistics{hits}++;
617 my $read = sub {
618 return undef unless $fh; # already closed
619 my $output = <$fh>;
620 if ($output) {
621 # Write to cache and return.
622 if ($tmp_cache_file && ! print $tmp_cache_file $output) {
623 # Writing to cache failed; clean up
624 # and stop caching this pipe.
625 close $tmp_cache_file;
626 $tmp_cache_file = undef;
627 unlink $tmp_cache_file_name;
629 return $output;
631 # End of output; close and finalize cache.
632 if (close $fh) {
633 # We sometimes get mysterious "Bad file
634 # descriptor" errors here, but reading from
635 # the pipe worked fine, so let's not die.
636 #die "error closing pipe ($!): $cmd_str" if $!;
637 my $exit_code = $? >> 8;
638 die "Command died with exit code $exit_code: $cmd_str"
639 if $exit_code > ($opts{max_exit_code} || 0);
641 $fh = undef;
642 if ($tmp_cache_file && close $tmp_cache_file) {
643 # Cache file written OK, move it in place.
644 if (rename $tmp_cache_file_name, $cache_file_name) {
645 $large_cache_statistics{failed_sets}--;
646 $large_cache_statistics{sets}++;
649 return undef;
651 # We can also provide a &close function here in case it
652 # becomes necessary to close pipes prematurely.
653 return $read;
656 # The following methods override the base class (Git::Repo) methods to
657 # add caching.
659 sub get_sha1 {
660 my ($self, $object_id) = @_;
661 my $expire = ($object_id !~ /^[0-9a-f]{40}(?![0-9a-f])/);
662 my $triple = $self->cache_get(['SHA1', $object_id], $expire);
663 unless (defined $triple) {
664 $triple = [$self->SUPER::get_sha1($object_id)];
665 # Do not cache failed lookups -- missing SHA1s would
666 # be permanently cached, but a subsequent push to the
667 # repository might add those missing SHA1s to the
668 # repository.
669 return unless $triple->[0];
670 $self->cache_set(['SHA1', $object_id], $triple, $expire);
672 return wantarray ? @$triple : $triple->[0];
675 sub get_object {
676 my ($self, $sha1) = @_;
677 my $type_content = $self->cache_get(['cat-file', $sha1], 0);
678 unless (defined $type_content) {
679 $type_content = [$self->SUPER::get_object($sha1)];
680 die 'unexpected empty return value' unless @$type_content;
681 $self->cache_set(['cat-file', $sha1], $type_content, 0);
683 return wantarray ? @$type_content : $type_content->[1];
686 # get_commit and get_tag only return empty Commit and Tag objects,
687 # which when loaded (lazily), happen to call get_object and thus are
688 # cached as well.
690 sub name_rev {
691 my ($self, $sha1, $tags_only) = @_;
692 my $name = $self->cache_get(['name-rev', $sha1, $tags_only], 1);
693 unless (defined $name) {
694 # || '' is to cache failed lookups (name_rev doesn't
695 # ever return empty names).
696 $name = $self->SUPER::name_rev($sha1, $tags_only) || '';
697 $self->cache_set(['name-rev', $sha1, $tags_only], $name, 1);
699 return $name || undef;
702 # Return the seconds since epoch when the repository was last touched.
703 sub get_last_modification {
704 my $self = shift;
705 return $self->{last_modification} if $self->{last_modification};
706 # Hashref mapping absolute paths of directories to mtimes. We
707 # rely on the fact here that every time git updates a file, it
708 # creates it under a different name and then moves it in
709 # place, thus causing the mtime of the containing directory to
710 # be updated. Hence it's enough to just stat the directories.
711 my $mtimes = $self->cache_get(['mtimes'], 0);
712 if ($mtimes) {
713 CHECK_CACHE: {
714 # Check if the cache response is up to date.
715 while (my ($dir, $mtime) = each %$mtimes) {
716 last CHECK_CACHE if (stat $dir)[9] != $mtime;
718 $self->{last_modification} = max(values %$mtimes);
719 return max(values %$mtimes);
722 # Either mtimes are not in cache, or at least one directory
723 # has been updated. Traverse the whole ref tree and record
724 # all directory mtimes -- this is a bit slower than the
725 # up-to-date-ness check above since we end up stat'ing all
726 # files in the refs directory.
727 my $time = time;
728 $mtimes = { $self->repo_dir => (stat $self->repo_dir)[9] };
729 my $cacheable = 1;
730 # Traverse refs/ directory and record the mtimes of all
731 # directories. Implementation note: This stats *all* files in
732 # the tree; this can become quite inefficient for repositories
733 # with many refs (say, >100) that are pushed to frequently.
734 # There are two possible solutions:
735 # 1. If we dropped the requirement of following symlinks, it
736 # would in theory be possible to exploit the nlinks==2
737 # feature of many Unix file systems. However, File::Find
738 # doesn't allow us to only traverse directories.
739 # 2. We could traverse only those directories whose mtimes have
740 # changed since the last time we checked (which we got from
741 # the cache); this makes the traversal code slightly more
742 # complicated, but should solve the worst efficiency issues.
743 File::Find::find( {
744 wanted => sub {
745 my $time = time; # get time first
746 # No way to avoid stat'ing unconditionally
747 # with File::Find.
748 my @stat = stat($_);
749 if (Fcntl::S_ISDIR($stat[2])) {
750 # Record the directory's mtime.
751 $mtimes->{$File::Find::name} = $stat[9];
752 # Mtimes have a 1-second granularity,
753 # so if the directory has *just* been
754 # modified, we might miss subsequent
755 # modifictions in the same second if
756 # we cached it.
757 $cacheable = 0 if $stat[9] >= $time;
760 # The refs/ tree can contain symlinks -- e.g. as of
761 # Aug 08 repo.or.cz uses a 'forkee' symlink, which
762 # links to another repository's refs/ directory.
763 follow => 1,
764 follow_skip => 2, # ignore duplicates
765 }, File::Spec->catfile($self->repo_dir, 'refs'));
766 $self->cache_set(['mtimes'], $mtimes, 0) if $cacheable;
767 $self->{last_modification} = max(values %$mtimes);
768 return max(values %$mtimes);
771 package main;
774 our $repo_root = Git::RepoRoot->new(root_dir => $projectroot,
775 git_binary => $GIT,
776 cache => $cache,
777 large_cache_root => $large_cache_root,
778 transient_expiration_time =>
779 $cache_transient_expiration_time,
780 repo_class => 'CachedRepo'
783 $projects_list ||= $projectroot;
785 # ======================================================================
786 # input validation and dispatch
787 our $action = $cgi->param('a');
788 if (defined $action) {
789 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
790 die_error(400, "Invalid action parameter");
794 # parameters which are pathnames
795 our $project = $cgi->param('p');
796 our $repo = $repo_root->repo(repo_dir => $project) if $project;
797 if (defined $project) {
798 if (!validate_pathname($project) ||
799 !(-d "$projectroot/$project") ||
800 !check_head_link("$projectroot/$project") ||
801 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
802 ($strict_export && !project_in_list($project))) {
803 undef $project;
804 die_error(404, "No such project");
808 our $file_name = $cgi->param('f');
809 if (defined $file_name) {
810 if (!validate_pathname($file_name)) {
811 die_error(400, "Invalid file parameter");
815 our $file_parent = $cgi->param('fp');
816 if (defined $file_parent) {
817 if (!validate_pathname($file_parent)) {
818 die_error(400, "Invalid file parent parameter");
822 # parameters which are refnames
823 our $hash = $cgi->param('h');
824 if (defined $hash) {
825 if (!validate_refname($hash)) {
826 die_error(400, "Invalid hash parameter");
830 our $hash_parent = $cgi->param('hp');
831 if (defined $hash_parent) {
832 if (!validate_refname($hash_parent)) {
833 die_error(400, "Invalid hash parent parameter");
837 our $hash_base = $cgi->param('hb');
838 if (defined $hash_base) {
839 if (!validate_refname($hash_base)) {
840 die_error(400, "Invalid hash base parameter");
844 my %allowed_options = (
845 "--no-merges" => [ qw(rss atom log shortlog history) ],
848 our @extra_options = $cgi->param('opt');
849 if (defined @extra_options) {
850 foreach my $opt (@extra_options) {
851 if (not exists $allowed_options{$opt}) {
852 die_error(400, "Invalid option parameter");
854 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
855 die_error(400, "Invalid option parameter for this action");
860 our $hash_parent_base = $cgi->param('hpb');
861 if (defined $hash_parent_base) {
862 if (!validate_refname($hash_parent_base)) {
863 die_error(400, "Invalid hash parent base parameter");
867 # other parameters
868 our $page = $cgi->param('pg');
869 if (defined $page) {
870 if ($page =~ m/[^0-9]/) {
871 die_error(400, "Invalid page parameter");
875 our $searchtype = $cgi->param('st');
876 if (defined $searchtype) {
877 if ($searchtype =~ m/[^a-z]/) {
878 die_error(400, "Invalid searchtype parameter");
882 our $search_use_regexp = $cgi->param('sr');
884 our $searchtext = $cgi->param('s');
885 our $search_regexp;
886 if (defined $searchtext) {
887 if (length($searchtext) < 2) {
888 die_error(403, "At least two characters are required for search parameter");
890 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
893 # now read PATH_INFO and use it as alternative to parameters
894 sub evaluate_path_info {
895 return if defined $project;
896 my $path_info = $ENV{"PATH_INFO"};
897 return if !$path_info;
898 $path_info =~ s,^/+,,;
899 return if !$path_info;
900 # find which part of PATH_INFO is project
901 $project = $path_info;
902 $project =~ s,/+$,,;
903 while ($project && !check_head_link("$projectroot/$project")) {
904 $project =~ s,/*[^/]*$,,;
906 # validate project
907 $project = validate_pathname($project);
908 if (!$project ||
909 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
910 ($strict_export && !project_in_list($project))) {
911 undef $project;
912 return;
914 $repo = $repo_root->repo(repo_dir => $project);
915 # do not change any parameters if an action is given using the query string
916 return if $action;
917 $path_info =~ s,^\Q$project\E/*,,;
918 my ($refname, $pathname) = split(/:/, $path_info, 2);
919 if (defined $pathname) {
920 # we got "project.git/branch:filename" or "project.git/branch:dir/"
921 # we could use git_get_type(branch:pathname) here
922 $pathname =~ s,^/+,,;
923 if (!$pathname || substr($pathname, -1) eq "/") {
924 $action ||= "tree";
925 $pathname =~ s,/$,,;
926 } else {
927 $action ||= "blob_plain";
929 $hash_base ||= validate_refname($refname);
930 $file_name ||= validate_pathname($pathname);
931 } elsif (defined $refname) {
932 # we got "project.git/branch"
933 $action ||= "shortlog";
934 $hash ||= validate_refname($refname);
937 evaluate_path_info();
939 # dispatch
940 my %actions = (
941 "blame" => \&git_blame,
942 "blobdiff" => \&git_blobdiff,
943 "blobdiff_plain" => \&git_blobdiff_plain,
944 "blob" => \&git_blob,
945 "blob_plain" => \&git_blob_plain,
946 "commitdiff" => \&git_commitdiff,
947 "commitdiff_plain" => \&git_commitdiff_plain,
948 "commit" => \&git_commit,
949 "forks" => \&git_forks,
950 "heads" => \&git_heads,
951 "history" => \&git_history,
952 "log" => \&git_log,
953 "rss" => \&git_rss,
954 "atom" => \&git_atom,
955 "search" => \&git_search,
956 "search_help" => \&git_search_help,
957 "shortlog" => \&git_shortlog,
958 "summary" => \&git_summary,
959 "tag" => \&git_tag,
960 "tags" => \&git_tags,
961 "tree" => \&git_tree,
962 "snapshot" => \&git_snapshot,
963 "object" => \&git_object,
964 # those below don't need $project
965 "opml" => \&git_opml,
966 "project_list" => \&git_project_list,
967 "project_index" => \&git_project_index,
970 if (!defined $action) {
971 if (defined $hash) {
972 $action = git_get_type($hash);
973 } elsif (defined $hash_base && defined $file_name) {
974 $action = git_get_type("$hash_base:$file_name");
975 } elsif (defined $project) {
976 $action = 'summary';
977 } else {
978 $action = 'project_list';
981 if (!defined($actions{$action})) {
982 die_error(400, "Unknown action");
984 if ($action !~ m/^(opml|project_list|project_index)$/ &&
985 !$project) {
986 die_error(400, "Project needed");
988 $actions{$action}->();
989 exit;
991 ## ======================================================================
992 ## action links
994 sub href (%) {
995 my %params = @_;
996 # default is to use -absolute url() i.e. $my_uri
997 my $href = $params{-full} ? $my_url : $my_uri;
999 # If you touch this, check the search form for updating, too.
1001 my @mapping = (
1002 project => "p",
1003 action => "a",
1004 file_name => "f",
1005 file_parent => "fp",
1006 hash => "h",
1007 hash_parent => "hp",
1008 hash_base => "hb",
1009 hash_parent_base => "hpb",
1010 page => "pg",
1011 order => "o",
1012 searchtext => "s",
1013 searchtype => "st",
1014 snapshot_format => "sf",
1015 extra_options => "opt",
1016 search_use_regexp => "sr",
1018 my %mapping = @mapping;
1020 $params{'project'} = $project unless exists $params{'project'};
1022 if ($params{-replay}) {
1023 while (my ($name, $symbol) = each %mapping) {
1024 if (!exists $params{$name}) {
1025 # to allow for multivalued params we use arrayref form
1026 $params{$name} = [ $cgi->param($symbol) ];
1031 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
1032 if ($use_pathinfo) {
1033 # use PATH_INFO for project name
1034 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
1035 delete $params{'project'};
1037 # Summary just uses the project path URL
1038 if (defined $params{'action'} && $params{'action'} eq 'summary') {
1039 delete $params{'action'};
1043 # now encode the parameters explicitly
1044 my @result = ();
1045 for (my $i = 0; $i < @mapping; $i += 2) {
1046 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
1047 if (defined $params{$name}) {
1048 if (ref($params{$name}) eq "ARRAY") {
1049 foreach my $par (@{$params{$name}}) {
1050 push @result, $symbol . "=" . esc_param($par);
1052 } else {
1053 push @result, $symbol . "=" . esc_param($params{$name});
1057 $href .= "?" . join(';', @result) if scalar @result;
1059 return $href;
1063 ## ======================================================================
1064 ## validation, quoting/unquoting and escaping
1066 sub validate_pathname {
1067 my $input = shift || return undef;
1069 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1070 # at the beginning, at the end, and between slashes.
1071 # also this catches doubled slashes
1072 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1073 return undef;
1075 # no null characters
1076 if ($input =~ m!\0!) {
1077 return undef;
1079 return $input;
1082 sub validate_refname {
1083 my $input = shift || return undef;
1085 # textual hashes are O.K.
1086 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1087 return $input;
1089 # it must be correct pathname
1090 $input = validate_pathname($input)
1091 or return undef;
1092 # restrictions on ref name according to git-check-ref-format
1093 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1094 return undef;
1096 return $input;
1099 # decode sequences of octets in utf8 into Perl's internal form,
1100 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1101 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1102 sub to_utf8 {
1103 my $str = shift;
1104 if (utf8::valid($str)) {
1105 utf8::decode($str);
1106 return $str;
1107 } else {
1108 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1112 # quote unsafe chars, but keep the slash, even when it's not
1113 # correct, but quoted slashes look too horrible in bookmarks
1114 sub esc_param {
1115 my $str = shift;
1116 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
1117 $str =~ s/\+/%2B/g;
1118 $str =~ s/ /\+/g;
1119 return $str;
1122 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
1123 sub esc_url {
1124 my $str = shift;
1125 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
1126 $str =~ s/\+/%2B/g;
1127 $str =~ s/ /\+/g;
1128 return $str;
1131 # replace invalid utf8 character with SUBSTITUTION sequence
1132 sub esc_html ($;%) {
1133 my $str = shift;
1134 my %opts = @_;
1136 $str = to_utf8($str);
1137 $str = $cgi->escapeHTML($str);
1138 if ($opts{'-nbsp'}) {
1139 $str =~ s/ /&nbsp;/g;
1141 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1142 return $str;
1145 # quote control characters and escape filename to HTML
1146 sub esc_path {
1147 my $str = shift;
1148 my %opts = @_;
1150 $str = to_utf8($str);
1151 $str = $cgi->escapeHTML($str);
1152 if ($opts{'-nbsp'}) {
1153 $str =~ s/ /&nbsp;/g;
1155 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1156 return $str;
1159 # Make control characters "printable", using character escape codes (CEC)
1160 sub quot_cec {
1161 my $cntrl = shift;
1162 my %opts = @_;
1163 my %es = ( # character escape codes, aka escape sequences
1164 "\t" => '\t', # tab (HT)
1165 "\n" => '\n', # line feed (LF)
1166 "\r" => '\r', # carrige return (CR)
1167 "\f" => '\f', # form feed (FF)
1168 "\b" => '\b', # backspace (BS)
1169 "\a" => '\a', # alarm (bell) (BEL)
1170 "\e" => '\e', # escape (ESC)
1171 "\013" => '\v', # vertical tab (VT)
1172 "\000" => '\0', # nul character (NUL)
1174 my $chr = ( (exists $es{$cntrl})
1175 ? $es{$cntrl}
1176 : sprintf('\%03o', ord($cntrl)) );
1177 if ($opts{-nohtml}) {
1178 return $chr;
1179 } else {
1180 return "<span class=\"cntrl\">$chr</span>";
1184 # Alternatively use unicode control pictures codepoints,
1185 # Unicode "printable representation" (PR)
1186 sub quot_upr {
1187 my $cntrl = shift;
1188 my %opts = @_;
1190 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1191 if ($opts{-nohtml}) {
1192 return $chr;
1193 } else {
1194 return "<span class=\"cntrl\">$chr</span>";
1198 # git may return quoted and escaped filenames
1199 sub unquote {
1200 my $str = shift;
1202 sub unq {
1203 my $seq = shift;
1204 my %es = ( # character escape codes, aka escape sequences
1205 't' => "\t", # tab (HT, TAB)
1206 'n' => "\n", # newline (NL)
1207 'r' => "\r", # return (CR)
1208 'f' => "\f", # form feed (FF)
1209 'b' => "\b", # backspace (BS)
1210 'a' => "\a", # alarm (bell) (BEL)
1211 'e' => "\e", # escape (ESC)
1212 'v' => "\013", # vertical tab (VT)
1215 if ($seq =~ m/^[0-7]{1,3}$/) {
1216 # octal char sequence
1217 return chr(oct($seq));
1218 } elsif (exists $es{$seq}) {
1219 # C escape sequence, aka character escape code
1220 return $es{$seq};
1222 # quoted ordinary character
1223 return $seq;
1226 if ($str =~ m/^"(.*)"$/) {
1227 # needs unquoting
1228 $str = $1;
1229 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1231 return $str;
1234 # escape tabs (convert tabs to spaces)
1235 sub untabify {
1236 my $line = shift;
1238 while ((my $pos = index($line, "\t")) != -1) {
1239 if (my $count = (8 - ($pos % 8))) {
1240 my $spaces = ' ' x $count;
1241 $line =~ s/\t/$spaces/;
1245 return $line;
1248 sub project_in_list {
1249 my $project = shift;
1250 my @list = git_get_projects_list();
1251 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1254 ## ----------------------------------------------------------------------
1255 ## HTML aware string manipulation
1257 # Try to chop given string on a word boundary between position
1258 # $len and $len+$add_len. If there is no word boundary there,
1259 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1260 # (marking chopped part) would be longer than given string.
1261 sub chop_str {
1262 my $str = shift;
1263 my $len = shift;
1264 my $add_len = shift || 10;
1265 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1267 # Make sure perl knows it is utf8 encoded so we don't
1268 # cut in the middle of a utf8 multibyte char.
1269 $str = to_utf8($str);
1271 # allow only $len chars, but don't cut a word if it would fit in $add_len
1272 # if it doesn't fit, cut it if it's still longer than the dots we would add
1273 # remove chopped character entities entirely
1275 # when chopping in the middle, distribute $len into left and right part
1276 # return early if chopping wouldn't make string shorter
1277 if ($where eq 'center') {
1278 return $str if ($len + 5 >= length($str)); # filler is length 5
1279 $len = int($len/2);
1280 } else {
1281 return $str if ($len + 4 >= length($str)); # filler is length 4
1284 # regexps: ending and beginning with word part up to $add_len
1285 my $endre = qr/.{$len}\w{0,$add_len}/;
1286 my $begre = qr/\w{0,$add_len}.{$len}/;
1288 if ($where eq 'left') {
1289 $str =~ m/^(.*?)($begre)$/;
1290 my ($lead, $body) = ($1, $2);
1291 if (length($lead) > 4) {
1292 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
1293 $lead = " ...";
1295 return "$lead$body";
1297 } elsif ($where eq 'center') {
1298 $str =~ m/^($endre)(.*)$/;
1299 my ($left, $str) = ($1, $2);
1300 $str =~ m/^(.*?)($begre)$/;
1301 my ($mid, $right) = ($1, $2);
1302 if (length($mid) > 5) {
1303 $left =~ s/&[^;]*$//;
1304 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
1305 $mid = " ... ";
1307 return "$left$mid$right";
1309 } else {
1310 $str =~ m/^($endre)(.*)$/;
1311 my $body = $1;
1312 my $tail = $2;
1313 if (length($tail) > 4) {
1314 $body =~ s/&[^;]*$//;
1315 $tail = "... ";
1317 return "$body$tail";
1321 # takes the same arguments as chop_str, but also wraps a <span> around the
1322 # result with a title attribute if it does get chopped. Additionally, the
1323 # string is HTML-escaped.
1324 sub chop_and_escape_str {
1325 my ($str) = @_;
1327 my $chopped = chop_str(@_);
1328 if ($chopped eq $str) {
1329 return esc_html($chopped);
1330 } else {
1331 $str =~ s/([[:cntrl:]])/?/g;
1332 return $cgi->span({-title=>$str}, esc_html($chopped));
1336 ## ----------------------------------------------------------------------
1337 ## functions returning short strings
1339 # CSS class for given age value (in seconds)
1340 sub age_class {
1341 my $age = shift;
1343 if (!defined $age) {
1344 return "noage";
1345 } elsif ($age < 60*60*2) {
1346 return "age0";
1347 } elsif ($age < 60*60*24*2) {
1348 return "age1";
1349 } else {
1350 return "age2";
1354 # convert age in seconds to "nn units ago" string
1355 sub age_string {
1356 my $age = shift;
1357 my $age_str;
1359 if ($age > 60*60*24*365*2) {
1360 $age_str = (int $age/60/60/24/365);
1361 $age_str .= " years ago";
1362 } elsif ($age > 60*60*24*(365/12)*2) {
1363 $age_str = int $age/60/60/24/(365/12);
1364 $age_str .= " months ago";
1365 } elsif ($age > 60*60*24*7*2) {
1366 $age_str = int $age/60/60/24/7;
1367 $age_str .= " weeks ago";
1368 } elsif ($age > 60*60*24*2) {
1369 $age_str = int $age/60/60/24;
1370 $age_str .= " days ago";
1371 } elsif ($age > 60*60*2) {
1372 $age_str = int $age/60/60;
1373 $age_str .= " hours ago";
1374 } elsif ($age > 60*2) {
1375 $age_str = int $age/60;
1376 $age_str .= " min ago";
1377 } elsif ($age > 2) {
1378 $age_str = int $age;
1379 $age_str .= " sec ago";
1380 } else {
1381 $age_str .= " right now";
1383 return $age_str;
1386 use constant {
1387 S_IFINVALID => 0030000,
1388 S_IFGITLINK => 0160000,
1391 # submodule/subproject, a commit object reference
1392 sub S_ISGITLINK($) {
1393 my $mode = shift;
1395 return (($mode & S_IFMT) == S_IFGITLINK)
1398 # convert file mode in octal to symbolic file mode string
1399 sub mode_str {
1400 my $mode = oct shift;
1402 if (S_ISGITLINK($mode)) {
1403 return 'm---------';
1404 } elsif (S_ISDIR($mode & S_IFMT)) {
1405 return 'drwxr-xr-x';
1406 } elsif (S_ISLNK($mode)) {
1407 return 'lrwxrwxrwx';
1408 } elsif (S_ISREG($mode)) {
1409 # git cares only about the executable bit
1410 if ($mode & S_IXUSR) {
1411 return '-rwxr-xr-x';
1412 } else {
1413 return '-rw-r--r--';
1415 } else {
1416 return '----------';
1420 # convert file mode in octal to file type string
1421 sub file_type {
1422 my $mode = shift;
1424 if ($mode !~ m/^[0-7]+$/) {
1425 return $mode;
1426 } else {
1427 $mode = oct $mode;
1430 if (S_ISGITLINK($mode)) {
1431 return "submodule";
1432 } elsif (S_ISDIR($mode & S_IFMT)) {
1433 return "directory";
1434 } elsif (S_ISLNK($mode)) {
1435 return "symlink";
1436 } elsif (S_ISREG($mode)) {
1437 return "file";
1438 } else {
1439 return "unknown";
1443 # convert file mode in octal to file type description string
1444 sub file_type_long {
1445 my $mode = shift;
1447 if ($mode !~ m/^[0-7]+$/) {
1448 return $mode;
1449 } else {
1450 $mode = oct $mode;
1453 if (S_ISGITLINK($mode)) {
1454 return "submodule";
1455 } elsif (S_ISDIR($mode & S_IFMT)) {
1456 return "directory";
1457 } elsif (S_ISLNK($mode)) {
1458 return "symlink";
1459 } elsif (S_ISREG($mode)) {
1460 if ($mode & S_IXUSR) {
1461 return "executable";
1462 } else {
1463 return "file";
1465 } else {
1466 return "unknown";
1471 ## ----------------------------------------------------------------------
1472 ## functions returning short HTML fragments, or transforming HTML fragments
1473 ## which don't belong to other sections
1475 # format line of commit message.
1476 sub format_log_line_html {
1477 my $line = shift;
1479 $line = esc_html($line, -nbsp=>1);
1480 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1481 my $hash_text = $1;
1482 my $link =
1483 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1484 -class => "text"}, $hash_text);
1485 $line =~ s/$hash_text/$link/;
1487 return $line;
1490 # format marker of refs pointing to given object
1491 sub format_ref_marker {
1492 my ($refs, $id) = @_;
1493 my $markers = '';
1495 if (defined $refs->{$id}) {
1496 foreach my $ref (@{$refs->{$id}}) {
1497 my ($type, $name) = qw();
1498 # e.g. tags/v2.6.11 or heads/next
1499 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1500 $type = $1;
1501 $name = $2;
1502 } else {
1503 $type = "ref";
1504 $name = $ref;
1507 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1508 esc_html($name) . "</span>";
1512 if ($markers) {
1513 return ' <span class="refs">'. $markers . '</span>';
1514 } else {
1515 return "";
1519 # format, perhaps shortened and with markers, title line
1520 sub format_subject_html {
1521 my ($long, $short, $href, $extra) = @_;
1522 $extra = '' unless defined($extra);
1524 if (length($short) < length($long)) {
1525 return $cgi->a({-href => $href, -class => "list subject",
1526 -title => to_utf8($long)},
1527 esc_html($short) . $extra);
1528 } else {
1529 return $cgi->a({-href => $href, -class => "list subject"},
1530 esc_html($long) . $extra);
1534 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1535 sub format_git_diff_header_line {
1536 my $line = shift;
1537 my $diffinfo = shift;
1538 my ($from, $to) = @_;
1540 if ($diffinfo->{'nparents'}) {
1541 # combined diff
1542 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1543 if ($to->{'href'}) {
1544 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1545 esc_path($to->{'file'}));
1546 } else { # file was deleted (no href)
1547 $line .= esc_path($to->{'file'});
1549 } else {
1550 # "ordinary" diff
1551 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1552 if ($from->{'href'}) {
1553 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1554 'a/' . esc_path($from->{'file'}));
1555 } else { # file was added (no href)
1556 $line .= 'a/' . esc_path($from->{'file'});
1558 $line .= ' ';
1559 if ($to->{'href'}) {
1560 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1561 'b/' . esc_path($to->{'file'}));
1562 } else { # file was deleted
1563 $line .= 'b/' . esc_path($to->{'file'});
1567 return "<div class=\"diff header\">$line</div>\n";
1570 # format extended diff header line, before patch itself
1571 sub format_extended_diff_header_line {
1572 my $line = shift;
1573 my $diffinfo = shift;
1574 my ($from, $to) = @_;
1576 # match <path>
1577 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1578 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1579 esc_path($from->{'file'}));
1581 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1582 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1583 esc_path($to->{'file'}));
1585 # match single <mode>
1586 if ($line =~ m/\s(\d{6})$/) {
1587 $line .= '<span class="info"> (' .
1588 file_type_long($1) .
1589 ')</span>';
1591 # match <hash>
1592 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1593 # can match only for combined diff
1594 $line = 'index ';
1595 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1596 if ($from->{'href'}[$i]) {
1597 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1598 -class=>"hash"},
1599 substr($diffinfo->{'from_id'}[$i],0,7));
1600 } else {
1601 $line .= '0' x 7;
1603 # separator
1604 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1606 $line .= '..';
1607 if ($to->{'href'}) {
1608 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1609 substr($diffinfo->{'to_id'},0,7));
1610 } else {
1611 $line .= '0' x 7;
1614 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1615 # can match only for ordinary diff
1616 my ($from_link, $to_link);
1617 if ($from->{'href'}) {
1618 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1619 substr($diffinfo->{'from_id'},0,7));
1620 } else {
1621 $from_link = '0' x 7;
1623 if ($to->{'href'}) {
1624 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1625 substr($diffinfo->{'to_id'},0,7));
1626 } else {
1627 $to_link = '0' x 7;
1629 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1630 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1633 return $line . "<br/>\n";
1636 # format from-file/to-file diff header
1637 sub format_diff_from_to_header {
1638 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1639 my $line;
1640 my $result = '';
1642 $line = $from_line;
1643 #assert($line =~ m/^---/) if DEBUG;
1644 # no extra formatting for "^--- /dev/null"
1645 if (! $diffinfo->{'nparents'}) {
1646 # ordinary (single parent) diff
1647 if ($line =~ m!^--- "?a/!) {
1648 if ($from->{'href'}) {
1649 $line = '--- a/' .
1650 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1651 esc_path($from->{'file'}));
1652 } else {
1653 $line = '--- a/' .
1654 esc_path($from->{'file'});
1657 $result .= qq!<div class="diff from_file">$line</div>\n!;
1659 } else {
1660 # combined diff (merge commit)
1661 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1662 if ($from->{'href'}[$i]) {
1663 $line = '--- ' .
1664 $cgi->a({-href=>href(action=>"blobdiff",
1665 hash_parent=>$diffinfo->{'from_id'}[$i],
1666 hash_parent_base=>$parents[$i],
1667 file_parent=>$from->{'file'}[$i],
1668 hash=>$diffinfo->{'to_id'},
1669 hash_base=>$hash,
1670 file_name=>$to->{'file'}),
1671 -class=>"path",
1672 -title=>"diff" . ($i+1)},
1673 $i+1) .
1674 '/' .
1675 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1676 esc_path($from->{'file'}[$i]));
1677 } else {
1678 $line = '--- /dev/null';
1680 $result .= qq!<div class="diff from_file">$line</div>\n!;
1684 $line = $to_line;
1685 #assert($line =~ m/^\+\+\+/) if DEBUG;
1686 # no extra formatting for "^+++ /dev/null"
1687 if ($line =~ m!^\+\+\+ "?b/!) {
1688 if ($to->{'href'}) {
1689 $line = '+++ b/' .
1690 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1691 esc_path($to->{'file'}));
1692 } else {
1693 $line = '+++ b/' .
1694 esc_path($to->{'file'});
1697 $result .= qq!<div class="diff to_file">$line</div>\n!;
1699 return $result;
1702 # create note for patch simplified by combined diff
1703 sub format_diff_cc_simplified {
1704 my ($diffinfo, @parents) = @_;
1705 my $result = '';
1707 $result .= "<div class=\"diff header\">" .
1708 "diff --cc ";
1709 if (!is_deleted($diffinfo)) {
1710 $result .= $cgi->a(
1711 {-href => href(action=>"blob",
1712 $hash && git_get_type($hash) eq 'commit' ?
1713 (hash_base=>$hash) : (),
1714 hash=>$diffinfo->{'to_id'},
1715 file_name=>$diffinfo->{'to_file'}),
1716 -class => "path"},
1717 esc_path($diffinfo->{'to_file'}));
1718 } else {
1719 $result .= esc_path($diffinfo->{'to_file'});
1721 $result .= "</div>\n" . # class="diff header"
1722 "<div class=\"diff nodifferences\">" .
1723 "Simple merge" .
1724 "</div>\n"; # class="diff nodifferences"
1726 return $result;
1729 # format patch (diff) line (not to be used for diff headers)
1730 sub format_diff_line {
1731 my $line = shift;
1732 my ($from, $to) = @_;
1733 my $diff_class = "";
1735 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1736 # combined diff
1737 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1738 if ($line =~ m/^\@{3}/) {
1739 $diff_class = " chunk_header";
1740 } elsif ($line =~ m/^\\/) {
1741 $diff_class = " incomplete";
1742 } elsif ($prefix =~ tr/+/+/) {
1743 $diff_class = " add";
1744 } elsif ($prefix =~ tr/-/-/) {
1745 $diff_class = " rem";
1747 } else {
1748 # assume ordinary diff
1749 my $char = substr($line, 0, 1);
1750 if ($char eq '+') {
1751 $diff_class = " add";
1752 } elsif ($char eq '-') {
1753 $diff_class = " rem";
1754 } elsif ($char eq '@') {
1755 $diff_class = " chunk_header";
1756 } elsif ($char eq "\\") {
1757 $diff_class = " incomplete";
1760 $line = untabify($line);
1761 if ($from && $to && $line =~ m/^\@{2} /) {
1762 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1763 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1765 $from_lines = 0 unless defined $from_lines;
1766 $to_lines = 0 unless defined $to_lines;
1768 if ($from->{'href'}) {
1769 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1770 -class=>"list"}, $from_text);
1772 if ($to->{'href'}) {
1773 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1774 -class=>"list"}, $to_text);
1776 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1777 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1778 return "<div class=\"diff$diff_class\">$line</div>\n";
1779 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1780 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1781 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1783 @from_text = split(' ', $ranges);
1784 for (my $i = 0; $i < @from_text; ++$i) {
1785 ($from_start[$i], $from_nlines[$i]) =
1786 (split(',', substr($from_text[$i], 1)), 0);
1789 $to_text = pop @from_text;
1790 $to_start = pop @from_start;
1791 $to_nlines = pop @from_nlines;
1793 $line = "<span class=\"chunk_info\">$prefix ";
1794 for (my $i = 0; $i < @from_text; ++$i) {
1795 if ($from->{'href'}[$i]) {
1796 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1797 -class=>"list"}, $from_text[$i]);
1798 } else {
1799 $line .= $from_text[$i];
1801 $line .= " ";
1803 if ($to->{'href'}) {
1804 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1805 -class=>"list"}, $to_text);
1806 } else {
1807 $line .= $to_text;
1809 $line .= " $prefix</span>" .
1810 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1811 return "<div class=\"diff$diff_class\">$line</div>\n";
1813 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1816 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1817 # linked. Pass the hash of the tree/commit to snapshot.
1818 sub format_snapshot_links {
1819 my ($hash) = @_;
1820 my @snapshot_fmts = gitweb_check_feature('snapshot');
1821 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1822 my $num_fmts = @snapshot_fmts;
1823 if ($num_fmts > 1) {
1824 # A parenthesized list of links bearing format names.
1825 # e.g. "snapshot (_tar.gz_ _zip_)"
1826 return "snapshot (" . join(' ', map
1827 $cgi->a({
1828 -href => href(
1829 action=>"snapshot",
1830 hash=>$hash,
1831 snapshot_format=>$_
1833 }, $known_snapshot_formats{$_}{'display'})
1834 , @snapshot_fmts) . ")";
1835 } elsif ($num_fmts == 1) {
1836 # A single "snapshot" link whose tooltip bears the format name.
1837 # i.e. "_snapshot_"
1838 my ($fmt) = @snapshot_fmts;
1839 return
1840 $cgi->a({
1841 -href => href(
1842 action=>"snapshot",
1843 hash=>$hash,
1844 snapshot_format=>$fmt
1846 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1847 }, "snapshot");
1848 } else { # $num_fmts == 0
1849 return undef;
1853 ## ......................................................................
1854 ## functions returning values to be passed, perhaps after some
1855 ## transformation, to other functions; e.g. returning arguments to href()
1857 # returns hash to be passed to href to generate gitweb URL
1858 # in -title key it returns description of link
1859 sub get_feed_info {
1860 my $format = shift || 'Atom';
1861 my %res = (action => lc($format));
1863 # feed links are possible only for project views
1864 return unless (defined $project);
1865 # some views should link to OPML, or to generic project feed,
1866 # or don't have specific feed yet (so they should use generic)
1867 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1869 my $branch;
1870 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1871 # from tag links; this also makes possible to detect branch links
1872 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1873 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1874 $branch = $1;
1876 # find log type for feed description (title)
1877 my $type = 'log';
1878 if (defined $file_name) {
1879 $type = "history of $file_name";
1880 $type .= "/" if ($action eq 'tree');
1881 $type .= " on '$branch'" if (defined $branch);
1882 } else {
1883 $type = "log of $branch" if (defined $branch);
1886 $res{-title} = $type;
1887 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1888 $res{'file_name'} = $file_name;
1890 return %res;
1893 ## ----------------------------------------------------------------------
1894 ## git utility subroutines, invoking git commands
1896 # quote the given arguments for passing them to the shell
1897 # quote_command("command", "arg 1", "arg with ' and ! characters")
1898 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1899 # Try to avoid using this function wherever possible.
1900 sub quote_command {
1901 return join(' ',
1902 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1905 # git_get_sha1_or_die ( EXTENDED_OBJECT_IDENTIFER [, TYPE] )
1907 # Look up the object referred to by C<EXTENDED_OBJECT_IDENTIFER> and
1908 # return its SHA1 hash in scalar context or its ($hash, $type, $size)
1909 # in list context. Return an error page to the browser if the object
1910 # couldn't be found.
1912 # If C<TYPE> is given, resolve tag and commit objects if necessary and
1913 # die unless the object found has the right type. The $type return
1914 # value is guaranteed to equal C<TYPE>.
1915 sub git_get_sha1_or_die {
1916 my ($object_id, $want_type) = @_;
1917 # This method shouldn't be used for checking missing
1918 # parameters, since it cannot generate proper error messages.
1919 # Hence we die with 500.
1920 die_error(500, 'No object given') unless $object_id;
1921 my ($hash, $type, $size) = $repo->get_sha1($object_id);
1922 unless ($hash) {
1923 my $human_type = ucfirst($want_type || 'object');
1924 die_error(404, "$human_type not found: '$object_id'");
1926 if ($want_type && $want_type ne $type) {
1927 if ($type eq 'tag') {
1928 return git_get_sha1_or_die(
1929 $repo->get_tag($hash)->object, $want_type);
1930 } elsif ($type eq 'commit' && $want_type eq 'tree') {
1931 return git_get_sha1_or_die(
1932 $repo->get_commit($hash)->tree, $want_type);
1933 } else {
1934 # $object_id and $type can be off due to recursion,
1935 # but fixing it complicates the code too much.
1936 die_error(400, "Expected a $want_type object, but " .
1937 "'$object_id' is a $type object");
1940 return wantarray ? ($hash, $type, $size) : $hash;
1943 # get HEAD ref hash of current project or die if no HEAD ref was found
1944 sub git_get_head_hash {
1945 # Need to have the global $project variable defined.
1946 die_error(400, 'no project given') unless $project;
1947 my $sha1 = $repo->get_sha1('HEAD')
1948 or die_error(500, "HEAD ref not found for project '$project'");
1949 return $sha1;
1952 # get type of given object
1953 sub git_get_type {
1954 my ($sha1, $type, $size) = $repo->get_sha1(shift);
1955 return $type;
1958 # repository configuration
1959 our $config_file = '';
1960 our %config;
1962 # store multiple values for single key as anonymous array reference
1963 # single values stored directly in the hash, not as [ <value> ]
1964 sub hash_set_multi {
1965 my ($hash, $key, $value) = @_;
1967 if (!exists $hash->{$key}) {
1968 $hash->{$key} = $value;
1969 } elsif (!ref $hash->{$key}) {
1970 $hash->{$key} = [ $hash->{$key}, $value ];
1971 } else {
1972 push @{$hash->{$key}}, $value;
1976 # return hash of git project configuration
1977 # optionally limited to some section, e.g. 'gitweb'
1978 sub git_parse_project_config {
1979 my $section_regexp = shift;
1980 my %config;
1982 return unless $repo;
1983 for my $keyval (split "\0", $repo->cmd_output(
1984 cmd => [qw(config -z -l)], cache => 1)) {
1985 my ($key, $value) = split(/\n/, $keyval, 2);
1987 hash_set_multi(\%config, $key, $value)
1988 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1990 return %config;
1993 # convert config value to boolean, 'true' or 'false'
1994 # no value, number > 0, 'true' and 'yes' values are true
1995 # rest of values are treated as false (never as error)
1996 sub config_to_bool {
1997 my $val = shift;
1999 # strip leading and trailing whitespace
2000 $val =~ s/^\s+//;
2001 $val =~ s/\s+$//;
2003 return (!defined $val || # section.key
2004 ($val =~ /^\d+$/ && $val) || # section.key = 1
2005 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2008 # convert config value to simple decimal number
2009 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2010 # to be multiplied by 1024, 1048576, or 1073741824
2011 sub config_to_int {
2012 my $val = shift;
2014 # strip leading and trailing whitespace
2015 $val =~ s/^\s+//;
2016 $val =~ s/\s+$//;
2018 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2019 $unit = lc($unit);
2020 # unknown unit is treated as 1
2021 return $num * ($unit eq 'g' ? 1073741824 :
2022 $unit eq 'm' ? 1048576 :
2023 $unit eq 'k' ? 1024 : 1);
2025 return $val;
2028 # convert config value to array reference, if needed
2029 sub config_to_multi {
2030 my $val = shift;
2032 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2035 sub git_get_project_config {
2036 my ($key, $type) = @_;
2038 # key sanity check
2039 return unless ($key);
2040 $key =~ s/^gitweb\.//;
2041 return if ($key =~ m/\W/);
2043 # type sanity check
2044 if (defined $type) {
2045 $type =~ s/^--//;
2046 $type = undef
2047 unless ($type eq 'bool' || $type eq 'int');
2050 # get config
2051 if (!defined $config_file ||
2052 $config_file ne "$projectroot/$project/config") {
2053 %config = git_parse_project_config('gitweb');
2054 $config_file = "$projectroot/$project/config";
2057 # ensure given type
2058 if (!defined $type) {
2059 return $config{"gitweb.$key"};
2060 } elsif ($type eq 'bool') {
2061 # backward compatibility: 'git config --bool' returns true/false
2062 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2063 } elsif ($type eq 'int') {
2064 return config_to_int($config{"gitweb.$key"});
2066 return $config{"gitweb.$key"};
2069 # Return the SHA1 of the blob or tree at the path in the given commit,
2070 # or return undef if it does not exist.
2071 sub git_get_sha1_by_path {
2072 my ($base, $path, $type) = @_;
2073 $path =~ s,/+$,,;
2074 return $repo->get_sha1("$base:$path", $type);
2077 # Get path of entry with given hash at given tree-ish (ref); used to
2078 # get 'from' filename for combined diff (merge commit) for renames.
2079 # Note that this does not resolve tag or commit objects in the $hash
2080 # parameter, you must pass a tree or blob object.
2081 sub git_get_path_by_hash {
2082 my $base = shift || return;
2083 my $hash = shift || return;
2085 # This subroutine could be extracted into the Git::Tree API
2086 # once it exists.
2087 my $tree = git_get_sha1_or_die($base, 'tree');
2088 my ($file_sha1, $file_type) = $repo->get_sha1($hash);
2089 die_error(404, "object not found: '$hash'") unless $file_sha1;
2090 die_error(400, "'$hash' is a $file_type object, not a tree or blob object")
2091 unless $file_type eq 'blob' || $file_type eq 'tree';
2093 # The ls-tree output can be quite large, so use
2094 # progressive_cmd_output.
2095 my $ls_tree_read = $repo->progressive_cmd_output(
2096 cmd => [qw(ls-tree -r -t), $tree], separator => "\n",
2097 cache => 1);
2098 while (my $line = $ls_tree_read->()) {
2099 if ($line =~ /^[0-9]+ [a-z]+ $file_sha1\t(.+)$/) {
2100 while ($ls_tree_read->()) { } # cache it
2101 # TODO: needs unquoting
2102 return $1;
2105 return undef;
2108 ## ......................................................................
2109 ## git utility functions, directly accessing git repository
2111 # The following subroutines locally change the global $project
2112 # variable as a side-effect so that their calls to
2113 # git_get_project_config work.
2115 sub git_get_project_description {
2116 local $project = shift;
2118 open my $fd, "$projectroot/$project/description"
2119 or return git_get_project_config('description');
2120 my $descr = <$fd>;
2121 close $fd;
2122 if (defined $descr) {
2123 chomp $descr;
2125 return $descr;
2128 sub git_get_project_url_list {
2129 local $project = shift;
2131 open my $fd, "$projectroot/$project/cloneurl"
2132 or return wantarray ?
2133 @{ config_to_multi(git_get_project_config('url')) } :
2134 config_to_multi(git_get_project_config('url'));
2135 my @git_project_url_list = map { chomp; $_ } <$fd>;
2136 close $fd;
2138 return wantarray ? @git_project_url_list : \@git_project_url_list;
2141 sub git_get_projects_list {
2142 my ($filter) = @_;
2143 my @list;
2145 $filter ||= '';
2146 $filter =~ s/\.git$//;
2148 my ($check_forks) = gitweb_check_feature('forks');
2150 if (-d $projects_list) {
2151 # search in directory
2152 my $dir = $projects_list . ($filter ? "/$filter" : '');
2153 # remove the trailing "/"
2154 $dir =~ s!/+$!!;
2155 my $pfxlen = length("$dir");
2156 my $pfxdepth = ($dir =~ tr!/!!);
2158 File::Find::find({
2159 follow_fast => 1, # follow symbolic links
2160 follow_skip => 2, # ignore duplicates
2161 dangling_symlinks => 0, # ignore dangling symlinks, silently
2162 wanted => sub {
2163 # skip project-list toplevel, if we get it.
2164 return if (m!^[/.]$!);
2165 # only directories can be git repositories
2166 return unless (-d $_);
2167 # don't traverse too deep (Find is super slow on os x)
2168 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2169 $File::Find::prune = 1;
2170 return;
2173 my $subdir = substr($File::Find::name, $pfxlen + 1);
2174 # we check related file in $projectroot
2175 if ($check_forks and $subdir =~ m#/.#) {
2176 $File::Find::prune = 1;
2177 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
2178 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
2179 $File::Find::prune = 1;
2182 }, "$dir");
2184 } elsif (-f $projects_list) {
2185 # read from file(url-encoded):
2186 # 'git%2Fgit.git Linus+Torvalds'
2187 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2188 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2189 my %paths;
2190 open my ($fd), $projects_list or return;
2191 PROJECT:
2192 while (my $line = <$fd>) {
2193 chomp $line;
2194 my ($path, $owner) = split ' ', $line;
2195 $path = unescape($path);
2196 $owner = unescape($owner);
2197 if (!defined $path) {
2198 next;
2200 if ($filter ne '') {
2201 # looking for forks;
2202 my $pfx = substr($path, 0, length($filter));
2203 if ($pfx ne $filter) {
2204 next PROJECT;
2206 my $sfx = substr($path, length($filter));
2207 if ($sfx !~ /^\/.*\.git$/) {
2208 next PROJECT;
2210 } elsif ($check_forks) {
2211 PATH:
2212 foreach my $filter (keys %paths) {
2213 # looking for forks;
2214 my $pfx = substr($path, 0, length($filter));
2215 if ($pfx ne $filter) {
2216 next PATH;
2218 my $sfx = substr($path, length($filter));
2219 if ($sfx !~ /^\/.*\.git$/) {
2220 next PATH;
2222 # is a fork, don't include it in
2223 # the list
2224 next PROJECT;
2227 if (check_export_ok("$projectroot/$path")) {
2228 my $pr = {
2229 path => $path,
2230 owner => to_utf8($owner),
2232 push @list, $pr;
2233 (my $forks_path = $path) =~ s/\.git$//;
2234 $paths{$forks_path}++;
2237 close $fd;
2239 return @list;
2242 our $gitweb_project_owner = undef;
2243 sub git_get_project_list_from_file {
2245 return if (defined $gitweb_project_owner);
2247 $gitweb_project_owner = {};
2248 # read from file (url-encoded):
2249 # 'git%2Fgit.git Linus+Torvalds'
2250 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2251 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2252 if (-f $projects_list) {
2253 open (my $fd , $projects_list);
2254 while (my $line = <$fd>) {
2255 chomp $line;
2256 my ($pr, $ow) = split ' ', $line;
2257 $pr = unescape($pr);
2258 $ow = unescape($ow);
2259 $gitweb_project_owner->{$pr} = to_utf8($ow);
2261 close $fd;
2265 sub git_get_project_owner {
2266 local $project = shift;
2267 my $owner;
2269 return undef unless $project;
2271 if (!defined $gitweb_project_owner) {
2272 git_get_project_list_from_file();
2275 if (exists $gitweb_project_owner->{$project}) {
2276 $owner = $gitweb_project_owner->{$project};
2278 if (!defined $owner){
2279 $owner = git_get_project_config('owner');
2281 if (!defined $owner) {
2282 $owner = get_file_owner("$projectroot/$project");
2285 return $owner;
2288 sub git_get_last_activity {
2289 my $path = shift;
2291 chomp(my $most_recent = $repo_root->repo(repo_dir => $path)->cmd_output(
2292 cmd => [ qw(for-each-ref --count=1 --format=%(committer)),
2293 qw(--sort=-committerdate refs/heads) ],
2294 cache => 1, max_exit_code => 255)) or return;
2295 $most_recent =~ / (\d+) [-+][01]\d\d\d$/ or return;
2296 my $timestamp = $1;
2297 my $age = time - $timestamp;
2298 return ($age, age_string($age));
2301 # Return a hashref from SHA1s to arrayrefs of ref names. Example:
2302 # { '7e51...' => ['tags/tag-object'], # tag SHA1
2303 # '51ba...' => ['tags/tag-object'], # referenced commit SHA1
2304 # '3c4a...' => ['heads/master', 'tags/another-tag'] }
2305 sub git_get_references {
2306 my $type = shift || "";
2307 my %refs;
2308 # This is not implementable in terms of a simple for-each-refs
2309 # call (e.g. extractable to Git::Repo->get_refs) because it
2310 # doesn't dereference, and we cannot dereference a lot of
2311 # SHA1s ourselves as long as there is no implementation that
2312 # uses Cache::Memcached->get_multi. Hence, we use
2313 # cmd_output.
2314 my @lines = split "\n", $repo->cmd_output(
2315 cmd => ['show-ref', '--dereference',
2316 ($type ? ("--", "refs/$type") : ())],
2317 max_exit_code => 1, # exits with status 1 on empty repos
2318 cache => 1);
2319 for my $line (@lines) {
2320 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2321 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2322 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
2323 if (defined $refs{$1}) {
2324 push @{$refs{$1}}, $2;
2325 } else {
2326 $refs{$1} = [ $2 ];
2330 return \%refs;
2333 sub git_get_rev_name_tags {
2334 my $hash = shift or die_error();
2336 my $name = $repo->name_rev($hash);
2337 $name =~ s!^tags/!! if $name;
2338 return $name;
2341 ## ----------------------------------------------------------------------
2342 ## parse to hash functions
2344 sub parse_date {
2345 my $epoch = shift;
2346 my $tz = shift || "-0000";
2348 my %date;
2349 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2350 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2351 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2352 $date{'hour'} = $hour;
2353 $date{'minute'} = $min;
2354 $date{'mday'} = $mday;
2355 $date{'day'} = $days[$wday];
2356 $date{'month'} = $months[$mon];
2357 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2358 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2359 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2360 $mday, $months[$mon], $hour ,$min;
2361 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2362 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2364 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2365 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2366 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2367 $date{'hour_local'} = $hour;
2368 $date{'minute_local'} = $min;
2369 $date{'tz_local'} = $tz;
2370 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2371 1900+$year, $mon+1, $mday,
2372 $hour, $min, $sec, $tz);
2373 return %date;
2376 sub parse_tag {
2377 my $sha1 = shift;
2378 my %tag;
2379 my @comment;
2381 my ($raw_header, $raw_comment) = split "\n\n", $repo->get_object($sha1), 2;
2382 for my $line (split "\n", $raw_header) {
2383 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2384 $tag{'object'} = $1;
2385 } elsif ($line =~ m/^type (.+)$/) {
2386 $tag{'type'} = $1;
2387 } elsif ($line =~ m/^tag (.+)$/) {
2388 $tag{'name'} = $1;
2389 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2390 $tag{'author'} = $1;
2391 $tag{'epoch'} = $2;
2392 $tag{'tz'} = $3;
2395 $tag{'comment'} = [split "\n", $raw_comment];
2396 return %tag
2399 sub parse_commit_text {
2400 my ($commit_text, $withparents) = @_;
2401 my @commit_lines = split '\n', $commit_text;
2402 my %co;
2404 if (! @commit_lines) {
2405 return;
2408 my $header = shift @commit_lines;
2409 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2410 return;
2412 ($co{'id'}, my @parents) = split ' ', $header;
2413 while (my $line = shift @commit_lines) {
2414 last if $line eq "\n";
2415 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2416 $co{'tree'} = $1;
2417 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2418 push @parents, $1;
2419 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2420 $co{'author'} = $1;
2421 $co{'author_epoch'} = $2;
2422 $co{'author_tz'} = $3;
2423 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2424 $co{'author_name'} = $1;
2425 $co{'author_email'} = $2;
2426 } else {
2427 $co{'author_name'} = $co{'author'};
2429 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2430 $co{'committer'} = $1;
2431 $co{'committer_epoch'} = $2;
2432 $co{'committer_tz'} = $3;
2433 $co{'committer_name'} = $co{'committer'};
2434 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2435 $co{'committer_name'} = $1;
2436 $co{'committer_email'} = $2;
2437 } else {
2438 $co{'committer_name'} = $co{'committer'};
2442 if (!defined $co{'tree'}) {
2443 return;
2445 $co{'parents'} = \@parents;
2446 $co{'parent'} = $parents[0];
2448 foreach my $title (@commit_lines) {
2449 $title =~ s/^ //;
2450 if ($title ne "") {
2451 $co{'title'} = chop_str($title, 80, 5);
2452 # remove leading stuff of merges to make the interesting part visible
2453 if (length($title) > 50) {
2454 $title =~ s/^Automatic //;
2455 $title =~ s/^merge (of|with) /Merge ... /i;
2456 if (length($title) > 50) {
2457 $title =~ s/(http|rsync):\/\///;
2459 if (length($title) > 50) {
2460 $title =~ s/(master|www|rsync)\.//;
2462 if (length($title) > 50) {
2463 $title =~ s/kernel.org:?//;
2465 if (length($title) > 50) {
2466 $title =~ s/\/pub\/scm//;
2469 $co{'title_short'} = chop_str($title, 50, 5);
2470 last;
2473 if ($co{'title'} eq "") {
2474 $co{'title'} = $co{'title_short'} = '(no commit message)';
2476 # remove added spaces
2477 foreach my $line (@commit_lines) {
2478 $line =~ s/^ //;
2480 $co{'comment'} = \@commit_lines;
2482 my $age = time - $co{'committer_epoch'};
2483 $co{'age'} = $age;
2484 $co{'age_string'} = age_string($age);
2485 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2486 if ($age > 60*60*24*7*2) {
2487 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2488 $co{'age_string_age'} = $co{'age_string'};
2489 } else {
2490 $co{'age_string_date'} = $co{'age_string'};
2491 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2493 return %co;
2496 sub parse_commit {
2497 my ($commit_id) = @_;
2498 # This currently supports arbitrary object names, so we
2499 # resolve it here.
2500 my $sha1 = git_get_sha1_or_die($commit_id, 'commit');
2502 # This formats commits slightly differently than the raw
2503 # cat-file, so we cannot use get_object here. This function
2504 # should be replaced by $repo->get_commit anyway.
2505 my $commit_text = $repo->cmd_output(
2506 cmd => [qw(rev-list --parents --header --max-count=1), $sha1, '--'],
2507 cache => 2);
2508 $commit_text =~ s/\x00$//;
2509 return parse_commit_text($commit_text, 1);
2512 sub parse_commits {
2513 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2514 # This currently supports arbitrary object names, so we
2515 # resolve it here.
2516 my $sha1 = git_get_sha1_or_die($commit_id);
2517 git_get_sha1_or_die("$sha1:$filename") if $filename; # check existence
2519 $maxcount ||= 1;
2520 $skip ||= 0;
2522 # TODO: Integrate this into Git::Repo, and get rid of @args
2523 # and @extra_options.
2524 my @raw_commits = split "\0", $repo->cmd_output(
2525 cmd => ['rev-list', '--header', @args, "--max-count=$maxcount",
2526 "--skip=$skip", @extra_options, $sha1, "--",
2527 $filename || ()],
2528 cache => 2);
2529 my @cos = map { { parse_commit_text($_) } } @raw_commits;
2530 return wantarray ? @cos : \@cos;
2533 # parse line of git-diff-tree "raw" output
2534 sub parse_difftree_raw_line {
2535 my $line = shift;
2536 my %res;
2538 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2539 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2540 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2541 $res{'from_mode'} = $1;
2542 $res{'to_mode'} = $2;
2543 $res{'from_id'} = $3;
2544 $res{'to_id'} = $4;
2545 $res{'status'} = $5;
2546 $res{'similarity'} = $6;
2547 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2548 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2549 } else {
2550 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2553 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2554 # combined diff (for merge commit)
2555 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2556 $res{'nparents'} = length($1);
2557 $res{'from_mode'} = [ split(' ', $2) ];
2558 $res{'to_mode'} = pop @{$res{'from_mode'}};
2559 $res{'from_id'} = [ split(' ', $3) ];
2560 $res{'to_id'} = pop @{$res{'from_id'}};
2561 $res{'status'} = [ split('', $4) ];
2562 $res{'to_file'} = unquote($5);
2564 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2565 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2566 $res{'commit'} = $1;
2569 return wantarray ? %res : \%res;
2572 # wrapper: return parsed line of git-diff-tree "raw" output
2573 # (the argument might be raw line, or parsed info)
2574 sub parsed_difftree_line {
2575 my $line_or_ref = shift;
2577 if (ref($line_or_ref) eq "HASH") {
2578 # pre-parsed (or generated by hand)
2579 return $line_or_ref;
2580 } else {
2581 return parse_difftree_raw_line($line_or_ref);
2585 # parse line of git-ls-tree output
2586 sub parse_ls_tree_line ($;%) {
2587 my $line = shift;
2588 my %opts = @_;
2589 my %res;
2591 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2592 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2594 $res{'mode'} = $1;
2595 $res{'type'} = $2;
2596 $res{'hash'} = $3;
2597 if ($opts{'-z'}) {
2598 $res{'name'} = $4;
2599 } else {
2600 $res{'name'} = unquote($4);
2603 return wantarray ? %res : \%res;
2606 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2607 sub parse_from_to_diffinfo {
2608 my ($diffinfo, $from, $to, @parents) = @_;
2610 if ($diffinfo->{'nparents'}) {
2611 # combined diff
2612 $from->{'file'} = [];
2613 $from->{'href'} = [];
2614 fill_from_file_info($diffinfo, @parents)
2615 unless exists $diffinfo->{'from_file'};
2616 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2617 $from->{'file'}[$i] =
2618 defined $diffinfo->{'from_file'}[$i] ?
2619 $diffinfo->{'from_file'}[$i] :
2620 $diffinfo->{'to_file'};
2621 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2622 $from->{'href'}[$i] = href(action=>"blob",
2623 hash_base=>$parents[$i],
2624 hash=>$diffinfo->{'from_id'}[$i],
2625 file_name=>$from->{'file'}[$i]);
2626 } else {
2627 $from->{'href'}[$i] = undef;
2630 } else {
2631 # ordinary (not combined) diff
2632 $from->{'file'} = $diffinfo->{'from_file'};
2633 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2634 $from->{'href'} = href(action=>"blob",
2635 $hash_parent && git_get_type($hash_parent) eq 'commit' ?
2636 (hash_base=>$hash_parent) : (),
2637 hash=>$diffinfo->{'from_id'},
2638 file_name=>$from->{'file'});
2639 } else {
2640 delete $from->{'href'};
2644 $to->{'file'} = $diffinfo->{'to_file'};
2645 if (!is_deleted($diffinfo)) { # file exists in result
2646 $to->{'href'} = href(action=>"blob",
2647 $hash && git_get_type($hash) eq 'commit' ?
2648 (hash_base=>$hash) : (),
2649 hash=>$diffinfo->{'to_id'},
2650 file_name=>$to->{'file'});
2651 } else {
2652 delete $to->{'href'};
2656 ## ......................................................................
2657 ## parse to array of hashes functions
2659 sub git_get_heads_list {
2660 my $limit = shift;
2661 my @headslist;
2663 my @lines = split "\n", $repo->cmd_output(
2664 cmd => ['for-each-ref', '--sort=-committerdate',
2665 $limit ? '--count='.($limit+1) : (),
2666 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2667 'refs/heads'],
2668 cache => 1);
2669 for my $line (@lines) {
2670 my %ref_item;
2672 my ($refinfo, $committerinfo) = split(/\0/, $line);
2673 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2674 my ($committer, $epoch, $tz) =
2675 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2676 $ref_item{'fullname'} = $name;
2677 $name =~ s!^refs/heads/!!;
2679 $ref_item{'name'} = $name;
2680 $ref_item{'id'} = $hash;
2681 $ref_item{'title'} = $title || '(no commit message)';
2682 $ref_item{'epoch'} = $epoch;
2683 if ($epoch) {
2684 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2685 } else {
2686 $ref_item{'age'} = "unknown";
2689 push @headslist, \%ref_item;
2692 return wantarray ? @headslist : \@headslist;
2695 sub git_get_tags_list {
2696 my $limit = shift;
2697 my @tagslist;
2699 my @lines = split "\n", $repo->cmd_output(
2700 cmd => ['for-each-ref', '--sort=-creatordate',
2701 ($limit ? '--count='.($limit+1) : ()),
2702 '--format=%(objectname) %(objecttype) %(refname) '.
2703 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2704 'refs/tags'],
2705 cache => 1);
2706 for my $line (@lines) {
2707 my %ref_item;
2709 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2710 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2711 my ($creator, $epoch, $tz) =
2712 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2713 $ref_item{'fullname'} = $name;
2714 $name =~ s!^refs/tags/!!;
2716 $ref_item{'type'} = $type;
2717 $ref_item{'id'} = $id;
2718 $ref_item{'name'} = $name;
2719 if ($type eq "tag") {
2720 $ref_item{'subject'} = $title;
2721 $ref_item{'reftype'} = $reftype;
2722 $ref_item{'refid'} = $refid;
2723 } else {
2724 $ref_item{'reftype'} = $type;
2725 $ref_item{'refid'} = $id;
2728 if ($type eq "tag" || $type eq "commit") {
2729 $ref_item{'epoch'} = $epoch;
2730 if ($epoch) {
2731 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2732 } else {
2733 $ref_item{'age'} = "unknown";
2737 push @tagslist, \%ref_item;
2740 return wantarray ? @tagslist : \@tagslist;
2743 ## ----------------------------------------------------------------------
2744 ## filesystem-related functions
2746 sub get_file_owner {
2747 my $path = shift;
2749 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2750 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2751 if (!defined $gcos) {
2752 return undef;
2754 my $owner = $gcos;
2755 $owner =~ s/[,;].*$//;
2756 return to_utf8($owner);
2759 ## ......................................................................
2760 ## mimetype related functions
2762 sub mimetype_guess_file {
2763 my $filename = shift;
2764 my $mimemap = shift;
2765 -r $mimemap or return undef;
2767 my %mimemap;
2768 open(MIME, $mimemap) or return undef;
2769 while (<MIME>) {
2770 next if m/^#/; # skip comments
2771 my ($mime, $exts) = split(/\t+/);
2772 if (defined $exts) {
2773 my @exts = split(/\s+/, $exts);
2774 foreach my $ext (@exts) {
2775 $mimemap{$ext} = $mime;
2779 close(MIME);
2781 $filename =~ /\.([^.]*)$/;
2782 return $mimemap{$1};
2785 sub mimetype_guess {
2786 my $filename = shift;
2787 my $mime;
2788 $filename =~ /\./ or return undef;
2790 if ($mimetypes_file) {
2791 my $file = $mimetypes_file;
2792 if ($file !~ m!^/!) { # if it is relative path
2793 # it is relative to project
2794 $file = "$projectroot/$project/$file";
2796 $mime = mimetype_guess_file($filename, $file);
2798 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2799 return $mime;
2802 # Replacement for (heuristic) -T operator. (perldoc -f -T)
2803 sub is_ascii_text {
2804 my $text = shift;
2805 return 1 if length($text) == 0;
2806 return ((grep { ord($_) > 127 } split('', $text)) / length $text) <= 0.3;
2809 # Determine the MIME type of a blob based on its file name ($filename)
2810 # and its first n bytes ($snippet).
2811 sub blob_mimetype {
2812 my $snippet = shift;
2813 my $filename = shift;
2815 if ($filename) {
2816 my $mime = mimetype_guess($filename);
2817 $mime and return $mime;
2820 if (is_ascii_text($snippet)) {
2821 return 'text/plain';
2822 } elsif (! $filename) {
2823 return 'application/octet-stream';
2824 } elsif ($filename =~ m/\.png$/i) {
2825 return 'image/png';
2826 } elsif ($filename =~ m/\.gif$/i) {
2827 return 'image/gif';
2828 } elsif ($filename =~ m/\.jpe?g$/i) {
2829 return 'image/jpeg';
2830 } else {
2831 return 'application/octet-stream';
2835 sub blob_contenttype {
2836 my ($snippet, $file_name, $type) = @_;
2838 $type ||= blob_mimetype($snippet, $file_name);
2839 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2840 $type .= "; charset=$default_text_plain_charset";
2843 return $type;
2846 ## ======================================================================
2847 ## functions printing HTML: header, footer, error page
2849 sub git_header_html {
2850 my $status = shift || "200 OK";
2851 my $expires = shift || 'now';
2853 my $title = "$site_name";
2854 if (defined $project) {
2855 $title .= " - " . to_utf8($project);
2856 if (defined $action) {
2857 $title .= "/$action";
2858 if (defined $file_name) {
2859 $title .= " - " . esc_path($file_name);
2860 if ($action eq "tree" && $file_name !~ m|/$|) {
2861 $title .= "/";
2866 my $content_type;
2867 # require explicit support from the UA if we are to send the page as
2868 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2869 # we have to do this because MSIE sometimes globs '*/*', pretending to
2870 # support xhtml+xml but choking when it gets what it asked for.
2871 if (defined $cgi->http('HTTP_ACCEPT') &&
2872 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2873 $cgi->Accept('application/xhtml+xml') != 0) {
2874 $content_type = 'application/xhtml+xml';
2875 } else {
2876 $content_type = 'text/html';
2878 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2879 -status=> $status, -expires => $expires);
2880 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2881 print <<EOF;
2882 <?xml version="1.0" encoding="utf-8"?>
2883 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2884 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2885 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2886 <!-- git core binaries version $git_version -->
2887 <head>
2888 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2889 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2890 <meta name="robots" content="index, nofollow"/>
2891 <title>$title</title>
2893 # print out each stylesheet that exist
2894 if (defined $stylesheet) {
2895 #provides backwards capability for those people who define style sheet in a config file
2896 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2897 } else {
2898 foreach my $stylesheet (@stylesheets) {
2899 next unless $stylesheet;
2900 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2903 if (defined $project) {
2904 my %href_params = get_feed_info();
2905 if (!exists $href_params{'-title'}) {
2906 $href_params{'-title'} = 'log';
2909 foreach my $format qw(RSS Atom) {
2910 my $type = lc($format);
2911 my %link_attr = (
2912 '-rel' => 'alternate',
2913 '-title' => "$project - $href_params{'-title'} - $format feed",
2914 '-type' => "application/$type+xml"
2917 $href_params{'action'} = $type;
2918 $link_attr{'-href'} = href(%href_params);
2919 print "<link ".
2920 "rel=\"$link_attr{'-rel'}\" ".
2921 "title=\"$link_attr{'-title'}\" ".
2922 "href=\"$link_attr{'-href'}\" ".
2923 "type=\"$link_attr{'-type'}\" ".
2924 "/>\n";
2926 $href_params{'extra_options'} = '--no-merges';
2927 $link_attr{'-href'} = href(%href_params);
2928 $link_attr{'-title'} .= ' (no merges)';
2929 print "<link ".
2930 "rel=\"$link_attr{'-rel'}\" ".
2931 "title=\"$link_attr{'-title'}\" ".
2932 "href=\"$link_attr{'-href'}\" ".
2933 "type=\"$link_attr{'-type'}\" ".
2934 "/>\n";
2937 } else {
2938 printf('<link rel="alternate" title="%s projects list" '.
2939 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2940 $site_name, href(project=>undef, action=>"project_index"));
2941 printf('<link rel="alternate" title="%s projects feeds" '.
2942 'href="%s" type="text/x-opml" />'."\n",
2943 $site_name, href(project=>undef, action=>"opml"));
2945 if (defined $favicon) {
2946 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2949 print "</head>\n" .
2950 "<body>\n";
2952 if ($site_header && -f $site_header) {
2953 open (my $fd, $site_header);
2954 print <$fd>;
2955 close $fd;
2958 print "<div class=\"page_header\">\n" .
2959 $cgi->a({-href => esc_url($logo_url),
2960 -title => $logo_label},
2961 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2962 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2963 if (defined $project) {
2964 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2965 if (defined $action) {
2966 print " / $action";
2968 print "\n";
2970 print "</div>\n";
2972 if (defined $project && gitweb_check_feature('search') &&
2973 $repo->get_sha1('HEAD')) {
2974 if (!defined $searchtext) {
2975 $searchtext = "";
2977 my $search_hash;
2978 if (defined $hash_base) {
2979 $search_hash = $hash_base;
2980 } elsif (defined $hash) {
2981 $search_hash = $hash;
2982 } else {
2983 $search_hash = "HEAD";
2985 my $action = $my_uri;
2986 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2987 if ($use_pathinfo) {
2988 $action .= "/".esc_url($project);
2990 print $cgi->startform(-method => "get", -action => $action) .
2991 "<div class=\"search\">\n" .
2992 (!$use_pathinfo &&
2993 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2994 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2995 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2996 $cgi->popup_menu(-name => 'st', -default => 'commit',
2997 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2998 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2999 " search:\n",
3000 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3001 "<span title=\"Extended regular expression\">" .
3002 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3003 -checked => $search_use_regexp) .
3004 "</span>" .
3005 "</div>" .
3006 $cgi->end_form() . "\n";
3010 sub git_footer_html {
3011 my $feed_class = 'rss_logo';
3013 print "<div class=\"page_footer\">\n";
3014 if (defined $project) {
3015 my $descr = git_get_project_description($project);
3016 if (defined $descr) {
3017 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3020 my %href_params = get_feed_info();
3021 if (!%href_params) {
3022 $feed_class .= ' generic';
3024 $href_params{'-title'} ||= 'log';
3026 foreach my $format qw(RSS Atom) {
3027 $href_params{'action'} = lc($format);
3028 print $cgi->a({-href => href(%href_params),
3029 -title => "$href_params{'-title'} $format feed",
3030 -class => $feed_class}, $format)."\n";
3033 } else {
3034 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3035 -class => $feed_class}, "OPML") . " ";
3036 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3037 -class => $feed_class}, "TXT") . "\n";
3039 print "</div>\n"; # class="page_footer"
3041 if ($page_info) {
3042 print "<div class=\"page_info\">\n";
3043 my $print_stats = sub {
3044 my ($name, $cache_exists) = (shift, shift);
3045 my %s = @_; # statistics hash
3046 if ($cache_exists) {
3047 my $gets = $s{hits} + $s{misses};
3048 print "<p>" . ucfirst($name) . ": " .
3049 "<b>$gets</b> gets " .
3050 "(<b>$s{hits}</b> hits + ".
3051 "<b>$s{misses}</b> misses); " .
3052 "<b>$s{sets}</b> sets, " .
3053 "<b>$s{failed_sets}</b> failed sets.</p>\n";
3054 if ($page_info == 2 && @{$s{get_list}}) {
3055 print "<pre class=\"cache_list\">";
3056 print join("\n",
3057 map(esc_html(join ',',
3058 map(defined $_ ? " $_" : '',
3059 @$_)),
3060 @{$s{get_list}}));
3061 print "</pre>\n";
3063 } else {
3064 print "<p><i>No $name.</i></p>\n";
3067 $print_stats->('main cache', $cache,
3068 %CachedRepo::cache_statistics);
3069 $print_stats->('large cache', $large_cache_root,
3070 %CachedRepo::large_cache_statistics);
3071 print "</div>\n"; # class="page_info"
3074 if (-f $site_footer) {
3075 open (my $fd, $site_footer);
3076 print <$fd>;
3077 close $fd;
3080 print "</body>\n" .
3081 "</html>";
3084 # die_error(<http_status_code>, <error_message>)
3085 # Example: die_error(404, 'Hash not found')
3086 # By convention, use the following status codes (as defined in RFC 2616):
3087 # 400: Invalid or missing CGI parameters, or
3088 # requested object exists but has wrong type.
3089 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3090 # this server or project.
3091 # 404: Requested object/revision/project doesn't exist.
3092 # 500: The server isn't configured properly, or
3093 # an internal error occurred (e.g. failed assertions caused by bugs), or
3094 # an unknown error occurred (e.g. the git binary died unexpectedly).
3095 sub die_error {
3096 my $status = shift || 500;
3097 my $error = shift || "Internal server error";
3099 my %http_responses = (400 => '400 Bad Request',
3100 403 => '403 Forbidden',
3101 404 => '404 Not Found',
3102 500 => '500 Internal Server Error');
3103 git_header_html($http_responses{$status});
3104 print <<EOF;
3105 <div class="page_body">
3106 <br /><br />
3107 $status - $error
3108 <br />
3109 </div>
3111 git_footer_html();
3112 exit;
3115 ## ----------------------------------------------------------------------
3116 ## functions printing or outputting HTML: navigation
3118 sub git_print_page_nav {
3119 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3120 $extra = '' if !defined $extra; # pager or formats
3122 return unless $repo->get_sha1('HEAD'); # no navigation for empty repos
3124 my @navs = qw(summary shortlog log commit commitdiff tree);
3125 if ($suppress) {
3126 @navs = grep { $_ ne $suppress } @navs;
3129 my %arg = map { $_ => {action=>$_} } @navs;
3130 if (defined $head) {
3131 for (qw(commit commitdiff)) {
3132 $arg{$_}{'hash'} = $head;
3134 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3135 for (qw(shortlog log)) {
3136 $arg{$_}{'hash'} = $head;
3140 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3141 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3143 print "<div class=\"page_nav\">\n" .
3144 (join " | ",
3145 map { $_ eq $current ?
3146 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
3147 } @navs);
3148 print "<br/>\n$extra<br/>\n" .
3149 "</div>\n";
3152 sub format_paging_nav {
3153 my ($action, $hash, $head, $page, $has_next_link) = @_;
3154 my $paging_nav;
3157 if ($hash ne $head || $page) {
3158 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
3159 } else {
3160 $paging_nav .= "HEAD";
3163 if ($page > 0) {
3164 $paging_nav .= " &sdot; " .
3165 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3166 -accesskey => "p", -title => "Alt-p"}, "prev");
3167 } else {
3168 $paging_nav .= " &sdot; prev";
3171 if ($has_next_link) {
3172 $paging_nav .= " &sdot; " .
3173 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3174 -accesskey => "n", -title => "Alt-n"}, "next");
3175 } else {
3176 $paging_nav .= " &sdot; next";
3179 return $paging_nav;
3182 ## ......................................................................
3183 ## functions printing or outputting HTML: div
3185 sub git_print_header_div {
3186 my ($action, $title, $hash, $hash_base) = @_;
3187 my %args = ();
3189 $args{'action'} = $action;
3190 $args{'hash'} = $hash if $hash;
3191 $args{'hash_base'} = $hash_base if $hash_base;
3193 print "<div class=\"header\">\n" .
3194 $cgi->a({-href => href(%args), -class => "title"},
3195 $title ? $title : $action) .
3196 "\n</div>\n";
3199 #sub git_print_authorship (\%) {
3200 sub git_print_authorship {
3201 my $co = shift;
3203 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3204 print "<div class=\"author_date\">" .
3205 esc_html($co->{'author_name'}) .
3206 " [$ad{'rfc2822'}";
3207 if ($ad{'hour_local'} < 6) {
3208 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3209 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3210 } else {
3211 printf(" (%02d:%02d %s)",
3212 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3214 print "]</div>\n";
3217 sub git_print_page_path {
3218 my $name = shift;
3219 my $type = shift;
3220 my $hb = shift;
3223 print "<div class=\"page_path\">";
3224 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3225 -title => 'tree root'}, to_utf8("[$project]"));
3226 print " / ";
3227 if (defined $name) {
3228 my @dirname = split '/', $name;
3229 my $basename = pop @dirname;
3230 my $fullname = '';
3232 foreach my $dir (@dirname) {
3233 $fullname .= ($fullname ? '/' : '') . $dir;
3234 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3235 hash_base=>$hb),
3236 -title => $fullname}, esc_path($dir));
3237 print " / ";
3239 if (defined $type && $type eq 'blob') {
3240 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3241 hash_base=>$hb),
3242 -title => $name}, esc_path($basename));
3243 } elsif (defined $type && $type eq 'tree') {
3244 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3245 hash_base=>$hb),
3246 -title => $name}, esc_path($basename));
3247 print " / ";
3248 } else {
3249 print esc_path($basename);
3252 print "<br/></div>\n";
3255 # sub git_print_log (\@;%) {
3256 sub git_print_log ($;%) {
3257 my $log = shift;
3258 my %opts = @_;
3260 if ($opts{'-remove_title'}) {
3261 # remove title, i.e. first line of log
3262 shift @$log;
3264 # remove leading empty lines
3265 while (defined $log->[0] && $log->[0] eq "") {
3266 shift @$log;
3269 # print log
3270 my $signoff = 0;
3271 my $empty = 0;
3272 foreach my $line (@$log) {
3273 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3274 $signoff = 1;
3275 $empty = 0;
3276 if (! $opts{'-remove_signoff'}) {
3277 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3278 next;
3279 } else {
3280 # remove signoff lines
3281 next;
3283 } else {
3284 $signoff = 0;
3287 # print only one empty line
3288 # do not print empty line after signoff
3289 if ($line eq "") {
3290 next if ($empty || $signoff);
3291 $empty = 1;
3292 } else {
3293 $empty = 0;
3296 print format_log_line_html($line) . "<br/>\n";
3299 if ($opts{'-final_empty_line'}) {
3300 # end with single empty line
3301 print "<br/>\n" unless $empty;
3305 # given link target, and the directory (basedir) the link is in,
3306 # return target of link relative to top directory (top tree);
3307 # return undef if it is not possible (including absolute links).
3308 sub normalize_link_target {
3309 my ($link_target, $basedir, $hash_base) = @_;
3311 # we can normalize symlink target only if $hash_base is provided
3312 return unless $hash_base;
3314 # absolute symlinks (beginning with '/') cannot be normalized
3315 return if (substr($link_target, 0, 1) eq '/');
3317 # normalize link target to path from top (root) tree (dir)
3318 my $path;
3319 if ($basedir) {
3320 $path = $basedir . '/' . $link_target;
3321 } else {
3322 # we are in top (root) tree (dir)
3323 $path = $link_target;
3326 # remove //, /./, and /../
3327 my @path_parts;
3328 foreach my $part (split('/', $path)) {
3329 # discard '.' and ''
3330 next if (!$part || $part eq '.');
3331 # handle '..'
3332 if ($part eq '..') {
3333 if (@path_parts) {
3334 pop @path_parts;
3335 } else {
3336 # link leads outside repository (outside top dir)
3337 return;
3339 } else {
3340 push @path_parts, $part;
3343 $path = join('/', @path_parts);
3345 return $path;
3348 # print tree entry (row of git_tree), but without encompassing <tr> element
3349 sub git_print_tree_entry {
3350 my ($t, $basedir, $hash_base, $have_blame) = @_;
3352 my %base_key = ();
3353 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3355 # The format of a table row is: mode list link. Where mode is
3356 # the mode of the entry, list is the name of the entry, an href,
3357 # and link is the action links of the entry.
3359 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3360 if ($t->{'type'} eq "blob") {
3361 print "<td class=\"list\">" .
3362 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3363 file_name=>"$basedir$t->{'name'}", %base_key),
3364 -class => "list"}, esc_path($t->{'name'}));
3365 if (S_ISLNK(oct $t->{'mode'})) {
3366 my $link_target = $repo->get_object($t->{'hash'});
3367 if ($link_target) {
3368 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3369 if (defined $norm_target) {
3370 print " -> " .
3371 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3372 file_name=>$norm_target),
3373 -title => $norm_target}, esc_path($link_target));
3374 } else {
3375 print " -> " . esc_path($link_target);
3379 print "</td>\n";
3380 print "<td class=\"link\">";
3381 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3382 file_name=>"$basedir$t->{'name'}", %base_key)},
3383 "blob");
3384 if ($have_blame) {
3385 print " | " .
3386 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3387 file_name=>"$basedir$t->{'name'}", %base_key)},
3388 "blame");
3390 if (defined $hash_base) {
3391 print " | " .
3392 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3393 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3394 "history");
3396 print " | " .
3397 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3398 file_name=>"$basedir$t->{'name'}")},
3399 "raw");
3400 print "</td>\n";
3402 } elsif ($t->{'type'} eq "tree") {
3403 print "<td class=\"list\">";
3404 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3405 file_name=>"$basedir$t->{'name'}", %base_key)},
3406 esc_path($t->{'name'}));
3407 print "</td>\n";
3408 print "<td class=\"link\">";
3409 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3410 file_name=>"$basedir$t->{'name'}", %base_key)},
3411 "tree");
3412 if (defined $hash_base) {
3413 print " | " .
3414 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3415 file_name=>"$basedir$t->{'name'}")},
3416 "history");
3418 print "</td>\n";
3419 } else {
3420 # unknown object: we can only present history for it
3421 # (this includes 'commit' object, i.e. submodule support)
3422 print "<td class=\"list\">" .
3423 esc_path($t->{'name'}) .
3424 "</td>\n";
3425 print "<td class=\"link\">";
3426 if (defined $hash_base) {
3427 print $cgi->a({-href => href(action=>"history",
3428 hash_base=>$hash_base,
3429 file_name=>"$basedir$t->{'name'}")},
3430 "history");
3432 print "</td>\n";
3436 ## ......................................................................
3437 ## functions printing large fragments of HTML
3439 # get pre-image filenames for merge (combined) diff
3440 sub fill_from_file_info {
3441 my ($diff, @parents) = @_;
3443 $diff->{'from_file'} = [ ];
3444 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3445 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3446 if ($diff->{'status'}[$i] eq 'R' ||
3447 $diff->{'status'}[$i] eq 'C') {
3448 $diff->{'from_file'}[$i] =
3449 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3453 return $diff;
3456 # is current raw difftree line of file deletion
3457 sub is_deleted {
3458 my $diffinfo = shift;
3460 return $diffinfo->{'to_id'} eq ('0' x 40);
3463 # does patch correspond to [previous] difftree raw line
3464 # $diffinfo - hashref of parsed raw diff format
3465 # $patchinfo - hashref of parsed patch diff format
3466 # (the same keys as in $diffinfo)
3467 sub is_patch_split {
3468 my ($diffinfo, $patchinfo) = @_;
3470 return defined $diffinfo && defined $patchinfo
3471 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3475 sub git_difftree_body {
3476 my ($difftree, $hash, @parents) = @_;
3477 my ($parent) = $parents[0];
3478 my ($have_blame) = gitweb_check_feature('blame');
3479 print "<div class=\"list_head\">\n";
3480 if ($#{$difftree} > 10) {
3481 print(($#{$difftree} + 1) . " files changed:\n");
3483 print "</div>\n";
3485 print "<table class=\"" .
3486 (@parents > 1 ? "combined " : "") .
3487 "diff_tree\">\n";
3489 # header only for combined diff in 'commitdiff' view
3490 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3491 if ($has_header) {
3492 # table header
3493 print "<thead><tr>\n" .
3494 "<th></th><th></th>\n"; # filename, patchN link
3495 for (my $i = 0; $i < @parents; $i++) {
3496 my $par = $parents[$i];
3497 print "<th>" .
3498 $cgi->a({-href => href(action=>"commitdiff",
3499 hash=>$hash, hash_parent=>$par),
3500 -title => 'commitdiff to parent number ' .
3501 ($i+1) . ': ' . substr($par,0,7)},
3502 $i+1) .
3503 "&nbsp;</th>\n";
3505 print "</tr></thead>\n<tbody>\n";
3508 my $alternate = 1;
3509 my $patchno = 0;
3510 foreach my $line (@{$difftree}) {
3511 my $diff = parsed_difftree_line($line);
3513 if ($alternate) {
3514 print "<tr class=\"dark\">\n";
3515 } else {
3516 print "<tr class=\"light\">\n";
3518 $alternate ^= 1;
3520 if (exists $diff->{'nparents'}) { # combined diff
3522 fill_from_file_info($diff, @parents)
3523 unless exists $diff->{'from_file'};
3525 if (!is_deleted($diff)) {
3526 # file exists in the result (child) commit
3527 print "<td>" .
3528 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3529 file_name=>$diff->{'to_file'},
3530 hash_base=>$hash),
3531 -class => "list"}, esc_path($diff->{'to_file'})) .
3532 "</td>\n";
3533 } else {
3534 print "<td>" .
3535 esc_path($diff->{'to_file'}) .
3536 "</td>\n";
3539 if ($action eq 'commitdiff') {
3540 # link to patch
3541 $patchno++;
3542 print "<td class=\"link\">" .
3543 $cgi->a({-href => "#patch$patchno"}, "patch") .
3544 " | " .
3545 "</td>\n";
3548 my $has_history = 0;
3549 my $not_deleted = 0;
3550 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3551 my $hash_parent = $parents[$i];
3552 my $from_hash = $diff->{'from_id'}[$i];
3553 my $from_path = $diff->{'from_file'}[$i];
3554 my $status = $diff->{'status'}[$i];
3556 $has_history ||= ($status ne 'A');
3557 $not_deleted ||= ($status ne 'D');
3559 if ($status eq 'A') {
3560 print "<td class=\"link\" align=\"right\"> | </td>\n";
3561 } elsif ($status eq 'D') {
3562 print "<td class=\"link\">" .
3563 $cgi->a({-href => href(action=>"blob",
3564 hash_base=>$hash,
3565 hash=>$from_hash,
3566 file_name=>$from_path)},
3567 "blob" . ($i+1)) .
3568 " | </td>\n";
3569 } else {
3570 if ($diff->{'to_id'} eq $from_hash) {
3571 print "<td class=\"link nochange\">";
3572 } else {
3573 print "<td class=\"link\">";
3575 print $cgi->a({-href => href(action=>"blobdiff",
3576 hash=>$diff->{'to_id'},
3577 hash_parent=>$from_hash,
3578 hash_base=>$hash,
3579 hash_parent_base=>$hash_parent,
3580 file_name=>$diff->{'to_file'},
3581 file_parent=>$from_path)},
3582 "diff" . ($i+1)) .
3583 " | </td>\n";
3587 print "<td class=\"link\">";
3588 if ($not_deleted) {
3589 print $cgi->a({-href => href(action=>"blob",
3590 hash=>$diff->{'to_id'},
3591 file_name=>$diff->{'to_file'},
3592 hash_base=>$hash)},
3593 "blob");
3594 print " | " if ($has_history);
3596 if ($has_history) {
3597 print $cgi->a({-href => href(action=>"history",
3598 file_name=>$diff->{'to_file'},
3599 hash_base=>$hash)},
3600 "history");
3602 print "</td>\n";
3604 print "</tr>\n";
3605 next; # instead of 'else' clause, to avoid extra indent
3607 # else ordinary diff
3609 my ($to_mode_oct, $to_mode_str, $to_file_type);
3610 my ($from_mode_oct, $from_mode_str, $from_file_type);
3611 if ($diff->{'to_mode'} ne ('0' x 6)) {
3612 $to_mode_oct = oct $diff->{'to_mode'};
3613 if (S_ISREG($to_mode_oct)) { # only for regular file
3614 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3616 $to_file_type = file_type($diff->{'to_mode'});
3618 if ($diff->{'from_mode'} ne ('0' x 6)) {
3619 $from_mode_oct = oct $diff->{'from_mode'};
3620 if (S_ISREG($to_mode_oct)) { # only for regular file
3621 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3623 $from_file_type = file_type($diff->{'from_mode'});
3626 if ($diff->{'status'} eq "A") { # created
3627 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3628 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3629 $mode_chng .= "]</span>";
3630 print "<td>";
3631 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3632 hash_base=>$hash, file_name=>$diff->{'file'}),
3633 -class => "list"}, esc_path($diff->{'file'}));
3634 print "</td>\n";
3635 print "<td>$mode_chng</td>\n";
3636 print "<td class=\"link\">";
3637 if ($action eq 'commitdiff') {
3638 # link to patch
3639 $patchno++;
3640 print $cgi->a({-href => "#patch$patchno"}, "patch");
3641 print " | ";
3643 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3644 hash_base=>$hash, file_name=>$diff->{'file'})},
3645 "blob");
3646 print "</td>\n";
3648 } elsif ($diff->{'status'} eq "D") { # deleted
3649 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3650 print "<td>";
3651 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3652 hash_base=>$parent, file_name=>$diff->{'file'}),
3653 -class => "list"}, esc_path($diff->{'file'}));
3654 print "</td>\n";
3655 print "<td>$mode_chng</td>\n";
3656 print "<td class=\"link\">";
3657 if ($action eq 'commitdiff') {
3658 # link to patch
3659 $patchno++;
3660 print $cgi->a({-href => "#patch$patchno"}, "patch");
3661 print " | ";
3663 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3664 hash_base=>$parent, file_name=>$diff->{'file'})},
3665 "blob") . " | ";
3666 if ($have_blame) {
3667 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3668 file_name=>$diff->{'file'})},
3669 "blame") . " | ";
3671 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3672 file_name=>$diff->{'file'})},
3673 "history");
3674 print "</td>\n";
3676 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3677 my $mode_chnge = "";
3678 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3679 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3680 if ($from_file_type ne $to_file_type) {
3681 $mode_chnge .= " from $from_file_type to $to_file_type";
3683 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3684 if ($from_mode_str && $to_mode_str) {
3685 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3686 } elsif ($to_mode_str) {
3687 $mode_chnge .= " mode: $to_mode_str";
3690 $mode_chnge .= "]</span>\n";
3692 print "<td>";
3693 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3694 hash_base=>$hash, file_name=>$diff->{'file'}),
3695 -class => "list"}, esc_path($diff->{'file'}));
3696 print "</td>\n";
3697 print "<td>$mode_chnge</td>\n";
3698 print "<td class=\"link\">";
3699 if ($action eq 'commitdiff') {
3700 # link to patch
3701 $patchno++;
3702 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3703 " | ";
3704 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3705 # "commit" view and modified file (not onlu mode changed)
3706 print $cgi->a({-href => href(action=>"blobdiff",
3707 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3708 hash_base=>$hash, hash_parent_base=>$parent,
3709 file_name=>$diff->{'file'})},
3710 "diff") .
3711 " | ";
3713 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3714 hash_base=>$hash, file_name=>$diff->{'file'})},
3715 "blob") . " | ";
3716 if ($have_blame) {
3717 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3718 file_name=>$diff->{'file'})},
3719 "blame") . " | ";
3721 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3722 file_name=>$diff->{'file'})},
3723 "history");
3724 print "</td>\n";
3726 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3727 my %status_name = ('R' => 'moved', 'C' => 'copied');
3728 my $nstatus = $status_name{$diff->{'status'}};
3729 my $mode_chng = "";
3730 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3731 # mode also for directories, so we cannot use $to_mode_str
3732 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3734 print "<td>" .
3735 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3736 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3737 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3738 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3739 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3740 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3741 -class => "list"}, esc_path($diff->{'from_file'})) .
3742 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3743 "<td class=\"link\">";
3744 if ($action eq 'commitdiff') {
3745 # link to patch
3746 $patchno++;
3747 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3748 " | ";
3749 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3750 # "commit" view and modified file (not only pure rename or copy)
3751 print $cgi->a({-href => href(action=>"blobdiff",
3752 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3753 hash_base=>$hash, hash_parent_base=>$parent,
3754 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3755 "diff") .
3756 " | ";
3758 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3759 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3760 "blob") . " | ";
3761 if ($have_blame) {
3762 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3763 file_name=>$diff->{'to_file'})},
3764 "blame") . " | ";
3766 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3767 file_name=>$diff->{'to_file'})},
3768 "history");
3769 print "</td>\n";
3771 } # we should not encounter Unmerged (U) or Unknown (X) status
3772 print "</tr>\n";
3774 print "</tbody>" if $has_header;
3775 print "</table>\n";
3778 sub git_patchset_body {
3779 my ($read, $difftree, $hash, @hash_parents) = @_;
3780 my ($hash_parent) = $hash_parents[0];
3782 my $is_combined = (@hash_parents > 1);
3783 my $patch_idx = 0;
3784 my $patch_number = 0;
3785 my $patch_line;
3786 my $diffinfo;
3787 my $to_name;
3788 my (%from, %to);
3790 print "<div class=\"patchset\">\n";
3792 # skip to first patch
3793 while ($patch_line = $read->()) {
3794 chomp $patch_line;
3796 last if ($patch_line =~ m/^diff /);
3799 PATCH:
3800 while ($patch_line) {
3802 # parse "git diff" header line
3803 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3804 # $1 is from_name, which we do not use
3805 $to_name = unquote($2);
3806 $to_name =~ s!^b/!!;
3807 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3808 # $1 is 'cc' or 'combined', which we do not use
3809 $to_name = unquote($2);
3810 } else {
3811 $to_name = undef;
3814 # check if current patch belong to current raw line
3815 # and parse raw git-diff line if needed
3816 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3817 # this is continuation of a split patch
3818 print "<div class=\"patch cont\">\n";
3819 } else {
3820 # advance raw git-diff output if needed
3821 $patch_idx++ if defined $diffinfo;
3823 # read and prepare patch information
3824 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3826 # compact combined diff output can have some patches skipped
3827 # find which patch (using pathname of result) we are at now;
3828 if ($is_combined) {
3829 while ($to_name ne $diffinfo->{'to_file'}) {
3830 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3831 format_diff_cc_simplified($diffinfo, @hash_parents) .
3832 "</div>\n"; # class="patch"
3834 $patch_idx++;
3835 $patch_number++;
3837 last if $patch_idx > $#$difftree;
3838 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3842 # modifies %from, %to hashes
3843 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3845 # this is first patch for raw difftree line with $patch_idx index
3846 # we index @$difftree array from 0, but number patches from 1
3847 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3850 # git diff header
3851 #assert($patch_line =~ m/^diff /) if DEBUG;
3852 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3853 $patch_number++;
3854 # print "git diff" header
3855 print format_git_diff_header_line($patch_line, $diffinfo,
3856 \%from, \%to);
3858 # print extended diff header
3859 print "<div class=\"diff extended_header\">\n";
3860 EXTENDED_HEADER:
3861 while ($patch_line = $read->()) {
3862 chomp $patch_line;
3864 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3866 print format_extended_diff_header_line($patch_line, $diffinfo,
3867 \%from, \%to);
3869 print "</div>\n"; # class="diff extended_header"
3871 # from-file/to-file diff header
3872 if (! $patch_line) {
3873 print "</div>\n"; # class="patch"
3874 last PATCH;
3876 next PATCH if ($patch_line =~ m/^diff /);
3877 #assert($patch_line =~ m/^---/) if DEBUG;
3879 my $last_patch_line = $patch_line;
3880 $patch_line = $read->();
3881 chomp $patch_line;
3882 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3884 print format_diff_from_to_header($last_patch_line, $patch_line,
3885 $diffinfo, \%from, \%to,
3886 @hash_parents);
3888 # the patch itself
3889 LINE:
3890 while ($patch_line = $read->()) {
3891 chomp $patch_line;
3893 next PATCH if ($patch_line =~ m/^diff /);
3895 print format_diff_line($patch_line, \%from, \%to);
3898 } continue {
3899 print "</div>\n"; # class="patch"
3902 # for compact combined (--cc) format, with chunk and patch simpliciaction
3903 # patchset might be empty, but there might be unprocessed raw lines
3904 for (++$patch_idx if $patch_number > 0;
3905 $patch_idx < @$difftree;
3906 ++$patch_idx) {
3907 # read and prepare patch information
3908 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3910 # generate anchor for "patch" links in difftree / whatchanged part
3911 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3912 format_diff_cc_simplified($diffinfo, @hash_parents) .
3913 "</div>\n"; # class="patch"
3915 $patch_number++;
3918 if ($patch_number == 0) {
3919 if (@hash_parents > 1) {
3920 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3921 } else {
3922 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3926 print "</div>\n"; # class="patchset"
3929 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3931 # fills project list info (age, description, owner, forks) for each
3932 # project in the list, removing invalid projects from returned list
3933 # NOTE: modifies $projlist, but does not remove entries from it
3934 sub fill_project_list_info {
3935 my ($projlist, $check_forks) = @_;
3936 my @projects;
3938 PROJECT:
3939 foreach my $pr (@$projlist) {
3940 my (@activity) = git_get_last_activity($pr->{'path'});
3941 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3942 if (!defined $pr->{'descr'}) {
3943 my $descr = git_get_project_description($pr->{'path'}) || "";
3944 $descr = to_utf8($descr);
3945 $pr->{'descr_long'} = $descr;
3946 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3948 if (!defined $pr->{'owner'}) {
3949 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3951 if ($check_forks) {
3952 my $pname = $pr->{'path'};
3953 if (($pname =~ s/\.git$//) &&
3954 ($pname !~ /\/$/) &&
3955 (-d "$projectroot/$pname")) {
3956 $pr->{'forks'} = "-d $projectroot/$pname";
3957 } else {
3958 $pr->{'forks'} = 0;
3961 push @projects, $pr;
3964 return @projects;
3967 # print 'sort by' <th> element, either sorting by $key if $name eq $order
3968 # (changing $list), or generating 'sort by $name' replay link otherwise
3969 sub print_sort_th {
3970 my ($str_sort, $name, $order, $key, $header, $list) = @_;
3971 $key ||= $name;
3972 $header ||= ucfirst($name);
3974 if ($order eq $name) {
3975 if ($str_sort) {
3976 @$list = sort {$a->{$key} cmp $b->{$key}} @$list;
3977 } else {
3978 # Sort undefined keys last.
3979 @$list = sort { (defined $a->{$key} ? $a->{$key} : 1e30) <=>
3980 (defined $b->{$key} ? $b->{$key} : 1e30)} @$list;
3982 print "<th>$header</th>\n";
3983 } else {
3984 print "<th>" .
3985 $cgi->a({-href => href(-replay=>1, order=>$name),
3986 -class => "header"}, $header) .
3987 "</th>\n";
3991 sub print_sort_th_str {
3992 print_sort_th(1, @_);
3995 sub print_sort_th_num {
3996 print_sort_th(0, @_);
3999 sub git_project_list_body {
4000 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4002 my ($check_forks) = gitweb_check_feature('forks');
4003 my @projects = fill_project_list_info($projlist, $check_forks);
4005 $order ||= $default_projects_order;
4006 $from = 0 unless defined $from;
4007 $to = $#projects if (!defined $to || $#projects < $to);
4009 print "<table class=\"project_list\">\n";
4010 unless ($no_header) {
4011 print "<tr>\n";
4012 if ($check_forks) {
4013 print "<th></th>\n";
4015 print_sort_th_str('project', $order, 'path',
4016 'Project', \@projects);
4017 print_sort_th_str('descr', $order, 'descr_long',
4018 'Description', \@projects);
4019 print_sort_th_str('owner', $order, 'owner',
4020 'Owner', \@projects);
4021 print_sort_th_num('age', $order, 'age',
4022 'Last Change', \@projects);
4023 print "<th></th>\n" . # for links
4024 "</tr>\n";
4026 my $alternate = 1;
4027 for (my $i = $from; $i <= $to; $i++) {
4028 my $pr = $projects[$i];
4029 if ($alternate) {
4030 print "<tr class=\"dark\">\n";
4031 } else {
4032 print "<tr class=\"light\">\n";
4034 $alternate ^= 1;
4035 if ($check_forks) {
4036 print "<td>";
4037 if ($pr->{'forks'}) {
4038 print "<!-- $pr->{'forks'} -->\n";
4039 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4041 print "</td>\n";
4043 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4044 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4045 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4046 -class => "list", -title => $pr->{'descr_long'}},
4047 esc_html($pr->{'descr'})) . "</td>\n" .
4048 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n" .
4049 "<td class=\"". age_class($pr->{'age'}) . "\">" .
4050 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4051 "<td class=\"link\">" .
4052 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary");
4053 if ($pr->{'age_string'}) {
4054 # Non-empty repository.
4055 print " | " .
4056 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
4057 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4058 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree");
4060 print " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") if $pr->{'forks'};
4061 print "</td>\n" .
4062 "</tr>\n";
4064 if (defined $extra) {
4065 print "<tr>\n";
4066 if ($check_forks) {
4067 print "<td></td>\n";
4069 print "<td colspan=\"5\">$extra</td>\n" .
4070 "</tr>\n";
4072 print "</table>\n";
4075 sub git_shortlog_body {
4076 # uses global variable $project
4077 my ($commitlist, $from, $to, $refs, $extra) = @_;
4079 $from = 0 unless defined $from;
4080 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4082 print "<table class=\"shortlog\">\n";
4083 my $alternate = 1;
4084 for (my $i = $from; $i <= $to; $i++) {
4085 my %co = %{$commitlist->[$i]};
4086 my $commit = $co{'id'};
4087 my $ref = format_ref_marker($refs, $commit);
4088 if ($alternate) {
4089 print "<tr class=\"dark\">\n";
4090 } else {
4091 print "<tr class=\"light\">\n";
4093 $alternate ^= 1;
4094 my $author = chop_and_escape_str($co{'author_name'}, 10);
4095 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4096 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4097 "<td><i>" . $author . "</i></td>\n" .
4098 "<td>";
4099 print format_subject_html($co{'title'}, $co{'title_short'},
4100 href(action=>"commit", hash=>$commit), $ref);
4101 print "</td>\n" .
4102 "<td class=\"link\">" .
4103 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4104 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4105 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4106 my $snapshot_links = format_snapshot_links($commit);
4107 if (defined $snapshot_links) {
4108 print " | " . $snapshot_links;
4110 print "</td>\n" .
4111 "</tr>\n";
4113 if (defined $extra) {
4114 print "<tr>\n" .
4115 "<td colspan=\"4\">$extra</td>\n" .
4116 "</tr>\n";
4118 print "</table>\n";
4121 sub git_history_body {
4122 # Warning: assumes constant type (blob or tree) during history
4123 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
4125 $from = 0 unless defined $from;
4126 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4128 print "<table class=\"history\">\n";
4129 my $alternate = 1;
4130 for (my $i = $from; $i <= $to; $i++) {
4131 my %co = %{$commitlist->[$i]};
4132 if (!%co) {
4133 next;
4135 my $commit = $co{'id'};
4137 my $ref = format_ref_marker($refs, $commit);
4139 if ($alternate) {
4140 print "<tr class=\"dark\">\n";
4141 } else {
4142 print "<tr class=\"light\">\n";
4144 $alternate ^= 1;
4145 # shortlog uses chop_str($co{'author_name'}, 10)
4146 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
4147 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4148 "<td><i>" . $author . "</i></td>\n" .
4149 "<td>";
4150 # originally git_history used chop_str($co{'title'}, 50)
4151 print format_subject_html($co{'title'}, $co{'title_short'},
4152 href(action=>"commit", hash=>$commit), $ref);
4153 print "</td>\n" .
4154 "<td class=\"link\">" .
4155 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4156 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4158 if ($ftype eq 'blob') {
4159 my $blob_current = git_get_sha1_by_path($hash_base, $file_name);
4160 my $blob_parent = git_get_sha1_by_path($commit, $file_name);
4161 if (defined $blob_current && defined $blob_parent &&
4162 $blob_current ne $blob_parent) {
4163 print " | " .
4164 $cgi->a({-href => href(action=>"blobdiff",
4165 hash=>$blob_current, hash_parent=>$blob_parent,
4166 hash_base=>$hash_base, hash_parent_base=>$commit,
4167 file_name=>$file_name)},
4168 "diff to current");
4171 print "</td>\n" .
4172 "</tr>\n";
4174 if (defined $extra) {
4175 print "<tr>\n" .
4176 "<td colspan=\"4\">$extra</td>\n" .
4177 "</tr>\n";
4179 print "</table>\n";
4182 sub git_tags_body {
4183 # uses global variable $project
4184 my ($taglist, $from, $to, $extra) = @_;
4185 $from = 0 unless defined $from;
4186 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4188 print "<table class=\"tags\">\n";
4189 my $alternate = 1;
4190 for (my $i = $from; $i <= $to; $i++) {
4191 my $entry = $taglist->[$i];
4192 my %tag = %$entry;
4193 my $comment = $tag{'subject'};
4194 my $comment_short;
4195 if (defined $comment) {
4196 $comment_short = chop_str($comment, 30, 5);
4198 if ($alternate) {
4199 print "<tr class=\"dark\">\n";
4200 } else {
4201 print "<tr class=\"light\">\n";
4203 $alternate ^= 1;
4204 if (defined $tag{'age'}) {
4205 print "<td><i>$tag{'age'}</i></td>\n";
4206 } else {
4207 print "<td></td>\n";
4209 print "<td>" .
4210 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4211 -class => "list name"}, esc_html($tag{'name'})) .
4212 "</td>\n" .
4213 "<td>";
4214 if (defined $comment) {
4215 print format_subject_html($comment, $comment_short,
4216 href(action=>"tag", hash=>$tag{'id'}));
4218 print "</td>\n" .
4219 "<td class=\"selflink\">";
4220 if ($tag{'type'} eq "tag") {
4221 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4222 } else {
4223 print "&nbsp;";
4225 print "</td>\n" .
4226 "<td class=\"link\">" . " | " .
4227 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4228 if ($tag{'reftype'} eq "commit") {
4229 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4230 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
4231 } elsif ($tag{'reftype'} eq "blob") {
4232 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4234 print "</td>\n" .
4235 "</tr>";
4237 if (defined $extra) {
4238 print "<tr>\n" .
4239 "<td colspan=\"5\">$extra</td>\n" .
4240 "</tr>\n";
4242 print "</table>\n";
4245 sub git_heads_body {
4246 # uses global variable $project
4247 my ($headlist, $head, $from, $to, $extra) = @_;
4248 $from = 0 unless defined $from;
4249 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4251 print "<table class=\"heads\">\n";
4252 my $alternate = 1;
4253 for (my $i = $from; $i <= $to; $i++) {
4254 my $entry = $headlist->[$i];
4255 my %ref = %$entry;
4256 my $curr = $ref{'id'} eq $head;
4257 if ($alternate) {
4258 print "<tr class=\"dark\">\n";
4259 } else {
4260 print "<tr class=\"light\">\n";
4262 $alternate ^= 1;
4263 print "<td><i>$ref{'age'}</i></td>\n" .
4264 ($curr ? "<td class=\"current_head\">" : "<td>") .
4265 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4266 -class => "list name"},esc_html($ref{'name'})) .
4267 "</td>\n" .
4268 "<td class=\"link\">" .
4269 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4270 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4271 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4272 "</td>\n" .
4273 "</tr>";
4275 if (defined $extra) {
4276 print "<tr>\n" .
4277 "<td colspan=\"3\">$extra</td>\n" .
4278 "</tr>\n";
4280 print "</table>\n";
4283 sub git_search_grep_body {
4284 my ($commitlist, $from, $to, $extra) = @_;
4285 $from = 0 unless defined $from;
4286 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4288 print "<table class=\"commit_search\">\n";
4289 my $alternate = 1;
4290 for (my $i = $from; $i <= $to; $i++) {
4291 my %co = %{$commitlist->[$i]};
4292 if (!%co) {
4293 next;
4295 my $commit = $co{'id'};
4296 if ($alternate) {
4297 print "<tr class=\"dark\">\n";
4298 } else {
4299 print "<tr class=\"light\">\n";
4301 $alternate ^= 1;
4302 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
4303 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4304 "<td><i>" . $author . "</i></td>\n" .
4305 "<td>" .
4306 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4307 -class => "list subject"},
4308 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4309 my $comment = $co{'comment'};
4310 foreach my $line (@$comment) {
4311 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4312 my ($lead, $match, $trail) = ($1, $2, $3);
4313 $match = chop_str($match, 70, 5, 'center');
4314 my $contextlen = int((80 - length($match))/2);
4315 $contextlen = 30 if ($contextlen > 30);
4316 $lead = chop_str($lead, $contextlen, 10, 'left');
4317 $trail = chop_str($trail, $contextlen, 10, 'right');
4319 $lead = esc_html($lead);
4320 $match = esc_html($match);
4321 $trail = esc_html($trail);
4323 print "$lead<span class=\"match\">$match</span>$trail<br />";
4326 print "</td>\n" .
4327 "<td class=\"link\">" .
4328 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4329 " | " .
4330 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4331 " | " .
4332 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4333 print "</td>\n" .
4334 "</tr>\n";
4336 if (defined $extra) {
4337 print "<tr>\n" .
4338 "<td colspan=\"3\">$extra</td>\n" .
4339 "</tr>\n";
4341 print "</table>\n";
4344 ## ======================================================================
4345 ## ======================================================================
4346 ## actions
4348 sub git_project_list {
4349 my $order = $cgi->param('o');
4350 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4351 die_error(400, "Unknown order parameter");
4354 my @list = git_get_projects_list();
4355 if (!@list) {
4356 die_error(404, "No projects found");
4359 git_header_html();
4360 if (-f $home_text) {
4361 print "<div class=\"index_include\">\n";
4362 open (my $fd, $home_text);
4363 print <$fd>;
4364 close $fd;
4365 print "</div>\n";
4367 git_project_list_body(\@list, $order);
4368 git_footer_html();
4371 sub git_forks {
4372 my $order = $cgi->param('o');
4373 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4374 die_error(400, "Unknown order parameter");
4377 my @list = git_get_projects_list($project);
4378 if (!@list) {
4379 die_error(404, "No forks found");
4382 git_header_html();
4383 git_print_page_nav('','');
4384 git_print_header_div('summary', "$project forks");
4385 git_project_list_body(\@list, $order);
4386 git_footer_html();
4389 sub git_project_index {
4390 my @projects = git_get_projects_list($project);
4392 print $cgi->header(
4393 -type => 'text/plain',
4394 -charset => 'utf-8',
4395 -content_disposition => 'inline; filename="index.aux"');
4397 foreach my $pr (@projects) {
4398 if (!exists $pr->{'owner'}) {
4399 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4402 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4403 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4404 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4405 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4406 $path =~ s/ /\+/g;
4407 $owner =~ s/ /\+/g;
4409 print "$path $owner\n";
4413 sub git_summary {
4414 my $descr = git_get_project_description($project) || "none";
4415 my $head = $repo->get_sha1('HEAD', 'commit');
4416 my %co = parse_commit($head) if $head;
4417 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'}) if $head;
4419 my $owner = git_get_project_owner($project);
4421 my $refs = git_get_references();
4422 # These get_*_list functions return one more to allow us to see if
4423 # there are more ...
4424 my @taglist = git_get_tags_list(16);
4425 my @headlist = git_get_heads_list(16);
4426 my @forklist;
4427 my ($check_forks) = gitweb_check_feature('forks');
4429 if ($check_forks) {
4430 @forklist = git_get_projects_list($project);
4433 git_header_html();
4434 git_print_page_nav('summary','', $head);
4436 print "<div class=\"title\">&nbsp;</div>\n" if $head;
4437 print "<table class=\"projects_list\">\n" .
4438 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4439 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4440 if (defined $cd{'rfc2822'}) {
4441 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4444 # use per project git URL list in $projectroot/$project/cloneurl
4445 # or make project git URL from git base URL and project name
4446 my $url_tag = "URL";
4447 my @url_list = git_get_project_url_list($project);
4448 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4449 foreach my $git_url (@url_list) {
4450 next unless $git_url;
4451 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
4452 $url_tag = "";
4454 print "</table>\n";
4456 if (-s "$projectroot/$project/README.html") {
4457 if (open my $fd, "$projectroot/$project/README.html") {
4458 print "<div class=\"title\">readme</div>\n" .
4459 "<div class=\"readme\">\n";
4460 print $_ while (<$fd>);
4461 print "\n</div>\n"; # class="readme"
4462 close $fd;
4466 # we need to request one more than 16 (0..15) to check if
4467 # those 16 are all
4468 my @commitlist = $head ? parse_commits($head, 17) : ();
4469 if (@commitlist) {
4470 git_print_header_div('shortlog');
4471 git_shortlog_body(\@commitlist, 0, 15, $refs,
4472 $#commitlist <= 15 ? undef :
4473 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4476 if (@taglist) {
4477 git_print_header_div('tags');
4478 git_tags_body(\@taglist, 0, 15,
4479 $#taglist <= 15 ? undef :
4480 $cgi->a({-href => href(action=>"tags")}, "..."));
4483 if (@headlist) {
4484 git_print_header_div('heads');
4485 git_heads_body(\@headlist, $head, 0, 15,
4486 $#headlist <= 15 ? undef :
4487 $cgi->a({-href => href(action=>"heads")}, "..."));
4490 if (@forklist) {
4491 git_print_header_div('forks');
4492 git_project_list_body(\@forklist, undef, 0, 15,
4493 $#forklist <= 15 ? undef :
4494 $cgi->a({-href => href(action=>"forks")}, "..."),
4495 'noheader');
4498 git_footer_html();
4501 sub git_tag {
4502 my $head = git_get_head_hash();
4503 my $sha1 = git_get_sha1_or_die($hash, 'tag');
4504 git_header_html();
4505 git_print_page_nav('','', $head,undef,$head);
4506 # TODO: This wants to become $repo->get_tag.
4507 my %tag = parse_tag($sha1);
4509 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4510 print "<div class=\"title_text\">\n" .
4511 "<table class=\"object_header\">\n" .
4512 "<tr>\n" .
4513 "<td>object</td>\n" .
4514 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4515 $tag{'object'}) . "</td>\n" .
4516 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4517 $tag{'type'}) . "</td>\n" .
4518 "</tr>\n";
4519 if (defined($tag{'author'})) {
4520 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4521 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4522 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4523 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4524 "</td></tr>\n";
4526 print "</table>\n\n" .
4527 "</div>\n";
4528 print "<div class=\"page_body\">";
4529 my $comment = $tag{'comment'};
4530 foreach my $line (@$comment) {
4531 print esc_html($line, -nbsp=>1) . "<br/>\n";
4533 print "</div>\n";
4534 git_footer_html();
4537 sub git_blame {
4538 my $fd;
4539 my $ftype;
4541 die_error(403, "Blame view not allowed")
4542 unless gitweb_check_feature('blame');
4544 die_error(400, "No file name given") unless $file_name;
4545 $hash_base ||= git_get_head_hash();
4546 my $hash_base_sha1 = git_get_sha1_or_die($hash_base, 'commit');
4547 my %co = parse_commit($hash_base);
4548 $hash ||= git_get_sha1_by_path($hash_base, $file_name, 'blob')
4549 or die_error(404, "Error looking up file: '$file_name'");
4550 git_get_sha1_or_die($hash, 'blob'); # check existence
4551 my @blame_lines = split "\n", $repo->cmd_output(
4552 cmd => ['blame', '-p', '--', $file_name, $hash_base_sha1],
4553 cache => 2);
4554 git_header_html();
4555 my $formats_nav =
4556 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4557 "blob") .
4558 " | " .
4559 $cgi->a({-href => href(action=>"history", -replay=>1)},
4560 "history") .
4561 " | " .
4562 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4563 "HEAD");
4564 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4565 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4566 git_print_page_path($file_name, $ftype, $hash_base);
4567 my @rev_color = (qw(light2 dark2));
4568 my $num_colors = scalar(@rev_color);
4569 my $current_color = 0;
4570 my $last_rev;
4571 print <<HTML;
4572 <div class="page_body">
4573 <table class="blame">
4574 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4575 HTML
4576 my %metainfo = ();
4577 while (1) {
4578 $_ = shift @blame_lines;
4579 last unless defined $_;
4580 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4581 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4582 if (!exists $metainfo{$full_rev}) {
4583 $metainfo{$full_rev} = {};
4585 my $meta = $metainfo{$full_rev};
4586 while ($_ = shift @blame_lines) {
4587 last if (s/^\t//);
4588 if (/^(\S+) (.*)$/) {
4589 $meta->{$1} = $2;
4592 my $data = $_;
4593 my $rev = substr($full_rev, 0, 8);
4594 my $author = $meta->{'author'};
4595 my %date = parse_date($meta->{'author-time'},
4596 $meta->{'author-tz'});
4597 my $date = $date{'iso-tz'};
4598 if ($group_size) {
4599 $current_color = ++$current_color % $num_colors;
4601 print "<tr class=\"$rev_color[$current_color]\">\n";
4602 if ($group_size) {
4603 print "<td class=\"sha1\"";
4604 print " title=\"". esc_html($author) . ", $date\"";
4605 print " rowspan=\"$group_size\"" if ($group_size > 1);
4606 print ">";
4607 print $cgi->a({-href => href(action=>"commit",
4608 hash=>$full_rev,
4609 file_name=>$file_name)},
4610 esc_html($rev));
4611 print "</td>\n";
4613 # TODO: $parent_commit can be undef, in which case the
4614 # link becomes invalid.
4615 my $parent_commit = $repo->get_sha1("$full_rev^");
4616 my $blamed = href(action => 'blame',
4617 file_name => $meta->{'filename'},
4618 hash_base => $parent_commit);
4619 print "<td class=\"linenr\">";
4620 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4621 -id => "l$lineno",
4622 -class => "linenr" },
4623 esc_html($lineno));
4624 print "</td>";
4625 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4626 print "</tr>\n";
4628 print "</table>\n";
4629 print "</div>";
4630 git_footer_html();
4633 sub git_tags {
4634 my $head = git_get_head_hash();
4635 git_header_html();
4636 git_print_page_nav('','', $head,undef,$head);
4637 git_print_header_div('summary', $project);
4639 my @tagslist = git_get_tags_list();
4640 if (@tagslist) {
4641 git_tags_body(\@tagslist);
4643 git_footer_html();
4646 sub git_heads {
4647 my $head = git_get_head_hash();
4648 git_header_html();
4649 git_print_page_nav('','', $head,undef,$head);
4650 git_print_header_div('summary', $project);
4652 my @headslist = git_get_heads_list();
4653 if (@headslist) {
4654 git_heads_body(\@headslist, $head);
4656 git_footer_html();
4659 sub git_blob_plain {
4660 my $type = shift;
4661 my $expires;
4663 if (!defined $hash) {
4664 if (defined $file_name) {
4665 my $base = $hash_base || git_get_head_hash();
4666 $hash = git_get_sha1_by_path($base, $file_name, 'blob')
4667 or die_error(404, "Cannot find file: '$file_name'");
4668 } else {
4669 die_error(400, "No file name defined");
4671 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4672 # blobs defined by non-textual hash id's can be cached
4673 $expires = "+1d";
4676 my $sha1 = git_get_sha1_or_die($hash, 'blob');
4677 my $blob_read = $repo->progressive_cmd_output(
4678 cmd => ['cat-file', 'blob', $sha1], separator => undef,
4679 cache => 1);
4680 my $first_chunk = $blob_read->() || '';
4682 # content-type (can include charset)
4683 $type = blob_contenttype(substr($first_chunk, 0, 1024),
4684 $file_name, $type);
4686 # "save as" filename, even when no $file_name is given
4687 my $save_as = "$hash";
4688 if (defined $file_name) {
4689 $save_as = $file_name;
4690 } elsif ($type =~ m/^text\//) {
4691 $save_as .= '.txt';
4694 print $cgi->header(
4695 -type => $type,
4696 -expires => $expires,
4697 -content_disposition => 'inline; filename="' . $save_as . '"');
4699 local $/;
4700 binmode STDOUT, ':raw';
4701 print $first_chunk;
4702 while (my $chunk = $blob_read->()) {
4703 print $chunk;
4705 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4709 sub git_blob {
4710 my $expires;
4712 if (!defined $hash) {
4713 if (defined $file_name) {
4714 my $base = $hash_base || git_get_head_hash();
4715 $hash = git_get_sha1_by_path($base, $file_name, 'blob')
4716 or die_error(404, "Cannot find file: '$file_name'");
4717 } else {
4718 die_error(400, "No file name defined");
4720 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4721 # blobs defined by non-textual hash id's can be cached
4722 $expires = "+1d";
4725 my ($have_blame) = gitweb_check_feature('blame');
4726 my $sha1 = git_get_sha1_or_die($hash, 'blob');
4727 my $blob_read = $repo->progressive_cmd_output(
4728 cmd => ['cat-file', 'blob', $sha1], separator => "\n",
4729 cache => 1);
4730 my @first_lines;
4731 for my $i (0..20) {
4732 my $line = $blob_read->() or last;
4733 push @first_lines, $line;
4735 my $test_snippet = join("\n", @first_lines);
4736 my $mimetype = blob_mimetype($test_snippet, $file_name);
4737 return git_blob_plain($mimetype)
4738 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! &&
4739 ! is_ascii_text($test_snippet));
4740 # we can have blame only for text/* mimetype
4741 $have_blame &&= ($mimetype =~ m!^text/!);
4743 git_header_html(undef, $expires);
4744 my $formats_nav = '';
4745 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4746 if (defined $file_name) {
4747 if ($have_blame) {
4748 $formats_nav .=
4749 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4750 "blame") .
4751 " | ";
4753 $formats_nav .=
4754 $cgi->a({-href => href(action=>"history", -replay=>1)},
4755 "history") .
4756 " | " .
4757 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4758 "raw") .
4759 " | " .
4760 $cgi->a({-href => href(action=>"blob",
4761 hash_base=>"HEAD", file_name=>$file_name)},
4762 "HEAD");
4763 } else {
4764 $formats_nav .=
4765 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4766 "raw");
4768 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4769 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4770 } else {
4771 print "<div class=\"page_nav\">\n" .
4772 "<br/><br/></div>\n" .
4773 "<div class=\"title\">$hash</div>\n";
4775 git_print_page_path($file_name, "blob", $hash_base);
4776 print "<div class=\"page_body\">\n";
4777 if ($mimetype =~ m!^image/!) {
4778 print qq!<img type="$mimetype"!;
4779 if ($file_name) {
4780 print qq! alt="$file_name" title="$file_name"!;
4782 print qq! src="! .
4783 href(action=>"blob_plain", hash=>$hash,
4784 hash_base=>$hash_base, file_name=>$file_name) .
4785 qq!" />\n!;
4786 } else {
4787 my $nr;
4788 while (my $line = shift @first_lines || $blob_read->()) {
4789 chomp $line;
4790 $nr++;
4791 $line = untabify($line);
4792 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4793 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4796 print "</div>";
4797 git_footer_html();
4800 sub git_tree {
4801 $hash_base ||= "HEAD";
4802 if (!defined $hash) {
4803 if (defined $file_name) {
4804 $hash = git_get_sha1_by_path($hash_base, $file_name, 'tree')
4805 or die_error(404, "Cannot find file: '$file_name'");
4806 } else {
4807 $hash = $hash_base;
4810 my $sha1 = git_get_sha1_or_die($hash, 'tree');
4811 my @entries = split "\0", $repo->cmd_output(
4812 cmd => ['ls-tree', '-z', $sha1], cache => 2);
4814 my $refs = git_get_references();
4815 my $ref = format_ref_marker($refs, $hash_base);
4816 git_header_html();
4817 my $basedir = '';
4818 my ($have_blame) = gitweb_check_feature('blame');
4819 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4820 my @views_nav = ();
4821 if (defined $file_name) {
4822 push @views_nav,
4823 $cgi->a({-href => href(action=>"history", -replay=>1)},
4824 "history"),
4825 $cgi->a({-href => href(action=>"tree",
4826 hash_base=>"HEAD", file_name=>$file_name)},
4827 "HEAD"),
4829 my $snapshot_links = format_snapshot_links($hash);
4830 if (defined $snapshot_links) {
4831 # FIXME: Should be available when we have no hash base as well.
4832 push @views_nav, $snapshot_links;
4834 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4835 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4836 } else {
4837 undef $hash_base;
4838 print "<div class=\"page_nav\">\n";
4839 print "<br/><br/></div>\n";
4840 print "<div class=\"title\">$hash</div>\n";
4842 if (defined $file_name) {
4843 $basedir = $file_name;
4844 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4845 $basedir .= '/';
4848 git_print_page_path($file_name, 'tree', $hash_base);
4849 print "<div class=\"page_body\">\n";
4850 print "<table class=\"tree\">\n";
4851 my $alternate = 1;
4852 # '..' (top directory) link if possible
4853 if (defined $hash_base &&
4854 defined $file_name && $file_name =~ m![^/]+$!) {
4855 if ($alternate) {
4856 print "<tr class=\"dark\">\n";
4857 } else {
4858 print "<tr class=\"light\">\n";
4860 $alternate ^= 1;
4862 my $up = $file_name;
4863 $up =~ s!/?[^/]+$!!;
4864 undef $up unless $up;
4865 # based on git_print_tree_entry
4866 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4867 print '<td class="list">';
4868 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4869 file_name=>$up)},
4870 "..");
4871 print "</td>\n";
4872 print "<td class=\"link\"></td>\n";
4874 print "</tr>\n";
4876 foreach my $line (@entries) {
4877 my %t = parse_ls_tree_line($line, -z => 1);
4879 if ($alternate) {
4880 print "<tr class=\"dark\">\n";
4881 } else {
4882 print "<tr class=\"light\">\n";
4884 $alternate ^= 1;
4886 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4888 print "</tr>\n";
4890 print "</table>\n" .
4891 "</div>";
4892 git_footer_html();
4895 sub git_snapshot {
4896 my @supported_fmts = gitweb_check_feature('snapshot');
4897 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4899 my $format = $cgi->param('sf');
4900 if (!@supported_fmts) {
4901 die_error(403, "Snapshots not allowed");
4903 # default to first supported snapshot format
4904 $format ||= $supported_fmts[0];
4905 if ($format !~ m/^[a-z0-9]+$/) {
4906 die_error(400, "Invalid snapshot format parameter");
4907 } elsif (!exists($known_snapshot_formats{$format})) {
4908 die_error(400, "Unknown snapshot format");
4909 } elsif (!grep($_ eq $format, @supported_fmts)) {
4910 die_error(403, "Unsupported snapshot format");
4913 $hash ||= git_get_head_hash();
4914 my $sha1 = git_get_sha1_or_die($hash);
4916 my $name = $project;
4917 $name =~ s,([^/])/*\.git$,$1,;
4918 $name = basename($name);
4919 my $filename = to_utf8($name);
4920 $name =~ s/\047/\047\\\047\047/g;
4921 my $cmd;
4922 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4923 $cmd = quote_command(
4924 'archive',
4925 "--format=$known_snapshot_formats{$format}{'format'}",
4926 "--prefix=$name/", $sha1);
4927 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4928 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
4931 print $cgi->header(
4932 -type => $known_snapshot_formats{$format}{'type'},
4933 -content_disposition => 'inline; filename="' . "$filename" . '"',
4934 -status => '200 OK');
4936 my $snapshot_read = $repo->progressive_cmd_output(
4937 cmd => $cmd, separator => undef, cache => 1);
4938 binmode STDOUT, ':raw';
4939 while (my $chunk = $snapshot_read->()) {
4940 print $chunk;
4942 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4945 sub git_log {
4946 my $head = git_get_head_hash();
4947 if (!defined $hash) {
4948 $hash = $head;
4950 if (!defined $page) {
4951 $page = 0;
4953 my $refs = git_get_references();
4955 my @commitlist = parse_commits($hash, 101, (100 * $page));
4957 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
4959 git_header_html();
4960 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4962 if (!@commitlist) {
4963 my %co = parse_commit($hash);
4965 git_print_header_div('summary', $project);
4966 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4968 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4969 for (my $i = 0; $i <= $to; $i++) {
4970 my %co = %{$commitlist[$i]};
4971 next if !%co;
4972 my $commit = $co{'id'};
4973 my $ref = format_ref_marker($refs, $commit);
4974 my %ad = parse_date($co{'author_epoch'});
4975 git_print_header_div('commit',
4976 "<span class=\"age\">$co{'age_string'}</span>" .
4977 esc_html($co{'title'}) . $ref,
4978 $commit);
4979 print "<div class=\"title_text\">\n" .
4980 "<div class=\"log_link\">\n" .
4981 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4982 " | " .
4983 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4984 " | " .
4985 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4986 "<br/>\n" .
4987 "</div>\n" .
4988 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4989 "</div>\n";
4991 print "<div class=\"log_body\">\n";
4992 git_print_log($co{'comment'}, -final_empty_line=> 1);
4993 print "</div>\n";
4995 if ($#commitlist >= 100) {
4996 print "<div class=\"page_nav\">\n";
4997 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4998 -accesskey => "n", -title => "Alt-n"}, "next");
4999 print "</div>\n";
5001 git_footer_html();
5004 sub git_commit {
5005 $hash ||= $hash_base || "HEAD";
5006 my $sha1 = git_get_sha1_or_die($hash, 'commit');
5007 my %co = parse_commit($hash);
5008 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5009 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
5011 my $parent = $co{'parent'};
5012 my $parents = $co{'parents'}; # listref
5014 # we need to prepare $formats_nav before any parameter munging
5015 my $formats_nav;
5016 if (!defined $parent) {
5017 # --root commitdiff
5018 $formats_nav .= '(initial)';
5019 } elsif (@$parents == 1) {
5020 # single parent commit
5021 $formats_nav .=
5022 '(parent: ' .
5023 $cgi->a({-href => href(action=>"commit",
5024 hash=>$parent)},
5025 esc_html(substr($parent, 0, 7))) .
5026 ')';
5027 } else {
5028 # merge commit
5029 $formats_nav .=
5030 '(merge: ' .
5031 join(' ', map {
5032 $cgi->a({-href => href(action=>"commit",
5033 hash=>$_)},
5034 esc_html(substr($_, 0, 7)));
5035 } @$parents ) .
5036 ')';
5039 if (!defined $parent) {
5040 $parent = "--root";
5042 my @difftree = split "\n", $repo->cmd_output(
5043 cmd => ['diff-tree', '-r', '--no-commit-id', @diff_opts,
5044 (@$parents <= 1 ? $parent : '-c'), $sha1, '--'],
5045 cache => 2);
5047 # non-textual hash id's can be cached
5048 my $expires;
5049 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5050 $expires = "+1d";
5052 my $refs = git_get_references();
5053 my $ref = format_ref_marker($refs, $co{'id'});
5055 git_header_html(undef, $expires);
5056 git_print_page_nav('commit', '',
5057 $hash, $co{'tree'}, $hash,
5058 $formats_nav);
5060 if (defined $co{'parent'}) {
5061 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5062 } else {
5063 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5065 print "<div class=\"title_text\">\n" .
5066 "<table class=\"object_header\">\n";
5067 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
5068 "<tr>" .
5069 "<td></td><td> $ad{'rfc2822'}";
5070 if ($ad{'hour_local'} < 6) {
5071 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
5072 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
5073 } else {
5074 printf(" (%02d:%02d %s)",
5075 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
5077 print "</td>" .
5078 "</tr>\n";
5079 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
5080 print "<tr><td></td><td> $cd{'rfc2822'}" .
5081 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
5082 "</td></tr>\n";
5083 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5084 print "<tr>" .
5085 "<td>tree</td>" .
5086 "<td class=\"sha1\">" .
5087 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5088 class => "list"}, $co{'tree'}) .
5089 "</td>" .
5090 "<td class=\"link\">" .
5091 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5092 "tree");
5093 my $snapshot_links = format_snapshot_links($hash);
5094 if (defined $snapshot_links) {
5095 print " | " . $snapshot_links;
5097 print "</td>" .
5098 "</tr>\n";
5100 foreach my $par (@$parents) {
5101 print "<tr>" .
5102 "<td>parent</td>" .
5103 "<td class=\"sha1\">" .
5104 $cgi->a({-href => href(action=>"commit", hash=>$par),
5105 class => "list"}, $par) .
5106 "</td>" .
5107 "<td class=\"link\">" .
5108 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
5109 " | " .
5110 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
5111 "</td>" .
5112 "</tr>\n";
5114 print "</table>".
5115 "</div>\n";
5117 print "<div class=\"page_body\">\n";
5118 git_print_log($co{'comment'});
5119 print "</div>\n";
5121 git_difftree_body(\@difftree, $hash, @$parents);
5123 git_footer_html();
5126 sub git_object {
5127 # object is defined by:
5128 # - hash or hash_base alone
5129 # - hash_base and file_name
5130 my $type;
5132 if ($hash || ($hash_base && !defined $file_name)) {
5133 # hash or hash_base alone
5134 $type = (git_get_sha1_or_die($hash || $hash_base))[1];
5135 } elsif ($hash_base && defined $file_name) {
5136 # hash_base and file_name
5137 $file_name =~ s,/+$,,;
5138 ($hash, $type) = git_get_sha1_or_die("$hash_base:$file_name");
5139 } else {
5140 die_error(400, "Not enough information to find object");
5142 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5143 hash=>$hash, hash_base=>$hash_base,
5144 file_name=>$file_name),
5145 -status => '302 Found');
5148 sub git_blobdiff {
5149 my $format = shift || 'html';
5151 my @difftree;
5152 my %diffinfo;
5153 my $expires;
5154 my $diff_read;
5156 # prepare $diff_read and %diffinfo for git_patchset_body
5157 # new style URI
5158 if (defined $hash_base && defined $hash_parent_base) {
5159 my $commit_sha1 = git_get_sha1_or_die($hash_base, 'commit');
5160 my $parent_base_sha1 = git_get_sha1_or_die($hash_parent_base, 'commit');
5161 git_get_sha1_or_die($file_parent, 'blob') if $file_parent;
5162 # There used to be code to handle hash (h) parameters,
5163 # but it's not used (anymore), so we can require $file_name.
5164 die_error(400, "No file name given") unless $file_name;
5165 # read raw output
5166 @difftree = split "\n", $repo->cmd_output(
5167 cmd => ['diff-tree', '-r', @diff_opts, $parent_base_sha1,
5168 $commit_sha1, '--', $file_parent || (),
5169 $file_name],
5170 cache => 2);
5172 if (@difftree > 1) {
5173 die_error(400, "Ambiguous blob diff specification");
5176 %diffinfo = parse_difftree_raw_line($difftree[0]);
5177 $file_parent ||= $diffinfo{'from_file'} || $file_name;
5178 $file_name ||= $diffinfo{'to_file'};
5180 $hash_parent ||= $diffinfo{'from_id'};
5181 $hash ||= $diffinfo{'to_id'};
5183 # non-textual hash id's can be cached
5184 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
5185 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
5186 $expires = '+1d';
5189 # open patch output
5190 # TODO: uncovered (?)
5191 my $hash_base_sha1 = git_get_sha1_or_die($hash_base, 'commit');
5192 $diff_read = $repo->progressive_cmd_output(
5193 cmd => ['diff-tree', '-r', @diff_opts, '-p',
5194 ($format eq 'html' ? '--full-index' : ()),
5195 $parent_base_sha1, $hash_base_sha1, '--',
5196 $file_parent || (), $file_name],
5197 separator => "\n", cache => 1);
5200 # old/legacy style URI (still used in feed [Atom/RSS] view)
5201 if (!%diffinfo && # if new style URI failed
5202 defined $hash && defined $hash_parent) {
5203 # fake git-diff-tree raw output
5204 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
5205 $diffinfo{'from_id'} = $hash_parent;
5206 $diffinfo{'to_id'} = $hash;
5207 if (defined $file_name) {
5208 if (defined $file_parent) {
5209 $diffinfo{'status'} = '2';
5210 $diffinfo{'from_file'} = $file_parent;
5211 $diffinfo{'to_file'} = $file_name;
5212 } else { # assume not renamed
5213 $diffinfo{'status'} = '1';
5214 $diffinfo{'from_file'} = $file_name;
5215 $diffinfo{'to_file'} = $file_name;
5217 } else { # no filename given
5218 $diffinfo{'status'} = '2';
5219 $diffinfo{'from_file'} = $hash_parent;
5220 $diffinfo{'to_file'} = $hash;
5223 # non-textual hash id's can be cached
5224 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
5225 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5226 $expires = '+1d';
5229 # open patch output
5230 my $parent_sha1 = git_get_sha1_or_die($hash_parent, 'blob');
5231 my $sha1 = git_get_sha1_or_die($hash, 'commit');
5232 $diff_read = $repo->progressive_cmd_output(
5233 cmd => ['diff', @diff_opts, '-p',
5234 ($format eq 'html' ? '--full-index' : ()),
5235 $parent_sha1, $sha1, '--'],
5236 separator => "\n", cache => 1);
5237 } else {
5238 die_error(400, "Missing one of the blob diff parameters")
5239 unless %diffinfo;
5242 # header
5243 if ($format eq 'html') {
5244 my $formats_nav =
5245 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5246 "raw");
5247 git_header_html(undef, $expires);
5248 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5249 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5250 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5251 } else {
5252 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5253 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5255 if (defined $file_name) {
5256 git_print_page_path($file_name, "blob", $hash_base);
5257 } else {
5258 print "<div class=\"page_path\"></div>\n";
5261 } elsif ($format eq 'plain') {
5262 print $cgi->header(
5263 -type => 'text/plain',
5264 -charset => 'utf-8',
5265 -expires => $expires,
5266 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5268 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5270 } else {
5271 die_error(400, "Unknown blobdiff format");
5274 # patch
5275 if ($format eq 'html') {
5276 print "<div class=\"page_body\">\n";
5278 git_patchset_body($diff_read, [ \%diffinfo ], $hash_base, $hash_parent_base);
5280 print "</div>\n"; # class="page_body"
5281 git_footer_html();
5283 } else {
5284 while (my $line = $diff_read->()) {
5285 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5286 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5288 print $line;
5290 last if $line =~ m!^\+\+\+!;
5292 while (my $line = $diff_read->()) {
5293 print $line;
5298 sub git_blobdiff_plain {
5299 git_blobdiff('plain');
5302 sub git_commitdiff {
5303 my $format = shift || 'html';
5304 $hash ||= $hash_base || "HEAD";
5305 my $sha1 = git_get_sha1_or_die($hash, 'commit');
5306 my %co = parse_commit($hash);
5308 # choose format for commitdiff for merge
5309 my $hash_parent_param = $hash_parent;
5310 # Unfortunately we can pass in command line options as
5311 # $hash_parent.
5312 if ($hash_parent_param && $hash_parent_param ne '-c' &&
5313 $hash_parent_param ne '--cc') {
5314 $hash_parent_param =
5315 git_get_sha1_or_die($hash_parent_param, 'commit');
5317 if (! defined $hash_parent_param && @{$co{'parents'}} > 1) {
5318 $hash_parent_param = '--cc';
5320 # we need to prepare $formats_nav before almost any parameter munging
5321 my $formats_nav;
5322 if ($format eq 'html') {
5323 $formats_nav =
5324 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5325 "raw");
5327 if (defined $hash_parent) {
5328 # commitdiff with two commits given
5329 my $hash_parent_short = $hash_parent;
5330 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5331 $hash_parent_short = substr($hash_parent, 0, 7);
5333 $formats_nav .=
5334 ' (from';
5335 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5336 if ($co{'parents'}[$i] eq $hash_parent) {
5337 $formats_nav .= ' parent ' . ($i+1);
5338 last;
5341 $formats_nav .= ': ' .
5342 $cgi->a({-href => href(action=>"commitdiff",
5343 hash=>$hash_parent)},
5344 esc_html($hash_parent_short)) .
5345 ')';
5346 } elsif (!$co{'parent'}) {
5347 # --root commitdiff
5348 $formats_nav .= ' (initial)';
5349 } elsif (scalar @{$co{'parents'}} == 1) {
5350 # single parent commit
5351 $formats_nav .=
5352 ' (parent: ' .
5353 $cgi->a({-href => href(action=>"commitdiff",
5354 hash=>$co{'parent'})},
5355 esc_html(substr($co{'parent'}, 0, 7))) .
5356 ')';
5357 } else {
5358 # merge commit
5359 if ($hash_parent && $hash_parent eq '--cc') {
5360 $formats_nav .= ' | ' .
5361 $cgi->a({-href => href(action=>"commitdiff",
5362 hash=>$hash, hash_parent=>'-c')},
5363 'combined');
5364 } else { # $hash_parent eq '-c'
5365 $formats_nav .= ' | ' .
5366 $cgi->a({-href => href(action=>"commitdiff",
5367 hash=>$hash, hash_parent=>'--cc')},
5368 'compact');
5370 $formats_nav .=
5371 ' (merge: ' .
5372 join(' ', map {
5373 $cgi->a({-href => href(action=>"commitdiff",
5374 hash=>$_)},
5375 esc_html(substr($_, 0, 7)));
5376 } @{$co{'parents'}} ) .
5377 ')';
5381 if (!defined $hash_parent_param) {
5382 # --cc for multiple parents, --root for parentless
5383 $hash_parent_param =
5384 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5387 # read commitdiff
5388 my $diff_read;
5389 my @difftree;
5390 if ($format eq 'html') {
5391 $diff_read = $repo->progressive_cmd_output(
5392 cmd => ['diff-tree', '-r', @diff_opts, '--no-commit-id',
5393 '--patch-with-raw', '--full-index',
5394 $hash_parent_param, $sha1, '--'],
5395 separator => "\n", cache => 1);
5396 while (my $line = $diff_read->()) {
5397 chomp $line;
5398 # empty line ends raw part of diff-tree output
5399 last unless $line;
5400 push @difftree, scalar parse_difftree_raw_line($line);
5402 } else {
5403 die unless $format eq 'plain';
5404 $diff_read = $repo->progressive_cmd_output(
5405 cmd => ['diff-tree', '-r', @diff_opts, '-p',
5406 $hash_parent_param, $sha1, '--'],
5407 separator => "\n", cache => 1);
5410 # non-textual hash id's can be cached
5411 my $expires;
5412 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5413 $expires = "+1d";
5416 # write commit message
5417 if ($format eq 'html') {
5418 my $refs = git_get_references();
5419 my $ref = format_ref_marker($refs, $co{'id'});
5421 git_header_html(undef, $expires);
5422 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5423 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5424 git_print_authorship(\%co);
5425 print "<div class=\"page_body\">\n";
5426 if (@{$co{'comment'}} > 1) {
5427 print "<div class=\"log\">\n";
5428 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5429 print "</div>\n"; # class="log"
5432 } elsif ($format eq 'plain') {
5433 my $tagname = git_get_rev_name_tags($sha1);
5434 my $filename = basename($project) . "-$hash.patch";
5436 print $cgi->header(
5437 -type => 'text/plain',
5438 -charset => 'utf-8',
5439 -expires => $expires,
5440 -content_disposition => 'inline; filename="' . "$filename" . '"');
5441 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5442 print "From: " . to_utf8($co{'author'}) . "\n";
5443 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5444 print "Subject: " . to_utf8($co{'title'}) . "\n";
5446 print "X-Git-Tag: $tagname\n" if $tagname;
5447 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5449 foreach my $line (@{$co{'comment'}}) {
5450 print to_utf8($line) . "\n";
5452 print "---\n\n";
5455 # write patch
5456 if ($format eq 'html') {
5457 my $use_parents = !defined $hash_parent ||
5458 $hash_parent eq '-c' || $hash_parent eq '--cc';
5459 git_difftree_body(\@difftree, $hash,
5460 $use_parents ? @{$co{'parents'}} : $hash_parent);
5461 print "<br/>\n";
5463 git_patchset_body($diff_read, \@difftree, $hash,
5464 $use_parents ? @{$co{'parents'}} : $hash_parent);
5465 print "</div>\n"; # class="page_body"
5466 git_footer_html();
5468 } elsif ($format eq 'plain') {
5469 while (my $line = $diff_read->()) {
5470 print $line;
5475 sub git_commitdiff_plain {
5476 git_commitdiff('plain');
5479 sub git_history {
5480 $hash_base ||= git_get_head_hash();
5481 $page ||= 0;
5482 my $ftype;
5483 my %co = parse_commit($hash_base);
5485 my $refs = git_get_references();
5486 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5488 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5489 $file_name, "--full-history");
5491 if (!defined $hash && defined $file_name) {
5492 # some commits could have deleted file in question,
5493 # and not have it in tree, but one of them has to have it
5494 for (my $i = 0; $i <= @commitlist; $i++) {
5495 $hash = git_get_sha1_by_path($commitlist[$i]{'id'}, $file_name);
5496 last if defined $hash;
5499 if (defined $hash) {
5500 git_get_sha1_or_die($hash);
5501 $ftype = git_get_type($hash);
5504 my $paging_nav = '';
5505 if ($page > 0) {
5506 $paging_nav .=
5507 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5508 file_name=>$file_name)},
5509 "first");
5510 $paging_nav .= " &sdot; " .
5511 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5512 -accesskey => "p", -title => "Alt-p"}, "prev");
5513 } else {
5514 $paging_nav .= "first";
5515 $paging_nav .= " &sdot; prev";
5517 my $next_link = '';
5518 if ($#commitlist >= 100) {
5519 $next_link =
5520 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5521 -accesskey => "n", -title => "Alt-n"}, "next");
5522 $paging_nav .= " &sdot; $next_link";
5523 } else {
5524 $paging_nav .= " &sdot; next";
5527 git_header_html();
5528 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5529 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5530 git_print_page_path($file_name, $ftype, $hash_base);
5532 git_history_body(\@commitlist, 0, 99,
5533 $refs, $hash_base, $ftype, $next_link);
5535 git_footer_html();
5538 sub git_search {
5539 gitweb_check_feature('search') or die_error(403, "Search is disabled");
5540 if (!defined $searchtext) {
5541 die_error(400, "Text field is empty");
5543 $hash ||= git_get_head_hash();
5544 my %co = parse_commit($hash);
5545 if (!defined $page) {
5546 $page = 0;
5549 $searchtype ||= 'commit';
5550 if ($searchtype eq 'pickaxe') {
5551 # pickaxe may take all resources of your box and run for several minutes
5552 # with every query - so decide by yourself how public you make this feature
5553 gitweb_check_feature('pickaxe')
5554 or die_error(403, "Pickaxe is disabled");
5556 if ($searchtype eq 'grep') {
5557 gitweb_check_feature('grep')
5558 or die_error(403, "Grep is disabled");
5561 git_header_html();
5563 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5564 my $greptype;
5565 if ($searchtype eq 'commit') {
5566 $greptype = "--grep=";
5567 } elsif ($searchtype eq 'author') {
5568 $greptype = "--author=";
5569 } elsif ($searchtype eq 'committer') {
5570 $greptype = "--committer=";
5572 $greptype .= $searchtext;
5573 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5574 $greptype, '--regexp-ignore-case',
5575 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5577 my $paging_nav = '';
5578 if ($page > 0) {
5579 $paging_nav .=
5580 $cgi->a({-href => href(action=>"search", hash=>$hash,
5581 searchtext=>$searchtext,
5582 searchtype=>$searchtype)},
5583 "first");
5584 $paging_nav .= " &sdot; " .
5585 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5586 -accesskey => "p", -title => "Alt-p"}, "prev");
5587 } else {
5588 $paging_nav .= "first";
5589 $paging_nav .= " &sdot; prev";
5591 my $next_link = '';
5592 if ($#commitlist >= 100) {
5593 $next_link =
5594 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5595 -accesskey => "n", -title => "Alt-n"}, "next");
5596 $paging_nav .= " &sdot; $next_link";
5597 } else {
5598 $paging_nav .= " &sdot; next";
5601 if ($#commitlist >= 100) {
5604 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5605 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5606 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5609 if ($searchtype eq 'pickaxe') {
5610 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5611 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5613 print "<table class=\"pickaxe search\">\n";
5614 my $alternate = 1;
5615 $/ = "\n";
5616 my $pickaxe_read = $repo->progressive_cmd_output(
5617 cmd => ['log', @diff_opts, '--pretty=format:%H',
5618 '--no-abbrev', '--raw', "-S$searchtext",
5619 ($search_use_regexp ? '--pickaxe-regex' : ()),
5620 git_get_head_hash()],
5621 separator => "\n", cache => 1);
5622 undef %co;
5623 my @files;
5624 while (my $line = $pickaxe_read->()) {
5625 chomp $line;
5626 next unless $line;
5628 my %set = parse_difftree_raw_line($line);
5629 if (defined $set{'commit'}) {
5630 # finish previous commit
5631 if (%co) {
5632 print "</td>\n" .
5633 "<td class=\"link\">" .
5634 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5635 " | " .
5636 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5637 print "</td>\n" .
5638 "</tr>\n";
5641 if ($alternate) {
5642 print "<tr class=\"dark\">\n";
5643 } else {
5644 print "<tr class=\"light\">\n";
5646 $alternate ^= 1;
5647 %co = parse_commit($set{'commit'});
5648 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5649 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5650 "<td><i>$author</i></td>\n" .
5651 "<td>" .
5652 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5653 -class => "list subject"},
5654 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5655 } elsif (defined $set{'to_id'}) {
5656 next if ($set{'to_id'} =~ m/^0{40}$/);
5658 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5659 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5660 -class => "list"},
5661 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5662 "<br/>\n";
5666 # finish last commit (warning: repetition!)
5667 if (%co) {
5668 print "</td>\n" .
5669 "<td class=\"link\">" .
5670 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5671 " | " .
5672 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5673 print "</td>\n" .
5674 "</tr>\n";
5677 print "</table>\n";
5680 if ($searchtype eq 'grep') {
5681 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5682 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5684 print "<table class=\"grep_search\">\n";
5685 my $alternate = 1;
5686 my $matches = 0;
5687 my $grep_read = $repo->progressive_cmd_output(
5688 cmd => ['grep', '-n',
5689 $search_use_regexp ? ('-E', '-i') : '-F',
5690 $searchtext, $co{'tree'}],
5691 separator => "\n", cache => 1);
5692 my $lastfile = '';
5693 while (my $line = $grep_read->()) {
5694 chomp $line;
5695 my ($file, $lno, $ltext, $binary);
5696 last if ($matches++ > 1000);
5697 if ($line =~ /^Binary file (.+) matches$/) {
5698 $file = $1;
5699 $binary = 1;
5700 } else {
5701 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5703 if ($file ne $lastfile) {
5704 $lastfile and print "</td></tr>\n";
5705 if ($alternate++) {
5706 print "<tr class=\"dark\">\n";
5707 } else {
5708 print "<tr class=\"light\">\n";
5710 print "<td class=\"list\">".
5711 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5712 file_name=>"$file"),
5713 -class => "list"}, esc_path($file));
5714 print "</td><td>\n";
5715 $lastfile = $file;
5717 if ($binary) {
5718 print "<div class=\"binary\">Binary file</div>\n";
5719 } else {
5720 $ltext = untabify($ltext);
5721 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5722 $ltext = esc_html($1, -nbsp=>1);
5723 $ltext .= '<span class="match">';
5724 $ltext .= esc_html($2, -nbsp=>1);
5725 $ltext .= '</span>';
5726 $ltext .= esc_html($3, -nbsp=>1);
5727 } else {
5728 $ltext = esc_html($ltext, -nbsp=>1);
5730 print "<div class=\"pre\">" .
5731 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5732 file_name=>"$file").'#l'.$lno,
5733 -class => "linenr"}, sprintf('%4i', $lno))
5734 . ' ' . $ltext . "</div>\n";
5737 if ($lastfile) {
5738 print "</td></tr>\n";
5739 if ($matches > 1000) {
5740 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5742 } else {
5743 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5746 print "</table>\n";
5748 git_footer_html();
5751 sub git_search_help {
5752 git_header_html();
5753 git_print_page_nav('','', $hash,$hash,$hash);
5754 print <<EOT;
5755 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5756 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5757 the pattern entered is recognized as the POSIX extended
5758 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5759 insensitive).</p>
5760 <dl>
5761 <dt><b>commit</b></dt>
5762 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5764 my ($have_grep) = gitweb_check_feature('grep');
5765 if ($have_grep) {
5766 print <<EOT;
5767 <dt><b>grep</b></dt>
5768 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5769 a different one) are searched for the given pattern. On large trees, this search can take
5770 a while and put some strain on the server, so please use it with some consideration. Note that
5771 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5772 case-sensitive.</dd>
5775 print <<EOT;
5776 <dt><b>author</b></dt>
5777 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5778 <dt><b>committer</b></dt>
5779 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5781 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5782 if ($have_pickaxe) {
5783 print <<EOT;
5784 <dt><b>pickaxe</b></dt>
5785 <dd>All commits that caused the string to appear or disappear from any file (changes that
5786 added, removed or "modified" the string) will be listed. This search can take a while and
5787 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5788 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5791 print "</dl>\n";
5792 git_footer_html();
5795 sub git_shortlog {
5796 my $head = git_get_head_hash();
5797 if (!defined $hash) {
5798 $hash = $head;
5800 if (!defined $page) {
5801 $page = 0;
5803 my $refs = git_get_references();
5805 my @commitlist = parse_commits($hash, 101, (100 * $page));
5807 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5808 my $next_link = '';
5809 if ($#commitlist >= 100) {
5810 $next_link =
5811 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5812 -accesskey => "n", -title => "Alt-n"}, "next");
5815 git_header_html();
5816 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5817 git_print_header_div('summary', $project);
5819 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5821 git_footer_html();
5824 ## ......................................................................
5825 ## feeds (RSS, Atom; OPML)
5827 sub git_feed {
5828 my $format = shift || 'atom';
5829 my ($have_blame) = gitweb_check_feature('blame');
5831 # Atom: http://www.atomenabled.org/developers/syndication/
5832 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5833 if ($format ne 'rss' && $format ne 'atom') {
5834 die_error(400, "Unknown web feed format");
5837 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5838 my $head = $hash || $repo->get_sha1('HEAD'); # can be undef
5839 my %last_modified = parse_date($repo->get_last_modification);
5841 my $content_type = "application/$format+xml";
5842 if (defined $cgi->http('HTTP_ACCEPT') &&
5843 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5844 # browser (feed reader) prefers text/xml
5845 $content_type = 'text/xml';
5847 print $cgi->header(
5848 -type => $content_type, -charset => 'utf-8',
5849 -last_modified => $last_modified{'rfc2822'});
5851 # Optimization: skip generating the body if client asks only
5852 # for Last-Modified date.
5853 return if $cgi->request_method() && $cgi->request_method() eq 'HEAD';
5855 # header variables
5856 my $title = "$site_name - $project/$action";
5857 my $feed_type = 'log';
5858 if (defined $hash) {
5859 $title .= " - '$hash'";
5860 $feed_type = 'branch log';
5861 if (defined $file_name) {
5862 $title .= " :: $file_name";
5863 $feed_type = 'history';
5865 } elsif (defined $file_name) {
5866 $title .= " - $file_name";
5867 $feed_type = 'history';
5869 $title .= " $feed_type";
5870 my $descr = git_get_project_description($project);
5871 if (defined $descr) {
5872 $descr = esc_html($descr);
5873 } else {
5874 $descr = "$project " .
5875 ($format eq 'rss' ? 'RSS' : 'Atom') .
5876 " feed";
5878 my $owner = git_get_project_owner($project);
5879 $owner = esc_html($owner);
5881 #header
5882 my $alt_url;
5883 if (defined $file_name) {
5884 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5885 } elsif (defined $hash) {
5886 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5887 } else {
5888 $alt_url = href(-full=>1, action=>"summary");
5890 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5891 if ($format eq 'rss') {
5892 print <<XML;
5893 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5894 <channel>
5896 print "<title>$title</title>\n" .
5897 "<link>$alt_url</link>\n" .
5898 "<description>$descr</description>\n" .
5899 "<language>en</language>\n";
5900 } elsif ($format eq 'atom') {
5901 print <<XML;
5902 <feed xmlns="http://www.w3.org/2005/Atom">
5904 print "<title>$title</title>\n" .
5905 "<subtitle>$descr</subtitle>\n" .
5906 '<link rel="alternate" type="text/html" href="' .
5907 $alt_url . '" />' . "\n" .
5908 '<link rel="self" type="' . $content_type . '" href="' .
5909 $cgi->self_url() . '" />' . "\n" .
5910 "<id>" . href(-full=>1) . "</id>\n" .
5911 # use project owner for feed author
5912 "<author><name>$owner</name></author>\n";
5913 if (defined $favicon) {
5914 print "<icon>" . esc_url($favicon) . "</icon>\n";
5916 if (defined $logo_url) {
5917 # not twice as wide as tall: 72 x 27 pixels
5918 print "<logo>" . esc_url($logo) . "</logo>\n";
5920 print "<updated>$last_modified{'iso-8601'}</updated>\n";
5923 # contents
5924 my @commitlist = parse_commits($head, 150, 0, $file_name) if $head;
5925 for (my $i = 0; $i <= $#commitlist; $i++) {
5926 my %co = %{$commitlist[$i]};
5927 my $commit = $co{'id'};
5928 # we read 150, we always show 30 and the ones more recent than 48 hours
5929 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5930 last;
5932 my %cd = parse_date($co{'author_epoch'});
5934 # get list of changed files
5935 my @difftree = split "\n", $repo->cmd_output(
5936 cmd => ['diff-tree', '-r', @diff_opts,
5937 $co{'parent'} || '--root', $co{'id'}, '--',
5938 (defined $file_name ? $file_name : ())],
5939 cache => 2);
5941 # print element (entry, item)
5942 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5943 if ($format eq 'rss') {
5944 print "<item>\n" .
5945 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5946 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5947 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5948 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5949 "<link>$co_url</link>\n" .
5950 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5951 "<content:encoded>" .
5952 "<![CDATA[\n";
5953 } elsif ($format eq 'atom') {
5954 print "<entry>\n" .
5955 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5956 "<updated>$cd{'iso-8601'}</updated>\n" .
5957 "<author>\n" .
5958 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5959 if ($co{'author_email'}) {
5960 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5962 print "</author>\n" .
5963 # use committer for contributor
5964 "<contributor>\n" .
5965 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5966 if ($co{'committer_email'}) {
5967 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5969 print "</contributor>\n" .
5970 "<published>$cd{'iso-8601'}</published>\n" .
5971 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5972 "<id>$co_url</id>\n" .
5973 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5974 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5976 my $comment = $co{'comment'};
5977 print "<pre>\n";
5978 foreach my $line (@$comment) {
5979 $line = esc_html($line);
5980 print "$line\n";
5982 print "</pre><ul>\n";
5983 foreach my $difftree_line (@difftree) {
5984 my %difftree = parse_difftree_raw_line($difftree_line);
5985 next if !$difftree{'from_id'};
5987 my $file = $difftree{'file'} || $difftree{'to_file'};
5989 print "<li>" .
5990 "[" .
5991 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5992 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5993 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5994 file_name=>$file, file_parent=>$difftree{'from_file'}),
5995 -title => "diff"}, 'D');
5996 if ($have_blame) {
5997 print $cgi->a({-href => href(-full=>1, action=>"blame",
5998 file_name=>$file, hash_base=>$commit),
5999 -title => "blame"}, 'B');
6001 # if this is not a feed of a file history
6002 if (!defined $file_name || $file_name ne $file) {
6003 print $cgi->a({-href => href(-full=>1, action=>"history",
6004 file_name=>$file, hash=>$commit),
6005 -title => "history"}, 'H');
6007 $file = esc_path($file);
6008 print "] ".
6009 "$file</li>\n";
6011 if ($format eq 'rss') {
6012 print "</ul>]]>\n" .
6013 "</content:encoded>\n" .
6014 "</item>\n";
6015 } elsif ($format eq 'atom') {
6016 print "</ul>\n</div>\n" .
6017 "</content>\n" .
6018 "</entry>\n";
6022 # end of feed
6023 if ($format eq 'rss') {
6024 print "</channel>\n</rss>\n";
6025 } elsif ($format eq 'atom') {
6026 print "</feed>\n";
6030 sub git_rss {
6031 git_feed('rss');
6034 sub git_atom {
6035 git_feed('atom');
6038 sub git_opml {
6039 my @list = git_get_projects_list();
6041 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
6042 print <<XML;
6043 <?xml version="1.0" encoding="utf-8"?>
6044 <opml version="1.0">
6045 <head>
6046 <title>$site_name OPML Export</title>
6047 </head>
6048 <body>
6049 <outline text="git RSS feeds">
6052 foreach my $pr (@list) {
6053 my %proj = %$pr;
6054 next unless $repo_root->repo(repo_dir => $proj{'path'})
6055 ->get_sha1('HEAD');
6056 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6057 my $rss = "$my_url?p=$proj{'path'};a=rss";
6058 my $html = "$my_url?p=$proj{'path'};a=summary";
6059 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6061 print <<XML;
6062 </outline>
6063 </body>
6064 </opml>