Code

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