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