gitweb: use decode_utf8 directly
[git/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
JN
21BEGIN {
22 CGI->compile() if $ENV{MOD_PERL};
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
32f4aacc
ML
74# show repository only if this file exists
75# (only effective if this variable evaluates to true)
76our $export_ok = "++GITWEB_EXPORT_OK++";
77
78# only allow viewing of repositories also shown on the overview page
79our $strict_export = "++GITWEB_STRICT_EXPORT++";
80
19a8721e
JN
81# list of git base URLs used for URL to where fetch project from,
82# i.e. full URL is "$git_base_url/$project"
d6b7e0b9 83our @git_base_url_list = grep { $_ ne '' } ("++GITWEB_BASE_URL++");
19a8721e 84
f5aa79d9 85# default blob_plain mimetype and default charset for text/plain blob
4a87b43e
DS
86our $default_blob_plain_mimetype = 'text/plain';
87our $default_text_plain_charset = undef;
f5aa79d9 88
2d007374
PB
89# file to use for guessing MIME types before trying /etc/mime.types
90# (relative to the current git repository)
4a87b43e 91our $mimetypes_file = undef;
2d007374 92
ddb8d900
AK
93# You define site-wide feature defaults here; override them with
94# $GITWEB_CONFIG as necessary.
952c65fc 95our %feature = (
17848fc6
JN
96 # feature => {
97 # 'sub' => feature-sub (subroutine),
98 # 'override' => allow-override (boolean),
99 # 'default' => [ default options...] (array reference)}
100 #
101 # if feature is overridable (it means that allow-override has true value,
102 # then feature-sub will be called with default options as parameters;
103 # return value of feature-sub indicates if to enable specified feature
104 #
105 # use gitweb_check_feature(<feature>) to check if <feature> is enabled
952c65fc 106
45a3b12c
PB
107 # Enable the 'blame' blob view, showing the last commit that modified
108 # each line in the file. This can be very CPU-intensive.
109
110 # To enable system wide have in $GITWEB_CONFIG
111 # $feature{'blame'}{'default'} = [1];
112 # To have project specific config enable override in $GITWEB_CONFIG
113 # $feature{'blame'}{'override'} = 1;
114 # and in project config gitweb.blame = 0|1;
952c65fc
JN
115 'blame' => {
116 'sub' => \&feature_blame,
117 'override' => 0,
118 'default' => [0]},
119
45a3b12c
PB
120 # Enable the 'snapshot' link, providing a compressed tarball of any
121 # tree. This can potentially generate high traffic if you have large
122 # project.
123
124 # To disable system wide have in $GITWEB_CONFIG
125 # $feature{'snapshot'}{'default'} = [undef];
126 # To have project specific config enable override in $GITWEB_CONFIG
bbee1d97 127 # $feature{'snapshot'}{'override'} = 1;
45a3b12c 128 # and in project config gitweb.snapshot = none|gzip|bzip2;
952c65fc
JN
129 'snapshot' => {
130 'sub' => \&feature_snapshot,
131 'override' => 0,
132 # => [content-encoding, suffix, program]
133 'default' => ['x-gzip', 'gz', 'gzip']},
04f7a94f 134
6be93511
RF
135 # Enable text search, which will list the commits which match author,
136 # committer or commit text to a given string. Enabled by default.
137 'search' => {
138 'override' => 0,
139 'default' => [1]},
140
45a3b12c
PB
141 # Enable the pickaxe search, which will list the commits that modified
142 # a given string in a file. This can be practical and quite faster
143 # alternative to 'blame', but still potentially CPU-intensive.
144
145 # To enable system wide have in $GITWEB_CONFIG
146 # $feature{'pickaxe'}{'default'} = [1];
147 # To have project specific config enable override in $GITWEB_CONFIG
148 # $feature{'pickaxe'}{'override'} = 1;
149 # and in project config gitweb.pickaxe = 0|1;
04f7a94f
JN
150 'pickaxe' => {
151 'sub' => \&feature_pickaxe,
152 'override' => 0,
153 'default' => [1]},
9e756904 154
45a3b12c
PB
155 # Make gitweb use an alternative format of the URLs which can be
156 # more readable and natural-looking: project name is embedded
157 # directly in the path and the query string contains other
158 # auxiliary information. All gitweb installations recognize
159 # URL in either format; this configures in which formats gitweb
160 # generates links.
161
162 # To enable system wide have in $GITWEB_CONFIG
163 # $feature{'pathinfo'}{'default'} = [1];
164 # Project specific override is not supported.
165
166 # Note that you will need to change the default location of CSS,
167 # favicon, logo and possibly other files to an absolute URL. Also,
168 # if gitweb.cgi serves as your indexfile, you will need to force
169 # $my_uri to contain the script name in your $GITWEB_CONFIG.
9e756904
MW
170 'pathinfo' => {
171 'override' => 0,
172 'default' => [0]},
e30496df
PB
173
174 # Make gitweb consider projects in project root subdirectories
175 # to be forks of existing projects. Given project $projname.git,
176 # projects matching $projname/*.git will not be shown in the main
177 # projects list, instead a '+' mark will be added to $projname
178 # there and a 'forks' view will be enabled for the project, listing
179 # all the forks. This feature is supported only if project list
180 # is taken from a directory, not file.
181
182 # To enable system wide have in $GITWEB_CONFIG
183 # $feature{'forks'}{'default'} = [1];
184 # Project specific override is not supported.
185 'forks' => {
186 'override' => 0,
187 'default' => [0]},
ddb8d900
AK
188);
189
190sub gitweb_check_feature {
191 my ($name) = @_;
dd1ad5f1 192 return unless exists $feature{$name};
952c65fc
JN
193 my ($sub, $override, @defaults) = (
194 $feature{$name}{'sub'},
195 $feature{$name}{'override'},
196 @{$feature{$name}{'default'}});
ddb8d900 197 if (!$override) { return @defaults; }
a9455919
MW
198 if (!defined $sub) {
199 warn "feature $name is not overrideable";
200 return @defaults;
201 }
ddb8d900
AK
202 return $sub->(@defaults);
203}
204
ddb8d900
AK
205sub feature_blame {
206 my ($val) = git_get_project_config('blame', '--bool');
207
208 if ($val eq 'true') {
209 return 1;
210 } elsif ($val eq 'false') {
211 return 0;
212 }
213
214 return $_[0];
215}
216
ddb8d900
AK
217sub feature_snapshot {
218 my ($ctype, $suffix, $command) = @_;
219
220 my ($val) = git_get_project_config('snapshot');
221
222 if ($val eq 'gzip') {
223 return ('x-gzip', 'gz', 'gzip');
224 } elsif ($val eq 'bzip2') {
225 return ('x-bzip2', 'bz2', 'bzip2');
226 } elsif ($val eq 'none') {
227 return ();
228 }
229
230 return ($ctype, $suffix, $command);
231}
232
de9272f4
LT
233sub gitweb_have_snapshot {
234 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
235 my $have_snapshot = (defined $ctype && defined $suffix);
236
237 return $have_snapshot;
238}
239
04f7a94f
JN
240sub feature_pickaxe {
241 my ($val) = git_get_project_config('pickaxe', '--bool');
242
243 if ($val eq 'true') {
244 return (1);
245 } elsif ($val eq 'false') {
246 return (0);
247 }
248
249 return ($_[0]);
250}
251
2172ce4b
JH
252# checking HEAD file with -e is fragile if the repository was
253# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed
254# and then pruned.
255sub check_head_link {
256 my ($dir) = @_;
257 my $headfile = "$dir/HEAD";
258 return ((-e $headfile) ||
259 (-l $headfile && readlink($headfile) =~ /^refs\/heads\//));
260}
261
262sub check_export_ok {
263 my ($dir) = @_;
264 return (check_head_link($dir) &&
265 (!$export_ok || -e "$dir/$export_ok"));
266}
267
6bcf4b46
JN
268# rename detection options for git-diff and git-diff-tree
269# - default is '-M', with the cost proportional to
270# (number of removed files) * (number of new files).
271# - more costly is '-C' (or '-C', '-M'), with the cost proportional to
272# (number of changed files + number of removed files) * (number of new files)
273# - even more costly is '-C', '--find-copies-harder' with cost
274# (number of files in the original tree) * (number of new files)
275# - one might want to include '-B' option, e.g. '-B', '-M'
276our @diff_opts = ('-M'); # taken from git_commit
277
06c084d2 278our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
4b5dc988 279do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
c8d138a8
JK
280
281# version of the core git binary
282our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
283
284$projects_list ||= $projectroot;
c8d138a8 285
154b4d78 286# ======================================================================
09bd7898 287# input validation and dispatch
4a87b43e 288our $action = $cgi->param('a');
09bd7898 289if (defined $action) {
c91da262 290 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
cac4bd94 291 die_error(undef, "Invalid action parameter");
b87d78d6 292 }
b87d78d6 293}
44ad2978 294
24d0693a 295# parameters which are pathnames
dd70235f 296our $project = $cgi->param('p');
13d02165 297if (defined $project) {
24d0693a 298 if (!validate_pathname($project) ||
7939fe44 299 !(-d "$projectroot/$project") ||
2172ce4b 300 !check_head_link("$projectroot/$project") ||
32f4aacc
ML
301 ($export_ok && !(-e "$projectroot/$project/$export_ok")) ||
302 ($strict_export && !project_in_list($project))) {
7939fe44 303 undef $project;
cac4bd94 304 die_error(undef, "No such project");
9cd3d988 305 }
a59d4afd 306}
6191f8e1 307
4a87b43e 308our $file_name = $cgi->param('f');
24d0693a
JN
309if (defined $file_name) {
310 if (!validate_pathname($file_name)) {
311 die_error(undef, "Invalid file parameter");
312 }
313}
314
5c95fab0 315our $file_parent = $cgi->param('fp');
24d0693a
JN
316if (defined $file_parent) {
317 if (!validate_pathname($file_parent)) {
318 die_error(undef, "Invalid file parent parameter");
319 }
320}
5c95fab0 321
24d0693a 322# parameters which are refnames
4a87b43e 323our $hash = $cgi->param('h');
4fac5294 324if (defined $hash) {
24d0693a 325 if (!validate_refname($hash)) {
cac4bd94 326 die_error(undef, "Invalid hash parameter");
4fac5294 327 }
a59d4afd 328}
6191f8e1 329
4a87b43e 330our $hash_parent = $cgi->param('hp');
c91da262 331if (defined $hash_parent) {
24d0693a 332 if (!validate_refname($hash_parent)) {
cac4bd94 333 die_error(undef, "Invalid hash parent parameter");
c91da262 334 }
09bd7898
KS
335}
336
4a87b43e 337our $hash_base = $cgi->param('hb');
c91da262 338if (defined $hash_base) {
24d0693a 339 if (!validate_refname($hash_base)) {
cac4bd94 340 die_error(undef, "Invalid hash base parameter");
c91da262 341 }
a59d4afd 342}
6191f8e1 343
420e92f2
JN
344our $hash_parent_base = $cgi->param('hpb');
345if (defined $hash_parent_base) {
24d0693a 346 if (!validate_refname($hash_parent_base)) {
420e92f2
JN
347 die_error(undef, "Invalid hash parent base parameter");
348 }
349}
350
24d0693a 351# other parameters
4a87b43e 352our $page = $cgi->param('pg');
ea4a6df4 353if (defined $page) {
ac8e3f2b 354 if ($page =~ m/[^0-9]/) {
cac4bd94 355 die_error(undef, "Invalid page parameter");
b87d78d6 356 }
2ad9331e 357}
823d5dc8 358
4a87b43e 359our $searchtext = $cgi->param('s');
19806691
KS
360if (defined $searchtext) {
361 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
cac4bd94 362 die_error(undef, "Invalid search parameter");
19806691 363 }
9d032c72
RF
364 if (length($searchtext) < 2) {
365 die_error(undef, "At least two characters are required for search parameter");
366 }
19806691
KS
367 $searchtext = quotemeta $searchtext;
368}
369
88ad729b
PB
370our $searchtype = $cgi->param('st');
371if (defined $searchtype) {
372 if ($searchtype =~ m/[^a-z]/) {
373 die_error(undef, "Invalid searchtype parameter");
374 }
375}
376
dd70235f 377# now read PATH_INFO and use it as alternative to parameters
645927ce
ML
378sub evaluate_path_info {
379 return if defined $project;
380 my $path_info = $ENV{"PATH_INFO"};
381 return if !$path_info;
cd90e75f 382 $path_info =~ s,^/+,,;
645927ce 383 return if !$path_info;
cd90e75f 384 # find which part of PATH_INFO is project
dd70235f 385 $project = $path_info;
cd90e75f 386 $project =~ s,/+$,,;
2172ce4b 387 while ($project && !check_head_link("$projectroot/$project")) {
dd70235f
MW
388 $project =~ s,/*[^/]*$,,;
389 }
cd90e75f 390 # validate project
24d0693a 391 $project = validate_pathname($project);
645927ce
ML
392 if (!$project ||
393 ($export_ok && !-e "$projectroot/$project/$export_ok") ||
394 ($strict_export && !project_in_list($project))) {
395 undef $project;
396 return;
dd70235f 397 }
645927ce
ML
398 # do not change any parameters if an action is given using the query string
399 return if $action;
cd90e75f
JN
400 $path_info =~ s,^$project/*,,;
401 my ($refname, $pathname) = split(/:/, $path_info, 2);
402 if (defined $pathname) {
403 # we got "project.git/branch:filename" or "project.git/branch:dir/"
404 # we could use git_get_type(branch:pathname), but it needs $git_dir
405 $pathname =~ s,^/+,,;
406 if (!$pathname || substr($pathname, -1) eq "/") {
407 $action ||= "tree";
053d62bb 408 $pathname =~ s,/$,,;
cd90e75f
JN
409 } else {
410 $action ||= "blob_plain";
411 }
24d0693a
JN
412 $hash_base ||= validate_refname($refname);
413 $file_name ||= validate_pathname($pathname);
cd90e75f 414 } elsif (defined $refname) {
dd70235f
MW
415 # we got "project.git/branch"
416 $action ||= "shortlog";
24d0693a 417 $hash ||= validate_refname($refname);
dd70235f
MW
418 }
419}
645927ce 420evaluate_path_info();
dd70235f 421
645927ce
ML
422# path to the current git repository
423our $git_dir;
424$git_dir = "$projectroot/$project" if $project;
dd70235f 425
717b8311 426# dispatch
8e85cdc4
ML
427my %actions = (
428 "blame" => \&git_blame2,
429 "blobdiff" => \&git_blobdiff,
430 "blobdiff_plain" => \&git_blobdiff_plain,
431 "blob" => \&git_blob,
432 "blob_plain" => \&git_blob_plain,
433 "commitdiff" => \&git_commitdiff,
434 "commitdiff_plain" => \&git_commitdiff_plain,
435 "commit" => \&git_commit,
e30496df 436 "forks" => \&git_forks,
8e85cdc4
ML
437 "heads" => \&git_heads,
438 "history" => \&git_history,
439 "log" => \&git_log,
440 "rss" => \&git_rss,
af6feeb2 441 "atom" => \&git_atom,
8e85cdc4 442 "search" => \&git_search,
88ad729b 443 "search_help" => \&git_search_help,
8e85cdc4
ML
444 "shortlog" => \&git_shortlog,
445 "summary" => \&git_summary,
446 "tag" => \&git_tag,
447 "tags" => \&git_tags,
448 "tree" => \&git_tree,
cb9c6e5b 449 "snapshot" => \&git_snapshot,
ca94601c 450 "object" => \&git_object,
77a153fd
JN
451 # those below don't need $project
452 "opml" => \&git_opml,
453 "project_list" => \&git_project_list,
fc2b2be0 454 "project_index" => \&git_project_index,
8e85cdc4
ML
455);
456
77a153fd
JN
457if (defined $project) {
458 $action ||= 'summary';
459} else {
460 $action ||= 'project_list';
461}
8e85cdc4 462if (!defined($actions{$action})) {
cac4bd94 463 die_error(undef, "Unknown action");
09bd7898 464}
d04d3d42
JN
465if ($action !~ m/^(opml|project_list|project_index)$/ &&
466 !$project) {
467 die_error(undef, "Project needed");
468}
8e85cdc4
ML
469$actions{$action}->();
470exit;
09bd7898 471
06a9d86b
MW
472## ======================================================================
473## action links
474
475sub href(%) {
498fe002 476 my %params = @_;
bd5d1e42
JN
477 # default is to use -absolute url() i.e. $my_uri
478 my $href = $params{-full} ? $my_url : $my_uri;
498fe002 479
34c06118
PB
480 # XXX: Warning: If you touch this, check the search form for updating,
481 # too.
498fe002
JN
482
483 my @mapping = (
06a9d86b 484 project => "p",
420e92f2 485 action => "a",
06a9d86b 486 file_name => "f",
5c95fab0 487 file_parent => "fp",
06a9d86b
MW
488 hash => "h",
489 hash_parent => "hp",
490 hash_base => "hb",
420e92f2 491 hash_parent_base => "hpb",
06a9d86b 492 page => "pg",
a1565c44 493 order => "o",
06a9d86b 494 searchtext => "s",
88ad729b 495 searchtype => "st",
06a9d86b 496 );
498fe002 497 my %mapping = @mapping;
06a9d86b 498
a1565c44 499 $params{'project'} = $project unless exists $params{'project'};
06a9d86b 500
9e756904
MW
501 my ($use_pathinfo) = gitweb_check_feature('pathinfo');
502 if ($use_pathinfo) {
503 # use PATH_INFO for project name
504 $href .= "/$params{'project'}" if defined $params{'project'};
505 delete $params{'project'};
506
507 # Summary just uses the project path URL
508 if (defined $params{'action'} && $params{'action'} eq 'summary') {
509 delete $params{'action'};
510 }
511 }
512
513 # now encode the parameters explicitly
498fe002
JN
514 my @result = ();
515 for (my $i = 0; $i < @mapping; $i += 2) {
516 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
517 if (defined $params{$name}) {
518 push @result, $symbol . "=" . esc_param($params{$name});
519 }
520 }
9e756904
MW
521 $href .= "?" . join(';', @result) if scalar @result;
522
523 return $href;
06a9d86b
MW
524}
525
526
717b8311
JN
527## ======================================================================
528## validation, quoting/unquoting and escaping
529
24d0693a
JN
530sub validate_pathname {
531 my $input = shift || return undef;
717b8311 532
24d0693a
JN
533 # no '.' or '..' as elements of path, i.e. no '.' nor '..'
534 # at the beginning, at the end, and between slashes.
535 # also this catches doubled slashes
536 if ($input =~ m!(^|/)(|\.|\.\.)(/|$)!) {
537 return undef;
717b8311 538 }
24d0693a
JN
539 # no null characters
540 if ($input =~ m!\0!) {
717b8311
JN
541 return undef;
542 }
24d0693a
JN
543 return $input;
544}
545
546sub validate_refname {
547 my $input = shift || return undef;
548
549 # textual hashes are O.K.
550 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
551 return $input;
552 }
553 # it must be correct pathname
554 $input = validate_pathname($input)
555 or return undef;
556 # restrictions on ref name according to git-check-ref-format
557 if ($input =~ m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {
717b8311
JN
558 return undef;
559 }
560 return $input;
561}
562
232ff553
KS
563# quote unsafe chars, but keep the slash, even when it's not
564# correct, but quoted slashes look too horrible in bookmarks
565sub esc_param {
353347b0 566 my $str = shift;
a2f3db2f 567 $str =~ s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X", ord($1))/eg;
18216710 568 $str =~ s/\+/%2B/g;
a9e60b7d 569 $str =~ s/ /\+/g;
353347b0
KS
570 return $str;
571}
572
f93bff8d
JN
573# quote unsafe chars in whole URL, so some charactrs cannot be quoted
574sub esc_url {
575 my $str = shift;
576 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
577 $str =~ s/\+/%2B/g;
578 $str =~ s/ /\+/g;
579 return $str;
580}
581
232ff553 582# replace invalid utf8 character with SUBSTITUTION sequence
6255ef08 583sub esc_html ($;%) {
40c13813 584 my $str = shift;
6255ef08
JN
585 my %opts = @_;
586
e3ad95a8 587 $str = decode_utf8($str);
c390ae97 588 $str = $cgi->escapeHTML($str);
6255ef08
JN
589 if ($opts{'-nbsp'}) {
590 $str =~ s/ /&nbsp;/g;
591 }
25ffbb27 592 $str =~ s|([[:cntrl:]])|(($1 ne "\t") ? quot_cec($1) : $1)|eg;
40c13813
KS
593 return $str;
594}
595
391862e3
JN
596# quote control characters and escape filename to HTML
597sub esc_path {
598 my $str = shift;
599 my %opts = @_;
600
e3ad95a8 601 $str = decode_utf8($str);
c390ae97 602 $str = $cgi->escapeHTML($str);
391862e3
JN
603 if ($opts{'-nbsp'}) {
604 $str =~ s/ /&nbsp;/g;
605 }
606 $str =~ s|([[:cntrl:]])|quot_cec($1)|eg;
607 return $str;
608}
609
610# Make control characters "printable", using character escape codes (CEC)
1d3bc0cc
JN
611sub quot_cec {
612 my $cntrl = shift;
613 my %es = ( # character escape codes, aka escape sequences
614 "\t" => '\t', # tab (HT)
615 "\n" => '\n', # line feed (LF)
616 "\r" => '\r', # carrige return (CR)
617 "\f" => '\f', # form feed (FF)
618 "\b" => '\b', # backspace (BS)
619 "\a" => '\a', # alarm (bell) (BEL)
620 "\e" => '\e', # escape (ESC)
621 "\013" => '\v', # vertical tab (VT)
622 "\000" => '\0', # nul character (NUL)
623 );
624 my $chr = ( (exists $es{$cntrl})
625 ? $es{$cntrl}
626 : sprintf('\%03o', ord($cntrl)) );
627 return "<span class=\"cntrl\">$chr</span>";
628}
629
391862e3
JN
630# Alternatively use unicode control pictures codepoints,
631# Unicode "printable representation" (PR)
1d3bc0cc
JN
632sub quot_upr {
633 my $cntrl = shift;
634 my $chr = sprintf('&#%04d;', 0x2400+ord($cntrl));
635 return "<span class=\"cntrl\">$chr</span>";
636}
637
232ff553
KS
638# git may return quoted and escaped filenames
639sub unquote {
640 my $str = shift;
403d0906
JN
641
642 sub unq {
643 my $seq = shift;
644 my %es = ( # character escape codes, aka escape sequences
645 't' => "\t", # tab (HT, TAB)
646 'n' => "\n", # newline (NL)
647 'r' => "\r", # return (CR)
648 'f' => "\f", # form feed (FF)
649 'b' => "\b", # backspace (BS)
650 'a' => "\a", # alarm (bell) (BEL)
651 'e' => "\e", # escape (ESC)
652 'v' => "\013", # vertical tab (VT)
653 );
654
655 if ($seq =~ m/^[0-7]{1,3}$/) {
656 # octal char sequence
657 return chr(oct($seq));
658 } elsif (exists $es{$seq}) {
659 # C escape sequence, aka character escape code
660 return $es{$seq}
661 }
662 # quoted ordinary character
663 return $seq;
664 }
665
232ff553 666 if ($str =~ m/^"(.*)"$/) {
403d0906 667 # needs unquoting
232ff553 668 $str = $1;
403d0906 669 $str =~ s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;
232ff553
KS
670 }
671 return $str;
672}
673
f16db173
JN
674# escape tabs (convert tabs to spaces)
675sub untabify {
676 my $line = shift;
677
678 while ((my $pos = index($line, "\t")) != -1) {
679 if (my $count = (8 - ($pos % 8))) {
680 my $spaces = ' ' x $count;
681 $line =~ s/\t/$spaces/;
682 }
683 }
684
685 return $line;
686}
687
32f4aacc
ML
688sub project_in_list {
689 my $project = shift;
690 my @list = git_get_projects_list();
691 return @list && scalar(grep { $_->{'path'} eq $project } @list);
692}
693
717b8311
JN
694## ----------------------------------------------------------------------
695## HTML aware string manipulation
696
697sub chop_str {
698 my $str = shift;
699 my $len = shift;
700 my $add_len = shift || 10;
701
702 # allow only $len chars, but don't cut a word if it would fit in $add_len
703 # if it doesn't fit, cut it if it's still longer than the dots we would add
704 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
705 my $body = $1;
706 my $tail = $2;
707 if (length($tail) > 4) {
708 $tail = " ...";
709 $body =~ s/&[^;]*$//; # remove chopped character entities
710 }
711 return "$body$tail";
712}
713
714## ----------------------------------------------------------------------
715## functions returning short strings
716
1f1ab5f0
JN
717# CSS class for given age value (in seconds)
718sub age_class {
719 my $age = shift;
720
721 if ($age < 60*60*2) {
722 return "age0";
723 } elsif ($age < 60*60*24*2) {
724 return "age1";
725 } else {
726 return "age2";
727 }
728}
729
717b8311
JN
730# convert age in seconds to "nn units ago" string
731sub age_string {
732 my $age = shift;
733 my $age_str;
a59d4afd 734
717b8311
JN
735 if ($age > 60*60*24*365*2) {
736 $age_str = (int $age/60/60/24/365);
737 $age_str .= " years ago";
738 } elsif ($age > 60*60*24*(365/12)*2) {
739 $age_str = int $age/60/60/24/(365/12);
740 $age_str .= " months ago";
741 } elsif ($age > 60*60*24*7*2) {
742 $age_str = int $age/60/60/24/7;
743 $age_str .= " weeks ago";
744 } elsif ($age > 60*60*24*2) {
745 $age_str = int $age/60/60/24;
746 $age_str .= " days ago";
747 } elsif ($age > 60*60*2) {
748 $age_str = int $age/60/60;
749 $age_str .= " hours ago";
750 } elsif ($age > 60*2) {
751 $age_str = int $age/60;
752 $age_str .= " min ago";
753 } elsif ($age > 2) {
754 $age_str = int $age;
755 $age_str .= " sec ago";
f6801d66 756 } else {
717b8311 757 $age_str .= " right now";
4c02e3c5 758 }
717b8311 759 return $age_str;
161332a5
KS
760}
761
717b8311
JN
762# convert file mode in octal to symbolic file mode string
763sub mode_str {
764 my $mode = oct shift;
765
766 if (S_ISDIR($mode & S_IFMT)) {
767 return 'drwxr-xr-x';
768 } elsif (S_ISLNK($mode)) {
769 return 'lrwxrwxrwx';
770 } elsif (S_ISREG($mode)) {
771 # git cares only about the executable bit
772 if ($mode & S_IXUSR) {
773 return '-rwxr-xr-x';
774 } else {
775 return '-rw-r--r--';
776 };
c994d620 777 } else {
717b8311 778 return '----------';
ff7669a5 779 }
161332a5
KS
780}
781
717b8311
JN
782# convert file mode in octal to file type string
783sub file_type {
7c5e2ebb
JN
784 my $mode = shift;
785
786 if ($mode !~ m/^[0-7]+$/) {
787 return $mode;
788 } else {
789 $mode = oct $mode;
790 }
664f4cc5 791
717b8311
JN
792 if (S_ISDIR($mode & S_IFMT)) {
793 return "directory";
794 } elsif (S_ISLNK($mode)) {
795 return "symlink";
796 } elsif (S_ISREG($mode)) {
797 return "file";
798 } else {
799 return "unknown";
800 }
a59d4afd
KS
801}
802
744d0ac3
JN
803# convert file mode in octal to file type description string
804sub file_type_long {
805 my $mode = shift;
806
807 if ($mode !~ m/^[0-7]+$/) {
808 return $mode;
809 } else {
810 $mode = oct $mode;
811 }
812
813 if (S_ISDIR($mode & S_IFMT)) {
814 return "directory";
815 } elsif (S_ISLNK($mode)) {
816 return "symlink";
817 } elsif (S_ISREG($mode)) {
818 if ($mode & S_IXUSR) {
819 return "executable";
820 } else {
821 return "file";
822 };
823 } else {
824 return "unknown";
825 }
826}
827
828
717b8311
JN
829## ----------------------------------------------------------------------
830## functions returning short HTML fragments, or transforming HTML fragments
3dff5379 831## which don't belong to other sections
b18f9bf4 832
225932ed 833# format line of commit message.
717b8311
JN
834sub format_log_line_html {
835 my $line = shift;
b18f9bf4 836
225932ed 837 $line = esc_html($line, -nbsp=>1);
bfe2191f 838 if ($line =~ m/([0-9a-fA-F]{8,40})/) {
717b8311 839 my $hash_text = $1;
bfe2191f
JN
840 my $link =
841 $cgi->a({-href => href(action=>"object", hash=>$hash_text),
842 -class => "text"}, $hash_text);
843 $line =~ s/$hash_text/$link/;
b18f9bf4 844 }
717b8311 845 return $line;
b18f9bf4
JN
846}
847
717b8311 848# format marker of refs pointing to given object
847e01fb 849sub format_ref_marker {
717b8311 850 my ($refs, $id) = @_;
d294e1ca 851 my $markers = '';
27fb8c40 852
717b8311 853 if (defined $refs->{$id}) {
d294e1ca
JN
854 foreach my $ref (@{$refs->{$id}}) {
855 my ($type, $name) = qw();
856 # e.g. tags/v2.6.11 or heads/next
857 if ($ref =~ m!^(.*?)s?/(.*)$!) {
858 $type = $1;
859 $name = $2;
860 } else {
861 $type = "ref";
862 $name = $ref;
863 }
864
5fce278e
JN
865 $markers .= " <span class=\"$type\" title=\"$ref\">" .
866 esc_html($name) . "</span>";
d294e1ca
JN
867 }
868 }
869
870 if ($markers) {
871 return ' <span class="refs">'. $markers . '</span>';
717b8311
JN
872 } else {
873 return "";
874 }
27fb8c40
JN
875}
876
17d07443
JN
877# format, perhaps shortened and with markers, title line
878sub format_subject_html {
1c2a4f5a 879 my ($long, $short, $href, $extra) = @_;
17d07443
JN
880 $extra = '' unless defined($extra);
881
882 if (length($short) < length($long)) {
7c278014 883 return $cgi->a({-href => $href, -class => "list subject",
e3ad95a8 884 -title => decode_utf8($long)},
17d07443
JN
885 esc_html($short) . $extra);
886 } else {
7c278014 887 return $cgi->a({-href => $href, -class => "list subject"},
17d07443
JN
888 esc_html($long) . $extra);
889 }
890}
891
59e3b14e 892# format patch (diff) line (rather not to be used for diff headers)
eee08903
JN
893sub format_diff_line {
894 my $line = shift;
59e3b14e 895 my ($from, $to) = @_;
eee08903
JN
896 my $char = substr($line, 0, 1);
897 my $diff_class = "";
898
899 chomp $line;
900
901 if ($char eq '+') {
902 $diff_class = " add";
903 } elsif ($char eq "-") {
904 $diff_class = " rem";
905 } elsif ($char eq "@") {
906 $diff_class = " chunk_header";
907 } elsif ($char eq "\\") {
af33ef21 908 $diff_class = " incomplete";
eee08903
JN
909 }
910 $line = untabify($line);
59e3b14e
JN
911 if ($from && $to && $line =~ m/^\@{2} /) {
912 my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
913 $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
914
915 $from_lines = 0 unless defined $from_lines;
916 $to_lines = 0 unless defined $to_lines;
917
918 if ($from->{'href'}) {
919 $from_text = $cgi->a({-href=>"$from->{'href'}#l$from_start",
920 -class=>"list"}, $from_text);
921 }
922 if ($to->{'href'}) {
923 $to_text = $cgi->a({-href=>"$to->{'href'}#l$to_start",
924 -class=>"list"}, $to_text);
925 }
926 $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
927 "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
928 return "<div class=\"diff$diff_class\">$line</div>\n";
929 }
6255ef08 930 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
eee08903
JN
931}
932
717b8311
JN
933## ----------------------------------------------------------------------
934## git utility subroutines, invoking git commands
42f7eb94 935
25691fbe
DS
936# returns path to the core git executable and the --git-dir parameter as list
937sub git_cmd {
938 return $GIT, '--git-dir='.$git_dir;
939}
940
941# returns path to the core git executable and the --git-dir parameter as string
942sub git_cmd_str {
943 return join(' ', git_cmd());
944}
945
717b8311 946# get HEAD ref of given project as hash
847e01fb 947sub git_get_head_hash {
df2c37a5 948 my $project = shift;
25691fbe 949 my $o_git_dir = $git_dir;
df2c37a5 950 my $retval = undef;
25691fbe
DS
951 $git_dir = "$projectroot/$project";
952 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
df2c37a5
JH
953 my $head = <$fd>;
954 close $fd;
2c5c008b
KS
955 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
956 $retval = $1;
df2c37a5
JH
957 }
958 }
25691fbe
DS
959 if (defined $o_git_dir) {
960 $git_dir = $o_git_dir;
2c5c008b 961 }
df2c37a5
JH
962 return $retval;
963}
964
717b8311
JN
965# get type of given object
966sub git_get_type {
967 my $hash = shift;
968
25691fbe 969 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
717b8311
JN
970 my $type = <$fd>;
971 close $fd or return;
972 chomp $type;
973 return $type;
974}
975
976sub git_get_project_config {
ddb8d900 977 my ($key, $type) = @_;
717b8311
JN
978
979 return unless ($key);
980 $key =~ s/^gitweb\.//;
981 return if ($key =~ m/\W/);
982
e0d10e1c 983 my @x = (git_cmd(), 'config');
ddb8d900
AK
984 if (defined $type) { push @x, $type; }
985 push @x, "--get";
986 push @x, "gitweb.$key";
987 my $val = qx(@x);
988 chomp $val;
717b8311
JN
989 return ($val);
990}
991
717b8311
JN
992# get hash of given path at given ref
993sub git_get_hash_by_path {
994 my $base = shift;
995 my $path = shift || return undef;
1d782b03 996 my $type = shift;
717b8311 997
4b02f483 998 $path =~ s,/+$,,;
717b8311 999
25691fbe 1000 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
cac4bd94 1001 or die_error(undef, "Open git-ls-tree failed");
717b8311
JN
1002 my $line = <$fd>;
1003 close $fd or return undef;
1004
1005 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
8b4b94cc 1006 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;
1d782b03
JN
1007 if (defined $type && $type ne $2) {
1008 # type doesn't match
1009 return undef;
1010 }
717b8311
JN
1011 return $3;
1012}
1013
1014## ......................................................................
1015## git utility functions, directly accessing git repository
1016
847e01fb 1017sub git_get_project_description {
b87d78d6 1018 my $path = shift;
09bd7898 1019
19806691 1020 open my $fd, "$projectroot/$path/description" or return undef;
b87d78d6
KS
1021 my $descr = <$fd>;
1022 close $fd;
1023 chomp $descr;
1024 return $descr;
12a88f2f
KS
1025}
1026
e79ca7cc
JN
1027sub git_get_project_url_list {
1028 my $path = shift;
1029
74d61667 1030 open my $fd, "$projectroot/$path/cloneurl" or return;
e79ca7cc
JN
1031 my @git_project_url_list = map { chomp; $_ } <$fd>;
1032 close $fd;
1033
1034 return wantarray ? @git_project_url_list : \@git_project_url_list;
1035}
1036
847e01fb 1037sub git_get_projects_list {
e30496df 1038 my ($filter) = @_;
717b8311
JN
1039 my @list;
1040
e30496df
PB
1041 $filter ||= '';
1042 $filter =~ s/\.git$//;
1043
717b8311
JN
1044 if (-d $projects_list) {
1045 # search in directory
e30496df 1046 my $dir = $projects_list . ($filter ? "/$filter" : '');
6768d6b8
AK
1047 # remove the trailing "/"
1048 $dir =~ s!/+$!!;
c0011ff8
JN
1049 my $pfxlen = length("$dir");
1050
5dd5ed09 1051 my ($check_forks) = gitweb_check_feature('forks');
e30496df 1052
c0011ff8
JN
1053 File::Find::find({
1054 follow_fast => 1, # follow symbolic links
1055 dangling_symlinks => 0, # ignore dangling symlinks, silently
1056 wanted => sub {
1057 # skip project-list toplevel, if we get it.
1058 return if (m!^[/.]$!);
1059 # only directories can be git repositories
1060 return unless (-d $_);
1061
1062 my $subdir = substr($File::Find::name, $pfxlen + 1);
1063 # we check related file in $projectroot
e30496df
PB
1064 if ($check_forks and $subdir =~ m#/.#) {
1065 $File::Find::prune = 1;
1066 } elsif (check_export_ok("$projectroot/$filter/$subdir")) {
1067 push @list, { path => ($filter ? "$filter/" : '') . $subdir };
c0011ff8
JN
1068 $File::Find::prune = 1;
1069 }
1070 },
1071 }, "$dir");
1072
717b8311
JN
1073 } elsif (-f $projects_list) {
1074 # read from file(url-encoded):
1075 # 'git%2Fgit.git Linus+Torvalds'
1076 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1077 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
dd1ad5f1 1078 open my ($fd), $projects_list or return;
717b8311
JN
1079 while (my $line = <$fd>) {
1080 chomp $line;
1081 my ($path, $owner) = split ' ', $line;
1082 $path = unescape($path);
1083 $owner = unescape($owner);
1084 if (!defined $path) {
1085 next;
1086 }
83ee94c1
JH
1087 if ($filter ne '') {
1088 # looking for forks;
1089 my $pfx = substr($path, 0, length($filter));
1090 if ($pfx ne $filter) {
1091 next;
1092 }
1093 my $sfx = substr($path, length($filter));
1094 if ($sfx !~ /^\/.*\.git$/) {
1095 next;
1096 }
1097 }
2172ce4b 1098 if (check_export_ok("$projectroot/$path")) {
717b8311
JN
1099 my $pr = {
1100 path => $path,
e3ad95a8 1101 owner => decode_utf8($owner),
717b8311
JN
1102 };
1103 push @list, $pr
1104 }
1105 }
1106 close $fd;
1107 }
1108 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
1109 return @list;
1110}
1111
1e0cf030
JN
1112sub git_get_project_owner {
1113 my $project = shift;
1114 my $owner;
1115
1116 return undef unless $project;
1117
1118 # read from file (url-encoded):
1119 # 'git%2Fgit.git Linus+Torvalds'
1120 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
1121 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
1122 if (-f $projects_list) {
1123 open (my $fd , $projects_list);
1124 while (my $line = <$fd>) {
1125 chomp $line;
1126 my ($pr, $ow) = split ' ', $line;
1127 $pr = unescape($pr);
1128 $ow = unescape($ow);
1129 if ($pr eq $project) {
e3ad95a8 1130 $owner = decode_utf8($ow);
1e0cf030
JN
1131 last;
1132 }
1133 }
1134 close $fd;
1135 }
1136 if (!defined $owner) {
1137 $owner = get_file_owner("$projectroot/$project");
1138 }
1139
1140 return $owner;
1141}
1142
c60c56cc
JN
1143sub git_get_last_activity {
1144 my ($path) = @_;
1145 my $fd;
1146
1147 $git_dir = "$projectroot/$path";
1148 open($fd, "-|", git_cmd(), 'for-each-ref',
0ff5ec70 1149 '--format=%(committer)',
c60c56cc 1150 '--sort=-committerdate',
0ff5ec70 1151 '--count=1',
c60c56cc
JN
1152 'refs/heads') or return;
1153 my $most_recent = <$fd>;
1154 close $fd or return;
1155 if ($most_recent =~ / (\d+) [-+][01]\d\d\d$/) {
1156 my $timestamp = $1;
1157 my $age = time - $timestamp;
1158 return ($age, age_string($age));
1159 }
1160}
1161
847e01fb 1162sub git_get_references {
717b8311
JN
1163 my $type = shift || "";
1164 my %refs;
28b9d9f7
JN
1165 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
1166 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
1167 open my $fd, "-|", git_cmd(), "show-ref", "--dereference",
1168 ($type ? ("--", "refs/$type") : ()) # use -- <pattern> if $type
9704d75d 1169 or return;
d294e1ca 1170
717b8311
JN
1171 while (my $line = <$fd>) {
1172 chomp $line;
28b9d9f7 1173 if ($line =~ m!^([0-9a-fA-F]{40})\srefs/($type/?[^^]+)!) {
717b8311 1174 if (defined $refs{$1}) {
d294e1ca 1175 push @{$refs{$1}}, $2;
717b8311 1176 } else {
d294e1ca 1177 $refs{$1} = [ $2 ];
717b8311
JN
1178 }
1179 }
1180 }
1181 close $fd or return;
1182 return \%refs;
1183}
1184
56a322f1
JN
1185sub git_get_rev_name_tags {
1186 my $hash = shift || return undef;
1187
25691fbe 1188 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
56a322f1
JN
1189 or return;
1190 my $name_rev = <$fd>;
1191 close $fd;
1192
1193 if ($name_rev =~ m|^$hash tags/(.*)$|) {
1194 return $1;
1195 } else {
1196 # catches also '$hash undefined' output
1197 return undef;
1198 }
1199}
1200
717b8311
JN
1201## ----------------------------------------------------------------------
1202## parse to hash functions
1203
847e01fb 1204sub parse_date {
717b8311
JN
1205 my $epoch = shift;
1206 my $tz = shift || "-0000";
1207
1208 my %date;
1209 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
1210 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
1211 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
1212 $date{'hour'} = $hour;
1213 $date{'minute'} = $min;
1214 $date{'mday'} = $mday;
1215 $date{'day'} = $days[$wday];
1216 $date{'month'} = $months[$mon];
af6feeb2
JN
1217 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
1218 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
952c65fc
JN
1219 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
1220 $mday, $months[$mon], $hour ,$min;
af6feeb2
JN
1221 $date{'iso-8601'} = sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ",
1222 1900+$year, $mon, $mday, $hour ,$min, $sec;
717b8311
JN
1223
1224 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
1225 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
1226 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
1227 $date{'hour_local'} = $hour;
1228 $date{'minute_local'} = $min;
1229 $date{'tz_local'} = $tz;
af6feeb2
JN
1230 $date{'iso-tz'} = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s",
1231 1900+$year, $mon+1, $mday,
1232 $hour, $min, $sec, $tz);
717b8311
JN
1233 return %date;
1234}
1235
847e01fb 1236sub parse_tag {
ede5e100
KS
1237 my $tag_id = shift;
1238 my %tag;
d8a20ba9 1239 my @comment;
ede5e100 1240
25691fbe 1241 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
d8a20ba9 1242 $tag{'id'} = $tag_id;
ede5e100
KS
1243 while (my $line = <$fd>) {
1244 chomp $line;
1245 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
1246 $tag{'object'} = $1;
7ab0d2b6 1247 } elsif ($line =~ m/^type (.+)$/) {
ede5e100 1248 $tag{'type'} = $1;
7ab0d2b6 1249 } elsif ($line =~ m/^tag (.+)$/) {
ede5e100 1250 $tag{'name'} = $1;
d8a20ba9
KS
1251 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
1252 $tag{'author'} = $1;
1253 $tag{'epoch'} = $2;
1254 $tag{'tz'} = $3;
1255 } elsif ($line =~ m/--BEGIN/) {
1256 push @comment, $line;
1257 last;
1258 } elsif ($line eq "") {
1259 last;
ede5e100
KS
1260 }
1261 }
d8a20ba9
KS
1262 push @comment, <$fd>;
1263 $tag{'comment'} = \@comment;
19806691 1264 close $fd or return;
ede5e100
KS
1265 if (!defined $tag{'name'}) {
1266 return
1267 };
1268 return %tag
1269}
1270
756bbf54 1271sub parse_commit_text {
ccdfdea0 1272 my ($commit_text, $withparents) = @_;
756bbf54 1273 my @commit_lines = split '\n', $commit_text;
703ac710 1274 my %co;
703ac710 1275
756bbf54
RF
1276 pop @commit_lines; # Remove '\0'
1277
25f422fb
KS
1278 my $header = shift @commit_lines;
1279 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
1280 return;
1281 }
ccdfdea0 1282 ($co{'id'}, my @parents) = split ' ', $header;
19806691 1283 while (my $line = shift @commit_lines) {
b87d78d6 1284 last if $line eq "\n";
7ab0d2b6 1285 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
703ac710 1286 $co{'tree'} = $1;
ccdfdea0 1287 } elsif ((!defined $withparents) && ($line =~ m/^parent ([0-9a-fA-F]{40})$/)) {
208b2dff 1288 push @parents, $1;
022be3d0 1289 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3f714537 1290 $co{'author'} = $1;
185f09e5
KS
1291 $co{'author_epoch'} = $2;
1292 $co{'author_tz'} = $3;
ba00b8c1
JN
1293 if ($co{'author'} =~ m/^([^<]+) <([^>]*)>/) {
1294 $co{'author_name'} = $1;
1295 $co{'author_email'} = $2;
2bf7a52c
KS
1296 } else {
1297 $co{'author_name'} = $co{'author'};
1298 }
86eed32d
KS
1299 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1300 $co{'committer'} = $1;
185f09e5
KS
1301 $co{'committer_epoch'} = $2;
1302 $co{'committer_tz'} = $3;
991910a9 1303 $co{'committer_name'} = $co{'committer'};
ba00b8c1
JN
1304 if ($co{'committer'} =~ m/^([^<]+) <([^>]*)>/) {
1305 $co{'committer_name'} = $1;
1306 $co{'committer_email'} = $2;
1307 } else {
1308 $co{'committer_name'} = $co{'committer'};
1309 }
703ac710
KS
1310 }
1311 }
ede5e100 1312 if (!defined $co{'tree'}) {
25f422fb 1313 return;
ede5e100 1314 };
208b2dff
RF
1315 $co{'parents'} = \@parents;
1316 $co{'parent'} = $parents[0];
25f422fb 1317
19806691 1318 foreach my $title (@commit_lines) {
c2488d06 1319 $title =~ s/^ //;
19806691 1320 if ($title ne "") {
48c771f4 1321 $co{'title'} = chop_str($title, 80, 5);
19806691
KS
1322 # remove leading stuff of merges to make the interesting part visible
1323 if (length($title) > 50) {
1324 $title =~ s/^Automatic //;
1325 $title =~ s/^merge (of|with) /Merge ... /i;
1326 if (length($title) > 50) {
1327 $title =~ s/(http|rsync):\/\///;
1328 }
1329 if (length($title) > 50) {
1330 $title =~ s/(master|www|rsync)\.//;
1331 }
1332 if (length($title) > 50) {
1333 $title =~ s/kernel.org:?//;
1334 }
1335 if (length($title) > 50) {
1336 $title =~ s/\/pub\/scm//;
1337 }
1338 }
48c771f4 1339 $co{'title_short'} = chop_str($title, 50, 5);
19806691
KS
1340 last;
1341 }
1342 }
7e0fe5c9
PB
1343 if ($co{'title'} eq "") {
1344 $co{'title'} = $co{'title_short'} = '(no commit message)';
1345 }
25f422fb
KS
1346 # remove added spaces
1347 foreach my $line (@commit_lines) {
1348 $line =~ s/^ //;
1349 }
1350 $co{'comment'} = \@commit_lines;
2ae100df
KS
1351
1352 my $age = time - $co{'committer_epoch'};
1353 $co{'age'} = $age;
d263a6bd 1354 $co{'age_string'} = age_string($age);
71be1e79
KS
1355 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1356 if ($age > 60*60*24*7*2) {
1b1cd421 1357 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
71be1e79
KS
1358 $co{'age_string_age'} = $co{'age_string'};
1359 } else {
1360 $co{'age_string_date'} = $co{'age_string'};
1b1cd421 1361 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
71be1e79 1362 }
703ac710
KS
1363 return %co;
1364}
1365
756bbf54
RF
1366sub parse_commit {
1367 my ($commit_id) = @_;
1368 my %co;
1369
1370 local $/ = "\0";
1371
1372 open my $fd, "-|", git_cmd(), "rev-list",
ccdfdea0 1373 "--parents",
756bbf54 1374 "--header",
756bbf54
RF
1375 "--max-count=1",
1376 $commit_id,
1377 "--",
1378 or die_error(undef, "Open git-rev-list failed");
ccdfdea0 1379 %co = parse_commit_text(<$fd>, 1);
756bbf54
RF
1380 close $fd;
1381
1382 return %co;
1383}
1384
1385sub parse_commits {
1386 my ($commit_id, $maxcount, $skip, $arg, $filename) = @_;
1387 my @cos;
1388
1389 $maxcount ||= 1;
1390 $skip ||= 0;
1391
756bbf54
RF
1392 local $/ = "\0";
1393
1394 open my $fd, "-|", git_cmd(), "rev-list",
1395 "--header",
756bbf54
RF
1396 ($arg ? ($arg) : ()),
1397 ("--max-count=" . $maxcount),
f47efbb7 1398 ("--skip=" . $skip),
756bbf54
RF
1399 $commit_id,
1400 "--",
1401 ($filename ? ($filename) : ())
1402 or die_error(undef, "Open git-rev-list failed");
1403 while (my $line = <$fd>) {
1404 my %co = parse_commit_text($line);
1405 push @cos, \%co;
1406 }
1407 close $fd;
1408
1409 return wantarray ? @cos : \@cos;
1410}
1411
a446d6bb
JN
1412# parse ref from ref_file, given by ref_id, with given type
1413sub parse_ref {
1414 my $ref_file = shift;
1415 my $ref_id = shift;
1416 my $type = shift || git_get_type($ref_id);
1417 my %ref_item;
1418
1419 $ref_item{'type'} = $type;
1420 $ref_item{'id'} = $ref_id;
1421 $ref_item{'epoch'} = 0;
1422 $ref_item{'age'} = "unknown";
1423 if ($type eq "tag") {
1424 my %tag = parse_tag($ref_id);
1425 $ref_item{'comment'} = $tag{'comment'};
1426 if ($tag{'type'} eq "commit") {
1427 my %co = parse_commit($tag{'object'});
1428 $ref_item{'epoch'} = $co{'committer_epoch'};
1429 $ref_item{'age'} = $co{'age_string'};
1430 } elsif (defined($tag{'epoch'})) {
1431 my $age = time - $tag{'epoch'};
1432 $ref_item{'epoch'} = $tag{'epoch'};
1433 $ref_item{'age'} = age_string($age);
1434 }
1435 $ref_item{'reftype'} = $tag{'type'};
1436 $ref_item{'name'} = $tag{'name'};
1437 $ref_item{'refid'} = $tag{'object'};
1438 } elsif ($type eq "commit"){
1439 my %co = parse_commit($ref_id);
1440 $ref_item{'reftype'} = "commit";
1441 $ref_item{'name'} = $ref_file;
1442 $ref_item{'title'} = $co{'title'};
1443 $ref_item{'refid'} = $ref_id;
1444 $ref_item{'epoch'} = $co{'committer_epoch'};
1445 $ref_item{'age'} = $co{'age_string'};
1446 } else {
1447 $ref_item{'reftype'} = $type;
1448 $ref_item{'name'} = $ref_file;
1449 $ref_item{'refid'} = $ref_id;
1450 }
1451
1452 return %ref_item;
1453}
1454
e8e41a93 1455# parse line of git-diff-tree "raw" output
740e67f9
JN
1456sub parse_difftree_raw_line {
1457 my $line = shift;
1458 my %res;
1459
1460 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1461 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1462 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1463 $res{'from_mode'} = $1;
1464 $res{'to_mode'} = $2;
1465 $res{'from_id'} = $3;
1466 $res{'to_id'} = $4;
1467 $res{'status'} = $5;
1468 $res{'similarity'} = $6;
1469 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
e8e41a93 1470 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
740e67f9
JN
1471 } else {
1472 $res{'file'} = unquote($7);
1473 }
1474 }
1475 # 'c512b523472485aef4fff9e57b229d9d243c967f'
0edcb37d
JN
1476 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1477 $res{'commit'} = $1;
1478 }
740e67f9
JN
1479
1480 return wantarray ? %res : \%res;
1481}
1482
cb849b46
JN
1483# parse line of git-ls-tree output
1484sub parse_ls_tree_line ($;%) {
1485 my $line = shift;
1486 my %opts = @_;
1487 my %res;
1488
1489 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
8b4b94cc 1490 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
cb849b46
JN
1491
1492 $res{'mode'} = $1;
1493 $res{'type'} = $2;
1494 $res{'hash'} = $3;
1495 if ($opts{'-z'}) {
1496 $res{'name'} = $4;
1497 } else {
1498 $res{'name'} = unquote($4);
1499 }
1500
1501 return wantarray ? %res : \%res;
1502}
1503
717b8311
JN
1504## ......................................................................
1505## parse to array of hashes functions
4c02e3c5 1506
cd146408
JN
1507sub git_get_heads_list {
1508 my $limit = shift;
1509 my @headslist;
1510
1511 open my $fd, '-|', git_cmd(), 'for-each-ref',
1512 ($limit ? '--count='.($limit+1) : ()), '--sort=-committerdate',
1513 '--format=%(objectname) %(refname) %(subject)%00%(committer)',
1514 'refs/heads'
c83a77e4
JN
1515 or return;
1516 while (my $line = <$fd>) {
cd146408 1517 my %ref_item;
120ddde2 1518
cd146408
JN
1519 chomp $line;
1520 my ($refinfo, $committerinfo) = split(/\0/, $line);
1521 my ($hash, $name, $title) = split(' ', $refinfo, 3);
1522 my ($committer, $epoch, $tz) =
1523 ($committerinfo =~ /^(.*) ([0-9]+) (.*)$/);
1524 $name =~ s!^refs/heads/!!;
1525
1526 $ref_item{'name'} = $name;
1527 $ref_item{'id'} = $hash;
1528 $ref_item{'title'} = $title || '(no commit message)';
1529 $ref_item{'epoch'} = $epoch;
1530 if ($epoch) {
1531 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1532 } else {
1533 $ref_item{'age'} = "unknown";
717b8311 1534 }
cd146408
JN
1535
1536 push @headslist, \%ref_item;
c83a77e4
JN
1537 }
1538 close $fd;
1539
cd146408
JN
1540 return wantarray ? @headslist : \@headslist;
1541}
1542
1543sub git_get_tags_list {
1544 my $limit = shift;
1545 my @tagslist;
1546
1547 open my $fd, '-|', git_cmd(), 'for-each-ref',
1548 ($limit ? '--count='.($limit+1) : ()), '--sort=-creatordate',
1549 '--format=%(objectname) %(objecttype) %(refname) '.
1550 '%(*objectname) %(*objecttype) %(subject)%00%(creator)',
1551 'refs/tags'
1552 or return;
1553 while (my $line = <$fd>) {
1554 my %ref_item;
7a13b999 1555
cd146408
JN
1556 chomp $line;
1557 my ($refinfo, $creatorinfo) = split(/\0/, $line);
1558 my ($id, $type, $name, $refid, $reftype, $title) = split(' ', $refinfo, 6);
1559 my ($creator, $epoch, $tz) =
1560 ($creatorinfo =~ /^(.*) ([0-9]+) (.*)$/);
1561 $name =~ s!^refs/tags/!!;
1562
1563 $ref_item{'type'} = $type;
1564 $ref_item{'id'} = $id;
1565 $ref_item{'name'} = $name;
1566 if ($type eq "tag") {
1567 $ref_item{'subject'} = $title;
1568 $ref_item{'reftype'} = $reftype;
1569 $ref_item{'refid'} = $refid;
1570 } else {
1571 $ref_item{'reftype'} = $type;
1572 $ref_item{'refid'} = $id;
1573 }
1574
1575 if ($type eq "tag" || $type eq "commit") {
1576 $ref_item{'epoch'} = $epoch;
1577 if ($epoch) {
1578 $ref_item{'age'} = age_string(time - $ref_item{'epoch'});
1579 } else {
1580 $ref_item{'age'} = "unknown";
1581 }
1582 }
991910a9 1583
cd146408 1584 push @tagslist, \%ref_item;
717b8311 1585 }
cd146408
JN
1586 close $fd;
1587
1588 return wantarray ? @tagslist : \@tagslist;
86eed32d
KS
1589}
1590
717b8311
JN
1591## ----------------------------------------------------------------------
1592## filesystem-related functions
022be3d0 1593
c07ad4b9
KS
1594sub get_file_owner {
1595 my $path = shift;
1596
1597 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1598 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1599 if (!defined $gcos) {
1600 return undef;
1601 }
1602 my $owner = $gcos;
1603 $owner =~ s/[,;].*$//;
e3ad95a8 1604 return decode_utf8($owner);
c07ad4b9
KS
1605}
1606
717b8311
JN
1607## ......................................................................
1608## mimetype related functions
09bd7898 1609
717b8311
JN
1610sub mimetype_guess_file {
1611 my $filename = shift;
1612 my $mimemap = shift;
1613 -r $mimemap or return undef;
1614
1615 my %mimemap;
1616 open(MIME, $mimemap) or return undef;
1617 while (<MIME>) {
618918e5 1618 next if m/^#/; # skip comments
717b8311 1619 my ($mime, $exts) = split(/\t+/);
46b059d7
JH
1620 if (defined $exts) {
1621 my @exts = split(/\s+/, $exts);
1622 foreach my $ext (@exts) {
1623 $mimemap{$ext} = $mime;
1624 }
09bd7898 1625 }
09bd7898 1626 }
717b8311 1627 close(MIME);
09bd7898 1628
8059319a 1629 $filename =~ /\.([^.]*)$/;
717b8311
JN
1630 return $mimemap{$1};
1631}
5996ca08 1632
717b8311
JN
1633sub mimetype_guess {
1634 my $filename = shift;
1635 my $mime;
1636 $filename =~ /\./ or return undef;
5996ca08 1637
717b8311
JN
1638 if ($mimetypes_file) {
1639 my $file = $mimetypes_file;
d5aa50de
JN
1640 if ($file !~ m!^/!) { # if it is relative path
1641 # it is relative to project
1642 $file = "$projectroot/$project/$file";
1643 }
717b8311
JN
1644 $mime = mimetype_guess_file($filename, $file);
1645 }
1646 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1647 return $mime;
5996ca08
FF
1648}
1649
847e01fb 1650sub blob_mimetype {
717b8311
JN
1651 my $fd = shift;
1652 my $filename = shift;
5996ca08 1653
717b8311
JN
1654 if ($filename) {
1655 my $mime = mimetype_guess($filename);
1656 $mime and return $mime;
d8d17b5d 1657 }
717b8311
JN
1658
1659 # just in case
1660 return $default_blob_plain_mimetype unless $fd;
1661
1662 if (-T $fd) {
1663 return 'text/plain' .
1664 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1665 } elsif (! $filename) {
1666 return 'application/octet-stream';
1667 } elsif ($filename =~ m/\.png$/i) {
1668 return 'image/png';
1669 } elsif ($filename =~ m/\.gif$/i) {
1670 return 'image/gif';
1671 } elsif ($filename =~ m/\.jpe?g$/i) {
1672 return 'image/jpeg';
d8d17b5d 1673 } else {
717b8311 1674 return 'application/octet-stream';
f7ab660c 1675 }
717b8311
JN
1676}
1677
1678## ======================================================================
1679## functions printing HTML: header, footer, error page
1680
1681sub git_header_html {
1682 my $status = shift || "200 OK";
1683 my $expires = shift;
1684
8be2890c 1685 my $title = "$site_name";
717b8311 1686 if (defined $project) {
e3ad95a8 1687 $title .= " - " . decode_utf8($project);
717b8311
JN
1688 if (defined $action) {
1689 $title .= "/$action";
1690 if (defined $file_name) {
403d0906 1691 $title .= " - " . esc_path($file_name);
717b8311
JN
1692 if ($action eq "tree" && $file_name !~ m|/$|) {
1693 $title .= "/";
1694 }
1695 }
1696 }
f7ab660c 1697 }
717b8311
JN
1698 my $content_type;
1699 # require explicit support from the UA if we are to send the page as
1700 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1701 # we have to do this because MSIE sometimes globs '*/*', pretending to
1702 # support xhtml+xml but choking when it gets what it asked for.
952c65fc
JN
1703 if (defined $cgi->http('HTTP_ACCEPT') &&
1704 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1705 $cgi->Accept('application/xhtml+xml') != 0) {
717b8311 1706 $content_type = 'application/xhtml+xml';
f7ab660c 1707 } else {
717b8311 1708 $content_type = 'text/html';
f7ab660c 1709 }
952c65fc
JN
1710 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1711 -status=> $status, -expires => $expires);
45c9a758 1712 my $mod_perl_version = $ENV{'MOD_PERL'} ? " $ENV{'MOD_PERL'}" : '';
717b8311
JN
1713 print <<EOF;
1714<?xml version="1.0" encoding="utf-8"?>
1715<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1716<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
d4baf9ea 1717<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
717b8311
JN
1718<!-- git core binaries version $git_version -->
1719<head>
1720<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
45c9a758 1721<meta name="generator" content="gitweb/$version git/$git_version$mod_perl_version"/>
717b8311
JN
1722<meta name="robots" content="index, nofollow"/>
1723<title>$title</title>
717b8311 1724EOF
b2d3476e
AC
1725# print out each stylesheet that exist
1726 if (defined $stylesheet) {
1727#provides backwards capability for those people who define style sheet in a config file
1728 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1729 } else {
1730 foreach my $stylesheet (@stylesheets) {
1731 next unless $stylesheet;
1732 print '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";
1733 }
1734 }
dd04c428 1735 if (defined $project) {
af6feeb2
JN
1736 printf('<link rel="alternate" title="%s log RSS feed" '.
1737 'href="%s" type="application/rss+xml" />'."\n",
1c2a4f5a 1738 esc_param($project), href(action=>"rss"));
af6feeb2
JN
1739 printf('<link rel="alternate" title="%s log Atom feed" '.
1740 'href="%s" type="application/atom+xml" />'."\n",
1741 esc_param($project), href(action=>"atom"));
9d0734ae
JN
1742 } else {
1743 printf('<link rel="alternate" title="%s projects list" '.
1744 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1745 $site_name, href(project=>undef, action=>"project_index"));
af6feeb2 1746 printf('<link rel="alternate" title="%s projects feeds" '.
9d0734ae
JN
1747 'href="%s" type="text/x-opml"/>'."\n",
1748 $site_name, href(project=>undef, action=>"opml"));
dd04c428 1749 }
0b5deba1
JN
1750 if (defined $favicon) {
1751 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1752 }
10161355 1753
dd04c428 1754 print "</head>\n" .
b2d3476e
AC
1755 "<body>\n";
1756
1757 if (-f $site_header) {
1758 open (my $fd, $site_header);
1759 print <$fd>;
1760 close $fd;
1761 }
1762
1763 print "<div class=\"page_header\">\n" .
9a7a62ff
JN
1764 $cgi->a({-href => esc_url($logo_url),
1765 -title => $logo_label},
1766 qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));
f93bff8d 1767 print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
717b8311 1768 if (defined $project) {
1c2a4f5a 1769 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
717b8311
JN
1770 if (defined $action) {
1771 print " / $action";
1772 }
1773 print "\n";
6be93511
RF
1774 }
1775 my ($have_search) = gitweb_check_feature('search');
1776 if ((defined $project) && ($have_search)) {
717b8311
JN
1777 if (!defined $searchtext) {
1778 $searchtext = "";
1779 }
1780 my $search_hash;
1781 if (defined $hash_base) {
1782 $search_hash = $hash_base;
1783 } elsif (defined $hash) {
1784 $search_hash = $hash;
bddec01d 1785 } else {
717b8311 1786 $search_hash = "HEAD";
bddec01d 1787 }
717b8311
JN
1788 $cgi->param("a", "search");
1789 $cgi->param("h", $search_hash);
34c06118 1790 $cgi->param("p", $project);
717b8311
JN
1791 print $cgi->startform(-method => "get", -action => $my_uri) .
1792 "<div class=\"search\">\n" .
1793 $cgi->hidden(-name => "p") . "\n" .
1794 $cgi->hidden(-name => "a") . "\n" .
1795 $cgi->hidden(-name => "h") . "\n" .
88ad729b
PB
1796 $cgi->popup_menu(-name => 'st', -default => 'commit',
1797 -values => ['commit', 'author', 'committer', 'pickaxe']) .
1798 $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
1799 " search:\n",
717b8311
JN
1800 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1801 "</div>" .
1802 $cgi->end_form() . "\n";
b87d78d6 1803 }
717b8311
JN
1804 print "</div>\n";
1805}
1806
1807sub git_footer_html {
1808 print "<div class=\"page_footer\">\n";
1809 if (defined $project) {
847e01fb 1810 my $descr = git_get_project_description($project);
717b8311
JN
1811 if (defined $descr) {
1812 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1813 }
a1565c44 1814 print $cgi->a({-href => href(action=>"rss"),
af6feeb2
JN
1815 -class => "rss_logo"}, "RSS") . " ";
1816 print $cgi->a({-href => href(action=>"atom"),
1817 -class => "rss_logo"}, "Atom") . "\n";
717b8311 1818 } else {
a1565c44 1819 print $cgi->a({-href => href(project=>undef, action=>"opml"),
9d0734ae
JN
1820 -class => "rss_logo"}, "OPML") . " ";
1821 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1822 -class => "rss_logo"}, "TXT") . "\n";
717b8311 1823 }
b2d3476e
AC
1824 print "</div>\n" ;
1825
1826 if (-f $site_footer) {
1827 open (my $fd, $site_footer);
1828 print <$fd>;
1829 close $fd;
1830 }
1831
1832 print "</body>\n" .
717b8311
JN
1833 "</html>";
1834}
1835
1836sub die_error {
1837 my $status = shift || "403 Forbidden";
1838 my $error = shift || "Malformed query, file missing or permission denied";
1839
1840 git_header_html($status);
59b9f61a
JN
1841 print <<EOF;
1842<div class="page_body">
1843<br /><br />
1844$status - $error
1845<br />
1846</div>
1847EOF
b87d78d6 1848 git_footer_html();
717b8311 1849 exit;
161332a5
KS
1850}
1851
717b8311
JN
1852## ----------------------------------------------------------------------
1853## functions printing or outputting HTML: navigation
1854
847e01fb 1855sub git_print_page_nav {
717b8311
JN
1856 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1857 $extra = '' if !defined $extra; # pager or formats
1858
1859 my @navs = qw(summary shortlog log commit commitdiff tree);
1860 if ($suppress) {
1861 @navs = grep { $_ ne $suppress } @navs;
1862 }
1863
1c2a4f5a 1864 my %arg = map { $_ => {action=>$_} } @navs;
717b8311
JN
1865 if (defined $head) {
1866 for (qw(commit commitdiff)) {
1c2a4f5a 1867 $arg{$_}{hash} = $head;
717b8311
JN
1868 }
1869 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1870 for (qw(shortlog log)) {
1c2a4f5a 1871 $arg{$_}{hash} = $head;
045e531a 1872 }
6a928415
KS
1873 }
1874 }
1c2a4f5a
MW
1875 $arg{tree}{hash} = $treehead if defined $treehead;
1876 $arg{tree}{hash_base} = $treebase if defined $treebase;
717b8311
JN
1877
1878 print "<div class=\"page_nav\">\n" .
1879 (join " | ",
1c2a4f5a
MW
1880 map { $_ eq $current ?
1881 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1882 } @navs);
717b8311
JN
1883 print "<br/>\n$extra<br/>\n" .
1884 "</div>\n";
6a928415
KS
1885}
1886
847e01fb 1887sub format_paging_nav {
717b8311
JN
1888 my ($action, $hash, $head, $page, $nrevs) = @_;
1889 my $paging_nav;
594e212b 1890
717b8311
JN
1891
1892 if ($hash ne $head || $page) {
1c2a4f5a 1893 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
594e212b 1894 } else {
717b8311
JN
1895 $paging_nav .= "HEAD";
1896 }
1897
1898 if ($page > 0) {
1899 $paging_nav .= " &sdot; " .
1c2a4f5a 1900 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
26298b5f 1901 -accesskey => "p", -title => "Alt-p"}, "prev");
717b8311
JN
1902 } else {
1903 $paging_nav .= " &sdot; prev";
1904 }
1905
1906 if ($nrevs >= (100 * ($page+1)-1)) {
1907 $paging_nav .= " &sdot; " .
1c2a4f5a 1908 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
26298b5f 1909 -accesskey => "n", -title => "Alt-n"}, "next");
717b8311
JN
1910 } else {
1911 $paging_nav .= " &sdot; next";
594e212b 1912 }
717b8311
JN
1913
1914 return $paging_nav;
594e212b
JN
1915}
1916
717b8311
JN
1917## ......................................................................
1918## functions printing or outputting HTML: div
1919
847e01fb 1920sub git_print_header_div {
717b8311 1921 my ($action, $title, $hash, $hash_base) = @_;
1c2a4f5a 1922 my %args = ();
717b8311 1923
1c2a4f5a
MW
1924 $args{action} = $action;
1925 $args{hash} = $hash if $hash;
1926 $args{hash_base} = $hash_base if $hash_base;
717b8311
JN
1927
1928 print "<div class=\"header\">\n" .
1c2a4f5a
MW
1929 $cgi->a({-href => href(%args), -class => "title"},
1930 $title ? $title : $action) .
1931 "\n</div>\n";
717b8311 1932}
ede5e100 1933
6fd92a28
JN
1934#sub git_print_authorship (\%) {
1935sub git_print_authorship {
1936 my $co = shift;
1937
a44465cc 1938 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
6fd92a28
JN
1939 print "<div class=\"author_date\">" .
1940 esc_html($co->{'author_name'}) .
a44465cc
JN
1941 " [$ad{'rfc2822'}";
1942 if ($ad{'hour_local'} < 6) {
1943 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1944 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1945 } else {
1946 printf(" (%02d:%02d %s)",
1947 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1948 }
1949 print "]</div>\n";
6fd92a28
JN
1950}
1951
717b8311
JN
1952sub git_print_page_path {
1953 my $name = shift;
1954 my $type = shift;
59fb1c94 1955 my $hb = shift;
ede5e100 1956
4df118ed
JN
1957
1958 print "<div class=\"page_path\">";
1959 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
e3ad95a8 1960 -title => 'tree root'}, decode_utf8("[$project]"));
4df118ed
JN
1961 print " / ";
1962 if (defined $name) {
762c7205
JN
1963 my @dirname = split '/', $name;
1964 my $basename = pop @dirname;
1965 my $fullname = '';
1966
762c7205 1967 foreach my $dir (@dirname) {
16fdb488 1968 $fullname .= ($fullname ? '/' : '') . $dir;
762c7205
JN
1969 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1970 hash_base=>$hb),
edc04e90 1971 -title => $fullname}, esc_path($dir));
26d0a976 1972 print " / ";
762c7205
JN
1973 }
1974 if (defined $type && $type eq 'blob') {
952c65fc 1975 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
762c7205 1976 hash_base=>$hb),
edc04e90 1977 -title => $name}, esc_path($basename));
762c7205
JN
1978 } elsif (defined $type && $type eq 'tree') {
1979 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1980 hash_base=>$hb),
edc04e90 1981 -title => $name}, esc_path($basename));
4df118ed 1982 print " / ";
59fb1c94 1983 } else {
403d0906 1984 print esc_path($basename);
59fb1c94 1985 }
ede5e100 1986 }
4df118ed 1987 print "<br/></div>\n";
ede5e100
KS
1988}
1989
b7f9253d
JN
1990# sub git_print_log (\@;%) {
1991sub git_print_log ($;%) {
d16d093c 1992 my $log = shift;
b7f9253d 1993 my %opts = @_;
d16d093c 1994
b7f9253d
JN
1995 if ($opts{'-remove_title'}) {
1996 # remove title, i.e. first line of log
1997 shift @$log;
1998 }
d16d093c
JN
1999 # remove leading empty lines
2000 while (defined $log->[0] && $log->[0] eq "") {
2001 shift @$log;
2002 }
2003
2004 # print log
2005 my $signoff = 0;
2006 my $empty = 0;
2007 foreach my $line (@$log) {
b7f9253d
JN
2008 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2009 $signoff = 1;
fba20b42 2010 $empty = 0;
b7f9253d
JN
2011 if (! $opts{'-remove_signoff'}) {
2012 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2013 next;
2014 } else {
2015 # remove signoff lines
2016 next;
2017 }
2018 } else {
2019 $signoff = 0;
2020 }
2021
d16d093c
JN
2022 # print only one empty line
2023 # do not print empty line after signoff
2024 if ($line eq "") {
2025 next if ($empty || $signoff);
2026 $empty = 1;
2027 } else {
2028 $empty = 0;
2029 }
b7f9253d
JN
2030
2031 print format_log_line_html($line) . "<br/>\n";
2032 }
2033
2034 if ($opts{'-final_empty_line'}) {
2035 # end with single empty line
2036 print "<br/>\n" unless $empty;
d16d093c
JN
2037 }
2038}
2039
e33fba4c
JN
2040# return link target (what link points to)
2041sub git_get_link_target {
2042 my $hash = shift;
2043 my $link_target;
2044
2045 # read link
2046 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2047 or return;
2048 {
2049 local $/;
2050 $link_target = <$fd>;
2051 }
2052 close $fd
2053 or return;
2054
2055 return $link_target;
2056}
2057
3bf9d570
JN
2058# given link target, and the directory (basedir) the link is in,
2059# return target of link relative to top directory (top tree);
2060# return undef if it is not possible (including absolute links).
2061sub normalize_link_target {
2062 my ($link_target, $basedir, $hash_base) = @_;
2063
2064 # we can normalize symlink target only if $hash_base is provided
2065 return unless $hash_base;
2066
2067 # absolute symlinks (beginning with '/') cannot be normalized
2068 return if (substr($link_target, 0, 1) eq '/');
2069
2070 # normalize link target to path from top (root) tree (dir)
2071 my $path;
2072 if ($basedir) {
2073 $path = $basedir . '/' . $link_target;
2074 } else {
2075 # we are in top (root) tree (dir)
2076 $path = $link_target;
2077 }
2078
2079 # remove //, /./, and /../
2080 my @path_parts;
2081 foreach my $part (split('/', $path)) {
2082 # discard '.' and ''
2083 next if (!$part || $part eq '.');
2084 # handle '..'
2085 if ($part eq '..') {
2086 if (@path_parts) {
2087 pop @path_parts;
2088 } else {
2089 # link leads outside repository (outside top dir)
2090 return;
2091 }
2092 } else {
2093 push @path_parts, $part;
2094 }
2095 }
2096 $path = join('/', @path_parts);
2097
2098 return $path;
2099}
e33fba4c 2100
fa702003
JN
2101# print tree entry (row of git_tree), but without encompassing <tr> element
2102sub git_print_tree_entry {
2103 my ($t, $basedir, $hash_base, $have_blame) = @_;
2104
2105 my %base_key = ();
e33fba4c 2106 $base_key{'hash_base'} = $hash_base if defined $hash_base;
fa702003 2107
4de741b3
LT
2108 # The format of a table row is: mode list link. Where mode is
2109 # the mode of the entry, list is the name of the entry, an href,
2110 # and link is the action links of the entry.
2111
fa702003
JN
2112 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
2113 if ($t->{'type'} eq "blob") {
2114 print "<td class=\"list\">" .
4de741b3 2115 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
e7fb022a 2116 file_name=>"$basedir$t->{'name'}", %base_key),
e33fba4c
JN
2117 -class => "list"}, esc_path($t->{'name'}));
2118 if (S_ISLNK(oct $t->{'mode'})) {
2119 my $link_target = git_get_link_target($t->{'hash'});
2120 if ($link_target) {
3bf9d570
JN
2121 my $norm_target = normalize_link_target($link_target, $basedir, $hash_base);
2122 if (defined $norm_target) {
2123 print " -> " .
2124 $cgi->a({-href => href(action=>"object", hash_base=>$hash_base,
2125 file_name=>$norm_target),
2126 -title => $norm_target}, esc_path($link_target));
2127 } else {
2128 print " -> " . esc_path($link_target);
2129 }
e33fba4c
JN
2130 }
2131 }
2132 print "</td>\n";
4de741b3 2133 print "<td class=\"link\">";
4777b014 2134 print $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
e33fba4c
JN
2135 file_name=>"$basedir$t->{'name'}", %base_key)},
2136 "blob");
fa702003 2137 if ($have_blame) {
4777b014
PB
2138 print " | " .
2139 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
e33fba4c
JN
2140 file_name=>"$basedir$t->{'name'}", %base_key)},
2141 "blame");
fa702003
JN
2142 }
2143 if (defined $hash_base) {
4777b014
PB
2144 print " | " .
2145 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
fa702003
JN
2146 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
2147 "history");
2148 }
2149 print " | " .
6f7ea5fb 2150 $cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,
e7fb022a
JN
2151 file_name=>"$basedir$t->{'name'}")},
2152 "raw");
4de741b3 2153 print "</td>\n";
fa702003
JN
2154
2155 } elsif ($t->{'type'} eq "tree") {
0fa105e7
LT
2156 print "<td class=\"list\">";
2157 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
fa702003 2158 file_name=>"$basedir$t->{'name'}", %base_key)},
403d0906 2159 esc_path($t->{'name'}));
0fa105e7
LT
2160 print "</td>\n";
2161 print "<td class=\"link\">";
4777b014 2162 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
e33fba4c
JN
2163 file_name=>"$basedir$t->{'name'}", %base_key)},
2164 "tree");
fa702003 2165 if (defined $hash_base) {
4777b014
PB
2166 print " | " .
2167 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
fa702003
JN
2168 file_name=>"$basedir$t->{'name'}")},
2169 "history");
2170 }
2171 print "</td>\n";
2172 }
2173}
2174
717b8311
JN
2175## ......................................................................
2176## functions printing large fragments of HTML
2177
4a4a1a53 2178sub git_difftree_body {
eee08903 2179 my ($difftree, $hash, $parent) = @_;
2b2a8c78 2180 my ($have_blame) = gitweb_check_feature('blame');
4a4a1a53
JN
2181 print "<div class=\"list_head\">\n";
2182 if ($#{$difftree} > 10) {
2183 print(($#{$difftree} + 1) . " files changed:\n");
2184 }
2185 print "</div>\n";
2186
2187 print "<table class=\"diff_tree\">\n";
6dd36acd 2188 my $alternate = 1;
b4657e77 2189 my $patchno = 0;
4a4a1a53 2190 foreach my $line (@{$difftree}) {
e8e41a93 2191 my %diff = parse_difftree_raw_line($line);
4a4a1a53
JN
2192
2193 if ($alternate) {
2194 print "<tr class=\"dark\">\n";
2195 } else {
2196 print "<tr class=\"light\">\n";
2197 }
2198 $alternate ^= 1;
2199
e8e41a93
JN
2200 my ($to_mode_oct, $to_mode_str, $to_file_type);
2201 my ($from_mode_oct, $from_mode_str, $from_file_type);
2202 if ($diff{'to_mode'} ne ('0' x 6)) {
2203 $to_mode_oct = oct $diff{'to_mode'};
2204 if (S_ISREG($to_mode_oct)) { # only for regular file
2205 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
2206 }
2207 $to_file_type = file_type($diff{'to_mode'});
2208 }
2209 if ($diff{'from_mode'} ne ('0' x 6)) {
2210 $from_mode_oct = oct $diff{'from_mode'};
2211 if (S_ISREG($to_mode_oct)) { # only for regular file
2212 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4a4a1a53 2213 }
e8e41a93
JN
2214 $from_file_type = file_type($diff{'from_mode'});
2215 }
2216
2217 if ($diff{'status'} eq "A") { # created
2218 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
2219 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
2220 $mode_chng .= "]</span>";
499faeda
LT
2221 print "<td>";
2222 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
e8e41a93 2223 hash_base=>$hash, file_name=>$diff{'file'}),
403d0906 2224 -class => "list"}, esc_path($diff{'file'}));
499faeda
LT
2225 print "</td>\n";
2226 print "<td>$mode_chng</td>\n";
2227 print "<td class=\"link\">";
72dbafa1 2228 if ($action eq 'commitdiff') {
b4657e77
JN
2229 # link to patch
2230 $patchno++;
499faeda 2231 print $cgi->a({-href => "#patch$patchno"}, "patch");
897d1d2e 2232 print " | ";
b4657e77 2233 }
897d1d2e
JN
2234 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2235 hash_base=>$hash, file_name=>$diff{'file'})},
3faa541f 2236 "blob");
b4657e77 2237 print "</td>\n";
4a4a1a53 2238
e8e41a93
JN
2239 } elsif ($diff{'status'} eq "D") { # deleted
2240 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
499faeda
LT
2241 print "<td>";
2242 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
e8e41a93 2243 hash_base=>$parent, file_name=>$diff{'file'}),
403d0906 2244 -class => "list"}, esc_path($diff{'file'}));
499faeda
LT
2245 print "</td>\n";
2246 print "<td>$mode_chng</td>\n";
2247 print "<td class=\"link\">";
72dbafa1 2248 if ($action eq 'commitdiff') {
b4657e77
JN
2249 # link to patch
2250 $patchno++;
499faeda
LT
2251 print $cgi->a({-href => "#patch$patchno"}, "patch");
2252 print " | ";
b4657e77 2253 }
4777b014
PB
2254 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
2255 hash_base=>$parent, file_name=>$diff{'file'})},
897d1d2e 2256 "blob") . " | ";
2b2a8c78 2257 if ($have_blame) {
897d1d2e
JN
2258 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent,
2259 file_name=>$diff{'file'})},
2260 "blame") . " | ";
2b2a8c78 2261 }
b4657e77 2262 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
e7fb022a
JN
2263 file_name=>$diff{'file'})},
2264 "history");
499faeda 2265 print "</td>\n";
4a4a1a53 2266
e8e41a93 2267 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
4a4a1a53 2268 my $mode_chnge = "";
e8e41a93
JN
2269 if ($diff{'from_mode'} != $diff{'to_mode'}) {
2270 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
6e72cf43 2271 if ($from_file_type ne $to_file_type) {
e8e41a93 2272 $mode_chnge .= " from $from_file_type to $to_file_type";
4a4a1a53 2273 }
e8e41a93
JN
2274 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
2275 if ($from_mode_str && $to_mode_str) {
2276 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
2277 } elsif ($to_mode_str) {
2278 $mode_chnge .= " mode: $to_mode_str";
4a4a1a53
JN
2279 }
2280 }
2281 $mode_chnge .= "]</span>\n";
2282 }
2283 print "<td>";
499faeda 2284 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
e7fb022a 2285 hash_base=>$hash, file_name=>$diff{'file'}),
403d0906 2286 -class => "list"}, esc_path($diff{'file'}));
499faeda
LT
2287 print "</td>\n";
2288 print "<td>$mode_chnge</td>\n";
2289 print "<td class=\"link\">";
241cc599
JN
2290 if ($action eq 'commitdiff') {
2291 # link to patch
2292 $patchno++;
2293 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2294 " | ";
2295 } elsif ($diff{'to_id'} ne $diff{'from_id'}) {
2296 # "commit" view and modified file (not onlu mode changed)
2297 print $cgi->a({-href => href(action=>"blobdiff",
2298 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2299 hash_base=>$hash, hash_parent_base=>$parent,
2300 file_name=>$diff{'file'})},
2301 "diff") .
2302 " | ";
4a4a1a53 2303 }
4777b014 2304 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
897d1d2e
JN
2305 hash_base=>$hash, file_name=>$diff{'file'})},
2306 "blob") . " | ";
2b2a8c78 2307 if ($have_blame) {
897d1d2e
JN
2308 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2309 file_name=>$diff{'file'})},
2310 "blame") . " | ";
2b2a8c78 2311 }
eb51ec9c 2312 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
e7fb022a
JN
2313 file_name=>$diff{'file'})},
2314 "history");
4a4a1a53
JN
2315 print "</td>\n";
2316
e8e41a93
JN
2317 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
2318 my %status_name = ('R' => 'moved', 'C' => 'copied');
2319 my $nstatus = $status_name{$diff{'status'}};
4a4a1a53 2320 my $mode_chng = "";
e8e41a93
JN
2321 if ($diff{'from_mode'} != $diff{'to_mode'}) {
2322 # mode also for directories, so we cannot use $to_mode_str
2323 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4a4a1a53
JN
2324 }
2325 print "<td>" .
e8e41a93
JN
2326 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2327 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
403d0906 2328 -class => "list"}, esc_path($diff{'to_file'})) . "</td>\n" .
e8e41a93
JN
2329 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
2330 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
2331 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
403d0906 2332 -class => "list"}, esc_path($diff{'from_file'})) .
e8e41a93 2333 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
499faeda 2334 "<td class=\"link\">";
241cc599
JN
2335 if ($action eq 'commitdiff') {
2336 # link to patch
2337 $patchno++;
2338 print $cgi->a({-href => "#patch$patchno"}, "patch") .
2339 " | ";
2340 } elsif ($diff{'to_id'} ne $diff{'from_id'}) {
2341 # "commit" view and modified file (not only pure rename or copy)
2342 print $cgi->a({-href => href(action=>"blobdiff",
2343 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
2344 hash_base=>$hash, hash_parent_base=>$parent,
2345 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
2346 "diff") .
2347 " | ";
4a4a1a53 2348 }
897d1d2e
JN
2349 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
2350 hash_base=>$parent, file_name=>$diff{'to_file'})},
2351 "blob") . " | ";
2b2a8c78 2352 if ($have_blame) {
897d1d2e
JN
2353 print $cgi->a({-href => href(action=>"blame", hash_base=>$hash,
2354 file_name=>$diff{'to_file'})},
2355 "blame") . " | ";
2b2a8c78 2356 }
897d1d2e
JN
2357 print $cgi->a({-href => href(action=>"history", hash_base=>$hash,
2358 file_name=>$diff{'to_file'})},
e7fb022a 2359 "history");
4a4a1a53 2360 print "</td>\n";
e8e41a93 2361
4a4a1a53
JN
2362 } # we should not encounter Unmerged (U) or Unknown (X) status
2363 print "</tr>\n";
2364 }
2365 print "</table>\n";
2366}
2367
eee08903 2368sub git_patchset_body {
157e43b4 2369 my ($fd, $difftree, $hash, $hash_parent) = @_;
eee08903
JN
2370
2371 my $patch_idx = 0;
6d55f055 2372 my $patch_line;
fe87585e 2373 my $diffinfo;
744d0ac3 2374 my (%from, %to);
eee08903
JN
2375
2376 print "<div class=\"patchset\">\n";
2377
6d55f055
JN
2378 # skip to first patch
2379 while ($patch_line = <$fd>) {
157e43b4 2380 chomp $patch_line;
eee08903 2381
6d55f055
JN
2382 last if ($patch_line =~ m/^diff /);
2383 }
2384
2385 PATCH:
2386 while ($patch_line) {
2387 my @diff_header;
62e4f26f 2388 my ($from_id, $to_id);
6d55f055
JN
2389
2390 # git diff header
2391 #assert($patch_line =~ m/^diff /) if DEBUG;
2392 #assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed
2393 push @diff_header, $patch_line;
2394
2395 # extended diff header
2396 EXTENDED_HEADER:
2397 while ($patch_line = <$fd>) {
2398 chomp $patch_line;
2399
66399eff 2400 last EXTENDED_HEADER if ($patch_line =~ m/^--- |^diff /);
6d55f055
JN
2401
2402 if ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/) {
2403 $from_id = $1;
2404 $to_id = $2;
eee08903 2405 }
6d55f055
JN
2406
2407 push @diff_header, $patch_line;
2408 }
6d55f055
JN
2409 my $last_patch_line = $patch_line;
2410
2411 # check if current patch belong to current raw line
2412 # and parse raw git-diff line if needed
2413 if (defined $diffinfo &&
2414 $diffinfo->{'from_id'} eq $from_id &&
2415 $diffinfo->{'to_id'} eq $to_id) {
2416 # this is split patch
2417 print "<div class=\"patch cont\">\n";
2418 } else {
2419 # advance raw git-diff output if needed
2420 $patch_idx++ if defined $diffinfo;
eee08903 2421
744d0ac3 2422 # read and prepare patch information
fe87585e 2423 if (ref($difftree->[$patch_idx]) eq "HASH") {
744d0ac3 2424 # pre-parsed (or generated by hand)
fe87585e
JN
2425 $diffinfo = $difftree->[$patch_idx];
2426 } else {
2427 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
2428 }
744d0ac3
JN
2429 $from{'file'} = $diffinfo->{'from_file'} || $diffinfo->{'file'};
2430 $to{'file'} = $diffinfo->{'to_file'} || $diffinfo->{'file'};
2431 if ($diffinfo->{'status'} ne "A") { # not new (added) file
2432 $from{'href'} = href(action=>"blob", hash_base=>$hash_parent,
2433 hash=>$diffinfo->{'from_id'},
2434 file_name=>$from{'file'});
62e4f26f
JN
2435 } else {
2436 delete $from{'href'};
744d0ac3
JN
2437 }
2438 if ($diffinfo->{'status'} ne "D") { # not deleted file
2439 $to{'href'} = href(action=>"blob", hash_base=>$hash,
2440 hash=>$diffinfo->{'to_id'},
2441 file_name=>$to{'file'});
62e4f26f
JN
2442 } else {
2443 delete $to{'href'};
744d0ac3 2444 }
6d55f055
JN
2445 # this is first patch for raw difftree line with $patch_idx index
2446 # we index @$difftree array from 0, but number patches from 1
2447 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
744d0ac3 2448 }
eee08903 2449
6d55f055
JN
2450 # print "git diff" header
2451 $patch_line = shift @diff_header;
2452 $patch_line =~ s!^(diff (.*?) )"?a/.*$!$1!;
2453 if ($from{'href'}) {
2454 $patch_line .= $cgi->a({-href => $from{'href'}, -class => "path"},
2455 'a/' . esc_path($from{'file'}));
2456 } else { # file was added
2457 $patch_line .= 'a/' . esc_path($from{'file'});
2458 }
2459 $patch_line .= ' ';
2460 if ($to{'href'}) {
2461 $patch_line .= $cgi->a({-href => $to{'href'}, -class => "path"},
2462 'b/' . esc_path($to{'file'}));
2463 } else { # file was deleted
2464 $patch_line .= 'b/' . esc_path($to{'file'});
2465 }
2466 print "<div class=\"diff header\">$patch_line</div>\n";
2467
2468 # print extended diff header
2469 print "<div class=\"diff extended_header\">\n" if (@diff_header > 0);
2470 EXTENDED_HEADER:
2471 foreach $patch_line (@diff_header) {
2472 # match <path>
2473 if ($patch_line =~ s!^((copy|rename) from ).*$!$1! && $from{'href'}) {
2474 $patch_line .= $cgi->a({-href=>$from{'href'}, -class=>"path"},
2e1951f6 2475 esc_path($from{'file'}));
6d55f055
JN
2476 }
2477 if ($patch_line =~ s!^((copy|rename) to ).*$!$1! && $to{'href'}) {
2e1951f6
JN
2478 $patch_line .= $cgi->a({-href=>$to{'href'}, -class=>"path"},
2479 esc_path($to{'file'}));
6d55f055
JN
2480 }
2481 # match <mode>
2482 if ($patch_line =~ m/\s(\d{6})$/) {
2483 $patch_line .= '<span class="info"> (' .
2484 file_type_long($1) .
2485 ')</span>';
2486 }
2487 # match <hash>
2488 if ($patch_line =~ m/^index/) {
2489 my ($from_link, $to_link);
2490 if ($from{'href'}) {
2491 $from_link = $cgi->a({-href=>$from{'href'}, -class=>"hash"},
2492 substr($diffinfo->{'from_id'},0,7));
2493 } else {
2494 $from_link = '0' x 7;
744d0ac3 2495 }
6d55f055
JN
2496 if ($to{'href'}) {
2497 $to_link = $cgi->a({-href=>$to{'href'}, -class=>"hash"},
2498 substr($diffinfo->{'to_id'},0,7));
2499 } else {
2500 $to_link = '0' x 7;
744d0ac3 2501 }
6d55f055
JN
2502 #affirm {
2503 # my ($from_hash, $to_hash) =
2504 # ($patch_line =~ m/^index ([0-9a-fA-F]{40})..([0-9a-fA-F]{40})/);
2505 # my ($from_id, $to_id) =
2506 # ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2507 # ($from_hash eq $from_id) && ($to_hash eq $to_id);
2508 #} if DEBUG;
2509 my ($from_id, $to_id) = ($diffinfo->{'from_id'}, $diffinfo->{'to_id'});
2510 $patch_line =~ s!$from_id\.\.$to_id!$from_link..$to_link!;
2511 }
2512 print $patch_line . "<br/>\n";
2513 }
2514 print "</div>\n" if (@diff_header > 0); # class="diff extended_header"
2515
2516 # from-file/to-file diff header
2517 $patch_line = $last_patch_line;
0bdb28c9
JN
2518 if (! $patch_line) {
2519 print "</div>\n"; # class="patch"
2520 last PATCH;
2521 }
66399eff 2522 next PATCH if ($patch_line =~ m/^diff /);
6d55f055 2523 #assert($patch_line =~ m/^---/) if DEBUG;
13e86efb 2524 if ($from{'href'} && $patch_line =~ m!^--- "?a/!) {
6d55f055
JN
2525 $patch_line = '--- a/' .
2526 $cgi->a({-href=>$from{'href'}, -class=>"path"},
2527 esc_path($from{'file'}));
2528 }
2529 print "<div class=\"diff from_file\">$patch_line</div>\n";
744d0ac3 2530
6d55f055 2531 $patch_line = <$fd>;
6d55f055 2532 chomp $patch_line;
e4e4f825 2533
6d55f055 2534 #assert($patch_line =~ m/^+++/) if DEBUG;
13e86efb 2535 if ($to{'href'} && $patch_line =~ m!^\+\+\+ "?b/!) {
6d55f055
JN
2536 $patch_line = '+++ b/' .
2537 $cgi->a({-href=>$to{'href'}, -class=>"path"},
2538 esc_path($to{'file'}));
2539 }
2540 print "<div class=\"diff to_file\">$patch_line</div>\n";
e4e4f825 2541
6d55f055
JN
2542 # the patch itself
2543 LINE:
2544 while ($patch_line = <$fd>) {
2545 chomp $patch_line;
e4e4f825 2546
6d55f055 2547 next PATCH if ($patch_line =~ m/^diff /);
e4e4f825 2548
59e3b14e 2549 print format_diff_line($patch_line, \%from, \%to);
eee08903 2550 }
eee08903 2551
6d55f055
JN
2552 } continue {
2553 print "</div>\n"; # class="patch"
eee08903 2554 }
eee08903
JN
2555
2556 print "</div>\n"; # class="patchset"
2557}
2558
2559# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2560
e30496df
PB
2561sub git_project_list_body {
2562 my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
2563
5dd5ed09 2564 my ($check_forks) = gitweb_check_feature('forks');
e30496df
PB
2565
2566 my @projects;
2567 foreach my $pr (@$projlist) {
2568 my (@aa) = git_get_last_activity($pr->{'path'});
2569 unless (@aa) {
2570 next;
2571 }
2572 ($pr->{'age'}, $pr->{'age_string'}) = @aa;
2573 if (!defined $pr->{'descr'}) {
2574 my $descr = git_get_project_description($pr->{'path'}) || "";
e3ad95a8 2575 $pr->{'descr_long'} = decode_utf8($descr);
e30496df
PB
2576 $pr->{'descr'} = chop_str($descr, 25, 5);
2577 }
2578 if (!defined $pr->{'owner'}) {
2579 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2580 }
2581 if ($check_forks) {
2582 my $pname = $pr->{'path'};
83ee94c1
JH
2583 if (($pname =~ s/\.git$//) &&
2584 ($pname !~ /\/$/) &&
2585 (-d "$projectroot/$pname")) {
2586 $pr->{'forks'} = "-d $projectroot/$pname";
2587 }
2588 else {
2589 $pr->{'forks'} = 0;
2590 }
e30496df
PB
2591 }
2592 push @projects, $pr;
2593 }
2594
2595 $order ||= "project";
2596 $from = 0 unless defined $from;
2597 $to = $#projects if (!defined $to || $#projects < $to);
2598
2599 print "<table class=\"project_list\">\n";
2600 unless ($no_header) {
2601 print "<tr>\n";
2602 if ($check_forks) {
2603 print "<th></th>\n";
2604 }
2605 if ($order eq "project") {
2606 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2607 print "<th>Project</th>\n";
2608 } else {
2609 print "<th>" .
2610 $cgi->a({-href => href(project=>undef, order=>'project'),
e88ce8a4 2611 -class => "header"}, "Project") .
e30496df
PB
2612 "</th>\n";
2613 }
2614 if ($order eq "descr") {
2615 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2616 print "<th>Description</th>\n";
2617 } else {
2618 print "<th>" .
2619 $cgi->a({-href => href(project=>undef, order=>'descr'),
e88ce8a4 2620 -class => "header"}, "Description") .
e30496df
PB
2621 "</th>\n";
2622 }
2623 if ($order eq "owner") {
2624 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2625 print "<th>Owner</th>\n";
2626 } else {
2627 print "<th>" .
2628 $cgi->a({-href => href(project=>undef, order=>'owner'),
e88ce8a4 2629 -class => "header"}, "Owner") .
e30496df
PB
2630 "</th>\n";
2631 }
2632 if ($order eq "age") {
2633 @projects = sort {$a->{'age'} <=> $b->{'age'}} @projects;
2634 print "<th>Last Change</th>\n";
2635 } else {
2636 print "<th>" .
2637 $cgi->a({-href => href(project=>undef, order=>'age'),
e88ce8a4 2638 -class => "header"}, "Last Change") .
e30496df
PB
2639 "</th>\n";
2640 }
2641 print "<th></th>\n" .
2642 "</tr>\n";
2643 }
2644 my $alternate = 1;
2645 for (my $i = $from; $i <= $to; $i++) {
2646 my $pr = $projects[$i];
2647 if ($alternate) {
2648 print "<tr class=\"dark\">\n";
2649 } else {
2650 print "<tr class=\"light\">\n";
2651 }
2652 $alternate ^= 1;
2653 if ($check_forks) {
2654 print "<td>";
2655 if ($pr->{'forks'}) {
83ee94c1 2656 print "<!-- $pr->{'forks'} -->\n";
e30496df
PB
2657 print $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "+");
2658 }
2659 print "</td>\n";
2660 }
2661 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2662 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
e88ce8a4
JN
2663 "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2664 -class => "list", -title => $pr->{'descr_long'}},
2665 esc_html($pr->{'descr'})) . "</td>\n" .
e30496df
PB
2666 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2667 print "<td class=\"". age_class($pr->{'age'}) . "\">" .
2668 $pr->{'age_string'} . "</td>\n" .
2669 "<td class=\"link\">" .
2670 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
faa1bbfd 2671 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
e30496df
PB
2672 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2673 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
2674 ($pr->{'forks'} ? " | " . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")}, "forks") : '') .
2675 "</td>\n" .
2676 "</tr>\n";
2677 }
2678 if (defined $extra) {
2679 print "<tr>\n";
2680 if ($check_forks) {
2681 print "<td></td>\n";
2682 }
2683 print "<td colspan=\"5\">$extra</td>\n" .
2684 "</tr>\n";
2685 }
2686 print "</table>\n";
2687}
2688
9f5dcb81
JN
2689sub git_shortlog_body {
2690 # uses global variable $project
190d7fdc 2691 my ($commitlist, $from, $to, $refs, $extra) = @_;
ddb8d900 2692
3fcf06be
RF
2693 my $have_snapshot = gitweb_have_snapshot();
2694
9f5dcb81 2695 $from = 0 unless defined $from;
190d7fdc 2696 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
9f5dcb81
JN
2697
2698 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
6dd36acd 2699 my $alternate = 1;
9f5dcb81 2700 for (my $i = $from; $i <= $to; $i++) {
190d7fdc
RF
2701 my %co = %{$commitlist->[$i]};
2702 my $commit = $co{'id'};
847e01fb 2703 my $ref = format_ref_marker($refs, $commit);
9f5dcb81
JN
2704 if ($alternate) {
2705 print "<tr class=\"dark\">\n";
2706 } else {
2707 print "<tr class=\"light\">\n";
2708 }
2709 $alternate ^= 1;
2710 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2711 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2712 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2713 "<td>";
952c65fc
JN
2714 print format_subject_html($co{'title'}, $co{'title_short'},
2715 href(action=>"commit", hash=>$commit), $ref);
9f5dcb81
JN
2716 print "</td>\n" .
2717 "<td class=\"link\">" .
4777b014 2718 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
35749ae5 2719 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
55ff35cb 2720 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3fcf06be 2721 if ($have_snapshot) {
55ff35cb
PB
2722 print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
2723 }
cb9c6e5b 2724 print "</td>\n" .
9f5dcb81
JN
2725 "</tr>\n";
2726 }
2727 if (defined $extra) {
2728 print "<tr>\n" .
2729 "<td colspan=\"4\">$extra</td>\n" .
2730 "</tr>\n";
2731 }
2732 print "</table>\n";
2733}
2734
581860e1
JN
2735sub git_history_body {
2736 # Warning: assumes constant type (blob or tree) during history
a8b983bf 2737 my ($commitlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
8be68352
JN
2738
2739 $from = 0 unless defined $from;
a8b983bf 2740 $to = $#{$commitlist} unless (defined $to && $to <= $#{$commitlist});
581860e1
JN
2741
2742 print "<table class=\"history\" cellspacing=\"0\">\n";
6dd36acd 2743 my $alternate = 1;
8be68352 2744 for (my $i = $from; $i <= $to; $i++) {
a8b983bf 2745 my %co = %{$commitlist->[$i]};
581860e1
JN
2746 if (!%co) {
2747 next;
2748 }
a8b983bf 2749 my $commit = $co{'id'};
581860e1
JN
2750
2751 my $ref = format_ref_marker($refs, $commit);
2752
2753 if ($alternate) {
2754 print "<tr class=\"dark\">\n";
2755 } else {
2756 print "<tr class=\"light\">\n";
2757 }
2758 $alternate ^= 1;
2759 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2760 # shortlog uses chop_str($co{'author_name'}, 10)
2761 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2762 "<td>";
2763 # originally git_history used chop_str($co{'title'}, 50)
952c65fc
JN
2764 print format_subject_html($co{'title'}, $co{'title_short'},
2765 href(action=>"commit", hash=>$commit), $ref);
581860e1
JN
2766 print "</td>\n" .
2767 "<td class=\"link\">" .
6d81c5a2
LT
2768 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype) . " | " .
2769 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
581860e1
JN
2770
2771 if ($ftype eq 'blob') {
2772 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2773 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2774 if (defined $blob_current && defined $blob_parent &&
2775 $blob_current ne $blob_parent) {
2776 print " | " .
420e92f2
JN
2777 $cgi->a({-href => href(action=>"blobdiff",
2778 hash=>$blob_current, hash_parent=>$blob_parent,
2779 hash_base=>$hash_base, hash_parent_base=>$commit,
2780 file_name=>$file_name)},
581860e1
JN
2781 "diff to current");
2782 }
2783 }
2784 print "</td>\n" .
2785 "</tr>\n";
2786 }
2787 if (defined $extra) {
2788 print "<tr>\n" .
2789 "<td colspan=\"4\">$extra</td>\n" .
2790 "</tr>\n";
2791 }
2792 print "</table>\n";
2793}
2794
717b8311
JN
2795sub git_tags_body {
2796 # uses global variable $project
2797 my ($taglist, $from, $to, $extra) = @_;
2798 $from = 0 unless defined $from;
2799 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2800
2801 print "<table class=\"tags\" cellspacing=\"0\">\n";
6dd36acd 2802 my $alternate = 1;
717b8311
JN
2803 for (my $i = $from; $i <= $to; $i++) {
2804 my $entry = $taglist->[$i];
2805 my %tag = %$entry;
cd146408 2806 my $comment = $tag{'subject'};
717b8311
JN
2807 my $comment_short;
2808 if (defined $comment) {
2809 $comment_short = chop_str($comment, 30, 5);
2810 }
2811 if ($alternate) {
2812 print "<tr class=\"dark\">\n";
2813 } else {
2814 print "<tr class=\"light\">\n";
2815 }
2816 $alternate ^= 1;
27dd1a83
JN
2817 if (defined $tag{'age'}) {
2818 print "<td><i>$tag{'age'}</i></td>\n";
2819 } else {
2820 print "<td></td>\n";
2821 }
2822 print "<td>" .
1c2a4f5a 2823 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
63e4220b 2824 -class => "list name"}, esc_html($tag{'name'})) .
717b8311
JN
2825 "</td>\n" .
2826 "<td>";
2827 if (defined $comment) {
952c65fc
JN
2828 print format_subject_html($comment, $comment_short,
2829 href(action=>"tag", hash=>$tag{'id'}));
717b8311
JN
2830 }
2831 print "</td>\n" .
2832 "<td class=\"selflink\">";
2833 if ($tag{'type'} eq "tag") {
1c2a4f5a 2834 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
717b8311
JN
2835 } else {
2836 print "&nbsp;";
2837 }
2838 print "</td>\n" .
2839 "<td class=\"link\">" . " | " .
1c2a4f5a 2840 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
717b8311 2841 if ($tag{'reftype'} eq "commit") {
1c2a4f5a 2842 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
cd146408 2843 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log");
717b8311 2844 } elsif ($tag{'reftype'} eq "blob") {
1c2a4f5a 2845 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
717b8311
JN
2846 }
2847 print "</td>\n" .
2848 "</tr>";
2849 }
2850 if (defined $extra) {
2851 print "<tr>\n" .
2852 "<td colspan=\"5\">$extra</td>\n" .
2853 "</tr>\n";
2854 }
2855 print "</table>\n";
2856}
2857
2858sub git_heads_body {
2859 # uses global variable $project
120ddde2 2860 my ($headlist, $head, $from, $to, $extra) = @_;
717b8311 2861 $from = 0 unless defined $from;
120ddde2 2862 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
717b8311
JN
2863
2864 print "<table class=\"heads\" cellspacing=\"0\">\n";
6dd36acd 2865 my $alternate = 1;
717b8311 2866 for (my $i = $from; $i <= $to; $i++) {
120ddde2 2867 my $entry = $headlist->[$i];
cd146408
JN
2868 my %ref = %$entry;
2869 my $curr = $ref{'id'} eq $head;
717b8311
JN
2870 if ($alternate) {
2871 print "<tr class=\"dark\">\n";
2872 } else {
2873 print "<tr class=\"light\">\n";
2874 }
2875 $alternate ^= 1;
cd146408
JN
2876 print "<td><i>$ref{'age'}</i></td>\n" .
2877 ($curr ? "<td class=\"current_head\">" : "<td>") .
2878 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'}),
2879 -class => "list name"},esc_html($ref{'name'})) .
717b8311
JN
2880 "</td>\n" .
2881 "<td class=\"link\">" .
cd146408
JN
2882 $cgi->a({-href => href(action=>"shortlog", hash=>$ref{'name'})}, "shortlog") . " | " .
2883 $cgi->a({-href => href(action=>"log", hash=>$ref{'name'})}, "log") . " | " .
2884 $cgi->a({-href => href(action=>"tree", hash=>$ref{'name'}, hash_base=>$ref{'name'})}, "tree") .
717b8311
JN
2885 "</td>\n" .
2886 "</tr>";
2887 }
2888 if (defined $extra) {
2889 print "<tr>\n" .
2890 "<td colspan=\"3\">$extra</td>\n" .
2891 "</tr>\n";
2892 }
2893 print "</table>\n";
2894}
2895
8dbc0fce 2896sub git_search_grep_body {
5ad66088 2897 my ($commitlist, $from, $to, $extra) = @_;
8dbc0fce 2898 $from = 0 unless defined $from;
5ad66088 2899 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
8dbc0fce
RF
2900
2901 print "<table class=\"grep\" cellspacing=\"0\">\n";
2902 my $alternate = 1;
2903 for (my $i = $from; $i <= $to; $i++) {
5ad66088 2904 my %co = %{$commitlist->[$i]};
8dbc0fce
RF
2905 if (!%co) {
2906 next;
2907 }
5ad66088 2908 my $commit = $co{'id'};
8dbc0fce
RF
2909 if ($alternate) {
2910 print "<tr class=\"dark\">\n";
2911 } else {
2912 print "<tr class=\"light\">\n";
2913 }
2914 $alternate ^= 1;
2915 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2916 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2917 "<td>" .
2918 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2919 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2920 my $comment = $co{'comment'};
2921 foreach my $line (@$comment) {
2922 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2923 my $lead = esc_html($1) || "";
2924 $lead = chop_str($lead, 30, 10);
2925 my $match = esc_html($2) || "";
2926 my $trail = esc_html($3) || "";
2927 $trail = chop_str($trail, 30, 10);
2928 my $text = "$lead<span class=\"match\">$match</span>$trail";
2929 print chop_str($text, 80, 5) . "<br/>\n";
2930 }
2931 }
2932 print "</td>\n" .
2933 "<td class=\"link\">" .
2934 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2935 " | " .
2936 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2937 print "</td>\n" .
2938 "</tr>\n";
2939 }
2940 if (defined $extra) {
2941 print "<tr>\n" .
2942 "<td colspan=\"3\">$extra</td>\n" .
2943 "</tr>\n";
2944 }
2945 print "</table>\n";
2946}
2947
717b8311
JN
2948## ======================================================================
2949## ======================================================================
2950## actions
2951
717b8311 2952sub git_project_list {
6326b60c
JN
2953 my $order = $cgi->param('o');
2954 if (defined $order && $order !~ m/project|descr|owner|age/) {
e2860ead 2955 die_error(undef, "Unknown order parameter");
6326b60c
JN
2956 }
2957
847e01fb 2958 my @list = git_get_projects_list();
717b8311 2959 if (!@list) {
cac4bd94 2960 die_error(undef, "No projects found");
717b8311 2961 }
6326b60c 2962
717b8311
JN
2963 git_header_html();
2964 if (-f $home_text) {
2965 print "<div class=\"index_include\">\n";
2966 open (my $fd, $home_text);
2967 print <$fd>;
2968 close $fd;
2969 print "</div>\n";
9f5dcb81 2970 }
e30496df
PB
2971 git_project_list_body(\@list, $order);
2972 git_footer_html();
2973}
2974
2975sub git_forks {
2976 my $order = $cgi->param('o');
2977 if (defined $order && $order !~ m/project|descr|owner|age/) {
2978 die_error(undef, "Unknown order parameter");
717b8311 2979 }
e30496df
PB
2980
2981 my @list = git_get_projects_list($project);
2982 if (!@list) {
2983 die_error(undef, "No forks found");
9f5dcb81 2984 }
e30496df
PB
2985
2986 git_header_html();
2987 git_print_page_nav('','');
2988 git_print_header_div('summary', "$project forks");
2989 git_project_list_body(\@list, $order);
717b8311 2990 git_footer_html();
9f5dcb81
JN
2991}
2992
fc2b2be0 2993sub git_project_index {
e30496df 2994 my @projects = git_get_projects_list($project);
fc2b2be0
JN
2995
2996 print $cgi->header(
2997 -type => 'text/plain',
2998 -charset => 'utf-8',
ab41dfbf 2999 -content_disposition => 'inline; filename="index.aux"');
fc2b2be0
JN
3000
3001 foreach my $pr (@projects) {
3002 if (!exists $pr->{'owner'}) {
956259ee 3003 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}");
fc2b2be0
JN
3004 }
3005
3006 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
3007 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
3008 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3009 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
3010 $path =~ s/ /\+/g;
3011 $owner =~ s/ /\+/g;
3012
3013 print "$path $owner\n";
3014 }
3015}
3016
ede5e100 3017sub git_summary {
847e01fb 3018 my $descr = git_get_project_description($project) || "none";
a979d128 3019 my %co = parse_commit("HEAD");
847e01fb 3020 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
a979d128 3021 my $head = $co{'id'};
ede5e100 3022
1e0cf030 3023 my $owner = git_get_project_owner($project);
ede5e100 3024
cd146408 3025 my $refs = git_get_references();
313ce8ce
RF
3026 # These get_*_list functions return one more to allow us to see if
3027 # there are more ...
3028 my @taglist = git_get_tags_list(16);
3029 my @headlist = git_get_heads_list(16);
e30496df 3030 my @forklist;
5dd5ed09
JH
3031 my ($check_forks) = gitweb_check_feature('forks');
3032
3033 if ($check_forks) {
e30496df
PB
3034 @forklist = git_get_projects_list($project);
3035 }
120ddde2 3036
ede5e100 3037 git_header_html();
847e01fb 3038 git_print_page_nav('summary','', $head);
9f5dcb81 3039
19806691 3040 print "<div class=\"title\">&nbsp;</div>\n";
bddec01d 3041 print "<table cellspacing=\"0\">\n" .
40c13813 3042 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
ede5e100 3043 "<tr><td>owner</td><td>$owner</td></tr>\n" .
19a8721e 3044 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
e79ca7cc
JN
3045 # use per project git URL list in $projectroot/$project/cloneurl
3046 # or make project git URL from git base URL and project name
19a8721e 3047 my $url_tag = "URL";
e79ca7cc
JN
3048 my @url_list = git_get_project_url_list($project);
3049 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
3050 foreach my $git_url (@url_list) {
3051 next unless $git_url;
3052 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
19a8721e
JN
3053 $url_tag = "";
3054 }
3055 print "</table>\n";
9f5dcb81 3056
447ef09a
PB
3057 if (-s "$projectroot/$project/README.html") {
3058 if (open my $fd, "$projectroot/$project/README.html") {
3059 print "<div class=\"title\">readme</div>\n";
3060 print $_ while (<$fd>);
3061 close $fd;
3062 }
3063 }
3064
313ce8ce
RF
3065 # we need to request one more than 16 (0..15) to check if
3066 # those 16 are all
190d7fdc 3067 my @commitlist = parse_commits($head, 17);
847e01fb 3068 git_print_header_div('shortlog');
190d7fdc
RF
3069 git_shortlog_body(\@commitlist, 0, 15, $refs,
3070 $#commitlist <= 15 ? undef :
1c2a4f5a 3071 $cgi->a({-href => href(action=>"shortlog")}, "..."));
ede5e100 3072
120ddde2 3073 if (@taglist) {
847e01fb 3074 git_print_header_div('tags');
120ddde2 3075 git_tags_body(\@taglist, 0, 15,
313ce8ce 3076 $#taglist <= 15 ? undef :
1c2a4f5a 3077 $cgi->a({-href => href(action=>"tags")}, "..."));
ede5e100 3078 }
0db37973 3079
120ddde2 3080 if (@headlist) {
847e01fb 3081 git_print_header_div('heads');
120ddde2 3082 git_heads_body(\@headlist, $head, 0, 15,
313ce8ce 3083 $#headlist <= 15 ? undef :
1c2a4f5a 3084 $cgi->a({-href => href(action=>"heads")}, "..."));
0db37973 3085 }
9f5dcb81 3086
e30496df
PB
3087 if (@forklist) {
3088 git_print_header_div('forks');
3089 git_project_list_body(\@forklist, undef, 0, 15,
aaca9675 3090 $#forklist <= 15 ? undef :
e30496df
PB
3091 $cgi->a({-href => href(action=>"forks")}, "..."),
3092 'noheader');
3093 }
3094
ede5e100
KS
3095 git_footer_html();
3096}
3097
d8a20ba9 3098sub git_tag {
847e01fb 3099 my $head = git_get_head_hash($project);
d8a20ba9 3100 git_header_html();
847e01fb
JN
3101 git_print_page_nav('','', $head,undef,$head);
3102 my %tag = parse_tag($hash);
3103 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
d8a20ba9
KS
3104 print "<div class=\"title_text\">\n" .
3105 "<table cellspacing=\"0\">\n" .
e4669df9
KS
3106 "<tr>\n" .
3107 "<td>object</td>\n" .
952c65fc
JN
3108 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3109 $tag{'object'}) . "</td>\n" .
3110 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
3111 $tag{'type'}) . "</td>\n" .
e4669df9 3112 "</tr>\n";
d8a20ba9 3113 if (defined($tag{'author'})) {
847e01fb 3114 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
40c13813 3115 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
952c65fc
JN
3116 print "<tr><td></td><td>" . $ad{'rfc2822'} .
3117 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
3118 "</td></tr>\n";
d8a20ba9
KS
3119 }
3120 print "</table>\n\n" .
3121 "</div>\n";
3122 print "<div class=\"page_body\">";
3123 my $comment = $tag{'comment'};
3124 foreach my $line (@$comment) {
7002243f 3125 chomp $line;
793c400c 3126 print esc_html($line, -nbsp=>1) . "<br/>\n";
d8a20ba9
KS
3127 }
3128 print "</div>\n";
3129 git_footer_html();
3130}
3131
1f2857ea
LT
3132sub git_blame2 {
3133 my $fd;
3134 my $ftype;
ddb8d900 3135
1d3fc68a
AK
3136 my ($have_blame) = gitweb_check_feature('blame');
3137 if (!$have_blame) {
ddb8d900
AK
3138 die_error('403 Permission denied', "Permission denied");
3139 }
1f2857ea 3140 die_error('404 Not Found', "File name not defined") if (!$file_name);
847e01fb 3141 $hash_base ||= git_get_head_hash($project);
cac4bd94 3142 die_error(undef, "Couldn't find base commit") unless ($hash_base);
847e01fb 3143 my %co = parse_commit($hash_base)
1f2857ea
LT
3144 or die_error(undef, "Reading commit failed");
3145 if (!defined $hash) {
3146 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
3147 or die_error(undef, "Error looking up file");
3148 }
3149 $ftype = git_get_type($hash);
3150 if ($ftype !~ "blob") {
f73bbb2d 3151 die_error('400 Bad Request', "Object is not a blob");
1f2857ea 3152 }
48fd688a 3153 open ($fd, "-|", git_cmd(), "blame", '-p', '--',
eeef88cd 3154 $file_name, $hash_base)
cac4bd94 3155 or die_error(undef, "Open git-blame failed");
1f2857ea 3156 git_header_html();
0d83ddc4 3157 my $formats_nav =
952c65fc
JN
3158 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3159 "blob") .
3160 " | " .
cae1862a
PB
3161 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3162 "history") .
3163 " | " .
952c65fc 3164 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
f35274da 3165 "HEAD");
847e01fb
JN
3166 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3167 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
59fb1c94 3168 git_print_page_path($file_name, $ftype, $hash_base);
82f930de 3169 my @rev_color = (qw(light2 dark2));
cc1bf97e
LT
3170 my $num_colors = scalar(@rev_color);
3171 my $current_color = 0;
3172 my $last_rev;
59b9f61a
JN
3173 print <<HTML;
3174<div class="page_body">
3175<table class="blame">
3176<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
3177HTML
eeef88cd
JH
3178 my %metainfo = ();
3179 while (1) {
3180 $_ = <$fd>;
3181 last unless defined $_;
d15c55aa 3182 my ($full_rev, $orig_lineno, $lineno, $group_size) =
eeef88cd
JH
3183 /^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;
3184 if (!exists $metainfo{$full_rev}) {
3185 $metainfo{$full_rev} = {};
3186 }
3187 my $meta = $metainfo{$full_rev};
3188 while (<$fd>) {
3189 last if (s/^\t//);
3190 if (/^(\S+) (.*)$/) {
3191 $meta->{$1} = $2;
3192 }
3193 }
3194 my $data = $_;
7002243f 3195 chomp $data;
1f2857ea 3196 my $rev = substr($full_rev, 0, 8);
eeef88cd
JH
3197 my $author = $meta->{'author'};
3198 my %date = parse_date($meta->{'author-time'},
3199 $meta->{'author-tz'});
3200 my $date = $date{'iso-tz'};
3201 if ($group_size) {
cc1bf97e
LT
3202 $current_color = ++$current_color % $num_colors;
3203 }
3204 print "<tr class=\"$rev_color[$current_color]\">\n";
eeef88cd
JH
3205 if ($group_size) {
3206 print "<td class=\"sha1\"";
5ad0828c 3207 print " title=\"". esc_html($author) . ", $date\"";
eeef88cd
JH
3208 print " rowspan=\"$group_size\"" if ($group_size > 1);
3209 print ">";
3210 print $cgi->a({-href => href(action=>"commit",
3211 hash=>$full_rev,
3212 file_name=>$file_name)},
9dc5f8c9 3213 esc_html($rev));
eeef88cd 3214 print "</td>\n";
9dc5f8c9 3215 }
244a70e6 3216 open (my $dd, "-|", git_cmd(), "rev-parse", "$full_rev^")
f73bbb2d 3217 or die_error(undef, "Open git-rev-parse failed");
244a70e6
LT
3218 my $parent_commit = <$dd>;
3219 close $dd;
3220 chomp($parent_commit);
eeef88cd
JH
3221 my $blamed = href(action => 'blame',
3222 file_name => $meta->{'filename'},
244a70e6 3223 hash_base => $parent_commit);
eeef88cd
JH
3224 print "<td class=\"linenr\">";
3225 print $cgi->a({ -href => "$blamed#l$orig_lineno",
3226 -id => "l$lineno",
3227 -class => "linenr" },
3228 esc_html($lineno));
3229 print "</td>";
1f2857ea
LT
3230 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
3231 print "</tr>\n";
3232 }
3233 print "</table>\n";
3234 print "</div>";
952c65fc
JN
3235 close $fd
3236 or print "Reading blob failed\n";
1f2857ea
LT
3237 git_footer_html();
3238}
3239
e34ef621
FF
3240sub git_blame {
3241 my $fd;
ddb8d900 3242
1d3fc68a
AK
3243 my ($have_blame) = gitweb_check_feature('blame');
3244 if (!$have_blame) {
ddb8d900
AK
3245 die_error('403 Permission denied', "Permission denied");
3246 }
cac4bd94 3247 die_error('404 Not Found', "File name not defined") if (!$file_name);
847e01fb 3248 $hash_base ||= git_get_head_hash($project);
cac4bd94 3249 die_error(undef, "Couldn't find base commit") unless ($hash_base);
847e01fb 3250 my %co = parse_commit($hash_base)
cac4bd94 3251 or die_error(undef, "Reading commit failed");
e34ef621
FF
3252 if (!defined $hash) {
3253 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
cac4bd94 3254 or die_error(undef, "Error lookup file");
e34ef621 3255 }
25691fbe 3256 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
cac4bd94 3257 or die_error(undef, "Open git-annotate failed");
e34ef621 3258 git_header_html();
0d83ddc4 3259 my $formats_nav =
952c65fc
JN
3260 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3261 "blob") .
3262 " | " .
cae1862a
PB
3263 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
3264 "history") .
3265 " | " .
952c65fc 3266 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
f35274da 3267 "HEAD");
847e01fb
JN
3268 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3269 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
59fb1c94 3270 git_print_page_path($file_name, 'blob', $hash_base);
e34ef621
FF
3271 print "<div class=\"page_body\">\n";
3272 print <<HTML;
1f1ab5f0 3273<table class="blame">
e34ef621
FF
3274 <tr>
3275 <th>Commit</th>
3276 <th>Age</th>
3277 <th>Author</th>
3278 <th>Line</th>
3279 <th>Data</th>
3280 </tr>
3281HTML
3282 my @line_class = (qw(light dark));
3283 my $line_class_len = scalar (@line_class);
3284 my $line_class_num = $#line_class;
3285 while (my $line = <$fd>) {
3286 my $long_rev;
3287 my $short_rev;
3288 my $author;
3289 my $time;
3290 my $lineno;
3291 my $data;
3292 my $age;
3293 my $age_str;
1f1ab5f0 3294 my $age_class;
e34ef621
FF
3295
3296 chomp $line;
3297 $line_class_num = ($line_class_num + 1) % $line_class_len;
3298
030b5208 3299 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
e34ef621
FF
3300 $long_rev = $1;
3301 $author = $2;
3302 $time = $3;
3303 $lineno = $4;
3304 $data = $5;
3305 } else {
1f1ab5f0 3306 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
e34ef621
FF
3307 next;
3308 }
3309 $short_rev = substr ($long_rev, 0, 8);
3310 $age = time () - $time;
3311 $age_str = age_string ($age);
3312 $age_str =~ s/ /&nbsp;/g;
1f1ab5f0 3313 $age_class = age_class($age);
e34ef621
FF
3314 $author = esc_html ($author);
3315 $author =~ s/ /&nbsp;/g;
f16db173
JN
3316
3317 $data = untabify($data);
717b8311 3318 $data = esc_html ($data);
2d007374 3319
717b8311
JN
3320 print <<HTML;
3321 <tr class="$line_class[$line_class_num]">
1c2a4f5a 3322 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
717b8311
JN
3323 <td class="$age_class">$age_str</td>
3324 <td>$author</td>
3325 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
3326 <td class="pre">$data</td>
3327 </tr>
3328HTML
3329 } # while (my $line = <$fd>)
3330 print "</table>\n\n";
952c65fc
JN
3331 close $fd
3332 or print "Reading blob failed.\n";
717b8311
JN
3333 print "</div>";
3334 git_footer_html();
2d007374
PB
3335}
3336
717b8311 3337sub git_tags {
847e01fb 3338 my $head = git_get_head_hash($project);
717b8311 3339 git_header_html();
847e01fb
JN
3340 git_print_page_nav('','', $head,undef,$head);
3341 git_print_header_div('summary', $project);
2d007374 3342
cd146408
JN
3343 my @tagslist = git_get_tags_list();
3344 if (@tagslist) {
3345 git_tags_body(\@tagslist);
2d007374 3346 }
717b8311 3347 git_footer_html();
2d007374
PB
3348}
3349
717b8311 3350sub git_heads {
847e01fb 3351 my $head = git_get_head_hash($project);
717b8311 3352 git_header_html();
847e01fb
JN
3353 git_print_page_nav('','', $head,undef,$head);
3354 git_print_header_div('summary', $project);
930cf7dd 3355
cd146408
JN
3356 my @headslist = git_get_heads_list();
3357 if (@headslist) {
3358 git_heads_body(\@headslist, $head);
f5aa79d9 3359 }
717b8311 3360 git_footer_html();
f5aa79d9
JN
3361}
3362
19806691 3363sub git_blob_plain {
f2e73302 3364 my $expires;
f2e73302 3365
cff0771b 3366 if (!defined $hash) {
5be01bc8 3367 if (defined $file_name) {
847e01fb 3368 my $base = $hash_base || git_get_head_hash($project);
5be01bc8 3369 $hash = git_get_hash_by_path($base, $file_name, "blob")
cac4bd94 3370 or die_error(undef, "Error lookup file");
5be01bc8 3371 } else {
cac4bd94 3372 die_error(undef, "No file name defined");
5be01bc8 3373 }
800764cf
MW
3374 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3375 # blobs defined by non-textual hash id's can be cached
3376 $expires = "+1d";
5be01bc8 3377 }
800764cf 3378
930cf7dd 3379 my $type = shift;
25691fbe 3380 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
cfd82669 3381 or die_error(undef, "Couldn't cat $file_name, $hash");
930cf7dd 3382
847e01fb 3383 $type ||= blob_mimetype($fd, $file_name);
f5aa79d9
JN
3384
3385 # save as filename, even when no $file_name is given
3386 my $save_as = "$hash";
9312944d
KS
3387 if (defined $file_name) {