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