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