Code

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