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