Code

gitweb: Use underscore instead of hyphen to separate words in HTTP headers names
[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 # location for temporary files needed for diffs
35 our $git_temp = "/tmp/gitweb";
37 # target of the home link on top of all pages
38 our $home_link = $my_uri || "/";
40 # string of the home link on top of all pages
41 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
43 # name of your site or organization to appear in page titles
44 # replace this with something more descriptive for clearer bookmarks
45 our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
47 # html text to include at home page
48 our $home_text = "++GITWEB_HOMETEXT++";
50 # URI of default stylesheet
51 our $stylesheet = "++GITWEB_CSS++";
52 # URI of GIT logo
53 our $logo = "++GITWEB_LOGO++";
55 # source of projects list
56 our $projects_list = "++GITWEB_LIST++";
58 # list of git base URLs used for URL to where fetch project from,
59 # i.e. full URL is "$git_base_url/$project"
60 our @git_base_url_list = ("++GITWEB_BASE_URL++");
62 # default blob_plain mimetype and default charset for text/plain blob
63 our $default_blob_plain_mimetype = 'text/plain';
64 our $default_text_plain_charset  = undef;
66 # file to use for guessing MIME types before trying /etc/mime.types
67 # (relative to the current git repository)
68 our $mimetypes_file = undef;
70 # You define site-wide feature defaults here; override them with
71 # $GITWEB_CONFIG as necessary.
72 our %feature = (
73         # feature => {'sub' => feature-sub, 'override' => allow-override, 'default' => [ default options...]
74         # if feature is overridable, feature-sub will be called with default options;
75         # return value indicates if to enable specified feature
77         'blame' => {
78                 'sub' => \&feature_blame,
79                 'override' => 0,
80                 'default' => [0]},
82         'snapshot' => {
83                 'sub' => \&feature_snapshot,
84                 'override' => 0,
85                 #         => [content-encoding, suffix, program]
86                 'default' => ['x-gzip', 'gz', 'gzip']},
87 );
89 sub gitweb_check_feature {
90         my ($name) = @_;
91         return undef unless exists $feature{$name};
92         my ($sub, $override, @defaults) = (
93                 $feature{$name}{'sub'},
94                 $feature{$name}{'override'},
95                 @{$feature{$name}{'default'}});
96         if (!$override) { return @defaults; }
97         return $sub->(@defaults);
98 }
100 # To enable system wide have in $GITWEB_CONFIG
101 # $feature{'blame'}{'default'} =  [1];
102 # To have project specific config enable override in  $GITWEB_CONFIG
103 # $feature{'blame'}{'override'} =  1;
104 # and in project config gitweb.blame = 0|1;
106 sub feature_blame {
107         my ($val) = git_get_project_config('blame', '--bool');
109         if ($val eq 'true') {
110                 return 1;
111         } elsif ($val eq 'false') {
112                 return 0;
113         }
115         return $_[0];
118 # To disable system wide have in $GITWEB_CONFIG
119 # $feature{'snapshot'}{'default'} =  [undef];
120 # To have project specific config enable override in  $GITWEB_CONFIG
121 # $feature{'blame'}{'override'} =  1;
122 # and in project config  gitweb.snapshot = none|gzip|bzip2
124 sub feature_snapshot {
125         my ($ctype, $suffix, $command) = @_;
127         my ($val) = git_get_project_config('snapshot');
129         if ($val eq 'gzip') {
130                 return ('x-gzip', 'gz', 'gzip');
131         } elsif ($val eq 'bzip2') {
132                 return ('x-bzip2', 'bz2', 'bzip2');
133         } elsif ($val eq 'none') {
134                 return ();
135         }
137         return ($ctype, $suffix, $command);
140 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
141 require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
143 # version of the core git binary
144 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
146 $projects_list ||= $projectroot;
147 if (! -d $git_temp) {
148         mkdir($git_temp, 0700) || die_error(undef, "Couldn't mkdir $git_temp");
151 # ======================================================================
152 # input validation and dispatch
153 our $action = $cgi->param('a');
154 if (defined $action) {
155         if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
156                 die_error(undef, "Invalid action parameter");
157         }
158         # action which does not check rest of parameters
159         if ($action eq "opml") {
160                 git_opml();
161                 exit;
162         }
165 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
166 if (defined $project) {
167         $project =~ s|^/||;
168         $project =~ s|/$||;
169         $project = undef unless $project;
171 if (defined $project) {
172         if (!validate_input($project)) {
173                 die_error(undef, "Invalid project parameter");
174         }
175         if (!(-d "$projectroot/$project")) {
176                 die_error(undef, "No such directory");
177         }
178         if (!(-e "$projectroot/$project/HEAD")) {
179                 die_error(undef, "No such project");
180         }
181         $ENV{'GIT_DIR'} = "$projectroot/$project";
182 } else {
183         git_project_list();
184         exit;
187 our $file_name = $cgi->param('f');
188 if (defined $file_name) {
189         if (!validate_input($file_name)) {
190                 die_error(undef, "Invalid file parameter");
191         }
194 our $file_parent = $cgi->param('fp');
195 if (defined $file_parent) {
196         if (!validate_input($file_parent)) {
197                 die_error(undef, "Invalid file parent parameter");
198         }
201 our $hash = $cgi->param('h');
202 if (defined $hash) {
203         if (!validate_input($hash)) {
204                 die_error(undef, "Invalid hash parameter");
205         }
208 our $hash_parent = $cgi->param('hp');
209 if (defined $hash_parent) {
210         if (!validate_input($hash_parent)) {
211                 die_error(undef, "Invalid hash parent parameter");
212         }
215 our $hash_base = $cgi->param('hb');
216 if (defined $hash_base) {
217         if (!validate_input($hash_base)) {
218                 die_error(undef, "Invalid hash base parameter");
219         }
222 our $page = $cgi->param('pg');
223 if (defined $page) {
224         if ($page =~ m/[^0-9]$/) {
225                 die_error(undef, "Invalid page parameter");
226         }
229 our $searchtext = $cgi->param('s');
230 if (defined $searchtext) {
231         if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
232                 die_error(undef, "Invalid search parameter");
233         }
234         $searchtext = quotemeta $searchtext;
237 # dispatch
238 my %actions = (
239         "blame" => \&git_blame2,
240         "blobdiff" => \&git_blobdiff,
241         "blobdiff_plain" => \&git_blobdiff_plain,
242         "blob" => \&git_blob,
243         "blob_plain" => \&git_blob_plain,
244         "commitdiff" => \&git_commitdiff,
245         "commitdiff_plain" => \&git_commitdiff_plain,
246         "commit" => \&git_commit,
247         "heads" => \&git_heads,
248         "history" => \&git_history,
249         "log" => \&git_log,
250         "rss" => \&git_rss,
251         "search" => \&git_search,
252         "shortlog" => \&git_shortlog,
253         "summary" => \&git_summary,
254         "tag" => \&git_tag,
255         "tags" => \&git_tags,
256         "tree" => \&git_tree,
257         "snapshot" => \&git_snapshot,
258 );
260 $action = 'summary' if (!defined($action));
261 if (!defined($actions{$action})) {
262         die_error(undef, "Unknown action");
264 $actions{$action}->();
265 exit;
267 ## ======================================================================
268 ## action links
270 sub href(%) {
271         my %mapping = (
272                 action => "a",
273                 project => "p",
274                 file_name => "f",
275                 file_parent => "fp",
276                 hash => "h",
277                 hash_parent => "hp",
278                 hash_base => "hb",
279                 page => "pg",
280                 searchtext => "s",
281         );
283         my %params = @_;
284         $params{"project"} ||= $project;
286         my $href = "$my_uri?";
287         $href .= esc_param( join(";",
288                 map {
289                         "$mapping{$_}=$params{$_}" if defined $params{$_}
290                 } keys %params
291         ) );
293         return $href;
297 ## ======================================================================
298 ## validation, quoting/unquoting and escaping
300 sub validate_input {
301         my $input = shift;
303         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
304                 return $input;
305         }
306         if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
307                 return undef;
308         }
309         if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
310                 return undef;
311         }
312         return $input;
315 # quote unsafe chars, but keep the slash, even when it's not
316 # correct, but quoted slashes look too horrible in bookmarks
317 sub esc_param {
318         my $str = shift;
319         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
320         $str =~ s/\+/%2B/g;
321         $str =~ s/ /\+/g;
322         return $str;
325 # replace invalid utf8 character with SUBSTITUTION sequence
326 sub esc_html {
327         my $str = shift;
328         $str = decode("utf8", $str, Encode::FB_DEFAULT);
329         $str = escapeHTML($str);
330         $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
331         return $str;
334 # git may return quoted and escaped filenames
335 sub unquote {
336         my $str = shift;
337         if ($str =~ m/^"(.*)"$/) {
338                 $str = $1;
339                 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
340         }
341         return $str;
344 # escape tabs (convert tabs to spaces)
345 sub untabify {
346         my $line = shift;
348         while ((my $pos = index($line, "\t")) != -1) {
349                 if (my $count = (8 - ($pos % 8))) {
350                         my $spaces = ' ' x $count;
351                         $line =~ s/\t/$spaces/;
352                 }
353         }
355         return $line;
358 ## ----------------------------------------------------------------------
359 ## HTML aware string manipulation
361 sub chop_str {
362         my $str = shift;
363         my $len = shift;
364         my $add_len = shift || 10;
366         # allow only $len chars, but don't cut a word if it would fit in $add_len
367         # if it doesn't fit, cut it if it's still longer than the dots we would add
368         $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
369         my $body = $1;
370         my $tail = $2;
371         if (length($tail) > 4) {
372                 $tail = " ...";
373                 $body =~ s/&[^;]*$//; # remove chopped character entities
374         }
375         return "$body$tail";
378 ## ----------------------------------------------------------------------
379 ## functions returning short strings
381 # CSS class for given age value (in seconds)
382 sub age_class {
383         my $age = shift;
385         if ($age < 60*60*2) {
386                 return "age0";
387         } elsif ($age < 60*60*24*2) {
388                 return "age1";
389         } else {
390                 return "age2";
391         }
394 # convert age in seconds to "nn units ago" string
395 sub age_string {
396         my $age = shift;
397         my $age_str;
399         if ($age > 60*60*24*365*2) {
400                 $age_str = (int $age/60/60/24/365);
401                 $age_str .= " years ago";
402         } elsif ($age > 60*60*24*(365/12)*2) {
403                 $age_str = int $age/60/60/24/(365/12);
404                 $age_str .= " months ago";
405         } elsif ($age > 60*60*24*7*2) {
406                 $age_str = int $age/60/60/24/7;
407                 $age_str .= " weeks ago";
408         } elsif ($age > 60*60*24*2) {
409                 $age_str = int $age/60/60/24;
410                 $age_str .= " days ago";
411         } elsif ($age > 60*60*2) {
412                 $age_str = int $age/60/60;
413                 $age_str .= " hours ago";
414         } elsif ($age > 60*2) {
415                 $age_str = int $age/60;
416                 $age_str .= " min ago";
417         } elsif ($age > 2) {
418                 $age_str = int $age;
419                 $age_str .= " sec ago";
420         } else {
421                 $age_str .= " right now";
422         }
423         return $age_str;
426 # convert file mode in octal to symbolic file mode string
427 sub mode_str {
428         my $mode = oct shift;
430         if (S_ISDIR($mode & S_IFMT)) {
431                 return 'drwxr-xr-x';
432         } elsif (S_ISLNK($mode)) {
433                 return 'lrwxrwxrwx';
434         } elsif (S_ISREG($mode)) {
435                 # git cares only about the executable bit
436                 if ($mode & S_IXUSR) {
437                         return '-rwxr-xr-x';
438                 } else {
439                         return '-rw-r--r--';
440                 };
441         } else {
442                 return '----------';
443         }
446 # convert file mode in octal to file type string
447 sub file_type {
448         my $mode = oct shift;
450         if (S_ISDIR($mode & S_IFMT)) {
451                 return "directory";
452         } elsif (S_ISLNK($mode)) {
453                 return "symlink";
454         } elsif (S_ISREG($mode)) {
455                 return "file";
456         } else {
457                 return "unknown";
458         }
461 ## ----------------------------------------------------------------------
462 ## functions returning short HTML fragments, or transforming HTML fragments
463 ## which don't beling to other sections
465 # format line of commit message or tag comment
466 sub format_log_line_html {
467         my $line = shift;
469         $line = esc_html($line);
470         $line =~ s/ /&nbsp;/g;
471         if ($line =~ m/([0-9a-fA-F]{40})/) {
472                 my $hash_text = $1;
473                 if (git_get_type($hash_text) eq "commit") {
474                         my $link =
475                                 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
476                                         -class => "text"}, $hash_text);
477                         $line =~ s/$hash_text/$link/;
478                 }
479         }
480         return $line;
483 # format marker of refs pointing to given object
484 sub format_ref_marker {
485         my ($refs, $id) = @_;
486         my $markers = '';
488         if (defined $refs->{$id}) {
489                 foreach my $ref (@{$refs->{$id}}) {
490                         my ($type, $name) = qw();
491                         # e.g. tags/v2.6.11 or heads/next
492                         if ($ref =~ m!^(.*?)s?/(.*)$!) {
493                                 $type = $1;
494                                 $name = $2;
495                         } else {
496                                 $type = "ref";
497                                 $name = $ref;
498                         }
500                         $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
501                 }
502         }
504         if ($markers) {
505                 return ' <span class="refs">'. $markers . '</span>';
506         } else {
507                 return "";
508         }
511 # format, perhaps shortened and with markers, title line
512 sub format_subject_html {
513         my ($long, $short, $href, $extra) = @_;
514         $extra = '' unless defined($extra);
516         if (length($short) < length($long)) {
517                 return $cgi->a({-href => $href, -class => "list subject",
518                                 -title => $long},
519                        esc_html($short) . $extra);
520         } else {
521                 return $cgi->a({-href => $href, -class => "list subject"},
522                        esc_html($long)  . $extra);
523         }
526 ## ----------------------------------------------------------------------
527 ## git utility subroutines, invoking git commands
529 # get HEAD ref of given project as hash
530 sub git_get_head_hash {
531         my $project = shift;
532         my $oENV = $ENV{'GIT_DIR'};
533         my $retval = undef;
534         $ENV{'GIT_DIR'} = "$projectroot/$project";
535         if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
536                 my $head = <$fd>;
537                 close $fd;
538                 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
539                         $retval = $1;
540                 }
541         }
542         if (defined $oENV) {
543                 $ENV{'GIT_DIR'} = $oENV;
544         }
545         return $retval;
548 # get type of given object
549 sub git_get_type {
550         my $hash = shift;
552         open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
553         my $type = <$fd>;
554         close $fd or return;
555         chomp $type;
556         return $type;
559 sub git_get_project_config {
560         my ($key, $type) = @_;
562         return unless ($key);
563         $key =~ s/^gitweb\.//;
564         return if ($key =~ m/\W/);
566         my @x = ($GIT, 'repo-config');
567         if (defined $type) { push @x, $type; }
568         push @x, "--get";
569         push @x, "gitweb.$key";
570         my $val = qx(@x);
571         chomp $val;
572         return ($val);
575 # get hash of given path at given ref
576 sub git_get_hash_by_path {
577         my $base = shift;
578         my $path = shift || return undef;
580         my $tree = $base;
582         open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
583                 or die_error(undef, "Open git-ls-tree failed");
584         my $line = <$fd>;
585         close $fd or return undef;
587         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
588         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
589         return $3;
592 ## ......................................................................
593 ## git utility functions, directly accessing git repository
595 # assumes that PATH is not symref
596 sub git_get_hash_by_ref {
597         my $path = shift;
599         open my $fd, "$projectroot/$path" or return undef;
600         my $head = <$fd>;
601         close $fd;
602         chomp $head;
603         if ($head =~ m/^[0-9a-fA-F]{40}$/) {
604                 return $head;
605         }
608 sub git_get_project_description {
609         my $path = shift;
611         open my $fd, "$projectroot/$path/description" or return undef;
612         my $descr = <$fd>;
613         close $fd;
614         chomp $descr;
615         return $descr;
618 sub git_get_project_url_list {
619         my $path = shift;
621         open my $fd, "$projectroot/$path/cloneurl" or return undef;
622         my @git_project_url_list = map { chomp; $_ } <$fd>;
623         close $fd;
625         return wantarray ? @git_project_url_list : \@git_project_url_list;
628 sub git_get_projects_list {
629         my @list;
631         if (-d $projects_list) {
632                 # search in directory
633                 my $dir = $projects_list;
634                 opendir my ($dh), $dir or return undef;
635                 while (my $dir = readdir($dh)) {
636                         if (-e "$projectroot/$dir/HEAD") {
637                                 my $pr = {
638                                         path => $dir,
639                                 };
640                                 push @list, $pr
641                         }
642                 }
643                 closedir($dh);
644         } elsif (-f $projects_list) {
645                 # read from file(url-encoded):
646                 # 'git%2Fgit.git Linus+Torvalds'
647                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
648                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
649                 open my ($fd), $projects_list or return undef;
650                 while (my $line = <$fd>) {
651                         chomp $line;
652                         my ($path, $owner) = split ' ', $line;
653                         $path = unescape($path);
654                         $owner = unescape($owner);
655                         if (!defined $path) {
656                                 next;
657                         }
658                         if (-e "$projectroot/$path/HEAD") {
659                                 my $pr = {
660                                         path => $path,
661                                         owner => decode("utf8", $owner, Encode::FB_DEFAULT),
662                                 };
663                                 push @list, $pr
664                         }
665                 }
666                 close $fd;
667         }
668         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
669         return @list;
672 sub git_get_project_owner {
673         my $project = shift;
674         my $owner;
676         return undef unless $project;
678         # read from file (url-encoded):
679         # 'git%2Fgit.git Linus+Torvalds'
680         # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
681         # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
682         if (-f $projects_list) {
683                 open (my $fd , $projects_list);
684                 while (my $line = <$fd>) {
685                         chomp $line;
686                         my ($pr, $ow) = split ' ', $line;
687                         $pr = unescape($pr);
688                         $ow = unescape($ow);
689                         if ($pr eq $project) {
690                                 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
691                                 last;
692                         }
693                 }
694                 close $fd;
695         }
696         if (!defined $owner) {
697                 $owner = get_file_owner("$projectroot/$project");
698         }
700         return $owner;
703 sub git_get_references {
704         my $type = shift || "";
705         my %refs;
706         my $fd;
707         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
708         # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
709         if (-f "$projectroot/$project/info/refs") {
710                 open $fd, "$projectroot/$project/info/refs"
711                         or return;
712         } else {
713                 open $fd, "-|", $GIT, "ls-remote", "."
714                         or return;
715         }
717         while (my $line = <$fd>) {
718                 chomp $line;
719                 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
720                         if (defined $refs{$1}) {
721                                 push @{$refs{$1}}, $2;
722                         } else {
723                                 $refs{$1} = [ $2 ];
724                         }
725                 }
726         }
727         close $fd or return;
728         return \%refs;
731 ## ----------------------------------------------------------------------
732 ## parse to hash functions
734 sub parse_date {
735         my $epoch = shift;
736         my $tz = shift || "-0000";
738         my %date;
739         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
740         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
741         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
742         $date{'hour'} = $hour;
743         $date{'minute'} = $min;
744         $date{'mday'} = $mday;
745         $date{'day'} = $days[$wday];
746         $date{'month'} = $months[$mon];
747         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
748                            $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
749         $date{'mday-time'} = sprintf "%d %s %02d:%02d",
750                              $mday, $months[$mon], $hour ,$min;
752         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
753         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
754         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
755         $date{'hour_local'} = $hour;
756         $date{'minute_local'} = $min;
757         $date{'tz_local'} = $tz;
758         return %date;
761 sub parse_tag {
762         my $tag_id = shift;
763         my %tag;
764         my @comment;
766         open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
767         $tag{'id'} = $tag_id;
768         while (my $line = <$fd>) {
769                 chomp $line;
770                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
771                         $tag{'object'} = $1;
772                 } elsif ($line =~ m/^type (.+)$/) {
773                         $tag{'type'} = $1;
774                 } elsif ($line =~ m/^tag (.+)$/) {
775                         $tag{'name'} = $1;
776                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
777                         $tag{'author'} = $1;
778                         $tag{'epoch'} = $2;
779                         $tag{'tz'} = $3;
780                 } elsif ($line =~ m/--BEGIN/) {
781                         push @comment, $line;
782                         last;
783                 } elsif ($line eq "") {
784                         last;
785                 }
786         }
787         push @comment, <$fd>;
788         $tag{'comment'} = \@comment;
789         close $fd or return;
790         if (!defined $tag{'name'}) {
791                 return
792         };
793         return %tag
796 sub parse_commit {
797         my $commit_id = shift;
798         my $commit_text = shift;
800         my @commit_lines;
801         my %co;
803         if (defined $commit_text) {
804                 @commit_lines = @$commit_text;
805         } else {
806                 $/ = "\0";
807                 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
808                         or return;
809                 @commit_lines = split '\n', <$fd>;
810                 close $fd or return;
811                 $/ = "\n";
812                 pop @commit_lines;
813         }
814         my $header = shift @commit_lines;
815         if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
816                 return;
817         }
818         ($co{'id'}, my @parents) = split ' ', $header;
819         $co{'parents'} = \@parents;
820         $co{'parent'} = $parents[0];
821         while (my $line = shift @commit_lines) {
822                 last if $line eq "\n";
823                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
824                         $co{'tree'} = $1;
825                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
826                         $co{'author'} = $1;
827                         $co{'author_epoch'} = $2;
828                         $co{'author_tz'} = $3;
829                         if ($co{'author'} =~ m/^([^<]+) </) {
830                                 $co{'author_name'} = $1;
831                         } else {
832                                 $co{'author_name'} = $co{'author'};
833                         }
834                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
835                         $co{'committer'} = $1;
836                         $co{'committer_epoch'} = $2;
837                         $co{'committer_tz'} = $3;
838                         $co{'committer_name'} = $co{'committer'};
839                         $co{'committer_name'} =~ s/ <.*//;
840                 }
841         }
842         if (!defined $co{'tree'}) {
843                 return;
844         };
846         foreach my $title (@commit_lines) {
847                 $title =~ s/^    //;
848                 if ($title ne "") {
849                         $co{'title'} = chop_str($title, 80, 5);
850                         # remove leading stuff of merges to make the interesting part visible
851                         if (length($title) > 50) {
852                                 $title =~ s/^Automatic //;
853                                 $title =~ s/^merge (of|with) /Merge ... /i;
854                                 if (length($title) > 50) {
855                                         $title =~ s/(http|rsync):\/\///;
856                                 }
857                                 if (length($title) > 50) {
858                                         $title =~ s/(master|www|rsync)\.//;
859                                 }
860                                 if (length($title) > 50) {
861                                         $title =~ s/kernel.org:?//;
862                                 }
863                                 if (length($title) > 50) {
864                                         $title =~ s/\/pub\/scm//;
865                                 }
866                         }
867                         $co{'title_short'} = chop_str($title, 50, 5);
868                         last;
869                 }
870         }
871         # remove added spaces
872         foreach my $line (@commit_lines) {
873                 $line =~ s/^    //;
874         }
875         $co{'comment'} = \@commit_lines;
877         my $age = time - $co{'committer_epoch'};
878         $co{'age'} = $age;
879         $co{'age_string'} = age_string($age);
880         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
881         if ($age > 60*60*24*7*2) {
882                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
883                 $co{'age_string_age'} = $co{'age_string'};
884         } else {
885                 $co{'age_string_date'} = $co{'age_string'};
886                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
887         }
888         return %co;
891 # parse ref from ref_file, given by ref_id, with given type
892 sub parse_ref {
893         my $ref_file = shift;
894         my $ref_id = shift;
895         my $type = shift || git_get_type($ref_id);
896         my %ref_item;
898         $ref_item{'type'} = $type;
899         $ref_item{'id'} = $ref_id;
900         $ref_item{'epoch'} = 0;
901         $ref_item{'age'} = "unknown";
902         if ($type eq "tag") {
903                 my %tag = parse_tag($ref_id);
904                 $ref_item{'comment'} = $tag{'comment'};
905                 if ($tag{'type'} eq "commit") {
906                         my %co = parse_commit($tag{'object'});
907                         $ref_item{'epoch'} = $co{'committer_epoch'};
908                         $ref_item{'age'} = $co{'age_string'};
909                 } elsif (defined($tag{'epoch'})) {
910                         my $age = time - $tag{'epoch'};
911                         $ref_item{'epoch'} = $tag{'epoch'};
912                         $ref_item{'age'} = age_string($age);
913                 }
914                 $ref_item{'reftype'} = $tag{'type'};
915                 $ref_item{'name'} = $tag{'name'};
916                 $ref_item{'refid'} = $tag{'object'};
917         } elsif ($type eq "commit"){
918                 my %co = parse_commit($ref_id);
919                 $ref_item{'reftype'} = "commit";
920                 $ref_item{'name'} = $ref_file;
921                 $ref_item{'title'} = $co{'title'};
922                 $ref_item{'refid'} = $ref_id;
923                 $ref_item{'epoch'} = $co{'committer_epoch'};
924                 $ref_item{'age'} = $co{'age_string'};
925         } else {
926                 $ref_item{'reftype'} = $type;
927                 $ref_item{'name'} = $ref_file;
928                 $ref_item{'refid'} = $ref_id;
929         }
931         return %ref_item;
934 # parse line of git-diff-tree "raw" output
935 sub parse_difftree_raw_line {
936         my $line = shift;
937         my %res;
939         # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
940         # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
941         if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
942                 $res{'from_mode'} = $1;
943                 $res{'to_mode'} = $2;
944                 $res{'from_id'} = $3;
945                 $res{'to_id'} = $4;
946                 $res{'status'} = $5;
947                 $res{'similarity'} = $6;
948                 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
949                         ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
950                 } else {
951                         $res{'file'} = unquote($7);
952                 }
953         }
954         # 'c512b523472485aef4fff9e57b229d9d243c967f'
955         #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
956         #       $res{'commit'} = $1;
957         #}
959         return wantarray ? %res : \%res;
962 ## ......................................................................
963 ## parse to array of hashes functions
965 sub git_get_refs_list {
966         my $ref_dir = shift;
967         my @reflist;
969         my @refs;
970         my $pfxlen = length("$projectroot/$project/$ref_dir");
971         File::Find::find(sub {
972                 return if (/^\./);
973                 if (-f $_) {
974                         push @refs, substr($File::Find::name, $pfxlen + 1);
975                 }
976         }, "$projectroot/$project/$ref_dir");
978         foreach my $ref_file (@refs) {
979                 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
980                 my $type = git_get_type($ref_id) || next;
981                 my %ref_item = parse_ref($ref_file, $ref_id, $type);
983                 push @reflist, \%ref_item;
984         }
985         # sort refs by age
986         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
987         return \@reflist;
990 ## ----------------------------------------------------------------------
991 ## filesystem-related functions
993 sub get_file_owner {
994         my $path = shift;
996         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
997         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
998         if (!defined $gcos) {
999                 return undef;
1000         }
1001         my $owner = $gcos;
1002         $owner =~ s/[,;].*$//;
1003         return decode("utf8", $owner, Encode::FB_DEFAULT);
1006 ## ......................................................................
1007 ## mimetype related functions
1009 sub mimetype_guess_file {
1010         my $filename = shift;
1011         my $mimemap = shift;
1012         -r $mimemap or return undef;
1014         my %mimemap;
1015         open(MIME, $mimemap) or return undef;
1016         while (<MIME>) {
1017                 next if m/^#/; # skip comments
1018                 my ($mime, $exts) = split(/\t+/);
1019                 if (defined $exts) {
1020                         my @exts = split(/\s+/, $exts);
1021                         foreach my $ext (@exts) {
1022                                 $mimemap{$ext} = $mime;
1023                         }
1024                 }
1025         }
1026         close(MIME);
1028         $filename =~ /\.(.*?)$/;
1029         return $mimemap{$1};
1032 sub mimetype_guess {
1033         my $filename = shift;
1034         my $mime;
1035         $filename =~ /\./ or return undef;
1037         if ($mimetypes_file) {
1038                 my $file = $mimetypes_file;
1039                 if ($file !~ m!^/!) { # if it is relative path
1040                         # it is relative to project
1041                         $file = "$projectroot/$project/$file";
1042                 }
1043                 $mime = mimetype_guess_file($filename, $file);
1044         }
1045         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1046         return $mime;
1049 sub blob_mimetype {
1050         my $fd = shift;
1051         my $filename = shift;
1053         if ($filename) {
1054                 my $mime = mimetype_guess($filename);
1055                 $mime and return $mime;
1056         }
1058         # just in case
1059         return $default_blob_plain_mimetype unless $fd;
1061         if (-T $fd) {
1062                 return 'text/plain' .
1063                        ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1064         } elsif (! $filename) {
1065                 return 'application/octet-stream';
1066         } elsif ($filename =~ m/\.png$/i) {
1067                 return 'image/png';
1068         } elsif ($filename =~ m/\.gif$/i) {
1069                 return 'image/gif';
1070         } elsif ($filename =~ m/\.jpe?g$/i) {
1071                 return 'image/jpeg';
1072         } else {
1073                 return 'application/octet-stream';
1074         }
1077 ## ======================================================================
1078 ## functions printing HTML: header, footer, error page
1080 sub git_header_html {
1081         my $status = shift || "200 OK";
1082         my $expires = shift;
1084         my $title = "$site_name git";
1085         if (defined $project) {
1086                 $title .= " - $project";
1087                 if (defined $action) {
1088                         $title .= "/$action";
1089                         if (defined $file_name) {
1090                                 $title .= " - $file_name";
1091                                 if ($action eq "tree" && $file_name !~ m|/$|) {
1092                                         $title .= "/";
1093                                 }
1094                         }
1095                 }
1096         }
1097         my $content_type;
1098         # require explicit support from the UA if we are to send the page as
1099         # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1100         # we have to do this because MSIE sometimes globs '*/*', pretending to
1101         # support xhtml+xml but choking when it gets what it asked for.
1102         if (defined $cgi->http('HTTP_ACCEPT') &&
1103             $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1104             $cgi->Accept('application/xhtml+xml') != 0) {
1105                 $content_type = 'application/xhtml+xml';
1106         } else {
1107                 $content_type = 'text/html';
1108         }
1109         print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1110                            -status=> $status, -expires => $expires);
1111         print <<EOF;
1112 <?xml version="1.0" encoding="utf-8"?>
1113 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1114 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1115 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1116 <!-- git core binaries version $git_version -->
1117 <head>
1118 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1119 <meta name="generator" content="gitweb/$version git/$git_version"/>
1120 <meta name="robots" content="index, nofollow"/>
1121 <title>$title</title>
1122 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1123 EOF
1124         if (defined $project) {
1125                 printf('<link rel="alternate" title="%s log" '.
1126                        'href="%s" type="application/rss+xml"/>'."\n",
1127                        esc_param($project), href(action=>"rss"));
1128         }
1130         print "</head>\n" .
1131               "<body>\n" .
1132               "<div class=\"page_header\">\n" .
1133               "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1134               "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1135               "</a>\n";
1136         print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1137         if (defined $project) {
1138                 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1139                 if (defined $action) {
1140                         print " / $action";
1141                 }
1142                 print "\n";
1143                 if (!defined $searchtext) {
1144                         $searchtext = "";
1145                 }
1146                 my $search_hash;
1147                 if (defined $hash_base) {
1148                         $search_hash = $hash_base;
1149                 } elsif (defined $hash) {
1150                         $search_hash = $hash;
1151                 } else {
1152                         $search_hash = "HEAD";
1153                 }
1154                 $cgi->param("a", "search");
1155                 $cgi->param("h", $search_hash);
1156                 print $cgi->startform(-method => "get", -action => $my_uri) .
1157                       "<div class=\"search\">\n" .
1158                       $cgi->hidden(-name => "p") . "\n" .
1159                       $cgi->hidden(-name => "a") . "\n" .
1160                       $cgi->hidden(-name => "h") . "\n" .
1161                       $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1162                       "</div>" .
1163                       $cgi->end_form() . "\n";
1164         }
1165         print "</div>\n";
1168 sub git_footer_html {
1169         print "<div class=\"page_footer\">\n";
1170         if (defined $project) {
1171                 my $descr = git_get_project_description($project);
1172                 if (defined $descr) {
1173                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1174                 }
1175                 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1176         } else {
1177                 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1178         }
1179         print "</div>\n" .
1180               "</body>\n" .
1181               "</html>";
1184 sub die_error {
1185         my $status = shift || "403 Forbidden";
1186         my $error = shift || "Malformed query, file missing or permission denied";
1188         git_header_html($status);
1189         print "<div class=\"page_body\">\n" .
1190               "<br/><br/>\n" .
1191               "$status - $error\n" .
1192               "<br/>\n" .
1193               "</div>\n";
1194         git_footer_html();
1195         exit;
1198 ## ----------------------------------------------------------------------
1199 ## functions printing or outputting HTML: navigation
1201 sub git_print_page_nav {
1202         my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1203         $extra = '' if !defined $extra; # pager or formats
1205         my @navs = qw(summary shortlog log commit commitdiff tree);
1206         if ($suppress) {
1207                 @navs = grep { $_ ne $suppress } @navs;
1208         }
1210         my %arg = map { $_ => {action=>$_} } @navs;
1211         if (defined $head) {
1212                 for (qw(commit commitdiff)) {
1213                         $arg{$_}{hash} = $head;
1214                 }
1215                 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1216                         for (qw(shortlog log)) {
1217                                 $arg{$_}{hash} = $head;
1218                         }
1219                 }
1220         }
1221         $arg{tree}{hash} = $treehead if defined $treehead;
1222         $arg{tree}{hash_base} = $treebase if defined $treebase;
1224         print "<div class=\"page_nav\">\n" .
1225                 (join " | ",
1226                  map { $_ eq $current ?
1227                        $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1228                  } @navs);
1229         print "<br/>\n$extra<br/>\n" .
1230               "</div>\n";
1233 sub format_paging_nav {
1234         my ($action, $hash, $head, $page, $nrevs) = @_;
1235         my $paging_nav;
1238         if ($hash ne $head || $page) {
1239                 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1240         } else {
1241                 $paging_nav .= "HEAD";
1242         }
1244         if ($page > 0) {
1245                 $paging_nav .= " &sdot; " .
1246                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1247                                  -accesskey => "p", -title => "Alt-p"}, "prev");
1248         } else {
1249                 $paging_nav .= " &sdot; prev";
1250         }
1252         if ($nrevs >= (100 * ($page+1)-1)) {
1253                 $paging_nav .= " &sdot; " .
1254                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1255                                  -accesskey => "n", -title => "Alt-n"}, "next");
1256         } else {
1257                 $paging_nav .= " &sdot; next";
1258         }
1260         return $paging_nav;
1263 ## ......................................................................
1264 ## functions printing or outputting HTML: div
1266 sub git_print_header_div {
1267         my ($action, $title, $hash, $hash_base) = @_;
1268         my %args = ();
1270         $args{action} = $action;
1271         $args{hash} = $hash if $hash;
1272         $args{hash_base} = $hash_base if $hash_base;
1274         print "<div class=\"header\">\n" .
1275               $cgi->a({-href => href(%args), -class => "title"},
1276               $title ? $title : $action) .
1277               "\n</div>\n";
1280 sub git_print_page_path {
1281         my $name = shift;
1282         my $type = shift;
1283         my $hb = shift;
1285         if (!defined $name) {
1286                 print "<div class=\"page_path\">/</div>\n";
1287         } elsif (defined $type && $type eq 'blob') {
1288                 print "<div class=\"page_path\">";
1289                 if (defined $hb) {
1290                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1291                                                      hash_base=>$hb)},
1292                                       esc_html($name));
1293                 } else {
1294                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1295                                       esc_html($name));
1296                 }
1297                 print "<br/></div>\n";
1298         } else {
1299                 print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1300         }
1303 sub git_print_log {
1304         my $log = shift;
1306         # remove leading empty lines
1307         while (defined $log->[0] && $log->[0] eq "") {
1308                 shift @$log;
1309         }
1311         # print log
1312         my $signoff = 0;
1313         my $empty = 0;
1314         foreach my $line (@$log) {
1315                 # print only one empty line
1316                 # do not print empty line after signoff
1317                 if ($line eq "") {
1318                         next if ($empty || $signoff);
1319                         $empty = 1;
1320                 } else {
1321                         $empty = 0;
1322                 }
1323                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1324                         $signoff = 1;
1325                         print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1326                 } else {
1327                         $signoff = 0;
1328                         print format_log_line_html($line) . "<br/>\n";
1329                 }
1330         }
1333 sub git_print_simplified_log {
1334         my $log = shift;
1335         my $remove_title = shift;
1337         shift @$log if $remove_title;
1338         # remove leading empty lines
1339         while (defined $log->[0] && $log->[0] eq "") {
1340                 shift @$log;
1341         }
1343         # simplify and print log
1344         my $empty = 0;
1345         foreach my $line (@$log) {
1346                 # remove signoff lines
1347                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1348                         next;
1349                 }
1350                 # print only one empty line
1351                 if ($line eq "") {
1352                         next if $empty;
1353                         $empty = 1;
1354                 } else {
1355                         $empty = 0;
1356                 }
1357                 print format_log_line_html($line) . "<br/>\n";
1358         }
1359         # end with single empty line
1360         print "<br/>\n" unless $empty;
1363 ## ......................................................................
1364 ## functions printing large fragments of HTML
1366 sub git_difftree_body {
1367         my ($difftree, $parent) = @_;
1369         print "<div class=\"list_head\">\n";
1370         if ($#{$difftree} > 10) {
1371                 print(($#{$difftree} + 1) . " files changed:\n");
1372         }
1373         print "</div>\n";
1375         print "<table class=\"diff_tree\">\n";
1376         my $alternate = 0;
1377         foreach my $line (@{$difftree}) {
1378                 my %diff = parse_difftree_raw_line($line);
1380                 if ($alternate) {
1381                         print "<tr class=\"dark\">\n";
1382                 } else {
1383                         print "<tr class=\"light\">\n";
1384                 }
1385                 $alternate ^= 1;
1387                 my ($to_mode_oct, $to_mode_str, $to_file_type);
1388                 my ($from_mode_oct, $from_mode_str, $from_file_type);
1389                 if ($diff{'to_mode'} ne ('0' x 6)) {
1390                         $to_mode_oct = oct $diff{'to_mode'};
1391                         if (S_ISREG($to_mode_oct)) { # only for regular file
1392                                 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1393                         }
1394                         $to_file_type = file_type($diff{'to_mode'});
1395                 }
1396                 if ($diff{'from_mode'} ne ('0' x 6)) {
1397                         $from_mode_oct = oct $diff{'from_mode'};
1398                         if (S_ISREG($to_mode_oct)) { # only for regular file
1399                                 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1400                         }
1401                         $from_file_type = file_type($diff{'from_mode'});
1402                 }
1404                 if ($diff{'status'} eq "A") { # created
1405                         my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1406                         $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
1407                         $mode_chng   .= "]</span>";
1408                         print "<td>" .
1409                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1410                                                      hash_base=>$hash, file_name=>$diff{'file'}),
1411                                       -class => "list"}, esc_html($diff{'file'})) .
1412                               "</td>\n" .
1413                               "<td>$mode_chng</td>\n" .
1414                               "<td class=\"link\">" .
1415                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1416                                                      hash_base=>$hash, file_name=>$diff{'file'})},
1417                                       "blob") .
1418                               "</td>\n";
1420                 } elsif ($diff{'status'} eq "D") { # deleted
1421                         my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1422                         print "<td>" .
1423                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1424                                                      hash_base=>$parent, file_name=>$diff{'file'}),
1425                                        -class => "list"}, esc_html($diff{'file'})) .
1426                               "</td>\n" .
1427                               "<td>$mode_chng</td>\n" .
1428                               "<td class=\"link\">" .
1429                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1430                                                      hash_base=>$parent, file_name=>$diff{'file'})},
1431                                       "blob") .
1432                               " | " .
1433                               $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1434                                                      file_name=>$diff{'file'})},\
1435                                       "history") .
1436                               "</td>\n";
1438                 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1439                         my $mode_chnge = "";
1440                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1441                                 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1442                                 if ($from_file_type != $to_file_type) {
1443                                         $mode_chnge .= " from $from_file_type to $to_file_type";
1444                                 }
1445                                 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1446                                         if ($from_mode_str && $to_mode_str) {
1447                                                 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1448                                         } elsif ($to_mode_str) {
1449                                                 $mode_chnge .= " mode: $to_mode_str";
1450                                         }
1451                                 }
1452                                 $mode_chnge .= "]</span>\n";
1453                         }
1454                         print "<td>";
1455                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1456                                 print $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1457                                                              hash_base=>$hash, file_name=>$diff{'file'}),
1458                                               -class => "list"}, esc_html($diff{'file'}));
1459                         } else { # only mode changed
1460                                 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1461                                                              hash_base=>$hash, file_name=>$diff{'file'}),
1462                                               -class => "list"}, esc_html($diff{'file'}));
1463                         }
1464                         print "</td>\n" .
1465                               "<td>$mode_chnge</td>\n" .
1466                               "<td class=\"link\">" .
1467                                 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1468                                                        hash_base=>$hash, file_name=>$diff{'file'})},
1469                                         "blob");
1470                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1471                                 print " | " .
1472                                         $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1473                                                                hash_base=>$hash, file_name=>$diff{'file'})},
1474                                                 "diff");
1475                         }
1476                         print " | " .
1477                                 $cgi->a({-href => href(action=>"history",
1478                                                        hash_base=>$hash, file_name=>$diff{'file'})},
1479                                         "history");
1480                         print "</td>\n";
1482                 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1483                         my %status_name = ('R' => 'moved', 'C' => 'copied');
1484                         my $nstatus = $status_name{$diff{'status'}};
1485                         my $mode_chng = "";
1486                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1487                                 # mode also for directories, so we cannot use $to_mode_str
1488                                 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1489                         }
1490                         print "<td>" .
1491                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1492                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1493                                       -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1494                               "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1495                               $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1496                                                      hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1497                                       -class => "list"}, esc_html($diff{'from_file'})) .
1498                               " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1499                               "<td class=\"link\">" .
1500                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1501                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1502                                       "blob");
1503                         if ($diff{'to_id'} ne $diff{'from_id'}) {
1504                                 print " | " .
1505                                         $cgi->a({-href => href(action=>"blobdiff", hash_base=>$hash,
1506                                                                hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1507                                                                file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1508                                                 "diff");
1509                         }
1510                         print "</td>\n";
1512                 } # we should not encounter Unmerged (U) or Unknown (X) status
1513                 print "</tr>\n";
1514         }
1515         print "</table>\n";
1518 sub git_shortlog_body {
1519         # uses global variable $project
1520         my ($revlist, $from, $to, $refs, $extra) = @_;
1522         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1523         my $have_snapshot = (defined $ctype && defined $suffix);
1525         $from = 0 unless defined $from;
1526         $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1528         print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1529         my $alternate = 0;
1530         for (my $i = $from; $i <= $to; $i++) {
1531                 my $commit = $revlist->[$i];
1532                 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1533                 my $ref = format_ref_marker($refs, $commit);
1534                 my %co = parse_commit($commit);
1535                 if ($alternate) {
1536                         print "<tr class=\"dark\">\n";
1537                 } else {
1538                         print "<tr class=\"light\">\n";
1539                 }
1540                 $alternate ^= 1;
1541                 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1542                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1543                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1544                       "<td>";
1545                 print format_subject_html($co{'title'}, $co{'title_short'},
1546                                           href(action=>"commit", hash=>$commit), $ref);
1547                 print "</td>\n" .
1548                       "<td class=\"link\">" .
1549                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1550                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1551                 if ($have_snapshot) {
1552                         print " | " .  $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1553                 }
1554                 print "</td>\n" .
1555                       "</tr>\n";
1556         }
1557         if (defined $extra) {
1558                 print "<tr>\n" .
1559                       "<td colspan=\"4\">$extra</td>\n" .
1560                       "</tr>\n";
1561         }
1562         print "</table>\n";
1565 sub git_history_body {
1566         # Warning: assumes constant type (blob or tree) during history
1567         my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1569         print "<table class=\"history\" cellspacing=\"0\">\n";
1570         my $alternate = 0;
1571         while (my $line = <$fd>) {
1572                 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1573                         next;
1574                 }
1576                 my $commit = $1;
1577                 my %co = parse_commit($commit);
1578                 if (!%co) {
1579                         next;
1580                 }
1582                 my $ref = format_ref_marker($refs, $commit);
1584                 if ($alternate) {
1585                         print "<tr class=\"dark\">\n";
1586                 } else {
1587                         print "<tr class=\"light\">\n";
1588                 }
1589                 $alternate ^= 1;
1590                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1591                       # shortlog uses      chop_str($co{'author_name'}, 10)
1592                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1593                       "<td>";
1594                 # originally git_history used chop_str($co{'title'}, 50)
1595                 print format_subject_html($co{'title'}, $co{'title_short'},
1596                                           href(action=>"commit", hash=>$commit), $ref);
1597                 print "</td>\n" .
1598                       "<td class=\"link\">" .
1599                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1600                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1601                       $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1603                 if ($ftype eq 'blob') {
1604                         my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1605                         my $blob_parent  = git_get_hash_by_path($commit, $file_name);
1606                         if (defined $blob_current && defined $blob_parent &&
1607                                         $blob_current ne $blob_parent) {
1608                                 print " | " .
1609                                         $cgi->a({-href => href(action=>"blobdiff", hash=>$blob_current, hash_parent=>$blob_parent,
1610                                                                hash_base=>$commit, file_name=>$file_name)},
1611                                                 "diff to current");
1612                         }
1613                 }
1614                 print "</td>\n" .
1615                       "</tr>\n";
1616         }
1617         if (defined $extra) {
1618                 print "<tr>\n" .
1619                       "<td colspan=\"4\">$extra</td>\n" .
1620                       "</tr>\n";
1621         }
1622         print "</table>\n";
1625 sub git_tags_body {
1626         # uses global variable $project
1627         my ($taglist, $from, $to, $extra) = @_;
1628         $from = 0 unless defined $from;
1629         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1631         print "<table class=\"tags\" cellspacing=\"0\">\n";
1632         my $alternate = 0;
1633         for (my $i = $from; $i <= $to; $i++) {
1634                 my $entry = $taglist->[$i];
1635                 my %tag = %$entry;
1636                 my $comment_lines = $tag{'comment'};
1637                 my $comment = shift @$comment_lines;
1638                 my $comment_short;
1639                 if (defined $comment) {
1640                         $comment_short = chop_str($comment, 30, 5);
1641                 }
1642                 if ($alternate) {
1643                         print "<tr class=\"dark\">\n";
1644                 } else {
1645                         print "<tr class=\"light\">\n";
1646                 }
1647                 $alternate ^= 1;
1648                 print "<td><i>$tag{'age'}</i></td>\n" .
1649                       "<td>" .
1650                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1651                                -class => "list name"}, esc_html($tag{'name'})) .
1652                       "</td>\n" .
1653                       "<td>";
1654                 if (defined $comment) {
1655                         print format_subject_html($comment, $comment_short,
1656                                                   href(action=>"tag", hash=>$tag{'id'}));
1657                 }
1658                 print "</td>\n" .
1659                       "<td class=\"selflink\">";
1660                 if ($tag{'type'} eq "tag") {
1661                         print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1662                 } else {
1663                         print "&nbsp;";
1664                 }
1665                 print "</td>\n" .
1666                       "<td class=\"link\">" . " | " .
1667                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1668                 if ($tag{'reftype'} eq "commit") {
1669                         print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1670                               " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1671                 } elsif ($tag{'reftype'} eq "blob") {
1672                         print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1673                 }
1674                 print "</td>\n" .
1675                       "</tr>";
1676         }
1677         if (defined $extra) {
1678                 print "<tr>\n" .
1679                       "<td colspan=\"5\">$extra</td>\n" .
1680                       "</tr>\n";
1681         }
1682         print "</table>\n";
1685 sub git_heads_body {
1686         # uses global variable $project
1687         my ($taglist, $head, $from, $to, $extra) = @_;
1688         $from = 0 unless defined $from;
1689         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1691         print "<table class=\"heads\" cellspacing=\"0\">\n";
1692         my $alternate = 0;
1693         for (my $i = $from; $i <= $to; $i++) {
1694                 my $entry = $taglist->[$i];
1695                 my %tag = %$entry;
1696                 my $curr = $tag{'id'} eq $head;
1697                 if ($alternate) {
1698                         print "<tr class=\"dark\">\n";
1699                 } else {
1700                         print "<tr class=\"light\">\n";
1701                 }
1702                 $alternate ^= 1;
1703                 print "<td><i>$tag{'age'}</i></td>\n" .
1704                       ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1705                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1706                                -class => "list name"},esc_html($tag{'name'})) .
1707                       "</td>\n" .
1708                       "<td class=\"link\">" .
1709                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1710                       $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1711                       "</td>\n" .
1712                       "</tr>";
1713         }
1714         if (defined $extra) {
1715                 print "<tr>\n" .
1716                       "<td colspan=\"3\">$extra</td>\n" .
1717                       "</tr>\n";
1718         }
1719         print "</table>\n";
1722 ## ----------------------------------------------------------------------
1723 ## functions printing large fragments, format as one of arguments
1725 sub git_diff_print {
1726         my $from = shift;
1727         my $from_name = shift;
1728         my $to = shift;
1729         my $to_name = shift;
1730         my $format = shift || "html";
1732         my $from_tmp = "/dev/null";
1733         my $to_tmp = "/dev/null";
1734         my $pid = $$;
1736         # create tmp from-file
1737         if (defined $from) {
1738                 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1739                 open my $fd2, "> $from_tmp";
1740                 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1741                 my @file = <$fd>;
1742                 print $fd2 @file;
1743                 close $fd2;
1744                 close $fd;
1745         }
1747         # create tmp to-file
1748         if (defined $to) {
1749                 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1750                 open my $fd2, "> $to_tmp";
1751                 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1752                 my @file = <$fd>;
1753                 print $fd2 @file;
1754                 close $fd2;
1755                 close $fd;
1756         }
1758         open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1759         if ($format eq "plain") {
1760                 undef $/;
1761                 print <$fd>;
1762                 $/ = "\n";
1763         } else {
1764                 while (my $line = <$fd>) {
1765                         chomp $line;
1766                         my $char = substr($line, 0, 1);
1767                         my $diff_class = "";
1768                         if ($char eq '+') {
1769                                 $diff_class = " add";
1770                         } elsif ($char eq "-") {
1771                                 $diff_class = " rem";
1772                         } elsif ($char eq "@") {
1773                                 $diff_class = " chunk_header";
1774                         } elsif ($char eq "\\") {
1775                                 # skip errors
1776                                 next;
1777                         }
1778                         $line = untabify($line);
1779                         print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1780                 }
1781         }
1782         close $fd;
1784         if (defined $from) {
1785                 unlink($from_tmp);
1786         }
1787         if (defined $to) {
1788                 unlink($to_tmp);
1789         }
1793 ## ======================================================================
1794 ## ======================================================================
1795 ## actions
1797 sub git_project_list {
1798         my $order = $cgi->param('o');
1799         if (defined $order && $order !~ m/project|descr|owner|age/) {
1800                 die_error(undef, "Unknown order parameter");
1801         }
1803         my @list = git_get_projects_list();
1804         my @projects;
1805         if (!@list) {
1806                 die_error(undef, "No projects found");
1807         }
1808         foreach my $pr (@list) {
1809                 my $head = git_get_head_hash($pr->{'path'});
1810                 if (!defined $head) {
1811                         next;
1812                 }
1813                 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1814                 my %co = parse_commit($head);
1815                 if (!%co) {
1816                         next;
1817                 }
1818                 $pr->{'commit'} = \%co;
1819                 if (!defined $pr->{'descr'}) {
1820                         my $descr = git_get_project_description($pr->{'path'}) || "";
1821                         $pr->{'descr'} = chop_str($descr, 25, 5);
1822                 }
1823                 if (!defined $pr->{'owner'}) {
1824                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1825                 }
1826                 push @projects, $pr;
1827         }
1829         git_header_html();
1830         if (-f $home_text) {
1831                 print "<div class=\"index_include\">\n";
1832                 open (my $fd, $home_text);
1833                 print <$fd>;
1834                 close $fd;
1835                 print "</div>\n";
1836         }
1837         print "<table class=\"project_list\">\n" .
1838               "<tr>\n";
1839         $order ||= "project";
1840         if ($order eq "project") {
1841                 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1842                 print "<th>Project</th>\n";
1843         } else {
1844                 print "<th>" .
1845                       $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
1846                                -class => "header"}, "Project") .
1847                       "</th>\n";
1848         }
1849         if ($order eq "descr") {
1850                 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1851                 print "<th>Description</th>\n";
1852         } else {
1853                 print "<th>" .
1854                       $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
1855                                -class => "header"}, "Description") .
1856                       "</th>\n";
1857         }
1858         if ($order eq "owner") {
1859                 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1860                 print "<th>Owner</th>\n";
1861         } else {
1862                 print "<th>" .
1863                       $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
1864                                -class => "header"}, "Owner") .
1865                       "</th>\n";
1866         }
1867         if ($order eq "age") {
1868                 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
1869                 print "<th>Last Change</th>\n";
1870         } else {
1871                 print "<th>" .
1872                       $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
1873                                -class => "header"}, "Last Change") .
1874                       "</th>\n";
1875         }
1876         print "<th></th>\n" .
1877               "</tr>\n";
1878         my $alternate = 0;
1879         foreach my $pr (@projects) {
1880                 if ($alternate) {
1881                         print "<tr class=\"dark\">\n";
1882                 } else {
1883                         print "<tr class=\"light\">\n";
1884                 }
1885                 $alternate ^= 1;
1886                 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
1887                                         -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
1888                       "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
1889                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
1890                 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
1891                       $pr->{'commit'}{'age_string'} . "</td>\n" .
1892                       "<td class=\"link\">" .
1893                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
1894                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
1895                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
1896                       "</td>\n" .
1897                       "</tr>\n";
1898         }
1899         print "</table>\n";
1900         git_footer_html();
1903 sub git_summary {
1904         my $descr = git_get_project_description($project) || "none";
1905         my $head = git_get_head_hash($project);
1906         my %co = parse_commit($head);
1907         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
1909         my $owner = git_get_project_owner($project);
1911         my $refs = git_get_references();
1912         git_header_html();
1913         git_print_page_nav('summary','', $head);
1915         print "<div class=\"title\">&nbsp;</div>\n";
1916         print "<table cellspacing=\"0\">\n" .
1917               "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1918               "<tr><td>owner</td><td>$owner</td></tr>\n" .
1919               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
1920         # use per project git URL list in $projectroot/$project/cloneurl
1921         # or make project git URL from git base URL and project name
1922         my $url_tag = "URL";
1923         my @url_list = git_get_project_url_list($project);
1924         @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
1925         foreach my $git_url (@url_list) {
1926                 next unless $git_url;
1927                 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
1928                 $url_tag = "";
1929         }
1930         print "</table>\n";
1932         open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
1933                 or die_error(undef, "Open git-rev-list failed");
1934         my @revlist = map { chomp; $_ } <$fd>;
1935         close $fd;
1936         git_print_header_div('shortlog');
1937         git_shortlog_body(\@revlist, 0, 15, $refs,
1938                           $cgi->a({-href => href(action=>"shortlog")}, "..."));
1940         my $taglist = git_get_refs_list("refs/tags");
1941         if (defined @$taglist) {
1942                 git_print_header_div('tags');
1943                 git_tags_body($taglist, 0, 15,
1944                               $cgi->a({-href => href(action=>"tags")}, "..."));
1945         }
1947         my $headlist = git_get_refs_list("refs/heads");
1948         if (defined @$headlist) {
1949                 git_print_header_div('heads');
1950                 git_heads_body($headlist, $head, 0, 15,
1951                                $cgi->a({-href => href(action=>"heads")}, "..."));
1952         }
1954         git_footer_html();
1957 sub git_tag {
1958         my $head = git_get_head_hash($project);
1959         git_header_html();
1960         git_print_page_nav('','', $head,undef,$head);
1961         my %tag = parse_tag($hash);
1962         git_print_header_div('commit', esc_html($tag{'name'}), $hash);
1963         print "<div class=\"title_text\">\n" .
1964               "<table cellspacing=\"0\">\n" .
1965               "<tr>\n" .
1966               "<td>object</td>\n" .
1967               "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
1968                                $tag{'object'}) . "</td>\n" .
1969               "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
1970                                               $tag{'type'}) . "</td>\n" .
1971               "</tr>\n";
1972         if (defined($tag{'author'})) {
1973                 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
1974                 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1975                 print "<tr><td></td><td>" . $ad{'rfc2822'} .
1976                         sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
1977                         "</td></tr>\n";
1978         }
1979         print "</table>\n\n" .
1980               "</div>\n";
1981         print "<div class=\"page_body\">";
1982         my $comment = $tag{'comment'};
1983         foreach my $line (@$comment) {
1984                 print esc_html($line) . "<br/>\n";
1985         }
1986         print "</div>\n";
1987         git_footer_html();
1990 sub git_blame2 {
1991         my $fd;
1992         my $ftype;
1994         if (!gitweb_check_feature('blame')) {
1995                 die_error('403 Permission denied', "Permission denied");
1996         }
1997         die_error('404 Not Found', "File name not defined") if (!$file_name);
1998         $hash_base ||= git_get_head_hash($project);
1999         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2000         my %co = parse_commit($hash_base)
2001                 or die_error(undef, "Reading commit failed");
2002         if (!defined $hash) {
2003                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2004                         or die_error(undef, "Error looking up file");
2005         }
2006         $ftype = git_get_type($hash);
2007         if ($ftype !~ "blob") {
2008                 die_error("400 Bad Request", "Object is not a blob");
2009         }
2010         open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2011                 or die_error(undef, "Open git-blame failed");
2012         git_header_html();
2013         my $formats_nav =
2014                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2015                         "blob") .
2016                 " | " .
2017                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2018                         "head");
2019         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2020         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2021         git_print_page_path($file_name, $ftype, $hash_base);
2022         my @rev_color = (qw(light2 dark2));
2023         my $num_colors = scalar(@rev_color);
2024         my $current_color = 0;
2025         my $last_rev;
2026         print "<div class=\"page_body\">\n";
2027         print "<table class=\"blame\">\n";
2028         print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
2029         while (<$fd>) {
2030                 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2031                 my $full_rev = $1;
2032                 my $rev = substr($full_rev, 0, 8);
2033                 my $lineno = $2;
2034                 my $data = $3;
2036                 if (!defined $last_rev) {
2037                         $last_rev = $full_rev;
2038                 } elsif ($last_rev ne $full_rev) {
2039                         $last_rev = $full_rev;
2040                         $current_color = ++$current_color % $num_colors;
2041                 }
2042                 print "<tr class=\"$rev_color[$current_color]\">\n";
2043                 print "<td class=\"sha1\">" .
2044                         $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2045                                 esc_html($rev)) . "</td>\n";
2046                 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2047                       esc_html($lineno) . "</a></td>\n";
2048                 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2049                 print "</tr>\n";
2050         }
2051         print "</table>\n";
2052         print "</div>";
2053         close $fd
2054                 or print "Reading blob failed\n";
2055         git_footer_html();
2058 sub git_blame {
2059         my $fd;
2061         if (!gitweb_check_feature('blame')) {
2062                 die_error('403 Permission denied', "Permission denied");
2063         }
2064         die_error('404 Not Found', "File name not defined") if (!$file_name);
2065         $hash_base ||= git_get_head_hash($project);
2066         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2067         my %co = parse_commit($hash_base)
2068                 or die_error(undef, "Reading commit failed");
2069         if (!defined $hash) {
2070                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2071                         or die_error(undef, "Error lookup file");
2072         }
2073         open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2074                 or die_error(undef, "Open git-annotate failed");
2075         git_header_html();
2076         my $formats_nav =
2077                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2078                         "blob") .
2079                 " | " .
2080                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2081                         "head");
2082         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2083         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2084         git_print_page_path($file_name, 'blob', $hash_base);
2085         print "<div class=\"page_body\">\n";
2086         print <<HTML;
2087 <table class="blame">
2088   <tr>
2089     <th>Commit</th>
2090     <th>Age</th>
2091     <th>Author</th>
2092     <th>Line</th>
2093     <th>Data</th>
2094   </tr>
2095 HTML
2096         my @line_class = (qw(light dark));
2097         my $line_class_len = scalar (@line_class);
2098         my $line_class_num = $#line_class;
2099         while (my $line = <$fd>) {
2100                 my $long_rev;
2101                 my $short_rev;
2102                 my $author;
2103                 my $time;
2104                 my $lineno;
2105                 my $data;
2106                 my $age;
2107                 my $age_str;
2108                 my $age_class;
2110                 chomp $line;
2111                 $line_class_num = ($line_class_num + 1) % $line_class_len;
2113                 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2114                         $long_rev = $1;
2115                         $author   = $2;
2116                         $time     = $3;
2117                         $lineno   = $4;
2118                         $data     = $5;
2119                 } else {
2120                         print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2121                         next;
2122                 }
2123                 $short_rev  = substr ($long_rev, 0, 8);
2124                 $age        = time () - $time;
2125                 $age_str    = age_string ($age);
2126                 $age_str    =~ s/ /&nbsp;/g;
2127                 $age_class  = age_class($age);
2128                 $author     = esc_html ($author);
2129                 $author     =~ s/ /&nbsp;/g;
2131                 $data = untabify($data);
2132                 $data = esc_html ($data);
2134                 print <<HTML;
2135   <tr class="$line_class[$line_class_num]">
2136     <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2137     <td class="$age_class">$age_str</td>
2138     <td>$author</td>
2139     <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2140     <td class="pre">$data</td>
2141   </tr>
2142 HTML
2143         } # while (my $line = <$fd>)
2144         print "</table>\n\n";
2145         close $fd
2146                 or print "Reading blob failed.\n";
2147         print "</div>";
2148         git_footer_html();
2151 sub git_tags {
2152         my $head = git_get_head_hash($project);
2153         git_header_html();
2154         git_print_page_nav('','', $head,undef,$head);
2155         git_print_header_div('summary', $project);
2157         my $taglist = git_get_refs_list("refs/tags");
2158         if (defined @$taglist) {
2159                 git_tags_body($taglist);
2160         }
2161         git_footer_html();
2164 sub git_heads {
2165         my $head = git_get_head_hash($project);
2166         git_header_html();
2167         git_print_page_nav('','', $head,undef,$head);
2168         git_print_header_div('summary', $project);
2170         my $taglist = git_get_refs_list("refs/heads");
2171         if (defined @$taglist) {
2172                 git_heads_body($taglist, $head);
2173         }
2174         git_footer_html();
2177 sub git_blob_plain {
2178         if (!defined $hash) {
2179                 if (defined $file_name) {
2180                         my $base = $hash_base || git_get_head_hash($project);
2181                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2182                                 or die_error(undef, "Error lookup file");
2183                 } else {
2184                         die_error(undef, "No file name defined");
2185                 }
2186         }
2187         my $type = shift;
2188         open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2189                 or die_error(undef, "Couldn't cat $file_name, $hash");
2191         $type ||= blob_mimetype($fd, $file_name);
2193         # save as filename, even when no $file_name is given
2194         my $save_as = "$hash";
2195         if (defined $file_name) {
2196                 $save_as = $file_name;
2197         } elsif ($type =~ m/^text\//) {
2198                 $save_as .= '.txt';
2199         }
2201         print $cgi->header(-type => "$type",
2202                            -content_disposition => "inline; filename=\"$save_as\"");
2203         undef $/;
2204         binmode STDOUT, ':raw';
2205         print <$fd>;
2206         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2207         $/ = "\n";
2208         close $fd;
2211 sub git_blob {
2212         if (!defined $hash) {
2213                 if (defined $file_name) {
2214                         my $base = $hash_base || git_get_head_hash($project);
2215                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2216                                 or die_error(undef, "Error lookup file");
2217                 } else {
2218                         die_error(undef, "No file name defined");
2219                 }
2220         }
2221         my $have_blame = gitweb_check_feature('blame');
2222         open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2223                 or die_error(undef, "Couldn't cat $file_name, $hash");
2224         my $mimetype = blob_mimetype($fd, $file_name);
2225         if ($mimetype !~ m/^text\//) {
2226                 close $fd;
2227                 return git_blob_plain($mimetype);
2228         }
2229         git_header_html();
2230         my $formats_nav = '';
2231         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2232                 if (defined $file_name) {
2233                         if ($have_blame) {
2234                                 $formats_nav .=
2235                                         $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2236                                                                hash=>$hash, file_name=>$file_name)},
2237                                                 "blame") .
2238                                         " | ";
2239                         }
2240                         $formats_nav .=
2241                                 $cgi->a({-href => href(action=>"blob_plain",
2242                                                        hash=>$hash, file_name=>$file_name)},
2243                                         "plain") .
2244                                 " | " .
2245                                 $cgi->a({-href => href(action=>"blob",
2246                                                        hash_base=>"HEAD", file_name=>$file_name)},
2247                                         "head");
2248                 } else {
2249                         $formats_nav .=
2250                                 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2251                 }
2252                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2253                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2254         } else {
2255                 print "<div class=\"page_nav\">\n" .
2256                       "<br/><br/></div>\n" .
2257                       "<div class=\"title\">$hash</div>\n";
2258         }
2259         git_print_page_path($file_name, "blob", $hash_base);
2260         print "<div class=\"page_body\">\n";
2261         my $nr;
2262         while (my $line = <$fd>) {
2263                 chomp $line;
2264                 $nr++;
2265                 $line = untabify($line);
2266                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2267                        $nr, $nr, $nr, esc_html($line);
2268         }
2269         close $fd
2270                 or print "Reading blob failed.\n";
2271         print "</div>";
2272         git_footer_html();
2275 sub git_tree {
2276         if (!defined $hash) {
2277                 $hash = git_get_head_hash($project);
2278                 if (defined $file_name) {
2279                         my $base = $hash_base || $hash;
2280                         $hash = git_get_hash_by_path($base, $file_name, "tree");
2281                 }
2282                 if (!defined $hash_base) {
2283                         $hash_base = $hash;
2284                 }
2285         }
2286         $/ = "\0";
2287         open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2288                 or die_error(undef, "Open git-ls-tree failed");
2289         my @entries = map { chomp; $_ } <$fd>;
2290         close $fd or die_error(undef, "Reading tree failed");
2291         $/ = "\n";
2293         my $refs = git_get_references();
2294         my $ref = format_ref_marker($refs, $hash_base);
2295         git_header_html();
2296         my %base_key = ();
2297         my $base = "";
2298         my $have_blame = gitweb_check_feature('blame');
2299         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2300                 $base_key{hash_base} = $hash_base;
2301                 git_print_page_nav('tree','', $hash_base);
2302                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2303         } else {
2304                 print "<div class=\"page_nav\">\n";
2305                 print "<br/><br/></div>\n";
2306                 print "<div class=\"title\">$hash</div>\n";
2307         }
2308         if (defined $file_name) {
2309                 $base = esc_html("$file_name/");
2310         }
2311         git_print_page_path($file_name, 'tree', $hash_base);
2312         print "<div class=\"page_body\">\n";
2313         print "<table cellspacing=\"0\">\n";
2314         my $alternate = 0;
2315         foreach my $line (@entries) {
2316                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
2317                 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2318                 my $t_mode = $1;
2319                 my $t_type = $2;
2320                 my $t_hash = $3;
2321                 my $t_name = validate_input($4);
2322                 if ($alternate) {
2323                         print "<tr class=\"dark\">\n";
2324                 } else {
2325                         print "<tr class=\"light\">\n";
2326                 }
2327                 $alternate ^= 1;
2328                 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2329                 if ($t_type eq "blob") {
2330                         print "<td class=\"list\">" .
2331                               $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2332                                       -class => "list"}, esc_html($t_name)) .
2333                               "</td>\n" .
2334                               "<td class=\"link\">" .
2335                               $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2336                                       "blob");
2337                         if ($have_blame) {
2338                                 print " | " .
2339                                         $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2340                                                 "blame");
2341                         }
2342                         print " | " .
2343                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2344                                                      hash=>$t_hash, file_name=>"$base$t_name")},
2345                                       "history") .
2346                               " | " .
2347                               $cgi->a({-href => href(action=>"blob_plain",
2348                                                      hash=>$t_hash, file_name=>"$base$t_name")},
2349                                       "raw") .
2350                               "</td>\n";
2351                 } elsif ($t_type eq "tree") {
2352                         print "<td class=\"list\">" .
2353                               $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2354                                       esc_html($t_name)) .
2355                               "</td>\n" .
2356                               "<td class=\"link\">" .
2357                               $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2358                                       "tree") .
2359                               " | " .
2360                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2361                                       "history") .
2362                               "</td>\n";
2363                 }
2364                 print "</tr>\n";
2365         }
2366         print "</table>\n" .
2367               "</div>";
2368         git_footer_html();
2371 sub git_snapshot {
2373         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2374         my $have_snapshot = (defined $ctype && defined $suffix);
2375         if (!$have_snapshot) {
2376                 die_error('403 Permission denied', "Permission denied");
2377         }
2379         if (!defined $hash) {
2380                 $hash = git_get_head_hash($project);
2381         }
2383         my $filename = basename($project) . "-$hash.tar.$suffix";
2385         print $cgi->header(-type => 'application/x-tar',
2386                            -content_encoding => $ctype,
2387                            -content_disposition => "inline; filename=\"$filename\"",
2388                            -status => '200 OK');
2390         open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2391                 die_error(undef, "Execute git-tar-tree failed.");
2392         binmode STDOUT, ':raw';
2393         print <$fd>;
2394         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2395         close $fd;
2399 sub git_log {
2400         my $head = git_get_head_hash($project);
2401         if (!defined $hash) {
2402                 $hash = $head;
2403         }
2404         if (!defined $page) {
2405                 $page = 0;
2406         }
2407         my $refs = git_get_references();
2409         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2410         open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2411                 or die_error(undef, "Open git-rev-list failed");
2412         my @revlist = map { chomp; $_ } <$fd>;
2413         close $fd;
2415         my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2417         git_header_html();
2418         git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2420         if (!@revlist) {
2421                 my %co = parse_commit($hash);
2423                 git_print_header_div('summary', $project);
2424                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2425         }
2426         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2427                 my $commit = $revlist[$i];
2428                 my $ref = format_ref_marker($refs, $commit);
2429                 my %co = parse_commit($commit);
2430                 next if !%co;
2431                 my %ad = parse_date($co{'author_epoch'});
2432                 git_print_header_div('commit',
2433                                "<span class=\"age\">$co{'age_string'}</span>" .
2434                                esc_html($co{'title'}) . $ref,
2435                                $commit);
2436                 print "<div class=\"title_text\">\n" .
2437                       "<div class=\"log_link\">\n" .
2438                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2439                       " | " .
2440                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2441                       "<br/>\n" .
2442                       "</div>\n" .
2443                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
2444                       "</div>\n";
2446                 print "<div class=\"log_body\">\n";
2447                 git_print_simplified_log($co{'comment'});
2448                 print "</div>\n";
2449         }
2450         git_footer_html();
2453 sub git_commit {
2454         my %co = parse_commit($hash);
2455         if (!%co) {
2456                 die_error(undef, "Unknown commit object");
2457         }
2458         my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2459         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2461         my $parent = $co{'parent'};
2462         if (!defined $parent) {
2463                 $parent = "--root";
2464         }
2465         open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2466                 or die_error(undef, "Open git-diff-tree failed");
2467         my @difftree = map { chomp; $_ } <$fd>;
2468         close $fd or die_error(undef, "Reading git-diff-tree failed");
2470         # non-textual hash id's can be cached
2471         my $expires;
2472         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2473                 $expires = "+1d";
2474         }
2475         my $refs = git_get_references();
2476         my $ref = format_ref_marker($refs, $co{'id'});
2478         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2479         my $have_snapshot = (defined $ctype && defined $suffix);
2481         my $formats_nav = '';
2482         if (defined $file_name && defined $co{'parent'}) {
2483                 my $parent = $co{'parent'};
2484                 $formats_nav .=
2485                         $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2486                                 "blame");
2487         }
2488         git_header_html(undef, $expires);
2489         git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2490                            $hash, $co{'tree'}, $hash,
2491                            $formats_nav);
2493         if (defined $co{'parent'}) {
2494                 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2495         } else {
2496                 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2497         }
2498         print "<div class=\"title_text\">\n" .
2499               "<table cellspacing=\"0\">\n";
2500         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2501               "<tr>" .
2502               "<td></td><td> $ad{'rfc2822'}";
2503         if ($ad{'hour_local'} < 6) {
2504                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2505                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2506         } else {
2507                 printf(" (%02d:%02d %s)",
2508                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2509         }
2510         print "</td>" .
2511               "</tr>\n";
2512         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2513         print "<tr><td></td><td> $cd{'rfc2822'}" .
2514               sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2515               "</td></tr>\n";
2516         print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2517         print "<tr>" .
2518               "<td>tree</td>" .
2519               "<td class=\"sha1\">" .
2520               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2521                        class => "list"}, $co{'tree'}) .
2522               "</td>" .
2523               "<td class=\"link\">" .
2524               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2525                       "tree");
2526         if ($have_snapshot) {
2527                 print " | " .
2528                       $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2529         }
2530         print "</td>" .
2531               "</tr>\n";
2532         my $parents = $co{'parents'};
2533         foreach my $par (@$parents) {
2534                 print "<tr>" .
2535                       "<td>parent</td>" .
2536                       "<td class=\"sha1\">" .
2537                       $cgi->a({-href => href(action=>"commit", hash=>$par),
2538                                class => "list"}, $par) .
2539                       "</td>" .
2540                       "<td class=\"link\">" .
2541                       $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2542                       " | " .
2543                       $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2544                       "</td>" .
2545                       "</tr>\n";
2546         }
2547         print "</table>".
2548               "</div>\n";
2550         print "<div class=\"page_body\">\n";
2551         git_print_log($co{'comment'});
2552         print "</div>\n";
2554         git_difftree_body(\@difftree, $parent);
2556         git_footer_html();
2559 sub git_blobdiff {
2560         mkdir($git_temp, 0700);
2561         git_header_html();
2562         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2563                 my $formats_nav =
2564                         $cgi->a({-href => href(action=>"blobdiff_plain",
2565                                                hash=>$hash, hash_parent=>$hash_parent)},
2566                                 "plain");
2567                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2568                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2569         } else {
2570                 print "<div class=\"page_nav\">\n" .
2571                       "<br/><br/></div>\n" .
2572                       "<div class=\"title\">$hash vs $hash_parent</div>\n";
2573         }
2574         git_print_page_path($file_name, "blob", $hash_base);
2575         print "<div class=\"page_body\">\n" .
2576               "<div class=\"diff_info\">blob:" .
2577               $cgi->a({-href => href(action=>"blob", hash=>$hash_parent,
2578                                      hash_base=>$hash_base, file_name=>($file_parent || $file_name))},
2579                       $hash_parent) .
2580               " -> blob:" .
2581               $cgi->a({-href => href(action=>"blob", hash=>$hash,
2582                                      hash_base=>$hash_base, file_name=>$file_name)},
2583                       $hash) .
2584               "</div>\n";
2585         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2586         print "</div>"; # page_body
2587         git_footer_html();
2590 sub git_blobdiff_plain {
2591         mkdir($git_temp, 0700);
2592         print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2593         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2596 sub git_commitdiff {
2597         mkdir($git_temp, 0700);
2598         my %co = parse_commit($hash);
2599         if (!%co) {
2600                 die_error(undef, "Unknown commit object");
2601         }
2602         if (!defined $hash_parent) {
2603                 $hash_parent = $co{'parent'} || '--root';
2604         }
2605         open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2606                 or die_error(undef, "Open git-diff-tree failed");
2607         my @difftree = map { chomp; $_ } <$fd>;
2608         close $fd or die_error(undef, "Reading git-diff-tree failed");
2610         # non-textual hash id's can be cached
2611         my $expires;
2612         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2613                 $expires = "+1d";
2614         }
2615         my $refs = git_get_references();
2616         my $ref = format_ref_marker($refs, $co{'id'});
2617         my $formats_nav =
2618                 $cgi->a({-href => href(action=>"commitdiff_plain", hash=>$hash, hash_parent=>$hash_parent)},
2619                         "plain");
2620         git_header_html(undef, $expires);
2621         git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2622         git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2623         print "<div class=\"page_body\">\n";
2624         git_print_simplified_log($co{'comment'}, 1); # skip title
2625         print "<br/>\n";
2626         foreach my $line (@difftree) {
2627                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
2628                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
2629                 if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2630                         next;
2631                 }
2632                 my $from_mode = $1;
2633                 my $to_mode = $2;
2634                 my $from_id = $3;
2635                 my $to_id = $4;
2636                 my $status = $5;
2637                 my $file = validate_input(unquote($6));
2638                 if ($status eq "A") {
2639                         print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
2640                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2641                                                      hash=>$to_id, file_name=>$file)},
2642                                       $to_id) . "(new)" .
2643                               "</div>\n";
2644                         git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2645                 } elsif ($status eq "D") {
2646                         print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2647                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2648                                                      hash=>$from_id, file_name=>$file)},
2649                                       $from_id) . "(deleted)" .
2650                               "</div>\n";
2651                         git_diff_print($from_id, "a/$file", undef, "/dev/null");
2652                 } elsif ($status eq "M") {
2653                         if ($from_id ne $to_id) {
2654                                 print "<div class=\"diff_info\">" .
2655                                       file_type($from_mode) . ":" .
2656                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
2657                                                              hash=>$from_id, file_name=>$file)},
2658                                               $from_id) .
2659                                       " -> " .
2660                                       file_type($to_mode) . ":" .
2661                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
2662                                                              hash=>$to_id, file_name=>$file)},
2663                                               $to_id);
2664                                 print "</div>\n";
2665                                 git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
2666                         }
2667                 }
2668         }
2669         print "<br/>\n" .
2670               "</div>";
2671         git_footer_html();
2674 sub git_commitdiff_plain {
2675         mkdir($git_temp, 0700);
2676         my %co = parse_commit($hash);
2677         if (!%co) {
2678                 die_error(undef, "Unknown commit object");
2679         }
2680         if (!defined $hash_parent) {
2681                 $hash_parent = $co{'parent'} || '--root';
2682         }
2683         open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2684                 or die_error(undef, "Open git-diff-tree failed");
2685         my @difftree = map { chomp; $_ } <$fd>;
2686         close $fd or die_error(undef, "Reading diff-tree failed");
2688         # try to figure out the next tag after this commit
2689         my $tagname;
2690         my $refs = git_get_references("tags");
2691         open $fd, "-|", $GIT, "rev-list", "HEAD";
2692         my @commits = map { chomp; $_ } <$fd>;
2693         close $fd;
2694         foreach my $commit (@commits) {
2695                 if (defined $refs->{$commit}) {
2696                         $tagname = $refs->{$commit}
2697                 }
2698                 if ($commit eq $hash) {
2699                         last;
2700                 }
2701         }
2703         print $cgi->header(-type => "text/plain",
2704                            -charset => 'utf-8',
2705                            -content_disposition => "inline; filename=\"git-$hash.patch\"");
2706         my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2707         my $comment = $co{'comment'};
2708         print "From: $co{'author'}\n" .
2709               "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
2710               "Subject: $co{'title'}\n";
2711         if (defined $tagname) {
2712                 print "X-Git-Tag: $tagname\n";
2713         }
2714         print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2715               "\n";
2717         foreach my $line (@$comment) {;
2718                 print "$line\n";
2719         }
2720         print "---\n\n";
2722         foreach my $line (@difftree) {
2723                 if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2724                         next;
2725                 }
2726                 my $from_id = $3;
2727                 my $to_id = $4;
2728                 my $status = $5;
2729                 my $file = $6;
2730                 if ($status eq "A") {
2731                         git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2732                 } elsif ($status eq "D") {
2733                         git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2734                 } elsif ($status eq "M") {
2735                         git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
2736                 }
2737         }
2740 sub git_history {
2741         if (!defined $hash_base) {
2742                 $hash_base = git_get_head_hash($project);
2743         }
2744         my $ftype;
2745         my %co = parse_commit($hash_base);
2746         if (!%co) {
2747                 die_error(undef, "Unknown commit object");
2748         }
2749         my $refs = git_get_references();
2750         git_header_html();
2751         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2752         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2753         if (!defined $hash && defined $file_name) {
2754                 $hash = git_get_hash_by_path($hash_base, $file_name);
2755         }
2756         if (defined $hash) {
2757                 $ftype = git_get_type($hash);
2758         }
2759         git_print_page_path($file_name, $ftype, $hash_base);
2761         open my $fd, "-|",
2762                 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2763         git_history_body($fd, $refs, $hash_base, $ftype);
2765         close $fd;
2766         git_footer_html();
2769 sub git_search {
2770         if (!defined $searchtext) {
2771                 die_error(undef, "Text field empty");
2772         }
2773         if (!defined $hash) {
2774                 $hash = git_get_head_hash($project);
2775         }
2776         my %co = parse_commit($hash);
2777         if (!%co) {
2778                 die_error(undef, "Unknown commit object");
2779         }
2780         # pickaxe may take all resources of your box and run for several minutes
2781         # with every query - so decide by yourself how public you make this feature :)
2782         my $commit_search = 1;
2783         my $author_search = 0;
2784         my $committer_search = 0;
2785         my $pickaxe_search = 0;
2786         if ($searchtext =~ s/^author\\://i) {
2787                 $author_search = 1;
2788         } elsif ($searchtext =~ s/^committer\\://i) {
2789                 $committer_search = 1;
2790         } elsif ($searchtext =~ s/^pickaxe\\://i) {
2791                 $commit_search = 0;
2792                 $pickaxe_search = 1;
2793         }
2794         git_header_html();
2795         git_print_page_nav('','', $hash,$co{'tree'},$hash);
2796         git_print_header_div('commit', esc_html($co{'title'}), $hash);
2798         print "<table cellspacing=\"0\">\n";
2799         my $alternate = 0;
2800         if ($commit_search) {
2801                 $/ = "\0";
2802                 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2803                 while (my $commit_text = <$fd>) {
2804                         if (!grep m/$searchtext/i, $commit_text) {
2805                                 next;
2806                         }
2807                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2808                                 next;
2809                         }
2810                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2811                                 next;
2812                         }
2813                         my @commit_lines = split "\n", $commit_text;
2814                         my %co = parse_commit(undef, \@commit_lines);
2815                         if (!%co) {
2816                                 next;
2817                         }
2818                         if ($alternate) {
2819                                 print "<tr class=\"dark\">\n";
2820                         } else {
2821                                 print "<tr class=\"light\">\n";
2822                         }
2823                         $alternate ^= 1;
2824                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2825                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2826                               "<td>" .
2827                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2828                                        esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2829                         my $comment = $co{'comment'};
2830                         foreach my $line (@$comment) {
2831                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2832                                         my $lead = esc_html($1) || "";
2833                                         $lead = chop_str($lead, 30, 10);
2834                                         my $match = esc_html($2) || "";
2835                                         my $trail = esc_html($3) || "";
2836                                         $trail = chop_str($trail, 30, 10);
2837                                         my $text = "$lead<span class=\"match\">$match</span>$trail";
2838                                         print chop_str($text, 80, 5) . "<br/>\n";
2839                                 }
2840                         }
2841                         print "</td>\n" .
2842                               "<td class=\"link\">" .
2843                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2844                               " | " .
2845                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2846                         print "</td>\n" .
2847                               "</tr>\n";
2848                 }
2849                 close $fd;
2850         }
2852         if ($pickaxe_search) {
2853                 $/ = "\n";
2854                 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2855                 undef %co;
2856                 my @files;
2857                 while (my $line = <$fd>) {
2858                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2859                                 my %set;
2860                                 $set{'file'} = $6;
2861                                 $set{'from_id'} = $3;
2862                                 $set{'to_id'} = $4;
2863                                 $set{'id'} = $set{'to_id'};
2864                                 if ($set{'id'} =~ m/0{40}/) {
2865                                         $set{'id'} = $set{'from_id'};
2866                                 }
2867                                 if ($set{'id'} =~ m/0{40}/) {
2868                                         next;
2869                                 }
2870                                 push @files, \%set;
2871                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2872                                 if (%co) {
2873                                         if ($alternate) {
2874                                                 print "<tr class=\"dark\">\n";
2875                                         } else {
2876                                                 print "<tr class=\"light\">\n";
2877                                         }
2878                                         $alternate ^= 1;
2879                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2880                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2881                                               "<td>" .
2882                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
2883                                                       -class => "list subject"},
2884                                                       esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2885                                         while (my $setref = shift @files) {
2886                                                 my %set = %$setref;
2887                                                 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
2888                                                                              hash=>$set{'id'}, file_name=>$set{'file'}),
2889                                                               -class => "list"},
2890                                                               "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2891                                                       "<br/>\n";
2892                                         }
2893                                         print "</td>\n" .
2894                                               "<td class=\"link\">" .
2895                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2896                                               " | " .
2897                                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2898                                         print "</td>\n" .
2899                                               "</tr>\n";
2900                                 }
2901                                 %co = parse_commit($1);
2902                         }
2903                 }
2904                 close $fd;
2905         }
2906         print "</table>\n";
2907         git_footer_html();
2910 sub git_shortlog {
2911         my $head = git_get_head_hash($project);
2912         if (!defined $hash) {
2913                 $hash = $head;
2914         }
2915         if (!defined $page) {
2916                 $page = 0;
2917         }
2918         my $refs = git_get_references();
2920         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2921         open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2922                 or die_error(undef, "Open git-rev-list failed");
2923         my @revlist = map { chomp; $_ } <$fd>;
2924         close $fd;
2926         my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
2927         my $next_link = '';
2928         if ($#revlist >= (100 * ($page+1)-1)) {
2929                 $next_link =
2930                         $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
2931                                  -title => "Alt-n"}, "next");
2932         }
2935         git_header_html();
2936         git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
2937         git_print_header_div('summary', $project);
2939         git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
2941         git_footer_html();
2944 ## ......................................................................
2945 ## feeds (RSS, OPML)
2947 sub git_rss {
2948         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
2949         open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
2950                 or die_error(undef, "Open git-rev-list failed");
2951         my @revlist = map { chomp; $_ } <$fd>;
2952         close $fd or die_error(undef, "Reading git-rev-list failed");
2953         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2954         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2955               "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
2956         print "<channel>\n";
2957         print "<title>$project</title>\n".
2958               "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
2959               "<description>$project log</description>\n".
2960               "<language>en</language>\n";
2962         for (my $i = 0; $i <= $#revlist; $i++) {
2963                 my $commit = $revlist[$i];
2964                 my %co = parse_commit($commit);
2965                 # we read 150, we always show 30 and the ones more recent than 48 hours
2966                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
2967                         last;
2968                 }
2969                 my %cd = parse_date($co{'committer_epoch'});
2970                 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
2971                 my @difftree = map { chomp; $_ } <$fd>;
2972                 close $fd or next;
2973                 print "<item>\n" .
2974                       "<title>" .
2975                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
2976                       "</title>\n" .
2977                       "<author>" . esc_html($co{'author'}) . "</author>\n" .
2978                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
2979                       "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
2980                       "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
2981                       "<description>" . esc_html($co{'title'}) . "</description>\n" .
2982                       "<content:encoded>" .
2983                       "<![CDATA[\n";
2984                 my $comment = $co{'comment'};
2985                 foreach my $line (@$comment) {
2986                         $line = decode("utf8", $line, Encode::FB_DEFAULT);
2987                         print "$line<br/>\n";
2988                 }
2989                 print "<br/>\n";
2990                 foreach my $line (@difftree) {
2991                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2992                                 next;
2993                         }
2994                         my $file = validate_input(unquote($7));
2995                         $file = decode("utf8", $file, Encode::FB_DEFAULT);
2996                         print "$file<br/>\n";
2997                 }
2998                 print "]]>\n" .
2999                       "</content:encoded>\n" .
3000                       "</item>\n";
3001         }
3002         print "</channel></rss>";
3005 sub git_opml {
3006         my @list = git_get_projects_list();
3008         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3009         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
3010               "<opml version=\"1.0\">\n".
3011               "<head>".
3012               "  <title>$site_name Git OPML Export</title>\n".
3013               "</head>\n".
3014               "<body>\n".
3015               "<outline text=\"git RSS feeds\">\n";
3017         foreach my $pr (@list) {
3018                 my %proj = %$pr;
3019                 my $head = git_get_head_hash($proj{'path'});
3020                 if (!defined $head) {
3021                         next;
3022                 }
3023                 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3024                 my %co = parse_commit($head);
3025                 if (!%co) {
3026                         next;
3027                 }
3029                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3030                 my $rss  = "$my_url?p=$proj{'path'};a=rss";
3031                 my $html = "$my_url?p=$proj{'path'};a=summary";
3032                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3033         }
3034         print "</outline>\n".
3035               "</body>\n".
3036               "</opml>\n";