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