Introduce gitweb.js file
[git/gitweb.git] / gitweb / gitweb.perl
blobed1ce0b713d225efda4fb54809fee7c5dc896aea
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 # core git executable to use
31 # this can just be "git" if your webserver has a sensible PATH
32 our $GIT = "++GIT_BINDIR++/git";
34 # absolute fs-path which will be prepended to the project path
35 #our $projectroot = "/pub/scm";
36 our $projectroot = "++GITWEB_PROJECTROOT++";
38 # fs traversing limit for getting project list
39 # the number is relative to the projectroot
40 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
42 # target of the home link on top of all pages
43 our $home_link = $my_uri || "/";
45 # string of the home link on top of all pages
46 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
48 # name of your site or organization to appear in page titles
49 # replace this with something more descriptive for clearer bookmarks
50 our $site_name = "++GITWEB_SITENAME++"
51 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
53 # filename of html text to include at top of each page
54 our $site_header = "++GITWEB_SITE_HEADER++";
55 # html text to include at home page
56 our $home_text = "++GITWEB_HOMETEXT++";
57 # filename of html text to include at bottom of each page
58 our $site_footer = "++GITWEB_SITE_FOOTER++";
60 # URI of stylesheets
61 our @stylesheets = ("++GITWEB_CSS++");
62 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
63 our $stylesheet = undef;
64 # URI of GIT logo (72x27 size)
65 our $logo = "++GITWEB_LOGO++";
66 # URI of GIT favicon, assumed to be image/png type
67 our $favicon = "++GITWEB_FAVICON++";
68 # URI of gitweb.js
69 our $gitwebjs = "++GITWEB_GITWEBJS++";
71 # URI and label (title) of GIT logo link
72 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
73 #our $logo_label = "git documentation";
74 our $logo_url = "http://git.or.cz/";
75 our $logo_label = "git homepage";
77 # source of projects list
78 our $projects_list = "++GITWEB_LIST++";
80 # the width (in characters) of the projects list "Description" column
81 our $projects_list_description_width = 25;
83 # default order of projects list
84 # valid values are none, project, descr, owner, and age
85 our $default_projects_order = "project";
87 # show repository only if this file exists
88 # (only effective if this variable evaluates to true)
89 our $export_ok = "++GITWEB_EXPORT_OK++";
91 # only allow viewing of repositories also shown on the overview page
92 our $strict_export = "++GITWEB_STRICT_EXPORT++";
94 # list of git base URLs used for URL to where fetch project from,
95 # i.e. full URL is "$git_base_url/$project"
96 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
98 # default blob_plain mimetype and default charset for text/plain blob
99 our $default_blob_plain_mimetype = 'text/plain';
100 our $default_text_plain_charset = undef;
102 # file to use for guessing MIME types before trying /etc/mime.types
103 # (relative to the current git repository)
104 our $mimetypes_file = undef;
106 # assume this charset if line contains non-UTF-8 characters;
107 # it should be valid encoding (see Encoding::Supported(3pm) for list),
108 # for which encoding all byte sequences are valid, for example
109 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
110 # could be even 'utf-8' for the old behavior)
111 our $fallback_encoding = 'latin1';
113 # rename detection options for git-diff and git-diff-tree
114 # - default is '-M', with the cost proportional to
115 # (number of removed files) * (number of new files).
116 # - more costly is '-C' (which implies '-M'), with the cost proportional to
117 # (number of changed files + number of removed files) * (number of new files)
118 # - even more costly is '-C', '--find-copies-harder' with cost
119 # (number of files in the original tree) * (number of new files)
120 # - one might want to include '-B' option, e.g. '-B', '-M'
121 our @diff_opts = ('-M'); # taken from git_commit
123 # information about snapshot formats that gitweb is capable of serving
124 our %known_snapshot_formats = (
125 # name => {
126 # 'display' => display name,
127 # 'type' => mime type,
128 # 'suffix' => filename suffix,
129 # 'format' => --format for git-archive,
130 # 'compressor' => [compressor command and arguments]
131 # (array reference, optional)}
133 'tgz' => {
134 'display' => 'tar.gz',
135 'type' => 'application/x-gzip',
136 'suffix' => '.tar.gz',
137 'format' => 'tar',
138 'compressor' => ['gzip']},
140 'tbz2' => {
141 'display' => 'tar.bz2',
142 'type' => 'application/x-bzip2',
143 'suffix' => '.tar.bz2',
144 'format' => 'tar',
145 'compressor' => ['bzip2']},
147 'zip' => {
148 'display' => 'zip',
149 'type' => 'application/x-zip',
150 'suffix' => '.zip',
151 'format' => 'zip'},
154 # Aliases so we understand old gitweb.snapshot values in repository
155 # configuration.
156 our %known_snapshot_format_aliases = (
157 'gzip' => 'tgz',
158 'bzip2' => 'tbz2',
160 # backward compatibility: legacy gitweb config support
161 'x-gzip' => undef, 'gz' => undef,
162 'x-bzip2' => undef, 'bz2' => undef,
163 'x-zip' => undef, '' => undef,
166 # You define site-wide feature defaults here; override them with
167 # $GITWEB_CONFIG as necessary.
168 our %feature = (
169 # feature => {
170 # 'sub' => feature-sub (subroutine),
171 # 'override' => allow-override (boolean),
172 # 'default' => [ default options...] (array reference)}
174 # if feature is overridable (it means that allow-override has true value),
175 # then feature-sub will be called with default options as parameters;
176 # return value of feature-sub indicates if to enable specified feature
178 # if there is no 'sub' key (no feature-sub), then feature cannot be
179 # overriden
181 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
183 # Enable the 'blame' blob view, showing the last commit that modified
184 # each line in the file. This can be very CPU-intensive.
186 # To enable system wide have in $GITWEB_CONFIG
187 # $feature{'blame'}{'default'} = [1];
188 # To have project specific config enable override in $GITWEB_CONFIG
189 # $feature{'blame'}{'override'} = 1;
190 # and in project config gitweb.blame = 0|1;
191 'blame' => {
192 'sub' => \&feature_blame,
193 'override' => 0,
194 'default' => [0]},
196 # Enable the 'snapshot' link, providing a compressed archive of any
197 # tree. This can potentially generate high traffic if you have large
198 # project.
200 # Value is a list of formats defined in %known_snapshot_formats that
201 # you wish to offer.
202 # To disable system wide have in $GITWEB_CONFIG
203 # $feature{'snapshot'}{'default'} = [];
204 # To have project specific config enable override in $GITWEB_CONFIG
205 # $feature{'snapshot'}{'override'} = 1;
206 # and in project config, a comma-separated list of formats or "none"
207 # to disable. Example: gitweb.snapshot = tbz2,zip;
208 'snapshot' => {
209 'sub' => \&feature_snapshot,
210 'override' => 0,
211 'default' => ['tgz']},
213 # Enable text search, which will list the commits which match author,
214 # committer or commit text to a given string. Enabled by default.
215 # Project specific override is not supported.
216 'search' => {
217 'override' => 0,
218 'default' => [1]},
220 # Enable grep search, which will list the files in currently selected
221 # tree containing the given string. Enabled by default. This can be
222 # potentially CPU-intensive, of course.
224 # To enable system wide have in $GITWEB_CONFIG
225 # $feature{'grep'}{'default'} = [1];
226 # To have project specific config enable override in $GITWEB_CONFIG
227 # $feature{'grep'}{'override'} = 1;
228 # and in project config gitweb.grep = 0|1;
229 'grep' => {
230 'override' => 0,
231 'default' => [1]},
233 # Enable the pickaxe search, which will list the commits that modified
234 # a given string in a file. This can be practical and quite faster
235 # alternative to 'blame', but still potentially CPU-intensive.
237 # To enable system wide have in $GITWEB_CONFIG
238 # $feature{'pickaxe'}{'default'} = [1];
239 # To have project specific config enable override in $GITWEB_CONFIG
240 # $feature{'pickaxe'}{'override'} = 1;
241 # and in project config gitweb.pickaxe = 0|1;
242 'pickaxe' => {
243 'sub' => \&feature_pickaxe,
244 'override' => 0,
245 'default' => [1]},
247 # Make gitweb use an alternative format of the URLs which can be
248 # more readable and natural-looking: project name is embedded
249 # directly in the path and the query string contains other
250 # auxiliary information. All gitweb installations recognize
251 # URL in either format; this configures in which formats gitweb
252 # generates links.
254 # To enable system wide have in $GITWEB_CONFIG
255 # $feature{'pathinfo'}{'default'} = [1];
256 # Project specific override is not supported.
258 # Note that you will need to change the default location of CSS,
259 # favicon, logo and possibly other files to an absolute URL. Also,
260 # if gitweb.cgi serves as your indexfile, you will need to force
261 # $my_uri to contain the script name in your $GITWEB_CONFIG.
262 'pathinfo' => {
263 'override' => 0,
264 'default' => [0]},
266 # Make gitweb consider projects in project root subdirectories
267 # to be forks of existing projects. Given project $projname.git,
268 # projects matching $projname/*.git will not be shown in the main
269 # projects list, instead a '+' mark will be added to $projname
270 # there and a 'forks' view will be enabled for the project, listing
271 # all the forks. If project list is taken from a file, forks have
272 # to be listed after the main project.
274 # To enable system wide have in $GITWEB_CONFIG
275 # $feature{'forks'}{'default'} = [1];
276 # Project specific override is not supported.
277 'forks' => {
278 'override' => 0,
279 'default' => [0]},
282 sub gitweb_check_feature {
283 my ($name) = @_;
284 return unless exists $feature{$name};
285 my ($sub, $override, @defaults) = (
286 $feature{$name}{'sub'},
287 $feature{$name}{'override'},
288 @{$feature{$name}{'default'}});
289 if (!$override) { return @defaults; }
290 if (!defined $sub) {
291 warn "feature $name is not overrideable";
292 return @defaults;
294 return $sub->(@defaults);
297 sub feature_blame {
298 my ($val) = git_get_project_config('blame', '--bool');
300 if ($val eq 'true') {
301 return 1;
302 } elsif ($val eq 'false') {
303 return 0;
306 return $_[0];
309 sub feature_snapshot {
310 my (@fmts) = @_;
312 my ($val) = git_get_project_config('snapshot');
314 if ($val) {
315 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
318 return @fmts;
321 sub feature_grep {
322 my ($val) = git_get_project_config('grep', '--bool');
324 if ($val eq 'true') {
325 return (1);
326 } elsif ($val eq 'false') {
327 return (0);
330 return ($_[0]);
333 sub feature_pickaxe {
334 my ($val) = git_get_project_config('pickaxe', '--bool');
336 if ($val eq 'true') {
337 return (1);
338 } elsif ($val eq 'false') {
339 return (0);
342 return ($_[0]);
345 # checking HEAD file with -e is fragile if the repository was
346 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
347 # and then pruned.
348 sub check_head_link {
349 my ($dir) = @_;
350 my $headfile = "$dir/HEAD";
351 return ((-e $headfile) ||
352 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
355 sub check_export_ok {
356 my ($dir) = @_;
357 return (check_head_link($dir) &&
358 (!$export_ok || -e "$dir/$export_ok"));
361 # process alternate names for backward compatibility
362 # filter out unsupported (unknown) snapshot formats
363 sub filter_snapshot_fmts {
364 my @fmts = @_;
366 @fmts = map {
367 exists $known_snapshot_format_aliases{$_} ?
368 $known_snapshot_format_aliases{$_} : $_} @fmts;
369 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
373 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
374 if (-e $GITWEB_CONFIG) {
375 do $GITWEB_CONFIG;
376 } else {
377 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
378 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
381 # version of the core git binary
382 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
384 $projects_list ||= $projectroot;
386 # ======================================================================
387 # input validation and dispatch
388 our $action = $cgi->param('a');
389 if (defined $action) {
390 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
391 die_error(400, "Invalid action parameter");
395 # parameters which are pathnames
396 our $project = $cgi->param('p');
397 if (defined $project) {
398 if (!validate_pathname($project) ||
399 !(-d "$projectroot/$project") ||
400 !check_head_link("$projectroot/$project") ||
401 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
402 ($strict_export && !project_in_list($project))) {
403 undef $project;
404 die_error(404, "No such project");
408 our $file_name = $cgi->param('f');
409 if (defined $file_name) {
410 if (!validate_pathname($file_name)) {
411 die_error(400, "Invalid file parameter");
415 our $file_parent = $cgi->param('fp');
416 if (defined $file_parent) {
417 if (!validate_pathname($file_parent)) {
418 die_error(400, "Invalid file parent parameter");
422 # parameters which are refnames
423 our $hash = $cgi->param('h');
424 if (defined $hash) {
425 if (!validate_refname($hash)) {
426 die_error(400, "Invalid hash parameter");
430 our $hash_parent = $cgi->param('hp');
431 if (defined $hash_parent) {
432 if (!validate_refname($hash_parent)) {
433 die_error(400, "Invalid hash parent parameter");
437 our $hash_base = $cgi->param('hb');
438 if (defined $hash_base) {
439 if (!validate_refname($hash_base)) {
440 die_error(400, "Invalid hash base parameter");
444 my %allowed_options = (
445 "--no-merges" => [ qw(rss atom log shortlog history) ],
448 our @extra_options = $cgi->param('opt');
449 if (defined @extra_options) {
450 foreach my $opt (@extra_options) {
451 if (not exists $allowed_options{$opt}) {
452 die_error(400, "Invalid option parameter");
454 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
455 die_error(400, "Invalid option parameter for this action");
460 our $hash_parent_base = $cgi->param('hpb');
461 if (defined $hash_parent_base) {
462 if (!validate_refname($hash_parent_base)) {
463 die_error(400, "Invalid hash parent base parameter");
467 # other parameters
468 our $page = $cgi->param('pg');
469 if (defined $page) {
470 if ($page =~ m/[^0-9]/) {
471 die_error(400, "Invalid page parameter");
475 our $searchtype = $cgi->param('st');
476 if (defined $searchtype) {
477 if ($searchtype =~ m/[^a-z]/) {
478 die_error(400, "Invalid searchtype parameter");
482 our $search_use_regexp = $cgi->param('sr');
484 our $searchtext = $cgi->param('s');
485 our $search_regexp;
486 if (defined $searchtext) {
487 if (length($searchtext) < 2) {
488 die_error(403, "At least two characters are required for search parameter");
490 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
493 # now read PATH_INFO and use it as alternative to parameters
494 sub evaluate_path_info {
495 return if defined $project;
496 my $path_info = $ENV{"PATH_INFO"};
497 return if !$path_info;
498 $path_info =~ s,^/+,,;
499 return if !$path_info;
500 # find which part of PATH_INFO is project
501 $project = $path_info;
502 $project =~ s,/+$,,;
503 while ($project && !check_head_link("$projectroot/$project")) {
504 $project =~ s,/*[^/]*$,,;
506 # validate project
507 $project = validate_pathname($project);
508 if (!$project ||
509 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
510 ($strict_export && !project_in_list($project))) {
511 undef $project;
512 return;
514 # do not change any parameters if an action is given using the query string
515 return if $action;
516 $path_info =~ s,^\Q$project\E/*,,;
517 my ($refname, $pathname) = split(/:/, $path_info, 2);
518 if (defined $pathname) {
519 # we got "project.git/branch:filename" or "project.git/branch:dir/"
520 # we could use git_get_type(branch:pathname), but it needs $git_dir
521 $pathname =~ s,^/+,,;
522 if (!$pathname || substr($pathname, -1) eq "/") {
523 $action ||= "tree";
524 $pathname =~ s,/$,,;
525 } else {
526 $action ||= "blob_plain";
528 $hash_base ||= validate_refname($refname);
529 $file_name ||= validate_pathname($pathname);
530 } elsif (defined $refname) {
531 # we got "project.git/branch"
532 $action ||= "shortlog";
533 $hash ||= validate_refname($refname);
536 evaluate_path_info();
538 # path to the current git repository
539 our $git_dir;
540 $git_dir = "$projectroot/$project" if $project;
542 # dispatch
543 my %actions = (
544 "blame" => \&git_blame,
545 "blobdiff" => \&git_blobdiff,
546 "blobdiff_plain" => \&git_blobdiff_plain,
547 "blob" => \&git_blob,
548 "blob_plain" => \&git_blob_plain,
549 "commitdiff" => \&git_commitdiff,
550 "commitdiff_plain" => \&git_commitdiff_plain,
551 "commit" => \&git_commit,
552 "forks" => \&git_forks,
553 "heads" => \&git_heads,
554 "history" => \&git_history,
555 "log" => \&git_log,
556 "rss" => \&git_rss,
557 "atom" => \&git_atom,
558 "search" => \&git_search,
559 "search_help" => \&git_search_help,
560 "shortlog" => \&git_shortlog,
561 "summary" => \&git_summary,
562 "tag" => \&git_tag,
563 "tags" => \&git_tags,
564 "tree" => \&git_tree,
565 "snapshot" => \&git_snapshot,
566 "object" => \&git_object,
567 # those below don't need $project
568 "opml" => \&git_opml,
569 "project_list" => \&git_project_list,
570 "project_index" => \&git_project_index,
573 if (!defined $action) {
574 if (defined $hash) {
575 $action = git_get_type($hash);
576 } elsif (defined $hash_base && defined $file_name) {
577 $action = git_get_type("$hash_base:$file_name");
578 } elsif (defined $project) {
579 $action = 'summary';
580 } else {
581 $action = 'project_list';
584 if (!defined($actions{$action})) {
585 die_error(400, "Unknown action");
587 if ($action !~ m/^(opml|project_list|project_index)$/ &&
588 !$project) {
589 die_error(400, "Project needed");
591 $actions{$action}->();
592 exit;
594 ## ======================================================================
595 ## action links
597 sub href (%) {
598 my %params = @_;
599 # default is to use -absolute url() i.e. $my_uri
600 my $href = $params{-full} ? $my_url : $my_uri;
602 # XXX: Warning: If you touch this, check the search form for updating,
603 # too.
605 my @mapping = (
606 project => "p",
607 action => "a",
608 file_name => "f",
609 file_parent => "fp",
610 hash => "h",
611 hash_parent => "hp",
612 hash_base => "hb",
613 hash_parent_base => "hpb",
614 page => "pg",
615 order => "o",
616 searchtext => "s",
617 searchtype => "st",
618 snapshot_format => "sf",
619 extra_options => "opt",
620 search_use_regexp => "sr",
622 my %mapping = @mapping;
624 $params{'project'} = $project unless exists $params{'project'};
626 if ($params{-replay}) {
627 while (my ($name, $symbol) = each %mapping) {
628 if (!exists $params{$name}) {
629 # to allow for multivalued params we use arrayref form
630 $params{$name} = [ $cgi->param($symbol) ];
635 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
636 if ($use_pathinfo) {
637 # use PATH_INFO for project name
638 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
639 delete $params{'project'};
641 # Summary just uses the project path URL
642 if (defined $params{'action'} && $params{'action'} eq 'summary') {
643 delete $params{'action'};
647 # now encode the parameters explicitly
648 my @result = ();
649 for (my $i = 0; $i < @mapping; $i += 2) {
650 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
651 if (defined $params{$name}) {
652 if (ref($params{$name}) eq "ARRAY") {
653 foreach my $par (@{$params{$name}}) {
654 push @result, $symbol . "=" . esc_param($par);
656 } else {
657 push @result, $symbol . "=" . esc_param($params{$name});
661 $href .= "?" . join(';', @result) if scalar @result;
663 return $href;
667 ## ======================================================================
668 ## validation, quoting/unquoting and escaping
670 sub validate_pathname {
671 my $input = shift || return undef;
673 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
674 # at the beginning, at the end, and between slashes.
675 # also this catches doubled slashes
676 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
677 return undef;
679 # no null characters
680 if ($input =~ m!\0!) {
681 return undef;
683 return $input;
686 sub validate_refname {
687 my $input = shift || return undef;
689 # textual hashes are O.K.
690 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
691 return $input;
693 # it must be correct pathname
694 $input = validate_pathname($input)
695 or return undef;
696 # restrictions on ref name according to git-check-ref-format
697 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
698 return undef;
700 return $input;
703 # decode sequences of octets in utf8 into Perl's internal form,
704 # which is utf-8 with utf8 flag set if needed. gitweb writes out
705 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
706 sub to_utf8 {
707 my $str = shift;
708 if (utf8::valid($str)) {
709 utf8::decode($str);
710 return $str;
711 } else {
712 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
716 # quote unsafe chars, but keep the slash, even when it's not
717 # correct, but quoted slashes look too horrible in bookmarks
718 sub esc_param {
719 my $str = shift;
720 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
721 $str =~ s/\+/%2B/g;
722 $str =~ s/ /\+/g;
723 return $str;
726 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
727 sub esc_url {
728 my $str = shift;
729 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
730 $str =~ s/\+/%2B/g;
731 $str =~ s/ /\+/g;
732 return $str;
735 # replace invalid utf8 character with SUBSTITUTION sequence
736 sub esc_html ($;%) {
737 my $str = shift;
738 my %opts = @_;
740 $str = to_utf8($str);
741 $str = $cgi->escapeHTML($str);
742 if ($opts{'-nbsp'}) {
743 $str =~ s/ /&nbsp;/g;
745 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
746 return $str;
749 # quote control characters and escape filename to HTML
750 sub esc_path {
751 my $str = shift;
752 my %opts = @_;
754 $str = to_utf8($str);
755 $str = $cgi->escapeHTML($str);
756 if ($opts{'-nbsp'}) {
757 $str =~ s/ /&nbsp;/g;
759 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
760 return $str;
763 # Make control characters "printable", using character escape codes (CEC)
764 sub quot_cec {
765 my $cntrl = shift;
766 my %opts = @_;
767 my %es = ( # character escape codes, aka escape sequences
768 "\t" => '\t', # tab (HT)
769 "\n" => '\n', # line feed (LF)
770 "\r" => '\r', # carrige return (CR)
771 "\f" => '\f', # form feed (FF)
772 "\b" => '\b', # backspace (BS)
773 "\a" => '\a', # alarm (bell) (BEL)
774 "\e" => '\e', # escape (ESC)
775 "\013" => '\v', # vertical tab (VT)
776 "\000" => '\0', # nul character (NUL)
778 my $chr = ( (exists $es{$cntrl})
779 ? $es{$cntrl}
780 : sprintf('\%03o', ord($cntrl)) );
781 if ($opts{-nohtml}) {
782 return $chr;
783 } else {
784 return "<span class=\"cntrl\">$chr</span>";
788 # Alternatively use unicode control pictures codepoints,
789 # Unicode "printable representation" (PR)
790 sub quot_upr {
791 my $cntrl = shift;
792 my %opts = @_;
794 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
795 if ($opts{-nohtml}) {
796 return $chr;
797 } else {
798 return "<span class=\"cntrl\">$chr</span>";
802 # git may return quoted and escaped filenames
803 sub unquote {
804 my $str = shift;
806 sub unq {
807 my $seq = shift;
808 my %es = ( # character escape codes, aka escape sequences
809 't' => "\t", # tab (HT, TAB)
810 'n' => "\n", # newline (NL)
811 'r' => "\r", # return (CR)
812 'f' => "\f", # form feed (FF)
813 'b' => "\b", # backspace (BS)
814 'a' => "\a", # alarm (bell) (BEL)
815 'e' => "\e", # escape (ESC)
816 'v' => "\013", # vertical tab (VT)
819 if ($seq =~ m/^[0-7]{1,3}$/) {
820 # octal char sequence
821 return chr(oct($seq));
822 } elsif (exists $es{$seq}) {
823 # C escape sequence, aka character escape code
824 return $es{$seq};
826 # quoted ordinary character
827 return $seq;
830 if ($str =~ m/^"(.*)"$/) {
831 # needs unquoting
832 $str = $1;
833 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
835 return $str;
838 # escape tabs (convert tabs to spaces)
839 sub untabify {
840 my $line = shift;
842 while ((my $pos = index($line, "\t")) != -1) {
843 if (my $count = (8 - ($pos % 8))) {
844 my $spaces = ' ' x $count;
845 $line =~ s/\t/$spaces/;
849 return $line;
852 sub project_in_list {
853 my $project = shift;
854 my @list = git_get_projects_list();
855 return @list && scalar(grep { $_->{'path'} eq $project } @list);
858 ## ----------------------------------------------------------------------
859 ## HTML aware string manipulation
861 # Try to chop given string on a word boundary between position
862 # $len and $len+$add_len. If there is no word boundary there,
863 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
864 # (marking chopped part) would be longer than given string.
865 sub chop_str {
866 my $str = shift;
867 my $len = shift;
868 my $add_len = shift || 10;
869 my $where = shift || 'right'; # 'left' | 'center' | 'right'
871 # Make sure perl knows it is utf8 encoded so we don't
872 # cut in the middle of a utf8 multibyte char.
873 $str = to_utf8($str);
875 # allow only $len chars, but don't cut a word if it would fit in $add_len
876 # if it doesn't fit, cut it if it's still longer than the dots we would add
877 # remove chopped character entities entirely
879 # when chopping in the middle, distribute $len into left and right part
880 # return early if chopping wouldn't make string shorter
881 if ($where eq 'center') {
882 return $str if ($len + 5 >= length($str)); # filler is length 5
883 $len = int($len/2);
884 } else {
885 return $str if ($len + 4 >= length($str)); # filler is length 4
888 # regexps: ending and beginning with word part up to $add_len
889 my $endre = qr/.{$len}\w{0,$add_len}/;
890 my $begre = qr/\w{0,$add_len}.{$len}/;
892 if ($where eq 'left') {
893 $str =~ m/^(.*?)($begre)$/;
894 my ($lead, $body) = ($1, $2);
895 if (length($lead) > 4) {
896 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
897 $lead = " ...";
899 return "$lead$body";
901 } elsif ($where eq 'center') {
902 $str =~ m/^($endre)(.*)$/;
903 my ($left, $str) = ($1, $2);
904 $str =~ m/^(.*?)($begre)$/;
905 my ($mid, $right) = ($1, $2);
906 if (length($mid) > 5) {
907 $left =~ s/&[^;]*$//;
908 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
909 $mid = " ... ";
911 return "$left$mid$right";
913 } else {
914 $str =~ m/^($endre)(.*)$/;
915 my $body = $1;
916 my $tail = $2;
917 if (length($tail) > 4) {
918 $body =~ s/&[^;]*$//;
919 $tail = "... ";
921 return "$body$tail";
925 # takes the same arguments as chop_str, but also wraps a <span> around the
926 # result with a title attribute if it does get chopped. Additionally, the
927 # string is HTML-escaped.
928 sub chop_and_escape_str {
929 my ($str) = @_;
931 my $chopped = chop_str(@_);
932 if ($chopped eq $str) {
933 return esc_html($chopped);
934 } else {
935 $str =~ s/([[:cntrl:]])/?/g;
936 return $cgi->span({-title=>$str}, esc_html($chopped));
940 ## ----------------------------------------------------------------------
941 ## functions returning short strings
943 # CSS class for given age value (in seconds)
944 sub age_class {
945 my $age = shift;
947 if (!defined $age) {
948 return "noage";
949 } elsif ($age < 60*60*2) {
950 return "age0";
951 } elsif ($age < 60*60*24*2) {
952 return "age1";
953 } else {
954 return "age2";
958 # convert age in seconds to "nn units ago" string
959 sub age_string {
960 my $age = shift;
961 my $age_str;
963 if ($age > 60*60*24*365*2) {
964 $age_str = (int $age/60/60/24/365);
965 $age_str .= " years ago";
966 } elsif ($age > 60*60*24*(365/12)*2) {
967 $age_str = int $age/60/60/24/(365/12);
968 $age_str .= " months ago";
969 } elsif ($age > 60*60*24*7*2) {
970 $age_str = int $age/60/60/24/7;
971 $age_str .= " weeks ago";
972 } elsif ($age > 60*60*24*2) {
973 $age_str = int $age/60/60/24;
974 $age_str .= " days ago";
975 } elsif ($age > 60*60*2) {
976 $age_str = int $age/60/60;
977 $age_str .= " hours ago";
978 } elsif ($age > 60*2) {
979 $age_str = int $age/60;
980 $age_str .= " min ago";
981 } elsif ($age > 2) {
982 $age_str = int $age;
983 $age_str .= " sec ago";
984 } else {
985 $age_str .= " right now";
987 return $age_str;
990 use constant {
991 S_IFINVALID => 0030000,
992 S_IFGITLINK => 0160000,
995 # submodule/subproject, a commit object reference
996 sub S_ISGITLINK($) {
997 my $mode = shift;
999 return (($mode & S_IFMT) == S_IFGITLINK)
1002 # convert file mode in octal to symbolic file mode string
1003 sub mode_str {
1004 my $mode = oct shift;
1006 if (S_ISGITLINK($mode)) {
1007 return 'm---------';
1008 } elsif (S_ISDIR($mode & S_IFMT)) {
1009 return 'drwxr-xr-x';
1010 } elsif (S_ISLNK($mode)) {
1011 return 'lrwxrwxrwx';
1012 } elsif (S_ISREG($mode)) {
1013 # git cares only about the executable bit
1014 if ($mode & S_IXUSR) {
1015 return '-rwxr-xr-x';
1016 } else {
1017 return '-rw-r--r--';
1019 } else {
1020 return '----------';
1024 # convert file mode in octal to file type string
1025 sub file_type {
1026 my $mode = shift;
1028 if ($mode !~ m/^[0-7]+$/) {
1029 return $mode;
1030 } else {
1031 $mode = oct $mode;
1034 if (S_ISGITLINK($mode)) {
1035 return "submodule";
1036 } elsif (S_ISDIR($mode & S_IFMT)) {
1037 return "directory";
1038 } elsif (S_ISLNK($mode)) {
1039 return "symlink";
1040 } elsif (S_ISREG($mode)) {
1041 return "file";
1042 } else {
1043 return "unknown";
1047 # convert file mode in octal to file type description string
1048 sub file_type_long {
1049 my $mode = shift;
1051 if ($mode !~ m/^[0-7]+$/) {
1052 return $mode;
1053 } else {
1054 $mode = oct $mode;
1057 if (S_ISGITLINK($mode)) {
1058 return "submodule";
1059 } elsif (S_ISDIR($mode & S_IFMT)) {
1060 return "directory";
1061 } elsif (S_ISLNK($mode)) {
1062 return "symlink";
1063 } elsif (S_ISREG($mode)) {
1064 if ($mode & S_IXUSR) {
1065 return "executable";
1066 } else {
1067 return "file";
1069 } else {
1070 return "unknown";
1075 ## ----------------------------------------------------------------------
1076 ## functions returning short HTML fragments, or transforming HTML fragments
1077 ## which don't belong to other sections
1079 # format line of commit message.
1080 sub format_log_line_html {
1081 my $line = shift;
1083 $line = esc_html($line, -nbsp=>1);
1084 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1085 my $hash_text = $1;
1086 my $link =
1087 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1088 -class => "text"}, $hash_text);
1089 $line =~ s/$hash_text/$link/;
1091 return $line;
1094 # format marker of refs pointing to given object
1095 sub format_ref_marker {
1096 my ($refs, $id) = @_;
1097 my $markers = '';
1099 if (defined $refs->{$id}) {
1100 foreach my $ref (@{$refs->{$id}}) {
1101 my ($type, $name) = qw();
1102 # e.g. tags/v2.6.11 or heads/next
1103 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1104 $type = $1;
1105 $name = $2;
1106 } else {
1107 $type = "ref";
1108 $name = $ref;
1111 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1112 esc_html($name) . "</span>";
1116 if ($markers) {
1117 return ' <span class="refs">'. $markers . '</span>';
1118 } else {
1119 return "";
1123 # format, perhaps shortened and with markers, title line
1124 sub format_subject_html {
1125 my ($long, $short, $href, $extra) = @_;
1126 $extra = '' unless defined($extra);
1128 if (length($short) < length($long)) {
1129 return $cgi->a({-href => $href, -class => "list subject",
1130 -title => to_utf8($long)},
1131 esc_html($short) . $extra);
1132 } else {
1133 return $cgi->a({-href => $href, -class => "list subject"},
1134 esc_html($long) . $extra);
1138 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1139 sub format_git_diff_header_line {
1140 my $line = shift;
1141 my $diffinfo = shift;
1142 my ($from, $to) = @_;
1144 if ($diffinfo->{'nparents'}) {
1145 # combined diff
1146 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1147 if ($to->{'href'}) {
1148 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1149 esc_path($to->{'file'}));
1150 } else { # file was deleted (no href)
1151 $line .= esc_path($to->{'file'});
1153 } else {
1154 # "ordinary" diff
1155 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1156 if ($from->{'href'}) {
1157 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1158 'a/' . esc_path($from->{'file'}));
1159 } else { # file was added (no href)
1160 $line .= 'a/' . esc_path($from->{'file'});
1162 $line .= ' ';
1163 if ($to->{'href'}) {
1164 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1165 'b/' . esc_path($to->{'file'}));
1166 } else { # file was deleted
1167 $line .= 'b/' . esc_path($to->{'file'});
1171 return "<div class=\"diff header\">$line</div>\n";
1174 # format extended diff header line, before patch itself
1175 sub format_extended_diff_header_line {
1176 my $line = shift;
1177 my $diffinfo = shift;
1178 my ($from, $to) = @_;
1180 # match <path>
1181 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1182 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1183 esc_path($from->{'file'}));
1185 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1186 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1187 esc_path($to->{'file'}));
1189 # match single <mode>
1190 if ($line =~ m/\s(\d{6})$/) {
1191 $line .= '<span class="info"> (' .
1192 file_type_long($1) .
1193 ')</span>';
1195 # match <hash>
1196 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1197 # can match only for combined diff
1198 $line = 'index ';
1199 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1200 if ($from->{'href'}[$i]) {
1201 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1202 -class=>"hash"},
1203 substr($diffinfo->{'from_id'}[$i],0,7));
1204 } else {
1205 $line .= '0' x 7;
1207 # separator
1208 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1210 $line .= '..';
1211 if ($to->{'href'}) {
1212 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1213 substr($diffinfo->{'to_id'},0,7));
1214 } else {
1215 $line .= '0' x 7;
1218 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1219 # can match only for ordinary diff
1220 my ($from_link, $to_link);
1221 if ($from->{'href'}) {
1222 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1223 substr($diffinfo->{'from_id'},0,7));
1224 } else {
1225 $from_link = '0' x 7;
1227 if ($to->{'href'}) {
1228 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1229 substr($diffinfo->{'to_id'},0,7));
1230 } else {
1231 $to_link = '0' x 7;
1233 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1234 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1237 return $line . "<br/>\n";
1240 # format from-file/to-file diff header
1241 sub format_diff_from_to_header {
1242 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1243 my $line;
1244 my $result = '';
1246 $line = $from_line;
1247 #assert($line =~ m/^---/) if DEBUG;
1248 # no extra formatting for "^--- /dev/null"
1249 if (! $diffinfo->{'nparents'}) {
1250 # ordinary (single parent) diff
1251 if ($line =~ m!^--- "?a/!) {
1252 if ($from->{'href'}) {
1253 $line = '--- a/' .
1254 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1255 esc_path($from->{'file'}));
1256 } else {
1257 $line = '--- a/' .
1258 esc_path($from->{'file'});
1261 $result .= qq!<div class="diff from_file">$line</div>\n!;
1263 } else {
1264 # combined diff (merge commit)
1265 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1266 if ($from->{'href'}[$i]) {
1267 $line = '--- ' .
1268 $cgi->a({-href=>href(action=>"blobdiff",
1269 hash_parent=>$diffinfo->{'from_id'}[$i],
1270 hash_parent_base=>$parents[$i],
1271 file_parent=>$from->{'file'}[$i],
1272 hash=>$diffinfo->{'to_id'},
1273 hash_base=>$hash,
1274 file_name=>$to->{'file'}),
1275 -class=>"path",
1276 -title=>"diff" . ($i+1)},
1277 $i+1) .
1278 '/' .
1279 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1280 esc_path($from->{'file'}[$i]));
1281 } else {
1282 $line = '--- /dev/null';
1284 $result .= qq!<div class="diff from_file">$line</div>\n!;
1288 $line = $to_line;
1289 #assert($line =~ m/^\+\+\+/) if DEBUG;
1290 # no extra formatting for "^+++ /dev/null"
1291 if ($line =~ m!^\+\+\+ "?b/!) {
1292 if ($to->{'href'}) {
1293 $line = '+++ b/' .
1294 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1295 esc_path($to->{'file'}));
1296 } else {
1297 $line = '+++ b/' .
1298 esc_path($to->{'file'});
1301 $result .= qq!<div class="diff to_file">$line</div>\n!;
1303 return $result;
1306 # create note for patch simplified by combined diff
1307 sub format_diff_cc_simplified {
1308 my ($diffinfo, @parents) = @_;
1309 my $result = '';
1311 $result .= "<div class=\"diff header\">" .
1312 "diff --cc ";
1313 if (!is_deleted($diffinfo)) {
1314 $result .= $cgi->a({-href => href(action=>"blob",
1315 hash_base=>$hash,
1316 hash=>$diffinfo->{'to_id'},
1317 file_name=>$diffinfo->{'to_file'}),
1318 -class => "path"},
1319 esc_path($diffinfo->{'to_file'}));
1320 } else {
1321 $result .= esc_path($diffinfo->{'to_file'});
1323 $result .= "</div>\n" . # class="diff header"
1324 "<div class=\"diff nodifferences\">" .
1325 "Simple merge" .
1326 "</div>\n"; # class="diff nodifferences"
1328 return $result;
1331 # format patch (diff) line (not to be used for diff headers)
1332 sub format_diff_line {
1333 my $line = shift;
1334 my ($from, $to) = @_;
1335 my $diff_class = "";
1337 chomp $line;
1339 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1340 # combined diff
1341 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1342 if ($line =~ m/^\@{3}/) {
1343 $diff_class = " chunk_header";
1344 } elsif ($line =~ m/^\\/) {
1345 $diff_class = " incomplete";
1346 } elsif ($prefix =~ tr/+/+/) {
1347 $diff_class = " add";
1348 } elsif ($prefix =~ tr/-/-/) {
1349 $diff_class = " rem";
1351 } else {
1352 # assume ordinary diff
1353 my $char = substr($line, 0, 1);
1354 if ($char eq '+') {
1355 $diff_class = " add";
1356 } elsif ($char eq '-') {
1357 $diff_class = " rem";
1358 } elsif ($char eq '@') {
1359 $diff_class = " chunk_header";
1360 } elsif ($char eq "\\") {
1361 $diff_class = " incomplete";
1364 $line = untabify($line);
1365 if ($from && $to && $line =~ m/^\@{2} /) {
1366 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1367 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1369 $from_lines = 0 unless defined $from_lines;
1370 $to_lines = 0 unless defined $to_lines;
1372 if ($from->{'href'}) {
1373 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1374 -class=>"list"}, $from_text);
1376 if ($to->{'href'}) {
1377 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1378 -class=>"list"}, $to_text);
1380 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1381 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1382 return "<div class=\"diff$diff_class\">$line</div>\n";
1383 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1384 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1385 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1387 @from_text = split(' ', $ranges);
1388 for (my $i = 0; $i < @from_text; ++$i) {
1389 ($from_start[$i], $from_nlines[$i]) =
1390 (split(',', substr($from_text[$i], 1)), 0);
1393 $to_text = pop @from_text;
1394 $to_start = pop @from_start;
1395 $to_nlines = pop @from_nlines;
1397 $line = "<span class=\"chunk_info\">$prefix ";
1398 for (my $i = 0; $i < @from_text; ++$i) {
1399 if ($from->{'href'}[$i]) {
1400 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1401 -class=>"list"}, $from_text[$i]);
1402 } else {
1403 $line .= $from_text[$i];
1405 $line .= " ";
1407 if ($to->{'href'}) {
1408 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1409 -class=>"list"}, $to_text);
1410 } else {
1411 $line .= $to_text;
1413 $line .= " $prefix</span>" .
1414 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1415 return "<div class=\"diff$diff_class\">$line</div>\n";
1417 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1420 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1421 # linked. Pass the hash of the tree/commit to snapshot.
1422 sub format_snapshot_links {
1423 my ($hash) = @_;
1424 my @snapshot_fmts = gitweb_check_feature('snapshot');
1425 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1426 my $num_fmts = @snapshot_fmts;
1427 if ($num_fmts > 1) {
1428 # A parenthesized list of links bearing format names.
1429 # e.g. "snapshot (_tar.gz_ _zip_)"
1430 return "snapshot (" . join(' ', map
1431 $cgi->a({
1432 -href => href(
1433 action=>"snapshot",
1434 hash=>$hash,
1435 snapshot_format=>$_
1437 }, $known_snapshot_formats{$_}{'display'})
1438 , @snapshot_fmts) . ")";
1439 } elsif ($num_fmts == 1) {
1440 # A single "snapshot" link whose tooltip bears the format name.
1441 # i.e. "_snapshot_"
1442 my ($fmt) = @snapshot_fmts;
1443 return
1444 $cgi->a({
1445 -href => href(
1446 action=>"snapshot",
1447 hash=>$hash,
1448 snapshot_format=>$fmt
1450 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1451 }, "snapshot");
1452 } else { # $num_fmts == 0
1453 return undef;
1457 ## ......................................................................
1458 ## functions returning values to be passed, perhaps after some
1459 ## transformation, to other functions; e.g. returning arguments to href()
1461 # returns hash to be passed to href to generate gitweb URL
1462 # in -title key it returns description of link
1463 sub get_feed_info {
1464 my $format = shift || 'Atom';
1465 my %res = (action => lc($format));
1467 # feed links are possible only for project views
1468 return unless (defined $project);
1469 # some views should link to OPML, or to generic project feed,
1470 # or don't have specific feed yet (so they should use generic)
1471 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1473 my $branch;
1474 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1475 # from tag links; this also makes possible to detect branch links
1476 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1477 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1478 $branch = $1;
1480 # find log type for feed description (title)
1481 my $type = 'log';
1482 if (defined $file_name) {
1483 $type = "history of $file_name";
1484 $type .= "/" if ($action eq 'tree');
1485 $type .= " on '$branch'" if (defined $branch);
1486 } else {
1487 $type = "log of $branch" if (defined $branch);
1490 $res{-title} = $type;
1491 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1492 $res{'file_name'} = $file_name;
1494 return %res;
1497 ## ----------------------------------------------------------------------
1498 ## git utility subroutines, invoking git commands
1500 # returns path to the core git executable and the --git-dir parameter as list
1501 sub git_cmd {
1502 return $GIT, '--git-dir='.$git_dir;
1505 # quote the given arguments for passing them to the shell
1506 # quote_command("command", "arg 1", "arg with ' and ! characters")
1507 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1508 # Try to avoid using this function wherever possible.
1509 sub quote_command {
1510 return join(' ',
1511 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1514 # get HEAD ref of given project as hash
1515 sub git_get_head_hash {
1516 my $project = shift;
1517 my $o_git_dir = $git_dir;
1518 my $retval = undef;
1519 $git_dir = "$projectroot/$project";
1520 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1521 my $head = <$fd>;
1522 close $fd;
1523 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1524 $retval = $1;
1527 if (defined $o_git_dir) {
1528 $git_dir = $o_git_dir;
1530 return $retval;
1533 # get type of given object
1534 sub git_get_type {
1535 my $hash = shift;
1537 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1538 my $type = <$fd>;
1539 close $fd or return;
1540 chomp $type;
1541 return $type;
1544 # repository configuration
1545 our $config_file = '';
1546 our %config;
1548 # store multiple values for single key as anonymous array reference
1549 # single values stored directly in the hash, not as [ <value> ]
1550 sub hash_set_multi {
1551 my ($hash, $key, $value) = @_;
1553 if (!exists $hash->{$key}) {
1554 $hash->{$key} = $value;
1555 } elsif (!ref $hash->{$key}) {
1556 $hash->{$key} = [ $hash->{$key}, $value ];
1557 } else {
1558 push @{$hash->{$key}}, $value;
1562 # return hash of git project configuration
1563 # optionally limited to some section, e.g. 'gitweb'
1564 sub git_parse_project_config {
1565 my $section_regexp = shift;
1566 my %config;
1568 local $/ = "\0";
1570 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1571 or return;
1573 while (my $keyval = <$fh>) {
1574 chomp $keyval;
1575 my ($key, $value) = split(/\n/, $keyval, 2);
1577 hash_set_multi(\%config, $key, $value)
1578 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1580 close $fh;
1582 return %config;
1585 # convert config value to boolean, 'true' or 'false'
1586 # no value, number > 0, 'true' and 'yes' values are true
1587 # rest of values are treated as false (never as error)
1588 sub config_to_bool {
1589 my $val = shift;
1591 # strip leading and trailing whitespace
1592 $val =~ s/^\s+//;
1593 $val =~ s/\s+$//;
1595 return (!defined $val || # section.key
1596 ($val =~ /^\d+$/ && $val) || # section.key = 1
1597 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1600 # convert config value to simple decimal number
1601 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1602 # to be multiplied by 1024, 1048576, or 1073741824
1603 sub config_to_int {
1604 my $val = shift;
1606 # strip leading and trailing whitespace
1607 $val =~ s/^\s+//;
1608 $val =~ s/\s+$//;
1610 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1611 $unit = lc($unit);
1612 # unknown unit is treated as 1
1613 return $num * ($unit eq 'g' ? 1073741824 :
1614 $unit eq 'm' ? 1048576 :
1615 $unit eq 'k' ? 1024 : 1);
1617 return $val;
1620 # convert config value to array reference, if needed
1621 sub config_to_multi {
1622 my $val = shift;
1624 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1627 sub git_get_project_config {
1628 my ($key, $type) = @_;
1630 # key sanity check
1631 return unless ($key);
1632 $key =~ s/^gitweb\.//;
1633 return if ($key =~ m/\W/);
1635 # type sanity check
1636 if (defined $type) {
1637 $type =~ s/^--//;
1638 $type = undef
1639 unless ($type eq 'bool' || $type eq 'int');
1642 # get config
1643 if (!defined $config_file ||
1644 $config_file ne "$git_dir/config") {
1645 %config = git_parse_project_config('gitweb');
1646 $config_file = "$git_dir/config";
1649 # ensure given type
1650 if (!defined $type) {
1651 return $config{"gitweb.$key"};
1652 } elsif ($type eq 'bool') {
1653 # backward compatibility: 'git config --bool' returns true/false
1654 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1655 } elsif ($type eq 'int') {
1656 return config_to_int($config{"gitweb.$key"});
1658 return $config{"gitweb.$key"};
1661 # get hash of given path at given ref
1662 sub git_get_hash_by_path {
1663 my $base = shift;
1664 my $path = shift || return undef;
1665 my $type = shift;
1667 $path =~ s,/+$,,;
1669 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1670 or die_error(500, "Open git-ls-tree failed");
1671 my $line = <$fd>;
1672 close $fd or return undef;
1674 if (!defined $line) {
1675 # there is no tree or hash given by $path at $base
1676 return undef;
1679 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1680 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1681 if (defined $type && $type ne $2) {
1682 # type doesn't match
1683 return undef;
1685 return $3;
1688 # get path of entry with given hash at given tree-ish (ref)
1689 # used to get 'from' filename for combined diff (merge commit) for renames
1690 sub git_get_path_by_hash {
1691 my $base = shift || return;
1692 my $hash = shift || return;
1694 local $/ = "\0";
1696 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1697 or return undef;
1698 while (my $line = <$fd>) {
1699 chomp $line;
1701 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1702 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1703 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1704 close $fd;
1705 return $1;
1708 close $fd;
1709 return undef;
1712 ## ......................................................................
1713 ## git utility functions, directly accessing git repository
1715 sub git_get_project_description {
1716 my $path = shift;
1718 $git_dir = "$projectroot/$path";
1719 open my $fd, "$git_dir/description"
1720 or return git_get_project_config('description');
1721 my $descr = <$fd>;
1722 close $fd;
1723 if (defined $descr) {
1724 chomp $descr;
1726 return $descr;
1729 sub git_get_project_url_list {
1730 my $path = shift;
1732 $git_dir = "$projectroot/$path";
1733 open my $fd, "$git_dir/cloneurl"
1734 or return wantarray ?
1735 @{ config_to_multi(git_get_project_config('url')) } :
1736 config_to_multi(git_get_project_config('url'));
1737 my @git_project_url_list = map { chomp; $_ } <$fd>;
1738 close $fd;
1740 return wantarray ? @git_project_url_list : \@git_project_url_list;
1743 sub git_get_projects_list {
1744 my ($filter) = @_;
1745 my @list;
1747 $filter ||= '';
1748 $filter =~ s/\.git$//;
1750 my ($check_forks) = gitweb_check_feature('forks');
1752 if (-d $projects_list) {
1753 # search in directory
1754 my $dir = $projects_list . ($filter ? "/$filter" : '');
1755 # remove the trailing "/"
1756 $dir =~ s!/+$!!;
1757 my $pfxlen = length("$dir");
1758 my $pfxdepth = ($dir =~ tr!/!!);
1760 File::Find::find({
1761 follow_fast => 1, # follow symbolic links
1762 follow_skip => 2, # ignore duplicates
1763 dangling_symlinks => 0, # ignore dangling symlinks, silently
1764 wanted => sub {
1765 # skip project-list toplevel, if we get it.
1766 return if (m!^[/.]$!);
1767 # only directories can be git repositories
1768 return unless (-d $_);
1769 # don't traverse too deep (Find is super slow on os x)
1770 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1771 $File::Find::prune = 1;
1772 return;
1775 my $subdir = substr($File::Find::name, $pfxlen + 1);
1776 # we check related file in $projectroot
1777 if ($check_forks and $subdir =~ m#/.#) {
1778 $File::Find::prune = 1;
1779 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1780 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1781 $File::Find::prune = 1;
1784 }, "$dir");
1786 } elsif (-f $projects_list) {
1787 # read from file(url-encoded):
1788 # 'git%2Fgit.git Linus+Torvalds'
1789 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1790 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1791 my %paths;
1792 open my ($fd), $projects_list or return;
1793 PROJECT:
1794 while (my $line = <$fd>) {
1795 chomp $line;
1796 my ($path, $owner) = split ' ', $line;
1797 $path = unescape($path);
1798 $owner = unescape($owner);
1799 if (!defined $path) {
1800 next;
1802 if ($filter ne '') {
1803 # looking for forks;
1804 my $pfx = substr($path, 0, length($filter));
1805 if ($pfx ne $filter) {
1806 next PROJECT;
1808 my $sfx = substr($path, length($filter));
1809 if ($sfx !~ /^\/.*\.git$/) {
1810 next PROJECT;
1812 } elsif ($check_forks) {
1813 PATH:
1814 foreach my $filter (keys %paths) {
1815 # looking for forks;
1816 my $pfx = substr($path, 0, length($filter));
1817 if ($pfx ne $filter) {
1818 next PATH;
1820 my $sfx = substr($path, length($filter));
1821 if ($sfx !~ /^\/.*\.git$/) {
1822 next PATH;
1824 # is a fork, don't include it in
1825 # the list
1826 next PROJECT;
1829 if (check_export_ok("$projectroot/$path")) {
1830 my $pr = {
1831 path => $path,
1832 owner => to_utf8($owner),
1834 push @list, $pr;
1835 (my $forks_path = $path) =~ s/\.git$//;
1836 $paths{$forks_path}++;
1839 close $fd;
1841 return @list;
1844 our $gitweb_project_owner = undef;
1845 sub git_get_project_list_from_file {
1847 return if (defined $gitweb_project_owner);
1849 $gitweb_project_owner = {};
1850 # read from file (url-encoded):
1851 # 'git%2Fgit.git Linus+Torvalds'
1852 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1853 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1854 if (-f $projects_list) {
1855 open (my $fd , $projects_list);
1856 while (my $line = <$fd>) {
1857 chomp $line;
1858 my ($pr, $ow) = split ' ', $line;
1859 $pr = unescape($pr);
1860 $ow = unescape($ow);
1861 $gitweb_project_owner->{$pr} = to_utf8($ow);
1863 close $fd;
1867 sub git_get_project_owner {
1868 my $project = shift;
1869 my $owner;
1871 return undef unless $project;
1872 $git_dir = "$projectroot/$project";
1874 if (!defined $gitweb_project_owner) {
1875 git_get_project_list_from_file();
1878 if (exists $gitweb_project_owner->{$project}) {
1879 $owner = $gitweb_project_owner->{$project};
1881 if (!defined $owner){
1882 $owner = git_get_project_config('owner');
1884 if (!defined $owner) {
1885 $owner = get_file_owner("$git_dir");
1888 return $owner;
1891 sub git_get_last_activity {
1892 my ($path) = @_;
1893 my $fd;
1895 $git_dir = "$projectroot/$path";
1896 open($fd, "-|", git_cmd(), 'for-each-ref',
1897 '--format=%(committer)',
1898 '--sort=-committerdate',
1899 '--count=1',
1900 'refs/heads') or return;
1901 my $most_recent = <$fd>;
1902 close $fd or return;
1903 if (defined $most_recent &&
1904 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1905 my $timestamp = $1;
1906 my $age = time - $timestamp;
1907 return ($age, age_string($age));
1909 return (undef, undef);
1912 sub git_get_references {
1913 my $type = shift || "";
1914 my %refs;
1915 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1916 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1917 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1918 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1919 or return;
1921 while (my $line = <$fd>) {
1922 chomp $line;
1923 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1924 if (defined $refs{$1}) {
1925 push @{$refs{$1}}, $2;
1926 } else {
1927 $refs{$1} = [ $2 ];
1931 close $fd or return;
1932 return \%refs;
1935 sub git_get_rev_name_tags {
1936 my $hash = shift || return undef;
1938 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1939 or return;
1940 my $name_rev = <$fd>;
1941 close $fd;
1943 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1944 return $1;
1945 } else {
1946 # catches also '$hash undefined' output
1947 return undef;
1951 ## ----------------------------------------------------------------------
1952 ## parse to hash functions
1954 sub parse_date {
1955 my $epoch = shift;
1956 my $tz = shift || "-0000";
1958 my %date;
1959 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1960 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1961 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1962 $date{'hour'} = $hour;
1963 $date{'minute'} = $min;
1964 $date{'mday'} = $mday;
1965 $date{'day'} = $days[$wday];
1966 $date{'month'} = $months[$mon];
1967 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1968 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1969 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1970 $mday, $months[$mon], $hour ,$min;
1971 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1972 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
1974 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1975 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1976 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1977 $date{'hour_local'} = $hour;
1978 $date{'minute_local'} = $min;
1979 $date{'tz_local'} = $tz;
1980 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1981 1900+$year, $mon+1, $mday,
1982 $hour, $min, $sec, $tz);
1983 return %date;
1986 sub parse_tag {
1987 my $tag_id = shift;
1988 my %tag;
1989 my @comment;
1991 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1992 $tag{'id'} = $tag_id;
1993 while (my $line = <$fd>) {
1994 chomp $line;
1995 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1996 $tag{'object'} = $1;
1997 } elsif ($line =~ m/^type (.+)$/) {
1998 $tag{'type'} = $1;
1999 } elsif ($line =~ m/^tag (.+)$/) {
2000 $tag{'name'} = $1;
2001 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2002 $tag{'author'} = $1;
2003 $tag{'epoch'} = $2;
2004 $tag{'tz'} = $3;
2005 } elsif ($line =~ m/--BEGIN/) {
2006 push @comment, $line;
2007 last;
2008 } elsif ($line eq "") {
2009 last;
2012 push @comment, <$fd>;
2013 $tag{'comment'} = \@comment;
2014 close $fd or return;
2015 if (!defined $tag{'name'}) {
2016 return
2018 return %tag
2021 sub parse_commit_text {
2022 my ($commit_text, $withparents) = @_;
2023 my @commit_lines = split '\n', $commit_text;
2024 my %co;
2026 pop @commit_lines; # Remove '\0'
2028 if (! @commit_lines) {
2029 return;
2032 my $header = shift @commit_lines;
2033 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2034 return;
2036 ($co{'id'}, my @parents) = split ' ', $header;
2037 while (my $line = shift @commit_lines) {
2038 last if $line eq "\n";
2039 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2040 $co{'tree'} = $1;
2041 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2042 push @parents, $1;
2043 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2044 $co{'author'} = $1;
2045 $co{'author_epoch'} = $2;
2046 $co{'author_tz'} = $3;
2047 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2048 $co{'author_name'} = $1;
2049 $co{'author_email'} = $2;
2050 } else {
2051 $co{'author_name'} = $co{'author'};
2053 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2054 $co{'committer'} = $1;
2055 $co{'committer_epoch'} = $2;
2056 $co{'committer_tz'} = $3;
2057 $co{'committer_name'} = $co{'committer'};
2058 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2059 $co{'committer_name'} = $1;
2060 $co{'committer_email'} = $2;
2061 } else {
2062 $co{'committer_name'} = $co{'committer'};
2066 if (!defined $co{'tree'}) {
2067 return;
2069 $co{'parents'} = \@parents;
2070 $co{'parent'} = $parents[0];
2072 foreach my $title (@commit_lines) {
2073 $title =~ s/^ //;
2074 if ($title ne "") {
2075 $co{'title'} = chop_str($title, 80, 5);
2076 # remove leading stuff of merges to make the interesting part visible
2077 if (length($title) > 50) {
2078 $title =~ s/^Automatic //;
2079 $title =~ s/^merge (of|with) /Merge ... /i;
2080 if (length($title) > 50) {
2081 $title =~ s/(http|rsync):\/\///;
2083 if (length($title) > 50) {
2084 $title =~ s/(master|www|rsync)\.//;
2086 if (length($title) > 50) {
2087 $title =~ s/kernel.org:?//;
2089 if (length($title) > 50) {
2090 $title =~ s/\/pub\/scm//;
2093 $co{'title_short'} = chop_str($title, 50, 5);
2094 last;
2097 if ($co{'title'} eq "") {
2098 $co{'title'} = $co{'title_short'} = '(no commit message)';
2100 # remove added spaces
2101 foreach my $line (@commit_lines) {
2102 $line =~ s/^ //;
2104 $co{'comment'} = \@commit_lines;
2106 my $age = time - $co{'committer_epoch'};
2107 $co{'age'} = $age;
2108 $co{'age_string'} = age_string($age);
2109 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2110 if ($age > 60*60*24*7*2) {
2111 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2112 $co{'age_string_age'} = $co{'age_string'};
2113 } else {
2114 $co{'age_string_date'} = $co{'age_string'};
2115 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2117 return %co;
2120 sub parse_commit {
2121 my ($commit_id) = @_;
2122 my %co;
2124 local $/ = "\0";
2126 open my $fd, "-|", git_cmd(), "rev-list",
2127 "--parents",
2128 "--header",
2129 "--max-count=1",
2130 $commit_id,
2131 "--",
2132 or die_error(500, "Open git-rev-list failed");
2133 %co = parse_commit_text(<$fd>, 1);
2134 close $fd;
2136 return %co;
2139 sub parse_commits {
2140 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2141 my @cos;
2143 $maxcount ||= 1;
2144 $skip ||= 0;
2146 local $/ = "\0";
2148 open my $fd, "-|", git_cmd(), "rev-list",
2149 "--header",
2150 @args,
2151 ("--max-count=" . $maxcount),
2152 ("--skip=" . $skip),
2153 @extra_options,
2154 $commit_id,
2155 "--",
2156 ($filename ? ($filename) : ())
2157 or die_error(500, "Open git-rev-list failed");
2158 while (my $line = <$fd>) {
2159 my %co = parse_commit_text($line);
2160 push @cos, \%co;
2162 close $fd;
2164 return wantarray ? @cos : \@cos;
2167 # parse line of git-diff-tree "raw" output
2168 sub parse_difftree_raw_line {
2169 my $line = shift;
2170 my %res;
2172 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2173 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2174 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2175 $res{'from_mode'} = $1;
2176 $res{'to_mode'} = $2;
2177 $res{'from_id'} = $3;
2178 $res{'to_id'} = $4;
2179 $res{'status'} = $5;
2180 $res{'similarity'} = $6;
2181 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2182 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2183 } else {
2184 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2187 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2188 # combined diff (for merge commit)
2189 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2190 $res{'nparents'} = length($1);
2191 $res{'from_mode'} = [ split(' ', $2) ];
2192 $res{'to_mode'} = pop @{$res{'from_mode'}};
2193 $res{'from_id'} = [ split(' ', $3) ];
2194 $res{'to_id'} = pop @{$res{'from_id'}};
2195 $res{'status'} = [ split('', $4) ];
2196 $res{'to_file'} = unquote($5);
2198 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2199 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2200 $res{'commit'} = $1;
2203 return wantarray ? %res : \%res;
2206 # wrapper: return parsed line of git-diff-tree "raw" output
2207 # (the argument might be raw line, or parsed info)
2208 sub parsed_difftree_line {
2209 my $line_or_ref = shift;
2211 if (ref($line_or_ref) eq "HASH") {
2212 # pre-parsed (or generated by hand)
2213 return $line_or_ref;
2214 } else {
2215 return parse_difftree_raw_line($line_or_ref);
2219 # parse line of git-ls-tree output
2220 sub parse_ls_tree_line ($;%) {
2221 my $line = shift;
2222 my %opts = @_;
2223 my %res;
2225 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2226 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2228 $res{'mode'} = $1;
2229 $res{'type'} = $2;
2230 $res{'hash'} = $3;
2231 if ($opts{'-z'}) {
2232 $res{'name'} = $4;
2233 } else {
2234 $res{'name'} = unquote($4);
2237 return wantarray ? %res : \%res;
2240 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2241 sub parse_from_to_diffinfo {
2242 my ($diffinfo, $from, $to, @parents) = @_;
2244 if ($diffinfo->{'nparents'}) {
2245 # combined diff
2246 $from->{'file'} = [];
2247 $from->{'href'} = [];
2248 fill_from_file_info($diffinfo, @parents)
2249 unless exists $diffinfo->{'from_file'};
2250 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2251 $from->{'file'}[$i] =
2252 defined $diffinfo->{'from_file'}[$i] ?
2253 $diffinfo->{'from_file'}[$i] :
2254 $diffinfo->{'to_file'};
2255 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2256 $from->{'href'}[$i] = href(action=>"blob",
2257 hash_base=>$parents[$i],
2258 hash=>$diffinfo->{'from_id'}[$i],
2259 file_name=>$from->{'file'}[$i]);
2260 } else {
2261 $from->{'href'}[$i] = undef;
2264 } else {
2265 # ordinary (not combined) diff
2266 $from->{'file'} = $diffinfo->{'from_file'};
2267 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2268 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2269 hash=>$diffinfo->{'from_id'},
2270 file_name=>$from->{'file'});
2271 } else {
2272 delete $from->{'href'};
2276 $to->{'file'} = $diffinfo->{'to_file'};
2277 if (!is_deleted($diffinfo)) { # file exists in result
2278 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2279 hash=>$diffinfo->{'to_id'},
2280 file_name=>$to->{'file'});
2281 } else {
2282 delete $to->{'href'};
2286 ## ......................................................................
2287 ## parse to array of hashes functions
2289 sub git_get_heads_list {
2290 my $limit = shift;
2291 my @headslist;
2293 open my $fd, '-|', git_cmd(), 'for-each-ref',
2294 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2295 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2296 'refs/heads'
2297 or return;
2298 while (my $line = <$fd>) {
2299 my %ref_item;
2301 chomp $line;
2302 my ($refinfo, $committerinfo) = split(/\0/, $line);
2303 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2304 my ($committer, $epoch, $tz) =
2305 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2306 $ref_item{'fullname'} = $name;
2307 $name =~ s!^refs/heads/!!;
2309 $ref_item{'name'} = $name;
2310 $ref_item{'id'} = $hash;
2311 $ref_item{'title'} = $title || '(no commit message)';
2312 $ref_item{'epoch'} = $epoch;
2313 if ($epoch) {
2314 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2315 } else {
2316 $ref_item{'age'} = "unknown";
2319 push @headslist, \%ref_item;
2321 close $fd;
2323 return wantarray ? @headslist : \@headslist;
2326 sub git_get_tags_list {
2327 my $limit = shift;
2328 my @tagslist;
2330 open my $fd, '-|', git_cmd(), 'for-each-ref',
2331 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2332 '--format=%(objectname) %(objecttype) %(refname) '.
2333 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2334 'refs/tags'
2335 or return;
2336 while (my $line = <$fd>) {
2337 my %ref_item;
2339 chomp $line;
2340 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2341 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2342 my ($creator, $epoch, $tz) =
2343 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2344 $ref_item{'fullname'} = $name;
2345 $name =~ s!^refs/tags/!!;
2347 $ref_item{'type'} = $type;
2348 $ref_item{'id'} = $id;
2349 $ref_item{'name'} = $name;
2350 if ($type eq "tag") {
2351 $ref_item{'subject'} = $title;
2352 $ref_item{'reftype'} = $reftype;
2353 $ref_item{'refid'} = $refid;
2354 } else {
2355 $ref_item{'reftype'} = $type;
2356 $ref_item{'refid'} = $id;
2359 if ($type eq "tag" || $type eq "commit") {
2360 $ref_item{'epoch'} = $epoch;
2361 if ($epoch) {
2362 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2363 } else {
2364 $ref_item{'age'} = "unknown";
2368 push @tagslist, \%ref_item;
2370 close $fd;
2372 return wantarray ? @tagslist : \@tagslist;
2375 ## ----------------------------------------------------------------------
2376 ## filesystem-related functions
2378 sub get_file_owner {
2379 my $path = shift;
2381 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2382 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2383 if (!defined $gcos) {
2384 return undef;
2386 my $owner = $gcos;
2387 $owner =~ s/[,;].*$//;
2388 return to_utf8($owner);
2391 ## ......................................................................
2392 ## mimetype related functions
2394 sub mimetype_guess_file {
2395 my $filename = shift;
2396 my $mimemap = shift;
2397 -r $mimemap or return undef;
2399 my %mimemap;
2400 open(MIME, $mimemap) or return undef;
2401 while (<MIME>) {
2402 next if m/^#/; # skip comments
2403 my ($mime, $exts) = split(/\t+/);
2404 if (defined $exts) {
2405 my @exts = split(/\s+/, $exts);
2406 foreach my $ext (@exts) {
2407 $mimemap{$ext} = $mime;
2411 close(MIME);
2413 $filename =~ /\.([^.]*)$/;
2414 return $mimemap{$1};
2417 sub mimetype_guess {
2418 my $filename = shift;
2419 my $mime;
2420 $filename =~ /\./ or return undef;
2422 if ($mimetypes_file) {
2423 my $file = $mimetypes_file;
2424 if ($file !~ m!^/!) { # if it is relative path
2425 # it is relative to project
2426 $file = "$projectroot/$project/$file";
2428 $mime = mimetype_guess_file($filename, $file);
2430 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2431 return $mime;
2434 sub blob_mimetype {
2435 my $fd = shift;
2436 my $filename = shift;
2438 if ($filename) {
2439 my $mime = mimetype_guess($filename);
2440 $mime and return $mime;
2443 # just in case
2444 return $default_blob_plain_mimetype unless $fd;
2446 if (-T $fd) {
2447 return 'text/plain';
2448 } elsif (! $filename) {
2449 return 'application/octet-stream';
2450 } elsif ($filename =~ m/\.png$/i) {
2451 return 'image/png';
2452 } elsif ($filename =~ m/\.gif$/i) {
2453 return 'image/gif';
2454 } elsif ($filename =~ m/\.jpe?g$/i) {
2455 return 'image/jpeg';
2456 } else {
2457 return 'application/octet-stream';
2461 sub blob_contenttype {
2462 my ($fd, $file_name, $type) = @_;
2464 $type ||= blob_mimetype($fd, $file_name);
2465 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2466 $type .= "; charset=$default_text_plain_charset";
2469 return $type;
2472 ## ======================================================================
2473 ## functions printing HTML: header, footer, error page
2475 sub git_header_html {
2476 my $status = shift || "200 OK";
2477 my $expires = shift;
2479 my $title = "$site_name";
2480 if (defined $project) {
2481 $title .= " - " . to_utf8($project);
2482 if (defined $action) {
2483 $title .= "/$action";
2484 if (defined $file_name) {
2485 $title .= " - " . esc_path($file_name);
2486 if ($action eq "tree" && $file_name !~ m|/$|) {
2487 $title .= "/";
2492 my $content_type;
2493 # require explicit support from the UA if we are to send the page as
2494 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2495 # we have to do this because MSIE sometimes globs '*/*', pretending to
2496 # support xhtml+xml but choking when it gets what it asked for.
2497 if (defined $cgi->http('HTTP_ACCEPT') &&
2498 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2499 $cgi->Accept('application/xhtml+xml') != 0) {
2500 $content_type = 'application/xhtml+xml';
2501 } else {
2502 $content_type = 'text/html';
2504 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2505 -status=> $status, -expires => $expires);
2506 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2507 print <<EOF;
2508 <?xml version="1.0" encoding="utf-8"?>
2509 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2510 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2511 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2512 <!-- git core binaries version $git_version -->
2513 <head>
2514 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2515 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2516 <meta name="robots" content="index, nofollow"/>
2517 <title>$title</title>
2519 # print out each stylesheet that exist
2520 if (defined $stylesheet) {
2521 #provides backwards capability for those people who define style sheet in a config file
2522 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2523 } else {
2524 foreach my $stylesheet (@stylesheets) {
2525 next unless $stylesheet;
2526 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2529 if (defined $project) {
2530 my %href_params = get_feed_info();
2531 if (!exists $href_params{'-title'}) {
2532 $href_params{'-title'} = 'log';
2535 foreach my $format qw(RSS Atom) {
2536 my $type = lc($format);
2537 my %link_attr = (
2538 '-rel' => 'alternate',
2539 '-title' => "$project - $href_params{'-title'} - $format feed",
2540 '-type' => "application/$type+xml"
2543 $href_params{'action'} = $type;
2544 $link_attr{'-href'} = href(%href_params);
2545 print "<link ".
2546 "rel=\"$link_attr{'-rel'}\" ".
2547 "title=\"$link_attr{'-title'}\" ".
2548 "href=\"$link_attr{'-href'}\" ".
2549 "type=\"$link_attr{'-type'}\" ".
2550 "/>\n";
2552 $href_params{'extra_options'} = '--no-merges';
2553 $link_attr{'-href'} = href(%href_params);
2554 $link_attr{'-title'} .= ' (no merges)';
2555 print "<link ".
2556 "rel=\"$link_attr{'-rel'}\" ".
2557 "title=\"$link_attr{'-title'}\" ".
2558 "href=\"$link_attr{'-href'}\" ".
2559 "type=\"$link_attr{'-type'}\" ".
2560 "/>\n";
2563 } else {
2564 printf('<link rel="alternate" title="%s projects list" '.
2565 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2566 $site_name, href(project=>undef, action=>"project_index"));
2567 printf('<link rel="alternate" title="%s projects feeds" '.
2568 'href="%s" type="text/x-opml" />'."\n",
2569 $site_name, href(project=>undef, action=>"opml"));
2571 if (defined $favicon) {
2572 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2575 if (defined $gitwebjs) {
2576 print qq(<script src="$gitwebjs" type="text/javascript"></script>\n);
2579 print "</head>\n" .
2580 "<body>\n";
2582 if (-f $site_header) {
2583 open (my $fd, $site_header);
2584 print <$fd>;
2585 close $fd;
2588 print "<div class=\"page_header\">\n" .
2589 $cgi->a({-href => esc_url($logo_url),
2590 -title => $logo_label},
2591 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2592 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2593 if (defined $project) {
2594 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2595 if (defined $action) {
2596 print " / $action";
2598 print "\n";
2600 print "</div>\n";
2602 my ($have_search) = gitweb_check_feature('search');
2603 if (defined $project && $have_search) {
2604 if (!defined $searchtext) {
2605 $searchtext = "";
2607 my $search_hash;
2608 if (defined $hash_base) {
2609 $search_hash = $hash_base;
2610 } elsif (defined $hash) {
2611 $search_hash = $hash;
2612 } else {
2613 $search_hash = "HEAD";
2615 my $action = $my_uri;
2616 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2617 if ($use_pathinfo) {
2618 $action .= "/".esc_url($project);
2620 print $cgi->startform(-method => "get", -action => $action) .
2621 "<div class=\"search\">\n" .
2622 (!$use_pathinfo &&
2623 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2624 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2625 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2626 $cgi->popup_menu(-name => 'st', -default => 'commit',
2627 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2628 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2629 " search:\n",
2630 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2631 "<span title=\"Extended regular expression\">" .
2632 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
2633 -checked => $search_use_regexp) .
2634 "</span>" .
2635 "</div>" .
2636 $cgi->end_form() . "\n";
2640 sub git_footer_html {
2641 my $feed_class = 'rss_logo';
2643 print "<div class=\"page_footer\">\n";
2644 if (defined $project) {
2645 my $descr = git_get_project_description($project);
2646 if (defined $descr) {
2647 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2650 my %href_params = get_feed_info();
2651 if (!%href_params) {
2652 $feed_class .= ' generic';
2654 $href_params{'-title'} ||= 'log';
2656 foreach my $format qw(RSS Atom) {
2657 $href_params{'action'} = lc($format);
2658 print $cgi->a({-href => href(%href_params),
2659 -title => "$href_params{'-title'} $format feed",
2660 -class => $feed_class}, $format)."\n";
2663 } else {
2664 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2665 -class => $feed_class}, "OPML") . " ";
2666 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2667 -class => $feed_class}, "TXT") . "\n";
2669 print "</div>\n"; # class="page_footer"
2671 if (-f $site_footer) {
2672 open (my $fd, $site_footer);
2673 print <$fd>;
2674 close $fd;
2677 print "</body>\n" .
2678 "</html>";
2681 # die_error(<http_status_code>, <error_message>)
2682 # Example: die_error(404, 'Hash not found')
2683 # By convention, use the following status codes (as defined in RFC 2616):
2684 # 400: Invalid or missing CGI parameters, or
2685 # requested object exists but has wrong type.
2686 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
2687 # this server or project.
2688 # 404: Requested object/revision/project doesn't exist.
2689 # 500: The server isn't configured properly, or
2690 # an internal error occurred (e.g. failed assertions caused by bugs), or
2691 # an unknown error occurred (e.g. the git binary died unexpectedly).
2692 sub die_error {
2693 my $status = shift || 500;
2694 my $error = shift || "Internal server error";
2696 my %http_responses = (400 => '400 Bad Request',
2697 403 => '403 Forbidden',
2698 404 => '404 Not Found',
2699 500 => '500 Internal Server Error');
2700 git_header_html($http_responses{$status});
2701 print <<EOF;
2702 <div class="page_body">
2703 <br /><br />
2704 $status - $error
2705 <br />
2706 </div>
2708 git_footer_html();
2709 exit;
2712 ## ----------------------------------------------------------------------
2713 ## functions printing or outputting HTML: navigation
2715 sub git_print_page_nav {
2716 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2717 $extra = '' if !defined $extra; # pager or formats
2719 my @navs = qw(summary shortlog log commit commitdiff tree);
2720 if ($suppress) {
2721 @navs = grep { $_ ne $suppress } @navs;
2724 my %arg = map { $_ => {action=>$_} } @navs;
2725 if (defined $head) {
2726 for (qw(commit commitdiff)) {
2727 $arg{$_}{'hash'} = $head;
2729 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2730 for (qw(shortlog log)) {
2731 $arg{$_}{'hash'} = $head;
2735 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2736 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2738 print "<div class=\"page_nav\">\n" .
2739 (join " | ",
2740 map { $_ eq $current ?
2741 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2742 } @navs);
2743 print "<br/>\n$extra<br/>\n" .
2744 "</div>\n";
2747 sub format_paging_nav {
2748 my ($action, $hash, $head, $page, $has_next_link) = @_;
2749 my $paging_nav;
2752 if ($hash ne $head || $page) {
2753 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2754 } else {
2755 $paging_nav .= "HEAD";
2758 if ($page > 0) {
2759 $paging_nav .= " &sdot; " .
2760 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2761 -accesskey => "p", -title => "Alt-p"}, "prev");
2762 } else {
2763 $paging_nav .= " &sdot; prev";
2766 if ($has_next_link) {
2767 $paging_nav .= " &sdot; " .
2768 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2769 -accesskey => "n", -title => "Alt-n"}, "next");
2770 } else {
2771 $paging_nav .= " &sdot; next";
2774 return $paging_nav;
2777 ## ......................................................................
2778 ## functions printing or outputting HTML: div
2780 sub git_print_header_div {
2781 my ($action, $title, $hash, $hash_base) = @_;
2782 my %args = ();
2784 $args{'action'} = $action;
2785 $args{'hash'} = $hash if $hash;
2786 $args{'hash_base'} = $hash_base if $hash_base;
2788 print "<div class=\"header\">\n" .
2789 $cgi->a({-href => href(%args), -class => "title"},
2790 $title ? $title : $action) .
2791 "\n</div>\n";
2794 #sub git_print_authorship (\%) {
2795 sub git_print_authorship {
2796 my $co = shift;
2798 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2799 print "<div class=\"author_date\">" .
2800 esc_html($co->{'author_name'}) .
2801 " [$ad{'rfc2822'}";
2802 if ($ad{'hour_local'} < 6) {
2803 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2804 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2805 } else {
2806 printf(" (%02d:%02d %s)",
2807 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2809 print "]</div>\n";
2812 sub git_print_page_path {
2813 my $name = shift;
2814 my $type = shift;
2815 my $hb = shift;
2818 print "<div class=\"page_path\">";
2819 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2820 -title => 'tree root'}, to_utf8("[$project]"));
2821 print " / ";
2822 if (defined $name) {
2823 my @dirname = split '/', $name;
2824 my $basename = pop @dirname;
2825 my $fullname = '';
2827 foreach my $dir (@dirname) {
2828 $fullname .= ($fullname ? '/' : '') . $dir;
2829 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2830 hash_base=>$hb),
2831 -title => $fullname}, esc_path($dir));
2832 print " / ";
2834 if (defined $type && $type eq 'blob') {
2835 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2836 hash_base=>$hb),
2837 -title => $name}, esc_path($basename));
2838 } elsif (defined $type && $type eq 'tree') {
2839 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2840 hash_base=>$hb),
2841 -title => $name}, esc_path($basename));
2842 print " / ";
2843 } else {
2844 print esc_path($basename);
2847 print "<br/></div>\n";
2850 # sub git_print_log (\@;%) {
2851 sub git_print_log ($;%) {
2852 my $log = shift;
2853 my %opts = @_;
2855 if ($opts{'-remove_title'}) {
2856 # remove title, i.e. first line of log
2857 shift @$log;
2859 # remove leading empty lines
2860 while (defined $log->[0] && $log->[0] eq "") {
2861 shift @$log;
2864 # print log
2865 my $signoff = 0;
2866 my $empty = 0;
2867 foreach my $line (@$log) {
2868 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2869 $signoff = 1;
2870 $empty = 0;
2871 if (! $opts{'-remove_signoff'}) {
2872 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2873 next;
2874 } else {
2875 # remove signoff lines
2876 next;
2878 } else {
2879 $signoff = 0;
2882 # print only one empty line
2883 # do not print empty line after signoff
2884 if ($line eq "") {
2885 next if ($empty || $signoff);
2886 $empty = 1;
2887 } else {
2888 $empty = 0;
2891 print format_log_line_html($line) . "<br/>\n";
2894 if ($opts{'-final_empty_line'}) {
2895 # end with single empty line
2896 print "<br/>\n" unless $empty;
2900 # return link target (what link points to)
2901 sub git_get_link_target {
2902 my $hash = shift;
2903 my $link_target;
2905 # read link
2906 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2907 or return;
2909 local $/;
2910 $link_target = <$fd>;
2912 close $fd
2913 or return;
2915 return $link_target;
2918 # given link target, and the directory (basedir) the link is in,
2919 # return target of link relative to top directory (top tree);
2920 # return undef if it is not possible (including absolute links).
2921 sub normalize_link_target {
2922 my ($link_target, $basedir, $hash_base) = @_;
2924 # we can normalize symlink target only if $hash_base is provided
2925 return unless $hash_base;
2927 # absolute symlinks (beginning with '/') cannot be normalized
2928 return if (substr($link_target, 0, 1) eq '/');
2930 # normalize link target to path from top (root) tree (dir)
2931 my $path;
2932 if ($basedir) {
2933 $path = $basedir . '/' . $link_target;
2934 } else {
2935 # we are in top (root) tree (dir)
2936 $path = $link_target;
2939 # remove //, /./, and /../
2940 my @path_parts;
2941 foreach my $part (split('/', $path)) {
2942 # discard '.' and ''
2943 next if (!$part || $part eq '.');
2944 # handle '..'
2945 if ($part eq '..') {
2946 if (@path_parts) {
2947 pop @path_parts;
2948 } else {
2949 # link leads outside repository (outside top dir)
2950 return;
2952 } else {
2953 push @path_parts, $part;
2956 $path = join('/', @path_parts);
2958 return $path;
2961 # print tree entry (row of git_tree), but without encompassing <tr> element
2962 sub git_print_tree_entry {
2963 my ($t, $basedir, $hash_base, $have_blame) = @_;
2965 my %base_key = ();
2966 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2968 # The format of a table row is: mode list link. Where mode is
2969 # the mode of the entry, list is the name of the entry, an href,
2970 # and link is the action links of the entry.
2972 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2973 if ($t->{'type'} eq "blob") {
2974 print "<td class=\"list\">" .
2975 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2976 file_name=>"$basedir$t->{'name'}", %base_key),
2977 -class => "list"}, esc_path($t->{'name'}));
2978 if (S_ISLNK(oct $t->{'mode'})) {
2979 my $link_target = git_get_link_target($t->{'hash'});
2980 if ($link_target) {
2981 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2982 if (defined $norm_target) {
2983 print " -> " .
2984 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2985 file_name=>$norm_target),
2986 -title => $norm_target}, esc_path($link_target));
2987 } else {
2988 print " -> " . esc_path($link_target);
2992 print "</td>\n";
2993 print "<td class=\"link\">";
2994 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2995 file_name=>"$basedir$t->{'name'}", %base_key)},
2996 "blob");
2997 if ($have_blame) {
2998 print " | " .
2999 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3000 file_name=>"$basedir$t->{'name'}", %base_key)},
3001 "blame");
3003 if (defined $hash_base) {
3004 print " | " .
3005 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3006 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3007 "history");
3009 print " | " .
3010 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3011 file_name=>"$basedir$t->{'name'}")},
3012 "raw");
3013 print "</td>\n";
3015 } elsif ($t->{'type'} eq "tree") {
3016 print "<td class=\"list\">";
3017 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3018 file_name=>"$basedir$t->{'name'}", %base_key)},
3019 esc_path($t->{'name'}));
3020 print "</td>\n";
3021 print "<td class=\"link\">";
3022 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3023 file_name=>"$basedir$t->{'name'}", %base_key)},
3024 "tree");
3025 if (defined $hash_base) {
3026 print " | " .
3027 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3028 file_name=>"$basedir$t->{'name'}")},
3029 "history");
3031 print "</td>\n";
3032 } else {
3033 # unknown object: we can only present history for it
3034 # (this includes 'commit' object, i.e. submodule support)
3035 print "<td class=\"list\">" .
3036 esc_path($t->{'name'}) .
3037 "</td>\n";
3038 print "<td class=\"link\">";
3039 if (defined $hash_base) {
3040 print $cgi->a({-href => href(action=>"history",
3041 hash_base=>$hash_base,
3042 file_name=>"$basedir$t->{'name'}")},
3043 "history");
3045 print "</td>\n";
3049 ## ......................................................................
3050 ## functions printing large fragments of HTML
3052 # get pre-image filenames for merge (combined) diff
3053 sub fill_from_file_info {
3054 my ($diff, @parents) = @_;
3056 $diff->{'from_file'} = [ ];
3057 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3058 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3059 if ($diff->{'status'}[$i] eq 'R' ||
3060 $diff->{'status'}[$i] eq 'C') {
3061 $diff->{'from_file'}[$i] =
3062 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3066 return $diff;
3069 # is current raw difftree line of file deletion
3070 sub is_deleted {
3071 my $diffinfo = shift;
3073 return $diffinfo->{'to_id'} eq ('0' x 40);
3076 # does patch correspond to [previous] difftree raw line
3077 # $diffinfo - hashref of parsed raw diff format
3078 # $patchinfo - hashref of parsed patch diff format
3079 # (the same keys as in $diffinfo)
3080 sub is_patch_split {
3081 my ($diffinfo, $patchinfo) = @_;
3083 return defined $diffinfo && defined $patchinfo
3084 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3088 sub git_difftree_body {
3089 my ($difftree, $hash, @parents) = @_;
3090 my ($parent) = $parents[0];
3091 my ($have_blame) = gitweb_check_feature('blame');
3092 print "<div class=\"list_head\">\n";
3093 if ($#{$difftree} > 10) {
3094 print(($#{$difftree} + 1) . " files changed:\n");
3096 print "</div>\n";
3098 print "<table class=\"" .
3099 (@parents > 1 ? "combined " : "") .
3100 "diff_tree\">\n";
3102 # header only for combined diff in 'commitdiff' view
3103 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3104 if ($has_header) {
3105 # table header
3106 print "<thead><tr>\n" .
3107 "<th></th><th></th>\n"; # filename, patchN link
3108 for (my $i = 0; $i < @parents; $i++) {
3109 my $par = $parents[$i];
3110 print "<th>" .
3111 $cgi->a({-href => href(action=>"commitdiff",
3112 hash=>$hash, hash_parent=>$par),
3113 -title => 'commitdiff to parent number ' .
3114 ($i+1) . ': ' . substr($par,0,7)},
3115 $i+1) .
3116 "&nbsp;</th>\n";
3118 print "</tr></thead>\n<tbody>\n";
3121 my $alternate = 1;
3122 my $patchno = 0;
3123 foreach my $line (@{$difftree}) {
3124 my $diff = parsed_difftree_line($line);
3126 if ($alternate) {
3127 print "<tr class=\"dark\">\n";
3128 } else {
3129 print "<tr class=\"light\">\n";
3131 $alternate ^= 1;
3133 if (exists $diff->{'nparents'}) { # combined diff
3135 fill_from_file_info($diff, @parents)
3136 unless exists $diff->{'from_file'};
3138 if (!is_deleted($diff)) {
3139 # file exists in the result (child) commit
3140 print "<td>" .
3141 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3142 file_name=>$diff->{'to_file'},
3143 hash_base=>$hash),
3144 -class => "list"}, esc_path($diff->{'to_file'})) .
3145 "</td>\n";
3146 } else {
3147 print "<td>" .
3148 esc_path($diff->{'to_file'}) .
3149 "</td>\n";
3152 if ($action eq 'commitdiff') {
3153 # link to patch
3154 $patchno++;
3155 print "<td class=\"link\">" .
3156 $cgi->a({-href => "#patch$patchno"}, "patch") .
3157 " | " .
3158 "</td>\n";
3161 my $has_history = 0;
3162 my $not_deleted = 0;
3163 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3164 my $hash_parent = $parents[$i];
3165 my $from_hash = $diff->{'from_id'}[$i];
3166 my $from_path = $diff->{'from_file'}[$i];
3167 my $status = $diff->{'status'}[$i];
3169 $has_history ||= ($status ne 'A');
3170 $not_deleted ||= ($status ne 'D');
3172 if ($status eq 'A') {
3173 print "<td class=\"link\" align=\"right\"> | </td>\n";
3174 } elsif ($status eq 'D') {
3175 print "<td class=\"link\">" .
3176 $cgi->a({-href => href(action=>"blob",
3177 hash_base=>$hash,
3178 hash=>$from_hash,
3179 file_name=>$from_path)},
3180 "blob" . ($i+1)) .
3181 " | </td>\n";
3182 } else {
3183 if ($diff->{'to_id'} eq $from_hash) {
3184 print "<td class=\"link nochange\">";
3185 } else {
3186 print "<td class=\"link\">";
3188 print $cgi->a({-href => href(action=>"blobdiff",
3189 hash=>$diff->{'to_id'},
3190 hash_parent=>$from_hash,
3191 hash_base=>$hash,
3192 hash_parent_base=>$hash_parent,
3193 file_name=>$diff->{'to_file'},
3194 file_parent=>$from_path)},
3195 "diff" . ($i+1)) .
3196 " | </td>\n";
3200 print "<td class=\"link\">";
3201 if ($not_deleted) {
3202 print $cgi->a({-href => href(action=>"blob",
3203 hash=>$diff->{'to_id'},
3204 file_name=>$diff->{'to_file'},
3205 hash_base=>$hash)},
3206 "blob");
3207 print " | " if ($has_history);
3209 if ($has_history) {
3210 print $cgi->a({-href => href(action=>"history",
3211 file_name=>$diff->{'to_file'},
3212 hash_base=>$hash)},
3213 "history");
3215 print "</td>\n";
3217 print "</tr>\n";
3218 next; # instead of 'else' clause, to avoid extra indent
3220 # else ordinary diff
3222 my ($to_mode_oct, $to_mode_str, $to_file_type);
3223 my ($from_mode_oct, $from_mode_str, $from_file_type);
3224 if ($diff->{'to_mode'} ne ('0' x 6)) {
3225 $to_mode_oct = oct $diff->{'to_mode'};
3226 if (S_ISREG($to_mode_oct)) { # only for regular file
3227 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3229 $to_file_type = file_type($diff->{'to_mode'});
3231 if ($diff->{'from_mode'} ne ('0' x 6)) {
3232 $from_mode_oct = oct $diff->{'from_mode'};
3233 if (S_ISREG($to_mode_oct)) { # only for regular file
3234 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3236 $from_file_type = file_type($diff->{'from_mode'});
3239 if ($diff->{'status'} eq "A") { # created
3240 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3241 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3242 $mode_chng .= "]</span>";
3243 print "<td>";
3244 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3245 hash_base=>$hash, file_name=>$diff->{'file'}),
3246 -class => "list"}, esc_path($diff->{'file'}));
3247 print "</td>\n";
3248 print "<td>$mode_chng</td>\n";
3249 print "<td class=\"link\">";
3250 if ($action eq 'commitdiff') {
3251 # link to patch
3252 $patchno++;
3253 print $cgi->a({-href => "#patch$patchno"}, "patch");
3254 print " | ";
3256 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3257 hash_base=>$hash, file_name=>$diff->{'file'})},
3258 "blob");
3259 print "</td>\n";
3261 } elsif ($diff->{'status'} eq "D") { # deleted
3262 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3263 print "<td>";
3264 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3265 hash_base=>$parent, file_name=>$diff->{'file'}),
3266 -class => "list"}, esc_path($diff->{'file'}));
3267 print "</td>\n";
3268 print "<td>$mode_chng</td>\n";
3269 print "<td class=\"link\">";
3270 if ($action eq 'commitdiff') {
3271 # link to patch
3272 $patchno++;
3273 print $cgi->a({-href => "#patch$patchno"}, "patch");
3274 print " | ";
3276 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3277 hash_base=>$parent, file_name=>$diff->{'file'})},
3278 "blob") . " | ";
3279 if ($have_blame) {
3280 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3281 file_name=>$diff->{'file'})},
3282 "blame") . " | ";
3284 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3285 file_name=>$diff->{'file'})},
3286 "history");
3287 print "</td>\n";
3289 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3290 my $mode_chnge = "";
3291 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3292 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3293 if ($from_file_type ne $to_file_type) {
3294 $mode_chnge .= " from $from_file_type to $to_file_type";
3296 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3297 if ($from_mode_str && $to_mode_str) {
3298 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3299 } elsif ($to_mode_str) {
3300 $mode_chnge .= " mode: $to_mode_str";
3303 $mode_chnge .= "]</span>\n";
3305 print "<td>";
3306 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3307 hash_base=>$hash, file_name=>$diff->{'file'}),
3308 -class => "list"}, esc_path($diff->{'file'}));
3309 print "</td>\n";
3310 print "<td>$mode_chnge</td>\n";
3311 print "<td class=\"link\">";
3312 if ($action eq 'commitdiff') {
3313 # link to patch
3314 $patchno++;
3315 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3316 " | ";
3317 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3318 # "commit" view and modified file (not onlu mode changed)
3319 print $cgi->a({-href => href(action=>"blobdiff",
3320 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3321 hash_base=>$hash, hash_parent_base=>$parent,
3322 file_name=>$diff->{'file'})},
3323 "diff") .
3324 " | ";
3326 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3327 hash_base=>$hash, file_name=>$diff->{'file'})},
3328 "blob") . " | ";
3329 if ($have_blame) {
3330 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3331 file_name=>$diff->{'file'})},
3332 "blame") . " | ";
3334 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3335 file_name=>$diff->{'file'})},
3336 "history");
3337 print "</td>\n";
3339 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3340 my %status_name = ('R' => 'moved', 'C' => 'copied');
3341 my $nstatus = $status_name{$diff->{'status'}};
3342 my $mode_chng = "";
3343 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3344 # mode also for directories, so we cannot use $to_mode_str
3345 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3347 print "<td>" .
3348 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3349 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3350 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3351 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3352 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3353 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3354 -class => "list"}, esc_path($diff->{'from_file'})) .
3355 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3356 "<td class=\"link\">";
3357 if ($action eq 'commitdiff') {
3358 # link to patch
3359 $patchno++;
3360 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3361 " | ";
3362 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3363 # "commit" view and modified file (not only pure rename or copy)
3364 print $cgi->a({-href => href(action=>"blobdiff",
3365 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3366 hash_base=>$hash, hash_parent_base=>$parent,
3367 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3368 "diff") .
3369 " | ";
3371 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3372 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3373 "blob") . " | ";
3374 if ($have_blame) {
3375 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3376 file_name=>$diff->{'to_file'})},
3377 "blame") . " | ";
3379 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3380 file_name=>$diff->{'to_file'})},
3381 "history");
3382 print "</td>\n";
3384 } # we should not encounter Unmerged (U) or Unknown (X) status
3385 print "</tr>\n";
3387 print "</tbody>" if $has_header;
3388 print "</table>\n";
3391 sub git_patchset_body {
3392 my ($fd, $difftree, $hash, @hash_parents) = @_;
3393 my ($hash_parent) = $hash_parents[0];
3395 my $is_combined = (@hash_parents > 1);
3396 my $patch_idx = 0;
3397 my $patch_number = 0;
3398 my $patch_line;
3399 my $diffinfo;
3400 my $to_name;
3401 my (%from, %to);
3403 print "<div class=\"patchset\">\n";
3405 # skip to first patch
3406 while ($patch_line = <$fd>) {
3407 chomp $patch_line;
3409 last if ($patch_line =~ m/^diff /);
3412 PATCH:
3413 while ($patch_line) {
3415 # parse "git diff" header line
3416 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3417 # $1 is from_name, which we do not use
3418 $to_name = unquote($2);
3419 $to_name =~ s!^b/!!;
3420 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3421 # $1 is 'cc' or 'combined', which we do not use
3422 $to_name = unquote($2);
3423 } else {
3424 $to_name = undef;
3427 # check if current patch belong to current raw line
3428 # and parse raw git-diff line if needed
3429 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3430 # this is continuation of a split patch
3431 print "<div class=\"patch cont\">\n";
3432 } else {
3433 # advance raw git-diff output if needed
3434 $patch_idx++ if defined $diffinfo;
3436 # read and prepare patch information
3437 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3439 # compact combined diff output can have some patches skipped
3440 # find which patch (using pathname of result) we are at now;
3441 if ($is_combined) {
3442 while ($to_name ne $diffinfo->{'to_file'}) {
3443 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3444 format_diff_cc_simplified($diffinfo, @hash_parents) .
3445 "</div>\n"; # class="patch"
3447 $patch_idx++;
3448 $patch_number++;
3450 last if $patch_idx > $#$difftree;
3451 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3455 # modifies %from, %to hashes
3456 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3458 # this is first patch for raw difftree line with $patch_idx index
3459 # we index @$difftree array from 0, but number patches from 1
3460 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3463 # git diff header
3464 #assert($patch_line =~ m/^diff /) if DEBUG;
3465 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3466 $patch_number++;
3467 # print "git diff" header
3468 print format_git_diff_header_line($patch_line, $diffinfo,
3469 \%from, \%to);
3471 # print extended diff header
3472 print "<div class=\"diff extended_header\">\n";
3473 EXTENDED_HEADER:
3474 while ($patch_line = <$fd>) {
3475 chomp $patch_line;
3477 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3479 print format_extended_diff_header_line($patch_line, $diffinfo,
3480 \%from, \%to);
3482 print "</div>\n"; # class="diff extended_header"
3484 # from-file/to-file diff header
3485 if (! $patch_line) {
3486 print "</div>\n"; # class="patch"
3487 last PATCH;
3489 next PATCH if ($patch_line =~ m/^diff /);
3490 #assert($patch_line =~ m/^---/) if DEBUG;
3492 my $last_patch_line = $patch_line;
3493 $patch_line = <$fd>;
3494 chomp $patch_line;
3495 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3497 print format_diff_from_to_header($last_patch_line, $patch_line,
3498 $diffinfo, \%from, \%to,
3499 @hash_parents);
3501 # the patch itself
3502 LINE:
3503 while ($patch_line = <$fd>) {
3504 chomp $patch_line;
3506 next PATCH if ($patch_line =~ m/^diff /);
3508 print format_diff_line($patch_line, \%from, \%to);
3511 } continue {
3512 print "</div>\n"; # class="patch"
3515 # for compact combined (--cc) format, with chunk and patch simpliciaction
3516 # patchset might be empty, but there might be unprocessed raw lines
3517 for (++$patch_idx if $patch_number > 0;
3518 $patch_idx < @$difftree;
3519 ++$patch_idx) {
3520 # read and prepare patch information
3521 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3523 # generate anchor for "patch" links in difftree / whatchanged part
3524 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3525 format_diff_cc_simplified($diffinfo, @hash_parents) .
3526 "</div>\n"; # class="patch"
3528 $patch_number++;
3531 if ($patch_number == 0) {
3532 if (@hash_parents > 1) {
3533 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3534 } else {
3535 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3539 print "</div>\n"; # class="patchset"
3542 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3544 # fills project list info (age, description, owner, forks) for each
3545 # project in the list, removing invalid projects from returned list
3546 # NOTE: modifies $projlist, but does not remove entries from it
3547 sub fill_project_list_info {
3548 my ($projlist, $check_forks) = @_;
3549 my @projects;
3551 PROJECT:
3552 foreach my $pr (@$projlist) {
3553 my (@activity) = git_get_last_activity($pr->{'path'});
3554 unless (@activity) {
3555 next PROJECT;
3557 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3558 if (!defined $pr->{'descr'}) {
3559 my $descr = git_get_project_description($pr->{'path'}) || "";
3560 $descr = to_utf8($descr);
3561 $pr->{'descr_long'} = $descr;
3562 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3564 if (!defined $pr->{'owner'}) {
3565 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3567 if ($check_forks) {
3568 my $pname = $pr->{'path'};
3569 if (($pname =~ s/\.git$//) &&
3570 ($pname !~ /\/$/) &&
3571 (-d "$projectroot/$pname")) {
3572 $pr->{'forks'} = "-d $projectroot/$pname";
3573 } else {
3574 $pr->{'forks'} = 0;
3577 push @projects, $pr;
3580 return @projects;
3583 # print 'sort by' <th> element, either sorting by $key if $name eq $order
3584 # (changing $list), or generating 'sort by $name' replay link otherwise
3585 sub print_sort_th {
3586 my ($str_sort, $name, $order, $key, $header, $list) = @_;
3587 $key ||= $name;
3588 $header ||= ucfirst($name);
3590 if ($order eq $name) {
3591 if ($str_sort) {
3592 @$list = sort {$a->{$key} cmp $b->{$key}} @$list;
3593 } else {
3594 @$list = sort {$a->{$key} <=> $b->{$key}} @$list;
3596 print "<th>$header</th>\n";
3597 } else {
3598 print "<th>" .
3599 $cgi->a({-href => href(-replay=>1, order=>$name),
3600 -class => "header"}, $header) .
3601 "</th>\n";
3605 sub print_sort_th_str {
3606 print_sort_th(1, @_);
3609 sub print_sort_th_num {
3610 print_sort_th(0, @_);
3613 sub git_project_list_body {
3614 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3616 my ($check_forks) = gitweb_check_feature('forks');
3617 my @projects = fill_project_list_info($projlist, $check_forks);
3619 $order ||= $default_projects_order;
3620 $from = 0 unless defined $from;
3621 $to = $#projects if (!defined $to || $#projects < $to);
3623 print "<table class=\"project_list\">\n";
3624 unless ($no_header) {
3625 print "<tr>\n";
3626 if ($check_forks) {
3627 print "<th></th>\n";
3629 print_sort_th_str('project', $order, 'path',
3630 'Project', \@projects);
3631 print_sort_th_str('descr', $order, 'descr_long',
3632 'Description', \@projects);
3633 print_sort_th_str('owner', $order, 'owner',
3634 'Owner', \@projects);
3635 print_sort_th_num('age', $order, 'age',
3636 'Last Change', \@projects);
3637 print "<th></th>\n" . # for links
3638 "</tr>\n";
3640 my $alternate = 1;
3641 for (my $i = $from; $i <= $to; $i++) {
3642 my $pr = $projects[$i];
3643 if ($alternate) {
3644 print "<tr class=\"dark\">\n";
3645 } else {
3646 print "<tr class=\"light\">\n";
3648 $alternate ^= 1;
3649 if ($check_forks) {
3650 print "<td>";
3651 if ($pr->{'forks'}) {
3652 print "<!-- $pr->{'forks'} -->\n";
3653 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3655 print "</td>\n";
3657 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3658 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3659 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3660 -class => "list", -title => $pr->{'descr_long'}},
3661 esc_html($pr->{'descr'})) . "</td>\n" .
3662 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3663 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3664 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3665 "<td class=\"link\">" .
3666 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3667 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3668 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3669 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3670 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3671 "</td>\n" .
3672 "</tr>\n";
3674 if (defined $extra) {
3675 print "<tr>\n";
3676 if ($check_forks) {
3677 print "<td></td>\n";
3679 print "<td colspan=\"5\">$extra</td>\n" .
3680 "</tr>\n";
3682 print "</table>\n";
3685 sub git_shortlog_body {
3686 # uses global variable $project
3687 my ($commitlist, $from, $to, $refs, $extra) = @_;
3689 $from = 0 unless defined $from;
3690 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3692 print "<table class=\"shortlog\">\n";
3693 my $alternate = 1;
3694 for (my $i = $from; $i <= $to; $i++) {
3695 my %co = %{$commitlist->[$i]};
3696 my $commit = $co{'id'};
3697 my $ref = format_ref_marker($refs, $commit);
3698 if ($alternate) {
3699 print "<tr class=\"dark\">\n";
3700 } else {
3701 print "<tr class=\"light\">\n";
3703 $alternate ^= 1;
3704 my $author = chop_and_escape_str($co{'author_name'}, 10);
3705 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3706 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3707 "<td><i>" . $author . "</i></td>\n" .
3708 "<td>";
3709 print format_subject_html($co{'title'}, $co{'title_short'},
3710 href(action=>"commit", hash=>$commit), $ref);
3711 print "</td>\n" .
3712 "<td class=\"link\">" .
3713 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3714 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3715 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3716 my $snapshot_links = format_snapshot_links($commit);
3717 if (defined $snapshot_links) {
3718 print " | " . $snapshot_links;
3720 print "</td>\n" .
3721 "</tr>\n";
3723 if (defined $extra) {
3724 print "<tr>\n" .
3725 "<td colspan=\"4\">$extra</td>\n" .
3726 "</tr>\n";
3728 print "</table>\n";
3731 sub git_history_body {
3732 # Warning: assumes constant type (blob or tree) during history
3733 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3735 $from = 0 unless defined $from;
3736 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3738 print "<table class=\"history\">\n";
3739 my $alternate = 1;
3740 for (my $i = $from; $i <= $to; $i++) {
3741 my %co = %{$commitlist->[$i]};
3742 if (!%co) {
3743 next;
3745 my $commit = $co{'id'};
3747 my $ref = format_ref_marker($refs, $commit);
3749 if ($alternate) {
3750 print "<tr class=\"dark\">\n";
3751 } else {
3752 print "<tr class=\"light\">\n";
3754 $alternate ^= 1;
3755 # shortlog uses chop_str($co{'author_name'}, 10)
3756 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3757 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3758 "<td><i>" . $author . "</i></td>\n" .
3759 "<td>";
3760 # originally git_history used chop_str($co{'title'}, 50)
3761 print format_subject_html($co{'title'}, $co{'title_short'},
3762 href(action=>"commit", hash=>$commit), $ref);
3763 print "</td>\n" .
3764 "<td class=\"link\">" .
3765 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3766 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3768 if ($ftype eq 'blob') {
3769 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3770 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3771 if (defined $blob_current && defined $blob_parent &&
3772 $blob_current ne $blob_parent) {
3773 print " | " .
3774 $cgi->a({-href => href(action=>"blobdiff",
3775 hash=>$blob_current, hash_parent=>$blob_parent,
3776 hash_base=>$hash_base, hash_parent_base=>$commit,
3777 file_name=>$file_name)},
3778 "diff to current");
3781 print "</td>\n" .
3782 "</tr>\n";
3784 if (defined $extra) {
3785 print "<tr>\n" .
3786 "<td colspan=\"4\">$extra</td>\n" .
3787 "</tr>\n";
3789 print "</table>\n";
3792 sub git_tags_body {
3793 # uses global variable $project
3794 my ($taglist, $from, $to, $extra) = @_;
3795 $from = 0 unless defined $from;
3796 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3798 print "<table class=\"tags\">\n";
3799 my $alternate = 1;
3800 for (my $i = $from; $i <= $to; $i++) {
3801 my $entry = $taglist->[$i];
3802 my %tag = %$entry;
3803 my $comment = $tag{'subject'};
3804 my $comment_short;
3805 if (defined $comment) {
3806 $comment_short = chop_str($comment, 30, 5);
3808 if ($alternate) {
3809 print "<tr class=\"dark\">\n";
3810 } else {
3811 print "<tr class=\"light\">\n";
3813 $alternate ^= 1;
3814 if (defined $tag{'age'}) {
3815 print "<td><i>$tag{'age'}</i></td>\n";
3816 } else {
3817 print "<td></td>\n";
3819 print "<td>" .
3820 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3821 -class => "list name"}, esc_html($tag{'name'})) .
3822 "</td>\n" .
3823 "<td>";
3824 if (defined $comment) {
3825 print format_subject_html($comment, $comment_short,
3826 href(action=>"tag", hash=>$tag{'id'}));
3828 print "</td>\n" .
3829 "<td class=\"selflink\">";
3830 if ($tag{'type'} eq "tag") {
3831 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3832 } else {
3833 print "&nbsp;";
3835 print "</td>\n" .
3836 "<td class=\"link\">" . " | " .
3837 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3838 if ($tag{'reftype'} eq "commit") {
3839 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
3840 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
3841 } elsif ($tag{'reftype'} eq "blob") {
3842 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3844 print "</td>\n" .
3845 "</tr>";
3847 if (defined $extra) {
3848 print "<tr>\n" .
3849 "<td colspan=\"5\">$extra</td>\n" .
3850 "</tr>\n";
3852 print "</table>\n";
3855 sub git_heads_body {
3856 # uses global variable $project
3857 my ($headlist, $head, $from, $to, $extra) = @_;
3858 $from = 0 unless defined $from;
3859 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3861 print "<table class=\"heads\">\n";
3862 my $alternate = 1;
3863 for (my $i = $from; $i <= $to; $i++) {
3864 my $entry = $headlist->[$i];
3865 my %ref = %$entry;
3866 my $curr = $ref{'id'} eq $head;
3867 if ($alternate) {
3868 print "<tr class=\"dark\">\n";
3869 } else {
3870 print "<tr class=\"light\">\n";
3872 $alternate ^= 1;
3873 print "<td><i>$ref{'age'}</i></td>\n" .
3874 ($curr ? "<td class=\"current_head\">" : "<td>") .
3875 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
3876 -class => "list name"},esc_html($ref{'name'})) .
3877 "</td>\n" .
3878 "<td class=\"link\">" .
3879 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
3880 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
3881 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
3882 "</td>\n" .
3883 "</tr>";
3885 if (defined $extra) {
3886 print "<tr>\n" .
3887 "<td colspan=\"3\">$extra</td>\n" .
3888 "</tr>\n";
3890 print "</table>\n";
3893 sub git_search_grep_body {
3894 my ($commitlist, $from, $to, $extra) = @_;
3895 $from = 0 unless defined $from;
3896 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3898 print "<table class=\"commit_search\">\n";
3899 my $alternate = 1;
3900 for (my $i = $from; $i <= $to; $i++) {
3901 my %co = %{$commitlist->[$i]};
3902 if (!%co) {
3903 next;
3905 my $commit = $co{'id'};
3906 if ($alternate) {
3907 print "<tr class=\"dark\">\n";
3908 } else {
3909 print "<tr class=\"light\">\n";
3911 $alternate ^= 1;
3912 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3913 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3914 "<td><i>" . $author . "</i></td>\n" .
3915 "<td>" .
3916 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3917 -class => "list subject"},
3918 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3919 my $comment = $co{'comment'};
3920 foreach my $line (@$comment) {
3921 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
3922 my ($lead, $match, $trail) = ($1, $2, $3);
3923 $match = chop_str($match, 70, 5, 'center');
3924 my $contextlen = int((80 - length($match))/2);
3925 $contextlen = 30 if ($contextlen > 30);
3926 $lead = chop_str($lead, $contextlen, 10, 'left');
3927 $trail = chop_str($trail, $contextlen, 10, 'right');
3929 $lead = esc_html($lead);
3930 $match = esc_html($match);
3931 $trail = esc_html($trail);
3933 print "$lead<span class=\"match\">$match</span>$trail<br />";
3936 print "</td>\n" .
3937 "<td class=\"link\">" .
3938 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3939 " | " .
3940 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
3941 " | " .
3942 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3943 print "</td>\n" .
3944 "</tr>\n";
3946 if (defined $extra) {
3947 print "<tr>\n" .
3948 "<td colspan=\"3\">$extra</td>\n" .
3949 "</tr>\n";
3951 print "</table>\n";
3954 ## ======================================================================
3955 ## ======================================================================
3956 ## actions
3958 sub git_project_list {
3959 my $order = $cgi->param('o');
3960 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3961 die_error(400, "Unknown order parameter");
3964 my @list = git_get_projects_list();
3965 if (!@list) {
3966 die_error(404, "No projects found");
3969 git_header_html();
3970 if (-f $home_text) {
3971 print "<div class=\"index_include\">\n";
3972 open (my $fd, $home_text);
3973 print <$fd>;
3974 close $fd;
3975 print "</div>\n";
3977 git_project_list_body(\@list, $order);
3978 git_footer_html();
3981 sub git_forks {
3982 my $order = $cgi->param('o');
3983 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3984 die_error(400, "Unknown order parameter");
3987 my @list = git_get_projects_list($project);
3988 if (!@list) {
3989 die_error(404, "No forks found");
3992 git_header_html();
3993 git_print_page_nav('','');
3994 git_print_header_div('summary', "$project forks");
3995 git_project_list_body(\@list, $order);
3996 git_footer_html();
3999 sub git_project_index {
4000 my @projects = git_get_projects_list($project);
4002 print $cgi->header(
4003 -type => 'text/plain',
4004 -charset => 'utf-8',
4005 -content_disposition => 'inline; filename="index.aux"');
4007 foreach my $pr (@projects) {
4008 if (!exists $pr->{'owner'}) {
4009 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4012 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4013 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4014 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4015 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4016 $path =~ s/ /\+/g;
4017 $owner =~ s/ /\+/g;
4019 print "$path $owner\n";
4023 sub git_summary {
4024 my $descr = git_get_project_description($project) || "none";
4025 my %co = parse_commit("HEAD");
4026 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4027 my $head = $co{'id'};
4029 my $owner = git_get_project_owner($project);
4031 my $refs = git_get_references();
4032 # These get_*_list functions return one more to allow us to see if
4033 # there are more ...
4034 my @taglist = git_get_tags_list(16);
4035 my @headlist = git_get_heads_list(16);
4036 my @forklist;
4037 my ($check_forks) = gitweb_check_feature('forks');
4039 if ($check_forks) {
4040 @forklist = git_get_projects_list($project);
4043 git_header_html();
4044 git_print_page_nav('summary','', $head);
4046 print "<div class=\"title\">&nbsp;</div>\n";
4047 print "<table class=\"projects_list\">\n" .
4048 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4049 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4050 if (defined $cd{'rfc2822'}) {
4051 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4054 # use per project git URL list in $projectroot/$project/cloneurl
4055 # or make project git URL from git base URL and project name
4056 my $url_tag = "URL";
4057 my @url_list = git_get_project_url_list($project);
4058 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4059 foreach my $git_url (@url_list) {
4060 next unless $git_url;
4061 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
4062 $url_tag = "";
4064 print "</table>\n";
4066 if (-s "$projectroot/$project/README.html") {
4067 if (open my $fd, "$projectroot/$project/README.html") {
4068 print "<div class=\"title\">readme</div>\n" .
4069 "<div class=\"readme\">\n";
4070 print $_ while (<$fd>);
4071 print "\n</div>\n"; # class="readme"
4072 close $fd;
4076 # we need to request one more than 16 (0..15) to check if
4077 # those 16 are all
4078 my @commitlist = $head ? parse_commits($head, 17) : ();
4079 if (@commitlist) {
4080 git_print_header_div('shortlog');
4081 git_shortlog_body(\@commitlist, 0, 15, $refs,
4082 $#commitlist <= 15 ? undef :
4083 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4086 if (@taglist) {
4087 git_print_header_div('tags');
4088 git_tags_body(\@taglist, 0, 15,
4089 $#taglist <= 15 ? undef :
4090 $cgi->a({-href => href(action=>"tags")}, "..."));
4093 if (@headlist) {
4094 git_print_header_div('heads');
4095 git_heads_body(\@headlist, $head, 0, 15,
4096 $#headlist <= 15 ? undef :
4097 $cgi->a({-href => href(action=>"heads")}, "..."));
4100 if (@forklist) {
4101 git_print_header_div('forks');
4102 git_project_list_body(\@forklist, undef, 0, 15,
4103 $#forklist <= 15 ? undef :
4104 $cgi->a({-href => href(action=>"forks")}, "..."),
4105 'noheader');
4108 git_footer_html();
4111 sub git_tag {
4112 my $head = git_get_head_hash($project);
4113 git_header_html();
4114 git_print_page_nav('','', $head,undef,$head);
4115 my %tag = parse_tag($hash);
4117 if (! %tag) {
4118 die_error(404, "Unknown tag object");
4121 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4122 print "<div class=\"title_text\">\n" .
4123 "<table class=\"object_header\">\n" .
4124 "<tr>\n" .
4125 "<td>object</td>\n" .
4126 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4127 $tag{'object'}) . "</td>\n" .
4128 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4129 $tag{'type'}) . "</td>\n" .
4130 "</tr>\n";
4131 if (defined($tag{'author'})) {
4132 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4133 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4134 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4135 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4136 "</td></tr>\n";
4138 print "</table>\n\n" .
4139 "</div>\n";
4140 print "<div class=\"page_body\">";
4141 my $comment = $tag{'comment'};
4142 foreach my $line (@$comment) {
4143 chomp $line;
4144 print esc_html($line, -nbsp=>1) . "<br/>\n";
4146 print "</div>\n";
4147 git_footer_html();
4150 sub git_blame {
4151 my $fd;
4152 my $ftype;
4154 gitweb_check_feature('blame')
4155 or die_error(403, "Blame view not allowed");
4157 die_error(400, "No file name given") unless $file_name;
4158 $hash_base ||= git_get_head_hash($project);
4159 die_error(404, "Couldn't find base commit") unless ($hash_base);
4160 my %co = parse_commit($hash_base)
4161 or die_error(404, "Commit not found");
4162 if (!defined $hash) {
4163 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4164 or die_error(404, "Error looking up file");
4166 $ftype = git_get_type($hash);
4167 if ($ftype !~ "blob") {
4168 die_error(400, "Object is not a blob");
4170 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4171 $file_name, $hash_base)
4172 or die_error(500, "Open git-blame failed");
4173 git_header_html();
4174 my $formats_nav =
4175 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4176 "blob") .
4177 " | " .
4178 $cgi->a({-href => href(action=>"history", -replay=>1)},
4179 "history") .
4180 " | " .
4181 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4182 "HEAD");
4183 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4184 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4185 git_print_page_path($file_name, $ftype, $hash_base);
4186 my @rev_color = (qw(light2 dark2));
4187 my $num_colors = scalar(@rev_color);
4188 my $current_color = 0;
4189 my $last_rev;
4190 print <<HTML;
4191 <div class="page_body">
4192 <table class="blame">
4193 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4194 HTML
4195 my %metainfo = ();
4196 while (1) {
4197 $_ = <$fd>;
4198 last unless defined $_;
4199 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4200 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4201 if (!exists $metainfo{$full_rev}) {
4202 $metainfo{$full_rev} = {};
4204 my $meta = $metainfo{$full_rev};
4205 while (<$fd>) {
4206 last if (s/^\t//);
4207 if (/^(\S+) (.*)$/) {
4208 $meta->{$1} = $2;
4211 my $data = $_;
4212 chomp $data;
4213 my $rev = substr($full_rev, 0, 8);
4214 my $author = $meta->{'author'};
4215 my %date = parse_date($meta->{'author-time'},
4216 $meta->{'author-tz'});
4217 my $date = $date{'iso-tz'};
4218 if ($group_size) {
4219 $current_color = ++$current_color % $num_colors;
4221 print "<tr class=\"$rev_color[$current_color]\">\n";
4222 if ($group_size) {
4223 print "<td class=\"sha1\"";
4224 print " title=\"". esc_html($author) . ", $date\"";
4225 print " rowspan=\"$group_size\"" if ($group_size > 1);
4226 print ">";
4227 print $cgi->a({-href => href(action=>"commit",
4228 hash=>$full_rev,
4229 file_name=>$file_name)},
4230 esc_html($rev));
4231 print "</td>\n";
4233 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4234 or die_error(500, "Open git-rev-parse failed");
4235 my $parent_commit = <$dd>;
4236 close $dd;
4237 chomp($parent_commit);
4238 my $blamed = href(action => 'blame',
4239 file_name => $meta->{'filename'},
4240 hash_base => $parent_commit);
4241 print "<td class=\"linenr\">";
4242 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4243 -id => "l$lineno",
4244 -class => "linenr" },
4245 esc_html($lineno));
4246 print "</td>";
4247 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4248 print "</tr>\n";
4250 print "</table>\n";
4251 print "</div>";
4252 close $fd
4253 or print "Reading blob failed\n";
4254 git_footer_html();
4257 sub git_tags {
4258 my $head = git_get_head_hash($project);
4259 git_header_html();
4260 git_print_page_nav('','', $head,undef,$head);
4261 git_print_header_div('summary', $project);
4263 my @tagslist = git_get_tags_list();
4264 if (@tagslist) {
4265 git_tags_body(\@tagslist);
4267 git_footer_html();
4270 sub git_heads {
4271 my $head = git_get_head_hash($project);
4272 git_header_html();
4273 git_print_page_nav('','', $head,undef,$head);
4274 git_print_header_div('summary', $project);
4276 my @headslist = git_get_heads_list();
4277 if (@headslist) {
4278 git_heads_body(\@headslist, $head);
4280 git_footer_html();
4283 sub git_blob_plain {
4284 my $type = shift;
4285 my $expires;
4287 if (!defined $hash) {
4288 if (defined $file_name) {
4289 my $base = $hash_base || git_get_head_hash($project);
4290 $hash = git_get_hash_by_path($base, $file_name, "blob")
4291 or die_error(404, "Cannot find file");
4292 } else {
4293 die_error(400, "No file name defined");
4295 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4296 # blobs defined by non-textual hash id's can be cached
4297 $expires = "+1d";
4300 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4301 or die_error(500, "Open git-cat-file blob '$hash' failed");
4303 # content-type (can include charset)
4304 $type = blob_contenttype($fd, $file_name, $type);
4306 # "save as" filename, even when no $file_name is given
4307 my $save_as = "$hash";
4308 if (defined $file_name) {
4309 $save_as = $file_name;
4310 } elsif ($type =~ m/^text\//) {
4311 $save_as .= '.txt';
4314 print $cgi->header(
4315 -type => $type,
4316 -expires => $expires,
4317 -content_disposition => 'inline; filename="' . $save_as . '"');
4318 undef $/;
4319 binmode STDOUT, ':raw';
4320 print <$fd>;
4321 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4322 $/ = "\n";
4323 close $fd;
4326 sub git_blob {
4327 my $expires;
4329 if (!defined $hash) {
4330 if (defined $file_name) {
4331 my $base = $hash_base || git_get_head_hash($project);
4332 $hash = git_get_hash_by_path($base, $file_name, "blob")
4333 or die_error(404, "Cannot find file");
4334 } else {
4335 die_error(400, "No file name defined");
4337 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4338 # blobs defined by non-textual hash id's can be cached
4339 $expires = "+1d";
4342 my ($have_blame) = gitweb_check_feature('blame');
4343 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4344 or die_error(500, "Couldn't cat $file_name, $hash");
4345 my $mimetype = blob_mimetype($fd, $file_name);
4346 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4347 close $fd;
4348 return git_blob_plain($mimetype);
4350 # we can have blame only for text/* mimetype
4351 $have_blame &&= ($mimetype =~ m!^text/!);
4353 git_header_html(undef, $expires);
4354 my $formats_nav = '';
4355 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4356 if (defined $file_name) {
4357 if ($have_blame) {
4358 $formats_nav .=
4359 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4360 "blame") .
4361 " | ";
4363 $formats_nav .=
4364 $cgi->a({-href => href(action=>"history", -replay=>1)},
4365 "history") .
4366 " | " .
4367 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4368 "raw") .
4369 " | " .
4370 $cgi->a({-href => href(action=>"blob",
4371 hash_base=>"HEAD", file_name=>$file_name)},
4372 "HEAD");
4373 } else {
4374 $formats_nav .=
4375 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4376 "raw");
4378 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4379 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4380 } else {
4381 print "<div class=\"page_nav\">\n" .
4382 "<br/><br/></div>\n" .
4383 "<div class=\"title\">$hash</div>\n";
4385 git_print_page_path($file_name, "blob", $hash_base);
4386 print "<div class=\"page_body\">\n";
4387 if ($mimetype =~ m!^image/!) {
4388 print qq!<img type="$mimetype"!;
4389 if ($file_name) {
4390 print qq! alt="$file_name" title="$file_name"!;
4392 print qq! src="! .
4393 href(action=>"blob_plain", hash=>$hash,
4394 hash_base=>$hash_base, file_name=>$file_name) .
4395 qq!" />\n!;
4396 } else {
4397 my $nr;
4398 while (my $line = <$fd>) {
4399 chomp $line;
4400 $nr++;
4401 $line = untabify($line);
4402 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4403 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4406 close $fd
4407 or print "Reading blob failed.\n";
4408 print "</div>";
4409 git_footer_html();
4412 sub git_tree {
4413 if (!defined $hash_base) {
4414 $hash_base = "HEAD";
4416 if (!defined $hash) {
4417 if (defined $file_name) {
4418 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4419 } else {
4420 $hash = $hash_base;
4423 $/ = "\0";
4424 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4425 or die_error(500, "Open git-ls-tree failed");
4426 my @entries = map { chomp; $_ } <$fd>;
4427 close $fd or die_error(404, "Reading tree failed");
4428 $/ = "\n";
4430 my $refs = git_get_references();
4431 my $ref = format_ref_marker($refs, $hash_base);
4432 git_header_html();
4433 my $basedir = '';
4434 my ($have_blame) = gitweb_check_feature('blame');
4435 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4436 my @views_nav = ();
4437 if (defined $file_name) {
4438 push @views_nav,
4439 $cgi->a({-href => href(action=>"history", -replay=>1)},
4440 "history"),
4441 $cgi->a({-href => href(action=>"tree",
4442 hash_base=>"HEAD", file_name=>$file_name)},
4443 "HEAD"),
4445 my $snapshot_links = format_snapshot_links($hash);
4446 if (defined $snapshot_links) {
4447 # FIXME: Should be available when we have no hash base as well.
4448 push @views_nav, $snapshot_links;
4450 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4451 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4452 } else {
4453 undef $hash_base;
4454 print "<div class=\"page_nav\">\n";
4455 print "<br/><br/></div>\n";
4456 print "<div class=\"title\">$hash</div>\n";
4458 if (defined $file_name) {
4459 $basedir = $file_name;
4460 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4461 $basedir .= '/';
4464 git_print_page_path($file_name, 'tree', $hash_base);
4465 print "<div class=\"page_body\">\n";
4466 print "<table class=\"tree\">\n";
4467 my $alternate = 1;
4468 # '..' (top directory) link if possible
4469 if (defined $hash_base &&
4470 defined $file_name && $file_name =~ m![^/]+$!) {
4471 if ($alternate) {
4472 print "<tr class=\"dark\">\n";
4473 } else {
4474 print "<tr class=\"light\">\n";
4476 $alternate ^= 1;
4478 my $up = $file_name;
4479 $up =~ s!/?[^/]+$!!;
4480 undef $up unless $up;
4481 # based on git_print_tree_entry
4482 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4483 print '<td class="list">';
4484 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4485 file_name=>$up)},
4486 "..");
4487 print "</td>\n";
4488 print "<td class=\"link\"></td>\n";
4490 print "</tr>\n";
4492 foreach my $line (@entries) {
4493 my %t = parse_ls_tree_line($line, -z => 1);
4495 if ($alternate) {
4496 print "<tr class=\"dark\">\n";
4497 } else {
4498 print "<tr class=\"light\">\n";
4500 $alternate ^= 1;
4502 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4504 print "</tr>\n";
4506 print "</table>\n" .
4507 "</div>";
4508 git_footer_html();
4511 sub git_snapshot {
4512 my @supported_fmts = gitweb_check_feature('snapshot');
4513 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4515 my $format = $cgi->param('sf');
4516 if (!@supported_fmts) {
4517 die_error(403, "Snapshots not allowed");
4519 # default to first supported snapshot format
4520 $format ||= $supported_fmts[0];
4521 if ($format !~ m/^[a-z0-9]+$/) {
4522 die_error(400, "Invalid snapshot format parameter");
4523 } elsif (!exists($known_snapshot_formats{$format})) {
4524 die_error(400, "Unknown snapshot format");
4525 } elsif (!grep($_ eq $format, @supported_fmts)) {
4526 die_error(403, "Unsupported snapshot format");
4529 if (!defined $hash) {
4530 $hash = git_get_head_hash($project);
4533 my $name = $project;
4534 $name =~ s,([^/])/*\.git$,$1,;
4535 $name = basename($name);
4536 my $filename = to_utf8($name);
4537 $name =~ s/\047/\047\\\047\047/g;
4538 my $cmd;
4539 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4540 $cmd = quote_command(
4541 git_cmd(), 'archive',
4542 "--format=$known_snapshot_formats{$format}{'format'}",
4543 "--prefix=$name/", $hash);
4544 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4545 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
4548 print $cgi->header(
4549 -type => $known_snapshot_formats{$format}{'type'},
4550 -content_disposition => 'inline; filename="' . "$filename" . '"',
4551 -status => '200 OK');
4553 open my $fd, "-|", $cmd
4554 or die_error(500, "Execute git-archive failed");
4555 binmode STDOUT, ':raw';
4556 print <$fd>;
4557 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4558 close $fd;
4561 sub git_log {
4562 my $head = git_get_head_hash($project);
4563 if (!defined $hash) {
4564 $hash = $head;
4566 if (!defined $page) {
4567 $page = 0;
4569 my $refs = git_get_references();
4571 my @commitlist = parse_commits($hash, 101, (100 * $page));
4573 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
4575 git_header_html();
4576 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4578 if (!@commitlist) {
4579 my %co = parse_commit($hash);
4581 git_print_header_div('summary', $project);
4582 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4584 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4585 for (my $i = 0; $i <= $to; $i++) {
4586 my %co = %{$commitlist[$i]};
4587 next if !%co;
4588 my $commit = $co{'id'};
4589 my $ref = format_ref_marker($refs, $commit);
4590 my %ad = parse_date($co{'author_epoch'});
4591 git_print_header_div('commit',
4592 "<span class=\"age\">$co{'age_string'}</span>" .
4593 esc_html($co{'title'}) . $ref,
4594 $commit);
4595 print "<div class=\"title_text\">\n" .
4596 "<div class=\"log_link\">\n" .
4597 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4598 " | " .
4599 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4600 " | " .
4601 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4602 "<br/>\n" .
4603 "</div>\n" .
4604 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4605 "</div>\n";
4607 print "<div class=\"log_body\">\n";
4608 git_print_log($co{'comment'}, -final_empty_line=> 1);
4609 print "</div>\n";
4611 if ($#commitlist >= 100) {
4612 print "<div class=\"page_nav\">\n";
4613 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4614 -accesskey => "n", -title => "Alt-n"}, "next");
4615 print "</div>\n";
4617 git_footer_html();
4620 sub git_commit {
4621 $hash ||= $hash_base || "HEAD";
4622 my %co = parse_commit($hash)
4623 or die_error(404, "Unknown commit object");
4624 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4625 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4627 my $parent = $co{'parent'};
4628 my $parents = $co{'parents'}; # listref
4630 # we need to prepare $formats_nav before any parameter munging
4631 my $formats_nav;
4632 if (!defined $parent) {
4633 # --root commitdiff
4634 $formats_nav .= '(initial)';
4635 } elsif (@$parents == 1) {
4636 # single parent commit
4637 $formats_nav .=
4638 '(parent: ' .
4639 $cgi->a({-href => href(action=>"commit",
4640 hash=>$parent)},
4641 esc_html(substr($parent, 0, 7))) .
4642 ')';
4643 } else {
4644 # merge commit
4645 $formats_nav .=
4646 '(merge: ' .
4647 join(' ', map {
4648 $cgi->a({-href => href(action=>"commit",
4649 hash=>$_)},
4650 esc_html(substr($_, 0, 7)));
4651 } @$parents ) .
4652 ')';
4655 if (!defined $parent) {
4656 $parent = "--root";
4658 my @difftree;
4659 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4660 @diff_opts,
4661 (@$parents <= 1 ? $parent : '-c'),
4662 $hash, "--"
4663 or die_error(500, "Open git-diff-tree failed");
4664 @difftree = map { chomp; $_ } <$fd>;
4665 close $fd or die_error(404, "Reading git-diff-tree failed");
4667 # non-textual hash id's can be cached
4668 my $expires;
4669 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4670 $expires = "+1d";
4672 my $refs = git_get_references();
4673 my $ref = format_ref_marker($refs, $co{'id'});
4675 git_header_html(undef, $expires);
4676 git_print_page_nav('commit', '',
4677 $hash, $co{'tree'}, $hash,
4678 $formats_nav);
4680 if (defined $co{'parent'}) {
4681 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4682 } else {
4683 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4685 print "<div class=\"title_text\">\n" .
4686 "<table class=\"object_header\">\n";
4687 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4688 "<tr>" .
4689 "<td></td><td> $ad{'rfc2822'}";
4690 if ($ad{'hour_local'} < 6) {
4691 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4692 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4693 } else {
4694 printf(" (%02d:%02d %s)",
4695 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4697 print "</td>" .
4698 "</tr>\n";
4699 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4700 print "<tr><td></td><td> $cd{'rfc2822'}" .
4701 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4702 "</td></tr>\n";
4703 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4704 print "<tr>" .
4705 "<td>tree</td>" .
4706 "<td class=\"sha1\">" .
4707 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4708 class => "list"}, $co{'tree'}) .
4709 "</td>" .
4710 "<td class=\"link\">" .
4711 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4712 "tree");
4713 my $snapshot_links = format_snapshot_links($hash);
4714 if (defined $snapshot_links) {
4715 print " | " . $snapshot_links;
4717 print "</td>" .
4718 "</tr>\n";
4720 foreach my $par (@$parents) {
4721 print "<tr>" .
4722 "<td>parent</td>" .
4723 "<td class=\"sha1\">" .
4724 $cgi->a({-href => href(action=>"commit", hash=>$par),
4725 class => "list"}, $par) .
4726 "</td>" .
4727 "<td class=\"link\">" .
4728 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4729 " | " .
4730 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4731 "</td>" .
4732 "</tr>\n";
4734 print "</table>".
4735 "</div>\n";
4737 print "<div class=\"page_body\">\n";
4738 git_print_log($co{'comment'});
4739 print "</div>\n";
4741 git_difftree_body(\@difftree, $hash, @$parents);
4743 git_footer_html();
4746 sub git_object {
4747 # object is defined by:
4748 # - hash or hash_base alone
4749 # - hash_base and file_name
4750 my $type;
4752 # - hash or hash_base alone
4753 if ($hash || ($hash_base && !defined $file_name)) {
4754 my $object_id = $hash || $hash_base;
4756 open my $fd, "-|", quote_command(
4757 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
4758 or die_error(404, "Object does not exist");
4759 $type = <$fd>;
4760 chomp $type;
4761 close $fd
4762 or die_error(404, "Object does not exist");
4764 # - hash_base and file_name
4765 } elsif ($hash_base && defined $file_name) {
4766 $file_name =~ s,/+$,,;
4768 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4769 or die_error(404, "Base object does not exist");
4771 # here errors should not hapen
4772 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4773 or die_error(500, "Open git-ls-tree failed");
4774 my $line = <$fd>;
4775 close $fd;
4777 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4778 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4779 die_error(404, "File or directory for given base does not exist");
4781 $type = $2;
4782 $hash = $3;
4783 } else {
4784 die_error(400, "Not enough information to find object");
4787 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4788 hash=>$hash, hash_base=>$hash_base,
4789 file_name=>$file_name),
4790 -status => '302 Found');
4793 sub git_blobdiff {
4794 my $format = shift || 'html';
4796 my $fd;
4797 my @difftree;
4798 my %diffinfo;
4799 my $expires;
4801 # preparing $fd and %diffinfo for git_patchset_body
4802 # new style URI
4803 if (defined $hash_base && defined $hash_parent_base) {
4804 if (defined $file_name) {
4805 # read raw output
4806 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4807 $hash_parent_base, $hash_base,
4808 "--", (defined $file_parent ? $file_parent : ()), $file_name
4809 or die_error(500, "Open git-diff-tree failed");
4810 @difftree = map { chomp; $_ } <$fd>;
4811 close $fd
4812 or die_error(404, "Reading git-diff-tree failed");
4813 @difftree
4814 or die_error(404, "Blob diff not found");
4816 } elsif (defined $hash &&
4817 $hash =~ /[0-9a-fA-F]{40}/) {
4818 # try to find filename from $hash
4820 # read filtered raw output
4821 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4822 $hash_parent_base, $hash_base, "--"
4823 or die_error(500, "Open git-diff-tree failed");
4824 @difftree =
4825 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4826 # $hash == to_id
4827 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4828 map { chomp; $_ } <$fd>;
4829 close $fd
4830 or die_error(404, "Reading git-diff-tree failed");
4831 @difftree
4832 or die_error(404, "Blob diff not found");
4834 } else {
4835 die_error(400, "Missing one of the blob diff parameters");
4838 if (@difftree > 1) {
4839 die_error(400, "Ambiguous blob diff specification");
4842 %diffinfo = parse_difftree_raw_line($difftree[0]);
4843 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4844 $file_name ||= $diffinfo{'to_file'};
4846 $hash_parent ||= $diffinfo{'from_id'};
4847 $hash ||= $diffinfo{'to_id'};
4849 # non-textual hash id's can be cached
4850 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4851 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4852 $expires = '+1d';
4855 # open patch output
4856 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4857 '-p', ($format eq 'html' ? "--full-index" : ()),
4858 $hash_parent_base, $hash_base,
4859 "--", (defined $file_parent ? $file_parent : ()), $file_name
4860 or die_error(500, "Open git-diff-tree failed");
4863 # old/legacy style URI
4864 if (!%diffinfo && # if new style URI failed
4865 defined $hash && defined $hash_parent) {
4866 # fake git-diff-tree raw output
4867 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4868 $diffinfo{'from_id'} = $hash_parent;
4869 $diffinfo{'to_id'} = $hash;
4870 if (defined $file_name) {
4871 if (defined $file_parent) {
4872 $diffinfo{'status'} = '2';
4873 $diffinfo{'from_file'} = $file_parent;
4874 $diffinfo{'to_file'} = $file_name;
4875 } else { # assume not renamed
4876 $diffinfo{'status'} = '1';
4877 $diffinfo{'from_file'} = $file_name;
4878 $diffinfo{'to_file'} = $file_name;
4880 } else { # no filename given
4881 $diffinfo{'status'} = '2';
4882 $diffinfo{'from_file'} = $hash_parent;
4883 $diffinfo{'to_file'} = $hash;
4886 # non-textual hash id's can be cached
4887 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4888 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4889 $expires = '+1d';
4892 # open patch output
4893 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4894 '-p', ($format eq 'html' ? "--full-index" : ()),
4895 $hash_parent, $hash, "--"
4896 or die_error(500, "Open git-diff failed");
4897 } else {
4898 die_error(400, "Missing one of the blob diff parameters")
4899 unless %diffinfo;
4902 # header
4903 if ($format eq 'html') {
4904 my $formats_nav =
4905 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
4906 "raw");
4907 git_header_html(undef, $expires);
4908 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4909 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4910 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4911 } else {
4912 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4913 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4915 if (defined $file_name) {
4916 git_print_page_path($file_name, "blob", $hash_base);
4917 } else {
4918 print "<div class=\"page_path\"></div>\n";
4921 } elsif ($format eq 'plain') {
4922 print $cgi->header(
4923 -type => 'text/plain',
4924 -charset => 'utf-8',
4925 -expires => $expires,
4926 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4928 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4930 } else {
4931 die_error(400, "Unknown blobdiff format");
4934 # patch
4935 if ($format eq 'html') {
4936 print "<div class=\"page_body\">\n";
4938 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4939 close $fd;
4941 print "</div>\n"; # class="page_body"
4942 git_footer_html();
4944 } else {
4945 while (my $line = <$fd>) {
4946 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
4947 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
4949 print $line;
4951 last if $line =~ m!^\+\+\+!;
4953 local $/ = undef;
4954 print <$fd>;
4955 close $fd;
4959 sub git_blobdiff_plain {
4960 git_blobdiff('plain');
4963 sub git_commitdiff {
4964 my $format = shift || 'html';
4965 $hash ||= $hash_base || "HEAD";
4966 my %co = parse_commit($hash)
4967 or die_error(404, "Unknown commit object");
4969 # choose format for commitdiff for merge
4970 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
4971 $hash_parent = '--cc';
4973 # we need to prepare $formats_nav before almost any parameter munging
4974 my $formats_nav;
4975 if ($format eq 'html') {
4976 $formats_nav =
4977 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
4978 "raw");
4980 if (defined $hash_parent &&
4981 $hash_parent ne '-c' && $hash_parent ne '--cc') {
4982 # commitdiff with two commits given
4983 my $hash_parent_short = $hash_parent;
4984 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4985 $hash_parent_short = substr($hash_parent, 0, 7);
4987 $formats_nav .=
4988 ' (from';
4989 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
4990 if ($co{'parents'}[$i] eq $hash_parent) {
4991 $formats_nav .= ' parent ' . ($i+1);
4992 last;
4995 $formats_nav .= ': ' .
4996 $cgi->a({-href => href(action=>"commitdiff",
4997 hash=>$hash_parent)},
4998 esc_html($hash_parent_short)) .
4999 ')';
5000 } elsif (!$co{'parent'}) {
5001 # --root commitdiff
5002 $formats_nav .= ' (initial)';
5003 } elsif (scalar @{$co{'parents'}} == 1) {
5004 # single parent commit
5005 $formats_nav .=
5006 ' (parent: ' .
5007 $cgi->a({-href => href(action=>"commitdiff",
5008 hash=>$co{'parent'})},
5009 esc_html(substr($co{'parent'}, 0, 7))) .
5010 ')';
5011 } else {
5012 # merge commit
5013 if ($hash_parent eq '--cc') {
5014 $formats_nav .= ' | ' .
5015 $cgi->a({-href => href(action=>"commitdiff",
5016 hash=>$hash, hash_parent=>'-c')},
5017 'combined');
5018 } else { # $hash_parent eq '-c'
5019 $formats_nav .= ' | ' .
5020 $cgi->a({-href => href(action=>"commitdiff",
5021 hash=>$hash, hash_parent=>'--cc')},
5022 'compact');
5024 $formats_nav .=
5025 ' (merge: ' .
5026 join(' ', map {
5027 $cgi->a({-href => href(action=>"commitdiff",
5028 hash=>$_)},
5029 esc_html(substr($_, 0, 7)));
5030 } @{$co{'parents'}} ) .
5031 ')';
5035 my $hash_parent_param = $hash_parent;
5036 if (!defined $hash_parent_param) {
5037 # --cc for multiple parents, --root for parentless
5038 $hash_parent_param =
5039 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5042 # read commitdiff
5043 my $fd;
5044 my @difftree;
5045 if ($format eq 'html') {
5046 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5047 "--no-commit-id", "--patch-with-raw", "--full-index",
5048 $hash_parent_param, $hash, "--"
5049 or die_error(500, "Open git-diff-tree failed");
5051 while (my $line = <$fd>) {
5052 chomp $line;
5053 # empty line ends raw part of diff-tree output
5054 last unless $line;
5055 push @difftree, scalar parse_difftree_raw_line($line);
5058 } elsif ($format eq 'plain') {
5059 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5060 '-p', $hash_parent_param, $hash, "--"
5061 or die_error(500, "Open git-diff-tree failed");
5063 } else {
5064 die_error(400, "Unknown commitdiff format");
5067 # non-textual hash id's can be cached
5068 my $expires;
5069 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5070 $expires = "+1d";
5073 # write commit message
5074 if ($format eq 'html') {
5075 my $refs = git_get_references();
5076 my $ref = format_ref_marker($refs, $co{'id'});
5078 git_header_html(undef, $expires);
5079 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5080 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5081 git_print_authorship(\%co);
5082 print "<div class=\"page_body\">\n";
5083 if (@{$co{'comment'}} > 1) {
5084 print "<div class=\"log\">\n";
5085 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5086 print "</div>\n"; # class="log"
5089 } elsif ($format eq 'plain') {
5090 my $refs = git_get_references("tags");
5091 my $tagname = git_get_rev_name_tags($hash);
5092 my $filename = basename($project) . "-$hash.patch";
5094 print $cgi->header(
5095 -type => 'text/plain',
5096 -charset => 'utf-8',
5097 -expires => $expires,
5098 -content_disposition => 'inline; filename="' . "$filename" . '"');
5099 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5100 print "From: " . to_utf8($co{'author'}) . "\n";
5101 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5102 print "Subject: " . to_utf8($co{'title'}) . "\n";
5104 print "X-Git-Tag: $tagname\n" if $tagname;
5105 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5107 foreach my $line (@{$co{'comment'}}) {
5108 print to_utf8($line) . "\n";
5110 print "---\n\n";
5113 # write patch
5114 if ($format eq 'html') {
5115 my $use_parents = !defined $hash_parent ||
5116 $hash_parent eq '-c' || $hash_parent eq '--cc';
5117 git_difftree_body(\@difftree, $hash,
5118 $use_parents ? @{$co{'parents'}} : $hash_parent);
5119 print "<br/>\n";
5121 git_patchset_body($fd, \@difftree, $hash,
5122 $use_parents ? @{$co{'parents'}} : $hash_parent);
5123 close $fd;
5124 print "</div>\n"; # class="page_body"
5125 git_footer_html();
5127 } elsif ($format eq 'plain') {
5128 local $/ = undef;
5129 print <$fd>;
5130 close $fd
5131 or print "Reading git-diff-tree failed\n";
5135 sub git_commitdiff_plain {
5136 git_commitdiff('plain');
5139 sub git_history {
5140 if (!defined $hash_base) {
5141 $hash_base = git_get_head_hash($project);
5143 if (!defined $page) {
5144 $page = 0;
5146 my $ftype;
5147 my %co = parse_commit($hash_base)
5148 or die_error(404, "Unknown commit object");
5150 my $refs = git_get_references();
5151 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5153 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5154 $file_name, "--full-history")
5155 or die_error(404, "No such file or directory on given branch");
5157 if (!defined $hash && defined $file_name) {
5158 # some commits could have deleted file in question,
5159 # and not have it in tree, but one of them has to have it
5160 for (my $i = 0; $i <= @commitlist; $i++) {
5161 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5162 last if defined $hash;
5165 if (defined $hash) {
5166 $ftype = git_get_type($hash);
5168 if (!defined $ftype) {
5169 die_error(500, "Unknown type of object");
5172 my $paging_nav = '';
5173 if ($page > 0) {
5174 $paging_nav .=
5175 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5176 file_name=>$file_name)},
5177 "first");
5178 $paging_nav .= " &sdot; " .
5179 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5180 -accesskey => "p", -title => "Alt-p"}, "prev");
5181 } else {
5182 $paging_nav .= "first";
5183 $paging_nav .= " &sdot; prev";
5185 my $next_link = '';
5186 if ($#commitlist >= 100) {
5187 $next_link =
5188 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5189 -accesskey => "n", -title => "Alt-n"}, "next");
5190 $paging_nav .= " &sdot; $next_link";
5191 } else {
5192 $paging_nav .= " &sdot; next";
5195 git_header_html();
5196 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5197 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5198 git_print_page_path($file_name, $ftype, $hash_base);
5200 git_history_body(\@commitlist, 0, 99,
5201 $refs, $hash_base, $ftype, $next_link);
5203 git_footer_html();
5206 sub git_search {
5207 gitweb_check_feature('search') or die_error(403, "Search is disabled");
5208 if (!defined $searchtext) {
5209 die_error(400, "Text field is empty");
5211 if (!defined $hash) {
5212 $hash = git_get_head_hash($project);
5214 my %co = parse_commit($hash);
5215 if (!%co) {
5216 die_error(404, "Unknown commit object");
5218 if (!defined $page) {
5219 $page = 0;
5222 $searchtype ||= 'commit';
5223 if ($searchtype eq 'pickaxe') {
5224 # pickaxe may take all resources of your box and run for several minutes
5225 # with every query - so decide by yourself how public you make this feature
5226 gitweb_check_feature('pickaxe')
5227 or die_error(403, "Pickaxe is disabled");
5229 if ($searchtype eq 'grep') {
5230 gitweb_check_feature('grep')
5231 or die_error(403, "Grep is disabled");
5234 git_header_html();
5236 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5237 my $greptype;
5238 if ($searchtype eq 'commit') {
5239 $greptype = "--grep=";
5240 } elsif ($searchtype eq 'author') {
5241 $greptype = "--author=";
5242 } elsif ($searchtype eq 'committer') {
5243 $greptype = "--committer=";
5245 $greptype .= $searchtext;
5246 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5247 $greptype, '--regexp-ignore-case',
5248 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5250 my $paging_nav = '';
5251 if ($page > 0) {
5252 $paging_nav .=
5253 $cgi->a({-href => href(action=>"search", hash=>$hash,
5254 searchtext=>$searchtext,
5255 searchtype=>$searchtype)},
5256 "first");
5257 $paging_nav .= " &sdot; " .
5258 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5259 -accesskey => "p", -title => "Alt-p"}, "prev");
5260 } else {
5261 $paging_nav .= "first";
5262 $paging_nav .= " &sdot; prev";
5264 my $next_link = '';
5265 if ($#commitlist >= 100) {
5266 $next_link =
5267 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5268 -accesskey => "n", -title => "Alt-n"}, "next");
5269 $paging_nav .= " &sdot; $next_link";
5270 } else {
5271 $paging_nav .= " &sdot; next";
5274 if ($#commitlist >= 100) {
5277 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5278 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5279 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5282 if ($searchtype eq 'pickaxe') {
5283 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5284 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5286 print "<table class=\"pickaxe search\">\n";
5287 my $alternate = 1;
5288 $/ = "\n";
5289 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5290 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5291 ($search_use_regexp ? '--pickaxe-regex' : ());
5292 undef %co;
5293 my @files;
5294 while (my $line = <$fd>) {
5295 chomp $line;
5296 next unless $line;
5298 my %set = parse_difftree_raw_line($line);
5299 if (defined $set{'commit'}) {
5300 # finish previous commit
5301 if (%co) {
5302 print "</td>\n" .
5303 "<td class=\"link\">" .
5304 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5305 " | " .
5306 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5307 print "</td>\n" .
5308 "</tr>\n";
5311 if ($alternate) {
5312 print "<tr class=\"dark\">\n";
5313 } else {
5314 print "<tr class=\"light\">\n";
5316 $alternate ^= 1;
5317 %co = parse_commit($set{'commit'});
5318 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5319 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5320 "<td><i>$author</i></td>\n" .
5321 "<td>" .
5322 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5323 -class => "list subject"},
5324 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5325 } elsif (defined $set{'to_id'}) {
5326 next if ($set{'to_id'} =~ m/^0{40}$/);
5328 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5329 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5330 -class => "list"},
5331 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5332 "<br/>\n";
5335 close $fd;
5337 # finish last commit (warning: repetition!)
5338 if (%co) {
5339 print "</td>\n" .
5340 "<td class=\"link\">" .
5341 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5342 " | " .
5343 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5344 print "</td>\n" .
5345 "</tr>\n";
5348 print "</table>\n";
5351 if ($searchtype eq 'grep') {
5352 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5353 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5355 print "<table class=\"grep_search\">\n";
5356 my $alternate = 1;
5357 my $matches = 0;
5358 $/ = "\n";
5359 open my $fd, "-|", git_cmd(), 'grep', '-n',
5360 $search_use_regexp ? ('-E', '-i') : '-F',
5361 $searchtext, $co{'tree'};
5362 my $lastfile = '';
5363 while (my $line = <$fd>) {
5364 chomp $line;
5365 my ($file, $lno, $ltext, $binary);
5366 last if ($matches++ > 1000);
5367 if ($line =~ /^Binary file (.+) matches$/) {
5368 $file = $1;
5369 $binary = 1;
5370 } else {
5371 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5373 if ($file ne $lastfile) {
5374 $lastfile and print "</td></tr>\n";
5375 if ($alternate++) {
5376 print "<tr class=\"dark\">\n";
5377 } else {
5378 print "<tr class=\"light\">\n";
5380 print "<td class=\"list\">".
5381 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5382 file_name=>"$file"),
5383 -class => "list"}, esc_path($file));
5384 print "</td><td>\n";
5385 $lastfile = $file;
5387 if ($binary) {
5388 print "<div class=\"binary\">Binary file</div>\n";
5389 } else {
5390 $ltext = untabify($ltext);
5391 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5392 $ltext = esc_html($1, -nbsp=>1);
5393 $ltext .= '<span class="match">';
5394 $ltext .= esc_html($2, -nbsp=>1);
5395 $ltext .= '</span>';
5396 $ltext .= esc_html($3, -nbsp=>1);
5397 } else {
5398 $ltext = esc_html($ltext, -nbsp=>1);
5400 print "<div class=\"pre\">" .
5401 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5402 file_name=>"$file").'#l'.$lno,
5403 -class => "linenr"}, sprintf('%4i', $lno))
5404 . ' ' . $ltext . "</div>\n";
5407 if ($lastfile) {
5408 print "</td></tr>\n";
5409 if ($matches > 1000) {
5410 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5412 } else {
5413 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5415 close $fd;
5417 print "</table>\n";
5419 git_footer_html();
5422 sub git_search_help {
5423 git_header_html();
5424 git_print_page_nav('','', $hash,$hash,$hash);
5425 print <<EOT;
5426 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5427 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5428 the pattern entered is recognized as the POSIX extended
5429 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5430 insensitive).</p>
5431 <dl>
5432 <dt><b>commit</b></dt>
5433 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5435 my ($have_grep) = gitweb_check_feature('grep');
5436 if ($have_grep) {
5437 print <<EOT;
5438 <dt><b>grep</b></dt>
5439 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5440 a different one) are searched for the given pattern. On large trees, this search can take
5441 a while and put some strain on the server, so please use it with some consideration. Note that
5442 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5443 case-sensitive.</dd>
5446 print <<EOT;
5447 <dt><b>author</b></dt>
5448 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5449 <dt><b>committer</b></dt>
5450 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5452 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5453 if ($have_pickaxe) {
5454 print <<EOT;
5455 <dt><b>pickaxe</b></dt>
5456 <dd>All commits that caused the string to appear or disappear from any file (changes that
5457 added, removed or "modified" the string) will be listed. This search can take a while and
5458 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5459 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5462 print "</dl>\n";
5463 git_footer_html();
5466 sub git_shortlog {
5467 my $head = git_get_head_hash($project);
5468 if (!defined $hash) {
5469 $hash = $head;
5471 if (!defined $page) {
5472 $page = 0;
5474 my $refs = git_get_references();
5476 my @commitlist = parse_commits($hash, 101, (100 * $page));
5478 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5479 my $next_link = '';
5480 if ($#commitlist >= 100) {
5481 $next_link =
5482 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5483 -accesskey => "n", -title => "Alt-n"}, "next");
5486 git_header_html();
5487 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5488 git_print_header_div('summary', $project);
5490 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5492 git_footer_html();
5495 ## ......................................................................
5496 ## feeds (RSS, Atom; OPML)
5498 sub git_feed {
5499 my $format = shift || 'atom';
5500 my ($have_blame) = gitweb_check_feature('blame');
5502 # Atom: http://www.atomenabled.org/developers/syndication/
5503 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5504 if ($format ne 'rss' && $format ne 'atom') {
5505 die_error(400, "Unknown web feed format");
5508 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5509 my $head = $hash || 'HEAD';
5510 my @commitlist = parse_commits($head, 150, 0, $file_name);
5512 my %latest_commit;
5513 my %latest_date;
5514 my $content_type = "application/$format+xml";
5515 if (defined $cgi->http('HTTP_ACCEPT') &&
5516 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5517 # browser (feed reader) prefers text/xml
5518 $content_type = 'text/xml';
5520 if (defined($commitlist[0])) {
5521 %latest_commit = %{$commitlist[0]};
5522 %latest_date = parse_date($latest_commit{'author_epoch'});
5523 print $cgi->header(
5524 -type => $content_type,
5525 -charset => 'utf-8',
5526 -last_modified => $latest_date{'rfc2822'});
5527 } else {
5528 print $cgi->header(
5529 -type => $content_type,
5530 -charset => 'utf-8');
5533 # Optimization: skip generating the body if client asks only
5534 # for Last-Modified date.
5535 return if ($cgi->request_method() eq 'HEAD');
5537 # header variables
5538 my $title = "$site_name - $project/$action";
5539 my $feed_type = 'log';
5540 if (defined $hash) {
5541 $title .= " - '$hash'";
5542 $feed_type = 'branch log';
5543 if (defined $file_name) {
5544 $title .= " :: $file_name";
5545 $feed_type = 'history';
5547 } elsif (defined $file_name) {
5548 $title .= " - $file_name";
5549 $feed_type = 'history';
5551 $title .= " $feed_type";
5552 my $descr = git_get_project_description($project);
5553 if (defined $descr) {
5554 $descr = esc_html($descr);
5555 } else {
5556 $descr = "$project " .
5557 ($format eq 'rss' ? 'RSS' : 'Atom') .
5558 " feed";
5560 my $owner = git_get_project_owner($project);
5561 $owner = esc_html($owner);
5563 #header
5564 my $alt_url;
5565 if (defined $file_name) {
5566 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5567 } elsif (defined $hash) {
5568 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5569 } else {
5570 $alt_url = href(-full=>1, action=>"summary");
5572 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5573 if ($format eq 'rss') {
5574 print <<XML;
5575 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5576 <channel>
5578 print "<title>$title</title>\n" .
5579 "<link>$alt_url</link>\n" .
5580 "<description>$descr</description>\n" .
5581 "<language>en</language>\n";
5582 } elsif ($format eq 'atom') {
5583 print <<XML;
5584 <feed xmlns="http://www.w3.org/2005/Atom">
5586 print "<title>$title</title>\n" .
5587 "<subtitle>$descr</subtitle>\n" .
5588 '<link rel="alternate" type="text/html" href="' .
5589 $alt_url . '" />' . "\n" .
5590 '<link rel="self" type="' . $content_type . '" href="' .
5591 $cgi->self_url() . '" />' . "\n" .
5592 "<id>" . href(-full=>1) . "</id>\n" .
5593 # use project owner for feed author
5594 "<author><name>$owner</name></author>\n";
5595 if (defined $favicon) {
5596 print "<icon>" . esc_url($favicon) . "</icon>\n";
5598 if (defined $logo_url) {
5599 # not twice as wide as tall: 72 x 27 pixels
5600 print "<logo>" . esc_url($logo) . "</logo>\n";
5602 if (! %latest_date) {
5603 # dummy date to keep the feed valid until commits trickle in:
5604 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5605 } else {
5606 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5610 # contents
5611 for (my $i = 0; $i <= $#commitlist; $i++) {
5612 my %co = %{$commitlist[$i]};
5613 my $commit = $co{'id'};
5614 # we read 150, we always show 30 and the ones more recent than 48 hours
5615 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5616 last;
5618 my %cd = parse_date($co{'author_epoch'});
5620 # get list of changed files
5621 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5622 $co{'parent'} || "--root",
5623 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5624 or next;
5625 my @difftree = map { chomp; $_ } <$fd>;
5626 close $fd
5627 or next;
5629 # print element (entry, item)
5630 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5631 if ($format eq 'rss') {
5632 print "<item>\n" .
5633 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5634 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5635 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5636 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5637 "<link>$co_url</link>\n" .
5638 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5639 "<content:encoded>" .
5640 "<![CDATA[\n";
5641 } elsif ($format eq 'atom') {
5642 print "<entry>\n" .
5643 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5644 "<updated>$cd{'iso-8601'}</updated>\n" .
5645 "<author>\n" .
5646 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5647 if ($co{'author_email'}) {
5648 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5650 print "</author>\n" .
5651 # use committer for contributor
5652 "<contributor>\n" .
5653 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5654 if ($co{'committer_email'}) {
5655 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5657 print "</contributor>\n" .
5658 "<published>$cd{'iso-8601'}</published>\n" .
5659 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5660 "<id>$co_url</id>\n" .
5661 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5662 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5664 my $comment = $co{'comment'};
5665 print "<pre>\n";
5666 foreach my $line (@$comment) {
5667 $line = esc_html($line);
5668 print "$line\n";
5670 print "</pre><ul>\n";
5671 foreach my $difftree_line (@difftree) {
5672 my %difftree = parse_difftree_raw_line($difftree_line);
5673 next if !$difftree{'from_id'};
5675 my $file = $difftree{'file'} || $difftree{'to_file'};
5677 print "<li>" .
5678 "[" .
5679 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5680 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5681 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5682 file_name=>$file, file_parent=>$difftree{'from_file'}),
5683 -title => "diff"}, 'D');
5684 if ($have_blame) {
5685 print $cgi->a({-href => href(-full=>1, action=>"blame",
5686 file_name=>$file, hash_base=>$commit),
5687 -title => "blame"}, 'B');
5689 # if this is not a feed of a file history
5690 if (!defined $file_name || $file_name ne $file) {
5691 print $cgi->a({-href => href(-full=>1, action=>"history",
5692 file_name=>$file, hash=>$commit),
5693 -title => "history"}, 'H');
5695 $file = esc_path($file);
5696 print "] ".
5697 "$file</li>\n";
5699 if ($format eq 'rss') {
5700 print "</ul>]]>\n" .
5701 "</content:encoded>\n" .
5702 "</item>\n";
5703 } elsif ($format eq 'atom') {
5704 print "</ul>\n</div>\n" .
5705 "</content>\n" .
5706 "</entry>\n";
5710 # end of feed
5711 if ($format eq 'rss') {
5712 print "</channel>\n</rss>\n";
5713 } elsif ($format eq 'atom') {
5714 print "</feed>\n";
5718 sub git_rss {
5719 git_feed('rss');
5722 sub git_atom {
5723 git_feed('atom');
5726 sub git_opml {
5727 my @list = git_get_projects_list();
5729 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5730 print <<XML;
5731 <?xml version="1.0" encoding="utf-8"?>
5732 <opml version="1.0">
5733 <head>
5734 <title>$site_name OPML Export</title>
5735 </head>
5736 <body>
5737 <outline text="git RSS feeds">
5740 foreach my $pr (@list) {
5741 my %proj = %$pr;
5742 my $head = git_get_head_hash($proj{'path'});
5743 if (!defined $head) {
5744 next;
5746 $git_dir = "$projectroot/$proj{'path'}";
5747 my %co = parse_commit($head);
5748 if (!%co) {
5749 next;
5752 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5753 my $rss = "$my_url?p=$proj{'path'};a=rss";
5754 my $html = "$my_url?p=$proj{'path'};a=summary";
5755 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5757 print <<XML;
5758 </outline>
5759 </body>
5760 </opml>