3 # gitweb - simple web interface to track changes in git repositories
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
8 # This program is licensed under the GPLv2
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
18 binmode STDOUT, ':utf8';
21 our $version = "@@GIT_VERSION@@";
22 our $my_url = $cgi->url();
23 our $my_uri = $cgi->url(-absolute => 1);
26 # core git executable to use
27 # this can just be "git" if your webserver has a sensible PATH
28 our $GIT = "@@GIT_BINDIR@@/git";
30 # absolute fs-path which will be prepended to the project path
31 #our $projectroot = "/pub/scm";
32 our $projectroot = "@@GITWEB_PROJECTROOT@@";
34 # version of the core git binary
35 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
37 # location for temporary files needed for diffs
38 our $git_temp = "/tmp/gitweb";
40 mkdir($git_temp, 0700) || die_error("Couldn't mkdir $git_temp");
43 # target of the home link on top of all pages
44 our $home_link = $my_uri;
46 # name of your site or organization to appear in page titles
47 # replace this with something more descriptive for clearer bookmarks
48 our $site_name = "@@GITWEB_SITENAME@@" || $ENV{'SERVER_NAME'} || "Untitled";
50 # html text to include at home page
51 our $home_text = "@@GITWEB_HOMETEXT@@";
53 # URI of default stylesheet
54 our $stylesheet = "@@GITWEB_CSS@@";
56 # source of projects list
57 our $projects_list = "@@GITWEB_LIST@@" || "$projectroot";
59 # default blob_plain mimetype and default charset for text/plain blob
60 our $default_blob_plain_mimetype = 'text/plain';
61 our $default_text_plain_charset = undef;
63 # file to use for guessing MIME types before trying /etc/mime.types
64 # (relative to the current git repository)
65 our $mimetypes_file = undef;
67 # input validation and dispatch
68 our $action = $cgi->param('a');
69 if (defined $action) {
70 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
72 die_error(undef, "Invalid action parameter.");
74 if ($action eq "git-logo.png") {
77 } elsif ($action eq "opml") {
83 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
84 if (defined $project) {
85 $project =~ s|^/||; $project =~ s|/$||;
86 $project = validate_input($project);
87 if (!defined($project)) {
88 die_error(undef, "Invalid project parameter.");
90 if (!(-d "$projectroot/$project")) {
92 die_error(undef, "No such directory.");
94 if (!(-e "$projectroot/$project/HEAD")) {
96 die_error(undef, "No such project.");
98 $rss_link = "<link rel=\"alternate\" title=\"" . esc_param($project) . " log\" href=\"" .
99 "$my_uri?" . esc_param("p=$project;a=rss") . "\" type=\"application/rss+xml\"/>";
100 $ENV{'GIT_DIR'} = "$projectroot/$project";
106 our $file_name = $cgi->param('f');
107 if (defined $file_name) {
108 $file_name = validate_input($file_name);
109 if (!defined($file_name)) {
110 die_error(undef, "Invalid file parameter.");
114 our $hash = $cgi->param('h');
116 $hash = validate_input($hash);
117 if (!defined($hash)) {
118 die_error(undef, "Invalid hash parameter.");
122 our $hash_parent = $cgi->param('hp');
123 if (defined $hash_parent) {
124 $hash_parent = validate_input($hash_parent);
125 if (!defined($hash_parent)) {
126 die_error(undef, "Invalid hash parent parameter.");
130 our $hash_base = $cgi->param('hb');
131 if (defined $hash_base) {
132 $hash_base = validate_input($hash_base);
133 if (!defined($hash_base)) {
134 die_error(undef, "Invalid hash base parameter.");
138 our $page = $cgi->param('pg');
140 if ($page =~ m/[^0-9]$/) {
142 die_error(undef, "Invalid page parameter.");
146 our $searchtext = $cgi->param('s');
147 if (defined $searchtext) {
148 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
150 die_error(undef, "Invalid search parameter.");
152 $searchtext = quotemeta $searchtext;
157 "blame" => \&git_blame2,
158 "blobdiff" => \&git_blobdiff,
159 "blobdiff_plain" => \&git_blobdiff_plain,
160 "blob" => \&git_blob,
161 "blob_plain" => \&git_blob_plain,
162 "commitdiff" => \&git_commitdiff,
163 "commitdiff_plain" => \&git_commitdiff_plain,
164 "commit" => \&git_commit,
165 "heads" => \&git_heads,
166 "history" => \&git_history,
169 "search" => \&git_search,
170 "shortlog" => \&git_shortlog,
171 "summary" => \&git_summary,
173 "tags" => \&git_tags,
174 "tree" => \&git_tree,
177 $action = 'summary' if (!defined($action));
178 if (!defined($actions{$action})) {
180 die_error(undef, "Unknown action.");
182 $actions{$action}->();
185 ## ======================================================================
186 ## validation, quoting/unquoting and escaping
191 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
194 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
197 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
203 # quote unsafe chars, but keep the slash, even when it's not
204 # correct, but quoted slashes look too horrible in bookmarks
207 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
213 # replace invalid utf8 character with SUBSTITUTION sequence
216 $str = decode("utf8", $str, Encode::FB_DEFAULT);
217 $str = escapeHTML($str);
218 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
222 # git may return quoted and escaped filenames
225 if ($str =~ m/^"(.*)"$/) {
227 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
232 ## ----------------------------------------------------------------------
233 ## HTML aware string manipulation
238 my $add_len = shift || 10;
240 # allow only $len chars, but don't cut a word if it would fit in $add_len
241 # if it doesn't fit, cut it if it's still longer than the dots we would add
242 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
245 if (length($tail) > 4) {
247 $body =~ s/&[^;]*$//; # remove chopped character entities
252 ## ----------------------------------------------------------------------
253 ## functions returning short strings
255 # CSS class for given age value (in seconds)
259 if ($age < 60*60*2) {
261 } elsif ($age < 60*60*24*2) {
268 # convert age in seconds to "nn units ago" string
273 if ($age > 60*60*24*365*2) {
274 $age_str = (int $age/60/60/24/365);
275 $age_str .= " years ago";
276 } elsif ($age > 60*60*24*(365/12)*2) {
277 $age_str = int $age/60/60/24/(365/12);
278 $age_str .= " months ago";
279 } elsif ($age > 60*60*24*7*2) {
280 $age_str = int $age/60/60/24/7;
281 $age_str .= " weeks ago";
282 } elsif ($age > 60*60*24*2) {
283 $age_str = int $age/60/60/24;
284 $age_str .= " days ago";
285 } elsif ($age > 60*60*2) {
286 $age_str = int $age/60/60;
287 $age_str .= " hours ago";
288 } elsif ($age > 60*2) {
289 $age_str = int $age/60;
290 $age_str .= " min ago";
293 $age_str .= " sec ago";
295 $age_str .= " right now";
300 # convert file mode in octal to symbolic file mode string
302 my $mode = oct shift;
304 if (S_ISDIR($mode & S_IFMT)) {
306 } elsif (S_ISLNK($mode)) {
308 } elsif (S_ISREG($mode)) {
309 # git cares only about the executable bit
310 if ($mode & S_IXUSR) {
320 # convert file mode in octal to file type string
322 my $mode = oct shift;
324 if (S_ISDIR($mode & S_IFMT)) {
326 } elsif (S_ISLNK($mode)) {
328 } elsif (S_ISREG($mode)) {
335 ## ----------------------------------------------------------------------
336 ## functions returning short HTML fragments, or transforming HTML fragments
337 ## which don't beling to other sections
339 # format line of commit message or tag comment
340 sub format_log_line_html {
343 $line = esc_html($line);
344 $line =~ s/ / /g;
345 if ($line =~ m/([0-9a-fA-F]{40})/) {
347 if (git_get_type($hash_text) eq "commit") {
348 my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
349 $line =~ s/$hash_text/$link/;
355 # format marker of refs pointing to given object
356 sub git_get_referencing {
357 my ($refs, $id) = @_;
359 if (defined $refs->{$id}) {
360 return ' <span class="tag">' . esc_html($refs->{$id}) . '</span>';
366 ## ----------------------------------------------------------------------
367 ## git utility subroutines, invoking git commands
369 # get HEAD ref of given project as hash
372 my $oENV = $ENV{'GIT_DIR'};
374 $ENV{'GIT_DIR'} = "$projectroot/$project";
375 if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
378 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
383 $ENV{'GIT_DIR'} = $oENV;
388 # get type of given object
392 open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
399 sub git_get_project_config {
402 return unless ($key);
403 $key =~ s/^gitweb\.//;
404 return if ($key =~ m/\W/);
406 my $val = qx($GIT repo-config --get gitweb.$key);
410 sub git_get_project_config_bool {
411 my $val = git_get_project_config (@_);
412 if ($val and $val =~ m/true|yes|on/) {
415 return; # implicit false
418 # get hash of given path at given ref
419 sub git_get_hash_by_path {
421 my $path = shift || return undef;
425 open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
426 or die_error(undef, "Open git-ls-tree failed.");
428 close $fd or return undef;
430 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
431 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
435 ## ......................................................................
436 ## git utility functions, directly accessing git repository
438 # assumes that PATH is not symref
442 open my $fd, "$projectroot/$path" or return undef;
446 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
451 sub git_read_description {
454 open my $fd, "$projectroot/$path/description" or return undef;
461 sub git_read_projects {
464 if (-d $projects_list) {
465 # search in directory
466 my $dir = $projects_list;
467 opendir my ($dh), $dir or return undef;
468 while (my $dir = readdir($dh)) {
469 if (-e "$projectroot/$dir/HEAD") {
477 } elsif (-f $projects_list) {
478 # read from file(url-encoded):
479 # 'git%2Fgit.git Linus+Torvalds'
480 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
481 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
482 open my ($fd), $projects_list or return undef;
483 while (my $line = <$fd>) {
485 my ($path, $owner) = split ' ', $line;
486 $path = unescape($path);
487 $owner = unescape($owner);
488 if (!defined $path) {
491 if (-e "$projectroot/$path/HEAD") {
494 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
501 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
506 my $type = shift || "";
508 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
509 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
510 open my $fd, "$projectroot/$project/info/refs" or return;
511 while (my $line = <$fd>) {
513 # attention: for $type == "" it saves only last path part of ref name
514 # e.g. from 'refs/heads/jn/gitweb' it would leave only 'gitweb'
515 if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
516 if (defined $refs{$1}) {
517 $refs{$1} .= " / $2";
527 ## ----------------------------------------------------------------------
528 ## parse to hash functions
532 my $tz = shift || "-0000";
535 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
536 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
537 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
538 $date{'hour'} = $hour;
539 $date{'minute'} = $min;
540 $date{'mday'} = $mday;
541 $date{'day'} = $days[$wday];
542 $date{'month'} = $months[$mon];
543 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
544 $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
546 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
547 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
548 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
549 $date{'hour_local'} = $hour;
550 $date{'minute_local'} = $min;
551 $date{'tz_local'} = $tz;
560 open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
561 $tag{'id'} = $tag_id;
562 while (my $line = <$fd>) {
564 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
566 } elsif ($line =~ m/^type (.+)$/) {
568 } elsif ($line =~ m/^tag (.+)$/) {
570 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
574 } elsif ($line =~ m/--BEGIN/) {
575 push @comment, $line;
577 } elsif ($line eq "") {
581 push @comment, <$fd>;
582 $tag{'comment'} = \@comment;
584 if (!defined $tag{'name'}) {
590 sub git_read_commit {
591 my $commit_id = shift;
592 my $commit_text = shift;
597 if (defined $commit_text) {
598 @commit_lines = @$commit_text;
601 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id or return;
602 @commit_lines = split '\n', <$fd>;
607 my $header = shift @commit_lines;
608 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
611 ($co{'id'}, my @parents) = split ' ', $header;
612 $co{'parents'} = \@parents;
613 $co{'parent'} = $parents[0];
614 while (my $line = shift @commit_lines) {
615 last if $line eq "\n";
616 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
618 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
620 $co{'author_epoch'} = $2;
621 $co{'author_tz'} = $3;
622 if ($co{'author'} =~ m/^([^<]+) </) {
623 $co{'author_name'} = $1;
625 $co{'author_name'} = $co{'author'};
627 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
628 $co{'committer'} = $1;
629 $co{'committer_epoch'} = $2;
630 $co{'committer_tz'} = $3;
631 $co{'committer_name'} = $co{'committer'};
632 $co{'committer_name'} =~ s/ <.*//;
635 if (!defined $co{'tree'}) {
639 foreach my $title (@commit_lines) {
642 $co{'title'} = chop_str($title, 80, 5);
643 # remove leading stuff of merges to make the interesting part visible
644 if (length($title) > 50) {
645 $title =~ s/^Automatic //;
646 $title =~ s/^merge (of|with) /Merge ... /i;
647 if (length($title) > 50) {
648 $title =~ s/(http|rsync):\/\///;
650 if (length($title) > 50) {
651 $title =~ s/(master|www|rsync)\.//;
653 if (length($title) > 50) {
654 $title =~ s/kernel.org:?//;
656 if (length($title) > 50) {
657 $title =~ s/\/pub\/scm//;
660 $co{'title_short'} = chop_str($title, 50, 5);
664 # remove added spaces
665 foreach my $line (@commit_lines) {
668 $co{'comment'} = \@commit_lines;
670 my $age = time - $co{'committer_epoch'};
672 $co{'age_string'} = age_string($age);
673 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
674 if ($age > 60*60*24*7*2) {
675 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
676 $co{'age_string_age'} = $co{'age_string'};
678 $co{'age_string_date'} = $co{'age_string'};
679 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
684 ## ......................................................................
685 ## parse to array of hashes functions
692 my $pfxlen = length("$projectroot/$project/$ref_dir");
693 File::Find::find(sub {
696 push @refs, substr($File::Find::name, $pfxlen + 1);
698 }, "$projectroot/$project/$ref_dir");
700 foreach my $ref_file (@refs) {
701 my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
702 my $type = git_get_type($ref_id) || next;
705 $ref_item{'type'} = $type;
706 $ref_item{'id'} = $ref_id;
707 $ref_item{'epoch'} = 0;
708 $ref_item{'age'} = "unknown";
709 if ($type eq "tag") {
710 my %tag = git_read_tag($ref_id);
711 $ref_item{'comment'} = $tag{'comment'};
712 if ($tag{'type'} eq "commit") {
713 %co = git_read_commit($tag{'object'});
714 $ref_item{'epoch'} = $co{'committer_epoch'};
715 $ref_item{'age'} = $co{'age_string'};
716 } elsif (defined($tag{'epoch'})) {
717 my $age = time - $tag{'epoch'};
718 $ref_item{'epoch'} = $tag{'epoch'};
719 $ref_item{'age'} = age_string($age);
721 $ref_item{'reftype'} = $tag{'type'};
722 $ref_item{'name'} = $tag{'name'};
723 $ref_item{'refid'} = $tag{'object'};
724 } elsif ($type eq "commit"){
725 %co = git_read_commit($ref_id);
726 $ref_item{'reftype'} = "commit";
727 $ref_item{'name'} = $ref_file;
728 $ref_item{'title'} = $co{'title'};
729 $ref_item{'refid'} = $ref_id;
730 $ref_item{'epoch'} = $co{'committer_epoch'};
731 $ref_item{'age'} = $co{'age_string'};
733 $ref_item{'reftype'} = $type;
734 $ref_item{'name'} = $ref_file;
735 $ref_item{'refid'} = $ref_id;
738 push @reflist, \%ref_item;
741 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
745 ## ----------------------------------------------------------------------
746 ## filesystem-related functions
751 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
752 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
753 if (!defined $gcos) {
757 $owner =~ s/[,;].*$//;
758 return decode("utf8", $owner, Encode::FB_DEFAULT);
761 ## ......................................................................
762 ## mimetype related functions
764 sub mimetype_guess_file {
765 my $filename = shift;
767 -r $mimemap or return undef;
770 open(MIME, $mimemap) or return undef;
772 my ($mime, $exts) = split(/\t+/);
774 my @exts = split(/\s+/, $exts);
775 foreach my $ext (@exts) {
776 $mimemap{$ext} = $mime;
782 $filename =~ /\.(.*?)$/;
787 my $filename = shift;
789 $filename =~ /\./ or return undef;
791 if ($mimetypes_file) {
792 my $file = $mimetypes_file;
793 #$file =~ m#^/# or $file = "$projectroot/$path/$file";
794 $mime = mimetype_guess_file($filename, $file);
796 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
800 sub git_blob_plain_mimetype {
802 my $filename = shift;
805 my $mime = mimetype_guess($filename);
806 $mime and return $mime;
810 return $default_blob_plain_mimetype unless $fd;
813 return 'text/plain' .
814 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
815 } elsif (! $filename) {
816 return 'application/octet-stream';
817 } elsif ($filename =~ m/\.png$/i) {
819 } elsif ($filename =~ m/\.gif$/i) {
821 } elsif ($filename =~ m/\.jpe?g$/i) {
824 return 'application/octet-stream';
828 ## ======================================================================
829 ## functions printing HTML: header, footer, error page
831 sub git_header_html {
832 my $status = shift || "200 OK";
835 my $title = "$site_name git";
836 if (defined $project) {
837 $title .= " - $project";
838 if (defined $action) {
839 $title .= "/$action";
840 if (defined $file_name) {
841 $title .= " - $file_name";
842 if ($action eq "tree" && $file_name !~ m|/$|) {
849 # require explicit support from the UA if we are to send the page as
850 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
851 # we have to do this because MSIE sometimes globs '*/*', pretending to
852 # support xhtml+xml but choking when it gets what it asked for.
853 if ($cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) {
854 $content_type = 'application/xhtml+xml';
856 $content_type = 'text/html';
858 print $cgi->header(-type=>$content_type, -charset => 'utf-8', -status=> $status, -expires => $expires);
860 <?xml version="1.0" encoding="utf-8"?>
861 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
862 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
863 <!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
864 <!-- git core binaries version $git_version -->
866 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
867 <meta name="robots" content="index, nofollow"/>
868 <title>$title</title>
869 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
874 print "<div class=\"page_header\">\n" .
875 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
876 "<img src=\"$my_uri?" . esc_param("a=git-logo.png") . "\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
878 print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
879 if (defined $project) {
880 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
881 if (defined $action) {
885 if (!defined $searchtext) {
889 if (defined $hash_base) {
890 $search_hash = $hash_base;
891 } elsif (defined $hash) {
892 $search_hash = $hash;
894 $search_hash = "HEAD";
896 $cgi->param("a", "search");
897 $cgi->param("h", $search_hash);
898 print $cgi->startform(-method => "get", -action => $my_uri) .
899 "<div class=\"search\">\n" .
900 $cgi->hidden(-name => "p") . "\n" .
901 $cgi->hidden(-name => "a") . "\n" .
902 $cgi->hidden(-name => "h") . "\n" .
903 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
905 $cgi->end_form() . "\n";
910 sub git_footer_html {
911 print "<div class=\"page_footer\">\n";
912 if (defined $project) {
913 my $descr = git_read_description($project);
914 if (defined $descr) {
915 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
917 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
919 print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
927 my $status = shift || "403 Forbidden";
928 my $error = shift || "Malformed query, file missing or permission denied";
930 git_header_html($status);
931 print "<div class=\"page_body\">\n" .
933 "$status - $error\n" .
940 ## ----------------------------------------------------------------------
941 ## functions printing or outputting HTML: navigation
944 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
945 $extra = '' if !defined $extra; # pager or formats
947 my @navs = qw(summary shortlog log commit commitdiff tree);
949 @navs = grep { $_ ne $suppress } @navs;
952 my %arg = map { $_, ''} @navs;
954 for (qw(commit commitdiff)) {
955 $arg{$_} = ";h=$head";
957 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
958 for (qw(shortlog log)) {
959 $arg{$_} = ";h=$head";
963 $arg{tree} .= ";h=$treehead" if defined $treehead;
964 $arg{tree} .= ";hb=$treebase" if defined $treebase;
966 print "<div class=\"page_nav\">\n" .
970 : $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$_$arg{$_}")}, "$_")
973 print "<br/>\n$extra<br/>\n" .
977 sub git_get_paging_nav {
978 my ($action, $hash, $head, $page, $nrevs) = @_;
982 if ($hash ne $head || $page) {
983 $paging_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action")}, "HEAD");
985 $paging_nav .= "HEAD";
989 $paging_nav .= " ⋅ " .
990 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page-1)),
991 -accesskey => "p", -title => "Alt-p"}, "prev");
993 $paging_nav .= " ⋅ prev";
996 if ($nrevs >= (100 * ($page+1)-1)) {
997 $paging_nav .= " ⋅ " .
998 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page+1)),
999 -accesskey => "n", -title => "Alt-n"}, "next");
1001 $paging_nav .= " ⋅ next";
1007 ## ......................................................................
1008 ## functions printing or outputting HTML: div
1010 sub git_header_div {
1011 my ($action, $title, $hash, $hash_base) = @_;
1014 $rest .= ";h=$hash" if $hash;
1015 $rest .= ";hb=$hash_base" if $hash_base;
1017 print "<div class=\"header\">\n" .
1018 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action$rest"),
1019 -class => "title"}, $title ? $title : $action) . "\n" .
1023 sub git_print_page_path {
1027 if (!defined $name) {
1028 print "<div class=\"page_path\"><b>/</b></div>\n";
1029 } elsif (defined $type && $type eq 'blob') {
1030 print "<div class=\"page_path\"><b>" .
1031 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "</b><br/></div>\n";
1033 print "<div class=\"page_path\"><b>" . esc_html($name) . "</b><br/></div>\n";
1037 ## ......................................................................
1038 ## functions printing large fragments of HTML
1040 sub git_shortlog_body {
1041 # uses global variable $project
1042 my ($revlist, $from, $to, $refs, $extra) = @_;
1043 $from = 0 unless defined $from;
1044 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1046 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1048 for (my $i = $from; $i <= $to; $i++) {
1049 my $commit = $revlist->[$i];
1050 #my $ref = defined $refs ? git_get_referencing($refs, $commit) : '';
1051 my $ref = git_get_referencing($refs, $commit);
1052 my %co = git_read_commit($commit);
1053 my %ad = date_str($co{'author_epoch'});
1055 print "<tr class=\"dark\">\n";
1057 print "<tr class=\"light\">\n";
1060 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1061 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1062 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1064 if (length($co{'title_short'}) < length($co{'title'})) {
1065 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"),
1066 -class => "list", -title => "$co{'title'}"},
1067 "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
1069 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"),
1071 "<b>" . esc_html($co{'title'}) . "$ref</b>");
1074 "<td class=\"link\">" .
1075 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") . " | " .
1076 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1080 if (defined $extra) {
1082 "<td colspan=\"4\">$extra</td>\n" .
1089 # uses global variable $project
1090 my ($taglist, $from, $to, $extra) = @_;
1091 $from = 0 unless defined $from;
1092 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1094 print "<table class=\"tags\" cellspacing=\"0\">\n";
1096 for (my $i = $from; $i <= $to; $i++) {
1097 my $entry = $taglist->[$i];
1099 my $comment_lines = $tag{'comment'};
1100 my $comment = shift @$comment_lines;
1102 if (defined $comment) {
1103 $comment_short = chop_str($comment, 30, 5);
1106 print "<tr class=\"dark\">\n";
1108 print "<tr class=\"light\">\n";
1111 print "<td><i>$tag{'age'}</i></td>\n" .
1113 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"),
1114 -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1117 if (defined $comment) {
1118 if (length($comment_short) < length($comment)) {
1119 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
1120 -class => "list", -title => $comment}, $comment_short);
1122 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
1123 -class => "list"}, $comment);
1127 "<td class=\"selflink\">";
1128 if ($tag{'type'} eq "tag") {
1129 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag");
1134 "<td class=\"link\">" . " | " .
1135 $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 } elsif ($tag{'reftype'} eq "blob") {
1140 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$tag{'refid'}")}, "raw");
1145 if (defined $extra) {
1147 "<td colspan=\"5\">$extra</td>\n" .
1153 sub git_heads_body {
1154 # uses global variable $project
1155 my ($taglist, $head, $from, $to, $extra) = @_;
1156 $from = 0 unless defined $from;
1157 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1159 print "<table class=\"heads\" cellspacing=\"0\">\n";
1161 for (my $i = $from; $i <= $to; $i++) {
1162 my $entry = $taglist->[$i];
1164 my $curr = $tag{'id'} eq $head;
1166 print "<tr class=\"dark\">\n";
1168 print "<tr class=\"light\">\n";
1171 print "<td><i>$tag{'age'}</i></td>\n" .
1172 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1173 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"),
1174 -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1176 "<td class=\"link\">" .
1177 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") . " | " .
1178 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1182 if (defined $extra) {
1184 "<td colspan=\"3\">$extra</td>\n" .
1190 ## ----------------------------------------------------------------------
1191 ## functions printing large fragments, format as one of arguments
1193 sub git_diff_print {
1195 my $from_name = shift;
1197 my $to_name = shift;
1198 my $format = shift || "html";
1200 my $from_tmp = "/dev/null";
1201 my $to_tmp = "/dev/null";
1204 # create tmp from-file
1205 if (defined $from) {
1206 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1207 open my $fd2, "> $from_tmp";
1208 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1215 # create tmp to-file
1217 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1218 open my $fd2, "> $to_tmp";
1219 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1226 open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1227 if ($format eq "plain") {
1232 while (my $line = <$fd>) {
1234 my $char = substr($line, 0, 1);
1235 my $diff_class = "";
1237 $diff_class = " add";
1238 } elsif ($char eq "-") {
1239 $diff_class = " rem";
1240 } elsif ($char eq "@") {
1241 $diff_class = " chunk_header";
1242 } elsif ($char eq "\\") {
1246 while ((my $pos = index($line, "\t")) != -1) {
1247 if (my $count = (8 - (($pos-1) % 8))) {
1248 my $spaces = ' ' x $count;
1249 $line =~ s/\t/$spaces/;
1252 print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1257 if (defined $from) {
1266 ## ======================================================================
1267 ## ======================================================================
1270 # git-logo (cached in browser for one day)
1272 binmode STDOUT, ':raw';
1273 print $cgi->header(-type => 'image/png', -expires => '+1d');
1274 # cat git-logo.png | hexdump -e '16/1 " %02x" "\n"' | sed 's/ /\\x/g'
1275 print "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
1276 "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
1277 "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
1278 "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
1279 "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
1280 "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
1281 "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
1282 "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
1283 "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
1284 "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
1285 "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
1286 "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
1287 "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
1290 sub git_project_list {
1291 my $order = $cgi->param('o');
1292 if (defined $order && $order !~ m/project|descr|owner|age/) {
1293 die_error(undef, "Invalid order parameter '$order'.");
1296 my @list = git_read_projects();
1299 die_error(undef, "No projects found.");
1301 foreach my $pr (@list) {
1302 my $head = git_read_head($pr->{'path'});
1303 if (!defined $head) {
1306 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1307 my %co = git_read_commit($head);
1311 $pr->{'commit'} = \%co;
1312 if (!defined $pr->{'descr'}) {
1313 my $descr = git_read_description($pr->{'path'}) || "";
1314 $pr->{'descr'} = chop_str($descr, 25, 5);
1316 if (!defined $pr->{'owner'}) {
1317 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1319 push @projects, $pr;
1323 if (-f $home_text) {
1324 print "<div class=\"index_include\">\n";
1325 open (my $fd, $home_text);
1330 print "<table class=\"project_list\">\n" .
1332 $order ||= "project";
1333 if ($order eq "project") {
1334 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1335 print "<th>Project</th>\n";
1338 $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
1339 -class => "header"}, "Project") .
1342 if ($order eq "descr") {
1343 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1344 print "<th>Description</th>\n";
1347 $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
1348 -class => "header"}, "Description") .
1351 if ($order eq "owner") {
1352 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1353 print "<th>Owner</th>\n";
1356 $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
1357 -class => "header"}, "Owner") .
1360 if ($order eq "age") {
1361 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
1362 print "<th>Last Change</th>\n";
1365 $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
1366 -class => "header"}, "Last Change") .
1369 print "<th></th>\n" .
1372 foreach my $pr (@projects) {
1374 print "<tr class=\"dark\">\n";
1376 print "<tr class=\"light\">\n";
1379 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"),
1380 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
1381 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
1382 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
1383 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
1384 $pr->{'commit'}{'age_string'} . "</td>\n" .
1385 "<td class=\"link\">" .
1386 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") . " | " .
1387 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") . " | " .
1388 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
1397 my $descr = git_read_description($project) || "none";
1398 my $head = git_read_head($project);
1399 my %co = git_read_commit($head);
1400 my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1403 if (-f $projects_list) {
1404 open (my $fd , $projects_list);
1405 while (my $line = <$fd>) {
1407 my ($pr, $ow) = split ' ', $line;
1408 $pr = unescape($pr);
1409 $ow = unescape($ow);
1410 if ($pr eq $project) {
1411 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
1417 if (!defined $owner) {
1418 $owner = get_file_owner("$projectroot/$project");
1421 my $refs = read_info_ref();
1423 git_page_nav('summary','', $head);
1425 print "<div class=\"title\"> </div>\n";
1426 print "<table cellspacing=\"0\">\n" .
1427 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1428 "<tr><td>owner</td><td>$owner</td></tr>\n" .
1429 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
1432 open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_read_head($project)
1433 or die_error(undef, "Open git-rev-list failed.");
1434 my @revlist = map { chomp; $_ } <$fd>;
1436 git_header_div('shortlog');
1437 git_shortlog_body(\@revlist, 0, 15, $refs,
1438 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "..."));
1440 my $taglist = git_read_refs("refs/tags");
1441 if (defined @$taglist) {
1442 git_header_div('tags');
1443 git_tags_body($taglist, 0, 15,
1444 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "..."));
1447 my $headlist = git_read_refs("refs/heads");
1448 if (defined @$headlist) {
1449 git_header_div('heads');
1450 git_heads_body($headlist, $head, 0, 15,
1451 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "..."));
1458 my $head = git_read_head($project);
1460 git_page_nav('','', $head,undef,$head);
1461 my %tag = git_read_tag($hash);
1462 git_header_div('commit', esc_html($tag{'name'}), $hash);
1463 print "<div class=\"title_text\">\n" .
1464 "<table cellspacing=\"0\">\n" .
1466 "<td>object</td>\n" .
1467 "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
1468 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
1470 if (defined($tag{'author'})) {
1471 my %ad = date_str($tag{'epoch'}, $tag{'tz'});
1472 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1473 print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1475 print "</table>\n\n" .
1477 print "<div class=\"page_body\">";
1478 my $comment = $tag{'comment'};
1479 foreach my $line (@$comment) {
1480 print esc_html($line) . "<br/>\n";
1489 die_error(undef, "Permission denied.") if (!git_get_project_config_bool ('blame'));
1490 die_error('404 Not Found', "File name not defined") if (!$file_name);
1491 $hash_base ||= git_read_head($project);
1492 die_error(undef, "Reading commit failed") unless ($hash_base);
1493 my %co = git_read_commit($hash_base)
1494 or die_error(undef, "Reading commit failed");
1495 if (!defined $hash) {
1496 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1497 or die_error(undef, "Error looking up file");
1499 $ftype = git_get_type($hash);
1500 if ($ftype !~ "blob") {
1501 die_error("400 Bad Request", "object is not a blob");
1503 open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
1504 or die_error(undef, "Open git-blame failed.");
1507 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1508 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1509 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1510 git_header_div('commit', esc_html($co{'title'}), $hash_base);
1511 git_print_page_path($file_name, $ftype);
1512 my @rev_color = (qw(light dark));
1513 my $num_colors = scalar(@rev_color);
1514 my $current_color = 0;
1516 print "<div class=\"page_body\">\n";
1517 print "<table class=\"blame\">\n";
1518 print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
1520 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
1522 my $rev = substr($full_rev, 0, 8);
1526 if (!defined $last_rev) {
1527 $last_rev = $full_rev;
1528 } elsif ($last_rev ne $full_rev) {
1529 $last_rev = $full_rev;
1530 $current_color = ++$current_color % $num_colors;
1532 print "<tr class=\"$rev_color[$current_color]\">\n";
1533 print "<td class=\"sha1\">" .
1534 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$full_rev;f=$file_name")}, esc_html($rev)) . "</td>\n";
1535 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" . esc_html($lineno) . "</a></td>\n";
1536 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
1541 close $fd or print "Reading blob failed\n";
1547 die_error('403 Permission denied', "Permission denied.") if (!git_get_project_config_bool ('blame'));
1548 die_error('404 Not Found', "What file will it be, master?") if (!$file_name);
1549 $hash_base ||= git_read_head($project);
1550 die_error(undef, "Reading commit failed.") unless ($hash_base);
1551 my %co = git_read_commit($hash_base)
1552 or die_error(undef, "Reading commit failed.");
1553 if (!defined $hash) {
1554 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1555 or die_error(undef, "Error lookup file.");
1557 open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
1558 or die_error(undef, "Open git-annotate failed.");
1561 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1562 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1563 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1564 git_header_div('commit', esc_html($co{'title'}), $hash_base);
1565 git_print_page_path($file_name, 'blob');
1566 print "<div class=\"page_body\">\n";
1568 <table class="blame">
1577 my @line_class = (qw(light dark));
1578 my $line_class_len = scalar (@line_class);
1579 my $line_class_num = $#line_class;
1580 while (my $line = <$fd>) {
1592 $line_class_num = ($line_class_num + 1) % $line_class_len;
1594 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
1601 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
1604 $short_rev = substr ($long_rev, 0, 8);
1605 $age = time () - $time;
1606 $age_str = age_string ($age);
1607 $age_str =~ s/ / /g;
1608 $age_class = age_class($age);
1609 $author = esc_html ($author);
1610 $author =~ s/ / /g;
1612 while ((my $pos = index($data, "\t")) != -1) {
1613 if (my $count = (8 - ($pos % 8))) {
1614 my $spaces = ' ' x $count;
1615 $data =~ s/\t/$spaces/;
1618 $data = esc_html ($data);
1621 <tr class="$line_class[$line_class_num]">
1622 <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1623 <td class="$age_class">$age_str</td>
1625 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1626 <td class="pre">$data</td>
1629 } # while (my $line = <$fd>)
1630 print "</table>\n\n";
1631 close $fd or print "Reading blob failed.\n";
1637 my $head = git_read_head($project);
1639 git_page_nav('','', $head,undef,$head);
1640 git_header_div('summary', $project);
1642 my $taglist = git_read_refs("refs/tags");
1643 if (defined @$taglist) {
1644 git_tags_body($taglist);
1650 my $head = git_read_head($project);
1652 git_page_nav('','', $head,undef,$head);
1653 git_header_div('summary', $project);
1655 my $taglist = git_read_refs("refs/heads");
1657 if (defined @$taglist) {
1658 git_heads_body($taglist, $head);
1663 sub git_blob_plain {
1664 if (!defined $hash) {
1665 if (defined $file_name) {
1666 my $base = $hash_base || git_read_head($project);
1667 $hash = git_get_hash_by_path($base, $file_name, "blob")
1668 or die_error(undef, "Error lookup file.");
1670 die_error(undef, "No file name defined.");
1674 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1675 or die_error("Couldn't cat $file_name, $hash");
1677 $type ||= git_blob_plain_mimetype($fd, $file_name);
1679 # save as filename, even when no $file_name is given
1680 my $save_as = "$hash";
1681 if (defined $file_name) {
1682 $save_as = $file_name;
1683 } elsif ($type =~ m/^text\//) {
1687 print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
1689 binmode STDOUT, ':raw';
1691 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
1697 if (!defined $hash) {
1698 if (defined $file_name) {
1699 my $base = $hash_base || git_read_head($project);
1700 $hash = git_get_hash_by_path($base, $file_name, "blob")
1701 or die_error(undef, "Error lookup file.");
1703 die_error(undef, "No file name defined.");
1706 my $have_blame = git_get_project_config_bool ('blame');
1707 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1708 or die_error(undef, "Couldn't cat $file_name, $hash.");
1709 my $mimetype = git_blob_plain_mimetype($fd, $file_name);
1710 if ($mimetype !~ m/^text\//) {
1712 return git_blob_plain($mimetype);
1715 my $formats_nav = '';
1716 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1717 if (defined $file_name) {
1719 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
1722 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1723 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head");
1725 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain");
1727 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1728 git_header_div('commit', esc_html($co{'title'}), $hash_base);
1730 print "<div class=\"page_nav\">\n" .
1731 "<br/><br/></div>\n" .
1732 "<div class=\"title\">$hash</div>\n";
1734 git_print_page_path($file_name, "blob");
1735 print "<div class=\"page_body\">\n";
1737 while (my $line = <$fd>) {
1740 while ((my $pos = index($line, "\t")) != -1) {
1741 if (my $count = (8 - ($pos % 8))) {
1742 my $spaces = ' ' x $count;
1743 $line =~ s/\t/$spaces/;
1746 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1748 close $fd or print "Reading blob failed.\n";
1754 if (!defined $hash) {
1755 $hash = git_read_head($project);
1756 if (defined $file_name) {
1757 my $base = $hash_base || $hash;
1758 $hash = git_get_hash_by_path($base, $file_name, "tree");
1760 if (!defined $hash_base) {
1765 open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
1766 or die_error(undef, "Open git-ls-tree failed.");
1767 my @entries = map { chomp; $_ } <$fd>;
1768 close $fd or die_error(undef, "Reading tree failed.");
1771 my $refs = read_info_ref();
1772 my $ref = git_get_referencing($refs, $hash_base);
1776 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1777 $base_key = ";hb=$hash_base";
1778 git_page_nav('tree','', $hash_base);
1779 git_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
1781 print "<div class=\"page_nav\">\n";
1782 print "<br/><br/></div>\n";
1783 print "<div class=\"title\">$hash</div>\n";
1785 if (defined $file_name) {
1786 $base = esc_html("$file_name/");
1788 git_print_page_path($file_name, 'tree');
1789 print "<div class=\"page_body\">\n";
1790 print "<table cellspacing=\"0\">\n";
1792 foreach my $line (@entries) {
1793 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1794 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1798 my $t_name = validate_input($4);
1800 print "<tr class=\"dark\">\n";
1802 print "<tr class=\"light\">\n";
1805 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
1806 if ($t_type eq "blob") {
1807 print "<td class=\"list\">" .
1808 $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)) .
1810 "<td class=\"link\">" .
1811 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
1812 # " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") .
1813 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$t_hash;hb=$hash_base;f=$base$t_name")}, "history") .
1814 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") .
1816 } elsif ($t_type eq "tree") {
1817 print "<td class=\"list\">" .
1818 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
1820 "<td class=\"link\">" .
1821 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
1822 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") .
1827 print "</table>\n" .
1833 my $head = git_read_head($project);
1834 if (!defined $hash) {
1837 if (!defined $page) {
1840 my $refs = read_info_ref();
1842 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1843 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
1844 or die_error(undef, "Open git-rev-list failed.");
1845 my @revlist = map { chomp; $_ } <$fd>;
1848 my $paging_nav = git_get_paging_nav('log', $hash, $head, $page, $#revlist);
1851 git_page_nav('log','', $hash,undef,undef, $paging_nav);
1854 my %co = git_read_commit($hash);
1856 git_header_div('summary', $project);
1857 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1859 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1860 my $commit = $revlist[$i];
1861 my $ref = git_get_referencing($refs, $commit);
1862 my %co = git_read_commit($commit);
1864 my %ad = date_str($co{'author_epoch'});
1865 git_header_div('commit',
1866 "<span class=\"age\">$co{'age_string'}</span>" .
1867 esc_html($co{'title'}) . $ref,
1869 print "<div class=\"title_text\">\n" .
1870 "<div class=\"log_link\">\n" .
1871 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1872 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1875 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
1877 "<div class=\"log_body\">\n";
1878 my $comment = $co{'comment'};
1880 foreach my $line (@$comment) {
1881 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1892 print format_log_line_html($line) . "<br/>\n";
1903 my %co = git_read_commit($hash);
1905 die_error(undef, "Unknown commit object.");
1907 my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1908 my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1910 my $parent = $co{'parent'};
1911 if (!defined $parent) {
1914 open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
1915 or die_error(undef, "Open git-diff-tree failed.");
1916 my @difftree = map { chomp; $_ } <$fd>;
1917 close $fd or die_error(undef, "Reading git-diff-tree failed.");
1919 # non-textual hash id's can be cached
1921 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1924 my $refs = read_info_ref();
1925 my $ref = git_get_referencing($refs, $co{'id'});
1926 my $formats_nav = '';
1927 if (defined $file_name && defined $co{'parent'}) {
1928 my $parent = $co{'parent'};
1929 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;hb=$parent;f=$file_name")}, "blame");
1931 git_header_html(undef, $expires);
1932 git_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
1933 $hash, $co{'tree'}, $hash,
1936 if (defined $co{'parent'}) {
1937 git_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
1939 git_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
1941 print "<div class=\"title_text\">\n" .
1942 "<table cellspacing=\"0\">\n";
1943 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
1945 "<td></td><td> $ad{'rfc2822'}";
1946 if ($ad{'hour_local'} < 6) {
1947 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1949 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1953 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
1954 print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1955 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
1958 "<td class=\"sha1\">" .
1959 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
1961 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
1964 my $parents = $co{'parents'};
1965 foreach my $par (@$parents) {
1968 "<td class=\"sha1\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
1969 "<td class=\"link\">" .
1970 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
1971 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
1977 print "<div class=\"page_body\">\n";
1978 my $comment = $co{'comment'};
1981 foreach my $line (@$comment) {
1982 # print only one empty line
1984 if ($empty || $signed) {
1991 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1993 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1996 print format_log_line_html($line) . "<br/>\n";
2000 print "<div class=\"list_head\">\n";
2001 if ($#difftree > 10) {
2002 print(($#difftree + 1) . " files changed:\n");
2005 print "<table class=\"diff_tree\">\n";
2007 foreach my $line (@difftree) {
2008 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2009 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2010 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2018 my $similarity = $6;
2019 my $file = validate_input(unquote($7));
2021 print "<tr class=\"dark\">\n";
2023 print "<tr class=\"light\">\n";
2026 if ($status eq "A") {
2028 if (S_ISREG(oct $to_mode)) {
2029 $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
2032 $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" .
2033 "<td><span class=\"file_status new\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
2034 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") . "</td>\n";
2035 } elsif ($status eq "D") {
2037 $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" .
2038 "<td><span class=\"file_status deleted\">[deleted " . file_type($from_mode). "]</span></td>\n" .
2039 "<td class=\"link\">" .
2040 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") .
2041 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") .
2043 } elsif ($status eq "M" || $status eq "T") {
2044 my $mode_chnge = "";
2045 if ($from_mode != $to_mode) {
2046 $mode_chnge = " <span class=\"file_status mode_chnge\">[changed";
2047 if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
2048 $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
2050 if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
2051 if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
2052 $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
2053 } elsif (S_ISREG($to_mode)) {
2054 $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
2057 $mode_chnge .= "]</span>\n";
2060 if ($to_id ne $from_id) {
2061 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));
2063 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
2066 "<td>$mode_chnge</td>\n" .
2067 "<td class=\"link\">";
2068 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$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=$file")}, "diff");
2072 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") . "\n";
2074 } elsif ($status eq "R") {
2075 my ($from_file, $to_file) = split "\t", $file;
2077 if ($from_mode != $to_mode) {
2078 $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
2081 $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" .
2082 "<td><span class=\"file_status moved\">[moved from " .
2083 $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)) .
2084 " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
2085 "<td class=\"link\">" .
2086 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
2087 if ($to_id ne $from_id) {
2088 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file")}, "diff");
2099 mkdir($git_temp, 0700);
2101 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
2103 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2104 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2105 git_header_div('commit', esc_html($co{'title'}), $hash_base);
2107 print "<div class=\"page_nav\">\n" .
2108 "<br/><br/></div>\n" .
2109 "<div class=\"title\">$hash vs $hash_parent</div>\n";
2111 git_print_page_path($file_name, "blob");
2112 print "<div class=\"page_body\">\n" .
2113 "<div class=\"diff_info\">blob:" .
2114 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
2116 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
2118 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2123 sub git_blobdiff_plain {
2124 mkdir($git_temp, 0700);
2125 print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2126 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2129 sub git_commitdiff {
2130 mkdir($git_temp, 0700);
2131 my %co = git_read_commit($hash);
2133 die_error(undef, "Unknown commit object.");
2135 if (!defined $hash_parent) {
2136 $hash_parent = $co{'parent'};
2138 open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2139 or die_error(undef, "Open git-diff-tree failed.");
2140 my @difftree = map { chomp; $_ } <$fd>;
2141 close $fd or die_error(undef, "Reading diff-tree failed.");
2143 # non-textual hash id's can be cached
2145 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2148 my $refs = read_info_ref();
2149 my $ref = git_get_referencing($refs, $co{'id'});
2151 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2152 git_header_html(undef, $expires);
2153 git_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2154 git_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2155 print "<div class=\"page_body\">\n";
2156 my $comment = $co{'comment'};
2159 my @log = @$comment;
2160 # remove first and empty lines after that
2162 while (defined $log[0] && $log[0] eq "") {
2165 foreach my $line (@log) {
2166 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2177 print format_log_line_html($line) . "<br/>\n";
2180 foreach my $line (@difftree) {
2181 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2182 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2183 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2189 my $file = validate_input(unquote($6));
2190 if ($status eq "A") {
2191 print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
2192 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
2194 git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2195 } elsif ($status eq "D") {
2196 print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2197 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) . "(deleted)" .
2199 git_diff_print($from_id, "a/$file", undef, "/dev/null");
2200 } elsif ($status eq "M") {
2201 if ($from_id ne $to_id) {
2202 print "<div class=\"diff_info\">" .
2203 file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) .
2205 file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
2207 git_diff_print($from_id, "a/$file", $to_id, "b/$file");
2216 sub git_commitdiff_plain {
2217 mkdir($git_temp, 0700);
2218 open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2219 or die_error(undef, "Open git-diff-tree failed.");
2220 my @difftree = map { chomp; $_ } <$fd>;
2221 close $fd or die_error(undef, "Reading diff-tree failed.");
2223 # try to figure out the next tag after this commit
2225 my $refs = read_info_ref("tags");
2226 open $fd, "-|", $GIT, "rev-list", "HEAD";
2227 my @commits = map { chomp; $_ } <$fd>;
2229 foreach my $commit (@commits) {
2230 if (defined $refs->{$commit}) {
2231 $tagname = $refs->{$commit}
2233 if ($commit eq $hash) {
2238 print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
2239 my %co = git_read_commit($hash);
2240 my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
2241 my $comment = $co{'comment'};
2242 print "From: $co{'author'}\n" .
2243 "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
2244 "Subject: $co{'title'}\n";
2245 if (defined $tagname) {
2246 print "X-Git-Tag: $tagname\n";
2248 print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2251 foreach my $line (@$comment) {;
2256 foreach my $line (@difftree) {
2257 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2262 if ($status eq "A") {
2263 git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2264 } elsif ($status eq "D") {
2265 git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2266 } elsif ($status eq "M") {
2267 git_diff_print($from_id, "a/$file", $to_id, "b/$file", "plain");
2273 if (!defined $hash_base) {
2274 $hash_base = git_read_head($project);
2277 my %co = git_read_commit($hash_base);
2279 die_error(undef, "Unknown commit object.");
2281 my $refs = read_info_ref();
2283 git_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2284 git_header_div('commit', esc_html($co{'title'}), $hash_base);
2285 if (!defined $hash && defined $file_name) {
2286 $hash = git_get_hash_by_path($hash_base, $file_name);
2288 if (defined $hash) {
2289 $ftype = git_get_type($hash);
2291 git_print_page_path($file_name, $ftype);
2294 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2295 print "<table cellspacing=\"0\">\n";
2297 while (my $line = <$fd>) {
2298 if ($line =~ m/^([0-9a-fA-F]{40})/){
2300 my %co = git_read_commit($commit);
2304 my $ref = git_get_referencing($refs, $commit);
2306 print "<tr class=\"dark\">\n";
2308 print "<tr class=\"light\">\n";
2311 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2312 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2313 "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
2314 esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
2315 "<td class=\"link\">" .
2316 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2317 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2318 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$ftype;hb=$commit;f=$file_name")}, $ftype);
2319 my $blob = git_get_hash_by_path($hash_base, $file_name);
2320 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2321 if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
2323 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
2336 if (!defined $searchtext) {
2337 die_error("", "Text field empty.");
2339 if (!defined $hash) {
2340 $hash = git_read_head($project);
2342 my %co = git_read_commit($hash);
2344 die_error(undef, "Unknown commit object.");
2346 # pickaxe may take all resources of your box and run for several minutes
2347 # with every query - so decide by yourself how public you make this feature :)
2348 my $commit_search = 1;
2349 my $author_search = 0;
2350 my $committer_search = 0;
2351 my $pickaxe_search = 0;
2352 if ($searchtext =~ s/^author\\://i) {
2354 } elsif ($searchtext =~ s/^committer\\://i) {
2355 $committer_search = 1;
2356 } elsif ($searchtext =~ s/^pickaxe\\://i) {
2358 $pickaxe_search = 1;
2361 git_page_nav('','', $hash,$co{'tree'},$hash);
2362 git_header_div('commit', esc_html($co{'title'}), $hash);
2364 print "<table cellspacing=\"0\">\n";
2366 if ($commit_search) {
2368 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2369 while (my $commit_text = <$fd>) {
2370 if (!grep m/$searchtext/i, $commit_text) {
2373 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2376 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2379 my @commit_lines = split "\n", $commit_text;
2380 my %co = git_read_commit(undef, \@commit_lines);
2385 print "<tr class=\"dark\">\n";
2387 print "<tr class=\"light\">\n";
2390 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2391 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2393 $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/>");
2394 my $comment = $co{'comment'};
2395 foreach my $line (@$comment) {
2396 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2397 my $lead = esc_html($1) || "";
2398 $lead = chop_str($lead, 30, 10);
2399 my $match = esc_html($2) || "";
2400 my $trail = esc_html($3) || "";
2401 $trail = chop_str($trail, 30, 10);
2402 my $text = "$lead<span class=\"match\">$match</span>$trail";
2403 print chop_str($text, 80, 5) . "<br/>\n";
2407 "<td class=\"link\">" .
2408 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2409 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2416 if ($pickaxe_search) {
2418 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2421 while (my $line = <$fd>) {
2422 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2425 $set{'from_id'} = $3;
2427 $set{'id'} = $set{'to_id'};
2428 if ($set{'id'} =~ m/0{40}/) {
2429 $set{'id'} = $set{'from_id'};
2431 if ($set{'id'} =~ m/0{40}/) {
2435 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2438 print "<tr class=\"dark\">\n";
2440 print "<tr class=\"light\">\n";
2443 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2444 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2446 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
2447 esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2448 while (my $setref = shift @files) {
2450 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
2451 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2455 "<td class=\"link\">" .
2456 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2457 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2461 %co = git_read_commit($1);
2471 my $head = git_read_head($project);
2472 if (!defined $hash) {
2475 if (!defined $page) {
2478 my $refs = read_info_ref();
2480 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2481 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2482 or die_error(undef, "Open git-rev-list failed.");
2483 my @revlist = map { chomp; $_ } <$fd>;
2486 my $paging_nav = git_get_paging_nav('shortlog', $hash, $head, $page, $#revlist);
2488 if ($#revlist >= (100 * ($page+1)-1)) {
2490 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)),
2491 -title => "Alt-n"}, "next");
2496 git_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
2497 git_header_div('summary', $project);
2499 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
2504 ## ......................................................................
2505 ## feeds (RSS, OPML)
2508 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
2509 open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_read_head($project)
2510 or die_error(undef, "Open git-rev-list failed.");
2511 my @revlist = map { chomp; $_ } <$fd>;
2512 close $fd or die_error(undef, "Reading rev-list failed.");
2513 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2514 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2515 "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
2516 print "<channel>\n";
2517 print "<title>$project</title>\n".
2518 "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
2519 "<description>$project log</description>\n".
2520 "<language>en</language>\n";
2522 for (my $i = 0; $i <= $#revlist; $i++) {
2523 my $commit = $revlist[$i];
2524 my %co = git_read_commit($commit);
2525 # we read 150, we always show 30 and the ones more recent than 48 hours
2526 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
2529 my %cd = date_str($co{'committer_epoch'});
2530 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
2531 my @difftree = map { chomp; $_ } <$fd>;
2535 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
2537 "<author>" . esc_html($co{'author'}) . "</author>\n" .
2538 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
2539 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
2540 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
2541 "<description>" . esc_html($co{'title'}) . "</description>\n" .
2542 "<content:encoded>" .
2544 my $comment = $co{'comment'};
2545 foreach my $line (@$comment) {
2546 $line = decode("utf8", $line, Encode::FB_DEFAULT);
2547 print "$line<br/>\n";
2550 foreach my $line (@difftree) {
2551 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2554 my $file = validate_input(unquote($7));
2555 $file = decode("utf8", $file, Encode::FB_DEFAULT);
2556 print "$file<br/>\n";
2559 "</content:encoded>\n" .
2562 print "</channel></rss>";
2566 my @list = git_read_projects();
2568 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2569 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2570 "<opml version=\"1.0\">\n".
2572 " <title>$site_name Git OPML Export</title>\n".
2575 "<outline text=\"git RSS feeds\">\n";
2577 foreach my $pr (@list) {
2579 my $head = git_read_head($proj{'path'});
2580 if (!defined $head) {
2583 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
2584 my %co = git_read_commit($head);
2589 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
2590 my $rss = "$my_url?p=$proj{'path'};a=rss";
2591 my $html = "$my_url?p=$proj{'path'};a=summary";
2592 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
2594 print "</outline>\n".