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