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