Merge commit 'refs/top-bases/girocco' into girocco
[git/repo.git] / gitweb / gitweb.perl
blob75e15727b06f61bc83e1d37cd2e093af2bafc2ec
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 sub gitweb_check_feature {
346 my ($name) = @_;
347 return unless exists $feature{$name};
348 my ($sub, $override, @defaults) = (
349 $feature{$name}{'sub'},
350 $feature{$name}{'override'},
351 @{$feature{$name}{'default'}});
352 if (!$override) { return @defaults; }
353 if (!defined $sub) {
354 warn "feature $name is not overrideable";
355 return @defaults;
357 return $sub->(@defaults);
360 sub feature_blame {
361 my ($val) = git_get_project_config('blame', '--bool');
363 if ($val eq 'true') {
364 return 1;
365 } elsif ($val eq 'false') {
366 return 0;
369 return $_[0];
372 sub feature_snapshot {
373 my (@fmts) = @_;
375 my ($val) = git_get_project_config('snapshot');
377 if ($val) {
378 @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
381 return @fmts;
384 sub feature_grep {
385 my ($val) = git_get_project_config('grep', '--bool');
387 if ($val eq 'true') {
388 return (1);
389 } elsif ($val eq 'false') {
390 return (0);
393 return ($_[0]);
396 sub feature_pickaxe {
397 my ($val) = git_get_project_config('pickaxe', '--bool');
399 if ($val eq 'true') {
400 return (1);
401 } elsif ($val eq 'false') {
402 return (0);
405 return ($_[0]);
408 # checking HEAD file with -e is fragile if the repository was
409 # initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
410 # and then pruned.
411 sub check_head_link {
412 my ($dir) = @_;
413 my $headfile = "$dir/HEAD";
414 return ((-e $headfile) ||
415 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
418 sub check_export_ok {
419 my ($dir) = @_;
420 return (check_head_link($dir) &&
421 (!$export_ok || -e "$dir/$export_ok"));
424 # process alternate names for backward compatibility
425 # filter out unsupported (unknown) snapshot formats
426 sub filter_snapshot_fmts {
427 my @fmts = @_;
429 @fmts = map {
430 exists $known_snapshot_format_aliases{$_} ?
431 $known_snapshot_format_aliases{$_} : $_} @fmts;
432 @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
436 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
437 if (-e $GITWEB_CONFIG) {
438 do $GITWEB_CONFIG;
439 } else {
440 our $GITWEB_CONFIG_SYSTEM = $ENV{'GITWEB_CONFIG_SYSTEM'} || "++GITWEB_CONFIG_SYSTEM++";
441 do $GITWEB_CONFIG_SYSTEM if -e $GITWEB_CONFIG_SYSTEM;
444 # version of the core git binary
445 our $git_version = qx("$GIT" --version) =~ m/git version (.*)$/ ? $1 : "unknown";
447 $projects_list ||= $projectroot;
449 # ======================================================================
450 # input validation and dispatch
451 our $action = $cgi->param('a');
452 if (defined $action) {
453 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
454 die_error(400, "Invalid action parameter");
458 # parameters which are pathnames
459 our $project = $cgi->param('p');
460 if (defined $project) {
461 if (!validate_pathname($project) ||
462 !(-d "$projectroot/$project") ||
463 !check_head_link("$projectroot/$project") ||
464 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
465 ($strict_export && !project_in_list($project))) {
466 undef $project;
467 die_error(404, "No such project");
471 our $file_name = $cgi->param('f');
472 if (defined $file_name) {
473 if (!validate_pathname($file_name)) {
474 die_error(400, "Invalid file parameter");
478 our $file_parent = $cgi->param('fp');
479 if (defined $file_parent) {
480 if (!validate_pathname($file_parent)) {
481 die_error(400, "Invalid file parent parameter");
485 # parameters which are refnames
486 our $hash = $cgi->param('h');
487 if (defined $hash) {
488 if (!validate_refname($hash)) {
489 die_error(400, "Invalid hash parameter");
493 our $hash_parent = $cgi->param('hp');
494 if (defined $hash_parent) {
495 if (!validate_refname($hash_parent)) {
496 die_error(400, "Invalid hash parent parameter");
500 our $hash_base = $cgi->param('hb');
501 if (defined $hash_base) {
502 if (!validate_refname($hash_base)) {
503 die_error(400, "Invalid hash base parameter");
507 my %allowed_options = (
508 "--no-merges" => [ qw(rss atom log shortlog history) ],
511 our @extra_options = $cgi->param('opt');
512 if (defined @extra_options) {
513 foreach my $opt (@extra_options) {
514 if (not exists $allowed_options{$opt}) {
515 die_error(400, "Invalid option parameter");
517 if (not grep(/^$action$/, @{$allowed_options{$opt}})) {
518 die_error(400, "Invalid option parameter for this action");
523 our $hash_parent_base = $cgi->param('hpb');
524 if (defined $hash_parent_base) {
525 if (!validate_refname($hash_parent_base)) {
526 die_error(400, "Invalid hash parent base parameter");
530 # other parameters
531 our $page = $cgi->param('pg');
532 if (defined $page) {
533 if ($page =~ m/[^0-9]/) {
534 die_error(400, "Invalid page parameter");
538 our $searchtype = $cgi->param('st');
539 if (defined $searchtype) {
540 if ($searchtype =~ m/[^a-z]/) {
541 die_error(400, "Invalid searchtype parameter");
545 our $search_use_regexp = $cgi->param('sr');
547 our $searchtext = $cgi->param('s');
548 our $search_regexp;
549 if (defined $searchtext) {
550 if (length($searchtext) < 2) {
551 die_error(403, "At least two characters are required for search parameter");
553 $search_regexp = $search_use_regexp ? $searchtext : quotemeta $searchtext;
556 # now read PATH_INFO and use it as alternative to parameters
557 sub evaluate_path_info {
558 return if defined $project;
559 my $path_info = $ENV{"PATH_INFO"};
560 return if !$path_info;
561 $path_info =~ s,^/+,,;
562 return if !$path_info;
563 # find which part of PATH_INFO is project
564 $project = $path_info;
565 $project =~ s,/+$,,;
566 while ($project && !check_head_link("$projectroot/$project")) {
567 $project =~ s,/*[^/]*$,,;
569 # validate project
570 $project = validate_pathname($project);
571 if (!$project ||
572 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
573 ($strict_export && !project_in_list($project))) {
574 undef $project;
575 return;
577 # do not change any parameters if an action is given using the query string
578 return if $action;
579 $path_info =~ s,^\Q$project\E/*,,;
580 my ($refname, $pathname) = split(/:/, $path_info, 2);
581 if (defined $pathname) {
582 # we got "project.git/branch:filename" or "project.git/branch:dir/"
583 # we could use git_get_type(branch:pathname), but it needs $git_dir
584 $pathname =~ s,^/+,,;
585 if (!$pathname || substr($pathname, -1) eq "/") {
586 $action ||= "tree";
587 $pathname =~ s,/$,,;
588 } else {
589 $action ||= "blob_plain";
591 $hash_base ||= validate_refname($refname);
592 $file_name ||= validate_pathname($pathname);
593 } elsif (defined $refname) {
594 # we got "project.git/branch"
595 $action ||= "shortlog";
596 $hash ||= validate_refname($refname);
599 evaluate_path_info();
601 # path to the current git repository
602 our $git_dir;
603 $git_dir = "$projectroot/$project" if $project;
605 # dispatch
606 my %actions = (
607 "blame" => \&git_blame,
608 "blame_incremental" => \&git_blame_incremental,
609 "blame_data" => \&git_blame_data,
610 "blobdiff" => \&git_blobdiff,
611 "blobdiff_plain" => \&git_blobdiff_plain,
612 "blob" => \&git_blob,
613 "blob_plain" => \&git_blob_plain,
614 "commitdiff" => \&git_commitdiff,
615 "commitdiff_plain" => \&git_commitdiff_plain,
616 "commit" => \&git_commit,
617 "forks" => \&git_forks,
618 "heads" => \&git_heads,
619 "history" => \&git_history,
620 "log" => \&git_log,
621 "rss" => \&git_rss,
622 "atom" => \&git_atom,
623 "search" => \&git_search,
624 "search_help" => \&git_search_help,
625 "shortlog" => \&git_shortlog,
626 "summary" => \&git_summary,
627 "tag" => \&git_tag,
628 "tags" => \&git_tags,
629 "tree" => \&git_tree,
630 "snapshot" => \&git_snapshot,
631 "object" => \&git_object,
632 # those below don't need $project
633 "opml" => \&git_opml,
634 "project_list" => \&git_project_list,
635 "project_index" => \&git_project_index,
638 if (!defined $action) {
639 if (defined $hash) {
640 $action = git_get_type($hash);
641 } elsif (defined $hash_base && defined $file_name) {
642 $action = git_get_type("$hash_base:$file_name");
643 } elsif (defined $project) {
644 $action = 'summary';
645 } else {
646 $action = 'project_list';
649 if (!defined($actions{$action})) {
650 die_error(400, "Unknown action");
652 if ($action !~ m/^(opml|project_list|project_index)$/ &&
653 !$project) {
654 die_error(400, "Project needed");
656 $actions{$action}->();
657 exit;
659 ## ======================================================================
660 ## action links
662 sub href (%) {
663 my %params = @_;
664 # default is to use -absolute url() i.e. $my_uri
665 my $href = $params{-full} ? $my_url : $my_uri;
667 # XXX: Warning: If you touch this, check the search form for updating,
668 # too.
670 my @mapping = (
671 project => "p",
672 action => "a",
673 file_name => "f",
674 file_parent => "fp",
675 hash => "h",
676 hash_parent => "hp",
677 hash_base => "hb",
678 hash_parent_base => "hpb",
679 page => "pg",
680 order => "o",
681 searchtext => "s",
682 searchtype => "st",
683 snapshot_format => "sf",
684 extra_options => "opt",
685 search_use_regexp => "sr",
687 my %mapping = @mapping;
689 $params{'project'} = $project unless exists $params{'project'};
691 if ($params{-replay}) {
692 while (my ($name, $symbol) = each %mapping) {
693 if (!exists $params{$name}) {
694 # to allow for multivalued params we use arrayref form
695 $params{$name} = [ $cgi->param($symbol) ];
700 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
701 if ($use_pathinfo) {
702 # use PATH_INFO for project name
703 $href .= "/".esc_url($params{'project'}) if defined $params{'project'};
704 delete $params{'project'};
706 # Summary just uses the project path URL
707 if (defined $params{'action'} && $params{'action'} eq 'summary') {
708 delete $params{'action'};
712 # now encode the parameters explicitly
713 my @result = ();
714 for (my $i = 0; $i < @mapping; $i += 2) {
715 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
716 if (defined $params{$name}) {
717 if (ref($params{$name}) eq "ARRAY") {
718 foreach my $par (@{$params{$name}}) {
719 push @result, $symbol . "=" . esc_param($par);
721 } else {
722 push @result, $symbol . "=" . esc_param($params{$name});
726 $href .= "?" . join(';', @result) if $params{-partial_query} or scalar @result;
728 return $href;
732 ## ======================================================================
733 ## validation, quoting/unquoting and escaping
735 sub validate_pathname {
736 my $input = shift || return undef;
738 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
739 # at the beginning, at the end, and between slashes.
740 # also this catches doubled slashes
741 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
742 return undef;
744 # no null characters
745 if ($input =~ m!\0!) {
746 return undef;
748 return $input;
751 sub validate_refname {
752 my $input = shift || return undef;
754 # textual hashes are O.K.
755 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
756 return $input;
758 # it must be correct pathname
759 $input = validate_pathname($input)
760 or return undef;
761 # restrictions on ref name according to git-check-ref-format
762 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
763 return undef;
765 return $input;
768 # decode sequences of octets in utf8 into Perl's internal form,
769 # which is utf-8 with utf8 flag set if needed. gitweb writes out
770 # in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning
771 sub to_utf8 {
772 my $str = shift;
773 if (utf8::valid($str)) {
774 utf8::decode($str);
775 return $str;
776 } else {
777 return decode($fallback_encoding, $str, Encode::FB_DEFAULT);
781 # quote unsafe chars, but keep the slash, even when it's not
782 # correct, but quoted slashes look too horrible in bookmarks
783 sub esc_param {
784 my $str = shift;
785 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
786 $str =~ s/\+/%2B/g;
787 $str =~ s/ /\+/g;
788 return $str;
791 # quote unsafe chars in whole URL, so some charactrs cannot be quoted
792 sub esc_url {
793 my $str = shift;
794 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
795 $str =~ s/\+/%2B/g;
796 $str =~ s/ /\+/g;
797 return $str;
800 # replace invalid utf8 character with SUBSTITUTION sequence
801 sub esc_html ($;%) {
802 my $str = shift;
803 my %opts = @_;
805 $str = to_utf8($str);
806 $str = $cgi->escapeHTML($str);
807 if ($opts{'-nbsp'}) {
808 $str =~ s/ /&nbsp;/g;
810 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
811 return $str;
814 # quote control characters and escape filename to HTML
815 sub esc_path {
816 my $str = shift;
817 my %opts = @_;
819 $str = to_utf8($str);
820 $str = $cgi->escapeHTML($str);
821 if ($opts{'-nbsp'}) {
822 $str =~ s/ /&nbsp;/g;
824 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
825 return $str;
828 # Make control characters "printable", using character escape codes (CEC)
829 sub quot_cec {
830 my $cntrl = shift;
831 my %opts = @_;
832 my %es = ( # character escape codes, aka escape sequences
833 "\t" => '\t', # tab (HT)
834 "\n" => '\n', # line feed (LF)
835 "\r" => '\r', # carrige return (CR)
836 "\f" => '\f', # form feed (FF)
837 "\b" => '\b', # backspace (BS)
838 "\a" => '\a', # alarm (bell) (BEL)
839 "\e" => '\e', # escape (ESC)
840 "\013" => '\v', # vertical tab (VT)
841 "\000" => '\0', # nul character (NUL)
843 my $chr = ( (exists $es{$cntrl})
844 ? $es{$cntrl}
845 : sprintf('\%2x', ord($cntrl)) );
846 if ($opts{-nohtml}) {
847 return $chr;
848 } else {
849 return "<span class=\"cntrl\">$chr</span>";
853 # Alternatively use unicode control pictures codepoints,
854 # Unicode "printable representation" (PR)
855 sub quot_upr {
856 my $cntrl = shift;
857 my %opts = @_;
859 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
860 if ($opts{-nohtml}) {
861 return $chr;
862 } else {
863 return "<span class=\"cntrl\">$chr</span>";
867 # git may return quoted and escaped filenames
868 sub unquote {
869 my $str = shift;
871 sub unq {
872 my $seq = shift;
873 my %es = ( # character escape codes, aka escape sequences
874 't' => "\t", # tab (HT, TAB)
875 'n' => "\n", # newline (NL)
876 'r' => "\r", # return (CR)
877 'f' => "\f", # form feed (FF)
878 'b' => "\b", # backspace (BS)
879 'a' => "\a", # alarm (bell) (BEL)
880 'e' => "\e", # escape (ESC)
881 'v' => "\013", # vertical tab (VT)
884 if ($seq =~ m/^[0-7]{1,3}$/) {
885 # octal char sequence
886 return chr(oct($seq));
887 } elsif (exists $es{$seq}) {
888 # C escape sequence, aka character escape code
889 return $es{$seq};
891 # quoted ordinary character
892 return $seq;
895 if ($str =~ m/^"(.*)"$/) {
896 # needs unquoting
897 $str = $1;
898 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
900 return $str;
903 # escape tabs (convert tabs to spaces)
904 sub untabify {
905 my $line = shift;
907 while ((my $pos = index($line, "\t")) != -1) {
908 if (my $count = (8 - ($pos % 8))) {
909 my $spaces = ' ' x $count;
910 $line =~ s/\t/$spaces/;
914 return $line;
917 sub project_in_list {
918 my $project = shift;
919 my @list = git_get_projects_list();
920 return @list && scalar(grep { $_->{'path'} eq $project } @list);
923 ## ----------------------------------------------------------------------
924 ## HTML aware string manipulation
926 # Try to chop given string on a word boundary between position
927 # $len and $len+$add_len. If there is no word boundary there,
928 # chop at $len+$add_len. Do not chop if chopped part plus ellipsis
929 # (marking chopped part) would be longer than given string.
930 sub chop_str {
931 my $str = shift;
932 my $len = shift;
933 my $add_len = shift || 10;
934 my $where = shift || 'right'; # 'left' | 'center' | 'right'
936 # Make sure perl knows it is utf8 encoded so we don't
937 # cut in the middle of a utf8 multibyte char.
938 $str = to_utf8($str);
940 # allow only $len chars, but don't cut a word if it would fit in $add_len
941 # if it doesn't fit, cut it if it's still longer than the dots we would add
942 # remove chopped character entities entirely
944 # when chopping in the middle, distribute $len into left and right part
945 # return early if chopping wouldn't make string shorter
946 if ($where eq 'center') {
947 return $str if ($len + 5 >= length($str)); # filler is length 5
948 $len = int($len/2);
949 } else {
950 return $str if ($len + 4 >= length($str)); # filler is length 4
953 # regexps: ending and beginning with word part up to $add_len
954 my $endre = qr/.{$len}\w{0,$add_len}/;
955 my $begre = qr/\w{0,$add_len}.{$len}/;
957 if ($where eq 'left') {
958 $str =~ m/^(.*?)($begre)$/;
959 my ($lead, $body) = ($1, $2);
960 if (length($lead) > 4) {
961 $body =~ s/^[^;]*;// if ($lead =~ m/&[^;]*$/);
962 $lead = " ...";
964 return "$lead$body";
966 } elsif ($where eq 'center') {
967 $str =~ m/^($endre)(.*)$/;
968 my ($left, $str) = ($1, $2);
969 $str =~ m/^(.*?)($begre)$/;
970 my ($mid, $right) = ($1, $2);
971 if (length($mid) > 5) {
972 $left =~ s/&[^;]*$//;
973 $right =~ s/^[^;]*;// if ($mid =~ m/&[^;]*$/);
974 $mid = " ... ";
976 return "$left$mid$right";
978 } else {
979 $str =~ m/^($endre)(.*)$/;
980 my $body = $1;
981 my $tail = $2;
982 if (length($tail) > 4) {
983 $body =~ s/&[^;]*$//;
984 $tail = "... ";
986 return "$body$tail";
990 # takes the same arguments as chop_str, but also wraps a <span> around the
991 # result with a title attribute if it does get chopped. Additionally, the
992 # string is HTML-escaped.
993 sub chop_and_escape_str {
994 my ($str) = @_;
996 my $chopped = chop_str(@_);
997 if ($chopped eq $str) {
998 return esc_html($chopped);
999 } else {
1000 $str =~ s/([[:cntrl:]])/?/g;
1001 return $cgi->span({-title=>$str}, esc_html($chopped));
1005 ## ----------------------------------------------------------------------
1006 ## functions returning short strings
1008 # CSS class for given age value (in seconds)
1009 sub age_class {
1010 my $age = shift;
1012 if (!defined $age) {
1013 return "noage";
1014 } elsif ($age < 60*60*2) {
1015 return "age0";
1016 } elsif ($age < 60*60*24*2) {
1017 return "age1";
1018 } else {
1019 return "age2";
1023 # convert age in seconds to "nn units ago" string
1024 sub age_string {
1025 my $age = shift;
1026 my $age_str;
1028 if ($age > 60*60*24*365*2) {
1029 $age_str = (int $age/60/60/24/365);
1030 $age_str .= " years ago";
1031 } elsif ($age > 60*60*24*(365/12)*2) {
1032 $age_str = int $age/60/60/24/(365/12);
1033 $age_str .= " months ago";
1034 } elsif ($age > 60*60*24*7*2) {
1035 $age_str = int $age/60/60/24/7;
1036 $age_str .= " weeks ago";
1037 } elsif ($age > 60*60*24*2) {
1038 $age_str = int $age/60/60/24;
1039 $age_str .= " days ago";
1040 } elsif ($age > 60*60*2) {
1041 $age_str = int $age/60/60;
1042 $age_str .= " hours ago";
1043 } elsif ($age > 60*2) {
1044 $age_str = int $age/60;
1045 $age_str .= " min ago";
1046 } elsif ($age > 2) {
1047 $age_str = int $age;
1048 $age_str .= " sec ago";
1049 } else {
1050 $age_str .= " right now";
1052 return $age_str;
1055 use constant {
1056 S_IFINVALID => 0030000,
1057 S_IFGITLINK => 0160000,
1060 # submodule/subproject, a commit object reference
1061 sub S_ISGITLINK($) {
1062 my $mode = shift;
1064 return (($mode & S_IFMT) == S_IFGITLINK)
1067 # convert file mode in octal to symbolic file mode string
1068 sub mode_str {
1069 my $mode = oct shift;
1071 if (S_ISGITLINK($mode)) {
1072 return 'm---------';
1073 } elsif (S_ISDIR($mode & S_IFMT)) {
1074 return 'drwxr-xr-x';
1075 } elsif (S_ISLNK($mode)) {
1076 return 'lrwxrwxrwx';
1077 } elsif (S_ISREG($mode)) {
1078 # git cares only about the executable bit
1079 if ($mode & S_IXUSR) {
1080 return '-rwxr-xr-x';
1081 } else {
1082 return '-rw-r--r--';
1084 } else {
1085 return '----------';
1089 # convert file mode in octal to file type string
1090 sub file_type {
1091 my $mode = shift;
1093 if ($mode !~ m/^[0-7]+$/) {
1094 return $mode;
1095 } else {
1096 $mode = oct $mode;
1099 if (S_ISGITLINK($mode)) {
1100 return "submodule";
1101 } elsif (S_ISDIR($mode & S_IFMT)) {
1102 return "directory";
1103 } elsif (S_ISLNK($mode)) {
1104 return "symlink";
1105 } elsif (S_ISREG($mode)) {
1106 return "file";
1107 } else {
1108 return "unknown";
1112 # convert file mode in octal to file type description string
1113 sub file_type_long {
1114 my $mode = shift;
1116 if ($mode !~ m/^[0-7]+$/) {
1117 return $mode;
1118 } else {
1119 $mode = oct $mode;
1122 if (S_ISGITLINK($mode)) {
1123 return "submodule";
1124 } elsif (S_ISDIR($mode & S_IFMT)) {
1125 return "directory";
1126 } elsif (S_ISLNK($mode)) {
1127 return "symlink";
1128 } elsif (S_ISREG($mode)) {
1129 if ($mode & S_IXUSR) {
1130 return "executable";
1131 } else {
1132 return "file";
1134 } else {
1135 return "unknown";
1140 ## ----------------------------------------------------------------------
1141 ## functions returning short HTML fragments, or transforming HTML fragments
1142 ## which don't belong to other sections
1144 # format line of commit message.
1145 sub format_log_line_html {
1146 my $line = shift;
1148 $line = esc_html($line, -nbsp=>1);
1149 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
1150 my $hash_text = $1;
1151 my $link =
1152 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
1153 -class => "text"}, $hash_text);
1154 $line =~ s/$hash_text/$link/;
1156 return $line;
1159 # format marker of refs pointing to given object
1161 # the destination action is chosen based on object type and current context:
1162 # - for annotated tags, we choose the tag view unless it's the current view
1163 # already, in which case we go to shortlog view
1164 # - for other refs, we keep the current view if we're in history, shortlog or
1165 # log view, and select shortlog otherwise
1166 sub format_ref_marker {
1167 my ($refs, $id) = @_;
1168 my $markers = '';
1170 if (defined $refs->{$id}) {
1171 foreach my $ref (@{$refs->{$id}}) {
1172 # this code exploits the fact that non-lightweight tags are the
1173 # only indirect objects, and that they are the only objects for which
1174 # we want to use tag instead of shortlog as action
1175 my ($type, $name) = qw();
1176 my $indirect = ($ref =~ s/\^\{\}$//);
1177 # e.g. tags/v2.6.11 or heads/next
1178 if ($ref =~ m!^(.*?)s?/(.*)$!) {
1179 $type = $1;
1180 $name = $2;
1181 } else {
1182 $type = "ref";
1183 $name = $ref;
1186 my $class = $type;
1187 $class .= " indirect" if $indirect;
1189 my $dest_action = "shortlog";
1191 if ($indirect) {
1192 $dest_action = "tag" unless $action eq "tag";
1193 } elsif ($action =~ /^(history|(short)?log)$/) {
1194 $dest_action = $action;
1197 my $dest = "";
1198 $dest .= "refs/" unless $ref =~ m!^refs/!;
1199 $dest .= $ref;
1201 my $link = $cgi->a({
1202 -href => href(
1203 action=>$dest_action,
1204 hash=>$dest
1205 )}, $name);
1207 $markers .= " <span class=\"$class\" title=\"$ref\">" .
1208 $link . "</span>";
1212 if ($markers) {
1213 return ' <span class="refs">'. $markers . '</span>';
1214 } else {
1215 return "";
1219 # format, perhaps shortened and with markers, title line
1220 sub format_subject_html {
1221 my ($long, $short, $href, $extra) = @_;
1222 $extra = '' unless defined($extra);
1224 if (length($short) < length($long)) {
1225 return $cgi->a({-href => $href, -class => "list subject",
1226 -title => to_utf8($long)},
1227 esc_html($short) . $extra);
1228 } else {
1229 return $cgi->a({-href => $href, -class => "list subject"},
1230 esc_html($long) . $extra);
1234 # format git diff header line, i.e. "diff --(git|combined|cc) ..."
1235 sub format_git_diff_header_line {
1236 my $line = shift;
1237 my $diffinfo = shift;
1238 my ($from, $to) = @_;
1240 if ($diffinfo->{'nparents'}) {
1241 # combined diff
1242 $line =~ s!^(diff (.*?) )"?.*$!$1!;
1243 if ($to->{'href'}) {
1244 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1245 esc_path($to->{'file'}));
1246 } else { # file was deleted (no href)
1247 $line .= esc_path($to->{'file'});
1249 } else {
1250 # "ordinary" diff
1251 $line =~ s!^(diff (.*?) )"?a/.*$!$1!;
1252 if ($from->{'href'}) {
1253 $line .= $cgi->a({-href => $from->{'href'}, -class => "path"},
1254 'a/' . esc_path($from->{'file'}));
1255 } else { # file was added (no href)
1256 $line .= 'a/' . esc_path($from->{'file'});
1258 $line .= ' ';
1259 if ($to->{'href'}) {
1260 $line .= $cgi->a({-href => $to->{'href'}, -class => "path"},
1261 'b/' . esc_path($to->{'file'}));
1262 } else { # file was deleted
1263 $line .= 'b/' . esc_path($to->{'file'});
1267 return "<div class=\"diff header\">$line</div>\n";
1270 # format extended diff header line, before patch itself
1271 sub format_extended_diff_header_line {
1272 my $line = shift;
1273 my $diffinfo = shift;
1274 my ($from, $to) = @_;
1276 # match <path>
1277 if ($line =~ s!^((copy|rename) from ).*$!$1! && $from->{'href'}) {
1278 $line .= $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1279 esc_path($from->{'file'}));
1281 if ($line =~ s!^((copy|rename) to ).*$!$1! && $to->{'href'}) {
1282 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1283 esc_path($to->{'file'}));
1285 # match single <mode>
1286 if ($line =~ m/\s(\d{6})$/) {
1287 $line .= '<span class="info"> (' .
1288 file_type_long($1) .
1289 ')</span>';
1291 # match <hash>
1292 if ($line =~ m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {
1293 # can match only for combined diff
1294 $line = 'index ';
1295 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1296 if ($from->{'href'}[$i]) {
1297 $line .= $cgi->a({-href=>$from->{'href'}[$i],
1298 -class=>"hash"},
1299 substr($diffinfo->{'from_id'}[$i],0,7));
1300 } else {
1301 $line .= '0' x 7;
1303 # separator
1304 $line .= ',' if ($i < $diffinfo->{'nparents'} - 1);
1306 $line .= '..';
1307 if ($to->{'href'}) {
1308 $line .= $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1309 substr($diffinfo->{'to_id'},0,7));
1310 } else {
1311 $line .= '0' x 7;
1314 } elsif ($line =~ m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {
1315 # can match only for ordinary diff
1316 my ($from_link, $to_link);
1317 if ($from->{'href'}) {
1318 $from_link = $cgi->a({-href=>$from->{'href'}, -class=>"hash"},
1319 substr($diffinfo->{'from_id'},0,7));
1320 } else {
1321 $from_link = '0' x 7;
1323 if ($to->{'href'}) {
1324 $to_link = $cgi->a({-href=>$to->{'href'}, -class=>"hash"},
1325 substr($diffinfo->{'to_id'},0,7));
1326 } else {
1327 $to_link = '0' x 7;
1329 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
1330 $line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
1333 return $line . "<br/>\n";
1336 # format from-file/to-file diff header
1337 sub format_diff_from_to_header {
1338 my ($from_line, $to_line, $diffinfo, $from, $to, @parents) = @_;
1339 my $line;
1340 my $result = '';
1342 $line = $from_line;
1343 #assert($line =~ m/^---/) if DEBUG;
1344 # no extra formatting for "^--- /dev/null"
1345 if (! $diffinfo->{'nparents'}) {
1346 # ordinary (single parent) diff
1347 if ($line =~ m!^--- "?a/!) {
1348 if ($from->{'href'}) {
1349 $line = '--- a/' .
1350 $cgi->a({-href=>$from->{'href'}, -class=>"path"},
1351 esc_path($from->{'file'}));
1352 } else {
1353 $line = '--- a/' .
1354 esc_path($from->{'file'});
1357 $result .= qq!<div class="diff from_file">$line</div>\n!;
1359 } else {
1360 # combined diff (merge commit)
1361 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
1362 if ($from->{'href'}[$i]) {
1363 $line = '--- ' .
1364 $cgi->a({-href=>href(action=>"blobdiff",
1365 hash_parent=>$diffinfo->{'from_id'}[$i],
1366 hash_parent_base=>$parents[$i],
1367 file_parent=>$diffinfo->{'from_prefix'}.$from->{'file'}[$i],
1368 hash=>$diffinfo->{'to_id'},
1369 hash_base=>$hash,
1370 file_name=>$diffinfo->{'to_prefix'}.$to->{'file'}),
1371 -class=>"path",
1372 -title=>"diff" . ($i+1)},
1373 $i+1) .
1374 '/' .
1375 $cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},
1376 esc_path($from->{'file'}[$i]));
1377 } else {
1378 $line = '--- /dev/null';
1380 $result .= qq!<div class="diff from_file">$line</div>\n!;
1384 $line = $to_line;
1385 #assert($line =~ m/^\+\+\+/) if DEBUG;
1386 # no extra formatting for "^+++ /dev/null"
1387 if ($line =~ m!^\+\+\+ "?b/!) {
1388 if ($to->{'href'}) {
1389 $line = '+++ b/' .
1390 $cgi->a({-href=>$to->{'href'}, -class=>"path"},
1391 esc_path($to->{'file'}));
1392 } else {
1393 $line = '+++ b/' .
1394 esc_path($to->{'file'});
1397 $result .= qq!<div class="diff to_file">$line</div>\n!;
1399 return $result;
1402 # create note for patch simplified by combined diff
1403 sub format_diff_cc_simplified {
1404 my ($diffinfo, @parents) = @_;
1405 my $result = '';
1407 $result .= "<div class=\"diff header\">" .
1408 "diff --cc ";
1409 if (!is_deleted($diffinfo)) {
1410 $result .= $cgi->a({-href => href(action=>"blob",
1411 hash_base=>$hash,
1412 hash=>$diffinfo->{'to_id'},
1413 file_name=>$diffinfo->{'to_prefix'}.$diffinfo->{'to_file'}),
1414 -class => "path"},
1415 esc_path($diffinfo->{'to_file'}));
1416 } else {
1417 $result .= esc_path($diffinfo->{'to_file'});
1419 $result .= "</div>\n" . # class="diff header"
1420 "<div class=\"diff nodifferences\">" .
1421 "Simple merge" .
1422 "</div>\n"; # class="diff nodifferences"
1424 return $result;
1427 # format patch (diff) line (not to be used for diff headers)
1428 sub format_diff_line {
1429 my $line = shift;
1430 my ($from, $to) = @_;
1431 my $diff_class = "";
1433 chomp $line;
1435 if ($from && $to && ref($from->{'href'}) eq "ARRAY") {
1436 # combined diff
1437 my $prefix = substr($line, 0, scalar @{$from->{'href'}});
1438 if ($line =~ m/^\@{3}/) {
1439 $diff_class = " chunk_header";
1440 } elsif ($line =~ m/^\\/) {
1441 $diff_class = " incomplete";
1442 } elsif ($prefix =~ tr/+/+/) {
1443 $diff_class = " add";
1444 } elsif ($prefix =~ tr/-/-/) {
1445 $diff_class = " rem";
1447 } else {
1448 # assume ordinary diff
1449 my $char = substr($line, 0, 1);
1450 if ($char eq '+') {
1451 $diff_class = " add";
1452 } elsif ($char eq '-') {
1453 $diff_class = " rem";
1454 } elsif ($char eq '@') {
1455 $diff_class = " chunk_header";
1456 } elsif ($char eq "\\") {
1457 $diff_class = " incomplete";
1460 $line = untabify($line);
1461 if ($from && $to && $line =~ m/^\@{2} /) {
1462 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
1463 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
1465 $from_lines = 0 unless defined $from_lines;
1466 $to_lines = 0 unless defined $to_lines;
1468 if ($from->{'href'}) {
1469 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
1470 -class=>"list"}, $from_text);
1472 if ($to->{'href'}) {
1473 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
1474 -class=>"list"}, $to_text);
1476 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
1477 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1478 return "<div class=\"diff$diff_class\">$line</div>\n";
1479 } elsif ($from && $to && $line =~ m/^\@{3}/) {
1480 my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
1481 my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
1483 @from_text = split(' ', $ranges);
1484 for (my $i = 0; $i < @from_text; ++$i) {
1485 ($from_start[$i], $from_nlines[$i]) =
1486 (split(',', substr($from_text[$i], 1)), 0);
1489 $to_text = pop @from_text;
1490 $to_start = pop @from_start;
1491 $to_nlines = pop @from_nlines;
1493 $line = "<span class=\"chunk_info\">$prefix ";
1494 for (my $i = 0; $i < @from_text; ++$i) {
1495 if ($from->{'href'}[$i]) {
1496 $line .= $cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",
1497 -class=>"list"}, $from_text[$i]);
1498 } else {
1499 $line .= $from_text[$i];
1501 $line .= " ";
1503 if ($to->{'href'}) {
1504 $line .= $cgi->a({-href=>"$to->{'href'}#l$to_start",
1505 -class=>"list"}, $to_text);
1506 } else {
1507 $line .= $to_text;
1509 $line .= " $prefix</span>" .
1510 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
1511 return "<div class=\"diff$diff_class\">$line</div>\n";
1513 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1516 # Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1517 # linked. Pass the hash of the tree/commit to snapshot.
1518 sub format_snapshot_links {
1519 my ($hash) = @_;
1520 my @snapshot_fmts = gitweb_check_feature('snapshot');
1521 @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts);
1522 my $num_fmts = @snapshot_fmts;
1523 if ($num_fmts > 1) {
1524 # A parenthesized list of links bearing format names.
1525 # e.g. "snapshot (_tar.gz_ _zip_)"
1526 return "snapshot (" . join(' ', map
1527 $cgi->a({
1528 -href => href(
1529 action=>"snapshot",
1530 hash=>$hash,
1531 snapshot_format=>$_
1533 }, $known_snapshot_formats{$_}{'display'})
1534 , @snapshot_fmts) . ")";
1535 } elsif ($num_fmts == 1) {
1536 # A single "snapshot" link whose tooltip bears the format name.
1537 # i.e. "_snapshot_"
1538 my ($fmt) = @snapshot_fmts;
1539 return
1540 $cgi->a({
1541 -href => href(
1542 action=>"snapshot",
1543 hash=>$hash,
1544 snapshot_format=>$fmt
1546 -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1547 }, "snapshot");
1548 } else { # $num_fmts == 0
1549 return undef;
1553 ## ......................................................................
1554 ## functions returning values to be passed, perhaps after some
1555 ## transformation, to other functions; e.g. returning arguments to href()
1557 # returns hash to be passed to href to generate gitweb URL
1558 # in -title key it returns description of link
1559 sub get_feed_info {
1560 my $format = shift || 'Atom';
1561 my %res = (action => lc($format));
1563 # feed links are possible only for project views
1564 return unless (defined $project);
1565 # some views should link to OPML, or to generic project feed,
1566 # or don't have specific feed yet (so they should use generic)
1567 return if ($action =~ /^(?:tags|heads|forks|tag|search)$/x);
1569 my $branch;
1570 # branches refs uses 'refs/heads/' prefix (fullname) to differentiate
1571 # from tag links; this also makes possible to detect branch links
1572 if ((defined $hash_base && $hash_base =~ m!^refs/heads/(.*)$!) ||
1573 (defined $hash && $hash =~ m!^refs/heads/(.*)$!)) {
1574 $branch = $1;
1576 # find log type for feed description (title)
1577 my $type = 'log';
1578 if (defined $file_name) {
1579 $type = "history of $file_name";
1580 $type .= "/" if ($action eq 'tree');
1581 $type .= " on '$branch'" if (defined $branch);
1582 } else {
1583 $type = "log of $branch" if (defined $branch);
1586 $res{-title} = $type;
1587 $res{'hash'} = (defined $branch ? "refs/heads/$branch" : undef);
1588 $res{'file_name'} = $file_name;
1590 return %res;
1593 ## ----------------------------------------------------------------------
1594 ## git utility subroutines, invoking git commands
1596 # returns path to the core git executable and the --git-dir parameter as list
1597 sub git_cmd {
1598 return $GIT, '--git-dir='.$git_dir;
1601 # quote the given arguments for passing them to the shell
1602 # quote_command("command", "arg 1", "arg with ' and ! characters")
1603 # => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1604 # Try to avoid using this function wherever possible.
1605 sub quote_command {
1606 return join(' ',
1607 map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
1610 # get HEAD ref of given project as hash
1611 sub git_get_head_hash {
1612 my $project = shift;
1613 my $o_git_dir = $git_dir;
1614 my $retval = undef;
1615 $git_dir = "$projectroot/$project";
1616 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
1617 my $head = <$fd>;
1618 close $fd;
1619 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
1620 $retval = $1;
1623 if (defined $o_git_dir) {
1624 $git_dir = $o_git_dir;
1626 return $retval;
1629 # get type of given object
1630 sub git_get_type {
1631 my $hash = shift;
1633 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
1634 my $type = <$fd>;
1635 close $fd or return;
1636 chomp $type;
1637 return $type;
1640 # repository configuration
1641 our $config_file = '';
1642 our %config;
1644 # store multiple values for single key as anonymous array reference
1645 # single values stored directly in the hash, not as [ <value> ]
1646 sub hash_set_multi {
1647 my ($hash, $key, $value) = @_;
1649 if (!exists $hash->{$key}) {
1650 $hash->{$key} = $value;
1651 } elsif (!ref $hash->{$key}) {
1652 $hash->{$key} = [ $hash->{$key}, $value ];
1653 } else {
1654 push @{$hash->{$key}}, $value;
1658 # return hash of git project configuration
1659 # optionally limited to some section, e.g. 'gitweb'
1660 sub git_parse_project_config {
1661 my $section_regexp = shift;
1662 my %config;
1664 local $/ = "\0";
1666 open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1667 or return;
1669 while (my $keyval = <$fh>) {
1670 chomp $keyval;
1671 my ($key, $value) = split(/\n/, $keyval, 2);
1673 hash_set_multi(\%config, $key, $value)
1674 if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1676 close $fh;
1678 return %config;
1681 # convert config value to boolean, 'true' or 'false'
1682 # no value, number > 0, 'true' and 'yes' values are true
1683 # rest of values are treated as false (never as error)
1684 sub config_to_bool {
1685 my $val = shift;
1687 # strip leading and trailing whitespace
1688 $val =~ s/^\s+//;
1689 $val =~ s/\s+$//;
1691 return (!defined $val || # section.key
1692 ($val =~ /^\d+$/ && $val) || # section.key = 1
1693 ($val =~ /^(?:true|yes)$/i)); # section.key = true
1696 # convert config value to simple decimal number
1697 # an optional value suffix of 'k', 'm', or 'g' will cause the value
1698 # to be multiplied by 1024, 1048576, or 1073741824
1699 sub config_to_int {
1700 my $val = shift;
1702 # strip leading and trailing whitespace
1703 $val =~ s/^\s+//;
1704 $val =~ s/\s+$//;
1706 if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1707 $unit = lc($unit);
1708 # unknown unit is treated as 1
1709 return $num * ($unit eq 'g' ? 1073741824 :
1710 $unit eq 'm' ? 1048576 :
1711 $unit eq 'k' ? 1024 : 1);
1713 return $val;
1716 # convert config value to array reference, if needed
1717 sub config_to_multi {
1718 my $val = shift;
1720 return ref($val) ? $val : (defined($val) ? [ $val ] : []);
1723 sub git_get_project_config {
1724 my ($key, $type) = @_;
1726 # key sanity check
1727 return unless ($key);
1728 $key =~ s/^gitweb\.//;
1729 return if ($key =~ m/\W/);
1731 # type sanity check
1732 if (defined $type) {
1733 $type =~ s/^--//;
1734 $type = undef
1735 unless ($type eq 'bool' || $type eq 'int');
1738 # get config
1739 if (!defined $config_file ||
1740 $config_file ne "$git_dir/config") {
1741 %config = git_parse_project_config('gitweb');
1742 $config_file = "$git_dir/config";
1745 # ensure given type
1746 if (!defined $type) {
1747 return $config{"gitweb.$key"};
1748 } elsif ($type eq 'bool') {
1749 # backward compatibility: 'git config --bool' returns true/false
1750 return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1751 } elsif ($type eq 'int') {
1752 return config_to_int($config{"gitweb.$key"});
1754 return $config{"gitweb.$key"};
1757 # get hash of given path at given ref
1758 sub git_get_hash_by_path {
1759 my $base = shift;
1760 my $path = shift || return undef;
1761 my $type = shift;
1763 $path =~ s,/+$,,;
1765 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
1766 or die_error(500, "Open git-ls-tree failed");
1767 my $line = <$fd>;
1768 close $fd or return undef;
1770 if (!defined $line) {
1771 # there is no tree or hash given by $path at $base
1772 return undef;
1775 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1776 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1777 if (defined $type && $type ne $2) {
1778 # type doesn't match
1779 return undef;
1781 return $3;
1784 # get path of entry with given hash at given tree-ish (ref)
1785 # used to get 'from' filename for combined diff (merge commit) for renames
1786 sub git_get_path_by_hash {
1787 my $base = shift || return;
1788 my $hash = shift || return;
1790 local $/ = "\0";
1792 open my $fd, "-|", git_cmd(), "ls-tree", '-r', '-t', '-z', $base
1793 or return undef;
1794 while (my $line = <$fd>) {
1795 chomp $line;
1797 #'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'
1798 #'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'
1799 if ($line =~ m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {
1800 close $fd;
1801 return $1;
1804 close $fd;
1805 return undef;
1808 ## ......................................................................
1809 ## git utility functions, directly accessing git repository
1811 sub git_get_project_description {
1812 my $path = shift;
1814 $git_dir = "$projectroot/$path";
1815 open my $fd, "$git_dir/description"
1816 or return git_get_project_config('description');
1817 my $descr = <$fd>;
1818 close $fd;
1819 if (defined $descr) {
1820 chomp $descr;
1822 return $descr;
1825 sub git_get_project_ctags {
1826 my $path = shift;
1827 my $ctags = {};
1829 $git_dir = "$projectroot/$path";
1830 foreach (<$git_dir/ctags/*>) {
1831 open CT, $_ or next;
1832 my $val = <CT>;
1833 chomp $val;
1834 close CT;
1835 my $ctag = $_; $ctag =~ s#.*/##;
1836 $ctags->{$ctag} = $val;
1838 $ctags;
1841 sub git_populate_project_tagcloud {
1842 my $ctags = shift;
1844 # First, merge different-cased tags; tags vote on casing
1845 my %ctags_lc;
1846 foreach (keys %$ctags) {
1847 $ctags_lc{lc $_}->{count} += $ctags->{$_};
1848 if (not $ctags_lc{lc $_}->{topcount}
1849 or $ctags_lc{lc $_}->{topcount} < $ctags->{$_}) {
1850 $ctags_lc{lc $_}->{topcount} = $ctags->{$_};
1851 $ctags_lc{lc $_}->{topname} = $_;
1855 my $cloud;
1856 if (eval { require HTML::TagCloud; 1; }) {
1857 $cloud = HTML::TagCloud->new;
1858 foreach (sort keys %ctags_lc) {
1859 # Pad the title with spaces so that the cloud looks
1860 # less crammed.
1861 my $title = $ctags_lc{$_}->{topname};
1862 $title =~ s/ /&nbsp;/g;
1863 $title =~ s/^/&nbsp;/g;
1864 $title =~ s/$/&nbsp;/g;
1865 $cloud->add($title, $home_link."?by_tag=".$_, $ctags_lc{$_}->{count});
1867 } else {
1868 $cloud = \%ctags_lc;
1870 $cloud;
1873 sub git_show_project_tagcloud {
1874 my ($cloud, $count) = @_;
1875 print STDERR ref($cloud)."..\n";
1876 if (ref $cloud eq 'HTML::TagCloud') {
1877 return $cloud->html_and_css($count);
1878 } else {
1879 my @tags = sort { $cloud->{$a}->{count} <=> $cloud->{$b}->{count} } keys %$cloud;
1880 return '<p align="center">' . join (', ', map {
1881 "<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"
1882 } splice(@tags, 0, $count)) . '</p>';
1886 sub git_get_project_url_list {
1887 my $path = shift;
1889 $git_dir = "$projectroot/$path";
1890 open my $fd, "$git_dir/cloneurl"
1891 or return wantarray ?
1892 @{ config_to_multi(git_get_project_config('url')) } :
1893 config_to_multi(git_get_project_config('url'));
1894 my @git_project_url_list = map { chomp; $_ } <$fd>;
1895 close $fd;
1897 return wantarray ? @git_project_url_list : \@git_project_url_list;
1900 sub git_get_projects_list {
1901 my ($filter) = @_;
1902 my @list;
1904 $filter ||= '';
1905 $filter =~ s/\.git$//;
1907 my ($check_forks) = gitweb_check_feature('forks');
1909 if (-d $projects_list) {
1910 # search in directory
1911 my $dir = $projects_list . ($filter ? "/$filter" : '');
1912 # remove the trailing "/"
1913 $dir =~ s!/+$!!;
1914 my $pfxlen = length("$dir");
1915 my $pfxdepth = ($dir =~ tr!/!!);
1917 File::Find::find({
1918 follow_fast => 1, # follow symbolic links
1919 follow_skip => 2, # ignore duplicates
1920 dangling_symlinks => 0, # ignore dangling symlinks, silently
1921 wanted => sub {
1922 # skip project-list toplevel, if we get it.
1923 return if (m!^[/.]$!);
1924 # only directories can be git repositories
1925 return unless (-d $_);
1926 # don't traverse too deep (Find is super slow on os x)
1927 if (($File::Find::name =~ tr!/!!) - $pfxdepth > $project_maxdepth) {
1928 $File::Find::prune = 1;
1929 return;
1932 my $subdir = substr($File::Find::name, $pfxlen + 1);
1933 # we check related file in $projectroot
1934 if (check_export_ok("$projectroot/$filter/$subdir")) {
1935 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
1936 $File::Find::prune = 1;
1939 }, "$dir");
1941 } elsif (-f $projects_list) {
1942 # read from file(url-encoded):
1943 # 'git%2Fgit.git Linus+Torvalds'
1944 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1945 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1946 my %paths;
1947 open my ($fd), $projects_list or return;
1948 PROJECT:
1949 while (my $line = <$fd>) {
1950 chomp $line;
1951 my ($path, $owner) = split ' ', $line;
1952 $path = unescape($path);
1953 $owner = unescape($owner);
1954 if (!defined $path) {
1955 next;
1957 if ($filter ne '') {
1958 # looking for forks;
1959 my $pfx = substr($path, 0, length($filter));
1960 if ($pfx ne $filter) {
1961 next PROJECT;
1963 my $sfx = substr($path, length($filter));
1964 if ($sfx !~ /^\/.*\.git$/) {
1965 next PROJECT;
1967 } elsif ($check_forks) {
1968 PATH:
1969 foreach my $filter (keys %paths) {
1970 # looking for forks;
1971 my $pfx = substr($path, 0, length($filter));
1972 if ($pfx ne $filter) {
1973 next PATH;
1975 my $sfx = substr($path, length($filter));
1976 if ($sfx !~ /^\/.*\.git$/) {
1977 next PATH;
1979 # is a fork, don't include it in
1980 # the list
1981 next PROJECT;
1984 if (check_export_ok("$projectroot/$path")) {
1985 my $pr = {
1986 path => $path,
1987 owner => to_utf8($owner),
1989 push @list, $pr;
1990 (my $forks_path = $path) =~ s/\.git$//;
1991 $paths{$forks_path}++;
1994 close $fd;
1996 return @list;
1999 our $gitweb_project_owner = undef;
2000 sub git_get_project_list_from_file {
2002 return if (defined $gitweb_project_owner);
2004 $gitweb_project_owner = {};
2005 # read from file (url-encoded):
2006 # 'git%2Fgit.git Linus+Torvalds'
2007 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
2008 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
2009 if (-f $projects_list) {
2010 open (my $fd , $projects_list);
2011 while (my $line = <$fd>) {
2012 chomp $line;
2013 my ($pr, $ow) = split ' ', $line;
2014 $pr = unescape($pr);
2015 $ow = unescape($ow);
2016 $gitweb_project_owner->{$pr} = to_utf8($ow);
2018 close $fd;
2022 sub git_get_project_owner {
2023 my $project = shift;
2024 my $owner;
2026 return undef unless $project;
2027 $git_dir = "$projectroot/$project";
2029 if (!defined $gitweb_project_owner) {
2030 git_get_project_list_from_file();
2033 if (exists $gitweb_project_owner->{$project}) {
2034 $owner = $gitweb_project_owner->{$project};
2036 if (!defined $owner){
2037 $owner = git_get_project_config('owner');
2039 if (!defined $owner) {
2040 $owner = get_file_owner("$git_dir");
2043 return $owner;
2046 sub git_get_last_activity {
2047 my ($path) = @_;
2048 my $fd;
2050 $git_dir = "$projectroot/$path";
2051 open($fd, "-|", git_cmd(), 'for-each-ref',
2052 '--format=%(committer)',
2053 '--sort=-committerdate',
2054 '--count=1',
2055 'refs/heads') or return;
2056 my $most_recent = <$fd>;
2057 close $fd or return;
2058 if (defined $most_recent &&
2059 $most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
2060 my $timestamp = $1;
2061 my $age = time - $timestamp;
2062 return ($age, age_string($age));
2064 return (undef, undef);
2067 sub git_get_references {
2068 my $type = shift || "";
2069 my %refs;
2070 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
2071 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
2072 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
2073 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
2074 or return;
2076 while (my $line = <$fd>) {
2077 chomp $line;
2078 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {
2079 if (defined $refs{$1}) {
2080 push @{$refs{$1}}, $2;
2081 } else {
2082 $refs{$1} = [ $2 ];
2086 close $fd or return;
2087 return \%refs;
2090 sub git_get_rev_name_tags {
2091 my $hash = shift || return undef;
2093 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
2094 or return;
2095 my $name_rev = <$fd>;
2096 close $fd;
2098 if ($name_rev =~ m|^$hash tags/(.*)$|) {
2099 return $1;
2100 } else {
2101 # catches also '$hash undefined' output
2102 return undef;
2106 ## ----------------------------------------------------------------------
2107 ## parse to hash functions
2109 sub parse_date {
2110 my $epoch = shift;
2111 my $tz = shift || "-0000";
2113 my %date;
2114 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
2115 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
2116 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
2117 $date{'hour'} = $hour;
2118 $date{'minute'} = $min;
2119 $date{'mday'} = $mday;
2120 $date{'day'} = $days[$wday];
2121 $date{'month'} = $months[$mon];
2122 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
2123 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
2124 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
2125 $mday, $months[$mon], $hour ,$min;
2126 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
2127 1900+$year, 1+$mon, $mday, $hour ,$min, $sec;
2129 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
2130 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
2131 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
2132 $date{'hour_local'} = $hour;
2133 $date{'minute_local'} = $min;
2134 $date{'tz_local'} = $tz;
2135 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
2136 1900+$year, $mon+1, $mday,
2137 $hour, $min, $sec, $tz);
2138 return %date;
2141 sub parse_tag {
2142 my $tag_id = shift;
2143 my %tag;
2144 my @comment;
2146 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
2147 $tag{'id'} = $tag_id;
2148 while (my $line = <$fd>) {
2149 chomp $line;
2150 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
2151 $tag{'object'} = $1;
2152 } elsif ($line =~ m/^type (.+)$/) {
2153 $tag{'type'} = $1;
2154 } elsif ($line =~ m/^tag (.+)$/) {
2155 $tag{'name'} = $1;
2156 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
2157 $tag{'author'} = $1;
2158 $tag{'epoch'} = $2;
2159 $tag{'tz'} = $3;
2160 } elsif ($line =~ m/--BEGIN/) {
2161 push @comment, $line;
2162 last;
2163 } elsif ($line eq "") {
2164 last;
2167 push @comment, <$fd>;
2168 $tag{'comment'} = \@comment;
2169 close $fd or return;
2170 if (!defined $tag{'name'}) {
2171 return
2173 return %tag
2176 sub parse_commit_text {
2177 my ($commit_text, $withparents) = @_;
2178 my @commit_lines = split '\n', $commit_text;
2179 my %co;
2181 pop @commit_lines; # Remove '\0'
2183 if (! @commit_lines) {
2184 return;
2187 my $header = shift @commit_lines;
2188 if ($header !~ m/^[0-9a-fA-F]{40}/) {
2189 return;
2191 ($co{'id'}, my @parents) = split ' ', $header;
2192 while (my $line = shift @commit_lines) {
2193 last if $line eq "\n";
2194 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
2195 $co{'tree'} = $1;
2196 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
2197 push @parents, $1;
2198 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
2199 $co{'author'} = $1;
2200 $co{'author_epoch'} = $2;
2201 $co{'author_tz'} = $3;
2202 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
2203 $co{'author_name'} = $1;
2204 $co{'author_email'} = $2;
2205 } else {
2206 $co{'author_name'} = $co{'author'};
2208 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
2209 $co{'committer'} = $1;
2210 $co{'committer_epoch'} = $2;
2211 $co{'committer_tz'} = $3;
2212 $co{'committer_name'} = $co{'committer'};
2213 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
2214 $co{'committer_name'} = $1;
2215 $co{'committer_email'} = $2;
2216 } else {
2217 $co{'committer_name'} = $co{'committer'};
2221 if (!defined $co{'tree'}) {
2222 return;
2224 $co{'parents'} = \@parents;
2225 $co{'parent'} = $parents[0];
2227 foreach my $title (@commit_lines) {
2228 $title =~ s/^ //;
2229 if ($title ne "") {
2230 $co{'title'} = chop_str($title, 80, 5);
2231 # remove leading stuff of merges to make the interesting part visible
2232 if (length($title) > 50) {
2233 $title =~ s/^Automatic //;
2234 $title =~ s/^merge (of|with) /Merge ... /i;
2235 if (length($title) > 50) {
2236 $title =~ s/(http|rsync):\/\///;
2238 if (length($title) > 50) {
2239 $title =~ s/(master|www|rsync)\.//;
2241 if (length($title) > 50) {
2242 $title =~ s/kernel.org:?//;
2244 if (length($title) > 50) {
2245 $title =~ s/\/pub\/scm//;
2248 $co{'title_short'} = chop_str($title, 50, 5);
2249 last;
2252 if (! defined $co{'title'} || $co{'title'} eq "") {
2253 $co{'title'} = $co{'title_short'} = '(no commit message)';
2255 # remove added spaces
2256 foreach my $line (@commit_lines) {
2257 $line =~ s/^ //;
2259 $co{'comment'} = \@commit_lines;
2261 my $age = time - $co{'committer_epoch'};
2262 $co{'age'} = $age;
2263 $co{'age_string'} = age_string($age);
2264 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
2265 if ($age > 60*60*24*7*2) {
2266 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2267 $co{'age_string_age'} = $co{'age_string'};
2268 } else {
2269 $co{'age_string_date'} = $co{'age_string'};
2270 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
2272 return %co;
2275 sub parse_commit {
2276 my ($commit_id) = @_;
2277 my %co;
2279 local $/ = "\0";
2281 open my $fd, "-|", git_cmd(), "rev-list",
2282 "--parents",
2283 "--header",
2284 "--max-count=1",
2285 $commit_id,
2286 "--",
2287 or die_error(500, "Open git-rev-list failed");
2288 %co = parse_commit_text(<$fd>, 1);
2289 close $fd;
2291 return %co;
2294 sub parse_commits {
2295 my ($commit_id, $maxcount, $skip, $filename, @args) = @_;
2296 my @cos;
2298 $maxcount ||= 1;
2299 $skip ||= 0;
2301 local $/ = "\0";
2303 open my $fd, "-|", git_cmd(), "rev-list",
2304 "--header",
2305 @args,
2306 ("--max-count=" . $maxcount),
2307 ("--skip=" . $skip),
2308 @extra_options,
2309 $commit_id,
2310 "--",
2311 ($filename ? ($filename) : ())
2312 or die_error(500, "Open git-rev-list failed");
2313 while (my $line = <$fd>) {
2314 my %co = parse_commit_text($line);
2315 push @cos, \%co;
2317 close $fd;
2319 return wantarray ? @cos : \@cos;
2322 # parse line of git-diff-tree "raw" output
2323 sub parse_difftree_raw_line {
2324 my $line = shift;
2325 my %res;
2327 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2328 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2329 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
2330 $res{'from_mode'} = $1;
2331 $res{'to_mode'} = $2;
2332 $res{'from_id'} = $3;
2333 $res{'to_id'} = $4;
2334 $res{'status'} = $5;
2335 $res{'similarity'} = $6;
2336 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
2337 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
2338 } else {
2339 $res{'from_file'} = $res{'to_file'} = $res{'file'} = unquote($7);
2342 # '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'
2343 # combined diff (for merge commit)
2344 elsif ($line =~ s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {
2345 $res{'nparents'} = length($1);
2346 $res{'from_mode'} = [ split(' ', $2) ];
2347 $res{'to_mode'} = pop @{$res{'from_mode'}};
2348 $res{'from_id'} = [ split(' ', $3) ];
2349 $res{'to_id'} = pop @{$res{'from_id'}};
2350 $res{'status'} = [ split('', $4) ];
2351 $res{'to_file'} = unquote($5);
2353 # 'c512b523472485aef4fff9e57b229d9d243c967f'
2354 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
2355 $res{'commit'} = $1;
2358 return wantarray ? %res : \%res;
2361 # wrapper: return parsed line of git-diff-tree "raw" output
2362 # (the argument might be raw line, or parsed info)
2363 sub parsed_difftree_line {
2364 my $line_or_ref = shift;
2366 if (ref($line_or_ref) eq "HASH") {
2367 # pre-parsed (or generated by hand)
2368 return $line_or_ref;
2369 } else {
2370 return parse_difftree_raw_line($line_or_ref);
2374 # parse line of git-ls-tree output
2375 sub parse_ls_tree_line ($;%) {
2376 my $line = shift;
2377 my %opts = @_;
2378 my %res;
2380 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
2381 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
2383 $res{'mode'} = $1;
2384 $res{'type'} = $2;
2385 $res{'hash'} = $3;
2386 if ($opts{'-z'}) {
2387 $res{'name'} = $4;
2388 } else {
2389 $res{'name'} = unquote($4);
2392 return wantarray ? %res : \%res;
2395 # generates _two_ hashes, references to which are passed as 2 and 3 argument
2396 sub parse_from_to_diffinfo {
2397 my ($diffinfo, $from, $to, @parents) = @_;
2399 if ($diffinfo->{'nparents'}) {
2400 # combined diff
2401 $from->{'file'} = [];
2402 $from->{'href'} = [];
2403 fill_from_file_info($diffinfo, @parents)
2404 unless exists $diffinfo->{'from_file'};
2405 for (my $i = 0; $i < $diffinfo->{'nparents'}; $i++) {
2406 $from->{'file'}[$i] =
2407 defined $diffinfo->{'from_file'}[$i] ?
2408 $diffinfo->{'from_file'}[$i] :
2409 $diffinfo->{'to_file'};
2410 if ($diffinfo->{'status'}[$i] ne "A") { # not new (added) file
2411 $from->{'href'}[$i] = href(action=>"blob",
2412 hash_base=>$parents[$i],
2413 hash=>$diffinfo->{'from_id'}[$i],
2414 file_name=>$diffinfo->{'from_prefix'}.$from->{'file'}[$i]);
2415 } else {
2416 $from->{'href'}[$i] = undef;
2419 } else {
2420 # ordinary (not combined) diff
2421 $from->{'file'} = $diffinfo->{'from_file'};
2422 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2423 $from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2424 hash=>$diffinfo->{'from_id'},
2425 file_name=>$diffinfo->{'from_prefix'}.$from->{'file'});
2426 } else {
2427 delete $from->{'href'};
2431 $to->{'file'} = $diffinfo->{'to_file'};
2432 if (!is_deleted($diffinfo)) { # file exists in result
2433 $to->{'href'} = href(action=>"blob", hash_base=>$hash,
2434 hash=>$diffinfo->{'to_id'},
2435 file_name=>$diffinfo->{'to_prefix'}.$to->{'file'});
2436 } else {
2437 delete $to->{'href'};
2441 ## ......................................................................
2442 ## parse to array of hashes functions
2444 sub git_get_heads_list {
2445 my $limit = shift;
2446 my @headslist;
2448 open my $fd, '-|', git_cmd(), 'for-each-ref',
2449 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
2450 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
2451 'refs/heads'
2452 or return;
2453 while (my $line = <$fd>) {
2454 my %ref_item;
2456 chomp $line;
2457 my ($refinfo, $committerinfo) = split(/\0/, $line);
2458 my ($hash, $name, $title) = split(' ', $refinfo, 3);
2459 my ($committer, $epoch, $tz) =
2460 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
2461 $ref_item{'fullname'} = $name;
2462 $name =~ s!^refs/heads/!!;
2464 $ref_item{'name'} = $name;
2465 $ref_item{'id'} = $hash;
2466 $ref_item{'title'} = $title || '(no commit message)';
2467 $ref_item{'epoch'} = $epoch;
2468 if ($epoch) {
2469 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2470 } else {
2471 $ref_item{'age'} = "unknown";
2474 push @headslist, \%ref_item;
2476 close $fd;
2478 return wantarray ? @headslist : \@headslist;
2481 sub git_get_tags_list {
2482 my $limit = shift;
2483 my @tagslist;
2485 open my $fd, '-|', git_cmd(), 'for-each-ref',
2486 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
2487 '--format=%(objectname) %(objecttype) %(refname) '.
2488 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
2489 'refs/tags'
2490 or return;
2491 while (my $line = <$fd>) {
2492 my %ref_item;
2494 chomp $line;
2495 my ($refinfo, $creatorinfo) = split(/\0/, $line);
2496 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
2497 my ($creator, $epoch, $tz) =
2498 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
2499 $ref_item{'fullname'} = $name;
2500 $name =~ s!^refs/tags/!!;
2502 $ref_item{'type'} = $type;
2503 $ref_item{'id'} = $id;
2504 $ref_item{'name'} = $name;
2505 if ($type eq "tag") {
2506 $ref_item{'subject'} = $title;
2507 $ref_item{'reftype'} = $reftype;
2508 $ref_item{'refid'} = $refid;
2509 } else {
2510 $ref_item{'reftype'} = $type;
2511 $ref_item{'refid'} = $id;
2514 if ($type eq "tag" || $type eq "commit") {
2515 $ref_item{'epoch'} = $epoch;
2516 if ($epoch) {
2517 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
2518 } else {
2519 $ref_item{'age'} = "unknown";
2523 push @tagslist, \%ref_item;
2525 close $fd;
2527 return wantarray ? @tagslist : \@tagslist;
2530 ## ----------------------------------------------------------------------
2531 ## filesystem-related functions
2533 sub get_file_owner {
2534 my $path = shift;
2536 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
2537 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
2538 if (!defined $gcos) {
2539 return undef;
2541 my $owner = $gcos;
2542 $owner =~ s/[,;].*$//;
2543 return to_utf8($owner);
2546 ## ......................................................................
2547 ## mimetype related functions
2549 sub mimetype_guess_file {
2550 my $filename = shift;
2551 my $mimemap = shift;
2552 -r $mimemap or return undef;
2554 my %mimemap;
2555 open(MIME, $mimemap) or return undef;
2556 while (<MIME>) {
2557 next if m/^#/; # skip comments
2558 my ($mime, $exts) = split(/\t+/);
2559 if (defined $exts) {
2560 my @exts = split(/\s+/, $exts);
2561 foreach my $ext (@exts) {
2562 $mimemap{$ext} = $mime;
2566 close(MIME);
2568 $filename =~ /\.([^.]*)$/;
2569 return $mimemap{$1};
2572 sub mimetype_guess {
2573 my $filename = shift;
2574 my $mime;
2575 $filename =~ /\./ or return undef;
2577 if ($mimetypes_file) {
2578 my $file = $mimetypes_file;
2579 if ($file !~ m!^/!) { # if it is relative path
2580 # it is relative to project
2581 $file = "$projectroot/$project/$file";
2583 $mime = mimetype_guess_file($filename, $file);
2585 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
2586 return $mime;
2589 sub blob_mimetype {
2590 my $fd = shift;
2591 my $filename = shift;
2593 if ($filename) {
2594 my $mime = mimetype_guess($filename);
2595 $mime and return $mime;
2598 # just in case
2599 return $default_blob_plain_mimetype unless $fd;
2601 if (-T $fd) {
2602 return 'text/plain';
2603 } elsif (! $filename) {
2604 return 'application/octet-stream';
2605 } elsif ($filename =~ m/\.png$/i) {
2606 return 'image/png';
2607 } elsif ($filename =~ m/\.gif$/i) {
2608 return 'image/gif';
2609 } elsif ($filename =~ m/\.jpe?g$/i) {
2610 return 'image/jpeg';
2611 } else {
2612 return 'application/octet-stream';
2616 sub blob_contenttype {
2617 my ($fd, $file_name, $type) = @_;
2619 $type ||= blob_mimetype($fd, $file_name);
2620 if ($type eq 'text/plain' && defined $default_text_plain_charset) {
2621 $type .= "; charset=$default_text_plain_charset";
2624 return $type;
2627 ## ======================================================================
2628 ## functions printing HTML: header, footer, error page
2630 sub git_header_html {
2631 my $status = shift || "200 OK";
2632 my $expires = shift;
2634 my $title = "$site_name";
2635 if (defined $project) {
2636 $title .= " - " . to_utf8($project);
2637 if (defined $action) {
2638 $title .= "/$action";
2639 if (defined $file_name) {
2640 $title .= " - " . esc_path($file_name);
2641 if ($action eq "tree" && $file_name !~ m|/$|) {
2642 $title .= "/";
2647 # We do not ever emit application/xhtml+xml since that gives us
2648 # no benefits and it makes many browsers (e.g. Firefox) exceedingly
2649 # strict, which is troublesome for example when showing user-supplied
2650 # README.html files.
2651 my $content_type = 'text/html';
2652 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
2653 -status=> $status, -expires => $expires);
2654 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
2655 print <<EOF;
2656 <?xml version="1.0" encoding="utf-8"?>
2657 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2658 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
2659 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
2660 <!-- git core binaries version $git_version -->
2661 <head>
2662 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
2663 <meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
2664 <meta name="robots" content="index, nofollow"/>
2665 <title>$title</title>
2666 <script type="text/javascript">/* <![CDATA[ */
2667 function fixBlameLinks() {
2668 var allLinks = document.getElementsByTagName("a");
2669 for (var i = 0; i < allLinks.length; i++) {
2670 var link = allLinks.item(i);
2671 if (link.className == 'blamelink')
2672 link.href = link.href.replace("a=blame", "a=blame_incremental");
2675 /* ]]> */</script>
2677 # print out each stylesheet that exist
2678 if (defined $stylesheet) {
2679 #provides backwards capability for those people who define style sheet in a config file
2680 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2681 } else {
2682 foreach my $stylesheet (@stylesheets) {
2683 next unless $stylesheet;
2684 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
2687 if (defined $project) {
2688 my %href_params = get_feed_info();
2689 if (!exists $href_params{'-title'}) {
2690 $href_params{'-title'} = 'log';
2693 foreach my $format qw(RSS Atom) {
2694 my $type = lc($format);
2695 my %link_attr = (
2696 '-rel' => 'alternate',
2697 '-title' => "$project - $href_params{'-title'} - $format feed",
2698 '-type' => "application/$type+xml"
2701 $href_params{'action'} = $type;
2702 $link_attr{'-href'} = href(%href_params);
2703 print "<link ".
2704 "rel=\"$link_attr{'-rel'}\" ".
2705 "title=\"$link_attr{'-title'}\" ".
2706 "href=\"$link_attr{'-href'}\" ".
2707 "type=\"$link_attr{'-type'}\" ".
2708 "/>\n";
2710 $href_params{'extra_options'} = '--no-merges';
2711 $link_attr{'-href'} = href(%href_params);
2712 $link_attr{'-title'} .= ' (no merges)';
2713 print "<link ".
2714 "rel=\"$link_attr{'-rel'}\" ".
2715 "title=\"$link_attr{'-title'}\" ".
2716 "href=\"$link_attr{'-href'}\" ".
2717 "type=\"$link_attr{'-type'}\" ".
2718 "/>\n";
2721 } else {
2722 printf('<link rel="alternate" title="%s projects list" '.
2723 'href="%s" type="text/plain; charset=utf-8" />'."\n",
2724 $site_name, href(project=>undef, action=>"project_index"));
2725 printf('<link rel="alternate" title="%s projects feeds" '.
2726 'href="%s" type="text/x-opml" />'."\n",
2727 $site_name, href(project=>undef, action=>"opml"));
2729 if (defined $favicon) {
2730 print qq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);
2733 if (defined $gitwebjs) {
2734 print qq(<script src="$gitwebjs" type="text/javascript"></script>\n);
2737 print "</head>\n" .
2738 "<body onload=\"GitAddLinks(); fixBlameLinks();\">\n";
2740 if (-f $site_header) {
2741 open (my $fd, $site_header);
2742 print <$fd>;
2743 close $fd;
2746 print "<div class=\"page_header\">\n" .
2747 $cgi->a({-href => esc_url($logo_url),
2748 -title => $logo_label},
2749 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
2750 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
2751 if (defined $project) {
2752 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
2753 if (defined $action) {
2754 print " / $action";
2756 print "\n";
2758 print "</div>\n";
2760 my ($have_search) = gitweb_check_feature('search');
2761 if (defined $project && $have_search) {
2762 if (!defined $searchtext) {
2763 $searchtext = "";
2765 my $search_hash;
2766 if (defined $hash_base) {
2767 $search_hash = $hash_base;
2768 } elsif (defined $hash) {
2769 $search_hash = $hash;
2770 } else {
2771 $search_hash = "HEAD";
2773 my $action = $my_uri;
2774 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
2775 if ($use_pathinfo) {
2776 $action .= "/".esc_url($project);
2778 print $cgi->startform(-method => "get", -action => $action) .
2779 "<div class=\"search\">\n" .
2780 (!$use_pathinfo &&
2781 $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
2782 $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
2783 $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
2784 $cgi->popup_menu(-name => 'st', -default => 'commit',
2785 -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
2786 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
2787 " search:\n",
2788 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
2789 "<span title=\"Extended regular expression\">" .
2790 $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
2791 -checked => $search_use_regexp) .
2792 "</span>" .
2793 "</div>" .
2794 $cgi->end_form() . "\n";
2798 sub git_footer_html {
2799 my $feed_class = 'rss_logo';
2801 print "<div class=\"page_footer\">\n";
2802 if (defined $project) {
2803 my $descr = git_get_project_description($project);
2804 if (defined $descr) {
2805 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
2808 my %href_params = get_feed_info();
2809 if (!%href_params) {
2810 $feed_class .= ' generic';
2812 $href_params{'-title'} ||= 'log';
2814 foreach my $format qw(RSS Atom) {
2815 $href_params{'action'} = lc($format);
2816 print $cgi->a({-href => href(%href_params),
2817 -title => "$href_params{'-title'} $format feed",
2818 -class => $feed_class}, $format)."\n";
2821 } else {
2822 print $cgi->a({-href => href(project=>undef, action=>"opml"),
2823 -class => $feed_class}, "OPML") . " ";
2824 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
2825 -class => $feed_class}, "TXT") . "\n";
2827 print "</div>\n"; # class="page_footer"
2829 if (-f $site_footer) {
2830 open (my $fd, $site_footer);
2831 print <$fd>;
2832 close $fd;
2835 print "</body>\n" .
2836 "</html>";
2839 # die_error(<http_status_code>, <error_message>)
2840 # Example: die_error(404, 'Hash not found')
2841 # By convention, use the following status codes (as defined in RFC 2616):
2842 # 400: Invalid or missing CGI parameters, or
2843 # requested object exists but has wrong type.
2844 # 403: Requested feature (like "pickaxe" or "snapshot") not enabled on
2845 # this server or project.
2846 # 404: Requested object/revision/project doesn't exist.
2847 # 500: The server isn't configured properly, or
2848 # an internal error occurred (e.g. failed assertions caused by bugs), or
2849 # an unknown error occurred (e.g. the git binary died unexpectedly).
2850 sub die_error {
2851 my $status = shift || 500;
2852 my $error = shift || "Internal server error";
2854 my %http_responses = (400 => '400 Bad Request',
2855 403 => '403 Forbidden',
2856 404 => '404 Not Found',
2857 500 => '500 Internal Server Error');
2858 git_header_html($http_responses{$status});
2859 print <<EOF;
2860 <div class="page_body">
2861 <br /><br />
2862 $status - $error
2863 <br />
2864 </div>
2866 git_footer_html();
2867 exit;
2870 ## ----------------------------------------------------------------------
2871 ## functions printing or outputting HTML: navigation
2873 sub git_print_page_nav {
2874 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
2875 $extra = '' if !defined $extra; # pager or formats
2877 my @navs = qw(summary log commit commitdiff tree);
2878 if ($suppress) {
2879 @navs = grep { $_ ne $suppress } @navs;
2882 my %arg = map { $_ => {action=>$_} } @navs;
2883 if (defined $head) {
2884 for (qw(commit commitdiff)) {
2885 $arg{$_}{'hash'} = $head;
2887 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
2888 $arg{'log'}{'hash'} = $head;
2892 $arg{'log'}{'action'} = 'shortlog';
2893 $arg{'tree'}{'hash'} = $treehead if defined $treehead;
2894 $arg{'tree'}{'hash_base'} = $treebase if defined $treebase;
2896 my @actions = gitweb_check_feature('actions');
2897 while (@actions) {
2898 my ($label, $link, $pos) = (shift(@actions), shift(@actions), shift(@actions));
2899 @navs = map { $_ eq $pos ? ($_, $label) : $_ } @navs;
2900 # munch munch
2901 $link =~ s#%n#$project#g;
2902 $link =~ s#%f#$git_dir#g;
2903 $treehead ? $link =~ s#%h#$treehead#g : $link =~ s#%h##g;
2904 $treebase ? $link =~ s#%b#$treebase#g : $link =~ s#%b##g;
2905 $arg{$label}{'_href'} = $link;
2908 print "<div class=\"page_nav\">\n" .
2909 (join " | ",
2910 map { $_ eq $current ?
2911 $_ : $cgi->a({-href => ($arg{$_}{_href} ? $arg{$_}{_href} : href(%{$arg{$_}}))}, "$_")
2912 } @navs);
2913 print "<br/>\n$extra<br/>\n" .
2914 "</div>\n";
2917 sub format_paging_nav {
2918 my ($action, $hash, $head, $page, $has_next_link) = @_;
2919 my $paging_nav;
2922 if ($hash ne $head || $page) {
2923 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
2924 } else {
2925 $paging_nav .= "HEAD";
2928 if ($page > 0) {
2929 $paging_nav .= " &sdot; " .
2930 $cgi->a({-href => href(-replay=>1, page=>$page-1),
2931 -accesskey => "p", -title => "Alt-p"}, "prev");
2932 } else {
2933 $paging_nav .= " &sdot; prev";
2936 if ($has_next_link) {
2937 $paging_nav .= " &sdot; " .
2938 $cgi->a({-href => href(-replay=>1, page=>$page+1),
2939 -accesskey => "n", -title => "Alt-n"}, "next");
2940 } else {
2941 $paging_nav .= " &sdot; next";
2944 return $paging_nav;
2947 sub format_log_nav {
2948 my ($action, $hash, $head, $page, $has_next_link) = @_;
2949 my $paging_nav;
2951 if ($action eq 'shortlog') {
2952 $paging_nav .= 'shortlog';
2953 } else {
2954 $paging_nav .= $cgi->a({-href => href(action=>'shortlog', -replay=>1)}, 'shortlog');
2956 $paging_nav .= ' | ';
2957 if ($action eq 'log') {
2958 $paging_nav .= 'fulllog';
2959 } else {
2960 $paging_nav .= $cgi->a({-href => href(action=>'log', -replay=>1)}, 'fulllog');
2963 $paging_nav .= " | " . format_paging_nav($action, $hash, $head, $page, $has_next_link);
2964 return $paging_nav;
2967 ## ......................................................................
2968 ## functions printing or outputting HTML: div
2970 sub git_print_header_div {
2971 my ($action, $title, $hash, $hash_base) = @_;
2972 my %args = ();
2974 $args{'action'} = $action;
2975 $args{'hash'} = $hash if $hash;
2976 $args{'hash_base'} = $hash_base if $hash_base;
2978 print "<div class=\"header\">\n" .
2979 $cgi->a({-href => href(%args), -class => "title"},
2980 $title ? $title : $action) .
2981 "\n</div>\n";
2984 #sub git_print_authorship (\%) {
2985 sub git_print_authorship {
2986 my $co = shift;
2988 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
2989 print "<div class=\"author_date\">" .
2990 esc_html($co->{'author_name'}) .
2991 " [$ad{'rfc2822'}";
2992 if ($ad{'hour_local'} < 6) {
2993 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2994 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2995 } else {
2996 printf(" (%02d:%02d %s)",
2997 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2999 print "]</div>\n";
3002 sub git_print_page_path {
3003 my $name = shift;
3004 my $type = shift;
3005 my $hb = shift;
3008 print "<div class=\"page_path\">";
3009 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
3010 -title => 'tree root'}, to_utf8("[$project]"));
3011 print " / ";
3012 if (defined $name) {
3013 my @dirname = split '/', $name;
3014 my $basename = pop @dirname;
3015 my $fullname = '';
3017 foreach my $dir (@dirname) {
3018 $fullname .= ($fullname ? '/' : '') . $dir;
3019 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
3020 hash_base=>$hb),
3021 -title => $fullname}, esc_path($dir));
3022 print " / ";
3024 if (defined $type && $type eq 'blob') {
3025 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
3026 hash_base=>$hb),
3027 -title => $name}, esc_path($basename));
3028 } elsif (defined $type && $type eq 'tree') {
3029 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
3030 hash_base=>$hb),
3031 -title => $name}, esc_path($basename));
3032 print " / ";
3033 } else {
3034 print esc_path($basename);
3037 print "<br/></div>\n";
3040 # sub git_print_log (\@;%) {
3041 sub git_print_log ($;%) {
3042 my $log = shift;
3043 my %opts = @_;
3045 if ($opts{'-remove_title'}) {
3046 # remove title, i.e. first line of log
3047 shift @$log;
3049 # remove leading empty lines
3050 while (defined $log->[0] && $log->[0] eq "") {
3051 shift @$log;
3054 # print log
3055 my $signoff = 0;
3056 my $empty = 0;
3057 foreach my $line (@$log) {
3058 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
3059 $signoff = 1;
3060 $empty = 0;
3061 if (! $opts{'-remove_signoff'}) {
3062 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3063 next;
3064 } else {
3065 # remove signoff lines
3066 next;
3068 } else {
3069 $signoff = 0;
3072 # print only one empty line
3073 # do not print empty line after signoff
3074 if ($line eq "") {
3075 next if ($empty || $signoff);
3076 $empty = 1;
3077 } else {
3078 $empty = 0;
3081 print format_log_line_html($line) . "<br/>\n";
3084 if ($opts{'-final_empty_line'}) {
3085 # end with single empty line
3086 print "<br/>\n" unless $empty;
3090 # return link target (what link points to)
3091 sub git_get_link_target {
3092 my $hash = shift;
3093 my $link_target;
3095 # read link
3096 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
3097 or return;
3099 local $/;
3100 $link_target = <$fd>;
3102 close $fd
3103 or return;
3105 return $link_target;
3108 # given link target, and the directory (basedir) the link is in,
3109 # return target of link relative to top directory (top tree);
3110 # return undef if it is not possible (including absolute links).
3111 sub normalize_link_target {
3112 my ($link_target, $basedir, $hash_base) = @_;
3114 # we can normalize symlink target only if $hash_base is provided
3115 return unless $hash_base;
3117 # absolute symlinks (beginning with '/') cannot be normalized
3118 return if (substr($link_target, 0, 1) eq '/');
3120 # normalize link target to path from top (root) tree (dir)
3121 my $path;
3122 if ($basedir) {
3123 $path = $basedir . '/' . $link_target;
3124 } else {
3125 # we are in top (root) tree (dir)
3126 $path = $link_target;
3129 # remove //, /./, and /../
3130 my @path_parts;
3131 foreach my $part (split('/', $path)) {
3132 # discard '.' and ''
3133 next if (!$part || $part eq '.');
3134 # handle '..'
3135 if ($part eq '..') {
3136 if (@path_parts) {
3137 pop @path_parts;
3138 } else {
3139 # link leads outside repository (outside top dir)
3140 return;
3142 } else {
3143 push @path_parts, $part;
3146 $path = join('/', @path_parts);
3148 return $path;
3151 # print tree entry (row of git_tree), but without encompassing <tr> element
3152 sub git_print_tree_entry {
3153 my ($t, $basedir, $hash_base, $have_blame) = @_;
3155 my %base_key = ();
3156 $base_key{'hash_base'} = $hash_base if defined $hash_base;
3158 # The format of a table row is: mode list link. Where mode is
3159 # the mode of the entry, list is the name of the entry, an href,
3160 # and link is the action links of the entry.
3162 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
3163 if ($t->{'type'} eq "blob") {
3164 print "<td class=\"list\">" .
3165 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3166 file_name=>"$basedir$t->{'name'}", %base_key),
3167 -class => "list"}, esc_path($t->{'name'}));
3168 if (S_ISLNK(oct $t->{'mode'})) {
3169 my $link_target = git_get_link_target($t->{'hash'});
3170 if ($link_target) {
3171 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
3172 if (defined $norm_target) {
3173 print " -> " .
3174 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
3175 file_name=>$norm_target),
3176 -title => $norm_target}, esc_path($link_target));
3177 } else {
3178 print " -> " . esc_path($link_target);
3182 print "</td>\n";
3183 print "<td class=\"link\">";
3184 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
3185 file_name=>"$basedir$t->{'name'}", %base_key)},
3186 "blob");
3187 if ($have_blame) {
3188 print " | " .
3189 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
3190 file_name=>"$basedir$t->{'name'}", %base_key), -class => "blamelink"},
3191 "blame");
3193 if (defined $hash_base) {
3194 print " | " .
3195 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3196 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
3197 "history");
3199 print " | " .
3200 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
3201 file_name=>"$basedir$t->{'name'}")},
3202 "raw");
3203 print "</td>\n";
3205 } elsif ($t->{'type'} eq "tree") {
3206 print "<td class=\"list\">";
3207 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3208 file_name=>"$basedir$t->{'name'}", %base_key)},
3209 esc_path($t->{'name'}));
3210 print "</td>\n";
3211 print "<td class=\"link\">";
3212 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
3213 file_name=>"$basedir$t->{'name'}", %base_key)},
3214 "tree");
3215 if (defined $hash_base) {
3216 print " | " .
3217 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
3218 file_name=>"$basedir$t->{'name'}")},
3219 "history");
3221 print "</td>\n";
3222 } else {
3223 # unknown object: we can only present history for it
3224 # (this includes 'commit' object, i.e. submodule support)
3225 print "<td class=\"list\">" .
3226 esc_path($t->{'name'}) .
3227 "</td>\n";
3228 print "<td class=\"link\">";
3229 if (defined $hash_base) {
3230 print $cgi->a({-href => href(action=>"history",
3231 hash_base=>$hash_base,
3232 file_name=>"$basedir$t->{'name'}")},
3233 "history");
3235 print "</td>\n";
3239 ## ......................................................................
3240 ## functions printing large fragments of HTML
3242 # get pre-image filenames for merge (combined) diff
3243 sub fill_from_file_info {
3244 my ($diff, @parents) = @_;
3246 $diff->{'from_file'} = [ ];
3247 $diff->{'from_file'}[$diff->{'nparents'} - 1] = undef;
3248 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3249 if ($diff->{'status'}[$i] eq 'R' ||
3250 $diff->{'status'}[$i] eq 'C') {
3251 $diff->{'from_file'}[$i] =
3252 git_get_path_by_hash($parents[$i], $diff->{'from_id'}[$i]);
3256 return $diff;
3259 # is current raw difftree line of file deletion
3260 sub is_deleted {
3261 my $diffinfo = shift;
3263 return $diffinfo->{'to_id'} eq ('0' x 40);
3266 # does patch correspond to [previous] difftree raw line
3267 # $diffinfo - hashref of parsed raw diff format
3268 # $patchinfo - hashref of parsed patch diff format
3269 # (the same keys as in $diffinfo)
3270 sub is_patch_split {
3271 my ($diffinfo, $patchinfo) = @_;
3273 return defined $diffinfo && defined $patchinfo
3274 && $diffinfo->{'to_file'} eq $patchinfo->{'to_file'};
3278 sub git_difftree_body {
3279 my ($difftree, $from_prefix, $to_prefix, $hash, @parents) = @_;
3280 my ($parent) = $parents[0];
3281 my ($have_blame) = gitweb_check_feature('blame');
3283 $from_prefix = !defined $from_prefix ? '' : $from_prefix.'/';
3284 $to_prefix = !defined $to_prefix ? '' : $to_prefix . '/';
3286 print "<div class=\"list_head\">\n";
3287 if ($#{$difftree} > 10) {
3288 print(($#{$difftree} + 1) . " files changed:\n");
3290 print "</div>\n";
3292 print "<table class=\"" .
3293 (@parents > 1 ? "combined " : "") .
3294 "diff_tree\">\n";
3296 # header only for combined diff in 'commitdiff' view
3297 my $has_header = @$difftree && @parents > 1 && $action eq 'commitdiff';
3298 if ($has_header) {
3299 # table header
3300 print "<thead><tr>\n" .
3301 "<th></th><th></th>\n"; # filename, patchN link
3302 for (my $i = 0; $i < @parents; $i++) {
3303 my $par = $parents[$i];
3304 print "<th>" .
3305 $cgi->a({-href => href(action=>"commitdiff",
3306 hash=>$hash, hash_parent=>$par),
3307 -title => 'commitdiff to parent number ' .
3308 ($i+1) . ': ' . substr($par,0,7)},
3309 $i+1) .
3310 "&nbsp;</th>\n";
3312 print "</tr></thead>\n<tbody>\n";
3315 my $alternate = 1;
3316 my $patchno = 0;
3317 foreach my $line (@{$difftree}) {
3318 my $diff = parsed_difftree_line($line);
3320 if ($alternate) {
3321 print "<tr class=\"dark\">\n";
3322 } else {
3323 print "<tr class=\"light\">\n";
3325 $alternate ^= 1;
3327 if (exists $diff->{'nparents'}) { # combined diff
3329 fill_from_file_info($diff, @parents)
3330 unless exists $diff->{'from_file'};
3332 if (!is_deleted($diff)) {
3333 # file exists in the result (child) commit
3334 print "<td>" .
3335 $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3336 file_name=>$to_prefix.$diff->{'to_file'},
3337 hash_base=>$hash),
3338 -class => "list"}, esc_path($diff->{'to_file'})) .
3339 "</td>\n";
3340 } else {
3341 print "<td>" .
3342 esc_path($diff->{'to_file'}) .
3343 "</td>\n";
3346 if ($action eq 'commitdiff') {
3347 # link to patch
3348 $patchno++;
3349 print "<td class=\"link\">" .
3350 $cgi->a({-href => "#patch$patchno"}, "patch") .
3351 " | " .
3352 "</td>\n";
3355 my $has_history = 0;
3356 my $not_deleted = 0;
3357 for (my $i = 0; $i < $diff->{'nparents'}; $i++) {
3358 my $hash_parent = $parents[$i];
3359 my $from_hash = $diff->{'from_id'}[$i];
3360 my $from_path = $diff->{'from_file'}[$i];
3361 my $status = $diff->{'status'}[$i];
3363 $has_history ||= ($status ne 'A');
3364 $not_deleted ||= ($status ne 'D');
3366 if ($status eq 'A') {
3367 print "<td class=\"link\" align=\"right\"> | </td>\n";
3368 } elsif ($status eq 'D') {
3369 print "<td class=\"link\">" .
3370 $cgi->a({-href => href(action=>"blob",
3371 hash_base=>$hash,
3372 hash=>$from_hash,
3373 file_name=>$from_prefix.$from_path)},
3374 "blob" . ($i+1)) .
3375 " | </td>\n";
3376 } else {
3377 if ($diff->{'to_id'} eq $from_hash) {
3378 print "<td class=\"link nochange\">";
3379 } else {
3380 print "<td class=\"link\">";
3382 print $cgi->a({-href => href(action=>"blobdiff",
3383 hash=>$diff->{'to_id'},
3384 hash_parent=>$from_hash,
3385 hash_base=>$hash,
3386 hash_parent_base=>$hash_parent,
3387 file_name=>$to_prefix.$diff->{'to_file'},
3388 file_parent=>$from_prefix.$from_path)},
3389 "diff" . ($i+1)) .
3390 " | </td>\n";
3394 print "<td class=\"link\">";
3395 if ($not_deleted) {
3396 print $cgi->a({-href => href(action=>"blob",
3397 hash=>$diff->{'to_id'},
3398 file_name=>$to_prefix.$diff->{'to_file'},
3399 hash_base=>$hash)},
3400 "blob");
3401 print " | " if ($has_history);
3403 if ($has_history) {
3404 print $cgi->a({-href => href(action=>"history",
3405 file_name=>$to_prefix.$diff->{'to_file'},
3406 hash_base=>$hash)},
3407 "history");
3409 print "</td>\n";
3411 print "</tr>\n";
3412 next; # instead of 'else' clause, to avoid extra indent
3414 # else ordinary diff
3416 my ($to_mode_oct, $to_mode_str, $to_file_type);
3417 my ($from_mode_oct, $from_mode_str, $from_file_type);
3418 if ($diff->{'to_mode'} ne ('0' x 6)) {
3419 $to_mode_oct = oct $diff->{'to_mode'};
3420 if (S_ISREG($to_mode_oct)) { # only for regular file
3421 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
3423 $to_file_type = file_type($diff->{'to_mode'});
3425 if ($diff->{'from_mode'} ne ('0' x 6)) {
3426 $from_mode_oct = oct $diff->{'from_mode'};
3427 if (S_ISREG($to_mode_oct)) { # only for regular file
3428 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
3430 $from_file_type = file_type($diff->{'from_mode'});
3433 if ($diff->{'status'} eq "A") { # created
3434 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
3435 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
3436 $mode_chng .= "]</span>";
3437 print "<td>";
3438 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3439 hash_base=>$hash, file_name=>$to_prefix.$diff->{'file'}),
3440 -class => "list"}, esc_path($diff->{'file'}));
3441 print "</td>\n";
3442 print "<td>$mode_chng</td>\n";
3443 print "<td class=\"link\">";
3444 if ($action eq 'commitdiff') {
3445 # link to patch
3446 $patchno++;
3447 print $cgi->a({-href => "#patch$patchno"}, "patch");
3448 print " | ";
3450 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3451 hash_base=>$hash, file_name=>$to_prefix.$diff->{'file'})},
3452 "blob");
3453 print "</td>\n";
3455 } elsif ($diff->{'status'} eq "D") { # deleted
3456 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
3457 print "<td>";
3458 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3459 hash_base=>$parent, file_name=>$from_prefix.$diff->{'file'}),
3460 -class => "list"}, esc_path($diff->{'file'}));
3461 print "</td>\n";
3462 print "<td>$mode_chng</td>\n";
3463 print "<td class=\"link\">";
3464 if ($action eq 'commitdiff') {
3465 # link to patch
3466 $patchno++;
3467 print $cgi->a({-href => "#patch$patchno"}, "patch");
3468 print " | ";
3470 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},
3471 hash_base=>$parent, file_name=>$from_prefix.$diff->{'file'})},
3472 "blob") . " | ";
3473 if ($have_blame) {
3474 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
3475 file_name=>$from_prefix.$diff->{'file'}), -class => "blamelink"},
3476 "blame") . " | ";
3478 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
3479 file_name=>$from_prefix.$diff->{'file'})},
3480 "history");
3481 print "</td>\n";
3483 } elsif ($diff->{'status'} eq "M" || $diff->{'status'} eq "T") { # modified, or type changed
3484 my $mode_chnge = "";
3485 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3486 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
3487 if ($from_file_type ne $to_file_type) {
3488 $mode_chnge .= " from $from_file_type to $to_file_type";
3490 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
3491 if ($from_mode_str && $to_mode_str) {
3492 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
3493 } elsif ($to_mode_str) {
3494 $mode_chnge .= " mode: $to_mode_str";
3497 $mode_chnge .= "]</span>\n";
3499 print "<td>";
3500 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3501 hash_base=>$hash, file_name=>$to_prefix.$diff->{'file'}),
3502 -class => "list"}, esc_path($diff->{'file'}));
3503 print "</td>\n";
3504 print "<td>$mode_chnge</td>\n";
3505 print "<td class=\"link\">";
3506 if ($action eq 'commitdiff') {
3507 # link to patch
3508 $patchno++;
3509 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3510 " | ";
3511 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3512 # "commit" view and modified file (not onlu mode changed)
3513 print $cgi->a({-href => href(action=>"blobdiff",
3514 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3515 hash_base=>$hash, hash_parent_base=>$parent,
3516 file_name=>$to_prefix.$diff->{'file'},
3517 file_parent=>$from_prefix.$diff->{'file'})},
3518 "diff") .
3519 " | ";
3521 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3522 hash_base=>$hash, file_name=>$to_prefix.$diff->{'file'})},
3523 "blob") . " | ";
3524 if ($have_blame) {
3525 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3526 file_name=>$diff->{'file'}), -class => "blamelink"},
3527 "blame") . " | ";
3529 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3530 file_name=>$to_prefix.$diff->{'file'})},
3531 "history");
3532 print "</td>\n";
3534 } elsif ($diff->{'status'} eq "R" || $diff->{'status'} eq "C") { # renamed or copied
3535 my %status_name = ('R' => 'moved', 'C' => 'copied');
3536 my $nstatus = $status_name{$diff->{'status'}};
3537 my $mode_chng = "";
3538 if ($diff->{'from_mode'} != $diff->{'to_mode'}) {
3539 # mode also for directories, so we cannot use $to_mode_str
3540 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
3542 print "<td>" .
3543 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
3544 hash=>$diff->{'to_id'}, file_name=>$to_prefix.$diff->{'to_file'}),
3545 -class => "list"}, esc_path($diff->{'to_file'})) . "</td>\n" .
3546 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
3547 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
3548 hash=>$diff->{'from_id'}, file_name=>$from_prefix.$diff->{'from_file'}),
3549 -class => "list"}, esc_path($diff->{'from_file'})) .
3550 " with " . (int $diff->{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
3551 "<td class=\"link\">";
3552 if ($action eq 'commitdiff') {
3553 # link to patch
3554 $patchno++;
3555 print $cgi->a({-href => "#patch$patchno"}, "patch") .
3556 " | ";
3557 } elsif ($diff->{'to_id'} ne $diff->{'from_id'}) {
3558 # "commit" view and modified file (not only pure rename or copy)
3559 print $cgi->a({-href => href(action=>"blobdiff",
3560 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},
3561 hash_base=>$hash, hash_parent_base=>$parent,
3562 file_name=>$to_prefix.$diff->{'to_file'}, file_parent=>$from_prefix.$diff->{'from_file'})},
3563 "diff") .
3564 " | ";
3566 print $cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},
3567 hash_base=>$parent, file_name=>$to_prefix.$diff->{'to_file'})},
3568 "blob") . " | ";
3569 if ($have_blame) {
3570 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
3571 file_name=>$diff->{'to_file'}), -class => "blamelink"},
3572 "blame") . " | ";
3574 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
3575 file_name=>$to_prefix.$diff->{'to_file'})},
3576 "history");
3577 print "</td>\n";
3579 } # we should not encounter Unmerged (U) or Unknown (X) status
3580 print "</tr>\n";
3582 print "</tbody>" if $has_header;
3583 print "</table>\n";
3586 sub git_patchset_body {
3587 my ($fd, $difftree, $from_prefix, $to_prefix, $hash, @hash_parents) = @_;
3588 my ($hash_parent) = $hash_parents[0];
3590 my $is_combined = (@hash_parents > 1);
3591 my $patch_idx = 0;
3592 my $patch_number = 0;
3593 my $patch_line;
3594 my $diffinfo;
3595 my $to_name;
3596 my (%from, %to);
3598 $from_prefix = !defined $from_prefix ? '' : $from_prefix.'/';
3599 $to_prefix = !defined $to_prefix ? '' : $to_prefix . '/';
3601 print "<div class=\"patchset\">\n";
3603 # skip to first patch
3604 while ($patch_line = <$fd>) {
3605 chomp $patch_line;
3607 last if ($patch_line =~ m/^diff /);
3610 PATCH:
3611 while ($patch_line) {
3613 # parse "git diff" header line
3614 if ($patch_line =~ m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {
3615 # $1 is from_name, which we do not use
3616 $to_name = unquote($2);
3617 $to_name =~ s!^b/!!;
3618 } elsif ($patch_line =~ m/^diff --(cc|combined) ("?.*"?)$/) {
3619 # $1 is 'cc' or 'combined', which we do not use
3620 $to_name = unquote($2);
3621 } else {
3622 $to_name = undef;
3625 # check if current patch belong to current raw line
3626 # and parse raw git-diff line if needed
3627 if (is_patch_split($diffinfo, { 'to_file' => $to_name })) {
3628 # this is continuation of a split patch
3629 print "<div class=\"patch cont\">\n";
3630 $diffinfo->{'from_prefix'} = $from_prefix;
3631 $diffinfo->{'to_prefix'} = $to_prefix;
3632 } else {
3633 # advance raw git-diff output if needed
3634 $patch_idx++ if defined $diffinfo;
3636 # read and prepare patch information
3637 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3639 # compact combined diff output can have some patches skipped
3640 # find which patch (using pathname of result) we are at now;
3641 if ($is_combined) {
3642 while ($to_name ne $diffinfo->{'to_file'}) {
3643 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3644 format_diff_cc_simplified($diffinfo, @hash_parents) .
3645 "</div>\n"; # class="patch"
3647 $patch_idx++;
3648 $patch_number++;
3650 last if $patch_idx > $#$difftree;
3651 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3655 $diffinfo->{'from_prefix'} = $from_prefix;
3656 $diffinfo->{'to_prefix'} = $to_prefix;
3658 # modifies %from, %to hashes
3659 parse_from_to_diffinfo($diffinfo, \%from, \%to, @hash_parents);
3660 # this is first patch for raw difftree line with $patch_idx index
3661 # we index @$difftree array from 0, but number patches from 1
3662 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
3665 # git diff header
3666 #assert($patch_line =~ m/^diff /) if DEBUG;
3667 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
3668 $patch_number++;
3669 # print "git diff" header
3670 print format_git_diff_header_line($patch_line, $diffinfo,
3671 \%from, \%to);
3673 # print extended diff header
3674 print "<div class=\"diff extended_header\">\n";
3675 EXTENDED_HEADER:
3676 while ($patch_line = <$fd>) {
3677 chomp $patch_line;
3679 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
3681 print format_extended_diff_header_line($patch_line, $diffinfo,
3682 \%from, \%to);
3684 print "</div>\n"; # class="diff extended_header"
3686 # from-file/to-file diff header
3687 if (! $patch_line) {
3688 print "</div>\n"; # class="patch"
3689 last PATCH;
3691 next PATCH if ($patch_line =~ m/^diff /);
3692 #assert($patch_line =~ m/^---/) if DEBUG;
3694 my $last_patch_line = $patch_line;
3695 $patch_line = <$fd>;
3696 chomp $patch_line;
3697 #assert($patch_line =~ m/^\+\+\+/) if DEBUG;
3699 print format_diff_from_to_header($last_patch_line, $patch_line,
3700 $diffinfo, \%from, \%to,
3701 @hash_parents);
3703 # the patch itself
3704 LINE:
3705 while ($patch_line = <$fd>) {
3706 chomp $patch_line;
3708 next PATCH if ($patch_line =~ m/^diff /);
3710 print format_diff_line($patch_line, \%from, \%to);
3713 } continue {
3714 print "</div>\n"; # class="patch"
3717 # for compact combined (--cc) format, with chunk and patch simpliciaction
3718 # patchset might be empty, but there might be unprocessed raw lines
3719 for (++$patch_idx if $patch_number > 0;
3720 $patch_idx < @$difftree;
3721 ++$patch_idx) {
3722 # read and prepare patch information
3723 $diffinfo = parsed_difftree_line($difftree->[$patch_idx]);
3725 # generate anchor for "patch" links in difftree / whatchanged part
3726 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n" .
3727 format_diff_cc_simplified($diffinfo, @hash_parents) .
3728 "</div>\n"; # class="patch"
3730 $patch_number++;
3733 if ($patch_number == 0) {
3734 if (@hash_parents > 1) {
3735 print "<div class=\"diff nodifferences\">Trivial merge</div>\n";
3736 } else {
3737 print "<div class=\"diff nodifferences\">No differences found</div>\n";
3741 print "</div>\n"; # class="patchset"
3744 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3746 # fills project list info (age, description, owner, forks) for each
3747 # project in the list, removing invalid projects from returned list
3748 # NOTE: modifies $projlist, but does not remove entries from it
3749 sub fill_project_list_info {
3750 my ($projlist, $check_forks) = @_;
3751 my @projects;
3753 my $show_ctags = gitweb_check_feature('ctags');
3754 PROJECT:
3755 foreach my $pr (@$projlist) {
3756 my (@activity) = git_get_last_activity($pr->{'path'});
3757 unless (@activity) {
3758 next PROJECT;
3760 ($pr->{'age'}, $pr->{'age_string'}) = @activity;
3761 if (!defined $pr->{'descr'}) {
3762 my $descr = git_get_project_description($pr->{'path'}) || "";
3763 $descr = to_utf8($descr);
3764 $pr->{'descr_long'} = $descr;
3765 $pr->{'descr'} = chop_str($descr, $projects_list_description_width, 5);
3767 if (!defined $pr->{'owner'}) {
3768 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}") || "";
3770 if ($check_forks) {
3771 my $pname = $pr->{'path'};
3772 if (($pname =~ s/\.git$//) &&
3773 ($pname !~ /\/$/) &&
3774 (-d "$projectroot/$pname")) {
3775 $pr->{'forks'} = "-d $projectroot/$pname";
3776 } else {
3777 $pr->{'forks'} = 0;
3780 $show_ctags and $pr->{'ctags'} = git_get_project_ctags($pr->{'path'});
3781 push @projects, $pr;
3784 return @projects;
3787 # print 'sort by' <th> element, generating 'sort by $name' replay link
3788 # if that order is not selected
3789 sub print_sort_th {
3790 my ($name, $order, $header) = @_;
3791 $header ||= ucfirst($name);
3793 if ($order eq $name) {
3794 print "<th>$header</th>\n";
3795 } else {
3796 print "<th>" .
3797 $cgi->a({-href => href(-replay=>1, order=>$name),
3798 -class => "header"}, $header) .
3799 "</th>\n";
3803 sub git_project_list_body {
3804 # actually uses global variable $project
3805 my ($projlist, $order, $from, $to, $extra, $no_header, $cache_lifetime) = @_;
3807 my ($check_forks) = gitweb_check_feature('forks');
3809 use File::stat;
3810 use POSIX qw(:fcntl_h);
3811 use Storable qw(store_fd retrieve);
3813 my $cache_file = "$cache_dir/$projlist_cache_name";
3815 my @projects;
3816 my $stale = 0;
3817 my $now = time();
3818 my $cache_mtime;
3819 if ($cache_lifetime && -f $cache_file) {
3820 $cache_mtime = stat($cache_file)->mtime;
3822 if (defined $cache_mtime && # caching is on and $cache_file exists
3823 $cache_mtime + $cache_lifetime*60 > $now &&
3824 (my $dump = retrieve($cache_file))) {
3825 $stale = $now - $cache_mtime;
3826 @projects = @$dump;
3827 } else {
3828 if (defined $cache_mtime) {
3829 # Postpone timeout by two minutes so that we get
3830 # enough time to do our job, or to be more exact
3831 # make cache expire after two minutes from now.
3832 my $time = $now - $cache_lifetime*60 + 120;
3833 utime $time, $time, $cache_file;
3835 @projects = fill_project_list_info($projlist, $check_forks);
3836 if ($cache_lifetime &&
3837 (-d $cache_dir || mkdir($cache_dir, 0700)) &&
3838 sysopen(my $fd, "$cache_file.lock", O_WRONLY|O_CREAT|O_EXCL, 0600)) {
3839 store_fd(\@projects, $fd);
3840 close $fd;
3841 rename "$cache_file.lock", $cache_file;
3845 $order ||= $default_projects_order;
3846 $from = 0 unless defined $from;
3847 $to = $#projects if (!defined $to || $#projects < $to);
3849 my %order_info = (
3850 project => { key => 'path', type => 'str' },
3851 descr => { key => 'descr_long', type => 'str' },
3852 owner => { key => 'owner', type => 'str' },
3853 age => { key => 'age', type => 'num' }
3855 my $oi = $order_info{$order};
3856 if ($oi->{'type'} eq 'str') {
3857 @projects = sort {$a->{$oi->{'key'}} cmp $b->{$oi->{'key'}}} @projects;
3858 } else {
3859 @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
3862 if ($cache_lifetime && $stale > 0) {
3863 print "<div class=\"stale_info\">Cached version (${stale}s old)</div>\n";
3866 my $show_ctags = gitweb_check_feature('ctags');
3867 if ($show_ctags) {
3868 my %ctags;
3869 foreach my $p (@projects) {
3870 foreach my $ct (keys %{$p->{'ctags'}}) {
3871 $ctags{$ct} += $p->{'ctags'}->{$ct};
3874 my $cloud = git_populate_project_tagcloud(\%ctags);
3875 print git_show_project_tagcloud($cloud, 64);
3878 print "<table class=\"project_list\">\n";
3879 unless ($no_header) {
3880 print "<tr>\n";
3881 if ($check_forks) {
3882 print "<th></th>\n";
3884 print_sort_th('project', $order, 'Project');
3885 print_sort_th('descr', $order, 'Description');
3886 print_sort_th('owner', $order, 'Owner');
3887 print_sort_th('age', $order, 'Last Change');
3888 print "<th></th>\n" . # for links
3889 "</tr>\n";
3891 my $alternate = 1;
3892 my $tagfilter = $cgi->param('by_tag');
3893 for (my $i = $from; $i <= $to; $i++) {
3894 my $pr = $projects[$i];
3896 next if $tagfilter and $show_ctags and not grep { lc $_ eq lc $tagfilter } keys %{$pr->{'ctags'}};
3897 next if $searchtext and not $pr->{'path'} =~ /$searchtext/
3898 and not $pr->{'descr_long'} =~ /$searchtext/;
3899 # Weed out forks or non-matching entries of search
3900 if ($check_forks) {
3901 my $forkbase = $project; $forkbase ||= ''; $forkbase =~ s#\.git$#/#;
3902 $forkbase="^$forkbase" if $forkbase;
3903 next if not $searchtext and not $tagfilter and $show_ctags
3904 and $pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe
3907 if ($alternate) {
3908 print "<tr class=\"dark\">\n";
3909 } else {
3910 print "<tr class=\"light\">\n";
3912 $alternate ^= 1;
3913 if ($check_forks) {
3914 print "<td>";
3915 if ($pr->{'forks'}) {
3916 print "<!-- $pr->{'forks'} -->\n";
3917 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
3919 print "</td>\n";
3921 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3922 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
3923 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
3924 -class => "list", -title => $pr->{'descr_long'}},
3925 esc_html($pr->{'descr'})) . "</td>\n" .
3926 "<td><i>" . chop_and_escape_str($pr->{'owner'}, 15) . "</i></td>\n";
3927 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
3928 (defined $pr->{'age_string'} ? $pr->{'age_string'} : "No commits") . "</td>\n" .
3929 "<td class=\"link\">" .
3930 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
3931 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "log") . " | " .
3932 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
3933 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
3934 "</td>\n" .
3935 "</tr>\n";
3937 if (defined $extra) {
3938 print "<tr>\n";
3939 if ($check_forks) {
3940 print "<td></td>\n";
3942 print "<td colspan=\"5\">$extra</td>\n" .
3943 "</tr>\n";
3945 print "</table>\n";
3948 sub git_shortlog_body {
3949 # uses global variable $project
3950 my ($commitlist, $from, $to, $refs, $extra) = @_;
3952 $from = 0 unless defined $from;
3953 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3955 print "<table class=\"shortlog\">\n";
3956 my $alternate = 1;
3957 for (my $i = $from; $i <= $to; $i++) {
3958 my %co = %{$commitlist->[$i]};
3959 my $commit = $co{'id'};
3960 my $ref = format_ref_marker($refs, $commit);
3961 if ($alternate) {
3962 print "<tr class=\"dark\">\n";
3963 } else {
3964 print "<tr class=\"light\">\n";
3966 $alternate ^= 1;
3967 my $author = chop_and_escape_str($co{'author_name'}, 10);
3968 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
3969 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3970 "<td><i>" . $author . "</i></td>\n" .
3971 "<td>";
3972 print format_subject_html($co{'title'}, $co{'title_short'},
3973 href(action=>"commit", hash=>$commit), $ref);
3974 print "</td>\n" .
3975 "<td class=\"link\">" .
3976 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3977 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3978 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3979 my $snapshot_links = format_snapshot_links($commit);
3980 if (defined $snapshot_links) {
3981 print " | " . $snapshot_links;
3983 print "</td>\n" .
3984 "</tr>\n";
3986 if (defined $extra) {
3987 print "<tr>\n" .
3988 "<td colspan=\"4\">$extra</td>\n" .
3989 "</tr>\n";
3991 print "</table>\n";
3994 sub git_history_body {
3995 # Warning: assumes constant type (blob or tree) during history
3996 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
3998 $from = 0 unless defined $from;
3999 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
4001 print "<table class=\"history\">\n";
4002 my $alternate = 1;
4003 for (my $i = $from; $i <= $to; $i++) {
4004 my %co = %{$commitlist->[$i]};
4005 if (!%co) {
4006 next;
4008 my $commit = $co{'id'};
4010 my $ref = format_ref_marker($refs, $commit);
4012 if ($alternate) {
4013 print "<tr class=\"dark\">\n";
4014 } else {
4015 print "<tr class=\"light\">\n";
4017 $alternate ^= 1;
4018 # shortlog uses chop_str($co{'author_name'}, 10)
4019 my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
4020 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4021 "<td><i>" . $author . "</i></td>\n" .
4022 "<td>";
4023 # originally git_history used chop_str($co{'title'}, 50)
4024 print format_subject_html($co{'title'}, $co{'title_short'},
4025 href(action=>"commit", hash=>$commit), $ref);
4026 print "</td>\n" .
4027 "<td class=\"link\">" .
4028 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
4029 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
4031 if ($ftype eq 'blob') {
4032 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
4033 my $blob_parent = git_get_hash_by_path($commit, $file_name);
4034 if (defined $blob_current && defined $blob_parent &&
4035 $blob_current ne $blob_parent) {
4036 print " | " .
4037 $cgi->a({-href => href(action=>"blobdiff",
4038 hash=>$blob_current, hash_parent=>$blob_parent,
4039 hash_base=>$hash_base, hash_parent_base=>$commit,
4040 file_name=>$file_name)},
4041 "diff to current");
4044 print "</td>\n" .
4045 "</tr>\n";
4047 if (defined $extra) {
4048 print "<tr>\n" .
4049 "<td colspan=\"4\">$extra</td>\n" .
4050 "</tr>\n";
4052 print "</table>\n";
4055 sub git_tags_body {
4056 # uses global variable $project
4057 my ($taglist, $from, $to, $extra) = @_;
4058 $from = 0 unless defined $from;
4059 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
4061 print "<table class=\"tags\">\n";
4062 my $alternate = 1;
4063 for (my $i = $from; $i <= $to; $i++) {
4064 my $entry = $taglist->[$i];
4065 my %tag = %$entry;
4066 my $comment = $tag{'subject'};
4067 my $comment_short;
4068 if (defined $comment) {
4069 $comment_short = chop_str($comment, 30, 5);
4071 if ($alternate) {
4072 print "<tr class=\"dark\">\n";
4073 } else {
4074 print "<tr class=\"light\">\n";
4076 $alternate ^= 1;
4077 if (defined $tag{'age'}) {
4078 print "<td><i>$tag{'age'}</i></td>\n";
4079 } else {
4080 print "<td></td>\n";
4082 print "<td>" .
4083 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
4084 -class => "list name"}, esc_html($tag{'name'})) .
4085 "</td>\n" .
4086 "<td>";
4087 if (defined $comment) {
4088 print format_subject_html($comment, $comment_short,
4089 href(action=>"tag", hash=>$tag{'id'}));
4091 print "</td>\n" .
4092 "<td class=\"selflink\">";
4093 if ($tag{'type'} eq "tag") {
4094 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
4095 } else {
4096 print "&nbsp;";
4098 print "</td>\n" .
4099 "<td class=\"link\">" . " | " .
4100 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
4101 if ($tag{'reftype'} eq "commit") {
4102 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})}, "log");
4103 } elsif ($tag{'reftype'} eq "blob") {
4104 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
4106 print "</td>\n" .
4107 "</tr>";
4109 if (defined $extra) {
4110 print "<tr>\n" .
4111 "<td colspan=\"5\">$extra</td>\n" .
4112 "</tr>\n";
4114 print "</table>\n";
4117 sub git_heads_body {
4118 # uses global variable $project
4119 my ($headlist, $head, $from, $to, $extra) = @_;
4120 $from = 0 unless defined $from;
4121 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
4123 print "<table class=\"heads\">\n";
4124 my $alternate = 1;
4125 for (my $i = $from; $i <= $to; $i++) {
4126 my $entry = $headlist->[$i];
4127 my %ref = %$entry;
4128 my $curr = $ref{'id'} eq $head;
4129 if ($alternate) {
4130 print "<tr class=\"dark\">\n";
4131 } else {
4132 print "<tr class=\"light\">\n";
4134 $alternate ^= 1;
4135 print "<td><i>$ref{'age'}</i></td>\n" .
4136 ($curr ? "<td class=\"current_head\">" : "<td>") .
4137 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),
4138 -class => "list name"},esc_html($ref{'name'})) .
4139 "</td>\n" .
4140 "<td class=\"link\">" .
4141 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})}, "log") . " | " .
4142 $cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})}, "tree") .
4143 "</td>\n" .
4144 "</tr>";
4146 if (defined $extra) {
4147 print "<tr>\n" .
4148 "<td colspan=\"3\">$extra</td>\n" .
4149 "</tr>\n";
4151 print "</table>\n";
4154 sub git_search_grep_body {
4155 my ($commitlist, $from, $to, $extra) = @_;
4156 $from = 0 unless defined $from;
4157 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
4159 print "<table class=\"commit_search\">\n";
4160 my $alternate = 1;
4161 for (my $i = $from; $i <= $to; $i++) {
4162 my %co = %{$commitlist->[$i]};
4163 if (!%co) {
4164 next;
4166 my $commit = $co{'id'};
4167 if ($alternate) {
4168 print "<tr class=\"dark\">\n";
4169 } else {
4170 print "<tr class=\"light\">\n";
4172 $alternate ^= 1;
4173 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
4174 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4175 "<td><i>" . $author . "</i></td>\n" .
4176 "<td>" .
4177 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4178 -class => "list subject"},
4179 chop_and_escape_str($co{'title'}, 50) . "<br/>");
4180 my $comment = $co{'comment'};
4181 foreach my $line (@$comment) {
4182 if ($line =~ m/^(.*?)($search_regexp)(.*)$/i) {
4183 my ($lead, $match, $trail) = ($1, $2, $3);
4184 $match = chop_str($match, 70, 5, 'center');
4185 my $contextlen = int((80 - length($match))/2);
4186 $contextlen = 30 if ($contextlen > 30);
4187 $lead = chop_str($lead, $contextlen, 10, 'left');
4188 $trail = chop_str($trail, $contextlen, 10, 'right');
4190 $lead = esc_html($lead);
4191 $match = esc_html($match);
4192 $trail = esc_html($trail);
4194 print "$lead<span class=\"match\">$match</span>$trail<br />";
4197 print "</td>\n" .
4198 "<td class=\"link\">" .
4199 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
4200 " | " .
4201 $cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})}, "commitdiff") .
4202 " | " .
4203 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
4204 print "</td>\n" .
4205 "</tr>\n";
4207 if (defined $extra) {
4208 print "<tr>\n" .
4209 "<td colspan=\"3\">$extra</td>\n" .
4210 "</tr>\n";
4212 print "</table>\n";
4215 ## ======================================================================
4216 ## ======================================================================
4217 ## actions
4219 sub git_project_list {
4220 my $order = $cgi->param('o');
4221 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4222 die_error(400, "Unknown order parameter");
4225 my @list = git_get_projects_list();
4226 if (!@list) {
4227 die_error(404, "No projects found");
4230 git_header_html();
4231 if (-f $home_text) {
4232 print "<div class=\"index_include\">\n";
4233 open (my $fd, $home_text);
4234 print <$fd>;
4235 close $fd;
4236 print "</div>\n";
4238 print $cgi->startform(-method => "get") .
4239 "<p class=\"projsearch\">Search:\n" .
4240 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
4241 "</p>" .
4242 $cgi->end_form() . "\n";
4243 git_project_list_body(\@list, $order, undef, undef, undef, undef, $projlist_cache_lifetime);
4244 git_footer_html();
4247 sub git_forks {
4248 my $order = $cgi->param('o');
4249 if (defined $order && $order !~ m/none|project|descr|owner|age/) {
4250 die_error(400, "Unknown order parameter");
4253 my @list = git_get_projects_list($project);
4254 if (!@list) {
4255 die_error(404, "No forks found");
4258 git_header_html();
4259 git_print_page_nav('','');
4260 git_print_header_div('summary', "$project forks");
4261 git_project_list_body(\@list, $order);
4262 git_footer_html();
4265 sub git_project_index {
4266 my @projects = git_get_projects_list($project);
4268 print $cgi->header(
4269 -type => 'text/plain',
4270 -charset => 'utf-8',
4271 -content_disposition => 'inline; filename="index.aux"');
4273 foreach my $pr (@projects) {
4274 if (!exists $pr->{'owner'}) {
4275 $pr->{'owner'} = git_get_project_owner("$pr->{'path'}");
4278 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
4279 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
4280 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4281 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
4282 $path =~ s/ /\+/g;
4283 $owner =~ s/ /\+/g;
4285 print "$path $owner\n";
4289 sub git_summary {
4290 my $descr = git_get_project_description($project) || "none";
4291 my %co = parse_commit("HEAD");
4292 my %cd = %co ? parse_date($co{'committer_epoch'}, $co{'committer_tz'}) : ();
4293 my $head = $co{'id'};
4295 my $owner = git_get_project_owner($project);
4296 my $homepage = git_get_project_config('homepage');
4297 my $base_url = git_get_project_config('base_url');
4298 my $last_refresh = git_get_project_config("last_refresh");
4300 my $refs = git_get_references();
4301 # These get_*_list functions return one more to allow us to see if
4302 # there are more ...
4303 my @taglist = git_get_tags_list(16);
4304 my @headlist = git_get_heads_list(16);
4305 my @forklist;
4306 my ($check_forks) = gitweb_check_feature('forks');
4308 if ($check_forks) {
4309 @forklist = git_get_projects_list($project);
4312 git_header_html();
4313 git_print_page_nav('summary','', $head);
4315 print "<div class=\"title\">&nbsp;</div>\n";
4316 print "<table class=\"projects_list\">\n";
4317 print "<tr id=\"metadata_desc\"><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
4318 $homepage and print "<tr id=\"metadata_homepage\"><td>homepage URL</td><td>" . $cgi->a({-href => $homepage}, $homepage) . "</td></tr>\n";
4319 $base_url and print "<tr id=\"metadata_baseurl\"><td>repository URL</td><td>" . esc_html($base_url) . "</td></tr>\n";
4320 print "<tr id=\"metadata_owner\"><td>owner</td><td>" . esc_html($owner) . "</td></tr>\n";
4321 if (defined $cd{'rfc2822'}) {
4322 print "<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
4324 $last_refresh and print "<tr id=\"metadata_lrefresh\"><td>last refresh</td><td>$last_refresh</td></tr>\n";
4326 # use per project git URL list in $projectroot/$project/cloneurl
4327 # or make project git URL from git base URL and project name
4328 my $url_tag = $base_url ? "mirror URL" : "URL";
4329 my @url_list = git_get_project_url_list($project);
4330 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
4331 foreach my $git_url (@url_list) {
4332 next unless $git_url;
4333 print "<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";
4334 $url_tag = "";
4336 -f "$projectroot/$project/.nofetch" and $git_base_push_url and
4337 print "<tr id=\"metadata_pushurl\"><td>Push URL</td><td>$git_base_push_url/$project</td></tr>\n";
4339 # Tag cloud
4340 my $show_ctags = (gitweb_check_feature('ctags'))[0];
4341 if ($show_ctags) {
4342 my $ctags = git_get_project_ctags($project);
4343 my $cloud = git_populate_project_tagcloud($ctags);
4344 print "<tr id=\"metadata_ctags\"><td>Content tags:<br />";
4345 print "</td>\n<td>" unless %$ctags;
4346 print "<form action=\"$show_ctags\" method=\"post\"><input type=\"hidden\" name=\"p\" value=\"$project\" />Add: <input type=\"text\" name=\"t\" size=\"8\" /></form>";
4347 print "</td>\n<td>" if %$ctags;
4348 print git_show_project_tagcloud($cloud, 48);
4349 print "</td></tr>";
4352 print "</table>\n";
4354 if (-s "$projectroot/$project/README.html") {
4355 if (open my $fd, "$projectroot/$project/README.html") {
4356 print "<div class=\"title\">readme</div>\n" .
4357 "<div class=\"readme\">\n";
4358 print $_ while (<$fd>);
4359 print "\n</div>\n"; # class="readme"
4360 close $fd;
4364 # we need to request one more than 16 (0..15) to check if
4365 # those 16 are all
4366 my @commitlist = $head ? parse_commits($head, 17) : ();
4367 if (@commitlist) {
4368 git_print_header_div('shortlog');
4369 git_shortlog_body(\@commitlist, 0, 15, $refs,
4370 $#commitlist <= 15 ? undef :
4371 $cgi->a({-href => href(action=>"shortlog")}, "..."));
4374 if (@taglist) {
4375 git_print_header_div('tags');
4376 git_tags_body(\@taglist, 0, 15,
4377 $#taglist <= 15 ? undef :
4378 $cgi->a({-href => href(action=>"tags")}, "..."));
4381 if (@headlist) {
4382 git_print_header_div('heads');
4383 git_heads_body(\@headlist, $head, 0, 15,
4384 $#headlist <= 15 ? undef :
4385 $cgi->a({-href => href(action=>"heads")}, "..."));
4388 if (@forklist) {
4389 git_print_header_div('forks');
4390 git_project_list_body(\@forklist, 'age', 0, 15,
4391 $#forklist <= 15 ? undef :
4392 $cgi->a({-href => href(action=>"forks")}, "..."),
4393 'no_header');
4396 git_footer_html();
4399 sub git_tag {
4400 my $head = git_get_head_hash($project);
4401 git_header_html();
4402 git_print_page_nav('','', $head,undef,$head);
4403 my %tag = parse_tag($hash);
4405 if (! %tag) {
4406 die_error(404, "Unknown tag object");
4409 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
4410 print "<div class=\"title_text\">\n" .
4411 "<table class=\"object_header\">\n" .
4412 "<tr>\n" .
4413 "<td>object</td>\n" .
4414 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4415 $tag{'object'}) . "</td>\n" .
4416 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
4417 $tag{'type'}) . "</td>\n" .
4418 "</tr>\n";
4419 if (defined($tag{'author'})) {
4420 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
4421 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
4422 print "<tr><td></td><td>" . $ad{'rfc2822'} .
4423 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
4424 "</td></tr>\n";
4426 print "</table>\n\n" .
4427 "</div>\n";
4428 print "<div class=\"page_body\">";
4429 my $comment = $tag{'comment'};
4430 foreach my $line (@$comment) {
4431 chomp $line;
4432 print esc_html($line, -nbsp=>1) . "<br/>\n";
4434 print "</div>\n";
4435 git_footer_html();
4438 sub git_blame_data {
4439 my $fd;
4440 my $ftype;
4442 my ($have_blame) = gitweb_check_feature('blame');
4443 if (!$have_blame) {
4444 die_error('403 Permission denied', "Permission denied");
4446 die_error('404 Not Found', "File name not defined") if (!$file_name);
4447 $hash_base ||= git_get_head_hash($project);
4448 die_error(undef, "Couldn't find base commit") unless ($hash_base);
4449 my %co = parse_commit($hash_base)
4450 or die_error(undef, "Reading commit failed");
4451 if (!defined $hash) {
4452 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4453 or die_error(undef, "Error looking up file");
4455 $ftype = git_get_type($hash);
4456 if ($ftype !~ "blob") {
4457 die_error("400 Bad Request", "Object is not a blob");
4459 open ($fd, "-|", git_cmd(), "blame", '--incremental', $hash_base, '--',
4460 $file_name)
4461 or die_error(undef, "Open git-blame --incremental failed");
4463 print $cgi->header(-type=>"text/plain", -charset => 'utf-8',
4464 -status=> "200 OK");
4466 while(<$fd>) {
4467 if (/^([0-9a-f]{40}) ([0-9]+) ([0-9]+) ([0-9]+)/ or
4468 /^author-time |^author |^filename /) {
4469 print;
4473 close $fd or print "Reading blame data failed\n";
4476 sub git_blame_common {
4477 my ($type) = @_;
4479 my $fd;
4480 my $ftype;
4482 gitweb_check_feature('blame')
4483 or die_error(403, "Blame view not allowed");
4485 die_error(400, "No file name given") unless $file_name;
4486 $hash_base ||= git_get_head_hash($project);
4487 die_error(404, "Couldn't find base commit") unless ($hash_base);
4488 my %co = parse_commit($hash_base)
4489 or die_error(404, "Commit not found");
4490 if (!defined $hash) {
4491 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
4492 or die_error(404, "Error looking up file");
4494 $ftype = git_get_type($hash);
4495 if ($ftype !~ "blob") {
4496 die_error(400, "Object is not a blob");
4498 if ($type eq 'incremental') {
4499 open ($fd, "-|", git_cmd(), 'cat-file', 'blob', $hash)
4500 or die_error(undef, "Open git-cat-file failed");
4501 } else {
4502 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
4503 $file_name, $hash_base)
4504 or die_error(500, "Open git-blame failed");
4506 git_header_html();
4507 my $formats_nav =
4508 $cgi->a({-href => href(action=>"blob", -replay=>1)},
4509 "blob") .
4510 " | " .
4511 $cgi->a({-href => href(action=>"history", -replay=>1)},
4512 "history") .
4513 " | " .
4514 $cgi->a({-href => href(action=>"blame", file_name=>$file_name), -class => "blamelink"},
4515 "HEAD");
4516 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4517 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4518 git_print_page_path($file_name, $ftype, $hash_base);
4519 my @rev_color = (qw(light2 dark2));
4520 my $num_colors = scalar(@rev_color);
4521 my $current_color = 0;
4522 my $last_rev;
4523 print <<HTML;
4525 <div class="page_body">
4526 <table class="blame">
4527 <tr><th>Commit&nbsp;<a href="javascript:extra_blame_columns()" id="columns_expander">[+]</a></th>
4528 <th class="extra_column">Author</th>
4529 <th class="extra_column">Date</th>
4530 <th>Line</th>
4531 <th>Data</th></tr>
4532 HTML
4533 my %metainfo = ();
4534 my $linenr = 0;
4535 while (<$fd>) {
4536 chomp;
4537 if ($type eq 'incremental') {
4538 # Empty stage with just the file contents
4539 $linenr += 1;
4540 print "<tr id=\"l$linenr\" class=\"light2\">";
4541 print '<td class="sha1"><a href=""></a></td>';
4542 print "<td class=\"extra_column\"></td>";
4543 print "<td class=\"extra_column\"></td>";
4544 print "<td class=\"linenr\"><a class=\"linenr\" href=\"\">$linenr</a></td><td class=\"pre\">" . esc_html($_) . "</td>\n";
4545 print "</tr>\n";
4546 next;
4549 my ($full_rev, $orig_lineno, $lineno, $group_size) =
4550 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
4551 if (!exists $metainfo{$full_rev}) {
4552 $metainfo{$full_rev} = {};
4554 my $meta = $metainfo{$full_rev};
4555 while (<$fd>) {
4556 last if (s/^\t//);
4557 if (/^(\S+) (.*)$/) {
4558 $meta->{$1} = $2;
4561 my $data = $_;
4562 chomp $data;
4563 my $rev = substr($full_rev, 0, 8);
4564 my $author = $meta->{'author'};
4565 my %date = parse_date($meta->{'author-time'},
4566 $meta->{'author-tz'});
4567 my $date = $date{'iso-tz'};
4568 if ($group_size) {
4569 $current_color = ++$current_color % $num_colors;
4571 print "<tr class=\"$rev_color[$current_color]\">\n";
4572 if ($group_size) {
4573 my $rowspan = $group_size > 1 ? " rowspan=\"$group_size\"" : "";
4574 print "<td class=\"sha1\"";
4575 print " title=\"". esc_html($author) . ", $date\"";
4576 print "$rowspan>";
4577 print $cgi->a({-href => href(action=>"commit",
4578 hash=>$full_rev,
4579 file_name=>$file_name)},
4580 esc_html($rev));
4581 print "</td>\n";
4582 print "<td class=\"extra_column\" $rowspan>". esc_html($author) . "</td>";
4583 print "<td class=\"extra_column\" $rowspan>". $date . "</td>";
4585 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
4586 or die_error(500, "Open git-rev-parse failed");
4587 my $parent_commit = <$dd>;
4588 close $dd;
4589 chomp($parent_commit);
4590 my $blamed = href(action => 'blame',
4591 file_name => $meta->{'filename'},
4592 hash_base => $parent_commit);
4593 print "<td class=\"linenr\">";
4594 print $cgi->a({ -href => "$blamed#l$orig_lineno",
4595 -id => "l$lineno",
4596 -class => "linenr" },
4597 esc_html($lineno));
4598 print "</td>";
4599 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
4600 print "</tr>\n";
4603 print "</table>\n";
4604 print "</div>";
4605 close $fd
4606 or print "Reading blob failed\n";
4608 if ($type eq 'incremental') {
4609 print "<script type=\"text/javascript\">\n";
4610 print "startBlame(\"" . href(action=>"blame_data", hash_base=>$hash_base, file_name=>$file_name) . "\", \"" .
4611 href(-partial_query=>1) . "\");\n";
4612 print "</script>\n";
4615 git_footer_html();
4618 sub git_blame_incremental {
4619 git_blame_common('incremental');
4622 sub git_blame {
4623 git_blame_common('oneshot');
4626 sub git_tags {
4627 my $head = git_get_head_hash($project);
4628 git_header_html();
4629 git_print_page_nav('','', $head,undef,$head);
4630 git_print_header_div('summary', $project);
4632 my @tagslist = git_get_tags_list();
4633 if (@tagslist) {
4634 git_tags_body(\@tagslist);
4636 git_footer_html();
4639 sub git_heads {
4640 my $head = git_get_head_hash($project);
4641 git_header_html();
4642 git_print_page_nav('','', $head,undef,$head);
4643 git_print_header_div('summary', $project);
4645 my @headslist = git_get_heads_list();
4646 if (@headslist) {
4647 git_heads_body(\@headslist, $head);
4649 git_footer_html();
4652 sub git_blob_plain {
4653 my $type = shift;
4654 my $expires;
4656 if (!defined $hash) {
4657 if (defined $file_name) {
4658 my $base = $hash_base || git_get_head_hash($project);
4659 $hash = git_get_hash_by_path($base, $file_name, "blob")
4660 or die_error(404, "Cannot find file");
4661 } else {
4662 die_error(400, "No file name defined");
4664 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4665 # blobs defined by non-textual hash id's can be cached
4666 $expires = "+1d";
4669 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4670 or die_error(500, "Open git-cat-file blob '$hash' failed");
4672 # content-type (can include charset)
4673 $type = blob_contenttype($fd, $file_name, $type);
4675 # "save as" filename, even when no $file_name is given
4676 my $save_as = "$hash";
4677 if (defined $file_name) {
4678 $save_as = $file_name;
4679 } elsif ($type =~ m/^text\//) {
4680 $save_as .= '.txt';
4683 print $cgi->header(
4684 -type => $type,
4685 -expires => $expires,
4686 -content_disposition => 'inline; filename="' . $save_as . '"');
4687 undef $/;
4688 binmode STDOUT, ':raw';
4689 print <$fd>;
4690 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4691 $/ = "\n";
4692 close $fd;
4695 sub git_blob {
4696 my $expires;
4698 if (!defined $hash) {
4699 if (defined $file_name) {
4700 my $base = $hash_base || git_get_head_hash($project);
4701 $hash = git_get_hash_by_path($base, $file_name, "blob")
4702 or die_error(404, "Cannot find file");
4703 } else {
4704 die_error(400, "No file name defined");
4706 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
4707 # blobs defined by non-textual hash id's can be cached
4708 $expires = "+1d";
4711 my ($have_blame) = gitweb_check_feature('blame');
4712 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
4713 or die_error(500, "Couldn't cat $file_name, $hash");
4714 my $mimetype = blob_mimetype($fd, $file_name);
4715 if ($mimetype !~ m!^(?:text/|image/(?:gif|png|jpeg)$)! && -B $fd) {
4716 close $fd;
4717 return git_blob_plain($mimetype);
4719 # we can have blame only for text/* mimetype
4720 $have_blame &&= ($mimetype =~ m!^text/!);
4722 git_header_html(undef, $expires);
4723 my $formats_nav = '';
4724 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4725 if (defined $file_name) {
4726 if ($have_blame) {
4727 $formats_nav .=
4728 $cgi->a({-href => href(action=>"blame", -replay=>1,
4729 -class => "blamelink")},
4730 "blame") .
4731 " | ";
4733 $formats_nav .=
4734 $cgi->a({-href => href(action=>"history", -replay=>1)},
4735 "history") .
4736 " | " .
4737 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4738 "raw") .
4739 " | " .
4740 $cgi->a({-href => href(action=>"blob",
4741 hash_base=>"HEAD", file_name=>$file_name)},
4742 "HEAD");
4743 } else {
4744 $formats_nav .=
4745 $cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
4746 "raw");
4748 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
4749 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
4750 } else {
4751 print "<div class=\"page_nav\">\n" .
4752 "<br/><br/></div>\n" .
4753 "<div class=\"title\">$hash</div>\n";
4755 git_print_page_path($file_name, "blob", $hash_base);
4756 print "<div class=\"page_body\">\n";
4757 if ($mimetype =~ m!^image/!) {
4758 print qq!<img type="$mimetype"!;
4759 if ($file_name) {
4760 print qq! alt="$file_name" title="$file_name"!;
4762 print qq! src="! .
4763 href(action=>"blob_plain", hash=>$hash,
4764 hash_base=>$hash_base, file_name=>$file_name) .
4765 qq!" />\n!;
4766 } else {
4767 my $nr;
4768 while (my $line = <$fd>) {
4769 chomp $line;
4770 $nr++;
4771 $line = untabify($line);
4772 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
4773 $nr, $nr, $nr, esc_html($line, -nbsp=>1);
4776 close $fd
4777 or print "Reading blob failed.\n";
4778 print "</div>";
4779 git_footer_html();
4782 sub git_tree {
4783 if (!defined $hash_base) {
4784 $hash_base = "HEAD";
4786 if (!defined $hash) {
4787 if (defined $file_name) {
4788 $hash = git_get_hash_by_path($hash_base, $file_name, "tree");
4789 } else {
4790 $hash = $hash_base;
4793 die_error(404, "No such tree") unless defined($hash);
4794 $/ = "\0";
4795 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
4796 or die_error(500, "Open git-ls-tree failed");
4797 my @entries = map { chomp; $_ } <$fd>;
4798 close $fd or die_error(404, "Reading tree failed");
4799 $/ = "\n";
4801 my $refs = git_get_references();
4802 my $ref = format_ref_marker($refs, $hash_base);
4803 git_header_html();
4804 my $basedir = '';
4805 my ($have_blame) = gitweb_check_feature('blame');
4806 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
4807 my @views_nav = ();
4808 if (defined $file_name) {
4809 push @views_nav,
4810 $cgi->a({-href => href(action=>"history", -replay=>1)},
4811 "history"),
4812 $cgi->a({-href => href(action=>"tree",
4813 hash_base=>"HEAD", file_name=>$file_name)},
4814 "HEAD"),
4816 my $snapshot_links = format_snapshot_links($hash);
4817 if (defined $snapshot_links) {
4818 # FIXME: Should be available when we have no hash base as well.
4819 push @views_nav, $snapshot_links;
4821 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4822 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
4823 } else {
4824 undef $hash_base;
4825 print "<div class=\"page_nav\">\n";
4826 print "<br/><br/></div>\n";
4827 print "<div class=\"title\">$hash</div>\n";
4829 if (defined $file_name) {
4830 $basedir = $file_name;
4831 if ($basedir ne '' && substr($basedir, -1) ne '/') {
4832 $basedir .= '/';
4834 git_print_page_path($file_name, 'tree', $hash_base);
4836 print "<div class=\"page_body\">\n";
4837 print "<table class=\"tree\">\n";
4838 my $alternate = 1;
4839 # '..' (top directory) link if possible
4840 if (defined $hash_base &&
4841 defined $file_name && $file_name =~ m![^/]+$!) {
4842 if ($alternate) {
4843 print "<tr class=\"dark\">\n";
4844 } else {
4845 print "<tr class=\"light\">\n";
4847 $alternate ^= 1;
4849 my $up = $file_name;
4850 $up =~ s!/?[^/]+$!!;
4851 undef $up unless $up;
4852 # based on git_print_tree_entry
4853 print '<td class="mode">' . mode_str('040000') . "</td>\n";
4854 print '<td class="list">';
4855 print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
4856 file_name=>$up)},
4857 "..");
4858 print "</td>\n";
4859 print "<td class=\"link\"></td>\n";
4861 print "</tr>\n";
4863 foreach my $line (@entries) {
4864 my %t = parse_ls_tree_line($line, -z => 1);
4866 if ($alternate) {
4867 print "<tr class=\"dark\">\n";
4868 } else {
4869 print "<tr class=\"light\">\n";
4871 $alternate ^= 1;
4873 git_print_tree_entry(\%t, $basedir, $hash_base, $have_blame);
4875 print "</tr>\n";
4877 print "</table>\n" .
4878 "</div>";
4879 git_footer_html();
4882 sub git_snapshot {
4883 my @supported_fmts = gitweb_check_feature('snapshot');
4884 @supported_fmts = filter_snapshot_fmts(@supported_fmts);
4886 my $format = $cgi->param('sf');
4887 if (!@supported_fmts) {
4888 die_error(403, "Snapshots not allowed");
4890 # default to first supported snapshot format
4891 $format ||= $supported_fmts[0];
4892 if ($format !~ m/^[a-z0-9]+$/) {
4893 die_error(400, "Invalid snapshot format parameter");
4894 } elsif (!exists($known_snapshot_formats{$format})) {
4895 die_error(400, "Unknown snapshot format");
4896 } elsif (!grep($_ eq $format, @supported_fmts)) {
4897 die_error(403, "Unsupported snapshot format");
4900 if (!defined $hash) {
4901 $hash = git_get_head_hash($project);
4904 my $name = $project;
4905 $name =~ s,([^/])/*\.git$,$1,;
4906 $name = basename($name);
4907 my $filename = to_utf8($name);
4908 $name =~ s/\047/\047\\\047\047/g;
4909 my $cmd;
4910 $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4911 $cmd = quote_command(
4912 git_cmd(), 'archive',
4913 "--format=$known_snapshot_formats{$format}{'format'}",
4914 "--prefix=$name/", $hash);
4915 if (exists $known_snapshot_formats{$format}{'compressor'}) {
4916 $cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
4919 print $cgi->header(
4920 -type => $known_snapshot_formats{$format}{'type'},
4921 -content_disposition => 'inline; filename="' . "$filename" . '"',
4922 -status => '200 OK');
4924 open my $fd, "-|", $cmd
4925 or die_error(500, "Execute git-archive failed");
4926 binmode STDOUT, ':raw';
4927 print <$fd>;
4928 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4929 close $fd;
4932 sub git_log {
4933 my $head = git_get_head_hash($project);
4934 if (!defined $hash) {
4935 $hash = $head;
4937 if (!defined $page) {
4938 $page = 0;
4940 my $refs = git_get_references();
4942 my @commitlist = parse_commits($hash, 101, (100 * $page));
4944 my $paging_nav = format_log_nav('log', $hash, $head, $page, $#commitlist >= 100);
4947 local $action = 'fulllog';
4948 git_header_html();
4950 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
4952 if (!@commitlist) {
4953 my %co = parse_commit($hash);
4955 git_print_header_div('summary', $project);
4956 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
4958 my $to = ($#commitlist >= 99) ? (99) : ($#commitlist);
4959 for (my $i = 0; $i <= $to; $i++) {
4960 my %co = %{$commitlist[$i]};
4961 next if !%co;
4962 my $commit = $co{'id'};
4963 my $ref = format_ref_marker($refs, $commit);
4964 my %ad = parse_date($co{'author_epoch'});
4965 git_print_header_div('commit',
4966 "<span class=\"age\">$co{'age_string'}</span>" .
4967 esc_html($co{'title'}) . $ref,
4968 $commit);
4969 print "<div class=\"title_text\">\n" .
4970 "<div class=\"log_link\">\n" .
4971 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
4972 " | " .
4973 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
4974 " | " .
4975 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
4976 "<br/>\n" .
4977 "</div>\n" .
4978 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
4979 "</div>\n";
4981 print "<div class=\"log_body\">\n";
4982 git_print_log($co{'comment'}, -final_empty_line=> 1);
4983 print "</div>\n";
4985 if ($#commitlist >= 100) {
4986 print "<div class=\"page_nav\">\n";
4987 print $cgi->a({-href => href(-replay=>1, page=>$page+1),
4988 -accesskey => "n", -title => "Alt-n"}, "next");
4989 print "</div>\n";
4991 git_footer_html();
4994 sub git_commit {
4995 $hash ||= $hash_base || "HEAD";
4996 my %co = parse_commit($hash)
4997 or die_error(404, "Unknown commit object");
4998 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
4999 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
5001 my $parent = $co{'parent'};
5002 my $parents = $co{'parents'}; # listref
5004 # we need to prepare $formats_nav before any parameter munging
5005 my $formats_nav;
5006 if (!defined $parent) {
5007 # --root commitdiff
5008 $formats_nav .= '(initial)';
5009 } elsif (@$parents == 1) {
5010 # single parent commit
5011 $formats_nav .=
5012 '(parent: ' .
5013 $cgi->a({-href => href(action=>"commit",
5014 hash=>$parent)},
5015 esc_html(substr($parent, 0, 7))) .
5016 ')';
5017 } else {
5018 # merge commit
5019 $formats_nav .=
5020 '(merge: ' .
5021 join(' ', map {
5022 $cgi->a({-href => href(action=>"commit",
5023 hash=>$_)},
5024 esc_html(substr($_, 0, 7)));
5025 } @$parents ) .
5026 ')';
5029 if (!defined $parent) {
5030 $parent = "--root";
5032 my @difftree;
5033 open my $fd, "-|", git_cmd(), "diff-tree", '-r', "--no-commit-id",
5034 @diff_opts,
5035 (@$parents <= 1 ? $parent : '-c'),
5036 $hash, "--"
5037 or die_error(500, "Open git-diff-tree failed");
5038 @difftree = map { chomp; $_ } <$fd>;
5039 close $fd or die_error(404, "Reading git-diff-tree failed");
5041 # non-textual hash id's can be cached
5042 my $expires;
5043 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5044 $expires = "+1d";
5046 my $refs = git_get_references();
5047 my $ref = format_ref_marker($refs, $co{'id'});
5049 git_header_html(undef, $expires);
5050 git_print_page_nav('commit', '',
5051 $hash, $co{'tree'}, $hash,
5052 $formats_nav);
5054 if (defined $co{'parent'}) {
5055 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
5056 } else {
5057 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
5059 print "<div class=\"title_text\">\n" .
5060 "<table class=\"object_header\">\n";
5061 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
5062 "<tr>" .
5063 "<td></td><td> $ad{'rfc2822'}";
5064 if ($ad{'hour_local'} < 6) {
5065 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
5066 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
5067 } else {
5068 printf(" (%02d:%02d %s)",
5069 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
5071 print "</td>" .
5072 "</tr>\n";
5073 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
5074 print "<tr><td></td><td> $cd{'rfc2822'}" .
5075 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
5076 "</td></tr>\n";
5077 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5078 print "<tr>" .
5079 "<td>tree</td>" .
5080 "<td class=\"sha1\">" .
5081 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
5082 class => "list"}, $co{'tree'}) .
5083 "</td>" .
5084 "<td class=\"link\">" .
5085 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
5086 "tree");
5087 my $snapshot_links = format_snapshot_links($hash);
5088 if (defined $snapshot_links) {
5089 print " | " . $snapshot_links;
5091 print "</td>" .
5092 "</tr>\n";
5094 foreach my $par (@$parents) {
5095 print "<tr>" .
5096 "<td>parent</td>" .
5097 "<td class=\"sha1\">" .
5098 $cgi->a({-href => href(action=>"commit", hash=>$par),
5099 class => "list"}, $par) .
5100 "</td>" .
5101 "<td class=\"link\">" .
5102 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
5103 " | " .
5104 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
5105 "</td>" .
5106 "</tr>\n";
5108 print "</table>".
5109 "</div>\n";
5111 print "<div class=\"page_body\">\n";
5112 git_print_log($co{'comment'});
5113 print "</div>\n";
5115 git_difftree_body(\@difftree, undef, undef, $hash, @$parents);
5117 git_footer_html();
5120 sub git_object {
5121 # object is defined by:
5122 # - hash or hash_base alone
5123 # - hash_base and file_name
5124 my $type;
5126 # - hash or hash_base alone
5127 if ($hash || ($hash_base && !defined $file_name)) {
5128 my $object_id = $hash || $hash_base;
5130 open my $fd, "-|", quote_command(
5131 git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
5132 or die_error(404, "Object does not exist");
5133 $type = <$fd>;
5134 chomp $type;
5135 close $fd
5136 or die_error(404, "Object does not exist");
5138 # - hash_base and file_name
5139 } elsif ($hash_base && defined $file_name) {
5140 $file_name =~ s,/+$,,;
5142 system(git_cmd(), "cat-file", '-e', $hash_base) == 0
5143 or die_error(404, "Base object does not exist");
5145 # here errors should not hapen
5146 open my $fd, "-|", git_cmd(), "ls-tree", $hash_base, "--", $file_name
5147 or die_error(500, "Open git-ls-tree failed");
5148 my $line = <$fd>;
5149 close $fd;
5151 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
5152 unless ($line && $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {
5153 die_error(404, "File or directory for given base does not exist");
5155 $type = $2;
5156 $hash = $3;
5157 } else {
5158 die_error(400, "Not enough information to find object");
5161 print $cgi->redirect(-uri => href(action=>$type, -full=>1,
5162 hash=>$hash, hash_base=>$hash_base,
5163 file_name=>$file_name),
5164 -status => '302 Found');
5167 sub git_blobdiff {
5168 my $format = shift || 'html';
5170 my $fd;
5171 my @difftree;
5172 my %diffinfo;
5173 my $expires = '+1d';
5174 my ($from, $to);
5176 $file_parent ||= $file_name;
5178 # non-textual hash id's can be cached
5179 if (defined $hash && $hash !~ m/^[0-9a-fA-F]{40}$/) {
5180 $expires = undef;
5181 } elsif (defined $hash_parent && $hash_parent !~ m/^[0-9a-fA-F]{40}$/) {
5182 $expires = undef;
5183 } elsif (defined $hash_base && $hash_base !~ m/^[0-9a-fA-F]{40}$/) {
5184 $expires = undef;
5185 } elsif (defined $hash_parent_base && $hash_parent_base !~ m/^[0-9a-fA-F]{40}$/) {
5186 $expires = undef;
5189 # if hash parameter is missing, read it from the commit.
5190 if (defined $hash_base && defined $file_name && !defined $hash) {
5191 $hash = git_get_hash_by_path($hash_base, $file_name);
5194 if (defined $hash_parent_base && defined $file_parent && !defined $hash_parent) {
5195 $hash_parent = git_get_hash_by_path($hash_parent_base, $file_parent);
5198 if (!defined $hash || ! defined $hash_parent) {
5199 die_error(404, "Missing one of the blob diff parameters");
5202 if (defined $hash_base && defined $file_name) {
5203 $to = $hash_base . ':' . $file_name;
5204 } else {
5205 $to = $hash;
5208 if (defined $hash_parent_base && defined $file_parent) {
5209 $from = $hash_parent_base . ':' . $file_parent;
5210 } else {
5211 $from = $hash_parent;
5214 # fake git-diff-tree raw output
5215 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
5216 $diffinfo{'from_id'} = $hash_parent;
5217 $diffinfo{'to_id'} = $hash;
5218 if (defined $file_name) {
5219 $diffinfo{'status'} = '2';
5220 $diffinfo{'from_file'} = $file_parent;
5221 $diffinfo{'to_file'} = $file_name;
5222 } else { # no filename given
5223 $diffinfo{'status'} = '2';
5224 $diffinfo{'from_file'} = $hash_parent;
5225 $diffinfo{'to_file'} = $hash;
5228 # open patch output
5229 open $fd, "-|", git_cmd(), "diff", @diff_opts, '-p', "--full-index",
5230 ($format eq 'html' ? "--raw" : ()), $from, $to, "--"
5231 or die_error(500, "Open git-diff failed");
5233 # header
5234 if ($format eq 'html') {
5235 my $formats_nav =
5236 $cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
5237 "raw");
5238 git_header_html(undef, $expires);
5239 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
5240 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
5241 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5242 } else {
5243 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
5244 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
5246 if (defined $file_name) {
5247 git_print_page_path($file_name, "blob", $hash_base);
5248 } else {
5249 print "<div class=\"page_path\"></div>\n";
5252 } elsif ($format eq 'plain') {
5253 my $patch_file_name = $file_name || $hash;
5254 print $cgi->header(
5255 -type => 'text/plain',
5256 -charset => 'utf-8',
5257 -expires => $expires,
5258 -content_disposition => 'inline; filename="' . "$patch_file_name" . '.patch"');
5260 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5262 } else {
5263 die_error(400, "Unknown blobdiff format");
5266 # patch
5267 if ($format eq 'html') {
5268 print "<div class=\"page_body\">\n";
5270 git_patchset_body($fd, [ \%diffinfo ], undef, undef, $hash_base, $hash_parent_base);
5271 close $fd;
5273 print "</div>\n"; # class="page_body"
5274 git_footer_html();
5276 } else {
5277 while (my $line = <$fd>) {
5278 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;
5279 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;
5281 print $line;
5283 last if $line =~ m!^\+\+\+!;
5285 local $/ = undef;
5286 print <$fd>;
5287 close $fd;
5291 sub git_blobdiff_plain {
5292 git_blobdiff('plain');
5295 sub git_commitdiff {
5296 my $format = shift || 'html';
5297 $hash ||= $hash_base || "HEAD";
5298 my %co = parse_commit($hash)
5299 or die_error(404, "Unknown commit object");
5301 # choose format for commitdiff for merge
5302 if (! defined $hash_parent && @{$co{'parents'}} > 1) {
5303 $hash_parent = '--cc';
5305 # we need to prepare $formats_nav before almost any parameter munging
5306 my $formats_nav;
5307 if ($format eq 'html') {
5308 $formats_nav =
5309 $cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
5310 "raw");
5312 if (defined $hash_parent &&
5313 $hash_parent ne '-c' && $hash_parent ne '--cc') {
5314 # commitdiff with two commits given
5315 my $hash_parent_short = $hash_parent;
5316 if ($hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
5317 $hash_parent_short = substr($hash_parent, 0, 7);
5319 $formats_nav .=
5320 ' (from';
5321 for (my $i = 0; $i < @{$co{'parents'}}; $i++) {
5322 if ($co{'parents'}[$i] eq $hash_parent) {
5323 $formats_nav .= ' parent ' . ($i+1);
5324 last;
5327 $formats_nav .= ': ' .
5328 $cgi->a({-href => href(action=>"commitdiff",
5329 hash=>$hash_parent)},
5330 esc_html($hash_parent_short)) .
5331 ')';
5332 } elsif (!$co{'parent'}) {
5333 # --root commitdiff
5334 $formats_nav .= ' (initial)';
5335 } elsif (scalar @{$co{'parents'}} == 1) {
5336 # single parent commit
5337 $formats_nav .=
5338 ' (parent: ' .
5339 $cgi->a({-href => href(action=>"commitdiff",
5340 hash=>$co{'parent'})},
5341 esc_html(substr($co{'parent'}, 0, 7))) .
5342 ')';
5343 } else {
5344 # merge commit
5345 if ($hash_parent eq '--cc') {
5346 $formats_nav .= ' | ' .
5347 $cgi->a({-href => href(action=>"commitdiff",
5348 hash=>$hash, hash_parent=>'-c')},
5349 'combined');
5350 } else { # $hash_parent eq '-c'
5351 $formats_nav .= ' | ' .
5352 $cgi->a({-href => href(action=>"commitdiff",
5353 hash=>$hash, hash_parent=>'--cc')},
5354 'compact');
5356 $formats_nav .=
5357 ' (merge: ' .
5358 join(' ', map {
5359 $cgi->a({-href => href(action=>"commitdiff",
5360 hash=>$_)},
5361 esc_html(substr($_, 0, 7)));
5362 } @{$co{'parents'}} ) .
5363 ')';
5367 my $hash_parent_param = $hash_parent;
5368 if (!defined $hash_parent_param) {
5369 # --cc for multiple parents, --root for parentless
5370 $hash_parent_param =
5371 @{$co{'parents'}} > 1 ? '--cc' : $co{'parent'} || '--root';
5374 # read commitdiff
5375 my $fd;
5376 my @difftree;
5377 if ($format eq 'html') {
5378 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5379 "--no-commit-id", "--patch-with-raw", "--full-index",
5380 $hash_parent_param, $hash, "--"
5381 or die_error(500, "Open git-diff-tree failed");
5383 while (my $line = <$fd>) {
5384 chomp $line;
5385 # empty line ends raw part of diff-tree output
5386 last unless $line;
5387 push @difftree, scalar parse_difftree_raw_line($line);
5390 } elsif ($format eq 'plain') {
5391 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5392 '-p', $hash_parent_param, $hash, "--"
5393 or die_error(500, "Open git-diff-tree failed");
5395 } else {
5396 die_error(400, "Unknown commitdiff format");
5399 # non-textual hash id's can be cached
5400 my $expires;
5401 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
5402 $expires = "+1d";
5405 # write commit message
5406 if ($format eq 'html') {
5407 my $refs = git_get_references();
5408 my $ref = format_ref_marker($refs, $co{'id'});
5410 git_header_html(undef, $expires);
5411 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5412 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5413 git_print_authorship(\%co);
5414 print "<div class=\"page_body\">\n";
5415 if (@{$co{'comment'}} > 1) {
5416 print "<div class=\"log\">\n";
5417 git_print_log($co{'comment'}, -final_empty_line=> 1, -remove_title => 1);
5418 print "</div>\n"; # class="log"
5421 } elsif ($format eq 'plain') {
5422 my $refs = git_get_references("tags");
5423 my $tagname = git_get_rev_name_tags($hash);
5424 my $filename = basename($project) . "-$hash.patch";
5426 print $cgi->header(
5427 -type => 'text/plain',
5428 -charset => 'utf-8',
5429 -expires => $expires,
5430 -content_disposition => 'inline; filename="' . "$filename" . '"');
5431 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5432 print "From: " . to_utf8($co{'author'}) . "\n";
5433 print "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n";
5434 print "Subject: " . to_utf8($co{'title'}) . "\n";
5436 print "X-Git-Tag: $tagname\n" if $tagname;
5437 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
5439 foreach my $line (@{$co{'comment'}}) {
5440 print to_utf8($line) . "\n";
5442 print "---\n\n";
5445 # write patch
5446 if ($format eq 'html') {
5447 my $use_parents = !defined $hash_parent ||
5448 $hash_parent eq '-c' || $hash_parent eq '--cc';
5449 git_difftree_body(\@difftree, undef, undef, $hash,
5450 $use_parents ? @{$co{'parents'}} : $hash_parent);
5451 print "<br/>\n";
5453 git_patchset_body($fd, \@difftree, undef, undef, $hash,
5454 $use_parents ? @{$co{'parents'}} : $hash_parent);
5455 close $fd;
5456 print "</div>\n"; # class="page_body"
5457 git_footer_html();
5459 } elsif ($format eq 'plain') {
5460 local $/ = undef;
5461 print <$fd>;
5462 close $fd
5463 or print "Reading git-diff-tree failed\n";
5467 sub git_commitdiff_plain {
5468 git_commitdiff('plain');
5471 sub git_history {
5472 if (!defined $hash_base) {
5473 $hash_base = git_get_head_hash($project);
5475 if (!defined $page) {
5476 $page = 0;
5478 my $ftype;
5479 my %co = parse_commit($hash_base)
5480 or die_error(404, "Unknown commit object");
5482 my $refs = git_get_references();
5483 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
5485 my @commitlist = parse_commits($hash_base, 101, (100 * $page),
5486 $file_name, "--full-history")
5487 or die_error(404, "No such file or directory on given branch");
5489 if (!defined $hash && defined $file_name) {
5490 # some commits could have deleted file in question,
5491 # and not have it in tree, but one of them has to have it
5492 for (my $i = 0; $i <= @commitlist; $i++) {
5493 $hash = git_get_hash_by_path($commitlist[$i]{'id'}, $file_name);
5494 last if defined $hash;
5497 if (defined $hash) {
5498 $ftype = git_get_type($hash);
5500 if (!defined $ftype) {
5501 die_error(500, "Unknown type of object");
5504 my $paging_nav = '';
5505 if ($page > 0) {
5506 $paging_nav .=
5507 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
5508 file_name=>$file_name)},
5509 "first");
5510 $paging_nav .= " &sdot; " .
5511 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5512 -accesskey => "p", -title => "Alt-p"}, "prev");
5513 } else {
5514 $paging_nav .= "first";
5515 $paging_nav .= " &sdot; prev";
5517 my $next_link = '';
5518 if ($#commitlist >= 100) {
5519 $next_link =
5520 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5521 -accesskey => "n", -title => "Alt-n"}, "next");
5522 $paging_nav .= " &sdot; $next_link";
5523 } else {
5524 $paging_nav .= " &sdot; next";
5527 git_header_html();
5528 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
5529 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
5530 git_print_page_path($file_name, $ftype, $hash_base);
5532 git_history_body(\@commitlist, 0, 99,
5533 $refs, $hash_base, $ftype, $next_link);
5535 git_footer_html();
5538 sub git_search {
5539 gitweb_check_feature('search') or die_error(403, "Search is disabled");
5540 if (!defined $searchtext) {
5541 die_error(400, "Text field is empty");
5543 if (!defined $hash) {
5544 $hash = git_get_head_hash($project);
5546 my %co = parse_commit($hash);
5547 if (!%co) {
5548 die_error(404, "Unknown commit object");
5550 if (!defined $page) {
5551 $page = 0;
5554 $searchtype ||= 'commit';
5555 if ($searchtype eq 'pickaxe') {
5556 # pickaxe may take all resources of your box and run for several minutes
5557 # with every query - so decide by yourself how public you make this feature
5558 gitweb_check_feature('pickaxe')
5559 or die_error(403, "Pickaxe is disabled");
5561 if ($searchtype eq 'grep') {
5562 gitweb_check_feature('grep')
5563 or die_error(403, "Grep is disabled");
5566 git_header_html();
5568 if ($searchtype eq 'commit' or $searchtype eq 'author' or $searchtype eq 'committer') {
5569 my $greptype;
5570 if ($searchtype eq 'commit') {
5571 $greptype = "--grep=";
5572 } elsif ($searchtype eq 'author') {
5573 $greptype = "--author=";
5574 } elsif ($searchtype eq 'committer') {
5575 $greptype = "--committer=";
5577 $greptype .= $searchtext;
5578 my @commitlist = parse_commits($hash, 101, (100 * $page), undef,
5579 $greptype, '--regexp-ignore-case',
5580 $search_use_regexp ? '--extended-regexp' : '--fixed-strings');
5582 my $paging_nav = '';
5583 if ($page > 0) {
5584 $paging_nav .=
5585 $cgi->a({-href => href(action=>"search", hash=>$hash,
5586 searchtext=>$searchtext,
5587 searchtype=>$searchtype)},
5588 "first");
5589 $paging_nav .= " &sdot; " .
5590 $cgi->a({-href => href(-replay=>1, page=>$page-1),
5591 -accesskey => "p", -title => "Alt-p"}, "prev");
5592 } else {
5593 $paging_nav .= "first";
5594 $paging_nav .= " &sdot; prev";
5596 my $next_link = '';
5597 if ($#commitlist >= 100) {
5598 $next_link =
5599 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5600 -accesskey => "n", -title => "Alt-n"}, "next");
5601 $paging_nav .= " &sdot; $next_link";
5602 } else {
5603 $paging_nav .= " &sdot; next";
5606 if ($#commitlist >= 100) {
5609 git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
5610 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5611 git_search_grep_body(\@commitlist, 0, 99, $next_link);
5614 if ($searchtype eq 'pickaxe') {
5615 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5616 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5618 print "<table class=\"pickaxe search\">\n";
5619 my $alternate = 1;
5620 $/ = "\n";
5621 open my $fd, '-|', git_cmd(), '--no-pager', 'log', @diff_opts,
5622 '--pretty=format:%H', '--no-abbrev', '--raw', "-S$searchtext",
5623 ($search_use_regexp ? '--pickaxe-regex' : ());
5624 undef %co;
5625 my @files;
5626 while (my $line = <$fd>) {
5627 chomp $line;
5628 next unless $line;
5630 my %set = parse_difftree_raw_line($line);
5631 if (defined $set{'commit'}) {
5632 # finish previous commit
5633 if (%co) {
5634 print "</td>\n" .
5635 "<td class=\"link\">" .
5636 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5637 " | " .
5638 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5639 print "</td>\n" .
5640 "</tr>\n";
5643 if ($alternate) {
5644 print "<tr class=\"dark\">\n";
5645 } else {
5646 print "<tr class=\"light\">\n";
5648 $alternate ^= 1;
5649 %co = parse_commit($set{'commit'});
5650 my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
5651 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
5652 "<td><i>$author</i></td>\n" .
5653 "<td>" .
5654 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
5655 -class => "list subject"},
5656 chop_and_escape_str($co{'title'}, 50) . "<br/>");
5657 } elsif (defined $set{'to_id'}) {
5658 next if ($set{'to_id'} =~ m/^0{40}$/);
5660 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
5661 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),
5662 -class => "list"},
5663 "<span class=\"match\">" . esc_path($set{'file'}) . "</span>") .
5664 "<br/>\n";
5667 close $fd;
5669 # finish last commit (warning: repetition!)
5670 if (%co) {
5671 print "</td>\n" .
5672 "<td class=\"link\">" .
5673 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
5674 " | " .
5675 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
5676 print "</td>\n" .
5677 "</tr>\n";
5680 print "</table>\n";
5683 if ($searchtype eq 'grep') {
5684 git_print_page_nav('','', $hash,$co{'tree'},$hash);
5685 git_print_header_div('commit', esc_html($co{'title'}), $hash);
5687 print "<table class=\"grep_search\">\n";
5688 my $alternate = 1;
5689 my $matches = 0;
5690 $/ = "\n";
5691 open my $fd, "-|", git_cmd(), 'grep', '-n',
5692 $search_use_regexp ? ('-E', '-i') : '-F',
5693 $searchtext, $co{'tree'};
5694 my $lastfile = '';
5695 while (my $line = <$fd>) {
5696 chomp $line;
5697 my ($file, $lno, $ltext, $binary);
5698 last if ($matches++ > 1000);
5699 if ($line =~ /^Binary file (.+) matches$/) {
5700 $file = $1;
5701 $binary = 1;
5702 } else {
5703 (undef, $file, $lno, $ltext) = split(/:/, $line, 4);
5705 if ($file ne $lastfile) {
5706 $lastfile and print "</td></tr>\n";
5707 if ($alternate++) {
5708 print "<tr class=\"dark\">\n";
5709 } else {
5710 print "<tr class=\"light\">\n";
5712 print "<td class=\"list\">".
5713 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5714 file_name=>"$file"),
5715 -class => "list"}, esc_path($file));
5716 print "</td><td>\n";
5717 $lastfile = $file;
5719 if ($binary) {
5720 print "<div class=\"binary\">Binary file</div>\n";
5721 } else {
5722 $ltext = untabify($ltext);
5723 if ($ltext =~ m/^(.*)($search_regexp)(.*)$/i) {
5724 $ltext = esc_html($1, -nbsp=>1);
5725 $ltext .= '<span class="match">';
5726 $ltext .= esc_html($2, -nbsp=>1);
5727 $ltext .= '</span>';
5728 $ltext .= esc_html($3, -nbsp=>1);
5729 } else {
5730 $ltext = esc_html($ltext, -nbsp=>1);
5732 print "<div class=\"pre\">" .
5733 $cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},
5734 file_name=>"$file").'#l'.$lno,
5735 -class => "linenr"}, sprintf('%4i', $lno))
5736 . ' ' . $ltext . "</div>\n";
5739 if ($lastfile) {
5740 print "</td></tr>\n";
5741 if ($matches > 1000) {
5742 print "<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";
5744 } else {
5745 print "<div class=\"diff nodifferences\">No matches found</div>\n";
5747 close $fd;
5749 print "</table>\n";
5751 git_footer_html();
5754 sub git_search_help {
5755 git_header_html();
5756 git_print_page_nav('','', $hash,$hash,$hash);
5757 print <<EOT;
5758 <p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without
5759 regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,
5760 the pattern entered is recognized as the POSIX extended
5761 <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case
5762 insensitive).</p>
5763 <dl>
5764 <dt><b>commit</b></dt>
5765 <dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
5767 my ($have_grep) = gitweb_check_feature('grep');
5768 if ($have_grep) {
5769 print <<EOT;
5770 <dt><b>grep</b></dt>
5771 <dd>All files in the currently selected tree (HEAD unless you are explicitly browsing
5772 a different one) are searched for the given pattern. On large trees, this search can take
5773 a while and put some strain on the server, so please use it with some consideration. Note that
5774 due to git-grep peculiarity, currently if regexp mode is turned off, the matches are
5775 case-sensitive.</dd>
5778 print <<EOT;
5779 <dt><b>author</b></dt>
5780 <dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>
5781 <dt><b>committer</b></dt>
5782 <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
5784 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
5785 if ($have_pickaxe) {
5786 print <<EOT;
5787 <dt><b>pickaxe</b></dt>
5788 <dd>All commits that caused the string to appear or disappear from any file (changes that
5789 added, removed or "modified" the string) will be listed. This search can take a while and
5790 takes a lot of strain on the server, so please use it wisely. Note that since you may be
5791 interested even in changes just changing the case as well, this search is case sensitive.</dd>
5794 print "</dl>\n";
5795 git_footer_html();
5798 sub git_shortlog {
5799 my $head = git_get_head_hash($project);
5800 if (!defined $hash) {
5801 $hash = $head;
5803 if (!defined $page) {
5804 $page = 0;
5806 my $refs = git_get_references();
5808 my $commit_hash = $hash;
5809 if (defined $hash_parent) {
5810 $commit_hash = "$hash_parent..$hash";
5812 my @commitlist = parse_commits($commit_hash, 101, (100 * $page));
5814 my $paging_nav = format_log_nav('shortlog', $hash, $head, $page, $#commitlist >= 100);
5816 my $next_link = '';
5817 if ($#commitlist >= 100) {
5818 $next_link =
5819 $cgi->a({-href => href(-replay=>1, page=>$page+1),
5820 -accesskey => "n", -title => "Alt-n"}, "next");
5823 git_header_html();
5824 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
5825 git_print_header_div('summary', $project);
5827 git_shortlog_body(\@commitlist, 0, 99, $refs, $next_link);
5829 git_footer_html();
5832 ## ......................................................................
5833 ## feeds (RSS, Atom; OPML)
5835 sub git_feed {
5836 my $format = shift || 'atom';
5837 my ($have_blame) = gitweb_check_feature('blame');
5839 # Atom: http://www.atomenabled.org/developers/syndication/
5840 # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
5841 if ($format ne 'rss' && $format ne 'atom') {
5842 die_error(400, "Unknown web feed format");
5845 # log/feed of current (HEAD) branch, log of given branch, history of file/directory
5846 my $head = $hash || 'HEAD';
5847 my @commitlist = parse_commits($head, 150, 0, $file_name);
5849 my %latest_commit;
5850 my %latest_date;
5851 my $content_type = "application/$format+xml";
5852 if (defined $cgi->http('HTTP_ACCEPT') &&
5853 $cgi->Accept('text/xml') > $cgi->Accept($content_type)) {
5854 # browser (feed reader) prefers text/xml
5855 $content_type = 'text/xml';
5857 if (defined($commitlist[0])) {
5858 %latest_commit = %{$commitlist[0]};
5859 %latest_date = parse_date($latest_commit{'author_epoch'});
5860 print $cgi->header(
5861 -type => $content_type,
5862 -charset => 'utf-8',
5863 -last_modified => $latest_date{'rfc2822'});
5864 } else {
5865 print $cgi->header(
5866 -type => $content_type,
5867 -charset => 'utf-8');
5870 # Optimization: skip generating the body if client asks only
5871 # for Last-Modified date.
5872 return if ($cgi->request_method() eq 'HEAD');
5874 # header variables
5875 my $title = "$site_name - $project/$action";
5876 my $feed_type = 'log';
5877 if (defined $hash) {
5878 $title .= " - '$hash'";
5879 $feed_type = 'branch log';
5880 if (defined $file_name) {
5881 $title .= " :: $file_name";
5882 $feed_type = 'history';
5884 } elsif (defined $file_name) {
5885 $title .= " - $file_name";
5886 $feed_type = 'history';
5888 $title .= " $feed_type";
5889 my $descr = git_get_project_description($project);
5890 if (defined $descr) {
5891 $descr = esc_html($descr);
5892 } else {
5893 $descr = "$project " .
5894 ($format eq 'rss' ? 'RSS' : 'Atom') .
5895 " feed";
5897 my $owner = git_get_project_owner($project);
5898 $owner = esc_html($owner);
5900 #header
5901 my $alt_url;
5902 if (defined $file_name) {
5903 $alt_url = href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);
5904 } elsif (defined $hash) {
5905 $alt_url = href(-full=>1, action=>"log", hash=>$hash);
5906 } else {
5907 $alt_url = href(-full=>1, action=>"summary");
5909 print qq!<?xml version="1.0" encoding="utf-8"?>\n!;
5910 if ($format eq 'rss') {
5911 print <<XML;
5912 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
5913 <channel>
5915 print "<title>$title</title>\n" .
5916 "<link>$alt_url</link>\n" .
5917 "<description>$descr</description>\n" .
5918 "<language>en</language>\n";
5919 } elsif ($format eq 'atom') {
5920 print <<XML;
5921 <feed xmlns="http://www.w3.org/2005/Atom">
5923 print "<title>$title</title>\n" .
5924 "<subtitle>$descr</subtitle>\n" .
5925 '<link rel="alternate" type="text/html" href="' .
5926 $alt_url . '" />' . "\n" .
5927 '<link rel="self" type="' . $content_type . '" href="' .
5928 $cgi->self_url() . '" />' . "\n" .
5929 "<id>" . href(-full=>1) . "</id>\n" .
5930 # use project owner for feed author
5931 "<author><name>$owner</name></author>\n";
5932 if (defined $favicon) {
5933 print "<icon>" . esc_url($favicon) . "</icon>\n";
5935 if (defined $logo_url) {
5936 # not twice as wide as tall: 72 x 27 pixels
5937 print "<logo>" . esc_url($logo) . "</logo>\n";
5939 if (! %latest_date) {
5940 # dummy date to keep the feed valid until commits trickle in:
5941 print "<updated>1970-01-01T00:00:00Z</updated>\n";
5942 } else {
5943 print "<updated>$latest_date{'iso-8601'}</updated>\n";
5947 # contents
5948 for (my $i = 0; $i <= $#commitlist; $i++) {
5949 my %co = %{$commitlist[$i]};
5950 my $commit = $co{'id'};
5951 # we read 150, we always show 30 and the ones more recent than 48 hours
5952 if (($i >= 20) && ((time - $co{'author_epoch'}) > 48*60*60)) {
5953 last;
5955 my %cd = parse_date($co{'author_epoch'});
5957 # get list of changed files
5958 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
5959 $co{'parent'} || "--root",
5960 $co{'id'}, "--", (defined $file_name ? $file_name : ())
5961 or next;
5962 my @difftree = map { chomp; $_ } <$fd>;
5963 close $fd
5964 or next;
5966 # print element (entry, item)
5967 my $co_url = href(-full=>1, action=>"commitdiff", hash=>$commit);
5968 if ($format eq 'rss') {
5969 print "<item>\n" .
5970 "<title>" . esc_html($co{'title'}) . "</title>\n" .
5971 "<author>" . esc_html($co{'author'}) . "</author>\n" .
5972 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
5973 "<guid isPermaLink=\"true\">$co_url</guid>\n" .
5974 "<link>$co_url</link>\n" .
5975 "<description>" . esc_html($co{'title'}) . "</description>\n" .
5976 "<content:encoded>" .
5977 "<![CDATA[\n";
5978 } elsif ($format eq 'atom') {
5979 print "<entry>\n" .
5980 "<title type=\"html\">" . esc_html($co{'title'}) . "</title>\n" .
5981 "<updated>$cd{'iso-8601'}</updated>\n" .
5982 "<author>\n" .
5983 " <name>" . esc_html($co{'author_name'}) . "</name>\n";
5984 if ($co{'author_email'}) {
5985 print " <email>" . esc_html($co{'author_email'}) . "</email>\n";
5987 print "</author>\n" .
5988 # use committer for contributor
5989 "<contributor>\n" .
5990 " <name>" . esc_html($co{'committer_name'}) . "</name>\n";
5991 if ($co{'committer_email'}) {
5992 print " <email>" . esc_html($co{'committer_email'}) . "</email>\n";
5994 print "</contributor>\n" .
5995 "<published>$cd{'iso-8601'}</published>\n" .
5996 "<link rel=\"alternate\" type=\"text/html\" href=\"$co_url\" />\n" .
5997 "<id>$co_url</id>\n" .
5998 "<content type=\"xhtml\" xml:base=\"" . esc_url($my_url) . "\">\n" .
5999 "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
6001 my $comment = $co{'comment'};
6002 print "<pre>\n";
6003 foreach my $line (@$comment) {
6004 $line = esc_html($line);
6005 print "$line\n";
6007 print "</pre><ul>\n";
6008 foreach my $difftree_line (@difftree) {
6009 my %difftree = parse_difftree_raw_line($difftree_line);
6010 next if !$difftree{'from_id'};
6012 my $file = $difftree{'file'} || $difftree{'to_file'};
6014 print "<li>" .
6015 "[" .
6016 $cgi->a({-href => href(-full=>1, action=>"blobdiff",
6017 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},
6018 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},
6019 file_name=>$file, file_parent=>$difftree{'from_file'}),
6020 -title => "diff"}, 'D');
6021 if ($have_blame) {
6022 print $cgi->a({-href => href(-full=>1, action=>"blame",
6023 file_name=>$file, hash_base=>$commit), -class => "blamelink",
6024 -title => "blame"}, 'B');
6026 # if this is not a feed of a file history
6027 if (!defined $file_name || $file_name ne $file) {
6028 print $cgi->a({-href => href(-full=>1, action=>"history",
6029 file_name=>$file, hash=>$commit),
6030 -title => "history"}, 'H');
6032 $file = esc_path($file);
6033 print "] ".
6034 "$file</li>\n";
6036 if ($format eq 'rss') {
6037 print "</ul>]]>\n" .
6038 "</content:encoded>\n" .
6039 "</item>\n";
6040 } elsif ($format eq 'atom') {
6041 print "</ul>\n</div>\n" .
6042 "</content>\n" .
6043 "</entry>\n";
6047 # end of feed
6048 if ($format eq 'rss') {
6049 print "</channel>\n</rss>\n";
6050 } elsif ($format eq 'atom') {
6051 print "</feed>\n";
6055 sub git_rss {
6056 git_feed('rss');
6059 sub git_atom {
6060 git_feed('atom');
6063 sub git_opml {
6064 my @list = git_get_projects_list();
6066 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
6067 print <<XML;
6068 <?xml version="1.0" encoding="utf-8"?>
6069 <opml version="1.0">
6070 <head>
6071 <title>$site_name OPML Export</title>
6072 </head>
6073 <body>
6074 <outline text="git RSS feeds">
6077 foreach my $pr (@list) {
6078 my %proj = %$pr;
6079 my $head = git_get_head_hash($proj{'path'});
6080 if (!defined $head) {
6081 next;
6083 $git_dir = "$projectroot/$proj{'path'}";
6084 my %co = parse_commit($head);
6085 if (!%co) {
6086 next;
6089 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
6090 my $rss = "$my_url?p=$proj{'path'};a=rss";
6091 my $html = "$my_url?p=$proj{'path'};a=summary";
6092 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
6094 print <<XML;
6095 </outline>
6096 </body>
6097 </opml>