gitweb: Separate filling list of projects info
[git/dscho.git] / gitweb / gitweb.perl
blobd7a980902701e654d0a46e648838e8cfe1c3c7cb
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 # returns path to the core git executable and the --git-dir parameter as string
1504 sub git_cmd_str {
1505 return join(' ', git_cmd());
1508 # get HEAD ref of given project as hash
1509 sub git_get_head_hash {
1510 my $project = shift;
1511 my $o_git_dir = $git_dir;
1512 my $retval = undef;
1513 $git_dir = "$projectroot/$project";
1514 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1515 my $head = <$fd>;
1516 close $fd;
1517 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1518 $retval = $1;
1521 if (defined $o_git_dir) {
1522 $git_dir = $o_git_dir;
1524 return $retval;
1527 # get type of given object
1528 sub git_get_type {
1529 my $hash = shift;
1531 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1532 my $type = <$fd>;
1533 close $fd or return;
1534 chomp $type;
1535 return $type;
1538 # repository configuration
1539 our $config_file = '';
1540 our %config;
1542 # store multiple values for single key as anonymous array reference
1543 # single values stored directly in the hash, not as [ <value> ]
1544 sub hash_set_multi {
1545 my ($hash, $key, $value) = @_;
1547 if (!exists $hash->{$key}) {
1548 $hash->{$key} = $value;
1549 } elsif (!ref $hash->{$key}) {
1550 $hash->{$key} = [ $hash->{$key}, $value ];
1551 } else {
1552 push @{$hash->{$key}}, $value;
1556 # return hash of git project configuration
1557 # optionally limited to some section, e.g. 'gitweb'
1558 sub git_parse_project_config {
1559 my $section_regexp = shift;
1560 my %config;
1562 local $/ = "\0";
1564 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1565 or return;
1567 while (my $keyval = <$fh>) {
1568 chomp $keyval;
1569 my ($key, $value) = split(/\n/, $keyval, 2);
1571 hash_set_multi(\%config, $key, $value)
1572 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1574 close $fh;
1576 return %config;
1579 # convert config value to boolean, 'true' or 'false'
1580 # no value, number > 0, 'true' and 'yes' values are true
1581 # rest of values are treated as false (never as error)
1582 sub config_to_bool {
1583 my $val = shift;
1585 # strip leading and trailing whitespace
1586 $val =~ s/^\s+//;
1587 $val =~ s/\s+$//;
1589 return (!defined $val || # section.key
1590 ($val =~ /^\d+$/ && $val) || # section.key = 1
1591 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1594 # convert config value to simple decimal number
1595 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1596 # to be multiplied by 1024, 1048576, or 1073741824
1597 sub config_to_int {
1598 my $val = shift;
1600 # strip leading and trailing whitespace
1601 $val =~ s/^\s+//;
1602 $val =~ s/\s+$//;
1604 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1605 $unit = lc($unit);
1606 # unknown unit is treated as 1
1607 return $num * ($unit eq 'g' ? 1073741824 :
1608 $unit eq 'm' ? 1048576 :
1609 $unit eq 'k' ? 1024 : 1);
1611 return $val;
1614 # convert config value to array reference, if needed
1615 sub config_to_multi {
1616 my $val = shift;
1618 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1621 sub git_get_project_config {
1622 my ($key, $type) = @_;
1624 # key sanity check
1625 return unless ($key);
1626 $key =~ s/^gitweb\.//;
1627 return if ($key =~ m/\W/);
1629 # type sanity check
1630 if (defined $type) {
1631 $type =~ s/^--//;
1632 $type = undef
1633 unless ($type eq 'bool' || $type eq 'int');
1636 # get config
1637 if (!defined $config_file ||
1638 $config_file ne "$git_dir/config") {
1639 %config = git_parse_project_config('gitweb');
1640 $config_file = "$git_dir/config";
1643 # ensure given type
1644 if (!defined $type) {
1645 return $config{"gitweb.$key"};
1646 } elsif ($type eq 'bool') {
1647 # backward compatibility: 'git config --bool' returns true/false
1648 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1649 } elsif ($type eq 'int') {
1650 return config_to_int($config{"gitweb.$key"});
1652 return $config{"gitweb.$key"};
1655 # get hash of given path at given ref
1656 sub git_get_hash_by_path {
1657 my $base = shift;
1658 my $path = shift || return undef;
1659 my $type = shift;
1661 $path =~ s,/+$,,;
1663 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1664 or die_error(undef, "Open git-ls-tree failed");
1665 my $line = <$fd>;
1666 close $fd or return undef;
1668 if (!defined $line) {
1669 # there is no tree or hash given by $path at $base
1670 return undef;
1673 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1674 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1675 if (defined $type && $type ne $2) {
1676 # type doesn't match
1677 return undef;
1679 return $3;
1682 # get path of entry with given hash at given tree-ish (ref)
1683 # used to get 'from' filename for combined diff (merge commit) for renames
1684 sub git_get_path_by_hash {
1685 my $base = shift || return;
1686 my $hash = shift || return;
1688 local $/ = "\0";
1690 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1691 or return undef;
1692 while (my $line = <$fd>) {
1693 chomp $line;
1695 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1696 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1697 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1698 close $fd;
1699 return $1;
1702 close $fd;
1703 return undef;
1706 ## ......................................................................
1707 ## git utility functions, directly accessing git repository
1709 sub git_get_project_description {
1710 my $path = shift;
1712 $git_dir = "$projectroot/$path";
1713 open my $fd, "$git_dir/description"
1714 or return git_get_project_config('description');
1715 my $descr = <$fd>;
1716 close $fd;
1717 if (defined $descr) {
1718 chomp $descr;
1720 return $descr;
1723 sub git_get_project_url_list {
1724 my $path = shift;
1726 $git_dir = "$projectroot/$path";
1727 open my $fd, "$git_dir/cloneurl"
1728 or return wantarray ?
1729 @{ config_to_multi(git_get_project_config('url')) } :
1730 config_to_multi(git_get_project_config('url'));
1731 my @git_project_url_list = map { chomp; $_ } <$fd>;
1732 close $fd;
1734 return wantarray ? @git_project_url_list : \@git_project_url_list;
1737 sub git_get_projects_list {
1738 my ($filter) = @_;
1739 my @list;
1741 $filter ||= '';
1742 $filter =~ s/\.git$//;
1744 my ($check_forks) = gitweb_check_feature('forks');
1746 if (-d $projects_list) {
1747 # search in directory
1748 my $dir = $projects_list . ($filter ? "/$filter" : '');
1749 # remove the trailing "/"
1750 $dir =~ s!/+$!!;
1751 my $pfxlen = length("$dir");
1752 my $pfxdepth = ($dir =~ tr!/!!);
1754 File::Find::find({
1755 follow_fast => 1, # follow symbolic links
1756 follow_skip => 2, # ignore duplicates
1757 dangling_symlinks => 0, # ignore dangling symlinks, silently
1758 wanted => sub {
1759 # skip project-list toplevel, if we get it.
1760 return if (m!^[/.]$!);
1761 # only directories can be git repositories
1762 return unless (-d $_);
1763 # don't traverse too deep (Find is super slow on os x)
1764 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1765 $File::Find::prune = 1;
1766 return;
1769 my $subdir = substr($File::Find::name, $pfxlen + 1);
1770 # we check related file in $projectroot
1771 if ($check_forks and $subdir =~ m#/.#) {
1772 $File::Find::prune = 1;
1773 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1774 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1775 $File::Find::prune = 1;
1778 }, "$dir");
1780 } elsif (-f $projects_list) {
1781 # read from file(url-encoded):
1782 # 'git%2Fgit.git Linus+Torvalds'
1783 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1784 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1785 my %paths;
1786 open my ($fd), $projects_list or return;
1787 PROJECT:
1788 while (my $line = <$fd>) {
1789 chomp $line;
1790 my ($path, $owner) = split ' ', $line;
1791 $path = unescape($path);
1792 $owner = unescape($owner);
1793 if (!defined $path) {
1794 next;
1796 if ($filter ne '') {
1797 # looking for forks;
1798 my $pfx = substr($path, 0, length($filter));
1799 if ($pfx ne $filter) {
1800 next PROJECT;
1802 my $sfx = substr($path, length($filter));
1803 if ($sfx !~ /^\/.*\.git$/) {
1804 next PROJECT;
1806 } elsif ($check_forks) {
1807 PATH:
1808 foreach my $filter (keys %paths) {
1809 # looking for forks;
1810 my $pfx = substr($path, 0, length($filter));
1811 if ($pfx ne $filter) {
1812 next PATH;
1814 my $sfx = substr($path, length($filter));
1815 if ($sfx !~ /^\/.*\.git$/) {
1816 next PATH;
1818 # is a fork, don't include it in
1819 # the list
1820 next PROJECT;
1823 if (check_export_ok("$projectroot/$path")) {
1824 my $pr = {
1825 path => $path,
1826 owner => to_utf8($owner),
1828 push @list, $pr;
1829 (my $forks_path = $path) =~ s/\.git$//;
1830 $paths{$forks_path}++;
1833 close $fd;
1835 return @list;
1838 our $gitweb_project_owner = undef;
1839 sub git_get_project_list_from_file {
1841 return if (defined $gitweb_project_owner);
1843 $gitweb_project_owner = {};
1844 # read from file (url-encoded):
1845 # 'git%2Fgit.git Linus+Torvalds'
1846 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1847 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1848 if (-f $projects_list) {
1849 open (my $fd , $projects_list);
1850 while (my $line = <$fd>) {
1851 chomp $line;
1852 my ($pr, $ow) = split ' ', $line;
1853 $pr = unescape($pr);
1854 $ow = unescape($ow);
1855 $gitweb_project_owner->{$pr} = to_utf8($ow);
1857 close $fd;
1861 sub git_get_project_owner {
1862 my $project = shift;
1863 my $owner;
1865 return undef unless $project;
1866 $git_dir = "$projectroot/$project";
1868 if (!defined $gitweb_project_owner) {
1869 git_get_project_list_from_file();
1872 if (exists $gitweb_project_owner->{$project}) {
1873 $owner = $gitweb_project_owner->{$project};
1875 if (!defined $owner){
1876 $owner = git_get_project_config('owner');
1878 if (!defined $owner) {
1879 $owner = get_file_owner("$git_dir");
1882 return $owner;
1885 sub git_get_last_activity {
1886 my ($path) = @_;
1887 my $fd;
1889 $git_dir = "$projectroot/$path";
1890 open($fd, "-|", git_cmd(), 'for-each-ref',
1891 '--format=%(committer)',
1892 '--sort=-committerdate',
1893 '--count=1',
1894 'refs/heads') or return;
1895 my $most_recent = <$fd>;
1896 close $fd or return;
1897 if (defined $most_recent &&
1898 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1899 my $timestamp = $1;
1900 my $age = time - $timestamp;
1901 return ($age, age_string($age));
1903 return (undef, undef);
1906 sub git_get_references {
1907 my $type = shift || "";
1908 my %refs;
1909 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1910 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1911 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1912 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1913 or return;
1915 while (my $line = <$fd>) {
1916 chomp $line;
1917 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
1918 if (defined $refs{$1}) {
1919 push @{$refs{$1}}, $2;
1920 } else {
1921 $refs{$1} = [ $2 ];
1925 close $fd or return;
1926 return \%refs;
1929 sub git_get_rev_name_tags {
1930 my $hash = shift || return undef;
1932 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1933 or return;
1934 my $name_rev = <$fd>;
1935 close $fd;
1937 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1938 return $1;
1939 } else {
1940 # catches also '$hash undefined' output
1941 return undef;
1945 ## ----------------------------------------------------------------------
1946 ## parse to hash functions
1948 sub parse_date {
1949 my $epoch = shift;
1950 my $tz = shift || "-0000";
1952 my %date;
1953 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1954 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1955 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1956 $date{'hour'} = $hour;
1957 $date{'minute'} = $min;
1958 $date{'mday'} = $mday;
1959 $date{'day'} = $days[$wday];
1960 $date{'month'} = $months[$mon];
1961 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1962 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
1963 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1964 $mday, $months[$mon], $hour ,$min;
1965 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1966 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
1968 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1969 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1970 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1971 $date{'hour_local'} = $hour;
1972 $date{'minute_local'} = $min;
1973 $date{'tz_local'} = $tz;
1974 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1975 1900+$year, $mon+1, $mday,
1976 $hour, $min, $sec, $tz);
1977 return %date;
1980 sub parse_tag {
1981 my $tag_id = shift;
1982 my %tag;
1983 my @comment;
1985 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
1986 $tag{'id'} = $tag_id;
1987 while (my $line = <$fd>) {
1988 chomp $line;
1989 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1990 $tag{'object'} = $1;
1991 } elsif ($line =~ m/^type (.+)$/) {
1992 $tag{'type'} = $1;
1993 } elsif ($line =~ m/^tag (.+)$/) {
1994 $tag{'name'} = $1;
1995 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1996 $tag{'author'} = $1;
1997 $tag{'epoch'} = $2;
1998 $tag{'tz'} = $3;
1999 } elsif ($line =~ m/--BEGIN/) {
2000 push @comment, $line;
2001 last;
2002 } elsif ($line eq "") {
2003 last;
2006 push @comment, <$fd>;
2007 $tag{'comment'} = \@comment;
2008 close $fd or return;
2009 if (!defined $tag{'name'}) {
2010 return
2012 return %tag
2015 sub parse_commit_text {
2016 my ($commit_text, $withparents) = @_;
2017 my @commit_lines = split '\n', $commit_text;
2018 my %co;
2020 pop @commit_lines; # Remove '\0'
2022 if (! @commit_lines) {
2023 return;
2026 my $header = shift @commit_lines;
2027 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2028 return;
2030 ($co{'id'}, my @parents) = split ' ', $header;
2031 while (my $line = shift @commit_lines) {
2032 last if $line eq "\n";
2033 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2034 $co{'tree'} = $1;
2035 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2036 push @parents, $1;
2037 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2038 $co{'author'} = $1;
2039 $co{'author_epoch'} = $2;
2040 $co{'author_tz'} = $3;
2041 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2042 $co{'author_name'} = $1;
2043 $co{'author_email'} = $2;
2044 } else {
2045 $co{'author_name'} = $co{'author'};
2047 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2048 $co{'committer'} = $1;
2049 $co{'committer_epoch'} = $2;
2050 $co{'committer_tz'} = $3;
2051 $co{'committer_name'} = $co{'committer'};
2052 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2053 $co{'committer_name'} = $1;
2054 $co{'committer_email'} = $2;
2055 } else {
2056 $co{'committer_name'} = $co{'committer'};
2060 if (!defined $co{'tree'}) {
2061 return;
2063 $co{'parents'} = \@parents;
2064 $co{'parent'} = $parents[0];
2066 foreach my $title (@commit_lines) {
2067 $title =~ s/^ //;
2068 if ($title ne "") {
2069 $co{'title'} = chop_str($title, 80, 5);
2070 # remove leading stuff of merges to make the interesting part visible
2071 if (length($title) > 50) {
2072 $title =~ s/^Automatic //;
2073 $title =~ s/^merge (of|with) /Merge ... /i;
2074 if (length($title) > 50) {
2075 $title =~ s/(http|rsync):\/\///;
2077 if (length($title) > 50) {
2078 $title =~ s/(master|www|rsync)\.//;
2080 if (length($title) > 50) {
2081 $title =~ s/kernel.org:?//;
2083 if (length($title) > 50) {
2084 $title =~ s/\/pub\/scm//;
2087 $co{'title_short'} = chop_str($title, 50, 5);
2088 last;
2091 if ($co{'title'} eq "") {
2092 $co{'title'} = $co{'title_short'} = '(no commit message)';
2094 # remove added spaces
2095 foreach my $line (@commit_lines) {
2096 $line =~ s/^ //;
2098 $co{'comment'} = \@commit_lines;
2100 my $age = time - $co{'committer_epoch'};
2101 $co{'age'} = $age;
2102 $co{'age_string'} = age_string($age);
2103 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2104 if ($age > 60*60*24*7*2) {
2105 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2106 $co{'age_string_age'} = $co{'age_string'};
2107 } else {
2108 $co{'age_string_date'} = $co{'age_string'};
2109 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2111 return %co;
2114 sub parse_commit {
2115 my ($commit_id) = @_;
2116 my %co;
2118 local $/ = "\0";
2120 open my $fd, "-|", git_cmd(), "rev-list",
2121 "--parents",
2122 "--header",
2123 "--max-count=1",
2124 $commit_id,
2125 "--",
2126 or die_error(undef, "Open git-rev-list failed");
2127 %co = parse_commit_text(<$fd>, 1);
2128 close $fd;
2130 return %co;
2133 sub parse_commits {
2134 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2135 my @cos;
2137 $maxcount ||= 1;
2138 $skip ||= 0;
2140 local $/ = "\0";
2142 open my $fd, "-|", git_cmd(), "rev-list",
2143 "--header",
2144 @args,
2145 ("--max-count=" . $maxcount),
2146 ("--skip=" . $skip),
2147 @extra_options,
2148 $commit_id,
2149 "--",
2150 ($filename ? ($filename) : ())
2151 or die_error(undef, "Open git-rev-list failed");
2152 while (my $line = <$fd>) {
2153 my %co = parse_commit_text($line);
2154 push @cos, \%co;
2156 close $fd;
2158 return wantarray ? @cos : \@cos;
2161 # parse ref from ref_file, given by ref_id, with given type
2162 sub parse_ref {
2163 my $ref_file = shift;
2164 my $ref_id = shift;
2165 my $type = shift || git_get_type($ref_id);
2166 my %ref_item;
2168 $ref_item{'type'} = $type;
2169 $ref_item{'id'} = $ref_id;
2170 $ref_item{'epoch'} = 0;
2171 $ref_item{'age'} = "unknown";
2172 if ($type eq "tag") {
2173 my %tag = parse_tag($ref_id);
2174 $ref_item{'comment'} = $tag{'comment'};
2175 if ($tag{'type'} eq "commit") {
2176 my %co = parse_commit($tag{'object'});
2177 $ref_item{'epoch'} = $co{'committer_epoch'};
2178 $ref_item{'age'} = $co{'age_string'};
2179 } elsif (defined($tag{'epoch'})) {
2180 my $age = time - $tag{'epoch'};
2181 $ref_item{'epoch'} = $tag{'epoch'};
2182 $ref_item{'age'} = age_string($age);
2184 $ref_item{'reftype'} = $tag{'type'};
2185 $ref_item{'name'} = $tag{'name'};
2186 $ref_item{'refid'} = $tag{'object'};
2187 } elsif ($type eq "commit"){
2188 my %co = parse_commit($ref_id);
2189 $ref_item{'reftype'} = "commit";
2190 $ref_item{'name'} = $ref_file;
2191 $ref_item{'title'} = $co{'title'};
2192 $ref_item{'refid'} = $ref_id;
2193 $ref_item{'epoch'} = $co{'committer_epoch'};
2194 $ref_item{'age'} = $co{'age_string'};
2195 } else {
2196 $ref_item{'reftype'} = $type;
2197 $ref_item{'name'} = $ref_file;
2198 $ref_item{'refid'} = $ref_id;
2201 return %ref_item;
2204 # parse line of git-diff-tree "raw" output
2205 sub parse_difftree_raw_line {
2206 my $line = shift;
2207 my %res;
2209 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2210 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2211 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2212 $res{'from_mode'} = $1;
2213 $res{'to_mode'} = $2;
2214 $res{'from_id'} = $3;
2215 $res{'to_id'} = $4;
2216 $res{'status'} = $5;
2217 $res{'similarity'} = $6;
2218 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2219 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2220 } else {
2221 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2224 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2225 # combined diff (for merge commit)
2226 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2227 $res{'nparents'} = length($1);
2228 $res{'from_mode'} = [ split(' ', $2) ];
2229 $res{'to_mode'} = pop @{$res{'from_mode'}};
2230 $res{'from_id'} = [ split(' ', $3) ];
2231 $res{'to_id'} = pop @{$res{'from_id'}};
2232 $res{'status'} = [ split('', $4) ];
2233 $res{'to_file'} = unquote($5);
2235 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2236 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2237 $res{'commit'} = $1;
2240 return wantarray ? %res : \%res;
2243 # wrapper: return parsed line of git-diff-tree "raw" output
2244 # (the argument might be raw line, or parsed info)
2245 sub parsed_difftree_line {
2246 my $line_or_ref = shift;
2248 if (ref($line_or_ref) eq "HASH") {
2249 # pre-parsed (or generated by hand)
2250 return $line_or_ref;
2251 } else {
2252 return parse_difftree_raw_line($line_or_ref);
2256 # parse line of git-ls-tree output
2257 sub parse_ls_tree_line ($;%) {
2258 my $line = shift;
2259 my %opts = @_;
2260 my %res;
2262 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2263 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2265 $res{'mode'} = $1;
2266 $res{'type'} = $2;
2267 $res{'hash'} = $3;
2268 if ($opts{'-z'}) {
2269 $res{'name'} = $4;
2270 } else {
2271 $res{'name'} = unquote($4);
2274 return wantarray ? %res : \%res;
2277 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2278 sub parse_from_to_diffinfo {
2279 my ($diffinfo, $from, $to, @parents) = @_;
2281 if ($diffinfo->{'nparents'}) {
2282 # combined diff
2283 $from->{'file'} = [];
2284 $from->{'href'} = [];
2285 fill_from_file_info($diffinfo, @parents)
2286 unless exists $diffinfo->{'from_file'};
2287 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2288 $from->{'file'}[$i] =
2289 defined $diffinfo->{'from_file'}[$i] ?
2290 $diffinfo->{'from_file'}[$i] :
2291 $diffinfo->{'to_file'};
2292 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2293 $from->{'href'}[$i] = href(action=>"blob",
2294 hash_base=>$parents[$i],
2295 hash=>$diffinfo->{'from_id'}[$i],
2296 file_name=>$from->{'file'}[$i]);
2297 } else {
2298 $from->{'href'}[$i] = undef;
2301 } else {
2302 # ordinary (not combined) diff
2303 $from->{'file'} = $diffinfo->{'from_file'};
2304 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2305 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2306 hash=>$diffinfo->{'from_id'},
2307 file_name=>$from->{'file'});
2308 } else {
2309 delete $from->{'href'};
2313 $to->{'file'} = $diffinfo->{'to_file'};
2314 if (!is_deleted($diffinfo)) { # file exists in result
2315 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2316 hash=>$diffinfo->{'to_id'},
2317 file_name=>$to->{'file'});
2318 } else {
2319 delete $to->{'href'};
2323 ## ......................................................................
2324 ## parse to array of hashes functions
2326 sub git_get_heads_list {
2327 my $limit = shift;
2328 my @headslist;
2330 open my $fd, '-|', git_cmd(), 'for-each-ref',
2331 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2332 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2333 'refs/heads'
2334 or return;
2335 while (my $line = <$fd>) {
2336 my %ref_item;
2338 chomp $line;
2339 my ($refinfo, $committerinfo) = split(/\0/, $line);
2340 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2341 my ($committer, $epoch, $tz) =
2342 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2343 $ref_item{'fullname'} = $name;
2344 $name =~ s!^refs/heads/!!;
2346 $ref_item{'name'} = $name;
2347 $ref_item{'id'} = $hash;
2348 $ref_item{'title'} = $title || '(no commit message)';
2349 $ref_item{'epoch'} = $epoch;
2350 if ($epoch) {
2351 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2352 } else {
2353 $ref_item{'age'} = "unknown";
2356 push @headslist, \%ref_item;
2358 close $fd;
2360 return wantarray ? @headslist : \@headslist;
2363 sub git_get_tags_list {
2364 my $limit = shift;
2365 my @tagslist;
2367 open my $fd, '-|', git_cmd(), 'for-each-ref',
2368 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2369 '--format=%(objectname) %(objecttype) %(refname) '.
2370 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2371 'refs/tags'
2372 or return;
2373 while (my $line = <$fd>) {
2374 my %ref_item;
2376 chomp $line;
2377 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2378 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2379 my ($creator, $epoch, $tz) =
2380 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2381 $ref_item{'fullname'} = $name;
2382 $name =~ s!^refs/tags/!!;
2384 $ref_item{'type'} = $type;
2385 $ref_item{'id'} = $id;
2386 $ref_item{'name'} = $name;
2387 if ($type eq "tag") {
2388 $ref_item{'subject'} = $title;
2389 $ref_item{'reftype'} = $reftype;
2390 $ref_item{'refid'} = $refid;
2391 } else {
2392 $ref_item{'reftype'} = $type;
2393 $ref_item{'refid'} = $id;
2396 if ($type eq "tag" || $type eq "commit") {
2397 $ref_item{'epoch'} = $epoch;
2398 if ($epoch) {
2399 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2400 } else {
2401 $ref_item{'age'} = "unknown";
2405 push @tagslist, \%ref_item;
2407 close $fd;
2409 return wantarray ? @tagslist : \@tagslist;
2412 ## ----------------------------------------------------------------------
2413 ## filesystem-related functions
2415 sub get_file_owner {
2416 my $path = shift;
2418 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2419 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2420 if (!defined $gcos) {
2421 return undef;
2423 my $owner = $gcos;
2424 $owner =~ s/[,;].*$//;
2425 return to_utf8($owner);
2428 ## ......................................................................
2429 ## mimetype related functions
2431 sub mimetype_guess_file {
2432 my $filename = shift;
2433 my $mimemap = shift;
2434 -r $mimemap or return undef;
2436 my %mimemap;
2437 open(MIME, $mimemap) or return undef;
2438 while (<MIME>) {
2439 next if m/^#/; # skip comments
2440 my ($mime, $exts) = split(/\t+/);
2441 if (defined $exts) {
2442 my @exts = split(/\s+/, $exts);
2443 foreach my $ext (@exts) {
2444 $mimemap{$ext} = $mime;
2448 close(MIME);
2450 $filename =~ /\.([^.]*)$/;
2451 return $mimemap{$1};
2454 sub mimetype_guess {
2455 my $filename = shift;
2456 my $mime;
2457 $filename =~ /\./ or return undef;
2459 if ($mimetypes_file) {
2460 my $file = $mimetypes_file;
2461 if ($file !~ m!^/!) { # if it is relative path
2462 # it is relative to project
2463 $file = "$projectroot/$project/$file";
2465 $mime = mimetype_guess_file($filename, $file);
2467 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2468 return $mime;
2471 sub blob_mimetype {
2472 my $fd = shift;
2473 my $filename = shift;
2475 if ($filename) {
2476 my $mime = mimetype_guess($filename);
2477 $mime and return $mime;
2480 # just in case
2481 return $default_blob_plain_mimetype unless $fd;
2483 if (-T $fd) {
2484 return 'text/plain';
2485 } elsif (! $filename) {
2486 return 'application/octet-stream';
2487 } elsif ($filename =~ m/\.png$/i) {
2488 return 'image/png';
2489 } elsif ($filename =~ m/\.gif$/i) {
2490 return 'image/gif';
2491 } elsif ($filename =~ m/\.jpe?g$/i) {
2492 return 'image/jpeg';
2493 } else {
2494 return 'application/octet-stream';
2498 sub blob_contenttype {
2499 my ($fd, $file_name, $type) = @_;
2501 $type ||= blob_mimetype($fd, $file_name);
2502 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2503 $type .= "; charset=$default_text_plain_charset";
2506 return $type;
2509 ## ======================================================================
2510 ## functions printing HTML: header, footer, error page
2512 sub git_header_html {
2513 my $status = shift || "200 OK";
2514 my $expires = shift;
2516 my $title = "$site_name";
2517 if (defined $project) {
2518 $title .= " - " . to_utf8($project);
2519 if (defined $action) {
2520 $title .= "/$action";
2521 if (defined $file_name) {
2522 $title .= " - " . esc_path($file_name);
2523 if ($action eq "tree" && $file_name !~ m|/$|) {
2524 $title .= "/";
2529 my $content_type;
2530 # require explicit support from the UA if we are to send the page as
2531 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2532 # we have to do this because MSIE sometimes globs '*/*', pretending to
2533 # support xhtml+xml but choking when it gets what it asked for.
2534 if (defined $cgi->http('HTTP_ACCEPT') &&
2535 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2536 $cgi->Accept('application/xhtml+xml') != 0) {
2537 $content_type = 'application/xhtml+xml';
2538 } else {
2539 $content_type = 'text/html';
2541 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2542 -status=> $status, -expires => $expires);
2543 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2544 print <<EOF;
2545 <?xml version="1.0" encoding="utf-8"?>
2546 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2547 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2548 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2549 <!-- git core binaries version $git_version -->
2550 <head>
2551 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2552 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2553 <meta name="robots" content="index, nofollow"/>
2554 <title>$title</title>
2556 # print out each stylesheet that exist
2557 if (defined $stylesheet) {
2558 #provides backwards capability for those people who define style sheet in a config file
2559 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2560 } else {
2561 foreach my $stylesheet (@stylesheets) {
2562 next unless $stylesheet;
2563 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2566 if (defined $project) {
2567 my %href_params = get_feed_info();
2568 if (!exists $href_params{'-title'}) {
2569 $href_params{'-title'} = 'log';
2572 foreach my $format qw(RSS Atom) {
2573 my $type = lc($format);
2574 my %link_attr = (
2575 '-rel' => 'alternate',
2576 '-title' => "$project - $href_params{'-title'} - $format feed",
2577 '-type' => "application/$type+xml"
2580 $href_params{'action'} = $type;
2581 $link_attr{'-href'} = href(%href_params);
2582 print "<link ".
2583 "rel=\"$link_attr{'-rel'}\" ".
2584 "title=\"$link_attr{'-title'}\" ".
2585 "href=\"$link_attr{'-href'}\" ".
2586 "type=\"$link_attr{'-type'}\" ".
2587 "/>\n";
2589 $href_params{'extra_options'} = '--no-merges';
2590 $link_attr{'-href'} = href(%href_params);
2591 $link_attr{'-title'} .= ' (no merges)';
2592 print "<link ".
2593 "rel=\"$link_attr{'-rel'}\" ".
2594 "title=\"$link_attr{'-title'}\" ".
2595 "href=\"$link_attr{'-href'}\" ".
2596 "type=\"$link_attr{'-type'}\" ".
2597 "/>\n";
2600 } else {
2601 printf('<link rel="alternate" title="%s projects list" '.
2602 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2603 $site_name, href(project=>undef, action=>"project_index"));
2604 printf('<link rel="alternate" title="%s projects feeds" '.
2605 'href="%s" type="text/x-opml" />'."\n",
2606 $site_name, href(project=>undef, action=>"opml"));
2608 if (defined $favicon) {
2609 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2612 print "</head>\n" .
2613 "<body>\n";
2615 if (-f $site_header) {
2616 open (my $fd, $site_header);
2617 print <$fd>;
2618 close $fd;
2621 print "<div class=\"page_header\">\n" .
2622 $cgi->a({-href => esc_url($logo_url),
2623 -title => $logo_label},
2624 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2625 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2626 if (defined $project) {
2627 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2628 if (defined $action) {
2629 print " / $action";
2631 print "\n";
2633 print "</div>\n";
2635 my ($have_search) = gitweb_check_feature('search');
2636 if (defined $project && $have_search) {
2637 if (!defined $searchtext) {
2638 $searchtext = "";
2640 my $search_hash;
2641 if (defined $hash_base) {
2642 $search_hash = $hash_base;
2643 } elsif (defined $hash) {
2644 $search_hash = $hash;
2645 } else {
2646 $search_hash = "HEAD";
2648 my $action = $my_uri;
2649 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2650 if ($use_pathinfo) {
2651 $action .= "/".esc_url($project);
2653 print $cgi->startform(-method => "get", -action => $action) .
2654 "<div class=\"search\">\n" .
2655 (!$use_pathinfo &&
2656 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2657 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2658 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2659 $cgi->popup_menu(-name => 'st', -default => 'commit',
2660 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2661 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2662 " search:\n",
2663 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2664 "<span title=\"Extended regular expression\">" .
2665 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
2666 -checked => $search_use_regexp) .
2667 "</span>" .
2668 "</div>" .
2669 $cgi->end_form() . "\n";
2673 sub git_footer_html {
2674 my $feed_class = 'rss_logo';
2676 print "<div class=\"page_footer\">\n";
2677 if (defined $project) {
2678 my $descr = git_get_project_description($project);
2679 if (defined $descr) {
2680 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2683 my %href_params = get_feed_info();
2684 if (!%href_params) {
2685 $feed_class .= ' generic';
2687 $href_params{'-title'} ||= 'log';
2689 foreach my $format qw(RSS Atom) {
2690 $href_params{'action'} = lc($format);
2691 print $cgi->a({-href => href(%href_params),
2692 -title => "$href_params{'-title'} $format feed",
2693 -class => $feed_class}, $format)."\n";
2696 } else {
2697 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2698 -class => $feed_class}, "OPML") . " ";
2699 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2700 -class => $feed_class}, "TXT") . "\n";
2702 print "</div>\n"; # class="page_footer"
2704 if (-f $site_footer) {
2705 open (my $fd, $site_footer);
2706 print <$fd>;
2707 close $fd;
2710 print "</body>\n" .
2711 "</html>";
2714 sub die_error {
2715 my $status = shift || "403 Forbidden";
2716 my $error = shift || "Malformed query, file missing or permission denied";
2718 git_header_html($status);
2719 print <<EOF;
2720 <div class="page_body">
2721 <br /><br />
2722 $status - $error
2723 <br />
2724 </div>
2726 git_footer_html();
2727 exit;
2730 ## ----------------------------------------------------------------------
2731 ## functions printing or outputting HTML: navigation
2733 sub git_print_page_nav {
2734 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2735 $extra = '' if !defined $extra; # pager or formats
2737 my @navs = qw(summary shortlog log commit commitdiff tree);
2738 if ($suppress) {
2739 @navs = grep { $_ ne $suppress } @navs;
2742 my %arg = map { $_ => {action=>$_} } @navs;
2743 if (defined $head) {
2744 for (qw(commit commitdiff)) {
2745 $arg{$_}{'hash'} = $head;
2747 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2748 for (qw(shortlog log)) {
2749 $arg{$_}{'hash'} = $head;
2753 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2754 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2756 print "<div class=\"page_nav\">\n" .
2757 (join " | ",
2758 map { $_ eq $current ?
2759 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
2760 } @navs);
2761 print "<br/>\n$extra<br/>\n" .
2762 "</div>\n";
2765 sub format_paging_nav {
2766 my ($action, $hash, $head, $page, $has_next_link) = @_;
2767 my $paging_nav;
2770 if ($hash ne $head || $page) {
2771 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2772 } else {
2773 $paging_nav .= "HEAD";
2776 if ($page > 0) {
2777 $paging_nav .= " &sdot; " .
2778 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2779 -accesskey => "p", -title => "Alt-p"}, "prev");
2780 } else {
2781 $paging_nav .= " &sdot; prev";
2784 if ($has_next_link) {
2785 $paging_nav .= " &sdot; " .
2786 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2787 -accesskey => "n", -title => "Alt-n"}, "next");
2788 } else {
2789 $paging_nav .= " &sdot; next";
2792 return $paging_nav;
2795 ## ......................................................................
2796 ## functions printing or outputting HTML: div
2798 sub git_print_header_div {
2799 my ($action, $title, $hash, $hash_base) = @_;
2800 my %args = ();
2802 $args{'action'} = $action;
2803 $args{'hash'} = $hash if $hash;
2804 $args{'hash_base'} = $hash_base if $hash_base;
2806 print "<div class=\"header\">\n" .
2807 $cgi->a({-href => href(%args), -class => "title"},
2808 $title ? $title : $action) .
2809 "\n</div>\n";
2812 #sub git_print_authorship (\%) {
2813 sub git_print_authorship {
2814 my $co = shift;
2816 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2817 print "<div class=\"author_date\">" .
2818 esc_html($co->{'author_name'}) .
2819 " [$ad{'rfc2822'}";
2820 if ($ad{'hour_local'} < 6) {
2821 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2822 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2823 } else {
2824 printf(" (%02d:%02d %s)",
2825 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2827 print "]</div>\n";
2830 sub git_print_page_path {
2831 my $name = shift;
2832 my $type = shift;
2833 my $hb = shift;
2836 print "<div class=\"page_path\">";
2837 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2838 -title => 'tree root'}, to_utf8("[$project]"));
2839 print " / ";
2840 if (defined $name) {
2841 my @dirname = split '/', $name;
2842 my $basename = pop @dirname;
2843 my $fullname = '';
2845 foreach my $dir (@dirname) {
2846 $fullname .= ($fullname ? '/' : '') . $dir;
2847 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2848 hash_base=>$hb),
2849 -title => $fullname}, esc_path($dir));
2850 print " / ";
2852 if (defined $type && $type eq 'blob') {
2853 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2854 hash_base=>$hb),
2855 -title => $name}, esc_path($basename));
2856 } elsif (defined $type && $type eq 'tree') {
2857 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2858 hash_base=>$hb),
2859 -title => $name}, esc_path($basename));
2860 print " / ";
2861 } else {
2862 print esc_path($basename);
2865 print "<br/></div>\n";
2868 # sub git_print_log (\@;%) {
2869 sub git_print_log ($;%) {
2870 my $log = shift;
2871 my %opts = @_;
2873 if ($opts{'-remove_title'}) {
2874 # remove title, i.e. first line of log
2875 shift @$log;
2877 # remove leading empty lines
2878 while (defined $log->[0] && $log->[0] eq "") {
2879 shift @$log;
2882 # print log
2883 my $signoff = 0;
2884 my $empty = 0;
2885 foreach my $line (@$log) {
2886 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2887 $signoff = 1;
2888 $empty = 0;
2889 if (! $opts{'-remove_signoff'}) {
2890 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2891 next;
2892 } else {
2893 # remove signoff lines
2894 next;
2896 } else {
2897 $signoff = 0;
2900 # print only one empty line
2901 # do not print empty line after signoff
2902 if ($line eq "") {
2903 next if ($empty || $signoff);
2904 $empty = 1;
2905 } else {
2906 $empty = 0;
2909 print format_log_line_html($line) . "<br/>\n";
2912 if ($opts{'-final_empty_line'}) {
2913 # end with single empty line
2914 print "<br/>\n" unless $empty;
2918 # return link target (what link points to)
2919 sub git_get_link_target {
2920 my $hash = shift;
2921 my $link_target;
2923 # read link
2924 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2925 or return;
2927 local $/;
2928 $link_target = <$fd>;
2930 close $fd
2931 or return;
2933 return $link_target;
2936 # given link target, and the directory (basedir) the link is in,
2937 # return target of link relative to top directory (top tree);
2938 # return undef if it is not possible (including absolute links).
2939 sub normalize_link_target {
2940 my ($link_target, $basedir, $hash_base) = @_;
2942 # we can normalize symlink target only if $hash_base is provided
2943 return unless $hash_base;
2945 # absolute symlinks (beginning with '/') cannot be normalized
2946 return if (substr($link_target, 0, 1) eq '/');
2948 # normalize link target to path from top (root) tree (dir)
2949 my $path;
2950 if ($basedir) {
2951 $path = $basedir . '/' . $link_target;
2952 } else {
2953 # we are in top (root) tree (dir)
2954 $path = $link_target;
2957 # remove //, /./, and /../
2958 my @path_parts;
2959 foreach my $part (split('/', $path)) {
2960 # discard '.' and ''
2961 next if (!$part || $part eq '.');
2962 # handle '..'
2963 if ($part eq '..') {
2964 if (@path_parts) {
2965 pop @path_parts;
2966 } else {
2967 # link leads outside repository (outside top dir)
2968 return;
2970 } else {
2971 push @path_parts, $part;
2974 $path = join('/', @path_parts);
2976 return $path;
2979 # print tree entry (row of git_tree), but without encompassing <tr> element
2980 sub git_print_tree_entry {
2981 my ($t, $basedir, $hash_base, $have_blame) = @_;
2983 my %base_key = ();
2984 $base_key{'hash_base'} = $hash_base if defined $hash_base;
2986 # The format of a table row is: mode list link. Where mode is
2987 # the mode of the entry, list is the name of the entry, an href,
2988 # and link is the action links of the entry.
2990 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2991 if ($t->{'type'} eq "blob") {
2992 print "<td class=\"list\">" .
2993 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
2994 file_name=>"$basedir$t->{'name'}", %base_key),
2995 -class => "list"}, esc_path($t->{'name'}));
2996 if (S_ISLNK(oct $t->{'mode'})) {
2997 my $link_target = git_get_link_target($t->{'hash'});
2998 if ($link_target) {
2999 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3000 if (defined $norm_target) {
3001 print " -> " .
3002 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3003 file_name=>$norm_target),
3004 -title => $norm_target}, esc_path($link_target));
3005 } else {
3006 print " -> " . esc_path($link_target);
3010 print "</td>\n";
3011 print "<td class=\"link\">";
3012 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3013 file_name=>"$basedir$t->{'name'}", %base_key)},
3014 "blob");
3015 if ($have_blame) {
3016 print " | " .
3017 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3018 file_name=>"$basedir$t->{'name'}", %base_key)},
3019 "blame");
3021 if (defined $hash_base) {
3022 print " | " .
3023 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3024 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3025 "history");
3027 print " | " .
3028 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3029 file_name=>"$basedir$t->{'name'}")},
3030 "raw");
3031 print "</td>\n";
3033 } elsif ($t->{'type'} eq "tree") {
3034 print "<td class=\"list\">";
3035 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3036 file_name=>"$basedir$t->{'name'}", %base_key)},
3037 esc_path($t->{'name'}));
3038 print "</td>\n";
3039 print "<td class=\"link\">";
3040 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3041 file_name=>"$basedir$t->{'name'}", %base_key)},
3042 "tree");
3043 if (defined $hash_base) {
3044 print " | " .
3045 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3046 file_name=>"$basedir$t->{'name'}")},
3047 "history");
3049 print "</td>\n";
3050 } else {
3051 # unknown object: we can only present history for it
3052 # (this includes 'commit' object, i.e. submodule support)
3053 print "<td class=\"list\">" .
3054 esc_path($t->{'name'}) .
3055 "</td>\n";
3056 print "<td class=\"link\">";
3057 if (defined $hash_base) {
3058 print $cgi->a({-href => href(action=>"history",
3059 hash_base=>$hash_base,
3060 file_name=>"$basedir$t->{'name'}")},
3061 "history");
3063 print "</td>\n";
3067 ## ......................................................................
3068 ## functions printing large fragments of HTML
3070 # get pre-image filenames for merge (combined) diff
3071 sub fill_from_file_info {
3072 my ($diff, @parents) = @_;
3074 $diff->{'from_file'} = [ ];
3075 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3076 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3077 if ($diff->{'status'}[$i] eq 'R' ||
3078 $diff->{'status'}[$i] eq 'C') {
3079 $diff->{'from_file'}[$i] =
3080 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3084 return $diff;
3087 # is current raw difftree line of file deletion
3088 sub is_deleted {
3089 my $diffinfo = shift;
3091 return $diffinfo->{'to_id'} eq ('0' x 40);
3094 # does patch correspond to [previous] difftree raw line
3095 # $diffinfo - hashref of parsed raw diff format
3096 # $patchinfo - hashref of parsed patch diff format
3097 # (the same keys as in $diffinfo)
3098 sub is_patch_split {
3099 my ($diffinfo, $patchinfo) = @_;
3101 return defined $diffinfo && defined $patchinfo
3102 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3106 sub git_difftree_body {
3107 my ($difftree, $hash, @parents) = @_;
3108 my ($parent) = $parents[0];
3109 my ($have_blame) = gitweb_check_feature('blame');
3110 print "<div class=\"list_head\">\n";
3111 if ($#{$difftree} > 10) {
3112 print(($#{$difftree} + 1) . " files changed:\n");
3114 print "</div>\n";
3116 print "<table class=\"" .
3117 (@parents > 1 ? "combined " : "") .
3118 "diff_tree\">\n";
3120 # header only for combined diff in 'commitdiff' view
3121 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3122 if ($has_header) {
3123 # table header
3124 print "<thead><tr>\n" .
3125 "<th></th><th></th>\n"; # filename, patchN link
3126 for (my $i = 0; $i < @parents; $i++) {
3127 my $par = $parents[$i];
3128 print "<th>" .
3129 $cgi->a({-href => href(action=>"commitdiff",
3130 hash=>$hash, hash_parent=>$par),
3131 -title => 'commitdiff to parent number ' .
3132 ($i+1) . ': ' . substr($par,0,7)},
3133 $i+1) .
3134 "&nbsp;</th>\n";
3136 print "</tr></thead>\n<tbody>\n";
3139 my $alternate = 1;
3140 my $patchno = 0;
3141 foreach my $line (@{$difftree}) {
3142 my $diff = parsed_difftree_line($line);
3144 if ($alternate) {
3145 print "<tr class=\"dark\">\n";
3146 } else {
3147 print "<tr class=\"light\">\n";
3149 $alternate ^= 1;
3151 if (exists $diff->{'nparents'}) { # combined diff
3153 fill_from_file_info($diff, @parents)
3154 unless exists $diff->{'from_file'};
3156 if (!is_deleted($diff)) {
3157 # file exists in the result (child) commit
3158 print "<td>" .
3159 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3160 file_name=>$diff->{'to_file'},
3161 hash_base=>$hash),
3162 -class => "list"}, esc_path($diff->{'to_file'})) .
3163 "</td>\n";
3164 } else {
3165 print "<td>" .
3166 esc_path($diff->{'to_file'}) .
3167 "</td>\n";
3170 if ($action eq 'commitdiff') {
3171 # link to patch
3172 $patchno++;
3173 print "<td class=\"link\">" .
3174 $cgi->a({-href => "#patch$patchno"}, "patch") .
3175 " | " .
3176 "</td>\n";
3179 my $has_history = 0;
3180 my $not_deleted = 0;
3181 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3182 my $hash_parent = $parents[$i];
3183 my $from_hash = $diff->{'from_id'}[$i];
3184 my $from_path = $diff->{'from_file'}[$i];
3185 my $status = $diff->{'status'}[$i];
3187 $has_history ||= ($status ne 'A');
3188 $not_deleted ||= ($status ne 'D');
3190 if ($status eq 'A') {
3191 print "<td class=\"link\" align=\"right\"> | </td>\n";
3192 } elsif ($status eq 'D') {
3193 print "<td class=\"link\">" .
3194 $cgi->a({-href => href(action=>"blob",
3195 hash_base=>$hash,
3196 hash=>$from_hash,
3197 file_name=>$from_path)},
3198 "blob" . ($i+1)) .
3199 " | </td>\n";
3200 } else {
3201 if ($diff->{'to_id'} eq $from_hash) {
3202 print "<td class=\"link nochange\">";
3203 } else {
3204 print "<td class=\"link\">";
3206 print $cgi->a({-href => href(action=>"blobdiff",
3207 hash=>$diff->{'to_id'},
3208 hash_parent=>$from_hash,
3209 hash_base=>$hash,
3210 hash_parent_base=>$hash_parent,
3211 file_name=>$diff->{'to_file'},
3212 file_parent=>$from_path)},
3213 "diff" . ($i+1)) .
3214 " | </td>\n";
3218 print "<td class=\"link\">";
3219 if ($not_deleted) {
3220 print $cgi->a({-href => href(action=>"blob",
3221 hash=>$diff->{'to_id'},
3222 file_name=>$diff->{'to_file'},
3223 hash_base=>$hash)},
3224 "blob");
3225 print " | " if ($has_history);
3227 if ($has_history) {
3228 print $cgi->a({-href => href(action=>"history",
3229 file_name=>$diff->{'to_file'},
3230 hash_base=>$hash)},
3231 "history");
3233 print "</td>\n";
3235 print "</tr>\n";
3236 next; # instead of 'else' clause, to avoid extra indent
3238 # else ordinary diff
3240 my ($to_mode_oct, $to_mode_str, $to_file_type);
3241 my ($from_mode_oct, $from_mode_str, $from_file_type);
3242 if ($diff->{'to_mode'} ne ('0' x 6)) {
3243 $to_mode_oct = oct $diff->{'to_mode'};
3244 if (S_ISREG($to_mode_oct)) { # only for regular file
3245 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3247 $to_file_type = file_type($diff->{'to_mode'});
3249 if ($diff->{'from_mode'} ne ('0' x 6)) {
3250 $from_mode_oct = oct $diff->{'from_mode'};
3251 if (S_ISREG($to_mode_oct)) { # only for regular file
3252 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3254 $from_file_type = file_type($diff->{'from_mode'});
3257 if ($diff->{'status'} eq "A") { # created
3258 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3259 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3260 $mode_chng .= "]</span>";
3261 print "<td>";
3262 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3263 hash_base=>$hash, file_name=>$diff->{'file'}),
3264 -class => "list"}, esc_path($diff->{'file'}));
3265 print "</td>\n";
3266 print "<td>$mode_chng</td>\n";
3267 print "<td class=\"link\">";
3268 if ($action eq 'commitdiff') {
3269 # link to patch
3270 $patchno++;
3271 print $cgi->a({-href => "#patch$patchno"}, "patch");
3272 print " | ";
3274 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3275 hash_base=>$hash, file_name=>$diff->{'file'})},
3276 "blob");
3277 print "</td>\n";
3279 } elsif ($diff->{'status'} eq "D") { # deleted
3280 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3281 print "<td>";
3282 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3283 hash_base=>$parent, file_name=>$diff->{'file'}),
3284 -class => "list"}, esc_path($diff->{'file'}));
3285 print "</td>\n";
3286 print "<td>$mode_chng</td>\n";
3287 print "<td class=\"link\">";
3288 if ($action eq 'commitdiff') {
3289 # link to patch
3290 $patchno++;
3291 print $cgi->a({-href => "#patch$patchno"}, "patch");
3292 print " | ";
3294 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3295 hash_base=>$parent, file_name=>$diff->{'file'})},
3296 "blob") . " | ";
3297 if ($have_blame) {
3298 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3299 file_name=>$diff->{'file'})},
3300 "blame") . " | ";
3302 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3303 file_name=>$diff->{'file'})},
3304 "history");
3305 print "</td>\n";
3307 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3308 my $mode_chnge = "";
3309 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3310 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3311 if ($from_file_type ne $to_file_type) {
3312 $mode_chnge .= " from $from_file_type to $to_file_type";
3314 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3315 if ($from_mode_str && $to_mode_str) {
3316 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3317 } elsif ($to_mode_str) {
3318 $mode_chnge .= " mode: $to_mode_str";
3321 $mode_chnge .= "]</span>\n";
3323 print "<td>";
3324 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3325 hash_base=>$hash, file_name=>$diff->{'file'}),
3326 -class => "list"}, esc_path($diff->{'file'}));
3327 print "</td>\n";
3328 print "<td>$mode_chnge</td>\n";
3329 print "<td class=\"link\">";
3330 if ($action eq 'commitdiff') {
3331 # link to patch
3332 $patchno++;
3333 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3334 " | ";
3335 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3336 # "commit" view and modified file (not onlu mode changed)
3337 print $cgi->a({-href => href(action=>"blobdiff",
3338 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3339 hash_base=>$hash, hash_parent_base=>$parent,
3340 file_name=>$diff->{'file'})},
3341 "diff") .
3342 " | ";
3344 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3345 hash_base=>$hash, file_name=>$diff->{'file'})},
3346 "blob") . " | ";
3347 if ($have_blame) {
3348 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3349 file_name=>$diff->{'file'})},
3350 "blame") . " | ";
3352 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3353 file_name=>$diff->{'file'})},
3354 "history");
3355 print "</td>\n";
3357 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3358 my %status_name = ('R' => 'moved', 'C' => 'copied');
3359 my $nstatus = $status_name{$diff->{'status'}};
3360 my $mode_chng = "";
3361 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3362 # mode also for directories, so we cannot use $to_mode_str
3363 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3365 print "<td>" .
3366 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3367 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3368 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3369 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3370 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3371 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3372 -class => "list"}, esc_path($diff->{'from_file'})) .
3373 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3374 "<td class=\"link\">";
3375 if ($action eq 'commitdiff') {
3376 # link to patch
3377 $patchno++;
3378 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3379 " | ";
3380 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3381 # "commit" view and modified file (not only pure rename or copy)
3382 print $cgi->a({-href => href(action=>"blobdiff",
3383 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3384 hash_base=>$hash, hash_parent_base=>$parent,
3385 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3386 "diff") .
3387 " | ";
3389 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3390 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3391 "blob") . " | ";
3392 if ($have_blame) {
3393 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3394 file_name=>$diff->{'to_file'})},
3395 "blame") . " | ";
3397 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3398 file_name=>$diff->{'to_file'})},
3399 "history");
3400 print "</td>\n";
3402 } # we should not encounter Unmerged (U) or Unknown (X) status
3403 print "</tr>\n";
3405 print "</tbody>" if $has_header;
3406 print "</table>\n";
3409 sub git_patchset_body {
3410 my ($fd, $difftree, $hash, @hash_parents) = @_;
3411 my ($hash_parent) = $hash_parents[0];
3413 my $is_combined = (@hash_parents > 1);
3414 my $patch_idx = 0;
3415 my $patch_number = 0;
3416 my $patch_line;
3417 my $diffinfo;
3418 my $to_name;
3419 my (%from, %to);
3421 print "<div class=\"patchset\">\n";
3423 # skip to first patch
3424 while ($patch_line = <$fd>) {
3425 chomp $patch_line;
3427 last if ($patch_line =~ m/^diff /);
3430 PATCH:
3431 while ($patch_line) {
3433 # parse "git diff" header line
3434 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3435 # $1 is from_name, which we do not use
3436 $to_name = unquote($2);
3437 $to_name =~ s!^b/!!;
3438 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3439 # $1 is 'cc' or 'combined', which we do not use
3440 $to_name = unquote($2);
3441 } else {
3442 $to_name = undef;
3445 # check if current patch belong to current raw line
3446 # and parse raw git-diff line if needed
3447 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3448 # this is continuation of a split patch
3449 print "<div class=\"patch cont\">\n";
3450 } else {
3451 # advance raw git-diff output if needed
3452 $patch_idx++ if defined $diffinfo;
3454 # read and prepare patch information
3455 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3457 # compact combined diff output can have some patches skipped
3458 # find which patch (using pathname of result) we are at now;
3459 if ($is_combined) {
3460 while ($to_name ne $diffinfo->{'to_file'}) {
3461 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3462 format_diff_cc_simplified($diffinfo, @hash_parents) .
3463 "</div>\n"; # class="patch"
3465 $patch_idx++;
3466 $patch_number++;
3468 last if $patch_idx > $#$difftree;
3469 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3473 # modifies %from, %to hashes
3474 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3476 # this is first patch for raw difftree line with $patch_idx index
3477 # we index @$difftree array from 0, but number patches from 1
3478 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3481 # git diff header
3482 #assert($patch_line =~ m/^diff /) if DEBUG;
3483 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3484 $patch_number++;
3485 # print "git diff" header
3486 print format_git_diff_header_line($patch_line, $diffinfo,
3487 \%from, \%to);
3489 # print extended diff header
3490 print "<div class=\"diff extended_header\">\n";
3491 EXTENDED_HEADER:
3492 while ($patch_line = <$fd>) {
3493 chomp $patch_line;
3495 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3497 print format_extended_diff_header_line($patch_line, $diffinfo,
3498 \%from, \%to);
3500 print "</div>\n"; # class="diff extended_header"
3502 # from-file/to-file diff header
3503 if (! $patch_line) {
3504 print "</div>\n"; # class="patch"
3505 last PATCH;
3507 next PATCH if ($patch_line =~ m/^diff /);
3508 #assert($patch_line =~ m/^---/) if DEBUG;
3510 my $last_patch_line = $patch_line;
3511 $patch_line = <$fd>;
3512 chomp $patch_line;
3513 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3515 print format_diff_from_to_header($last_patch_line, $patch_line,
3516 $diffinfo, \%from, \%to,
3517 @hash_parents);
3519 # the patch itself
3520 LINE:
3521 while ($patch_line = <$fd>) {
3522 chomp $patch_line;
3524 next PATCH if ($patch_line =~ m/^diff /);
3526 print format_diff_line($patch_line, \%from, \%to);
3529 } continue {
3530 print "</div>\n"; # class="patch"
3533 # for compact combined (--cc) format, with chunk and patch simpliciaction
3534 # patchset might be empty, but there might be unprocessed raw lines
3535 for (++$patch_idx if $patch_number > 0;
3536 $patch_idx < @$difftree;
3537 ++$patch_idx) {
3538 # read and prepare patch information
3539 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3541 # generate anchor for "patch" links in difftree / whatchanged part
3542 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3543 format_diff_cc_simplified($diffinfo, @hash_parents) .
3544 "</div>\n"; # class="patch"
3546 $patch_number++;
3549 if ($patch_number == 0) {
3550 if (@hash_parents > 1) {
3551 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3552 } else {
3553 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3557 print "</div>\n"; # class="patchset"
3560 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3562 # fills project list info (age, description, owner, forks) for each
3563 # project in the list, removing invalid projects from returned list
3564 # NOTE: modifies $projlist, but does not remove entries from it
3565 sub fill_project_list_info {
3566 my ($projlist, $check_forks) = @_;
3567 my @projects;
3569 PROJECT:
3570 foreach my $pr (@$projlist) {
3571 my (@activity) = git_get_last_activity($pr->{'path'});
3572 unless (@activity) {
3573 next PROJECT;
3575 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3576 if (!defined $pr->{'descr'}) {
3577 my $descr = git_get_project_description($pr->{'path'}) || "";
3578 $descr = to_utf8($descr);
3579 $pr->{'descr_long'} = $descr;
3580 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3582 if (!defined $pr->{'owner'}) {
3583 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3585 if ($check_forks) {
3586 my $pname = $pr->{'path'};
3587 if (($pname =~ s/\.git$//) &&
3588 ($pname !~ /\/$/) &&
3589 (-d "$projectroot/$pname")) {
3590 $pr->{'forks'} = "-d $projectroot/$pname";
3591 } else {
3592 $pr->{'forks'} = 0;
3595 push @projects, $pr;
3598 return @projects;
3601 sub git_project_list_body {
3602 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3604 my ($check_forks) = gitweb_check_feature('forks');
3605 my @projects = fill_project_list_info($projlist, $check_forks);
3607 $order ||= $default_projects_order;
3608 $from = 0 unless defined $from;
3609 $to = $#projects if (!defined $to || $#projects < $to);
3611 print "<table class=\"project_list\">\n";
3612 unless ($no_header) {
3613 print "<tr>\n";
3614 if ($check_forks) {
3615 print "<th></th>\n";
3617 if ($order eq "project") {
3618 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
3619 print "<th>Project</th>\n";
3620 } else {
3621 print "<th>" .
3622 $cgi->a({-href => href(project=>undef, order=>'project'),
3623 -class => "header"}, "Project") .
3624 "</th>\n";
3626 if ($order eq "descr") {
3627 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
3628 print "<th>Description</th>\n";
3629 } else {
3630 print "<th>" .
3631 $cgi->a({-href => href(project=>undef, order=>'descr'),
3632 -class => "header"}, "Description") .
3633 "</th>\n";
3635 if ($order eq "owner") {
3636 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
3637 print "<th>Owner</th>\n";
3638 } else {
3639 print "<th>" .
3640 $cgi->a({-href => href(project=>undef, order=>'owner'),
3641 -class => "header"}, "Owner") .
3642 "</th>\n";
3644 if ($order eq "age") {
3645 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
3646 print "<th>Last Change</th>\n";
3647 } else {
3648 print "<th>" .
3649 $cgi->a({-href => href(project=>undef, order=>'age'),
3650 -class => "header"}, "Last Change") .
3651 "</th>\n";
3653 print "<th></th>\n" .
3654 "</tr>\n";
3656 my $alternate = 1;
3657 for (my $i = $from; $i <= $to; $i++) {
3658 my $pr = $projects[$i];
3659 if ($alternate) {
3660 print "<tr class=\"dark\">\n";
3661 } else {
3662 print "<tr class=\"light\">\n";
3664 $alternate ^= 1;
3665 if ($check_forks) {
3666 print "<td>";
3667 if ($pr->{'forks'}) {
3668 print "<!-- $pr->{'forks'} -->\n";
3669 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3671 print "</td>\n";
3673 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3674 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3675 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3676 -class => "list", -title => $pr->{'descr_long'}},
3677 esc_html($pr->{'descr'})) . "</td>\n" .
3678 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3679 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3680 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3681 "<td class=\"link\">" .
3682 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3683 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3684 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3685 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3686 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3687 "</td>\n" .
3688 "</tr>\n";
3690 if (defined $extra) {
3691 print "<tr>\n";
3692 if ($check_forks) {
3693 print "<td></td>\n";
3695 print "<td colspan=\"5\">$extra</td>\n" .
3696 "</tr>\n";
3698 print "</table>\n";
3701 sub git_shortlog_body {
3702 # uses global variable $project
3703 my ($commitlist, $from, $to, $refs, $extra) = @_;
3705 $from = 0 unless defined $from;
3706 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3708 print "<table class=\"shortlog\">\n";
3709 my $alternate = 1;
3710 for (my $i = $from; $i <= $to; $i++) {
3711 my %co = %{$commitlist->[$i]};
3712 my $commit = $co{'id'};
3713 my $ref = format_ref_marker($refs, $commit);
3714 if ($alternate) {
3715 print "<tr class=\"dark\">\n";
3716 } else {
3717 print "<tr class=\"light\">\n";
3719 $alternate ^= 1;
3720 my $author = chop_and_escape_str($co{'author_name'}, 10);
3721 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3722 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3723 "<td><i>" . $author . "</i></td>\n" .
3724 "<td>";
3725 print format_subject_html($co{'title'}, $co{'title_short'},
3726 href(action=>"commit", hash=>$commit), $ref);
3727 print "</td>\n" .
3728 "<td class=\"link\">" .
3729 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3730 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3731 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3732 my $snapshot_links = format_snapshot_links($commit);
3733 if (defined $snapshot_links) {
3734 print " | " . $snapshot_links;
3736 print "</td>\n" .
3737 "</tr>\n";
3739 if (defined $extra) {
3740 print "<tr>\n" .
3741 "<td colspan=\"4\">$extra</td>\n" .
3742 "</tr>\n";
3744 print "</table>\n";
3747 sub git_history_body {
3748 # Warning: assumes constant type (blob or tree) during history
3749 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3751 $from = 0 unless defined $from;
3752 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3754 print "<table class=\"history\">\n";
3755 my $alternate = 1;
3756 for (my $i = $from; $i <= $to; $i++) {
3757 my %co = %{$commitlist->[$i]};
3758 if (!%co) {
3759 next;
3761 my $commit = $co{'id'};
3763 my $ref = format_ref_marker($refs, $commit);
3765 if ($alternate) {
3766 print "<tr class=\"dark\">\n";
3767 } else {
3768 print "<tr class=\"light\">\n";
3770 $alternate ^= 1;
3771 # shortlog uses chop_str($co{'author_name'}, 10)
3772 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3773 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3774 "<td><i>" . $author . "</i></td>\n" .
3775 "<td>";
3776 # originally git_history used chop_str($co{'title'}, 50)
3777 print format_subject_html($co{'title'}, $co{'title_short'},
3778 href(action=>"commit", hash=>$commit), $ref);
3779 print "</td>\n" .
3780 "<td class=\"link\">" .
3781 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3782 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3784 if ($ftype eq 'blob') {
3785 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3786 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3787 if (defined $blob_current && defined $blob_parent &&
3788 $blob_current ne $blob_parent) {
3789 print " | " .
3790 $cgi->a({-href => href(action=>"blobdiff",
3791 hash=>$blob_current, hash_parent=>$blob_parent,
3792 hash_base=>$hash_base, hash_parent_base=>$commit,
3793 file_name=>$file_name)},
3794 "diff to current");
3797 print "</td>\n" .
3798 "</tr>\n";
3800 if (defined $extra) {
3801 print "<tr>\n" .
3802 "<td colspan=\"4\">$extra</td>\n" .
3803 "</tr>\n";
3805 print "</table>\n";
3808 sub git_tags_body {
3809 # uses global variable $project
3810 my ($taglist, $from, $to, $extra) = @_;
3811 $from = 0 unless defined $from;
3812 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3814 print "<table class=\"tags\">\n";
3815 my $alternate = 1;
3816 for (my $i = $from; $i <= $to; $i++) {
3817 my $entry = $taglist->[$i];
3818 my %tag = %$entry;
3819 my $comment = $tag{'subject'};
3820 my $comment_short;
3821 if (defined $comment) {
3822 $comment_short = chop_str($comment, 30, 5);
3824 if ($alternate) {
3825 print "<tr class=\"dark\">\n";
3826 } else {
3827 print "<tr class=\"light\">\n";
3829 $alternate ^= 1;
3830 if (defined $tag{'age'}) {
3831 print "<td><i>$tag{'age'}</i></td>\n";
3832 } else {
3833 print "<td></td>\n";
3835 print "<td>" .
3836 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3837 -class => "list name"}, esc_html($tag{'name'})) .
3838 "</td>\n" .
3839 "<td>";
3840 if (defined $comment) {
3841 print format_subject_html($comment, $comment_short,
3842 href(action=>"tag", hash=>$tag{'id'}));
3844 print "</td>\n" .
3845 "<td class=\"selflink\">";
3846 if ($tag{'type'} eq "tag") {
3847 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3848 } else {
3849 print "&nbsp;";
3851 print "</td>\n" .
3852 "<td class=\"link\">" . " | " .
3853 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3854 if ($tag{'reftype'} eq "commit") {
3855 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
3856 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
3857 } elsif ($tag{'reftype'} eq "blob") {
3858 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3860 print "</td>\n" .
3861 "</tr>";
3863 if (defined $extra) {
3864 print "<tr>\n" .
3865 "<td colspan=\"5\">$extra</td>\n" .
3866 "</tr>\n";
3868 print "</table>\n";
3871 sub git_heads_body {
3872 # uses global variable $project
3873 my ($headlist, $head, $from, $to, $extra) = @_;
3874 $from = 0 unless defined $from;
3875 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3877 print "<table class=\"heads\">\n";
3878 my $alternate = 1;
3879 for (my $i = $from; $i <= $to; $i++) {
3880 my $entry = $headlist->[$i];
3881 my %ref = %$entry;
3882 my $curr = $ref{'id'} eq $head;
3883 if ($alternate) {
3884 print "<tr class=\"dark\">\n";
3885 } else {
3886 print "<tr class=\"light\">\n";
3888 $alternate ^= 1;
3889 print "<td><i>$ref{'age'}</i></td>\n" .
3890 ($curr ? "<td class=\"current_head\">" : "<td>") .
3891 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
3892 -class => "list name"},esc_html($ref{'name'})) .
3893 "</td>\n" .
3894 "<td class=\"link\">" .
3895 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
3896 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
3897 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
3898 "</td>\n" .
3899 "</tr>";
3901 if (defined $extra) {
3902 print "<tr>\n" .
3903 "<td colspan=\"3\">$extra</td>\n" .
3904 "</tr>\n";
3906 print "</table>\n";
3909 sub git_search_grep_body {
3910 my ($commitlist, $from, $to, $extra) = @_;
3911 $from = 0 unless defined $from;
3912 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3914 print "<table class=\"commit_search\">\n";
3915 my $alternate = 1;
3916 for (my $i = $from; $i <= $to; $i++) {
3917 my %co = %{$commitlist->[$i]};
3918 if (!%co) {
3919 next;
3921 my $commit = $co{'id'};
3922 if ($alternate) {
3923 print "<tr class=\"dark\">\n";
3924 } else {
3925 print "<tr class=\"light\">\n";
3927 $alternate ^= 1;
3928 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3929 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3930 "<td><i>" . $author . "</i></td>\n" .
3931 "<td>" .
3932 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3933 -class => "list subject"},
3934 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3935 my $comment = $co{'comment'};
3936 foreach my $line (@$comment) {
3937 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
3938 my ($lead, $match, $trail) = ($1, $2, $3);
3939 $match = chop_str($match, 70, 5, 'center');
3940 my $contextlen = int((80 - length($match))/2);
3941 $contextlen = 30 if ($contextlen > 30);
3942 $lead = chop_str($lead, $contextlen, 10, 'left');
3943 $trail = chop_str($trail, $contextlen, 10, 'right');
3945 $lead = esc_html($lead);
3946 $match = esc_html($match);
3947 $trail = esc_html($trail);
3949 print "$lead<span class=\"match\">$match</span>$trail<br />";
3952 print "</td>\n" .
3953 "<td class=\"link\">" .
3954 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3955 " | " .
3956 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
3957 " | " .
3958 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3959 print "</td>\n" .
3960 "</tr>\n";
3962 if (defined $extra) {
3963 print "<tr>\n" .
3964 "<td colspan=\"3\">$extra</td>\n" .
3965 "</tr>\n";
3967 print "</table>\n";
3970 ## ======================================================================
3971 ## ======================================================================
3972 ## actions
3974 sub git_project_list {
3975 my $order = $cgi->param('o');
3976 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
3977 die_error(undef, "Unknown order parameter");
3980 my @list = git_get_projects_list();
3981 if (!@list) {
3982 die_error(undef, "No projects found");
3985 git_header_html();
3986 if (-f $home_text) {
3987 print "<div class=\"index_include\">\n";
3988 open (my $fd, $home_text);
3989 print <$fd>;
3990 close $fd;
3991 print "</div>\n";
3993 git_project_list_body(\@list, $order);
3994 git_footer_html();
3997 sub git_forks {
3998 my $order = $cgi->param('o');
3999 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4000 die_error(undef, "Unknown order parameter");
4003 my @list = git_get_projects_list($project);
4004 if (!@list) {
4005 die_error(undef, "No forks found");
4008 git_header_html();
4009 git_print_page_nav('','');
4010 git_print_header_div('summary', "$project forks");
4011 git_project_list_body(\@list, $order);
4012 git_footer_html();
4015 sub git_project_index {
4016 my @projects = git_get_projects_list($project);
4018 print $cgi->header(
4019 -type => 'text/plain',
4020 -charset => 'utf-8',
4021 -content_disposition => 'inline; filename="index.aux"');
4023 foreach my $pr (@projects) {
4024 if (!exists $pr->{'owner'}) {
4025 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4028 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4029 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4030 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4031 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4032 $path =~ s/ /\+/g;
4033 $owner =~ s/ /\+/g;
4035 print "$path $owner\n";
4039 sub git_summary {
4040 my $descr = git_get_project_description($project) || "none";
4041 my %co = parse_commit("HEAD");
4042 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4043 my $head = $co{'id'};
4045 my $owner = git_get_project_owner($project);
4047 my $refs = git_get_references();
4048 # These get_*_list functions return one more to allow us to see if
4049 # there are more ...
4050 my @taglist = git_get_tags_list(16);
4051 my @headlist = git_get_heads_list(16);
4052 my @forklist;
4053 my ($check_forks) = gitweb_check_feature('forks');
4055 if ($check_forks) {
4056 @forklist = git_get_projects_list($project);
4059 git_header_html();
4060 git_print_page_nav('summary','', $head);
4062 print "<div class=\"title\">&nbsp;</div>\n";
4063 print "<table class=\"projects_list\">\n" .
4064 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4065 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4066 if (defined $cd{'rfc2822'}) {
4067 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4070 # use per project git URL list in $projectroot/$project/cloneurl
4071 # or make project git URL from git base URL and project name
4072 my $url_tag = "URL";
4073 my @url_list = git_get_project_url_list($project);
4074 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4075 foreach my $git_url (@url_list) {
4076 next unless $git_url;
4077 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
4078 $url_tag = "";
4080 print "</table>\n";
4082 if (-s "$projectroot/$project/README.html") {
4083 if (open my $fd, "$projectroot/$project/README.html") {
4084 print "<div class=\"title\">readme</div>\n" .
4085 "<div class=\"readme\">\n";
4086 print $_ while (<$fd>);
4087 print "\n</div>\n"; # class="readme"
4088 close $fd;
4092 # we need to request one more than 16 (0..15) to check if
4093 # those 16 are all
4094 my @commitlist = $head ? parse_commits($head, 17) : ();
4095 if (@commitlist) {
4096 git_print_header_div('shortlog');
4097 git_shortlog_body(\@commitlist, 0, 15, $refs,
4098 $#commitlist <= 15 ? undef :
4099 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4102 if (@taglist) {
4103 git_print_header_div('tags');
4104 git_tags_body(\@taglist, 0, 15,
4105 $#taglist <= 15 ? undef :
4106 $cgi->a({-href => href(action=>"tags")}, "..."));
4109 if (@headlist) {
4110 git_print_header_div('heads');
4111 git_heads_body(\@headlist, $head, 0, 15,
4112 $#headlist <= 15 ? undef :
4113 $cgi->a({-href => href(action=>"heads")}, "..."));
4116 if (@forklist) {
4117 git_print_header_div('forks');
4118 git_project_list_body(\@forklist, undef, 0, 15,
4119 $#forklist <= 15 ? undef :
4120 $cgi->a({-href => href(action=>"forks")}, "..."),
4121 'noheader');
4124 git_footer_html();
4127 sub git_tag {
4128 my $head = git_get_head_hash($project);
4129 git_header_html();
4130 git_print_page_nav('','', $head,undef,$head);
4131 my %tag = parse_tag($hash);
4133 if (! %tag) {
4134 die_error(undef, "Unknown tag object");
4137 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4138 print "<div class=\"title_text\">\n" .
4139 "<table class=\"object_header\">\n" .
4140 "<tr>\n" .
4141 "<td>object</td>\n" .
4142 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4143 $tag{'object'}) . "</td>\n" .
4144 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4145 $tag{'type'}) . "</td>\n" .
4146 "</tr>\n";
4147 if (defined($tag{'author'})) {
4148 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4149 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4150 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4151 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4152 "</td></tr>\n";
4154 print "</table>\n\n" .
4155 "</div>\n";
4156 print "<div class=\"page_body\">";
4157 my $comment = $tag{'comment'};
4158 foreach my $line (@$comment) {
4159 chomp $line;
4160 print esc_html($line, -nbsp=>1) . "<br/>\n";
4162 print "</div>\n";
4163 git_footer_html();
4166 sub git_blame2 {
4167 my $fd;
4168 my $ftype;
4170 my ($have_blame) = gitweb_check_feature('blame');
4171 if (!$have_blame) {
4172 die_error('403 Permission denied', "Permission denied");
4174 die_error('404 Not Found', "File name not defined") if (!$file_name);
4175 $hash_base ||= git_get_head_hash($project);
4176 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4177 my %co = parse_commit($hash_base)
4178 or die_error(undef, "Reading commit failed");
4179 if (!defined $hash) {
4180 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4181 or die_error(undef, "Error looking up file");
4183 $ftype = git_get_type($hash);
4184 if ($ftype !~ "blob") {
4185 die_error('400 Bad Request', "Object is not a blob");
4187 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4188 $file_name, $hash_base)
4189 or die_error(undef, "Open git-blame failed");
4190 git_header_html();
4191 my $formats_nav =
4192 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4193 "blob") .
4194 " | " .
4195 $cgi->a({-href => href(action=>"history", -replay=>1)},
4196 "history") .
4197 " | " .
4198 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4199 "HEAD");
4200 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4201 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4202 git_print_page_path($file_name, $ftype, $hash_base);
4203 my @rev_color = (qw(light2 dark2));
4204 my $num_colors = scalar(@rev_color);
4205 my $current_color = 0;
4206 my $last_rev;
4207 print <<HTML;
4208 <div class="page_body">
4209 <table class="blame">
4210 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4211 HTML
4212 my %metainfo = ();
4213 while (1) {
4214 $_ = <$fd>;
4215 last unless defined $_;
4216 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4217 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4218 if (!exists $metainfo{$full_rev}) {
4219 $metainfo{$full_rev} = {};
4221 my $meta = $metainfo{$full_rev};
4222 while (<$fd>) {
4223 last if (s/^\t//);
4224 if (/^(\S+) (.*)$/) {
4225 $meta->{$1} = $2;
4228 my $data = $_;
4229 chomp $data;
4230 my $rev = substr($full_rev, 0, 8);
4231 my $author = $meta->{'author'};
4232 my %date = parse_date($meta->{'author-time'},
4233 $meta->{'author-tz'});
4234 my $date = $date{'iso-tz'};
4235 if ($group_size) {
4236 $current_color = ++$current_color % $num_colors;
4238 print "<tr class=\"$rev_color[$current_color]\">\n";
4239 if ($group_size) {
4240 print "<td class=\"sha1\"";
4241 print " title=\"". esc_html($author) . ", $date\"";
4242 print " rowspan=\"$group_size\"" if ($group_size > 1);
4243 print ">";
4244 print $cgi->a({-href => href(action=>"commit",
4245 hash=>$full_rev,
4246 file_name=>$file_name)},
4247 esc_html($rev));
4248 print "</td>\n";
4250 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4251 or die_error(undef, "Open git-rev-parse failed");
4252 my $parent_commit = <$dd>;
4253 close $dd;
4254 chomp($parent_commit);
4255 my $blamed = href(action => 'blame',
4256 file_name => $meta->{'filename'},
4257 hash_base => $parent_commit);
4258 print "<td class=\"linenr\">";
4259 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4260 -id => "l$lineno",
4261 -class => "linenr" },
4262 esc_html($lineno));
4263 print "</td>";
4264 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4265 print "</tr>\n";
4267 print "</table>\n";
4268 print "</div>";
4269 close $fd
4270 or print "Reading blob failed\n";
4271 git_footer_html();
4274 sub git_blame {
4275 my $fd;
4277 my ($have_blame) = gitweb_check_feature('blame');
4278 if (!$have_blame) {
4279 die_error('403 Permission denied', "Permission denied");
4281 die_error('404 Not Found', "File name not defined") if (!$file_name);
4282 $hash_base ||= git_get_head_hash($project);
4283 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4284 my %co = parse_commit($hash_base)
4285 or die_error(undef, "Reading commit failed");
4286 if (!defined $hash) {
4287 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4288 or die_error(undef, "Error lookup file");
4290 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
4291 or die_error(undef, "Open git-annotate failed");
4292 git_header_html();
4293 my $formats_nav =
4294 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4295 "blob") .
4296 " | " .
4297 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
4298 "history") .
4299 " | " .
4300 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4301 "HEAD");
4302 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4303 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4304 git_print_page_path($file_name, 'blob', $hash_base);
4305 print "<div class=\"page_body\">\n";
4306 print <<HTML;
4307 <table class="blame">
4308 <tr>
4309 <th>Commit</th>
4310 <th>Age</th>
4311 <th>Author</th>
4312 <th>Line</th>
4313 <th>Data</th>
4314 </tr>
4315 HTML
4316 my @line_class = (qw(light dark));
4317 my $line_class_len = scalar (@line_class);
4318 my $line_class_num = $#line_class;
4319 while (my $line = <$fd>) {
4320 my $long_rev;
4321 my $short_rev;
4322 my $author;
4323 my $time;
4324 my $lineno;
4325 my $data;
4326 my $age;
4327 my $age_str;
4328 my $age_class;
4330 chomp $line;
4331 $line_class_num = ($line_class_num + 1) % $line_class_len;
4333 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
4334 $long_rev = $1;
4335 $author = $2;
4336 $time = $3;
4337 $lineno = $4;
4338 $data = $5;
4339 } else {
4340 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
4341 next;
4343 $short_rev = substr ($long_rev, 0, 8);
4344 $age = time () - $time;
4345 $age_str = age_string ($age);
4346 $age_str =~ s/ /&nbsp;/g;
4347 $age_class = age_class($age);
4348 $author = esc_html ($author);
4349 $author =~ s/ /&nbsp;/g;
4351 $data = untabify($data);
4352 $data = esc_html ($data);
4354 print <<HTML;
4355 <tr class="$line_class[$line_class_num]">
4356 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
4357 <td class="$age_class">$age_str</td>
4358 <td>$author</td>
4359 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
4360 <td class="pre">$data</td>
4361 </tr>
4362 HTML
4363 } # while (my $line = <$fd>)
4364 print "</table>\n\n";
4365 close $fd
4366 or print "Reading blob failed.\n";
4367 print "</div>";
4368 git_footer_html();
4371 sub git_tags {
4372 my $head = git_get_head_hash($project);
4373 git_header_html();
4374 git_print_page_nav('','', $head,undef,$head);
4375 git_print_header_div('summary', $project);
4377 my @tagslist = git_get_tags_list();
4378 if (@tagslist) {
4379 git_tags_body(\@tagslist);
4381 git_footer_html();
4384 sub git_heads {
4385 my $head = git_get_head_hash($project);
4386 git_header_html();
4387 git_print_page_nav('','', $head,undef,$head);
4388 git_print_header_div('summary', $project);
4390 my @headslist = git_get_heads_list();
4391 if (@headslist) {
4392 git_heads_body(\@headslist, $head);
4394 git_footer_html();
4397 sub git_blob_plain {
4398 my $type = shift;
4399 my $expires;
4401 if (!defined $hash) {
4402 if (defined $file_name) {
4403 my $base = $hash_base || git_get_head_hash($project);
4404 $hash = git_get_hash_by_path($base, $file_name, "blob")
4405 or die_error(undef, "Error lookup file");
4406 } else {
4407 die_error(undef, "No file name defined");
4409 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4410 # blobs defined by non-textual hash id's can be cached
4411 $expires = "+1d";
4414 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4415 or die_error(undef, "Open git-cat-file blob '$hash' failed");
4417 # content-type (can include charset)
4418 $type = blob_contenttype($fd, $file_name, $type);
4420 # "save as" filename, even when no $file_name is given
4421 my $save_as = "$hash";
4422 if (defined $file_name) {
4423 $save_as = $file_name;
4424 } elsif ($type =~ m/^text\//) {
4425 $save_as .= '.txt';
4428 print $cgi->header(
4429 -type => $type,
4430 -expires => $expires,
4431 -content_disposition => 'inline; filename="' . $save_as . '"');
4432 undef $/;
4433 binmode STDOUT, ':raw';
4434 print <$fd>;
4435 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4436 $/ = "\n";
4437 close $fd;
4440 sub git_blob {
4441 my $expires;
4443 if (!defined $hash) {
4444 if (defined $file_name) {
4445 my $base = $hash_base || git_get_head_hash($project);
4446 $hash = git_get_hash_by_path($base, $file_name, "blob")
4447 or die_error(undef, "Error lookup file");
4448 } else {
4449 die_error(undef, "No file name defined");
4451 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4452 # blobs defined by non-textual hash id's can be cached
4453 $expires = "+1d";
4456 my ($have_blame) = gitweb_check_feature('blame');
4457 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4458 or die_error(undef, "Couldn't cat $file_name, $hash");
4459 my $mimetype = blob_mimetype($fd, $file_name);
4460 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4461 close $fd;
4462 return git_blob_plain($mimetype);
4464 # we can have blame only for text/* mimetype
4465 $have_blame &&= ($mimetype =~ m!^text/!);
4467 git_header_html(undef, $expires);
4468 my $formats_nav = '';
4469 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4470 if (defined $file_name) {
4471 if ($have_blame) {
4472 $formats_nav .=
4473 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4474 "blame") .
4475 " | ";
4477 $formats_nav .=
4478 $cgi->a({-href => href(action=>"history", -replay=>1)},
4479 "history") .
4480 " | " .
4481 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4482 "raw") .
4483 " | " .
4484 $cgi->a({-href => href(action=>"blob",
4485 hash_base=>"HEAD", file_name=>$file_name)},
4486 "HEAD");
4487 } else {
4488 $formats_nav .=
4489 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4490 "raw");
4492 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4493 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4494 } else {
4495 print "<div class=\"page_nav\">\n" .
4496 "<br/><br/></div>\n" .
4497 "<div class=\"title\">$hash</div>\n";
4499 git_print_page_path($file_name, "blob", $hash_base);
4500 print "<div class=\"page_body\">\n";
4501 if ($mimetype =~ m!^image/!) {
4502 print qq!<img type="$mimetype"!;
4503 if ($file_name) {
4504 print qq! alt="$file_name" title="$file_name"!;
4506 print qq! src="! .
4507 href(action=>"blob_plain", hash=>$hash,
4508 hash_base=>$hash_base, file_name=>$file_name) .
4509 qq!" />\n!;
4510 } else {
4511 my $nr;
4512 while (my $line = <$fd>) {
4513 chomp $line;
4514 $nr++;
4515 $line = untabify($line);
4516 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4517 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4520 close $fd
4521 or print "Reading blob failed.\n";
4522 print "</div>";
4523 git_footer_html();
4526 sub git_tree {
4527 if (!defined $hash_base) {
4528 $hash_base = "HEAD";
4530 if (!defined $hash) {
4531 if (defined $file_name) {
4532 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4533 } else {
4534 $hash = $hash_base;
4537 $/ = "\0";
4538 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4539 or die_error(undef, "Open git-ls-tree failed");
4540 my @entries = map { chomp; $_ } <$fd>;
4541 close $fd or die_error(undef, "Reading tree failed");
4542 $/ = "\n";
4544 my $refs = git_get_references();
4545 my $ref = format_ref_marker($refs, $hash_base);
4546 git_header_html();
4547 my $basedir = '';
4548 my ($have_blame) = gitweb_check_feature('blame');
4549 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4550 my @views_nav = ();
4551 if (defined $file_name) {
4552 push @views_nav,
4553 $cgi->a({-href => href(action=>"history", -replay=>1)},
4554 "history"),
4555 $cgi->a({-href => href(action=>"tree",
4556 hash_base=>"HEAD", file_name=>$file_name)},
4557 "HEAD"),
4559 my $snapshot_links = format_snapshot_links($hash);
4560 if (defined $snapshot_links) {
4561 # FIXME: Should be available when we have no hash base as well.
4562 push @views_nav, $snapshot_links;
4564 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4565 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4566 } else {
4567 undef $hash_base;
4568 print "<div class=\"page_nav\">\n";
4569 print "<br/><br/></div>\n";
4570 print "<div class=\"title\">$hash</div>\n";
4572 if (defined $file_name) {
4573 $basedir = $file_name;
4574 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4575 $basedir .= '/';
4578 git_print_page_path($file_name, 'tree', $hash_base);
4579 print "<div class=\"page_body\">\n";
4580 print "<table class=\"tree\">\n";
4581 my $alternate = 1;
4582 # '..' (top directory) link if possible
4583 if (defined $hash_base &&
4584 defined $file_name && $file_name =~ m![^/]+$!) {
4585 if ($alternate) {
4586 print "<tr class=\"dark\">\n";
4587 } else {
4588 print "<tr class=\"light\">\n";
4590 $alternate ^= 1;
4592 my $up = $file_name;
4593 $up =~ s!/?[^/]+$!!;
4594 undef $up unless $up;
4595 # based on git_print_tree_entry
4596 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4597 print '<td class="list">';
4598 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4599 file_name=>$up)},
4600 "..");
4601 print "</td>\n";
4602 print "<td class=\"link\"></td>\n";
4604 print "</tr>\n";
4606 foreach my $line (@entries) {
4607 my %t = parse_ls_tree_line($line, -z => 1);
4609 if ($alternate) {
4610 print "<tr class=\"dark\">\n";
4611 } else {
4612 print "<tr class=\"light\">\n";
4614 $alternate ^= 1;
4616 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4618 print "</tr>\n";
4620 print "</table>\n" .
4621 "</div>";
4622 git_footer_html();
4625 sub git_snapshot {
4626 my @supported_fmts = gitweb_check_feature('snapshot');
4627 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4629 my $format = $cgi->param('sf');
4630 if (!@supported_fmts) {
4631 die_error('403 Permission denied', "Permission denied");
4633 # default to first supported snapshot format
4634 $format ||= $supported_fmts[0];
4635 if ($format !~ m/^[a-z0-9]+$/) {
4636 die_error(undef, "Invalid snapshot format parameter");
4637 } elsif (!exists($known_snapshot_formats{$format})) {
4638 die_error(undef, "Unknown snapshot format");
4639 } elsif (!grep($_ eq $format, @supported_fmts)) {
4640 die_error(undef, "Unsupported snapshot format");
4643 if (!defined $hash) {
4644 $hash = git_get_head_hash($project);
4647 my $git_command = git_cmd_str();
4648 my $name = $project;
4649 $name =~ s,([^/])/*\.git$,$1,;
4650 $name = basename($name);
4651 my $filename = to_utf8($name);
4652 $name =~ s/\047/\047\\\047\047/g;
4653 my $cmd;
4654 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4655 $cmd = "$git_command archive " .
4656 "--format=$known_snapshot_formats{$format}{'format'} " .
4657 "--prefix=\'$name\'/ $hash";
4658 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4659 $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4662 print $cgi->header(
4663 -type => $known_snapshot_formats{$format}{'type'},
4664 -content_disposition => 'inline; filename="' . "$filename" . '"',
4665 -status => '200 OK');
4667 open my $fd, "-|", $cmd
4668 or die_error(undef, "Execute git-archive failed");
4669 binmode STDOUT, ':raw';
4670 print <$fd>;
4671 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4672 close $fd;
4675 sub git_log {
4676 my $head = git_get_head_hash($project);
4677 if (!defined $hash) {
4678 $hash = $head;
4680 if (!defined $page) {
4681 $page = 0;
4683 my $refs = git_get_references();
4685 my @commitlist = parse_commits($hash, 101, (100 * $page));
4687 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
4689 git_header_html();
4690 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4692 if (!@commitlist) {
4693 my %co = parse_commit($hash);
4695 git_print_header_div('summary', $project);
4696 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4698 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4699 for (my $i = 0; $i <= $to; $i++) {
4700 my %co = %{$commitlist[$i]};
4701 next if !%co;
4702 my $commit = $co{'id'};
4703 my $ref = format_ref_marker($refs, $commit);
4704 my %ad = parse_date($co{'author_epoch'});
4705 git_print_header_div('commit',
4706 "<span class=\"age\">$co{'age_string'}</span>" .
4707 esc_html($co{'title'}) . $ref,
4708 $commit);
4709 print "<div class=\"title_text\">\n" .
4710 "<div class=\"log_link\">\n" .
4711 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4712 " | " .
4713 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4714 " | " .
4715 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4716 "<br/>\n" .
4717 "</div>\n" .
4718 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4719 "</div>\n";
4721 print "<div class=\"log_body\">\n";
4722 git_print_log($co{'comment'}, -final_empty_line=> 1);
4723 print "</div>\n";
4725 if ($#commitlist >= 100) {
4726 print "<div class=\"page_nav\">\n";
4727 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4728 -accesskey => "n", -title => "Alt-n"}, "next");
4729 print "</div>\n";
4731 git_footer_html();
4734 sub git_commit {
4735 $hash ||= $hash_base || "HEAD";
4736 my %co = parse_commit($hash);
4737 if (!%co) {
4738 die_error(undef, "Unknown commit object");
4740 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4741 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4743 my $parent = $co{'parent'};
4744 my $parents = $co{'parents'}; # listref
4746 # we need to prepare $formats_nav before any parameter munging
4747 my $formats_nav;
4748 if (!defined $parent) {
4749 # --root commitdiff
4750 $formats_nav .= '(initial)';
4751 } elsif (@$parents == 1) {
4752 # single parent commit
4753 $formats_nav .=
4754 '(parent: ' .
4755 $cgi->a({-href => href(action=>"commit",
4756 hash=>$parent)},
4757 esc_html(substr($parent, 0, 7))) .
4758 ')';
4759 } else {
4760 # merge commit
4761 $formats_nav .=
4762 '(merge: ' .
4763 join(' ', map {
4764 $cgi->a({-href => href(action=>"commit",
4765 hash=>$_)},
4766 esc_html(substr($_, 0, 7)));
4767 } @$parents ) .
4768 ')';
4771 if (!defined $parent) {
4772 $parent = "--root";
4774 my @difftree;
4775 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4776 @diff_opts,
4777 (@$parents <= 1 ? $parent : '-c'),
4778 $hash, "--"
4779 or die_error(undef, "Open git-diff-tree failed");
4780 @difftree = map { chomp; $_ } <$fd>;
4781 close $fd or die_error(undef, "Reading git-diff-tree failed");
4783 # non-textual hash id's can be cached
4784 my $expires;
4785 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4786 $expires = "+1d";
4788 my $refs = git_get_references();
4789 my $ref = format_ref_marker($refs, $co{'id'});
4791 git_header_html(undef, $expires);
4792 git_print_page_nav('commit', '',
4793 $hash, $co{'tree'}, $hash,
4794 $formats_nav);
4796 if (defined $co{'parent'}) {
4797 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4798 } else {
4799 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4801 print "<div class=\"title_text\">\n" .
4802 "<table class=\"object_header\">\n";
4803 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4804 "<tr>" .
4805 "<td></td><td> $ad{'rfc2822'}";
4806 if ($ad{'hour_local'} < 6) {
4807 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4808 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4809 } else {
4810 printf(" (%02d:%02d %s)",
4811 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4813 print "</td>" .
4814 "</tr>\n";
4815 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4816 print "<tr><td></td><td> $cd{'rfc2822'}" .
4817 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4818 "</td></tr>\n";
4819 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4820 print "<tr>" .
4821 "<td>tree</td>" .
4822 "<td class=\"sha1\">" .
4823 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4824 class => "list"}, $co{'tree'}) .
4825 "</td>" .
4826 "<td class=\"link\">" .
4827 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4828 "tree");
4829 my $snapshot_links = format_snapshot_links($hash);
4830 if (defined $snapshot_links) {
4831 print " | " . $snapshot_links;
4833 print "</td>" .
4834 "</tr>\n";
4836 foreach my $par (@$parents) {
4837 print "<tr>" .
4838 "<td>parent</td>" .
4839 "<td class=\"sha1\">" .
4840 $cgi->a({-href => href(action=>"commit", hash=>$par),
4841 class => "list"}, $par) .
4842 "</td>" .
4843 "<td class=\"link\">" .
4844 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4845 " | " .
4846 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4847 "</td>" .
4848 "</tr>\n";
4850 print "</table>".
4851 "</div>\n";
4853 print "<div class=\"page_body\">\n";
4854 git_print_log($co{'comment'});
4855 print "</div>\n";
4857 git_difftree_body(\@difftree, $hash, @$parents);
4859 git_footer_html();
4862 sub git_object {
4863 # object is defined by:
4864 # - hash or hash_base alone
4865 # - hash_base and file_name
4866 my $type;
4868 # - hash or hash_base alone
4869 if ($hash || ($hash_base && !defined $file_name)) {
4870 my $object_id = $hash || $hash_base;
4872 my $git_command = git_cmd_str();
4873 open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4874 or die_error('404 Not Found', "Object does not exist");
4875 $type = <$fd>;
4876 chomp $type;
4877 close $fd
4878 or die_error('404 Not Found', "Object does not exist");
4880 # - hash_base and file_name
4881 } elsif ($hash_base && defined $file_name) {
4882 $file_name =~ s,/+$,,;
4884 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4885 or die_error('404 Not Found', "Base object does not exist");
4887 # here errors should not hapen
4888 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4889 or die_error(undef, "Open git-ls-tree failed");
4890 my $line = <$fd>;
4891 close $fd;
4893 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4894 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4895 die_error('404 Not Found', "File or directory for given base does not exist");
4897 $type = $2;
4898 $hash = $3;
4899 } else {
4900 die_error('404 Not Found', "Not enough information to find object");
4903 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4904 hash=>$hash, hash_base=>$hash_base,
4905 file_name=>$file_name),
4906 -status => '302 Found');
4909 sub git_blobdiff {
4910 my $format = shift || 'html';
4912 my $fd;
4913 my @difftree;
4914 my %diffinfo;
4915 my $expires;
4917 # preparing $fd and %diffinfo for git_patchset_body
4918 # new style URI
4919 if (defined $hash_base && defined $hash_parent_base) {
4920 if (defined $file_name) {
4921 # read raw output
4922 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4923 $hash_parent_base, $hash_base,
4924 "--", (defined $file_parent ? $file_parent : ()), $file_name
4925 or die_error(undef, "Open git-diff-tree failed");
4926 @difftree = map { chomp; $_ } <$fd>;
4927 close $fd
4928 or die_error(undef, "Reading git-diff-tree failed");
4929 @difftree
4930 or die_error('404 Not Found', "Blob diff not found");
4932 } elsif (defined $hash &&
4933 $hash =~ /[0-9a-fA-F]{40}/) {
4934 # try to find filename from $hash
4936 # read filtered raw output
4937 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4938 $hash_parent_base, $hash_base, "--"
4939 or die_error(undef, "Open git-diff-tree failed");
4940 @difftree =
4941 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4942 # $hash == to_id
4943 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4944 map { chomp; $_ } <$fd>;
4945 close $fd
4946 or die_error(undef, "Reading git-diff-tree failed");
4947 @difftree
4948 or die_error('404 Not Found', "Blob diff not found");
4950 } else {
4951 die_error('404 Not Found', "Missing one of the blob diff parameters");
4954 if (@difftree > 1) {
4955 die_error('404 Not Found', "Ambiguous blob diff specification");
4958 %diffinfo = parse_difftree_raw_line($difftree[0]);
4959 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4960 $file_name ||= $diffinfo{'to_file'};
4962 $hash_parent ||= $diffinfo{'from_id'};
4963 $hash ||= $diffinfo{'to_id'};
4965 # non-textual hash id's can be cached
4966 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4967 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4968 $expires = '+1d';
4971 # open patch output
4972 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4973 '-p', ($format eq 'html' ? "--full-index" : ()),
4974 $hash_parent_base, $hash_base,
4975 "--", (defined $file_parent ? $file_parent : ()), $file_name
4976 or die_error(undef, "Open git-diff-tree failed");
4979 # old/legacy style URI
4980 if (!%diffinfo && # if new style URI failed
4981 defined $hash && defined $hash_parent) {
4982 # fake git-diff-tree raw output
4983 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4984 $diffinfo{'from_id'} = $hash_parent;
4985 $diffinfo{'to_id'} = $hash;
4986 if (defined $file_name) {
4987 if (defined $file_parent) {
4988 $diffinfo{'status'} = '2';
4989 $diffinfo{'from_file'} = $file_parent;
4990 $diffinfo{'to_file'} = $file_name;
4991 } else { # assume not renamed
4992 $diffinfo{'status'} = '1';
4993 $diffinfo{'from_file'} = $file_name;
4994 $diffinfo{'to_file'} = $file_name;
4996 } else { # no filename given
4997 $diffinfo{'status'} = '2';
4998 $diffinfo{'from_file'} = $hash_parent;
4999 $diffinfo{'to_file'} = $hash;
5002 # non-textual hash id's can be cached
5003 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
5004 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5005 $expires = '+1d';
5008 # open patch output
5009 open $fd, "-|", git_cmd(), "diff", @diff_opts,
5010 '-p', ($format eq 'html' ? "--full-index" : ()),
5011 $hash_parent, $hash, "--"
5012 or die_error(undef, "Open git-diff failed");
5013 } else {
5014 die_error('404 Not Found', "Missing one of the blob diff parameters")
5015 unless %diffinfo;
5018 # header
5019 if ($format eq 'html') {
5020 my $formats_nav =
5021 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5022 "raw");
5023 git_header_html(undef, $expires);
5024 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5025 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5026 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5027 } else {
5028 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5029 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5031 if (defined $file_name) {
5032 git_print_page_path($file_name, "blob", $hash_base);
5033 } else {
5034 print "<div class=\"page_path\"></div>\n";
5037 } elsif ($format eq 'plain') {
5038 print $cgi->header(
5039 -type => 'text/plain',
5040 -charset => 'utf-8',
5041 -expires => $expires,
5042 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
5044 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5046 } else {
5047 die_error(undef, "Unknown blobdiff format");
5050 # patch
5051 if ($format eq 'html') {
5052 print "<div class=\"page_body\">\n";
5054 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
5055 close $fd;
5057 print "</div>\n"; # class="page_body"
5058 git_footer_html();
5060 } else {
5061 while (my $line = <$fd>) {
5062 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5063 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5065 print $line;
5067 last if $line =~ m!^\+\+\+!;
5069 local $/ = undef;
5070 print <$fd>;
5071 close $fd;
5075 sub git_blobdiff_plain {
5076 git_blobdiff('plain');
5079 sub git_commitdiff {
5080 my $format = shift || 'html';
5081 $hash ||= $hash_base || "HEAD";
5082 my %co = parse_commit($hash);
5083 if (!%co) {
5084 die_error(undef, "Unknown commit object");
5087 # choose format for commitdiff for merge
5088 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5089 $hash_parent = '--cc';
5091 # we need to prepare $formats_nav before almost any parameter munging
5092 my $formats_nav;
5093 if ($format eq 'html') {
5094 $formats_nav =
5095 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5096 "raw");
5098 if (defined $hash_parent &&
5099 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5100 # commitdiff with two commits given
5101 my $hash_parent_short = $hash_parent;
5102 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5103 $hash_parent_short = substr($hash_parent, 0, 7);
5105 $formats_nav .=
5106 ' (from';
5107 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5108 if ($co{'parents'}[$i] eq $hash_parent) {
5109 $formats_nav .= ' parent ' . ($i+1);
5110 last;
5113 $formats_nav .= ': ' .
5114 $cgi->a({-href => href(action=>"commitdiff",
5115 hash=>$hash_parent)},
5116 esc_html($hash_parent_short)) .
5117 ')';
5118 } elsif (!$co{'parent'}) {
5119 # --root commitdiff
5120 $formats_nav .= ' (initial)';
5121 } elsif (scalar @{$co{'parents'}} == 1) {
5122 # single parent commit
5123 $formats_nav .=
5124 ' (parent: ' .
5125 $cgi->a({-href => href(action=>"commitdiff",
5126 hash=>$co{'parent'})},
5127 esc_html(substr($co{'parent'}, 0, 7))) .
5128 ')';
5129 } else {
5130 # merge commit
5131 if ($hash_parent eq '--cc') {
5132 $formats_nav .= ' | ' .
5133 $cgi->a({-href => href(action=>"commitdiff",
5134 hash=>$hash, hash_parent=>'-c')},
5135 'combined');
5136 } else { # $hash_parent eq '-c'
5137 $formats_nav .= ' | ' .
5138 $cgi->a({-href => href(action=>"commitdiff",
5139 hash=>$hash, hash_parent=>'--cc')},
5140 'compact');
5142 $formats_nav .=
5143 ' (merge: ' .
5144 join(' ', map {
5145 $cgi->a({-href => href(action=>"commitdiff",
5146 hash=>$_)},
5147 esc_html(substr($_, 0, 7)));
5148 } @{$co{'parents'}} ) .
5149 ')';
5153 my $hash_parent_param = $hash_parent;
5154 if (!defined $hash_parent_param) {
5155 # --cc for multiple parents, --root for parentless
5156 $hash_parent_param =
5157 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5160 # read commitdiff
5161 my $fd;
5162 my @difftree;
5163 if ($format eq 'html') {
5164 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5165 "--no-commit-id", "--patch-with-raw", "--full-index",
5166 $hash_parent_param, $hash, "--"
5167 or die_error(undef, "Open git-diff-tree failed");
5169 while (my $line = <$fd>) {
5170 chomp $line;
5171 # empty line ends raw part of diff-tree output
5172 last unless $line;
5173 push @difftree, scalar parse_difftree_raw_line($line);
5176 } elsif ($format eq 'plain') {
5177 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5178 '-p', $hash_parent_param, $hash, "--"
5179 or die_error(undef, "Open git-diff-tree failed");
5181 } else {
5182 die_error(undef, "Unknown commitdiff format");
5185 # non-textual hash id's can be cached
5186 my $expires;
5187 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5188 $expires = "+1d";
5191 # write commit message
5192 if ($format eq 'html') {
5193 my $refs = git_get_references();
5194 my $ref = format_ref_marker($refs, $co{'id'});
5196 git_header_html(undef, $expires);
5197 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5198 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5199 git_print_authorship(\%co);
5200 print "<div class=\"page_body\">\n";
5201 if (@{$co{'comment'}} > 1) {
5202 print "<div class=\"log\">\n";
5203 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5204 print "</div>\n"; # class="log"
5207 } elsif ($format eq 'plain') {
5208 my $refs = git_get_references("tags");
5209 my $tagname = git_get_rev_name_tags($hash);
5210 my $filename = basename($project) . "-$hash.patch";
5212 print $cgi->header(
5213 -type => 'text/plain',
5214 -charset => 'utf-8',
5215 -expires => $expires,
5216 -content_disposition => 'inline; filename="' . "$filename" . '"');
5217 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5218 print "From: " . to_utf8($co{'author'}) . "\n";
5219 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5220 print "Subject: " . to_utf8($co{'title'}) . "\n";
5222 print "X-Git-Tag: $tagname\n" if $tagname;
5223 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5225 foreach my $line (@{$co{'comment'}}) {
5226 print to_utf8($line) . "\n";
5228 print "---\n\n";
5231 # write patch
5232 if ($format eq 'html') {
5233 my $use_parents = !defined $hash_parent ||
5234 $hash_parent eq '-c' || $hash_parent eq '--cc';
5235 git_difftree_body(\@difftree, $hash,
5236 $use_parents ? @{$co{'parents'}} : $hash_parent);
5237 print "<br/>\n";
5239 git_patchset_body($fd, \@difftree, $hash,
5240 $use_parents ? @{$co{'parents'}} : $hash_parent);
5241 close $fd;
5242 print "</div>\n"; # class="page_body"
5243 git_footer_html();
5245 } elsif ($format eq 'plain') {
5246 local $/ = undef;
5247 print <$fd>;
5248 close $fd
5249 or print "Reading git-diff-tree failed\n";
5253 sub git_commitdiff_plain {
5254 git_commitdiff('plain');
5257 sub git_history {
5258 if (!defined $hash_base) {
5259 $hash_base = git_get_head_hash($project);
5261 if (!defined $page) {
5262 $page = 0;
5264 my $ftype;
5265 my %co = parse_commit($hash_base);
5266 if (!%co) {
5267 die_error(undef, "Unknown commit object");
5270 my $refs = git_get_references();
5271 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5273 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5274 $file_name, "--full-history");
5275 if (!@commitlist) {
5276 die_error('404 Not Found', "No such file or directory on given branch");
5279 if (!defined $hash && defined $file_name) {
5280 # some commits could have deleted file in question,
5281 # and not have it in tree, but one of them has to have it
5282 for (my $i = 0; $i <= @commitlist; $i++) {
5283 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5284 last if defined $hash;
5287 if (defined $hash) {
5288 $ftype = git_get_type($hash);
5290 if (!defined $ftype) {
5291 die_error(undef, "Unknown type of object");
5294 my $paging_nav = '';
5295 if ($page > 0) {
5296 $paging_nav .=
5297 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5298 file_name=>$file_name)},
5299 "first");
5300 $paging_nav .= " &sdot; " .
5301 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5302 -accesskey => "p", -title => "Alt-p"}, "prev");
5303 } else {
5304 $paging_nav .= "first";
5305 $paging_nav .= " &sdot; prev";
5307 my $next_link = '';
5308 if ($#commitlist >= 100) {
5309 $next_link =
5310 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5311 -accesskey => "n", -title => "Alt-n"}, "next");
5312 $paging_nav .= " &sdot; $next_link";
5313 } else {
5314 $paging_nav .= " &sdot; next";
5317 git_header_html();
5318 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5319 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5320 git_print_page_path($file_name, $ftype, $hash_base);
5322 git_history_body(\@commitlist, 0, 99,
5323 $refs, $hash_base, $ftype, $next_link);
5325 git_footer_html();
5328 sub git_search {
5329 my ($have_search) = gitweb_check_feature('search');
5330 if (!$have_search) {
5331 die_error('403 Permission denied', "Permission denied");
5333 if (!defined $searchtext) {
5334 die_error(undef, "Text field empty");
5336 if (!defined $hash) {
5337 $hash = git_get_head_hash($project);
5339 my %co = parse_commit($hash);
5340 if (!%co) {
5341 die_error(undef, "Unknown commit object");
5343 if (!defined $page) {
5344 $page = 0;
5347 $searchtype ||= 'commit';
5348 if ($searchtype eq 'pickaxe') {
5349 # pickaxe may take all resources of your box and run for several minutes
5350 # with every query - so decide by yourself how public you make this feature
5351 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5352 if (!$have_pickaxe) {
5353 die_error('403 Permission denied', "Permission denied");
5356 if ($searchtype eq 'grep') {
5357 my ($have_grep) = gitweb_check_feature('grep');
5358 if (!$have_grep) {
5359 die_error('403 Permission denied', "Permission denied");
5363 git_header_html();
5365 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5366 my $greptype;
5367 if ($searchtype eq 'commit') {
5368 $greptype = "--grep=";
5369 } elsif ($searchtype eq 'author') {
5370 $greptype = "--author=";
5371 } elsif ($searchtype eq 'committer') {
5372 $greptype = "--committer=";
5374 $greptype .= $searchtext;
5375 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5376 $greptype, '--regexp-ignore-case',
5377 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5379 my $paging_nav = '';
5380 if ($page > 0) {
5381 $paging_nav .=
5382 $cgi->a({-href => href(action=>"search", hash=>$hash,
5383 searchtext=>$searchtext,
5384 searchtype=>$searchtype)},
5385 "first");
5386 $paging_nav .= " &sdot; " .
5387 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5388 -accesskey => "p", -title => "Alt-p"}, "prev");
5389 } else {
5390 $paging_nav .= "first";
5391 $paging_nav .= " &sdot; prev";
5393 my $next_link = '';
5394 if ($#commitlist >= 100) {
5395 $next_link =
5396 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5397 -accesskey => "n", -title => "Alt-n"}, "next");
5398 $paging_nav .= " &sdot; $next_link";
5399 } else {
5400 $paging_nav .= " &sdot; next";
5403 if ($#commitlist >= 100) {
5406 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5407 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5408 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5411 if ($searchtype eq 'pickaxe') {
5412 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5413 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5415 print "<table class=\"pickaxe search\">\n";
5416 my $alternate = 1;
5417 $/ = "\n";
5418 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5419 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5420 ($search_use_regexp ? '--pickaxe-regex' : ());
5421 undef %co;
5422 my @files;
5423 while (my $line = <$fd>) {
5424 chomp $line;
5425 next unless $line;
5427 my %set = parse_difftree_raw_line($line);
5428 if (defined $set{'commit'}) {
5429 # finish previous commit
5430 if (%co) {
5431 print "</td>\n" .
5432 "<td class=\"link\">" .
5433 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5434 " | " .
5435 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5436 print "</td>\n" .
5437 "</tr>\n";
5440 if ($alternate) {
5441 print "<tr class=\"dark\">\n";
5442 } else {
5443 print "<tr class=\"light\">\n";
5445 $alternate ^= 1;
5446 %co = parse_commit($set{'commit'});
5447 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5448 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5449 "<td><i>$author</i></td>\n" .
5450 "<td>" .
5451 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5452 -class => "list subject"},
5453 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5454 } elsif (defined $set{'to_id'}) {
5455 next if ($set{'to_id'} =~ m/^0{40}$/);
5457 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5458 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5459 -class => "list"},
5460 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5461 "<br/>\n";
5464 close $fd;
5466 # finish last commit (warning: repetition!)
5467 if (%co) {
5468 print "</td>\n" .
5469 "<td class=\"link\">" .
5470 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5471 " | " .
5472 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5473 print "</td>\n" .
5474 "</tr>\n";
5477 print "</table>\n";
5480 if ($searchtype eq 'grep') {
5481 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5482 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5484 print "<table class=\"grep_search\">\n";
5485 my $alternate = 1;
5486 my $matches = 0;
5487 $/ = "\n";
5488 open my $fd, "-|", git_cmd(), 'grep', '-n',
5489 $search_use_regexp ? ('-E', '-i') : '-F',
5490 $searchtext, $co{'tree'};
5491 my $lastfile = '';
5492 while (my $line = <$fd>) {
5493 chomp $line;
5494 my ($file, $lno, $ltext, $binary);
5495 last if ($matches++ > 1000);
5496 if ($line =~ /^Binary file (.+) matches$/) {
5497 $file = $1;
5498 $binary = 1;
5499 } else {
5500 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5502 if ($file ne $lastfile) {
5503 $lastfile and print "</td></tr>\n";
5504 if ($alternate++) {
5505 print "<tr class=\"dark\">\n";
5506 } else {
5507 print "<tr class=\"light\">\n";
5509 print "<td class=\"list\">".
5510 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5511 file_name=>"$file"),
5512 -class => "list"}, esc_path($file));
5513 print "</td><td>\n";
5514 $lastfile = $file;
5516 if ($binary) {
5517 print "<div class=\"binary\">Binary file</div>\n";
5518 } else {
5519 $ltext = untabify($ltext);
5520 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5521 $ltext = esc_html($1, -nbsp=>1);
5522 $ltext .= '<span class="match">';
5523 $ltext .= esc_html($2, -nbsp=>1);
5524 $ltext .= '</span>';
5525 $ltext .= esc_html($3, -nbsp=>1);
5526 } else {
5527 $ltext = esc_html($ltext, -nbsp=>1);
5529 print "<div class=\"pre\">" .
5530 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5531 file_name=>"$file").'#l'.$lno,
5532 -class => "linenr"}, sprintf('%4i', $lno))
5533 . ' ' . $ltext . "</div>\n";
5536 if ($lastfile) {
5537 print "</td></tr>\n";
5538 if ($matches > 1000) {
5539 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5541 } else {
5542 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5544 close $fd;
5546 print "</table>\n";
5548 git_footer_html();
5551 sub git_search_help {
5552 git_header_html();
5553 git_print_page_nav('','', $hash,$hash,$hash);
5554 print <<EOT;
5555 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5556 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5557 the pattern entered is recognized as the POSIX extended
5558 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5559 insensitive).</p>
5560 <dl>
5561 <dt><b>commit</b></dt>
5562 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5564 my ($have_grep) = gitweb_check_feature('grep');
5565 if ($have_grep) {
5566 print <<EOT;
5567 <dt><b>grep</b></dt>
5568 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5569 a different one) are searched for the given pattern. On large trees, this search can take
5570 a while and put some strain on the server, so please use it with some consideration. Note that
5571 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5572 case-sensitive.</dd>
5575 print <<EOT;
5576 <dt><b>author</b></dt>
5577 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5578 <dt><b>committer</b></dt>
5579 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5581 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5582 if ($have_pickaxe) {
5583 print <<EOT;
5584 <dt><b>pickaxe</b></dt>
5585 <dd>All commits that caused the string to appear or disappear from any file (changes that
5586 added, removed or "modified" the string) will be listed. This search can take a while and
5587 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5588 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5591 print "</dl>\n";
5592 git_footer_html();
5595 sub git_shortlog {
5596 my $head = git_get_head_hash($project);
5597 if (!defined $hash) {
5598 $hash = $head;
5600 if (!defined $page) {
5601 $page = 0;
5603 my $refs = git_get_references();
5605 my @commitlist = parse_commits($hash, 101, (100 * $page));
5607 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5608 my $next_link = '';
5609 if ($#commitlist >= 100) {
5610 $next_link =
5611 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5612 -accesskey => "n", -title => "Alt-n"}, "next");
5615 git_header_html();
5616 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5617 git_print_header_div('summary', $project);
5619 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5621 git_footer_html();
5624 ## ......................................................................
5625 ## feeds (RSS, Atom; OPML)
5627 sub git_feed {
5628 my $format = shift || 'atom';
5629 my ($have_blame) = gitweb_check_feature('blame');
5631 # Atom: http://www.atomenabled.org/developers/syndication/
5632 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5633 if ($format ne 'rss' && $format ne 'atom') {
5634 die_error(undef, "Unknown web feed format");
5637 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5638 my $head = $hash || 'HEAD';
5639 my @commitlist = parse_commits($head, 150, 0, $file_name);
5641 my %latest_commit;
5642 my %latest_date;
5643 my $content_type = "application/$format+xml";
5644 if (defined $cgi->http('HTTP_ACCEPT') &&
5645 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5646 # browser (feed reader) prefers text/xml
5647 $content_type = 'text/xml';
5649 if (defined($commitlist[0])) {
5650 %latest_commit = %{$commitlist[0]};
5651 %latest_date = parse_date($latest_commit{'author_epoch'});
5652 print $cgi->header(
5653 -type => $content_type,
5654 -charset => 'utf-8',
5655 -last_modified => $latest_date{'rfc2822'});
5656 } else {
5657 print $cgi->header(
5658 -type => $content_type,
5659 -charset => 'utf-8');
5662 # Optimization: skip generating the body if client asks only
5663 # for Last-Modified date.
5664 return if ($cgi->request_method() eq 'HEAD');
5666 # header variables
5667 my $title = "$site_name - $project/$action";
5668 my $feed_type = 'log';
5669 if (defined $hash) {
5670 $title .= " - '$hash'";
5671 $feed_type = 'branch log';
5672 if (defined $file_name) {
5673 $title .= " :: $file_name";
5674 $feed_type = 'history';
5676 } elsif (defined $file_name) {
5677 $title .= " - $file_name";
5678 $feed_type = 'history';
5680 $title .= " $feed_type";
5681 my $descr = git_get_project_description($project);
5682 if (defined $descr) {
5683 $descr = esc_html($descr);
5684 } else {
5685 $descr = "$project " .
5686 ($format eq 'rss' ? 'RSS' : 'Atom') .
5687 " feed";
5689 my $owner = git_get_project_owner($project);
5690 $owner = esc_html($owner);
5692 #header
5693 my $alt_url;
5694 if (defined $file_name) {
5695 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5696 } elsif (defined $hash) {
5697 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5698 } else {
5699 $alt_url = href(-full=>1, action=>"summary");
5701 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5702 if ($format eq 'rss') {
5703 print <<XML;
5704 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5705 <channel>
5707 print "<title>$title</title>\n" .
5708 "<link>$alt_url</link>\n" .
5709 "<description>$descr</description>\n" .
5710 "<language>en</language>\n";
5711 } elsif ($format eq 'atom') {
5712 print <<XML;
5713 <feed xmlns="http://www.w3.org/2005/Atom">
5715 print "<title>$title</title>\n" .
5716 "<subtitle>$descr</subtitle>\n" .
5717 '<link rel="alternate" type="text/html" href="' .
5718 $alt_url . '" />' . "\n" .
5719 '<link rel="self" type="' . $content_type . '" href="' .
5720 $cgi->self_url() . '" />' . "\n" .
5721 "<id>" . href(-full=>1) . "</id>\n" .
5722 # use project owner for feed author
5723 "<author><name>$owner</name></author>\n";
5724 if (defined $favicon) {
5725 print "<icon>" . esc_url($favicon) . "</icon>\n";
5727 if (defined $logo_url) {
5728 # not twice as wide as tall: 72 x 27 pixels
5729 print "<logo>" . esc_url($logo) . "</logo>\n";
5731 if (! %latest_date) {
5732 # dummy date to keep the feed valid until commits trickle in:
5733 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5734 } else {
5735 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5739 # contents
5740 for (my $i = 0; $i <= $#commitlist; $i++) {
5741 my %co = %{$commitlist[$i]};
5742 my $commit = $co{'id'};
5743 # we read 150, we always show 30 and the ones more recent than 48 hours
5744 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5745 last;
5747 my %cd = parse_date($co{'author_epoch'});
5749 # get list of changed files
5750 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5751 $co{'parent'} || "--root",
5752 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5753 or next;
5754 my @difftree = map { chomp; $_ } <$fd>;
5755 close $fd
5756 or next;
5758 # print element (entry, item)
5759 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5760 if ($format eq 'rss') {
5761 print "<item>\n" .
5762 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5763 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5764 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5765 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5766 "<link>$co_url</link>\n" .
5767 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5768 "<content:encoded>" .
5769 "<![CDATA[\n";
5770 } elsif ($format eq 'atom') {
5771 print "<entry>\n" .
5772 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5773 "<updated>$cd{'iso-8601'}</updated>\n" .
5774 "<author>\n" .
5775 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5776 if ($co{'author_email'}) {
5777 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5779 print "</author>\n" .
5780 # use committer for contributor
5781 "<contributor>\n" .
5782 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5783 if ($co{'committer_email'}) {
5784 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5786 print "</contributor>\n" .
5787 "<published>$cd{'iso-8601'}</published>\n" .
5788 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5789 "<id>$co_url</id>\n" .
5790 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5791 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5793 my $comment = $co{'comment'};
5794 print "<pre>\n";
5795 foreach my $line (@$comment) {
5796 $line = esc_html($line);
5797 print "$line\n";
5799 print "</pre><ul>\n";
5800 foreach my $difftree_line (@difftree) {
5801 my %difftree = parse_difftree_raw_line($difftree_line);
5802 next if !$difftree{'from_id'};
5804 my $file = $difftree{'file'} || $difftree{'to_file'};
5806 print "<li>" .
5807 "[" .
5808 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5809 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5810 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5811 file_name=>$file, file_parent=>$difftree{'from_file'}),
5812 -title => "diff"}, 'D');
5813 if ($have_blame) {
5814 print $cgi->a({-href => href(-full=>1, action=>"blame",
5815 file_name=>$file, hash_base=>$commit),
5816 -title => "blame"}, 'B');
5818 # if this is not a feed of a file history
5819 if (!defined $file_name || $file_name ne $file) {
5820 print $cgi->a({-href => href(-full=>1, action=>"history",
5821 file_name=>$file, hash=>$commit),
5822 -title => "history"}, 'H');
5824 $file = esc_path($file);
5825 print "] ".
5826 "$file</li>\n";
5828 if ($format eq 'rss') {
5829 print "</ul>]]>\n" .
5830 "</content:encoded>\n" .
5831 "</item>\n";
5832 } elsif ($format eq 'atom') {
5833 print "</ul>\n</div>\n" .
5834 "</content>\n" .
5835 "</entry>\n";
5839 # end of feed
5840 if ($format eq 'rss') {
5841 print "</channel>\n</rss>\n";
5842 } elsif ($format eq 'atom') {
5843 print "</feed>\n";
5847 sub git_rss {
5848 git_feed('rss');
5851 sub git_atom {
5852 git_feed('atom');
5855 sub git_opml {
5856 my @list = git_get_projects_list();
5858 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5859 print <<XML;
5860 <?xml version="1.0" encoding="utf-8"?>
5861 <opml version="1.0">
5862 <head>
5863 <title>$site_name OPML Export</title>
5864 </head>
5865 <body>
5866 <outline text="git RSS feeds">
5869 foreach my $pr (@list) {
5870 my %proj = %$pr;
5871 my $head = git_get_head_hash($proj{'path'});
5872 if (!defined $head) {
5873 next;
5875 $git_dir = "$projectroot/$proj{'path'}";
5876 my %co = parse_commit($head);
5877 if (!%co) {
5878 next;
5881 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5882 my $rss = "$my_url?p=$proj{'path'};a=rss";
5883 my $html = "$my_url?p=$proj{'path'};a=summary";
5884 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5886 print <<XML;
5887 </outline>
5888 </body>
5889 </opml>