Code

gitweb: Do not parse refs by hand, use git-peek-remote instead
[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 );
301 if (defined $project) {
302         $action ||= 'summary';
303 } else {
304         $action ||= 'project_list';
306 if (!defined($actions{$action})) {
307         die_error(undef, "Unknown action");
309 $actions{$action}->();
310 exit;
312 ## ======================================================================
313 ## action links
315 sub href(%) {
316         my %params = @_;
318         my @mapping = (
319                 project => "p",
320                 action => "a",
321                 file_name => "f",
322                 file_parent => "fp",
323                 hash => "h",
324                 hash_parent => "hp",
325                 hash_base => "hb",
326                 hash_parent_base => "hpb",
327                 page => "pg",
328                 searchtext => "s",
329         );
330         my %mapping = @mapping;
332         $params{"project"} ||= $project;
334         my @result = ();
335         for (my $i = 0; $i < @mapping; $i += 2) {
336                 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
337                 if (defined $params{$name}) {
338                         push @result, $symbol . "=" . esc_param($params{$name});
339                 }
340         }
341         return "$my_uri?" . join(';', @result);
345 ## ======================================================================
346 ## validation, quoting/unquoting and escaping
348 sub validate_input {
349         my $input = shift;
351         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
352                 return $input;
353         }
354         if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
355                 return undef;
356         }
357         if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
358                 return undef;
359         }
360         return $input;
363 # quote unsafe chars, but keep the slash, even when it's not
364 # correct, but quoted slashes look too horrible in bookmarks
365 sub esc_param {
366         my $str = shift;
367         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
368         $str =~ s/\+/%2B/g;
369         $str =~ s/ /\+/g;
370         return $str;
373 # replace invalid utf8 character with SUBSTITUTION sequence
374 sub esc_html {
375         my $str = shift;
376         $str = decode("utf8", $str, Encode::FB_DEFAULT);
377         $str = escapeHTML($str);
378         $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
379         return $str;
382 # git may return quoted and escaped filenames
383 sub unquote {
384         my $str = shift;
385         if ($str =~ m/^"(.*)"$/) {
386                 $str = $1;
387                 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
388         }
389         return $str;
392 # escape tabs (convert tabs to spaces)
393 sub untabify {
394         my $line = shift;
396         while ((my $pos = index($line, "\t")) != -1) {
397                 if (my $count = (8 - ($pos % 8))) {
398                         my $spaces = ' ' x $count;
399                         $line =~ s/\t/$spaces/;
400                 }
401         }
403         return $line;
406 ## ----------------------------------------------------------------------
407 ## HTML aware string manipulation
409 sub chop_str {
410         my $str = shift;
411         my $len = shift;
412         my $add_len = shift || 10;
414         # allow only $len chars, but don't cut a word if it would fit in $add_len
415         # if it doesn't fit, cut it if it's still longer than the dots we would add
416         $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
417         my $body = $1;
418         my $tail = $2;
419         if (length($tail) > 4) {
420                 $tail = " ...";
421                 $body =~ s/&[^;]*$//; # remove chopped character entities
422         }
423         return "$body$tail";
426 ## ----------------------------------------------------------------------
427 ## functions returning short strings
429 # CSS class for given age value (in seconds)
430 sub age_class {
431         my $age = shift;
433         if ($age < 60*60*2) {
434                 return "age0";
435         } elsif ($age < 60*60*24*2) {
436                 return "age1";
437         } else {
438                 return "age2";
439         }
442 # convert age in seconds to "nn units ago" string
443 sub age_string {
444         my $age = shift;
445         my $age_str;
447         if ($age > 60*60*24*365*2) {
448                 $age_str = (int $age/60/60/24/365);
449                 $age_str .= " years ago";
450         } elsif ($age > 60*60*24*(365/12)*2) {
451                 $age_str = int $age/60/60/24/(365/12);
452                 $age_str .= " months ago";
453         } elsif ($age > 60*60*24*7*2) {
454                 $age_str = int $age/60/60/24/7;
455                 $age_str .= " weeks ago";
456         } elsif ($age > 60*60*24*2) {
457                 $age_str = int $age/60/60/24;
458                 $age_str .= " days ago";
459         } elsif ($age > 60*60*2) {
460                 $age_str = int $age/60/60;
461                 $age_str .= " hours ago";
462         } elsif ($age > 60*2) {
463                 $age_str = int $age/60;
464                 $age_str .= " min ago";
465         } elsif ($age > 2) {
466                 $age_str = int $age;
467                 $age_str .= " sec ago";
468         } else {
469                 $age_str .= " right now";
470         }
471         return $age_str;
474 # convert file mode in octal to symbolic file mode string
475 sub mode_str {
476         my $mode = oct shift;
478         if (S_ISDIR($mode & S_IFMT)) {
479                 return 'drwxr-xr-x';
480         } elsif (S_ISLNK($mode)) {
481                 return 'lrwxrwxrwx';
482         } elsif (S_ISREG($mode)) {
483                 # git cares only about the executable bit
484                 if ($mode & S_IXUSR) {
485                         return '-rwxr-xr-x';
486                 } else {
487                         return '-rw-r--r--';
488                 };
489         } else {
490                 return '----------';
491         }
494 # convert file mode in octal to file type string
495 sub file_type {
496         my $mode = shift;
498         if ($mode !~ m/^[0-7]+$/) {
499                 return $mode;
500         } else {
501                 $mode = oct $mode;
502         }
504         if (S_ISDIR($mode & S_IFMT)) {
505                 return "directory";
506         } elsif (S_ISLNK($mode)) {
507                 return "symlink";
508         } elsif (S_ISREG($mode)) {
509                 return "file";
510         } else {
511                 return "unknown";
512         }
515 ## ----------------------------------------------------------------------
516 ## functions returning short HTML fragments, or transforming HTML fragments
517 ## which don't beling to other sections
519 # format line of commit message or tag comment
520 sub format_log_line_html {
521         my $line = shift;
523         $line = esc_html($line);
524         $line =~ s/ /&nbsp;/g;
525         if ($line =~ m/([0-9a-fA-F]{40})/) {
526                 my $hash_text = $1;
527                 if (git_get_type($hash_text) eq "commit") {
528                         my $link =
529                                 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
530                                         -class => "text"}, $hash_text);
531                         $line =~ s/$hash_text/$link/;
532                 }
533         }
534         return $line;
537 # format marker of refs pointing to given object
538 sub format_ref_marker {
539         my ($refs, $id) = @_;
540         my $markers = '';
542         if (defined $refs->{$id}) {
543                 foreach my $ref (@{$refs->{$id}}) {
544                         my ($type, $name) = qw();
545                         # e.g. tags/v2.6.11 or heads/next
546                         if ($ref =~ m!^(.*?)s?/(.*)$!) {
547                                 $type = $1;
548                                 $name = $2;
549                         } else {
550                                 $type = "ref";
551                                 $name = $ref;
552                         }
554                         $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
555                 }
556         }
558         if ($markers) {
559                 return ' <span class="refs">'. $markers . '</span>';
560         } else {
561                 return "";
562         }
565 # format, perhaps shortened and with markers, title line
566 sub format_subject_html {
567         my ($long, $short, $href, $extra) = @_;
568         $extra = '' unless defined($extra);
570         if (length($short) < length($long)) {
571                 return $cgi->a({-href => $href, -class => "list subject",
572                                 -title => $long},
573                        esc_html($short) . $extra);
574         } else {
575                 return $cgi->a({-href => $href, -class => "list subject"},
576                        esc_html($long)  . $extra);
577         }
580 sub format_diff_line {
581         my $line = shift;
582         my $char = substr($line, 0, 1);
583         my $diff_class = "";
585         chomp $line;
587         if ($char eq '+') {
588                 $diff_class = " add";
589         } elsif ($char eq "-") {
590                 $diff_class = " rem";
591         } elsif ($char eq "@") {
592                 $diff_class = " chunk_header";
593         } elsif ($char eq "\\") {
594                 $diff_class = " incomplete";
595         }
596         $line = untabify($line);
597         return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
600 ## ----------------------------------------------------------------------
601 ## git utility subroutines, invoking git commands
603 # returns path to the core git executable and the --git-dir parameter as list
604 sub git_cmd {
605         return $GIT, '--git-dir='.$git_dir;
608 # returns path to the core git executable and the --git-dir parameter as string
609 sub git_cmd_str {
610         return join(' ', git_cmd());
613 # get HEAD ref of given project as hash
614 sub git_get_head_hash {
615         my $project = shift;
616         my $o_git_dir = $git_dir;
617         my $retval = undef;
618         $git_dir = "$projectroot/$project";
619         if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
620                 my $head = <$fd>;
621                 close $fd;
622                 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
623                         $retval = $1;
624                 }
625         }
626         if (defined $o_git_dir) {
627                 $git_dir = $o_git_dir;
628         }
629         return $retval;
632 # get type of given object
633 sub git_get_type {
634         my $hash = shift;
636         open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
637         my $type = <$fd>;
638         close $fd or return;
639         chomp $type;
640         return $type;
643 sub git_get_project_config {
644         my ($key, $type) = @_;
646         return unless ($key);
647         $key =~ s/^gitweb\.//;
648         return if ($key =~ m/\W/);
650         my @x = (git_cmd(), 'repo-config');
651         if (defined $type) { push @x, $type; }
652         push @x, "--get";
653         push @x, "gitweb.$key";
654         my $val = qx(@x);
655         chomp $val;
656         return ($val);
659 # get hash of given path at given ref
660 sub git_get_hash_by_path {
661         my $base = shift;
662         my $path = shift || return undef;
664         my $tree = $base;
666         open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
667                 or die_error(undef, "Open git-ls-tree failed");
668         my $line = <$fd>;
669         close $fd or return undef;
671         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
672         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
673         return $3;
676 ## ......................................................................
677 ## git utility functions, directly accessing git repository
679 sub git_get_project_description {
680         my $path = shift;
682         open my $fd, "$projectroot/$path/description" or return undef;
683         my $descr = <$fd>;
684         close $fd;
685         chomp $descr;
686         return $descr;
689 sub git_get_project_url_list {
690         my $path = shift;
692         open my $fd, "$projectroot/$path/cloneurl" or return undef;
693         my @git_project_url_list = map { chomp; $_ } <$fd>;
694         close $fd;
696         return wantarray ? @git_project_url_list : \@git_project_url_list;
699 sub git_get_projects_list {
700         my @list;
702         if (-d $projects_list) {
703                 # search in directory
704                 my $dir = $projects_list;
705                 my $pfxlen = length("$dir");
707                 File::Find::find({
708                         follow_fast => 1, # follow symbolic links
709                         dangling_symlinks => 0, # ignore dangling symlinks, silently
710                         wanted => sub {
711                                 # skip project-list toplevel, if we get it.
712                                 return if (m!^[/.]$!);
713                                 # only directories can be git repositories
714                                 return unless (-d $_);
716                                 my $subdir = substr($File::Find::name, $pfxlen + 1);
717                                 # we check related file in $projectroot
718                                 if (-e "$projectroot/$subdir/HEAD") {
719                                         push @list, { path => $subdir };
720                                         $File::Find::prune = 1;
721                                 }
722                         },
723                 }, "$dir");
725         } elsif (-f $projects_list) {
726                 # read from file(url-encoded):
727                 # 'git%2Fgit.git Linus+Torvalds'
728                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
729                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
730                 open my ($fd), $projects_list or return undef;
731                 while (my $line = <$fd>) {
732                         chomp $line;
733                         my ($path, $owner) = split ' ', $line;
734                         $path = unescape($path);
735                         $owner = unescape($owner);
736                         if (!defined $path) {
737                                 next;
738                         }
739                         if (-e "$projectroot/$path/HEAD") {
740                                 my $pr = {
741                                         path => $path,
742                                         owner => decode("utf8", $owner, Encode::FB_DEFAULT),
743                                 };
744                                 push @list, $pr
745                         }
746                 }
747                 close $fd;
748         }
749         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
750         return @list;
753 sub git_get_project_owner {
754         my $project = shift;
755         my $owner;
757         return undef unless $project;
759         # read from file (url-encoded):
760         # 'git%2Fgit.git Linus+Torvalds'
761         # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
762         # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
763         if (-f $projects_list) {
764                 open (my $fd , $projects_list);
765                 while (my $line = <$fd>) {
766                         chomp $line;
767                         my ($pr, $ow) = split ' ', $line;
768                         $pr = unescape($pr);
769                         $ow = unescape($ow);
770                         if ($pr eq $project) {
771                                 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
772                                 last;
773                         }
774                 }
775                 close $fd;
776         }
777         if (!defined $owner) {
778                 $owner = get_file_owner("$projectroot/$project");
779         }
781         return $owner;
784 sub git_get_references {
785         my $type = shift || "";
786         my %refs;
787         my $fd;
788         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
789         # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
790         if (-f "$projectroot/$project/info/refs") {
791                 open $fd, "$projectroot/$project/info/refs"
792                         or return;
793         } else {
794                 open $fd, "-|", git_cmd(), "ls-remote", "."
795                         or return;
796         }
798         while (my $line = <$fd>) {
799                 chomp $line;
800                 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
801                         if (defined $refs{$1}) {
802                                 push @{$refs{$1}}, $2;
803                         } else {
804                                 $refs{$1} = [ $2 ];
805                         }
806                 }
807         }
808         close $fd or return;
809         return \%refs;
812 sub git_get_rev_name_tags {
813         my $hash = shift || return undef;
815         open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
816                 or return;
817         my $name_rev = <$fd>;
818         close $fd;
820         if ($name_rev =~ m|^$hash tags/(.*)$|) {
821                 return $1;
822         } else {
823                 # catches also '$hash undefined' output
824                 return undef;
825         }
828 ## ----------------------------------------------------------------------
829 ## parse to hash functions
831 sub parse_date {
832         my $epoch = shift;
833         my $tz = shift || "-0000";
835         my %date;
836         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
837         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
838         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
839         $date{'hour'} = $hour;
840         $date{'minute'} = $min;
841         $date{'mday'} = $mday;
842         $date{'day'} = $days[$wday];
843         $date{'month'} = $months[$mon];
844         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
845                            $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
846         $date{'mday-time'} = sprintf "%d %s %02d:%02d",
847                              $mday, $months[$mon], $hour ,$min;
849         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
850         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
851         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
852         $date{'hour_local'} = $hour;
853         $date{'minute_local'} = $min;
854         $date{'tz_local'} = $tz;
855         return %date;
858 sub parse_tag {
859         my $tag_id = shift;
860         my %tag;
861         my @comment;
863         open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
864         $tag{'id'} = $tag_id;
865         while (my $line = <$fd>) {
866                 chomp $line;
867                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
868                         $tag{'object'} = $1;
869                 } elsif ($line =~ m/^type (.+)$/) {
870                         $tag{'type'} = $1;
871                 } elsif ($line =~ m/^tag (.+)$/) {
872                         $tag{'name'} = $1;
873                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
874                         $tag{'author'} = $1;
875                         $tag{'epoch'} = $2;
876                         $tag{'tz'} = $3;
877                 } elsif ($line =~ m/--BEGIN/) {
878                         push @comment, $line;
879                         last;
880                 } elsif ($line eq "") {
881                         last;
882                 }
883         }
884         push @comment, <$fd>;
885         $tag{'comment'} = \@comment;
886         close $fd or return;
887         if (!defined $tag{'name'}) {
888                 return
889         };
890         return %tag
893 sub parse_commit {
894         my $commit_id = shift;
895         my $commit_text = shift;
897         my @commit_lines;
898         my %co;
900         if (defined $commit_text) {
901                 @commit_lines = @$commit_text;
902         } else {
903                 $/ = "\0";
904                 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
905                         or return;
906                 @commit_lines = split '\n', <$fd>;
907                 close $fd or return;
908                 $/ = "\n";
909                 pop @commit_lines;
910         }
911         my $header = shift @commit_lines;
912         if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
913                 return;
914         }
915         ($co{'id'}, my @parents) = split ' ', $header;
916         $co{'parents'} = \@parents;
917         $co{'parent'} = $parents[0];
918         while (my $line = shift @commit_lines) {
919                 last if $line eq "\n";
920                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
921                         $co{'tree'} = $1;
922                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
923                         $co{'author'} = $1;
924                         $co{'author_epoch'} = $2;
925                         $co{'author_tz'} = $3;
926                         if ($co{'author'} =~ m/^([^<]+) </) {
927                                 $co{'author_name'} = $1;
928                         } else {
929                                 $co{'author_name'} = $co{'author'};
930                         }
931                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
932                         $co{'committer'} = $1;
933                         $co{'committer_epoch'} = $2;
934                         $co{'committer_tz'} = $3;
935                         $co{'committer_name'} = $co{'committer'};
936                         $co{'committer_name'} =~ s/ <.*//;
937                 }
938         }
939         if (!defined $co{'tree'}) {
940                 return;
941         };
943         foreach my $title (@commit_lines) {
944                 $title =~ s/^    //;
945                 if ($title ne "") {
946                         $co{'title'} = chop_str($title, 80, 5);
947                         # remove leading stuff of merges to make the interesting part visible
948                         if (length($title) > 50) {
949                                 $title =~ s/^Automatic //;
950                                 $title =~ s/^merge (of|with) /Merge ... /i;
951                                 if (length($title) > 50) {
952                                         $title =~ s/(http|rsync):\/\///;
953                                 }
954                                 if (length($title) > 50) {
955                                         $title =~ s/(master|www|rsync)\.//;
956                                 }
957                                 if (length($title) > 50) {
958                                         $title =~ s/kernel.org:?//;
959                                 }
960                                 if (length($title) > 50) {
961                                         $title =~ s/\/pub\/scm//;
962                                 }
963                         }
964                         $co{'title_short'} = chop_str($title, 50, 5);
965                         last;
966                 }
967         }
968         # remove added spaces
969         foreach my $line (@commit_lines) {
970                 $line =~ s/^    //;
971         }
972         $co{'comment'} = \@commit_lines;
974         my $age = time - $co{'committer_epoch'};
975         $co{'age'} = $age;
976         $co{'age_string'} = age_string($age);
977         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
978         if ($age > 60*60*24*7*2) {
979                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
980                 $co{'age_string_age'} = $co{'age_string'};
981         } else {
982                 $co{'age_string_date'} = $co{'age_string'};
983                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
984         }
985         return %co;
988 # parse ref from ref_file, given by ref_id, with given type
989 sub parse_ref {
990         my $ref_file = shift;
991         my $ref_id = shift;
992         my $type = shift || git_get_type($ref_id);
993         my %ref_item;
995         $ref_item{'type'} = $type;
996         $ref_item{'id'} = $ref_id;
997         $ref_item{'epoch'} = 0;
998         $ref_item{'age'} = "unknown";
999         if ($type eq "tag") {
1000                 my %tag = parse_tag($ref_id);
1001                 $ref_item{'comment'} = $tag{'comment'};
1002                 if ($tag{'type'} eq "commit") {
1003                         my %co = parse_commit($tag{'object'});
1004                         $ref_item{'epoch'} = $co{'committer_epoch'};
1005                         $ref_item{'age'} = $co{'age_string'};
1006                 } elsif (defined($tag{'epoch'})) {
1007                         my $age = time - $tag{'epoch'};
1008                         $ref_item{'epoch'} = $tag{'epoch'};
1009                         $ref_item{'age'} = age_string($age);
1010                 }
1011                 $ref_item{'reftype'} = $tag{'type'};
1012                 $ref_item{'name'} = $tag{'name'};
1013                 $ref_item{'refid'} = $tag{'object'};
1014         } elsif ($type eq "commit"){
1015                 my %co = parse_commit($ref_id);
1016                 $ref_item{'reftype'} = "commit";
1017                 $ref_item{'name'} = $ref_file;
1018                 $ref_item{'title'} = $co{'title'};
1019                 $ref_item{'refid'} = $ref_id;
1020                 $ref_item{'epoch'} = $co{'committer_epoch'};
1021                 $ref_item{'age'} = $co{'age_string'};
1022         } else {
1023                 $ref_item{'reftype'} = $type;
1024                 $ref_item{'name'} = $ref_file;
1025                 $ref_item{'refid'} = $ref_id;
1026         }
1028         return %ref_item;
1031 # parse line of git-diff-tree "raw" output
1032 sub parse_difftree_raw_line {
1033         my $line = shift;
1034         my %res;
1036         # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
1037         # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
1038         if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1039                 $res{'from_mode'} = $1;
1040                 $res{'to_mode'} = $2;
1041                 $res{'from_id'} = $3;
1042                 $res{'to_id'} = $4;
1043                 $res{'status'} = $5;
1044                 $res{'similarity'} = $6;
1045                 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1046                         ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1047                 } else {
1048                         $res{'file'} = unquote($7);
1049                 }
1050         }
1051         # 'c512b523472485aef4fff9e57b229d9d243c967f'
1052         elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1053                 $res{'commit'} = $1;
1054         }
1056         return wantarray ? %res : \%res;
1059 # parse line of git-ls-tree output
1060 sub parse_ls_tree_line ($;%) {
1061         my $line = shift;
1062         my %opts = @_;
1063         my %res;
1065         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
1066         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1068         $res{'mode'} = $1;
1069         $res{'type'} = $2;
1070         $res{'hash'} = $3;
1071         if ($opts{'-z'}) {
1072                 $res{'name'} = $4;
1073         } else {
1074                 $res{'name'} = unquote($4);
1075         }
1077         return wantarray ? %res : \%res;
1080 ## ......................................................................
1081 ## parse to array of hashes functions
1083 sub git_get_refs_list {
1084         my $ref_dir = shift;
1085         my @reflist;
1087         my @refs;
1088         open my $fd, "-|", $GIT, "peek-remote", "$projectroot/$project/"
1089                 or return;
1090         while (my $line = <$fd>) {
1091                 chomp $line;
1092                 if ($line =~ m/^([0-9a-fA-F]{40})\t$ref_dir\/?([^\^]+)$/) {
1093                         push @refs, { hash => $1, name => $2 };
1094                 } elsif ($line =~ m/^[0-9a-fA-F]{40}\t$ref_dir\/?(.*)\^\{\}$/ &&
1095                          $1 eq $refs[-1]{'name'}) {
1096                         # most likely a tag is followed by its peeled
1097                         # (deref) one, and when that happens we know the
1098                         # previous one was of type 'tag'.
1099                         $refs[-1]{'type'} = "tag";
1100                 }
1101         }
1102         close $fd;
1104         foreach my $ref (@refs) {
1105                 my $ref_file = $ref->{'name'};
1106                 my $ref_id   = $ref->{'hash'};
1108                 my $type = $ref->{'type'} || git_get_type($ref_id) || next;
1109                 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1111                 push @reflist, \%ref_item;
1112         }
1113         # sort refs by age
1114         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1115         return \@reflist;
1118 ## ----------------------------------------------------------------------
1119 ## filesystem-related functions
1121 sub get_file_owner {
1122         my $path = shift;
1124         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1125         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1126         if (!defined $gcos) {
1127                 return undef;
1128         }
1129         my $owner = $gcos;
1130         $owner =~ s/[,;].*$//;
1131         return decode("utf8", $owner, Encode::FB_DEFAULT);
1134 ## ......................................................................
1135 ## mimetype related functions
1137 sub mimetype_guess_file {
1138         my $filename = shift;
1139         my $mimemap = shift;
1140         -r $mimemap or return undef;
1142         my %mimemap;
1143         open(MIME, $mimemap) or return undef;
1144         while (<MIME>) {
1145                 next if m/^#/; # skip comments
1146                 my ($mime, $exts) = split(/\t+/);
1147                 if (defined $exts) {
1148                         my @exts = split(/\s+/, $exts);
1149                         foreach my $ext (@exts) {
1150                                 $mimemap{$ext} = $mime;
1151                         }
1152                 }
1153         }
1154         close(MIME);
1156         $filename =~ /\.(.*?)$/;
1157         return $mimemap{$1};
1160 sub mimetype_guess {
1161         my $filename = shift;
1162         my $mime;
1163         $filename =~ /\./ or return undef;
1165         if ($mimetypes_file) {
1166                 my $file = $mimetypes_file;
1167                 if ($file !~ m!^/!) { # if it is relative path
1168                         # it is relative to project
1169                         $file = "$projectroot/$project/$file";
1170                 }
1171                 $mime = mimetype_guess_file($filename, $file);
1172         }
1173         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1174         return $mime;
1177 sub blob_mimetype {
1178         my $fd = shift;
1179         my $filename = shift;
1181         if ($filename) {
1182                 my $mime = mimetype_guess($filename);
1183                 $mime and return $mime;
1184         }
1186         # just in case
1187         return $default_blob_plain_mimetype unless $fd;
1189         if (-T $fd) {
1190                 return 'text/plain' .
1191                        ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1192         } elsif (! $filename) {
1193                 return 'application/octet-stream';
1194         } elsif ($filename =~ m/\.png$/i) {
1195                 return 'image/png';
1196         } elsif ($filename =~ m/\.gif$/i) {
1197                 return 'image/gif';
1198         } elsif ($filename =~ m/\.jpe?g$/i) {
1199                 return 'image/jpeg';
1200         } else {
1201                 return 'application/octet-stream';
1202         }
1205 ## ======================================================================
1206 ## functions printing HTML: header, footer, error page
1208 sub git_header_html {
1209         my $status = shift || "200 OK";
1210         my $expires = shift;
1212         my $title = "$site_name git";
1213         if (defined $project) {
1214                 $title .= " - $project";
1215                 if (defined $action) {
1216                         $title .= "/$action";
1217                         if (defined $file_name) {
1218                                 $title .= " - $file_name";
1219                                 if ($action eq "tree" && $file_name !~ m|/$|) {
1220                                         $title .= "/";
1221                                 }
1222                         }
1223                 }
1224         }
1225         my $content_type;
1226         # require explicit support from the UA if we are to send the page as
1227         # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1228         # we have to do this because MSIE sometimes globs '*/*', pretending to
1229         # support xhtml+xml but choking when it gets what it asked for.
1230         if (defined $cgi->http('HTTP_ACCEPT') &&
1231             $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1232             $cgi->Accept('application/xhtml+xml') != 0) {
1233                 $content_type = 'application/xhtml+xml';
1234         } else {
1235                 $content_type = 'text/html';
1236         }
1237         print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1238                            -status=> $status, -expires => $expires);
1239         print <<EOF;
1240 <?xml version="1.0" encoding="utf-8"?>
1241 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1242 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1243 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1244 <!-- git core binaries version $git_version -->
1245 <head>
1246 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1247 <meta name="generator" content="gitweb/$version git/$git_version"/>
1248 <meta name="robots" content="index, nofollow"/>
1249 <title>$title</title>
1250 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1251 EOF
1252         if (defined $project) {
1253                 printf('<link rel="alternate" title="%s log" '.
1254                        'href="%s" type="application/rss+xml"/>'."\n",
1255                        esc_param($project), href(action=>"rss"));
1256         }
1257         if (defined $favicon) {
1258                 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1259         }
1261         print "</head>\n" .
1262               "<body>\n" .
1263               "<div class=\"page_header\">\n" .
1264               "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1265               "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1266               "</a>\n";
1267         print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1268         if (defined $project) {
1269                 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1270                 if (defined $action) {
1271                         print " / $action";
1272                 }
1273                 print "\n";
1274                 if (!defined $searchtext) {
1275                         $searchtext = "";
1276                 }
1277                 my $search_hash;
1278                 if (defined $hash_base) {
1279                         $search_hash = $hash_base;
1280                 } elsif (defined $hash) {
1281                         $search_hash = $hash;
1282                 } else {
1283                         $search_hash = "HEAD";
1284                 }
1285                 $cgi->param("a", "search");
1286                 $cgi->param("h", $search_hash);
1287                 print $cgi->startform(-method => "get", -action => $my_uri) .
1288                       "<div class=\"search\">\n" .
1289                       $cgi->hidden(-name => "p") . "\n" .
1290                       $cgi->hidden(-name => "a") . "\n" .
1291                       $cgi->hidden(-name => "h") . "\n" .
1292                       $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1293                       "</div>" .
1294                       $cgi->end_form() . "\n";
1295         }
1296         print "</div>\n";
1299 sub git_footer_html {
1300         print "<div class=\"page_footer\">\n";
1301         if (defined $project) {
1302                 my $descr = git_get_project_description($project);
1303                 if (defined $descr) {
1304                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1305                 }
1306                 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1307         } else {
1308                 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1309         }
1310         print "</div>\n" .
1311               "</body>\n" .
1312               "</html>";
1315 sub die_error {
1316         my $status = shift || "403 Forbidden";
1317         my $error = shift || "Malformed query, file missing or permission denied";
1319         git_header_html($status);
1320         print <<EOF;
1321 <div class="page_body">
1322 <br /><br />
1323 $status - $error
1324 <br />
1325 </div>
1326 EOF
1327         git_footer_html();
1328         exit;
1331 ## ----------------------------------------------------------------------
1332 ## functions printing or outputting HTML: navigation
1334 sub git_print_page_nav {
1335         my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1336         $extra = '' if !defined $extra; # pager or formats
1338         my @navs = qw(summary shortlog log commit commitdiff tree);
1339         if ($suppress) {
1340                 @navs = grep { $_ ne $suppress } @navs;
1341         }
1343         my %arg = map { $_ => {action=>$_} } @navs;
1344         if (defined $head) {
1345                 for (qw(commit commitdiff)) {
1346                         $arg{$_}{hash} = $head;
1347                 }
1348                 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1349                         for (qw(shortlog log)) {
1350                                 $arg{$_}{hash} = $head;
1351                         }
1352                 }
1353         }
1354         $arg{tree}{hash} = $treehead if defined $treehead;
1355         $arg{tree}{hash_base} = $treebase if defined $treebase;
1357         print "<div class=\"page_nav\">\n" .
1358                 (join " | ",
1359                  map { $_ eq $current ?
1360                        $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1361                  } @navs);
1362         print "<br/>\n$extra<br/>\n" .
1363               "</div>\n";
1366 sub format_paging_nav {
1367         my ($action, $hash, $head, $page, $nrevs) = @_;
1368         my $paging_nav;
1371         if ($hash ne $head || $page) {
1372                 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1373         } else {
1374                 $paging_nav .= "HEAD";
1375         }
1377         if ($page > 0) {
1378                 $paging_nav .= " &sdot; " .
1379                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1380                                  -accesskey => "p", -title => "Alt-p"}, "prev");
1381         } else {
1382                 $paging_nav .= " &sdot; prev";
1383         }
1385         if ($nrevs >= (100 * ($page+1)-1)) {
1386                 $paging_nav .= " &sdot; " .
1387                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1388                                  -accesskey => "n", -title => "Alt-n"}, "next");
1389         } else {
1390                 $paging_nav .= " &sdot; next";
1391         }
1393         return $paging_nav;
1396 ## ......................................................................
1397 ## functions printing or outputting HTML: div
1399 sub git_print_header_div {
1400         my ($action, $title, $hash, $hash_base) = @_;
1401         my %args = ();
1403         $args{action} = $action;
1404         $args{hash} = $hash if $hash;
1405         $args{hash_base} = $hash_base if $hash_base;
1407         print "<div class=\"header\">\n" .
1408               $cgi->a({-href => href(%args), -class => "title"},
1409               $title ? $title : $action) .
1410               "\n</div>\n";
1413 #sub git_print_authorship (\%) {
1414 sub git_print_authorship {
1415         my $co = shift;
1417         my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1418         print "<div class=\"author_date\">" .
1419               esc_html($co->{'author_name'}) .
1420               " [$ad{'rfc2822'}";
1421         if ($ad{'hour_local'} < 6) {
1422                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1423                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1424         } else {
1425                 printf(" (%02d:%02d %s)",
1426                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1427         }
1428         print "]</div>\n";
1431 sub git_print_page_path {
1432         my $name = shift;
1433         my $type = shift;
1434         my $hb = shift;
1436         if (!defined $name) {
1437                 print "<div class=\"page_path\">/</div>\n";
1438         } else {
1439                 my @dirname = split '/', $name;
1440                 my $basename = pop @dirname;
1441                 my $fullname = '';
1443                 print "<div class=\"page_path\">";
1444                 foreach my $dir (@dirname) {
1445                         $fullname .= $dir . '/';
1446                         print $cgi->a({-href => href(action=>"tree", file_name=>$fullname,
1447                                                      hash_base=>$hb),
1448                                       -title => $fullname}, esc_html($dir));
1449                         print "/";
1450                 }
1451                 if (defined $type && $type eq 'blob') {
1452                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1453                                                      hash_base=>$hb),
1454                                       -title => $name}, esc_html($basename));
1455                 } elsif (defined $type && $type eq 'tree') {
1456                         print $cgi->a({-href => href(action=>"tree", file_name=>$file_name,
1457                                                      hash_base=>$hb),
1458                                       -title => $name}, esc_html($basename));
1459                         print "/";
1460                 } else {
1461                         print esc_html($basename);
1462                 }
1463                 print "<br/></div>\n";
1464         }
1467 # sub git_print_log (\@;%) {
1468 sub git_print_log ($;%) {
1469         my $log = shift;
1470         my %opts = @_;
1472         if ($opts{'-remove_title'}) {
1473                 # remove title, i.e. first line of log
1474                 shift @$log;
1475         }
1476         # remove leading empty lines
1477         while (defined $log->[0] && $log->[0] eq "") {
1478                 shift @$log;
1479         }
1481         # print log
1482         my $signoff = 0;
1483         my $empty = 0;
1484         foreach my $line (@$log) {
1485                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1486                         $signoff = 1;
1487                         $empty = 0;
1488                         if (! $opts{'-remove_signoff'}) {
1489                                 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1490                                 next;
1491                         } else {
1492                                 # remove signoff lines
1493                                 next;
1494                         }
1495                 } else {
1496                         $signoff = 0;
1497                 }
1499                 # print only one empty line
1500                 # do not print empty line after signoff
1501                 if ($line eq "") {
1502                         next if ($empty || $signoff);
1503                         $empty = 1;
1504                 } else {
1505                         $empty = 0;
1506                 }
1508                 print format_log_line_html($line) . "<br/>\n";
1509         }
1511         if ($opts{'-final_empty_line'}) {
1512                 # end with single empty line
1513                 print "<br/>\n" unless $empty;
1514         }
1517 sub git_print_simplified_log {
1518         my $log = shift;
1519         my $remove_title = shift;
1521         git_print_log($log,
1522                 -final_empty_line=> 1,
1523                 -remove_title => $remove_title);
1526 # print tree entry (row of git_tree), but without encompassing <tr> element
1527 sub git_print_tree_entry {
1528         my ($t, $basedir, $hash_base, $have_blame) = @_;
1530         my %base_key = ();
1531         $base_key{hash_base} = $hash_base if defined $hash_base;
1533         print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1534         if ($t->{'type'} eq "blob") {
1535                 print "<td class=\"list\">" .
1536                       $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1537                                              file_name=>"$basedir$t->{'name'}", %base_key),
1538                               -class => "list"}, esc_html($t->{'name'})) .
1539                       "</td>\n" .
1540                       "<td class=\"link\">" .
1541                       $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1542                                              file_name=>"$basedir$t->{'name'}", %base_key)},
1543                               "blob");
1544                 if ($have_blame) {
1545                         print " | " .
1546                                 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1547                                                        file_name=>"$basedir$t->{'name'}", %base_key)},
1548                                         "blame");
1549                 }
1550                 if (defined $hash_base) {
1551                         print " | " .
1552                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1553                                                      hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1554                                       "history");
1555                 }
1556                 print " | " .
1557                       $cgi->a({-href => href(action=>"blob_plain",
1558                                              hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1559                               "raw") .
1560                       "</td>\n";
1562         } elsif ($t->{'type'} eq "tree") {
1563                 print "<td class=\"list\">" .
1564                       $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1565                                              file_name=>"$basedir$t->{'name'}", %base_key)},
1566                               esc_html($t->{'name'})) .
1567                       "</td>\n" .
1568                       "<td class=\"link\">" .
1569                       $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1570                                              file_name=>"$basedir$t->{'name'}", %base_key)},
1571                               "tree");
1572                 if (defined $hash_base) {
1573                         print " | " .
1574                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1575                                                      file_name=>"$basedir$t->{'name'}")},
1576                                       "history");
1577                 }
1578                 print "</td>\n";
1579         }
1582 ## ......................................................................
1583 ## functions printing large fragments of HTML
1585 sub git_difftree_body {
1586         my ($difftree, $hash, $parent) = @_;
1588         print "<div class=\"list_head\">\n";
1589         if ($#{$difftree} > 10) {
1590                 print(($#{$difftree} + 1) . " files changed:\n");
1591         }
1592         print "</div>\n";
1594         print "<table class=\"diff_tree\">\n";
1595         my $alternate = 0;
1596         my $patchno = 0;
1597         foreach my $line (@{$difftree}) {
1598                 my %diff = parse_difftree_raw_line($line);
1600                 if ($alternate) {
1601                         print "<tr class=\"dark\">\n";
1602                 } else {
1603                         print "<tr class=\"light\">\n";
1604                 }
1605                 $alternate ^= 1;
1607                 my ($to_mode_oct, $to_mode_str, $to_file_type);
1608                 my ($from_mode_oct, $from_mode_str, $from_file_type);
1609                 if ($diff{'to_mode'} ne ('0' x 6)) {
1610                         $to_mode_oct = oct $diff{'to_mode'};
1611                         if (S_ISREG($to_mode_oct)) { # only for regular file
1612                                 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1613                         }
1614                         $to_file_type = file_type($diff{'to_mode'});
1615                 }
1616                 if ($diff{'from_mode'} ne ('0' x 6)) {
1617                         $from_mode_oct = oct $diff{'from_mode'};
1618                         if (S_ISREG($to_mode_oct)) { # only for regular file
1619                                 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1620                         }
1621                         $from_file_type = file_type($diff{'from_mode'});
1622                 }
1624                 if ($diff{'status'} eq "A") { # created
1625                         my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1626                         $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
1627                         $mode_chng   .= "]</span>";
1628                         print "<td>" .
1629                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1630                                                      hash_base=>$hash, file_name=>$diff{'file'}),
1631                                       -class => "list"}, esc_html($diff{'file'})) .
1632                               "</td>\n" .
1633                               "<td>$mode_chng</td>\n" .
1634                               "<td class=\"link\">" .
1635                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1636                                                      hash_base=>$hash, file_name=>$diff{'file'})},
1637                                       "blob");
1638                         if ($action eq 'commitdiff') {
1639                                 # link to patch
1640                                 $patchno++;
1641                                 print " | " .
1642                                       $cgi->a({-href => "#patch$patchno"}, "patch");
1643                         }
1644                         print "</td>\n";
1646                 } elsif ($diff{'status'} eq "D") { # deleted
1647                         my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1648                         print "<td>" .
1649                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1650                                                      hash_base=>$parent, file_name=>$diff{'file'}),
1651                                        -class => "list"}, esc_html($diff{'file'})) .
1652                               "</td>\n" .
1653                               "<td>$mode_chng</td>\n" .
1654                               "<td class=\"link\">" .
1655                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1656                                                      hash_base=>$parent, file_name=>$diff{'file'})},
1657                                       "blob") .
1658                               " | ";
1659                         if ($action eq 'commitdiff') {
1660                                 # link to patch
1661                                 $patchno++;
1662                                 print " | " .
1663                                       $cgi->a({-href => "#patch$patchno"}, "patch");
1664                         }
1665                         print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1666                                                      file_name=>$diff{'file'})},
1667                                       "history") .
1668                               "</td>\n";
1670                 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1671                         my $mode_chnge = "";
1672                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1673                                 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1674                                 if ($from_file_type != $to_file_type) {
1675                                         $mode_chnge .= " from $from_file_type to $to_file_type";
1676                                 }
1677                                 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1678                                         if ($from_mode_str && $to_mode_str) {
1679                                                 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1680                                         } elsif ($to_mode_str) {
1681                                                 $mode_chnge .= " mode: $to_mode_str";
1682                                         }
1683                                 }
1684                                 $mode_chnge .= "]</span>\n";
1685                         }
1686                         print "<td>";
1687                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1688                                 print $cgi->a({-href => href(action=>"blobdiff",
1689                                                              hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1690                                                              hash_base=>$hash, hash_parent_base=>$parent,
1691                                                              file_name=>$diff{'file'}),
1692                                               -class => "list"}, esc_html($diff{'file'}));
1693                         } else { # only mode changed
1694                                 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1695                                                              hash_base=>$hash, file_name=>$diff{'file'}),
1696                                               -class => "list"}, esc_html($diff{'file'}));
1697                         }
1698                         print "</td>\n" .
1699                               "<td>$mode_chnge</td>\n" .
1700                               "<td class=\"link\">" .
1701                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1702                                                      hash_base=>$hash, file_name=>$diff{'file'})},
1703                                       "blob");
1704                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1705                                 if ($action eq 'commitdiff') {
1706                                         # link to patch
1707                                         $patchno++;
1708                                         print " | " .
1709                                                 $cgi->a({-href => "#patch$patchno"}, "patch");
1710                                 } else {
1711                                         print " | " .
1712                                                 $cgi->a({-href => href(action=>"blobdiff",
1713                                                                        hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1714                                                                        hash_base=>$hash, hash_parent_base=>$parent,
1715                                                                        file_name=>$diff{'file'})},
1716                                                         "diff");
1717                                 }
1718                         }
1719                         print " | " .
1720                                 $cgi->a({-href => href(action=>"history",
1721                                                        hash_base=>$hash, file_name=>$diff{'file'})},
1722                                         "history");
1723                         print "</td>\n";
1725                 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1726                         my %status_name = ('R' => 'moved', 'C' => 'copied');
1727                         my $nstatus = $status_name{$diff{'status'}};
1728                         my $mode_chng = "";
1729                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1730                                 # mode also for directories, so we cannot use $to_mode_str
1731                                 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1732                         }
1733                         print "<td>" .
1734                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1735                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1736                                       -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1737                               "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1738                               $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1739                                                      hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1740                                       -class => "list"}, esc_html($diff{'from_file'})) .
1741                               " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1742                               "<td class=\"link\">" .
1743                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1744                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1745                                       "blob");
1746                         if ($diff{'to_id'} ne $diff{'from_id'}) {
1747                                 if ($action eq 'commitdiff') {
1748                                         # link to patch
1749                                         $patchno++;
1750                                         print " | " .
1751                                                 $cgi->a({-href => "#patch$patchno"}, "patch");
1752                                 } else {
1753                                         print " | " .
1754                                                 $cgi->a({-href => href(action=>"blobdiff",
1755                                                                        hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1756                                                                        hash_base=>$hash, hash_parent_base=>$parent,
1757                                                                        file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1758                                                         "diff");
1759                                 }
1760                         }
1761                         print "</td>\n";
1763                 } # we should not encounter Unmerged (U) or Unknown (X) status
1764                 print "</tr>\n";
1765         }
1766         print "</table>\n";
1769 sub git_patchset_body {
1770         my ($fd, $difftree, $hash, $hash_parent) = @_;
1772         my $patch_idx = 0;
1773         my $in_header = 0;
1774         my $patch_found = 0;
1775         my $diffinfo;
1777         print "<div class=\"patchset\">\n";
1779         LINE:
1780         while (my $patch_line = <$fd>) {
1781                 chomp $patch_line;
1783                 if ($patch_line =~ m/^diff /) { # "git diff" header
1784                         # beginning of patch (in patchset)
1785                         if ($patch_found) {
1786                                 # close previous patch
1787                                 print "</div>\n"; # class="patch"
1788                         } else {
1789                                 # first patch in patchset
1790                                 $patch_found = 1;
1791                         }
1792                         print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1794                         if (ref($difftree->[$patch_idx]) eq "HASH") {
1795                                 $diffinfo = $difftree->[$patch_idx];
1796                         } else {
1797                                 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1798                         }
1799                         $patch_idx++;
1801                         # for now, no extended header, hence we skip empty patches
1802                         # companion to  next LINE if $in_header;
1803                         if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1804                                 $in_header = 1;
1805                                 next LINE;
1806                         }
1808                         if ($diffinfo->{'status'} eq "A") { # added
1809                                 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1810                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1811                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1812                                               $diffinfo->{'to_id'}) . "(new)" .
1813                                       "</div>\n"; # class="diff_info"
1815                         } elsif ($diffinfo->{'status'} eq "D") { # deleted
1816                                 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1817                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1818                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1819                                               $diffinfo->{'from_id'}) . "(deleted)" .
1820                                       "</div>\n"; # class="diff_info"
1822                         } elsif ($diffinfo->{'status'} eq "R" || # renamed
1823                                  $diffinfo->{'status'} eq "C" || # copied
1824                                  $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1825                                 print "<div class=\"diff_info\">" .
1826                                       file_type($diffinfo->{'from_mode'}) . ":" .
1827                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1828                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1829                                               $diffinfo->{'from_id'}) .
1830                                       " -> " .
1831                                       file_type($diffinfo->{'to_mode'}) . ":" .
1832                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1833                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1834                                               $diffinfo->{'to_id'});
1835                                 print "</div>\n"; # class="diff_info"
1837                         } else { # modified, mode changed, ...
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->{'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->{'file'})},
1847                                               $diffinfo->{'to_id'});
1848                                 print "</div>\n"; # class="diff_info"
1849                         }
1851                         #print "<div class=\"diff extended_header\">\n";
1852                         $in_header = 1;
1853                         next LINE;
1854                 } # start of patch in patchset
1857                 if ($in_header && $patch_line =~ m/^---/) {
1858                         #print "</div>\n"; # class="diff extended_header"
1859                         $in_header = 0;
1861                         my $file = $diffinfo->{'from_file'};
1862                         $file  ||= $diffinfo->{'file'};
1863                         $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1864                                                        hash=>$diffinfo->{'from_id'}, file_name=>$file),
1865                                         -class => "list"}, esc_html($file));
1866                         $patch_line =~ s|a/.*$|a/$file|g;
1867                         print "<div class=\"diff from_file\">$patch_line</div>\n";
1869                         $patch_line = <$fd>;
1870                         chomp $patch_line;
1872                         #$patch_line =~ m/^+++/;
1873                         $file    = $diffinfo->{'to_file'};
1874                         $file  ||= $diffinfo->{'file'};
1875                         $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1876                                                        hash=>$diffinfo->{'to_id'}, file_name=>$file),
1877                                         -class => "list"}, esc_html($file));
1878                         $patch_line =~ s|b/.*|b/$file|g;
1879                         print "<div class=\"diff to_file\">$patch_line</div>\n";
1881                         next LINE;
1882                 }
1883                 next LINE if $in_header;
1885                 print format_diff_line($patch_line);
1886         }
1887         print "</div>\n" if $patch_found; # class="patch"
1889         print "</div>\n"; # class="patchset"
1892 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1894 sub git_shortlog_body {
1895         # uses global variable $project
1896         my ($revlist, $from, $to, $refs, $extra) = @_;
1898         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1899         my $have_snapshot = (defined $ctype && defined $suffix);
1901         $from = 0 unless defined $from;
1902         $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1904         print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1905         my $alternate = 0;
1906         for (my $i = $from; $i <= $to; $i++) {
1907                 my $commit = $revlist->[$i];
1908                 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1909                 my $ref = format_ref_marker($refs, $commit);
1910                 my %co = parse_commit($commit);
1911                 if ($alternate) {
1912                         print "<tr class=\"dark\">\n";
1913                 } else {
1914                         print "<tr class=\"light\">\n";
1915                 }
1916                 $alternate ^= 1;
1917                 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1918                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1919                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1920                       "<td>";
1921                 print format_subject_html($co{'title'}, $co{'title_short'},
1922                                           href(action=>"commit", hash=>$commit), $ref);
1923                 print "</td>\n" .
1924                       "<td class=\"link\">" .
1925                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1926                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1927                 if ($have_snapshot) {
1928                         print " | " .  $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1929                 }
1930                 print "</td>\n" .
1931                       "</tr>\n";
1932         }
1933         if (defined $extra) {
1934                 print "<tr>\n" .
1935                       "<td colspan=\"4\">$extra</td>\n" .
1936                       "</tr>\n";
1937         }
1938         print "</table>\n";
1941 sub git_history_body {
1942         # Warning: assumes constant type (blob or tree) during history
1943         my ($revlist, $from, $to, $refs, $hash_base, $ftype, $extra) = @_;
1945         $from = 0 unless defined $from;
1946         $to = $#{$revlist} unless (defined $to && $to <= $#{$revlist});
1948         print "<table class=\"history\" cellspacing=\"0\">\n";
1949         my $alternate = 0;
1950         for (my $i = $from; $i <= $to; $i++) {
1951                 if ($revlist->[$i] !~ m/^([0-9a-fA-F]{40})/) {
1952                         next;
1953                 }
1955                 my $commit = $1;
1956                 my %co = parse_commit($commit);
1957                 if (!%co) {
1958                         next;
1959                 }
1961                 my $ref = format_ref_marker($refs, $commit);
1963                 if ($alternate) {
1964                         print "<tr class=\"dark\">\n";
1965                 } else {
1966                         print "<tr class=\"light\">\n";
1967                 }
1968                 $alternate ^= 1;
1969                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1970                       # shortlog uses      chop_str($co{'author_name'}, 10)
1971                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1972                       "<td>";
1973                 # originally git_history used chop_str($co{'title'}, 50)
1974                 print format_subject_html($co{'title'}, $co{'title_short'},
1975                                           href(action=>"commit", hash=>$commit), $ref);
1976                 print "</td>\n" .
1977                       "<td class=\"link\">" .
1978                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1979                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1980                       $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1982                 if ($ftype eq 'blob') {
1983                         my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1984                         my $blob_parent  = git_get_hash_by_path($commit, $file_name);
1985                         if (defined $blob_current && defined $blob_parent &&
1986                                         $blob_current ne $blob_parent) {
1987                                 print " | " .
1988                                         $cgi->a({-href => href(action=>"blobdiff",
1989                                                                hash=>$blob_current, hash_parent=>$blob_parent,
1990                                                                hash_base=>$hash_base, hash_parent_base=>$commit,
1991                                                                file_name=>$file_name)},
1992                                                 "diff to current");
1993                         }
1994                 }
1995                 print "</td>\n" .
1996                       "</tr>\n";
1997         }
1998         if (defined $extra) {
1999                 print "<tr>\n" .
2000                       "<td colspan=\"4\">$extra</td>\n" .
2001                       "</tr>\n";
2002         }
2003         print "</table>\n";
2006 sub git_tags_body {
2007         # uses global variable $project
2008         my ($taglist, $from, $to, $extra) = @_;
2009         $from = 0 unless defined $from;
2010         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2012         print "<table class=\"tags\" cellspacing=\"0\">\n";
2013         my $alternate = 0;
2014         for (my $i = $from; $i <= $to; $i++) {
2015                 my $entry = $taglist->[$i];
2016                 my %tag = %$entry;
2017                 my $comment_lines = $tag{'comment'};
2018                 my $comment = shift @$comment_lines;
2019                 my $comment_short;
2020                 if (defined $comment) {
2021                         $comment_short = chop_str($comment, 30, 5);
2022                 }
2023                 if ($alternate) {
2024                         print "<tr class=\"dark\">\n";
2025                 } else {
2026                         print "<tr class=\"light\">\n";
2027                 }
2028                 $alternate ^= 1;
2029                 print "<td><i>$tag{'age'}</i></td>\n" .
2030                       "<td>" .
2031                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
2032                                -class => "list name"}, esc_html($tag{'name'})) .
2033                       "</td>\n" .
2034                       "<td>";
2035                 if (defined $comment) {
2036                         print format_subject_html($comment, $comment_short,
2037                                                   href(action=>"tag", hash=>$tag{'id'}));
2038                 }
2039                 print "</td>\n" .
2040                       "<td class=\"selflink\">";
2041                 if ($tag{'type'} eq "tag") {
2042                         print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
2043                 } else {
2044                         print "&nbsp;";
2045                 }
2046                 print "</td>\n" .
2047                       "<td class=\"link\">" . " | " .
2048                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2049                 if ($tag{'reftype'} eq "commit") {
2050                         print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2051                               " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2052                 } elsif ($tag{'reftype'} eq "blob") {
2053                         print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2054                 }
2055                 print "</td>\n" .
2056                       "</tr>";
2057         }
2058         if (defined $extra) {
2059                 print "<tr>\n" .
2060                       "<td colspan=\"5\">$extra</td>\n" .
2061                       "</tr>\n";
2062         }
2063         print "</table>\n";
2066 sub git_heads_body {
2067         # uses global variable $project
2068         my ($taglist, $head, $from, $to, $extra) = @_;
2069         $from = 0 unless defined $from;
2070         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2072         print "<table class=\"heads\" cellspacing=\"0\">\n";
2073         my $alternate = 0;
2074         for (my $i = $from; $i <= $to; $i++) {
2075                 my $entry = $taglist->[$i];
2076                 my %tag = %$entry;
2077                 my $curr = $tag{'id'} eq $head;
2078                 if ($alternate) {
2079                         print "<tr class=\"dark\">\n";
2080                 } else {
2081                         print "<tr class=\"light\">\n";
2082                 }
2083                 $alternate ^= 1;
2084                 print "<td><i>$tag{'age'}</i></td>\n" .
2085                       ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2086                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2087                                -class => "list name"},esc_html($tag{'name'})) .
2088                       "</td>\n" .
2089                       "<td class=\"link\">" .
2090                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2091                       $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
2092                       "</td>\n" .
2093                       "</tr>";
2094         }
2095         if (defined $extra) {
2096                 print "<tr>\n" .
2097                       "<td colspan=\"3\">$extra</td>\n" .
2098                       "</tr>\n";
2099         }
2100         print "</table>\n";
2103 ## ======================================================================
2104 ## ======================================================================
2105 ## actions
2107 sub git_project_list {
2108         my $order = $cgi->param('o');
2109         if (defined $order && $order !~ m/project|descr|owner|age/) {
2110                 die_error(undef, "Unknown order parameter");
2111         }
2113         my @list = git_get_projects_list();
2114         my @projects;
2115         if (!@list) {
2116                 die_error(undef, "No projects found");
2117         }
2118         foreach my $pr (@list) {
2119                 my $head = git_get_head_hash($pr->{'path'});
2120                 if (!defined $head) {
2121                         next;
2122                 }
2123                 $git_dir = "$projectroot/$pr->{'path'}";
2124                 my %co = parse_commit($head);
2125                 if (!%co) {
2126                         next;
2127                 }
2128                 $pr->{'commit'} = \%co;
2129                 if (!defined $pr->{'descr'}) {
2130                         my $descr = git_get_project_description($pr->{'path'}) || "";
2131                         $pr->{'descr'} = chop_str($descr, 25, 5);
2132                 }
2133                 if (!defined $pr->{'owner'}) {
2134                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2135                 }
2136                 push @projects, $pr;
2137         }
2139         git_header_html();
2140         if (-f $home_text) {
2141                 print "<div class=\"index_include\">\n";
2142                 open (my $fd, $home_text);
2143                 print <$fd>;
2144                 close $fd;
2145                 print "</div>\n";
2146         }
2147         print "<table class=\"project_list\">\n" .
2148               "<tr>\n";
2149         $order ||= "project";
2150         if ($order eq "project") {
2151                 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2152                 print "<th>Project</th>\n";
2153         } else {
2154                 print "<th>" .
2155                       $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
2156                                -class => "header"}, "Project") .
2157                       "</th>\n";
2158         }
2159         if ($order eq "descr") {
2160                 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2161                 print "<th>Description</th>\n";
2162         } else {
2163                 print "<th>" .
2164                       $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
2165                                -class => "header"}, "Description") .
2166                       "</th>\n";
2167         }
2168         if ($order eq "owner") {
2169                 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2170                 print "<th>Owner</th>\n";
2171         } else {
2172                 print "<th>" .
2173                       $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
2174                                -class => "header"}, "Owner") .
2175                       "</th>\n";
2176         }
2177         if ($order eq "age") {
2178                 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2179                 print "<th>Last Change</th>\n";
2180         } else {
2181                 print "<th>" .
2182                       $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
2183                                -class => "header"}, "Last Change") .
2184                       "</th>\n";
2185         }
2186         print "<th></th>\n" .
2187               "</tr>\n";
2188         my $alternate = 0;
2189         foreach my $pr (@projects) {
2190                 if ($alternate) {
2191                         print "<tr class=\"dark\">\n";
2192                 } else {
2193                         print "<tr class=\"light\">\n";
2194                 }
2195                 $alternate ^= 1;
2196                 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2197                                         -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2198                       "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2199                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2200                 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2201                       $pr->{'commit'}{'age_string'} . "</td>\n" .
2202                       "<td class=\"link\">" .
2203                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
2204                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2205                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2206                       "</td>\n" .
2207                       "</tr>\n";
2208         }
2209         print "</table>\n";
2210         git_footer_html();
2213 sub git_summary {
2214         my $descr = git_get_project_description($project) || "none";
2215         my $head = git_get_head_hash($project);
2216         my %co = parse_commit($head);
2217         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2219         my $owner = git_get_project_owner($project);
2221         my $refs = git_get_references();
2222         git_header_html();
2223         git_print_page_nav('summary','', $head);
2225         print "<div class=\"title\">&nbsp;</div>\n";
2226         print "<table cellspacing=\"0\">\n" .
2227               "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2228               "<tr><td>owner</td><td>$owner</td></tr>\n" .
2229               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2230         # use per project git URL list in $projectroot/$project/cloneurl
2231         # or make project git URL from git base URL and project name
2232         my $url_tag = "URL";
2233         my @url_list = git_get_project_url_list($project);
2234         @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2235         foreach my $git_url (@url_list) {
2236                 next unless $git_url;
2237                 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2238                 $url_tag = "";
2239         }
2240         print "</table>\n";
2242         open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2243                 git_get_head_hash($project)
2244                 or die_error(undef, "Open git-rev-list failed");
2245         my @revlist = map { chomp; $_ } <$fd>;
2246         close $fd;
2247         git_print_header_div('shortlog');
2248         git_shortlog_body(\@revlist, 0, 15, $refs,
2249                           $cgi->a({-href => href(action=>"shortlog")}, "..."));
2251         my $taglist = git_get_refs_list("refs/tags");
2252         if (defined @$taglist) {
2253                 git_print_header_div('tags');
2254                 git_tags_body($taglist, 0, 15,
2255                               $cgi->a({-href => href(action=>"tags")}, "..."));
2256         }
2258         my $headlist = git_get_refs_list("refs/heads");
2259         if (defined @$headlist) {
2260                 git_print_header_div('heads');
2261                 git_heads_body($headlist, $head, 0, 15,
2262                                $cgi->a({-href => href(action=>"heads")}, "..."));
2263         }
2265         git_footer_html();
2268 sub git_tag {
2269         my $head = git_get_head_hash($project);
2270         git_header_html();
2271         git_print_page_nav('','', $head,undef,$head);
2272         my %tag = parse_tag($hash);
2273         git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2274         print "<div class=\"title_text\">\n" .
2275               "<table cellspacing=\"0\">\n" .
2276               "<tr>\n" .
2277               "<td>object</td>\n" .
2278               "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2279                                $tag{'object'}) . "</td>\n" .
2280               "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2281                                               $tag{'type'}) . "</td>\n" .
2282               "</tr>\n";
2283         if (defined($tag{'author'})) {
2284                 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2285                 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2286                 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2287                         sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2288                         "</td></tr>\n";
2289         }
2290         print "</table>\n\n" .
2291               "</div>\n";
2292         print "<div class=\"page_body\">";
2293         my $comment = $tag{'comment'};
2294         foreach my $line (@$comment) {
2295                 print esc_html($line) . "<br/>\n";
2296         }
2297         print "</div>\n";
2298         git_footer_html();
2301 sub git_blame2 {
2302         my $fd;
2303         my $ftype;
2305         my ($have_blame) = gitweb_check_feature('blame');
2306         if (!$have_blame) {
2307                 die_error('403 Permission denied', "Permission denied");
2308         }
2309         die_error('404 Not Found', "File name not defined") if (!$file_name);
2310         $hash_base ||= git_get_head_hash($project);
2311         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2312         my %co = parse_commit($hash_base)
2313                 or die_error(undef, "Reading commit failed");
2314         if (!defined $hash) {
2315                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2316                         or die_error(undef, "Error looking up file");
2317         }
2318         $ftype = git_get_type($hash);
2319         if ($ftype !~ "blob") {
2320                 die_error("400 Bad Request", "Object is not a blob");
2321         }
2322         open ($fd, "-|", git_cmd(), "blame", '-l', $file_name, $hash_base)
2323                 or die_error(undef, "Open git-blame failed");
2324         git_header_html();
2325         my $formats_nav =
2326                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2327                         "blob") .
2328                 " | " .
2329                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2330                         "head");
2331         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2332         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2333         git_print_page_path($file_name, $ftype, $hash_base);
2334         my @rev_color = (qw(light2 dark2));
2335         my $num_colors = scalar(@rev_color);
2336         my $current_color = 0;
2337         my $last_rev;
2338         print <<HTML;
2339 <div class="page_body">
2340 <table class="blame">
2341 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2342 HTML
2343         while (<$fd>) {
2344                 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2345                 my $full_rev = $1;
2346                 my $rev = substr($full_rev, 0, 8);
2347                 my $lineno = $2;
2348                 my $data = $3;
2350                 if (!defined $last_rev) {
2351                         $last_rev = $full_rev;
2352                 } elsif ($last_rev ne $full_rev) {
2353                         $last_rev = $full_rev;
2354                         $current_color = ++$current_color % $num_colors;
2355                 }
2356                 print "<tr class=\"$rev_color[$current_color]\">\n";
2357                 print "<td class=\"sha1\">" .
2358                         $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2359                                 esc_html($rev)) . "</td>\n";
2360                 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2361                       esc_html($lineno) . "</a></td>\n";
2362                 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2363                 print "</tr>\n";
2364         }
2365         print "</table>\n";
2366         print "</div>";
2367         close $fd
2368                 or print "Reading blob failed\n";
2369         git_footer_html();
2372 sub git_blame {
2373         my $fd;
2375         my ($have_blame) = gitweb_check_feature('blame');
2376         if (!$have_blame) {
2377                 die_error('403 Permission denied', "Permission denied");
2378         }
2379         die_error('404 Not Found', "File name not defined") if (!$file_name);
2380         $hash_base ||= git_get_head_hash($project);
2381         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2382         my %co = parse_commit($hash_base)
2383                 or die_error(undef, "Reading commit failed");
2384         if (!defined $hash) {
2385                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2386                         or die_error(undef, "Error lookup file");
2387         }
2388         open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2389                 or die_error(undef, "Open git-annotate failed");
2390         git_header_html();
2391         my $formats_nav =
2392                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2393                         "blob") .
2394                 " | " .
2395                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2396                         "head");
2397         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2398         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2399         git_print_page_path($file_name, 'blob', $hash_base);
2400         print "<div class=\"page_body\">\n";
2401         print <<HTML;
2402 <table class="blame">
2403   <tr>
2404     <th>Commit</th>
2405     <th>Age</th>
2406     <th>Author</th>
2407     <th>Line</th>
2408     <th>Data</th>
2409   </tr>
2410 HTML
2411         my @line_class = (qw(light dark));
2412         my $line_class_len = scalar (@line_class);
2413         my $line_class_num = $#line_class;
2414         while (my $line = <$fd>) {
2415                 my $long_rev;
2416                 my $short_rev;
2417                 my $author;
2418                 my $time;
2419                 my $lineno;
2420                 my $data;
2421                 my $age;
2422                 my $age_str;
2423                 my $age_class;
2425                 chomp $line;
2426                 $line_class_num = ($line_class_num + 1) % $line_class_len;
2428                 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2429                         $long_rev = $1;
2430                         $author   = $2;
2431                         $time     = $3;
2432                         $lineno   = $4;
2433                         $data     = $5;
2434                 } else {
2435                         print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2436                         next;
2437                 }
2438                 $short_rev  = substr ($long_rev, 0, 8);
2439                 $age        = time () - $time;
2440                 $age_str    = age_string ($age);
2441                 $age_str    =~ s/ /&nbsp;/g;
2442                 $age_class  = age_class($age);
2443                 $author     = esc_html ($author);
2444                 $author     =~ s/ /&nbsp;/g;
2446                 $data = untabify($data);
2447                 $data = esc_html ($data);
2449                 print <<HTML;
2450   <tr class="$line_class[$line_class_num]">
2451     <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2452     <td class="$age_class">$age_str</td>
2453     <td>$author</td>
2454     <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2455     <td class="pre">$data</td>
2456   </tr>
2457 HTML
2458         } # while (my $line = <$fd>)
2459         print "</table>\n\n";
2460         close $fd
2461                 or print "Reading blob failed.\n";
2462         print "</div>";
2463         git_footer_html();
2466 sub git_tags {
2467         my $head = git_get_head_hash($project);
2468         git_header_html();
2469         git_print_page_nav('','', $head,undef,$head);
2470         git_print_header_div('summary', $project);
2472         my $taglist = git_get_refs_list("refs/tags");
2473         if (defined @$taglist) {
2474                 git_tags_body($taglist);
2475         }
2476         git_footer_html();
2479 sub git_heads {
2480         my $head = git_get_head_hash($project);
2481         git_header_html();
2482         git_print_page_nav('','', $head,undef,$head);
2483         git_print_header_div('summary', $project);
2485         my $taglist = git_get_refs_list("refs/heads");
2486         if (defined @$taglist) {
2487                 git_heads_body($taglist, $head);
2488         }
2489         git_footer_html();
2492 sub git_blob_plain {
2493         # blobs defined by non-textual hash id's can be cached
2494         my $expires;
2495         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2496                 $expires = "+1d";
2497         }
2499         if (!defined $hash) {
2500                 if (defined $file_name) {
2501                         my $base = $hash_base || git_get_head_hash($project);
2502                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2503                                 or die_error(undef, "Error lookup file");
2504                 } else {
2505                         die_error(undef, "No file name defined");
2506                 }
2507         }
2508         my $type = shift;
2509         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2510                 or die_error(undef, "Couldn't cat $file_name, $hash");
2512         $type ||= blob_mimetype($fd, $file_name);
2514         # save as filename, even when no $file_name is given
2515         my $save_as = "$hash";
2516         if (defined $file_name) {
2517                 $save_as = $file_name;
2518         } elsif ($type =~ m/^text\//) {
2519                 $save_as .= '.txt';
2520         }
2522         print $cgi->header(
2523                 -type => "$type",
2524                 -expires=>$expires,
2525                 -content_disposition => "inline; filename=\"$save_as\"");
2526         undef $/;
2527         binmode STDOUT, ':raw';
2528         print <$fd>;
2529         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2530         $/ = "\n";
2531         close $fd;
2534 sub git_blob {
2535         # blobs defined by non-textual hash id's can be cached
2536         my $expires;
2537         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2538                 $expires = "+1d";
2539         }
2541         if (!defined $hash) {
2542                 if (defined $file_name) {
2543                         my $base = $hash_base || git_get_head_hash($project);
2544                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2545                                 or die_error(undef, "Error lookup file");
2546                 } else {
2547                         die_error(undef, "No file name defined");
2548                 }
2549         }
2550         my ($have_blame) = gitweb_check_feature('blame');
2551         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2552                 or die_error(undef, "Couldn't cat $file_name, $hash");
2553         my $mimetype = blob_mimetype($fd, $file_name);
2554         if ($mimetype !~ m/^text\//) {
2555                 close $fd;
2556                 return git_blob_plain($mimetype);
2557         }
2558         git_header_html(undef, $expires);
2559         my $formats_nav = '';
2560         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2561                 if (defined $file_name) {
2562                         if ($have_blame) {
2563                                 $formats_nav .=
2564                                         $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2565                                                                hash=>$hash, file_name=>$file_name)},
2566                                                 "blame") .
2567                                         " | ";
2568                         }
2569                         $formats_nav .=
2570                                 $cgi->a({-href => href(action=>"blob_plain",
2571                                                        hash=>$hash, file_name=>$file_name)},
2572                                         "plain") .
2573                                 " | " .
2574                                 $cgi->a({-href => href(action=>"blob",
2575                                                        hash_base=>"HEAD", file_name=>$file_name)},
2576                                         "head");
2577                 } else {
2578                         $formats_nav .=
2579                                 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2580                 }
2581                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2582                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2583         } else {
2584                 print "<div class=\"page_nav\">\n" .
2585                       "<br/><br/></div>\n" .
2586                       "<div class=\"title\">$hash</div>\n";
2587         }
2588         git_print_page_path($file_name, "blob", $hash_base);
2589         print "<div class=\"page_body\">\n";
2590         my $nr;
2591         while (my $line = <$fd>) {
2592                 chomp $line;
2593                 $nr++;
2594                 $line = untabify($line);
2595                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2596                        $nr, $nr, $nr, esc_html($line);
2597         }
2598         close $fd
2599                 or print "Reading blob failed.\n";
2600         print "</div>";
2601         git_footer_html();
2604 sub git_tree {
2605         if (!defined $hash) {
2606                 $hash = git_get_head_hash($project);
2607                 if (defined $file_name) {
2608                         my $base = $hash_base || $hash;
2609                         $hash = git_get_hash_by_path($base, $file_name, "tree");
2610                 }
2611                 if (!defined $hash_base) {
2612                         $hash_base = $hash;
2613                 }
2614         }
2615         $/ = "\0";
2616         open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2617                 or die_error(undef, "Open git-ls-tree failed");
2618         my @entries = map { chomp; $_ } <$fd>;
2619         close $fd or die_error(undef, "Reading tree failed");
2620         $/ = "\n";
2622         my $refs = git_get_references();
2623         my $ref = format_ref_marker($refs, $hash_base);
2624         git_header_html();
2625         my $base = "";
2626         my ($have_blame) = gitweb_check_feature('blame');
2627         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2628                 git_print_page_nav('tree','', $hash_base);
2629                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2630         } else {
2631                 undef $hash_base;
2632                 print "<div class=\"page_nav\">\n";
2633                 print "<br/><br/></div>\n";
2634                 print "<div class=\"title\">$hash</div>\n";
2635         }
2636         if (defined $file_name) {
2637                 $base = esc_html("$file_name/");
2638         }
2639         git_print_page_path($file_name, 'tree', $hash_base);
2640         print "<div class=\"page_body\">\n";
2641         print "<table cellspacing=\"0\">\n";
2642         my $alternate = 0;
2643         foreach my $line (@entries) {
2644                 my %t = parse_ls_tree_line($line, -z => 1);
2646                 if ($alternate) {
2647                         print "<tr class=\"dark\">\n";
2648                 } else {
2649                         print "<tr class=\"light\">\n";
2650                 }
2651                 $alternate ^= 1;
2653                 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2655                 print "</tr>\n";
2656         }
2657         print "</table>\n" .
2658               "</div>";
2659         git_footer_html();
2662 sub git_snapshot {
2664         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2665         my $have_snapshot = (defined $ctype && defined $suffix);
2666         if (!$have_snapshot) {
2667                 die_error('403 Permission denied', "Permission denied");
2668         }
2670         if (!defined $hash) {
2671                 $hash = git_get_head_hash($project);
2672         }
2674         my $filename = basename($project) . "-$hash.tar.$suffix";
2676         print $cgi->header(-type => 'application/x-tar',
2677                            -content_encoding => $ctype,
2678                            -content_disposition => "inline; filename=\"$filename\"",
2679                            -status => '200 OK');
2681         my $git_command = git_cmd_str();
2682         open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2683                 die_error(undef, "Execute git-tar-tree failed.");
2684         binmode STDOUT, ':raw';
2685         print <$fd>;
2686         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2687         close $fd;
2691 sub git_log {
2692         my $head = git_get_head_hash($project);
2693         if (!defined $hash) {
2694                 $hash = $head;
2695         }
2696         if (!defined $page) {
2697                 $page = 0;
2698         }
2699         my $refs = git_get_references();
2701         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2702         open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2703                 or die_error(undef, "Open git-rev-list failed");
2704         my @revlist = map { chomp; $_ } <$fd>;
2705         close $fd;
2707         my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2709         git_header_html();
2710         git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2712         if (!@revlist) {
2713                 my %co = parse_commit($hash);
2715                 git_print_header_div('summary', $project);
2716                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2717         }
2718         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2719                 my $commit = $revlist[$i];
2720                 my $ref = format_ref_marker($refs, $commit);
2721                 my %co = parse_commit($commit);
2722                 next if !%co;
2723                 my %ad = parse_date($co{'author_epoch'});
2724                 git_print_header_div('commit',
2725                                "<span class=\"age\">$co{'age_string'}</span>" .
2726                                esc_html($co{'title'}) . $ref,
2727                                $commit);
2728                 print "<div class=\"title_text\">\n" .
2729                       "<div class=\"log_link\">\n" .
2730                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2731                       " | " .
2732                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2733                       "<br/>\n" .
2734                       "</div>\n" .
2735                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
2736                       "</div>\n";
2738                 print "<div class=\"log_body\">\n";
2739                 git_print_simplified_log($co{'comment'});
2740                 print "</div>\n";
2741         }
2742         git_footer_html();
2745 sub git_commit {
2746         my %co = parse_commit($hash);
2747         if (!%co) {
2748                 die_error(undef, "Unknown commit object");
2749         }
2750         my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2751         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2753         my $parent = $co{'parent'};
2754         if (!defined $parent) {
2755                 $parent = "--root";
2756         }
2757         open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2758                 or die_error(undef, "Open git-diff-tree failed");
2759         my @difftree = map { chomp; $_ } <$fd>;
2760         close $fd or die_error(undef, "Reading git-diff-tree failed");
2762         # non-textual hash id's can be cached
2763         my $expires;
2764         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2765                 $expires = "+1d";
2766         }
2767         my $refs = git_get_references();
2768         my $ref = format_ref_marker($refs, $co{'id'});
2770         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2771         my $have_snapshot = (defined $ctype && defined $suffix);
2773         my $formats_nav = '';
2774         if (defined $file_name && defined $co{'parent'}) {
2775                 my $parent = $co{'parent'};
2776                 $formats_nav .=
2777                         $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2778                                 "blame");
2779         }
2780         git_header_html(undef, $expires);
2781         git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2782                            $hash, $co{'tree'}, $hash,
2783                            $formats_nav);
2785         if (defined $co{'parent'}) {
2786                 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2787         } else {
2788                 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2789         }
2790         print "<div class=\"title_text\">\n" .
2791               "<table cellspacing=\"0\">\n";
2792         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2793               "<tr>" .
2794               "<td></td><td> $ad{'rfc2822'}";
2795         if ($ad{'hour_local'} < 6) {
2796                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2797                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2798         } else {
2799                 printf(" (%02d:%02d %s)",
2800                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2801         }
2802         print "</td>" .
2803               "</tr>\n";
2804         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2805         print "<tr><td></td><td> $cd{'rfc2822'}" .
2806               sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2807               "</td></tr>\n";
2808         print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2809         print "<tr>" .
2810               "<td>tree</td>" .
2811               "<td class=\"sha1\">" .
2812               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2813                        class => "list"}, $co{'tree'}) .
2814               "</td>" .
2815               "<td class=\"link\">" .
2816               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2817                       "tree");
2818         if ($have_snapshot) {
2819                 print " | " .
2820                       $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2821         }
2822         print "</td>" .
2823               "</tr>\n";
2824         my $parents = $co{'parents'};
2825         foreach my $par (@$parents) {
2826                 print "<tr>" .
2827                       "<td>parent</td>" .
2828                       "<td class=\"sha1\">" .
2829                       $cgi->a({-href => href(action=>"commit", hash=>$par),
2830                                class => "list"}, $par) .
2831                       "</td>" .
2832                       "<td class=\"link\">" .
2833                       $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2834                       " | " .
2835                       $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
2836                       "</td>" .
2837                       "</tr>\n";
2838         }
2839         print "</table>".
2840               "</div>\n";
2842         print "<div class=\"page_body\">\n";
2843         git_print_log($co{'comment'});
2844         print "</div>\n";
2846         git_difftree_body(\@difftree, $hash, $parent);
2848         git_footer_html();
2851 sub git_blobdiff {
2852         my $format = shift || 'html';
2854         my $fd;
2855         my @difftree;
2856         my %diffinfo;
2857         my $expires;
2859         # preparing $fd and %diffinfo for git_patchset_body
2860         # new style URI
2861         if (defined $hash_base && defined $hash_parent_base) {
2862                 if (defined $file_name) {
2863                         # read raw output
2864                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
2865                                 "--", $file_name
2866                                 or die_error(undef, "Open git-diff-tree failed");
2867                         @difftree = map { chomp; $_ } <$fd>;
2868                         close $fd
2869                                 or die_error(undef, "Reading git-diff-tree failed");
2870                         @difftree
2871                                 or die_error('404 Not Found', "Blob diff not found");
2873                 } elsif (defined $hash &&
2874                          $hash =~ /[0-9a-fA-F]{40}/) {
2875                         # try to find filename from $hash
2877                         # read filtered raw output
2878                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
2879                                 or die_error(undef, "Open git-diff-tree failed");
2880                         @difftree =
2881                                 # ':100644 100644 03b21826... 3b93d5e7... M     ls-files.c'
2882                                 # $hash == to_id
2883                                 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
2884                                 map { chomp; $_ } <$fd>;
2885                         close $fd
2886                                 or die_error(undef, "Reading git-diff-tree failed");
2887                         @difftree
2888                                 or die_error('404 Not Found', "Blob diff not found");
2890                 } else {
2891                         die_error('404 Not Found', "Missing one of the blob diff parameters");
2892                 }
2894                 if (@difftree > 1) {
2895                         die_error('404 Not Found', "Ambiguous blob diff specification");
2896                 }
2898                 %diffinfo = parse_difftree_raw_line($difftree[0]);
2899                 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
2900                 $file_name   ||= $diffinfo{'to_file'}   || $diffinfo{'file'};
2902                 $hash_parent ||= $diffinfo{'from_id'};
2903                 $hash        ||= $diffinfo{'to_id'};
2905                 # non-textual hash id's can be cached
2906                 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
2907                     $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
2908                         $expires = '+1d';
2909                 }
2911                 # open patch output
2912                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2913                         '-p', $hash_parent_base, $hash_base,
2914                         "--", $file_name
2915                         or die_error(undef, "Open git-diff-tree failed");
2916         }
2918         # old/legacy style URI
2919         if (!%diffinfo && # if new style URI failed
2920             defined $hash && defined $hash_parent) {
2921                 # fake git-diff-tree raw output
2922                 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
2923                 $diffinfo{'from_id'} = $hash_parent;
2924                 $diffinfo{'to_id'}   = $hash;
2925                 if (defined $file_name) {
2926                         if (defined $file_parent) {
2927                                 $diffinfo{'status'} = '2';
2928                                 $diffinfo{'from_file'} = $file_parent;
2929                                 $diffinfo{'to_file'}   = $file_name;
2930                         } else { # assume not renamed
2931                                 $diffinfo{'status'} = '1';
2932                                 $diffinfo{'from_file'} = $file_name;
2933                                 $diffinfo{'to_file'}   = $file_name;
2934                         }
2935                 } else { # no filename given
2936                         $diffinfo{'status'} = '2';
2937                         $diffinfo{'from_file'} = $hash_parent;
2938                         $diffinfo{'to_file'}   = $hash;
2939                 }
2941                 # non-textual hash id's can be cached
2942                 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
2943                     $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
2944                         $expires = '+1d';
2945                 }
2947                 # open patch output
2948                 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
2949                         or die_error(undef, "Open git-diff failed");
2950         } else  {
2951                 die_error('404 Not Found', "Missing one of the blob diff parameters")
2952                         unless %diffinfo;
2953         }
2955         # header
2956         if ($format eq 'html') {
2957                 my $formats_nav =
2958                         $cgi->a({-href => href(action=>"blobdiff_plain",
2959                                                hash=>$hash, hash_parent=>$hash_parent,
2960                                                hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
2961                                                file_name=>$file_name, file_parent=>$file_parent)},
2962                                 "plain");
2963                 git_header_html(undef, $expires);
2964                 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2965                         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2966                         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2967                 } else {
2968                         print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
2969                         print "<div class=\"title\">$hash vs $hash_parent</div>\n";
2970                 }
2971                 if (defined $file_name) {
2972                         git_print_page_path($file_name, "blob", $hash_base);
2973                 } else {
2974                         print "<div class=\"page_path\"></div>\n";
2975                 }
2977         } elsif ($format eq 'plain') {
2978                 print $cgi->header(
2979                         -type => 'text/plain',
2980                         -charset => 'utf-8',
2981                         -expires => $expires,
2982                         -content_disposition => qq(inline; filename="${file_name}.patch"));
2984                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2986         } else {
2987                 die_error(undef, "Unknown blobdiff format");
2988         }
2990         # patch
2991         if ($format eq 'html') {
2992                 print "<div class=\"page_body\">\n";
2994                 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
2995                 close $fd;
2997                 print "</div>\n"; # class="page_body"
2998                 git_footer_html();
3000         } else {
3001                 while (my $line = <$fd>) {
3002                         $line =~ s!a/($hash|$hash_parent)!a/$diffinfo{'from_file'}!g;
3003                         $line =~ s!b/($hash|$hash_parent)!b/$diffinfo{'to_file'}!g;
3005                         print $line;
3007                         last if $line =~ m!^\+\+\+!;
3008                 }
3009                 local $/ = undef;
3010                 print <$fd>;
3011                 close $fd;
3012         }
3015 sub git_blobdiff_plain {
3016         git_blobdiff('plain');
3019 sub git_commitdiff {
3020         my $format = shift || 'html';
3021         my %co = parse_commit($hash);
3022         if (!%co) {
3023                 die_error(undef, "Unknown commit object");
3024         }
3025         if (!defined $hash_parent) {
3026                 $hash_parent = $co{'parent'} || '--root';
3027         }
3029         # read commitdiff
3030         my $fd;
3031         my @difftree;
3032         if ($format eq 'html') {
3033                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3034                         "--patch-with-raw", "--full-index", $hash_parent, $hash
3035                         or die_error(undef, "Open git-diff-tree failed");
3037                 while (chomp(my $line = <$fd>)) {
3038                         # empty line ends raw part of diff-tree output
3039                         last unless $line;
3040                         push @difftree, $line;
3041                 }
3043         } elsif ($format eq 'plain') {
3044                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3045                         '-p', $hash_parent, $hash
3046                         or die_error(undef, "Open git-diff-tree failed");
3048         } else {
3049                 die_error(undef, "Unknown commitdiff format");
3050         }
3052         # non-textual hash id's can be cached
3053         my $expires;
3054         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3055                 $expires = "+1d";
3056         }
3058         # write commit message
3059         if ($format eq 'html') {
3060                 my $refs = git_get_references();
3061                 my $ref = format_ref_marker($refs, $co{'id'});
3062                 my $formats_nav =
3063                         $cgi->a({-href => href(action=>"commitdiff_plain",
3064                                                hash=>$hash, hash_parent=>$hash_parent)},
3065                                 "plain");
3067                 git_header_html(undef, $expires);
3068                 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3069                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3070                 git_print_authorship(\%co);
3071                 print "<div class=\"page_body\">\n";
3072                 print "<div class=\"log\">\n";
3073                 git_print_simplified_log($co{'comment'}, 1); # skip title
3074                 print "</div>\n"; # class="log"
3076         } elsif ($format eq 'plain') {
3077                 my $refs = git_get_references("tags");
3078                 my $tagname = git_get_rev_name_tags($hash);
3079                 my $filename = basename($project) . "-$hash.patch";
3081                 print $cgi->header(
3082                         -type => 'text/plain',
3083                         -charset => 'utf-8',
3084                         -expires => $expires,
3085                         -content_disposition => qq(inline; filename="$filename"));
3086                 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3087                 print <<TEXT;
3088 From: $co{'author'}
3089 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3090 Subject: $co{'title'}
3091 TEXT
3092                 print "X-Git-Tag: $tagname\n" if $tagname;
3093                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3095                 foreach my $line (@{$co{'comment'}}) {
3096                         print "$line\n";
3097                 }
3098                 print "---\n\n";
3099         }
3101         # write patch
3102         if ($format eq 'html') {
3103                 git_difftree_body(\@difftree, $hash, $hash_parent);
3104                 print "<br/>\n";
3106                 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3107                 close $fd;
3108                 print "</div>\n"; # class="page_body"
3109                 git_footer_html();
3111         } elsif ($format eq 'plain') {
3112                 local $/ = undef;
3113                 print <$fd>;
3114                 close $fd
3115                         or print "Reading git-diff-tree failed\n";
3116         }
3119 sub git_commitdiff_plain {
3120         git_commitdiff('plain');
3123 sub git_history {
3124         if (!defined $hash_base) {
3125                 $hash_base = git_get_head_hash($project);
3126         }
3127         if (!defined $page) {
3128                 $page = 0;
3129         }
3130         my $ftype;
3131         my %co = parse_commit($hash_base);
3132         if (!%co) {
3133                 die_error(undef, "Unknown commit object");
3134         }
3136         my $refs = git_get_references();
3137         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3139         if (!defined $hash && defined $file_name) {
3140                 $hash = git_get_hash_by_path($hash_base, $file_name);
3141         }
3142         if (defined $hash) {
3143                 $ftype = git_get_type($hash);
3144         }
3146         open my $fd, "-|",
3147                 git_cmd(), "rev-list", $limit, "--full-history", $hash_base, "--", $file_name
3148                         or die_error(undef, "Open git-rev-list-failed");
3149         my @revlist = map { chomp; $_ } <$fd>;
3150         close $fd
3151                 or die_error(undef, "Reading git-rev-list failed");
3153         my $paging_nav = '';
3154         if ($page > 0) {
3155                 $paging_nav .=
3156                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3157                                                file_name=>$file_name)},
3158                                 "first");
3159                 $paging_nav .= " &sdot; " .
3160                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3161                                                file_name=>$file_name, page=>$page-1),
3162                                  -accesskey => "p", -title => "Alt-p"}, "prev");
3163         } else {
3164                 $paging_nav .= "first";
3165                 $paging_nav .= " &sdot; prev";
3166         }
3167         if ($#revlist >= (100 * ($page+1)-1)) {
3168                 $paging_nav .= " &sdot; " .
3169                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3170                                                file_name=>$file_name, page=>$page+1),
3171                                  -accesskey => "n", -title => "Alt-n"}, "next");
3172         } else {
3173                 $paging_nav .= " &sdot; next";
3174         }
3175         my $next_link = '';
3176         if ($#revlist >= (100 * ($page+1)-1)) {
3177                 $next_link =
3178                         $cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
3179                                                file_name=>$file_name, page=>$page+1),
3180                                  -title => "Alt-n"}, "next");
3181         }
3183         git_header_html();
3184         git_print_page_nav('history','', $hash_base,$co{'tree'},$hash_base, $paging_nav);
3185         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3186         git_print_page_path($file_name, $ftype, $hash_base);
3188         git_history_body(\@revlist, ($page * 100), $#revlist,
3189                          $refs, $hash_base, $ftype, $next_link);
3191         git_footer_html();
3194 sub git_search {
3195         if (!defined $searchtext) {
3196                 die_error(undef, "Text field empty");
3197         }
3198         if (!defined $hash) {
3199                 $hash = git_get_head_hash($project);
3200         }
3201         my %co = parse_commit($hash);
3202         if (!%co) {
3203                 die_error(undef, "Unknown commit object");
3204         }
3206         my $commit_search = 1;
3207         my $author_search = 0;
3208         my $committer_search = 0;
3209         my $pickaxe_search = 0;
3210         if ($searchtext =~ s/^author\\://i) {
3211                 $author_search = 1;
3212         } elsif ($searchtext =~ s/^committer\\://i) {
3213                 $committer_search = 1;
3214         } elsif ($searchtext =~ s/^pickaxe\\://i) {
3215                 $commit_search = 0;
3216                 $pickaxe_search = 1;
3218                 # pickaxe may take all resources of your box and run for several minutes
3219                 # with every query - so decide by yourself how public you make this feature
3220                 my ($have_pickaxe) = gitweb_check_feature('pickaxe');
3221                 if (!$have_pickaxe) {
3222                         die_error('403 Permission denied', "Permission denied");
3223                 }
3224         }
3225         git_header_html();
3226         git_print_page_nav('','', $hash,$co{'tree'},$hash);
3227         git_print_header_div('commit', esc_html($co{'title'}), $hash);
3229         print "<table cellspacing=\"0\">\n";
3230         my $alternate = 0;
3231         if ($commit_search) {
3232                 $/ = "\0";
3233                 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3234                 while (my $commit_text = <$fd>) {
3235                         if (!grep m/$searchtext/i, $commit_text) {
3236                                 next;
3237                         }
3238                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3239                                 next;
3240                         }
3241                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3242                                 next;
3243                         }
3244                         my @commit_lines = split "\n", $commit_text;
3245                         my %co = parse_commit(undef, \@commit_lines);
3246                         if (!%co) {
3247                                 next;
3248                         }
3249                         if ($alternate) {
3250                                 print "<tr class=\"dark\">\n";
3251                         } else {
3252                                 print "<tr class=\"light\">\n";
3253                         }
3254                         $alternate ^= 1;
3255                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3256                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3257                               "<td>" .
3258                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3259                                        esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3260                         my $comment = $co{'comment'};
3261                         foreach my $line (@$comment) {
3262                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3263                                         my $lead = esc_html($1) || "";
3264                                         $lead = chop_str($lead, 30, 10);
3265                                         my $match = esc_html($2) || "";
3266                                         my $trail = esc_html($3) || "";
3267                                         $trail = chop_str($trail, 30, 10);
3268                                         my $text = "$lead<span class=\"match\">$match</span>$trail";
3269                                         print chop_str($text, 80, 5) . "<br/>\n";
3270                                 }
3271                         }
3272                         print "</td>\n" .
3273                               "<td class=\"link\">" .
3274                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3275                               " | " .
3276                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3277                         print "</td>\n" .
3278                               "</tr>\n";
3279                 }
3280                 close $fd;
3281         }
3283         if ($pickaxe_search) {
3284                 $/ = "\n";
3285                 my $git_command = git_cmd_str();
3286                 open my $fd, "-|", "$git_command rev-list $hash | " .
3287                         "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3288                 undef %co;
3289                 my @files;
3290                 while (my $line = <$fd>) {
3291                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3292                                 my %set;
3293                                 $set{'file'} = $6;
3294                                 $set{'from_id'} = $3;
3295                                 $set{'to_id'} = $4;
3296                                 $set{'id'} = $set{'to_id'};
3297                                 if ($set{'id'} =~ m/0{40}/) {
3298                                         $set{'id'} = $set{'from_id'};
3299                                 }
3300                                 if ($set{'id'} =~ m/0{40}/) {
3301                                         next;
3302                                 }
3303                                 push @files, \%set;
3304                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3305                                 if (%co) {
3306                                         if ($alternate) {
3307                                                 print "<tr class=\"dark\">\n";
3308                                         } else {
3309                                                 print "<tr class=\"light\">\n";
3310                                         }
3311                                         $alternate ^= 1;
3312                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3313                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3314                                               "<td>" .
3315                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3316                                                       -class => "list subject"},
3317                                                       esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3318                                         while (my $setref = shift @files) {
3319                                                 my %set = %$setref;
3320                                                 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3321                                                                              hash=>$set{'id'}, file_name=>$set{'file'}),
3322                                                               -class => "list"},
3323                                                               "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3324                                                       "<br/>\n";
3325                                         }
3326                                         print "</td>\n" .
3327                                               "<td class=\"link\">" .
3328                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3329                                               " | " .
3330                                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3331                                         print "</td>\n" .
3332                                               "</tr>\n";
3333                                 }
3334                                 %co = parse_commit($1);
3335                         }
3336                 }
3337                 close $fd;
3338         }
3339         print "</table>\n";
3340         git_footer_html();
3343 sub git_shortlog {
3344         my $head = git_get_head_hash($project);
3345         if (!defined $hash) {
3346                 $hash = $head;
3347         }
3348         if (!defined $page) {
3349                 $page = 0;
3350         }
3351         my $refs = git_get_references();
3353         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3354         open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3355                 or die_error(undef, "Open git-rev-list failed");
3356         my @revlist = map { chomp; $_ } <$fd>;
3357         close $fd;
3359         my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3360         my $next_link = '';
3361         if ($#revlist >= (100 * ($page+1)-1)) {
3362                 $next_link =
3363                         $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3364                                  -title => "Alt-n"}, "next");
3365         }
3368         git_header_html();
3369         git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3370         git_print_header_div('summary', $project);
3372         git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3374         git_footer_html();
3377 ## ......................................................................
3378 ## feeds (RSS, OPML)
3380 sub git_rss {
3381         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3382         open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3383                 or die_error(undef, "Open git-rev-list failed");
3384         my @revlist = map { chomp; $_ } <$fd>;
3385         close $fd or die_error(undef, "Reading git-rev-list failed");
3386         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3387         print <<XML;
3388 <?xml version="1.0" encoding="utf-8"?>
3389 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3390 <channel>
3391 <title>$project $my_uri $my_url</title>
3392 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3393 <description>$project log</description>
3394 <language>en</language>
3395 XML
3397         for (my $i = 0; $i <= $#revlist; $i++) {
3398                 my $commit = $revlist[$i];
3399                 my %co = parse_commit($commit);
3400                 # we read 150, we always show 30 and the ones more recent than 48 hours
3401                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3402                         last;
3403                 }
3404                 my %cd = parse_date($co{'committer_epoch'});
3405                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3406                         $co{'parent'}, $co{'id'}
3407                         or next;
3408                 my @difftree = map { chomp; $_ } <$fd>;
3409                 close $fd
3410                         or next;
3411                 print "<item>\n" .
3412                       "<title>" .
3413                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3414                       "</title>\n" .
3415                       "<author>" . esc_html($co{'author'}) . "</author>\n" .
3416                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3417                       "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3418                       "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3419                       "<description>" . esc_html($co{'title'}) . "</description>\n" .
3420                       "<content:encoded>" .
3421                       "<![CDATA[\n";
3422                 my $comment = $co{'comment'};
3423                 foreach my $line (@$comment) {
3424                         $line = decode("utf8", $line, Encode::FB_DEFAULT);
3425                         print "$line<br/>\n";
3426                 }
3427                 print "<br/>\n";
3428                 foreach my $line (@difftree) {
3429                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3430                                 next;
3431                         }
3432                         my $file = validate_input(unquote($7));
3433                         $file = decode("utf8", $file, Encode::FB_DEFAULT);
3434                         print "$file<br/>\n";
3435                 }
3436                 print "]]>\n" .
3437                       "</content:encoded>\n" .
3438                       "</item>\n";
3439         }
3440         print "</channel></rss>";
3443 sub git_opml {
3444         my @list = git_get_projects_list();
3446         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3447         print <<XML;
3448 <?xml version="1.0" encoding="utf-8"?>
3449 <opml version="1.0">
3450 <head>
3451   <title>$site_name Git OPML Export</title>
3452 </head>
3453 <body>
3454 <outline text="git RSS feeds">
3455 XML
3457         foreach my $pr (@list) {
3458                 my %proj = %$pr;
3459                 my $head = git_get_head_hash($proj{'path'});
3460                 if (!defined $head) {
3461                         next;
3462                 }
3463                 $git_dir = "$projectroot/$proj{'path'}";
3464                 my %co = parse_commit($head);
3465                 if (!%co) {
3466                         next;
3467                 }
3469                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3470                 my $rss  = "$my_url?p=$proj{'path'};a=rss";
3471                 my $html = "$my_url?p=$proj{'path'};a=summary";
3472                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3473         }
3474         print <<XML;
3475 </outline>
3476 </body>
3477 </opml>
3478 XML