Code

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