]> git.ipfire.org Git - thirdparty/git.git/blame - gitweb/gitweb.perl
gitweb: Split validate_input into validate_pathname and validate_refname
[thirdparty/git.git] / gitweb / gitweb.perl
CommitLineData
161332a5
KS
1#!/usr/bin/perl
2
c994d620 3# gitweb - simple web interface to track changes in git repositories
22fafb99 4#
00cd0794
KS
5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6# (C) 2005, Christian Gierke
823d5dc8 7#
d8f1c5c2 8# This program is licensed under the GPLv2
161332a5
KS
9
10use strict;
11use warnings;
19806691 12use CGI qw(:standard :escapeHTML -nosticky);
7403d50b 13use CGI::Util qw(unescape);
161332a5 14use CGI::Carp qw(fatalsToBrowser);
40c13813 15use Encode;
b87d78d6 16use Fcntl ':mode';
7a13b999 17use File::Find qw();
cb9c6e5b 18use File::Basename qw(basename);
10bb9036 19binmode STDOUT, ':utf8';
161332a5 20
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
232ff553 446# replace invalid utf8 character with SUBSTITUTION sequence
40c13813
KS
447sub esc_html {
448 my $str = shift;
40c13813 449 $str = decode("utf8", $str, Encode::FB_DEFAULT);
10bb9036 450 $str = escapeHTML($str);
3dc13832 451 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
40c13813
KS
452 return $str;
453}
454
232ff553
KS
455# git may return quoted and escaped filenames
456sub unquote {
457 my $str = shift;
458 if ($str =~ m/^"(.*)"$/) {
459 $str = $1;
460 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
461 }
462 return $str;
463}
464
f16db173
JN
465# escape tabs (convert tabs to spaces)
466sub untabify {
467 my $line = shift;
468
469 while ((my $pos = index($line, "\t")) != -1) {
470 if (my $count = (8 - ($pos % 8))) {
471 my $spaces = ' ' x $count;
472 $line =~ s/\t/$spaces/;
473 }
474 }
475
476 return $line;
477}
478
32f4aacc
ML
479sub project_in_list {
480 my $project = shift;
481 my @list = git_get_projects_list();
482 return @list && scalar(grep { $_->{'path'} eq $project } @list);
483}
484
717b8311
JN
485## ----------------------------------------------------------------------
486## HTML aware string manipulation
487
488sub chop_str {
489 my $str = shift;
490 my $len = shift;
491 my $add_len = shift || 10;
492
493 # allow only $len chars, but don't cut a word if it would fit in $add_len
494 # if it doesn't fit, cut it if it's still longer than the dots we would add
495 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
496 my $body = $1;
497 my $tail = $2;
498 if (length($tail) > 4) {
499 $tail = " ...";
500 $body =~ s/&[^;]*$//; # remove chopped character entities
501 }
502 return "$body$tail";
503}
504
505## ----------------------------------------------------------------------
506## functions returning short strings
507
1f1ab5f0
JN
508# CSS class for given age value (in seconds)
509sub age_class {
510 my $age = shift;
511
512 if ($age < 60*60*2) {
513 return "age0";
514 } elsif ($age < 60*60*24*2) {
515 return "age1";
516 } else {
517 return "age2";
518 }
519}
520
717b8311
JN
521# convert age in seconds to "nn units ago" string
522sub age_string {
523 my $age = shift;
524 my $age_str;
a59d4afd 525
717b8311
JN
526 if ($age > 60*60*24*365*2) {
527 $age_str = (int $age/60/60/24/365);
528 $age_str .= " years ago";
529 } elsif ($age > 60*60*24*(365/12)*2) {
530 $age_str = int $age/60/60/24/(365/12);
531 $age_str .= " months ago";
532 } elsif ($age > 60*60*24*7*2) {
533 $age_str = int $age/60/60/24/7;
534 $age_str .= " weeks ago";
535 } elsif ($age > 60*60*24*2) {
536 $age_str = int $age/60/60/24;
537 $age_str .= " days ago";
538 } elsif ($age > 60*60*2) {
539 $age_str = int $age/60/60;
540 $age_str .= " hours ago";
541 } elsif ($age > 60*2) {
542 $age_str = int $age/60;
543 $age_str .= " min ago";
544 } elsif ($age > 2) {
545 $age_str = int $age;
546 $age_str .= " sec ago";
f6801d66 547 } else {
717b8311 548 $age_str .= " right now";
4c02e3c5 549 }
717b8311 550 return $age_str;
161332a5
KS
551}
552
717b8311
JN
553# convert file mode in octal to symbolic file mode string
554sub mode_str {
555 my $mode = oct shift;
556
557 if (S_ISDIR($mode & S_IFMT)) {
558 return 'drwxr-xr-x';
559 } elsif (S_ISLNK($mode)) {
560 return 'lrwxrwxrwx';
561 } elsif (S_ISREG($mode)) {
562 # git cares only about the executable bit
563 if ($mode & S_IXUSR) {
564 return '-rwxr-xr-x';
565 } else {
566 return '-rw-r--r--';
567 };
c994d620 568 } else {
717b8311 569 return '----------';
ff7669a5 570 }
161332a5
KS
571}
572
717b8311
JN
573# convert file mode in octal to file type string
574sub file_type {
7c5e2ebb
JN
575 my $mode = shift;
576
577 if ($mode !~ m/^[0-7]+$/) {
578 return $mode;
579 } else {
580 $mode = oct $mode;
581 }
664f4cc5 582
717b8311
JN
583 if (S_ISDIR($mode & S_IFMT)) {
584 return "directory";
585 } elsif (S_ISLNK($mode)) {
586 return "symlink";
587 } elsif (S_ISREG($mode)) {
588 return "file";
589 } else {
590 return "unknown";
591 }
a59d4afd
KS
592}
593
717b8311
JN
594## ----------------------------------------------------------------------
595## functions returning short HTML fragments, or transforming HTML fragments
596## which don't beling to other sections
b18f9bf4 597
717b8311
JN
598# format line of commit message or tag comment
599sub format_log_line_html {
600 my $line = shift;
b18f9bf4 601
717b8311
JN
602 $line = esc_html($line);
603 $line =~ s/ /&nbsp;/g;
604 if ($line =~ m/([0-9a-fA-F]{40})/) {
605 my $hash_text = $1;
606 if (git_get_type($hash_text) eq "commit") {
952c65fc
JN
607 my $link =
608 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
609 -class => "text"}, $hash_text);
717b8311 610 $line =~ s/$hash_text/$link/;
b18f9bf4
JN
611 }
612 }
717b8311 613 return $line;
b18f9bf4
JN
614}
615
717b8311 616# format marker of refs pointing to given object
847e01fb 617sub format_ref_marker {
717b8311 618 my ($refs, $id) = @_;
d294e1ca 619 my $markers = '';
27fb8c40 620
717b8311 621 if (defined $refs->{$id}) {
d294e1ca
JN
622 foreach my $ref (@{$refs->{$id}}) {
623 my ($type, $name) = qw();
624 # e.g. tags/v2.6.11 or heads/next
625 if ($ref =~ m!^(.*?)s?/(.*)$!) {
626 $type = $1;
627 $name = $2;
628 } else {
629 $type = "ref";
630 $name = $ref;
631 }
632
633 $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
634 }
635 }
636
637 if ($markers) {
638 return ' <span class="refs">'. $markers . '</span>';
717b8311
JN
639 } else {
640 return "";
641 }
27fb8c40
JN
642}
643
17d07443
JN
644# format, perhaps shortened and with markers, title line
645sub format_subject_html {
1c2a4f5a 646 my ($long, $short, $href, $extra) = @_;
17d07443
JN
647 $extra = '' unless defined($extra);
648
649 if (length($short) < length($long)) {
7c278014 650 return $cgi->a({-href => $href, -class => "list subject",
1c2a4f5a 651 -title => $long},
17d07443
JN
652 esc_html($short) . $extra);
653 } else {
7c278014 654 return $cgi->a({-href => $href, -class => "list subject"},
17d07443
JN
655 esc_html($long) . $extra);
656 }
657}
658
eee08903
JN
659sub format_diff_line {
660 my $line = shift;
661 my $char = substr($line, 0, 1);
662 my $diff_class = "";
663
664 chomp $line;
665
666 if ($char eq '+') {
667 $diff_class = " add";
668 } elsif ($char eq "-") {
669 $diff_class = " rem";
670 } elsif ($char eq "@") {
671 $diff_class = " chunk_header";
672 } elsif ($char eq "\\") {
af33ef21 673 $diff_class = " incomplete";
eee08903
JN
674 }
675 $line = untabify($line);
676 return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
677}
678
717b8311
JN
679## ----------------------------------------------------------------------
680## git utility subroutines, invoking git commands
42f7eb94 681
25691fbe
DS
682# returns path to the core git executable and the --git-dir parameter as list
683sub git_cmd {
684 return $GIT, '--git-dir='.$git_dir;
685}
686
687# returns path to the core git executable and the --git-dir parameter as string
688sub git_cmd_str {
689 return join(' ', git_cmd());
690}
691
717b8311 692# get HEAD ref of given project as hash
847e01fb 693sub git_get_head_hash {
df2c37a5 694 my $project = shift;
25691fbe 695 my $o_git_dir = $git_dir;
df2c37a5 696 my $retval = undef;
25691fbe
DS
697 $git_dir = "$projectroot/$project";
698 if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
df2c37a5
JH
699 my $head = <$fd>;
700 close $fd;
2c5c008b
KS
701 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
702 $retval = $1;
df2c37a5
JH
703 }
704 }
25691fbe
DS
705 if (defined $o_git_dir) {
706 $git_dir = $o_git_dir;
2c5c008b 707 }
df2c37a5
JH
708 return $retval;
709}
710
717b8311
JN
711# get type of given object
712sub git_get_type {
713 my $hash = shift;
714
25691fbe 715 open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
717b8311
JN
716 my $type = <$fd>;
717 close $fd or return;
718 chomp $type;
719 return $type;
720}
721
722sub git_get_project_config {
ddb8d900 723 my ($key, $type) = @_;
717b8311
JN
724
725 return unless ($key);
726 $key =~ s/^gitweb\.//;
727 return if ($key =~ m/\W/);
728
25691fbe 729 my @x = (git_cmd(), 'repo-config');
ddb8d900
AK
730 if (defined $type) { push @x, $type; }
731 push @x, "--get";
732 push @x, "gitweb.$key";
733 my $val = qx(@x);
734 chomp $val;
717b8311
JN
735 return ($val);
736}
737
717b8311
JN
738# get hash of given path at given ref
739sub git_get_hash_by_path {
740 my $base = shift;
741 my $path = shift || return undef;
1d782b03 742 my $type = shift;
717b8311 743
4b02f483 744 $path =~ s,/+$,,;
717b8311 745
25691fbe 746 open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
cac4bd94 747 or die_error(undef, "Open git-ls-tree failed");
717b8311
JN
748 my $line = <$fd>;
749 close $fd or return undef;
750
751 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
752 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1d782b03
JN
753 if (defined $type && $type ne $2) {
754 # type doesn't match
755 return undef;
756 }
717b8311
JN
757 return $3;
758}
759
760## ......................................................................
761## git utility functions, directly accessing git repository
762
847e01fb 763sub git_get_project_description {
b87d78d6 764 my $path = shift;
09bd7898 765
19806691 766 open my $fd, "$projectroot/$path/description" or return undef;
b87d78d6
KS
767 my $descr = <$fd>;
768 close $fd;
769 chomp $descr;
770 return $descr;
12a88f2f
KS
771}
772
e79ca7cc
JN
773sub git_get_project_url_list {
774 my $path = shift;
775
74d61667 776 open my $fd, "$projectroot/$path/cloneurl" or return;
e79ca7cc
JN
777 my @git_project_url_list = map { chomp; $_ } <$fd>;
778 close $fd;
779
780 return wantarray ? @git_project_url_list : \@git_project_url_list;
781}
782
847e01fb 783sub git_get_projects_list {
717b8311
JN
784 my @list;
785
786 if (-d $projects_list) {
787 # search in directory
788 my $dir = $projects_list;
c0011ff8
JN
789 my $pfxlen = length("$dir");
790
791 File::Find::find({
792 follow_fast => 1, # follow symbolic links
793 dangling_symlinks => 0, # ignore dangling symlinks, silently
794 wanted => sub {
795 # skip project-list toplevel, if we get it.
796 return if (m!^[/.]$!);
797 # only directories can be git repositories
798 return unless (-d $_);
799
800 my $subdir = substr($File::Find::name, $pfxlen + 1);
801 # we check related file in $projectroot
32f4aacc
ML
802 if (-e "$projectroot/$subdir/HEAD" && (!$export_ok ||
803 -e "$projectroot/$subdir/$export_ok")) {
c0011ff8
JN
804 push @list, { path => $subdir };
805 $File::Find::prune = 1;
806 }
807 },
808 }, "$dir");
809
717b8311
JN
810 } elsif (-f $projects_list) {
811 # read from file(url-encoded):
812 # 'git%2Fgit.git Linus+Torvalds'
813 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
814 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
dd1ad5f1 815 open my ($fd), $projects_list or return;
717b8311
JN
816 while (my $line = <$fd>) {
817 chomp $line;
818 my ($path, $owner) = split ' ', $line;
819 $path = unescape($path);
820 $owner = unescape($owner);
821 if (!defined $path) {
822 next;
823 }
32f4aacc
ML
824 if (-e "$projectroot/$path/HEAD" && (!$export_ok ||
825 -e "$projectroot/$path/$export_ok")) {
717b8311
JN
826 my $pr = {
827 path => $path,
828 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
829 };
830 push @list, $pr
831 }
832 }
833 close $fd;
834 }
835 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
836 return @list;
837}
838
1e0cf030
JN
839sub git_get_project_owner {
840 my $project = shift;
841 my $owner;
842
843 return undef unless $project;
844
845 # read from file (url-encoded):
846 # 'git%2Fgit.git Linus+Torvalds'
847 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
848 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
849 if (-f $projects_list) {
850 open (my $fd , $projects_list);
851 while (my $line = <$fd>) {
852 chomp $line;
853 my ($pr, $ow) = split ' ', $line;
854 $pr = unescape($pr);
855 $ow = unescape($ow);
856 if ($pr eq $project) {
857 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
858 last;
859 }
860 }
861 close $fd;
862 }
863 if (!defined $owner) {
864 $owner = get_file_owner("$projectroot/$project");
865 }
866
867 return $owner;
868}
869
847e01fb 870sub git_get_references {
717b8311
JN
871 my $type = shift || "";
872 my %refs;
873 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
874 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
9704d75d
JN
875 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
876 or return;
d294e1ca 877
717b8311
JN
878 while (my $line = <$fd>) {
879 chomp $line;
d294e1ca 880 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
717b8311 881 if (defined $refs{$1}) {
d294e1ca 882 push @{$refs{$1}}, $2;
717b8311 883 } else {
d294e1ca 884 $refs{$1} = [ $2 ];
717b8311
JN
885 }
886 }
887 }
888 close $fd or return;
889 return \%refs;
890}
891
56a322f1
JN
892sub git_get_rev_name_tags {
893 my $hash = shift || return undef;
894
25691fbe 895 open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
56a322f1
JN
896 or return;
897 my $name_rev = <$fd>;
898 close $fd;
899
900 if ($name_rev =~ m|^$hash tags/(.*)$|) {
901 return $1;
902 } else {
903 # catches also '$hash undefined' output
904 return undef;
905 }
906}
907
717b8311
JN
908## ----------------------------------------------------------------------
909## parse to hash functions
910
847e01fb 911sub parse_date {
717b8311
JN
912 my $epoch = shift;
913 my $tz = shift || "-0000";
914
915 my %date;
916 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
917 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
918 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
919 $date{'hour'} = $hour;
920 $date{'minute'} = $min;
921 $date{'mday'} = $mday;
922 $date{'day'} = $days[$wday];
923 $date{'month'} = $months[$mon];
952c65fc
JN
924 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
925 $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
926 $date{'mday-time'} = sprintf "%d %s %02d:%02d",
927 $mday, $months[$mon], $hour ,$min;
717b8311
JN
928
929 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
930 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
931 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
932 $date{'hour_local'} = $hour;
933 $date{'minute_local'} = $min;
934 $date{'tz_local'} = $tz;
935 return %date;
936}
937
847e01fb 938sub parse_tag {
ede5e100
KS
939 my $tag_id = shift;
940 my %tag;
d8a20ba9 941 my @comment;
ede5e100 942
25691fbe 943 open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
d8a20ba9 944 $tag{'id'} = $tag_id;
ede5e100
KS
945 while (my $line = <$fd>) {
946 chomp $line;
947 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
948 $tag{'object'} = $1;
7ab0d2b6 949 } elsif ($line =~ m/^type (.+)$/) {
ede5e100 950 $tag{'type'} = $1;
7ab0d2b6 951 } elsif ($line =~ m/^tag (.+)$/) {
ede5e100 952 $tag{'name'} = $1;
d8a20ba9
KS
953 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
954 $tag{'author'} = $1;
955 $tag{'epoch'} = $2;
956 $tag{'tz'} = $3;
957 } elsif ($line =~ m/--BEGIN/) {
958 push @comment, $line;
959 last;
960 } elsif ($line eq "") {
961 last;
ede5e100
KS
962 }
963 }
d8a20ba9
KS
964 push @comment, <$fd>;
965 $tag{'comment'} = \@comment;
19806691 966 close $fd or return;
ede5e100
KS
967 if (!defined $tag{'name'}) {
968 return
969 };
970 return %tag
971}
972
847e01fb 973sub parse_commit {
19806691
KS
974 my $commit_id = shift;
975 my $commit_text = shift;
976
977 my @commit_lines;
703ac710 978 my %co;
703ac710 979
19806691
KS
980 if (defined $commit_text) {
981 @commit_lines = @$commit_text;
982 } else {
25f422fb 983 $/ = "\0";
25691fbe 984 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
952c65fc 985 or return;
25f422fb 986 @commit_lines = split '\n', <$fd>;
19806691 987 close $fd or return;
25f422fb
KS
988 $/ = "\n";
989 pop @commit_lines;
19806691 990 }
25f422fb
KS
991 my $header = shift @commit_lines;
992 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
993 return;
994 }
995 ($co{'id'}, my @parents) = split ' ', $header;
996 $co{'parents'} = \@parents;
997 $co{'parent'} = $parents[0];
19806691 998 while (my $line = shift @commit_lines) {
b87d78d6 999 last if $line eq "\n";
7ab0d2b6 1000 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
703ac710 1001 $co{'tree'} = $1;
022be3d0 1002 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3f714537 1003 $co{'author'} = $1;
185f09e5
KS
1004 $co{'author_epoch'} = $2;
1005 $co{'author_tz'} = $3;
2bf7a52c
KS
1006 if ($co{'author'} =~ m/^([^<]+) </) {
1007 $co{'author_name'} = $1;
1008 } else {
1009 $co{'author_name'} = $co{'author'};
1010 }
86eed32d
KS
1011 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
1012 $co{'committer'} = $1;
185f09e5
KS
1013 $co{'committer_epoch'} = $2;
1014 $co{'committer_tz'} = $3;
991910a9
KS
1015 $co{'committer_name'} = $co{'committer'};
1016 $co{'committer_name'} =~ s/ <.*//;
703ac710
KS
1017 }
1018 }
ede5e100 1019 if (!defined $co{'tree'}) {
25f422fb 1020 return;
ede5e100 1021 };
25f422fb 1022
19806691 1023 foreach my $title (@commit_lines) {
c2488d06 1024 $title =~ s/^ //;
19806691 1025 if ($title ne "") {
48c771f4 1026 $co{'title'} = chop_str($title, 80, 5);
19806691
KS
1027 # remove leading stuff of merges to make the interesting part visible
1028 if (length($title) > 50) {
1029 $title =~ s/^Automatic //;
1030 $title =~ s/^merge (of|with) /Merge ... /i;
1031 if (length($title) > 50) {
1032 $title =~ s/(http|rsync):\/\///;
1033 }
1034 if (length($title) > 50) {
1035 $title =~ s/(master|www|rsync)\.//;
1036 }
1037 if (length($title) > 50) {
1038 $title =~ s/kernel.org:?//;
1039 }
1040 if (length($title) > 50) {
1041 $title =~ s/\/pub\/scm//;
1042 }
1043 }
48c771f4 1044 $co{'title_short'} = chop_str($title, 50, 5);
19806691
KS
1045 last;
1046 }
1047 }
25f422fb
KS
1048 # remove added spaces
1049 foreach my $line (@commit_lines) {
1050 $line =~ s/^ //;
1051 }
1052 $co{'comment'} = \@commit_lines;
2ae100df
KS
1053
1054 my $age = time - $co{'committer_epoch'};
1055 $co{'age'} = $age;
d263a6bd 1056 $co{'age_string'} = age_string($age);
71be1e79
KS
1057 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
1058 if ($age > 60*60*24*7*2) {
1b1cd421 1059 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
71be1e79
KS
1060 $co{'age_string_age'} = $co{'age_string'};
1061 } else {
1062 $co{'age_string_date'} = $co{'age_string'};
1b1cd421 1063 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
71be1e79 1064 }
703ac710
KS
1065 return %co;
1066}
1067
a446d6bb
JN
1068# parse ref from ref_file, given by ref_id, with given type
1069sub parse_ref {
1070 my $ref_file = shift;
1071 my $ref_id = shift;
1072 my $type = shift || git_get_type($ref_id);
1073 my %ref_item;
1074
1075 $ref_item{'type'} = $type;
1076 $ref_item{'id'} = $ref_id;
1077 $ref_item{'epoch'} = 0;
1078 $ref_item{'age'} = "unknown";
1079 if ($type eq "tag") {
1080 my %tag = parse_tag($ref_id);
1081 $ref_item{'comment'} = $tag{'comment'};
1082 if ($tag{'type'} eq "commit") {
1083 my %co = parse_commit($tag{'object'});
1084 $ref_item{'epoch'} = $co{'committer_epoch'};
1085 $ref_item{'age'} = $co{'age_string'};
1086 } elsif (defined($tag{'epoch'})) {
1087 my $age = time - $tag{'epoch'};
1088 $ref_item{'epoch'} = $tag{'epoch'};
1089 $ref_item{'age'} = age_string($age);
1090 }
1091 $ref_item{'reftype'} = $tag{'type'};
1092 $ref_item{'name'} = $tag{'name'};
1093 $ref_item{'refid'} = $tag{'object'};
1094 } elsif ($type eq "commit"){
1095 my %co = parse_commit($ref_id);
1096 $ref_item{'reftype'} = "commit";
1097 $ref_item{'name'} = $ref_file;
1098 $ref_item{'title'} = $co{'title'};
1099 $ref_item{'refid'} = $ref_id;
1100 $ref_item{'epoch'} = $co{'committer_epoch'};
1101 $ref_item{'age'} = $co{'age_string'};
1102 } else {
1103 $ref_item{'reftype'} = $type;
1104 $ref_item{'name'} = $ref_file;
1105 $ref_item{'refid'} = $ref_id;
1106 }
1107
1108 return %ref_item;
1109}
1110
e8e41a93 1111# parse line of git-diff-tree "raw" output
740e67f9
JN
1112sub parse_difftree_raw_line {
1113 my $line = shift;
1114 my %res;
1115
1116 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1117 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1118 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1119 $res{'from_mode'} = $1;
1120 $res{'to_mode'} = $2;
1121 $res{'from_id'} = $3;
1122 $res{'to_id'} = $4;
1123 $res{'status'} = $5;
1124 $res{'similarity'} = $6;
1125 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
e8e41a93 1126 ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
740e67f9
JN
1127 } else {
1128 $res{'file'} = unquote($7);
1129 }
1130 }
1131 # 'c512b523472485aef4fff9e57b229d9d243c967f'
0edcb37d
JN
1132 elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1133 $res{'commit'} = $1;
1134 }
740e67f9
JN
1135
1136 return wantarray ? %res : \%res;
1137}
1138
cb849b46
JN
1139# parse line of git-ls-tree output
1140sub parse_ls_tree_line ($;%) {
1141 my $line = shift;
1142 my %opts = @_;
1143 my %res;
1144
1145 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1146 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1147
1148 $res{'mode'} = $1;
1149 $res{'type'} = $2;
1150 $res{'hash'} = $3;
1151 if ($opts{'-z'}) {
1152 $res{'name'} = $4;
1153 } else {
1154 $res{'name'} = unquote($4);
1155 }
1156
1157 return wantarray ? %res : \%res;
1158}
1159
717b8311
JN
1160## ......................................................................
1161## parse to array of hashes functions
4c02e3c5 1162
847e01fb 1163sub git_get_refs_list {
120ddde2
JN
1164 my $type = shift || "";
1165 my %refs;
717b8311 1166 my @reflist;
4c02e3c5 1167
717b8311 1168 my @refs;
c83a77e4
JN
1169 open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1170 or return;
1171 while (my $line = <$fd>) {
1172 chomp $line;
120ddde2
JN
1173 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?([^\^]+))(\^\{\})?$/) {
1174 if (defined $refs{$1}) {
1175 push @{$refs{$1}}, $2;
1176 } else {
1177 $refs{$1} = [ $2 ];
1178 }
1179
1180 if (! $4) { # unpeeled, direct reference
1181 push @refs, { hash => $1, name => $3 }; # without type
1182 } elsif ($3 eq $refs[-1]{'name'}) {
1183 # most likely a tag is followed by its peeled
1184 # (deref) one, and when that happens we know the
1185 # previous one was of type 'tag'.
1186 $refs[-1]{'type'} = "tag";
1187 }
717b8311 1188 }
c83a77e4
JN
1189 }
1190 close $fd;
1191
1192 foreach my $ref (@refs) {
1193 my $ref_file = $ref->{'name'};
1194 my $ref_id = $ref->{'hash'};
7a13b999 1195
c83a77e4 1196 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
a446d6bb 1197 my %ref_item = parse_ref($ref_file, $ref_id, $type);
991910a9 1198
717b8311
JN
1199 push @reflist, \%ref_item;
1200 }
a446d6bb 1201 # sort refs by age
717b8311 1202 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
120ddde2 1203 return (\@reflist, \%refs);
86eed32d
KS
1204}
1205
717b8311
JN
1206## ----------------------------------------------------------------------
1207## filesystem-related functions
022be3d0 1208
c07ad4b9
KS
1209sub get_file_owner {
1210 my $path = shift;
1211
1212 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1213 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1214 if (!defined $gcos) {
1215 return undef;
1216 }
1217 my $owner = $gcos;
1218 $owner =~ s/[,;].*$//;
281bf0cf 1219 return decode("utf8", $owner, Encode::FB_DEFAULT);
c07ad4b9
KS
1220}
1221
717b8311
JN
1222## ......................................................................
1223## mimetype related functions
09bd7898 1224
717b8311
JN
1225sub mimetype_guess_file {
1226 my $filename = shift;
1227 my $mimemap = shift;
1228 -r $mimemap or return undef;
1229
1230 my %mimemap;
1231 open(MIME, $mimemap) or return undef;
1232 while (<MIME>) {
618918e5 1233 next if m/^#/; # skip comments
717b8311 1234 my ($mime, $exts) = split(/\t+/);
46b059d7
JH
1235 if (defined $exts) {
1236 my @exts = split(/\s+/, $exts);
1237 foreach my $ext (@exts) {
1238 $mimemap{$ext} = $mime;
1239 }
09bd7898 1240 }
09bd7898 1241 }
717b8311 1242 close(MIME);
09bd7898 1243
8059319a 1244 $filename =~ /\.([^.]*)$/;
717b8311
JN
1245 return $mimemap{$1};
1246}
5996ca08 1247
717b8311
JN
1248sub mimetype_guess {
1249 my $filename = shift;
1250 my $mime;
1251 $filename =~ /\./ or return undef;
5996ca08 1252
717b8311
JN
1253 if ($mimetypes_file) {
1254 my $file = $mimetypes_file;
d5aa50de
JN
1255 if ($file !~ m!^/!) { # if it is relative path
1256 # it is relative to project
1257 $file = "$projectroot/$project/$file";
1258 }
717b8311
JN
1259 $mime = mimetype_guess_file($filename, $file);
1260 }
1261 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1262 return $mime;
5996ca08
FF
1263}
1264
847e01fb 1265sub blob_mimetype {
717b8311
JN
1266 my $fd = shift;
1267 my $filename = shift;
5996ca08 1268
717b8311
JN
1269 if ($filename) {
1270 my $mime = mimetype_guess($filename);
1271 $mime and return $mime;
d8d17b5d 1272 }
717b8311
JN
1273
1274 # just in case
1275 return $default_blob_plain_mimetype unless $fd;
1276
1277 if (-T $fd) {
1278 return 'text/plain' .
1279 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1280 } elsif (! $filename) {
1281 return 'application/octet-stream';
1282 } elsif ($filename =~ m/\.png$/i) {
1283 return 'image/png';
1284 } elsif ($filename =~ m/\.gif$/i) {
1285 return 'image/gif';
1286 } elsif ($filename =~ m/\.jpe?g$/i) {
1287 return 'image/jpeg';
d8d17b5d 1288 } else {
717b8311 1289 return 'application/octet-stream';
f7ab660c 1290 }
717b8311
JN
1291}
1292
1293## ======================================================================
1294## functions printing HTML: header, footer, error page
1295
1296sub git_header_html {
1297 my $status = shift || "200 OK";
1298 my $expires = shift;
1299
1300 my $title = "$site_name git";
1301 if (defined $project) {
1302 $title .= " - $project";
1303 if (defined $action) {
1304 $title .= "/$action";
1305 if (defined $file_name) {
a2f3db2f 1306 $title .= " - " . esc_html($file_name);
717b8311
JN
1307 if ($action eq "tree" && $file_name !~ m|/$|) {
1308 $title .= "/";
1309 }
1310 }
1311 }
f7ab660c 1312 }
717b8311
JN
1313 my $content_type;
1314 # require explicit support from the UA if we are to send the page as
1315 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1316 # we have to do this because MSIE sometimes globs '*/*', pretending to
1317 # support xhtml+xml but choking when it gets what it asked for.
952c65fc
JN
1318 if (defined $cgi->http('HTTP_ACCEPT') &&
1319 $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1320 $cgi->Accept('application/xhtml+xml') != 0) {
717b8311 1321 $content_type = 'application/xhtml+xml';
f7ab660c 1322 } else {
717b8311 1323 $content_type = 'text/html';
f7ab660c 1324 }
952c65fc
JN
1325 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1326 -status=> $status, -expires => $expires);
717b8311
JN
1327 print <<EOF;
1328<?xml version="1.0" encoding="utf-8"?>
1329<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1330<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
d4baf9ea 1331<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
717b8311
JN
1332<!-- git core binaries version $git_version -->
1333<head>
1334<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
d4baf9ea 1335<meta name="generator" content="gitweb/$version git/$git_version"/>
717b8311
JN
1336<meta name="robots" content="index, nofollow"/>
1337<title>$title</title>
1338<link rel="stylesheet" type="text/css" href="$stylesheet"/>
717b8311 1339EOF
dd04c428
ML
1340 if (defined $project) {
1341 printf('<link rel="alternate" title="%s log" '.
1342 'href="%s" type="application/rss+xml"/>'."\n",
1c2a4f5a 1343 esc_param($project), href(action=>"rss"));
9d0734ae
JN
1344 } else {
1345 printf('<link rel="alternate" title="%s projects list" '.
1346 'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1347 $site_name, href(project=>undef, action=>"project_index"));
1348 printf('<link rel="alternate" title="%s projects logs" '.
1349 'href="%s" type="text/x-opml"/>'."\n",
1350 $site_name, href(project=>undef, action=>"opml"));
dd04c428 1351 }
0b5deba1
JN
1352 if (defined $favicon) {
1353 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1354 }
10161355 1355
dd04c428
ML
1356 print "</head>\n" .
1357 "<body>\n" .
10161355 1358 "<div class=\"page_header\">\n" .
717b8311 1359 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
281f2f6b 1360 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
717b8311 1361 "</a>\n";
2de21fac 1362 print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
717b8311 1363 if (defined $project) {
1c2a4f5a 1364 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
717b8311
JN
1365 if (defined $action) {
1366 print " / $action";
1367 }
1368 print "\n";
1369 if (!defined $searchtext) {
1370 $searchtext = "";
1371 }
1372 my $search_hash;
1373 if (defined $hash_base) {
1374 $search_hash = $hash_base;
1375 } elsif (defined $hash) {
1376 $search_hash = $hash;
bddec01d 1377 } else {
717b8311 1378 $search_hash = "HEAD";
bddec01d 1379 }
717b8311
JN
1380 $cgi->param("a", "search");
1381 $cgi->param("h", $search_hash);
1382 print $cgi->startform(-method => "get", -action => $my_uri) .
1383 "<div class=\"search\">\n" .
1384 $cgi->hidden(-name => "p") . "\n" .
1385 $cgi->hidden(-name => "a") . "\n" .
1386 $cgi->hidden(-name => "h") . "\n" .
1387 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1388 "</div>" .
1389 $cgi->end_form() . "\n";
b87d78d6 1390 }
717b8311
JN
1391 print "</div>\n";
1392}
1393
1394sub git_footer_html {
1395 print "<div class=\"page_footer\">\n";
1396 if (defined $project) {
847e01fb 1397 my $descr = git_get_project_description($project);
717b8311
JN
1398 if (defined $descr) {
1399 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1400 }
a1565c44
JN
1401 print $cgi->a({-href => href(action=>"rss"),
1402 -class => "rss_logo"}, "RSS") . "\n";
717b8311 1403 } else {
a1565c44 1404 print $cgi->a({-href => href(project=>undef, action=>"opml"),
9d0734ae
JN
1405 -class => "rss_logo"}, "OPML") . " ";
1406 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1407 -class => "rss_logo"}, "TXT") . "\n";
717b8311
JN
1408 }
1409 print "</div>\n" .
1410 "</body>\n" .
1411 "</html>";
1412}
1413
1414sub die_error {
1415 my $status = shift || "403 Forbidden";
1416 my $error = shift || "Malformed query, file missing or permission denied";
1417
1418 git_header_html($status);
59b9f61a
JN
1419 print <<EOF;
1420<div class="page_body">
1421<br /><br />
1422$status - $error
1423<br />
1424</div>
1425EOF
b87d78d6 1426 git_footer_html();
717b8311 1427 exit;
161332a5
KS
1428}
1429
717b8311
JN
1430## ----------------------------------------------------------------------
1431## functions printing or outputting HTML: navigation
1432
847e01fb 1433sub git_print_page_nav {
717b8311
JN
1434 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1435 $extra = '' if !defined $extra; # pager or formats
1436
1437 my @navs = qw(summary shortlog log commit commitdiff tree);
1438 if ($suppress) {
1439 @navs = grep { $_ ne $suppress } @navs;
1440 }
1441
1c2a4f5a 1442 my %arg = map { $_ => {action=>$_} } @navs;
717b8311
JN
1443 if (defined $head) {
1444 for (qw(commit commitdiff)) {
1c2a4f5a 1445 $arg{$_}{hash} = $head;
717b8311
JN
1446 }
1447 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1448 for (qw(shortlog log)) {
1c2a4f5a 1449 $arg{$_}{hash} = $head;
045e531a 1450 }
6a928415
KS
1451 }
1452 }
1c2a4f5a
MW
1453 $arg{tree}{hash} = $treehead if defined $treehead;
1454 $arg{tree}{hash_base} = $treebase if defined $treebase;
717b8311
JN
1455
1456 print "<div class=\"page_nav\">\n" .
1457 (join " | ",
1c2a4f5a
MW
1458 map { $_ eq $current ?
1459 $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1460 } @navs);
717b8311
JN
1461 print "<br/>\n$extra<br/>\n" .
1462 "</div>\n";
6a928415
KS
1463}
1464
847e01fb 1465sub format_paging_nav {
717b8311
JN
1466 my ($action, $hash, $head, $page, $nrevs) = @_;
1467 my $paging_nav;
594e212b 1468
717b8311
JN
1469
1470 if ($hash ne $head || $page) {
1c2a4f5a 1471 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
594e212b 1472 } else {
717b8311
JN
1473 $paging_nav .= "HEAD";
1474 }
1475
1476 if ($page > 0) {
1477 $paging_nav .= " &sdot; " .
1c2a4f5a 1478 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
26298b5f 1479 -accesskey => "p", -title => "Alt-p"}, "prev");
717b8311
JN
1480 } else {
1481 $paging_nav .= " &sdot; prev";
1482 }
1483
1484 if ($nrevs >= (100 * ($page+1)-1)) {
1485 $paging_nav .= " &sdot; " .
1c2a4f5a 1486 $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
26298b5f 1487 -accesskey => "n", -title => "Alt-n"}, "next");
717b8311
JN
1488 } else {
1489 $paging_nav .= " &sdot; next";
594e212b 1490 }
717b8311
JN
1491
1492 return $paging_nav;
594e212b
JN
1493}
1494
717b8311
JN
1495## ......................................................................
1496## functions printing or outputting HTML: div
1497
847e01fb 1498sub git_print_header_div {
717b8311 1499 my ($action, $title, $hash, $hash_base) = @_;
1c2a4f5a 1500 my %args = ();
717b8311 1501
1c2a4f5a
MW
1502 $args{action} = $action;
1503 $args{hash} = $hash if $hash;
1504 $args{hash_base} = $hash_base if $hash_base;
717b8311
JN
1505
1506 print "<div class=\"header\">\n" .
1c2a4f5a
MW
1507 $cgi->a({-href => href(%args), -class => "title"},
1508 $title ? $title : $action) .
1509 "\n</div>\n";
717b8311 1510}
ede5e100 1511
6fd92a28
JN
1512#sub git_print_authorship (\%) {
1513sub git_print_authorship {
1514 my $co = shift;
1515
a44465cc 1516 my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
6fd92a28
JN
1517 print "<div class=\"author_date\">" .
1518 esc_html($co->{'author_name'}) .
a44465cc
JN
1519 " [$ad{'rfc2822'}";
1520 if ($ad{'hour_local'} < 6) {
1521 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1522 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1523 } else {
1524 printf(" (%02d:%02d %s)",
1525 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1526 }
1527 print "]</div>\n";
6fd92a28
JN
1528}
1529
717b8311
JN
1530sub git_print_page_path {
1531 my $name = shift;
1532 my $type = shift;
59fb1c94 1533 my $hb = shift;
ede5e100 1534
717b8311 1535 if (!defined $name) {
63e4220b 1536 print "<div class=\"page_path\">/</div>\n";
762c7205
JN
1537 } else {
1538 my @dirname = split '/', $name;
1539 my $basename = pop @dirname;
1540 my $fullname = '';
1541
63e4220b 1542 print "<div class=\"page_path\">";
16fdb488 1543 print $cgi->a({-href => href(action=>"tree", hash_base=>$hb),
26d0a976
PB
1544 -title => 'tree root'}, "[$project]");
1545 print " / ";
762c7205 1546 foreach my $dir (@dirname) {
16fdb488 1547 $fullname .= ($fullname ? '/' : '') . $dir;
762c7205
JN
1548 print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1549 hash_base=>$hb),
26d0a976
PB
1550 -title => $fullname}, esc_html($dir));
1551 print " / ";
762c7205
JN
1552 }
1553 if (defined $type && $type eq 'blob') {
952c65fc 1554 print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
762c7205
JN
1555 hash_base=>$hb),
1556 -title => $name}, esc_html($basename));
1557 } elsif (defined $type && $type eq 'tree') {
1558 print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1559 hash_base=>$hb),
26d0a976 1560 -title => $name}, esc_html($basename));
59fb1c94 1561 } else {
762c7205 1562 print esc_html($basename);
59fb1c94 1563 }
63e4220b 1564 print "<br/></div>\n";
ede5e100 1565 }
ede5e100
KS
1566}
1567
b7f9253d
JN
1568# sub git_print_log (\@;%) {
1569sub git_print_log ($;%) {
d16d093c 1570 my $log = shift;
b7f9253d 1571 my %opts = @_;
d16d093c 1572
b7f9253d
JN
1573 if ($opts{'-remove_title'}) {
1574 # remove title, i.e. first line of log
1575 shift @$log;
1576 }
d16d093c
JN
1577 # remove leading empty lines
1578 while (defined $log->[0] && $log->[0] eq "") {
1579 shift @$log;
1580 }
1581
1582 # print log
1583 my $signoff = 0;
1584 my $empty = 0;
1585 foreach my $line (@$log) {
b7f9253d
JN
1586 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1587 $signoff = 1;
fba20b42 1588 $empty = 0;
b7f9253d
JN
1589 if (! $opts{'-remove_signoff'}) {
1590 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1591 next;
1592 } else {
1593 # remove signoff lines
1594 next;
1595 }
1596 } else {
1597 $signoff = 0;
1598 }
1599
d16d093c
JN
1600 # print only one empty line
1601 # do not print empty line after signoff
1602 if ($line eq "") {
1603 next if ($empty || $signoff);
1604 $empty = 1;
1605 } else {
1606 $empty = 0;
1607 }
b7f9253d
JN
1608
1609 print format_log_line_html($line) . "<br/>\n";
1610 }
1611
1612 if ($opts{'-final_empty_line'}) {
1613 # end with single empty line
1614 print "<br/>\n" unless $empty;
d16d093c
JN
1615 }
1616}
1617
1618sub git_print_simplified_log {
1619 my $log = shift;
1620 my $remove_title = shift;
1621
b7f9253d
JN
1622 git_print_log($log,
1623 -final_empty_line=> 1,
b7f9253d 1624 -remove_title => $remove_title);
d16d093c
JN
1625}
1626
fa702003
JN
1627# print tree entry (row of git_tree), but without encompassing <tr> element
1628sub git_print_tree_entry {
1629 my ($t, $basedir, $hash_base, $have_blame) = @_;
1630
1631 my %base_key = ();
1632 $base_key{hash_base} = $hash_base if defined $hash_base;
1633
4de741b3
LT
1634 # The format of a table row is: mode list link. Where mode is
1635 # the mode of the entry, list is the name of the entry, an href,
1636 # and link is the action links of the entry.
1637
fa702003
JN
1638 print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1639 if ($t->{'type'} eq "blob") {
1640 print "<td class=\"list\">" .
4de741b3
LT
1641 $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1642 file_name=>"$basedir$t->{'name'}", %base_key),
1643 -class => "list"}, esc_html($t->{'name'})) . "</td>\n";
1644 print "<td class=\"link\">";
fa702003 1645 if ($have_blame) {
4de741b3
LT
1646 print $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1647 file_name=>"$basedir$t->{'name'}", %base_key)},
1648 "blame");
fa702003
JN
1649 }
1650 if (defined $hash_base) {
4de741b3
LT
1651 if ($have_blame) {
1652 print " | ";
1653 }
1654 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
fa702003
JN
1655 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1656 "history");
1657 }
1658 print " | " .
1659 $cgi->a({-href => href(action=>"blob_plain",
1660 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
4de741b3
LT
1661 "raw");
1662 print "</td>\n";
fa702003
JN
1663
1664 } elsif ($t->{'type'} eq "tree") {
0fa105e7
LT
1665 print "<td class=\"list\">";
1666 print $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
fa702003 1667 file_name=>"$basedir$t->{'name'}", %base_key)},
0fa105e7
LT
1668 esc_html($t->{'name'}));
1669 print "</td>\n";
1670 print "<td class=\"link\">";
fa702003 1671 if (defined $hash_base) {
0fa105e7 1672 print $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
fa702003
JN
1673 file_name=>"$basedir$t->{'name'}")},
1674 "history");
1675 }
1676 print "</td>\n";
1677 }
1678}
1679
717b8311
JN
1680## ......................................................................
1681## functions printing large fragments of HTML
1682
4a4a1a53 1683sub git_difftree_body {
eee08903 1684 my ($difftree, $hash, $parent) = @_;
4a4a1a53
JN
1685
1686 print "<div class=\"list_head\">\n";
1687 if ($#{$difftree} > 10) {
1688 print(($#{$difftree} + 1) . " files changed:\n");
1689 }
1690 print "</div>\n";
1691
1692 print "<table class=\"diff_tree\">\n";
1693 my $alternate = 0;
b4657e77 1694 my $patchno = 0;
4a4a1a53 1695 foreach my $line (@{$difftree}) {
e8e41a93 1696 my %diff = parse_difftree_raw_line($line);
4a4a1a53
JN
1697
1698 if ($alternate) {
1699 print "<tr class=\"dark\">\n";
1700 } else {
1701 print "<tr class=\"light\">\n";
1702 }
1703 $alternate ^= 1;
1704
e8e41a93
JN
1705 my ($to_mode_oct, $to_mode_str, $to_file_type);
1706 my ($from_mode_oct, $from_mode_str, $from_file_type);
1707 if ($diff{'to_mode'} ne ('0' x 6)) {
1708 $to_mode_oct = oct $diff{'to_mode'};
1709 if (S_ISREG($to_mode_oct)) { # only for regular file
1710 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1711 }
1712 $to_file_type = file_type($diff{'to_mode'});
1713 }
1714 if ($diff{'from_mode'} ne ('0' x 6)) {
1715 $from_mode_oct = oct $diff{'from_mode'};
1716 if (S_ISREG($to_mode_oct)) { # only for regular file
1717 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
4a4a1a53 1718 }
e8e41a93
JN
1719 $from_file_type = file_type($diff{'from_mode'});
1720 }
1721
1722 if ($diff{'status'} eq "A") { # created
1723 my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1724 $mode_chng .= " with mode: $to_mode_str" if $to_mode_str;
1725 $mode_chng .= "]</span>";
4a4a1a53 1726 print "<td>" .
e8e41a93
JN
1727 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1728 hash_base=>$hash, file_name=>$diff{'file'}),
1729 -class => "list"}, esc_html($diff{'file'})) .
4a4a1a53 1730 "</td>\n" .
e8e41a93 1731 "<td>$mode_chng</td>\n" .
4a4a1a53 1732 "<td class=\"link\">" .
e8e41a93
JN
1733 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1734 hash_base=>$hash, file_name=>$diff{'file'})},
b4657e77 1735 "blob");
72dbafa1 1736 if ($action eq 'commitdiff') {
b4657e77
JN
1737 # link to patch
1738 $patchno++;
1739 print " | " .
1740 $cgi->a({-href => "#patch$patchno"}, "patch");
1741 }
1742 print "</td>\n";
4a4a1a53 1743
e8e41a93
JN
1744 } elsif ($diff{'status'} eq "D") { # deleted
1745 my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
4a4a1a53 1746 print "<td>" .
e8e41a93
JN
1747 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1748 hash_base=>$parent, file_name=>$diff{'file'}),
1749 -class => "list"}, esc_html($diff{'file'})) .
1750 "</td>\n" .
1751 "<td>$mode_chng</td>\n" .
4a4a1a53 1752 "<td class=\"link\">" .
e8e41a93
JN
1753 $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1754 hash_base=>$parent, file_name=>$diff{'file'})},
1755 "blob") .
b4657e77 1756 " | ";
72dbafa1 1757 if ($action eq 'commitdiff') {
b4657e77
JN
1758 # link to patch
1759 $patchno++;
1760 print " | " .
1761 $cgi->a({-href => "#patch$patchno"}, "patch");
1762 }
1763 print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
09052554 1764 file_name=>$diff{'file'})},
e8e41a93
JN
1765 "history") .
1766 "</td>\n";
4a4a1a53 1767
e8e41a93 1768 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
4a4a1a53 1769 my $mode_chnge = "";
e8e41a93
JN
1770 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1771 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1772 if ($from_file_type != $to_file_type) {
1773 $mode_chnge .= " from $from_file_type to $to_file_type";
4a4a1a53 1774 }
e8e41a93
JN
1775 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1776 if ($from_mode_str && $to_mode_str) {
1777 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1778 } elsif ($to_mode_str) {
1779 $mode_chnge .= " mode: $to_mode_str";
4a4a1a53
JN
1780 }
1781 }
1782 $mode_chnge .= "]</span>\n";
1783 }
1784 print "<td>";
e8e41a93 1785 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
420e92f2
JN
1786 print $cgi->a({-href => href(action=>"blobdiff",
1787 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1788 hash_base=>$hash, hash_parent_base=>$parent,
1789 file_name=>$diff{'file'}),
e8e41a93
JN
1790 -class => "list"}, esc_html($diff{'file'}));
1791 } else { # only mode changed
1792 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1793 hash_base=>$hash, file_name=>$diff{'file'}),
1794 -class => "list"}, esc_html($diff{'file'}));
4a4a1a53
JN
1795 }
1796 print "</td>\n" .
1797 "<td>$mode_chnge</td>\n" .
1798 "<td class=\"link\">" .
b4657e77
JN
1799 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1800 hash_base=>$hash, file_name=>$diff{'file'})},
1801 "blob");
e8e41a93 1802 if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
72dbafa1 1803 if ($action eq 'commitdiff') {
b4657e77
JN
1804 # link to patch
1805 $patchno++;
1806 print " | " .
1807 $cgi->a({-href => "#patch$patchno"}, "patch");
1808 } else {
1809 print " | " .
1810 $cgi->a({-href => href(action=>"blobdiff",
1811 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1812 hash_base=>$hash, hash_parent_base=>$parent,
1813 file_name=>$diff{'file'})},
1814 "diff");
1815 }
4a4a1a53 1816 }
e8e41a93
JN
1817 print " | " .
1818 $cgi->a({-href => href(action=>"history",
1819 hash_base=>$hash, file_name=>$diff{'file'})},
1820 "history");
4a4a1a53
JN
1821 print "</td>\n";
1822
e8e41a93
JN
1823 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1824 my %status_name = ('R' => 'moved', 'C' => 'copied');
1825 my $nstatus = $status_name{$diff{'status'}};
4a4a1a53 1826 my $mode_chng = "";
e8e41a93
JN
1827 if ($diff{'from_mode'} != $diff{'to_mode'}) {
1828 # mode also for directories, so we cannot use $to_mode_str
1829 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
4a4a1a53
JN
1830 }
1831 print "<td>" .
e8e41a93
JN
1832 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1833 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1834 -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1835 "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1836 $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1837 hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1838 -class => "list"}, esc_html($diff{'from_file'})) .
1839 " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
4a4a1a53 1840 "<td class=\"link\">" .
e8e41a93
JN
1841 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1842 hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1843 "blob");
1844 if ($diff{'to_id'} ne $diff{'from_id'}) {
72dbafa1 1845 if ($action eq 'commitdiff') {
b4657e77
JN
1846 # link to patch
1847 $patchno++;
1848 print " | " .
1849 $cgi->a({-href => "#patch$patchno"}, "patch");
1850 } else {
1851 print " | " .
1852 $cgi->a({-href => href(action=>"blobdiff",
1853 hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1854 hash_base=>$hash, hash_parent_base=>$parent,
1855 file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1856 "diff");
1857 }
4a4a1a53
JN
1858 }
1859 print "</td>\n";
e8e41a93 1860
4a4a1a53
JN
1861 } # we should not encounter Unmerged (U) or Unknown (X) status
1862 print "</tr>\n";
1863 }
1864 print "</table>\n";
1865}
1866
eee08903 1867sub git_patchset_body {
157e43b4 1868 my ($fd, $difftree, $hash, $hash_parent) = @_;
eee08903
JN
1869
1870 my $patch_idx = 0;
1871 my $in_header = 0;
1872 my $patch_found = 0;
fe87585e 1873 my $diffinfo;
eee08903
JN
1874
1875 print "<div class=\"patchset\">\n";
1876
157e43b4 1877 LINE:
c8a99d76 1878 while (my $patch_line = <$fd>) {
157e43b4 1879 chomp $patch_line;
eee08903
JN
1880
1881 if ($patch_line =~ m/^diff /) { # "git diff" header
1882 # beginning of patch (in patchset)
1883 if ($patch_found) {
1884 # close previous patch
1885 print "</div>\n"; # class="patch"
1886 } else {
1887 # first patch in patchset
1888 $patch_found = 1;
1889 }
b4657e77 1890 print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
eee08903 1891
fe87585e
JN
1892 if (ref($difftree->[$patch_idx]) eq "HASH") {
1893 $diffinfo = $difftree->[$patch_idx];
1894 } else {
1895 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1896 }
1897 $patch_idx++;
eee08903
JN
1898
1899 # for now, no extended header, hence we skip empty patches
1900 # companion to next LINE if $in_header;
fe87585e 1901 if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
eee08903
JN
1902 $in_header = 1;
1903 next LINE;
1904 }
1905
fe87585e
JN
1906 if ($diffinfo->{'status'} eq "A") { # added
1907 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
eee08903 1908 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
fe87585e
JN
1909 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1910 $diffinfo->{'to_id'}) . "(new)" .
eee08903
JN
1911 "</div>\n"; # class="diff_info"
1912
fe87585e
JN
1913 } elsif ($diffinfo->{'status'} eq "D") { # deleted
1914 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
eee08903 1915 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
fe87585e
JN
1916 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1917 $diffinfo->{'from_id'}) . "(deleted)" .
eee08903
JN
1918 "</div>\n"; # class="diff_info"
1919
fe87585e 1920 } elsif ($diffinfo->{'status'} eq "R" || # renamed
7c5e2ebb
JN
1921 $diffinfo->{'status'} eq "C" || # copied
1922 $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
eee08903 1923 print "<div class=\"diff_info\">" .
fe87585e 1924 file_type($diffinfo->{'from_mode'}) . ":" .
eee08903 1925 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
fe87585e
JN
1926 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1927 $diffinfo->{'from_id'}) .
eee08903 1928 " -> " .
fe87585e 1929 file_type($diffinfo->{'to_mode'}) . ":" .
eee08903 1930 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
fe87585e
JN
1931 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1932 $diffinfo->{'to_id'});
eee08903
JN
1933 print "</div>\n"; # class="diff_info"
1934
1935 } else { # modified, mode changed, ...
1936 print "<div class=\"diff_info\">" .
fe87585e 1937 file_type($diffinfo->{'from_mode'}) . ":" .
eee08903 1938 $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
fe87585e
JN
1939 hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1940 $diffinfo->{'from_id'}) .
eee08903 1941 " -> " .
fe87585e 1942 file_type($diffinfo->{'to_mode'}) . ":" .
eee08903 1943 $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
fe87585e
JN
1944 hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1945 $diffinfo->{'to_id'});
eee08903
JN
1946 print "</div>\n"; # class="diff_info"
1947 }
1948
1949 #print "<div class=\"diff extended_header\">\n";
1950 $in_header = 1;
1951 next LINE;
1952 } # start of patch in patchset
1953
1954
1955 if ($in_header && $patch_line =~ m/^---/) {
e4e4f825 1956 #print "</div>\n"; # class="diff extended_header"
eee08903 1957 $in_header = 0;
e4e4f825
JN
1958
1959 my $file = $diffinfo->{'from_file'};
1960 $file ||= $diffinfo->{'file'};
ef10ee87
JN
1961 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1962 hash=>$diffinfo->{'from_id'}, file_name=>$file),
1963 -class => "list"}, esc_html($file));
1964 $patch_line =~ s|a/.*$|a/$file|g;
1965 print "<div class=\"diff from_file\">$patch_line</div>\n";
e4e4f825
JN
1966
1967 $patch_line = <$fd>;
1968 chomp $patch_line;
1969
1970 #$patch_line =~ m/^+++/;
1971 $file = $diffinfo->{'to_file'};
1972 $file ||= $diffinfo->{'file'};
ef10ee87
JN
1973 $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1974 hash=>$diffinfo->{'to_id'}, file_name=>$file),
1975 -class => "list"}, esc_html($file));
1976 $patch_line =~ s|b/.*|b/$file|g;
1977 print "<div class=\"diff to_file\">$patch_line</div>\n";
e4e4f825
JN
1978
1979 next LINE;
eee08903
JN
1980 }
1981 next LINE if $in_header;
1982
1983 print format_diff_line($patch_line);
1984 }
1985 print "</div>\n" if $patch_found; # class="patch"
1986
1987 print "</div>\n"; # class="patchset"
1988}
1989
1990# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1991
9f5dcb81
JN
1992sub git_shortlog_body {
1993 # uses global variable $project
1994 my ($revlist, $from, $to, $refs, $extra) = @_;
ddb8d900 1995
9f5dcb81
JN
1996 $from = 0 unless defined $from;
1997 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1998
1999 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
2000 my $alternate = 0;
2001 for (my $i = $from; $i <= $to; $i++) {
2002 my $commit = $revlist->[$i];
847e01fb
JN
2003 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
2004 my $ref = format_ref_marker($refs, $commit);
2005 my %co = parse_commit($commit);
9f5dcb81
JN
2006 if ($alternate) {
2007 print "<tr class=\"dark\">\n";
2008 } else {
2009 print "<tr class=\"light\">\n";
2010 }
2011 $alternate ^= 1;
2012 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
2013 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2014 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2015 "<td>";
952c65fc
JN
2016 print format_subject_html($co{'title'}, $co{'title_short'},
2017 href(action=>"commit", hash=>$commit), $ref);
9f5dcb81
JN
2018 print "</td>\n" .
2019 "<td class=\"link\">" .
1c2a4f5a 2020 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
35749ae5
PB
2021 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2022 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
cb9c6e5b 2023 print "</td>\n" .
9f5dcb81
JN
2024 "</tr>\n";
2025 }
2026 if (defined $extra) {
2027 print "<tr>\n" .
2028 "<td colspan=\"4\">$extra</td>\n" .
2029 "</tr>\n";
2030 }
2031 print "</table>\n";
2032}
2033
581860e1
JN
2034sub git_history_body {
2035 # Warning: assumes constant type (blob or tree) during history
8be68352
JN
2036 my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
2037
2038 $from = 0 unless defined $from;
2039 $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
581860e1
JN
2040
2041 print "<table class=\"history\" cellspacing=\"0\">\n";
2042 my $alternate = 0;
8be68352
JN
2043 for (my $i = $from; $i <= $to; $i++) {
2044 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
581860e1
JN
2045 next;
2046 }
2047
2048 my $commit = $1;
2049 my %co = parse_commit($commit);
2050 if (!%co) {
2051 next;
2052 }
2053
2054 my $ref = format_ref_marker($refs, $commit);
2055
2056 if ($alternate) {
2057 print "<tr class=\"dark\">\n";
2058 } else {
2059 print "<tr class=\"light\">\n";
2060 }
2061 $alternate ^= 1;
2062 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2063 # shortlog uses chop_str($co{'author_name'}, 10)
2064 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2065 "<td>";
2066 # originally git_history used chop_str($co{'title'}, 50)
952c65fc
JN
2067 print format_subject_html($co{'title'}, $co{'title_short'},
2068 href(action=>"commit", hash=>$commit), $ref);
581860e1
JN
2069 print "</td>\n" .
2070 "<td class=\"link\">" .
1c2a4f5a
MW
2071 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
2072 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
2073 $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
581860e1
JN
2074
2075 if ($ftype eq 'blob') {
2076 my $blob_current = git_get_hash_by_path($hash_base, $file_name);
2077 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2078 if (defined $blob_current && defined $blob_parent &&
2079 $blob_current ne $blob_parent) {
2080 print " | " .
420e92f2
JN
2081 $cgi->a({-href => href(action=>"blobdiff",
2082 hash=>$blob_current, hash_parent=>$blob_parent,
2083 hash_base=>$hash_base, hash_parent_base=>$commit,
2084 file_name=>$file_name)},
581860e1
JN
2085 "diff to current");
2086 }
2087 }
2088 print "</td>\n" .
2089 "</tr>\n";
2090 }
2091 if (defined $extra) {
2092 print "<tr>\n" .
2093 "<td colspan=\"4\">$extra</td>\n" .
2094 "</tr>\n";
2095 }
2096 print "</table>\n";
2097}
2098
717b8311
JN
2099sub git_tags_body {
2100 # uses global variable $project
2101 my ($taglist, $from, $to, $extra) = @_;
2102 $from = 0 unless defined $from;
2103 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2104
2105 print "<table class=\"tags\" cellspacing=\"0\">\n";
2106 my $alternate = 0;
2107 for (my $i = $from; $i <= $to; $i++) {
2108 my $entry = $taglist->[$i];
2109 my %tag = %$entry;
2110 my $comment_lines = $tag{'comment'};
2111 my $comment = shift @$comment_lines;
2112 my $comment_short;
2113 if (defined $comment) {
2114 $comment_short = chop_str($comment, 30, 5);
2115 }
2116 if ($alternate) {
2117 print "<tr class=\"dark\">\n";
2118 } else {
2119 print "<tr class=\"light\">\n";
2120 }
2121 $alternate ^= 1;
2122 print "<td><i>$tag{'age'}</i></td>\n" .
2123 "<td>" .
1c2a4f5a 2124 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
63e4220b 2125 -class => "list name"}, esc_html($tag{'name'})) .
717b8311
JN
2126 "</td>\n" .
2127 "<td>";
2128 if (defined $comment) {
952c65fc
JN
2129 print format_subject_html($comment, $comment_short,
2130 href(action=>"tag", hash=>$tag{'id'}));
717b8311
JN
2131 }
2132 print "</td>\n" .
2133 "<td class=\"selflink\">";
2134 if ($tag{'type'} eq "tag") {
1c2a4f5a 2135 print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
717b8311
JN
2136 } else {
2137 print "&nbsp;";
2138 }
2139 print "</td>\n" .
2140 "<td class=\"link\">" . " | " .
1c2a4f5a 2141 $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
717b8311 2142 if ($tag{'reftype'} eq "commit") {
1c2a4f5a
MW
2143 print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2144 " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
717b8311 2145 } elsif ($tag{'reftype'} eq "blob") {
1c2a4f5a 2146 print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
717b8311
JN
2147 }
2148 print "</td>\n" .
2149 "</tr>";
2150 }
2151 if (defined $extra) {
2152 print "<tr>\n" .
2153 "<td colspan=\"5\">$extra</td>\n" .
2154 "</tr>\n";
2155 }
2156 print "</table>\n";
2157}
2158
2159sub git_heads_body {
2160 # uses global variable $project
120ddde2 2161 my ($headlist, $head, $from, $to, $extra) = @_;
717b8311 2162 $from = 0 unless defined $from;
120ddde2 2163 $to = $#{$headlist} if (!defined $to || $#{$headlist} < $to);
717b8311
JN
2164
2165 print "<table class=\"heads\" cellspacing=\"0\">\n";
2166 my $alternate = 0;
2167 for (my $i = $from; $i <= $to; $i++) {
120ddde2 2168 my $entry = $headlist->[$i];
717b8311
JN
2169 my %tag = %$entry;
2170 my $curr = $tag{'id'} eq $head;
2171 if ($alternate) {
2172 print "<tr class=\"dark\">\n";
2173 } else {
2174 print "<tr class=\"light\">\n";
2175 }
2176 $alternate ^= 1;
2177 print "<td><i>$tag{'age'}</i></td>\n" .
2178 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1c2a4f5a 2179 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
63e4220b 2180 -class => "list name"},esc_html($tag{'name'})) .
717b8311
JN
2181 "</td>\n" .
2182 "<td class=\"link\">" .
1c2a4f5a 2183 $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1d62be25
PB
2184 $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") . " | " .
2185 $cgi->a({-href => href(action=>"tree", hash=>$tag{'name'}, hash_base=>$tag{'name'})}, "tree") .
717b8311
JN
2186 "</td>\n" .
2187 "</tr>";
2188 }
2189 if (defined $extra) {
2190 print "<tr>\n" .
2191 "<td colspan=\"3\">$extra</td>\n" .
2192 "</tr>\n";
2193 }
2194 print "</table>\n";
2195}
2196
717b8311
JN
2197## ======================================================================
2198## ======================================================================
2199## actions
2200
717b8311 2201sub git_project_list {
6326b60c
JN
2202 my $order = $cgi->param('o');
2203 if (defined $order && $order !~ m/project|descr|owner|age/) {
e2860ead 2204 die_error(undef, "Unknown order parameter");
6326b60c
JN
2205 }
2206
847e01fb 2207 my @list = git_get_projects_list();
717b8311
JN
2208 my @projects;
2209 if (!@list) {
cac4bd94 2210 die_error(undef, "No projects found");
717b8311
JN
2211 }
2212 foreach my $pr (@list) {
847e01fb 2213 my $head = git_get_head_hash($pr->{'path'});
717b8311
JN
2214 if (!defined $head) {
2215 next;
9f5dcb81 2216 }
25691fbe 2217 $git_dir = "$projectroot/$pr->{'path'}";
847e01fb 2218 my %co = parse_commit($head);
717b8311
JN
2219 if (!%co) {
2220 next;
9f5dcb81 2221 }
717b8311
JN
2222 $pr->{'commit'} = \%co;
2223 if (!defined $pr->{'descr'}) {
847e01fb 2224 my $descr = git_get_project_description($pr->{'path'}) || "";
717b8311 2225 $pr->{'descr'} = chop_str($descr, 25, 5);
9f5dcb81 2226 }
717b8311
JN
2227 if (!defined $pr->{'owner'}) {
2228 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
9f5dcb81 2229 }
717b8311 2230 push @projects, $pr;
9f5dcb81 2231 }
6326b60c 2232
717b8311
JN
2233 git_header_html();
2234 if (-f $home_text) {
2235 print "<div class=\"index_include\">\n";
2236 open (my $fd, $home_text);
2237 print <$fd>;
2238 close $fd;
2239 print "</div>\n";
9f5dcb81 2240 }
717b8311
JN
2241 print "<table class=\"project_list\">\n" .
2242 "<tr>\n";
6326b60c
JN
2243 $order ||= "project";
2244 if ($order eq "project") {
717b8311
JN
2245 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2246 print "<th>Project</th>\n";
2247 } else {
6326b60c 2248 print "<th>" .
a1565c44 2249 $cgi->a({-href => href(project=>undef, order=>'project'),
6326b60c
JN
2250 -class => "header"}, "Project") .
2251 "</th>\n";
717b8311 2252 }
6326b60c 2253 if ($order eq "descr") {
717b8311
JN
2254 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2255 print "<th>Description</th>\n";
2256 } else {
6326b60c 2257 print "<th>" .
a1565c44 2258 $cgi->a({-href => href(project=>undef, order=>'descr'),
6326b60c
JN
2259 -class => "header"}, "Description") .
2260 "</th>\n";
717b8311 2261 }
6326b60c 2262 if ($order eq "owner") {
717b8311
JN
2263 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2264 print "<th>Owner</th>\n";
2265 } else {
6326b60c 2266 print "<th>" .
a1565c44 2267 $cgi->a({-href => href(project=>undef, order=>'owner'),
6326b60c
JN
2268 -class => "header"}, "Owner") .
2269 "</th>\n";
717b8311 2270 }
6326b60c 2271 if ($order eq "age") {
717b8311
JN
2272 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2273 print "<th>Last Change</th>\n";
2274 } else {
6326b60c 2275 print "<th>" .
a1565c44 2276 $cgi->a({-href => href(project=>undef, order=>'age'),
6326b60c
JN
2277 -class => "header"}, "Last Change") .
2278 "</th>\n";
717b8311
JN
2279 }
2280 print "<th></th>\n" .
2281 "</tr>\n";
9f5dcb81 2282 my $alternate = 0;
717b8311 2283 foreach my $pr (@projects) {
9f5dcb81
JN
2284 if ($alternate) {
2285 print "<tr class=\"dark\">\n";
2286 } else {
2287 print "<tr class=\"light\">\n";
2288 }
2289 $alternate ^= 1;
756d2f06 2290 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
6326b60c 2291 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
717b8311
JN
2292 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2293 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
6326b60c
JN
2294 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2295 $pr->{'commit'}{'age_string'} . "</td>\n" .
9f5dcb81 2296 "<td class=\"link\">" .
756d2f06
MW
2297 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary") . " | " .
2298 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
609ff267
PB
2299 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") . " | " .
2300 $cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")}, "tree") .
9f5dcb81 2301 "</td>\n" .
9f5dcb81
JN
2302 "</tr>\n";
2303 }
2304 print "</table>\n";
717b8311 2305 git_footer_html();
9f5dcb81
JN
2306}
2307
fc2b2be0
JN
2308sub git_project_index {
2309 my @projects = git_get_projects_list();
2310
2311 print $cgi->header(
2312 -type => 'text/plain',
2313 -charset => 'utf-8',
2314 -content_disposition => qq(inline; filename="index.aux"));
2315
2316 foreach my $pr (@projects) {
2317 if (!exists $pr->{'owner'}) {
2318 $pr->{'owner'} = get_file_owner("$projectroot/$project");
2319 }
2320
2321 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2322 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2323 $path =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2324 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2325 $path =~ s/ /\+/g;
2326 $owner =~ s/ /\+/g;
2327
2328 print "$path $owner\n";
2329 }
2330}
2331
ede5e100 2332sub git_summary {
847e01fb
JN
2333 my $descr = git_get_project_description($project) || "none";
2334 my $head = git_get_head_hash($project);
2335 my %co = parse_commit($head);
2336 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
ede5e100 2337
1e0cf030 2338 my $owner = git_get_project_owner($project);
ede5e100 2339
120ddde2
JN
2340 my ($reflist, $refs) = git_get_refs_list();
2341
2342 my @taglist;
2343 my @headlist;
2344 foreach my $ref (@$reflist) {
2345 if ($ref->{'name'} =~ s!^heads/!!) {
2346 push @headlist, $ref;
2347 } else {
2348 $ref->{'name'} =~ s!^tags/!!;
2349 push @taglist, $ref;
2350 }
2351 }
2352
ede5e100 2353 git_header_html();
847e01fb 2354 git_print_page_nav('summary','', $head);
9f5dcb81 2355
19806691 2356 print "<div class=\"title\">&nbsp;</div>\n";
bddec01d 2357 print "<table cellspacing=\"0\">\n" .
40c13813 2358 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
ede5e100 2359 "<tr><td>owner</td><td>$owner</td></tr>\n" .
19a8721e 2360 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
e79ca7cc
JN
2361 # use per project git URL list in $projectroot/$project/cloneurl
2362 # or make project git URL from git base URL and project name
19a8721e 2363 my $url_tag = "URL";
e79ca7cc
JN
2364 my @url_list = git_get_project_url_list($project);
2365 @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2366 foreach my $git_url (@url_list) {
2367 next unless $git_url;
2368 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
19a8721e
JN
2369 $url_tag = "";
2370 }
2371 print "</table>\n";
9f5dcb81 2372
25691fbe
DS
2373 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2374 git_get_head_hash($project)
cac4bd94 2375 or die_error(undef, "Open git-rev-list failed");
0881d2d1 2376 my @revlist = map { chomp; $_ } <$fd>;
ede5e100 2377 close $fd;
847e01fb 2378 git_print_header_div('shortlog');
9f5dcb81 2379 git_shortlog_body(\@revlist, 0, 15, $refs,
1c2a4f5a 2380 $cgi->a({-href => href(action=>"shortlog")}, "..."));
ede5e100 2381
120ddde2 2382 if (@taglist) {
847e01fb 2383 git_print_header_div('tags');
120ddde2 2384 git_tags_body(\@taglist, 0, 15,
1c2a4f5a 2385 $cgi->a({-href => href(action=>"tags")}, "..."));
ede5e100 2386 }
0db37973 2387
120ddde2 2388 if (@headlist) {
847e01fb 2389 git_print_header_div('heads');
120ddde2 2390 git_heads_body(\@headlist, $head, 0, 15,
1c2a4f5a 2391 $cgi->a({-href => href(action=>"heads")}, "..."));
0db37973 2392 }
9f5dcb81 2393
ede5e100
KS
2394 git_footer_html();
2395}
2396
d8a20ba9 2397sub git_tag {
847e01fb 2398 my $head = git_get_head_hash($project);
d8a20ba9 2399 git_header_html();
847e01fb
JN
2400 git_print_page_nav('','', $head,undef,$head);
2401 my %tag = parse_tag($hash);
2402 git_print_header_div('commit', esc_html($tag{'name'}), $hash);
d8a20ba9
KS
2403 print "<div class=\"title_text\">\n" .
2404 "<table cellspacing=\"0\">\n" .
e4669df9
KS
2405 "<tr>\n" .
2406 "<td>object</td>\n" .
952c65fc
JN
2407 "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2408 $tag{'object'}) . "</td>\n" .
2409 "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2410 $tag{'type'}) . "</td>\n" .
e4669df9 2411 "</tr>\n";
d8a20ba9 2412 if (defined($tag{'author'})) {
847e01fb 2413 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
40c13813 2414 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
952c65fc
JN
2415 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2416 sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2417 "</td></tr>\n";
d8a20ba9
KS
2418 }
2419 print "</table>\n\n" .
2420 "</div>\n";
2421 print "<div class=\"page_body\">";
2422 my $comment = $tag{'comment'};
2423 foreach my $line (@$comment) {
40c13813 2424 print esc_html($line) . "<br/>\n";
d8a20ba9
KS
2425 }
2426 print "</div>\n";
2427 git_footer_html();
2428}
2429
1f2857ea
LT
2430sub git_blame2 {
2431 my $fd;
2432 my $ftype;
ddb8d900 2433
1d3fc68a
AK
2434 my ($have_blame) = gitweb_check_feature('blame');
2435 if (!$have_blame) {
ddb8d900
AK
2436 die_error('403 Permission denied', "Permission denied");
2437 }
1f2857ea 2438 die_error('404 Not Found', "File name not defined") if (!$file_name);
847e01fb 2439 $hash_base ||= git_get_head_hash($project);
cac4bd94 2440 die_error(undef, "Couldn't find base commit") unless ($hash_base);
847e01fb 2441 my %co = parse_commit($hash_base)
1f2857ea
LT
2442 or die_error(undef, "Reading commit failed");
2443 if (!defined $hash) {
2444 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2445 or die_error(undef, "Error looking up file");
2446 }
2447 $ftype = git_get_type($hash);
2448 if ($ftype !~ "blob") {
e484a2d6 2449 die_error("400 Bad Request", "Object is not a blob");
1f2857ea 2450 }
a2f3db2f 2451 open ($fd, "-|", git_cmd(), "blame", '-l', '--', $file_name, $hash_base)
cac4bd94 2452 or die_error(undef, "Open git-blame failed");
1f2857ea 2453 git_header_html();
0d83ddc4 2454 my $formats_nav =
952c65fc
JN
2455 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2456 "blob") .
2457 " | " .
cae1862a
PB
2458 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2459 "history") .
2460 " | " .
952c65fc 2461 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
f35274da 2462 "HEAD");
847e01fb
JN
2463 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2464 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
59fb1c94 2465 git_print_page_path($file_name, $ftype, $hash_base);
82f930de 2466 my @rev_color = (qw(light2 dark2));
cc1bf97e
LT
2467 my $num_colors = scalar(@rev_color);
2468 my $current_color = 0;
2469 my $last_rev;
59b9f61a
JN
2470 print <<HTML;
2471<div class="page_body">
2472<table class="blame">
65910395 2473<tr><th>Prev</th><th>Diff</th><th>Commit</th><th>Line</th><th>Data</th></tr>
59b9f61a 2474HTML
acb0f6f3
LT
2475 while (<$fd>) {
2476 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2477 my $full_rev = $1;
1f2857ea 2478 my $rev = substr($full_rev, 0, 8);
acb0f6f3
LT
2479 my $lineno = $2;
2480 my $data = $3;
65910395
LT
2481 my %pco = parse_commit($full_rev);
2482 my $parent = $pco{'parent'};
1f2857ea 2483
cc1bf97e
LT
2484 if (!defined $last_rev) {
2485 $last_rev = $full_rev;
2486 } elsif ($last_rev ne $full_rev) {
2487 $last_rev = $full_rev;
2488 $current_color = ++$current_color % $num_colors;
2489 }
2490 print "<tr class=\"$rev_color[$current_color]\">\n";
65910395
LT
2491 # Print the Prev link
2492 print "<td class=\"sha1\">";
2493 print $cgi->a({-href => href(action=>"blame", hash_base=>$parent, file_name=>$file_name)},
2494 esc_html(substr($parent, 0, 8)));
2495 print "</td>\n";
2496 # Print the Diff (blobdiff) link
2497 print "<td>";
2498 print $cgi->a({-href => href(action=>"blobdiff", file_name=>$file_name, hash_parent_base=>$parent,
2499 hash_base=>$full_rev)},
2500 esc_html("Diff"));
2501 print "</td>\n";
2502 # Print the Commit link
1f2857ea 2503 print "<td class=\"sha1\">" .
952c65fc
JN
2504 $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2505 esc_html($rev)) . "</td>\n";
65910395 2506 # Print the Line number
952c65fc
JN
2507 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2508 esc_html($lineno) . "</a></td>\n";
65910395 2509 # Print the Data
1f2857ea
LT
2510 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2511 print "</tr>\n";
2512 }
2513 print "</table>\n";
2514 print "</div>";
952c65fc
JN
2515 close $fd
2516 or print "Reading blob failed\n";
1f2857ea
LT
2517 git_footer_html();
2518}
2519
e34ef621
FF
2520sub git_blame {
2521 my $fd;
ddb8d900 2522
1d3fc68a
AK
2523 my ($have_blame) = gitweb_check_feature('blame');
2524 if (!$have_blame) {
ddb8d900
AK
2525 die_error('403 Permission denied', "Permission denied");
2526 }
cac4bd94 2527 die_error('404 Not Found', "File name not defined") if (!$file_name);
847e01fb 2528 $hash_base ||= git_get_head_hash($project);
cac4bd94 2529 die_error(undef, "Couldn't find base commit") unless ($hash_base);
847e01fb 2530 my %co = parse_commit($hash_base)
cac4bd94 2531 or die_error(undef, "Reading commit failed");
e34ef621
FF
2532 if (!defined $hash) {
2533 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
cac4bd94 2534 or die_error(undef, "Error lookup file");
e34ef621 2535 }
25691fbe 2536 open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
cac4bd94 2537 or die_error(undef, "Open git-annotate failed");
e34ef621 2538 git_header_html();
0d83ddc4 2539 my $formats_nav =
952c65fc
JN
2540 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2541 "blob") .
2542 " | " .
cae1862a
PB
2543 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2544 "history") .
2545 " | " .
952c65fc 2546 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
f35274da 2547 "HEAD");
847e01fb
JN
2548 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2549 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
59fb1c94 2550 git_print_page_path($file_name, 'blob', $hash_base);
e34ef621
FF
2551 print "<div class=\"page_body\">\n";
2552 print <<HTML;
1f1ab5f0 2553<table class="blame">
e34ef621
FF
2554 <tr>
2555 <th>Commit</th>
2556 <th>Age</th>
2557 <th>Author</th>
2558 <th>Line</th>
2559 <th>Data</th>
2560 </tr>
2561HTML
2562 my @line_class = (qw(light dark));
2563 my $line_class_len = scalar (@line_class);
2564 my $line_class_num = $#line_class;
2565 while (my $line = <$fd>) {
2566 my $long_rev;
2567 my $short_rev;
2568 my $author;
2569 my $time;
2570 my $lineno;
2571 my $data;
2572 my $age;
2573 my $age_str;
1f1ab5f0 2574 my $age_class;
e34ef621
FF
2575
2576 chomp $line;
2577 $line_class_num = ($line_class_num + 1) % $line_class_len;
2578
030b5208 2579 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
e34ef621
FF
2580 $long_rev = $1;
2581 $author = $2;
2582 $time = $3;
2583 $lineno = $4;
2584 $data = $5;
2585 } else {
1f1ab5f0 2586 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
e34ef621
FF
2587 next;
2588 }
2589 $short_rev = substr ($long_rev, 0, 8);
2590 $age = time () - $time;
2591 $age_str = age_string ($age);
2592 $age_str =~ s/ /&nbsp;/g;
1f1ab5f0 2593 $age_class = age_class($age);
e34ef621
FF
2594 $author = esc_html ($author);
2595 $author =~ s/ /&nbsp;/g;
f16db173
JN
2596
2597 $data = untabify($data);
717b8311 2598 $data = esc_html ($data);
2d007374 2599
717b8311
JN
2600 print <<HTML;
2601 <tr class="$line_class[$line_class_num]">
1c2a4f5a 2602 <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
717b8311
JN
2603 <td class="$age_class">$age_str</td>
2604 <td>$author</td>
2605 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2606 <td class="pre">$data</td>
2607 </tr>
2608HTML
2609 } # while (my $line = <$fd>)
2610 print "</table>\n\n";
952c65fc
JN
2611 close $fd
2612 or print "Reading blob failed.\n";
717b8311
JN
2613 print "</div>";
2614 git_footer_html();
2d007374
PB
2615}
2616
717b8311 2617sub git_tags {
847e01fb 2618 my $head = git_get_head_hash($project);
717b8311 2619 git_header_html();
847e01fb
JN
2620 git_print_page_nav('','', $head,undef,$head);
2621 git_print_header_div('summary', $project);
2d007374 2622
120ddde2 2623 my ($taglist) = git_get_refs_list("tags");
62e27f27 2624 if (@$taglist) {
717b8311 2625 git_tags_body($taglist);
2d007374 2626 }
717b8311 2627 git_footer_html();
2d007374
PB
2628}
2629
717b8311 2630sub git_heads {
847e01fb 2631 my $head = git_get_head_hash($project);
717b8311 2632 git_header_html();
847e01fb
JN
2633 git_print_page_nav('','', $head,undef,$head);
2634 git_print_header_div('summary', $project);
930cf7dd 2635
120ddde2 2636 my ($headlist) = git_get_refs_list("heads");
62e27f27 2637 if (@$headlist) {
120ddde2 2638 git_heads_body($headlist, $head);
f5aa79d9 2639 }
717b8311 2640 git_footer_html();
f5aa79d9
JN
2641}
2642
19806691 2643sub git_blob_plain {
f2e73302 2644 my $expires;
f2e73302 2645
cff0771b 2646 if (!defined $hash) {
5be01bc8 2647 if (defined $file_name) {
847e01fb 2648 my $base = $hash_base || git_get_head_hash($project);
5be01bc8 2649 $hash = git_get_hash_by_path($base, $file_name, "blob")
cac4bd94 2650 or die_error(undef, "Error lookup file");
5be01bc8 2651 } else {
cac4bd94 2652 die_error(undef, "No file name defined");
5be01bc8 2653 }
800764cf
MW
2654 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2655 # blobs defined by non-textual hash id's can be cached
2656 $expires = "+1d";
5be01bc8 2657 }
800764cf 2658
930cf7dd 2659 my $type = shift;
25691fbe 2660 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
cfd82669 2661 or die_error(undef, "Couldn't cat $file_name, $hash");
930cf7dd 2662
847e01fb 2663 $type ||= blob_mimetype($fd, $file_name);
f5aa79d9
JN
2664
2665 # save as filename, even when no $file_name is given
2666 my $save_as = "$hash";
9312944d
KS
2667 if (defined $file_name) {
2668 $save_as = $file_name;
f5aa79d9
JN
2669 } elsif ($type =~ m/^text\//) {
2670 $save_as .= '.txt';
9312944d 2671 }
f5aa79d9 2672
f2e73302
JN
2673 print $cgi->header(
2674 -type => "$type",
2675 -expires=>$expires,
2676 -content_disposition => "inline; filename=\"$save_as\"");
19806691 2677 undef $/;
ad14e931 2678 binmode STDOUT, ':raw';
19806691 2679 print <$fd>;
ad14e931 2680 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
19806691
KS
2681 $/ = "\n";
2682 close $fd;
2683}
2684
930cf7dd 2685sub git_blob {
f2e73302 2686 my $expires;
f2e73302 2687
cff0771b 2688 if (!defined $hash) {
5be01bc8 2689 if (defined $file_name) {
847e01fb 2690 my $base = $hash_base || git_get_head_hash($project);
5be01bc8 2691 $hash = git_get_hash_by_path($base, $file_name, "blob")
cac4bd94 2692 or die_error(undef, "Error lookup file");
5be01bc8 2693 } else {
cac4bd94 2694 die_error(undef, "No file name defined");
5be01bc8 2695 }
800764cf
MW
2696 } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2697 # blobs defined by non-textual hash id's can be cached
2698 $expires = "+1d";
5be01bc8 2699 }
800764cf 2700
1d3fc68a 2701 my ($have_blame) = gitweb_check_feature('blame');
25691fbe 2702 open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
cac4bd94 2703 or die_error(undef, "Couldn't cat $file_name, $hash");
847e01fb 2704 my $mimetype = blob_mimetype($fd, $file_name);
930cf7dd
LT
2705 if ($mimetype !~ m/^text\//) {
2706 close $fd;
2707 return git_blob_plain($mimetype);
2708 }
f2e73302 2709 git_header_html(undef, $expires);
0d83ddc4 2710 my $formats_nav = '';
847e01fb 2711 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
930cf7dd
LT
2712 if (defined $file_name) {
2713 if ($have_blame) {
952c65fc
JN
2714 $formats_nav .=
2715 $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2716 hash=>$hash, file_name=>$file_name)},
2717 "blame") .
2718 " | ";
930cf7dd 2719 }
0d83ddc4 2720 $formats_nav .=
cae1862a
PB
2721 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2722 hash=>$hash, file_name=>$file_name)},
2723 "history") .
2724 " | " .
952c65fc
JN
2725 $cgi->a({-href => href(action=>"blob_plain",
2726 hash=>$hash, file_name=>$file_name)},
35329cc1 2727 "raw") .
952c65fc
JN
2728 " | " .
2729 $cgi->a({-href => href(action=>"blob",
2730 hash_base=>"HEAD", file_name=>$file_name)},
f35274da 2731 "HEAD");
930cf7dd 2732 } else {
952c65fc 2733 $formats_nav .=
35329cc1 2734 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
930cf7dd 2735 }
847e01fb
JN
2736 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2737 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
930cf7dd
LT
2738 } else {
2739 print "<div class=\"page_nav\">\n" .
2740 "<br/><br/></div>\n" .
2741 "<div class=\"title\">$hash</div>\n";
2742 }
59fb1c94 2743 git_print_page_path($file_name, "blob", $hash_base);
930cf7dd
LT
2744 print "<div class=\"page_body\">\n";
2745 my $nr;
2746 while (my $line = <$fd>) {
2747 chomp $line;
2748 $nr++;
f16db173 2749 $line = untabify($line);
952c65fc
JN
2750 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2751 $nr, $nr, $nr, esc_html($line);
930cf7dd 2752 }
952c65fc
JN
2753 close $fd
2754 or print "Reading blob failed.\n";
930cf7dd
LT
2755 print "</div>";
2756 git_footer_html();
2757}
2758
09bd7898 2759sub git_tree {
cae1862a
PB
2760 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2761 my $have_snapshot = (defined $ctype && defined $suffix);
2762
b87d78d6 2763 if (!defined $hash) {
847e01fb 2764 $hash = git_get_head_hash($project);
09bd7898 2765 if (defined $file_name) {
df2c37a5 2766 my $base = $hash_base || $hash;
09bd7898
KS
2767 $hash = git_get_hash_by_path($base, $file_name, "tree");
2768 }
10dba28d 2769 if (!defined $hash_base) {
df2c37a5 2770 $hash_base = $hash;
10dba28d 2771 }
e925f38c 2772 }
232ff553 2773 $/ = "\0";
25691fbe 2774 open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
cac4bd94 2775 or die_error(undef, "Open git-ls-tree failed");
0881d2d1 2776 my @entries = map { chomp; $_ } <$fd>;
cac4bd94 2777 close $fd or die_error(undef, "Reading tree failed");
232ff553 2778 $/ = "\n";
d63577da 2779
847e01fb
JN
2780 my $refs = git_get_references();
2781 my $ref = format_ref_marker($refs, $hash_base);
12a88f2f 2782 git_header_html();
09bd7898 2783 my $base = "";
1d3fc68a 2784 my ($have_blame) = gitweb_check_feature('blame');
847e01fb 2785 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
cae1862a
PB
2786 my @views_nav = ();
2787 if (defined $file_name) {
2788 push @views_nav,
2789 $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2790 hash=>$hash, file_name=>$file_name)},
2791 "history"),
2792 $cgi->a({-href => href(action=>"tree",
2793 hash_base=>"HEAD", file_name=>$file_name)},
f35274da 2794 "HEAD"),
cae1862a
PB
2795 }
2796 if ($have_snapshot) {
2797 # FIXME: Should be available when we have no hash base as well.
2798 push @views_nav,
5c7d2cf3 2799 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
cae1862a
PB
2800 "snapshot");
2801 }
2802 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
847e01fb 2803 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
d63577da 2804 } else {
fa702003 2805 undef $hash_base;
d63577da
KS
2806 print "<div class=\"page_nav\">\n";
2807 print "<br/><br/></div>\n";
2808 print "<div class=\"title\">$hash</div>\n";
2809 }
09bd7898 2810 if (defined $file_name) {
232ff553 2811 $base = esc_html("$file_name/");
09bd7898 2812 }
59fb1c94 2813 git_print_page_path($file_name, 'tree', $hash_base);
fbb592a9 2814 print "<div class=\"page_body\">\n";
42f7eb94 2815 print "<table cellspacing=\"0\">\n";
bddec01d 2816 my $alternate = 0;
161332a5 2817 foreach my $line (@entries) {
cb849b46
JN
2818 my %t = parse_ls_tree_line($line, -z => 1);
2819
bddec01d 2820 if ($alternate) {
c994d620 2821 print "<tr class=\"dark\">\n";
bddec01d 2822 } else {
c994d620 2823 print "<tr class=\"light\">\n";
bddec01d
KS
2824 }
2825 $alternate ^= 1;
cb849b46 2826
fa702003
JN
2827 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2828
42f7eb94 2829 print "</tr>\n";
161332a5 2830 }
42f7eb94
KS
2831 print "</table>\n" .
2832 "</div>";
12a88f2f 2833 git_footer_html();
09bd7898
KS
2834}
2835
cb9c6e5b
AK
2836sub git_snapshot {
2837
ddb8d900
AK
2838 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2839 my $have_snapshot = (defined $ctype && defined $suffix);
2840 if (!$have_snapshot) {
2841 die_error('403 Permission denied', "Permission denied");
2842 }
2843
cb9c6e5b
AK
2844 if (!defined $hash) {
2845 $hash = git_get_head_hash($project);
2846 }
2847
ddb8d900 2848 my $filename = basename($project) . "-$hash.tar.$suffix";
cb9c6e5b
AK
2849
2850 print $cgi->header(-type => 'application/x-tar',
134a6941
JN
2851 -content_encoding => $ctype,
2852 -content_disposition => "inline; filename=\"$filename\"",
952c65fc 2853 -status => '200 OK');
cb9c6e5b 2854
25691fbe
DS
2855 my $git_command = git_cmd_str();
2856 open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
ddb8d900 2857 die_error(undef, "Execute git-tar-tree failed.");
cb9c6e5b
AK
2858 binmode STDOUT, ':raw';
2859 print <$fd>;
2860 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2861 close $fd;
2862
cb9c6e5b
AK
2863}
2864
09bd7898 2865sub git_log {
847e01fb 2866 my $head = git_get_head_hash($project);
0db37973 2867 if (!defined $hash) {
19806691 2868 $hash = $head;
0db37973 2869 }
ea4a6df4
KS
2870 if (!defined $page) {
2871 $page = 0;
b87d78d6 2872 }
847e01fb 2873 my $refs = git_get_references();
ea4a6df4
KS
2874
2875 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
25691fbe 2876 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
cac4bd94 2877 or die_error(undef, "Open git-rev-list failed");
0881d2d1 2878 my @revlist = map { chomp; $_ } <$fd>;
ea4a6df4
KS
2879 close $fd;
2880
847e01fb 2881 my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
0d83ddc4
JN
2882
2883 git_header_html();
847e01fb 2884 git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
0d83ddc4 2885
b87d78d6 2886 if (!@revlist) {
847e01fb 2887 my %co = parse_commit($hash);
27fb8c40 2888
847e01fb 2889 git_print_header_div('summary', $project);
e925f38c 2890 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
161332a5 2891 }
c994d620
KS
2892 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2893 my $commit = $revlist[$i];
847e01fb
JN
2894 my $ref = format_ref_marker($refs, $commit);
2895 my %co = parse_commit($commit);
b87d78d6 2896 next if !%co;
847e01fb
JN
2897 my %ad = parse_date($co{'author_epoch'});
2898 git_print_header_div('commit',
26298b5f
JN
2899 "<span class=\"age\">$co{'age_string'}</span>" .
2900 esc_html($co{'title'}) . $ref,
2901 $commit);
034df39e
KS
2902 print "<div class=\"title_text\">\n" .
2903 "<div class=\"log_link\">\n" .
1c2a4f5a 2904 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
952c65fc
JN
2905 " | " .
2906 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
6ef4cb2e 2907 " | " .
d7267207 2908 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
eb28240b 2909 "<br/>\n" .
034df39e 2910 "</div>\n" .
40c13813 2911 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
d16d093c
JN
2912 "</div>\n";
2913
2914 print "<div class=\"log_body\">\n";
2915 git_print_simplified_log($co{'comment'});
09bd7898 2916 print "</div>\n";
e334d18c 2917 }
034df39e 2918 git_footer_html();
09bd7898
KS
2919}
2920
2921sub git_commit {
847e01fb 2922 my %co = parse_commit($hash);
034df39e 2923 if (!%co) {
cac4bd94 2924 die_error(undef, "Unknown commit object");
d63577da 2925 }
847e01fb
JN
2926 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2927 my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
161332a5 2928
d8a20ba9
KS
2929 my $parent = $co{'parent'};
2930 if (!defined $parent) {
b9182987 2931 $parent = "--root";
6191f8e1 2932 }
25691fbe 2933 open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
cac4bd94 2934 or die_error(undef, "Open git-diff-tree failed");
0881d2d1 2935 my @difftree = map { chomp; $_ } <$fd>;
cac4bd94 2936 close $fd or die_error(undef, "Reading git-diff-tree failed");
11044297
KS
2937
2938 # non-textual hash id's can be cached
2939 my $expires;
2940 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2941 $expires = "+1d";
2942 }
847e01fb
JN
2943 my $refs = git_get_references();
2944 my $ref = format_ref_marker($refs, $co{'id'});
ddb8d900
AK
2945
2946 my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2947 my $have_snapshot = (defined $ctype && defined $suffix);
2948
cae1862a 2949 my @views_nav = ();
4f7b34c9
LT
2950 if (defined $file_name && defined $co{'parent'}) {
2951 my $parent = $co{'parent'};
cae1862a 2952 push @views_nav,
952c65fc
JN
2953 $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2954 "blame");
4f7b34c9 2955 }
cae1862a
PB
2956 if (defined $co{'parent'}) {
2957 push @views_nav,
2958 $cgi->a({-href => href(action=>"shortlog", hash=>$hash)}, "shortlog"),
2959 $cgi->a({-href => href(action=>"log", hash=>$hash)}, "log");
2960 }
594e212b 2961 git_header_html(undef, $expires);
847e01fb 2962 git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
952c65fc 2963 $hash, $co{'tree'}, $hash,
cae1862a 2964 join (' | ', @views_nav));
4f7b34c9 2965
b87d78d6 2966 if (defined $co{'parent'}) {
847e01fb 2967 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
b87d78d6 2968 } else {
847e01fb 2969 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
b87d78d6 2970 }
6191f8e1 2971 print "<div class=\"title_text\">\n" .
b87d78d6 2972 "<table cellspacing=\"0\">\n";
40c13813 2973 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
bddec01d
KS
2974 "<tr>" .
2975 "<td></td><td> $ad{'rfc2822'}";
927dcec4 2976 if ($ad{'hour_local'} < 6) {
952c65fc
JN
2977 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2978 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
b87d78d6 2979 } else {
952c65fc
JN
2980 printf(" (%02d:%02d %s)",
2981 $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
b87d78d6 2982 }
bddec01d
KS
2983 print "</td>" .
2984 "</tr>\n";
40c13813 2985 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
952c65fc
JN
2986 print "<tr><td></td><td> $cd{'rfc2822'}" .
2987 sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2988 "</td></tr>\n";
1f1ab5f0 2989 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
bddec01d
KS
2990 print "<tr>" .
2991 "<td>tree</td>" .
1f1ab5f0 2992 "<td class=\"sha1\">" .
952c65fc
JN
2993 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2994 class => "list"}, $co{'tree'}) .
19806691 2995 "</td>" .
952c65fc
JN
2996 "<td class=\"link\">" .
2997 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2998 "tree");
cb9c6e5b 2999 if ($have_snapshot) {
952c65fc
JN
3000 print " | " .
3001 $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
cb9c6e5b
AK
3002 }
3003 print "</td>" .
bddec01d 3004 "</tr>\n";
8adc4bd4 3005 my $parents = $co{'parents'};
3e029299 3006 foreach my $par (@$parents) {
bddec01d
KS
3007 print "<tr>" .
3008 "<td>parent</td>" .
952c65fc
JN
3009 "<td class=\"sha1\">" .
3010 $cgi->a({-href => href(action=>"commit", hash=>$par),
3011 class => "list"}, $par) .
3012 "</td>" .
bddec01d 3013 "<td class=\"link\">" .
1c2a4f5a 3014 $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
952c65fc 3015 " | " .
f2e60947 3016 $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
bddec01d
KS
3017 "</td>" .
3018 "</tr>\n";
3e029299 3019 }
7a9b4c5f 3020 print "</table>".
b87d78d6 3021 "</div>\n";
d16d093c 3022
fbb592a9 3023 print "<div class=\"page_body\">\n";
d16d093c 3024 git_print_log($co{'comment'});
927dcec4 3025 print "</div>\n";
4a4a1a53 3026
eee08903 3027 git_difftree_body(\@difftree, $hash, $parent);
4a4a1a53 3028
12a88f2f 3029 git_footer_html();
09bd7898
KS
3030}
3031
3032sub git_blobdiff {
9b71b1f6
JN
3033 my $format = shift || 'html';
3034
7c5e2ebb
JN
3035 my $fd;
3036 my @difftree;
3037 my %diffinfo;
9b71b1f6 3038 my $expires;
7c5e2ebb
JN
3039
3040 # preparing $fd and %diffinfo for git_patchset_body
3041 # new style URI
3042 if (defined $hash_base && defined $hash_parent_base) {
3043 if (defined $file_name) {
3044 # read raw output
25691fbe 3045 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
7c5e2ebb
JN
3046 "--", $file_name
3047 or die_error(undef, "Open git-diff-tree failed");
3048 @difftree = map { chomp; $_ } <$fd>;
3049 close $fd
3050 or die_error(undef, "Reading git-diff-tree failed");
3051 @difftree
3052 or die_error('404 Not Found', "Blob diff not found");
3053
0aea3376
JN
3054 } elsif (defined $hash &&
3055 $hash =~ /[0-9a-fA-F]{40}/) {
3056 # try to find filename from $hash
7c5e2ebb
JN
3057
3058 # read filtered raw output
25691fbe 3059 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
7c5e2ebb
JN
3060 or die_error(undef, "Open git-diff-tree failed");
3061 @difftree =
3062 # ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'
3063 # $hash == to_id
3064 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
3065 map { chomp; $_ } <$fd>;
3066 close $fd
3067 or die_error(undef, "Reading git-diff-tree failed");
3068 @difftree
3069 or die_error('404 Not Found', "Blob diff not found");
3070
3071 } else {
3072 die_error('404 Not Found', "Missing one of the blob diff parameters");
3073 }
3074
3075 if (@difftree > 1) {
3076 die_error('404 Not Found', "Ambiguous blob diff specification");
3077 }
3078
3079 %diffinfo = parse_difftree_raw_line($difftree[0]);
3080 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
3081 $file_name ||= $diffinfo{'to_file'} || $diffinfo{'file'};
3082
3083 $hash_parent ||= $diffinfo{'from_id'};
3084 $hash ||= $diffinfo{'to_id'};
3085
9b71b1f6
JN
3086 # non-textual hash id's can be cached
3087 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
3088 $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
3089 $expires = '+1d';
3090 }
3091
7c5e2ebb 3092 # open patch output
25691fbe 3093 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6bcf4b46 3094 '-p', $hash_parent_base, $hash_base,
7c5e2ebb
JN
3095 "--", $file_name
3096 or die_error(undef, "Open git-diff-tree failed");
3097 }
3098
3099 # old/legacy style URI
3100 if (!%diffinfo && # if new style URI failed
3101 defined $hash && defined $hash_parent) {
3102 # fake git-diff-tree raw output
3103 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
3104 $diffinfo{'from_id'} = $hash_parent;
3105 $diffinfo{'to_id'} = $hash;
3106 if (defined $file_name) {
3107 if (defined $file_parent) {
3108 $diffinfo{'status'} = '2';
8391548e
PB
3109 $diffinfo{'from_file'} = $file_parent;
3110 $diffinfo{'to_file'} = $file_name;
7c5e2ebb
JN
3111 } else { # assume not renamed
3112 $diffinfo{'status'} = '1';
8391548e
PB
3113 $diffinfo{'from_file'} = $file_name;
3114 $diffinfo{'to_file'} = $file_name;
7c5e2ebb
JN
3115 }
3116 } else { # no filename given
3117 $diffinfo{'status'} = '2';
3118 $diffinfo{'from_file'} = $hash_parent;
3119 $diffinfo{'to_file'} = $hash;
3120 }
3121
9b71b1f6
JN
3122 # non-textual hash id's can be cached
3123 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
3124 $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
3125 $expires = '+1d';
3126 }
3127
3128 # open patch output
25691fbe 3129 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
7c5e2ebb
JN
3130 or die_error(undef, "Open git-diff failed");
3131 } else {
3132 die_error('404 Not Found', "Missing one of the blob diff parameters")
3133 unless %diffinfo;
3134 }
3135
3136 # header
9b71b1f6
JN
3137 if ($format eq 'html') {
3138 my $formats_nav =
3139 $cgi->a({-href => href(action=>"blobdiff_plain",
3140 hash=>$hash, hash_parent=>$hash_parent,
3141 hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
3142 file_name=>$file_name, file_parent=>$file_parent)},
35329cc1 3143 "raw");
9b71b1f6
JN
3144 git_header_html(undef, $expires);
3145 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3146 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3147 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3148 } else {
3149 print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3150 print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3151 }
3152 if (defined $file_name) {
3153 git_print_page_path($file_name, "blob", $hash_base);
3154 } else {
3155 print "<div class=\"page_path\"></div>\n";
3156 }
3157
3158 } elsif ($format eq 'plain') {
3159 print $cgi->header(
3160 -type => 'text/plain',
3161 -charset => 'utf-8',
3162 -expires => $expires,
a2f3db2f 3163 -content_disposition => qq(inline; filename=") . quotemeta($file_name) . qq(.patch"));
9b71b1f6
JN
3164
3165 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3166
7c5e2ebb 3167 } else {
9b71b1f6 3168 die_error(undef, "Unknown blobdiff format");
7c5e2ebb
JN
3169 }
3170
3171 # patch
9b71b1f6
JN
3172 if ($format eq 'html') {
3173 print "<div class=\"page_body\">\n";
7c5e2ebb 3174
9b71b1f6
JN
3175 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3176 close $fd;
7c5e2ebb 3177
9b71b1f6
JN
3178 print "</div>\n"; # class="page_body"
3179 git_footer_html();
3180
3181 } else {
3182 while (my $line = <$fd>) {
8391548e
PB
3183 $line =~ s!a/($hash|$hash_parent)!'a/'.esc_html($diffinfo{'from_file'})!eg;
3184 $line =~ s!b/($hash|$hash_parent)!'b/'.esc_html($diffinfo{'to_file'})!eg;
9b71b1f6
JN
3185
3186 print $line;
3187
3188 last if $line =~ m!^\+\+\+!;
3189 }
3190 local $/ = undef;
3191 print <$fd>;
3192 close $fd;
3193 }
09bd7898
KS
3194}
3195
19806691 3196sub git_blobdiff_plain {
9b71b1f6 3197 git_blobdiff('plain');
19806691
KS
3198}
3199
09bd7898 3200sub git_commitdiff {
eee08903 3201 my $format = shift || 'html';
847e01fb 3202 my %co = parse_commit($hash);
034df39e 3203 if (!%co) {
cac4bd94 3204 die_error(undef, "Unknown commit object");
d63577da 3205 }
bddec01d 3206 if (!defined $hash_parent) {
b5ff2cf9 3207 $hash_parent = $co{'parent'} || '--root';
bddec01d 3208 }
eee08903
JN
3209
3210 # read commitdiff
3211 my $fd;
3212 my @difftree;
eee08903 3213 if ($format eq 'html') {
25691fbe 3214 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
eee08903
JN
3215 "--patch-with-raw", "--full-index", $hash_parent, $hash
3216 or die_error(undef, "Open git-diff-tree failed");
3217
3218 while (chomp(my $line = <$fd>)) {
3219 # empty line ends raw part of diff-tree output
3220 last unless $line;
3221 push @difftree, $line;
3222 }
eee08903 3223
eee08903 3224 } elsif ($format eq 'plain') {
25691fbe 3225 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6bcf4b46 3226 '-p', $hash_parent, $hash
eee08903 3227 or die_error(undef, "Open git-diff-tree failed");
157e43b4 3228
eee08903
JN
3229 } else {
3230 die_error(undef, "Unknown commitdiff format");
3231 }
161332a5 3232
11044297
KS
3233 # non-textual hash id's can be cached
3234 my $expires;
3235 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3236 $expires = "+1d";
3237 }
09bd7898 3238
eee08903
JN
3239 # write commit message
3240 if ($format eq 'html') {
3241 my $refs = git_get_references();
3242 my $ref = format_ref_marker($refs, $co{'id'});
3243 my $formats_nav =
3244 $cgi->a({-href => href(action=>"commitdiff_plain",
3245 hash=>$hash, hash_parent=>$hash_parent)},
35329cc1 3246 "raw");
1b1cd421 3247
eee08903
JN
3248 git_header_html(undef, $expires);
3249 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3250 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
6fd92a28 3251 git_print_authorship(\%co);
eee08903
JN
3252 print "<div class=\"page_body\">\n";
3253 print "<div class=\"log\">\n";
3254 git_print_simplified_log($co{'comment'}, 1); # skip title
3255 print "</div>\n"; # class="log"
3256
3257 } elsif ($format eq 'plain') {
3258 my $refs = git_get_references("tags");
edf735ab 3259 my $tagname = git_get_rev_name_tags($hash);
eee08903
JN
3260 my $filename = basename($project) . "-$hash.patch";
3261
3262 print $cgi->header(
3263 -type => 'text/plain',
3264 -charset => 'utf-8',
3265 -expires => $expires,
3266 -content_disposition => qq(inline; filename="$filename"));
3267 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3268 print <<TEXT;
59b9f61a
JN
3269From: $co{'author'}
3270Date: $ad{'rfc2822'} ($ad{'tz_local'})
3271Subject: $co{'title'}
3272TEXT
edf735ab 3273 print "X-Git-Tag: $tagname\n" if $tagname;
eee08903 3274 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
edf735ab 3275
eee08903
JN
3276 foreach my $line (@{$co{'comment'}}) {
3277 print "$line\n";
3278 }
3279 print "---\n\n";
1b1cd421 3280 }
1b1cd421 3281
eee08903
JN
3282 # write patch
3283 if ($format eq 'html') {
b4657e77
JN
3284 git_difftree_body(\@difftree, $hash, $hash_parent);
3285 print "<br/>\n";
1b1cd421 3286
157e43b4
JN
3287 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3288 close $fd;
eee08903
JN
3289 print "</div>\n"; # class="page_body"
3290 git_footer_html();
3291
3292 } elsif ($format eq 'plain') {
3293 local $/ = undef;
3294 print <$fd>;
3295 close $fd
3296 or print "Reading git-diff-tree failed\n";
19806691
KS
3297 }
3298}
3299
eee08903
JN
3300sub git_commitdiff_plain {
3301 git_commitdiff('plain');
3302}
3303
09bd7898 3304sub git_history {
c6e1d9ed 3305 if (!defined $hash_base) {
847e01fb 3306 $hash_base = git_get_head_hash($project);
09bd7898 3307 }
8be68352
JN
3308 if (!defined $page) {
3309 $page = 0;
3310 }
63433106 3311 my $ftype;
847e01fb 3312 my %co = parse_commit($hash_base);
09bd7898 3313 if (!%co) {
cac4bd94 3314 die_error(undef, "Unknown commit object");
2ae100df 3315 }
8be68352 3316
847e01fb 3317 my $refs = git_get_references();
8be68352
JN
3318 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3319
93d5f061
LT
3320 if (!defined $hash && defined $file_name) {
3321 $hash = git_get_hash_by_path($hash_base, $file_name);
3322 }
cff0771b 3323 if (defined $hash) {
63433106 3324 $ftype = git_get_type($hash);
cff0771b 3325 }
10dba28d 3326
cdd4037d 3327 open my $fd, "-|",
8be68352
JN
3328 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3329 or die_error(undef, "Open git-rev-list-failed");
3330 my @revlist = map { chomp; $_ } <$fd>;
3331 close $fd
3332 or die_error(undef, "Reading git-rev-list failed");
25691fbe 3333
8be68352
JN
3334 my $paging_nav = '';
3335 if ($page > 0) {
3336 $paging_nav .=
3337 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3338 file_name=>$file_name)},
3339 "first");
3340 $paging_nav .= " &sdot; " .
3341 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3342 file_name=>$file_name, page=>$page-1),
3343 -accesskey => "p", -title => "Alt-p"}, "prev");
3344 } else {
3345 $paging_nav .= "first";
3346 $paging_nav .= " &sdot; prev";
3347 }
3348 if ($#revlist >= (100 * ($page+1)-1)) {
3349 $paging_nav .= " &sdot; " .
3350 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3351 file_name=>$file_name, page=>$page+1),
3352 -accesskey => "n", -title => "Alt-n"}, "next");
3353 } else {
3354 $paging_nav .= " &sdot; next";
3355 }
3356 my $next_link = '';
3357 if ($#revlist >= (100 * ($page+1)-1)) {
3358 $next_link =
3359 $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3360 file_name=>$file_name, page=>$page+1),
3361 -title => "Alt-n"}, "next");
3362 }
3363
3364 git_header_html();
3365 git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3366 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3367 git_print_page_path($file_name, $ftype, $hash_base);
3368
3369 git_history_body(\@revlist, ($page * 100), $#revlist,
3370 $refs, $hash_base, $ftype, $next_link);
581860e1 3371
d51e902a 3372 git_footer_html();
161332a5 3373}
19806691
KS
3374
3375sub git_search {
3376 if (!defined $searchtext) {
cac4bd94 3377 die_error(undef, "Text field empty");
19806691
KS
3378 }
3379 if (!defined $hash) {
847e01fb 3380 $hash = git_get_head_hash($project);
19806691 3381 }
847e01fb 3382 my %co = parse_commit($hash);
19806691 3383 if (!%co) {
cac4bd94 3384 die_error(undef, "Unknown commit object");
19806691 3385 }
04f7a94f 3386
c994d620
KS
3387 my $commit_search = 1;
3388 my $author_search = 0;
3389 my $committer_search = 0;
3390 my $pickaxe_search = 0;
3391 if ($searchtext =~ s/^author\\://i) {
3392 $author_search = 1;
3393 } elsif ($searchtext =~ s/^committer\\://i) {
3394 $committer_search = 1;
3395 } elsif ($searchtext =~ s/^pickaxe\\://i) {
3396 $commit_search = 0;
3397 $pickaxe_search = 1;
04f7a94f
JN
3398
3399 # pickaxe may take all resources of your box and run for several minutes
3400 # with every query - so decide by yourself how public you make this feature
3401 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3402 if (!$have_pickaxe) {
3403 die_error('403 Permission denied', "Permission denied");
3404 }
c994d620 3405 }
19806691 3406 git_header_html();
847e01fb
JN
3407 git_print_page_nav('','', $hash,$co{'tree'},$hash);
3408 git_print_header_div('commit', esc_html($co{'title'}), $hash);
19806691 3409
19806691 3410 print "<table cellspacing=\"0\">\n";
19806691 3411 my $alternate = 0;
c994d620
KS
3412 if ($commit_search) {
3413 $/ = "\0";
25691fbe 3414 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
c994d620
KS
3415 while (my $commit_text = <$fd>) {
3416 if (!grep m/$searchtext/i, $commit_text) {
3417 next;
19806691 3418 }
c994d620
KS
3419 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3420 next;
19806691 3421 }
c994d620 3422 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
19806691
KS
3423 next;
3424 }
c994d620 3425 my @commit_lines = split "\n", $commit_text;
847e01fb 3426 my %co = parse_commit(undef, \@commit_lines);
c994d620
KS
3427 if (!%co) {
3428 next;
3429 }
3430 if ($alternate) {
3431 print "<tr class=\"dark\">\n";
3432 } else {
3433 print "<tr class=\"light\">\n";
3434 }
3435 $alternate ^= 1;
71be1e79 3436 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
40c13813 3437 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
c994d620 3438 "<td>" .
63e4220b
JN
3439 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3440 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
c994d620
KS
3441 my $comment = $co{'comment'};
3442 foreach my $line (@$comment) {
3443 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
40c13813 3444 my $lead = esc_html($1) || "";
c994d620 3445 $lead = chop_str($lead, 30, 10);
40c13813
KS
3446 my $match = esc_html($2) || "";
3447 my $trail = esc_html($3) || "";
c994d620 3448 $trail = chop_str($trail, 30, 10);
1f1ab5f0 3449 my $text = "$lead<span class=\"match\">$match</span>$trail";
c994d620 3450 print chop_str($text, 80, 5) . "<br/>\n";
19806691 3451 }
c994d620
KS
3452 }
3453 print "</td>\n" .
3454 "<td class=\"link\">" .
756d2f06 3455 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
952c65fc
JN
3456 " | " .
3457 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
c994d620
KS
3458 print "</td>\n" .
3459 "</tr>\n";
3460 }
3461 close $fd;
3462 }
3463
3464 if ($pickaxe_search) {
3465 $/ = "\n";
25691fbe
DS
3466 my $git_command = git_cmd_str();
3467 open my $fd, "-|", "$git_command rev-list $hash | " .
3468 "$git_command diff-tree -r --stdin -S\'$searchtext\'";
c994d620
KS
3469 undef %co;
3470 my @files;
3471 while (my $line = <$fd>) {
3472 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3473 my %set;
3474 $set{'file'} = $6;
3475 $set{'from_id'} = $3;
3476 $set{'to_id'} = $4;
3477 $set{'id'} = $set{'to_id'};
3478 if ($set{'id'} =~ m/0{40}/) {
3479 $set{'id'} = $set{'from_id'};
19806691 3480 }
c994d620
KS
3481 if ($set{'id'} =~ m/0{40}/) {
3482 next;
3483 }
3484 push @files, \%set;
53b89d8d 3485 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
c994d620
KS
3486 if (%co) {
3487 if ($alternate) {
3488 print "<tr class=\"dark\">\n";
3489 } else {
3490 print "<tr class=\"light\">\n";
3491 }
3492 $alternate ^= 1;
71be1e79 3493 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
40c13813 3494 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
c994d620 3495 "<td>" .
952c65fc
JN
3496 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3497 -class => "list subject"},
63e4220b 3498 esc_html(chop_str($co{'title'}, 50)) . "<br/>");
c994d620
KS
3499 while (my $setref = shift @files) {
3500 my %set = %$setref;
952c65fc
JN
3501 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3502 hash=>$set{'id'}, file_name=>$set{'file'}),
3503 -class => "list"},
3504 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
c994d620
KS
3505 "<br/>\n";
3506 }
3507 print "</td>\n" .
3508 "<td class=\"link\">" .
756d2f06 3509 $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
952c65fc
JN
3510 " | " .
3511 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
c994d620
KS
3512 print "</td>\n" .
3513 "</tr>\n";
3514 }
847e01fb 3515 %co = parse_commit($1);
19806691 3516 }
19806691 3517 }
c994d620 3518 close $fd;
19806691
KS
3519 }
3520 print "</table>\n";
19806691
KS
3521 git_footer_html();
3522}
3523
3524sub git_shortlog {
847e01fb 3525 my $head = git_get_head_hash($project);
19806691
KS
3526 if (!defined $hash) {
3527 $hash = $head;
3528 }
ea4a6df4
KS
3529 if (!defined $page) {
3530 $page = 0;
3531 }
847e01fb 3532 my $refs = git_get_references();
ea4a6df4
KS
3533
3534 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
25691fbe 3535 open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
cac4bd94 3536 or die_error(undef, "Open git-rev-list failed");
0881d2d1 3537 my @revlist = map { chomp; $_ } <$fd>;
19806691 3538 close $fd;
ea4a6df4 3539
847e01fb 3540 my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
9f5dcb81
JN
3541 my $next_link = '';
3542 if ($#revlist >= (100 * ($page+1)-1)) {
3543 $next_link =
756d2f06 3544 $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
9f5dcb81
JN
3545 -title => "Alt-n"}, "next");
3546 }
3547
0d83ddc4
JN
3548
3549 git_header_html();
847e01fb
JN
3550 git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3551 git_print_header_div('summary', $project);
0d83ddc4 3552
9f5dcb81
JN
3553 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3554
19806691
KS
3555 git_footer_html();
3556}
717b8311
JN
3557
3558## ......................................................................
3559## feeds (RSS, OPML)
3560
3561sub git_rss {
3562 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
25691fbe 3563 open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
cac4bd94 3564 or die_error(undef, "Open git-rev-list failed");
717b8311 3565 my @revlist = map { chomp; $_ } <$fd>;
cac4bd94 3566 close $fd or die_error(undef, "Reading git-rev-list failed");
717b8311 3567 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
59b9f61a
JN
3568 print <<XML;
3569<?xml version="1.0" encoding="utf-8"?>
3570<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3571<channel>
3572<title>$project $my_uri $my_url</title>
3573<link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3574<description>$project log</description>
3575<language>en</language>
3576XML
717b8311
JN
3577
3578 for (my $i = 0; $i <= $#revlist; $i++) {
3579 my $commit = $revlist[$i];
847e01fb 3580 my %co = parse_commit($commit);
717b8311
JN
3581 # we read 150, we always show 30 and the ones more recent than 48 hours
3582 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3583 last;
3584 }
847e01fb 3585 my %cd = parse_date($co{'committer_epoch'});
25691fbe 3586 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
6bcf4b46
JN
3587 $co{'parent'}, $co{'id'}
3588 or next;
717b8311 3589 my @difftree = map { chomp; $_ } <$fd>;
6bcf4b46
JN
3590 close $fd
3591 or next;
717b8311
JN
3592 print "<item>\n" .
3593 "<title>" .
3594 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3595 "</title>\n" .
3596 "<author>" . esc_html($co{'author'}) . "</author>\n" .
3597 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3598 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3599 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3600 "<description>" . esc_html($co{'title'}) . "</description>\n" .
3601 "<content:encoded>" .
3602 "<![CDATA[\n";
3603 my $comment = $co{'comment'};
3604 foreach my $line (@$comment) {
3605 $line = decode("utf8", $line, Encode::FB_DEFAULT);
3606 print "$line<br/>\n";
3607 }
3608 print "<br/>\n";
3609 foreach my $line (@difftree) {
3610 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3611 next;
3612 }
a2f3db2f 3613 my $file = esc_html(unquote($7));
717b8311
JN
3614 $file = decode("utf8", $file, Encode::FB_DEFAULT);
3615 print "$file<br/>\n";
3616 }
3617 print "]]>\n" .
3618 "</content:encoded>\n" .
3619 "</item>\n";
3620 }
3621 print "</channel></rss>";
3622}
3623
3624sub git_opml {
847e01fb 3625 my @list = git_get_projects_list();
717b8311
JN
3626
3627 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
59b9f61a
JN
3628 print <<XML;
3629<?xml version="1.0" encoding="utf-8"?>
3630<opml version="1.0">
3631<head>
3632 <title>$site_name Git OPML Export</title>
3633</head>
3634<body>
3635<outline text="git RSS feeds">
3636XML
717b8311
JN
3637
3638 foreach my $pr (@list) {
3639 my %proj = %$pr;
847e01fb 3640 my $head = git_get_head_hash($proj{'path'});
717b8311
JN
3641 if (!defined $head) {
3642 next;
3643 }
25691fbe 3644 $git_dir = "$projectroot/$proj{'path'}";
847e01fb 3645 my %co = parse_commit($head);
717b8311
JN
3646 if (!%co) {
3647 next;
3648 }
3649
3650 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3651 my $rss = "$my_url?p=$proj{'path'};a=rss";
3652 my $html = "$my_url?p=$proj{'path'};a=summary";
3653 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3654 }
59b9f61a
JN
3655 print <<XML;
3656</outline>
3657</body>
3658</opml>
3659XML
717b8311 3660}