Merge commit 'refs/top-bases/girocco' into girocco
[git/repo.git] / gitweb / gitweb.perl
blob198ae7c820d0ca5548a8b05b563950a228124c1e
1 #!/usr/bin/perl
3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 BEGIN {
22 CGI->compile() if $ENV{'MOD_PERL'};
25 our $cgi = new CGI;
26 our $version = "++GIT_VERSION++";
27 our $my_url = $cgi->url();
28 our $my_uri = $cgi->url(-absolute => 1);
30 # if we're called with PATH_INFO, we have to strip that
31 # from the URL to find our real URL
32 if (my $path_info = $ENV{"PATH_INFO"}) {
33 $my_url =~ s,\Q$path_info\E$,,;
34 $my_uri =~ s,\Q$path_info\E$,,;
37 # core git executable to use
38 # this can just be "git" if your webserver has a sensible PATH
39 our $GIT = "++GIT_BINDIR++/git";
41 # absolute fs-path which will be prepended to the project path
42 #our $projectroot = "/pub/scm";
43 our $projectroot = "++GITWEB_PROJECTROOT++";
45 # fs traversing limit for getting project list
46 # the number is relative to the projectroot
47 our $project_maxdepth = "++GITWEB_PROJECT_MAXDEPTH++";
49 # target of the home link on top of all pages
50 our $home_link = $my_uri || "/";
52 # string of the home link on top of all pages
53 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
55 # name of your site or organization to appear in page titles
56 # replace this with something more descriptive for clearer bookmarks
57 our $site_name = "++GITWEB_SITENAME++"
58 || ($ENV{'SERVER_NAME'} || "Untitled") . " Git";
60 # filename of html text to include at top of each page
61 our $site_header = "++GITWEB_SITE_HEADER++";
62 # html text to include at home page
63 our $home_text = "++GITWEB_HOMETEXT++";
64 # filename of html text to include at bottom of each page
65 our $site_footer = "++GITWEB_SITE_FOOTER++";
67 # URI of stylesheets
68 our @stylesheets = ("++GITWEB_CSS++");
69 # URI of a single stylesheet, which can be overridden in GITWEB_CONFIG.
70 our $stylesheet = undef;
71 # URI of GIT logo (72x27 size)
72 our $logo = "++GITWEB_LOGO++";
73 # URI of GIT favicon, assumed to be image/png type
74 our $favicon = "++GITWEB_FAVICON++";
75 # URI of gitweb.js
76 our $gitwebjs = "++GITWEB_GITWEBJS++";
78 # URI and label (title) of GIT logo link
79 #our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/";
80 #our $logo_label = "git documentation";
81 our $logo_url = "http://git.or.cz/";
82 our $logo_label = "git homepage";
84 # source of projects list
85 our $projects_list = "++GITWEB_LIST++";
87 # the width (in characters) of the projects list "Description" column
88 our $projects_list_description_width = 25;
90 # default order of projects list
91 # valid values are none, project, descr, owner, and age
92 our $default_projects_order = "project";
94 # show repository only if this file exists
95 # (only effective if this variable evaluates to true)
96 our $export_ok = "++GITWEB_EXPORT_OK++";
98 # only allow viewing of repositories also shown on the overview page
99 our $strict_export = "++GITWEB_STRICT_EXPORT++";
101 # list of git base URLs used for URL to where fetch project from,
102 # i.e. full URL is "$git_base_url/$project"
103 our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
105 # an URL designated for pushing new changes, extended by the
106 # project name (i.e. "$git_base_push_url/$project")
107 our $git_base_push_url = '';
109 # default blob_plain mimetype and default charset for text/plain blob
110 our $default_blob_plain_mimetype = 'text/plain';
111 our $default_text_plain_charset = undef;
113 # file to use for guessing MIME types before trying /etc/mime.types
114 # (relative to the current git repository)
115 our $mimetypes_file = undef;
117 # assume this charset if line contains non-UTF-8 characters;
118 # it should be valid encoding (see Encoding::Supported(3pm) for list),
119 # for which encoding all byte sequences are valid, for example
120 # 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it
121 # could be even 'utf-8' for the old behavior)
122 our $fallback_encoding = 'latin1';
124 # rename detection options for git-diff and git-diff-tree
125 # - default is '-M', with the cost proportional to
126 # (number of removed files) * (number of new files).
127 # - more costly is '-C' (which implies '-M'), with the cost proportional to
128 # (number of changed files + number of removed files) * (number of new files)
129 # - even more costly is '-C', '--find-copies-harder' with cost
130 # (number of files in the original tree) * (number of new files)
131 # - one might want to include '-B' option, e.g. '-B', '-M'
132 our @diff_opts = ('-M'); # taken from git_commit
134 # projects list cache for busy sites with many projects;
135 # if you set this to non-zero, it will be used as the cached
136 # index lifetime in minutes
138 # the cached list version is stored in $cache_dir/$cache_name and can
139 # be tweaked by other scripts running with the same uid as gitweb -
140 # use this ONLY at secure installations; only single gitweb project
141 # root per system is supported, unless you tweak configuration!
142 our $projlist_cache_lifetime = 0; # in minutes
143 # FHS compliant $cache_dir would be "/var/cache/gitweb"
144 our $cache_dir =
145 (defined $ENV{'TMPDIR'} ? $ENV{'TMPDIR'} : '/tmp').'/gitweb';
146 our $projlist_cache_name = 'gitweb.index.cache';
148 # information about snapshot formats that gitweb is capable of serving
149 our %known_snapshot_formats = (
150 # name => {
151 # 'display' => display name,
152 # 'type' => mime type,
153 # 'suffix' => filename suffix,
154 # 'format' => --format for git-archive,
155 # 'compressor' => [compressor command and arguments]
156 # (array reference, optional)}
158 'tgz' => {
159 'display' => 'tar.gz',
160 'type' => 'application/x-gzip',
161 'suffix' => '.tar.gz',
162 'format' => 'tar',
163 'compressor' => ['gzip']},
165 'tbz2' => {
166 'display' => 'tar.bz2',
167 'type' => 'application/x-bzip2',
168 'suffix' => '.tar.bz2',
169 'format' => 'tar',
170 'compressor' => ['bzip2']},
172 'zip' => {
173 'display' => 'zip',
174 'type' => 'application/x-zip',
175 'suffix' => '.zip',
176 'format' => 'zip'},
179 # Aliases so we understand old gitweb.snapshot values in repository
180 # configuration.
181 our %known_snapshot_format_aliases = (
182 'gzip' => 'tgz',
183 'bzip2' => 'tbz2',
185 # backward compatibility: legacy gitweb config support
186 'x-gzip' => undef, 'gz' => undef,
187 'x-bzip2' => undef, 'bz2' => undef,
188 'x-zip' => undef, '' => undef,
191 # You define site-wide feature defaults here; override them with
192 # $GITWEB_CONFIG as necessary.
193 our %feature = (
194 # feature => {
195 # 'sub' => feature-sub (subroutine),
196 # 'override' => allow-override (boolean),
197 # 'default' => [ default options...] (array reference)}
199 # if feature is overridable (it means that allow-override has true value),
200 # then feature-sub will be called with default options as parameters;
201 # return value of feature-sub indicates if to enable specified feature
203 # if there is no 'sub' key (no feature-sub), then feature cannot be
204 # overriden
206 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
208 # Enable the 'blame' blob view, showing the last commit that modified
209 # each line in the file. This can be very CPU-intensive.
211 # To enable system wide have in $GITWEB_CONFIG
212 # $feature{'blame'}{'default'} = [1];
213 # To have project specific config enable override in $GITWEB_CONFIG
214 # $feature{'blame'}{'override'} = 1;
215 # and in project config gitweb.blame = 0|1;
216 'blame' => {
217 'sub' => \&feature_blame,
218 'override' => 0,
219 'default' => [0]},
221 # Enable the 'snapshot' link, providing a compressed archive of any
222 # tree. This can potentially generate high traffic if you have large
223 # project.
225 # Value is a list of formats defined in %known_snapshot_formats that
226 # you wish to offer.
227 # To disable system wide have in $GITWEB_CONFIG
228 # $feature{'snapshot'}{'default'} = [];
229 # To have project specific config enable override in $GITWEB_CONFIG
230 # $feature{'snapshot'}{'override'} = 1;
231 # and in project config, a comma-separated list of formats or "none"
232 # to disable. Example: gitweb.snapshot = tbz2,zip;
233 'snapshot' => {
234 'sub' => \&feature_snapshot,
235 'override' => 0,
236 'default' => ['tgz']},
238 # Enable text search, which will list the commits which match author,
239 # committer or commit text to a given string. Enabled by default.
240 # Project specific override is not supported.
241 'search' => {
242 'override' => 0,
243 'default' => [1]},
245 # Enable grep search, which will list the files in currently selected
246 # tree containing the given string. Enabled by default. This can be
247 # potentially CPU-intensive, of course.
249 # To enable system wide have in $GITWEB_CONFIG
250 # $feature{'grep'}{'default'} = [1];
251 # To have project specific config enable override in $GITWEB_CONFIG
252 # $feature{'grep'}{'override'} = 1;
253 # and in project config gitweb.grep = 0|1;
254 'grep' => {
255 'override' => 0,
256 'default' => [1]},
258 # Enable the pickaxe search, which will list the commits that modified
259 # a given string in a file. This can be practical and quite faster
260 # alternative to 'blame', but still potentially CPU-intensive.
262 # To enable system wide have in $GITWEB_CONFIG
263 # $feature{'pickaxe'}{'default'} = [1];
264 # To have project specific config enable override in $GITWEB_CONFIG
265 # $feature{'pickaxe'}{'override'} = 1;
266 # and in project config gitweb.pickaxe = 0|1;
267 'pickaxe' => {
268 'sub' => \&feature_pickaxe,
269 'override' => 0,
270 'default' => [1]},
272 # Make gitweb use an alternative format of the URLs which can be
273 # more readable and natural-looking: project name is embedded
274 # directly in the path and the query string contains other
275 # auxiliary information. All gitweb installations recognize
276 # URL in either format; this configures in which formats gitweb
277 # generates links.
279 # To enable system wide have in $GITWEB_CONFIG
280 # $feature{'pathinfo'}{'default'} = [1];
281 # Project specific override is not supported.
283 # Note that you will need to change the default location of CSS,
284 # favicon, logo and possibly other files to an absolute URL. Also,
285 # if gitweb.cgi serves as your indexfile, you will need to force
286 # $my_uri to contain the script name in your $GITWEB_CONFIG.
287 'pathinfo' => {
288 'override' => 0,
289 'default' => [0]},
291 # Make gitweb consider projects in project root subdirectories
292 # to be forks of existing projects. Given project $projname.git,
293 # projects matching $projname/*.git will not be shown in the main
294 # projects list, instead a '+' mark will be added to $projname
295 # there and a 'forks' view will be enabled for the project, listing
296 # all the forks. If project list is taken from a file, forks have
297 # to be listed after the main project.
299 # To enable system wide have in $GITWEB_CONFIG
300 # $feature{'forks'}{'default'} = [1];
301 # Project specific override is not supported.
302 'forks' => {
303 'override' => 0,
304 'default' => [0]},
306 # Insert custom links to the action bar of all project pages.
307 # This enables you mainly to link to third-party scripts integrating
308 # into gitweb; e.g. git-browser for graphical history representation
309 # or custom web-based repository administration interface.
311 # The 'default' value consists of a list of triplets in the form
312 # (label, link, position) where position is the label after which
313 # to inster the link and link is a format string where %n expands
314 # to the project name, %f to the project path within the filesystem,
315 # %h to the current hash (h gitweb parameter) and %b to the current
316 # hash base (hb gitweb parameter).
318 # To enable system wide have in $GITWEB_CONFIG e.g.
319 # $feature{'actions'}{'default'} = [('graphiclog',
320 # '/git-browser/by-commit.html?r=%n', 'summary')];
321 # Project specific override is not supported.
322 'actions' => {
323 'override' => 0,
324 'default' => []},
326 # Allow gitweb scan project content tags described in ctags/
327 # of project repository, and display the popular Web 2.0-ish
328 # "tag cloud" near the project list. Note that this is something
329 # COMPLETELY different from the normal Git tags.
331 # gitweb by itself can show existing tags, but it does not handle
332 # tagging itself; you need an external application for that.
333 # For an example script, check Girocco's cgi/tagproj.cgi.
334 # You may want to install the HTML::TagCloud Perl module to get
335 # a pretty tag cloud instead of just a list of tags.
337 # To enable system wide have in $GITWEB_CONFIG
338 # $feature{'ctags'}{'default'} = ['path_to_tag_script'];
339 # Project specific override is not supported.
340 'ctags' => {
341 'override' => 0,
342 'default' => [0]},
345 # email obfuscation
346 our $email;
347 if (eval { require HTML::Email::Obfuscate; 1 }) {
348 $email = HTML::Email::Obfuscate->new(lite => 1);
351 sub gitweb_check_feature {
352 my ($name) = @_;
353 return unless exists $feature{$name};
354 my ($sub, $override, @defaults) = (
355 $feature{$name}{'sub'},
356 $feature{$name}{'override'},
357 @{$feature{$name}{'default'}});
358 if (!$override) { return @defaults; }
359 if (!defined $sub) {
360 warn "feature $name is not overrideable";
361 return @defaults;
363 return $sub->(@defaults);
366 sub feature_blame {
367 my ($val) = git_get_project_config('blame', '--bool');
369 if ($val eq 'true') {
370 return 1;
371 } elsif ($val eq 'false') {
372 return 0;
375 return $_[0];
378 sub feature_snapshot {
379 my (@fmts) = @_;
381 my ($val) = git_get_project_config('snapshot');
383 if ($val) {
384 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
387 return @fmts;
390 sub feature_grep {
391 my ($val) = git_get_project_config('grep', '--bool');
393 if ($val eq 'true') {
394 return (1);
395 } elsif ($val eq 'false') {
396 return (0);
399 return ($_[0]);
402 sub feature_pickaxe {
403 my ($val) = git_get_project_config('pickaxe', '--bool');
405 if ($val eq 'true') {
406 return (1);
407 } elsif ($val eq 'false') {
408 return (0);
411 return ($_[0]);
414 # checking HEAD file with -e is fragile if the repository was
415 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
416 # and then pruned.
417 sub check_head_link {
418 my ($dir) = @_;
419 my $headfile = "$dir/HEAD";
420 return ((-e $headfile) ||
421 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
424 sub check_export_ok {
425 my ($dir) = @_;
426 return (check_head_link($dir) &&
427 (!$export_ok || -e "$dir/$export_ok"));
430 # process alternate names for backward compatibility
431 # filter out unsupported (unknown) snapshot formats
432 sub filter_snapshot_fmts {
433 my @fmts = @_;
435 @fmts = map {
436 exists $known_snapshot_format_aliases{$_} ?
437 $known_snapshot_format_aliases{$_} : $_} @fmts;
438 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
442 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
443 if (-e $GITWEB_CONFIG) {
444 do $GITWEB_CONFIG;
445 } else {
446 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
447 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
450 # version of the core git binary
451 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
453 $projects_list ||= $projectroot;
455 # ======================================================================
456 # input validation and dispatch
457 our $action = $cgi->param('a');
458 if (defined $action) {
459 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
460 die_error(400, "Invalid action parameter");
464 # parameters which are pathnames
465 our $project = $cgi->param('p');
466 if (defined $project) {
467 if (!validate_pathname($project) ||
468 !(-d "$projectroot/$project") ||
469 !check_head_link("$projectroot/$project") ||
470 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
471 ($strict_export && !project_in_list($project))) {
472 undef $project;
473 die_error(404, "No such project");
477 our $file_name = $cgi->param('f');
478 if (defined $file_name) {
479 if (!validate_pathname($file_name)) {
480 die_error(400, "Invalid file parameter");
484 our $file_parent = $cgi->param('fp');
485 if (defined $file_parent) {
486 if (!validate_pathname($file_parent)) {
487 die_error(400, "Invalid file parent parameter");
491 # parameters which are refnames
492 our $hash = $cgi->param('h');
493 if (defined $hash) {
494 if (!validate_refname($hash)) {
495 die_error(400, "Invalid hash parameter");
499 our $hash_parent = $cgi->param('hp');
500 if (defined $hash_parent) {
501 if (!validate_refname($hash_parent)) {
502 die_error(400, "Invalid hash parent parameter");
506 our $hash_base = $cgi->param('hb');
507 if (defined $hash_base) {
508 if (!validate_refname($hash_base)) {
509 die_error(400, "Invalid hash base parameter");
513 my %allowed_options = (
514 "--no-merges" => [ qw(rss atom log shortlog history) ],
517 our @extra_options = $cgi->param('opt');
518 if (defined @extra_options) {
519 foreach my $opt (@extra_options) {
520 if (not exists $allowed_options{$opt}) {
521 die_error(400, "Invalid option parameter");
523 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
524 die_error(400, "Invalid option parameter for this action");
529 our $hash_parent_base = $cgi->param('hpb');
530 if (defined $hash_parent_base) {
531 if (!validate_refname($hash_parent_base)) {
532 die_error(400, "Invalid hash parent base parameter");
536 # other parameters
537 our $page = $cgi->param('pg');
538 if (defined $page) {
539 if ($page =~ m/[^0-9]/) {
540 die_error(400, "Invalid page parameter");
544 our $searchtype = $cgi->param('st');
545 if (defined $searchtype) {
546 if ($searchtype =~ m/[^a-z]/) {
547 die_error(400, "Invalid searchtype parameter");
551 our $search_use_regexp = $cgi->param('sr');
553 our $searchtext = $cgi->param('s');
554 our $search_regexp;
555 if (defined $searchtext) {
556 if (length($searchtext) < 2) {
557 die_error(403, "At least two characters are required for search parameter");
559 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
562 # now read PATH_INFO and use it as alternative to parameters
563 sub evaluate_path_info {
564 return if defined $project;
565 my $path_info = $ENV{"PATH_INFO"};
566 return if !$path_info;
567 $path_info =~ s,^/+,,;
568 return if !$path_info;
569 # find which part of PATH_INFO is project
570 $project = $path_info;
571 $project =~ s,/+$,,;
572 while ($project && !check_head_link("$projectroot/$project")) {
573 $project =~ s,/*[^/]*$,,;
575 # validate project
576 $project = validate_pathname($project);
577 if (!$project ||
578 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
579 ($strict_export && !project_in_list($project))) {
580 undef $project;
581 return;
583 # do not change any parameters if an action is given using the query string
584 return if $action;
585 $path_info =~ s,^\Q$project\E/*,,;
586 my ($refname, $pathname) = split(/:/, $path_info, 2);
587 if (defined $pathname) {
588 # we got "project.git/branch:filename" or "project.git/branch:dir/"
589 # we could use git_get_type(branch:pathname), but it needs $git_dir
590 $pathname =~ s,^/+,,;
591 if (!$pathname || substr($pathname, -1) eq "/") {
592 $action ||= "tree";
593 $pathname =~ s,/$,,;
594 } else {
595 $action ||= "blob_plain";
597 $hash_base ||= validate_refname($refname);
598 $file_name ||= validate_pathname($pathname);
599 } elsif (defined $refname) {
600 # we got "project.git/branch"
601 $action ||= "shortlog";
602 $hash ||= validate_refname($refname);
605 evaluate_path_info();
607 # path to the current git repository
608 our $git_dir;
609 $git_dir = "$projectroot/$project" if $project;
611 # dispatch
612 my %actions = (
613 "blame" => \&git_blame,
614 "blame_incremental" => \&git_blame_incremental,
615 "blame_data" => \&git_blame_data,
616 "blobdiff" => \&git_blobdiff,
617 "blobdiff_plain" => \&git_blobdiff_plain,
618 "blob" => \&git_blob,
619 "blob_plain" => \&git_blob_plain,
620 "commitdiff" => \&git_commitdiff,
621 "commitdiff_plain" => \&git_commitdiff_plain,
622 "commit" => \&git_commit,
623 "forks" => \&git_forks,
624 "heads" => \&git_heads,
625 "history" => \&git_history,
626 "log" => \&git_log,
627 "rss" => \&git_rss,
628 "atom" => \&git_atom,
629 "search" => \&git_search,
630 "search_help" => \&git_search_help,
631 "shortlog" => \&git_shortlog,
632 "summary" => \&git_summary,
633 "tag" => \&git_tag,
634 "tags" => \&git_tags,
635 "tree" => \&git_tree,
636 "snapshot" => \&git_snapshot,
637 "object" => \&git_object,
638 # those below don't need $project
639 "opml" => \&git_opml,
640 "project_list" => \&git_project_list,
641 "project_index" => \&git_project_index,
644 if (!defined $action) {
645 if (defined $hash) {
646 $action = git_get_type($hash);
647 } elsif (defined $hash_base && defined $file_name) {
648 $action = git_get_type("$hash_base:$file_name");
649 } elsif (defined $project) {
650 $action = 'summary';
651 } else {
652 $action = 'project_list';
655 if (!defined($actions{$action})) {
656 die_error(400, "Unknown action");
658 if ($action !~ m/^(opml|project_list|project_index)$/ &&
659 !$project) {
660 die_error(400, "Project needed");
662 $actions{$action}->();
663 exit;
665 ## ======================================================================
666 ## action links
668 sub href (%) {
669 my %params = @_;
670 # default is to use -absolute url() i.e. $my_uri
671 my $href = $params{-full} ? $my_url : $my_uri;
673 # XXX: Warning: If you touch this, check the search form for updating,
674 # too.
676 my @mapping = (
677 project => "p",
678 action => "a",
679 file_name => "f",
680 file_parent => "fp",
681 hash => "h",
682 hash_parent => "hp",
683 hash_base => "hb",
684 hash_parent_base => "hpb",
685 page => "pg",
686 order => "o",
687 searchtext => "s",
688 searchtype => "st",
689 snapshot_format => "sf",
690 extra_options => "opt",
691 search_use_regexp => "sr",
693 my %mapping = @mapping;
695 $params{'project'} = $project unless exists $params{'project'};
697 if ($params{-replay}) {
698 while (my ($name, $symbol) = each %mapping) {
699 if (!exists $params{$name}) {
700 # to allow for multivalued params we use arrayref form
701 $params{$name} = [ $cgi->param($symbol) ];
706 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
707 if ($use_pathinfo) {
708 # use PATH_INFO for project name
709 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
710 delete $params{'project'};
712 # Summary just uses the project path URL
713 if (defined $params{'action'} && $params{'action'} eq 'summary') {
714 delete $params{'action'};
718 # now encode the parameters explicitly
719 my @result = ();
720 for (my $i = 0; $i < @mapping; $i += 2) {
721 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
722 if (defined $params{$name}) {
723 if (ref($params{$name}) eq "ARRAY") {
724 foreach my $par (@{$params{$name}}) {
725 push @result, $symbol . "=" . esc_param($par);
727 } else {
728 push @result, $symbol . "=" . esc_param($params{$name});
732 $href .= "?" . join(';', @result) if $params{-partial_query} or scalar @result;
734 return $href;
738 ## ======================================================================
739 ## validation, quoting/unquoting and escaping
741 sub validate_pathname {
742 my $input = shift || return undef;
744 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
745 # at the beginning, at the end, and between slashes.
746 # also this catches doubled slashes
747 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
748 return undef;
750 # no null characters
751 if ($input =~ m!\0!) {
752 return undef;
754 return $input;
757 sub validate_refname {
758 my $input = shift || return undef;
760 # textual hashes are O.K.
761 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
762 return $input;
764 # it must be correct pathname
765 $input = validate_pathname($input)
766 or return undef;
767 # restrictions on ref name according to git-check-ref-format
768 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
769 return undef;
771 return $input;
774 # decode sequences of octets in utf8 into Perl's internal form,
775 # which is utf-8 with utf8 flag set if needed. gitweb writes out
776 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
777 sub to_utf8 {
778 my $str = shift;
779 if (utf8::valid($str)) {
780 utf8::decode($str);
781 return $str;
782 } else {
783 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
787 # quote unsafe chars, but keep the slash, even when it's not
788 # correct, but quoted slashes look too horrible in bookmarks
789 sub esc_param {
790 my $str = shift;
791 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
792 $str =~ s/\+/%2B/g;
793 $str =~ s/ /\+/g;
794 return $str;
797 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
798 sub esc_url {
799 my $str = shift;
800 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
801 $str =~ s/\+/%2B/g;
802 $str =~ s/ /\+/g;
803 return $str;
806 # replace invalid utf8 character with SUBSTITUTION sequence
807 sub esc_html ($;%) {
808 my $str = shift;
809 my %opts = @_;
811 $str = to_utf8($str);
812 $str = $cgi->escapeHTML($str);
813 if ($opts{'-nbsp'}) {
814 $str =~ s/ /&nbsp;/g;
816 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
817 return $str;
820 # quote control characters and escape filename to HTML
821 sub esc_path {
822 my $str = shift;
823 my %opts = @_;
825 $str = to_utf8($str);
826 $str = $cgi->escapeHTML($str);
827 if ($opts{'-nbsp'}) {
828 $str =~ s/ /&nbsp;/g;
830 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
831 return $str;
834 # Make control characters "printable", using character escape codes (CEC)
835 sub quot_cec {
836 my $cntrl = shift;
837 my %opts = @_;
838 my %es = ( # character escape codes, aka escape sequences
839 "\t" => '\t', # tab (HT)
840 "\n" => '\n', # line feed (LF)
841 "\r" => '\r', # carrige return (CR)
842 "\f" => '\f', # form feed (FF)
843 "\b" => '\b', # backspace (BS)
844 "\a" => '\a', # alarm (bell) (BEL)
845 "\e" => '\e', # escape (ESC)
846 "\013" => '\v', # vertical tab (VT)
847 "\000" => '\0', # nul character (NUL)
849 my $chr = ( (exists $es{$cntrl})
850 ? $es{$cntrl}
851 : sprintf('\%2x', ord($cntrl)) );
852 if ($opts{-nohtml}) {
853 return $chr;
854 } else {
855 return "<span class=\"cntrl\">$chr</span>";
859 # Alternatively use unicode control pictures codepoints,
860 # Unicode "printable representation" (PR)
861 sub quot_upr {
862 my $cntrl = shift;
863 my %opts = @_;
865 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
866 if ($opts{-nohtml}) {
867 return $chr;
868 } else {
869 return "<span class=\"cntrl\">$chr</span>";
873 # git may return quoted and escaped filenames
874 sub unquote {
875 my $str = shift;
877 sub unq {
878 my $seq = shift;
879 my %es = ( # character escape codes, aka escape sequences
880 't' => "\t", # tab (HT, TAB)
881 'n' => "\n", # newline (NL)
882 'r' => "\r", # return (CR)
883 'f' => "\f", # form feed (FF)
884 'b' => "\b", # backspace (BS)
885 'a' => "\a", # alarm (bell) (BEL)
886 'e' => "\e", # escape (ESC)
887 'v' => "\013", # vertical tab (VT)
890 if ($seq =~ m/^[0-7]{1,3}$/) {
891 # octal char sequence
892 return chr(oct($seq));
893 } elsif (exists $es{$seq}) {
894 # C escape sequence, aka character escape code
895 return $es{$seq};
897 # quoted ordinary character
898 return $seq;
901 if ($str =~ m/^"(.*)"$/) {
902 # needs unquoting
903 $str = $1;
904 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
906 return $str;
909 # escape tabs (convert tabs to spaces)
910 sub untabify {
911 my $line = shift;
913 while ((my $pos = index($line, "\t")) != -1) {
914 if (my $count = (8 - ($pos % 8))) {
915 my $spaces = ' ' x $count;
916 $line =~ s/\t/$spaces/;
920 return $line;
923 sub project_in_list {
924 my $project = shift;
925 my @list = git_get_projects_list();
926 return @list && scalar(grep { $_->{'path'} eq $project } @list);
929 ## ----------------------------------------------------------------------
930 ## HTML aware string manipulation
932 # Try to chop given string on a word boundary between position
933 # $len and $len+$add_len. If there is no word boundary there,
934 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
935 # (marking chopped part) would be longer than given string.
936 sub chop_str {
937 my $str = shift;
938 my $len = shift;
939 my $add_len = shift || 10;
940 my $where = shift || 'right'; # 'left' | 'center' | 'right'
942 # Make sure perl knows it is utf8 encoded so we don't
943 # cut in the middle of a utf8 multibyte char.
944 $str = to_utf8($str);
946 # allow only $len chars, but don't cut a word if it would fit in $add_len
947 # if it doesn't fit, cut it if it's still longer than the dots we would add
948 # remove chopped character entities entirely
950 # when chopping in the middle, distribute $len into left and right part
951 # return early if chopping wouldn't make string shorter
952 if ($where eq 'center') {
953 return $str if ($len + 5 >= length($str)); # filler is length 5
954 $len = int($len/2);
955 } else {
956 return $str if ($len + 4 >= length($str)); # filler is length 4
959 # regexps: ending and beginning with word part up to $add_len
960 my $endre = qr/.{$len}\w{0,$add_len}/;
961 my $begre = qr/\w{0,$add_len}.{$len}/;
963 if ($where eq 'left') {
964 $str =~ m/^(.*?)($begre)$/;
965 my ($lead, $body) = ($1, $2);
966 if (length($lead) > 4) {
967 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
968 $lead = " ...";
970 return "$lead$body";
972 } elsif ($where eq 'center') {
973 $str =~ m/^($endre)(.*)$/;
974 my ($left, $str) = ($1, $2);
975 $str =~ m/^(.*?)($begre)$/;
976 my ($mid, $right) = ($1, $2);
977 if (length($mid) > 5) {
978 $left =~ s/&[^;]*$//;
979 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
980 $mid = " ... ";
982 return "$left$mid$right";
984 } else {
985 $str =~ m/^($endre)(.*)$/;
986 my $body = $1;
987 my $tail = $2;
988 if (length($tail) > 4) {
989 $body =~ s/&[^;]*$//;
990 $tail = "... ";
992 return "$body$tail";
996 # pass-through email filter, obfuscating it when possible
997 sub email_obfuscate {
998 my ($str) = @_;
999 if ($email) {
1000 $str = $email->escape_html($str);
1001 # Stock HTML::Email::Obfuscate version likes to produce
1002 # invalid XHTML...
1003 $str =~ s#<(/?)B>#<$1b>#g;
1004 return $str;
1005 } else {
1006 $str = esc_html($str);
1007 $str =~ s/@/&#x40;/;
1008 return $str;
1012 # takes the same arguments as chop_str, but also wraps a <span> around the
1013 # result with a title attribute if it does get chopped. Additionally, the
1014 # string is HTML-escaped.
1015 sub chop_and_escape_str {
1016 my ($str) = @_;
1018 my $chopped = chop_str(@_);
1019 if ($chopped eq $str) {
1020 return email_obfuscate($chopped);
1021 } else {
1022 $str =~ s/([[:cntrl:]])/?/g;
1023 return $cgi->span({-title=>$str}, email_obfuscate($chopped));
1027 ## ----------------------------------------------------------------------
1028 ## functions returning short strings
1030 # CSS class for given age value (in seconds)
1031 sub age_class {
1032 my $age = shift;
1034 if (!defined $age) {
1035 return "noage";
1036 } elsif ($age < 60*60*2) {
1037 return "age0";
1038 } elsif ($age < 60*60*24*2) {
1039 return "age1";
1040 } else {
1041 return "age2";
1045 # convert age in seconds to "nn units ago" string
1046 sub age_string {
1047 my $age = shift;
1048 my $age_str;
1050 if ($age > 60*60*24*365*2) {
1051 $age_str = (int $age/60/60/24/365);
1052 $age_str .= " years ago";
1053 } elsif ($age > 60*60*24*(365/12)*2) {
1054 $age_str = int $age/60/60/24/(365/12);
1055 $age_str .= " months ago";
1056 } elsif ($age > 60*60*24*7*2) {
1057 $age_str = int $age/60/60/24/7;
1058 $age_str .= " weeks ago";
1059 } elsif ($age > 60*60*24*2) {
1060 $age_str = int $age/60/60/24;
1061 $age_str .= " days ago";
1062 } elsif ($age > 60*60*2) {
1063 $age_str = int $age/60/60;
1064 $age_str .= " hours ago";
1065 } elsif ($age > 60*2) {
1066 $age_str = int $age/60;
1067 $age_str .= " min ago";
1068 } elsif ($age > 2) {
1069 $age_str = int $age;
1070 $age_str .= " sec ago";
1071 } else {
1072 $age_str .= " right now";
1074 return $age_str;
1077 use constant {
1078 S_IFINVALID => 0030000,
1079 S_IFGITLINK => 0160000,
1082 # submodule/subproject, a commit object reference
1083 sub S_ISGITLINK($) {
1084 my $mode = shift;
1086 return (($mode & S_IFMT) == S_IFGITLINK)
1089 # convert file mode in octal to symbolic file mode string
1090 sub mode_str {
1091 my $mode = oct shift;
1093 if (S_ISGITLINK($mode)) {
1094 return 'm---------';
1095 } elsif (S_ISDIR($mode & S_IFMT)) {
1096 return 'drwxr-xr-x';
1097 } elsif (S_ISLNK($mode)) {
1098 return 'lrwxrwxrwx';
1099 } elsif (S_ISREG($mode)) {
1100 # git cares only about the executable bit
1101 if ($mode & S_IXUSR) {
1102 return '-rwxr-xr-x';
1103 } else {
1104 return '-rw-r--r--';
1106 } else {
1107 return '----------';
1111 # convert file mode in octal to file type string
1112 sub file_type {
1113 my $mode = shift;
1115 if ($mode !~ m/^[0-7]+$/) {
1116 return $mode;
1117 } else {
1118 $mode = oct $mode;
1121 if (S_ISGITLINK($mode)) {
1122 return "submodule";
1123 } elsif (S_ISDIR($mode & S_IFMT)) {
1124 return "directory";
1125 } elsif (S_ISLNK($mode)) {
1126 return "symlink";
1127 } elsif (S_ISREG($mode)) {
1128 return "file";
1129 } else {
1130 return "unknown";
1134 # convert file mode in octal to file type description string
1135 sub file_type_long {
1136 my $mode = shift;
1138 if ($mode !~ m/^[0-7]+$/) {
1139 return $mode;
1140 } else {
1141 $mode = oct $mode;
1144 if (S_ISGITLINK($mode)) {
1145 return "submodule";
1146 } elsif (S_ISDIR($mode & S_IFMT)) {
1147 return "directory";
1148 } elsif (S_ISLNK($mode)) {
1149 return "symlink";
1150 } elsif (S_ISREG($mode)) {
1151 if ($mode & S_IXUSR) {
1152 return "executable";
1153 } else {
1154 return "file";
1156 } else {
1157 return "unknown";
1162 ## ----------------------------------------------------------------------
1163 ## functions returning short HTML fragments, or transforming HTML fragments
1164 ## which don't belong to other sections
1166 # format line of commit message.
1167 sub format_log_line_html {
1168 my $line = shift;
1170 $line = esc_html($line, -nbsp=>1);
1171 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1172 my $hash_text = $1;
1173 my $link =
1174 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1175 -class => "text"}, $hash_text);
1176 $line =~ s/$hash_text/$link/;
1178 return $line;
1181 # format marker of refs pointing to given object
1183 # the destination action is chosen based on object type and current context:
1184 # - for annotated tags, we choose the tag view unless it's the current view
1185 # already, in which case we go to shortlog view
1186 # - for other refs, we keep the current view if we're in history, shortlog or
1187 # log view, and select shortlog otherwise
1188 sub format_ref_marker {
1189 my ($refs, $id) = @_;
1190 my $markers = '';
1192 if (defined $refs->{$id}) {
1193 foreach my $ref (@{$refs->{$id}}) {
1194 # this code exploits the fact that non-lightweight tags are the
1195 # only indirect objects, and that they are the only objects for which
1196 # we want to use tag instead of shortlog as action
1197 my ($type, $name) = qw();
1198 my $indirect = ($ref =~ s/\^\{\}$//);
1199 # e.g. tags/v2.6.11 or heads/next
1200 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1201 $type = $1;
1202 $name = $2;
1203 } else {
1204 $type = "ref";
1205 $name = $ref;
1208 my $class = $type;
1209 $class .= " indirect" if $indirect;
1211 my $dest_action = "shortlog";
1213 if ($indirect) {
1214 $dest_action = "tag" unless $action eq "tag";
1215 } elsif ($action =~ /^(history|(short)?log)$/) {
1216 $dest_action = $action;
1219 my $dest = "";
1220 $dest .= "refs/" unless $ref =~ m!^refs/!;
1221 $dest .= $ref;
1223 my $link = $cgi->a({
1224 -href => href(
1225 action=>$dest_action,
1226 hash=>$dest
1227 )}, $name);
1229 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1230 $link . "</span>";
1234 if ($markers) {
1235 return ' <span class="refs">'. $markers . '</span>';
1236 } else {
1237 return "";
1241 # format, perhaps shortened and with markers, title line
1242 sub format_subject_html {
1243 my ($long, $short, $href, $extra) = @_;
1244 $extra = '' unless defined($extra);
1246 if (length($short) < length($long)) {
1247 return $cgi->a({-href => $href, -class => "list subject",
1248 -title => to_utf8($long)},
1249 esc_html($short) . $extra);
1250 } else {
1251 return $cgi->a({-href => $href, -class => "list subject"},
1252 esc_html($long) . $extra);
1256 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1257 sub format_git_diff_header_line {
1258 my $line = shift;
1259 my $diffinfo = shift;
1260 my ($from, $to) = @_;
1262 if ($diffinfo->{'nparents'}) {
1263 # combined diff
1264 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1265 if ($to->{'href'}) {
1266 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1267 esc_path($to->{'file'}));
1268 } else { # file was deleted (no href)
1269 $line .= esc_path($to->{'file'});
1271 } else {
1272 # "ordinary" diff
1273 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1274 if ($from->{'href'}) {
1275 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1276 'a/' . esc_path($from->{'file'}));
1277 } else { # file was added (no href)
1278 $line .= 'a/' . esc_path($from->{'file'});
1280 $line .= ' ';
1281 if ($to->{'href'}) {
1282 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1283 'b/' . esc_path($to->{'file'}));
1284 } else { # file was deleted
1285 $line .= 'b/' . esc_path($to->{'file'});
1289 return "<div class=\"diff header\">$line</div>\n";
1292 # format extended diff header line, before patch itself
1293 sub format_extended_diff_header_line {
1294 my $line = shift;
1295 my $diffinfo = shift;
1296 my ($from, $to) = @_;
1298 # match <path>
1299 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1300 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1301 esc_path($from->{'file'}));
1303 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1304 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1305 esc_path($to->{'file'}));
1307 # match single <mode>
1308 if ($line =~ m/\s(\d{6})$/) {
1309 $line .= '<span class="info"> (' .
1310 file_type_long($1) .
1311 ')</span>';
1313 # match <hash>
1314 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1315 # can match only for combined diff
1316 $line = 'index ';
1317 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1318 if ($from->{'href'}[$i]) {
1319 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1320 -class=>"hash"},
1321 substr($diffinfo->{'from_id'}[$i],0,7));
1322 } else {
1323 $line .= '0' x 7;
1325 # separator
1326 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1328 $line .= '..';
1329 if ($to->{'href'}) {
1330 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1331 substr($diffinfo->{'to_id'},0,7));
1332 } else {
1333 $line .= '0' x 7;
1336 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1337 # can match only for ordinary diff
1338 my ($from_link, $to_link);
1339 if ($from->{'href'}) {
1340 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1341 substr($diffinfo->{'from_id'},0,7));
1342 } else {
1343 $from_link = '0' x 7;
1345 if ($to->{'href'}) {
1346 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1347 substr($diffinfo->{'to_id'},0,7));
1348 } else {
1349 $to_link = '0' x 7;
1351 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1352 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1355 return $line . "<br/>\n";
1358 # format from-file/to-file diff header
1359 sub format_diff_from_to_header {
1360 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1361 my $line;
1362 my $result = '';
1364 $line = $from_line;
1365 #assert($line =~ m/^---/) if DEBUG;
1366 # no extra formatting for "^--- /dev/null"
1367 if (! $diffinfo->{'nparents'}) {
1368 # ordinary (single parent) diff
1369 if ($line =~ m!^--- "?a/!) {
1370 if ($from->{'href'}) {
1371 $line = '--- a/' .
1372 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1373 esc_path($from->{'file'}));
1374 } else {
1375 $line = '--- a/' .
1376 esc_path($from->{'file'});
1379 $result .= qq!<div class="diff from_file">$line</div>\n!;
1381 } else {
1382 # combined diff (merge commit)
1383 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1384 if ($from->{'href'}[$i]) {
1385 $line = '--- ' .
1386 $cgi->a({-href=>href(action=>"blobdiff",
1387 hash_parent=>$diffinfo->{'from_id'}[$i],
1388 hash_parent_base=>$parents[$i],
1389 file_parent=>$diffinfo->{'from_prefix'}.$from->{'file'}[$i],
1390 hash=>$diffinfo->{'to_id'},
1391 hash_base=>$hash,
1392 file_name=>$diffinfo->{'to_prefix'}.$to->{'file'}),
1393 -class=>"path",
1394 -title=>"diff" . ($i+1)},
1395 $i+1) .
1396 '/' .
1397 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1398 esc_path($from->{'file'}[$i]));
1399 } else {
1400 $line = '--- /dev/null';
1402 $result .= qq!<div class="diff from_file">$line</div>\n!;
1406 $line = $to_line;
1407 #assert($line =~ m/^\+\+\+/) if DEBUG;
1408 # no extra formatting for "^+++ /dev/null"
1409 if ($line =~ m!^\+\+\+ "?b/!) {
1410 if ($to->{'href'}) {
1411 $line = '+++ b/' .
1412 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1413 esc_path($to->{'file'}));
1414 } else {
1415 $line = '+++ b/' .
1416 esc_path($to->{'file'});
1419 $result .= qq!<div class="diff to_file">$line</div>\n!;
1421 return $result;
1424 # create note for patch simplified by combined diff
1425 sub format_diff_cc_simplified {
1426 my ($diffinfo, @parents) = @_;
1427 my $result = '';
1429 $result .= "<div class=\"diff header\">" .
1430 "diff --cc ";
1431 if (!is_deleted($diffinfo)) {
1432 $result .= $cgi->a({-href => href(action=>"blob",
1433 hash_base=>$hash,
1434 hash=>$diffinfo->{'to_id'},
1435 file_name=>$diffinfo->{'to_prefix'}.$diffinfo->{'to_file'}),
1436 -class => "path"},
1437 esc_path($diffinfo->{'to_file'}));
1438 } else {
1439 $result .= esc_path($diffinfo->{'to_file'});
1441 $result .= "</div>\n" . # class="diff header"
1442 "<div class=\"diff nodifferences\">" .
1443 "Simple merge" .
1444 "</div>\n"; # class="diff nodifferences"
1446 return $result;
1449 # format patch (diff) line (not to be used for diff headers)
1450 sub format_diff_line {
1451 my $line = shift;
1452 my ($from, $to) = @_;
1453 my $diff_class = "";
1455 chomp $line;
1457 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1458 # combined diff
1459 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1460 if ($line =~ m/^\@{3}/) {
1461 $diff_class = " chunk_header";
1462 } elsif ($line =~ m/^\\/) {
1463 $diff_class = " incomplete";
1464 } elsif ($prefix =~ tr/+/+/) {
1465 $diff_class = " add";
1466 } elsif ($prefix =~ tr/-/-/) {
1467 $diff_class = " rem";
1469 } else {
1470 # assume ordinary diff
1471 my $char = substr($line, 0, 1);
1472 if ($char eq '+') {
1473 $diff_class = " add";
1474 } elsif ($char eq '-') {
1475 $diff_class = " rem";
1476 } elsif ($char eq '@') {
1477 $diff_class = " chunk_header";
1478 } elsif ($char eq "\\") {
1479 $diff_class = " incomplete";
1482 $line = untabify($line);
1483 if ($from && $to && $line =~ m/^\@{2} /) {
1484 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1485 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1487 $from_lines = 0 unless defined $from_lines;
1488 $to_lines = 0 unless defined $to_lines;
1490 if ($from->{'href'}) {
1491 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1492 -class=>"list"}, $from_text);
1494 if ($to->{'href'}) {
1495 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1496 -class=>"list"}, $to_text);
1498 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1499 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1500 return "<div class=\"diff$diff_class\">$line</div>\n";
1501 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1502 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1503 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1505 @from_text = split(' ', $ranges);
1506 for (my $i = 0; $i < @from_text; ++$i) {
1507 ($from_start[$i], $from_nlines[$i]) =
1508 (split(',', substr($from_text[$i], 1)), 0);
1511 $to_text = pop @from_text;
1512 $to_start = pop @from_start;
1513 $to_nlines = pop @from_nlines;
1515 $line = "<span class=\"chunk_info\">$prefix ";
1516 for (my $i = 0; $i < @from_text; ++$i) {
1517 if ($from->{'href'}[$i]) {
1518 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1519 -class=>"list"}, $from_text[$i]);
1520 } else {
1521 $line .= $from_text[$i];
1523 $line .= " ";
1525 if ($to->{'href'}) {
1526 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1527 -class=>"list"}, $to_text);
1528 } else {
1529 $line .= $to_text;
1531 $line .= " $prefix</span>" .
1532 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1533 return "<div class=\"diff$diff_class\">$line</div>\n";
1535 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1538 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1539 # linked. Pass the hash of the tree/commit to snapshot.
1540 sub format_snapshot_links {
1541 my ($hash) = @_;
1542 my @snapshot_fmts = gitweb_check_feature('snapshot');
1543 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1544 my $num_fmts = @snapshot_fmts;
1545 if ($num_fmts > 1) {
1546 # A parenthesized list of links bearing format names.
1547 # e.g. "snapshot (_tar.gz_ _zip_)"
1548 return "snapshot (" . join(' ', map
1549 $cgi->a({
1550 -href => href(
1551 action=>"snapshot",
1552 hash=>$hash,
1553 snapshot_format=>$_
1555 }, $known_snapshot_formats{$_}{'display'})
1556 , @snapshot_fmts) . ")";
1557 } elsif ($num_fmts == 1) {
1558 # A single "snapshot" link whose tooltip bears the format name.
1559 # i.e. "_snapshot_"
1560 my ($fmt) = @snapshot_fmts;
1561 return
1562 $cgi->a({
1563 -href => href(
1564 action=>"snapshot",
1565 hash=>$hash,
1566 snapshot_format=>$fmt
1568 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1569 }, "snapshot");
1570 } else { # $num_fmts == 0
1571 return undef;
1575 ## ......................................................................
1576 ## functions returning values to be passed, perhaps after some
1577 ## transformation, to other functions; e.g. returning arguments to href()
1579 # returns hash to be passed to href to generate gitweb URL
1580 # in -title key it returns description of link
1581 sub get_feed_info {
1582 my $format = shift || 'Atom';
1583 my %res = (action => lc($format));
1585 # feed links are possible only for project views
1586 return unless (defined $project);
1587 # some views should link to OPML, or to generic project feed,
1588 # or don't have specific feed yet (so they should use generic)
1589 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1591 my $branch;
1592 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1593 # from tag links; this also makes possible to detect branch links
1594 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1595 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1596 $branch = $1;
1598 # find log type for feed description (title)
1599 my $type = 'log';
1600 if (defined $file_name) {
1601 $type = "history of $file_name";
1602 $type .= "/" if ($action eq 'tree');
1603 $type .= " on '$branch'" if (defined $branch);
1604 } else {
1605 $type = "log of $branch" if (defined $branch);
1608 $res{-title} = $type;
1609 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1610 $res{'file_name'} = $file_name;
1612 return %res;
1615 ## ----------------------------------------------------------------------
1616 ## git utility subroutines, invoking git commands
1618 # returns path to the core git executable and the --git-dir parameter as list
1619 sub git_cmd {
1620 return $GIT, '--git-dir='.$git_dir;
1623 # quote the given arguments for passing them to the shell
1624 # quote_command("command", "arg 1", "arg with ' and ! characters")
1625 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1626 # Try to avoid using this function wherever possible.
1627 sub quote_command {
1628 return join(' ',
1629 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1632 # get HEAD ref of given project as hash
1633 sub git_get_head_hash {
1634 my $project = shift;
1635 my $o_git_dir = $git_dir;
1636 my $retval = undef;
1637 $git_dir = "$projectroot/$project";
1638 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1639 my $head = <$fd>;
1640 close $fd;
1641 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1642 $retval = $1;
1645 if (defined $o_git_dir) {
1646 $git_dir = $o_git_dir;
1648 return $retval;
1651 # get type of given object
1652 sub git_get_type {
1653 my $hash = shift;
1655 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1656 my $type = <$fd>;
1657 close $fd or return;
1658 chomp $type;
1659 return $type;
1662 # repository configuration
1663 our $config_file = '';
1664 our %config;
1666 # store multiple values for single key as anonymous array reference
1667 # single values stored directly in the hash, not as [ <value> ]
1668 sub hash_set_multi {
1669 my ($hash, $key, $value) = @_;
1671 if (!exists $hash->{$key}) {
1672 $hash->{$key} = $value;
1673 } elsif (!ref $hash->{$key}) {
1674 $hash->{$key} = [ $hash->{$key}, $value ];
1675 } else {
1676 push @{$hash->{$key}}, $value;
1680 # return hash of git project configuration
1681 # optionally limited to some section, e.g. 'gitweb'
1682 sub git_parse_project_config {
1683 my $section_regexp = shift;
1684 my %config;
1686 local $/ = "\0";
1688 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1689 or return;
1691 while (my $keyval = <$fh>) {
1692 chomp $keyval;
1693 my ($key, $value) = split(/\n/, $keyval, 2);
1695 hash_set_multi(\%config, $key, $value)
1696 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1698 close $fh;
1700 return %config;
1703 # convert config value to boolean, 'true' or 'false'
1704 # no value, number > 0, 'true' and 'yes' values are true
1705 # rest of values are treated as false (never as error)
1706 sub config_to_bool {
1707 my $val = shift;
1709 # strip leading and trailing whitespace
1710 $val =~ s/^\s+//;
1711 $val =~ s/\s+$//;
1713 return (!defined $val || # section.key
1714 ($val =~ /^\d+$/ && $val) || # section.key = 1
1715 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1718 # convert config value to simple decimal number
1719 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1720 # to be multiplied by 1024, 1048576, or 1073741824
1721 sub config_to_int {
1722 my $val = shift;
1724 # strip leading and trailing whitespace
1725 $val =~ s/^\s+//;
1726 $val =~ s/\s+$//;
1728 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1729 $unit = lc($unit);
1730 # unknown unit is treated as 1
1731 return $num * ($unit eq 'g' ? 1073741824 :
1732 $unit eq 'm' ? 1048576 :
1733 $unit eq 'k' ? 1024 : 1);
1735 return $val;
1738 # convert config value to array reference, if needed
1739 sub config_to_multi {
1740 my $val = shift;
1742 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1745 sub git_get_project_config {
1746 my ($key, $type) = @_;
1748 # key sanity check
1749 return unless ($key);
1750 $key =~ s/^gitweb\.//;
1751 return if ($key =~ m/\W/);
1753 # type sanity check
1754 if (defined $type) {
1755 $type =~ s/^--//;
1756 $type = undef
1757 unless ($type eq 'bool' || $type eq 'int');
1760 # get config
1761 if (!defined $config_file ||
1762 $config_file ne "$git_dir/config") {
1763 %config = git_parse_project_config('gitweb');
1764 $config_file = "$git_dir/config";
1767 # ensure given type
1768 if (!defined $type) {
1769 return $config{"gitweb.$key"};
1770 } elsif ($type eq 'bool') {
1771 # backward compatibility: 'git config --bool' returns true/false
1772 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1773 } elsif ($type eq 'int') {
1774 return config_to_int($config{"gitweb.$key"});
1776 return $config{"gitweb.$key"};
1779 # get hash of given path at given ref
1780 sub git_get_hash_by_path {
1781 my $base = shift;
1782 my $path = shift || return undef;
1783 my $type = shift;
1785 $path =~ s,/+$,,;
1787 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1788 or die_error(500, "Open git-ls-tree failed");
1789 my $line = <$fd>;
1790 close $fd or return undef;
1792 if (!defined $line) {
1793 # there is no tree or hash given by $path at $base
1794 return undef;
1797 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1798 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1799 if (defined $type && $type ne $2) {
1800 # type doesn't match
1801 return undef;
1803 return $3;
1806 # get path of entry with given hash at given tree-ish (ref)
1807 # used to get 'from' filename for combined diff (merge commit) for renames
1808 sub git_get_path_by_hash {
1809 my $base = shift || return;
1810 my $hash = shift || return;
1812 local $/ = "\0";
1814 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1815 or return undef;
1816 while (my $line = <$fd>) {
1817 chomp $line;
1819 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1820 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1821 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1822 close $fd;
1823 return $1;
1826 close $fd;
1827 return undef;
1830 ## ......................................................................
1831 ## git utility functions, directly accessing git repository
1833 sub git_get_project_description {
1834 my $path = shift;
1836 $git_dir = "$projectroot/$path";
1837 open my $fd, "$git_dir/description"
1838 or return git_get_project_config('description');
1839 my $descr = <$fd>;
1840 close $fd;
1841 if (defined $descr) {
1842 chomp $descr;
1844 return $descr;
1847 sub git_get_project_ctags {
1848 my $path = shift;
1849 my $ctags = {};
1851 $git_dir = "$projectroot/$path";
1852 foreach (<$git_dir/ctags/*>) {
1853 open CT, $_ or next;
1854 my $val = <CT>;
1855 chomp $val;
1856 close CT;
1857 my $ctag = $_; $ctag =~ s#.*/##;
1858 $ctags->{$ctag} = $val;
1860 $ctags;
1863 sub git_populate_project_tagcloud {
1864 my $ctags = shift;
1866 # First, merge different-cased tags; tags vote on casing
1867 my %ctags_lc;
1868 foreach (keys %$ctags) {
1869 $ctags_lc{lc $_}->{count} += $ctags->{$_};
1870 if (not $ctags_lc{lc $_}->{topcount}
1871 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
1872 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
1873 $ctags_lc{lc $_}->{topname} = $_;
1877 my $cloud;
1878 if (eval { require HTML::TagCloud; 1; }) {
1879 $cloud = HTML::TagCloud->new;
1880 foreach (sort keys %ctags_lc) {
1881 # Pad the title with spaces so that the cloud looks
1882 # less crammed.
1883 my $title = $ctags_lc{$_}->{topname};
1884 $title =~ s/ /&nbsp;/g;
1885 $title =~ s/^/&nbsp;/g;
1886 $title =~ s/$/&nbsp;/g;
1887 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
1889 } else {
1890 $cloud = \%ctags_lc;
1892 $cloud;
1895 sub git_show_project_tagcloud {
1896 my ($cloud, $count) = @_;
1897 print STDERR ref($cloud)."..\n";
1898 if (ref $cloud eq 'HTML::TagCloud') {
1899 return $cloud->html_and_css($count);
1900 } else {
1901 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
1902 return '<p align="center">' . join (', ', map {
1903 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
1904 } splice(@tags, 0, $count)) . '</p>';
1908 sub git_get_project_url_list {
1909 my $path = shift;
1911 $git_dir = "$projectroot/$path";
1912 open my $fd, "$git_dir/cloneurl"
1913 or return wantarray ?
1914 @{ config_to_multi(git_get_project_config('url')) } :
1915 config_to_multi(git_get_project_config('url'));
1916 my @git_project_url_list = map { chomp; $_ } <$fd>;
1917 close $fd;
1919 return wantarray ? @git_project_url_list : \@git_project_url_list;
1922 sub git_get_projects_list {
1923 my ($filter) = @_;
1924 my @list;
1926 $filter ||= '';
1927 $filter =~ s/\.git$//;
1929 my ($check_forks) = gitweb_check_feature('forks');
1931 if (-d $projects_list) {
1932 # search in directory
1933 my $dir = $projects_list . ($filter ? "/$filter" : '');
1934 # remove the trailing "/"
1935 $dir =~ s!/+$!!;
1936 my $pfxlen = length("$dir");
1937 my $pfxdepth = ($dir =~ tr!/!!);
1939 File::Find::find({
1940 follow_fast => 1, # follow symbolic links
1941 follow_skip => 2, # ignore duplicates
1942 dangling_symlinks => 0, # ignore dangling symlinks, silently
1943 wanted => sub {
1944 # skip project-list toplevel, if we get it.
1945 return if (m!^[/.]$!);
1946 # only directories can be git repositories
1947 return unless (-d $_);
1948 # don't traverse too deep (Find is super slow on os x)
1949 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1950 $File::Find::prune = 1;
1951 return;
1954 my $subdir = substr($File::Find::name, $pfxlen + 1);
1955 # we check related file in $projectroot
1956 if (check_export_ok("$projectroot/$filter/$subdir")) {
1957 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1958 $File::Find::prune = 1;
1961 }, "$dir");
1963 } elsif (-f $projects_list) {
1964 # read from file(url-encoded):
1965 # 'git%2Fgit.git Linus+Torvalds'
1966 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1967 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1968 my %paths;
1969 open my ($fd), $projects_list or return;
1970 PROJECT:
1971 while (my $line = <$fd>) {
1972 chomp $line;
1973 my ($path, $owner) = split ' ', $line;
1974 $path = unescape($path);
1975 $owner = unescape($owner);
1976 if (!defined $path) {
1977 next;
1979 if ($filter ne '') {
1980 # looking for forks;
1981 my $pfx = substr($path, 0, length($filter));
1982 if ($pfx ne $filter) {
1983 next PROJECT;
1985 my $sfx = substr($path, length($filter));
1986 if ($sfx !~ /^\/.*\.git$/) {
1987 next PROJECT;
1989 } elsif ($check_forks) {
1990 PATH:
1991 foreach my $filter (keys %paths) {
1992 # looking for forks;
1993 my $pfx = substr($path, 0, length($filter));
1994 if ($pfx ne $filter) {
1995 next PATH;
1997 my $sfx = substr($path, length($filter));
1998 if ($sfx !~ /^\/.*\.git$/) {
1999 next PATH;
2001 # is a fork, don't include it in
2002 # the list
2003 next PROJECT;
2006 if (check_export_ok("$projectroot/$path")) {
2007 my $pr = {
2008 path => $path,
2009 owner => to_utf8($owner),
2011 push @list, $pr;
2012 (my $forks_path = $path) =~ s/\.git$//;
2013 $paths{$forks_path}++;
2016 close $fd;
2018 return @list;
2021 our $gitweb_project_owner = undef;
2022 sub git_get_project_list_from_file {
2024 return if (defined $gitweb_project_owner);
2026 $gitweb_project_owner = {};
2027 # read from file (url-encoded):
2028 # 'git%2Fgit.git Linus+Torvalds'
2029 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2030 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2031 if (-f $projects_list) {
2032 open (my $fd , $projects_list);
2033 while (my $line = <$fd>) {
2034 chomp $line;
2035 my ($pr, $ow) = split ' ', $line;
2036 $pr = unescape($pr);
2037 $ow = unescape($ow);
2038 $gitweb_project_owner->{$pr} = to_utf8($ow);
2040 close $fd;
2044 sub git_get_project_owner {
2045 my $project = shift;
2046 my $owner;
2048 return undef unless $project;
2049 $git_dir = "$projectroot/$project";
2051 if (!defined $gitweb_project_owner) {
2052 git_get_project_list_from_file();
2055 if (exists $gitweb_project_owner->{$project}) {
2056 $owner = $gitweb_project_owner->{$project};
2058 if (!defined $owner){
2059 $owner = git_get_project_config('owner');
2061 if (!defined $owner) {
2062 $owner = get_file_owner("$git_dir");
2065 return $owner;
2068 sub git_get_last_activity {
2069 my ($path) = @_;
2070 my $fd;
2072 $git_dir = "$projectroot/$path";
2073 open($fd, "-|", git_cmd(), 'for-each-ref',
2074 '--format=%(committer)',
2075 '--sort=-committerdate',
2076 '--count=1',
2077 'refs/heads') or return;
2078 my $most_recent = <$fd>;
2079 close $fd or return;
2080 if (defined $most_recent &&
2081 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2082 my $timestamp = $1;
2083 my $age = time - $timestamp;
2084 return ($age, age_string($age));
2086 return (undef, undef);
2089 sub git_get_references {
2090 my $type = shift || "";
2091 my %refs;
2092 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2093 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2094 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2095 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2096 or return;
2098 while (my $line = <$fd>) {
2099 chomp $line;
2100 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2101 if (defined $refs{$1}) {
2102 push @{$refs{$1}}, $2;
2103 } else {
2104 $refs{$1} = [ $2 ];
2108 close $fd or return;
2109 return \%refs;
2112 sub git_get_rev_name_tags {
2113 my $hash = shift || return undef;
2115 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2116 or return;
2117 my $name_rev = <$fd>;
2118 close $fd;
2120 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2121 return $1;
2122 } else {
2123 # catches also '$hash undefined' output
2124 return undef;
2128 ## ----------------------------------------------------------------------
2129 ## parse to hash functions
2131 sub parse_date {
2132 my $epoch = shift;
2133 my $tz = shift || "-0000";
2135 my %date;
2136 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2137 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2138 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2139 $date{'hour'} = $hour;
2140 $date{'minute'} = $min;
2141 $date{'mday'} = $mday;
2142 $date{'day'} = $days[$wday];
2143 $date{'month'} = $months[$mon];
2144 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2145 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2146 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2147 $mday, $months[$mon], $hour ,$min;
2148 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2149 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2151 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2152 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2153 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2154 $date{'hour_local'} = $hour;
2155 $date{'minute_local'} = $min;
2156 $date{'tz_local'} = $tz;
2157 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2158 1900+$year, $mon+1, $mday,
2159 $hour, $min, $sec, $tz);
2160 return %date;
2163 sub parse_tag {
2164 my $tag_id = shift;
2165 my %tag;
2166 my @comment;
2168 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2169 $tag{'id'} = $tag_id;
2170 while (my $line = <$fd>) {
2171 chomp $line;
2172 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2173 $tag{'object'} = $1;
2174 } elsif ($line =~ m/^type (.+)$/) {
2175 $tag{'type'} = $1;
2176 } elsif ($line =~ m/^tag (.+)$/) {
2177 $tag{'name'} = $1;
2178 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2179 $tag{'author'} = $1;
2180 $tag{'epoch'} = $2;
2181 $tag{'tz'} = $3;
2182 } elsif ($line =~ m/--BEGIN/) {
2183 push @comment, $line;
2184 last;
2185 } elsif ($line eq "") {
2186 last;
2189 push @comment, <$fd>;
2190 $tag{'comment'} = \@comment;
2191 close $fd or return;
2192 if (!defined $tag{'name'}) {
2193 return
2195 return %tag
2198 sub parse_commit_text {
2199 my ($commit_text, $withparents) = @_;
2200 my @commit_lines = split '\n', $commit_text;
2201 my %co;
2203 pop @commit_lines; # Remove '\0'
2205 if (! @commit_lines) {
2206 return;
2209 my $header = shift @commit_lines;
2210 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2211 return;
2213 ($co{'id'}, my @parents) = split ' ', $header;
2214 while (my $line = shift @commit_lines) {
2215 last if $line eq "\n";
2216 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2217 $co{'tree'} = $1;
2218 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2219 push @parents, $1;
2220 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2221 $co{'author'} = $1;
2222 $co{'author_epoch'} = $2;
2223 $co{'author_tz'} = $3;
2224 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2225 $co{'author_name'} = $1;
2226 $co{'author_email'} = $2;
2227 } else {
2228 $co{'author_name'} = $co{'author'};
2230 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2231 $co{'committer'} = $1;
2232 $co{'committer_epoch'} = $2;
2233 $co{'committer_tz'} = $3;
2234 $co{'committer_name'} = $co{'committer'};
2235 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2236 $co{'committer_name'} = $1;
2237 $co{'committer_email'} = $2;
2238 } else {
2239 $co{'committer_name'} = $co{'committer'};
2243 if (!defined $co{'tree'}) {
2244 return;
2246 $co{'parents'} = \@parents;
2247 $co{'parent'} = $parents[0];
2249 foreach my $title (@commit_lines) {
2250 $title =~ s/^ //;
2251 if ($title ne "") {
2252 $co{'title'} = chop_str($title, 80, 5);
2253 # remove leading stuff of merges to make the interesting part visible
2254 if (length($title) > 50) {
2255 $title =~ s/^Automatic //;
2256 $title =~ s/^merge (of|with) /Merge ... /i;
2257 if (length($title) > 50) {
2258 $title =~ s/(http|rsync):\/\///;
2260 if (length($title) > 50) {
2261 $title =~ s/(master|www|rsync)\.//;
2263 if (length($title) > 50) {
2264 $title =~ s/kernel.org:?//;
2266 if (length($title) > 50) {
2267 $title =~ s/\/pub\/scm//;
2270 $co{'title_short'} = chop_str($title, 50, 5);
2271 last;
2274 if (! defined $co{'title'} || $co{'title'} eq "") {
2275 $co{'title'} = $co{'title_short'} = '(no commit message)';
2277 # remove added spaces
2278 foreach my $line (@commit_lines) {
2279 $line =~ s/^ //;
2281 $co{'comment'} = \@commit_lines;
2283 my $age = time - $co{'committer_epoch'};
2284 $co{'age'} = $age;
2285 $co{'age_string'} = age_string($age);
2286 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2287 if ($age > 60*60*24*7*2) {
2288 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2289 $co{'age_string_age'} = $co{'age_string'};
2290 } else {
2291 $co{'age_string_date'} = $co{'age_string'};
2292 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2294 return %co;
2297 sub parse_commit {
2298 my ($commit_id) = @_;
2299 my %co;
2301 local $/ = "\0";
2303 open my $fd, "-|", git_cmd(), "rev-list",
2304 "--parents",
2305 "--header",
2306 "--max-count=1",
2307 $commit_id,
2308 "--",
2309 or die_error(500, "Open git-rev-list failed");
2310 %co = parse_commit_text(<$fd>, 1);
2311 close $fd;
2313 return %co;
2316 sub parse_commits {
2317 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2318 my @cos;
2320 $maxcount ||= 1;
2321 $skip ||= 0;
2323 local $/ = "\0";
2325 open my $fd, "-|", git_cmd(), "rev-list",
2326 "--header",
2327 @args,
2328 ("--max-count=" . $maxcount),
2329 ("--skip=" . $skip),
2330 @extra_options,
2331 $commit_id,
2332 "--",
2333 ($filename ? ($filename) : ())
2334 or die_error(500, "Open git-rev-list failed");
2335 while (my $line = <$fd>) {
2336 my %co = parse_commit_text($line);
2337 push @cos, \%co;
2339 close $fd;
2341 return wantarray ? @cos : \@cos;
2344 # parse line of git-diff-tree "raw" output
2345 sub parse_difftree_raw_line {
2346 my $line = shift;
2347 my %res;
2349 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2350 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2351 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2352 $res{'from_mode'} = $1;
2353 $res{'to_mode'} = $2;
2354 $res{'from_id'} = $3;
2355 $res{'to_id'} = $4;
2356 $res{'status'} = $5;
2357 $res{'similarity'} = $6;
2358 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2359 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2360 } else {
2361 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2364 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2365 # combined diff (for merge commit)
2366 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2367 $res{'nparents'} = length($1);
2368 $res{'from_mode'} = [ split(' ', $2) ];
2369 $res{'to_mode'} = pop @{$res{'from_mode'}};
2370 $res{'from_id'} = [ split(' ', $3) ];
2371 $res{'to_id'} = pop @{$res{'from_id'}};
2372 $res{'status'} = [ split('', $4) ];
2373 $res{'to_file'} = unquote($5);
2375 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2376 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2377 $res{'commit'} = $1;
2380 return wantarray ? %res : \%res;
2383 # wrapper: return parsed line of git-diff-tree "raw" output
2384 # (the argument might be raw line, or parsed info)
2385 sub parsed_difftree_line {
2386 my $line_or_ref = shift;
2388 if (ref($line_or_ref) eq "HASH") {
2389 # pre-parsed (or generated by hand)
2390 return $line_or_ref;
2391 } else {
2392 return parse_difftree_raw_line($line_or_ref);
2396 # parse line of git-ls-tree output
2397 sub parse_ls_tree_line ($;%) {
2398 my $line = shift;
2399 my %opts = @_;
2400 my %res;
2402 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2403 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2405 $res{'mode'} = $1;
2406 $res{'type'} = $2;
2407 $res{'hash'} = $3;
2408 if ($opts{'-z'}) {
2409 $res{'name'} = $4;
2410 } else {
2411 $res{'name'} = unquote($4);
2414 return wantarray ? %res : \%res;
2417 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2418 sub parse_from_to_diffinfo {
2419 my ($diffinfo, $from, $to, @parents) = @_;
2421 if ($diffinfo->{'nparents'}) {
2422 # combined diff
2423 $from->{'file'} = [];
2424 $from->{'href'} = [];
2425 fill_from_file_info($diffinfo, @parents)
2426 unless exists $diffinfo->{'from_file'};
2427 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2428 $from->{'file'}[$i] =
2429 defined $diffinfo->{'from_file'}[$i] ?
2430 $diffinfo->{'from_file'}[$i] :
2431 $diffinfo->{'to_file'};
2432 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2433 $from->{'href'}[$i] = href(action=>"blob",
2434 hash_base=>$parents[$i],
2435 hash=>$diffinfo->{'from_id'}[$i],
2436 file_name=>$diffinfo->{'from_prefix'}.$from->{'file'}[$i]);
2437 } else {
2438 $from->{'href'}[$i] = undef;
2441 } else {
2442 # ordinary (not combined) diff
2443 $from->{'file'} = $diffinfo->{'from_file'};
2444 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2445 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2446 hash=>$diffinfo->{'from_id'},
2447 file_name=>$diffinfo->{'from_prefix'}.$from->{'file'});
2448 } else {
2449 delete $from->{'href'};
2453 $to->{'file'} = $diffinfo->{'to_file'};
2454 if (!is_deleted($diffinfo)) { # file exists in result
2455 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2456 hash=>$diffinfo->{'to_id'},
2457 file_name=>$diffinfo->{'to_prefix'}.$to->{'file'});
2458 } else {
2459 delete $to->{'href'};
2463 ## ......................................................................
2464 ## parse to array of hashes functions
2466 sub git_get_heads_list {
2467 my $limit = shift;
2468 my @headslist;
2470 open my $fd, '-|', git_cmd(), 'for-each-ref',
2471 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2472 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2473 'refs/heads'
2474 or return;
2475 while (my $line = <$fd>) {
2476 my %ref_item;
2478 chomp $line;
2479 my ($refinfo, $committerinfo) = split(/\0/, $line);
2480 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2481 my ($committer, $epoch, $tz) =
2482 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2483 $ref_item{'fullname'} = $name;
2484 $name =~ s!^refs/heads/!!;
2486 $ref_item{'name'} = $name;
2487 $ref_item{'id'} = $hash;
2488 $ref_item{'title'} = $title || '(no commit message)';
2489 $ref_item{'epoch'} = $epoch;
2490 if ($epoch) {
2491 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2492 } else {
2493 $ref_item{'age'} = "unknown";
2496 push @headslist, \%ref_item;
2498 close $fd;
2500 return wantarray ? @headslist : \@headslist;
2503 sub git_get_tags_list {
2504 my $limit = shift;
2505 my @tagslist;
2507 open my $fd, '-|', git_cmd(), 'for-each-ref',
2508 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2509 '--format=%(objectname) %(objecttype) %(refname) '.
2510 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2511 'refs/tags'
2512 or return;
2513 while (my $line = <$fd>) {
2514 my %ref_item;
2516 chomp $line;
2517 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2518 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2519 my ($creator, $epoch, $tz) =
2520 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2521 $ref_item{'fullname'} = $name;
2522 $name =~ s!^refs/tags/!!;
2524 $ref_item{'type'} = $type;
2525 $ref_item{'id'} = $id;
2526 $ref_item{'name'} = $name;
2527 if ($type eq "tag") {
2528 $ref_item{'subject'} = $title;
2529 $ref_item{'reftype'} = $reftype;
2530 $ref_item{'refid'} = $refid;
2531 } else {
2532 $ref_item{'reftype'} = $type;
2533 $ref_item{'refid'} = $id;
2536 if ($type eq "tag" || $type eq "commit") {
2537 $ref_item{'epoch'} = $epoch;
2538 if ($epoch) {
2539 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2540 } else {
2541 $ref_item{'age'} = "unknown";
2545 push @tagslist, \%ref_item;
2547 close $fd;
2549 return wantarray ? @tagslist : \@tagslist;
2552 ## ----------------------------------------------------------------------
2553 ## filesystem-related functions
2555 sub get_file_owner {
2556 my $path = shift;
2558 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2559 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2560 if (!defined $gcos) {
2561 return undef;
2563 my $owner = $gcos;
2564 $owner =~ s/[,;].*$//;
2565 return to_utf8($owner);
2568 ## ......................................................................
2569 ## mimetype related functions
2571 sub mimetype_guess_file {
2572 my $filename = shift;
2573 my $mimemap = shift;
2574 -r $mimemap or return undef;
2576 my %mimemap;
2577 open(MIME, $mimemap) or return undef;
2578 while (<MIME>) {
2579 next if m/^#/; # skip comments
2580 my ($mime, $exts) = split(/\t+/);
2581 if (defined $exts) {
2582 my @exts = split(/\s+/, $exts);
2583 foreach my $ext (@exts) {
2584 $mimemap{$ext} = $mime;
2588 close(MIME);
2590 $filename =~ /\.([^.]*)$/;
2591 return $mimemap{$1};
2594 sub mimetype_guess {
2595 my $filename = shift;
2596 my $mime;
2597 $filename =~ /\./ or return undef;
2599 if ($mimetypes_file) {
2600 my $file = $mimetypes_file;
2601 if ($file !~ m!^/!) { # if it is relative path
2602 # it is relative to project
2603 $file = "$projectroot/$project/$file";
2605 $mime = mimetype_guess_file($filename, $file);
2607 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2608 return $mime;
2611 sub blob_mimetype {
2612 my $fd = shift;
2613 my $filename = shift;
2615 if ($filename) {
2616 my $mime = mimetype_guess($filename);
2617 $mime and return $mime;
2620 # just in case
2621 return $default_blob_plain_mimetype unless $fd;
2623 if (-T $fd) {
2624 return 'text/plain';
2625 } elsif (! $filename) {
2626 return 'application/octet-stream';
2627 } elsif ($filename =~ m/\.png$/i) {
2628 return 'image/png';
2629 } elsif ($filename =~ m/\.gif$/i) {
2630 return 'image/gif';
2631 } elsif ($filename =~ m/\.jpe?g$/i) {
2632 return 'image/jpeg';
2633 } else {
2634 return 'application/octet-stream';
2638 sub blob_contenttype {
2639 my ($fd, $file_name, $type) = @_;
2641 $type ||= blob_mimetype($fd, $file_name);
2642 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2643 $type .= "; charset=$default_text_plain_charset";
2646 return $type;
2649 ## ======================================================================
2650 ## functions printing HTML: header, footer, error page
2652 sub git_header_html {
2653 my $status = shift || "200 OK";
2654 my $expires = shift;
2656 my $title = "$site_name";
2657 if (defined $project) {
2658 $title .= " - " . to_utf8($project);
2659 if (defined $action) {
2660 $title .= "/$action";
2661 if (defined $file_name) {
2662 $title .= " - " . esc_path($file_name);
2663 if ($action eq "tree" && $file_name !~ m|/$|) {
2664 $title .= "/";
2669 # We do not ever emit application/xhtml+xml since that gives us
2670 # no benefits and it makes many browsers (e.g. Firefox) exceedingly
2671 # strict, which is troublesome for example when showing user-supplied
2672 # README.html files.
2673 my $content_type = 'text/html';
2674 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2675 -status=> $status, -expires => $expires);
2676 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2677 print <<EOF;
2678 <?xml version="1.0" encoding="utf-8"?>
2679 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2680 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2681 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2682 <!-- git core binaries version $git_version -->
2683 <head>
2684 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2685 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2686 <meta name="robots" content="index, nofollow"/>
2687 <title>$title</title>
2688 <script type="text/javascript">/* <![CDATA[ */
2689 function fixBlameLinks() {
2690 var allLinks = document.getElementsByTagName("a");
2691 for (var i = 0; i < allLinks.length; i++) {
2692 var link = allLinks.item(i);
2693 if (link.className == 'blamelink')
2694 link.href = link.href.replace("a=blame", "a=blame_incremental");
2697 /* ]]> */</script>
2699 # print out each stylesheet that exist
2700 if (defined $stylesheet) {
2701 #provides backwards capability for those people who define style sheet in a config file
2702 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2703 } else {
2704 foreach my $stylesheet (@stylesheets) {
2705 next unless $stylesheet;
2706 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2709 if (defined $project) {
2710 my %href_params = get_feed_info();
2711 if (!exists $href_params{'-title'}) {
2712 $href_params{'-title'} = 'log';
2715 foreach my $format qw(RSS Atom) {
2716 my $type = lc($format);
2717 my %link_attr = (
2718 '-rel' => 'alternate',
2719 '-title' => "$project - $href_params{'-title'} - $format feed",
2720 '-type' => "application/$type+xml"
2723 $href_params{'action'} = $type;
2724 $link_attr{'-href'} = href(%href_params);
2725 print "<link ".
2726 "rel=\"$link_attr{'-rel'}\" ".
2727 "title=\"$link_attr{'-title'}\" ".
2728 "href=\"$link_attr{'-href'}\" ".
2729 "type=\"$link_attr{'-type'}\" ".
2730 "/>\n";
2732 $href_params{'extra_options'} = '--no-merges';
2733 $link_attr{'-href'} = href(%href_params);
2734 $link_attr{'-title'} .= ' (no merges)';
2735 print "<link ".
2736 "rel=\"$link_attr{'-rel'}\" ".
2737 "title=\"$link_attr{'-title'}\" ".
2738 "href=\"$link_attr{'-href'}\" ".
2739 "type=\"$link_attr{'-type'}\" ".
2740 "/>\n";
2743 } else {
2744 printf('<link rel="alternate" title="%s projects list" '.
2745 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2746 $site_name, href(project=>undef, action=>"project_index"));
2747 printf('<link rel="alternate" title="%s projects feeds" '.
2748 'href="%s" type="text/x-opml" />'."\n",
2749 $site_name, href(project=>undef, action=>"opml"));
2751 if (defined $favicon) {
2752 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2755 if (defined $gitwebjs) {
2756 print qq(<script src="$gitwebjs" type="text/javascript"></script>\n);
2759 print "</head>\n" .
2760 "<body onload=\"GitAddLinks(); fixBlameLinks();\">\n";
2762 if (-f $site_header) {
2763 open (my $fd, $site_header);
2764 print <$fd>;
2765 close $fd;
2768 print "<div class=\"page_header\">\n" .
2769 $cgi->a({-href => esc_url($logo_url),
2770 -title => $logo_label},
2771 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2772 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2773 if (defined $project) {
2774 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2775 if (defined $action) {
2776 print " / $action";
2778 print "\n";
2780 print "</div>\n";
2782 my ($have_search) = gitweb_check_feature('search');
2783 if (defined $project && $have_search) {
2784 if (!defined $searchtext) {
2785 $searchtext = "";
2787 my $search_hash;
2788 if (defined $hash_base) {
2789 $search_hash = $hash_base;
2790 } elsif (defined $hash) {
2791 $search_hash = $hash;
2792 } else {
2793 $search_hash = "HEAD";
2795 my $action = $my_uri;
2796 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2797 if ($use_pathinfo) {
2798 $action .= "/".esc_url($project);
2800 print $cgi->startform(-method => "get", -action => $action) .
2801 "<div class=\"search\">\n" .
2802 (!$use_pathinfo &&
2803 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2804 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2805 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2806 $cgi->popup_menu(-name => 'st', -default => 'commit',
2807 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2808 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2809 " search:\n",
2810 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2811 "<span title=\"Extended regular expression\">" .
2812 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
2813 -checked => $search_use_regexp) .
2814 "</span>" .
2815 "</div>" .
2816 $cgi->end_form() . "\n";
2820 sub git_footer_html {
2821 my $feed_class = 'rss_logo';
2823 print "<div class=\"page_footer\">\n";
2824 if (defined $project) {
2825 my $descr = git_get_project_description($project);
2826 if (defined $descr) {
2827 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2830 my %href_params = get_feed_info();
2831 if (!%href_params) {
2832 $feed_class .= ' generic';
2834 $href_params{'-title'} ||= 'log';
2836 foreach my $format qw(RSS Atom) {
2837 $href_params{'action'} = lc($format);
2838 print $cgi->a({-href => href(%href_params),
2839 -title => "$href_params{'-title'} $format feed",
2840 -class => $feed_class}, $format)."\n";
2843 } else {
2844 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2845 -class => $feed_class}, "OPML") . " ";
2846 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2847 -class => $feed_class}, "TXT") . "\n";
2849 print "</div>\n"; # class="page_footer"
2851 if (-f $site_footer) {
2852 open (my $fd, $site_footer);
2853 print <$fd>;
2854 close $fd;
2857 print "</body>\n" .
2858 "</html>";
2861 # die_error(<http_status_code>, <error_message>)
2862 # Example: die_error(404, 'Hash not found')
2863 # By convention, use the following status codes (as defined in RFC 2616):
2864 # 400: Invalid or missing CGI parameters, or
2865 # requested object exists but has wrong type.
2866 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
2867 # this server or project.
2868 # 404: Requested object/revision/project doesn't exist.
2869 # 500: The server isn't configured properly, or
2870 # an internal error occurred (e.g. failed assertions caused by bugs), or
2871 # an unknown error occurred (e.g. the git binary died unexpectedly).
2872 sub die_error {
2873 my $status = shift || 500;
2874 my $error = shift || "Internal server error";
2876 my %http_responses = (400 => '400 Bad Request',
2877 403 => '403 Forbidden',
2878 404 => '404 Not Found',
2879 500 => '500 Internal Server Error');
2880 git_header_html($http_responses{$status});
2881 print <<EOF;
2882 <div class="page_body">
2883 <br /><br />
2884 $status - $error
2885 <br />
2886 </div>
2888 git_footer_html();
2889 exit;
2892 ## ----------------------------------------------------------------------
2893 ## functions printing or outputting HTML: navigation
2895 sub git_print_page_nav {
2896 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2897 $extra = '' if !defined $extra; # pager or formats
2899 my @navs = qw(summary log commit commitdiff tree);
2900 if ($suppress) {
2901 @navs = grep { $_ ne $suppress } @navs;
2904 my %arg = map { $_ => {action=>$_} } @navs;
2905 if (defined $head) {
2906 for (qw(commit commitdiff)) {
2907 $arg{$_}{'hash'} = $head;
2909 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2910 $arg{'log'}{'hash'} = $head;
2914 $arg{'log'}{'action'} = 'shortlog';
2915 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2916 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2918 my @actions = gitweb_check_feature('actions');
2919 while (@actions) {
2920 my ($label, $link, $pos) = (shift(@actions), shift(@actions), shift(@actions));
2921 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
2922 # munch munch
2923 $link =~ s#%n#$project#g;
2924 $link =~ s#%f#$git_dir#g;
2925 $treehead ? $link =~ s#%h#$treehead#g : $link =~ s#%h##g;
2926 $treebase ? $link =~ s#%b#$treebase#g : $link =~ s#%b##g;
2927 $arg{$label}{'_href'} = $link;
2930 print "<div class=\"page_nav\">\n" .
2931 (join " | ",
2932 map { $_ eq $current ?
2933 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
2934 } @navs);
2935 print "<br/>\n$extra<br/>\n" .
2936 "</div>\n";
2939 sub format_paging_nav {
2940 my ($action, $hash, $head, $page, $has_next_link) = @_;
2941 my $paging_nav;
2944 if ($hash ne $head || $page) {
2945 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2946 } else {
2947 $paging_nav .= "HEAD";
2950 if ($page > 0) {
2951 $paging_nav .= " &sdot; " .
2952 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2953 -accesskey => "p", -title => "Alt-p"}, "prev");
2954 } else {
2955 $paging_nav .= " &sdot; prev";
2958 if ($has_next_link) {
2959 $paging_nav .= " &sdot; " .
2960 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2961 -accesskey => "n", -title => "Alt-n"}, "next");
2962 } else {
2963 $paging_nav .= " &sdot; next";
2966 return $paging_nav;
2969 sub format_log_nav {
2970 my ($action, $hash, $head, $page, $has_next_link) = @_;
2971 my $paging_nav;
2973 if ($action eq 'shortlog') {
2974 $paging_nav .= 'shortlog';
2975 } else {
2976 $paging_nav .= $cgi->a({-href => href(action=>'shortlog', -replay=>1)}, 'shortlog');
2978 $paging_nav .= ' | ';
2979 if ($action eq 'log') {
2980 $paging_nav .= 'fulllog';
2981 } else {
2982 $paging_nav .= $cgi->a({-href => href(action=>'log', -replay=>1)}, 'fulllog');
2985 $paging_nav .= " | " . format_paging_nav($action, $hash, $head, $page, $has_next_link);
2986 return $paging_nav;
2989 ## ......................................................................
2990 ## functions printing or outputting HTML: div
2992 sub git_print_header_div {
2993 my ($action, $title, $hash, $hash_base) = @_;
2994 my %args = ();
2996 $args{'action'} = $action;
2997 $args{'hash'} = $hash if $hash;
2998 $args{'hash_base'} = $hash_base if $hash_base;
3000 print "<div class=\"header\">\n" .
3001 $cgi->a({-href => href(%args), -class => "title"},
3002 $title ? $title : $action) .
3003 "\n</div>\n";
3006 #sub git_print_authorship (\%) {
3007 sub git_print_authorship {
3008 my $co = shift;
3010 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3011 print "<div class=\"author_date\">" .
3012 esc_html($co->{'author_name'}) .
3013 " [$ad{'rfc2822'}";
3014 if ($ad{'hour_local'} < 6) {
3015 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3016 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3017 } else {
3018 printf(" (%02d:%02d %s)",
3019 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3021 print "]</div>\n";
3024 sub git_print_page_path {
3025 my $name = shift;
3026 my $type = shift;
3027 my $hb = shift;
3030 print "<div class=\"page_path\">";
3031 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3032 -title => 'tree root'}, to_utf8("[$project]"));
3033 print " / ";
3034 if (defined $name) {
3035 my @dirname = split '/', $name;
3036 my $basename = pop @dirname;
3037 my $fullname = '';
3039 foreach my $dir (@dirname) {
3040 $fullname .= ($fullname ? '/' : '') . $dir;
3041 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3042 hash_base=>$hb),
3043 -title => $fullname}, esc_path($dir));
3044 print " / ";
3046 if (defined $type && $type eq 'blob') {
3047 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3048 hash_base=>$hb),
3049 -title => $name}, esc_path($basename));
3050 } elsif (defined $type && $type eq 'tree') {
3051 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3052 hash_base=>$hb),
3053 -title => $name}, esc_path($basename));
3054 print " / ";
3055 } else {
3056 print esc_path($basename);
3059 print "<br/></div>\n";
3062 # sub git_print_log (\@;%) {
3063 sub git_print_log ($;%) {
3064 my $log = shift;
3065 my %opts = @_;
3067 if ($opts{'-remove_title'}) {
3068 # remove title, i.e. first line of log
3069 shift @$log;
3071 # remove leading empty lines
3072 while (defined $log->[0] && $log->[0] eq "") {
3073 shift @$log;
3076 # print log
3077 my $signoff = 0;
3078 my $empty = 0;
3079 foreach my $line (@$log) {
3080 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3081 $signoff = 1;
3082 $empty = 0;
3083 if (! $opts{'-remove_signoff'}) {
3084 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3085 next;
3086 } else {
3087 # remove signoff lines
3088 next;
3090 } else {
3091 $signoff = 0;
3094 # print only one empty line
3095 # do not print empty line after signoff
3096 if ($line eq "") {
3097 next if ($empty || $signoff);
3098 $empty = 1;
3099 } else {
3100 $empty = 0;
3103 print format_log_line_html($line) . "<br/>\n";
3106 if ($opts{'-final_empty_line'}) {
3107 # end with single empty line
3108 print "<br/>\n" unless $empty;
3112 # return link target (what link points to)
3113 sub git_get_link_target {
3114 my $hash = shift;
3115 my $link_target;
3117 # read link
3118 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3119 or return;
3121 local $/;
3122 $link_target = <$fd>;
3124 close $fd
3125 or return;
3127 return $link_target;
3130 # given link target, and the directory (basedir) the link is in,
3131 # return target of link relative to top directory (top tree);
3132 # return undef if it is not possible (including absolute links).
3133 sub normalize_link_target {
3134 my ($link_target, $basedir, $hash_base) = @_;
3136 # we can normalize symlink target only if $hash_base is provided
3137 return unless $hash_base;
3139 # absolute symlinks (beginning with '/') cannot be normalized
3140 return if (substr($link_target, 0, 1) eq '/');
3142 # normalize link target to path from top (root) tree (dir)
3143 my $path;
3144 if ($basedir) {
3145 $path = $basedir . '/' . $link_target;
3146 } else {
3147 # we are in top (root) tree (dir)
3148 $path = $link_target;
3151 # remove //, /./, and /../
3152 my @path_parts;
3153 foreach my $part (split('/', $path)) {
3154 # discard '.' and ''
3155 next if (!$part || $part eq '.');
3156 # handle '..'
3157 if ($part eq '..') {
3158 if (@path_parts) {
3159 pop @path_parts;
3160 } else {
3161 # link leads outside repository (outside top dir)
3162 return;
3164 } else {
3165 push @path_parts, $part;
3168 $path = join('/', @path_parts);
3170 return $path;
3173 # print tree entry (row of git_tree), but without encompassing <tr> element
3174 sub git_print_tree_entry {
3175 my ($t, $basedir, $hash_base, $have_blame) = @_;
3177 my %base_key = ();
3178 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3180 # The format of a table row is: mode list link. Where mode is
3181 # the mode of the entry, list is the name of the entry, an href,
3182 # and link is the action links of the entry.
3184 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3185 if ($t->{'type'} eq "blob") {
3186 print "<td class=\"list\">" .
3187 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3188 file_name=>"$basedir$t->{'name'}", %base_key),
3189 -class => "list"}, esc_path($t->{'name'}));
3190 if (S_ISLNK(oct $t->{'mode'})) {
3191 my $link_target = git_get_link_target($t->{'hash'});
3192 if ($link_target) {
3193 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3194 if (defined $norm_target) {
3195 print " -> " .
3196 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3197 file_name=>$norm_target),
3198 -title => $norm_target}, esc_path($link_target));
3199 } else {
3200 print " -> " . esc_path($link_target);
3204 print "</td>\n";
3205 print "<td class=\"link\">";
3206 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3207 file_name=>"$basedir$t->{'name'}", %base_key)},
3208 "blob");
3209 if ($have_blame) {
3210 print " | " .
3211 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3212 file_name=>"$basedir$t->{'name'}", %base_key), -class => "blamelink"},
3213 "blame");
3215 if (defined $hash_base) {
3216 print " | " .
3217 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3218 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3219 "history");
3221 print " | " .
3222 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3223 file_name=>"$basedir$t->{'name'}")},
3224 "raw");
3225 print "</td>\n";
3227 } elsif ($t->{'type'} eq "tree") {
3228 print "<td class=\"list\">";
3229 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3230 file_name=>"$basedir$t->{'name'}", %base_key)},
3231 esc_path($t->{'name'}));
3232 print "</td>\n";
3233 print "<td class=\"link\">";
3234 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3235 file_name=>"$basedir$t->{'name'}", %base_key)},
3236 "tree");
3237 if (defined $hash_base) {
3238 print " | " .
3239 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3240 file_name=>"$basedir$t->{'name'}")},
3241 "history");
3243 print "</td>\n";
3244 } else {
3245 # unknown object: we can only present history for it
3246 # (this includes 'commit' object, i.e. submodule support)
3247 print "<td class=\"list\">" .
3248 esc_path($t->{'name'}) .
3249 "</td>\n";
3250 print "<td class=\"link\">";
3251 if (defined $hash_base) {
3252 print $cgi->a({-href => href(action=>"history",
3253 hash_base=>$hash_base,
3254 file_name=>"$basedir$t->{'name'}")},
3255 "history");
3257 print "</td>\n";
3261 ## ......................................................................
3262 ## functions printing large fragments of HTML
3264 # get pre-image filenames for merge (combined) diff
3265 sub fill_from_file_info {
3266 my ($diff, @parents) = @_;
3268 $diff->{'from_file'} = [ ];
3269 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3270 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3271 if ($diff->{'status'}[$i] eq 'R' ||
3272 $diff->{'status'}[$i] eq 'C') {
3273 $diff->{'from_file'}[$i] =
3274 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3278 return $diff;
3281 # is current raw difftree line of file deletion
3282 sub is_deleted {
3283 my $diffinfo = shift;
3285 return $diffinfo->{'to_id'} eq ('0' x 40);
3288 # does patch correspond to [previous] difftree raw line
3289 # $diffinfo - hashref of parsed raw diff format
3290 # $patchinfo - hashref of parsed patch diff format
3291 # (the same keys as in $diffinfo)
3292 sub is_patch_split {
3293 my ($diffinfo, $patchinfo) = @_;
3295 return defined $diffinfo && defined $patchinfo
3296 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3300 sub git_difftree_body {
3301 my ($difftree, $from_prefix, $to_prefix, $hash, @parents) = @_;
3302 my ($parent) = $parents[0];
3303 my ($have_blame) = gitweb_check_feature('blame');
3305 $from_prefix = !defined $from_prefix ? '' : $from_prefix.'/';
3306 $to_prefix = !defined $to_prefix ? '' : $to_prefix . '/';
3308 print "<div class=\"list_head\">\n";
3309 if ($#{$difftree} > 10) {
3310 print(($#{$difftree} + 1) . " files changed:\n");
3312 print "</div>\n";
3314 print "<table class=\"" .
3315 (@parents > 1 ? "combined " : "") .
3316 "diff_tree\">\n";
3318 # header only for combined diff in 'commitdiff' view
3319 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3320 if ($has_header) {
3321 # table header
3322 print "<thead><tr>\n" .
3323 "<th></th><th></th>\n"; # filename, patchN link
3324 for (my $i = 0; $i < @parents; $i++) {
3325 my $par = $parents[$i];
3326 print "<th>" .
3327 $cgi->a({-href => href(action=>"commitdiff",
3328 hash=>$hash, hash_parent=>$par),
3329 -title => 'commitdiff to parent number ' .
3330 ($i+1) . ': ' . substr($par,0,7)},
3331 $i+1) .
3332 "&nbsp;</th>\n";
3334 print "</tr></thead>\n<tbody>\n";
3337 my $alternate = 1;
3338 my $patchno = 0;
3339 foreach my $line (@{$difftree}) {
3340 my $diff = parsed_difftree_line($line);
3342 if ($alternate) {
3343 print "<tr class=\"dark\">\n";
3344 } else {
3345 print "<tr class=\"light\">\n";
3347 $alternate ^= 1;
3349 if (exists $diff->{'nparents'}) { # combined diff
3351 fill_from_file_info($diff, @parents)
3352 unless exists $diff->{'from_file'};
3354 if (!is_deleted($diff)) {
3355 # file exists in the result (child) commit
3356 print "<td>" .
3357 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3358 file_name=>$to_prefix.$diff->{'to_file'},
3359 hash_base=>$hash),
3360 -class => "list"}, esc_path($diff->{'to_file'})) .
3361 "</td>\n";
3362 } else {
3363 print "<td>" .
3364 esc_path($diff->{'to_file'}) .
3365 "</td>\n";
3368 if ($action eq 'commitdiff') {
3369 # link to patch
3370 $patchno++;
3371 print "<td class=\"link\">" .
3372 $cgi->a({-href => "#patch$patchno"}, "patch") .
3373 " | " .
3374 "</td>\n";
3377 my $has_history = 0;
3378 my $not_deleted = 0;
3379 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3380 my $hash_parent = $parents[$i];
3381 my $from_hash = $diff->{'from_id'}[$i];
3382 my $from_path = $diff->{'from_file'}[$i];
3383 my $status = $diff->{'status'}[$i];
3385 $has_history ||= ($status ne 'A');
3386 $not_deleted ||= ($status ne 'D');
3388 if ($status eq 'A') {
3389 print "<td class=\"link\" align=\"right\"> | </td>\n";
3390 } elsif ($status eq 'D') {
3391 print "<td class=\"link\">" .
3392 $cgi->a({-href => href(action=>"blob",
3393 hash_base=>$hash,
3394 hash=>$from_hash,
3395 file_name=>$from_prefix.$from_path)},
3396 "blob" . ($i+1)) .
3397 " | </td>\n";
3398 } else {
3399 if ($diff->{'to_id'} eq $from_hash) {
3400 print "<td class=\"link nochange\">";
3401 } else {
3402 print "<td class=\"link\">";
3404 print $cgi->a({-href => href(action=>"blobdiff",
3405 hash=>$diff->{'to_id'},
3406 hash_parent=>$from_hash,
3407 hash_base=>$hash,
3408 hash_parent_base=>$hash_parent,
3409 file_name=>$to_prefix.$diff->{'to_file'},
3410 file_parent=>$from_prefix.$from_path)},
3411 "diff" . ($i+1)) .
3412 " | </td>\n";
3416 print "<td class=\"link\">";
3417 if ($not_deleted) {
3418 print $cgi->a({-href => href(action=>"blob",
3419 hash=>$diff->{'to_id'},
3420 file_name=>$to_prefix.$diff->{'to_file'},
3421 hash_base=>$hash)},
3422 "blob");
3423 print " | " if ($has_history);
3425 if ($has_history) {
3426 print $cgi->a({-href => href(action=>"history",
3427 file_name=>$to_prefix.$diff->{'to_file'},
3428 hash_base=>$hash)},
3429 "history");
3431 print "</td>\n";
3433 print "</tr>\n";
3434 next; # instead of 'else' clause, to avoid extra indent
3436 # else ordinary diff
3438 my ($to_mode_oct, $to_mode_str, $to_file_type);
3439 my ($from_mode_oct, $from_mode_str, $from_file_type);
3440 if ($diff->{'to_mode'} ne ('0' x 6)) {
3441 $to_mode_oct = oct $diff->{'to_mode'};
3442 if (S_ISREG($to_mode_oct)) { # only for regular file
3443 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3445 $to_file_type = file_type($diff->{'to_mode'});
3447 if ($diff->{'from_mode'} ne ('0' x 6)) {
3448 $from_mode_oct = oct $diff->{'from_mode'};
3449 if (S_ISREG($to_mode_oct)) { # only for regular file
3450 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3452 $from_file_type = file_type($diff->{'from_mode'});
3455 if ($diff->{'status'} eq "A") { # created
3456 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3457 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3458 $mode_chng .= "]</span>";
3459 print "<td>";
3460 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3461 hash_base=>$hash, file_name=>$to_prefix.$diff->{'file'}),
3462 -class => "list"}, esc_path($diff->{'file'}));
3463 print "</td>\n";
3464 print "<td>$mode_chng</td>\n";
3465 print "<td class=\"link\">";
3466 if ($action eq 'commitdiff') {
3467 # link to patch
3468 $patchno++;
3469 print $cgi->a({-href => "#patch$patchno"}, "patch");
3470 print " | ";
3472 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3473 hash_base=>$hash, file_name=>$to_prefix.$diff->{'file'})},
3474 "blob");
3475 print "</td>\n";
3477 } elsif ($diff->{'status'} eq "D") { # deleted
3478 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3479 print "<td>";
3480 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3481 hash_base=>$parent, file_name=>$from_prefix.$diff->{'file'}),
3482 -class => "list"}, esc_path($diff->{'file'}));
3483 print "</td>\n";
3484 print "<td>$mode_chng</td>\n";
3485 print "<td class=\"link\">";
3486 if ($action eq 'commitdiff') {
3487 # link to patch
3488 $patchno++;
3489 print $cgi->a({-href => "#patch$patchno"}, "patch");
3490 print " | ";
3492 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3493 hash_base=>$parent, file_name=>$from_prefix.$diff->{'file'})},
3494 "blob") . " | ";
3495 if ($have_blame) {
3496 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3497 file_name=>$from_prefix.$diff->{'file'}), -class => "blamelink"},
3498 "blame") . " | ";
3500 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3501 file_name=>$from_prefix.$diff->{'file'})},
3502 "history");
3503 print "</td>\n";
3505 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3506 my $mode_chnge = "";
3507 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3508 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3509 if ($from_file_type ne $to_file_type) {
3510 $mode_chnge .= " from $from_file_type to $to_file_type";
3512 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3513 if ($from_mode_str && $to_mode_str) {
3514 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3515 } elsif ($to_mode_str) {
3516 $mode_chnge .= " mode: $to_mode_str";
3519 $mode_chnge .= "]</span>\n";
3521 print "<td>";
3522 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3523 hash_base=>$hash, file_name=>$to_prefix.$diff->{'file'}),
3524 -class => "list"}, esc_path($diff->{'file'}));
3525 print "</td>\n";
3526 print "<td>$mode_chnge</td>\n";
3527 print "<td class=\"link\">";
3528 if ($action eq 'commitdiff') {
3529 # link to patch
3530 $patchno++;
3531 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3532 " | ";
3533 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3534 # "commit" view and modified file (not onlu mode changed)
3535 print $cgi->a({-href => href(action=>"blobdiff",
3536 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3537 hash_base=>$hash, hash_parent_base=>$parent,
3538 file_name=>$to_prefix.$diff->{'file'},
3539 file_parent=>$from_prefix.$diff->{'file'})},
3540 "diff") .
3541 " | ";
3543 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3544 hash_base=>$hash, file_name=>$to_prefix.$diff->{'file'})},
3545 "blob") . " | ";
3546 if ($have_blame) {
3547 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3548 file_name=>$diff->{'file'}), -class => "blamelink"},
3549 "blame") . " | ";
3551 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3552 file_name=>$to_prefix.$diff->{'file'})},
3553 "history");
3554 print "</td>\n";
3556 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3557 my %status_name = ('R' => 'moved', 'C' => 'copied');
3558 my $nstatus = $status_name{$diff->{'status'}};
3559 my $mode_chng = "";
3560 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3561 # mode also for directories, so we cannot use $to_mode_str
3562 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3564 print "<td>" .
3565 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3566 hash=>$diff->{'to_id'}, file_name=>$to_prefix.$diff->{'to_file'}),
3567 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3568 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3569 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3570 hash=>$diff->{'from_id'}, file_name=>$from_prefix.$diff->{'from_file'}),
3571 -class => "list"}, esc_path($diff->{'from_file'})) .
3572 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3573 "<td class=\"link\">";
3574 if ($action eq 'commitdiff') {
3575 # link to patch
3576 $patchno++;
3577 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3578 " | ";
3579 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3580 # "commit" view and modified file (not only pure rename or copy)
3581 print $cgi->a({-href => href(action=>"blobdiff",
3582 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3583 hash_base=>$hash, hash_parent_base=>$parent,
3584 file_name=>$to_prefix.$diff->{'to_file'}, file_parent=>$from_prefix.$diff->{'from_file'})},
3585 "diff") .
3586 " | ";
3588 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3589 hash_base=>$parent, file_name=>$to_prefix.$diff->{'to_file'})},
3590 "blob") . " | ";
3591 if ($have_blame) {
3592 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3593 file_name=>$diff->{'to_file'}), -class => "blamelink"},
3594 "blame") . " | ";
3596 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3597 file_name=>$to_prefix.$diff->{'to_file'})},
3598 "history");
3599 print "</td>\n";
3601 } # we should not encounter Unmerged (U) or Unknown (X) status
3602 print "</tr>\n";
3604 print "</tbody>" if $has_header;
3605 print "</table>\n";
3608 sub git_patchset_body {
3609 my ($fd, $difftree, $from_prefix, $to_prefix, $hash, @hash_parents) = @_;
3610 my ($hash_parent) = $hash_parents[0];
3612 my $is_combined = (@hash_parents > 1);
3613 my $patch_idx = 0;
3614 my $patch_number = 0;
3615 my $patch_line;
3616 my $diffinfo;
3617 my $to_name;
3618 my (%from, %to);
3620 $from_prefix = !defined $from_prefix ? '' : $from_prefix.'/';
3621 $to_prefix = !defined $to_prefix ? '' : $to_prefix . '/';
3623 print "<div class=\"patchset\">\n";
3625 # skip to first patch
3626 while ($patch_line = <$fd>) {
3627 chomp $patch_line;
3629 last if ($patch_line =~ m/^diff /);
3632 PATCH:
3633 while ($patch_line) {
3635 # parse "git diff" header line
3636 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3637 # $1 is from_name, which we do not use
3638 $to_name = unquote($2);
3639 $to_name =~ s!^b/!!;
3640 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3641 # $1 is 'cc' or 'combined', which we do not use
3642 $to_name = unquote($2);
3643 } else {
3644 $to_name = undef;
3647 # check if current patch belong to current raw line
3648 # and parse raw git-diff line if needed
3649 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3650 # this is continuation of a split patch
3651 print "<div class=\"patch cont\">\n";
3652 $diffinfo->{'from_prefix'} = $from_prefix;
3653 $diffinfo->{'to_prefix'} = $to_prefix;
3654 } else {
3655 # advance raw git-diff output if needed
3656 $patch_idx++ if defined $diffinfo;
3658 # read and prepare patch information
3659 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3661 # compact combined diff output can have some patches skipped
3662 # find which patch (using pathname of result) we are at now;
3663 if ($is_combined) {
3664 while ($to_name ne $diffinfo->{'to_file'}) {
3665 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3666 format_diff_cc_simplified($diffinfo, @hash_parents) .
3667 "</div>\n"; # class="patch"
3669 $patch_idx++;
3670 $patch_number++;
3672 last if $patch_idx > $#$difftree;
3673 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3677 $diffinfo->{'from_prefix'} = $from_prefix;
3678 $diffinfo->{'to_prefix'} = $to_prefix;
3680 # modifies %from, %to hashes
3681 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3682 # this is first patch for raw difftree line with $patch_idx index
3683 # we index @$difftree array from 0, but number patches from 1
3684 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3687 # git diff header
3688 #assert($patch_line =~ m/^diff /) if DEBUG;
3689 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3690 $patch_number++;
3691 # print "git diff" header
3692 print format_git_diff_header_line($patch_line, $diffinfo,
3693 \%from, \%to);
3695 # print extended diff header
3696 print "<div class=\"diff extended_header\">\n";
3697 EXTENDED_HEADER:
3698 while ($patch_line = <$fd>) {
3699 chomp $patch_line;
3701 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3703 print format_extended_diff_header_line($patch_line, $diffinfo,
3704 \%from, \%to);
3706 print "</div>\n"; # class="diff extended_header"
3708 # from-file/to-file diff header
3709 if (! $patch_line) {
3710 print "</div>\n"; # class="patch"
3711 last PATCH;
3713 next PATCH if ($patch_line =~ m/^diff /);
3714 #assert($patch_line =~ m/^---/) if DEBUG;
3716 my $last_patch_line = $patch_line;
3717 $patch_line = <$fd>;
3718 chomp $patch_line;
3719 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3721 print format_diff_from_to_header($last_patch_line, $patch_line,
3722 $diffinfo, \%from, \%to,
3723 @hash_parents);
3725 # the patch itself
3726 LINE:
3727 while ($patch_line = <$fd>) {
3728 chomp $patch_line;
3730 next PATCH if ($patch_line =~ m/^diff /);
3732 print format_diff_line($patch_line, \%from, \%to);
3735 } continue {
3736 print "</div>\n"; # class="patch"
3739 # for compact combined (--cc) format, with chunk and patch simpliciaction
3740 # patchset might be empty, but there might be unprocessed raw lines
3741 for (++$patch_idx if $patch_number > 0;
3742 $patch_idx < @$difftree;
3743 ++$patch_idx) {
3744 # read and prepare patch information
3745 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3747 # generate anchor for "patch" links in difftree / whatchanged part
3748 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3749 format_diff_cc_simplified($diffinfo, @hash_parents) .
3750 "</div>\n"; # class="patch"
3752 $patch_number++;
3755 if ($patch_number == 0) {
3756 if (@hash_parents > 1) {
3757 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3758 } else {
3759 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3763 print "</div>\n"; # class="patchset"
3766 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3768 # fills project list info (age, description, owner, forks) for each
3769 # project in the list, removing invalid projects from returned list
3770 # NOTE: modifies $projlist, but does not remove entries from it
3771 sub fill_project_list_info {
3772 my ($projlist, $check_forks) = @_;
3773 my @projects;
3775 my $show_ctags = gitweb_check_feature('ctags');
3776 PROJECT:
3777 foreach my $pr (@$projlist) {
3778 my (@activity) = git_get_last_activity($pr->{'path'});
3779 unless (@activity) {
3780 next PROJECT;
3782 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3783 if (!defined $pr->{'descr'}) {
3784 my $descr = git_get_project_description($pr->{'path'}) || "";
3785 $descr = to_utf8($descr);
3786 $pr->{'descr_long'} = $descr;
3787 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3789 if (!defined $pr->{'owner'}) {
3790 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3792 if ($check_forks) {
3793 my $pname = $pr->{'path'};
3794 if (($pname =~ s/\.git$//) &&
3795 ($pname !~ /\/$/) &&
3796 (-d "$projectroot/$pname")) {
3797 $pr->{'forks'} = "-d $projectroot/$pname";
3798 } else {
3799 $pr->{'forks'} = 0;
3802 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
3803 push @projects, $pr;
3806 return @projects;
3809 # print 'sort by' <th> element, generating 'sort by $name' replay link
3810 # if that order is not selected
3811 sub print_sort_th {
3812 my ($name, $order, $header) = @_;
3813 $header ||= ucfirst($name);
3815 if ($order eq $name) {
3816 print "<th>$header</th>\n";
3817 } else {
3818 print "<th>" .
3819 $cgi->a({-href => href(-replay=>1, order=>$name),
3820 -class => "header"}, $header) .
3821 "</th>\n";
3825 sub git_project_list_body {
3826 # actually uses global variable $project
3827 my ($projlist, $order, $from, $to, $extra, $no_header, $cache_lifetime) = @_;
3829 my ($check_forks) = gitweb_check_feature('forks');
3831 use File::stat;
3832 use POSIX qw(:fcntl_h);
3833 use Storable qw(store_fd retrieve);
3835 my $cache_file = "$cache_dir/$projlist_cache_name";
3837 my @projects;
3838 my $stale = 0;
3839 my $now = time();
3840 my $cache_mtime;
3841 if ($cache_lifetime && -f $cache_file) {
3842 $cache_mtime = stat($cache_file)->mtime;
3844 if (defined $cache_mtime && # caching is on and $cache_file exists
3845 $cache_mtime + $cache_lifetime*60 > $now &&
3846 (my $dump = retrieve($cache_file))) {
3847 $stale = $now - $cache_mtime;
3848 @projects = @$dump;
3849 } else {
3850 if (defined $cache_mtime) {
3851 # Postpone timeout by two minutes so that we get
3852 # enough time to do our job, or to be more exact
3853 # make cache expire after two minutes from now.
3854 my $time = $now - $cache_lifetime*60 + 120;
3855 utime $time, $time, $cache_file;
3857 @projects = fill_project_list_info($projlist, $check_forks);
3858 if ($cache_lifetime &&
3859 (-d $cache_dir || mkdir($cache_dir, 0700)) &&
3860 sysopen(my $fd, "$cache_file.lock", O_WRONLY|O_CREAT|O_EXCL, 0600)) {
3861 store_fd(\@projects, $fd);
3862 close $fd;
3863 rename "$cache_file.lock", $cache_file;
3867 $order ||= $default_projects_order;
3868 $from = 0 unless defined $from;
3869 $to = $#projects if (!defined $to || $#projects < $to);
3871 if ($cache_lifetime && $stale > 0) {
3872 print "<div class=\"stale_info\">Cached version (${stale}s old)</div>\n";
3875 my %order_info = (
3876 project => { key => 'path', type => 'str' },
3877 descr => { key => 'descr_long', type => 'str' },
3878 owner => { key => 'owner', type => 'str' },
3879 age => { key => 'age', type => 'num' }
3881 my $oi = $order_info{$order};
3882 if ($oi->{'type'} eq 'str') {
3883 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
3884 } else {
3885 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
3888 if ($cache_lifetime && $stale > 0) {
3889 print "<div class=\"stale_info\">Cached version (${stale}s old)</div>\n";
3892 my $show_ctags = gitweb_check_feature('ctags');
3893 if ($show_ctags) {
3894 my %ctags;
3895 foreach my $p (@projects) {
3896 foreach my $ct (keys %{$p->{'ctags'}}) {
3897 $ctags{$ct} += $p->{'ctags'}->{$ct};
3900 my $cloud = git_populate_project_tagcloud(\%ctags);
3901 print git_show_project_tagcloud($cloud, 64);
3904 print "<table class=\"project_list\">\n";
3905 unless ($no_header) {
3906 print "<tr>\n";
3907 if ($check_forks) {
3908 print "<th></th>\n";
3910 print_sort_th('project', $order, 'Project');
3911 print_sort_th('descr', $order, 'Description');
3912 print_sort_th('owner', $order, 'Owner');
3913 print_sort_th('age', $order, 'Last Change');
3914 print "<th></th>\n" . # for links
3915 "</tr>\n";
3917 my $alternate = 1;
3918 my $tagfilter = $cgi->param('by_tag');
3919 for (my $i = $from; $i <= $to; $i++) {
3920 my $pr = $projects[$i];
3922 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
3923 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
3924 and not $pr->{'descr_long'} =~ /$searchtext/;
3925 # Weed out forks or non-matching entries of search
3926 if ($check_forks) {
3927 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
3928 $forkbase="^$forkbase" if $forkbase;
3929 next if not $searchtext and not $tagfilter and $show_ctags
3930 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
3933 if ($alternate) {
3934 print "<tr class=\"dark\">\n";
3935 } else {
3936 print "<tr class=\"light\">\n";
3938 $alternate ^= 1;
3939 if ($check_forks) {
3940 print "<td>";
3941 if ($pr->{'forks'}) {
3942 print "<!-- $pr->{'forks'} -->\n";
3943 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3945 print "</td>\n";
3947 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3948 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3949 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3950 -class => "list", -title => $pr->{'descr_long'}},
3951 esc_html($pr->{'descr'})) . "</td>\n" .
3952 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3953 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3954 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3955 "<td class=\"link\">" .
3956 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3957 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "log") . " | " .
3958 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3959 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3960 "</td>\n" .
3961 "</tr>\n";
3963 if (defined $extra) {
3964 print "<tr>\n";
3965 if ($check_forks) {
3966 print "<td></td>\n";
3968 print "<td colspan=\"5\">$extra</td>\n" .
3969 "</tr>\n";
3971 print "</table>\n";
3974 sub git_shortlog_body {
3975 # uses global variable $project
3976 my ($commitlist, $from, $to, $refs, $extra) = @_;
3978 $from = 0 unless defined $from;
3979 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3981 print "<table class=\"shortlog\">\n";
3982 my $alternate = 1;
3983 for (my $i = $from; $i <= $to; $i++) {
3984 my %co = %{$commitlist->[$i]};
3985 my $commit = $co{'id'};
3986 my $ref = format_ref_marker($refs, $commit);
3987 if ($alternate) {
3988 print "<tr class=\"dark\">\n";
3989 } else {
3990 print "<tr class=\"light\">\n";
3992 $alternate ^= 1;
3993 my $author = chop_and_escape_str($co{'author_name'}, 10);
3994 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3995 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3996 "<td><i>" . $author . "</i></td>\n" .
3997 "<td>";
3998 print format_subject_html($co{'title'}, $co{'title_short'},
3999 href(action=>"commit", hash=>$commit), $ref);
4000 print "</td>\n" .
4001 "<td class=\"link\">" .
4002 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
4003 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
4004 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
4005 my $snapshot_links = format_snapshot_links($commit);
4006 if (defined $snapshot_links) {
4007 print " | " . $snapshot_links;
4009 print "</td>\n" .
4010 "</tr>\n";
4012 if (defined $extra) {
4013 print "<tr>\n" .
4014 "<td colspan=\"4\">$extra</td>\n" .
4015 "</tr>\n";
4017 print "</table>\n";
4020 sub git_history_body {
4021 # Warning: assumes constant type (blob or tree) during history
4022 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
4024 $from = 0 unless defined $from;
4025 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4027 print "<table class=\"history\">\n";
4028 my $alternate = 1;
4029 for (my $i = $from; $i <= $to; $i++) {
4030 my %co = %{$commitlist->[$i]};
4031 if (!%co) {
4032 next;
4034 my $commit = $co{'id'};
4036 my $ref = format_ref_marker($refs, $commit);
4038 if ($alternate) {
4039 print "<tr class=\"dark\">\n";
4040 } else {
4041 print "<tr class=\"light\">\n";
4043 $alternate ^= 1;
4044 # shortlog uses chop_str($co{'author_name'}, 10)
4045 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
4046 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4047 "<td><i>" . $author . "</i></td>\n" .
4048 "<td>";
4049 # originally git_history used chop_str($co{'title'}, 50)
4050 print format_subject_html($co{'title'}, $co{'title_short'},
4051 href(action=>"commit", hash=>$commit), $ref);
4052 print "</td>\n" .
4053 "<td class=\"link\">" .
4054 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4055 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4057 if ($ftype eq 'blob') {
4058 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
4059 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4060 if (defined $blob_current && defined $blob_parent &&
4061 $blob_current ne $blob_parent) {
4062 print " | " .
4063 $cgi->a({-href => href(action=>"blobdiff",
4064 hash=>$blob_current, hash_parent=>$blob_parent,
4065 hash_base=>$hash_base, hash_parent_base=>$commit,
4066 file_name=>$file_name)},
4067 "diff to current");
4070 print "</td>\n" .
4071 "</tr>\n";
4073 if (defined $extra) {
4074 print "<tr>\n" .
4075 "<td colspan=\"4\">$extra</td>\n" .
4076 "</tr>\n";
4078 print "</table>\n";
4081 sub git_tags_body {
4082 # uses global variable $project
4083 my ($taglist, $from, $to, $extra) = @_;
4084 $from = 0 unless defined $from;
4085 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4087 print "<table class=\"tags\">\n";
4088 my $alternate = 1;
4089 for (my $i = $from; $i <= $to; $i++) {
4090 my $entry = $taglist->[$i];
4091 my %tag = %$entry;
4092 my $comment = $tag{'subject'};
4093 my $comment_short;
4094 if (defined $comment) {
4095 $comment_short = chop_str($comment, 30, 5);
4097 if ($alternate) {
4098 print "<tr class=\"dark\">\n";
4099 } else {
4100 print "<tr class=\"light\">\n";
4102 $alternate ^= 1;
4103 if (defined $tag{'age'}) {
4104 print "<td><i>$tag{'age'}</i></td>\n";
4105 } else {
4106 print "<td></td>\n";
4108 print "<td>" .
4109 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4110 -class => "list name"}, esc_html($tag{'name'})) .
4111 "</td>\n" .
4112 "<td>";
4113 if (defined $comment) {
4114 print format_subject_html($comment, $comment_short,
4115 href(action=>"tag", hash=>$tag{'id'}));
4117 print "</td>\n" .
4118 "<td class=\"selflink\">";
4119 if ($tag{'type'} eq "tag") {
4120 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4121 } else {
4122 print "&nbsp;";
4124 print "</td>\n" .
4125 "<td class=\"link\">" . " | " .
4126 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4127 if ($tag{'reftype'} eq "commit") {
4128 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "log");
4129 } elsif ($tag{'reftype'} eq "blob") {
4130 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4132 print "</td>\n" .
4133 "</tr>";
4135 if (defined $extra) {
4136 print "<tr>\n" .
4137 "<td colspan=\"5\">$extra</td>\n" .
4138 "</tr>\n";
4140 print "</table>\n";
4143 sub git_heads_body {
4144 # uses global variable $project
4145 my ($headlist, $head, $from, $to, $extra) = @_;
4146 $from = 0 unless defined $from;
4147 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4149 print "<table class=\"heads\">\n";
4150 my $alternate = 1;
4151 for (my $i = $from; $i <= $to; $i++) {
4152 my $entry = $headlist->[$i];
4153 my %ref = %$entry;
4154 my $curr = $ref{'id'} eq $head;
4155 if ($alternate) {
4156 print "<tr class=\"dark\">\n";
4157 } else {
4158 print "<tr class=\"light\">\n";
4160 $alternate ^= 1;
4161 print "<td><i>$ref{'age'}</i></td>\n" .
4162 ($curr ? "<td class=\"current_head\">" : "<td>") .
4163 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4164 -class => "list name"},esc_html($ref{'name'})) .
4165 "</td>\n" .
4166 "<td class=\"link\">" .
4167 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "log") . " | " .
4168 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4169 "</td>\n" .
4170 "</tr>";
4172 if (defined $extra) {
4173 print "<tr>\n" .
4174 "<td colspan=\"3\">$extra</td>\n" .
4175 "</tr>\n";
4177 print "</table>\n";
4180 sub git_search_grep_body {
4181 my ($commitlist, $from, $to, $extra) = @_;
4182 $from = 0 unless defined $from;
4183 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4185 print "<table class=\"commit_search\">\n";
4186 my $alternate = 1;
4187 for (my $i = $from; $i <= $to; $i++) {
4188 my %co = %{$commitlist->[$i]};
4189 if (!%co) {
4190 next;
4192 my $commit = $co{'id'};
4193 if ($alternate) {
4194 print "<tr class=\"dark\">\n";
4195 } else {
4196 print "<tr class=\"light\">\n";
4198 $alternate ^= 1;
4199 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
4200 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4201 "<td><i>" . $author . "</i></td>\n" .
4202 "<td>" .
4203 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4204 -class => "list subject"},
4205 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4206 my $comment = $co{'comment'};
4207 foreach my $line (@$comment) {
4208 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4209 my ($lead, $match, $trail) = ($1, $2, $3);
4210 $match = chop_str($match, 70, 5, 'center');
4211 my $contextlen = int((80 - length($match))/2);
4212 $contextlen = 30 if ($contextlen > 30);
4213 $lead = chop_str($lead, $contextlen, 10, 'left');
4214 $trail = chop_str($trail, $contextlen, 10, 'right');
4216 $lead = esc_html($lead);
4217 $match = esc_html($match);
4218 $trail = esc_html($trail);
4220 print "$lead<span class=\"match\">$match</span>$trail<br />";
4223 print "</td>\n" .
4224 "<td class=\"link\">" .
4225 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4226 " | " .
4227 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4228 " | " .
4229 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4230 print "</td>\n" .
4231 "</tr>\n";
4233 if (defined $extra) {
4234 print "<tr>\n" .
4235 "<td colspan=\"3\">$extra</td>\n" .
4236 "</tr>\n";
4238 print "</table>\n";
4241 ## ======================================================================
4242 ## ======================================================================
4243 ## actions
4245 sub git_project_list {
4246 my $order = $cgi->param('o');
4247 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4248 die_error(400, "Unknown order parameter");
4251 my @list = git_get_projects_list();
4252 if (!@list) {
4253 die_error(404, "No projects found");
4256 git_header_html();
4257 if (-f $home_text) {
4258 print "<div class=\"index_include\">\n";
4259 open (my $fd, $home_text);
4260 print <$fd>;
4261 close $fd;
4262 print "</div>\n";
4264 print $cgi->startform(-method => "get") .
4265 "<p class=\"projsearch\">Search:\n" .
4266 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4267 "</p>" .
4268 $cgi->end_form() . "\n";
4269 git_project_list_body(\@list, $order, undef, undef, undef, undef, $projlist_cache_lifetime);
4270 git_footer_html();
4273 sub git_forks {
4274 my $order = $cgi->param('o');
4275 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4276 die_error(400, "Unknown order parameter");
4279 my @list = git_get_projects_list($project);
4280 if (!@list) {
4281 die_error(404, "No forks found");
4284 git_header_html();
4285 git_print_page_nav('','');
4286 git_print_header_div('summary', "$project forks");
4287 git_project_list_body(\@list, $order);
4288 git_footer_html();
4291 sub git_project_index {
4292 my @projects = git_get_projects_list($project);
4294 print $cgi->header(
4295 -type => 'text/plain',
4296 -charset => 'utf-8',
4297 -content_disposition => 'inline; filename="index.aux"');
4299 foreach my $pr (@projects) {
4300 if (!exists $pr->{'owner'}) {
4301 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4304 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4305 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4306 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4307 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4308 $path =~ s/ /\+/g;
4309 $owner =~ s/ /\+/g;
4311 print "$path $owner\n";
4315 sub git_summary {
4316 my $descr = git_get_project_description($project) || "none";
4317 my %co = parse_commit("HEAD");
4318 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4319 my $head = $co{'id'};
4321 my $owner = git_get_project_owner($project);
4322 my $homepage = git_get_project_config('homepage');
4323 my $base_url = git_get_project_config('base_url');
4324 my $last_refresh = git_get_project_config("last_refresh");
4326 my $refs = git_get_references();
4327 # These get_*_list functions return one more to allow us to see if
4328 # there are more ...
4329 my @taglist = git_get_tags_list(16);
4330 my @headlist = git_get_heads_list(16);
4331 my @forklist;
4332 my ($check_forks) = gitweb_check_feature('forks');
4334 if ($check_forks) {
4335 @forklist = git_get_projects_list($project);
4338 git_header_html();
4339 git_print_page_nav('summary','', $head);
4341 if ($check_forks and $project =~ m#/#) {
4342 my $xproject = $project; $xproject =~ s#/.+?$#.git#; #
4343 my $r = $cgi->a({-href=> href(project => $xproject, action => 'summary')}, $xproject);
4344 print <<EOT;
4345 <div class="forkinfo">
4346 This project is a fork of the $r project. If you have that one
4347 already cloned locally, you can use
4348 <pre>git clone --reference /path/to/your/$xproject/incarnation mirror_URL</pre>
4349 to save bandwidth during cloning.
4350 </div>
4354 print "<div class=\"title\">&nbsp;</div>\n";
4355 print "<table class=\"projects_list\">\n";
4356 print "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4357 $homepage and print "<tr id=\"metadata_homepage\"><td>homepage URL</td><td>" . $cgi->a({-href => $homepage}, $homepage) . "</td></tr>\n";
4358 $base_url and print "<tr id=\"metadata_baseurl\"><td>repository URL</td><td>" . esc_html($base_url) . "</td></tr>\n";
4359 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . email_obfuscate($owner) . "</td></tr>\n";
4360 if (defined $cd{'rfc2822'}) {
4361 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4363 $last_refresh and print "<tr id=\"metadata_lrefresh\"><td>last refresh</td><td>$last_refresh</td></tr>\n";
4365 # use per project git URL list in $projectroot/$project/cloneurl
4366 # or make project git URL from git base URL and project name
4367 my $url_tag = $base_url ? "mirror URL" : "URL";
4368 my @url_list = git_get_project_url_list($project);
4369 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4370 foreach my $git_url (@url_list) {
4371 next unless $git_url;
4372 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4373 $url_tag = "";
4375 -f "$projectroot/$project/.nofetch" and $git_base_push_url and
4376 print "<tr id=\"metadata_pushurl\"><td>Push URL</td><td>$git_base_push_url/$project</td></tr>\n";
4378 # Tag cloud
4379 my $show_ctags = (gitweb_check_feature('ctags'))[0];
4380 if ($show_ctags) {
4381 my $ctags = git_get_project_ctags($project);
4382 my $cloud = git_populate_project_tagcloud($ctags);
4383 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4384 print "</td>\n<td>" unless %$ctags;
4385 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4386 print "</td>\n<td>" if %$ctags;
4387 print git_show_project_tagcloud($cloud, 48);
4388 print "</td></tr>";
4391 print "</table>\n";
4393 if (-s "$projectroot/$project/README.html") {
4394 if (open my $fd, "$projectroot/$project/README.html") {
4395 print "<div class=\"title\">readme</div>\n" .
4396 "<div class=\"readme\">\n";
4397 print $_ while (<$fd>);
4398 print "\n</div>\n"; # class="readme"
4399 close $fd;
4403 # we need to request one more than 16 (0..15) to check if
4404 # those 16 are all
4405 my @commitlist = $head ? parse_commits($head, 17) : ();
4406 if (@commitlist) {
4407 git_print_header_div('shortlog');
4408 git_shortlog_body(\@commitlist, 0, 15, $refs,
4409 $#commitlist <= 15 ? undef :
4410 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4413 if (@taglist) {
4414 git_print_header_div('tags');
4415 git_tags_body(\@taglist, 0, 15,
4416 $#taglist <= 15 ? undef :
4417 $cgi->a({-href => href(action=>"tags")}, "..."));
4420 if (@headlist) {
4421 git_print_header_div('heads');
4422 git_heads_body(\@headlist, $head, 0, 15,
4423 $#headlist <= 15 ? undef :
4424 $cgi->a({-href => href(action=>"heads")}, "..."));
4427 if (@forklist) {
4428 git_print_header_div('forks');
4429 git_project_list_body(\@forklist, 'age', 0, 15,
4430 $#forklist <= 15 ? undef :
4431 $cgi->a({-href => href(action=>"forks")}, "..."),
4432 'no_header');
4435 git_footer_html();
4438 sub git_tag {
4439 my $head = git_get_head_hash($project);
4440 git_header_html();
4441 git_print_page_nav('','', $head,undef,$head);
4442 my %tag = parse_tag($hash);
4444 if (! %tag) {
4445 die_error(404, "Unknown tag object");
4448 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4449 print "<div class=\"title_text\">\n" .
4450 "<table class=\"object_header\">\n" .
4451 "<tr>\n" .
4452 "<td>object</td>\n" .
4453 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4454 $tag{'object'}) . "</td>\n" .
4455 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4456 $tag{'type'}) . "</td>\n" .
4457 "</tr>\n";
4458 if (defined($tag{'author'})) {
4459 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4460 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4461 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4462 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4463 "</td></tr>\n";
4465 print "</table>\n\n" .
4466 "</div>\n";
4467 print "<div class=\"page_body\">";
4468 my $comment = $tag{'comment'};
4469 foreach my $line (@$comment) {
4470 chomp $line;
4471 print esc_html($line, -nbsp=>1) . "<br/>\n";
4473 print "</div>\n";
4474 git_footer_html();
4477 sub git_blame_data {
4478 my $fd;
4479 my $ftype;
4481 my ($have_blame) = gitweb_check_feature('blame');
4482 if (!$have_blame) {
4483 die_error('403 Permission denied', "Permission denied");
4485 die_error('404 Not Found', "File name not defined") if (!$file_name);
4486 $hash_base ||= git_get_head_hash($project);
4487 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4488 my %co = parse_commit($hash_base)
4489 or die_error(undef, "Reading commit failed");
4490 if (!defined $hash) {
4491 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4492 or die_error(undef, "Error looking up file");
4494 $ftype = git_get_type($hash);
4495 if ($ftype !~ "blob") {
4496 die_error("400 Bad Request", "Object is not a blob");
4498 open ($fd, "-|", git_cmd(), "blame", '--incremental', $hash_base, '--',
4499 $file_name)
4500 or die_error(undef, "Open git-blame --incremental failed");
4502 print $cgi->header(-type=>"text/plain", -charset => 'utf-8',
4503 -status=> "200 OK");
4505 while(<$fd>) {
4506 if (/^([0-9a-f]{40}) ([0-9]+) ([0-9]+) ([0-9]+)/ or
4507 /^author-time |^author |^filename /) {
4508 print;
4512 close $fd or print "Reading blame data failed\n";
4515 sub git_blame_common {
4516 my ($type) = @_;
4518 my $fd;
4519 my $ftype;
4521 gitweb_check_feature('blame')
4522 or die_error(403, "Blame view not allowed");
4524 die_error(400, "No file name given") unless $file_name;
4525 $hash_base ||= git_get_head_hash($project);
4526 die_error(404, "Couldn't find base commit") unless ($hash_base);
4527 my %co = parse_commit($hash_base)
4528 or die_error(404, "Commit not found");
4529 if (!defined $hash) {
4530 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4531 or die_error(404, "Error looking up file");
4533 $ftype = git_get_type($hash);
4534 if ($ftype !~ "blob") {
4535 die_error(400, "Object is not a blob");
4537 if ($type eq 'incremental') {
4538 open ($fd, "-|", git_cmd(), 'cat-file', 'blob', $hash)
4539 or die_error(undef, "Open git-cat-file failed");
4540 } else {
4541 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4542 $file_name, $hash_base)
4543 or die_error(500, "Open git-blame failed");
4545 git_header_html();
4546 my $formats_nav =
4547 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4548 "blob") .
4549 " | " .
4550 $cgi->a({-href => href(action=>"history", -replay=>1)},
4551 "history") .
4552 " | " .
4553 $cgi->a({-href => href(action=>"blame", file_name=>$file_name), -class => "blamelink"},
4554 "HEAD");
4555 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4556 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4557 git_print_page_path($file_name, $ftype, $hash_base);
4558 my @rev_color = (qw(light2 dark2));
4559 my $num_colors = scalar(@rev_color);
4560 my $current_color = 0;
4561 my $last_rev;
4562 print <<HTML;
4564 <div class="page_body">
4565 <table class="blame">
4566 <tr><th>Commit&nbsp;<a href="javascript:extra_blame_columns()" id="columns_expander">[+]</a></th>
4567 <th class="extra_column">Author</th>
4568 <th class="extra_column">Date</th>
4569 <th>Line</th>
4570 <th>Data</th></tr>
4571 HTML
4572 my %metainfo = ();
4573 my $linenr = 0;
4574 while (<$fd>) {
4575 chomp;
4576 if ($type eq 'incremental') {
4577 # Empty stage with just the file contents
4578 $linenr += 1;
4579 print "<tr id=\"l$linenr\" class=\"light2\">";
4580 print '<td class="sha1"><a href=""></a></td>';
4581 print "<td class=\"extra_column\"></td>";
4582 print "<td class=\"extra_column\"></td>";
4583 print "<td class=\"linenr\"><a class=\"linenr\" href=\"\">$linenr</a></td><td class=\"pre\">" . esc_html($_) . "</td>\n";
4584 print "</tr>\n";
4585 next;
4588 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4589 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4590 if (!exists $metainfo{$full_rev}) {
4591 $metainfo{$full_rev} = {};
4593 my $meta = $metainfo{$full_rev};
4594 while (<$fd>) {
4595 last if (s/^\t//);
4596 if (/^(\S+) (.*)$/) {
4597 $meta->{$1} = $2;
4600 my $data = $_;
4601 chomp $data;
4602 my $rev = substr($full_rev, 0, 8);
4603 my $author = $meta->{'author'};
4604 my %date = parse_date($meta->{'author-time'},
4605 $meta->{'author-tz'});
4606 my $date = $date{'iso-tz'};
4607 if ($group_size) {
4608 $current_color = ++$current_color % $num_colors;
4610 print "<tr class=\"$rev_color[$current_color]\">\n";
4611 if ($group_size) {
4612 my $rowspan = $group_size > 1 ? " rowspan=\"$group_size\"" : "";
4613 print "<td class=\"sha1\"";
4614 print " title=\"". esc_html($author) . ", $date\"";
4615 print "$rowspan>";
4616 print $cgi->a({-href => href(action=>"commit",
4617 hash=>$full_rev,
4618 file_name=>$file_name)},
4619 esc_html($rev));
4620 print "</td>\n";
4621 print "<td class=\"extra_column\" $rowspan>". esc_html($author) . "</td>";
4622 print "<td class=\"extra_column\" $rowspan>". $date . "</td>";
4624 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4625 or die_error(500, "Open git-rev-parse failed");
4626 my $parent_commit = <$dd>;
4627 close $dd;
4628 chomp($parent_commit);
4629 my $blamed = href(action => 'blame',
4630 file_name => $meta->{'filename'},
4631 hash_base => $parent_commit);
4632 print "<td class=\"linenr\">";
4633 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4634 -id => "l$lineno",
4635 -class => "linenr" },
4636 esc_html($lineno));
4637 print "</td>";
4638 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4639 print "</tr>\n";
4642 print "</table>\n";
4643 print "</div>";
4644 close $fd
4645 or print "Reading blob failed\n";
4647 if ($type eq 'incremental') {
4648 print "<script type=\"text/javascript\">\n";
4649 print "startBlame(\"" . href(action=>"blame_data", hash_base=>$hash_base, file_name=>$file_name) . "\", \"" .
4650 href(-partial_query=>1) . "\");\n";
4651 print "</script>\n";
4654 git_footer_html();
4657 sub git_blame_incremental {
4658 git_blame_common('incremental');
4661 sub git_blame {
4662 git_blame_common('oneshot');
4665 sub git_tags {
4666 my $head = git_get_head_hash($project);
4667 git_header_html();
4668 git_print_page_nav('','', $head,undef,$head);
4669 git_print_header_div('summary', $project);
4671 my @tagslist = git_get_tags_list();
4672 if (@tagslist) {
4673 git_tags_body(\@tagslist);
4675 git_footer_html();
4678 sub git_heads {
4679 my $head = git_get_head_hash($project);
4680 git_header_html();
4681 git_print_page_nav('','', $head,undef,$head);
4682 git_print_header_div('summary', $project);
4684 my @headslist = git_get_heads_list();
4685 if (@headslist) {
4686 git_heads_body(\@headslist, $head);
4688 git_footer_html();
4691 sub git_blob_plain {
4692 my $type = shift;
4693 my $expires;
4695 if (!defined $hash) {
4696 if (defined $file_name) {
4697 my $base = $hash_base || git_get_head_hash($project);
4698 $hash = git_get_hash_by_path($base, $file_name, "blob")
4699 or die_error(404, "Cannot find file");
4700 } else {
4701 die_error(400, "No file name defined");
4703 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4704 # blobs defined by non-textual hash id's can be cached
4705 $expires = "+1d";
4708 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4709 or die_error(500, "Open git-cat-file blob '$hash' failed");
4711 # content-type (can include charset)
4712 $type = blob_contenttype($fd, $file_name, $type);
4714 # "save as" filename, even when no $file_name is given
4715 my $save_as = "$hash";
4716 if (defined $file_name) {
4717 $save_as = $file_name;
4718 } elsif ($type =~ m/^text\//) {
4719 $save_as .= '.txt';
4722 print $cgi->header(
4723 -type => $type,
4724 -expires => $expires,
4725 -content_disposition => 'inline; filename="' . $save_as . '"');
4726 undef $/;
4727 binmode STDOUT, ':raw';
4728 print <$fd>;
4729 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4730 $/ = "\n";
4731 close $fd;
4734 sub git_blob {
4735 my $expires;
4737 if (!defined $hash) {
4738 if (defined $file_name) {
4739 my $base = $hash_base || git_get_head_hash($project);
4740 $hash = git_get_hash_by_path($base, $file_name, "blob")
4741 or die_error(404, "Cannot find file");
4742 } else {
4743 die_error(400, "No file name defined");
4745 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4746 # blobs defined by non-textual hash id's can be cached
4747 $expires = "+1d";
4750 my ($have_blame) = gitweb_check_feature('blame');
4751 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4752 or die_error(500, "Couldn't cat $file_name, $hash");
4753 my $mimetype = blob_mimetype($fd, $file_name);
4754 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4755 close $fd;
4756 return git_blob_plain($mimetype);
4758 # we can have blame only for text/* mimetype
4759 $have_blame &&= ($mimetype =~ m!^text/!);
4761 git_header_html(undef, $expires);
4762 my $formats_nav = '';
4763 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4764 if (defined $file_name) {
4765 if ($have_blame) {
4766 $formats_nav .=
4767 $cgi->a({-href => href(action=>"blame", -replay=>1,
4768 -class => "blamelink")},
4769 "blame") .
4770 " | ";
4772 $formats_nav .=
4773 $cgi->a({-href => href(action=>"history", -replay=>1)},
4774 "history") .
4775 " | " .
4776 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4777 "raw") .
4778 " | " .
4779 $cgi->a({-href => href(action=>"blob",
4780 hash_base=>"HEAD", file_name=>$file_name)},
4781 "HEAD");
4782 } else {
4783 $formats_nav .=
4784 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4785 "raw");
4787 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4788 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4789 } else {
4790 print "<div class=\"page_nav\">\n" .
4791 "<br/><br/></div>\n" .
4792 "<div class=\"title\">$hash</div>\n";
4794 git_print_page_path($file_name, "blob", $hash_base);
4795 print "<div class=\"page_body\">\n";
4796 if ($mimetype =~ m!^image/!) {
4797 print qq!<img type="$mimetype"!;
4798 if ($file_name) {
4799 print qq! alt="$file_name" title="$file_name"!;
4801 print qq! src="! .
4802 href(action=>"blob_plain", hash=>$hash,
4803 hash_base=>$hash_base, file_name=>$file_name) .
4804 qq!" />\n!;
4805 } else {
4806 my $nr;
4807 while (my $line = <$fd>) {
4808 chomp $line;
4809 $nr++;
4810 $line = untabify($line);
4811 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4812 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4815 close $fd
4816 or print "Reading blob failed.\n";
4817 print "</div>";
4818 git_footer_html();
4821 sub git_tree {
4822 if (!defined $hash_base) {
4823 $hash_base = "HEAD";
4825 if (!defined $hash) {
4826 if (defined $file_name) {
4827 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4828 } else {
4829 $hash = $hash_base;
4832 die_error(404, "No such tree") unless defined($hash);
4833 $/ = "\0";
4834 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4835 or die_error(500, "Open git-ls-tree failed");
4836 my @entries = map { chomp; $_ } <$fd>;
4837 close $fd or die_error(404, "Reading tree failed");
4838 $/ = "\n";
4840 my $refs = git_get_references();
4841 my $ref = format_ref_marker($refs, $hash_base);
4842 git_header_html();
4843 my $basedir = '';
4844 my ($have_blame) = gitweb_check_feature('blame');
4845 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4846 my @views_nav = ();
4847 if (defined $file_name) {
4848 push @views_nav,
4849 $cgi->a({-href => href(action=>"history", -replay=>1)},
4850 "history"),
4851 $cgi->a({-href => href(action=>"tree",
4852 hash_base=>"HEAD", file_name=>$file_name)},
4853 "HEAD"),
4855 my $snapshot_links = format_snapshot_links($hash);
4856 if (defined $snapshot_links) {
4857 # FIXME: Should be available when we have no hash base as well.
4858 push @views_nav, $snapshot_links;
4860 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4861 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4862 } else {
4863 undef $hash_base;
4864 print "<div class=\"page_nav\">\n";
4865 print "<br/><br/></div>\n";
4866 print "<div class=\"title\">$hash</div>\n";
4868 if (defined $file_name) {
4869 $basedir = $file_name;
4870 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4871 $basedir .= '/';
4873 git_print_page_path($file_name, 'tree', $hash_base);
4875 print "<div class=\"page_body\">\n";
4876 print "<table class=\"tree\">\n";
4877 my $alternate = 1;
4878 # '..' (top directory) link if possible
4879 if (defined $hash_base &&
4880 defined $file_name && $file_name =~ m![^/]+$!) {
4881 if ($alternate) {
4882 print "<tr class=\"dark\">\n";
4883 } else {
4884 print "<tr class=\"light\">\n";
4886 $alternate ^= 1;
4888 my $up = $file_name;
4889 $up =~ s!/?[^/]+$!!;
4890 undef $up unless $up;
4891 # based on git_print_tree_entry
4892 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4893 print '<td class="list">';
4894 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4895 file_name=>$up)},
4896 "..");
4897 print "</td>\n";
4898 print "<td class=\"link\"></td>\n";
4900 print "</tr>\n";
4902 foreach my $line (@entries) {
4903 my %t = parse_ls_tree_line($line, -z => 1);
4905 if ($alternate) {
4906 print "<tr class=\"dark\">\n";
4907 } else {
4908 print "<tr class=\"light\">\n";
4910 $alternate ^= 1;
4912 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4914 print "</tr>\n";
4916 print "</table>\n" .
4917 "</div>";
4918 git_footer_html();
4921 sub git_snapshot {
4922 my @supported_fmts = gitweb_check_feature('snapshot');
4923 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4925 my $format = $cgi->param('sf');
4926 if (!@supported_fmts) {
4927 die_error(403, "Snapshots not allowed");
4929 # default to first supported snapshot format
4930 $format ||= $supported_fmts[0];
4931 if ($format !~ m/^[a-z0-9]+$/) {
4932 die_error(400, "Invalid snapshot format parameter");
4933 } elsif (!exists($known_snapshot_formats{$format})) {
4934 die_error(400, "Unknown snapshot format");
4935 } elsif (!grep($_ eq $format, @supported_fmts)) {
4936 die_error(403, "Unsupported snapshot format");
4939 if (!defined $hash) {
4940 $hash = git_get_head_hash($project);
4943 my $name = $project;
4944 $name =~ s,([^/])/*\.git$,$1,;
4945 $name = basename($name);
4946 my $filename = to_utf8($name);
4947 $name =~ s/\047/\047\\\047\047/g;
4948 my $cmd;
4949 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4950 $cmd = quote_command(
4951 git_cmd(), 'archive',
4952 "--format=$known_snapshot_formats{$format}{'format'}",
4953 "--prefix=$name/", $hash);
4954 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4955 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
4958 print $cgi->header(
4959 -type => $known_snapshot_formats{$format}{'type'},
4960 -content_disposition => 'inline; filename="' . "$filename" . '"',
4961 -status => '200 OK');
4963 open my $fd, "-|", $cmd
4964 or die_error(500, "Execute git-archive failed");
4965 binmode STDOUT, ':raw';
4966 print <$fd>;
4967 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4968 close $fd;
4971 sub git_log {
4972 my $head = git_get_head_hash($project);
4973 if (!defined $hash) {
4974 $hash = $head;
4976 if (!defined $page) {
4977 $page = 0;
4979 my $refs = git_get_references();
4981 my @commitlist = parse_commits($hash, 101, (100 * $page));
4983 my $paging_nav = format_log_nav('log', $hash, $head, $page, $#commitlist >= 100);
4986 local $action = 'fulllog';
4987 git_header_html();
4989 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4991 if (!@commitlist) {
4992 my %co = parse_commit($hash);
4994 git_print_header_div('summary', $project);
4995 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4997 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4998 for (my $i = 0; $i <= $to; $i++) {
4999 my %co = %{$commitlist[$i]};
5000 next if !%co;
5001 my $commit = $co{'id'};
5002 my $ref = format_ref_marker($refs, $commit);
5003 my %ad = parse_date($co{'author_epoch'});
5004 git_print_header_div('commit',
5005 "<span class=\"age\">$co{'age_string'}</span>" .
5006 esc_html($co{'title'}) . $ref,
5007 $commit);
5008 print "<div class=\"title_text\">\n" .
5009 "<div class=\"log_link\">\n" .
5010 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
5011 " | " .
5012 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
5013 " | " .
5014 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5015 "<br/>\n" .
5016 "</div>\n" .
5017 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
5018 "</div>\n";
5020 print "<div class=\"log_body\">\n";
5021 git_print_log($co{'comment'}, -final_empty_line=> 1);
5022 print "</div>\n";
5024 if ($#commitlist >= 100) {
5025 print "<div class=\"page_nav\">\n";
5026 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
5027 -accesskey => "n", -title => "Alt-n"}, "next");
5028 print "</div>\n";
5030 git_footer_html();
5033 sub git_commit {
5034 $hash ||= $hash_base || "HEAD";
5035 my %co = parse_commit($hash)
5036 or die_error(404, "Unknown commit object");
5037 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5038 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
5040 my $parent = $co{'parent'};
5041 my $parents = $co{'parents'}; # listref
5043 # we need to prepare $formats_nav before any parameter munging
5044 my $formats_nav;
5045 if (!defined $parent) {
5046 # --root commitdiff
5047 $formats_nav .= '(initial)';
5048 } elsif (@$parents == 1) {
5049 # single parent commit
5050 $formats_nav .=
5051 '(parent: ' .
5052 $cgi->a({-href => href(action=>"commit",
5053 hash=>$parent)},
5054 esc_html(substr($parent, 0, 7))) .
5055 ')';
5056 } else {
5057 # merge commit
5058 $formats_nav .=
5059 '(merge: ' .
5060 join(' ', map {
5061 $cgi->a({-href => href(action=>"commit",
5062 hash=>$_)},
5063 esc_html(substr($_, 0, 7)));
5064 } @$parents ) .
5065 ')';
5068 if (!defined $parent) {
5069 $parent = "--root";
5071 my @difftree;
5072 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5073 @diff_opts,
5074 (@$parents <= 1 ? $parent : '-c'),
5075 $hash, "--"
5076 or die_error(500, "Open git-diff-tree failed");
5077 @difftree = map { chomp; $_ } <$fd>;
5078 close $fd or die_error(404, "Reading git-diff-tree failed");
5080 # non-textual hash id's can be cached
5081 my $expires;
5082 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5083 $expires = "+1d";
5085 my $refs = git_get_references();
5086 my $ref = format_ref_marker($refs, $co{'id'});
5088 git_header_html(undef, $expires);
5089 git_print_page_nav('commit', '',
5090 $hash, $co{'tree'}, $hash,
5091 $formats_nav);
5093 if (defined $co{'parent'}) {
5094 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5095 } else {
5096 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5098 print "<div class=\"title_text\">\n" .
5099 "<table class=\"object_header\">\n";
5100 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
5101 "<tr>" .
5102 "<td></td><td> $ad{'rfc2822'}";
5103 if ($ad{'hour_local'} < 6) {
5104 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
5105 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
5106 } else {
5107 printf(" (%02d:%02d %s)",
5108 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
5110 print "</td>" .
5111 "</tr>\n";
5112 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
5113 print "<tr><td></td><td> $cd{'rfc2822'}" .
5114 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
5115 "</td></tr>\n";
5116 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5117 print "<tr>" .
5118 "<td>tree</td>" .
5119 "<td class=\"sha1\">" .
5120 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5121 class => "list"}, $co{'tree'}) .
5122 "</td>" .
5123 "<td class=\"link\">" .
5124 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5125 "tree");
5126 my $snapshot_links = format_snapshot_links($hash);
5127 if (defined $snapshot_links) {
5128 print " | " . $snapshot_links;
5130 print "</td>" .
5131 "</tr>\n";
5133 foreach my $par (@$parents) {
5134 print "<tr>" .
5135 "<td>parent</td>" .
5136 "<td class=\"sha1\">" .
5137 $cgi->a({-href => href(action=>"commit", hash=>$par),
5138 class => "list"}, $par) .
5139 "</td>" .
5140 "<td class=\"link\">" .
5141 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
5142 " | " .
5143 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
5144 "</td>" .
5145 "</tr>\n";
5147 print "</table>".
5148 "</div>\n";
5150 print "<div class=\"page_body\">\n";
5151 git_print_log($co{'comment'});
5152 print "</div>\n";
5154 git_difftree_body(\@difftree, undef, undef, $hash, @$parents);
5156 git_footer_html();
5159 sub git_object {
5160 # object is defined by:
5161 # - hash or hash_base alone
5162 # - hash_base and file_name
5163 my $type;
5165 # - hash or hash_base alone
5166 if ($hash || ($hash_base && !defined $file_name)) {
5167 my $object_id = $hash || $hash_base;
5169 open my $fd, "-|", quote_command(
5170 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
5171 or die_error(404, "Object does not exist");
5172 $type = <$fd>;
5173 chomp $type;
5174 close $fd
5175 or die_error(404, "Object does not exist");
5177 # - hash_base and file_name
5178 } elsif ($hash_base && defined $file_name) {
5179 $file_name =~ s,/+$,,;
5181 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
5182 or die_error(404, "Base object does not exist");
5184 # here errors should not hapen
5185 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
5186 or die_error(500, "Open git-ls-tree failed");
5187 my $line = <$fd>;
5188 close $fd;
5190 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5191 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
5192 die_error(404, "File or directory for given base does not exist");
5194 $type = $2;
5195 $hash = $3;
5196 } else {
5197 die_error(400, "Not enough information to find object");
5200 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5201 hash=>$hash, hash_base=>$hash_base,
5202 file_name=>$file_name),
5203 -status => '302 Found');
5206 sub git_blobdiff {
5207 my $format = shift || 'html';
5209 my $fd;
5210 my @difftree;
5211 my %diffinfo;
5212 my $expires = '+1d';
5213 my ($from, $to);
5215 $file_parent ||= $file_name;
5217 # non-textual hash id's can be cached
5218 if (defined $hash && $hash !~ m/^[0-9a-fA-F]{40}$/) {
5219 $expires = undef;
5220 } elsif (defined $hash_parent && $hash_parent !~ m/^[0-9a-fA-F]{40}$/) {
5221 $expires = undef;
5222 } elsif (defined $hash_base && $hash_base !~ m/^[0-9a-fA-F]{40}$/) {
5223 $expires = undef;
5224 } elsif (defined $hash_parent_base && $hash_parent_base !~ m/^[0-9a-fA-F]{40}$/) {
5225 $expires = undef;
5228 # if hash parameter is missing, read it from the commit.
5229 if (defined $hash_base && defined $file_name && !defined $hash) {
5230 $hash = git_get_hash_by_path($hash_base, $file_name);
5233 if (defined $hash_parent_base && defined $file_parent && !defined $hash_parent) {
5234 $hash_parent = git_get_hash_by_path($hash_parent_base, $file_parent);
5237 if (!defined $hash || ! defined $hash_parent) {
5238 die_error(404, "Missing one of the blob diff parameters");
5241 if (defined $hash_base && defined $file_name) {
5242 $to = $hash_base . ':' . $file_name;
5243 } else {
5244 $to = $hash;
5247 if (defined $hash_parent_base && defined $file_parent) {
5248 $from = $hash_parent_base . ':' . $file_parent;
5249 } else {
5250 $from = $hash_parent;
5253 # fake git-diff-tree raw output
5254 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
5255 $diffinfo{'from_id'} = $hash_parent;
5256 $diffinfo{'to_id'} = $hash;
5257 if (defined $file_name) {
5258 $diffinfo{'status'} = '2';
5259 $diffinfo{'from_file'} = $file_parent;
5260 $diffinfo{'to_file'} = $file_name;
5261 } else { # no filename given
5262 $diffinfo{'status'} = '2';
5263 $diffinfo{'from_file'} = $hash_parent;
5264 $diffinfo{'to_file'} = $hash;
5267 # open patch output
5268 open $fd, "-|", git_cmd(), "diff", @diff_opts, '-p', "--full-index",
5269 ($format eq 'html' ? "--raw" : ()), $from, $to, "--"
5270 or die_error(500, "Open git-diff failed");
5272 # header
5273 if ($format eq 'html') {
5274 my $formats_nav =
5275 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5276 "raw");
5277 git_header_html(undef, $expires);
5278 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5279 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5280 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5281 } else {
5282 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5283 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5285 if (defined $file_name) {
5286 git_print_page_path($file_name, "blob", $hash_base);
5287 } else {
5288 print "<div class=\"page_path\"></div>\n";
5291 } elsif ($format eq 'plain') {
5292 my $patch_file_name = $file_name || $hash;
5293 print $cgi->header(
5294 -type => 'text/plain',
5295 -charset => 'utf-8',
5296 -expires => $expires,
5297 -content_disposition => 'inline; filename="' . "$patch_file_name" . '.patch"');
5299 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5301 } else {
5302 die_error(400, "Unknown blobdiff format");
5305 # patch
5306 if ($format eq 'html') {
5307 print "<div class=\"page_body\">\n";
5309 git_patchset_body($fd, [ \%diffinfo ], undef, undef, $hash_base, $hash_parent_base);
5310 close $fd;
5312 print "</div>\n"; # class="page_body"
5313 git_footer_html();
5315 } else {
5316 while (my $line = <$fd>) {
5317 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5318 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5320 print $line;
5322 last if $line =~ m!^\+\+\+!;
5324 local $/ = undef;
5325 print <$fd>;
5326 close $fd;
5330 sub git_blobdiff_plain {
5331 git_blobdiff('plain');
5334 sub git_commitdiff {
5335 my $format = shift || 'html';
5336 $hash ||= $hash_base || "HEAD";
5337 my %co = parse_commit($hash)
5338 or die_error(404, "Unknown commit object");
5340 # choose format for commitdiff for merge
5341 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5342 $hash_parent = '--cc';
5344 # we need to prepare $formats_nav before almost any parameter munging
5345 my $formats_nav;
5346 if ($format eq 'html') {
5347 $formats_nav =
5348 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5349 "raw");
5351 if (defined $hash_parent &&
5352 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5353 # commitdiff with two commits given
5354 my $hash_parent_short = $hash_parent;
5355 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5356 $hash_parent_short = substr($hash_parent, 0, 7);
5358 $formats_nav .=
5359 ' (from';
5360 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5361 if ($co{'parents'}[$i] eq $hash_parent) {
5362 $formats_nav .= ' parent ' . ($i+1);
5363 last;
5366 $formats_nav .= ': ' .
5367 $cgi->a({-href => href(action=>"commitdiff",
5368 hash=>$hash_parent)},
5369 esc_html($hash_parent_short)) .
5370 ')';
5371 } elsif (!$co{'parent'}) {
5372 # --root commitdiff
5373 $formats_nav .= ' (initial)';
5374 } elsif (scalar @{$co{'parents'}} == 1) {
5375 # single parent commit
5376 $formats_nav .=
5377 ' (parent: ' .
5378 $cgi->a({-href => href(action=>"commitdiff",
5379 hash=>$co{'parent'})},
5380 esc_html(substr($co{'parent'}, 0, 7))) .
5381 ')';
5382 } else {
5383 # merge commit
5384 if ($hash_parent eq '--cc') {
5385 $formats_nav .= ' | ' .
5386 $cgi->a({-href => href(action=>"commitdiff",
5387 hash=>$hash, hash_parent=>'-c')},
5388 'combined');
5389 } else { # $hash_parent eq '-c'
5390 $formats_nav .= ' | ' .
5391 $cgi->a({-href => href(action=>"commitdiff",
5392 hash=>$hash, hash_parent=>'--cc')},
5393 'compact');
5395 $formats_nav .=
5396 ' (merge: ' .
5397 join(' ', map {
5398 $cgi->a({-href => href(action=>"commitdiff",
5399 hash=>$_)},
5400 esc_html(substr($_, 0, 7)));
5401 } @{$co{'parents'}} ) .
5402 ')';
5406 my $hash_parent_param = $hash_parent;
5407 if (!defined $hash_parent_param) {
5408 # --cc for multiple parents, --root for parentless
5409 $hash_parent_param =
5410 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5413 # read commitdiff
5414 my $fd;
5415 my @difftree;
5416 if ($format eq 'html') {
5417 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5418 "--no-commit-id", "--patch-with-raw", "--full-index",
5419 $hash_parent_param, $hash, "--"
5420 or die_error(500, "Open git-diff-tree failed");
5422 while (my $line = <$fd>) {
5423 chomp $line;
5424 # empty line ends raw part of diff-tree output
5425 last unless $line;
5426 push @difftree, scalar parse_difftree_raw_line($line);
5429 } elsif ($format eq 'plain') {
5430 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5431 '-p', $hash_parent_param, $hash, "--"
5432 or die_error(500, "Open git-diff-tree failed");
5434 } else {
5435 die_error(400, "Unknown commitdiff format");
5438 # non-textual hash id's can be cached
5439 my $expires;
5440 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5441 $expires = "+1d";
5444 # write commit message
5445 if ($format eq 'html') {
5446 my $refs = git_get_references();
5447 my $ref = format_ref_marker($refs, $co{'id'});
5449 git_header_html(undef, $expires);
5450 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5451 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5452 git_print_authorship(\%co);
5453 print "<div class=\"page_body\">\n";
5454 if (@{$co{'comment'}} > 1) {
5455 print "<div class=\"log\">\n";
5456 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5457 print "</div>\n"; # class="log"
5460 } elsif ($format eq 'plain') {
5461 my $refs = git_get_references("tags");
5462 my $tagname = git_get_rev_name_tags($hash);
5463 my $filename = basename($project) . "-$hash.patch";
5465 print $cgi->header(
5466 -type => 'text/plain',
5467 -charset => 'utf-8',
5468 -expires => $expires,
5469 -content_disposition => 'inline; filename="' . "$filename" . '"');
5470 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5471 print "From: " . to_utf8($co{'author'}) . "\n";
5472 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5473 print "Subject: " . to_utf8($co{'title'}) . "\n";
5475 print "X-Git-Tag: $tagname\n" if $tagname;
5476 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5478 foreach my $line (@{$co{'comment'}}) {
5479 print to_utf8($line) . "\n";
5481 print "---\n\n";
5484 # write patch
5485 if ($format eq 'html') {
5486 my $use_parents = !defined $hash_parent ||
5487 $hash_parent eq '-c' || $hash_parent eq '--cc';
5488 git_difftree_body(\@difftree, undef, undef, $hash,
5489 $use_parents ? @{$co{'parents'}} : $hash_parent);
5490 print "<br/>\n";
5492 git_patchset_body($fd, \@difftree, undef, undef, $hash,
5493 $use_parents ? @{$co{'parents'}} : $hash_parent);
5494 close $fd;
5495 print "</div>\n"; # class="page_body"
5496 git_footer_html();
5498 } elsif ($format eq 'plain') {
5499 local $/ = undef;
5500 print <$fd>;
5501 close $fd
5502 or print "Reading git-diff-tree failed\n";
5506 sub git_commitdiff_plain {
5507 git_commitdiff('plain');
5510 sub git_history {
5511 if (!defined $hash_base) {
5512 $hash_base = git_get_head_hash($project);
5514 if (!defined $page) {
5515 $page = 0;
5517 my $ftype;
5518 my %co = parse_commit($hash_base)
5519 or die_error(404, "Unknown commit object");
5521 my $refs = git_get_references();
5522 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5524 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5525 $file_name, "--full-history")
5526 or die_error(404, "No such file or directory on given branch");
5528 if (!defined $hash && defined $file_name) {
5529 # some commits could have deleted file in question,
5530 # and not have it in tree, but one of them has to have it
5531 for (my $i = 0; $i <= @commitlist; $i++) {
5532 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5533 last if defined $hash;
5536 if (defined $hash) {
5537 $ftype = git_get_type($hash);
5539 if (!defined $ftype) {
5540 die_error(500, "Unknown type of object");
5543 my $paging_nav = '';
5544 if ($page > 0) {
5545 $paging_nav .=
5546 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5547 file_name=>$file_name)},
5548 "first");
5549 $paging_nav .= " &sdot; " .
5550 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5551 -accesskey => "p", -title => "Alt-p"}, "prev");
5552 } else {
5553 $paging_nav .= "first";
5554 $paging_nav .= " &sdot; prev";
5556 my $next_link = '';
5557 if ($#commitlist >= 100) {
5558 $next_link =
5559 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5560 -accesskey => "n", -title => "Alt-n"}, "next");
5561 $paging_nav .= " &sdot; $next_link";
5562 } else {
5563 $paging_nav .= " &sdot; next";
5566 git_header_html();
5567 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5568 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5569 git_print_page_path($file_name, $ftype, $hash_base);
5571 git_history_body(\@commitlist, 0, 99,
5572 $refs, $hash_base, $ftype, $next_link);
5574 git_footer_html();
5577 sub git_search {
5578 gitweb_check_feature('search') or die_error(403, "Search is disabled");
5579 if (!defined $searchtext) {
5580 die_error(400, "Text field is empty");
5582 if (!defined $hash) {
5583 $hash = git_get_head_hash($project);
5585 my %co = parse_commit($hash);
5586 if (!%co) {
5587 die_error(404, "Unknown commit object");
5589 if (!defined $page) {
5590 $page = 0;
5593 $searchtype ||= 'commit';
5594 if ($searchtype eq 'pickaxe') {
5595 # pickaxe may take all resources of your box and run for several minutes
5596 # with every query - so decide by yourself how public you make this feature
5597 gitweb_check_feature('pickaxe')
5598 or die_error(403, "Pickaxe is disabled");
5600 if ($searchtype eq 'grep') {
5601 gitweb_check_feature('grep')
5602 or die_error(403, "Grep is disabled");
5605 git_header_html();
5607 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5608 my $greptype;
5609 if ($searchtype eq 'commit') {
5610 $greptype = "--grep=";
5611 } elsif ($searchtype eq 'author') {
5612 $greptype = "--author=";
5613 } elsif ($searchtype eq 'committer') {
5614 $greptype = "--committer=";
5616 $greptype .= $searchtext;
5617 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5618 $greptype, '--regexp-ignore-case',
5619 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5621 my $paging_nav = '';
5622 if ($page > 0) {
5623 $paging_nav .=
5624 $cgi->a({-href => href(action=>"search", hash=>$hash,
5625 searchtext=>$searchtext,
5626 searchtype=>$searchtype)},
5627 "first");
5628 $paging_nav .= " &sdot; " .
5629 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5630 -accesskey => "p", -title => "Alt-p"}, "prev");
5631 } else {
5632 $paging_nav .= "first";
5633 $paging_nav .= " &sdot; prev";
5635 my $next_link = '';
5636 if ($#commitlist >= 100) {
5637 $next_link =
5638 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5639 -accesskey => "n", -title => "Alt-n"}, "next");
5640 $paging_nav .= " &sdot; $next_link";
5641 } else {
5642 $paging_nav .= " &sdot; next";
5645 if ($#commitlist >= 100) {
5648 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5649 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5650 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5653 if ($searchtype eq 'pickaxe') {
5654 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5655 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5657 print "<table class=\"pickaxe search\">\n";
5658 my $alternate = 1;
5659 $/ = "\n";
5660 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5661 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5662 ($search_use_regexp ? '--pickaxe-regex' : ());
5663 undef %co;
5664 my @files;
5665 while (my $line = <$fd>) {
5666 chomp $line;
5667 next unless $line;
5669 my %set = parse_difftree_raw_line($line);
5670 if (defined $set{'commit'}) {
5671 # finish previous commit
5672 if (%co) {
5673 print "</td>\n" .
5674 "<td class=\"link\">" .
5675 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5676 " | " .
5677 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5678 print "</td>\n" .
5679 "</tr>\n";
5682 if ($alternate) {
5683 print "<tr class=\"dark\">\n";
5684 } else {
5685 print "<tr class=\"light\">\n";
5687 $alternate ^= 1;
5688 %co = parse_commit($set{'commit'});
5689 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5690 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5691 "<td><i>$author</i></td>\n" .
5692 "<td>" .
5693 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5694 -class => "list subject"},
5695 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5696 } elsif (defined $set{'to_id'}) {
5697 next if ($set{'to_id'} =~ m/^0{40}$/);
5699 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5700 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5701 -class => "list"},
5702 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5703 "<br/>\n";
5706 close $fd;
5708 # finish last commit (warning: repetition!)
5709 if (%co) {
5710 print "</td>\n" .
5711 "<td class=\"link\">" .
5712 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5713 " | " .
5714 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5715 print "</td>\n" .
5716 "</tr>\n";
5719 print "</table>\n";
5722 if ($searchtype eq 'grep') {
5723 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5724 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5726 print "<table class=\"grep_search\">\n";
5727 my $alternate = 1;
5728 my $matches = 0;
5729 $/ = "\n";
5730 open my $fd, "-|", git_cmd(), 'grep', '-n',
5731 $search_use_regexp ? ('-E', '-i') : '-F',
5732 $searchtext, $co{'tree'};
5733 my $lastfile = '';
5734 while (my $line = <$fd>) {
5735 chomp $line;
5736 my ($file, $lno, $ltext, $binary);
5737 last if ($matches++ > 1000);
5738 if ($line =~ /^Binary file (.+) matches$/) {
5739 $file = $1;
5740 $binary = 1;
5741 } else {
5742 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5744 if ($file ne $lastfile) {
5745 $lastfile and print "</td></tr>\n";
5746 if ($alternate++) {
5747 print "<tr class=\"dark\">\n";
5748 } else {
5749 print "<tr class=\"light\">\n";
5751 print "<td class=\"list\">".
5752 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5753 file_name=>"$file"),
5754 -class => "list"}, esc_path($file));
5755 print "</td><td>\n";
5756 $lastfile = $file;
5758 if ($binary) {
5759 print "<div class=\"binary\">Binary file</div>\n";
5760 } else {
5761 $ltext = untabify($ltext);
5762 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5763 $ltext = esc_html($1, -nbsp=>1);
5764 $ltext .= '<span class="match">';
5765 $ltext .= esc_html($2, -nbsp=>1);
5766 $ltext .= '</span>';
5767 $ltext .= esc_html($3, -nbsp=>1);
5768 } else {
5769 $ltext = esc_html($ltext, -nbsp=>1);
5771 print "<div class=\"pre\">" .
5772 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5773 file_name=>"$file").'#l'.$lno,
5774 -class => "linenr"}, sprintf('%4i', $lno))
5775 . ' ' . $ltext . "</div>\n";
5778 if ($lastfile) {
5779 print "</td></tr>\n";
5780 if ($matches > 1000) {
5781 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5783 } else {
5784 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5786 close $fd;
5788 print "</table>\n";
5790 git_footer_html();
5793 sub git_search_help {
5794 git_header_html();
5795 git_print_page_nav('','', $hash,$hash,$hash);
5796 print <<EOT;
5797 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5798 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5799 the pattern entered is recognized as the POSIX extended
5800 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5801 insensitive).</p>
5802 <dl>
5803 <dt><b>commit</b></dt>
5804 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5806 my ($have_grep) = gitweb_check_feature('grep');
5807 if ($have_grep) {
5808 print <<EOT;
5809 <dt><b>grep</b></dt>
5810 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5811 a different one) are searched for the given pattern. On large trees, this search can take
5812 a while and put some strain on the server, so please use it with some consideration. Note that
5813 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5814 case-sensitive.</dd>
5817 print <<EOT;
5818 <dt><b>author</b></dt>
5819 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5820 <dt><b>committer</b></dt>
5821 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5823 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5824 if ($have_pickaxe) {
5825 print <<EOT;
5826 <dt><b>pickaxe</b></dt>
5827 <dd>All commits that caused the string to appear or disappear from any file (changes that
5828 added, removed or "modified" the string) will be listed. This search can take a while and
5829 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5830 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5833 print "</dl>\n";
5834 git_footer_html();
5837 sub git_shortlog {
5838 my $head = git_get_head_hash($project);
5839 if (!defined $hash) {
5840 $hash = $head;
5842 if (!defined $page) {
5843 $page = 0;
5845 my $refs = git_get_references();
5847 my $commit_hash = $hash;
5848 if (defined $hash_parent) {
5849 $commit_hash = "$hash_parent..$hash";
5851 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
5853 my $paging_nav = format_log_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5855 my $next_link = '';
5856 if ($#commitlist >= 100) {
5857 $next_link =
5858 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5859 -accesskey => "n", -title => "Alt-n"}, "next");
5862 git_header_html();
5863 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5864 git_print_header_div('summary', $project);
5866 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5868 git_footer_html();
5871 ## ......................................................................
5872 ## feeds (RSS, Atom; OPML)
5874 sub git_feed {
5875 my $format = shift || 'atom';
5876 my ($have_blame) = gitweb_check_feature('blame');
5878 # Atom: http://www.atomenabled.org/developers/syndication/
5879 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5880 if ($format ne 'rss' && $format ne 'atom') {
5881 die_error(400, "Unknown web feed format");
5884 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5885 my $head = $hash || 'HEAD';
5886 my @commitlist = parse_commits($head, 150, 0, $file_name);
5888 my %latest_commit;
5889 my %latest_date;
5890 my $content_type = "application/$format+xml";
5891 if (defined $cgi->http('HTTP_ACCEPT') &&
5892 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5893 # browser (feed reader) prefers text/xml
5894 $content_type = 'text/xml';
5896 if (defined($commitlist[0])) {
5897 %latest_commit = %{$commitlist[0]};
5898 %latest_date = parse_date($latest_commit{'author_epoch'});
5899 print $cgi->header(
5900 -type => $content_type,
5901 -charset => 'utf-8',
5902 -last_modified => $latest_date{'rfc2822'});
5903 } else {
5904 print $cgi->header(
5905 -type => $content_type,
5906 -charset => 'utf-8');
5909 # Optimization: skip generating the body if client asks only
5910 # for Last-Modified date.
5911 return if ($cgi->request_method() eq 'HEAD');
5913 # header variables
5914 my $title = "$site_name - $project/$action";
5915 my $feed_type = 'log';
5916 if (defined $hash) {
5917 $title .= " - '$hash'";
5918 $feed_type = 'branch log';
5919 if (defined $file_name) {
5920 $title .= " :: $file_name";
5921 $feed_type = 'history';
5923 } elsif (defined $file_name) {
5924 $title .= " - $file_name";
5925 $feed_type = 'history';
5927 $title .= " $feed_type";
5928 my $descr = git_get_project_description($project);
5929 if (defined $descr) {
5930 $descr = esc_html($descr);
5931 } else {
5932 $descr = "$project " .
5933 ($format eq 'rss' ? 'RSS' : 'Atom') .
5934 " feed";
5936 my $owner = git_get_project_owner($project);
5937 $owner = esc_html($owner);
5939 #header
5940 my $alt_url;
5941 if (defined $file_name) {
5942 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5943 } elsif (defined $hash) {
5944 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5945 } else {
5946 $alt_url = href(-full=>1, action=>"summary");
5948 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5949 if ($format eq 'rss') {
5950 print <<XML;
5951 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5952 <channel>
5954 print "<title>$title</title>\n" .
5955 "<link>$alt_url</link>\n" .
5956 "<description>$descr</description>\n" .
5957 "<language>en</language>\n";
5958 } elsif ($format eq 'atom') {
5959 print <<XML;
5960 <feed xmlns="http://www.w3.org/2005/Atom">
5962 print "<title>$title</title>\n" .
5963 "<subtitle>$descr</subtitle>\n" .
5964 '<link rel="alternate" type="text/html" href="' .
5965 $alt_url . '" />' . "\n" .
5966 '<link rel="self" type="' . $content_type . '" href="' .
5967 $cgi->self_url() . '" />' . "\n" .
5968 "<id>" . href(-full=>1) . "</id>\n" .
5969 # use project owner for feed author
5970 '<author><name>'. email_obfuscate($owner) . '</name></author>\n';
5971 if (defined $favicon) {
5972 print "<icon>" . esc_url($favicon) . "</icon>\n";
5974 if (defined $logo_url) {
5975 # not twice as wide as tall: 72 x 27 pixels
5976 print "<logo>" . esc_url($logo) . "</logo>\n";
5978 if (! %latest_date) {
5979 # dummy date to keep the feed valid until commits trickle in:
5980 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5981 } else {
5982 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5986 # contents
5987 for (my $i = 0; $i <= $#commitlist; $i++) {
5988 my %co = %{$commitlist[$i]};
5989 my $commit = $co{'id'};
5990 # we read 150, we always show 30 and the ones more recent than 48 hours
5991 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5992 last;
5994 my %cd = parse_date($co{'author_epoch'});
5996 # get list of changed files
5997 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5998 $co{'parent'} || "--root",
5999 $co{'id'}, "--", (defined $file_name ? $file_name : ())
6000 or next;
6001 my @difftree = map { chomp; $_ } <$fd>;
6002 close $fd
6003 or next;
6005 # print element (entry, item)
6006 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
6007 if ($format eq 'rss') {
6008 print "<item>\n" .
6009 "<title>" . esc_html($co{'title'}) . "</title>\n" .
6010 "<author>" . esc_html($co{'author'}) . "</author>\n" .
6011 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
6012 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
6013 "<link>$co_url</link>\n" .
6014 "<description>" . esc_html($co{'title'}) . "</description>\n" .
6015 "<content:encoded>" .
6016 "<![CDATA[\n";
6017 } elsif ($format eq 'atom') {
6018 print "<entry>\n" .
6019 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
6020 "<updated>$cd{'iso-8601'}</updated>\n" .
6021 "<author>\n" .
6022 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
6023 if ($co{'author_email'}) {
6024 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
6026 print "</author>\n" .
6027 # use committer for contributor
6028 "<contributor>\n" .
6029 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
6030 if ($co{'committer_email'}) {
6031 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
6033 print "</contributor>\n" .
6034 "<published>$cd{'iso-8601'}</published>\n" .
6035 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
6036 "<id>$co_url</id>\n" .
6037 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
6038 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6040 my $comment = $co{'comment'};
6041 print "<pre>\n";
6042 foreach my $line (@$comment) {
6043 $line = esc_html($line);
6044 print "$line\n";
6046 print "</pre><ul>\n";
6047 foreach my $difftree_line (@difftree) {
6048 my %difftree = parse_difftree_raw_line($difftree_line);
6049 next if !$difftree{'from_id'};
6051 my $file = $difftree{'file'} || $difftree{'to_file'};
6053 print "<li>" .
6054 "[" .
6055 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6056 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6057 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6058 file_name=>$file, file_parent=>$difftree{'from_file'}),
6059 -title => "diff"}, 'D');
6060 if ($have_blame) {
6061 print $cgi->a({-href => href(-full=>1, action=>"blame",
6062 file_name=>$file, hash_base=>$commit), -class => "blamelink",
6063 -title => "blame"}, 'B');
6065 # if this is not a feed of a file history
6066 if (!defined $file_name || $file_name ne $file) {
6067 print $cgi->a({-href => href(-full=>1, action=>"history",
6068 file_name=>$file, hash=>$commit),
6069 -title => "history"}, 'H');
6071 $file = esc_path($file);
6072 print "] ".
6073 "$file</li>\n";
6075 if ($format eq 'rss') {
6076 print "</ul>]]>\n" .
6077 "</content:encoded>\n" .
6078 "</item>\n";
6079 } elsif ($format eq 'atom') {
6080 print "</ul>\n</div>\n" .
6081 "</content>\n" .
6082 "</entry>\n";
6086 # end of feed
6087 if ($format eq 'rss') {
6088 print "</channel>\n</rss>\n";
6089 } elsif ($format eq 'atom') {
6090 print "</feed>\n";
6094 sub git_rss {
6095 git_feed('rss');
6098 sub git_atom {
6099 git_feed('atom');
6102 sub git_opml {
6103 my @list = git_get_projects_list();
6105 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
6106 print <<XML;
6107 <?xml version="1.0" encoding="utf-8"?>
6108 <opml version="1.0">
6109 <head>
6110 <title>$site_name OPML Export</title>
6111 </head>
6112 <body>
6113 <outline text="git RSS feeds">
6116 foreach my $pr (@list) {
6117 my %proj = %$pr;
6118 my $head = git_get_head_hash($proj{'path'});
6119 if (!defined $head) {
6120 next;
6122 $git_dir = "$projectroot/$proj{'path'}";
6123 my %co = parse_commit($head);
6124 if (!%co) {
6125 next;
6128 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6129 my $rss = "$my_url?p=$proj{'path'};a=rss";
6130 my $html = "$my_url?p=$proj{'path'};a=summary";
6131 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6133 print <<XML;
6134 </outline>
6135 </body>
6136 </opml>