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