Merge branch 'maint-1.5.5' into maint
[git/dscho.git] / gitweb / gitweb.perl
blob49b01d8c25b7a7d9c850b5484414bfcfa9bbf85b
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++";
69 # URI and label (title) of GIT logo link
70 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
71 #our $logo_label = "git documentation";
72 our $logo_url = "http://git.or.cz/";
73 our $logo_label = "git homepage";
75 # source of projects list
76 our $projects_list = "++GITWEB_LIST++";
78 # the width (in characters) of the projects list "Description" column
79 our $projects_list_description_width = 25;
81 # default order of projects list
82 # valid values are none, project, descr, owner, and age
83 our $default_projects_order = "project";
85 # show repository only if this file exists
86 # (only effective if this variable evaluates to true)
87 our $export_ok = "++GITWEB_EXPORT_OK++";
89 # only allow viewing of repositories also shown on the overview page
90 our $strict_export = "++GITWEB_STRICT_EXPORT++";
92 # list of git base URLs used for URL to where fetch project from,
93 # i.e. full URL is "$git_base_url/$project"
94 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
96 # default blob_plain mimetype and default charset for text/plain blob
97 our $default_blob_plain_mimetype = 'text/plain';
98 our $default_text_plain_charset = undef;
100 # file to use for guessing MIME types before trying /etc/mime.types
101 # (relative to the current git repository)
102 our $mimetypes_file = undef;
104 # assume this charset if line contains non-UTF-8 characters;
105 # it should be valid encoding (see Encoding::Supported(3pm) for list),
106 # for which encoding all byte sequences are valid, for example
107 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
108 # could be even 'utf-8' for the old behavior)
109 our $fallback_encoding = 'latin1';
111 # rename detection options for git-diff and git-diff-tree
112 # - default is '-M', with the cost proportional to
113 # (number of removed files) * (number of new files).
114 # - more costly is '-C' (which implies '-M'), with the cost proportional to
115 # (number of changed files + number of removed files) * (number of new files)
116 # - even more costly is '-C', '--find-copies-harder' with cost
117 # (number of files in the original tree) * (number of new files)
118 # - one might want to include '-B' option, e.g. '-B', '-M'
119 our @diff_opts = ('-M'); # taken from git_commit
121 # information about snapshot formats that gitweb is capable of serving
122 our %known_snapshot_formats = (
123 # name => {
124 # 'display' => display name,
125 # 'type' => mime type,
126 # 'suffix' => filename suffix,
127 # 'format' => --format for git-archive,
128 # 'compressor' => [compressor command and arguments]
129 # (array reference, optional)}
131 'tgz' => {
132 'display' => 'tar.gz',
133 'type' => 'application/x-gzip',
134 'suffix' => '.tar.gz',
135 'format' => 'tar',
136 'compressor' => ['gzip']},
138 'tbz2' => {
139 'display' => 'tar.bz2',
140 'type' => 'application/x-bzip2',
141 'suffix' => '.tar.bz2',
142 'format' => 'tar',
143 'compressor' => ['bzip2']},
145 'zip' => {
146 'display' => 'zip',
147 'type' => 'application/x-zip',
148 'suffix' => '.zip',
149 'format' => 'zip'},
152 # Aliases so we understand old gitweb.snapshot values in repository
153 # configuration.
154 our %known_snapshot_format_aliases = (
155 'gzip' => 'tgz',
156 'bzip2' => 'tbz2',
158 # backward compatibility: legacy gitweb config support
159 'x-gzip' => undef, 'gz' => undef,
160 'x-bzip2' => undef, 'bz2' => undef,
161 'x-zip' => undef, '' => undef,
164 # You define site-wide feature defaults here; override them with
165 # $GITWEB_CONFIG as necessary.
166 our %feature = (
167 # feature => {
168 # 'sub' => feature-sub (subroutine),
169 # 'override' => allow-override (boolean),
170 # 'default' => [ default options...] (array reference)}
172 # if feature is overridable (it means that allow-override has true value),
173 # then feature-sub will be called with default options as parameters;
174 # return value of feature-sub indicates if to enable specified feature
176 # if there is no 'sub' key (no feature-sub), then feature cannot be
177 # overriden
179 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
181 # Enable the 'blame' blob view, showing the last commit that modified
182 # each line in the file. This can be very CPU-intensive.
184 # To enable system wide have in $GITWEB_CONFIG
185 # $feature{'blame'}{'default'} = [1];
186 # To have project specific config enable override in $GITWEB_CONFIG
187 # $feature{'blame'}{'override'} = 1;
188 # and in project config gitweb.blame = 0|1;
189 'blame' => {
190 'sub' => \&feature_blame,
191 'override' => 0,
192 'default' => [0]},
194 # Enable the 'snapshot' link, providing a compressed archive of any
195 # tree. This can potentially generate high traffic if you have large
196 # project.
198 # Value is a list of formats defined in %known_snapshot_formats that
199 # you wish to offer.
200 # To disable system wide have in $GITWEB_CONFIG
201 # $feature{'snapshot'}{'default'} = [];
202 # To have project specific config enable override in $GITWEB_CONFIG
203 # $feature{'snapshot'}{'override'} = 1;
204 # and in project config, a comma-separated list of formats or "none"
205 # to disable. Example: gitweb.snapshot = tbz2,zip;
206 'snapshot' => {
207 'sub' => \&feature_snapshot,
208 'override' => 0,
209 'default' => ['tgz']},
211 # Enable text search, which will list the commits which match author,
212 # committer or commit text to a given string. Enabled by default.
213 # Project specific override is not supported.
214 'search' => {
215 'override' => 0,
216 'default' => [1]},
218 # Enable grep search, which will list the files in currently selected
219 # tree containing the given string. Enabled by default. This can be
220 # potentially CPU-intensive, of course.
222 # To enable system wide have in $GITWEB_CONFIG
223 # $feature{'grep'}{'default'} = [1];
224 # To have project specific config enable override in $GITWEB_CONFIG
225 # $feature{'grep'}{'override'} = 1;
226 # and in project config gitweb.grep = 0|1;
227 'grep' => {
228 'override' => 0,
229 'default' => [1]},
231 # Enable the pickaxe search, which will list the commits that modified
232 # a given string in a file. This can be practical and quite faster
233 # alternative to 'blame', but still potentially CPU-intensive.
235 # To enable system wide have in $GITWEB_CONFIG
236 # $feature{'pickaxe'}{'default'} = [1];
237 # To have project specific config enable override in $GITWEB_CONFIG
238 # $feature{'pickaxe'}{'override'} = 1;
239 # and in project config gitweb.pickaxe = 0|1;
240 'pickaxe' => {
241 'sub' => \&feature_pickaxe,
242 'override' => 0,
243 'default' => [1]},
245 # Make gitweb use an alternative format of the URLs which can be
246 # more readable and natural-looking: project name is embedded
247 # directly in the path and the query string contains other
248 # auxiliary information. All gitweb installations recognize
249 # URL in either format; this configures in which formats gitweb
250 # generates links.
252 # To enable system wide have in $GITWEB_CONFIG
253 # $feature{'pathinfo'}{'default'} = [1];
254 # Project specific override is not supported.
256 # Note that you will need to change the default location of CSS,
257 # favicon, logo and possibly other files to an absolute URL. Also,
258 # if gitweb.cgi serves as your indexfile, you will need to force
259 # $my_uri to contain the script name in your $GITWEB_CONFIG.
260 'pathinfo' => {
261 'override' => 0,
262 'default' => [0]},
264 # Make gitweb consider projects in project root subdirectories
265 # to be forks of existing projects. Given project $projname.git,
266 # projects matching $projname/*.git will not be shown in the main
267 # projects list, instead a '+' mark will be added to $projname
268 # there and a 'forks' view will be enabled for the project, listing
269 # all the forks. If project list is taken from a file, forks have
270 # to be listed after the main project.
272 # To enable system wide have in $GITWEB_CONFIG
273 # $feature{'forks'}{'default'} = [1];
274 # Project specific override is not supported.
275 'forks' => {
276 'override' => 0,
277 'default' => [0]},
280 sub gitweb_check_feature {
281 my ($name) = @_;
282 return unless exists $feature{$name};
283 my ($sub, $override, @defaults) = (
284 $feature{$name}{'sub'},
285 $feature{$name}{'override'},
286 @{$feature{$name}{'default'}});
287 if (!$override) { return @defaults; }
288 if (!defined $sub) {
289 warn "feature $name is not overrideable";
290 return @defaults;
292 return $sub->(@defaults);
295 sub feature_blame {
296 my ($val) = git_get_project_config('blame', '--bool');
298 if ($val eq 'true') {
299 return 1;
300 } elsif ($val eq 'false') {
301 return 0;
304 return $_[0];
307 sub feature_snapshot {
308 my (@fmts) = @_;
310 my ($val) = git_get_project_config('snapshot');
312 if ($val) {
313 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
316 return @fmts;
319 sub feature_grep {
320 my ($val) = git_get_project_config('grep', '--bool');
322 if ($val eq 'true') {
323 return (1);
324 } elsif ($val eq 'false') {
325 return (0);
328 return ($_[0]);
331 sub feature_pickaxe {
332 my ($val) = git_get_project_config('pickaxe', '--bool');
334 if ($val eq 'true') {
335 return (1);
336 } elsif ($val eq 'false') {
337 return (0);
340 return ($_[0]);
343 # checking HEAD file with -e is fragile if the repository was
344 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
345 # and then pruned.
346 sub check_head_link {
347 my ($dir) = @_;
348 my $headfile = "$dir/HEAD";
349 return ((-e $headfile) ||
350 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
353 sub check_export_ok {
354 my ($dir) = @_;
355 return (check_head_link($dir) &&
356 (!$export_ok || -e "$dir/$export_ok"));
359 # process alternate names for backward compatibility
360 # filter out unsupported (unknown) snapshot formats
361 sub filter_snapshot_fmts {
362 my @fmts = @_;
364 @fmts = map {
365 exists $known_snapshot_format_aliases{$_} ?
366 $known_snapshot_format_aliases{$_} : $_} @fmts;
367 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
371 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
372 if (-e $GITWEB_CONFIG) {
373 do $GITWEB_CONFIG;
374 } else {
375 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
376 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
379 # version of the core git binary
380 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
382 $projects_list ||= $projectroot;
384 # ======================================================================
385 # input validation and dispatch
386 our $action = $cgi->param('a');
387 if (defined $action) {
388 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
389 die_error(undef, "Invalid action parameter");
393 # parameters which are pathnames
394 our $project = $cgi->param('p');
395 if (defined $project) {
396 if (!validate_pathname($project) ||
397 !(-d "$projectroot/$project") ||
398 !check_head_link("$projectroot/$project") ||
399 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
400 ($strict_export && !project_in_list($project))) {
401 undef $project;
402 die_error(undef, "No such project");
406 our $file_name = $cgi->param('f');
407 if (defined $file_name) {
408 if (!validate_pathname($file_name)) {
409 die_error(undef, "Invalid file parameter");
413 our $file_parent = $cgi->param('fp');
414 if (defined $file_parent) {
415 if (!validate_pathname($file_parent)) {
416 die_error(undef, "Invalid file parent parameter");
420 # parameters which are refnames
421 our $hash = $cgi->param('h');
422 if (defined $hash) {
423 if (!validate_refname($hash)) {
424 die_error(undef, "Invalid hash parameter");
428 our $hash_parent = $cgi->param('hp');
429 if (defined $hash_parent) {
430 if (!validate_refname($hash_parent)) {
431 die_error(undef, "Invalid hash parent parameter");
435 our $hash_base = $cgi->param('hb');
436 if (defined $hash_base) {
437 if (!validate_refname($hash_base)) {
438 die_error(undef, "Invalid hash base parameter");
442 my %allowed_options = (
443 "--no-merges" => [ qw(rss atom log shortlog history) ],
446 our @extra_options = $cgi->param('opt');
447 if (defined @extra_options) {
448 foreach my $opt (@extra_options) {
449 if (not exists $allowed_options{$opt}) {
450 die_error(undef, "Invalid option parameter");
452 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
453 die_error(undef, "Invalid option parameter for this action");
458 our $hash_parent_base = $cgi->param('hpb');
459 if (defined $hash_parent_base) {
460 if (!validate_refname($hash_parent_base)) {
461 die_error(undef, "Invalid hash parent base parameter");
465 # other parameters
466 our $page = $cgi->param('pg');
467 if (defined $page) {
468 if ($page =~ m/[^0-9]/) {
469 die_error(undef, "Invalid page parameter");
473 our $searchtype = $cgi->param('st');
474 if (defined $searchtype) {
475 if ($searchtype =~ m/[^a-z]/) {
476 die_error(undef, "Invalid searchtype parameter");
480 our $search_use_regexp = $cgi->param('sr');
482 our $searchtext = $cgi->param('s');
483 our $search_regexp;
484 if (defined $searchtext) {
485 if (length($searchtext) < 2) {
486 die_error(undef, "At least two characters are required for search parameter");
488 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
491 # now read PATH_INFO and use it as alternative to parameters
492 sub evaluate_path_info {
493 return if defined $project;
494 my $path_info = $ENV{"PATH_INFO"};
495 return if !$path_info;
496 $path_info =~ s,^/+,,;
497 return if !$path_info;
498 # find which part of PATH_INFO is project
499 $project = $path_info;
500 $project =~ s,/+$,,;
501 while ($project && !check_head_link("$projectroot/$project")) {
502 $project =~ s,/*[^/]*$,,;
504 # validate project
505 $project = validate_pathname($project);
506 if (!$project ||
507 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
508 ($strict_export && !project_in_list($project))) {
509 undef $project;
510 return;
512 # do not change any parameters if an action is given using the query string
513 return if $action;
514 $path_info =~ s,^\Q$project\E/*,,;
515 my ($refname, $pathname) = split(/:/, $path_info, 2);
516 if (defined $pathname) {
517 # we got "project.git/branch:filename" or "project.git/branch:dir/"
518 # we could use git_get_type(branch:pathname), but it needs $git_dir
519 $pathname =~ s,^/+,,;
520 if (!$pathname || substr($pathname, -1) eq "/") {
521 $action ||= "tree";
522 $pathname =~ s,/$,,;
523 } else {
524 $action ||= "blob_plain";
526 $hash_base ||= validate_refname($refname);
527 $file_name ||= validate_pathname($pathname);
528 } elsif (defined $refname) {
529 # we got "project.git/branch"
530 $action ||= "shortlog";
531 $hash ||= validate_refname($refname);
534 evaluate_path_info();
536 # path to the current git repository
537 our $git_dir;
538 $git_dir = "$projectroot/$project" if $project;
540 # dispatch
541 my %actions = (
542 "blame" => \&git_blame2,
543 "blobdiff" => \&git_blobdiff,
544 "blobdiff_plain" => \&git_blobdiff_plain,
545 "blob" => \&git_blob,
546 "blob_plain" => \&git_blob_plain,
547 "commitdiff" => \&git_commitdiff,
548 "commitdiff_plain" => \&git_commitdiff_plain,
549 "commit" => \&git_commit,
550 "forks" => \&git_forks,
551 "heads" => \&git_heads,
552 "history" => \&git_history,
553 "log" => \&git_log,
554 "rss" => \&git_rss,
555 "atom" => \&git_atom,
556 "search" => \&git_search,
557 "search_help" => \&git_search_help,
558 "shortlog" => \&git_shortlog,
559 "summary" => \&git_summary,
560 "tag" => \&git_tag,
561 "tags" => \&git_tags,
562 "tree" => \&git_tree,
563 "snapshot" => \&git_snapshot,
564 "object" => \&git_object,
565 # those below don't need $project
566 "opml" => \&git_opml,
567 "project_list" => \&git_project_list,
568 "project_index" => \&git_project_index,
571 if (!defined $action) {
572 if (defined $hash) {
573 $action = git_get_type($hash);
574 } elsif (defined $hash_base && defined $file_name) {
575 $action = git_get_type("$hash_base:$file_name");
576 } elsif (defined $project) {
577 $action = 'summary';
578 } else {
579 $action = 'project_list';
582 if (!defined($actions{$action})) {
583 die_error(undef, "Unknown action");
585 if ($action !~ m/^(opml|project_list|project_index)$/ &&
586 !$project) {
587 die_error(undef, "Project needed");
589 $actions{$action}->();
590 exit;
592 ## ======================================================================
593 ## action links
595 sub href (%) {
596 my %params = @_;
597 # default is to use -absolute url() i.e. $my_uri
598 my $href = $params{-full} ? $my_url : $my_uri;
600 # XXX: Warning: If you touch this, check the search form for updating,
601 # too.
603 my @mapping = (
604 project => "p",
605 action => "a",
606 file_name => "f",
607 file_parent => "fp",
608 hash => "h",
609 hash_parent => "hp",
610 hash_base => "hb",
611 hash_parent_base => "hpb",
612 page => "pg",
613 order => "o",
614 searchtext => "s",
615 searchtype => "st",
616 snapshot_format => "sf",
617 extra_options => "opt",
618 search_use_regexp => "sr",
620 my %mapping = @mapping;
622 $params{'project'} = $project unless exists $params{'project'};
624 if ($params{-replay}) {
625 while (my ($name, $symbol) = each %mapping) {
626 if (!exists $params{$name}) {
627 # to allow for multivalued params we use arrayref form
628 $params{$name} = [ $cgi->param($symbol) ];
633 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
634 if ($use_pathinfo) {
635 # use PATH_INFO for project name
636 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
637 delete $params{'project'};
639 # Summary just uses the project path URL
640 if (defined $params{'action'} && $params{'action'} eq 'summary') {
641 delete $params{'action'};
645 # now encode the parameters explicitly
646 my @result = ();
647 for (my $i = 0; $i < @mapping; $i += 2) {
648 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
649 if (defined $params{$name}) {
650 if (ref($params{$name}) eq "ARRAY") {
651 foreach my $par (@{$params{$name}}) {
652 push @result, $symbol . "=" . esc_param($par);
654 } else {
655 push @result, $symbol . "=" . esc_param($params{$name});
659 $href .= "?" . join(';', @result) if scalar @result;
661 return $href;
665 ## ======================================================================
666 ## validation, quoting/unquoting and escaping
668 sub validate_pathname {
669 my $input = shift || return undef;
671 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
672 # at the beginning, at the end, and between slashes.
673 # also this catches doubled slashes
674 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
675 return undef;
677 # no null characters
678 if ($input =~ m!\0!) {
679 return undef;
681 return $input;
684 sub validate_refname {
685 my $input = shift || return undef;
687 # textual hashes are O.K.
688 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
689 return $input;
691 # it must be correct pathname
692 $input = validate_pathname($input)
693 or return undef;
694 # restrictions on ref name according to git-check-ref-format
695 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
696 return undef;
698 return $input;
701 # decode sequences of octets in utf8 into Perl's internal form,
702 # which is utf-8 with utf8 flag set if needed. gitweb writes out
703 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
704 sub to_utf8 {
705 my $str = shift;
706 if (utf8::valid($str)) {
707 utf8::decode($str);
708 return $str;
709 } else {
710 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
714 # quote unsafe chars, but keep the slash, even when it's not
715 # correct, but quoted slashes look too horrible in bookmarks
716 sub esc_param {
717 my $str = shift;
718 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
719 $str =~ s/\+/%2B/g;
720 $str =~ s/ /\+/g;
721 return $str;
724 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
725 sub esc_url {
726 my $str = shift;
727 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
728 $str =~ s/\+/%2B/g;
729 $str =~ s/ /\+/g;
730 return $str;
733 # replace invalid utf8 character with SUBSTITUTION sequence
734 sub esc_html ($;%) {
735 my $str = shift;
736 my %opts = @_;
738 $str = to_utf8($str);
739 $str = $cgi->escapeHTML($str);
740 if ($opts{'-nbsp'}) {
741 $str =~ s/ /&nbsp;/g;
743 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
744 return $str;
747 # quote control characters and escape filename to HTML
748 sub esc_path {
749 my $str = shift;
750 my %opts = @_;
752 $str = to_utf8($str);
753 $str = $cgi->escapeHTML($str);
754 if ($opts{'-nbsp'}) {
755 $str =~ s/ /&nbsp;/g;
757 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
758 return $str;
761 # Make control characters "printable", using character escape codes (CEC)
762 sub quot_cec {
763 my $cntrl = shift;
764 my %opts = @_;
765 my %es = ( # character escape codes, aka escape sequences
766 "\t" => '\t', # tab (HT)
767 "\n" => '\n', # line feed (LF)
768 "\r" => '\r', # carrige return (CR)
769 "\f" => '\f', # form feed (FF)
770 "\b" => '\b', # backspace (BS)
771 "\a" => '\a', # alarm (bell) (BEL)
772 "\e" => '\e', # escape (ESC)
773 "\013" => '\v', # vertical tab (VT)
774 "\000" => '\0', # nul character (NUL)
776 my $chr = ( (exists $es{$cntrl})
777 ? $es{$cntrl}
778 : sprintf('\%03o', ord($cntrl)) );
779 if ($opts{-nohtml}) {
780 return $chr;
781 } else {
782 return "<span class=\"cntrl\">$chr</span>";
786 # Alternatively use unicode control pictures codepoints,
787 # Unicode "printable representation" (PR)
788 sub quot_upr {
789 my $cntrl = shift;
790 my %opts = @_;
792 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
793 if ($opts{-nohtml}) {
794 return $chr;
795 } else {
796 return "<span class=\"cntrl\">$chr</span>";
800 # git may return quoted and escaped filenames
801 sub unquote {
802 my $str = shift;
804 sub unq {
805 my $seq = shift;
806 my %es = ( # character escape codes, aka escape sequences
807 't' => "\t", # tab (HT, TAB)
808 'n' => "\n", # newline (NL)
809 'r' => "\r", # return (CR)
810 'f' => "\f", # form feed (FF)
811 'b' => "\b", # backspace (BS)
812 'a' => "\a", # alarm (bell) (BEL)
813 'e' => "\e", # escape (ESC)
814 'v' => "\013", # vertical tab (VT)
817 if ($seq =~ m/^[0-7]{1,3}$/) {
818 # octal char sequence
819 return chr(oct($seq));
820 } elsif (exists $es{$seq}) {
821 # C escape sequence, aka character escape code
822 return $es{$seq};
824 # quoted ordinary character
825 return $seq;
828 if ($str =~ m/^"(.*)"$/) {
829 # needs unquoting
830 $str = $1;
831 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
833 return $str;
836 # escape tabs (convert tabs to spaces)
837 sub untabify {
838 my $line = shift;
840 while ((my $pos = index($line, "\t")) != -1) {
841 if (my $count = (8 - ($pos % 8))) {
842 my $spaces = ' ' x $count;
843 $line =~ s/\t/$spaces/;
847 return $line;
850 sub project_in_list {
851 my $project = shift;
852 my @list = git_get_projects_list();
853 return @list && scalar(grep { $_->{'path'} eq $project } @list);
856 ## ----------------------------------------------------------------------
857 ## HTML aware string manipulation
859 # Try to chop given string on a word boundary between position
860 # $len and $len+$add_len. If there is no word boundary there,
861 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
862 # (marking chopped part) would be longer than given string.
863 sub chop_str {
864 my $str = shift;
865 my $len = shift;
866 my $add_len = shift || 10;
867 my $where = shift || 'right'; # 'left' | 'center' | 'right'
869 # Make sure perl knows it is utf8 encoded so we don't
870 # cut in the middle of a utf8 multibyte char.
871 $str = to_utf8($str);
873 # allow only $len chars, but don't cut a word if it would fit in $add_len
874 # if it doesn't fit, cut it if it's still longer than the dots we would add
875 # remove chopped character entities entirely
877 # when chopping in the middle, distribute $len into left and right part
878 # return early if chopping wouldn't make string shorter
879 if ($where eq 'center') {
880 return $str if ($len + 5 >= length($str)); # filler is length 5
881 $len = int($len/2);
882 } else {
883 return $str if ($len + 4 >= length($str)); # filler is length 4
886 # regexps: ending and beginning with word part up to $add_len
887 my $endre = qr/.{$len}\w{0,$add_len}/;
888 my $begre = qr/\w{0,$add_len}.{$len}/;
890 if ($where eq 'left') {
891 $str =~ m/^(.*?)($begre)$/;
892 my ($lead, $body) = ($1, $2);
893 if (length($lead) > 4) {
894 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
895 $lead = " ...";
897 return "$lead$body";
899 } elsif ($where eq 'center') {
900 $str =~ m/^($endre)(.*)$/;
901 my ($left, $str) = ($1, $2);
902 $str =~ m/^(.*?)($begre)$/;
903 my ($mid, $right) = ($1, $2);
904 if (length($mid) > 5) {
905 $left =~ s/&[^;]*$//;
906 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
907 $mid = " ... ";
909 return "$left$mid$right";
911 } else {
912 $str =~ m/^($endre)(.*)$/;
913 my $body = $1;
914 my $tail = $2;
915 if (length($tail) > 4) {
916 $body =~ s/&[^;]*$//;
917 $tail = "... ";
919 return "$body$tail";
923 # takes the same arguments as chop_str, but also wraps a <span> around the
924 # result with a title attribute if it does get chopped. Additionally, the
925 # string is HTML-escaped.
926 sub chop_and_escape_str {
927 my ($str) = @_;
929 my $chopped = chop_str(@_);
930 if ($chopped eq $str) {
931 return esc_html($chopped);
932 } else {
933 $str =~ s/([[:cntrl:]])/?/g;
934 return $cgi->span({-title=>$str}, esc_html($chopped));
938 ## ----------------------------------------------------------------------
939 ## functions returning short strings
941 # CSS class for given age value (in seconds)
942 sub age_class {
943 my $age = shift;
945 if (!defined $age) {
946 return "noage";
947 } elsif ($age < 60*60*2) {
948 return "age0";
949 } elsif ($age < 60*60*24*2) {
950 return "age1";
951 } else {
952 return "age2";
956 # convert age in seconds to "nn units ago" string
957 sub age_string {
958 my $age = shift;
959 my $age_str;
961 if ($age > 60*60*24*365*2) {
962 $age_str = (int $age/60/60/24/365);
963 $age_str .= " years ago";
964 } elsif ($age > 60*60*24*(365/12)*2) {
965 $age_str = int $age/60/60/24/(365/12);
966 $age_str .= " months ago";
967 } elsif ($age > 60*60*24*7*2) {
968 $age_str = int $age/60/60/24/7;
969 $age_str .= " weeks ago";
970 } elsif ($age > 60*60*24*2) {
971 $age_str = int $age/60/60/24;
972 $age_str .= " days ago";
973 } elsif ($age > 60*60*2) {
974 $age_str = int $age/60/60;
975 $age_str .= " hours ago";
976 } elsif ($age > 60*2) {
977 $age_str = int $age/60;
978 $age_str .= " min ago";
979 } elsif ($age > 2) {
980 $age_str = int $age;
981 $age_str .= " sec ago";
982 } else {
983 $age_str .= " right now";
985 return $age_str;
988 use constant {
989 S_IFINVALID => 0030000,
990 S_IFGITLINK => 0160000,
993 # submodule/subproject, a commit object reference
994 sub S_ISGITLINK($) {
995 my $mode = shift;
997 return (($mode & S_IFMT) == S_IFGITLINK)
1000 # convert file mode in octal to symbolic file mode string
1001 sub mode_str {
1002 my $mode = oct shift;
1004 if (S_ISGITLINK($mode)) {
1005 return 'm---------';
1006 } elsif (S_ISDIR($mode & S_IFMT)) {
1007 return 'drwxr-xr-x';
1008 } elsif (S_ISLNK($mode)) {
1009 return 'lrwxrwxrwx';
1010 } elsif (S_ISREG($mode)) {
1011 # git cares only about the executable bit
1012 if ($mode & S_IXUSR) {
1013 return '-rwxr-xr-x';
1014 } else {
1015 return '-rw-r--r--';
1017 } else {
1018 return '----------';
1022 # convert file mode in octal to file type string
1023 sub file_type {
1024 my $mode = shift;
1026 if ($mode !~ m/^[0-7]+$/) {
1027 return $mode;
1028 } else {
1029 $mode = oct $mode;
1032 if (S_ISGITLINK($mode)) {
1033 return "submodule";
1034 } elsif (S_ISDIR($mode & S_IFMT)) {
1035 return "directory";
1036 } elsif (S_ISLNK($mode)) {
1037 return "symlink";
1038 } elsif (S_ISREG($mode)) {
1039 return "file";
1040 } else {
1041 return "unknown";
1045 # convert file mode in octal to file type description string
1046 sub file_type_long {
1047 my $mode = shift;
1049 if ($mode !~ m/^[0-7]+$/) {
1050 return $mode;
1051 } else {
1052 $mode = oct $mode;
1055 if (S_ISGITLINK($mode)) {
1056 return "submodule";
1057 } elsif (S_ISDIR($mode & S_IFMT)) {
1058 return "directory";
1059 } elsif (S_ISLNK($mode)) {
1060 return "symlink";
1061 } elsif (S_ISREG($mode)) {
1062 if ($mode & S_IXUSR) {
1063 return "executable";
1064 } else {
1065 return "file";
1067 } else {
1068 return "unknown";
1073 ## ----------------------------------------------------------------------
1074 ## functions returning short HTML fragments, or transforming HTML fragments
1075 ## which don't belong to other sections
1077 # format line of commit message.
1078 sub format_log_line_html {
1079 my $line = shift;
1081 $line = esc_html($line, -nbsp=>1);
1082 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1083 my $hash_text = $1;
1084 my $link =
1085 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1086 -class => "text"}, $hash_text);
1087 $line =~ s/$hash_text/$link/;
1089 return $line;
1092 # format marker of refs pointing to given object
1093 sub format_ref_marker {
1094 my ($refs, $id) = @_;
1095 my $markers = '';
1097 if (defined $refs->{$id}) {
1098 foreach my $ref (@{$refs->{$id}}) {
1099 my ($type, $name) = qw();
1100 # e.g. tags/v2.6.11 or heads/next
1101 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1102 $type = $1;
1103 $name = $2;
1104 } else {
1105 $type = "ref";
1106 $name = $ref;
1109 $markers .= " <span class=\"$type\" title=\"$ref\">" .
1110 esc_html($name) . "</span>";
1114 if ($markers) {
1115 return ' <span class="refs">'. $markers . '</span>';
1116 } else {
1117 return "";
1121 # format, perhaps shortened and with markers, title line
1122 sub format_subject_html {
1123 my ($long, $short, $href, $extra) = @_;
1124 $extra = '' unless defined($extra);
1126 if (length($short) < length($long)) {
1127 return $cgi->a({-href => $href, -class => "list subject",
1128 -title => to_utf8($long)},
1129 esc_html($short) . $extra);
1130 } else {
1131 return $cgi->a({-href => $href, -class => "list subject"},
1132 esc_html($long) . $extra);
1136 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1137 sub format_git_diff_header_line {
1138 my $line = shift;
1139 my $diffinfo = shift;
1140 my ($from, $to) = @_;
1142 if ($diffinfo->{'nparents'}) {
1143 # combined diff
1144 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1145 if ($to->{'href'}) {
1146 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1147 esc_path($to->{'file'}));
1148 } else { # file was deleted (no href)
1149 $line .= esc_path($to->{'file'});
1151 } else {
1152 # "ordinary" diff
1153 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1154 if ($from->{'href'}) {
1155 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1156 'a/' . esc_path($from->{'file'}));
1157 } else { # file was added (no href)
1158 $line .= 'a/' . esc_path($from->{'file'});
1160 $line .= ' ';
1161 if ($to->{'href'}) {
1162 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1163 'b/' . esc_path($to->{'file'}));
1164 } else { # file was deleted
1165 $line .= 'b/' . esc_path($to->{'file'});
1169 return "<div class=\"diff header\">$line</div>\n";
1172 # format extended diff header line, before patch itself
1173 sub format_extended_diff_header_line {
1174 my $line = shift;
1175 my $diffinfo = shift;
1176 my ($from, $to) = @_;
1178 # match <path>
1179 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1180 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1181 esc_path($from->{'file'}));
1183 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1184 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1185 esc_path($to->{'file'}));
1187 # match single <mode>
1188 if ($line =~ m/\s(\d{6})$/) {
1189 $line .= '<span class="info"> (' .
1190 file_type_long($1) .
1191 ')</span>';
1193 # match <hash>
1194 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1195 # can match only for combined diff
1196 $line = 'index ';
1197 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1198 if ($from->{'href'}[$i]) {
1199 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1200 -class=>"hash"},
1201 substr($diffinfo->{'from_id'}[$i],0,7));
1202 } else {
1203 $line .= '0' x 7;
1205 # separator
1206 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1208 $line .= '..';
1209 if ($to->{'href'}) {
1210 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1211 substr($diffinfo->{'to_id'},0,7));
1212 } else {
1213 $line .= '0' x 7;
1216 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1217 # can match only for ordinary diff
1218 my ($from_link, $to_link);
1219 if ($from->{'href'}) {
1220 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1221 substr($diffinfo->{'from_id'},0,7));
1222 } else {
1223 $from_link = '0' x 7;
1225 if ($to->{'href'}) {
1226 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1227 substr($diffinfo->{'to_id'},0,7));
1228 } else {
1229 $to_link = '0' x 7;
1231 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1232 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1235 return $line . "<br/>\n";
1238 # format from-file/to-file diff header
1239 sub format_diff_from_to_header {
1240 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1241 my $line;
1242 my $result = '';
1244 $line = $from_line;
1245 #assert($line =~ m/^---/) if DEBUG;
1246 # no extra formatting for "^--- /dev/null"
1247 if (! $diffinfo->{'nparents'}) {
1248 # ordinary (single parent) diff
1249 if ($line =~ m!^--- "?a/!) {
1250 if ($from->{'href'}) {
1251 $line = '--- a/' .
1252 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1253 esc_path($from->{'file'}));
1254 } else {
1255 $line = '--- a/' .
1256 esc_path($from->{'file'});
1259 $result .= qq!<div class="diff from_file">$line</div>\n!;
1261 } else {
1262 # combined diff (merge commit)
1263 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1264 if ($from->{'href'}[$i]) {
1265 $line = '--- ' .
1266 $cgi->a({-href=>href(action=>"blobdiff",
1267 hash_parent=>$diffinfo->{'from_id'}[$i],
1268 hash_parent_base=>$parents[$i],
1269 file_parent=>$from->{'file'}[$i],
1270 hash=>$diffinfo->{'to_id'},
1271 hash_base=>$hash,
1272 file_name=>$to->{'file'}),
1273 -class=>"path",
1274 -title=>"diff" . ($i+1)},
1275 $i+1) .
1276 '/' .
1277 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1278 esc_path($from->{'file'}[$i]));
1279 } else {
1280 $line = '--- /dev/null';
1282 $result .= qq!<div class="diff from_file">$line</div>\n!;
1286 $line = $to_line;
1287 #assert($line =~ m/^\+\+\+/) if DEBUG;
1288 # no extra formatting for "^+++ /dev/null"
1289 if ($line =~ m!^\+\+\+ "?b/!) {
1290 if ($to->{'href'}) {
1291 $line = '+++ b/' .
1292 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1293 esc_path($to->{'file'}));
1294 } else {
1295 $line = '+++ b/' .
1296 esc_path($to->{'file'});
1299 $result .= qq!<div class="diff to_file">$line</div>\n!;
1301 return $result;
1304 # create note for patch simplified by combined diff
1305 sub format_diff_cc_simplified {
1306 my ($diffinfo, @parents) = @_;
1307 my $result = '';
1309 $result .= "<div class=\"diff header\">" .
1310 "diff --cc ";
1311 if (!is_deleted($diffinfo)) {
1312 $result .= $cgi->a({-href => href(action=>"blob",
1313 hash_base=>$hash,
1314 hash=>$diffinfo->{'to_id'},
1315 file_name=>$diffinfo->{'to_file'}),
1316 -class => "path"},
1317 esc_path($diffinfo->{'to_file'}));
1318 } else {
1319 $result .= esc_path($diffinfo->{'to_file'});
1321 $result .= "</div>\n" . # class="diff header"
1322 "<div class=\"diff nodifferences\">" .
1323 "Simple merge" .
1324 "</div>\n"; # class="diff nodifferences"
1326 return $result;
1329 # format patch (diff) line (not to be used for diff headers)
1330 sub format_diff_line {
1331 my $line = shift;
1332 my ($from, $to) = @_;
1333 my $diff_class = "";
1335 chomp $line;
1337 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1338 # combined diff
1339 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1340 if ($line =~ m/^\@{3}/) {
1341 $diff_class = " chunk_header";
1342 } elsif ($line =~ m/^\\/) {
1343 $diff_class = " incomplete";
1344 } elsif ($prefix =~ tr/+/+/) {
1345 $diff_class = " add";
1346 } elsif ($prefix =~ tr/-/-/) {
1347 $diff_class = " rem";
1349 } else {
1350 # assume ordinary diff
1351 my $char = substr($line, 0, 1);
1352 if ($char eq '+') {
1353 $diff_class = " add";
1354 } elsif ($char eq '-') {
1355 $diff_class = " rem";
1356 } elsif ($char eq '@') {
1357 $diff_class = " chunk_header";
1358 } elsif ($char eq "\\") {
1359 $diff_class = " incomplete";
1362 $line = untabify($line);
1363 if ($from && $to && $line =~ m/^\@{2} /) {
1364 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1365 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1367 $from_lines = 0 unless defined $from_lines;
1368 $to_lines = 0 unless defined $to_lines;
1370 if ($from->{'href'}) {
1371 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1372 -class=>"list"}, $from_text);
1374 if ($to->{'href'}) {
1375 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1376 -class=>"list"}, $to_text);
1378 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1379 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1380 return "<div class=\"diff$diff_class\">$line</div>\n";
1381 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1382 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1383 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1385 @from_text = split(' ', $ranges);
1386 for (my $i = 0; $i < @from_text; ++$i) {
1387 ($from_start[$i], $from_nlines[$i]) =
1388 (split(',', substr($from_text[$i], 1)), 0);
1391 $to_text = pop @from_text;
1392 $to_start = pop @from_start;
1393 $to_nlines = pop @from_nlines;
1395 $line = "<span class=\"chunk_info\">$prefix ";
1396 for (my $i = 0; $i < @from_text; ++$i) {
1397 if ($from->{'href'}[$i]) {
1398 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1399 -class=>"list"}, $from_text[$i]);
1400 } else {
1401 $line .= $from_text[$i];
1403 $line .= " ";
1405 if ($to->{'href'}) {
1406 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1407 -class=>"list"}, $to_text);
1408 } else {
1409 $line .= $to_text;
1411 $line .= " $prefix</span>" .
1412 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1413 return "<div class=\"diff$diff_class\">$line</div>\n";
1415 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1418 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1419 # linked. Pass the hash of the tree/commit to snapshot.
1420 sub format_snapshot_links {
1421 my ($hash) = @_;
1422 my @snapshot_fmts = gitweb_check_feature('snapshot');
1423 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1424 my $num_fmts = @snapshot_fmts;
1425 if ($num_fmts > 1) {
1426 # A parenthesized list of links bearing format names.
1427 # e.g. "snapshot (_tar.gz_ _zip_)"
1428 return "snapshot (" . join(' ', map
1429 $cgi->a({
1430 -href => href(
1431 action=>"snapshot",
1432 hash=>$hash,
1433 snapshot_format=>$_
1435 }, $known_snapshot_formats{$_}{'display'})
1436 , @snapshot_fmts) . ")";
1437 } elsif ($num_fmts == 1) {
1438 # A single "snapshot" link whose tooltip bears the format name.
1439 # i.e. "_snapshot_"
1440 my ($fmt) = @snapshot_fmts;
1441 return
1442 $cgi->a({
1443 -href => href(
1444 action=>"snapshot",
1445 hash=>$hash,
1446 snapshot_format=>$fmt
1448 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1449 }, "snapshot");
1450 } else { # $num_fmts == 0
1451 return undef;
1455 ## ......................................................................
1456 ## functions returning values to be passed, perhaps after some
1457 ## transformation, to other functions; e.g. returning arguments to href()
1459 # returns hash to be passed to href to generate gitweb URL
1460 # in -title key it returns description of link
1461 sub get_feed_info {
1462 my $format = shift || 'Atom';
1463 my %res = (action => lc($format));
1465 # feed links are possible only for project views
1466 return unless (defined $project);
1467 # some views should link to OPML, or to generic project feed,
1468 # or don't have specific feed yet (so they should use generic)
1469 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1471 my $branch;
1472 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1473 # from tag links; this also makes possible to detect branch links
1474 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1475 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1476 $branch = $1;
1478 # find log type for feed description (title)
1479 my $type = 'log';
1480 if (defined $file_name) {
1481 $type = "history of $file_name";
1482 $type .= "/" if ($action eq 'tree');
1483 $type .= " on '$branch'" if (defined $branch);
1484 } else {
1485 $type = "log of $branch" if (defined $branch);
1488 $res{-title} = $type;
1489 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1490 $res{'file_name'} = $file_name;
1492 return %res;
1495 ## ----------------------------------------------------------------------
1496 ## git utility subroutines, invoking git commands
1498 # returns path to the core git executable and the --git-dir parameter as list
1499 sub git_cmd {
1500 return $GIT, '--git-dir='.$git_dir;
1503 # quote the given arguments for passing them to the shell
1504 # quote_command("command", "arg 1", "arg with ' and ! characters")
1505 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1506 # Try to avoid using this function wherever possible.
1507 sub quote_command {
1508 return join(' ',
1509 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1512 # get HEAD ref of given project as hash
1513 sub git_get_head_hash {
1514 my $project = shift;
1515 my $o_git_dir = $git_dir;
1516 my $retval = undef;
1517 $git_dir = "$projectroot/$project";
1518 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1519 my $head = <$fd>;
1520 close $fd;
1521 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1522 $retval = $1;
1525 if (defined $o_git_dir) {
1526 $git_dir = $o_git_dir;
1528 return $retval;
1531 # get type of given object
1532 sub git_get_type {
1533 my $hash = shift;
1535 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1536 my $type = <$fd>;
1537 close $fd or return;
1538 chomp $type;
1539 return $type;
1542 # repository configuration
1543 our $config_file = '';
1544 our %config;
1546 # store multiple values for single key as anonymous array reference
1547 # single values stored directly in the hash, not as [ <value> ]
1548 sub hash_set_multi {
1549 my ($hash, $key, $value) = @_;
1551 if (!exists $hash->{$key}) {
1552 $hash->{$key} = $value;
1553 } elsif (!ref $hash->{$key}) {
1554 $hash->{$key} = [ $hash->{$key}, $value ];
1555 } else {
1556 push @{$hash->{$key}}, $value;
1560 # return hash of git project configuration
1561 # optionally limited to some section, e.g. 'gitweb'
1562 sub git_parse_project_config {
1563 my $section_regexp = shift;
1564 my %config;
1566 local $/ = "\0";
1568 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1569 or return;
1571 while (my $keyval = <$fh>) {
1572 chomp $keyval;
1573 my ($key, $value) = split(/\n/, $keyval, 2);
1575 hash_set_multi(\%config, $key, $value)
1576 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1578 close $fh;
1580 return %config;
1583 # convert config value to boolean, 'true' or 'false'
1584 # no value, number > 0, 'true' and 'yes' values are true
1585 # rest of values are treated as false (never as error)
1586 sub config_to_bool {
1587 my $val = shift;
1589 # strip leading and trailing whitespace
1590 $val =~ s/^\s+//;
1591 $val =~ s/\s+$//;
1593 return (!defined $val || # section.key
1594 ($val =~ /^\d+$/ && $val) || # section.key = 1
1595 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1598 # convert config value to simple decimal number
1599 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1600 # to be multiplied by 1024, 1048576, or 1073741824
1601 sub config_to_int {
1602 my $val = shift;
1604 # strip leading and trailing whitespace
1605 $val =~ s/^\s+//;
1606 $val =~ s/\s+$//;
1608 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1609 $unit = lc($unit);
1610 # unknown unit is treated as 1
1611 return $num * ($unit eq 'g' ? 1073741824 :
1612 $unit eq 'm' ? 1048576 :
1613 $unit eq 'k' ? 1024 : 1);
1615 return $val;
1618 # convert config value to array reference, if needed
1619 sub config_to_multi {
1620 my $val = shift;
1622 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1625 sub git_get_project_config {
1626 my ($key, $type) = @_;
1628 # key sanity check
1629 return unless ($key);
1630 $key =~ s/^gitweb\.//;
1631 return if ($key =~ m/\W/);
1633 # type sanity check
1634 if (defined $type) {
1635 $type =~ s/^--//;
1636 $type = undef
1637 unless ($type eq 'bool' || $type eq 'int');
1640 # get config
1641 if (!defined $config_file ||
1642 $config_file ne "$git_dir/config") {
1643 %config = git_parse_project_config('gitweb');
1644 $config_file = "$git_dir/config";
1647 # ensure given type
1648 if (!defined $type) {
1649 return $config{"gitweb.$key"};
1650 } elsif ($type eq 'bool') {
1651 # backward compatibility: 'git config --bool' returns true/false
1652 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1653 } elsif ($type eq 'int') {
1654 return config_to_int($config{"gitweb.$key"});
1656 return $config{"gitweb.$key"};
1659 # get hash of given path at given ref
1660 sub git_get_hash_by_path {
1661 my $base = shift;
1662 my $path = shift || return undef;
1663 my $type = shift;
1665 $path =~ s,/+$,,;
1667 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1668 or die_error(undef, "Open git-ls-tree failed");
1669 my $line = <$fd>;
1670 close $fd or return undef;
1672 if (!defined $line) {
1673 # there is no tree or hash given by $path at $base
1674 return undef;
1677 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1678 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1679 if (defined $type && $type ne $2) {
1680 # type doesn't match
1681 return undef;
1683 return $3;
1686 # get path of entry with given hash at given tree-ish (ref)
1687 # used to get 'from' filename for combined diff (merge commit) for renames
1688 sub git_get_path_by_hash {
1689 my $base = shift || return;
1690 my $hash = shift || return;
1692 local $/ = "\0";
1694 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1695 or return undef;
1696 while (my $line = <$fd>) {
1697 chomp $line;
1699 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1700 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1701 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1702 close $fd;
1703 return $1;
1706 close $fd;
1707 return undef;
1710 ## ......................................................................
1711 ## git utility functions, directly accessing git repository
1713 sub git_get_project_description {
1714 my $path = shift;
1716 $git_dir = "$projectroot/$path";
1717 open my $fd, "$git_dir/description"
1718 or return git_get_project_config('description');
1719 my $descr = <$fd>;
1720 close $fd;
1721 if (defined $descr) {
1722 chomp $descr;
1724 return $descr;
1727 sub git_get_project_url_list {
1728 my $path = shift;
1730 $git_dir = "$projectroot/$path";
1731 open my $fd, "$git_dir/cloneurl"
1732 or return wantarray ?
1733 @{ config_to_multi(git_get_project_config('url')) } :
1734 config_to_multi(git_get_project_config('url'));
1735 my @git_project_url_list = map { chomp; $_ } <$fd>;
1736 close $fd;
1738 return wantarray ? @git_project_url_list : \@git_project_url_list;
1741 sub git_get_projects_list {
1742 my ($filter) = @_;
1743 my @list;
1745 $filter ||= '';
1746 $filter =~ s/\.git$//;
1748 my ($check_forks) = gitweb_check_feature('forks');
1750 if (-d $projects_list) {
1751 # search in directory
1752 my $dir = $projects_list . ($filter ? "/$filter" : '');
1753 # remove the trailing "/"
1754 $dir =~ s!/+$!!;
1755 my $pfxlen = length("$dir");
1756 my $pfxdepth = ($dir =~ tr!/!!);
1758 File::Find::find({
1759 follow_fast => 1, # follow symbolic links
1760 follow_skip => 2, # ignore duplicates
1761 dangling_symlinks => 0, # ignore dangling symlinks, silently
1762 wanted => sub {
1763 # skip project-list toplevel, if we get it.
1764 return if (m!^[/.]$!);
1765 # only directories can be git repositories
1766 return unless (-d $_);
1767 # don't traverse too deep (Find is super slow on os x)
1768 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1769 $File::Find::prune = 1;
1770 return;
1773 my $subdir = substr($File::Find::name, $pfxlen + 1);
1774 # we check related file in $projectroot
1775 if ($check_forks and $subdir =~ m#/.#) {
1776 $File::Find::prune = 1;
1777 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1778 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1779 $File::Find::prune = 1;
1782 }, "$dir");
1784 } elsif (-f $projects_list) {
1785 # read from file(url-encoded):
1786 # 'git%2Fgit.git Linus+Torvalds'
1787 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1788 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1789 my %paths;
1790 open my ($fd), $projects_list or return;
1791 PROJECT:
1792 while (my $line = <$fd>) {
1793 chomp $line;
1794 my ($path, $owner) = split ' ', $line;
1795 $path = unescape($path);
1796 $owner = unescape($owner);
1797 if (!defined $path) {
1798 next;
1800 if ($filter ne '') {
1801 # looking for forks;
1802 my $pfx = substr($path, 0, length($filter));
1803 if ($pfx ne $filter) {
1804 next PROJECT;
1806 my $sfx = substr($path, length($filter));
1807 if ($sfx !~ /^\/.*\.git$/) {
1808 next PROJECT;
1810 } elsif ($check_forks) {
1811 PATH:
1812 foreach my $filter (keys %paths) {
1813 # looking for forks;
1814 my $pfx = substr($path, 0, length($filter));
1815 if ($pfx ne $filter) {
1816 next PATH;
1818 my $sfx = substr($path, length($filter));
1819 if ($sfx !~ /^\/.*\.git$/) {
1820 next PATH;
1822 # is a fork, don't include it in
1823 # the list
1824 next PROJECT;
1827 if (check_export_ok("$projectroot/$path")) {
1828 my $pr = {
1829 path => $path,
1830 owner => to_utf8($owner),
1832 push @list, $pr;
1833 (my $forks_path = $path) =~ s/\.git$//;
1834 $paths{$forks_path}++;
1837 close $fd;
1839 return @list;
1842 our $gitweb_project_owner = undef;
1843 sub git_get_project_list_from_file {
1845 return if (defined $gitweb_project_owner);
1847 $gitweb_project_owner = {};
1848 # read from file (url-encoded):
1849 # 'git%2Fgit.git Linus+Torvalds'
1850 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1851 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1852 if (-f $projects_list) {
1853 open (my $fd , $projects_list);
1854 while (my $line = <$fd>) {
1855 chomp $line;
1856 my ($pr, $ow) = split ' ', $line;
1857 $pr = unescape($pr);
1858 $ow = unescape($ow);
1859 $gitweb_project_owner->{$pr} = to_utf8($ow);
1861 close $fd;
1865 sub git_get_project_owner {
1866 my $project = shift;
1867 my $owner;
1869 return undef unless $project;
1870 $git_dir = "$projectroot/$project";
1872 if (!defined $gitweb_project_owner) {
1873 git_get_project_list_from_file();
1876 if (exists $gitweb_project_owner->{$project}) {
1877 $owner = $gitweb_project_owner->{$project};
1879 if (!defined $owner){
1880 $owner = git_get_project_config('owner');
1882 if (!defined $owner) {
1883 $owner = get_file_owner("$git_dir");
1886 return $owner;
1889 sub git_get_last_activity {
1890 my ($path) = @_;
1891 my $fd;
1893 $git_dir = "$projectroot/$path";
1894 open($fd, "-|", git_cmd(), 'for-each-ref',
1895 '--format=%(committer)',
1896 '--sort=-committerdate',
1897 '--count=1',
1898 'refs/heads') or return;
1899 my $most_recent = <$fd>;
1900 close $fd or return;
1901 if (defined $most_recent &&
1902 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1903 my $timestamp = $1;
1904 my $age = time - $timestamp;
1905 return ($age, age_string($age));
1907 return (undef, undef);
1910 sub git_get_references {
1911 my $type = shift || "";
1912 my %refs;
1913 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1914 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1915 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1916 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1917 or return;
1919 while (my $line = <$fd>) {
1920 chomp $line;
1921 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1922 if (defined $refs{$1}) {
1923 push @{$refs{$1}}, $2;
1924 } else {
1925 $refs{$1} = [ $2 ];
1929 close $fd or return;
1930 return \%refs;
1933 sub git_get_rev_name_tags {
1934 my $hash = shift || return undef;
1936 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1937 or return;
1938 my $name_rev = <$fd>;
1939 close $fd;
1941 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1942 return $1;
1943 } else {
1944 # catches also '$hash undefined' output
1945 return undef;
1949 ## ----------------------------------------------------------------------
1950 ## parse to hash functions
1952 sub parse_date {
1953 my $epoch = shift;
1954 my $tz = shift || "-0000";
1956 my %date;
1957 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1958 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1959 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1960 $date{'hour'} = $hour;
1961 $date{'minute'} = $min;
1962 $date{'mday'} = $mday;
1963 $date{'day'} = $days[$wday];
1964 $date{'month'} = $months[$mon];
1965 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1966 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1967 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1968 $mday, $months[$mon], $hour ,$min;
1969 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1970 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
1972 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1973 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1974 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1975 $date{'hour_local'} = $hour;
1976 $date{'minute_local'} = $min;
1977 $date{'tz_local'} = $tz;
1978 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1979 1900+$year, $mon+1, $mday,
1980 $hour, $min, $sec, $tz);
1981 return %date;
1984 sub parse_tag {
1985 my $tag_id = shift;
1986 my %tag;
1987 my @comment;
1989 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1990 $tag{'id'} = $tag_id;
1991 while (my $line = <$fd>) {
1992 chomp $line;
1993 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1994 $tag{'object'} = $1;
1995 } elsif ($line =~ m/^type (.+)$/) {
1996 $tag{'type'} = $1;
1997 } elsif ($line =~ m/^tag (.+)$/) {
1998 $tag{'name'} = $1;
1999 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2000 $tag{'author'} = $1;
2001 $tag{'epoch'} = $2;
2002 $tag{'tz'} = $3;
2003 } elsif ($line =~ m/--BEGIN/) {
2004 push @comment, $line;
2005 last;
2006 } elsif ($line eq "") {
2007 last;
2010 push @comment, <$fd>;
2011 $tag{'comment'} = \@comment;
2012 close $fd or return;
2013 if (!defined $tag{'name'}) {
2014 return
2016 return %tag
2019 sub parse_commit_text {
2020 my ($commit_text, $withparents) = @_;
2021 my @commit_lines = split '\n', $commit_text;
2022 my %co;
2024 pop @commit_lines; # Remove '\0'
2026 if (! @commit_lines) {
2027 return;
2030 my $header = shift @commit_lines;
2031 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2032 return;
2034 ($co{'id'}, my @parents) = split ' ', $header;
2035 while (my $line = shift @commit_lines) {
2036 last if $line eq "\n";
2037 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2038 $co{'tree'} = $1;
2039 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2040 push @parents, $1;
2041 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2042 $co{'author'} = $1;
2043 $co{'author_epoch'} = $2;
2044 $co{'author_tz'} = $3;
2045 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2046 $co{'author_name'} = $1;
2047 $co{'author_email'} = $2;
2048 } else {
2049 $co{'author_name'} = $co{'author'};
2051 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2052 $co{'committer'} = $1;
2053 $co{'committer_epoch'} = $2;
2054 $co{'committer_tz'} = $3;
2055 $co{'committer_name'} = $co{'committer'};
2056 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2057 $co{'committer_name'} = $1;
2058 $co{'committer_email'} = $2;
2059 } else {
2060 $co{'committer_name'} = $co{'committer'};
2064 if (!defined $co{'tree'}) {
2065 return;
2067 $co{'parents'} = \@parents;
2068 $co{'parent'} = $parents[0];
2070 foreach my $title (@commit_lines) {
2071 $title =~ s/^ //;
2072 if ($title ne "") {
2073 $co{'title'} = chop_str($title, 80, 5);
2074 # remove leading stuff of merges to make the interesting part visible
2075 if (length($title) > 50) {
2076 $title =~ s/^Automatic //;
2077 $title =~ s/^merge (of|with) /Merge ... /i;
2078 if (length($title) > 50) {
2079 $title =~ s/(http|rsync):\/\///;
2081 if (length($title) > 50) {
2082 $title =~ s/(master|www|rsync)\.//;
2084 if (length($title) > 50) {
2085 $title =~ s/kernel.org:?//;
2087 if (length($title) > 50) {
2088 $title =~ s/\/pub\/scm//;
2091 $co{'title_short'} = chop_str($title, 50, 5);
2092 last;
2095 if ($co{'title'} eq "") {
2096 $co{'title'} = $co{'title_short'} = '(no commit message)';
2098 # remove added spaces
2099 foreach my $line (@commit_lines) {
2100 $line =~ s/^ //;
2102 $co{'comment'} = \@commit_lines;
2104 my $age = time - $co{'committer_epoch'};
2105 $co{'age'} = $age;
2106 $co{'age_string'} = age_string($age);
2107 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2108 if ($age > 60*60*24*7*2) {
2109 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2110 $co{'age_string_age'} = $co{'age_string'};
2111 } else {
2112 $co{'age_string_date'} = $co{'age_string'};
2113 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2115 return %co;
2118 sub parse_commit {
2119 my ($commit_id) = @_;
2120 my %co;
2122 local $/ = "\0";
2124 open my $fd, "-|", git_cmd(), "rev-list",
2125 "--parents",
2126 "--header",
2127 "--max-count=1",
2128 $commit_id,
2129 "--",
2130 or die_error(undef, "Open git-rev-list failed");
2131 %co = parse_commit_text(<$fd>, 1);
2132 close $fd;
2134 return %co;
2137 sub parse_commits {
2138 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2139 my @cos;
2141 $maxcount ||= 1;
2142 $skip ||= 0;
2144 local $/ = "\0";
2146 open my $fd, "-|", git_cmd(), "rev-list",
2147 "--header",
2148 @args,
2149 ("--max-count=" . $maxcount),
2150 ("--skip=" . $skip),
2151 @extra_options,
2152 $commit_id,
2153 "--",
2154 ($filename ? ($filename) : ())
2155 or die_error(undef, "Open git-rev-list failed");
2156 while (my $line = <$fd>) {
2157 my %co = parse_commit_text($line);
2158 push @cos, \%co;
2160 close $fd;
2162 return wantarray ? @cos : \@cos;
2165 # parse line of git-diff-tree "raw" output
2166 sub parse_difftree_raw_line {
2167 my $line = shift;
2168 my %res;
2170 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2171 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2172 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2173 $res{'from_mode'} = $1;
2174 $res{'to_mode'} = $2;
2175 $res{'from_id'} = $3;
2176 $res{'to_id'} = $4;
2177 $res{'status'} = $5;
2178 $res{'similarity'} = $6;
2179 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2180 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2181 } else {
2182 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2185 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2186 # combined diff (for merge commit)
2187 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2188 $res{'nparents'} = length($1);
2189 $res{'from_mode'} = [ split(' ', $2) ];
2190 $res{'to_mode'} = pop @{$res{'from_mode'}};
2191 $res{'from_id'} = [ split(' ', $3) ];
2192 $res{'to_id'} = pop @{$res{'from_id'}};
2193 $res{'status'} = [ split('', $4) ];
2194 $res{'to_file'} = unquote($5);
2196 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2197 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2198 $res{'commit'} = $1;
2201 return wantarray ? %res : \%res;
2204 # wrapper: return parsed line of git-diff-tree "raw" output
2205 # (the argument might be raw line, or parsed info)
2206 sub parsed_difftree_line {
2207 my $line_or_ref = shift;
2209 if (ref($line_or_ref) eq "HASH") {
2210 # pre-parsed (or generated by hand)
2211 return $line_or_ref;
2212 } else {
2213 return parse_difftree_raw_line($line_or_ref);
2217 # parse line of git-ls-tree output
2218 sub parse_ls_tree_line ($;%) {
2219 my $line = shift;
2220 my %opts = @_;
2221 my %res;
2223 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2224 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2226 $res{'mode'} = $1;
2227 $res{'type'} = $2;
2228 $res{'hash'} = $3;
2229 if ($opts{'-z'}) {
2230 $res{'name'} = $4;
2231 } else {
2232 $res{'name'} = unquote($4);
2235 return wantarray ? %res : \%res;
2238 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2239 sub parse_from_to_diffinfo {
2240 my ($diffinfo, $from, $to, @parents) = @_;
2242 if ($diffinfo->{'nparents'}) {
2243 # combined diff
2244 $from->{'file'} = [];
2245 $from->{'href'} = [];
2246 fill_from_file_info($diffinfo, @parents)
2247 unless exists $diffinfo->{'from_file'};
2248 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2249 $from->{'file'}[$i] =
2250 defined $diffinfo->{'from_file'}[$i] ?
2251 $diffinfo->{'from_file'}[$i] :
2252 $diffinfo->{'to_file'};
2253 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2254 $from->{'href'}[$i] = href(action=>"blob",
2255 hash_base=>$parents[$i],
2256 hash=>$diffinfo->{'from_id'}[$i],
2257 file_name=>$from->{'file'}[$i]);
2258 } else {
2259 $from->{'href'}[$i] = undef;
2262 } else {
2263 # ordinary (not combined) diff
2264 $from->{'file'} = $diffinfo->{'from_file'};
2265 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2266 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2267 hash=>$diffinfo->{'from_id'},
2268 file_name=>$from->{'file'});
2269 } else {
2270 delete $from->{'href'};
2274 $to->{'file'} = $diffinfo->{'to_file'};
2275 if (!is_deleted($diffinfo)) { # file exists in result
2276 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2277 hash=>$diffinfo->{'to_id'},
2278 file_name=>$to->{'file'});
2279 } else {
2280 delete $to->{'href'};
2284 ## ......................................................................
2285 ## parse to array of hashes functions
2287 sub git_get_heads_list {
2288 my $limit = shift;
2289 my @headslist;
2291 open my $fd, '-|', git_cmd(), 'for-each-ref',
2292 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2293 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2294 'refs/heads'
2295 or return;
2296 while (my $line = <$fd>) {
2297 my %ref_item;
2299 chomp $line;
2300 my ($refinfo, $committerinfo) = split(/\0/, $line);
2301 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2302 my ($committer, $epoch, $tz) =
2303 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2304 $ref_item{'fullname'} = $name;
2305 $name =~ s!^refs/heads/!!;
2307 $ref_item{'name'} = $name;
2308 $ref_item{'id'} = $hash;
2309 $ref_item{'title'} = $title || '(no commit message)';
2310 $ref_item{'epoch'} = $epoch;
2311 if ($epoch) {
2312 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2313 } else {
2314 $ref_item{'age'} = "unknown";
2317 push @headslist, \%ref_item;
2319 close $fd;
2321 return wantarray ? @headslist : \@headslist;
2324 sub git_get_tags_list {
2325 my $limit = shift;
2326 my @tagslist;
2328 open my $fd, '-|', git_cmd(), 'for-each-ref',
2329 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2330 '--format=%(objectname) %(objecttype) %(refname) '.
2331 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2332 'refs/tags'
2333 or return;
2334 while (my $line = <$fd>) {
2335 my %ref_item;
2337 chomp $line;
2338 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2339 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2340 my ($creator, $epoch, $tz) =
2341 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2342 $ref_item{'fullname'} = $name;
2343 $name =~ s!^refs/tags/!!;
2345 $ref_item{'type'} = $type;
2346 $ref_item{'id'} = $id;
2347 $ref_item{'name'} = $name;
2348 if ($type eq "tag") {
2349 $ref_item{'subject'} = $title;
2350 $ref_item{'reftype'} = $reftype;
2351 $ref_item{'refid'} = $refid;
2352 } else {
2353 $ref_item{'reftype'} = $type;
2354 $ref_item{'refid'} = $id;
2357 if ($type eq "tag" || $type eq "commit") {
2358 $ref_item{'epoch'} = $epoch;
2359 if ($epoch) {
2360 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2361 } else {
2362 $ref_item{'age'} = "unknown";
2366 push @tagslist, \%ref_item;
2368 close $fd;
2370 return wantarray ? @tagslist : \@tagslist;
2373 ## ----------------------------------------------------------------------
2374 ## filesystem-related functions
2376 sub get_file_owner {
2377 my $path = shift;
2379 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2380 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2381 if (!defined $gcos) {
2382 return undef;
2384 my $owner = $gcos;
2385 $owner =~ s/[,;].*$//;
2386 return to_utf8($owner);
2389 ## ......................................................................
2390 ## mimetype related functions
2392 sub mimetype_guess_file {
2393 my $filename = shift;
2394 my $mimemap = shift;
2395 -r $mimemap or return undef;
2397 my %mimemap;
2398 open(MIME, $mimemap) or return undef;
2399 while (<MIME>) {
2400 next if m/^#/; # skip comments
2401 my ($mime, $exts) = split(/\t+/);
2402 if (defined $exts) {
2403 my @exts = split(/\s+/, $exts);
2404 foreach my $ext (@exts) {
2405 $mimemap{$ext} = $mime;
2409 close(MIME);
2411 $filename =~ /\.([^.]*)$/;
2412 return $mimemap{$1};
2415 sub mimetype_guess {
2416 my $filename = shift;
2417 my $mime;
2418 $filename =~ /\./ or return undef;
2420 if ($mimetypes_file) {
2421 my $file = $mimetypes_file;
2422 if ($file !~ m!^/!) { # if it is relative path
2423 # it is relative to project
2424 $file = "$projectroot/$project/$file";
2426 $mime = mimetype_guess_file($filename, $file);
2428 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2429 return $mime;
2432 sub blob_mimetype {
2433 my $fd = shift;
2434 my $filename = shift;
2436 if ($filename) {
2437 my $mime = mimetype_guess($filename);
2438 $mime and return $mime;
2441 # just in case
2442 return $default_blob_plain_mimetype unless $fd;
2444 if (-T $fd) {
2445 return 'text/plain';
2446 } elsif (! $filename) {
2447 return 'application/octet-stream';
2448 } elsif ($filename =~ m/\.png$/i) {
2449 return 'image/png';
2450 } elsif ($filename =~ m/\.gif$/i) {
2451 return 'image/gif';
2452 } elsif ($filename =~ m/\.jpe?g$/i) {
2453 return 'image/jpeg';
2454 } else {
2455 return 'application/octet-stream';
2459 sub blob_contenttype {
2460 my ($fd, $file_name, $type) = @_;
2462 $type ||= blob_mimetype($fd, $file_name);
2463 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2464 $type .= "; charset=$default_text_plain_charset";
2467 return $type;
2470 ## ======================================================================
2471 ## functions printing HTML: header, footer, error page
2473 sub git_header_html {
2474 my $status = shift || "200 OK";
2475 my $expires = shift;
2477 my $title = "$site_name";
2478 if (defined $project) {
2479 $title .= " - " . to_utf8($project);
2480 if (defined $action) {
2481 $title .= "/$action";
2482 if (defined $file_name) {
2483 $title .= " - " . esc_path($file_name);
2484 if ($action eq "tree" && $file_name !~ m|/$|) {
2485 $title .= "/";
2490 my $content_type;
2491 # require explicit support from the UA if we are to send the page as
2492 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2493 # we have to do this because MSIE sometimes globs '*/*', pretending to
2494 # support xhtml+xml but choking when it gets what it asked for.
2495 if (defined $cgi->http('HTTP_ACCEPT') &&
2496 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2497 $cgi->Accept('application/xhtml+xml') != 0) {
2498 $content_type = 'application/xhtml+xml';
2499 } else {
2500 $content_type = 'text/html';
2502 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2503 -status=> $status, -expires => $expires);
2504 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2505 print <<EOF;
2506 <?xml version="1.0" encoding="utf-8"?>
2507 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2508 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2509 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2510 <!-- git core binaries version $git_version -->
2511 <head>
2512 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2513 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2514 <meta name="robots" content="index, nofollow"/>
2515 <title>$title</title>
2517 # print out each stylesheet that exist
2518 if (defined $stylesheet) {
2519 #provides backwards capability for those people who define style sheet in a config file
2520 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2521 } else {
2522 foreach my $stylesheet (@stylesheets) {
2523 next unless $stylesheet;
2524 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2527 if (defined $project) {
2528 my %href_params = get_feed_info();
2529 if (!exists $href_params{'-title'}) {
2530 $href_params{'-title'} = 'log';
2533 foreach my $format qw(RSS Atom) {
2534 my $type = lc($format);
2535 my %link_attr = (
2536 '-rel' => 'alternate',
2537 '-title' => "$project - $href_params{'-title'} - $format feed",
2538 '-type' => "application/$type+xml"
2541 $href_params{'action'} = $type;
2542 $link_attr{'-href'} = href(%href_params);
2543 print "<link ".
2544 "rel=\"$link_attr{'-rel'}\" ".
2545 "title=\"$link_attr{'-title'}\" ".
2546 "href=\"$link_attr{'-href'}\" ".
2547 "type=\"$link_attr{'-type'}\" ".
2548 "/>\n";
2550 $href_params{'extra_options'} = '--no-merges';
2551 $link_attr{'-href'} = href(%href_params);
2552 $link_attr{'-title'} .= ' (no merges)';
2553 print "<link ".
2554 "rel=\"$link_attr{'-rel'}\" ".
2555 "title=\"$link_attr{'-title'}\" ".
2556 "href=\"$link_attr{'-href'}\" ".
2557 "type=\"$link_attr{'-type'}\" ".
2558 "/>\n";
2561 } else {
2562 printf('<link rel="alternate" title="%s projects list" '.
2563 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2564 $site_name, href(project=>undef, action=>"project_index"));
2565 printf('<link rel="alternate" title="%s projects feeds" '.
2566 'href="%s" type="text/x-opml" />'."\n",
2567 $site_name, href(project=>undef, action=>"opml"));
2569 if (defined $favicon) {
2570 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2573 print "</head>\n" .
2574 "<body>\n";
2576 if (-f $site_header) {
2577 open (my $fd, $site_header);
2578 print <$fd>;
2579 close $fd;
2582 print "<div class=\"page_header\">\n" .
2583 $cgi->a({-href => esc_url($logo_url),
2584 -title => $logo_label},
2585 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2586 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2587 if (defined $project) {
2588 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2589 if (defined $action) {
2590 print " / $action";
2592 print "\n";
2594 print "</div>\n";
2596 my ($have_search) = gitweb_check_feature('search');
2597 if (defined $project && $have_search) {
2598 if (!defined $searchtext) {
2599 $searchtext = "";
2601 my $search_hash;
2602 if (defined $hash_base) {
2603 $search_hash = $hash_base;
2604 } elsif (defined $hash) {
2605 $search_hash = $hash;
2606 } else {
2607 $search_hash = "HEAD";
2609 my $action = $my_uri;
2610 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2611 if ($use_pathinfo) {
2612 $action .= "/".esc_url($project);
2614 print $cgi->startform(-method => "get", -action => $action) .
2615 "<div class=\"search\">\n" .
2616 (!$use_pathinfo &&
2617 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2618 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2619 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2620 $cgi->popup_menu(-name => 'st', -default => 'commit',
2621 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2622 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2623 " search:\n",
2624 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2625 "<span title=\"Extended regular expression\">" .
2626 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
2627 -checked => $search_use_regexp) .
2628 "</span>" .
2629 "</div>" .
2630 $cgi->end_form() . "\n";
2634 sub git_footer_html {
2635 my $feed_class = 'rss_logo';
2637 print "<div class=\"page_footer\">\n";
2638 if (defined $project) {
2639 my $descr = git_get_project_description($project);
2640 if (defined $descr) {
2641 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2644 my %href_params = get_feed_info();
2645 if (!%href_params) {
2646 $feed_class .= ' generic';
2648 $href_params{'-title'} ||= 'log';
2650 foreach my $format qw(RSS Atom) {
2651 $href_params{'action'} = lc($format);
2652 print $cgi->a({-href => href(%href_params),
2653 -title => "$href_params{'-title'} $format feed",
2654 -class => $feed_class}, $format)."\n";
2657 } else {
2658 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2659 -class => $feed_class}, "OPML") . " ";
2660 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2661 -class => $feed_class}, "TXT") . "\n";
2663 print "</div>\n"; # class="page_footer"
2665 if (-f $site_footer) {
2666 open (my $fd, $site_footer);
2667 print <$fd>;
2668 close $fd;
2671 print "</body>\n" .
2672 "</html>";
2675 sub die_error {
2676 my $status = shift || "403 Forbidden";
2677 my $error = shift || "Malformed query, file missing or permission denied";
2679 git_header_html($status);
2680 print <<EOF;
2681 <div class="page_body">
2682 <br /><br />
2683 $status - $error
2684 <br />
2685 </div>
2687 git_footer_html();
2688 exit;
2691 ## ----------------------------------------------------------------------
2692 ## functions printing or outputting HTML: navigation
2694 sub git_print_page_nav {
2695 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2696 $extra = '' if !defined $extra; # pager or formats
2698 my @navs = qw(summary shortlog log commit commitdiff tree);
2699 if ($suppress) {
2700 @navs = grep { $_ ne $suppress } @navs;
2703 my %arg = map { $_ => {action=>$_} } @navs;
2704 if (defined $head) {
2705 for (qw(commit commitdiff)) {
2706 $arg{$_}{'hash'} = $head;
2708 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2709 for (qw(shortlog log)) {
2710 $arg{$_}{'hash'} = $head;
2714 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2715 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2717 print "<div class=\"page_nav\">\n" .
2718 (join " | ",
2719 map { $_ eq $current ?
2720 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2721 } @navs);
2722 print "<br/>\n$extra<br/>\n" .
2723 "</div>\n";
2726 sub format_paging_nav {
2727 my ($action, $hash, $head, $page, $has_next_link) = @_;
2728 my $paging_nav;
2731 if ($hash ne $head || $page) {
2732 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2733 } else {
2734 $paging_nav .= "HEAD";
2737 if ($page > 0) {
2738 $paging_nav .= " &sdot; " .
2739 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2740 -accesskey => "p", -title => "Alt-p"}, "prev");
2741 } else {
2742 $paging_nav .= " &sdot; prev";
2745 if ($has_next_link) {
2746 $paging_nav .= " &sdot; " .
2747 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2748 -accesskey => "n", -title => "Alt-n"}, "next");
2749 } else {
2750 $paging_nav .= " &sdot; next";
2753 return $paging_nav;
2756 ## ......................................................................
2757 ## functions printing or outputting HTML: div
2759 sub git_print_header_div {
2760 my ($action, $title, $hash, $hash_base) = @_;
2761 my %args = ();
2763 $args{'action'} = $action;
2764 $args{'hash'} = $hash if $hash;
2765 $args{'hash_base'} = $hash_base if $hash_base;
2767 print "<div class=\"header\">\n" .
2768 $cgi->a({-href => href(%args), -class => "title"},
2769 $title ? $title : $action) .
2770 "\n</div>\n";
2773 #sub git_print_authorship (\%) {
2774 sub git_print_authorship {
2775 my $co = shift;
2777 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2778 print "<div class=\"author_date\">" .
2779 esc_html($co->{'author_name'}) .
2780 " [$ad{'rfc2822'}";
2781 if ($ad{'hour_local'} < 6) {
2782 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2783 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2784 } else {
2785 printf(" (%02d:%02d %s)",
2786 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2788 print "]</div>\n";
2791 sub git_print_page_path {
2792 my $name = shift;
2793 my $type = shift;
2794 my $hb = shift;
2797 print "<div class=\"page_path\">";
2798 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2799 -title => 'tree root'}, to_utf8("[$project]"));
2800 print " / ";
2801 if (defined $name) {
2802 my @dirname = split '/', $name;
2803 my $basename = pop @dirname;
2804 my $fullname = '';
2806 foreach my $dir (@dirname) {
2807 $fullname .= ($fullname ? '/' : '') . $dir;
2808 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2809 hash_base=>$hb),
2810 -title => $fullname}, esc_path($dir));
2811 print " / ";
2813 if (defined $type && $type eq 'blob') {
2814 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2815 hash_base=>$hb),
2816 -title => $name}, esc_path($basename));
2817 } elsif (defined $type && $type eq 'tree') {
2818 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2819 hash_base=>$hb),
2820 -title => $name}, esc_path($basename));
2821 print " / ";
2822 } else {
2823 print esc_path($basename);
2826 print "<br/></div>\n";
2829 # sub git_print_log (\@;%) {
2830 sub git_print_log ($;%) {
2831 my $log = shift;
2832 my %opts = @_;
2834 if ($opts{'-remove_title'}) {
2835 # remove title, i.e. first line of log
2836 shift @$log;
2838 # remove leading empty lines
2839 while (defined $log->[0] && $log->[0] eq "") {
2840 shift @$log;
2843 # print log
2844 my $signoff = 0;
2845 my $empty = 0;
2846 foreach my $line (@$log) {
2847 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2848 $signoff = 1;
2849 $empty = 0;
2850 if (! $opts{'-remove_signoff'}) {
2851 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2852 next;
2853 } else {
2854 # remove signoff lines
2855 next;
2857 } else {
2858 $signoff = 0;
2861 # print only one empty line
2862 # do not print empty line after signoff
2863 if ($line eq "") {
2864 next if ($empty || $signoff);
2865 $empty = 1;
2866 } else {
2867 $empty = 0;
2870 print format_log_line_html($line) . "<br/>\n";
2873 if ($opts{'-final_empty_line'}) {
2874 # end with single empty line
2875 print "<br/>\n" unless $empty;
2879 # return link target (what link points to)
2880 sub git_get_link_target {
2881 my $hash = shift;
2882 my $link_target;
2884 # read link
2885 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2886 or return;
2888 local $/;
2889 $link_target = <$fd>;
2891 close $fd
2892 or return;
2894 return $link_target;
2897 # given link target, and the directory (basedir) the link is in,
2898 # return target of link relative to top directory (top tree);
2899 # return undef if it is not possible (including absolute links).
2900 sub normalize_link_target {
2901 my ($link_target, $basedir, $hash_base) = @_;
2903 # we can normalize symlink target only if $hash_base is provided
2904 return unless $hash_base;
2906 # absolute symlinks (beginning with '/') cannot be normalized
2907 return if (substr($link_target, 0, 1) eq '/');
2909 # normalize link target to path from top (root) tree (dir)
2910 my $path;
2911 if ($basedir) {
2912 $path = $basedir . '/' . $link_target;
2913 } else {
2914 # we are in top (root) tree (dir)
2915 $path = $link_target;
2918 # remove //, /./, and /../
2919 my @path_parts;
2920 foreach my $part (split('/', $path)) {
2921 # discard '.' and ''
2922 next if (!$part || $part eq '.');
2923 # handle '..'
2924 if ($part eq '..') {
2925 if (@path_parts) {
2926 pop @path_parts;
2927 } else {
2928 # link leads outside repository (outside top dir)
2929 return;
2931 } else {
2932 push @path_parts, $part;
2935 $path = join('/', @path_parts);
2937 return $path;
2940 # print tree entry (row of git_tree), but without encompassing <tr> element
2941 sub git_print_tree_entry {
2942 my ($t, $basedir, $hash_base, $have_blame) = @_;
2944 my %base_key = ();
2945 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2947 # The format of a table row is: mode list link. Where mode is
2948 # the mode of the entry, list is the name of the entry, an href,
2949 # and link is the action links of the entry.
2951 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2952 if ($t->{'type'} eq "blob") {
2953 print "<td class=\"list\">" .
2954 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2955 file_name=>"$basedir$t->{'name'}", %base_key),
2956 -class => "list"}, esc_path($t->{'name'}));
2957 if (S_ISLNK(oct $t->{'mode'})) {
2958 my $link_target = git_get_link_target($t->{'hash'});
2959 if ($link_target) {
2960 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2961 if (defined $norm_target) {
2962 print " -> " .
2963 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2964 file_name=>$norm_target),
2965 -title => $norm_target}, esc_path($link_target));
2966 } else {
2967 print " -> " . esc_path($link_target);
2971 print "</td>\n";
2972 print "<td class=\"link\">";
2973 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2974 file_name=>"$basedir$t->{'name'}", %base_key)},
2975 "blob");
2976 if ($have_blame) {
2977 print " | " .
2978 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
2979 file_name=>"$basedir$t->{'name'}", %base_key)},
2980 "blame");
2982 if (defined $hash_base) {
2983 print " | " .
2984 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2985 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2986 "history");
2988 print " | " .
2989 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
2990 file_name=>"$basedir$t->{'name'}")},
2991 "raw");
2992 print "</td>\n";
2994 } elsif ($t->{'type'} eq "tree") {
2995 print "<td class=\"list\">";
2996 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
2997 file_name=>"$basedir$t->{'name'}", %base_key)},
2998 esc_path($t->{'name'}));
2999 print "</td>\n";
3000 print "<td class=\"link\">";
3001 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3002 file_name=>"$basedir$t->{'name'}", %base_key)},
3003 "tree");
3004 if (defined $hash_base) {
3005 print " | " .
3006 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3007 file_name=>"$basedir$t->{'name'}")},
3008 "history");
3010 print "</td>\n";
3011 } else {
3012 # unknown object: we can only present history for it
3013 # (this includes 'commit' object, i.e. submodule support)
3014 print "<td class=\"list\">" .
3015 esc_path($t->{'name'}) .
3016 "</td>\n";
3017 print "<td class=\"link\">";
3018 if (defined $hash_base) {
3019 print $cgi->a({-href => href(action=>"history",
3020 hash_base=>$hash_base,
3021 file_name=>"$basedir$t->{'name'}")},
3022 "history");
3024 print "</td>\n";
3028 ## ......................................................................
3029 ## functions printing large fragments of HTML
3031 # get pre-image filenames for merge (combined) diff
3032 sub fill_from_file_info {
3033 my ($diff, @parents) = @_;
3035 $diff->{'from_file'} = [ ];
3036 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3037 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3038 if ($diff->{'status'}[$i] eq 'R' ||
3039 $diff->{'status'}[$i] eq 'C') {
3040 $diff->{'from_file'}[$i] =
3041 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3045 return $diff;
3048 # is current raw difftree line of file deletion
3049 sub is_deleted {
3050 my $diffinfo = shift;
3052 return $diffinfo->{'to_id'} eq ('0' x 40);
3055 # does patch correspond to [previous] difftree raw line
3056 # $diffinfo - hashref of parsed raw diff format
3057 # $patchinfo - hashref of parsed patch diff format
3058 # (the same keys as in $diffinfo)
3059 sub is_patch_split {
3060 my ($diffinfo, $patchinfo) = @_;
3062 return defined $diffinfo && defined $patchinfo
3063 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3067 sub git_difftree_body {
3068 my ($difftree, $hash, @parents) = @_;
3069 my ($parent) = $parents[0];
3070 my ($have_blame) = gitweb_check_feature('blame');
3071 print "<div class=\"list_head\">\n";
3072 if ($#{$difftree} > 10) {
3073 print(($#{$difftree} + 1) . " files changed:\n");
3075 print "</div>\n";
3077 print "<table class=\"" .
3078 (@parents > 1 ? "combined " : "") .
3079 "diff_tree\">\n";
3081 # header only for combined diff in 'commitdiff' view
3082 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3083 if ($has_header) {
3084 # table header
3085 print "<thead><tr>\n" .
3086 "<th></th><th></th>\n"; # filename, patchN link
3087 for (my $i = 0; $i < @parents; $i++) {
3088 my $par = $parents[$i];
3089 print "<th>" .
3090 $cgi->a({-href => href(action=>"commitdiff",
3091 hash=>$hash, hash_parent=>$par),
3092 -title => 'commitdiff to parent number ' .
3093 ($i+1) . ': ' . substr($par,0,7)},
3094 $i+1) .
3095 "&nbsp;</th>\n";
3097 print "</tr></thead>\n<tbody>\n";
3100 my $alternate = 1;
3101 my $patchno = 0;
3102 foreach my $line (@{$difftree}) {
3103 my $diff = parsed_difftree_line($line);
3105 if ($alternate) {
3106 print "<tr class=\"dark\">\n";
3107 } else {
3108 print "<tr class=\"light\">\n";
3110 $alternate ^= 1;
3112 if (exists $diff->{'nparents'}) { # combined diff
3114 fill_from_file_info($diff, @parents)
3115 unless exists $diff->{'from_file'};
3117 if (!is_deleted($diff)) {
3118 # file exists in the result (child) commit
3119 print "<td>" .
3120 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3121 file_name=>$diff->{'to_file'},
3122 hash_base=>$hash),
3123 -class => "list"}, esc_path($diff->{'to_file'})) .
3124 "</td>\n";
3125 } else {
3126 print "<td>" .
3127 esc_path($diff->{'to_file'}) .
3128 "</td>\n";
3131 if ($action eq 'commitdiff') {
3132 # link to patch
3133 $patchno++;
3134 print "<td class=\"link\">" .
3135 $cgi->a({-href => "#patch$patchno"}, "patch") .
3136 " | " .
3137 "</td>\n";
3140 my $has_history = 0;
3141 my $not_deleted = 0;
3142 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3143 my $hash_parent = $parents[$i];
3144 my $from_hash = $diff->{'from_id'}[$i];
3145 my $from_path = $diff->{'from_file'}[$i];
3146 my $status = $diff->{'status'}[$i];
3148 $has_history ||= ($status ne 'A');
3149 $not_deleted ||= ($status ne 'D');
3151 if ($status eq 'A') {
3152 print "<td class=\"link\" align=\"right\"> | </td>\n";
3153 } elsif ($status eq 'D') {
3154 print "<td class=\"link\">" .
3155 $cgi->a({-href => href(action=>"blob",
3156 hash_base=>$hash,
3157 hash=>$from_hash,
3158 file_name=>$from_path)},
3159 "blob" . ($i+1)) .
3160 " | </td>\n";
3161 } else {
3162 if ($diff->{'to_id'} eq $from_hash) {
3163 print "<td class=\"link nochange\">";
3164 } else {
3165 print "<td class=\"link\">";
3167 print $cgi->a({-href => href(action=>"blobdiff",
3168 hash=>$diff->{'to_id'},
3169 hash_parent=>$from_hash,
3170 hash_base=>$hash,
3171 hash_parent_base=>$hash_parent,
3172 file_name=>$diff->{'to_file'},
3173 file_parent=>$from_path)},
3174 "diff" . ($i+1)) .
3175 " | </td>\n";
3179 print "<td class=\"link\">";
3180 if ($not_deleted) {
3181 print $cgi->a({-href => href(action=>"blob",
3182 hash=>$diff->{'to_id'},
3183 file_name=>$diff->{'to_file'},
3184 hash_base=>$hash)},
3185 "blob");
3186 print " | " if ($has_history);
3188 if ($has_history) {
3189 print $cgi->a({-href => href(action=>"history",
3190 file_name=>$diff->{'to_file'},
3191 hash_base=>$hash)},
3192 "history");
3194 print "</td>\n";
3196 print "</tr>\n";
3197 next; # instead of 'else' clause, to avoid extra indent
3199 # else ordinary diff
3201 my ($to_mode_oct, $to_mode_str, $to_file_type);
3202 my ($from_mode_oct, $from_mode_str, $from_file_type);
3203 if ($diff->{'to_mode'} ne ('0' x 6)) {
3204 $to_mode_oct = oct $diff->{'to_mode'};
3205 if (S_ISREG($to_mode_oct)) { # only for regular file
3206 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3208 $to_file_type = file_type($diff->{'to_mode'});
3210 if ($diff->{'from_mode'} ne ('0' x 6)) {
3211 $from_mode_oct = oct $diff->{'from_mode'};
3212 if (S_ISREG($to_mode_oct)) { # only for regular file
3213 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3215 $from_file_type = file_type($diff->{'from_mode'});
3218 if ($diff->{'status'} eq "A") { # created
3219 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3220 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3221 $mode_chng .= "]</span>";
3222 print "<td>";
3223 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3224 hash_base=>$hash, file_name=>$diff->{'file'}),
3225 -class => "list"}, esc_path($diff->{'file'}));
3226 print "</td>\n";
3227 print "<td>$mode_chng</td>\n";
3228 print "<td class=\"link\">";
3229 if ($action eq 'commitdiff') {
3230 # link to patch
3231 $patchno++;
3232 print $cgi->a({-href => "#patch$patchno"}, "patch");
3233 print " | ";
3235 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3236 hash_base=>$hash, file_name=>$diff->{'file'})},
3237 "blob");
3238 print "</td>\n";
3240 } elsif ($diff->{'status'} eq "D") { # deleted
3241 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3242 print "<td>";
3243 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3244 hash_base=>$parent, file_name=>$diff->{'file'}),
3245 -class => "list"}, esc_path($diff->{'file'}));
3246 print "</td>\n";
3247 print "<td>$mode_chng</td>\n";
3248 print "<td class=\"link\">";
3249 if ($action eq 'commitdiff') {
3250 # link to patch
3251 $patchno++;
3252 print $cgi->a({-href => "#patch$patchno"}, "patch");
3253 print " | ";
3255 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3256 hash_base=>$parent, file_name=>$diff->{'file'})},
3257 "blob") . " | ";
3258 if ($have_blame) {
3259 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3260 file_name=>$diff->{'file'})},
3261 "blame") . " | ";
3263 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3264 file_name=>$diff->{'file'})},
3265 "history");
3266 print "</td>\n";
3268 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3269 my $mode_chnge = "";
3270 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3271 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3272 if ($from_file_type ne $to_file_type) {
3273 $mode_chnge .= " from $from_file_type to $to_file_type";
3275 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3276 if ($from_mode_str && $to_mode_str) {
3277 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3278 } elsif ($to_mode_str) {
3279 $mode_chnge .= " mode: $to_mode_str";
3282 $mode_chnge .= "]</span>\n";
3284 print "<td>";
3285 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3286 hash_base=>$hash, file_name=>$diff->{'file'}),
3287 -class => "list"}, esc_path($diff->{'file'}));
3288 print "</td>\n";
3289 print "<td>$mode_chnge</td>\n";
3290 print "<td class=\"link\">";
3291 if ($action eq 'commitdiff') {
3292 # link to patch
3293 $patchno++;
3294 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3295 " | ";
3296 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3297 # "commit" view and modified file (not onlu mode changed)
3298 print $cgi->a({-href => href(action=>"blobdiff",
3299 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3300 hash_base=>$hash, hash_parent_base=>$parent,
3301 file_name=>$diff->{'file'})},
3302 "diff") .
3303 " | ";
3305 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3306 hash_base=>$hash, file_name=>$diff->{'file'})},
3307 "blob") . " | ";
3308 if ($have_blame) {
3309 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3310 file_name=>$diff->{'file'})},
3311 "blame") . " | ";
3313 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3314 file_name=>$diff->{'file'})},
3315 "history");
3316 print "</td>\n";
3318 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3319 my %status_name = ('R' => 'moved', 'C' => 'copied');
3320 my $nstatus = $status_name{$diff->{'status'}};
3321 my $mode_chng = "";
3322 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3323 # mode also for directories, so we cannot use $to_mode_str
3324 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3326 print "<td>" .
3327 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3328 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3329 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3330 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3331 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3332 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3333 -class => "list"}, esc_path($diff->{'from_file'})) .
3334 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3335 "<td class=\"link\">";
3336 if ($action eq 'commitdiff') {
3337 # link to patch
3338 $patchno++;
3339 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3340 " | ";
3341 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3342 # "commit" view and modified file (not only pure rename or copy)
3343 print $cgi->a({-href => href(action=>"blobdiff",
3344 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3345 hash_base=>$hash, hash_parent_base=>$parent,
3346 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3347 "diff") .
3348 " | ";
3350 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3351 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3352 "blob") . " | ";
3353 if ($have_blame) {
3354 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3355 file_name=>$diff->{'to_file'})},
3356 "blame") . " | ";
3358 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3359 file_name=>$diff->{'to_file'})},
3360 "history");
3361 print "</td>\n";
3363 } # we should not encounter Unmerged (U) or Unknown (X) status
3364 print "</tr>\n";
3366 print "</tbody>" if $has_header;
3367 print "</table>\n";
3370 sub git_patchset_body {
3371 my ($fd, $difftree, $hash, @hash_parents) = @_;
3372 my ($hash_parent) = $hash_parents[0];
3374 my $is_combined = (@hash_parents > 1);
3375 my $patch_idx = 0;
3376 my $patch_number = 0;
3377 my $patch_line;
3378 my $diffinfo;
3379 my $to_name;
3380 my (%from, %to);
3382 print "<div class=\"patchset\">\n";
3384 # skip to first patch
3385 while ($patch_line = <$fd>) {
3386 chomp $patch_line;
3388 last if ($patch_line =~ m/^diff /);
3391 PATCH:
3392 while ($patch_line) {
3394 # parse "git diff" header line
3395 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3396 # $1 is from_name, which we do not use
3397 $to_name = unquote($2);
3398 $to_name =~ s!^b/!!;
3399 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3400 # $1 is 'cc' or 'combined', which we do not use
3401 $to_name = unquote($2);
3402 } else {
3403 $to_name = undef;
3406 # check if current patch belong to current raw line
3407 # and parse raw git-diff line if needed
3408 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3409 # this is continuation of a split patch
3410 print "<div class=\"patch cont\">\n";
3411 } else {
3412 # advance raw git-diff output if needed
3413 $patch_idx++ if defined $diffinfo;
3415 # read and prepare patch information
3416 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3418 # compact combined diff output can have some patches skipped
3419 # find which patch (using pathname of result) we are at now;
3420 if ($is_combined) {
3421 while ($to_name ne $diffinfo->{'to_file'}) {
3422 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3423 format_diff_cc_simplified($diffinfo, @hash_parents) .
3424 "</div>\n"; # class="patch"
3426 $patch_idx++;
3427 $patch_number++;
3429 last if $patch_idx > $#$difftree;
3430 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3434 # modifies %from, %to hashes
3435 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3437 # this is first patch for raw difftree line with $patch_idx index
3438 # we index @$difftree array from 0, but number patches from 1
3439 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3442 # git diff header
3443 #assert($patch_line =~ m/^diff /) if DEBUG;
3444 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3445 $patch_number++;
3446 # print "git diff" header
3447 print format_git_diff_header_line($patch_line, $diffinfo,
3448 \%from, \%to);
3450 # print extended diff header
3451 print "<div class=\"diff extended_header\">\n";
3452 EXTENDED_HEADER:
3453 while ($patch_line = <$fd>) {
3454 chomp $patch_line;
3456 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3458 print format_extended_diff_header_line($patch_line, $diffinfo,
3459 \%from, \%to);
3461 print "</div>\n"; # class="diff extended_header"
3463 # from-file/to-file diff header
3464 if (! $patch_line) {
3465 print "</div>\n"; # class="patch"
3466 last PATCH;
3468 next PATCH if ($patch_line =~ m/^diff /);
3469 #assert($patch_line =~ m/^---/) if DEBUG;
3471 my $last_patch_line = $patch_line;
3472 $patch_line = <$fd>;
3473 chomp $patch_line;
3474 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3476 print format_diff_from_to_header($last_patch_line, $patch_line,
3477 $diffinfo, \%from, \%to,
3478 @hash_parents);
3480 # the patch itself
3481 LINE:
3482 while ($patch_line = <$fd>) {
3483 chomp $patch_line;
3485 next PATCH if ($patch_line =~ m/^diff /);
3487 print format_diff_line($patch_line, \%from, \%to);
3490 } continue {
3491 print "</div>\n"; # class="patch"
3494 # for compact combined (--cc) format, with chunk and patch simpliciaction
3495 # patchset might be empty, but there might be unprocessed raw lines
3496 for (++$patch_idx if $patch_number > 0;
3497 $patch_idx < @$difftree;
3498 ++$patch_idx) {
3499 # read and prepare patch information
3500 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3502 # generate anchor for "patch" links in difftree / whatchanged part
3503 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3504 format_diff_cc_simplified($diffinfo, @hash_parents) .
3505 "</div>\n"; # class="patch"
3507 $patch_number++;
3510 if ($patch_number == 0) {
3511 if (@hash_parents > 1) {
3512 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3513 } else {
3514 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3518 print "</div>\n"; # class="patchset"
3521 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3523 sub git_project_list_body {
3524 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3526 my ($check_forks) = gitweb_check_feature('forks');
3528 my @projects;
3529 foreach my $pr (@$projlist) {
3530 my (@aa) = git_get_last_activity($pr->{'path'});
3531 unless (@aa) {
3532 next;
3534 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
3535 if (!defined $pr->{'descr'}) {
3536 my $descr = git_get_project_description($pr->{'path'}) || "";
3537 $pr->{'descr_long'} = to_utf8($descr);
3538 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3540 if (!defined $pr->{'owner'}) {
3541 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3543 if ($check_forks) {
3544 my $pname = $pr->{'path'};
3545 if (($pname =~ s/\.git$//) &&
3546 ($pname !~ /\/$/) &&
3547 (-d "$projectroot/$pname")) {
3548 $pr->{'forks'} = "-d $projectroot/$pname";
3550 else {
3551 $pr->{'forks'} = 0;
3554 push @projects, $pr;
3557 $order ||= $default_projects_order;
3558 $from = 0 unless defined $from;
3559 $to = $#projects if (!defined $to || $#projects < $to);
3561 print "<table class=\"project_list\">\n";
3562 unless ($no_header) {
3563 print "<tr>\n";
3564 if ($check_forks) {
3565 print "<th></th>\n";
3567 if ($order eq "project") {
3568 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3569 print "<th>Project</th>\n";
3570 } else {
3571 print "<th>" .
3572 $cgi->a({-href => href(project=>undef, order=>'project'),
3573 -class => "header"}, "Project") .
3574 "</th>\n";
3576 if ($order eq "descr") {
3577 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3578 print "<th>Description</th>\n";
3579 } else {
3580 print "<th>" .
3581 $cgi->a({-href => href(project=>undef, order=>'descr'),
3582 -class => "header"}, "Description") .
3583 "</th>\n";
3585 if ($order eq "owner") {
3586 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3587 print "<th>Owner</th>\n";
3588 } else {
3589 print "<th>" .
3590 $cgi->a({-href => href(project=>undef, order=>'owner'),
3591 -class => "header"}, "Owner") .
3592 "</th>\n";
3594 if ($order eq "age") {
3595 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3596 print "<th>Last Change</th>\n";
3597 } else {
3598 print "<th>" .
3599 $cgi->a({-href => href(project=>undef, order=>'age'),
3600 -class => "header"}, "Last Change") .
3601 "</th>\n";
3603 print "<th></th>\n" .
3604 "</tr>\n";
3606 my $alternate = 1;
3607 for (my $i = $from; $i <= $to; $i++) {
3608 my $pr = $projects[$i];
3609 if ($alternate) {
3610 print "<tr class=\"dark\">\n";
3611 } else {
3612 print "<tr class=\"light\">\n";
3614 $alternate ^= 1;
3615 if ($check_forks) {
3616 print "<td>";
3617 if ($pr->{'forks'}) {
3618 print "<!-- $pr->{'forks'} -->\n";
3619 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3621 print "</td>\n";
3623 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3624 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3625 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3626 -class => "list", -title => $pr->{'descr_long'}},
3627 esc_html($pr->{'descr'})) . "</td>\n" .
3628 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3629 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3630 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3631 "<td class=\"link\">" .
3632 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3633 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3634 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3635 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3636 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3637 "</td>\n" .
3638 "</tr>\n";
3640 if (defined $extra) {
3641 print "<tr>\n";
3642 if ($check_forks) {
3643 print "<td></td>\n";
3645 print "<td colspan=\"5\">$extra</td>\n" .
3646 "</tr>\n";
3648 print "</table>\n";
3651 sub git_shortlog_body {
3652 # uses global variable $project
3653 my ($commitlist, $from, $to, $refs, $extra) = @_;
3655 $from = 0 unless defined $from;
3656 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3658 print "<table class=\"shortlog\">\n";
3659 my $alternate = 1;
3660 for (my $i = $from; $i <= $to; $i++) {
3661 my %co = %{$commitlist->[$i]};
3662 my $commit = $co{'id'};
3663 my $ref = format_ref_marker($refs, $commit);
3664 if ($alternate) {
3665 print "<tr class=\"dark\">\n";
3666 } else {
3667 print "<tr class=\"light\">\n";
3669 $alternate ^= 1;
3670 my $author = chop_and_escape_str($co{'author_name'}, 10);
3671 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3672 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3673 "<td><i>" . $author . "</i></td>\n" .
3674 "<td>";
3675 print format_subject_html($co{'title'}, $co{'title_short'},
3676 href(action=>"commit", hash=>$commit), $ref);
3677 print "</td>\n" .
3678 "<td class=\"link\">" .
3679 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3680 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3681 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3682 my $snapshot_links = format_snapshot_links($commit);
3683 if (defined $snapshot_links) {
3684 print " | " . $snapshot_links;
3686 print "</td>\n" .
3687 "</tr>\n";
3689 if (defined $extra) {
3690 print "<tr>\n" .
3691 "<td colspan=\"4\">$extra</td>\n" .
3692 "</tr>\n";
3694 print "</table>\n";
3697 sub git_history_body {
3698 # Warning: assumes constant type (blob or tree) during history
3699 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3701 $from = 0 unless defined $from;
3702 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3704 print "<table class=\"history\">\n";
3705 my $alternate = 1;
3706 for (my $i = $from; $i <= $to; $i++) {
3707 my %co = %{$commitlist->[$i]};
3708 if (!%co) {
3709 next;
3711 my $commit = $co{'id'};
3713 my $ref = format_ref_marker($refs, $commit);
3715 if ($alternate) {
3716 print "<tr class=\"dark\">\n";
3717 } else {
3718 print "<tr class=\"light\">\n";
3720 $alternate ^= 1;
3721 # shortlog uses chop_str($co{'author_name'}, 10)
3722 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3723 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3724 "<td><i>" . $author . "</i></td>\n" .
3725 "<td>";
3726 # originally git_history used chop_str($co{'title'}, 50)
3727 print format_subject_html($co{'title'}, $co{'title_short'},
3728 href(action=>"commit", hash=>$commit), $ref);
3729 print "</td>\n" .
3730 "<td class=\"link\">" .
3731 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3732 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3734 if ($ftype eq 'blob') {
3735 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3736 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3737 if (defined $blob_current && defined $blob_parent &&
3738 $blob_current ne $blob_parent) {
3739 print " | " .
3740 $cgi->a({-href => href(action=>"blobdiff",
3741 hash=>$blob_current, hash_parent=>$blob_parent,
3742 hash_base=>$hash_base, hash_parent_base=>$commit,
3743 file_name=>$file_name)},
3744 "diff to current");
3747 print "</td>\n" .
3748 "</tr>\n";
3750 if (defined $extra) {
3751 print "<tr>\n" .
3752 "<td colspan=\"4\">$extra</td>\n" .
3753 "</tr>\n";
3755 print "</table>\n";
3758 sub git_tags_body {
3759 # uses global variable $project
3760 my ($taglist, $from, $to, $extra) = @_;
3761 $from = 0 unless defined $from;
3762 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3764 print "<table class=\"tags\">\n";
3765 my $alternate = 1;
3766 for (my $i = $from; $i <= $to; $i++) {
3767 my $entry = $taglist->[$i];
3768 my %tag = %$entry;
3769 my $comment = $tag{'subject'};
3770 my $comment_short;
3771 if (defined $comment) {
3772 $comment_short = chop_str($comment, 30, 5);
3774 if ($alternate) {
3775 print "<tr class=\"dark\">\n";
3776 } else {
3777 print "<tr class=\"light\">\n";
3779 $alternate ^= 1;
3780 if (defined $tag{'age'}) {
3781 print "<td><i>$tag{'age'}</i></td>\n";
3782 } else {
3783 print "<td></td>\n";
3785 print "<td>" .
3786 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3787 -class => "list name"}, esc_html($tag{'name'})) .
3788 "</td>\n" .
3789 "<td>";
3790 if (defined $comment) {
3791 print format_subject_html($comment, $comment_short,
3792 href(action=>"tag", hash=>$tag{'id'}));
3794 print "</td>\n" .
3795 "<td class=\"selflink\">";
3796 if ($tag{'type'} eq "tag") {
3797 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3798 } else {
3799 print "&nbsp;";
3801 print "</td>\n" .
3802 "<td class=\"link\">" . " | " .
3803 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3804 if ($tag{'reftype'} eq "commit") {
3805 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
3806 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
3807 } elsif ($tag{'reftype'} eq "blob") {
3808 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3810 print "</td>\n" .
3811 "</tr>";
3813 if (defined $extra) {
3814 print "<tr>\n" .
3815 "<td colspan=\"5\">$extra</td>\n" .
3816 "</tr>\n";
3818 print "</table>\n";
3821 sub git_heads_body {
3822 # uses global variable $project
3823 my ($headlist, $head, $from, $to, $extra) = @_;
3824 $from = 0 unless defined $from;
3825 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3827 print "<table class=\"heads\">\n";
3828 my $alternate = 1;
3829 for (my $i = $from; $i <= $to; $i++) {
3830 my $entry = $headlist->[$i];
3831 my %ref = %$entry;
3832 my $curr = $ref{'id'} eq $head;
3833 if ($alternate) {
3834 print "<tr class=\"dark\">\n";
3835 } else {
3836 print "<tr class=\"light\">\n";
3838 $alternate ^= 1;
3839 print "<td><i>$ref{'age'}</i></td>\n" .
3840 ($curr ? "<td class=\"current_head\">" : "<td>") .
3841 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
3842 -class => "list name"},esc_html($ref{'name'})) .
3843 "</td>\n" .
3844 "<td class=\"link\">" .
3845 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
3846 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
3847 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
3848 "</td>\n" .
3849 "</tr>";
3851 if (defined $extra) {
3852 print "<tr>\n" .
3853 "<td colspan=\"3\">$extra</td>\n" .
3854 "</tr>\n";
3856 print "</table>\n";
3859 sub git_search_grep_body {
3860 my ($commitlist, $from, $to, $extra) = @_;
3861 $from = 0 unless defined $from;
3862 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3864 print "<table class=\"commit_search\">\n";
3865 my $alternate = 1;
3866 for (my $i = $from; $i <= $to; $i++) {
3867 my %co = %{$commitlist->[$i]};
3868 if (!%co) {
3869 next;
3871 my $commit = $co{'id'};
3872 if ($alternate) {
3873 print "<tr class=\"dark\">\n";
3874 } else {
3875 print "<tr class=\"light\">\n";
3877 $alternate ^= 1;
3878 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3879 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3880 "<td><i>" . $author . "</i></td>\n" .
3881 "<td>" .
3882 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3883 -class => "list subject"},
3884 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3885 my $comment = $co{'comment'};
3886 foreach my $line (@$comment) {
3887 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
3888 my ($lead, $match, $trail) = ($1, $2, $3);
3889 $match = chop_str($match, 70, 5, 'center');
3890 my $contextlen = int((80 - length($match))/2);
3891 $contextlen = 30 if ($contextlen > 30);
3892 $lead = chop_str($lead, $contextlen, 10, 'left');
3893 $trail = chop_str($trail, $contextlen, 10, 'right');
3895 $lead = esc_html($lead);
3896 $match = esc_html($match);
3897 $trail = esc_html($trail);
3899 print "$lead<span class=\"match\">$match</span>$trail<br />";
3902 print "</td>\n" .
3903 "<td class=\"link\">" .
3904 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3905 " | " .
3906 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
3907 " | " .
3908 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3909 print "</td>\n" .
3910 "</tr>\n";
3912 if (defined $extra) {
3913 print "<tr>\n" .
3914 "<td colspan=\"3\">$extra</td>\n" .
3915 "</tr>\n";
3917 print "</table>\n";
3920 ## ======================================================================
3921 ## ======================================================================
3922 ## actions
3924 sub git_project_list {
3925 my $order = $cgi->param('o');
3926 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3927 die_error(undef, "Unknown order parameter");
3930 my @list = git_get_projects_list();
3931 if (!@list) {
3932 die_error(undef, "No projects found");
3935 git_header_html();
3936 if (-f $home_text) {
3937 print "<div class=\"index_include\">\n";
3938 open (my $fd, $home_text);
3939 print <$fd>;
3940 close $fd;
3941 print "</div>\n";
3943 git_project_list_body(\@list, $order);
3944 git_footer_html();
3947 sub git_forks {
3948 my $order = $cgi->param('o');
3949 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3950 die_error(undef, "Unknown order parameter");
3953 my @list = git_get_projects_list($project);
3954 if (!@list) {
3955 die_error(undef, "No forks found");
3958 git_header_html();
3959 git_print_page_nav('','');
3960 git_print_header_div('summary', "$project forks");
3961 git_project_list_body(\@list, $order);
3962 git_footer_html();
3965 sub git_project_index {
3966 my @projects = git_get_projects_list($project);
3968 print $cgi->header(
3969 -type => 'text/plain',
3970 -charset => 'utf-8',
3971 -content_disposition => 'inline; filename="index.aux"');
3973 foreach my $pr (@projects) {
3974 if (!exists $pr->{'owner'}) {
3975 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
3978 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3979 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3980 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3981 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3982 $path =~ s/ /\+/g;
3983 $owner =~ s/ /\+/g;
3985 print "$path $owner\n";
3989 sub git_summary {
3990 my $descr = git_get_project_description($project) || "none";
3991 my %co = parse_commit("HEAD");
3992 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
3993 my $head = $co{'id'};
3995 my $owner = git_get_project_owner($project);
3997 my $refs = git_get_references();
3998 # These get_*_list functions return one more to allow us to see if
3999 # there are more ...
4000 my @taglist = git_get_tags_list(16);
4001 my @headlist = git_get_heads_list(16);
4002 my @forklist;
4003 my ($check_forks) = gitweb_check_feature('forks');
4005 if ($check_forks) {
4006 @forklist = git_get_projects_list($project);
4009 git_header_html();
4010 git_print_page_nav('summary','', $head);
4012 print "<div class=\"title\">&nbsp;</div>\n";
4013 print "<table class=\"projects_list\">\n" .
4014 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4015 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4016 if (defined $cd{'rfc2822'}) {
4017 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4020 # use per project git URL list in $projectroot/$project/cloneurl
4021 # or make project git URL from git base URL and project name
4022 my $url_tag = "URL";
4023 my @url_list = git_get_project_url_list($project);
4024 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4025 foreach my $git_url (@url_list) {
4026 next unless $git_url;
4027 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
4028 $url_tag = "";
4030 print "</table>\n";
4032 if (-s "$projectroot/$project/README.html") {
4033 if (open my $fd, "$projectroot/$project/README.html") {
4034 print "<div class=\"title\">readme</div>\n" .
4035 "<div class=\"readme\">\n";
4036 print $_ while (<$fd>);
4037 print "\n</div>\n"; # class="readme"
4038 close $fd;
4042 # we need to request one more than 16 (0..15) to check if
4043 # those 16 are all
4044 my @commitlist = $head ? parse_commits($head, 17) : ();
4045 if (@commitlist) {
4046 git_print_header_div('shortlog');
4047 git_shortlog_body(\@commitlist, 0, 15, $refs,
4048 $#commitlist <= 15 ? undef :
4049 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4052 if (@taglist) {
4053 git_print_header_div('tags');
4054 git_tags_body(\@taglist, 0, 15,
4055 $#taglist <= 15 ? undef :
4056 $cgi->a({-href => href(action=>"tags")}, "..."));
4059 if (@headlist) {
4060 git_print_header_div('heads');
4061 git_heads_body(\@headlist, $head, 0, 15,
4062 $#headlist <= 15 ? undef :
4063 $cgi->a({-href => href(action=>"heads")}, "..."));
4066 if (@forklist) {
4067 git_print_header_div('forks');
4068 git_project_list_body(\@forklist, undef, 0, 15,
4069 $#forklist <= 15 ? undef :
4070 $cgi->a({-href => href(action=>"forks")}, "..."),
4071 'noheader');
4074 git_footer_html();
4077 sub git_tag {
4078 my $head = git_get_head_hash($project);
4079 git_header_html();
4080 git_print_page_nav('','', $head,undef,$head);
4081 my %tag = parse_tag($hash);
4083 if (! %tag) {
4084 die_error(undef, "Unknown tag object");
4087 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4088 print "<div class=\"title_text\">\n" .
4089 "<table class=\"object_header\">\n" .
4090 "<tr>\n" .
4091 "<td>object</td>\n" .
4092 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4093 $tag{'object'}) . "</td>\n" .
4094 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4095 $tag{'type'}) . "</td>\n" .
4096 "</tr>\n";
4097 if (defined($tag{'author'})) {
4098 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4099 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4100 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4101 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4102 "</td></tr>\n";
4104 print "</table>\n\n" .
4105 "</div>\n";
4106 print "<div class=\"page_body\">";
4107 my $comment = $tag{'comment'};
4108 foreach my $line (@$comment) {
4109 chomp $line;
4110 print esc_html($line, -nbsp=>1) . "<br/>\n";
4112 print "</div>\n";
4113 git_footer_html();
4116 sub git_blame2 {
4117 my $fd;
4118 my $ftype;
4120 my ($have_blame) = gitweb_check_feature('blame');
4121 if (!$have_blame) {
4122 die_error('403 Permission denied', "Permission denied");
4124 die_error('404 Not Found', "File name not defined") if (!$file_name);
4125 $hash_base ||= git_get_head_hash($project);
4126 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4127 my %co = parse_commit($hash_base)
4128 or die_error(undef, "Reading commit failed");
4129 if (!defined $hash) {
4130 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4131 or die_error(undef, "Error looking up file");
4133 $ftype = git_get_type($hash);
4134 if ($ftype !~ "blob") {
4135 die_error('400 Bad Request', "Object is not a blob");
4137 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4138 $file_name, $hash_base)
4139 or die_error(undef, "Open git-blame failed");
4140 git_header_html();
4141 my $formats_nav =
4142 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4143 "blob") .
4144 " | " .
4145 $cgi->a({-href => href(action=>"history", -replay=>1)},
4146 "history") .
4147 " | " .
4148 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4149 "HEAD");
4150 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4151 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4152 git_print_page_path($file_name, $ftype, $hash_base);
4153 my @rev_color = (qw(light2 dark2));
4154 my $num_colors = scalar(@rev_color);
4155 my $current_color = 0;
4156 my $last_rev;
4157 print <<HTML;
4158 <div class="page_body">
4159 <table class="blame">
4160 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4161 HTML
4162 my %metainfo = ();
4163 while (1) {
4164 $_ = <$fd>;
4165 last unless defined $_;
4166 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4167 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4168 if (!exists $metainfo{$full_rev}) {
4169 $metainfo{$full_rev} = {};
4171 my $meta = $metainfo{$full_rev};
4172 while (<$fd>) {
4173 last if (s/^\t//);
4174 if (/^(\S+) (.*)$/) {
4175 $meta->{$1} = $2;
4178 my $data = $_;
4179 chomp $data;
4180 my $rev = substr($full_rev, 0, 8);
4181 my $author = $meta->{'author'};
4182 my %date = parse_date($meta->{'author-time'},
4183 $meta->{'author-tz'});
4184 my $date = $date{'iso-tz'};
4185 if ($group_size) {
4186 $current_color = ++$current_color % $num_colors;
4188 print "<tr class=\"$rev_color[$current_color]\">\n";
4189 if ($group_size) {
4190 print "<td class=\"sha1\"";
4191 print " title=\"". esc_html($author) . ", $date\"";
4192 print " rowspan=\"$group_size\"" if ($group_size > 1);
4193 print ">";
4194 print $cgi->a({-href => href(action=>"commit",
4195 hash=>$full_rev,
4196 file_name=>$file_name)},
4197 esc_html($rev));
4198 print "</td>\n";
4200 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4201 or die_error(undef, "Open git-rev-parse failed");
4202 my $parent_commit = <$dd>;
4203 close $dd;
4204 chomp($parent_commit);
4205 my $blamed = href(action => 'blame',
4206 file_name => $meta->{'filename'},
4207 hash_base => $parent_commit);
4208 print "<td class=\"linenr\">";
4209 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4210 -id => "l$lineno",
4211 -class => "linenr" },
4212 esc_html($lineno));
4213 print "</td>";
4214 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4215 print "</tr>\n";
4217 print "</table>\n";
4218 print "</div>";
4219 close $fd
4220 or print "Reading blob failed\n";
4221 git_footer_html();
4224 sub git_blame {
4225 my $fd;
4227 my ($have_blame) = gitweb_check_feature('blame');
4228 if (!$have_blame) {
4229 die_error('403 Permission denied', "Permission denied");
4231 die_error('404 Not Found', "File name not defined") if (!$file_name);
4232 $hash_base ||= git_get_head_hash($project);
4233 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4234 my %co = parse_commit($hash_base)
4235 or die_error(undef, "Reading commit failed");
4236 if (!defined $hash) {
4237 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4238 or die_error(undef, "Error lookup file");
4240 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
4241 or die_error(undef, "Open git-annotate failed");
4242 git_header_html();
4243 my $formats_nav =
4244 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4245 "blob") .
4246 " | " .
4247 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4248 "history") .
4249 " | " .
4250 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4251 "HEAD");
4252 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4253 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4254 git_print_page_path($file_name, 'blob', $hash_base);
4255 print "<div class=\"page_body\">\n";
4256 print <<HTML;
4257 <table class="blame">
4258 <tr>
4259 <th>Commit</th>
4260 <th>Age</th>
4261 <th>Author</th>
4262 <th>Line</th>
4263 <th>Data</th>
4264 </tr>
4265 HTML
4266 my @line_class = (qw(light dark));
4267 my $line_class_len = scalar (@line_class);
4268 my $line_class_num = $#line_class;
4269 while (my $line = <$fd>) {
4270 my $long_rev;
4271 my $short_rev;
4272 my $author;
4273 my $time;
4274 my $lineno;
4275 my $data;
4276 my $age;
4277 my $age_str;
4278 my $age_class;
4280 chomp $line;
4281 $line_class_num = ($line_class_num + 1) % $line_class_len;
4283 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
4284 $long_rev = $1;
4285 $author = $2;
4286 $time = $3;
4287 $lineno = $4;
4288 $data = $5;
4289 } else {
4290 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
4291 next;
4293 $short_rev = substr ($long_rev, 0, 8);
4294 $age = time () - $time;
4295 $age_str = age_string ($age);
4296 $age_str =~ s/ /&nbsp;/g;
4297 $age_class = age_class($age);
4298 $author = esc_html ($author);
4299 $author =~ s/ /&nbsp;/g;
4301 $data = untabify($data);
4302 $data = esc_html ($data);
4304 print <<HTML;
4305 <tr class="$line_class[$line_class_num]">
4306 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
4307 <td class="$age_class">$age_str</td>
4308 <td>$author</td>
4309 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4310 <td class="pre">$data</td>
4311 </tr>
4312 HTML
4313 } # while (my $line = <$fd>)
4314 print "</table>\n\n";
4315 close $fd
4316 or print "Reading blob failed.\n";
4317 print "</div>";
4318 git_footer_html();
4321 sub git_tags {
4322 my $head = git_get_head_hash($project);
4323 git_header_html();
4324 git_print_page_nav('','', $head,undef,$head);
4325 git_print_header_div('summary', $project);
4327 my @tagslist = git_get_tags_list();
4328 if (@tagslist) {
4329 git_tags_body(\@tagslist);
4331 git_footer_html();
4334 sub git_heads {
4335 my $head = git_get_head_hash($project);
4336 git_header_html();
4337 git_print_page_nav('','', $head,undef,$head);
4338 git_print_header_div('summary', $project);
4340 my @headslist = git_get_heads_list();
4341 if (@headslist) {
4342 git_heads_body(\@headslist, $head);
4344 git_footer_html();
4347 sub git_blob_plain {
4348 my $type = shift;
4349 my $expires;
4351 if (!defined $hash) {
4352 if (defined $file_name) {
4353 my $base = $hash_base || git_get_head_hash($project);
4354 $hash = git_get_hash_by_path($base, $file_name, "blob")
4355 or die_error(undef, "Error lookup file");
4356 } else {
4357 die_error(undef, "No file name defined");
4359 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4360 # blobs defined by non-textual hash id's can be cached
4361 $expires = "+1d";
4364 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4365 or die_error(undef, "Open git-cat-file blob '$hash' failed");
4367 # content-type (can include charset)
4368 $type = blob_contenttype($fd, $file_name, $type);
4370 # "save as" filename, even when no $file_name is given
4371 my $save_as = "$hash";
4372 if (defined $file_name) {
4373 $save_as = $file_name;
4374 } elsif ($type =~ m/^text\//) {
4375 $save_as .= '.txt';
4378 print $cgi->header(
4379 -type => $type,
4380 -expires => $expires,
4381 -content_disposition => 'inline; filename="' . $save_as . '"');
4382 undef $/;
4383 binmode STDOUT, ':raw';
4384 print <$fd>;
4385 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4386 $/ = "\n";
4387 close $fd;
4390 sub git_blob {
4391 my $expires;
4393 if (!defined $hash) {
4394 if (defined $file_name) {
4395 my $base = $hash_base || git_get_head_hash($project);
4396 $hash = git_get_hash_by_path($base, $file_name, "blob")
4397 or die_error(undef, "Error lookup file");
4398 } else {
4399 die_error(undef, "No file name defined");
4401 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4402 # blobs defined by non-textual hash id's can be cached
4403 $expires = "+1d";
4406 my ($have_blame) = gitweb_check_feature('blame');
4407 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4408 or die_error(undef, "Couldn't cat $file_name, $hash");
4409 my $mimetype = blob_mimetype($fd, $file_name);
4410 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4411 close $fd;
4412 return git_blob_plain($mimetype);
4414 # we can have blame only for text/* mimetype
4415 $have_blame &&= ($mimetype =~ m!^text/!);
4417 git_header_html(undef, $expires);
4418 my $formats_nav = '';
4419 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4420 if (defined $file_name) {
4421 if ($have_blame) {
4422 $formats_nav .=
4423 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4424 "blame") .
4425 " | ";
4427 $formats_nav .=
4428 $cgi->a({-href => href(action=>"history", -replay=>1)},
4429 "history") .
4430 " | " .
4431 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4432 "raw") .
4433 " | " .
4434 $cgi->a({-href => href(action=>"blob",
4435 hash_base=>"HEAD", file_name=>$file_name)},
4436 "HEAD");
4437 } else {
4438 $formats_nav .=
4439 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4440 "raw");
4442 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4443 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4444 } else {
4445 print "<div class=\"page_nav\">\n" .
4446 "<br/><br/></div>\n" .
4447 "<div class=\"title\">$hash</div>\n";
4449 git_print_page_path($file_name, "blob", $hash_base);
4450 print "<div class=\"page_body\">\n";
4451 if ($mimetype =~ m!^image/!) {
4452 print qq!<img type="$mimetype"!;
4453 if ($file_name) {
4454 print qq! alt="$file_name" title="$file_name"!;
4456 print qq! src="! .
4457 href(action=>"blob_plain", hash=>$hash,
4458 hash_base=>$hash_base, file_name=>$file_name) .
4459 qq!" />\n!;
4460 } else {
4461 my $nr;
4462 while (my $line = <$fd>) {
4463 chomp $line;
4464 $nr++;
4465 $line = untabify($line);
4466 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4467 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4470 close $fd
4471 or print "Reading blob failed.\n";
4472 print "</div>";
4473 git_footer_html();
4476 sub git_tree {
4477 if (!defined $hash_base) {
4478 $hash_base = "HEAD";
4480 if (!defined $hash) {
4481 if (defined $file_name) {
4482 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4483 } else {
4484 $hash = $hash_base;
4487 $/ = "\0";
4488 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4489 or die_error(undef, "Open git-ls-tree failed");
4490 my @entries = map { chomp; $_ } <$fd>;
4491 close $fd or die_error(undef, "Reading tree failed");
4492 $/ = "\n";
4494 my $refs = git_get_references();
4495 my $ref = format_ref_marker($refs, $hash_base);
4496 git_header_html();
4497 my $basedir = '';
4498 my ($have_blame) = gitweb_check_feature('blame');
4499 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4500 my @views_nav = ();
4501 if (defined $file_name) {
4502 push @views_nav,
4503 $cgi->a({-href => href(action=>"history", -replay=>1)},
4504 "history"),
4505 $cgi->a({-href => href(action=>"tree",
4506 hash_base=>"HEAD", file_name=>$file_name)},
4507 "HEAD"),
4509 my $snapshot_links = format_snapshot_links($hash);
4510 if (defined $snapshot_links) {
4511 # FIXME: Should be available when we have no hash base as well.
4512 push @views_nav, $snapshot_links;
4514 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4515 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4516 } else {
4517 undef $hash_base;
4518 print "<div class=\"page_nav\">\n";
4519 print "<br/><br/></div>\n";
4520 print "<div class=\"title\">$hash</div>\n";
4522 if (defined $file_name) {
4523 $basedir = $file_name;
4524 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4525 $basedir .= '/';
4528 git_print_page_path($file_name, 'tree', $hash_base);
4529 print "<div class=\"page_body\">\n";
4530 print "<table class=\"tree\">\n";
4531 my $alternate = 1;
4532 # '..' (top directory) link if possible
4533 if (defined $hash_base &&
4534 defined $file_name && $file_name =~ m![^/]+$!) {
4535 if ($alternate) {
4536 print "<tr class=\"dark\">\n";
4537 } else {
4538 print "<tr class=\"light\">\n";
4540 $alternate ^= 1;
4542 my $up = $file_name;
4543 $up =~ s!/?[^/]+$!!;
4544 undef $up unless $up;
4545 # based on git_print_tree_entry
4546 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4547 print '<td class="list">';
4548 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4549 file_name=>$up)},
4550 "..");
4551 print "</td>\n";
4552 print "<td class=\"link\"></td>\n";
4554 print "</tr>\n";
4556 foreach my $line (@entries) {
4557 my %t = parse_ls_tree_line($line, -z => 1);
4559 if ($alternate) {
4560 print "<tr class=\"dark\">\n";
4561 } else {
4562 print "<tr class=\"light\">\n";
4564 $alternate ^= 1;
4566 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4568 print "</tr>\n";
4570 print "</table>\n" .
4571 "</div>";
4572 git_footer_html();
4575 sub git_snapshot {
4576 my @supported_fmts = gitweb_check_feature('snapshot');
4577 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4579 my $format = $cgi->param('sf');
4580 if (!@supported_fmts) {
4581 die_error('403 Permission denied', "Permission denied");
4583 # default to first supported snapshot format
4584 $format ||= $supported_fmts[0];
4585 if ($format !~ m/^[a-z0-9]+$/) {
4586 die_error(undef, "Invalid snapshot format parameter");
4587 } elsif (!exists($known_snapshot_formats{$format})) {
4588 die_error(undef, "Unknown snapshot format");
4589 } elsif (!grep($_ eq $format, @supported_fmts)) {
4590 die_error(undef, "Unsupported snapshot format");
4593 if (!defined $hash) {
4594 $hash = git_get_head_hash($project);
4597 my $name = $project;
4598 $name =~ s,([^/])/*\.git$,$1,;
4599 $name = basename($name);
4600 my $filename = to_utf8($name);
4601 $name =~ s/\047/\047\\\047\047/g;
4602 my $cmd;
4603 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4604 $cmd = quote_command(
4605 git_cmd(), 'archive',
4606 "--format=$known_snapshot_formats{$format}{'format'}",
4607 "--prefix=$name/", $hash);
4608 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4609 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
4612 print $cgi->header(
4613 -type => $known_snapshot_formats{$format}{'type'},
4614 -content_disposition => 'inline; filename="' . "$filename" . '"',
4615 -status => '200 OK');
4617 open my $fd, "-|", $cmd
4618 or die_error(undef, "Execute git-archive failed");
4619 binmode STDOUT, ':raw';
4620 print <$fd>;
4621 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4622 close $fd;
4625 sub git_log {
4626 my $head = git_get_head_hash($project);
4627 if (!defined $hash) {
4628 $hash = $head;
4630 if (!defined $page) {
4631 $page = 0;
4633 my $refs = git_get_references();
4635 my @commitlist = parse_commits($hash, 101, (100 * $page));
4637 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
4639 git_header_html();
4640 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4642 if (!@commitlist) {
4643 my %co = parse_commit($hash);
4645 git_print_header_div('summary', $project);
4646 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4648 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4649 for (my $i = 0; $i <= $to; $i++) {
4650 my %co = %{$commitlist[$i]};
4651 next if !%co;
4652 my $commit = $co{'id'};
4653 my $ref = format_ref_marker($refs, $commit);
4654 my %ad = parse_date($co{'author_epoch'});
4655 git_print_header_div('commit',
4656 "<span class=\"age\">$co{'age_string'}</span>" .
4657 esc_html($co{'title'}) . $ref,
4658 $commit);
4659 print "<div class=\"title_text\">\n" .
4660 "<div class=\"log_link\">\n" .
4661 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4662 " | " .
4663 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4664 " | " .
4665 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4666 "<br/>\n" .
4667 "</div>\n" .
4668 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4669 "</div>\n";
4671 print "<div class=\"log_body\">\n";
4672 git_print_log($co{'comment'}, -final_empty_line=> 1);
4673 print "</div>\n";
4675 if ($#commitlist >= 100) {
4676 print "<div class=\"page_nav\">\n";
4677 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4678 -accesskey => "n", -title => "Alt-n"}, "next");
4679 print "</div>\n";
4681 git_footer_html();
4684 sub git_commit {
4685 $hash ||= $hash_base || "HEAD";
4686 my %co = parse_commit($hash);
4687 if (!%co) {
4688 die_error(undef, "Unknown commit object");
4690 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4691 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4693 my $parent = $co{'parent'};
4694 my $parents = $co{'parents'}; # listref
4696 # we need to prepare $formats_nav before any parameter munging
4697 my $formats_nav;
4698 if (!defined $parent) {
4699 # --root commitdiff
4700 $formats_nav .= '(initial)';
4701 } elsif (@$parents == 1) {
4702 # single parent commit
4703 $formats_nav .=
4704 '(parent: ' .
4705 $cgi->a({-href => href(action=>"commit",
4706 hash=>$parent)},
4707 esc_html(substr($parent, 0, 7))) .
4708 ')';
4709 } else {
4710 # merge commit
4711 $formats_nav .=
4712 '(merge: ' .
4713 join(' ', map {
4714 $cgi->a({-href => href(action=>"commit",
4715 hash=>$_)},
4716 esc_html(substr($_, 0, 7)));
4717 } @$parents ) .
4718 ')';
4721 if (!defined $parent) {
4722 $parent = "--root";
4724 my @difftree;
4725 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4726 @diff_opts,
4727 (@$parents <= 1 ? $parent : '-c'),
4728 $hash, "--"
4729 or die_error(undef, "Open git-diff-tree failed");
4730 @difftree = map { chomp; $_ } <$fd>;
4731 close $fd or die_error(undef, "Reading git-diff-tree failed");
4733 # non-textual hash id's can be cached
4734 my $expires;
4735 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4736 $expires = "+1d";
4738 my $refs = git_get_references();
4739 my $ref = format_ref_marker($refs, $co{'id'});
4741 git_header_html(undef, $expires);
4742 git_print_page_nav('commit', '',
4743 $hash, $co{'tree'}, $hash,
4744 $formats_nav);
4746 if (defined $co{'parent'}) {
4747 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4748 } else {
4749 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4751 print "<div class=\"title_text\">\n" .
4752 "<table class=\"object_header\">\n";
4753 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4754 "<tr>" .
4755 "<td></td><td> $ad{'rfc2822'}";
4756 if ($ad{'hour_local'} < 6) {
4757 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4758 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4759 } else {
4760 printf(" (%02d:%02d %s)",
4761 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4763 print "</td>" .
4764 "</tr>\n";
4765 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4766 print "<tr><td></td><td> $cd{'rfc2822'}" .
4767 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4768 "</td></tr>\n";
4769 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4770 print "<tr>" .
4771 "<td>tree</td>" .
4772 "<td class=\"sha1\">" .
4773 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4774 class => "list"}, $co{'tree'}) .
4775 "</td>" .
4776 "<td class=\"link\">" .
4777 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4778 "tree");
4779 my $snapshot_links = format_snapshot_links($hash);
4780 if (defined $snapshot_links) {
4781 print " | " . $snapshot_links;
4783 print "</td>" .
4784 "</tr>\n";
4786 foreach my $par (@$parents) {
4787 print "<tr>" .
4788 "<td>parent</td>" .
4789 "<td class=\"sha1\">" .
4790 $cgi->a({-href => href(action=>"commit", hash=>$par),
4791 class => "list"}, $par) .
4792 "</td>" .
4793 "<td class=\"link\">" .
4794 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4795 " | " .
4796 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4797 "</td>" .
4798 "</tr>\n";
4800 print "</table>".
4801 "</div>\n";
4803 print "<div class=\"page_body\">\n";
4804 git_print_log($co{'comment'});
4805 print "</div>\n";
4807 git_difftree_body(\@difftree, $hash, @$parents);
4809 git_footer_html();
4812 sub git_object {
4813 # object is defined by:
4814 # - hash or hash_base alone
4815 # - hash_base and file_name
4816 my $type;
4818 # - hash or hash_base alone
4819 if ($hash || ($hash_base && !defined $file_name)) {
4820 my $object_id = $hash || $hash_base;
4822 open my $fd, "-|", quote_command(
4823 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
4824 or die_error('404 Not Found', "Object does not exist");
4825 $type = <$fd>;
4826 chomp $type;
4827 close $fd
4828 or die_error('404 Not Found', "Object does not exist");
4830 # - hash_base and file_name
4831 } elsif ($hash_base && defined $file_name) {
4832 $file_name =~ s,/+$,,;
4834 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4835 or die_error('404 Not Found', "Base object does not exist");
4837 # here errors should not hapen
4838 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4839 or die_error(undef, "Open git-ls-tree failed");
4840 my $line = <$fd>;
4841 close $fd;
4843 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4844 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4845 die_error('404 Not Found', "File or directory for given base does not exist");
4847 $type = $2;
4848 $hash = $3;
4849 } else {
4850 die_error('404 Not Found', "Not enough information to find object");
4853 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4854 hash=>$hash, hash_base=>$hash_base,
4855 file_name=>$file_name),
4856 -status => '302 Found');
4859 sub git_blobdiff {
4860 my $format = shift || 'html';
4862 my $fd;
4863 my @difftree;
4864 my %diffinfo;
4865 my $expires;
4867 # preparing $fd and %diffinfo for git_patchset_body
4868 # new style URI
4869 if (defined $hash_base && defined $hash_parent_base) {
4870 if (defined $file_name) {
4871 # read raw output
4872 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4873 $hash_parent_base, $hash_base,
4874 "--", (defined $file_parent ? $file_parent : ()), $file_name
4875 or die_error(undef, "Open git-diff-tree failed");
4876 @difftree = map { chomp; $_ } <$fd>;
4877 close $fd
4878 or die_error(undef, "Reading git-diff-tree failed");
4879 @difftree
4880 or die_error('404 Not Found', "Blob diff not found");
4882 } elsif (defined $hash &&
4883 $hash =~ /[0-9a-fA-F]{40}/) {
4884 # try to find filename from $hash
4886 # read filtered raw output
4887 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4888 $hash_parent_base, $hash_base, "--"
4889 or die_error(undef, "Open git-diff-tree failed");
4890 @difftree =
4891 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4892 # $hash == to_id
4893 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4894 map { chomp; $_ } <$fd>;
4895 close $fd
4896 or die_error(undef, "Reading git-diff-tree failed");
4897 @difftree
4898 or die_error('404 Not Found', "Blob diff not found");
4900 } else {
4901 die_error('404 Not Found', "Missing one of the blob diff parameters");
4904 if (@difftree > 1) {
4905 die_error('404 Not Found', "Ambiguous blob diff specification");
4908 %diffinfo = parse_difftree_raw_line($difftree[0]);
4909 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4910 $file_name ||= $diffinfo{'to_file'};
4912 $hash_parent ||= $diffinfo{'from_id'};
4913 $hash ||= $diffinfo{'to_id'};
4915 # non-textual hash id's can be cached
4916 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4917 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4918 $expires = '+1d';
4921 # open patch output
4922 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4923 '-p', ($format eq 'html' ? "--full-index" : ()),
4924 $hash_parent_base, $hash_base,
4925 "--", (defined $file_parent ? $file_parent : ()), $file_name
4926 or die_error(undef, "Open git-diff-tree failed");
4929 # old/legacy style URI
4930 if (!%diffinfo && # if new style URI failed
4931 defined $hash && defined $hash_parent) {
4932 # fake git-diff-tree raw output
4933 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4934 $diffinfo{'from_id'} = $hash_parent;
4935 $diffinfo{'to_id'} = $hash;
4936 if (defined $file_name) {
4937 if (defined $file_parent) {
4938 $diffinfo{'status'} = '2';
4939 $diffinfo{'from_file'} = $file_parent;
4940 $diffinfo{'to_file'} = $file_name;
4941 } else { # assume not renamed
4942 $diffinfo{'status'} = '1';
4943 $diffinfo{'from_file'} = $file_name;
4944 $diffinfo{'to_file'} = $file_name;
4946 } else { # no filename given
4947 $diffinfo{'status'} = '2';
4948 $diffinfo{'from_file'} = $hash_parent;
4949 $diffinfo{'to_file'} = $hash;
4952 # non-textual hash id's can be cached
4953 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4954 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4955 $expires = '+1d';
4958 # open patch output
4959 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4960 '-p', ($format eq 'html' ? "--full-index" : ()),
4961 $hash_parent, $hash, "--"
4962 or die_error(undef, "Open git-diff failed");
4963 } else {
4964 die_error('404 Not Found', "Missing one of the blob diff parameters")
4965 unless %diffinfo;
4968 # header
4969 if ($format eq 'html') {
4970 my $formats_nav =
4971 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
4972 "raw");
4973 git_header_html(undef, $expires);
4974 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4975 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4976 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4977 } else {
4978 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4979 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4981 if (defined $file_name) {
4982 git_print_page_path($file_name, "blob", $hash_base);
4983 } else {
4984 print "<div class=\"page_path\"></div>\n";
4987 } elsif ($format eq 'plain') {
4988 print $cgi->header(
4989 -type => 'text/plain',
4990 -charset => 'utf-8',
4991 -expires => $expires,
4992 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4994 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4996 } else {
4997 die_error(undef, "Unknown blobdiff format");
5000 # patch
5001 if ($format eq 'html') {
5002 print "<div class=\"page_body\">\n";
5004 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5005 close $fd;
5007 print "</div>\n"; # class="page_body"
5008 git_footer_html();
5010 } else {
5011 while (my $line = <$fd>) {
5012 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5013 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5015 print $line;
5017 last if $line =~ m!^\+\+\+!;
5019 local $/ = undef;
5020 print <$fd>;
5021 close $fd;
5025 sub git_blobdiff_plain {
5026 git_blobdiff('plain');
5029 sub git_commitdiff {
5030 my $format = shift || 'html';
5031 $hash ||= $hash_base || "HEAD";
5032 my %co = parse_commit($hash);
5033 if (!%co) {
5034 die_error(undef, "Unknown commit object");
5037 # choose format for commitdiff for merge
5038 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5039 $hash_parent = '--cc';
5041 # we need to prepare $formats_nav before almost any parameter munging
5042 my $formats_nav;
5043 if ($format eq 'html') {
5044 $formats_nav =
5045 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5046 "raw");
5048 if (defined $hash_parent &&
5049 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5050 # commitdiff with two commits given
5051 my $hash_parent_short = $hash_parent;
5052 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5053 $hash_parent_short = substr($hash_parent, 0, 7);
5055 $formats_nav .=
5056 ' (from';
5057 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5058 if ($co{'parents'}[$i] eq $hash_parent) {
5059 $formats_nav .= ' parent ' . ($i+1);
5060 last;
5063 $formats_nav .= ': ' .
5064 $cgi->a({-href => href(action=>"commitdiff",
5065 hash=>$hash_parent)},
5066 esc_html($hash_parent_short)) .
5067 ')';
5068 } elsif (!$co{'parent'}) {
5069 # --root commitdiff
5070 $formats_nav .= ' (initial)';
5071 } elsif (scalar @{$co{'parents'}} == 1) {
5072 # single parent commit
5073 $formats_nav .=
5074 ' (parent: ' .
5075 $cgi->a({-href => href(action=>"commitdiff",
5076 hash=>$co{'parent'})},
5077 esc_html(substr($co{'parent'}, 0, 7))) .
5078 ')';
5079 } else {
5080 # merge commit
5081 if ($hash_parent eq '--cc') {
5082 $formats_nav .= ' | ' .
5083 $cgi->a({-href => href(action=>"commitdiff",
5084 hash=>$hash, hash_parent=>'-c')},
5085 'combined');
5086 } else { # $hash_parent eq '-c'
5087 $formats_nav .= ' | ' .
5088 $cgi->a({-href => href(action=>"commitdiff",
5089 hash=>$hash, hash_parent=>'--cc')},
5090 'compact');
5092 $formats_nav .=
5093 ' (merge: ' .
5094 join(' ', map {
5095 $cgi->a({-href => href(action=>"commitdiff",
5096 hash=>$_)},
5097 esc_html(substr($_, 0, 7)));
5098 } @{$co{'parents'}} ) .
5099 ')';
5103 my $hash_parent_param = $hash_parent;
5104 if (!defined $hash_parent_param) {
5105 # --cc for multiple parents, --root for parentless
5106 $hash_parent_param =
5107 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5110 # read commitdiff
5111 my $fd;
5112 my @difftree;
5113 if ($format eq 'html') {
5114 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5115 "--no-commit-id", "--patch-with-raw", "--full-index",
5116 $hash_parent_param, $hash, "--"
5117 or die_error(undef, "Open git-diff-tree failed");
5119 while (my $line = <$fd>) {
5120 chomp $line;
5121 # empty line ends raw part of diff-tree output
5122 last unless $line;
5123 push @difftree, scalar parse_difftree_raw_line($line);
5126 } elsif ($format eq 'plain') {
5127 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5128 '-p', $hash_parent_param, $hash, "--"
5129 or die_error(undef, "Open git-diff-tree failed");
5131 } else {
5132 die_error(undef, "Unknown commitdiff format");
5135 # non-textual hash id's can be cached
5136 my $expires;
5137 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5138 $expires = "+1d";
5141 # write commit message
5142 if ($format eq 'html') {
5143 my $refs = git_get_references();
5144 my $ref = format_ref_marker($refs, $co{'id'});
5146 git_header_html(undef, $expires);
5147 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5148 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5149 git_print_authorship(\%co);
5150 print "<div class=\"page_body\">\n";
5151 if (@{$co{'comment'}} > 1) {
5152 print "<div class=\"log\">\n";
5153 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5154 print "</div>\n"; # class="log"
5157 } elsif ($format eq 'plain') {
5158 my $refs = git_get_references("tags");
5159 my $tagname = git_get_rev_name_tags($hash);
5160 my $filename = basename($project) . "-$hash.patch";
5162 print $cgi->header(
5163 -type => 'text/plain',
5164 -charset => 'utf-8',
5165 -expires => $expires,
5166 -content_disposition => 'inline; filename="' . "$filename" . '"');
5167 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5168 print "From: " . to_utf8($co{'author'}) . "\n";
5169 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5170 print "Subject: " . to_utf8($co{'title'}) . "\n";
5172 print "X-Git-Tag: $tagname\n" if $tagname;
5173 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5175 foreach my $line (@{$co{'comment'}}) {
5176 print to_utf8($line) . "\n";
5178 print "---\n\n";
5181 # write patch
5182 if ($format eq 'html') {
5183 my $use_parents = !defined $hash_parent ||
5184 $hash_parent eq '-c' || $hash_parent eq '--cc';
5185 git_difftree_body(\@difftree, $hash,
5186 $use_parents ? @{$co{'parents'}} : $hash_parent);
5187 print "<br/>\n";
5189 git_patchset_body($fd, \@difftree, $hash,
5190 $use_parents ? @{$co{'parents'}} : $hash_parent);
5191 close $fd;
5192 print "</div>\n"; # class="page_body"
5193 git_footer_html();
5195 } elsif ($format eq 'plain') {
5196 local $/ = undef;
5197 print <$fd>;
5198 close $fd
5199 or print "Reading git-diff-tree failed\n";
5203 sub git_commitdiff_plain {
5204 git_commitdiff('plain');
5207 sub git_history {
5208 if (!defined $hash_base) {
5209 $hash_base = git_get_head_hash($project);
5211 if (!defined $page) {
5212 $page = 0;
5214 my $ftype;
5215 my %co = parse_commit($hash_base);
5216 if (!%co) {
5217 die_error(undef, "Unknown commit object");
5220 my $refs = git_get_references();
5221 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5223 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5224 $file_name, "--full-history");
5225 if (!@commitlist) {
5226 die_error('404 Not Found', "No such file or directory on given branch");
5229 if (!defined $hash && defined $file_name) {
5230 # some commits could have deleted file in question,
5231 # and not have it in tree, but one of them has to have it
5232 for (my $i = 0; $i <= @commitlist; $i++) {
5233 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5234 last if defined $hash;
5237 if (defined $hash) {
5238 $ftype = git_get_type($hash);
5240 if (!defined $ftype) {
5241 die_error(undef, "Unknown type of object");
5244 my $paging_nav = '';
5245 if ($page > 0) {
5246 $paging_nav .=
5247 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5248 file_name=>$file_name)},
5249 "first");
5250 $paging_nav .= " &sdot; " .
5251 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5252 -accesskey => "p", -title => "Alt-p"}, "prev");
5253 } else {
5254 $paging_nav .= "first";
5255 $paging_nav .= " &sdot; prev";
5257 my $next_link = '';
5258 if ($#commitlist >= 100) {
5259 $next_link =
5260 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5261 -accesskey => "n", -title => "Alt-n"}, "next");
5262 $paging_nav .= " &sdot; $next_link";
5263 } else {
5264 $paging_nav .= " &sdot; next";
5267 git_header_html();
5268 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5269 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5270 git_print_page_path($file_name, $ftype, $hash_base);
5272 git_history_body(\@commitlist, 0, 99,
5273 $refs, $hash_base, $ftype, $next_link);
5275 git_footer_html();
5278 sub git_search {
5279 my ($have_search) = gitweb_check_feature('search');
5280 if (!$have_search) {
5281 die_error('403 Permission denied', "Permission denied");
5283 if (!defined $searchtext) {
5284 die_error(undef, "Text field empty");
5286 if (!defined $hash) {
5287 $hash = git_get_head_hash($project);
5289 my %co = parse_commit($hash);
5290 if (!%co) {
5291 die_error(undef, "Unknown commit object");
5293 if (!defined $page) {
5294 $page = 0;
5297 $searchtype ||= 'commit';
5298 if ($searchtype eq 'pickaxe') {
5299 # pickaxe may take all resources of your box and run for several minutes
5300 # with every query - so decide by yourself how public you make this feature
5301 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5302 if (!$have_pickaxe) {
5303 die_error('403 Permission denied', "Permission denied");
5306 if ($searchtype eq 'grep') {
5307 my ($have_grep) = gitweb_check_feature('grep');
5308 if (!$have_grep) {
5309 die_error('403 Permission denied', "Permission denied");
5313 git_header_html();
5315 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5316 my $greptype;
5317 if ($searchtype eq 'commit') {
5318 $greptype = "--grep=";
5319 } elsif ($searchtype eq 'author') {
5320 $greptype = "--author=";
5321 } elsif ($searchtype eq 'committer') {
5322 $greptype = "--committer=";
5324 $greptype .= $searchtext;
5325 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5326 $greptype, '--regexp-ignore-case',
5327 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5329 my $paging_nav = '';
5330 if ($page > 0) {
5331 $paging_nav .=
5332 $cgi->a({-href => href(action=>"search", hash=>$hash,
5333 searchtext=>$searchtext,
5334 searchtype=>$searchtype)},
5335 "first");
5336 $paging_nav .= " &sdot; " .
5337 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5338 -accesskey => "p", -title => "Alt-p"}, "prev");
5339 } else {
5340 $paging_nav .= "first";
5341 $paging_nav .= " &sdot; prev";
5343 my $next_link = '';
5344 if ($#commitlist >= 100) {
5345 $next_link =
5346 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5347 -accesskey => "n", -title => "Alt-n"}, "next");
5348 $paging_nav .= " &sdot; $next_link";
5349 } else {
5350 $paging_nav .= " &sdot; next";
5353 if ($#commitlist >= 100) {
5356 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5357 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5358 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5361 if ($searchtype eq 'pickaxe') {
5362 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5363 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5365 print "<table class=\"pickaxe search\">\n";
5366 my $alternate = 1;
5367 $/ = "\n";
5368 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5369 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5370 ($search_use_regexp ? '--pickaxe-regex' : ());
5371 undef %co;
5372 my @files;
5373 while (my $line = <$fd>) {
5374 chomp $line;
5375 next unless $line;
5377 my %set = parse_difftree_raw_line($line);
5378 if (defined $set{'commit'}) {
5379 # finish previous commit
5380 if (%co) {
5381 print "</td>\n" .
5382 "<td class=\"link\">" .
5383 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5384 " | " .
5385 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5386 print "</td>\n" .
5387 "</tr>\n";
5390 if ($alternate) {
5391 print "<tr class=\"dark\">\n";
5392 } else {
5393 print "<tr class=\"light\">\n";
5395 $alternate ^= 1;
5396 %co = parse_commit($set{'commit'});
5397 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5398 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5399 "<td><i>$author</i></td>\n" .
5400 "<td>" .
5401 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5402 -class => "list subject"},
5403 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5404 } elsif (defined $set{'to_id'}) {
5405 next if ($set{'to_id'} =~ m/^0{40}$/);
5407 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5408 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5409 -class => "list"},
5410 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5411 "<br/>\n";
5414 close $fd;
5416 # finish last commit (warning: repetition!)
5417 if (%co) {
5418 print "</td>\n" .
5419 "<td class=\"link\">" .
5420 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5421 " | " .
5422 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5423 print "</td>\n" .
5424 "</tr>\n";
5427 print "</table>\n";
5430 if ($searchtype eq 'grep') {
5431 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5432 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5434 print "<table class=\"grep_search\">\n";
5435 my $alternate = 1;
5436 my $matches = 0;
5437 $/ = "\n";
5438 open my $fd, "-|", git_cmd(), 'grep', '-n',
5439 $search_use_regexp ? ('-E', '-i') : '-F',
5440 $searchtext, $co{'tree'};
5441 my $lastfile = '';
5442 while (my $line = <$fd>) {
5443 chomp $line;
5444 my ($file, $lno, $ltext, $binary);
5445 last if ($matches++ > 1000);
5446 if ($line =~ /^Binary file (.+) matches$/) {
5447 $file = $1;
5448 $binary = 1;
5449 } else {
5450 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5452 if ($file ne $lastfile) {
5453 $lastfile and print "</td></tr>\n";
5454 if ($alternate++) {
5455 print "<tr class=\"dark\">\n";
5456 } else {
5457 print "<tr class=\"light\">\n";
5459 print "<td class=\"list\">".
5460 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5461 file_name=>"$file"),
5462 -class => "list"}, esc_path($file));
5463 print "</td><td>\n";
5464 $lastfile = $file;
5466 if ($binary) {
5467 print "<div class=\"binary\">Binary file</div>\n";
5468 } else {
5469 $ltext = untabify($ltext);
5470 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5471 $ltext = esc_html($1, -nbsp=>1);
5472 $ltext .= '<span class="match">';
5473 $ltext .= esc_html($2, -nbsp=>1);
5474 $ltext .= '</span>';
5475 $ltext .= esc_html($3, -nbsp=>1);
5476 } else {
5477 $ltext = esc_html($ltext, -nbsp=>1);
5479 print "<div class=\"pre\">" .
5480 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5481 file_name=>"$file").'#l'.$lno,
5482 -class => "linenr"}, sprintf('%4i', $lno))
5483 . ' ' . $ltext . "</div>\n";
5486 if ($lastfile) {
5487 print "</td></tr>\n";
5488 if ($matches > 1000) {
5489 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5491 } else {
5492 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5494 close $fd;
5496 print "</table>\n";
5498 git_footer_html();
5501 sub git_search_help {
5502 git_header_html();
5503 git_print_page_nav('','', $hash,$hash,$hash);
5504 print <<EOT;
5505 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5506 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5507 the pattern entered is recognized as the POSIX extended
5508 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5509 insensitive).</p>
5510 <dl>
5511 <dt><b>commit</b></dt>
5512 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5514 my ($have_grep) = gitweb_check_feature('grep');
5515 if ($have_grep) {
5516 print <<EOT;
5517 <dt><b>grep</b></dt>
5518 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5519 a different one) are searched for the given pattern. On large trees, this search can take
5520 a while and put some strain on the server, so please use it with some consideration. Note that
5521 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5522 case-sensitive.</dd>
5525 print <<EOT;
5526 <dt><b>author</b></dt>
5527 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5528 <dt><b>committer</b></dt>
5529 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5531 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5532 if ($have_pickaxe) {
5533 print <<EOT;
5534 <dt><b>pickaxe</b></dt>
5535 <dd>All commits that caused the string to appear or disappear from any file (changes that
5536 added, removed or "modified" the string) will be listed. This search can take a while and
5537 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5538 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5541 print "</dl>\n";
5542 git_footer_html();
5545 sub git_shortlog {
5546 my $head = git_get_head_hash($project);
5547 if (!defined $hash) {
5548 $hash = $head;
5550 if (!defined $page) {
5551 $page = 0;
5553 my $refs = git_get_references();
5555 my @commitlist = parse_commits($hash, 101, (100 * $page));
5557 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5558 my $next_link = '';
5559 if ($#commitlist >= 100) {
5560 $next_link =
5561 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5562 -accesskey => "n", -title => "Alt-n"}, "next");
5565 git_header_html();
5566 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5567 git_print_header_div('summary', $project);
5569 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5571 git_footer_html();
5574 ## ......................................................................
5575 ## feeds (RSS, Atom; OPML)
5577 sub git_feed {
5578 my $format = shift || 'atom';
5579 my ($have_blame) = gitweb_check_feature('blame');
5581 # Atom: http://www.atomenabled.org/developers/syndication/
5582 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5583 if ($format ne 'rss' && $format ne 'atom') {
5584 die_error(undef, "Unknown web feed format");
5587 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5588 my $head = $hash || 'HEAD';
5589 my @commitlist = parse_commits($head, 150, 0, $file_name);
5591 my %latest_commit;
5592 my %latest_date;
5593 my $content_type = "application/$format+xml";
5594 if (defined $cgi->http('HTTP_ACCEPT') &&
5595 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5596 # browser (feed reader) prefers text/xml
5597 $content_type = 'text/xml';
5599 if (defined($commitlist[0])) {
5600 %latest_commit = %{$commitlist[0]};
5601 %latest_date = parse_date($latest_commit{'author_epoch'});
5602 print $cgi->header(
5603 -type => $content_type,
5604 -charset => 'utf-8',
5605 -last_modified => $latest_date{'rfc2822'});
5606 } else {
5607 print $cgi->header(
5608 -type => $content_type,
5609 -charset => 'utf-8');
5612 # Optimization: skip generating the body if client asks only
5613 # for Last-Modified date.
5614 return if ($cgi->request_method() eq 'HEAD');
5616 # header variables
5617 my $title = "$site_name - $project/$action";
5618 my $feed_type = 'log';
5619 if (defined $hash) {
5620 $title .= " - '$hash'";
5621 $feed_type = 'branch log';
5622 if (defined $file_name) {
5623 $title .= " :: $file_name";
5624 $feed_type = 'history';
5626 } elsif (defined $file_name) {
5627 $title .= " - $file_name";
5628 $feed_type = 'history';
5630 $title .= " $feed_type";
5631 my $descr = git_get_project_description($project);
5632 if (defined $descr) {
5633 $descr = esc_html($descr);
5634 } else {
5635 $descr = "$project " .
5636 ($format eq 'rss' ? 'RSS' : 'Atom') .
5637 " feed";
5639 my $owner = git_get_project_owner($project);
5640 $owner = esc_html($owner);
5642 #header
5643 my $alt_url;
5644 if (defined $file_name) {
5645 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5646 } elsif (defined $hash) {
5647 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5648 } else {
5649 $alt_url = href(-full=>1, action=>"summary");
5651 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5652 if ($format eq 'rss') {
5653 print <<XML;
5654 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5655 <channel>
5657 print "<title>$title</title>\n" .
5658 "<link>$alt_url</link>\n" .
5659 "<description>$descr</description>\n" .
5660 "<language>en</language>\n";
5661 } elsif ($format eq 'atom') {
5662 print <<XML;
5663 <feed xmlns="http://www.w3.org/2005/Atom">
5665 print "<title>$title</title>\n" .
5666 "<subtitle>$descr</subtitle>\n" .
5667 '<link rel="alternate" type="text/html" href="' .
5668 $alt_url . '" />' . "\n" .
5669 '<link rel="self" type="' . $content_type . '" href="' .
5670 $cgi->self_url() . '" />' . "\n" .
5671 "<id>" . href(-full=>1) . "</id>\n" .
5672 # use project owner for feed author
5673 "<author><name>$owner</name></author>\n";
5674 if (defined $favicon) {
5675 print "<icon>" . esc_url($favicon) . "</icon>\n";
5677 if (defined $logo_url) {
5678 # not twice as wide as tall: 72 x 27 pixels
5679 print "<logo>" . esc_url($logo) . "</logo>\n";
5681 if (! %latest_date) {
5682 # dummy date to keep the feed valid until commits trickle in:
5683 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5684 } else {
5685 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5689 # contents
5690 for (my $i = 0; $i <= $#commitlist; $i++) {
5691 my %co = %{$commitlist[$i]};
5692 my $commit = $co{'id'};
5693 # we read 150, we always show 30 and the ones more recent than 48 hours
5694 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5695 last;
5697 my %cd = parse_date($co{'author_epoch'});
5699 # get list of changed files
5700 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5701 $co{'parent'} || "--root",
5702 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5703 or next;
5704 my @difftree = map { chomp; $_ } <$fd>;
5705 close $fd
5706 or next;
5708 # print element (entry, item)
5709 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5710 if ($format eq 'rss') {
5711 print "<item>\n" .
5712 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5713 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5714 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5715 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5716 "<link>$co_url</link>\n" .
5717 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5718 "<content:encoded>" .
5719 "<![CDATA[\n";
5720 } elsif ($format eq 'atom') {
5721 print "<entry>\n" .
5722 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5723 "<updated>$cd{'iso-8601'}</updated>\n" .
5724 "<author>\n" .
5725 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5726 if ($co{'author_email'}) {
5727 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5729 print "</author>\n" .
5730 # use committer for contributor
5731 "<contributor>\n" .
5732 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5733 if ($co{'committer_email'}) {
5734 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5736 print "</contributor>\n" .
5737 "<published>$cd{'iso-8601'}</published>\n" .
5738 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5739 "<id>$co_url</id>\n" .
5740 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5741 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5743 my $comment = $co{'comment'};
5744 print "<pre>\n";
5745 foreach my $line (@$comment) {
5746 $line = esc_html($line);
5747 print "$line\n";
5749 print "</pre><ul>\n";
5750 foreach my $difftree_line (@difftree) {
5751 my %difftree = parse_difftree_raw_line($difftree_line);
5752 next if !$difftree{'from_id'};
5754 my $file = $difftree{'file'} || $difftree{'to_file'};
5756 print "<li>" .
5757 "[" .
5758 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5759 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5760 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5761 file_name=>$file, file_parent=>$difftree{'from_file'}),
5762 -title => "diff"}, 'D');
5763 if ($have_blame) {
5764 print $cgi->a({-href => href(-full=>1, action=>"blame",
5765 file_name=>$file, hash_base=>$commit),
5766 -title => "blame"}, 'B');
5768 # if this is not a feed of a file history
5769 if (!defined $file_name || $file_name ne $file) {
5770 print $cgi->a({-href => href(-full=>1, action=>"history",
5771 file_name=>$file, hash=>$commit),
5772 -title => "history"}, 'H');
5774 $file = esc_path($file);
5775 print "] ".
5776 "$file</li>\n";
5778 if ($format eq 'rss') {
5779 print "</ul>]]>\n" .
5780 "</content:encoded>\n" .
5781 "</item>\n";
5782 } elsif ($format eq 'atom') {
5783 print "</ul>\n</div>\n" .
5784 "</content>\n" .
5785 "</entry>\n";
5789 # end of feed
5790 if ($format eq 'rss') {
5791 print "</channel>\n</rss>\n";
5792 } elsif ($format eq 'atom') {
5793 print "</feed>\n";
5797 sub git_rss {
5798 git_feed('rss');
5801 sub git_atom {
5802 git_feed('atom');
5805 sub git_opml {
5806 my @list = git_get_projects_list();
5808 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5809 print <<XML;
5810 <?xml version="1.0" encoding="utf-8"?>
5811 <opml version="1.0">
5812 <head>
5813 <title>$site_name OPML Export</title>
5814 </head>
5815 <body>
5816 <outline text="git RSS feeds">
5819 foreach my $pr (@list) {
5820 my %proj = %$pr;
5821 my $head = git_get_head_hash($proj{'path'});
5822 if (!defined $head) {
5823 next;
5825 $git_dir = "$projectroot/$proj{'path'}";
5826 my %co = parse_commit($head);
5827 if (!%co) {
5828 next;
5831 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5832 my $rss = "$my_url?p=$proj{'path'};a=rss";
5833 my $html = "$my_url?p=$proj{'path'};a=summary";
5834 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5836 print <<XML;
5837 </outline>
5838 </body>
5839 </opml>