Code

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