Merge branch 't/extra-actions/log-consolidate' into refs/top-bases/master
[git/gitweb.git] / gitweb / gitweb.perl
blob0197ec2afe0b37bc1bc444a983eedadf63d921eb
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]},
286 # Insert custom links to the action bar of all project pages.
287 # This enables you mainly to link to third-party scripts integrating
288 # into gitweb; e.g. git-browser for graphical history representation
289 # or custom web-based repository administration interface.
291 # The 'default' value consists of a list of triplets in the form
292 # (label, link, position) where position is the label after which
293 # to inster the link and link is a format string where %n expands
294 # to the project name, %f to the project path within the filesystem,
295 # %h to the current hash (h gitweb parameter) and %b to the current
296 # hash base (hb gitweb parameter).
298 # To enable system wide have in $GITWEB_CONFIG e.g.
299 # $feature{'actions'}{'default'} = [('graphiclog',
300 # '/git-browser/by-commit.html?r=%n', 'summary')];
301 # Project specific override is not supported.
302 'actions' => {
303 'override' => 0,
304 'default' => []},
307 sub gitweb_check_feature {
308 my ($name) = @_;
309 return unless exists $feature{$name};
310 my ($sub, $override, @defaults) = (
311 $feature{$name}{'sub'},
312 $feature{$name}{'override'},
313 @{$feature{$name}{'default'}});
314 if (!$override) { return @defaults; }
315 if (!defined $sub) {
316 warn "feature $name is not overrideable";
317 return @defaults;
319 return $sub->(@defaults);
322 sub feature_blame {
323 my ($val) = git_get_project_config('blame', '--bool');
325 if ($val eq 'true') {
326 return 1;
327 } elsif ($val eq 'false') {
328 return 0;
331 return $_[0];
334 sub feature_snapshot {
335 my (@fmts) = @_;
337 my ($val) = git_get_project_config('snapshot');
339 if ($val) {
340 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
343 return @fmts;
346 sub feature_grep {
347 my ($val) = git_get_project_config('grep', '--bool');
349 if ($val eq 'true') {
350 return (1);
351 } elsif ($val eq 'false') {
352 return (0);
355 return ($_[0]);
358 sub feature_pickaxe {
359 my ($val) = git_get_project_config('pickaxe', '--bool');
361 if ($val eq 'true') {
362 return (1);
363 } elsif ($val eq 'false') {
364 return (0);
367 return ($_[0]);
370 # checking HEAD file with -e is fragile if the repository was
371 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
372 # and then pruned.
373 sub check_head_link {
374 my ($dir) = @_;
375 my $headfile = "$dir/HEAD";
376 return ((-e $headfile) ||
377 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
380 sub check_export_ok {
381 my ($dir) = @_;
382 return (check_head_link($dir) &&
383 (!$export_ok || -e "$dir/$export_ok"));
386 # process alternate names for backward compatibility
387 # filter out unsupported (unknown) snapshot formats
388 sub filter_snapshot_fmts {
389 my @fmts = @_;
391 @fmts = map {
392 exists $known_snapshot_format_aliases{$_} ?
393 $known_snapshot_format_aliases{$_} : $_} @fmts;
394 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
398 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
399 if (-e $GITWEB_CONFIG) {
400 do $GITWEB_CONFIG;
401 } else {
402 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
403 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
406 # version of the core git binary
407 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
409 $projects_list ||= $projectroot;
411 # ======================================================================
412 # input validation and dispatch
413 our $action = $cgi->param('a');
414 if (defined $action) {
415 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
416 die_error(400, "Invalid action parameter");
420 # parameters which are pathnames
421 our $project = $cgi->param('p');
422 if (defined $project) {
423 if (!validate_pathname($project) ||
424 !(-d "$projectroot/$project") ||
425 !check_head_link("$projectroot/$project") ||
426 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
427 ($strict_export && !project_in_list($project))) {
428 undef $project;
429 die_error(404, "No such project");
433 our $file_name = $cgi->param('f');
434 if (defined $file_name) {
435 if (!validate_pathname($file_name)) {
436 die_error(400, "Invalid file parameter");
440 our $file_parent = $cgi->param('fp');
441 if (defined $file_parent) {
442 if (!validate_pathname($file_parent)) {
443 die_error(400, "Invalid file parent parameter");
447 # parameters which are refnames
448 our $hash = $cgi->param('h');
449 if (defined $hash) {
450 if (!validate_refname($hash)) {
451 die_error(400, "Invalid hash parameter");
455 our $hash_parent = $cgi->param('hp');
456 if (defined $hash_parent) {
457 if (!validate_refname($hash_parent)) {
458 die_error(400, "Invalid hash parent parameter");
462 our $hash_base = $cgi->param('hb');
463 if (defined $hash_base) {
464 if (!validate_refname($hash_base)) {
465 die_error(400, "Invalid hash base parameter");
469 my %allowed_options = (
470 "--no-merges" => [ qw(rss atom log shortlog history) ],
473 our @extra_options = $cgi->param('opt');
474 if (defined @extra_options) {
475 foreach my $opt (@extra_options) {
476 if (not exists $allowed_options{$opt}) {
477 die_error(400, "Invalid option parameter");
479 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
480 die_error(400, "Invalid option parameter for this action");
485 our $hash_parent_base = $cgi->param('hpb');
486 if (defined $hash_parent_base) {
487 if (!validate_refname($hash_parent_base)) {
488 die_error(400, "Invalid hash parent base parameter");
492 # other parameters
493 our $page = $cgi->param('pg');
494 if (defined $page) {
495 if ($page =~ m/[^0-9]/) {
496 die_error(400, "Invalid page parameter");
500 our $searchtype = $cgi->param('st');
501 if (defined $searchtype) {
502 if ($searchtype =~ m/[^a-z]/) {
503 die_error(400, "Invalid searchtype parameter");
507 our $search_use_regexp = $cgi->param('sr');
509 our $searchtext = $cgi->param('s');
510 our $search_regexp;
511 if (defined $searchtext) {
512 if (length($searchtext) < 2) {
513 die_error(403, "At least two characters are required for search parameter");
515 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
518 # now read PATH_INFO and use it as alternative to parameters
519 sub evaluate_path_info {
520 return if defined $project;
521 my $path_info = $ENV{"PATH_INFO"};
522 return if !$path_info;
523 $path_info =~ s,^/+,,;
524 return if !$path_info;
525 # find which part of PATH_INFO is project
526 $project = $path_info;
527 $project =~ s,/+$,,;
528 while ($project && !check_head_link("$projectroot/$project")) {
529 $project =~ s,/*[^/]*$,,;
531 # validate project
532 $project = validate_pathname($project);
533 if (!$project ||
534 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
535 ($strict_export && !project_in_list($project))) {
536 undef $project;
537 return;
539 # do not change any parameters if an action is given using the query string
540 return if $action;
541 $path_info =~ s,^\Q$project\E/*,,;
542 my ($refname, $pathname) = split(/:/, $path_info, 2);
543 if (defined $pathname) {
544 # we got "project.git/branch:filename" or "project.git/branch:dir/"
545 # we could use git_get_type(branch:pathname), but it needs $git_dir
546 $pathname =~ s,^/+,,;
547 if (!$pathname || substr($pathname, -1) eq "/") {
548 $action ||= "tree";
549 $pathname =~ s,/$,,;
550 } else {
551 $action ||= "blob_plain";
553 $hash_base ||= validate_refname($refname);
554 $file_name ||= validate_pathname($pathname);
555 } elsif (defined $refname) {
556 # we got "project.git/branch"
557 $action ||= "shortlog";
558 $hash ||= validate_refname($refname);
561 evaluate_path_info();
563 # path to the current git repository
564 our $git_dir;
565 $git_dir = "$projectroot/$project" if $project;
567 # dispatch
568 my %actions = (
569 "blame" => \&git_blame,
570 "blobdiff" => \&git_blobdiff,
571 "blobdiff_plain" => \&git_blobdiff_plain,
572 "blob" => \&git_blob,
573 "blob_plain" => \&git_blob_plain,
574 "commitdiff" => \&git_commitdiff,
575 "commitdiff_plain" => \&git_commitdiff_plain,
576 "commit" => \&git_commit,
577 "forks" => \&git_forks,
578 "heads" => \&git_heads,
579 "history" => \&git_history,
580 "log" => \&git_log,
581 "rss" => \&git_rss,
582 "atom" => \&git_atom,
583 "search" => \&git_search,
584 "search_help" => \&git_search_help,
585 "shortlog" => \&git_shortlog,
586 "summary" => \&git_summary,
587 "tag" => \&git_tag,
588 "tags" => \&git_tags,
589 "tree" => \&git_tree,
590 "snapshot" => \&git_snapshot,
591 "object" => \&git_object,
592 # those below don't need $project
593 "opml" => \&git_opml,
594 "project_list" => \&git_project_list,
595 "project_index" => \&git_project_index,
598 if (!defined $action) {
599 if (defined $hash) {
600 $action = git_get_type($hash);
601 } elsif (defined $hash_base && defined $file_name) {
602 $action = git_get_type("$hash_base:$file_name");
603 } elsif (defined $project) {
604 $action = 'summary';
605 } else {
606 $action = 'project_list';
609 if (!defined($actions{$action})) {
610 die_error(400, "Unknown action");
612 if ($action !~ m/^(opml|project_list|project_index)$/ &&
613 !$project) {
614 die_error(400, "Project needed");
616 $actions{$action}->();
617 exit;
619 ## ======================================================================
620 ## action links
622 sub href (%) {
623 my %params = @_;
624 # default is to use -absolute url() i.e. $my_uri
625 my $href = $params{-full} ? $my_url : $my_uri;
627 # XXX: Warning: If you touch this, check the search form for updating,
628 # too.
630 my @mapping = (
631 project => "p",
632 action => "a",
633 file_name => "f",
634 file_parent => "fp",
635 hash => "h",
636 hash_parent => "hp",
637 hash_base => "hb",
638 hash_parent_base => "hpb",
639 page => "pg",
640 order => "o",
641 searchtext => "s",
642 searchtype => "st",
643 snapshot_format => "sf",
644 extra_options => "opt",
645 search_use_regexp => "sr",
647 my %mapping = @mapping;
649 $params{'project'} = $project unless exists $params{'project'};
651 if ($params{-replay}) {
652 while (my ($name, $symbol) = each %mapping) {
653 if (!exists $params{$name}) {
654 # to allow for multivalued params we use arrayref form
655 $params{$name} = [ $cgi->param($symbol) ];
660 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
661 if ($use_pathinfo) {
662 # use PATH_INFO for project name
663 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
664 delete $params{'project'};
666 # Summary just uses the project path URL
667 if (defined $params{'action'} && $params{'action'} eq 'summary') {
668 delete $params{'action'};
672 # now encode the parameters explicitly
673 my @result = ();
674 for (my $i = 0; $i < @mapping; $i += 2) {
675 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
676 if (defined $params{$name}) {
677 if (ref($params{$name}) eq "ARRAY") {
678 foreach my $par (@{$params{$name}}) {
679 push @result, $symbol . "=" . esc_param($par);
681 } else {
682 push @result, $symbol . "=" . esc_param($params{$name});
686 $href .= "?" . join(';', @result) if scalar @result;
688 return $href;
692 ## ======================================================================
693 ## validation, quoting/unquoting and escaping
695 sub validate_pathname {
696 my $input = shift || return undef;
698 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
699 # at the beginning, at the end, and between slashes.
700 # also this catches doubled slashes
701 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
702 return undef;
704 # no null characters
705 if ($input =~ m!\0!) {
706 return undef;
708 return $input;
711 sub validate_refname {
712 my $input = shift || return undef;
714 # textual hashes are O.K.
715 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
716 return $input;
718 # it must be correct pathname
719 $input = validate_pathname($input)
720 or return undef;
721 # restrictions on ref name according to git-check-ref-format
722 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
723 return undef;
725 return $input;
728 # decode sequences of octets in utf8 into Perl's internal form,
729 # which is utf-8 with utf8 flag set if needed. gitweb writes out
730 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
731 sub to_utf8 {
732 my $str = shift;
733 if (utf8::valid($str)) {
734 utf8::decode($str);
735 return $str;
736 } else {
737 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
741 # quote unsafe chars, but keep the slash, even when it's not
742 # correct, but quoted slashes look too horrible in bookmarks
743 sub esc_param {
744 my $str = shift;
745 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
746 $str =~ s/\+/%2B/g;
747 $str =~ s/ /\+/g;
748 return $str;
751 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
752 sub esc_url {
753 my $str = shift;
754 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
755 $str =~ s/\+/%2B/g;
756 $str =~ s/ /\+/g;
757 return $str;
760 # replace invalid utf8 character with SUBSTITUTION sequence
761 sub esc_html ($;%) {
762 my $str = shift;
763 my %opts = @_;
765 $str = to_utf8($str);
766 $str = $cgi->escapeHTML($str);
767 if ($opts{'-nbsp'}) {
768 $str =~ s/ /&nbsp;/g;
770 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
771 return $str;
774 # quote control characters and escape filename to HTML
775 sub esc_path {
776 my $str = shift;
777 my %opts = @_;
779 $str = to_utf8($str);
780 $str = $cgi->escapeHTML($str);
781 if ($opts{'-nbsp'}) {
782 $str =~ s/ /&nbsp;/g;
784 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
785 return $str;
788 # Make control characters "printable", using character escape codes (CEC)
789 sub quot_cec {
790 my $cntrl = shift;
791 my %opts = @_;
792 my %es = ( # character escape codes, aka escape sequences
793 "\t" => '\t', # tab (HT)
794 "\n" => '\n', # line feed (LF)
795 "\r" => '\r', # carrige return (CR)
796 "\f" => '\f', # form feed (FF)
797 "\b" => '\b', # backspace (BS)
798 "\a" => '\a', # alarm (bell) (BEL)
799 "\e" => '\e', # escape (ESC)
800 "\013" => '\v', # vertical tab (VT)
801 "\000" => '\0', # nul character (NUL)
803 my $chr = ( (exists $es{$cntrl})
804 ? $es{$cntrl}
805 : sprintf('\%2x', ord($cntrl)) );
806 if ($opts{-nohtml}) {
807 return $chr;
808 } else {
809 return "<span class=\"cntrl\">$chr</span>";
813 # Alternatively use unicode control pictures codepoints,
814 # Unicode "printable representation" (PR)
815 sub quot_upr {
816 my $cntrl = shift;
817 my %opts = @_;
819 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
820 if ($opts{-nohtml}) {
821 return $chr;
822 } else {
823 return "<span class=\"cntrl\">$chr</span>";
827 # git may return quoted and escaped filenames
828 sub unquote {
829 my $str = shift;
831 sub unq {
832 my $seq = shift;
833 my %es = ( # character escape codes, aka escape sequences
834 't' => "\t", # tab (HT, TAB)
835 'n' => "\n", # newline (NL)
836 'r' => "\r", # return (CR)
837 'f' => "\f", # form feed (FF)
838 'b' => "\b", # backspace (BS)
839 'a' => "\a", # alarm (bell) (BEL)
840 'e' => "\e", # escape (ESC)
841 'v' => "\013", # vertical tab (VT)
844 if ($seq =~ m/^[0-7]{1,3}$/) {
845 # octal char sequence
846 return chr(oct($seq));
847 } elsif (exists $es{$seq}) {
848 # C escape sequence, aka character escape code
849 return $es{$seq};
851 # quoted ordinary character
852 return $seq;
855 if ($str =~ m/^"(.*)"$/) {
856 # needs unquoting
857 $str = $1;
858 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
860 return $str;
863 # escape tabs (convert tabs to spaces)
864 sub untabify {
865 my $line = shift;
867 while ((my $pos = index($line, "\t")) != -1) {
868 if (my $count = (8 - ($pos % 8))) {
869 my $spaces = ' ' x $count;
870 $line =~ s/\t/$spaces/;
874 return $line;
877 sub project_in_list {
878 my $project = shift;
879 my @list = git_get_projects_list();
880 return @list && scalar(grep { $_->{'path'} eq $project } @list);
883 ## ----------------------------------------------------------------------
884 ## HTML aware string manipulation
886 # Try to chop given string on a word boundary between position
887 # $len and $len+$add_len. If there is no word boundary there,
888 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
889 # (marking chopped part) would be longer than given string.
890 sub chop_str {
891 my $str = shift;
892 my $len = shift;
893 my $add_len = shift || 10;
894 my $where = shift || 'right'; # 'left' | 'center' | 'right'
896 # Make sure perl knows it is utf8 encoded so we don't
897 # cut in the middle of a utf8 multibyte char.
898 $str = to_utf8($str);
900 # allow only $len chars, but don't cut a word if it would fit in $add_len
901 # if it doesn't fit, cut it if it's still longer than the dots we would add
902 # remove chopped character entities entirely
904 # when chopping in the middle, distribute $len into left and right part
905 # return early if chopping wouldn't make string shorter
906 if ($where eq 'center') {
907 return $str if ($len + 5 >= length($str)); # filler is length 5
908 $len = int($len/2);
909 } else {
910 return $str if ($len + 4 >= length($str)); # filler is length 4
913 # regexps: ending and beginning with word part up to $add_len
914 my $endre = qr/.{$len}\w{0,$add_len}/;
915 my $begre = qr/\w{0,$add_len}.{$len}/;
917 if ($where eq 'left') {
918 $str =~ m/^(.*?)($begre)$/;
919 my ($lead, $body) = ($1, $2);
920 if (length($lead) > 4) {
921 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
922 $lead = " ...";
924 return "$lead$body";
926 } elsif ($where eq 'center') {
927 $str =~ m/^($endre)(.*)$/;
928 my ($left, $str) = ($1, $2);
929 $str =~ m/^(.*?)($begre)$/;
930 my ($mid, $right) = ($1, $2);
931 if (length($mid) > 5) {
932 $left =~ s/&[^;]*$//;
933 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
934 $mid = " ... ";
936 return "$left$mid$right";
938 } else {
939 $str =~ m/^($endre)(.*)$/;
940 my $body = $1;
941 my $tail = $2;
942 if (length($tail) > 4) {
943 $body =~ s/&[^;]*$//;
944 $tail = "... ";
946 return "$body$tail";
950 # takes the same arguments as chop_str, but also wraps a <span> around the
951 # result with a title attribute if it does get chopped. Additionally, the
952 # string is HTML-escaped.
953 sub chop_and_escape_str {
954 my ($str) = @_;
956 my $chopped = chop_str(@_);
957 if ($chopped eq $str) {
958 return esc_html($chopped);
959 } else {
960 $str =~ s/([[:cntrl:]])/?/g;
961 return $cgi->span({-title=>$str}, esc_html($chopped));
965 ## ----------------------------------------------------------------------
966 ## functions returning short strings
968 # CSS class for given age value (in seconds)
969 sub age_class {
970 my $age = shift;
972 if (!defined $age) {
973 return "noage";
974 } elsif ($age < 60*60*2) {
975 return "age0";
976 } elsif ($age < 60*60*24*2) {
977 return "age1";
978 } else {
979 return "age2";
983 # convert age in seconds to "nn units ago" string
984 sub age_string {
985 my $age = shift;
986 my $age_str;
988 if ($age > 60*60*24*365*2) {
989 $age_str = (int $age/60/60/24/365);
990 $age_str .= " years ago";
991 } elsif ($age > 60*60*24*(365/12)*2) {
992 $age_str = int $age/60/60/24/(365/12);
993 $age_str .= " months ago";
994 } elsif ($age > 60*60*24*7*2) {
995 $age_str = int $age/60/60/24/7;
996 $age_str .= " weeks ago";
997 } elsif ($age > 60*60*24*2) {
998 $age_str = int $age/60/60/24;
999 $age_str .= " days ago";
1000 } elsif ($age > 60*60*2) {
1001 $age_str = int $age/60/60;
1002 $age_str .= " hours ago";
1003 } elsif ($age > 60*2) {
1004 $age_str = int $age/60;
1005 $age_str .= " min ago";
1006 } elsif ($age > 2) {
1007 $age_str = int $age;
1008 $age_str .= " sec ago";
1009 } else {
1010 $age_str .= " right now";
1012 return $age_str;
1015 use constant {
1016 S_IFINVALID => 0030000,
1017 S_IFGITLINK => 0160000,
1020 # submodule/subproject, a commit object reference
1021 sub S_ISGITLINK($) {
1022 my $mode = shift;
1024 return (($mode & S_IFMT) == S_IFGITLINK)
1027 # convert file mode in octal to symbolic file mode string
1028 sub mode_str {
1029 my $mode = oct shift;
1031 if (S_ISGITLINK($mode)) {
1032 return 'm---------';
1033 } elsif (S_ISDIR($mode & S_IFMT)) {
1034 return 'drwxr-xr-x';
1035 } elsif (S_ISLNK($mode)) {
1036 return 'lrwxrwxrwx';
1037 } elsif (S_ISREG($mode)) {
1038 # git cares only about the executable bit
1039 if ($mode & S_IXUSR) {
1040 return '-rwxr-xr-x';
1041 } else {
1042 return '-rw-r--r--';
1044 } else {
1045 return '----------';
1049 # convert file mode in octal to file type string
1050 sub file_type {
1051 my $mode = shift;
1053 if ($mode !~ m/^[0-7]+$/) {
1054 return $mode;
1055 } else {
1056 $mode = oct $mode;
1059 if (S_ISGITLINK($mode)) {
1060 return "submodule";
1061 } elsif (S_ISDIR($mode & S_IFMT)) {
1062 return "directory";
1063 } elsif (S_ISLNK($mode)) {
1064 return "symlink";
1065 } elsif (S_ISREG($mode)) {
1066 return "file";
1067 } else {
1068 return "unknown";
1072 # convert file mode in octal to file type description string
1073 sub file_type_long {
1074 my $mode = shift;
1076 if ($mode !~ m/^[0-7]+$/) {
1077 return $mode;
1078 } else {
1079 $mode = oct $mode;
1082 if (S_ISGITLINK($mode)) {
1083 return "submodule";
1084 } elsif (S_ISDIR($mode & S_IFMT)) {
1085 return "directory";
1086 } elsif (S_ISLNK($mode)) {
1087 return "symlink";
1088 } elsif (S_ISREG($mode)) {
1089 if ($mode & S_IXUSR) {
1090 return "executable";
1091 } else {
1092 return "file";
1094 } else {
1095 return "unknown";
1100 ## ----------------------------------------------------------------------
1101 ## functions returning short HTML fragments, or transforming HTML fragments
1102 ## which don't belong to other sections
1104 # format line of commit message.
1105 sub format_log_line_html {
1106 my $line = shift;
1108 $line = esc_html($line, -nbsp=>1);
1109 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1110 my $hash_text = $1;
1111 my $link =
1112 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1113 -class => "text"}, $hash_text);
1114 $line =~ s/$hash_text/$link/;
1116 return $line;
1119 # format marker of refs pointing to given object
1121 # the destination action is chosen based on object type and current context:
1122 # - for annotated tags, we choose the tag view unless it's the current view
1123 # already, in which case we go to shortlog view
1124 # - for other refs, we keep the current view if we're in history, shortlog or
1125 # log view, and select shortlog otherwise
1126 sub format_ref_marker {
1127 my ($refs, $id) = @_;
1128 my $markers = '';
1130 if (defined $refs->{$id}) {
1131 foreach my $ref (@{$refs->{$id}}) {
1132 # this code exploits the fact that non-lightweight tags are the
1133 # only indirect objects, and that they are the only objects for which
1134 # we want to use tag instead of shortlog as action
1135 my ($type, $name) = qw();
1136 my $indirect = ($ref =~ s/\^\{\}$//);
1137 # e.g. tags/v2.6.11 or heads/next
1138 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1139 $type = $1;
1140 $name = $2;
1141 } else {
1142 $type = "ref";
1143 $name = $ref;
1146 my $class = $type;
1147 $class .= " indirect" if $indirect;
1149 my $dest_action = "shortlog";
1151 if ($indirect) {
1152 $dest_action = "tag" unless $action eq "tag";
1153 } elsif ($action =~ /^(history|(short)?log)$/) {
1154 $dest_action = $action;
1157 my $dest = "";
1158 $dest .= "refs/" unless $ref =~ m!^refs/!;
1159 $dest .= $ref;
1161 my $link = $cgi->a({
1162 -href => href(
1163 action=>$dest_action,
1164 hash=>$dest
1165 )}, $name);
1167 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1168 $link . "</span>";
1172 if ($markers) {
1173 return ' <span class="refs">'. $markers . '</span>';
1174 } else {
1175 return "";
1179 # format, perhaps shortened and with markers, title line
1180 sub format_subject_html {
1181 my ($long, $short, $href, $extra) = @_;
1182 $extra = '' unless defined($extra);
1184 if (length($short) < length($long)) {
1185 return $cgi->a({-href => $href, -class => "list subject",
1186 -title => to_utf8($long)},
1187 esc_html($short) . $extra);
1188 } else {
1189 return $cgi->a({-href => $href, -class => "list subject"},
1190 esc_html($long) . $extra);
1194 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1195 sub format_git_diff_header_line {
1196 my $line = shift;
1197 my $diffinfo = shift;
1198 my ($from, $to) = @_;
1200 if ($diffinfo->{'nparents'}) {
1201 # combined diff
1202 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1203 if ($to->{'href'}) {
1204 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1205 esc_path($to->{'file'}));
1206 } else { # file was deleted (no href)
1207 $line .= esc_path($to->{'file'});
1209 } else {
1210 # "ordinary" diff
1211 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1212 if ($from->{'href'}) {
1213 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1214 'a/' . esc_path($from->{'file'}));
1215 } else { # file was added (no href)
1216 $line .= 'a/' . esc_path($from->{'file'});
1218 $line .= ' ';
1219 if ($to->{'href'}) {
1220 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1221 'b/' . esc_path($to->{'file'}));
1222 } else { # file was deleted
1223 $line .= 'b/' . esc_path($to->{'file'});
1227 return "<div class=\"diff header\">$line</div>\n";
1230 # format extended diff header line, before patch itself
1231 sub format_extended_diff_header_line {
1232 my $line = shift;
1233 my $diffinfo = shift;
1234 my ($from, $to) = @_;
1236 # match <path>
1237 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1238 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1239 esc_path($from->{'file'}));
1241 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1242 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1243 esc_path($to->{'file'}));
1245 # match single <mode>
1246 if ($line =~ m/\s(\d{6})$/) {
1247 $line .= '<span class="info"> (' .
1248 file_type_long($1) .
1249 ')</span>';
1251 # match <hash>
1252 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1253 # can match only for combined diff
1254 $line = 'index ';
1255 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1256 if ($from->{'href'}[$i]) {
1257 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1258 -class=>"hash"},
1259 substr($diffinfo->{'from_id'}[$i],0,7));
1260 } else {
1261 $line .= '0' x 7;
1263 # separator
1264 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1266 $line .= '..';
1267 if ($to->{'href'}) {
1268 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1269 substr($diffinfo->{'to_id'},0,7));
1270 } else {
1271 $line .= '0' x 7;
1274 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1275 # can match only for ordinary diff
1276 my ($from_link, $to_link);
1277 if ($from->{'href'}) {
1278 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1279 substr($diffinfo->{'from_id'},0,7));
1280 } else {
1281 $from_link = '0' x 7;
1283 if ($to->{'href'}) {
1284 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1285 substr($diffinfo->{'to_id'},0,7));
1286 } else {
1287 $to_link = '0' x 7;
1289 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1290 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1293 return $line . "<br/>\n";
1296 # format from-file/to-file diff header
1297 sub format_diff_from_to_header {
1298 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1299 my $line;
1300 my $result = '';
1302 $line = $from_line;
1303 #assert($line =~ m/^---/) if DEBUG;
1304 # no extra formatting for "^--- /dev/null"
1305 if (! $diffinfo->{'nparents'}) {
1306 # ordinary (single parent) diff
1307 if ($line =~ m!^--- "?a/!) {
1308 if ($from->{'href'}) {
1309 $line = '--- a/' .
1310 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1311 esc_path($from->{'file'}));
1312 } else {
1313 $line = '--- a/' .
1314 esc_path($from->{'file'});
1317 $result .= qq!<div class="diff from_file">$line</div>\n!;
1319 } else {
1320 # combined diff (merge commit)
1321 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1322 if ($from->{'href'}[$i]) {
1323 $line = '--- ' .
1324 $cgi->a({-href=>href(action=>"blobdiff",
1325 hash_parent=>$diffinfo->{'from_id'}[$i],
1326 hash_parent_base=>$parents[$i],
1327 file_parent=>$from->{'file'}[$i],
1328 hash=>$diffinfo->{'to_id'},
1329 hash_base=>$hash,
1330 file_name=>$to->{'file'}),
1331 -class=>"path",
1332 -title=>"diff" . ($i+1)},
1333 $i+1) .
1334 '/' .
1335 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1336 esc_path($from->{'file'}[$i]));
1337 } else {
1338 $line = '--- /dev/null';
1340 $result .= qq!<div class="diff from_file">$line</div>\n!;
1344 $line = $to_line;
1345 #assert($line =~ m/^\+\+\+/) if DEBUG;
1346 # no extra formatting for "^+++ /dev/null"
1347 if ($line =~ m!^\+\+\+ "?b/!) {
1348 if ($to->{'href'}) {
1349 $line = '+++ b/' .
1350 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1351 esc_path($to->{'file'}));
1352 } else {
1353 $line = '+++ b/' .
1354 esc_path($to->{'file'});
1357 $result .= qq!<div class="diff to_file">$line</div>\n!;
1359 return $result;
1362 # create note for patch simplified by combined diff
1363 sub format_diff_cc_simplified {
1364 my ($diffinfo, @parents) = @_;
1365 my $result = '';
1367 $result .= "<div class=\"diff header\">" .
1368 "diff --cc ";
1369 if (!is_deleted($diffinfo)) {
1370 $result .= $cgi->a({-href => href(action=>"blob",
1371 hash_base=>$hash,
1372 hash=>$diffinfo->{'to_id'},
1373 file_name=>$diffinfo->{'to_file'}),
1374 -class => "path"},
1375 esc_path($diffinfo->{'to_file'}));
1376 } else {
1377 $result .= esc_path($diffinfo->{'to_file'});
1379 $result .= "</div>\n" . # class="diff header"
1380 "<div class=\"diff nodifferences\">" .
1381 "Simple merge" .
1382 "</div>\n"; # class="diff nodifferences"
1384 return $result;
1387 # format patch (diff) line (not to be used for diff headers)
1388 sub format_diff_line {
1389 my $line = shift;
1390 my ($from, $to) = @_;
1391 my $diff_class = "";
1393 chomp $line;
1395 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1396 # combined diff
1397 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1398 if ($line =~ m/^\@{3}/) {
1399 $diff_class = " chunk_header";
1400 } elsif ($line =~ m/^\\/) {
1401 $diff_class = " incomplete";
1402 } elsif ($prefix =~ tr/+/+/) {
1403 $diff_class = " add";
1404 } elsif ($prefix =~ tr/-/-/) {
1405 $diff_class = " rem";
1407 } else {
1408 # assume ordinary diff
1409 my $char = substr($line, 0, 1);
1410 if ($char eq '+') {
1411 $diff_class = " add";
1412 } elsif ($char eq '-') {
1413 $diff_class = " rem";
1414 } elsif ($char eq '@') {
1415 $diff_class = " chunk_header";
1416 } elsif ($char eq "\\") {
1417 $diff_class = " incomplete";
1420 $line = untabify($line);
1421 if ($from && $to && $line =~ m/^\@{2} /) {
1422 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1423 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1425 $from_lines = 0 unless defined $from_lines;
1426 $to_lines = 0 unless defined $to_lines;
1428 if ($from->{'href'}) {
1429 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1430 -class=>"list"}, $from_text);
1432 if ($to->{'href'}) {
1433 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1434 -class=>"list"}, $to_text);
1436 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1437 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1438 return "<div class=\"diff$diff_class\">$line</div>\n";
1439 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1440 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1441 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1443 @from_text = split(' ', $ranges);
1444 for (my $i = 0; $i < @from_text; ++$i) {
1445 ($from_start[$i], $from_nlines[$i]) =
1446 (split(',', substr($from_text[$i], 1)), 0);
1449 $to_text = pop @from_text;
1450 $to_start = pop @from_start;
1451 $to_nlines = pop @from_nlines;
1453 $line = "<span class=\"chunk_info\">$prefix ";
1454 for (my $i = 0; $i < @from_text; ++$i) {
1455 if ($from->{'href'}[$i]) {
1456 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1457 -class=>"list"}, $from_text[$i]);
1458 } else {
1459 $line .= $from_text[$i];
1461 $line .= " ";
1463 if ($to->{'href'}) {
1464 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1465 -class=>"list"}, $to_text);
1466 } else {
1467 $line .= $to_text;
1469 $line .= " $prefix</span>" .
1470 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1471 return "<div class=\"diff$diff_class\">$line</div>\n";
1473 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1476 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1477 # linked. Pass the hash of the tree/commit to snapshot.
1478 sub format_snapshot_links {
1479 my ($hash) = @_;
1480 my @snapshot_fmts = gitweb_check_feature('snapshot');
1481 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1482 my $num_fmts = @snapshot_fmts;
1483 if ($num_fmts > 1) {
1484 # A parenthesized list of links bearing format names.
1485 # e.g. "snapshot (_tar.gz_ _zip_)"
1486 return "snapshot (" . join(' ', map
1487 $cgi->a({
1488 -href => href(
1489 action=>"snapshot",
1490 hash=>$hash,
1491 snapshot_format=>$_
1493 }, $known_snapshot_formats{$_}{'display'})
1494 , @snapshot_fmts) . ")";
1495 } elsif ($num_fmts == 1) {
1496 # A single "snapshot" link whose tooltip bears the format name.
1497 # i.e. "_snapshot_"
1498 my ($fmt) = @snapshot_fmts;
1499 return
1500 $cgi->a({
1501 -href => href(
1502 action=>"snapshot",
1503 hash=>$hash,
1504 snapshot_format=>$fmt
1506 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1507 }, "snapshot");
1508 } else { # $num_fmts == 0
1509 return undef;
1513 ## ......................................................................
1514 ## functions returning values to be passed, perhaps after some
1515 ## transformation, to other functions; e.g. returning arguments to href()
1517 # returns hash to be passed to href to generate gitweb URL
1518 # in -title key it returns description of link
1519 sub get_feed_info {
1520 my $format = shift || 'Atom';
1521 my %res = (action => lc($format));
1523 # feed links are possible only for project views
1524 return unless (defined $project);
1525 # some views should link to OPML, or to generic project feed,
1526 # or don't have specific feed yet (so they should use generic)
1527 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1529 my $branch;
1530 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1531 # from tag links; this also makes possible to detect branch links
1532 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1533 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1534 $branch = $1;
1536 # find log type for feed description (title)
1537 my $type = 'log';
1538 if (defined $file_name) {
1539 $type = "history of $file_name";
1540 $type .= "/" if ($action eq 'tree');
1541 $type .= " on '$branch'" if (defined $branch);
1542 } else {
1543 $type = "log of $branch" if (defined $branch);
1546 $res{-title} = $type;
1547 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1548 $res{'file_name'} = $file_name;
1550 return %res;
1553 ## ----------------------------------------------------------------------
1554 ## git utility subroutines, invoking git commands
1556 # returns path to the core git executable and the --git-dir parameter as list
1557 sub git_cmd {
1558 return $GIT, '--git-dir='.$git_dir;
1561 # quote the given arguments for passing them to the shell
1562 # quote_command("command", "arg 1", "arg with ' and ! characters")
1563 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1564 # Try to avoid using this function wherever possible.
1565 sub quote_command {
1566 return join(' ',
1567 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1570 # get HEAD ref of given project as hash
1571 sub git_get_head_hash {
1572 my $project = shift;
1573 my $o_git_dir = $git_dir;
1574 my $retval = undef;
1575 $git_dir = "$projectroot/$project";
1576 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1577 my $head = <$fd>;
1578 close $fd;
1579 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1580 $retval = $1;
1583 if (defined $o_git_dir) {
1584 $git_dir = $o_git_dir;
1586 return $retval;
1589 # get type of given object
1590 sub git_get_type {
1591 my $hash = shift;
1593 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1594 my $type = <$fd>;
1595 close $fd or return;
1596 chomp $type;
1597 return $type;
1600 # repository configuration
1601 our $config_file = '';
1602 our %config;
1604 # store multiple values for single key as anonymous array reference
1605 # single values stored directly in the hash, not as [ <value> ]
1606 sub hash_set_multi {
1607 my ($hash, $key, $value) = @_;
1609 if (!exists $hash->{$key}) {
1610 $hash->{$key} = $value;
1611 } elsif (!ref $hash->{$key}) {
1612 $hash->{$key} = [ $hash->{$key}, $value ];
1613 } else {
1614 push @{$hash->{$key}}, $value;
1618 # return hash of git project configuration
1619 # optionally limited to some section, e.g. 'gitweb'
1620 sub git_parse_project_config {
1621 my $section_regexp = shift;
1622 my %config;
1624 local $/ = "\0";
1626 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1627 or return;
1629 while (my $keyval = <$fh>) {
1630 chomp $keyval;
1631 my ($key, $value) = split(/\n/, $keyval, 2);
1633 hash_set_multi(\%config, $key, $value)
1634 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1636 close $fh;
1638 return %config;
1641 # convert config value to boolean, 'true' or 'false'
1642 # no value, number > 0, 'true' and 'yes' values are true
1643 # rest of values are treated as false (never as error)
1644 sub config_to_bool {
1645 my $val = shift;
1647 # strip leading and trailing whitespace
1648 $val =~ s/^\s+//;
1649 $val =~ s/\s+$//;
1651 return (!defined $val || # section.key
1652 ($val =~ /^\d+$/ && $val) || # section.key = 1
1653 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1656 # convert config value to simple decimal number
1657 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1658 # to be multiplied by 1024, 1048576, or 1073741824
1659 sub config_to_int {
1660 my $val = shift;
1662 # strip leading and trailing whitespace
1663 $val =~ s/^\s+//;
1664 $val =~ s/\s+$//;
1666 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1667 $unit = lc($unit);
1668 # unknown unit is treated as 1
1669 return $num * ($unit eq 'g' ? 1073741824 :
1670 $unit eq 'm' ? 1048576 :
1671 $unit eq 'k' ? 1024 : 1);
1673 return $val;
1676 # convert config value to array reference, if needed
1677 sub config_to_multi {
1678 my $val = shift;
1680 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1683 sub git_get_project_config {
1684 my ($key, $type) = @_;
1686 # key sanity check
1687 return unless ($key);
1688 $key =~ s/^gitweb\.//;
1689 return if ($key =~ m/\W/);
1691 # type sanity check
1692 if (defined $type) {
1693 $type =~ s/^--//;
1694 $type = undef
1695 unless ($type eq 'bool' || $type eq 'int');
1698 # get config
1699 if (!defined $config_file ||
1700 $config_file ne "$git_dir/config") {
1701 %config = git_parse_project_config('gitweb');
1702 $config_file = "$git_dir/config";
1705 # ensure given type
1706 if (!defined $type) {
1707 return $config{"gitweb.$key"};
1708 } elsif ($type eq 'bool') {
1709 # backward compatibility: 'git config --bool' returns true/false
1710 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1711 } elsif ($type eq 'int') {
1712 return config_to_int($config{"gitweb.$key"});
1714 return $config{"gitweb.$key"};
1717 # get hash of given path at given ref
1718 sub git_get_hash_by_path {
1719 my $base = shift;
1720 my $path = shift || return undef;
1721 my $type = shift;
1723 $path =~ s,/+$,,;
1725 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1726 or die_error(500, "Open git-ls-tree failed");
1727 my $line = <$fd>;
1728 close $fd or return undef;
1730 if (!defined $line) {
1731 # there is no tree or hash given by $path at $base
1732 return undef;
1735 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1736 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1737 if (defined $type && $type ne $2) {
1738 # type doesn't match
1739 return undef;
1741 return $3;
1744 # get path of entry with given hash at given tree-ish (ref)
1745 # used to get 'from' filename for combined diff (merge commit) for renames
1746 sub git_get_path_by_hash {
1747 my $base = shift || return;
1748 my $hash = shift || return;
1750 local $/ = "\0";
1752 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1753 or return undef;
1754 while (my $line = <$fd>) {
1755 chomp $line;
1757 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1758 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1759 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1760 close $fd;
1761 return $1;
1764 close $fd;
1765 return undef;
1768 ## ......................................................................
1769 ## git utility functions, directly accessing git repository
1771 sub git_get_project_description {
1772 my $path = shift;
1774 $git_dir = "$projectroot/$path";
1775 open my $fd, "$git_dir/description"
1776 or return git_get_project_config('description');
1777 my $descr = <$fd>;
1778 close $fd;
1779 if (defined $descr) {
1780 chomp $descr;
1782 return $descr;
1785 sub git_get_project_url_list {
1786 my $path = shift;
1788 $git_dir = "$projectroot/$path";
1789 open my $fd, "$git_dir/cloneurl"
1790 or return wantarray ?
1791 @{ config_to_multi(git_get_project_config('url')) } :
1792 config_to_multi(git_get_project_config('url'));
1793 my @git_project_url_list = map { chomp; $_ } <$fd>;
1794 close $fd;
1796 return wantarray ? @git_project_url_list : \@git_project_url_list;
1799 sub git_get_projects_list {
1800 my ($filter) = @_;
1801 my @list;
1803 $filter ||= '';
1804 $filter =~ s/\.git$//;
1806 my ($check_forks) = gitweb_check_feature('forks');
1808 if (-d $projects_list) {
1809 # search in directory
1810 my $dir = $projects_list . ($filter ? "/$filter" : '');
1811 # remove the trailing "/"
1812 $dir =~ s!/+$!!;
1813 my $pfxlen = length("$dir");
1814 my $pfxdepth = ($dir =~ tr!/!!);
1816 File::Find::find({
1817 follow_fast => 1, # follow symbolic links
1818 follow_skip => 2, # ignore duplicates
1819 dangling_symlinks => 0, # ignore dangling symlinks, silently
1820 wanted => sub {
1821 # skip project-list toplevel, if we get it.
1822 return if (m!^[/.]$!);
1823 # only directories can be git repositories
1824 return unless (-d $_);
1825 # don't traverse too deep (Find is super slow on os x)
1826 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1827 $File::Find::prune = 1;
1828 return;
1831 my $subdir = substr($File::Find::name, $pfxlen + 1);
1832 # we check related file in $projectroot
1833 if ($check_forks and $subdir =~ m#/.#) {
1834 $File::Find::prune = 1;
1835 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1836 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1837 $File::Find::prune = 1;
1840 }, "$dir");
1842 } elsif (-f $projects_list) {
1843 # read from file(url-encoded):
1844 # 'git%2Fgit.git Linus+Torvalds'
1845 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1846 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1847 my %paths;
1848 open my ($fd), $projects_list or return;
1849 PROJECT:
1850 while (my $line = <$fd>) {
1851 chomp $line;
1852 my ($path, $owner) = split ' ', $line;
1853 $path = unescape($path);
1854 $owner = unescape($owner);
1855 if (!defined $path) {
1856 next;
1858 if ($filter ne '') {
1859 # looking for forks;
1860 my $pfx = substr($path, 0, length($filter));
1861 if ($pfx ne $filter) {
1862 next PROJECT;
1864 my $sfx = substr($path, length($filter));
1865 if ($sfx !~ /^\/.*\.git$/) {
1866 next PROJECT;
1868 } elsif ($check_forks) {
1869 PATH:
1870 foreach my $filter (keys %paths) {
1871 # looking for forks;
1872 my $pfx = substr($path, 0, length($filter));
1873 if ($pfx ne $filter) {
1874 next PATH;
1876 my $sfx = substr($path, length($filter));
1877 if ($sfx !~ /^\/.*\.git$/) {
1878 next PATH;
1880 # is a fork, don't include it in
1881 # the list
1882 next PROJECT;
1885 if (check_export_ok("$projectroot/$path")) {
1886 my $pr = {
1887 path => $path,
1888 owner => to_utf8($owner),
1890 push @list, $pr;
1891 (my $forks_path = $path) =~ s/\.git$//;
1892 $paths{$forks_path}++;
1895 close $fd;
1897 return @list;
1900 our $gitweb_project_owner = undef;
1901 sub git_get_project_list_from_file {
1903 return if (defined $gitweb_project_owner);
1905 $gitweb_project_owner = {};
1906 # read from file (url-encoded):
1907 # 'git%2Fgit.git Linus+Torvalds'
1908 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1909 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1910 if (-f $projects_list) {
1911 open (my $fd , $projects_list);
1912 while (my $line = <$fd>) {
1913 chomp $line;
1914 my ($pr, $ow) = split ' ', $line;
1915 $pr = unescape($pr);
1916 $ow = unescape($ow);
1917 $gitweb_project_owner->{$pr} = to_utf8($ow);
1919 close $fd;
1923 sub git_get_project_owner {
1924 my $project = shift;
1925 my $owner;
1927 return undef unless $project;
1928 $git_dir = "$projectroot/$project";
1930 if (!defined $gitweb_project_owner) {
1931 git_get_project_list_from_file();
1934 if (exists $gitweb_project_owner->{$project}) {
1935 $owner = $gitweb_project_owner->{$project};
1937 if (!defined $owner){
1938 $owner = git_get_project_config('owner');
1940 if (!defined $owner) {
1941 $owner = get_file_owner("$git_dir");
1944 return $owner;
1947 sub git_get_last_activity {
1948 my ($path) = @_;
1949 my $fd;
1951 $git_dir = "$projectroot/$path";
1952 open($fd, "-|", git_cmd(), 'for-each-ref',
1953 '--format=%(committer)',
1954 '--sort=-committerdate',
1955 '--count=1',
1956 'refs/heads') or return;
1957 my $most_recent = <$fd>;
1958 close $fd or return;
1959 if (defined $most_recent &&
1960 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1961 my $timestamp = $1;
1962 my $age = time - $timestamp;
1963 return ($age, age_string($age));
1965 return (undef, undef);
1968 sub git_get_references {
1969 my $type = shift || "";
1970 my %refs;
1971 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1972 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1973 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1974 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1975 or return;
1977 while (my $line = <$fd>) {
1978 chomp $line;
1979 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
1980 if (defined $refs{$1}) {
1981 push @{$refs{$1}}, $2;
1982 } else {
1983 $refs{$1} = [ $2 ];
1987 close $fd or return;
1988 return \%refs;
1991 sub git_get_rev_name_tags {
1992 my $hash = shift || return undef;
1994 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1995 or return;
1996 my $name_rev = <$fd>;
1997 close $fd;
1999 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2000 return $1;
2001 } else {
2002 # catches also '$hash undefined' output
2003 return undef;
2007 ## ----------------------------------------------------------------------
2008 ## parse to hash functions
2010 sub parse_date {
2011 my $epoch = shift;
2012 my $tz = shift || "-0000";
2014 my %date;
2015 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2016 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2017 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2018 $date{'hour'} = $hour;
2019 $date{'minute'} = $min;
2020 $date{'mday'} = $mday;
2021 $date{'day'} = $days[$wday];
2022 $date{'month'} = $months[$mon];
2023 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2024 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2025 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2026 $mday, $months[$mon], $hour ,$min;
2027 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2028 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2030 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2031 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2032 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2033 $date{'hour_local'} = $hour;
2034 $date{'minute_local'} = $min;
2035 $date{'tz_local'} = $tz;
2036 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2037 1900+$year, $mon+1, $mday,
2038 $hour, $min, $sec, $tz);
2039 return %date;
2042 sub parse_tag {
2043 my $tag_id = shift;
2044 my %tag;
2045 my @comment;
2047 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2048 $tag{'id'} = $tag_id;
2049 while (my $line = <$fd>) {
2050 chomp $line;
2051 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2052 $tag{'object'} = $1;
2053 } elsif ($line =~ m/^type (.+)$/) {
2054 $tag{'type'} = $1;
2055 } elsif ($line =~ m/^tag (.+)$/) {
2056 $tag{'name'} = $1;
2057 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2058 $tag{'author'} = $1;
2059 $tag{'epoch'} = $2;
2060 $tag{'tz'} = $3;
2061 } elsif ($line =~ m/--BEGIN/) {
2062 push @comment, $line;
2063 last;
2064 } elsif ($line eq "") {
2065 last;
2068 push @comment, <$fd>;
2069 $tag{'comment'} = \@comment;
2070 close $fd or return;
2071 if (!defined $tag{'name'}) {
2072 return
2074 return %tag
2077 sub parse_commit_text {
2078 my ($commit_text, $withparents) = @_;
2079 my @commit_lines = split '\n', $commit_text;
2080 my %co;
2082 pop @commit_lines; # Remove '\0'
2084 if (! @commit_lines) {
2085 return;
2088 my $header = shift @commit_lines;
2089 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2090 return;
2092 ($co{'id'}, my @parents) = split ' ', $header;
2093 while (my $line = shift @commit_lines) {
2094 last if $line eq "\n";
2095 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2096 $co{'tree'} = $1;
2097 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2098 push @parents, $1;
2099 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2100 $co{'author'} = $1;
2101 $co{'author_epoch'} = $2;
2102 $co{'author_tz'} = $3;
2103 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2104 $co{'author_name'} = $1;
2105 $co{'author_email'} = $2;
2106 } else {
2107 $co{'author_name'} = $co{'author'};
2109 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2110 $co{'committer'} = $1;
2111 $co{'committer_epoch'} = $2;
2112 $co{'committer_tz'} = $3;
2113 $co{'committer_name'} = $co{'committer'};
2114 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2115 $co{'committer_name'} = $1;
2116 $co{'committer_email'} = $2;
2117 } else {
2118 $co{'committer_name'} = $co{'committer'};
2122 if (!defined $co{'tree'}) {
2123 return;
2125 $co{'parents'} = \@parents;
2126 $co{'parent'} = $parents[0];
2128 foreach my $title (@commit_lines) {
2129 $title =~ s/^ //;
2130 if ($title ne "") {
2131 $co{'title'} = chop_str($title, 80, 5);
2132 # remove leading stuff of merges to make the interesting part visible
2133 if (length($title) > 50) {
2134 $title =~ s/^Automatic //;
2135 $title =~ s/^merge (of|with) /Merge ... /i;
2136 if (length($title) > 50) {
2137 $title =~ s/(http|rsync):\/\///;
2139 if (length($title) > 50) {
2140 $title =~ s/(master|www|rsync)\.//;
2142 if (length($title) > 50) {
2143 $title =~ s/kernel.org:?//;
2145 if (length($title) > 50) {
2146 $title =~ s/\/pub\/scm//;
2149 $co{'title_short'} = chop_str($title, 50, 5);
2150 last;
2153 if (! defined $co{'title'} || $co{'title'} eq "") {
2154 $co{'title'} = $co{'title_short'} = '(no commit message)';
2156 # remove added spaces
2157 foreach my $line (@commit_lines) {
2158 $line =~ s/^ //;
2160 $co{'comment'} = \@commit_lines;
2162 my $age = time - $co{'committer_epoch'};
2163 $co{'age'} = $age;
2164 $co{'age_string'} = age_string($age);
2165 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2166 if ($age > 60*60*24*7*2) {
2167 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2168 $co{'age_string_age'} = $co{'age_string'};
2169 } else {
2170 $co{'age_string_date'} = $co{'age_string'};
2171 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2173 return %co;
2176 sub parse_commit {
2177 my ($commit_id) = @_;
2178 my %co;
2180 local $/ = "\0";
2182 open my $fd, "-|", git_cmd(), "rev-list",
2183 "--parents",
2184 "--header",
2185 "--max-count=1",
2186 $commit_id,
2187 "--",
2188 or die_error(500, "Open git-rev-list failed");
2189 %co = parse_commit_text(<$fd>, 1);
2190 close $fd;
2192 return %co;
2195 sub parse_commits {
2196 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2197 my @cos;
2199 $maxcount ||= 1;
2200 $skip ||= 0;
2202 local $/ = "\0";
2204 open my $fd, "-|", git_cmd(), "rev-list",
2205 "--header",
2206 @args,
2207 ("--max-count=" . $maxcount),
2208 ("--skip=" . $skip),
2209 @extra_options,
2210 $commit_id,
2211 "--",
2212 ($filename ? ($filename) : ())
2213 or die_error(500, "Open git-rev-list failed");
2214 while (my $line = <$fd>) {
2215 my %co = parse_commit_text($line);
2216 push @cos, \%co;
2218 close $fd;
2220 return wantarray ? @cos : \@cos;
2223 # parse line of git-diff-tree "raw" output
2224 sub parse_difftree_raw_line {
2225 my $line = shift;
2226 my %res;
2228 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2229 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2230 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2231 $res{'from_mode'} = $1;
2232 $res{'to_mode'} = $2;
2233 $res{'from_id'} = $3;
2234 $res{'to_id'} = $4;
2235 $res{'status'} = $5;
2236 $res{'similarity'} = $6;
2237 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2238 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2239 } else {
2240 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2243 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2244 # combined diff (for merge commit)
2245 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2246 $res{'nparents'} = length($1);
2247 $res{'from_mode'} = [ split(' ', $2) ];
2248 $res{'to_mode'} = pop @{$res{'from_mode'}};
2249 $res{'from_id'} = [ split(' ', $3) ];
2250 $res{'to_id'} = pop @{$res{'from_id'}};
2251 $res{'status'} = [ split('', $4) ];
2252 $res{'to_file'} = unquote($5);
2254 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2255 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2256 $res{'commit'} = $1;
2259 return wantarray ? %res : \%res;
2262 # wrapper: return parsed line of git-diff-tree "raw" output
2263 # (the argument might be raw line, or parsed info)
2264 sub parsed_difftree_line {
2265 my $line_or_ref = shift;
2267 if (ref($line_or_ref) eq "HASH") {
2268 # pre-parsed (or generated by hand)
2269 return $line_or_ref;
2270 } else {
2271 return parse_difftree_raw_line($line_or_ref);
2275 # parse line of git-ls-tree output
2276 sub parse_ls_tree_line ($;%) {
2277 my $line = shift;
2278 my %opts = @_;
2279 my %res;
2281 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2282 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2284 $res{'mode'} = $1;
2285 $res{'type'} = $2;
2286 $res{'hash'} = $3;
2287 if ($opts{'-z'}) {
2288 $res{'name'} = $4;
2289 } else {
2290 $res{'name'} = unquote($4);
2293 return wantarray ? %res : \%res;
2296 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2297 sub parse_from_to_diffinfo {
2298 my ($diffinfo, $from, $to, @parents) = @_;
2300 if ($diffinfo->{'nparents'}) {
2301 # combined diff
2302 $from->{'file'} = [];
2303 $from->{'href'} = [];
2304 fill_from_file_info($diffinfo, @parents)
2305 unless exists $diffinfo->{'from_file'};
2306 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2307 $from->{'file'}[$i] =
2308 defined $diffinfo->{'from_file'}[$i] ?
2309 $diffinfo->{'from_file'}[$i] :
2310 $diffinfo->{'to_file'};
2311 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2312 $from->{'href'}[$i] = href(action=>"blob",
2313 hash_base=>$parents[$i],
2314 hash=>$diffinfo->{'from_id'}[$i],
2315 file_name=>$from->{'file'}[$i]);
2316 } else {
2317 $from->{'href'}[$i] = undef;
2320 } else {
2321 # ordinary (not combined) diff
2322 $from->{'file'} = $diffinfo->{'from_file'};
2323 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2324 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2325 hash=>$diffinfo->{'from_id'},
2326 file_name=>$from->{'file'});
2327 } else {
2328 delete $from->{'href'};
2332 $to->{'file'} = $diffinfo->{'to_file'};
2333 if (!is_deleted($diffinfo)) { # file exists in result
2334 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2335 hash=>$diffinfo->{'to_id'},
2336 file_name=>$to->{'file'});
2337 } else {
2338 delete $to->{'href'};
2342 ## ......................................................................
2343 ## parse to array of hashes functions
2345 sub git_get_heads_list {
2346 my $limit = shift;
2347 my @headslist;
2349 open my $fd, '-|', git_cmd(), 'for-each-ref',
2350 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2351 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2352 'refs/heads'
2353 or return;
2354 while (my $line = <$fd>) {
2355 my %ref_item;
2357 chomp $line;
2358 my ($refinfo, $committerinfo) = split(/\0/, $line);
2359 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2360 my ($committer, $epoch, $tz) =
2361 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2362 $ref_item{'fullname'} = $name;
2363 $name =~ s!^refs/heads/!!;
2365 $ref_item{'name'} = $name;
2366 $ref_item{'id'} = $hash;
2367 $ref_item{'title'} = $title || '(no commit message)';
2368 $ref_item{'epoch'} = $epoch;
2369 if ($epoch) {
2370 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2371 } else {
2372 $ref_item{'age'} = "unknown";
2375 push @headslist, \%ref_item;
2377 close $fd;
2379 return wantarray ? @headslist : \@headslist;
2382 sub git_get_tags_list {
2383 my $limit = shift;
2384 my @tagslist;
2386 open my $fd, '-|', git_cmd(), 'for-each-ref',
2387 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2388 '--format=%(objectname) %(objecttype) %(refname) '.
2389 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2390 'refs/tags'
2391 or return;
2392 while (my $line = <$fd>) {
2393 my %ref_item;
2395 chomp $line;
2396 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2397 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2398 my ($creator, $epoch, $tz) =
2399 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2400 $ref_item{'fullname'} = $name;
2401 $name =~ s!^refs/tags/!!;
2403 $ref_item{'type'} = $type;
2404 $ref_item{'id'} = $id;
2405 $ref_item{'name'} = $name;
2406 if ($type eq "tag") {
2407 $ref_item{'subject'} = $title;
2408 $ref_item{'reftype'} = $reftype;
2409 $ref_item{'refid'} = $refid;
2410 } else {
2411 $ref_item{'reftype'} = $type;
2412 $ref_item{'refid'} = $id;
2415 if ($type eq "tag" || $type eq "commit") {
2416 $ref_item{'epoch'} = $epoch;
2417 if ($epoch) {
2418 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2419 } else {
2420 $ref_item{'age'} = "unknown";
2424 push @tagslist, \%ref_item;
2426 close $fd;
2428 return wantarray ? @tagslist : \@tagslist;
2431 ## ----------------------------------------------------------------------
2432 ## filesystem-related functions
2434 sub get_file_owner {
2435 my $path = shift;
2437 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2438 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2439 if (!defined $gcos) {
2440 return undef;
2442 my $owner = $gcos;
2443 $owner =~ s/[,;].*$//;
2444 return to_utf8($owner);
2447 ## ......................................................................
2448 ## mimetype related functions
2450 sub mimetype_guess_file {
2451 my $filename = shift;
2452 my $mimemap = shift;
2453 -r $mimemap or return undef;
2455 my %mimemap;
2456 open(MIME, $mimemap) or return undef;
2457 while (<MIME>) {
2458 next if m/^#/; # skip comments
2459 my ($mime, $exts) = split(/\t+/);
2460 if (defined $exts) {
2461 my @exts = split(/\s+/, $exts);
2462 foreach my $ext (@exts) {
2463 $mimemap{$ext} = $mime;
2467 close(MIME);
2469 $filename =~ /\.([^.]*)$/;
2470 return $mimemap{$1};
2473 sub mimetype_guess {
2474 my $filename = shift;
2475 my $mime;
2476 $filename =~ /\./ or return undef;
2478 if ($mimetypes_file) {
2479 my $file = $mimetypes_file;
2480 if ($file !~ m!^/!) { # if it is relative path
2481 # it is relative to project
2482 $file = "$projectroot/$project/$file";
2484 $mime = mimetype_guess_file($filename, $file);
2486 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2487 return $mime;
2490 sub blob_mimetype {
2491 my $fd = shift;
2492 my $filename = shift;
2494 if ($filename) {
2495 my $mime = mimetype_guess($filename);
2496 $mime and return $mime;
2499 # just in case
2500 return $default_blob_plain_mimetype unless $fd;
2502 if (-T $fd) {
2503 return 'text/plain';
2504 } elsif (! $filename) {
2505 return 'application/octet-stream';
2506 } elsif ($filename =~ m/\.png$/i) {
2507 return 'image/png';
2508 } elsif ($filename =~ m/\.gif$/i) {
2509 return 'image/gif';
2510 } elsif ($filename =~ m/\.jpe?g$/i) {
2511 return 'image/jpeg';
2512 } else {
2513 return 'application/octet-stream';
2517 sub blob_contenttype {
2518 my ($fd, $file_name, $type) = @_;
2520 $type ||= blob_mimetype($fd, $file_name);
2521 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2522 $type .= "; charset=$default_text_plain_charset";
2525 return $type;
2528 ## ======================================================================
2529 ## functions printing HTML: header, footer, error page
2531 sub git_header_html {
2532 my $status = shift || "200 OK";
2533 my $expires = shift;
2535 my $title = "$site_name";
2536 if (defined $project) {
2537 $title .= " - " . to_utf8($project);
2538 if (defined $action) {
2539 $title .= "/$action";
2540 if (defined $file_name) {
2541 $title .= " - " . esc_path($file_name);
2542 if ($action eq "tree" && $file_name !~ m|/$|) {
2543 $title .= "/";
2548 my $content_type;
2549 # require explicit support from the UA if we are to send the page as
2550 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2551 # we have to do this because MSIE sometimes globs '*/*', pretending to
2552 # support xhtml+xml but choking when it gets what it asked for.
2553 if (defined $cgi->http('HTTP_ACCEPT') &&
2554 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2555 $cgi->Accept('application/xhtml+xml') != 0) {
2556 $content_type = 'application/xhtml+xml';
2557 } else {
2558 $content_type = 'text/html';
2560 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2561 -status=> $status, -expires => $expires);
2562 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2563 print <<EOF;
2564 <?xml version="1.0" encoding="utf-8"?>
2565 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2566 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2567 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2568 <!-- git core binaries version $git_version -->
2569 <head>
2570 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2571 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2572 <meta name="robots" content="index, nofollow"/>
2573 <title>$title</title>
2575 # print out each stylesheet that exist
2576 if (defined $stylesheet) {
2577 #provides backwards capability for those people who define style sheet in a config file
2578 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2579 } else {
2580 foreach my $stylesheet (@stylesheets) {
2581 next unless $stylesheet;
2582 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2585 if (defined $project) {
2586 my %href_params = get_feed_info();
2587 if (!exists $href_params{'-title'}) {
2588 $href_params{'-title'} = 'log';
2591 foreach my $format qw(RSS Atom) {
2592 my $type = lc($format);
2593 my %link_attr = (
2594 '-rel' => 'alternate',
2595 '-title' => "$project - $href_params{'-title'} - $format feed",
2596 '-type' => "application/$type+xml"
2599 $href_params{'action'} = $type;
2600 $link_attr{'-href'} = href(%href_params);
2601 print "<link ".
2602 "rel=\"$link_attr{'-rel'}\" ".
2603 "title=\"$link_attr{'-title'}\" ".
2604 "href=\"$link_attr{'-href'}\" ".
2605 "type=\"$link_attr{'-type'}\" ".
2606 "/>\n";
2608 $href_params{'extra_options'} = '--no-merges';
2609 $link_attr{'-href'} = href(%href_params);
2610 $link_attr{'-title'} .= ' (no merges)';
2611 print "<link ".
2612 "rel=\"$link_attr{'-rel'}\" ".
2613 "title=\"$link_attr{'-title'}\" ".
2614 "href=\"$link_attr{'-href'}\" ".
2615 "type=\"$link_attr{'-type'}\" ".
2616 "/>\n";
2619 } else {
2620 printf('<link rel="alternate" title="%s projects list" '.
2621 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2622 $site_name, href(project=>undef, action=>"project_index"));
2623 printf('<link rel="alternate" title="%s projects feeds" '.
2624 'href="%s" type="text/x-opml" />'."\n",
2625 $site_name, href(project=>undef, action=>"opml"));
2627 if (defined $favicon) {
2628 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2631 print "</head>\n" .
2632 "<body>\n";
2634 if (-f $site_header) {
2635 open (my $fd, $site_header);
2636 print <$fd>;
2637 close $fd;
2640 print "<div class=\"page_header\">\n" .
2641 $cgi->a({-href => esc_url($logo_url),
2642 -title => $logo_label},
2643 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2644 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2645 if (defined $project) {
2646 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2647 if (defined $action) {
2648 print " / $action";
2650 print "\n";
2652 print "</div>\n";
2654 my ($have_search) = gitweb_check_feature('search');
2655 if (defined $project && $have_search) {
2656 if (!defined $searchtext) {
2657 $searchtext = "";
2659 my $search_hash;
2660 if (defined $hash_base) {
2661 $search_hash = $hash_base;
2662 } elsif (defined $hash) {
2663 $search_hash = $hash;
2664 } else {
2665 $search_hash = "HEAD";
2667 my $action = $my_uri;
2668 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2669 if ($use_pathinfo) {
2670 $action .= "/".esc_url($project);
2672 print $cgi->startform(-method => "get", -action => $action) .
2673 "<div class=\"search\">\n" .
2674 (!$use_pathinfo &&
2675 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2676 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2677 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2678 $cgi->popup_menu(-name => 'st', -default => 'commit',
2679 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2680 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2681 " search:\n",
2682 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2683 "<span title=\"Extended regular expression\">" .
2684 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
2685 -checked => $search_use_regexp) .
2686 "</span>" .
2687 "</div>" .
2688 $cgi->end_form() . "\n";
2692 sub git_footer_html {
2693 my $feed_class = 'rss_logo';
2695 print "<div class=\"page_footer\">\n";
2696 if (defined $project) {
2697 my $descr = git_get_project_description($project);
2698 if (defined $descr) {
2699 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2702 my %href_params = get_feed_info();
2703 if (!%href_params) {
2704 $feed_class .= ' generic';
2706 $href_params{'-title'} ||= 'log';
2708 foreach my $format qw(RSS Atom) {
2709 $href_params{'action'} = lc($format);
2710 print $cgi->a({-href => href(%href_params),
2711 -title => "$href_params{'-title'} $format feed",
2712 -class => $feed_class}, $format)."\n";
2715 } else {
2716 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2717 -class => $feed_class}, "OPML") . " ";
2718 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2719 -class => $feed_class}, "TXT") . "\n";
2721 print "</div>\n"; # class="page_footer"
2723 if (-f $site_footer) {
2724 open (my $fd, $site_footer);
2725 print <$fd>;
2726 close $fd;
2729 print "</body>\n" .
2730 "</html>";
2733 # die_error(<http_status_code>, <error_message>)
2734 # Example: die_error(404, 'Hash not found')
2735 # By convention, use the following status codes (as defined in RFC 2616):
2736 # 400: Invalid or missing CGI parameters, or
2737 # requested object exists but has wrong type.
2738 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
2739 # this server or project.
2740 # 404: Requested object/revision/project doesn't exist.
2741 # 500: The server isn't configured properly, or
2742 # an internal error occurred (e.g. failed assertions caused by bugs), or
2743 # an unknown error occurred (e.g. the git binary died unexpectedly).
2744 sub die_error {
2745 my $status = shift || 500;
2746 my $error = shift || "Internal server error";
2748 my %http_responses = (400 => '400 Bad Request',
2749 403 => '403 Forbidden',
2750 404 => '404 Not Found',
2751 500 => '500 Internal Server Error');
2752 git_header_html($http_responses{$status});
2753 print <<EOF;
2754 <div class="page_body">
2755 <br /><br />
2756 $status - $error
2757 <br />
2758 </div>
2760 git_footer_html();
2761 exit;
2764 ## ----------------------------------------------------------------------
2765 ## functions printing or outputting HTML: navigation
2767 sub git_print_page_nav {
2768 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2769 $extra = '' if !defined $extra; # pager or formats
2771 my @navs = qw(summary log commit commitdiff tree);
2772 if ($suppress) {
2773 @navs = grep { $_ ne $suppress } @navs;
2776 my %arg = map { $_ => {action=>$_} } @navs;
2777 if (defined $head) {
2778 for (qw(commit commitdiff)) {
2779 $arg{$_}{'hash'} = $head;
2781 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2782 $arg{'log'}{'hash'} = $head;
2786 $arg{'log'}{'action'} = 'shortlog';
2787 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2788 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2790 my @actions = gitweb_check_feature('actions');
2791 while (@actions) {
2792 my ($label, $link, $pos) = (shift(@actions), shift(@actions), shift(@actions));
2793 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
2794 # munch munch
2795 $link =~ s#%n#$project#g;
2796 $link =~ s#%f#$git_dir#g;
2797 $treehead ? $link =~ s#%h#$treehead#g : $link =~ s#%h##g;
2798 $treebase ? $link =~ s#%b#$treebase#g : $link =~ s#%b##g;
2799 $arg{$label}{'_href'} = $link;
2802 print "<div class=\"page_nav\">\n" .
2803 (join " | ",
2804 map { $_ eq $current ?
2805 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
2806 } @navs);
2807 print "<br/>\n$extra<br/>\n" .
2808 "</div>\n";
2811 sub format_paging_nav {
2812 my ($action, $hash, $head, $page, $has_next_link) = @_;
2813 my $paging_nav;
2816 if ($hash ne $head || $page) {
2817 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2818 } else {
2819 $paging_nav .= "HEAD";
2822 if ($page > 0) {
2823 $paging_nav .= " &sdot; " .
2824 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2825 -accesskey => "p", -title => "Alt-p"}, "prev");
2826 } else {
2827 $paging_nav .= " &sdot; prev";
2830 if ($has_next_link) {
2831 $paging_nav .= " &sdot; " .
2832 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2833 -accesskey => "n", -title => "Alt-n"}, "next");
2834 } else {
2835 $paging_nav .= " &sdot; next";
2838 return $paging_nav;
2841 sub format_log_nav {
2842 my ($action, $hash, $head, $page, $has_next_link) = @_;
2843 my $paging_nav;
2845 if ($action eq 'shortlog') {
2846 $paging_nav .= 'shortlog';
2847 } else {
2848 $paging_nav .= $cgi->a({-href => href(action=>'shortlog', -replay=>1)}, 'shortlog');
2850 $paging_nav .= ' | ';
2851 if ($action eq 'log') {
2852 $paging_nav .= 'fulllog';
2853 } else {
2854 $paging_nav .= $cgi->a({-href => href(action=>'log', -replay=>1)}, 'fulllog');
2857 $paging_nav .= " | " . format_paging_nav($action, $hash, $head, $page, $has_next_link);
2858 return $paging_nav;
2861 ## ......................................................................
2862 ## functions printing or outputting HTML: div
2864 sub git_print_header_div {
2865 my ($action, $title, $hash, $hash_base) = @_;
2866 my %args = ();
2868 $args{'action'} = $action;
2869 $args{'hash'} = $hash if $hash;
2870 $args{'hash_base'} = $hash_base if $hash_base;
2872 print "<div class=\"header\">\n" .
2873 $cgi->a({-href => href(%args), -class => "title"},
2874 $title ? $title : $action) .
2875 "\n</div>\n";
2878 #sub git_print_authorship (\%) {
2879 sub git_print_authorship {
2880 my $co = shift;
2882 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2883 print "<div class=\"author_date\">" .
2884 esc_html($co->{'author_name'}) .
2885 " [$ad{'rfc2822'}";
2886 if ($ad{'hour_local'} < 6) {
2887 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2888 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2889 } else {
2890 printf(" (%02d:%02d %s)",
2891 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2893 print "]</div>\n";
2896 sub git_print_page_path {
2897 my $name = shift;
2898 my $type = shift;
2899 my $hb = shift;
2902 print "<div class=\"page_path\">";
2903 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2904 -title => 'tree root'}, to_utf8("[$project]"));
2905 print " / ";
2906 if (defined $name) {
2907 my @dirname = split '/', $name;
2908 my $basename = pop @dirname;
2909 my $fullname = '';
2911 foreach my $dir (@dirname) {
2912 $fullname .= ($fullname ? '/' : '') . $dir;
2913 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2914 hash_base=>$hb),
2915 -title => $fullname}, esc_path($dir));
2916 print " / ";
2918 if (defined $type && $type eq 'blob') {
2919 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2920 hash_base=>$hb),
2921 -title => $name}, esc_path($basename));
2922 } elsif (defined $type && $type eq 'tree') {
2923 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2924 hash_base=>$hb),
2925 -title => $name}, esc_path($basename));
2926 print " / ";
2927 } else {
2928 print esc_path($basename);
2931 print "<br/></div>\n";
2934 # sub git_print_log (\@;%) {
2935 sub git_print_log ($;%) {
2936 my $log = shift;
2937 my %opts = @_;
2939 if ($opts{'-remove_title'}) {
2940 # remove title, i.e. first line of log
2941 shift @$log;
2943 # remove leading empty lines
2944 while (defined $log->[0] && $log->[0] eq "") {
2945 shift @$log;
2948 # print log
2949 my $signoff = 0;
2950 my $empty = 0;
2951 foreach my $line (@$log) {
2952 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2953 $signoff = 1;
2954 $empty = 0;
2955 if (! $opts{'-remove_signoff'}) {
2956 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2957 next;
2958 } else {
2959 # remove signoff lines
2960 next;
2962 } else {
2963 $signoff = 0;
2966 # print only one empty line
2967 # do not print empty line after signoff
2968 if ($line eq "") {
2969 next if ($empty || $signoff);
2970 $empty = 1;
2971 } else {
2972 $empty = 0;
2975 print format_log_line_html($line) . "<br/>\n";
2978 if ($opts{'-final_empty_line'}) {
2979 # end with single empty line
2980 print "<br/>\n" unless $empty;
2984 # return link target (what link points to)
2985 sub git_get_link_target {
2986 my $hash = shift;
2987 my $link_target;
2989 # read link
2990 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2991 or return;
2993 local $/;
2994 $link_target = <$fd>;
2996 close $fd
2997 or return;
2999 return $link_target;
3002 # given link target, and the directory (basedir) the link is in,
3003 # return target of link relative to top directory (top tree);
3004 # return undef if it is not possible (including absolute links).
3005 sub normalize_link_target {
3006 my ($link_target, $basedir, $hash_base) = @_;
3008 # we can normalize symlink target only if $hash_base is provided
3009 return unless $hash_base;
3011 # absolute symlinks (beginning with '/') cannot be normalized
3012 return if (substr($link_target, 0, 1) eq '/');
3014 # normalize link target to path from top (root) tree (dir)
3015 my $path;
3016 if ($basedir) {
3017 $path = $basedir . '/' . $link_target;
3018 } else {
3019 # we are in top (root) tree (dir)
3020 $path = $link_target;
3023 # remove //, /./, and /../
3024 my @path_parts;
3025 foreach my $part (split('/', $path)) {
3026 # discard '.' and ''
3027 next if (!$part || $part eq '.');
3028 # handle '..'
3029 if ($part eq '..') {
3030 if (@path_parts) {
3031 pop @path_parts;
3032 } else {
3033 # link leads outside repository (outside top dir)
3034 return;
3036 } else {
3037 push @path_parts, $part;
3040 $path = join('/', @path_parts);
3042 return $path;
3045 # print tree entry (row of git_tree), but without encompassing <tr> element
3046 sub git_print_tree_entry {
3047 my ($t, $basedir, $hash_base, $have_blame) = @_;
3049 my %base_key = ();
3050 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3052 # The format of a table row is: mode list link. Where mode is
3053 # the mode of the entry, list is the name of the entry, an href,
3054 # and link is the action links of the entry.
3056 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3057 if ($t->{'type'} eq "blob") {
3058 print "<td class=\"list\">" .
3059 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3060 file_name=>"$basedir$t->{'name'}", %base_key),
3061 -class => "list"}, esc_path($t->{'name'}));
3062 if (S_ISLNK(oct $t->{'mode'})) {
3063 my $link_target = git_get_link_target($t->{'hash'});
3064 if ($link_target) {
3065 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3066 if (defined $norm_target) {
3067 print " -> " .
3068 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3069 file_name=>$norm_target),
3070 -title => $norm_target}, esc_path($link_target));
3071 } else {
3072 print " -> " . esc_path($link_target);
3076 print "</td>\n";
3077 print "<td class=\"link\">";
3078 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3079 file_name=>"$basedir$t->{'name'}", %base_key)},
3080 "blob");
3081 if ($have_blame) {
3082 print " | " .
3083 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3084 file_name=>"$basedir$t->{'name'}", %base_key)},
3085 "blame");
3087 if (defined $hash_base) {
3088 print " | " .
3089 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3090 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3091 "history");
3093 print " | " .
3094 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3095 file_name=>"$basedir$t->{'name'}")},
3096 "raw");
3097 print "</td>\n";
3099 } elsif ($t->{'type'} eq "tree") {
3100 print "<td class=\"list\">";
3101 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3102 file_name=>"$basedir$t->{'name'}", %base_key)},
3103 esc_path($t->{'name'}));
3104 print "</td>\n";
3105 print "<td class=\"link\">";
3106 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3107 file_name=>"$basedir$t->{'name'}", %base_key)},
3108 "tree");
3109 if (defined $hash_base) {
3110 print " | " .
3111 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3112 file_name=>"$basedir$t->{'name'}")},
3113 "history");
3115 print "</td>\n";
3116 } else {
3117 # unknown object: we can only present history for it
3118 # (this includes 'commit' object, i.e. submodule support)
3119 print "<td class=\"list\">" .
3120 esc_path($t->{'name'}) .
3121 "</td>\n";
3122 print "<td class=\"link\">";
3123 if (defined $hash_base) {
3124 print $cgi->a({-href => href(action=>"history",
3125 hash_base=>$hash_base,
3126 file_name=>"$basedir$t->{'name'}")},
3127 "history");
3129 print "</td>\n";
3133 ## ......................................................................
3134 ## functions printing large fragments of HTML
3136 # get pre-image filenames for merge (combined) diff
3137 sub fill_from_file_info {
3138 my ($diff, @parents) = @_;
3140 $diff->{'from_file'} = [ ];
3141 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3142 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3143 if ($diff->{'status'}[$i] eq 'R' ||
3144 $diff->{'status'}[$i] eq 'C') {
3145 $diff->{'from_file'}[$i] =
3146 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3150 return $diff;
3153 # is current raw difftree line of file deletion
3154 sub is_deleted {
3155 my $diffinfo = shift;
3157 return $diffinfo->{'to_id'} eq ('0' x 40);
3160 # does patch correspond to [previous] difftree raw line
3161 # $diffinfo - hashref of parsed raw diff format
3162 # $patchinfo - hashref of parsed patch diff format
3163 # (the same keys as in $diffinfo)
3164 sub is_patch_split {
3165 my ($diffinfo, $patchinfo) = @_;
3167 return defined $diffinfo && defined $patchinfo
3168 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3172 sub git_difftree_body {
3173 my ($difftree, $hash, @parents) = @_;
3174 my ($parent) = $parents[0];
3175 my ($have_blame) = gitweb_check_feature('blame');
3176 print "<div class=\"list_head\">\n";
3177 if ($#{$difftree} > 10) {
3178 print(($#{$difftree} + 1) . " files changed:\n");
3180 print "</div>\n";
3182 print "<table class=\"" .
3183 (@parents > 1 ? "combined " : "") .
3184 "diff_tree\">\n";
3186 # header only for combined diff in 'commitdiff' view
3187 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3188 if ($has_header) {
3189 # table header
3190 print "<thead><tr>\n" .
3191 "<th></th><th></th>\n"; # filename, patchN link
3192 for (my $i = 0; $i < @parents; $i++) {
3193 my $par = $parents[$i];
3194 print "<th>" .
3195 $cgi->a({-href => href(action=>"commitdiff",
3196 hash=>$hash, hash_parent=>$par),
3197 -title => 'commitdiff to parent number ' .
3198 ($i+1) . ': ' . substr($par,0,7)},
3199 $i+1) .
3200 "&nbsp;</th>\n";
3202 print "</tr></thead>\n<tbody>\n";
3205 my $alternate = 1;
3206 my $patchno = 0;
3207 foreach my $line (@{$difftree}) {
3208 my $diff = parsed_difftree_line($line);
3210 if ($alternate) {
3211 print "<tr class=\"dark\">\n";
3212 } else {
3213 print "<tr class=\"light\">\n";
3215 $alternate ^= 1;
3217 if (exists $diff->{'nparents'}) { # combined diff
3219 fill_from_file_info($diff, @parents)
3220 unless exists $diff->{'from_file'};
3222 if (!is_deleted($diff)) {
3223 # file exists in the result (child) commit
3224 print "<td>" .
3225 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3226 file_name=>$diff->{'to_file'},
3227 hash_base=>$hash),
3228 -class => "list"}, esc_path($diff->{'to_file'})) .
3229 "</td>\n";
3230 } else {
3231 print "<td>" .
3232 esc_path($diff->{'to_file'}) .
3233 "</td>\n";
3236 if ($action eq 'commitdiff') {
3237 # link to patch
3238 $patchno++;
3239 print "<td class=\"link\">" .
3240 $cgi->a({-href => "#patch$patchno"}, "patch") .
3241 " | " .
3242 "</td>\n";
3245 my $has_history = 0;
3246 my $not_deleted = 0;
3247 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3248 my $hash_parent = $parents[$i];
3249 my $from_hash = $diff->{'from_id'}[$i];
3250 my $from_path = $diff->{'from_file'}[$i];
3251 my $status = $diff->{'status'}[$i];
3253 $has_history ||= ($status ne 'A');
3254 $not_deleted ||= ($status ne 'D');
3256 if ($status eq 'A') {
3257 print "<td class=\"link\" align=\"right\"> | </td>\n";
3258 } elsif ($status eq 'D') {
3259 print "<td class=\"link\">" .
3260 $cgi->a({-href => href(action=>"blob",
3261 hash_base=>$hash,
3262 hash=>$from_hash,
3263 file_name=>$from_path)},
3264 "blob" . ($i+1)) .
3265 " | </td>\n";
3266 } else {
3267 if ($diff->{'to_id'} eq $from_hash) {
3268 print "<td class=\"link nochange\">";
3269 } else {
3270 print "<td class=\"link\">";
3272 print $cgi->a({-href => href(action=>"blobdiff",
3273 hash=>$diff->{'to_id'},
3274 hash_parent=>$from_hash,
3275 hash_base=>$hash,
3276 hash_parent_base=>$hash_parent,
3277 file_name=>$diff->{'to_file'},
3278 file_parent=>$from_path)},
3279 "diff" . ($i+1)) .
3280 " | </td>\n";
3284 print "<td class=\"link\">";
3285 if ($not_deleted) {
3286 print $cgi->a({-href => href(action=>"blob",
3287 hash=>$diff->{'to_id'},
3288 file_name=>$diff->{'to_file'},
3289 hash_base=>$hash)},
3290 "blob");
3291 print " | " if ($has_history);
3293 if ($has_history) {
3294 print $cgi->a({-href => href(action=>"history",
3295 file_name=>$diff->{'to_file'},
3296 hash_base=>$hash)},
3297 "history");
3299 print "</td>\n";
3301 print "</tr>\n";
3302 next; # instead of 'else' clause, to avoid extra indent
3304 # else ordinary diff
3306 my ($to_mode_oct, $to_mode_str, $to_file_type);
3307 my ($from_mode_oct, $from_mode_str, $from_file_type);
3308 if ($diff->{'to_mode'} ne ('0' x 6)) {
3309 $to_mode_oct = oct $diff->{'to_mode'};
3310 if (S_ISREG($to_mode_oct)) { # only for regular file
3311 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3313 $to_file_type = file_type($diff->{'to_mode'});
3315 if ($diff->{'from_mode'} ne ('0' x 6)) {
3316 $from_mode_oct = oct $diff->{'from_mode'};
3317 if (S_ISREG($to_mode_oct)) { # only for regular file
3318 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3320 $from_file_type = file_type($diff->{'from_mode'});
3323 if ($diff->{'status'} eq "A") { # created
3324 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3325 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3326 $mode_chng .= "]</span>";
3327 print "<td>";
3328 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3329 hash_base=>$hash, file_name=>$diff->{'file'}),
3330 -class => "list"}, esc_path($diff->{'file'}));
3331 print "</td>\n";
3332 print "<td>$mode_chng</td>\n";
3333 print "<td class=\"link\">";
3334 if ($action eq 'commitdiff') {
3335 # link to patch
3336 $patchno++;
3337 print $cgi->a({-href => "#patch$patchno"}, "patch");
3338 print " | ";
3340 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3341 hash_base=>$hash, file_name=>$diff->{'file'})},
3342 "blob");
3343 print "</td>\n";
3345 } elsif ($diff->{'status'} eq "D") { # deleted
3346 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3347 print "<td>";
3348 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3349 hash_base=>$parent, file_name=>$diff->{'file'}),
3350 -class => "list"}, esc_path($diff->{'file'}));
3351 print "</td>\n";
3352 print "<td>$mode_chng</td>\n";
3353 print "<td class=\"link\">";
3354 if ($action eq 'commitdiff') {
3355 # link to patch
3356 $patchno++;
3357 print $cgi->a({-href => "#patch$patchno"}, "patch");
3358 print " | ";
3360 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3361 hash_base=>$parent, file_name=>$diff->{'file'})},
3362 "blob") . " | ";
3363 if ($have_blame) {
3364 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3365 file_name=>$diff->{'file'})},
3366 "blame") . " | ";
3368 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3369 file_name=>$diff->{'file'})},
3370 "history");
3371 print "</td>\n";
3373 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3374 my $mode_chnge = "";
3375 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3376 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3377 if ($from_file_type ne $to_file_type) {
3378 $mode_chnge .= " from $from_file_type to $to_file_type";
3380 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3381 if ($from_mode_str && $to_mode_str) {
3382 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3383 } elsif ($to_mode_str) {
3384 $mode_chnge .= " mode: $to_mode_str";
3387 $mode_chnge .= "]</span>\n";
3389 print "<td>";
3390 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3391 hash_base=>$hash, file_name=>$diff->{'file'}),
3392 -class => "list"}, esc_path($diff->{'file'}));
3393 print "</td>\n";
3394 print "<td>$mode_chnge</td>\n";
3395 print "<td class=\"link\">";
3396 if ($action eq 'commitdiff') {
3397 # link to patch
3398 $patchno++;
3399 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3400 " | ";
3401 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3402 # "commit" view and modified file (not onlu mode changed)
3403 print $cgi->a({-href => href(action=>"blobdiff",
3404 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3405 hash_base=>$hash, hash_parent_base=>$parent,
3406 file_name=>$diff->{'file'})},
3407 "diff") .
3408 " | ";
3410 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3411 hash_base=>$hash, file_name=>$diff->{'file'})},
3412 "blob") . " | ";
3413 if ($have_blame) {
3414 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3415 file_name=>$diff->{'file'})},
3416 "blame") . " | ";
3418 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3419 file_name=>$diff->{'file'})},
3420 "history");
3421 print "</td>\n";
3423 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3424 my %status_name = ('R' => 'moved', 'C' => 'copied');
3425 my $nstatus = $status_name{$diff->{'status'}};
3426 my $mode_chng = "";
3427 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3428 # mode also for directories, so we cannot use $to_mode_str
3429 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3431 print "<td>" .
3432 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3433 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3434 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3435 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3436 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3437 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3438 -class => "list"}, esc_path($diff->{'from_file'})) .
3439 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3440 "<td class=\"link\">";
3441 if ($action eq 'commitdiff') {
3442 # link to patch
3443 $patchno++;
3444 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3445 " | ";
3446 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3447 # "commit" view and modified file (not only pure rename or copy)
3448 print $cgi->a({-href => href(action=>"blobdiff",
3449 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3450 hash_base=>$hash, hash_parent_base=>$parent,
3451 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3452 "diff") .
3453 " | ";
3455 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3456 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3457 "blob") . " | ";
3458 if ($have_blame) {
3459 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3460 file_name=>$diff->{'to_file'})},
3461 "blame") . " | ";
3463 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3464 file_name=>$diff->{'to_file'})},
3465 "history");
3466 print "</td>\n";
3468 } # we should not encounter Unmerged (U) or Unknown (X) status
3469 print "</tr>\n";
3471 print "</tbody>" if $has_header;
3472 print "</table>\n";
3475 sub git_patchset_body {
3476 my ($fd, $difftree, $hash, @hash_parents) = @_;
3477 my ($hash_parent) = $hash_parents[0];
3479 my $is_combined = (@hash_parents > 1);
3480 my $patch_idx = 0;
3481 my $patch_number = 0;
3482 my $patch_line;
3483 my $diffinfo;
3484 my $to_name;
3485 my (%from, %to);
3487 print "<div class=\"patchset\">\n";
3489 # skip to first patch
3490 while ($patch_line = <$fd>) {
3491 chomp $patch_line;
3493 last if ($patch_line =~ m/^diff /);
3496 PATCH:
3497 while ($patch_line) {
3499 # parse "git diff" header line
3500 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3501 # $1 is from_name, which we do not use
3502 $to_name = unquote($2);
3503 $to_name =~ s!^b/!!;
3504 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3505 # $1 is 'cc' or 'combined', which we do not use
3506 $to_name = unquote($2);
3507 } else {
3508 $to_name = undef;
3511 # check if current patch belong to current raw line
3512 # and parse raw git-diff line if needed
3513 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3514 # this is continuation of a split patch
3515 print "<div class=\"patch cont\">\n";
3516 } else {
3517 # advance raw git-diff output if needed
3518 $patch_idx++ if defined $diffinfo;
3520 # read and prepare patch information
3521 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3523 # compact combined diff output can have some patches skipped
3524 # find which patch (using pathname of result) we are at now;
3525 if ($is_combined) {
3526 while ($to_name ne $diffinfo->{'to_file'}) {
3527 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3528 format_diff_cc_simplified($diffinfo, @hash_parents) .
3529 "</div>\n"; # class="patch"
3531 $patch_idx++;
3532 $patch_number++;
3534 last if $patch_idx > $#$difftree;
3535 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3539 # modifies %from, %to hashes
3540 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3542 # this is first patch for raw difftree line with $patch_idx index
3543 # we index @$difftree array from 0, but number patches from 1
3544 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3547 # git diff header
3548 #assert($patch_line =~ m/^diff /) if DEBUG;
3549 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3550 $patch_number++;
3551 # print "git diff" header
3552 print format_git_diff_header_line($patch_line, $diffinfo,
3553 \%from, \%to);
3555 # print extended diff header
3556 print "<div class=\"diff extended_header\">\n";
3557 EXTENDED_HEADER:
3558 while ($patch_line = <$fd>) {
3559 chomp $patch_line;
3561 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3563 print format_extended_diff_header_line($patch_line, $diffinfo,
3564 \%from, \%to);
3566 print "</div>\n"; # class="diff extended_header"
3568 # from-file/to-file diff header
3569 if (! $patch_line) {
3570 print "</div>\n"; # class="patch"
3571 last PATCH;
3573 next PATCH if ($patch_line =~ m/^diff /);
3574 #assert($patch_line =~ m/^---/) if DEBUG;
3576 my $last_patch_line = $patch_line;
3577 $patch_line = <$fd>;
3578 chomp $patch_line;
3579 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3581 print format_diff_from_to_header($last_patch_line, $patch_line,
3582 $diffinfo, \%from, \%to,
3583 @hash_parents);
3585 # the patch itself
3586 LINE:
3587 while ($patch_line = <$fd>) {
3588 chomp $patch_line;
3590 next PATCH if ($patch_line =~ m/^diff /);
3592 print format_diff_line($patch_line, \%from, \%to);
3595 } continue {
3596 print "</div>\n"; # class="patch"
3599 # for compact combined (--cc) format, with chunk and patch simpliciaction
3600 # patchset might be empty, but there might be unprocessed raw lines
3601 for (++$patch_idx if $patch_number > 0;
3602 $patch_idx < @$difftree;
3603 ++$patch_idx) {
3604 # read and prepare patch information
3605 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3607 # generate anchor for "patch" links in difftree / whatchanged part
3608 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3609 format_diff_cc_simplified($diffinfo, @hash_parents) .
3610 "</div>\n"; # class="patch"
3612 $patch_number++;
3615 if ($patch_number == 0) {
3616 if (@hash_parents > 1) {
3617 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3618 } else {
3619 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3623 print "</div>\n"; # class="patchset"
3626 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3628 # fills project list info (age, description, owner, forks) for each
3629 # project in the list, removing invalid projects from returned list
3630 # NOTE: modifies $projlist, but does not remove entries from it
3631 sub fill_project_list_info {
3632 my ($projlist, $check_forks) = @_;
3633 my @projects;
3635 PROJECT:
3636 foreach my $pr (@$projlist) {
3637 my (@activity) = git_get_last_activity($pr->{'path'});
3638 unless (@activity) {
3639 next PROJECT;
3641 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3642 if (!defined $pr->{'descr'}) {
3643 my $descr = git_get_project_description($pr->{'path'}) || "";
3644 $descr = to_utf8($descr);
3645 $pr->{'descr_long'} = $descr;
3646 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3648 if (!defined $pr->{'owner'}) {
3649 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3651 if ($check_forks) {
3652 my $pname = $pr->{'path'};
3653 if (($pname =~ s/\.git$//) &&
3654 ($pname !~ /\/$/) &&
3655 (-d "$projectroot/$pname")) {
3656 $pr->{'forks'} = "-d $projectroot/$pname";
3657 } else {
3658 $pr->{'forks'} = 0;
3661 push @projects, $pr;
3664 return @projects;
3667 # print 'sort by' <th> element, generating 'sort by $name' replay link
3668 # if that order is not selected
3669 sub print_sort_th {
3670 my ($name, $order, $header) = @_;
3671 $header ||= ucfirst($name);
3673 if ($order eq $name) {
3674 print "<th>$header</th>\n";
3675 } else {
3676 print "<th>" .
3677 $cgi->a({-href => href(-replay=>1, order=>$name),
3678 -class => "header"}, $header) .
3679 "</th>\n";
3683 sub git_project_list_body {
3684 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3686 my ($check_forks) = gitweb_check_feature('forks');
3687 my @projects = fill_project_list_info($projlist, $check_forks);
3689 $order ||= $default_projects_order;
3690 $from = 0 unless defined $from;
3691 $to = $#projects if (!defined $to || $#projects < $to);
3693 my %order_info = (
3694 project => { key => 'path', type => 'str' },
3695 descr => { key => 'descr_long', type => 'str' },
3696 owner => { key => 'owner', type => 'str' },
3697 age => { key => 'age', type => 'num' }
3699 my $oi = $order_info{$order};
3700 if ($oi->{'type'} eq 'str') {
3701 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
3702 } else {
3703 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
3706 print "<table class=\"project_list\">\n";
3707 unless ($no_header) {
3708 print "<tr>\n";
3709 if ($check_forks) {
3710 print "<th></th>\n";
3712 print_sort_th('project', $order, 'Project');
3713 print_sort_th('descr', $order, 'Description');
3714 print_sort_th('owner', $order, 'Owner');
3715 print_sort_th('age', $order, 'Last Change');
3716 print "<th></th>\n" . # for links
3717 "</tr>\n";
3719 my $alternate = 1;
3720 for (my $i = $from; $i <= $to; $i++) {
3721 my $pr = $projects[$i];
3722 if ($alternate) {
3723 print "<tr class=\"dark\">\n";
3724 } else {
3725 print "<tr class=\"light\">\n";
3727 $alternate ^= 1;
3728 if ($check_forks) {
3729 print "<td>";
3730 if ($pr->{'forks'}) {
3731 print "<!-- $pr->{'forks'} -->\n";
3732 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3734 print "</td>\n";
3736 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3737 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3738 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3739 -class => "list", -title => $pr->{'descr_long'}},
3740 esc_html($pr->{'descr'})) . "</td>\n" .
3741 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3742 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3743 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3744 "<td class=\"link\">" .
3745 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3746 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "log") . " | " .
3747 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3748 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3749 "</td>\n" .
3750 "</tr>\n";
3752 if (defined $extra) {
3753 print "<tr>\n";
3754 if ($check_forks) {
3755 print "<td></td>\n";
3757 print "<td colspan=\"5\">$extra</td>\n" .
3758 "</tr>\n";
3760 print "</table>\n";
3763 sub git_shortlog_body {
3764 # uses global variable $project
3765 my ($commitlist, $from, $to, $refs, $extra) = @_;
3767 $from = 0 unless defined $from;
3768 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3770 print "<table class=\"shortlog\">\n";
3771 my $alternate = 1;
3772 for (my $i = $from; $i <= $to; $i++) {
3773 my %co = %{$commitlist->[$i]};
3774 my $commit = $co{'id'};
3775 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 my $author = chop_and_escape_str($co{'author_name'}, 10);
3783 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
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 print format_subject_html($co{'title'}, $co{'title_short'},
3788 href(action=>"commit", hash=>$commit), $ref);
3789 print "</td>\n" .
3790 "<td class=\"link\">" .
3791 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3792 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3793 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3794 my $snapshot_links = format_snapshot_links($commit);
3795 if (defined $snapshot_links) {
3796 print " | " . $snapshot_links;
3798 print "</td>\n" .
3799 "</tr>\n";
3801 if (defined $extra) {
3802 print "<tr>\n" .
3803 "<td colspan=\"4\">$extra</td>\n" .
3804 "</tr>\n";
3806 print "</table>\n";
3809 sub git_history_body {
3810 # Warning: assumes constant type (blob or tree) during history
3811 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3813 $from = 0 unless defined $from;
3814 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3816 print "<table class=\"history\">\n";
3817 my $alternate = 1;
3818 for (my $i = $from; $i <= $to; $i++) {
3819 my %co = %{$commitlist->[$i]};
3820 if (!%co) {
3821 next;
3823 my $commit = $co{'id'};
3825 my $ref = format_ref_marker($refs, $commit);
3827 if ($alternate) {
3828 print "<tr class=\"dark\">\n";
3829 } else {
3830 print "<tr class=\"light\">\n";
3832 $alternate ^= 1;
3833 # shortlog uses chop_str($co{'author_name'}, 10)
3834 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3835 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3836 "<td><i>" . $author . "</i></td>\n" .
3837 "<td>";
3838 # originally git_history used chop_str($co{'title'}, 50)
3839 print format_subject_html($co{'title'}, $co{'title_short'},
3840 href(action=>"commit", hash=>$commit), $ref);
3841 print "</td>\n" .
3842 "<td class=\"link\">" .
3843 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3844 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3846 if ($ftype eq 'blob') {
3847 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3848 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3849 if (defined $blob_current && defined $blob_parent &&
3850 $blob_current ne $blob_parent) {
3851 print " | " .
3852 $cgi->a({-href => href(action=>"blobdiff",
3853 hash=>$blob_current, hash_parent=>$blob_parent,
3854 hash_base=>$hash_base, hash_parent_base=>$commit,
3855 file_name=>$file_name)},
3856 "diff to current");
3859 print "</td>\n" .
3860 "</tr>\n";
3862 if (defined $extra) {
3863 print "<tr>\n" .
3864 "<td colspan=\"4\">$extra</td>\n" .
3865 "</tr>\n";
3867 print "</table>\n";
3870 sub git_tags_body {
3871 # uses global variable $project
3872 my ($taglist, $from, $to, $extra) = @_;
3873 $from = 0 unless defined $from;
3874 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3876 print "<table class=\"tags\">\n";
3877 my $alternate = 1;
3878 for (my $i = $from; $i <= $to; $i++) {
3879 my $entry = $taglist->[$i];
3880 my %tag = %$entry;
3881 my $comment = $tag{'subject'};
3882 my $comment_short;
3883 if (defined $comment) {
3884 $comment_short = chop_str($comment, 30, 5);
3886 if ($alternate) {
3887 print "<tr class=\"dark\">\n";
3888 } else {
3889 print "<tr class=\"light\">\n";
3891 $alternate ^= 1;
3892 if (defined $tag{'age'}) {
3893 print "<td><i>$tag{'age'}</i></td>\n";
3894 } else {
3895 print "<td></td>\n";
3897 print "<td>" .
3898 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3899 -class => "list name"}, esc_html($tag{'name'})) .
3900 "</td>\n" .
3901 "<td>";
3902 if (defined $comment) {
3903 print format_subject_html($comment, $comment_short,
3904 href(action=>"tag", hash=>$tag{'id'}));
3906 print "</td>\n" .
3907 "<td class=\"selflink\">";
3908 if ($tag{'type'} eq "tag") {
3909 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3910 } else {
3911 print "&nbsp;";
3913 print "</td>\n" .
3914 "<td class=\"link\">" . " | " .
3915 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3916 if ($tag{'reftype'} eq "commit") {
3917 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "log");
3918 } elsif ($tag{'reftype'} eq "blob") {
3919 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3921 print "</td>\n" .
3922 "</tr>";
3924 if (defined $extra) {
3925 print "<tr>\n" .
3926 "<td colspan=\"5\">$extra</td>\n" .
3927 "</tr>\n";
3929 print "</table>\n";
3932 sub git_heads_body {
3933 # uses global variable $project
3934 my ($headlist, $head, $from, $to, $extra) = @_;
3935 $from = 0 unless defined $from;
3936 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3938 print "<table class=\"heads\">\n";
3939 my $alternate = 1;
3940 for (my $i = $from; $i <= $to; $i++) {
3941 my $entry = $headlist->[$i];
3942 my %ref = %$entry;
3943 my $curr = $ref{'id'} eq $head;
3944 if ($alternate) {
3945 print "<tr class=\"dark\">\n";
3946 } else {
3947 print "<tr class=\"light\">\n";
3949 $alternate ^= 1;
3950 print "<td><i>$ref{'age'}</i></td>\n" .
3951 ($curr ? "<td class=\"current_head\">" : "<td>") .
3952 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
3953 -class => "list name"},esc_html($ref{'name'})) .
3954 "</td>\n" .
3955 "<td class=\"link\">" .
3956 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "log") . " | " .
3957 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
3958 "</td>\n" .
3959 "</tr>";
3961 if (defined $extra) {
3962 print "<tr>\n" .
3963 "<td colspan=\"3\">$extra</td>\n" .
3964 "</tr>\n";
3966 print "</table>\n";
3969 sub git_search_grep_body {
3970 my ($commitlist, $from, $to, $extra) = @_;
3971 $from = 0 unless defined $from;
3972 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3974 print "<table class=\"commit_search\">\n";
3975 my $alternate = 1;
3976 for (my $i = $from; $i <= $to; $i++) {
3977 my %co = %{$commitlist->[$i]};
3978 if (!%co) {
3979 next;
3981 my $commit = $co{'id'};
3982 if ($alternate) {
3983 print "<tr class=\"dark\">\n";
3984 } else {
3985 print "<tr class=\"light\">\n";
3987 $alternate ^= 1;
3988 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3989 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3990 "<td><i>" . $author . "</i></td>\n" .
3991 "<td>" .
3992 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3993 -class => "list subject"},
3994 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3995 my $comment = $co{'comment'};
3996 foreach my $line (@$comment) {
3997 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
3998 my ($lead, $match, $trail) = ($1, $2, $3);
3999 $match = chop_str($match, 70, 5, 'center');
4000 my $contextlen = int((80 - length($match))/2);
4001 $contextlen = 30 if ($contextlen > 30);
4002 $lead = chop_str($lead, $contextlen, 10, 'left');
4003 $trail = chop_str($trail, $contextlen, 10, 'right');
4005 $lead = esc_html($lead);
4006 $match = esc_html($match);
4007 $trail = esc_html($trail);
4009 print "$lead<span class=\"match\">$match</span>$trail<br />";
4012 print "</td>\n" .
4013 "<td class=\"link\">" .
4014 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4015 " | " .
4016 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4017 " | " .
4018 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4019 print "</td>\n" .
4020 "</tr>\n";
4022 if (defined $extra) {
4023 print "<tr>\n" .
4024 "<td colspan=\"3\">$extra</td>\n" .
4025 "</tr>\n";
4027 print "</table>\n";
4030 ## ======================================================================
4031 ## ======================================================================
4032 ## actions
4034 sub git_project_list {
4035 my $order = $cgi->param('o');
4036 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4037 die_error(400, "Unknown order parameter");
4040 my @list = git_get_projects_list();
4041 if (!@list) {
4042 die_error(404, "No projects found");
4045 git_header_html();
4046 if (-f $home_text) {
4047 print "<div class=\"index_include\">\n";
4048 open (my $fd, $home_text);
4049 print <$fd>;
4050 close $fd;
4051 print "</div>\n";
4053 git_project_list_body(\@list, $order);
4054 git_footer_html();
4057 sub git_forks {
4058 my $order = $cgi->param('o');
4059 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4060 die_error(400, "Unknown order parameter");
4063 my @list = git_get_projects_list($project);
4064 if (!@list) {
4065 die_error(404, "No forks found");
4068 git_header_html();
4069 git_print_page_nav('','');
4070 git_print_header_div('summary', "$project forks");
4071 git_project_list_body(\@list, $order);
4072 git_footer_html();
4075 sub git_project_index {
4076 my @projects = git_get_projects_list($project);
4078 print $cgi->header(
4079 -type => 'text/plain',
4080 -charset => 'utf-8',
4081 -content_disposition => 'inline; filename="index.aux"');
4083 foreach my $pr (@projects) {
4084 if (!exists $pr->{'owner'}) {
4085 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4088 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4089 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4090 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4091 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4092 $path =~ s/ /\+/g;
4093 $owner =~ s/ /\+/g;
4095 print "$path $owner\n";
4099 sub git_summary {
4100 my $descr = git_get_project_description($project) || "none";
4101 my %co = parse_commit("HEAD");
4102 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4103 my $head = $co{'id'};
4105 my $owner = git_get_project_owner($project);
4107 my $refs = git_get_references();
4108 # These get_*_list functions return one more to allow us to see if
4109 # there are more ...
4110 my @taglist = git_get_tags_list(16);
4111 my @headlist = git_get_heads_list(16);
4112 my @forklist;
4113 my ($check_forks) = gitweb_check_feature('forks');
4115 if ($check_forks) {
4116 @forklist = git_get_projects_list($project);
4119 git_header_html();
4120 git_print_page_nav('summary','', $head);
4122 print "<div class=\"title\">&nbsp;</div>\n";
4123 print "<table class=\"projects_list\">\n" .
4124 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4125 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4126 if (defined $cd{'rfc2822'}) {
4127 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4130 # use per project git URL list in $projectroot/$project/cloneurl
4131 # or make project git URL from git base URL and project name
4132 my $url_tag = "URL";
4133 my @url_list = git_get_project_url_list($project);
4134 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4135 foreach my $git_url (@url_list) {
4136 next unless $git_url;
4137 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4138 $url_tag = "";
4140 print "</table>\n";
4142 if (-s "$projectroot/$project/README.html") {
4143 if (open my $fd, "$projectroot/$project/README.html") {
4144 print "<div class=\"title\">readme</div>\n" .
4145 "<div class=\"readme\">\n";
4146 print $_ while (<$fd>);
4147 print "\n</div>\n"; # class="readme"
4148 close $fd;
4152 # we need to request one more than 16 (0..15) to check if
4153 # those 16 are all
4154 my @commitlist = $head ? parse_commits($head, 17) : ();
4155 if (@commitlist) {
4156 git_print_header_div('shortlog');
4157 git_shortlog_body(\@commitlist, 0, 15, $refs,
4158 $#commitlist <= 15 ? undef :
4159 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4162 if (@taglist) {
4163 git_print_header_div('tags');
4164 git_tags_body(\@taglist, 0, 15,
4165 $#taglist <= 15 ? undef :
4166 $cgi->a({-href => href(action=>"tags")}, "..."));
4169 if (@headlist) {
4170 git_print_header_div('heads');
4171 git_heads_body(\@headlist, $head, 0, 15,
4172 $#headlist <= 15 ? undef :
4173 $cgi->a({-href => href(action=>"heads")}, "..."));
4176 if (@forklist) {
4177 git_print_header_div('forks');
4178 git_project_list_body(\@forklist, 'age', 0, 15,
4179 $#forklist <= 15 ? undef :
4180 $cgi->a({-href => href(action=>"forks")}, "..."),
4181 'no_header');
4184 git_footer_html();
4187 sub git_tag {
4188 my $head = git_get_head_hash($project);
4189 git_header_html();
4190 git_print_page_nav('','', $head,undef,$head);
4191 my %tag = parse_tag($hash);
4193 if (! %tag) {
4194 die_error(404, "Unknown tag object");
4197 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4198 print "<div class=\"title_text\">\n" .
4199 "<table class=\"object_header\">\n" .
4200 "<tr>\n" .
4201 "<td>object</td>\n" .
4202 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4203 $tag{'object'}) . "</td>\n" .
4204 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4205 $tag{'type'}) . "</td>\n" .
4206 "</tr>\n";
4207 if (defined($tag{'author'})) {
4208 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4209 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4210 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4211 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4212 "</td></tr>\n";
4214 print "</table>\n\n" .
4215 "</div>\n";
4216 print "<div class=\"page_body\">";
4217 my $comment = $tag{'comment'};
4218 foreach my $line (@$comment) {
4219 chomp $line;
4220 print esc_html($line, -nbsp=>1) . "<br/>\n";
4222 print "</div>\n";
4223 git_footer_html();
4226 sub git_blame {
4227 my $fd;
4228 my $ftype;
4230 gitweb_check_feature('blame')
4231 or die_error(403, "Blame view not allowed");
4233 die_error(400, "No file name given") unless $file_name;
4234 $hash_base ||= git_get_head_hash($project);
4235 die_error(404, "Couldn't find base commit") unless ($hash_base);
4236 my %co = parse_commit($hash_base)
4237 or die_error(404, "Commit not found");
4238 if (!defined $hash) {
4239 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4240 or die_error(404, "Error looking up file");
4242 $ftype = git_get_type($hash);
4243 if ($ftype !~ "blob") {
4244 die_error(400, "Object is not a blob");
4246 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4247 $file_name, $hash_base)
4248 or die_error(500, "Open git-blame failed");
4249 git_header_html();
4250 my $formats_nav =
4251 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4252 "blob") .
4253 " | " .
4254 $cgi->a({-href => href(action=>"history", -replay=>1)},
4255 "history") .
4256 " | " .
4257 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4258 "HEAD");
4259 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4260 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4261 git_print_page_path($file_name, $ftype, $hash_base);
4262 my @rev_color = (qw(light2 dark2));
4263 my $num_colors = scalar(@rev_color);
4264 my $current_color = 0;
4265 my $last_rev;
4266 print <<HTML;
4267 <div class="page_body">
4268 <table class="blame">
4269 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4270 HTML
4271 my %metainfo = ();
4272 while (1) {
4273 $_ = <$fd>;
4274 last unless defined $_;
4275 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4276 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4277 if (!exists $metainfo{$full_rev}) {
4278 $metainfo{$full_rev} = {};
4280 my $meta = $metainfo{$full_rev};
4281 while (<$fd>) {
4282 last if (s/^\t//);
4283 if (/^(\S+) (.*)$/) {
4284 $meta->{$1} = $2;
4287 my $data = $_;
4288 chomp $data;
4289 my $rev = substr($full_rev, 0, 8);
4290 my $author = $meta->{'author'};
4291 my %date = parse_date($meta->{'author-time'},
4292 $meta->{'author-tz'});
4293 my $date = $date{'iso-tz'};
4294 if ($group_size) {
4295 $current_color = ++$current_color % $num_colors;
4297 print "<tr class=\"$rev_color[$current_color]\">\n";
4298 if ($group_size) {
4299 print "<td class=\"sha1\"";
4300 print " title=\"". esc_html($author) . ", $date\"";
4301 print " rowspan=\"$group_size\"" if ($group_size > 1);
4302 print ">";
4303 print $cgi->a({-href => href(action=>"commit",
4304 hash=>$full_rev,
4305 file_name=>$file_name)},
4306 esc_html($rev));
4307 print "</td>\n";
4309 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4310 or die_error(500, "Open git-rev-parse failed");
4311 my $parent_commit = <$dd>;
4312 close $dd;
4313 chomp($parent_commit);
4314 my $blamed = href(action => 'blame',
4315 file_name => $meta->{'filename'},
4316 hash_base => $parent_commit);
4317 print "<td class=\"linenr\">";
4318 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4319 -id => "l$lineno",
4320 -class => "linenr" },
4321 esc_html($lineno));
4322 print "</td>";
4323 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4324 print "</tr>\n";
4326 print "</table>\n";
4327 print "</div>";
4328 close $fd
4329 or print "Reading blob failed\n";
4330 git_footer_html();
4333 sub git_tags {
4334 my $head = git_get_head_hash($project);
4335 git_header_html();
4336 git_print_page_nav('','', $head,undef,$head);
4337 git_print_header_div('summary', $project);
4339 my @tagslist = git_get_tags_list();
4340 if (@tagslist) {
4341 git_tags_body(\@tagslist);
4343 git_footer_html();
4346 sub git_heads {
4347 my $head = git_get_head_hash($project);
4348 git_header_html();
4349 git_print_page_nav('','', $head,undef,$head);
4350 git_print_header_div('summary', $project);
4352 my @headslist = git_get_heads_list();
4353 if (@headslist) {
4354 git_heads_body(\@headslist, $head);
4356 git_footer_html();
4359 sub git_blob_plain {
4360 my $type = shift;
4361 my $expires;
4363 if (!defined $hash) {
4364 if (defined $file_name) {
4365 my $base = $hash_base || git_get_head_hash($project);
4366 $hash = git_get_hash_by_path($base, $file_name, "blob")
4367 or die_error(404, "Cannot find file");
4368 } else {
4369 die_error(400, "No file name defined");
4371 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4372 # blobs defined by non-textual hash id's can be cached
4373 $expires = "+1d";
4376 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4377 or die_error(500, "Open git-cat-file blob '$hash' failed");
4379 # content-type (can include charset)
4380 $type = blob_contenttype($fd, $file_name, $type);
4382 # "save as" filename, even when no $file_name is given
4383 my $save_as = "$hash";
4384 if (defined $file_name) {
4385 $save_as = $file_name;
4386 } elsif ($type =~ m/^text\//) {
4387 $save_as .= '.txt';
4390 print $cgi->header(
4391 -type => $type,
4392 -expires => $expires,
4393 -content_disposition => 'inline; filename="' . $save_as . '"');
4394 undef $/;
4395 binmode STDOUT, ':raw';
4396 print <$fd>;
4397 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4398 $/ = "\n";
4399 close $fd;
4402 sub git_blob {
4403 my $expires;
4405 if (!defined $hash) {
4406 if (defined $file_name) {
4407 my $base = $hash_base || git_get_head_hash($project);
4408 $hash = git_get_hash_by_path($base, $file_name, "blob")
4409 or die_error(404, "Cannot find file");
4410 } else {
4411 die_error(400, "No file name defined");
4413 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4414 # blobs defined by non-textual hash id's can be cached
4415 $expires = "+1d";
4418 my ($have_blame) = gitweb_check_feature('blame');
4419 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4420 or die_error(500, "Couldn't cat $file_name, $hash");
4421 my $mimetype = blob_mimetype($fd, $file_name);
4422 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4423 close $fd;
4424 return git_blob_plain($mimetype);
4426 # we can have blame only for text/* mimetype
4427 $have_blame &&= ($mimetype =~ m!^text/!);
4429 git_header_html(undef, $expires);
4430 my $formats_nav = '';
4431 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4432 if (defined $file_name) {
4433 if ($have_blame) {
4434 $formats_nav .=
4435 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4436 "blame") .
4437 " | ";
4439 $formats_nav .=
4440 $cgi->a({-href => href(action=>"history", -replay=>1)},
4441 "history") .
4442 " | " .
4443 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4444 "raw") .
4445 " | " .
4446 $cgi->a({-href => href(action=>"blob",
4447 hash_base=>"HEAD", file_name=>$file_name)},
4448 "HEAD");
4449 } else {
4450 $formats_nav .=
4451 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4452 "raw");
4454 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4455 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4456 } else {
4457 print "<div class=\"page_nav\">\n" .
4458 "<br/><br/></div>\n" .
4459 "<div class=\"title\">$hash</div>\n";
4461 git_print_page_path($file_name, "blob", $hash_base);
4462 print "<div class=\"page_body\">\n";
4463 if ($mimetype =~ m!^image/!) {
4464 print qq!<img type="$mimetype"!;
4465 if ($file_name) {
4466 print qq! alt="$file_name" title="$file_name"!;
4468 print qq! src="! .
4469 href(action=>"blob_plain", hash=>$hash,
4470 hash_base=>$hash_base, file_name=>$file_name) .
4471 qq!" />\n!;
4472 } else {
4473 my $nr;
4474 while (my $line = <$fd>) {
4475 chomp $line;
4476 $nr++;
4477 $line = untabify($line);
4478 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4479 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4482 close $fd
4483 or print "Reading blob failed.\n";
4484 print "</div>";
4485 git_footer_html();
4488 sub git_tree {
4489 if (!defined $hash_base) {
4490 $hash_base = "HEAD";
4492 if (!defined $hash) {
4493 if (defined $file_name) {
4494 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4495 } else {
4496 $hash = $hash_base;
4499 die_error(404, "No such tree") unless defined($hash);
4500 $/ = "\0";
4501 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4502 or die_error(500, "Open git-ls-tree failed");
4503 my @entries = map { chomp; $_ } <$fd>;
4504 close $fd or die_error(404, "Reading tree failed");
4505 $/ = "\n";
4507 my $refs = git_get_references();
4508 my $ref = format_ref_marker($refs, $hash_base);
4509 git_header_html();
4510 my $basedir = '';
4511 my ($have_blame) = gitweb_check_feature('blame');
4512 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4513 my @views_nav = ();
4514 if (defined $file_name) {
4515 push @views_nav,
4516 $cgi->a({-href => href(action=>"history", -replay=>1)},
4517 "history"),
4518 $cgi->a({-href => href(action=>"tree",
4519 hash_base=>"HEAD", file_name=>$file_name)},
4520 "HEAD"),
4522 my $snapshot_links = format_snapshot_links($hash);
4523 if (defined $snapshot_links) {
4524 # FIXME: Should be available when we have no hash base as well.
4525 push @views_nav, $snapshot_links;
4527 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4528 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4529 } else {
4530 undef $hash_base;
4531 print "<div class=\"page_nav\">\n";
4532 print "<br/><br/></div>\n";
4533 print "<div class=\"title\">$hash</div>\n";
4535 if (defined $file_name) {
4536 $basedir = $file_name;
4537 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4538 $basedir .= '/';
4540 git_print_page_path($file_name, 'tree', $hash_base);
4542 print "<div class=\"page_body\">\n";
4543 print "<table class=\"tree\">\n";
4544 my $alternate = 1;
4545 # '..' (top directory) link if possible
4546 if (defined $hash_base &&
4547 defined $file_name && $file_name =~ m![^/]+$!) {
4548 if ($alternate) {
4549 print "<tr class=\"dark\">\n";
4550 } else {
4551 print "<tr class=\"light\">\n";
4553 $alternate ^= 1;
4555 my $up = $file_name;
4556 $up =~ s!/?[^/]+$!!;
4557 undef $up unless $up;
4558 # based on git_print_tree_entry
4559 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4560 print '<td class="list">';
4561 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4562 file_name=>$up)},
4563 "..");
4564 print "</td>\n";
4565 print "<td class=\"link\"></td>\n";
4567 print "</tr>\n";
4569 foreach my $line (@entries) {
4570 my %t = parse_ls_tree_line($line, -z => 1);
4572 if ($alternate) {
4573 print "<tr class=\"dark\">\n";
4574 } else {
4575 print "<tr class=\"light\">\n";
4577 $alternate ^= 1;
4579 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4581 print "</tr>\n";
4583 print "</table>\n" .
4584 "</div>";
4585 git_footer_html();
4588 sub git_snapshot {
4589 my @supported_fmts = gitweb_check_feature('snapshot');
4590 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4592 my $format = $cgi->param('sf');
4593 if (!@supported_fmts) {
4594 die_error(403, "Snapshots not allowed");
4596 # default to first supported snapshot format
4597 $format ||= $supported_fmts[0];
4598 if ($format !~ m/^[a-z0-9]+$/) {
4599 die_error(400, "Invalid snapshot format parameter");
4600 } elsif (!exists($known_snapshot_formats{$format})) {
4601 die_error(400, "Unknown snapshot format");
4602 } elsif (!grep($_ eq $format, @supported_fmts)) {
4603 die_error(403, "Unsupported snapshot format");
4606 if (!defined $hash) {
4607 $hash = git_get_head_hash($project);
4610 my $name = $project;
4611 $name =~ s,([^/])/*\.git$,$1,;
4612 $name = basename($name);
4613 my $filename = to_utf8($name);
4614 $name =~ s/\047/\047\\\047\047/g;
4615 my $cmd;
4616 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4617 $cmd = quote_command(
4618 git_cmd(), 'archive',
4619 "--format=$known_snapshot_formats{$format}{'format'}",
4620 "--prefix=$name/", $hash);
4621 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4622 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
4625 print $cgi->header(
4626 -type => $known_snapshot_formats{$format}{'type'},
4627 -content_disposition => 'inline; filename="' . "$filename" . '"',
4628 -status => '200 OK');
4630 open my $fd, "-|", $cmd
4631 or die_error(500, "Execute git-archive failed");
4632 binmode STDOUT, ':raw';
4633 print <$fd>;
4634 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4635 close $fd;
4638 sub git_log {
4639 my $head = git_get_head_hash($project);
4640 if (!defined $hash) {
4641 $hash = $head;
4643 if (!defined $page) {
4644 $page = 0;
4646 my $refs = git_get_references();
4648 my @commitlist = parse_commits($hash, 101, (100 * $page));
4650 my $paging_nav = format_log_nav('log', $hash, $head, $page, $#commitlist >= 100);
4653 local $action = 'fulllog';
4654 git_header_html();
4656 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4658 if (!@commitlist) {
4659 my %co = parse_commit($hash);
4661 git_print_header_div('summary', $project);
4662 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4664 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4665 for (my $i = 0; $i <= $to; $i++) {
4666 my %co = %{$commitlist[$i]};
4667 next if !%co;
4668 my $commit = $co{'id'};
4669 my $ref = format_ref_marker($refs, $commit);
4670 my %ad = parse_date($co{'author_epoch'});
4671 git_print_header_div('commit',
4672 "<span class=\"age\">$co{'age_string'}</span>" .
4673 esc_html($co{'title'}) . $ref,
4674 $commit);
4675 print "<div class=\"title_text\">\n" .
4676 "<div class=\"log_link\">\n" .
4677 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4678 " | " .
4679 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4680 " | " .
4681 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4682 "<br/>\n" .
4683 "</div>\n" .
4684 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4685 "</div>\n";
4687 print "<div class=\"log_body\">\n";
4688 git_print_log($co{'comment'}, -final_empty_line=> 1);
4689 print "</div>\n";
4691 if ($#commitlist >= 100) {
4692 print "<div class=\"page_nav\">\n";
4693 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4694 -accesskey => "n", -title => "Alt-n"}, "next");
4695 print "</div>\n";
4697 git_footer_html();
4700 sub git_commit {
4701 $hash ||= $hash_base || "HEAD";
4702 my %co = parse_commit($hash)
4703 or die_error(404, "Unknown commit object");
4704 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4705 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4707 my $parent = $co{'parent'};
4708 my $parents = $co{'parents'}; # listref
4710 # we need to prepare $formats_nav before any parameter munging
4711 my $formats_nav;
4712 if (!defined $parent) {
4713 # --root commitdiff
4714 $formats_nav .= '(initial)';
4715 } elsif (@$parents == 1) {
4716 # single parent commit
4717 $formats_nav .=
4718 '(parent: ' .
4719 $cgi->a({-href => href(action=>"commit",
4720 hash=>$parent)},
4721 esc_html(substr($parent, 0, 7))) .
4722 ')';
4723 } else {
4724 # merge commit
4725 $formats_nav .=
4726 '(merge: ' .
4727 join(' ', map {
4728 $cgi->a({-href => href(action=>"commit",
4729 hash=>$_)},
4730 esc_html(substr($_, 0, 7)));
4731 } @$parents ) .
4732 ')';
4735 if (!defined $parent) {
4736 $parent = "--root";
4738 my @difftree;
4739 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4740 @diff_opts,
4741 (@$parents <= 1 ? $parent : '-c'),
4742 $hash, "--"
4743 or die_error(500, "Open git-diff-tree failed");
4744 @difftree = map { chomp; $_ } <$fd>;
4745 close $fd or die_error(404, "Reading git-diff-tree failed");
4747 # non-textual hash id's can be cached
4748 my $expires;
4749 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4750 $expires = "+1d";
4752 my $refs = git_get_references();
4753 my $ref = format_ref_marker($refs, $co{'id'});
4755 git_header_html(undef, $expires);
4756 git_print_page_nav('commit', '',
4757 $hash, $co{'tree'}, $hash,
4758 $formats_nav);
4760 if (defined $co{'parent'}) {
4761 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4762 } else {
4763 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4765 print "<div class=\"title_text\">\n" .
4766 "<table class=\"object_header\">\n";
4767 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4768 "<tr>" .
4769 "<td></td><td> $ad{'rfc2822'}";
4770 if ($ad{'hour_local'} < 6) {
4771 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4772 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4773 } else {
4774 printf(" (%02d:%02d %s)",
4775 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4777 print "</td>" .
4778 "</tr>\n";
4779 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4780 print "<tr><td></td><td> $cd{'rfc2822'}" .
4781 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4782 "</td></tr>\n";
4783 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4784 print "<tr>" .
4785 "<td>tree</td>" .
4786 "<td class=\"sha1\">" .
4787 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4788 class => "list"}, $co{'tree'}) .
4789 "</td>" .
4790 "<td class=\"link\">" .
4791 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4792 "tree");
4793 my $snapshot_links = format_snapshot_links($hash);
4794 if (defined $snapshot_links) {
4795 print " | " . $snapshot_links;
4797 print "</td>" .
4798 "</tr>\n";
4800 foreach my $par (@$parents) {
4801 print "<tr>" .
4802 "<td>parent</td>" .
4803 "<td class=\"sha1\">" .
4804 $cgi->a({-href => href(action=>"commit", hash=>$par),
4805 class => "list"}, $par) .
4806 "</td>" .
4807 "<td class=\"link\">" .
4808 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4809 " | " .
4810 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4811 "</td>" .
4812 "</tr>\n";
4814 print "</table>".
4815 "</div>\n";
4817 print "<div class=\"page_body\">\n";
4818 git_print_log($co{'comment'});
4819 print "</div>\n";
4821 git_difftree_body(\@difftree, $hash, @$parents);
4823 git_footer_html();
4826 sub git_object {
4827 # object is defined by:
4828 # - hash or hash_base alone
4829 # - hash_base and file_name
4830 my $type;
4832 # - hash or hash_base alone
4833 if ($hash || ($hash_base && !defined $file_name)) {
4834 my $object_id = $hash || $hash_base;
4836 open my $fd, "-|", quote_command(
4837 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
4838 or die_error(404, "Object does not exist");
4839 $type = <$fd>;
4840 chomp $type;
4841 close $fd
4842 or die_error(404, "Object does not exist");
4844 # - hash_base and file_name
4845 } elsif ($hash_base && defined $file_name) {
4846 $file_name =~ s,/+$,,;
4848 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4849 or die_error(404, "Base object does not exist");
4851 # here errors should not hapen
4852 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4853 or die_error(500, "Open git-ls-tree failed");
4854 my $line = <$fd>;
4855 close $fd;
4857 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4858 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4859 die_error(404, "File or directory for given base does not exist");
4861 $type = $2;
4862 $hash = $3;
4863 } else {
4864 die_error(400, "Not enough information to find object");
4867 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4868 hash=>$hash, hash_base=>$hash_base,
4869 file_name=>$file_name),
4870 -status => '302 Found');
4873 sub git_blobdiff {
4874 my $format = shift || 'html';
4876 my $fd;
4877 my @difftree;
4878 my %diffinfo;
4879 my $expires;
4881 # preparing $fd and %diffinfo for git_patchset_body
4882 # new style URI
4883 if (defined $hash_base && defined $hash_parent_base) {
4884 if (defined $file_name) {
4885 # read raw output
4886 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4887 $hash_parent_base, $hash_base,
4888 "--", (defined $file_parent ? $file_parent : ()), $file_name
4889 or die_error(500, "Open git-diff-tree failed");
4890 @difftree = map { chomp; $_ } <$fd>;
4891 close $fd
4892 or die_error(404, "Reading git-diff-tree failed");
4893 @difftree
4894 or die_error(404, "Blob diff not found");
4896 } elsif (defined $hash &&
4897 $hash =~ /[0-9a-fA-F]{40}/) {
4898 # try to find filename from $hash
4900 # read filtered raw output
4901 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4902 $hash_parent_base, $hash_base, "--"
4903 or die_error(500, "Open git-diff-tree failed");
4904 @difftree =
4905 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4906 # $hash == to_id
4907 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4908 map { chomp; $_ } <$fd>;
4909 close $fd
4910 or die_error(404, "Reading git-diff-tree failed");
4911 @difftree
4912 or die_error(404, "Blob diff not found");
4914 } else {
4915 die_error(400, "Missing one of the blob diff parameters");
4918 if (@difftree > 1) {
4919 die_error(400, "Ambiguous blob diff specification");
4922 %diffinfo = parse_difftree_raw_line($difftree[0]);
4923 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4924 $file_name ||= $diffinfo{'to_file'};
4926 $hash_parent ||= $diffinfo{'from_id'};
4927 $hash ||= $diffinfo{'to_id'};
4929 # non-textual hash id's can be cached
4930 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4931 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4932 $expires = '+1d';
4935 # open patch output
4936 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4937 '-p', ($format eq 'html' ? "--full-index" : ()),
4938 $hash_parent_base, $hash_base,
4939 "--", (defined $file_parent ? $file_parent : ()), $file_name
4940 or die_error(500, "Open git-diff-tree failed");
4943 # old/legacy style URI
4944 if (!%diffinfo && # if new style URI failed
4945 defined $hash && defined $hash_parent) {
4946 # fake git-diff-tree raw output
4947 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4948 $diffinfo{'from_id'} = $hash_parent;
4949 $diffinfo{'to_id'} = $hash;
4950 if (defined $file_name) {
4951 if (defined $file_parent) {
4952 $diffinfo{'status'} = '2';
4953 $diffinfo{'from_file'} = $file_parent;
4954 $diffinfo{'to_file'} = $file_name;
4955 } else { # assume not renamed
4956 $diffinfo{'status'} = '1';
4957 $diffinfo{'from_file'} = $file_name;
4958 $diffinfo{'to_file'} = $file_name;
4960 } else { # no filename given
4961 $diffinfo{'status'} = '2';
4962 $diffinfo{'from_file'} = $hash_parent;
4963 $diffinfo{'to_file'} = $hash;
4966 # non-textual hash id's can be cached
4967 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4968 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4969 $expires = '+1d';
4972 # open patch output
4973 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4974 '-p', ($format eq 'html' ? "--full-index" : ()),
4975 $hash_parent, $hash, "--"
4976 or die_error(500, "Open git-diff failed");
4977 } else {
4978 die_error(400, "Missing one of the blob diff parameters")
4979 unless %diffinfo;
4982 # header
4983 if ($format eq 'html') {
4984 my $formats_nav =
4985 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
4986 "raw");
4987 git_header_html(undef, $expires);
4988 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4989 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4990 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4991 } else {
4992 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4993 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4995 if (defined $file_name) {
4996 git_print_page_path($file_name, "blob", $hash_base);
4997 } else {
4998 print "<div class=\"page_path\"></div>\n";
5001 } elsif ($format eq 'plain') {
5002 print $cgi->header(
5003 -type => 'text/plain',
5004 -charset => 'utf-8',
5005 -expires => $expires,
5006 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5008 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5010 } else {
5011 die_error(400, "Unknown blobdiff format");
5014 # patch
5015 if ($format eq 'html') {
5016 print "<div class=\"page_body\">\n";
5018 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5019 close $fd;
5021 print "</div>\n"; # class="page_body"
5022 git_footer_html();
5024 } else {
5025 while (my $line = <$fd>) {
5026 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5027 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5029 print $line;
5031 last if $line =~ m!^\+\+\+!;
5033 local $/ = undef;
5034 print <$fd>;
5035 close $fd;
5039 sub git_blobdiff_plain {
5040 git_blobdiff('plain');
5043 sub git_commitdiff {
5044 my $format = shift || 'html';
5045 $hash ||= $hash_base || "HEAD";
5046 my %co = parse_commit($hash)
5047 or die_error(404, "Unknown commit object");
5049 # choose format for commitdiff for merge
5050 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5051 $hash_parent = '--cc';
5053 # we need to prepare $formats_nav before almost any parameter munging
5054 my $formats_nav;
5055 if ($format eq 'html') {
5056 $formats_nav =
5057 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5058 "raw");
5060 if (defined $hash_parent &&
5061 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5062 # commitdiff with two commits given
5063 my $hash_parent_short = $hash_parent;
5064 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5065 $hash_parent_short = substr($hash_parent, 0, 7);
5067 $formats_nav .=
5068 ' (from';
5069 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5070 if ($co{'parents'}[$i] eq $hash_parent) {
5071 $formats_nav .= ' parent ' . ($i+1);
5072 last;
5075 $formats_nav .= ': ' .
5076 $cgi->a({-href => href(action=>"commitdiff",
5077 hash=>$hash_parent)},
5078 esc_html($hash_parent_short)) .
5079 ')';
5080 } elsif (!$co{'parent'}) {
5081 # --root commitdiff
5082 $formats_nav .= ' (initial)';
5083 } elsif (scalar @{$co{'parents'}} == 1) {
5084 # single parent commit
5085 $formats_nav .=
5086 ' (parent: ' .
5087 $cgi->a({-href => href(action=>"commitdiff",
5088 hash=>$co{'parent'})},
5089 esc_html(substr($co{'parent'}, 0, 7))) .
5090 ')';
5091 } else {
5092 # merge commit
5093 if ($hash_parent eq '--cc') {
5094 $formats_nav .= ' | ' .
5095 $cgi->a({-href => href(action=>"commitdiff",
5096 hash=>$hash, hash_parent=>'-c')},
5097 'combined');
5098 } else { # $hash_parent eq '-c'
5099 $formats_nav .= ' | ' .
5100 $cgi->a({-href => href(action=>"commitdiff",
5101 hash=>$hash, hash_parent=>'--cc')},
5102 'compact');
5104 $formats_nav .=
5105 ' (merge: ' .
5106 join(' ', map {
5107 $cgi->a({-href => href(action=>"commitdiff",
5108 hash=>$_)},
5109 esc_html(substr($_, 0, 7)));
5110 } @{$co{'parents'}} ) .
5111 ')';
5115 my $hash_parent_param = $hash_parent;
5116 if (!defined $hash_parent_param) {
5117 # --cc for multiple parents, --root for parentless
5118 $hash_parent_param =
5119 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5122 # read commitdiff
5123 my $fd;
5124 my @difftree;
5125 if ($format eq 'html') {
5126 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5127 "--no-commit-id", "--patch-with-raw", "--full-index",
5128 $hash_parent_param, $hash, "--"
5129 or die_error(500, "Open git-diff-tree failed");
5131 while (my $line = <$fd>) {
5132 chomp $line;
5133 # empty line ends raw part of diff-tree output
5134 last unless $line;
5135 push @difftree, scalar parse_difftree_raw_line($line);
5138 } elsif ($format eq 'plain') {
5139 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5140 '-p', $hash_parent_param, $hash, "--"
5141 or die_error(500, "Open git-diff-tree failed");
5143 } else {
5144 die_error(400, "Unknown commitdiff format");
5147 # non-textual hash id's can be cached
5148 my $expires;
5149 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5150 $expires = "+1d";
5153 # write commit message
5154 if ($format eq 'html') {
5155 my $refs = git_get_references();
5156 my $ref = format_ref_marker($refs, $co{'id'});
5158 git_header_html(undef, $expires);
5159 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5160 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5161 git_print_authorship(\%co);
5162 print "<div class=\"page_body\">\n";
5163 if (@{$co{'comment'}} > 1) {
5164 print "<div class=\"log\">\n";
5165 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5166 print "</div>\n"; # class="log"
5169 } elsif ($format eq 'plain') {
5170 my $refs = git_get_references("tags");
5171 my $tagname = git_get_rev_name_tags($hash);
5172 my $filename = basename($project) . "-$hash.patch";
5174 print $cgi->header(
5175 -type => 'text/plain',
5176 -charset => 'utf-8',
5177 -expires => $expires,
5178 -content_disposition => 'inline; filename="' . "$filename" . '"');
5179 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5180 print "From: " . to_utf8($co{'author'}) . "\n";
5181 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5182 print "Subject: " . to_utf8($co{'title'}) . "\n";
5184 print "X-Git-Tag: $tagname\n" if $tagname;
5185 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5187 foreach my $line (@{$co{'comment'}}) {
5188 print to_utf8($line) . "\n";
5190 print "---\n\n";
5193 # write patch
5194 if ($format eq 'html') {
5195 my $use_parents = !defined $hash_parent ||
5196 $hash_parent eq '-c' || $hash_parent eq '--cc';
5197 git_difftree_body(\@difftree, $hash,
5198 $use_parents ? @{$co{'parents'}} : $hash_parent);
5199 print "<br/>\n";
5201 git_patchset_body($fd, \@difftree, $hash,
5202 $use_parents ? @{$co{'parents'}} : $hash_parent);
5203 close $fd;
5204 print "</div>\n"; # class="page_body"
5205 git_footer_html();
5207 } elsif ($format eq 'plain') {
5208 local $/ = undef;
5209 print <$fd>;
5210 close $fd
5211 or print "Reading git-diff-tree failed\n";
5215 sub git_commitdiff_plain {
5216 git_commitdiff('plain');
5219 sub git_history {
5220 if (!defined $hash_base) {
5221 $hash_base = git_get_head_hash($project);
5223 if (!defined $page) {
5224 $page = 0;
5226 my $ftype;
5227 my %co = parse_commit($hash_base)
5228 or die_error(404, "Unknown commit object");
5230 my $refs = git_get_references();
5231 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5233 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5234 $file_name, "--full-history")
5235 or die_error(404, "No such file or directory on given branch");
5237 if (!defined $hash && defined $file_name) {
5238 # some commits could have deleted file in question,
5239 # and not have it in tree, but one of them has to have it
5240 for (my $i = 0; $i <= @commitlist; $i++) {
5241 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5242 last if defined $hash;
5245 if (defined $hash) {
5246 $ftype = git_get_type($hash);
5248 if (!defined $ftype) {
5249 die_error(500, "Unknown type of object");
5252 my $paging_nav = '';
5253 if ($page > 0) {
5254 $paging_nav .=
5255 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5256 file_name=>$file_name)},
5257 "first");
5258 $paging_nav .= " &sdot; " .
5259 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5260 -accesskey => "p", -title => "Alt-p"}, "prev");
5261 } else {
5262 $paging_nav .= "first";
5263 $paging_nav .= " &sdot; prev";
5265 my $next_link = '';
5266 if ($#commitlist >= 100) {
5267 $next_link =
5268 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5269 -accesskey => "n", -title => "Alt-n"}, "next");
5270 $paging_nav .= " &sdot; $next_link";
5271 } else {
5272 $paging_nav .= " &sdot; next";
5275 git_header_html();
5276 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5277 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5278 git_print_page_path($file_name, $ftype, $hash_base);
5280 git_history_body(\@commitlist, 0, 99,
5281 $refs, $hash_base, $ftype, $next_link);
5283 git_footer_html();
5286 sub git_search {
5287 gitweb_check_feature('search') or die_error(403, "Search is disabled");
5288 if (!defined $searchtext) {
5289 die_error(400, "Text field is empty");
5291 if (!defined $hash) {
5292 $hash = git_get_head_hash($project);
5294 my %co = parse_commit($hash);
5295 if (!%co) {
5296 die_error(404, "Unknown commit object");
5298 if (!defined $page) {
5299 $page = 0;
5302 $searchtype ||= 'commit';
5303 if ($searchtype eq 'pickaxe') {
5304 # pickaxe may take all resources of your box and run for several minutes
5305 # with every query - so decide by yourself how public you make this feature
5306 gitweb_check_feature('pickaxe')
5307 or die_error(403, "Pickaxe is disabled");
5309 if ($searchtype eq 'grep') {
5310 gitweb_check_feature('grep')
5311 or die_error(403, "Grep is disabled");
5314 git_header_html();
5316 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5317 my $greptype;
5318 if ($searchtype eq 'commit') {
5319 $greptype = "--grep=";
5320 } elsif ($searchtype eq 'author') {
5321 $greptype = "--author=";
5322 } elsif ($searchtype eq 'committer') {
5323 $greptype = "--committer=";
5325 $greptype .= $searchtext;
5326 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5327 $greptype, '--regexp-ignore-case',
5328 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5330 my $paging_nav = '';
5331 if ($page > 0) {
5332 $paging_nav .=
5333 $cgi->a({-href => href(action=>"search", hash=>$hash,
5334 searchtext=>$searchtext,
5335 searchtype=>$searchtype)},
5336 "first");
5337 $paging_nav .= " &sdot; " .
5338 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5339 -accesskey => "p", -title => "Alt-p"}, "prev");
5340 } else {
5341 $paging_nav .= "first";
5342 $paging_nav .= " &sdot; prev";
5344 my $next_link = '';
5345 if ($#commitlist >= 100) {
5346 $next_link =
5347 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5348 -accesskey => "n", -title => "Alt-n"}, "next");
5349 $paging_nav .= " &sdot; $next_link";
5350 } else {
5351 $paging_nav .= " &sdot; next";
5354 if ($#commitlist >= 100) {
5357 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5358 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5359 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5362 if ($searchtype eq 'pickaxe') {
5363 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5364 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5366 print "<table class=\"pickaxe search\">\n";
5367 my $alternate = 1;
5368 $/ = "\n";
5369 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5370 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5371 ($search_use_regexp ? '--pickaxe-regex' : ());
5372 undef %co;
5373 my @files;
5374 while (my $line = <$fd>) {
5375 chomp $line;
5376 next unless $line;
5378 my %set = parse_difftree_raw_line($line);
5379 if (defined $set{'commit'}) {
5380 # finish previous commit
5381 if (%co) {
5382 print "</td>\n" .
5383 "<td class=\"link\">" .
5384 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5385 " | " .
5386 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5387 print "</td>\n" .
5388 "</tr>\n";
5391 if ($alternate) {
5392 print "<tr class=\"dark\">\n";
5393 } else {
5394 print "<tr class=\"light\">\n";
5396 $alternate ^= 1;
5397 %co = parse_commit($set{'commit'});
5398 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5399 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5400 "<td><i>$author</i></td>\n" .
5401 "<td>" .
5402 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5403 -class => "list subject"},
5404 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5405 } elsif (defined $set{'to_id'}) {
5406 next if ($set{'to_id'} =~ m/^0{40}$/);
5408 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5409 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5410 -class => "list"},
5411 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5412 "<br/>\n";
5415 close $fd;
5417 # finish last commit (warning: repetition!)
5418 if (%co) {
5419 print "</td>\n" .
5420 "<td class=\"link\">" .
5421 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5422 " | " .
5423 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5424 print "</td>\n" .
5425 "</tr>\n";
5428 print "</table>\n";
5431 if ($searchtype eq 'grep') {
5432 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5433 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5435 print "<table class=\"grep_search\">\n";
5436 my $alternate = 1;
5437 my $matches = 0;
5438 $/ = "\n";
5439 open my $fd, "-|", git_cmd(), 'grep', '-n',
5440 $search_use_regexp ? ('-E', '-i') : '-F',
5441 $searchtext, $co{'tree'};
5442 my $lastfile = '';
5443 while (my $line = <$fd>) {
5444 chomp $line;
5445 my ($file, $lno, $ltext, $binary);
5446 last if ($matches++ > 1000);
5447 if ($line =~ /^Binary file (.+) matches$/) {
5448 $file = $1;
5449 $binary = 1;
5450 } else {
5451 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5453 if ($file ne $lastfile) {
5454 $lastfile and print "</td></tr>\n";
5455 if ($alternate++) {
5456 print "<tr class=\"dark\">\n";
5457 } else {
5458 print "<tr class=\"light\">\n";
5460 print "<td class=\"list\">".
5461 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5462 file_name=>"$file"),
5463 -class => "list"}, esc_path($file));
5464 print "</td><td>\n";
5465 $lastfile = $file;
5467 if ($binary) {
5468 print "<div class=\"binary\">Binary file</div>\n";
5469 } else {
5470 $ltext = untabify($ltext);
5471 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5472 $ltext = esc_html($1, -nbsp=>1);
5473 $ltext .= '<span class="match">';
5474 $ltext .= esc_html($2, -nbsp=>1);
5475 $ltext .= '</span>';
5476 $ltext .= esc_html($3, -nbsp=>1);
5477 } else {
5478 $ltext = esc_html($ltext, -nbsp=>1);
5480 print "<div class=\"pre\">" .
5481 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5482 file_name=>"$file").'#l'.$lno,
5483 -class => "linenr"}, sprintf('%4i', $lno))
5484 . ' ' . $ltext . "</div>\n";
5487 if ($lastfile) {
5488 print "</td></tr>\n";
5489 if ($matches > 1000) {
5490 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5492 } else {
5493 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5495 close $fd;
5497 print "</table>\n";
5499 git_footer_html();
5502 sub git_search_help {
5503 git_header_html();
5504 git_print_page_nav('','', $hash,$hash,$hash);
5505 print <<EOT;
5506 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5507 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5508 the pattern entered is recognized as the POSIX extended
5509 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5510 insensitive).</p>
5511 <dl>
5512 <dt><b>commit</b></dt>
5513 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5515 my ($have_grep) = gitweb_check_feature('grep');
5516 if ($have_grep) {
5517 print <<EOT;
5518 <dt><b>grep</b></dt>
5519 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5520 a different one) are searched for the given pattern. On large trees, this search can take
5521 a while and put some strain on the server, so please use it with some consideration. Note that
5522 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5523 case-sensitive.</dd>
5526 print <<EOT;
5527 <dt><b>author</b></dt>
5528 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5529 <dt><b>committer</b></dt>
5530 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5532 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5533 if ($have_pickaxe) {
5534 print <<EOT;
5535 <dt><b>pickaxe</b></dt>
5536 <dd>All commits that caused the string to appear or disappear from any file (changes that
5537 added, removed or "modified" the string) will be listed. This search can take a while and
5538 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5539 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5542 print "</dl>\n";
5543 git_footer_html();
5546 sub git_shortlog {
5547 my $head = git_get_head_hash($project);
5548 if (!defined $hash) {
5549 $hash = $head;
5551 if (!defined $page) {
5552 $page = 0;
5554 my $refs = git_get_references();
5556 my $commit_hash = $hash;
5557 if (defined $hash_parent) {
5558 $commit_hash = "$hash_parent..$hash";
5560 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
5562 my $paging_nav = format_log_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5564 my $next_link = '';
5565 if ($#commitlist >= 100) {
5566 $next_link =
5567 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5568 -accesskey => "n", -title => "Alt-n"}, "next");
5571 git_header_html();
5572 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5573 git_print_header_div('summary', $project);
5575 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5577 git_footer_html();
5580 ## ......................................................................
5581 ## feeds (RSS, Atom; OPML)
5583 sub git_feed {
5584 my $format = shift || 'atom';
5585 my ($have_blame) = gitweb_check_feature('blame');
5587 # Atom: http://www.atomenabled.org/developers/syndication/
5588 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5589 if ($format ne 'rss' && $format ne 'atom') {
5590 die_error(400, "Unknown web feed format");
5593 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5594 my $head = $hash || 'HEAD';
5595 my @commitlist = parse_commits($head, 150, 0, $file_name);
5597 my %latest_commit;
5598 my %latest_date;
5599 my $content_type = "application/$format+xml";
5600 if (defined $cgi->http('HTTP_ACCEPT') &&
5601 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5602 # browser (feed reader) prefers text/xml
5603 $content_type = 'text/xml';
5605 if (defined($commitlist[0])) {
5606 %latest_commit = %{$commitlist[0]};
5607 %latest_date = parse_date($latest_commit{'author_epoch'});
5608 print $cgi->header(
5609 -type => $content_type,
5610 -charset => 'utf-8',
5611 -last_modified => $latest_date{'rfc2822'});
5612 } else {
5613 print $cgi->header(
5614 -type => $content_type,
5615 -charset => 'utf-8');
5618 # Optimization: skip generating the body if client asks only
5619 # for Last-Modified date.
5620 return if ($cgi->request_method() eq 'HEAD');
5622 # header variables
5623 my $title = "$site_name - $project/$action";
5624 my $feed_type = 'log';
5625 if (defined $hash) {
5626 $title .= " - '$hash'";
5627 $feed_type = 'branch log';
5628 if (defined $file_name) {
5629 $title .= " :: $file_name";
5630 $feed_type = 'history';
5632 } elsif (defined $file_name) {
5633 $title .= " - $file_name";
5634 $feed_type = 'history';
5636 $title .= " $feed_type";
5637 my $descr = git_get_project_description($project);
5638 if (defined $descr) {
5639 $descr = esc_html($descr);
5640 } else {
5641 $descr = "$project " .
5642 ($format eq 'rss' ? 'RSS' : 'Atom') .
5643 " feed";
5645 my $owner = git_get_project_owner($project);
5646 $owner = esc_html($owner);
5648 #header
5649 my $alt_url;
5650 if (defined $file_name) {
5651 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5652 } elsif (defined $hash) {
5653 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5654 } else {
5655 $alt_url = href(-full=>1, action=>"summary");
5657 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5658 if ($format eq 'rss') {
5659 print <<XML;
5660 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5661 <channel>
5663 print "<title>$title</title>\n" .
5664 "<link>$alt_url</link>\n" .
5665 "<description>$descr</description>\n" .
5666 "<language>en</language>\n";
5667 } elsif ($format eq 'atom') {
5668 print <<XML;
5669 <feed xmlns="http://www.w3.org/2005/Atom">
5671 print "<title>$title</title>\n" .
5672 "<subtitle>$descr</subtitle>\n" .
5673 '<link rel="alternate" type="text/html" href="' .
5674 $alt_url . '" />' . "\n" .
5675 '<link rel="self" type="' . $content_type . '" href="' .
5676 $cgi->self_url() . '" />' . "\n" .
5677 "<id>" . href(-full=>1) . "</id>\n" .
5678 # use project owner for feed author
5679 "<author><name>$owner</name></author>\n";
5680 if (defined $favicon) {
5681 print "<icon>" . esc_url($favicon) . "</icon>\n";
5683 if (defined $logo_url) {
5684 # not twice as wide as tall: 72 x 27 pixels
5685 print "<logo>" . esc_url($logo) . "</logo>\n";
5687 if (! %latest_date) {
5688 # dummy date to keep the feed valid until commits trickle in:
5689 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5690 } else {
5691 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5695 # contents
5696 for (my $i = 0; $i <= $#commitlist; $i++) {
5697 my %co = %{$commitlist[$i]};
5698 my $commit = $co{'id'};
5699 # we read 150, we always show 30 and the ones more recent than 48 hours
5700 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5701 last;
5703 my %cd = parse_date($co{'author_epoch'});
5705 # get list of changed files
5706 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5707 $co{'parent'} || "--root",
5708 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5709 or next;
5710 my @difftree = map { chomp; $_ } <$fd>;
5711 close $fd
5712 or next;
5714 # print element (entry, item)
5715 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5716 if ($format eq 'rss') {
5717 print "<item>\n" .
5718 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5719 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5720 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5721 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5722 "<link>$co_url</link>\n" .
5723 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5724 "<content:encoded>" .
5725 "<![CDATA[\n";
5726 } elsif ($format eq 'atom') {
5727 print "<entry>\n" .
5728 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5729 "<updated>$cd{'iso-8601'}</updated>\n" .
5730 "<author>\n" .
5731 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5732 if ($co{'author_email'}) {
5733 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5735 print "</author>\n" .
5736 # use committer for contributor
5737 "<contributor>\n" .
5738 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5739 if ($co{'committer_email'}) {
5740 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5742 print "</contributor>\n" .
5743 "<published>$cd{'iso-8601'}</published>\n" .
5744 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5745 "<id>$co_url</id>\n" .
5746 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5747 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5749 my $comment = $co{'comment'};
5750 print "<pre>\n";
5751 foreach my $line (@$comment) {
5752 $line = esc_html($line);
5753 print "$line\n";
5755 print "</pre><ul>\n";
5756 foreach my $difftree_line (@difftree) {
5757 my %difftree = parse_difftree_raw_line($difftree_line);
5758 next if !$difftree{'from_id'};
5760 my $file = $difftree{'file'} || $difftree{'to_file'};
5762 print "<li>" .
5763 "[" .
5764 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5765 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5766 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5767 file_name=>$file, file_parent=>$difftree{'from_file'}),
5768 -title => "diff"}, 'D');
5769 if ($have_blame) {
5770 print $cgi->a({-href => href(-full=>1, action=>"blame",
5771 file_name=>$file, hash_base=>$commit),
5772 -title => "blame"}, 'B');
5774 # if this is not a feed of a file history
5775 if (!defined $file_name || $file_name ne $file) {
5776 print $cgi->a({-href => href(-full=>1, action=>"history",
5777 file_name=>$file, hash=>$commit),
5778 -title => "history"}, 'H');
5780 $file = esc_path($file);
5781 print "] ".
5782 "$file</li>\n";
5784 if ($format eq 'rss') {
5785 print "</ul>]]>\n" .
5786 "</content:encoded>\n" .
5787 "</item>\n";
5788 } elsif ($format eq 'atom') {
5789 print "</ul>\n</div>\n" .
5790 "</content>\n" .
5791 "</entry>\n";
5795 # end of feed
5796 if ($format eq 'rss') {
5797 print "</channel>\n</rss>\n";
5798 } elsif ($format eq 'atom') {
5799 print "</feed>\n";
5803 sub git_rss {
5804 git_feed('rss');
5807 sub git_atom {
5808 git_feed('atom');
5811 sub git_opml {
5812 my @list = git_get_projects_list();
5814 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5815 print <<XML;
5816 <?xml version="1.0" encoding="utf-8"?>
5817 <opml version="1.0">
5818 <head>
5819 <title>$site_name OPML Export</title>
5820 </head>
5821 <body>
5822 <outline text="git RSS feeds">
5825 foreach my $pr (@list) {
5826 my %proj = %$pr;
5827 my $head = git_get_head_hash($proj{'path'});
5828 if (!defined $head) {
5829 next;
5831 $git_dir = "$projectroot/$proj{'path'}";
5832 my %co = parse_commit($head);
5833 if (!%co) {
5834 next;
5837 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5838 my $rss = "$my_url?p=$proj{'path'};a=rss";
5839 my $html = "$my_url?p=$proj{'path'};a=summary";
5840 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5842 print <<XML;
5843 </outline>
5844 </body>
5845 </opml>