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