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