[PATCH] gitweb: Add support for extending the action bar with custom links
[git/gitweb.git] / gitweb / gitweb.perl
blob9fbc9f88ed01a9b8e725c37b921371425630ae3f
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]},
279 # Insert custom links to the action bar of all project pages.
280 # This enables you mainly to link to third-party scripts integrating
281 # into gitweb; e.g. git-browser for graphical history representation
282 # or custom web-based repository administration interface.
284 # The 'default' value consists of a list of triplets in the form
285 # (label, link, position) where position is the label after which
286 # to inster the link and link is a format string where %n expands
287 # to the project name, %f to the project path within the filesystem,
288 # %h to the current hash (h gitweb parameter) and %b to the current
289 # hash base (hb gitweb parameter).
291 # To enable system wide have in $GITWEB_CONFIG e.g.
292 # $feature{'actions'}{'default'} = [('graphiclog',
293 # '/git-browser/by-commit.html?r=%n', 'summary')];
294 # Project specific override is not supported.
295 'actions' => {
296 'override' => 0,
297 'default' => []},
300 sub gitweb_check_feature {
301 my ($name) = @_;
302 return unless exists $feature{$name};
303 my ($sub, $override, @defaults) = (
304 $feature{$name}{'sub'},
305 $feature{$name}{'override'},
306 @{$feature{$name}{'default'}});
307 if (!$override) { return @defaults; }
308 if (!defined $sub) {
309 warn "feature $name is not overrideable";
310 return @defaults;
312 return $sub->(@defaults);
315 sub feature_blame {
316 my ($val) = git_get_project_config('blame', '--bool');
318 if ($val eq 'true') {
319 return 1;
320 } elsif ($val eq 'false') {
321 return 0;
324 return $_[0];
327 sub feature_snapshot {
328 my (@fmts) = @_;
330 my ($val) = git_get_project_config('snapshot');
332 if ($val) {
333 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
336 return @fmts;
339 sub feature_grep {
340 my ($val) = git_get_project_config('grep', '--bool');
342 if ($val eq 'true') {
343 return (1);
344 } elsif ($val eq 'false') {
345 return (0);
348 return ($_[0]);
351 sub feature_pickaxe {
352 my ($val) = git_get_project_config('pickaxe', '--bool');
354 if ($val eq 'true') {
355 return (1);
356 } elsif ($val eq 'false') {
357 return (0);
360 return ($_[0]);
363 # checking HEAD file with -e is fragile if the repository was
364 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
365 # and then pruned.
366 sub check_head_link {
367 my ($dir) = @_;
368 my $headfile = "$dir/HEAD";
369 return ((-e $headfile) ||
370 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
373 sub check_export_ok {
374 my ($dir) = @_;
375 return (check_head_link($dir) &&
376 (!$export_ok || -e "$dir/$export_ok"));
379 # process alternate names for backward compatibility
380 # filter out unsupported (unknown) snapshot formats
381 sub filter_snapshot_fmts {
382 my @fmts = @_;
384 @fmts = map {
385 exists $known_snapshot_format_aliases{$_} ?
386 $known_snapshot_format_aliases{$_} : $_} @fmts;
387 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
391 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
392 if (-e $GITWEB_CONFIG) {
393 do $GITWEB_CONFIG;
394 } else {
395 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
396 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
399 # version of the core git binary
400 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
402 $projects_list ||= $projectroot;
404 # ======================================================================
405 # input validation and dispatch
406 our $action = $cgi->param('a');
407 if (defined $action) {
408 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
409 die_error(400, "Invalid action parameter");
413 # parameters which are pathnames
414 our $project = $cgi->param('p');
415 if (defined $project) {
416 if (!validate_pathname($project) ||
417 !(-d "$projectroot/$project") ||
418 !check_head_link("$projectroot/$project") ||
419 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
420 ($strict_export && !project_in_list($project))) {
421 undef $project;
422 die_error(404, "No such project");
426 our $file_name = $cgi->param('f');
427 if (defined $file_name) {
428 if (!validate_pathname($file_name)) {
429 die_error(400, "Invalid file parameter");
433 our $file_parent = $cgi->param('fp');
434 if (defined $file_parent) {
435 if (!validate_pathname($file_parent)) {
436 die_error(400, "Invalid file parent parameter");
440 # parameters which are refnames
441 our $hash = $cgi->param('h');
442 if (defined $hash) {
443 if (!validate_refname($hash)) {
444 die_error(400, "Invalid hash parameter");
448 our $hash_parent = $cgi->param('hp');
449 if (defined $hash_parent) {
450 if (!validate_refname($hash_parent)) {
451 die_error(400, "Invalid hash parent parameter");
455 our $hash_base = $cgi->param('hb');
456 if (defined $hash_base) {
457 if (!validate_refname($hash_base)) {
458 die_error(400, "Invalid hash base parameter");
462 my %allowed_options = (
463 "--no-merges" => [ qw(rss atom log shortlog history) ],
466 our @extra_options = $cgi->param('opt');
467 if (defined @extra_options) {
468 foreach my $opt (@extra_options) {
469 if (not exists $allowed_options{$opt}) {
470 die_error(400, "Invalid option parameter");
472 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
473 die_error(400, "Invalid option parameter for this action");
478 our $hash_parent_base = $cgi->param('hpb');
479 if (defined $hash_parent_base) {
480 if (!validate_refname($hash_parent_base)) {
481 die_error(400, "Invalid hash parent base parameter");
485 # other parameters
486 our $page = $cgi->param('pg');
487 if (defined $page) {
488 if ($page =~ m/[^0-9]/) {
489 die_error(400, "Invalid page parameter");
493 our $searchtype = $cgi->param('st');
494 if (defined $searchtype) {
495 if ($searchtype =~ m/[^a-z]/) {
496 die_error(400, "Invalid searchtype parameter");
500 our $search_use_regexp = $cgi->param('sr');
502 our $searchtext = $cgi->param('s');
503 our $search_regexp;
504 if (defined $searchtext) {
505 if (length($searchtext) < 2) {
506 die_error(403, "At least two characters are required for search parameter");
508 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
511 # now read PATH_INFO and use it as alternative to parameters
512 sub evaluate_path_info {
513 return if defined $project;
514 my $path_info = $ENV{"PATH_INFO"};
515 return if !$path_info;
516 $path_info =~ s,^/+,,;
517 return if !$path_info;
518 # find which part of PATH_INFO is project
519 $project = $path_info;
520 $project =~ s,/+$,,;
521 while ($project && !check_head_link("$projectroot/$project")) {
522 $project =~ s,/*[^/]*$,,;
524 # validate project
525 $project = validate_pathname($project);
526 if (!$project ||
527 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
528 ($strict_export && !project_in_list($project))) {
529 undef $project;
530 return;
532 # do not change any parameters if an action is given using the query string
533 return if $action;
534 $path_info =~ s,^\Q$project\E/*,,;
535 my ($refname, $pathname) = split(/:/, $path_info, 2);
536 if (defined $pathname) {
537 # we got "project.git/branch:filename" or "project.git/branch:dir/"
538 # we could use git_get_type(branch:pathname), but it needs $git_dir
539 $pathname =~ s,^/+,,;
540 if (!$pathname || substr($pathname, -1) eq "/") {
541 $action ||= "tree";
542 $pathname =~ s,/$,,;
543 } else {
544 $action ||= "blob_plain";
546 $hash_base ||= validate_refname($refname);
547 $file_name ||= validate_pathname($pathname);
548 } elsif (defined $refname) {
549 # we got "project.git/branch"
550 $action ||= "shortlog";
551 $hash ||= validate_refname($refname);
554 evaluate_path_info();
556 # path to the current git repository
557 our $git_dir;
558 $git_dir = "$projectroot/$project" if $project;
560 # dispatch
561 my %actions = (
562 "blame" => \&git_blame,
563 "blobdiff" => \&git_blobdiff,
564 "blobdiff_plain" => \&git_blobdiff_plain,
565 "blob" => \&git_blob,
566 "blob_plain" => \&git_blob_plain,
567 "commitdiff" => \&git_commitdiff,
568 "commitdiff_plain" => \&git_commitdiff_plain,
569 "commit" => \&git_commit,
570 "forks" => \&git_forks,
571 "heads" => \&git_heads,
572 "history" => \&git_history,
573 "log" => \&git_log,
574 "rss" => \&git_rss,
575 "atom" => \&git_atom,
576 "search" => \&git_search,
577 "search_help" => \&git_search_help,
578 "shortlog" => \&git_shortlog,
579 "summary" => \&git_summary,
580 "tag" => \&git_tag,
581 "tags" => \&git_tags,
582 "tree" => \&git_tree,
583 "snapshot" => \&git_snapshot,
584 "object" => \&git_object,
585 # those below don't need $project
586 "opml" => \&git_opml,
587 "project_list" => \&git_project_list,
588 "project_index" => \&git_project_index,
591 if (!defined $action) {
592 if (defined $hash) {
593 $action = git_get_type($hash);
594 } elsif (defined $hash_base && defined $file_name) {
595 $action = git_get_type("$hash_base:$file_name");
596 } elsif (defined $project) {
597 $action = 'summary';
598 } else {
599 $action = 'project_list';
602 if (!defined($actions{$action})) {
603 die_error(400, "Unknown action");
605 if ($action !~ m/^(opml|project_list|project_index)$/ &&
606 !$project) {
607 die_error(400, "Project needed");
609 $actions{$action}->();
610 exit;
612 ## ======================================================================
613 ## action links
615 sub href (%) {
616 my %params = @_;
617 # default is to use -absolute url() i.e. $my_uri
618 my $href = $params{-full} ? $my_url : $my_uri;
620 # XXX: Warning: If you touch this, check the search form for updating,
621 # too.
623 my @mapping = (
624 project => "p",
625 action => "a",
626 file_name => "f",
627 file_parent => "fp",
628 hash => "h",
629 hash_parent => "hp",
630 hash_base => "hb",
631 hash_parent_base => "hpb",
632 page => "pg",
633 order => "o",
634 searchtext => "s",
635 searchtype => "st",
636 snapshot_format => "sf",
637 extra_options => "opt",
638 search_use_regexp => "sr",
640 my %mapping = @mapping;
642 $params{'project'} = $project unless exists $params{'project'};
644 if ($params{-replay}) {
645 while (my ($name, $symbol) = each %mapping) {
646 if (!exists $params{$name}) {
647 # to allow for multivalued params we use arrayref form
648 $params{$name} = [ $cgi->param($symbol) ];
653 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
654 if ($use_pathinfo) {
655 # use PATH_INFO for project name
656 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
657 delete $params{'project'};
659 # Summary just uses the project path URL
660 if (defined $params{'action'} && $params{'action'} eq 'summary') {
661 delete $params{'action'};
665 # now encode the parameters explicitly
666 my @result = ();
667 for (my $i = 0; $i < @mapping; $i += 2) {
668 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
669 if (defined $params{$name}) {
670 if (ref($params{$name}) eq "ARRAY") {
671 foreach my $par (@{$params{$name}}) {
672 push @result, $symbol . "=" . esc_param($par);
674 } else {
675 push @result, $symbol . "=" . esc_param($params{$name});
679 $href .= "?" . join(';', @result) if scalar @result;
681 return $href;
685 ## ======================================================================
686 ## validation, quoting/unquoting and escaping
688 sub validate_pathname {
689 my $input = shift || return undef;
691 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
692 # at the beginning, at the end, and between slashes.
693 # also this catches doubled slashes
694 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
695 return undef;
697 # no null characters
698 if ($input =~ m!\0!) {
699 return undef;
701 return $input;
704 sub validate_refname {
705 my $input = shift || return undef;
707 # textual hashes are O.K.
708 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
709 return $input;
711 # it must be correct pathname
712 $input = validate_pathname($input)
713 or return undef;
714 # restrictions on ref name according to git-check-ref-format
715 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
716 return undef;
718 return $input;
721 # decode sequences of octets in utf8 into Perl's internal form,
722 # which is utf-8 with utf8 flag set if needed. gitweb writes out
723 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
724 sub to_utf8 {
725 my $str = shift;
726 if (utf8::valid($str)) {
727 utf8::decode($str);
728 return $str;
729 } else {
730 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
734 # quote unsafe chars, but keep the slash, even when it's not
735 # correct, but quoted slashes look too horrible in bookmarks
736 sub esc_param {
737 my $str = shift;
738 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
739 $str =~ s/\+/%2B/g;
740 $str =~ s/ /\+/g;
741 return $str;
744 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
745 sub esc_url {
746 my $str = shift;
747 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
748 $str =~ s/\+/%2B/g;
749 $str =~ s/ /\+/g;
750 return $str;
753 # replace invalid utf8 character with SUBSTITUTION sequence
754 sub esc_html ($;%) {
755 my $str = shift;
756 my %opts = @_;
758 $str = to_utf8($str);
759 $str = $cgi->escapeHTML($str);
760 if ($opts{'-nbsp'}) {
761 $str =~ s/ /&nbsp;/g;
763 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
764 return $str;
767 # quote control characters and escape filename to HTML
768 sub esc_path {
769 my $str = shift;
770 my %opts = @_;
772 $str = to_utf8($str);
773 $str = $cgi->escapeHTML($str);
774 if ($opts{'-nbsp'}) {
775 $str =~ s/ /&nbsp;/g;
777 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
778 return $str;
781 # Make control characters "printable", using character escape codes (CEC)
782 sub quot_cec {
783 my $cntrl = shift;
784 my %opts = @_;
785 my %es = ( # character escape codes, aka escape sequences
786 "\t" => '\t', # tab (HT)
787 "\n" => '\n', # line feed (LF)
788 "\r" => '\r', # carrige return (CR)
789 "\f" => '\f', # form feed (FF)
790 "\b" => '\b', # backspace (BS)
791 "\a" => '\a', # alarm (bell) (BEL)
792 "\e" => '\e', # escape (ESC)
793 "\013" => '\v', # vertical tab (VT)
794 "\000" => '\0', # nul character (NUL)
796 my $chr = ( (exists $es{$cntrl})
797 ? $es{$cntrl}
798 : sprintf('\%03o', ord($cntrl)) );
799 if ($opts{-nohtml}) {
800 return $chr;
801 } else {
802 return "<span class=\"cntrl\">$chr</span>";
806 # Alternatively use unicode control pictures codepoints,
807 # Unicode "printable representation" (PR)
808 sub quot_upr {
809 my $cntrl = shift;
810 my %opts = @_;
812 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
813 if ($opts{-nohtml}) {
814 return $chr;
815 } else {
816 return "<span class=\"cntrl\">$chr</span>";
820 # git may return quoted and escaped filenames
821 sub unquote {
822 my $str = shift;
824 sub unq {
825 my $seq = shift;
826 my %es = ( # character escape codes, aka escape sequences
827 't' => "\t", # tab (HT, TAB)
828 'n' => "\n", # newline (NL)
829 'r' => "\r", # return (CR)
830 'f' => "\f", # form feed (FF)
831 'b' => "\b", # backspace (BS)
832 'a' => "\a", # alarm (bell) (BEL)
833 'e' => "\e", # escape (ESC)
834 'v' => "\013", # vertical tab (VT)
837 if ($seq =~ m/^[0-7]{1,3}$/) {
838 # octal char sequence
839 return chr(oct($seq));
840 } elsif (exists $es{$seq}) {
841 # C escape sequence, aka character escape code
842 return $es{$seq};
844 # quoted ordinary character
845 return $seq;
848 if ($str =~ m/^"(.*)"$/) {
849 # needs unquoting
850 $str = $1;
851 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
853 return $str;
856 # escape tabs (convert tabs to spaces)
857 sub untabify {
858 my $line = shift;
860 while ((my $pos = index($line, "\t")) != -1) {
861 if (my $count = (8 - ($pos % 8))) {
862 my $spaces = ' ' x $count;
863 $line =~ s/\t/$spaces/;
867 return $line;
870 sub project_in_list {
871 my $project = shift;
872 my @list = git_get_projects_list();
873 return @list && scalar(grep { $_->{'path'} eq $project } @list);
876 ## ----------------------------------------------------------------------
877 ## HTML aware string manipulation
879 # Try to chop given string on a word boundary between position
880 # $len and $len+$add_len. If there is no word boundary there,
881 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
882 # (marking chopped part) would be longer than given string.
883 sub chop_str {
884 my $str = shift;
885 my $len = shift;
886 my $add_len = shift || 10;
887 my $where = shift || 'right'; # 'left' | 'center' | 'right'
889 # Make sure perl knows it is utf8 encoded so we don't
890 # cut in the middle of a utf8 multibyte char.
891 $str = to_utf8($str);
893 # allow only $len chars, but don't cut a word if it would fit in $add_len
894 # if it doesn't fit, cut it if it's still longer than the dots we would add
895 # remove chopped character entities entirely
897 # when chopping in the middle, distribute $len into left and right part
898 # return early if chopping wouldn't make string shorter
899 if ($where eq 'center') {
900 return $str if ($len + 5 >= length($str)); # filler is length 5
901 $len = int($len/2);
902 } else {
903 return $str if ($len + 4 >= length($str)); # filler is length 4
906 # regexps: ending and beginning with word part up to $add_len
907 my $endre = qr/.{$len}\w{0,$add_len}/;
908 my $begre = qr/\w{0,$add_len}.{$len}/;
910 if ($where eq 'left') {
911 $str =~ m/^(.*?)($begre)$/;
912 my ($lead, $body) = ($1, $2);
913 if (length($lead) > 4) {
914 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
915 $lead = " ...";
917 return "$lead$body";
919 } elsif ($where eq 'center') {
920 $str =~ m/^($endre)(.*)$/;
921 my ($left, $str) = ($1, $2);
922 $str =~ m/^(.*?)($begre)$/;
923 my ($mid, $right) = ($1, $2);
924 if (length($mid) > 5) {
925 $left =~ s/&[^;]*$//;
926 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
927 $mid = " ... ";
929 return "$left$mid$right";
931 } else {
932 $str =~ m/^($endre)(.*)$/;
933 my $body = $1;
934 my $tail = $2;
935 if (length($tail) > 4) {
936 $body =~ s/&[^;]*$//;
937 $tail = "... ";
939 return "$body$tail";
943 # takes the same arguments as chop_str, but also wraps a <span> around the
944 # result with a title attribute if it does get chopped. Additionally, the
945 # string is HTML-escaped.
946 sub chop_and_escape_str {
947 my ($str) = @_;
949 my $chopped = chop_str(@_);
950 if ($chopped eq $str) {
951 return esc_html($chopped);
952 } else {
953 $str =~ s/([[:cntrl:]])/?/g;
954 return $cgi->span({-title=>$str}, esc_html($chopped));
958 ## ----------------------------------------------------------------------
959 ## functions returning short strings
961 # CSS class for given age value (in seconds)
962 sub age_class {
963 my $age = shift;
965 if (!defined $age) {
966 return "noage";
967 } elsif ($age < 60*60*2) {
968 return "age0";
969 } elsif ($age < 60*60*24*2) {
970 return "age1";
971 } else {
972 return "age2";
976 # convert age in seconds to "nn units ago" string
977 sub age_string {
978 my $age = shift;
979 my $age_str;
981 if ($age > 60*60*24*365*2) {
982 $age_str = (int $age/60/60/24/365);
983 $age_str .= " years ago";
984 } elsif ($age > 60*60*24*(365/12)*2) {
985 $age_str = int $age/60/60/24/(365/12);
986 $age_str .= " months ago";
987 } elsif ($age > 60*60*24*7*2) {
988 $age_str = int $age/60/60/24/7;
989 $age_str .= " weeks ago";
990 } elsif ($age > 60*60*24*2) {
991 $age_str = int $age/60/60/24;
992 $age_str .= " days ago";
993 } elsif ($age > 60*60*2) {
994 $age_str = int $age/60/60;
995 $age_str .= " hours ago";
996 } elsif ($age > 60*2) {
997 $age_str = int $age/60;
998 $age_str .= " min ago";
999 } elsif ($age > 2) {
1000 $age_str = int $age;
1001 $age_str .= " sec ago";
1002 } else {
1003 $age_str .= " right now";
1005 return $age_str;
1008 use constant {
1009 S_IFINVALID => 0030000,
1010 S_IFGITLINK => 0160000,
1013 # submodule/subproject, a commit object reference
1014 sub S_ISGITLINK($) {
1015 my $mode = shift;
1017 return (($mode & S_IFMT) == S_IFGITLINK)
1020 # convert file mode in octal to symbolic file mode string
1021 sub mode_str {
1022 my $mode = oct shift;
1024 if (S_ISGITLINK($mode)) {
1025 return 'm---------';
1026 } elsif (S_ISDIR($mode & S_IFMT)) {
1027 return 'drwxr-xr-x';
1028 } elsif (S_ISLNK($mode)) {
1029 return 'lrwxrwxrwx';
1030 } elsif (S_ISREG($mode)) {
1031 # git cares only about the executable bit
1032 if ($mode & S_IXUSR) {
1033 return '-rwxr-xr-x';
1034 } else {
1035 return '-rw-r--r--';
1037 } else {
1038 return '----------';
1042 # convert file mode in octal to file type string
1043 sub file_type {
1044 my $mode = shift;
1046 if ($mode !~ m/^[0-7]+$/) {
1047 return $mode;
1048 } else {
1049 $mode = oct $mode;
1052 if (S_ISGITLINK($mode)) {
1053 return "submodule";
1054 } elsif (S_ISDIR($mode & S_IFMT)) {
1055 return "directory";
1056 } elsif (S_ISLNK($mode)) {
1057 return "symlink";
1058 } elsif (S_ISREG($mode)) {
1059 return "file";
1060 } else {
1061 return "unknown";
1065 # convert file mode in octal to file type description string
1066 sub file_type_long {
1067 my $mode = shift;
1069 if ($mode !~ m/^[0-7]+$/) {
1070 return $mode;
1071 } else {
1072 $mode = oct $mode;
1075 if (S_ISGITLINK($mode)) {
1076 return "submodule";
1077 } elsif (S_ISDIR($mode & S_IFMT)) {
1078 return "directory";
1079 } elsif (S_ISLNK($mode)) {
1080 return "symlink";
1081 } elsif (S_ISREG($mode)) {
1082 if ($mode & S_IXUSR) {
1083 return "executable";
1084 } else {
1085 return "file";
1087 } else {
1088 return "unknown";
1093 ## ----------------------------------------------------------------------
1094 ## functions returning short HTML fragments, or transforming HTML fragments
1095 ## which don't belong to other sections
1097 # format line of commit message.
1098 sub format_log_line_html {
1099 my $line = shift;
1101 $line = esc_html($line, -nbsp=>1);
1102 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1103 my $hash_text = $1;
1104 my $link =
1105 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1106 -class => "text"}, $hash_text);
1107 $line =~ s/$hash_text/$link/;
1109 return $line;
1112 # format marker of refs pointing to given object
1114 # the destination action is chosen based on object type and current context:
1115 # - for annotated tags, we choose the tag view unless it's the current view
1116 # already, in which case we go to shortlog view
1117 # - for other refs, we keep the current view if we're in history, shortlog or
1118 # log view, and select shortlog otherwise
1119 sub format_ref_marker {
1120 my ($refs, $id) = @_;
1121 my $markers = '';
1123 if (defined $refs->{$id}) {
1124 foreach my $ref (@{$refs->{$id}}) {
1125 # this code exploits the fact that non-lightweight tags are the
1126 # only indirect objects, and that they are the only objects for which
1127 # we want to use tag instead of shortlog as action
1128 my ($type, $name) = qw();
1129 my $indirect = ($ref =~ s/\^\{\}$//);
1130 # e.g. tags/v2.6.11 or heads/next
1131 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1132 $type = $1;
1133 $name = $2;
1134 } else {
1135 $type = "ref";
1136 $name = $ref;
1139 my $class = $type;
1140 $class .= " indirect" if $indirect;
1142 my $dest_action = "shortlog";
1144 if ($indirect) {
1145 $dest_action = "tag" unless $action eq "tag";
1146 } elsif ($action =~ /^(history|(short)?log)$/) {
1147 $dest_action = $action;
1150 my $dest = "";
1151 $dest .= "refs/" unless $ref =~ m!^refs/!;
1152 $dest .= $ref;
1154 my $link = $cgi->a({
1155 -href => href(
1156 action=>$dest_action,
1157 hash=>$dest
1158 )}, $name);
1160 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1161 $link . "</span>";
1165 if ($markers) {
1166 return ' <span class="refs">'. $markers . '</span>';
1167 } else {
1168 return "";
1172 # format, perhaps shortened and with markers, title line
1173 sub format_subject_html {
1174 my ($long, $short, $href, $extra) = @_;
1175 $extra = '' unless defined($extra);
1177 if (length($short) < length($long)) {
1178 return $cgi->a({-href => $href, -class => "list subject",
1179 -title => to_utf8($long)},
1180 esc_html($short) . $extra);
1181 } else {
1182 return $cgi->a({-href => $href, -class => "list subject"},
1183 esc_html($long) . $extra);
1187 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1188 sub format_git_diff_header_line {
1189 my $line = shift;
1190 my $diffinfo = shift;
1191 my ($from, $to) = @_;
1193 if ($diffinfo->{'nparents'}) {
1194 # combined diff
1195 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1196 if ($to->{'href'}) {
1197 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1198 esc_path($to->{'file'}));
1199 } else { # file was deleted (no href)
1200 $line .= esc_path($to->{'file'});
1202 } else {
1203 # "ordinary" diff
1204 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1205 if ($from->{'href'}) {
1206 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1207 'a/' . esc_path($from->{'file'}));
1208 } else { # file was added (no href)
1209 $line .= 'a/' . esc_path($from->{'file'});
1211 $line .= ' ';
1212 if ($to->{'href'}) {
1213 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1214 'b/' . esc_path($to->{'file'}));
1215 } else { # file was deleted
1216 $line .= 'b/' . esc_path($to->{'file'});
1220 return "<div class=\"diff header\">$line</div>\n";
1223 # format extended diff header line, before patch itself
1224 sub format_extended_diff_header_line {
1225 my $line = shift;
1226 my $diffinfo = shift;
1227 my ($from, $to) = @_;
1229 # match <path>
1230 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1231 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1232 esc_path($from->{'file'}));
1234 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1235 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1236 esc_path($to->{'file'}));
1238 # match single <mode>
1239 if ($line =~ m/\s(\d{6})$/) {
1240 $line .= '<span class="info"> (' .
1241 file_type_long($1) .
1242 ')</span>';
1244 # match <hash>
1245 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1246 # can match only for combined diff
1247 $line = 'index ';
1248 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1249 if ($from->{'href'}[$i]) {
1250 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1251 -class=>"hash"},
1252 substr($diffinfo->{'from_id'}[$i],0,7));
1253 } else {
1254 $line .= '0' x 7;
1256 # separator
1257 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1259 $line .= '..';
1260 if ($to->{'href'}) {
1261 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1262 substr($diffinfo->{'to_id'},0,7));
1263 } else {
1264 $line .= '0' x 7;
1267 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1268 # can match only for ordinary diff
1269 my ($from_link, $to_link);
1270 if ($from->{'href'}) {
1271 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1272 substr($diffinfo->{'from_id'},0,7));
1273 } else {
1274 $from_link = '0' x 7;
1276 if ($to->{'href'}) {
1277 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1278 substr($diffinfo->{'to_id'},0,7));
1279 } else {
1280 $to_link = '0' x 7;
1282 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1283 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1286 return $line . "<br/>\n";
1289 # format from-file/to-file diff header
1290 sub format_diff_from_to_header {
1291 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1292 my $line;
1293 my $result = '';
1295 $line = $from_line;
1296 #assert($line =~ m/^---/) if DEBUG;
1297 # no extra formatting for "^--- /dev/null"
1298 if (! $diffinfo->{'nparents'}) {
1299 # ordinary (single parent) diff
1300 if ($line =~ m!^--- "?a/!) {
1301 if ($from->{'href'}) {
1302 $line = '--- a/' .
1303 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1304 esc_path($from->{'file'}));
1305 } else {
1306 $line = '--- a/' .
1307 esc_path($from->{'file'});
1310 $result .= qq!<div class="diff from_file">$line</div>\n!;
1312 } else {
1313 # combined diff (merge commit)
1314 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1315 if ($from->{'href'}[$i]) {
1316 $line = '--- ' .
1317 $cgi->a({-href=>href(action=>"blobdiff",
1318 hash_parent=>$diffinfo->{'from_id'}[$i],
1319 hash_parent_base=>$parents[$i],
1320 file_parent=>$from->{'file'}[$i],
1321 hash=>$diffinfo->{'to_id'},
1322 hash_base=>$hash,
1323 file_name=>$to->{'file'}),
1324 -class=>"path",
1325 -title=>"diff" . ($i+1)},
1326 $i+1) .
1327 '/' .
1328 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1329 esc_path($from->{'file'}[$i]));
1330 } else {
1331 $line = '--- /dev/null';
1333 $result .= qq!<div class="diff from_file">$line</div>\n!;
1337 $line = $to_line;
1338 #assert($line =~ m/^\+\+\+/) if DEBUG;
1339 # no extra formatting for "^+++ /dev/null"
1340 if ($line =~ m!^\+\+\+ "?b/!) {
1341 if ($to->{'href'}) {
1342 $line = '+++ b/' .
1343 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1344 esc_path($to->{'file'}));
1345 } else {
1346 $line = '+++ b/' .
1347 esc_path($to->{'file'});
1350 $result .= qq!<div class="diff to_file">$line</div>\n!;
1352 return $result;
1355 # create note for patch simplified by combined diff
1356 sub format_diff_cc_simplified {
1357 my ($diffinfo, @parents) = @_;
1358 my $result = '';
1360 $result .= "<div class=\"diff header\">" .
1361 "diff --cc ";
1362 if (!is_deleted($diffinfo)) {
1363 $result .= $cgi->a({-href => href(action=>"blob",
1364 hash_base=>$hash,
1365 hash=>$diffinfo->{'to_id'},
1366 file_name=>$diffinfo->{'to_file'}),
1367 -class => "path"},
1368 esc_path($diffinfo->{'to_file'}));
1369 } else {
1370 $result .= esc_path($diffinfo->{'to_file'});
1372 $result .= "</div>\n" . # class="diff header"
1373 "<div class=\"diff nodifferences\">" .
1374 "Simple merge" .
1375 "</div>\n"; # class="diff nodifferences"
1377 return $result;
1380 # format patch (diff) line (not to be used for diff headers)
1381 sub format_diff_line {
1382 my $line = shift;
1383 my ($from, $to) = @_;
1384 my $diff_class = "";
1386 chomp $line;
1388 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1389 # combined diff
1390 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1391 if ($line =~ m/^\@{3}/) {
1392 $diff_class = " chunk_header";
1393 } elsif ($line =~ m/^\\/) {
1394 $diff_class = " incomplete";
1395 } elsif ($prefix =~ tr/+/+/) {
1396 $diff_class = " add";
1397 } elsif ($prefix =~ tr/-/-/) {
1398 $diff_class = " rem";
1400 } else {
1401 # assume ordinary diff
1402 my $char = substr($line, 0, 1);
1403 if ($char eq '+') {
1404 $diff_class = " add";
1405 } elsif ($char eq '-') {
1406 $diff_class = " rem";
1407 } elsif ($char eq '@') {
1408 $diff_class = " chunk_header";
1409 } elsif ($char eq "\\") {
1410 $diff_class = " incomplete";
1413 $line = untabify($line);
1414 if ($from && $to && $line =~ m/^\@{2} /) {
1415 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1416 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1418 $from_lines = 0 unless defined $from_lines;
1419 $to_lines = 0 unless defined $to_lines;
1421 if ($from->{'href'}) {
1422 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1423 -class=>"list"}, $from_text);
1425 if ($to->{'href'}) {
1426 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1427 -class=>"list"}, $to_text);
1429 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1430 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1431 return "<div class=\"diff$diff_class\">$line</div>\n";
1432 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1433 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1434 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1436 @from_text = split(' ', $ranges);
1437 for (my $i = 0; $i < @from_text; ++$i) {
1438 ($from_start[$i], $from_nlines[$i]) =
1439 (split(',', substr($from_text[$i], 1)), 0);
1442 $to_text = pop @from_text;
1443 $to_start = pop @from_start;
1444 $to_nlines = pop @from_nlines;
1446 $line = "<span class=\"chunk_info\">$prefix ";
1447 for (my $i = 0; $i < @from_text; ++$i) {
1448 if ($from->{'href'}[$i]) {
1449 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1450 -class=>"list"}, $from_text[$i]);
1451 } else {
1452 $line .= $from_text[$i];
1454 $line .= " ";
1456 if ($to->{'href'}) {
1457 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1458 -class=>"list"}, $to_text);
1459 } else {
1460 $line .= $to_text;
1462 $line .= " $prefix</span>" .
1463 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1464 return "<div class=\"diff$diff_class\">$line</div>\n";
1466 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1469 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1470 # linked. Pass the hash of the tree/commit to snapshot.
1471 sub format_snapshot_links {
1472 my ($hash) = @_;
1473 my @snapshot_fmts = gitweb_check_feature('snapshot');
1474 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1475 my $num_fmts = @snapshot_fmts;
1476 if ($num_fmts > 1) {
1477 # A parenthesized list of links bearing format names.
1478 # e.g. "snapshot (_tar.gz_ _zip_)"
1479 return "snapshot (" . join(' ', map
1480 $cgi->a({
1481 -href => href(
1482 action=>"snapshot",
1483 hash=>$hash,
1484 snapshot_format=>$_
1486 }, $known_snapshot_formats{$_}{'display'})
1487 , @snapshot_fmts) . ")";
1488 } elsif ($num_fmts == 1) {
1489 # A single "snapshot" link whose tooltip bears the format name.
1490 # i.e. "_snapshot_"
1491 my ($fmt) = @snapshot_fmts;
1492 return
1493 $cgi->a({
1494 -href => href(
1495 action=>"snapshot",
1496 hash=>$hash,
1497 snapshot_format=>$fmt
1499 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1500 }, "snapshot");
1501 } else { # $num_fmts == 0
1502 return undef;
1506 ## ......................................................................
1507 ## functions returning values to be passed, perhaps after some
1508 ## transformation, to other functions; e.g. returning arguments to href()
1510 # returns hash to be passed to href to generate gitweb URL
1511 # in -title key it returns description of link
1512 sub get_feed_info {
1513 my $format = shift || 'Atom';
1514 my %res = (action => lc($format));
1516 # feed links are possible only for project views
1517 return unless (defined $project);
1518 # some views should link to OPML, or to generic project feed,
1519 # or don't have specific feed yet (so they should use generic)
1520 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1522 my $branch;
1523 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1524 # from tag links; this also makes possible to detect branch links
1525 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1526 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1527 $branch = $1;
1529 # find log type for feed description (title)
1530 my $type = 'log';
1531 if (defined $file_name) {
1532 $type = "history of $file_name";
1533 $type .= "/" if ($action eq 'tree');
1534 $type .= " on '$branch'" if (defined $branch);
1535 } else {
1536 $type = "log of $branch" if (defined $branch);
1539 $res{-title} = $type;
1540 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1541 $res{'file_name'} = $file_name;
1543 return %res;
1546 ## ----------------------------------------------------------------------
1547 ## git utility subroutines, invoking git commands
1549 # returns path to the core git executable and the --git-dir parameter as list
1550 sub git_cmd {
1551 return $GIT, '--git-dir='.$git_dir;
1554 # quote the given arguments for passing them to the shell
1555 # quote_command("command", "arg 1", "arg with ' and ! characters")
1556 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1557 # Try to avoid using this function wherever possible.
1558 sub quote_command {
1559 return join(' ',
1560 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1563 # get HEAD ref of given project as hash
1564 sub git_get_head_hash {
1565 my $project = shift;
1566 my $o_git_dir = $git_dir;
1567 my $retval = undef;
1568 $git_dir = "$projectroot/$project";
1569 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1570 my $head = <$fd>;
1571 close $fd;
1572 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1573 $retval = $1;
1576 if (defined $o_git_dir) {
1577 $git_dir = $o_git_dir;
1579 return $retval;
1582 # get type of given object
1583 sub git_get_type {
1584 my $hash = shift;
1586 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1587 my $type = <$fd>;
1588 close $fd or return;
1589 chomp $type;
1590 return $type;
1593 # repository configuration
1594 our $config_file = '';
1595 our %config;
1597 # store multiple values for single key as anonymous array reference
1598 # single values stored directly in the hash, not as [ <value> ]
1599 sub hash_set_multi {
1600 my ($hash, $key, $value) = @_;
1602 if (!exists $hash->{$key}) {
1603 $hash->{$key} = $value;
1604 } elsif (!ref $hash->{$key}) {
1605 $hash->{$key} = [ $hash->{$key}, $value ];
1606 } else {
1607 push @{$hash->{$key}}, $value;
1611 # return hash of git project configuration
1612 # optionally limited to some section, e.g. 'gitweb'
1613 sub git_parse_project_config {
1614 my $section_regexp = shift;
1615 my %config;
1617 local $/ = "\0";
1619 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1620 or return;
1622 while (my $keyval = <$fh>) {
1623 chomp $keyval;
1624 my ($key, $value) = split(/\n/, $keyval, 2);
1626 hash_set_multi(\%config, $key, $value)
1627 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1629 close $fh;
1631 return %config;
1634 # convert config value to boolean, 'true' or 'false'
1635 # no value, number > 0, 'true' and 'yes' values are true
1636 # rest of values are treated as false (never as error)
1637 sub config_to_bool {
1638 my $val = shift;
1640 # strip leading and trailing whitespace
1641 $val =~ s/^\s+//;
1642 $val =~ s/\s+$//;
1644 return (!defined $val || # section.key
1645 ($val =~ /^\d+$/ && $val) || # section.key = 1
1646 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1649 # convert config value to simple decimal number
1650 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1651 # to be multiplied by 1024, 1048576, or 1073741824
1652 sub config_to_int {
1653 my $val = shift;
1655 # strip leading and trailing whitespace
1656 $val =~ s/^\s+//;
1657 $val =~ s/\s+$//;
1659 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1660 $unit = lc($unit);
1661 # unknown unit is treated as 1
1662 return $num * ($unit eq 'g' ? 1073741824 :
1663 $unit eq 'm' ? 1048576 :
1664 $unit eq 'k' ? 1024 : 1);
1666 return $val;
1669 # convert config value to array reference, if needed
1670 sub config_to_multi {
1671 my $val = shift;
1673 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1676 sub git_get_project_config {
1677 my ($key, $type) = @_;
1679 # key sanity check
1680 return unless ($key);
1681 $key =~ s/^gitweb\.//;
1682 return if ($key =~ m/\W/);
1684 # type sanity check
1685 if (defined $type) {
1686 $type =~ s/^--//;
1687 $type = undef
1688 unless ($type eq 'bool' || $type eq 'int');
1691 # get config
1692 if (!defined $config_file ||
1693 $config_file ne "$git_dir/config") {
1694 %config = git_parse_project_config('gitweb');
1695 $config_file = "$git_dir/config";
1698 # ensure given type
1699 if (!defined $type) {
1700 return $config{"gitweb.$key"};
1701 } elsif ($type eq 'bool') {
1702 # backward compatibility: 'git config --bool' returns true/false
1703 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1704 } elsif ($type eq 'int') {
1705 return config_to_int($config{"gitweb.$key"});
1707 return $config{"gitweb.$key"};
1710 # get hash of given path at given ref
1711 sub git_get_hash_by_path {
1712 my $base = shift;
1713 my $path = shift || return undef;
1714 my $type = shift;
1716 $path =~ s,/+$,,;
1718 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1719 or die_error(500, "Open git-ls-tree failed");
1720 my $line = <$fd>;
1721 close $fd or return undef;
1723 if (!defined $line) {
1724 # there is no tree or hash given by $path at $base
1725 return undef;
1728 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1729 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1730 if (defined $type && $type ne $2) {
1731 # type doesn't match
1732 return undef;
1734 return $3;
1737 # get path of entry with given hash at given tree-ish (ref)
1738 # used to get 'from' filename for combined diff (merge commit) for renames
1739 sub git_get_path_by_hash {
1740 my $base = shift || return;
1741 my $hash = shift || return;
1743 local $/ = "\0";
1745 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1746 or return undef;
1747 while (my $line = <$fd>) {
1748 chomp $line;
1750 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1751 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1752 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1753 close $fd;
1754 return $1;
1757 close $fd;
1758 return undef;
1761 ## ......................................................................
1762 ## git utility functions, directly accessing git repository
1764 sub git_get_project_description {
1765 my $path = shift;
1767 $git_dir = "$projectroot/$path";
1768 open my $fd, "$git_dir/description"
1769 or return git_get_project_config('description');
1770 my $descr = <$fd>;
1771 close $fd;
1772 if (defined $descr) {
1773 chomp $descr;
1775 return $descr;
1778 sub git_get_project_url_list {
1779 my $path = shift;
1781 $git_dir = "$projectroot/$path";
1782 open my $fd, "$git_dir/cloneurl"
1783 or return wantarray ?
1784 @{ config_to_multi(git_get_project_config('url')) } :
1785 config_to_multi(git_get_project_config('url'));
1786 my @git_project_url_list = map { chomp; $_ } <$fd>;
1787 close $fd;
1789 return wantarray ? @git_project_url_list : \@git_project_url_list;
1792 sub git_get_projects_list {
1793 my ($filter) = @_;
1794 my @list;
1796 $filter ||= '';
1797 $filter =~ s/\.git$//;
1799 my ($check_forks) = gitweb_check_feature('forks');
1801 if (-d $projects_list) {
1802 # search in directory
1803 my $dir = $projects_list . ($filter ? "/$filter" : '');
1804 # remove the trailing "/"
1805 $dir =~ s!/+$!!;
1806 my $pfxlen = length("$dir");
1807 my $pfxdepth = ($dir =~ tr!/!!);
1809 File::Find::find({
1810 follow_fast => 1, # follow symbolic links
1811 follow_skip => 2, # ignore duplicates
1812 dangling_symlinks => 0, # ignore dangling symlinks, silently
1813 wanted => sub {
1814 # skip project-list toplevel, if we get it.
1815 return if (m!^[/.]$!);
1816 # only directories can be git repositories
1817 return unless (-d $_);
1818 # don't traverse too deep (Find is super slow on os x)
1819 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1820 $File::Find::prune = 1;
1821 return;
1824 my $subdir = substr($File::Find::name, $pfxlen + 1);
1825 # we check related file in $projectroot
1826 if ($check_forks and $subdir =~ m#/.#) {
1827 $File::Find::prune = 1;
1828 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1829 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1830 $File::Find::prune = 1;
1833 }, "$dir");
1835 } elsif (-f $projects_list) {
1836 # read from file(url-encoded):
1837 # 'git%2Fgit.git Linus+Torvalds'
1838 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1839 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1840 my %paths;
1841 open my ($fd), $projects_list or return;
1842 PROJECT:
1843 while (my $line = <$fd>) {
1844 chomp $line;
1845 my ($path, $owner) = split ' ', $line;
1846 $path = unescape($path);
1847 $owner = unescape($owner);
1848 if (!defined $path) {
1849 next;
1851 if ($filter ne '') {
1852 # looking for forks;
1853 my $pfx = substr($path, 0, length($filter));
1854 if ($pfx ne $filter) {
1855 next PROJECT;
1857 my $sfx = substr($path, length($filter));
1858 if ($sfx !~ /^\/.*\.git$/) {
1859 next PROJECT;
1861 } elsif ($check_forks) {
1862 PATH:
1863 foreach my $filter (keys %paths) {
1864 # looking for forks;
1865 my $pfx = substr($path, 0, length($filter));
1866 if ($pfx ne $filter) {
1867 next PATH;
1869 my $sfx = substr($path, length($filter));
1870 if ($sfx !~ /^\/.*\.git$/) {
1871 next PATH;
1873 # is a fork, don't include it in
1874 # the list
1875 next PROJECT;
1878 if (check_export_ok("$projectroot/$path")) {
1879 my $pr = {
1880 path => $path,
1881 owner => to_utf8($owner),
1883 push @list, $pr;
1884 (my $forks_path = $path) =~ s/\.git$//;
1885 $paths{$forks_path}++;
1888 close $fd;
1890 return @list;
1893 our $gitweb_project_owner = undef;
1894 sub git_get_project_list_from_file {
1896 return if (defined $gitweb_project_owner);
1898 $gitweb_project_owner = {};
1899 # read from file (url-encoded):
1900 # 'git%2Fgit.git Linus+Torvalds'
1901 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1902 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1903 if (-f $projects_list) {
1904 open (my $fd , $projects_list);
1905 while (my $line = <$fd>) {
1906 chomp $line;
1907 my ($pr, $ow) = split ' ', $line;
1908 $pr = unescape($pr);
1909 $ow = unescape($ow);
1910 $gitweb_project_owner->{$pr} = to_utf8($ow);
1912 close $fd;
1916 sub git_get_project_owner {
1917 my $project = shift;
1918 my $owner;
1920 return undef unless $project;
1921 $git_dir = "$projectroot/$project";
1923 if (!defined $gitweb_project_owner) {
1924 git_get_project_list_from_file();
1927 if (exists $gitweb_project_owner->{$project}) {
1928 $owner = $gitweb_project_owner->{$project};
1930 if (!defined $owner){
1931 $owner = git_get_project_config('owner');
1933 if (!defined $owner) {
1934 $owner = get_file_owner("$git_dir");
1937 return $owner;
1940 sub git_get_last_activity {
1941 my ($path) = @_;
1942 my $fd;
1944 $git_dir = "$projectroot/$path";
1945 open($fd, "-|", git_cmd(), 'for-each-ref',
1946 '--format=%(committer)',
1947 '--sort=-committerdate',
1948 '--count=1',
1949 'refs/heads') or return;
1950 my $most_recent = <$fd>;
1951 close $fd or return;
1952 if (defined $most_recent &&
1953 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1954 my $timestamp = $1;
1955 my $age = time - $timestamp;
1956 return ($age, age_string($age));
1958 return (undef, undef);
1961 sub git_get_references {
1962 my $type = shift || "";
1963 my %refs;
1964 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1965 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1966 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1967 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
1968 or return;
1970 while (my $line = <$fd>) {
1971 chomp $line;
1972 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
1973 if (defined $refs{$1}) {
1974 push @{$refs{$1}}, $2;
1975 } else {
1976 $refs{$1} = [ $2 ];
1980 close $fd or return;
1981 return \%refs;
1984 sub git_get_rev_name_tags {
1985 my $hash = shift || return undef;
1987 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
1988 or return;
1989 my $name_rev = <$fd>;
1990 close $fd;
1992 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1993 return $1;
1994 } else {
1995 # catches also '$hash undefined' output
1996 return undef;
2000 ## ----------------------------------------------------------------------
2001 ## parse to hash functions
2003 sub parse_date {
2004 my $epoch = shift;
2005 my $tz = shift || "-0000";
2007 my %date;
2008 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2009 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2010 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2011 $date{'hour'} = $hour;
2012 $date{'minute'} = $min;
2013 $date{'mday'} = $mday;
2014 $date{'day'} = $days[$wday];
2015 $date{'month'} = $months[$mon];
2016 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2017 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2018 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2019 $mday, $months[$mon], $hour ,$min;
2020 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2021 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2023 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2024 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2025 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2026 $date{'hour_local'} = $hour;
2027 $date{'minute_local'} = $min;
2028 $date{'tz_local'} = $tz;
2029 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2030 1900+$year, $mon+1, $mday,
2031 $hour, $min, $sec, $tz);
2032 return %date;
2035 sub parse_tag {
2036 my $tag_id = shift;
2037 my %tag;
2038 my @comment;
2040 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2041 $tag{'id'} = $tag_id;
2042 while (my $line = <$fd>) {
2043 chomp $line;
2044 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2045 $tag{'object'} = $1;
2046 } elsif ($line =~ m/^type (.+)$/) {
2047 $tag{'type'} = $1;
2048 } elsif ($line =~ m/^tag (.+)$/) {
2049 $tag{'name'} = $1;
2050 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2051 $tag{'author'} = $1;
2052 $tag{'epoch'} = $2;
2053 $tag{'tz'} = $3;
2054 } elsif ($line =~ m/--BEGIN/) {
2055 push @comment, $line;
2056 last;
2057 } elsif ($line eq "") {
2058 last;
2061 push @comment, <$fd>;
2062 $tag{'comment'} = \@comment;
2063 close $fd or return;
2064 if (!defined $tag{'name'}) {
2065 return
2067 return %tag
2070 sub parse_commit_text {
2071 my ($commit_text, $withparents) = @_;
2072 my @commit_lines = split '\n', $commit_text;
2073 my %co;
2075 pop @commit_lines; # Remove '\0'
2077 if (! @commit_lines) {
2078 return;
2081 my $header = shift @commit_lines;
2082 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2083 return;
2085 ($co{'id'}, my @parents) = split ' ', $header;
2086 while (my $line = shift @commit_lines) {
2087 last if $line eq "\n";
2088 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2089 $co{'tree'} = $1;
2090 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2091 push @parents, $1;
2092 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2093 $co{'author'} = $1;
2094 $co{'author_epoch'} = $2;
2095 $co{'author_tz'} = $3;
2096 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2097 $co{'author_name'} = $1;
2098 $co{'author_email'} = $2;
2099 } else {
2100 $co{'author_name'} = $co{'author'};
2102 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2103 $co{'committer'} = $1;
2104 $co{'committer_epoch'} = $2;
2105 $co{'committer_tz'} = $3;
2106 $co{'committer_name'} = $co{'committer'};
2107 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2108 $co{'committer_name'} = $1;
2109 $co{'committer_email'} = $2;
2110 } else {
2111 $co{'committer_name'} = $co{'committer'};
2115 if (!defined $co{'tree'}) {
2116 return;
2118 $co{'parents'} = \@parents;
2119 $co{'parent'} = $parents[0];
2121 foreach my $title (@commit_lines) {
2122 $title =~ s/^ //;
2123 if ($title ne "") {
2124 $co{'title'} = chop_str($title, 80, 5);
2125 # remove leading stuff of merges to make the interesting part visible
2126 if (length($title) > 50) {
2127 $title =~ s/^Automatic //;
2128 $title =~ s/^merge (of|with) /Merge ... /i;
2129 if (length($title) > 50) {
2130 $title =~ s/(http|rsync):\/\///;
2132 if (length($title) > 50) {
2133 $title =~ s/(master|www|rsync)\.//;
2135 if (length($title) > 50) {
2136 $title =~ s/kernel.org:?//;
2138 if (length($title) > 50) {
2139 $title =~ s/\/pub\/scm//;
2142 $co{'title_short'} = chop_str($title, 50, 5);
2143 last;
2146 if (! defined $co{'title'} || $co{'title'} eq "") {
2147 $co{'title'} = $co{'title_short'} = '(no commit message)';
2149 # remove added spaces
2150 foreach my $line (@commit_lines) {
2151 $line =~ s/^ //;
2153 $co{'comment'} = \@commit_lines;
2155 my $age = time - $co{'committer_epoch'};
2156 $co{'age'} = $age;
2157 $co{'age_string'} = age_string($age);
2158 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2159 if ($age > 60*60*24*7*2) {
2160 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2161 $co{'age_string_age'} = $co{'age_string'};
2162 } else {
2163 $co{'age_string_date'} = $co{'age_string'};
2164 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2166 return %co;
2169 sub parse_commit {
2170 my ($commit_id) = @_;
2171 my %co;
2173 local $/ = "\0";
2175 open my $fd, "-|", git_cmd(), "rev-list",
2176 "--parents",
2177 "--header",
2178 "--max-count=1",
2179 $commit_id,
2180 "--",
2181 or die_error(500, "Open git-rev-list failed");
2182 %co = parse_commit_text(<$fd>, 1);
2183 close $fd;
2185 return %co;
2188 sub parse_commits {
2189 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2190 my @cos;
2192 $maxcount ||= 1;
2193 $skip ||= 0;
2195 local $/ = "\0";
2197 open my $fd, "-|", git_cmd(), "rev-list",
2198 "--header",
2199 @args,
2200 ("--max-count=" . $maxcount),
2201 ("--skip=" . $skip),
2202 @extra_options,
2203 $commit_id,
2204 "--",
2205 ($filename ? ($filename) : ())
2206 or die_error(500, "Open git-rev-list failed");
2207 while (my $line = <$fd>) {
2208 my %co = parse_commit_text($line);
2209 push @cos, \%co;
2211 close $fd;
2213 return wantarray ? @cos : \@cos;
2216 # parse line of git-diff-tree "raw" output
2217 sub parse_difftree_raw_line {
2218 my $line = shift;
2219 my %res;
2221 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2222 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2223 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2224 $res{'from_mode'} = $1;
2225 $res{'to_mode'} = $2;
2226 $res{'from_id'} = $3;
2227 $res{'to_id'} = $4;
2228 $res{'status'} = $5;
2229 $res{'similarity'} = $6;
2230 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2231 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2232 } else {
2233 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2236 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2237 # combined diff (for merge commit)
2238 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2239 $res{'nparents'} = length($1);
2240 $res{'from_mode'} = [ split(' ', $2) ];
2241 $res{'to_mode'} = pop @{$res{'from_mode'}};
2242 $res{'from_id'} = [ split(' ', $3) ];
2243 $res{'to_id'} = pop @{$res{'from_id'}};
2244 $res{'status'} = [ split('', $4) ];
2245 $res{'to_file'} = unquote($5);
2247 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2248 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2249 $res{'commit'} = $1;
2252 return wantarray ? %res : \%res;
2255 # wrapper: return parsed line of git-diff-tree "raw" output
2256 # (the argument might be raw line, or parsed info)
2257 sub parsed_difftree_line {
2258 my $line_or_ref = shift;
2260 if (ref($line_or_ref) eq "HASH") {
2261 # pre-parsed (or generated by hand)
2262 return $line_or_ref;
2263 } else {
2264 return parse_difftree_raw_line($line_or_ref);
2268 # parse line of git-ls-tree output
2269 sub parse_ls_tree_line ($;%) {
2270 my $line = shift;
2271 my %opts = @_;
2272 my %res;
2274 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2275 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2277 $res{'mode'} = $1;
2278 $res{'type'} = $2;
2279 $res{'hash'} = $3;
2280 if ($opts{'-z'}) {
2281 $res{'name'} = $4;
2282 } else {
2283 $res{'name'} = unquote($4);
2286 return wantarray ? %res : \%res;
2289 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2290 sub parse_from_to_diffinfo {
2291 my ($diffinfo, $from, $to, @parents) = @_;
2293 if ($diffinfo->{'nparents'}) {
2294 # combined diff
2295 $from->{'file'} = [];
2296 $from->{'href'} = [];
2297 fill_from_file_info($diffinfo, @parents)
2298 unless exists $diffinfo->{'from_file'};
2299 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2300 $from->{'file'}[$i] =
2301 defined $diffinfo->{'from_file'}[$i] ?
2302 $diffinfo->{'from_file'}[$i] :
2303 $diffinfo->{'to_file'};
2304 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2305 $from->{'href'}[$i] = href(action=>"blob",
2306 hash_base=>$parents[$i],
2307 hash=>$diffinfo->{'from_id'}[$i],
2308 file_name=>$from->{'file'}[$i]);
2309 } else {
2310 $from->{'href'}[$i] = undef;
2313 } else {
2314 # ordinary (not combined) diff
2315 $from->{'file'} = $diffinfo->{'from_file'};
2316 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2317 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2318 hash=>$diffinfo->{'from_id'},
2319 file_name=>$from->{'file'});
2320 } else {
2321 delete $from->{'href'};
2325 $to->{'file'} = $diffinfo->{'to_file'};
2326 if (!is_deleted($diffinfo)) { # file exists in result
2327 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2328 hash=>$diffinfo->{'to_id'},
2329 file_name=>$to->{'file'});
2330 } else {
2331 delete $to->{'href'};
2335 ## ......................................................................
2336 ## parse to array of hashes functions
2338 sub git_get_heads_list {
2339 my $limit = shift;
2340 my @headslist;
2342 open my $fd, '-|', git_cmd(), 'for-each-ref',
2343 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2344 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2345 'refs/heads'
2346 or return;
2347 while (my $line = <$fd>) {
2348 my %ref_item;
2350 chomp $line;
2351 my ($refinfo, $committerinfo) = split(/\0/, $line);
2352 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2353 my ($committer, $epoch, $tz) =
2354 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2355 $ref_item{'fullname'} = $name;
2356 $name =~ s!^refs/heads/!!;
2358 $ref_item{'name'} = $name;
2359 $ref_item{'id'} = $hash;
2360 $ref_item{'title'} = $title || '(no commit message)';
2361 $ref_item{'epoch'} = $epoch;
2362 if ($epoch) {
2363 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2364 } else {
2365 $ref_item{'age'} = "unknown";
2368 push @headslist, \%ref_item;
2370 close $fd;
2372 return wantarray ? @headslist : \@headslist;
2375 sub git_get_tags_list {
2376 my $limit = shift;
2377 my @tagslist;
2379 open my $fd, '-|', git_cmd(), 'for-each-ref',
2380 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2381 '--format=%(objectname) %(objecttype) %(refname) '.
2382 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2383 'refs/tags'
2384 or return;
2385 while (my $line = <$fd>) {
2386 my %ref_item;
2388 chomp $line;
2389 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2390 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2391 my ($creator, $epoch, $tz) =
2392 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2393 $ref_item{'fullname'} = $name;
2394 $name =~ s!^refs/tags/!!;
2396 $ref_item{'type'} = $type;
2397 $ref_item{'id'} = $id;
2398 $ref_item{'name'} = $name;
2399 if ($type eq "tag") {
2400 $ref_item{'subject'} = $title;
2401 $ref_item{'reftype'} = $reftype;
2402 $ref_item{'refid'} = $refid;
2403 } else {
2404 $ref_item{'reftype'} = $type;
2405 $ref_item{'refid'} = $id;
2408 if ($type eq "tag" || $type eq "commit") {
2409 $ref_item{'epoch'} = $epoch;
2410 if ($epoch) {
2411 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2412 } else {
2413 $ref_item{'age'} = "unknown";
2417 push @tagslist, \%ref_item;
2419 close $fd;
2421 return wantarray ? @tagslist : \@tagslist;
2424 ## ----------------------------------------------------------------------
2425 ## filesystem-related functions
2427 sub get_file_owner {
2428 my $path = shift;
2430 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2431 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2432 if (!defined $gcos) {
2433 return undef;
2435 my $owner = $gcos;
2436 $owner =~ s/[,;].*$//;
2437 return to_utf8($owner);
2440 ## ......................................................................
2441 ## mimetype related functions
2443 sub mimetype_guess_file {
2444 my $filename = shift;
2445 my $mimemap = shift;
2446 -r $mimemap or return undef;
2448 my %mimemap;
2449 open(MIME, $mimemap) or return undef;
2450 while (<MIME>) {
2451 next if m/^#/; # skip comments
2452 my ($mime, $exts) = split(/\t+/);
2453 if (defined $exts) {
2454 my @exts = split(/\s+/, $exts);
2455 foreach my $ext (@exts) {
2456 $mimemap{$ext} = $mime;
2460 close(MIME);
2462 $filename =~ /\.([^.]*)$/;
2463 return $mimemap{$1};
2466 sub mimetype_guess {
2467 my $filename = shift;
2468 my $mime;
2469 $filename =~ /\./ or return undef;
2471 if ($mimetypes_file) {
2472 my $file = $mimetypes_file;
2473 if ($file !~ m!^/!) { # if it is relative path
2474 # it is relative to project
2475 $file = "$projectroot/$project/$file";
2477 $mime = mimetype_guess_file($filename, $file);
2479 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2480 return $mime;
2483 sub blob_mimetype {
2484 my $fd = shift;
2485 my $filename = shift;
2487 if ($filename) {
2488 my $mime = mimetype_guess($filename);
2489 $mime and return $mime;
2492 # just in case
2493 return $default_blob_plain_mimetype unless $fd;
2495 if (-T $fd) {
2496 return 'text/plain';
2497 } elsif (! $filename) {
2498 return 'application/octet-stream';
2499 } elsif ($filename =~ m/\.png$/i) {
2500 return 'image/png';
2501 } elsif ($filename =~ m/\.gif$/i) {
2502 return 'image/gif';
2503 } elsif ($filename =~ m/\.jpe?g$/i) {
2504 return 'image/jpeg';
2505 } else {
2506 return 'application/octet-stream';
2510 sub blob_contenttype {
2511 my ($fd, $file_name, $type) = @_;
2513 $type ||= blob_mimetype($fd, $file_name);
2514 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2515 $type .= "; charset=$default_text_plain_charset";
2518 return $type;
2521 ## ======================================================================
2522 ## functions printing HTML: header, footer, error page
2524 sub git_header_html {
2525 my $status = shift || "200 OK";
2526 my $expires = shift;
2528 my $title = "$site_name";
2529 if (defined $project) {
2530 $title .= " - " . to_utf8($project);
2531 if (defined $action) {
2532 $title .= "/$action";
2533 if (defined $file_name) {
2534 $title .= " - " . esc_path($file_name);
2535 if ($action eq "tree" && $file_name !~ m|/$|) {
2536 $title .= "/";
2541 my $content_type;
2542 # require explicit support from the UA if we are to send the page as
2543 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
2544 # we have to do this because MSIE sometimes globs '*/*', pretending to
2545 # support xhtml+xml but choking when it gets what it asked for.
2546 if (defined $cgi->http('HTTP_ACCEPT') &&
2547 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
2548 $cgi->Accept('application/xhtml+xml') != 0) {
2549 $content_type = 'application/xhtml+xml';
2550 } else {
2551 $content_type = 'text/html';
2553 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2554 -status=> $status, -expires => $expires);
2555 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2556 print <<EOF;
2557 <?xml version="1.0" encoding="utf-8"?>
2558 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2559 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2560 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2561 <!-- git core binaries version $git_version -->
2562 <head>
2563 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2564 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2565 <meta name="robots" content="index, nofollow"/>
2566 <title>$title</title>
2568 # print out each stylesheet that exist
2569 if (defined $stylesheet) {
2570 #provides backwards capability for those people who define style sheet in a config file
2571 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2572 } else {
2573 foreach my $stylesheet (@stylesheets) {
2574 next unless $stylesheet;
2575 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2578 if (defined $project) {
2579 my %href_params = get_feed_info();
2580 if (!exists $href_params{'-title'}) {
2581 $href_params{'-title'} = 'log';
2584 foreach my $format qw(RSS Atom) {
2585 my $type = lc($format);
2586 my %link_attr = (
2587 '-rel' => 'alternate',
2588 '-title' => "$project - $href_params{'-title'} - $format feed",
2589 '-type' => "application/$type+xml"
2592 $href_params{'action'} = $type;
2593 $link_attr{'-href'} = href(%href_params);
2594 print "<link ".
2595 "rel=\"$link_attr{'-rel'}\" ".
2596 "title=\"$link_attr{'-title'}\" ".
2597 "href=\"$link_attr{'-href'}\" ".
2598 "type=\"$link_attr{'-type'}\" ".
2599 "/>\n";
2601 $href_params{'extra_options'} = '--no-merges';
2602 $link_attr{'-href'} = href(%href_params);
2603 $link_attr{'-title'} .= ' (no merges)';
2604 print "<link ".
2605 "rel=\"$link_attr{'-rel'}\" ".
2606 "title=\"$link_attr{'-title'}\" ".
2607 "href=\"$link_attr{'-href'}\" ".
2608 "type=\"$link_attr{'-type'}\" ".
2609 "/>\n";
2612 } else {
2613 printf('<link rel="alternate" title="%s projects list" '.
2614 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2615 $site_name, href(project=>undef, action=>"project_index"));
2616 printf('<link rel="alternate" title="%s projects feeds" '.
2617 'href="%s" type="text/x-opml" />'."\n",
2618 $site_name, href(project=>undef, action=>"opml"));
2620 if (defined $favicon) {
2621 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2624 print "</head>\n" .
2625 "<body>\n";
2627 if (-f $site_header) {
2628 open (my $fd, $site_header);
2629 print <$fd>;
2630 close $fd;
2633 print "<div class=\"page_header\">\n" .
2634 $cgi->a({-href => esc_url($logo_url),
2635 -title => $logo_label},
2636 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2637 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2638 if (defined $project) {
2639 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2640 if (defined $action) {
2641 print " / $action";
2643 print "\n";
2645 print "</div>\n";
2647 my ($have_search) = gitweb_check_feature('search');
2648 if (defined $project && $have_search) {
2649 if (!defined $searchtext) {
2650 $searchtext = "";
2652 my $search_hash;
2653 if (defined $hash_base) {
2654 $search_hash = $hash_base;
2655 } elsif (defined $hash) {
2656 $search_hash = $hash;
2657 } else {
2658 $search_hash = "HEAD";
2660 my $action = $my_uri;
2661 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2662 if ($use_pathinfo) {
2663 $action .= "/".esc_url($project);
2665 print $cgi->startform(-method => "get", -action => $action) .
2666 "<div class=\"search\">\n" .
2667 (!$use_pathinfo &&
2668 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2669 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2670 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2671 $cgi->popup_menu(-name => 'st', -default => 'commit',
2672 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2673 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2674 " search:\n",
2675 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2676 "<span title=\"Extended regular expression\">" .
2677 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
2678 -checked => $search_use_regexp) .
2679 "</span>" .
2680 "</div>" .
2681 $cgi->end_form() . "\n";
2685 sub git_footer_html {
2686 my $feed_class = 'rss_logo';
2688 print "<div class=\"page_footer\">\n";
2689 if (defined $project) {
2690 my $descr = git_get_project_description($project);
2691 if (defined $descr) {
2692 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2695 my %href_params = get_feed_info();
2696 if (!%href_params) {
2697 $feed_class .= ' generic';
2699 $href_params{'-title'} ||= 'log';
2701 foreach my $format qw(RSS Atom) {
2702 $href_params{'action'} = lc($format);
2703 print $cgi->a({-href => href(%href_params),
2704 -title => "$href_params{'-title'} $format feed",
2705 -class => $feed_class}, $format)."\n";
2708 } else {
2709 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2710 -class => $feed_class}, "OPML") . " ";
2711 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2712 -class => $feed_class}, "TXT") . "\n";
2714 print "</div>\n"; # class="page_footer"
2716 if (-f $site_footer) {
2717 open (my $fd, $site_footer);
2718 print <$fd>;
2719 close $fd;
2722 print "</body>\n" .
2723 "</html>";
2726 # die_error(<http_status_code>, <error_message>)
2727 # Example: die_error(404, 'Hash not found')
2728 # By convention, use the following status codes (as defined in RFC 2616):
2729 # 400: Invalid or missing CGI parameters, or
2730 # requested object exists but has wrong type.
2731 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
2732 # this server or project.
2733 # 404: Requested object/revision/project doesn't exist.
2734 # 500: The server isn't configured properly, or
2735 # an internal error occurred (e.g. failed assertions caused by bugs), or
2736 # an unknown error occurred (e.g. the git binary died unexpectedly).
2737 sub die_error {
2738 my $status = shift || 500;
2739 my $error = shift || "Internal server error";
2741 my %http_responses = (400 => '400 Bad Request',
2742 403 => '403 Forbidden',
2743 404 => '404 Not Found',
2744 500 => '500 Internal Server Error');
2745 git_header_html($http_responses{$status});
2746 print <<EOF;
2747 <div class="page_body">
2748 <br /><br />
2749 $status - $error
2750 <br />
2751 </div>
2753 git_footer_html();
2754 exit;
2757 ## ----------------------------------------------------------------------
2758 ## functions printing or outputting HTML: navigation
2760 sub git_print_page_nav {
2761 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2762 $extra = '' if !defined $extra; # pager or formats
2764 my @navs = qw(summary shortlog log commit commitdiff tree);
2765 if ($suppress) {
2766 @navs = grep { $_ ne $suppress } @navs;
2769 my %arg = map { $_ => {action=>$_} } @navs;
2770 if (defined $head) {
2771 for (qw(commit commitdiff)) {
2772 $arg{$_}{'hash'} = $head;
2774 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2775 for (qw(shortlog log)) {
2776 $arg{$_}{'hash'} = $head;
2781 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2782 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2784 my @actions = gitweb_check_feature('actions');
2785 while (@actions) {
2786 my ($label, $link, $pos) = (shift(@actions), shift(@actions), shift(@actions));
2787 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
2788 # munch munch
2789 $link =~ s#%n#$project#g;
2790 $link =~ s#%f#$git_dir#g;
2791 $treehead ? $link =~ s#%h#$treehead#g : $link =~ s#%h##g;
2792 $treebase ? $link =~ s#%b#$treebase#g : $link =~ s#%b##g;
2793 $arg{$label}{'_href'} = $link;
2796 print "<div class=\"page_nav\">\n" .
2797 (join " | ",
2798 map { $_ eq $current ?
2799 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
2800 } @navs);
2801 print "<br/>\n$extra<br/>\n" .
2802 "</div>\n";
2805 sub format_paging_nav {
2806 my ($action, $hash, $head, $page, $has_next_link) = @_;
2807 my $paging_nav;
2810 if ($hash ne $head || $page) {
2811 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2812 } else {
2813 $paging_nav .= "HEAD";
2816 if ($page > 0) {
2817 $paging_nav .= " &sdot; " .
2818 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2819 -accesskey => "p", -title => "Alt-p"}, "prev");
2820 } else {
2821 $paging_nav .= " &sdot; prev";
2824 if ($has_next_link) {
2825 $paging_nav .= " &sdot; " .
2826 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2827 -accesskey => "n", -title => "Alt-n"}, "next");
2828 } else {
2829 $paging_nav .= " &sdot; next";
2832 return $paging_nav;
2835 ## ......................................................................
2836 ## functions printing or outputting HTML: div
2838 sub git_print_header_div {
2839 my ($action, $title, $hash, $hash_base) = @_;
2840 my %args = ();
2842 $args{'action'} = $action;
2843 $args{'hash'} = $hash if $hash;
2844 $args{'hash_base'} = $hash_base if $hash_base;
2846 print "<div class=\"header\">\n" .
2847 $cgi->a({-href => href(%args), -class => "title"},
2848 $title ? $title : $action) .
2849 "\n</div>\n";
2852 #sub git_print_authorship (\%) {
2853 sub git_print_authorship {
2854 my $co = shift;
2856 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2857 print "<div class=\"author_date\">" .
2858 esc_html($co->{'author_name'}) .
2859 " [$ad{'rfc2822'}";
2860 if ($ad{'hour_local'} < 6) {
2861 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2862 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2863 } else {
2864 printf(" (%02d:%02d %s)",
2865 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2867 print "]</div>\n";
2870 sub git_print_page_path {
2871 my $name = shift;
2872 my $type = shift;
2873 my $hb = shift;
2876 print "<div class=\"page_path\">";
2877 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
2878 -title => 'tree root'}, to_utf8("[$project]"));
2879 print " / ";
2880 if (defined $name) {
2881 my @dirname = split '/', $name;
2882 my $basename = pop @dirname;
2883 my $fullname = '';
2885 foreach my $dir (@dirname) {
2886 $fullname .= ($fullname ? '/' : '') . $dir;
2887 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
2888 hash_base=>$hb),
2889 -title => $fullname}, esc_path($dir));
2890 print " / ";
2892 if (defined $type && $type eq 'blob') {
2893 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
2894 hash_base=>$hb),
2895 -title => $name}, esc_path($basename));
2896 } elsif (defined $type && $type eq 'tree') {
2897 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
2898 hash_base=>$hb),
2899 -title => $name}, esc_path($basename));
2900 print " / ";
2901 } else {
2902 print esc_path($basename);
2905 print "<br/></div>\n";
2908 # sub git_print_log (\@;%) {
2909 sub git_print_log ($;%) {
2910 my $log = shift;
2911 my %opts = @_;
2913 if ($opts{'-remove_title'}) {
2914 # remove title, i.e. first line of log
2915 shift @$log;
2917 # remove leading empty lines
2918 while (defined $log->[0] && $log->[0] eq "") {
2919 shift @$log;
2922 # print log
2923 my $signoff = 0;
2924 my $empty = 0;
2925 foreach my $line (@$log) {
2926 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2927 $signoff = 1;
2928 $empty = 0;
2929 if (! $opts{'-remove_signoff'}) {
2930 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2931 next;
2932 } else {
2933 # remove signoff lines
2934 next;
2936 } else {
2937 $signoff = 0;
2940 # print only one empty line
2941 # do not print empty line after signoff
2942 if ($line eq "") {
2943 next if ($empty || $signoff);
2944 $empty = 1;
2945 } else {
2946 $empty = 0;
2949 print format_log_line_html($line) . "<br/>\n";
2952 if ($opts{'-final_empty_line'}) {
2953 # end with single empty line
2954 print "<br/>\n" unless $empty;
2958 # return link target (what link points to)
2959 sub git_get_link_target {
2960 my $hash = shift;
2961 my $link_target;
2963 # read link
2964 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2965 or return;
2967 local $/;
2968 $link_target = <$fd>;
2970 close $fd
2971 or return;
2973 return $link_target;
2976 # given link target, and the directory (basedir) the link is in,
2977 # return target of link relative to top directory (top tree);
2978 # return undef if it is not possible (including absolute links).
2979 sub normalize_link_target {
2980 my ($link_target, $basedir, $hash_base) = @_;
2982 # we can normalize symlink target only if $hash_base is provided
2983 return unless $hash_base;
2985 # absolute symlinks (beginning with '/') cannot be normalized
2986 return if (substr($link_target, 0, 1) eq '/');
2988 # normalize link target to path from top (root) tree (dir)
2989 my $path;
2990 if ($basedir) {
2991 $path = $basedir . '/' . $link_target;
2992 } else {
2993 # we are in top (root) tree (dir)
2994 $path = $link_target;
2997 # remove //, /./, and /../
2998 my @path_parts;
2999 foreach my $part (split('/', $path)) {
3000 # discard '.' and ''
3001 next if (!$part || $part eq '.');
3002 # handle '..'
3003 if ($part eq '..') {
3004 if (@path_parts) {
3005 pop @path_parts;
3006 } else {
3007 # link leads outside repository (outside top dir)
3008 return;
3010 } else {
3011 push @path_parts, $part;
3014 $path = join('/', @path_parts);
3016 return $path;
3019 # print tree entry (row of git_tree), but without encompassing <tr> element
3020 sub git_print_tree_entry {
3021 my ($t, $basedir, $hash_base, $have_blame) = @_;
3023 my %base_key = ();
3024 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3026 # The format of a table row is: mode list link. Where mode is
3027 # the mode of the entry, list is the name of the entry, an href,
3028 # and link is the action links of the entry.
3030 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3031 if ($t->{'type'} eq "blob") {
3032 print "<td class=\"list\">" .
3033 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3034 file_name=>"$basedir$t->{'name'}", %base_key),
3035 -class => "list"}, esc_path($t->{'name'}));
3036 if (S_ISLNK(oct $t->{'mode'})) {
3037 my $link_target = git_get_link_target($t->{'hash'});
3038 if ($link_target) {
3039 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3040 if (defined $norm_target) {
3041 print " -> " .
3042 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3043 file_name=>$norm_target),
3044 -title => $norm_target}, esc_path($link_target));
3045 } else {
3046 print " -> " . esc_path($link_target);
3050 print "</td>\n";
3051 print "<td class=\"link\">";
3052 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3053 file_name=>"$basedir$t->{'name'}", %base_key)},
3054 "blob");
3055 if ($have_blame) {
3056 print " | " .
3057 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3058 file_name=>"$basedir$t->{'name'}", %base_key)},
3059 "blame");
3061 if (defined $hash_base) {
3062 print " | " .
3063 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3064 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3065 "history");
3067 print " | " .
3068 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3069 file_name=>"$basedir$t->{'name'}")},
3070 "raw");
3071 print "</td>\n";
3073 } elsif ($t->{'type'} eq "tree") {
3074 print "<td class=\"list\">";
3075 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3076 file_name=>"$basedir$t->{'name'}", %base_key)},
3077 esc_path($t->{'name'}));
3078 print "</td>\n";
3079 print "<td class=\"link\">";
3080 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3081 file_name=>"$basedir$t->{'name'}", %base_key)},
3082 "tree");
3083 if (defined $hash_base) {
3084 print " | " .
3085 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3086 file_name=>"$basedir$t->{'name'}")},
3087 "history");
3089 print "</td>\n";
3090 } else {
3091 # unknown object: we can only present history for it
3092 # (this includes 'commit' object, i.e. submodule support)
3093 print "<td class=\"list\">" .
3094 esc_path($t->{'name'}) .
3095 "</td>\n";
3096 print "<td class=\"link\">";
3097 if (defined $hash_base) {
3098 print $cgi->a({-href => href(action=>"history",
3099 hash_base=>$hash_base,
3100 file_name=>"$basedir$t->{'name'}")},
3101 "history");
3103 print "</td>\n";
3107 ## ......................................................................
3108 ## functions printing large fragments of HTML
3110 # get pre-image filenames for merge (combined) diff
3111 sub fill_from_file_info {
3112 my ($diff, @parents) = @_;
3114 $diff->{'from_file'} = [ ];
3115 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3116 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3117 if ($diff->{'status'}[$i] eq 'R' ||
3118 $diff->{'status'}[$i] eq 'C') {
3119 $diff->{'from_file'}[$i] =
3120 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3124 return $diff;
3127 # is current raw difftree line of file deletion
3128 sub is_deleted {
3129 my $diffinfo = shift;
3131 return $diffinfo->{'to_id'} eq ('0' x 40);
3134 # does patch correspond to [previous] difftree raw line
3135 # $diffinfo - hashref of parsed raw diff format
3136 # $patchinfo - hashref of parsed patch diff format
3137 # (the same keys as in $diffinfo)
3138 sub is_patch_split {
3139 my ($diffinfo, $patchinfo) = @_;
3141 return defined $diffinfo && defined $patchinfo
3142 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3146 sub git_difftree_body {
3147 my ($difftree, $hash, @parents) = @_;
3148 my ($parent) = $parents[0];
3149 my ($have_blame) = gitweb_check_feature('blame');
3150 print "<div class=\"list_head\">\n";
3151 if ($#{$difftree} > 10) {
3152 print(($#{$difftree} + 1) . " files changed:\n");
3154 print "</div>\n";
3156 print "<table class=\"" .
3157 (@parents > 1 ? "combined " : "") .
3158 "diff_tree\">\n";
3160 # header only for combined diff in 'commitdiff' view
3161 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3162 if ($has_header) {
3163 # table header
3164 print "<thead><tr>\n" .
3165 "<th></th><th></th>\n"; # filename, patchN link
3166 for (my $i = 0; $i < @parents; $i++) {
3167 my $par = $parents[$i];
3168 print "<th>" .
3169 $cgi->a({-href => href(action=>"commitdiff",
3170 hash=>$hash, hash_parent=>$par),
3171 -title => 'commitdiff to parent number ' .
3172 ($i+1) . ': ' . substr($par,0,7)},
3173 $i+1) .
3174 "&nbsp;</th>\n";
3176 print "</tr></thead>\n<tbody>\n";
3179 my $alternate = 1;
3180 my $patchno = 0;
3181 foreach my $line (@{$difftree}) {
3182 my $diff = parsed_difftree_line($line);
3184 if ($alternate) {
3185 print "<tr class=\"dark\">\n";
3186 } else {
3187 print "<tr class=\"light\">\n";
3189 $alternate ^= 1;
3191 if (exists $diff->{'nparents'}) { # combined diff
3193 fill_from_file_info($diff, @parents)
3194 unless exists $diff->{'from_file'};
3196 if (!is_deleted($diff)) {
3197 # file exists in the result (child) commit
3198 print "<td>" .
3199 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3200 file_name=>$diff->{'to_file'},
3201 hash_base=>$hash),
3202 -class => "list"}, esc_path($diff->{'to_file'})) .
3203 "</td>\n";
3204 } else {
3205 print "<td>" .
3206 esc_path($diff->{'to_file'}) .
3207 "</td>\n";
3210 if ($action eq 'commitdiff') {
3211 # link to patch
3212 $patchno++;
3213 print "<td class=\"link\">" .
3214 $cgi->a({-href => "#patch$patchno"}, "patch") .
3215 " | " .
3216 "</td>\n";
3219 my $has_history = 0;
3220 my $not_deleted = 0;
3221 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3222 my $hash_parent = $parents[$i];
3223 my $from_hash = $diff->{'from_id'}[$i];
3224 my $from_path = $diff->{'from_file'}[$i];
3225 my $status = $diff->{'status'}[$i];
3227 $has_history ||= ($status ne 'A');
3228 $not_deleted ||= ($status ne 'D');
3230 if ($status eq 'A') {
3231 print "<td class=\"link\" align=\"right\"> | </td>\n";
3232 } elsif ($status eq 'D') {
3233 print "<td class=\"link\">" .
3234 $cgi->a({-href => href(action=>"blob",
3235 hash_base=>$hash,
3236 hash=>$from_hash,
3237 file_name=>$from_path)},
3238 "blob" . ($i+1)) .
3239 " | </td>\n";
3240 } else {
3241 if ($diff->{'to_id'} eq $from_hash) {
3242 print "<td class=\"link nochange\">";
3243 } else {
3244 print "<td class=\"link\">";
3246 print $cgi->a({-href => href(action=>"blobdiff",
3247 hash=>$diff->{'to_id'},
3248 hash_parent=>$from_hash,
3249 hash_base=>$hash,
3250 hash_parent_base=>$hash_parent,
3251 file_name=>$diff->{'to_file'},
3252 file_parent=>$from_path)},
3253 "diff" . ($i+1)) .
3254 " | </td>\n";
3258 print "<td class=\"link\">";
3259 if ($not_deleted) {
3260 print $cgi->a({-href => href(action=>"blob",
3261 hash=>$diff->{'to_id'},
3262 file_name=>$diff->{'to_file'},
3263 hash_base=>$hash)},
3264 "blob");
3265 print " | " if ($has_history);
3267 if ($has_history) {
3268 print $cgi->a({-href => href(action=>"history",
3269 file_name=>$diff->{'to_file'},
3270 hash_base=>$hash)},
3271 "history");
3273 print "</td>\n";
3275 print "</tr>\n";
3276 next; # instead of 'else' clause, to avoid extra indent
3278 # else ordinary diff
3280 my ($to_mode_oct, $to_mode_str, $to_file_type);
3281 my ($from_mode_oct, $from_mode_str, $from_file_type);
3282 if ($diff->{'to_mode'} ne ('0' x 6)) {
3283 $to_mode_oct = oct $diff->{'to_mode'};
3284 if (S_ISREG($to_mode_oct)) { # only for regular file
3285 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3287 $to_file_type = file_type($diff->{'to_mode'});
3289 if ($diff->{'from_mode'} ne ('0' x 6)) {
3290 $from_mode_oct = oct $diff->{'from_mode'};
3291 if (S_ISREG($to_mode_oct)) { # only for regular file
3292 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3294 $from_file_type = file_type($diff->{'from_mode'});
3297 if ($diff->{'status'} eq "A") { # created
3298 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3299 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3300 $mode_chng .= "]</span>";
3301 print "<td>";
3302 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3303 hash_base=>$hash, file_name=>$diff->{'file'}),
3304 -class => "list"}, esc_path($diff->{'file'}));
3305 print "</td>\n";
3306 print "<td>$mode_chng</td>\n";
3307 print "<td class=\"link\">";
3308 if ($action eq 'commitdiff') {
3309 # link to patch
3310 $patchno++;
3311 print $cgi->a({-href => "#patch$patchno"}, "patch");
3312 print " | ";
3314 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3315 hash_base=>$hash, file_name=>$diff->{'file'})},
3316 "blob");
3317 print "</td>\n";
3319 } elsif ($diff->{'status'} eq "D") { # deleted
3320 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3321 print "<td>";
3322 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3323 hash_base=>$parent, file_name=>$diff->{'file'}),
3324 -class => "list"}, esc_path($diff->{'file'}));
3325 print "</td>\n";
3326 print "<td>$mode_chng</td>\n";
3327 print "<td class=\"link\">";
3328 if ($action eq 'commitdiff') {
3329 # link to patch
3330 $patchno++;
3331 print $cgi->a({-href => "#patch$patchno"}, "patch");
3332 print " | ";
3334 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3335 hash_base=>$parent, file_name=>$diff->{'file'})},
3336 "blob") . " | ";
3337 if ($have_blame) {
3338 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3339 file_name=>$diff->{'file'})},
3340 "blame") . " | ";
3342 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3343 file_name=>$diff->{'file'})},
3344 "history");
3345 print "</td>\n";
3347 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3348 my $mode_chnge = "";
3349 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3350 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3351 if ($from_file_type ne $to_file_type) {
3352 $mode_chnge .= " from $from_file_type to $to_file_type";
3354 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3355 if ($from_mode_str && $to_mode_str) {
3356 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3357 } elsif ($to_mode_str) {
3358 $mode_chnge .= " mode: $to_mode_str";
3361 $mode_chnge .= "]</span>\n";
3363 print "<td>";
3364 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3365 hash_base=>$hash, file_name=>$diff->{'file'}),
3366 -class => "list"}, esc_path($diff->{'file'}));
3367 print "</td>\n";
3368 print "<td>$mode_chnge</td>\n";
3369 print "<td class=\"link\">";
3370 if ($action eq 'commitdiff') {
3371 # link to patch
3372 $patchno++;
3373 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3374 " | ";
3375 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3376 # "commit" view and modified file (not onlu mode changed)
3377 print $cgi->a({-href => href(action=>"blobdiff",
3378 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3379 hash_base=>$hash, hash_parent_base=>$parent,
3380 file_name=>$diff->{'file'})},
3381 "diff") .
3382 " | ";
3384 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3385 hash_base=>$hash, file_name=>$diff->{'file'})},
3386 "blob") . " | ";
3387 if ($have_blame) {
3388 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3389 file_name=>$diff->{'file'})},
3390 "blame") . " | ";
3392 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3393 file_name=>$diff->{'file'})},
3394 "history");
3395 print "</td>\n";
3397 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3398 my %status_name = ('R' => 'moved', 'C' => 'copied');
3399 my $nstatus = $status_name{$diff->{'status'}};
3400 my $mode_chng = "";
3401 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3402 # mode also for directories, so we cannot use $to_mode_str
3403 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3405 print "<td>" .
3406 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3407 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),
3408 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3409 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3410 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3411 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),
3412 -class => "list"}, esc_path($diff->{'from_file'})) .
3413 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3414 "<td class=\"link\">";
3415 if ($action eq 'commitdiff') {
3416 # link to patch
3417 $patchno++;
3418 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3419 " | ";
3420 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3421 # "commit" view and modified file (not only pure rename or copy)
3422 print $cgi->a({-href => href(action=>"blobdiff",
3423 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3424 hash_base=>$hash, hash_parent_base=>$parent,
3425 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},
3426 "diff") .
3427 " | ";
3429 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3430 hash_base=>$parent, file_name=>$diff->{'to_file'})},
3431 "blob") . " | ";
3432 if ($have_blame) {
3433 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3434 file_name=>$diff->{'to_file'})},
3435 "blame") . " | ";
3437 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3438 file_name=>$diff->{'to_file'})},
3439 "history");
3440 print "</td>\n";
3442 } # we should not encounter Unmerged (U) or Unknown (X) status
3443 print "</tr>\n";
3445 print "</tbody>" if $has_header;
3446 print "</table>\n";
3449 sub git_patchset_body {
3450 my ($fd, $difftree, $hash, @hash_parents) = @_;
3451 my ($hash_parent) = $hash_parents[0];
3453 my $is_combined = (@hash_parents > 1);
3454 my $patch_idx = 0;
3455 my $patch_number = 0;
3456 my $patch_line;
3457 my $diffinfo;
3458 my $to_name;
3459 my (%from, %to);
3461 print "<div class=\"patchset\">\n";
3463 # skip to first patch
3464 while ($patch_line = <$fd>) {
3465 chomp $patch_line;
3467 last if ($patch_line =~ m/^diff /);
3470 PATCH:
3471 while ($patch_line) {
3473 # parse "git diff" header line
3474 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3475 # $1 is from_name, which we do not use
3476 $to_name = unquote($2);
3477 $to_name =~ s!^b/!!;
3478 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3479 # $1 is 'cc' or 'combined', which we do not use
3480 $to_name = unquote($2);
3481 } else {
3482 $to_name = undef;
3485 # check if current patch belong to current raw line
3486 # and parse raw git-diff line if needed
3487 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3488 # this is continuation of a split patch
3489 print "<div class=\"patch cont\">\n";
3490 } else {
3491 # advance raw git-diff output if needed
3492 $patch_idx++ if defined $diffinfo;
3494 # read and prepare patch information
3495 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3497 # compact combined diff output can have some patches skipped
3498 # find which patch (using pathname of result) we are at now;
3499 if ($is_combined) {
3500 while ($to_name ne $diffinfo->{'to_file'}) {
3501 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3502 format_diff_cc_simplified($diffinfo, @hash_parents) .
3503 "</div>\n"; # class="patch"
3505 $patch_idx++;
3506 $patch_number++;
3508 last if $patch_idx > $#$difftree;
3509 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3513 # modifies %from, %to hashes
3514 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3516 # this is first patch for raw difftree line with $patch_idx index
3517 # we index @$difftree array from 0, but number patches from 1
3518 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3521 # git diff header
3522 #assert($patch_line =~ m/^diff /) if DEBUG;
3523 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3524 $patch_number++;
3525 # print "git diff" header
3526 print format_git_diff_header_line($patch_line, $diffinfo,
3527 \%from, \%to);
3529 # print extended diff header
3530 print "<div class=\"diff extended_header\">\n";
3531 EXTENDED_HEADER:
3532 while ($patch_line = <$fd>) {
3533 chomp $patch_line;
3535 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3537 print format_extended_diff_header_line($patch_line, $diffinfo,
3538 \%from, \%to);
3540 print "</div>\n"; # class="diff extended_header"
3542 # from-file/to-file diff header
3543 if (! $patch_line) {
3544 print "</div>\n"; # class="patch"
3545 last PATCH;
3547 next PATCH if ($patch_line =~ m/^diff /);
3548 #assert($patch_line =~ m/^---/) if DEBUG;
3550 my $last_patch_line = $patch_line;
3551 $patch_line = <$fd>;
3552 chomp $patch_line;
3553 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3555 print format_diff_from_to_header($last_patch_line, $patch_line,
3556 $diffinfo, \%from, \%to,
3557 @hash_parents);
3559 # the patch itself
3560 LINE:
3561 while ($patch_line = <$fd>) {
3562 chomp $patch_line;
3564 next PATCH if ($patch_line =~ m/^diff /);
3566 print format_diff_line($patch_line, \%from, \%to);
3569 } continue {
3570 print "</div>\n"; # class="patch"
3573 # for compact combined (--cc) format, with chunk and patch simpliciaction
3574 # patchset might be empty, but there might be unprocessed raw lines
3575 for (++$patch_idx if $patch_number > 0;
3576 $patch_idx < @$difftree;
3577 ++$patch_idx) {
3578 # read and prepare patch information
3579 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3581 # generate anchor for "patch" links in difftree / whatchanged part
3582 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3583 format_diff_cc_simplified($diffinfo, @hash_parents) .
3584 "</div>\n"; # class="patch"
3586 $patch_number++;
3589 if ($patch_number == 0) {
3590 if (@hash_parents > 1) {
3591 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3592 } else {
3593 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3597 print "</div>\n"; # class="patchset"
3600 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3602 # fills project list info (age, description, owner, forks) for each
3603 # project in the list, removing invalid projects from returned list
3604 # NOTE: modifies $projlist, but does not remove entries from it
3605 sub fill_project_list_info {
3606 my ($projlist, $check_forks) = @_;
3607 my @projects;
3609 PROJECT:
3610 foreach my $pr (@$projlist) {
3611 my (@activity) = git_get_last_activity($pr->{'path'});
3612 unless (@activity) {
3613 next PROJECT;
3615 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3616 if (!defined $pr->{'descr'}) {
3617 my $descr = git_get_project_description($pr->{'path'}) || "";
3618 $descr = to_utf8($descr);
3619 $pr->{'descr_long'} = $descr;
3620 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3622 if (!defined $pr->{'owner'}) {
3623 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3625 if ($check_forks) {
3626 my $pname = $pr->{'path'};
3627 if (($pname =~ s/\.git$//) &&
3628 ($pname !~ /\/$/) &&
3629 (-d "$projectroot/$pname")) {
3630 $pr->{'forks'} = "-d $projectroot/$pname";
3631 } else {
3632 $pr->{'forks'} = 0;
3635 push @projects, $pr;
3638 return @projects;
3641 # print 'sort by' <th> element, either sorting by $key if $name eq $order
3642 # (changing $list), or generating 'sort by $name' replay link otherwise
3643 sub print_sort_th {
3644 my ($str_sort, $name, $order, $key, $header, $list) = @_;
3645 $key ||= $name;
3646 $header ||= ucfirst($name);
3648 if ($order eq $name) {
3649 if ($str_sort) {
3650 @$list = sort {$a->{$key} cmp $b->{$key}} @$list;
3651 } else {
3652 @$list = sort {$a->{$key} <=> $b->{$key}} @$list;
3654 print "<th>$header</th>\n";
3655 } else {
3656 print "<th>" .
3657 $cgi->a({-href => href(-replay=>1, order=>$name),
3658 -class => "header"}, $header) .
3659 "</th>\n";
3663 sub print_sort_th_str {
3664 print_sort_th(1, @_);
3667 sub print_sort_th_num {
3668 print_sort_th(0, @_);
3671 sub git_project_list_body {
3672 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
3674 my ($check_forks) = gitweb_check_feature('forks');
3675 my @projects = fill_project_list_info($projlist, $check_forks);
3677 $order ||= $default_projects_order;
3678 $from = 0 unless defined $from;
3679 $to = $#projects if (!defined $to || $#projects < $to);
3681 print "<table class=\"project_list\">\n";
3682 unless ($no_header) {
3683 print "<tr>\n";
3684 if ($check_forks) {
3685 print "<th></th>\n";
3687 print_sort_th_str('project', $order, 'path',
3688 'Project', \@projects);
3689 print_sort_th_str('descr', $order, 'descr_long',
3690 'Description', \@projects);
3691 print_sort_th_str('owner', $order, 'owner',
3692 'Owner', \@projects);
3693 print_sort_th_num('age', $order, 'age',
3694 'Last Change', \@projects);
3695 print "<th></th>\n" . # for links
3696 "</tr>\n";
3698 my $alternate = 1;
3699 for (my $i = $from; $i <= $to; $i++) {
3700 my $pr = $projects[$i];
3701 if ($alternate) {
3702 print "<tr class=\"dark\">\n";
3703 } else {
3704 print "<tr class=\"light\">\n";
3706 $alternate ^= 1;
3707 if ($check_forks) {
3708 print "<td>";
3709 if ($pr->{'forks'}) {
3710 print "<!-- $pr->{'forks'} -->\n";
3711 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3713 print "</td>\n";
3715 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3716 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3717 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3718 -class => "list", -title => $pr->{'descr_long'}},
3719 esc_html($pr->{'descr'})) . "</td>\n" .
3720 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3721 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3722 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3723 "<td class=\"link\">" .
3724 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3725 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
3726 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
3727 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3728 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3729 "</td>\n" .
3730 "</tr>\n";
3732 if (defined $extra) {
3733 print "<tr>\n";
3734 if ($check_forks) {
3735 print "<td></td>\n";
3737 print "<td colspan=\"5\">$extra</td>\n" .
3738 "</tr>\n";
3740 print "</table>\n";
3743 sub git_shortlog_body {
3744 # uses global variable $project
3745 my ($commitlist, $from, $to, $refs, $extra) = @_;
3747 $from = 0 unless defined $from;
3748 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3750 print "<table class=\"shortlog\">\n";
3751 my $alternate = 1;
3752 for (my $i = $from; $i <= $to; $i++) {
3753 my %co = %{$commitlist->[$i]};
3754 my $commit = $co{'id'};
3755 my $ref = format_ref_marker($refs, $commit);
3756 if ($alternate) {
3757 print "<tr class=\"dark\">\n";
3758 } else {
3759 print "<tr class=\"light\">\n";
3761 $alternate ^= 1;
3762 my $author = chop_and_escape_str($co{'author_name'}, 10);
3763 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3764 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3765 "<td><i>" . $author . "</i></td>\n" .
3766 "<td>";
3767 print format_subject_html($co{'title'}, $co{'title_short'},
3768 href(action=>"commit", hash=>$commit), $ref);
3769 print "</td>\n" .
3770 "<td class=\"link\">" .
3771 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3772 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3773 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3774 my $snapshot_links = format_snapshot_links($commit);
3775 if (defined $snapshot_links) {
3776 print " | " . $snapshot_links;
3778 print "</td>\n" .
3779 "</tr>\n";
3781 if (defined $extra) {
3782 print "<tr>\n" .
3783 "<td colspan=\"4\">$extra</td>\n" .
3784 "</tr>\n";
3786 print "</table>\n";
3789 sub git_history_body {
3790 # Warning: assumes constant type (blob or tree) during history
3791 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3793 $from = 0 unless defined $from;
3794 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
3796 print "<table class=\"history\">\n";
3797 my $alternate = 1;
3798 for (my $i = $from; $i <= $to; $i++) {
3799 my %co = %{$commitlist->[$i]};
3800 if (!%co) {
3801 next;
3803 my $commit = $co{'id'};
3805 my $ref = format_ref_marker($refs, $commit);
3807 if ($alternate) {
3808 print "<tr class=\"dark\">\n";
3809 } else {
3810 print "<tr class=\"light\">\n";
3812 $alternate ^= 1;
3813 # shortlog uses chop_str($co{'author_name'}, 10)
3814 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
3815 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3816 "<td><i>" . $author . "</i></td>\n" .
3817 "<td>";
3818 # originally git_history used chop_str($co{'title'}, 50)
3819 print format_subject_html($co{'title'}, $co{'title_short'},
3820 href(action=>"commit", hash=>$commit), $ref);
3821 print "</td>\n" .
3822 "<td class=\"link\">" .
3823 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
3824 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
3826 if ($ftype eq 'blob') {
3827 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
3828 my $blob_parent = git_get_hash_by_path($commit, $file_name);
3829 if (defined $blob_current && defined $blob_parent &&
3830 $blob_current ne $blob_parent) {
3831 print " | " .
3832 $cgi->a({-href => href(action=>"blobdiff",
3833 hash=>$blob_current, hash_parent=>$blob_parent,
3834 hash_base=>$hash_base, hash_parent_base=>$commit,
3835 file_name=>$file_name)},
3836 "diff to current");
3839 print "</td>\n" .
3840 "</tr>\n";
3842 if (defined $extra) {
3843 print "<tr>\n" .
3844 "<td colspan=\"4\">$extra</td>\n" .
3845 "</tr>\n";
3847 print "</table>\n";
3850 sub git_tags_body {
3851 # uses global variable $project
3852 my ($taglist, $from, $to, $extra) = @_;
3853 $from = 0 unless defined $from;
3854 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
3856 print "<table class=\"tags\">\n";
3857 my $alternate = 1;
3858 for (my $i = $from; $i <= $to; $i++) {
3859 my $entry = $taglist->[$i];
3860 my %tag = %$entry;
3861 my $comment = $tag{'subject'};
3862 my $comment_short;
3863 if (defined $comment) {
3864 $comment_short = chop_str($comment, 30, 5);
3866 if ($alternate) {
3867 print "<tr class=\"dark\">\n";
3868 } else {
3869 print "<tr class=\"light\">\n";
3871 $alternate ^= 1;
3872 if (defined $tag{'age'}) {
3873 print "<td><i>$tag{'age'}</i></td>\n";
3874 } else {
3875 print "<td></td>\n";
3877 print "<td>" .
3878 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
3879 -class => "list name"}, esc_html($tag{'name'})) .
3880 "</td>\n" .
3881 "<td>";
3882 if (defined $comment) {
3883 print format_subject_html($comment, $comment_short,
3884 href(action=>"tag", hash=>$tag{'id'}));
3886 print "</td>\n" .
3887 "<td class=\"selflink\">";
3888 if ($tag{'type'} eq "tag") {
3889 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
3890 } else {
3891 print "&nbsp;";
3893 print "</td>\n" .
3894 "<td class=\"link\">" . " | " .
3895 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
3896 if ($tag{'reftype'} eq "commit") {
3897 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "shortlog") .
3898 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})}, "log");
3899 } elsif ($tag{'reftype'} eq "blob") {
3900 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
3902 print "</td>\n" .
3903 "</tr>";
3905 if (defined $extra) {
3906 print "<tr>\n" .
3907 "<td colspan=\"5\">$extra</td>\n" .
3908 "</tr>\n";
3910 print "</table>\n";
3913 sub git_heads_body {
3914 # uses global variable $project
3915 my ($headlist, $head, $from, $to, $extra) = @_;
3916 $from = 0 unless defined $from;
3917 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
3919 print "<table class=\"heads\">\n";
3920 my $alternate = 1;
3921 for (my $i = $from; $i <= $to; $i++) {
3922 my $entry = $headlist->[$i];
3923 my %ref = %$entry;
3924 my $curr = $ref{'id'} eq $head;
3925 if ($alternate) {
3926 print "<tr class=\"dark\">\n";
3927 } else {
3928 print "<tr class=\"light\">\n";
3930 $alternate ^= 1;
3931 print "<td><i>$ref{'age'}</i></td>\n" .
3932 ($curr ? "<td class=\"current_head\">" : "<td>") .
3933 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
3934 -class => "list name"},esc_html($ref{'name'})) .
3935 "</td>\n" .
3936 "<td class=\"link\">" .
3937 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "shortlog") . " | " .
3938 $cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})}, "log") . " | " .
3939 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
3940 "</td>\n" .
3941 "</tr>";
3943 if (defined $extra) {
3944 print "<tr>\n" .
3945 "<td colspan=\"3\">$extra</td>\n" .
3946 "</tr>\n";
3948 print "</table>\n";
3951 sub git_search_grep_body {
3952 my ($commitlist, $from, $to, $extra) = @_;
3953 $from = 0 unless defined $from;
3954 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3956 print "<table class=\"commit_search\">\n";
3957 my $alternate = 1;
3958 for (my $i = $from; $i <= $to; $i++) {
3959 my %co = %{$commitlist->[$i]};
3960 if (!%co) {
3961 next;
3963 my $commit = $co{'id'};
3964 if ($alternate) {
3965 print "<tr class=\"dark\">\n";
3966 } else {
3967 print "<tr class=\"light\">\n";
3969 $alternate ^= 1;
3970 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
3971 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3972 "<td><i>" . $author . "</i></td>\n" .
3973 "<td>" .
3974 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3975 -class => "list subject"},
3976 chop_and_escape_str($co{'title'}, 50) . "<br/>");
3977 my $comment = $co{'comment'};
3978 foreach my $line (@$comment) {
3979 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
3980 my ($lead, $match, $trail) = ($1, $2, $3);
3981 $match = chop_str($match, 70, 5, 'center');
3982 my $contextlen = int((80 - length($match))/2);
3983 $contextlen = 30 if ($contextlen > 30);
3984 $lead = chop_str($lead, $contextlen, 10, 'left');
3985 $trail = chop_str($trail, $contextlen, 10, 'right');
3987 $lead = esc_html($lead);
3988 $match = esc_html($match);
3989 $trail = esc_html($trail);
3991 print "$lead<span class=\"match\">$match</span>$trail<br />";
3994 print "</td>\n" .
3995 "<td class=\"link\">" .
3996 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3997 " | " .
3998 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
3999 " | " .
4000 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4001 print "</td>\n" .
4002 "</tr>\n";
4004 if (defined $extra) {
4005 print "<tr>\n" .
4006 "<td colspan=\"3\">$extra</td>\n" .
4007 "</tr>\n";
4009 print "</table>\n";
4012 ## ======================================================================
4013 ## ======================================================================
4014 ## actions
4016 sub git_project_list {
4017 my $order = $cgi->param('o');
4018 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4019 die_error(400, "Unknown order parameter");
4022 my @list = git_get_projects_list();
4023 if (!@list) {
4024 die_error(404, "No projects found");
4027 git_header_html();
4028 if (-f $home_text) {
4029 print "<div class=\"index_include\">\n";
4030 open (my $fd, $home_text);
4031 print <$fd>;
4032 close $fd;
4033 print "</div>\n";
4035 git_project_list_body(\@list, $order);
4036 git_footer_html();
4039 sub git_forks {
4040 my $order = $cgi->param('o');
4041 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4042 die_error(400, "Unknown order parameter");
4045 my @list = git_get_projects_list($project);
4046 if (!@list) {
4047 die_error(404, "No forks found");
4050 git_header_html();
4051 git_print_page_nav('','');
4052 git_print_header_div('summary', "$project forks");
4053 git_project_list_body(\@list, $order);
4054 git_footer_html();
4057 sub git_project_index {
4058 my @projects = git_get_projects_list($project);
4060 print $cgi->header(
4061 -type => 'text/plain',
4062 -charset => 'utf-8',
4063 -content_disposition => 'inline; filename="index.aux"');
4065 foreach my $pr (@projects) {
4066 if (!exists $pr->{'owner'}) {
4067 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4070 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4071 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4072 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4073 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4074 $path =~ s/ /\+/g;
4075 $owner =~ s/ /\+/g;
4077 print "$path $owner\n";
4081 sub git_summary {
4082 my $descr = git_get_project_description($project) || "none";
4083 my %co = parse_commit("HEAD");
4084 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4085 my $head = $co{'id'};
4087 my $owner = git_get_project_owner($project);
4089 my $refs = git_get_references();
4090 # These get_*_list functions return one more to allow us to see if
4091 # there are more ...
4092 my @taglist = git_get_tags_list(16);
4093 my @headlist = git_get_heads_list(16);
4094 my @forklist;
4095 my ($check_forks) = gitweb_check_feature('forks');
4097 if ($check_forks) {
4098 @forklist = git_get_projects_list($project);
4101 git_header_html();
4102 git_print_page_nav('summary','', $head);
4104 print "<div class=\"title\">&nbsp;</div>\n";
4105 print "<table class=\"projects_list\">\n" .
4106 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4107 "<tr><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4108 if (defined $cd{'rfc2822'}) {
4109 print "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4112 # use per project git URL list in $projectroot/$project/cloneurl
4113 # or make project git URL from git base URL and project name
4114 my $url_tag = "URL";
4115 my @url_list = git_get_project_url_list($project);
4116 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4117 foreach my $git_url (@url_list) {
4118 next unless $git_url;
4119 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
4120 $url_tag = "";
4122 print "</table>\n";
4124 if (-s "$projectroot/$project/README.html") {
4125 if (open my $fd, "$projectroot/$project/README.html") {
4126 print "<div class=\"title\">readme</div>\n" .
4127 "<div class=\"readme\">\n";
4128 print $_ while (<$fd>);
4129 print "\n</div>\n"; # class="readme"
4130 close $fd;
4134 # we need to request one more than 16 (0..15) to check if
4135 # those 16 are all
4136 my @commitlist = $head ? parse_commits($head, 17) : ();
4137 if (@commitlist) {
4138 git_print_header_div('shortlog');
4139 git_shortlog_body(\@commitlist, 0, 15, $refs,
4140 $#commitlist <= 15 ? undef :
4141 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4144 if (@taglist) {
4145 git_print_header_div('tags');
4146 git_tags_body(\@taglist, 0, 15,
4147 $#taglist <= 15 ? undef :
4148 $cgi->a({-href => href(action=>"tags")}, "..."));
4151 if (@headlist) {
4152 git_print_header_div('heads');
4153 git_heads_body(\@headlist, $head, 0, 15,
4154 $#headlist <= 15 ? undef :
4155 $cgi->a({-href => href(action=>"heads")}, "..."));
4158 if (@forklist) {
4159 git_print_header_div('forks');
4160 git_project_list_body(\@forklist, undef, 0, 15,
4161 $#forklist <= 15 ? undef :
4162 $cgi->a({-href => href(action=>"forks")}, "..."),
4163 'noheader');
4166 git_footer_html();
4169 sub git_tag {
4170 my $head = git_get_head_hash($project);
4171 git_header_html();
4172 git_print_page_nav('','', $head,undef,$head);
4173 my %tag = parse_tag($hash);
4175 if (! %tag) {
4176 die_error(404, "Unknown tag object");
4179 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4180 print "<div class=\"title_text\">\n" .
4181 "<table class=\"object_header\">\n" .
4182 "<tr>\n" .
4183 "<td>object</td>\n" .
4184 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4185 $tag{'object'}) . "</td>\n" .
4186 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4187 $tag{'type'}) . "</td>\n" .
4188 "</tr>\n";
4189 if (defined($tag{'author'})) {
4190 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4191 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4192 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4193 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4194 "</td></tr>\n";
4196 print "</table>\n\n" .
4197 "</div>\n";
4198 print "<div class=\"page_body\">";
4199 my $comment = $tag{'comment'};
4200 foreach my $line (@$comment) {
4201 chomp $line;
4202 print esc_html($line, -nbsp=>1) . "<br/>\n";
4204 print "</div>\n";
4205 git_footer_html();
4208 sub git_blame {
4209 my $fd;
4210 my $ftype;
4212 gitweb_check_feature('blame')
4213 or die_error(403, "Blame view not allowed");
4215 die_error(400, "No file name given") unless $file_name;
4216 $hash_base ||= git_get_head_hash($project);
4217 die_error(404, "Couldn't find base commit") unless ($hash_base);
4218 my %co = parse_commit($hash_base)
4219 or die_error(404, "Commit not found");
4220 if (!defined $hash) {
4221 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4222 or die_error(404, "Error looking up file");
4224 $ftype = git_get_type($hash);
4225 if ($ftype !~ "blob") {
4226 die_error(400, "Object is not a blob");
4228 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4229 $file_name, $hash_base)
4230 or die_error(500, "Open git-blame failed");
4231 git_header_html();
4232 my $formats_nav =
4233 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4234 "blob") .
4235 " | " .
4236 $cgi->a({-href => href(action=>"history", -replay=>1)},
4237 "history") .
4238 " | " .
4239 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
4240 "HEAD");
4241 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4242 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4243 git_print_page_path($file_name, $ftype, $hash_base);
4244 my @rev_color = (qw(light2 dark2));
4245 my $num_colors = scalar(@rev_color);
4246 my $current_color = 0;
4247 my $last_rev;
4248 print <<HTML;
4249 <div class="page_body">
4250 <table class="blame">
4251 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
4252 HTML
4253 my %metainfo = ();
4254 while (1) {
4255 $_ = <$fd>;
4256 last unless defined $_;
4257 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4258 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4259 if (!exists $metainfo{$full_rev}) {
4260 $metainfo{$full_rev} = {};
4262 my $meta = $metainfo{$full_rev};
4263 while (<$fd>) {
4264 last if (s/^\t//);
4265 if (/^(\S+) (.*)$/) {
4266 $meta->{$1} = $2;
4269 my $data = $_;
4270 chomp $data;
4271 my $rev = substr($full_rev, 0, 8);
4272 my $author = $meta->{'author'};
4273 my %date = parse_date($meta->{'author-time'},
4274 $meta->{'author-tz'});
4275 my $date = $date{'iso-tz'};
4276 if ($group_size) {
4277 $current_color = ++$current_color % $num_colors;
4279 print "<tr class=\"$rev_color[$current_color]\">\n";
4280 if ($group_size) {
4281 print "<td class=\"sha1\"";
4282 print " title=\"". esc_html($author) . ", $date\"";
4283 print " rowspan=\"$group_size\"" if ($group_size > 1);
4284 print ">";
4285 print $cgi->a({-href => href(action=>"commit",
4286 hash=>$full_rev,
4287 file_name=>$file_name)},
4288 esc_html($rev));
4289 print "</td>\n";
4291 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4292 or die_error(500, "Open git-rev-parse failed");
4293 my $parent_commit = <$dd>;
4294 close $dd;
4295 chomp($parent_commit);
4296 my $blamed = href(action => 'blame',
4297 file_name => $meta->{'filename'},
4298 hash_base => $parent_commit);
4299 print "<td class=\"linenr\">";
4300 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4301 -id => "l$lineno",
4302 -class => "linenr" },
4303 esc_html($lineno));
4304 print "</td>";
4305 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4306 print "</tr>\n";
4308 print "</table>\n";
4309 print "</div>";
4310 close $fd
4311 or print "Reading blob failed\n";
4312 git_footer_html();
4315 sub git_tags {
4316 my $head = git_get_head_hash($project);
4317 git_header_html();
4318 git_print_page_nav('','', $head,undef,$head);
4319 git_print_header_div('summary', $project);
4321 my @tagslist = git_get_tags_list();
4322 if (@tagslist) {
4323 git_tags_body(\@tagslist);
4325 git_footer_html();
4328 sub git_heads {
4329 my $head = git_get_head_hash($project);
4330 git_header_html();
4331 git_print_page_nav('','', $head,undef,$head);
4332 git_print_header_div('summary', $project);
4334 my @headslist = git_get_heads_list();
4335 if (@headslist) {
4336 git_heads_body(\@headslist, $head);
4338 git_footer_html();
4341 sub git_blob_plain {
4342 my $type = shift;
4343 my $expires;
4345 if (!defined $hash) {
4346 if (defined $file_name) {
4347 my $base = $hash_base || git_get_head_hash($project);
4348 $hash = git_get_hash_by_path($base, $file_name, "blob")
4349 or die_error(404, "Cannot find file");
4350 } else {
4351 die_error(400, "No file name defined");
4353 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4354 # blobs defined by non-textual hash id's can be cached
4355 $expires = "+1d";
4358 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4359 or die_error(500, "Open git-cat-file blob '$hash' failed");
4361 # content-type (can include charset)
4362 $type = blob_contenttype($fd, $file_name, $type);
4364 # "save as" filename, even when no $file_name is given
4365 my $save_as = "$hash";
4366 if (defined $file_name) {
4367 $save_as = $file_name;
4368 } elsif ($type =~ m/^text\//) {
4369 $save_as .= '.txt';
4372 print $cgi->header(
4373 -type => $type,
4374 -expires => $expires,
4375 -content_disposition => 'inline; filename="' . $save_as . '"');
4376 undef $/;
4377 binmode STDOUT, ':raw';
4378 print <$fd>;
4379 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4380 $/ = "\n";
4381 close $fd;
4384 sub git_blob {
4385 my $expires;
4387 if (!defined $hash) {
4388 if (defined $file_name) {
4389 my $base = $hash_base || git_get_head_hash($project);
4390 $hash = git_get_hash_by_path($base, $file_name, "blob")
4391 or die_error(404, "Cannot find file");
4392 } else {
4393 die_error(400, "No file name defined");
4395 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4396 # blobs defined by non-textual hash id's can be cached
4397 $expires = "+1d";
4400 my ($have_blame) = gitweb_check_feature('blame');
4401 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4402 or die_error(500, "Couldn't cat $file_name, $hash");
4403 my $mimetype = blob_mimetype($fd, $file_name);
4404 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4405 close $fd;
4406 return git_blob_plain($mimetype);
4408 # we can have blame only for text/* mimetype
4409 $have_blame &&= ($mimetype =~ m!^text/!);
4411 git_header_html(undef, $expires);
4412 my $formats_nav = '';
4413 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4414 if (defined $file_name) {
4415 if ($have_blame) {
4416 $formats_nav .=
4417 $cgi->a({-href => href(action=>"blame", -replay=>1)},
4418 "blame") .
4419 " | ";
4421 $formats_nav .=
4422 $cgi->a({-href => href(action=>"history", -replay=>1)},
4423 "history") .
4424 " | " .
4425 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4426 "raw") .
4427 " | " .
4428 $cgi->a({-href => href(action=>"blob",
4429 hash_base=>"HEAD", file_name=>$file_name)},
4430 "HEAD");
4431 } else {
4432 $formats_nav .=
4433 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4434 "raw");
4436 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4437 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4438 } else {
4439 print "<div class=\"page_nav\">\n" .
4440 "<br/><br/></div>\n" .
4441 "<div class=\"title\">$hash</div>\n";
4443 git_print_page_path($file_name, "blob", $hash_base);
4444 print "<div class=\"page_body\">\n";
4445 if ($mimetype =~ m!^image/!) {
4446 print qq!<img type="$mimetype"!;
4447 if ($file_name) {
4448 print qq! alt="$file_name" title="$file_name"!;
4450 print qq! src="! .
4451 href(action=>"blob_plain", hash=>$hash,
4452 hash_base=>$hash_base, file_name=>$file_name) .
4453 qq!" />\n!;
4454 } else {
4455 my $nr;
4456 while (my $line = <$fd>) {
4457 chomp $line;
4458 $nr++;
4459 $line = untabify($line);
4460 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4461 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4464 close $fd
4465 or print "Reading blob failed.\n";
4466 print "</div>";
4467 git_footer_html();
4470 sub git_tree {
4471 if (!defined $hash_base) {
4472 $hash_base = "HEAD";
4474 if (!defined $hash) {
4475 if (defined $file_name) {
4476 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4477 } else {
4478 $hash = $hash_base;
4481 $/ = "\0";
4482 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4483 or die_error(500, "Open git-ls-tree failed");
4484 my @entries = map { chomp; $_ } <$fd>;
4485 close $fd or die_error(404, "Reading tree failed");
4486 $/ = "\n";
4488 my $refs = git_get_references();
4489 my $ref = format_ref_marker($refs, $hash_base);
4490 git_header_html();
4491 my $basedir = '';
4492 my ($have_blame) = gitweb_check_feature('blame');
4493 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4494 my @views_nav = ();
4495 if (defined $file_name) {
4496 push @views_nav,
4497 $cgi->a({-href => href(action=>"history", -replay=>1)},
4498 "history"),
4499 $cgi->a({-href => href(action=>"tree",
4500 hash_base=>"HEAD", file_name=>$file_name)},
4501 "HEAD"),
4503 my $snapshot_links = format_snapshot_links($hash);
4504 if (defined $snapshot_links) {
4505 # FIXME: Should be available when we have no hash base as well.
4506 push @views_nav, $snapshot_links;
4508 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4509 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4510 } else {
4511 undef $hash_base;
4512 print "<div class=\"page_nav\">\n";
4513 print "<br/><br/></div>\n";
4514 print "<div class=\"title\">$hash</div>\n";
4516 if (defined $file_name) {
4517 $basedir = $file_name;
4518 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4519 $basedir .= '/';
4522 git_print_page_path($file_name, 'tree', $hash_base);
4523 print "<div class=\"page_body\">\n";
4524 print "<table class=\"tree\">\n";
4525 my $alternate = 1;
4526 # '..' (top directory) link if possible
4527 if (defined $hash_base &&
4528 defined $file_name && $file_name =~ m![^/]+$!) {
4529 if ($alternate) {
4530 print "<tr class=\"dark\">\n";
4531 } else {
4532 print "<tr class=\"light\">\n";
4534 $alternate ^= 1;
4536 my $up = $file_name;
4537 $up =~ s!/?[^/]+$!!;
4538 undef $up unless $up;
4539 # based on git_print_tree_entry
4540 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4541 print '<td class="list">';
4542 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4543 file_name=>$up)},
4544 "..");
4545 print "</td>\n";
4546 print "<td class=\"link\"></td>\n";
4548 print "</tr>\n";
4550 foreach my $line (@entries) {
4551 my %t = parse_ls_tree_line($line, -z => 1);
4553 if ($alternate) {
4554 print "<tr class=\"dark\">\n";
4555 } else {
4556 print "<tr class=\"light\">\n";
4558 $alternate ^= 1;
4560 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4562 print "</tr>\n";
4564 print "</table>\n" .
4565 "</div>";
4566 git_footer_html();
4569 sub git_snapshot {
4570 my @supported_fmts = gitweb_check_feature('snapshot');
4571 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4573 my $format = $cgi->param('sf');
4574 if (!@supported_fmts) {
4575 die_error(403, "Snapshots not allowed");
4577 # default to first supported snapshot format
4578 $format ||= $supported_fmts[0];
4579 if ($format !~ m/^[a-z0-9]+$/) {
4580 die_error(400, "Invalid snapshot format parameter");
4581 } elsif (!exists($known_snapshot_formats{$format})) {
4582 die_error(400, "Unknown snapshot format");
4583 } elsif (!grep($_ eq $format, @supported_fmts)) {
4584 die_error(403, "Unsupported snapshot format");
4587 if (!defined $hash) {
4588 $hash = git_get_head_hash($project);
4591 my $name = $project;
4592 $name =~ s,([^/])/*\.git$,$1,;
4593 $name = basename($name);
4594 my $filename = to_utf8($name);
4595 $name =~ s/\047/\047\\\047\047/g;
4596 my $cmd;
4597 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4598 $cmd = quote_command(
4599 git_cmd(), 'archive',
4600 "--format=$known_snapshot_formats{$format}{'format'}",
4601 "--prefix=$name/", $hash);
4602 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4603 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
4606 print $cgi->header(
4607 -type => $known_snapshot_formats{$format}{'type'},
4608 -content_disposition => 'inline; filename="' . "$filename" . '"',
4609 -status => '200 OK');
4611 open my $fd, "-|", $cmd
4612 or die_error(500, "Execute git-archive failed");
4613 binmode STDOUT, ':raw';
4614 print <$fd>;
4615 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4616 close $fd;
4619 sub git_log {
4620 my $head = git_get_head_hash($project);
4621 if (!defined $hash) {
4622 $hash = $head;
4624 if (!defined $page) {
4625 $page = 0;
4627 my $refs = git_get_references();
4629 my @commitlist = parse_commits($hash, 101, (100 * $page));
4631 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#commitlist >= 100);
4633 git_header_html();
4634 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4636 if (!@commitlist) {
4637 my %co = parse_commit($hash);
4639 git_print_header_div('summary', $project);
4640 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4642 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4643 for (my $i = 0; $i <= $to; $i++) {
4644 my %co = %{$commitlist[$i]};
4645 next if !%co;
4646 my $commit = $co{'id'};
4647 my $ref = format_ref_marker($refs, $commit);
4648 my %ad = parse_date($co{'author_epoch'});
4649 git_print_header_div('commit',
4650 "<span class=\"age\">$co{'age_string'}</span>" .
4651 esc_html($co{'title'}) . $ref,
4652 $commit);
4653 print "<div class=\"title_text\">\n" .
4654 "<div class=\"log_link\">\n" .
4655 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4656 " | " .
4657 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4658 " | " .
4659 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4660 "<br/>\n" .
4661 "</div>\n" .
4662 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4663 "</div>\n";
4665 print "<div class=\"log_body\">\n";
4666 git_print_log($co{'comment'}, -final_empty_line=> 1);
4667 print "</div>\n";
4669 if ($#commitlist >= 100) {
4670 print "<div class=\"page_nav\">\n";
4671 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4672 -accesskey => "n", -title => "Alt-n"}, "next");
4673 print "</div>\n";
4675 git_footer_html();
4678 sub git_commit {
4679 $hash ||= $hash_base || "HEAD";
4680 my %co = parse_commit($hash)
4681 or die_error(404, "Unknown commit object");
4682 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4683 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
4685 my $parent = $co{'parent'};
4686 my $parents = $co{'parents'}; # listref
4688 # we need to prepare $formats_nav before any parameter munging
4689 my $formats_nav;
4690 if (!defined $parent) {
4691 # --root commitdiff
4692 $formats_nav .= '(initial)';
4693 } elsif (@$parents == 1) {
4694 # single parent commit
4695 $formats_nav .=
4696 '(parent: ' .
4697 $cgi->a({-href => href(action=>"commit",
4698 hash=>$parent)},
4699 esc_html(substr($parent, 0, 7))) .
4700 ')';
4701 } else {
4702 # merge commit
4703 $formats_nav .=
4704 '(merge: ' .
4705 join(' ', map {
4706 $cgi->a({-href => href(action=>"commit",
4707 hash=>$_)},
4708 esc_html(substr($_, 0, 7)));
4709 } @$parents ) .
4710 ')';
4713 if (!defined $parent) {
4714 $parent = "--root";
4716 my @difftree;
4717 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
4718 @diff_opts,
4719 (@$parents <= 1 ? $parent : '-c'),
4720 $hash, "--"
4721 or die_error(500, "Open git-diff-tree failed");
4722 @difftree = map { chomp; $_ } <$fd>;
4723 close $fd or die_error(404, "Reading git-diff-tree failed");
4725 # non-textual hash id's can be cached
4726 my $expires;
4727 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4728 $expires = "+1d";
4730 my $refs = git_get_references();
4731 my $ref = format_ref_marker($refs, $co{'id'});
4733 git_header_html(undef, $expires);
4734 git_print_page_nav('commit', '',
4735 $hash, $co{'tree'}, $hash,
4736 $formats_nav);
4738 if (defined $co{'parent'}) {
4739 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
4740 } else {
4741 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
4743 print "<div class=\"title_text\">\n" .
4744 "<table class=\"object_header\">\n";
4745 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
4746 "<tr>" .
4747 "<td></td><td> $ad{'rfc2822'}";
4748 if ($ad{'hour_local'} < 6) {
4749 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
4750 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4751 } else {
4752 printf(" (%02d:%02d %s)",
4753 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
4755 print "</td>" .
4756 "</tr>\n";
4757 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
4758 print "<tr><td></td><td> $cd{'rfc2822'}" .
4759 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
4760 "</td></tr>\n";
4761 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
4762 print "<tr>" .
4763 "<td>tree</td>" .
4764 "<td class=\"sha1\">" .
4765 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
4766 class => "list"}, $co{'tree'}) .
4767 "</td>" .
4768 "<td class=\"link\">" .
4769 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4770 "tree");
4771 my $snapshot_links = format_snapshot_links($hash);
4772 if (defined $snapshot_links) {
4773 print " | " . $snapshot_links;
4775 print "</td>" .
4776 "</tr>\n";
4778 foreach my $par (@$parents) {
4779 print "<tr>" .
4780 "<td>parent</td>" .
4781 "<td class=\"sha1\">" .
4782 $cgi->a({-href => href(action=>"commit", hash=>$par),
4783 class => "list"}, $par) .
4784 "</td>" .
4785 "<td class=\"link\">" .
4786 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
4787 " | " .
4788 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
4789 "</td>" .
4790 "</tr>\n";
4792 print "</table>".
4793 "</div>\n";
4795 print "<div class=\"page_body\">\n";
4796 git_print_log($co{'comment'});
4797 print "</div>\n";
4799 git_difftree_body(\@difftree, $hash, @$parents);
4801 git_footer_html();
4804 sub git_object {
4805 # object is defined by:
4806 # - hash or hash_base alone
4807 # - hash_base and file_name
4808 my $type;
4810 # - hash or hash_base alone
4811 if ($hash || ($hash_base && !defined $file_name)) {
4812 my $object_id = $hash || $hash_base;
4814 open my $fd, "-|", quote_command(
4815 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
4816 or die_error(404, "Object does not exist");
4817 $type = <$fd>;
4818 chomp $type;
4819 close $fd
4820 or die_error(404, "Object does not exist");
4822 # - hash_base and file_name
4823 } elsif ($hash_base && defined $file_name) {
4824 $file_name =~ s,/+$,,;
4826 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
4827 or die_error(404, "Base object does not exist");
4829 # here errors should not hapen
4830 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
4831 or die_error(500, "Open git-ls-tree failed");
4832 my $line = <$fd>;
4833 close $fd;
4835 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
4836 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
4837 die_error(404, "File or directory for given base does not exist");
4839 $type = $2;
4840 $hash = $3;
4841 } else {
4842 die_error(400, "Not enough information to find object");
4845 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
4846 hash=>$hash, hash_base=>$hash_base,
4847 file_name=>$file_name),
4848 -status => '302 Found');
4851 sub git_blobdiff {
4852 my $format = shift || 'html';
4854 my $fd;
4855 my @difftree;
4856 my %diffinfo;
4857 my $expires;
4859 # preparing $fd and %diffinfo for git_patchset_body
4860 # new style URI
4861 if (defined $hash_base && defined $hash_parent_base) {
4862 if (defined $file_name) {
4863 # read raw output
4864 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4865 $hash_parent_base, $hash_base,
4866 "--", (defined $file_parent ? $file_parent : ()), $file_name
4867 or die_error(500, "Open git-diff-tree failed");
4868 @difftree = map { chomp; $_ } <$fd>;
4869 close $fd
4870 or die_error(404, "Reading git-diff-tree failed");
4871 @difftree
4872 or die_error(404, "Blob diff not found");
4874 } elsif (defined $hash &&
4875 $hash =~ /[0-9a-fA-F]{40}/) {
4876 # try to find filename from $hash
4878 # read filtered raw output
4879 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4880 $hash_parent_base, $hash_base, "--"
4881 or die_error(500, "Open git-diff-tree failed");
4882 @difftree =
4883 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
4884 # $hash == to_id
4885 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
4886 map { chomp; $_ } <$fd>;
4887 close $fd
4888 or die_error(404, "Reading git-diff-tree failed");
4889 @difftree
4890 or die_error(404, "Blob diff not found");
4892 } else {
4893 die_error(400, "Missing one of the blob diff parameters");
4896 if (@difftree > 1) {
4897 die_error(400, "Ambiguous blob diff specification");
4900 %diffinfo = parse_difftree_raw_line($difftree[0]);
4901 $file_parent ||= $diffinfo{'from_file'} || $file_name;
4902 $file_name ||= $diffinfo{'to_file'};
4904 $hash_parent ||= $diffinfo{'from_id'};
4905 $hash ||= $diffinfo{'to_id'};
4907 # non-textual hash id's can be cached
4908 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
4909 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
4910 $expires = '+1d';
4913 # open patch output
4914 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
4915 '-p', ($format eq 'html' ? "--full-index" : ()),
4916 $hash_parent_base, $hash_base,
4917 "--", (defined $file_parent ? $file_parent : ()), $file_name
4918 or die_error(500, "Open git-diff-tree failed");
4921 # old/legacy style URI
4922 if (!%diffinfo && # if new style URI failed
4923 defined $hash && defined $hash_parent) {
4924 # fake git-diff-tree raw output
4925 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
4926 $diffinfo{'from_id'} = $hash_parent;
4927 $diffinfo{'to_id'} = $hash;
4928 if (defined $file_name) {
4929 if (defined $file_parent) {
4930 $diffinfo{'status'} = '2';
4931 $diffinfo{'from_file'} = $file_parent;
4932 $diffinfo{'to_file'} = $file_name;
4933 } else { # assume not renamed
4934 $diffinfo{'status'} = '1';
4935 $diffinfo{'from_file'} = $file_name;
4936 $diffinfo{'to_file'} = $file_name;
4938 } else { # no filename given
4939 $diffinfo{'status'} = '2';
4940 $diffinfo{'from_file'} = $hash_parent;
4941 $diffinfo{'to_file'} = $hash;
4944 # non-textual hash id's can be cached
4945 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
4946 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
4947 $expires = '+1d';
4950 # open patch output
4951 open $fd, "-|", git_cmd(), "diff", @diff_opts,
4952 '-p', ($format eq 'html' ? "--full-index" : ()),
4953 $hash_parent, $hash, "--"
4954 or die_error(500, "Open git-diff failed");
4955 } else {
4956 die_error(400, "Missing one of the blob diff parameters")
4957 unless %diffinfo;
4960 # header
4961 if ($format eq 'html') {
4962 my $formats_nav =
4963 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
4964 "raw");
4965 git_header_html(undef, $expires);
4966 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4967 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4968 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4969 } else {
4970 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
4971 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
4973 if (defined $file_name) {
4974 git_print_page_path($file_name, "blob", $hash_base);
4975 } else {
4976 print "<div class=\"page_path\"></div>\n";
4979 } elsif ($format eq 'plain') {
4980 print $cgi->header(
4981 -type => 'text/plain',
4982 -charset => 'utf-8',
4983 -expires => $expires,
4984 -content_disposition => 'inline; filename="' . "$file_name" . '.patch"');
4986 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
4988 } else {
4989 die_error(400, "Unknown blobdiff format");
4992 # patch
4993 if ($format eq 'html') {
4994 print "<div class=\"page_body\">\n";
4996 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
4997 close $fd;
4999 print "</div>\n"; # class="page_body"
5000 git_footer_html();
5002 } else {
5003 while (my $line = <$fd>) {
5004 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5005 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5007 print $line;
5009 last if $line =~ m!^\+\+\+!;
5011 local $/ = undef;
5012 print <$fd>;
5013 close $fd;
5017 sub git_blobdiff_plain {
5018 git_blobdiff('plain');
5021 sub git_commitdiff {
5022 my $format = shift || 'html';
5023 $hash ||= $hash_base || "HEAD";
5024 my %co = parse_commit($hash)
5025 or die_error(404, "Unknown commit object");
5027 # choose format for commitdiff for merge
5028 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5029 $hash_parent = '--cc';
5031 # we need to prepare $formats_nav before almost any parameter munging
5032 my $formats_nav;
5033 if ($format eq 'html') {
5034 $formats_nav =
5035 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5036 "raw");
5038 if (defined $hash_parent &&
5039 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5040 # commitdiff with two commits given
5041 my $hash_parent_short = $hash_parent;
5042 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5043 $hash_parent_short = substr($hash_parent, 0, 7);
5045 $formats_nav .=
5046 ' (from';
5047 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5048 if ($co{'parents'}[$i] eq $hash_parent) {
5049 $formats_nav .= ' parent ' . ($i+1);
5050 last;
5053 $formats_nav .= ': ' .
5054 $cgi->a({-href => href(action=>"commitdiff",
5055 hash=>$hash_parent)},
5056 esc_html($hash_parent_short)) .
5057 ')';
5058 } elsif (!$co{'parent'}) {
5059 # --root commitdiff
5060 $formats_nav .= ' (initial)';
5061 } elsif (scalar @{$co{'parents'}} == 1) {
5062 # single parent commit
5063 $formats_nav .=
5064 ' (parent: ' .
5065 $cgi->a({-href => href(action=>"commitdiff",
5066 hash=>$co{'parent'})},
5067 esc_html(substr($co{'parent'}, 0, 7))) .
5068 ')';
5069 } else {
5070 # merge commit
5071 if ($hash_parent eq '--cc') {
5072 $formats_nav .= ' | ' .
5073 $cgi->a({-href => href(action=>"commitdiff",
5074 hash=>$hash, hash_parent=>'-c')},
5075 'combined');
5076 } else { # $hash_parent eq '-c'
5077 $formats_nav .= ' | ' .
5078 $cgi->a({-href => href(action=>"commitdiff",
5079 hash=>$hash, hash_parent=>'--cc')},
5080 'compact');
5082 $formats_nav .=
5083 ' (merge: ' .
5084 join(' ', map {
5085 $cgi->a({-href => href(action=>"commitdiff",
5086 hash=>$_)},
5087 esc_html(substr($_, 0, 7)));
5088 } @{$co{'parents'}} ) .
5089 ')';
5093 my $hash_parent_param = $hash_parent;
5094 if (!defined $hash_parent_param) {
5095 # --cc for multiple parents, --root for parentless
5096 $hash_parent_param =
5097 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5100 # read commitdiff
5101 my $fd;
5102 my @difftree;
5103 if ($format eq 'html') {
5104 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5105 "--no-commit-id", "--patch-with-raw", "--full-index",
5106 $hash_parent_param, $hash, "--"
5107 or die_error(500, "Open git-diff-tree failed");
5109 while (my $line = <$fd>) {
5110 chomp $line;
5111 # empty line ends raw part of diff-tree output
5112 last unless $line;
5113 push @difftree, scalar parse_difftree_raw_line($line);
5116 } elsif ($format eq 'plain') {
5117 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5118 '-p', $hash_parent_param, $hash, "--"
5119 or die_error(500, "Open git-diff-tree failed");
5121 } else {
5122 die_error(400, "Unknown commitdiff format");
5125 # non-textual hash id's can be cached
5126 my $expires;
5127 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5128 $expires = "+1d";
5131 # write commit message
5132 if ($format eq 'html') {
5133 my $refs = git_get_references();
5134 my $ref = format_ref_marker($refs, $co{'id'});
5136 git_header_html(undef, $expires);
5137 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5138 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5139 git_print_authorship(\%co);
5140 print "<div class=\"page_body\">\n";
5141 if (@{$co{'comment'}} > 1) {
5142 print "<div class=\"log\">\n";
5143 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5144 print "</div>\n"; # class="log"
5147 } elsif ($format eq 'plain') {
5148 my $refs = git_get_references("tags");
5149 my $tagname = git_get_rev_name_tags($hash);
5150 my $filename = basename($project) . "-$hash.patch";
5152 print $cgi->header(
5153 -type => 'text/plain',
5154 -charset => 'utf-8',
5155 -expires => $expires,
5156 -content_disposition => 'inline; filename="' . "$filename" . '"');
5157 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5158 print "From: " . to_utf8($co{'author'}) . "\n";
5159 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5160 print "Subject: " . to_utf8($co{'title'}) . "\n";
5162 print "X-Git-Tag: $tagname\n" if $tagname;
5163 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5165 foreach my $line (@{$co{'comment'}}) {
5166 print to_utf8($line) . "\n";
5168 print "---\n\n";
5171 # write patch
5172 if ($format eq 'html') {
5173 my $use_parents = !defined $hash_parent ||
5174 $hash_parent eq '-c' || $hash_parent eq '--cc';
5175 git_difftree_body(\@difftree, $hash,
5176 $use_parents ? @{$co{'parents'}} : $hash_parent);
5177 print "<br/>\n";
5179 git_patchset_body($fd, \@difftree, $hash,
5180 $use_parents ? @{$co{'parents'}} : $hash_parent);
5181 close $fd;
5182 print "</div>\n"; # class="page_body"
5183 git_footer_html();
5185 } elsif ($format eq 'plain') {
5186 local $/ = undef;
5187 print <$fd>;
5188 close $fd
5189 or print "Reading git-diff-tree failed\n";
5193 sub git_commitdiff_plain {
5194 git_commitdiff('plain');
5197 sub git_history {
5198 if (!defined $hash_base) {
5199 $hash_base = git_get_head_hash($project);
5201 if (!defined $page) {
5202 $page = 0;
5204 my $ftype;
5205 my %co = parse_commit($hash_base)
5206 or die_error(404, "Unknown commit object");
5208 my $refs = git_get_references();
5209 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5211 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5212 $file_name, "--full-history")
5213 or die_error(404, "No such file or directory on given branch");
5215 if (!defined $hash && defined $file_name) {
5216 # some commits could have deleted file in question,
5217 # and not have it in tree, but one of them has to have it
5218 for (my $i = 0; $i <= @commitlist; $i++) {
5219 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5220 last if defined $hash;
5223 if (defined $hash) {
5224 $ftype = git_get_type($hash);
5226 if (!defined $ftype) {
5227 die_error(500, "Unknown type of object");
5230 my $paging_nav = '';
5231 if ($page > 0) {
5232 $paging_nav .=
5233 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5234 file_name=>$file_name)},
5235 "first");
5236 $paging_nav .= " &sdot; " .
5237 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5238 -accesskey => "p", -title => "Alt-p"}, "prev");
5239 } else {
5240 $paging_nav .= "first";
5241 $paging_nav .= " &sdot; prev";
5243 my $next_link = '';
5244 if ($#commitlist >= 100) {
5245 $next_link =
5246 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5247 -accesskey => "n", -title => "Alt-n"}, "next");
5248 $paging_nav .= " &sdot; $next_link";
5249 } else {
5250 $paging_nav .= " &sdot; next";
5253 git_header_html();
5254 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5255 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5256 git_print_page_path($file_name, $ftype, $hash_base);
5258 git_history_body(\@commitlist, 0, 99,
5259 $refs, $hash_base, $ftype, $next_link);
5261 git_footer_html();
5264 sub git_search {
5265 gitweb_check_feature('search') or die_error(403, "Search is disabled");
5266 if (!defined $searchtext) {
5267 die_error(400, "Text field is empty");
5269 if (!defined $hash) {
5270 $hash = git_get_head_hash($project);
5272 my %co = parse_commit($hash);
5273 if (!%co) {
5274 die_error(404, "Unknown commit object");
5276 if (!defined $page) {
5277 $page = 0;
5280 $searchtype ||= 'commit';
5281 if ($searchtype eq 'pickaxe') {
5282 # pickaxe may take all resources of your box and run for several minutes
5283 # with every query - so decide by yourself how public you make this feature
5284 gitweb_check_feature('pickaxe')
5285 or die_error(403, "Pickaxe is disabled");
5287 if ($searchtype eq 'grep') {
5288 gitweb_check_feature('grep')
5289 or die_error(403, "Grep is disabled");
5292 git_header_html();
5294 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5295 my $greptype;
5296 if ($searchtype eq 'commit') {
5297 $greptype = "--grep=";
5298 } elsif ($searchtype eq 'author') {
5299 $greptype = "--author=";
5300 } elsif ($searchtype eq 'committer') {
5301 $greptype = "--committer=";
5303 $greptype .= $searchtext;
5304 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5305 $greptype, '--regexp-ignore-case',
5306 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5308 my $paging_nav = '';
5309 if ($page > 0) {
5310 $paging_nav .=
5311 $cgi->a({-href => href(action=>"search", hash=>$hash,
5312 searchtext=>$searchtext,
5313 searchtype=>$searchtype)},
5314 "first");
5315 $paging_nav .= " &sdot; " .
5316 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5317 -accesskey => "p", -title => "Alt-p"}, "prev");
5318 } else {
5319 $paging_nav .= "first";
5320 $paging_nav .= " &sdot; prev";
5322 my $next_link = '';
5323 if ($#commitlist >= 100) {
5324 $next_link =
5325 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5326 -accesskey => "n", -title => "Alt-n"}, "next");
5327 $paging_nav .= " &sdot; $next_link";
5328 } else {
5329 $paging_nav .= " &sdot; next";
5332 if ($#commitlist >= 100) {
5335 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5336 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5337 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5340 if ($searchtype eq 'pickaxe') {
5341 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5342 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5344 print "<table class=\"pickaxe search\">\n";
5345 my $alternate = 1;
5346 $/ = "\n";
5347 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5348 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5349 ($search_use_regexp ? '--pickaxe-regex' : ());
5350 undef %co;
5351 my @files;
5352 while (my $line = <$fd>) {
5353 chomp $line;
5354 next unless $line;
5356 my %set = parse_difftree_raw_line($line);
5357 if (defined $set{'commit'}) {
5358 # finish previous commit
5359 if (%co) {
5360 print "</td>\n" .
5361 "<td class=\"link\">" .
5362 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5363 " | " .
5364 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5365 print "</td>\n" .
5366 "</tr>\n";
5369 if ($alternate) {
5370 print "<tr class=\"dark\">\n";
5371 } else {
5372 print "<tr class=\"light\">\n";
5374 $alternate ^= 1;
5375 %co = parse_commit($set{'commit'});
5376 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5377 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5378 "<td><i>$author</i></td>\n" .
5379 "<td>" .
5380 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5381 -class => "list subject"},
5382 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5383 } elsif (defined $set{'to_id'}) {
5384 next if ($set{'to_id'} =~ m/^0{40}$/);
5386 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5387 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5388 -class => "list"},
5389 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5390 "<br/>\n";
5393 close $fd;
5395 # finish last commit (warning: repetition!)
5396 if (%co) {
5397 print "</td>\n" .
5398 "<td class=\"link\">" .
5399 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5400 " | " .
5401 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5402 print "</td>\n" .
5403 "</tr>\n";
5406 print "</table>\n";
5409 if ($searchtype eq 'grep') {
5410 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5411 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5413 print "<table class=\"grep_search\">\n";
5414 my $alternate = 1;
5415 my $matches = 0;
5416 $/ = "\n";
5417 open my $fd, "-|", git_cmd(), 'grep', '-n',
5418 $search_use_regexp ? ('-E', '-i') : '-F',
5419 $searchtext, $co{'tree'};
5420 my $lastfile = '';
5421 while (my $line = <$fd>) {
5422 chomp $line;
5423 my ($file, $lno, $ltext, $binary);
5424 last if ($matches++ > 1000);
5425 if ($line =~ /^Binary file (.+) matches$/) {
5426 $file = $1;
5427 $binary = 1;
5428 } else {
5429 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5431 if ($file ne $lastfile) {
5432 $lastfile and print "</td></tr>\n";
5433 if ($alternate++) {
5434 print "<tr class=\"dark\">\n";
5435 } else {
5436 print "<tr class=\"light\">\n";
5438 print "<td class=\"list\">".
5439 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5440 file_name=>"$file"),
5441 -class => "list"}, esc_path($file));
5442 print "</td><td>\n";
5443 $lastfile = $file;
5445 if ($binary) {
5446 print "<div class=\"binary\">Binary file</div>\n";
5447 } else {
5448 $ltext = untabify($ltext);
5449 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5450 $ltext = esc_html($1, -nbsp=>1);
5451 $ltext .= '<span class="match">';
5452 $ltext .= esc_html($2, -nbsp=>1);
5453 $ltext .= '</span>';
5454 $ltext .= esc_html($3, -nbsp=>1);
5455 } else {
5456 $ltext = esc_html($ltext, -nbsp=>1);
5458 print "<div class=\"pre\">" .
5459 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5460 file_name=>"$file").'#l'.$lno,
5461 -class => "linenr"}, sprintf('%4i', $lno))
5462 . ' ' . $ltext . "</div>\n";
5465 if ($lastfile) {
5466 print "</td></tr>\n";
5467 if ($matches > 1000) {
5468 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5470 } else {
5471 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5473 close $fd;
5475 print "</table>\n";
5477 git_footer_html();
5480 sub git_search_help {
5481 git_header_html();
5482 git_print_page_nav('','', $hash,$hash,$hash);
5483 print <<EOT;
5484 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5485 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5486 the pattern entered is recognized as the POSIX extended
5487 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5488 insensitive).</p>
5489 <dl>
5490 <dt><b>commit</b></dt>
5491 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5493 my ($have_grep) = gitweb_check_feature('grep');
5494 if ($have_grep) {
5495 print <<EOT;
5496 <dt><b>grep</b></dt>
5497 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5498 a different one) are searched for the given pattern. On large trees, this search can take
5499 a while and put some strain on the server, so please use it with some consideration. Note that
5500 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5501 case-sensitive.</dd>
5504 print <<EOT;
5505 <dt><b>author</b></dt>
5506 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5507 <dt><b>committer</b></dt>
5508 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5510 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5511 if ($have_pickaxe) {
5512 print <<EOT;
5513 <dt><b>pickaxe</b></dt>
5514 <dd>All commits that caused the string to appear or disappear from any file (changes that
5515 added, removed or "modified" the string) will be listed. This search can take a while and
5516 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5517 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5520 print "</dl>\n";
5521 git_footer_html();
5524 sub git_shortlog {
5525 my $head = git_get_head_hash($project);
5526 if (!defined $hash) {
5527 $hash = $head;
5529 if (!defined $page) {
5530 $page = 0;
5532 my $refs = git_get_references();
5534 my @commitlist = parse_commits($hash, 101, (100 * $page));
5536 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5537 my $next_link = '';
5538 if ($#commitlist >= 100) {
5539 $next_link =
5540 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5541 -accesskey => "n", -title => "Alt-n"}, "next");
5544 git_header_html();
5545 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5546 git_print_header_div('summary', $project);
5548 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5550 git_footer_html();
5553 ## ......................................................................
5554 ## feeds (RSS, Atom; OPML)
5556 sub git_feed {
5557 my $format = shift || 'atom';
5558 my ($have_blame) = gitweb_check_feature('blame');
5560 # Atom: http://www.atomenabled.org/developers/syndication/
5561 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5562 if ($format ne 'rss' && $format ne 'atom') {
5563 die_error(400, "Unknown web feed format");
5566 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5567 my $head = $hash || 'HEAD';
5568 my @commitlist = parse_commits($head, 150, 0, $file_name);
5570 my %latest_commit;
5571 my %latest_date;
5572 my $content_type = "application/$format+xml";
5573 if (defined $cgi->http('HTTP_ACCEPT') &&
5574 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5575 # browser (feed reader) prefers text/xml
5576 $content_type = 'text/xml';
5578 if (defined($commitlist[0])) {
5579 %latest_commit = %{$commitlist[0]};
5580 %latest_date = parse_date($latest_commit{'author_epoch'});
5581 print $cgi->header(
5582 -type => $content_type,
5583 -charset => 'utf-8',
5584 -last_modified => $latest_date{'rfc2822'});
5585 } else {
5586 print $cgi->header(
5587 -type => $content_type,
5588 -charset => 'utf-8');
5591 # Optimization: skip generating the body if client asks only
5592 # for Last-Modified date.
5593 return if ($cgi->request_method() eq 'HEAD');
5595 # header variables
5596 my $title = "$site_name - $project/$action";
5597 my $feed_type = 'log';
5598 if (defined $hash) {
5599 $title .= " - '$hash'";
5600 $feed_type = 'branch log';
5601 if (defined $file_name) {
5602 $title .= " :: $file_name";
5603 $feed_type = 'history';
5605 } elsif (defined $file_name) {
5606 $title .= " - $file_name";
5607 $feed_type = 'history';
5609 $title .= " $feed_type";
5610 my $descr = git_get_project_description($project);
5611 if (defined $descr) {
5612 $descr = esc_html($descr);
5613 } else {
5614 $descr = "$project " .
5615 ($format eq 'rss' ? 'RSS' : 'Atom') .
5616 " feed";
5618 my $owner = git_get_project_owner($project);
5619 $owner = esc_html($owner);
5621 #header
5622 my $alt_url;
5623 if (defined $file_name) {
5624 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5625 } elsif (defined $hash) {
5626 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5627 } else {
5628 $alt_url = href(-full=>1, action=>"summary");
5630 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5631 if ($format eq 'rss') {
5632 print <<XML;
5633 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5634 <channel>
5636 print "<title>$title</title>\n" .
5637 "<link>$alt_url</link>\n" .
5638 "<description>$descr</description>\n" .
5639 "<language>en</language>\n";
5640 } elsif ($format eq 'atom') {
5641 print <<XML;
5642 <feed xmlns="http://www.w3.org/2005/Atom">
5644 print "<title>$title</title>\n" .
5645 "<subtitle>$descr</subtitle>\n" .
5646 '<link rel="alternate" type="text/html" href="' .
5647 $alt_url . '" />' . "\n" .
5648 '<link rel="self" type="' . $content_type . '" href="' .
5649 $cgi->self_url() . '" />' . "\n" .
5650 "<id>" . href(-full=>1) . "</id>\n" .
5651 # use project owner for feed author
5652 "<author><name>$owner</name></author>\n";
5653 if (defined $favicon) {
5654 print "<icon>" . esc_url($favicon) . "</icon>\n";
5656 if (defined $logo_url) {
5657 # not twice as wide as tall: 72 x 27 pixels
5658 print "<logo>" . esc_url($logo) . "</logo>\n";
5660 if (! %latest_date) {
5661 # dummy date to keep the feed valid until commits trickle in:
5662 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5663 } else {
5664 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5668 # contents
5669 for (my $i = 0; $i <= $#commitlist; $i++) {
5670 my %co = %{$commitlist[$i]};
5671 my $commit = $co{'id'};
5672 # we read 150, we always show 30 and the ones more recent than 48 hours
5673 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5674 last;
5676 my %cd = parse_date($co{'author_epoch'});
5678 # get list of changed files
5679 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5680 $co{'parent'} || "--root",
5681 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5682 or next;
5683 my @difftree = map { chomp; $_ } <$fd>;
5684 close $fd
5685 or next;
5687 # print element (entry, item)
5688 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5689 if ($format eq 'rss') {
5690 print "<item>\n" .
5691 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5692 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5693 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5694 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5695 "<link>$co_url</link>\n" .
5696 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5697 "<content:encoded>" .
5698 "<![CDATA[\n";
5699 } elsif ($format eq 'atom') {
5700 print "<entry>\n" .
5701 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5702 "<updated>$cd{'iso-8601'}</updated>\n" .
5703 "<author>\n" .
5704 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5705 if ($co{'author_email'}) {
5706 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5708 print "</author>\n" .
5709 # use committer for contributor
5710 "<contributor>\n" .
5711 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5712 if ($co{'committer_email'}) {
5713 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5715 print "</contributor>\n" .
5716 "<published>$cd{'iso-8601'}</published>\n" .
5717 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5718 "<id>$co_url</id>\n" .
5719 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5720 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
5722 my $comment = $co{'comment'};
5723 print "<pre>\n";
5724 foreach my $line (@$comment) {
5725 $line = esc_html($line);
5726 print "$line\n";
5728 print "</pre><ul>\n";
5729 foreach my $difftree_line (@difftree) {
5730 my %difftree = parse_difftree_raw_line($difftree_line);
5731 next if !$difftree{'from_id'};
5733 my $file = $difftree{'file'} || $difftree{'to_file'};
5735 print "<li>" .
5736 "[" .
5737 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
5738 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
5739 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
5740 file_name=>$file, file_parent=>$difftree{'from_file'}),
5741 -title => "diff"}, 'D');
5742 if ($have_blame) {
5743 print $cgi->a({-href => href(-full=>1, action=>"blame",
5744 file_name=>$file, hash_base=>$commit),
5745 -title => "blame"}, 'B');
5747 # if this is not a feed of a file history
5748 if (!defined $file_name || $file_name ne $file) {
5749 print $cgi->a({-href => href(-full=>1, action=>"history",
5750 file_name=>$file, hash=>$commit),
5751 -title => "history"}, 'H');
5753 $file = esc_path($file);
5754 print "] ".
5755 "$file</li>\n";
5757 if ($format eq 'rss') {
5758 print "</ul>]]>\n" .
5759 "</content:encoded>\n" .
5760 "</item>\n";
5761 } elsif ($format eq 'atom') {
5762 print "</ul>\n</div>\n" .
5763 "</content>\n" .
5764 "</entry>\n";
5768 # end of feed
5769 if ($format eq 'rss') {
5770 print "</channel>\n</rss>\n";
5771 } elsif ($format eq 'atom') {
5772 print "</feed>\n";
5776 sub git_rss {
5777 git_feed('rss');
5780 sub git_atom {
5781 git_feed('atom');
5784 sub git_opml {
5785 my @list = git_get_projects_list();
5787 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
5788 print <<XML;
5789 <?xml version="1.0" encoding="utf-8"?>
5790 <opml version="1.0">
5791 <head>
5792 <title>$site_name OPML Export</title>
5793 </head>
5794 <body>
5795 <outline text="git RSS feeds">
5798 foreach my $pr (@list) {
5799 my %proj = %$pr;
5800 my $head = git_get_head_hash($proj{'path'});
5801 if (!defined $head) {
5802 next;
5804 $git_dir = "$projectroot/$proj{'path'}";
5805 my %co = parse_commit($head);
5806 if (!%co) {
5807 next;
5810 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
5811 my $rss = "$my_url?p=$proj{'path'};a=rss";
5812 my $html = "$my_url?p=$proj{'path'};a=summary";
5813 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
5815 print <<XML;
5816 </outline>
5817 </body>
5818 </opml>