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