Code

gitweb: Add GIT favicon, assuming image/png type
[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']},
93 );
95 sub gitweb_check_feature {
96         my ($name) = @_;
97         return undef unless exists $feature{$name};
98         my ($sub, $override, @defaults) = (
99                 $feature{$name}{'sub'},
100                 $feature{$name}{'override'},
101                 @{$feature{$name}{'default'}});
102         if (!$override) { return @defaults; }
103         return $sub->(@defaults);
106 # To enable system wide have in $GITWEB_CONFIG
107 # $feature{'blame'}{'default'} = [1];
108 # To have project specific config enable override in $GITWEB_CONFIG
109 # $feature{'blame'}{'override'} = 1;
110 # and in project config gitweb.blame = 0|1;
112 sub feature_blame {
113         my ($val) = git_get_project_config('blame', '--bool');
115         if ($val eq 'true') {
116                 return 1;
117         } elsif ($val eq 'false') {
118                 return 0;
119         }
121         return $_[0];
124 # To disable system wide have in $GITWEB_CONFIG
125 # $feature{'snapshot'}{'default'} = [undef];
126 # To have project specific config enable override in $GITWEB_CONFIG
127 # $feature{'blame'}{'override'} = 1;
128 # and in project config  gitweb.snapshot = none|gzip|bzip2
130 sub feature_snapshot {
131         my ($ctype, $suffix, $command) = @_;
133         my ($val) = git_get_project_config('snapshot');
135         if ($val eq 'gzip') {
136                 return ('x-gzip', 'gz', 'gzip');
137         } elsif ($val eq 'bzip2') {
138                 return ('x-bzip2', 'bz2', 'bzip2');
139         } elsif ($val eq 'none') {
140                 return ();
141         }
143         return ($ctype, $suffix, $command);
146 # rename detection options for git-diff and git-diff-tree
147 # - default is '-M', with the cost proportional to
148 #   (number of removed files) * (number of new files).
149 # - more costly is '-C' (or '-C', '-M'), with the cost proportional to
150 #   (number of changed files + number of removed files) * (number of new files)
151 # - even more costly is '-C', '--find-copies-harder' with cost
152 #   (number of files in the original tree) * (number of new files)
153 # - one might want to include '-B' option, e.g. '-B', '-M'
154 our @diff_opts = ('-M'); # taken from git_commit
156 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
157 do $GITWEB_CONFIG if -e $GITWEB_CONFIG;
159 # version of the core git binary
160 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
162 # path to the current git repository
163 our $git_dir;
165 $projects_list ||= $projectroot;
167 # ======================================================================
168 # input validation and dispatch
169 our $action = $cgi->param('a');
170 if (defined $action) {
171         if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
172                 die_error(undef, "Invalid action parameter");
173         }
176 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
177 if (defined $project) {
178         $project =~ s|^/||;
179         $project =~ s|/$||;
180         $project = undef unless $project;
182 if (defined $project) {
183         if (!validate_input($project)) {
184                 die_error(undef, "Invalid project parameter");
185         }
186         if (!(-d "$projectroot/$project")) {
187                 die_error(undef, "No such directory");
188         }
189         if (!(-e "$projectroot/$project/HEAD")) {
190                 die_error(undef, "No such project");
191         }
192         $git_dir = "$projectroot/$project";
195 our $file_name = $cgi->param('f');
196 if (defined $file_name) {
197         if (!validate_input($file_name)) {
198                 die_error(undef, "Invalid file parameter");
199         }
202 our $file_parent = $cgi->param('fp');
203 if (defined $file_parent) {
204         if (!validate_input($file_parent)) {
205                 die_error(undef, "Invalid file parent parameter");
206         }
209 our $hash = $cgi->param('h');
210 if (defined $hash) {
211         if (!validate_input($hash)) {
212                 die_error(undef, "Invalid hash parameter");
213         }
216 our $hash_parent = $cgi->param('hp');
217 if (defined $hash_parent) {
218         if (!validate_input($hash_parent)) {
219                 die_error(undef, "Invalid hash parent parameter");
220         }
223 our $hash_base = $cgi->param('hb');
224 if (defined $hash_base) {
225         if (!validate_input($hash_base)) {
226                 die_error(undef, "Invalid hash base parameter");
227         }
230 our $hash_parent_base = $cgi->param('hpb');
231 if (defined $hash_parent_base) {
232         if (!validate_input($hash_parent_base)) {
233                 die_error(undef, "Invalid hash parent base parameter");
234         }
237 our $page = $cgi->param('pg');
238 if (defined $page) {
239         if ($page =~ m/[^0-9]$/) {
240                 die_error(undef, "Invalid page parameter");
241         }
244 our $searchtext = $cgi->param('s');
245 if (defined $searchtext) {
246         if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
247                 die_error(undef, "Invalid search parameter");
248         }
249         $searchtext = quotemeta $searchtext;
252 # dispatch
253 my %actions = (
254         "blame" => \&git_blame2,
255         "blobdiff" => \&git_blobdiff,
256         "blobdiff_plain" => \&git_blobdiff_plain,
257         "blob" => \&git_blob,
258         "blob_plain" => \&git_blob_plain,
259         "commitdiff" => \&git_commitdiff,
260         "commitdiff_plain" => \&git_commitdiff_plain,
261         "commit" => \&git_commit,
262         "heads" => \&git_heads,
263         "history" => \&git_history,
264         "log" => \&git_log,
265         "rss" => \&git_rss,
266         "search" => \&git_search,
267         "shortlog" => \&git_shortlog,
268         "summary" => \&git_summary,
269         "tag" => \&git_tag,
270         "tags" => \&git_tags,
271         "tree" => \&git_tree,
272         "snapshot" => \&git_snapshot,
273         # those below don't need $project
274         "opml" => \&git_opml,
275         "project_list" => \&git_project_list,
276 );
278 if (defined $project) {
279         $action ||= 'summary';
280 } else {
281         $action ||= 'project_list';
283 if (!defined($actions{$action})) {
284         die_error(undef, "Unknown action");
286 $actions{$action}->();
287 exit;
289 ## ======================================================================
290 ## action links
292 sub href(%) {
293         my %params = @_;
295         my @mapping = (
296                 project => "p",
297                 action => "a",
298                 file_name => "f",
299                 file_parent => "fp",
300                 hash => "h",
301                 hash_parent => "hp",
302                 hash_base => "hb",
303                 hash_parent_base => "hpb",
304                 page => "pg",
305                 searchtext => "s",
306         );
307         my %mapping = @mapping;
309         $params{"project"} ||= $project;
311         my @result = ();
312         for (my $i = 0; $i < @mapping; $i += 2) {
313                 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
314                 if (defined $params{$name}) {
315                         push @result, $symbol . "=" . esc_param($params{$name});
316                 }
317         }
318         return "$my_uri?" . join(';', @result);
322 ## ======================================================================
323 ## validation, quoting/unquoting and escaping
325 sub validate_input {
326         my $input = shift;
328         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
329                 return $input;
330         }
331         if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
332                 return undef;
333         }
334         if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
335                 return undef;
336         }
337         return $input;
340 # quote unsafe chars, but keep the slash, even when it's not
341 # correct, but quoted slashes look too horrible in bookmarks
342 sub esc_param {
343         my $str = shift;
344         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
345         $str =~ s/\+/%2B/g;
346         $str =~ s/ /\+/g;
347         return $str;
350 # replace invalid utf8 character with SUBSTITUTION sequence
351 sub esc_html {
352         my $str = shift;
353         $str = decode("utf8", $str, Encode::FB_DEFAULT);
354         $str = escapeHTML($str);
355         $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
356         return $str;
359 # git may return quoted and escaped filenames
360 sub unquote {
361         my $str = shift;
362         if ($str =~ m/^"(.*)"$/) {
363                 $str = $1;
364                 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
365         }
366         return $str;
369 # escape tabs (convert tabs to spaces)
370 sub untabify {
371         my $line = shift;
373         while ((my $pos = index($line, "\t")) != -1) {
374                 if (my $count = (8 - ($pos % 8))) {
375                         my $spaces = ' ' x $count;
376                         $line =~ s/\t/$spaces/;
377                 }
378         }
380         return $line;
383 ## ----------------------------------------------------------------------
384 ## HTML aware string manipulation
386 sub chop_str {
387         my $str = shift;
388         my $len = shift;
389         my $add_len = shift || 10;
391         # allow only $len chars, but don't cut a word if it would fit in $add_len
392         # if it doesn't fit, cut it if it's still longer than the dots we would add
393         $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
394         my $body = $1;
395         my $tail = $2;
396         if (length($tail) > 4) {
397                 $tail = " ...";
398                 $body =~ s/&[^;]*$//; # remove chopped character entities
399         }
400         return "$body$tail";
403 ## ----------------------------------------------------------------------
404 ## functions returning short strings
406 # CSS class for given age value (in seconds)
407 sub age_class {
408         my $age = shift;
410         if ($age < 60*60*2) {
411                 return "age0";
412         } elsif ($age < 60*60*24*2) {
413                 return "age1";
414         } else {
415                 return "age2";
416         }
419 # convert age in seconds to "nn units ago" string
420 sub age_string {
421         my $age = shift;
422         my $age_str;
424         if ($age > 60*60*24*365*2) {
425                 $age_str = (int $age/60/60/24/365);
426                 $age_str .= " years ago";
427         } elsif ($age > 60*60*24*(365/12)*2) {
428                 $age_str = int $age/60/60/24/(365/12);
429                 $age_str .= " months ago";
430         } elsif ($age > 60*60*24*7*2) {
431                 $age_str = int $age/60/60/24/7;
432                 $age_str .= " weeks ago";
433         } elsif ($age > 60*60*24*2) {
434                 $age_str = int $age/60/60/24;
435                 $age_str .= " days ago";
436         } elsif ($age > 60*60*2) {
437                 $age_str = int $age/60/60;
438                 $age_str .= " hours ago";
439         } elsif ($age > 60*2) {
440                 $age_str = int $age/60;
441                 $age_str .= " min ago";
442         } elsif ($age > 2) {
443                 $age_str = int $age;
444                 $age_str .= " sec ago";
445         } else {
446                 $age_str .= " right now";
447         }
448         return $age_str;
451 # convert file mode in octal to symbolic file mode string
452 sub mode_str {
453         my $mode = oct shift;
455         if (S_ISDIR($mode & S_IFMT)) {
456                 return 'drwxr-xr-x';
457         } elsif (S_ISLNK($mode)) {
458                 return 'lrwxrwxrwx';
459         } elsif (S_ISREG($mode)) {
460                 # git cares only about the executable bit
461                 if ($mode & S_IXUSR) {
462                         return '-rwxr-xr-x';
463                 } else {
464                         return '-rw-r--r--';
465                 };
466         } else {
467                 return '----------';
468         }
471 # convert file mode in octal to file type string
472 sub file_type {
473         my $mode = shift;
475         if ($mode !~ m/^[0-7]+$/) {
476                 return $mode;
477         } else {
478                 $mode = oct $mode;
479         }
481         if (S_ISDIR($mode & S_IFMT)) {
482                 return "directory";
483         } elsif (S_ISLNK($mode)) {
484                 return "symlink";
485         } elsif (S_ISREG($mode)) {
486                 return "file";
487         } else {
488                 return "unknown";
489         }
492 ## ----------------------------------------------------------------------
493 ## functions returning short HTML fragments, or transforming HTML fragments
494 ## which don't beling to other sections
496 # format line of commit message or tag comment
497 sub format_log_line_html {
498         my $line = shift;
500         $line = esc_html($line);
501         $line =~ s/ /&nbsp;/g;
502         if ($line =~ m/([0-9a-fA-F]{40})/) {
503                 my $hash_text = $1;
504                 if (git_get_type($hash_text) eq "commit") {
505                         my $link =
506                                 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
507                                         -class => "text"}, $hash_text);
508                         $line =~ s/$hash_text/$link/;
509                 }
510         }
511         return $line;
514 # format marker of refs pointing to given object
515 sub format_ref_marker {
516         my ($refs, $id) = @_;
517         my $markers = '';
519         if (defined $refs->{$id}) {
520                 foreach my $ref (@{$refs->{$id}}) {
521                         my ($type, $name) = qw();
522                         # e.g. tags/v2.6.11 or heads/next
523                         if ($ref =~ m!^(.*?)s?/(.*)$!) {
524                                 $type = $1;
525                                 $name = $2;
526                         } else {
527                                 $type = "ref";
528                                 $name = $ref;
529                         }
531                         $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
532                 }
533         }
535         if ($markers) {
536                 return ' <span class="refs">'. $markers . '</span>';
537         } else {
538                 return "";
539         }
542 # format, perhaps shortened and with markers, title line
543 sub format_subject_html {
544         my ($long, $short, $href, $extra) = @_;
545         $extra = '' unless defined($extra);
547         if (length($short) < length($long)) {
548                 return $cgi->a({-href => $href, -class => "list subject",
549                                 -title => $long},
550                        esc_html($short) . $extra);
551         } else {
552                 return $cgi->a({-href => $href, -class => "list subject"},
553                        esc_html($long)  . $extra);
554         }
557 sub format_diff_line {
558         my $line = shift;
559         my $char = substr($line, 0, 1);
560         my $diff_class = "";
562         chomp $line;
564         if ($char eq '+') {
565                 $diff_class = " add";
566         } elsif ($char eq "-") {
567                 $diff_class = " rem";
568         } elsif ($char eq "@") {
569                 $diff_class = " chunk_header";
570         } elsif ($char eq "\\") {
571                 $diff_class = " incomplete";
572         }
573         $line = untabify($line);
574         return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
577 ## ----------------------------------------------------------------------
578 ## git utility subroutines, invoking git commands
580 # returns path to the core git executable and the --git-dir parameter as list
581 sub git_cmd {
582         return $GIT, '--git-dir='.$git_dir;
585 # returns path to the core git executable and the --git-dir parameter as string
586 sub git_cmd_str {
587         return join(' ', git_cmd());
590 # get HEAD ref of given project as hash
591 sub git_get_head_hash {
592         my $project = shift;
593         my $o_git_dir = $git_dir;
594         my $retval = undef;
595         $git_dir = "$projectroot/$project";
596         if (open my $fd, "-|", git_cmd(), "rev-parse", "--verify", "HEAD") {
597                 my $head = <$fd>;
598                 close $fd;
599                 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
600                         $retval = $1;
601                 }
602         }
603         if (defined $o_git_dir) {
604                 $git_dir = $o_git_dir;
605         }
606         return $retval;
609 # get type of given object
610 sub git_get_type {
611         my $hash = shift;
613         open my $fd, "-|", git_cmd(), "cat-file", '-t', $hash or return;
614         my $type = <$fd>;
615         close $fd or return;
616         chomp $type;
617         return $type;
620 sub git_get_project_config {
621         my ($key, $type) = @_;
623         return unless ($key);
624         $key =~ s/^gitweb\.//;
625         return if ($key =~ m/\W/);
627         my @x = (git_cmd(), 'repo-config');
628         if (defined $type) { push @x, $type; }
629         push @x, "--get";
630         push @x, "gitweb.$key";
631         my $val = qx(@x);
632         chomp $val;
633         return ($val);
636 # get hash of given path at given ref
637 sub git_get_hash_by_path {
638         my $base = shift;
639         my $path = shift || return undef;
641         my $tree = $base;
643         open my $fd, "-|", git_cmd(), "ls-tree", $base, "--", $path
644                 or die_error(undef, "Open git-ls-tree failed");
645         my $line = <$fd>;
646         close $fd or return undef;
648         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
649         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
650         return $3;
653 ## ......................................................................
654 ## git utility functions, directly accessing git repository
656 # assumes that PATH is not symref
657 sub git_get_hash_by_ref {
658         my $path = shift;
660         open my $fd, "$projectroot/$path" or return undef;
661         my $head = <$fd>;
662         close $fd;
663         chomp $head;
664         if ($head =~ m/^[0-9a-fA-F]{40}$/) {
665                 return $head;
666         }
669 sub git_get_project_description {
670         my $path = shift;
672         open my $fd, "$projectroot/$path/description" or return undef;
673         my $descr = <$fd>;
674         close $fd;
675         chomp $descr;
676         return $descr;
679 sub git_get_project_url_list {
680         my $path = shift;
682         open my $fd, "$projectroot/$path/cloneurl" or return undef;
683         my @git_project_url_list = map { chomp; $_ } <$fd>;
684         close $fd;
686         return wantarray ? @git_project_url_list : \@git_project_url_list;
689 sub git_get_projects_list {
690         my @list;
692         if (-d $projects_list) {
693                 # search in directory
694                 my $dir = $projects_list;
695                 opendir my ($dh), $dir or return undef;
696                 while (my $dir = readdir($dh)) {
697                         if (-e "$projectroot/$dir/HEAD") {
698                                 my $pr = {
699                                         path => $dir,
700                                 };
701                                 push @list, $pr
702                         }
703                 }
704                 closedir($dh);
705         } elsif (-f $projects_list) {
706                 # read from file(url-encoded):
707                 # 'git%2Fgit.git Linus+Torvalds'
708                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
709                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
710                 open my ($fd), $projects_list or return undef;
711                 while (my $line = <$fd>) {
712                         chomp $line;
713                         my ($path, $owner) = split ' ', $line;
714                         $path = unescape($path);
715                         $owner = unescape($owner);
716                         if (!defined $path) {
717                                 next;
718                         }
719                         if (-e "$projectroot/$path/HEAD") {
720                                 my $pr = {
721                                         path => $path,
722                                         owner => decode("utf8", $owner, Encode::FB_DEFAULT),
723                                 };
724                                 push @list, $pr
725                         }
726                 }
727                 close $fd;
728         }
729         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
730         return @list;
733 sub git_get_project_owner {
734         my $project = shift;
735         my $owner;
737         return undef unless $project;
739         # read from file (url-encoded):
740         # 'git%2Fgit.git Linus+Torvalds'
741         # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
742         # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
743         if (-f $projects_list) {
744                 open (my $fd , $projects_list);
745                 while (my $line = <$fd>) {
746                         chomp $line;
747                         my ($pr, $ow) = split ' ', $line;
748                         $pr = unescape($pr);
749                         $ow = unescape($ow);
750                         if ($pr eq $project) {
751                                 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
752                                 last;
753                         }
754                 }
755                 close $fd;
756         }
757         if (!defined $owner) {
758                 $owner = get_file_owner("$projectroot/$project");
759         }
761         return $owner;
764 sub git_get_references {
765         my $type = shift || "";
766         my %refs;
767         my $fd;
768         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
769         # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
770         if (-f "$projectroot/$project/info/refs") {
771                 open $fd, "$projectroot/$project/info/refs"
772                         or return;
773         } else {
774                 open $fd, "-|", git_cmd(), "ls-remote", "."
775                         or return;
776         }
778         while (my $line = <$fd>) {
779                 chomp $line;
780                 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
781                         if (defined $refs{$1}) {
782                                 push @{$refs{$1}}, $2;
783                         } else {
784                                 $refs{$1} = [ $2 ];
785                         }
786                 }
787         }
788         close $fd or return;
789         return \%refs;
792 sub git_get_rev_name_tags {
793         my $hash = shift || return undef;
795         open my $fd, "-|", git_cmd(), "name-rev", "--tags", $hash
796                 or return;
797         my $name_rev = <$fd>;
798         close $fd;
800         if ($name_rev =~ m|^$hash tags/(.*)$|) {
801                 return $1;
802         } else {
803                 # catches also '$hash undefined' output
804                 return undef;
805         }
808 ## ----------------------------------------------------------------------
809 ## parse to hash functions
811 sub parse_date {
812         my $epoch = shift;
813         my $tz = shift || "-0000";
815         my %date;
816         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
817         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
818         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
819         $date{'hour'} = $hour;
820         $date{'minute'} = $min;
821         $date{'mday'} = $mday;
822         $date{'day'} = $days[$wday];
823         $date{'month'} = $months[$mon];
824         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
825                            $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
826         $date{'mday-time'} = sprintf "%d %s %02d:%02d",
827                              $mday, $months[$mon], $hour ,$min;
829         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
830         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
831         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
832         $date{'hour_local'} = $hour;
833         $date{'minute_local'} = $min;
834         $date{'tz_local'} = $tz;
835         return %date;
838 sub parse_tag {
839         my $tag_id = shift;
840         my %tag;
841         my @comment;
843         open my $fd, "-|", git_cmd(), "cat-file", "tag", $tag_id or return;
844         $tag{'id'} = $tag_id;
845         while (my $line = <$fd>) {
846                 chomp $line;
847                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
848                         $tag{'object'} = $1;
849                 } elsif ($line =~ m/^type (.+)$/) {
850                         $tag{'type'} = $1;
851                 } elsif ($line =~ m/^tag (.+)$/) {
852                         $tag{'name'} = $1;
853                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
854                         $tag{'author'} = $1;
855                         $tag{'epoch'} = $2;
856                         $tag{'tz'} = $3;
857                 } elsif ($line =~ m/--BEGIN/) {
858                         push @comment, $line;
859                         last;
860                 } elsif ($line eq "") {
861                         last;
862                 }
863         }
864         push @comment, <$fd>;
865         $tag{'comment'} = \@comment;
866         close $fd or return;
867         if (!defined $tag{'name'}) {
868                 return
869         };
870         return %tag
873 sub parse_commit {
874         my $commit_id = shift;
875         my $commit_text = shift;
877         my @commit_lines;
878         my %co;
880         if (defined $commit_text) {
881                 @commit_lines = @$commit_text;
882         } else {
883                 $/ = "\0";
884                 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", "--max-count=1", $commit_id
885                         or return;
886                 @commit_lines = split '\n', <$fd>;
887                 close $fd or return;
888                 $/ = "\n";
889                 pop @commit_lines;
890         }
891         my $header = shift @commit_lines;
892         if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
893                 return;
894         }
895         ($co{'id'}, my @parents) = split ' ', $header;
896         $co{'parents'} = \@parents;
897         $co{'parent'} = $parents[0];
898         while (my $line = shift @commit_lines) {
899                 last if $line eq "\n";
900                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
901                         $co{'tree'} = $1;
902                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
903                         $co{'author'} = $1;
904                         $co{'author_epoch'} = $2;
905                         $co{'author_tz'} = $3;
906                         if ($co{'author'} =~ m/^([^<]+) </) {
907                                 $co{'author_name'} = $1;
908                         } else {
909                                 $co{'author_name'} = $co{'author'};
910                         }
911                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
912                         $co{'committer'} = $1;
913                         $co{'committer_epoch'} = $2;
914                         $co{'committer_tz'} = $3;
915                         $co{'committer_name'} = $co{'committer'};
916                         $co{'committer_name'} =~ s/ <.*//;
917                 }
918         }
919         if (!defined $co{'tree'}) {
920                 return;
921         };
923         foreach my $title (@commit_lines) {
924                 $title =~ s/^    //;
925                 if ($title ne "") {
926                         $co{'title'} = chop_str($title, 80, 5);
927                         # remove leading stuff of merges to make the interesting part visible
928                         if (length($title) > 50) {
929                                 $title =~ s/^Automatic //;
930                                 $title =~ s/^merge (of|with) /Merge ... /i;
931                                 if (length($title) > 50) {
932                                         $title =~ s/(http|rsync):\/\///;
933                                 }
934                                 if (length($title) > 50) {
935                                         $title =~ s/(master|www|rsync)\.//;
936                                 }
937                                 if (length($title) > 50) {
938                                         $title =~ s/kernel.org:?//;
939                                 }
940                                 if (length($title) > 50) {
941                                         $title =~ s/\/pub\/scm//;
942                                 }
943                         }
944                         $co{'title_short'} = chop_str($title, 50, 5);
945                         last;
946                 }
947         }
948         # remove added spaces
949         foreach my $line (@commit_lines) {
950                 $line =~ s/^    //;
951         }
952         $co{'comment'} = \@commit_lines;
954         my $age = time - $co{'committer_epoch'};
955         $co{'age'} = $age;
956         $co{'age_string'} = age_string($age);
957         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
958         if ($age > 60*60*24*7*2) {
959                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
960                 $co{'age_string_age'} = $co{'age_string'};
961         } else {
962                 $co{'age_string_date'} = $co{'age_string'};
963                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
964         }
965         return %co;
968 # parse ref from ref_file, given by ref_id, with given type
969 sub parse_ref {
970         my $ref_file = shift;
971         my $ref_id = shift;
972         my $type = shift || git_get_type($ref_id);
973         my %ref_item;
975         $ref_item{'type'} = $type;
976         $ref_item{'id'} = $ref_id;
977         $ref_item{'epoch'} = 0;
978         $ref_item{'age'} = "unknown";
979         if ($type eq "tag") {
980                 my %tag = parse_tag($ref_id);
981                 $ref_item{'comment'} = $tag{'comment'};
982                 if ($tag{'type'} eq "commit") {
983                         my %co = parse_commit($tag{'object'});
984                         $ref_item{'epoch'} = $co{'committer_epoch'};
985                         $ref_item{'age'} = $co{'age_string'};
986                 } elsif (defined($tag{'epoch'})) {
987                         my $age = time - $tag{'epoch'};
988                         $ref_item{'epoch'} = $tag{'epoch'};
989                         $ref_item{'age'} = age_string($age);
990                 }
991                 $ref_item{'reftype'} = $tag{'type'};
992                 $ref_item{'name'} = $tag{'name'};
993                 $ref_item{'refid'} = $tag{'object'};
994         } elsif ($type eq "commit"){
995                 my %co = parse_commit($ref_id);
996                 $ref_item{'reftype'} = "commit";
997                 $ref_item{'name'} = $ref_file;
998                 $ref_item{'title'} = $co{'title'};
999                 $ref_item{'refid'} = $ref_id;
1000                 $ref_item{'epoch'} = $co{'committer_epoch'};
1001                 $ref_item{'age'} = $co{'age_string'};
1002         } else {
1003                 $ref_item{'reftype'} = $type;
1004                 $ref_item{'name'} = $ref_file;
1005                 $ref_item{'refid'} = $ref_id;
1006         }
1008         return %ref_item;
1011 # parse line of git-diff-tree "raw" output
1012 sub parse_difftree_raw_line {
1013         my $line = shift;
1014         my %res;
1016         # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
1017         # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
1018         if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1019                 $res{'from_mode'} = $1;
1020                 $res{'to_mode'} = $2;
1021                 $res{'from_id'} = $3;
1022                 $res{'to_id'} = $4;
1023                 $res{'status'} = $5;
1024                 $res{'similarity'} = $6;
1025                 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1026                         ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1027                 } else {
1028                         $res{'file'} = unquote($7);
1029                 }
1030         }
1031         # 'c512b523472485aef4fff9e57b229d9d243c967f'
1032         elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1033                 $res{'commit'} = $1;
1034         }
1036         return wantarray ? %res : \%res;
1039 # parse line of git-ls-tree output
1040 sub parse_ls_tree_line ($;%) {
1041         my $line = shift;
1042         my %opts = @_;
1043         my %res;
1045         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
1046         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1048         $res{'mode'} = $1;
1049         $res{'type'} = $2;
1050         $res{'hash'} = $3;
1051         if ($opts{'-z'}) {
1052                 $res{'name'} = $4;
1053         } else {
1054                 $res{'name'} = unquote($4);
1055         }
1057         return wantarray ? %res : \%res;
1060 ## ......................................................................
1061 ## parse to array of hashes functions
1063 sub git_get_refs_list {
1064         my $ref_dir = shift;
1065         my @reflist;
1067         my @refs;
1068         my $pfxlen = length("$projectroot/$project/$ref_dir");
1069         File::Find::find(sub {
1070                 return if (/^\./);
1071                 if (-f $_) {
1072                         push @refs, substr($File::Find::name, $pfxlen + 1);
1073                 }
1074         }, "$projectroot/$project/$ref_dir");
1076         foreach my $ref_file (@refs) {
1077                 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
1078                 my $type = git_get_type($ref_id) || next;
1079                 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1081                 push @reflist, \%ref_item;
1082         }
1083         # sort refs by age
1084         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1085         return \@reflist;
1088 ## ----------------------------------------------------------------------
1089 ## filesystem-related functions
1091 sub get_file_owner {
1092         my $path = shift;
1094         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1095         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1096         if (!defined $gcos) {
1097                 return undef;
1098         }
1099         my $owner = $gcos;
1100         $owner =~ s/[,;].*$//;
1101         return decode("utf8", $owner, Encode::FB_DEFAULT);
1104 ## ......................................................................
1105 ## mimetype related functions
1107 sub mimetype_guess_file {
1108         my $filename = shift;
1109         my $mimemap = shift;
1110         -r $mimemap or return undef;
1112         my %mimemap;
1113         open(MIME, $mimemap) or return undef;
1114         while (<MIME>) {
1115                 next if m/^#/; # skip comments
1116                 my ($mime, $exts) = split(/\t+/);
1117                 if (defined $exts) {
1118                         my @exts = split(/\s+/, $exts);
1119                         foreach my $ext (@exts) {
1120                                 $mimemap{$ext} = $mime;
1121                         }
1122                 }
1123         }
1124         close(MIME);
1126         $filename =~ /\.(.*?)$/;
1127         return $mimemap{$1};
1130 sub mimetype_guess {
1131         my $filename = shift;
1132         my $mime;
1133         $filename =~ /\./ or return undef;
1135         if ($mimetypes_file) {
1136                 my $file = $mimetypes_file;
1137                 if ($file !~ m!^/!) { # if it is relative path
1138                         # it is relative to project
1139                         $file = "$projectroot/$project/$file";
1140                 }
1141                 $mime = mimetype_guess_file($filename, $file);
1142         }
1143         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1144         return $mime;
1147 sub blob_mimetype {
1148         my $fd = shift;
1149         my $filename = shift;
1151         if ($filename) {
1152                 my $mime = mimetype_guess($filename);
1153                 $mime and return $mime;
1154         }
1156         # just in case
1157         return $default_blob_plain_mimetype unless $fd;
1159         if (-T $fd) {
1160                 return 'text/plain' .
1161                        ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1162         } elsif (! $filename) {
1163                 return 'application/octet-stream';
1164         } elsif ($filename =~ m/\.png$/i) {
1165                 return 'image/png';
1166         } elsif ($filename =~ m/\.gif$/i) {
1167                 return 'image/gif';
1168         } elsif ($filename =~ m/\.jpe?g$/i) {
1169                 return 'image/jpeg';
1170         } else {
1171                 return 'application/octet-stream';
1172         }
1175 ## ======================================================================
1176 ## functions printing HTML: header, footer, error page
1178 sub git_header_html {
1179         my $status = shift || "200 OK";
1180         my $expires = shift;
1182         my $title = "$site_name git";
1183         if (defined $project) {
1184                 $title .= " - $project";
1185                 if (defined $action) {
1186                         $title .= "/$action";
1187                         if (defined $file_name) {
1188                                 $title .= " - $file_name";
1189                                 if ($action eq "tree" && $file_name !~ m|/$|) {
1190                                         $title .= "/";
1191                                 }
1192                         }
1193                 }
1194         }
1195         my $content_type;
1196         # require explicit support from the UA if we are to send the page as
1197         # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1198         # we have to do this because MSIE sometimes globs '*/*', pretending to
1199         # support xhtml+xml but choking when it gets what it asked for.
1200         if (defined $cgi->http('HTTP_ACCEPT') &&
1201             $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1202             $cgi->Accept('application/xhtml+xml') != 0) {
1203                 $content_type = 'application/xhtml+xml';
1204         } else {
1205                 $content_type = 'text/html';
1206         }
1207         print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1208                            -status=> $status, -expires => $expires);
1209         print <<EOF;
1210 <?xml version="1.0" encoding="utf-8"?>
1211 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1212 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1213 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1214 <!-- git core binaries version $git_version -->
1215 <head>
1216 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1217 <meta name="generator" content="gitweb/$version git/$git_version"/>
1218 <meta name="robots" content="index, nofollow"/>
1219 <title>$title</title>
1220 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1221 EOF
1222         if (defined $project) {
1223                 printf('<link rel="alternate" title="%s log" '.
1224                        'href="%s" type="application/rss+xml"/>'."\n",
1225                        esc_param($project), href(action=>"rss"));
1226         }
1227         if (defined $favicon) {
1228                 print qq(<link rel="shortcut icon" href="$favicon" type="image/png"/>\n);
1229         }
1231         print "</head>\n" .
1232               "<body>\n" .
1233               "<div class=\"page_header\">\n" .
1234               "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1235               "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1236               "</a>\n";
1237         print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1238         if (defined $project) {
1239                 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1240                 if (defined $action) {
1241                         print " / $action";
1242                 }
1243                 print "\n";
1244                 if (!defined $searchtext) {
1245                         $searchtext = "";
1246                 }
1247                 my $search_hash;
1248                 if (defined $hash_base) {
1249                         $search_hash = $hash_base;
1250                 } elsif (defined $hash) {
1251                         $search_hash = $hash;
1252                 } else {
1253                         $search_hash = "HEAD";
1254                 }
1255                 $cgi->param("a", "search");
1256                 $cgi->param("h", $search_hash);
1257                 print $cgi->startform(-method => "get", -action => $my_uri) .
1258                       "<div class=\"search\">\n" .
1259                       $cgi->hidden(-name => "p") . "\n" .
1260                       $cgi->hidden(-name => "a") . "\n" .
1261                       $cgi->hidden(-name => "h") . "\n" .
1262                       $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1263                       "</div>" .
1264                       $cgi->end_form() . "\n";
1265         }
1266         print "</div>\n";
1269 sub git_footer_html {
1270         print "<div class=\"page_footer\">\n";
1271         if (defined $project) {
1272                 my $descr = git_get_project_description($project);
1273                 if (defined $descr) {
1274                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1275                 }
1276                 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1277         } else {
1278                 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1279         }
1280         print "</div>\n" .
1281               "</body>\n" .
1282               "</html>";
1285 sub die_error {
1286         my $status = shift || "403 Forbidden";
1287         my $error = shift || "Malformed query, file missing or permission denied";
1289         git_header_html($status);
1290         print <<EOF;
1291 <div class="page_body">
1292 <br /><br />
1293 $status - $error
1294 <br />
1295 </div>
1296 EOF
1297         git_footer_html();
1298         exit;
1301 ## ----------------------------------------------------------------------
1302 ## functions printing or outputting HTML: navigation
1304 sub git_print_page_nav {
1305         my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1306         $extra = '' if !defined $extra; # pager or formats
1308         my @navs = qw(summary shortlog log commit commitdiff tree);
1309         if ($suppress) {
1310                 @navs = grep { $_ ne $suppress } @navs;
1311         }
1313         my %arg = map { $_ => {action=>$_} } @navs;
1314         if (defined $head) {
1315                 for (qw(commit commitdiff)) {
1316                         $arg{$_}{hash} = $head;
1317                 }
1318                 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1319                         for (qw(shortlog log)) {
1320                                 $arg{$_}{hash} = $head;
1321                         }
1322                 }
1323         }
1324         $arg{tree}{hash} = $treehead if defined $treehead;
1325         $arg{tree}{hash_base} = $treebase if defined $treebase;
1327         print "<div class=\"page_nav\">\n" .
1328                 (join " | ",
1329                  map { $_ eq $current ?
1330                        $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1331                  } @navs);
1332         print "<br/>\n$extra<br/>\n" .
1333               "</div>\n";
1336 sub format_paging_nav {
1337         my ($action, $hash, $head, $page, $nrevs) = @_;
1338         my $paging_nav;
1341         if ($hash ne $head || $page) {
1342                 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1343         } else {
1344                 $paging_nav .= "HEAD";
1345         }
1347         if ($page > 0) {
1348                 $paging_nav .= " &sdot; " .
1349                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1350                                  -accesskey => "p", -title => "Alt-p"}, "prev");
1351         } else {
1352                 $paging_nav .= " &sdot; prev";
1353         }
1355         if ($nrevs >= (100 * ($page+1)-1)) {
1356                 $paging_nav .= " &sdot; " .
1357                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1358                                  -accesskey => "n", -title => "Alt-n"}, "next");
1359         } else {
1360                 $paging_nav .= " &sdot; next";
1361         }
1363         return $paging_nav;
1366 ## ......................................................................
1367 ## functions printing or outputting HTML: div
1369 sub git_print_header_div {
1370         my ($action, $title, $hash, $hash_base) = @_;
1371         my %args = ();
1373         $args{action} = $action;
1374         $args{hash} = $hash if $hash;
1375         $args{hash_base} = $hash_base if $hash_base;
1377         print "<div class=\"header\">\n" .
1378               $cgi->a({-href => href(%args), -class => "title"},
1379               $title ? $title : $action) .
1380               "\n</div>\n";
1383 #sub git_print_authorship (\%) {
1384 sub git_print_authorship {
1385         my $co = shift;
1387         my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
1388         print "<div class=\"author_date\">" .
1389               esc_html($co->{'author_name'}) .
1390               " [$ad{'rfc2822'}";
1391         if ($ad{'hour_local'} < 6) {
1392                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
1393                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1394         } else {
1395                 printf(" (%02d:%02d %s)",
1396                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
1397         }
1398         print "]</div>\n";
1401 sub git_print_page_path {
1402         my $name = shift;
1403         my $type = shift;
1404         my $hb = shift;
1406         if (!defined $name) {
1407                 print "<div class=\"page_path\">/</div>\n";
1408         } elsif (defined $type && $type eq 'blob') {
1409                 print "<div class=\"page_path\">";
1410                 if (defined $hb) {
1411                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1412                                                      hash_base=>$hb)},
1413                                       esc_html($name));
1414                 } else {
1415                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1416                                       esc_html($name));
1417                 }
1418                 print "<br/></div>\n";
1419         } else {
1420                 print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1421         }
1424 # sub git_print_log (\@;%) {
1425 sub git_print_log ($;%) {
1426         my $log = shift;
1427         my %opts = @_;
1429         if ($opts{'-remove_title'}) {
1430                 # remove title, i.e. first line of log
1431                 shift @$log;
1432         }
1433         # remove leading empty lines
1434         while (defined $log->[0] && $log->[0] eq "") {
1435                 shift @$log;
1436         }
1438         # print log
1439         my $signoff = 0;
1440         my $empty = 0;
1441         foreach my $line (@$log) {
1442                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1443                         $signoff = 1;
1444                         $empty = 0;
1445                         if (! $opts{'-remove_signoff'}) {
1446                                 print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1447                                 next;
1448                         } else {
1449                                 # remove signoff lines
1450                                 next;
1451                         }
1452                 } else {
1453                         $signoff = 0;
1454                 }
1456                 # print only one empty line
1457                 # do not print empty line after signoff
1458                 if ($line eq "") {
1459                         next if ($empty || $signoff);
1460                         $empty = 1;
1461                 } else {
1462                         $empty = 0;
1463                 }
1465                 print format_log_line_html($line) . "<br/>\n";
1466         }
1468         if ($opts{'-final_empty_line'}) {
1469                 # end with single empty line
1470                 print "<br/>\n" unless $empty;
1471         }
1474 sub git_print_simplified_log {
1475         my $log = shift;
1476         my $remove_title = shift;
1478         git_print_log($log,
1479                 -final_empty_line=> 1,
1480                 -remove_title => $remove_title);
1483 # print tree entry (row of git_tree), but without encompassing <tr> element
1484 sub git_print_tree_entry {
1485         my ($t, $basedir, $hash_base, $have_blame) = @_;
1487         my %base_key = ();
1488         $base_key{hash_base} = $hash_base if defined $hash_base;
1490         print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
1491         if ($t->{'type'} eq "blob") {
1492                 print "<td class=\"list\">" .
1493                       $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1494                                              file_name=>"$basedir$t->{'name'}", %base_key),
1495                               -class => "list"}, esc_html($t->{'name'})) .
1496                       "</td>\n" .
1497                       "<td class=\"link\">" .
1498                       $cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
1499                                              file_name=>"$basedir$t->{'name'}", %base_key)},
1500                               "blob");
1501                 if ($have_blame) {
1502                         print " | " .
1503                                 $cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},
1504                                                        file_name=>"$basedir$t->{'name'}", %base_key)},
1505                                         "blame");
1506                 }
1507                 if (defined $hash_base) {
1508                         print " | " .
1509                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1510                                                      hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1511                                       "history");
1512                 }
1513                 print " | " .
1514                       $cgi->a({-href => href(action=>"blob_plain",
1515                                              hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},
1516                               "raw") .
1517                       "</td>\n";
1519         } elsif ($t->{'type'} eq "tree") {
1520                 print "<td class=\"list\">" .
1521                       $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1522                                              file_name=>"$basedir$t->{'name'}", %base_key)},
1523                               esc_html($t->{'name'})) .
1524                       "</td>\n" .
1525                       "<td class=\"link\">" .
1526                       $cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},
1527                                              file_name=>"$basedir$t->{'name'}", %base_key)},
1528                               "tree");
1529                 if (defined $hash_base) {
1530                         print " | " .
1531                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
1532                                                      file_name=>"$basedir$t->{'name'}")},
1533                                       "history");
1534                 }
1535                 print "</td>\n";
1536         }
1539 ## ......................................................................
1540 ## functions printing large fragments of HTML
1542 sub git_difftree_body {
1543         my ($difftree, $hash, $parent) = @_;
1545         print "<div class=\"list_head\">\n";
1546         if ($#{$difftree} > 10) {
1547                 print(($#{$difftree} + 1) . " files changed:\n");
1548         }
1549         print "</div>\n";
1551         print "<table class=\"diff_tree\">\n";
1552         my $alternate = 0;
1553         my $patchno = 0;
1554         foreach my $line (@{$difftree}) {
1555                 my %diff = parse_difftree_raw_line($line);
1557                 if ($alternate) {
1558                         print "<tr class=\"dark\">\n";
1559                 } else {
1560                         print "<tr class=\"light\">\n";
1561                 }
1562                 $alternate ^= 1;
1564                 my ($to_mode_oct, $to_mode_str, $to_file_type);
1565                 my ($from_mode_oct, $from_mode_str, $from_file_type);
1566                 if ($diff{'to_mode'} ne ('0' x 6)) {
1567                         $to_mode_oct = oct $diff{'to_mode'};
1568                         if (S_ISREG($to_mode_oct)) { # only for regular file
1569                                 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1570                         }
1571                         $to_file_type = file_type($diff{'to_mode'});
1572                 }
1573                 if ($diff{'from_mode'} ne ('0' x 6)) {
1574                         $from_mode_oct = oct $diff{'from_mode'};
1575                         if (S_ISREG($to_mode_oct)) { # only for regular file
1576                                 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1577                         }
1578                         $from_file_type = file_type($diff{'from_mode'});
1579                 }
1581                 if ($diff{'status'} eq "A") { # created
1582                         my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1583                         $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
1584                         $mode_chng   .= "]</span>";
1585                         print "<td>" .
1586                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1587                                                      hash_base=>$hash, file_name=>$diff{'file'}),
1588                                       -class => "list"}, esc_html($diff{'file'})) .
1589                               "</td>\n" .
1590                               "<td>$mode_chng</td>\n" .
1591                               "<td class=\"link\">" .
1592                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1593                                                      hash_base=>$hash, file_name=>$diff{'file'})},
1594                                       "blob");
1595                         if ($action == "commitdiff") {
1596                                 # link to patch
1597                                 $patchno++;
1598                                 print " | " .
1599                                       $cgi->a({-href => "#patch$patchno"}, "patch");
1600                         }
1601                         print "</td>\n";
1603                 } elsif ($diff{'status'} eq "D") { # deleted
1604                         my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1605                         print "<td>" .
1606                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1607                                                      hash_base=>$parent, file_name=>$diff{'file'}),
1608                                        -class => "list"}, esc_html($diff{'file'})) .
1609                               "</td>\n" .
1610                               "<td>$mode_chng</td>\n" .
1611                               "<td class=\"link\">" .
1612                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1613                                                      hash_base=>$parent, file_name=>$diff{'file'})},
1614                                       "blob") .
1615                               " | ";
1616                         if ($action == "commitdiff") {
1617                                 # link to patch
1618                                 $patchno++;
1619                                 print " | " .
1620                                       $cgi->a({-href => "#patch$patchno"}, "patch");
1621                         }
1622                         print $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1623                                                      file_name=>$diff{'file'})},
1624                                       "history") .
1625                               "</td>\n";
1627                 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1628                         my $mode_chnge = "";
1629                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1630                                 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1631                                 if ($from_file_type != $to_file_type) {
1632                                         $mode_chnge .= " from $from_file_type to $to_file_type";
1633                                 }
1634                                 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1635                                         if ($from_mode_str && $to_mode_str) {
1636                                                 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1637                                         } elsif ($to_mode_str) {
1638                                                 $mode_chnge .= " mode: $to_mode_str";
1639                                         }
1640                                 }
1641                                 $mode_chnge .= "]</span>\n";
1642                         }
1643                         print "<td>";
1644                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1645                                 print $cgi->a({-href => href(action=>"blobdiff",
1646                                                              hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1647                                                              hash_base=>$hash, hash_parent_base=>$parent,
1648                                                              file_name=>$diff{'file'}),
1649                                               -class => "list"}, esc_html($diff{'file'}));
1650                         } else { # only mode changed
1651                                 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1652                                                              hash_base=>$hash, file_name=>$diff{'file'}),
1653                                               -class => "list"}, esc_html($diff{'file'}));
1654                         }
1655                         print "</td>\n" .
1656                               "<td>$mode_chnge</td>\n" .
1657                               "<td class=\"link\">" .
1658                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1659                                                      hash_base=>$hash, file_name=>$diff{'file'})},
1660                                       "blob");
1661                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1662                                 if ($action == "commitdiff") {
1663                                         # link to patch
1664                                         $patchno++;
1665                                         print " | " .
1666                                                 $cgi->a({-href => "#patch$patchno"}, "patch");
1667                                 } else {
1668                                         print " | " .
1669                                                 $cgi->a({-href => href(action=>"blobdiff",
1670                                                                        hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1671                                                                        hash_base=>$hash, hash_parent_base=>$parent,
1672                                                                        file_name=>$diff{'file'})},
1673                                                         "diff");
1674                                 }
1675                         }
1676                         print " | " .
1677                                 $cgi->a({-href => href(action=>"history",
1678                                                        hash_base=>$hash, file_name=>$diff{'file'})},
1679                                         "history");
1680                         print "</td>\n";
1682                 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1683                         my %status_name = ('R' => 'moved', 'C' => 'copied');
1684                         my $nstatus = $status_name{$diff{'status'}};
1685                         my $mode_chng = "";
1686                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1687                                 # mode also for directories, so we cannot use $to_mode_str
1688                                 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1689                         }
1690                         print "<td>" .
1691                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1692                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1693                                       -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1694                               "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1695                               $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1696                                                      hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1697                                       -class => "list"}, esc_html($diff{'from_file'})) .
1698                               " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1699                               "<td class=\"link\">" .
1700                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1701                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1702                                       "blob");
1703                         if ($diff{'to_id'} ne $diff{'from_id'}) {
1704                                 if ($action == "commitdiff") {
1705                                         # link to patch
1706                                         $patchno++;
1707                                         print " | " .
1708                                                 $cgi->a({-href => "#patch$patchno"}, "patch");
1709                                 } else {
1710                                         print " | " .
1711                                                 $cgi->a({-href => href(action=>"blobdiff",
1712                                                                        hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1713                                                                        hash_base=>$hash, hash_parent_base=>$parent,
1714                                                                        file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1715                                                         "diff");
1716                                 }
1717                         }
1718                         print "</td>\n";
1720                 } # we should not encounter Unmerged (U) or Unknown (X) status
1721                 print "</tr>\n";
1722         }
1723         print "</table>\n";
1726 sub git_patchset_body {
1727         my ($fd, $difftree, $hash, $hash_parent) = @_;
1729         my $patch_idx = 0;
1730         my $in_header = 0;
1731         my $patch_found = 0;
1732         my $diffinfo;
1734         print "<div class=\"patchset\">\n";
1736         LINE:
1737         while (my $patch_line = <$fd>) {
1738                 chomp $patch_line;
1740                 if ($patch_line =~ m/^diff /) { # "git diff" header
1741                         # beginning of patch (in patchset)
1742                         if ($patch_found) {
1743                                 # close previous patch
1744                                 print "</div>\n"; # class="patch"
1745                         } else {
1746                                 # first patch in patchset
1747                                 $patch_found = 1;
1748                         }
1749                         print "<div class=\"patch\" id=\"patch". ($patch_idx+1) ."\">\n";
1751                         if (ref($difftree->[$patch_idx]) eq "HASH") {
1752                                 $diffinfo = $difftree->[$patch_idx];
1753                         } else {
1754                                 $diffinfo = parse_difftree_raw_line($difftree->[$patch_idx]);
1755                         }
1756                         $patch_idx++;
1758                         # for now, no extended header, hence we skip empty patches
1759                         # companion to  next LINE if $in_header;
1760                         if ($diffinfo->{'from_id'} eq $diffinfo->{'to_id'}) { # no change
1761                                 $in_header = 1;
1762                                 next LINE;
1763                         }
1765                         if ($diffinfo->{'status'} eq "A") { # added
1766                                 print "<div class=\"diff_info\">" . file_type($diffinfo->{'to_mode'}) . ":" .
1767                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1768                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1769                                               $diffinfo->{'to_id'}) . "(new)" .
1770                                       "</div>\n"; # class="diff_info"
1772                         } elsif ($diffinfo->{'status'} eq "D") { # deleted
1773                                 print "<div class=\"diff_info\">" . file_type($diffinfo->{'from_mode'}) . ":" .
1774                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1775                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1776                                               $diffinfo->{'from_id'}) . "(deleted)" .
1777                                       "</div>\n"; # class="diff_info"
1779                         } elsif ($diffinfo->{'status'} eq "R" || # renamed
1780                                  $diffinfo->{'status'} eq "C" || # copied
1781                                  $diffinfo->{'status'} eq "2") { # with two filenames (from git_blobdiff)
1782                                 print "<div class=\"diff_info\">" .
1783                                       file_type($diffinfo->{'from_mode'}) . ":" .
1784                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1785                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'from_file'})},
1786                                               $diffinfo->{'from_id'}) .
1787                                       " -> " .
1788                                       file_type($diffinfo->{'to_mode'}) . ":" .
1789                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1790                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'to_file'})},
1791                                               $diffinfo->{'to_id'});
1792                                 print "</div>\n"; # class="diff_info"
1794                         } else { # modified, mode changed, ...
1795                                 print "<div class=\"diff_info\">" .
1796                                       file_type($diffinfo->{'from_mode'}) . ":" .
1797                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1798                                                              hash=>$diffinfo->{'from_id'}, file_name=>$diffinfo->{'file'})},
1799                                               $diffinfo->{'from_id'}) .
1800                                       " -> " .
1801                                       file_type($diffinfo->{'to_mode'}) . ":" .
1802                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1803                                                              hash=>$diffinfo->{'to_id'}, file_name=>$diffinfo->{'file'})},
1804                                               $diffinfo->{'to_id'});
1805                                 print "</div>\n"; # class="diff_info"
1806                         }
1808                         #print "<div class=\"diff extended_header\">\n";
1809                         $in_header = 1;
1810                         next LINE;
1811                 } # start of patch in patchset
1814                 if ($in_header && $patch_line =~ m/^---/) {
1815                         #print "</div>\n"; # class="diff extended_header"
1816                         $in_header = 0;
1818                         my $file = $diffinfo->{'from_file'};
1819                         $file  ||= $diffinfo->{'file'};
1820                         $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1821                                                        hash=>$diffinfo->{'from_id'}, file_name=>$file),
1822                                         -class => "list"}, esc_html($file));
1823                         $patch_line =~ s|a/.*$|a/$file|g;
1824                         print "<div class=\"diff from_file\">$patch_line</div>\n";
1826                         $patch_line = <$fd>;
1827                         chomp $patch_line;
1829                         #$patch_line =~ m/^+++/;
1830                         $file    = $diffinfo->{'to_file'};
1831                         $file  ||= $diffinfo->{'file'};
1832                         $file = $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1833                                                        hash=>$diffinfo->{'to_id'}, file_name=>$file),
1834                                         -class => "list"}, esc_html($file));
1835                         $patch_line =~ s|b/.*|b/$file|g;
1836                         print "<div class=\"diff to_file\">$patch_line</div>\n";
1838                         next LINE;
1839                 }
1840                 next LINE if $in_header;
1842                 print format_diff_line($patch_line);
1843         }
1844         print "</div>\n" if $patch_found; # class="patch"
1846         print "</div>\n"; # class="patchset"
1849 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1851 sub git_shortlog_body {
1852         # uses global variable $project
1853         my ($revlist, $from, $to, $refs, $extra) = @_;
1855         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1856         my $have_snapshot = (defined $ctype && defined $suffix);
1858         $from = 0 unless defined $from;
1859         $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1861         print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1862         my $alternate = 0;
1863         for (my $i = $from; $i <= $to; $i++) {
1864                 my $commit = $revlist->[$i];
1865                 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1866                 my $ref = format_ref_marker($refs, $commit);
1867                 my %co = parse_commit($commit);
1868                 if ($alternate) {
1869                         print "<tr class=\"dark\">\n";
1870                 } else {
1871                         print "<tr class=\"light\">\n";
1872                 }
1873                 $alternate ^= 1;
1874                 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1875                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1876                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1877                       "<td>";
1878                 print format_subject_html($co{'title'}, $co{'title_short'},
1879                                           href(action=>"commit", hash=>$commit), $ref);
1880                 print "</td>\n" .
1881                       "<td class=\"link\">" .
1882                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1883                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1884                 if ($have_snapshot) {
1885                         print " | " .  $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1886                 }
1887                 print "</td>\n" .
1888                       "</tr>\n";
1889         }
1890         if (defined $extra) {
1891                 print "<tr>\n" .
1892                       "<td colspan=\"4\">$extra</td>\n" .
1893                       "</tr>\n";
1894         }
1895         print "</table>\n";
1898 sub git_history_body {
1899         # Warning: assumes constant type (blob or tree) during history
1900         my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1902         print "<table class=\"history\" cellspacing=\"0\">\n";
1903         my $alternate = 0;
1904         while (my $line = <$fd>) {
1905                 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1906                         next;
1907                 }
1909                 my $commit = $1;
1910                 my %co = parse_commit($commit);
1911                 if (!%co) {
1912                         next;
1913                 }
1915                 my $ref = format_ref_marker($refs, $commit);
1917                 if ($alternate) {
1918                         print "<tr class=\"dark\">\n";
1919                 } else {
1920                         print "<tr class=\"light\">\n";
1921                 }
1922                 $alternate ^= 1;
1923                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1924                       # shortlog uses      chop_str($co{'author_name'}, 10)
1925                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1926                       "<td>";
1927                 # originally git_history used chop_str($co{'title'}, 50)
1928                 print format_subject_html($co{'title'}, $co{'title_short'},
1929                                           href(action=>"commit", hash=>$commit), $ref);
1930                 print "</td>\n" .
1931                       "<td class=\"link\">" .
1932                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1933                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1934                       $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1936                 if ($ftype eq 'blob') {
1937                         my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1938                         my $blob_parent  = git_get_hash_by_path($commit, $file_name);
1939                         if (defined $blob_current && defined $blob_parent &&
1940                                         $blob_current ne $blob_parent) {
1941                                 print " | " .
1942                                         $cgi->a({-href => href(action=>"blobdiff",
1943                                                                hash=>$blob_current, hash_parent=>$blob_parent,
1944                                                                hash_base=>$hash_base, hash_parent_base=>$commit,
1945                                                                file_name=>$file_name)},
1946                                                 "diff to current");
1947                         }
1948                 }
1949                 print "</td>\n" .
1950                       "</tr>\n";
1951         }
1952         if (defined $extra) {
1953                 print "<tr>\n" .
1954                       "<td colspan=\"4\">$extra</td>\n" .
1955                       "</tr>\n";
1956         }
1957         print "</table>\n";
1960 sub git_tags_body {
1961         # uses global variable $project
1962         my ($taglist, $from, $to, $extra) = @_;
1963         $from = 0 unless defined $from;
1964         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1966         print "<table class=\"tags\" cellspacing=\"0\">\n";
1967         my $alternate = 0;
1968         for (my $i = $from; $i <= $to; $i++) {
1969                 my $entry = $taglist->[$i];
1970                 my %tag = %$entry;
1971                 my $comment_lines = $tag{'comment'};
1972                 my $comment = shift @$comment_lines;
1973                 my $comment_short;
1974                 if (defined $comment) {
1975                         $comment_short = chop_str($comment, 30, 5);
1976                 }
1977                 if ($alternate) {
1978                         print "<tr class=\"dark\">\n";
1979                 } else {
1980                         print "<tr class=\"light\">\n";
1981                 }
1982                 $alternate ^= 1;
1983                 print "<td><i>$tag{'age'}</i></td>\n" .
1984                       "<td>" .
1985                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1986                                -class => "list name"}, esc_html($tag{'name'})) .
1987                       "</td>\n" .
1988                       "<td>";
1989                 if (defined $comment) {
1990                         print format_subject_html($comment, $comment_short,
1991                                                   href(action=>"tag", hash=>$tag{'id'}));
1992                 }
1993                 print "</td>\n" .
1994                       "<td class=\"selflink\">";
1995                 if ($tag{'type'} eq "tag") {
1996                         print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1997                 } else {
1998                         print "&nbsp;";
1999                 }
2000                 print "</td>\n" .
2001                       "<td class=\"link\">" . " | " .
2002                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
2003                 if ($tag{'reftype'} eq "commit") {
2004                         print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
2005                               " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
2006                 } elsif ($tag{'reftype'} eq "blob") {
2007                         print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
2008                 }
2009                 print "</td>\n" .
2010                       "</tr>";
2011         }
2012         if (defined $extra) {
2013                 print "<tr>\n" .
2014                       "<td colspan=\"5\">$extra</td>\n" .
2015                       "</tr>\n";
2016         }
2017         print "</table>\n";
2020 sub git_heads_body {
2021         # uses global variable $project
2022         my ($taglist, $head, $from, $to, $extra) = @_;
2023         $from = 0 unless defined $from;
2024         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
2026         print "<table class=\"heads\" cellspacing=\"0\">\n";
2027         my $alternate = 0;
2028         for (my $i = $from; $i <= $to; $i++) {
2029                 my $entry = $taglist->[$i];
2030                 my %tag = %$entry;
2031                 my $curr = $tag{'id'} eq $head;
2032                 if ($alternate) {
2033                         print "<tr class=\"dark\">\n";
2034                 } else {
2035                         print "<tr class=\"light\">\n";
2036                 }
2037                 $alternate ^= 1;
2038                 print "<td><i>$tag{'age'}</i></td>\n" .
2039                       ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
2040                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
2041                                -class => "list name"},esc_html($tag{'name'})) .
2042                       "</td>\n" .
2043                       "<td class=\"link\">" .
2044                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
2045                       $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
2046                       "</td>\n" .
2047                       "</tr>";
2048         }
2049         if (defined $extra) {
2050                 print "<tr>\n" .
2051                       "<td colspan=\"3\">$extra</td>\n" .
2052                       "</tr>\n";
2053         }
2054         print "</table>\n";
2057 ## ======================================================================
2058 ## ======================================================================
2059 ## actions
2061 sub git_project_list {
2062         my $order = $cgi->param('o');
2063         if (defined $order && $order !~ m/project|descr|owner|age/) {
2064                 die_error(undef, "Unknown order parameter");
2065         }
2067         my @list = git_get_projects_list();
2068         my @projects;
2069         if (!@list) {
2070                 die_error(undef, "No projects found");
2071         }
2072         foreach my $pr (@list) {
2073                 my $head = git_get_head_hash($pr->{'path'});
2074                 if (!defined $head) {
2075                         next;
2076                 }
2077                 $git_dir = "$projectroot/$pr->{'path'}";
2078                 my %co = parse_commit($head);
2079                 if (!%co) {
2080                         next;
2081                 }
2082                 $pr->{'commit'} = \%co;
2083                 if (!defined $pr->{'descr'}) {
2084                         my $descr = git_get_project_description($pr->{'path'}) || "";
2085                         $pr->{'descr'} = chop_str($descr, 25, 5);
2086                 }
2087                 if (!defined $pr->{'owner'}) {
2088                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
2089                 }
2090                 push @projects, $pr;
2091         }
2093         git_header_html();
2094         if (-f $home_text) {
2095                 print "<div class=\"index_include\">\n";
2096                 open (my $fd, $home_text);
2097                 print <$fd>;
2098                 close $fd;
2099                 print "</div>\n";
2100         }
2101         print "<table class=\"project_list\">\n" .
2102               "<tr>\n";
2103         $order ||= "project";
2104         if ($order eq "project") {
2105                 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2106                 print "<th>Project</th>\n";
2107         } else {
2108                 print "<th>" .
2109                       $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
2110                                -class => "header"}, "Project") .
2111                       "</th>\n";
2112         }
2113         if ($order eq "descr") {
2114                 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2115                 print "<th>Description</th>\n";
2116         } else {
2117                 print "<th>" .
2118                       $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
2119                                -class => "header"}, "Description") .
2120                       "</th>\n";
2121         }
2122         if ($order eq "owner") {
2123                 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2124                 print "<th>Owner</th>\n";
2125         } else {
2126                 print "<th>" .
2127                       $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
2128                                -class => "header"}, "Owner") .
2129                       "</th>\n";
2130         }
2131         if ($order eq "age") {
2132                 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2133                 print "<th>Last Change</th>\n";
2134         } else {
2135                 print "<th>" .
2136                       $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
2137                                -class => "header"}, "Last Change") .
2138                       "</th>\n";
2139         }
2140         print "<th></th>\n" .
2141               "</tr>\n";
2142         my $alternate = 0;
2143         foreach my $pr (@projects) {
2144                 if ($alternate) {
2145                         print "<tr class=\"dark\">\n";
2146                 } else {
2147                         print "<tr class=\"light\">\n";
2148                 }
2149                 $alternate ^= 1;
2150                 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2151                                         -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2152                       "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2153                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2154                 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2155                       $pr->{'commit'}{'age_string'} . "</td>\n" .
2156                       "<td class=\"link\">" .
2157                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
2158                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2159                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2160                       "</td>\n" .
2161                       "</tr>\n";
2162         }
2163         print "</table>\n";
2164         git_footer_html();
2167 sub git_summary {
2168         my $descr = git_get_project_description($project) || "none";
2169         my $head = git_get_head_hash($project);
2170         my %co = parse_commit($head);
2171         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2173         my $owner = git_get_project_owner($project);
2175         my $refs = git_get_references();
2176         git_header_html();
2177         git_print_page_nav('summary','', $head);
2179         print "<div class=\"title\">&nbsp;</div>\n";
2180         print "<table cellspacing=\"0\">\n" .
2181               "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2182               "<tr><td>owner</td><td>$owner</td></tr>\n" .
2183               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2184         # use per project git URL list in $projectroot/$project/cloneurl
2185         # or make project git URL from git base URL and project name
2186         my $url_tag = "URL";
2187         my @url_list = git_get_project_url_list($project);
2188         @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2189         foreach my $git_url (@url_list) {
2190                 next unless $git_url;
2191                 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2192                 $url_tag = "";
2193         }
2194         print "</table>\n";
2196         open my $fd, "-|", git_cmd(), "rev-list", "--max-count=17",
2197                 git_get_head_hash($project)
2198                 or die_error(undef, "Open git-rev-list failed");
2199         my @revlist = map { chomp; $_ } <$fd>;
2200         close $fd;
2201         git_print_header_div('shortlog');
2202         git_shortlog_body(\@revlist, 0, 15, $refs,
2203                           $cgi->a({-href => href(action=>"shortlog")}, "..."));
2205         my $taglist = git_get_refs_list("refs/tags");
2206         if (defined @$taglist) {
2207                 git_print_header_div('tags');
2208                 git_tags_body($taglist, 0, 15,
2209                               $cgi->a({-href => href(action=>"tags")}, "..."));
2210         }
2212         my $headlist = git_get_refs_list("refs/heads");
2213         if (defined @$headlist) {
2214                 git_print_header_div('heads');
2215                 git_heads_body($headlist, $head, 0, 15,
2216                                $cgi->a({-href => href(action=>"heads")}, "..."));
2217         }
2219         git_footer_html();
2222 sub git_tag {
2223         my $head = git_get_head_hash($project);
2224         git_header_html();
2225         git_print_page_nav('','', $head,undef,$head);
2226         my %tag = parse_tag($hash);
2227         git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2228         print "<div class=\"title_text\">\n" .
2229               "<table cellspacing=\"0\">\n" .
2230               "<tr>\n" .
2231               "<td>object</td>\n" .
2232               "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2233                                $tag{'object'}) . "</td>\n" .
2234               "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2235                                               $tag{'type'}) . "</td>\n" .
2236               "</tr>\n";
2237         if (defined($tag{'author'})) {
2238                 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2239                 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2240                 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2241                         sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2242                         "</td></tr>\n";
2243         }
2244         print "</table>\n\n" .
2245               "</div>\n";
2246         print "<div class=\"page_body\">";
2247         my $comment = $tag{'comment'};
2248         foreach my $line (@$comment) {
2249                 print esc_html($line) . "<br/>\n";
2250         }
2251         print "</div>\n";
2252         git_footer_html();
2255 sub git_blame2 {
2256         my $fd;
2257         my $ftype;
2259         my ($have_blame) = gitweb_check_feature('blame');
2260         if (!$have_blame) {
2261                 die_error('403 Permission denied', "Permission denied");
2262         }
2263         die_error('404 Not Found', "File name not defined") if (!$file_name);
2264         $hash_base ||= git_get_head_hash($project);
2265         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2266         my %co = parse_commit($hash_base)
2267                 or die_error(undef, "Reading commit failed");
2268         if (!defined $hash) {
2269                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2270                         or die_error(undef, "Error looking up file");
2271         }
2272         $ftype = git_get_type($hash);
2273         if ($ftype !~ "blob") {
2274                 die_error("400 Bad Request", "Object is not a blob");
2275         }
2276         open ($fd, "-|", git_cmd(), "blame", '-l', $file_name, $hash_base)
2277                 or die_error(undef, "Open git-blame failed");
2278         git_header_html();
2279         my $formats_nav =
2280                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2281                         "blob") .
2282                 " | " .
2283                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2284                         "head");
2285         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2286         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2287         git_print_page_path($file_name, $ftype, $hash_base);
2288         my @rev_color = (qw(light2 dark2));
2289         my $num_colors = scalar(@rev_color);
2290         my $current_color = 0;
2291         my $last_rev;
2292         print <<HTML;
2293 <div class="page_body">
2294 <table class="blame">
2295 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2296 HTML
2297         while (<$fd>) {
2298                 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2299                 my $full_rev = $1;
2300                 my $rev = substr($full_rev, 0, 8);
2301                 my $lineno = $2;
2302                 my $data = $3;
2304                 if (!defined $last_rev) {
2305                         $last_rev = $full_rev;
2306                 } elsif ($last_rev ne $full_rev) {
2307                         $last_rev = $full_rev;
2308                         $current_color = ++$current_color % $num_colors;
2309                 }
2310                 print "<tr class=\"$rev_color[$current_color]\">\n";
2311                 print "<td class=\"sha1\">" .
2312                         $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2313                                 esc_html($rev)) . "</td>\n";
2314                 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2315                       esc_html($lineno) . "</a></td>\n";
2316                 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2317                 print "</tr>\n";
2318         }
2319         print "</table>\n";
2320         print "</div>";
2321         close $fd
2322                 or print "Reading blob failed\n";
2323         git_footer_html();
2326 sub git_blame {
2327         my $fd;
2329         my ($have_blame) = gitweb_check_feature('blame');
2330         if (!$have_blame) {
2331                 die_error('403 Permission denied', "Permission denied");
2332         }
2333         die_error('404 Not Found', "File name not defined") if (!$file_name);
2334         $hash_base ||= git_get_head_hash($project);
2335         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2336         my %co = parse_commit($hash_base)
2337                 or die_error(undef, "Reading commit failed");
2338         if (!defined $hash) {
2339                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2340                         or die_error(undef, "Error lookup file");
2341         }
2342         open ($fd, "-|", git_cmd(), "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2343                 or die_error(undef, "Open git-annotate failed");
2344         git_header_html();
2345         my $formats_nav =
2346                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2347                         "blob") .
2348                 " | " .
2349                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2350                         "head");
2351         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2352         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2353         git_print_page_path($file_name, 'blob', $hash_base);
2354         print "<div class=\"page_body\">\n";
2355         print <<HTML;
2356 <table class="blame">
2357   <tr>
2358     <th>Commit</th>
2359     <th>Age</th>
2360     <th>Author</th>
2361     <th>Line</th>
2362     <th>Data</th>
2363   </tr>
2364 HTML
2365         my @line_class = (qw(light dark));
2366         my $line_class_len = scalar (@line_class);
2367         my $line_class_num = $#line_class;
2368         while (my $line = <$fd>) {
2369                 my $long_rev;
2370                 my $short_rev;
2371                 my $author;
2372                 my $time;
2373                 my $lineno;
2374                 my $data;
2375                 my $age;
2376                 my $age_str;
2377                 my $age_class;
2379                 chomp $line;
2380                 $line_class_num = ($line_class_num + 1) % $line_class_len;
2382                 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) [+-]\d\d\d\d\t(\d+)\)(.*)$/) {
2383                         $long_rev = $1;
2384                         $author   = $2;
2385                         $time     = $3;
2386                         $lineno   = $4;
2387                         $data     = $5;
2388                 } else {
2389                         print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2390                         next;
2391                 }
2392                 $short_rev  = substr ($long_rev, 0, 8);
2393                 $age        = time () - $time;
2394                 $age_str    = age_string ($age);
2395                 $age_str    =~ s/ /&nbsp;/g;
2396                 $age_class  = age_class($age);
2397                 $author     = esc_html ($author);
2398                 $author     =~ s/ /&nbsp;/g;
2400                 $data = untabify($data);
2401                 $data = esc_html ($data);
2403                 print <<HTML;
2404   <tr class="$line_class[$line_class_num]">
2405     <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2406     <td class="$age_class">$age_str</td>
2407     <td>$author</td>
2408     <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2409     <td class="pre">$data</td>
2410   </tr>
2411 HTML
2412         } # while (my $line = <$fd>)
2413         print "</table>\n\n";
2414         close $fd
2415                 or print "Reading blob failed.\n";
2416         print "</div>";
2417         git_footer_html();
2420 sub git_tags {
2421         my $head = git_get_head_hash($project);
2422         git_header_html();
2423         git_print_page_nav('','', $head,undef,$head);
2424         git_print_header_div('summary', $project);
2426         my $taglist = git_get_refs_list("refs/tags");
2427         if (defined @$taglist) {
2428                 git_tags_body($taglist);
2429         }
2430         git_footer_html();
2433 sub git_heads {
2434         my $head = git_get_head_hash($project);
2435         git_header_html();
2436         git_print_page_nav('','', $head,undef,$head);
2437         git_print_header_div('summary', $project);
2439         my $taglist = git_get_refs_list("refs/heads");
2440         if (defined @$taglist) {
2441                 git_heads_body($taglist, $head);
2442         }
2443         git_footer_html();
2446 sub git_blob_plain {
2447         # blobs defined by non-textual hash id's can be cached
2448         my $expires;
2449         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2450                 $expires = "+1d";
2451         }
2453         if (!defined $hash) {
2454                 if (defined $file_name) {
2455                         my $base = $hash_base || git_get_head_hash($project);
2456                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2457                                 or die_error(undef, "Error lookup file");
2458                 } else {
2459                         die_error(undef, "No file name defined");
2460                 }
2461         }
2462         my $type = shift;
2463         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2464                 or die_error(undef, "Couldn't cat $file_name, $hash");
2466         $type ||= blob_mimetype($fd, $file_name);
2468         # save as filename, even when no $file_name is given
2469         my $save_as = "$hash";
2470         if (defined $file_name) {
2471                 $save_as = $file_name;
2472         } elsif ($type =~ m/^text\//) {
2473                 $save_as .= '.txt';
2474         }
2476         print $cgi->header(
2477                 -type => "$type",
2478                 -expires=>$expires,
2479                 -content_disposition => "inline; filename=\"$save_as\"");
2480         undef $/;
2481         binmode STDOUT, ':raw';
2482         print <$fd>;
2483         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2484         $/ = "\n";
2485         close $fd;
2488 sub git_blob {
2489         # blobs defined by non-textual hash id's can be cached
2490         my $expires;
2491         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2492                 $expires = "+1d";
2493         }
2495         if (!defined $hash) {
2496                 if (defined $file_name) {
2497                         my $base = $hash_base || git_get_head_hash($project);
2498                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2499                                 or die_error(undef, "Error lookup file");
2500                 } else {
2501                         die_error(undef, "No file name defined");
2502                 }
2503         }
2504         my ($have_blame) = gitweb_check_feature('blame');
2505         open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
2506                 or die_error(undef, "Couldn't cat $file_name, $hash");
2507         my $mimetype = blob_mimetype($fd, $file_name);
2508         if ($mimetype !~ m/^text\//) {
2509                 close $fd;
2510                 return git_blob_plain($mimetype);
2511         }
2512         git_header_html(undef, $expires);
2513         my $formats_nav = '';
2514         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2515                 if (defined $file_name) {
2516                         if ($have_blame) {
2517                                 $formats_nav .=
2518                                         $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2519                                                                hash=>$hash, file_name=>$file_name)},
2520                                                 "blame") .
2521                                         " | ";
2522                         }
2523                         $formats_nav .=
2524                                 $cgi->a({-href => href(action=>"blob_plain",
2525                                                        hash=>$hash, file_name=>$file_name)},
2526                                         "plain") .
2527                                 " | " .
2528                                 $cgi->a({-href => href(action=>"blob",
2529                                                        hash_base=>"HEAD", file_name=>$file_name)},
2530                                         "head");
2531                 } else {
2532                         $formats_nav .=
2533                                 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2534                 }
2535                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2536                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2537         } else {
2538                 print "<div class=\"page_nav\">\n" .
2539                       "<br/><br/></div>\n" .
2540                       "<div class=\"title\">$hash</div>\n";
2541         }
2542         git_print_page_path($file_name, "blob", $hash_base);
2543         print "<div class=\"page_body\">\n";
2544         my $nr;
2545         while (my $line = <$fd>) {
2546                 chomp $line;
2547                 $nr++;
2548                 $line = untabify($line);
2549                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2550                        $nr, $nr, $nr, esc_html($line);
2551         }
2552         close $fd
2553                 or print "Reading blob failed.\n";
2554         print "</div>";
2555         git_footer_html();
2558 sub git_tree {
2559         if (!defined $hash) {
2560                 $hash = git_get_head_hash($project);
2561                 if (defined $file_name) {
2562                         my $base = $hash_base || $hash;
2563                         $hash = git_get_hash_by_path($base, $file_name, "tree");
2564                 }
2565                 if (!defined $hash_base) {
2566                         $hash_base = $hash;
2567                 }
2568         }
2569         $/ = "\0";
2570         open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
2571                 or die_error(undef, "Open git-ls-tree failed");
2572         my @entries = map { chomp; $_ } <$fd>;
2573         close $fd or die_error(undef, "Reading tree failed");
2574         $/ = "\n";
2576         my $refs = git_get_references();
2577         my $ref = format_ref_marker($refs, $hash_base);
2578         git_header_html();
2579         my $base = "";
2580         my ($have_blame) = gitweb_check_feature('blame');
2581         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2582                 git_print_page_nav('tree','', $hash_base);
2583                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2584         } else {
2585                 undef $hash_base;
2586                 print "<div class=\"page_nav\">\n";
2587                 print "<br/><br/></div>\n";
2588                 print "<div class=\"title\">$hash</div>\n";
2589         }
2590         if (defined $file_name) {
2591                 $base = esc_html("$file_name/");
2592         }
2593         git_print_page_path($file_name, 'tree', $hash_base);
2594         print "<div class=\"page_body\">\n";
2595         print "<table cellspacing=\"0\">\n";
2596         my $alternate = 0;
2597         foreach my $line (@entries) {
2598                 my %t = parse_ls_tree_line($line, -z => 1);
2600                 if ($alternate) {
2601                         print "<tr class=\"dark\">\n";
2602                 } else {
2603                         print "<tr class=\"light\">\n";
2604                 }
2605                 $alternate ^= 1;
2607                 git_print_tree_entry(\%t, $base, $hash_base, $have_blame);
2609                 print "</tr>\n";
2610         }
2611         print "</table>\n" .
2612               "</div>";
2613         git_footer_html();
2616 sub git_snapshot {
2618         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2619         my $have_snapshot = (defined $ctype && defined $suffix);
2620         if (!$have_snapshot) {
2621                 die_error('403 Permission denied', "Permission denied");
2622         }
2624         if (!defined $hash) {
2625                 $hash = git_get_head_hash($project);
2626         }
2628         my $filename = basename($project) . "-$hash.tar.$suffix";
2630         print $cgi->header(-type => 'application/x-tar',
2631                            -content_encoding => $ctype,
2632                            -content_disposition => "inline; filename=\"$filename\"",
2633                            -status => '200 OK');
2635         my $git_command = git_cmd_str();
2636         open my $fd, "-|", "$git_command tar-tree $hash \'$project\' | $command" or
2637                 die_error(undef, "Execute git-tar-tree failed.");
2638         binmode STDOUT, ':raw';
2639         print <$fd>;
2640         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2641         close $fd;
2645 sub git_log {
2646         my $head = git_get_head_hash($project);
2647         if (!defined $hash) {
2648                 $hash = $head;
2649         }
2650         if (!defined $page) {
2651                 $page = 0;
2652         }
2653         my $refs = git_get_references();
2655         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2656         open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
2657                 or die_error(undef, "Open git-rev-list failed");
2658         my @revlist = map { chomp; $_ } <$fd>;
2659         close $fd;
2661         my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2663         git_header_html();
2664         git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2666         if (!@revlist) {
2667                 my %co = parse_commit($hash);
2669                 git_print_header_div('summary', $project);
2670                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2671         }
2672         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2673                 my $commit = $revlist[$i];
2674                 my $ref = format_ref_marker($refs, $commit);
2675                 my %co = parse_commit($commit);
2676                 next if !%co;
2677                 my %ad = parse_date($co{'author_epoch'});
2678                 git_print_header_div('commit',
2679                                "<span class=\"age\">$co{'age_string'}</span>" .
2680                                esc_html($co{'title'}) . $ref,
2681                                $commit);
2682                 print "<div class=\"title_text\">\n" .
2683                       "<div class=\"log_link\">\n" .
2684                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2685                       " | " .
2686                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2687                       "<br/>\n" .
2688                       "</div>\n" .
2689                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
2690                       "</div>\n";
2692                 print "<div class=\"log_body\">\n";
2693                 git_print_simplified_log($co{'comment'});
2694                 print "</div>\n";
2695         }
2696         git_footer_html();
2699 sub git_commit {
2700         my %co = parse_commit($hash);
2701         if (!%co) {
2702                 die_error(undef, "Unknown commit object");
2703         }
2704         my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2705         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2707         my $parent = $co{'parent'};
2708         if (!defined $parent) {
2709                 $parent = "--root";
2710         }
2711         open my $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $parent, $hash
2712                 or die_error(undef, "Open git-diff-tree failed");
2713         my @difftree = map { chomp; $_ } <$fd>;
2714         close $fd or die_error(undef, "Reading git-diff-tree failed");
2716         # non-textual hash id's can be cached
2717         my $expires;
2718         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2719                 $expires = "+1d";
2720         }
2721         my $refs = git_get_references();
2722         my $ref = format_ref_marker($refs, $co{'id'});
2724         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2725         my $have_snapshot = (defined $ctype && defined $suffix);
2727         my $formats_nav = '';
2728         if (defined $file_name && defined $co{'parent'}) {
2729                 my $parent = $co{'parent'};
2730                 $formats_nav .=
2731                         $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2732                                 "blame");
2733         }
2734         git_header_html(undef, $expires);
2735         git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2736                            $hash, $co{'tree'}, $hash,
2737                            $formats_nav);
2739         if (defined $co{'parent'}) {
2740                 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2741         } else {
2742                 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2743         }
2744         print "<div class=\"title_text\">\n" .
2745               "<table cellspacing=\"0\">\n";
2746         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2747               "<tr>" .
2748               "<td></td><td> $ad{'rfc2822'}";
2749         if ($ad{'hour_local'} < 6) {
2750                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2751                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2752         } else {
2753                 printf(" (%02d:%02d %s)",
2754                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2755         }
2756         print "</td>" .
2757               "</tr>\n";
2758         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2759         print "<tr><td></td><td> $cd{'rfc2822'}" .
2760               sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2761               "</td></tr>\n";
2762         print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2763         print "<tr>" .
2764               "<td>tree</td>" .
2765               "<td class=\"sha1\">" .
2766               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2767                        class => "list"}, $co{'tree'}) .
2768               "</td>" .
2769               "<td class=\"link\">" .
2770               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2771                       "tree");
2772         if ($have_snapshot) {
2773                 print " | " .
2774                       $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2775         }
2776         print "</td>" .
2777               "</tr>\n";
2778         my $parents = $co{'parents'};
2779         foreach my $par (@$parents) {
2780                 print "<tr>" .
2781                       "<td>parent</td>" .
2782                       "<td class=\"sha1\">" .
2783                       $cgi->a({-href => href(action=>"commit", hash=>$par),
2784                                class => "list"}, $par) .
2785                       "</td>" .
2786                       "<td class=\"link\">" .
2787                       $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2788                       " | " .
2789                       $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "diff") .
2790                       "</td>" .
2791                       "</tr>\n";
2792         }
2793         print "</table>".
2794               "</div>\n";
2796         print "<div class=\"page_body\">\n";
2797         git_print_log($co{'comment'});
2798         print "</div>\n";
2800         git_difftree_body(\@difftree, $hash, $parent);
2802         git_footer_html();
2805 sub git_blobdiff {
2806         my $format = shift || 'html';
2808         my $fd;
2809         my @difftree;
2810         my %diffinfo;
2811         my $expires;
2813         # preparing $fd and %diffinfo for git_patchset_body
2814         # new style URI
2815         if (defined $hash_base && defined $hash_parent_base) {
2816                 if (defined $file_name) {
2817                         # read raw output
2818                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base,
2819                                 "--", $file_name
2820                                 or die_error(undef, "Open git-diff-tree failed");
2821                         @difftree = map { chomp; $_ } <$fd>;
2822                         close $fd
2823                                 or die_error(undef, "Reading git-diff-tree failed");
2824                         @difftree
2825                                 or die_error('404 Not Found', "Blob diff not found");
2827                 } elsif (defined $hash &&
2828                          $hash =~ /[0-9a-fA-F]{40}/) {
2829                         # try to find filename from $hash
2831                         # read filtered raw output
2832                         open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts, $hash_parent_base, $hash_base
2833                                 or die_error(undef, "Open git-diff-tree failed");
2834                         @difftree =
2835                                 # ':100644 100644 03b21826... 3b93d5e7... M     ls-files.c'
2836                                 # $hash == to_id
2837                                 grep { /^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/ }
2838                                 map { chomp; $_ } <$fd>;
2839                         close $fd
2840                                 or die_error(undef, "Reading git-diff-tree failed");
2841                         @difftree
2842                                 or die_error('404 Not Found', "Blob diff not found");
2844                 } else {
2845                         die_error('404 Not Found', "Missing one of the blob diff parameters");
2846                 }
2848                 if (@difftree > 1) {
2849                         die_error('404 Not Found', "Ambiguous blob diff specification");
2850                 }
2852                 %diffinfo = parse_difftree_raw_line($difftree[0]);
2853                 $file_parent ||= $diffinfo{'from_file'} || $file_name || $diffinfo{'file'};
2854                 $file_name   ||= $diffinfo{'to_file'}   || $diffinfo{'file'};
2856                 $hash_parent ||= $diffinfo{'from_id'};
2857                 $hash        ||= $diffinfo{'to_id'};
2859                 # non-textual hash id's can be cached
2860                 if ($hash_base =~ m/^[0-9a-fA-F]{40}$/ &&
2861                     $hash_parent_base =~ m/^[0-9a-fA-F]{40}$/) {
2862                         $expires = '+1d';
2863                 }
2865                 # open patch output
2866                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2867                         '-p', $hash_parent_base, $hash_base,
2868                         "--", $file_name
2869                         or die_error(undef, "Open git-diff-tree failed");
2870         }
2872         # old/legacy style URI
2873         if (!%diffinfo && # if new style URI failed
2874             defined $hash && defined $hash_parent) {
2875                 # fake git-diff-tree raw output
2876                 $diffinfo{'from_mode'} = $diffinfo{'to_mode'} = "blob";
2877                 $diffinfo{'from_id'} = $hash_parent;
2878                 $diffinfo{'to_id'}   = $hash;
2879                 if (defined $file_name) {
2880                         if (defined $file_parent) {
2881                                 $diffinfo{'status'} = '2';
2882                                 $diffinfo{'from_file'} = $file_parent;
2883                                 $diffinfo{'to_file'}   = $file_name;
2884                         } else { # assume not renamed
2885                                 $diffinfo{'status'} = '1';
2886                                 $diffinfo{'from_file'} = $file_name;
2887                                 $diffinfo{'to_file'}   = $file_name;
2888                         }
2889                 } else { # no filename given
2890                         $diffinfo{'status'} = '2';
2891                         $diffinfo{'from_file'} = $hash_parent;
2892                         $diffinfo{'to_file'}   = $hash;
2893                 }
2895                 # non-textual hash id's can be cached
2896                 if ($hash =~ m/^[0-9a-fA-F]{40}$/ &&
2897                     $hash_parent =~ m/^[0-9a-fA-F]{40}$/) {
2898                         $expires = '+1d';
2899                 }
2901                 # open patch output
2902                 open $fd, "-|", git_cmd(), "diff", '-p', @diff_opts, $hash_parent, $hash
2903                         or die_error(undef, "Open git-diff failed");
2904         } else  {
2905                 die_error('404 Not Found', "Missing one of the blob diff parameters")
2906                         unless %diffinfo;
2907         }
2909         # header
2910         if ($format eq 'html') {
2911                 my $formats_nav =
2912                         $cgi->a({-href => href(action=>"blobdiff_plain",
2913                                                hash=>$hash, hash_parent=>$hash_parent,
2914                                                hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
2915                                                file_name=>$file_name, file_parent=>$file_parent)},
2916                                 "plain");
2917                 git_header_html(undef, $expires);
2918                 if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2919                         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2920                         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2921                 } else {
2922                         print "<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";
2923                         print "<div class=\"title\">$hash vs $hash_parent</div>\n";
2924                 }
2925                 if (defined $file_name) {
2926                         git_print_page_path($file_name, "blob", $hash_base);
2927                 } else {
2928                         print "<div class=\"page_path\"></div>\n";
2929                 }
2931         } elsif ($format eq 'plain') {
2932                 print $cgi->header(
2933                         -type => 'text/plain',
2934                         -charset => 'utf-8',
2935                         -expires => $expires,
2936                         -content_disposition => qq(inline; filename="${file_name}.patch"));
2938                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2940         } else {
2941                 die_error(undef, "Unknown blobdiff format");
2942         }
2944         # patch
2945         if ($format eq 'html') {
2946                 print "<div class=\"page_body\">\n";
2948                 git_patchset_body($fd, [ \%diffinfo ], $hash_base, $hash_parent_base);
2949                 close $fd;
2951                 print "</div>\n"; # class="page_body"
2952                 git_footer_html();
2954         } else {
2955                 while (my $line = <$fd>) {
2956                         $line =~ s!a/($hash|$hash_parent)!a/$diffinfo{'from_file'}!g;
2957                         $line =~ s!b/($hash|$hash_parent)!b/$diffinfo{'to_file'}!g;
2959                         print $line;
2961                         last if $line =~ m!^\+\+\+!;
2962                 }
2963                 local $/ = undef;
2964                 print <$fd>;
2965                 close $fd;
2966         }
2969 sub git_blobdiff_plain {
2970         git_blobdiff('plain');
2973 sub git_commitdiff {
2974         my $format = shift || 'html';
2975         my %co = parse_commit($hash);
2976         if (!%co) {
2977                 die_error(undef, "Unknown commit object");
2978         }
2979         if (!defined $hash_parent) {
2980                 $hash_parent = $co{'parent'} || '--root';
2981         }
2983         # read commitdiff
2984         my $fd;
2985         my @difftree;
2986         if ($format eq 'html') {
2987                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2988                         "--patch-with-raw", "--full-index", $hash_parent, $hash
2989                         or die_error(undef, "Open git-diff-tree failed");
2991                 while (chomp(my $line = <$fd>)) {
2992                         # empty line ends raw part of diff-tree output
2993                         last unless $line;
2994                         push @difftree, $line;
2995                 }
2997         } elsif ($format eq 'plain') {
2998                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
2999                         '-p', $hash_parent, $hash
3000                         or die_error(undef, "Open git-diff-tree failed");
3002         } else {
3003                 die_error(undef, "Unknown commitdiff format");
3004         }
3006         # non-textual hash id's can be cached
3007         my $expires;
3008         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
3009                 $expires = "+1d";
3010         }
3012         # write commit message
3013         if ($format eq 'html') {
3014                 my $refs = git_get_references();
3015                 my $ref = format_ref_marker($refs, $co{'id'});
3016                 my $formats_nav =
3017                         $cgi->a({-href => href(action=>"commitdiff_plain",
3018                                                hash=>$hash, hash_parent=>$hash_parent)},
3019                                 "plain");
3021                 git_header_html(undef, $expires);
3022                 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
3023                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
3024                 git_print_authorship(\%co);
3025                 print "<div class=\"page_body\">\n";
3026                 print "<div class=\"log\">\n";
3027                 git_print_simplified_log($co{'comment'}, 1); # skip title
3028                 print "</div>\n"; # class="log"
3030         } elsif ($format eq 'plain') {
3031                 my $refs = git_get_references("tags");
3032                 my $tagname = git_get_rev_name_tags($hash);
3033                 my $filename = basename($project) . "-$hash.patch";
3035                 print $cgi->header(
3036                         -type => 'text/plain',
3037                         -charset => 'utf-8',
3038                         -expires => $expires,
3039                         -content_disposition => qq(inline; filename="$filename"));
3040                 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
3041                 print <<TEXT;
3042 From: $co{'author'}
3043 Date: $ad{'rfc2822'} ($ad{'tz_local'})
3044 Subject: $co{'title'}
3045 TEXT
3046                 print "X-Git-Tag: $tagname\n" if $tagname;
3047                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
3049                 foreach my $line (@{$co{'comment'}}) {
3050                         print "$line\n";
3051                 }
3052                 print "---\n\n";
3053         }
3055         # write patch
3056         if ($format eq 'html') {
3057                 git_difftree_body(\@difftree, $hash, $hash_parent);
3058                 print "<br/>\n";
3060                 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
3061                 close $fd;
3062                 print "</div>\n"; # class="page_body"
3063                 git_footer_html();
3065         } elsif ($format eq 'plain') {
3066                 local $/ = undef;
3067                 print <$fd>;
3068                 close $fd
3069                         or print "Reading git-diff-tree failed\n";
3070         }
3073 sub git_commitdiff_plain {
3074         git_commitdiff('plain');
3077 sub git_history {
3078         if (!defined $hash_base) {
3079                 $hash_base = git_get_head_hash($project);
3080         }
3081         my $ftype;
3082         my %co = parse_commit($hash_base);
3083         if (!%co) {
3084                 die_error(undef, "Unknown commit object");
3085         }
3086         my $refs = git_get_references();
3087         git_header_html();
3088         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
3089         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
3090         if (!defined $hash && defined $file_name) {
3091                 $hash = git_get_hash_by_path($hash_base, $file_name);
3092         }
3093         if (defined $hash) {
3094                 $ftype = git_get_type($hash);
3095         }
3096         git_print_page_path($file_name, $ftype, $hash_base);
3098         open my $fd, "-|",
3099                 git_cmd(), "rev-list", "--full-history", $hash_base, "--", $file_name;
3101         git_history_body($fd, $refs, $hash_base, $ftype);
3103         close $fd;
3104         git_footer_html();
3107 sub git_search {
3108         if (!defined $searchtext) {
3109                 die_error(undef, "Text field empty");
3110         }
3111         if (!defined $hash) {
3112                 $hash = git_get_head_hash($project);
3113         }
3114         my %co = parse_commit($hash);
3115         if (!%co) {
3116                 die_error(undef, "Unknown commit object");
3117         }
3118         # pickaxe may take all resources of your box and run for several minutes
3119         # with every query - so decide by yourself how public you make this feature :)
3120         my $commit_search = 1;
3121         my $author_search = 0;
3122         my $committer_search = 0;
3123         my $pickaxe_search = 0;
3124         if ($searchtext =~ s/^author\\://i) {
3125                 $author_search = 1;
3126         } elsif ($searchtext =~ s/^committer\\://i) {
3127                 $committer_search = 1;
3128         } elsif ($searchtext =~ s/^pickaxe\\://i) {
3129                 $commit_search = 0;
3130                 $pickaxe_search = 1;
3131         }
3132         git_header_html();
3133         git_print_page_nav('','', $hash,$co{'tree'},$hash);
3134         git_print_header_div('commit', esc_html($co{'title'}), $hash);
3136         print "<table cellspacing=\"0\">\n";
3137         my $alternate = 0;
3138         if ($commit_search) {
3139                 $/ = "\0";
3140                 open my $fd, "-|", git_cmd(), "rev-list", "--header", "--parents", $hash or next;
3141                 while (my $commit_text = <$fd>) {
3142                         if (!grep m/$searchtext/i, $commit_text) {
3143                                 next;
3144                         }
3145                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
3146                                 next;
3147                         }
3148                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
3149                                 next;
3150                         }
3151                         my @commit_lines = split "\n", $commit_text;
3152                         my %co = parse_commit(undef, \@commit_lines);
3153                         if (!%co) {
3154                                 next;
3155                         }
3156                         if ($alternate) {
3157                                 print "<tr class=\"dark\">\n";
3158                         } else {
3159                                 print "<tr class=\"light\">\n";
3160                         }
3161                         $alternate ^= 1;
3162                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3163                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3164                               "<td>" .
3165                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
3166                                        esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3167                         my $comment = $co{'comment'};
3168                         foreach my $line (@$comment) {
3169                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
3170                                         my $lead = esc_html($1) || "";
3171                                         $lead = chop_str($lead, 30, 10);
3172                                         my $match = esc_html($2) || "";
3173                                         my $trail = esc_html($3) || "";
3174                                         $trail = chop_str($trail, 30, 10);
3175                                         my $text = "$lead<span class=\"match\">$match</span>$trail";
3176                                         print chop_str($text, 80, 5) . "<br/>\n";
3177                                 }
3178                         }
3179                         print "</td>\n" .
3180                               "<td class=\"link\">" .
3181                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3182                               " | " .
3183                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3184                         print "</td>\n" .
3185                               "</tr>\n";
3186                 }
3187                 close $fd;
3188         }
3190         if ($pickaxe_search) {
3191                 $/ = "\n";
3192                 my $git_command = git_cmd_str();
3193                 open my $fd, "-|", "$git_command rev-list $hash | " .
3194                         "$git_command diff-tree -r --stdin -S\'$searchtext\'";
3195                 undef %co;
3196                 my @files;
3197                 while (my $line = <$fd>) {
3198                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
3199                                 my %set;
3200                                 $set{'file'} = $6;
3201                                 $set{'from_id'} = $3;
3202                                 $set{'to_id'} = $4;
3203                                 $set{'id'} = $set{'to_id'};
3204                                 if ($set{'id'} =~ m/0{40}/) {
3205                                         $set{'id'} = $set{'from_id'};
3206                                 }
3207                                 if ($set{'id'} =~ m/0{40}/) {
3208                                         next;
3209                                 }
3210                                 push @files, \%set;
3211                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3212                                 if (%co) {
3213                                         if ($alternate) {
3214                                                 print "<tr class=\"dark\">\n";
3215                                         } else {
3216                                                 print "<tr class=\"light\">\n";
3217                                         }
3218                                         $alternate ^= 1;
3219                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3220                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3221                                               "<td>" .
3222                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3223                                                       -class => "list subject"},
3224                                                       esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3225                                         while (my $setref = shift @files) {
3226                                                 my %set = %$setref;
3227                                                 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3228                                                                              hash=>$set{'id'}, file_name=>$set{'file'}),
3229                                                               -class => "list"},
3230                                                               "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3231                                                       "<br/>\n";
3232                                         }
3233                                         print "</td>\n" .
3234                                               "<td class=\"link\">" .
3235                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3236                                               " | " .
3237                                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3238                                         print "</td>\n" .
3239                                               "</tr>\n";
3240                                 }
3241                                 %co = parse_commit($1);
3242                         }
3243                 }
3244                 close $fd;
3245         }
3246         print "</table>\n";
3247         git_footer_html();
3250 sub git_shortlog {
3251         my $head = git_get_head_hash($project);
3252         if (!defined $hash) {
3253                 $hash = $head;
3254         }
3255         if (!defined $page) {
3256                 $page = 0;
3257         }
3258         my $refs = git_get_references();
3260         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3261         open my $fd, "-|", git_cmd(), "rev-list", $limit, $hash
3262                 or die_error(undef, "Open git-rev-list failed");
3263         my @revlist = map { chomp; $_ } <$fd>;
3264         close $fd;
3266         my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3267         my $next_link = '';
3268         if ($#revlist >= (100 * ($page+1)-1)) {
3269                 $next_link =
3270                         $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3271                                  -title => "Alt-n"}, "next");
3272         }
3275         git_header_html();
3276         git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3277         git_print_header_div('summary', $project);
3279         git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3281         git_footer_html();
3284 ## ......................................................................
3285 ## feeds (RSS, OPML)
3287 sub git_rss {
3288         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3289         open my $fd, "-|", git_cmd(), "rev-list", "--max-count=150", git_get_head_hash($project)
3290                 or die_error(undef, "Open git-rev-list failed");
3291         my @revlist = map { chomp; $_ } <$fd>;
3292         close $fd or die_error(undef, "Reading git-rev-list failed");
3293         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3294         print <<XML;
3295 <?xml version="1.0" encoding="utf-8"?>
3296 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3297 <channel>
3298 <title>$project $my_uri $my_url</title>
3299 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3300 <description>$project log</description>
3301 <language>en</language>
3302 XML
3304         for (my $i = 0; $i <= $#revlist; $i++) {
3305                 my $commit = $revlist[$i];
3306                 my %co = parse_commit($commit);
3307                 # we read 150, we always show 30 and the ones more recent than 48 hours
3308                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3309                         last;
3310                 }
3311                 my %cd = parse_date($co{'committer_epoch'});
3312                 open $fd, "-|", git_cmd(), "diff-tree", '-r', @diff_opts,
3313                         $co{'parent'}, $co{'id'}
3314                         or next;
3315                 my @difftree = map { chomp; $_ } <$fd>;
3316                 close $fd
3317                         or next;
3318                 print "<item>\n" .
3319                       "<title>" .
3320                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3321                       "</title>\n" .
3322                       "<author>" . esc_html($co{'author'}) . "</author>\n" .
3323                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3324                       "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3325                       "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3326                       "<description>" . esc_html($co{'title'}) . "</description>\n" .
3327                       "<content:encoded>" .
3328                       "<![CDATA[\n";
3329                 my $comment = $co{'comment'};
3330                 foreach my $line (@$comment) {
3331                         $line = decode("utf8", $line, Encode::FB_DEFAULT);
3332                         print "$line<br/>\n";
3333                 }
3334                 print "<br/>\n";
3335                 foreach my $line (@difftree) {
3336                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3337                                 next;
3338                         }
3339                         my $file = validate_input(unquote($7));
3340                         $file = decode("utf8", $file, Encode::FB_DEFAULT);
3341                         print "$file<br/>\n";
3342                 }
3343                 print "]]>\n" .
3344                       "</content:encoded>\n" .
3345                       "</item>\n";
3346         }
3347         print "</channel></rss>";
3350 sub git_opml {
3351         my @list = git_get_projects_list();
3353         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3354         print <<XML;
3355 <?xml version="1.0" encoding="utf-8"?>
3356 <opml version="1.0">
3357 <head>
3358   <title>$site_name Git OPML Export</title>
3359 </head>
3360 <body>
3361 <outline text="git RSS feeds">
3362 XML
3364         foreach my $pr (@list) {
3365                 my %proj = %$pr;
3366                 my $head = git_get_head_hash($proj{'path'});
3367                 if (!defined $head) {
3368                         next;
3369                 }
3370                 $git_dir = "$projectroot/$proj{'path'}";
3371                 my %co = parse_commit($head);
3372                 if (!%co) {
3373                         next;
3374                 }
3376                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3377                 my $rss  = "$my_url?p=$proj{'path'};a=rss";
3378                 my $html = "$my_url?p=$proj{'path'};a=summary";
3379                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3380         }
3381         print <<XML;
3382 </outline>
3383 </body>
3384 </opml>
3385 XML