Code

Merge branch 'master' into pb/gitpm
[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 binmode STDOUT, ':utf8';
20 our $cgi = new CGI;
21 our $version = "++GIT_VERSION++";
22 our $my_url = $cgi->url();
23 our $my_uri = $cgi->url(-absolute => 1);
25 # core git executable to use
26 # this can just be "git" if your webserver has a sensible PATH
27 our $GIT = "++GIT_BINDIR++/git";
29 # absolute fs-path which will be prepended to the project path
30 #our $projectroot = "/pub/scm";
31 our $projectroot = "++GITWEB_PROJECTROOT++";
33 # location for temporary files needed for diffs
34 our $git_temp = "/tmp/gitweb";
36 # target of the home link on top of all pages
37 our $home_link = $my_uri;
39 # string of the home link on top of all pages
40 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
42 # name of your site or organization to appear in page titles
43 # replace this with something more descriptive for clearer bookmarks
44 our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
46 # html text to include at home page
47 our $home_text = "++GITWEB_HOMETEXT++";
49 # URI of default stylesheet
50 our $stylesheet = "++GITWEB_CSS++";
51 # URI of GIT logo
52 our $logo = "++GITWEB_LOGO++";
54 # source of projects list
55 our $projects_list = "++GITWEB_LIST++";
57 # default blob_plain mimetype and default charset for text/plain blob
58 our $default_blob_plain_mimetype = 'text/plain';
59 our $default_text_plain_charset  = undef;
61 # file to use for guessing MIME types before trying /etc/mime.types
62 # (relative to the current git repository)
63 our $mimetypes_file = undef;
65 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
66 require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
68 # version of the core git binary
69 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
71 $projects_list ||= $projectroot;
72 if (! -d $git_temp) {
73         mkdir($git_temp, 0700) || die_error(undef, "Couldn't mkdir $git_temp");
74 }
76 # ======================================================================
77 # input validation and dispatch
78 our $action = $cgi->param('a');
79 if (defined $action) {
80         if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
81                 die_error(undef, "Invalid action parameter");
82         }
83         # action which does not check rest of parameters
84         if ($action eq "opml") {
85                 git_opml();
86                 exit;
87         }
88 }
90 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
91 if (defined $project) {
92         $project =~ s|^/||;
93         $project =~ s|/$||;
94 }
95 if (defined $project && $project) {
96         if (!validate_input($project)) {
97                 die_error(undef, "Invalid project parameter");
98         }
99         if (!(-d "$projectroot/$project")) {
100                 die_error(undef, "No such directory");
101         }
102         if (!(-e "$projectroot/$project/HEAD")) {
103                 die_error(undef, "No such project");
104         }
105         $ENV{'GIT_DIR'} = "$projectroot/$project";
106 } else {
107         git_project_list();
108         exit;
111 our $file_name = $cgi->param('f');
112 if (defined $file_name) {
113         if (!validate_input($file_name)) {
114                 die_error(undef, "Invalid file parameter");
115         }
118 our $hash = $cgi->param('h');
119 if (defined $hash) {
120         if (!validate_input($hash)) {
121                 die_error(undef, "Invalid hash parameter");
122         }
125 our $hash_parent = $cgi->param('hp');
126 if (defined $hash_parent) {
127         if (!validate_input($hash_parent)) {
128                 die_error(undef, "Invalid hash parent parameter");
129         }
132 our $hash_base = $cgi->param('hb');
133 if (defined $hash_base) {
134         if (!validate_input($hash_base)) {
135                 die_error(undef, "Invalid hash base parameter");
136         }
139 our $page = $cgi->param('pg');
140 if (defined $page) {
141         if ($page =~ m/[^0-9]$/) {
142                 die_error(undef, "Invalid page parameter");
143         }
146 our $searchtext = $cgi->param('s');
147 if (defined $searchtext) {
148         if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
149                 die_error(undef, "Invalid search parameter");
150         }
151         $searchtext = quotemeta $searchtext;
154 # dispatch
155 my %actions = (
156         "blame" => \&git_blame2,
157         "blobdiff" => \&git_blobdiff,
158         "blobdiff_plain" => \&git_blobdiff_plain,
159         "blob" => \&git_blob,
160         "blob_plain" => \&git_blob_plain,
161         "commitdiff" => \&git_commitdiff,
162         "commitdiff_plain" => \&git_commitdiff_plain,
163         "commit" => \&git_commit,
164         "heads" => \&git_heads,
165         "history" => \&git_history,
166         "log" => \&git_log,
167         "rss" => \&git_rss,
168         "search" => \&git_search,
169         "shortlog" => \&git_shortlog,
170         "summary" => \&git_summary,
171         "tag" => \&git_tag,
172         "tags" => \&git_tags,
173         "tree" => \&git_tree,
174 );
176 $action = 'summary' if (!defined($action));
177 if (!defined($actions{$action})) {
178         die_error(undef, "Unknown action");
180 $actions{$action}->();
181 exit;
183 ## ======================================================================
184 ## validation, quoting/unquoting and escaping
186 sub validate_input {
187         my $input = shift;
189         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
190                 return $input;
191         }
192         if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
193                 return undef;
194         }
195         if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
196                 return undef;
197         }
198         return $input;
201 # quote unsafe chars, but keep the slash, even when it's not
202 # correct, but quoted slashes look too horrible in bookmarks
203 sub esc_param {
204         my $str = shift;
205         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
206         $str =~ s/\+/%2B/g;
207         $str =~ s/ /\+/g;
208         return $str;
211 # replace invalid utf8 character with SUBSTITUTION sequence
212 sub esc_html {
213         my $str = shift;
214         $str = decode("utf8", $str, Encode::FB_DEFAULT);
215         $str = escapeHTML($str);
216         $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
217         return $str;
220 # git may return quoted and escaped filenames
221 sub unquote {
222         my $str = shift;
223         if ($str =~ m/^"(.*)"$/) {
224                 $str = $1;
225                 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
226         }
227         return $str;
230 # escape tabs (convert tabs to spaces)
231 sub untabify {
232         my $line = shift;
234         while ((my $pos = index($line, "\t")) != -1) {
235                 if (my $count = (8 - ($pos % 8))) {
236                         my $spaces = ' ' x $count;
237                         $line =~ s/\t/$spaces/;
238                 }
239         }
241         return $line;
244 ## ----------------------------------------------------------------------
245 ## HTML aware string manipulation
247 sub chop_str {
248         my $str = shift;
249         my $len = shift;
250         my $add_len = shift || 10;
252         # allow only $len chars, but don't cut a word if it would fit in $add_len
253         # if it doesn't fit, cut it if it's still longer than the dots we would add
254         $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
255         my $body = $1;
256         my $tail = $2;
257         if (length($tail) > 4) {
258                 $tail = " ...";
259                 $body =~ s/&[^;]*$//; # remove chopped character entities
260         }
261         return "$body$tail";
264 ## ----------------------------------------------------------------------
265 ## functions returning short strings
267 # CSS class for given age value (in seconds)
268 sub age_class {
269         my $age = shift;
271         if ($age < 60*60*2) {
272                 return "age0";
273         } elsif ($age < 60*60*24*2) {
274                 return "age1";
275         } else {
276                 return "age2";
277         }
280 # convert age in seconds to "nn units ago" string
281 sub age_string {
282         my $age = shift;
283         my $age_str;
285         if ($age > 60*60*24*365*2) {
286                 $age_str = (int $age/60/60/24/365);
287                 $age_str .= " years ago";
288         } elsif ($age > 60*60*24*(365/12)*2) {
289                 $age_str = int $age/60/60/24/(365/12);
290                 $age_str .= " months ago";
291         } elsif ($age > 60*60*24*7*2) {
292                 $age_str = int $age/60/60/24/7;
293                 $age_str .= " weeks ago";
294         } elsif ($age > 60*60*24*2) {
295                 $age_str = int $age/60/60/24;
296                 $age_str .= " days ago";
297         } elsif ($age > 60*60*2) {
298                 $age_str = int $age/60/60;
299                 $age_str .= " hours ago";
300         } elsif ($age > 60*2) {
301                 $age_str = int $age/60;
302                 $age_str .= " min ago";
303         } elsif ($age > 2) {
304                 $age_str = int $age;
305                 $age_str .= " sec ago";
306         } else {
307                 $age_str .= " right now";
308         }
309         return $age_str;
312 # convert file mode in octal to symbolic file mode string
313 sub mode_str {
314         my $mode = oct shift;
316         if (S_ISDIR($mode & S_IFMT)) {
317                 return 'drwxr-xr-x';
318         } elsif (S_ISLNK($mode)) {
319                 return 'lrwxrwxrwx';
320         } elsif (S_ISREG($mode)) {
321                 # git cares only about the executable bit
322                 if ($mode & S_IXUSR) {
323                         return '-rwxr-xr-x';
324                 } else {
325                         return '-rw-r--r--';
326                 };
327         } else {
328                 return '----------';
329         }
332 # convert file mode in octal to file type string
333 sub file_type {
334         my $mode = oct shift;
336         if (S_ISDIR($mode & S_IFMT)) {
337                 return "directory";
338         } elsif (S_ISLNK($mode)) {
339                 return "symlink";
340         } elsif (S_ISREG($mode)) {
341                 return "file";
342         } else {
343                 return "unknown";
344         }
347 ## ----------------------------------------------------------------------
348 ## functions returning short HTML fragments, or transforming HTML fragments
349 ## which don't beling to other sections
351 # format line of commit message or tag comment
352 sub format_log_line_html {
353         my $line = shift;
355         $line = esc_html($line);
356         $line =~ s/ /&nbsp;/g;
357         if ($line =~ m/([0-9a-fA-F]{40})/) {
358                 my $hash_text = $1;
359                 if (git_get_type($hash_text) eq "commit") {
360                         my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
361                         $line =~ s/$hash_text/$link/;
362                 }
363         }
364         return $line;
367 # format marker of refs pointing to given object
368 sub format_ref_marker {
369         my ($refs, $id) = @_;
370         my $markers = '';
372         if (defined $refs->{$id}) {
373                 foreach my $ref (@{$refs->{$id}}) {
374                         my ($type, $name) = qw();
375                         # e.g. tags/v2.6.11 or heads/next
376                         if ($ref =~ m!^(.*?)s?/(.*)$!) {
377                                 $type = $1;
378                                 $name = $2;
379                         } else {
380                                 $type = "ref";
381                                 $name = $ref;
382                         }
384                         $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
385                 }
386         }
388         if ($markers) {
389                 return ' <span class="refs">'. $markers . '</span>';
390         } else {
391                 return "";
392         }
395 # format, perhaps shortened and with markers, title line
396 sub format_subject_html {
397         my ($long, $short, $query, $extra) = @_;
398         $extra = '' unless defined($extra);
400         if (length($short) < length($long)) {
401                 return $cgi->a({-href => "$my_uri?" . esc_param($query),
402                                -class => "list", -title => $long},
403                        esc_html($short) . $extra);
404         } else {
405                 return $cgi->a({-href => "$my_uri?" . esc_param($query),
406                                -class => "list"},
407                        esc_html($long)  . $extra);
408         }
411 ## ----------------------------------------------------------------------
412 ## git utility subroutines, invoking git commands
414 # get HEAD ref of given project as hash
415 sub git_get_head_hash {
416         my $project = shift;
417         my $oENV = $ENV{'GIT_DIR'};
418         my $retval = undef;
419         $ENV{'GIT_DIR'} = "$projectroot/$project";
420         if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
421                 my $head = <$fd>;
422                 close $fd;
423                 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
424                         $retval = $1;
425                 }
426         }
427         if (defined $oENV) {
428                 $ENV{'GIT_DIR'} = $oENV;
429         }
430         return $retval;
433 # get type of given object
434 sub git_get_type {
435         my $hash = shift;
437         open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
438         my $type = <$fd>;
439         close $fd or return;
440         chomp $type;
441         return $type;
444 sub git_get_project_config {
445         my $key = shift;
447         return unless ($key);
448         $key =~ s/^gitweb\.//;
449         return if ($key =~ m/\W/);
451         my $val = qx($GIT repo-config --get gitweb.$key);
452         return ($val);
455 sub git_get_project_config_bool {
456         my $val = git_get_project_config (@_);
457         if ($val and $val =~ m/true|yes|on/) {
458                 return (1);
459         }
460         return; # implicit false
463 # get hash of given path at given ref
464 sub git_get_hash_by_path {
465         my $base = shift;
466         my $path = shift || return undef;
468         my $tree = $base;
470         open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
471                 or die_error(undef, "Open git-ls-tree failed");
472         my $line = <$fd>;
473         close $fd or return undef;
475         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
476         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
477         return $3;
480 ## ......................................................................
481 ## git utility functions, directly accessing git repository
483 # assumes that PATH is not symref
484 sub git_get_hash_by_ref {
485         my $path = shift;
487         open my $fd, "$projectroot/$path" or return undef;
488         my $head = <$fd>;
489         close $fd;
490         chomp $head;
491         if ($head =~ m/^[0-9a-fA-F]{40}$/) {
492                 return $head;
493         }
496 sub git_get_project_description {
497         my $path = shift;
499         open my $fd, "$projectroot/$path/description" or return undef;
500         my $descr = <$fd>;
501         close $fd;
502         chomp $descr;
503         return $descr;
506 sub git_get_projects_list {
507         my @list;
509         if (-d $projects_list) {
510                 # search in directory
511                 my $dir = $projects_list;
512                 opendir my ($dh), $dir or return undef;
513                 while (my $dir = readdir($dh)) {
514                         if (-e "$projectroot/$dir/HEAD") {
515                                 my $pr = {
516                                         path => $dir,
517                                 };
518                                 push @list, $pr
519                         }
520                 }
521                 closedir($dh);
522         } elsif (-f $projects_list) {
523                 # read from file(url-encoded):
524                 # 'git%2Fgit.git Linus+Torvalds'
525                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
526                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
527                 open my ($fd), $projects_list or return undef;
528                 while (my $line = <$fd>) {
529                         chomp $line;
530                         my ($path, $owner) = split ' ', $line;
531                         $path = unescape($path);
532                         $owner = unescape($owner);
533                         if (!defined $path) {
534                                 next;
535                         }
536                         if (-e "$projectroot/$path/HEAD") {
537                                 my $pr = {
538                                         path => $path,
539                                         owner => decode("utf8", $owner, Encode::FB_DEFAULT),
540                                 };
541                                 push @list, $pr
542                         }
543                 }
544                 close $fd;
545         }
546         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
547         return @list;
550 sub git_get_project_owner {
551         my $project = shift;
552         my $owner;
554         return undef unless $project;
556         # read from file (url-encoded):
557         # 'git%2Fgit.git Linus+Torvalds'
558         # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
559         # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
560         if (-f $projects_list) {
561                 open (my $fd , $projects_list);
562                 while (my $line = <$fd>) {
563                         chomp $line;
564                         my ($pr, $ow) = split ' ', $line;
565                         $pr = unescape($pr);
566                         $ow = unescape($ow);
567                         if ($pr eq $project) {
568                                 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
569                                 last;
570                         }
571                 }
572                 close $fd;
573         }
574         if (!defined $owner) {
575                 $owner = get_file_owner("$projectroot/$project");
576         }
578         return $owner;
581 sub git_get_references {
582         my $type = shift || "";
583         my %refs;
584         my $fd;
585         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
586         # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
587         if (-f "$projectroot/$project/info/refs") {
588                 open $fd, "$projectroot/$project/info/refs"
589                         or return;
590         } else {
591                 open $fd, "-|", $GIT, "ls-remote", "."
592                         or return;
593         }
595         while (my $line = <$fd>) {
596                 chomp $line;
597                 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
598                         if (defined $refs{$1}) {
599                                 push @{$refs{$1}}, $2;
600                         } else {
601                                 $refs{$1} = [ $2 ];
602                         }
603                 }
604         }
605         close $fd or return;
606         return \%refs;
609 ## ----------------------------------------------------------------------
610 ## parse to hash functions
612 sub parse_date {
613         my $epoch = shift;
614         my $tz = shift || "-0000";
616         my %date;
617         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
618         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
619         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
620         $date{'hour'} = $hour;
621         $date{'minute'} = $min;
622         $date{'mday'} = $mday;
623         $date{'day'} = $days[$wday];
624         $date{'month'} = $months[$mon];
625         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
626         $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
628         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
629         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
630         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
631         $date{'hour_local'} = $hour;
632         $date{'minute_local'} = $min;
633         $date{'tz_local'} = $tz;
634         return %date;
637 sub parse_tag {
638         my $tag_id = shift;
639         my %tag;
640         my @comment;
642         open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
643         $tag{'id'} = $tag_id;
644         while (my $line = <$fd>) {
645                 chomp $line;
646                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
647                         $tag{'object'} = $1;
648                 } elsif ($line =~ m/^type (.+)$/) {
649                         $tag{'type'} = $1;
650                 } elsif ($line =~ m/^tag (.+)$/) {
651                         $tag{'name'} = $1;
652                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
653                         $tag{'author'} = $1;
654                         $tag{'epoch'} = $2;
655                         $tag{'tz'} = $3;
656                 } elsif ($line =~ m/--BEGIN/) {
657                         push @comment, $line;
658                         last;
659                 } elsif ($line eq "") {
660                         last;
661                 }
662         }
663         push @comment, <$fd>;
664         $tag{'comment'} = \@comment;
665         close $fd or return;
666         if (!defined $tag{'name'}) {
667                 return
668         };
669         return %tag
672 sub parse_commit {
673         my $commit_id = shift;
674         my $commit_text = shift;
676         my @commit_lines;
677         my %co;
679         if (defined $commit_text) {
680                 @commit_lines = @$commit_text;
681         } else {
682                 $/ = "\0";
683                 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id or return;
684                 @commit_lines = split '\n', <$fd>;
685                 close $fd or return;
686                 $/ = "\n";
687                 pop @commit_lines;
688         }
689         my $header = shift @commit_lines;
690         if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
691                 return;
692         }
693         ($co{'id'}, my @parents) = split ' ', $header;
694         $co{'parents'} = \@parents;
695         $co{'parent'} = $parents[0];
696         while (my $line = shift @commit_lines) {
697                 last if $line eq "\n";
698                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
699                         $co{'tree'} = $1;
700                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
701                         $co{'author'} = $1;
702                         $co{'author_epoch'} = $2;
703                         $co{'author_tz'} = $3;
704                         if ($co{'author'} =~ m/^([^<]+) </) {
705                                 $co{'author_name'} = $1;
706                         } else {
707                                 $co{'author_name'} = $co{'author'};
708                         }
709                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
710                         $co{'committer'} = $1;
711                         $co{'committer_epoch'} = $2;
712                         $co{'committer_tz'} = $3;
713                         $co{'committer_name'} = $co{'committer'};
714                         $co{'committer_name'} =~ s/ <.*//;
715                 }
716         }
717         if (!defined $co{'tree'}) {
718                 return;
719         };
721         foreach my $title (@commit_lines) {
722                 $title =~ s/^    //;
723                 if ($title ne "") {
724                         $co{'title'} = chop_str($title, 80, 5);
725                         # remove leading stuff of merges to make the interesting part visible
726                         if (length($title) > 50) {
727                                 $title =~ s/^Automatic //;
728                                 $title =~ s/^merge (of|with) /Merge ... /i;
729                                 if (length($title) > 50) {
730                                         $title =~ s/(http|rsync):\/\///;
731                                 }
732                                 if (length($title) > 50) {
733                                         $title =~ s/(master|www|rsync)\.//;
734                                 }
735                                 if (length($title) > 50) {
736                                         $title =~ s/kernel.org:?//;
737                                 }
738                                 if (length($title) > 50) {
739                                         $title =~ s/\/pub\/scm//;
740                                 }
741                         }
742                         $co{'title_short'} = chop_str($title, 50, 5);
743                         last;
744                 }
745         }
746         # remove added spaces
747         foreach my $line (@commit_lines) {
748                 $line =~ s/^    //;
749         }
750         $co{'comment'} = \@commit_lines;
752         my $age = time - $co{'committer_epoch'};
753         $co{'age'} = $age;
754         $co{'age_string'} = age_string($age);
755         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
756         if ($age > 60*60*24*7*2) {
757                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
758                 $co{'age_string_age'} = $co{'age_string'};
759         } else {
760                 $co{'age_string_date'} = $co{'age_string'};
761                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
762         }
763         return %co;
766 # parse ref from ref_file, given by ref_id, with given type
767 sub parse_ref {
768         my $ref_file = shift;
769         my $ref_id = shift;
770         my $type = shift || git_get_type($ref_id);
771         my %ref_item;
773         $ref_item{'type'} = $type;
774         $ref_item{'id'} = $ref_id;
775         $ref_item{'epoch'} = 0;
776         $ref_item{'age'} = "unknown";
777         if ($type eq "tag") {
778                 my %tag = parse_tag($ref_id);
779                 $ref_item{'comment'} = $tag{'comment'};
780                 if ($tag{'type'} eq "commit") {
781                         my %co = parse_commit($tag{'object'});
782                         $ref_item{'epoch'} = $co{'committer_epoch'};
783                         $ref_item{'age'} = $co{'age_string'};
784                 } elsif (defined($tag{'epoch'})) {
785                         my $age = time - $tag{'epoch'};
786                         $ref_item{'epoch'} = $tag{'epoch'};
787                         $ref_item{'age'} = age_string($age);
788                 }
789                 $ref_item{'reftype'} = $tag{'type'};
790                 $ref_item{'name'} = $tag{'name'};
791                 $ref_item{'refid'} = $tag{'object'};
792         } elsif ($type eq "commit"){
793                 my %co = parse_commit($ref_id);
794                 $ref_item{'reftype'} = "commit";
795                 $ref_item{'name'} = $ref_file;
796                 $ref_item{'title'} = $co{'title'};
797                 $ref_item{'refid'} = $ref_id;
798                 $ref_item{'epoch'} = $co{'committer_epoch'};
799                 $ref_item{'age'} = $co{'age_string'};
800         } else {
801                 $ref_item{'reftype'} = $type;
802                 $ref_item{'name'} = $ref_file;
803                 $ref_item{'refid'} = $ref_id;
804         }
806         return %ref_item;
809 ## ......................................................................
810 ## parse to array of hashes functions
812 sub git_get_refs_list {
813         my $ref_dir = shift;
814         my @reflist;
816         my @refs;
817         my $pfxlen = length("$projectroot/$project/$ref_dir");
818         File::Find::find(sub {
819                 return if (/^\./);
820                 if (-f $_) {
821                         push @refs, substr($File::Find::name, $pfxlen + 1);
822                 }
823         }, "$projectroot/$project/$ref_dir");
825         foreach my $ref_file (@refs) {
826                 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
827                 my $type = git_get_type($ref_id) || next;
828                 my %ref_item = parse_ref($ref_file, $ref_id, $type);
830                 push @reflist, \%ref_item;
831         }
832         # sort refs by age
833         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
834         return \@reflist;
837 ## ----------------------------------------------------------------------
838 ## filesystem-related functions
840 sub get_file_owner {
841         my $path = shift;
843         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
844         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
845         if (!defined $gcos) {
846                 return undef;
847         }
848         my $owner = $gcos;
849         $owner =~ s/[,;].*$//;
850         return decode("utf8", $owner, Encode::FB_DEFAULT);
853 ## ......................................................................
854 ## mimetype related functions
856 sub mimetype_guess_file {
857         my $filename = shift;
858         my $mimemap = shift;
859         -r $mimemap or return undef;
861         my %mimemap;
862         open(MIME, $mimemap) or return undef;
863         while (<MIME>) {
864                 next if m/^#/; # skip comments
865                 my ($mime, $exts) = split(/\t+/);
866                 if (defined $exts) {
867                         my @exts = split(/\s+/, $exts);
868                         foreach my $ext (@exts) {
869                                 $mimemap{$ext} = $mime;
870                         }
871                 }
872         }
873         close(MIME);
875         $filename =~ /\.(.*?)$/;
876         return $mimemap{$1};
879 sub mimetype_guess {
880         my $filename = shift;
881         my $mime;
882         $filename =~ /\./ or return undef;
884         if ($mimetypes_file) {
885                 my $file = $mimetypes_file;
886                 if ($file !~ m!^/!) { # if it is relative path
887                         # it is relative to project
888                         $file = "$projectroot/$project/$file";
889                 }
890                 $mime = mimetype_guess_file($filename, $file);
891         }
892         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
893         return $mime;
896 sub blob_mimetype {
897         my $fd = shift;
898         my $filename = shift;
900         if ($filename) {
901                 my $mime = mimetype_guess($filename);
902                 $mime and return $mime;
903         }
905         # just in case
906         return $default_blob_plain_mimetype unless $fd;
908         if (-T $fd) {
909                 return 'text/plain' .
910                        ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
911         } elsif (! $filename) {
912                 return 'application/octet-stream';
913         } elsif ($filename =~ m/\.png$/i) {
914                 return 'image/png';
915         } elsif ($filename =~ m/\.gif$/i) {
916                 return 'image/gif';
917         } elsif ($filename =~ m/\.jpe?g$/i) {
918                 return 'image/jpeg';
919         } else {
920                 return 'application/octet-stream';
921         }
924 ## ======================================================================
925 ## functions printing HTML: header, footer, error page
927 sub git_header_html {
928         my $status = shift || "200 OK";
929         my $expires = shift;
931         my $title = "$site_name git";
932         if (defined $project) {
933                 $title .= " - $project";
934                 if (defined $action) {
935                         $title .= "/$action";
936                         if (defined $file_name) {
937                                 $title .= " - $file_name";
938                                 if ($action eq "tree" && $file_name !~ m|/$|) {
939                                         $title .= "/";
940                                 }
941                         }
942                 }
943         }
944         my $content_type;
945         # require explicit support from the UA if we are to send the page as
946         # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
947         # we have to do this because MSIE sometimes globs '*/*', pretending to
948         # support xhtml+xml but choking when it gets what it asked for.
949         if (defined $cgi->http('HTTP_ACCEPT') && $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) {
950                 $content_type = 'application/xhtml+xml';
951         } else {
952                 $content_type = 'text/html';
953         }
954         print $cgi->header(-type=>$content_type, -charset => 'utf-8', -status=> $status, -expires => $expires);
955         print <<EOF;
956 <?xml version="1.0" encoding="utf-8"?>
957 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
958 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
959 <!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
960 <!-- git core binaries version $git_version -->
961 <head>
962 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
963 <meta name="robots" content="index, nofollow"/>
964 <title>$title</title>
965 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
966 EOF
967         if (defined $project) {
968                 printf('<link rel="alternate" title="%s log" '.
969                        'href="%s" type="application/rss+xml"/>'."\n",
970                        esc_param($project),
971                        esc_param("$my_uri?p=$project;a=rss"));
972         }
974         print "</head>\n" .
975               "<body>\n" .
976               "<div class=\"page_header\">\n" .
977               "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
978               "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
979               "</a>\n";
980         print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
981         if (defined $project) {
982                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
983                 if (defined $action) {
984                         print " / $action";
985                 }
986                 print "\n";
987                 if (!defined $searchtext) {
988                         $searchtext = "";
989                 }
990                 my $search_hash;
991                 if (defined $hash_base) {
992                         $search_hash = $hash_base;
993                 } elsif (defined $hash) {
994                         $search_hash = $hash;
995                 } else {
996                         $search_hash = "HEAD";
997                 }
998                 $cgi->param("a", "search");
999                 $cgi->param("h", $search_hash);
1000                 print $cgi->startform(-method => "get", -action => $my_uri) .
1001                       "<div class=\"search\">\n" .
1002                       $cgi->hidden(-name => "p") . "\n" .
1003                       $cgi->hidden(-name => "a") . "\n" .
1004                       $cgi->hidden(-name => "h") . "\n" .
1005                       $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1006                       "</div>" .
1007                       $cgi->end_form() . "\n";
1008         }
1009         print "</div>\n";
1012 sub git_footer_html {
1013         print "<div class=\"page_footer\">\n";
1014         if (defined $project) {
1015                 my $descr = git_get_project_description($project);
1016                 if (defined $descr) {
1017                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1018                 }
1019                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
1020         } else {
1021                 print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
1022         }
1023         print "</div>\n" .
1024               "</body>\n" .
1025               "</html>";
1028 sub die_error {
1029         my $status = shift || "403 Forbidden";
1030         my $error = shift || "Malformed query, file missing or permission denied";
1032         git_header_html($status);
1033         print "<div class=\"page_body\">\n" .
1034               "<br/><br/>\n" .
1035               "$status - $error\n" .
1036               "<br/>\n" .
1037               "</div>\n";
1038         git_footer_html();
1039         exit;
1042 ## ----------------------------------------------------------------------
1043 ## functions printing or outputting HTML: navigation
1045 sub git_print_page_nav {
1046         my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1047         $extra = '' if !defined $extra; # pager or formats
1049         my @navs = qw(summary shortlog log commit commitdiff tree);
1050         if ($suppress) {
1051                 @navs = grep { $_ ne $suppress } @navs;
1052         }
1054         my %arg = map { $_, ''} @navs;
1055         if (defined $head) {
1056                 for (qw(commit commitdiff)) {
1057                         $arg{$_} = ";h=$head";
1058                 }
1059                 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1060                         for (qw(shortlog log)) {
1061                                 $arg{$_} = ";h=$head";
1062                         }
1063                 }
1064         }
1065         $arg{tree} .= ";h=$treehead" if defined $treehead;
1066         $arg{tree} .= ";hb=$treebase" if defined $treebase;
1068         print "<div class=\"page_nav\">\n" .
1069                 (join " | ",
1070                  map { $_ eq $current
1071                                          ? $_
1072                                          : $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$_$arg{$_}")}, "$_")
1073                                  }
1074                  @navs);
1075         print "<br/>\n$extra<br/>\n" .
1076               "</div>\n";
1079 sub format_paging_nav {
1080         my ($action, $hash, $head, $page, $nrevs) = @_;
1081         my $paging_nav;
1084         if ($hash ne $head || $page) {
1085                 $paging_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action")}, "HEAD");
1086         } else {
1087                 $paging_nav .= "HEAD";
1088         }
1090         if ($page > 0) {
1091                 $paging_nav .= " &sdot; " .
1092                         $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page-1)),
1093                                  -accesskey => "p", -title => "Alt-p"}, "prev");
1094         } else {
1095                 $paging_nav .= " &sdot; prev";
1096         }
1098         if ($nrevs >= (100 * ($page+1)-1)) {
1099                 $paging_nav .= " &sdot; " .
1100                         $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action;h=$hash;pg=" . ($page+1)),
1101                                  -accesskey => "n", -title => "Alt-n"}, "next");
1102         } else {
1103                 $paging_nav .= " &sdot; next";
1104         }
1106         return $paging_nav;
1109 ## ......................................................................
1110 ## functions printing or outputting HTML: div
1112 sub git_print_header_div {
1113         my ($action, $title, $hash, $hash_base) = @_;
1114         my $rest = '';
1116         $rest .= ";h=$hash" if $hash;
1117         $rest .= ";hb=$hash_base" if $hash_base;
1119         print "<div class=\"header\">\n" .
1120               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$action$rest"),
1121                        -class => "title"}, $title ? $title : $action) . "\n" .
1122               "</div>\n";
1125 sub git_print_page_path {
1126         my $name = shift;
1127         my $type = shift;
1129         if (!defined $name) {
1130                 print "<div class=\"page_path\"><b>/</b></div>\n";
1131         } elsif (defined $type && $type eq 'blob') {
1132                 print "<div class=\"page_path\"><b>" .
1133                         $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "</b><br/></div>\n";
1134         } else {
1135                 print "<div class=\"page_path\"><b>" . esc_html($name) . "</b><br/></div>\n";
1136         }
1139 ## ......................................................................
1140 ## functions printing large fragments of HTML
1142 sub git_difftree_body {
1143         my ($difftree, $parent) = @_;
1145         print "<div class=\"list_head\">\n";
1146         if ($#{$difftree} > 10) {
1147                 print(($#{$difftree} + 1) . " files changed:\n");
1148         }
1149         print "</div>\n";
1151         print "<table class=\"diff_tree\">\n";
1152         my $alternate = 0;
1153         foreach my $line (@{$difftree}) {
1154                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
1155                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
1156                 if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1157                         next;
1158                 }
1159                 my $from_mode = $1;
1160                 my $to_mode = $2;
1161                 my $from_id = $3;
1162                 my $to_id = $4;
1163                 my $status = $5;
1164                 my $similarity = $6; # score
1165                 my $file = validate_input(unquote($7));
1167                 if ($alternate) {
1168                         print "<tr class=\"dark\">\n";
1169                 } else {
1170                         print "<tr class=\"light\">\n";
1171                 }
1172                 $alternate ^= 1;
1174                 if ($status eq "A") { # created
1175                         my $mode_chng = "";
1176                         if (S_ISREG(oct $to_mode)) {
1177                                 $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
1178                         }
1179                         print "<td>" .
1180                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"),
1181                                       -class => "list"}, esc_html($file)) .
1182                               "</td>\n" .
1183                               "<td><span class=\"file_status new\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
1184                               "<td class=\"link\">" .
1185                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") .
1186                               "</td>\n";
1188                 } elsif ($status eq "D") { # deleted
1189                         print "<td>" .
1190                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$parent;f=$file"),
1191                                        -class => "list"}, esc_html($file)) . "</td>\n" .
1192                               "<td><span class=\"file_status deleted\">[deleted " . file_type($from_mode). "]</span></td>\n" .
1193                               "<td class=\"link\">" .
1194                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$parent;f=$file")}, "blob") . " | " .
1195                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$parent;f=$file")}, "history") .
1196                               "</td>\n"
1198                 } elsif ($status eq "M" || $status eq "T") { # modified, or type changed
1199                         my $mode_chnge = "";
1200                         if ($from_mode != $to_mode) {
1201                                 $mode_chnge = " <span class=\"file_status mode_chnge\">[changed";
1202                                 if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
1203                                         $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
1204                                 }
1205                                 if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
1206                                         if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
1207                                                 $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
1208                                         } elsif (S_ISREG($to_mode)) {
1209                                                 $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
1210                                         }
1211                                 }
1212                                 $mode_chnge .= "]</span>\n";
1213                         }
1214                         print "<td>";
1215                         if ($to_id ne $from_id) { # modified
1216                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"),
1217                                               -class => "list"}, esc_html($file));
1218                         } else { # mode changed
1219                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"),
1220                                               -class => "list"}, esc_html($file));
1221                         }
1222                         print "</td>\n" .
1223                               "<td>$mode_chnge</td>\n" .
1224                               "<td class=\"link\">" .
1225                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob");
1226                         if ($to_id ne $from_id) { # modified
1227                                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff");
1228                         }
1229                         print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") . "\n";
1230                         print "</td>\n";
1232                 } elsif ($status eq "R") { # renamed
1233                         my ($from_file, $to_file) = split "\t", $file;
1234                         my $mode_chng = "";
1235                         if ($from_mode != $to_mode) {
1236                                 $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
1237                         }
1238                         print "<td>" .
1239                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file"),
1240                                       -class => "list"}, esc_html($to_file)) . "</td>\n" .
1241                               "<td><span class=\"file_status moved\">[moved from " .
1242                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$parent;f=$from_file"),
1243                                       -class => "list"}, esc_html($from_file)) .
1244                               " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
1245                               "<td class=\"link\">" .
1246                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
1247                         if ($to_id ne $from_id) {
1248                                 print " | " .
1249                                       $cgi->a({-href => "$my_uri?" .
1250                                               esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file;fp=$from_file")}, "diff");
1251                         }
1252                         print "</td>\n";
1254                 } elsif ($status eq "C") { # copied
1255                         my ($from_file, $to_file) = split "\t", $file;
1256                         my $mode_chng = "";
1257                         if ($from_mode != $to_mode) {
1258                                 $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
1259                         }
1260                         print "<td>" .
1261                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file"),
1262                                       -class => "list"}, esc_html($to_file)) . "</td>\n" .
1263                               "<td><span class=\"file_status copied\">[copied from " .
1264                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$parent;f=$from_file"),
1265                                       -class => "list"}, esc_html($from_file)) .
1266                               " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
1267                               "<td class=\"link\">" .
1268                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
1269                         if ($to_id ne $from_id) {
1270                                 print " | " .
1271                                       $cgi->a({-href => "$my_uri?" .
1272                                               esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file;fp=$from_file")}, "diff");
1273                         }
1274                         print "</td>\n";
1275                 } # we should not encounter Unmerged (U) or Unknown (X) status
1276                 print "</tr>\n";
1277         }
1278         print "</table>\n";
1281 sub git_shortlog_body {
1282         # uses global variable $project
1283         my ($revlist, $from, $to, $refs, $extra) = @_;
1284         $from = 0 unless defined $from;
1285         $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1287         print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1288         my $alternate = 0;
1289         for (my $i = $from; $i <= $to; $i++) {
1290                 my $commit = $revlist->[$i];
1291                 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1292                 my $ref = format_ref_marker($refs, $commit);
1293                 my %co = parse_commit($commit);
1294                 if ($alternate) {
1295                         print "<tr class=\"dark\">\n";
1296                 } else {
1297                         print "<tr class=\"light\">\n";
1298                 }
1299                 $alternate ^= 1;
1300                 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1301                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1302                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1303                       "<td>";
1304                 print format_subject_html($co{'title'}, $co{'title_short'}, "p=$project;a=commit;h=$commit", $ref);
1305                 print "</td>\n" .
1306                       "<td class=\"link\">" .
1307                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") . " | " .
1308                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1309                       "</td>\n" .
1310                       "</tr>\n";
1311         }
1312         if (defined $extra) {
1313                 print "<tr>\n" .
1314                       "<td colspan=\"4\">$extra</td>\n" .
1315                       "</tr>\n";
1316         }
1317         print "</table>\n";
1320 sub git_history_body {
1321         # Warning: assumes constant type (blob or tree) during history
1322         my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1324         print "<table class=\"history\" cellspacing=\"0\">\n";
1325         my $alternate = 0;
1326         while (my $line = <$fd>) {
1327                 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1328                         next;
1329                 }
1331                 my $commit = $1;
1332                 my %co = parse_commit($commit);
1333                 if (!%co) {
1334                         next;
1335                 }
1337                 my $ref = format_ref_marker($refs, $commit);
1339                 if ($alternate) {
1340                         print "<tr class=\"dark\">\n";
1341                 } else {
1342                         print "<tr class=\"light\">\n";
1343                 }
1344                 $alternate ^= 1;
1345                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1346                       # shortlog uses      chop_str($co{'author_name'}, 10)
1347                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1348                       "<td>";
1349                 # originally git_history used chop_str($co{'title'}, 50)
1350                 print format_subject_html($co{'title'}, $co{'title_short'}, "p=$project;a=commit;h=$commit", $ref);
1351                 print "</td>\n" .
1352                       "<td class=\"link\">" .
1353                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") . " | " .
1354                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") . " | " .
1355                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$ftype;hb=$commit;f=$file_name")}, $ftype);
1357                 if ($ftype eq 'blob') {
1358                         my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1359                         my $blob_parent  = git_get_hash_by_path($commit, $file_name);
1360                         if (defined $blob_current && defined $blob_parent &&
1361                                         $blob_current ne $blob_parent) {
1362                                 print " | " .
1363                                         $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob_current;hp=$blob_parent;hb=$commit;f=$file_name")},
1364                                                 "diff to current");
1365                         }
1366                 }
1367                 print "</td>\n" .
1368                       "</tr>\n";
1369         }
1370         if (defined $extra) {
1371                 print "<tr>\n" .
1372                       "<td colspan=\"4\">$extra</td>\n" .
1373                       "</tr>\n";
1374         }
1375         print "</table>\n";
1378 sub git_tags_body {
1379         # uses global variable $project
1380         my ($taglist, $from, $to, $extra) = @_;
1381         $from = 0 unless defined $from;
1382         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1384         print "<table class=\"tags\" cellspacing=\"0\">\n";
1385         my $alternate = 0;
1386         for (my $i = $from; $i <= $to; $i++) {
1387                 my $entry = $taglist->[$i];
1388                 my %tag = %$entry;
1389                 my $comment_lines = $tag{'comment'};
1390                 my $comment = shift @$comment_lines;
1391                 my $comment_short;
1392                 if (defined $comment) {
1393                         $comment_short = chop_str($comment, 30, 5);
1394                 }
1395                 if ($alternate) {
1396                         print "<tr class=\"dark\">\n";
1397                 } else {
1398                         print "<tr class=\"light\">\n";
1399                 }
1400                 $alternate ^= 1;
1401                 print "<td><i>$tag{'age'}</i></td>\n" .
1402                       "<td>" .
1403                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"),
1404                                -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1405                       "</td>\n" .
1406                       "<td>";
1407                 if (defined $comment) {
1408                         print format_subject_html($comment, $comment_short, "p=$project;a=tag;h=$tag{'id'}");
1409                 }
1410                 print "</td>\n" .
1411                       "<td class=\"selflink\">";
1412                 if ($tag{'type'} eq "tag") {
1413                         print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag");
1414                 } else {
1415                         print "&nbsp;";
1416                 }
1417                 print "</td>\n" .
1418                       "<td class=\"link\">" . " | " .
1419                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1420                 if ($tag{'reftype'} eq "commit") {
1421                         print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1422                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1423                 } elsif ($tag{'reftype'} eq "blob") {
1424                         print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$tag{'refid'}")}, "raw");
1425                 }
1426                 print "</td>\n" .
1427                       "</tr>";
1428         }
1429         if (defined $extra) {
1430                 print "<tr>\n" .
1431                       "<td colspan=\"5\">$extra</td>\n" .
1432                       "</tr>\n";
1433         }
1434         print "</table>\n";
1437 sub git_heads_body {
1438         # uses global variable $project
1439         my ($taglist, $head, $from, $to, $extra) = @_;
1440         $from = 0 unless defined $from;
1441         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1443         print "<table class=\"heads\" cellspacing=\"0\">\n";
1444         my $alternate = 0;
1445         for (my $i = $from; $i <= $to; $i++) {
1446                 my $entry = $taglist->[$i];
1447                 my %tag = %$entry;
1448                 my $curr = $tag{'id'} eq $head;
1449                 if ($alternate) {
1450                         print "<tr class=\"dark\">\n";
1451                 } else {
1452                         print "<tr class=\"light\">\n";
1453                 }
1454                 $alternate ^= 1;
1455                 print "<td><i>$tag{'age'}</i></td>\n" .
1456                       ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1457                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"),
1458                                -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1459                       "</td>\n" .
1460                       "<td class=\"link\">" .
1461                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") . " | " .
1462                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1463                       "</td>\n" .
1464                       "</tr>";
1465         }
1466         if (defined $extra) {
1467                 print "<tr>\n" .
1468                       "<td colspan=\"3\">$extra</td>\n" .
1469                       "</tr>\n";
1470         }
1471         print "</table>\n";
1474 ## ----------------------------------------------------------------------
1475 ## functions printing large fragments, format as one of arguments
1477 sub git_diff_print {
1478         my $from = shift;
1479         my $from_name = shift;
1480         my $to = shift;
1481         my $to_name = shift;
1482         my $format = shift || "html";
1484         my $from_tmp = "/dev/null";
1485         my $to_tmp = "/dev/null";
1486         my $pid = $$;
1488         # create tmp from-file
1489         if (defined $from) {
1490                 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1491                 open my $fd2, "> $from_tmp";
1492                 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1493                 my @file = <$fd>;
1494                 print $fd2 @file;
1495                 close $fd2;
1496                 close $fd;
1497         }
1499         # create tmp to-file
1500         if (defined $to) {
1501                 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1502                 open my $fd2, "> $to_tmp";
1503                 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1504                 my @file = <$fd>;
1505                 print $fd2 @file;
1506                 close $fd2;
1507                 close $fd;
1508         }
1510         open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1511         if ($format eq "plain") {
1512                 undef $/;
1513                 print <$fd>;
1514                 $/ = "\n";
1515         } else {
1516                 while (my $line = <$fd>) {
1517                         chomp $line;
1518                         my $char = substr($line, 0, 1);
1519                         my $diff_class = "";
1520                         if ($char eq '+') {
1521                                 $diff_class = " add";
1522                         } elsif ($char eq "-") {
1523                                 $diff_class = " rem";
1524                         } elsif ($char eq "@") {
1525                                 $diff_class = " chunk_header";
1526                         } elsif ($char eq "\\") {
1527                                 # skip errors
1528                                 next;
1529                         }
1530                         $line = untabify($line);
1531                         print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1532                 }
1533         }
1534         close $fd;
1536         if (defined $from) {
1537                 unlink($from_tmp);
1538         }
1539         if (defined $to) {
1540                 unlink($to_tmp);
1541         }
1545 ## ======================================================================
1546 ## ======================================================================
1547 ## actions
1549 sub git_project_list {
1550         my $order = $cgi->param('o');
1551         if (defined $order && $order !~ m/project|descr|owner|age/) {
1552                 die_error(undef, "Unknown order parameter");
1553         }
1555         my @list = git_get_projects_list();
1556         my @projects;
1557         if (!@list) {
1558                 die_error(undef, "No projects found");
1559         }
1560         foreach my $pr (@list) {
1561                 my $head = git_get_head_hash($pr->{'path'});
1562                 if (!defined $head) {
1563                         next;
1564                 }
1565                 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1566                 my %co = parse_commit($head);
1567                 if (!%co) {
1568                         next;
1569                 }
1570                 $pr->{'commit'} = \%co;
1571                 if (!defined $pr->{'descr'}) {
1572                         my $descr = git_get_project_description($pr->{'path'}) || "";
1573                         $pr->{'descr'} = chop_str($descr, 25, 5);
1574                 }
1575                 if (!defined $pr->{'owner'}) {
1576                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1577                 }
1578                 push @projects, $pr;
1579         }
1581         git_header_html();
1582         if (-f $home_text) {
1583                 print "<div class=\"index_include\">\n";
1584                 open (my $fd, $home_text);
1585                 print <$fd>;
1586                 close $fd;
1587                 print "</div>\n";
1588         }
1589         print "<table class=\"project_list\">\n" .
1590               "<tr>\n";
1591         $order ||= "project";
1592         if ($order eq "project") {
1593                 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1594                 print "<th>Project</th>\n";
1595         } else {
1596                 print "<th>" .
1597                       $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
1598                                -class => "header"}, "Project") .
1599                       "</th>\n";
1600         }
1601         if ($order eq "descr") {
1602                 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1603                 print "<th>Description</th>\n";
1604         } else {
1605                 print "<th>" .
1606                       $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
1607                                -class => "header"}, "Description") .
1608                       "</th>\n";
1609         }
1610         if ($order eq "owner") {
1611                 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1612                 print "<th>Owner</th>\n";
1613         } else {
1614                 print "<th>" .
1615                       $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
1616                                -class => "header"}, "Owner") .
1617                       "</th>\n";
1618         }
1619         if ($order eq "age") {
1620                 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
1621                 print "<th>Last Change</th>\n";
1622         } else {
1623                 print "<th>" .
1624                       $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
1625                                -class => "header"}, "Last Change") .
1626                       "</th>\n";
1627         }
1628         print "<th></th>\n" .
1629               "</tr>\n";
1630         my $alternate = 0;
1631         foreach my $pr (@projects) {
1632                 if ($alternate) {
1633                         print "<tr class=\"dark\">\n";
1634                 } else {
1635                         print "<tr class=\"light\">\n";
1636                 }
1637                 $alternate ^= 1;
1638                 print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"),
1639                                         -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
1640                       "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
1641                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
1642                 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
1643                       $pr->{'commit'}{'age_string'} . "</td>\n" .
1644                       "<td class=\"link\">" .
1645                       $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary")   . " | " .
1646                       $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") . " | " .
1647                       $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
1648                       "</td>\n" .
1649                       "</tr>\n";
1650         }
1651         print "</table>\n";
1652         git_footer_html();
1655 sub git_summary {
1656         my $descr = git_get_project_description($project) || "none";
1657         my $head = git_get_head_hash($project);
1658         my %co = parse_commit($head);
1659         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
1661         my $owner = git_get_project_owner($project);
1663         my $refs = git_get_references();
1664         git_header_html();
1665         git_print_page_nav('summary','', $head);
1667         print "<div class=\"title\">&nbsp;</div>\n";
1668         print "<table cellspacing=\"0\">\n" .
1669               "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1670               "<tr><td>owner</td><td>$owner</td></tr>\n" .
1671               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
1672               "</table>\n";
1674         open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
1675                 or die_error(undef, "Open git-rev-list failed");
1676         my @revlist = map { chomp; $_ } <$fd>;
1677         close $fd;
1678         git_print_header_div('shortlog');
1679         git_shortlog_body(\@revlist, 0, 15, $refs,
1680                           $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "..."));
1682         my $taglist = git_get_refs_list("refs/tags");
1683         if (defined @$taglist) {
1684                 git_print_header_div('tags');
1685                 git_tags_body($taglist, 0, 15,
1686                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "..."));
1687         }
1689         my $headlist = git_get_refs_list("refs/heads");
1690         if (defined @$headlist) {
1691                 git_print_header_div('heads');
1692                 git_heads_body($headlist, $head, 0, 15,
1693                                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "..."));
1694         }
1696         git_footer_html();
1699 sub git_tag {
1700         my $head = git_get_head_hash($project);
1701         git_header_html();
1702         git_print_page_nav('','', $head,undef,$head);
1703         my %tag = parse_tag($hash);
1704         git_print_header_div('commit', esc_html($tag{'name'}), $hash);
1705         print "<div class=\"title_text\">\n" .
1706               "<table cellspacing=\"0\">\n" .
1707               "<tr>\n" .
1708               "<td>object</td>\n" .
1709               "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
1710               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
1711               "</tr>\n";
1712         if (defined($tag{'author'})) {
1713                 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
1714                 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1715                 print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1716         }
1717         print "</table>\n\n" .
1718               "</div>\n";
1719         print "<div class=\"page_body\">";
1720         my $comment = $tag{'comment'};
1721         foreach my $line (@$comment) {
1722                 print esc_html($line) . "<br/>\n";
1723         }
1724         print "</div>\n";
1725         git_footer_html();
1728 sub git_blame2 {
1729         my $fd;
1730         my $ftype;
1731         die_error(undef, "Permission denied") if (!git_get_project_config_bool ('blame'));
1732         die_error('404 Not Found', "File name not defined") if (!$file_name);
1733         $hash_base ||= git_get_head_hash($project);
1734         die_error(undef, "Couldn't find base commit") unless ($hash_base);
1735         my %co = parse_commit($hash_base)
1736                 or die_error(undef, "Reading commit failed");
1737         if (!defined $hash) {
1738                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1739                         or die_error(undef, "Error looking up file");
1740         }
1741         $ftype = git_get_type($hash);
1742         if ($ftype !~ "blob") {
1743                 die_error("400 Bad Request", "Object is not a blob");
1744         }
1745         open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
1746                 or die_error(undef, "Open git-blame failed");
1747         git_header_html();
1748         my $formats_nav =
1749                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1750                 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1751         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1752         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
1753         git_print_page_path($file_name, $ftype);
1754         my @rev_color = (qw(light2 dark2));
1755         my $num_colors = scalar(@rev_color);
1756         my $current_color = 0;
1757         my $last_rev;
1758         print "<div class=\"page_body\">\n";
1759         print "<table class=\"blame\">\n";
1760         print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
1761         while (<$fd>) {
1762                 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
1763                 my $full_rev = $1;
1764                 my $rev = substr($full_rev, 0, 8);
1765                 my $lineno = $2;
1766                 my $data = $3;
1768                 if (!defined $last_rev) {
1769                         $last_rev = $full_rev;
1770                 } elsif ($last_rev ne $full_rev) {
1771                         $last_rev = $full_rev;
1772                         $current_color = ++$current_color % $num_colors;
1773                 }
1774                 print "<tr class=\"$rev_color[$current_color]\">\n";
1775                 print "<td class=\"sha1\">" .
1776                         $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$full_rev;f=$file_name")}, esc_html($rev)) . "</td>\n";
1777                 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" . esc_html($lineno) . "</a></td>\n";
1778                 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
1779                 print "</tr>\n";
1780         }
1781         print "</table>\n";
1782         print "</div>";
1783         close $fd or print "Reading blob failed\n";
1784         git_footer_html();
1787 sub git_blame {
1788         my $fd;
1789         die_error('403 Permission denied', "Permission denied") if (!git_get_project_config_bool ('blame'));
1790         die_error('404 Not Found', "File name not defined") if (!$file_name);
1791         $hash_base ||= git_get_head_hash($project);
1792         die_error(undef, "Couldn't find base commit") unless ($hash_base);
1793         my %co = parse_commit($hash_base)
1794                 or die_error(undef, "Reading commit failed");
1795         if (!defined $hash) {
1796                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1797                         or die_error(undef, "Error lookup file");
1798         }
1799         open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
1800                 or die_error(undef, "Open git-annotate failed");
1801         git_header_html();
1802         my $formats_nav =
1803                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1804                 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1805         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1806         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
1807         git_print_page_path($file_name, 'blob');
1808         print "<div class=\"page_body\">\n";
1809         print <<HTML;
1810 <table class="blame">
1811   <tr>
1812     <th>Commit</th>
1813     <th>Age</th>
1814     <th>Author</th>
1815     <th>Line</th>
1816     <th>Data</th>
1817   </tr>
1818 HTML
1819         my @line_class = (qw(light dark));
1820         my $line_class_len = scalar (@line_class);
1821         my $line_class_num = $#line_class;
1822         while (my $line = <$fd>) {
1823                 my $long_rev;
1824                 my $short_rev;
1825                 my $author;
1826                 my $time;
1827                 my $lineno;
1828                 my $data;
1829                 my $age;
1830                 my $age_str;
1831                 my $age_class;
1833                 chomp $line;
1834                 $line_class_num = ($line_class_num + 1) % $line_class_len;
1836                 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
1837                         $long_rev = $1;
1838                         $author   = $2;
1839                         $time     = $3;
1840                         $lineno   = $4;
1841                         $data     = $5;
1842                 } else {
1843                         print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
1844                         next;
1845                 }
1846                 $short_rev  = substr ($long_rev, 0, 8);
1847                 $age        = time () - $time;
1848                 $age_str    = age_string ($age);
1849                 $age_str    =~ s/ /&nbsp;/g;
1850                 $age_class  = age_class($age);
1851                 $author     = esc_html ($author);
1852                 $author     =~ s/ /&nbsp;/g;
1854                 $data = untabify($data);
1855                 $data = esc_html ($data);
1857                 print <<HTML;
1858   <tr class="$line_class[$line_class_num]">
1859     <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1860     <td class="$age_class">$age_str</td>
1861     <td>$author</td>
1862     <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1863     <td class="pre">$data</td>
1864   </tr>
1865 HTML
1866         } # while (my $line = <$fd>)
1867         print "</table>\n\n";
1868         close $fd or print "Reading blob failed.\n";
1869         print "</div>";
1870         git_footer_html();
1873 sub git_tags {
1874         my $head = git_get_head_hash($project);
1875         git_header_html();
1876         git_print_page_nav('','', $head,undef,$head);
1877         git_print_header_div('summary', $project);
1879         my $taglist = git_get_refs_list("refs/tags");
1880         if (defined @$taglist) {
1881                 git_tags_body($taglist);
1882         }
1883         git_footer_html();
1886 sub git_heads {
1887         my $head = git_get_head_hash($project);
1888         git_header_html();
1889         git_print_page_nav('','', $head,undef,$head);
1890         git_print_header_div('summary', $project);
1892         my $taglist = git_get_refs_list("refs/heads");
1893         if (defined @$taglist) {
1894                 git_heads_body($taglist, $head);
1895         }
1896         git_footer_html();
1899 sub git_blob_plain {
1900         if (!defined $hash) {
1901                 if (defined $file_name) {
1902                         my $base = $hash_base || git_get_head_hash($project);
1903                         $hash = git_get_hash_by_path($base, $file_name, "blob")
1904                                 or die_error(undef, "Error lookup file");
1905                 } else {
1906                         die_error(undef, "No file name defined");
1907                 }
1908         }
1909         my $type = shift;
1910         open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1911                 or die_error(undef, "Couldn't cat $file_name, $hash");
1913         $type ||= blob_mimetype($fd, $file_name);
1915         # save as filename, even when no $file_name is given
1916         my $save_as = "$hash";
1917         if (defined $file_name) {
1918                 $save_as = $file_name;
1919         } elsif ($type =~ m/^text\//) {
1920                 $save_as .= '.txt';
1921         }
1923         print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
1924         undef $/;
1925         binmode STDOUT, ':raw';
1926         print <$fd>;
1927         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
1928         $/ = "\n";
1929         close $fd;
1932 sub git_blob {
1933         if (!defined $hash) {
1934                 if (defined $file_name) {
1935                         my $base = $hash_base || git_get_head_hash($project);
1936                         $hash = git_get_hash_by_path($base, $file_name, "blob")
1937                                 or die_error(undef, "Error lookup file");
1938                 } else {
1939                         die_error(undef, "No file name defined");
1940                 }
1941         }
1942         my $have_blame = git_get_project_config_bool ('blame');
1943         open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1944                 or die_error(undef, "Couldn't cat $file_name, $hash");
1945         my $mimetype = blob_mimetype($fd, $file_name);
1946         if ($mimetype !~ m/^text\//) {
1947                 close $fd;
1948                 return git_blob_plain($mimetype);
1949         }
1950         git_header_html();
1951         my $formats_nav = '';
1952         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
1953                 if (defined $file_name) {
1954                         if ($have_blame) {
1955                                 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
1956                         }
1957                         $formats_nav .=
1958                                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1959                                 " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head");
1960                 } else {
1961                         $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain");
1962                 }
1963                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1964                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
1965         } else {
1966                 print "<div class=\"page_nav\">\n" .
1967                       "<br/><br/></div>\n" .
1968                       "<div class=\"title\">$hash</div>\n";
1969         }
1970         git_print_page_path($file_name, "blob");
1971         print "<div class=\"page_body\">\n";
1972         my $nr;
1973         while (my $line = <$fd>) {
1974                 chomp $line;
1975                 $nr++;
1976                 $line = untabify($line);
1977                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1978         }
1979         close $fd or print "Reading blob failed.\n";
1980         print "</div>";
1981         git_footer_html();
1984 sub git_tree {
1985         if (!defined $hash) {
1986                 $hash = git_get_head_hash($project);
1987                 if (defined $file_name) {
1988                         my $base = $hash_base || $hash;
1989                         $hash = git_get_hash_by_path($base, $file_name, "tree");
1990                 }
1991                 if (!defined $hash_base) {
1992                         $hash_base = $hash;
1993                 }
1994         }
1995         $/ = "\0";
1996         open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
1997                 or die_error(undef, "Open git-ls-tree failed");
1998         my @entries = map { chomp; $_ } <$fd>;
1999         close $fd or die_error(undef, "Reading tree failed");
2000         $/ = "\n";
2002         my $refs = git_get_references();
2003         my $ref = format_ref_marker($refs, $hash_base);
2004         git_header_html();
2005         my $base_key = "";
2006         my $base = "";
2007         my $have_blame = git_get_project_config_bool ('blame');
2008         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2009                 $base_key = ";hb=$hash_base";
2010                 git_print_page_nav('tree','', $hash_base);
2011                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2012         } else {
2013                 print "<div class=\"page_nav\">\n";
2014                 print "<br/><br/></div>\n";
2015                 print "<div class=\"title\">$hash</div>\n";
2016         }
2017         if (defined $file_name) {
2018                 $base = esc_html("$file_name/");
2019         }
2020         git_print_page_path($file_name, 'tree');
2021         print "<div class=\"page_body\">\n";
2022         print "<table cellspacing=\"0\">\n";
2023         my $alternate = 0;
2024         foreach my $line (@entries) {
2025                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
2026                 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2027                 my $t_mode = $1;
2028                 my $t_type = $2;
2029                 my $t_hash = $3;
2030                 my $t_name = validate_input($4);
2031                 if ($alternate) {
2032                         print "<tr class=\"dark\">\n";
2033                 } else {
2034                         print "<tr class=\"light\">\n";
2035                 }
2036                 $alternate ^= 1;
2037                 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2038                 if ($t_type eq "blob") {
2039                         print "<td class=\"list\">" .
2040                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name"), -class => "list"}, esc_html($t_name)) .
2041                               "</td>\n" .
2042                               "<td class=\"link\">" .
2043                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob");
2044                         if ($have_blame) {
2045                                 print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame");
2046                         }
2047                         print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$t_hash;hb=$hash_base;f=$base$t_name")}, "history") .
2048                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") .
2049                               "</td>\n";
2050                 } elsif ($t_type eq "tree") {
2051                         print "<td class=\"list\">" .
2052                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
2053                               "</td>\n" .
2054                               "<td class=\"link\">" .
2055                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
2056                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") .
2057                               "</td>\n";
2058                 }
2059                 print "</tr>\n";
2060         }
2061         print "</table>\n" .
2062               "</div>";
2063         git_footer_html();
2066 sub git_log {
2067         my $head = git_get_head_hash($project);
2068         if (!defined $hash) {
2069                 $hash = $head;
2070         }
2071         if (!defined $page) {
2072                 $page = 0;
2073         }
2074         my $refs = git_get_references();
2076         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2077         open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2078                 or die_error(undef, "Open git-rev-list failed");
2079         my @revlist = map { chomp; $_ } <$fd>;
2080         close $fd;
2082         my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2084         git_header_html();
2085         git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2087         if (!@revlist) {
2088                 my %co = parse_commit($hash);
2090                 git_print_header_div('summary', $project);
2091                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2092         }
2093         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2094                 my $commit = $revlist[$i];
2095                 my $ref = format_ref_marker($refs, $commit);
2096                 my %co = parse_commit($commit);
2097                 next if !%co;
2098                 my %ad = parse_date($co{'author_epoch'});
2099                 git_print_header_div('commit',
2100                                "<span class=\"age\">$co{'age_string'}</span>" .
2101                                esc_html($co{'title'}) . $ref,
2102                                $commit);
2103                 print "<div class=\"title_text\">\n" .
2104                       "<div class=\"log_link\">\n" .
2105                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2106                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2107                       "<br/>\n" .
2108                       "</div>\n" .
2109                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
2110                       "</div>\n" .
2111                       "<div class=\"log_body\">\n";
2112                 my $comment = $co{'comment'};
2113                 my $empty = 0;
2114                 foreach my $line (@$comment) {
2115                         if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2116                                 next;
2117                         }
2118                         if ($line eq "") {
2119                                 if ($empty) {
2120                                         next;
2121                                 }
2122                                 $empty = 1;
2123                         } else {
2124                                 $empty = 0;
2125                         }
2126                         print format_log_line_html($line) . "<br/>\n";
2127                 }
2128                 if (!$empty) {
2129                         print "<br/>\n";
2130                 }
2131                 print "</div>\n";
2132         }
2133         git_footer_html();
2136 sub git_commit {
2137         my %co = parse_commit($hash);
2138         if (!%co) {
2139                 die_error(undef, "Unknown commit object");
2140         }
2141         my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2142         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2144         my $parent = $co{'parent'};
2145         if (!defined $parent) {
2146                 $parent = "--root";
2147         }
2148         open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2149                 or die_error(undef, "Open git-diff-tree failed");
2150         my @difftree = map { chomp; $_ } <$fd>;
2151         close $fd or die_error(undef, "Reading git-diff-tree failed");
2153         # non-textual hash id's can be cached
2154         my $expires;
2155         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2156                 $expires = "+1d";
2157         }
2158         my $refs = git_get_references();
2159         my $ref = format_ref_marker($refs, $co{'id'});
2160         my $formats_nav = '';
2161         if (defined $file_name && defined $co{'parent'}) {
2162                 my $parent = $co{'parent'};
2163                 $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;hb=$parent;f=$file_name")}, "blame");
2164         }
2165         git_header_html(undef, $expires);
2166         git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2167                      $hash, $co{'tree'}, $hash,
2168                      $formats_nav);
2170         if (defined $co{'parent'}) {
2171                 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2172         } else {
2173                 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2174         }
2175         print "<div class=\"title_text\">\n" .
2176               "<table cellspacing=\"0\">\n";
2177         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2178               "<tr>" .
2179               "<td></td><td> $ad{'rfc2822'}";
2180         if ($ad{'hour_local'} < 6) {
2181                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2182         } else {
2183                 printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2184         }
2185         print "</td>" .
2186               "</tr>\n";
2187         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2188         print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
2189         print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2190         print "<tr>" .
2191               "<td>tree</td>" .
2192               "<td class=\"sha1\">" .
2193               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
2194               "</td>" .
2195               "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
2196               "</td>" .
2197               "</tr>\n";
2198         my $parents = $co{'parents'};
2199         foreach my $par (@$parents) {
2200                 print "<tr>" .
2201                       "<td>parent</td>" .
2202                       "<td class=\"sha1\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
2203                       "<td class=\"link\">" .
2204                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
2205                       " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
2206                       "</td>" .
2207                       "</tr>\n";
2208         }
2209         print "</table>".
2210               "</div>\n";
2211         print "<div class=\"page_body\">\n";
2212         my $comment = $co{'comment'};
2213         my $empty = 0;
2214         my $signed = 0;
2215         foreach my $line (@$comment) {
2216                 # print only one empty line
2217                 if ($line eq "") {
2218                         if ($empty || $signed) {
2219                                 next;
2220                         }
2221                         $empty = 1;
2222                 } else {
2223                         $empty = 0;
2224                 }
2225                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2226                         $signed = 1;
2227                         print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2228                 } else {
2229                         $signed = 0;
2230                         print format_log_line_html($line) . "<br/>\n";
2231                 }
2232         }
2233         print "</div>\n";
2235         git_difftree_body(\@difftree, $parent);
2237         git_footer_html();
2240 sub git_blobdiff {
2241         mkdir($git_temp, 0700);
2242         git_header_html();
2243         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2244                 my $formats_nav =
2245                         $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2246                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2247                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2248         } else {
2249                 print "<div class=\"page_nav\">\n" .
2250                       "<br/><br/></div>\n" .
2251                       "<div class=\"title\">$hash vs $hash_parent</div>\n";
2252         }
2253         git_print_page_path($file_name, "blob");
2254         print "<div class=\"page_body\">\n" .
2255               "<div class=\"diff_info\">blob:" .
2256               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
2257               " -> blob:" .
2258               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
2259               "</div>\n";
2260         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2261         print "</div>";
2262         git_footer_html();
2265 sub git_blobdiff_plain {
2266         mkdir($git_temp, 0700);
2267         print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2268         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2271 sub git_commitdiff {
2272         mkdir($git_temp, 0700);
2273         my %co = parse_commit($hash);
2274         if (!%co) {
2275                 die_error(undef, "Unknown commit object");
2276         }
2277         if (!defined $hash_parent) {
2278                 $hash_parent = $co{'parent'} || '--root';
2279         }
2280         open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2281                 or die_error(undef, "Open git-diff-tree failed");
2282         my @difftree = map { chomp; $_ } <$fd>;
2283         close $fd or die_error(undef, "Reading git-diff-tree failed");
2285         # non-textual hash id's can be cached
2286         my $expires;
2287         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2288                 $expires = "+1d";
2289         }
2290         my $refs = git_get_references();
2291         my $ref = format_ref_marker($refs, $co{'id'});
2292         my $formats_nav =
2293                 $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2294         git_header_html(undef, $expires);
2295         git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2296         git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2297         print "<div class=\"page_body\">\n";
2298         my $comment = $co{'comment'};
2299         my $empty = 0;
2300         my $signed = 0;
2301         my @log = @$comment;
2302         # remove first and empty lines after that
2303         shift @log;
2304         while (defined $log[0] && $log[0] eq "") {
2305                 shift @log;
2306         }
2307         foreach my $line (@log) {
2308                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2309                         next;
2310                 }
2311                 if ($line eq "") {
2312                         if ($empty) {
2313                                 next;
2314                         }
2315                         $empty = 1;
2316                 } else {
2317                         $empty = 0;
2318                 }
2319                 print format_log_line_html($line) . "<br/>\n";
2320         }
2321         print "<br/>\n";
2322         foreach my $line (@difftree) {
2323                 # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
2324                 # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
2325                 if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2326                         next;
2327                 }
2328                 my $from_mode = $1;
2329                 my $to_mode = $2;
2330                 my $from_id = $3;
2331                 my $to_id = $4;
2332                 my $status = $5;
2333                 my $file = validate_input(unquote($6));
2334                 if ($status eq "A") {
2335                         print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
2336                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
2337                               "</div>\n";
2338                         git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2339                 } elsif ($status eq "D") {
2340                         print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2341                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash_parent;f=$file")}, $from_id) . "(deleted)" .
2342                               "</div>\n";
2343                         git_diff_print($from_id, "a/$file", undef, "/dev/null");
2344                 } elsif ($status eq "M") {
2345                         if ($from_id ne $to_id) {
2346                                 print "<div class=\"diff_info\">" .
2347                                       file_type($from_mode) . ":" .
2348                                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash_parent;f=$file")}, $from_id) .
2349                                       " -> " .
2350                                       file_type($to_mode) . ":" .
2351                                       $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
2352                                 print "</div>\n";
2353                                 git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
2354                         }
2355                 }
2356         }
2357         print "<br/>\n" .
2358               "</div>";
2359         git_footer_html();
2362 sub git_commitdiff_plain {
2363         mkdir($git_temp, 0700);
2364         my %co = parse_commit($hash);
2365         if (!%co) {
2366                 die_error(undef, "Unknown commit object");
2367         }
2368         if (!defined $hash_parent) {
2369                 $hash_parent = $co{'parent'} || '--root';
2370         }
2371         open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2372                 or die_error(undef, "Open git-diff-tree failed");
2373         my @difftree = map { chomp; $_ } <$fd>;
2374         close $fd or die_error(undef, "Reading diff-tree failed");
2376         # try to figure out the next tag after this commit
2377         my $tagname;
2378         my $refs = git_get_references("tags");
2379         open $fd, "-|", $GIT, "rev-list", "HEAD";
2380         my @commits = map { chomp; $_ } <$fd>;
2381         close $fd;
2382         foreach my $commit (@commits) {
2383                 if (defined $refs->{$commit}) {
2384                         $tagname = $refs->{$commit}
2385                 }
2386                 if ($commit eq $hash) {
2387                         last;
2388                 }
2389         }
2391         print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
2392         my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2393         my $comment = $co{'comment'};
2394         print "From: $co{'author'}\n" .
2395               "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
2396               "Subject: $co{'title'}\n";
2397         if (defined $tagname) {
2398                 print "X-Git-Tag: $tagname\n";
2399         }
2400         print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2401               "\n";
2403         foreach my $line (@$comment) {;
2404                 print "$line\n";
2405         }
2406         print "---\n\n";
2408         foreach my $line (@difftree) {
2409                 if ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2410                         next;
2411                 }
2412                 my $from_id = $3;
2413                 my $to_id = $4;
2414                 my $status = $5;
2415                 my $file = $6;
2416                 if ($status eq "A") {
2417                         git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2418                 } elsif ($status eq "D") {
2419                         git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2420                 } elsif ($status eq "M") {
2421                         git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
2422                 }
2423         }
2426 sub git_history {
2427         if (!defined $hash_base) {
2428                 $hash_base = git_get_head_hash($project);
2429         }
2430         my $ftype;
2431         my %co = parse_commit($hash_base);
2432         if (!%co) {
2433                 die_error(undef, "Unknown commit object");
2434         }
2435         my $refs = git_get_references();
2436         git_header_html();
2437         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2438         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2439         if (!defined $hash && defined $file_name) {
2440                 $hash = git_get_hash_by_path($hash_base, $file_name);
2441         }
2442         if (defined $hash) {
2443                 $ftype = git_get_type($hash);
2444         }
2445         git_print_page_path($file_name, $ftype);
2447         open my $fd, "-|",
2448                 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2449         git_history_body($fd, $refs, $hash_base, $ftype);
2451         close $fd;
2452         git_footer_html();
2455 sub git_search {
2456         if (!defined $searchtext) {
2457                 die_error(undef, "Text field empty");
2458         }
2459         if (!defined $hash) {
2460                 $hash = git_get_head_hash($project);
2461         }
2462         my %co = parse_commit($hash);
2463         if (!%co) {
2464                 die_error(undef, "Unknown commit object");
2465         }
2466         # pickaxe may take all resources of your box and run for several minutes
2467         # with every query - so decide by yourself how public you make this feature :)
2468         my $commit_search = 1;
2469         my $author_search = 0;
2470         my $committer_search = 0;
2471         my $pickaxe_search = 0;
2472         if ($searchtext =~ s/^author\\://i) {
2473                 $author_search = 1;
2474         } elsif ($searchtext =~ s/^committer\\://i) {
2475                 $committer_search = 1;
2476         } elsif ($searchtext =~ s/^pickaxe\\://i) {
2477                 $commit_search = 0;
2478                 $pickaxe_search = 1;
2479         }
2480         git_header_html();
2481         git_print_page_nav('','', $hash,$co{'tree'},$hash);
2482         git_print_header_div('commit', esc_html($co{'title'}), $hash);
2484         print "<table cellspacing=\"0\">\n";
2485         my $alternate = 0;
2486         if ($commit_search) {
2487                 $/ = "\0";
2488                 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2489                 while (my $commit_text = <$fd>) {
2490                         if (!grep m/$searchtext/i, $commit_text) {
2491                                 next;
2492                         }
2493                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2494                                 next;
2495                         }
2496                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2497                                 next;
2498                         }
2499                         my @commit_lines = split "\n", $commit_text;
2500                         my %co = parse_commit(undef, \@commit_lines);
2501                         if (!%co) {
2502                                 next;
2503                         }
2504                         if ($alternate) {
2505                                 print "<tr class=\"dark\">\n";
2506                         } else {
2507                                 print "<tr class=\"light\">\n";
2508                         }
2509                         $alternate ^= 1;
2510                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2511                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2512                               "<td>" .
2513                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" . esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2514                         my $comment = $co{'comment'};
2515                         foreach my $line (@$comment) {
2516                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2517                                         my $lead = esc_html($1) || "";
2518                                         $lead = chop_str($lead, 30, 10);
2519                                         my $match = esc_html($2) || "";
2520                                         my $trail = esc_html($3) || "";
2521                                         $trail = chop_str($trail, 30, 10);
2522                                         my $text = "$lead<span class=\"match\">$match</span>$trail";
2523                                         print chop_str($text, 80, 5) . "<br/>\n";
2524                                 }
2525                         }
2526                         print "</td>\n" .
2527                               "<td class=\"link\">" .
2528                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2529                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2530                         print "</td>\n" .
2531                               "</tr>\n";
2532                 }
2533                 close $fd;
2534         }
2536         if ($pickaxe_search) {
2537                 $/ = "\n";
2538                 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2539                 undef %co;
2540                 my @files;
2541                 while (my $line = <$fd>) {
2542                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2543                                 my %set;
2544                                 $set{'file'} = $6;
2545                                 $set{'from_id'} = $3;
2546                                 $set{'to_id'} = $4;
2547                                 $set{'id'} = $set{'to_id'};
2548                                 if ($set{'id'} =~ m/0{40}/) {
2549                                         $set{'id'} = $set{'from_id'};
2550                                 }
2551                                 if ($set{'id'} =~ m/0{40}/) {
2552                                         next;
2553                                 }
2554                                 push @files, \%set;
2555                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2556                                 if (%co) {
2557                                         if ($alternate) {
2558                                                 print "<tr class=\"dark\">\n";
2559                                         } else {
2560                                                 print "<tr class=\"light\">\n";
2561                                         }
2562                                         $alternate ^= 1;
2563                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2564                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2565                                               "<td>" .
2566                                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
2567                                               esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2568                                         while (my $setref = shift @files) {
2569                                                 my %set = %$setref;
2570                                                 print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
2571                                                       "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2572                                                       "<br/>\n";
2573                                         }
2574                                         print "</td>\n" .
2575                                               "<td class=\"link\">" .
2576                                               $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2577                                               " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2578                                         print "</td>\n" .
2579                                               "</tr>\n";
2580                                 }
2581                                 %co = parse_commit($1);
2582                         }
2583                 }
2584                 close $fd;
2585         }
2586         print "</table>\n";
2587         git_footer_html();
2590 sub git_shortlog {
2591         my $head = git_get_head_hash($project);
2592         if (!defined $hash) {
2593                 $hash = $head;
2594         }
2595         if (!defined $page) {
2596                 $page = 0;
2597         }
2598         my $refs = git_get_references();
2600         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2601         open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2602                 or die_error(undef, "Open git-rev-list failed");
2603         my @revlist = map { chomp; $_ } <$fd>;
2604         close $fd;
2606         my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
2607         my $next_link = '';
2608         if ($#revlist >= (100 * ($page+1)-1)) {
2609                 $next_link =
2610                         $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)),
2611                                  -title => "Alt-n"}, "next");
2612         }
2615         git_header_html();
2616         git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
2617         git_print_header_div('summary', $project);
2619         git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
2621         git_footer_html();
2624 ## ......................................................................
2625 ## feeds (RSS, OPML)
2627 sub git_rss {
2628         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
2629         open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
2630                 or die_error(undef, "Open git-rev-list failed");
2631         my @revlist = map { chomp; $_ } <$fd>;
2632         close $fd or die_error(undef, "Reading git-rev-list failed");
2633         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2634         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2635               "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
2636         print "<channel>\n";
2637         print "<title>$project</title>\n".
2638               "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
2639               "<description>$project log</description>\n".
2640               "<language>en</language>\n";
2642         for (my $i = 0; $i <= $#revlist; $i++) {
2643                 my $commit = $revlist[$i];
2644                 my %co = parse_commit($commit);
2645                 # we read 150, we always show 30 and the ones more recent than 48 hours
2646                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
2647                         last;
2648                 }
2649                 my %cd = parse_date($co{'committer_epoch'});
2650                 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
2651                 my @difftree = map { chomp; $_ } <$fd>;
2652                 close $fd or next;
2653                 print "<item>\n" .
2654                       "<title>" .
2655                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
2656                       "</title>\n" .
2657                       "<author>" . esc_html($co{'author'}) . "</author>\n" .
2658                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
2659                       "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
2660                       "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
2661                       "<description>" . esc_html($co{'title'}) . "</description>\n" .
2662                       "<content:encoded>" .
2663                       "<![CDATA[\n";
2664                 my $comment = $co{'comment'};
2665                 foreach my $line (@$comment) {
2666                         $line = decode("utf8", $line, Encode::FB_DEFAULT);
2667                         print "$line<br/>\n";
2668                 }
2669                 print "<br/>\n";
2670                 foreach my $line (@difftree) {
2671                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2672                                 next;
2673                         }
2674                         my $file = validate_input(unquote($7));
2675                         $file = decode("utf8", $file, Encode::FB_DEFAULT);
2676                         print "$file<br/>\n";
2677                 }
2678                 print "]]>\n" .
2679                       "</content:encoded>\n" .
2680                       "</item>\n";
2681         }
2682         print "</channel></rss>";
2685 sub git_opml {
2686         my @list = git_get_projects_list();
2688         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
2689         print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
2690               "<opml version=\"1.0\">\n".
2691               "<head>".
2692               "  <title>$site_name Git OPML Export</title>\n".
2693               "</head>\n".
2694               "<body>\n".
2695               "<outline text=\"git RSS feeds\">\n";
2697         foreach my $pr (@list) {
2698                 my %proj = %$pr;
2699                 my $head = git_get_head_hash($proj{'path'});
2700                 if (!defined $head) {
2701                         next;
2702                 }
2703                 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
2704                 my %co = parse_commit($head);
2705                 if (!%co) {
2706                         next;
2707                 }
2709                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
2710                 my $rss  = "$my_url?p=$proj{'path'};a=rss";
2711                 my $html = "$my_url?p=$proj{'path'};a=summary";
2712                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
2713         }
2714         print "</outline>\n".
2715               "</body>\n".
2716               "</opml>\n";