Merge branch 't/extra-actions/extra-actions' into refs/top-bases/master
[git/gitweb.git] / gitweb / gitweb.perl
blob68caa2a04c747c0919875c922fcb2658f417f55e
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 shortlog 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 for (qw(shortlog log)) {
2783 $arg{$_}{'hash'} = $head;
2788 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2789 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2791 my @actions = gitweb_check_feature('actions');
2792 while (@actions) {
2793 my ($label, $link, $pos) = (shift(@actions), shift(@actions), shift(@actions));
2794 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
2795 # munch munch
2796 $link =~ s#%n#$project#g;
2797 $link =~ s#%f#$git_dir#g;
2798 $treehead ? $link =~ s#%h#$treehead#g : $link =~ s#%h##g;
2799 $treebase ? $link =~ s#%b#$treebase#g : $link =~ s#%b##g;
2800 $arg{$label}{'_href'} = $link;
2803 print "<div class=\"page_nav\">\n" .
2804 (join " | ",
2805 map { $_ eq $current ?
2806 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
2807 } @navs);
2808 print "<br/>\n$extra<br/>\n" .
2809 "</div>\n";
2812 sub format_paging_nav {
2813 my ($action, $hash, $head, $page, $has_next_link) = @_;
2814 my $paging_nav;
2817 if ($hash ne $head || $page) {
2818 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2819 } else {
2820 $paging_nav .= "HEAD";
2823 if ($page > 0) {
2824 $paging_nav .= " &sdot; " .
2825 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2826 -accesskey => "p", -title => "Alt-p"}, "prev");
2827 } else {
2828 $paging_nav .= " &sdot; prev";
2831 if ($has_next_link) {
2832 $paging_nav .= " &sdot; " .
2833 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2834 -accesskey => "n", -title => "Alt-n"}, "next");
2835 } else {
2836 $paging_nav .= " &sdot; next";
2839 return $paging_nav;
2842 ## ......................................................................
2843 ## functions printing or outputting HTML: div
2845 sub git_print_header_div {
2846 my ($action, $title, $hash, $hash_base) = @_;
2847 my %args = ();
2849 $args{'action'} = $action;
2850 $args{'hash'} = $hash if $hash;
2851 $args{'hash_base'} = $hash_base if $hash_base;
2853 print "<div class=\"header\">\n" .
2854 $cgi->a({-href => href(%args), -class => "title"},
2855 $title ? $title : $action) .
2856 "\n</div>\n";
2859 #sub git_print_authorship (\%) {
2860 sub git_print_authorship {
2861 my $co = shift;
2863 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2864 print "<div class=\"author_date\">" .
2865 esc_html($co->{'author_name'}) .
2866 " [$ad{'rfc2822'}";
2867 if ($ad{'hour_local'} < 6) {
2868 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2869 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2870 } else {
2871 printf(" (%02d:%02d %s)",
2872 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2874 print "]</div>\n";
2877 sub git_print_page_path {
2878 my $name = shift;
2879 my $type = shift;
2880 my $hb = shift;
2883 print "<div class=\"page_path\">";
2884 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2885 -title => 'tree root'}, to_utf8("[$project]"));
2886 print " / ";
2887 if (defined $name) {
2888 my @dirname = split '/', $name;
2889 my $basename = pop @dirname;
2890 my $fullname = '';
2892 foreach my $dir (@dirname) {
2893 $fullname .= ($fullname ? '/' : '') . $dir;
2894 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2895 hash_base=>$hb),
2896 -title => $fullname}, esc_path($dir));
2897 print " / ";
2899 if (defined $type && $type eq 'blob') {
2900 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2901 hash_base=>$hb),
2902 -title => $name}, esc_path($basename));
2903 } elsif (defined $type && $type eq 'tree') {
2904 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2905 hash_base=>$hb),
2906 -title => $name}, esc_path($basename));
2907 print " / ";
2908 } else {
2909 print esc_path($basename);
2912 print "<br/></div>\n";
2915 # sub git_print_log (\@;%) {
2916 sub git_print_log ($;%) {
2917 my $log = shift;
2918 my %opts = @_;
2920 if ($opts{'-remove_title'}) {
2921 # remove title, i.e. first line of log
2922 shift @$log;
2924 # remove leading empty lines
2925 while (defined $log->[0] && $log->[0] eq "") {
2926 shift @$log;
2929 # print log
2930 my $signoff = 0;
2931 my $empty = 0;
2932 foreach my $line (@$log) {
2933 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2934 $signoff = 1;
2935 $empty = 0;
2936 if (! $opts{'-remove_signoff'}) {
2937 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2938 next;
2939 } else {
2940 # remove signoff lines
2941 next;
2943 } else {
2944 $signoff = 0;
2947 # print only one empty line
2948 # do not print empty line after signoff
2949 if ($line eq "") {
2950 next if ($empty || $signoff);
2951 $empty = 1;
2952 } else {
2953 $empty = 0;
2956 print format_log_line_html($line) . "<br/>\n";
2959 if ($opts{'-final_empty_line'}) {
2960 # end with single empty line
2961 print "<br/>\n" unless $empty;
2965 # return link target (what link points to)
2966 sub git_get_link_target {
2967 my $hash = shift;
2968 my $link_target;
2970 # read link
2971 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2972 or return;
2974 local $/;
2975 $link_target = <$fd>;
2977 close $fd
2978 or return;
2980 return $link_target;
2983 # given link target, and the directory (basedir) the link is in,
2984 # return target of link relative to top directory (top tree);
2985 # return undef if it is not possible (including absolute links).
2986 sub normalize_link_target {
2987 my ($link_target, $basedir, $hash_base) = @_;
2989 # we can normalize symlink target only if $hash_base is provided
2990 return unless $hash_base;
2992 # absolute symlinks (beginning with '/') cannot be normalized
2993 return if (substr($link_target, 0, 1) eq '/');
2995 # normalize link target to path from top (root) tree (dir)
2996 my $path;
2997 if ($basedir) {
2998 $path = $basedir . '/' . $link_target;
2999 } else {
3000 # we are in top (root) tree (dir)
3001 $path = $link_target;
3004 # remove //, /./, and /../
3005 my @path_parts;
3006 foreach my $part (split('/', $path)) {
3007 # discard '.' and ''
3008 next if (!$part || $part eq '.');
3009 # handle '..'
3010 if ($part eq '..') {
3011 if (@path_parts) {
3012 pop @path_parts;
3013 } else {
3014 # link leads outside repository (outside top dir)
3015 return;
3017 } else {
3018 push @path_parts, $part;
3021 $path = join('/', @path_parts);
3023 return $path;
3026 # print tree entry (row of git_tree), but without encompassing <tr> element
3027 sub git_print_tree_entry {
3028 my ($t, $basedir, $hash_base, $have_blame) = @_;
3030 my %base_key = ();
3031 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3033 # The format of a table row is: mode list link. Where mode is
3034 # the mode of the entry, list is the name of the entry, an href,
3035 # and link is the action links of the entry.
3037 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3038 if ($t->{'type'} eq "blob") {
3039 print "<td class=\"list\">" .
3040 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3041 file_name=>"$basedir$t->{'name'}", %base_key),
3042 -class => "list"}, esc_path($t->{'name'}));
3043 if (S_ISLNK(oct $t->{'mode'})) {
3044 my $link_target = git_get_link_target($t->{'hash'});
3045 if ($link_target) {
3046 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3047 if (defined $norm_target) {
3048 print " -> " .
3049 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3050 file_name=>$norm_target),
3051 -title => $norm_target}, esc_path($link_target));
3052 } else {
3053 print " -> " . esc_path($link_target);
3057 print "</td>\n";
3058 print "<td class=\"link\">";
3059 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3060 file_name=>"$basedir$t->{'name'}", %base_key)},
3061 "blob");
3062 if ($have_blame) {
3063 print " | " .
3064 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3065 file_name=>"$basedir$t->{'name'}", %base_key)},
3066 "blame");
3068 if (defined $hash_base) {
3069 print " | " .
3070 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3071 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3072 "history");
3074 print " | " .
3075 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3076 file_name=>"$basedir$t->{'name'}")},
3077 "raw");
3078 print "</td>\n";
3080 } elsif ($t->{'type'} eq "tree") {
3081 print "<td class=\"list\">";
3082 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3083 file_name=>"$basedir$t->{'name'}", %base_key)},
3084 esc_path($t->{'name'}));
3085 print "</td>\n";
3086 print "<td class=\"link\">";
3087 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3088 file_name=>"$basedir$t->{'name'}", %base_key)},
3089 "tree");
3090 if (defined $hash_base) {
3091 print " | " .
3092 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3093 file_name=>"$basedir$t->{'name'}")},
3094 "history");
3096 print "</td>\n";
3097 } else {
3098 # unknown object: we can only present history for it
3099 # (this includes 'commit' object, i.e. submodule support)
3100 print "<td class=\"list\">" .
3101 esc_path($t->{'name'}) .
3102 "</td>\n";
3103 print "<td class=\"link\">";
3104 if (defined $hash_base) {
3105 print $cgi->a({-href => href(action=>"history",
3106 hash_base=>$hash_base,
3107 file_name=>"$basedir$t->{'name'}")},
3108 "history");
3110 print "</td>\n";
3114 ## ......................................................................
3115 ## functions printing large fragments of HTML
3117 # get pre-image filenames for merge (combined) diff
3118 sub fill_from_file_info {
3119 my ($diff, @parents) = @_;
3121 $diff->{'from_file'} = [ ];
3122 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3123 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3124 if ($diff->{'status'}[$i] eq 'R' ||
3125 $diff->{'status'}[$i] eq 'C') {
3126 $diff->{'from_file'}[$i] =
3127 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3131 return $diff;
3134 # is current raw difftree line of file deletion
3135 sub is_deleted {
3136 my $diffinfo = shift;
3138 return $diffinfo->{'to_id'} eq ('0' x 40);
3141 # does patch correspond to [previous] difftree raw line
3142 # $diffinfo - hashref of parsed raw diff format
3143 # $patchinfo - hashref of parsed patch diff format
3144 # (the same keys as in $diffinfo)
3145 sub is_patch_split {
3146 my ($diffinfo, $patchinfo) = @_;
3148 return defined $diffinfo && defined $patchinfo
3149 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3153 sub git_difftree_body {
3154 my ($difftree, $hash, @parents) = @_;
3155 my ($parent) = $parents[0];
3156 my ($have_blame) = gitweb_check_feature('blame');
3157 print "<div class=\"list_head\">\n";
3158 if ($#{$difftree} > 10) {
3159 print(($#{$difftree} + 1) . " files changed:\n");
3161 print "</div>\n";
3163 print "<table class=\"" .
3164 (@parents > 1 ? "combined " : "") .
3165 "diff_tree\">\n";
3167 # header only for combined diff in 'commitdiff' view
3168 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3169 if ($has_header) {
3170 # table header
3171 print "<thead><tr>\n" .
3172 "<th></th><th></th>\n"; # filename, patchN link
3173 for (my $i = 0; $i < @parents; $i++) {
3174 my $par = $parents[$i];
3175 print "<th>" .
3176 $cgi->a({-href => href(action=>"commitdiff",
3177 hash=>$hash, hash_parent=>$par),
3178 -title => 'commitdiff to parent number ' .
3179 ($i+1) . ': ' . substr($par,0,7)},
3180 $i+1) .
3181 "&nbsp;</th>\n";
3183 print "</tr></thead>\n<tbody>\n";
3186 my $alternate = 1;
3187 my $patchno = 0;
3188 foreach my $line (@{$difftree}) {
3189 my $diff = parsed_difftree_line($line);
3191 if ($alternate) {
3192 print "<tr class=\"dark\">\n";
3193 } else {
3194 print "<tr class=\"light\">\n";
3196 $alternate ^= 1;
3198 if (exists $diff->{'nparents'}) { # combined diff
3200 fill_from_file_info($diff, @parents)
3201 unless exists $diff->{'from_file'};
3203 if (!is_deleted($diff)) {
3204 # file exists in the result (child) commit
3205 print "<td>" .
3206 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3207 file_name=>$diff->{'to_file'},
3208 hash_base=>$hash),
3209 -class => "list"}, esc_path($diff->{'to_file'})) .
3210 "</td>\n";
3211 } else {
3212 print "<td>" .
3213 esc_path($diff->{'to_file'}) .
3214 "</td>\n";
3217 if ($action eq 'commitdiff') {
3218 # link to patch
3219 $patchno++;
3220 print "<td class=\"link\">" .
3221 $cgi->a({-href => "#patch$patchno"}, "patch") .
3222 " | " .
3223 "</td>\n";
3226 my $has_history = 0;
3227 my $not_deleted = 0;
3228 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3229 my $hash_parent = $parents[$i];
3230 my $from_hash = $diff->{'from_id'}[$i];
3231 my $from_path = $diff->{'from_file'}[$i];
3232 my $status = $diff->{'status'}[$i];
3234 $has_history ||= ($status ne 'A');
3235 $not_deleted ||= ($status ne 'D');
3237 if ($status eq 'A') {
3238 print "<td class=\"link\" align=\"right\"> | </td>\n";
3239 } elsif ($status eq 'D') {
3240 print "<td class=\"link\">" .
3241 $cgi->a({-href => href(action=>"blob",
3242 hash_base=>$hash,
3243 hash=>$from_hash,
3244 file_name=>$from_path)},
3245 "blob" . ($i+1)) .
3246 " | </td>\n";
3247 } else {
3248 if ($diff->{'to_id'} eq $from_hash) {
3249 print "<td class=\"link nochange\">";
3250 } else {
3251 print "<td class=\"link\">";
3253 print $cgi->a({-href => href(action=>"blobdiff",
3254 hash=>$diff->{'to_id'},
3255 hash_parent=>$from_hash,
3256 hash_base=>$hash,
3257 hash_parent_base=>$hash_parent,
3258 file_name=>$diff->{'to_file'},
3259 file_parent=>$from_path)},
3260 "diff" . ($i+1)) .
3261 " | </td>\n";
3265 print "<td class=\"link\">";
3266 if ($not_deleted) {
3267 print $cgi->a({-href => href(action=>"blob",
3268 hash=>$diff->{'to_id'},
3269 file_name=>$diff->{'to_file'},
3270 hash_base=>$hash)},
3271 "blob");
3272 print " | " if ($has_history);
3274 if ($has_history) {
3275 print $cgi->a({-href => href(action=>"history",
3276 file_name=>$diff->{'to_file'},
3277 hash_base=>$hash)},
3278 "history");
3280 print "</td>\n";
3282 print "</tr>\n";
3283 next; # instead of 'else' clause, to avoid extra indent
3285 # else ordinary diff
3287 my ($to_mode_oct, $to_mode_str, $to_file_type);
3288 my ($from_mode_oct, $from_mode_str, $from_file_type);
3289 if ($diff->{'to_mode'} ne ('0' x 6)) {
3290 $to_mode_oct = oct $diff->{'to_mode'};
3291 if (S_ISREG($to_mode_oct)) { # only for regular file
3292 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3294 $to_file_type = file_type($diff->{'to_mode'});
3296 if ($diff->{'from_mode'} ne ('0' x 6)) {
3297 $from_mode_oct = oct $diff->{'from_mode'};
3298 if (S_ISREG($to_mode_oct)) { # only for regular file
3299 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3301 $from_file_type = file_type($diff->{'from_mode'});
3304 if ($diff->{'status'} eq "A") { # created
3305 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3306 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3307 $mode_chng .= "]</span>";
3308 print "<td>";
3309 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3310 hash_base=>$hash, file_name=>$diff->{'file'}),
3311 -class => "list"}, esc_path($diff->{'file'}));
3312 print "</td>\n";
3313 print "<td>$mode_chng</td>\n";
3314 print "<td class=\"link\">";
3315 if ($action eq 'commitdiff') {
3316 # link to patch
3317 $patchno++;
3318 print $cgi->a({-href => "#patch$patchno"}, "patch");
3319 print " | ";
3321 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3322 hash_base=>$hash, file_name=>$diff->{'file'})},
3323 "blob");
3324 print "</td>\n";
3326 } elsif ($diff->{'status'} eq "D") { # deleted
3327 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3328 print "<td>";
3329 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3330 hash_base=>$parent, file_name=>$diff->{'file'}),
3331 -class => "list"}, esc_path($diff->{'file'}));
3332 print "</td>\n";
3333 print "<td>$mode_chng</td>\n";
3334 print "<td class=\"link\">";
3335 if ($action eq 'commitdiff') {
3336 # link to patch
3337 $patchno++;
3338 print $cgi->a({-href => "#patch$patchno"}, "patch");
3339 print " | ";
3341 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3342 hash_base=>$parent, file_name=>$diff->{'file'})},
3343 "blob") . " | ";
3344 if ($have_blame) {
3345 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3346 file_name=>$diff->{'file'})},
3347 "blame") . " | ";
3349 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3350 file_name=>$diff->{'file'})},
3351 "history");
3352 print "</td>\n";
3354 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3355 my $mode_chnge = "";
3356 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3357 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3358 if ($from_file_type ne $to_file_type) {
3359 $mode_chnge .= " from $from_file_type to $to_file_type";
3361 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3362 if ($from_mode_str && $to_mode_str) {
3363 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3364 } elsif ($to_mode_str) {
3365 $mode_chnge .= " mode: $to_mode_str";
3368 $mode_chnge .= "]</span>\n";
3370 print "<td>";
3371 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3372 hash_base=>$hash, file_name=>$diff->{'file'}),
3373 -class => "list"}, esc_path($diff->{'file'}));
3374 print "</td>\n";
3375 print "<td>$mode_chnge</td>\n";
3376 print "<td class=\"link\">";
3377 if ($action eq 'commitdiff') {
3378 # link to patch
3379 $patchno++;
3380 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3381 " | ";
3382 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3383 # "commit" view and modified file (not onlu mode changed)
3384 print $cgi->a({-href => href(action=>"blobdiff",
3385 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3386 hash_base=>$hash, hash_parent_base=>$parent,
3387 file_name=>$diff->{'file'})},
3388 "diff") .
3389 " | ";
3391 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3392 hash_base=>$hash, file_name=>$diff->{'file'})},
3393 "blob") . " | ";
3394 if ($have_blame) {
3395 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3396 file_name=>$diff->{'file'})},
3397 "blame") . " | ";
3399 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3400 file_name=>$diff->{'file'})},
3401 "history");
3402 print "</td>\n";
3404 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3405 my %status_name = ('R' => 'moved', 'C' => 'copied');
3406 my $nstatus = $status_name{$diff->{'status'}};
3407 my $mode_chng = "";
3408 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3409 # mode also for directories, so we cannot use $to_mode_str
3410 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3412 print "<td>" .
3413 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3414 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3415 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3416 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3417 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3418 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3419 -class => "list"}, esc_path($diff->{'from_file'})) .
3420 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3421 "<td class=\"link\">";
3422 if ($action eq 'commitdiff') {
3423 # link to patch
3424 $patchno++;
3425 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3426 " | ";
3427 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3428 # "commit" view and modified file (not only pure rename or copy)
3429 print $cgi->a({-href => href(action=>"blobdiff",
3430 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3431 hash_base=>$hash, hash_parent_base=>$parent,
3432 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3433 "diff") .
3434 " | ";
3436 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3437 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3438 "blob") . " | ";
3439 if ($have_blame) {
3440 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3441 file_name=>$diff->{'to_file'})},
3442 "blame") . " | ";
3444 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3445 file_name=>$diff->{'to_file'})},
3446 "history");
3447 print "</td>\n";
3449 } # we should not encounter Unmerged (U) or Unknown (X) status
3450 print "</tr>\n";
3452 print "</tbody>" if $has_header;
3453 print "</table>\n";
3456 sub git_patchset_body {
3457 my ($fd, $difftree, $hash, @hash_parents) = @_;
3458 my ($hash_parent) = $hash_parents[0];
3460 my $is_combined = (@hash_parents > 1);
3461 my $patch_idx = 0;
3462 my $patch_number = 0;
3463 my $patch_line;
3464 my $diffinfo;
3465 my $to_name;
3466 my (%from, %to);
3468 print "<div class=\"patchset\">\n";
3470 # skip to first patch
3471 while ($patch_line = <$fd>) {
3472 chomp $patch_line;
3474 last if ($patch_line =~ m/^diff /);
3477 PATCH:
3478 while ($patch_line) {
3480 # parse "git diff" header line
3481 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3482 # $1 is from_name, which we do not use
3483 $to_name = unquote($2);
3484 $to_name =~ s!^b/!!;
3485 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3486 # $1 is 'cc' or 'combined', which we do not use
3487 $to_name = unquote($2);
3488 } else {
3489 $to_name = undef;
3492 # check if current patch belong to current raw line
3493 # and parse raw git-diff line if needed
3494 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3495 # this is continuation of a split patch
3496 print "<div class=\"patch cont\">\n";
3497 } else {
3498 # advance raw git-diff output if needed
3499 $patch_idx++ if defined $diffinfo;
3501 # read and prepare patch information
3502 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3504 # compact combined diff output can have some patches skipped
3505 # find which patch (using pathname of result) we are at now;
3506 if ($is_combined) {
3507 while ($to_name ne $diffinfo->{'to_file'}) {
3508 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3509 format_diff_cc_simplified($diffinfo, @hash_parents) .
3510 "</div>\n"; # class="patch"
3512 $patch_idx++;
3513 $patch_number++;
3515 last if $patch_idx > $#$difftree;
3516 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3520 # modifies %from, %to hashes
3521 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3523 # this is first patch for raw difftree line with $patch_idx index
3524 # we index @$difftree array from 0, but number patches from 1
3525 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3528 # git diff header
3529 #assert($patch_line =~ m/^diff /) if DEBUG;
3530 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3531 $patch_number++;
3532 # print "git diff" header
3533 print format_git_diff_header_line($patch_line, $diffinfo,
3534 \%from, \%to);
3536 # print extended diff header
3537 print "<div class=\"diff extended_header\">\n";
3538 EXTENDED_HEADER:
3539 while ($patch_line = <$fd>) {
3540 chomp $patch_line;
3542 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3544 print format_extended_diff_header_line($patch_line, $diffinfo,
3545 \%from, \%to);
3547 print "</div>\n"; # class="diff extended_header"
3549 # from-file/to-file diff header
3550 if (! $patch_line) {
3551 print "</div>\n"; # class="patch"
3552 last PATCH;
3554 next PATCH if ($patch_line =~ m/^diff /);
3555 #assert($patch_line =~ m/^---/) if DEBUG;
3557 my $last_patch_line = $patch_line;
3558 $patch_line = <$fd>;
3559 chomp $patch_line;
3560 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3562 print format_diff_from_to_header($last_patch_line, $patch_line,
3563 $diffinfo, \%from, \%to,
3564 @hash_parents);
3566 # the patch itself
3567 LINE:
3568 while ($patch_line = <$fd>) {
3569 chomp $patch_line;
3571 next PATCH if ($patch_line =~ m/^diff /);
3573 print format_diff_line($patch_line, \%from, \%to);
3576 } continue {
3577 print "</div>\n"; # class="patch"
3580 # for compact combined (--cc) format, with chunk and patch simpliciaction
3581 # patchset might be empty, but there might be unprocessed raw lines
3582 for (++$patch_idx if $patch_number > 0;
3583 $patch_idx < @$difftree;
3584 ++$patch_idx) {
3585 # read and prepare patch information
3586 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3588 # generate anchor for "patch" links in difftree / whatchanged part
3589 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3590 format_diff_cc_simplified($diffinfo, @hash_parents) .
3591 "</div>\n"; # class="patch"
3593 $patch_number++;
3596 if ($patch_number == 0) {
3597 if (@hash_parents > 1) {
3598 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3599 } else {
3600 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3604 print "</div>\n"; # class="patchset"
3607 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3609 # fills project list info (age, description, owner, forks) for each
3610 # project in the list, removing invalid projects from returned list
3611 # NOTE: modifies $projlist, but does not remove entries from it
3612 sub fill_project_list_info {
3613 my ($projlist, $check_forks) = @_;
3614 my @projects;
3616 PROJECT:
3617 foreach my $pr (@$projlist) {
3618 my (@activity) = git_get_last_activity($pr->{'path'});
3619 unless (@activity) {
3620 next PROJECT;
3622 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3623 if (!defined $pr->{'descr'}) {
3624 my $descr = git_get_project_description($pr->{'path'}) || "";
3625 $descr = to_utf8($descr);
3626 $pr->{'descr_long'} = $descr;
3627 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3629 if (!defined $pr->{'owner'}) {
3630 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3632 if ($check_forks) {
3633 my $pname = $pr->{'path'};
3634 if (($pname =~ s/\.git$//) &&
3635 ($pname !~ /\/$/) &&
3636 (-d "$projectroot/$pname")) {
3637 $pr->{'forks'} = "-d $projectroot/$pname";
3638 } else {
3639 $pr->{'forks'} = 0;
3642 push @projects, $pr;
3645 return @projects;
3648 # print 'sort by' <th> element, generating 'sort by $name' replay link
3649 # if that order is not selected
3650 sub print_sort_th {
3651 my ($name, $order, $header) = @_;
3652 $header ||= ucfirst($name);
3654 if ($order eq $name) {
3655 print "<th>$header</th>\n";
3656 } else {
3657 print "<th>" .
3658 $cgi->a({-href => href(-replay=>1, order=>$name),
3659 -class => "header"}, $header) .
3660 "</th>\n";
3664 sub git_project_list_body {
3665 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3667 my ($check_forks) = gitweb_check_feature('forks');
3668 my @projects = fill_project_list_info($projlist, $check_forks);
3670 $order ||= $default_projects_order;
3671 $from = 0 unless defined $from;
3672 $to = $#projects if (!defined $to || $#projects < $to);
3674 my %order_info = (
3675 project => { key => 'path', type => 'str' },
3676 descr => { key => 'descr_long', type => 'str' },
3677 owner => { key => 'owner', type => 'str' },
3678 age => { key => 'age', type => 'num' }
3680 my $oi = $order_info{$order};
3681 if ($oi->{'type'} eq 'str') {
3682 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
3683 } else {
3684 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
3687 print "<table class=\"project_list\">\n";
3688 unless ($no_header) {
3689 print "<tr>\n";
3690 if ($check_forks) {
3691 print "<th></th>\n";
3693 print_sort_th('project', $order, 'Project');
3694 print_sort_th('descr', $order, 'Description');
3695 print_sort_th('owner', $order, 'Owner');
3696 print_sort_th('age', $order, 'Last Change');
3697 print "<th></th>\n" . # for links
3698 "</tr>\n";
3700 my $alternate = 1;
3701 for (my $i = $from; $i <= $to; $i++) {
3702 my $pr = $projects[$i];
3703 if ($alternate) {
3704 print "<tr class=\"dark\">\n";
3705 } else {
3706 print "<tr class=\"light\">\n";
3708 $alternate ^= 1;
3709 if ($check_forks) {
3710 print "<td>";
3711 if ($pr->{'forks'}) {
3712 print "<!-- $pr->{'forks'} -->\n";
3713 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3715 print "</td>\n";
3717 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3718 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3719 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3720 -class => "list", -title => $pr->{'descr_long'}},
3721 esc_html($pr->{'descr'})) . "</td>\n" .
3722 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3723 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3724 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3725 "<td class=\"link\">" .
3726 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3727 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3728 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3729 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3730 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3731 "</td>\n" .
3732 "</tr>\n";
3734 if (defined $extra) {
3735 print "<tr>\n";
3736 if ($check_forks) {
3737 print "<td></td>\n";
3739 print "<td colspan=\"5\">$extra</td>\n" .
3740 "</tr>\n";
3742 print "</table>\n";
3745 sub git_shortlog_body {
3746 # uses global variable $project
3747 my ($commitlist, $from, $to, $refs, $extra) = @_;
3749 $from = 0 unless defined $from;
3750 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3752 print "<table class=\"shortlog\">\n";
3753 my $alternate = 1;
3754 for (my $i = $from; $i <= $to; $i++) {
3755 my %co = %{$commitlist->[$i]};
3756 my $commit = $co{'id'};
3757 my $ref = format_ref_marker($refs, $commit);
3758 if ($alternate) {
3759 print "<tr class=\"dark\">\n";
3760 } else {
3761 print "<tr class=\"light\">\n";
3763 $alternate ^= 1;
3764 my $author = chop_and_escape_str($co{'author_name'}, 10);
3765 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3766 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3767 "<td><i>" . $author . "</i></td>\n" .
3768 "<td>";
3769 print format_subject_html($co{'title'}, $co{'title_short'},
3770 href(action=>"commit", hash=>$commit), $ref);
3771 print "</td>\n" .
3772 "<td class=\"link\">" .
3773 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3774 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3775 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3776 my $snapshot_links = format_snapshot_links($commit);
3777 if (defined $snapshot_links) {
3778 print " | " . $snapshot_links;
3780 print "</td>\n" .
3781 "</tr>\n";
3783 if (defined $extra) {
3784 print "<tr>\n" .
3785 "<td colspan=\"4\">$extra</td>\n" .
3786 "</tr>\n";
3788 print "</table>\n";
3791 sub git_history_body {
3792 # Warning: assumes constant type (blob or tree) during history
3793 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3795 $from = 0 unless defined $from;
3796 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3798 print "<table class=\"history\">\n";
3799 my $alternate = 1;
3800 for (my $i = $from; $i <= $to; $i++) {
3801 my %co = %{$commitlist->[$i]};
3802 if (!%co) {
3803 next;
3805 my $commit = $co{'id'};
3807 my $ref = format_ref_marker($refs, $commit);
3809 if ($alternate) {
3810 print "<tr class=\"dark\">\n";
3811 } else {
3812 print "<tr class=\"light\">\n";
3814 $alternate ^= 1;
3815 # shortlog uses chop_str($co{'author_name'}, 10)
3816 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3817 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3818 "<td><i>" . $author . "</i></td>\n" .
3819 "<td>";
3820 # originally git_history used chop_str($co{'title'}, 50)
3821 print format_subject_html($co{'title'}, $co{'title_short'},
3822 href(action=>"commit", hash=>$commit), $ref);
3823 print "</td>\n" .
3824 "<td class=\"link\">" .
3825 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3826 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3828 if ($ftype eq 'blob') {
3829 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3830 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3831 if (defined $blob_current && defined $blob_parent &&
3832 $blob_current ne $blob_parent) {
3833 print " | " .
3834 $cgi->a({-href => href(action=>"blobdiff",
3835 hash=>$blob_current, hash_parent=>$blob_parent,
3836 hash_base=>$hash_base, hash_parent_base=>$commit,
3837 file_name=>$file_name)},
3838 "diff to current");
3841 print "</td>\n" .
3842 "</tr>\n";
3844 if (defined $extra) {
3845 print "<tr>\n" .
3846 "<td colspan=\"4\">$extra</td>\n" .
3847 "</tr>\n";
3849 print "</table>\n";
3852 sub git_tags_body {
3853 # uses global variable $project
3854 my ($taglist, $from, $to, $extra) = @_;
3855 $from = 0 unless defined $from;
3856 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3858 print "<table class=\"tags\">\n";
3859 my $alternate = 1;
3860 for (my $i = $from; $i <= $to; $i++) {
3861 my $entry = $taglist->[$i];
3862 my %tag = %$entry;
3863 my $comment = $tag{'subject'};
3864 my $comment_short;
3865 if (defined $comment) {
3866 $comment_short = chop_str($comment, 30, 5);
3868 if ($alternate) {
3869 print "<tr class=\"dark\">\n";
3870 } else {
3871 print "<tr class=\"light\">\n";
3873 $alternate ^= 1;
3874 if (defined $tag{'age'}) {
3875 print "<td><i>$tag{'age'}</i></td>\n";
3876 } else {
3877 print "<td></td>\n";
3879 print "<td>" .
3880 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3881 -class => "list name"}, esc_html($tag{'name'})) .
3882 "</td>\n" .
3883 "<td>";
3884 if (defined $comment) {
3885 print format_subject_html($comment, $comment_short,
3886 href(action=>"tag", hash=>$tag{'id'}));
3888 print "</td>\n" .
3889 "<td class=\"selflink\">";
3890 if ($tag{'type'} eq "tag") {
3891 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3892 } else {
3893 print "&nbsp;";
3895 print "</td>\n" .
3896 "<td class=\"link\">" . " | " .
3897 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3898 if ($tag{'reftype'} eq "commit") {
3899 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
3900 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
3901 } elsif ($tag{'reftype'} eq "blob") {
3902 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3904 print "</td>\n" .
3905 "</tr>";
3907 if (defined $extra) {
3908 print "<tr>\n" .
3909 "<td colspan=\"5\">$extra</td>\n" .
3910 "</tr>\n";
3912 print "</table>\n";
3915 sub git_heads_body {
3916 # uses global variable $project
3917 my ($headlist, $head, $from, $to, $extra) = @_;
3918 $from = 0 unless defined $from;
3919 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3921 print "<table class=\"heads\">\n";
3922 my $alternate = 1;
3923 for (my $i = $from; $i <= $to; $i++) {
3924 my $entry = $headlist->[$i];
3925 my %ref = %$entry;
3926 my $curr = $ref{'id'} eq $head;
3927 if ($alternate) {
3928 print "<tr class=\"dark\">\n";
3929 } else {
3930 print "<tr class=\"light\">\n";
3932 $alternate ^= 1;
3933 print "<td><i>$ref{'age'}</i></td>\n" .
3934 ($curr ? "<td class=\"current_head\">" : "<td>") .
3935 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
3936 -class => "list name"},esc_html($ref{'name'})) .
3937 "</td>\n" .
3938 "<td class=\"link\">" .
3939 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
3940 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
3941 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
3942 "</td>\n" .
3943 "</tr>";
3945 if (defined $extra) {
3946 print "<tr>\n" .
3947 "<td colspan=\"3\">$extra</td>\n" .
3948 "</tr>\n";
3950 print "</table>\n";
3953 sub git_search_grep_body {
3954 my ($commitlist, $from, $to, $extra) = @_;
3955 $from = 0 unless defined $from;
3956 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3958 print "<table class=\"commit_search\">\n";
3959 my $alternate = 1;
3960 for (my $i = $from; $i <= $to; $i++) {
3961 my %co = %{$commitlist->[$i]};
3962 if (!%co) {
3963 next;
3965 my $commit = $co{'id'};
3966 if ($alternate) {
3967 print "<tr class=\"dark\">\n";
3968 } else {
3969 print "<tr class=\"light\">\n";
3971 $alternate ^= 1;
3972 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3973 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3974 "<td><i>" . $author . "</i></td>\n" .
3975 "<td>" .
3976 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3977 -class => "list subject"},
3978 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3979 my $comment = $co{'comment'};
3980 foreach my $line (@$comment) {
3981 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
3982 my ($lead, $match, $trail) = ($1, $2, $3);
3983 $match = chop_str($match, 70, 5, 'center');
3984 my $contextlen = int((80 - length($match))/2);
3985 $contextlen = 30 if ($contextlen > 30);
3986 $lead = chop_str($lead, $contextlen, 10, 'left');
3987 $trail = chop_str($trail, $contextlen, 10, 'right');
3989 $lead = esc_html($lead);
3990 $match = esc_html($match);
3991 $trail = esc_html($trail);
3993 print "$lead<span class=\"match\">$match</span>$trail<br />";
3996 print "</td>\n" .
3997 "<td class=\"link\">" .
3998 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3999 " | " .
4000 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4001 " | " .
4002 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4003 print "</td>\n" .
4004 "</tr>\n";
4006 if (defined $extra) {
4007 print "<tr>\n" .
4008 "<td colspan=\"3\">$extra</td>\n" .
4009 "</tr>\n";
4011 print "</table>\n";
4014 ## ======================================================================
4015 ## ======================================================================
4016 ## actions
4018 sub git_project_list {
4019 my $order = $cgi->param('o');
4020 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4021 die_error(400, "Unknown order parameter");
4024 my @list = git_get_projects_list();
4025 if (!@list) {
4026 die_error(404, "No projects found");
4029 git_header_html();
4030 if (-f $home_text) {
4031 print "<div class=\"index_include\">\n";
4032 open (my $fd, $home_text);
4033 print <$fd>;
4034 close $fd;
4035 print "</div>\n";
4037 git_project_list_body(\@list, $order);
4038 git_footer_html();
4041 sub git_forks {
4042 my $order = $cgi->param('o');
4043 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4044 die_error(400, "Unknown order parameter");
4047 my @list = git_get_projects_list($project);
4048 if (!@list) {
4049 die_error(404, "No forks found");
4052 git_header_html();
4053 git_print_page_nav('','');
4054 git_print_header_div('summary', "$project forks");
4055 git_project_list_body(\@list, $order);
4056 git_footer_html();
4059 sub git_project_index {
4060 my @projects = git_get_projects_list($project);
4062 print $cgi->header(
4063 -type => 'text/plain',
4064 -charset => 'utf-8',
4065 -content_disposition => 'inline; filename="index.aux"');
4067 foreach my $pr (@projects) {
4068 if (!exists $pr->{'owner'}) {
4069 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4072 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4073 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4074 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4075 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4076 $path =~ s/ /\+/g;
4077 $owner =~ s/ /\+/g;
4079 print "$path $owner\n";
4083 sub git_summary {
4084 my $descr = git_get_project_description($project) || "none";
4085 my %co = parse_commit("HEAD");
4086 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4087 my $head = $co{'id'};
4089 my $owner = git_get_project_owner($project);
4091 my $refs = git_get_references();
4092 # These get_*_list functions return one more to allow us to see if
4093 # there are more ...
4094 my @taglist = git_get_tags_list(16);
4095 my @headlist = git_get_heads_list(16);
4096 my @forklist;
4097 my ($check_forks) = gitweb_check_feature('forks');
4099 if ($check_forks) {
4100 @forklist = git_get_projects_list($project);
4103 git_header_html();
4104 git_print_page_nav('summary','', $head);
4106 print "<div class=\"title\">&nbsp;</div>\n";
4107 print "<table class=\"projects_list\">\n" .
4108 "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4109 "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4110 if (defined $cd{'rfc2822'}) {
4111 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4114 # use per project git URL list in $projectroot/$project/cloneurl
4115 # or make project git URL from git base URL and project name
4116 my $url_tag = "URL";
4117 my @url_list = git_get_project_url_list($project);
4118 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4119 foreach my $git_url (@url_list) {
4120 next unless $git_url;
4121 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4122 $url_tag = "";
4124 print "</table>\n";
4126 if (-s "$projectroot/$project/README.html") {
4127 if (open my $fd, "$projectroot/$project/README.html") {
4128 print "<div class=\"title\">readme</div>\n" .
4129 "<div class=\"readme\">\n";
4130 print $_ while (<$fd>);
4131 print "\n</div>\n"; # class="readme"
4132 close $fd;
4136 # we need to request one more than 16 (0..15) to check if
4137 # those 16 are all
4138 my @commitlist = $head ? parse_commits($head, 17) : ();
4139 if (@commitlist) {
4140 git_print_header_div('shortlog');
4141 git_shortlog_body(\@commitlist, 0, 15, $refs,
4142 $#commitlist <= 15 ? undef :
4143 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4146 if (@taglist) {
4147 git_print_header_div('tags');
4148 git_tags_body(\@taglist, 0, 15,
4149 $#taglist <= 15 ? undef :
4150 $cgi->a({-href => href(action=>"tags")}, "..."));
4153 if (@headlist) {
4154 git_print_header_div('heads');
4155 git_heads_body(\@headlist, $head, 0, 15,
4156 $#headlist <= 15 ? undef :
4157 $cgi->a({-href => href(action=>"heads")}, "..."));
4160 if (@forklist) {
4161 git_print_header_div('forks');
4162 git_project_list_body(\@forklist, 'age', 0, 15,
4163 $#forklist <= 15 ? undef :
4164 $cgi->a({-href => href(action=>"forks")}, "..."),
4165 'no_header');
4168 git_footer_html();
4171 sub git_tag {
4172 my $head = git_get_head_hash($project);
4173 git_header_html();
4174 git_print_page_nav('','', $head,undef,$head);
4175 my %tag = parse_tag($hash);
4177 if (! %tag) {
4178 die_error(404, "Unknown tag object");
4181 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4182 print "<div class=\"title_text\">\n" .
4183 "<table class=\"object_header\">\n" .
4184 "<tr>\n" .
4185 "<td>object</td>\n" .
4186 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4187 $tag{'object'}) . "</td>\n" .
4188 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4189 $tag{'type'}) . "</td>\n" .
4190 "</tr>\n";
4191 if (defined($tag{'author'})) {
4192 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4193 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4194 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4195 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4196 "</td></tr>\n";
4198 print "</table>\n\n" .
4199 "</div>\n";
4200 print "<div class=\"page_body\">";
4201 my $comment = $tag{'comment'};
4202 foreach my $line (@$comment) {
4203 chomp $line;
4204 print esc_html($line, -nbsp=>1) . "<br/>\n";
4206 print "</div>\n";
4207 git_footer_html();
4210 sub git_blame {
4211 my $fd;
4212 my $ftype;
4214 gitweb_check_feature('blame')
4215 or die_error(403, "Blame view not allowed");
4217 die_error(400, "No file name given") unless $file_name;
4218 $hash_base ||= git_get_head_hash($project);
4219 die_error(404, "Couldn't find base commit") unless ($hash_base);
4220 my %co = parse_commit($hash_base)
4221 or die_error(404, "Commit not found");
4222 if (!defined $hash) {
4223 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4224 or die_error(404, "Error looking up file");
4226 $ftype = git_get_type($hash);
4227 if ($ftype !~ "blob") {
4228 die_error(400, "Object is not a blob");
4230 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4231 $file_name, $hash_base)
4232 or die_error(500, "Open git-blame failed");
4233 git_header_html();
4234 my $formats_nav =
4235 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4236 "blob") .
4237 " | " .
4238 $cgi->a({-href => href(action=>"history", -replay=>1)},
4239 "history") .
4240 " | " .
4241 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4242 "HEAD");
4243 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4244 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4245 git_print_page_path($file_name, $ftype, $hash_base);
4246 my @rev_color = (qw(light2 dark2));
4247 my $num_colors = scalar(@rev_color);
4248 my $current_color = 0;
4249 my $last_rev;
4250 print <<HTML;
4251 <div class="page_body">
4252 <table class="blame">
4253 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4254 HTML
4255 my %metainfo = ();
4256 while (1) {
4257 $_ = <$fd>;
4258 last unless defined $_;
4259 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4260 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4261 if (!exists $metainfo{$full_rev}) {
4262 $metainfo{$full_rev} = {};
4264 my $meta = $metainfo{$full_rev};
4265 while (<$fd>) {
4266 last if (s/^\t//);
4267 if (/^(\S+) (.*)$/) {
4268 $meta->{$1} = $2;
4271 my $data = $_;
4272 chomp $data;
4273 my $rev = substr($full_rev, 0, 8);
4274 my $author = $meta->{'author'};
4275 my %date = parse_date($meta->{'author-time'},
4276 $meta->{'author-tz'});
4277 my $date = $date{'iso-tz'};
4278 if ($group_size) {
4279 $current_color = ++$current_color % $num_colors;
4281 print "<tr class=\"$rev_color[$current_color]\">\n";
4282 if ($group_size) {
4283 print "<td class=\"sha1\"";
4284 print " title=\"". esc_html($author) . ", $date\"";
4285 print " rowspan=\"$group_size\"" if ($group_size > 1);
4286 print ">";
4287 print $cgi->a({-href => href(action=>"commit",
4288 hash=>$full_rev,
4289 file_name=>$file_name)},
4290 esc_html($rev));
4291 print "</td>\n";
4293 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4294 or die_error(500, "Open git-rev-parse failed");
4295 my $parent_commit = <$dd>;
4296 close $dd;
4297 chomp($parent_commit);
4298 my $blamed = href(action => 'blame',
4299 file_name => $meta->{'filename'},
4300 hash_base => $parent_commit);
4301 print "<td class=\"linenr\">";
4302 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4303 -id => "l$lineno",
4304 -class => "linenr" },
4305 esc_html($lineno));
4306 print "</td>";
4307 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4308 print "</tr>\n";
4310 print "</table>\n";
4311 print "</div>";
4312 close $fd
4313 or print "Reading blob failed\n";
4314 git_footer_html();
4317 sub git_tags {
4318 my $head = git_get_head_hash($project);
4319 git_header_html();
4320 git_print_page_nav('','', $head,undef,$head);
4321 git_print_header_div('summary', $project);
4323 my @tagslist = git_get_tags_list();
4324 if (@tagslist) {
4325 git_tags_body(\@tagslist);
4327 git_footer_html();
4330 sub git_heads {
4331 my $head = git_get_head_hash($project);
4332 git_header_html();
4333 git_print_page_nav('','', $head,undef,$head);
4334 git_print_header_div('summary', $project);
4336 my @headslist = git_get_heads_list();
4337 if (@headslist) {
4338 git_heads_body(\@headslist, $head);
4340 git_footer_html();
4343 sub git_blob_plain {
4344 my $type = shift;
4345 my $expires;
4347 if (!defined $hash) {
4348 if (defined $file_name) {
4349 my $base = $hash_base || git_get_head_hash($project);
4350 $hash = git_get_hash_by_path($base, $file_name, "blob")
4351 or die_error(404, "Cannot find file");
4352 } else {
4353 die_error(400, "No file name defined");
4355 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4356 # blobs defined by non-textual hash id's can be cached
4357 $expires = "+1d";
4360 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4361 or die_error(500, "Open git-cat-file blob '$hash' failed");
4363 # content-type (can include charset)
4364 $type = blob_contenttype($fd, $file_name, $type);
4366 # "save as" filename, even when no $file_name is given
4367 my $save_as = "$hash";
4368 if (defined $file_name) {
4369 $save_as = $file_name;
4370 } elsif ($type =~ m/^text\//) {
4371 $save_as .= '.txt';
4374 print $cgi->header(
4375 -type => $type,
4376 -expires => $expires,
4377 -content_disposition => 'inline; filename="' . $save_as . '"');
4378 undef $/;
4379 binmode STDOUT, ':raw';
4380 print <$fd>;
4381 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4382 $/ = "\n";
4383 close $fd;
4386 sub git_blob {
4387 my $expires;
4389 if (!defined $hash) {
4390 if (defined $file_name) {
4391 my $base = $hash_base || git_get_head_hash($project);
4392 $hash = git_get_hash_by_path($base, $file_name, "blob")
4393 or die_error(404, "Cannot find file");
4394 } else {
4395 die_error(400, "No file name defined");
4397 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4398 # blobs defined by non-textual hash id's can be cached
4399 $expires = "+1d";
4402 my ($have_blame) = gitweb_check_feature('blame');
4403 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4404 or die_error(500, "Couldn't cat $file_name, $hash");
4405 my $mimetype = blob_mimetype($fd, $file_name);
4406 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4407 close $fd;
4408 return git_blob_plain($mimetype);
4410 # we can have blame only for text/* mimetype
4411 $have_blame &&= ($mimetype =~ m!^text/!);
4413 git_header_html(undef, $expires);
4414 my $formats_nav = '';
4415 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4416 if (defined $file_name) {
4417 if ($have_blame) {
4418 $formats_nav .=
4419 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4420 "blame") .
4421 " | ";
4423 $formats_nav .=
4424 $cgi->a({-href => href(action=>"history", -replay=>1)},
4425 "history") .
4426 " | " .
4427 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4428 "raw") .
4429 " | " .
4430 $cgi->a({-href => href(action=>"blob",
4431 hash_base=>"HEAD", file_name=>$file_name)},
4432 "HEAD");
4433 } else {
4434 $formats_nav .=
4435 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4436 "raw");
4438 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4439 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4440 } else {
4441 print "<div class=\"page_nav\">\n" .
4442 "<br/><br/></div>\n" .
4443 "<div class=\"title\">$hash</div>\n";
4445 git_print_page_path($file_name, "blob", $hash_base);
4446 print "<div class=\"page_body\">\n";
4447 if ($mimetype =~ m!^image/!) {
4448 print qq!<img type="$mimetype"!;
4449 if ($file_name) {
4450 print qq! alt="$file_name" title="$file_name"!;
4452 print qq! src="! .
4453 href(action=>"blob_plain", hash=>$hash,
4454 hash_base=>$hash_base, file_name=>$file_name) .
4455 qq!" />\n!;
4456 } else {
4457 my $nr;
4458 while (my $line = <$fd>) {
4459 chomp $line;
4460 $nr++;
4461 $line = untabify($line);
4462 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4463 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4466 close $fd
4467 or print "Reading blob failed.\n";
4468 print "</div>";
4469 git_footer_html();
4472 sub git_tree {
4473 if (!defined $hash_base) {
4474 $hash_base = "HEAD";
4476 if (!defined $hash) {
4477 if (defined $file_name) {
4478 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4479 } else {
4480 $hash = $hash_base;
4483 die_error(404, "No such tree") unless defined($hash);
4484 $/ = "\0";
4485 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4486 or die_error(500, "Open git-ls-tree failed");
4487 my @entries = map { chomp; $_ } <$fd>;
4488 close $fd or die_error(404, "Reading tree failed");
4489 $/ = "\n";
4491 my $refs = git_get_references();
4492 my $ref = format_ref_marker($refs, $hash_base);
4493 git_header_html();
4494 my $basedir = '';
4495 my ($have_blame) = gitweb_check_feature('blame');
4496 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4497 my @views_nav = ();
4498 if (defined $file_name) {
4499 push @views_nav,
4500 $cgi->a({-href => href(action=>"history", -replay=>1)},
4501 "history"),
4502 $cgi->a({-href => href(action=>"tree",
4503 hash_base=>"HEAD", file_name=>$file_name)},
4504 "HEAD"),
4506 my $snapshot_links = format_snapshot_links($hash);
4507 if (defined $snapshot_links) {
4508 # FIXME: Should be available when we have no hash base as well.
4509 push @views_nav, $snapshot_links;
4511 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4512 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4513 } else {
4514 undef $hash_base;
4515 print "<div class=\"page_nav\">\n";
4516 print "<br/><br/></div>\n";
4517 print "<div class=\"title\">$hash</div>\n";
4519 if (defined $file_name) {
4520 $basedir = $file_name;
4521 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4522 $basedir .= '/';
4524 git_print_page_path($file_name, 'tree', $hash_base);
4526 print "<div class=\"page_body\">\n";
4527 print "<table class=\"tree\">\n";
4528 my $alternate = 1;
4529 # '..' (top directory) link if possible
4530 if (defined $hash_base &&
4531 defined $file_name && $file_name =~ m![^/]+$!) {
4532 if ($alternate) {
4533 print "<tr class=\"dark\">\n";
4534 } else {
4535 print "<tr class=\"light\">\n";
4537 $alternate ^= 1;
4539 my $up = $file_name;
4540 $up =~ s!/?[^/]+$!!;
4541 undef $up unless $up;
4542 # based on git_print_tree_entry
4543 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4544 print '<td class="list">';
4545 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4546 file_name=>$up)},
4547 "..");
4548 print "</td>\n";
4549 print "<td class=\"link\"></td>\n";
4551 print "</tr>\n";
4553 foreach my $line (@entries) {
4554 my %t = parse_ls_tree_line($line, -z => 1);
4556 if ($alternate) {
4557 print "<tr class=\"dark\">\n";
4558 } else {
4559 print "<tr class=\"light\">\n";
4561 $alternate ^= 1;
4563 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4565 print "</tr>\n";
4567 print "</table>\n" .
4568 "</div>";
4569 git_footer_html();
4572 sub git_snapshot {
4573 my @supported_fmts = gitweb_check_feature('snapshot');
4574 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4576 my $format = $cgi->param('sf');
4577 if (!@supported_fmts) {
4578 die_error(403, "Snapshots not allowed");
4580 # default to first supported snapshot format
4581 $format ||= $supported_fmts[0];
4582 if ($format !~ m/^[a-z0-9]+$/) {
4583 die_error(400, "Invalid snapshot format parameter");
4584 } elsif (!exists($known_snapshot_formats{$format})) {
4585 die_error(400, "Unknown snapshot format");
4586 } elsif (!grep($_ eq $format, @supported_fmts)) {
4587 die_error(403, "Unsupported snapshot format");
4590 if (!defined $hash) {
4591 $hash = git_get_head_hash($project);
4594 my $name = $project;
4595 $name =~ s,([^/])/*\.git$,$1,;
4596 $name = basename($name);
4597 my $filename = to_utf8($name);
4598 $name =~ s/\047/\047\\\047\047/g;
4599 my $cmd;
4600 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4601 $cmd = quote_command(
4602 git_cmd(), 'archive',
4603 "--format=$known_snapshot_formats{$format}{'format'}",
4604 "--prefix=$name/", $hash);
4605 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4606 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
4609 print $cgi->header(
4610 -type => $known_snapshot_formats{$format}{'type'},
4611 -content_disposition => 'inline; filename="' . "$filename" . '"',
4612 -status => '200 OK');
4614 open my $fd, "-|", $cmd
4615 or die_error(500, "Execute git-archive failed");
4616 binmode STDOUT, ':raw';
4617 print <$fd>;
4618 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4619 close $fd;
4622 sub git_log {
4623 my $head = git_get_head_hash($project);
4624 if (!defined $hash) {
4625 $hash = $head;
4627 if (!defined $page) {
4628 $page = 0;
4630 my $refs = git_get_references();
4632 my @commitlist = parse_commits($hash, 101, (100 * $page));
4634 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
4636 git_header_html();
4637 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4639 if (!@commitlist) {
4640 my %co = parse_commit($hash);
4642 git_print_header_div('summary', $project);
4643 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4645 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4646 for (my $i = 0; $i <= $to; $i++) {
4647 my %co = %{$commitlist[$i]};
4648 next if !%co;
4649 my $commit = $co{'id'};
4650 my $ref = format_ref_marker($refs, $commit);
4651 my %ad = parse_date($co{'author_epoch'});
4652 git_print_header_div('commit',
4653 "<span class=\"age\">$co{'age_string'}</span>" .
4654 esc_html($co{'title'}) . $ref,
4655 $commit);
4656 print "<div class=\"title_text\">\n" .
4657 "<div class=\"log_link\">\n" .
4658 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4659 " | " .
4660 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4661 " | " .
4662 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4663 "<br/>\n" .
4664 "</div>\n" .
4665 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4666 "</div>\n";
4668 print "<div class=\"log_body\">\n";
4669 git_print_log($co{'comment'}, -final_empty_line=> 1);
4670 print "</div>\n";
4672 if ($#commitlist >= 100) {
4673 print "<div class=\"page_nav\">\n";
4674 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4675 -accesskey => "n", -title => "Alt-n"}, "next");
4676 print "</div>\n";
4678 git_footer_html();
4681 sub git_commit {
4682 $hash ||= $hash_base || "HEAD";
4683 my %co = parse_commit($hash)
4684 or die_error(404, "Unknown commit object");
4685 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4686 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4688 my $parent = $co{'parent'};
4689 my $parents = $co{'parents'}; # listref
4691 # we need to prepare $formats_nav before any parameter munging
4692 my $formats_nav;
4693 if (!defined $parent) {
4694 # --root commitdiff
4695 $formats_nav .= '(initial)';
4696 } elsif (@$parents == 1) {
4697 # single parent commit
4698 $formats_nav .=
4699 '(parent: ' .
4700 $cgi->a({-href => href(action=>"commit",
4701 hash=>$parent)},
4702 esc_html(substr($parent, 0, 7))) .
4703 ')';
4704 } else {
4705 # merge commit
4706 $formats_nav .=
4707 '(merge: ' .
4708 join(' ', map {
4709 $cgi->a({-href => href(action=>"commit",
4710 hash=>$_)},
4711 esc_html(substr($_, 0, 7)));
4712 } @$parents ) .
4713 ')';
4716 if (!defined $parent) {
4717 $parent = "--root";
4719 my @difftree;
4720 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4721 @diff_opts,
4722 (@$parents <= 1 ? $parent : '-c'),
4723 $hash, "--"
4724 or die_error(500, "Open git-diff-tree failed");
4725 @difftree = map { chomp; $_ } <$fd>;
4726 close $fd or die_error(404, "Reading git-diff-tree failed");
4728 # non-textual hash id's can be cached
4729 my $expires;
4730 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4731 $expires = "+1d";
4733 my $refs = git_get_references();
4734 my $ref = format_ref_marker($refs, $co{'id'});
4736 git_header_html(undef, $expires);
4737 git_print_page_nav('commit', '',
4738 $hash, $co{'tree'}, $hash,
4739 $formats_nav);
4741 if (defined $co{'parent'}) {
4742 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4743 } else {
4744 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4746 print "<div class=\"title_text\">\n" .
4747 "<table class=\"object_header\">\n";
4748 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4749 "<tr>" .
4750 "<td></td><td> $ad{'rfc2822'}";
4751 if ($ad{'hour_local'} < 6) {
4752 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4753 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4754 } else {
4755 printf(" (%02d:%02d %s)",
4756 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4758 print "</td>" .
4759 "</tr>\n";
4760 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4761 print "<tr><td></td><td> $cd{'rfc2822'}" .
4762 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4763 "</td></tr>\n";
4764 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4765 print "<tr>" .
4766 "<td>tree</td>" .
4767 "<td class=\"sha1\">" .
4768 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4769 class => "list"}, $co{'tree'}) .
4770 "</td>" .
4771 "<td class=\"link\">" .
4772 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4773 "tree");
4774 my $snapshot_links = format_snapshot_links($hash);
4775 if (defined $snapshot_links) {
4776 print " | " . $snapshot_links;
4778 print "</td>" .
4779 "</tr>\n";
4781 foreach my $par (@$parents) {
4782 print "<tr>" .
4783 "<td>parent</td>" .
4784 "<td class=\"sha1\">" .
4785 $cgi->a({-href => href(action=>"commit", hash=>$par),
4786 class => "list"}, $par) .
4787 "</td>" .
4788 "<td class=\"link\">" .
4789 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4790 " | " .
4791 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4792 "</td>" .
4793 "</tr>\n";
4795 print "</table>".
4796 "</div>\n";
4798 print "<div class=\"page_body\">\n";
4799 git_print_log($co{'comment'});
4800 print "</div>\n";
4802 git_difftree_body(\@difftree, $hash, @$parents);
4804 git_footer_html();
4807 sub git_object {
4808 # object is defined by:
4809 # - hash or hash_base alone
4810 # - hash_base and file_name
4811 my $type;
4813 # - hash or hash_base alone
4814 if ($hash || ($hash_base && !defined $file_name)) {
4815 my $object_id = $hash || $hash_base;
4817 open my $fd, "-|", quote_command(
4818 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
4819 or die_error(404, "Object does not exist");
4820 $type = <$fd>;
4821 chomp $type;
4822 close $fd
4823 or die_error(404, "Object does not exist");
4825 # - hash_base and file_name
4826 } elsif ($hash_base && defined $file_name) {
4827 $file_name =~ s,/+$,,;
4829 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4830 or die_error(404, "Base object does not exist");
4832 # here errors should not hapen
4833 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4834 or die_error(500, "Open git-ls-tree failed");
4835 my $line = <$fd>;
4836 close $fd;
4838 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4839 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4840 die_error(404, "File or directory for given base does not exist");
4842 $type = $2;
4843 $hash = $3;
4844 } else {
4845 die_error(400, "Not enough information to find object");
4848 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4849 hash=>$hash, hash_base=>$hash_base,
4850 file_name=>$file_name),
4851 -status => '302 Found');
4854 sub git_blobdiff {
4855 my $format = shift || 'html';
4857 my $fd;
4858 my @difftree;
4859 my %diffinfo;
4860 my $expires;
4862 # preparing $fd and %diffinfo for git_patchset_body
4863 # new style URI
4864 if (defined $hash_base && defined $hash_parent_base) {
4865 if (defined $file_name) {
4866 # read raw output
4867 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4868 $hash_parent_base, $hash_base,
4869 "--", (defined $file_parent ? $file_parent : ()), $file_name
4870 or die_error(500, "Open git-diff-tree failed");
4871 @difftree = map { chomp; $_ } <$fd>;
4872 close $fd
4873 or die_error(404, "Reading git-diff-tree failed");
4874 @difftree
4875 or die_error(404, "Blob diff not found");
4877 } elsif (defined $hash &&
4878 $hash =~ /[0-9a-fA-F]{40}/) {
4879 # try to find filename from $hash
4881 # read filtered raw output
4882 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4883 $hash_parent_base, $hash_base, "--"
4884 or die_error(500, "Open git-diff-tree failed");
4885 @difftree =
4886 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4887 # $hash == to_id
4888 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4889 map { chomp; $_ } <$fd>;
4890 close $fd
4891 or die_error(404, "Reading git-diff-tree failed");
4892 @difftree
4893 or die_error(404, "Blob diff not found");
4895 } else {
4896 die_error(400, "Missing one of the blob diff parameters");
4899 if (@difftree > 1) {
4900 die_error(400, "Ambiguous blob diff specification");
4903 %diffinfo = parse_difftree_raw_line($difftree[0]);
4904 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4905 $file_name ||= $diffinfo{'to_file'};
4907 $hash_parent ||= $diffinfo{'from_id'};
4908 $hash ||= $diffinfo{'to_id'};
4910 # non-textual hash id's can be cached
4911 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4912 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4913 $expires = '+1d';
4916 # open patch output
4917 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4918 '-p', ($format eq 'html' ? "--full-index" : ()),
4919 $hash_parent_base, $hash_base,
4920 "--", (defined $file_parent ? $file_parent : ()), $file_name
4921 or die_error(500, "Open git-diff-tree failed");
4924 # old/legacy style URI
4925 if (!%diffinfo && # if new style URI failed
4926 defined $hash && defined $hash_parent) {
4927 # fake git-diff-tree raw output
4928 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4929 $diffinfo{'from_id'} = $hash_parent;
4930 $diffinfo{'to_id'} = $hash;
4931 if (defined $file_name) {
4932 if (defined $file_parent) {
4933 $diffinfo{'status'} = '2';
4934 $diffinfo{'from_file'} = $file_parent;
4935 $diffinfo{'to_file'} = $file_name;
4936 } else { # assume not renamed
4937 $diffinfo{'status'} = '1';
4938 $diffinfo{'from_file'} = $file_name;
4939 $diffinfo{'to_file'} = $file_name;
4941 } else { # no filename given
4942 $diffinfo{'status'} = '2';
4943 $diffinfo{'from_file'} = $hash_parent;
4944 $diffinfo{'to_file'} = $hash;
4947 # non-textual hash id's can be cached
4948 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4949 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4950 $expires = '+1d';
4953 # open patch output
4954 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4955 '-p', ($format eq 'html' ? "--full-index" : ()),
4956 $hash_parent, $hash, "--"
4957 or die_error(500, "Open git-diff failed");
4958 } else {
4959 die_error(400, "Missing one of the blob diff parameters")
4960 unless %diffinfo;
4963 # header
4964 if ($format eq 'html') {
4965 my $formats_nav =
4966 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
4967 "raw");
4968 git_header_html(undef, $expires);
4969 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4970 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4971 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4972 } else {
4973 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4974 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4976 if (defined $file_name) {
4977 git_print_page_path($file_name, "blob", $hash_base);
4978 } else {
4979 print "<div class=\"page_path\"></div>\n";
4982 } elsif ($format eq 'plain') {
4983 print $cgi->header(
4984 -type => 'text/plain',
4985 -charset => 'utf-8',
4986 -expires => $expires,
4987 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4989 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4991 } else {
4992 die_error(400, "Unknown blobdiff format");
4995 # patch
4996 if ($format eq 'html') {
4997 print "<div class=\"page_body\">\n";
4999 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5000 close $fd;
5002 print "</div>\n"; # class="page_body"
5003 git_footer_html();
5005 } else {
5006 while (my $line = <$fd>) {
5007 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5008 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5010 print $line;
5012 last if $line =~ m!^\+\+\+!;
5014 local $/ = undef;
5015 print <$fd>;
5016 close $fd;
5020 sub git_blobdiff_plain {
5021 git_blobdiff('plain');
5024 sub git_commitdiff {
5025 my $format = shift || 'html';
5026 $hash ||= $hash_base || "HEAD";
5027 my %co = parse_commit($hash)
5028 or die_error(404, "Unknown commit object");
5030 # choose format for commitdiff for merge
5031 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5032 $hash_parent = '--cc';
5034 # we need to prepare $formats_nav before almost any parameter munging
5035 my $formats_nav;
5036 if ($format eq 'html') {
5037 $formats_nav =
5038 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5039 "raw");
5041 if (defined $hash_parent &&
5042 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5043 # commitdiff with two commits given
5044 my $hash_parent_short = $hash_parent;
5045 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5046 $hash_parent_short = substr($hash_parent, 0, 7);
5048 $formats_nav .=
5049 ' (from';
5050 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5051 if ($co{'parents'}[$i] eq $hash_parent) {
5052 $formats_nav .= ' parent ' . ($i+1);
5053 last;
5056 $formats_nav .= ': ' .
5057 $cgi->a({-href => href(action=>"commitdiff",
5058 hash=>$hash_parent)},
5059 esc_html($hash_parent_short)) .
5060 ')';
5061 } elsif (!$co{'parent'}) {
5062 # --root commitdiff
5063 $formats_nav .= ' (initial)';
5064 } elsif (scalar @{$co{'parents'}} == 1) {
5065 # single parent commit
5066 $formats_nav .=
5067 ' (parent: ' .
5068 $cgi->a({-href => href(action=>"commitdiff",
5069 hash=>$co{'parent'})},
5070 esc_html(substr($co{'parent'}, 0, 7))) .
5071 ')';
5072 } else {
5073 # merge commit
5074 if ($hash_parent eq '--cc') {
5075 $formats_nav .= ' | ' .
5076 $cgi->a({-href => href(action=>"commitdiff",
5077 hash=>$hash, hash_parent=>'-c')},
5078 'combined');
5079 } else { # $hash_parent eq '-c'
5080 $formats_nav .= ' | ' .
5081 $cgi->a({-href => href(action=>"commitdiff",
5082 hash=>$hash, hash_parent=>'--cc')},
5083 'compact');
5085 $formats_nav .=
5086 ' (merge: ' .
5087 join(' ', map {
5088 $cgi->a({-href => href(action=>"commitdiff",
5089 hash=>$_)},
5090 esc_html(substr($_, 0, 7)));
5091 } @{$co{'parents'}} ) .
5092 ')';
5096 my $hash_parent_param = $hash_parent;
5097 if (!defined $hash_parent_param) {
5098 # --cc for multiple parents, --root for parentless
5099 $hash_parent_param =
5100 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5103 # read commitdiff
5104 my $fd;
5105 my @difftree;
5106 if ($format eq 'html') {
5107 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5108 "--no-commit-id", "--patch-with-raw", "--full-index",
5109 $hash_parent_param, $hash, "--"
5110 or die_error(500, "Open git-diff-tree failed");
5112 while (my $line = <$fd>) {
5113 chomp $line;
5114 # empty line ends raw part of diff-tree output
5115 last unless $line;
5116 push @difftree, scalar parse_difftree_raw_line($line);
5119 } elsif ($format eq 'plain') {
5120 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5121 '-p', $hash_parent_param, $hash, "--"
5122 or die_error(500, "Open git-diff-tree failed");
5124 } else {
5125 die_error(400, "Unknown commitdiff format");
5128 # non-textual hash id's can be cached
5129 my $expires;
5130 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5131 $expires = "+1d";
5134 # write commit message
5135 if ($format eq 'html') {
5136 my $refs = git_get_references();
5137 my $ref = format_ref_marker($refs, $co{'id'});
5139 git_header_html(undef, $expires);
5140 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5141 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5142 git_print_authorship(\%co);
5143 print "<div class=\"page_body\">\n";
5144 if (@{$co{'comment'}} > 1) {
5145 print "<div class=\"log\">\n";
5146 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5147 print "</div>\n"; # class="log"
5150 } elsif ($format eq 'plain') {
5151 my $refs = git_get_references("tags");
5152 my $tagname = git_get_rev_name_tags($hash);
5153 my $filename = basename($project) . "-$hash.patch";
5155 print $cgi->header(
5156 -type => 'text/plain',
5157 -charset => 'utf-8',
5158 -expires => $expires,
5159 -content_disposition => 'inline; filename="' . "$filename" . '"');
5160 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5161 print "From: " . to_utf8($co{'author'}) . "\n";
5162 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5163 print "Subject: " . to_utf8($co{'title'}) . "\n";
5165 print "X-Git-Tag: $tagname\n" if $tagname;
5166 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5168 foreach my $line (@{$co{'comment'}}) {
5169 print to_utf8($line) . "\n";
5171 print "---\n\n";
5174 # write patch
5175 if ($format eq 'html') {
5176 my $use_parents = !defined $hash_parent ||
5177 $hash_parent eq '-c' || $hash_parent eq '--cc';
5178 git_difftree_body(\@difftree, $hash,
5179 $use_parents ? @{$co{'parents'}} : $hash_parent);
5180 print "<br/>\n";
5182 git_patchset_body($fd, \@difftree, $hash,
5183 $use_parents ? @{$co{'parents'}} : $hash_parent);
5184 close $fd;
5185 print "</div>\n"; # class="page_body"
5186 git_footer_html();
5188 } elsif ($format eq 'plain') {
5189 local $/ = undef;
5190 print <$fd>;
5191 close $fd
5192 or print "Reading git-diff-tree failed\n";
5196 sub git_commitdiff_plain {
5197 git_commitdiff('plain');
5200 sub git_history {
5201 if (!defined $hash_base) {
5202 $hash_base = git_get_head_hash($project);
5204 if (!defined $page) {
5205 $page = 0;
5207 my $ftype;
5208 my %co = parse_commit($hash_base)
5209 or die_error(404, "Unknown commit object");
5211 my $refs = git_get_references();
5212 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5214 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5215 $file_name, "--full-history")
5216 or die_error(404, "No such file or directory on given branch");
5218 if (!defined $hash && defined $file_name) {
5219 # some commits could have deleted file in question,
5220 # and not have it in tree, but one of them has to have it
5221 for (my $i = 0; $i <= @commitlist; $i++) {
5222 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5223 last if defined $hash;
5226 if (defined $hash) {
5227 $ftype = git_get_type($hash);
5229 if (!defined $ftype) {
5230 die_error(500, "Unknown type of object");
5233 my $paging_nav = '';
5234 if ($page > 0) {
5235 $paging_nav .=
5236 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5237 file_name=>$file_name)},
5238 "first");
5239 $paging_nav .= " &sdot; " .
5240 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5241 -accesskey => "p", -title => "Alt-p"}, "prev");
5242 } else {
5243 $paging_nav .= "first";
5244 $paging_nav .= " &sdot; prev";
5246 my $next_link = '';
5247 if ($#commitlist >= 100) {
5248 $next_link =
5249 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5250 -accesskey => "n", -title => "Alt-n"}, "next");
5251 $paging_nav .= " &sdot; $next_link";
5252 } else {
5253 $paging_nav .= " &sdot; next";
5256 git_header_html();
5257 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5258 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5259 git_print_page_path($file_name, $ftype, $hash_base);
5261 git_history_body(\@commitlist, 0, 99,
5262 $refs, $hash_base, $ftype, $next_link);
5264 git_footer_html();
5267 sub git_search {
5268 gitweb_check_feature('search') or die_error(403, "Search is disabled");
5269 if (!defined $searchtext) {
5270 die_error(400, "Text field is empty");
5272 if (!defined $hash) {
5273 $hash = git_get_head_hash($project);
5275 my %co = parse_commit($hash);
5276 if (!%co) {
5277 die_error(404, "Unknown commit object");
5279 if (!defined $page) {
5280 $page = 0;
5283 $searchtype ||= 'commit';
5284 if ($searchtype eq 'pickaxe') {
5285 # pickaxe may take all resources of your box and run for several minutes
5286 # with every query - so decide by yourself how public you make this feature
5287 gitweb_check_feature('pickaxe')
5288 or die_error(403, "Pickaxe is disabled");
5290 if ($searchtype eq 'grep') {
5291 gitweb_check_feature('grep')
5292 or die_error(403, "Grep is disabled");
5295 git_header_html();
5297 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5298 my $greptype;
5299 if ($searchtype eq 'commit') {
5300 $greptype = "--grep=";
5301 } elsif ($searchtype eq 'author') {
5302 $greptype = "--author=";
5303 } elsif ($searchtype eq 'committer') {
5304 $greptype = "--committer=";
5306 $greptype .= $searchtext;
5307 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5308 $greptype, '--regexp-ignore-case',
5309 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5311 my $paging_nav = '';
5312 if ($page > 0) {
5313 $paging_nav .=
5314 $cgi->a({-href => href(action=>"search", hash=>$hash,
5315 searchtext=>$searchtext,
5316 searchtype=>$searchtype)},
5317 "first");
5318 $paging_nav .= " &sdot; " .
5319 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5320 -accesskey => "p", -title => "Alt-p"}, "prev");
5321 } else {
5322 $paging_nav .= "first";
5323 $paging_nav .= " &sdot; prev";
5325 my $next_link = '';
5326 if ($#commitlist >= 100) {
5327 $next_link =
5328 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5329 -accesskey => "n", -title => "Alt-n"}, "next");
5330 $paging_nav .= " &sdot; $next_link";
5331 } else {
5332 $paging_nav .= " &sdot; next";
5335 if ($#commitlist >= 100) {
5338 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5339 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5340 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5343 if ($searchtype eq 'pickaxe') {
5344 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5345 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5347 print "<table class=\"pickaxe search\">\n";
5348 my $alternate = 1;
5349 $/ = "\n";
5350 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5351 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5352 ($search_use_regexp ? '--pickaxe-regex' : ());
5353 undef %co;
5354 my @files;
5355 while (my $line = <$fd>) {
5356 chomp $line;
5357 next unless $line;
5359 my %set = parse_difftree_raw_line($line);
5360 if (defined $set{'commit'}) {
5361 # finish previous commit
5362 if (%co) {
5363 print "</td>\n" .
5364 "<td class=\"link\">" .
5365 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5366 " | " .
5367 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5368 print "</td>\n" .
5369 "</tr>\n";
5372 if ($alternate) {
5373 print "<tr class=\"dark\">\n";
5374 } else {
5375 print "<tr class=\"light\">\n";
5377 $alternate ^= 1;
5378 %co = parse_commit($set{'commit'});
5379 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5380 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5381 "<td><i>$author</i></td>\n" .
5382 "<td>" .
5383 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5384 -class => "list subject"},
5385 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5386 } elsif (defined $set{'to_id'}) {
5387 next if ($set{'to_id'} =~ m/^0{40}$/);
5389 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5390 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5391 -class => "list"},
5392 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5393 "<br/>\n";
5396 close $fd;
5398 # finish last commit (warning: repetition!)
5399 if (%co) {
5400 print "</td>\n" .
5401 "<td class=\"link\">" .
5402 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5403 " | " .
5404 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5405 print "</td>\n" .
5406 "</tr>\n";
5409 print "</table>\n";
5412 if ($searchtype eq 'grep') {
5413 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5414 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5416 print "<table class=\"grep_search\">\n";
5417 my $alternate = 1;
5418 my $matches = 0;
5419 $/ = "\n";
5420 open my $fd, "-|", git_cmd(), 'grep', '-n',
5421 $search_use_regexp ? ('-E', '-i') : '-F',
5422 $searchtext, $co{'tree'};
5423 my $lastfile = '';
5424 while (my $line = <$fd>) {
5425 chomp $line;
5426 my ($file, $lno, $ltext, $binary);
5427 last if ($matches++ > 1000);
5428 if ($line =~ /^Binary file (.+) matches$/) {
5429 $file = $1;
5430 $binary = 1;
5431 } else {
5432 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5434 if ($file ne $lastfile) {
5435 $lastfile and print "</td></tr>\n";
5436 if ($alternate++) {
5437 print "<tr class=\"dark\">\n";
5438 } else {
5439 print "<tr class=\"light\">\n";
5441 print "<td class=\"list\">".
5442 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5443 file_name=>"$file"),
5444 -class => "list"}, esc_path($file));
5445 print "</td><td>\n";
5446 $lastfile = $file;
5448 if ($binary) {
5449 print "<div class=\"binary\">Binary file</div>\n";
5450 } else {
5451 $ltext = untabify($ltext);
5452 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5453 $ltext = esc_html($1, -nbsp=>1);
5454 $ltext .= '<span class="match">';
5455 $ltext .= esc_html($2, -nbsp=>1);
5456 $ltext .= '</span>';
5457 $ltext .= esc_html($3, -nbsp=>1);
5458 } else {
5459 $ltext = esc_html($ltext, -nbsp=>1);
5461 print "<div class=\"pre\">" .
5462 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5463 file_name=>"$file").'#l'.$lno,
5464 -class => "linenr"}, sprintf('%4i', $lno))
5465 . ' ' . $ltext . "</div>\n";
5468 if ($lastfile) {
5469 print "</td></tr>\n";
5470 if ($matches > 1000) {
5471 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5473 } else {
5474 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5476 close $fd;
5478 print "</table>\n";
5480 git_footer_html();
5483 sub git_search_help {
5484 git_header_html();
5485 git_print_page_nav('','', $hash,$hash,$hash);
5486 print <<EOT;
5487 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5488 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5489 the pattern entered is recognized as the POSIX extended
5490 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5491 insensitive).</p>
5492 <dl>
5493 <dt><b>commit</b></dt>
5494 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5496 my ($have_grep) = gitweb_check_feature('grep');
5497 if ($have_grep) {
5498 print <<EOT;
5499 <dt><b>grep</b></dt>
5500 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5501 a different one) are searched for the given pattern. On large trees, this search can take
5502 a while and put some strain on the server, so please use it with some consideration. Note that
5503 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5504 case-sensitive.</dd>
5507 print <<EOT;
5508 <dt><b>author</b></dt>
5509 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5510 <dt><b>committer</b></dt>
5511 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5513 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5514 if ($have_pickaxe) {
5515 print <<EOT;
5516 <dt><b>pickaxe</b></dt>
5517 <dd>All commits that caused the string to appear or disappear from any file (changes that
5518 added, removed or "modified" the string) will be listed. This search can take a while and
5519 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5520 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5523 print "</dl>\n";
5524 git_footer_html();
5527 sub git_shortlog {
5528 my $head = git_get_head_hash($project);
5529 if (!defined $hash) {
5530 $hash = $head;
5532 if (!defined $page) {
5533 $page = 0;
5535 my $refs = git_get_references();
5537 my $commit_hash = $hash;
5538 if (defined $hash_parent) {
5539 $commit_hash = "$hash_parent..$hash";
5541 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
5543 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5544 my $next_link = '';
5545 if ($#commitlist >= 100) {
5546 $next_link =
5547 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5548 -accesskey => "n", -title => "Alt-n"}, "next");
5551 git_header_html();
5552 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5553 git_print_header_div('summary', $project);
5555 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5557 git_footer_html();
5560 ## ......................................................................
5561 ## feeds (RSS, Atom; OPML)
5563 sub git_feed {
5564 my $format = shift || 'atom';
5565 my ($have_blame) = gitweb_check_feature('blame');
5567 # Atom: http://www.atomenabled.org/developers/syndication/
5568 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5569 if ($format ne 'rss' && $format ne 'atom') {
5570 die_error(400, "Unknown web feed format");
5573 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5574 my $head = $hash || 'HEAD';
5575 my @commitlist = parse_commits($head, 150, 0, $file_name);
5577 my %latest_commit;
5578 my %latest_date;
5579 my $content_type = "application/$format+xml";
5580 if (defined $cgi->http('HTTP_ACCEPT') &&
5581 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5582 # browser (feed reader) prefers text/xml
5583 $content_type = 'text/xml';
5585 if (defined($commitlist[0])) {
5586 %latest_commit = %{$commitlist[0]};
5587 %latest_date = parse_date($latest_commit{'author_epoch'});
5588 print $cgi->header(
5589 -type => $content_type,
5590 -charset => 'utf-8',
5591 -last_modified => $latest_date{'rfc2822'});
5592 } else {
5593 print $cgi->header(
5594 -type => $content_type,
5595 -charset => 'utf-8');
5598 # Optimization: skip generating the body if client asks only
5599 # for Last-Modified date.
5600 return if ($cgi->request_method() eq 'HEAD');
5602 # header variables
5603 my $title = "$site_name - $project/$action";
5604 my $feed_type = 'log';
5605 if (defined $hash) {
5606 $title .= " - '$hash'";
5607 $feed_type = 'branch log';
5608 if (defined $file_name) {
5609 $title .= " :: $file_name";
5610 $feed_type = 'history';
5612 } elsif (defined $file_name) {
5613 $title .= " - $file_name";
5614 $feed_type = 'history';
5616 $title .= " $feed_type";
5617 my $descr = git_get_project_description($project);
5618 if (defined $descr) {
5619 $descr = esc_html($descr);
5620 } else {
5621 $descr = "$project " .
5622 ($format eq 'rss' ? 'RSS' : 'Atom') .
5623 " feed";
5625 my $owner = git_get_project_owner($project);
5626 $owner = esc_html($owner);
5628 #header
5629 my $alt_url;
5630 if (defined $file_name) {
5631 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5632 } elsif (defined $hash) {
5633 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5634 } else {
5635 $alt_url = href(-full=>1, action=>"summary");
5637 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5638 if ($format eq 'rss') {
5639 print <<XML;
5640 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5641 <channel>
5643 print "<title>$title</title>\n" .
5644 "<link>$alt_url</link>\n" .
5645 "<description>$descr</description>\n" .
5646 "<language>en</language>\n";
5647 } elsif ($format eq 'atom') {
5648 print <<XML;
5649 <feed xmlns="http://www.w3.org/2005/Atom">
5651 print "<title>$title</title>\n" .
5652 "<subtitle>$descr</subtitle>\n" .
5653 '<link rel="alternate" type="text/html" href="' .
5654 $alt_url . '" />' . "\n" .
5655 '<link rel="self" type="' . $content_type . '" href="' .
5656 $cgi->self_url() . '" />' . "\n" .
5657 "<id>" . href(-full=>1) . "</id>\n" .
5658 # use project owner for feed author
5659 "<author><name>$owner</name></author>\n";
5660 if (defined $favicon) {
5661 print "<icon>" . esc_url($favicon) . "</icon>\n";
5663 if (defined $logo_url) {
5664 # not twice as wide as tall: 72 x 27 pixels
5665 print "<logo>" . esc_url($logo) . "</logo>\n";
5667 if (! %latest_date) {
5668 # dummy date to keep the feed valid until commits trickle in:
5669 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5670 } else {
5671 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5675 # contents
5676 for (my $i = 0; $i <= $#commitlist; $i++) {
5677 my %co = %{$commitlist[$i]};
5678 my $commit = $co{'id'};
5679 # we read 150, we always show 30 and the ones more recent than 48 hours
5680 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5681 last;
5683 my %cd = parse_date($co{'author_epoch'});
5685 # get list of changed files
5686 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5687 $co{'parent'} || "--root",
5688 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5689 or next;
5690 my @difftree = map { chomp; $_ } <$fd>;
5691 close $fd
5692 or next;
5694 # print element (entry, item)
5695 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5696 if ($format eq 'rss') {
5697 print "<item>\n" .
5698 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5699 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5700 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5701 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5702 "<link>$co_url</link>\n" .
5703 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5704 "<content:encoded>" .
5705 "<![CDATA[\n";
5706 } elsif ($format eq 'atom') {
5707 print "<entry>\n" .
5708 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5709 "<updated>$cd{'iso-8601'}</updated>\n" .
5710 "<author>\n" .
5711 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5712 if ($co{'author_email'}) {
5713 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5715 print "</author>\n" .
5716 # use committer for contributor
5717 "<contributor>\n" .
5718 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5719 if ($co{'committer_email'}) {
5720 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5722 print "</contributor>\n" .
5723 "<published>$cd{'iso-8601'}</published>\n" .
5724 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5725 "<id>$co_url</id>\n" .
5726 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5727 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5729 my $comment = $co{'comment'};
5730 print "<pre>\n";
5731 foreach my $line (@$comment) {
5732 $line = esc_html($line);
5733 print "$line\n";
5735 print "</pre><ul>\n";
5736 foreach my $difftree_line (@difftree) {
5737 my %difftree = parse_difftree_raw_line($difftree_line);
5738 next if !$difftree{'from_id'};
5740 my $file = $difftree{'file'} || $difftree{'to_file'};
5742 print "<li>" .
5743 "[" .
5744 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5745 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5746 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5747 file_name=>$file, file_parent=>$difftree{'from_file'}),
5748 -title => "diff"}, 'D');
5749 if ($have_blame) {
5750 print $cgi->a({-href => href(-full=>1, action=>"blame",
5751 file_name=>$file, hash_base=>$commit),
5752 -title => "blame"}, 'B');
5754 # if this is not a feed of a file history
5755 if (!defined $file_name || $file_name ne $file) {
5756 print $cgi->a({-href => href(-full=>1, action=>"history",
5757 file_name=>$file, hash=>$commit),
5758 -title => "history"}, 'H');
5760 $file = esc_path($file);
5761 print "] ".
5762 "$file</li>\n";
5764 if ($format eq 'rss') {
5765 print "</ul>]]>\n" .
5766 "</content:encoded>\n" .
5767 "</item>\n";
5768 } elsif ($format eq 'atom') {
5769 print "</ul>\n</div>\n" .
5770 "</content>\n" .
5771 "</entry>\n";
5775 # end of feed
5776 if ($format eq 'rss') {
5777 print "</channel>\n</rss>\n";
5778 } elsif ($format eq 'atom') {
5779 print "</feed>\n";
5783 sub git_rss {
5784 git_feed('rss');
5787 sub git_atom {
5788 git_feed('atom');
5791 sub git_opml {
5792 my @list = git_get_projects_list();
5794 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5795 print <<XML;
5796 <?xml version="1.0" encoding="utf-8"?>
5797 <opml version="1.0">
5798 <head>
5799 <title>$site_name OPML Export</title>
5800 </head>
5801 <body>
5802 <outline text="git RSS feeds">
5805 foreach my $pr (@list) {
5806 my %proj = %$pr;
5807 my $head = git_get_head_hash($proj{'path'});
5808 if (!defined $head) {
5809 next;
5811 $git_dir = "$projectroot/$proj{'path'}";
5812 my %co = parse_commit($head);
5813 if (!%co) {
5814 next;
5817 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5818 my $rss = "$my_url?p=$proj{'path'};a=rss";
5819 my $html = "$my_url?p=$proj{'path'};a=summary";
5820 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5822 print <<XML;
5823 </outline>
5824 </body>
5825 </opml>