Code

gitweb: Add git_get_{following,preceding}_references functions
[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 use File::Basename qw(basename);
19 binmode STDOUT, ':utf8';
21 our $cgi = new CGI;
22 our $version = "++GIT_VERSION++";
23 our $my_url = $cgi->url();
24 our $my_uri = $cgi->url(-absolute => 1);
26 # core git executable to use
27 # this can just be "git" if your webserver has a sensible PATH
28 our $GIT = "++GIT_BINDIR++/git";
30 # absolute fs-path which will be prepended to the project path
31 #our $projectroot = "/pub/scm";
32 our $projectroot = "++GITWEB_PROJECTROOT++";
34 # location for temporary files needed for diffs
35 our $git_temp = "/tmp/gitweb";
37 # target of the home link on top of all pages
38 our $home_link = $my_uri || "/";
40 # string of the home link on top of all pages
41 our $home_link_str = "++GITWEB_HOME_LINK_STR++";
43 # name of your site or organization to appear in page titles
44 # replace this with something more descriptive for clearer bookmarks
45 our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
47 # html text to include at home page
48 our $home_text = "++GITWEB_HOMETEXT++";
50 # URI of default stylesheet
51 our $stylesheet = "++GITWEB_CSS++";
52 # URI of GIT logo
53 our $logo = "++GITWEB_LOGO++";
55 # source of projects list
56 our $projects_list = "++GITWEB_LIST++";
58 # list of git base URLs used for URL to where fetch project from,
59 # i.e. full URL is "$git_base_url/$project"
60 our @git_base_url_list = ("++GITWEB_BASE_URL++");
62 # default blob_plain mimetype and default charset for text/plain blob
63 our $default_blob_plain_mimetype = 'text/plain';
64 our $default_text_plain_charset  = undef;
66 # file to use for guessing MIME types before trying /etc/mime.types
67 # (relative to the current git repository)
68 our $mimetypes_file = undef;
70 # You define site-wide feature defaults here; override them with
71 # $GITWEB_CONFIG as necessary.
72 our %feature = (
73         # feature => {'sub' => feature-sub, 'override' => allow-override, 'default' => [ default options...]
74         # if feature is overridable, feature-sub will be called with default options;
75         # return value indicates if to enable specified feature
77         'blame' => {
78                 'sub' => \&feature_blame,
79                 'override' => 0,
80                 'default' => [0]},
82         'snapshot' => {
83                 'sub' => \&feature_snapshot,
84                 'override' => 0,
85                 #         => [content-encoding, suffix, program]
86                 'default' => ['x-gzip', 'gz', 'gzip']},
87 );
89 sub gitweb_check_feature {
90         my ($name) = @_;
91         return undef unless exists $feature{$name};
92         my ($sub, $override, @defaults) = (
93                 $feature{$name}{'sub'},
94                 $feature{$name}{'override'},
95                 @{$feature{$name}{'default'}});
96         if (!$override) { return @defaults; }
97         return $sub->(@defaults);
98 }
100 # To enable system wide have in $GITWEB_CONFIG
101 # $feature{'blame'}{'default'} =  [1];
102 # To have project specific config enable override in  $GITWEB_CONFIG
103 # $feature{'blame'}{'override'} =  1;
104 # and in project config gitweb.blame = 0|1;
106 sub feature_blame {
107         my ($val) = git_get_project_config('blame', '--bool');
109         if ($val eq 'true') {
110                 return 1;
111         } elsif ($val eq 'false') {
112                 return 0;
113         }
115         return $_[0];
118 # To disable system wide have in $GITWEB_CONFIG
119 # $feature{'snapshot'}{'default'} =  [undef];
120 # To have project specific config enable override in  $GITWEB_CONFIG
121 # $feature{'blame'}{'override'} =  1;
122 # and in project config  gitweb.snapshot = none|gzip|bzip2
124 sub feature_snapshot {
125         my ($ctype, $suffix, $command) = @_;
127         my ($val) = git_get_project_config('snapshot');
129         if ($val eq 'gzip') {
130                 return ('x-gzip', 'gz', 'gzip');
131         } elsif ($val eq 'bzip2') {
132                 return ('x-bzip2', 'bz2', 'bzip2');
133         } elsif ($val eq 'none') {
134                 return ();
135         }
137         return ($ctype, $suffix, $command);
140 our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
141 require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
143 # version of the core git binary
144 our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
146 $projects_list ||= $projectroot;
147 if (! -d $git_temp) {
148         mkdir($git_temp, 0700) || die_error(undef, "Couldn't mkdir $git_temp");
151 # ======================================================================
152 # input validation and dispatch
153 our $action = $cgi->param('a');
154 if (defined $action) {
155         if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
156                 die_error(undef, "Invalid action parameter");
157         }
160 our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
161 if (defined $project) {
162         $project =~ s|^/||;
163         $project =~ s|/$||;
164         $project = undef unless $project;
166 if (defined $project) {
167         if (!validate_input($project)) {
168                 die_error(undef, "Invalid project parameter");
169         }
170         if (!(-d "$projectroot/$project")) {
171                 die_error(undef, "No such directory");
172         }
173         if (!(-e "$projectroot/$project/HEAD")) {
174                 die_error(undef, "No such project");
175         }
176         $ENV{'GIT_DIR'} = "$projectroot/$project";
179 our $file_name = $cgi->param('f');
180 if (defined $file_name) {
181         if (!validate_input($file_name)) {
182                 die_error(undef, "Invalid file parameter");
183         }
186 our $file_parent = $cgi->param('fp');
187 if (defined $file_parent) {
188         if (!validate_input($file_parent)) {
189                 die_error(undef, "Invalid file parent parameter");
190         }
193 our $hash = $cgi->param('h');
194 if (defined $hash) {
195         if (!validate_input($hash)) {
196                 die_error(undef, "Invalid hash parameter");
197         }
200 our $hash_parent = $cgi->param('hp');
201 if (defined $hash_parent) {
202         if (!validate_input($hash_parent)) {
203                 die_error(undef, "Invalid hash parent parameter");
204         }
207 our $hash_base = $cgi->param('hb');
208 if (defined $hash_base) {
209         if (!validate_input($hash_base)) {
210                 die_error(undef, "Invalid hash base parameter");
211         }
214 our $page = $cgi->param('pg');
215 if (defined $page) {
216         if ($page =~ m/[^0-9]$/) {
217                 die_error(undef, "Invalid page parameter");
218         }
221 our $searchtext = $cgi->param('s');
222 if (defined $searchtext) {
223         if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
224                 die_error(undef, "Invalid search parameter");
225         }
226         $searchtext = quotemeta $searchtext;
229 # dispatch
230 my %actions = (
231         "blame" => \&git_blame2,
232         "blobdiff" => \&git_blobdiff,
233         "blobdiff_plain" => \&git_blobdiff_plain,
234         "blob" => \&git_blob,
235         "blob_plain" => \&git_blob_plain,
236         "commitdiff" => \&git_commitdiff,
237         "commitdiff_plain" => \&git_commitdiff_plain,
238         "commit" => \&git_commit,
239         "heads" => \&git_heads,
240         "history" => \&git_history,
241         "log" => \&git_log,
242         "rss" => \&git_rss,
243         "search" => \&git_search,
244         "shortlog" => \&git_shortlog,
245         "summary" => \&git_summary,
246         "tag" => \&git_tag,
247         "tags" => \&git_tags,
248         "tree" => \&git_tree,
249         "snapshot" => \&git_snapshot,
250         # those below don't need $project
251         "opml" => \&git_opml,
252         "project_list" => \&git_project_list,
253 );
255 if (defined $project) {
256         $action ||= 'summary';
257 } else {
258         $action ||= 'project_list';
260 if (!defined($actions{$action})) {
261         die_error(undef, "Unknown action");
263 $actions{$action}->();
264 exit;
266 ## ======================================================================
267 ## action links
269 sub href(%) {
270         my %params = @_;
272         my @mapping = (
273                 action => "a",
274                 project => "p",
275                 file_name => "f",
276                 file_parent => "fp",
277                 hash => "h",
278                 hash_parent => "hp",
279                 hash_base => "hb",
280                 page => "pg",
281                 searchtext => "s",
282         );
283         my %mapping = @mapping;
285         $params{"project"} ||= $project;
287         my @result = ();
288         for (my $i = 0; $i < @mapping; $i += 2) {
289                 my ($name, $symbol) = ($mapping[$i], $mapping[$i+1]);
290                 if (defined $params{$name}) {
291                         push @result, $symbol . "=" . esc_param($params{$name});
292                 }
293         }
294         return "$my_uri?" . join(';', @result);
298 ## ======================================================================
299 ## validation, quoting/unquoting and escaping
301 sub validate_input {
302         my $input = shift;
304         if ($input =~ m/^[0-9a-fA-F]{40}$/) {
305                 return $input;
306         }
307         if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
308                 return undef;
309         }
310         if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
311                 return undef;
312         }
313         return $input;
316 # quote unsafe chars, but keep the slash, even when it's not
317 # correct, but quoted slashes look too horrible in bookmarks
318 sub esc_param {
319         my $str = shift;
320         $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
321         $str =~ s/\+/%2B/g;
322         $str =~ s/ /\+/g;
323         return $str;
326 # replace invalid utf8 character with SUBSTITUTION sequence
327 sub esc_html {
328         my $str = shift;
329         $str = decode("utf8", $str, Encode::FB_DEFAULT);
330         $str = escapeHTML($str);
331         $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
332         return $str;
335 # git may return quoted and escaped filenames
336 sub unquote {
337         my $str = shift;
338         if ($str =~ m/^"(.*)"$/) {
339                 $str = $1;
340                 $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
341         }
342         return $str;
345 # escape tabs (convert tabs to spaces)
346 sub untabify {
347         my $line = shift;
349         while ((my $pos = index($line, "\t")) != -1) {
350                 if (my $count = (8 - ($pos % 8))) {
351                         my $spaces = ' ' x $count;
352                         $line =~ s/\t/$spaces/;
353                 }
354         }
356         return $line;
359 ## ----------------------------------------------------------------------
360 ## HTML aware string manipulation
362 sub chop_str {
363         my $str = shift;
364         my $len = shift;
365         my $add_len = shift || 10;
367         # allow only $len chars, but don't cut a word if it would fit in $add_len
368         # if it doesn't fit, cut it if it's still longer than the dots we would add
369         $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
370         my $body = $1;
371         my $tail = $2;
372         if (length($tail) > 4) {
373                 $tail = " ...";
374                 $body =~ s/&[^;]*$//; # remove chopped character entities
375         }
376         return "$body$tail";
379 ## ----------------------------------------------------------------------
380 ## functions returning short strings
382 # CSS class for given age value (in seconds)
383 sub age_class {
384         my $age = shift;
386         if ($age < 60*60*2) {
387                 return "age0";
388         } elsif ($age < 60*60*24*2) {
389                 return "age1";
390         } else {
391                 return "age2";
392         }
395 # convert age in seconds to "nn units ago" string
396 sub age_string {
397         my $age = shift;
398         my $age_str;
400         if ($age > 60*60*24*365*2) {
401                 $age_str = (int $age/60/60/24/365);
402                 $age_str .= " years ago";
403         } elsif ($age > 60*60*24*(365/12)*2) {
404                 $age_str = int $age/60/60/24/(365/12);
405                 $age_str .= " months ago";
406         } elsif ($age > 60*60*24*7*2) {
407                 $age_str = int $age/60/60/24/7;
408                 $age_str .= " weeks ago";
409         } elsif ($age > 60*60*24*2) {
410                 $age_str = int $age/60/60/24;
411                 $age_str .= " days ago";
412         } elsif ($age > 60*60*2) {
413                 $age_str = int $age/60/60;
414                 $age_str .= " hours ago";
415         } elsif ($age > 60*2) {
416                 $age_str = int $age/60;
417                 $age_str .= " min ago";
418         } elsif ($age > 2) {
419                 $age_str = int $age;
420                 $age_str .= " sec ago";
421         } else {
422                 $age_str .= " right now";
423         }
424         return $age_str;
427 # convert file mode in octal to symbolic file mode string
428 sub mode_str {
429         my $mode = oct shift;
431         if (S_ISDIR($mode & S_IFMT)) {
432                 return 'drwxr-xr-x';
433         } elsif (S_ISLNK($mode)) {
434                 return 'lrwxrwxrwx';
435         } elsif (S_ISREG($mode)) {
436                 # git cares only about the executable bit
437                 if ($mode & S_IXUSR) {
438                         return '-rwxr-xr-x';
439                 } else {
440                         return '-rw-r--r--';
441                 };
442         } else {
443                 return '----------';
444         }
447 # convert file mode in octal to file type string
448 sub file_type {
449         my $mode = oct shift;
451         if (S_ISDIR($mode & S_IFMT)) {
452                 return "directory";
453         } elsif (S_ISLNK($mode)) {
454                 return "symlink";
455         } elsif (S_ISREG($mode)) {
456                 return "file";
457         } else {
458                 return "unknown";
459         }
462 ## ----------------------------------------------------------------------
463 ## functions returning short HTML fragments, or transforming HTML fragments
464 ## which don't beling to other sections
466 # format line of commit message or tag comment
467 sub format_log_line_html {
468         my $line = shift;
470         $line = esc_html($line);
471         $line =~ s/ /&nbsp;/g;
472         if ($line =~ m/([0-9a-fA-F]{40})/) {
473                 my $hash_text = $1;
474                 if (git_get_type($hash_text) eq "commit") {
475                         my $link =
476                                 $cgi->a({-href => href(action=>"commit", hash=>$hash_text),
477                                         -class => "text"}, $hash_text);
478                         $line =~ s/$hash_text/$link/;
479                 }
480         }
481         return $line;
484 # format marker of refs pointing to given object
485 sub format_ref_marker {
486         my ($refs, $id) = @_;
487         my $markers = '';
489         if (defined $refs->{$id}) {
490                 foreach my $ref (@{$refs->{$id}}) {
491                         my ($type, $name) = qw();
492                         # e.g. tags/v2.6.11 or heads/next
493                         if ($ref =~ m!^(.*?)s?/(.*)$!) {
494                                 $type = $1;
495                                 $name = $2;
496                         } else {
497                                 $type = "ref";
498                                 $name = $ref;
499                         }
501                         $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
502                 }
503         }
505         if ($markers) {
506                 return ' <span class="refs">'. $markers . '</span>';
507         } else {
508                 return "";
509         }
512 # format, perhaps shortened and with markers, title line
513 sub format_subject_html {
514         my ($long, $short, $href, $extra) = @_;
515         $extra = '' unless defined($extra);
517         if (length($short) < length($long)) {
518                 return $cgi->a({-href => $href, -class => "list subject",
519                                 -title => $long},
520                        esc_html($short) . $extra);
521         } else {
522                 return $cgi->a({-href => $href, -class => "list subject"},
523                        esc_html($long)  . $extra);
524         }
527 sub format_diff_line {
528         my $line = shift;
529         my $char = substr($line, 0, 1);
530         my $diff_class = "";
532         chomp $line;
534         if ($char eq '+') {
535                 $diff_class = " add";
536         } elsif ($char eq "-") {
537                 $diff_class = " rem";
538         } elsif ($char eq "@") {
539                 $diff_class = " chunk_header";
540         } elsif ($char eq "\\") {
541                 $diff_class = " incomplete";
542         }
543         $line = untabify($line);
544         return "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
547 ## ----------------------------------------------------------------------
548 ## git utility subroutines, invoking git commands
550 # get HEAD ref of given project as hash
551 sub git_get_head_hash {
552         my $project = shift;
553         my $oENV = $ENV{'GIT_DIR'};
554         my $retval = undef;
555         $ENV{'GIT_DIR'} = "$projectroot/$project";
556         if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
557                 my $head = <$fd>;
558                 close $fd;
559                 if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
560                         $retval = $1;
561                 }
562         }
563         if (defined $oENV) {
564                 $ENV{'GIT_DIR'} = $oENV;
565         }
566         return $retval;
569 # get type of given object
570 sub git_get_type {
571         my $hash = shift;
573         open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
574         my $type = <$fd>;
575         close $fd or return;
576         chomp $type;
577         return $type;
580 sub git_get_project_config {
581         my ($key, $type) = @_;
583         return unless ($key);
584         $key =~ s/^gitweb\.//;
585         return if ($key =~ m/\W/);
587         my @x = ($GIT, 'repo-config');
588         if (defined $type) { push @x, $type; }
589         push @x, "--get";
590         push @x, "gitweb.$key";
591         my $val = qx(@x);
592         chomp $val;
593         return ($val);
596 # get hash of given path at given ref
597 sub git_get_hash_by_path {
598         my $base = shift;
599         my $path = shift || return undef;
601         my $tree = $base;
603         open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
604                 or die_error(undef, "Open git-ls-tree failed");
605         my $line = <$fd>;
606         close $fd or return undef;
608         #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
609         $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
610         return $3;
613 ## ......................................................................
614 ## git utility functions, directly accessing git repository
616 # assumes that PATH is not symref
617 sub git_get_hash_by_ref {
618         my $path = shift;
620         open my $fd, "$projectroot/$path" or return undef;
621         my $head = <$fd>;
622         close $fd;
623         chomp $head;
624         if ($head =~ m/^[0-9a-fA-F]{40}$/) {
625                 return $head;
626         }
629 sub git_get_project_description {
630         my $path = shift;
632         open my $fd, "$projectroot/$path/description" or return undef;
633         my $descr = <$fd>;
634         close $fd;
635         chomp $descr;
636         return $descr;
639 sub git_get_project_url_list {
640         my $path = shift;
642         open my $fd, "$projectroot/$path/cloneurl" or return undef;
643         my @git_project_url_list = map { chomp; $_ } <$fd>;
644         close $fd;
646         return wantarray ? @git_project_url_list : \@git_project_url_list;
649 sub git_get_projects_list {
650         my @list;
652         if (-d $projects_list) {
653                 # search in directory
654                 my $dir = $projects_list;
655                 opendir my ($dh), $dir or return undef;
656                 while (my $dir = readdir($dh)) {
657                         if (-e "$projectroot/$dir/HEAD") {
658                                 my $pr = {
659                                         path => $dir,
660                                 };
661                                 push @list, $pr
662                         }
663                 }
664                 closedir($dh);
665         } elsif (-f $projects_list) {
666                 # read from file(url-encoded):
667                 # 'git%2Fgit.git Linus+Torvalds'
668                 # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
669                 # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
670                 open my ($fd), $projects_list or return undef;
671                 while (my $line = <$fd>) {
672                         chomp $line;
673                         my ($path, $owner) = split ' ', $line;
674                         $path = unescape($path);
675                         $owner = unescape($owner);
676                         if (!defined $path) {
677                                 next;
678                         }
679                         if (-e "$projectroot/$path/HEAD") {
680                                 my $pr = {
681                                         path => $path,
682                                         owner => decode("utf8", $owner, Encode::FB_DEFAULT),
683                                 };
684                                 push @list, $pr
685                         }
686                 }
687                 close $fd;
688         }
689         @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
690         return @list;
693 sub git_get_project_owner {
694         my $project = shift;
695         my $owner;
697         return undef unless $project;
699         # read from file (url-encoded):
700         # 'git%2Fgit.git Linus+Torvalds'
701         # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
702         # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
703         if (-f $projects_list) {
704                 open (my $fd , $projects_list);
705                 while (my $line = <$fd>) {
706                         chomp $line;
707                         my ($pr, $ow) = split ' ', $line;
708                         $pr = unescape($pr);
709                         $ow = unescape($ow);
710                         if ($pr eq $project) {
711                                 $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
712                                 last;
713                         }
714                 }
715                 close $fd;
716         }
717         if (!defined $owner) {
718                 $owner = get_file_owner("$projectroot/$project");
719         }
721         return $owner;
724 sub git_get_references {
725         my $type = shift || "";
726         my %refs;
727         my $fd;
728         # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
729         # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
730         if (-f "$projectroot/$project/info/refs") {
731                 open $fd, "$projectroot/$project/info/refs"
732                         or return;
733         } else {
734                 open $fd, "-|", $GIT, "ls-remote", "."
735                         or return;
736         }
738         while (my $line = <$fd>) {
739                 chomp $line;
740                 if ($line =~ m/^([0-9a-fA-F]{40})\trefs\/($type\/?[^\^]+)/) {
741                         if (defined $refs{$1}) {
742                                 push @{$refs{$1}}, $2;
743                         } else {
744                                 $refs{$1} = [ $2 ];
745                         }
746                 }
747         }
748         close $fd or return;
749         return \%refs;
752 sub git_get_following_references {
753         my $hash = shift || return undef;
754         my $type = shift;
755         my $base = shift || $hash_base || "HEAD";
757         my $refs = git_get_references($type);
758         open my $fd, "-|", $GIT, "rev-list", $base
759                 or return undef;
760         my @commits = map { chomp; $_ } <$fd>;
761         close $fd
762                 or return undef;
764         my @reflist;
765         my $lastref;
767         foreach my $commit (@commits) {
768                 foreach my $ref (@{$refs->{$commit}}) {
769                         $lastref = $ref;
770                         push @reflist, $lastref;
771                 }
772                 if ($commit eq $hash) {
773                         last;
774                 }
775         }
777         return wantarray ? @reflist : $lastref;
780 sub git_get_preceding_references {
781         my $hash = shift || return undef;
782         my $type = shift;
784         my $refs = git_get_references($type);
785         open my $fd, "-|", $GIT, "rev-list", $hash
786                 or return undef;
787         my @commits = map { chomp; $_ } <$fd>;
788         close $fd
789                 or return undef;
791         my @reflist;
792         my $firstref;
794         foreach my $commit (@commits) {
795                 foreach my $ref (@{$refs->{$commit}}) {
796                         $firstref = $ref unless $firstref;
797                         push @reflist, $ref;
798                 }
799         }
801         return wantarray ? @reflist : $firstref;
804 ## ----------------------------------------------------------------------
805 ## parse to hash functions
807 sub parse_date {
808         my $epoch = shift;
809         my $tz = shift || "-0000";
811         my %date;
812         my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
813         my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
814         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
815         $date{'hour'} = $hour;
816         $date{'minute'} = $min;
817         $date{'mday'} = $mday;
818         $date{'day'} = $days[$wday];
819         $date{'month'} = $months[$mon];
820         $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
821                            $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
822         $date{'mday-time'} = sprintf "%d %s %02d:%02d",
823                              $mday, $months[$mon], $hour ,$min;
825         $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
826         my $local = $epoch + ((int $1 + ($2/60)) * 3600);
827         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
828         $date{'hour_local'} = $hour;
829         $date{'minute_local'} = $min;
830         $date{'tz_local'} = $tz;
831         return %date;
834 sub parse_tag {
835         my $tag_id = shift;
836         my %tag;
837         my @comment;
839         open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
840         $tag{'id'} = $tag_id;
841         while (my $line = <$fd>) {
842                 chomp $line;
843                 if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
844                         $tag{'object'} = $1;
845                 } elsif ($line =~ m/^type (.+)$/) {
846                         $tag{'type'} = $1;
847                 } elsif ($line =~ m/^tag (.+)$/) {
848                         $tag{'name'} = $1;
849                 } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
850                         $tag{'author'} = $1;
851                         $tag{'epoch'} = $2;
852                         $tag{'tz'} = $3;
853                 } elsif ($line =~ m/--BEGIN/) {
854                         push @comment, $line;
855                         last;
856                 } elsif ($line eq "") {
857                         last;
858                 }
859         }
860         push @comment, <$fd>;
861         $tag{'comment'} = \@comment;
862         close $fd or return;
863         if (!defined $tag{'name'}) {
864                 return
865         };
866         return %tag
869 sub parse_commit {
870         my $commit_id = shift;
871         my $commit_text = shift;
873         my @commit_lines;
874         my %co;
876         if (defined $commit_text) {
877                 @commit_lines = @$commit_text;
878         } else {
879                 $/ = "\0";
880                 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
881                         or return;
882                 @commit_lines = split '\n', <$fd>;
883                 close $fd or return;
884                 $/ = "\n";
885                 pop @commit_lines;
886         }
887         my $header = shift @commit_lines;
888         if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
889                 return;
890         }
891         ($co{'id'}, my @parents) = split ' ', $header;
892         $co{'parents'} = \@parents;
893         $co{'parent'} = $parents[0];
894         while (my $line = shift @commit_lines) {
895                 last if $line eq "\n";
896                 if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
897                         $co{'tree'} = $1;
898                 } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
899                         $co{'author'} = $1;
900                         $co{'author_epoch'} = $2;
901                         $co{'author_tz'} = $3;
902                         if ($co{'author'} =~ m/^([^<]+) </) {
903                                 $co{'author_name'} = $1;
904                         } else {
905                                 $co{'author_name'} = $co{'author'};
906                         }
907                 } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
908                         $co{'committer'} = $1;
909                         $co{'committer_epoch'} = $2;
910                         $co{'committer_tz'} = $3;
911                         $co{'committer_name'} = $co{'committer'};
912                         $co{'committer_name'} =~ s/ <.*//;
913                 }
914         }
915         if (!defined $co{'tree'}) {
916                 return;
917         };
919         foreach my $title (@commit_lines) {
920                 $title =~ s/^    //;
921                 if ($title ne "") {
922                         $co{'title'} = chop_str($title, 80, 5);
923                         # remove leading stuff of merges to make the interesting part visible
924                         if (length($title) > 50) {
925                                 $title =~ s/^Automatic //;
926                                 $title =~ s/^merge (of|with) /Merge ... /i;
927                                 if (length($title) > 50) {
928                                         $title =~ s/(http|rsync):\/\///;
929                                 }
930                                 if (length($title) > 50) {
931                                         $title =~ s/(master|www|rsync)\.//;
932                                 }
933                                 if (length($title) > 50) {
934                                         $title =~ s/kernel.org:?//;
935                                 }
936                                 if (length($title) > 50) {
937                                         $title =~ s/\/pub\/scm//;
938                                 }
939                         }
940                         $co{'title_short'} = chop_str($title, 50, 5);
941                         last;
942                 }
943         }
944         # remove added spaces
945         foreach my $line (@commit_lines) {
946                 $line =~ s/^    //;
947         }
948         $co{'comment'} = \@commit_lines;
950         my $age = time - $co{'committer_epoch'};
951         $co{'age'} = $age;
952         $co{'age_string'} = age_string($age);
953         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
954         if ($age > 60*60*24*7*2) {
955                 $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
956                 $co{'age_string_age'} = $co{'age_string'};
957         } else {
958                 $co{'age_string_date'} = $co{'age_string'};
959                 $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
960         }
961         return %co;
964 # parse ref from ref_file, given by ref_id, with given type
965 sub parse_ref {
966         my $ref_file = shift;
967         my $ref_id = shift;
968         my $type = shift || git_get_type($ref_id);
969         my %ref_item;
971         $ref_item{'type'} = $type;
972         $ref_item{'id'} = $ref_id;
973         $ref_item{'epoch'} = 0;
974         $ref_item{'age'} = "unknown";
975         if ($type eq "tag") {
976                 my %tag = parse_tag($ref_id);
977                 $ref_item{'comment'} = $tag{'comment'};
978                 if ($tag{'type'} eq "commit") {
979                         my %co = parse_commit($tag{'object'});
980                         $ref_item{'epoch'} = $co{'committer_epoch'};
981                         $ref_item{'age'} = $co{'age_string'};
982                 } elsif (defined($tag{'epoch'})) {
983                         my $age = time - $tag{'epoch'};
984                         $ref_item{'epoch'} = $tag{'epoch'};
985                         $ref_item{'age'} = age_string($age);
986                 }
987                 $ref_item{'reftype'} = $tag{'type'};
988                 $ref_item{'name'} = $tag{'name'};
989                 $ref_item{'refid'} = $tag{'object'};
990         } elsif ($type eq "commit"){
991                 my %co = parse_commit($ref_id);
992                 $ref_item{'reftype'} = "commit";
993                 $ref_item{'name'} = $ref_file;
994                 $ref_item{'title'} = $co{'title'};
995                 $ref_item{'refid'} = $ref_id;
996                 $ref_item{'epoch'} = $co{'committer_epoch'};
997                 $ref_item{'age'} = $co{'age_string'};
998         } else {
999                 $ref_item{'reftype'} = $type;
1000                 $ref_item{'name'} = $ref_file;
1001                 $ref_item{'refid'} = $ref_id;
1002         }
1004         return %ref_item;
1007 # parse line of git-diff-tree "raw" output
1008 sub parse_difftree_raw_line {
1009         my $line = shift;
1010         my %res;
1012         # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
1013         # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
1014         if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
1015                 $res{'from_mode'} = $1;
1016                 $res{'to_mode'} = $2;
1017                 $res{'from_id'} = $3;
1018                 $res{'to_id'} = $4;
1019                 $res{'status'} = $5;
1020                 $res{'similarity'} = $6;
1021                 if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
1022                         ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
1023                 } else {
1024                         $res{'file'} = unquote($7);
1025                 }
1026         }
1027         # 'c512b523472485aef4fff9e57b229d9d243c967f'
1028         #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
1029         #       $res{'commit'} = $1;
1030         #}
1032         return wantarray ? %res : \%res;
1035 ## ......................................................................
1036 ## parse to array of hashes functions
1038 sub git_get_refs_list {
1039         my $ref_dir = shift;
1040         my @reflist;
1042         my @refs;
1043         my $pfxlen = length("$projectroot/$project/$ref_dir");
1044         File::Find::find(sub {
1045                 return if (/^\./);
1046                 if (-f $_) {
1047                         push @refs, substr($File::Find::name, $pfxlen + 1);
1048                 }
1049         }, "$projectroot/$project/$ref_dir");
1051         foreach my $ref_file (@refs) {
1052                 my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
1053                 my $type = git_get_type($ref_id) || next;
1054                 my %ref_item = parse_ref($ref_file, $ref_id, $type);
1056                 push @reflist, \%ref_item;
1057         }
1058         # sort refs by age
1059         @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1060         return \@reflist;
1063 ## ----------------------------------------------------------------------
1064 ## filesystem-related functions
1066 sub get_file_owner {
1067         my $path = shift;
1069         my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1070         my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1071         if (!defined $gcos) {
1072                 return undef;
1073         }
1074         my $owner = $gcos;
1075         $owner =~ s/[,;].*$//;
1076         return decode("utf8", $owner, Encode::FB_DEFAULT);
1079 ## ......................................................................
1080 ## mimetype related functions
1082 sub mimetype_guess_file {
1083         my $filename = shift;
1084         my $mimemap = shift;
1085         -r $mimemap or return undef;
1087         my %mimemap;
1088         open(MIME, $mimemap) or return undef;
1089         while (<MIME>) {
1090                 next if m/^#/; # skip comments
1091                 my ($mime, $exts) = split(/\t+/);
1092                 if (defined $exts) {
1093                         my @exts = split(/\s+/, $exts);
1094                         foreach my $ext (@exts) {
1095                                 $mimemap{$ext} = $mime;
1096                         }
1097                 }
1098         }
1099         close(MIME);
1101         $filename =~ /\.(.*?)$/;
1102         return $mimemap{$1};
1105 sub mimetype_guess {
1106         my $filename = shift;
1107         my $mime;
1108         $filename =~ /\./ or return undef;
1110         if ($mimetypes_file) {
1111                 my $file = $mimetypes_file;
1112                 if ($file !~ m!^/!) { # if it is relative path
1113                         # it is relative to project
1114                         $file = "$projectroot/$project/$file";
1115                 }
1116                 $mime = mimetype_guess_file($filename, $file);
1117         }
1118         $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1119         return $mime;
1122 sub blob_mimetype {
1123         my $fd = shift;
1124         my $filename = shift;
1126         if ($filename) {
1127                 my $mime = mimetype_guess($filename);
1128                 $mime and return $mime;
1129         }
1131         # just in case
1132         return $default_blob_plain_mimetype unless $fd;
1134         if (-T $fd) {
1135                 return 'text/plain' .
1136                        ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1137         } elsif (! $filename) {
1138                 return 'application/octet-stream';
1139         } elsif ($filename =~ m/\.png$/i) {
1140                 return 'image/png';
1141         } elsif ($filename =~ m/\.gif$/i) {
1142                 return 'image/gif';
1143         } elsif ($filename =~ m/\.jpe?g$/i) {
1144                 return 'image/jpeg';
1145         } else {
1146                 return 'application/octet-stream';
1147         }
1150 ## ======================================================================
1151 ## functions printing HTML: header, footer, error page
1153 sub git_header_html {
1154         my $status = shift || "200 OK";
1155         my $expires = shift;
1157         my $title = "$site_name git";
1158         if (defined $project) {
1159                 $title .= " - $project";
1160                 if (defined $action) {
1161                         $title .= "/$action";
1162                         if (defined $file_name) {
1163                                 $title .= " - $file_name";
1164                                 if ($action eq "tree" && $file_name !~ m|/$|) {
1165                                         $title .= "/";
1166                                 }
1167                         }
1168                 }
1169         }
1170         my $content_type;
1171         # require explicit support from the UA if we are to send the page as
1172         # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1173         # we have to do this because MSIE sometimes globs '*/*', pretending to
1174         # support xhtml+xml but choking when it gets what it asked for.
1175         if (defined $cgi->http('HTTP_ACCEPT') &&
1176             $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1177             $cgi->Accept('application/xhtml+xml') != 0) {
1178                 $content_type = 'application/xhtml+xml';
1179         } else {
1180                 $content_type = 'text/html';
1181         }
1182         print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1183                            -status=> $status, -expires => $expires);
1184         print <<EOF;
1185 <?xml version="1.0" encoding="utf-8"?>
1186 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1187 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1188 <!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1189 <!-- git core binaries version $git_version -->
1190 <head>
1191 <meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1192 <meta name="generator" content="gitweb/$version git/$git_version"/>
1193 <meta name="robots" content="index, nofollow"/>
1194 <title>$title</title>
1195 <link rel="stylesheet" type="text/css" href="$stylesheet"/>
1196 EOF
1197         if (defined $project) {
1198                 printf('<link rel="alternate" title="%s log" '.
1199                        'href="%s" type="application/rss+xml"/>'."\n",
1200                        esc_param($project), href(action=>"rss"));
1201         }
1203         print "</head>\n" .
1204               "<body>\n" .
1205               "<div class=\"page_header\">\n" .
1206               "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1207               "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1208               "</a>\n";
1209         print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1210         if (defined $project) {
1211                 print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1212                 if (defined $action) {
1213                         print " / $action";
1214                 }
1215                 print "\n";
1216                 if (!defined $searchtext) {
1217                         $searchtext = "";
1218                 }
1219                 my $search_hash;
1220                 if (defined $hash_base) {
1221                         $search_hash = $hash_base;
1222                 } elsif (defined $hash) {
1223                         $search_hash = $hash;
1224                 } else {
1225                         $search_hash = "HEAD";
1226                 }
1227                 $cgi->param("a", "search");
1228                 $cgi->param("h", $search_hash);
1229                 print $cgi->startform(-method => "get", -action => $my_uri) .
1230                       "<div class=\"search\">\n" .
1231                       $cgi->hidden(-name => "p") . "\n" .
1232                       $cgi->hidden(-name => "a") . "\n" .
1233                       $cgi->hidden(-name => "h") . "\n" .
1234                       $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1235                       "</div>" .
1236                       $cgi->end_form() . "\n";
1237         }
1238         print "</div>\n";
1241 sub git_footer_html {
1242         print "<div class=\"page_footer\">\n";
1243         if (defined $project) {
1244                 my $descr = git_get_project_description($project);
1245                 if (defined $descr) {
1246                         print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1247                 }
1248                 print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1249         } else {
1250                 print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1251         }
1252         print "</div>\n" .
1253               "</body>\n" .
1254               "</html>";
1257 sub die_error {
1258         my $status = shift || "403 Forbidden";
1259         my $error = shift || "Malformed query, file missing or permission denied";
1261         git_header_html($status);
1262         print <<EOF;
1263 <div class="page_body">
1264 <br /><br />
1265 $status - $error
1266 <br />
1267 </div>
1268 EOF
1269         git_footer_html();
1270         exit;
1273 ## ----------------------------------------------------------------------
1274 ## functions printing or outputting HTML: navigation
1276 sub git_print_page_nav {
1277         my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1278         $extra = '' if !defined $extra; # pager or formats
1280         my @navs = qw(summary shortlog log commit commitdiff tree);
1281         if ($suppress) {
1282                 @navs = grep { $_ ne $suppress } @navs;
1283         }
1285         my %arg = map { $_ => {action=>$_} } @navs;
1286         if (defined $head) {
1287                 for (qw(commit commitdiff)) {
1288                         $arg{$_}{hash} = $head;
1289                 }
1290                 if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1291                         for (qw(shortlog log)) {
1292                                 $arg{$_}{hash} = $head;
1293                         }
1294                 }
1295         }
1296         $arg{tree}{hash} = $treehead if defined $treehead;
1297         $arg{tree}{hash_base} = $treebase if defined $treebase;
1299         print "<div class=\"page_nav\">\n" .
1300                 (join " | ",
1301                  map { $_ eq $current ?
1302                        $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1303                  } @navs);
1304         print "<br/>\n$extra<br/>\n" .
1305               "</div>\n";
1308 sub format_paging_nav {
1309         my ($action, $hash, $head, $page, $nrevs) = @_;
1310         my $paging_nav;
1313         if ($hash ne $head || $page) {
1314                 $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1315         } else {
1316                 $paging_nav .= "HEAD";
1317         }
1319         if ($page > 0) {
1320                 $paging_nav .= " &sdot; " .
1321                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1322                                  -accesskey => "p", -title => "Alt-p"}, "prev");
1323         } else {
1324                 $paging_nav .= " &sdot; prev";
1325         }
1327         if ($nrevs >= (100 * ($page+1)-1)) {
1328                 $paging_nav .= " &sdot; " .
1329                         $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1330                                  -accesskey => "n", -title => "Alt-n"}, "next");
1331         } else {
1332                 $paging_nav .= " &sdot; next";
1333         }
1335         return $paging_nav;
1338 ## ......................................................................
1339 ## functions printing or outputting HTML: div
1341 sub git_print_header_div {
1342         my ($action, $title, $hash, $hash_base) = @_;
1343         my %args = ();
1345         $args{action} = $action;
1346         $args{hash} = $hash if $hash;
1347         $args{hash_base} = $hash_base if $hash_base;
1349         print "<div class=\"header\">\n" .
1350               $cgi->a({-href => href(%args), -class => "title"},
1351               $title ? $title : $action) .
1352               "\n</div>\n";
1355 sub git_print_page_path {
1356         my $name = shift;
1357         my $type = shift;
1358         my $hb = shift;
1360         if (!defined $name) {
1361                 print "<div class=\"page_path\">/</div>\n";
1362         } elsif (defined $type && $type eq 'blob') {
1363                 print "<div class=\"page_path\">";
1364                 if (defined $hb) {
1365                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1366                                                      hash_base=>$hb)},
1367                                       esc_html($name));
1368                 } else {
1369                         print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1370                                       esc_html($name));
1371                 }
1372                 print "<br/></div>\n";
1373         } else {
1374                 print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1375         }
1378 sub git_print_log {
1379         my $log = shift;
1381         # remove leading empty lines
1382         while (defined $log->[0] && $log->[0] eq "") {
1383                 shift @$log;
1384         }
1386         # print log
1387         my $signoff = 0;
1388         my $empty = 0;
1389         foreach my $line (@$log) {
1390                 # print only one empty line
1391                 # do not print empty line after signoff
1392                 if ($line eq "") {
1393                         next if ($empty || $signoff);
1394                         $empty = 1;
1395                 } else {
1396                         $empty = 0;
1397                 }
1398                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1399                         $signoff = 1;
1400                         print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1401                 } else {
1402                         $signoff = 0;
1403                         print format_log_line_html($line) . "<br/>\n";
1404                 }
1405         }
1408 sub git_print_simplified_log {
1409         my $log = shift;
1410         my $remove_title = shift;
1412         shift @$log if $remove_title;
1413         # remove leading empty lines
1414         while (defined $log->[0] && $log->[0] eq "") {
1415                 shift @$log;
1416         }
1418         # simplify and print log
1419         my $empty = 0;
1420         foreach my $line (@$log) {
1421                 # remove signoff lines
1422                 if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1423                         next;
1424                 }
1425                 # print only one empty line
1426                 if ($line eq "") {
1427                         next if $empty;
1428                         $empty = 1;
1429                 } else {
1430                         $empty = 0;
1431                 }
1432                 print format_log_line_html($line) . "<br/>\n";
1433         }
1434         # end with single empty line
1435         print "<br/>\n" unless $empty;
1438 ## ......................................................................
1439 ## functions printing large fragments of HTML
1441 sub git_difftree_body {
1442         my ($difftree, $hash, $parent) = @_;
1444         print "<div class=\"list_head\">\n";
1445         if ($#{$difftree} > 10) {
1446                 print(($#{$difftree} + 1) . " files changed:\n");
1447         }
1448         print "</div>\n";
1450         print "<table class=\"diff_tree\">\n";
1451         my $alternate = 0;
1452         foreach my $line (@{$difftree}) {
1453                 my %diff = parse_difftree_raw_line($line);
1455                 if ($alternate) {
1456                         print "<tr class=\"dark\">\n";
1457                 } else {
1458                         print "<tr class=\"light\">\n";
1459                 }
1460                 $alternate ^= 1;
1462                 my ($to_mode_oct, $to_mode_str, $to_file_type);
1463                 my ($from_mode_oct, $from_mode_str, $from_file_type);
1464                 if ($diff{'to_mode'} ne ('0' x 6)) {
1465                         $to_mode_oct = oct $diff{'to_mode'};
1466                         if (S_ISREG($to_mode_oct)) { # only for regular file
1467                                 $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1468                         }
1469                         $to_file_type = file_type($diff{'to_mode'});
1470                 }
1471                 if ($diff{'from_mode'} ne ('0' x 6)) {
1472                         $from_mode_oct = oct $diff{'from_mode'};
1473                         if (S_ISREG($to_mode_oct)) { # only for regular file
1474                                 $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1475                         }
1476                         $from_file_type = file_type($diff{'from_mode'});
1477                 }
1479                 if ($diff{'status'} eq "A") { # created
1480                         my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1481                         $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
1482                         $mode_chng   .= "]</span>";
1483                         print "<td>" .
1484                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1485                                                      hash_base=>$hash, file_name=>$diff{'file'}),
1486                                       -class => "list"}, esc_html($diff{'file'})) .
1487                               "</td>\n" .
1488                               "<td>$mode_chng</td>\n" .
1489                               "<td class=\"link\">" .
1490                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1491                                                      hash_base=>$hash, file_name=>$diff{'file'})},
1492                                       "blob") .
1493                               "</td>\n";
1495                 } elsif ($diff{'status'} eq "D") { # deleted
1496                         my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1497                         print "<td>" .
1498                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1499                                                      hash_base=>$parent, file_name=>$diff{'file'}),
1500                                        -class => "list"}, esc_html($diff{'file'})) .
1501                               "</td>\n" .
1502                               "<td>$mode_chng</td>\n" .
1503                               "<td class=\"link\">" .
1504                               $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1505                                                      hash_base=>$parent, file_name=>$diff{'file'})},
1506                                       "blob") .
1507                               " | " .
1508                               $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1509                                                      file_name=>$diff{'file'})},\
1510                                       "history") .
1511                               "</td>\n";
1513                 } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1514                         my $mode_chnge = "";
1515                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1516                                 $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1517                                 if ($from_file_type != $to_file_type) {
1518                                         $mode_chnge .= " from $from_file_type to $to_file_type";
1519                                 }
1520                                 if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1521                                         if ($from_mode_str && $to_mode_str) {
1522                                                 $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1523                                         } elsif ($to_mode_str) {
1524                                                 $mode_chnge .= " mode: $to_mode_str";
1525                                         }
1526                                 }
1527                                 $mode_chnge .= "]</span>\n";
1528                         }
1529                         print "<td>";
1530                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1531                                 print $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1532                                                              hash_base=>$hash, file_name=>$diff{'file'}),
1533                                               -class => "list"}, esc_html($diff{'file'}));
1534                         } else { # only mode changed
1535                                 print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1536                                                              hash_base=>$hash, file_name=>$diff{'file'}),
1537                                               -class => "list"}, esc_html($diff{'file'}));
1538                         }
1539                         print "</td>\n" .
1540                               "<td>$mode_chnge</td>\n" .
1541                               "<td class=\"link\">" .
1542                                 $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1543                                                        hash_base=>$hash, file_name=>$diff{'file'})},
1544                                         "blob");
1545                         if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1546                                 print " | " .
1547                                         $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1548                                                                hash_base=>$hash, file_name=>$diff{'file'})},
1549                                                 "diff");
1550                         }
1551                         print " | " .
1552                                 $cgi->a({-href => href(action=>"history",
1553                                                        hash_base=>$hash, file_name=>$diff{'file'})},
1554                                         "history");
1555                         print "</td>\n";
1557                 } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1558                         my %status_name = ('R' => 'moved', 'C' => 'copied');
1559                         my $nstatus = $status_name{$diff{'status'}};
1560                         my $mode_chng = "";
1561                         if ($diff{'from_mode'} != $diff{'to_mode'}) {
1562                                 # mode also for directories, so we cannot use $to_mode_str
1563                                 $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1564                         }
1565                         print "<td>" .
1566                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1567                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1568                                       -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1569                               "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1570                               $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1571                                                      hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1572                                       -class => "list"}, esc_html($diff{'from_file'})) .
1573                               " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1574                               "<td class=\"link\">" .
1575                               $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1576                                                      hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1577                                       "blob");
1578                         if ($diff{'to_id'} ne $diff{'from_id'}) {
1579                                 print " | " .
1580                                         $cgi->a({-href => href(action=>"blobdiff", hash_base=>$hash,
1581                                                                hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1582                                                                file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1583                                                 "diff");
1584                         }
1585                         print "</td>\n";
1587                 } # we should not encounter Unmerged (U) or Unknown (X) status
1588                 print "</tr>\n";
1589         }
1590         print "</table>\n";
1593 sub git_patchset_body {
1594         my ($fd, $difftree, $hash, $hash_parent) = @_;
1596         my $patch_idx = 0;
1597         my $in_header = 0;
1598         my $patch_found = 0;
1599         my %diffinfo;
1601         print "<div class=\"patchset\">\n";
1603         LINE:
1604         while (my $patch_line @$fd>) {
1605                 chomp $patch_line;
1607                 if ($patch_line =~ m/^diff /) { # "git diff" header
1608                         # beginning of patch (in patchset)
1609                         if ($patch_found) {
1610                                 # close previous patch
1611                                 print "</div>\n"; # class="patch"
1612                         } else {
1613                                 # first patch in patchset
1614                                 $patch_found = 1;
1615                         }
1616                         print "<div class=\"patch\">\n";
1618                         %diffinfo = parse_difftree_raw_line($difftree->[$patch_idx++]);
1620                         # for now, no extended header, hence we skip empty patches
1621                         # companion to  next LINE if $in_header;
1622                         if ($diffinfo{'from_id'} eq $diffinfo{'to_id'}) { # no change
1623                                 $in_header = 1;
1624                                 next LINE;
1625                         }
1627                         if ($diffinfo{'status'} eq "A") { # added
1628                                 print "<div class=\"diff_info\">" . file_type($diffinfo{'to_mode'}) . ":" .
1629                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1630                                                              hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1631                                               $diffinfo{'to_id'}) . "(new)" .
1632                                       "</div>\n"; # class="diff_info"
1634                         } elsif ($diffinfo{'status'} eq "D") { # deleted
1635                                 print "<div class=\"diff_info\">" . file_type($diffinfo{'from_mode'}) . ":" .
1636                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1637                                                              hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1638                                               $diffinfo{'from_id'}) . "(deleted)" .
1639                                       "</div>\n"; # class="diff_info"
1641                         } elsif ($diffinfo{'status'} eq "R" || # renamed
1642                                  $diffinfo{'status'} eq "C") { # copied
1643                                 print "<div class=\"diff_info\">" .
1644                                       file_type($diffinfo{'from_mode'}) . ":" .
1645                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1646                                                              hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'from_file'})},
1647                                               $diffinfo{'from_id'}) .
1648                                       " -> " .
1649                                       file_type($diffinfo{'to_mode'}) . ":" .
1650                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1651                                                              hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'to_file'})},
1652                                               $diffinfo{'to_id'});
1653                                 print "</div>\n"; # class="diff_info"
1655                         } else { # modified, mode changed, ...
1656                                 print "<div class=\"diff_info\">" .
1657                                       file_type($diffinfo{'from_mode'}) . ":" .
1658                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1659                                                              hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1660                                               $diffinfo{'from_id'}) .
1661                                       " -> " .
1662                                       file_type($diffinfo{'to_mode'}) . ":" .
1663                                       $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1664                                                              hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1665                                               $diffinfo{'to_id'});
1666                                 print "</div>\n"; # class="diff_info"
1667                         }
1669                         #print "<div class=\"diff extended_header\">\n";
1670                         $in_header = 1;
1671                         next LINE;
1672                 } # start of patch in patchset
1675                 if ($in_header && $patch_line =~ m/^---/) {
1676                         #print "</div>\n"
1677                         $in_header = 0;
1678                 }
1679                 next LINE if $in_header;
1681                 print format_diff_line($patch_line);
1682         }
1683         print "</div>\n" if $patch_found; # class="patch"
1685         print "</div>\n"; # class="patchset"
1688 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1690 sub git_shortlog_body {
1691         # uses global variable $project
1692         my ($revlist, $from, $to, $refs, $extra) = @_;
1694         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1695         my $have_snapshot = (defined $ctype && defined $suffix);
1697         $from = 0 unless defined $from;
1698         $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1700         print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1701         my $alternate = 0;
1702         for (my $i = $from; $i <= $to; $i++) {
1703                 my $commit = $revlist->[$i];
1704                 #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1705                 my $ref = format_ref_marker($refs, $commit);
1706                 my %co = parse_commit($commit);
1707                 if ($alternate) {
1708                         print "<tr class=\"dark\">\n";
1709                 } else {
1710                         print "<tr class=\"light\">\n";
1711                 }
1712                 $alternate ^= 1;
1713                 # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1714                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1715                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1716                       "<td>";
1717                 print format_subject_html($co{'title'}, $co{'title_short'},
1718                                           href(action=>"commit", hash=>$commit), $ref);
1719                 print "</td>\n" .
1720                       "<td class=\"link\">" .
1721                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1722                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1723                 if ($have_snapshot) {
1724                         print " | " .  $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1725                 }
1726                 print "</td>\n" .
1727                       "</tr>\n";
1728         }
1729         if (defined $extra) {
1730                 print "<tr>\n" .
1731                       "<td colspan=\"4\">$extra</td>\n" .
1732                       "</tr>\n";
1733         }
1734         print "</table>\n";
1737 sub git_history_body {
1738         # Warning: assumes constant type (blob or tree) during history
1739         my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1741         print "<table class=\"history\" cellspacing=\"0\">\n";
1742         my $alternate = 0;
1743         while (my $line = <$fd>) {
1744                 if ($line !~ m/^([0-9a-fA-F]{40})/) {
1745                         next;
1746                 }
1748                 my $commit = $1;
1749                 my %co = parse_commit($commit);
1750                 if (!%co) {
1751                         next;
1752                 }
1754                 my $ref = format_ref_marker($refs, $commit);
1756                 if ($alternate) {
1757                         print "<tr class=\"dark\">\n";
1758                 } else {
1759                         print "<tr class=\"light\">\n";
1760                 }
1761                 $alternate ^= 1;
1762                 print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1763                       # shortlog uses      chop_str($co{'author_name'}, 10)
1764                       "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1765                       "<td>";
1766                 # originally git_history used chop_str($co{'title'}, 50)
1767                 print format_subject_html($co{'title'}, $co{'title_short'},
1768                                           href(action=>"commit", hash=>$commit), $ref);
1769                 print "</td>\n" .
1770                       "<td class=\"link\">" .
1771                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1772                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1773                       $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1775                 if ($ftype eq 'blob') {
1776                         my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1777                         my $blob_parent  = git_get_hash_by_path($commit, $file_name);
1778                         if (defined $blob_current && defined $blob_parent &&
1779                                         $blob_current ne $blob_parent) {
1780                                 print " | " .
1781                                         $cgi->a({-href => href(action=>"blobdiff", hash=>$blob_current, hash_parent=>$blob_parent,
1782                                                                hash_base=>$commit, file_name=>$file_name)},
1783                                                 "diff to current");
1784                         }
1785                 }
1786                 print "</td>\n" .
1787                       "</tr>\n";
1788         }
1789         if (defined $extra) {
1790                 print "<tr>\n" .
1791                       "<td colspan=\"4\">$extra</td>\n" .
1792                       "</tr>\n";
1793         }
1794         print "</table>\n";
1797 sub git_tags_body {
1798         # uses global variable $project
1799         my ($taglist, $from, $to, $extra) = @_;
1800         $from = 0 unless defined $from;
1801         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1803         print "<table class=\"tags\" cellspacing=\"0\">\n";
1804         my $alternate = 0;
1805         for (my $i = $from; $i <= $to; $i++) {
1806                 my $entry = $taglist->[$i];
1807                 my %tag = %$entry;
1808                 my $comment_lines = $tag{'comment'};
1809                 my $comment = shift @$comment_lines;
1810                 my $comment_short;
1811                 if (defined $comment) {
1812                         $comment_short = chop_str($comment, 30, 5);
1813                 }
1814                 if ($alternate) {
1815                         print "<tr class=\"dark\">\n";
1816                 } else {
1817                         print "<tr class=\"light\">\n";
1818                 }
1819                 $alternate ^= 1;
1820                 print "<td><i>$tag{'age'}</i></td>\n" .
1821                       "<td>" .
1822                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1823                                -class => "list name"}, esc_html($tag{'name'})) .
1824                       "</td>\n" .
1825                       "<td>";
1826                 if (defined $comment) {
1827                         print format_subject_html($comment, $comment_short,
1828                                                   href(action=>"tag", hash=>$tag{'id'}));
1829                 }
1830                 print "</td>\n" .
1831                       "<td class=\"selflink\">";
1832                 if ($tag{'type'} eq "tag") {
1833                         print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1834                 } else {
1835                         print "&nbsp;";
1836                 }
1837                 print "</td>\n" .
1838                       "<td class=\"link\">" . " | " .
1839                       $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1840                 if ($tag{'reftype'} eq "commit") {
1841                         print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1842                               " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1843                 } elsif ($tag{'reftype'} eq "blob") {
1844                         print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1845                 }
1846                 print "</td>\n" .
1847                       "</tr>";
1848         }
1849         if (defined $extra) {
1850                 print "<tr>\n" .
1851                       "<td colspan=\"5\">$extra</td>\n" .
1852                       "</tr>\n";
1853         }
1854         print "</table>\n";
1857 sub git_heads_body {
1858         # uses global variable $project
1859         my ($taglist, $head, $from, $to, $extra) = @_;
1860         $from = 0 unless defined $from;
1861         $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1863         print "<table class=\"heads\" cellspacing=\"0\">\n";
1864         my $alternate = 0;
1865         for (my $i = $from; $i <= $to; $i++) {
1866                 my $entry = $taglist->[$i];
1867                 my %tag = %$entry;
1868                 my $curr = $tag{'id'} eq $head;
1869                 if ($alternate) {
1870                         print "<tr class=\"dark\">\n";
1871                 } else {
1872                         print "<tr class=\"light\">\n";
1873                 }
1874                 $alternate ^= 1;
1875                 print "<td><i>$tag{'age'}</i></td>\n" .
1876                       ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1877                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1878                                -class => "list name"},esc_html($tag{'name'})) .
1879                       "</td>\n" .
1880                       "<td class=\"link\">" .
1881                       $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1882                       $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1883                       "</td>\n" .
1884                       "</tr>";
1885         }
1886         if (defined $extra) {
1887                 print "<tr>\n" .
1888                       "<td colspan=\"3\">$extra</td>\n" .
1889                       "</tr>\n";
1890         }
1891         print "</table>\n";
1894 ## ----------------------------------------------------------------------
1895 ## functions printing large fragments, format as one of arguments
1897 sub git_diff_print {
1898         my $from = shift;
1899         my $from_name = shift;
1900         my $to = shift;
1901         my $to_name = shift;
1902         my $format = shift || "html";
1904         my $from_tmp = "/dev/null";
1905         my $to_tmp = "/dev/null";
1906         my $pid = $$;
1908         # create tmp from-file
1909         if (defined $from) {
1910                 $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1911                 open my $fd2, "> $from_tmp";
1912                 open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1913                 my @file = <$fd>;
1914                 print $fd2 @file;
1915                 close $fd2;
1916                 close $fd;
1917         }
1919         # create tmp to-file
1920         if (defined $to) {
1921                 $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1922                 open my $fd2, "> $to_tmp";
1923                 open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1924                 my @file = <$fd>;
1925                 print $fd2 @file;
1926                 close $fd2;
1927                 close $fd;
1928         }
1930         open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1931         if ($format eq "plain") {
1932                 undef $/;
1933                 print <$fd>;
1934                 $/ = "\n";
1935         } else {
1936                 while (my $line = <$fd>) {
1937                         chomp $line;
1938                         my $char = substr($line, 0, 1);
1939                         my $diff_class = "";
1940                         if ($char eq '+') {
1941                                 $diff_class = " add";
1942                         } elsif ($char eq "-") {
1943                                 $diff_class = " rem";
1944                         } elsif ($char eq "@") {
1945                                 $diff_class = " chunk_header";
1946                         } elsif ($char eq "\\") {
1947                                 # skip errors
1948                                 next;
1949                         }
1950                         $line = untabify($line);
1951                         print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1952                 }
1953         }
1954         close $fd;
1956         if (defined $from) {
1957                 unlink($from_tmp);
1958         }
1959         if (defined $to) {
1960                 unlink($to_tmp);
1961         }
1965 ## ======================================================================
1966 ## ======================================================================
1967 ## actions
1969 sub git_project_list {
1970         my $order = $cgi->param('o');
1971         if (defined $order && $order !~ m/project|descr|owner|age/) {
1972                 die_error(undef, "Unknown order parameter");
1973         }
1975         my @list = git_get_projects_list();
1976         my @projects;
1977         if (!@list) {
1978                 die_error(undef, "No projects found");
1979         }
1980         foreach my $pr (@list) {
1981                 my $head = git_get_head_hash($pr->{'path'});
1982                 if (!defined $head) {
1983                         next;
1984                 }
1985                 $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1986                 my %co = parse_commit($head);
1987                 if (!%co) {
1988                         next;
1989                 }
1990                 $pr->{'commit'} = \%co;
1991                 if (!defined $pr->{'descr'}) {
1992                         my $descr = git_get_project_description($pr->{'path'}) || "";
1993                         $pr->{'descr'} = chop_str($descr, 25, 5);
1994                 }
1995                 if (!defined $pr->{'owner'}) {
1996                         $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1997                 }
1998                 push @projects, $pr;
1999         }
2001         git_header_html();
2002         if (-f $home_text) {
2003                 print "<div class=\"index_include\">\n";
2004                 open (my $fd, $home_text);
2005                 print <$fd>;
2006                 close $fd;
2007                 print "</div>\n";
2008         }
2009         print "<table class=\"project_list\">\n" .
2010               "<tr>\n";
2011         $order ||= "project";
2012         if ($order eq "project") {
2013                 @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
2014                 print "<th>Project</th>\n";
2015         } else {
2016                 print "<th>" .
2017                       $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
2018                                -class => "header"}, "Project") .
2019                       "</th>\n";
2020         }
2021         if ($order eq "descr") {
2022                 @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
2023                 print "<th>Description</th>\n";
2024         } else {
2025                 print "<th>" .
2026                       $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
2027                                -class => "header"}, "Description") .
2028                       "</th>\n";
2029         }
2030         if ($order eq "owner") {
2031                 @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
2032                 print "<th>Owner</th>\n";
2033         } else {
2034                 print "<th>" .
2035                       $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
2036                                -class => "header"}, "Owner") .
2037                       "</th>\n";
2038         }
2039         if ($order eq "age") {
2040                 @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
2041                 print "<th>Last Change</th>\n";
2042         } else {
2043                 print "<th>" .
2044                       $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
2045                                -class => "header"}, "Last Change") .
2046                       "</th>\n";
2047         }
2048         print "<th></th>\n" .
2049               "</tr>\n";
2050         my $alternate = 0;
2051         foreach my $pr (@projects) {
2052                 if ($alternate) {
2053                         print "<tr class=\"dark\">\n";
2054                 } else {
2055                         print "<tr class=\"light\">\n";
2056                 }
2057                 $alternate ^= 1;
2058                 print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2059                                         -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2060                       "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2061                       "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2062                 print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2063                       $pr->{'commit'}{'age_string'} . "</td>\n" .
2064                       "<td class=\"link\">" .
2065                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
2066                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2067                       $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2068                       "</td>\n" .
2069                       "</tr>\n";
2070         }
2071         print "</table>\n";
2072         git_footer_html();
2075 sub git_summary {
2076         my $descr = git_get_project_description($project) || "none";
2077         my $head = git_get_head_hash($project);
2078         my %co = parse_commit($head);
2079         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2081         my $owner = git_get_project_owner($project);
2083         my $refs = git_get_references();
2084         git_header_html();
2085         git_print_page_nav('summary','', $head);
2087         print "<div class=\"title\">&nbsp;</div>\n";
2088         print "<table cellspacing=\"0\">\n" .
2089               "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2090               "<tr><td>owner</td><td>$owner</td></tr>\n" .
2091               "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2092         # use per project git URL list in $projectroot/$project/cloneurl
2093         # or make project git URL from git base URL and project name
2094         my $url_tag = "URL";
2095         my @url_list = git_get_project_url_list($project);
2096         @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2097         foreach my $git_url (@url_list) {
2098                 next unless $git_url;
2099                 print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2100                 $url_tag = "";
2101         }
2102         print "</table>\n";
2104         open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
2105                 or die_error(undef, "Open git-rev-list failed");
2106         my @revlist = map { chomp; $_ } <$fd>;
2107         close $fd;
2108         git_print_header_div('shortlog');
2109         git_shortlog_body(\@revlist, 0, 15, $refs,
2110                           $cgi->a({-href => href(action=>"shortlog")}, "..."));
2112         my $taglist = git_get_refs_list("refs/tags");
2113         if (defined @$taglist) {
2114                 git_print_header_div('tags');
2115                 git_tags_body($taglist, 0, 15,
2116                               $cgi->a({-href => href(action=>"tags")}, "..."));
2117         }
2119         my $headlist = git_get_refs_list("refs/heads");
2120         if (defined @$headlist) {
2121                 git_print_header_div('heads');
2122                 git_heads_body($headlist, $head, 0, 15,
2123                                $cgi->a({-href => href(action=>"heads")}, "..."));
2124         }
2126         git_footer_html();
2129 sub git_tag {
2130         my $head = git_get_head_hash($project);
2131         git_header_html();
2132         git_print_page_nav('','', $head,undef,$head);
2133         my %tag = parse_tag($hash);
2134         git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2135         print "<div class=\"title_text\">\n" .
2136               "<table cellspacing=\"0\">\n" .
2137               "<tr>\n" .
2138               "<td>object</td>\n" .
2139               "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2140                                $tag{'object'}) . "</td>\n" .
2141               "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2142                                               $tag{'type'}) . "</td>\n" .
2143               "</tr>\n";
2144         if (defined($tag{'author'})) {
2145                 my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2146                 print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2147                 print "<tr><td></td><td>" . $ad{'rfc2822'} .
2148                         sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2149                         "</td></tr>\n";
2150         }
2151         print "</table>\n\n" .
2152               "</div>\n";
2153         print "<div class=\"page_body\">";
2154         my $comment = $tag{'comment'};
2155         foreach my $line (@$comment) {
2156                 print esc_html($line) . "<br/>\n";
2157         }
2158         print "</div>\n";
2159         git_footer_html();
2162 sub git_blame2 {
2163         my $fd;
2164         my $ftype;
2166         if (!gitweb_check_feature('blame')) {
2167                 die_error('403 Permission denied', "Permission denied");
2168         }
2169         die_error('404 Not Found', "File name not defined") if (!$file_name);
2170         $hash_base ||= git_get_head_hash($project);
2171         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2172         my %co = parse_commit($hash_base)
2173                 or die_error(undef, "Reading commit failed");
2174         if (!defined $hash) {
2175                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2176                         or die_error(undef, "Error looking up file");
2177         }
2178         $ftype = git_get_type($hash);
2179         if ($ftype !~ "blob") {
2180                 die_error("400 Bad Request", "Object is not a blob");
2181         }
2182         open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2183                 or die_error(undef, "Open git-blame failed");
2184         git_header_html();
2185         my $formats_nav =
2186                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2187                         "blob") .
2188                 " | " .
2189                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2190                         "head");
2191         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2192         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2193         git_print_page_path($file_name, $ftype, $hash_base);
2194         my @rev_color = (qw(light2 dark2));
2195         my $num_colors = scalar(@rev_color);
2196         my $current_color = 0;
2197         my $last_rev;
2198         print <<HTML;
2199 <div class="page_body">
2200 <table class="blame">
2201 <tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2202 HTML
2203         while (<$fd>) {
2204                 /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2205                 my $full_rev = $1;
2206                 my $rev = substr($full_rev, 0, 8);
2207                 my $lineno = $2;
2208                 my $data = $3;
2210                 if (!defined $last_rev) {
2211                         $last_rev = $full_rev;
2212                 } elsif ($last_rev ne $full_rev) {
2213                         $last_rev = $full_rev;
2214                         $current_color = ++$current_color % $num_colors;
2215                 }
2216                 print "<tr class=\"$rev_color[$current_color]\">\n";
2217                 print "<td class=\"sha1\">" .
2218                         $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2219                                 esc_html($rev)) . "</td>\n";
2220                 print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2221                       esc_html($lineno) . "</a></td>\n";
2222                 print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2223                 print "</tr>\n";
2224         }
2225         print "</table>\n";
2226         print "</div>";
2227         close $fd
2228                 or print "Reading blob failed\n";
2229         git_footer_html();
2232 sub git_blame {
2233         my $fd;
2235         if (!gitweb_check_feature('blame')) {
2236                 die_error('403 Permission denied', "Permission denied");
2237         }
2238         die_error('404 Not Found', "File name not defined") if (!$file_name);
2239         $hash_base ||= git_get_head_hash($project);
2240         die_error(undef, "Couldn't find base commit") unless ($hash_base);
2241         my %co = parse_commit($hash_base)
2242                 or die_error(undef, "Reading commit failed");
2243         if (!defined $hash) {
2244                 $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2245                         or die_error(undef, "Error lookup file");
2246         }
2247         open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2248                 or die_error(undef, "Open git-annotate failed");
2249         git_header_html();
2250         my $formats_nav =
2251                 $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2252                         "blob") .
2253                 " | " .
2254                 $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2255                         "head");
2256         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2257         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2258         git_print_page_path($file_name, 'blob', $hash_base);
2259         print "<div class=\"page_body\">\n";
2260         print <<HTML;
2261 <table class="blame">
2262   <tr>
2263     <th>Commit</th>
2264     <th>Age</th>
2265     <th>Author</th>
2266     <th>Line</th>
2267     <th>Data</th>
2268   </tr>
2269 HTML
2270         my @line_class = (qw(light dark));
2271         my $line_class_len = scalar (@line_class);
2272         my $line_class_num = $#line_class;
2273         while (my $line = <$fd>) {
2274                 my $long_rev;
2275                 my $short_rev;
2276                 my $author;
2277                 my $time;
2278                 my $lineno;
2279                 my $data;
2280                 my $age;
2281                 my $age_str;
2282                 my $age_class;
2284                 chomp $line;
2285                 $line_class_num = ($line_class_num + 1) % $line_class_len;
2287                 if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2288                         $long_rev = $1;
2289                         $author   = $2;
2290                         $time     = $3;
2291                         $lineno   = $4;
2292                         $data     = $5;
2293                 } else {
2294                         print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2295                         next;
2296                 }
2297                 $short_rev  = substr ($long_rev, 0, 8);
2298                 $age        = time () - $time;
2299                 $age_str    = age_string ($age);
2300                 $age_str    =~ s/ /&nbsp;/g;
2301                 $age_class  = age_class($age);
2302                 $author     = esc_html ($author);
2303                 $author     =~ s/ /&nbsp;/g;
2305                 $data = untabify($data);
2306                 $data = esc_html ($data);
2308                 print <<HTML;
2309   <tr class="$line_class[$line_class_num]">
2310     <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2311     <td class="$age_class">$age_str</td>
2312     <td>$author</td>
2313     <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2314     <td class="pre">$data</td>
2315   </tr>
2316 HTML
2317         } # while (my $line = <$fd>)
2318         print "</table>\n\n";
2319         close $fd
2320                 or print "Reading blob failed.\n";
2321         print "</div>";
2322         git_footer_html();
2325 sub git_tags {
2326         my $head = git_get_head_hash($project);
2327         git_header_html();
2328         git_print_page_nav('','', $head,undef,$head);
2329         git_print_header_div('summary', $project);
2331         my $taglist = git_get_refs_list("refs/tags");
2332         if (defined @$taglist) {
2333                 git_tags_body($taglist);
2334         }
2335         git_footer_html();
2338 sub git_heads {
2339         my $head = git_get_head_hash($project);
2340         git_header_html();
2341         git_print_page_nav('','', $head,undef,$head);
2342         git_print_header_div('summary', $project);
2344         my $taglist = git_get_refs_list("refs/heads");
2345         if (defined @$taglist) {
2346                 git_heads_body($taglist, $head);
2347         }
2348         git_footer_html();
2351 sub git_blob_plain {
2352         if (!defined $hash) {
2353                 if (defined $file_name) {
2354                         my $base = $hash_base || git_get_head_hash($project);
2355                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2356                                 or die_error(undef, "Error lookup file");
2357                 } else {
2358                         die_error(undef, "No file name defined");
2359                 }
2360         }
2361         my $type = shift;
2362         open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2363                 or die_error(undef, "Couldn't cat $file_name, $hash");
2365         $type ||= blob_mimetype($fd, $file_name);
2367         # save as filename, even when no $file_name is given
2368         my $save_as = "$hash";
2369         if (defined $file_name) {
2370                 $save_as = $file_name;
2371         } elsif ($type =~ m/^text\//) {
2372                 $save_as .= '.txt';
2373         }
2375         print $cgi->header(-type => "$type",
2376                            -content_disposition => "inline; filename=\"$save_as\"");
2377         undef $/;
2378         binmode STDOUT, ':raw';
2379         print <$fd>;
2380         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2381         $/ = "\n";
2382         close $fd;
2385 sub git_blob {
2386         if (!defined $hash) {
2387                 if (defined $file_name) {
2388                         my $base = $hash_base || git_get_head_hash($project);
2389                         $hash = git_get_hash_by_path($base, $file_name, "blob")
2390                                 or die_error(undef, "Error lookup file");
2391                 } else {
2392                         die_error(undef, "No file name defined");
2393                 }
2394         }
2395         my $have_blame = gitweb_check_feature('blame');
2396         open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2397                 or die_error(undef, "Couldn't cat $file_name, $hash");
2398         my $mimetype = blob_mimetype($fd, $file_name);
2399         if ($mimetype !~ m/^text\//) {
2400                 close $fd;
2401                 return git_blob_plain($mimetype);
2402         }
2403         git_header_html();
2404         my $formats_nav = '';
2405         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2406                 if (defined $file_name) {
2407                         if ($have_blame) {
2408                                 $formats_nav .=
2409                                         $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2410                                                                hash=>$hash, file_name=>$file_name)},
2411                                                 "blame") .
2412                                         " | ";
2413                         }
2414                         $formats_nav .=
2415                                 $cgi->a({-href => href(action=>"blob_plain",
2416                                                        hash=>$hash, file_name=>$file_name)},
2417                                         "plain") .
2418                                 " | " .
2419                                 $cgi->a({-href => href(action=>"blob",
2420                                                        hash_base=>"HEAD", file_name=>$file_name)},
2421                                         "head");
2422                 } else {
2423                         $formats_nav .=
2424                                 $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2425                 }
2426                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2427                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2428         } else {
2429                 print "<div class=\"page_nav\">\n" .
2430                       "<br/><br/></div>\n" .
2431                       "<div class=\"title\">$hash</div>\n";
2432         }
2433         git_print_page_path($file_name, "blob", $hash_base);
2434         print "<div class=\"page_body\">\n";
2435         my $nr;
2436         while (my $line = <$fd>) {
2437                 chomp $line;
2438                 $nr++;
2439                 $line = untabify($line);
2440                 printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2441                        $nr, $nr, $nr, esc_html($line);
2442         }
2443         close $fd
2444                 or print "Reading blob failed.\n";
2445         print "</div>";
2446         git_footer_html();
2449 sub git_tree {
2450         if (!defined $hash) {
2451                 $hash = git_get_head_hash($project);
2452                 if (defined $file_name) {
2453                         my $base = $hash_base || $hash;
2454                         $hash = git_get_hash_by_path($base, $file_name, "tree");
2455                 }
2456                 if (!defined $hash_base) {
2457                         $hash_base = $hash;
2458                 }
2459         }
2460         $/ = "\0";
2461         open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2462                 or die_error(undef, "Open git-ls-tree failed");
2463         my @entries = map { chomp; $_ } <$fd>;
2464         close $fd or die_error(undef, "Reading tree failed");
2465         $/ = "\n";
2467         my $refs = git_get_references();
2468         my $ref = format_ref_marker($refs, $hash_base);
2469         git_header_html();
2470         my %base_key = ();
2471         my $base = "";
2472         my $have_blame = gitweb_check_feature('blame');
2473         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2474                 $base_key{hash_base} = $hash_base;
2475                 git_print_page_nav('tree','', $hash_base);
2476                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2477         } else {
2478                 print "<div class=\"page_nav\">\n";
2479                 print "<br/><br/></div>\n";
2480                 print "<div class=\"title\">$hash</div>\n";
2481         }
2482         if (defined $file_name) {
2483                 $base = esc_html("$file_name/");
2484         }
2485         git_print_page_path($file_name, 'tree', $hash_base);
2486         print "<div class=\"page_body\">\n";
2487         print "<table cellspacing=\"0\">\n";
2488         my $alternate = 0;
2489         foreach my $line (@entries) {
2490                 #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
2491                 $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2492                 my $t_mode = $1;
2493                 my $t_type = $2;
2494                 my $t_hash = $3;
2495                 my $t_name = validate_input($4);
2496                 if ($alternate) {
2497                         print "<tr class=\"dark\">\n";
2498                 } else {
2499                         print "<tr class=\"light\">\n";
2500                 }
2501                 $alternate ^= 1;
2502                 print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2503                 if ($t_type eq "blob") {
2504                         print "<td class=\"list\">" .
2505                               $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2506                                       -class => "list"}, esc_html($t_name)) .
2507                               "</td>\n" .
2508                               "<td class=\"link\">" .
2509                               $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2510                                       "blob");
2511                         if ($have_blame) {
2512                                 print " | " .
2513                                         $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2514                                                 "blame");
2515                         }
2516                         print " | " .
2517                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2518                                                      hash=>$t_hash, file_name=>"$base$t_name")},
2519                                       "history") .
2520                               " | " .
2521                               $cgi->a({-href => href(action=>"blob_plain",
2522                                                      hash=>$t_hash, file_name=>"$base$t_name")},
2523                                       "raw") .
2524                               "</td>\n";
2525                 } elsif ($t_type eq "tree") {
2526                         print "<td class=\"list\">" .
2527                               $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2528                                       esc_html($t_name)) .
2529                               "</td>\n" .
2530                               "<td class=\"link\">" .
2531                               $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2532                                       "tree") .
2533                               " | " .
2534                               $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2535                                       "history") .
2536                               "</td>\n";
2537                 }
2538                 print "</tr>\n";
2539         }
2540         print "</table>\n" .
2541               "</div>";
2542         git_footer_html();
2545 sub git_snapshot {
2547         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2548         my $have_snapshot = (defined $ctype && defined $suffix);
2549         if (!$have_snapshot) {
2550                 die_error('403 Permission denied', "Permission denied");
2551         }
2553         if (!defined $hash) {
2554                 $hash = git_get_head_hash($project);
2555         }
2557         my $filename = basename($project) . "-$hash.tar.$suffix";
2559         print $cgi->header(-type => 'application/x-tar',
2560                            -content_encoding => $ctype,
2561                            -content_disposition => "inline; filename=\"$filename\"",
2562                            -status => '200 OK');
2564         open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2565                 die_error(undef, "Execute git-tar-tree failed.");
2566         binmode STDOUT, ':raw';
2567         print <$fd>;
2568         binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2569         close $fd;
2573 sub git_log {
2574         my $head = git_get_head_hash($project);
2575         if (!defined $hash) {
2576                 $hash = $head;
2577         }
2578         if (!defined $page) {
2579                 $page = 0;
2580         }
2581         my $refs = git_get_references();
2583         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2584         open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2585                 or die_error(undef, "Open git-rev-list failed");
2586         my @revlist = map { chomp; $_ } <$fd>;
2587         close $fd;
2589         my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2591         git_header_html();
2592         git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2594         if (!@revlist) {
2595                 my %co = parse_commit($hash);
2597                 git_print_header_div('summary', $project);
2598                 print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2599         }
2600         for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2601                 my $commit = $revlist[$i];
2602                 my $ref = format_ref_marker($refs, $commit);
2603                 my %co = parse_commit($commit);
2604                 next if !%co;
2605                 my %ad = parse_date($co{'author_epoch'});
2606                 git_print_header_div('commit',
2607                                "<span class=\"age\">$co{'age_string'}</span>" .
2608                                esc_html($co{'title'}) . $ref,
2609                                $commit);
2610                 print "<div class=\"title_text\">\n" .
2611                       "<div class=\"log_link\">\n" .
2612                       $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2613                       " | " .
2614                       $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2615                       "<br/>\n" .
2616                       "</div>\n" .
2617                       "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
2618                       "</div>\n";
2620                 print "<div class=\"log_body\">\n";
2621                 git_print_simplified_log($co{'comment'});
2622                 print "</div>\n";
2623         }
2624         git_footer_html();
2627 sub git_commit {
2628         my %co = parse_commit($hash);
2629         if (!%co) {
2630                 die_error(undef, "Unknown commit object");
2631         }
2632         my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2633         my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2635         my $parent = $co{'parent'};
2636         if (!defined $parent) {
2637                 $parent = "--root";
2638         }
2639         open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2640                 or die_error(undef, "Open git-diff-tree failed");
2641         my @difftree = map { chomp; $_ } <$fd>;
2642         close $fd or die_error(undef, "Reading git-diff-tree failed");
2644         # non-textual hash id's can be cached
2645         my $expires;
2646         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2647                 $expires = "+1d";
2648         }
2649         my $refs = git_get_references();
2650         my $ref = format_ref_marker($refs, $co{'id'});
2652         my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2653         my $have_snapshot = (defined $ctype && defined $suffix);
2655         my $formats_nav = '';
2656         if (defined $file_name && defined $co{'parent'}) {
2657                 my $parent = $co{'parent'};
2658                 $formats_nav .=
2659                         $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2660                                 "blame");
2661         }
2662         git_header_html(undef, $expires);
2663         git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2664                            $hash, $co{'tree'}, $hash,
2665                            $formats_nav);
2667         if (defined $co{'parent'}) {
2668                 git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2669         } else {
2670                 git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2671         }
2672         print "<div class=\"title_text\">\n" .
2673               "<table cellspacing=\"0\">\n";
2674         print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2675               "<tr>" .
2676               "<td></td><td> $ad{'rfc2822'}";
2677         if ($ad{'hour_local'} < 6) {
2678                 printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2679                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2680         } else {
2681                 printf(" (%02d:%02d %s)",
2682                        $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2683         }
2684         print "</td>" .
2685               "</tr>\n";
2686         print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2687         print "<tr><td></td><td> $cd{'rfc2822'}" .
2688               sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2689               "</td></tr>\n";
2690         print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2691         print "<tr>" .
2692               "<td>tree</td>" .
2693               "<td class=\"sha1\">" .
2694               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2695                        class => "list"}, $co{'tree'}) .
2696               "</td>" .
2697               "<td class=\"link\">" .
2698               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2699                       "tree");
2700         if ($have_snapshot) {
2701                 print " | " .
2702                       $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2703         }
2704         print "</td>" .
2705               "</tr>\n";
2706         my $parents = $co{'parents'};
2707         foreach my $par (@$parents) {
2708                 print "<tr>" .
2709                       "<td>parent</td>" .
2710                       "<td class=\"sha1\">" .
2711                       $cgi->a({-href => href(action=>"commit", hash=>$par),
2712                                class => "list"}, $par) .
2713                       "</td>" .
2714                       "<td class=\"link\">" .
2715                       $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2716                       " | " .
2717                       $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2718                       "</td>" .
2719                       "</tr>\n";
2720         }
2721         print "</table>".
2722               "</div>\n";
2724         print "<div class=\"page_body\">\n";
2725         git_print_log($co{'comment'});
2726         print "</div>\n";
2728         git_difftree_body(\@difftree, $hash, $parent);
2730         git_footer_html();
2733 sub git_blobdiff {
2734         mkdir($git_temp, 0700);
2735         git_header_html();
2736         if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2737                 my $formats_nav =
2738                         $cgi->a({-href => href(action=>"blobdiff_plain",
2739                                                hash=>$hash, hash_parent=>$hash_parent)},
2740                                 "plain");
2741                 git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2742                 git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2743         } else {
2744                 print <<HTML;
2745 <div class="page_nav"><br/><br/></div>
2746 <div class="title">$hash vs $hash_parent</div>
2747 HTML
2748         }
2749         git_print_page_path($file_name, "blob", $hash_base);
2750         print "<div class=\"page_body\">\n" .
2751               "<div class=\"diff_info\">blob:" .
2752               $cgi->a({-href => href(action=>"blob", hash=>$hash_parent,
2753                                      hash_base=>$hash_base, file_name=>($file_parent || $file_name))},
2754                       $hash_parent) .
2755               " -> blob:" .
2756               $cgi->a({-href => href(action=>"blob", hash=>$hash,
2757                                      hash_base=>$hash_base, file_name=>$file_name)},
2758                       $hash) .
2759               "</div>\n";
2760         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2761         print "</div>"; # page_body
2762         git_footer_html();
2765 sub git_blobdiff_plain {
2766         mkdir($git_temp, 0700);
2767         print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2768         git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2771 sub git_commitdiff {
2772         my $format = shift || 'html';
2773         my %co = parse_commit($hash);
2774         if (!%co) {
2775                 die_error(undef, "Unknown commit object");
2776         }
2777         if (!defined $hash_parent) {
2778                 $hash_parent = $co{'parent'} || '--root';
2779         }
2781         # read commitdiff
2782         my $fd;
2783         my @difftree;
2784         if ($format eq 'html') {
2785                 open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C',
2786                         "--patch-with-raw", "--full-index", $hash_parent, $hash
2787                         or die_error(undef, "Open git-diff-tree failed");
2789                 while (chomp(my $line = <$fd>)) {
2790                         # empty line ends raw part of diff-tree output
2791                         last unless $line;
2792                         push @difftree, $line;
2793                 }
2795         } elsif ($format eq 'plain') {
2796                 open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-B', $hash_parent, $hash
2797                         or die_error(undef, "Open git-diff-tree failed");
2799         } else {
2800                 die_error(undef, "Unknown commitdiff format");
2801         }
2803         # non-textual hash id's can be cached
2804         my $expires;
2805         if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2806                 $expires = "+1d";
2807         }
2809         # write commit message
2810         if ($format eq 'html') {
2811                 my $refs = git_get_references();
2812                 my $ref = format_ref_marker($refs, $co{'id'});
2813                 my $formats_nav =
2814                         $cgi->a({-href => href(action=>"commitdiff_plain",
2815                                                hash=>$hash, hash_parent=>$hash_parent)},
2816                                 "plain");
2818                 git_header_html(undef, $expires);
2819                 git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2820                 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2821                 print "<div class=\"page_body\">\n";
2822                 print "<div class=\"log\">\n";
2823                 git_print_simplified_log($co{'comment'}, 1); # skip title
2824                 print "</div>\n"; # class="log"
2826         } elsif ($format eq 'plain') {
2827                 my $refs = git_get_references("tags");
2828                 my @tagnames;
2829                 if (exists $refs->{$hash}) {
2830                         @tagnames = map { s|^tags/|| } $refs->{$hash};
2831                 }
2832                 my $filename = basename($project) . "-$hash.patch";
2834                 print $cgi->header(
2835                         -type => 'text/plain',
2836                         -charset => 'utf-8',
2837                         -expires => $expires,
2838                         -content_disposition => qq(inline; filename="$filename"));
2839                 my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2840                 print <<TEXT;
2841 From: $co{'author'}
2842 Date: $ad{'rfc2822'} ($ad{'tz_local'})
2843 Subject: $co{'title'}
2844 TEXT
2845                 foreach my $tag (@tagnames) {
2846                         print "X-Git-Tag: $tag\n";
2847                 }
2848                 print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2849                 foreach my $line (@{$co{'comment'}}) {
2850                         print "$line\n";
2851                 }
2852                 print "---\n\n";
2853         }
2855         # write patch
2856         if ($format eq 'html') {
2857                 #git_difftree_body(\@difftree, $hash, $hash_parent);
2858                 #print "<br/>\n";
2860                 git_patchset_body($fd, \@difftree, $hash, $hash_parent);
2861                 close $fd;
2862                 print "</div>\n"; # class="page_body"
2863                 git_footer_html();
2865         } elsif ($format eq 'plain') {
2866                 local $/ = undef;
2867                 print <$fd>;
2868                 close $fd
2869                         or print "Reading git-diff-tree failed\n";
2870         }
2873 sub git_commitdiff_plain {
2874         git_commitdiff('plain');
2877 sub git_history {
2878         if (!defined $hash_base) {
2879                 $hash_base = git_get_head_hash($project);
2880         }
2881         my $ftype;
2882         my %co = parse_commit($hash_base);
2883         if (!%co) {
2884                 die_error(undef, "Unknown commit object");
2885         }
2886         my $refs = git_get_references();
2887         git_header_html();
2888         git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2889         git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2890         if (!defined $hash && defined $file_name) {
2891                 $hash = git_get_hash_by_path($hash_base, $file_name);
2892         }
2893         if (defined $hash) {
2894                 $ftype = git_get_type($hash);
2895         }
2896         git_print_page_path($file_name, $ftype, $hash_base);
2898         open my $fd, "-|",
2899                 $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2900         git_history_body($fd, $refs, $hash_base, $ftype);
2902         close $fd;
2903         git_footer_html();
2906 sub git_search {
2907         if (!defined $searchtext) {
2908                 die_error(undef, "Text field empty");
2909         }
2910         if (!defined $hash) {
2911                 $hash = git_get_head_hash($project);
2912         }
2913         my %co = parse_commit($hash);
2914         if (!%co) {
2915                 die_error(undef, "Unknown commit object");
2916         }
2917         # pickaxe may take all resources of your box and run for several minutes
2918         # with every query - so decide by yourself how public you make this feature :)
2919         my $commit_search = 1;
2920         my $author_search = 0;
2921         my $committer_search = 0;
2922         my $pickaxe_search = 0;
2923         if ($searchtext =~ s/^author\\://i) {
2924                 $author_search = 1;
2925         } elsif ($searchtext =~ s/^committer\\://i) {
2926                 $committer_search = 1;
2927         } elsif ($searchtext =~ s/^pickaxe\\://i) {
2928                 $commit_search = 0;
2929                 $pickaxe_search = 1;
2930         }
2931         git_header_html();
2932         git_print_page_nav('','', $hash,$co{'tree'},$hash);
2933         git_print_header_div('commit', esc_html($co{'title'}), $hash);
2935         print "<table cellspacing=\"0\">\n";
2936         my $alternate = 0;
2937         if ($commit_search) {
2938                 $/ = "\0";
2939                 open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2940                 while (my $commit_text = <$fd>) {
2941                         if (!grep m/$searchtext/i, $commit_text) {
2942                                 next;
2943                         }
2944                         if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2945                                 next;
2946                         }
2947                         if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2948                                 next;
2949                         }
2950                         my @commit_lines = split "\n", $commit_text;
2951                         my %co = parse_commit(undef, \@commit_lines);
2952                         if (!%co) {
2953                                 next;
2954                         }
2955                         if ($alternate) {
2956                                 print "<tr class=\"dark\">\n";
2957                         } else {
2958                                 print "<tr class=\"light\">\n";
2959                         }
2960                         $alternate ^= 1;
2961                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2962                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2963                               "<td>" .
2964                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2965                                        esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2966                         my $comment = $co{'comment'};
2967                         foreach my $line (@$comment) {
2968                                 if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2969                                         my $lead = esc_html($1) || "";
2970                                         $lead = chop_str($lead, 30, 10);
2971                                         my $match = esc_html($2) || "";
2972                                         my $trail = esc_html($3) || "";
2973                                         $trail = chop_str($trail, 30, 10);
2974                                         my $text = "$lead<span class=\"match\">$match</span>$trail";
2975                                         print chop_str($text, 80, 5) . "<br/>\n";
2976                                 }
2977                         }
2978                         print "</td>\n" .
2979                               "<td class=\"link\">" .
2980                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2981                               " | " .
2982                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2983                         print "</td>\n" .
2984                               "</tr>\n";
2985                 }
2986                 close $fd;
2987         }
2989         if ($pickaxe_search) {
2990                 $/ = "\n";
2991                 open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2992                 undef %co;
2993                 my @files;
2994                 while (my $line = <$fd>) {
2995                         if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2996                                 my %set;
2997                                 $set{'file'} = $6;
2998                                 $set{'from_id'} = $3;
2999                                 $set{'to_id'} = $4;
3000                                 $set{'id'} = $set{'to_id'};
3001                                 if ($set{'id'} =~ m/0{40}/) {
3002                                         $set{'id'} = $set{'from_id'};
3003                                 }
3004                                 if ($set{'id'} =~ m/0{40}/) {
3005                                         next;
3006                                 }
3007                                 push @files, \%set;
3008                         } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
3009                                 if (%co) {
3010                                         if ($alternate) {
3011                                                 print "<tr class=\"dark\">\n";
3012                                         } else {
3013                                                 print "<tr class=\"light\">\n";
3014                                         }
3015                                         $alternate ^= 1;
3016                                         print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
3017                                               "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
3018                                               "<td>" .
3019                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
3020                                                       -class => "list subject"},
3021                                                       esc_html(chop_str($co{'title'}, 50)) . "<br/>");
3022                                         while (my $setref = shift @files) {
3023                                                 my %set = %$setref;
3024                                                 print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
3025                                                                              hash=>$set{'id'}, file_name=>$set{'file'}),
3026                                                               -class => "list"},
3027                                                               "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
3028                                                       "<br/>\n";
3029                                         }
3030                                         print "</td>\n" .
3031                                               "<td class=\"link\">" .
3032                                               $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
3033                                               " | " .
3034                                               $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
3035                                         print "</td>\n" .
3036                                               "</tr>\n";
3037                                 }
3038                                 %co = parse_commit($1);
3039                         }
3040                 }
3041                 close $fd;
3042         }
3043         print "</table>\n";
3044         git_footer_html();
3047 sub git_shortlog {
3048         my $head = git_get_head_hash($project);
3049         if (!defined $hash) {
3050                 $hash = $head;
3051         }
3052         if (!defined $page) {
3053                 $page = 0;
3054         }
3055         my $refs = git_get_references();
3057         my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3058         open my $fd, "-|", $GIT, "rev-list", $limit, $hash
3059                 or die_error(undef, "Open git-rev-list failed");
3060         my @revlist = map { chomp; $_ } <$fd>;
3061         close $fd;
3063         my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3064         my $next_link = '';
3065         if ($#revlist >= (100 * ($page+1)-1)) {
3066                 $next_link =
3067                         $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3068                                  -title => "Alt-n"}, "next");
3069         }
3072         git_header_html();
3073         git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3074         git_print_header_div('summary', $project);
3076         git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3078         git_footer_html();
3081 ## ......................................................................
3082 ## feeds (RSS, OPML)
3084 sub git_rss {
3085         # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3086         open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
3087                 or die_error(undef, "Open git-rev-list failed");
3088         my @revlist = map { chomp; $_ } <$fd>;
3089         close $fd or die_error(undef, "Reading git-rev-list failed");
3090         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3091         print <<XML;
3092 <?xml version="1.0" encoding="utf-8"?>
3093 <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3094 <channel>
3095 <title>$project $my_uri $my_url</title>
3096 <link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3097 <description>$project log</description>
3098 <language>en</language>
3099 XML
3101         for (my $i = 0; $i <= $#revlist; $i++) {
3102                 my $commit = $revlist[$i];
3103                 my %co = parse_commit($commit);
3104                 # we read 150, we always show 30 and the ones more recent than 48 hours
3105                 if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3106                         last;
3107                 }
3108                 my %cd = parse_date($co{'committer_epoch'});
3109                 open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
3110                 my @difftree = map { chomp; $_ } <$fd>;
3111                 close $fd or next;
3112                 print "<item>\n" .
3113                       "<title>" .
3114                       sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3115                       "</title>\n" .
3116                       "<author>" . esc_html($co{'author'}) . "</author>\n" .
3117                       "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3118                       "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3119                       "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3120                       "<description>" . esc_html($co{'title'}) . "</description>\n" .
3121                       "<content:encoded>" .
3122                       "<![CDATA[\n";
3123                 my $comment = $co{'comment'};
3124                 foreach my $line (@$comment) {
3125                         $line = decode("utf8", $line, Encode::FB_DEFAULT);
3126                         print "$line<br/>\n";
3127                 }
3128                 print "<br/>\n";
3129                 foreach my $line (@difftree) {
3130                         if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3131                                 next;
3132                         }
3133                         my $file = validate_input(unquote($7));
3134                         $file = decode("utf8", $file, Encode::FB_DEFAULT);
3135                         print "$file<br/>\n";
3136                 }
3137                 print "]]>\n" .
3138                       "</content:encoded>\n" .
3139                       "</item>\n";
3140         }
3141         print "</channel></rss>";
3144 sub git_opml {
3145         my @list = git_get_projects_list();
3147         print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3148         print <<XML;
3149 <?xml version="1.0" encoding="utf-8"?>
3150 <opml version="1.0">
3151 <head>
3152   <title>$site_name Git OPML Export</title>
3153 </head>
3154 <body>
3155 <outline text="git RSS feeds">
3156 XML
3158         foreach my $pr (@list) {
3159                 my %proj = %$pr;
3160                 my $head = git_get_head_hash($proj{'path'});
3161                 if (!defined $head) {
3162                         next;
3163                 }
3164                 $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3165                 my %co = parse_commit($head);
3166                 if (!%co) {
3167                         next;
3168                 }
3170                 my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3171                 my $rss  = "$my_url?p=$proj{'path'};a=rss";
3172                 my $html = "$my_url?p=$proj{'path'};a=summary";
3173                 print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3174         }
3175         print <<XML;
3176 </outline>
3177 </body>
3178 </opml>
3179 XML