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
19my $cgi = new CGI;
20my $version = "267";
21my $my_url = $cgi->url();
22my $my_uri = $cgi->url(-absolute => 1);
23my $rss_link = "";
24
25# location of the git-core binaries
26my $gitbin = "/usr/bin";
27
28# absolute fs-path which will be prepended to the project path
29#my $projectroot = "/pub/scm";
30my $projectroot = "/home/kay/public_html/pub/scm";
31
32# version of the git-core binaries
33my $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
41my $git_temp = "/tmp/gitweb";
42
43# target of the home link on top of all pages
44my $home_link = $my_uri;
45
46# html text to include at home page
47my $home_text = "indextext.html";
48
49# URI of default stylesheet
50my $stylesheet = "gitweb.css";
51
52# source of projects list
53#my $projects_list = $projectroot;
54my $projects_list = "index/index.aux";
55
56# default blob_plain mimetype and default charset for text/plain blob
57my $default_blob_plain_mimetype = 'text/plain';
58my $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)
62my $mimetypes_file = undef;
63
64
65# input validation and dispatch
66my $action = $cgi->param('a');
67if (defined $action) {
68 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
69 undef $action;
70 die_error(undef, "Invalid action parameter.");
71 }
72 if ($action eq "git-logo.png") {
73 git_logo();
74 exit;
75 } elsif ($action eq "opml") {
76 git_opml();
77 exit;
78 }
79}
80
81my $order = $cgi->param('o');
82if (defined $order) {
83 if ($order =~ m/[^0-9a-zA-Z_]/) {
84 undef $order;
85 die_error(undef, "Invalid order parameter.");
86 }
87}
88
89my $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
90if (defined $project) {
91 $project =~ s|^/||; $project =~ s|/$||;
92 $project = validate_input($project);
93 if (!defined($project)) {
94 die_error(undef, "Invalid project parameter.");
95 }
96 if (!(-d "$projectroot/$project")) {
97 undef $project;
98 die_error(undef, "No such directory.");
99 }
100 if (!(-e "$projectroot/$project/HEAD")) {
101 undef $project;
102 die_error(undef, "No such project.");
103 }
104 $rss_link = "<link rel=\"alternate\" title=\"" . esc_param($project) . " log\" href=\"" .
105 "$my_uri?" . esc_param("p=$project;a=rss") . "\" type=\"application/rss+xml\"/>";
106 $ENV{'GIT_DIR'} = "$projectroot/$project";
107} else {
108 git_project_list();
109 exit;
110}
111
112my $file_name = $cgi->param('f');
113if (defined $file_name) {
114 $file_name = validate_input($file_name);
115 if (!defined($file_name)) {
116 die_error(undef, "Invalid file parameter.");
117 }
118}
119
120my $hash = $cgi->param('h');
121if (defined $hash) {
122 $hash = validate_input($hash);
123 if (!defined($hash)) {
124 die_error(undef, "Invalid hash parameter.");
125 }
126}
127
128my $hash_parent = $cgi->param('hp');
129if (defined $hash_parent) {
130 $hash_parent = validate_input($hash_parent);
131 if (!defined($hash_parent)) {
132 die_error(undef, "Invalid hash parent parameter.");
133 }
134}
135
136my $hash_base = $cgi->param('hb');
137if (defined $hash_base) {
138 $hash_base = validate_input($hash_base);
139 if (!defined($hash_base)) {
140 die_error(undef, "Invalid hash base parameter.");
141 }
142}
143
144my $page = $cgi->param('pg');
145if (defined $page) {
146 if ($page =~ m/[^0-9]$/) {
147 undef $page;
148 die_error(undef, "Invalid page parameter.");
149 }
150}
151
152my $searchtext = $cgi->param('s');
153if (defined $searchtext) {
154 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
155 undef $searchtext;
156 die_error(undef, "Invalid search parameter.");
157 }
158 $searchtext = quotemeta $searchtext;
159}
160
161sub validate_input {
162 my $input = shift;
163
164 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
165 return $input;
166 }
167 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
168 return undef;
169 }
170 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
171 return undef;
172 }
173 return $input;
174}
175
176if (!defined $action || $action eq "summary") {
177 git_summary();
178 exit;
179} elsif ($action eq "heads") {
180 git_heads();
181 exit;
182} elsif ($action eq "tags") {
183 git_tags();
184 exit;
185} elsif ($action eq "blob") {
186 git_blob();
187 exit;
188} elsif ($action eq "blob_plain") {
189 git_blob_plain();
190 exit;
191} elsif ($action eq "tree") {
192 git_tree();
193 exit;
194} elsif ($action eq "rss") {
195 git_rss();
196 exit;
197} elsif ($action eq "commit") {
198 git_commit();
199 exit;
200} elsif ($action eq "log") {
201 git_log();
202 exit;
203} elsif ($action eq "blobdiff") {
204 git_blobdiff();
205 exit;
206} elsif ($action eq "blobdiff_plain") {
207 git_blobdiff_plain();
208 exit;
209} elsif ($action eq "commitdiff") {
210 git_commitdiff();
211 exit;
212} elsif ($action eq "commitdiff_plain") {
213 git_commitdiff_plain();
214 exit;
215} elsif ($action eq "history") {
216 git_history();
217 exit;
218} elsif ($action eq "search") {
219 git_search();
220 exit;
221} elsif ($action eq "shortlog") {
222 git_shortlog();
223 exit;
224} elsif ($action eq "tag") {
225 git_tag();
226 exit;
227} elsif ($action eq "blame") {
228 git_blame();
229 exit;
230} else {
231 undef $action;
232 die_error(undef, "Unknown action.");
233 exit;
234}
235
236# quote unsafe chars, but keep the slash, even when it's not
237# correct, but quoted slashes look too horrible in bookmarks
238sub esc_param {
239 my $str = shift;
240 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
241 $str =~ s/\+/%2B/g;
242 $str =~ s/ /\+/g;
243 return $str;
244}
245
246# replace invalid utf8 character with SUBSTITUTION sequence
247sub esc_html {
248 my $str = shift;
249 $str = decode("utf8", $str, Encode::FB_DEFAULT);
250 $str = escapeHTML($str);
251 return $str;
252}
253
254# git may return quoted and escaped filenames
255sub unquote {
256 my $str = shift;
257 if ($str =~ m/^"(.*)"$/) {
258 $str = $1;
259 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
260 }
261 return $str;
262}
263
264# CSS class for given age value (in seconds)
265sub age_class {
266 my $age = shift;
267
268 if ($age < 60*60*2) {
269 return "age0";
270 } elsif ($age < 60*60*24*2) {
271 return "age1";
272 } else {
273 return "age2";
274 }
275}
276
277sub git_header_html {
278 my $status = shift || "200 OK";
279 my $expires = shift;
280
281 my $title = "git";
282 if (defined $project) {
283 $title .= " - $project";
284 if (defined $action) {
285 $title .= "/$action";
286 if (defined $file_name) {
287 $title .= " - $file_name";
288 if ($action eq "tree" && $file_name !~ m|/$|) {
289 $title .= "/";
290 }
291 }
292 }
293 }
294 print $cgi->header(-type=>'text/html', -charset => 'utf-8', -status=> $status, -expires => $expires);
295 print <<EOF;
296<?xml version="1.0" encoding="utf-8"?>
297<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
298<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
299<!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
300<!-- git core binaries version $git_version -->
301<head>
302<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
303<meta name="robots" content="index, nofollow"/>
304<title>$title</title>
305<link rel="stylesheet" type="text/css" href="$stylesheet"/>
306$rss_link
307</head>
308<body>
309EOF
310 print "<div class=\"page_header\">\n" .
311 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
312 "<img src=\"$my_uri?" . esc_param("a=git-logo.png") . "\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
313 "</a>\n";
314 print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
315 if (defined $project) {
316 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
317 if (defined $action) {
318 print " / $action";
319 }
320 print "\n";
321 if (!defined $searchtext) {
322 $searchtext = "";
323 }
324 my $search_hash;
325 if (defined $hash_base) {
326 $search_hash = $hash_base;
327 } elsif (defined $hash) {
328 $search_hash = $hash;
329 } else {
330 $search_hash = "HEAD";
331 }
332 $cgi->param("a", "search");
333 $cgi->param("h", $search_hash);
334 print $cgi->startform(-method => "get", -action => $my_uri) .
335 "<div class=\"search\">\n" .
336 $cgi->hidden(-name => "p") . "\n" .
337 $cgi->hidden(-name => "a") . "\n" .
338 $cgi->hidden(-name => "h") . "\n" .
339 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
340 "</div>" .
341 $cgi->end_form() . "\n";
342 }
343 print "</div>\n";
344}
345
346sub git_footer_html {
347 print "<div class=\"page_footer\">\n";
348 if (defined $project) {
349 my $descr = git_read_description($project);
350 if (defined $descr) {
351 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
352 }
353 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
354 } else {
355 print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
356 }
357 print "</div>\n" .
358 "</body>\n" .
359 "</html>";
360}
361
362sub die_error {
363 my $status = shift || "403 Forbidden";
364 my $error = shift || "Malformed query, file missing or permission denied";
365
366 git_header_html($status);
367 print "<div class=\"page_body\">\n" .
368 "<br/><br/>\n" .
369 "$status - $error\n" .
370 "<br/>\n" .
371 "</div>\n";
372 git_footer_html();
373 exit;
374}
375
376sub git_get_type {
377 my $hash = shift;
378
379 open my $fd, "-|", "$gitbin/git-cat-file -t $hash" or return;
380 my $type = <$fd>;
381 close $fd or return;
382 chomp $type;
383 return $type;
384}
385
386sub git_read_head {
387 my $project = shift;
388 my $oENV = $ENV{'GIT_DIR'};
389 my $retval = undef;
390 $ENV{'GIT_DIR'} = "$projectroot/$project";
391 if (open my $fd, "-|", "$gitbin/git-rev-parse", "--verify", "HEAD") {
392 my $head = <$fd>;
393 close $fd;
394 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
395 $retval = $1;
396 }
397 }
398 if (defined $oENV) {
399 $ENV{'GIT_DIR'} = $oENV;
400 }
401 return $retval;
402}
403
404sub git_read_hash {
405 my $path = shift;
406
407 open my $fd, "$projectroot/$path" or return undef;
408 my $head = <$fd>;
409 close $fd;
410 chomp $head;
411 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
412 return $head;
413 }
414}
415
416sub git_read_description {
417 my $path = shift;
418
419 open my $fd, "$projectroot/$path/description" or return undef;
420 my $descr = <$fd>;
421 close $fd;
422 chomp $descr;
423 return $descr;
424}
425
426sub git_read_tag {
427 my $tag_id = shift;
428 my %tag;
429 my @comment;
430
431 open my $fd, "-|", "$gitbin/git-cat-file tag $tag_id" or return;
432 $tag{'id'} = $tag_id;
433 while (my $line = <$fd>) {
434 chomp $line;
435 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
436 $tag{'object'} = $1;
437 } elsif ($line =~ m/^type (.+)$/) {
438 $tag{'type'} = $1;
439 } elsif ($line =~ m/^tag (.+)$/) {
440 $tag{'name'} = $1;
441 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
442 $tag{'author'} = $1;
443 $tag{'epoch'} = $2;
444 $tag{'tz'} = $3;
445 } elsif ($line =~ m/--BEGIN/) {
446 push @comment, $line;
447 last;
448 } elsif ($line eq "") {
449 last;
450 }
451 }
452 push @comment, <$fd>;
453 $tag{'comment'} = \@comment;
454 close $fd or return;
455 if (!defined $tag{'name'}) {
456 return
457 };
458 return %tag
459}
460
461sub age_string {
462 my $age = shift;
463 my $age_str;
464
465 if ($age > 60*60*24*365*2) {
466 $age_str = (int $age/60/60/24/365);
467 $age_str .= " years ago";
468 } elsif ($age > 60*60*24*(365/12)*2) {
469 $age_str = int $age/60/60/24/(365/12);
470 $age_str .= " months ago";
471 } elsif ($age > 60*60*24*7*2) {
472 $age_str = int $age/60/60/24/7;
473 $age_str .= " weeks ago";
474 } elsif ($age > 60*60*24*2) {
475 $age_str = int $age/60/60/24;
476 $age_str .= " days ago";
477 } elsif ($age > 60*60*2) {
478 $age_str = int $age/60/60;
479 $age_str .= " hours ago";
480 } elsif ($age > 60*2) {
481 $age_str = int $age/60;
482 $age_str .= " min ago";
483 } elsif ($age > 2) {
484 $age_str = int $age;
485 $age_str .= " sec ago";
486 } else {
487 $age_str .= " right now";
488 }
489 return $age_str;
490}
491
492sub git_read_commit {
493 my $commit_id = shift;
494 my $commit_text = shift;
495
496 my @commit_lines;
497 my %co;
498
499 if (defined $commit_text) {
500 @commit_lines = @$commit_text;
501 } else {
502 $/ = "\0";
503 open my $fd, "-|", "$gitbin/git-rev-list --header --parents --max-count=1 $commit_id" or return;
504 @commit_lines = split '\n', <$fd>;
505 close $fd or return;
506 $/ = "\n";
507 pop @commit_lines;
508 }
509 my $header = shift @commit_lines;
510 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
511 return;
512 }
513 ($co{'id'}, my @parents) = split ' ', $header;
514 $co{'parents'} = \@parents;
515 $co{'parent'} = $parents[0];
516 while (my $line = shift @commit_lines) {
517 last if $line eq "\n";
518 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
519 $co{'tree'} = $1;
520 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
521 $co{'author'} = $1;
522 $co{'author_epoch'} = $2;
523 $co{'author_tz'} = $3;
524 if ($co{'author'} =~ m/^([^<]+) </) {
525 $co{'author_name'} = $1;
526 } else {
527 $co{'author_name'} = $co{'author'};
528 }
529 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
530 $co{'committer'} = $1;
531 $co{'committer_epoch'} = $2;
532 $co{'committer_tz'} = $3;
533 $co{'committer_name'} = $co{'committer'};
534 $co{'committer_name'} =~ s/ <.*//;
535 }
536 }
537 if (!defined $co{'tree'}) {
538 return;
539 };
540
541 foreach my $title (@commit_lines) {
542 $title =~ s/^ //;
543 if ($title ne "") {
544 $co{'title'} = chop_str($title, 80, 5);
545 # remove leading stuff of merges to make the interesting part visible
546 if (length($title) > 50) {
547 $title =~ s/^Automatic //;
548 $title =~ s/^merge (of|with) /Merge ... /i;
549 if (length($title) > 50) {
550 $title =~ s/(http|rsync):\/\///;
551 }
552 if (length($title) > 50) {
553 $title =~ s/(master|www|rsync)\.//;
554 }
555 if (length($title) > 50) {
556 $title =~ s/kernel.org:?//;
557 }
558 if (length($title) > 50) {
559 $title =~ s/\/pub\/scm//;
560 }
561 }
562 $co{'title_short'} = chop_str($title, 50, 5);
563 last;
564 }
565 }
566 # remove added spaces
567 foreach my $line (@commit_lines) {
568 $line =~ s/^ //;
569 }
570 $co{'comment'} = \@commit_lines;
571
572 my $age = time - $co{'committer_epoch'};
573 $co{'age'} = $age;
574 $co{'age_string'} = age_string($age);
575 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
576 if ($age > 60*60*24*7*2) {
577 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
578 $co{'age_string_age'} = $co{'age_string'};
579 } else {
580 $co{'age_string_date'} = $co{'age_string'};
581 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
582 }
583 return %co;
584}
585
586sub git_diff_print {
587 my $from = shift;
588 my $from_name = shift;
589 my $to = shift;
590 my $to_name = shift;
591 my $format = shift || "html";
592
593 my $from_tmp = "/dev/null";
594 my $to_tmp = "/dev/null";
595 my $pid = $$;
596
597 # create tmp from-file
598 if (defined $from) {
599 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
600 open my $fd2, "> $from_tmp";
601 open my $fd, "-|", "$gitbin/git-cat-file blob $from";
602 my @file = <$fd>;
603 print $fd2 @file;
604 close $fd2;
605 close $fd;
606 }
607
608 # create tmp to-file
609 if (defined $to) {
610 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
611 open my $fd2, "> $to_tmp";
612 open my $fd, "-|", "$gitbin/git-cat-file blob $to";
613 my @file = <$fd>;
614 print $fd2 @file;
615 close $fd2;
616 close $fd;
617 }
618
619 open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
620 if ($format eq "plain") {
621 undef $/;
622 print <$fd>;
623 $/ = "\n";
624 } else {
625 while (my $line = <$fd>) {
626 chomp($line);
627 my $char = substr($line, 0, 1);
628 my $diff_class = "";
629 if ($char eq '+') {
630 $diff_class = " add";
631 } elsif ($char eq "-") {
632 $diff_class = " rem";
633 } elsif ($char eq "@") {
634 $diff_class = " chunk_header";
635 } elsif ($char eq "\\") {
636 # skip errors
637 next;
638 }
639 while ((my $pos = index($line, "\t")) != -1) {
640 if (my $count = (8 - (($pos-1) % 8))) {
641 my $spaces = ' ' x $count;
642 $line =~ s/\t/$spaces/;
643 }
644 }
645 print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
646 }
647 }
648 close $fd;
649
650 if (defined $from) {
651 unlink($from_tmp);
652 }
653 if (defined $to) {
654 unlink($to_tmp);
655 }
656}
657
658sub mode_str {
659 my $mode = oct shift;
660
661 if (S_ISDIR($mode & S_IFMT)) {
662 return 'drwxr-xr-x';
663 } elsif (S_ISLNK($mode)) {
664 return 'lrwxrwxrwx';
665 } elsif (S_ISREG($mode)) {
666 # git cares only about the executable bit
667 if ($mode & S_IXUSR) {
668 return '-rwxr-xr-x';
669 } else {
670 return '-rw-r--r--';
671 };
672 } else {
673 return '----------';
674 }
675}
676
677sub chop_str {
678 my $str = shift;
679 my $len = shift;
680 my $add_len = shift || 10;
681
682 # allow only $len chars, but don't cut a word if it would fit in $add_len
683 # if it doesn't fit, cut it if it's still longer than the dots we would add
684 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
685 my $body = $1;
686 my $tail = $2;
687 if (length($tail) > 4) {
688 $tail = " ...";
689 }
690 return "$body$tail";
691}
692
693sub file_type {
694 my $mode = oct shift;
695
696 if (S_ISDIR($mode & S_IFMT)) {
697 return "directory";
698 } elsif (S_ISLNK($mode)) {
699 return "symlink";
700 } elsif (S_ISREG($mode)) {
701 return "file";
702 } else {
703 return "unknown";
704 }
705}
706
707sub format_log_line_html {
708 my $line = shift;
709
710 $line = esc_html($line);
711 $line =~ s/ / /g;
712 if ($line =~ m/([0-9a-fA-F]{40})/) {
713 my $hash_text = $1;
714 if (git_get_type($hash_text) eq "commit") {
715 my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
716 $line =~ s/$hash_text/$link/;
717 }
718 }
719 return $line;
720}
721
722sub date_str {
723 my $epoch = shift;
724 my $tz = shift || "-0000";
725
726 my %date;
727 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
728 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
729 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
730 $date{'hour'} = $hour;
731 $date{'minute'} = $min;
732 $date{'mday'} = $mday;
733 $date{'day'} = $days[$wday];
734 $date{'month'} = $months[$mon];
735 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
736 $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
737
738 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
739 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
740 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
741 $date{'hour_local'} = $hour;
742 $date{'minute_local'} = $min;
743 $date{'tz_local'} = $tz;
744 return %date;
745}
746
747# git-logo (cached in browser for one day)
748sub git_logo {
749 binmode STDOUT, ':raw';
750 print $cgi->header(-type => 'image/png', -expires => '+1d');
751 # cat git-logo.png | hexdump -e '16/1 " %02x" "\n"' | sed 's/ /\\x/g'
752 print "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
753 "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
754 "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
755 "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
756 "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
757 "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
758 "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
759 "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
760 "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
761 "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
762 "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
763 "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
764 "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
765}
766
767sub get_file_owner {
768 my $path = shift;
769
770 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
771 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
772 if (!defined $gcos) {
773 return undef;
774 }
775 my $owner = $gcos;
776 $owner =~ s/[,;].*$//;
777 return decode("utf8", $owner, Encode::FB_DEFAULT);
778}
779
780sub git_read_projects {
781 my @list;
782
783 if (-d $projects_list) {
784 # search in directory
785 my $dir = $projects_list;
786 opendir my $dh, $dir or return undef;
787 while (my $dir = readdir($dh)) {
788 if (-e "$projectroot/$dir/HEAD") {
789 my $pr = {
790 path => $dir,
791 };
792 push @list, $pr
793 }
794 }
795 closedir($dh);
796 } elsif (-f $projects_list) {
797 # read from file(url-encoded):
798 # 'git%2Fgit.git Linus+Torvalds'
799 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
800 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
801 open my $fd , $projects_list or return undef;
802 while (my $line = <$fd>) {
803 chomp $line;
804 my ($path, $owner) = split ' ', $line;
805 $path = unescape($path);
806 $owner = unescape($owner);
807 if (!defined $path) {
808 next;
809 }
810 if (-e "$projectroot/$path/HEAD") {
811 my $pr = {
812 path => $path,
813 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
814 };
815 push @list, $pr
816 }
817 }
818 close $fd;
819 }
820 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
821 return @list;
822}
823
824sub git_get_project_config {
825 my $key = shift;
826
827 return unless ($key);
828 $key =~ s/^gitweb\.//;
829 return if ($key =~ m/\W/);
830
831 my $val = qx($gitbin/git-repo-config --get gitweb.$key);
832 return ($val);
833}
834
835sub git_get_project_config_bool {
836 my $val = git_get_project_config (@_);
837 if ($val and $val =~ m/true|yes|on/) {
838 return (1);
839 }
840 return; # implicit false
841}
842
843sub git_project_list {
844 my @list = git_read_projects();
845 my @projects;
846 if (!@list) {
847 die_error(undef, "No project found.");
848 }
849 foreach my $pr (@list) {
850 my $head = git_read_head($pr->{'path'});
851 if (!defined $head) {
852 next;
853 }
854 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
855 my %co = git_read_commit($head);
856 if (!%co) {
857 next;
858 }
859 $pr->{'commit'} = \%co;
860 if (!defined $pr->{'descr'}) {
861 my $descr = git_read_description($pr->{'path'}) || "";
862 $pr->{'descr'} = chop_str($descr, 25, 5);
863 }
864 if (!defined $pr->{'owner'}) {
865 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
866 }
867 push @projects, $pr;
868 }
869 git_header_html();
870 if (-f $home_text) {
871 print "<div class=\"index_include\">\n";
872 open (my $fd, $home_text);
873 print <$fd>;
874 close $fd;
875 print "</div>\n";
876 }
877 print "<table class=\"project_list\">\n" .
878 "<tr>\n";
879 if (!defined($order) || (defined($order) && ($order eq "project"))) {
880 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
881 print "<th>Project</th>\n";
882 } else {
883 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=project")}, "Project") . "</th>\n";
884 }
885 if (defined($order) && ($order eq "descr")) {
886 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
887 print "<th>Description</th>\n";
888 } else {
889 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=descr")}, "Description") . "</th>\n";
890 }
891 if (defined($order) && ($order eq "owner")) {
892 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
893 print "<th>Owner</th>\n";
894 } else {
895 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=owner")}, "Owner") . "</th>\n";
896 }
897 if (defined($order) && ($order eq "age")) {
898 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
899 print "<th>Last Change</th>\n";
900 } else {
901 print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=age")}, "Last Change") . "</th>\n";
902 }
903 print "<th></th>\n" .
904 "</tr>\n";
905 my $alternate = 0;
906 foreach my $pr (@projects) {
907 if ($alternate) {
908 print "<tr class=\"dark\">\n";
909 } else {
910 print "<tr class=\"light\">\n";
911 }
912 $alternate ^= 1;
913 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"), -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
914 "<td>$pr->{'descr'}</td>\n" .
915 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
916 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" . $pr->{'commit'}{'age_string'} . "</td>\n" .
917 "<td class=\"link\">" .
918 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") .
919 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") .
920 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
921 "</td>\n" .
922 "</tr>\n";
923 }
924 print "</table>\n";
925 git_footer_html();
926}
927
928sub read_info_ref {
929 my $type = shift || "";
930 my %refs;
931 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
932 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
933 open my $fd, "$projectroot/$project/info/refs" or return;
934 while (my $line = <$fd>) {
935 chomp($line);
936 if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
937 if (defined $refs{$1}) {
938 $refs{$1} .= " / $2";
939 } else {
940 $refs{$1} = $2;
941 }
942 }
943 }
944 close $fd or return;
945 return \%refs;
946}
947
948sub git_read_refs {
949 my $ref_dir = shift;
950 my @reflist;
951
952 my @refs;
953 opendir my $dh, "$projectroot/$project/$ref_dir";
954 while (my $dir = readdir($dh)) {
955 if ($dir =~ m/^\./) {
956 next;
957 }
958 if (-d "$projectroot/$project/$ref_dir/$dir") {
959 opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
960 my @subdirs = grep !m/^\./, readdir $dh2;
961 closedir($dh2);
962 foreach my $subdir (@subdirs) {
963 push @refs, "$dir/$subdir"
964 }
965 next;
966 }
967 push @refs, $dir;
968 }
969 closedir($dh);
970 foreach my $ref_file (@refs) {
971 my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
972 my $type = git_get_type($ref_id) || next;
973 my %ref_item;
974 my %co;
975 $ref_item{'type'} = $type;
976 $ref_item{'id'} = $ref_id;
977 $ref_item{'epoch'} = 0;
978 $ref_item{'age'} = "unknown";
979 if ($type eq "tag") {
980 my %tag = git_read_tag($ref_id);
981 $ref_item{'comment'} = $tag{'comment'};
982 if ($tag{'type'} eq "commit") {
983 %co = git_read_commit($tag{'object'});
984 $ref_item{'epoch'} = $co{'committer_epoch'};
985 $ref_item{'age'} = $co{'age_string'};
986 } elsif (defined($tag{'epoch'})) {
987 my $age = time - $tag{'epoch'};
988 $ref_item{'epoch'} = $tag{'epoch'};
989 $ref_item{'age'} = age_string($age);
990 }
991 $ref_item{'reftype'} = $tag{'type'};
992 $ref_item{'name'} = $tag{'name'};
993 $ref_item{'refid'} = $tag{'object'};
994 } elsif ($type eq "commit"){
995 %co = git_read_commit($ref_id);
996 $ref_item{'reftype'} = "commit";
997 $ref_item{'name'} = $ref_file;
998 $ref_item{'title'} = $co{'title'};
999 $ref_item{'refid'} = $ref_id;
1000 $ref_item{'epoch'} = $co{'committer_epoch'};
1001 $ref_item{'age'} = $co{'age_string'};
1002 }
1003
1004 push @reflist, \%ref_item;
1005 }
1006 # sort tags by age
1007 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1008 return \@reflist;
1009}
1010
1011sub git_summary {
1012 my $descr = git_read_description($project) || "none";
1013 my $head = git_read_head($project);
1014 my %co = git_read_commit($head);
1015 my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1016
1017 my $owner;
1018 if (-f $projects_list) {
1019 open (my $fd , $projects_list);
1020 while (my $line = <$fd>) {
1021 chomp $line;
1022 my ($pr, $ow) = split ' ', $line;
1023 $pr = unescape($pr);
1024 $ow = unescape($ow);
1025 if ($pr eq $project) {
1026 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
1027 last;
1028 }
1029 }
1030 close $fd;
1031 }
1032 if (!defined $owner) {
1033 $owner = get_file_owner("$projectroot/$project");
1034 }
1035
1036 my $refs = read_info_ref();
1037 git_header_html();
1038 print "<div class=\"page_nav\">\n" .
1039 "summary".
1040 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1041 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1042 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1043 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1044 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree")}, "tree") .
1045 "<br/><br/>\n" .
1046 "</div>\n";
1047 print "<div class=\"title\"> </div>\n";
1048 print "<table cellspacing=\"0\">\n" .
1049 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1050 "<tr><td>owner</td><td>$owner</td></tr>\n" .
1051 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
1052 "</table>\n";
1053 open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_head($project) or die_error(undef, "Open failed.");
1054 my (@revlist) = map { chomp; $_ } <$fd>;
1055 close $fd;
1056 print "<div>\n" .
1057 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog"), -class => "title"}, "shortlog") .
1058 "</div>\n";
1059 my $i = 16;
1060 print "<table cellspacing=\"0\">\n";
1061 my $alternate = 0;
1062 foreach my $commit (@revlist) {
1063 my %co = git_read_commit($commit);
1064 my %ad = date_str($co{'author_epoch'});
1065 if ($alternate) {
1066 print "<tr class=\"dark\">\n";
1067 } else {
1068 print "<tr class=\"light\">\n";
1069 }
1070 $alternate ^= 1;
1071 if ($i-- > 0) {
1072 my $ref = "";
1073 if (defined $refs->{$commit}) {
1074 $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
1075 }
1076 print "<td><i>$co{'age_string'}</i></td>\n" .
1077 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1078 "<td>";
1079 if (length($co{'title_short'}) < length($co{'title'})) {
1080 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
1081 "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
1082 } else {
1083 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
1084 "<b>" . esc_html($co{'title'}) . "$ref</b>");
1085 }
1086 print "</td>\n" .
1087 "<td class=\"link\">" .
1088 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1089 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1090 "</td>\n" .
1091 "</tr>";
1092 } else {
1093 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "...") . "</td>\n" .
1094 "</tr>";
1095 last;
1096 }
1097 }
1098 print "</table\n>";
1099
1100 my $taglist = git_read_refs("refs/tags");
1101 if (defined @$taglist) {
1102 print "<div>\n" .
1103 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags"), -class => "title"}, "tags") .
1104 "</div>\n";
1105 my $i = 16;
1106 print "<table cellspacing=\"0\">\n";
1107 my $alternate = 0;
1108 foreach my $entry (@$taglist) {
1109 my %tag = %$entry;
1110 my $comment_lines = $tag{'comment'};
1111 my $comment = shift @$comment_lines;
1112 if (defined($comment)) {
1113 $comment = chop_str($comment, 30, 5);
1114 }
1115 if ($alternate) {
1116 print "<tr class=\"dark\">\n";
1117 } else {
1118 print "<tr class=\"light\">\n";
1119 }
1120 $alternate ^= 1;
1121 if ($i-- > 0) {
1122 print "<td><i>$tag{'age'}</i></td>\n" .
1123 "<td>" .
1124 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
1125 "<b>" . esc_html($tag{'name'}) . "</b>") .
1126 "</td>\n" .
1127 "<td>";
1128 if (defined($comment)) {
1129 print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
1130 }
1131 print "</td>\n" .
1132 "<td class=\"link\">";
1133 if ($tag{'type'} eq "tag") {
1134 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
1135 }
1136 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1137 if ($tag{'reftype'} eq "commit") {
1138 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1139 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1140 }
1141 print "</td>\n" .
1142 "</tr>";
1143 } else {
1144 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "...") . "</td>\n" .
1145 "</tr>";
1146 last;
1147 }
1148 }
1149 print "</table\n>";
1150 }
1151
1152 my $headlist = git_read_refs("refs/heads");
1153 if (defined @$headlist) {
1154 print "<div>\n" .
1155 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads"), -class => "title"}, "heads") .
1156 "</div>\n";
1157 my $i = 16;
1158 print "<table cellspacing=\"0\">\n";
1159 my $alternate = 0;
1160 foreach my $entry (@$headlist) {
1161 my %tag = %$entry;
1162 if ($alternate) {
1163 print "<tr class=\"dark\">\n";
1164 } else {
1165 print "<tr class=\"light\">\n";
1166 }
1167 $alternate ^= 1;
1168 if ($i-- > 0) {
1169 print "<td><i>$tag{'age'}</i></td>\n" .
1170 "<td>" .
1171 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"},
1172 "<b>" . esc_html($tag{'name'}) . "</b>") .
1173 "</td>\n" .
1174 "<td class=\"link\">" .
1175 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1176 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1177 "</td>\n" .
1178 "</tr>";
1179 } else {
1180 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "...") . "</td>\n" .
1181 "</tr>";
1182 last;
1183 }
1184 }
1185 print "</table\n>";
1186 }
1187 git_footer_html();
1188}
1189
1190sub git_tag {
1191 my $head = git_read_head($project);
1192 git_header_html();
1193 print "<div class=\"page_nav\">\n" .
1194 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1195 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1196 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1197 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1198 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1199 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
1200 "<br/>\n" .
1201 "</div>\n";
1202 my %tag = git_read_tag($hash);
1203 print "<div>\n" .
1204 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($tag{'name'})) . "\n" .
1205 "</div>\n";
1206 print "<div class=\"title_text\">\n" .
1207 "<table cellspacing=\"0\">\n" .
1208 "<tr>\n" .
1209 "<td>object</td>\n" .
1210 "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
1211 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
1212 "</tr>\n";
1213 if (defined($tag{'author'})) {
1214 my %ad = date_str($tag{'epoch'}, $tag{'tz'});
1215 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1216 print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1217 }
1218 print "</table>\n\n" .
1219 "</div>\n";
1220 print "<div class=\"page_body\">";
1221 my $comment = $tag{'comment'};
1222 foreach my $line (@$comment) {
1223 print esc_html($line) . "<br/>\n";
1224 }
1225 print "</div>\n";
1226 git_footer_html();
1227}
1228
1229sub git_blame {
1230 my $fd;
1231 die_error('403 Permission denied', "Permission denied.") if (!git_get_project_config_bool ('blame'));
1232 die_error('404 Not Found', "What file will it be, master?") if (!$file_name);
1233 $hash_base ||= git_read_head($project);
1234 die_error(undef, "Reading commit failed.") unless ($hash_base);
1235 my %co = git_read_commit($hash_base)
1236 or die_error(undef, "Reading commit failed.");
1237 if (!defined $hash) {
1238 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1239 or die_error(undef, "Error lookup file.");
1240 }
1241 open ($fd, "-|", "$gitbin/git-annotate", '-l', '-t', '-r', $file_name, $hash_base)
1242 or die_error(undef, "Open failed.");
1243 git_header_html();
1244 print "<div class=\"page_nav\">\n" .
1245 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1246 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1247 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1248 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1249 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1250 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1251 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1252 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head") . "<br/>\n";
1253 print "</div>\n".
1254 "<div>" .
1255 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1256 "</div>\n";
1257 print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
1258 print "<div class=\"page_body\">\n";
1259 print <<HTML;
1260<table class="blame">
1261 <tr>
1262 <th>Commit</th>
1263 <th>Age</th>
1264 <th>Author</th>
1265 <th>Line</th>
1266 <th>Data</th>
1267 </tr>
1268HTML
1269 my @line_class = (qw(light dark));
1270 my $line_class_len = scalar (@line_class);
1271 my $line_class_num = $#line_class;
1272 while (my $line = <$fd>) {
1273 my $long_rev;
1274 my $short_rev;
1275 my $author;
1276 my $time;
1277 my $lineno;
1278 my $data;
1279 my $age;
1280 my $age_str;
1281 my $age_class;
1282
1283 chomp $line;
1284 $line_class_num = ($line_class_num + 1) % $line_class_len;
1285
1286 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
1287 $long_rev = $1;
1288 $author = $2;
1289 $time = $3;
1290 $lineno = $4;
1291 $data = $5;
1292 } else {
1293 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
1294 next;
1295 }
1296 $short_rev = substr ($long_rev, 0, 8);
1297 $age = time () - $time;
1298 $age_str = age_string ($age);
1299 $age_str =~ s/ / /g;
1300 $age_class = age_class($age);
1301 $author = esc_html ($author);
1302 $author =~ s/ / /g;
1303 # escape tabs
1304 while ((my $pos = index($data, "\t")) != -1) {
1305 if (my $count = (8 - ($pos % 8))) {
1306 my $spaces = ' ' x $count;
1307 $data =~ s/\t/$spaces/;
1308 }
1309 }
1310 $data = esc_html ($data);
1311
1312 print <<HTML;
1313 <tr class="$line_class[$line_class_num]">
1314 <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1315 <td class="$age_class">$age_str</td>
1316 <td>$author</td>
1317 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1318 <td class="pre">$data</td>
1319 </tr>
1320HTML
1321 } # while (my $line = <$fd>)
1322 print "</table>\n\n";
1323 close $fd or print "Reading blob failed.\n";
1324 print "</div>";
1325 git_footer_html();
1326}
1327
1328sub git_tags {
1329 my $head = git_read_head($project);
1330 git_header_html();
1331 print "<div class=\"page_nav\">\n" .
1332 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1333 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1334 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1335 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1336 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1337 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
1338 "<br/>\n" .
1339 "</div>\n";
1340 my $taglist = git_read_refs("refs/tags");
1341 print "<div>\n" .
1342 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, " ") .
1343 "</div>\n";
1344 print "<table cellspacing=\"0\">\n";
1345 my $alternate = 0;
1346 if (defined @$taglist) {
1347 foreach my $entry (@$taglist) {
1348 my %tag = %$entry;
1349 my $comment_lines = $tag{'comment'};
1350 my $comment = shift @$comment_lines;
1351 if (defined($comment)) {
1352 $comment = chop_str($comment, 30, 5);
1353 }
1354 if ($alternate) {
1355 print "<tr class=\"dark\">\n";
1356 } else {
1357 print "<tr class=\"light\">\n";
1358 }
1359 $alternate ^= 1;
1360 print "<td><i>$tag{'age'}</i></td>\n" .
1361 "<td>" .
1362 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
1363 "<b>" . esc_html($tag{'name'}) . "</b>") .
1364 "</td>\n" .
1365 "<td>";
1366 if (defined($comment)) {
1367 print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
1368 }
1369 print "</td>\n" .
1370 "<td class=\"link\">";
1371 if ($tag{'type'} eq "tag") {
1372 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
1373 }
1374 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1375 if ($tag{'reftype'} eq "commit") {
1376 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1377 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1378 }
1379 print "</td>\n" .
1380 "</tr>";
1381 }
1382 }
1383 print "</table\n>";
1384 git_footer_html();
1385}
1386
1387sub git_heads {
1388 my $head = git_read_head($project);
1389 git_header_html();
1390 print "<div class=\"page_nav\">\n" .
1391 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1392 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1393 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1394 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$head")}, "commit") .
1395 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$head")}, "commitdiff") .
1396 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;hb=$head")}, "tree") . "<br/>\n" .
1397 "<br/>\n" .
1398 "</div>\n";
1399 my $taglist = git_read_refs("refs/heads");
1400 print "<div>\n" .
1401 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, " ") .
1402 "</div>\n";
1403 print "<table cellspacing=\"0\">\n";
1404 my $alternate = 0;
1405 if (defined @$taglist) {
1406 foreach my $entry (@$taglist) {
1407 my %tag = %$entry;
1408 if ($alternate) {
1409 print "<tr class=\"dark\">\n";
1410 } else {
1411 print "<tr class=\"light\">\n";
1412 }
1413 $alternate ^= 1;
1414 print "<td><i>$tag{'age'}</i></td>\n" .
1415 "<td>" .
1416 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1417 "</td>\n" .
1418 "<td class=\"link\">" .
1419 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1420 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1421 "</td>\n" .
1422 "</tr>";
1423 }
1424 }
1425 print "</table\n>";
1426 git_footer_html();
1427}
1428
1429sub git_get_hash_by_path {
1430 my $base = shift;
1431 my $path = shift || return undef;
1432
1433 my $tree = $base;
1434 my @parts = split '/', $path;
1435 while (my $part = shift @parts) {
1436 open my $fd, "-|", "$gitbin/git-ls-tree $tree" or die_error(undef, "Open git-ls-tree failed.");
1437 my (@entries) = map { chomp; $_ } <$fd>;
1438 close $fd or return undef;
1439 foreach my $line (@entries) {
1440 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1441 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1442 my $t_mode = $1;
1443 my $t_type = $2;
1444 my $t_hash = $3;
1445 my $t_name = validate_input(unquote($4));
1446 if ($t_name eq $part) {
1447 if (!(@parts)) {
1448 return $t_hash;
1449 }
1450 if ($t_type eq "tree") {
1451 $tree = $t_hash;
1452 }
1453 last;
1454 }
1455 }
1456 }
1457}
1458
1459sub git_blob {
1460 if (!defined $hash && defined $file_name) {
1461 my $base = $hash_base || git_read_head($project);
1462 $hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file.");
1463 }
1464 my $have_blame = git_get_project_config_bool ('blame');
1465 open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
1466 git_header_html();
1467 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1468 print "<div class=\"page_nav\">\n" .
1469 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1470 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
1471 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "log") .
1472 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1473 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1474 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash_base")}, "tree") . "<br/>\n";
1475 if (defined $file_name) {
1476 if ($have_blame) {
1477 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
1478 }
1479 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1480 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head") . "<br/>\n";
1481 } else {
1482 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain") . "<br/>\n";
1483 }
1484 print "</div>\n".
1485 "<div>" .
1486 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1487 "</div>\n";
1488 } else {
1489 print "<div class=\"page_nav\">\n" .
1490 "<br/><br/></div>\n" .
1491 "<div class=\"title\">$hash</div>\n";
1492 }
1493 if (defined $file_name) {
1494 print "<div class=\"page_path\"><b>" . esc_html($file_name) . "</b></div>\n";
1495 }
1496 print "<div class=\"page_body\">\n";
1497 my $nr;
1498 while (my $line = <$fd>) {
1499 chomp $line;
1500 $nr++;
1501 while ((my $pos = index($line, "\t")) != -1) {
1502 if (my $count = (8 - ($pos % 8))) {
1503 my $spaces = ' ' x $count;
1504 $line =~ s/\t/$spaces/;
1505 }
1506 }
1507 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1508 }
1509 close $fd or print "Reading blob failed.\n";
1510 print "</div>";
1511 git_footer_html();
1512}
1513
1514sub mimetype_guess_file {
1515 my $filename = shift;
1516 my $mimemap = shift;
1517 -r $mimemap or return undef;
1518
1519 my %mimemap;
1520 open(MIME, $mimemap) or return undef;
1521 while (<MIME>) {
1522 my ($mime, $exts) = split(/\t+/);
1523 my @exts = split(/\s+/, $exts);
1524 foreach my $ext (@exts) {
1525 $mimemap{$ext} = $mime;
1526 }
1527 }
1528 close(MIME);
1529
1530 $filename =~ /\.(.*?)$/;
1531 return $mimemap{$1};
1532}
1533
1534sub mimetype_guess {
1535 my $filename = shift;
1536 my $mime;
1537 $filename =~ /\./ or return undef;
1538
1539 if ($mimetypes_file) {
1540 my $file = $mimetypes_file;
1541 #$file =~ m#^/# or $file = "$projectroot/$path/$file";
1542 $mime = mimetype_guess_file($filename, $file);
1543 }
1544 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1545 return $mime;
1546}
1547
1548sub git_blob_plain_mimetype {
1549 my $fd = shift;
1550 my $filename = shift;
1551
1552 # just in case
1553 return $default_blob_plain_mimetype unless $fd;
1554
1555 if ($filename) {
1556 my $mime = mimetype_guess($filename);
1557 $mime and return $mime;
1558 }
1559
1560 if (-T $fd) {
1561 return 'text/plain' .
1562 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1563 } elsif (! $filename) {
1564 return 'application/octet-stream';
1565 } elsif ($filename =~ m/\.png$/i) {
1566 return 'image/png';
1567 } elsif ($filename =~ m/\.gif$/i) {
1568 return 'image/gif';
1569 } elsif ($filename =~ m/\.jpe?g$/i) {
1570 return 'image/jpeg';
1571 } else {
1572 return 'application/octet-stream';
1573 }
1574}
1575
1576sub git_blob_plain {
1577 open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
1578 my $type = git_blob_plain_mimetype($fd, $file_name);
1579
1580 # save as filename, even when no $file_name is given
1581 my $save_as = "$hash";
1582 if (defined $file_name) {
1583 $save_as = $file_name;
1584 } elsif ($type =~ m/^text\//) {
1585 $save_as .= '.txt';
1586 }
1587
1588 print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
1589 undef $/;
1590 binmode STDOUT, ':raw';
1591 print <$fd>;
1592 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
1593 $/ = "\n";
1594 close $fd;
1595}
1596
1597sub git_tree {
1598 if (!defined $hash) {
1599 $hash = git_read_head($project);
1600 if (defined $file_name) {
1601 my $base = $hash_base || $hash;
1602 $hash = git_get_hash_by_path($base, $file_name, "tree");
1603 }
1604 if (!defined $hash_base) {
1605 $hash_base = $hash;
1606 }
1607 }
1608 $/ = "\0";
1609 open my $fd, "-|", "$gitbin/git-ls-tree -z $hash" or die_error(undef, "Open git-ls-tree failed.");
1610 chomp (my (@entries) = <$fd>);
1611 close $fd or die_error(undef, "Reading tree failed.");
1612 $/ = "\n";
1613
1614 my $refs = read_info_ref();
1615 my $ref = "";
1616 if (defined $refs->{$hash_base}) {
1617 $ref = " <span class=\"tag\">" . esc_html($refs->{$hash_base}) . "</span>";
1618 }
1619 git_header_html();
1620 my $base_key = "";
1621 my $base = "";
1622 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1623 $base_key = ";hb=$hash_base";
1624 print "<div class=\"page_nav\">\n" .
1625 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
1626 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash_base")}, "shortlog") .
1627 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash_base")}, "log") .
1628 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base")}, "commit") .
1629 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash_base")}, "commitdiff") .
1630 " | tree" .
1631 "<br/><br/>\n" .
1632 "</div>\n";
1633 print "<div>\n" .
1634 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
1635 "</div>\n";
1636 } else {
1637 print "<div class=\"page_nav\">\n";
1638 print "<br/><br/></div>\n";
1639 print "<div class=\"title\">$hash</div>\n";
1640 }
1641 if (defined $file_name) {
1642 $base = esc_html("$file_name/");
1643 print "<div class=\"page_path\"><b>/" . esc_html($file_name) . "</b></div>\n";
1644 } else {
1645 print "<div class=\"page_path\"><b>/</b></div>\n";
1646 }
1647 print "<div class=\"page_body\">\n";
1648 print "<table cellspacing=\"0\">\n";
1649 my $alternate = 0;
1650 foreach my $line (@entries) {
1651 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1652 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1653 my $t_mode = $1;
1654 my $t_type = $2;
1655 my $t_hash = $3;
1656 my $t_name = validate_input($4);
1657 if ($alternate) {
1658 print "<tr class=\"dark\">\n";
1659 } else {
1660 print "<tr class=\"light\">\n";
1661 }
1662 $alternate ^= 1;
1663 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
1664 if ($t_type eq "blob") {
1665 print "<td class=\"list\">" .
1666 $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)) .
1667 "</td>\n" .
1668 "<td class=\"link\">" .
1669 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
1670# " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") .
1671 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$hash_base;f=$base$t_name")}, "history") .
1672 "</td>\n";
1673 } elsif ($t_type eq "tree") {
1674 print "<td class=\"list\">" .
1675 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
1676 "</td>\n" .
1677 "<td class=\"link\">" .
1678 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
1679 "</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, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -- \'$file_name\'";
2299 my $commit;
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 $commit = $1;
2305 next;
2306 }
2307 if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/ && (defined $commit)) {
2308 my %co = git_read_commit($commit);
2309 if (!%co) {
2310 next;
2311 }
2312 my $ref = "";
2313 if (defined $refs->{$commit}) {
2314 $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
2315 }
2316 if ($alternate) {
2317 print "<tr class=\"dark\">\n";
2318 } else {
2319 print "<tr class=\"light\">\n";
2320 }
2321 $alternate ^= 1;
2322 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2323 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2324 "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
2325 esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
2326 "<td class=\"link\">" .
2327 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2328 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2329 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=$commit;f=$file_name")}, "blob");
2330 my $blob = git_get_hash_by_path($hash, $file_name);
2331 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2332 if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
2333 print " | " .
2334 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
2335 "diff to current");
2336 }
2337 print "</td>\n" .
2338 "</tr>\n";
2339 undef $commit;
2340 }
2341 }
2342 print "</table>\n";
2343 close $fd;
2344 git_footer_html();
2345}
2346
2347sub git_search {
2348 if (!defined $searchtext) {
2349 die_error("", "Text field empty.");
2350 }
2351 if (!defined $hash) {
2352 $hash = git_read_head($project);
2353 }
2354 my %co = git_read_commit($hash);
2355 if (!%co) {
2356 die_error(undef, "Unknown commit object.");
2357 }
2358 # pickaxe may take all resources of your box and run for several minutes
2359 # with every query - so decide by yourself how public you make this feature :)
2360 my $commit_search = 1;
2361 my $author_search = 0;
2362 my $committer_search = 0;
2363 my $pickaxe_search = 0;
2364 if ($searchtext =~ s/^author\\://i) {
2365 $author_search = 1;
2366 } elsif ($searchtext =~ s/^committer\\://i) {
2367 $committer_search = 1;
2368 } elsif ($searchtext =~ s/^pickaxe\\://i) {
2369 $commit_search = 0;
2370 $pickaxe_search = 1;
2371 }
2372 git_header_html();
2373 print "<div class=\"page_nav\">\n" .
2374 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary;h=$hash")}, "summary") .
2375 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "shortlog") .
2376 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2377 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2378 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2379 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
2380 "<br/><br/>\n" .
2381 "</div>\n";
2382
2383 print "<div>\n" .
2384 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2385 "</div>\n";
2386 print "<table cellspacing=\"0\">\n";
2387 my $alternate = 0;
2388 if ($commit_search) {
2389 $/ = "\0";
2390 open my $fd, "-|", "$gitbin/git-rev-list --header --parents $hash" or next;
2391 while (my $commit_text = <$fd>) {
2392 if (!grep m/$searchtext/i, $commit_text) {
2393 next;
2394 }
2395 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2396 next;
2397 }
2398 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2399 next;
2400 }
2401 my @commit_lines = split "\n", $commit_text;
2402 my %co = git_read_commit(undef, \@commit_lines);
2403 if (!%co) {
2404 next;
2405 }
2406 if ($alternate) {
2407 print "<tr class=\"dark\">\n";
2408 } else {
2409 print "<tr class=\"light\">\n";
2410 }
2411 $alternate ^= 1;
2412 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2413 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2414 "<td>" .
2415 $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/>");
2416 my $comment = $co{'comment'};
2417 foreach my $line (@$comment) {
2418 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2419 my $lead = esc_html($1) || "";
2420 $lead = chop_str($lead, 30, 10);
2421 my $match = esc_html($2) || "";
2422 my $trail = esc_html($3) || "";
2423 $trail = chop_str($trail, 30, 10);
2424 my $text = "$lead<span class=\"match\">$match</span>$trail";
2425 print chop_str($text, 80, 5) . "<br/>\n";
2426 }
2427 }
2428 print "</td>\n" .
2429 "<td class=\"link\">" .
2430 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2431 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2432 print "</td>\n" .
2433 "</tr>\n";
2434 }
2435 close $fd;
2436 }
2437
2438 if ($pickaxe_search) {
2439 $/ = "\n";
2440 open my $fd, "-|", "$gitbin/git-rev-list $hash | $gitbin/git-diff-tree -r --stdin -S\'$searchtext\'";
2441 undef %co;
2442 my @files;
2443 while (my $line = <$fd>) {
2444 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2445 my %set;
2446 $set{'file'} = $6;
2447 $set{'from_id'} = $3;
2448 $set{'to_id'} = $4;
2449 $set{'id'} = $set{'to_id'};
2450 if ($set{'id'} =~ m/0{40}/) {
2451 $set{'id'} = $set{'from_id'};
2452 }
2453 if ($set{'id'} =~ m/0{40}/) {
2454 next;
2455 }
2456 push @files, \%set;
2457 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2458 if (%co) {
2459 if ($alternate) {
2460 print "<tr class=\"dark\">\n";
2461 } else {
2462 print "<tr class=\"light\">\n";
2463 }
2464 $alternate ^= 1;
2465 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2466 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2467 "<td>" .
2468 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
2469 esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2470 while (my $setref = shift @files) {
2471 my %set = %$setref;
2472 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
2473 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2474 "<br/>\n";
2475 }
2476 print "</td>\n" .
2477 "<td class=\"link\">" .
2478 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2479 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2480 print "</td>\n" .
2481 "</tr>\n";
2482 }
2483 %co = git_read_commit($1);
2484 }
2485 }
2486 close $fd;
2487 }
2488 print "</table>\n";
2489 git_footer_html();
2490}
2491
2492sub git_shortlog {
2493 my $head = git_read_head($project);
2494 if (!defined $hash) {
2495 $hash = $head;
2496 }
2497 if (!defined $page) {
2498 $page = 0;
2499 }
2500 my $refs = read_info_ref();
2501 git_header_html();
2502 print "<div class=\"page_nav\">\n" .
2503 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
2504 " | shortlog" .
2505 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash")}, "log") .
2506 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash")}, "commit") .
2507 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash")}, "commitdiff") .
2508 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$hash;hb=$hash")}, "tree") . "<br/>\n";
2509
2510 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2511 open my $fd, "-|", "$gitbin/git-rev-list $limit $hash" or die_error(undef, "Open failed.");
2512 my (@revlist) = map { chomp; $_ } <$fd>;
2513 close $fd;
2514
2515 if ($hash ne $head || $page) {
2516 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "HEAD");
2517 } else {
2518 print "HEAD";
2519 }
2520 if ($page > 0) {
2521 print " ⋅ " .
2522 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
2523 } else {
2524 print " ⋅ prev";
2525 }
2526 if ($#revlist >= (100 * ($page+1)-1)) {
2527 print " ⋅ " .
2528 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
2529 } else {
2530 print " ⋅ next";
2531 }
2532 print "<br/>\n" .
2533 "</div>\n";
2534 print "<div>\n" .
2535 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, " ") .
2536 "</div>\n";
2537 print "<table cellspacing=\"0\">\n";
2538 my $alternate = 0;
2539 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2540 my $commit = $revlist[$i];
2541 my $ref = "";
2542 if (defined $refs->{$commit}) {
2543 $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
2544 }
2545 my %co = git_read_commit($commit);
2546 my %ad = date_str($co{'author_epoch'});
2547 if ($alternate) {
2548 print "<tr class=\"dark\">\n";
2549 } else {
2550 print "<tr class=\"light\">\n";
2551 }
2552 $alternate ^= 1;
2553 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2554 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2555 "<td>";
2556 if (length($co{'title_short'}) < length($co{'title'})) {
2557 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
2558 "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
2559 } else {
2560 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
2561 "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
2562 }
2563 print "</td>\n" .
2564 "<td class=\"link\">" .
2565 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2566 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2567 "</td>\n" .
2568 "</tr>";
2569 }
2570 if ($#revlist >= (100 * ($page+1)-1)) {
2571 print "<tr>\n" .
2572 "<td>" .
2573 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -title => "Alt-n"}, "next") .
2574 "</td>\n" .
2575 "</tr>\n";
2576 }
2577 print "</table\n>";
2578 git_footer_html();
2579}