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