Code

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