Code

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