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