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