Code

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