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