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