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