Code

gitweb: Make git_print_log generic; git_print_simplified_log uses it
[git.git] / gitweb / gitweb.perl
1 #!/usr/bin/perl
3 # gitweb - simple web interface to track changes in git repositories
4 #
5 # (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>
6 # (C) 2005, Christian Gierke
7 #
8 # This program is licensed under the GPLv2
10 use strict;
11 use warnings;
12 use CGI qw(:standard :escapeHTML -nosticky);
13 use CGI::Util qw(unescape);
14 use CGI::Carp qw(fatalsToBrowser);
15 use Encode;
16 use Fcntl ':mode';
17 use File::Find qw();
18 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 our $cgi = new CGI;
22 our $version = "++GIT_VERSION++";
23 our $my_url = $cgi->url();
24 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 # target of the home link on top of all pages
35 our $home_link = $my_uri || "/";
37 # string of the home link on top of all pages
38 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
40 # name of your site or organization to appear in page titles
41 # replace this with something more descriptive for clearer bookmarks
42 our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
44 # html text to include at home page
45 our $home_text = "++GITWEB_HOMETEXT++";
47 # URI of default stylesheet
48 our $stylesheet = "++GITWEB_CSS++";
49 # URI of GIT logo
50 our $logo = "++GITWEB_LOGO++";
52 # source of projects list
53 our $projects_list = "++GITWEB_LIST++";
55 # list of git base URLs used for URL to where fetch project from,
56 # i.e. full URL is "$git_base_url/$project"
57 our @git_base_url_list = ("++GITWEB_BASE_URL++");
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 # You define site-wide feature defaults here; override them with
68 # $GITWEB_CONFIG as necessary.
69 our %feature = (
70         # feature => {
71         #       'sub' => feature-sub (subroutine),
72         #       'override' => allow-override (boolean),
73         #       'default' => [ default options...] (array reference)}
74         #
75         # if feature is overridable (it means that allow-override has true value,
76         # then feature-sub will be called with default options as parameters;
77         # return value of feature-sub indicates if to enable specified feature
78         #
79         # use gitweb_check_feature(<feature>) to check if <feature> is enabled
81         'blame' => {
82                 'sub' => \&feature_blame,
83                 'override' => 0,
84                 'default' => [0]},
86         'snapshot' => {
87                 'sub' => \&feature_snapshot,
88                 'override' => 0,
89                 #         => [content-encoding, suffix, program]
90                 'default' => ['x-gzip', 'gz', 'gzip']},
91 );
93 sub gitweb_check_feature {
94         my ($name) = @_;
95         return undef unless exists $feature{$name};
96         my ($sub, $override, @defaults) = (
97                 $feature{$name}{'sub'},
98                 $feature{$name}{'override'},
99                 @{$feature{$name}{'default'}});
100         if (!$override) { return @defaults; }
101         return $sub->(@defaults);
104 # To enable system wide have in $GITWEB_CONFIG
105 # $feature{'blame'}{'default'} = [1];
106 # To have project specific config enable override in $GITWEB_CONFIG
107 # $feature{'blame'}{'override'} = 1;
108 # and in project config gitweb.blame = 0|1;
110 sub feature_blame {
111         my ($val) = git_get_project_config('blame', '--bool');
113         if ($val eq 'true') {
114                 return 1;
115         } elsif ($val eq 'false') {
116                 return 0;
117         }
119         return $_[0];
122 # To disable system wide have in $GITWEB_CONFIG
123 # $feature{'snapshot'}{'default'} = [undef];
124 # To have project specific config enable override in $GITWEB_CONFIG
125 # $feature{'blame'}{'override'} = 1;
126 # and in project config  gitweb.snapshot = none|gzip|bzip2
128 sub feature_snapshot {
129         my ($ctype, $suffix, $command) = @_;
131         my ($val) = git_get_project_config('snapshot');
133         if ($val eq 'gzip') {
134                 return ('x-gzip', 'gz', 'gzip');
135         } elsif ($val eq 'bzip2') {
136                 return ('x-bzip2', 'bz2', 'bzip2');
137         } elsif ($val eq 'none') {
138                 return ();
139         }
141         return ($ctype, $suffix, $command);
144 # rename detection options for git-diff and git-diff-tree
145 # - default is '-M', with the cost proportional to
146 #   (number of removed files) * (number of new files).
147 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
148 #   (number of changed files + number of removed files) * (number of new files)
149 # - even more costly is '-C', '--find-copies-harder' with cost
150 #   (number of files in the original tree) * (number of new files)
151 # - one might want to include '-B' option, e.g. '-B', '-M'
152 our @diff_opts = ('-M'); # taken from git_commit
154 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
155 require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
157 # version of the core git binary
158 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
160 # path to the current git repository
161 our $git_dir;
163 $projects_list ||= $projectroot;
165 # ======================================================================
166 # input validation and dispatch
167 our $action = $cgi->param('a');
168 if (defined $action) {
169         if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
170                 die_error(undef, "Invalid action parameter");
171         }
174 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
175 if (defined $project) {
176         $project =~ s|^/||;
177         $project =~ s|/$||;
178         $project = undef unless $project;
180 if (defined $project) {
181         if (!validate_input($project)) {
182                 die_error(undef, "Invalid project parameter");
183         }
184         if (!(-d "$projectroot/$project")) {
185                 die_error(undef, "No such directory");
186         }
187         if (!(-e "$projectroot/$project/HEAD")) {
188                 die_error(undef, "No such project");
189         }
190         $git_dir = "$projectroot/$project";
193 our $file_name = $cgi->param('f');
194 if (defined $file_name) {
195         if (!validate_input($file_name)) {
196                 die_error(undef, "Invalid file parameter");
197         }
200 our $file_parent = $cgi->param('fp');
201 if (defined $file_parent) {
202         if (!validate_input($file_parent)) {
203                 die_error(undef, "Invalid file parent parameter");
204         }
207 our $hash = $cgi->param('h');
208 if (defined $hash) {
209         if (!validate_input($hash)) {
210                 die_error(undef, "Invalid hash parameter");
211         }
214 our $hash_parent = $cgi->param('hp');
215 if (defined $hash_parent) {
216         if (!validate_input($hash_parent)) {
217                 die_error(undef, "Invalid hash parent parameter");
218         }
221 our $hash_base = $cgi->param('hb');
222 if (defined $hash_base) {
223         if (!validate_input($hash_base)) {
224                 die_error(undef, "Invalid hash base parameter");
225         }
228 our $hash_parent_base = $cgi->param('hpb');
229 if (defined $hash_parent_base) {
230         if (!validate_input($hash_parent_base)) {
231                 die_error(undef, "Invalid hash parent base parameter");
232         }
235 our $page = $cgi->param('pg');
236 if (defined $page) {
237         if ($page =~ m/[^0-9]$/) {
238                 die_error(undef, "Invalid page parameter");
239         }
242 our $searchtext = $cgi->param('s');
243 if (defined $searchtext) {
244         if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
245                 die_error(undef, "Invalid search parameter");
246         }
247         $searchtext = quotemeta $searchtext;
250 # dispatch
251 my %actions = (
252         "blame" => \&git_blame2,
253         "blobdiff" => \&git_blobdiff,
254         "blobdiff_plain" => \&git_blobdiff_plain,
255         "blob" => \&git_blob,
256         "blob_plain" => \&git_blob_plain,
257         "commitdiff" => \&git_commitdiff,
258         "commitdiff_plain" => \&git_commitdiff_plain,
259         "commit" => \&git_commit,
260         "heads" => \&git_heads,
261         "history" => \&git_history,
262         "log" => \&git_log,
263         "rss" => \&git_rss,
264         "search" => \&git_search,
265         "shortlog" => \&git_shortlog,
266         "summary" => \&git_summary,
267         "tag" => \&git_tag,
268         "tags" => \&git_tags,
269         "tree" => \&git_tree,
270         "snapshot" => \&git_snapshot,
271         # those below don't need $project
272         "opml" => \&git_opml,
273         "project_list" => \&git_project_list,
274 );
276 if (defined $project) {
277         $action ||= 'summary';
278 } else {
279         $action ||= 'project_list';
281 if (!defined($actions{$action})) {
282         die_error(undef, "Unknown action");
284 $actions{$action}->();
285 exit;
287 ## ======================================================================
288 ## action links
290 sub href(%) {
291         my %params = @_;
293         my @mapping = (
294                 project => "p",
295                 action => "a",
296                 file_name => "f",
297                 file_parent => "fp",
298                 hash => "h",
299                 hash_parent => "hp",
300                 hash_base => "hb",
301                 hash_parent_base => "hpb",
302                 page => "pg",
303                 searchtext => "s",
304         );
305         my %mapping = @mapping;
307         $params{"project"} ||= $project;
309         my @result = ();
310         for (my $i = 0; $i < @mapping; $i += 2) {
311                 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
312                 if (defined $params{$name}) {
313                         push @result, $symbol . "=" . esc_param($params{$name});
314                 }
315         }
316         return "$my_uri?" . join(';', @result);
320 ## ======================================================================
321 ## validation, quoting/unquoting and escaping
323 sub validate_input {
324         my $input = shift;
326         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
327                 return $input;
328         }
329         if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
330                 return undef;
331         }
332         if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
333                 return undef;
334         }
335         return $input;
338 # quote unsafe chars, but keep the slash, even when it's not
339 # correct, but quoted slashes look too horrible in bookmarks
340 sub esc_param {
341         my $str = shift;
342         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
343         $str =~ s/\+/%2B/g;
344         $str =~ s/ /\+/g;
345         return $str;
348 # replace invalid utf8 character with SUBSTITUTION sequence
349 sub esc_html {
350         my $str = shift;
351         $str = decode("utf8", $str, Encode::FB_DEFAULT);
352         $str = escapeHTML($str);
353         $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
354         return $str;
357 # git may return quoted and escaped filenames
358 sub unquote {
359         my $str = shift;
360         if ($str =~ m/^"(.*)"$/) {
361                 $str = $1;
362                 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
363         }
364         return $str;
367 # escape tabs (convert tabs to spaces)
368 sub untabify {
369         my $line = shift;
371         while ((my $pos = index($line, "\t")) != -1) {
372                 if (my $count = (8 - ($pos % 8))) {
373                         my $spaces = ' ' x $count;
374                         $line =~ s/\t/$spaces/;
375                 }
376         }
378         return $line;
381 ## ----------------------------------------------------------------------
382 ## HTML aware string manipulation
384 sub chop_str {
385         my $str = shift;
386         my $len = shift;
387         my $add_len = shift || 10;
389         # allow only $len chars, but don't cut a word if it would fit in $add_len
390         # if it doesn't fit, cut it if it's still longer than the dots we would add
391         $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
392         my $body = $1;
393         my $tail = $2;
394         if (length($tail) > 4) {
395                 $tail = " ...";
396                 $body =~ s/&[^;]*$//; # remove chopped character entities
397         }
398         return "$body$tail";
401 ## ----------------------------------------------------------------------
402 ## functions returning short strings
404 # CSS class for given age value (in seconds)
405 sub age_class {
406         my $age = shift;
408         if ($age < 60*60*2) {
409                 return "age0";
410         } elsif ($age < 60*60*24*2) {
411                 return "age1";
412         } else {
413                 return "age2";
414         }
417 # convert age in seconds to "nn units ago" string
418 sub age_string {
419         my $age = shift;
420         my $age_str;
422         if ($age > 60*60*24*365*2) {
423                 $age_str = (int $age/60/60/24/365);
424                 $age_str .= " years ago";
425         } elsif ($age > 60*60*24*(365/12)*2) {
426                 $age_str = int $age/60/60/24/(365/12);
427                 $age_str .= " months ago";
428         } elsif ($age > 60*60*24*7*2) {
429                 $age_str = int $age/60/60/24/7;
430                 $age_str .= " weeks ago";
431         } elsif ($age > 60*60*24*2) {
432                 $age_str = int $age/60/60/24;
433                 $age_str .= " days ago";
434         } elsif ($age > 60*60*2) {
435                 $age_str = int $age/60/60;
436                 $age_str .= " hours ago";
437         } elsif ($age > 60*2) {
438                 $age_str = int $age/60;
439                 $age_str .= " min ago";
440         } elsif ($age > 2) {
441                 $age_str = int $age;
442                 $age_str .= " sec ago";
443         } else {
444                 $age_str .= " right now";
445         }
446         return $age_str;
449 # convert file mode in octal to symbolic file mode string
450 sub mode_str {
451         my $mode = oct shift;
453         if (S_ISDIR($mode & S_IFMT)) {
454                 return 'drwxr-xr-x';
455         } elsif (S_ISLNK($mode)) {
456                 return 'lrwxrwxrwx';
457         } elsif (S_ISREG($mode)) {
458                 # git cares only about the executable bit
459                 if ($mode & S_IXUSR) {
460                         return '-rwxr-xr-x';
461                 } else {
462                         return '-rw-r--r--';
463                 };
464         } else {
465                 return '----------';
466         }
469 # convert file mode in octal to file type string
470 sub file_type {
471         my $mode = shift;
473         if ($mode !~ m/^[0-7]+$/) {
474                 return $mode;
475         } else {
476                 $mode = oct $mode;
477         }
479         if (S_ISDIR($mode & S_IFMT)) {
480                 return "directory";
481         } elsif (S_ISLNK($mode)) {
482                 return "symlink";
483         } elsif (S_ISREG($mode)) {
484                 return "file";
485         } else {
486                 return "unknown";
487         }
490 ## ----------------------------------------------------------------------
491 ## functions returning short HTML fragments, or transforming HTML fragments
492 ## which don't beling to other sections
494 # format line of commit message or tag comment
495 sub format_log_line_html {
496         my $line = shift;
498         $line = esc_html($line);
499         $line =~ s/ /&nbsp;/g;
500         if ($line =~ m/([0-9a-fA-F]{40})/) {
501                 my $hash_text = $1;
502                 if (git_get_type($hash_text) eq "commit") {
503                         my $link =
504                                 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
505                                         -class => "text"}, $hash_text);
506                         $line =~ s/$hash_text/$link/;
507                 }
508         }
509         return $line;
512 # format marker of refs pointing to given object
513 sub format_ref_marker {
514         my ($refs, $id) = @_;
515         my $markers = '';
517         if (defined $refs->{$id}) {
518                 foreach my $ref (@{$refs->{$id}}) {
519                         my ($type, $name) = qw();
520                         # e.g. tags/v2.6.11 or heads/next
521                         if ($ref =~ m!^(.*?)s?/(.*)$!) {
522                                 $type = $1;
523                                 $name = $2;
524                         } else {
525                                 $type = "ref";
526                                 $name = $ref;
527                         }
529                         $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
530                 }
531         }
533         if ($markers) {
534                 return ' <span class="refs">'. $markers . '</span>';
535         } else {
536                 return "";
537         }
540 # format, perhaps shortened and with markers, title line
541 sub format_subject_html {
542         my ($long, $short, $href, $extra) = @_;
543         $extra = '' unless defined($extra);
545         if (length($short) < length($long)) {
546                 return $cgi->a({-href => $href, -class => "list subject",
547                                 -title => $long},
548                        esc_html($short) . $extra);
549         } else {
550                 return $cgi->a({-href => $href, -class => "list subject"},
551                        esc_html($long)  . $extra);
552         }
555 sub format_diff_line {
556         my $line = shift;
557         my $char = substr($line, 0, 1);
558         my $diff_class = "";
560         chomp $line;
562         if ($char eq '+') {
563                 $diff_class = " add";
564         } elsif ($char eq "-") {
565                 $diff_class = " rem";
566         } elsif ($char eq "@") {
567                 $diff_class = " chunk_header";
568         } elsif ($char eq "\\") {
569                 $diff_class = " incomplete";
570         }
571         $line = untabify($line);
572         return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
575 ## ----------------------------------------------------------------------
576 ## git utility subroutines, invoking git commands
578 # returns path to the core git executable and the --git-dir parameter as list
579 sub git_cmd {
580         return $GIT, '--git-dir='.$git_dir;
583 # returns path to the core git executable and the --git-dir parameter as string
584 sub git_cmd_str {
585         return join(' ', git_cmd());
588 # get HEAD ref of given project as hash
589 sub git_get_head_hash {
590         my $project = shift;
591         my $o_git_dir = $git_dir;
592         my $retval = undef;
593         $git_dir = "$projectroot/$project";
594         if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
595                 my $head = <$fd>;
596                 close $fd;
597                 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
598                         $retval = $1;
599                 }
600         }
601         if (defined $o_git_dir) {
602                 $git_dir = $o_git_dir;
603         }
604         return $retval;
607 # get type of given object
608 sub git_get_type {
609         my $hash = shift;
611         open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
612         my $type = <$fd>;
613         close $fd or return;
614         chomp $type;
615         return $type;
618 sub git_get_project_config {
619         my ($key, $type) = @_;
621         return unless ($key);
622         $key =~ s/^gitweb\.//;
623         return if ($key =~ m/\W/);
625         my @x = (git_cmd(), 'repo-config');
626         if (defined $type) { push @x, $type; }
627         push @x, "--get";
628         push @x, "gitweb.$key";
629         my $val = qx(@x);
630         chomp $val;
631         return ($val);
634 # get hash of given path at given ref
635 sub git_get_hash_by_path {
636         my $base = shift;
637         my $path = shift || return undef;
639         my $tree = $base;
641         open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
642                 or die_error(undef, "Open git-ls-tree failed");
643         my $line = <$fd>;
644         close $fd or return undef;
646         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
647         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
648         return $3;
651 ## ......................................................................
652 ## git utility functions, directly accessing git repository
654 # assumes that PATH is not symref
655 sub git_get_hash_by_ref {
656         my $path = shift;
658         open my $fd, "$projectroot/$path" or return undef;
659         my $head = <$fd>;
660         close $fd;
661         chomp $head;
662         if ($head =~ m/^[0-9a-fA-F]{40}$/) {
663                 return $head;
664         }
667 sub git_get_project_description {
668         my $path = shift;
670         open my $fd, "$projectroot/$path/description" or return undef;
671         my $descr = <$fd>;
672         close $fd;
673         chomp $descr;
674         return $descr;
677 sub git_get_project_url_list {
678         my $path = shift;
680         open my $fd, "$projectroot/$path/cloneurl" or return undef;
681         my @git_project_url_list = map { chomp; $_ } <$fd>;
682         close $fd;
684         return wantarray ? @git_project_url_list : \@git_project_url_list;
687 sub git_get_projects_list {
688         my @list;
690         if (-d $projects_list) {
691                 # search in directory
692                 my $dir = $projects_list;
693                 opendir my ($dh), $dir or return undef;
694                 while (my $dir = readdir($dh)) {
695                         if (-e "$projectroot/$dir/HEAD") {
696                                 my $pr = {
697                                         path => $dir,
698                                 };
699                                 push @list, $pr
700                         }
701                 }
702                 closedir($dh);
703         } elsif (-f $projects_list) {
704                 # read from file(url-encoded):
705                 # 'git%2Fgit.git Linus+Torvalds'
706                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
707                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
708                 open my ($fd), $projects_list or return undef;
709                 while (my $line = <$fd>) {
710                         chomp $line;
711                         my ($path, $owner) = split ' ', $line;
712                         $path = unescape($path);
713                         $owner = unescape($owner);
714                         if (!defined $path) {
715                                 next;
716                         }
717                         if (-e "$projectroot/$path/HEAD") {
718                                 my $pr = {
719                                         path => $path,
720                                         owner => decode("utf8", $owner, Encode::FB_DEFAULT),
721                                 };
722                                 push @list, $pr
723                         }
724                 }
725                 close $fd;
726         }
727         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
728         return @list;
731 sub git_get_project_owner {
732         my $project = shift;
733         my $owner;
735         return undef unless $project;
737         # read from file (url-encoded):
738         # 'git%2Fgit.git Linus+Torvalds'
739         # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
740         # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
741         if (-f $projects_list) {
742                 open (my $fd , $projects_list);
743                 while (my $line = <$fd>) {
744                         chomp $line;
745                         my ($pr, $ow) = split ' ', $line;
746                         $pr = unescape($pr);
747                         $ow = unescape($ow);
748                         if ($pr eq $project) {
749                                 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
750                                 last;
751                         }
752                 }
753                 close $fd;
754         }
755         if (!defined $owner) {
756                 $owner = get_file_owner("$projectroot/$project");
757         }
759         return $owner;
762 sub git_get_references {
763         my $type = shift || "";
764         my %refs;
765         my $fd;
766         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
767         # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
768         if (-f "$projectroot/$project/info/refs") {
769                 open $fd, "$projectroot/$project/info/refs"
770                         or return;
771         } else {
772                 open $fd, "-|", git_cmd(), "ls-remote", "."
773                         or return;
774         }
776         while (my $line = <$fd>) {
777                 chomp $line;
778                 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
779                         if (defined $refs{$1}) {
780                                 push @{$refs{$1}}, $2;
781                         } else {
782                                 $refs{$1} = [ $2 ];
783                         }
784                 }
785         }
786         close $fd or return;
787         return \%refs;
790 sub git_get_rev_name_tags {
791         my $hash = shift || return undef;
793         open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
794                 or return;
795         my $name_rev = <$fd>;
796         close $fd;
798         if ($name_rev =~ m|^$hash tags/(.*)$|) {
799                 return $1;
800         } else {
801                 # catches also '$hash undefined' output
802                 return undef;
803         }
806 ## ----------------------------------------------------------------------
807 ## parse to hash functions
809 sub parse_date {
810         my $epoch = shift;
811         my $tz = shift || "-0000";
813         my %date;
814         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
815         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
816         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
817         $date{'hour'} = $hour;
818         $date{'minute'} = $min;
819         $date{'mday'} = $mday;
820         $date{'day'} = $days[$wday];
821         $date{'month'} = $months[$mon];
822         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
823                            $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
824         $date{'mday-time'} = sprintf "%d %s %02d:%02d",
825                              $mday, $months[$mon], $hour ,$min;
827         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
828         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
829         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
830         $date{'hour_local'} = $hour;
831         $date{'minute_local'} = $min;
832         $date{'tz_local'} = $tz;
833         return %date;
836 sub parse_tag {
837         my $tag_id = shift;
838         my %tag;
839         my @comment;
841         open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
842         $tag{'id'} = $tag_id;
843         while (my $line = <$fd>) {
844                 chomp $line;
845                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
846                         $tag{'object'} = $1;
847                 } elsif ($line =~ m/^type (.+)$/) {
848                         $tag{'type'} = $1;
849                 } elsif ($line =~ m/^tag (.+)$/) {
850                         $tag{'name'} = $1;
851                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
852                         $tag{'author'} = $1;
853                         $tag{'epoch'} = $2;
854                         $tag{'tz'} = $3;
855                 } elsif ($line =~ m/--BEGIN/) {
856                         push @comment, $line;
857                         last;
858                 } elsif ($line eq "") {
859                         last;
860                 }
861         }
862         push @comment, <$fd>;
863         $tag{'comment'} = \@comment;
864         close $fd or return;
865         if (!defined $tag{'name'}) {
866                 return
867         };
868         return %tag
871 sub parse_commit {
872         my $commit_id = shift;
873         my $commit_text = shift;
875         my @commit_lines;
876         my %co;
878         if (defined $commit_text) {
879                 @commit_lines = @$commit_text;
880         } else {
881                 $/ = "\0";
882                 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
883                         or return;
884                 @commit_lines = split '\n', <$fd>;
885                 close $fd or return;
886                 $/ = "\n";
887                 pop @commit_lines;
888         }
889         my $header = shift @commit_lines;
890         if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
891                 return;
892         }
893         ($co{'id'}, my @parents) = split ' ', $header;
894         $co{'parents'} = \@parents;
895         $co{'parent'} = $parents[0];
896         while (my $line = shift @commit_lines) {
897                 last if $line eq "\n";
898                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
899                         $co{'tree'} = $1;
900                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
901                         $co{'author'} = $1;
902                         $co{'author_epoch'} = $2;
903                         $co{'author_tz'} = $3;
904                         if ($co{'author'} =~ m/^([^<]+) </) {
905                                 $co{'author_name'} = $1;
906                         } else {
907                                 $co{'author_name'} = $co{'author'};
908                         }
909                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
910                         $co{'committer'} = $1;
911                         $co{'committer_epoch'} = $2;
912                         $co{'committer_tz'} = $3;
913                         $co{'committer_name'} = $co{'committer'};
914                         $co{'committer_name'} =~ s/ <.*//;
915                 }
916         }
917         if (!defined $co{'tree'}) {
918                 return;
919         };
921         foreach my $title (@commit_lines) {
922                 $title =~ s/^    //;
923                 if ($title ne "") {
924                         $co{'title'} = chop_str($title, 80, 5);
925                         # remove leading stuff of merges to make the interesting part visible
926                         if (length($title) > 50) {
927                                 $title =~ s/^Automatic //;
928                                 $title =~ s/^merge (of|with) /Merge ... /i;
929                                 if (length($title) > 50) {
930                                         $title =~ s/(http|rsync):\/\///;
931                                 }
932                                 if (length($title) > 50) {
933                                         $title =~ s/(master|www|rsync)\.//;
934                                 }
935                                 if (length($title) > 50) {
936                                         $title =~ s/kernel.org:?//;
937                                 }
938                                 if (length($title) > 50) {
939                                         $title =~ s/\/pub\/scm//;
940                                 }
941                         }
942                         $co{'title_short'} = chop_str($title, 50, 5);
943                         last;
944                 }
945         }
946         # remove added spaces
947         foreach my $line (@commit_lines) {
948                 $line =~ s/^    //;
949         }
950         $co{'comment'} = \@commit_lines;
952         my $age = time - $co{'committer_epoch'};
953         $co{'age'} = $age;
954         $co{'age_string'} = age_string($age);
955         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
956         if ($age > 60*60*24*7*2) {
957                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
958                 $co{'age_string_age'} = $co{'age_string'};
959         } else {
960                 $co{'age_string_date'} = $co{'age_string'};
961                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
962         }
963         return %co;
966 # parse ref from ref_file, given by ref_id, with given type
967 sub parse_ref {
968         my $ref_file = shift;
969         my $ref_id = shift;
970         my $type = shift || git_get_type($ref_id);
971         my %ref_item;
973         $ref_item{'type'} = $type;
974         $ref_item{'id'} = $ref_id;
975         $ref_item{'epoch'} = 0;
976         $ref_item{'age'} = "unknown";
977         if ($type eq "tag") {
978                 my %tag = parse_tag($ref_id);
979                 $ref_item{'comment'} = $tag{'comment'};
980                 if ($tag{'type'} eq "commit") {
981                         my %co = parse_commit($tag{'object'});
982                         $ref_item{'epoch'} = $co{'committer_epoch'};
983                         $ref_item{'age'} = $co{'age_string'};
984                 } elsif (defined($tag{'epoch'})) {
985                         my $age = time - $tag{'epoch'};
986                         $ref_item{'epoch'} = $tag{'epoch'};
987                         $ref_item{'age'} = age_string($age);
988                 }
989                 $ref_item{'reftype'} = $tag{'type'};
990                 $ref_item{'name'} = $tag{'name'};
991                 $ref_item{'refid'} = $tag{'object'};
992         } elsif ($type eq "commit"){
993                 my %co = parse_commit($ref_id);
994                 $ref_item{'reftype'} = "commit";
995                 $ref_item{'name'} = $ref_file;
996                 $ref_item{'title'} = $co{'title'};
997                 $ref_item{'refid'} = $ref_id;
998                 $ref_item{'epoch'} = $co{'committer_epoch'};
999                 $ref_item{'age'} = $co{'age_string'};
1000         } else {
1001                 $ref_item{'reftype'} = $type;
1002                 $ref_item{'name'} = $ref_file;
1003                 $ref_item{'refid'} = $ref_id;
1004         }
1006         return %ref_item;
1009 # parse line of git-diff-tree "raw" output
1010 sub parse_difftree_raw_line {
1011         my $line = shift;
1012         my %res;
1014         # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
1015         # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
1016         if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1017                 $res{'from_mode'} = $1;
1018                 $res{'to_mode'} = $2;
1019                 $res{'from_id'} = $3;
1020                 $res{'to_id'} = $4;
1021                 $res{'status'} = $5;
1022                 $res{'similarity'} = $6;
1023                 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1024                         ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1025                 } else {
1026                         $res{'file'} = unquote($7);
1027                 }
1028         }
1029         # 'c512b523472485aef4fff9e57b229d9d243c967f'
1030         #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1031         #       $res{'commit'} = $1;
1032         #}
1034         return wantarray ? %res : \%res;
1037 ## ......................................................................
1038 ## parse to array of hashes functions
1040 sub git_get_refs_list {
1041         my $ref_dir = shift;
1042         my @reflist;
1044         my @refs;
1045         my $pfxlen = length("$projectroot/$project/$ref_dir");
1046         File::Find::find(sub {
1047                 return if (/^\./);
1048                 if (-f $_) {
1049                         push @refs, substr($File::Find::name, $pfxlen + 1);
1050                 }
1051         }, "$projectroot/$project/$ref_dir");
1053         foreach my $ref_file (@refs) {
1054                 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
1055                 my $type = git_get_type($ref_id) || next;
1056                 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1058                 push @reflist, \%ref_item;
1059         }
1060         # sort refs by age
1061         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1062         return \@reflist;
1065 ## ----------------------------------------------------------------------
1066 ## filesystem-related functions
1068 sub get_file_owner {
1069         my $path = shift;
1071         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1072         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1073         if (!defined $gcos) {
1074                 return undef;
1075         }
1076         my $owner = $gcos;
1077         $owner =~ s/[,;].*$//;
1078         return decode("utf8", $owner, Encode::FB_DEFAULT);
1081 ## ......................................................................
1082 ## mimetype related functions
1084 sub mimetype_guess_file {
1085         my $filename = shift;
1086         my $mimemap = shift;
1087         -r $mimemap or return undef;
1089         my %mimemap;
1090         open(MIME, $mimemap) or return undef;
1091         while (<MIME>) {
1092                 next if m/^#/; # skip comments
1093                 my ($mime, $exts) = split(/\t+/);
1094                 if (defined $exts) {
1095                         my @exts = split(/\s+/, $exts);
1096                         foreach my $ext (@exts) {
1097                                 $mimemap{$ext} = $mime;
1098                         }
1099                 }
1100         }
1101         close(MIME);
1103         $filename =~ /\.(.*?)$/;
1104         return $mimemap{$1};
1107 sub mimetype_guess {
1108         my $filename = shift;
1109         my $mime;
1110         $filename =~ /\./ or return undef;
1112         if ($mimetypes_file) {
1113                 my $file = $mimetypes_file;
1114                 if ($file !~ m!^/!) { # if it is relative path
1115                         # it is relative to project
1116                         $file = "$projectroot/$project/$file";
1117                 }
1118                 $mime = mimetype_guess_file($filename, $file);
1119         }
1120         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1121         return $mime;
1124 sub blob_mimetype {
1125         my $fd = shift;
1126         my $filename = shift;
1128         if ($filename) {
1129                 my $mime = mimetype_guess($filename);
1130                 $mime and return $mime;
1131         }
1133         # just in case
1134         return $default_blob_plain_mimetype unless $fd;
1136         if (-T $fd) {
1137                 return 'text/plain' .
1138                        ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1139         } elsif (! $filename) {
1140                 return 'application/octet-stream';
1141         } elsif ($filename =~ m/\.png$/i) {
1142                 return 'image/png';
1143         } elsif ($filename =~ m/\.gif$/i) {
1144                 return 'image/gif';
1145         } elsif ($filename =~ m/\.jpe?g$/i) {
1146                 return 'image/jpeg';
1147         } else {
1148                 return 'application/octet-stream';
1149         }
1152 ## ======================================================================
1153 ## functions printing HTML: header, footer, error page
1155 sub git_header_html {
1156         my $status = shift || "200 OK";
1157         my $expires = shift;
1159         my $title = "$site_name git";
1160         if (defined $project) {
1161                 $title .= " - $project";
1162                 if (defined $action) {
1163                         $title .= "/$action";
1164                         if (defined $file_name) {
1165                                 $title .= " - $file_name";
1166                                 if ($action eq "tree" && $file_name !~ m|/$|) {
1167                                         $title .= "/";
1168                                 }
1169                         }
1170                 }
1171         }
1172         my $content_type;
1173         # require explicit support from the UA if we are to send the page as
1174         # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1175         # we have to do this because MSIE sometimes globs '*/*', pretending to
1176         # support xhtml+xml but choking when it gets what it asked for.
1177         if (defined $cgi->http('HTTP_ACCEPT') &&
1178             $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1179             $cgi->Accept('application/xhtml+xml') != 0) {
1180                 $content_type = 'application/xhtml+xml';
1181         } else {
1182                 $content_type = 'text/html';
1183         }
1184         print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1185                            -status=> $status, -expires => $expires);
1186         print <<EOF;
1187 <?xml version="1.0" encoding="utf-8"?>
1188 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1189 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1190 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1191 <!-- git core binaries version $git_version -->
1192 <head>
1193 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1194 <meta name="generator" content="gitweb/$version git/$git_version"/>
1195 <meta name="robots" content="index, nofollow"/>
1196 <title>$title</title>
1197 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1198 EOF
1199         if (defined $project) {
1200                 printf('<link rel="alternate" title="%s log" '.
1201                        'href="%s" type="application/rss+xml"/>'."\n",
1202                        esc_param($project), href(action=>"rss"));
1203         }
1205         print "</head>\n" .
1206               "<body>\n" .
1207               "<div class=\"page_header\">\n" .
1208               "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1209               "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1210               "</a>\n";
1211         print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1212         if (defined $project) {
1213                 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1214                 if (defined $action) {
1215                         print " / $action";
1216                 }
1217                 print "\n";
1218                 if (!defined $searchtext) {
1219                         $searchtext = "";
1220                 }
1221                 my $search_hash;
1222                 if (defined $hash_base) {
1223                         $search_hash = $hash_base;
1224                 } elsif (defined $hash) {
1225                         $search_hash = $hash;
1226                 } else {
1227                         $search_hash = "HEAD";
1228                 }
1229                 $cgi->param("a", "search");
1230                 $cgi->param("h", $search_hash);
1231                 print $cgi->startform(-method => "get", -action => $my_uri) .
1232                       "<div class=\"search\">\n" .
1233                       $cgi->hidden(-name => "p") . "\n" .
1234                       $cgi->hidden(-name => "a") . "\n" .
1235                       $cgi->hidden(-name => "h") . "\n" .
1236                       $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1237                       "</div>" .
1238                       $cgi->end_form() . "\n";
1239         }
1240         print "</div>\n";
1243 sub git_footer_html {
1244         print "<div class=\"page_footer\">\n";
1245         if (defined $project) {
1246                 my $descr = git_get_project_description($project);
1247                 if (defined $descr) {
1248                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1249                 }
1250                 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1251         } else {
1252                 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1253         }
1254         print "</div>\n" .
1255               "</body>\n" .
1256               "</html>";
1259 sub die_error {
1260         my $status = shift || "403 Forbidden";
1261         my $error = shift || "Malformed query, file missing or permission denied";
1263         git_header_html($status);
1264         print <<EOF;
1265 <div class="page_body">
1266 <br /><br />
1267 $status - $error
1268 <br />
1269 </div>
1270 EOF
1271         git_footer_html();
1272         exit;
1275 ## ----------------------------------------------------------------------
1276 ## functions printing or outputting HTML: navigation
1278 sub git_print_page_nav {
1279         my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1280         $extra = '' if !defined $extra; # pager or formats
1282         my @navs = qw(summary shortlog log commit commitdiff tree);
1283         if ($suppress) {
1284                 @navs = grep { $_ ne $suppress } @navs;
1285         }
1287         my %arg = map { $_ => {action=>$_} } @navs;
1288         if (defined $head) {
1289                 for (qw(commit commitdiff)) {
1290                         $arg{$_}{hash} = $head;
1291                 }
1292                 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1293                         for (qw(shortlog log)) {
1294                                 $arg{$_}{hash} = $head;
1295                         }
1296                 }
1297         }
1298         $arg{tree}{hash} = $treehead if defined $treehead;
1299         $arg{tree}{hash_base} = $treebase if defined $treebase;
1301         print "<div class=\"page_nav\">\n" .
1302                 (join " | ",
1303                  map { $_ eq $current ?
1304                        $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1305                  } @navs);
1306         print "<br/>\n$extra<br/>\n" .
1307               "</div>\n";
1310 sub format_paging_nav {
1311         my ($action, $hash, $head, $page, $nrevs) = @_;
1312         my $paging_nav;
1315         if ($hash ne $head || $page) {
1316                 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1317         } else {
1318                 $paging_nav .= "HEAD";
1319         }
1321         if ($page > 0) {
1322                 $paging_nav .= " &sdot; " .
1323                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1324                                  -accesskey => "p", -title => "Alt-p"}, "prev");
1325         } else {
1326                 $paging_nav .= " &sdot; prev";
1327         }
1329         if ($nrevs >= (100 * ($page+1)-1)) {
1330                 $paging_nav .= " &sdot; " .
1331                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1332                                  -accesskey => "n", -title => "Alt-n"}, "next");
1333         } else {
1334                 $paging_nav .= " &sdot; next";
1335         }
1337         return $paging_nav;
1340 ## ......................................................................
1341 ## functions printing or outputting HTML: div
1343 sub git_print_header_div {
1344         my ($action, $title, $hash, $hash_base) = @_;
1345         my %args = ();
1347         $args{action} = $action;
1348         $args{hash} = $hash if $hash;
1349         $args{hash_base} = $hash_base if $hash_base;
1351         print "<div class=\"header\">\n" .
1352               $cgi->a({-href => href(%args), -class => "title"},
1353               $title ? $title : $action) .
1354               "\n</div>\n";
1357 sub git_print_page_path {
1358         my $name = shift;
1359         my $type = shift;
1360         my $hb = shift;
1362         if (!defined $name) {
1363                 print "<div class=\"page_path\">/</div>\n";
1364         } elsif (defined $type && $type eq 'blob') {
1365                 print "<div class=\"page_path\">";
1366                 if (defined $hb) {
1367                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1368                                                      hash_base=>$hb)},
1369                                       esc_html($name));
1370                 } else {
1371                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1372                                       esc_html($name));
1373                 }
1374                 print "<br/></div>\n";
1375         } else {
1376                 print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1377         }
1380 # sub git_print_log (\@;%) {
1381 sub git_print_log ($;%) {
1382         my $log = shift;
1383         my %opts = @_;
1385         if ($opts{'-remove_title'}) {
1386                 # remove title, i.e. first line of log
1387                 shift @$log;
1388         }
1389         # remove leading empty lines
1390         while (defined $log->[0] && $log->[0] eq "") {
1391                 shift @$log;
1392         }
1394         # print log
1395         my $signoff = 0;
1396         my $empty = 0;
1397         foreach my $line (@$log) {
1398                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1399                         $signoff = 1;
1400                         if (! $opts{'-remove_signoff'}) {
1401                                 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1402                                 next;
1403                         } else {
1404                                 # remove signoff lines
1405                                 next;
1406                         }
1407                 } else {
1408                         $signoff = 0;
1409                 }
1411                 # print only one empty line
1412                 # do not print empty line after signoff
1413                 if ($line eq "") {
1414                         next if ($empty || $signoff);
1415                         $empty = 1;
1416                 } else {
1417                         $empty = 0;
1418                 }
1420                 print format_log_line_html($line) . "<br/>\n";
1421         }
1423         if ($opts{'-final_empty_line'}) {
1424                 # end with single empty line
1425                 print "<br/>\n" unless $empty;
1426         }
1429 sub git_print_simplified_log {
1430         my $log = shift;
1431         my $remove_title = shift;
1433         git_print_log($log,
1434                 -final_empty_line=> 1,
1435                 -remove_signoff => 1,
1436                 -remove_title => $remove_title);
1439 ## ......................................................................
1440 ## functions printing large fragments of HTML
1442 sub git_difftree_body {
1443         my ($difftree, $hash, $parent) = @_;
1445         print "<div class=\"list_head\">\n";
1446         if ($#{$difftree} > 10) {
1447                 print(($#{$difftree} + 1) . " files changed:\n");
1448         }
1449         print "</div>\n";
1451         print "<table class=\"diff_tree\">\n";
1452         my $alternate = 0;
1453         foreach my $line (@{$difftree}) {
1454                 my %diff = parse_difftree_raw_line($line);
1456                 if ($alternate) {
1457                         print "<tr class=\"dark\">\n";
1458                 } else {
1459                         print "<tr class=\"light\">\n";
1460                 }
1461                 $alternate ^= 1;
1463                 my ($to_mode_oct, $to_mode_str, $to_file_type);
1464                 my ($from_mode_oct, $from_mode_str, $from_file_type);
1465                 if ($diff{'to_mode'} ne ('0' x 6)) {
1466                         $to_mode_oct = oct $diff{'to_mode'};
1467                         if (S_ISREG($to_mode_oct)) { # only for regular file
1468                                 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1469                         }
1470                         $to_file_type = file_type($diff{'to_mode'});
1471                 }
1472                 if ($diff{'from_mode'} ne ('0' x 6)) {
1473                         $from_mode_oct = oct $diff{'from_mode'};
1474                         if (S_ISREG($to_mode_oct)) { # only for regular file
1475                                 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1476                         }
1477                         $from_file_type = file_type($diff{'from_mode'});
1478                 }
1480                 if ($diff{'status'} eq "A") { # created
1481                         my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1482                         $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
1483                         $mode_chng   .= "]</span>";
1484                         print "<td>" .
1485                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1486                                                      hash_base=>$hash, file_name=>$diff{'file'}),
1487                                       -class => "list"}, esc_html($diff{'file'})) .
1488                               "</td>\n" .
1489                               "<td>$mode_chng</td>\n" .
1490                               "<td class=\"link\">" .
1491                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1492                                                      hash_base=>$hash, file_name=>$diff{'file'})},
1493                                       "blob") .
1494                               "</td>\n";
1496                 } elsif ($diff{'status'} eq "D") { # deleted
1497                         my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1498                         print "<td>" .
1499                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1500                                                      hash_base=>$parent, file_name=>$diff{'file'}),
1501                                        -class => "list"}, esc_html($diff{'file'})) .
1502                               "</td>\n" .
1503                               "<td>$mode_chng</td>\n" .
1504                               "<td class=\"link\">" .
1505                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1506                                                      hash_base=>$parent, file_name=>$diff{'file'})},
1507                                       "blob") .
1508                               " | " .
1509                               $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1510                                                      file_name=>$diff{'file'})},
1511                                       "history") .
1512                               "</td>\n";
1514                 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1515                         my $mode_chnge = "";
1516                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1517                                 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1518                                 if ($from_file_type != $to_file_type) {
1519                                         $mode_chnge .= " from $from_file_type to $to_file_type";
1520                                 }
1521                                 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1522                                         if ($from_mode_str && $to_mode_str) {
1523                                                 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1524                                         } elsif ($to_mode_str) {
1525                                                 $mode_chnge .= " mode: $to_mode_str";
1526                                         }
1527                                 }
1528                                 $mode_chnge .= "]</span>\n";
1529                         }
1530                         print "<td>";
1531                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1532                                 print $cgi->a({-href => href(action=>"blobdiff",
1533                                                              hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1534                                                              hash_base=>$hash, hash_parent_base=>$parent,
1535                                                              file_name=>$diff{'file'}),
1536                                               -class => "list"}, esc_html($diff{'file'}));
1537                         } else { # only mode changed
1538                                 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1539                                                              hash_base=>$hash, file_name=>$diff{'file'}),
1540                                               -class => "list"}, esc_html($diff{'file'}));
1541                         }
1542                         print "</td>\n" .
1543                               "<td>$mode_chnge</td>\n" .
1544                               "<td class=\"link\">" .
1545                                 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1546                                                        hash_base=>$hash, file_name=>$diff{'file'})},
1547                                         "blob");
1548                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1549                                 print " | " .
1550                                         $cgi->a({-href => href(action=>"blobdiff",
1551                                                                hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1552                                                                hash_base=>$hash, hash_parent_base=>$parent,
1553                                                                file_name=>$diff{'file'})},
1554                                                 "diff");
1555                         }
1556                         print " | " .
1557                                 $cgi->a({-href => href(action=>"history",
1558                                                        hash_base=>$hash, file_name=>$diff{'file'})},
1559                                         "history");
1560                         print "</td>\n";
1562                 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1563                         my %status_name = ('R' => 'moved', 'C' => 'copied');
1564                         my $nstatus = $status_name{$diff{'status'}};
1565                         my $mode_chng = "";
1566                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1567                                 # mode also for directories, so we cannot use $to_mode_str
1568                                 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1569                         }
1570                         print "<td>" .
1571                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1572                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1573                                       -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1574                               "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1575                               $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1576                                                      hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1577                                       -class => "list"}, esc_html($diff{'from_file'})) .
1578                               " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1579                               "<td class=\"link\">" .
1580                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1581                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1582                                       "blob");
1583                         if ($diff{'to_id'} ne $diff{'from_id'}) {
1584                                 print " | " .
1585                                         $cgi->a({-href => href(action=>"blobdiff",
1586                                                                hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1587                                                                hash_base=>$hash, hash_parent_base=>$parent,
1588                                                                file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1589                                                 "diff");
1590                         }
1591                         print "</td>\n";
1593                 } # we should not encounter Unmerged (U) or Unknown (X) status
1594                 print "</tr>\n";
1595         }
1596         print "</table>\n";
1599 sub git_patchset_body {
1600         my ($fd, $difftree, $hash, $hash_parent) = @_;
1602         my $patch_idx = 0;
1603         my $in_header = 0;
1604         my $patch_found = 0;
1605         my $diffinfo;
1607         print "<div class=\"patchset\">\n";
1609         LINE:
1610         while (my $patch_line = <$fd>) {
1611                 chomp $patch_line;
1613                 if ($patch_line =~ m/^diff /) { # "git diff" header
1614                         # beginning of patch (in patchset)
1615                         if ($patch_found) {
1616                                 # close previous patch
1617                                 print "</div>\n"; # class="patch"
1618                         } else {
1619                                 # first patch in patchset
1620                                 $patch_found = 1;
1621                         }
1622                         print "<div class=\"patch\">\n";
1624                         if (ref($difftree->[$patch_idx]) eq "HASH") {
1625                                 $diffinfo = $difftree->[$patch_idx];
1626                         } else {
1627                                 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1628                         }
1629                         $patch_idx++;
1631                         # for now, no extended header, hence we skip empty patches
1632                         # companion to  next LINE if $in_header;
1633                         if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1634                                 $in_header = 1;
1635                                 next LINE;
1636                         }
1638                         if ($diffinfo->{'status'} eq "A") { # added
1639                                 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1640                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1641                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1642                                               $diffinfo->{'to_id'}) . "(new)" .
1643                                       "</div>\n"; # class="diff_info"
1645                         } elsif ($diffinfo->{'status'} eq "D") { # deleted
1646                                 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1647                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1648                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1649                                               $diffinfo->{'from_id'}) . "(deleted)" .
1650                                       "</div>\n"; # class="diff_info"
1652                         } elsif ($diffinfo->{'status'} eq "R" || # renamed
1653                                  $diffinfo->{'status'} eq "C" || # copied
1654                                  $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1655                                 print "<div class=\"diff_info\">" .
1656                                       file_type($diffinfo->{'from_mode'}) . ":" .
1657                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1658                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1659                                               $diffinfo->{'from_id'}) .
1660                                       " -> " .
1661                                       file_type($diffinfo->{'to_mode'}) . ":" .
1662                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1663                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1664                                               $diffinfo->{'to_id'});
1665                                 print "</div>\n"; # class="diff_info"
1667                         } else { # modified, mode changed, ...
1668                                 print "<div class=\"diff_info\">" .
1669                                       file_type($diffinfo->{'from_mode'}) . ":" .
1670                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1671                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1672                                               $diffinfo->{'from_id'}) .
1673                                       " -> " .
1674                                       file_type($diffinfo->{'to_mode'}) . ":" .
1675                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1676                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1677                                               $diffinfo->{'to_id'});
1678                                 print "</div>\n"; # class="diff_info"
1679                         }
1681                         #print "<div class=\"diff extended_header\">\n";
1682                         $in_header = 1;
1683                         next LINE;
1684                 } # start of patch in patchset
1687                 if ($in_header && $patch_line =~ m/^---/) {
1688                         #print "</div>\n"; # class="diff extended_header"
1689                         $in_header = 0;
1691                         my $file = $diffinfo->{'from_file'};
1692                         $file  ||= $diffinfo->{'file'};
1693                         $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1694                                                        hash=>$diffinfo->{'from_id'}, file_name=>$file),
1695                                         -class => "list"}, esc_html($file));
1696                         $patch_line =~ s|a/.*$|a/$file|g;
1697                         print "<div class=\"diff from_file\">$patch_line</div>\n";
1699                         $patch_line = <$fd>;
1700                         chomp $patch_line;
1702                         #$patch_line =~ m/^+++/;
1703                         $file    = $diffinfo->{'to_file'};
1704                         $file  ||= $diffinfo->{'file'};
1705                         $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1706                                                        hash=>$diffinfo->{'to_id'}, file_name=>$file),
1707                                         -class => "list"}, esc_html($file));
1708                         $patch_line =~ s|b/.*|b/$file|g;
1709                         print "<div class=\"diff to_file\">$patch_line</div>\n";
1711                         next LINE;
1712                 }
1713                 next LINE if $in_header;
1715                 print format_diff_line($patch_line);
1716         }
1717         print "</div>\n" if $patch_found; # class="patch"
1719         print "</div>\n"; # class="patchset"
1722 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1724 sub git_shortlog_body {
1725         # uses global variable $project
1726         my ($revlist, $from, $to, $refs, $extra) = @_;
1728         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1729         my $have_snapshot = (defined $ctype && defined $suffix);
1731         $from = 0 unless defined $from;
1732         $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1734         print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1735         my $alternate = 0;
1736         for (my $i = $from; $i <= $to; $i++) {
1737                 my $commit = $revlist->[$i];
1738                 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1739                 my $ref = format_ref_marker($refs, $commit);
1740                 my %co = parse_commit($commit);
1741                 if ($alternate) {
1742                         print "<tr class=\"dark\">\n";
1743                 } else {
1744                         print "<tr class=\"light\">\n";
1745                 }
1746                 $alternate ^= 1;
1747                 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1748                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1749                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1750                       "<td>";
1751                 print format_subject_html($co{'title'}, $co{'title_short'},
1752                                           href(action=>"commit", hash=>$commit), $ref);
1753                 print "</td>\n" .
1754                       "<td class=\"link\">" .
1755                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1756                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1757                 if ($have_snapshot) {
1758                         print " | " .  $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1759                 }
1760                 print "</td>\n" .
1761                       "</tr>\n";
1762         }
1763         if (defined $extra) {
1764                 print "<tr>\n" .
1765                       "<td colspan=\"4\">$extra</td>\n" .
1766                       "</tr>\n";
1767         }
1768         print "</table>\n";
1771 sub git_history_body {
1772         # Warning: assumes constant type (blob or tree) during history
1773         my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1775         print "<table class=\"history\" cellspacing=\"0\">\n";
1776         my $alternate = 0;
1777         while (my $line = <$fd>) {
1778                 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1779                         next;
1780                 }
1782                 my $commit = $1;
1783                 my %co = parse_commit($commit);
1784                 if (!%co) {
1785                         next;
1786                 }
1788                 my $ref = format_ref_marker($refs, $commit);
1790                 if ($alternate) {
1791                         print "<tr class=\"dark\">\n";
1792                 } else {
1793                         print "<tr class=\"light\">\n";
1794                 }
1795                 $alternate ^= 1;
1796                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1797                       # shortlog uses      chop_str($co{'author_name'}, 10)
1798                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1799                       "<td>";
1800                 # originally git_history used chop_str($co{'title'}, 50)
1801                 print format_subject_html($co{'title'}, $co{'title_short'},
1802                                           href(action=>"commit", hash=>$commit), $ref);
1803                 print "</td>\n" .
1804                       "<td class=\"link\">" .
1805                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1806                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1807                       $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1809                 if ($ftype eq 'blob') {
1810                         my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1811                         my $blob_parent  = git_get_hash_by_path($commit, $file_name);
1812                         if (defined $blob_current && defined $blob_parent &&
1813                                         $blob_current ne $blob_parent) {
1814                                 print " | " .
1815                                         $cgi->a({-href => href(action=>"blobdiff",
1816                                                                hash=>$blob_current, hash_parent=>$blob_parent,
1817                                                                hash_base=>$hash_base, hash_parent_base=>$commit,
1818                                                                file_name=>$file_name)},
1819                                                 "diff to current");
1820                         }
1821                 }
1822                 print "</td>\n" .
1823                       "</tr>\n";
1824         }
1825         if (defined $extra) {
1826                 print "<tr>\n" .
1827                       "<td colspan=\"4\">$extra</td>\n" .
1828                       "</tr>\n";
1829         }
1830         print "</table>\n";
1833 sub git_tags_body {
1834         # uses global variable $project
1835         my ($taglist, $from, $to, $extra) = @_;
1836         $from = 0 unless defined $from;
1837         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1839         print "<table class=\"tags\" cellspacing=\"0\">\n";
1840         my $alternate = 0;
1841         for (my $i = $from; $i <= $to; $i++) {
1842                 my $entry = $taglist->[$i];
1843                 my %tag = %$entry;
1844                 my $comment_lines = $tag{'comment'};
1845                 my $comment = shift @$comment_lines;
1846                 my $comment_short;
1847                 if (defined $comment) {
1848                         $comment_short = chop_str($comment, 30, 5);
1849                 }
1850                 if ($alternate) {
1851                         print "<tr class=\"dark\">\n";
1852                 } else {
1853                         print "<tr class=\"light\">\n";
1854                 }
1855                 $alternate ^= 1;
1856                 print "<td><i>$tag{'age'}</i></td>\n" .
1857                       "<td>" .
1858                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1859                                -class => "list name"}, esc_html($tag{'name'})) .
1860                       "</td>\n" .
1861                       "<td>";
1862                 if (defined $comment) {
1863                         print format_subject_html($comment, $comment_short,
1864                                                   href(action=>"tag", hash=>$tag{'id'}));
1865                 }
1866                 print "</td>\n" .
1867                       "<td class=\"selflink\">";
1868                 if ($tag{'type'} eq "tag") {
1869                         print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1870                 } else {
1871                         print "&nbsp;";
1872                 }
1873                 print "</td>\n" .
1874                       "<td class=\"link\">" . " | " .
1875                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1876                 if ($tag{'reftype'} eq "commit") {
1877                         print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1878                               " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1879                 } elsif ($tag{'reftype'} eq "blob") {
1880                         print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1881                 }
1882                 print "</td>\n" .
1883                       "</tr>";
1884         }
1885         if (defined $extra) {
1886                 print "<tr>\n" .
1887                       "<td colspan=\"5\">$extra</td>\n" .
1888                       "</tr>\n";
1889         }
1890         print "</table>\n";
1893 sub git_heads_body {
1894         # uses global variable $project
1895         my ($taglist, $head, $from, $to, $extra) = @_;
1896         $from = 0 unless defined $from;
1897         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1899         print "<table class=\"heads\" cellspacing=\"0\">\n";
1900         my $alternate = 0;
1901         for (my $i = $from; $i <= $to; $i++) {
1902                 my $entry = $taglist->[$i];
1903                 my %tag = %$entry;
1904                 my $curr = $tag{'id'} eq $head;
1905                 if ($alternate) {
1906                         print "<tr class=\"dark\">\n";
1907                 } else {
1908                         print "<tr class=\"light\">\n";
1909                 }
1910                 $alternate ^= 1;
1911                 print "<td><i>$tag{'age'}</i></td>\n" .
1912                       ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1913                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1914                                -class => "list name"},esc_html($tag{'name'})) .
1915                       "</td>\n" .
1916                       "<td class=\"link\">" .
1917                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1918                       $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1919                       "</td>\n" .
1920                       "</tr>";
1921         }
1922         if (defined $extra) {
1923                 print "<tr>\n" .
1924                       "<td colspan=\"3\">$extra</td>\n" .
1925                       "</tr>\n";
1926         }
1927         print "</table>\n";
1930 ## ======================================================================
1931 ## ======================================================================
1932 ## actions
1934 sub git_project_list {
1935         my $order = $cgi->param('o');
1936         if (defined $order && $order !~ m/project|descr|owner|age/) {
1937                 die_error(undef, "Unknown order parameter");
1938         }
1940         my @list = git_get_projects_list();
1941         my @projects;
1942         if (!@list) {
1943                 die_error(undef, "No projects found");
1944         }
1945         foreach my $pr (@list) {
1946                 my $head = git_get_head_hash($pr->{'path'});
1947                 if (!defined $head) {
1948                         next;
1949                 }
1950                 $git_dir = "$projectroot/$pr->{'path'}";
1951                 my %co = parse_commit($head);
1952                 if (!%co) {
1953                         next;
1954                 }
1955                 $pr->{'commit'} = \%co;
1956                 if (!defined $pr->{'descr'}) {
1957                         my $descr = git_get_project_description($pr->{'path'}) || "";
1958                         $pr->{'descr'} = chop_str($descr, 25, 5);
1959                 }
1960                 if (!defined $pr->{'owner'}) {
1961                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1962                 }
1963                 push @projects, $pr;
1964         }
1966         git_header_html();
1967         if (-f $home_text) {
1968                 print "<div class=\"index_include\">\n";
1969                 open (my $fd, $home_text);
1970                 print <$fd>;
1971                 close $fd;
1972                 print "</div>\n";
1973         }
1974         print "<table class=\"project_list\">\n" .
1975               "<tr>\n";
1976         $order ||= "project";
1977         if ($order eq "project") {
1978                 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1979                 print "<th>Project</th>\n";
1980         } else {
1981                 print "<th>" .
1982                       $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
1983                                -class => "header"}, "Project") .
1984                       "</th>\n";
1985         }
1986         if ($order eq "descr") {
1987                 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1988                 print "<th>Description</th>\n";
1989         } else {
1990                 print "<th>" .
1991                       $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
1992                                -class => "header"}, "Description") .
1993                       "</th>\n";
1994         }
1995         if ($order eq "owner") {
1996                 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1997                 print "<th>Owner</th>\n";
1998         } else {
1999                 print "<th>" .
2000                       $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
2001                                -class => "header"}, "Owner") .
2002                       "</th>\n";
2003         }
2004         if ($order eq "age") {
2005                 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2006                 print "<th>Last Change</th>\n";
2007         } else {
2008                 print "<th>" .
2009                       $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
2010                                -class => "header"}, "Last Change") .
2011                       "</th>\n";
2012         }
2013         print "<th></th>\n" .
2014               "</tr>\n";
2015         my $alternate = 0;
2016         foreach my $pr (@projects) {
2017                 if ($alternate) {
2018                         print "<tr class=\"dark\">\n";
2019                 } else {
2020                         print "<tr class=\"light\">\n";
2021                 }
2022                 $alternate ^= 1;
2023                 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2024                                         -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2025                       "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2026                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2027                 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2028                       $pr->{'commit'}{'age_string'} . "</td>\n" .
2029                       "<td class=\"link\">" .
2030                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
2031                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2032                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2033                       "</td>\n" .
2034                       "</tr>\n";
2035         }
2036         print "</table>\n";
2037         git_footer_html();
2040 sub git_summary {
2041         my $descr = git_get_project_description($project) || "none";
2042         my $head = git_get_head_hash($project);
2043         my %co = parse_commit($head);
2044         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2046         my $owner = git_get_project_owner($project);
2048         my $refs = git_get_references();
2049         git_header_html();
2050         git_print_page_nav('summary','', $head);
2052         print "<div class=\"title\">&nbsp;</div>\n";
2053         print "<table cellspacing=\"0\">\n" .
2054               "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2055               "<tr><td>owner</td><td>$owner</td></tr>\n" .
2056               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2057         # use per project git URL list in $projectroot/$project/cloneurl
2058         # or make project git URL from git base URL and project name
2059         my $url_tag = "URL";
2060         my @url_list = git_get_project_url_list($project);
2061         @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2062         foreach my $git_url (@url_list) {
2063                 next unless $git_url;
2064                 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2065                 $url_tag = "";
2066         }
2067         print "</table>\n";
2069         open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2070                 git_get_head_hash($project)
2071                 or die_error(undef, "Open git-rev-list failed");
2072         my @revlist = map { chomp; $_ } <$fd>;
2073         close $fd;
2074         git_print_header_div('shortlog');
2075         git_shortlog_body(\@revlist, 0, 15, $refs,
2076                           $cgi->a({-href => href(action=>"shortlog")}, "..."));
2078         my $taglist = git_get_refs_list("refs/tags");
2079         if (defined @$taglist) {
2080                 git_print_header_div('tags');
2081                 git_tags_body($taglist, 0, 15,
2082                               $cgi->a({-href => href(action=>"tags")}, "..."));
2083         }
2085         my $headlist = git_get_refs_list("refs/heads");
2086         if (defined @$headlist) {
2087                 git_print_header_div('heads');
2088                 git_heads_body($headlist, $head, 0, 15,
2089                                $cgi->a({-href => href(action=>"heads")}, "..."));
2090         }
2092         git_footer_html();
2095 sub git_tag {
2096         my $head = git_get_head_hash($project);
2097         git_header_html();
2098         git_print_page_nav('','', $head,undef,$head);
2099         my %tag = parse_tag($hash);
2100         git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2101         print "<div class=\"title_text\">\n" .
2102               "<table cellspacing=\"0\">\n" .
2103               "<tr>\n" .
2104               "<td>object</td>\n" .
2105               "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2106                                $tag{'object'}) . "</td>\n" .
2107               "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2108                                               $tag{'type'}) . "</td>\n" .
2109               "</tr>\n";
2110         if (defined($tag{'author'})) {
2111                 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2112                 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2113                 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2114                         sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2115                         "</td></tr>\n";
2116         }
2117         print "</table>\n\n" .
2118               "</div>\n";
2119         print "<div class=\"page_body\">";
2120         my $comment = $tag{'comment'};
2121         foreach my $line (@$comment) {
2122                 print esc_html($line) . "<br/>\n";
2123         }
2124         print "</div>\n";
2125         git_footer_html();
2128 sub git_blame2 {
2129         my $fd;
2130         my $ftype;
2132         if (!gitweb_check_feature('blame')) {
2133                 die_error('403 Permission denied', "Permission denied");
2134         }
2135         die_error('404 Not Found', "File name not defined") if (!$file_name);
2136         $hash_base ||= git_get_head_hash($project);
2137         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2138         my %co = parse_commit($hash_base)
2139                 or die_error(undef, "Reading commit failed");
2140         if (!defined $hash) {
2141                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2142                         or die_error(undef, "Error looking up file");
2143         }
2144         $ftype = git_get_type($hash);
2145         if ($ftype !~ "blob") {
2146                 die_error("400 Bad Request", "Object is not a blob");
2147         }
2148         open ($fd, "-|", git_cmd(), "blame", '-l', $file_name, $hash_base)
2149                 or die_error(undef, "Open git-blame failed");
2150         git_header_html();
2151         my $formats_nav =
2152                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2153                         "blob") .
2154                 " | " .
2155                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2156                         "head");
2157         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2158         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2159         git_print_page_path($file_name, $ftype, $hash_base);
2160         my @rev_color = (qw(light2 dark2));
2161         my $num_colors = scalar(@rev_color);
2162         my $current_color = 0;
2163         my $last_rev;
2164         print <<HTML;
2165 <div class="page_body">
2166 <table class="blame">
2167 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2168 HTML
2169         while (<$fd>) {
2170                 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2171                 my $full_rev = $1;
2172                 my $rev = substr($full_rev, 0, 8);
2173                 my $lineno = $2;
2174                 my $data = $3;
2176                 if (!defined $last_rev) {
2177                         $last_rev = $full_rev;
2178                 } elsif ($last_rev ne $full_rev) {
2179                         $last_rev = $full_rev;
2180                         $current_color = ++$current_color % $num_colors;
2181                 }
2182                 print "<tr class=\"$rev_color[$current_color]\">\n";
2183                 print "<td class=\"sha1\">" .
2184                         $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2185                                 esc_html($rev)) . "</td>\n";
2186                 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2187                       esc_html($lineno) . "</a></td>\n";
2188                 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2189                 print "</tr>\n";
2190         }
2191         print "</table>\n";
2192         print "</div>";
2193         close $fd
2194                 or print "Reading blob failed\n";
2195         git_footer_html();
2198 sub git_blame {
2199         my $fd;
2201         if (!gitweb_check_feature('blame')) {
2202                 die_error('403 Permission denied', "Permission denied");
2203         }
2204         die_error('404 Not Found', "File name not defined") if (!$file_name);
2205         $hash_base ||= git_get_head_hash($project);
2206         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2207         my %co = parse_commit($hash_base)
2208                 or die_error(undef, "Reading commit failed");
2209         if (!defined $hash) {
2210                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2211                         or die_error(undef, "Error lookup file");
2212         }
2213         open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2214                 or die_error(undef, "Open git-annotate failed");
2215         git_header_html();
2216         my $formats_nav =
2217                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2218                         "blob") .
2219                 " | " .
2220                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2221                         "head");
2222         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2223         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2224         git_print_page_path($file_name, 'blob', $hash_base);
2225         print "<div class=\"page_body\">\n";
2226         print <<HTML;
2227 <table class="blame">
2228   <tr>
2229     <th>Commit</th>
2230     <th>Age</th>
2231     <th>Author</th>
2232     <th>Line</th>
2233     <th>Data</th>
2234   </tr>
2235 HTML
2236         my @line_class = (qw(light dark));
2237         my $line_class_len = scalar (@line_class);
2238         my $line_class_num = $#line_class;
2239         while (my $line = <$fd>) {
2240                 my $long_rev;
2241                 my $short_rev;
2242                 my $author;
2243                 my $time;
2244                 my $lineno;
2245                 my $data;
2246                 my $age;
2247                 my $age_str;
2248                 my $age_class;
2250                 chomp $line;
2251                 $line_class_num = ($line_class_num + 1) % $line_class_len;
2253                 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2254                         $long_rev = $1;
2255                         $author   = $2;
2256                         $time     = $3;
2257                         $lineno   = $4;
2258                         $data     = $5;
2259                 } else {
2260                         print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2261                         next;
2262                 }
2263                 $short_rev  = substr ($long_rev, 0, 8);
2264                 $age        = time () - $time;
2265                 $age_str    = age_string ($age);
2266                 $age_str    =~ s/ /&nbsp;/g;
2267                 $age_class  = age_class($age);
2268                 $author     = esc_html ($author);
2269                 $author     =~ s/ /&nbsp;/g;
2271                 $data = untabify($data);
2272                 $data = esc_html ($data);
2274                 print <<HTML;
2275   <tr class="$line_class[$line_class_num]">
2276     <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2277     <td class="$age_class">$age_str</td>
2278     <td>$author</td>
2279     <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2280     <td class="pre">$data</td>
2281   </tr>
2282 HTML
2283         } # while (my $line = <$fd>)
2284         print "</table>\n\n";
2285         close $fd
2286                 or print "Reading blob failed.\n";
2287         print "</div>";
2288         git_footer_html();
2291 sub git_tags {
2292         my $head = git_get_head_hash($project);
2293         git_header_html();
2294         git_print_page_nav('','', $head,undef,$head);
2295         git_print_header_div('summary', $project);
2297         my $taglist = git_get_refs_list("refs/tags");
2298         if (defined @$taglist) {
2299                 git_tags_body($taglist);
2300         }
2301         git_footer_html();
2304 sub git_heads {
2305         my $head = git_get_head_hash($project);
2306         git_header_html();
2307         git_print_page_nav('','', $head,undef,$head);
2308         git_print_header_div('summary', $project);
2310         my $taglist = git_get_refs_list("refs/heads");
2311         if (defined @$taglist) {
2312                 git_heads_body($taglist, $head);
2313         }
2314         git_footer_html();
2317 sub git_blob_plain {
2318         # blobs defined by non-textual hash id's can be cached
2319         my $expires;
2320         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2321                 $expires = "+1d";
2322         }
2324         if (!defined $hash) {
2325                 if (defined $file_name) {
2326                         my $base = $hash_base || git_get_head_hash($project);
2327                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2328                                 or die_error(undef, "Error lookup file");
2329                 } else {
2330                         die_error(undef, "No file name defined");
2331                 }
2332         }
2333         my $type = shift;
2334         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2335                 or die_error(undef, "Couldn't cat $file_name, $hash");
2337         $type ||= blob_mimetype($fd, $file_name);
2339         # save as filename, even when no $file_name is given
2340         my $save_as = "$hash";
2341         if (defined $file_name) {
2342                 $save_as = $file_name;
2343         } elsif ($type =~ m/^text\//) {
2344                 $save_as .= '.txt';
2345         }
2347         print $cgi->header(
2348                 -type => "$type",
2349                 -expires=>$expires,
2350                 -content_disposition => "inline; filename=\"$save_as\"");
2351         undef $/;
2352         binmode STDOUT, ':raw';
2353         print <$fd>;
2354         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2355         $/ = "\n";
2356         close $fd;
2359 sub git_blob {
2360         # blobs defined by non-textual hash id's can be cached
2361         my $expires;
2362         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2363                 $expires = "+1d";
2364         }
2366         if (!defined $hash) {
2367                 if (defined $file_name) {
2368                         my $base = $hash_base || git_get_head_hash($project);
2369                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2370                                 or die_error(undef, "Error lookup file");
2371                 } else {
2372                         die_error(undef, "No file name defined");
2373                 }
2374         }
2375         my $have_blame = gitweb_check_feature('blame');
2376         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2377                 or die_error(undef, "Couldn't cat $file_name, $hash");
2378         my $mimetype = blob_mimetype($fd, $file_name);
2379         if ($mimetype !~ m/^text\//) {
2380                 close $fd;
2381                 return git_blob_plain($mimetype);
2382         }
2383         git_header_html(undef, $expires);
2384         my $formats_nav = '';
2385         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2386                 if (defined $file_name) {
2387                         if ($have_blame) {
2388                                 $formats_nav .=
2389                                         $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2390                                                                hash=>$hash, file_name=>$file_name)},
2391                                                 "blame") .
2392                                         " | ";
2393                         }
2394                         $formats_nav .=
2395                                 $cgi->a({-href => href(action=>"blob_plain",
2396                                                        hash=>$hash, file_name=>$file_name)},
2397                                         "plain") .
2398                                 " | " .
2399                                 $cgi->a({-href => href(action=>"blob",
2400                                                        hash_base=>"HEAD", file_name=>$file_name)},
2401                                         "head");
2402                 } else {
2403                         $formats_nav .=
2404                                 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2405                 }
2406                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2407                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2408         } else {
2409                 print "<div class=\"page_nav\">\n" .
2410                       "<br/><br/></div>\n" .
2411                       "<div class=\"title\">$hash</div>\n";
2412         }
2413         git_print_page_path($file_name, "blob", $hash_base);
2414         print "<div class=\"page_body\">\n";
2415         my $nr;
2416         while (my $line = <$fd>) {
2417                 chomp $line;
2418                 $nr++;
2419                 $line = untabify($line);
2420                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2421                        $nr, $nr, $nr, esc_html($line);
2422         }
2423         close $fd
2424                 or print "Reading blob failed.\n";
2425         print "</div>";
2426         git_footer_html();
2429 sub git_tree {
2430         if (!defined $hash) {
2431                 $hash = git_get_head_hash($project);
2432                 if (defined $file_name) {
2433                         my $base = $hash_base || $hash;
2434                         $hash = git_get_hash_by_path($base, $file_name, "tree");
2435                 }
2436                 if (!defined $hash_base) {
2437                         $hash_base = $hash;
2438                 }
2439         }
2440         $/ = "\0";
2441         open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2442                 or die_error(undef, "Open git-ls-tree failed");
2443         my @entries = map { chomp; $_ } <$fd>;
2444         close $fd or die_error(undef, "Reading tree failed");
2445         $/ = "\n";
2447         my $refs = git_get_references();
2448         my $ref = format_ref_marker($refs, $hash_base);
2449         git_header_html();
2450         my %base_key = ();
2451         my $base = "";
2452         my $have_blame = gitweb_check_feature('blame');
2453         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2454                 $base_key{hash_base} = $hash_base;
2455                 git_print_page_nav('tree','', $hash_base);
2456                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2457         } else {
2458                 print "<div class=\"page_nav\">\n";
2459                 print "<br/><br/></div>\n";
2460                 print "<div class=\"title\">$hash</div>\n";
2461         }
2462         if (defined $file_name) {
2463                 $base = esc_html("$file_name/");
2464         }
2465         git_print_page_path($file_name, 'tree', $hash_base);
2466         print "<div class=\"page_body\">\n";
2467         print "<table cellspacing=\"0\">\n";
2468         my $alternate = 0;
2469         foreach my $line (@entries) {
2470                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
2471                 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2472                 my $t_mode = $1;
2473                 my $t_type = $2;
2474                 my $t_hash = $3;
2475                 my $t_name = validate_input($4);
2476                 if ($alternate) {
2477                         print "<tr class=\"dark\">\n";
2478                 } else {
2479                         print "<tr class=\"light\">\n";
2480                 }
2481                 $alternate ^= 1;
2482                 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2483                 if ($t_type eq "blob") {
2484                         print "<td class=\"list\">" .
2485                               $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2486                                       -class => "list"}, esc_html($t_name)) .
2487                               "</td>\n" .
2488                               "<td class=\"link\">" .
2489                               $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2490                                       "blob");
2491                         if ($have_blame) {
2492                                 print " | " .
2493                                         $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2494                                                 "blame");
2495                         }
2496                         print " | " .
2497                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2498                                                      hash=>$t_hash, file_name=>"$base$t_name")},
2499                                       "history") .
2500                               " | " .
2501                               $cgi->a({-href => href(action=>"blob_plain",
2502                                                      hash=>$t_hash, file_name=>"$base$t_name")},
2503                                       "raw") .
2504                               "</td>\n";
2505                 } elsif ($t_type eq "tree") {
2506                         print "<td class=\"list\">" .
2507                               $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2508                                       esc_html($t_name)) .
2509                               "</td>\n" .
2510                               "<td class=\"link\">" .
2511                               $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2512                                       "tree") .
2513                               " | " .
2514                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2515                                       "history") .
2516                               "</td>\n";
2517                 }
2518                 print "</tr>\n";
2519         }
2520         print "</table>\n" .
2521               "</div>";
2522         git_footer_html();
2525 sub git_snapshot {
2527         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2528         my $have_snapshot = (defined $ctype && defined $suffix);
2529         if (!$have_snapshot) {
2530                 die_error('403 Permission denied', "Permission denied");
2531         }
2533         if (!defined $hash) {
2534                 $hash = git_get_head_hash($project);
2535         }
2537         my $filename = basename($project) . "-$hash.tar.$suffix";
2539         print $cgi->header(-type => 'application/x-tar',
2540                            -content_encoding => $ctype,
2541                            -content_disposition => "inline; filename=\"$filename\"",
2542                            -status => '200 OK');
2544         my $git_command = git_cmd_str();
2545         open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2546                 die_error(undef, "Execute git-tar-tree failed.");
2547         binmode STDOUT, ':raw';
2548         print <$fd>;
2549         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2550         close $fd;
2554 sub git_log {
2555         my $head = git_get_head_hash($project);
2556         if (!defined $hash) {
2557                 $hash = $head;
2558         }
2559         if (!defined $page) {
2560                 $page = 0;
2561         }
2562         my $refs = git_get_references();
2564         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2565         open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2566                 or die_error(undef, "Open git-rev-list failed");
2567         my @revlist = map { chomp; $_ } <$fd>;
2568         close $fd;
2570         my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2572         git_header_html();
2573         git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2575         if (!@revlist) {
2576                 my %co = parse_commit($hash);
2578                 git_print_header_div('summary', $project);
2579                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2580         }
2581         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2582                 my $commit = $revlist[$i];
2583                 my $ref = format_ref_marker($refs, $commit);
2584                 my %co = parse_commit($commit);
2585                 next if !%co;
2586                 my %ad = parse_date($co{'author_epoch'});
2587                 git_print_header_div('commit',
2588                                "<span class=\"age\">$co{'age_string'}</span>" .
2589                                esc_html($co{'title'}) . $ref,
2590                                $commit);
2591                 print "<div class=\"title_text\">\n" .
2592                       "<div class=\"log_link\">\n" .
2593                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2594                       " | " .
2595                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2596                       "<br/>\n" .
2597                       "</div>\n" .
2598                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
2599                       "</div>\n";
2601                 print "<div class=\"log_body\">\n";
2602                 git_print_simplified_log($co{'comment'});
2603                 print "</div>\n";
2604         }
2605         git_footer_html();
2608 sub git_commit {
2609         my %co = parse_commit($hash);
2610         if (!%co) {
2611                 die_error(undef, "Unknown commit object");
2612         }
2613         my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2614         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2616         my $parent = $co{'parent'};
2617         if (!defined $parent) {
2618                 $parent = "--root";
2619         }
2620         open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2621                 or die_error(undef, "Open git-diff-tree failed");
2622         my @difftree = map { chomp; $_ } <$fd>;
2623         close $fd or die_error(undef, "Reading git-diff-tree failed");
2625         # non-textual hash id's can be cached
2626         my $expires;
2627         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2628                 $expires = "+1d";
2629         }
2630         my $refs = git_get_references();
2631         my $ref = format_ref_marker($refs, $co{'id'});
2633         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2634         my $have_snapshot = (defined $ctype && defined $suffix);
2636         my $formats_nav = '';
2637         if (defined $file_name && defined $co{'parent'}) {
2638                 my $parent = $co{'parent'};
2639                 $formats_nav .=
2640                         $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2641                                 "blame");
2642         }
2643         git_header_html(undef, $expires);
2644         git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2645                            $hash, $co{'tree'}, $hash,
2646                            $formats_nav);
2648         if (defined $co{'parent'}) {
2649                 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2650         } else {
2651                 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2652         }
2653         print "<div class=\"title_text\">\n" .
2654               "<table cellspacing=\"0\">\n";
2655         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2656               "<tr>" .
2657               "<td></td><td> $ad{'rfc2822'}";
2658         if ($ad{'hour_local'} < 6) {
2659                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2660                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2661         } else {
2662                 printf(" (%02d:%02d %s)",
2663                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2664         }
2665         print "</td>" .
2666               "</tr>\n";
2667         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2668         print "<tr><td></td><td> $cd{'rfc2822'}" .
2669               sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2670               "</td></tr>\n";
2671         print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2672         print "<tr>" .
2673               "<td>tree</td>" .
2674               "<td class=\"sha1\">" .
2675               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2676                        class => "list"}, $co{'tree'}) .
2677               "</td>" .
2678               "<td class=\"link\">" .
2679               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2680                       "tree");
2681         if ($have_snapshot) {
2682                 print " | " .
2683                       $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2684         }
2685         print "</td>" .
2686               "</tr>\n";
2687         my $parents = $co{'parents'};
2688         foreach my $par (@$parents) {
2689                 print "<tr>" .
2690                       "<td>parent</td>" .
2691                       "<td class=\"sha1\">" .
2692                       $cgi->a({-href => href(action=>"commit", hash=>$par),
2693                                class => "list"}, $par) .
2694                       "</td>" .
2695                       "<td class=\"link\">" .
2696                       $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2697                       " | " .
2698                       $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2699                       "</td>" .
2700                       "</tr>\n";
2701         }
2702         print "</table>".
2703               "</div>\n";
2705         print "<div class=\"page_body\">\n";
2706         git_print_log($co{'comment'});
2707         print "</div>\n";
2709         git_difftree_body(\@difftree, $hash, $parent);
2711         git_footer_html();
2714 sub git_blobdiff {
2715         my $format = shift || 'html';
2717         my $fd;
2718         my @difftree;
2719         my %diffinfo;
2720         my $expires;
2722         # preparing $fd and %diffinfo for git_patchset_body
2723         # new style URI
2724         if (defined $hash_base && defined $hash_parent_base) {
2725                 if (defined $file_name) {
2726                         # read raw output
2727                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
2728                                 "--", $file_name
2729                                 or die_error(undef, "Open git-diff-tree failed");
2730                         @difftree = map { chomp; $_ } <$fd>;
2731                         close $fd
2732                                 or die_error(undef, "Reading git-diff-tree failed");
2733                         @difftree
2734                                 or die_error('404 Not Found', "Blob diff not found");
2736                 } elsif (defined $hash) { # try to find filename from $hash
2737                         if ($hash !~ /[0-9a-fA-F]{40}/) {
2738                                 $hash = git_to_hash($hash);
2739                         }
2740                 } elsif (defined $hash &&
2741                          $hash =~ /[0-9a-fA-F]{40}/) {
2742                         # try to find filename from $hash
2744                         # read filtered raw output
2745                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
2746                                 or die_error(undef, "Open git-diff-tree failed");
2747                         @difftree =
2748                                 # ':100644 100644 03b21826... 3b93d5e7... M     ls-files.c'
2749                                 # $hash == to_id
2750                                 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
2751                                 map { chomp; $_ } <$fd>;
2752                         close $fd
2753                                 or die_error(undef, "Reading git-diff-tree failed");
2754                         @difftree
2755                                 or die_error('404 Not Found', "Blob diff not found");
2757                 } else {
2758                         die_error('404 Not Found', "Missing one of the blob diff parameters");
2759                 }
2761                 if (@difftree > 1) {
2762                         die_error('404 Not Found', "Ambiguous blob diff specification");
2763                 }
2765                 %diffinfo = parse_difftree_raw_line($difftree[0]);
2766                 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
2767                 $file_name   ||= $diffinfo{'to_file'}   || $diffinfo{'file'};
2769                 $hash_parent ||= $diffinfo{'from_id'};
2770                 $hash        ||= $diffinfo{'to_id'};
2772                 # non-textual hash id's can be cached
2773                 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
2774                     $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
2775                         $expires = '+1d';
2776                 }
2778                 # open patch output
2779                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2780                         '-p', $hash_parent_base, $hash_base,
2781                         "--", $file_name
2782                         or die_error(undef, "Open git-diff-tree failed");
2783         }
2785         # old/legacy style URI
2786         if (!%diffinfo && # if new style URI failed
2787             defined $hash && defined $hash_parent) {
2788                 # fake git-diff-tree raw output
2789                 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
2790                 $diffinfo{'from_id'} = $hash_parent;
2791                 $diffinfo{'to_id'}   = $hash;
2792                 if (defined $file_name) {
2793                         if (defined $file_parent) {
2794                                 $diffinfo{'status'} = '2';
2795                                 $diffinfo{'from_file'} = $file_parent;
2796                                 $diffinfo{'to_file'}   = $file_name;
2797                         } else { # assume not renamed
2798                                 $diffinfo{'status'} = '1';
2799                                 $diffinfo{'from_file'} = $file_name;
2800                                 $diffinfo{'to_file'}   = $file_name;
2801                         }
2802                 } else { # no filename given
2803                         $diffinfo{'status'} = '2';
2804                         $diffinfo{'from_file'} = $hash_parent;
2805                         $diffinfo{'to_file'}   = $hash;
2806                 }
2808                 # non-textual hash id's can be cached
2809                 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
2810                     $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
2811                         $expires = '+1d';
2812                 }
2814                 # open patch output
2815                 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
2816                         or die_error(undef, "Open git-diff failed");
2817         } else  {
2818                 die_error('404 Not Found', "Missing one of the blob diff parameters")
2819                         unless %diffinfo;
2820         }
2822         # header
2823         if ($format eq 'html') {
2824                 my $formats_nav =
2825                         $cgi->a({-href => href(action=>"blobdiff_plain",
2826                                                hash=>$hash, hash_parent=>$hash_parent,
2827                                                hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
2828                                                file_name=>$file_name, file_parent=>$file_parent)},
2829                                 "plain");
2830                 git_header_html(undef, $expires);
2831                 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2832                         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2833                         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2834                 } else {
2835                         print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
2836                         print "<div class=\"title\">$hash vs $hash_parent</div>\n";
2837                 }
2838                 if (defined $file_name) {
2839                         git_print_page_path($file_name, "blob", $hash_base);
2840                 } else {
2841                         print "<div class=\"page_path\"></div>\n";
2842                 }
2844         } elsif ($format eq 'plain') {
2845                 print $cgi->header(
2846                         -type => 'text/plain',
2847                         -charset => 'utf-8',
2848                         -expires => $expires,
2849                         -content_disposition => qq(inline; filename="${file_name}.patch"));
2851                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2853         } else {
2854                 die_error(undef, "Unknown blobdiff format");
2855         }
2857         # patch
2858         if ($format eq 'html') {
2859                 print "<div class=\"page_body\">\n";
2861                 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
2862                 close $fd;
2864                 print "</div>\n"; # class="page_body"
2865                 git_footer_html();
2867         } else {
2868                 while (my $line = <$fd>) {
2869                         $line =~ s!a/($hash|$hash_parent)!a/$diffinfo{'from_file'}!g;
2870                         $line =~ s!b/($hash|$hash_parent)!b/$diffinfo{'to_file'}!g;
2872                         print $line;
2874                         last if $line =~ m!^\+\+\+!;
2875                 }
2876                 local $/ = undef;
2877                 print <$fd>;
2878                 close $fd;
2879         }
2882 sub git_blobdiff_plain {
2883         git_blobdiff('plain');
2886 sub git_commitdiff {
2887         my $format = shift || 'html';
2888         my %co = parse_commit($hash);
2889         if (!%co) {
2890                 die_error(undef, "Unknown commit object");
2891         }
2892         if (!defined $hash_parent) {
2893                 $hash_parent = $co{'parent'} || '--root';
2894         }
2896         # read commitdiff
2897         my $fd;
2898         my @difftree;
2899         if ($format eq 'html') {
2900                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2901                         "--patch-with-raw", "--full-index", $hash_parent, $hash
2902                         or die_error(undef, "Open git-diff-tree failed");
2904                 while (chomp(my $line = <$fd>)) {
2905                         # empty line ends raw part of diff-tree output
2906                         last unless $line;
2907                         push @difftree, $line;
2908                 }
2910         } elsif ($format eq 'plain') {
2911                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2912                         '-p', $hash_parent, $hash
2913                         or die_error(undef, "Open git-diff-tree failed");
2915         } else {
2916                 die_error(undef, "Unknown commitdiff format");
2917         }
2919         # non-textual hash id's can be cached
2920         my $expires;
2921         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2922                 $expires = "+1d";
2923         }
2925         # write commit message
2926         if ($format eq 'html') {
2927                 my $refs = git_get_references();
2928                 my $ref = format_ref_marker($refs, $co{'id'});
2929                 my $formats_nav =
2930                         $cgi->a({-href => href(action=>"commitdiff_plain",
2931                                                hash=>$hash, hash_parent=>$hash_parent)},
2932                                 "plain");
2934                 git_header_html(undef, $expires);
2935                 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2936                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2937                 print "<div class=\"page_body\">\n";
2938                 print "<div class=\"log\">\n";
2939                 git_print_simplified_log($co{'comment'}, 1); # skip title
2940                 print "</div>\n"; # class="log"
2942         } elsif ($format eq 'plain') {
2943                 my $refs = git_get_references("tags");
2944                 my $tagname = git_get_rev_name_tags($hash);
2945                 my $filename = basename($project) . "-$hash.patch";
2947                 print $cgi->header(
2948                         -type => 'text/plain',
2949                         -charset => 'utf-8',
2950                         -expires => $expires,
2951                         -content_disposition => qq(inline; filename="$filename"));
2952                 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2953                 print <<TEXT;
2954 From: $co{'author'}
2955 Date: $ad{'rfc2822'} ($ad{'tz_local'})
2956 Subject: $co{'title'}
2957 TEXT
2958                 print "X-Git-Tag: $tagname\n" if $tagname;
2959                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2961                 foreach my $line (@{$co{'comment'}}) {
2962                         print "$line\n";
2963                 }
2964                 print "---\n\n";
2965         }
2967         # write patch
2968         if ($format eq 'html') {
2969                 #git_difftree_body(\@difftree, $hash, $hash_parent);
2970                 #print "<br/>\n";
2972                 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
2973                 close $fd;
2974                 print "</div>\n"; # class="page_body"
2975                 git_footer_html();
2977         } elsif ($format eq 'plain') {
2978                 local $/ = undef;
2979                 print <$fd>;
2980                 close $fd
2981                         or print "Reading git-diff-tree failed\n";
2982         }
2985 sub git_commitdiff_plain {
2986         git_commitdiff('plain');
2989 sub git_history {
2990         if (!defined $hash_base) {
2991                 $hash_base = git_get_head_hash($project);
2992         }
2993         my $ftype;
2994         my %co = parse_commit($hash_base);
2995         if (!%co) {
2996                 die_error(undef, "Unknown commit object");
2997         }
2998         my $refs = git_get_references();
2999         git_header_html();
3000         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
3001         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3002         if (!defined $hash && defined $file_name) {
3003                 $hash = git_get_hash_by_path($hash_base, $file_name);
3004         }
3005         if (defined $hash) {
3006                 $ftype = git_get_type($hash);
3007         }
3008         git_print_page_path($file_name, $ftype, $hash_base);
3010         open my $fd, "-|",
3011                 git_cmd(), "rev-list", "--full-history", $hash_base, "--", $file_name;
3013         git_history_body($fd, $refs, $hash_base, $ftype);
3015         close $fd;
3016         git_footer_html();
3019 sub git_search {
3020         if (!defined $searchtext) {
3021                 die_error(undef, "Text field empty");
3022         }
3023         if (!defined $hash) {
3024                 $hash = git_get_head_hash($project);
3025         }
3026         my %co = parse_commit($hash);
3027         if (!%co) {
3028                 die_error(undef, "Unknown commit object");
3029         }
3030         # pickaxe may take all resources of your box and run for several minutes
3031         # with every query - so decide by yourself how public you make this feature :)
3032         my $commit_search = 1;
3033         my $author_search = 0;
3034         my $committer_search = 0;
3035         my $pickaxe_search = 0;
3036         if ($searchtext =~ s/^author\\://i) {
3037                 $author_search = 1;
3038         } elsif ($searchtext =~ s/^committer\\://i) {
3039                 $committer_search = 1;
3040         } elsif ($searchtext =~ s/^pickaxe\\://i) {
3041                 $commit_search = 0;
3042                 $pickaxe_search = 1;
3043         }
3044         git_header_html();
3045         git_print_page_nav('','', $hash,$co{'tree'},$hash);
3046         git_print_header_div('commit', esc_html($co{'title'}), $hash);
3048         print "<table cellspacing=\"0\">\n";
3049         my $alternate = 0;
3050         if ($commit_search) {
3051                 $/ = "\0";
3052                 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3053                 while (my $commit_text = <$fd>) {
3054                         if (!grep m/$searchtext/i, $commit_text) {
3055                                 next;
3056                         }
3057                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3058                                 next;
3059                         }
3060                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3061                                 next;
3062                         }
3063                         my @commit_lines = split "\n", $commit_text;
3064                         my %co = parse_commit(undef, \@commit_lines);
3065                         if (!%co) {
3066                                 next;
3067                         }
3068                         if ($alternate) {
3069                                 print "<tr class=\"dark\">\n";
3070                         } else {
3071                                 print "<tr class=\"light\">\n";
3072                         }
3073                         $alternate ^= 1;
3074                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3075                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3076                               "<td>" .
3077                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3078                                        esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3079                         my $comment = $co{'comment'};
3080                         foreach my $line (@$comment) {
3081                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3082                                         my $lead = esc_html($1) || "";
3083                                         $lead = chop_str($lead, 30, 10);
3084                                         my $match = esc_html($2) || "";
3085                                         my $trail = esc_html($3) || "";
3086                                         $trail = chop_str($trail, 30, 10);
3087                                         my $text = "$lead<span class=\"match\">$match</span>$trail";
3088                                         print chop_str($text, 80, 5) . "<br/>\n";
3089                                 }
3090                         }
3091                         print "</td>\n" .
3092                               "<td class=\"link\">" .
3093                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3094                               " | " .
3095                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3096                         print "</td>\n" .
3097                               "</tr>\n";
3098                 }
3099                 close $fd;
3100         }
3102         if ($pickaxe_search) {
3103                 $/ = "\n";
3104                 my $git_command = git_cmd_str();
3105                 open my $fd, "-|", "$git_command rev-list $hash | " .
3106                         "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3107                 undef %co;
3108                 my @files;
3109                 while (my $line = <$fd>) {
3110                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3111                                 my %set;
3112                                 $set{'file'} = $6;
3113                                 $set{'from_id'} = $3;
3114                                 $set{'to_id'} = $4;
3115                                 $set{'id'} = $set{'to_id'};
3116                                 if ($set{'id'} =~ m/0{40}/) {
3117                                         $set{'id'} = $set{'from_id'};
3118                                 }
3119                                 if ($set{'id'} =~ m/0{40}/) {
3120                                         next;
3121                                 }
3122                                 push @files, \%set;
3123                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3124                                 if (%co) {
3125                                         if ($alternate) {
3126                                                 print "<tr class=\"dark\">\n";
3127                                         } else {
3128                                                 print "<tr class=\"light\">\n";
3129                                         }
3130                                         $alternate ^= 1;
3131                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3132                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3133                                               "<td>" .
3134                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3135                                                       -class => "list subject"},
3136                                                       esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3137                                         while (my $setref = shift @files) {
3138                                                 my %set = %$setref;
3139                                                 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3140                                                                              hash=>$set{'id'}, file_name=>$set{'file'}),
3141                                                               -class => "list"},
3142                                                               "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3143                                                       "<br/>\n";
3144                                         }
3145                                         print "</td>\n" .
3146                                               "<td class=\"link\">" .
3147                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3148                                               " | " .
3149                                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3150                                         print "</td>\n" .
3151                                               "</tr>\n";
3152                                 }
3153                                 %co = parse_commit($1);
3154                         }
3155                 }
3156                 close $fd;
3157         }
3158         print "</table>\n";
3159         git_footer_html();
3162 sub git_shortlog {
3163         my $head = git_get_head_hash($project);
3164         if (!defined $hash) {
3165                 $hash = $head;
3166         }
3167         if (!defined $page) {
3168                 $page = 0;
3169         }
3170         my $refs = git_get_references();
3172         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3173         open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3174                 or die_error(undef, "Open git-rev-list failed");
3175         my @revlist = map { chomp; $_ } <$fd>;
3176         close $fd;
3178         my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3179         my $next_link = '';
3180         if ($#revlist >= (100 * ($page+1)-1)) {
3181                 $next_link =
3182                         $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3183                                  -title => "Alt-n"}, "next");
3184         }
3187         git_header_html();
3188         git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3189         git_print_header_div('summary', $project);
3191         git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3193         git_footer_html();
3196 ## ......................................................................
3197 ## feeds (RSS, OPML)
3199 sub git_rss {
3200         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3201         open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3202                 or die_error(undef, "Open git-rev-list failed");
3203         my @revlist = map { chomp; $_ } <$fd>;
3204         close $fd or die_error(undef, "Reading git-rev-list failed");
3205         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3206         print <<XML;
3207 <?xml version="1.0" encoding="utf-8"?>
3208 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3209 <channel>
3210 <title>$project $my_uri $my_url</title>
3211 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3212 <description>$project log</description>
3213 <language>en</language>
3214 XML
3216         for (my $i = 0; $i <= $#revlist; $i++) {
3217                 my $commit = $revlist[$i];
3218                 my %co = parse_commit($commit);
3219                 # we read 150, we always show 30 and the ones more recent than 48 hours
3220                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3221                         last;
3222                 }
3223                 my %cd = parse_date($co{'committer_epoch'});
3224                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3225                         $co{'parent'}, $co{'id'}
3226                         or next;
3227                 my @difftree = map { chomp; $_ } <$fd>;
3228                 close $fd
3229                         or next;
3230                 print "<item>\n" .
3231                       "<title>" .
3232                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3233                       "</title>\n" .
3234                       "<author>" . esc_html($co{'author'}) . "</author>\n" .
3235                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3236                       "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3237                       "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3238                       "<description>" . esc_html($co{'title'}) . "</description>\n" .
3239                       "<content:encoded>" .
3240                       "<![CDATA[\n";
3241                 my $comment = $co{'comment'};
3242                 foreach my $line (@$comment) {
3243                         $line = decode("utf8", $line, Encode::FB_DEFAULT);
3244                         print "$line<br/>\n";
3245                 }
3246                 print "<br/>\n";
3247                 foreach my $line (@difftree) {
3248                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3249                                 next;
3250                         }
3251                         my $file = validate_input(unquote($7));
3252                         $file = decode("utf8", $file, Encode::FB_DEFAULT);
3253                         print "$file<br/>\n";
3254                 }
3255                 print "]]>\n" .
3256                       "</content:encoded>\n" .
3257                       "</item>\n";
3258         }
3259         print "</channel></rss>";
3262 sub git_opml {
3263         my @list = git_get_projects_list();
3265         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3266         print <<XML;
3267 <?xml version="1.0" encoding="utf-8"?>
3268 <opml version="1.0">
3269 <head>
3270   <title>$site_name Git OPML Export</title>
3271 </head>
3272 <body>
3273 <outline text="git RSS feeds">
3274 XML
3276         foreach my $pr (@list) {
3277                 my %proj = %$pr;
3278                 my $head = git_get_head_hash($proj{'path'});
3279                 if (!defined $head) {
3280                         next;
3281                 }
3282                 $git_dir = "$projectroot/$proj{'path'}";
3283                 my %co = parse_commit($head);
3284                 if (!%co) {
3285                         next;
3286                 }
3288                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3289                 my $rss  = "$my_url?p=$proj{'path'};a=rss";
3290                 my $html = "$my_url?p=$proj{'path'};a=summary";
3291                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3292         }
3293         print <<XML;
3294 </outline>
3295 </body>
3296 </opml>
3297 XML