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