Code

gitweb: Add link to "project_index" view to "project_list" page
[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++";
51 # URI of GIT favicon, assumed to be image/png type
52 our $favicon = "++GITWEB_FAVICON++";
54 # source of projects list
55 our $projects_list = "++GITWEB_LIST++";
57 # list of git base URLs used for URL to where fetch project from,
58 # i.e. full URL is "$git_base_url/$project"
59 our @git_base_url_list = ("++GITWEB_BASE_URL++");
61 # default blob_plain mimetype and default charset for text/plain blob
62 our $default_blob_plain_mimetype = 'text/plain';
63 our $default_text_plain_charset  = undef;
65 # file to use for guessing MIME types before trying /etc/mime.types
66 # (relative to the current git repository)
67 our $mimetypes_file = undef;
69 # You define site-wide feature defaults here; override them with
70 # $GITWEB_CONFIG as necessary.
71 our %feature = (
72         # feature => {
73         #       'sub' => feature-sub (subroutine),
74         #       'override' => allow-override (boolean),
75         #       'default' => [ default options...] (array reference)}
76         #
77         # if feature is overridable (it means that allow-override has true value,
78         # then feature-sub will be called with default options as parameters;
79         # return value of feature-sub indicates if to enable specified feature
80         #
81         # use gitweb_check_feature(<feature>) to check if <feature> is enabled
83         'blame' => {
84                 'sub' => \&feature_blame,
85                 'override' => 0,
86                 'default' => [0]},
88         'snapshot' => {
89                 'sub' => \&feature_snapshot,
90                 'override' => 0,
91                 #         => [content-encoding, suffix, program]
92                 'default' => ['x-gzip', 'gz', 'gzip']},
94         'pickaxe' => {
95                 'sub' => \&feature_pickaxe,
96                 'override' => 0,
97                 'default' => [1]},
98 );
100 sub gitweb_check_feature {
101         my ($name) = @_;
102         return undef unless exists $feature{$name};
103         my ($sub, $override, @defaults) = (
104                 $feature{$name}{'sub'},
105                 $feature{$name}{'override'},
106                 @{$feature{$name}{'default'}});
107         if (!$override) { return @defaults; }
108         return $sub->(@defaults);
111 # To enable system wide have in $GITWEB_CONFIG
112 # $feature{'blame'}{'default'} = [1];
113 # To have project specific config enable override in $GITWEB_CONFIG
114 # $feature{'blame'}{'override'} = 1;
115 # and in project config gitweb.blame = 0|1;
117 sub feature_blame {
118         my ($val) = git_get_project_config('blame', '--bool');
120         if ($val eq 'true') {
121                 return 1;
122         } elsif ($val eq 'false') {
123                 return 0;
124         }
126         return $_[0];
129 # To disable system wide have in $GITWEB_CONFIG
130 # $feature{'snapshot'}{'default'} = [undef];
131 # To have project specific config enable override in $GITWEB_CONFIG
132 # $feature{'blame'}{'override'} = 1;
133 # and in project config  gitweb.snapshot = none|gzip|bzip2
135 sub feature_snapshot {
136         my ($ctype, $suffix, $command) = @_;
138         my ($val) = git_get_project_config('snapshot');
140         if ($val eq 'gzip') {
141                 return ('x-gzip', 'gz', 'gzip');
142         } elsif ($val eq 'bzip2') {
143                 return ('x-bzip2', 'bz2', 'bzip2');
144         } elsif ($val eq 'none') {
145                 return ();
146         }
148         return ($ctype, $suffix, $command);
151 # To enable system wide have in $GITWEB_CONFIG
152 # $feature{'pickaxe'}{'default'} = [1];
153 # To have project specific config enable override in $GITWEB_CONFIG
154 # $feature{'pickaxe'}{'override'} = 1;
155 # and in project config gitweb.pickaxe = 0|1;
157 sub feature_pickaxe {
158         my ($val) = git_get_project_config('pickaxe', '--bool');
160         if ($val eq 'true') {
161                 return (1);
162         } elsif ($val eq 'false') {
163                 return (0);
164         }
166         return ($_[0]);
169 # rename detection options for git-diff and git-diff-tree
170 # - default is '-M', with the cost proportional to
171 #   (number of removed files) * (number of new files).
172 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
173 #   (number of changed files + number of removed files) * (number of new files)
174 # - even more costly is '-C', '--find-copies-harder' with cost
175 #   (number of files in the original tree) * (number of new files)
176 # - one might want to include '-B' option, e.g. '-B', '-M'
177 our @diff_opts = ('-M'); # taken from git_commit
179 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
180 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
182 # version of the core git binary
183 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
185 # path to the current git repository
186 our $git_dir;
188 $projects_list ||= $projectroot;
190 # ======================================================================
191 # input validation and dispatch
192 our $action = $cgi->param('a');
193 if (defined $action) {
194         if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
195                 die_error(undef, "Invalid action parameter");
196         }
199 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
200 if (defined $project) {
201         $project =~ s|^/||;
202         $project =~ s|/$||;
203         $project = undef unless $project;
205 if (defined $project) {
206         if (!validate_input($project)) {
207                 die_error(undef, "Invalid project parameter");
208         }
209         if (!(-d "$projectroot/$project")) {
210                 die_error(undef, "No such directory");
211         }
212         if (!(-e "$projectroot/$project/HEAD")) {
213                 die_error(undef, "No such project");
214         }
215         $git_dir = "$projectroot/$project";
218 our $file_name = $cgi->param('f');
219 if (defined $file_name) {
220         if (!validate_input($file_name)) {
221                 die_error(undef, "Invalid file parameter");
222         }
225 our $file_parent = $cgi->param('fp');
226 if (defined $file_parent) {
227         if (!validate_input($file_parent)) {
228                 die_error(undef, "Invalid file parent parameter");
229         }
232 our $hash = $cgi->param('h');
233 if (defined $hash) {
234         if (!validate_input($hash)) {
235                 die_error(undef, "Invalid hash parameter");
236         }
239 our $hash_parent = $cgi->param('hp');
240 if (defined $hash_parent) {
241         if (!validate_input($hash_parent)) {
242                 die_error(undef, "Invalid hash parent parameter");
243         }
246 our $hash_base = $cgi->param('hb');
247 if (defined $hash_base) {
248         if (!validate_input($hash_base)) {
249                 die_error(undef, "Invalid hash base parameter");
250         }
253 our $hash_parent_base = $cgi->param('hpb');
254 if (defined $hash_parent_base) {
255         if (!validate_input($hash_parent_base)) {
256                 die_error(undef, "Invalid hash parent base parameter");
257         }
260 our $page = $cgi->param('pg');
261 if (defined $page) {
262         if ($page =~ m/[^0-9]$/) {
263                 die_error(undef, "Invalid page parameter");
264         }
267 our $searchtext = $cgi->param('s');
268 if (defined $searchtext) {
269         if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
270                 die_error(undef, "Invalid search parameter");
271         }
272         $searchtext = quotemeta $searchtext;
275 # dispatch
276 my %actions = (
277         "blame" => \&git_blame2,
278         "blobdiff" => \&git_blobdiff,
279         "blobdiff_plain" => \&git_blobdiff_plain,
280         "blob" => \&git_blob,
281         "blob_plain" => \&git_blob_plain,
282         "commitdiff" => \&git_commitdiff,
283         "commitdiff_plain" => \&git_commitdiff_plain,
284         "commit" => \&git_commit,
285         "heads" => \&git_heads,
286         "history" => \&git_history,
287         "log" => \&git_log,
288         "rss" => \&git_rss,
289         "search" => \&git_search,
290         "shortlog" => \&git_shortlog,
291         "summary" => \&git_summary,
292         "tag" => \&git_tag,
293         "tags" => \&git_tags,
294         "tree" => \&git_tree,
295         "snapshot" => \&git_snapshot,
296         # those below don't need $project
297         "opml" => \&git_opml,
298         "project_list" => \&git_project_list,
299         "project_index" => \&git_project_index,
300 );
302 if (defined $project) {
303         $action ||= 'summary';
304 } else {
305         $action ||= 'project_list';
307 if (!defined($actions{$action})) {
308         die_error(undef, "Unknown action");
310 $actions{$action}->();
311 exit;
313 ## ======================================================================
314 ## action links
316 sub href(%) {
317         my %params = @_;
319         my @mapping = (
320                 project => "p",
321                 action => "a",
322                 file_name => "f",
323                 file_parent => "fp",
324                 hash => "h",
325                 hash_parent => "hp",
326                 hash_base => "hb",
327                 hash_parent_base => "hpb",
328                 page => "pg",
329                 order => "o",
330                 searchtext => "s",
331         );
332         my %mapping = @mapping;
334         $params{'project'} = $project unless exists $params{'project'};
336         my @result = ();
337         for (my $i = 0; $i < @mapping; $i += 2) {
338                 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
339                 if (defined $params{$name}) {
340                         push @result, $symbol . "=" . esc_param($params{$name});
341                 }
342         }
343         return "$my_uri?" . join(';', @result);
347 ## ======================================================================
348 ## validation, quoting/unquoting and escaping
350 sub validate_input {
351         my $input = shift;
353         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
354                 return $input;
355         }
356         if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
357                 return undef;
358         }
359         if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
360                 return undef;
361         }
362         return $input;
365 # quote unsafe chars, but keep the slash, even when it's not
366 # correct, but quoted slashes look too horrible in bookmarks
367 sub esc_param {
368         my $str = shift;
369         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
370         $str =~ s/\+/%2B/g;
371         $str =~ s/ /\+/g;
372         return $str;
375 # replace invalid utf8 character with SUBSTITUTION sequence
376 sub esc_html {
377         my $str = shift;
378         $str = decode("utf8", $str, Encode::FB_DEFAULT);
379         $str = escapeHTML($str);
380         $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
381         return $str;
384 # git may return quoted and escaped filenames
385 sub unquote {
386         my $str = shift;
387         if ($str =~ m/^"(.*)"$/) {
388                 $str = $1;
389                 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
390         }
391         return $str;
394 # escape tabs (convert tabs to spaces)
395 sub untabify {
396         my $line = shift;
398         while ((my $pos = index($line, "\t")) != -1) {
399                 if (my $count = (8 - ($pos % 8))) {
400                         my $spaces = ' ' x $count;
401                         $line =~ s/\t/$spaces/;
402                 }
403         }
405         return $line;
408 ## ----------------------------------------------------------------------
409 ## HTML aware string manipulation
411 sub chop_str {
412         my $str = shift;
413         my $len = shift;
414         my $add_len = shift || 10;
416         # allow only $len chars, but don't cut a word if it would fit in $add_len
417         # if it doesn't fit, cut it if it's still longer than the dots we would add
418         $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
419         my $body = $1;
420         my $tail = $2;
421         if (length($tail) > 4) {
422                 $tail = " ...";
423                 $body =~ s/&[^;]*$//; # remove chopped character entities
424         }
425         return "$body$tail";
428 ## ----------------------------------------------------------------------
429 ## functions returning short strings
431 # CSS class for given age value (in seconds)
432 sub age_class {
433         my $age = shift;
435         if ($age < 60*60*2) {
436                 return "age0";
437         } elsif ($age < 60*60*24*2) {
438                 return "age1";
439         } else {
440                 return "age2";
441         }
444 # convert age in seconds to "nn units ago" string
445 sub age_string {
446         my $age = shift;
447         my $age_str;
449         if ($age > 60*60*24*365*2) {
450                 $age_str = (int $age/60/60/24/365);
451                 $age_str .= " years ago";
452         } elsif ($age > 60*60*24*(365/12)*2) {
453                 $age_str = int $age/60/60/24/(365/12);
454                 $age_str .= " months ago";
455         } elsif ($age > 60*60*24*7*2) {
456                 $age_str = int $age/60/60/24/7;
457                 $age_str .= " weeks ago";
458         } elsif ($age > 60*60*24*2) {
459                 $age_str = int $age/60/60/24;
460                 $age_str .= " days ago";
461         } elsif ($age > 60*60*2) {
462                 $age_str = int $age/60/60;
463                 $age_str .= " hours ago";
464         } elsif ($age > 60*2) {
465                 $age_str = int $age/60;
466                 $age_str .= " min ago";
467         } elsif ($age > 2) {
468                 $age_str = int $age;
469                 $age_str .= " sec ago";
470         } else {
471                 $age_str .= " right now";
472         }
473         return $age_str;
476 # convert file mode in octal to symbolic file mode string
477 sub mode_str {
478         my $mode = oct shift;
480         if (S_ISDIR($mode & S_IFMT)) {
481                 return 'drwxr-xr-x';
482         } elsif (S_ISLNK($mode)) {
483                 return 'lrwxrwxrwx';
484         } elsif (S_ISREG($mode)) {
485                 # git cares only about the executable bit
486                 if ($mode & S_IXUSR) {
487                         return '-rwxr-xr-x';
488                 } else {
489                         return '-rw-r--r--';
490                 };
491         } else {
492                 return '----------';
493         }
496 # convert file mode in octal to file type string
497 sub file_type {
498         my $mode = shift;
500         if ($mode !~ m/^[0-7]+$/) {
501                 return $mode;
502         } else {
503                 $mode = oct $mode;
504         }
506         if (S_ISDIR($mode & S_IFMT)) {
507                 return "directory";
508         } elsif (S_ISLNK($mode)) {
509                 return "symlink";
510         } elsif (S_ISREG($mode)) {
511                 return "file";
512         } else {
513                 return "unknown";
514         }
517 ## ----------------------------------------------------------------------
518 ## functions returning short HTML fragments, or transforming HTML fragments
519 ## which don't beling to other sections
521 # format line of commit message or tag comment
522 sub format_log_line_html {
523         my $line = shift;
525         $line = esc_html($line);
526         $line =~ s/ /&nbsp;/g;
527         if ($line =~ m/([0-9a-fA-F]{40})/) {
528                 my $hash_text = $1;
529                 if (git_get_type($hash_text) eq "commit") {
530                         my $link =
531                                 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
532                                         -class => "text"}, $hash_text);
533                         $line =~ s/$hash_text/$link/;
534                 }
535         }
536         return $line;
539 # format marker of refs pointing to given object
540 sub format_ref_marker {
541         my ($refs, $id) = @_;
542         my $markers = '';
544         if (defined $refs->{$id}) {
545                 foreach my $ref (@{$refs->{$id}}) {
546                         my ($type, $name) = qw();
547                         # e.g. tags/v2.6.11 or heads/next
548                         if ($ref =~ m!^(.*?)s?/(.*)$!) {
549                                 $type = $1;
550                                 $name = $2;
551                         } else {
552                                 $type = "ref";
553                                 $name = $ref;
554                         }
556                         $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
557                 }
558         }
560         if ($markers) {
561                 return ' <span class="refs">'. $markers . '</span>';
562         } else {
563                 return "";
564         }
567 # format, perhaps shortened and with markers, title line
568 sub format_subject_html {
569         my ($long, $short, $href, $extra) = @_;
570         $extra = '' unless defined($extra);
572         if (length($short) < length($long)) {
573                 return $cgi->a({-href => $href, -class => "list subject",
574                                 -title => $long},
575                        esc_html($short) . $extra);
576         } else {
577                 return $cgi->a({-href => $href, -class => "list subject"},
578                        esc_html($long)  . $extra);
579         }
582 sub format_diff_line {
583         my $line = shift;
584         my $char = substr($line, 0, 1);
585         my $diff_class = "";
587         chomp $line;
589         if ($char eq '+') {
590                 $diff_class = " add";
591         } elsif ($char eq "-") {
592                 $diff_class = " rem";
593         } elsif ($char eq "@") {
594                 $diff_class = " chunk_header";
595         } elsif ($char eq "\\") {
596                 $diff_class = " incomplete";
597         }
598         $line = untabify($line);
599         return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
602 ## ----------------------------------------------------------------------
603 ## git utility subroutines, invoking git commands
605 # returns path to the core git executable and the --git-dir parameter as list
606 sub git_cmd {
607         return $GIT, '--git-dir='.$git_dir;
610 # returns path to the core git executable and the --git-dir parameter as string
611 sub git_cmd_str {
612         return join(' ', git_cmd());
615 # get HEAD ref of given project as hash
616 sub git_get_head_hash {
617         my $project = shift;
618         my $o_git_dir = $git_dir;
619         my $retval = undef;
620         $git_dir = "$projectroot/$project";
621         if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
622                 my $head = <$fd>;
623                 close $fd;
624                 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
625                         $retval = $1;
626                 }
627         }
628         if (defined $o_git_dir) {
629                 $git_dir = $o_git_dir;
630         }
631         return $retval;
634 # get type of given object
635 sub git_get_type {
636         my $hash = shift;
638         open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
639         my $type = <$fd>;
640         close $fd or return;
641         chomp $type;
642         return $type;
645 sub git_get_project_config {
646         my ($key, $type) = @_;
648         return unless ($key);
649         $key =~ s/^gitweb\.//;
650         return if ($key =~ m/\W/);
652         my @x = (git_cmd(), 'repo-config');
653         if (defined $type) { push @x, $type; }
654         push @x, "--get";
655         push @x, "gitweb.$key";
656         my $val = qx(@x);
657         chomp $val;
658         return ($val);
661 # get hash of given path at given ref
662 sub git_get_hash_by_path {
663         my $base = shift;
664         my $path = shift || return undef;
666         my $tree = $base;
668         open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
669                 or die_error(undef, "Open git-ls-tree failed");
670         my $line = <$fd>;
671         close $fd or return undef;
673         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
674         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
675         return $3;
678 ## ......................................................................
679 ## git utility functions, directly accessing git repository
681 sub git_get_project_description {
682         my $path = shift;
684         open my $fd, "$projectroot/$path/description" or return undef;
685         my $descr = <$fd>;
686         close $fd;
687         chomp $descr;
688         return $descr;
691 sub git_get_project_url_list {
692         my $path = shift;
694         open my $fd, "$projectroot/$path/cloneurl" or return undef;
695         my @git_project_url_list = map { chomp; $_ } <$fd>;
696         close $fd;
698         return wantarray ? @git_project_url_list : \@git_project_url_list;
701 sub git_get_projects_list {
702         my @list;
704         if (-d $projects_list) {
705                 # search in directory
706                 my $dir = $projects_list;
707                 my $pfxlen = length("$dir");
709                 File::Find::find({
710                         follow_fast => 1, # follow symbolic links
711                         dangling_symlinks => 0, # ignore dangling symlinks, silently
712                         wanted => sub {
713                                 # skip project-list toplevel, if we get it.
714                                 return if (m!^[/.]$!);
715                                 # only directories can be git repositories
716                                 return unless (-d $_);
718                                 my $subdir = substr($File::Find::name, $pfxlen + 1);
719                                 # we check related file in $projectroot
720                                 if (-e "$projectroot/$subdir/HEAD") {
721                                         push @list, { path => $subdir };
722                                         $File::Find::prune = 1;
723                                 }
724                         },
725                 }, "$dir");
727         } elsif (-f $projects_list) {
728                 # read from file(url-encoded):
729                 # 'git%2Fgit.git Linus+Torvalds'
730                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
731                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
732                 open my ($fd), $projects_list or return undef;
733                 while (my $line = <$fd>) {
734                         chomp $line;
735                         my ($path, $owner) = split ' ', $line;
736                         $path = unescape($path);
737                         $owner = unescape($owner);
738                         if (!defined $path) {
739                                 next;
740                         }
741                         if (-e "$projectroot/$path/HEAD") {
742                                 my $pr = {
743                                         path => $path,
744                                         owner => decode("utf8", $owner, Encode::FB_DEFAULT),
745                                 };
746                                 push @list, $pr
747                         }
748                 }
749                 close $fd;
750         }
751         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
752         return @list;
755 sub git_get_project_owner {
756         my $project = shift;
757         my $owner;
759         return undef unless $project;
761         # read from file (url-encoded):
762         # 'git%2Fgit.git Linus+Torvalds'
763         # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
764         # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
765         if (-f $projects_list) {
766                 open (my $fd , $projects_list);
767                 while (my $line = <$fd>) {
768                         chomp $line;
769                         my ($pr, $ow) = split ' ', $line;
770                         $pr = unescape($pr);
771                         $ow = unescape($ow);
772                         if ($pr eq $project) {
773                                 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
774                                 last;
775                         }
776                 }
777                 close $fd;
778         }
779         if (!defined $owner) {
780                 $owner = get_file_owner("$projectroot/$project");
781         }
783         return $owner;
786 sub git_get_references {
787         my $type = shift || "";
788         my %refs;
789         my $fd;
790         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
791         # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
792         if (-f "$projectroot/$project/info/refs") {
793                 open $fd, "$projectroot/$project/info/refs"
794                         or return;
795         } else {
796                 open $fd, "-|", git_cmd(), "ls-remote", "."
797                         or return;
798         }
800         while (my $line = <$fd>) {
801                 chomp $line;
802                 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
803                         if (defined $refs{$1}) {
804                                 push @{$refs{$1}}, $2;
805                         } else {
806                                 $refs{$1} = [ $2 ];
807                         }
808                 }
809         }
810         close $fd or return;
811         return \%refs;
814 sub git_get_rev_name_tags {
815         my $hash = shift || return undef;
817         open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
818                 or return;
819         my $name_rev = <$fd>;
820         close $fd;
822         if ($name_rev =~ m|^$hash tags/(.*)$|) {
823                 return $1;
824         } else {
825                 # catches also '$hash undefined' output
826                 return undef;
827         }
830 ## ----------------------------------------------------------------------
831 ## parse to hash functions
833 sub parse_date {
834         my $epoch = shift;
835         my $tz = shift || "-0000";
837         my %date;
838         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
839         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
840         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
841         $date{'hour'} = $hour;
842         $date{'minute'} = $min;
843         $date{'mday'} = $mday;
844         $date{'day'} = $days[$wday];
845         $date{'month'} = $months[$mon];
846         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
847                            $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
848         $date{'mday-time'} = sprintf "%d %s %02d:%02d",
849                              $mday, $months[$mon], $hour ,$min;
851         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
852         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
853         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
854         $date{'hour_local'} = $hour;
855         $date{'minute_local'} = $min;
856         $date{'tz_local'} = $tz;
857         return %date;
860 sub parse_tag {
861         my $tag_id = shift;
862         my %tag;
863         my @comment;
865         open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
866         $tag{'id'} = $tag_id;
867         while (my $line = <$fd>) {
868                 chomp $line;
869                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
870                         $tag{'object'} = $1;
871                 } elsif ($line =~ m/^type (.+)$/) {
872                         $tag{'type'} = $1;
873                 } elsif ($line =~ m/^tag (.+)$/) {
874                         $tag{'name'} = $1;
875                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
876                         $tag{'author'} = $1;
877                         $tag{'epoch'} = $2;
878                         $tag{'tz'} = $3;
879                 } elsif ($line =~ m/--BEGIN/) {
880                         push @comment, $line;
881                         last;
882                 } elsif ($line eq "") {
883                         last;
884                 }
885         }
886         push @comment, <$fd>;
887         $tag{'comment'} = \@comment;
888         close $fd or return;
889         if (!defined $tag{'name'}) {
890                 return
891         };
892         return %tag
895 sub parse_commit {
896         my $commit_id = shift;
897         my $commit_text = shift;
899         my @commit_lines;
900         my %co;
902         if (defined $commit_text) {
903                 @commit_lines = @$commit_text;
904         } else {
905                 $/ = "\0";
906                 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
907                         or return;
908                 @commit_lines = split '\n', <$fd>;
909                 close $fd or return;
910                 $/ = "\n";
911                 pop @commit_lines;
912         }
913         my $header = shift @commit_lines;
914         if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
915                 return;
916         }
917         ($co{'id'}, my @parents) = split ' ', $header;
918         $co{'parents'} = \@parents;
919         $co{'parent'} = $parents[0];
920         while (my $line = shift @commit_lines) {
921                 last if $line eq "\n";
922                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
923                         $co{'tree'} = $1;
924                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
925                         $co{'author'} = $1;
926                         $co{'author_epoch'} = $2;
927                         $co{'author_tz'} = $3;
928                         if ($co{'author'} =~ m/^([^<]+) </) {
929                                 $co{'author_name'} = $1;
930                         } else {
931                                 $co{'author_name'} = $co{'author'};
932                         }
933                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
934                         $co{'committer'} = $1;
935                         $co{'committer_epoch'} = $2;
936                         $co{'committer_tz'} = $3;
937                         $co{'committer_name'} = $co{'committer'};
938                         $co{'committer_name'} =~ s/ <.*//;
939                 }
940         }
941         if (!defined $co{'tree'}) {
942                 return;
943         };
945         foreach my $title (@commit_lines) {
946                 $title =~ s/^    //;
947                 if ($title ne "") {
948                         $co{'title'} = chop_str($title, 80, 5);
949                         # remove leading stuff of merges to make the interesting part visible
950                         if (length($title) > 50) {
951                                 $title =~ s/^Automatic //;
952                                 $title =~ s/^merge (of|with) /Merge ... /i;
953                                 if (length($title) > 50) {
954                                         $title =~ s/(http|rsync):\/\///;
955                                 }
956                                 if (length($title) > 50) {
957                                         $title =~ s/(master|www|rsync)\.//;
958                                 }
959                                 if (length($title) > 50) {
960                                         $title =~ s/kernel.org:?//;
961                                 }
962                                 if (length($title) > 50) {
963                                         $title =~ s/\/pub\/scm//;
964                                 }
965                         }
966                         $co{'title_short'} = chop_str($title, 50, 5);
967                         last;
968                 }
969         }
970         # remove added spaces
971         foreach my $line (@commit_lines) {
972                 $line =~ s/^    //;
973         }
974         $co{'comment'} = \@commit_lines;
976         my $age = time - $co{'committer_epoch'};
977         $co{'age'} = $age;
978         $co{'age_string'} = age_string($age);
979         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
980         if ($age > 60*60*24*7*2) {
981                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
982                 $co{'age_string_age'} = $co{'age_string'};
983         } else {
984                 $co{'age_string_date'} = $co{'age_string'};
985                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
986         }
987         return %co;
990 # parse ref from ref_file, given by ref_id, with given type
991 sub parse_ref {
992         my $ref_file = shift;
993         my $ref_id = shift;
994         my $type = shift || git_get_type($ref_id);
995         my %ref_item;
997         $ref_item{'type'} = $type;
998         $ref_item{'id'} = $ref_id;
999         $ref_item{'epoch'} = 0;
1000         $ref_item{'age'} = "unknown";
1001         if ($type eq "tag") {
1002                 my %tag = parse_tag($ref_id);
1003                 $ref_item{'comment'} = $tag{'comment'};
1004                 if ($tag{'type'} eq "commit") {
1005                         my %co = parse_commit($tag{'object'});
1006                         $ref_item{'epoch'} = $co{'committer_epoch'};
1007                         $ref_item{'age'} = $co{'age_string'};
1008                 } elsif (defined($tag{'epoch'})) {
1009                         my $age = time - $tag{'epoch'};
1010                         $ref_item{'epoch'} = $tag{'epoch'};
1011                         $ref_item{'age'} = age_string($age);
1012                 }
1013                 $ref_item{'reftype'} = $tag{'type'};
1014                 $ref_item{'name'} = $tag{'name'};
1015                 $ref_item{'refid'} = $tag{'object'};
1016         } elsif ($type eq "commit"){
1017                 my %co = parse_commit($ref_id);
1018                 $ref_item{'reftype'} = "commit";
1019                 $ref_item{'name'} = $ref_file;
1020                 $ref_item{'title'} = $co{'title'};
1021                 $ref_item{'refid'} = $ref_id;
1022                 $ref_item{'epoch'} = $co{'committer_epoch'};
1023                 $ref_item{'age'} = $co{'age_string'};
1024         } else {
1025                 $ref_item{'reftype'} = $type;
1026                 $ref_item{'name'} = $ref_file;
1027                 $ref_item{'refid'} = $ref_id;
1028         }
1030         return %ref_item;
1033 # parse line of git-diff-tree "raw" output
1034 sub parse_difftree_raw_line {
1035         my $line = shift;
1036         my %res;
1038         # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
1039         # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
1040         if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1041                 $res{'from_mode'} = $1;
1042                 $res{'to_mode'} = $2;
1043                 $res{'from_id'} = $3;
1044                 $res{'to_id'} = $4;
1045                 $res{'status'} = $5;
1046                 $res{'similarity'} = $6;
1047                 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1048                         ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1049                 } else {
1050                         $res{'file'} = unquote($7);
1051                 }
1052         }
1053         # 'c512b523472485aef4fff9e57b229d9d243c967f'
1054         elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1055                 $res{'commit'} = $1;
1056         }
1058         return wantarray ? %res : \%res;
1061 # parse line of git-ls-tree output
1062 sub parse_ls_tree_line ($;%) {
1063         my $line = shift;
1064         my %opts = @_;
1065         my %res;
1067         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
1068         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1070         $res{'mode'} = $1;
1071         $res{'type'} = $2;
1072         $res{'hash'} = $3;
1073         if ($opts{'-z'}) {
1074                 $res{'name'} = $4;
1075         } else {
1076                 $res{'name'} = unquote($4);
1077         }
1079         return wantarray ? %res : \%res;
1082 ## ......................................................................
1083 ## parse to array of hashes functions
1085 sub git_get_refs_list {
1086         my $ref_dir = shift;
1087         my @reflist;
1089         my @refs;
1090         open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1091                 or return;
1092         while (my $line = <$fd>) {
1093                 chomp $line;
1094                 if ($line =~ m/^([0-9a-fA-F]{40})\t$ref_dir\/?([^\^]+)$/) {
1095                         push @refs, { hash => $1, name => $2 };
1096                 } elsif ($line =~ m/^[0-9a-fA-F]{40}\t$ref_dir\/?(.*)\^\{\}$/ &&
1097                          $1 eq $refs[-1]{'name'}) {
1098                         # most likely a tag is followed by its peeled
1099                         # (deref) one, and when that happens we know the
1100                         # previous one was of type 'tag'.
1101                         $refs[-1]{'type'} = "tag";
1102                 }
1103         }
1104         close $fd;
1106         foreach my $ref (@refs) {
1107                 my $ref_file = $ref->{'name'};
1108                 my $ref_id   = $ref->{'hash'};
1110                 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1111                 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1113                 push @reflist, \%ref_item;
1114         }
1115         # sort refs by age
1116         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1117         return \@reflist;
1120 ## ----------------------------------------------------------------------
1121 ## filesystem-related functions
1123 sub get_file_owner {
1124         my $path = shift;
1126         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1127         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1128         if (!defined $gcos) {
1129                 return undef;
1130         }
1131         my $owner = $gcos;
1132         $owner =~ s/[,;].*$//;
1133         return decode("utf8", $owner, Encode::FB_DEFAULT);
1136 ## ......................................................................
1137 ## mimetype related functions
1139 sub mimetype_guess_file {
1140         my $filename = shift;
1141         my $mimemap = shift;
1142         -r $mimemap or return undef;
1144         my %mimemap;
1145         open(MIME, $mimemap) or return undef;
1146         while (<MIME>) {
1147                 next if m/^#/; # skip comments
1148                 my ($mime, $exts) = split(/\t+/);
1149                 if (defined $exts) {
1150                         my @exts = split(/\s+/, $exts);
1151                         foreach my $ext (@exts) {
1152                                 $mimemap{$ext} = $mime;
1153                         }
1154                 }
1155         }
1156         close(MIME);
1158         $filename =~ /\.(.*?)$/;
1159         return $mimemap{$1};
1162 sub mimetype_guess {
1163         my $filename = shift;
1164         my $mime;
1165         $filename =~ /\./ or return undef;
1167         if ($mimetypes_file) {
1168                 my $file = $mimetypes_file;
1169                 if ($file !~ m!^/!) { # if it is relative path
1170                         # it is relative to project
1171                         $file = "$projectroot/$project/$file";
1172                 }
1173                 $mime = mimetype_guess_file($filename, $file);
1174         }
1175         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1176         return $mime;
1179 sub blob_mimetype {
1180         my $fd = shift;
1181         my $filename = shift;
1183         if ($filename) {
1184                 my $mime = mimetype_guess($filename);
1185                 $mime and return $mime;
1186         }
1188         # just in case
1189         return $default_blob_plain_mimetype unless $fd;
1191         if (-T $fd) {
1192                 return 'text/plain' .
1193                        ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1194         } elsif (! $filename) {
1195                 return 'application/octet-stream';
1196         } elsif ($filename =~ m/\.png$/i) {
1197                 return 'image/png';
1198         } elsif ($filename =~ m/\.gif$/i) {
1199                 return 'image/gif';
1200         } elsif ($filename =~ m/\.jpe?g$/i) {
1201                 return 'image/jpeg';
1202         } else {
1203                 return 'application/octet-stream';
1204         }
1207 ## ======================================================================
1208 ## functions printing HTML: header, footer, error page
1210 sub git_header_html {
1211         my $status = shift || "200 OK";
1212         my $expires = shift;
1214         my $title = "$site_name git";
1215         if (defined $project) {
1216                 $title .= " - $project";
1217                 if (defined $action) {
1218                         $title .= "/$action";
1219                         if (defined $file_name) {
1220                                 $title .= " - $file_name";
1221                                 if ($action eq "tree" && $file_name !~ m|/$|) {
1222                                         $title .= "/";
1223                                 }
1224                         }
1225                 }
1226         }
1227         my $content_type;
1228         # require explicit support from the UA if we are to send the page as
1229         # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1230         # we have to do this because MSIE sometimes globs '*/*', pretending to
1231         # support xhtml+xml but choking when it gets what it asked for.
1232         if (defined $cgi->http('HTTP_ACCEPT') &&
1233             $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1234             $cgi->Accept('application/xhtml+xml') != 0) {
1235                 $content_type = 'application/xhtml+xml';
1236         } else {
1237                 $content_type = 'text/html';
1238         }
1239         print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1240                            -status=> $status, -expires => $expires);
1241         print <<EOF;
1242 <?xml version="1.0" encoding="utf-8"?>
1243 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1244 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1245 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1246 <!-- git core binaries version $git_version -->
1247 <head>
1248 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1249 <meta name="generator" content="gitweb/$version git/$git_version"/>
1250 <meta name="robots" content="index, nofollow"/>
1251 <title>$title</title>
1252 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1253 EOF
1254         if (defined $project) {
1255                 printf('<link rel="alternate" title="%s log" '.
1256                        'href="%s" type="application/rss+xml"/>'."\n",
1257                        esc_param($project), href(action=>"rss"));
1258         } else {
1259                 printf('<link rel="alternate" title="%s projects list" '.
1260                        'href="%s" type="text/plain; charset=utf-8"/>'."\n",
1261                        $site_name, href(project=>undef, action=>"project_index"));
1262                 printf('<link rel="alternate" title="%s projects logs" '.
1263                        'href="%s" type="text/x-opml"/>'."\n",
1264                        $site_name, href(project=>undef, action=>"opml"));
1265         }
1266         if (defined $favicon) {
1267                 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1268         }
1270         print "</head>\n" .
1271               "<body>\n" .
1272               "<div class=\"page_header\">\n" .
1273               "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1274               "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1275               "</a>\n";
1276         print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1277         if (defined $project) {
1278                 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1279                 if (defined $action) {
1280                         print " / $action";
1281                 }
1282                 print "\n";
1283                 if (!defined $searchtext) {
1284                         $searchtext = "";
1285                 }
1286                 my $search_hash;
1287                 if (defined $hash_base) {
1288                         $search_hash = $hash_base;
1289                 } elsif (defined $hash) {
1290                         $search_hash = $hash;
1291                 } else {
1292                         $search_hash = "HEAD";
1293                 }
1294                 $cgi->param("a", "search");
1295                 $cgi->param("h", $search_hash);
1296                 print $cgi->startform(-method => "get", -action => $my_uri) .
1297                       "<div class=\"search\">\n" .
1298                       $cgi->hidden(-name => "p") . "\n" .
1299                       $cgi->hidden(-name => "a") . "\n" .
1300                       $cgi->hidden(-name => "h") . "\n" .
1301                       $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1302                       "</div>" .
1303                       $cgi->end_form() . "\n";
1304         }
1305         print "</div>\n";
1308 sub git_footer_html {
1309         print "<div class=\"page_footer\">\n";
1310         if (defined $project) {
1311                 my $descr = git_get_project_description($project);
1312                 if (defined $descr) {
1313                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1314                 }
1315                 print $cgi->a({-href => href(action=>"rss"),
1316                               -class => "rss_logo"}, "RSS") . "\n";
1317         } else {
1318                 print $cgi->a({-href => href(project=>undef, action=>"opml"),
1319                               -class => "rss_logo"}, "OPML") . " ";
1320                 print $cgi->a({-href => href(project=>undef, action=>"project_index"),
1321                               -class => "rss_logo"}, "TXT") . "\n";
1322         }
1323         print "</div>\n" .
1324               "</body>\n" .
1325               "</html>";
1328 sub die_error {
1329         my $status = shift || "403 Forbidden";
1330         my $error = shift || "Malformed query, file missing or permission denied";
1332         git_header_html($status);
1333         print <<EOF;
1334 <div class="page_body">
1335 <br /><br />
1336 $status - $error
1337 <br />
1338 </div>
1339 EOF
1340         git_footer_html();
1341         exit;
1344 ## ----------------------------------------------------------------------
1345 ## functions printing or outputting HTML: navigation
1347 sub git_print_page_nav {
1348         my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1349         $extra = '' if !defined $extra; # pager or formats
1351         my @navs = qw(summary shortlog log commit commitdiff tree);
1352         if ($suppress) {
1353                 @navs = grep { $_ ne $suppress } @navs;
1354         }
1356         my %arg = map { $_ => {action=>$_} } @navs;
1357         if (defined $head) {
1358                 for (qw(commit commitdiff)) {
1359                         $arg{$_}{hash} = $head;
1360                 }
1361                 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1362                         for (qw(shortlog log)) {
1363                                 $arg{$_}{hash} = $head;
1364                         }
1365                 }
1366         }
1367         $arg{tree}{hash} = $treehead if defined $treehead;
1368         $arg{tree}{hash_base} = $treebase if defined $treebase;
1370         print "<div class=\"page_nav\">\n" .
1371                 (join " | ",
1372                  map { $_ eq $current ?
1373                        $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1374                  } @navs);
1375         print "<br/>\n$extra<br/>\n" .
1376               "</div>\n";
1379 sub format_paging_nav {
1380         my ($action, $hash, $head, $page, $nrevs) = @_;
1381         my $paging_nav;
1384         if ($hash ne $head || $page) {
1385                 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1386         } else {
1387                 $paging_nav .= "HEAD";
1388         }
1390         if ($page > 0) {
1391                 $paging_nav .= " &sdot; " .
1392                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1393                                  -accesskey => "p", -title => "Alt-p"}, "prev");
1394         } else {
1395                 $paging_nav .= " &sdot; prev";
1396         }
1398         if ($nrevs >= (100 * ($page+1)-1)) {
1399                 $paging_nav .= " &sdot; " .
1400                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1401                                  -accesskey => "n", -title => "Alt-n"}, "next");
1402         } else {
1403                 $paging_nav .= " &sdot; next";
1404         }
1406         return $paging_nav;
1409 ## ......................................................................
1410 ## functions printing or outputting HTML: div
1412 sub git_print_header_div {
1413         my ($action, $title, $hash, $hash_base) = @_;
1414         my %args = ();
1416         $args{action} = $action;
1417         $args{hash} = $hash if $hash;
1418         $args{hash_base} = $hash_base if $hash_base;
1420         print "<div class=\"header\">\n" .
1421               $cgi->a({-href => href(%args), -class => "title"},
1422               $title ? $title : $action) .
1423               "\n</div>\n";
1426 #sub git_print_authorship (\%) {
1427 sub git_print_authorship {
1428         my $co = shift;
1430         my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1431         print "<div class=\"author_date\">" .
1432               esc_html($co->{'author_name'}) .
1433               " [$ad{'rfc2822'}";
1434         if ($ad{'hour_local'} < 6) {
1435                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1436                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1437         } else {
1438                 printf(" (%02d:%02d %s)",
1439                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1440         }
1441         print "]</div>\n";
1444 sub git_print_page_path {
1445         my $name = shift;
1446         my $type = shift;
1447         my $hb = shift;
1449         if (!defined $name) {
1450                 print "<div class=\"page_path\">/</div>\n";
1451         } else {
1452                 my @dirname = split '/', $name;
1453                 my $basename = pop @dirname;
1454                 my $fullname = '';
1456                 print "<div class=\"page_path\">";
1457                 foreach my $dir (@dirname) {
1458                         $fullname .= $dir . '/';
1459                         print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1460                                                      hash_base=>$hb),
1461                                       -title => $fullname}, esc_html($dir));
1462                         print "/";
1463                 }
1464                 if (defined $type && $type eq 'blob') {
1465                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1466                                                      hash_base=>$hb),
1467                                       -title => $name}, esc_html($basename));
1468                 } elsif (defined $type && $type eq 'tree') {
1469                         print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1470                                                      hash_base=>$hb),
1471                                       -title => $name}, esc_html($basename));
1472                         print "/";
1473                 } else {
1474                         print esc_html($basename);
1475                 }
1476                 print "<br/></div>\n";
1477         }
1480 # sub git_print_log (\@;%) {
1481 sub git_print_log ($;%) {
1482         my $log = shift;
1483         my %opts = @_;
1485         if ($opts{'-remove_title'}) {
1486                 # remove title, i.e. first line of log
1487                 shift @$log;
1488         }
1489         # remove leading empty lines
1490         while (defined $log->[0] && $log->[0] eq "") {
1491                 shift @$log;
1492         }
1494         # print log
1495         my $signoff = 0;
1496         my $empty = 0;
1497         foreach my $line (@$log) {
1498                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1499                         $signoff = 1;
1500                         $empty = 0;
1501                         if (! $opts{'-remove_signoff'}) {
1502                                 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1503                                 next;
1504                         } else {
1505                                 # remove signoff lines
1506                                 next;
1507                         }
1508                 } else {
1509                         $signoff = 0;
1510                 }
1512                 # print only one empty line
1513                 # do not print empty line after signoff
1514                 if ($line eq "") {
1515                         next if ($empty || $signoff);
1516                         $empty = 1;
1517                 } else {
1518                         $empty = 0;
1519                 }
1521                 print format_log_line_html($line) . "<br/>\n";
1522         }
1524         if ($opts{'-final_empty_line'}) {
1525                 # end with single empty line
1526                 print "<br/>\n" unless $empty;
1527         }
1530 sub git_print_simplified_log {
1531         my $log = shift;
1532         my $remove_title = shift;
1534         git_print_log($log,
1535                 -final_empty_line=> 1,
1536                 -remove_title => $remove_title);
1539 # print tree entry (row of git_tree), but without encompassing <tr> element
1540 sub git_print_tree_entry {
1541         my ($t, $basedir, $hash_base, $have_blame) = @_;
1543         my %base_key = ();
1544         $base_key{hash_base} = $hash_base if defined $hash_base;
1546         print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1547         if ($t->{'type'} eq "blob") {
1548                 print "<td class=\"list\">" .
1549                       $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1550                                              file_name=>"$basedir$t->{'name'}", %base_key),
1551                               -class => "list"}, esc_html($t->{'name'})) .
1552                       "</td>\n" .
1553                       "<td class=\"link\">" .
1554                       $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1555                                              file_name=>"$basedir$t->{'name'}", %base_key)},
1556                               "blob");
1557                 if ($have_blame) {
1558                         print " | " .
1559                                 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1560                                                        file_name=>"$basedir$t->{'name'}", %base_key)},
1561                                         "blame");
1562                 }
1563                 if (defined $hash_base) {
1564                         print " | " .
1565                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1566                                                      hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1567                                       "history");
1568                 }
1569                 print " | " .
1570                       $cgi->a({-href => href(action=>"blob_plain",
1571                                              hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1572                               "raw") .
1573                       "</td>\n";
1575         } elsif ($t->{'type'} eq "tree") {
1576                 print "<td class=\"list\">" .
1577                       $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1578                                              file_name=>"$basedir$t->{'name'}", %base_key)},
1579                               esc_html($t->{'name'})) .
1580                       "</td>\n" .
1581                       "<td class=\"link\">" .
1582                       $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1583                                              file_name=>"$basedir$t->{'name'}", %base_key)},
1584                               "tree");
1585                 if (defined $hash_base) {
1586                         print " | " .
1587                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1588                                                      file_name=>"$basedir$t->{'name'}")},
1589                                       "history");
1590                 }
1591                 print "</td>\n";
1592         }
1595 ## ......................................................................
1596 ## functions printing large fragments of HTML
1598 sub git_difftree_body {
1599         my ($difftree, $hash, $parent) = @_;
1601         print "<div class=\"list_head\">\n";
1602         if ($#{$difftree} > 10) {
1603                 print(($#{$difftree} + 1) . " files changed:\n");
1604         }
1605         print "</div>\n";
1607         print "<table class=\"diff_tree\">\n";
1608         my $alternate = 0;
1609         my $patchno = 0;
1610         foreach my $line (@{$difftree}) {
1611                 my %diff = parse_difftree_raw_line($line);
1613                 if ($alternate) {
1614                         print "<tr class=\"dark\">\n";
1615                 } else {
1616                         print "<tr class=\"light\">\n";
1617                 }
1618                 $alternate ^= 1;
1620                 my ($to_mode_oct, $to_mode_str, $to_file_type);
1621                 my ($from_mode_oct, $from_mode_str, $from_file_type);
1622                 if ($diff{'to_mode'} ne ('0' x 6)) {
1623                         $to_mode_oct = oct $diff{'to_mode'};
1624                         if (S_ISREG($to_mode_oct)) { # only for regular file
1625                                 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1626                         }
1627                         $to_file_type = file_type($diff{'to_mode'});
1628                 }
1629                 if ($diff{'from_mode'} ne ('0' x 6)) {
1630                         $from_mode_oct = oct $diff{'from_mode'};
1631                         if (S_ISREG($to_mode_oct)) { # only for regular file
1632                                 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1633                         }
1634                         $from_file_type = file_type($diff{'from_mode'});
1635                 }
1637                 if ($diff{'status'} eq "A") { # created
1638                         my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1639                         $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
1640                         $mode_chng   .= "]</span>";
1641                         print "<td>" .
1642                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1643                                                      hash_base=>$hash, file_name=>$diff{'file'}),
1644                                       -class => "list"}, esc_html($diff{'file'})) .
1645                               "</td>\n" .
1646                               "<td>$mode_chng</td>\n" .
1647                               "<td class=\"link\">" .
1648                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1649                                                      hash_base=>$hash, file_name=>$diff{'file'})},
1650                                       "blob");
1651                         if ($action eq 'commitdiff') {
1652                                 # link to patch
1653                                 $patchno++;
1654                                 print " | " .
1655                                       $cgi->a({-href => "#patch$patchno"}, "patch");
1656                         }
1657                         print "</td>\n";
1659                 } elsif ($diff{'status'} eq "D") { # deleted
1660                         my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1661                         print "<td>" .
1662                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1663                                                      hash_base=>$parent, file_name=>$diff{'file'}),
1664                                        -class => "list"}, esc_html($diff{'file'})) .
1665                               "</td>\n" .
1666                               "<td>$mode_chng</td>\n" .
1667                               "<td class=\"link\">" .
1668                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1669                                                      hash_base=>$parent, file_name=>$diff{'file'})},
1670                                       "blob") .
1671                               " | ";
1672                         if ($action eq 'commitdiff') {
1673                                 # link to patch
1674                                 $patchno++;
1675                                 print " | " .
1676                                       $cgi->a({-href => "#patch$patchno"}, "patch");
1677                         }
1678                         print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1679                                                      file_name=>$diff{'file'})},
1680                                       "history") .
1681                               "</td>\n";
1683                 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1684                         my $mode_chnge = "";
1685                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1686                                 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1687                                 if ($from_file_type != $to_file_type) {
1688                                         $mode_chnge .= " from $from_file_type to $to_file_type";
1689                                 }
1690                                 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1691                                         if ($from_mode_str && $to_mode_str) {
1692                                                 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1693                                         } elsif ($to_mode_str) {
1694                                                 $mode_chnge .= " mode: $to_mode_str";
1695                                         }
1696                                 }
1697                                 $mode_chnge .= "]</span>\n";
1698                         }
1699                         print "<td>";
1700                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1701                                 print $cgi->a({-href => href(action=>"blobdiff",
1702                                                              hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1703                                                              hash_base=>$hash, hash_parent_base=>$parent,
1704                                                              file_name=>$diff{'file'}),
1705                                               -class => "list"}, esc_html($diff{'file'}));
1706                         } else { # only mode changed
1707                                 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1708                                                              hash_base=>$hash, file_name=>$diff{'file'}),
1709                                               -class => "list"}, esc_html($diff{'file'}));
1710                         }
1711                         print "</td>\n" .
1712                               "<td>$mode_chnge</td>\n" .
1713                               "<td class=\"link\">" .
1714                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1715                                                      hash_base=>$hash, file_name=>$diff{'file'})},
1716                                       "blob");
1717                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1718                                 if ($action eq 'commitdiff') {
1719                                         # link to patch
1720                                         $patchno++;
1721                                         print " | " .
1722                                                 $cgi->a({-href => "#patch$patchno"}, "patch");
1723                                 } else {
1724                                         print " | " .
1725                                                 $cgi->a({-href => href(action=>"blobdiff",
1726                                                                        hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1727                                                                        hash_base=>$hash, hash_parent_base=>$parent,
1728                                                                        file_name=>$diff{'file'})},
1729                                                         "diff");
1730                                 }
1731                         }
1732                         print " | " .
1733                                 $cgi->a({-href => href(action=>"history",
1734                                                        hash_base=>$hash, file_name=>$diff{'file'})},
1735                                         "history");
1736                         print "</td>\n";
1738                 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1739                         my %status_name = ('R' => 'moved', 'C' => 'copied');
1740                         my $nstatus = $status_name{$diff{'status'}};
1741                         my $mode_chng = "";
1742                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1743                                 # mode also for directories, so we cannot use $to_mode_str
1744                                 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1745                         }
1746                         print "<td>" .
1747                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1748                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1749                                       -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1750                               "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1751                               $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1752                                                      hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1753                                       -class => "list"}, esc_html($diff{'from_file'})) .
1754                               " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1755                               "<td class=\"link\">" .
1756                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1757                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1758                                       "blob");
1759                         if ($diff{'to_id'} ne $diff{'from_id'}) {
1760                                 if ($action eq 'commitdiff') {
1761                                         # link to patch
1762                                         $patchno++;
1763                                         print " | " .
1764                                                 $cgi->a({-href => "#patch$patchno"}, "patch");
1765                                 } else {
1766                                         print " | " .
1767                                                 $cgi->a({-href => href(action=>"blobdiff",
1768                                                                        hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1769                                                                        hash_base=>$hash, hash_parent_base=>$parent,
1770                                                                        file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1771                                                         "diff");
1772                                 }
1773                         }
1774                         print "</td>\n";
1776                 } # we should not encounter Unmerged (U) or Unknown (X) status
1777                 print "</tr>\n";
1778         }
1779         print "</table>\n";
1782 sub git_patchset_body {
1783         my ($fd, $difftree, $hash, $hash_parent) = @_;
1785         my $patch_idx = 0;
1786         my $in_header = 0;
1787         my $patch_found = 0;
1788         my $diffinfo;
1790         print "<div class=\"patchset\">\n";
1792         LINE:
1793         while (my $patch_line = <$fd>) {
1794                 chomp $patch_line;
1796                 if ($patch_line =~ m/^diff /) { # "git diff" header
1797                         # beginning of patch (in patchset)
1798                         if ($patch_found) {
1799                                 # close previous patch
1800                                 print "</div>\n"; # class="patch"
1801                         } else {
1802                                 # first patch in patchset
1803                                 $patch_found = 1;
1804                         }
1805                         print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1807                         if (ref($difftree->[$patch_idx]) eq "HASH") {
1808                                 $diffinfo = $difftree->[$patch_idx];
1809                         } else {
1810                                 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1811                         }
1812                         $patch_idx++;
1814                         # for now, no extended header, hence we skip empty patches
1815                         # companion to  next LINE if $in_header;
1816                         if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1817                                 $in_header = 1;
1818                                 next LINE;
1819                         }
1821                         if ($diffinfo->{'status'} eq "A") { # added
1822                                 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1823                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1824                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1825                                               $diffinfo->{'to_id'}) . "(new)" .
1826                                       "</div>\n"; # class="diff_info"
1828                         } elsif ($diffinfo->{'status'} eq "D") { # deleted
1829                                 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1830                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1831                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1832                                               $diffinfo->{'from_id'}) . "(deleted)" .
1833                                       "</div>\n"; # class="diff_info"
1835                         } elsif ($diffinfo->{'status'} eq "R" || # renamed
1836                                  $diffinfo->{'status'} eq "C" || # copied
1837                                  $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1838                                 print "<div class=\"diff_info\">" .
1839                                       file_type($diffinfo->{'from_mode'}) . ":" .
1840                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1841                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1842                                               $diffinfo->{'from_id'}) .
1843                                       " -> " .
1844                                       file_type($diffinfo->{'to_mode'}) . ":" .
1845                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1846                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1847                                               $diffinfo->{'to_id'});
1848                                 print "</div>\n"; # class="diff_info"
1850                         } else { # modified, mode changed, ...
1851                                 print "<div class=\"diff_info\">" .
1852                                       file_type($diffinfo->{'from_mode'}) . ":" .
1853                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1854                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1855                                               $diffinfo->{'from_id'}) .
1856                                       " -> " .
1857                                       file_type($diffinfo->{'to_mode'}) . ":" .
1858                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1859                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1860                                               $diffinfo->{'to_id'});
1861                                 print "</div>\n"; # class="diff_info"
1862                         }
1864                         #print "<div class=\"diff extended_header\">\n";
1865                         $in_header = 1;
1866                         next LINE;
1867                 } # start of patch in patchset
1870                 if ($in_header && $patch_line =~ m/^---/) {
1871                         #print "</div>\n"; # class="diff extended_header"
1872                         $in_header = 0;
1874                         my $file = $diffinfo->{'from_file'};
1875                         $file  ||= $diffinfo->{'file'};
1876                         $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1877                                                        hash=>$diffinfo->{'from_id'}, file_name=>$file),
1878                                         -class => "list"}, esc_html($file));
1879                         $patch_line =~ s|a/.*$|a/$file|g;
1880                         print "<div class=\"diff from_file\">$patch_line</div>\n";
1882                         $patch_line = <$fd>;
1883                         chomp $patch_line;
1885                         #$patch_line =~ m/^+++/;
1886                         $file    = $diffinfo->{'to_file'};
1887                         $file  ||= $diffinfo->{'file'};
1888                         $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1889                                                        hash=>$diffinfo->{'to_id'}, file_name=>$file),
1890                                         -class => "list"}, esc_html($file));
1891                         $patch_line =~ s|b/.*|b/$file|g;
1892                         print "<div class=\"diff to_file\">$patch_line</div>\n";
1894                         next LINE;
1895                 }
1896                 next LINE if $in_header;
1898                 print format_diff_line($patch_line);
1899         }
1900         print "</div>\n" if $patch_found; # class="patch"
1902         print "</div>\n"; # class="patchset"
1905 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1907 sub git_shortlog_body {
1908         # uses global variable $project
1909         my ($revlist, $from, $to, $refs, $extra) = @_;
1911         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1912         my $have_snapshot = (defined $ctype && defined $suffix);
1914         $from = 0 unless defined $from;
1915         $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1917         print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1918         my $alternate = 0;
1919         for (my $i = $from; $i <= $to; $i++) {
1920                 my $commit = $revlist->[$i];
1921                 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1922                 my $ref = format_ref_marker($refs, $commit);
1923                 my %co = parse_commit($commit);
1924                 if ($alternate) {
1925                         print "<tr class=\"dark\">\n";
1926                 } else {
1927                         print "<tr class=\"light\">\n";
1928                 }
1929                 $alternate ^= 1;
1930                 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1931                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1932                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1933                       "<td>";
1934                 print format_subject_html($co{'title'}, $co{'title_short'},
1935                                           href(action=>"commit", hash=>$commit), $ref);
1936                 print "</td>\n" .
1937                       "<td class=\"link\">" .
1938                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1939                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1940                 if ($have_snapshot) {
1941                         print " | " .  $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1942                 }
1943                 print "</td>\n" .
1944                       "</tr>\n";
1945         }
1946         if (defined $extra) {
1947                 print "<tr>\n" .
1948                       "<td colspan=\"4\">$extra</td>\n" .
1949                       "</tr>\n";
1950         }
1951         print "</table>\n";
1954 sub git_history_body {
1955         # Warning: assumes constant type (blob or tree) during history
1956         my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
1958         $from = 0 unless defined $from;
1959         $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
1961         print "<table class=\"history\" cellspacing=\"0\">\n";
1962         my $alternate = 0;
1963         for (my $i = $from; $i <= $to; $i++) {
1964                 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
1965                         next;
1966                 }
1968                 my $commit = $1;
1969                 my %co = parse_commit($commit);
1970                 if (!%co) {
1971                         next;
1972                 }
1974                 my $ref = format_ref_marker($refs, $commit);
1976                 if ($alternate) {
1977                         print "<tr class=\"dark\">\n";
1978                 } else {
1979                         print "<tr class=\"light\">\n";
1980                 }
1981                 $alternate ^= 1;
1982                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1983                       # shortlog uses      chop_str($co{'author_name'}, 10)
1984                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1985                       "<td>";
1986                 # originally git_history used chop_str($co{'title'}, 50)
1987                 print format_subject_html($co{'title'}, $co{'title_short'},
1988                                           href(action=>"commit", hash=>$commit), $ref);
1989                 print "</td>\n" .
1990                       "<td class=\"link\">" .
1991                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1992                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1993                       $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1995                 if ($ftype eq 'blob') {
1996                         my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1997                         my $blob_parent  = git_get_hash_by_path($commit, $file_name);
1998                         if (defined $blob_current && defined $blob_parent &&
1999                                         $blob_current ne $blob_parent) {
2000                                 print " | " .
2001                                         $cgi->a({-href => href(action=>"blobdiff",
2002                                                                hash=>$blob_current, hash_parent=>$blob_parent,
2003                                                                hash_base=>$hash_base, hash_parent_base=>$commit,
2004                                                                file_name=>$file_name)},
2005                                                 "diff to current");
2006                         }
2007                 }
2008                 print "</td>\n" .
2009                       "</tr>\n";
2010         }
2011         if (defined $extra) {
2012                 print "<tr>\n" .
2013                       "<td colspan=\"4\">$extra</td>\n" .
2014                       "</tr>\n";
2015         }
2016         print "</table>\n";
2019 sub git_tags_body {
2020         # uses global variable $project
2021         my ($taglist, $from, $to, $extra) = @_;
2022         $from = 0 unless defined $from;
2023         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2025         print "<table class=\"tags\" cellspacing=\"0\">\n";
2026         my $alternate = 0;
2027         for (my $i = $from; $i <= $to; $i++) {
2028                 my $entry = $taglist->[$i];
2029                 my %tag = %$entry;
2030                 my $comment_lines = $tag{'comment'};
2031                 my $comment = shift @$comment_lines;
2032                 my $comment_short;
2033                 if (defined $comment) {
2034                         $comment_short = chop_str($comment, 30, 5);
2035                 }
2036                 if ($alternate) {
2037                         print "<tr class=\"dark\">\n";
2038                 } else {
2039                         print "<tr class=\"light\">\n";
2040                 }
2041                 $alternate ^= 1;
2042                 print "<td><i>$tag{'age'}</i></td>\n" .
2043                       "<td>" .
2044                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2045                                -class => "list name"}, esc_html($tag{'name'})) .
2046                       "</td>\n" .
2047                       "<td>";
2048                 if (defined $comment) {
2049                         print format_subject_html($comment, $comment_short,
2050                                                   href(action=>"tag", hash=>$tag{'id'}));
2051                 }
2052                 print "</td>\n" .
2053                       "<td class=\"selflink\">";
2054                 if ($tag{'type'} eq "tag") {
2055                         print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2056                 } else {
2057                         print "&nbsp;";
2058                 }
2059                 print "</td>\n" .
2060                       "<td class=\"link\">" . " | " .
2061                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2062                 if ($tag{'reftype'} eq "commit") {
2063                         print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2064                               " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2065                 } elsif ($tag{'reftype'} eq "blob") {
2066                         print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2067                 }
2068                 print "</td>\n" .
2069                       "</tr>";
2070         }
2071         if (defined $extra) {
2072                 print "<tr>\n" .
2073                       "<td colspan=\"5\">$extra</td>\n" .
2074                       "</tr>\n";
2075         }
2076         print "</table>\n";
2079 sub git_heads_body {
2080         # uses global variable $project
2081         my ($taglist, $head, $from, $to, $extra) = @_;
2082         $from = 0 unless defined $from;
2083         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2085         print "<table class=\"heads\" cellspacing=\"0\">\n";
2086         my $alternate = 0;
2087         for (my $i = $from; $i <= $to; $i++) {
2088                 my $entry = $taglist->[$i];
2089                 my %tag = %$entry;
2090                 my $curr = $tag{'id'} eq $head;
2091                 if ($alternate) {
2092                         print "<tr class=\"dark\">\n";
2093                 } else {
2094                         print "<tr class=\"light\">\n";
2095                 }
2096                 $alternate ^= 1;
2097                 print "<td><i>$tag{'age'}</i></td>\n" .
2098                       ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2099                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2100                                -class => "list name"},esc_html($tag{'name'})) .
2101                       "</td>\n" .
2102                       "<td class=\"link\">" .
2103                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2104                       $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
2105                       "</td>\n" .
2106                       "</tr>";
2107         }
2108         if (defined $extra) {
2109                 print "<tr>\n" .
2110                       "<td colspan=\"3\">$extra</td>\n" .
2111                       "</tr>\n";
2112         }
2113         print "</table>\n";
2116 ## ======================================================================
2117 ## ======================================================================
2118 ## actions
2120 sub git_project_list {
2121         my $order = $cgi->param('o');
2122         if (defined $order && $order !~ m/project|descr|owner|age/) {
2123                 die_error(undef, "Unknown order parameter");
2124         }
2126         my @list = git_get_projects_list();
2127         my @projects;
2128         if (!@list) {
2129                 die_error(undef, "No projects found");
2130         }
2131         foreach my $pr (@list) {
2132                 my $head = git_get_head_hash($pr->{'path'});
2133                 if (!defined $head) {
2134                         next;
2135                 }
2136                 $git_dir = "$projectroot/$pr->{'path'}";
2137                 my %co = parse_commit($head);
2138                 if (!%co) {
2139                         next;
2140                 }
2141                 $pr->{'commit'} = \%co;
2142                 if (!defined $pr->{'descr'}) {
2143                         my $descr = git_get_project_description($pr->{'path'}) || "";
2144                         $pr->{'descr'} = chop_str($descr, 25, 5);
2145                 }
2146                 if (!defined $pr->{'owner'}) {
2147                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2148                 }
2149                 push @projects, $pr;
2150         }
2152         git_header_html();
2153         if (-f $home_text) {
2154                 print "<div class=\"index_include\">\n";
2155                 open (my $fd, $home_text);
2156                 print <$fd>;
2157                 close $fd;
2158                 print "</div>\n";
2159         }
2160         print "<table class=\"project_list\">\n" .
2161               "<tr>\n";
2162         $order ||= "project";
2163         if ($order eq "project") {
2164                 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2165                 print "<th>Project</th>\n";
2166         } else {
2167                 print "<th>" .
2168                       $cgi->a({-href => href(project=>undef, order=>'project'),
2169                                -class => "header"}, "Project") .
2170                       "</th>\n";
2171         }
2172         if ($order eq "descr") {
2173                 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2174                 print "<th>Description</th>\n";
2175         } else {
2176                 print "<th>" .
2177                       $cgi->a({-href => href(project=>undef, order=>'descr'),
2178                                -class => "header"}, "Description") .
2179                       "</th>\n";
2180         }
2181         if ($order eq "owner") {
2182                 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2183                 print "<th>Owner</th>\n";
2184         } else {
2185                 print "<th>" .
2186                       $cgi->a({-href => href(project=>undef, order=>'owner'),
2187                                -class => "header"}, "Owner") .
2188                       "</th>\n";
2189         }
2190         if ($order eq "age") {
2191                 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2192                 print "<th>Last Change</th>\n";
2193         } else {
2194                 print "<th>" .
2195                       $cgi->a({-href => href(project=>undef, order=>'age'),
2196                                -class => "header"}, "Last Change") .
2197                       "</th>\n";
2198         }
2199         print "<th></th>\n" .
2200               "</tr>\n";
2201         my $alternate = 0;
2202         foreach my $pr (@projects) {
2203                 if ($alternate) {
2204                         print "<tr class=\"dark\">\n";
2205                 } else {
2206                         print "<tr class=\"light\">\n";
2207                 }
2208                 $alternate ^= 1;
2209                 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2210                                         -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2211                       "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2212                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2213                 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2214                       $pr->{'commit'}{'age_string'} . "</td>\n" .
2215                       "<td class=\"link\">" .
2216                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
2217                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2218                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2219                       "</td>\n" .
2220                       "</tr>\n";
2221         }
2222         print "</table>\n";
2223         git_footer_html();
2226 sub git_project_index {
2227         my @projects = git_get_projects_list();
2229         print $cgi->header(
2230                 -type => 'text/plain',
2231                 -charset => 'utf-8',
2232                 -content_disposition => qq(inline; filename="index.aux"));
2234         foreach my $pr (@projects) {
2235                 if (!exists $pr->{'owner'}) {
2236                         $pr->{'owner'} = get_file_owner("$projectroot/$project");
2237                 }
2239                 my ($path, $owner) = ($pr->{'path'}, $pr->{'owner'});
2240                 # quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '
2241                 $path  =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2242                 $owner =~ s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X", ord($1))/eg;
2243                 $path  =~ s/ /\+/g;
2244                 $owner =~ s/ /\+/g;
2246                 print "$path $owner\n";
2247         }
2250 sub git_summary {
2251         my $descr = git_get_project_description($project) || "none";
2252         my $head = git_get_head_hash($project);
2253         my %co = parse_commit($head);
2254         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2256         my $owner = git_get_project_owner($project);
2258         my $refs = git_get_references();
2259         git_header_html();
2260         git_print_page_nav('summary','', $head);
2262         print "<div class=\"title\">&nbsp;</div>\n";
2263         print "<table cellspacing=\"0\">\n" .
2264               "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2265               "<tr><td>owner</td><td>$owner</td></tr>\n" .
2266               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2267         # use per project git URL list in $projectroot/$project/cloneurl
2268         # or make project git URL from git base URL and project name
2269         my $url_tag = "URL";
2270         my @url_list = git_get_project_url_list($project);
2271         @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2272         foreach my $git_url (@url_list) {
2273                 next unless $git_url;
2274                 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2275                 $url_tag = "";
2276         }
2277         print "</table>\n";
2279         open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2280                 git_get_head_hash($project)
2281                 or die_error(undef, "Open git-rev-list failed");
2282         my @revlist = map { chomp; $_ } <$fd>;
2283         close $fd;
2284         git_print_header_div('shortlog');
2285         git_shortlog_body(\@revlist, 0, 15, $refs,
2286                           $cgi->a({-href => href(action=>"shortlog")}, "..."));
2288         my $taglist = git_get_refs_list("refs/tags");
2289         if (defined @$taglist) {
2290                 git_print_header_div('tags');
2291                 git_tags_body($taglist, 0, 15,
2292                               $cgi->a({-href => href(action=>"tags")}, "..."));
2293         }
2295         my $headlist = git_get_refs_list("refs/heads");
2296         if (defined @$headlist) {
2297                 git_print_header_div('heads');
2298                 git_heads_body($headlist, $head, 0, 15,
2299                                $cgi->a({-href => href(action=>"heads")}, "..."));
2300         }
2302         git_footer_html();
2305 sub git_tag {
2306         my $head = git_get_head_hash($project);
2307         git_header_html();
2308         git_print_page_nav('','', $head,undef,$head);
2309         my %tag = parse_tag($hash);
2310         git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2311         print "<div class=\"title_text\">\n" .
2312               "<table cellspacing=\"0\">\n" .
2313               "<tr>\n" .
2314               "<td>object</td>\n" .
2315               "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2316                                $tag{'object'}) . "</td>\n" .
2317               "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2318                                               $tag{'type'}) . "</td>\n" .
2319               "</tr>\n";
2320         if (defined($tag{'author'})) {
2321                 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2322                 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2323                 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2324                         sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2325                         "</td></tr>\n";
2326         }
2327         print "</table>\n\n" .
2328               "</div>\n";
2329         print "<div class=\"page_body\">";
2330         my $comment = $tag{'comment'};
2331         foreach my $line (@$comment) {
2332                 print esc_html($line) . "<br/>\n";
2333         }
2334         print "</div>\n";
2335         git_footer_html();
2338 sub git_blame2 {
2339         my $fd;
2340         my $ftype;
2342         my ($have_blame) = gitweb_check_feature('blame');
2343         if (!$have_blame) {
2344                 die_error('403 Permission denied', "Permission denied");
2345         }
2346         die_error('404 Not Found', "File name not defined") if (!$file_name);
2347         $hash_base ||= git_get_head_hash($project);
2348         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2349         my %co = parse_commit($hash_base)
2350                 or die_error(undef, "Reading commit failed");
2351         if (!defined $hash) {
2352                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2353                         or die_error(undef, "Error looking up file");
2354         }
2355         $ftype = git_get_type($hash);
2356         if ($ftype !~ "blob") {
2357                 die_error("400 Bad Request", "Object is not a blob");
2358         }
2359         open ($fd, "-|", git_cmd(), "blame", '-l', $file_name, $hash_base)
2360                 or die_error(undef, "Open git-blame failed");
2361         git_header_html();
2362         my $formats_nav =
2363                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2364                         "blob") .
2365                 " | " .
2366                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2367                         "head");
2368         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2369         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2370         git_print_page_path($file_name, $ftype, $hash_base);
2371         my @rev_color = (qw(light2 dark2));
2372         my $num_colors = scalar(@rev_color);
2373         my $current_color = 0;
2374         my $last_rev;
2375         print <<HTML;
2376 <div class="page_body">
2377 <table class="blame">
2378 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2379 HTML
2380         while (<$fd>) {
2381                 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2382                 my $full_rev = $1;
2383                 my $rev = substr($full_rev, 0, 8);
2384                 my $lineno = $2;
2385                 my $data = $3;
2387                 if (!defined $last_rev) {
2388                         $last_rev = $full_rev;
2389                 } elsif ($last_rev ne $full_rev) {
2390                         $last_rev = $full_rev;
2391                         $current_color = ++$current_color % $num_colors;
2392                 }
2393                 print "<tr class=\"$rev_color[$current_color]\">\n";
2394                 print "<td class=\"sha1\">" .
2395                         $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2396                                 esc_html($rev)) . "</td>\n";
2397                 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2398                       esc_html($lineno) . "</a></td>\n";
2399                 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2400                 print "</tr>\n";
2401         }
2402         print "</table>\n";
2403         print "</div>";
2404         close $fd
2405                 or print "Reading blob failed\n";
2406         git_footer_html();
2409 sub git_blame {
2410         my $fd;
2412         my ($have_blame) = gitweb_check_feature('blame');
2413         if (!$have_blame) {
2414                 die_error('403 Permission denied', "Permission denied");
2415         }
2416         die_error('404 Not Found', "File name not defined") if (!$file_name);
2417         $hash_base ||= git_get_head_hash($project);
2418         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2419         my %co = parse_commit($hash_base)
2420                 or die_error(undef, "Reading commit failed");
2421         if (!defined $hash) {
2422                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2423                         or die_error(undef, "Error lookup file");
2424         }
2425         open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2426                 or die_error(undef, "Open git-annotate failed");
2427         git_header_html();
2428         my $formats_nav =
2429                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2430                         "blob") .
2431                 " | " .
2432                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2433                         "head");
2434         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2435         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2436         git_print_page_path($file_name, 'blob', $hash_base);
2437         print "<div class=\"page_body\">\n";
2438         print <<HTML;
2439 <table class="blame">
2440   <tr>
2441     <th>Commit</th>
2442     <th>Age</th>
2443     <th>Author</th>
2444     <th>Line</th>
2445     <th>Data</th>
2446   </tr>
2447 HTML
2448         my @line_class = (qw(light dark));
2449         my $line_class_len = scalar (@line_class);
2450         my $line_class_num = $#line_class;
2451         while (my $line = <$fd>) {
2452                 my $long_rev;
2453                 my $short_rev;
2454                 my $author;
2455                 my $time;
2456                 my $lineno;
2457                 my $data;
2458                 my $age;
2459                 my $age_str;
2460                 my $age_class;
2462                 chomp $line;
2463                 $line_class_num = ($line_class_num + 1) % $line_class_len;
2465                 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2466                         $long_rev = $1;
2467                         $author   = $2;
2468                         $time     = $3;
2469                         $lineno   = $4;
2470                         $data     = $5;
2471                 } else {
2472                         print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2473                         next;
2474                 }
2475                 $short_rev  = substr ($long_rev, 0, 8);
2476                 $age        = time () - $time;
2477                 $age_str    = age_string ($age);
2478                 $age_str    =~ s/ /&nbsp;/g;
2479                 $age_class  = age_class($age);
2480                 $author     = esc_html ($author);
2481                 $author     =~ s/ /&nbsp;/g;
2483                 $data = untabify($data);
2484                 $data = esc_html ($data);
2486                 print <<HTML;
2487   <tr class="$line_class[$line_class_num]">
2488     <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2489     <td class="$age_class">$age_str</td>
2490     <td>$author</td>
2491     <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2492     <td class="pre">$data</td>
2493   </tr>
2494 HTML
2495         } # while (my $line = <$fd>)
2496         print "</table>\n\n";
2497         close $fd
2498                 or print "Reading blob failed.\n";
2499         print "</div>";
2500         git_footer_html();
2503 sub git_tags {
2504         my $head = git_get_head_hash($project);
2505         git_header_html();
2506         git_print_page_nav('','', $head,undef,$head);
2507         git_print_header_div('summary', $project);
2509         my $taglist = git_get_refs_list("refs/tags");
2510         if (defined @$taglist) {
2511                 git_tags_body($taglist);
2512         }
2513         git_footer_html();
2516 sub git_heads {
2517         my $head = git_get_head_hash($project);
2518         git_header_html();
2519         git_print_page_nav('','', $head,undef,$head);
2520         git_print_header_div('summary', $project);
2522         my $taglist = git_get_refs_list("refs/heads");
2523         if (defined @$taglist) {
2524                 git_heads_body($taglist, $head);
2525         }
2526         git_footer_html();
2529 sub git_blob_plain {
2530         # blobs defined by non-textual hash id's can be cached
2531         my $expires;
2532         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2533                 $expires = "+1d";
2534         }
2536         if (!defined $hash) {
2537                 if (defined $file_name) {
2538                         my $base = $hash_base || git_get_head_hash($project);
2539                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2540                                 or die_error(undef, "Error lookup file");
2541                 } else {
2542                         die_error(undef, "No file name defined");
2543                 }
2544         }
2545         my $type = shift;
2546         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2547                 or die_error(undef, "Couldn't cat $file_name, $hash");
2549         $type ||= blob_mimetype($fd, $file_name);
2551         # save as filename, even when no $file_name is given
2552         my $save_as = "$hash";
2553         if (defined $file_name) {
2554                 $save_as = $file_name;
2555         } elsif ($type =~ m/^text\//) {
2556                 $save_as .= '.txt';
2557         }
2559         print $cgi->header(
2560                 -type => "$type",
2561                 -expires=>$expires,
2562                 -content_disposition => "inline; filename=\"$save_as\"");
2563         undef $/;
2564         binmode STDOUT, ':raw';
2565         print <$fd>;
2566         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2567         $/ = "\n";
2568         close $fd;
2571 sub git_blob {
2572         # blobs defined by non-textual hash id's can be cached
2573         my $expires;
2574         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2575                 $expires = "+1d";
2576         }
2578         if (!defined $hash) {
2579                 if (defined $file_name) {
2580                         my $base = $hash_base || git_get_head_hash($project);
2581                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2582                                 or die_error(undef, "Error lookup file");
2583                 } else {
2584                         die_error(undef, "No file name defined");
2585                 }
2586         }
2587         my ($have_blame) = gitweb_check_feature('blame');
2588         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2589                 or die_error(undef, "Couldn't cat $file_name, $hash");
2590         my $mimetype = blob_mimetype($fd, $file_name);
2591         if ($mimetype !~ m/^text\//) {
2592                 close $fd;
2593                 return git_blob_plain($mimetype);
2594         }
2595         git_header_html(undef, $expires);
2596         my $formats_nav = '';
2597         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2598                 if (defined $file_name) {
2599                         if ($have_blame) {
2600                                 $formats_nav .=
2601                                         $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2602                                                                hash=>$hash, file_name=>$file_name)},
2603                                                 "blame") .
2604                                         " | ";
2605                         }
2606                         $formats_nav .=
2607                                 $cgi->a({-href => href(action=>"blob_plain",
2608                                                        hash=>$hash, file_name=>$file_name)},
2609                                         "plain") .
2610                                 " | " .
2611                                 $cgi->a({-href => href(action=>"blob",
2612                                                        hash_base=>"HEAD", file_name=>$file_name)},
2613                                         "head");
2614                 } else {
2615                         $formats_nav .=
2616                                 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2617                 }
2618                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2619                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2620         } else {
2621                 print "<div class=\"page_nav\">\n" .
2622                       "<br/><br/></div>\n" .
2623                       "<div class=\"title\">$hash</div>\n";
2624         }
2625         git_print_page_path($file_name, "blob", $hash_base);
2626         print "<div class=\"page_body\">\n";
2627         my $nr;
2628         while (my $line = <$fd>) {
2629                 chomp $line;
2630                 $nr++;
2631                 $line = untabify($line);
2632                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2633                        $nr, $nr, $nr, esc_html($line);
2634         }
2635         close $fd
2636                 or print "Reading blob failed.\n";
2637         print "</div>";
2638         git_footer_html();
2641 sub git_tree {
2642         if (!defined $hash) {
2643                 $hash = git_get_head_hash($project);
2644                 if (defined $file_name) {
2645                         my $base = $hash_base || $hash;
2646                         $hash = git_get_hash_by_path($base, $file_name, "tree");
2647                 }
2648                 if (!defined $hash_base) {
2649                         $hash_base = $hash;
2650                 }
2651         }
2652         $/ = "\0";
2653         open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2654                 or die_error(undef, "Open git-ls-tree failed");
2655         my @entries = map { chomp; $_ } <$fd>;
2656         close $fd or die_error(undef, "Reading tree failed");
2657         $/ = "\n";
2659         my $refs = git_get_references();
2660         my $ref = format_ref_marker($refs, $hash_base);
2661         git_header_html();
2662         my $base = "";
2663         my ($have_blame) = gitweb_check_feature('blame');
2664         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2665                 git_print_page_nav('tree','', $hash_base);
2666                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2667         } else {
2668                 undef $hash_base;
2669                 print "<div class=\"page_nav\">\n";
2670                 print "<br/><br/></div>\n";
2671                 print "<div class=\"title\">$hash</div>\n";
2672         }
2673         if (defined $file_name) {
2674                 $base = esc_html("$file_name/");
2675         }
2676         git_print_page_path($file_name, 'tree', $hash_base);
2677         print "<div class=\"page_body\">\n";
2678         print "<table cellspacing=\"0\">\n";
2679         my $alternate = 0;
2680         foreach my $line (@entries) {
2681                 my %t = parse_ls_tree_line($line, -z => 1);
2683                 if ($alternate) {
2684                         print "<tr class=\"dark\">\n";
2685                 } else {
2686                         print "<tr class=\"light\">\n";
2687                 }
2688                 $alternate ^= 1;
2690                 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2692                 print "</tr>\n";
2693         }
2694         print "</table>\n" .
2695               "</div>";
2696         git_footer_html();
2699 sub git_snapshot {
2701         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2702         my $have_snapshot = (defined $ctype && defined $suffix);
2703         if (!$have_snapshot) {
2704                 die_error('403 Permission denied', "Permission denied");
2705         }
2707         if (!defined $hash) {
2708                 $hash = git_get_head_hash($project);
2709         }
2711         my $filename = basename($project) . "-$hash.tar.$suffix";
2713         print $cgi->header(-type => 'application/x-tar',
2714                            -content_encoding => $ctype,
2715                            -content_disposition => "inline; filename=\"$filename\"",
2716                            -status => '200 OK');
2718         my $git_command = git_cmd_str();
2719         open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2720                 die_error(undef, "Execute git-tar-tree failed.");
2721         binmode STDOUT, ':raw';
2722         print <$fd>;
2723         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2724         close $fd;
2728 sub git_log {
2729         my $head = git_get_head_hash($project);
2730         if (!defined $hash) {
2731                 $hash = $head;
2732         }
2733         if (!defined $page) {
2734                 $page = 0;
2735         }
2736         my $refs = git_get_references();
2738         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2739         open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2740                 or die_error(undef, "Open git-rev-list failed");
2741         my @revlist = map { chomp; $_ } <$fd>;
2742         close $fd;
2744         my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2746         git_header_html();
2747         git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2749         if (!@revlist) {
2750                 my %co = parse_commit($hash);
2752                 git_print_header_div('summary', $project);
2753                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2754         }
2755         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2756                 my $commit = $revlist[$i];
2757                 my $ref = format_ref_marker($refs, $commit);
2758                 my %co = parse_commit($commit);
2759                 next if !%co;
2760                 my %ad = parse_date($co{'author_epoch'});
2761                 git_print_header_div('commit',
2762                                "<span class=\"age\">$co{'age_string'}</span>" .
2763                                esc_html($co{'title'}) . $ref,
2764                                $commit);
2765                 print "<div class=\"title_text\">\n" .
2766                       "<div class=\"log_link\">\n" .
2767                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2768                       " | " .
2769                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2770                       "<br/>\n" .
2771                       "</div>\n" .
2772                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
2773                       "</div>\n";
2775                 print "<div class=\"log_body\">\n";
2776                 git_print_simplified_log($co{'comment'});
2777                 print "</div>\n";
2778         }
2779         git_footer_html();
2782 sub git_commit {
2783         my %co = parse_commit($hash);
2784         if (!%co) {
2785                 die_error(undef, "Unknown commit object");
2786         }
2787         my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2788         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2790         my $parent = $co{'parent'};
2791         if (!defined $parent) {
2792                 $parent = "--root";
2793         }
2794         open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2795                 or die_error(undef, "Open git-diff-tree failed");
2796         my @difftree = map { chomp; $_ } <$fd>;
2797         close $fd or die_error(undef, "Reading git-diff-tree failed");
2799         # non-textual hash id's can be cached
2800         my $expires;
2801         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2802                 $expires = "+1d";
2803         }
2804         my $refs = git_get_references();
2805         my $ref = format_ref_marker($refs, $co{'id'});
2807         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2808         my $have_snapshot = (defined $ctype && defined $suffix);
2810         my $formats_nav = '';
2811         if (defined $file_name && defined $co{'parent'}) {
2812                 my $parent = $co{'parent'};
2813                 $formats_nav .=
2814                         $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2815                                 "blame");
2816         }
2817         git_header_html(undef, $expires);
2818         git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2819                            $hash, $co{'tree'}, $hash,
2820                            $formats_nav);
2822         if (defined $co{'parent'}) {
2823                 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2824         } else {
2825                 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2826         }
2827         print "<div class=\"title_text\">\n" .
2828               "<table cellspacing=\"0\">\n";
2829         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2830               "<tr>" .
2831               "<td></td><td> $ad{'rfc2822'}";
2832         if ($ad{'hour_local'} < 6) {
2833                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2834                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2835         } else {
2836                 printf(" (%02d:%02d %s)",
2837                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2838         }
2839         print "</td>" .
2840               "</tr>\n";
2841         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2842         print "<tr><td></td><td> $cd{'rfc2822'}" .
2843               sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2844               "</td></tr>\n";
2845         print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2846         print "<tr>" .
2847               "<td>tree</td>" .
2848               "<td class=\"sha1\">" .
2849               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2850                        class => "list"}, $co{'tree'}) .
2851               "</td>" .
2852               "<td class=\"link\">" .
2853               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2854                       "tree");
2855         if ($have_snapshot) {
2856                 print " | " .
2857                       $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2858         }
2859         print "</td>" .
2860               "</tr>\n";
2861         my $parents = $co{'parents'};
2862         foreach my $par (@$parents) {
2863                 print "<tr>" .
2864                       "<td>parent</td>" .
2865                       "<td class=\"sha1\">" .
2866                       $cgi->a({-href => href(action=>"commit", hash=>$par),
2867                                class => "list"}, $par) .
2868                       "</td>" .
2869                       "<td class=\"link\">" .
2870                       $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2871                       " | " .
2872                       $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
2873                       "</td>" .
2874                       "</tr>\n";
2875         }
2876         print "</table>".
2877               "</div>\n";
2879         print "<div class=\"page_body\">\n";
2880         git_print_log($co{'comment'});
2881         print "</div>\n";
2883         git_difftree_body(\@difftree, $hash, $parent);
2885         git_footer_html();
2888 sub git_blobdiff {
2889         my $format = shift || 'html';
2891         my $fd;
2892         my @difftree;
2893         my %diffinfo;
2894         my $expires;
2896         # preparing $fd and %diffinfo for git_patchset_body
2897         # new style URI
2898         if (defined $hash_base && defined $hash_parent_base) {
2899                 if (defined $file_name) {
2900                         # read raw output
2901                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
2902                                 "--", $file_name
2903                                 or die_error(undef, "Open git-diff-tree failed");
2904                         @difftree = map { chomp; $_ } <$fd>;
2905                         close $fd
2906                                 or die_error(undef, "Reading git-diff-tree failed");
2907                         @difftree
2908                                 or die_error('404 Not Found', "Blob diff not found");
2910                 } elsif (defined $hash &&
2911                          $hash =~ /[0-9a-fA-F]{40}/) {
2912                         # try to find filename from $hash
2914                         # read filtered raw output
2915                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
2916                                 or die_error(undef, "Open git-diff-tree failed");
2917                         @difftree =
2918                                 # ':100644 100644 03b21826... 3b93d5e7... M     ls-files.c'
2919                                 # $hash == to_id
2920                                 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
2921                                 map { chomp; $_ } <$fd>;
2922                         close $fd
2923                                 or die_error(undef, "Reading git-diff-tree failed");
2924                         @difftree
2925                                 or die_error('404 Not Found', "Blob diff not found");
2927                 } else {
2928                         die_error('404 Not Found', "Missing one of the blob diff parameters");
2929                 }
2931                 if (@difftree > 1) {
2932                         die_error('404 Not Found', "Ambiguous blob diff specification");
2933                 }
2935                 %diffinfo = parse_difftree_raw_line($difftree[0]);
2936                 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
2937                 $file_name   ||= $diffinfo{'to_file'}   || $diffinfo{'file'};
2939                 $hash_parent ||= $diffinfo{'from_id'};
2940                 $hash        ||= $diffinfo{'to_id'};
2942                 # non-textual hash id's can be cached
2943                 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
2944                     $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
2945                         $expires = '+1d';
2946                 }
2948                 # open patch output
2949                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2950                         '-p', $hash_parent_base, $hash_base,
2951                         "--", $file_name
2952                         or die_error(undef, "Open git-diff-tree failed");
2953         }
2955         # old/legacy style URI
2956         if (!%diffinfo && # if new style URI failed
2957             defined $hash && defined $hash_parent) {
2958                 # fake git-diff-tree raw output
2959                 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
2960                 $diffinfo{'from_id'} = $hash_parent;
2961                 $diffinfo{'to_id'}   = $hash;
2962                 if (defined $file_name) {
2963                         if (defined $file_parent) {
2964                                 $diffinfo{'status'} = '2';
2965                                 $diffinfo{'from_file'} = $file_parent;
2966                                 $diffinfo{'to_file'}   = $file_name;
2967                         } else { # assume not renamed
2968                                 $diffinfo{'status'} = '1';
2969                                 $diffinfo{'from_file'} = $file_name;
2970                                 $diffinfo{'to_file'}   = $file_name;
2971                         }
2972                 } else { # no filename given
2973                         $diffinfo{'status'} = '2';
2974                         $diffinfo{'from_file'} = $hash_parent;
2975                         $diffinfo{'to_file'}   = $hash;
2976                 }
2978                 # non-textual hash id's can be cached
2979                 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
2980                     $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
2981                         $expires = '+1d';
2982                 }
2984                 # open patch output
2985                 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
2986                         or die_error(undef, "Open git-diff failed");
2987         } else  {
2988                 die_error('404 Not Found', "Missing one of the blob diff parameters")
2989                         unless %diffinfo;
2990         }
2992         # header
2993         if ($format eq 'html') {
2994                 my $formats_nav =
2995                         $cgi->a({-href => href(action=>"blobdiff_plain",
2996                                                hash=>$hash, hash_parent=>$hash_parent,
2997                                                hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
2998                                                file_name=>$file_name, file_parent=>$file_parent)},
2999                                 "plain");
3000                 git_header_html(undef, $expires);
3001                 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
3002                         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
3003                         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3004                 } else {
3005                         print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
3006                         print "<div class=\"title\">$hash vs $hash_parent</div>\n";
3007                 }
3008                 if (defined $file_name) {
3009                         git_print_page_path($file_name, "blob", $hash_base);
3010                 } else {
3011                         print "<div class=\"page_path\"></div>\n";
3012                 }
3014         } elsif ($format eq 'plain') {
3015                 print $cgi->header(
3016                         -type => 'text/plain',
3017                         -charset => 'utf-8',
3018                         -expires => $expires,
3019                         -content_disposition => qq(inline; filename="${file_name}.patch"));
3021                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3023         } else {
3024                 die_error(undef, "Unknown blobdiff format");
3025         }
3027         # patch
3028         if ($format eq 'html') {
3029                 print "<div class=\"page_body\">\n";
3031                 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
3032                 close $fd;
3034                 print "</div>\n"; # class="page_body"
3035                 git_footer_html();
3037         } else {
3038                 while (my $line = <$fd>) {
3039                         $line =~ s!a/($hash|$hash_parent)!a/$diffinfo{'from_file'}!g;
3040                         $line =~ s!b/($hash|$hash_parent)!b/$diffinfo{'to_file'}!g;
3042                         print $line;
3044                         last if $line =~ m!^\+\+\+!;
3045                 }
3046                 local $/ = undef;
3047                 print <$fd>;
3048                 close $fd;
3049         }
3052 sub git_blobdiff_plain {
3053         git_blobdiff('plain');
3056 sub git_commitdiff {
3057         my $format = shift || 'html';
3058         my %co = parse_commit($hash);
3059         if (!%co) {
3060                 die_error(undef, "Unknown commit object");
3061         }
3062         if (!defined $hash_parent) {
3063                 $hash_parent = $co{'parent'} || '--root';
3064         }
3066         # read commitdiff
3067         my $fd;
3068         my @difftree;
3069         if ($format eq 'html') {
3070                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3071                         "--patch-with-raw", "--full-index", $hash_parent, $hash
3072                         or die_error(undef, "Open git-diff-tree failed");
3074                 while (chomp(my $line = <$fd>)) {
3075                         # empty line ends raw part of diff-tree output
3076                         last unless $line;
3077                         push @difftree, $line;
3078                 }
3080         } elsif ($format eq 'plain') {
3081                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3082                         '-p', $hash_parent, $hash
3083                         or die_error(undef, "Open git-diff-tree failed");
3085         } else {
3086                 die_error(undef, "Unknown commitdiff format");
3087         }
3089         # non-textual hash id's can be cached
3090         my $expires;
3091         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3092                 $expires = "+1d";
3093         }
3095         # write commit message
3096         if ($format eq 'html') {
3097                 my $refs = git_get_references();
3098                 my $ref = format_ref_marker($refs, $co{'id'});
3099                 my $formats_nav =
3100                         $cgi->a({-href => href(action=>"commitdiff_plain",
3101                                                hash=>$hash, hash_parent=>$hash_parent)},
3102                                 "plain");
3104                 git_header_html(undef, $expires);
3105                 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3106                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3107                 git_print_authorship(\%co);
3108                 print "<div class=\"page_body\">\n";
3109                 print "<div class=\"log\">\n";
3110                 git_print_simplified_log($co{'comment'}, 1); # skip title
3111                 print "</div>\n"; # class="log"
3113         } elsif ($format eq 'plain') {
3114                 my $refs = git_get_references("tags");
3115                 my $tagname = git_get_rev_name_tags($hash);
3116                 my $filename = basename($project) . "-$hash.patch";
3118                 print $cgi->header(
3119                         -type => 'text/plain',
3120                         -charset => 'utf-8',
3121                         -expires => $expires,
3122                         -content_disposition => qq(inline; filename="$filename"));
3123                 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3124                 print <<TEXT;
3125 From: $co{'author'}
3126 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3127 Subject: $co{'title'}
3128 TEXT
3129                 print "X-Git-Tag: $tagname\n" if $tagname;
3130                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3132                 foreach my $line (@{$co{'comment'}}) {
3133                         print "$line\n";
3134                 }
3135                 print "---\n\n";
3136         }
3138         # write patch
3139         if ($format eq 'html') {
3140                 git_difftree_body(\@difftree, $hash, $hash_parent);
3141                 print "<br/>\n";
3143                 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3144                 close $fd;
3145                 print "</div>\n"; # class="page_body"
3146                 git_footer_html();
3148         } elsif ($format eq 'plain') {
3149                 local $/ = undef;
3150                 print <$fd>;
3151                 close $fd
3152                         or print "Reading git-diff-tree failed\n";
3153         }
3156 sub git_commitdiff_plain {
3157         git_commitdiff('plain');
3160 sub git_history {
3161         if (!defined $hash_base) {
3162                 $hash_base = git_get_head_hash($project);
3163         }
3164         if (!defined $page) {
3165                 $page = 0;
3166         }
3167         my $ftype;
3168         my %co = parse_commit($hash_base);
3169         if (!%co) {
3170                 die_error(undef, "Unknown commit object");
3171         }
3173         my $refs = git_get_references();
3174         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3176         if (!defined $hash && defined $file_name) {
3177                 $hash = git_get_hash_by_path($hash_base, $file_name);
3178         }
3179         if (defined $hash) {
3180                 $ftype = git_get_type($hash);
3181         }
3183         open my $fd, "-|",
3184                 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3185                         or die_error(undef, "Open git-rev-list-failed");
3186         my @revlist = map { chomp; $_ } <$fd>;
3187         close $fd
3188                 or die_error(undef, "Reading git-rev-list failed");
3190         my $paging_nav = '';
3191         if ($page > 0) {
3192                 $paging_nav .=
3193                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3194                                                file_name=>$file_name)},
3195                                 "first");
3196                 $paging_nav .= " &sdot; " .
3197                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3198                                                file_name=>$file_name, page=>$page-1),
3199                                  -accesskey => "p", -title => "Alt-p"}, "prev");
3200         } else {
3201                 $paging_nav .= "first";
3202                 $paging_nav .= " &sdot; prev";
3203         }
3204         if ($#revlist >= (100 * ($page+1)-1)) {
3205                 $paging_nav .= " &sdot; " .
3206                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3207                                                file_name=>$file_name, page=>$page+1),
3208                                  -accesskey => "n", -title => "Alt-n"}, "next");
3209         } else {
3210                 $paging_nav .= " &sdot; next";
3211         }
3212         my $next_link = '';
3213         if ($#revlist >= (100 * ($page+1)-1)) {
3214                 $next_link =
3215                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3216                                                file_name=>$file_name, page=>$page+1),
3217                                  -title => "Alt-n"}, "next");
3218         }
3220         git_header_html();
3221         git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3222         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3223         git_print_page_path($file_name, $ftype, $hash_base);
3225         git_history_body(\@revlist, ($page * 100), $#revlist,
3226                          $refs, $hash_base, $ftype, $next_link);
3228         git_footer_html();
3231 sub git_search {
3232         if (!defined $searchtext) {
3233                 die_error(undef, "Text field empty");
3234         }
3235         if (!defined $hash) {
3236                 $hash = git_get_head_hash($project);
3237         }
3238         my %co = parse_commit($hash);
3239         if (!%co) {
3240                 die_error(undef, "Unknown commit object");
3241         }
3243         my $commit_search = 1;
3244         my $author_search = 0;
3245         my $committer_search = 0;
3246         my $pickaxe_search = 0;
3247         if ($searchtext =~ s/^author\\://i) {
3248                 $author_search = 1;
3249         } elsif ($searchtext =~ s/^committer\\://i) {
3250                 $committer_search = 1;
3251         } elsif ($searchtext =~ s/^pickaxe\\://i) {
3252                 $commit_search = 0;
3253                 $pickaxe_search = 1;
3255                 # pickaxe may take all resources of your box and run for several minutes
3256                 # with every query - so decide by yourself how public you make this feature
3257                 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3258                 if (!$have_pickaxe) {
3259                         die_error('403 Permission denied', "Permission denied");
3260                 }
3261         }
3262         git_header_html();
3263         git_print_page_nav('','', $hash,$co{'tree'},$hash);
3264         git_print_header_div('commit', esc_html($co{'title'}), $hash);
3266         print "<table cellspacing=\"0\">\n";
3267         my $alternate = 0;
3268         if ($commit_search) {
3269                 $/ = "\0";
3270                 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3271                 while (my $commit_text = <$fd>) {
3272                         if (!grep m/$searchtext/i, $commit_text) {
3273                                 next;
3274                         }
3275                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3276                                 next;
3277                         }
3278                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3279                                 next;
3280                         }
3281                         my @commit_lines = split "\n", $commit_text;
3282                         my %co = parse_commit(undef, \@commit_lines);
3283                         if (!%co) {
3284                                 next;
3285                         }
3286                         if ($alternate) {
3287                                 print "<tr class=\"dark\">\n";
3288                         } else {
3289                                 print "<tr class=\"light\">\n";
3290                         }
3291                         $alternate ^= 1;
3292                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3293                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3294                               "<td>" .
3295                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3296                                        esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3297                         my $comment = $co{'comment'};
3298                         foreach my $line (@$comment) {
3299                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3300                                         my $lead = esc_html($1) || "";
3301                                         $lead = chop_str($lead, 30, 10);
3302                                         my $match = esc_html($2) || "";
3303                                         my $trail = esc_html($3) || "";
3304                                         $trail = chop_str($trail, 30, 10);
3305                                         my $text = "$lead<span class=\"match\">$match</span>$trail";
3306                                         print chop_str($text, 80, 5) . "<br/>\n";
3307                                 }
3308                         }
3309                         print "</td>\n" .
3310                               "<td class=\"link\">" .
3311                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3312                               " | " .
3313                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3314                         print "</td>\n" .
3315                               "</tr>\n";
3316                 }
3317                 close $fd;
3318         }
3320         if ($pickaxe_search) {
3321                 $/ = "\n";
3322                 my $git_command = git_cmd_str();
3323                 open my $fd, "-|", "$git_command rev-list $hash | " .
3324                         "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3325                 undef %co;
3326                 my @files;
3327                 while (my $line = <$fd>) {
3328                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3329                                 my %set;
3330                                 $set{'file'} = $6;
3331                                 $set{'from_id'} = $3;
3332                                 $set{'to_id'} = $4;
3333                                 $set{'id'} = $set{'to_id'};
3334                                 if ($set{'id'} =~ m/0{40}/) {
3335                                         $set{'id'} = $set{'from_id'};
3336                                 }
3337                                 if ($set{'id'} =~ m/0{40}/) {
3338                                         next;
3339                                 }
3340                                 push @files, \%set;
3341                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3342                                 if (%co) {
3343                                         if ($alternate) {
3344                                                 print "<tr class=\"dark\">\n";
3345                                         } else {
3346                                                 print "<tr class=\"light\">\n";
3347                                         }
3348                                         $alternate ^= 1;
3349                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3350                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3351                                               "<td>" .
3352                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3353                                                       -class => "list subject"},
3354                                                       esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3355                                         while (my $setref = shift @files) {
3356                                                 my %set = %$setref;
3357                                                 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3358                                                                              hash=>$set{'id'}, file_name=>$set{'file'}),
3359                                                               -class => "list"},
3360                                                               "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3361                                                       "<br/>\n";
3362                                         }
3363                                         print "</td>\n" .
3364                                               "<td class=\"link\">" .
3365                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3366                                               " | " .
3367                                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3368                                         print "</td>\n" .
3369                                               "</tr>\n";
3370                                 }
3371                                 %co = parse_commit($1);
3372                         }
3373                 }
3374                 close $fd;
3375         }
3376         print "</table>\n";
3377         git_footer_html();
3380 sub git_shortlog {
3381         my $head = git_get_head_hash($project);
3382         if (!defined $hash) {
3383                 $hash = $head;
3384         }
3385         if (!defined $page) {
3386                 $page = 0;
3387         }
3388         my $refs = git_get_references();
3390         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3391         open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3392                 or die_error(undef, "Open git-rev-list failed");
3393         my @revlist = map { chomp; $_ } <$fd>;
3394         close $fd;
3396         my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3397         my $next_link = '';
3398         if ($#revlist >= (100 * ($page+1)-1)) {
3399                 $next_link =
3400                         $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3401                                  -title => "Alt-n"}, "next");
3402         }
3405         git_header_html();
3406         git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3407         git_print_header_div('summary', $project);
3409         git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3411         git_footer_html();
3414 ## ......................................................................
3415 ## feeds (RSS, OPML)
3417 sub git_rss {
3418         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3419         open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3420                 or die_error(undef, "Open git-rev-list failed");
3421         my @revlist = map { chomp; $_ } <$fd>;
3422         close $fd or die_error(undef, "Reading git-rev-list failed");
3423         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3424         print <<XML;
3425 <?xml version="1.0" encoding="utf-8"?>
3426 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3427 <channel>
3428 <title>$project $my_uri $my_url</title>
3429 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3430 <description>$project log</description>
3431 <language>en</language>
3432 XML
3434         for (my $i = 0; $i <= $#revlist; $i++) {
3435                 my $commit = $revlist[$i];
3436                 my %co = parse_commit($commit);
3437                 # we read 150, we always show 30 and the ones more recent than 48 hours
3438                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3439                         last;
3440                 }
3441                 my %cd = parse_date($co{'committer_epoch'});
3442                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3443                         $co{'parent'}, $co{'id'}
3444                         or next;
3445                 my @difftree = map { chomp; $_ } <$fd>;
3446                 close $fd
3447                         or next;
3448                 print "<item>\n" .
3449                       "<title>" .
3450                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3451                       "</title>\n" .
3452                       "<author>" . esc_html($co{'author'}) . "</author>\n" .
3453                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3454                       "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3455                       "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3456                       "<description>" . esc_html($co{'title'}) . "</description>\n" .
3457                       "<content:encoded>" .
3458                       "<![CDATA[\n";
3459                 my $comment = $co{'comment'};
3460                 foreach my $line (@$comment) {
3461                         $line = decode("utf8", $line, Encode::FB_DEFAULT);
3462                         print "$line<br/>\n";
3463                 }
3464                 print "<br/>\n";
3465                 foreach my $line (@difftree) {
3466                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3467                                 next;
3468                         }
3469                         my $file = validate_input(unquote($7));
3470                         $file = decode("utf8", $file, Encode::FB_DEFAULT);
3471                         print "$file<br/>\n";
3472                 }
3473                 print "]]>\n" .
3474                       "</content:encoded>\n" .
3475                       "</item>\n";
3476         }
3477         print "</channel></rss>";
3480 sub git_opml {
3481         my @list = git_get_projects_list();
3483         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3484         print <<XML;
3485 <?xml version="1.0" encoding="utf-8"?>
3486 <opml version="1.0">
3487 <head>
3488   <title>$site_name Git OPML Export</title>
3489 </head>
3490 <body>
3491 <outline text="git RSS feeds">
3492 XML
3494         foreach my $pr (@list) {
3495                 my %proj = %$pr;
3496                 my $head = git_get_head_hash($proj{'path'});
3497                 if (!defined $head) {
3498                         next;
3499                 }
3500                 $git_dir = "$projectroot/$proj{'path'}";
3501                 my %co = parse_commit($head);
3502                 if (!%co) {
3503                         next;
3504                 }
3506                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3507                 my $rss  = "$my_url?p=$proj{'path'};a=rss";
3508                 my $html = "$my_url?p=$proj{'path'};a=summary";
3509                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3510         }
3511         print <<XML;
3512 </outline>
3513 </body>
3514 </opml>
3515 XML