Merge commit 'vanilla/master' into refs/top-bases/master
[git/gitweb.git] / gitweb / gitweb.perl
blob825e5e5e3f5b4d3a0c2d6fcb18311eb78a5b52d7
1 #!/usr/bin/perl
3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 BEGIN {
22 CGI->compile() if $ENV{'MOD_PERL'};
25 our $cgi = new CGI;
26 our $version = "++GIT_VERSION++";
27 our $my_url = $cgi->url();
28 our $my_uri = $cgi->url(-absolute => 1);
30 # if we're called with PATH_INFO, we have to strip that
31 # from the URL to find our real URL
32 if (my $path_info = $ENV{"PATH_INFO"}) {
33 $my_url =~ s,\Q$path_info\E$,,;
34 $my_uri =~ s,\Q$path_info\E$,,;
37 # core git executable to use
38 # this can just be "git" if your webserver has a sensible PATH
39 our $GIT = "++GIT_BINDIR++/git";
41 # absolute fs-path which will be prepended to the project path
42 #our $projectroot = "/pub/scm";
43 our $projectroot = "++GITWEB_PROJECTROOT++";
45 # fs traversing limit for getting project list
46 # the number is relative to the projectroot
47 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
49 # target of the home link on top of all pages
50 our $home_link = $my_uri || "/";
52 # string of the home link on top of all pages
53 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
55 # name of your site or organization to appear in page titles
56 # replace this with something more descriptive for clearer bookmarks
57 our $site_name = "++GITWEB_SITENAME++"
58 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
60 # filename of html text to include at top of each page
61 our $site_header = "++GITWEB_SITE_HEADER++";
62 # html text to include at home page
63 our $home_text = "++GITWEB_HOMETEXT++";
64 # filename of html text to include at bottom of each page
65 our $site_footer = "++GITWEB_SITE_FOOTER++";
67 # URI of stylesheets
68 our @stylesheets = ("++GITWEB_CSS++");
69 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
70 our $stylesheet = undef;
71 # URI of GIT logo (72x27 size)
72 our $logo = "++GITWEB_LOGO++";
73 # URI of GIT favicon, assumed to be image/png type
74 our $favicon = "++GITWEB_FAVICON++";
76 # URI and label (title) of GIT logo link
77 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
78 #our $logo_label = "git documentation";
79 our $logo_url = "http://git.or.cz/";
80 our $logo_label = "git homepage";
82 # source of projects list
83 our $projects_list = "++GITWEB_LIST++";
85 # the width (in characters) of the projects list "Description" column
86 our $projects_list_description_width = 25;
88 # default order of projects list
89 # valid values are none, project, descr, owner, and age
90 our $default_projects_order = "project";
92 # show repository only if this file exists
93 # (only effective if this variable evaluates to true)
94 our $export_ok = "++GITWEB_EXPORT_OK++";
96 # only allow viewing of repositories also shown on the overview page
97 our $strict_export = "++GITWEB_STRICT_EXPORT++";
99 # list of git base URLs used for URL to where fetch project from,
100 # i.e. full URL is "$git_base_url/$project"
101 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
103 # default blob_plain mimetype and default charset for text/plain blob
104 our $default_blob_plain_mimetype = 'text/plain';
105 our $default_text_plain_charset = undef;
107 # file to use for guessing MIME types before trying /etc/mime.types
108 # (relative to the current git repository)
109 our $mimetypes_file = undef;
111 # assume this charset if line contains non-UTF-8 characters;
112 # it should be valid encoding (see Encoding::Supported(3pm) for list),
113 # for which encoding all byte sequences are valid, for example
114 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
115 # could be even 'utf-8' for the old behavior)
116 our $fallback_encoding = 'latin1';
118 # rename detection options for git-diff and git-diff-tree
119 # - default is '-M', with the cost proportional to
120 # (number of removed files) * (number of new files).
121 # - more costly is '-C' (which implies '-M'), with the cost proportional to
122 # (number of changed files + number of removed files) * (number of new files)
123 # - even more costly is '-C', '--find-copies-harder' with cost
124 # (number of files in the original tree) * (number of new files)
125 # - one might want to include '-B' option, e.g. '-B', '-M'
126 our @diff_opts = ('-M'); # taken from git_commit
128 # information about snapshot formats that gitweb is capable of serving
129 our %known_snapshot_formats = (
130 # name => {
131 # 'display' => display name,
132 # 'type' => mime type,
133 # 'suffix' => filename suffix,
134 # 'format' => --format for git-archive,
135 # 'compressor' => [compressor command and arguments]
136 # (array reference, optional)}
138 'tgz' => {
139 'display' => 'tar.gz',
140 'type' => 'application/x-gzip',
141 'suffix' => '.tar.gz',
142 'format' => 'tar',
143 'compressor' => ['gzip']},
145 'tbz2' => {
146 'display' => 'tar.bz2',
147 'type' => 'application/x-bzip2',
148 'suffix' => '.tar.bz2',
149 'format' => 'tar',
150 'compressor' => ['bzip2']},
152 'zip' => {
153 'display' => 'zip',
154 'type' => 'application/x-zip',
155 'suffix' => '.zip',
156 'format' => 'zip'},
159 # Aliases so we understand old gitweb.snapshot values in repository
160 # configuration.
161 our %known_snapshot_format_aliases = (
162 'gzip' => 'tgz',
163 'bzip2' => 'tbz2',
165 # backward compatibility: legacy gitweb config support
166 'x-gzip' => undef, 'gz' => undef,
167 'x-bzip2' => undef, 'bz2' => undef,
168 'x-zip' => undef, '' => undef,
171 # You define site-wide feature defaults here; override them with
172 # $GITWEB_CONFIG as necessary.
173 our %feature = (
174 # feature => {
175 # 'sub' => feature-sub (subroutine),
176 # 'override' => allow-override (boolean),
177 # 'default' => [ default options...] (array reference)}
179 # if feature is overridable (it means that allow-override has true value),
180 # then feature-sub will be called with default options as parameters;
181 # return value of feature-sub indicates if to enable specified feature
183 # if there is no 'sub' key (no feature-sub), then feature cannot be
184 # overriden
186 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
188 # Enable the 'blame' blob view, showing the last commit that modified
189 # each line in the file. This can be very CPU-intensive.
191 # To enable system wide have in $GITWEB_CONFIG
192 # $feature{'blame'}{'default'} = [1];
193 # To have project specific config enable override in $GITWEB_CONFIG
194 # $feature{'blame'}{'override'} = 1;
195 # and in project config gitweb.blame = 0|1;
196 'blame' => {
197 'sub' => \&feature_blame,
198 'override' => 0,
199 'default' => [0]},
201 # Enable the 'snapshot' link, providing a compressed archive of any
202 # tree. This can potentially generate high traffic if you have large
203 # project.
205 # Value is a list of formats defined in %known_snapshot_formats that
206 # you wish to offer.
207 # To disable system wide have in $GITWEB_CONFIG
208 # $feature{'snapshot'}{'default'} = [];
209 # To have project specific config enable override in $GITWEB_CONFIG
210 # $feature{'snapshot'}{'override'} = 1;
211 # and in project config, a comma-separated list of formats or "none"
212 # to disable. Example: gitweb.snapshot = tbz2,zip;
213 'snapshot' => {
214 'sub' => \&feature_snapshot,
215 'override' => 0,
216 'default' => ['tgz']},
218 # Enable text search, which will list the commits which match author,
219 # committer or commit text to a given string. Enabled by default.
220 # Project specific override is not supported.
221 'search' => {
222 'override' => 0,
223 'default' => [1]},
225 # Enable grep search, which will list the files in currently selected
226 # tree containing the given string. Enabled by default. This can be
227 # potentially CPU-intensive, of course.
229 # To enable system wide have in $GITWEB_CONFIG
230 # $feature{'grep'}{'default'} = [1];
231 # To have project specific config enable override in $GITWEB_CONFIG
232 # $feature{'grep'}{'override'} = 1;
233 # and in project config gitweb.grep = 0|1;
234 'grep' => {
235 'override' => 0,
236 'default' => [1]},
238 # Enable the pickaxe search, which will list the commits that modified
239 # a given string in a file. This can be practical and quite faster
240 # alternative to 'blame', but still potentially CPU-intensive.
242 # To enable system wide have in $GITWEB_CONFIG
243 # $feature{'pickaxe'}{'default'} = [1];
244 # To have project specific config enable override in $GITWEB_CONFIG
245 # $feature{'pickaxe'}{'override'} = 1;
246 # and in project config gitweb.pickaxe = 0|1;
247 'pickaxe' => {
248 'sub' => \&feature_pickaxe,
249 'override' => 0,
250 'default' => [1]},
252 # Make gitweb use an alternative format of the URLs which can be
253 # more readable and natural-looking: project name is embedded
254 # directly in the path and the query string contains other
255 # auxiliary information. All gitweb installations recognize
256 # URL in either format; this configures in which formats gitweb
257 # generates links.
259 # To enable system wide have in $GITWEB_CONFIG
260 # $feature{'pathinfo'}{'default'} = [1];
261 # Project specific override is not supported.
263 # Note that you will need to change the default location of CSS,
264 # favicon, logo and possibly other files to an absolute URL. Also,
265 # if gitweb.cgi serves as your indexfile, you will need to force
266 # $my_uri to contain the script name in your $GITWEB_CONFIG.
267 'pathinfo' => {
268 'override' => 0,
269 'default' => [0]},
271 # Make gitweb consider projects in project root subdirectories
272 # to be forks of existing projects. Given project $projname.git,
273 # projects matching $projname/*.git will not be shown in the main
274 # projects list, instead a '+' mark will be added to $projname
275 # there and a 'forks' view will be enabled for the project, listing
276 # all the forks. If project list is taken from a file, forks have
277 # to be listed after the main project.
279 # To enable system wide have in $GITWEB_CONFIG
280 # $feature{'forks'}{'default'} = [1];
281 # Project specific override is not supported.
282 'forks' => {
283 'override' => 0,
284 'default' => [0]},
287 sub gitweb_check_feature {
288 my ($name) = @_;
289 return unless exists $feature{$name};
290 my ($sub, $override, @defaults) = (
291 $feature{$name}{'sub'},
292 $feature{$name}{'override'},
293 @{$feature{$name}{'default'}});
294 if (!$override) { return @defaults; }
295 if (!defined $sub) {
296 warn "feature $name is not overrideable";
297 return @defaults;
299 return $sub->(@defaults);
302 sub feature_blame {
303 my ($val) = git_get_project_config('blame', '--bool');
305 if ($val eq 'true') {
306 return 1;
307 } elsif ($val eq 'false') {
308 return 0;
311 return $_[0];
314 sub feature_snapshot {
315 my (@fmts) = @_;
317 my ($val) = git_get_project_config('snapshot');
319 if ($val) {
320 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
323 return @fmts;
326 sub feature_grep {
327 my ($val) = git_get_project_config('grep', '--bool');
329 if ($val eq 'true') {
330 return (1);
331 } elsif ($val eq 'false') {
332 return (0);
335 return ($_[0]);
338 sub feature_pickaxe {
339 my ($val) = git_get_project_config('pickaxe', '--bool');
341 if ($val eq 'true') {
342 return (1);
343 } elsif ($val eq 'false') {
344 return (0);
347 return ($_[0]);
350 # checking HEAD file with -e is fragile if the repository was
351 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
352 # and then pruned.
353 sub check_head_link {
354 my ($dir) = @_;
355 my $headfile = "$dir/HEAD";
356 return ((-e $headfile) ||
357 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
360 sub check_export_ok {
361 my ($dir) = @_;
362 return (check_head_link($dir) &&
363 (!$export_ok || -e "$dir/$export_ok"));
366 # process alternate names for backward compatibility
367 # filter out unsupported (unknown) snapshot formats
368 sub filter_snapshot_fmts {
369 my @fmts = @_;
371 @fmts = map {
372 exists $known_snapshot_format_aliases{$_} ?
373 $known_snapshot_format_aliases{$_} : $_} @fmts;
374 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
378 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
379 if (-e $GITWEB_CONFIG) {
380 do $GITWEB_CONFIG;
381 } else {
382 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
383 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
386 # version of the core git binary
387 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
389 $projects_list ||= $projectroot;
391 # ======================================================================
392 # input validation and dispatch
393 our $action = $cgi->param('a');
394 if (defined $action) {
395 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
396 die_error(400, "Invalid action parameter");
400 # parameters which are pathnames
401 our $project = $cgi->param('p');
402 if (defined $project) {
403 if (!validate_pathname($project) ||
404 !(-d "$projectroot/$project") ||
405 !check_head_link("$projectroot/$project") ||
406 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
407 ($strict_export && !project_in_list($project))) {
408 undef $project;
409 die_error(404, "No such project");
413 our $file_name = $cgi->param('f');
414 if (defined $file_name) {
415 if (!validate_pathname($file_name)) {
416 die_error(400, "Invalid file parameter");
420 our $file_parent = $cgi->param('fp');
421 if (defined $file_parent) {
422 if (!validate_pathname($file_parent)) {
423 die_error(400, "Invalid file parent parameter");
427 # parameters which are refnames
428 our $hash = $cgi->param('h');
429 if (defined $hash) {
430 if (!validate_refname($hash)) {
431 die_error(400, "Invalid hash parameter");
435 our $hash_parent = $cgi->param('hp');
436 if (defined $hash_parent) {
437 if (!validate_refname($hash_parent)) {
438 die_error(400, "Invalid hash parent parameter");
442 our $hash_base = $cgi->param('hb');
443 if (defined $hash_base) {
444 if (!validate_refname($hash_base)) {
445 die_error(400, "Invalid hash base parameter");
449 my %allowed_options = (
450 "--no-merges" => [ qw(rss atom log shortlog history) ],
453 our @extra_options = $cgi->param('opt');
454 if (defined @extra_options) {
455 foreach my $opt (@extra_options) {
456 if (not exists $allowed_options{$opt}) {
457 die_error(400, "Invalid option parameter");
459 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
460 die_error(400, "Invalid option parameter for this action");
465 our $hash_parent_base = $cgi->param('hpb');
466 if (defined $hash_parent_base) {
467 if (!validate_refname($hash_parent_base)) {
468 die_error(400, "Invalid hash parent base parameter");
472 # other parameters
473 our $page = $cgi->param('pg');
474 if (defined $page) {
475 if ($page =~ m/[^0-9]/) {
476 die_error(400, "Invalid page parameter");
480 our $searchtype = $cgi->param('st');
481 if (defined $searchtype) {
482 if ($searchtype =~ m/[^a-z]/) {
483 die_error(400, "Invalid searchtype parameter");
487 our $search_use_regexp = $cgi->param('sr');
489 our $searchtext = $cgi->param('s');
490 our $search_regexp;
491 if (defined $searchtext) {
492 if (length($searchtext) < 2) {
493 die_error(403, "At least two characters are required for search parameter");
495 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
498 # now read PATH_INFO and use it as alternative to parameters
499 sub evaluate_path_info {
500 return if defined $project;
501 my $path_info = $ENV{"PATH_INFO"};
502 return if !$path_info;
503 $path_info =~ s,^/+,,;
504 return if !$path_info;
505 # find which part of PATH_INFO is project
506 $project = $path_info;
507 $project =~ s,/+$,,;
508 while ($project && !check_head_link("$projectroot/$project")) {
509 $project =~ s,/*[^/]*$,,;
511 # validate project
512 $project = validate_pathname($project);
513 if (!$project ||
514 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
515 ($strict_export && !project_in_list($project))) {
516 undef $project;
517 return;
519 # do not change any parameters if an action is given using the query string
520 return if $action;
521 $path_info =~ s,^\Q$project\E/*,,;
522 my ($refname, $pathname) = split(/:/, $path_info, 2);
523 if (defined $pathname) {
524 # we got "project.git/branch:filename" or "project.git/branch:dir/"
525 # we could use git_get_type(branch:pathname), but it needs $git_dir
526 $pathname =~ s,^/+,,;
527 if (!$pathname || substr($pathname, -1) eq "/") {
528 $action ||= "tree";
529 $pathname =~ s,/$,,;
530 } else {
531 $action ||= "blob_plain";
533 $hash_base ||= validate_refname($refname);
534 $file_name ||= validate_pathname($pathname);
535 } elsif (defined $refname) {
536 # we got "project.git/branch"
537 $action ||= "shortlog";
538 $hash ||= validate_refname($refname);
541 evaluate_path_info();
543 # path to the current git repository
544 our $git_dir;
545 $git_dir = "$projectroot/$project" if $project;
547 # dispatch
548 my %actions = (
549 "blame" => \&git_blame,
550 "blobdiff" => \&git_blobdiff,
551 "blobdiff_plain" => \&git_blobdiff_plain,
552 "blob" => \&git_blob,
553 "blob_plain" => \&git_blob_plain,
554 "commitdiff" => \&git_commitdiff,
555 "commitdiff_plain" => \&git_commitdiff_plain,
556 "commit" => \&git_commit,
557 "forks" => \&git_forks,
558 "heads" => \&git_heads,
559 "history" => \&git_history,
560 "log" => \&git_log,
561 "rss" => \&git_rss,
562 "atom" => \&git_atom,
563 "search" => \&git_search,
564 "search_help" => \&git_search_help,
565 "shortlog" => \&git_shortlog,
566 "summary" => \&git_summary,
567 "tag" => \&git_tag,
568 "tags" => \&git_tags,
569 "tree" => \&git_tree,
570 "snapshot" => \&git_snapshot,
571 "object" => \&git_object,
572 # those below don't need $project
573 "opml" => \&git_opml,
574 "project_list" => \&git_project_list,
575 "project_index" => \&git_project_index,
578 if (!defined $action) {
579 if (defined $hash) {
580 $action = git_get_type($hash);
581 } elsif (defined $hash_base && defined $file_name) {
582 $action = git_get_type("$hash_base:$file_name");
583 } elsif (defined $project) {
584 $action = 'summary';
585 } else {
586 $action = 'project_list';
589 if (!defined($actions{$action})) {
590 die_error(400, "Unknown action");
592 if ($action !~ m/^(opml|project_list|project_index)$/ &&
593 !$project) {
594 die_error(400, "Project needed");
596 $actions{$action}->();
597 exit;
599 ## ======================================================================
600 ## action links
602 sub href (%) {
603 my %params = @_;
604 # default is to use -absolute url() i.e. $my_uri
605 my $href = $params{-full} ? $my_url : $my_uri;
607 # XXX: Warning: If you touch this, check the search form for updating,
608 # too.
610 my @mapping = (
611 project => "p",
612 action => "a",
613 file_name => "f",
614 file_parent => "fp",
615 hash => "h",
616 hash_parent => "hp",
617 hash_base => "hb",
618 hash_parent_base => "hpb",
619 page => "pg",
620 order => "o",
621 searchtext => "s",
622 searchtype => "st",
623 snapshot_format => "sf",
624 extra_options => "opt",
625 search_use_regexp => "sr",
627 my %mapping = @mapping;
629 $params{'project'} = $project unless exists $params{'project'};
631 if ($params{-replay}) {
632 while (my ($name, $symbol) = each %mapping) {
633 if (!exists $params{$name}) {
634 # to allow for multivalued params we use arrayref form
635 $params{$name} = [ $cgi->param($symbol) ];
640 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
641 if ($use_pathinfo) {
642 # use PATH_INFO for project name
643 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
644 delete $params{'project'};
646 # Summary just uses the project path URL
647 if (defined $params{'action'} && $params{'action'} eq 'summary') {
648 delete $params{'action'};
652 # now encode the parameters explicitly
653 my @result = ();
654 for (my $i = 0; $i < @mapping; $i += 2) {
655 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
656 if (defined $params{$name}) {
657 if (ref($params{$name}) eq "ARRAY") {
658 foreach my $par (@{$params{$name}}) {
659 push @result, $symbol . "=" . esc_param($par);
661 } else {
662 push @result, $symbol . "=" . esc_param($params{$name});
666 $href .= "?" . join(';', @result) if scalar @result;
668 return $href;
672 ## ======================================================================
673 ## validation, quoting/unquoting and escaping
675 sub validate_pathname {
676 my $input = shift || return undef;
678 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
679 # at the beginning, at the end, and between slashes.
680 # also this catches doubled slashes
681 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
682 return undef;
684 # no null characters
685 if ($input =~ m!\0!) {
686 return undef;
688 return $input;
691 sub validate_refname {
692 my $input = shift || return undef;
694 # textual hashes are O.K.
695 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
696 return $input;
698 # it must be correct pathname
699 $input = validate_pathname($input)
700 or return undef;
701 # restrictions on ref name according to git-check-ref-format
702 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
703 return undef;
705 return $input;
708 # decode sequences of octets in utf8 into Perl's internal form,
709 # which is utf-8 with utf8 flag set if needed. gitweb writes out
710 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
711 sub to_utf8 {
712 my $str = shift;
713 if (utf8::valid($str)) {
714 utf8::decode($str);
715 return $str;
716 } else {
717 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
721 # quote unsafe chars, but keep the slash, even when it's not
722 # correct, but quoted slashes look too horrible in bookmarks
723 sub esc_param {
724 my $str = shift;
725 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
726 $str =~ s/\+/%2B/g;
727 $str =~ s/ /\+/g;
728 return $str;
731 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
732 sub esc_url {
733 my $str = shift;
734 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
735 $str =~ s/\+/%2B/g;
736 $str =~ s/ /\+/g;
737 return $str;
740 # replace invalid utf8 character with SUBSTITUTION sequence
741 sub esc_html ($;%) {
742 my $str = shift;
743 my %opts = @_;
745 $str = to_utf8($str);
746 $str = $cgi->escapeHTML($str);
747 if ($opts{'-nbsp'}) {
748 $str =~ s/ /&nbsp;/g;
750 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
751 return $str;
754 # quote control characters and escape filename to HTML
755 sub esc_path {
756 my $str = shift;
757 my %opts = @_;
759 $str = to_utf8($str);
760 $str = $cgi->escapeHTML($str);
761 if ($opts{'-nbsp'}) {
762 $str =~ s/ /&nbsp;/g;
764 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
765 return $str;
768 # Make control characters "printable", using character escape codes (CEC)
769 sub quot_cec {
770 my $cntrl = shift;
771 my %opts = @_;
772 my %es = ( # character escape codes, aka escape sequences
773 "\t" => '\t', # tab (HT)
774 "\n" => '\n', # line feed (LF)
775 "\r" => '\r', # carrige return (CR)
776 "\f" => '\f', # form feed (FF)
777 "\b" => '\b', # backspace (BS)
778 "\a" => '\a', # alarm (bell) (BEL)
779 "\e" => '\e', # escape (ESC)
780 "\013" => '\v', # vertical tab (VT)
781 "\000" => '\0', # nul character (NUL)
783 my $chr = ( (exists $es{$cntrl})
784 ? $es{$cntrl}
785 : sprintf('\%2x', ord($cntrl)) );
786 if ($opts{-nohtml}) {
787 return $chr;
788 } else {
789 return "<span class=\"cntrl\">$chr</span>";
793 # Alternatively use unicode control pictures codepoints,
794 # Unicode "printable representation" (PR)
795 sub quot_upr {
796 my $cntrl = shift;
797 my %opts = @_;
799 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
800 if ($opts{-nohtml}) {
801 return $chr;
802 } else {
803 return "<span class=\"cntrl\">$chr</span>";
807 # git may return quoted and escaped filenames
808 sub unquote {
809 my $str = shift;
811 sub unq {
812 my $seq = shift;
813 my %es = ( # character escape codes, aka escape sequences
814 't' => "\t", # tab (HT, TAB)
815 'n' => "\n", # newline (NL)
816 'r' => "\r", # return (CR)
817 'f' => "\f", # form feed (FF)
818 'b' => "\b", # backspace (BS)
819 'a' => "\a", # alarm (bell) (BEL)
820 'e' => "\e", # escape (ESC)
821 'v' => "\013", # vertical tab (VT)
824 if ($seq =~ m/^[0-7]{1,3}$/) {
825 # octal char sequence
826 return chr(oct($seq));
827 } elsif (exists $es{$seq}) {
828 # C escape sequence, aka character escape code
829 return $es{$seq};
831 # quoted ordinary character
832 return $seq;
835 if ($str =~ m/^"(.*)"$/) {
836 # needs unquoting
837 $str = $1;
838 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
840 return $str;
843 # escape tabs (convert tabs to spaces)
844 sub untabify {
845 my $line = shift;
847 while ((my $pos = index($line, "\t")) != -1) {
848 if (my $count = (8 - ($pos % 8))) {
849 my $spaces = ' ' x $count;
850 $line =~ s/\t/$spaces/;
854 return $line;
857 sub project_in_list {
858 my $project = shift;
859 my @list = git_get_projects_list();
860 return @list && scalar(grep { $_->{'path'} eq $project } @list);
863 ## ----------------------------------------------------------------------
864 ## HTML aware string manipulation
866 # Try to chop given string on a word boundary between position
867 # $len and $len+$add_len. If there is no word boundary there,
868 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
869 # (marking chopped part) would be longer than given string.
870 sub chop_str {
871 my $str = shift;
872 my $len = shift;
873 my $add_len = shift || 10;
874 my $where = shift || 'right'; # 'left' | 'center' | 'right'
876 # Make sure perl knows it is utf8 encoded so we don't
877 # cut in the middle of a utf8 multibyte char.
878 $str = to_utf8($str);
880 # allow only $len chars, but don't cut a word if it would fit in $add_len
881 # if it doesn't fit, cut it if it's still longer than the dots we would add
882 # remove chopped character entities entirely
884 # when chopping in the middle, distribute $len into left and right part
885 # return early if chopping wouldn't make string shorter
886 if ($where eq 'center') {
887 return $str if ($len + 5 >= length($str)); # filler is length 5
888 $len = int($len/2);
889 } else {
890 return $str if ($len + 4 >= length($str)); # filler is length 4
893 # regexps: ending and beginning with word part up to $add_len
894 my $endre = qr/.{$len}\w{0,$add_len}/;
895 my $begre = qr/\w{0,$add_len}.{$len}/;
897 if ($where eq 'left') {
898 $str =~ m/^(.*?)($begre)$/;
899 my ($lead, $body) = ($1, $2);
900 if (length($lead) > 4) {
901 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
902 $lead = " ...";
904 return "$lead$body";
906 } elsif ($where eq 'center') {
907 $str =~ m/^($endre)(.*)$/;
908 my ($left, $str) = ($1, $2);
909 $str =~ m/^(.*?)($begre)$/;
910 my ($mid, $right) = ($1, $2);
911 if (length($mid) > 5) {
912 $left =~ s/&[^;]*$//;
913 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
914 $mid = " ... ";
916 return "$left$mid$right";
918 } else {
919 $str =~ m/^($endre)(.*)$/;
920 my $body = $1;
921 my $tail = $2;
922 if (length($tail) > 4) {
923 $body =~ s/&[^;]*$//;
924 $tail = "... ";
926 return "$body$tail";
930 # takes the same arguments as chop_str, but also wraps a <span> around the
931 # result with a title attribute if it does get chopped. Additionally, the
932 # string is HTML-escaped.
933 sub chop_and_escape_str {
934 my ($str) = @_;
936 my $chopped = chop_str(@_);
937 if ($chopped eq $str) {
938 return esc_html($chopped);
939 } else {
940 $str =~ s/([[:cntrl:]])/?/g;
941 return $cgi->span({-title=>$str}, esc_html($chopped));
945 ## ----------------------------------------------------------------------
946 ## functions returning short strings
948 # CSS class for given age value (in seconds)
949 sub age_class {
950 my $age = shift;
952 if (!defined $age) {
953 return "noage";
954 } elsif ($age < 60*60*2) {
955 return "age0";
956 } elsif ($age < 60*60*24*2) {
957 return "age1";
958 } else {
959 return "age2";
963 # convert age in seconds to "nn units ago" string
964 sub age_string {
965 my $age = shift;
966 my $age_str;
968 if ($age > 60*60*24*365*2) {
969 $age_str = (int $age/60/60/24/365);
970 $age_str .= " years ago";
971 } elsif ($age > 60*60*24*(365/12)*2) {
972 $age_str = int $age/60/60/24/(365/12);
973 $age_str .= " months ago";
974 } elsif ($age > 60*60*24*7*2) {
975 $age_str = int $age/60/60/24/7;
976 $age_str .= " weeks ago";
977 } elsif ($age > 60*60*24*2) {
978 $age_str = int $age/60/60/24;
979 $age_str .= " days ago";
980 } elsif ($age > 60*60*2) {
981 $age_str = int $age/60/60;
982 $age_str .= " hours ago";
983 } elsif ($age > 60*2) {
984 $age_str = int $age/60;
985 $age_str .= " min ago";
986 } elsif ($age > 2) {
987 $age_str = int $age;
988 $age_str .= " sec ago";
989 } else {
990 $age_str .= " right now";
992 return $age_str;
995 use constant {
996 S_IFINVALID => 0030000,
997 S_IFGITLINK => 0160000,
1000 # submodule/subproject, a commit object reference
1001 sub S_ISGITLINK($) {
1002 my $mode = shift;
1004 return (($mode & S_IFMT) == S_IFGITLINK)
1007 # convert file mode in octal to symbolic file mode string
1008 sub mode_str {
1009 my $mode = oct shift;
1011 if (S_ISGITLINK($mode)) {
1012 return 'm---------';
1013 } elsif (S_ISDIR($mode & S_IFMT)) {
1014 return 'drwxr-xr-x';
1015 } elsif (S_ISLNK($mode)) {
1016 return 'lrwxrwxrwx';
1017 } elsif (S_ISREG($mode)) {
1018 # git cares only about the executable bit
1019 if ($mode & S_IXUSR) {
1020 return '-rwxr-xr-x';
1021 } else {
1022 return '-rw-r--r--';
1024 } else {
1025 return '----------';
1029 # convert file mode in octal to file type string
1030 sub file_type {
1031 my $mode = shift;
1033 if ($mode !~ m/^[0-7]+$/) {
1034 return $mode;
1035 } else {
1036 $mode = oct $mode;
1039 if (S_ISGITLINK($mode)) {
1040 return "submodule";
1041 } elsif (S_ISDIR($mode & S_IFMT)) {
1042 return "directory";
1043 } elsif (S_ISLNK($mode)) {
1044 return "symlink";
1045 } elsif (S_ISREG($mode)) {
1046 return "file";
1047 } else {
1048 return "unknown";
1052 # convert file mode in octal to file type description string
1053 sub file_type_long {
1054 my $mode = shift;
1056 if ($mode !~ m/^[0-7]+$/) {
1057 return $mode;
1058 } else {
1059 $mode = oct $mode;
1062 if (S_ISGITLINK($mode)) {
1063 return "submodule";
1064 } elsif (S_ISDIR($mode & S_IFMT)) {
1065 return "directory";
1066 } elsif (S_ISLNK($mode)) {
1067 return "symlink";
1068 } elsif (S_ISREG($mode)) {
1069 if ($mode & S_IXUSR) {
1070 return "executable";
1071 } else {
1072 return "file";
1074 } else {
1075 return "unknown";
1080 ## ----------------------------------------------------------------------
1081 ## functions returning short HTML fragments, or transforming HTML fragments
1082 ## which don't belong to other sections
1084 # format line of commit message.
1085 sub format_log_line_html {
1086 my $line = shift;
1088 $line = esc_html($line, -nbsp=>1);
1089 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1090 my $hash_text = $1;
1091 my $link =
1092 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1093 -class => "text"}, $hash_text);
1094 $line =~ s/$hash_text/$link/;
1096 return $line;
1099 # format marker of refs pointing to given object
1101 # the destination action is chosen based on object type and current context:
1102 # - for annotated tags, we choose the tag view unless it's the current view
1103 # already, in which case we go to shortlog view
1104 # - for other refs, we keep the current view if we're in history, shortlog or
1105 # log view, and select shortlog otherwise
1106 sub format_ref_marker {
1107 my ($refs, $id) = @_;
1108 my $markers = '';
1110 if (defined $refs->{$id}) {
1111 foreach my $ref (@{$refs->{$id}}) {
1112 # this code exploits the fact that non-lightweight tags are the
1113 # only indirect objects, and that they are the only objects for which
1114 # we want to use tag instead of shortlog as action
1115 my ($type, $name) = qw();
1116 my $indirect = ($ref =~ s/\^\{\}$//);
1117 # e.g. tags/v2.6.11 or heads/next
1118 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1119 $type = $1;
1120 $name = $2;
1121 } else {
1122 $type = "ref";
1123 $name = $ref;
1126 my $class = $type;
1127 $class .= " indirect" if $indirect;
1129 my $dest_action = "shortlog";
1131 if ($indirect) {
1132 $dest_action = "tag" unless $action eq "tag";
1133 } elsif ($action =~ /^(history|(short)?log)$/) {
1134 $dest_action = $action;
1137 my $dest = "";
1138 $dest .= "refs/" unless $ref =~ m!^refs/!;
1139 $dest .= $ref;
1141 my $link = $cgi->a({
1142 -href => href(
1143 action=>$dest_action,
1144 hash=>$dest
1145 )}, $name);
1147 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1148 $link . "</span>";
1152 if ($markers) {
1153 return ' <span class="refs">'. $markers . '</span>';
1154 } else {
1155 return "";
1159 # format, perhaps shortened and with markers, title line
1160 sub format_subject_html {
1161 my ($long, $short, $href, $extra) = @_;
1162 $extra = '' unless defined($extra);
1164 if (length($short) < length($long)) {
1165 return $cgi->a({-href => $href, -class => "list subject",
1166 -title => to_utf8($long)},
1167 esc_html($short) . $extra);
1168 } else {
1169 return $cgi->a({-href => $href, -class => "list subject"},
1170 esc_html($long) . $extra);
1174 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1175 sub format_git_diff_header_line {
1176 my $line = shift;
1177 my $diffinfo = shift;
1178 my ($from, $to) = @_;
1180 if ($diffinfo->{'nparents'}) {
1181 # combined diff
1182 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1183 if ($to->{'href'}) {
1184 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1185 esc_path($to->{'file'}));
1186 } else { # file was deleted (no href)
1187 $line .= esc_path($to->{'file'});
1189 } else {
1190 # "ordinary" diff
1191 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1192 if ($from->{'href'}) {
1193 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1194 'a/' . esc_path($from->{'file'}));
1195 } else { # file was added (no href)
1196 $line .= 'a/' . esc_path($from->{'file'});
1198 $line .= ' ';
1199 if ($to->{'href'}) {
1200 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1201 'b/' . esc_path($to->{'file'}));
1202 } else { # file was deleted
1203 $line .= 'b/' . esc_path($to->{'file'});
1207 return "<div class=\"diff header\">$line</div>\n";
1210 # format extended diff header line, before patch itself
1211 sub format_extended_diff_header_line {
1212 my $line = shift;
1213 my $diffinfo = shift;
1214 my ($from, $to) = @_;
1216 # match <path>
1217 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1218 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1219 esc_path($from->{'file'}));
1221 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1222 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1223 esc_path($to->{'file'}));
1225 # match single <mode>
1226 if ($line =~ m/\s(\d{6})$/) {
1227 $line .= '<span class="info"> (' .
1228 file_type_long($1) .
1229 ')</span>';
1231 # match <hash>
1232 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1233 # can match only for combined diff
1234 $line = 'index ';
1235 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1236 if ($from->{'href'}[$i]) {
1237 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1238 -class=>"hash"},
1239 substr($diffinfo->{'from_id'}[$i],0,7));
1240 } else {
1241 $line .= '0' x 7;
1243 # separator
1244 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1246 $line .= '..';
1247 if ($to->{'href'}) {
1248 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1249 substr($diffinfo->{'to_id'},0,7));
1250 } else {
1251 $line .= '0' x 7;
1254 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1255 # can match only for ordinary diff
1256 my ($from_link, $to_link);
1257 if ($from->{'href'}) {
1258 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1259 substr($diffinfo->{'from_id'},0,7));
1260 } else {
1261 $from_link = '0' x 7;
1263 if ($to->{'href'}) {
1264 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1265 substr($diffinfo->{'to_id'},0,7));
1266 } else {
1267 $to_link = '0' x 7;
1269 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1270 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1273 return $line . "<br/>\n";
1276 # format from-file/to-file diff header
1277 sub format_diff_from_to_header {
1278 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1279 my $line;
1280 my $result = '';
1282 $line = $from_line;
1283 #assert($line =~ m/^---/) if DEBUG;
1284 # no extra formatting for "^--- /dev/null"
1285 if (! $diffinfo->{'nparents'}) {
1286 # ordinary (single parent) diff
1287 if ($line =~ m!^--- "?a/!) {
1288 if ($from->{'href'}) {
1289 $line = '--- a/' .
1290 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1291 esc_path($from->{'file'}));
1292 } else {
1293 $line = '--- a/' .
1294 esc_path($from->{'file'});
1297 $result .= qq!<div class="diff from_file">$line</div>\n!;
1299 } else {
1300 # combined diff (merge commit)
1301 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1302 if ($from->{'href'}[$i]) {
1303 $line = '--- ' .
1304 $cgi->a({-href=>href(action=>"blobdiff",
1305 hash_parent=>$diffinfo->{'from_id'}[$i],
1306 hash_parent_base=>$parents[$i],
1307 file_parent=>$from->{'file'}[$i],
1308 hash=>$diffinfo->{'to_id'},
1309 hash_base=>$hash,
1310 file_name=>$to->{'file'}),
1311 -class=>"path",
1312 -title=>"diff" . ($i+1)},
1313 $i+1) .
1314 '/' .
1315 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1316 esc_path($from->{'file'}[$i]));
1317 } else {
1318 $line = '--- /dev/null';
1320 $result .= qq!<div class="diff from_file">$line</div>\n!;
1324 $line = $to_line;
1325 #assert($line =~ m/^\+\+\+/) if DEBUG;
1326 # no extra formatting for "^+++ /dev/null"
1327 if ($line =~ m!^\+\+\+ "?b/!) {
1328 if ($to->{'href'}) {
1329 $line = '+++ b/' .
1330 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1331 esc_path($to->{'file'}));
1332 } else {
1333 $line = '+++ b/' .
1334 esc_path($to->{'file'});
1337 $result .= qq!<div class="diff to_file">$line</div>\n!;
1339 return $result;
1342 # create note for patch simplified by combined diff
1343 sub format_diff_cc_simplified {
1344 my ($diffinfo, @parents) = @_;
1345 my $result = '';
1347 $result .= "<div class=\"diff header\">" .
1348 "diff --cc ";
1349 if (!is_deleted($diffinfo)) {
1350 $result .= $cgi->a({-href => href(action=>"blob",
1351 hash_base=>$hash,
1352 hash=>$diffinfo->{'to_id'},
1353 file_name=>$diffinfo->{'to_file'}),
1354 -class => "path"},
1355 esc_path($diffinfo->{'to_file'}));
1356 } else {
1357 $result .= esc_path($diffinfo->{'to_file'});
1359 $result .= "</div>\n" . # class="diff header"
1360 "<div class=\"diff nodifferences\">" .
1361 "Simple merge" .
1362 "</div>\n"; # class="diff nodifferences"
1364 return $result;
1367 # format patch (diff) line (not to be used for diff headers)
1368 sub format_diff_line {
1369 my $line = shift;
1370 my ($from, $to) = @_;
1371 my $diff_class = "";
1373 chomp $line;
1375 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1376 # combined diff
1377 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1378 if ($line =~ m/^\@{3}/) {
1379 $diff_class = " chunk_header";
1380 } elsif ($line =~ m/^\\/) {
1381 $diff_class = " incomplete";
1382 } elsif ($prefix =~ tr/+/+/) {
1383 $diff_class = " add";
1384 } elsif ($prefix =~ tr/-/-/) {
1385 $diff_class = " rem";
1387 } else {
1388 # assume ordinary diff
1389 my $char = substr($line, 0, 1);
1390 if ($char eq '+') {
1391 $diff_class = " add";
1392 } elsif ($char eq '-') {
1393 $diff_class = " rem";
1394 } elsif ($char eq '@') {
1395 $diff_class = " chunk_header";
1396 } elsif ($char eq "\\") {
1397 $diff_class = " incomplete";
1400 $line = untabify($line);
1401 if ($from && $to && $line =~ m/^\@{2} /) {
1402 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1403 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1405 $from_lines = 0 unless defined $from_lines;
1406 $to_lines = 0 unless defined $to_lines;
1408 if ($from->{'href'}) {
1409 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1410 -class=>"list"}, $from_text);
1412 if ($to->{'href'}) {
1413 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1414 -class=>"list"}, $to_text);
1416 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1417 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1418 return "<div class=\"diff$diff_class\">$line</div>\n";
1419 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1420 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1421 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1423 @from_text = split(' ', $ranges);
1424 for (my $i = 0; $i < @from_text; ++$i) {
1425 ($from_start[$i], $from_nlines[$i]) =
1426 (split(',', substr($from_text[$i], 1)), 0);
1429 $to_text = pop @from_text;
1430 $to_start = pop @from_start;
1431 $to_nlines = pop @from_nlines;
1433 $line = "<span class=\"chunk_info\">$prefix ";
1434 for (my $i = 0; $i < @from_text; ++$i) {
1435 if ($from->{'href'}[$i]) {
1436 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1437 -class=>"list"}, $from_text[$i]);
1438 } else {
1439 $line .= $from_text[$i];
1441 $line .= " ";
1443 if ($to->{'href'}) {
1444 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1445 -class=>"list"}, $to_text);
1446 } else {
1447 $line .= $to_text;
1449 $line .= " $prefix</span>" .
1450 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1451 return "<div class=\"diff$diff_class\">$line</div>\n";
1453 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1456 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1457 # linked. Pass the hash of the tree/commit to snapshot.
1458 sub format_snapshot_links {
1459 my ($hash) = @_;
1460 my @snapshot_fmts = gitweb_check_feature('snapshot');
1461 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1462 my $num_fmts = @snapshot_fmts;
1463 if ($num_fmts > 1) {
1464 # A parenthesized list of links bearing format names.
1465 # e.g. "snapshot (_tar.gz_ _zip_)"
1466 return "snapshot (" . join(' ', map
1467 $cgi->a({
1468 -href => href(
1469 action=>"snapshot",
1470 hash=>$hash,
1471 snapshot_format=>$_
1473 }, $known_snapshot_formats{$_}{'display'})
1474 , @snapshot_fmts) . ")";
1475 } elsif ($num_fmts == 1) {
1476 # A single "snapshot" link whose tooltip bears the format name.
1477 # i.e. "_snapshot_"
1478 my ($fmt) = @snapshot_fmts;
1479 return
1480 $cgi->a({
1481 -href => href(
1482 action=>"snapshot",
1483 hash=>$hash,
1484 snapshot_format=>$fmt
1486 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1487 }, "snapshot");
1488 } else { # $num_fmts == 0
1489 return undef;
1493 ## ......................................................................
1494 ## functions returning values to be passed, perhaps after some
1495 ## transformation, to other functions; e.g. returning arguments to href()
1497 # returns hash to be passed to href to generate gitweb URL
1498 # in -title key it returns description of link
1499 sub get_feed_info {
1500 my $format = shift || 'Atom';
1501 my %res = (action => lc($format));
1503 # feed links are possible only for project views
1504 return unless (defined $project);
1505 # some views should link to OPML, or to generic project feed,
1506 # or don't have specific feed yet (so they should use generic)
1507 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1509 my $branch;
1510 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1511 # from tag links; this also makes possible to detect branch links
1512 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1513 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1514 $branch = $1;
1516 # find log type for feed description (title)
1517 my $type = 'log';
1518 if (defined $file_name) {
1519 $type = "history of $file_name";
1520 $type .= "/" if ($action eq 'tree');
1521 $type .= " on '$branch'" if (defined $branch);
1522 } else {
1523 $type = "log of $branch" if (defined $branch);
1526 $res{-title} = $type;
1527 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1528 $res{'file_name'} = $file_name;
1530 return %res;
1533 ## ----------------------------------------------------------------------
1534 ## git utility subroutines, invoking git commands
1536 # returns path to the core git executable and the --git-dir parameter as list
1537 sub git_cmd {
1538 return $GIT, '--git-dir='.$git_dir;
1541 # quote the given arguments for passing them to the shell
1542 # quote_command("command", "arg 1", "arg with ' and ! characters")
1543 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1544 # Try to avoid using this function wherever possible.
1545 sub quote_command {
1546 return join(' ',
1547 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1550 # get HEAD ref of given project as hash
1551 sub git_get_head_hash {
1552 my $project = shift;
1553 my $o_git_dir = $git_dir;
1554 my $retval = undef;
1555 $git_dir = "$projectroot/$project";
1556 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1557 my $head = <$fd>;
1558 close $fd;
1559 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1560 $retval = $1;
1563 if (defined $o_git_dir) {
1564 $git_dir = $o_git_dir;
1566 return $retval;
1569 # get type of given object
1570 sub git_get_type {
1571 my $hash = shift;
1573 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1574 my $type = <$fd>;
1575 close $fd or return;
1576 chomp $type;
1577 return $type;
1580 # repository configuration
1581 our $config_file = '';
1582 our %config;
1584 # store multiple values for single key as anonymous array reference
1585 # single values stored directly in the hash, not as [ <value> ]
1586 sub hash_set_multi {
1587 my ($hash, $key, $value) = @_;
1589 if (!exists $hash->{$key}) {
1590 $hash->{$key} = $value;
1591 } elsif (!ref $hash->{$key}) {
1592 $hash->{$key} = [ $hash->{$key}, $value ];
1593 } else {
1594 push @{$hash->{$key}}, $value;
1598 # return hash of git project configuration
1599 # optionally limited to some section, e.g. 'gitweb'
1600 sub git_parse_project_config {
1601 my $section_regexp = shift;
1602 my %config;
1604 local $/ = "\0";
1606 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1607 or return;
1609 while (my $keyval = <$fh>) {
1610 chomp $keyval;
1611 my ($key, $value) = split(/\n/, $keyval, 2);
1613 hash_set_multi(\%config, $key, $value)
1614 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1616 close $fh;
1618 return %config;
1621 # convert config value to boolean, 'true' or 'false'
1622 # no value, number > 0, 'true' and 'yes' values are true
1623 # rest of values are treated as false (never as error)
1624 sub config_to_bool {
1625 my $val = shift;
1627 # strip leading and trailing whitespace
1628 $val =~ s/^\s+//;
1629 $val =~ s/\s+$//;
1631 return (!defined $val || # section.key
1632 ($val =~ /^\d+$/ && $val) || # section.key = 1
1633 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1636 # convert config value to simple decimal number
1637 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1638 # to be multiplied by 1024, 1048576, or 1073741824
1639 sub config_to_int {
1640 my $val = shift;
1642 # strip leading and trailing whitespace
1643 $val =~ s/^\s+//;
1644 $val =~ s/\s+$//;
1646 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1647 $unit = lc($unit);
1648 # unknown unit is treated as 1
1649 return $num * ($unit eq 'g' ? 1073741824 :
1650 $unit eq 'm' ? 1048576 :
1651 $unit eq 'k' ? 1024 : 1);
1653 return $val;
1656 # convert config value to array reference, if needed
1657 sub config_to_multi {
1658 my $val = shift;
1660 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1663 sub git_get_project_config {
1664 my ($key, $type) = @_;
1666 # key sanity check
1667 return unless ($key);
1668 $key =~ s/^gitweb\.//;
1669 return if ($key =~ m/\W/);
1671 # type sanity check
1672 if (defined $type) {
1673 $type =~ s/^--//;
1674 $type = undef
1675 unless ($type eq 'bool' || $type eq 'int');
1678 # get config
1679 if (!defined $config_file ||
1680 $config_file ne "$git_dir/config") {
1681 %config = git_parse_project_config('gitweb');
1682 $config_file = "$git_dir/config";
1685 # ensure given type
1686 if (!defined $type) {
1687 return $config{"gitweb.$key"};
1688 } elsif ($type eq 'bool') {
1689 # backward compatibility: 'git config --bool' returns true/false
1690 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1691 } elsif ($type eq 'int') {
1692 return config_to_int($config{"gitweb.$key"});
1694 return $config{"gitweb.$key"};
1697 # get hash of given path at given ref
1698 sub git_get_hash_by_path {
1699 my $base = shift;
1700 my $path = shift || return undef;
1701 my $type = shift;
1703 $path =~ s,/+$,,;
1705 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1706 or die_error(500, "Open git-ls-tree failed");
1707 my $line = <$fd>;
1708 close $fd or return undef;
1710 if (!defined $line) {
1711 # there is no tree or hash given by $path at $base
1712 return undef;
1715 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1716 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1717 if (defined $type && $type ne $2) {
1718 # type doesn't match
1719 return undef;
1721 return $3;
1724 # get path of entry with given hash at given tree-ish (ref)
1725 # used to get 'from' filename for combined diff (merge commit) for renames
1726 sub git_get_path_by_hash {
1727 my $base = shift || return;
1728 my $hash = shift || return;
1730 local $/ = "\0";
1732 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1733 or return undef;
1734 while (my $line = <$fd>) {
1735 chomp $line;
1737 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1738 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1739 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1740 close $fd;
1741 return $1;
1744 close $fd;
1745 return undef;
1748 ## ......................................................................
1749 ## git utility functions, directly accessing git repository
1751 sub git_get_project_description {
1752 my $path = shift;
1754 $git_dir = "$projectroot/$path";
1755 open my $fd, "$git_dir/description"
1756 or return git_get_project_config('description');
1757 my $descr = <$fd>;
1758 close $fd;
1759 if (defined $descr) {
1760 chomp $descr;
1762 return $descr;
1765 sub git_get_project_url_list {
1766 my $path = shift;
1768 $git_dir = "$projectroot/$path";
1769 open my $fd, "$git_dir/cloneurl"
1770 or return wantarray ?
1771 @{ config_to_multi(git_get_project_config('url')) } :
1772 config_to_multi(git_get_project_config('url'));
1773 my @git_project_url_list = map { chomp; $_ } <$fd>;
1774 close $fd;
1776 return wantarray ? @git_project_url_list : \@git_project_url_list;
1779 sub git_get_projects_list {
1780 my ($filter) = @_;
1781 my @list;
1783 $filter ||= '';
1784 $filter =~ s/\.git$//;
1786 my ($check_forks) = gitweb_check_feature('forks');
1788 if (-d $projects_list) {
1789 # search in directory
1790 my $dir = $projects_list . ($filter ? "/$filter" : '');
1791 # remove the trailing "/"
1792 $dir =~ s!/+$!!;
1793 my $pfxlen = length("$dir");
1794 my $pfxdepth = ($dir =~ tr!/!!);
1796 File::Find::find({
1797 follow_fast => 1, # follow symbolic links
1798 follow_skip => 2, # ignore duplicates
1799 dangling_symlinks => 0, # ignore dangling symlinks, silently
1800 wanted => sub {
1801 # skip project-list toplevel, if we get it.
1802 return if (m!^[/.]$!);
1803 # only directories can be git repositories
1804 return unless (-d $_);
1805 # don't traverse too deep (Find is super slow on os x)
1806 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1807 $File::Find::prune = 1;
1808 return;
1811 my $subdir = substr($File::Find::name, $pfxlen + 1);
1812 # we check related file in $projectroot
1813 if ($check_forks and $subdir =~ m#/.#) {
1814 $File::Find::prune = 1;
1815 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1816 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1817 $File::Find::prune = 1;
1820 }, "$dir");
1822 } elsif (-f $projects_list) {
1823 # read from file(url-encoded):
1824 # 'git%2Fgit.git Linus+Torvalds'
1825 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1826 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1827 my %paths;
1828 open my ($fd), $projects_list or return;
1829 PROJECT:
1830 while (my $line = <$fd>) {
1831 chomp $line;
1832 my ($path, $owner) = split ' ', $line;
1833 $path = unescape($path);
1834 $owner = unescape($owner);
1835 if (!defined $path) {
1836 next;
1838 if ($filter ne '') {
1839 # looking for forks;
1840 my $pfx = substr($path, 0, length($filter));
1841 if ($pfx ne $filter) {
1842 next PROJECT;
1844 my $sfx = substr($path, length($filter));
1845 if ($sfx !~ /^\/.*\.git$/) {
1846 next PROJECT;
1848 } elsif ($check_forks) {
1849 PATH:
1850 foreach my $filter (keys %paths) {
1851 # looking for forks;
1852 my $pfx = substr($path, 0, length($filter));
1853 if ($pfx ne $filter) {
1854 next PATH;
1856 my $sfx = substr($path, length($filter));
1857 if ($sfx !~ /^\/.*\.git$/) {
1858 next PATH;
1860 # is a fork, don't include it in
1861 # the list
1862 next PROJECT;
1865 if (check_export_ok("$projectroot/$path")) {
1866 my $pr = {
1867 path => $path,
1868 owner => to_utf8($owner),
1870 push @list, $pr;
1871 (my $forks_path = $path) =~ s/\.git$//;
1872 $paths{$forks_path}++;
1875 close $fd;
1877 return @list;
1880 our $gitweb_project_owner = undef;
1881 sub git_get_project_list_from_file {
1883 return if (defined $gitweb_project_owner);
1885 $gitweb_project_owner = {};
1886 # read from file (url-encoded):
1887 # 'git%2Fgit.git Linus+Torvalds'
1888 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1889 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1890 if (-f $projects_list) {
1891 open (my $fd , $projects_list);
1892 while (my $line = <$fd>) {
1893 chomp $line;
1894 my ($pr, $ow) = split ' ', $line;
1895 $pr = unescape($pr);
1896 $ow = unescape($ow);
1897 $gitweb_project_owner->{$pr} = to_utf8($ow);
1899 close $fd;
1903 sub git_get_project_owner {
1904 my $project = shift;
1905 my $owner;
1907 return undef unless $project;
1908 $git_dir = "$projectroot/$project";
1910 if (!defined $gitweb_project_owner) {
1911 git_get_project_list_from_file();
1914 if (exists $gitweb_project_owner->{$project}) {
1915 $owner = $gitweb_project_owner->{$project};
1917 if (!defined $owner){
1918 $owner = git_get_project_config('owner');
1920 if (!defined $owner) {
1921 $owner = get_file_owner("$git_dir");
1924 return $owner;
1927 sub git_get_last_activity {
1928 my ($path) = @_;
1929 my $fd;
1931 $git_dir = "$projectroot/$path";
1932 open($fd, "-|", git_cmd(), 'for-each-ref',
1933 '--format=%(committer)',
1934 '--sort=-committerdate',
1935 '--count=1',
1936 'refs/heads') or return;
1937 my $most_recent = <$fd>;
1938 close $fd or return;
1939 if (defined $most_recent &&
1940 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1941 my $timestamp = $1;
1942 my $age = time - $timestamp;
1943 return ($age, age_string($age));
1945 return (undef, undef);
1948 sub git_get_references {
1949 my $type = shift || "";
1950 my %refs;
1951 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1952 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1953 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1954 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1955 or return;
1957 while (my $line = <$fd>) {
1958 chomp $line;
1959 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
1960 if (defined $refs{$1}) {
1961 push @{$refs{$1}}, $2;
1962 } else {
1963 $refs{$1} = [ $2 ];
1967 close $fd or return;
1968 return \%refs;
1971 sub git_get_rev_name_tags {
1972 my $hash = shift || return undef;
1974 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1975 or return;
1976 my $name_rev = <$fd>;
1977 close $fd;
1979 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1980 return $1;
1981 } else {
1982 # catches also '$hash undefined' output
1983 return undef;
1987 ## ----------------------------------------------------------------------
1988 ## parse to hash functions
1990 sub parse_date {
1991 my $epoch = shift;
1992 my $tz = shift || "-0000";
1994 my %date;
1995 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1996 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1997 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1998 $date{'hour'} = $hour;
1999 $date{'minute'} = $min;
2000 $date{'mday'} = $mday;
2001 $date{'day'} = $days[$wday];
2002 $date{'month'} = $months[$mon];
2003 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2004 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2005 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2006 $mday, $months[$mon], $hour ,$min;
2007 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2008 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2010 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2011 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2012 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2013 $date{'hour_local'} = $hour;
2014 $date{'minute_local'} = $min;
2015 $date{'tz_local'} = $tz;
2016 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2017 1900+$year, $mon+1, $mday,
2018 $hour, $min, $sec, $tz);
2019 return %date;
2022 sub parse_tag {
2023 my $tag_id = shift;
2024 my %tag;
2025 my @comment;
2027 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2028 $tag{'id'} = $tag_id;
2029 while (my $line = <$fd>) {
2030 chomp $line;
2031 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2032 $tag{'object'} = $1;
2033 } elsif ($line =~ m/^type (.+)$/) {
2034 $tag{'type'} = $1;
2035 } elsif ($line =~ m/^tag (.+)$/) {
2036 $tag{'name'} = $1;
2037 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2038 $tag{'author'} = $1;
2039 $tag{'epoch'} = $2;
2040 $tag{'tz'} = $3;
2041 } elsif ($line =~ m/--BEGIN/) {
2042 push @comment, $line;
2043 last;
2044 } elsif ($line eq "") {
2045 last;
2048 push @comment, <$fd>;
2049 $tag{'comment'} = \@comment;
2050 close $fd or return;
2051 if (!defined $tag{'name'}) {
2052 return
2054 return %tag
2057 sub parse_commit_text {
2058 my ($commit_text, $withparents) = @_;
2059 my @commit_lines = split '\n', $commit_text;
2060 my %co;
2062 pop @commit_lines; # Remove '\0'
2064 if (! @commit_lines) {
2065 return;
2068 my $header = shift @commit_lines;
2069 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2070 return;
2072 ($co{'id'}, my @parents) = split ' ', $header;
2073 while (my $line = shift @commit_lines) {
2074 last if $line eq "\n";
2075 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2076 $co{'tree'} = $1;
2077 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2078 push @parents, $1;
2079 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2080 $co{'author'} = $1;
2081 $co{'author_epoch'} = $2;
2082 $co{'author_tz'} = $3;
2083 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2084 $co{'author_name'} = $1;
2085 $co{'author_email'} = $2;
2086 } else {
2087 $co{'author_name'} = $co{'author'};
2089 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2090 $co{'committer'} = $1;
2091 $co{'committer_epoch'} = $2;
2092 $co{'committer_tz'} = $3;
2093 $co{'committer_name'} = $co{'committer'};
2094 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2095 $co{'committer_name'} = $1;
2096 $co{'committer_email'} = $2;
2097 } else {
2098 $co{'committer_name'} = $co{'committer'};
2102 if (!defined $co{'tree'}) {
2103 return;
2105 $co{'parents'} = \@parents;
2106 $co{'parent'} = $parents[0];
2108 foreach my $title (@commit_lines) {
2109 $title =~ s/^ //;
2110 if ($title ne "") {
2111 $co{'title'} = chop_str($title, 80, 5);
2112 # remove leading stuff of merges to make the interesting part visible
2113 if (length($title) > 50) {
2114 $title =~ s/^Automatic //;
2115 $title =~ s/^merge (of|with) /Merge ... /i;
2116 if (length($title) > 50) {
2117 $title =~ s/(http|rsync):\/\///;
2119 if (length($title) > 50) {
2120 $title =~ s/(master|www|rsync)\.//;
2122 if (length($title) > 50) {
2123 $title =~ s/kernel.org:?//;
2125 if (length($title) > 50) {
2126 $title =~ s/\/pub\/scm//;
2129 $co{'title_short'} = chop_str($title, 50, 5);
2130 last;
2133 if (! defined $co{'title'} || $co{'title'} eq "") {
2134 $co{'title'} = $co{'title_short'} = '(no commit message)';
2136 # remove added spaces
2137 foreach my $line (@commit_lines) {
2138 $line =~ s/^ //;
2140 $co{'comment'} = \@commit_lines;
2142 my $age = time - $co{'committer_epoch'};
2143 $co{'age'} = $age;
2144 $co{'age_string'} = age_string($age);
2145 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2146 if ($age > 60*60*24*7*2) {
2147 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2148 $co{'age_string_age'} = $co{'age_string'};
2149 } else {
2150 $co{'age_string_date'} = $co{'age_string'};
2151 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2153 return %co;
2156 sub parse_commit {
2157 my ($commit_id) = @_;
2158 my %co;
2160 local $/ = "\0";
2162 open my $fd, "-|", git_cmd(), "rev-list",
2163 "--parents",
2164 "--header",
2165 "--max-count=1",
2166 $commit_id,
2167 "--",
2168 or die_error(500, "Open git-rev-list failed");
2169 %co = parse_commit_text(<$fd>, 1);
2170 close $fd;
2172 return %co;
2175 sub parse_commits {
2176 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2177 my @cos;
2179 $maxcount ||= 1;
2180 $skip ||= 0;
2182 local $/ = "\0";
2184 open my $fd, "-|", git_cmd(), "rev-list",
2185 "--header",
2186 @args,
2187 ("--max-count=" . $maxcount),
2188 ("--skip=" . $skip),
2189 @extra_options,
2190 $commit_id,
2191 "--",
2192 ($filename ? ($filename) : ())
2193 or die_error(500, "Open git-rev-list failed");
2194 while (my $line = <$fd>) {
2195 my %co = parse_commit_text($line);
2196 push @cos, \%co;
2198 close $fd;
2200 return wantarray ? @cos : \@cos;
2203 # parse line of git-diff-tree "raw" output
2204 sub parse_difftree_raw_line {
2205 my $line = shift;
2206 my %res;
2208 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2209 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2210 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2211 $res{'from_mode'} = $1;
2212 $res{'to_mode'} = $2;
2213 $res{'from_id'} = $3;
2214 $res{'to_id'} = $4;
2215 $res{'status'} = $5;
2216 $res{'similarity'} = $6;
2217 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2218 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2219 } else {
2220 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2223 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2224 # combined diff (for merge commit)
2225 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2226 $res{'nparents'} = length($1);
2227 $res{'from_mode'} = [ split(' ', $2) ];
2228 $res{'to_mode'} = pop @{$res{'from_mode'}};
2229 $res{'from_id'} = [ split(' ', $3) ];
2230 $res{'to_id'} = pop @{$res{'from_id'}};
2231 $res{'status'} = [ split('', $4) ];
2232 $res{'to_file'} = unquote($5);
2234 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2235 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2236 $res{'commit'} = $1;
2239 return wantarray ? %res : \%res;
2242 # wrapper: return parsed line of git-diff-tree "raw" output
2243 # (the argument might be raw line, or parsed info)
2244 sub parsed_difftree_line {
2245 my $line_or_ref = shift;
2247 if (ref($line_or_ref) eq "HASH") {
2248 # pre-parsed (or generated by hand)
2249 return $line_or_ref;
2250 } else {
2251 return parse_difftree_raw_line($line_or_ref);
2255 # parse line of git-ls-tree output
2256 sub parse_ls_tree_line ($;%) {
2257 my $line = shift;
2258 my %opts = @_;
2259 my %res;
2261 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2262 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2264 $res{'mode'} = $1;
2265 $res{'type'} = $2;
2266 $res{'hash'} = $3;
2267 if ($opts{'-z'}) {
2268 $res{'name'} = $4;
2269 } else {
2270 $res{'name'} = unquote($4);
2273 return wantarray ? %res : \%res;
2276 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2277 sub parse_from_to_diffinfo {
2278 my ($diffinfo, $from, $to, @parents) = @_;
2280 if ($diffinfo->{'nparents'}) {
2281 # combined diff
2282 $from->{'file'} = [];
2283 $from->{'href'} = [];
2284 fill_from_file_info($diffinfo, @parents)
2285 unless exists $diffinfo->{'from_file'};
2286 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2287 $from->{'file'}[$i] =
2288 defined $diffinfo->{'from_file'}[$i] ?
2289 $diffinfo->{'from_file'}[$i] :
2290 $diffinfo->{'to_file'};
2291 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2292 $from->{'href'}[$i] = href(action=>"blob",
2293 hash_base=>$parents[$i],
2294 hash=>$diffinfo->{'from_id'}[$i],
2295 file_name=>$from->{'file'}[$i]);
2296 } else {
2297 $from->{'href'}[$i] = undef;
2300 } else {
2301 # ordinary (not combined) diff
2302 $from->{'file'} = $diffinfo->{'from_file'};
2303 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2304 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2305 hash=>$diffinfo->{'from_id'},
2306 file_name=>$from->{'file'});
2307 } else {
2308 delete $from->{'href'};
2312 $to->{'file'} = $diffinfo->{'to_file'};
2313 if (!is_deleted($diffinfo)) { # file exists in result
2314 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2315 hash=>$diffinfo->{'to_id'},
2316 file_name=>$to->{'file'});
2317 } else {
2318 delete $to->{'href'};
2322 ## ......................................................................
2323 ## parse to array of hashes functions
2325 sub git_get_heads_list {
2326 my $limit = shift;
2327 my @headslist;
2329 open my $fd, '-|', git_cmd(), 'for-each-ref',
2330 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2331 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2332 'refs/heads'
2333 or return;
2334 while (my $line = <$fd>) {
2335 my %ref_item;
2337 chomp $line;
2338 my ($refinfo, $committerinfo) = split(/\0/, $line);
2339 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2340 my ($committer, $epoch, $tz) =
2341 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2342 $ref_item{'fullname'} = $name;
2343 $name =~ s!^refs/heads/!!;
2345 $ref_item{'name'} = $name;
2346 $ref_item{'id'} = $hash;
2347 $ref_item{'title'} = $title || '(no commit message)';
2348 $ref_item{'epoch'} = $epoch;
2349 if ($epoch) {
2350 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2351 } else {
2352 $ref_item{'age'} = "unknown";
2355 push @headslist, \%ref_item;
2357 close $fd;
2359 return wantarray ? @headslist : \@headslist;
2362 sub git_get_tags_list {
2363 my $limit = shift;
2364 my @tagslist;
2366 open my $fd, '-|', git_cmd(), 'for-each-ref',
2367 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2368 '--format=%(objectname) %(objecttype) %(refname) '.
2369 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2370 'refs/tags'
2371 or return;
2372 while (my $line = <$fd>) {
2373 my %ref_item;
2375 chomp $line;
2376 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2377 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2378 my ($creator, $epoch, $tz) =
2379 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2380 $ref_item{'fullname'} = $name;
2381 $name =~ s!^refs/tags/!!;
2383 $ref_item{'type'} = $type;
2384 $ref_item{'id'} = $id;
2385 $ref_item{'name'} = $name;
2386 if ($type eq "tag") {
2387 $ref_item{'subject'} = $title;
2388 $ref_item{'reftype'} = $reftype;
2389 $ref_item{'refid'} = $refid;
2390 } else {
2391 $ref_item{'reftype'} = $type;
2392 $ref_item{'refid'} = $id;
2395 if ($type eq "tag" || $type eq "commit") {
2396 $ref_item{'epoch'} = $epoch;
2397 if ($epoch) {
2398 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2399 } else {
2400 $ref_item{'age'} = "unknown";
2404 push @tagslist, \%ref_item;
2406 close $fd;
2408 return wantarray ? @tagslist : \@tagslist;
2411 ## ----------------------------------------------------------------------
2412 ## filesystem-related functions
2414 sub get_file_owner {
2415 my $path = shift;
2417 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2418 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2419 if (!defined $gcos) {
2420 return undef;
2422 my $owner = $gcos;
2423 $owner =~ s/[,;].*$//;
2424 return to_utf8($owner);
2427 ## ......................................................................
2428 ## mimetype related functions
2430 sub mimetype_guess_file {
2431 my $filename = shift;
2432 my $mimemap = shift;
2433 -r $mimemap or return undef;
2435 my %mimemap;
2436 open(MIME, $mimemap) or return undef;
2437 while (<MIME>) {
2438 next if m/^#/; # skip comments
2439 my ($mime, $exts) = split(/\t+/);
2440 if (defined $exts) {
2441 my @exts = split(/\s+/, $exts);
2442 foreach my $ext (@exts) {
2443 $mimemap{$ext} = $mime;
2447 close(MIME);
2449 $filename =~ /\.([^.]*)$/;
2450 return $mimemap{$1};
2453 sub mimetype_guess {
2454 my $filename = shift;
2455 my $mime;
2456 $filename =~ /\./ or return undef;
2458 if ($mimetypes_file) {
2459 my $file = $mimetypes_file;
2460 if ($file !~ m!^/!) { # if it is relative path
2461 # it is relative to project
2462 $file = "$projectroot/$project/$file";
2464 $mime = mimetype_guess_file($filename, $file);
2466 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2467 return $mime;
2470 sub blob_mimetype {
2471 my $fd = shift;
2472 my $filename = shift;
2474 if ($filename) {
2475 my $mime = mimetype_guess($filename);
2476 $mime and return $mime;
2479 # just in case
2480 return $default_blob_plain_mimetype unless $fd;
2482 if (-T $fd) {
2483 return 'text/plain';
2484 } elsif (! $filename) {
2485 return 'application/octet-stream';
2486 } elsif ($filename =~ m/\.png$/i) {
2487 return 'image/png';
2488 } elsif ($filename =~ m/\.gif$/i) {
2489 return 'image/gif';
2490 } elsif ($filename =~ m/\.jpe?g$/i) {
2491 return 'image/jpeg';
2492 } else {
2493 return 'application/octet-stream';
2497 sub blob_contenttype {
2498 my ($fd, $file_name, $type) = @_;
2500 $type ||= blob_mimetype($fd, $file_name);
2501 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2502 $type .= "; charset=$default_text_plain_charset";
2505 return $type;
2508 ## ======================================================================
2509 ## functions printing HTML: header, footer, error page
2511 sub git_header_html {
2512 my $status = shift || "200 OK";
2513 my $expires = shift;
2515 my $title = "$site_name";
2516 if (defined $project) {
2517 $title .= " - " . to_utf8($project);
2518 if (defined $action) {
2519 $title .= "/$action";
2520 if (defined $file_name) {
2521 $title .= " - " . esc_path($file_name);
2522 if ($action eq "tree" && $file_name !~ m|/$|) {
2523 $title .= "/";
2528 my $content_type;
2529 # require explicit support from the UA if we are to send the page as
2530 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2531 # we have to do this because MSIE sometimes globs '*/*', pretending to
2532 # support xhtml+xml but choking when it gets what it asked for.
2533 if (defined $cgi->http('HTTP_ACCEPT') &&
2534 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2535 $cgi->Accept('application/xhtml+xml') != 0) {
2536 $content_type = 'application/xhtml+xml';
2537 } else {
2538 $content_type = 'text/html';
2540 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2541 -status=> $status, -expires => $expires);
2542 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2543 print <<EOF;
2544 <?xml version="1.0" encoding="utf-8"?>
2545 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2546 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2547 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2548 <!-- git core binaries version $git_version -->
2549 <head>
2550 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2551 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2552 <meta name="robots" content="index, nofollow"/>
2553 <title>$title</title>
2555 # print out each stylesheet that exist
2556 if (defined $stylesheet) {
2557 #provides backwards capability for those people who define style sheet in a config file
2558 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2559 } else {
2560 foreach my $stylesheet (@stylesheets) {
2561 next unless $stylesheet;
2562 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2565 if (defined $project) {
2566 my %href_params = get_feed_info();
2567 if (!exists $href_params{'-title'}) {
2568 $href_params{'-title'} = 'log';
2571 foreach my $format qw(RSS Atom) {
2572 my $type = lc($format);
2573 my %link_attr = (
2574 '-rel' => 'alternate',
2575 '-title' => "$project - $href_params{'-title'} - $format feed",
2576 '-type' => "application/$type+xml"
2579 $href_params{'action'} = $type;
2580 $link_attr{'-href'} = href(%href_params);
2581 print "<link ".
2582 "rel=\"$link_attr{'-rel'}\" ".
2583 "title=\"$link_attr{'-title'}\" ".
2584 "href=\"$link_attr{'-href'}\" ".
2585 "type=\"$link_attr{'-type'}\" ".
2586 "/>\n";
2588 $href_params{'extra_options'} = '--no-merges';
2589 $link_attr{'-href'} = href(%href_params);
2590 $link_attr{'-title'} .= ' (no merges)';
2591 print "<link ".
2592 "rel=\"$link_attr{'-rel'}\" ".
2593 "title=\"$link_attr{'-title'}\" ".
2594 "href=\"$link_attr{'-href'}\" ".
2595 "type=\"$link_attr{'-type'}\" ".
2596 "/>\n";
2599 } else {
2600 printf('<link rel="alternate" title="%s projects list" '.
2601 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2602 $site_name, href(project=>undef, action=>"project_index"));
2603 printf('<link rel="alternate" title="%s projects feeds" '.
2604 'href="%s" type="text/x-opml" />'."\n",
2605 $site_name, href(project=>undef, action=>"opml"));
2607 if (defined $favicon) {
2608 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2611 print "</head>\n" .
2612 "<body>\n";
2614 if (-f $site_header) {
2615 open (my $fd, $site_header);
2616 print <$fd>;
2617 close $fd;
2620 print "<div class=\"page_header\">\n" .
2621 $cgi->a({-href => esc_url($logo_url),
2622 -title => $logo_label},
2623 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2624 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2625 if (defined $project) {
2626 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2627 if (defined $action) {
2628 print " / $action";
2630 print "\n";
2632 print "</div>\n";
2634 my ($have_search) = gitweb_check_feature('search');
2635 if (defined $project && $have_search) {
2636 if (!defined $searchtext) {
2637 $searchtext = "";
2639 my $search_hash;
2640 if (defined $hash_base) {
2641 $search_hash = $hash_base;
2642 } elsif (defined $hash) {
2643 $search_hash = $hash;
2644 } else {
2645 $search_hash = "HEAD";
2647 my $action = $my_uri;
2648 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2649 if ($use_pathinfo) {
2650 $action .= "/".esc_url($project);
2652 print $cgi->startform(-method => "get", -action => $action) .
2653 "<div class=\"search\">\n" .
2654 (!$use_pathinfo &&
2655 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2656 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2657 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2658 $cgi->popup_menu(-name => 'st', -default => 'commit',
2659 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2660 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2661 " search:\n",
2662 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2663 "<span title=\"Extended regular expression\">" .
2664 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
2665 -checked => $search_use_regexp) .
2666 "</span>" .
2667 "</div>" .
2668 $cgi->end_form() . "\n";
2672 sub git_footer_html {
2673 my $feed_class = 'rss_logo';
2675 print "<div class=\"page_footer\">\n";
2676 if (defined $project) {
2677 my $descr = git_get_project_description($project);
2678 if (defined $descr) {
2679 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2682 my %href_params = get_feed_info();
2683 if (!%href_params) {
2684 $feed_class .= ' generic';
2686 $href_params{'-title'} ||= 'log';
2688 foreach my $format qw(RSS Atom) {
2689 $href_params{'action'} = lc($format);
2690 print $cgi->a({-href => href(%href_params),
2691 -title => "$href_params{'-title'} $format feed",
2692 -class => $feed_class}, $format)."\n";
2695 } else {
2696 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2697 -class => $feed_class}, "OPML") . " ";
2698 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2699 -class => $feed_class}, "TXT") . "\n";
2701 print "</div>\n"; # class="page_footer"
2703 if (-f $site_footer) {
2704 open (my $fd, $site_footer);
2705 print <$fd>;
2706 close $fd;
2709 print "</body>\n" .
2710 "</html>";
2713 # die_error(<http_status_code>, <error_message>)
2714 # Example: die_error(404, 'Hash not found')
2715 # By convention, use the following status codes (as defined in RFC 2616):
2716 # 400: Invalid or missing CGI parameters, or
2717 # requested object exists but has wrong type.
2718 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
2719 # this server or project.
2720 # 404: Requested object/revision/project doesn't exist.
2721 # 500: The server isn't configured properly, or
2722 # an internal error occurred (e.g. failed assertions caused by bugs), or
2723 # an unknown error occurred (e.g. the git binary died unexpectedly).
2724 sub die_error {
2725 my $status = shift || 500;
2726 my $error = shift || "Internal server error";
2728 my %http_responses = (400 => '400 Bad Request',
2729 403 => '403 Forbidden',
2730 404 => '404 Not Found',
2731 500 => '500 Internal Server Error');
2732 git_header_html($http_responses{$status});
2733 print <<EOF;
2734 <div class="page_body">
2735 <br /><br />
2736 $status - $error
2737 <br />
2738 </div>
2740 git_footer_html();
2741 exit;
2744 ## ----------------------------------------------------------------------
2745 ## functions printing or outputting HTML: navigation
2747 sub git_print_page_nav {
2748 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2749 $extra = '' if !defined $extra; # pager or formats
2751 my @navs = qw(summary shortlog log commit commitdiff tree);
2752 if ($suppress) {
2753 @navs = grep { $_ ne $suppress } @navs;
2756 my %arg = map { $_ => {action=>$_} } @navs;
2757 if (defined $head) {
2758 for (qw(commit commitdiff)) {
2759 $arg{$_}{'hash'} = $head;
2761 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2762 for (qw(shortlog log)) {
2763 $arg{$_}{'hash'} = $head;
2767 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2768 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2770 print "<div class=\"page_nav\">\n" .
2771 (join " | ",
2772 map { $_ eq $current ?
2773 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2774 } @navs);
2775 print "<br/>\n$extra<br/>\n" .
2776 "</div>\n";
2779 sub format_paging_nav {
2780 my ($action, $hash, $head, $page, $has_next_link) = @_;
2781 my $paging_nav;
2784 if ($hash ne $head || $page) {
2785 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2786 } else {
2787 $paging_nav .= "HEAD";
2790 if ($page > 0) {
2791 $paging_nav .= " &sdot; " .
2792 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2793 -accesskey => "p", -title => "Alt-p"}, "prev");
2794 } else {
2795 $paging_nav .= " &sdot; prev";
2798 if ($has_next_link) {
2799 $paging_nav .= " &sdot; " .
2800 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2801 -accesskey => "n", -title => "Alt-n"}, "next");
2802 } else {
2803 $paging_nav .= " &sdot; next";
2806 return $paging_nav;
2809 ## ......................................................................
2810 ## functions printing or outputting HTML: div
2812 sub git_print_header_div {
2813 my ($action, $title, $hash, $hash_base) = @_;
2814 my %args = ();
2816 $args{'action'} = $action;
2817 $args{'hash'} = $hash if $hash;
2818 $args{'hash_base'} = $hash_base if $hash_base;
2820 print "<div class=\"header\">\n" .
2821 $cgi->a({-href => href(%args), -class => "title"},
2822 $title ? $title : $action) .
2823 "\n</div>\n";
2826 #sub git_print_authorship (\%) {
2827 sub git_print_authorship {
2828 my $co = shift;
2830 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2831 print "<div class=\"author_date\">" .
2832 esc_html($co->{'author_name'}) .
2833 " [$ad{'rfc2822'}";
2834 if ($ad{'hour_local'} < 6) {
2835 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2836 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2837 } else {
2838 printf(" (%02d:%02d %s)",
2839 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2841 print "]</div>\n";
2844 sub git_print_page_path {
2845 my $name = shift;
2846 my $type = shift;
2847 my $hb = shift;
2850 print "<div class=\"page_path\">";
2851 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2852 -title => 'tree root'}, to_utf8("[$project]"));
2853 print " / ";
2854 if (defined $name) {
2855 my @dirname = split '/', $name;
2856 my $basename = pop @dirname;
2857 my $fullname = '';
2859 foreach my $dir (@dirname) {
2860 $fullname .= ($fullname ? '/' : '') . $dir;
2861 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2862 hash_base=>$hb),
2863 -title => $fullname}, esc_path($dir));
2864 print " / ";
2866 if (defined $type && $type eq 'blob') {
2867 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2868 hash_base=>$hb),
2869 -title => $name}, esc_path($basename));
2870 } elsif (defined $type && $type eq 'tree') {
2871 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2872 hash_base=>$hb),
2873 -title => $name}, esc_path($basename));
2874 print " / ";
2875 } else {
2876 print esc_path($basename);
2879 print "<br/></div>\n";
2882 # sub git_print_log (\@;%) {
2883 sub git_print_log ($;%) {
2884 my $log = shift;
2885 my %opts = @_;
2887 if ($opts{'-remove_title'}) {
2888 # remove title, i.e. first line of log
2889 shift @$log;
2891 # remove leading empty lines
2892 while (defined $log->[0] && $log->[0] eq "") {
2893 shift @$log;
2896 # print log
2897 my $signoff = 0;
2898 my $empty = 0;
2899 foreach my $line (@$log) {
2900 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2901 $signoff = 1;
2902 $empty = 0;
2903 if (! $opts{'-remove_signoff'}) {
2904 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2905 next;
2906 } else {
2907 # remove signoff lines
2908 next;
2910 } else {
2911 $signoff = 0;
2914 # print only one empty line
2915 # do not print empty line after signoff
2916 if ($line eq "") {
2917 next if ($empty || $signoff);
2918 $empty = 1;
2919 } else {
2920 $empty = 0;
2923 print format_log_line_html($line) . "<br/>\n";
2926 if ($opts{'-final_empty_line'}) {
2927 # end with single empty line
2928 print "<br/>\n" unless $empty;
2932 # return link target (what link points to)
2933 sub git_get_link_target {
2934 my $hash = shift;
2935 my $link_target;
2937 # read link
2938 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2939 or return;
2941 local $/;
2942 $link_target = <$fd>;
2944 close $fd
2945 or return;
2947 return $link_target;
2950 # given link target, and the directory (basedir) the link is in,
2951 # return target of link relative to top directory (top tree);
2952 # return undef if it is not possible (including absolute links).
2953 sub normalize_link_target {
2954 my ($link_target, $basedir, $hash_base) = @_;
2956 # we can normalize symlink target only if $hash_base is provided
2957 return unless $hash_base;
2959 # absolute symlinks (beginning with '/') cannot be normalized
2960 return if (substr($link_target, 0, 1) eq '/');
2962 # normalize link target to path from top (root) tree (dir)
2963 my $path;
2964 if ($basedir) {
2965 $path = $basedir . '/' . $link_target;
2966 } else {
2967 # we are in top (root) tree (dir)
2968 $path = $link_target;
2971 # remove //, /./, and /../
2972 my @path_parts;
2973 foreach my $part (split('/', $path)) {
2974 # discard '.' and ''
2975 next if (!$part || $part eq '.');
2976 # handle '..'
2977 if ($part eq '..') {
2978 if (@path_parts) {
2979 pop @path_parts;
2980 } else {
2981 # link leads outside repository (outside top dir)
2982 return;
2984 } else {
2985 push @path_parts, $part;
2988 $path = join('/', @path_parts);
2990 return $path;
2993 # print tree entry (row of git_tree), but without encompassing <tr> element
2994 sub git_print_tree_entry {
2995 my ($t, $basedir, $hash_base, $have_blame) = @_;
2997 my %base_key = ();
2998 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3000 # The format of a table row is: mode list link. Where mode is
3001 # the mode of the entry, list is the name of the entry, an href,
3002 # and link is the action links of the entry.
3004 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3005 if ($t->{'type'} eq "blob") {
3006 print "<td class=\"list\">" .
3007 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3008 file_name=>"$basedir$t->{'name'}", %base_key),
3009 -class => "list"}, esc_path($t->{'name'}));
3010 if (S_ISLNK(oct $t->{'mode'})) {
3011 my $link_target = git_get_link_target($t->{'hash'});
3012 if ($link_target) {
3013 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3014 if (defined $norm_target) {
3015 print " -> " .
3016 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3017 file_name=>$norm_target),
3018 -title => $norm_target}, esc_path($link_target));
3019 } else {
3020 print " -> " . esc_path($link_target);
3024 print "</td>\n";
3025 print "<td class=\"link\">";
3026 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3027 file_name=>"$basedir$t->{'name'}", %base_key)},
3028 "blob");
3029 if ($have_blame) {
3030 print " | " .
3031 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3032 file_name=>"$basedir$t->{'name'}", %base_key)},
3033 "blame");
3035 if (defined $hash_base) {
3036 print " | " .
3037 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3038 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3039 "history");
3041 print " | " .
3042 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3043 file_name=>"$basedir$t->{'name'}")},
3044 "raw");
3045 print "</td>\n";
3047 } elsif ($t->{'type'} eq "tree") {
3048 print "<td class=\"list\">";
3049 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3050 file_name=>"$basedir$t->{'name'}", %base_key)},
3051 esc_path($t->{'name'}));
3052 print "</td>\n";
3053 print "<td class=\"link\">";
3054 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3055 file_name=>"$basedir$t->{'name'}", %base_key)},
3056 "tree");
3057 if (defined $hash_base) {
3058 print " | " .
3059 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3060 file_name=>"$basedir$t->{'name'}")},
3061 "history");
3063 print "</td>\n";
3064 } else {
3065 # unknown object: we can only present history for it
3066 # (this includes 'commit' object, i.e. submodule support)
3067 print "<td class=\"list\">" .
3068 esc_path($t->{'name'}) .
3069 "</td>\n";
3070 print "<td class=\"link\">";
3071 if (defined $hash_base) {
3072 print $cgi->a({-href => href(action=>"history",
3073 hash_base=>$hash_base,
3074 file_name=>"$basedir$t->{'name'}")},
3075 "history");
3077 print "</td>\n";
3081 ## ......................................................................
3082 ## functions printing large fragments of HTML
3084 # get pre-image filenames for merge (combined) diff
3085 sub fill_from_file_info {
3086 my ($diff, @parents) = @_;
3088 $diff->{'from_file'} = [ ];
3089 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3090 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3091 if ($diff->{'status'}[$i] eq 'R' ||
3092 $diff->{'status'}[$i] eq 'C') {
3093 $diff->{'from_file'}[$i] =
3094 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3098 return $diff;
3101 # is current raw difftree line of file deletion
3102 sub is_deleted {
3103 my $diffinfo = shift;
3105 return $diffinfo->{'to_id'} eq ('0' x 40);
3108 # does patch correspond to [previous] difftree raw line
3109 # $diffinfo - hashref of parsed raw diff format
3110 # $patchinfo - hashref of parsed patch diff format
3111 # (the same keys as in $diffinfo)
3112 sub is_patch_split {
3113 my ($diffinfo, $patchinfo) = @_;
3115 return defined $diffinfo && defined $patchinfo
3116 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3120 sub git_difftree_body {
3121 my ($difftree, $hash, @parents) = @_;
3122 my ($parent) = $parents[0];
3123 my ($have_blame) = gitweb_check_feature('blame');
3124 print "<div class=\"list_head\">\n";
3125 if ($#{$difftree} > 10) {
3126 print(($#{$difftree} + 1) . " files changed:\n");
3128 print "</div>\n";
3130 print "<table class=\"" .
3131 (@parents > 1 ? "combined " : "") .
3132 "diff_tree\">\n";
3134 # header only for combined diff in 'commitdiff' view
3135 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3136 if ($has_header) {
3137 # table header
3138 print "<thead><tr>\n" .
3139 "<th></th><th></th>\n"; # filename, patchN link
3140 for (my $i = 0; $i < @parents; $i++) {
3141 my $par = $parents[$i];
3142 print "<th>" .
3143 $cgi->a({-href => href(action=>"commitdiff",
3144 hash=>$hash, hash_parent=>$par),
3145 -title => 'commitdiff to parent number ' .
3146 ($i+1) . ': ' . substr($par,0,7)},
3147 $i+1) .
3148 "&nbsp;</th>\n";
3150 print "</tr></thead>\n<tbody>\n";
3153 my $alternate = 1;
3154 my $patchno = 0;
3155 foreach my $line (@{$difftree}) {
3156 my $diff = parsed_difftree_line($line);
3158 if ($alternate) {
3159 print "<tr class=\"dark\">\n";
3160 } else {
3161 print "<tr class=\"light\">\n";
3163 $alternate ^= 1;
3165 if (exists $diff->{'nparents'}) { # combined diff
3167 fill_from_file_info($diff, @parents)
3168 unless exists $diff->{'from_file'};
3170 if (!is_deleted($diff)) {
3171 # file exists in the result (child) commit
3172 print "<td>" .
3173 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3174 file_name=>$diff->{'to_file'},
3175 hash_base=>$hash),
3176 -class => "list"}, esc_path($diff->{'to_file'})) .
3177 "</td>\n";
3178 } else {
3179 print "<td>" .
3180 esc_path($diff->{'to_file'}) .
3181 "</td>\n";
3184 if ($action eq 'commitdiff') {
3185 # link to patch
3186 $patchno++;
3187 print "<td class=\"link\">" .
3188 $cgi->a({-href => "#patch$patchno"}, "patch") .
3189 " | " .
3190 "</td>\n";
3193 my $has_history = 0;
3194 my $not_deleted = 0;
3195 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3196 my $hash_parent = $parents[$i];
3197 my $from_hash = $diff->{'from_id'}[$i];
3198 my $from_path = $diff->{'from_file'}[$i];
3199 my $status = $diff->{'status'}[$i];
3201 $has_history ||= ($status ne 'A');
3202 $not_deleted ||= ($status ne 'D');
3204 if ($status eq 'A') {
3205 print "<td class=\"link\" align=\"right\"> | </td>\n";
3206 } elsif ($status eq 'D') {
3207 print "<td class=\"link\">" .
3208 $cgi->a({-href => href(action=>"blob",
3209 hash_base=>$hash,
3210 hash=>$from_hash,
3211 file_name=>$from_path)},
3212 "blob" . ($i+1)) .
3213 " | </td>\n";
3214 } else {
3215 if ($diff->{'to_id'} eq $from_hash) {
3216 print "<td class=\"link nochange\">";
3217 } else {
3218 print "<td class=\"link\">";
3220 print $cgi->a({-href => href(action=>"blobdiff",
3221 hash=>$diff->{'to_id'},
3222 hash_parent=>$from_hash,
3223 hash_base=>$hash,
3224 hash_parent_base=>$hash_parent,
3225 file_name=>$diff->{'to_file'},
3226 file_parent=>$from_path)},
3227 "diff" . ($i+1)) .
3228 " | </td>\n";
3232 print "<td class=\"link\">";
3233 if ($not_deleted) {
3234 print $cgi->a({-href => href(action=>"blob",
3235 hash=>$diff->{'to_id'},
3236 file_name=>$diff->{'to_file'},
3237 hash_base=>$hash)},
3238 "blob");
3239 print " | " if ($has_history);
3241 if ($has_history) {
3242 print $cgi->a({-href => href(action=>"history",
3243 file_name=>$diff->{'to_file'},
3244 hash_base=>$hash)},
3245 "history");
3247 print "</td>\n";
3249 print "</tr>\n";
3250 next; # instead of 'else' clause, to avoid extra indent
3252 # else ordinary diff
3254 my ($to_mode_oct, $to_mode_str, $to_file_type);
3255 my ($from_mode_oct, $from_mode_str, $from_file_type);
3256 if ($diff->{'to_mode'} ne ('0' x 6)) {
3257 $to_mode_oct = oct $diff->{'to_mode'};
3258 if (S_ISREG($to_mode_oct)) { # only for regular file
3259 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3261 $to_file_type = file_type($diff->{'to_mode'});
3263 if ($diff->{'from_mode'} ne ('0' x 6)) {
3264 $from_mode_oct = oct $diff->{'from_mode'};
3265 if (S_ISREG($to_mode_oct)) { # only for regular file
3266 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3268 $from_file_type = file_type($diff->{'from_mode'});
3271 if ($diff->{'status'} eq "A") { # created
3272 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3273 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3274 $mode_chng .= "]</span>";
3275 print "<td>";
3276 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3277 hash_base=>$hash, file_name=>$diff->{'file'}),
3278 -class => "list"}, esc_path($diff->{'file'}));
3279 print "</td>\n";
3280 print "<td>$mode_chng</td>\n";
3281 print "<td class=\"link\">";
3282 if ($action eq 'commitdiff') {
3283 # link to patch
3284 $patchno++;
3285 print $cgi->a({-href => "#patch$patchno"}, "patch");
3286 print " | ";
3288 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3289 hash_base=>$hash, file_name=>$diff->{'file'})},
3290 "blob");
3291 print "</td>\n";
3293 } elsif ($diff->{'status'} eq "D") { # deleted
3294 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3295 print "<td>";
3296 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3297 hash_base=>$parent, file_name=>$diff->{'file'}),
3298 -class => "list"}, esc_path($diff->{'file'}));
3299 print "</td>\n";
3300 print "<td>$mode_chng</td>\n";
3301 print "<td class=\"link\">";
3302 if ($action eq 'commitdiff') {
3303 # link to patch
3304 $patchno++;
3305 print $cgi->a({-href => "#patch$patchno"}, "patch");
3306 print " | ";
3308 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3309 hash_base=>$parent, file_name=>$diff->{'file'})},
3310 "blob") . " | ";
3311 if ($have_blame) {
3312 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3313 file_name=>$diff->{'file'})},
3314 "blame") . " | ";
3316 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3317 file_name=>$diff->{'file'})},
3318 "history");
3319 print "</td>\n";
3321 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3322 my $mode_chnge = "";
3323 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3324 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3325 if ($from_file_type ne $to_file_type) {
3326 $mode_chnge .= " from $from_file_type to $to_file_type";
3328 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3329 if ($from_mode_str && $to_mode_str) {
3330 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3331 } elsif ($to_mode_str) {
3332 $mode_chnge .= " mode: $to_mode_str";
3335 $mode_chnge .= "]</span>\n";
3337 print "<td>";
3338 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3339 hash_base=>$hash, file_name=>$diff->{'file'}),
3340 -class => "list"}, esc_path($diff->{'file'}));
3341 print "</td>\n";
3342 print "<td>$mode_chnge</td>\n";
3343 print "<td class=\"link\">";
3344 if ($action eq 'commitdiff') {
3345 # link to patch
3346 $patchno++;
3347 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3348 " | ";
3349 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3350 # "commit" view and modified file (not onlu mode changed)
3351 print $cgi->a({-href => href(action=>"blobdiff",
3352 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3353 hash_base=>$hash, hash_parent_base=>$parent,
3354 file_name=>$diff->{'file'})},
3355 "diff") .
3356 " | ";
3358 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3359 hash_base=>$hash, file_name=>$diff->{'file'})},
3360 "blob") . " | ";
3361 if ($have_blame) {
3362 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3363 file_name=>$diff->{'file'})},
3364 "blame") . " | ";
3366 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3367 file_name=>$diff->{'file'})},
3368 "history");
3369 print "</td>\n";
3371 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3372 my %status_name = ('R' => 'moved', 'C' => 'copied');
3373 my $nstatus = $status_name{$diff->{'status'}};
3374 my $mode_chng = "";
3375 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3376 # mode also for directories, so we cannot use $to_mode_str
3377 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3379 print "<td>" .
3380 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3381 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3382 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3383 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3384 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3385 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3386 -class => "list"}, esc_path($diff->{'from_file'})) .
3387 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3388 "<td class=\"link\">";
3389 if ($action eq 'commitdiff') {
3390 # link to patch
3391 $patchno++;
3392 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3393 " | ";
3394 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3395 # "commit" view and modified file (not only pure rename or copy)
3396 print $cgi->a({-href => href(action=>"blobdiff",
3397 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3398 hash_base=>$hash, hash_parent_base=>$parent,
3399 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3400 "diff") .
3401 " | ";
3403 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3404 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3405 "blob") . " | ";
3406 if ($have_blame) {
3407 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3408 file_name=>$diff->{'to_file'})},
3409 "blame") . " | ";
3411 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3412 file_name=>$diff->{'to_file'})},
3413 "history");
3414 print "</td>\n";
3416 } # we should not encounter Unmerged (U) or Unknown (X) status
3417 print "</tr>\n";
3419 print "</tbody>" if $has_header;
3420 print "</table>\n";
3423 sub git_patchset_body {
3424 my ($fd, $difftree, $hash, @hash_parents) = @_;
3425 my ($hash_parent) = $hash_parents[0];
3427 my $is_combined = (@hash_parents > 1);
3428 my $patch_idx = 0;
3429 my $patch_number = 0;
3430 my $patch_line;
3431 my $diffinfo;
3432 my $to_name;
3433 my (%from, %to);
3435 print "<div class=\"patchset\">\n";
3437 # skip to first patch
3438 while ($patch_line = <$fd>) {
3439 chomp $patch_line;
3441 last if ($patch_line =~ m/^diff /);
3444 PATCH:
3445 while ($patch_line) {
3447 # parse "git diff" header line
3448 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3449 # $1 is from_name, which we do not use
3450 $to_name = unquote($2);
3451 $to_name =~ s!^b/!!;
3452 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3453 # $1 is 'cc' or 'combined', which we do not use
3454 $to_name = unquote($2);
3455 } else {
3456 $to_name = undef;
3459 # check if current patch belong to current raw line
3460 # and parse raw git-diff line if needed
3461 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3462 # this is continuation of a split patch
3463 print "<div class=\"patch cont\">\n";
3464 } else {
3465 # advance raw git-diff output if needed
3466 $patch_idx++ if defined $diffinfo;
3468 # read and prepare patch information
3469 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3471 # compact combined diff output can have some patches skipped
3472 # find which patch (using pathname of result) we are at now;
3473 if ($is_combined) {
3474 while ($to_name ne $diffinfo->{'to_file'}) {
3475 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3476 format_diff_cc_simplified($diffinfo, @hash_parents) .
3477 "</div>\n"; # class="patch"
3479 $patch_idx++;
3480 $patch_number++;
3482 last if $patch_idx > $#$difftree;
3483 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3487 # modifies %from, %to hashes
3488 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3490 # this is first patch for raw difftree line with $patch_idx index
3491 # we index @$difftree array from 0, but number patches from 1
3492 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3495 # git diff header
3496 #assert($patch_line =~ m/^diff /) if DEBUG;
3497 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3498 $patch_number++;
3499 # print "git diff" header
3500 print format_git_diff_header_line($patch_line, $diffinfo,
3501 \%from, \%to);
3503 # print extended diff header
3504 print "<div class=\"diff extended_header\">\n";
3505 EXTENDED_HEADER:
3506 while ($patch_line = <$fd>) {
3507 chomp $patch_line;
3509 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3511 print format_extended_diff_header_line($patch_line, $diffinfo,
3512 \%from, \%to);
3514 print "</div>\n"; # class="diff extended_header"
3516 # from-file/to-file diff header
3517 if (! $patch_line) {
3518 print "</div>\n"; # class="patch"
3519 last PATCH;
3521 next PATCH if ($patch_line =~ m/^diff /);
3522 #assert($patch_line =~ m/^---/) if DEBUG;
3524 my $last_patch_line = $patch_line;
3525 $patch_line = <$fd>;
3526 chomp $patch_line;
3527 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3529 print format_diff_from_to_header($last_patch_line, $patch_line,
3530 $diffinfo, \%from, \%to,
3531 @hash_parents);
3533 # the patch itself
3534 LINE:
3535 while ($patch_line = <$fd>) {
3536 chomp $patch_line;
3538 next PATCH if ($patch_line =~ m/^diff /);
3540 print format_diff_line($patch_line, \%from, \%to);
3543 } continue {
3544 print "</div>\n"; # class="patch"
3547 # for compact combined (--cc) format, with chunk and patch simpliciaction
3548 # patchset might be empty, but there might be unprocessed raw lines
3549 for (++$patch_idx if $patch_number > 0;
3550 $patch_idx < @$difftree;
3551 ++$patch_idx) {
3552 # read and prepare patch information
3553 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3555 # generate anchor for "patch" links in difftree / whatchanged part
3556 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3557 format_diff_cc_simplified($diffinfo, @hash_parents) .
3558 "</div>\n"; # class="patch"
3560 $patch_number++;
3563 if ($patch_number == 0) {
3564 if (@hash_parents > 1) {
3565 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3566 } else {
3567 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3571 print "</div>\n"; # class="patchset"
3574 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3576 # fills project list info (age, description, owner, forks) for each
3577 # project in the list, removing invalid projects from returned list
3578 # NOTE: modifies $projlist, but does not remove entries from it
3579 sub fill_project_list_info {
3580 my ($projlist, $check_forks) = @_;
3581 my @projects;
3583 PROJECT:
3584 foreach my $pr (@$projlist) {
3585 my (@activity) = git_get_last_activity($pr->{'path'});
3586 unless (@activity) {
3587 next PROJECT;
3589 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3590 if (!defined $pr->{'descr'}) {
3591 my $descr = git_get_project_description($pr->{'path'}) || "";
3592 $descr = to_utf8($descr);
3593 $pr->{'descr_long'} = $descr;
3594 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3596 if (!defined $pr->{'owner'}) {
3597 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3599 if ($check_forks) {
3600 my $pname = $pr->{'path'};
3601 if (($pname =~ s/\.git$//) &&
3602 ($pname !~ /\/$/) &&
3603 (-d "$projectroot/$pname")) {
3604 $pr->{'forks'} = "-d $projectroot/$pname";
3605 } else {
3606 $pr->{'forks'} = 0;
3609 push @projects, $pr;
3612 return @projects;
3615 # print 'sort by' <th> element, generating 'sort by $name' replay link
3616 # if that order is not selected
3617 sub print_sort_th {
3618 my ($name, $order, $header) = @_;
3619 $header ||= ucfirst($name);
3621 if ($order eq $name) {
3622 print "<th>$header</th>\n";
3623 } else {
3624 print "<th>" .
3625 $cgi->a({-href => href(-replay=>1, order=>$name),
3626 -class => "header"}, $header) .
3627 "</th>\n";
3631 sub git_project_list_body {
3632 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3634 my ($check_forks) = gitweb_check_feature('forks');
3635 my @projects = fill_project_list_info($projlist, $check_forks);
3637 $order ||= $default_projects_order;
3638 $from = 0 unless defined $from;
3639 $to = $#projects if (!defined $to || $#projects < $to);
3641 my %order_info = (
3642 project => { key => 'path', type => 'str' },
3643 descr => { key => 'descr_long', type => 'str' },
3644 owner => { key => 'owner', type => 'str' },
3645 age => { key => 'age', type => 'num' }
3647 my $oi = $order_info{$order};
3648 if ($oi->{'type'} eq 'str') {
3649 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
3650 } else {
3651 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
3654 print "<table class=\"project_list\">\n";
3655 unless ($no_header) {
3656 print "<tr>\n";
3657 if ($check_forks) {
3658 print "<th></th>\n";
3660 print_sort_th('project', $order, 'Project');
3661 print_sort_th('descr', $order, 'Description');
3662 print_sort_th('owner', $order, 'Owner');
3663 print_sort_th('age', $order, 'Last Change');
3664 print "<th></th>\n" . # for links
3665 "</tr>\n";
3667 my $alternate = 1;
3668 for (my $i = $from; $i <= $to; $i++) {
3669 my $pr = $projects[$i];
3670 if ($alternate) {
3671 print "<tr class=\"dark\">\n";
3672 } else {
3673 print "<tr class=\"light\">\n";
3675 $alternate ^= 1;
3676 if ($check_forks) {
3677 print "<td>";
3678 if ($pr->{'forks'}) {
3679 print "<!-- $pr->{'forks'} -->\n";
3680 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3682 print "</td>\n";
3684 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3685 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3686 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3687 -class => "list", -title => $pr->{'descr_long'}},
3688 esc_html($pr->{'descr'})) . "</td>\n" .
3689 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3690 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3691 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3692 "<td class=\"link\">" .
3693 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3694 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3695 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3696 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3697 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3698 "</td>\n" .
3699 "</tr>\n";
3701 if (defined $extra) {
3702 print "<tr>\n";
3703 if ($check_forks) {
3704 print "<td></td>\n";
3706 print "<td colspan=\"5\">$extra</td>\n" .
3707 "</tr>\n";
3709 print "</table>\n";
3712 sub git_shortlog_body {
3713 # uses global variable $project
3714 my ($commitlist, $from, $to, $refs, $extra) = @_;
3716 $from = 0 unless defined $from;
3717 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3719 print "<table class=\"shortlog\">\n";
3720 my $alternate = 1;
3721 for (my $i = $from; $i <= $to; $i++) {
3722 my %co = %{$commitlist->[$i]};
3723 my $commit = $co{'id'};
3724 my $ref = format_ref_marker($refs, $commit);
3725 if ($alternate) {
3726 print "<tr class=\"dark\">\n";
3727 } else {
3728 print "<tr class=\"light\">\n";
3730 $alternate ^= 1;
3731 my $author = chop_and_escape_str($co{'author_name'}, 10);
3732 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3733 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3734 "<td><i>" . $author . "</i></td>\n" .
3735 "<td>";
3736 print format_subject_html($co{'title'}, $co{'title_short'},
3737 href(action=>"commit", hash=>$commit), $ref);
3738 print "</td>\n" .
3739 "<td class=\"link\">" .
3740 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3741 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3742 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3743 my $snapshot_links = format_snapshot_links($commit);
3744 if (defined $snapshot_links) {
3745 print " | " . $snapshot_links;
3747 print "</td>\n" .
3748 "</tr>\n";
3750 if (defined $extra) {
3751 print "<tr>\n" .
3752 "<td colspan=\"4\">$extra</td>\n" .
3753 "</tr>\n";
3755 print "</table>\n";
3758 sub git_history_body {
3759 # Warning: assumes constant type (blob or tree) during history
3760 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3762 $from = 0 unless defined $from;
3763 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3765 print "<table class=\"history\">\n";
3766 my $alternate = 1;
3767 for (my $i = $from; $i <= $to; $i++) {
3768 my %co = %{$commitlist->[$i]};
3769 if (!%co) {
3770 next;
3772 my $commit = $co{'id'};
3774 my $ref = format_ref_marker($refs, $commit);
3776 if ($alternate) {
3777 print "<tr class=\"dark\">\n";
3778 } else {
3779 print "<tr class=\"light\">\n";
3781 $alternate ^= 1;
3782 # shortlog uses chop_str($co{'author_name'}, 10)
3783 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3784 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3785 "<td><i>" . $author . "</i></td>\n" .
3786 "<td>";
3787 # originally git_history used chop_str($co{'title'}, 50)
3788 print format_subject_html($co{'title'}, $co{'title_short'},
3789 href(action=>"commit", hash=>$commit), $ref);
3790 print "</td>\n" .
3791 "<td class=\"link\">" .
3792 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3793 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3795 if ($ftype eq 'blob') {
3796 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3797 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3798 if (defined $blob_current && defined $blob_parent &&
3799 $blob_current ne $blob_parent) {
3800 print " | " .
3801 $cgi->a({-href => href(action=>"blobdiff",
3802 hash=>$blob_current, hash_parent=>$blob_parent,
3803 hash_base=>$hash_base, hash_parent_base=>$commit,
3804 file_name=>$file_name)},
3805 "diff to current");
3808 print "</td>\n" .
3809 "</tr>\n";
3811 if (defined $extra) {
3812 print "<tr>\n" .
3813 "<td colspan=\"4\">$extra</td>\n" .
3814 "</tr>\n";
3816 print "</table>\n";
3819 sub git_tags_body {
3820 # uses global variable $project
3821 my ($taglist, $from, $to, $extra) = @_;
3822 $from = 0 unless defined $from;
3823 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3825 print "<table class=\"tags\">\n";
3826 my $alternate = 1;
3827 for (my $i = $from; $i <= $to; $i++) {
3828 my $entry = $taglist->[$i];
3829 my %tag = %$entry;
3830 my $comment = $tag{'subject'};
3831 my $comment_short;
3832 if (defined $comment) {
3833 $comment_short = chop_str($comment, 30, 5);
3835 if ($alternate) {
3836 print "<tr class=\"dark\">\n";
3837 } else {
3838 print "<tr class=\"light\">\n";
3840 $alternate ^= 1;
3841 if (defined $tag{'age'}) {
3842 print "<td><i>$tag{'age'}</i></td>\n";
3843 } else {
3844 print "<td></td>\n";
3846 print "<td>" .
3847 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3848 -class => "list name"}, esc_html($tag{'name'})) .
3849 "</td>\n" .
3850 "<td>";
3851 if (defined $comment) {
3852 print format_subject_html($comment, $comment_short,
3853 href(action=>"tag", hash=>$tag{'id'}));
3855 print "</td>\n" .
3856 "<td class=\"selflink\">";
3857 if ($tag{'type'} eq "tag") {
3858 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3859 } else {
3860 print "&nbsp;";
3862 print "</td>\n" .
3863 "<td class=\"link\">" . " | " .
3864 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3865 if ($tag{'reftype'} eq "commit") {
3866 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
3867 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
3868 } elsif ($tag{'reftype'} eq "blob") {
3869 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3871 print "</td>\n" .
3872 "</tr>";
3874 if (defined $extra) {
3875 print "<tr>\n" .
3876 "<td colspan=\"5\">$extra</td>\n" .
3877 "</tr>\n";
3879 print "</table>\n";
3882 sub git_heads_body {
3883 # uses global variable $project
3884 my ($headlist, $head, $from, $to, $extra) = @_;
3885 $from = 0 unless defined $from;
3886 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3888 print "<table class=\"heads\">\n";
3889 my $alternate = 1;
3890 for (my $i = $from; $i <= $to; $i++) {
3891 my $entry = $headlist->[$i];
3892 my %ref = %$entry;
3893 my $curr = $ref{'id'} eq $head;
3894 if ($alternate) {
3895 print "<tr class=\"dark\">\n";
3896 } else {
3897 print "<tr class=\"light\">\n";
3899 $alternate ^= 1;
3900 print "<td><i>$ref{'age'}</i></td>\n" .
3901 ($curr ? "<td class=\"current_head\">" : "<td>") .
3902 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
3903 -class => "list name"},esc_html($ref{'name'})) .
3904 "</td>\n" .
3905 "<td class=\"link\">" .
3906 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
3907 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
3908 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
3909 "</td>\n" .
3910 "</tr>";
3912 if (defined $extra) {
3913 print "<tr>\n" .
3914 "<td colspan=\"3\">$extra</td>\n" .
3915 "</tr>\n";
3917 print "</table>\n";
3920 sub git_search_grep_body {
3921 my ($commitlist, $from, $to, $extra) = @_;
3922 $from = 0 unless defined $from;
3923 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3925 print "<table class=\"commit_search\">\n";
3926 my $alternate = 1;
3927 for (my $i = $from; $i <= $to; $i++) {
3928 my %co = %{$commitlist->[$i]};
3929 if (!%co) {
3930 next;
3932 my $commit = $co{'id'};
3933 if ($alternate) {
3934 print "<tr class=\"dark\">\n";
3935 } else {
3936 print "<tr class=\"light\">\n";
3938 $alternate ^= 1;
3939 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3940 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3941 "<td><i>" . $author . "</i></td>\n" .
3942 "<td>" .
3943 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3944 -class => "list subject"},
3945 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3946 my $comment = $co{'comment'};
3947 foreach my $line (@$comment) {
3948 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
3949 my ($lead, $match, $trail) = ($1, $2, $3);
3950 $match = chop_str($match, 70, 5, 'center');
3951 my $contextlen = int((80 - length($match))/2);
3952 $contextlen = 30 if ($contextlen > 30);
3953 $lead = chop_str($lead, $contextlen, 10, 'left');
3954 $trail = chop_str($trail, $contextlen, 10, 'right');
3956 $lead = esc_html($lead);
3957 $match = esc_html($match);
3958 $trail = esc_html($trail);
3960 print "$lead<span class=\"match\">$match</span>$trail<br />";
3963 print "</td>\n" .
3964 "<td class=\"link\">" .
3965 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3966 " | " .
3967 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
3968 " | " .
3969 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3970 print "</td>\n" .
3971 "</tr>\n";
3973 if (defined $extra) {
3974 print "<tr>\n" .
3975 "<td colspan=\"3\">$extra</td>\n" .
3976 "</tr>\n";
3978 print "</table>\n";
3981 ## ======================================================================
3982 ## ======================================================================
3983 ## actions
3985 sub git_project_list {
3986 my $order = $cgi->param('o');
3987 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3988 die_error(400, "Unknown order parameter");
3991 my @list = git_get_projects_list();
3992 if (!@list) {
3993 die_error(404, "No projects found");
3996 git_header_html();
3997 if (-f $home_text) {
3998 print "<div class=\"index_include\">\n";
3999 open (my $fd, $home_text);
4000 print <$fd>;
4001 close $fd;
4002 print "</div>\n";
4004 git_project_list_body(\@list, $order);
4005 git_footer_html();
4008 sub git_forks {
4009 my $order = $cgi->param('o');
4010 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4011 die_error(400, "Unknown order parameter");
4014 my @list = git_get_projects_list($project);
4015 if (!@list) {
4016 die_error(404, "No forks found");
4019 git_header_html();
4020 git_print_page_nav('','');
4021 git_print_header_div('summary', "$project forks");
4022 git_project_list_body(\@list, $order);
4023 git_footer_html();
4026 sub git_project_index {
4027 my @projects = git_get_projects_list($project);
4029 print $cgi->header(
4030 -type => 'text/plain',
4031 -charset => 'utf-8',
4032 -content_disposition => 'inline; filename="index.aux"');
4034 foreach my $pr (@projects) {
4035 if (!exists $pr->{'owner'}) {
4036 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4039 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4040 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4041 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4042 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4043 $path =~ s/ /\+/g;
4044 $owner =~ s/ /\+/g;
4046 print "$path $owner\n";
4050 sub git_summary {
4051 my $descr = git_get_project_description($project) || "none";
4052 my %co = parse_commit("HEAD");
4053 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4054 my $head = $co{'id'};
4056 my $owner = git_get_project_owner($project);
4058 my $refs = git_get_references();
4059 # These get_*_list functions return one more to allow us to see if
4060 # there are more ...
4061 my @taglist = git_get_tags_list(16);
4062 my @headlist = git_get_heads_list(16);
4063 my @forklist;
4064 my ($check_forks) = gitweb_check_feature('forks');
4066 if ($check_forks) {
4067 @forklist = git_get_projects_list($project);
4070 git_header_html();
4071 git_print_page_nav('summary','', $head);
4073 print "<div class=\"title\">&nbsp;</div>\n";
4074 print "<table class=\"projects_list\">\n" .
4075 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4076 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4077 if (defined $cd{'rfc2822'}) {
4078 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4081 # use per project git URL list in $projectroot/$project/cloneurl
4082 # or make project git URL from git base URL and project name
4083 my $url_tag = "URL";
4084 my @url_list = git_get_project_url_list($project);
4085 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4086 foreach my $git_url (@url_list) {
4087 next unless $git_url;
4088 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4089 $url_tag = "";
4091 print "</table>\n";
4093 if (-s "$projectroot/$project/README.html") {
4094 if (open my $fd, "$projectroot/$project/README.html") {
4095 print "<div class=\"title\">readme</div>\n" .
4096 "<div class=\"readme\">\n";
4097 print $_ while (<$fd>);
4098 print "\n</div>\n"; # class="readme"
4099 close $fd;
4103 # we need to request one more than 16 (0..15) to check if
4104 # those 16 are all
4105 my @commitlist = $head ? parse_commits($head, 17) : ();
4106 if (@commitlist) {
4107 git_print_header_div('shortlog');
4108 git_shortlog_body(\@commitlist, 0, 15, $refs,
4109 $#commitlist <= 15 ? undef :
4110 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4113 if (@taglist) {
4114 git_print_header_div('tags');
4115 git_tags_body(\@taglist, 0, 15,
4116 $#taglist <= 15 ? undef :
4117 $cgi->a({-href => href(action=>"tags")}, "..."));
4120 if (@headlist) {
4121 git_print_header_div('heads');
4122 git_heads_body(\@headlist, $head, 0, 15,
4123 $#headlist <= 15 ? undef :
4124 $cgi->a({-href => href(action=>"heads")}, "..."));
4127 if (@forklist) {
4128 git_print_header_div('forks');
4129 git_project_list_body(\@forklist, 'age', 0, 15,
4130 $#forklist <= 15 ? undef :
4131 $cgi->a({-href => href(action=>"forks")}, "..."),
4132 'no_header');
4135 git_footer_html();
4138 sub git_tag {
4139 my $head = git_get_head_hash($project);
4140 git_header_html();
4141 git_print_page_nav('','', $head,undef,$head);
4142 my %tag = parse_tag($hash);
4144 if (! %tag) {
4145 die_error(404, "Unknown tag object");
4148 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4149 print "<div class=\"title_text\">\n" .
4150 "<table class=\"object_header\">\n" .
4151 "<tr>\n" .
4152 "<td>object</td>\n" .
4153 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4154 $tag{'object'}) . "</td>\n" .
4155 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4156 $tag{'type'}) . "</td>\n" .
4157 "</tr>\n";
4158 if (defined($tag{'author'})) {
4159 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4160 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4161 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4162 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4163 "</td></tr>\n";
4165 print "</table>\n\n" .
4166 "</div>\n";
4167 print "<div class=\"page_body\">";
4168 my $comment = $tag{'comment'};
4169 foreach my $line (@$comment) {
4170 chomp $line;
4171 print esc_html($line, -nbsp=>1) . "<br/>\n";
4173 print "</div>\n";
4174 git_footer_html();
4177 sub git_blame {
4178 my $fd;
4179 my $ftype;
4181 gitweb_check_feature('blame')
4182 or die_error(403, "Blame view not allowed");
4184 die_error(400, "No file name given") unless $file_name;
4185 $hash_base ||= git_get_head_hash($project);
4186 die_error(404, "Couldn't find base commit") unless ($hash_base);
4187 my %co = parse_commit($hash_base)
4188 or die_error(404, "Commit not found");
4189 if (!defined $hash) {
4190 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4191 or die_error(404, "Error looking up file");
4193 $ftype = git_get_type($hash);
4194 if ($ftype !~ "blob") {
4195 die_error(400, "Object is not a blob");
4197 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4198 $file_name, $hash_base)
4199 or die_error(500, "Open git-blame failed");
4200 git_header_html();
4201 my $formats_nav =
4202 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4203 "blob") .
4204 " | " .
4205 $cgi->a({-href => href(action=>"history", -replay=>1)},
4206 "history") .
4207 " | " .
4208 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4209 "HEAD");
4210 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4211 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4212 git_print_page_path($file_name, $ftype, $hash_base);
4213 my @rev_color = (qw(light2 dark2));
4214 my $num_colors = scalar(@rev_color);
4215 my $current_color = 0;
4216 my $last_rev;
4217 print <<HTML;
4218 <div class="page_body">
4219 <table class="blame">
4220 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4221 HTML
4222 my %metainfo = ();
4223 while (1) {
4224 $_ = <$fd>;
4225 last unless defined $_;
4226 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4227 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4228 if (!exists $metainfo{$full_rev}) {
4229 $metainfo{$full_rev} = {};
4231 my $meta = $metainfo{$full_rev};
4232 while (<$fd>) {
4233 last if (s/^\t//);
4234 if (/^(\S+) (.*)$/) {
4235 $meta->{$1} = $2;
4238 my $data = $_;
4239 chomp $data;
4240 my $rev = substr($full_rev, 0, 8);
4241 my $author = $meta->{'author'};
4242 my %date = parse_date($meta->{'author-time'},
4243 $meta->{'author-tz'});
4244 my $date = $date{'iso-tz'};
4245 if ($group_size) {
4246 $current_color = ++$current_color % $num_colors;
4248 print "<tr class=\"$rev_color[$current_color]\">\n";
4249 if ($group_size) {
4250 print "<td class=\"sha1\"";
4251 print " title=\"". esc_html($author) . ", $date\"";
4252 print " rowspan=\"$group_size\"" if ($group_size > 1);
4253 print ">";
4254 print $cgi->a({-href => href(action=>"commit",
4255 hash=>$full_rev,
4256 file_name=>$file_name)},
4257 esc_html($rev));
4258 print "</td>\n";
4260 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4261 or die_error(500, "Open git-rev-parse failed");
4262 my $parent_commit = <$dd>;
4263 close $dd;
4264 chomp($parent_commit);
4265 my $blamed = href(action => 'blame',
4266 file_name => $meta->{'filename'},
4267 hash_base => $parent_commit);
4268 print "<td class=\"linenr\">";
4269 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4270 -id => "l$lineno",
4271 -class => "linenr" },
4272 esc_html($lineno));
4273 print "</td>";
4274 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4275 print "</tr>\n";
4277 print "</table>\n";
4278 print "</div>";
4279 close $fd
4280 or print "Reading blob failed\n";
4281 git_footer_html();
4284 sub git_tags {
4285 my $head = git_get_head_hash($project);
4286 git_header_html();
4287 git_print_page_nav('','', $head,undef,$head);
4288 git_print_header_div('summary', $project);
4290 my @tagslist = git_get_tags_list();
4291 if (@tagslist) {
4292 git_tags_body(\@tagslist);
4294 git_footer_html();
4297 sub git_heads {
4298 my $head = git_get_head_hash($project);
4299 git_header_html();
4300 git_print_page_nav('','', $head,undef,$head);
4301 git_print_header_div('summary', $project);
4303 my @headslist = git_get_heads_list();
4304 if (@headslist) {
4305 git_heads_body(\@headslist, $head);
4307 git_footer_html();
4310 sub git_blob_plain {
4311 my $type = shift;
4312 my $expires;
4314 if (!defined $hash) {
4315 if (defined $file_name) {
4316 my $base = $hash_base || git_get_head_hash($project);
4317 $hash = git_get_hash_by_path($base, $file_name, "blob")
4318 or die_error(404, "Cannot find file");
4319 } else {
4320 die_error(400, "No file name defined");
4322 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4323 # blobs defined by non-textual hash id's can be cached
4324 $expires = "+1d";
4327 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4328 or die_error(500, "Open git-cat-file blob '$hash' failed");
4330 # content-type (can include charset)
4331 $type = blob_contenttype($fd, $file_name, $type);
4333 # "save as" filename, even when no $file_name is given
4334 my $save_as = "$hash";
4335 if (defined $file_name) {
4336 $save_as = $file_name;
4337 } elsif ($type =~ m/^text\//) {
4338 $save_as .= '.txt';
4341 print $cgi->header(
4342 -type => $type,
4343 -expires => $expires,
4344 -content_disposition => 'inline; filename="' . $save_as . '"');
4345 undef $/;
4346 binmode STDOUT, ':raw';
4347 print <$fd>;
4348 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4349 $/ = "\n";
4350 close $fd;
4353 sub git_blob {
4354 my $expires;
4356 if (!defined $hash) {
4357 if (defined $file_name) {
4358 my $base = $hash_base || git_get_head_hash($project);
4359 $hash = git_get_hash_by_path($base, $file_name, "blob")
4360 or die_error(404, "Cannot find file");
4361 } else {
4362 die_error(400, "No file name defined");
4364 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4365 # blobs defined by non-textual hash id's can be cached
4366 $expires = "+1d";
4369 my ($have_blame) = gitweb_check_feature('blame');
4370 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4371 or die_error(500, "Couldn't cat $file_name, $hash");
4372 my $mimetype = blob_mimetype($fd, $file_name);
4373 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4374 close $fd;
4375 return git_blob_plain($mimetype);
4377 # we can have blame only for text/* mimetype
4378 $have_blame &&= ($mimetype =~ m!^text/!);
4380 git_header_html(undef, $expires);
4381 my $formats_nav = '';
4382 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4383 if (defined $file_name) {
4384 if ($have_blame) {
4385 $formats_nav .=
4386 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4387 "blame") .
4388 " | ";
4390 $formats_nav .=
4391 $cgi->a({-href => href(action=>"history", -replay=>1)},
4392 "history") .
4393 " | " .
4394 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4395 "raw") .
4396 " | " .
4397 $cgi->a({-href => href(action=>"blob",
4398 hash_base=>"HEAD", file_name=>$file_name)},
4399 "HEAD");
4400 } else {
4401 $formats_nav .=
4402 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4403 "raw");
4405 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4406 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4407 } else {
4408 print "<div class=\"page_nav\">\n" .
4409 "<br/><br/></div>\n" .
4410 "<div class=\"title\">$hash</div>\n";
4412 git_print_page_path($file_name, "blob", $hash_base);
4413 print "<div class=\"page_body\">\n";
4414 if ($mimetype =~ m!^image/!) {
4415 print qq!<img type="$mimetype"!;
4416 if ($file_name) {
4417 print qq! alt="$file_name" title="$file_name"!;
4419 print qq! src="! .
4420 href(action=>"blob_plain", hash=>$hash,
4421 hash_base=>$hash_base, file_name=>$file_name) .
4422 qq!" />\n!;
4423 } else {
4424 my $nr;
4425 while (my $line = <$fd>) {
4426 chomp $line;
4427 $nr++;
4428 $line = untabify($line);
4429 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4430 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4433 close $fd
4434 or print "Reading blob failed.\n";
4435 print "</div>";
4436 git_footer_html();
4439 sub git_tree {
4440 if (!defined $hash_base) {
4441 $hash_base = "HEAD";
4443 if (!defined $hash) {
4444 if (defined $file_name) {
4445 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4446 } else {
4447 $hash = $hash_base;
4450 die_error(404, "No such tree") unless defined($hash);
4451 $/ = "\0";
4452 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4453 or die_error(500, "Open git-ls-tree failed");
4454 my @entries = map { chomp; $_ } <$fd>;
4455 close $fd or die_error(404, "Reading tree failed");
4456 $/ = "\n";
4458 my $refs = git_get_references();
4459 my $ref = format_ref_marker($refs, $hash_base);
4460 git_header_html();
4461 my $basedir = '';
4462 my ($have_blame) = gitweb_check_feature('blame');
4463 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4464 my @views_nav = ();
4465 if (defined $file_name) {
4466 push @views_nav,
4467 $cgi->a({-href => href(action=>"history", -replay=>1)},
4468 "history"),
4469 $cgi->a({-href => href(action=>"tree",
4470 hash_base=>"HEAD", file_name=>$file_name)},
4471 "HEAD"),
4473 my $snapshot_links = format_snapshot_links($hash);
4474 if (defined $snapshot_links) {
4475 # FIXME: Should be available when we have no hash base as well.
4476 push @views_nav, $snapshot_links;
4478 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4479 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4480 } else {
4481 undef $hash_base;
4482 print "<div class=\"page_nav\">\n";
4483 print "<br/><br/></div>\n";
4484 print "<div class=\"title\">$hash</div>\n";
4486 if (defined $file_name) {
4487 $basedir = $file_name;
4488 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4489 $basedir .= '/';
4491 git_print_page_path($file_name, 'tree', $hash_base);
4493 print "<div class=\"page_body\">\n";
4494 print "<table class=\"tree\">\n";
4495 my $alternate = 1;
4496 # '..' (top directory) link if possible
4497 if (defined $hash_base &&
4498 defined $file_name && $file_name =~ m![^/]+$!) {
4499 if ($alternate) {
4500 print "<tr class=\"dark\">\n";
4501 } else {
4502 print "<tr class=\"light\">\n";
4504 $alternate ^= 1;
4506 my $up = $file_name;
4507 $up =~ s!/?[^/]+$!!;
4508 undef $up unless $up;
4509 # based on git_print_tree_entry
4510 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4511 print '<td class="list">';
4512 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4513 file_name=>$up)},
4514 "..");
4515 print "</td>\n";
4516 print "<td class=\"link\"></td>\n";
4518 print "</tr>\n";
4520 foreach my $line (@entries) {
4521 my %t = parse_ls_tree_line($line, -z => 1);
4523 if ($alternate) {
4524 print "<tr class=\"dark\">\n";
4525 } else {
4526 print "<tr class=\"light\">\n";
4528 $alternate ^= 1;
4530 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4532 print "</tr>\n";
4534 print "</table>\n" .
4535 "</div>";
4536 git_footer_html();
4539 sub git_snapshot {
4540 my @supported_fmts = gitweb_check_feature('snapshot');
4541 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4543 my $format = $cgi->param('sf');
4544 if (!@supported_fmts) {
4545 die_error(403, "Snapshots not allowed");
4547 # default to first supported snapshot format
4548 $format ||= $supported_fmts[0];
4549 if ($format !~ m/^[a-z0-9]+$/) {
4550 die_error(400, "Invalid snapshot format parameter");
4551 } elsif (!exists($known_snapshot_formats{$format})) {
4552 die_error(400, "Unknown snapshot format");
4553 } elsif (!grep($_ eq $format, @supported_fmts)) {
4554 die_error(403, "Unsupported snapshot format");
4557 if (!defined $hash) {
4558 $hash = git_get_head_hash($project);
4561 my $name = $project;
4562 $name =~ s,([^/])/*\.git$,$1,;
4563 $name = basename($name);
4564 my $filename = to_utf8($name);
4565 $name =~ s/\047/\047\\\047\047/g;
4566 my $cmd;
4567 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4568 $cmd = quote_command(
4569 git_cmd(), 'archive',
4570 "--format=$known_snapshot_formats{$format}{'format'}",
4571 "--prefix=$name/", $hash);
4572 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4573 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
4576 print $cgi->header(
4577 -type => $known_snapshot_formats{$format}{'type'},
4578 -content_disposition => 'inline; filename="' . "$filename" . '"',
4579 -status => '200 OK');
4581 open my $fd, "-|", $cmd
4582 or die_error(500, "Execute git-archive failed");
4583 binmode STDOUT, ':raw';
4584 print <$fd>;
4585 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4586 close $fd;
4589 sub git_log {
4590 my $head = git_get_head_hash($project);
4591 if (!defined $hash) {
4592 $hash = $head;
4594 if (!defined $page) {
4595 $page = 0;
4597 my $refs = git_get_references();
4599 my @commitlist = parse_commits($hash, 101, (100 * $page));
4601 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
4603 git_header_html();
4604 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4606 if (!@commitlist) {
4607 my %co = parse_commit($hash);
4609 git_print_header_div('summary', $project);
4610 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4612 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4613 for (my $i = 0; $i <= $to; $i++) {
4614 my %co = %{$commitlist[$i]};
4615 next if !%co;
4616 my $commit = $co{'id'};
4617 my $ref = format_ref_marker($refs, $commit);
4618 my %ad = parse_date($co{'author_epoch'});
4619 git_print_header_div('commit',
4620 "<span class=\"age\">$co{'age_string'}</span>" .
4621 esc_html($co{'title'}) . $ref,
4622 $commit);
4623 print "<div class=\"title_text\">\n" .
4624 "<div class=\"log_link\">\n" .
4625 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4626 " | " .
4627 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4628 " | " .
4629 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4630 "<br/>\n" .
4631 "</div>\n" .
4632 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4633 "</div>\n";
4635 print "<div class=\"log_body\">\n";
4636 git_print_log($co{'comment'}, -final_empty_line=> 1);
4637 print "</div>\n";
4639 if ($#commitlist >= 100) {
4640 print "<div class=\"page_nav\">\n";
4641 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4642 -accesskey => "n", -title => "Alt-n"}, "next");
4643 print "</div>\n";
4645 git_footer_html();
4648 sub git_commit {
4649 $hash ||= $hash_base || "HEAD";
4650 my %co = parse_commit($hash)
4651 or die_error(404, "Unknown commit object");
4652 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4653 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4655 my $parent = $co{'parent'};
4656 my $parents = $co{'parents'}; # listref
4658 # we need to prepare $formats_nav before any parameter munging
4659 my $formats_nav;
4660 if (!defined $parent) {
4661 # --root commitdiff
4662 $formats_nav .= '(initial)';
4663 } elsif (@$parents == 1) {
4664 # single parent commit
4665 $formats_nav .=
4666 '(parent: ' .
4667 $cgi->a({-href => href(action=>"commit",
4668 hash=>$parent)},
4669 esc_html(substr($parent, 0, 7))) .
4670 ')';
4671 } else {
4672 # merge commit
4673 $formats_nav .=
4674 '(merge: ' .
4675 join(' ', map {
4676 $cgi->a({-href => href(action=>"commit",
4677 hash=>$_)},
4678 esc_html(substr($_, 0, 7)));
4679 } @$parents ) .
4680 ')';
4683 if (!defined $parent) {
4684 $parent = "--root";
4686 my @difftree;
4687 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4688 @diff_opts,
4689 (@$parents <= 1 ? $parent : '-c'),
4690 $hash, "--"
4691 or die_error(500, "Open git-diff-tree failed");
4692 @difftree = map { chomp; $_ } <$fd>;
4693 close $fd or die_error(404, "Reading git-diff-tree failed");
4695 # non-textual hash id's can be cached
4696 my $expires;
4697 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4698 $expires = "+1d";
4700 my $refs = git_get_references();
4701 my $ref = format_ref_marker($refs, $co{'id'});
4703 git_header_html(undef, $expires);
4704 git_print_page_nav('commit', '',
4705 $hash, $co{'tree'}, $hash,
4706 $formats_nav);
4708 if (defined $co{'parent'}) {
4709 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4710 } else {
4711 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4713 print "<div class=\"title_text\">\n" .
4714 "<table class=\"object_header\">\n";
4715 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4716 "<tr>" .
4717 "<td></td><td> $ad{'rfc2822'}";
4718 if ($ad{'hour_local'} < 6) {
4719 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4720 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4721 } else {
4722 printf(" (%02d:%02d %s)",
4723 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4725 print "</td>" .
4726 "</tr>\n";
4727 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4728 print "<tr><td></td><td> $cd{'rfc2822'}" .
4729 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4730 "</td></tr>\n";
4731 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4732 print "<tr>" .
4733 "<td>tree</td>" .
4734 "<td class=\"sha1\">" .
4735 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4736 class => "list"}, $co{'tree'}) .
4737 "</td>" .
4738 "<td class=\"link\">" .
4739 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4740 "tree");
4741 my $snapshot_links = format_snapshot_links($hash);
4742 if (defined $snapshot_links) {
4743 print " | " . $snapshot_links;
4745 print "</td>" .
4746 "</tr>\n";
4748 foreach my $par (@$parents) {
4749 print "<tr>" .
4750 "<td>parent</td>" .
4751 "<td class=\"sha1\">" .
4752 $cgi->a({-href => href(action=>"commit", hash=>$par),
4753 class => "list"}, $par) .
4754 "</td>" .
4755 "<td class=\"link\">" .
4756 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4757 " | " .
4758 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4759 "</td>" .
4760 "</tr>\n";
4762 print "</table>".
4763 "</div>\n";
4765 print "<div class=\"page_body\">\n";
4766 git_print_log($co{'comment'});
4767 print "</div>\n";
4769 git_difftree_body(\@difftree, $hash, @$parents);
4771 git_footer_html();
4774 sub git_object {
4775 # object is defined by:
4776 # - hash or hash_base alone
4777 # - hash_base and file_name
4778 my $type;
4780 # - hash or hash_base alone
4781 if ($hash || ($hash_base && !defined $file_name)) {
4782 my $object_id = $hash || $hash_base;
4784 open my $fd, "-|", quote_command(
4785 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
4786 or die_error(404, "Object does not exist");
4787 $type = <$fd>;
4788 chomp $type;
4789 close $fd
4790 or die_error(404, "Object does not exist");
4792 # - hash_base and file_name
4793 } elsif ($hash_base && defined $file_name) {
4794 $file_name =~ s,/+$,,;
4796 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4797 or die_error(404, "Base object does not exist");
4799 # here errors should not hapen
4800 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4801 or die_error(500, "Open git-ls-tree failed");
4802 my $line = <$fd>;
4803 close $fd;
4805 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4806 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4807 die_error(404, "File or directory for given base does not exist");
4809 $type = $2;
4810 $hash = $3;
4811 } else {
4812 die_error(400, "Not enough information to find object");
4815 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4816 hash=>$hash, hash_base=>$hash_base,
4817 file_name=>$file_name),
4818 -status => '302 Found');
4821 sub git_blobdiff {
4822 my $format = shift || 'html';
4824 my $fd;
4825 my @difftree;
4826 my %diffinfo;
4827 my $expires;
4829 # preparing $fd and %diffinfo for git_patchset_body
4830 # new style URI
4831 if (defined $hash_base && defined $hash_parent_base) {
4832 if (defined $file_name) {
4833 # read raw output
4834 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4835 $hash_parent_base, $hash_base,
4836 "--", (defined $file_parent ? $file_parent : ()), $file_name
4837 or die_error(500, "Open git-diff-tree failed");
4838 @difftree = map { chomp; $_ } <$fd>;
4839 close $fd
4840 or die_error(404, "Reading git-diff-tree failed");
4841 @difftree
4842 or die_error(404, "Blob diff not found");
4844 } elsif (defined $hash &&
4845 $hash =~ /[0-9a-fA-F]{40}/) {
4846 # try to find filename from $hash
4848 # read filtered raw output
4849 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4850 $hash_parent_base, $hash_base, "--"
4851 or die_error(500, "Open git-diff-tree failed");
4852 @difftree =
4853 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4854 # $hash == to_id
4855 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4856 map { chomp; $_ } <$fd>;
4857 close $fd
4858 or die_error(404, "Reading git-diff-tree failed");
4859 @difftree
4860 or die_error(404, "Blob diff not found");
4862 } else {
4863 die_error(400, "Missing one of the blob diff parameters");
4866 if (@difftree > 1) {
4867 die_error(400, "Ambiguous blob diff specification");
4870 %diffinfo = parse_difftree_raw_line($difftree[0]);
4871 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4872 $file_name ||= $diffinfo{'to_file'};
4874 $hash_parent ||= $diffinfo{'from_id'};
4875 $hash ||= $diffinfo{'to_id'};
4877 # non-textual hash id's can be cached
4878 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4879 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4880 $expires = '+1d';
4883 # open patch output
4884 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4885 '-p', ($format eq 'html' ? "--full-index" : ()),
4886 $hash_parent_base, $hash_base,
4887 "--", (defined $file_parent ? $file_parent : ()), $file_name
4888 or die_error(500, "Open git-diff-tree failed");
4891 # old/legacy style URI
4892 if (!%diffinfo && # if new style URI failed
4893 defined $hash && defined $hash_parent) {
4894 # fake git-diff-tree raw output
4895 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4896 $diffinfo{'from_id'} = $hash_parent;
4897 $diffinfo{'to_id'} = $hash;
4898 if (defined $file_name) {
4899 if (defined $file_parent) {
4900 $diffinfo{'status'} = '2';
4901 $diffinfo{'from_file'} = $file_parent;
4902 $diffinfo{'to_file'} = $file_name;
4903 } else { # assume not renamed
4904 $diffinfo{'status'} = '1';
4905 $diffinfo{'from_file'} = $file_name;
4906 $diffinfo{'to_file'} = $file_name;
4908 } else { # no filename given
4909 $diffinfo{'status'} = '2';
4910 $diffinfo{'from_file'} = $hash_parent;
4911 $diffinfo{'to_file'} = $hash;
4914 # non-textual hash id's can be cached
4915 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4916 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4917 $expires = '+1d';
4920 # open patch output
4921 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4922 '-p', ($format eq 'html' ? "--full-index" : ()),
4923 $hash_parent, $hash, "--"
4924 or die_error(500, "Open git-diff failed");
4925 } else {
4926 die_error(400, "Missing one of the blob diff parameters")
4927 unless %diffinfo;
4930 # header
4931 if ($format eq 'html') {
4932 my $formats_nav =
4933 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
4934 "raw");
4935 git_header_html(undef, $expires);
4936 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4937 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4938 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4939 } else {
4940 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4941 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4943 if (defined $file_name) {
4944 git_print_page_path($file_name, "blob", $hash_base);
4945 } else {
4946 print "<div class=\"page_path\"></div>\n";
4949 } elsif ($format eq 'plain') {
4950 print $cgi->header(
4951 -type => 'text/plain',
4952 -charset => 'utf-8',
4953 -expires => $expires,
4954 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4956 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4958 } else {
4959 die_error(400, "Unknown blobdiff format");
4962 # patch
4963 if ($format eq 'html') {
4964 print "<div class=\"page_body\">\n";
4966 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4967 close $fd;
4969 print "</div>\n"; # class="page_body"
4970 git_footer_html();
4972 } else {
4973 while (my $line = <$fd>) {
4974 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4975 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4977 print $line;
4979 last if $line =~ m!^\+\+\+!;
4981 local $/ = undef;
4982 print <$fd>;
4983 close $fd;
4987 sub git_blobdiff_plain {
4988 git_blobdiff('plain');
4991 sub git_commitdiff {
4992 my $format = shift || 'html';
4993 $hash ||= $hash_base || "HEAD";
4994 my %co = parse_commit($hash)
4995 or die_error(404, "Unknown commit object");
4997 # choose format for commitdiff for merge
4998 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4999 $hash_parent = '--cc';
5001 # we need to prepare $formats_nav before almost any parameter munging
5002 my $formats_nav;
5003 if ($format eq 'html') {
5004 $formats_nav =
5005 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5006 "raw");
5008 if (defined $hash_parent &&
5009 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5010 # commitdiff with two commits given
5011 my $hash_parent_short = $hash_parent;
5012 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5013 $hash_parent_short = substr($hash_parent, 0, 7);
5015 $formats_nav .=
5016 ' (from';
5017 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5018 if ($co{'parents'}[$i] eq $hash_parent) {
5019 $formats_nav .= ' parent ' . ($i+1);
5020 last;
5023 $formats_nav .= ': ' .
5024 $cgi->a({-href => href(action=>"commitdiff",
5025 hash=>$hash_parent)},
5026 esc_html($hash_parent_short)) .
5027 ')';
5028 } elsif (!$co{'parent'}) {
5029 # --root commitdiff
5030 $formats_nav .= ' (initial)';
5031 } elsif (scalar @{$co{'parents'}} == 1) {
5032 # single parent commit
5033 $formats_nav .=
5034 ' (parent: ' .
5035 $cgi->a({-href => href(action=>"commitdiff",
5036 hash=>$co{'parent'})},
5037 esc_html(substr($co{'parent'}, 0, 7))) .
5038 ')';
5039 } else {
5040 # merge commit
5041 if ($hash_parent eq '--cc') {
5042 $formats_nav .= ' | ' .
5043 $cgi->a({-href => href(action=>"commitdiff",
5044 hash=>$hash, hash_parent=>'-c')},
5045 'combined');
5046 } else { # $hash_parent eq '-c'
5047 $formats_nav .= ' | ' .
5048 $cgi->a({-href => href(action=>"commitdiff",
5049 hash=>$hash, hash_parent=>'--cc')},
5050 'compact');
5052 $formats_nav .=
5053 ' (merge: ' .
5054 join(' ', map {
5055 $cgi->a({-href => href(action=>"commitdiff",
5056 hash=>$_)},
5057 esc_html(substr($_, 0, 7)));
5058 } @{$co{'parents'}} ) .
5059 ')';
5063 my $hash_parent_param = $hash_parent;
5064 if (!defined $hash_parent_param) {
5065 # --cc for multiple parents, --root for parentless
5066 $hash_parent_param =
5067 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5070 # read commitdiff
5071 my $fd;
5072 my @difftree;
5073 if ($format eq 'html') {
5074 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5075 "--no-commit-id", "--patch-with-raw", "--full-index",
5076 $hash_parent_param, $hash, "--"
5077 or die_error(500, "Open git-diff-tree failed");
5079 while (my $line = <$fd>) {
5080 chomp $line;
5081 # empty line ends raw part of diff-tree output
5082 last unless $line;
5083 push @difftree, scalar parse_difftree_raw_line($line);
5086 } elsif ($format eq 'plain') {
5087 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5088 '-p', $hash_parent_param, $hash, "--"
5089 or die_error(500, "Open git-diff-tree failed");
5091 } else {
5092 die_error(400, "Unknown commitdiff format");
5095 # non-textual hash id's can be cached
5096 my $expires;
5097 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5098 $expires = "+1d";
5101 # write commit message
5102 if ($format eq 'html') {
5103 my $refs = git_get_references();
5104 my $ref = format_ref_marker($refs, $co{'id'});
5106 git_header_html(undef, $expires);
5107 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5108 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5109 git_print_authorship(\%co);
5110 print "<div class=\"page_body\">\n";
5111 if (@{$co{'comment'}} > 1) {
5112 print "<div class=\"log\">\n";
5113 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5114 print "</div>\n"; # class="log"
5117 } elsif ($format eq 'plain') {
5118 my $refs = git_get_references("tags");
5119 my $tagname = git_get_rev_name_tags($hash);
5120 my $filename = basename($project) . "-$hash.patch";
5122 print $cgi->header(
5123 -type => 'text/plain',
5124 -charset => 'utf-8',
5125 -expires => $expires,
5126 -content_disposition => 'inline; filename="' . "$filename" . '"');
5127 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5128 print "From: " . to_utf8($co{'author'}) . "\n";
5129 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5130 print "Subject: " . to_utf8($co{'title'}) . "\n";
5132 print "X-Git-Tag: $tagname\n" if $tagname;
5133 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5135 foreach my $line (@{$co{'comment'}}) {
5136 print to_utf8($line) . "\n";
5138 print "---\n\n";
5141 # write patch
5142 if ($format eq 'html') {
5143 my $use_parents = !defined $hash_parent ||
5144 $hash_parent eq '-c' || $hash_parent eq '--cc';
5145 git_difftree_body(\@difftree, $hash,
5146 $use_parents ? @{$co{'parents'}} : $hash_parent);
5147 print "<br/>\n";
5149 git_patchset_body($fd, \@difftree, $hash,
5150 $use_parents ? @{$co{'parents'}} : $hash_parent);
5151 close $fd;
5152 print "</div>\n"; # class="page_body"
5153 git_footer_html();
5155 } elsif ($format eq 'plain') {
5156 local $/ = undef;
5157 print <$fd>;
5158 close $fd
5159 or print "Reading git-diff-tree failed\n";
5163 sub git_commitdiff_plain {
5164 git_commitdiff('plain');
5167 sub git_history {
5168 if (!defined $hash_base) {
5169 $hash_base = git_get_head_hash($project);
5171 if (!defined $page) {
5172 $page = 0;
5174 my $ftype;
5175 my %co = parse_commit($hash_base)
5176 or die_error(404, "Unknown commit object");
5178 my $refs = git_get_references();
5179 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5181 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5182 $file_name, "--full-history")
5183 or die_error(404, "No such file or directory on given branch");
5185 if (!defined $hash && defined $file_name) {
5186 # some commits could have deleted file in question,
5187 # and not have it in tree, but one of them has to have it
5188 for (my $i = 0; $i <= @commitlist; $i++) {
5189 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5190 last if defined $hash;
5193 if (defined $hash) {
5194 $ftype = git_get_type($hash);
5196 if (!defined $ftype) {
5197 die_error(500, "Unknown type of object");
5200 my $paging_nav = '';
5201 if ($page > 0) {
5202 $paging_nav .=
5203 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5204 file_name=>$file_name)},
5205 "first");
5206 $paging_nav .= " &sdot; " .
5207 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5208 -accesskey => "p", -title => "Alt-p"}, "prev");
5209 } else {
5210 $paging_nav .= "first";
5211 $paging_nav .= " &sdot; prev";
5213 my $next_link = '';
5214 if ($#commitlist >= 100) {
5215 $next_link =
5216 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5217 -accesskey => "n", -title => "Alt-n"}, "next");
5218 $paging_nav .= " &sdot; $next_link";
5219 } else {
5220 $paging_nav .= " &sdot; next";
5223 git_header_html();
5224 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5225 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5226 git_print_page_path($file_name, $ftype, $hash_base);
5228 git_history_body(\@commitlist, 0, 99,
5229 $refs, $hash_base, $ftype, $next_link);
5231 git_footer_html();
5234 sub git_search {
5235 gitweb_check_feature('search') or die_error(403, "Search is disabled");
5236 if (!defined $searchtext) {
5237 die_error(400, "Text field is empty");
5239 if (!defined $hash) {
5240 $hash = git_get_head_hash($project);
5242 my %co = parse_commit($hash);
5243 if (!%co) {
5244 die_error(404, "Unknown commit object");
5246 if (!defined $page) {
5247 $page = 0;
5250 $searchtype ||= 'commit';
5251 if ($searchtype eq 'pickaxe') {
5252 # pickaxe may take all resources of your box and run for several minutes
5253 # with every query - so decide by yourself how public you make this feature
5254 gitweb_check_feature('pickaxe')
5255 or die_error(403, "Pickaxe is disabled");
5257 if ($searchtype eq 'grep') {
5258 gitweb_check_feature('grep')
5259 or die_error(403, "Grep is disabled");
5262 git_header_html();
5264 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5265 my $greptype;
5266 if ($searchtype eq 'commit') {
5267 $greptype = "--grep=";
5268 } elsif ($searchtype eq 'author') {
5269 $greptype = "--author=";
5270 } elsif ($searchtype eq 'committer') {
5271 $greptype = "--committer=";
5273 $greptype .= $searchtext;
5274 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5275 $greptype, '--regexp-ignore-case',
5276 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5278 my $paging_nav = '';
5279 if ($page > 0) {
5280 $paging_nav .=
5281 $cgi->a({-href => href(action=>"search", hash=>$hash,
5282 searchtext=>$searchtext,
5283 searchtype=>$searchtype)},
5284 "first");
5285 $paging_nav .= " &sdot; " .
5286 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5287 -accesskey => "p", -title => "Alt-p"}, "prev");
5288 } else {
5289 $paging_nav .= "first";
5290 $paging_nav .= " &sdot; prev";
5292 my $next_link = '';
5293 if ($#commitlist >= 100) {
5294 $next_link =
5295 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5296 -accesskey => "n", -title => "Alt-n"}, "next");
5297 $paging_nav .= " &sdot; $next_link";
5298 } else {
5299 $paging_nav .= " &sdot; next";
5302 if ($#commitlist >= 100) {
5305 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5306 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5307 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5310 if ($searchtype eq 'pickaxe') {
5311 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5312 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5314 print "<table class=\"pickaxe search\">\n";
5315 my $alternate = 1;
5316 $/ = "\n";
5317 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5318 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5319 ($search_use_regexp ? '--pickaxe-regex' : ());
5320 undef %co;
5321 my @files;
5322 while (my $line = <$fd>) {
5323 chomp $line;
5324 next unless $line;
5326 my %set = parse_difftree_raw_line($line);
5327 if (defined $set{'commit'}) {
5328 # finish previous commit
5329 if (%co) {
5330 print "</td>\n" .
5331 "<td class=\"link\">" .
5332 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5333 " | " .
5334 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5335 print "</td>\n" .
5336 "</tr>\n";
5339 if ($alternate) {
5340 print "<tr class=\"dark\">\n";
5341 } else {
5342 print "<tr class=\"light\">\n";
5344 $alternate ^= 1;
5345 %co = parse_commit($set{'commit'});
5346 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5347 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5348 "<td><i>$author</i></td>\n" .
5349 "<td>" .
5350 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5351 -class => "list subject"},
5352 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5353 } elsif (defined $set{'to_id'}) {
5354 next if ($set{'to_id'} =~ m/^0{40}$/);
5356 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5357 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5358 -class => "list"},
5359 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5360 "<br/>\n";
5363 close $fd;
5365 # finish last commit (warning: repetition!)
5366 if (%co) {
5367 print "</td>\n" .
5368 "<td class=\"link\">" .
5369 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5370 " | " .
5371 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5372 print "</td>\n" .
5373 "</tr>\n";
5376 print "</table>\n";
5379 if ($searchtype eq 'grep') {
5380 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5381 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5383 print "<table class=\"grep_search\">\n";
5384 my $alternate = 1;
5385 my $matches = 0;
5386 $/ = "\n";
5387 open my $fd, "-|", git_cmd(), 'grep', '-n',
5388 $search_use_regexp ? ('-E', '-i') : '-F',
5389 $searchtext, $co{'tree'};
5390 my $lastfile = '';
5391 while (my $line = <$fd>) {
5392 chomp $line;
5393 my ($file, $lno, $ltext, $binary);
5394 last if ($matches++ > 1000);
5395 if ($line =~ /^Binary file (.+) matches$/) {
5396 $file = $1;
5397 $binary = 1;
5398 } else {
5399 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5401 if ($file ne $lastfile) {
5402 $lastfile and print "</td></tr>\n";
5403 if ($alternate++) {
5404 print "<tr class=\"dark\">\n";
5405 } else {
5406 print "<tr class=\"light\">\n";
5408 print "<td class=\"list\">".
5409 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5410 file_name=>"$file"),
5411 -class => "list"}, esc_path($file));
5412 print "</td><td>\n";
5413 $lastfile = $file;
5415 if ($binary) {
5416 print "<div class=\"binary\">Binary file</div>\n";
5417 } else {
5418 $ltext = untabify($ltext);
5419 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5420 $ltext = esc_html($1, -nbsp=>1);
5421 $ltext .= '<span class="match">';
5422 $ltext .= esc_html($2, -nbsp=>1);
5423 $ltext .= '</span>';
5424 $ltext .= esc_html($3, -nbsp=>1);
5425 } else {
5426 $ltext = esc_html($ltext, -nbsp=>1);
5428 print "<div class=\"pre\">" .
5429 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5430 file_name=>"$file").'#l'.$lno,
5431 -class => "linenr"}, sprintf('%4i', $lno))
5432 . ' ' . $ltext . "</div>\n";
5435 if ($lastfile) {
5436 print "</td></tr>\n";
5437 if ($matches > 1000) {
5438 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5440 } else {
5441 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5443 close $fd;
5445 print "</table>\n";
5447 git_footer_html();
5450 sub git_search_help {
5451 git_header_html();
5452 git_print_page_nav('','', $hash,$hash,$hash);
5453 print <<EOT;
5454 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5455 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5456 the pattern entered is recognized as the POSIX extended
5457 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5458 insensitive).</p>
5459 <dl>
5460 <dt><b>commit</b></dt>
5461 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5463 my ($have_grep) = gitweb_check_feature('grep');
5464 if ($have_grep) {
5465 print <<EOT;
5466 <dt><b>grep</b></dt>
5467 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5468 a different one) are searched for the given pattern. On large trees, this search can take
5469 a while and put some strain on the server, so please use it with some consideration. Note that
5470 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5471 case-sensitive.</dd>
5474 print <<EOT;
5475 <dt><b>author</b></dt>
5476 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5477 <dt><b>committer</b></dt>
5478 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5480 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5481 if ($have_pickaxe) {
5482 print <<EOT;
5483 <dt><b>pickaxe</b></dt>
5484 <dd>All commits that caused the string to appear or disappear from any file (changes that
5485 added, removed or "modified" the string) will be listed. This search can take a while and
5486 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5487 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5490 print "</dl>\n";
5491 git_footer_html();
5494 sub git_shortlog {
5495 my $head = git_get_head_hash($project);
5496 if (!defined $hash) {
5497 $hash = $head;
5499 if (!defined $page) {
5500 $page = 0;
5502 my $refs = git_get_references();
5504 my $commit_hash = $hash;
5505 if (defined $hash_parent) {
5506 $commit_hash = "$hash_parent..$hash";
5508 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
5510 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5511 my $next_link = '';
5512 if ($#commitlist >= 100) {
5513 $next_link =
5514 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5515 -accesskey => "n", -title => "Alt-n"}, "next");
5518 git_header_html();
5519 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5520 git_print_header_div('summary', $project);
5522 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5524 git_footer_html();
5527 ## ......................................................................
5528 ## feeds (RSS, Atom; OPML)
5530 sub git_feed {
5531 my $format = shift || 'atom';
5532 my ($have_blame) = gitweb_check_feature('blame');
5534 # Atom: http://www.atomenabled.org/developers/syndication/
5535 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5536 if ($format ne 'rss' && $format ne 'atom') {
5537 die_error(400, "Unknown web feed format");
5540 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5541 my $head = $hash || 'HEAD';
5542 my @commitlist = parse_commits($head, 150, 0, $file_name);
5544 my %latest_commit;
5545 my %latest_date;
5546 my $content_type = "application/$format+xml";
5547 if (defined $cgi->http('HTTP_ACCEPT') &&
5548 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5549 # browser (feed reader) prefers text/xml
5550 $content_type = 'text/xml';
5552 if (defined($commitlist[0])) {
5553 %latest_commit = %{$commitlist[0]};
5554 %latest_date = parse_date($latest_commit{'author_epoch'});
5555 print $cgi->header(
5556 -type => $content_type,
5557 -charset => 'utf-8',
5558 -last_modified => $latest_date{'rfc2822'});
5559 } else {
5560 print $cgi->header(
5561 -type => $content_type,
5562 -charset => 'utf-8');
5565 # Optimization: skip generating the body if client asks only
5566 # for Last-Modified date.
5567 return if ($cgi->request_method() eq 'HEAD');
5569 # header variables
5570 my $title = "$site_name - $project/$action";
5571 my $feed_type = 'log';
5572 if (defined $hash) {
5573 $title .= " - '$hash'";
5574 $feed_type = 'branch log';
5575 if (defined $file_name) {
5576 $title .= " :: $file_name";
5577 $feed_type = 'history';
5579 } elsif (defined $file_name) {
5580 $title .= " - $file_name";
5581 $feed_type = 'history';
5583 $title .= " $feed_type";
5584 my $descr = git_get_project_description($project);
5585 if (defined $descr) {
5586 $descr = esc_html($descr);
5587 } else {
5588 $descr = "$project " .
5589 ($format eq 'rss' ? 'RSS' : 'Atom') .
5590 " feed";
5592 my $owner = git_get_project_owner($project);
5593 $owner = esc_html($owner);
5595 #header
5596 my $alt_url;
5597 if (defined $file_name) {
5598 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5599 } elsif (defined $hash) {
5600 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5601 } else {
5602 $alt_url = href(-full=>1, action=>"summary");
5604 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5605 if ($format eq 'rss') {
5606 print <<XML;
5607 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5608 <channel>
5610 print "<title>$title</title>\n" .
5611 "<link>$alt_url</link>\n" .
5612 "<description>$descr</description>\n" .
5613 "<language>en</language>\n";
5614 } elsif ($format eq 'atom') {
5615 print <<XML;
5616 <feed xmlns="http://www.w3.org/2005/Atom">
5618 print "<title>$title</title>\n" .
5619 "<subtitle>$descr</subtitle>\n" .
5620 '<link rel="alternate" type="text/html" href="' .
5621 $alt_url . '" />' . "\n" .
5622 '<link rel="self" type="' . $content_type . '" href="' .
5623 $cgi->self_url() . '" />' . "\n" .
5624 "<id>" . href(-full=>1) . "</id>\n" .
5625 # use project owner for feed author
5626 "<author><name>$owner</name></author>\n";
5627 if (defined $favicon) {
5628 print "<icon>" . esc_url($favicon) . "</icon>\n";
5630 if (defined $logo_url) {
5631 # not twice as wide as tall: 72 x 27 pixels
5632 print "<logo>" . esc_url($logo) . "</logo>\n";
5634 if (! %latest_date) {
5635 # dummy date to keep the feed valid until commits trickle in:
5636 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5637 } else {
5638 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5642 # contents
5643 for (my $i = 0; $i <= $#commitlist; $i++) {
5644 my %co = %{$commitlist[$i]};
5645 my $commit = $co{'id'};
5646 # we read 150, we always show 30 and the ones more recent than 48 hours
5647 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5648 last;
5650 my %cd = parse_date($co{'author_epoch'});
5652 # get list of changed files
5653 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5654 $co{'parent'} || "--root",
5655 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5656 or next;
5657 my @difftree = map { chomp; $_ } <$fd>;
5658 close $fd
5659 or next;
5661 # print element (entry, item)
5662 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5663 if ($format eq 'rss') {
5664 print "<item>\n" .
5665 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5666 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5667 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5668 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5669 "<link>$co_url</link>\n" .
5670 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5671 "<content:encoded>" .
5672 "<![CDATA[\n";
5673 } elsif ($format eq 'atom') {
5674 print "<entry>\n" .
5675 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5676 "<updated>$cd{'iso-8601'}</updated>\n" .
5677 "<author>\n" .
5678 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5679 if ($co{'author_email'}) {
5680 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5682 print "</author>\n" .
5683 # use committer for contributor
5684 "<contributor>\n" .
5685 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5686 if ($co{'committer_email'}) {
5687 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5689 print "</contributor>\n" .
5690 "<published>$cd{'iso-8601'}</published>\n" .
5691 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5692 "<id>$co_url</id>\n" .
5693 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5694 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5696 my $comment = $co{'comment'};
5697 print "<pre>\n";
5698 foreach my $line (@$comment) {
5699 $line = esc_html($line);
5700 print "$line\n";
5702 print "</pre><ul>\n";
5703 foreach my $difftree_line (@difftree) {
5704 my %difftree = parse_difftree_raw_line($difftree_line);
5705 next if !$difftree{'from_id'};
5707 my $file = $difftree{'file'} || $difftree{'to_file'};
5709 print "<li>" .
5710 "[" .
5711 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5712 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5713 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5714 file_name=>$file, file_parent=>$difftree{'from_file'}),
5715 -title => "diff"}, 'D');
5716 if ($have_blame) {
5717 print $cgi->a({-href => href(-full=>1, action=>"blame",
5718 file_name=>$file, hash_base=>$commit),
5719 -title => "blame"}, 'B');
5721 # if this is not a feed of a file history
5722 if (!defined $file_name || $file_name ne $file) {
5723 print $cgi->a({-href => href(-full=>1, action=>"history",
5724 file_name=>$file, hash=>$commit),
5725 -title => "history"}, 'H');
5727 $file = esc_path($file);
5728 print "] ".
5729 "$file</li>\n";
5731 if ($format eq 'rss') {
5732 print "</ul>]]>\n" .
5733 "</content:encoded>\n" .
5734 "</item>\n";
5735 } elsif ($format eq 'atom') {
5736 print "</ul>\n</div>\n" .
5737 "</content>\n" .
5738 "</entry>\n";
5742 # end of feed
5743 if ($format eq 'rss') {
5744 print "</channel>\n</rss>\n";
5745 } elsif ($format eq 'atom') {
5746 print "</feed>\n";
5750 sub git_rss {
5751 git_feed('rss');
5754 sub git_atom {
5755 git_feed('atom');
5758 sub git_opml {
5759 my @list = git_get_projects_list();
5761 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5762 print <<XML;
5763 <?xml version="1.0" encoding="utf-8"?>
5764 <opml version="1.0">
5765 <head>
5766 <title>$site_name OPML Export</title>
5767 </head>
5768 <body>
5769 <outline text="git RSS feeds">
5772 foreach my $pr (@list) {
5773 my %proj = %$pr;
5774 my $head = git_get_head_hash($proj{'path'});
5775 if (!defined $head) {
5776 next;
5778 $git_dir = "$projectroot/$proj{'path'}";
5779 my %co = parse_commit($head);
5780 if (!%co) {
5781 next;
5784 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5785 my $rss = "$my_url?p=$proj{'path'};a=rss";
5786 my $html = "$my_url?p=$proj{'path'};a=summary";
5787 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5789 print <<XML;
5790 </outline>
5791 </body>
5792 </opml>