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