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