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