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