]> git.ipfire.org Git - thirdparty/git.git/blame - gitweb/gitweb.cgi
gitweb.cgi: Create $git_temp if it doesn't exist
[thirdparty/git.git] / gitweb / gitweb.cgi
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';
10bb9036 17binmode STDOUT, ':utf8';
161332a5 18
4a87b43e
DS
19our $cgi = new CGI;
20our $version = "267";
21our $my_url = $cgi->url();
22our $my_uri = $cgi->url(-absolute => 1);
23our $rss_link = "";
3e029299 24
3f7f2710 25# location of the git-core binaries
4a87b43e 26our $gitbin = "/usr/bin";
3f7f2710 27
b87d78d6 28# absolute fs-path which will be prepended to the project path
4a87b43e
DS
29#our $projectroot = "/pub/scm";
30our $projectroot = "/home/kay/public_html/pub/scm";
b87d78d6 31
ae20de53 32# version of the git-core binaries
4a87b43e 33our $git_version = qx($gitbin/git --version);
ae20de53
JN
34if ($git_version =~ m/git version (.*)$/) {
35 $git_version = $1;
36} else {
37 $git_version = "unknown";
38}
39
b87d78d6 40# location for temporary files needed for diffs
4a87b43e 41our $git_temp = "/tmp/gitweb";
9af25117
LT
42if (! -d $git_temp) {
43 mkdir($git_temp, 0700) || die_error("Couldn't mkdir $git_temp");
44}
034df39e 45
b87d78d6 46# target of the home link on top of all pages
4a87b43e 47our $home_link = $my_uri;
b87d78d6 48
8ab1da2c 49# html text to include at home page
4a87b43e 50our $home_text = "indextext.html";
8ab1da2c 51
aedd9425 52# URI of default stylesheet
4a87b43e 53our $stylesheet = "gitweb.css";
aedd9425 54
09bd7898 55# source of projects list
4a87b43e
DS
56#our $projects_list = $projectroot;
57our $projects_list = "index/index.aux";
b87d78d6 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
09bd7898 67# input validation and dispatch
4a87b43e 68our $action = $cgi->param('a');
09bd7898 69if (defined $action) {
c91da262 70 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
09bd7898
KS
71 undef $action;
72 die_error(undef, "Invalid action parameter.");
b87d78d6 73 }
09bd7898
KS
74 if ($action eq "git-logo.png") {
75 git_logo();
76 exit;
c994d620
KS
77 } elsif ($action eq "opml") {
78 git_opml();
79 exit;
09bd7898 80 }
b87d78d6 81}
44ad2978 82
4a87b43e 83our $order = $cgi->param('o');
f7ab660c 84if (defined $order) {
c91da262 85 if ($order =~ m/[^0-9a-zA-Z_]/) {
f7ab660c
KS
86 undef $order;
87 die_error(undef, "Invalid order parameter.");
88 }
89}
90
4a87b43e 91our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
b87d78d6 92if (defined $project) {
49f582a0 93 $project =~ s|^/||; $project =~ s|/$||;
c91da262
KS
94 $project = validate_input($project);
95 if (!defined($project)) {
96 die_error(undef, "Invalid project parameter.");
9cd3d988
KS
97 }
98 if (!(-d "$projectroot/$project")) {
b87d78d6 99 undef $project;
09bd7898 100 die_error(undef, "No such directory.");
b87d78d6
KS
101 }
102 if (!(-e "$projectroot/$project/HEAD")) {
103 undef $project;
09bd7898 104 die_error(undef, "No such project.");
9cd3d988 105 }
232ff553
KS
106 $rss_link = "<link rel=\"alternate\" title=\"" . esc_param($project) . " log\" href=\"" .
107 "$my_uri?" . esc_param("p=$project;a=rss") . "\" type=\"application/rss+xml\"/>";
4fac5294 108 $ENV{'GIT_DIR'} = "$projectroot/$project";
09bd7898 109} else {
ede5e100 110 git_project_list();
09bd7898 111 exit;
a59d4afd 112}
6191f8e1 113
4a87b43e 114our $file_name = $cgi->param('f');
b87d78d6 115if (defined $file_name) {
c91da262
KS
116 $file_name = validate_input($file_name);
117 if (!defined($file_name)) {
118 die_error(undef, "Invalid file parameter.");
b87d78d6 119 }
a59d4afd 120}
6191f8e1 121
4a87b43e 122our $hash = $cgi->param('h');
4fac5294 123if (defined $hash) {
c91da262
KS
124 $hash = validate_input($hash);
125 if (!defined($hash)) {
126 die_error(undef, "Invalid hash parameter.");
4fac5294 127 }
a59d4afd 128}
6191f8e1 129
4a87b43e 130our $hash_parent = $cgi->param('hp');
c91da262
KS
131if (defined $hash_parent) {
132 $hash_parent = validate_input($hash_parent);
133 if (!defined($hash_parent)) {
134 die_error(undef, "Invalid hash parent parameter.");
135 }
09bd7898
KS
136}
137
4a87b43e 138our $hash_base = $cgi->param('hb');
c91da262
KS
139if (defined $hash_base) {
140 $hash_base = validate_input($hash_base);
141 if (!defined($hash_base)) {
142 die_error(undef, "Invalid hash base parameter.");
143 }
a59d4afd 144}
6191f8e1 145
4a87b43e 146our $page = $cgi->param('pg');
ea4a6df4 147if (defined $page) {
c91da262 148 if ($page =~ m/[^0-9]$/) {
ea4a6df4
KS
149 undef $page;
150 die_error(undef, "Invalid page parameter.");
b87d78d6 151 }
2ad9331e 152}
823d5dc8 153
4a87b43e 154our $searchtext = $cgi->param('s');
19806691
KS
155if (defined $searchtext) {
156 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
157 undef $searchtext;
158 die_error(undef, "Invalid search parameter.");
159 }
160 $searchtext = quotemeta $searchtext;
161}
162
c91da262
KS
163sub validate_input {
164 my $input = shift;
165
166 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
167 return $input;
168 }
169 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
170 return undef;
171 }
f5ac835a 172 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
c91da262
KS
173 return undef;
174 }
175 return $input;
176}
177
4fac5294 178if (!defined $action || $action eq "summary") {
ede5e100
KS
179 git_summary();
180 exit;
d8f1c5c2
KS
181} elsif ($action eq "heads") {
182 git_heads();
0db37973 183 exit;
ede5e100
KS
184} elsif ($action eq "tags") {
185 git_tags();
186 exit;
187} elsif ($action eq "blob") {
09bd7898
KS
188 git_blob();
189 exit;
19806691
KS
190} elsif ($action eq "blob_plain") {
191 git_blob_plain();
192 exit;
09bd7898
KS
193} elsif ($action eq "tree") {
194 git_tree();
195 exit;
196} elsif ($action eq "rss") {
197 git_rss();
198 exit;
199} elsif ($action eq "commit") {
200 git_commit();
201 exit;
202} elsif ($action eq "log") {
203 git_log();
204 exit;
205} elsif ($action eq "blobdiff") {
206 git_blobdiff();
207 exit;
19806691
KS
208} elsif ($action eq "blobdiff_plain") {
209 git_blobdiff_plain();
210 exit;
09bd7898
KS
211} elsif ($action eq "commitdiff") {
212 git_commitdiff();
213 exit;
19806691
KS
214} elsif ($action eq "commitdiff_plain") {
215 git_commitdiff_plain();
216 exit;
09bd7898
KS
217} elsif ($action eq "history") {
218 git_history();
219 exit;
19806691
KS
220} elsif ($action eq "search") {
221 git_search();
222 exit;
223} elsif ($action eq "shortlog") {
224 git_shortlog();
225 exit;
d8a20ba9
KS
226} elsif ($action eq "tag") {
227 git_tag();
228 exit;
e34ef621
FF
229} elsif ($action eq "blame") {
230 git_blame();
231 exit;
09bd7898
KS
232} else {
233 undef $action;
234 die_error(undef, "Unknown action.");
235 exit;
236}
237
232ff553
KS
238# quote unsafe chars, but keep the slash, even when it's not
239# correct, but quoted slashes look too horrible in bookmarks
240sub esc_param {
353347b0 241 my $str = shift;
232ff553 242 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
18216710 243 $str =~ s/\+/%2B/g;
a9e60b7d 244 $str =~ s/ /\+/g;
353347b0
KS
245 return $str;
246}
247
232ff553 248# replace invalid utf8 character with SUBSTITUTION sequence
40c13813
KS
249sub esc_html {
250 my $str = shift;
40c13813 251 $str = decode("utf8", $str, Encode::FB_DEFAULT);
10bb9036 252 $str = escapeHTML($str);
40c13813
KS
253 return $str;
254}
255
232ff553
KS
256# git may return quoted and escaped filenames
257sub unquote {
258 my $str = shift;
259 if ($str =~ m/^"(.*)"$/) {
260 $str = $1;
261 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
262 }
263 return $str;
264}
265
1f1ab5f0
JN
266# CSS class for given age value (in seconds)
267sub age_class {
268 my $age = shift;
269
270 if ($age < 60*60*2) {
271 return "age0";
272 } elsif ($age < 60*60*24*2) {
273 return "age1";
274 } else {
275 return "age2";
276 }
277}
278
12a88f2f 279sub git_header_html {
a59d4afd 280 my $status = shift || "200 OK";
11044297 281 my $expires = shift;
a59d4afd 282
b87d78d6
KS
283 my $title = "git";
284 if (defined $project) {
285 $title .= " - $project";
286 if (defined $action) {
287 $title .= "/$action";
7bedd9fc
JN
288 if (defined $file_name) {
289 $title .= " - $file_name";
290 if ($action eq "tree" && $file_name !~ m|/$|) {
291 $title .= "/";
292 }
293 }
b87d78d6
KS
294 }
295 }
11044297 296 print $cgi->header(-type=>'text/html', -charset => 'utf-8', -status=> $status, -expires => $expires);
a59d4afd 297 print <<EOF;
6191f8e1 298<?xml version="1.0" encoding="utf-8"?>
161332a5 299<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
034df39e 300<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
00cd0794 301<!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
ae20de53 302<!-- git core binaries version $git_version -->
161332a5 303<head>
c994d620
KS
304<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
305<meta name="robots" content="index, nofollow"/>
b87d78d6 306<title>$title</title>
ae20de53 307<link rel="stylesheet" type="text/css" href="$stylesheet"/>
6191f8e1 308$rss_link
161332a5
KS
309</head>
310<body>
311EOF
ff7669a5 312 print "<div class=\"page_header\">\n" .
c994d620 313 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
232ff553 314 "<img src=\"$my_uri?" . esc_param("a=git-logo.png") . "\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
19806691 315 "</a>\n";
232ff553 316 print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
b87d78d6 317 if (defined $project) {
232ff553 318 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
b87d78d6
KS
319 if (defined $action) {
320 print " / $action";
321 }
19806691
KS
322 print "\n";
323 if (!defined $searchtext) {
324 $searchtext = "";
325 }
c39e47d9 326 my $search_hash;
4c5c2026
TH
327 if (defined $hash_base) {
328 $search_hash = $hash_base;
329 } elsif (defined $hash) {
c39e47d9
KS
330 $search_hash = $hash;
331 } else {
8adc4bd4 332 $search_hash = "HEAD";
c39e47d9 333 }
19806691 334 $cgi->param("a", "search");
c39e47d9 335 $cgi->param("h", $search_hash);
353347b0 336 print $cgi->startform(-method => "get", -action => $my_uri) .
c994d620
KS
337 "<div class=\"search\">\n" .
338 $cgi->hidden(-name => "p") . "\n" .
339 $cgi->hidden(-name => "a") . "\n" .
c39e47d9 340 $cgi->hidden(-name => "h") . "\n" .
c994d620
KS
341 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
342 "</div>" .
343 $cgi->end_form() . "\n";
4c02e3c5
KS
344 }
345 print "</div>\n";
161332a5
KS
346}
347
12a88f2f 348sub git_footer_html {
6191f8e1 349 print "<div class=\"page_footer\">\n";
b87d78d6 350 if (defined $project) {
09bd7898 351 my $descr = git_read_description($project);
b87d78d6 352 if (defined $descr) {
40c13813 353 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
6191f8e1 354 }
232ff553 355 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
c994d620 356 } else {
232ff553 357 print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
ff7669a5 358 }
6191f8e1
KS
359 print "</div>\n" .
360 "</body>\n" .
9cd3d988 361 "</html>";
161332a5
KS
362}
363
061cc7cd
KS
364sub die_error {
365 my $status = shift || "403 Forbidden";
7a9b4c5f 366 my $error = shift || "Malformed query, file missing or permission denied";
664f4cc5 367
a59d4afd
KS
368 git_header_html($status);
369 print "<div class=\"page_body\">\n" .
7ab0d2b6
KS
370 "<br/><br/>\n" .
371 "$status - $error\n" .
372 "<br/>\n" .
373 "</div>\n";
a59d4afd 374 git_footer_html();
09bd7898 375 exit;
a59d4afd
KS
376}
377
42f7eb94
KS
378sub git_get_type {
379 my $hash = shift;
380
19806691 381 open my $fd, "-|", "$gitbin/git-cat-file -t $hash" or return;
42f7eb94 382 my $type = <$fd>;
d8a20ba9 383 close $fd or return;
42f7eb94
KS
384 chomp $type;
385 return $type;
386}
387
df2c37a5
JH
388sub git_read_head {
389 my $project = shift;
390 my $oENV = $ENV{'GIT_DIR'};
391 my $retval = undef;
392 $ENV{'GIT_DIR'} = "$projectroot/$project";
393 if (open my $fd, "-|", "$gitbin/git-rev-parse", "--verify", "HEAD") {
394 my $head = <$fd>;
395 close $fd;
2c5c008b
KS
396 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
397 $retval = $1;
df2c37a5
JH
398 }
399 }
2c5c008b
KS
400 if (defined $oENV) {
401 $ENV{'GIT_DIR'} = $oENV;
402 }
df2c37a5
JH
403 return $retval;
404}
405
ede5e100 406sub git_read_hash {
54b0a43c 407 my $path = shift;
09bd7898 408
19806691 409 open my $fd, "$projectroot/$path" or return undef;
12a88f2f
KS
410 my $head = <$fd>;
411 close $fd;
412 chomp $head;
b87d78d6
KS
413 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
414 return $head;
b87d78d6
KS
415 }
416}
417
09bd7898 418sub git_read_description {
b87d78d6 419 my $path = shift;
09bd7898 420
19806691 421 open my $fd, "$projectroot/$path/description" or return undef;
b87d78d6
KS
422 my $descr = <$fd>;
423 close $fd;
424 chomp $descr;
425 return $descr;
12a88f2f
KS
426}
427
ede5e100
KS
428sub git_read_tag {
429 my $tag_id = shift;
430 my %tag;
d8a20ba9 431 my @comment;
ede5e100 432
19806691 433 open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" or return;
d8a20ba9 434 $tag{'id'} = $tag_id;
ede5e100
KS
435 while (my $line = <$fd>) {
436 chomp $line;
437 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
438 $tag{'object'} = $1;
7ab0d2b6 439 } elsif ($line =~ m/^type (.+)$/) {
ede5e100 440 $tag{'type'} = $1;
7ab0d2b6 441 } elsif ($line =~ m/^tag (.+)$/) {
ede5e100 442 $tag{'name'} = $1;
d8a20ba9
KS
443 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
444 $tag{'author'} = $1;
445 $tag{'epoch'} = $2;
446 $tag{'tz'} = $3;
447 } elsif ($line =~ m/--BEGIN/) {
448 push @comment, $line;
449 last;
450 } elsif ($line eq "") {
451 last;
ede5e100
KS
452 }
453 }
d8a20ba9
KS
454 push @comment, <$fd>;
455 $tag{'comment'} = \@comment;
19806691 456 close $fd or return;
ede5e100
KS
457 if (!defined $tag{'name'}) {
458 return
459 };
460 return %tag
461}
462
d263a6bd
KS
463sub age_string {
464 my $age = shift;
465 my $age_str;
466
467 if ($age > 60*60*24*365*2) {
468 $age_str = (int $age/60/60/24/365);
469 $age_str .= " years ago";
470 } elsif ($age > 60*60*24*(365/12)*2) {
471 $age_str = int $age/60/60/24/(365/12);
472 $age_str .= " months ago";
473 } elsif ($age > 60*60*24*7*2) {
474 $age_str = int $age/60/60/24/7;
475 $age_str .= " weeks ago";
476 } elsif ($age > 60*60*24*2) {
477 $age_str = int $age/60/60/24;
478 $age_str .= " days ago";
479 } elsif ($age > 60*60*2) {
480 $age_str = int $age/60/60;
481 $age_str .= " hours ago";
482 } elsif ($age > 60*2) {
483 $age_str = int $age/60;
484 $age_str .= " min ago";
485 } elsif ($age > 2) {
486 $age_str = int $age;
487 $age_str .= " sec ago";
488 } else {
489 $age_str .= " right now";
490 }
491 return $age_str;
492}
493
09bd7898 494sub git_read_commit {
19806691
KS
495 my $commit_id = shift;
496 my $commit_text = shift;
497
498 my @commit_lines;
703ac710 499 my %co;
703ac710 500
19806691
KS
501 if (defined $commit_text) {
502 @commit_lines = @$commit_text;
503 } else {
25f422fb
KS
504 $/ = "\0";
505 open my $fd, "-|", "$gitbin/git-rev-list --header --parents --max-count=1 $commit_id" or return;
506 @commit_lines = split '\n', <$fd>;
19806691 507 close $fd or return;
25f422fb
KS
508 $/ = "\n";
509 pop @commit_lines;
19806691 510 }
25f422fb
KS
511 my $header = shift @commit_lines;
512 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
513 return;
514 }
515 ($co{'id'}, my @parents) = split ' ', $header;
516 $co{'parents'} = \@parents;
517 $co{'parent'} = $parents[0];
19806691 518 while (my $line = shift @commit_lines) {
b87d78d6 519 last if $line eq "\n";
7ab0d2b6 520 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
703ac710 521 $co{'tree'} = $1;
022be3d0 522 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
3f714537 523 $co{'author'} = $1;
185f09e5
KS
524 $co{'author_epoch'} = $2;
525 $co{'author_tz'} = $3;
2bf7a52c
KS
526 if ($co{'author'} =~ m/^([^<]+) </) {
527 $co{'author_name'} = $1;
528 } else {
529 $co{'author_name'} = $co{'author'};
530 }
86eed32d
KS
531 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
532 $co{'committer'} = $1;
185f09e5
KS
533 $co{'committer_epoch'} = $2;
534 $co{'committer_tz'} = $3;
991910a9
KS
535 $co{'committer_name'} = $co{'committer'};
536 $co{'committer_name'} =~ s/ <.*//;
703ac710
KS
537 }
538 }
ede5e100 539 if (!defined $co{'tree'}) {
25f422fb 540 return;
ede5e100 541 };
25f422fb 542
19806691 543 foreach my $title (@commit_lines) {
c2488d06 544 $title =~ s/^ //;
19806691 545 if ($title ne "") {
48c771f4 546 $co{'title'} = chop_str($title, 80, 5);
19806691
KS
547 # remove leading stuff of merges to make the interesting part visible
548 if (length($title) > 50) {
549 $title =~ s/^Automatic //;
550 $title =~ s/^merge (of|with) /Merge ... /i;
551 if (length($title) > 50) {
552 $title =~ s/(http|rsync):\/\///;
553 }
554 if (length($title) > 50) {
555 $title =~ s/(master|www|rsync)\.//;
556 }
557 if (length($title) > 50) {
558 $title =~ s/kernel.org:?//;
559 }
560 if (length($title) > 50) {
561 $title =~ s/\/pub\/scm//;
562 }
563 }
48c771f4 564 $co{'title_short'} = chop_str($title, 50, 5);
19806691
KS
565 last;
566 }
567 }
25f422fb
KS
568 # remove added spaces
569 foreach my $line (@commit_lines) {
570 $line =~ s/^ //;
571 }
572 $co{'comment'} = \@commit_lines;
2ae100df
KS
573
574 my $age = time - $co{'committer_epoch'};
575 $co{'age'} = $age;
d263a6bd 576 $co{'age_string'} = age_string($age);
71be1e79
KS
577 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
578 if ($age > 60*60*24*7*2) {
1b1cd421 579 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
71be1e79
KS
580 $co{'age_string_age'} = $co{'age_string'};
581 } else {
582 $co{'age_string_date'} = $co{'age_string'};
1b1cd421 583 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
71be1e79 584 }
703ac710
KS
585 return %co;
586}
587
19806691 588sub git_diff_print {
8ed23e1b 589 my $from = shift;
2735983d 590 my $from_name = shift;
8ed23e1b 591 my $to = shift;
2735983d 592 my $to_name = shift;
19806691 593 my $format = shift || "html";
4c02e3c5 594
8ed23e1b
KS
595 my $from_tmp = "/dev/null";
596 my $to_tmp = "/dev/null";
8ed23e1b 597 my $pid = $$;
4c02e3c5 598
ff7669a5 599 # create tmp from-file
b87d78d6 600 if (defined $from) {
19806691 601 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
b87d78d6 602 open my $fd2, "> $from_tmp";
034df39e 603 open my $fd, "-|", "$gitbin/git-cat-file blob $from";
8ed23e1b
KS
604 my @file = <$fd>;
605 print $fd2 @file;
4c02e3c5
KS
606 close $fd2;
607 close $fd;
4c02e3c5
KS
608 }
609
b531daf3 610 # create tmp to-file
b87d78d6 611 if (defined $to) {
19806691 612 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
8ed23e1b 613 open my $fd2, "> $to_tmp";
034df39e 614 open my $fd, "-|", "$gitbin/git-cat-file blob $to";
8ed23e1b
KS
615 my @file = <$fd>;
616 print $fd2 @file;
4c02e3c5
KS
617 close $fd2;
618 close $fd;
4c02e3c5
KS
619 }
620
18216710 621 open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
19806691
KS
622 if ($format eq "plain") {
623 undef $/;
624 print <$fd>;
625 $/ = "\n";
626 } else {
627 while (my $line = <$fd>) {
628 chomp($line);
629 my $char = substr($line, 0, 1);
1f1ab5f0 630 my $diff_class = "";
19806691 631 if ($char eq '+') {
1f1ab5f0 632 $diff_class = " add";
19806691 633 } elsif ($char eq "-") {
1f1ab5f0 634 $diff_class = " rem";
19806691 635 } elsif ($char eq "@") {
1f1ab5f0 636 $diff_class = " chunk_header";
19806691
KS
637 } elsif ($char eq "\\") {
638 # skip errors
639 next;
640 }
641 while ((my $pos = index($line, "\t")) != -1) {
642 if (my $count = (8 - (($pos-1) % 8))) {
643 my $spaces = ' ' x $count;
644 $line =~ s/\t/$spaces/;
645 }
646 }
1f1ab5f0 647 print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
c07ad4b9 648 }
4c02e3c5
KS
649 }
650 close $fd;
8ed23e1b 651
b87d78d6 652 if (defined $from) {
2735983d 653 unlink($from_tmp);
8ed23e1b 654 }
b87d78d6 655 if (defined $to) {
2735983d 656 unlink($to_tmp);
8ed23e1b 657 }
4c02e3c5
KS
658}
659
d767d59c 660sub mode_str {
2735983d
KS
661 my $mode = oct shift;
662
b87d78d6
KS
663 if (S_ISDIR($mode & S_IFMT)) {
664 return 'drwxr-xr-x';
665 } elsif (S_ISLNK($mode)) {
666 return 'lrwxrwxrwx';
667 } elsif (S_ISREG($mode)) {
991910a9 668 # git cares only about the executable bit
b87d78d6
KS
669 if ($mode & S_IXUSR) {
670 return '-rwxr-xr-x';
54b0a43c 671 } else {
b87d78d6 672 return '-rw-r--r--';
54b0a43c 673 };
b87d78d6
KS
674 } else {
675 return '----------';
d767d59c 676 }
d767d59c
KS
677}
678
2bf7a52c
KS
679sub chop_str {
680 my $str = shift;
681 my $len = shift;
19806691 682 my $add_len = shift || 10;
2bf7a52c 683
a48e11ca
KS
684 # allow only $len chars, but don't cut a word if it would fit in $add_len
685 # if it doesn't fit, cut it if it's still longer than the dots we would add
686 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
687 my $body = $1;
688 my $tail = $2;
689 if (length($tail) > 4) {
690 $tail = " ...";
2bf7a52c 691 }
a48e11ca 692 return "$body$tail";
2bf7a52c
KS
693}
694
2735983d
KS
695sub file_type {
696 my $mode = oct shift;
697
b87d78d6 698 if (S_ISDIR($mode & S_IFMT)) {
2735983d 699 return "directory";
b87d78d6 700 } elsif (S_ISLNK($mode)) {
2735983d 701 return "symlink";
b87d78d6
KS
702 } elsif (S_ISREG($mode)) {
703 return "file";
2735983d
KS
704 } else {
705 return "unknown";
706 }
707}
708
f49201a9
KS
709sub format_log_line_html {
710 my $line = shift;
711
40c13813 712 $line = esc_html($line);
f49201a9
KS
713 $line =~ s/ /&nbsp;/g;
714 if ($line =~ m/([0-9a-fA-F]{40})/) {
715 my $hash_text = $1;
716 if (git_get_type($hash_text) eq "commit") {
232ff553 717 my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
f49201a9
KS
718 $line =~ s/$hash_text/$link/;
719 }
720 }
721 return $line;
722}
723
86eed32d 724sub date_str {
991910a9
KS
725 my $epoch = shift;
726 my $tz = shift || "-0000";
86eed32d 727
991910a9 728 my %date;
86eed32d
KS
729 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
730 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
991910a9
KS
731 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
732 $date{'hour'} = $hour;
ff7669a5
KS
733 $date{'minute'} = $min;
734 $date{'mday'} = $mday;
735 $date{'day'} = $days[$wday];
736 $date{'month'} = $months[$mon];
991910a9
KS
737 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
738 $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
739
034df39e
KS
740 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
741 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
991910a9 742 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
185f09e5
KS
743 $date{'hour_local'} = $hour;
744 $date{'minute_local'} = $min;
745 $date{'tz_local'} = $tz;
991910a9 746 return %date;
86eed32d
KS
747}
748
b87d78d6 749# git-logo (cached in browser for one day)
eb28240b 750sub git_logo {
8f1deb5f 751 binmode STDOUT, ':raw';
022be3d0 752 print $cgi->header(-type => 'image/png', -expires => '+1d');
b87d78d6
KS
753 # cat git-logo.png | hexdump -e '16/1 " %02x" "\n"' | sed 's/ /\\x/g'
754 print "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
755 "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
756 "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
757 "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
758 "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
759 "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
760 "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
761 "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
762 "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
763 "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
764 "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
765 "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
766 "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
022be3d0
KS
767}
768
c07ad4b9
KS
769sub get_file_owner {
770 my $path = shift;
771
772 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
773 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
774 if (!defined $gcos) {
775 return undef;
776 }
777 my $owner = $gcos;
778 $owner =~ s/[,;].*$//;
281bf0cf 779 return decode("utf8", $owner, Encode::FB_DEFAULT);
c07ad4b9
KS
780}
781
c994d620 782sub git_read_projects {
09bd7898
KS
783 my @list;
784
ede5e100 785 if (-d $projects_list) {
09bd7898 786 # search in directory
ede5e100 787 my $dir = $projects_list;
19806691 788 opendir my $dh, $dir or return undef;
09bd7898
KS
789 while (my $dir = readdir($dh)) {
790 if (-e "$projectroot/$dir/HEAD") {
c07ad4b9
KS
791 my $pr = {
792 path => $dir,
793 };
794 push @list, $pr
09bd7898
KS
795 }
796 }
797 closedir($dh);
ede5e100 798 } elsif (-f $projects_list) {
7ab0d2b6
KS
799 # read from file(url-encoded):
800 # 'git%2Fgit.git Linus+Torvalds'
801 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
802 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
19806691 803 open my $fd , $projects_list or return undef;
09bd7898
KS
804 while (my $line = <$fd>) {
805 chomp $line;
7403d50b
KS
806 my ($path, $owner) = split ' ', $line;
807 $path = unescape($path);
808 $owner = unescape($owner);
c07ad4b9
KS
809 if (!defined $path) {
810 next;
811 }
812 if (-e "$projectroot/$path/HEAD") {
813 my $pr = {
814 path => $path,
281bf0cf 815 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
c07ad4b9
KS
816 };
817 push @list, $pr
09bd7898
KS
818 }
819 }
820 close $fd;
821 }
c994d620
KS
822 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
823 return @list;
824}
09bd7898 825
5996ca08
FF
826sub git_get_project_config {
827 my $key = shift;
828
829 return unless ($key);
830 $key =~ s/^gitweb\.//;
831 return if ($key =~ m/\W/);
832
ae20de53 833 my $val = qx($gitbin/git-repo-config --get gitweb.$key);
5996ca08
FF
834 return ($val);
835}
836
837sub git_get_project_config_bool {
838 my $val = git_get_project_config (@_);
839 if ($val and $val =~ m/true|yes|on/) {
840 return (1);
841 }
842 return; # implicit false
843}
844
c994d620
KS
845sub git_project_list {
846 my @list = git_read_projects();
f7ab660c 847 my @projects;
09bd7898
KS
848 if (!@list) {
849 die_error(undef, "No project found.");
850 }
f7ab660c 851 foreach my $pr (@list) {
df2c37a5 852 my $head = git_read_head($pr->{'path'});
f7ab660c
KS
853 if (!defined $head) {
854 next;
855 }
856 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
857 my %co = git_read_commit($head);
858 if (!%co) {
859 next;
860 }
861 $pr->{'commit'} = \%co;
862 if (!defined $pr->{'descr'}) {
863 my $descr = git_read_description($pr->{'path'}) || "";
864 $pr->{'descr'} = chop_str($descr, 25, 5);
865 }
866 if (!defined $pr->{'owner'}) {
867 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
868 }
869 push @projects, $pr;
870 }
b87d78d6 871 git_header_html();
8ab1da2c
KS
872 if (-f $home_text) {
873 print "<div class=\"index_include\">\n";
ede5e100 874 open (my $fd, $home_text);
8ab1da2c
KS
875 print <$fd>;
876 close $fd;
877 print "</div>\n";
878 }
1f1ab5f0 879 print "<table class=\"project_list\">\n" .
f7ab660c 880 "<tr>\n";
d8d17b5d 881 if (!defined($order) || (defined($order) && ($order eq "project"))) {
f7ab660c
KS
882 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
883 print "<th>Project</th>\n";
884 } else {
232ff553 885 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=project")}, "Project") . "</th>\n";
d8d17b5d
KS
886 }
887 if (defined($order) && ($order eq "descr")) {
888 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
889 print "<th>Description</th>\n";
890 } else {
232ff553 891 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=descr")}, "Description") . "</th>\n";
f7ab660c 892 }
f7ab660c
KS
893 if (defined($order) && ($order eq "owner")) {
894 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
895 print "<th>Owner</th>\n";
896 } else {
232ff553 897 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=owner")}, "Owner") . "</th>\n";
f7ab660c
KS
898 }
899 if (defined($order) && ($order eq "age")) {
900 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
d8d17b5d 901 print "<th>Last Change</th>\n";
f7ab660c 902 } else {
232ff553 903 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=age")}, "Last Change") . "</th>\n";
f7ab660c
KS
904 }
905 print "<th></th>\n" .
eb28240b 906 "</tr>\n";
bddec01d 907 my $alternate = 0;
f7ab660c 908 foreach my $pr (@projects) {
bddec01d 909 if ($alternate) {
c994d620 910 print "<tr class=\"dark\">\n";
bddec01d 911 } else {
c994d620 912 print "<tr class=\"light\">\n";
bddec01d
KS
913 }
914 $alternate ^= 1;
232ff553 915 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"), -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
f7ab660c
KS
916 "<td>$pr->{'descr'}</td>\n" .
917 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
1f1ab5f0 918 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" . $pr->{'commit'}{'age_string'} . "</td>\n" .
ede5e100 919 "<td class=\"link\">" .
232ff553
KS
920 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") .
921 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") .
922 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
9ea82aa8
KS
923 "</td>\n" .
924 "</tr>\n";
b87d78d6 925 }
7ab0d2b6 926 print "</table>\n";
b87d78d6 927 git_footer_html();
161332a5
KS
928}
929
6a928415 930sub read_info_ref {
4df11910 931 my $type = shift || "";
6a928415
KS
932 my %refs;
933 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
934 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
935 open my $fd, "$projectroot/$project/info/refs" or return;
936 while (my $line = <$fd>) {
c2488d06 937 chomp($line);
4df11910 938 if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
045e531a
KS
939 if (defined $refs{$1}) {
940 $refs{$1} .= " / $2";
941 } else {
942 $refs{$1} = $2;
943 }
6a928415
KS
944 }
945 }
946 close $fd or return;
947 return \%refs;
948}
949
0db37973
KS
950sub git_read_refs {
951 my $ref_dir = shift;
fa378499 952 my @reflist;
ede5e100 953
4fac5294 954 my @refs;
0db37973 955 opendir my $dh, "$projectroot/$project/$ref_dir";
4fac5294
KS
956 while (my $dir = readdir($dh)) {
957 if ($dir =~ m/^\./) {
958 next;
959 }
960 if (-d "$projectroot/$project/$ref_dir/$dir") {
961 opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
962 my @subdirs = grep !m/^\./, readdir $dh2;
963 closedir($dh2);
964 foreach my $subdir (@subdirs) {
965 push @refs, "$dir/$subdir"
966 }
967 next;
968 }
969 push @refs, $dir;
970 }
ede5e100 971 closedir($dh);
fa378499
KS
972 foreach my $ref_file (@refs) {
973 my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
974 my $type = git_get_type($ref_id) || next;
975 my %ref_item;
ede5e100 976 my %co;
d8a20ba9
KS
977 $ref_item{'type'} = $type;
978 $ref_item{'id'} = $ref_id;
d263a6bd
KS
979 $ref_item{'epoch'} = 0;
980 $ref_item{'age'} = "unknown";
ede5e100 981 if ($type eq "tag") {
fa378499 982 my %tag = git_read_tag($ref_id);
d8a20ba9 983 $ref_item{'comment'} = $tag{'comment'};
ede5e100
KS
984 if ($tag{'type'} eq "commit") {
985 %co = git_read_commit($tag{'object'});
d263a6bd
KS
986 $ref_item{'epoch'} = $co{'committer_epoch'};
987 $ref_item{'age'} = $co{'age_string'};
988 } elsif (defined($tag{'epoch'})) {
989 my $age = time - $tag{'epoch'};
990 $ref_item{'epoch'} = $tag{'epoch'};
991 $ref_item{'age'} = age_string($age);
ede5e100 992 }
d8a20ba9 993 $ref_item{'reftype'} = $tag{'type'};
fa378499 994 $ref_item{'name'} = $tag{'name'};
d8a20ba9 995 $ref_item{'refid'} = $tag{'object'};
ede5e100 996 } elsif ($type eq "commit"){
fa378499 997 %co = git_read_commit($ref_id);
d8a20ba9 998 $ref_item{'reftype'} = "commit";
fa378499
KS
999 $ref_item{'name'} = $ref_file;
1000 $ref_item{'title'} = $co{'title'};
d8a20ba9 1001 $ref_item{'refid'} = $ref_id;
d263a6bd
KS
1002 $ref_item{'epoch'} = $co{'committer_epoch'};
1003 $ref_item{'age'} = $co{'age_string'};
ede5e100 1004 }
ede5e100 1005
fa378499 1006 push @reflist, \%ref_item;
ede5e100
KS
1007 }
1008 # sort tags by age
fa378499
KS
1009 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1010 return \@reflist;
ede5e100
KS
1011}
1012
1013sub git_summary {
1014 my $descr = git_read_description($project) || "none";
df2c37a5 1015 my $head = git_read_head($project);
ede5e100
KS
1016 my %co = git_read_commit($head);
1017 my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1018
1019 my $owner;
1020 if (-f $projects_list) {
1021 open (my $fd , $projects_list);
1022 while (my $line = <$fd>) {
1023 chomp $line;
7403d50b
KS
1024 my ($pr, $ow) = split ' ', $line;
1025 $pr = unescape($pr);
1026 $ow = unescape($ow);
ede5e100 1027 if ($pr eq $project) {
281bf0cf 1028 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
ede5e100
KS
1029 last;
1030 }
1031 }
1032 close $fd;
1033 }
1034 if (!defined $owner) {
1035 $owner = get_file_owner("$projectroot/$project");
1036 }
1037
6a928415 1038 my $refs = read_info_ref();
ede5e100
KS
1039 git_header_html();
1040 print "<div class=\"page_nav\">\n" .
19806691 1041 "summary".
232ff553
KS
1042 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1043 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1044 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1045 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1046 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree")}, "tree") .
ede5e100
KS
1047 "<br/><br/>\n" .
1048 "</div>\n";
19806691 1049 print "<div class=\"title\">&nbsp;</div>\n";
bddec01d 1050 print "<table cellspacing=\"0\">\n" .
40c13813 1051 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
ede5e100
KS
1052 "<tr><td>owner</td><td>$owner</td></tr>\n" .
1053 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
bddec01d 1054 "</table>\n";
df2c37a5 1055 open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_head($project) or die_error(undef, "Open failed.");
ede5e100
KS
1056 my (@revlist) = map { chomp; $_ } <$fd>;
1057 close $fd;
1058 print "<div>\n" .
232ff553 1059 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog"), -class => "title"}, "shortlog") .
ede5e100 1060 "</div>\n";
19806691 1061 my $i = 16;
bddec01d
KS
1062 print "<table cellspacing=\"0\">\n";
1063 my $alternate = 0;
ede5e100
KS
1064 foreach my $commit (@revlist) {
1065 my %co = git_read_commit($commit);
1066 my %ad = date_str($co{'author_epoch'});
bddec01d 1067 if ($alternate) {
c994d620 1068 print "<tr class=\"dark\">\n";
bddec01d 1069 } else {
c994d620 1070 print "<tr class=\"light\">\n";
bddec01d
KS
1071 }
1072 $alternate ^= 1;
19806691 1073 if ($i-- > 0) {
4df11910
KS
1074 my $ref = "";
1075 if (defined $refs->{$commit}) {
edde3735 1076 $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
4df11910 1077 }
10dba28d 1078 print "<td><i>$co{'age_string'}</i></td>\n" .
40c13813 1079 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
c24fd7b7
KS
1080 "<td>";
1081 if (length($co{'title_short'}) < length($co{'title'})) {
232ff553 1082 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
7a9b4c5f 1083 "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
c24fd7b7 1084 } else {
232ff553 1085 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
4df11910 1086 "<b>" . esc_html($co{'title'}) . "$ref</b>");
6a928415 1087 }
76a8323a 1088 print "</td>\n" .
a4d26ef0 1089 "<td class=\"link\">" .
232ff553
KS
1090 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1091 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
a4d26ef0 1092 "</td>\n" .
440c6006
KS
1093 "</tr>";
1094 } else {
232ff553 1095 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "...") . "</td>\n" .
440c6006 1096 "</tr>";
ede5e100
KS
1097 last;
1098 }
1099 }
bddec01d 1100 print "</table\n>";
ede5e100 1101
0db37973 1102 my $taglist = git_read_refs("refs/tags");
ede5e100
KS
1103 if (defined @$taglist) {
1104 print "<div>\n" .
232ff553 1105 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags"), -class => "title"}, "tags") .
ede5e100 1106 "</div>\n";
19806691 1107 my $i = 16;
bddec01d
KS
1108 print "<table cellspacing=\"0\">\n";
1109 my $alternate = 0;
ede5e100
KS
1110 foreach my $entry (@$taglist) {
1111 my %tag = %$entry;
d8a20ba9
KS
1112 my $comment_lines = $tag{'comment'};
1113 my $comment = shift @$comment_lines;
1114 if (defined($comment)) {
1115 $comment = chop_str($comment, 30, 5);
1116 }
bddec01d 1117 if ($alternate) {
c994d620 1118 print "<tr class=\"dark\">\n";
bddec01d 1119 } else {
c994d620 1120 print "<tr class=\"light\">\n";
bddec01d
KS
1121 }
1122 $alternate ^= 1;
19806691 1123 if ($i-- > 0) {
10dba28d 1124 print "<td><i>$tag{'age'}</i></td>\n" .
19806691 1125 "<td>" .
232ff553 1126 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
40c13813 1127 "<b>" . esc_html($tag{'name'}) . "</b>") .
19806691 1128 "</td>\n" .
d8a20ba9
KS
1129 "<td>";
1130 if (defined($comment)) {
232ff553 1131 print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
d8a20ba9
KS
1132 }
1133 print "</td>\n" .
1134 "<td class=\"link\">";
1135 if ($tag{'type'} eq "tag") {
232ff553 1136 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
d8a20ba9 1137 }
232ff553 1138 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
d8a20ba9 1139 if ($tag{'reftype'} eq "commit") {
232ff553 1140 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
7a9b4c5f 1141 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
19806691
KS
1142 }
1143 print "</td>\n" .
440c6006
KS
1144 "</tr>";
1145 } else {
232ff553 1146 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "...") . "</td>\n" .
440c6006 1147 "</tr>";
ede5e100
KS
1148 last;
1149 }
1150 }
bddec01d 1151 print "</table\n>";
ede5e100 1152 }
0db37973 1153
d8f1c5c2
KS
1154 my $headlist = git_read_refs("refs/heads");
1155 if (defined @$headlist) {
0db37973 1156 print "<div>\n" .
232ff553 1157 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads"), -class => "title"}, "heads") .
0db37973 1158 "</div>\n";
19806691 1159 my $i = 16;
bddec01d
KS
1160 print "<table cellspacing=\"0\">\n";
1161 my $alternate = 0;
d8f1c5c2 1162 foreach my $entry (@$headlist) {
0db37973 1163 my %tag = %$entry;
bddec01d 1164 if ($alternate) {
c994d620 1165 print "<tr class=\"dark\">\n";
bddec01d 1166 } else {
c994d620 1167 print "<tr class=\"light\">\n";
bddec01d
KS
1168 }
1169 $alternate ^= 1;
19806691 1170 if ($i-- > 0) {
10dba28d 1171 print "<td><i>$tag{'age'}</i></td>\n" .
19806691 1172 "<td>" .
232ff553 1173 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"},
40c13813 1174 "<b>" . esc_html($tag{'name'}) . "</b>") .
19806691
KS
1175 "</td>\n" .
1176 "<td class=\"link\">" .
232ff553
KS
1177 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1178 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
19806691 1179 "</td>\n" .
0db37973
KS
1180 "</tr>";
1181 } else {
232ff553 1182 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "...") . "</td>\n" .
0db37973
KS
1183 "</tr>";
1184 last;
1185 }
1186 }
bddec01d 1187 print "</table\n>";
0db37973 1188 }
ede5e100
KS
1189 git_footer_html();
1190}
1191
d8a20ba9 1192sub git_tag {
df2c37a5 1193 my $head = git_read_head($project);
d8a20ba9
KS
1194 git_header_html();
1195 print "<div class=\"page_nav\">\n" .
232ff553
KS
1196 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1197 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1198 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1199 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1200 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1201 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
d8a20ba9
KS
1202 "<br/>\n" .
1203 "</div>\n";
1204 my %tag = git_read_tag($hash);
1205 print "<div>\n" .
232ff553 1206 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($tag{'name'})) . "\n" .
d8a20ba9
KS
1207 "</div>\n";
1208 print "<div class=\"title_text\">\n" .
1209 "<table cellspacing=\"0\">\n" .
e4669df9
KS
1210 "<tr>\n" .
1211 "<td>object</td>\n" .
232ff553
KS
1212 "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
1213 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
e4669df9 1214 "</tr>\n";
d8a20ba9
KS
1215 if (defined($tag{'author'})) {
1216 my %ad = date_str($tag{'epoch'}, $tag{'tz'});
40c13813 1217 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
d8a20ba9
KS
1218 print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1219 }
1220 print "</table>\n\n" .
1221 "</div>\n";
1222 print "<div class=\"page_body\">";
1223 my $comment = $tag{'comment'};
1224 foreach my $line (@$comment) {
40c13813 1225 print esc_html($line) . "<br/>\n";
d8a20ba9
KS
1226 }
1227 print "</div>\n";
1228 git_footer_html();
1229}
1230
e34ef621
FF
1231sub git_blame {
1232 my $fd;
5996ca08 1233 die_error('403 Permission denied', "Permission denied.") if (!git_get_project_config_bool ('blame'));
e34ef621
FF
1234 die_error('404 Not Found', "What file will it be, master?") if (!$file_name);
1235 $hash_base ||= git_read_head($project);
1236 die_error(undef, "Reading commit failed.") unless ($hash_base);
1237 my %co = git_read_commit($hash_base)
1238 or die_error(undef, "Reading commit failed.");
1239 if (!defined $hash) {
1240 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1241 or die_error(undef, "Error lookup file.");
1242 }
1243 open ($fd, "-|", "$gitbin/git-annotate", '-l', '-t', '-r', $file_name, $hash_base)
1244 or die_error(undef, "Open failed.");
1245 git_header_html();
1246 print "<div class=\"page_nav\">\n" .
1247 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1248 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1249 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1250 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1251 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1252 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1253 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1254 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head") . "<br/>\n";
1255 print "</div>\n".
1256 "<div>" .
1257 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1258 "</div>\n";
1259 print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
1260 print "<div class=\"page_body\">\n";
1261 print <<HTML;
1f1ab5f0 1262<table class="blame">
e34ef621
FF
1263 <tr>
1264 <th>Commit</th>
1265 <th>Age</th>
1266 <th>Author</th>
1267 <th>Line</th>
1268 <th>Data</th>
1269 </tr>
1270HTML
1271 my @line_class = (qw(light dark));
1272 my $line_class_len = scalar (@line_class);
1273 my $line_class_num = $#line_class;
1274 while (my $line = <$fd>) {
1275 my $long_rev;
1276 my $short_rev;
1277 my $author;
1278 my $time;
1279 my $lineno;
1280 my $data;
1281 my $age;
1282 my $age_str;
1f1ab5f0 1283 my $age_class;
e34ef621
FF
1284
1285 chomp $line;
1286 $line_class_num = ($line_class_num + 1) % $line_class_len;
1287
1288 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
1289 $long_rev = $1;
1290 $author = $2;
1291 $time = $3;
1292 $lineno = $4;
1293 $data = $5;
1294 } else {
1f1ab5f0 1295 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
e34ef621
FF
1296 next;
1297 }
1298 $short_rev = substr ($long_rev, 0, 8);
1299 $age = time () - $time;
1300 $age_str = age_string ($age);
1301 $age_str =~ s/ /&nbsp;/g;
1f1ab5f0 1302 $age_class = age_class($age);
e34ef621
FF
1303 $author = esc_html ($author);
1304 $author =~ s/ /&nbsp;/g;
1305 # escape tabs
1306 while ((my $pos = index($data, "\t")) != -1) {
1307 if (my $count = (8 - ($pos % 8))) {
1308 my $spaces = ' ' x $count;
1309 $data =~ s/\t/$spaces/;
1310 }
1311 }
1312 $data = esc_html ($data);
e34ef621
FF
1313
1314 print <<HTML;
1315 <tr class="$line_class[$line_class_num]">
1f1ab5f0
JN
1316 <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1317 <td class="$age_class">$age_str</td>
e34ef621 1318 <td>$author</td>
1f1ab5f0
JN
1319 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1320 <td class="pre">$data</td>
e34ef621
FF
1321 </tr>
1322HTML
1323 } # while (my $line = <$fd>)
1324 print "</table>\n\n";
1325 close $fd or print "Reading blob failed.\n";
1326 print "</div>";
1327 git_footer_html();
1328}
1329
ede5e100 1330sub git_tags {
df2c37a5 1331 my $head = git_read_head($project);
ede5e100
KS
1332 git_header_html();
1333 print "<div class=\"page_nav\">\n" .
232ff553
KS
1334 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1335 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1336 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1337 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1338 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1339 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
19806691 1340 "<br/>\n" .
ede5e100 1341 "</div>\n";
0db37973 1342 my $taglist = git_read_refs("refs/tags");
ede5e100 1343 print "<div>\n" .
232ff553 1344 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
ede5e100 1345 "</div>\n";
bddec01d
KS
1346 print "<table cellspacing=\"0\">\n";
1347 my $alternate = 0;
ede5e100
KS
1348 if (defined @$taglist) {
1349 foreach my $entry (@$taglist) {
1350 my %tag = %$entry;
d8a20ba9
KS
1351 my $comment_lines = $tag{'comment'};
1352 my $comment = shift @$comment_lines;
1353 if (defined($comment)) {
1354 $comment = chop_str($comment, 30, 5);
1355 }
bddec01d 1356 if ($alternate) {
c994d620 1357 print "<tr class=\"dark\">\n";
bddec01d 1358 } else {
c994d620 1359 print "<tr class=\"light\">\n";
bddec01d
KS
1360 }
1361 $alternate ^= 1;
1362 print "<td><i>$tag{'age'}</i></td>\n" .
19806691 1363 "<td>" .
232ff553 1364 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
40c13813 1365 "<b>" . esc_html($tag{'name'}) . "</b>") .
19806691 1366 "</td>\n" .
d8a20ba9
KS
1367 "<td>";
1368 if (defined($comment)) {
232ff553 1369 print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
d8a20ba9
KS
1370 }
1371 print "</td>\n" .
1372 "<td class=\"link\">";
1373 if ($tag{'type'} eq "tag") {
232ff553 1374 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
d8a20ba9 1375 }
232ff553 1376 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
d8a20ba9 1377 if ($tag{'reftype'} eq "commit") {
232ff553 1378 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
7a9b4c5f 1379 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
19806691
KS
1380 }
1381 print "</td>\n" .
10dba28d 1382 "</tr>";
ede5e100
KS
1383 }
1384 }
bddec01d 1385 print "</table\n>";
ede5e100
KS
1386 git_footer_html();
1387}
1388
d8f1c5c2 1389sub git_heads {
df2c37a5 1390 my $head = git_read_head($project);
0db37973
KS
1391 git_header_html();
1392 print "<div class=\"page_nav\">\n" .
232ff553
KS
1393 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1394 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1395 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1396 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1397 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1398 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
19806691 1399 "<br/>\n" .
0db37973
KS
1400 "</div>\n";
1401 my $taglist = git_read_refs("refs/heads");
1402 print "<div>\n" .
232ff553 1403 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
0db37973 1404 "</div>\n";
bddec01d
KS
1405 print "<table cellspacing=\"0\">\n";
1406 my $alternate = 0;
0db37973
KS
1407 if (defined @$taglist) {
1408 foreach my $entry (@$taglist) {
1409 my %tag = %$entry;
bddec01d 1410 if ($alternate) {
c994d620 1411 print "<tr class=\"dark\">\n";
bddec01d 1412 } else {
c994d620 1413 print "<tr class=\"light\">\n";
bddec01d
KS
1414 }
1415 $alternate ^= 1;
1416 print "<td><i>$tag{'age'}</i></td>\n" .
19806691 1417 "<td>" .
232ff553 1418 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
19806691
KS
1419 "</td>\n" .
1420 "<td class=\"link\">" .
232ff553
KS
1421 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1422 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
19806691 1423 "</td>\n" .
10dba28d 1424 "</tr>";
0db37973
KS
1425 }
1426 }
bddec01d 1427 print "</table\n>";
0db37973
KS
1428 git_footer_html();
1429}
1430
09bd7898
KS
1431sub git_get_hash_by_path {
1432 my $base = shift;
19806691 1433 my $path = shift || return undef;
09bd7898
KS
1434
1435 my $tree = $base;
1436 my @parts = split '/', $path;
1437 while (my $part = shift @parts) {
19806691 1438 open my $fd, "-|", "$gitbin/git-ls-tree $tree" or die_error(undef, "Open git-ls-tree failed.");
09bd7898 1439 my (@entries) = map { chomp; $_ } <$fd>;
19806691 1440 close $fd or return undef;
09bd7898
KS
1441 foreach my $line (@entries) {
1442 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
19806691 1443 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
09bd7898
KS
1444 my $t_mode = $1;
1445 my $t_type = $2;
1446 my $t_hash = $3;
ae786205 1447 my $t_name = validate_input(unquote($4));
09bd7898
KS
1448 if ($t_name eq $part) {
1449 if (!(@parts)) {
1450 return $t_hash;
1451 }
1452 if ($t_type eq "tree") {
1453 $tree = $t_hash;
1454 }
1455 last;
1456 }
1457 }
1458 }
1459}
1460
1461sub git_blob {
1462 if (!defined $hash && defined $file_name) {
df2c37a5 1463 my $base = $hash_base || git_read_head($project);
ae786205 1464 $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file.");
09bd7898 1465 }
5996ca08 1466 my $have_blame = git_get_project_config_bool ('blame');
19806691 1467 open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
12a88f2f 1468 git_header_html();
09bd7898 1469 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
42f7eb94 1470 print "<div class=\"page_nav\">\n" .
232ff553
KS
1471 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1472 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1473 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1474 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1475 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1476 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
9312944d 1477 if (defined $file_name) {
5996ca08
FF
1478 if ($have_blame) {
1479 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
1480 }
1481 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
232ff553 1482 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "<br/>\n";
9312944d 1483 } else {
232ff553 1484 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "<br/>\n";
9312944d
KS
1485 }
1486 print "</div>\n".
1487 "<div>" .
232ff553 1488 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
c07ad4b9 1489 "</div>\n";
09bd7898
KS
1490 } else {
1491 print "<div class=\"page_nav\">\n" .
1492 "<br/><br/></div>\n" .
1493 "<div class=\"title\">$hash</div>\n";
1494 }
1495 if (defined $file_name) {
232ff553 1496 print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
09bd7898 1497 }
c07ad4b9 1498 print "<div class=\"page_body\">\n";
161332a5
KS
1499 my $nr;
1500 while (my $line = <$fd>) {
c07ad4b9 1501 chomp $line;
161332a5 1502 $nr++;
19806691
KS
1503 while ((my $pos = index($line, "\t")) != -1) {
1504 if (my $count = (8 - ($pos % 8))) {
1505 my $spaces = ' ' x $count;
1506 $line =~ s/\t/$spaces/;
1507 }
1508 }
40c13813 1509 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
161332a5 1510 }
19806691 1511 close $fd or print "Reading blob failed.\n";
fbb592a9 1512 print "</div>";
12a88f2f 1513 git_footer_html();
09bd7898
KS
1514}
1515
2d007374
PB
1516sub mimetype_guess_file {
1517 my $filename = shift;
1518 my $mimemap = shift;
1519 -r $mimemap or return undef;
1520
1521 my %mimemap;
1522 open(MIME, $mimemap) or return undef;
1523 while (<MIME>) {
1524 my ($mime, $exts) = split(/\t+/);
1525 my @exts = split(/\s+/, $exts);
1526 foreach my $ext (@exts) {
1527 $mimemap{$ext} = $mime;
1528 }
1529 }
1530 close(MIME);
1531
1532 $filename =~ /\.(.*?)$/;
1533 return $mimemap{$1};
1534}
1535
1536sub mimetype_guess {
1537 my $filename = shift;
1538 my $mime;
1539 $filename =~ /\./ or return undef;
1540
1541 if ($mimetypes_file) {
1542 my $file = $mimetypes_file;
57bd4d35 1543 #$file =~ m#^/# or $file = "$projectroot/$path/$file";
2d007374
PB
1544 $mime = mimetype_guess_file($filename, $file);
1545 }
1546 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1547 return $mime;
1548}
1549
f5aa79d9
JN
1550sub git_blob_plain_mimetype {
1551 my $fd = shift;
1552 my $filename = shift;
1553
1554 # just in case
1555 return $default_blob_plain_mimetype unless $fd;
1556
2d007374
PB
1557 if ($filename) {
1558 my $mime = mimetype_guess($filename);
1559 $mime and return $mime;
1560 }
1561
f5aa79d9
JN
1562 if (-T $fd) {
1563 return 'text/plain' .
1564 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1565 } elsif (! $filename) {
1566 return 'application/octet-stream';
1567 } elsif ($filename =~ m/\.png$/i) {
1568 return 'image/png';
1569 } elsif ($filename =~ m/\.gif$/i) {
1570 return 'image/gif';
1571 } elsif ($filename =~ m/\.jpe?g$/i) {
1572 return 'image/jpeg';
1573 } else {
1574 return 'application/octet-stream';
1575 }
1576}
1577
19806691 1578sub git_blob_plain {
f5aa79d9
JN
1579 open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
1580 my $type = git_blob_plain_mimetype($fd, $file_name);
1581
1582 # save as filename, even when no $file_name is given
1583 my $save_as = "$hash";
9312944d
KS
1584 if (defined $file_name) {
1585 $save_as = $file_name;
f5aa79d9
JN
1586 } elsif ($type =~ m/^text\//) {
1587 $save_as .= '.txt';
9312944d 1588 }
f5aa79d9
JN
1589
1590 print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
19806691 1591 undef $/;
ad14e931 1592 binmode STDOUT, ':raw';
19806691 1593 print <$fd>;
ad14e931 1594 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
19806691
KS
1595 $/ = "\n";
1596 close $fd;
1597}
1598
09bd7898 1599sub git_tree {
b87d78d6 1600 if (!defined $hash) {
df2c37a5 1601 $hash = git_read_head($project);
09bd7898 1602 if (defined $file_name) {
df2c37a5 1603 my $base = $hash_base || $hash;
09bd7898
KS
1604 $hash = git_get_hash_by_path($base, $file_name, "tree");
1605 }
10dba28d 1606 if (!defined $hash_base) {
df2c37a5 1607 $hash_base = $hash;
10dba28d 1608 }
e925f38c 1609 }
232ff553
KS
1610 $/ = "\0";
1611 open my $fd, "-|", "$gitbin/git-ls-tree -z $hash" or die_error(undef, "Open git-ls-tree failed.");
1612 chomp (my (@entries) = <$fd>);
19806691 1613 close $fd or die_error(undef, "Reading tree failed.");
232ff553 1614 $/ = "\n";
d63577da 1615
edde3735
KS
1616 my $refs = read_info_ref();
1617 my $ref = "";
1618 if (defined $refs->{$hash_base}) {
1619 $ref = " <span class=\"tag\">" . esc_html($refs->{$hash_base}) . "</span>";
1620 }
12a88f2f 1621 git_header_html();
09bd7898 1622 my $base_key = "";
09bd7898
KS
1623 my $base = "";
1624 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1625 $base_key = ";hb=$hash_base";
42f7eb94 1626 print "<div class=\"page_nav\">\n" .
232ff553
KS
1627 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1628 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash_base")}, "shortlog") .
1629 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash_base")}, "log") .
1630 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1631 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
19806691 1632 " | tree" .
6191f8e1
KS
1633 "<br/><br/>\n" .
1634 "</div>\n";
d63577da 1635 print "<div>\n" .
edde3735 1636 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
d63577da
KS
1637 "</div>\n";
1638 } else {
1639 print "<div class=\"page_nav\">\n";
1640 print "<br/><br/></div>\n";
1641 print "<div class=\"title\">$hash</div>\n";
1642 }
09bd7898 1643 if (defined $file_name) {
232ff553
KS
1644 $base = esc_html("$file_name/");
1645 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
09bd7898 1646 } else {
19806691 1647 print "<div class=\"page_path\"><b>/</b></div>\n";
09bd7898 1648 }
fbb592a9 1649 print "<div class=\"page_body\">\n";
42f7eb94 1650 print "<table cellspacing=\"0\">\n";
bddec01d 1651 my $alternate = 0;
161332a5 1652 foreach my $line (@entries) {
c068cff1 1653 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
19806691 1654 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
d767d59c 1655 my $t_mode = $1;
161332a5
KS
1656 my $t_type = $2;
1657 my $t_hash = $3;
232ff553 1658 my $t_name = validate_input($4);
bddec01d 1659 if ($alternate) {
c994d620 1660 print "<tr class=\"dark\">\n";
bddec01d 1661 } else {
c994d620 1662 print "<tr class=\"light\">\n";
bddec01d
KS
1663 }
1664 $alternate ^= 1;
1f1ab5f0 1665 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
161332a5 1666 if ($t_type eq "blob") {
10dba28d 1667 print "<td class=\"list\">" .
232ff553 1668 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name"), -class => "list"}, esc_html($t_name)) .
c994d620
KS
1669 "</td>\n" .
1670 "<td class=\"link\">" .
232ff553 1671 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
e34ef621 1672# " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") .
232ff553 1673 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") .
25b7c18e 1674 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") .
42f7eb94 1675 "</td>\n";
161332a5 1676 } elsif ($t_type eq "tree") {
10dba28d 1677 print "<td class=\"list\">" .
232ff553 1678 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
19806691 1679 "</td>\n" .
c994d620 1680 "<td class=\"link\">" .
232ff553 1681 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
85b7cfb1 1682 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") .
c994d620 1683 "</td>\n";
161332a5 1684 }
42f7eb94 1685 print "</tr>\n";
161332a5 1686 }
42f7eb94
KS
1687 print "</table>\n" .
1688 "</div>";
12a88f2f 1689 git_footer_html();
09bd7898
KS
1690}
1691
1692sub git_rss {
19806691 1693 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
df2c37a5 1694 open my $fd, "-|", "$gitbin/git-rev-list --max-count=150 " . git_read_head($project) or die_error(undef, "Open failed.");
d51e902a 1695 my (@revlist) = map { chomp; $_ } <$fd>;
19806691 1696 close $fd or die_error(undef, "Reading rev-list failed.");
034df39e
KS
1697 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1698 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
19806691 1699 "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
034df39e
KS
1700 print "<channel>\n";
1701 print "<title>$project</title>\n".
40c13813 1702 "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
034df39e
KS
1703 "<description>$project log</description>\n".
1704 "<language>en</language>\n";
1705
d8d17b5d
KS
1706 for (my $i = 0; $i <= $#revlist; $i++) {
1707 my $commit = $revlist[$i];
09bd7898 1708 my %co = git_read_commit($commit);
d8d17b5d
KS
1709 # we read 150, we always show 30 and the ones more recent than 48 hours
1710 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
1711 last;
1712 }
19806691 1713 my %cd = date_str($co{'committer_epoch'});
d8d17b5d
KS
1714 open $fd, "-|", "$gitbin/git-diff-tree -r $co{'parent'} $co{'id'}" or next;
1715 my @difftree = map { chomp; $_ } <$fd>;
1716 close $fd or next;
034df39e 1717 print "<item>\n" .
19806691 1718 "<title>" .
40c13813 1719 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
19806691 1720 "</title>\n" .
40c13813 1721 "<author>" . esc_html($co{'author'}) . "</author>\n" .
19806691 1722 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
40c13813
KS
1723 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
1724 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
1725 "<description>" . esc_html($co{'title'}) . "</description>\n" .
19806691
KS
1726 "<content:encoded>" .
1727 "<![CDATA[\n";
034df39e
KS
1728 my $comment = $co{'comment'};
1729 foreach my $line (@$comment) {
40c13813 1730 $line = decode("utf8", $line, Encode::FB_DEFAULT);
19806691 1731 print "$line<br/>\n";
161332a5 1732 }
d8d17b5d
KS
1733 print "<br/>\n";
1734 foreach my $line (@difftree) {
1735 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1736 next;
1737 }
232ff553
KS
1738 my $file = validate_input(unquote($7));
1739 $file = decode("utf8", $file, Encode::FB_DEFAULT);
d8d17b5d
KS
1740 print "$file<br/>\n";
1741 }
19806691
KS
1742 print "]]>\n" .
1743 "</content:encoded>\n" .
034df39e
KS
1744 "</item>\n";
1745 }
1746 print "</channel></rss>";
09bd7898
KS
1747}
1748
c994d620
KS
1749sub git_opml {
1750 my @list = git_read_projects();
1751
1752 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1753 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1754 "<opml version=\"1.0\">\n".
1755 "<head>".
1756 " <title>Git OPML Export</title>\n".
1757 "</head>\n".
1758 "<body>\n".
1759 "<outline text=\"git RSS feeds\">\n";
1760
1761 foreach my $pr (@list) {
1762 my %proj = %$pr;
df2c37a5 1763 my $head = git_read_head($proj{'path'});
c994d620
KS
1764 if (!defined $head) {
1765 next;
1766 }
4fac5294 1767 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
c994d620
KS
1768 my %co = git_read_commit($head);
1769 if (!%co) {
1770 next;
1771 }
1772
40c13813 1773 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
8adc4bd4
JN
1774 my $rss = "$my_url?p=$proj{'path'};a=rss";
1775 my $html = "$my_url?p=$proj{'path'};a=summary";
c994d620
KS
1776 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
1777 }
1778 print "</outline>\n".
1779 "</body>\n".
1780 "</opml>\n";
1781}
1782
09bd7898 1783sub git_log {
df2c37a5 1784 my $head = git_read_head($project);
0db37973 1785 if (!defined $hash) {
19806691 1786 $hash = $head;
0db37973 1787 }
ea4a6df4
KS
1788 if (!defined $page) {
1789 $page = 0;
b87d78d6 1790 }
6a928415 1791 my $refs = read_info_ref();
034df39e
KS
1792 git_header_html();
1793 print "<div class=\"page_nav\">\n";
232ff553
KS
1794 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1795 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
19806691 1796 " | log" .
232ff553
KS
1797 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
1798 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
1799 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
ea4a6df4
KS
1800
1801 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1802 open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
1803 my (@revlist) = map { chomp; $_ } <$fd>;
1804 close $fd;
1805
1806 if ($hash ne $head || $page) {
232ff553 1807 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "HEAD");
ea4a6df4
KS
1808 } else {
1809 print "HEAD";
1810 }
1811 if ($page > 0) {
c994d620 1812 print " &sdot; " .
232ff553 1813 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
ea4a6df4
KS
1814 } else {
1815 print " &sdot; prev";
1816 }
1817 if ($#revlist >= (100 * ($page+1)-1)) {
c994d620 1818 print " &sdot; " .
232ff553 1819 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
ea4a6df4
KS
1820 } else {
1821 print " &sdot; next";
1822 }
ede5e100 1823 print "<br/>\n" .
034df39e 1824 "</div>\n";
b87d78d6 1825 if (!@revlist) {
19806691 1826 print "<div>\n" .
232ff553 1827 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
19806691 1828 "</div>\n";
0db37973 1829 my %co = git_read_commit($hash);
e925f38c 1830 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
161332a5 1831 }
c994d620
KS
1832 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1833 my $commit = $revlist[$i];
6a928415
KS
1834 my $ref = "";
1835 if (defined $refs->{$commit}) {
edde3735 1836 $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
6a928415 1837 }
09bd7898 1838 my %co = git_read_commit($commit);
b87d78d6 1839 next if !%co;
034df39e
KS
1840 my %ad = date_str($co{'author_epoch'});
1841 print "<div>\n" .
232ff553 1842 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "title"},
6a928415
KS
1843 "<span class=\"age\">$co{'age_string'}</span>" . esc_html($co{'title'}) . $ref) . "\n";
1844 print "</div>\n";
034df39e
KS
1845 print "<div class=\"title_text\">\n" .
1846 "<div class=\"log_link\">\n" .
232ff553
KS
1847 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1848 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
eb28240b 1849 "<br/>\n" .
034df39e 1850 "</div>\n" .
40c13813 1851 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
034df39e
KS
1852 "</div>\n" .
1853 "<div class=\"log_body\">\n";
1854 my $comment = $co{'comment'};
09bd7898 1855 my $empty = 0;
034df39e 1856 foreach my $line (@$comment) {
10dba28d 1857 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
09bd7898
KS
1858 next;
1859 }
1860 if ($line eq "") {
1861 if ($empty) {
1862 next;
1863 }
1864 $empty = 1;
1865 } else {
1866 $empty = 0;
1867 }
f49201a9 1868 print format_log_line_html($line) . "<br/>\n";
034df39e 1869 }
09bd7898
KS
1870 if (!$empty) {
1871 print "<br/>\n";
1872 }
1873 print "</div>\n";
e334d18c 1874 }
034df39e 1875 git_footer_html();
09bd7898
KS
1876}
1877
1878sub git_commit {
1879 my %co = git_read_commit($hash);
034df39e 1880 if (!%co) {
09bd7898 1881 die_error(undef, "Unknown commit object.");
d63577da 1882 }
185f09e5
KS
1883 my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1884 my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
161332a5 1885
6191f8e1 1886 my @difftree;
19806691 1887 my $root = "";
d8a20ba9
KS
1888 my $parent = $co{'parent'};
1889 if (!defined $parent) {
19806691 1890 $root = " --root";
d8a20ba9 1891 $parent = "";
6191f8e1 1892 }
d8a20ba9 1893 open my $fd, "-|", "$gitbin/git-diff-tree -r -M $root $parent $hash" or die_error(undef, "Open failed.");
19806691
KS
1894 @difftree = map { chomp; $_ } <$fd>;
1895 close $fd or die_error(undef, "Reading diff-tree failed.");
11044297
KS
1896
1897 # non-textual hash id's can be cached
1898 my $expires;
1899 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1900 $expires = "+1d";
1901 }
edde3735
KS
1902 my $refs = read_info_ref();
1903 my $ref = "";
d3a6fd94
KS
1904 if (defined $refs->{$co{'id'}}) {
1905 $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
edde3735 1906 }
11044297 1907 git_header_html(undef, $expires);
42f7eb94 1908 print "<div class=\"page_nav\">\n" .
232ff553
KS
1909 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1910 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
1911 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
19806691 1912 " | commit";
42f7eb94 1913 if (defined $co{'parent'}) {
232ff553 1914 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff");
42f7eb94 1915 }
232ff553 1916 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . "\n" .
ff7669a5 1917 "<br/><br/></div>\n";
b87d78d6
KS
1918 if (defined $co{'parent'}) {
1919 print "<div>\n" .
edde3735 1920 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
b87d78d6
KS
1921 "</div>\n";
1922 } else {
1923 print "<div>\n" .
232ff553 1924 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
b87d78d6
KS
1925 "</div>\n";
1926 }
6191f8e1 1927 print "<div class=\"title_text\">\n" .
b87d78d6 1928 "<table cellspacing=\"0\">\n";
40c13813 1929 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
bddec01d
KS
1930 "<tr>" .
1931 "<td></td><td> $ad{'rfc2822'}";
927dcec4 1932 if ($ad{'hour_local'} < 6) {
1f1ab5f0 1933 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
b87d78d6
KS
1934 } else {
1935 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1936 }
bddec01d
KS
1937 print "</td>" .
1938 "</tr>\n";
40c13813 1939 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
e925f38c 1940 print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1f1ab5f0 1941 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
bddec01d
KS
1942 print "<tr>" .
1943 "<td>tree</td>" .
1f1ab5f0 1944 "<td class=\"sha1\">" .
232ff553 1945 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
19806691 1946 "</td>" .
232ff553 1947 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
bddec01d
KS
1948 "</td>" .
1949 "</tr>\n";
8adc4bd4 1950 my $parents = $co{'parents'};
3e029299 1951 foreach my $par (@$parents) {
bddec01d
KS
1952 print "<tr>" .
1953 "<td>parent</td>" .
1f1ab5f0 1954 "<td class=\"sha1\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
bddec01d 1955 "<td class=\"link\">" .
232ff553
KS
1956 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
1957 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
bddec01d
KS
1958 "</td>" .
1959 "</tr>\n";
3e029299 1960 }
7a9b4c5f 1961 print "</table>".
b87d78d6 1962 "</div>\n";
fbb592a9 1963 print "<div class=\"page_body\">\n";
3e029299 1964 my $comment = $co{'comment'};
09bd7898
KS
1965 my $empty = 0;
1966 my $signed = 0;
3e029299 1967 foreach my $line (@$comment) {
09bd7898
KS
1968 # print only one empty line
1969 if ($line eq "") {
1970 if ($empty || $signed) {
1971 next;
1972 }
1973 $empty = 1;
1974 } else {
1975 $empty = 0;
1976 }
10dba28d 1977 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
09bd7898 1978 $signed = 1;
1f1ab5f0 1979 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
3e029299 1980 } else {
09bd7898 1981 $signed = 0;
f49201a9 1982 print format_log_line_html($line) . "<br/>\n";
3e029299
KS
1983 }
1984 }
927dcec4 1985 print "</div>\n";
09bd7898 1986 print "<div class=\"list_head\">\n";
6191f8e1 1987 if ($#difftree > 10) {
09bd7898 1988 print(($#difftree + 1) . " files changed:\n");
6191f8e1 1989 }
09bd7898 1990 print "</div>\n";
1f1ab5f0 1991 print "<table class=\"diff_tree\">\n";
bddec01d 1992 my $alternate = 0;
161332a5 1993 foreach my $line (@difftree) {
19806691
KS
1994 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1995 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
d8a20ba9
KS
1996 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1997 next;
1998 }
19806691
KS
1999 my $from_mode = $1;
2000 my $to_mode = $2;
2001 my $from_id = $3;
2002 my $to_id = $4;
2003 my $status = $5;
ea4a6df4 2004 my $similarity = $6;
232ff553 2005 my $file = validate_input(unquote($7));
bddec01d 2006 if ($alternate) {
c994d620 2007 print "<tr class=\"dark\">\n";
bddec01d 2008 } else {
c994d620 2009 print "<tr class=\"light\">\n";
bddec01d
KS
2010 }
2011 $alternate ^= 1;
f6375b24 2012 if ($status eq "A") {
10dba28d 2013 my $mode_chng = "";
19806691
KS
2014 if (S_ISREG(oct $to_mode)) {
2015 $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
10dba28d
KS
2016 }
2017 print "<td>" .
232ff553 2018 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
1f1ab5f0 2019 "<td><span class=\"file_status new\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
232ff553 2020 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") . "</td>\n";
19806691 2021 } elsif ($status eq "D") {
10dba28d 2022 print "<td>" .
232ff553 2023 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
1f1ab5f0 2024 "<td><span class=\"file_status deleted\">[deleted " . file_type($from_mode). "]</span></td>\n" .
10dba28d 2025 "<td class=\"link\">" .
232ff553
KS
2026 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") .
2027 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") .
10dba28d 2028 "</td>\n"
19806691 2029 } elsif ($status eq "M" || $status eq "T") {
10dba28d
KS
2030 my $mode_chnge = "";
2031 if ($from_mode != $to_mode) {
1f1ab5f0 2032 $mode_chnge = " <span class=\"file_status mode_chnge\">[changed";
10dba28d
KS
2033 if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
2034 $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
b87d78d6 2035 }
10dba28d
KS
2036 if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
2037 if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
2038 $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
2039 } elsif (S_ISREG($to_mode)) {
2040 $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
b87d78d6 2041 }
9cd3d988 2042 }
10dba28d
KS
2043 $mode_chnge .= "]</span>\n";
2044 }
2045 print "<td>";
2046 if ($to_id ne $from_id) {
232ff553 2047 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
10dba28d 2048 } else {
232ff553 2049 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
161332a5 2050 }
10dba28d
KS
2051 print "</td>\n" .
2052 "<td>$mode_chnge</td>\n" .
2053 "<td class=\"link\">";
232ff553 2054 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob");
10dba28d 2055 if ($to_id ne $from_id) {
232ff553 2056 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff");
10dba28d 2057 }
232ff553 2058 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash;f=$file")}, "history") . "\n";
10dba28d 2059 print "</td>\n";
dcea8d0b
KS
2060 } elsif ($status eq "R") {
2061 my ($from_file, $to_file) = split "\t", $file;
2062 my $mode_chng = "";
2063 if ($from_mode != $to_mode) {
2064 $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
2065 }
2066 print "<td>" .
232ff553 2067 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file"), -class => "list"}, esc_html($to_file)) . "</td>\n" .
1f1ab5f0 2068 "<td><span class=\"file_status moved\">[moved from " .
232ff553 2069 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$from_file"), -class => "list"}, esc_html($from_file)) .
ea4a6df4 2070 " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
dcea8d0b 2071 "<td class=\"link\">" .
232ff553 2072 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
dcea8d0b 2073 if ($to_id ne $from_id) {
232ff553 2074 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file")}, "diff");
dcea8d0b
KS
2075 }
2076 print "</td>\n";
161332a5 2077 }
10dba28d 2078 print "</tr>\n";
161332a5 2079 }
bddec01d 2080 print "</table>\n";
12a88f2f 2081 git_footer_html();
09bd7898
KS
2082}
2083
2084sub git_blobdiff {
19806691 2085 mkdir($git_temp, 0700);
12a88f2f 2086 git_header_html();
09bd7898 2087 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
42f7eb94 2088 print "<div class=\"page_nav\">\n" .
232ff553
KS
2089 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2090 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2091 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
2092 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
2093 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
2094 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") .
19806691 2095 "<br/>\n";
232ff553 2096 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain") .
09bd7898
KS
2097 "</div>\n";
2098 print "<div>\n" .
232ff553 2099 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" .
09bd7898
KS
2100 "</div>\n";
2101 } else {
2102 print "<div class=\"page_nav\">\n" .
2103 "<br/><br/></div>\n" .
2104 "<div class=\"title\">$hash vs $hash_parent</div>\n";
2105 }
2106 if (defined $file_name) {
232ff553 2107 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
09bd7898 2108 }
9cd3d988 2109 print "<div class=\"page_body\">\n" .
c07ad4b9 2110 "<div class=\"diff_info\">blob:" .
232ff553 2111 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
2735983d 2112 " -> blob:" .
232ff553 2113 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
c07ad4b9 2114 "</div>\n";
19806691 2115 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
c07ad4b9 2116 print "</div>";
12a88f2f 2117 git_footer_html();
09bd7898
KS
2118}
2119
19806691
KS
2120sub git_blobdiff_plain {
2121 mkdir($git_temp, 0700);
2122 print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2123 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2124}
2125
09bd7898 2126sub git_commitdiff {
19806691 2127 mkdir($git_temp, 0700);
09bd7898 2128 my %co = git_read_commit($hash);
034df39e 2129 if (!%co) {
09bd7898 2130 die_error(undef, "Unknown commit object.");
d63577da 2131 }
bddec01d
KS
2132 if (!defined $hash_parent) {
2133 $hash_parent = $co{'parent'};
2134 }
19806691 2135 open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
4c02e3c5 2136 my (@difftree) = map { chomp; $_ } <$fd>;
19806691 2137 close $fd or die_error(undef, "Reading diff-tree failed.");
161332a5 2138
11044297
KS
2139 # non-textual hash id's can be cached
2140 my $expires;
2141 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2142 $expires = "+1d";
2143 }
edde3735
KS
2144 my $refs = read_info_ref();
2145 my $ref = "";
d3a6fd94
KS
2146 if (defined $refs->{$co{'id'}}) {
2147 $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
edde3735 2148 }
11044297 2149 git_header_html(undef, $expires);
42f7eb94 2150 print "<div class=\"page_nav\">\n" .
232ff553
KS
2151 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2152 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash")}, "shortlog") .
2153 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2154 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
19806691 2155 " | commitdiff" .
232ff553
KS
2156 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") . "<br/>\n";
2157 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain") . "\n" .
19806691 2158 "</div>\n";
9cd3d988 2159 print "<div>\n" .
edde3735 2160 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
9cd3d988 2161 "</div>\n";
c07ad4b9 2162 print "<div class=\"page_body\">\n";
0db37973
KS
2163 my $comment = $co{'comment'};
2164 my $empty = 0;
2165 my $signed = 0;
fa378499 2166 my @log = @$comment;
a4d26ef0 2167 # remove first and empty lines after that
fa378499 2168 shift @log;
a4d26ef0
KS
2169 while (defined $log[0] && $log[0] eq "") {
2170 shift @log;
2171 }
fa378499 2172 foreach my $line (@log) {
10dba28d 2173 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
0db37973
KS
2174 next;
2175 }
2176 if ($line eq "") {
2177 if ($empty) {
2178 next;
2179 }
2180 $empty = 1;
2181 } else {
2182 $empty = 0;
2183 }
f49201a9 2184 print format_log_line_html($line) . "<br/>\n";
0db37973
KS
2185 }
2186 print "<br/>\n";
4c02e3c5 2187 foreach my $line (@difftree) {
19806691
KS
2188 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2189 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2190 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2191 my $from_mode = $1;
2192 my $to_mode = $2;
2193 my $from_id = $3;
2194 my $to_id = $4;
2195 my $status = $5;
232ff553 2196 my $file = validate_input(unquote($6));
f6375b24 2197 if ($status eq "A") {
19806691 2198 print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
232ff553 2199 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
19806691
KS
2200 "</div>\n";
2201 git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2202 } elsif ($status eq "D") {
2203 print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
232ff553 2204 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) . "(deleted)" .
19806691
KS
2205 "</div>\n";
2206 git_diff_print($from_id, "a/$file", undef, "/dev/null");
2207 } elsif ($status eq "M") {
2208 if ($from_id ne $to_id) {
2209 print "<div class=\"diff_info\">" .
232ff553 2210 file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) .
19806691 2211 " -> " .
232ff553 2212 file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
19806691
KS
2213 print "</div>\n";
2214 git_diff_print($from_id, "a/$file", $to_id, "b/$file");
4c02e3c5
KS
2215 }
2216 }
161332a5 2217 }
c07ad4b9
KS
2218 print "<br/>\n" .
2219 "</div>";
12a88f2f 2220 git_footer_html();
09bd7898
KS
2221}
2222
19806691
KS
2223sub git_commitdiff_plain {
2224 mkdir($git_temp, 0700);
2225 open my $fd, "-|", "$gitbin/git-diff-tree -r $hash_parent $hash" or die_error(undef, "Open failed.");
2226 my (@difftree) = map { chomp; $_ } <$fd>;
2227 close $fd or die_error(undef, "Reading diff-tree failed.");
2228
1b1cd421
KS
2229 # try to figure out the next tag after this commit
2230 my $tagname;
4df11910 2231 my $refs = read_info_ref("tags");
1b1cd421 2232 open $fd, "-|", "$gitbin/git-rev-list HEAD";
6a928415
KS
2233 chomp (my (@commits) = <$fd>);
2234 close $fd;
2235 foreach my $commit (@commits) {
2236 if (defined $refs->{$commit}) {
2237 $tagname = $refs->{$commit}
1b1cd421
KS
2238 }
2239 if ($commit eq $hash) {
2240 last;
2241 }
2242 }
1b1cd421 2243
9312944d 2244 print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
c994d620
KS
2245 my %co = git_read_commit($hash);
2246 my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
c994d620 2247 my $comment = $co{'comment'};
1b1cd421 2248 print "From: $co{'author'}\n" .
c994d620 2249 "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
1b1cd421
KS
2250 "Subject: $co{'title'}\n";
2251 if (defined $tagname) {
2252 print "X-Git-Tag: $tagname\n";
2253 }
2254 print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
c994d620 2255 "\n";
1b1cd421 2256
c994d620 2257 foreach my $line (@$comment) {;
c2488d06 2258 print "$line\n";
c994d620 2259 }
1b1cd421
KS
2260 print "---\n\n";
2261
19806691
KS
2262 foreach my $line (@difftree) {
2263 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2264 my $from_id = $3;
2265 my $to_id = $4;
2266 my $status = $5;
2267 my $file = $6;
f6375b24 2268 if ($status eq "A") {
19806691
KS
2269 git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2270 } elsif ($status eq "D") {
2271 git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2272 } elsif ($status eq "M") {
2273 git_diff_print($from_id, "a/$file", $to_id, "b/$file", "plain");
2274 }
2275 }
2276}
2277
09bd7898 2278sub git_history {
b87d78d6 2279 if (!defined $hash) {
df2c37a5 2280 $hash = git_read_head($project);
09bd7898
KS
2281 }
2282 my %co = git_read_commit($hash);
2283 if (!%co) {
2284 die_error(undef, "Unknown commit object.");
2ae100df 2285 }
6a928415 2286 my $refs = read_info_ref();
d51e902a 2287 git_header_html();
42f7eb94 2288 print "<div class=\"page_nav\">\n" .
232ff553
KS
2289 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2290 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2291 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
2292 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2293 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2294 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
09bd7898
KS
2295 "<br/><br/>\n" .
2296 "</div>\n";
d63577da 2297 print "<div>\n" .
232ff553 2298 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
820e4f6b 2299 "</div>\n";
232ff553 2300 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b><br/></div>\n";
10dba28d 2301
cdd4037d
JH
2302 open my $fd, "-|",
2303 "$gitbin/git-rev-list --full-history $hash -- \'$file_name\'";
bddec01d
KS
2304 print "<table cellspacing=\"0\">\n";
2305 my $alternate = 0;
b87d78d6 2306 while (my $line = <$fd>) {
d05c19ee 2307 if ($line =~ m/^([0-9a-fA-F]{40})/){
cdd4037d 2308 my $commit = $1;
09bd7898 2309 my %co = git_read_commit($commit);
b87d78d6
KS
2310 if (!%co) {
2311 next;
2312 }
6a928415
KS
2313 my $ref = "";
2314 if (defined $refs->{$commit}) {
edde3735 2315 $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
6a928415 2316 }
bddec01d 2317 if ($alternate) {
c994d620 2318 print "<tr class=\"dark\">\n";
bddec01d 2319 } else {
c994d620 2320 print "<tr class=\"light\">\n";
bddec01d
KS
2321 }
2322 $alternate ^= 1;
71be1e79 2323 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
40c13813 2324 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
232ff553 2325 "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
4df11910 2326 esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
10dba28d 2327 "<td class=\"link\">" .
232ff553
KS
2328 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2329 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2330 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=$commit;f=$file_name")}, "blob");
42f7eb94
KS
2331 my $blob = git_get_hash_by_path($hash, $file_name);
2332 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2333 if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
c994d620 2334 print " | " .
232ff553 2335 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
c994d620 2336 "diff to current");
42f7eb94 2337 }
10dba28d
KS
2338 print "</td>\n" .
2339 "</tr>\n";
2ae100df 2340 }
d51e902a 2341 }
bddec01d 2342 print "</table>\n";
b87d78d6 2343 close $fd;
d51e902a 2344 git_footer_html();
161332a5 2345}
19806691
KS
2346
2347sub git_search {
2348 if (!defined $searchtext) {
2349 die_error("", "Text field empty.");
2350 }
2351 if (!defined $hash) {
df2c37a5 2352 $hash = git_read_head($project);
19806691
KS
2353 }
2354 my %co = git_read_commit($hash);
2355 if (!%co) {
2356 die_error(undef, "Unknown commit object.");
2357 }
c994d620
KS
2358 # pickaxe may take all resources of your box and run for several minutes
2359 # with every query - so decide by yourself how public you make this feature :)
2360 my $commit_search = 1;
2361 my $author_search = 0;
2362 my $committer_search = 0;
2363 my $pickaxe_search = 0;
2364 if ($searchtext =~ s/^author\\://i) {
2365 $author_search = 1;
2366 } elsif ($searchtext =~ s/^committer\\://i) {
2367 $committer_search = 1;
2368 } elsif ($searchtext =~ s/^pickaxe\\://i) {
2369 $commit_search = 0;
2370 $pickaxe_search = 1;
2371 }
19806691
KS
2372 git_header_html();
2373 print "<div class=\"page_nav\">\n" .
232ff553
KS
2374 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary;h=$hash")}, "summary") .
2375 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2376 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2377 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2378 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2379 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
19806691
KS
2380 "<br/><br/>\n" .
2381 "</div>\n";
2382
2383 print "<div>\n" .
232ff553 2384 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
19806691
KS
2385 "</div>\n";
2386 print "<table cellspacing=\"0\">\n";
19806691 2387 my $alternate = 0;
c994d620
KS
2388 if ($commit_search) {
2389 $/ = "\0";
25f422fb 2390 open my $fd, "-|", "$gitbin/git-rev-list --header --parents $hash" or next;
c994d620
KS
2391 while (my $commit_text = <$fd>) {
2392 if (!grep m/$searchtext/i, $commit_text) {
2393 next;
19806691 2394 }
c994d620
KS
2395 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2396 next;
19806691 2397 }
c994d620 2398 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
19806691
KS
2399 next;
2400 }
c994d620 2401 my @commit_lines = split "\n", $commit_text;
25f422fb 2402 my %co = git_read_commit(undef, \@commit_lines);
c994d620
KS
2403 if (!%co) {
2404 next;
2405 }
2406 if ($alternate) {
2407 print "<tr class=\"dark\">\n";
2408 } else {
2409 print "<tr class=\"light\">\n";
2410 }
2411 $alternate ^= 1;
71be1e79 2412 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
40c13813 2413 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
c994d620 2414 "<td>" .
232ff553 2415 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" . esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
c994d620
KS
2416 my $comment = $co{'comment'};
2417 foreach my $line (@$comment) {
2418 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
40c13813 2419 my $lead = esc_html($1) || "";
c994d620 2420 $lead = chop_str($lead, 30, 10);
40c13813
KS
2421 my $match = esc_html($2) || "";
2422 my $trail = esc_html($3) || "";
c994d620 2423 $trail = chop_str($trail, 30, 10);
1f1ab5f0 2424 my $text = "$lead<span class=\"match\">$match</span>$trail";
c994d620 2425 print chop_str($text, 80, 5) . "<br/>\n";
19806691 2426 }
c994d620
KS
2427 }
2428 print "</td>\n" .
2429 "<td class=\"link\">" .
232ff553
KS
2430 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2431 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
c994d620
KS
2432 print "</td>\n" .
2433 "</tr>\n";
2434 }
2435 close $fd;
2436 }
2437
2438 if ($pickaxe_search) {
2439 $/ = "\n";
a9e60b7d 2440 open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -S\'$searchtext\'";
c994d620
KS
2441 undef %co;
2442 my @files;
2443 while (my $line = <$fd>) {
2444 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2445 my %set;
2446 $set{'file'} = $6;
2447 $set{'from_id'} = $3;
2448 $set{'to_id'} = $4;
2449 $set{'id'} = $set{'to_id'};
2450 if ($set{'id'} =~ m/0{40}/) {
2451 $set{'id'} = $set{'from_id'};
19806691 2452 }
c994d620
KS
2453 if ($set{'id'} =~ m/0{40}/) {
2454 next;
2455 }
2456 push @files, \%set;
53b89d8d 2457 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
c994d620
KS
2458 if (%co) {
2459 if ($alternate) {
2460 print "<tr class=\"dark\">\n";
2461 } else {
2462 print "<tr class=\"light\">\n";
2463 }
2464 $alternate ^= 1;
71be1e79 2465 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
40c13813 2466 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
c994d620 2467 "<td>" .
232ff553 2468 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
40c13813 2469 esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
c994d620
KS
2470 while (my $setref = shift @files) {
2471 my %set = %$setref;
232ff553 2472 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
1f1ab5f0 2473 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
c994d620
KS
2474 "<br/>\n";
2475 }
2476 print "</td>\n" .
2477 "<td class=\"link\">" .
232ff553
KS
2478 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2479 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
c994d620
KS
2480 print "</td>\n" .
2481 "</tr>\n";
2482 }
2483 %co = git_read_commit($1);
19806691 2484 }
19806691 2485 }
c994d620 2486 close $fd;
19806691
KS
2487 }
2488 print "</table>\n";
19806691
KS
2489 git_footer_html();
2490}
2491
2492sub git_shortlog {
df2c37a5 2493 my $head = git_read_head($project);
19806691
KS
2494 if (!defined $hash) {
2495 $hash = $head;
2496 }
ea4a6df4
KS
2497 if (!defined $page) {
2498 $page = 0;
2499 }
6a928415 2500 my $refs = read_info_ref();
19806691
KS
2501 git_header_html();
2502 print "<div class=\"page_nav\">\n" .
232ff553 2503 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
19806691 2504 " | shortlog" .
232ff553
KS
2505 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2506 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2507 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2508 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
ea4a6df4
KS
2509
2510 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
19806691
KS
2511 open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
2512 my (@revlist) = map { chomp; $_ } <$fd>;
2513 close $fd;
ea4a6df4
KS
2514
2515 if ($hash ne $head || $page) {
232ff553 2516 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "HEAD");
ea4a6df4
KS
2517 } else {
2518 print "HEAD";
2519 }
2520 if ($page > 0) {
c994d620 2521 print " &sdot; " .
232ff553 2522 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
ea4a6df4
KS
2523 } else {
2524 print " &sdot; prev";
2525 }
2526 if ($#revlist >= (100 * ($page+1)-1)) {
c994d620 2527 print " &sdot; " .
232ff553 2528 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
ea4a6df4
KS
2529 } else {
2530 print " &sdot; next";
2531 }
2532 print "<br/>\n" .
2533 "</div>\n";
19806691 2534 print "<div>\n" .
232ff553 2535 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
19806691
KS
2536 "</div>\n";
2537 print "<table cellspacing=\"0\">\n";
19806691 2538 my $alternate = 0;
ea4a6df4
KS
2539 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2540 my $commit = $revlist[$i];
4df11910
KS
2541 my $ref = "";
2542 if (defined $refs->{$commit}) {
edde3735 2543 $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
4df11910 2544 }
19806691
KS
2545 my %co = git_read_commit($commit);
2546 my %ad = date_str($co{'author_epoch'});
2547 if ($alternate) {
c994d620 2548 print "<tr class=\"dark\">\n";
19806691 2549 } else {
c994d620 2550 print "<tr class=\"light\">\n";
19806691
KS
2551 }
2552 $alternate ^= 1;
71be1e79 2553 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
40c13813 2554 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
c24fd7b7
KS
2555 "<td>";
2556 if (length($co{'title_short'}) < length($co{'title'})) {
232ff553 2557 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
4df11910 2558 "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
c24fd7b7 2559 } else {
232ff553 2560 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
4df11910 2561 "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
6a928415 2562 }
c24fd7b7 2563 print "</td>\n" .
19806691 2564 "<td class=\"link\">" .
232ff553
KS
2565 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2566 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
19806691
KS
2567 "</td>\n" .
2568 "</tr>";
2569 }
c994d620
KS
2570 if ($#revlist >= (100 * ($page+1)-1)) {
2571 print "<tr>\n" .
2572 "<td>" .
232ff553 2573 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -title => "Alt-n"}, "next") .
c994d620
KS
2574 "</td>\n" .
2575 "</tr>\n";
2576 }
19806691
KS
2577 print "</table\n>";
2578 git_footer_html();
2579}