commit: die before asking to edit the log message
[git/mingw.git] / gitweb / gitweb.perl
blob42cc1da7ff85bca5c23a59b321cd4a6a7aa45d46
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 5.008;
11 use strict;
12 use warnings;
13 use CGI qw(:standard :escapeHTML -nosticky);
14 use CGI::Util qw(unescape);
15 use CGI::Carp qw(fatalsToBrowser set_message);
16 use Encode;
17 use Fcntl ':mode';
18 use File::Find qw();
19 use File::Basename qw(basename);
20 binmode STDOUT, ':utf8';
22 our $t0;
23 if (eval { require Time::HiRes; 1; }) {
24 $t0 = [Time::HiRes::gettimeofday()];
26 our $number_of_git_cmds = 0;
28 BEGIN {
29 CGI->compile() if $ENV{'MOD_PERL'};
32 our $version = "++GIT_VERSION++";
34 our ($my_url, $my_uri, $base_url, $path_info, $home_link);
35 sub evaluate_uri {
36 our $cgi;
38 our $my_url = $cgi->url();
39 our $my_uri = $cgi->url(-absolute => 1);
41 # Base URL for relative URLs in gitweb ($logo, $favicon, ...),
42 # needed and used only for URLs with nonempty PATH_INFO
43 our $base_url = $my_url;
45 # When the script is used as DirectoryIndex, the URL does not contain the name
46 # of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we
47 # have to do it ourselves. We make $path_info global because it's also used
48 # later on.
50 # Another issue with the script being the DirectoryIndex is that the resulting
51 # $my_url data is not the full script URL: this is good, because we want
52 # generated links to keep implying the script name if it wasn't explicitly
53 # indicated in the URL we're handling, but it means that $my_url cannot be used
54 # as base URL.
55 # Therefore, if we needed to strip PATH_INFO, then we know that we have
56 # to build the base URL ourselves:
57 our $path_info = $ENV{"PATH_INFO"};
58 if ($path_info) {
59 if ($my_url =~ s,\Q$path_info\E$,, &&
60 $my_uri =~ s,\Q$path_info\E$,, &&
61 defined $ENV{'SCRIPT_NAME'}) {
62 $base_url = $cgi->url(-base => 1) . $ENV{'SCRIPT_NAME'};
66 # target of the home link on top of all pages
67 our $home_link = $my_uri || "/";
70 # core git executable to use
71 # this can just be "git" if your webserver has a sensible PATH
72 our $GIT = "++GIT_BINDIR++/git";
74 # absolute fs-path which will be prepended to the project path
75 #our $projectroot = "/pub/scm";
76 our $projectroot = "++GITWEB_PROJECTROOT++";
78 # fs traversing limit for getting project list
79 # the number is relative to the projectroot
80 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
82 # string of the home link on top of all pages
83 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
85 # name of your site or organization to appear in page titles
86 # replace this with something more descriptive for clearer bookmarks
87 our $site_name = "++GITWEB_SITENAME++"
88 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
90 # filename of html text to include at top of each page
91 our $site_header = "++GITWEB_SITE_HEADER++";
92 # html text to include at home page
93 our $home_text = "++GITWEB_HOMETEXT++";
94 # filename of html text to include at bottom of each page
95 our $site_footer = "++GITWEB_SITE_FOOTER++";
97 # URI of stylesheets
98 our @stylesheets = ("++GITWEB_CSS++");
99 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
100 our $stylesheet = undef;
101 # URI of GIT logo (72x27 size)
102 our $logo = "++GITWEB_LOGO++";
103 # URI of GIT favicon, assumed to be image/png type
104 our $favicon = "++GITWEB_FAVICON++";
105 # URI of gitweb.js (JavaScript code for gitweb)
106 our $javascript = "++GITWEB_JS++";
108 # URI and label (title) of GIT logo link
109 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
110 #our $logo_label = "git documentation";
111 our $logo_url = "http://git-scm.com/";
112 our $logo_label = "git homepage";
114 # source of projects list
115 our $projects_list = "++GITWEB_LIST++";
117 # the width (in characters) of the projects list "Description" column
118 our $projects_list_description_width = 25;
120 # default order of projects list
121 # valid values are none, project, descr, owner, and age
122 our $default_projects_order = "project";
124 # show repository only if this file exists
125 # (only effective if this variable evaluates to true)
126 our $export_ok = "++GITWEB_EXPORT_OK++";
128 # show repository only if this subroutine returns true
129 # when given the path to the project, for example:
130 # sub { return -e "$_[0]/git-daemon-export-ok"; }
131 our $export_auth_hook = undef;
133 # only allow viewing of repositories also shown on the overview page
134 our $strict_export = "++GITWEB_STRICT_EXPORT++";
136 # list of git base URLs used for URL to where fetch project from,
137 # i.e. full URL is "$git_base_url/$project"
138 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
140 # default blob_plain mimetype and default charset for text/plain blob
141 our $default_blob_plain_mimetype = 'text/plain';
142 our $default_text_plain_charset = undef;
144 # file to use for guessing MIME types before trying /etc/mime.types
145 # (relative to the current git repository)
146 our $mimetypes_file = undef;
148 # assume this charset if line contains non-UTF-8 characters;
149 # it should be valid encoding (see Encoding::Supported(3pm) for list),
150 # for which encoding all byte sequences are valid, for example
151 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
152 # could be even 'utf-8' for the old behavior)
153 our $fallback_encoding = 'latin1';
155 # rename detection options for git-diff and git-diff-tree
156 # - default is '-M', with the cost proportional to
157 # (number of removed files) * (number of new files).
158 # - more costly is '-C' (which implies '-M'), with the cost proportional to
159 # (number of changed files + number of removed files) * (number of new files)
160 # - even more costly is '-C', '--find-copies-harder' with cost
161 # (number of files in the original tree) * (number of new files)
162 # - one might want to include '-B' option, e.g. '-B', '-M'
163 our @diff_opts = ('-M'); # taken from git_commit
165 # Disables features that would allow repository owners to inject script into
166 # the gitweb domain.
167 our $prevent_xss = 0;
169 # information about snapshot formats that gitweb is capable of serving
170 our %known_snapshot_formats = (
171 # name => {
172 # 'display' => display name,
173 # 'type' => mime type,
174 # 'suffix' => filename suffix,
175 # 'format' => --format for git-archive,
176 # 'compressor' => [compressor command and arguments]
177 # (array reference, optional)
178 # 'disabled' => boolean (optional)}
180 'tgz' => {
181 'display' => 'tar.gz',
182 'type' => 'application/x-gzip',
183 'suffix' => '.tar.gz',
184 'format' => 'tar',
185 'compressor' => ['gzip']},
187 'tbz2' => {
188 'display' => 'tar.bz2',
189 'type' => 'application/x-bzip2',
190 'suffix' => '.tar.bz2',
191 'format' => 'tar',
192 'compressor' => ['bzip2']},
194 'txz' => {
195 'display' => 'tar.xz',
196 'type' => 'application/x-xz',
197 'suffix' => '.tar.xz',
198 'format' => 'tar',
199 'compressor' => ['xz'],
200 'disabled' => 1},
202 'zip' => {
203 'display' => 'zip',
204 'type' => 'application/x-zip',
205 'suffix' => '.zip',
206 'format' => 'zip'},
209 # Aliases so we understand old gitweb.snapshot values in repository
210 # configuration.
211 our %known_snapshot_format_aliases = (
212 'gzip' => 'tgz',
213 'bzip2' => 'tbz2',
214 'xz' => 'txz',
216 # backward compatibility: legacy gitweb config support
217 'x-gzip' => undef, 'gz' => undef,
218 'x-bzip2' => undef, 'bz2' => undef,
219 'x-zip' => undef, '' => undef,
222 # Pixel sizes for icons and avatars. If the default font sizes or lineheights
223 # are changed, it may be appropriate to change these values too via
224 # $GITWEB_CONFIG.
225 our %avatar_size = (
226 'default' => 16,
227 'double' => 32
230 # Used to set the maximum load that we will still respond to gitweb queries.
231 # If server load exceed this value then return "503 server busy" error.
232 # If gitweb cannot determined server load, it is taken to be 0.
233 # Leave it undefined (or set to 'undef') to turn off load checking.
234 our $maxload = 300;
236 # configuration for 'highlight' (http://www.andre-simon.de/)
237 # match by basename
238 our %highlight_basename = (
239 #'Program' => 'py',
240 #'Library' => 'py',
241 'SConstruct' => 'py', # SCons equivalent of Makefile
242 'Makefile' => 'make',
244 # match by extension
245 our %highlight_ext = (
246 # main extensions, defining name of syntax;
247 # see files in /usr/share/highlight/langDefs/ directory
248 map { $_ => $_ }
249 qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),
250 # alternate extensions, see /etc/highlight/filetypes.conf
251 'h' => 'c',
252 map { $_ => 'cpp' } qw(cxx c++ cc),
253 map { $_ => 'php' } qw(php3 php4),
254 map { $_ => 'pl' } qw(perl pm), # perhaps also 'cgi'
255 'mak' => 'make',
256 map { $_ => 'xml' } qw(xhtml html htm),
259 # You define site-wide feature defaults here; override them with
260 # $GITWEB_CONFIG as necessary.
261 our %feature = (
262 # feature => {
263 # 'sub' => feature-sub (subroutine),
264 # 'override' => allow-override (boolean),
265 # 'default' => [ default options...] (array reference)}
267 # if feature is overridable (it means that allow-override has true value),
268 # then feature-sub will be called with default options as parameters;
269 # return value of feature-sub indicates if to enable specified feature
271 # if there is no 'sub' key (no feature-sub), then feature cannot be
272 # overridden
274 # use gitweb_get_feature(<feature>) to retrieve the <feature> value
275 # (an array) or gitweb_check_feature(<feature>) to check if <feature>
276 # is enabled
278 # Enable the 'blame' blob view, showing the last commit that modified
279 # each line in the file. This can be very CPU-intensive.
281 # To enable system wide have in $GITWEB_CONFIG
282 # $feature{'blame'}{'default'} = [1];
283 # To have project specific config enable override in $GITWEB_CONFIG
284 # $feature{'blame'}{'override'} = 1;
285 # and in project config gitweb.blame = 0|1;
286 'blame' => {
287 'sub' => sub { feature_bool('blame', @_) },
288 'override' => 0,
289 'default' => [0]},
291 # Enable the 'snapshot' link, providing a compressed archive of any
292 # tree. This can potentially generate high traffic if you have large
293 # project.
295 # Value is a list of formats defined in %known_snapshot_formats that
296 # you wish to offer.
297 # To disable system wide have in $GITWEB_CONFIG
298 # $feature{'snapshot'}{'default'} = [];
299 # To have project specific config enable override in $GITWEB_CONFIG
300 # $feature{'snapshot'}{'override'} = 1;
301 # and in project config, a comma-separated list of formats or "none"
302 # to disable. Example: gitweb.snapshot = tbz2,zip;
303 'snapshot' => {
304 'sub' => \&feature_snapshot,
305 'override' => 0,
306 'default' => ['tgz']},
308 # Enable text search, which will list the commits which match author,
309 # committer or commit text to a given string. Enabled by default.
310 # Project specific override is not supported.
311 'search' => {
312 'override' => 0,
313 'default' => [1]},
315 # Enable grep search, which will list the files in currently selected
316 # tree containing the given string. Enabled by default. This can be
317 # potentially CPU-intensive, of course.
319 # To enable system wide have in $GITWEB_CONFIG
320 # $feature{'grep'}{'default'} = [1];
321 # To have project specific config enable override in $GITWEB_CONFIG
322 # $feature{'grep'}{'override'} = 1;
323 # and in project config gitweb.grep = 0|1;
324 'grep' => {
325 'sub' => sub { feature_bool('grep', @_) },
326 'override' => 0,
327 'default' => [1]},
329 # Enable the pickaxe search, which will list the commits that modified
330 # a given string in a file. This can be practical and quite faster
331 # alternative to 'blame', but still potentially CPU-intensive.
333 # To enable system wide have in $GITWEB_CONFIG
334 # $feature{'pickaxe'}{'default'} = [1];
335 # To have project specific config enable override in $GITWEB_CONFIG
336 # $feature{'pickaxe'}{'override'} = 1;
337 # and in project config gitweb.pickaxe = 0|1;
338 'pickaxe' => {
339 'sub' => sub { feature_bool('pickaxe', @_) },
340 'override' => 0,
341 'default' => [1]},
343 # Enable showing size of blobs in a 'tree' view, in a separate
344 # column, similar to what 'ls -l' does. This cost a bit of IO.
346 # To disable system wide have in $GITWEB_CONFIG
347 # $feature{'show-sizes'}{'default'} = [0];
348 # To have project specific config enable override in $GITWEB_CONFIG
349 # $feature{'show-sizes'}{'override'} = 1;
350 # and in project config gitweb.showsizes = 0|1;
351 'show-sizes' => {
352 'sub' => sub { feature_bool('showsizes', @_) },
353 'override' => 0,
354 'default' => [1]},
356 # Make gitweb use an alternative format of the URLs which can be
357 # more readable and natural-looking: project name is embedded
358 # directly in the path and the query string contains other
359 # auxiliary information. All gitweb installations recognize
360 # URL in either format; this configures in which formats gitweb
361 # generates links.
363 # To enable system wide have in $GITWEB_CONFIG
364 # $feature{'pathinfo'}{'default'} = [1];
365 # Project specific override is not supported.
367 # Note that you will need to change the default location of CSS,
368 # favicon, logo and possibly other files to an absolute URL. Also,
369 # if gitweb.cgi serves as your indexfile, you will need to force
370 # $my_uri to contain the script name in your $GITWEB_CONFIG.
371 'pathinfo' => {
372 'override' => 0,
373 'default' => [0]},
375 # Make gitweb consider projects in project root subdirectories
376 # to be forks of existing projects. Given project $projname.git,
377 # projects matching $projname/*.git will not be shown in the main
378 # projects list, instead a '+' mark will be added to $projname
379 # there and a 'forks' view will be enabled for the project, listing
380 # all the forks. If project list is taken from a file, forks have
381 # to be listed after the main project.
383 # To enable system wide have in $GITWEB_CONFIG
384 # $feature{'forks'}{'default'} = [1];
385 # Project specific override is not supported.
386 'forks' => {
387 'override' => 0,
388 'default' => [0]},
390 # Insert custom links to the action bar of all project pages.
391 # This enables you mainly to link to third-party scripts integrating
392 # into gitweb; e.g. git-browser for graphical history representation
393 # or custom web-based repository administration interface.
395 # The 'default' value consists of a list of triplets in the form
396 # (label, link, position) where position is the label after which
397 # to insert the link and link is a format string where %n expands
398 # to the project name, %f to the project path within the filesystem,
399 # %h to the current hash (h gitweb parameter) and %b to the current
400 # hash base (hb gitweb parameter); %% expands to %.
402 # To enable system wide have in $GITWEB_CONFIG e.g.
403 # $feature{'actions'}{'default'} = [('graphiclog',
404 # '/git-browser/by-commit.html?r=%n', 'summary')];
405 # Project specific override is not supported.
406 'actions' => {
407 'override' => 0,
408 'default' => []},
410 # Allow gitweb scan project content tags described in ctags/
411 # of project repository, and display the popular Web 2.0-ish
412 # "tag cloud" near the project list. Note that this is something
413 # COMPLETELY different from the normal Git tags.
415 # gitweb by itself can show existing tags, but it does not handle
416 # tagging itself; you need an external application for that.
417 # For an example script, check Girocco's cgi/tagproj.cgi.
418 # You may want to install the HTML::TagCloud Perl module to get
419 # a pretty tag cloud instead of just a list of tags.
421 # To enable system wide have in $GITWEB_CONFIG
422 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
423 # Project specific override is not supported.
424 'ctags' => {
425 'override' => 0,
426 'default' => [0]},
428 # The maximum number of patches in a patchset generated in patch
429 # view. Set this to 0 or undef to disable patch view, or to a
430 # negative number to remove any limit.
432 # To disable system wide have in $GITWEB_CONFIG
433 # $feature{'patches'}{'default'} = [0];
434 # To have project specific config enable override in $GITWEB_CONFIG
435 # $feature{'patches'}{'override'} = 1;
436 # and in project config gitweb.patches = 0|n;
437 # where n is the maximum number of patches allowed in a patchset.
438 'patches' => {
439 'sub' => \&feature_patches,
440 'override' => 0,
441 'default' => [16]},
443 # Avatar support. When this feature is enabled, views such as
444 # shortlog or commit will display an avatar associated with
445 # the email of the committer(s) and/or author(s).
447 # Currently available providers are gravatar and picon.
448 # If an unknown provider is specified, the feature is disabled.
450 # Gravatar depends on Digest::MD5.
451 # Picon currently relies on the indiana.edu database.
453 # To enable system wide have in $GITWEB_CONFIG
454 # $feature{'avatar'}{'default'} = ['<provider>'];
455 # where <provider> is either gravatar or picon.
456 # To have project specific config enable override in $GITWEB_CONFIG
457 # $feature{'avatar'}{'override'} = 1;
458 # and in project config gitweb.avatar = <provider>;
459 'avatar' => {
460 'sub' => \&feature_avatar,
461 'override' => 0,
462 'default' => ['']},
464 # Enable displaying how much time and how many git commands
465 # it took to generate and display page. Disabled by default.
466 # Project specific override is not supported.
467 'timed' => {
468 'override' => 0,
469 'default' => [0]},
471 # Enable turning some links into links to actions which require
472 # JavaScript to run (like 'blame_incremental'). Not enabled by
473 # default. Project specific override is currently not supported.
474 'javascript-actions' => {
475 'override' => 0,
476 'default' => [0]},
478 # Syntax highlighting support. This is based on Daniel Svensson's
479 # and Sham Chukoury's work in gitweb-xmms2.git.
480 # It requires the 'highlight' program present in $PATH,
481 # and therefore is disabled by default.
483 # To enable system wide have in $GITWEB_CONFIG
484 # $feature{'highlight'}{'default'} = [1];
486 'highlight' => {
487 'sub' => sub { feature_bool('highlight', @_) },
488 'override' => 0,
489 'default' => [0]},
492 sub gitweb_get_feature {
493 my ($name) = @_;
494 return unless exists $feature{$name};
495 my ($sub, $override, @defaults) = (
496 $feature{$name}{'sub'},
497 $feature{$name}{'override'},
498 @{$feature{$name}{'default'}});
499 # project specific override is possible only if we have project
500 our $git_dir; # global variable, declared later
501 if (!$override || !defined $git_dir) {
502 return @defaults;
504 if (!defined $sub) {
505 warn "feature $name is not overridable";
506 return @defaults;
508 return $sub->(@defaults);
511 # A wrapper to check if a given feature is enabled.
512 # With this, you can say
514 # my $bool_feat = gitweb_check_feature('bool_feat');
515 # gitweb_check_feature('bool_feat') or somecode;
517 # instead of
519 # my ($bool_feat) = gitweb_get_feature('bool_feat');
520 # (gitweb_get_feature('bool_feat'))[0] or somecode;
522 sub gitweb_check_feature {
523 return (gitweb_get_feature(@_))[0];
527 sub feature_bool {
528 my $key = shift;
529 my ($val) = git_get_project_config($key, '--bool');
531 if (!defined $val) {
532 return ($_[0]);
533 } elsif ($val eq 'true') {
534 return (1);
535 } elsif ($val eq 'false') {
536 return (0);
540 sub feature_snapshot {
541 my (@fmts) = @_;
543 my ($val) = git_get_project_config('snapshot');
545 if ($val) {
546 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
549 return @fmts;
552 sub feature_patches {
553 my @val = (git_get_project_config('patches', '--int'));
555 if (@val) {
556 return @val;
559 return ($_[0]);
562 sub feature_avatar {
563 my @val = (git_get_project_config('avatar'));
565 return @val ? @val : @_;
568 # checking HEAD file with -e is fragile if the repository was
569 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
570 # and then pruned.
571 sub check_head_link {
572 my ($dir) = @_;
573 my $headfile = "$dir/HEAD";
574 return ((-e $headfile) ||
575 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
578 sub check_export_ok {
579 my ($dir) = @_;
580 return (check_head_link($dir) &&
581 (!$export_ok || -e "$dir/$export_ok") &&
582 (!$export_auth_hook || $export_auth_hook->($dir)));
585 # process alternate names for backward compatibility
586 # filter out unsupported (unknown) snapshot formats
587 sub filter_snapshot_fmts {
588 my @fmts = @_;
590 @fmts = map {
591 exists $known_snapshot_format_aliases{$_} ?
592 $known_snapshot_format_aliases{$_} : $_} @fmts;
593 @fmts = grep {
594 exists $known_snapshot_formats{$_} &&
595 !$known_snapshot_formats{$_}{'disabled'}} @fmts;
598 our ($GITWEB_CONFIG, $GITWEB_CONFIG_SYSTEM);
599 sub evaluate_gitweb_config {
600 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
601 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
602 # die if there are errors parsing config file
603 if (-e $GITWEB_CONFIG) {
604 do $GITWEB_CONFIG;
605 die $@ if $@;
606 } elsif (-e $GITWEB_CONFIG_SYSTEM) {
607 do $GITWEB_CONFIG_SYSTEM;
608 die $@ if $@;
612 # Get loadavg of system, to compare against $maxload.
613 # Currently it requires '/proc/loadavg' present to get loadavg;
614 # if it is not present it returns 0, which means no load checking.
615 sub get_loadavg {
616 if( -e '/proc/loadavg' ){
617 open my $fd, '<', '/proc/loadavg'
618 or return 0;
619 my @load = split(/\s+/, scalar <$fd>);
620 close $fd;
622 # The first three columns measure CPU and IO utilization of the last one,
623 # five, and 10 minute periods. The fourth column shows the number of
624 # currently running processes and the total number of processes in the m/n
625 # format. The last column displays the last process ID used.
626 return $load[0] || 0;
628 # additional checks for load average should go here for things that don't export
629 # /proc/loadavg
631 return 0;
634 # version of the core git binary
635 our $git_version;
636 sub evaluate_git_version {
637 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
638 $number_of_git_cmds++;
641 sub check_loadavg {
642 if (defined $maxload && get_loadavg() > $maxload) {
643 die_error(503, "The load average on the server is too high");
647 # ======================================================================
648 # input validation and dispatch
650 # input parameters can be collected from a variety of sources (presently, CGI
651 # and PATH_INFO), so we define an %input_params hash that collects them all
652 # together during validation: this allows subsequent uses (e.g. href()) to be
653 # agnostic of the parameter origin
655 our %input_params = ();
657 # input parameters are stored with the long parameter name as key. This will
658 # also be used in the href subroutine to convert parameters to their CGI
659 # equivalent, and since the href() usage is the most frequent one, we store
660 # the name -> CGI key mapping here, instead of the reverse.
662 # XXX: Warning: If you touch this, check the search form for updating,
663 # too.
665 our @cgi_param_mapping = (
666 project => "p",
667 action => "a",
668 file_name => "f",
669 file_parent => "fp",
670 hash => "h",
671 hash_parent => "hp",
672 hash_base => "hb",
673 hash_parent_base => "hpb",
674 page => "pg",
675 order => "o",
676 searchtext => "s",
677 searchtype => "st",
678 snapshot_format => "sf",
679 extra_options => "opt",
680 search_use_regexp => "sr",
681 # this must be last entry (for manipulation from JavaScript)
682 javascript => "js"
684 our %cgi_param_mapping = @cgi_param_mapping;
686 # we will also need to know the possible actions, for validation
687 our %actions = (
688 "blame" => \&git_blame,
689 "blame_incremental" => \&git_blame_incremental,
690 "blame_data" => \&git_blame_data,
691 "blobdiff" => \&git_blobdiff,
692 "blobdiff_plain" => \&git_blobdiff_plain,
693 "blob" => \&git_blob,
694 "blob_plain" => \&git_blob_plain,
695 "commitdiff" => \&git_commitdiff,
696 "commitdiff_plain" => \&git_commitdiff_plain,
697 "commit" => \&git_commit,
698 "forks" => \&git_forks,
699 "heads" => \&git_heads,
700 "history" => \&git_history,
701 "log" => \&git_log,
702 "patch" => \&git_patch,
703 "patches" => \&git_patches,
704 "rss" => \&git_rss,
705 "atom" => \&git_atom,
706 "search" => \&git_search,
707 "search_help" => \&git_search_help,
708 "shortlog" => \&git_shortlog,
709 "summary" => \&git_summary,
710 "tag" => \&git_tag,
711 "tags" => \&git_tags,
712 "tree" => \&git_tree,
713 "snapshot" => \&git_snapshot,
714 "object" => \&git_object,
715 # those below don't need $project
716 "opml" => \&git_opml,
717 "project_list" => \&git_project_list,
718 "project_index" => \&git_project_index,
721 # finally, we have the hash of allowed extra_options for the commands that
722 # allow them
723 our %allowed_options = (
724 "--no-merges" => [ qw(rss atom log shortlog history) ],
727 # fill %input_params with the CGI parameters. All values except for 'opt'
728 # should be single values, but opt can be an array. We should probably
729 # build an array of parameters that can be multi-valued, but since for the time
730 # being it's only this one, we just single it out
731 sub evaluate_query_params {
732 our $cgi;
734 while (my ($name, $symbol) = each %cgi_param_mapping) {
735 if ($symbol eq 'opt') {
736 $input_params{$name} = [ $cgi->param($symbol) ];
737 } else {
738 $input_params{$name} = $cgi->param($symbol);
743 # now read PATH_INFO and update the parameter list for missing parameters
744 sub evaluate_path_info {
745 return if defined $input_params{'project'};
746 return if !$path_info;
747 $path_info =~ s,^/+,,;
748 return if !$path_info;
750 # find which part of PATH_INFO is project
751 my $project = $path_info;
752 $project =~ s,/+$,,;
753 while ($project && !check_head_link("$projectroot/$project")) {
754 $project =~ s,/*[^/]*$,,;
756 return unless $project;
757 $input_params{'project'} = $project;
759 # do not change any parameters if an action is given using the query string
760 return if $input_params{'action'};
761 $path_info =~ s,^\Q$project\E/*,,;
763 # next, check if we have an action
764 my $action = $path_info;
765 $action =~ s,/.*$,,;
766 if (exists $actions{$action}) {
767 $path_info =~ s,^$action/*,,;
768 $input_params{'action'} = $action;
771 # list of actions that want hash_base instead of hash, but can have no
772 # pathname (f) parameter
773 my @wants_base = (
774 'tree',
775 'history',
778 # we want to catch
779 # [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name]
780 my ($parentrefname, $parentpathname, $refname, $pathname) =
781 ($path_info =~ /^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/);
783 # first, analyze the 'current' part
784 if (defined $pathname) {
785 # we got "branch:filename" or "branch:dir/"
786 # we could use git_get_type(branch:pathname), but:
787 # - it needs $git_dir
788 # - it does a git() call
789 # - the convention of terminating directories with a slash
790 # makes it superfluous
791 # - embedding the action in the PATH_INFO would make it even
792 # more superfluous
793 $pathname =~ s,^/+,,;
794 if (!$pathname || substr($pathname, -1) eq "/") {
795 $input_params{'action'} ||= "tree";
796 $pathname =~ s,/$,,;
797 } else {
798 # the default action depends on whether we had parent info
799 # or not
800 if ($parentrefname) {
801 $input_params{'action'} ||= "blobdiff_plain";
802 } else {
803 $input_params{'action'} ||= "blob_plain";
806 $input_params{'hash_base'} ||= $refname;
807 $input_params{'file_name'} ||= $pathname;
808 } elsif (defined $refname) {
809 # we got "branch". In this case we have to choose if we have to
810 # set hash or hash_base.
812 # Most of the actions without a pathname only want hash to be
813 # set, except for the ones specified in @wants_base that want
814 # hash_base instead. It should also be noted that hand-crafted
815 # links having 'history' as an action and no pathname or hash
816 # set will fail, but that happens regardless of PATH_INFO.
817 $input_params{'action'} ||= "shortlog";
818 if (grep { $_ eq $input_params{'action'} } @wants_base) {
819 $input_params{'hash_base'} ||= $refname;
820 } else {
821 $input_params{'hash'} ||= $refname;
825 # next, handle the 'parent' part, if present
826 if (defined $parentrefname) {
827 # a missing pathspec defaults to the 'current' filename, allowing e.g.
828 # someproject/blobdiff/oldrev..newrev:/filename
829 if ($parentpathname) {
830 $parentpathname =~ s,^/+,,;
831 $parentpathname =~ s,/$,,;
832 $input_params{'file_parent'} ||= $parentpathname;
833 } else {
834 $input_params{'file_parent'} ||= $input_params{'file_name'};
836 # we assume that hash_parent_base is wanted if a path was specified,
837 # or if the action wants hash_base instead of hash
838 if (defined $input_params{'file_parent'} ||
839 grep { $_ eq $input_params{'action'} } @wants_base) {
840 $input_params{'hash_parent_base'} ||= $parentrefname;
841 } else {
842 $input_params{'hash_parent'} ||= $parentrefname;
846 # for the snapshot action, we allow URLs in the form
847 # $project/snapshot/$hash.ext
848 # where .ext determines the snapshot and gets removed from the
849 # passed $refname to provide the $hash.
851 # To be able to tell that $refname includes the format extension, we
852 # require the following two conditions to be satisfied:
853 # - the hash input parameter MUST have been set from the $refname part
854 # of the URL (i.e. they must be equal)
855 # - the snapshot format MUST NOT have been defined already (e.g. from
856 # CGI parameter sf)
857 # It's also useless to try any matching unless $refname has a dot,
858 # so we check for that too
859 if (defined $input_params{'action'} &&
860 $input_params{'action'} eq 'snapshot' &&
861 defined $refname && index($refname, '.') != -1 &&
862 $refname eq $input_params{'hash'} &&
863 !defined $input_params{'snapshot_format'}) {
864 # We loop over the known snapshot formats, checking for
865 # extensions. Allowed extensions are both the defined suffix
866 # (which includes the initial dot already) and the snapshot
867 # format key itself, with a prepended dot
868 while (my ($fmt, $opt) = each %known_snapshot_formats) {
869 my $hash = $refname;
870 unless ($hash =~ s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) {
871 next;
873 my $sfx = $1;
874 # a valid suffix was found, so set the snapshot format
875 # and reset the hash parameter
876 $input_params{'snapshot_format'} = $fmt;
877 $input_params{'hash'} = $hash;
878 # we also set the format suffix to the one requested
879 # in the URL: this way a request for e.g. .tgz returns
880 # a .tgz instead of a .tar.gz
881 $known_snapshot_formats{$fmt}{'suffix'} = $sfx;
882 last;
887 our ($action, $project, $file_name, $file_parent, $hash, $hash_parent, $hash_base,
888 $hash_parent_base, @extra_options, $page, $searchtype, $search_use_regexp,
889 $searchtext, $search_regexp);
890 sub evaluate_and_validate_params {
891 our $action = $input_params{'action'};
892 if (defined $action) {
893 if (!validate_action($action)) {
894 die_error(400, "Invalid action parameter");
898 # parameters which are pathnames
899 our $project = $input_params{'project'};
900 if (defined $project) {
901 if (!validate_project($project)) {
902 undef $project;
903 die_error(404, "No such project");
907 our $file_name = $input_params{'file_name'};
908 if (defined $file_name) {
909 if (!validate_pathname($file_name)) {
910 die_error(400, "Invalid file parameter");
914 our $file_parent = $input_params{'file_parent'};
915 if (defined $file_parent) {
916 if (!validate_pathname($file_parent)) {
917 die_error(400, "Invalid file parent parameter");
921 # parameters which are refnames
922 our $hash = $input_params{'hash'};
923 if (defined $hash) {
924 if (!validate_refname($hash)) {
925 die_error(400, "Invalid hash parameter");
929 our $hash_parent = $input_params{'hash_parent'};
930 if (defined $hash_parent) {
931 if (!validate_refname($hash_parent)) {
932 die_error(400, "Invalid hash parent parameter");
936 our $hash_base = $input_params{'hash_base'};
937 if (defined $hash_base) {
938 if (!validate_refname($hash_base)) {
939 die_error(400, "Invalid hash base parameter");
943 our @extra_options = @{$input_params{'extra_options'}};
944 # @extra_options is always defined, since it can only be (currently) set from
945 # CGI, and $cgi->param() returns the empty array in array context if the param
946 # is not set
947 foreach my $opt (@extra_options) {
948 if (not exists $allowed_options{$opt}) {
949 die_error(400, "Invalid option parameter");
951 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
952 die_error(400, "Invalid option parameter for this action");
956 our $hash_parent_base = $input_params{'hash_parent_base'};
957 if (defined $hash_parent_base) {
958 if (!validate_refname($hash_parent_base)) {
959 die_error(400, "Invalid hash parent base parameter");
963 # other parameters
964 our $page = $input_params{'page'};
965 if (defined $page) {
966 if ($page =~ m/[^0-9]/) {
967 die_error(400, "Invalid page parameter");
971 our $searchtype = $input_params{'searchtype'};
972 if (defined $searchtype) {
973 if ($searchtype =~ m/[^a-z]/) {
974 die_error(400, "Invalid searchtype parameter");
978 our $search_use_regexp = $input_params{'search_use_regexp'};
980 our $searchtext = $input_params{'searchtext'};
981 our $search_regexp;
982 if (defined $searchtext) {
983 if (length($searchtext) < 2) {
984 die_error(403, "At least two characters are required for search parameter");
986 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
990 # path to the current git repository
991 our $git_dir;
992 sub evaluate_git_dir {
993 our $git_dir = "$projectroot/$project" if $project;
996 our (@snapshot_fmts, $git_avatar);
997 sub configure_gitweb_features {
998 # list of supported snapshot formats
999 our @snapshot_fmts = gitweb_get_feature('snapshot');
1000 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1002 # check that the avatar feature is set to a known provider name,
1003 # and for each provider check if the dependencies are satisfied.
1004 # if the provider name is invalid or the dependencies are not met,
1005 # reset $git_avatar to the empty string.
1006 our ($git_avatar) = gitweb_get_feature('avatar');
1007 if ($git_avatar eq 'gravatar') {
1008 $git_avatar = '' unless (eval { require Digest::MD5; 1; });
1009 } elsif ($git_avatar eq 'picon') {
1010 # no dependencies
1011 } else {
1012 $git_avatar = '';
1016 # custom error handler: 'die <message>' is Internal Server Error
1017 sub handle_errors_html {
1018 my $msg = shift; # it is already HTML escaped
1020 # to avoid infinite loop where error occurs in die_error,
1021 # change handler to default handler, disabling handle_errors_html
1022 set_message("Error occured when inside die_error:\n$msg");
1024 # you cannot jump out of die_error when called as error handler;
1025 # the subroutine set via CGI::Carp::set_message is called _after_
1026 # HTTP headers are already written, so it cannot write them itself
1027 die_error(undef, undef, $msg, -error_handler => 1, -no_http_header => 1);
1029 set_message(\&handle_errors_html);
1031 # dispatch
1032 sub dispatch {
1033 if (!defined $action) {
1034 if (defined $hash) {
1035 $action = git_get_type($hash);
1036 } elsif (defined $hash_base && defined $file_name) {
1037 $action = git_get_type("$hash_base:$file_name");
1038 } elsif (defined $project) {
1039 $action = 'summary';
1040 } else {
1041 $action = 'project_list';
1044 if (!defined($actions{$action})) {
1045 die_error(400, "Unknown action");
1047 if ($action !~ m/^(?:opml|project_list|project_index)$/ &&
1048 !$project) {
1049 die_error(400, "Project needed");
1051 $actions{$action}->();
1054 sub reset_timer {
1055 our $t0 = [Time::HiRes::gettimeofday()]
1056 if defined $t0;
1057 our $number_of_git_cmds = 0;
1060 sub run_request {
1061 reset_timer();
1063 evaluate_uri();
1064 evaluate_gitweb_config();
1065 check_loadavg();
1067 # $projectroot and $projects_list might be set in gitweb config file
1068 $projects_list ||= $projectroot;
1070 evaluate_query_params();
1071 evaluate_path_info();
1072 evaluate_and_validate_params();
1073 evaluate_git_dir();
1075 configure_gitweb_features();
1077 dispatch();
1080 our $is_last_request = sub { 1 };
1081 our ($pre_dispatch_hook, $post_dispatch_hook, $pre_listen_hook);
1082 our $CGI = 'CGI';
1083 our $cgi;
1084 sub configure_as_fcgi {
1085 require CGI::Fast;
1086 our $CGI = 'CGI::Fast';
1088 my $request_number = 0;
1089 # let each child service 100 requests
1090 our $is_last_request = sub { ++$request_number > 100 };
1092 sub evaluate_argv {
1093 my $script_name = $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || __FILE__;
1094 configure_as_fcgi()
1095 if $script_name =~ /\.fcgi$/;
1097 return unless (@ARGV);
1099 require Getopt::Long;
1100 Getopt::Long::GetOptions(
1101 'fastcgi|fcgi|f' => \&configure_as_fcgi,
1102 'nproc|n=i' => sub {
1103 my ($arg, $val) = @_;
1104 return unless eval { require FCGI::ProcManager; 1; };
1105 my $proc_manager = FCGI::ProcManager->new({
1106 n_processes => $val,
1108 our $pre_listen_hook = sub { $proc_manager->pm_manage() };
1109 our $pre_dispatch_hook = sub { $proc_manager->pm_pre_dispatch() };
1110 our $post_dispatch_hook = sub { $proc_manager->pm_post_dispatch() };
1115 sub run {
1116 evaluate_argv();
1117 evaluate_git_version();
1119 $pre_listen_hook->()
1120 if $pre_listen_hook;
1122 REQUEST:
1123 while ($cgi = $CGI->new()) {
1124 $pre_dispatch_hook->()
1125 if $pre_dispatch_hook;
1127 run_request();
1129 $post_dispatch_hook->()
1130 if $post_dispatch_hook;
1132 last REQUEST if ($is_last_request->());
1135 DONE_GITWEB:
1139 run();
1141 if (defined caller) {
1142 # wrapped in a subroutine processing requests,
1143 # e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI
1144 return;
1145 } else {
1146 # pure CGI script, serving single request
1147 exit;
1150 ## ======================================================================
1151 ## action links
1153 # possible values of extra options
1154 # -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)
1155 # -replay => 1 - start from a current view (replay with modifications)
1156 # -path_info => 0|1 - don't use/use path_info URL (if possible)
1157 sub href {
1158 my %params = @_;
1159 # default is to use -absolute url() i.e. $my_uri
1160 my $href = $params{-full} ? $my_url : $my_uri;
1162 $params{'project'} = $project unless exists $params{'project'};
1164 if ($params{-replay}) {
1165 while (my ($name, $symbol) = each %cgi_param_mapping) {
1166 if (!exists $params{$name}) {
1167 $params{$name} = $input_params{$name};
1172 my $use_pathinfo = gitweb_check_feature('pathinfo');
1173 if (defined $params{'project'} &&
1174 (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
1175 # try to put as many parameters as possible in PATH_INFO:
1176 # - project name
1177 # - action
1178 # - hash_parent or hash_parent_base:/file_parent
1179 # - hash or hash_base:/filename
1180 # - the snapshot_format as an appropriate suffix
1182 # When the script is the root DirectoryIndex for the domain,
1183 # $href here would be something like http://gitweb.example.com/
1184 # Thus, we strip any trailing / from $href, to spare us double
1185 # slashes in the final URL
1186 $href =~ s,/$,,;
1188 # Then add the project name, if present
1189 $href .= "/".esc_url($params{'project'});
1190 delete $params{'project'};
1192 # since we destructively absorb parameters, we keep this
1193 # boolean that remembers if we're handling a snapshot
1194 my $is_snapshot = $params{'action'} eq 'snapshot';
1196 # Summary just uses the project path URL, any other action is
1197 # added to the URL
1198 if (defined $params{'action'}) {
1199 $href .= "/".esc_url($params{'action'}) unless $params{'action'} eq 'summary';
1200 delete $params{'action'};
1203 # Next, we put hash_parent_base:/file_parent..hash_base:/file_name,
1204 # stripping nonexistent or useless pieces
1205 $href .= "/" if ($params{'hash_base'} || $params{'hash_parent_base'}
1206 || $params{'hash_parent'} || $params{'hash'});
1207 if (defined $params{'hash_base'}) {
1208 if (defined $params{'hash_parent_base'}) {
1209 $href .= esc_url($params{'hash_parent_base'});
1210 # skip the file_parent if it's the same as the file_name
1211 if (defined $params{'file_parent'}) {
1212 if (defined $params{'file_name'} && $params{'file_parent'} eq $params{'file_name'}) {
1213 delete $params{'file_parent'};
1214 } elsif ($params{'file_parent'} !~ /\.\./) {
1215 $href .= ":/".esc_url($params{'file_parent'});
1216 delete $params{'file_parent'};
1219 $href .= "..";
1220 delete $params{'hash_parent'};
1221 delete $params{'hash_parent_base'};
1222 } elsif (defined $params{'hash_parent'}) {
1223 $href .= esc_url($params{'hash_parent'}). "..";
1224 delete $params{'hash_parent'};
1227 $href .= esc_url($params{'hash_base'});
1228 if (defined $params{'file_name'} && $params{'file_name'} !~ /\.\./) {
1229 $href .= ":/".esc_url($params{'file_name'});
1230 delete $params{'file_name'};
1232 delete $params{'hash'};
1233 delete $params{'hash_base'};
1234 } elsif (defined $params{'hash'}) {
1235 $href .= esc_url($params{'hash'});
1236 delete $params{'hash'};
1239 # If the action was a snapshot, we can absorb the
1240 # snapshot_format parameter too
1241 if ($is_snapshot) {
1242 my $fmt = $params{'snapshot_format'};
1243 # snapshot_format should always be defined when href()
1244 # is called, but just in case some code forgets, we
1245 # fall back to the default
1246 $fmt ||= $snapshot_fmts[0];
1247 $href .= $known_snapshot_formats{$fmt}{'suffix'};
1248 delete $params{'snapshot_format'};
1252 # now encode the parameters explicitly
1253 my @result = ();
1254 for (my $i = 0; $i < @cgi_param_mapping; $i += 2) {
1255 my ($name, $symbol) = ($cgi_param_mapping[$i], $cgi_param_mapping[$i+1]);
1256 if (defined $params{$name}) {
1257 if (ref($params{$name}) eq "ARRAY") {
1258 foreach my $par (@{$params{$name}}) {
1259 push @result, $symbol . "=" . esc_param($par);
1261 } else {
1262 push @result, $symbol . "=" . esc_param($params{$name});
1266 $href .= "?" . join(';', @result) if scalar @result;
1268 return $href;
1272 ## ======================================================================
1273 ## validation, quoting/unquoting and escaping
1275 sub validate_action {
1276 my $input = shift || return undef;
1277 return undef unless exists $actions{$input};
1278 return $input;
1281 sub validate_project {
1282 my $input = shift || return undef;
1283 if (!validate_pathname($input) ||
1284 !(-d "$projectroot/$input") ||
1285 !check_export_ok("$projectroot/$input") ||
1286 ($strict_export && !project_in_list($input))) {
1287 return undef;
1288 } else {
1289 return $input;
1293 sub validate_pathname {
1294 my $input = shift || return undef;
1296 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
1297 # at the beginning, at the end, and between slashes.
1298 # also this catches doubled slashes
1299 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
1300 return undef;
1302 # no null characters
1303 if ($input =~ m!\0!) {
1304 return undef;
1306 return $input;
1309 sub validate_refname {
1310 my $input = shift || return undef;
1312 # textual hashes are O.K.
1313 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
1314 return $input;
1316 # it must be correct pathname
1317 $input = validate_pathname($input)
1318 or return undef;
1319 # restrictions on ref name according to git-check-ref-format
1320 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
1321 return undef;
1323 return $input;
1326 # decode sequences of octets in utf8 into Perl's internal form,
1327 # which is utf-8 with utf8 flag set if needed. gitweb writes out
1328 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
1329 sub to_utf8 {
1330 my $str = shift;
1331 return undef unless defined $str;
1332 if (utf8::valid($str)) {
1333 utf8::decode($str);
1334 return $str;
1335 } else {
1336 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
1340 # quote unsafe chars, but keep the slash, even when it's not
1341 # correct, but quoted slashes look too horrible in bookmarks
1342 sub esc_param {
1343 my $str = shift;
1344 return undef unless defined $str;
1345 $str =~ s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;
1346 $str =~ s/ /\+/g;
1347 return $str;
1350 # quote unsafe chars in whole URL, so some characters cannot be quoted
1351 sub esc_url {
1352 my $str = shift;
1353 return undef unless defined $str;
1354 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;
1355 $str =~ s/ /\+/g;
1356 return $str;
1359 # quote unsafe characters in HTML attributes
1360 sub esc_attr {
1362 # for XHTML conformance escaping '"' to '&quot;' is not enough
1363 return esc_html(@_);
1366 # replace invalid utf8 character with SUBSTITUTION sequence
1367 sub esc_html {
1368 my $str = shift;
1369 my %opts = @_;
1371 return undef unless defined $str;
1373 $str = to_utf8($str);
1374 $str = $cgi->escapeHTML($str);
1375 if ($opts{'-nbsp'}) {
1376 $str =~ s/ /&nbsp;/g;
1378 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
1379 return $str;
1382 # quote control characters and escape filename to HTML
1383 sub esc_path {
1384 my $str = shift;
1385 my %opts = @_;
1387 return undef unless defined $str;
1389 $str = to_utf8($str);
1390 $str = $cgi->escapeHTML($str);
1391 if ($opts{'-nbsp'}) {
1392 $str =~ s/ /&nbsp;/g;
1394 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
1395 return $str;
1398 # Make control characters "printable", using character escape codes (CEC)
1399 sub quot_cec {
1400 my $cntrl = shift;
1401 my %opts = @_;
1402 my %es = ( # character escape codes, aka escape sequences
1403 "\t" => '\t', # tab (HT)
1404 "\n" => '\n', # line feed (LF)
1405 "\r" => '\r', # carrige return (CR)
1406 "\f" => '\f', # form feed (FF)
1407 "\b" => '\b', # backspace (BS)
1408 "\a" => '\a', # alarm (bell) (BEL)
1409 "\e" => '\e', # escape (ESC)
1410 "\013" => '\v', # vertical tab (VT)
1411 "\000" => '\0', # nul character (NUL)
1413 my $chr = ( (exists $es{$cntrl})
1414 ? $es{$cntrl}
1415 : sprintf('\%2x', ord($cntrl)) );
1416 if ($opts{-nohtml}) {
1417 return $chr;
1418 } else {
1419 return "<span class=\"cntrl\">$chr</span>";
1423 # Alternatively use unicode control pictures codepoints,
1424 # Unicode "printable representation" (PR)
1425 sub quot_upr {
1426 my $cntrl = shift;
1427 my %opts = @_;
1429 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
1430 if ($opts{-nohtml}) {
1431 return $chr;
1432 } else {
1433 return "<span class=\"cntrl\">$chr</span>";
1437 # git may return quoted and escaped filenames
1438 sub unquote {
1439 my $str = shift;
1441 sub unq {
1442 my $seq = shift;
1443 my %es = ( # character escape codes, aka escape sequences
1444 't' => "\t", # tab (HT, TAB)
1445 'n' => "\n", # newline (NL)
1446 'r' => "\r", # return (CR)
1447 'f' => "\f", # form feed (FF)
1448 'b' => "\b", # backspace (BS)
1449 'a' => "\a", # alarm (bell) (BEL)
1450 'e' => "\e", # escape (ESC)
1451 'v' => "\013", # vertical tab (VT)
1454 if ($seq =~ m/^[0-7]{1,3}$/) {
1455 # octal char sequence
1456 return chr(oct($seq));
1457 } elsif (exists $es{$seq}) {
1458 # C escape sequence, aka character escape code
1459 return $es{$seq};
1461 # quoted ordinary character
1462 return $seq;
1465 if ($str =~ m/^"(.*)"$/) {
1466 # needs unquoting
1467 $str = $1;
1468 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
1470 return $str;
1473 # escape tabs (convert tabs to spaces)
1474 sub untabify {
1475 my $line = shift;
1477 while ((my $pos = index($line, "\t")) != -1) {
1478 if (my $count = (8 - ($pos % 8))) {
1479 my $spaces = ' ' x $count;
1480 $line =~ s/\t/$spaces/;
1484 return $line;
1487 sub project_in_list {
1488 my $project = shift;
1489 my @list = git_get_projects_list();
1490 return @list && scalar(grep { $_->{'path'} eq $project } @list);
1493 ## ----------------------------------------------------------------------
1494 ## HTML aware string manipulation
1496 # Try to chop given string on a word boundary between position
1497 # $len and $len+$add_len. If there is no word boundary there,
1498 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
1499 # (marking chopped part) would be longer than given string.
1500 sub chop_str {
1501 my $str = shift;
1502 my $len = shift;
1503 my $add_len = shift || 10;
1504 my $where = shift || 'right'; # 'left' | 'center' | 'right'
1506 # Make sure perl knows it is utf8 encoded so we don't
1507 # cut in the middle of a utf8 multibyte char.
1508 $str = to_utf8($str);
1510 # allow only $len chars, but don't cut a word if it would fit in $add_len
1511 # if it doesn't fit, cut it if it's still longer than the dots we would add
1512 # remove chopped character entities entirely
1514 # when chopping in the middle, distribute $len into left and right part
1515 # return early if chopping wouldn't make string shorter
1516 if ($where eq 'center') {
1517 return $str if ($len + 5 >= length($str)); # filler is length 5
1518 $len = int($len/2);
1519 } else {
1520 return $str if ($len + 4 >= length($str)); # filler is length 4
1523 # regexps: ending and beginning with word part up to $add_len
1524 my $endre = qr/.{$len}\w{0,$add_len}/;
1525 my $begre = qr/\w{0,$add_len}.{$len}/;
1527 if ($where eq 'left') {
1528 $str =~ m/^(.*?)($begre)$/;
1529 my ($lead, $body) = ($1, $2);
1530 if (length($lead) > 4) {
1531 $lead = " ...";
1533 return "$lead$body";
1535 } elsif ($where eq 'center') {
1536 $str =~ m/^($endre)(.*)$/;
1537 my ($left, $str) = ($1, $2);
1538 $str =~ m/^(.*?)($begre)$/;
1539 my ($mid, $right) = ($1, $2);
1540 if (length($mid) > 5) {
1541 $mid = " ... ";
1543 return "$left$mid$right";
1545 } else {
1546 $str =~ m/^($endre)(.*)$/;
1547 my $body = $1;
1548 my $tail = $2;
1549 if (length($tail) > 4) {
1550 $tail = "... ";
1552 return "$body$tail";
1556 # takes the same arguments as chop_str, but also wraps a <span> around the
1557 # result with a title attribute if it does get chopped. Additionally, the
1558 # string is HTML-escaped.
1559 sub chop_and_escape_str {
1560 my ($str) = @_;
1562 my $chopped = chop_str(@_);
1563 if ($chopped eq $str) {
1564 return esc_html($chopped);
1565 } else {
1566 $str =~ s/[[:cntrl:]]/?/g;
1567 return $cgi->span({-title=>$str}, esc_html($chopped));
1571 ## ----------------------------------------------------------------------
1572 ## functions returning short strings
1574 # CSS class for given age value (in seconds)
1575 sub age_class {
1576 my $age = shift;
1578 if (!defined $age) {
1579 return "noage";
1580 } elsif ($age < 60*60*2) {
1581 return "age0";
1582 } elsif ($age < 60*60*24*2) {
1583 return "age1";
1584 } else {
1585 return "age2";
1589 # convert age in seconds to "nn units ago" string
1590 sub age_string {
1591 my $age = shift;
1592 my $age_str;
1594 if ($age > 60*60*24*365*2) {
1595 $age_str = (int $age/60/60/24/365);
1596 $age_str .= " years ago";
1597 } elsif ($age > 60*60*24*(365/12)*2) {
1598 $age_str = int $age/60/60/24/(365/12);
1599 $age_str .= " months ago";
1600 } elsif ($age > 60*60*24*7*2) {
1601 $age_str = int $age/60/60/24/7;
1602 $age_str .= " weeks ago";
1603 } elsif ($age > 60*60*24*2) {
1604 $age_str = int $age/60/60/24;
1605 $age_str .= " days ago";
1606 } elsif ($age > 60*60*2) {
1607 $age_str = int $age/60/60;
1608 $age_str .= " hours ago";
1609 } elsif ($age > 60*2) {
1610 $age_str = int $age/60;
1611 $age_str .= " min ago";
1612 } elsif ($age > 2) {
1613 $age_str = int $age;
1614 $age_str .= " sec ago";
1615 } else {
1616 $age_str .= " right now";
1618 return $age_str;
1621 use constant {
1622 S_IFINVALID => 0030000,
1623 S_IFGITLINK => 0160000,
1626 # submodule/subproject, a commit object reference
1627 sub S_ISGITLINK {
1628 my $mode = shift;
1630 return (($mode & S_IFMT) == S_IFGITLINK)
1633 # convert file mode in octal to symbolic file mode string
1634 sub mode_str {
1635 my $mode = oct shift;
1637 if (S_ISGITLINK($mode)) {
1638 return 'm---------';
1639 } elsif (S_ISDIR($mode & S_IFMT)) {
1640 return 'drwxr-xr-x';
1641 } elsif (S_ISLNK($mode)) {
1642 return 'lrwxrwxrwx';
1643 } elsif (S_ISREG($mode)) {
1644 # git cares only about the executable bit
1645 if ($mode & S_IXUSR) {
1646 return '-rwxr-xr-x';
1647 } else {
1648 return '-rw-r--r--';
1650 } else {
1651 return '----------';
1655 # convert file mode in octal to file type string
1656 sub file_type {
1657 my $mode = shift;
1659 if ($mode !~ m/^[0-7]+$/) {
1660 return $mode;
1661 } else {
1662 $mode = oct $mode;
1665 if (S_ISGITLINK($mode)) {
1666 return "submodule";
1667 } elsif (S_ISDIR($mode & S_IFMT)) {
1668 return "directory";
1669 } elsif (S_ISLNK($mode)) {
1670 return "symlink";
1671 } elsif (S_ISREG($mode)) {
1672 return "file";
1673 } else {
1674 return "unknown";
1678 # convert file mode in octal to file type description string
1679 sub file_type_long {
1680 my $mode = shift;
1682 if ($mode !~ m/^[0-7]+$/) {
1683 return $mode;
1684 } else {
1685 $mode = oct $mode;
1688 if (S_ISGITLINK($mode)) {
1689 return "submodule";
1690 } elsif (S_ISDIR($mode & S_IFMT)) {
1691 return "directory";
1692 } elsif (S_ISLNK($mode)) {
1693 return "symlink";
1694 } elsif (S_ISREG($mode)) {
1695 if ($mode & S_IXUSR) {
1696 return "executable";
1697 } else {
1698 return "file";
1700 } else {
1701 return "unknown";
1706 ## ----------------------------------------------------------------------
1707 ## functions returning short HTML fragments, or transforming HTML fragments
1708 ## which don't belong to other sections
1710 # format line of commit message.
1711 sub format_log_line_html {
1712 my $line = shift;
1714 $line = esc_html($line, -nbsp=>1);
1715 $line =~ s{\b([0-9a-fA-F]{8,40})\b}{
1716 $cgi->a({-href => href(action=>"object", hash=>$1),
1717 -class => "text"}, $1);
1718 }eg;
1720 return $line;
1723 # format marker of refs pointing to given object
1725 # the destination action is chosen based on object type and current context:
1726 # - for annotated tags, we choose the tag view unless it's the current view
1727 # already, in which case we go to shortlog view
1728 # - for other refs, we keep the current view if we're in history, shortlog or
1729 # log view, and select shortlog otherwise
1730 sub format_ref_marker {
1731 my ($refs, $id) = @_;
1732 my $markers = '';
1734 if (defined $refs->{$id}) {
1735 foreach my $ref (@{$refs->{$id}}) {
1736 # this code exploits the fact that non-lightweight tags are the
1737 # only indirect objects, and that they are the only objects for which
1738 # we want to use tag instead of shortlog as action
1739 my ($type, $name) = qw();
1740 my $indirect = ($ref =~ s/\^\{\}$//);
1741 # e.g. tags/v2.6.11 or heads/next
1742 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1743 $type = $1;
1744 $name = $2;
1745 } else {
1746 $type = "ref";
1747 $name = $ref;
1750 my $class = $type;
1751 $class .= " indirect" if $indirect;
1753 my $dest_action = "shortlog";
1755 if ($indirect) {
1756 $dest_action = "tag" unless $action eq "tag";
1757 } elsif ($action =~ /^(history|(short)?log)$/) {
1758 $dest_action = $action;
1761 my $dest = "";
1762 $dest .= "refs/" unless $ref =~ m!^refs/!;
1763 $dest .= $ref;
1765 my $link = $cgi->a({
1766 -href => href(
1767 action=>$dest_action,
1768 hash=>$dest
1769 )}, $name);
1771 $markers .= " <span class=\"".esc_attr($class)."\" title=\"".esc_attr($ref)."\">" .
1772 $link . "</span>";
1776 if ($markers) {
1777 return ' <span class="refs">'. $markers . '</span>';
1778 } else {
1779 return "";
1783 # format, perhaps shortened and with markers, title line
1784 sub format_subject_html {
1785 my ($long, $short, $href, $extra) = @_;
1786 $extra = '' unless defined($extra);
1788 if (length($short) < length($long)) {
1789 $long =~ s/[[:cntrl:]]/?/g;
1790 return $cgi->a({-href => $href, -class => "list subject",
1791 -title => to_utf8($long)},
1792 esc_html($short)) . $extra;
1793 } else {
1794 return $cgi->a({-href => $href, -class => "list subject"},
1795 esc_html($long)) . $extra;
1799 # Rather than recomputing the url for an email multiple times, we cache it
1800 # after the first hit. This gives a visible benefit in views where the avatar
1801 # for the same email is used repeatedly (e.g. shortlog).
1802 # The cache is shared by all avatar engines (currently gravatar only), which
1803 # are free to use it as preferred. Since only one avatar engine is used for any
1804 # given page, there's no risk for cache conflicts.
1805 our %avatar_cache = ();
1807 # Compute the picon url for a given email, by using the picon search service over at
1808 # http://www.cs.indiana.edu/picons/search.html
1809 sub picon_url {
1810 my $email = lc shift;
1811 if (!$avatar_cache{$email}) {
1812 my ($user, $domain) = split('@', $email);
1813 $avatar_cache{$email} =
1814 "http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/" .
1815 "$domain/$user/" .
1816 "users+domains+unknown/up/single";
1818 return $avatar_cache{$email};
1821 # Compute the gravatar url for a given email, if it's not in the cache already.
1822 # Gravatar stores only the part of the URL before the size, since that's the
1823 # one computationally more expensive. This also allows reuse of the cache for
1824 # different sizes (for this particular engine).
1825 sub gravatar_url {
1826 my $email = lc shift;
1827 my $size = shift;
1828 $avatar_cache{$email} ||=
1829 "http://www.gravatar.com/avatar/" .
1830 Digest::MD5::md5_hex($email) . "?s=";
1831 return $avatar_cache{$email} . $size;
1834 # Insert an avatar for the given $email at the given $size if the feature
1835 # is enabled.
1836 sub git_get_avatar {
1837 my ($email, %opts) = @_;
1838 my $pre_white = ($opts{-pad_before} ? "&nbsp;" : "");
1839 my $post_white = ($opts{-pad_after} ? "&nbsp;" : "");
1840 $opts{-size} ||= 'default';
1841 my $size = $avatar_size{$opts{-size}} || $avatar_size{'default'};
1842 my $url = "";
1843 if ($git_avatar eq 'gravatar') {
1844 $url = gravatar_url($email, $size);
1845 } elsif ($git_avatar eq 'picon') {
1846 $url = picon_url($email);
1848 # Other providers can be added by extending the if chain, defining $url
1849 # as needed. If no variant puts something in $url, we assume avatars
1850 # are completely disabled/unavailable.
1851 if ($url) {
1852 return $pre_white .
1853 "<img width=\"$size\" " .
1854 "class=\"avatar\" " .
1855 "src=\"".esc_url($url)."\" " .
1856 "alt=\"\" " .
1857 "/>" . $post_white;
1858 } else {
1859 return "";
1863 sub format_search_author {
1864 my ($author, $searchtype, $displaytext) = @_;
1865 my $have_search = gitweb_check_feature('search');
1867 if ($have_search) {
1868 my $performed = "";
1869 if ($searchtype eq 'author') {
1870 $performed = "authored";
1871 } elsif ($searchtype eq 'committer') {
1872 $performed = "committed";
1875 return $cgi->a({-href => href(action=>"search", hash=>$hash,
1876 searchtext=>$author,
1877 searchtype=>$searchtype), class=>"list",
1878 title=>"Search for commits $performed by $author"},
1879 $displaytext);
1881 } else {
1882 return $displaytext;
1886 # format the author name of the given commit with the given tag
1887 # the author name is chopped and escaped according to the other
1888 # optional parameters (see chop_str).
1889 sub format_author_html {
1890 my $tag = shift;
1891 my $co = shift;
1892 my $author = chop_and_escape_str($co->{'author_name'}, @_);
1893 return "<$tag class=\"author\">" .
1894 format_search_author($co->{'author_name'}, "author",
1895 git_get_avatar($co->{'author_email'}, -pad_after => 1) .
1896 $author) .
1897 "</$tag>";
1900 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1901 sub format_git_diff_header_line {
1902 my $line = shift;
1903 my $diffinfo = shift;
1904 my ($from, $to) = @_;
1906 if ($diffinfo->{'nparents'}) {
1907 # combined diff
1908 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1909 if ($to->{'href'}) {
1910 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1911 esc_path($to->{'file'}));
1912 } else { # file was deleted (no href)
1913 $line .= esc_path($to->{'file'});
1915 } else {
1916 # "ordinary" diff
1917 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1918 if ($from->{'href'}) {
1919 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1920 'a/' . esc_path($from->{'file'}));
1921 } else { # file was added (no href)
1922 $line .= 'a/' . esc_path($from->{'file'});
1924 $line .= ' ';
1925 if ($to->{'href'}) {
1926 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1927 'b/' . esc_path($to->{'file'}));
1928 } else { # file was deleted
1929 $line .= 'b/' . esc_path($to->{'file'});
1933 return "<div class=\"diff header\">$line</div>\n";
1936 # format extended diff header line, before patch itself
1937 sub format_extended_diff_header_line {
1938 my $line = shift;
1939 my $diffinfo = shift;
1940 my ($from, $to) = @_;
1942 # match <path>
1943 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1944 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1945 esc_path($from->{'file'}));
1947 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1948 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1949 esc_path($to->{'file'}));
1951 # match single <mode>
1952 if ($line =~ m/\s(\d{6})$/) {
1953 $line .= '<span class="info"> (' .
1954 file_type_long($1) .
1955 ')</span>';
1957 # match <hash>
1958 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1959 # can match only for combined diff
1960 $line = 'index ';
1961 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1962 if ($from->{'href'}[$i]) {
1963 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1964 -class=>"hash"},
1965 substr($diffinfo->{'from_id'}[$i],0,7));
1966 } else {
1967 $line .= '0' x 7;
1969 # separator
1970 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1972 $line .= '..';
1973 if ($to->{'href'}) {
1974 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1975 substr($diffinfo->{'to_id'},0,7));
1976 } else {
1977 $line .= '0' x 7;
1980 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1981 # can match only for ordinary diff
1982 my ($from_link, $to_link);
1983 if ($from->{'href'}) {
1984 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1985 substr($diffinfo->{'from_id'},0,7));
1986 } else {
1987 $from_link = '0' x 7;
1989 if ($to->{'href'}) {
1990 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1991 substr($diffinfo->{'to_id'},0,7));
1992 } else {
1993 $to_link = '0' x 7;
1995 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1996 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1999 return $line . "<br/>\n";
2002 # format from-file/to-file diff header
2003 sub format_diff_from_to_header {
2004 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
2005 my $line;
2006 my $result = '';
2008 $line = $from_line;
2009 #assert($line =~ m/^---/) if DEBUG;
2010 # no extra formatting for "^--- /dev/null"
2011 if (! $diffinfo->{'nparents'}) {
2012 # ordinary (single parent) diff
2013 if ($line =~ m!^--- "?a/!) {
2014 if ($from->{'href'}) {
2015 $line = '--- a/' .
2016 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
2017 esc_path($from->{'file'}));
2018 } else {
2019 $line = '--- a/' .
2020 esc_path($from->{'file'});
2023 $result .= qq!<div class="diff from_file">$line</div>\n!;
2025 } else {
2026 # combined diff (merge commit)
2027 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2028 if ($from->{'href'}[$i]) {
2029 $line = '--- ' .
2030 $cgi->a({-href=>href(action=>"blobdiff",
2031 hash_parent=>$diffinfo->{'from_id'}[$i],
2032 hash_parent_base=>$parents[$i],
2033 file_parent=>$from->{'file'}[$i],
2034 hash=>$diffinfo->{'to_id'},
2035 hash_base=>$hash,
2036 file_name=>$to->{'file'}),
2037 -class=>"path",
2038 -title=>"diff" . ($i+1)},
2039 $i+1) .
2040 '/' .
2041 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
2042 esc_path($from->{'file'}[$i]));
2043 } else {
2044 $line = '--- /dev/null';
2046 $result .= qq!<div class="diff from_file">$line</div>\n!;
2050 $line = $to_line;
2051 #assert($line =~ m/^\+\+\+/) if DEBUG;
2052 # no extra formatting for "^+++ /dev/null"
2053 if ($line =~ m!^\+\+\+ "?b/!) {
2054 if ($to->{'href'}) {
2055 $line = '+++ b/' .
2056 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
2057 esc_path($to->{'file'}));
2058 } else {
2059 $line = '+++ b/' .
2060 esc_path($to->{'file'});
2063 $result .= qq!<div class="diff to_file">$line</div>\n!;
2065 return $result;
2068 # create note for patch simplified by combined diff
2069 sub format_diff_cc_simplified {
2070 my ($diffinfo, @parents) = @_;
2071 my $result = '';
2073 $result .= "<div class=\"diff header\">" .
2074 "diff --cc ";
2075 if (!is_deleted($diffinfo)) {
2076 $result .= $cgi->a({-href => href(action=>"blob",
2077 hash_base=>$hash,
2078 hash=>$diffinfo->{'to_id'},
2079 file_name=>$diffinfo->{'to_file'}),
2080 -class => "path"},
2081 esc_path($diffinfo->{'to_file'}));
2082 } else {
2083 $result .= esc_path($diffinfo->{'to_file'});
2085 $result .= "</div>\n" . # class="diff header"
2086 "<div class=\"diff nodifferences\">" .
2087 "Simple merge" .
2088 "</div>\n"; # class="diff nodifferences"
2090 return $result;
2093 # format patch (diff) line (not to be used for diff headers)
2094 sub format_diff_line {
2095 my $line = shift;
2096 my ($from, $to) = @_;
2097 my $diff_class = "";
2099 chomp $line;
2101 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
2102 # combined diff
2103 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
2104 if ($line =~ m/^\@{3}/) {
2105 $diff_class = " chunk_header";
2106 } elsif ($line =~ m/^\\/) {
2107 $diff_class = " incomplete";
2108 } elsif ($prefix =~ tr/+/+/) {
2109 $diff_class = " add";
2110 } elsif ($prefix =~ tr/-/-/) {
2111 $diff_class = " rem";
2113 } else {
2114 # assume ordinary diff
2115 my $char = substr($line, 0, 1);
2116 if ($char eq '+') {
2117 $diff_class = " add";
2118 } elsif ($char eq '-') {
2119 $diff_class = " rem";
2120 } elsif ($char eq '@') {
2121 $diff_class = " chunk_header";
2122 } elsif ($char eq "\\") {
2123 $diff_class = " incomplete";
2126 $line = untabify($line);
2127 if ($from && $to && $line =~ m/^\@{2} /) {
2128 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
2129 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
2131 $from_lines = 0 unless defined $from_lines;
2132 $to_lines = 0 unless defined $to_lines;
2134 if ($from->{'href'}) {
2135 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
2136 -class=>"list"}, $from_text);
2138 if ($to->{'href'}) {
2139 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
2140 -class=>"list"}, $to_text);
2142 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
2143 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2144 return "<div class=\"diff$diff_class\">$line</div>\n";
2145 } elsif ($from && $to && $line =~ m/^\@{3}/) {
2146 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
2147 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
2149 @from_text = split(' ', $ranges);
2150 for (my $i = 0; $i < @from_text; ++$i) {
2151 ($from_start[$i], $from_nlines[$i]) =
2152 (split(',', substr($from_text[$i], 1)), 0);
2155 $to_text = pop @from_text;
2156 $to_start = pop @from_start;
2157 $to_nlines = pop @from_nlines;
2159 $line = "<span class=\"chunk_info\">$prefix ";
2160 for (my $i = 0; $i < @from_text; ++$i) {
2161 if ($from->{'href'}[$i]) {
2162 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
2163 -class=>"list"}, $from_text[$i]);
2164 } else {
2165 $line .= $from_text[$i];
2167 $line .= " ";
2169 if ($to->{'href'}) {
2170 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
2171 -class=>"list"}, $to_text);
2172 } else {
2173 $line .= $to_text;
2175 $line .= " $prefix</span>" .
2176 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
2177 return "<div class=\"diff$diff_class\">$line</div>\n";
2179 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
2182 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
2183 # linked. Pass the hash of the tree/commit to snapshot.
2184 sub format_snapshot_links {
2185 my ($hash) = @_;
2186 my $num_fmts = @snapshot_fmts;
2187 if ($num_fmts > 1) {
2188 # A parenthesized list of links bearing format names.
2189 # e.g. "snapshot (_tar.gz_ _zip_)"
2190 return "snapshot (" . join(' ', map
2191 $cgi->a({
2192 -href => href(
2193 action=>"snapshot",
2194 hash=>$hash,
2195 snapshot_format=>$_
2197 }, $known_snapshot_formats{$_}{'display'})
2198 , @snapshot_fmts) . ")";
2199 } elsif ($num_fmts == 1) {
2200 # A single "snapshot" link whose tooltip bears the format name.
2201 # i.e. "_snapshot_"
2202 my ($fmt) = @snapshot_fmts;
2203 return
2204 $cgi->a({
2205 -href => href(
2206 action=>"snapshot",
2207 hash=>$hash,
2208 snapshot_format=>$fmt
2210 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
2211 }, "snapshot");
2212 } else { # $num_fmts == 0
2213 return undef;
2217 ## ......................................................................
2218 ## functions returning values to be passed, perhaps after some
2219 ## transformation, to other functions; e.g. returning arguments to href()
2221 # returns hash to be passed to href to generate gitweb URL
2222 # in -title key it returns description of link
2223 sub get_feed_info {
2224 my $format = shift || 'Atom';
2225 my %res = (action => lc($format));
2227 # feed links are possible only for project views
2228 return unless (defined $project);
2229 # some views should link to OPML, or to generic project feed,
2230 # or don't have specific feed yet (so they should use generic)
2231 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
2233 my $branch;
2234 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
2235 # from tag links; this also makes possible to detect branch links
2236 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
2237 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
2238 $branch = $1;
2240 # find log type for feed description (title)
2241 my $type = 'log';
2242 if (defined $file_name) {
2243 $type = "history of $file_name";
2244 $type .= "/" if ($action eq 'tree');
2245 $type .= " on '$branch'" if (defined $branch);
2246 } else {
2247 $type = "log of $branch" if (defined $branch);
2250 $res{-title} = $type;
2251 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
2252 $res{'file_name'} = $file_name;
2254 return %res;
2257 ## ----------------------------------------------------------------------
2258 ## git utility subroutines, invoking git commands
2260 # returns path to the core git executable and the --git-dir parameter as list
2261 sub git_cmd {
2262 $number_of_git_cmds++;
2263 return $GIT, '--git-dir='.$git_dir;
2266 # quote the given arguments for passing them to the shell
2267 # quote_command("command", "arg 1", "arg with ' and ! characters")
2268 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
2269 # Try to avoid using this function wherever possible.
2270 sub quote_command {
2271 return join(' ',
2272 map { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ );
2275 # get HEAD ref of given project as hash
2276 sub git_get_head_hash {
2277 return git_get_full_hash(shift, 'HEAD');
2280 sub git_get_full_hash {
2281 return git_get_hash(@_);
2284 sub git_get_short_hash {
2285 return git_get_hash(@_, '--short=7');
2288 sub git_get_hash {
2289 my ($project, $hash, @options) = @_;
2290 my $o_git_dir = $git_dir;
2291 my $retval = undef;
2292 $git_dir = "$projectroot/$project";
2293 if (open my $fd, '-|', git_cmd(), 'rev-parse',
2294 '--verify', '-q', @options, $hash) {
2295 $retval = <$fd>;
2296 chomp $retval if defined $retval;
2297 close $fd;
2299 if (defined $o_git_dir) {
2300 $git_dir = $o_git_dir;
2302 return $retval;
2305 # get type of given object
2306 sub git_get_type {
2307 my $hash = shift;
2309 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
2310 my $type = <$fd>;
2311 close $fd or return;
2312 chomp $type;
2313 return $type;
2316 # repository configuration
2317 our $config_file = '';
2318 our %config;
2320 # store multiple values for single key as anonymous array reference
2321 # single values stored directly in the hash, not as [ <value> ]
2322 sub hash_set_multi {
2323 my ($hash, $key, $value) = @_;
2325 if (!exists $hash->{$key}) {
2326 $hash->{$key} = $value;
2327 } elsif (!ref $hash->{$key}) {
2328 $hash->{$key} = [ $hash->{$key}, $value ];
2329 } else {
2330 push @{$hash->{$key}}, $value;
2334 # return hash of git project configuration
2335 # optionally limited to some section, e.g. 'gitweb'
2336 sub git_parse_project_config {
2337 my $section_regexp = shift;
2338 my %config;
2340 local $/ = "\0";
2342 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
2343 or return;
2345 while (my $keyval = <$fh>) {
2346 chomp $keyval;
2347 my ($key, $value) = split(/\n/, $keyval, 2);
2349 hash_set_multi(\%config, $key, $value)
2350 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
2352 close $fh;
2354 return %config;
2357 # convert config value to boolean: 'true' or 'false'
2358 # no value, number > 0, 'true' and 'yes' values are true
2359 # rest of values are treated as false (never as error)
2360 sub config_to_bool {
2361 my $val = shift;
2363 return 1 if !defined $val; # section.key
2365 # strip leading and trailing whitespace
2366 $val =~ s/^\s+//;
2367 $val =~ s/\s+$//;
2369 return (($val =~ /^\d+$/ && $val) || # section.key = 1
2370 ($val =~ /^(?:true|yes)$/i)); # section.key = true
2373 # convert config value to simple decimal number
2374 # an optional value suffix of 'k', 'm', or 'g' will cause the value
2375 # to be multiplied by 1024, 1048576, or 1073741824
2376 sub config_to_int {
2377 my $val = shift;
2379 # strip leading and trailing whitespace
2380 $val =~ s/^\s+//;
2381 $val =~ s/\s+$//;
2383 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
2384 $unit = lc($unit);
2385 # unknown unit is treated as 1
2386 return $num * ($unit eq 'g' ? 1073741824 :
2387 $unit eq 'm' ? 1048576 :
2388 $unit eq 'k' ? 1024 : 1);
2390 return $val;
2393 # convert config value to array reference, if needed
2394 sub config_to_multi {
2395 my $val = shift;
2397 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
2400 sub git_get_project_config {
2401 my ($key, $type) = @_;
2403 return unless defined $git_dir;
2405 # key sanity check
2406 return unless ($key);
2407 $key =~ s/^gitweb\.//;
2408 return if ($key =~ m/\W/);
2410 # type sanity check
2411 if (defined $type) {
2412 $type =~ s/^--//;
2413 $type = undef
2414 unless ($type eq 'bool' || $type eq 'int');
2417 # get config
2418 if (!defined $config_file ||
2419 $config_file ne "$git_dir/config") {
2420 %config = git_parse_project_config('gitweb');
2421 $config_file = "$git_dir/config";
2424 # check if config variable (key) exists
2425 return unless exists $config{"gitweb.$key"};
2427 # ensure given type
2428 if (!defined $type) {
2429 return $config{"gitweb.$key"};
2430 } elsif ($type eq 'bool') {
2431 # backward compatibility: 'git config --bool' returns true/false
2432 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
2433 } elsif ($type eq 'int') {
2434 return config_to_int($config{"gitweb.$key"});
2436 return $config{"gitweb.$key"};
2439 # get hash of given path at given ref
2440 sub git_get_hash_by_path {
2441 my $base = shift;
2442 my $path = shift || return undef;
2443 my $type = shift;
2445 $path =~ s,/+$,,;
2447 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
2448 or die_error(500, "Open git-ls-tree failed");
2449 my $line = <$fd>;
2450 close $fd or return undef;
2452 if (!defined $line) {
2453 # there is no tree or hash given by $path at $base
2454 return undef;
2457 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2458 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
2459 if (defined $type && $type ne $2) {
2460 # type doesn't match
2461 return undef;
2463 return $3;
2466 # get path of entry with given hash at given tree-ish (ref)
2467 # used to get 'from' filename for combined diff (merge commit) for renames
2468 sub git_get_path_by_hash {
2469 my $base = shift || return;
2470 my $hash = shift || return;
2472 local $/ = "\0";
2474 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
2475 or return undef;
2476 while (my $line = <$fd>) {
2477 chomp $line;
2479 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
2480 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
2481 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
2482 close $fd;
2483 return $1;
2486 close $fd;
2487 return undef;
2490 ## ......................................................................
2491 ## git utility functions, directly accessing git repository
2493 sub git_get_project_description {
2494 my $path = shift;
2496 $git_dir = "$projectroot/$path";
2497 open my $fd, '<', "$git_dir/description"
2498 or return git_get_project_config('description');
2499 my $descr = <$fd>;
2500 close $fd;
2501 if (defined $descr) {
2502 chomp $descr;
2504 return $descr;
2507 sub git_get_project_ctags {
2508 my $path = shift;
2509 my $ctags = {};
2511 $git_dir = "$projectroot/$path";
2512 opendir my $dh, "$git_dir/ctags"
2513 or return $ctags;
2514 foreach (grep { -f $_ } map { "$git_dir/ctags/$_" } readdir($dh)) {
2515 open my $ct, '<', $_ or next;
2516 my $val = <$ct>;
2517 chomp $val;
2518 close $ct;
2519 my $ctag = $_; $ctag =~ s#.*/##;
2520 $ctags->{$ctag} = $val;
2522 closedir $dh;
2523 $ctags;
2526 sub git_populate_project_tagcloud {
2527 my $ctags = shift;
2529 # First, merge different-cased tags; tags vote on casing
2530 my %ctags_lc;
2531 foreach (keys %$ctags) {
2532 $ctags_lc{lc $_}->{count} += $ctags->{$_};
2533 if (not $ctags_lc{lc $_}->{topcount}
2534 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
2535 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
2536 $ctags_lc{lc $_}->{topname} = $_;
2540 my $cloud;
2541 if (eval { require HTML::TagCloud; 1; }) {
2542 $cloud = HTML::TagCloud->new;
2543 foreach (sort keys %ctags_lc) {
2544 # Pad the title with spaces so that the cloud looks
2545 # less crammed.
2546 my $title = $ctags_lc{$_}->{topname};
2547 $title =~ s/ /&nbsp;/g;
2548 $title =~ s/^/&nbsp;/g;
2549 $title =~ s/$/&nbsp;/g;
2550 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
2552 } else {
2553 $cloud = \%ctags_lc;
2555 $cloud;
2558 sub git_show_project_tagcloud {
2559 my ($cloud, $count) = @_;
2560 print STDERR ref($cloud)."..\n";
2561 if (ref $cloud eq 'HTML::TagCloud') {
2562 return $cloud->html_and_css($count);
2563 } else {
2564 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
2565 return '<p align="center">' . join (', ', map {
2566 $cgi->a({-href=>"$home_link?by_tag=$_"}, $cloud->{$_}->{topname})
2567 } splice(@tags, 0, $count)) . '</p>';
2571 sub git_get_project_url_list {
2572 my $path = shift;
2574 $git_dir = "$projectroot/$path";
2575 open my $fd, '<', "$git_dir/cloneurl"
2576 or return wantarray ?
2577 @{ config_to_multi(git_get_project_config('url')) } :
2578 config_to_multi(git_get_project_config('url'));
2579 my @git_project_url_list = map { chomp; $_ } <$fd>;
2580 close $fd;
2582 return wantarray ? @git_project_url_list : \@git_project_url_list;
2585 sub git_get_projects_list {
2586 my ($filter) = @_;
2587 my @list;
2589 $filter ||= '';
2590 $filter =~ s/\.git$//;
2592 my $check_forks = gitweb_check_feature('forks');
2594 if (-d $projects_list) {
2595 # search in directory
2596 my $dir = $projects_list . ($filter ? "/$filter" : '');
2597 # remove the trailing "/"
2598 $dir =~ s!/+$!!;
2599 my $pfxlen = length("$dir");
2600 my $pfxdepth = ($dir =~ tr!/!!);
2602 File::Find::find({
2603 follow_fast => 1, # follow symbolic links
2604 follow_skip => 2, # ignore duplicates
2605 dangling_symlinks => 0, # ignore dangling symlinks, silently
2606 wanted => sub {
2607 # global variables
2608 our $project_maxdepth;
2609 our $projectroot;
2610 # skip project-list toplevel, if we get it.
2611 return if (m!^[/.]$!);
2612 # only directories can be git repositories
2613 return unless (-d $_);
2614 # don't traverse too deep (Find is super slow on os x)
2615 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
2616 $File::Find::prune = 1;
2617 return;
2620 my $subdir = substr($File::Find::name, $pfxlen + 1);
2621 # we check related file in $projectroot
2622 my $path = ($filter ? "$filter/" : '') . $subdir;
2623 if (check_export_ok("$projectroot/$path")) {
2624 push @list, { path => $path };
2625 $File::Find::prune = 1;
2628 }, "$dir");
2630 } elsif (-f $projects_list) {
2631 # read from file(url-encoded):
2632 # 'git%2Fgit.git Linus+Torvalds'
2633 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2634 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2635 my %paths;
2636 open my $fd, '<', $projects_list or return;
2637 PROJECT:
2638 while (my $line = <$fd>) {
2639 chomp $line;
2640 my ($path, $owner) = split ' ', $line;
2641 $path = unescape($path);
2642 $owner = unescape($owner);
2643 if (!defined $path) {
2644 next;
2646 if ($filter ne '') {
2647 # looking for forks;
2648 my $pfx = substr($path, 0, length($filter));
2649 if ($pfx ne $filter) {
2650 next PROJECT;
2652 my $sfx = substr($path, length($filter));
2653 if ($sfx !~ /^\/.*\.git$/) {
2654 next PROJECT;
2656 } elsif ($check_forks) {
2657 PATH:
2658 foreach my $filter (keys %paths) {
2659 # looking for forks;
2660 my $pfx = substr($path, 0, length($filter));
2661 if ($pfx ne $filter) {
2662 next PATH;
2664 my $sfx = substr($path, length($filter));
2665 if ($sfx !~ /^\/.*\.git$/) {
2666 next PATH;
2668 # is a fork, don't include it in
2669 # the list
2670 next PROJECT;
2673 if (check_export_ok("$projectroot/$path")) {
2674 my $pr = {
2675 path => $path,
2676 owner => to_utf8($owner),
2678 push @list, $pr;
2679 (my $forks_path = $path) =~ s/\.git$//;
2680 $paths{$forks_path}++;
2683 close $fd;
2685 return @list;
2688 our $gitweb_project_owner = undef;
2689 sub git_get_project_list_from_file {
2691 return if (defined $gitweb_project_owner);
2693 $gitweb_project_owner = {};
2694 # read from file (url-encoded):
2695 # 'git%2Fgit.git Linus+Torvalds'
2696 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2697 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2698 if (-f $projects_list) {
2699 open(my $fd, '<', $projects_list);
2700 while (my $line = <$fd>) {
2701 chomp $line;
2702 my ($pr, $ow) = split ' ', $line;
2703 $pr = unescape($pr);
2704 $ow = unescape($ow);
2705 $gitweb_project_owner->{$pr} = to_utf8($ow);
2707 close $fd;
2711 sub git_get_project_owner {
2712 my $project = shift;
2713 my $owner;
2715 return undef unless $project;
2716 $git_dir = "$projectroot/$project";
2718 if (!defined $gitweb_project_owner) {
2719 git_get_project_list_from_file();
2722 if (exists $gitweb_project_owner->{$project}) {
2723 $owner = $gitweb_project_owner->{$project};
2725 if (!defined $owner){
2726 $owner = git_get_project_config('owner');
2728 if (!defined $owner) {
2729 $owner = get_file_owner("$git_dir");
2732 return $owner;
2735 sub git_get_last_activity {
2736 my ($path) = @_;
2737 my $fd;
2739 $git_dir = "$projectroot/$path";
2740 open($fd, "-|", git_cmd(), 'for-each-ref',
2741 '--format=%(committer)',
2742 '--sort=-committerdate',
2743 '--count=1',
2744 'refs/heads') or return;
2745 my $most_recent = <$fd>;
2746 close $fd or return;
2747 if (defined $most_recent &&
2748 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2749 my $timestamp = $1;
2750 my $age = time - $timestamp;
2751 return ($age, age_string($age));
2753 return (undef, undef);
2756 sub git_get_references {
2757 my $type = shift || "";
2758 my %refs;
2759 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2760 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2761 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2762 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2763 or return;
2765 while (my $line = <$fd>) {
2766 chomp $line;
2767 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2768 if (defined $refs{$1}) {
2769 push @{$refs{$1}}, $2;
2770 } else {
2771 $refs{$1} = [ $2 ];
2775 close $fd or return;
2776 return \%refs;
2779 sub git_get_rev_name_tags {
2780 my $hash = shift || return undef;
2782 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2783 or return;
2784 my $name_rev = <$fd>;
2785 close $fd;
2787 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2788 return $1;
2789 } else {
2790 # catches also '$hash undefined' output
2791 return undef;
2795 ## ----------------------------------------------------------------------
2796 ## parse to hash functions
2798 sub parse_date {
2799 my $epoch = shift;
2800 my $tz = shift || "-0000";
2802 my %date;
2803 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2804 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2805 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2806 $date{'hour'} = $hour;
2807 $date{'minute'} = $min;
2808 $date{'mday'} = $mday;
2809 $date{'day'} = $days[$wday];
2810 $date{'month'} = $months[$mon];
2811 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2812 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2813 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2814 $mday, $months[$mon], $hour ,$min;
2815 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2816 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2818 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2819 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2820 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2821 $date{'hour_local'} = $hour;
2822 $date{'minute_local'} = $min;
2823 $date{'tz_local'} = $tz;
2824 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2825 1900+$year, $mon+1, $mday,
2826 $hour, $min, $sec, $tz);
2827 return %date;
2830 sub parse_tag {
2831 my $tag_id = shift;
2832 my %tag;
2833 my @comment;
2835 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2836 $tag{'id'} = $tag_id;
2837 while (my $line = <$fd>) {
2838 chomp $line;
2839 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2840 $tag{'object'} = $1;
2841 } elsif ($line =~ m/^type (.+)$/) {
2842 $tag{'type'} = $1;
2843 } elsif ($line =~ m/^tag (.+)$/) {
2844 $tag{'name'} = $1;
2845 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2846 $tag{'author'} = $1;
2847 $tag{'author_epoch'} = $2;
2848 $tag{'author_tz'} = $3;
2849 if ($tag{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2850 $tag{'author_name'} = $1;
2851 $tag{'author_email'} = $2;
2852 } else {
2853 $tag{'author_name'} = $tag{'author'};
2855 } elsif ($line =~ m/--BEGIN/) {
2856 push @comment, $line;
2857 last;
2858 } elsif ($line eq "") {
2859 last;
2862 push @comment, <$fd>;
2863 $tag{'comment'} = \@comment;
2864 close $fd or return;
2865 if (!defined $tag{'name'}) {
2866 return
2868 return %tag
2871 sub parse_commit_text {
2872 my ($commit_text, $withparents) = @_;
2873 my @commit_lines = split '\n', $commit_text;
2874 my %co;
2876 pop @commit_lines; # Remove '\0'
2878 if (! @commit_lines) {
2879 return;
2882 my $header = shift @commit_lines;
2883 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2884 return;
2886 ($co{'id'}, my @parents) = split ' ', $header;
2887 while (my $line = shift @commit_lines) {
2888 last if $line eq "\n";
2889 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2890 $co{'tree'} = $1;
2891 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2892 push @parents, $1;
2893 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2894 $co{'author'} = to_utf8($1);
2895 $co{'author_epoch'} = $2;
2896 $co{'author_tz'} = $3;
2897 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2898 $co{'author_name'} = $1;
2899 $co{'author_email'} = $2;
2900 } else {
2901 $co{'author_name'} = $co{'author'};
2903 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2904 $co{'committer'} = to_utf8($1);
2905 $co{'committer_epoch'} = $2;
2906 $co{'committer_tz'} = $3;
2907 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2908 $co{'committer_name'} = $1;
2909 $co{'committer_email'} = $2;
2910 } else {
2911 $co{'committer_name'} = $co{'committer'};
2915 if (!defined $co{'tree'}) {
2916 return;
2918 $co{'parents'} = \@parents;
2919 $co{'parent'} = $parents[0];
2921 foreach my $title (@commit_lines) {
2922 $title =~ s/^ //;
2923 if ($title ne "") {
2924 $co{'title'} = chop_str($title, 80, 5);
2925 # remove leading stuff of merges to make the interesting part visible
2926 if (length($title) > 50) {
2927 $title =~ s/^Automatic //;
2928 $title =~ s/^merge (of|with) /Merge ... /i;
2929 if (length($title) > 50) {
2930 $title =~ s/(http|rsync):\/\///;
2932 if (length($title) > 50) {
2933 $title =~ s/(master|www|rsync)\.//;
2935 if (length($title) > 50) {
2936 $title =~ s/kernel.org:?//;
2938 if (length($title) > 50) {
2939 $title =~ s/\/pub\/scm//;
2942 $co{'title_short'} = chop_str($title, 50, 5);
2943 last;
2946 if (! defined $co{'title'} || $co{'title'} eq "") {
2947 $co{'title'} = $co{'title_short'} = '(no commit message)';
2949 # remove added spaces
2950 foreach my $line (@commit_lines) {
2951 $line =~ s/^ //;
2953 $co{'comment'} = \@commit_lines;
2955 my $age = time - $co{'committer_epoch'};
2956 $co{'age'} = $age;
2957 $co{'age_string'} = age_string($age);
2958 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2959 if ($age > 60*60*24*7*2) {
2960 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2961 $co{'age_string_age'} = $co{'age_string'};
2962 } else {
2963 $co{'age_string_date'} = $co{'age_string'};
2964 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2966 return %co;
2969 sub parse_commit {
2970 my ($commit_id) = @_;
2971 my %co;
2973 local $/ = "\0";
2975 open my $fd, "-|", git_cmd(), "rev-list",
2976 "--parents",
2977 "--header",
2978 "--max-count=1",
2979 $commit_id,
2980 "--",
2981 or die_error(500, "Open git-rev-list failed");
2982 %co = parse_commit_text(<$fd>, 1);
2983 close $fd;
2985 return %co;
2988 sub parse_commits {
2989 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2990 my @cos;
2992 $maxcount ||= 1;
2993 $skip ||= 0;
2995 local $/ = "\0";
2997 open my $fd, "-|", git_cmd(), "rev-list",
2998 "--header",
2999 @args,
3000 ("--max-count=" . $maxcount),
3001 ("--skip=" . $skip),
3002 @extra_options,
3003 $commit_id,
3004 "--",
3005 ($filename ? ($filename) : ())
3006 or die_error(500, "Open git-rev-list failed");
3007 while (my $line = <$fd>) {
3008 my %co = parse_commit_text($line);
3009 push @cos, \%co;
3011 close $fd;
3013 return wantarray ? @cos : \@cos;
3016 # parse line of git-diff-tree "raw" output
3017 sub parse_difftree_raw_line {
3018 my $line = shift;
3019 my %res;
3021 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
3022 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
3023 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
3024 $res{'from_mode'} = $1;
3025 $res{'to_mode'} = $2;
3026 $res{'from_id'} = $3;
3027 $res{'to_id'} = $4;
3028 $res{'status'} = $5;
3029 $res{'similarity'} = $6;
3030 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
3031 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
3032 } else {
3033 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
3036 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
3037 # combined diff (for merge commit)
3038 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
3039 $res{'nparents'} = length($1);
3040 $res{'from_mode'} = [ split(' ', $2) ];
3041 $res{'to_mode'} = pop @{$res{'from_mode'}};
3042 $res{'from_id'} = [ split(' ', $3) ];
3043 $res{'to_id'} = pop @{$res{'from_id'}};
3044 $res{'status'} = [ split('', $4) ];
3045 $res{'to_file'} = unquote($5);
3047 # 'c512b523472485aef4fff9e57b229d9d243c967f'
3048 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
3049 $res{'commit'} = $1;
3052 return wantarray ? %res : \%res;
3055 # wrapper: return parsed line of git-diff-tree "raw" output
3056 # (the argument might be raw line, or parsed info)
3057 sub parsed_difftree_line {
3058 my $line_or_ref = shift;
3060 if (ref($line_or_ref) eq "HASH") {
3061 # pre-parsed (or generated by hand)
3062 return $line_or_ref;
3063 } else {
3064 return parse_difftree_raw_line($line_or_ref);
3068 # parse line of git-ls-tree output
3069 sub parse_ls_tree_line {
3070 my $line = shift;
3071 my %opts = @_;
3072 my %res;
3074 if ($opts{'-l'}) {
3075 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'
3076 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
3078 $res{'mode'} = $1;
3079 $res{'type'} = $2;
3080 $res{'hash'} = $3;
3081 $res{'size'} = $4;
3082 if ($opts{'-z'}) {
3083 $res{'name'} = $5;
3084 } else {
3085 $res{'name'} = unquote($5);
3087 } else {
3088 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
3089 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
3091 $res{'mode'} = $1;
3092 $res{'type'} = $2;
3093 $res{'hash'} = $3;
3094 if ($opts{'-z'}) {
3095 $res{'name'} = $4;
3096 } else {
3097 $res{'name'} = unquote($4);
3101 return wantarray ? %res : \%res;
3104 # generates _two_ hashes, references to which are passed as 2 and 3 argument
3105 sub parse_from_to_diffinfo {
3106 my ($diffinfo, $from, $to, @parents) = @_;
3108 if ($diffinfo->{'nparents'}) {
3109 # combined diff
3110 $from->{'file'} = [];
3111 $from->{'href'} = [];
3112 fill_from_file_info($diffinfo, @parents)
3113 unless exists $diffinfo->{'from_file'};
3114 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
3115 $from->{'file'}[$i] =
3116 defined $diffinfo->{'from_file'}[$i] ?
3117 $diffinfo->{'from_file'}[$i] :
3118 $diffinfo->{'to_file'};
3119 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
3120 $from->{'href'}[$i] = href(action=>"blob",
3121 hash_base=>$parents[$i],
3122 hash=>$diffinfo->{'from_id'}[$i],
3123 file_name=>$from->{'file'}[$i]);
3124 } else {
3125 $from->{'href'}[$i] = undef;
3128 } else {
3129 # ordinary (not combined) diff
3130 $from->{'file'} = $diffinfo->{'from_file'};
3131 if ($diffinfo->{'status'} ne "A") { # not new (added) file
3132 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
3133 hash=>$diffinfo->{'from_id'},
3134 file_name=>$from->{'file'});
3135 } else {
3136 delete $from->{'href'};
3140 $to->{'file'} = $diffinfo->{'to_file'};
3141 if (!is_deleted($diffinfo)) { # file exists in result
3142 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
3143 hash=>$diffinfo->{'to_id'},
3144 file_name=>$to->{'file'});
3145 } else {
3146 delete $to->{'href'};
3150 ## ......................................................................
3151 ## parse to array of hashes functions
3153 sub git_get_heads_list {
3154 my $limit = shift;
3155 my @headslist;
3157 open my $fd, '-|', git_cmd(), 'for-each-ref',
3158 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
3159 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
3160 'refs/heads'
3161 or return;
3162 while (my $line = <$fd>) {
3163 my %ref_item;
3165 chomp $line;
3166 my ($refinfo, $committerinfo) = split(/\0/, $line);
3167 my ($hash, $name, $title) = split(' ', $refinfo, 3);
3168 my ($committer, $epoch, $tz) =
3169 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
3170 $ref_item{'fullname'} = $name;
3171 $name =~ s!^refs/heads/!!;
3173 $ref_item{'name'} = $name;
3174 $ref_item{'id'} = $hash;
3175 $ref_item{'title'} = $title || '(no commit message)';
3176 $ref_item{'epoch'} = $epoch;
3177 if ($epoch) {
3178 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3179 } else {
3180 $ref_item{'age'} = "unknown";
3183 push @headslist, \%ref_item;
3185 close $fd;
3187 return wantarray ? @headslist : \@headslist;
3190 sub git_get_tags_list {
3191 my $limit = shift;
3192 my @tagslist;
3194 open my $fd, '-|', git_cmd(), 'for-each-ref',
3195 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
3196 '--format=%(objectname) %(objecttype) %(refname) '.
3197 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
3198 'refs/tags'
3199 or return;
3200 while (my $line = <$fd>) {
3201 my %ref_item;
3203 chomp $line;
3204 my ($refinfo, $creatorinfo) = split(/\0/, $line);
3205 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
3206 my ($creator, $epoch, $tz) =
3207 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
3208 $ref_item{'fullname'} = $name;
3209 $name =~ s!^refs/tags/!!;
3211 $ref_item{'type'} = $type;
3212 $ref_item{'id'} = $id;
3213 $ref_item{'name'} = $name;
3214 if ($type eq "tag") {
3215 $ref_item{'subject'} = $title;
3216 $ref_item{'reftype'} = $reftype;
3217 $ref_item{'refid'} = $refid;
3218 } else {
3219 $ref_item{'reftype'} = $type;
3220 $ref_item{'refid'} = $id;
3223 if ($type eq "tag" || $type eq "commit") {
3224 $ref_item{'epoch'} = $epoch;
3225 if ($epoch) {
3226 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
3227 } else {
3228 $ref_item{'age'} = "unknown";
3232 push @tagslist, \%ref_item;
3234 close $fd;
3236 return wantarray ? @tagslist : \@tagslist;
3239 ## ----------------------------------------------------------------------
3240 ## filesystem-related functions
3242 sub get_file_owner {
3243 my $path = shift;
3245 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
3246 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
3247 if (!defined $gcos) {
3248 return undef;
3250 my $owner = $gcos;
3251 $owner =~ s/[,;].*$//;
3252 return to_utf8($owner);
3255 # assume that file exists
3256 sub insert_file {
3257 my $filename = shift;
3259 open my $fd, '<', $filename;
3260 print map { to_utf8($_) } <$fd>;
3261 close $fd;
3264 ## ......................................................................
3265 ## mimetype related functions
3267 sub mimetype_guess_file {
3268 my $filename = shift;
3269 my $mimemap = shift;
3270 -r $mimemap or return undef;
3272 my %mimemap;
3273 open(my $mh, '<', $mimemap) or return undef;
3274 while (<$mh>) {
3275 next if m/^#/; # skip comments
3276 my ($mimetype, $exts) = split(/\t+/);
3277 if (defined $exts) {
3278 my @exts = split(/\s+/, $exts);
3279 foreach my $ext (@exts) {
3280 $mimemap{$ext} = $mimetype;
3284 close($mh);
3286 $filename =~ /\.([^.]*)$/;
3287 return $mimemap{$1};
3290 sub mimetype_guess {
3291 my $filename = shift;
3292 my $mime;
3293 $filename =~ /\./ or return undef;
3295 if ($mimetypes_file) {
3296 my $file = $mimetypes_file;
3297 if ($file !~ m!^/!) { # if it is relative path
3298 # it is relative to project
3299 $file = "$projectroot/$project/$file";
3301 $mime = mimetype_guess_file($filename, $file);
3303 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
3304 return $mime;
3307 sub blob_mimetype {
3308 my $fd = shift;
3309 my $filename = shift;
3311 if ($filename) {
3312 my $mime = mimetype_guess($filename);
3313 $mime and return $mime;
3316 # just in case
3317 return $default_blob_plain_mimetype unless $fd;
3319 if (-T $fd) {
3320 return 'text/plain';
3321 } elsif (! $filename) {
3322 return 'application/octet-stream';
3323 } elsif ($filename =~ m/\.png$/i) {
3324 return 'image/png';
3325 } elsif ($filename =~ m/\.gif$/i) {
3326 return 'image/gif';
3327 } elsif ($filename =~ m/\.jpe?g$/i) {
3328 return 'image/jpeg';
3329 } else {
3330 return 'application/octet-stream';
3334 sub blob_contenttype {
3335 my ($fd, $file_name, $type) = @_;
3337 $type ||= blob_mimetype($fd, $file_name);
3338 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
3339 $type .= "; charset=$default_text_plain_charset";
3342 return $type;
3345 # guess file syntax for syntax highlighting; return undef if no highlighting
3346 # the name of syntax can (in the future) depend on syntax highlighter used
3347 sub guess_file_syntax {
3348 my ($highlight, $mimetype, $file_name) = @_;
3349 return undef unless ($highlight && defined $file_name);
3350 my $basename = basename($file_name, '.in');
3351 return $highlight_basename{$basename}
3352 if exists $highlight_basename{$basename};
3354 $basename =~ /\.([^.]*)$/;
3355 my $ext = $1 or return undef;
3356 return $highlight_ext{$ext}
3357 if exists $highlight_ext{$ext};
3359 return undef;
3362 # run highlighter and return FD of its output,
3363 # or return original FD if no highlighting
3364 sub run_highlighter {
3365 my ($fd, $highlight, $syntax) = @_;
3366 return $fd unless ($highlight && defined $syntax);
3368 close $fd
3369 or die_error(404, "Reading blob failed");
3370 open $fd, quote_command(git_cmd(), "cat-file", "blob", $hash)." | ".
3371 "highlight --xhtml --fragment --syntax $syntax |"
3372 or die_error(500, "Couldn't open file or run syntax highlighter");
3373 return $fd;
3376 ## ======================================================================
3377 ## functions printing HTML: header, footer, error page
3379 sub get_page_title {
3380 my $title = to_utf8($site_name);
3382 return $title unless (defined $project);
3383 $title .= " - " . to_utf8($project);
3385 return $title unless (defined $action);
3386 $title .= "/$action"; # $action is US-ASCII (7bit ASCII)
3388 return $title unless (defined $file_name);
3389 $title .= " - " . esc_path($file_name);
3390 if ($action eq "tree" && $file_name !~ m|/$|) {
3391 $title .= "/";
3394 return $title;
3397 sub print_feed_meta {
3398 if (defined $project) {
3399 my %href_params = get_feed_info();
3400 if (!exists $href_params{'-title'}) {
3401 $href_params{'-title'} = 'log';
3404 foreach my $format qw(RSS Atom) {
3405 my $type = lc($format);
3406 my %link_attr = (
3407 '-rel' => 'alternate',
3408 '-title' => esc_attr("$project - $href_params{'-title'} - $format feed"),
3409 '-type' => "application/$type+xml"
3412 $href_params{'action'} = $type;
3413 $link_attr{'-href'} = href(%href_params);
3414 print "<link ".
3415 "rel=\"$link_attr{'-rel'}\" ".
3416 "title=\"$link_attr{'-title'}\" ".
3417 "href=\"$link_attr{'-href'}\" ".
3418 "type=\"$link_attr{'-type'}\" ".
3419 "/>\n";
3421 $href_params{'extra_options'} = '--no-merges';
3422 $link_attr{'-href'} = href(%href_params);
3423 $link_attr{'-title'} .= ' (no merges)';
3424 print "<link ".
3425 "rel=\"$link_attr{'-rel'}\" ".
3426 "title=\"$link_attr{'-title'}\" ".
3427 "href=\"$link_attr{'-href'}\" ".
3428 "type=\"$link_attr{'-type'}\" ".
3429 "/>\n";
3432 } else {
3433 printf('<link rel="alternate" title="%s projects list" '.
3434 'href="%s" type="text/plain; charset=utf-8" />'."\n",
3435 esc_attr($site_name), href(project=>undef, action=>"project_index"));
3436 printf('<link rel="alternate" title="%s projects feeds" '.
3437 'href="%s" type="text/x-opml" />'."\n",
3438 esc_attr($site_name), href(project=>undef, action=>"opml"));
3442 sub git_header_html {
3443 my $status = shift || "200 OK";
3444 my $expires = shift;
3445 my %opts = @_;
3447 my $title = get_page_title();
3448 my $content_type;
3449 # require explicit support from the UA if we are to send the page as
3450 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3451 # we have to do this because MSIE sometimes globs '*/*', pretending to
3452 # support xhtml+xml but choking when it gets what it asked for.
3453 if (defined $cgi->http('HTTP_ACCEPT') &&
3454 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3455 $cgi->Accept('application/xhtml+xml') != 0) {
3456 $content_type = 'application/xhtml+xml';
3457 } else {
3458 $content_type = 'text/html';
3460 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3461 -status=> $status, -expires => $expires)
3462 unless ($opts{'-no_http_header'});
3463 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
3464 print <<EOF;
3465 <?xml version="1.0" encoding="utf-8"?>
3466 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3467 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
3468 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
3469 <!-- git core binaries version $git_version -->
3470 <head>
3471 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
3472 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
3473 <meta name="robots" content="index, nofollow"/>
3474 <title>$title</title>
3476 # the stylesheet, favicon etc urls won't work correctly with path_info
3477 # unless we set the appropriate base URL
3478 if ($ENV{'PATH_INFO'}) {
3479 print "<base href=\"".esc_url($base_url)."\" />\n";
3481 # print out each stylesheet that exist, providing backwards capability
3482 # for those people who defined $stylesheet in a config file
3483 if (defined $stylesheet) {
3484 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3485 } else {
3486 foreach my $stylesheet (@stylesheets) {
3487 next unless $stylesheet;
3488 print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3491 print_feed_meta()
3492 if ($status eq '200 OK');
3493 if (defined $favicon) {
3494 print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3497 print "</head>\n" .
3498 "<body>\n";
3500 if (defined $site_header && -f $site_header) {
3501 insert_file($site_header);
3504 print "<div class=\"page_header\">\n" .
3505 $cgi->a({-href => esc_url($logo_url),
3506 -title => $logo_label},
3507 qq(<img src=").esc_url($logo).qq(" width="72" height="27" alt="git" class="logo"/>));
3508 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3509 if (defined $project) {
3510 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3511 if (defined $action) {
3512 print " / $action";
3514 print "\n";
3516 print "</div>\n";
3518 my $have_search = gitweb_check_feature('search');
3519 if (defined $project && $have_search) {
3520 if (!defined $searchtext) {
3521 $searchtext = "";
3523 my $search_hash;
3524 if (defined $hash_base) {
3525 $search_hash = $hash_base;
3526 } elsif (defined $hash) {
3527 $search_hash = $hash;
3528 } else {
3529 $search_hash = "HEAD";
3531 my $action = $my_uri;
3532 my $use_pathinfo = gitweb_check_feature('pathinfo');
3533 if ($use_pathinfo) {
3534 $action .= "/".esc_url($project);
3536 print $cgi->startform(-method => "get", -action => $action) .
3537 "<div class=\"search\">\n" .
3538 (!$use_pathinfo &&
3539 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3540 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3541 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3542 $cgi->popup_menu(-name => 'st', -default => 'commit',
3543 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3544 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3545 " search:\n",
3546 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3547 "<span title=\"Extended regular expression\">" .
3548 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3549 -checked => $search_use_regexp) .
3550 "</span>" .
3551 "</div>" .
3552 $cgi->end_form() . "\n";
3556 sub git_footer_html {
3557 my $feed_class = 'rss_logo';
3559 print "<div class=\"page_footer\">\n";
3560 if (defined $project) {
3561 my $descr = git_get_project_description($project);
3562 if (defined $descr) {
3563 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
3566 my %href_params = get_feed_info();
3567 if (!%href_params) {
3568 $feed_class .= ' generic';
3570 $href_params{'-title'} ||= 'log';
3572 foreach my $format qw(RSS Atom) {
3573 $href_params{'action'} = lc($format);
3574 print $cgi->a({-href => href(%href_params),
3575 -title => "$href_params{'-title'} $format feed",
3576 -class => $feed_class}, $format)."\n";
3579 } else {
3580 print $cgi->a({-href => href(project=>undef, action=>"opml"),
3581 -class => $feed_class}, "OPML") . " ";
3582 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
3583 -class => $feed_class}, "TXT") . "\n";
3585 print "</div>\n"; # class="page_footer"
3587 if (defined $t0 && gitweb_check_feature('timed')) {
3588 print "<div id=\"generating_info\">\n";
3589 print 'This page took '.
3590 '<span id="generating_time" class="time_span">'.
3591 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
3592 ' seconds </span>'.
3593 ' and '.
3594 '<span id="generating_cmd">'.
3595 $number_of_git_cmds.
3596 '</span> git commands '.
3597 " to generate.\n";
3598 print "</div>\n"; # class="page_footer"
3601 if (defined $site_footer && -f $site_footer) {
3602 insert_file($site_footer);
3605 print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;
3606 if (defined $action &&
3607 $action eq 'blame_incremental') {
3608 print qq!<script type="text/javascript">\n!.
3609 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
3610 qq! "!. href() .qq!");\n!.
3611 qq!</script>\n!;
3612 } elsif (gitweb_check_feature('javascript-actions')) {
3613 print qq!<script type="text/javascript">\n!.
3614 qq!window.onload = fixLinks;\n!.
3615 qq!</script>\n!;
3618 print "</body>\n" .
3619 "</html>";
3622 # die_error(<http_status_code>, <error_message>[, <detailed_html_description>])
3623 # Example: die_error(404, 'Hash not found')
3624 # By convention, use the following status codes (as defined in RFC 2616):
3625 # 400: Invalid or missing CGI parameters, or
3626 # requested object exists but has wrong type.
3627 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
3628 # this server or project.
3629 # 404: Requested object/revision/project doesn't exist.
3630 # 500: The server isn't configured properly, or
3631 # an internal error occurred (e.g. failed assertions caused by bugs), or
3632 # an unknown error occurred (e.g. the git binary died unexpectedly).
3633 # 503: The server is currently unavailable (because it is overloaded,
3634 # or down for maintenance). Generally, this is a temporary state.
3635 sub die_error {
3636 my $status = shift || 500;
3637 my $error = esc_html(shift) || "Internal Server Error";
3638 my $extra = shift;
3639 my %opts = @_;
3641 my %http_responses = (
3642 400 => '400 Bad Request',
3643 403 => '403 Forbidden',
3644 404 => '404 Not Found',
3645 500 => '500 Internal Server Error',
3646 503 => '503 Service Unavailable',
3648 git_header_html($http_responses{$status}, undef, %opts);
3649 print <<EOF;
3650 <div class="page_body">
3651 <br /><br />
3652 $status - $error
3653 <br />
3655 if (defined $extra) {
3656 print "<hr />\n" .
3657 "$extra\n";
3659 print "</div>\n";
3661 git_footer_html();
3662 goto DONE_GITWEB
3663 unless ($opts{'-error_handler'});
3666 ## ----------------------------------------------------------------------
3667 ## functions printing or outputting HTML: navigation
3669 sub git_print_page_nav {
3670 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
3671 $extra = '' if !defined $extra; # pager or formats
3673 my @navs = qw(summary shortlog log commit commitdiff tree);
3674 if ($suppress) {
3675 @navs = grep { $_ ne $suppress } @navs;
3678 my %arg = map { $_ => {action=>$_} } @navs;
3679 if (defined $head) {
3680 for (qw(commit commitdiff)) {
3681 $arg{$_}{'hash'} = $head;
3683 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
3684 for (qw(shortlog log)) {
3685 $arg{$_}{'hash'} = $head;
3690 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
3691 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
3693 my @actions = gitweb_get_feature('actions');
3694 my %repl = (
3695 '%' => '%',
3696 'n' => $project, # project name
3697 'f' => $git_dir, # project path within filesystem
3698 'h' => $treehead || '', # current hash ('h' parameter)
3699 'b' => $treebase || '', # hash base ('hb' parameter)
3701 while (@actions) {
3702 my ($label, $link, $pos) = splice(@actions,0,3);
3703 # insert
3704 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
3705 # munch munch
3706 $link =~ s/%([%nfhb])/$repl{$1}/g;
3707 $arg{$label}{'_href'} = $link;
3710 print "<div class=\"page_nav\">\n" .
3711 (join " | ",
3712 map { $_ eq $current ?
3713 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
3714 } @navs);
3715 print "<br/>\n$extra<br/>\n" .
3716 "</div>\n";
3719 sub format_paging_nav {
3720 my ($action, $page, $has_next_link) = @_;
3721 my $paging_nav;
3724 if ($page > 0) {
3725 $paging_nav .=
3726 $cgi->a({-href => href(-replay=>1, page=>undef)}, "first") .
3727 " &sdot; " .
3728 $cgi->a({-href => href(-replay=>1, page=>$page-1),
3729 -accesskey => "p", -title => "Alt-p"}, "prev");
3730 } else {
3731 $paging_nav .= "first &sdot; prev";
3734 if ($has_next_link) {
3735 $paging_nav .= " &sdot; " .
3736 $cgi->a({-href => href(-replay=>1, page=>$page+1),
3737 -accesskey => "n", -title => "Alt-n"}, "next");
3738 } else {
3739 $paging_nav .= " &sdot; next";
3742 return $paging_nav;
3745 ## ......................................................................
3746 ## functions printing or outputting HTML: div
3748 sub git_print_header_div {
3749 my ($action, $title, $hash, $hash_base) = @_;
3750 my %args = ();
3752 $args{'action'} = $action;
3753 $args{'hash'} = $hash if $hash;
3754 $args{'hash_base'} = $hash_base if $hash_base;
3756 print "<div class=\"header\">\n" .
3757 $cgi->a({-href => href(%args), -class => "title"},
3758 $title ? $title : $action) .
3759 "\n</div>\n";
3762 sub print_local_time {
3763 print format_local_time(@_);
3766 sub format_local_time {
3767 my $localtime = '';
3768 my %date = @_;
3769 if ($date{'hour_local'} < 6) {
3770 $localtime .= sprintf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3771 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3772 } else {
3773 $localtime .= sprintf(" (%02d:%02d %s)",
3774 $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3777 return $localtime;
3780 # Outputs the author name and date in long form
3781 sub git_print_authorship {
3782 my $co = shift;
3783 my %opts = @_;
3784 my $tag = $opts{-tag} || 'div';
3785 my $author = $co->{'author_name'};
3787 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3788 print "<$tag class=\"author_date\">" .
3789 format_search_author($author, "author", esc_html($author)) .
3790 " [$ad{'rfc2822'}";
3791 print_local_time(%ad) if ($opts{-localtime});
3792 print "]" . git_get_avatar($co->{'author_email'}, -pad_before => 1)
3793 . "</$tag>\n";
3796 # Outputs table rows containing the full author or committer information,
3797 # in the format expected for 'commit' view (& similar).
3798 # Parameters are a commit hash reference, followed by the list of people
3799 # to output information for. If the list is empty it defaults to both
3800 # author and committer.
3801 sub git_print_authorship_rows {
3802 my $co = shift;
3803 # too bad we can't use @people = @_ || ('author', 'committer')
3804 my @people = @_;
3805 @people = ('author', 'committer') unless @people;
3806 foreach my $who (@people) {
3807 my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3808 print "<tr><td>$who</td><td>" .
3809 format_search_author($co->{"${who}_name"}, $who,
3810 esc_html($co->{"${who}_name"})) . " " .
3811 format_search_author($co->{"${who}_email"}, $who,
3812 esc_html("<" . $co->{"${who}_email"} . ">")) .
3813 "</td><td rowspan=\"2\">" .
3814 git_get_avatar($co->{"${who}_email"}, -size => 'double') .
3815 "</td></tr>\n" .
3816 "<tr>" .
3817 "<td></td><td> $wd{'rfc2822'}";
3818 print_local_time(%wd);
3819 print "</td>" .
3820 "</tr>\n";
3824 sub git_print_page_path {
3825 my $name = shift;
3826 my $type = shift;
3827 my $hb = shift;
3830 print "<div class=\"page_path\">";
3831 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3832 -title => 'tree root'}, to_utf8("[$project]"));
3833 print " / ";
3834 if (defined $name) {
3835 my @dirname = split '/', $name;
3836 my $basename = pop @dirname;
3837 my $fullname = '';
3839 foreach my $dir (@dirname) {
3840 $fullname .= ($fullname ? '/' : '') . $dir;
3841 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3842 hash_base=>$hb),
3843 -title => $fullname}, esc_path($dir));
3844 print " / ";
3846 if (defined $type && $type eq 'blob') {
3847 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3848 hash_base=>$hb),
3849 -title => $name}, esc_path($basename));
3850 } elsif (defined $type && $type eq 'tree') {
3851 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3852 hash_base=>$hb),
3853 -title => $name}, esc_path($basename));
3854 print " / ";
3855 } else {
3856 print esc_path($basename);
3859 print "<br/></div>\n";
3862 sub git_print_log {
3863 my $log = shift;
3864 my %opts = @_;
3866 if ($opts{'-remove_title'}) {
3867 # remove title, i.e. first line of log
3868 shift @$log;
3870 # remove leading empty lines
3871 while (defined $log->[0] && $log->[0] eq "") {
3872 shift @$log;
3875 # print log
3876 my $signoff = 0;
3877 my $empty = 0;
3878 foreach my $line (@$log) {
3879 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3880 $signoff = 1;
3881 $empty = 0;
3882 if (! $opts{'-remove_signoff'}) {
3883 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3884 next;
3885 } else {
3886 # remove signoff lines
3887 next;
3889 } else {
3890 $signoff = 0;
3893 # print only one empty line
3894 # do not print empty line after signoff
3895 if ($line eq "") {
3896 next if ($empty || $signoff);
3897 $empty = 1;
3898 } else {
3899 $empty = 0;
3902 print format_log_line_html($line) . "<br/>\n";
3905 if ($opts{'-final_empty_line'}) {
3906 # end with single empty line
3907 print "<br/>\n" unless $empty;
3911 # return link target (what link points to)
3912 sub git_get_link_target {
3913 my $hash = shift;
3914 my $link_target;
3916 # read link
3917 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3918 or return;
3920 local $/ = undef;
3921 $link_target = <$fd>;
3923 close $fd
3924 or return;
3926 return $link_target;
3929 # given link target, and the directory (basedir) the link is in,
3930 # return target of link relative to top directory (top tree);
3931 # return undef if it is not possible (including absolute links).
3932 sub normalize_link_target {
3933 my ($link_target, $basedir) = @_;
3935 # absolute symlinks (beginning with '/') cannot be normalized
3936 return if (substr($link_target, 0, 1) eq '/');
3938 # normalize link target to path from top (root) tree (dir)
3939 my $path;
3940 if ($basedir) {
3941 $path = $basedir . '/' . $link_target;
3942 } else {
3943 # we are in top (root) tree (dir)
3944 $path = $link_target;
3947 # remove //, /./, and /../
3948 my @path_parts;
3949 foreach my $part (split('/', $path)) {
3950 # discard '.' and ''
3951 next if (!$part || $part eq '.');
3952 # handle '..'
3953 if ($part eq '..') {
3954 if (@path_parts) {
3955 pop @path_parts;
3956 } else {
3957 # link leads outside repository (outside top dir)
3958 return;
3960 } else {
3961 push @path_parts, $part;
3964 $path = join('/', @path_parts);
3966 return $path;
3969 # print tree entry (row of git_tree), but without encompassing <tr> element
3970 sub git_print_tree_entry {
3971 my ($t, $basedir, $hash_base, $have_blame) = @_;
3973 my %base_key = ();
3974 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3976 # The format of a table row is: mode list link. Where mode is
3977 # the mode of the entry, list is the name of the entry, an href,
3978 # and link is the action links of the entry.
3980 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3981 if (exists $t->{'size'}) {
3982 print "<td class=\"size\">$t->{'size'}</td>\n";
3984 if ($t->{'type'} eq "blob") {
3985 print "<td class=\"list\">" .
3986 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3987 file_name=>"$basedir$t->{'name'}", %base_key),
3988 -class => "list"}, esc_path($t->{'name'}));
3989 if (S_ISLNK(oct $t->{'mode'})) {
3990 my $link_target = git_get_link_target($t->{'hash'});
3991 if ($link_target) {
3992 my $norm_target = normalize_link_target($link_target, $basedir);
3993 if (defined $norm_target) {
3994 print " -> " .
3995 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3996 file_name=>$norm_target),
3997 -title => $norm_target}, esc_path($link_target));
3998 } else {
3999 print " -> " . esc_path($link_target);
4003 print "</td>\n";
4004 print "<td class=\"link\">";
4005 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
4006 file_name=>"$basedir$t->{'name'}", %base_key)},
4007 "blob");
4008 if ($have_blame) {
4009 print " | " .
4010 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
4011 file_name=>"$basedir$t->{'name'}", %base_key)},
4012 "blame");
4014 if (defined $hash_base) {
4015 print " | " .
4016 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4017 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4018 "history");
4020 print " | " .
4021 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
4022 file_name=>"$basedir$t->{'name'}")},
4023 "raw");
4024 print "</td>\n";
4026 } elsif ($t->{'type'} eq "tree") {
4027 print "<td class=\"list\">";
4028 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4029 file_name=>"$basedir$t->{'name'}",
4030 %base_key)},
4031 esc_path($t->{'name'}));
4032 print "</td>\n";
4033 print "<td class=\"link\">";
4034 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
4035 file_name=>"$basedir$t->{'name'}",
4036 %base_key)},
4037 "tree");
4038 if (defined $hash_base) {
4039 print " | " .
4040 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
4041 file_name=>"$basedir$t->{'name'}")},
4042 "history");
4044 print "</td>\n";
4045 } else {
4046 # unknown object: we can only present history for it
4047 # (this includes 'commit' object, i.e. submodule support)
4048 print "<td class=\"list\">" .
4049 esc_path($t->{'name'}) .
4050 "</td>\n";
4051 print "<td class=\"link\">";
4052 if (defined $hash_base) {
4053 print $cgi->a({-href => href(action=>"history",
4054 hash_base=>$hash_base,
4055 file_name=>"$basedir$t->{'name'}")},
4056 "history");
4058 print "</td>\n";
4062 ## ......................................................................
4063 ## functions printing large fragments of HTML
4065 # get pre-image filenames for merge (combined) diff
4066 sub fill_from_file_info {
4067 my ($diff, @parents) = @_;
4069 $diff->{'from_file'} = [ ];
4070 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
4071 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4072 if ($diff->{'status'}[$i] eq 'R' ||
4073 $diff->{'status'}[$i] eq 'C') {
4074 $diff->{'from_file'}[$i] =
4075 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
4079 return $diff;
4082 # is current raw difftree line of file deletion
4083 sub is_deleted {
4084 my $diffinfo = shift;
4086 return $diffinfo->{'to_id'} eq ('0' x 40);
4089 # does patch correspond to [previous] difftree raw line
4090 # $diffinfo - hashref of parsed raw diff format
4091 # $patchinfo - hashref of parsed patch diff format
4092 # (the same keys as in $diffinfo)
4093 sub is_patch_split {
4094 my ($diffinfo, $patchinfo) = @_;
4096 return defined $diffinfo && defined $patchinfo
4097 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
4101 sub git_difftree_body {
4102 my ($difftree, $hash, @parents) = @_;
4103 my ($parent) = $parents[0];
4104 my $have_blame = gitweb_check_feature('blame');
4105 print "<div class=\"list_head\">\n";
4106 if ($#{$difftree} > 10) {
4107 print(($#{$difftree} + 1) . " files changed:\n");
4109 print "</div>\n";
4111 print "<table class=\"" .
4112 (@parents > 1 ? "combined " : "") .
4113 "diff_tree\">\n";
4115 # header only for combined diff in 'commitdiff' view
4116 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
4117 if ($has_header) {
4118 # table header
4119 print "<thead><tr>\n" .
4120 "<th></th><th></th>\n"; # filename, patchN link
4121 for (my $i = 0; $i < @parents; $i++) {
4122 my $par = $parents[$i];
4123 print "<th>" .
4124 $cgi->a({-href => href(action=>"commitdiff",
4125 hash=>$hash, hash_parent=>$par),
4126 -title => 'commitdiff to parent number ' .
4127 ($i+1) . ': ' . substr($par,0,7)},
4128 $i+1) .
4129 "&nbsp;</th>\n";
4131 print "</tr></thead>\n<tbody>\n";
4134 my $alternate = 1;
4135 my $patchno = 0;
4136 foreach my $line (@{$difftree}) {
4137 my $diff = parsed_difftree_line($line);
4139 if ($alternate) {
4140 print "<tr class=\"dark\">\n";
4141 } else {
4142 print "<tr class=\"light\">\n";
4144 $alternate ^= 1;
4146 if (exists $diff->{'nparents'}) { # combined diff
4148 fill_from_file_info($diff, @parents)
4149 unless exists $diff->{'from_file'};
4151 if (!is_deleted($diff)) {
4152 # file exists in the result (child) commit
4153 print "<td>" .
4154 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4155 file_name=>$diff->{'to_file'},
4156 hash_base=>$hash),
4157 -class => "list"}, esc_path($diff->{'to_file'})) .
4158 "</td>\n";
4159 } else {
4160 print "<td>" .
4161 esc_path($diff->{'to_file'}) .
4162 "</td>\n";
4165 if ($action eq 'commitdiff') {
4166 # link to patch
4167 $patchno++;
4168 print "<td class=\"link\">" .
4169 $cgi->a({-href => "#patch$patchno"}, "patch") .
4170 " | " .
4171 "</td>\n";
4174 my $has_history = 0;
4175 my $not_deleted = 0;
4176 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
4177 my $hash_parent = $parents[$i];
4178 my $from_hash = $diff->{'from_id'}[$i];
4179 my $from_path = $diff->{'from_file'}[$i];
4180 my $status = $diff->{'status'}[$i];
4182 $has_history ||= ($status ne 'A');
4183 $not_deleted ||= ($status ne 'D');
4185 if ($status eq 'A') {
4186 print "<td class=\"link\" align=\"right\"> | </td>\n";
4187 } elsif ($status eq 'D') {
4188 print "<td class=\"link\">" .
4189 $cgi->a({-href => href(action=>"blob",
4190 hash_base=>$hash,
4191 hash=>$from_hash,
4192 file_name=>$from_path)},
4193 "blob" . ($i+1)) .
4194 " | </td>\n";
4195 } else {
4196 if ($diff->{'to_id'} eq $from_hash) {
4197 print "<td class=\"link nochange\">";
4198 } else {
4199 print "<td class=\"link\">";
4201 print $cgi->a({-href => href(action=>"blobdiff",
4202 hash=>$diff->{'to_id'},
4203 hash_parent=>$from_hash,
4204 hash_base=>$hash,
4205 hash_parent_base=>$hash_parent,
4206 file_name=>$diff->{'to_file'},
4207 file_parent=>$from_path)},
4208 "diff" . ($i+1)) .
4209 " | </td>\n";
4213 print "<td class=\"link\">";
4214 if ($not_deleted) {
4215 print $cgi->a({-href => href(action=>"blob",
4216 hash=>$diff->{'to_id'},
4217 file_name=>$diff->{'to_file'},
4218 hash_base=>$hash)},
4219 "blob");
4220 print " | " if ($has_history);
4222 if ($has_history) {
4223 print $cgi->a({-href => href(action=>"history",
4224 file_name=>$diff->{'to_file'},
4225 hash_base=>$hash)},
4226 "history");
4228 print "</td>\n";
4230 print "</tr>\n";
4231 next; # instead of 'else' clause, to avoid extra indent
4233 # else ordinary diff
4235 my ($to_mode_oct, $to_mode_str, $to_file_type);
4236 my ($from_mode_oct, $from_mode_str, $from_file_type);
4237 if ($diff->{'to_mode'} ne ('0' x 6)) {
4238 $to_mode_oct = oct $diff->{'to_mode'};
4239 if (S_ISREG($to_mode_oct)) { # only for regular file
4240 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
4242 $to_file_type = file_type($diff->{'to_mode'});
4244 if ($diff->{'from_mode'} ne ('0' x 6)) {
4245 $from_mode_oct = oct $diff->{'from_mode'};
4246 if (S_ISREG($to_mode_oct)) { # only for regular file
4247 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4249 $from_file_type = file_type($diff->{'from_mode'});
4252 if ($diff->{'status'} eq "A") { # created
4253 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
4254 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
4255 $mode_chng .= "]</span>";
4256 print "<td>";
4257 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4258 hash_base=>$hash, file_name=>$diff->{'file'}),
4259 -class => "list"}, esc_path($diff->{'file'}));
4260 print "</td>\n";
4261 print "<td>$mode_chng</td>\n";
4262 print "<td class=\"link\">";
4263 if ($action eq 'commitdiff') {
4264 # link to patch
4265 $patchno++;
4266 print $cgi->a({-href => "#patch$patchno"}, "patch");
4267 print " | ";
4269 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4270 hash_base=>$hash, file_name=>$diff->{'file'})},
4271 "blob");
4272 print "</td>\n";
4274 } elsif ($diff->{'status'} eq "D") { # deleted
4275 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4276 print "<td>";
4277 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4278 hash_base=>$parent, file_name=>$diff->{'file'}),
4279 -class => "list"}, esc_path($diff->{'file'}));
4280 print "</td>\n";
4281 print "<td>$mode_chng</td>\n";
4282 print "<td class=\"link\">";
4283 if ($action eq 'commitdiff') {
4284 # link to patch
4285 $patchno++;
4286 print $cgi->a({-href => "#patch$patchno"}, "patch");
4287 print " | ";
4289 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
4290 hash_base=>$parent, file_name=>$diff->{'file'})},
4291 "blob") . " | ";
4292 if ($have_blame) {
4293 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
4294 file_name=>$diff->{'file'})},
4295 "blame") . " | ";
4297 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
4298 file_name=>$diff->{'file'})},
4299 "history");
4300 print "</td>\n";
4302 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
4303 my $mode_chnge = "";
4304 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4305 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
4306 if ($from_file_type ne $to_file_type) {
4307 $mode_chnge .= " from $from_file_type to $to_file_type";
4309 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
4310 if ($from_mode_str && $to_mode_str) {
4311 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
4312 } elsif ($to_mode_str) {
4313 $mode_chnge .= " mode: $to_mode_str";
4316 $mode_chnge .= "]</span>\n";
4318 print "<td>";
4319 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4320 hash_base=>$hash, file_name=>$diff->{'file'}),
4321 -class => "list"}, esc_path($diff->{'file'}));
4322 print "</td>\n";
4323 print "<td>$mode_chnge</td>\n";
4324 print "<td class=\"link\">";
4325 if ($action eq 'commitdiff') {
4326 # link to patch
4327 $patchno++;
4328 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4329 " | ";
4330 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4331 # "commit" view and modified file (not onlu mode changed)
4332 print $cgi->a({-href => href(action=>"blobdiff",
4333 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4334 hash_base=>$hash, hash_parent_base=>$parent,
4335 file_name=>$diff->{'file'})},
4336 "diff") .
4337 " | ";
4339 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4340 hash_base=>$hash, file_name=>$diff->{'file'})},
4341 "blob") . " | ";
4342 if ($have_blame) {
4343 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4344 file_name=>$diff->{'file'})},
4345 "blame") . " | ";
4347 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4348 file_name=>$diff->{'file'})},
4349 "history");
4350 print "</td>\n";
4352 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
4353 my %status_name = ('R' => 'moved', 'C' => 'copied');
4354 my $nstatus = $status_name{$diff->{'status'}};
4355 my $mode_chng = "";
4356 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
4357 # mode also for directories, so we cannot use $to_mode_str
4358 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4360 print "<td>" .
4361 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
4362 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
4363 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
4364 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
4365 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
4366 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
4367 -class => "list"}, esc_path($diff->{'from_file'})) .
4368 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4369 "<td class=\"link\">";
4370 if ($action eq 'commitdiff') {
4371 # link to patch
4372 $patchno++;
4373 print $cgi->a({-href => "#patch$patchno"}, "patch") .
4374 " | ";
4375 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
4376 # "commit" view and modified file (not only pure rename or copy)
4377 print $cgi->a({-href => href(action=>"blobdiff",
4378 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
4379 hash_base=>$hash, hash_parent_base=>$parent,
4380 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
4381 "diff") .
4382 " | ";
4384 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
4385 hash_base=>$parent, file_name=>$diff->{'to_file'})},
4386 "blob") . " | ";
4387 if ($have_blame) {
4388 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
4389 file_name=>$diff->{'to_file'})},
4390 "blame") . " | ";
4392 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
4393 file_name=>$diff->{'to_file'})},
4394 "history");
4395 print "</td>\n";
4397 } # we should not encounter Unmerged (U) or Unknown (X) status
4398 print "</tr>\n";
4400 print "</tbody>" if $has_header;
4401 print "</table>\n";
4404 sub git_patchset_body {
4405 my ($fd, $difftree, $hash, @hash_parents) = @_;
4406 my ($hash_parent) = $hash_parents[0];
4408 my $is_combined = (@hash_parents > 1);
4409 my $patch_idx = 0;
4410 my $patch_number = 0;
4411 my $patch_line;
4412 my $diffinfo;
4413 my $to_name;
4414 my (%from, %to);
4416 print "<div class=\"patchset\">\n";
4418 # skip to first patch
4419 while ($patch_line = <$fd>) {
4420 chomp $patch_line;
4422 last if ($patch_line =~ m/^diff /);
4425 PATCH:
4426 while ($patch_line) {
4428 # parse "git diff" header line
4429 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
4430 # $1 is from_name, which we do not use
4431 $to_name = unquote($2);
4432 $to_name =~ s!^b/!!;
4433 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
4434 # $1 is 'cc' or 'combined', which we do not use
4435 $to_name = unquote($2);
4436 } else {
4437 $to_name = undef;
4440 # check if current patch belong to current raw line
4441 # and parse raw git-diff line if needed
4442 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
4443 # this is continuation of a split patch
4444 print "<div class=\"patch cont\">\n";
4445 } else {
4446 # advance raw git-diff output if needed
4447 $patch_idx++ if defined $diffinfo;
4449 # read and prepare patch information
4450 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4452 # compact combined diff output can have some patches skipped
4453 # find which patch (using pathname of result) we are at now;
4454 if ($is_combined) {
4455 while ($to_name ne $diffinfo->{'to_file'}) {
4456 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4457 format_diff_cc_simplified($diffinfo, @hash_parents) .
4458 "</div>\n"; # class="patch"
4460 $patch_idx++;
4461 $patch_number++;
4463 last if $patch_idx > $#$difftree;
4464 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4468 # modifies %from, %to hashes
4469 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
4471 # this is first patch for raw difftree line with $patch_idx index
4472 # we index @$difftree array from 0, but number patches from 1
4473 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
4476 # git diff header
4477 #assert($patch_line =~ m/^diff /) if DEBUG;
4478 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
4479 $patch_number++;
4480 # print "git diff" header
4481 print format_git_diff_header_line($patch_line, $diffinfo,
4482 \%from, \%to);
4484 # print extended diff header
4485 print "<div class=\"diff extended_header\">\n";
4486 EXTENDED_HEADER:
4487 while ($patch_line = <$fd>) {
4488 chomp $patch_line;
4490 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
4492 print format_extended_diff_header_line($patch_line, $diffinfo,
4493 \%from, \%to);
4495 print "</div>\n"; # class="diff extended_header"
4497 # from-file/to-file diff header
4498 if (! $patch_line) {
4499 print "</div>\n"; # class="patch"
4500 last PATCH;
4502 next PATCH if ($patch_line =~ m/^diff /);
4503 #assert($patch_line =~ m/^---/) if DEBUG;
4505 my $last_patch_line = $patch_line;
4506 $patch_line = <$fd>;
4507 chomp $patch_line;
4508 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
4510 print format_diff_from_to_header($last_patch_line, $patch_line,
4511 $diffinfo, \%from, \%to,
4512 @hash_parents);
4514 # the patch itself
4515 LINE:
4516 while ($patch_line = <$fd>) {
4517 chomp $patch_line;
4519 next PATCH if ($patch_line =~ m/^diff /);
4521 print format_diff_line($patch_line, \%from, \%to);
4524 } continue {
4525 print "</div>\n"; # class="patch"
4528 # for compact combined (--cc) format, with chunk and patch simplification
4529 # the patchset might be empty, but there might be unprocessed raw lines
4530 for (++$patch_idx if $patch_number > 0;
4531 $patch_idx < @$difftree;
4532 ++$patch_idx) {
4533 # read and prepare patch information
4534 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
4536 # generate anchor for "patch" links in difftree / whatchanged part
4537 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
4538 format_diff_cc_simplified($diffinfo, @hash_parents) .
4539 "</div>\n"; # class="patch"
4541 $patch_number++;
4544 if ($patch_number == 0) {
4545 if (@hash_parents > 1) {
4546 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
4547 } else {
4548 print "<div class=\"diff nodifferences\">No differences found</div>\n";
4552 print "</div>\n"; # class="patchset"
4555 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4557 # fills project list info (age, description, owner, forks) for each
4558 # project in the list, removing invalid projects from returned list
4559 # NOTE: modifies $projlist, but does not remove entries from it
4560 sub fill_project_list_info {
4561 my ($projlist, $check_forks) = @_;
4562 my @projects;
4564 my $show_ctags = gitweb_check_feature('ctags');
4565 PROJECT:
4566 foreach my $pr (@$projlist) {
4567 my (@activity) = git_get_last_activity($pr->{'path'});
4568 unless (@activity) {
4569 next PROJECT;
4571 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
4572 if (!defined $pr->{'descr'}) {
4573 my $descr = git_get_project_description($pr->{'path'}) || "";
4574 $descr = to_utf8($descr);
4575 $pr->{'descr_long'} = $descr;
4576 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
4578 if (!defined $pr->{'owner'}) {
4579 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
4581 if ($check_forks) {
4582 my $pname = $pr->{'path'};
4583 if (($pname =~ s/\.git$//) &&
4584 ($pname !~ /\/$/) &&
4585 (-d "$projectroot/$pname")) {
4586 $pr->{'forks'} = "-d $projectroot/$pname";
4587 } else {
4588 $pr->{'forks'} = 0;
4591 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
4592 push @projects, $pr;
4595 return @projects;
4598 # print 'sort by' <th> element, generating 'sort by $name' replay link
4599 # if that order is not selected
4600 sub print_sort_th {
4601 print format_sort_th(@_);
4604 sub format_sort_th {
4605 my ($name, $order, $header) = @_;
4606 my $sort_th = "";
4607 $header ||= ucfirst($name);
4609 if ($order eq $name) {
4610 $sort_th .= "<th>$header</th>\n";
4611 } else {
4612 $sort_th .= "<th>" .
4613 $cgi->a({-href => href(-replay=>1, order=>$name),
4614 -class => "header"}, $header) .
4615 "</th>\n";
4618 return $sort_th;
4621 sub git_project_list_body {
4622 # actually uses global variable $project
4623 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
4625 my $check_forks = gitweb_check_feature('forks');
4626 my @projects = fill_project_list_info($projlist, $check_forks);
4628 $order ||= $default_projects_order;
4629 $from = 0 unless defined $from;
4630 $to = $#projects if (!defined $to || $#projects < $to);
4632 my %order_info = (
4633 project => { key => 'path', type => 'str' },
4634 descr => { key => 'descr_long', type => 'str' },
4635 owner => { key => 'owner', type => 'str' },
4636 age => { key => 'age', type => 'num' }
4638 my $oi = $order_info{$order};
4639 if ($oi->{'type'} eq 'str') {
4640 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
4641 } else {
4642 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
4645 my $show_ctags = gitweb_check_feature('ctags');
4646 if ($show_ctags) {
4647 my %ctags;
4648 foreach my $p (@projects) {
4649 foreach my $ct (keys %{$p->{'ctags'}}) {
4650 $ctags{$ct} += $p->{'ctags'}->{$ct};
4653 my $cloud = git_populate_project_tagcloud(\%ctags);
4654 print git_show_project_tagcloud($cloud, 64);
4657 print "<table class=\"project_list\">\n";
4658 unless ($no_header) {
4659 print "<tr>\n";
4660 if ($check_forks) {
4661 print "<th></th>\n";
4663 print_sort_th('project', $order, 'Project');
4664 print_sort_th('descr', $order, 'Description');
4665 print_sort_th('owner', $order, 'Owner');
4666 print_sort_th('age', $order, 'Last Change');
4667 print "<th></th>\n" . # for links
4668 "</tr>\n";
4670 my $alternate = 1;
4671 my $tagfilter = $cgi->param('by_tag');
4672 for (my $i = $from; $i <= $to; $i++) {
4673 my $pr = $projects[$i];
4675 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
4676 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
4677 and not $pr->{'descr_long'} =~ /$searchtext/;
4678 # Weed out forks or non-matching entries of search
4679 if ($check_forks) {
4680 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
4681 $forkbase="^$forkbase" if $forkbase;
4682 next if not $searchtext and not $tagfilter and $show_ctags
4683 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
4686 if ($alternate) {
4687 print "<tr class=\"dark\">\n";
4688 } else {
4689 print "<tr class=\"light\">\n";
4691 $alternate ^= 1;
4692 if ($check_forks) {
4693 print "<td>";
4694 if ($pr->{'forks'}) {
4695 print "<!-- $pr->{'forks'} -->\n";
4696 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
4698 print "</td>\n";
4700 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4701 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
4702 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
4703 -class => "list", -title => $pr->{'descr_long'}},
4704 esc_html($pr->{'descr'})) . "</td>\n" .
4705 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
4706 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
4707 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
4708 "<td class=\"link\">" .
4709 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
4710 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
4711 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
4712 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
4713 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
4714 "</td>\n" .
4715 "</tr>\n";
4717 if (defined $extra) {
4718 print "<tr>\n";
4719 if ($check_forks) {
4720 print "<td></td>\n";
4722 print "<td colspan=\"5\">$extra</td>\n" .
4723 "</tr>\n";
4725 print "</table>\n";
4728 sub git_log_body {
4729 # uses global variable $project
4730 my ($commitlist, $from, $to, $refs, $extra) = @_;
4732 $from = 0 unless defined $from;
4733 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4735 for (my $i = 0; $i <= $to; $i++) {
4736 my %co = %{$commitlist->[$i]};
4737 next if !%co;
4738 my $commit = $co{'id'};
4739 my $ref = format_ref_marker($refs, $commit);
4740 my %ad = parse_date($co{'author_epoch'});
4741 git_print_header_div('commit',
4742 "<span class=\"age\">$co{'age_string'}</span>" .
4743 esc_html($co{'title'}) . $ref,
4744 $commit);
4745 print "<div class=\"title_text\">\n" .
4746 "<div class=\"log_link\">\n" .
4747 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4748 " | " .
4749 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4750 " | " .
4751 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4752 "<br/>\n" .
4753 "</div>\n";
4754 git_print_authorship(\%co, -tag => 'span');
4755 print "<br/>\n</div>\n";
4757 print "<div class=\"log_body\">\n";
4758 git_print_log($co{'comment'}, -final_empty_line=> 1);
4759 print "</div>\n";
4761 if ($extra) {
4762 print "<div class=\"page_nav\">\n";
4763 print "$extra\n";
4764 print "</div>\n";
4768 sub git_shortlog_body {
4769 # uses global variable $project
4770 my ($commitlist, $from, $to, $refs, $extra) = @_;
4772 $from = 0 unless defined $from;
4773 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4775 print "<table class=\"shortlog\">\n";
4776 my $alternate = 1;
4777 for (my $i = $from; $i <= $to; $i++) {
4778 my %co = %{$commitlist->[$i]};
4779 my $commit = $co{'id'};
4780 my $ref = format_ref_marker($refs, $commit);
4781 if ($alternate) {
4782 print "<tr class=\"dark\">\n";
4783 } else {
4784 print "<tr class=\"light\">\n";
4786 $alternate ^= 1;
4787 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4788 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4789 format_author_html('td', \%co, 10) . "<td>";
4790 print format_subject_html($co{'title'}, $co{'title_short'},
4791 href(action=>"commit", hash=>$commit), $ref);
4792 print "</td>\n" .
4793 "<td class=\"link\">" .
4794 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4795 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4796 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4797 my $snapshot_links = format_snapshot_links($commit);
4798 if (defined $snapshot_links) {
4799 print " | " . $snapshot_links;
4801 print "</td>\n" .
4802 "</tr>\n";
4804 if (defined $extra) {
4805 print "<tr>\n" .
4806 "<td colspan=\"4\">$extra</td>\n" .
4807 "</tr>\n";
4809 print "</table>\n";
4812 sub git_history_body {
4813 # Warning: assumes constant type (blob or tree) during history
4814 my ($commitlist, $from, $to, $refs, $extra,
4815 $file_name, $file_hash, $ftype) = @_;
4817 $from = 0 unless defined $from;
4818 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4820 print "<table class=\"history\">\n";
4821 my $alternate = 1;
4822 for (my $i = $from; $i <= $to; $i++) {
4823 my %co = %{$commitlist->[$i]};
4824 if (!%co) {
4825 next;
4827 my $commit = $co{'id'};
4829 my $ref = format_ref_marker($refs, $commit);
4831 if ($alternate) {
4832 print "<tr class=\"dark\">\n";
4833 } else {
4834 print "<tr class=\"light\">\n";
4836 $alternate ^= 1;
4837 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4838 # shortlog: format_author_html('td', \%co, 10)
4839 format_author_html('td', \%co, 15, 3) . "<td>";
4840 # originally git_history used chop_str($co{'title'}, 50)
4841 print format_subject_html($co{'title'}, $co{'title_short'},
4842 href(action=>"commit", hash=>$commit), $ref);
4843 print "</td>\n" .
4844 "<td class=\"link\">" .
4845 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4846 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4848 if ($ftype eq 'blob') {
4849 my $blob_current = $file_hash;
4850 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4851 if (defined $blob_current && defined $blob_parent &&
4852 $blob_current ne $blob_parent) {
4853 print " | " .
4854 $cgi->a({-href => href(action=>"blobdiff",
4855 hash=>$blob_current, hash_parent=>$blob_parent,
4856 hash_base=>$hash_base, hash_parent_base=>$commit,
4857 file_name=>$file_name)},
4858 "diff to current");
4861 print "</td>\n" .
4862 "</tr>\n";
4864 if (defined $extra) {
4865 print "<tr>\n" .
4866 "<td colspan=\"4\">$extra</td>\n" .
4867 "</tr>\n";
4869 print "</table>\n";
4872 sub git_tags_body {
4873 # uses global variable $project
4874 my ($taglist, $from, $to, $extra) = @_;
4875 $from = 0 unless defined $from;
4876 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4878 print "<table class=\"tags\">\n";
4879 my $alternate = 1;
4880 for (my $i = $from; $i <= $to; $i++) {
4881 my $entry = $taglist->[$i];
4882 my %tag = %$entry;
4883 my $comment = $tag{'subject'};
4884 my $comment_short;
4885 if (defined $comment) {
4886 $comment_short = chop_str($comment, 30, 5);
4888 if ($alternate) {
4889 print "<tr class=\"dark\">\n";
4890 } else {
4891 print "<tr class=\"light\">\n";
4893 $alternate ^= 1;
4894 if (defined $tag{'age'}) {
4895 print "<td><i>$tag{'age'}</i></td>\n";
4896 } else {
4897 print "<td></td>\n";
4899 print "<td>" .
4900 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4901 -class => "list name"}, esc_html($tag{'name'})) .
4902 "</td>\n" .
4903 "<td>";
4904 if (defined $comment) {
4905 print format_subject_html($comment, $comment_short,
4906 href(action=>"tag", hash=>$tag{'id'}));
4908 print "</td>\n" .
4909 "<td class=\"selflink\">";
4910 if ($tag{'type'} eq "tag") {
4911 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4912 } else {
4913 print "&nbsp;";
4915 print "</td>\n" .
4916 "<td class=\"link\">" . " | " .
4917 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4918 if ($tag{'reftype'} eq "commit") {
4919 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
4920 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
4921 } elsif ($tag{'reftype'} eq "blob") {
4922 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4924 print "</td>\n" .
4925 "</tr>";
4927 if (defined $extra) {
4928 print "<tr>\n" .
4929 "<td colspan=\"5\">$extra</td>\n" .
4930 "</tr>\n";
4932 print "</table>\n";
4935 sub git_heads_body {
4936 # uses global variable $project
4937 my ($headlist, $head, $from, $to, $extra) = @_;
4938 $from = 0 unless defined $from;
4939 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4941 print "<table class=\"heads\">\n";
4942 my $alternate = 1;
4943 for (my $i = $from; $i <= $to; $i++) {
4944 my $entry = $headlist->[$i];
4945 my %ref = %$entry;
4946 my $curr = $ref{'id'} eq $head;
4947 if ($alternate) {
4948 print "<tr class=\"dark\">\n";
4949 } else {
4950 print "<tr class=\"light\">\n";
4952 $alternate ^= 1;
4953 print "<td><i>$ref{'age'}</i></td>\n" .
4954 ($curr ? "<td class=\"current_head\">" : "<td>") .
4955 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4956 -class => "list name"},esc_html($ref{'name'})) .
4957 "</td>\n" .
4958 "<td class=\"link\">" .
4959 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
4960 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
4961 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4962 "</td>\n" .
4963 "</tr>";
4965 if (defined $extra) {
4966 print "<tr>\n" .
4967 "<td colspan=\"3\">$extra</td>\n" .
4968 "</tr>\n";
4970 print "</table>\n";
4973 sub git_search_grep_body {
4974 my ($commitlist, $from, $to, $extra) = @_;
4975 $from = 0 unless defined $from;
4976 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4978 print "<table class=\"commit_search\">\n";
4979 my $alternate = 1;
4980 for (my $i = $from; $i <= $to; $i++) {
4981 my %co = %{$commitlist->[$i]};
4982 if (!%co) {
4983 next;
4985 my $commit = $co{'id'};
4986 if ($alternate) {
4987 print "<tr class=\"dark\">\n";
4988 } else {
4989 print "<tr class=\"light\">\n";
4991 $alternate ^= 1;
4992 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4993 format_author_html('td', \%co, 15, 5) .
4994 "<td>" .
4995 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4996 -class => "list subject"},
4997 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4998 my $comment = $co{'comment'};
4999 foreach my $line (@$comment) {
5000 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
5001 my ($lead, $match, $trail) = ($1, $2, $3);
5002 $match = chop_str($match, 70, 5, 'center');
5003 my $contextlen = int((80 - length($match))/2);
5004 $contextlen = 30 if ($contextlen > 30);
5005 $lead = chop_str($lead, $contextlen, 10, 'left');
5006 $trail = chop_str($trail, $contextlen, 10, 'right');
5008 $lead = esc_html($lead);
5009 $match = esc_html($match);
5010 $trail = esc_html($trail);
5012 print "$lead<span class=\"match\">$match</span>$trail<br />";
5015 print "</td>\n" .
5016 "<td class=\"link\">" .
5017 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5018 " | " .
5019 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
5020 " | " .
5021 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5022 print "</td>\n" .
5023 "</tr>\n";
5025 if (defined $extra) {
5026 print "<tr>\n" .
5027 "<td colspan=\"3\">$extra</td>\n" .
5028 "</tr>\n";
5030 print "</table>\n";
5033 ## ======================================================================
5034 ## ======================================================================
5035 ## actions
5037 sub git_project_list {
5038 my $order = $input_params{'order'};
5039 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5040 die_error(400, "Unknown order parameter");
5043 my @list = git_get_projects_list();
5044 if (!@list) {
5045 die_error(404, "No projects found");
5048 git_header_html();
5049 if (defined $home_text && -f $home_text) {
5050 print "<div class=\"index_include\">\n";
5051 insert_file($home_text);
5052 print "</div>\n";
5054 print $cgi->startform(-method => "get") .
5055 "<p class=\"projsearch\">Search:\n" .
5056 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
5057 "</p>" .
5058 $cgi->end_form() . "\n";
5059 git_project_list_body(\@list, $order);
5060 git_footer_html();
5063 sub git_forks {
5064 my $order = $input_params{'order'};
5065 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
5066 die_error(400, "Unknown order parameter");
5069 my @list = git_get_projects_list($project);
5070 if (!@list) {
5071 die_error(404, "No forks found");
5074 git_header_html();
5075 git_print_page_nav('','');
5076 git_print_header_div('summary', "$project forks");
5077 git_project_list_body(\@list, $order);
5078 git_footer_html();
5081 sub git_project_index {
5082 my @projects = git_get_projects_list($project);
5084 print $cgi->header(
5085 -type => 'text/plain',
5086 -charset => 'utf-8',
5087 -content_disposition => 'inline; filename="index.aux"');
5089 foreach my $pr (@projects) {
5090 if (!exists $pr->{'owner'}) {
5091 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
5094 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
5095 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
5096 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5097 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
5098 $path =~ s/ /\+/g;
5099 $owner =~ s/ /\+/g;
5101 print "$path $owner\n";
5105 sub git_summary {
5106 my $descr = git_get_project_description($project) || "none";
5107 my %co = parse_commit("HEAD");
5108 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
5109 my $head = $co{'id'};
5111 my $owner = git_get_project_owner($project);
5113 my $refs = git_get_references();
5114 # These get_*_list functions return one more to allow us to see if
5115 # there are more ...
5116 my @taglist = git_get_tags_list(16);
5117 my @headlist = git_get_heads_list(16);
5118 my @forklist;
5119 my $check_forks = gitweb_check_feature('forks');
5121 if ($check_forks) {
5122 @forklist = git_get_projects_list($project);
5125 git_header_html();
5126 git_print_page_nav('summary','', $head);
5128 print "<div class=\"title\">&nbsp;</div>\n";
5129 print "<table class=\"projects_list\">\n" .
5130 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
5131 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
5132 if (defined $cd{'rfc2822'}) {
5133 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
5136 # use per project git URL list in $projectroot/$project/cloneurl
5137 # or make project git URL from git base URL and project name
5138 my $url_tag = "URL";
5139 my @url_list = git_get_project_url_list($project);
5140 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
5141 foreach my $git_url (@url_list) {
5142 next unless $git_url;
5143 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
5144 $url_tag = "";
5147 # Tag cloud
5148 my $show_ctags = gitweb_check_feature('ctags');
5149 if ($show_ctags) {
5150 my $ctags = git_get_project_ctags($project);
5151 my $cloud = git_populate_project_tagcloud($ctags);
5152 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
5153 print "</td>\n<td>" unless %$ctags;
5154 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
5155 print "</td>\n<td>" if %$ctags;
5156 print git_show_project_tagcloud($cloud, 48);
5157 print "</td></tr>";
5160 print "</table>\n";
5162 # If XSS prevention is on, we don't include README.html.
5163 # TODO: Allow a readme in some safe format.
5164 if (!$prevent_xss && -s "$projectroot/$project/README.html") {
5165 print "<div class=\"title\">readme</div>\n" .
5166 "<div class=\"readme\">\n";
5167 insert_file("$projectroot/$project/README.html");
5168 print "\n</div>\n"; # class="readme"
5171 # we need to request one more than 16 (0..15) to check if
5172 # those 16 are all
5173 my @commitlist = $head ? parse_commits($head, 17) : ();
5174 if (@commitlist) {
5175 git_print_header_div('shortlog');
5176 git_shortlog_body(\@commitlist, 0, 15, $refs,
5177 $#commitlist <= 15 ? undef :
5178 $cgi->a({-href => href(action=>"shortlog")}, "..."));
5181 if (@taglist) {
5182 git_print_header_div('tags');
5183 git_tags_body(\@taglist, 0, 15,
5184 $#taglist <= 15 ? undef :
5185 $cgi->a({-href => href(action=>"tags")}, "..."));
5188 if (@headlist) {
5189 git_print_header_div('heads');
5190 git_heads_body(\@headlist, $head, 0, 15,
5191 $#headlist <= 15 ? undef :
5192 $cgi->a({-href => href(action=>"heads")}, "..."));
5195 if (@forklist) {
5196 git_print_header_div('forks');
5197 git_project_list_body(\@forklist, 'age', 0, 15,
5198 $#forklist <= 15 ? undef :
5199 $cgi->a({-href => href(action=>"forks")}, "..."),
5200 'no_header');
5203 git_footer_html();
5206 sub git_tag {
5207 my %tag = parse_tag($hash);
5209 if (! %tag) {
5210 die_error(404, "Unknown tag object");
5213 my $head = git_get_head_hash($project);
5214 git_header_html();
5215 git_print_page_nav('','', $head,undef,$head);
5216 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
5217 print "<div class=\"title_text\">\n" .
5218 "<table class=\"object_header\">\n" .
5219 "<tr>\n" .
5220 "<td>object</td>\n" .
5221 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5222 $tag{'object'}) . "</td>\n" .
5223 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
5224 $tag{'type'}) . "</td>\n" .
5225 "</tr>\n";
5226 if (defined($tag{'author'})) {
5227 git_print_authorship_rows(\%tag, 'author');
5229 print "</table>\n\n" .
5230 "</div>\n";
5231 print "<div class=\"page_body\">";
5232 my $comment = $tag{'comment'};
5233 foreach my $line (@$comment) {
5234 chomp $line;
5235 print esc_html($line, -nbsp=>1) . "<br/>\n";
5237 print "</div>\n";
5238 git_footer_html();
5241 sub git_blame_common {
5242 my $format = shift || 'porcelain';
5243 if ($format eq 'porcelain' && $cgi->param('js')) {
5244 $format = 'incremental';
5245 $action = 'blame_incremental'; # for page title etc
5248 # permissions
5249 gitweb_check_feature('blame')
5250 or die_error(403, "Blame view not allowed");
5252 # error checking
5253 die_error(400, "No file name given") unless $file_name;
5254 $hash_base ||= git_get_head_hash($project);
5255 die_error(404, "Couldn't find base commit") unless $hash_base;
5256 my %co = parse_commit($hash_base)
5257 or die_error(404, "Commit not found");
5258 my $ftype = "blob";
5259 if (!defined $hash) {
5260 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
5261 or die_error(404, "Error looking up file");
5262 } else {
5263 $ftype = git_get_type($hash);
5264 if ($ftype !~ "blob") {
5265 die_error(400, "Object is not a blob");
5269 my $fd;
5270 if ($format eq 'incremental') {
5271 # get file contents (as base)
5272 open $fd, "-|", git_cmd(), 'cat-file', 'blob', $hash
5273 or die_error(500, "Open git-cat-file failed");
5274 } elsif ($format eq 'data') {
5275 # run git-blame --incremental
5276 open $fd, "-|", git_cmd(), "blame", "--incremental",
5277 $hash_base, "--", $file_name
5278 or die_error(500, "Open git-blame --incremental failed");
5279 } else {
5280 # run git-blame --porcelain
5281 open $fd, "-|", git_cmd(), "blame", '-p',
5282 $hash_base, '--', $file_name
5283 or die_error(500, "Open git-blame --porcelain failed");
5286 # incremental blame data returns early
5287 if ($format eq 'data') {
5288 print $cgi->header(
5289 -type=>"text/plain", -charset => "utf-8",
5290 -status=> "200 OK");
5291 local $| = 1; # output autoflush
5292 print while <$fd>;
5293 close $fd
5294 or print "ERROR $!\n";
5296 print 'END';
5297 if (defined $t0 && gitweb_check_feature('timed')) {
5298 print ' '.
5299 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).
5300 ' '.$number_of_git_cmds;
5302 print "\n";
5304 return;
5307 # page header
5308 git_header_html();
5309 my $formats_nav =
5310 $cgi->a({-href => href(action=>"blob", -replay=>1)},
5311 "blob") .
5312 " | ";
5313 if ($format eq 'incremental') {
5314 $formats_nav .=
5315 $cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},
5316 "blame") . " (non-incremental)";
5317 } else {
5318 $formats_nav .=
5319 $cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},
5320 "blame") . " (incremental)";
5322 $formats_nav .=
5323 " | " .
5324 $cgi->a({-href => href(action=>"history", -replay=>1)},
5325 "history") .
5326 " | " .
5327 $cgi->a({-href => href(action=>$action, file_name=>$file_name)},
5328 "HEAD");
5329 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5330 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5331 git_print_page_path($file_name, $ftype, $hash_base);
5333 # page body
5334 if ($format eq 'incremental') {
5335 print "<noscript>\n<div class=\"error\"><center><b>\n".
5336 "This page requires JavaScript to run.\n Use ".
5337 $cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},
5338 'this page').
5339 " instead.\n".
5340 "</b></center></div>\n</noscript>\n";
5342 print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;
5345 print qq!<div class="page_body">\n!;
5346 print qq!<div id="progress_info">... / ...</div>\n!
5347 if ($format eq 'incremental');
5348 print qq!<table id="blame_table" class="blame" width="100%">\n!.
5349 #qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.
5350 qq!<thead>\n!.
5351 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.
5352 qq!</thead>\n!.
5353 qq!<tbody>\n!;
5355 my @rev_color = qw(light dark);
5356 my $num_colors = scalar(@rev_color);
5357 my $current_color = 0;
5359 if ($format eq 'incremental') {
5360 my $color_class = $rev_color[$current_color];
5362 #contents of a file
5363 my $linenr = 0;
5364 LINE:
5365 while (my $line = <$fd>) {
5366 chomp $line;
5367 $linenr++;
5369 print qq!<tr id="l$linenr" class="$color_class">!.
5370 qq!<td class="sha1"><a href=""> </a></td>!.
5371 qq!<td class="linenr">!.
5372 qq!<a class="linenr" href="">$linenr</a></td>!;
5373 print qq!<td class="pre">! . esc_html($line) . "</td>\n";
5374 print qq!</tr>\n!;
5377 } else { # porcelain, i.e. ordinary blame
5378 my %metainfo = (); # saves information about commits
5380 # blame data
5381 LINE:
5382 while (my $line = <$fd>) {
5383 chomp $line;
5384 # the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]
5385 # no <lines in group> for subsequent lines in group of lines
5386 my ($full_rev, $orig_lineno, $lineno, $group_size) =
5387 ($line =~ /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);
5388 if (!exists $metainfo{$full_rev}) {
5389 $metainfo{$full_rev} = { 'nprevious' => 0 };
5391 my $meta = $metainfo{$full_rev};
5392 my $data;
5393 while ($data = <$fd>) {
5394 chomp $data;
5395 last if ($data =~ s/^\t//); # contents of line
5396 if ($data =~ /^(\S+)(?: (.*))?$/) {
5397 $meta->{$1} = $2 unless exists $meta->{$1};
5399 if ($data =~ /^previous /) {
5400 $meta->{'nprevious'}++;
5403 my $short_rev = substr($full_rev, 0, 8);
5404 my $author = $meta->{'author'};
5405 my %date =
5406 parse_date($meta->{'author-time'}, $meta->{'author-tz'});
5407 my $date = $date{'iso-tz'};
5408 if ($group_size) {
5409 $current_color = ($current_color + 1) % $num_colors;
5411 my $tr_class = $rev_color[$current_color];
5412 $tr_class .= ' boundary' if (exists $meta->{'boundary'});
5413 $tr_class .= ' no-previous' if ($meta->{'nprevious'} == 0);
5414 $tr_class .= ' multiple-previous' if ($meta->{'nprevious'} > 1);
5415 print "<tr id=\"l$lineno\" class=\"$tr_class\">\n";
5416 if ($group_size) {
5417 print "<td class=\"sha1\"";
5418 print " title=\"". esc_html($author) . ", $date\"";
5419 print " rowspan=\"$group_size\"" if ($group_size > 1);
5420 print ">";
5421 print $cgi->a({-href => href(action=>"commit",
5422 hash=>$full_rev,
5423 file_name=>$file_name)},
5424 esc_html($short_rev));
5425 if ($group_size >= 2) {
5426 my @author_initials = ($author =~ /\b([[:upper:]])\B/g);
5427 if (@author_initials) {
5428 print "<br />" .
5429 esc_html(join('', @author_initials));
5430 # or join('.', ...)
5433 print "</td>\n";
5435 # 'previous' <sha1 of parent commit> <filename at commit>
5436 if (exists $meta->{'previous'} &&
5437 $meta->{'previous'} =~ /^([a-fA-F0-9]{40}) (.*)$/) {
5438 $meta->{'parent'} = $1;
5439 $meta->{'file_parent'} = unquote($2);
5441 my $linenr_commit =
5442 exists($meta->{'parent'}) ?
5443 $meta->{'parent'} : $full_rev;
5444 my $linenr_filename =
5445 exists($meta->{'file_parent'}) ?
5446 $meta->{'file_parent'} : unquote($meta->{'filename'});
5447 my $blamed = href(action => 'blame',
5448 file_name => $linenr_filename,
5449 hash_base => $linenr_commit);
5450 print "<td class=\"linenr\">";
5451 print $cgi->a({ -href => "$blamed#l$orig_lineno",
5452 -class => "linenr" },
5453 esc_html($lineno));
5454 print "</td>";
5455 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
5456 print "</tr>\n";
5457 } # end while
5461 # footer
5462 print "</tbody>\n".
5463 "</table>\n"; # class="blame"
5464 print "</div>\n"; # class="blame_body"
5465 close $fd
5466 or print "Reading blob failed\n";
5468 git_footer_html();
5471 sub git_blame {
5472 git_blame_common();
5475 sub git_blame_incremental {
5476 git_blame_common('incremental');
5479 sub git_blame_data {
5480 git_blame_common('data');
5483 sub git_tags {
5484 my $head = git_get_head_hash($project);
5485 git_header_html();
5486 git_print_page_nav('','', $head,undef,$head);
5487 git_print_header_div('summary', $project);
5489 my @tagslist = git_get_tags_list();
5490 if (@tagslist) {
5491 git_tags_body(\@tagslist);
5493 git_footer_html();
5496 sub git_heads {
5497 my $head = git_get_head_hash($project);
5498 git_header_html();
5499 git_print_page_nav('','', $head,undef,$head);
5500 git_print_header_div('summary', $project);
5502 my @headslist = git_get_heads_list();
5503 if (@headslist) {
5504 git_heads_body(\@headslist, $head);
5506 git_footer_html();
5509 sub git_blob_plain {
5510 my $type = shift;
5511 my $expires;
5513 if (!defined $hash) {
5514 if (defined $file_name) {
5515 my $base = $hash_base || git_get_head_hash($project);
5516 $hash = git_get_hash_by_path($base, $file_name, "blob")
5517 or die_error(404, "Cannot find file");
5518 } else {
5519 die_error(400, "No file name defined");
5521 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5522 # blobs defined by non-textual hash id's can be cached
5523 $expires = "+1d";
5526 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5527 or die_error(500, "Open git-cat-file blob '$hash' failed");
5529 # content-type (can include charset)
5530 $type = blob_contenttype($fd, $file_name, $type);
5532 # "save as" filename, even when no $file_name is given
5533 my $save_as = "$hash";
5534 if (defined $file_name) {
5535 $save_as = $file_name;
5536 } elsif ($type =~ m/^text\//) {
5537 $save_as .= '.txt';
5540 # With XSS prevention on, blobs of all types except a few known safe
5541 # ones are served with "Content-Disposition: attachment" to make sure
5542 # they don't run in our security domain. For certain image types,
5543 # blob view writes an <img> tag referring to blob_plain view, and we
5544 # want to be sure not to break that by serving the image as an
5545 # attachment (though Firefox 3 doesn't seem to care).
5546 my $sandbox = $prevent_xss &&
5547 $type !~ m!^(?:text/plain|image/(?:gif|png|jpeg))$!;
5549 print $cgi->header(
5550 -type => $type,
5551 -expires => $expires,
5552 -content_disposition =>
5553 ($sandbox ? 'attachment' : 'inline')
5554 . '; filename="' . $save_as . '"');
5555 local $/ = undef;
5556 binmode STDOUT, ':raw';
5557 print <$fd>;
5558 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5559 close $fd;
5562 sub git_blob {
5563 my $expires;
5565 if (!defined $hash) {
5566 if (defined $file_name) {
5567 my $base = $hash_base || git_get_head_hash($project);
5568 $hash = git_get_hash_by_path($base, $file_name, "blob")
5569 or die_error(404, "Cannot find file");
5570 } else {
5571 die_error(400, "No file name defined");
5573 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5574 # blobs defined by non-textual hash id's can be cached
5575 $expires = "+1d";
5578 my $have_blame = gitweb_check_feature('blame');
5579 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
5580 or die_error(500, "Couldn't cat $file_name, $hash");
5581 my $mimetype = blob_mimetype($fd, $file_name);
5582 # use 'blob_plain' (aka 'raw') view for files that cannot be displayed
5583 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
5584 close $fd;
5585 return git_blob_plain($mimetype);
5587 # we can have blame only for text/* mimetype
5588 $have_blame &&= ($mimetype =~ m!^text/!);
5590 my $highlight = gitweb_check_feature('highlight');
5591 my $syntax = guess_file_syntax($highlight, $mimetype, $file_name);
5592 $fd = run_highlighter($fd, $highlight, $syntax)
5593 if $syntax;
5595 git_header_html(undef, $expires);
5596 my $formats_nav = '';
5597 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5598 if (defined $file_name) {
5599 if ($have_blame) {
5600 $formats_nav .=
5601 $cgi->a({-href => href(action=>"blame", -replay=>1)},
5602 "blame") .
5603 " | ";
5605 $formats_nav .=
5606 $cgi->a({-href => href(action=>"history", -replay=>1)},
5607 "history") .
5608 " | " .
5609 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5610 "raw") .
5611 " | " .
5612 $cgi->a({-href => href(action=>"blob",
5613 hash_base=>"HEAD", file_name=>$file_name)},
5614 "HEAD");
5615 } else {
5616 $formats_nav .=
5617 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
5618 "raw");
5620 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5621 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5622 } else {
5623 print "<div class=\"page_nav\">\n" .
5624 "<br/><br/></div>\n" .
5625 "<div class=\"title\">".esc_html($hash)."</div>\n";
5627 git_print_page_path($file_name, "blob", $hash_base);
5628 print "<div class=\"page_body\">\n";
5629 if ($mimetype =~ m!^image/!) {
5630 print qq!<img type="!.esc_attr($mimetype).qq!"!;
5631 if ($file_name) {
5632 print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;
5634 print qq! src="! .
5635 href(action=>"blob_plain", hash=>$hash,
5636 hash_base=>$hash_base, file_name=>$file_name) .
5637 qq!" />\n!;
5638 } else {
5639 my $nr;
5640 while (my $line = <$fd>) {
5641 chomp $line;
5642 $nr++;
5643 $line = untabify($line);
5644 printf qq!<div class="pre"><a id="l%i" href="%s#l%i" class="linenr">%4i</a> %s</div>\n!,
5645 $nr, esc_attr(href(-replay => 1)), $nr, $nr, $syntax ? $line : esc_html($line, -nbsp=>1);
5648 close $fd
5649 or print "Reading blob failed.\n";
5650 print "</div>";
5651 git_footer_html();
5654 sub git_tree {
5655 if (!defined $hash_base) {
5656 $hash_base = "HEAD";
5658 if (!defined $hash) {
5659 if (defined $file_name) {
5660 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
5661 } else {
5662 $hash = $hash_base;
5665 die_error(404, "No such tree") unless defined($hash);
5667 my $show_sizes = gitweb_check_feature('show-sizes');
5668 my $have_blame = gitweb_check_feature('blame');
5670 my @entries = ();
5672 local $/ = "\0";
5673 open my $fd, "-|", git_cmd(), "ls-tree", '-z',
5674 ($show_sizes ? '-l' : ()), @extra_options, $hash
5675 or die_error(500, "Open git-ls-tree failed");
5676 @entries = map { chomp; $_ } <$fd>;
5677 close $fd
5678 or die_error(404, "Reading tree failed");
5681 my $refs = git_get_references();
5682 my $ref = format_ref_marker($refs, $hash_base);
5683 git_header_html();
5684 my $basedir = '';
5685 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5686 my @views_nav = ();
5687 if (defined $file_name) {
5688 push @views_nav,
5689 $cgi->a({-href => href(action=>"history", -replay=>1)},
5690 "history"),
5691 $cgi->a({-href => href(action=>"tree",
5692 hash_base=>"HEAD", file_name=>$file_name)},
5693 "HEAD"),
5695 my $snapshot_links = format_snapshot_links($hash);
5696 if (defined $snapshot_links) {
5697 # FIXME: Should be available when we have no hash base as well.
5698 push @views_nav, $snapshot_links;
5700 git_print_page_nav('tree','', $hash_base, undef, undef,
5701 join(' | ', @views_nav));
5702 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
5703 } else {
5704 undef $hash_base;
5705 print "<div class=\"page_nav\">\n";
5706 print "<br/><br/></div>\n";
5707 print "<div class=\"title\">".esc_html($hash)."</div>\n";
5709 if (defined $file_name) {
5710 $basedir = $file_name;
5711 if ($basedir ne '' && substr($basedir, -1) ne '/') {
5712 $basedir .= '/';
5714 git_print_page_path($file_name, 'tree', $hash_base);
5716 print "<div class=\"page_body\">\n";
5717 print "<table class=\"tree\">\n";
5718 my $alternate = 1;
5719 # '..' (top directory) link if possible
5720 if (defined $hash_base &&
5721 defined $file_name && $file_name =~ m![^/]+$!) {
5722 if ($alternate) {
5723 print "<tr class=\"dark\">\n";
5724 } else {
5725 print "<tr class=\"light\">\n";
5727 $alternate ^= 1;
5729 my $up = $file_name;
5730 $up =~ s!/?[^/]+$!!;
5731 undef $up unless $up;
5732 # based on git_print_tree_entry
5733 print '<td class="mode">' . mode_str('040000') . "</td>\n";
5734 print '<td class="size">&nbsp;</td>'."\n" if $show_sizes;
5735 print '<td class="list">';
5736 print $cgi->a({-href => href(action=>"tree",
5737 hash_base=>$hash_base,
5738 file_name=>$up)},
5739 "..");
5740 print "</td>\n";
5741 print "<td class=\"link\"></td>\n";
5743 print "</tr>\n";
5745 foreach my $line (@entries) {
5746 my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
5748 if ($alternate) {
5749 print "<tr class=\"dark\">\n";
5750 } else {
5751 print "<tr class=\"light\">\n";
5753 $alternate ^= 1;
5755 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
5757 print "</tr>\n";
5759 print "</table>\n" .
5760 "</div>";
5761 git_footer_html();
5764 sub snapshot_name {
5765 my ($project, $hash) = @_;
5767 # path/to/project.git -> project
5768 # path/to/project/.git -> project
5769 my $name = to_utf8($project);
5770 $name =~ s,([^/])/*\.git$,$1,;
5771 $name = basename($name);
5772 # sanitize name
5773 $name =~ s/[[:cntrl:]]/?/g;
5775 my $ver = $hash;
5776 if ($hash =~ /^[0-9a-fA-F]+$/) {
5777 # shorten SHA-1 hash
5778 my $full_hash = git_get_full_hash($project, $hash);
5779 if ($full_hash =~ /^$hash/ && length($hash) > 7) {
5780 $ver = git_get_short_hash($project, $hash);
5782 } elsif ($hash =~ m!^refs/tags/(.*)$!) {
5783 # tags don't need shortened SHA-1 hash
5784 $ver = $1;
5785 } else {
5786 # branches and other need shortened SHA-1 hash
5787 if ($hash =~ m!^refs/(?:heads|remotes)/(.*)$!) {
5788 $ver = $1;
5790 $ver .= '-' . git_get_short_hash($project, $hash);
5792 # in case of hierarchical branch names
5793 $ver =~ s!/!.!g;
5795 # name = project-version_string
5796 $name = "$name-$ver";
5798 return wantarray ? ($name, $name) : $name;
5801 sub git_snapshot {
5802 my $format = $input_params{'snapshot_format'};
5803 if (!@snapshot_fmts) {
5804 die_error(403, "Snapshots not allowed");
5806 # default to first supported snapshot format
5807 $format ||= $snapshot_fmts[0];
5808 if ($format !~ m/^[a-z0-9]+$/) {
5809 die_error(400, "Invalid snapshot format parameter");
5810 } elsif (!exists($known_snapshot_formats{$format})) {
5811 die_error(400, "Unknown snapshot format");
5812 } elsif ($known_snapshot_formats{$format}{'disabled'}) {
5813 die_error(403, "Snapshot format not allowed");
5814 } elsif (!grep($_ eq $format, @snapshot_fmts)) {
5815 die_error(403, "Unsupported snapshot format");
5818 my $type = git_get_type("$hash^{}");
5819 if (!$type) {
5820 die_error(404, 'Object does not exist');
5821 } elsif ($type eq 'blob') {
5822 die_error(400, 'Object is not a tree-ish');
5825 my ($name, $prefix) = snapshot_name($project, $hash);
5826 my $filename = "$name$known_snapshot_formats{$format}{'suffix'}";
5827 my $cmd = quote_command(
5828 git_cmd(), 'archive',
5829 "--format=$known_snapshot_formats{$format}{'format'}",
5830 "--prefix=$prefix/", $hash);
5831 if (exists $known_snapshot_formats{$format}{'compressor'}) {
5832 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
5835 $filename =~ s/(["\\])/\\$1/g;
5836 print $cgi->header(
5837 -type => $known_snapshot_formats{$format}{'type'},
5838 -content_disposition => 'inline; filename="' . $filename . '"',
5839 -status => '200 OK');
5841 open my $fd, "-|", $cmd
5842 or die_error(500, "Execute git-archive failed");
5843 binmode STDOUT, ':raw';
5844 print <$fd>;
5845 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
5846 close $fd;
5849 sub git_log_generic {
5850 my ($fmt_name, $body_subr, $base, $parent, $file_name, $file_hash) = @_;
5852 my $head = git_get_head_hash($project);
5853 if (!defined $base) {
5854 $base = $head;
5856 if (!defined $page) {
5857 $page = 0;
5859 my $refs = git_get_references();
5861 my $commit_hash = $base;
5862 if (defined $parent) {
5863 $commit_hash = "$parent..$base";
5865 my @commitlist =
5866 parse_commits($commit_hash, 101, (100 * $page),
5867 defined $file_name ? ($file_name, "--full-history") : ());
5869 my $ftype;
5870 if (!defined $file_hash && defined $file_name) {
5871 # some commits could have deleted file in question,
5872 # and not have it in tree, but one of them has to have it
5873 for (my $i = 0; $i < @commitlist; $i++) {
5874 $file_hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5875 last if defined $file_hash;
5878 if (defined $file_hash) {
5879 $ftype = git_get_type($file_hash);
5881 if (defined $file_name && !defined $ftype) {
5882 die_error(500, "Unknown type of object");
5884 my %co;
5885 if (defined $file_name) {
5886 %co = parse_commit($base)
5887 or die_error(404, "Unknown commit object");
5891 my $paging_nav = format_paging_nav($fmt_name, $page, $#commitlist >= 100);
5892 my $next_link = '';
5893 if ($#commitlist >= 100) {
5894 $next_link =
5895 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5896 -accesskey => "n", -title => "Alt-n"}, "next");
5898 my $patch_max = gitweb_get_feature('patches');
5899 if ($patch_max && !defined $file_name) {
5900 if ($patch_max < 0 || @commitlist <= $patch_max) {
5901 $paging_nav .= " &sdot; " .
5902 $cgi->a({-href => href(action=>"patches", -replay=>1)},
5903 "patches");
5907 git_header_html();
5908 git_print_page_nav($fmt_name,'', $hash,$hash,$hash, $paging_nav);
5909 if (defined $file_name) {
5910 git_print_header_div('commit', esc_html($co{'title'}), $base);
5911 } else {
5912 git_print_header_div('summary', $project)
5914 git_print_page_path($file_name, $ftype, $hash_base)
5915 if (defined $file_name);
5917 $body_subr->(\@commitlist, 0, 99, $refs, $next_link,
5918 $file_name, $file_hash, $ftype);
5920 git_footer_html();
5923 sub git_log {
5924 git_log_generic('log', \&git_log_body,
5925 $hash, $hash_parent);
5928 sub git_commit {
5929 $hash ||= $hash_base || "HEAD";
5930 my %co = parse_commit($hash)
5931 or die_error(404, "Unknown commit object");
5933 my $parent = $co{'parent'};
5934 my $parents = $co{'parents'}; # listref
5936 # we need to prepare $formats_nav before any parameter munging
5937 my $formats_nav;
5938 if (!defined $parent) {
5939 # --root commitdiff
5940 $formats_nav .= '(initial)';
5941 } elsif (@$parents == 1) {
5942 # single parent commit
5943 $formats_nav .=
5944 '(parent: ' .
5945 $cgi->a({-href => href(action=>"commit",
5946 hash=>$parent)},
5947 esc_html(substr($parent, 0, 7))) .
5948 ')';
5949 } else {
5950 # merge commit
5951 $formats_nav .=
5952 '(merge: ' .
5953 join(' ', map {
5954 $cgi->a({-href => href(action=>"commit",
5955 hash=>$_)},
5956 esc_html(substr($_, 0, 7)));
5957 } @$parents ) .
5958 ')';
5960 if (gitweb_check_feature('patches') && @$parents <= 1) {
5961 $formats_nav .= " | " .
5962 $cgi->a({-href => href(action=>"patch", -replay=>1)},
5963 "patch");
5966 if (!defined $parent) {
5967 $parent = "--root";
5969 my @difftree;
5970 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5971 @diff_opts,
5972 (@$parents <= 1 ? $parent : '-c'),
5973 $hash, "--"
5974 or die_error(500, "Open git-diff-tree failed");
5975 @difftree = map { chomp; $_ } <$fd>;
5976 close $fd or die_error(404, "Reading git-diff-tree failed");
5978 # non-textual hash id's can be cached
5979 my $expires;
5980 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5981 $expires = "+1d";
5983 my $refs = git_get_references();
5984 my $ref = format_ref_marker($refs, $co{'id'});
5986 git_header_html(undef, $expires);
5987 git_print_page_nav('commit', '',
5988 $hash, $co{'tree'}, $hash,
5989 $formats_nav);
5991 if (defined $co{'parent'}) {
5992 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5993 } else {
5994 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5996 print "<div class=\"title_text\">\n" .
5997 "<table class=\"object_header\">\n";
5998 git_print_authorship_rows(\%co);
5999 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
6000 print "<tr>" .
6001 "<td>tree</td>" .
6002 "<td class=\"sha1\">" .
6003 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
6004 class => "list"}, $co{'tree'}) .
6005 "</td>" .
6006 "<td class=\"link\">" .
6007 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
6008 "tree");
6009 my $snapshot_links = format_snapshot_links($hash);
6010 if (defined $snapshot_links) {
6011 print " | " . $snapshot_links;
6013 print "</td>" .
6014 "</tr>\n";
6016 foreach my $par (@$parents) {
6017 print "<tr>" .
6018 "<td>parent</td>" .
6019 "<td class=\"sha1\">" .
6020 $cgi->a({-href => href(action=>"commit", hash=>$par),
6021 class => "list"}, $par) .
6022 "</td>" .
6023 "<td class=\"link\">" .
6024 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
6025 " | " .
6026 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
6027 "</td>" .
6028 "</tr>\n";
6030 print "</table>".
6031 "</div>\n";
6033 print "<div class=\"page_body\">\n";
6034 git_print_log($co{'comment'});
6035 print "</div>\n";
6037 git_difftree_body(\@difftree, $hash, @$parents);
6039 git_footer_html();
6042 sub git_object {
6043 # object is defined by:
6044 # - hash or hash_base alone
6045 # - hash_base and file_name
6046 my $type;
6048 # - hash or hash_base alone
6049 if ($hash || ($hash_base && !defined $file_name)) {
6050 my $object_id = $hash || $hash_base;
6052 open my $fd, "-|", quote_command(
6053 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
6054 or die_error(404, "Object does not exist");
6055 $type = <$fd>;
6056 chomp $type;
6057 close $fd
6058 or die_error(404, "Object does not exist");
6060 # - hash_base and file_name
6061 } elsif ($hash_base && defined $file_name) {
6062 $file_name =~ s,/+$,,;
6064 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
6065 or die_error(404, "Base object does not exist");
6067 # here errors should not hapen
6068 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
6069 or die_error(500, "Open git-ls-tree failed");
6070 my $line = <$fd>;
6071 close $fd;
6073 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
6074 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
6075 die_error(404, "File or directory for given base does not exist");
6077 $type = $2;
6078 $hash = $3;
6079 } else {
6080 die_error(400, "Not enough information to find object");
6083 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
6084 hash=>$hash, hash_base=>$hash_base,
6085 file_name=>$file_name),
6086 -status => '302 Found');
6089 sub git_blobdiff {
6090 my $format = shift || 'html';
6092 my $fd;
6093 my @difftree;
6094 my %diffinfo;
6095 my $expires;
6097 # preparing $fd and %diffinfo for git_patchset_body
6098 # new style URI
6099 if (defined $hash_base && defined $hash_parent_base) {
6100 if (defined $file_name) {
6101 # read raw output
6102 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6103 $hash_parent_base, $hash_base,
6104 "--", (defined $file_parent ? $file_parent : ()), $file_name
6105 or die_error(500, "Open git-diff-tree failed");
6106 @difftree = map { chomp; $_ } <$fd>;
6107 close $fd
6108 or die_error(404, "Reading git-diff-tree failed");
6109 @difftree
6110 or die_error(404, "Blob diff not found");
6112 } elsif (defined $hash &&
6113 $hash =~ /[0-9a-fA-F]{40}/) {
6114 # try to find filename from $hash
6116 # read filtered raw output
6117 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6118 $hash_parent_base, $hash_base, "--"
6119 or die_error(500, "Open git-diff-tree failed");
6120 @difftree =
6121 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
6122 # $hash == to_id
6123 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
6124 map { chomp; $_ } <$fd>;
6125 close $fd
6126 or die_error(404, "Reading git-diff-tree failed");
6127 @difftree
6128 or die_error(404, "Blob diff not found");
6130 } else {
6131 die_error(400, "Missing one of the blob diff parameters");
6134 if (@difftree > 1) {
6135 die_error(400, "Ambiguous blob diff specification");
6138 %diffinfo = parse_difftree_raw_line($difftree[0]);
6139 $file_parent ||= $diffinfo{'from_file'} || $file_name;
6140 $file_name ||= $diffinfo{'to_file'};
6142 $hash_parent ||= $diffinfo{'from_id'};
6143 $hash ||= $diffinfo{'to_id'};
6145 # non-textual hash id's can be cached
6146 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
6147 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
6148 $expires = '+1d';
6151 # open patch output
6152 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6153 '-p', ($format eq 'html' ? "--full-index" : ()),
6154 $hash_parent_base, $hash_base,
6155 "--", (defined $file_parent ? $file_parent : ()), $file_name
6156 or die_error(500, "Open git-diff-tree failed");
6159 # old/legacy style URI -- not generated anymore since 1.4.3.
6160 if (!%diffinfo) {
6161 die_error('404 Not Found', "Missing one of the blob diff parameters")
6164 # header
6165 if ($format eq 'html') {
6166 my $formats_nav =
6167 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
6168 "raw");
6169 git_header_html(undef, $expires);
6170 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
6171 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
6172 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
6173 } else {
6174 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
6175 print "<div class=\"title\">".esc_html("$hash vs $hash_parent")."</div>\n";
6177 if (defined $file_name) {
6178 git_print_page_path($file_name, "blob", $hash_base);
6179 } else {
6180 print "<div class=\"page_path\"></div>\n";
6183 } elsif ($format eq 'plain') {
6184 print $cgi->header(
6185 -type => 'text/plain',
6186 -charset => 'utf-8',
6187 -expires => $expires,
6188 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
6190 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6192 } else {
6193 die_error(400, "Unknown blobdiff format");
6196 # patch
6197 if ($format eq 'html') {
6198 print "<div class=\"page_body\">\n";
6200 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
6201 close $fd;
6203 print "</div>\n"; # class="page_body"
6204 git_footer_html();
6206 } else {
6207 while (my $line = <$fd>) {
6208 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
6209 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
6211 print $line;
6213 last if $line =~ m!^\+\+\+!;
6215 local $/ = undef;
6216 print <$fd>;
6217 close $fd;
6221 sub git_blobdiff_plain {
6222 git_blobdiff('plain');
6225 sub git_commitdiff {
6226 my %params = @_;
6227 my $format = $params{-format} || 'html';
6229 my ($patch_max) = gitweb_get_feature('patches');
6230 if ($format eq 'patch') {
6231 die_error(403, "Patch view not allowed") unless $patch_max;
6234 $hash ||= $hash_base || "HEAD";
6235 my %co = parse_commit($hash)
6236 or die_error(404, "Unknown commit object");
6238 # choose format for commitdiff for merge
6239 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
6240 $hash_parent = '--cc';
6242 # we need to prepare $formats_nav before almost any parameter munging
6243 my $formats_nav;
6244 if ($format eq 'html') {
6245 $formats_nav =
6246 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
6247 "raw");
6248 if ($patch_max && @{$co{'parents'}} <= 1) {
6249 $formats_nav .= " | " .
6250 $cgi->a({-href => href(action=>"patch", -replay=>1)},
6251 "patch");
6254 if (defined $hash_parent &&
6255 $hash_parent ne '-c' && $hash_parent ne '--cc') {
6256 # commitdiff with two commits given
6257 my $hash_parent_short = $hash_parent;
6258 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
6259 $hash_parent_short = substr($hash_parent, 0, 7);
6261 $formats_nav .=
6262 ' (from';
6263 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
6264 if ($co{'parents'}[$i] eq $hash_parent) {
6265 $formats_nav .= ' parent ' . ($i+1);
6266 last;
6269 $formats_nav .= ': ' .
6270 $cgi->a({-href => href(action=>"commitdiff",
6271 hash=>$hash_parent)},
6272 esc_html($hash_parent_short)) .
6273 ')';
6274 } elsif (!$co{'parent'}) {
6275 # --root commitdiff
6276 $formats_nav .= ' (initial)';
6277 } elsif (scalar @{$co{'parents'}} == 1) {
6278 # single parent commit
6279 $formats_nav .=
6280 ' (parent: ' .
6281 $cgi->a({-href => href(action=>"commitdiff",
6282 hash=>$co{'parent'})},
6283 esc_html(substr($co{'parent'}, 0, 7))) .
6284 ')';
6285 } else {
6286 # merge commit
6287 if ($hash_parent eq '--cc') {
6288 $formats_nav .= ' | ' .
6289 $cgi->a({-href => href(action=>"commitdiff",
6290 hash=>$hash, hash_parent=>'-c')},
6291 'combined');
6292 } else { # $hash_parent eq '-c'
6293 $formats_nav .= ' | ' .
6294 $cgi->a({-href => href(action=>"commitdiff",
6295 hash=>$hash, hash_parent=>'--cc')},
6296 'compact');
6298 $formats_nav .=
6299 ' (merge: ' .
6300 join(' ', map {
6301 $cgi->a({-href => href(action=>"commitdiff",
6302 hash=>$_)},
6303 esc_html(substr($_, 0, 7)));
6304 } @{$co{'parents'}} ) .
6305 ')';
6309 my $hash_parent_param = $hash_parent;
6310 if (!defined $hash_parent_param) {
6311 # --cc for multiple parents, --root for parentless
6312 $hash_parent_param =
6313 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
6316 # read commitdiff
6317 my $fd;
6318 my @difftree;
6319 if ($format eq 'html') {
6320 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6321 "--no-commit-id", "--patch-with-raw", "--full-index",
6322 $hash_parent_param, $hash, "--"
6323 or die_error(500, "Open git-diff-tree failed");
6325 while (my $line = <$fd>) {
6326 chomp $line;
6327 # empty line ends raw part of diff-tree output
6328 last unless $line;
6329 push @difftree, scalar parse_difftree_raw_line($line);
6332 } elsif ($format eq 'plain') {
6333 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6334 '-p', $hash_parent_param, $hash, "--"
6335 or die_error(500, "Open git-diff-tree failed");
6336 } elsif ($format eq 'patch') {
6337 # For commit ranges, we limit the output to the number of
6338 # patches specified in the 'patches' feature.
6339 # For single commits, we limit the output to a single patch,
6340 # diverging from the git-format-patch default.
6341 my @commit_spec = ();
6342 if ($hash_parent) {
6343 if ($patch_max > 0) {
6344 push @commit_spec, "-$patch_max";
6346 push @commit_spec, '-n', "$hash_parent..$hash";
6347 } else {
6348 if ($params{-single}) {
6349 push @commit_spec, '-1';
6350 } else {
6351 if ($patch_max > 0) {
6352 push @commit_spec, "-$patch_max";
6354 push @commit_spec, "-n";
6356 push @commit_spec, '--root', $hash;
6358 open $fd, "-|", git_cmd(), "format-patch", @diff_opts,
6359 '--encoding=utf8', '--stdout', @commit_spec
6360 or die_error(500, "Open git-format-patch failed");
6361 } else {
6362 die_error(400, "Unknown commitdiff format");
6365 # non-textual hash id's can be cached
6366 my $expires;
6367 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
6368 $expires = "+1d";
6371 # write commit message
6372 if ($format eq 'html') {
6373 my $refs = git_get_references();
6374 my $ref = format_ref_marker($refs, $co{'id'});
6376 git_header_html(undef, $expires);
6377 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
6378 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6379 print "<div class=\"title_text\">\n" .
6380 "<table class=\"object_header\">\n";
6381 git_print_authorship_rows(\%co);
6382 print "</table>".
6383 "</div>\n";
6384 print "<div class=\"page_body\">\n";
6385 if (@{$co{'comment'}} > 1) {
6386 print "<div class=\"log\">\n";
6387 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
6388 print "</div>\n"; # class="log"
6391 } elsif ($format eq 'plain') {
6392 my $refs = git_get_references("tags");
6393 my $tagname = git_get_rev_name_tags($hash);
6394 my $filename = basename($project) . "-$hash.patch";
6396 print $cgi->header(
6397 -type => 'text/plain',
6398 -charset => 'utf-8',
6399 -expires => $expires,
6400 -content_disposition => 'inline; filename="' . "$filename" . '"');
6401 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
6402 print "From: " . to_utf8($co{'author'}) . "\n";
6403 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
6404 print "Subject: " . to_utf8($co{'title'}) . "\n";
6406 print "X-Git-Tag: $tagname\n" if $tagname;
6407 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
6409 foreach my $line (@{$co{'comment'}}) {
6410 print to_utf8($line) . "\n";
6412 print "---\n\n";
6413 } elsif ($format eq 'patch') {
6414 my $filename = basename($project) . "-$hash.patch";
6416 print $cgi->header(
6417 -type => 'text/plain',
6418 -charset => 'utf-8',
6419 -expires => $expires,
6420 -content_disposition => 'inline; filename="' . "$filename" . '"');
6423 # write patch
6424 if ($format eq 'html') {
6425 my $use_parents = !defined $hash_parent ||
6426 $hash_parent eq '-c' || $hash_parent eq '--cc';
6427 git_difftree_body(\@difftree, $hash,
6428 $use_parents ? @{$co{'parents'}} : $hash_parent);
6429 print "<br/>\n";
6431 git_patchset_body($fd, \@difftree, $hash,
6432 $use_parents ? @{$co{'parents'}} : $hash_parent);
6433 close $fd;
6434 print "</div>\n"; # class="page_body"
6435 git_footer_html();
6437 } elsif ($format eq 'plain') {
6438 local $/ = undef;
6439 print <$fd>;
6440 close $fd
6441 or print "Reading git-diff-tree failed\n";
6442 } elsif ($format eq 'patch') {
6443 local $/ = undef;
6444 print <$fd>;
6445 close $fd
6446 or print "Reading git-format-patch failed\n";
6450 sub git_commitdiff_plain {
6451 git_commitdiff(-format => 'plain');
6454 # format-patch-style patches
6455 sub git_patch {
6456 git_commitdiff(-format => 'patch', -single => 1);
6459 sub git_patches {
6460 git_commitdiff(-format => 'patch');
6463 sub git_history {
6464 git_log_generic('history', \&git_history_body,
6465 $hash_base, $hash_parent_base,
6466 $file_name, $hash);
6469 sub git_search {
6470 gitweb_check_feature('search') or die_error(403, "Search is disabled");
6471 if (!defined $searchtext) {
6472 die_error(400, "Text field is empty");
6474 if (!defined $hash) {
6475 $hash = git_get_head_hash($project);
6477 my %co = parse_commit($hash);
6478 if (!%co) {
6479 die_error(404, "Unknown commit object");
6481 if (!defined $page) {
6482 $page = 0;
6485 $searchtype ||= 'commit';
6486 if ($searchtype eq 'pickaxe') {
6487 # pickaxe may take all resources of your box and run for several minutes
6488 # with every query - so decide by yourself how public you make this feature
6489 gitweb_check_feature('pickaxe')
6490 or die_error(403, "Pickaxe is disabled");
6492 if ($searchtype eq 'grep') {
6493 gitweb_check_feature('grep')
6494 or die_error(403, "Grep is disabled");
6497 git_header_html();
6499 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
6500 my $greptype;
6501 if ($searchtype eq 'commit') {
6502 $greptype = "--grep=";
6503 } elsif ($searchtype eq 'author') {
6504 $greptype = "--author=";
6505 } elsif ($searchtype eq 'committer') {
6506 $greptype = "--committer=";
6508 $greptype .= $searchtext;
6509 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
6510 $greptype, '--regexp-ignore-case',
6511 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
6513 my $paging_nav = '';
6514 if ($page > 0) {
6515 $paging_nav .=
6516 $cgi->a({-href => href(action=>"search", hash=>$hash,
6517 searchtext=>$searchtext,
6518 searchtype=>$searchtype)},
6519 "first");
6520 $paging_nav .= " &sdot; " .
6521 $cgi->a({-href => href(-replay=>1, page=>$page-1),
6522 -accesskey => "p", -title => "Alt-p"}, "prev");
6523 } else {
6524 $paging_nav .= "first";
6525 $paging_nav .= " &sdot; prev";
6527 my $next_link = '';
6528 if ($#commitlist >= 100) {
6529 $next_link =
6530 $cgi->a({-href => href(-replay=>1, page=>$page+1),
6531 -accesskey => "n", -title => "Alt-n"}, "next");
6532 $paging_nav .= " &sdot; $next_link";
6533 } else {
6534 $paging_nav .= " &sdot; next";
6537 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
6538 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6539 if ($page == 0 && !@commitlist) {
6540 print "<p>No match.</p>\n";
6541 } else {
6542 git_search_grep_body(\@commitlist, 0, 99, $next_link);
6546 if ($searchtype eq 'pickaxe') {
6547 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6548 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6550 print "<table class=\"pickaxe search\">\n";
6551 my $alternate = 1;
6552 local $/ = "\n";
6553 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
6554 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
6555 ($search_use_regexp ? '--pickaxe-regex' : ());
6556 undef %co;
6557 my @files;
6558 while (my $line = <$fd>) {
6559 chomp $line;
6560 next unless $line;
6562 my %set = parse_difftree_raw_line($line);
6563 if (defined $set{'commit'}) {
6564 # finish previous commit
6565 if (%co) {
6566 print "</td>\n" .
6567 "<td class=\"link\">" .
6568 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6569 " | " .
6570 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6571 print "</td>\n" .
6572 "</tr>\n";
6575 if ($alternate) {
6576 print "<tr class=\"dark\">\n";
6577 } else {
6578 print "<tr class=\"light\">\n";
6580 $alternate ^= 1;
6581 %co = parse_commit($set{'commit'});
6582 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
6583 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
6584 "<td><i>$author</i></td>\n" .
6585 "<td>" .
6586 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
6587 -class => "list subject"},
6588 chop_and_escape_str($co{'title'}, 50) . "<br/>");
6589 } elsif (defined $set{'to_id'}) {
6590 next if ($set{'to_id'} =~ m/^0{40}$/);
6592 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
6593 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
6594 -class => "list"},
6595 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
6596 "<br/>\n";
6599 close $fd;
6601 # finish last commit (warning: repetition!)
6602 if (%co) {
6603 print "</td>\n" .
6604 "<td class=\"link\">" .
6605 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
6606 " | " .
6607 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
6608 print "</td>\n" .
6609 "</tr>\n";
6612 print "</table>\n";
6615 if ($searchtype eq 'grep') {
6616 git_print_page_nav('','', $hash,$co{'tree'},$hash);
6617 git_print_header_div('commit', esc_html($co{'title'}), $hash);
6619 print "<table class=\"grep_search\">\n";
6620 my $alternate = 1;
6621 my $matches = 0;
6622 local $/ = "\n";
6623 open my $fd, "-|", git_cmd(), 'grep', '-n',
6624 $search_use_regexp ? ('-E', '-i') : '-F',
6625 $searchtext, $co{'tree'};
6626 my $lastfile = '';
6627 while (my $line = <$fd>) {
6628 chomp $line;
6629 my ($file, $lno, $ltext, $binary);
6630 last if ($matches++ > 1000);
6631 if ($line =~ /^Binary file (.+) matches$/) {
6632 $file = $1;
6633 $binary = 1;
6634 } else {
6635 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
6637 if ($file ne $lastfile) {
6638 $lastfile and print "</td></tr>\n";
6639 if ($alternate++) {
6640 print "<tr class=\"dark\">\n";
6641 } else {
6642 print "<tr class=\"light\">\n";
6644 print "<td class=\"list\">".
6645 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6646 file_name=>"$file"),
6647 -class => "list"}, esc_path($file));
6648 print "</td><td>\n";
6649 $lastfile = $file;
6651 if ($binary) {
6652 print "<div class=\"binary\">Binary file</div>\n";
6653 } else {
6654 $ltext = untabify($ltext);
6655 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
6656 $ltext = esc_html($1, -nbsp=>1);
6657 $ltext .= '<span class="match">';
6658 $ltext .= esc_html($2, -nbsp=>1);
6659 $ltext .= '</span>';
6660 $ltext .= esc_html($3, -nbsp=>1);
6661 } else {
6662 $ltext = esc_html($ltext, -nbsp=>1);
6664 print "<div class=\"pre\">" .
6665 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
6666 file_name=>"$file").'#l'.$lno,
6667 -class => "linenr"}, sprintf('%4i', $lno))
6668 . ' ' . $ltext . "</div>\n";
6671 if ($lastfile) {
6672 print "</td></tr>\n";
6673 if ($matches > 1000) {
6674 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
6676 } else {
6677 print "<div class=\"diff nodifferences\">No matches found</div>\n";
6679 close $fd;
6681 print "</table>\n";
6683 git_footer_html();
6686 sub git_search_help {
6687 git_header_html();
6688 git_print_page_nav('','', $hash,$hash,$hash);
6689 print <<EOT;
6690 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
6691 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
6692 the pattern entered is recognized as the POSIX extended
6693 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
6694 insensitive).</p>
6695 <dl>
6696 <dt><b>commit</b></dt>
6697 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
6699 my $have_grep = gitweb_check_feature('grep');
6700 if ($have_grep) {
6701 print <<EOT;
6702 <dt><b>grep</b></dt>
6703 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
6704 a different one) are searched for the given pattern. On large trees, this search can take
6705 a while and put some strain on the server, so please use it with some consideration. Note that
6706 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
6707 case-sensitive.</dd>
6710 print <<EOT;
6711 <dt><b>author</b></dt>
6712 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
6713 <dt><b>committer</b></dt>
6714 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
6716 my $have_pickaxe = gitweb_check_feature('pickaxe');
6717 if ($have_pickaxe) {
6718 print <<EOT;
6719 <dt><b>pickaxe</b></dt>
6720 <dd>All commits that caused the string to appear or disappear from any file (changes that
6721 added, removed or "modified" the string) will be listed. This search can take a while and
6722 takes a lot of strain on the server, so please use it wisely. Note that since you may be
6723 interested even in changes just changing the case as well, this search is case sensitive.</dd>
6726 print "</dl>\n";
6727 git_footer_html();
6730 sub git_shortlog {
6731 git_log_generic('shortlog', \&git_shortlog_body,
6732 $hash, $hash_parent);
6735 ## ......................................................................
6736 ## feeds (RSS, Atom; OPML)
6738 sub git_feed {
6739 my $format = shift || 'atom';
6740 my $have_blame = gitweb_check_feature('blame');
6742 # Atom: http://www.atomenabled.org/developers/syndication/
6743 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
6744 if ($format ne 'rss' && $format ne 'atom') {
6745 die_error(400, "Unknown web feed format");
6748 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
6749 my $head = $hash || 'HEAD';
6750 my @commitlist = parse_commits($head, 150, 0, $file_name);
6752 my %latest_commit;
6753 my %latest_date;
6754 my $content_type = "application/$format+xml";
6755 if (defined $cgi->http('HTTP_ACCEPT') &&
6756 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
6757 # browser (feed reader) prefers text/xml
6758 $content_type = 'text/xml';
6760 if (defined($commitlist[0])) {
6761 %latest_commit = %{$commitlist[0]};
6762 my $latest_epoch = $latest_commit{'committer_epoch'};
6763 %latest_date = parse_date($latest_epoch);
6764 my $if_modified = $cgi->http('IF_MODIFIED_SINCE');
6765 if (defined $if_modified) {
6766 my $since;
6767 if (eval { require HTTP::Date; 1; }) {
6768 $since = HTTP::Date::str2time($if_modified);
6769 } elsif (eval { require Time::ParseDate; 1; }) {
6770 $since = Time::ParseDate::parsedate($if_modified, GMT => 1);
6772 if (defined $since && $latest_epoch <= $since) {
6773 print $cgi->header(
6774 -type => $content_type,
6775 -charset => 'utf-8',
6776 -last_modified => $latest_date{'rfc2822'},
6777 -status => '304 Not Modified');
6778 return;
6781 print $cgi->header(
6782 -type => $content_type,
6783 -charset => 'utf-8',
6784 -last_modified => $latest_date{'rfc2822'});
6785 } else {
6786 print $cgi->header(
6787 -type => $content_type,
6788 -charset => 'utf-8');
6791 # Optimization: skip generating the body if client asks only
6792 # for Last-Modified date.
6793 return if ($cgi->request_method() eq 'HEAD');
6795 # header variables
6796 my $title = "$site_name - $project/$action";
6797 my $feed_type = 'log';
6798 if (defined $hash) {
6799 $title .= " - '$hash'";
6800 $feed_type = 'branch log';
6801 if (defined $file_name) {
6802 $title .= " :: $file_name";
6803 $feed_type = 'history';
6805 } elsif (defined $file_name) {
6806 $title .= " - $file_name";
6807 $feed_type = 'history';
6809 $title .= " $feed_type";
6810 my $descr = git_get_project_description($project);
6811 if (defined $descr) {
6812 $descr = esc_html($descr);
6813 } else {
6814 $descr = "$project " .
6815 ($format eq 'rss' ? 'RSS' : 'Atom') .
6816 " feed";
6818 my $owner = git_get_project_owner($project);
6819 $owner = esc_html($owner);
6821 #header
6822 my $alt_url;
6823 if (defined $file_name) {
6824 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
6825 } elsif (defined $hash) {
6826 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
6827 } else {
6828 $alt_url = href(-full=>1, action=>"summary");
6830 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
6831 if ($format eq 'rss') {
6832 print <<XML;
6833 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
6834 <channel>
6836 print "<title>$title</title>\n" .
6837 "<link>$alt_url</link>\n" .
6838 "<description>$descr</description>\n" .
6839 "<language>en</language>\n" .
6840 # project owner is responsible for 'editorial' content
6841 "<managingEditor>$owner</managingEditor>\n";
6842 if (defined $logo || defined $favicon) {
6843 # prefer the logo to the favicon, since RSS
6844 # doesn't allow both
6845 my $img = esc_url($logo || $favicon);
6846 print "<image>\n" .
6847 "<url>$img</url>\n" .
6848 "<title>$title</title>\n" .
6849 "<link>$alt_url</link>\n" .
6850 "</image>\n";
6852 if (%latest_date) {
6853 print "<pubDate>$latest_date{'rfc2822'}</pubDate>\n";
6854 print "<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";
6856 print "<generator>gitweb v.$version/$git_version</generator>\n";
6857 } elsif ($format eq 'atom') {
6858 print <<XML;
6859 <feed xmlns="http://www.w3.org/2005/Atom">
6861 print "<title>$title</title>\n" .
6862 "<subtitle>$descr</subtitle>\n" .
6863 '<link rel="alternate" type="text/html" href="' .
6864 $alt_url . '" />' . "\n" .
6865 '<link rel="self" type="' . $content_type . '" href="' .
6866 $cgi->self_url() . '" />' . "\n" .
6867 "<id>" . href(-full=>1) . "</id>\n" .
6868 # use project owner for feed author
6869 "<author><name>$owner</name></author>\n";
6870 if (defined $favicon) {
6871 print "<icon>" . esc_url($favicon) . "</icon>\n";
6873 if (defined $logo_url) {
6874 # not twice as wide as tall: 72 x 27 pixels
6875 print "<logo>" . esc_url($logo) . "</logo>\n";
6877 if (! %latest_date) {
6878 # dummy date to keep the feed valid until commits trickle in:
6879 print "<updated>1970-01-01T00:00:00Z</updated>\n";
6880 } else {
6881 print "<updated>$latest_date{'iso-8601'}</updated>\n";
6883 print "<generator version='$version/$git_version'>gitweb</generator>\n";
6886 # contents
6887 for (my $i = 0; $i <= $#commitlist; $i++) {
6888 my %co = %{$commitlist[$i]};
6889 my $commit = $co{'id'};
6890 # we read 150, we always show 30 and the ones more recent than 48 hours
6891 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
6892 last;
6894 my %cd = parse_date($co{'author_epoch'});
6896 # get list of changed files
6897 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6898 $co{'parent'} || "--root",
6899 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6900 or next;
6901 my @difftree = map { chomp; $_ } <$fd>;
6902 close $fd
6903 or next;
6905 # print element (entry, item)
6906 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6907 if ($format eq 'rss') {
6908 print "<item>\n" .
6909 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6910 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6911 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6912 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6913 "<link>$co_url</link>\n" .
6914 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6915 "<content:encoded>" .
6916 "<![CDATA[\n";
6917 } elsif ($format eq 'atom') {
6918 print "<entry>\n" .
6919 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6920 "<updated>$cd{'iso-8601'}</updated>\n" .
6921 "<author>\n" .
6922 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6923 if ($co{'author_email'}) {
6924 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6926 print "</author>\n" .
6927 # use committer for contributor
6928 "<contributor>\n" .
6929 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6930 if ($co{'committer_email'}) {
6931 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6933 print "</contributor>\n" .
6934 "<published>$cd{'iso-8601'}</published>\n" .
6935 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6936 "<id>$co_url</id>\n" .
6937 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6938 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6940 my $comment = $co{'comment'};
6941 print "<pre>\n";
6942 foreach my $line (@$comment) {
6943 $line = esc_html($line);
6944 print "$line\n";
6946 print "</pre><ul>\n";
6947 foreach my $difftree_line (@difftree) {
6948 my %difftree = parse_difftree_raw_line($difftree_line);
6949 next if !$difftree{'from_id'};
6951 my $file = $difftree{'file'} || $difftree{'to_file'};
6953 print "<li>" .
6954 "[" .
6955 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6956 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6957 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6958 file_name=>$file, file_parent=>$difftree{'from_file'}),
6959 -title => "diff"}, 'D');
6960 if ($have_blame) {
6961 print $cgi->a({-href => href(-full=>1, action=>"blame",
6962 file_name=>$file, hash_base=>$commit),
6963 -title => "blame"}, 'B');
6965 # if this is not a feed of a file history
6966 if (!defined $file_name || $file_name ne $file) {
6967 print $cgi->a({-href => href(-full=>1, action=>"history",
6968 file_name=>$file, hash=>$commit),
6969 -title => "history"}, 'H');
6971 $file = esc_path($file);
6972 print "] ".
6973 "$file</li>\n";
6975 if ($format eq 'rss') {
6976 print "</ul>]]>\n" .
6977 "</content:encoded>\n" .
6978 "</item>\n";
6979 } elsif ($format eq 'atom') {
6980 print "</ul>\n</div>\n" .
6981 "</content>\n" .
6982 "</entry>\n";
6986 # end of feed
6987 if ($format eq 'rss') {
6988 print "</channel>\n</rss>\n";
6989 } elsif ($format eq 'atom') {
6990 print "</feed>\n";
6994 sub git_rss {
6995 git_feed('rss');
6998 sub git_atom {
6999 git_feed('atom');
7002 sub git_opml {
7003 my @list = git_get_projects_list();
7005 print $cgi->header(
7006 -type => 'text/xml',
7007 -charset => 'utf-8',
7008 -content_disposition => 'inline; filename="opml.xml"');
7010 print <<XML;
7011 <?xml version="1.0" encoding="utf-8"?>
7012 <opml version="1.0">
7013 <head>
7014 <title>$site_name OPML Export</title>
7015 </head>
7016 <body>
7017 <outline text="git RSS feeds">
7020 foreach my $pr (@list) {
7021 my %proj = %$pr;
7022 my $head = git_get_head_hash($proj{'path'});
7023 if (!defined $head) {
7024 next;
7026 $git_dir = "$projectroot/$proj{'path'}";
7027 my %co = parse_commit($head);
7028 if (!%co) {
7029 next;
7032 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
7033 my $rss = href('project' => $proj{'path'}, 'action' => 'rss', -full => 1);
7034 my $html = href('project' => $proj{'path'}, 'action' => 'summary', -full => 1);
7035 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
7037 print <<XML;
7038 </outline>
7039 </body>
7040 </opml>