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