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);
25 # core git executable to use
26 # this can just be "git" if your webserver has a sensible PATH
27 our $GIT = "++GIT_BINDIR++/git";
29 # absolute fs-path which will be prepended to the project path
30 #our $projectroot = "/pub/scm";
31 our $projectroot = "++GITWEB_PROJECTROOT++";
33 # location for temporary files needed for diffs
34 our $git_temp = "/tmp/gitweb";
36 # target of the home link on top of all pages
37 our $home_link = $my_uri;
39 # name of your site or organization to appear in page titles
40 # replace this with something more descriptive for clearer bookmarks
41 our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
43 # html text to include at home page
44 our $home_text = "++GITWEB_HOMETEXT++";
46 # URI of default stylesheet
47 our $stylesheet = "++GITWEB_CSS++";
49 our $logo = "++GITWEB_LOGO++";
51 # source of projects list
52 our $projects_list = "++GITWEB_LIST++";
54 # default blob_plain mimetype and default charset for text/plain blob
55 our $default_blob_plain_mimetype = 'text/plain';
56 our $default_text_plain_charset = undef;
58 # file to use for guessing MIME types before trying /etc/mime.types
59 # (relative to the current git repository)
60 our $mimetypes_file = undef;
62 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
63 require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
65 # version of the core git binary
66 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
68 $projects_list ||= $projectroot;
70 mkdir($git_temp, 0700) || die_error(undef, "Couldn't mkdir $git_temp");
73 # ======================================================================
74 # input validation and dispatch
75 our $action = $cgi->param('a');
76 if (defined $action) {
77 if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
78 die_error(undef, "Invalid action parameter");
80 # action which does not check rest of parameters
81 if ($action eq "opml") {
87 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
88 $project =~ s|^/||; $project =~ s|/$||;
89 if (defined $project && $project) {
90 if (!validate_input($project)) {
91 die_error(undef, "Invalid project parameter");
93 if (!(-d "$projectroot/$project")) {
94 die_error(undef, "No such directory");
96 if (!(-e "$projectroot/$project/HEAD")) {
97 die_error(undef, "No such project");
99 $ENV{'GIT_DIR'} = "$projectroot/$project";
105 our $file_name = $cgi->param('f');
106 if (defined $file_name) {
107 if (!validate_input($file_name)) {
108 die_error(undef, "Invalid file parameter");
112 our $hash = $cgi->param('h');
114 if (!validate_input($hash)) {
115 die_error(undef, "Invalid hash parameter");
119 our $hash_parent = $cgi->param('hp');
120 if (defined $hash_parent) {
121 if (!validate_input($hash_parent)) {
122 die_error(undef, "Invalid hash parent parameter");
126 our $hash_base = $cgi->param('hb');
127 if (defined $hash_base) {
128 if (!validate_input($hash_base)) {
129 die_error(undef, "Invalid hash base parameter");
133 our $page = $cgi->param('pg');
135 if ($page =~ m/[^0-9]$/) {
136 die_error(undef, "Invalid page parameter");
140 our $searchtext = $cgi->param('s');
141 if (defined $searchtext) {
142 if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
143 die_error(undef, "Invalid search parameter");
145 $searchtext = quotemeta $searchtext;
150 "blame" => \&git_blame2,
151 "blobdiff" => \&git_blobdiff,
152 "blobdiff_plain" => \&git_blobdiff_plain,
153 "blob" => \&git_blob,
154 "blob_plain" => \&git_blob_plain,
155 "commitdiff" => \&git_commitdiff,
156 "commitdiff_plain" => \&git_commitdiff_plain,
157 "commit" => \&git_commit,
158 "heads" => \&git_heads,
159 "history" => \&git_history,
162 "search" => \&git_search,
163 "shortlog" => \&git_shortlog,
164 "summary" => \&git_summary,
166 "tags" => \&git_tags,
167 "tree" => \&git_tree,
170 $action = 'summary' if (!defined($action));
171 if (!defined($actions{$action})) {
172 die_error(undef, "Unknown action");
174 $actions{$action}->();
177 ## ======================================================================
178 ## validation, quoting/unquoting and escaping
183 if ($input =~ m/^[0-9a-fA-F]{40}$/) {
186 if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
189 if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
195 # quote unsafe chars, but keep the slash, even when it's not
196 # correct, but quoted slashes look too horrible in bookmarks
199 $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
205 # replace invalid utf8 character with SUBSTITUTION sequence
208 $str = decode("utf8", $str, Encode::FB_DEFAULT);
209 $str = escapeHTML($str);
210 $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
214 # git may return quoted and escaped filenames
217 if ($str =~ m/^"(.*)"$/) {
219 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
224 ## ----------------------------------------------------------------------
225 ## HTML aware string manipulation
230 my $add_len = shift || 10;
232 # allow only $len chars, but don't cut a word if it would fit in $add_len
233 # if it doesn't fit, cut it if it's still longer than the dots we would add
234 $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
237 if (length($tail) > 4) {
239 $body =~ s/&[^;]*$//; # remove chopped character entities
244 ## ----------------------------------------------------------------------
245 ## functions returning short strings
247 # CSS class for given age value (in seconds)
251 if ($age < 60*60*2) {
253 } elsif ($age < 60*60*24*2) {
260 # convert age in seconds to "nn units ago" string
265 if ($age > 60*60*24*365*2) {
266 $age_str = (int $age/60/60/24/365);
267 $age_str .= " years ago";
268 } elsif ($age > 60*60*24*(365/12)*2) {
269 $age_str = int $age/60/60/24/(365/12);
270 $age_str .= " months ago";
271 } elsif ($age > 60*60*24*7*2) {
272 $age_str = int $age/60/60/24/7;
273 $age_str .= " weeks ago";
274 } elsif ($age > 60*60*24*2) {
275 $age_str = int $age/60/60/24;
276 $age_str .= " days ago";
277 } elsif ($age > 60*60*2) {
278 $age_str = int $age/60/60;
279 $age_str .= " hours ago";
280 } elsif ($age > 60*2) {
281 $age_str = int $age/60;
282 $age_str .= " min ago";
285 $age_str .= " sec ago";
287 $age_str .= " right now";
292 # convert file mode in octal to symbolic file mode string
294 my $mode = oct shift;
296 if (S_ISDIR($mode & S_IFMT)) {
298 } elsif (S_ISLNK($mode)) {
300 } elsif (S_ISREG($mode)) {
301 # git cares only about the executable bit
302 if ($mode & S_IXUSR) {
312 # convert file mode in octal to file type string
314 my $mode = oct shift;
316 if (S_ISDIR($mode & S_IFMT)) {
318 } elsif (S_ISLNK($mode)) {
320 } elsif (S_ISREG($mode)) {
327 ## ----------------------------------------------------------------------
328 ## functions returning short HTML fragments, or transforming HTML fragments
329 ## which don't beling to other sections
331 # format line of commit message or tag comment
332 sub format_log_line_html {
335 $line = esc_html($line);
336 $line =~ s/ / /g;
337 if ($line =~ m/([0-9a-fA-F]{40})/) {
339 if (git_get_type($hash_text) eq "commit") {
340 my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
341 $line =~ s/$hash_text/$link/;
347 # format marker of refs pointing to given object
348 sub git_get_referencing {
349 my ($refs, $id) = @_;
351 if (defined $refs->{$id}) {
352 return ' <span class="tag">' . esc_html($refs->{$id}) . '</span>';
358 ## ----------------------------------------------------------------------
359 ## git utility subroutines, invoking git commands
361 # get HEAD ref of given project as hash
364 my $oENV = $ENV{'GIT_DIR'};
366 $ENV{'GIT_DIR'} = "$projectroot/$project";
367 if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
370 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
375 $ENV{'GIT_DIR'} = $oENV;
380 # get type of given object
384 open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
391 sub git_get_project_config {
394 return unless ($key);
395 $key =~ s/^gitweb\.//;
396 return if ($key =~ m/\W/);
398 my $val = qx($GIT repo-config --get gitweb.$key);
402 sub git_get_project_config_bool {
403 my $val = git_get_project_config (@_);
404 if ($val and $val =~ m/true|yes|on/) {
407 return; # implicit false
410 # get hash of given path at given ref
411 sub git_get_hash_by_path {
413 my $path = shift || return undef;
417 open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
418 or die_error(undef, "Open git-ls-tree failed");
420 close $fd or return undef;
422 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
423 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
427 ## ......................................................................
428 ## git utility functions, directly accessing git repository
430 # assumes that PATH is not symref
434 open my $fd, "$projectroot/$path" or return undef;
438 if ($head =~ m/^[0-9a-fA-F]{40}$/) {
443 sub git_read_description {
446 open my $fd, "$projectroot/$path/description" or return undef;
453 sub git_read_projects {
456 if (-d $projects_list) {
457 # search in directory
458 my $dir = $projects_list;
459 opendir my ($dh), $dir or return undef;
460 while (my $dir = readdir($dh)) {
461 if (-e "$projectroot/$dir/HEAD") {
469 } elsif (-f $projects_list) {
470 # read from file(url-encoded):
471 # 'git%2Fgit.git Linus+Torvalds'
472 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
473 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
474 open my ($fd), $projects_list or return undef;
475 while (my $line = <$fd>) {
477 my ($path, $owner) = split ' ', $line;
478 $path = unescape($path);
479 $owner = unescape($owner);
480 if (!defined $path) {
483 if (-e "$projectroot/$path/HEAD") {
486 owner => decode("utf8", $owner, Encode::FB_DEFAULT),
493 @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
498 my $type = shift || "";
500 # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11
501 # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}
502 open my $fd, "$projectroot/$project/info/refs" or return;
503 while (my $line = <$fd>) {
505 # attention: for $type == "" it saves only last path part of ref name
506 # e.g. from 'refs/heads/jn/gitweb' it would leave only 'gitweb'
507 if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
508 if (defined $refs{$1}) {
509 $refs{$1} .= " / $2";
519 ## ----------------------------------------------------------------------
520 ## parse to hash functions
524 my $tz = shift || "-0000";
527 my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
528 my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
529 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
530 $date{'hour'} = $hour;
531 $date{'minute'} = $min;
532 $date{'mday'} = $mday;
533 $date{'day'} = $days[$wday];
534 $date{'month'} = $months[$mon];
535 $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
536 $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
538 $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
539 my $local = $epoch + ((int $1 + ($2/60)) * 3600);
540 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
541 $date{'hour_local'} = $hour;
542 $date{'minute_local'} = $min;
543 $date{'tz_local'} = $tz;
552 open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
553 $tag{'id'} = $tag_id;
554 while (my $line = <$fd>) {
556 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
558 } elsif ($line =~ m/^type (.+)$/) {
560 } elsif ($line =~ m/^tag (.+)$/) {
562 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
566 } elsif ($line =~ m/--BEGIN/) {
567 push @comment, $line;
569 } elsif ($line eq "") {
573 push @comment, <$fd>;
574 $tag{'comment'} = \@comment;
576 if (!defined $tag{'name'}) {
582 sub git_read_commit {
583 my $commit_id = shift;
584 my $commit_text = shift;
589 if (defined $commit_text) {
590 @commit_lines = @$commit_text;
593 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id or return;
594 @commit_lines = split '\n', <$fd>;
599 my $header = shift @commit_lines;
600 if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
603 ($co{'id'}, my @parents) = split ' ', $header;
604 $co{'parents'} = \@parents;
605 $co{'parent'} = $parents[0];
606 while (my $line = shift @commit_lines) {
607 last if $line eq "\n";
608 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
610 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
612 $co{'author_epoch'} = $2;
613 $co{'author_tz'} = $3;
614 if ($co{'author'} =~ m/^([^<]+) </) {
615 $co{'author_name'} = $1;
617 $co{'author_name'} = $co{'author'};
619 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
620 $co{'committer'} = $1;
621 $co{'committer_epoch'} = $2;
622 $co{'committer_tz'} = $3;
623 $co{'committer_name'} = $co{'committer'};
624 $co{'committer_name'} =~ s/ <.*//;
627 if (!defined $co{'tree'}) {
631 foreach my $title (@commit_lines) {
634 $co{'title'} = chop_str($title, 80, 5);
635 # remove leading stuff of merges to make the interesting part visible
636 if (length($title) > 50) {
637 $title =~ s/^Automatic //;
638 $title =~ s/^merge (of|with) /Merge ... /i;
639 if (length($title) > 50) {
640 $title =~ s/(http|rsync):\/\///;
642 if (length($title) > 50) {
643 $title =~ s/(master|www|rsync)\.//;
645 if (length($title) > 50) {
646 $title =~ s/kernel.org:?//;
648 if (length($title) > 50) {
649 $title =~ s/\/pub\/scm//;
652 $co{'title_short'} = chop_str($title, 50, 5);
656 # remove added spaces
657 foreach my $line (@commit_lines) {
660 $co{'comment'} = \@commit_lines;
662 my $age = time - $co{'committer_epoch'};
664 $co{'age_string'} = age_string($age);
665 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
666 if ($age > 60*60*24*7*2) {
667 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
668 $co{'age_string_age'} = $co{'age_string'};
670 $co{'age_string_date'} = $co{'age_string'};
671 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
676 ## ......................................................................
677 ## parse to array of hashes functions
684 my $pfxlen = length("$projectroot/$project/$ref_dir");
685 File::Find::find(sub {
688 push @refs, substr($File::Find::name, $pfxlen + 1);
690 }, "$projectroot/$project/$ref_dir");
692 foreach my $ref_file (@refs) {
693 my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
694 my $type = git_get_type($ref_id) || next;
697 $ref_item{'type'} = $type;
698 $ref_item{'id'} = $ref_id;
699 $ref_item{'epoch'} = 0;
700 $ref_item{'age'} = "unknown";
701 if ($type eq "tag") {
702 my %tag = git_read_tag($ref_id);
703 $ref_item{'comment'} = $tag{'comment'};
704 if ($tag{'type'} eq "commit") {
705 %co = git_read_commit($tag{'object'});
706 $ref_item{'epoch'} = $co{'committer_epoch'};
707 $ref_item{'age'} = $co{'age_string'};
708 } elsif (defined($tag{'epoch'})) {
709 my $age = time - $tag{'epoch'};
710 $ref_item{'epoch'} = $tag{'epoch'};
711 $ref_item{'age'} = age_string($age);
713 $ref_item{'reftype'} = $tag{'type'};
714 $ref_item{'name'} = $tag{'name'};
715 $ref_item{'refid'} = $tag{'object'};
716 } elsif ($type eq "commit"){
717 %co = git_read_commit($ref_id);
718 $ref_item{'reftype'} = "commit";
719 $ref_item{'name'} = $ref_file;
720 $ref_item{'title'} = $co{'title'};
721 $ref_item{'refid'} = $ref_id;
722 $ref_item{'epoch'} = $co{'committer_epoch'};
723 $ref_item{'age'} = $co{'age_string'};
725 $ref_item{'reftype'} = $type;
726 $ref_item{'name'} = $ref_file;
727 $ref_item{'refid'} = $ref_id;
730 push @reflist, \%ref_item;
733 @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
737 ## ----------------------------------------------------------------------
738 ## filesystem-related functions
743 my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
744 my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
745 if (!defined $gcos) {
749 $owner =~ s/[,;].*$//;
750 return decode("utf8", $owner, Encode::FB_DEFAULT);
753 ## ......................................................................
754 ## mimetype related functions
756 sub mimetype_guess_file {
757 my $filename = shift;
759 -r $mimemap or return undef;
762 open(MIME, $mimemap) or return undef;
764 my ($mime, $exts) = split(/\t+/);
766 my @exts = split(/\s+/, $exts);
767 foreach my $ext (@exts) {
768 $mimemap{$ext} = $mime;
774 $filename =~ /\.(.*?)$/;
779 my $filename = shift;
781 $filename =~ /\./ or return undef;
783 if ($mimetypes_file) {
784 my $file = $mimetypes_file;
785 #$file =~ m#^/# or $file = "$projectroot/$path/$file";
786 $mime = mimetype_guess_file($filename, $file);
788 $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
792 sub git_blob_plain_mimetype {
794 my $filename = shift;
797 my $mime = mimetype_guess($filename);
798 $mime and return $mime;
802 return $default_blob_plain_mimetype unless $fd;
805 return 'text/plain' .
806 ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
807 } elsif (! $filename) {
808 return 'application/octet-stream';
809 } elsif ($filename =~ m/\.png$/i) {
811 } elsif ($filename =~ m/\.gif$/i) {
813 } elsif ($filename =~ m/\.jpe?g$/i) {
816 return 'application/octet-stream';
820 ## ======================================================================
821 ## functions printing HTML: header, footer, error page
823 sub git_header_html {
824 my $status = shift || "200 OK";
827 my $title = "$site_name git";
828 if (defined $project) {
829 $title .= " - $project";
830 if (defined $action) {
831 $title .= "/$action";
832 if (defined $file_name) {
833 $title .= " - $file_name";
834 if ($action eq "tree" && $file_name !~ m|/$|) {
841 # require explicit support from the UA if we are to send the page as
842 # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
843 # we have to do this because MSIE sometimes globs '*/*', pretending to
844 # support xhtml+xml but choking when it gets what it asked for.
845 if ($cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) {
846 $content_type = 'application/xhtml+xml';
848 $content_type = 'text/html';
850 print $cgi->header(-type=>$content_type, -charset => 'utf-8', -status=> $status, -expires => $expires);
852 <?xml version="1.0" encoding="utf-8"?>
853 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
854 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
855 <!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
856 <!-- git core binaries version $git_version -->
858 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
859 <meta name="robots" content="index, nofollow"/>
860 <title>$title</title>
861 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
863 print "<link rel=\"alternate\" title=\"" . esc_param($project) . " log\" href=\"" .
864 "$my_uri?" . esc_param("p=$project;a=rss") . "\" type=\"application/rss+xml\"/>\n" .
868 "<div class=\"page_header\">\n" .
869 "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
870 "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
872 print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
873 if (defined $project) {
874 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
875 if (defined $action) {
879 if (!defined $searchtext) {
883 if (defined $hash_base) {
884 $search_hash = $hash_base;
885 } elsif (defined $hash) {
886 $search_hash = $hash;
888 $search_hash = "HEAD";
890 $cgi->param("a", "search");
891 $cgi->param("h", $search_hash);
892 print $cgi->startform(-method => "get", -action => $my_uri) .
893 "<div class=\"search\">\n" .
894 $cgi->hidden(-name => "p") . "\n" .
895 $cgi->hidden(-name => "a") . "\n" .
896 $cgi->hidden(-name => "h") . "\n" .
897 $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
899 $cgi->end_form() . "\n";
904 sub git_footer_html {
905 print "<div class=\"page_footer\">\n";
906 if (defined $project) {
907 my $descr = git_read_description($project);
908 if (defined $descr) {
909 print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
911 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
913 print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
921 my $status = shift || "403 Forbidden";
922 my $error = shift || "Malformed query, file missing or permission denied";
924 git_header_html($status);
925 print "<div class=\"page_body\">\n" .
927 "$status - $error\n" .
934 ## ----------------------------------------------------------------------
935 ## functions printing or outputting HTML: navigation
938 my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
939 $extra = '' if !defined $extra; # pager or formats
941 my @navs = qw(summary shortlog log commit commitdiff tree);
943 @navs = grep { $_ ne $suppress } @navs;
946 my %arg = map { $_, ''} @navs;
948 for (qw(commit commitdiff)) {
949 $arg{$_} = ";h=$head";
951 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
952 for (qw(shortlog log)) {
953 $arg{$_} = ";h=$head";
957 $arg{tree} .= ";h=$treehead" if defined $treehead;
958 $arg{tree} .= ";hb=$treebase" if defined $treebase;
960 print "<div class=\"page_nav\">\n" .
964 : $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$_$arg{$_}")}, "$_")
967 print "<br/>\n$extra<br/>\n" .
971 sub git_get_paging_nav {
972 my ($action, $hash, $head, $page, $nrevs) = @_;
976 if ($hash ne $head || $page) {
977 $paging_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action")}, "HEAD");
979 $paging_nav .= "HEAD";
983 $paging_nav .= " ⋅ " .
984 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page-1)),
985 -accesskey => "p", -title => "Alt-p"}, "prev");
987 $paging_nav .= " ⋅ prev";
990 if ($nrevs >= (100 * ($page+1)-1)) {
991 $paging_nav .= " ⋅ " .
992 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page+1)),
993 -accesskey => "n", -title => "Alt-n"}, "next");
995 $paging_nav .= " ⋅ next";
1001 ## ......................................................................
1002 ## functions printing or outputting HTML: div
1004 sub git_header_div {
1005 my ($action, $title, $hash, $hash_base) = @_;
1008 $rest .= ";h=$hash" if $hash;
1009 $rest .= ";hb=$hash_base" if $hash_base;
1011 print "<div class=\"header\">\n" .
1012 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action$rest"),
1013 -class => "title"}, $title ? $title : $action) . "\n" .
1017 sub git_print_page_path {
1021 if (!defined $name) {
1022 print "<div class=\"page_path\"><b>/</b></div>\n";
1023 } elsif (defined $type && $type eq 'blob') {
1024 print "<div class=\"page_path\"><b>" .
1025 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "</b><br/></div>\n";
1027 print "<div class=\"page_path\"><b>" . esc_html($name) . "</b><br/></div>\n";
1031 ## ......................................................................
1032 ## functions printing large fragments of HTML
1034 sub git_shortlog_body {
1035 # uses global variable $project
1036 my ($revlist, $from, $to, $refs, $extra) = @_;
1037 $from = 0 unless defined $from;
1038 $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1040 print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1042 for (my $i = $from; $i <= $to; $i++) {
1043 my $commit = $revlist->[$i];
1044 #my $ref = defined $refs ? git_get_referencing($refs, $commit) : '';
1045 my $ref = git_get_referencing($refs, $commit);
1046 my %co = git_read_commit($commit);
1047 my %ad = date_str($co{'author_epoch'});
1049 print "<tr class=\"dark\">\n";
1051 print "<tr class=\"light\">\n";
1054 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1055 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1056 "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1058 if (length($co{'title_short'}) < length($co{'title'})) {
1059 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"),
1060 -class => "list", -title => "$co{'title'}"},
1061 "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
1063 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"),
1065 "<b>" . esc_html($co{'title'}) . "$ref</b>");
1068 "<td class=\"link\">" .
1069 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") . " | " .
1070 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1074 if (defined $extra) {
1076 "<td colspan=\"4\">$extra</td>\n" .
1083 # uses global variable $project
1084 my ($taglist, $from, $to, $extra) = @_;
1085 $from = 0 unless defined $from;
1086 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1088 print "<table class=\"tags\" cellspacing=\"0\">\n";
1090 for (my $i = $from; $i <= $to; $i++) {
1091 my $entry = $taglist->[$i];
1093 my $comment_lines = $tag{'comment'};
1094 my $comment = shift @$comment_lines;
1096 if (defined $comment) {
1097 $comment_short = chop_str($comment, 30, 5);
1100 print "<tr class=\"dark\">\n";
1102 print "<tr class=\"light\">\n";
1105 print "<td><i>$tag{'age'}</i></td>\n" .
1107 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"),
1108 -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1111 if (defined $comment) {
1112 if (length($comment_short) < length($comment)) {
1113 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
1114 -class => "list", -title => $comment}, $comment_short);
1116 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}"),
1117 -class => "list"}, $comment);
1121 "<td class=\"selflink\">";
1122 if ($tag{'type'} eq "tag") {
1123 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag");
1128 "<td class=\"link\">" . " | " .
1129 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1130 if ($tag{'reftype'} eq "commit") {
1131 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1132 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1133 } elsif ($tag{'reftype'} eq "blob") {
1134 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$tag{'refid'}")}, "raw");
1139 if (defined $extra) {
1141 "<td colspan=\"5\">$extra</td>\n" .
1147 sub git_heads_body {
1148 # uses global variable $project
1149 my ($taglist, $head, $from, $to, $extra) = @_;
1150 $from = 0 unless defined $from;
1151 $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1153 print "<table class=\"heads\" cellspacing=\"0\">\n";
1155 for (my $i = $from; $i <= $to; $i++) {
1156 my $entry = $taglist->[$i];
1158 my $curr = $tag{'id'} eq $head;
1160 print "<tr class=\"dark\">\n";
1162 print "<tr class=\"light\">\n";
1165 print "<td><i>$tag{'age'}</i></td>\n" .
1166 ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1167 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"),
1168 -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1170 "<td class=\"link\">" .
1171 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") . " | " .
1172 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1176 if (defined $extra) {
1178 "<td colspan=\"3\">$extra</td>\n" .
1184 ## ----------------------------------------------------------------------
1185 ## functions printing large fragments, format as one of arguments
1187 sub git_diff_print {
1189 my $from_name = shift;
1191 my $to_name = shift;
1192 my $format = shift || "html";
1194 my $from_tmp = "/dev/null";
1195 my $to_tmp = "/dev/null";
1198 # create tmp from-file
1199 if (defined $from) {
1200 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1201 open my $fd2, "> $from_tmp";
1202 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1209 # create tmp to-file
1211 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1212 open my $fd2, "> $to_tmp";
1213 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1220 open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1221 if ($format eq "plain") {
1226 while (my $line = <$fd>) {
1228 my $char = substr($line, 0, 1);
1229 my $diff_class = "";
1231 $diff_class = " add";
1232 } elsif ($char eq "-") {
1233 $diff_class = " rem";
1234 } elsif ($char eq "@") {
1235 $diff_class = " chunk_header";
1236 } elsif ($char eq "\\") {
1240 while ((my $pos = index($line, "\t")) != -1) {
1241 if (my $count = (8 - (($pos-1) % 8))) {
1242 my $spaces = ' ' x $count;
1243 $line =~ s/\t/$spaces/;
1246 print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1251 if (defined $from) {
1260 ## ======================================================================
1261 ## ======================================================================
1264 sub git_project_list {
1265 my $order = $cgi->param('o');
1266 if (defined $order && $order !~ m/project|descr|owner|age/) {
1267 die_error(undef, "Unknown order parameter");
1270 my @list = git_read_projects();
1273 die_error(undef, "No projects found");
1275 foreach my $pr (@list) {
1276 my $head = git_read_head($pr->{'path'});
1277 if (!defined $head) {
1280 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1281 my %co = git_read_commit($head);
1285 $pr->{'commit'} = \%co;
1286 if (!defined $pr->{'descr'}) {
1287 my $descr = git_read_description($pr->{'path'}) || "";
1288 $pr->{'descr'} = chop_str($descr, 25, 5);
1290 if (!defined $pr->{'owner'}) {
1291 $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1293 push @projects, $pr;
1297 if (-f $home_text) {
1298 print "<div class=\"index_include\">\n";
1299 open (my $fd, $home_text);
1304 print "<table class=\"project_list\">\n" .
1306 $order ||= "project";
1307 if ($order eq "project") {
1308 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1309 print "<th>Project</th>\n";
1312 $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
1313 -class => "header"}, "Project") .
1316 if ($order eq "descr") {
1317 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1318 print "<th>Description</th>\n";
1321 $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
1322 -class => "header"}, "Description") .
1325 if ($order eq "owner") {
1326 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1327 print "<th>Owner</th>\n";
1330 $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
1331 -class => "header"}, "Owner") .
1334 if ($order eq "age") {
1335 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
1336 print "<th>Last Change</th>\n";
1339 $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
1340 -class => "header"}, "Last Change") .
1343 print "<th></th>\n" .
1346 foreach my $pr (@projects) {
1348 print "<tr class=\"dark\">\n";
1350 print "<tr class=\"light\">\n";
1353 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"),
1354 -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
1355 "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
1356 "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
1357 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
1358 $pr->{'commit'}{'age_string'} . "</td>\n" .
1359 "<td class=\"link\">" .
1360 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") . " | " .
1361 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") . " | " .
1362 $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
1371 my $descr = git_read_description($project) || "none";
1372 my $head = git_read_head($project);
1373 my %co = git_read_commit($head);
1374 my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1377 if (-f $projects_list) {
1378 open (my $fd , $projects_list);
1379 while (my $line = <$fd>) {
1381 my ($pr, $ow) = split ' ', $line;
1382 $pr = unescape($pr);
1383 $ow = unescape($ow);
1384 if ($pr eq $project) {
1385 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
1391 if (!defined $owner) {
1392 $owner = get_file_owner("$projectroot/$project");
1395 my $refs = read_info_ref();
1397 git_page_nav('summary','', $head);
1399 print "<div class=\"title\"> </div>\n";
1400 print "<table cellspacing=\"0\">\n" .
1401 "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1402 "<tr><td>owner</td><td>$owner</td></tr>\n" .
1403 "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
1406 open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_read_head($project)
1407 or die_error(undef, "Open git-rev-list failed");
1408 my @revlist = map { chomp; $_ } <$fd>;
1410 git_header_div('shortlog');
1411 git_shortlog_body(\@revlist, 0, 15, $refs,
1412 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "..."));
1414 my $taglist = git_read_refs("refs/tags");
1415 if (defined @$taglist) {
1416 git_header_div('tags');
1417 git_tags_body($taglist, 0, 15,
1418 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "..."));
1421 my $headlist = git_read_refs("refs/heads");
1422 if (defined @$headlist) {
1423 git_header_div('heads');
1424 git_heads_body($headlist, $head, 0, 15,
1425 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "..."));
1432 my $head = git_read_head($project);
1434 git_page_nav('','', $head,undef,$head);
1435 my %tag = git_read_tag($hash);
1436 git_header_div('commit', esc_html($tag{'name'}), $hash);
1437 print "<div class=\"title_text\">\n" .
1438 "<table cellspacing=\"0\">\n" .
1440 "<td>object</td>\n" .
1441 "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
1442 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
1444 if (defined($tag{'author'})) {
1445 my %ad = date_str($tag{'epoch'}, $tag{'tz'});
1446 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1447 print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1449 print "</table>\n\n" .
1451 print "<div class=\"page_body\">";
1452 my $comment = $tag{'comment'};
1453 foreach my $line (@$comment) {
1454 print esc_html($line) . "<br/>\n";
1463 die_error(undef, "Permission denied") if (!git_get_project_config_bool ('blame'));
1464 die_error('404 Not Found', "File name not defined") if (!$file_name);
1465 $hash_base ||= git_read_head($project);
1466 die_error(undef, "Couldn't find base commit") unless ($hash_base);
1467 my %co = git_read_commit($hash_base)
1468 or die_error(undef, "Reading commit failed");
1469 if (!defined $hash) {
1470 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1471 or die_error(undef, "Error looking up file");
1473 $ftype = git_get_type($hash);
1474 if ($ftype !~ "blob") {
1475 die_error("400 Bad Request", "Object is not a blob");
1477 open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
1478 or die_error(undef, "Open git-blame failed");
1481 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1482 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1483 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1484 git_header_div('commit', esc_html($co{'title'}), $hash_base);
1485 git_print_page_path($file_name, $ftype);
1486 my @rev_color = (qw(light dark));
1487 my $num_colors = scalar(@rev_color);
1488 my $current_color = 0;
1490 print "<div class=\"page_body\">\n";
1491 print "<table class=\"blame\">\n";
1492 print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
1494 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
1496 my $rev = substr($full_rev, 0, 8);
1500 if (!defined $last_rev) {
1501 $last_rev = $full_rev;
1502 } elsif ($last_rev ne $full_rev) {
1503 $last_rev = $full_rev;
1504 $current_color = ++$current_color % $num_colors;
1506 print "<tr class=\"$rev_color[$current_color]\">\n";
1507 print "<td class=\"sha1\">" .
1508 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$full_rev;f=$file_name")}, esc_html($rev)) . "</td>\n";
1509 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" . esc_html($lineno) . "</a></td>\n";
1510 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
1515 close $fd or print "Reading blob failed\n";
1521 die_error('403 Permission denied', "Permission denied") if (!git_get_project_config_bool ('blame'));
1522 die_error('404 Not Found', "File name not defined") if (!$file_name);
1523 $hash_base ||= git_read_head($project);
1524 die_error(undef, "Couldn't find base commit") unless ($hash_base);
1525 my %co = git_read_commit($hash_base)
1526 or die_error(undef, "Reading commit failed");
1527 if (!defined $hash) {
1528 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1529 or die_error(undef, "Error lookup file");
1531 open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
1532 or die_error(undef, "Open git-annotate failed");
1535 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1536 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1537 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1538 git_header_div('commit', esc_html($co{'title'}), $hash_base);
1539 git_print_page_path($file_name, 'blob');
1540 print "<div class=\"page_body\">\n";
1542 <table class="blame">
1551 my @line_class = (qw(light dark));
1552 my $line_class_len = scalar (@line_class);
1553 my $line_class_num = $#line_class;
1554 while (my $line = <$fd>) {
1566 $line_class_num = ($line_class_num + 1) % $line_class_len;
1568 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
1575 print qq( <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
1578 $short_rev = substr ($long_rev, 0, 8);
1579 $age = time () - $time;
1580 $age_str = age_string ($age);
1581 $age_str =~ s/ / /g;
1582 $age_class = age_class($age);
1583 $author = esc_html ($author);
1584 $author =~ s/ / /g;
1586 while ((my $pos = index($data, "\t")) != -1) {
1587 if (my $count = (8 - ($pos % 8))) {
1588 my $spaces = ' ' x $count;
1589 $data =~ s/\t/$spaces/;
1592 $data = esc_html ($data);
1595 <tr class="$line_class[$line_class_num]">
1596 <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1597 <td class="$age_class">$age_str</td>
1599 <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1600 <td class="pre">$data</td>
1603 } # while (my $line = <$fd>)
1604 print "</table>\n\n";
1605 close $fd or print "Reading blob failed.\n";
1611 my $head = git_read_head($project);
1613 git_page_nav('','', $head,undef,$head);
1614 git_header_div('summary', $project);
1616 my $taglist = git_read_refs("refs/tags");
1617 if (defined @$taglist) {
1618 git_tags_body($taglist);
1624 my $head = git_read_head($project);
1626 git_page_nav('','', $head,undef,$head);
1627 git_header_div('summary', $project);
1629 my $taglist = git_read_refs("refs/heads");
1631 if (defined @$taglist) {
1632 git_heads_body($taglist, $head);
1637 sub git_blob_plain {
1638 if (!defined $hash) {
1639 if (defined $file_name) {
1640 my $base = $hash_base || git_read_head($project);
1641 $hash = git_get_hash_by_path($base, $file_name, "blob")
1642 or die_error(undef, "Error lookup file");
1644 die_error(undef, "No file name defined");
1648 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1649 or die_error(undef, "Couldn't cat $file_name, $hash");
1651 $type ||= git_blob_plain_mimetype($fd, $file_name);
1653 # save as filename, even when no $file_name is given
1654 my $save_as = "$hash";
1655 if (defined $file_name) {
1656 $save_as = $file_name;
1657 } elsif ($type =~ m/^text\//) {
1661 print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
1663 binmode STDOUT, ':raw';
1665 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
1671 if (!defined $hash) {
1672 if (defined $file_name) {
1673 my $base = $hash_base || git_read_head($project);
1674 $hash = git_get_hash_by_path($base, $file_name, "blob")
1675 or die_error(undef, "Error lookup file");
1677 die_error(undef, "No file name defined");
1680 my $have_blame = git_get_project_config_bool ('blame');
1681 open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1682 or die_error(undef, "Couldn't cat $file_name, $hash");
1683 my $mimetype = git_blob_plain_mimetype($fd, $file_name);
1684 if ($mimetype !~ m/^text\//) {
1686 return git_blob_plain($mimetype);
1689 my $formats_nav = '';
1690 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1691 if (defined $file_name) {
1693 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
1696 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1697 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head");
1699 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain");
1701 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1702 git_header_div('commit', esc_html($co{'title'}), $hash_base);
1704 print "<div class=\"page_nav\">\n" .
1705 "<br/><br/></div>\n" .
1706 "<div class=\"title\">$hash</div>\n";
1708 git_print_page_path($file_name, "blob");
1709 print "<div class=\"page_body\">\n";
1711 while (my $line = <$fd>) {
1714 while ((my $pos = index($line, "\t")) != -1) {
1715 if (my $count = (8 - ($pos % 8))) {
1716 my $spaces = ' ' x $count;
1717 $line =~ s/\t/$spaces/;
1720 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1722 close $fd or print "Reading blob failed.\n";
1728 if (!defined $hash) {
1729 $hash = git_read_head($project);
1730 if (defined $file_name) {
1731 my $base = $hash_base || $hash;
1732 $hash = git_get_hash_by_path($base, $file_name, "tree");
1734 if (!defined $hash_base) {
1739 open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
1740 or die_error(undef, "Open git-ls-tree failed");
1741 my @entries = map { chomp; $_ } <$fd>;
1742 close $fd or die_error(undef, "Reading tree failed");
1745 my $refs = read_info_ref();
1746 my $ref = git_get_referencing($refs, $hash_base);
1750 my $have_blame = git_get_project_config_bool ('blame');
1751 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1752 $base_key = ";hb=$hash_base";
1753 git_page_nav('tree','', $hash_base);
1754 git_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
1756 print "<div class=\"page_nav\">\n";
1757 print "<br/><br/></div>\n";
1758 print "<div class=\"title\">$hash</div>\n";
1760 if (defined $file_name) {
1761 $base = esc_html("$file_name/");
1763 git_print_page_path($file_name, 'tree');
1764 print "<div class=\"page_body\">\n";
1765 print "<table cellspacing=\"0\">\n";
1767 foreach my $line (@entries) {
1768 #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'
1769 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1773 my $t_name = validate_input($4);
1775 print "<tr class=\"dark\">\n";
1777 print "<tr class=\"light\">\n";
1780 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
1781 if ($t_type eq "blob") {
1782 print "<td class=\"list\">" .
1783 $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)) .
1785 "<td class=\"link\">" .
1786 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob");
1788 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame");
1790 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$t_hash;hb=$hash_base;f=$base$t_name")}, "history") .
1791 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") .
1793 } elsif ($t_type eq "tree") {
1794 print "<td class=\"list\">" .
1795 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
1797 "<td class=\"link\">" .
1798 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
1799 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") .
1804 print "</table>\n" .
1810 my $head = git_read_head($project);
1811 if (!defined $hash) {
1814 if (!defined $page) {
1817 my $refs = read_info_ref();
1819 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1820 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
1821 or die_error(undef, "Open git-rev-list failed");
1822 my @revlist = map { chomp; $_ } <$fd>;
1825 my $paging_nav = git_get_paging_nav('log', $hash, $head, $page, $#revlist);
1828 git_page_nav('log','', $hash,undef,undef, $paging_nav);
1831 my %co = git_read_commit($hash);
1833 git_header_div('summary', $project);
1834 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1836 for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1837 my $commit = $revlist[$i];
1838 my $ref = git_get_referencing($refs, $commit);
1839 my %co = git_read_commit($commit);
1841 my %ad = date_str($co{'author_epoch'});
1842 git_header_div('commit',
1843 "<span class=\"age\">$co{'age_string'}</span>" .
1844 esc_html($co{'title'}) . $ref,
1846 print "<div class=\"title_text\">\n" .
1847 "<div class=\"log_link\">\n" .
1848 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1849 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1852 "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
1854 "<div class=\"log_body\">\n";
1855 my $comment = $co{'comment'};
1857 foreach my $line (@$comment) {
1858 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1869 print format_log_line_html($line) . "<br/>\n";
1880 my %co = git_read_commit($hash);
1882 die_error(undef, "Unknown commit object");
1884 my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1885 my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1887 my $parent = $co{'parent'};
1888 if (!defined $parent) {
1891 open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
1892 or die_error(undef, "Open git-diff-tree failed");
1893 my @difftree = map { chomp; $_ } <$fd>;
1894 close $fd or die_error(undef, "Reading git-diff-tree failed");
1896 # non-textual hash id's can be cached
1898 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1901 my $refs = read_info_ref();
1902 my $ref = git_get_referencing($refs, $co{'id'});
1903 my $formats_nav = '';
1904 if (defined $file_name && defined $co{'parent'}) {
1905 my $parent = $co{'parent'};
1906 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;hb=$parent;f=$file_name")}, "blame");
1908 git_header_html(undef, $expires);
1909 git_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
1910 $hash, $co{'tree'}, $hash,
1913 if (defined $co{'parent'}) {
1914 git_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
1916 git_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
1918 print "<div class=\"title_text\">\n" .
1919 "<table cellspacing=\"0\">\n";
1920 print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
1922 "<td></td><td> $ad{'rfc2822'}";
1923 if ($ad{'hour_local'} < 6) {
1924 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1926 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1930 print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
1931 print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
1932 print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
1935 "<td class=\"sha1\">" .
1936 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
1938 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
1941 my $parents = $co{'parents'};
1942 foreach my $par (@$parents) {
1945 "<td class=\"sha1\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
1946 "<td class=\"link\">" .
1947 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
1948 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
1954 print "<div class=\"page_body\">\n";
1955 my $comment = $co{'comment'};
1958 foreach my $line (@$comment) {
1959 # print only one empty line
1961 if ($empty || $signed) {
1968 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1970 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1973 print format_log_line_html($line) . "<br/>\n";
1977 print "<div class=\"list_head\">\n";
1978 if ($#difftree > 10) {
1979 print(($#difftree + 1) . " files changed:\n");
1982 print "<table class=\"diff_tree\">\n";
1984 foreach my $line (@difftree) {
1985 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
1986 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
1987 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1995 my $similarity = $6;
1996 my $file = validate_input(unquote($7));
1998 print "<tr class=\"dark\">\n";
2000 print "<tr class=\"light\">\n";
2003 if ($status eq "A") {
2005 if (S_ISREG(oct $to_mode)) {
2006 $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
2009 $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" .
2010 "<td><span class=\"file_status new\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
2011 "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") . "</td>\n";
2012 } elsif ($status eq "D") {
2014 $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" .
2015 "<td><span class=\"file_status deleted\">[deleted " . file_type($from_mode). "]</span></td>\n" .
2016 "<td class=\"link\">" .
2017 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") .
2018 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") .
2020 } elsif ($status eq "M" || $status eq "T") {
2021 my $mode_chnge = "";
2022 if ($from_mode != $to_mode) {
2023 $mode_chnge = " <span class=\"file_status mode_chnge\">[changed";
2024 if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
2025 $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
2027 if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
2028 if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
2029 $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
2030 } elsif (S_ISREG($to_mode)) {
2031 $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
2034 $mode_chnge .= "]</span>\n";
2037 if ($to_id ne $from_id) {
2038 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));
2040 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
2043 "<td>$mode_chnge</td>\n" .
2044 "<td class=\"link\">";
2045 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob");
2046 if ($to_id ne $from_id) {
2047 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff");
2049 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") . "\n";
2051 } elsif ($status eq "R") {
2052 my ($from_file, $to_file) = split "\t", $file;
2054 if ($from_mode != $to_mode) {
2055 $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
2058 $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" .
2059 "<td><span class=\"file_status moved\">[moved from " .
2060 $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)) .
2061 " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
2062 "<td class=\"link\">" .
2063 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
2064 if ($to_id ne $from_id) {
2065 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file")}, "diff");
2076 mkdir($git_temp, 0700);
2078 if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
2080 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2081 git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2082 git_header_div('commit', esc_html($co{'title'}), $hash_base);
2084 print "<div class=\"page_nav\">\n" .
2085 "<br/><br/></div>\n" .
2086 "<div class=\"title\">$hash vs $hash_parent</div>\n";
2088 git_print_page_path($file_name, "blob");
2089 print "<div class=\"page_body\">\n" .
2090 "<div class=\"diff_info\">blob:" .
2091 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
2093 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
2095 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2100 sub git_blobdiff_plain {
2101 mkdir($git_temp, 0700);
2102 print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2103 git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2106 sub git_commitdiff {
2107 mkdir($git_temp, 0700);
2108 my %co = git_read_commit($hash);
2110 die_error(undef, "Unknown commit object");
2112 if (!defined $hash_parent) {
2113 $hash_parent = $co{'parent'};
2115 open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2116 or die_error(undef, "Open git-diff-tree failed");
2117 my @difftree = map { chomp; $_ } <$fd>;
2118 close $fd or die_error(undef, "Reading git-diff-tree failed");
2120 # non-textual hash id's can be cached
2122 if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2125 my $refs = read_info_ref();
2126 my $ref = git_get_referencing($refs, $co{'id'});
2128 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2129 git_header_html(undef, $expires);
2130 git_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2131 git_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2132 print "<div class=\"page_body\">\n";
2133 my $comment = $co{'comment'};
2136 my @log = @$comment;
2137 # remove first and empty lines after that
2139 while (defined $log[0] && $log[0] eq "") {
2142 foreach my $line (@log) {
2143 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2154 print format_log_line_html($line) . "<br/>\n";
2157 foreach my $line (@difftree) {
2158 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'
2159 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'
2160 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2166 my $file = validate_input(unquote($6));
2167 if ($status eq "A") {
2168 print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
2169 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
2171 git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2172 } elsif ($status eq "D") {
2173 print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2174 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) . "(deleted)" .
2176 git_diff_print($from_id, "a/$file", undef, "/dev/null");
2177 } elsif ($status eq "M") {
2178 if ($from_id ne $to_id) {
2179 print "<div class=\"diff_info\">" .
2180 file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) .
2182 file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
2184 git_diff_print($from_id, "a/$file", $to_id, "b/$file");
2193 sub git_commitdiff_plain {
2194 mkdir($git_temp, 0700);
2195 open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2196 or die_error(undef, "Open git-diff-tree failed");
2197 my @difftree = map { chomp; $_ } <$fd>;
2198 close $fd or die_error(undef, "Reading diff-tree failed");
2200 # try to figure out the next tag after this commit
2202 my $refs = read_info_ref("tags");
2203 open $fd, "-|", $GIT, "rev-list", "HEAD";
2204 my @commits = map { chomp; $_ } <$fd>;
2206 foreach my $commit (@commits) {
2207 if (defined $refs->{$commit}) {
2208 $tagname = $refs->{$commit}
2210 if ($commit eq $hash) {
2215 print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
2216 my %co = git_read_commit($hash);
2217 my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
2218 my $comment = $co{'comment'};
2219 print "From: $co{'author'}\n" .
2220 "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
2221 "Subject: $co{'title'}\n";
2222 if (defined $tagname) {
2223 print "X-Git-Tag: $tagname\n";
2225 print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2228 foreach my $line (@$comment) {;
2233 foreach my $line (@difftree) {
2234 $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2239 if ($status eq "A") {
2240 git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2241 } elsif ($status eq "D") {
2242 git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2243 } elsif ($status eq "M") {
2244 git_diff_print($from_id, "a/$file", $to_id, "b/$file", "plain");
2250 if (!defined $hash_base) {
2251 $hash_base = git_read_head($project);
2254 my %co = git_read_commit($hash_base);
2256 die_error(undef, "Unknown commit object");
2258 my $refs = read_info_ref();
2260 git_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2261 git_header_div('commit', esc_html($co{'title'}), $hash_base);
2262 if (!defined $hash && defined $file_name) {
2263 $hash = git_get_hash_by_path($hash_base, $file_name);
2265 if (defined $hash) {
2266 $ftype = git_get_type($hash);
2268 git_print_page_path($file_name, $ftype);
2271 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2272 print "<table cellspacing=\"0\">\n";
2274 while (my $line = <$fd>) {
2275 if ($line =~ m/^([0-9a-fA-F]{40})/){
2277 my %co = git_read_commit($commit);
2281 my $ref = git_get_referencing($refs, $commit);
2283 print "<tr class=\"dark\">\n";
2285 print "<tr class=\"light\">\n";
2288 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2289 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2290 "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
2291 esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
2292 "<td class=\"link\">" .
2293 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2294 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2295 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$ftype;hb=$commit;f=$file_name")}, $ftype);
2296 my $blob = git_get_hash_by_path($hash_base, $file_name);
2297 my $blob_parent = git_get_hash_by_path($commit, $file_name);
2298 if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
2300 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
2313 if (!defined $searchtext) {
2314 die_error(undef, "Text field empty");
2316 if (!defined $hash) {
2317 $hash = git_read_head($project);
2319 my %co = git_read_commit($hash);
2321 die_error(undef, "Unknown commit object");
2323 # pickaxe may take all resources of your box and run for several minutes
2324 # with every query - so decide by yourself how public you make this feature :)
2325 my $commit_search = 1;
2326 my $author_search = 0;
2327 my $committer_search = 0;
2328 my $pickaxe_search = 0;
2329 if ($searchtext =~ s/^author\\://i) {
2331 } elsif ($searchtext =~ s/^committer\\://i) {
2332 $committer_search = 1;
2333 } elsif ($searchtext =~ s/^pickaxe\\://i) {
2335 $pickaxe_search = 1;
2338 git_page_nav('','', $hash,$co{'tree'},$hash);
2339 git_header_div('commit', esc_html($co{'title'}), $hash);
2341 print "<table cellspacing=\"0\">\n";
2343 if ($commit_search) {
2345 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2346 while (my $commit_text = <$fd>) {
2347 if (!grep m/$searchtext/i, $commit_text) {
2350 if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2353 if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2356 my @commit_lines = split "\n", $commit_text;
2357 my %co = git_read_commit(undef, \@commit_lines);
2362 print "<tr class=\"dark\">\n";
2364 print "<tr class=\"light\">\n";
2367 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2368 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2370 $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/>");
2371 my $comment = $co{'comment'};
2372 foreach my $line (@$comment) {
2373 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2374 my $lead = esc_html($1) || "";
2375 $lead = chop_str($lead, 30, 10);
2376 my $match = esc_html($2) || "";
2377 my $trail = esc_html($3) || "";
2378 $trail = chop_str($trail, 30, 10);
2379 my $text = "$lead<span class=\"match\">$match</span>$trail";
2380 print chop_str($text, 80, 5) . "<br/>\n";
2384 "<td class=\"link\">" .
2385 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2386 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2393 if ($pickaxe_search) {
2395 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2398 while (my $line = <$fd>) {
2399 if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2402 $set{'from_id'} = $3;
2404 $set{'id'} = $set{'to_id'};
2405 if ($set{'id'} =~ m/0{40}/) {
2406 $set{'id'} = $set{'from_id'};
2408 if ($set{'id'} =~ m/0{40}/) {
2412 } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2415 print "<tr class=\"dark\">\n";
2417 print "<tr class=\"light\">\n";
2420 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2421 "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2423 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
2424 esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2425 while (my $setref = shift @files) {
2427 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
2428 "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2432 "<td class=\"link\">" .
2433 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2434 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2438 %co = git_read_commit($1);
2448 my $head = git_read_head($project);
2449 if (!defined $hash) {
2452 if (!defined $page) {
2455 my $refs = read_info_ref();
2457 my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2458 open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2459 or die_error(undef, "Open git-rev-list failed");
2460 my @revlist = map { chomp; $_ } <$fd>;
2463 my $paging_nav = git_get_paging_nav('shortlog', $hash, $head, $page, $#revlist);
2465 if ($#revlist >= (100 * ($page+1)-1)) {
2467 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)),
2468 -title => "Alt-n"}, "next");
2473 git_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
2474 git_header_div('summary', $project);
2476 git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
2481 ## ......................................................................
2482 ## feeds (RSS, OPML)
2485 # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
2486 open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_read_head($project)
2487 or die_error(undef, "Open git-rev-list failed");
2488 my @revlist = map { chomp; $_ } <$fd>;
2489 close $fd or die_error(undef, "Reading git-rev-list failed");
2490 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2491 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2492 "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
2493 print "<channel>\n";
2494 print "<title>$project</title>\n".
2495 "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
2496 "<description>$project log</description>\n".
2497 "<language>en</language>\n";
2499 for (my $i = 0; $i <= $#revlist; $i++) {
2500 my $commit = $revlist[$i];
2501 my %co = git_read_commit($commit);
2502 # we read 150, we always show 30 and the ones more recent than 48 hours
2503 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
2506 my %cd = date_str($co{'committer_epoch'});
2507 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
2508 my @difftree = map { chomp; $_ } <$fd>;
2512 sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
2514 "<author>" . esc_html($co{'author'}) . "</author>\n" .
2515 "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
2516 "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
2517 "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
2518 "<description>" . esc_html($co{'title'}) . "</description>\n" .
2519 "<content:encoded>" .
2521 my $comment = $co{'comment'};
2522 foreach my $line (@$comment) {
2523 $line = decode("utf8", $line, Encode::FB_DEFAULT);
2524 print "$line<br/>\n";
2527 foreach my $line (@difftree) {
2528 if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2531 my $file = validate_input(unquote($7));
2532 $file = decode("utf8", $file, Encode::FB_DEFAULT);
2533 print "$file<br/>\n";
2536 "</content:encoded>\n" .
2539 print "</channel></rss>";
2543 my @list = git_read_projects();
2545 print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2546 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2547 "<opml version=\"1.0\">\n".
2549 " <title>$site_name Git OPML Export</title>\n".
2552 "<outline text=\"git RSS feeds\">\n";
2554 foreach my $pr (@list) {
2556 my $head = git_read_head($proj{'path'});
2557 if (!defined $head) {
2560 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
2561 my %co = git_read_commit($head);
2566 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
2567 my $rss = "$my_url?p=$proj{'path'};a=rss";
2568 my $html = "$my_url?p=$proj{'path'};a=summary";
2569 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
2571 print "</outline>\n".