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