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