1d3d9df752c67b35d603b70d7c12c69e14f62514
   1#!/usr/bin/perl
   2
   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
   9
  10use strict;
  11use warnings;
  12use CGI qw(:standard :escapeHTML -nosticky);
  13use CGI::Util qw(unescape);
  14use CGI::Carp qw(fatalsToBrowser);
  15use Encode;
  16use Fcntl ':mode';
  17use File::Find qw();
  18use File::Basename qw(basename);
  19binmode STDOUT, ':utf8';
  20
  21our $cgi = new CGI;
  22our $version = "++GIT_VERSION++";
  23our $my_url = $cgi->url();
  24our $my_uri = $cgi->url(-absolute => 1);
  25
  26# core git executable to use
  27# this can just be "git" if your webserver has a sensible PATH
  28our $GIT = "++GIT_BINDIR++/git";
  29
  30# absolute fs-path which will be prepended to the project path
  31#our $projectroot = "/pub/scm";
  32our $projectroot = "++GITWEB_PROJECTROOT++";
  33
  34# location for temporary files needed for diffs
  35our $git_temp = "/tmp/gitweb";
  36
  37# target of the home link on top of all pages
  38our $home_link = $my_uri || "/";
  39
  40# string of the home link on top of all pages
  41our $home_link_str = "++GITWEB_HOME_LINK_STR++";
  42
  43# name of your site or organization to appear in page titles
  44# replace this with something more descriptive for clearer bookmarks
  45our $site_name = "++GITWEB_SITENAME++" || $ENV{'SERVER_NAME'} || "Untitled";
  46
  47# html text to include at home page
  48our $home_text = "++GITWEB_HOMETEXT++";
  49
  50# URI of default stylesheet
  51our $stylesheet = "++GITWEB_CSS++";
  52# URI of GIT logo
  53our $logo = "++GITWEB_LOGO++";
  54
  55# source of projects list
  56our $projects_list = "++GITWEB_LIST++";
  57
  58# list of git base URLs used for URL to where fetch project from,
  59# i.e. full URL is "$git_base_url/$project"
  60our @git_base_url_list = ("++GITWEB_BASE_URL++");
  61
  62# default blob_plain mimetype and default charset for text/plain blob
  63our $default_blob_plain_mimetype = 'text/plain';
  64our $default_text_plain_charset  = undef;
  65
  66# file to use for guessing MIME types before trying /etc/mime.types
  67# (relative to the current git repository)
  68our $mimetypes_file = undef;
  69
  70# You define site-wide feature defaults here; override them with
  71# $GITWEB_CONFIG as necessary.
  72our %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
  76
  77        'blame' => {
  78                'sub' => \&feature_blame,
  79                'override' => 0,
  80                'default' => [0]},
  81
  82        'snapshot' => {
  83                'sub' => \&feature_snapshot,
  84                'override' => 0,
  85                #         => [content-encoding, suffix, program]
  86                'default' => ['x-gzip', 'gz', 'gzip']},
  87);
  88
  89sub 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}
  99
 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;
 105
 106sub feature_blame {
 107        my ($val) = git_get_project_config('blame', '--bool');
 108
 109        if ($val eq 'true') {
 110                return 1;
 111        } elsif ($val eq 'false') {
 112                return 0;
 113        }
 114
 115        return $_[0];
 116}
 117
 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
 123
 124sub feature_snapshot {
 125        my ($ctype, $suffix, $command) = @_;
 126
 127        my ($val) = git_get_project_config('snapshot');
 128
 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        }
 136
 137        return ($ctype, $suffix, $command);
 138}
 139
 140our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
 141require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
 142
 143# version of the core git binary
 144our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
 145
 146$projects_list ||= $projectroot;
 147if (! -d $git_temp) {
 148        mkdir($git_temp, 0700) || die_error(undef, "Couldn't mkdir $git_temp");
 149}
 150
 151# ======================================================================
 152# input validation and dispatch
 153our $action = $cgi->param('a');
 154if (defined $action) {
 155        if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
 156                die_error(undef, "Invalid action parameter");
 157        }
 158}
 159
 160our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
 161if (defined $project) {
 162        $project =~ s|^/||;
 163        $project =~ s|/$||;
 164        $project = undef unless $project;
 165}
 166if (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";
 177}
 178
 179our $file_name = $cgi->param('f');
 180if (defined $file_name) {
 181        if (!validate_input($file_name)) {
 182                die_error(undef, "Invalid file parameter");
 183        }
 184}
 185
 186our $file_parent = $cgi->param('fp');
 187if (defined $file_parent) {
 188        if (!validate_input($file_parent)) {
 189                die_error(undef, "Invalid file parent parameter");
 190        }
 191}
 192
 193our $hash = $cgi->param('h');
 194if (defined $hash) {
 195        if (!validate_input($hash)) {
 196                die_error(undef, "Invalid hash parameter");
 197        }
 198}
 199
 200our $hash_parent = $cgi->param('hp');
 201if (defined $hash_parent) {
 202        if (!validate_input($hash_parent)) {
 203                die_error(undef, "Invalid hash parent parameter");
 204        }
 205}
 206
 207our $hash_base = $cgi->param('hb');
 208if (defined $hash_base) {
 209        if (!validate_input($hash_base)) {
 210                die_error(undef, "Invalid hash base parameter");
 211        }
 212}
 213
 214our $page = $cgi->param('pg');
 215if (defined $page) {
 216        if ($page =~ m/[^0-9]$/) {
 217                die_error(undef, "Invalid page parameter");
 218        }
 219}
 220
 221our $searchtext = $cgi->param('s');
 222if (defined $searchtext) {
 223        if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
 224                die_error(undef, "Invalid search parameter");
 225        }
 226        $searchtext = quotemeta $searchtext;
 227}
 228
 229# dispatch
 230my %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);
 254
 255if (defined $project) {
 256        $action ||= 'summary';
 257} else {
 258        $action ||= 'project_list';
 259}
 260if (!defined($actions{$action})) {
 261        die_error(undef, "Unknown action");
 262}
 263$actions{$action}->();
 264exit;
 265
 266## ======================================================================
 267## action links
 268
 269sub href(%) {
 270        my %params = @_;
 271
 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;
 284
 285        $params{"project"} ||= $project;
 286
 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);
 295}
 296
 297
 298## ======================================================================
 299## validation, quoting/unquoting and escaping
 300
 301sub validate_input {
 302        my $input = shift;
 303
 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;
 314}
 315
 316# quote unsafe chars, but keep the slash, even when it's not
 317# correct, but quoted slashes look too horrible in bookmarks
 318sub 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;
 324}
 325
 326# replace invalid utf8 character with SUBSTITUTION sequence
 327sub 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;
 333}
 334
 335# git may return quoted and escaped filenames
 336sub 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;
 343}
 344
 345# escape tabs (convert tabs to spaces)
 346sub untabify {
 347        my $line = shift;
 348
 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        }
 355
 356        return $line;
 357}
 358
 359## ----------------------------------------------------------------------
 360## HTML aware string manipulation
 361
 362sub chop_str {
 363        my $str = shift;
 364        my $len = shift;
 365        my $add_len = shift || 10;
 366
 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";
 377}
 378
 379## ----------------------------------------------------------------------
 380## functions returning short strings
 381
 382# CSS class for given age value (in seconds)
 383sub age_class {
 384        my $age = shift;
 385
 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        }
 393}
 394
 395# convert age in seconds to "nn units ago" string
 396sub age_string {
 397        my $age = shift;
 398        my $age_str;
 399
 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;
 425}
 426
 427# convert file mode in octal to symbolic file mode string
 428sub mode_str {
 429        my $mode = oct shift;
 430
 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        }
 445}
 446
 447# convert file mode in octal to file type string
 448sub file_type {
 449        my $mode = oct shift;
 450
 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        }
 460}
 461
 462## ----------------------------------------------------------------------
 463## functions returning short HTML fragments, or transforming HTML fragments
 464## which don't beling to other sections
 465
 466# format line of commit message or tag comment
 467sub format_log_line_html {
 468        my $line = shift;
 469
 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;
 482}
 483
 484# format marker of refs pointing to given object
 485sub format_ref_marker {
 486        my ($refs, $id) = @_;
 487        my $markers = '';
 488
 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                        }
 500
 501                        $markers .= " <span class=\"$type\">" . esc_html($name) . "</span>";
 502                }
 503        }
 504
 505        if ($markers) {
 506                return ' <span class="refs">'. $markers . '</span>';
 507        } else {
 508                return "";
 509        }
 510}
 511
 512# format, perhaps shortened and with markers, title line
 513sub format_subject_html {
 514        my ($long, $short, $href, $extra) = @_;
 515        $extra = '' unless defined($extra);
 516
 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        }
 525}
 526
 527sub format_diff_line {
 528        my $line = shift;
 529        my $char = substr($line, 0, 1);
 530        my $diff_class = "";
 531
 532        chomp $line;
 533
 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";
 545}
 546
 547## ----------------------------------------------------------------------
 548## git utility subroutines, invoking git commands
 549
 550# get HEAD ref of given project as hash
 551sub 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;
 567}
 568
 569# get type of given object
 570sub git_get_type {
 571        my $hash = shift;
 572
 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;
 578}
 579
 580sub git_get_project_config {
 581        my ($key, $type) = @_;
 582
 583        return unless ($key);
 584        $key =~ s/^gitweb\.//;
 585        return if ($key =~ m/\W/);
 586
 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);
 594}
 595
 596# get hash of given path at given ref
 597sub git_get_hash_by_path {
 598        my $base = shift;
 599        my $path = shift || return undef;
 600
 601        my $tree = $base;
 602
 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;
 607
 608        #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
 609        $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
 610        return $3;
 611}
 612
 613## ......................................................................
 614## git utility functions, directly accessing git repository
 615
 616# assumes that PATH is not symref
 617sub git_get_hash_by_ref {
 618        my $path = shift;
 619
 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        }
 627}
 628
 629sub git_get_project_description {
 630        my $path = shift;
 631
 632        open my $fd, "$projectroot/$path/description" or return undef;
 633        my $descr = <$fd>;
 634        close $fd;
 635        chomp $descr;
 636        return $descr;
 637}
 638
 639sub git_get_project_url_list {
 640        my $path = shift;
 641
 642        open my $fd, "$projectroot/$path/cloneurl" or return undef;
 643        my @git_project_url_list = map { chomp; $_ } <$fd>;
 644        close $fd;
 645
 646        return wantarray ? @git_project_url_list : \@git_project_url_list;
 647}
 648
 649sub git_get_projects_list {
 650        my @list;
 651
 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;
 691}
 692
 693sub git_get_project_owner {
 694        my $project = shift;
 695        my $owner;
 696
 697        return undef unless $project;
 698
 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        }
 720
 721        return $owner;
 722}
 723
 724sub 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        }
 737
 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;
 750}
 751
 752## ----------------------------------------------------------------------
 753## parse to hash functions
 754
 755sub parse_date {
 756        my $epoch = shift;
 757        my $tz = shift || "-0000";
 758
 759        my %date;
 760        my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
 761        my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 762        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
 763        $date{'hour'} = $hour;
 764        $date{'minute'} = $min;
 765        $date{'mday'} = $mday;
 766        $date{'day'} = $days[$wday];
 767        $date{'month'} = $months[$mon];
 768        $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000",
 769                           $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
 770        $date{'mday-time'} = sprintf "%d %s %02d:%02d",
 771                             $mday, $months[$mon], $hour ,$min;
 772
 773        $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
 774        my $local = $epoch + ((int $1 + ($2/60)) * 3600);
 775        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
 776        $date{'hour_local'} = $hour;
 777        $date{'minute_local'} = $min;
 778        $date{'tz_local'} = $tz;
 779        return %date;
 780}
 781
 782sub parse_tag {
 783        my $tag_id = shift;
 784        my %tag;
 785        my @comment;
 786
 787        open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
 788        $tag{'id'} = $tag_id;
 789        while (my $line = <$fd>) {
 790                chomp $line;
 791                if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
 792                        $tag{'object'} = $1;
 793                } elsif ($line =~ m/^type (.+)$/) {
 794                        $tag{'type'} = $1;
 795                } elsif ($line =~ m/^tag (.+)$/) {
 796                        $tag{'name'} = $1;
 797                } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
 798                        $tag{'author'} = $1;
 799                        $tag{'epoch'} = $2;
 800                        $tag{'tz'} = $3;
 801                } elsif ($line =~ m/--BEGIN/) {
 802                        push @comment, $line;
 803                        last;
 804                } elsif ($line eq "") {
 805                        last;
 806                }
 807        }
 808        push @comment, <$fd>;
 809        $tag{'comment'} = \@comment;
 810        close $fd or return;
 811        if (!defined $tag{'name'}) {
 812                return
 813        };
 814        return %tag
 815}
 816
 817sub parse_commit {
 818        my $commit_id = shift;
 819        my $commit_text = shift;
 820
 821        my @commit_lines;
 822        my %co;
 823
 824        if (defined $commit_text) {
 825                @commit_lines = @$commit_text;
 826        } else {
 827                $/ = "\0";
 828                open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id
 829                        or return;
 830                @commit_lines = split '\n', <$fd>;
 831                close $fd or return;
 832                $/ = "\n";
 833                pop @commit_lines;
 834        }
 835        my $header = shift @commit_lines;
 836        if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
 837                return;
 838        }
 839        ($co{'id'}, my @parents) = split ' ', $header;
 840        $co{'parents'} = \@parents;
 841        $co{'parent'} = $parents[0];
 842        while (my $line = shift @commit_lines) {
 843                last if $line eq "\n";
 844                if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
 845                        $co{'tree'} = $1;
 846                } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
 847                        $co{'author'} = $1;
 848                        $co{'author_epoch'} = $2;
 849                        $co{'author_tz'} = $3;
 850                        if ($co{'author'} =~ m/^([^<]+) </) {
 851                                $co{'author_name'} = $1;
 852                        } else {
 853                                $co{'author_name'} = $co{'author'};
 854                        }
 855                } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
 856                        $co{'committer'} = $1;
 857                        $co{'committer_epoch'} = $2;
 858                        $co{'committer_tz'} = $3;
 859                        $co{'committer_name'} = $co{'committer'};
 860                        $co{'committer_name'} =~ s/ <.*//;
 861                }
 862        }
 863        if (!defined $co{'tree'}) {
 864                return;
 865        };
 866
 867        foreach my $title (@commit_lines) {
 868                $title =~ s/^    //;
 869                if ($title ne "") {
 870                        $co{'title'} = chop_str($title, 80, 5);
 871                        # remove leading stuff of merges to make the interesting part visible
 872                        if (length($title) > 50) {
 873                                $title =~ s/^Automatic //;
 874                                $title =~ s/^merge (of|with) /Merge ... /i;
 875                                if (length($title) > 50) {
 876                                        $title =~ s/(http|rsync):\/\///;
 877                                }
 878                                if (length($title) > 50) {
 879                                        $title =~ s/(master|www|rsync)\.//;
 880                                }
 881                                if (length($title) > 50) {
 882                                        $title =~ s/kernel.org:?//;
 883                                }
 884                                if (length($title) > 50) {
 885                                        $title =~ s/\/pub\/scm//;
 886                                }
 887                        }
 888                        $co{'title_short'} = chop_str($title, 50, 5);
 889                        last;
 890                }
 891        }
 892        # remove added spaces
 893        foreach my $line (@commit_lines) {
 894                $line =~ s/^    //;
 895        }
 896        $co{'comment'} = \@commit_lines;
 897
 898        my $age = time - $co{'committer_epoch'};
 899        $co{'age'} = $age;
 900        $co{'age_string'} = age_string($age);
 901        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
 902        if ($age > 60*60*24*7*2) {
 903                $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
 904                $co{'age_string_age'} = $co{'age_string'};
 905        } else {
 906                $co{'age_string_date'} = $co{'age_string'};
 907                $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
 908        }
 909        return %co;
 910}
 911
 912# parse ref from ref_file, given by ref_id, with given type
 913sub parse_ref {
 914        my $ref_file = shift;
 915        my $ref_id = shift;
 916        my $type = shift || git_get_type($ref_id);
 917        my %ref_item;
 918
 919        $ref_item{'type'} = $type;
 920        $ref_item{'id'} = $ref_id;
 921        $ref_item{'epoch'} = 0;
 922        $ref_item{'age'} = "unknown";
 923        if ($type eq "tag") {
 924                my %tag = parse_tag($ref_id);
 925                $ref_item{'comment'} = $tag{'comment'};
 926                if ($tag{'type'} eq "commit") {
 927                        my %co = parse_commit($tag{'object'});
 928                        $ref_item{'epoch'} = $co{'committer_epoch'};
 929                        $ref_item{'age'} = $co{'age_string'};
 930                } elsif (defined($tag{'epoch'})) {
 931                        my $age = time - $tag{'epoch'};
 932                        $ref_item{'epoch'} = $tag{'epoch'};
 933                        $ref_item{'age'} = age_string($age);
 934                }
 935                $ref_item{'reftype'} = $tag{'type'};
 936                $ref_item{'name'} = $tag{'name'};
 937                $ref_item{'refid'} = $tag{'object'};
 938        } elsif ($type eq "commit"){
 939                my %co = parse_commit($ref_id);
 940                $ref_item{'reftype'} = "commit";
 941                $ref_item{'name'} = $ref_file;
 942                $ref_item{'title'} = $co{'title'};
 943                $ref_item{'refid'} = $ref_id;
 944                $ref_item{'epoch'} = $co{'committer_epoch'};
 945                $ref_item{'age'} = $co{'age_string'};
 946        } else {
 947                $ref_item{'reftype'} = $type;
 948                $ref_item{'name'} = $ref_file;
 949                $ref_item{'refid'} = $ref_id;
 950        }
 951
 952        return %ref_item;
 953}
 954
 955# parse line of git-diff-tree "raw" output
 956sub parse_difftree_raw_line {
 957        my $line = shift;
 958        my %res;
 959
 960        # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M   ls-files.c'
 961        # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M   rev-tree.c'
 962        if ($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {
 963                $res{'from_mode'} = $1;
 964                $res{'to_mode'} = $2;
 965                $res{'from_id'} = $3;
 966                $res{'to_id'} = $4;
 967                $res{'status'} = $5;
 968                $res{'similarity'} = $6;
 969                if ($res{'status'} eq 'R' || $res{'status'} eq 'C') { # renamed or copied
 970                        ($res{'from_file'}, $res{'to_file'}) = map { unquote($_) } split("\t", $7);
 971                } else {
 972                        $res{'file'} = unquote($7);
 973                }
 974        }
 975        # 'c512b523472485aef4fff9e57b229d9d243c967f'
 976        #elsif ($line =~ m/^([0-9a-fA-F]{40})$/) {
 977        #       $res{'commit'} = $1;
 978        #}
 979
 980        return wantarray ? %res : \%res;
 981}
 982
 983## ......................................................................
 984## parse to array of hashes functions
 985
 986sub git_get_refs_list {
 987        my $ref_dir = shift;
 988        my @reflist;
 989
 990        my @refs;
 991        my $pfxlen = length("$projectroot/$project/$ref_dir");
 992        File::Find::find(sub {
 993                return if (/^\./);
 994                if (-f $_) {
 995                        push @refs, substr($File::Find::name, $pfxlen + 1);
 996                }
 997        }, "$projectroot/$project/$ref_dir");
 998
 999        foreach my $ref_file (@refs) {
1000                my $ref_id = git_get_hash_by_ref("$project/$ref_dir/$ref_file");
1001                my $type = git_get_type($ref_id) || next;
1002                my %ref_item = parse_ref($ref_file, $ref_id, $type);
1003
1004                push @reflist, \%ref_item;
1005        }
1006        # sort refs by age
1007        @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1008        return \@reflist;
1009}
1010
1011## ----------------------------------------------------------------------
1012## filesystem-related functions
1013
1014sub get_file_owner {
1015        my $path = shift;
1016
1017        my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
1018        my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
1019        if (!defined $gcos) {
1020                return undef;
1021        }
1022        my $owner = $gcos;
1023        $owner =~ s/[,;].*$//;
1024        return decode("utf8", $owner, Encode::FB_DEFAULT);
1025}
1026
1027## ......................................................................
1028## mimetype related functions
1029
1030sub mimetype_guess_file {
1031        my $filename = shift;
1032        my $mimemap = shift;
1033        -r $mimemap or return undef;
1034
1035        my %mimemap;
1036        open(MIME, $mimemap) or return undef;
1037        while (<MIME>) {
1038                next if m/^#/; # skip comments
1039                my ($mime, $exts) = split(/\t+/);
1040                if (defined $exts) {
1041                        my @exts = split(/\s+/, $exts);
1042                        foreach my $ext (@exts) {
1043                                $mimemap{$ext} = $mime;
1044                        }
1045                }
1046        }
1047        close(MIME);
1048
1049        $filename =~ /\.(.*?)$/;
1050        return $mimemap{$1};
1051}
1052
1053sub mimetype_guess {
1054        my $filename = shift;
1055        my $mime;
1056        $filename =~ /\./ or return undef;
1057
1058        if ($mimetypes_file) {
1059                my $file = $mimetypes_file;
1060                if ($file !~ m!^/!) { # if it is relative path
1061                        # it is relative to project
1062                        $file = "$projectroot/$project/$file";
1063                }
1064                $mime = mimetype_guess_file($filename, $file);
1065        }
1066        $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1067        return $mime;
1068}
1069
1070sub blob_mimetype {
1071        my $fd = shift;
1072        my $filename = shift;
1073
1074        if ($filename) {
1075                my $mime = mimetype_guess($filename);
1076                $mime and return $mime;
1077        }
1078
1079        # just in case
1080        return $default_blob_plain_mimetype unless $fd;
1081
1082        if (-T $fd) {
1083                return 'text/plain' .
1084                       ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1085        } elsif (! $filename) {
1086                return 'application/octet-stream';
1087        } elsif ($filename =~ m/\.png$/i) {
1088                return 'image/png';
1089        } elsif ($filename =~ m/\.gif$/i) {
1090                return 'image/gif';
1091        } elsif ($filename =~ m/\.jpe?g$/i) {
1092                return 'image/jpeg';
1093        } else {
1094                return 'application/octet-stream';
1095        }
1096}
1097
1098## ======================================================================
1099## functions printing HTML: header, footer, error page
1100
1101sub git_header_html {
1102        my $status = shift || "200 OK";
1103        my $expires = shift;
1104
1105        my $title = "$site_name git";
1106        if (defined $project) {
1107                $title .= " - $project";
1108                if (defined $action) {
1109                        $title .= "/$action";
1110                        if (defined $file_name) {
1111                                $title .= " - $file_name";
1112                                if ($action eq "tree" && $file_name !~ m|/$|) {
1113                                        $title .= "/";
1114                                }
1115                        }
1116                }
1117        }
1118        my $content_type;
1119        # require explicit support from the UA if we are to send the page as
1120        # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
1121        # we have to do this because MSIE sometimes globs '*/*', pretending to
1122        # support xhtml+xml but choking when it gets what it asked for.
1123        if (defined $cgi->http('HTTP_ACCEPT') &&
1124            $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
1125            $cgi->Accept('application/xhtml+xml') != 0) {
1126                $content_type = 'application/xhtml+xml';
1127        } else {
1128                $content_type = 'text/html';
1129        }
1130        print $cgi->header(-type=>$content_type, -charset => 'utf-8',
1131                           -status=> $status, -expires => $expires);
1132        print <<EOF;
1133<?xml version="1.0" encoding="utf-8"?>
1134<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1135<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
1136<!-- git web interface version $version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
1137<!-- git core binaries version $git_version -->
1138<head>
1139<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
1140<meta name="generator" content="gitweb/$version git/$git_version"/>
1141<meta name="robots" content="index, nofollow"/>
1142<title>$title</title>
1143<link rel="stylesheet" type="text/css" href="$stylesheet"/>
1144EOF
1145        if (defined $project) {
1146                printf('<link rel="alternate" title="%s log" '.
1147                       'href="%s" type="application/rss+xml"/>'."\n",
1148                       esc_param($project), href(action=>"rss"));
1149        }
1150
1151        print "</head>\n" .
1152              "<body>\n" .
1153              "<div class=\"page_header\">\n" .
1154              "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
1155              "<img src=\"$logo\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
1156              "</a>\n";
1157        print $cgi->a({-href => esc_param($home_link)}, $home_link_str) . " / ";
1158        if (defined $project) {
1159                print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
1160                if (defined $action) {
1161                        print " / $action";
1162                }
1163                print "\n";
1164                if (!defined $searchtext) {
1165                        $searchtext = "";
1166                }
1167                my $search_hash;
1168                if (defined $hash_base) {
1169                        $search_hash = $hash_base;
1170                } elsif (defined $hash) {
1171                        $search_hash = $hash;
1172                } else {
1173                        $search_hash = "HEAD";
1174                }
1175                $cgi->param("a", "search");
1176                $cgi->param("h", $search_hash);
1177                print $cgi->startform(-method => "get", -action => $my_uri) .
1178                      "<div class=\"search\">\n" .
1179                      $cgi->hidden(-name => "p") . "\n" .
1180                      $cgi->hidden(-name => "a") . "\n" .
1181                      $cgi->hidden(-name => "h") . "\n" .
1182                      $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
1183                      "</div>" .
1184                      $cgi->end_form() . "\n";
1185        }
1186        print "</div>\n";
1187}
1188
1189sub git_footer_html {
1190        print "<div class=\"page_footer\">\n";
1191        if (defined $project) {
1192                my $descr = git_get_project_description($project);
1193                if (defined $descr) {
1194                        print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
1195                }
1196                print $cgi->a({-href => href(action=>"rss"), -class => "rss_logo"}, "RSS") . "\n";
1197        } else {
1198                print $cgi->a({-href => href(action=>"opml"), -class => "rss_logo"}, "OPML") . "\n";
1199        }
1200        print "</div>\n" .
1201              "</body>\n" .
1202              "</html>";
1203}
1204
1205sub die_error {
1206        my $status = shift || "403 Forbidden";
1207        my $error = shift || "Malformed query, file missing or permission denied";
1208
1209        git_header_html($status);
1210        print <<EOF;
1211<div class="page_body">
1212<br /><br />
1213$status - $error
1214<br />
1215</div>
1216EOF
1217        git_footer_html();
1218        exit;
1219}
1220
1221## ----------------------------------------------------------------------
1222## functions printing or outputting HTML: navigation
1223
1224sub git_print_page_nav {
1225        my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
1226        $extra = '' if !defined $extra; # pager or formats
1227
1228        my @navs = qw(summary shortlog log commit commitdiff tree);
1229        if ($suppress) {
1230                @navs = grep { $_ ne $suppress } @navs;
1231        }
1232
1233        my %arg = map { $_ => {action=>$_} } @navs;
1234        if (defined $head) {
1235                for (qw(commit commitdiff)) {
1236                        $arg{$_}{hash} = $head;
1237                }
1238                if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
1239                        for (qw(shortlog log)) {
1240                                $arg{$_}{hash} = $head;
1241                        }
1242                }
1243        }
1244        $arg{tree}{hash} = $treehead if defined $treehead;
1245        $arg{tree}{hash_base} = $treebase if defined $treebase;
1246
1247        print "<div class=\"page_nav\">\n" .
1248                (join " | ",
1249                 map { $_ eq $current ?
1250                       $_ : $cgi->a({-href => href(%{$arg{$_}})}, "$_")
1251                 } @navs);
1252        print "<br/>\n$extra<br/>\n" .
1253              "</div>\n";
1254}
1255
1256sub format_paging_nav {
1257        my ($action, $hash, $head, $page, $nrevs) = @_;
1258        my $paging_nav;
1259
1260
1261        if ($hash ne $head || $page) {
1262                $paging_nav .= $cgi->a({-href => href(action=>$action)}, "HEAD");
1263        } else {
1264                $paging_nav .= "HEAD";
1265        }
1266
1267        if ($page > 0) {
1268                $paging_nav .= " &sdot; " .
1269                        $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
1270                                 -accesskey => "p", -title => "Alt-p"}, "prev");
1271        } else {
1272                $paging_nav .= " &sdot; prev";
1273        }
1274
1275        if ($nrevs >= (100 * ($page+1)-1)) {
1276                $paging_nav .= " &sdot; " .
1277                        $cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
1278                                 -accesskey => "n", -title => "Alt-n"}, "next");
1279        } else {
1280                $paging_nav .= " &sdot; next";
1281        }
1282
1283        return $paging_nav;
1284}
1285
1286## ......................................................................
1287## functions printing or outputting HTML: div
1288
1289sub git_print_header_div {
1290        my ($action, $title, $hash, $hash_base) = @_;
1291        my %args = ();
1292
1293        $args{action} = $action;
1294        $args{hash} = $hash if $hash;
1295        $args{hash_base} = $hash_base if $hash_base;
1296
1297        print "<div class=\"header\">\n" .
1298              $cgi->a({-href => href(%args), -class => "title"},
1299              $title ? $title : $action) .
1300              "\n</div>\n";
1301}
1302
1303sub git_print_page_path {
1304        my $name = shift;
1305        my $type = shift;
1306        my $hb = shift;
1307
1308        if (!defined $name) {
1309                print "<div class=\"page_path\">/</div>\n";
1310        } elsif (defined $type && $type eq 'blob') {
1311                print "<div class=\"page_path\">";
1312                if (defined $hb) {
1313                        print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,
1314                                                     hash_base=>$hb)},
1315                                      esc_html($name));
1316                } else {
1317                        print $cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name)},
1318                                      esc_html($name));
1319                }
1320                print "<br/></div>\n";
1321        } else {
1322                print "<div class=\"page_path\">" . esc_html($name) . "<br/></div>\n";
1323        }
1324}
1325
1326sub git_print_log {
1327        my $log = shift;
1328
1329        # remove leading empty lines
1330        while (defined $log->[0] && $log->[0] eq "") {
1331                shift @$log;
1332        }
1333
1334        # print log
1335        my $signoff = 0;
1336        my $empty = 0;
1337        foreach my $line (@$log) {
1338                # print only one empty line
1339                # do not print empty line after signoff
1340                if ($line eq "") {
1341                        next if ($empty || $signoff);
1342                        $empty = 1;
1343                } else {
1344                        $empty = 0;
1345                }
1346                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1347                        $signoff = 1;
1348                        print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
1349                } else {
1350                        $signoff = 0;
1351                        print format_log_line_html($line) . "<br/>\n";
1352                }
1353        }
1354}
1355
1356sub git_print_simplified_log {
1357        my $log = shift;
1358        my $remove_title = shift;
1359
1360        shift @$log if $remove_title;
1361        # remove leading empty lines
1362        while (defined $log->[0] && $log->[0] eq "") {
1363                shift @$log;
1364        }
1365
1366        # simplify and print log
1367        my $empty = 0;
1368        foreach my $line (@$log) {
1369                # remove signoff lines
1370                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1371                        next;
1372                }
1373                # print only one empty line
1374                if ($line eq "") {
1375                        next if $empty;
1376                        $empty = 1;
1377                } else {
1378                        $empty = 0;
1379                }
1380                print format_log_line_html($line) . "<br/>\n";
1381        }
1382        # end with single empty line
1383        print "<br/>\n" unless $empty;
1384}
1385
1386## ......................................................................
1387## functions printing large fragments of HTML
1388
1389sub git_difftree_body {
1390        my ($difftree, $hash, $parent) = @_;
1391
1392        print "<div class=\"list_head\">\n";
1393        if ($#{$difftree} > 10) {
1394                print(($#{$difftree} + 1) . " files changed:\n");
1395        }
1396        print "</div>\n";
1397
1398        print "<table class=\"diff_tree\">\n";
1399        my $alternate = 0;
1400        foreach my $line (@{$difftree}) {
1401                my %diff = parse_difftree_raw_line($line);
1402
1403                if ($alternate) {
1404                        print "<tr class=\"dark\">\n";
1405                } else {
1406                        print "<tr class=\"light\">\n";
1407                }
1408                $alternate ^= 1;
1409
1410                my ($to_mode_oct, $to_mode_str, $to_file_type);
1411                my ($from_mode_oct, $from_mode_str, $from_file_type);
1412                if ($diff{'to_mode'} ne ('0' x 6)) {
1413                        $to_mode_oct = oct $diff{'to_mode'};
1414                        if (S_ISREG($to_mode_oct)) { # only for regular file
1415                                $to_mode_str = sprintf("%04o", $to_mode_oct & 0777); # permission bits
1416                        }
1417                        $to_file_type = file_type($diff{'to_mode'});
1418                }
1419                if ($diff{'from_mode'} ne ('0' x 6)) {
1420                        $from_mode_oct = oct $diff{'from_mode'};
1421                        if (S_ISREG($to_mode_oct)) { # only for regular file
1422                                $from_mode_str = sprintf("%04o", $from_mode_oct & 0777); # permission bits
1423                        }
1424                        $from_file_type = file_type($diff{'from_mode'});
1425                }
1426
1427                if ($diff{'status'} eq "A") { # created
1428                        my $mode_chng = "<span class=\"file_status new\">[new $to_file_type";
1429                        $mode_chng   .= " with mode: $to_mode_str" if $to_mode_str;
1430                        $mode_chng   .= "]</span>";
1431                        print "<td>" .
1432                              $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1433                                                     hash_base=>$hash, file_name=>$diff{'file'}),
1434                                      -class => "list"}, esc_html($diff{'file'})) .
1435                              "</td>\n" .
1436                              "<td>$mode_chng</td>\n" .
1437                              "<td class=\"link\">" .
1438                              $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1439                                                     hash_base=>$hash, file_name=>$diff{'file'})},
1440                                      "blob") .
1441                              "</td>\n";
1442
1443                } elsif ($diff{'status'} eq "D") { # deleted
1444                        my $mode_chng = "<span class=\"file_status deleted\">[deleted $from_file_type]</span>";
1445                        print "<td>" .
1446                              $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1447                                                     hash_base=>$parent, file_name=>$diff{'file'}),
1448                                       -class => "list"}, esc_html($diff{'file'})) .
1449                              "</td>\n" .
1450                              "<td>$mode_chng</td>\n" .
1451                              "<td class=\"link\">" .
1452                              $cgi->a({-href => href(action=>"blob", hash=>$diff{'from_id'},
1453                                                     hash_base=>$parent, file_name=>$diff{'file'})},
1454                                      "blob") .
1455                              " | " .
1456                              $cgi->a({-href => href(action=>"history", hash_base=>$parent,
1457                                                     file_name=>$diff{'file'})},\
1458                                      "history") .
1459                              "</td>\n";
1460
1461                } elsif ($diff{'status'} eq "M" || $diff{'status'} eq "T") { # modified, or type changed
1462                        my $mode_chnge = "";
1463                        if ($diff{'from_mode'} != $diff{'to_mode'}) {
1464                                $mode_chnge = "<span class=\"file_status mode_chnge\">[changed";
1465                                if ($from_file_type != $to_file_type) {
1466                                        $mode_chnge .= " from $from_file_type to $to_file_type";
1467                                }
1468                                if (($from_mode_oct & 0777) != ($to_mode_oct & 0777)) {
1469                                        if ($from_mode_str && $to_mode_str) {
1470                                                $mode_chnge .= " mode: $from_mode_str->$to_mode_str";
1471                                        } elsif ($to_mode_str) {
1472                                                $mode_chnge .= " mode: $to_mode_str";
1473                                        }
1474                                }
1475                                $mode_chnge .= "]</span>\n";
1476                        }
1477                        print "<td>";
1478                        if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1479                                print $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1480                                                             hash_base=>$hash, file_name=>$diff{'file'}),
1481                                              -class => "list"}, esc_html($diff{'file'}));
1482                        } else { # only mode changed
1483                                print $cgi->a({-href => href(action=>"blob", hash=>$diff{'to_id'},
1484                                                             hash_base=>$hash, file_name=>$diff{'file'}),
1485                                              -class => "list"}, esc_html($diff{'file'}));
1486                        }
1487                        print "</td>\n" .
1488                              "<td>$mode_chnge</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                        if ($diff{'to_id'} ne $diff{'from_id'}) { # modified
1494                                print " | " .
1495                                        $cgi->a({-href => href(action=>"blobdiff", hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1496                                                               hash_base=>$hash, file_name=>$diff{'file'})},
1497                                                "diff");
1498                        }
1499                        print " | " .
1500                                $cgi->a({-href => href(action=>"history",
1501                                                       hash_base=>$hash, file_name=>$diff{'file'})},
1502                                        "history");
1503                        print "</td>\n";
1504
1505                } elsif ($diff{'status'} eq "R" || $diff{'status'} eq "C") { # renamed or copied
1506                        my %status_name = ('R' => 'moved', 'C' => 'copied');
1507                        my $nstatus = $status_name{$diff{'status'}};
1508                        my $mode_chng = "";
1509                        if ($diff{'from_mode'} != $diff{'to_mode'}) {
1510                                # mode also for directories, so we cannot use $to_mode_str
1511                                $mode_chng = sprintf(", mode: %04o", $to_mode_oct & 0777);
1512                        }
1513                        print "<td>" .
1514                              $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1515                                                     hash=>$diff{'to_id'}, file_name=>$diff{'to_file'}),
1516                                      -class => "list"}, esc_html($diff{'to_file'})) . "</td>\n" .
1517                              "<td><span class=\"file_status $nstatus\">[$nstatus from " .
1518                              $cgi->a({-href => href(action=>"blob", hash_base=>$parent,
1519                                                     hash=>$diff{'from_id'}, file_name=>$diff{'from_file'}),
1520                                      -class => "list"}, esc_html($diff{'from_file'})) .
1521                              " with " . (int $diff{'similarity'}) . "% similarity$mode_chng]</span></td>\n" .
1522                              "<td class=\"link\">" .
1523                              $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1524                                                     hash=>$diff{'to_id'}, file_name=>$diff{'to_file'})},
1525                                      "blob");
1526                        if ($diff{'to_id'} ne $diff{'from_id'}) {
1527                                print " | " .
1528                                        $cgi->a({-href => href(action=>"blobdiff", hash_base=>$hash,
1529                                                               hash=>$diff{'to_id'}, hash_parent=>$diff{'from_id'},
1530                                                               file_name=>$diff{'to_file'}, file_parent=>$diff{'from_file'})},
1531                                                "diff");
1532                        }
1533                        print "</td>\n";
1534
1535                } # we should not encounter Unmerged (U) or Unknown (X) status
1536                print "</tr>\n";
1537        }
1538        print "</table>\n";
1539}
1540
1541sub git_patchset_body {
1542        my ($patchset, $difftree, $hash, $hash_parent) = @_;
1543
1544        my $patch_idx = 0;
1545        my $in_header = 0;
1546        my $patch_found = 0;
1547        my %diffinfo;
1548
1549        print "<div class=\"patchset\">\n";
1550
1551        LINE: foreach my $patch_line (@$patchset) {
1552
1553                if ($patch_line =~ m/^diff /) { # "git diff" header
1554                        # beginning of patch (in patchset)
1555                        if ($patch_found) {
1556                                # close previous patch
1557                                print "</div>\n"; # class="patch"
1558                        } else {
1559                                # first patch in patchset
1560                                $patch_found = 1;
1561                        }
1562                        print "<div class=\"patch\">\n";
1563
1564                        %diffinfo = parse_difftree_raw_line($difftree->[$patch_idx++]);
1565
1566                        # for now, no extended header, hence we skip empty patches
1567                        # companion to  next LINE if $in_header;
1568                        if ($diffinfo{'from_id'} eq $diffinfo{'to_id'}) { # no change
1569                                $in_header = 1;
1570                                next LINE;
1571                        }
1572
1573                        if ($diffinfo{'status'} eq "A") { # added
1574                                print "<div class=\"diff_info\">" . file_type($diffinfo{'to_mode'}) . ":" .
1575                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1576                                                             hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1577                                              $diffinfo{'to_id'}) . "(new)" .
1578                                      "</div>\n"; # class="diff_info"
1579
1580                        } elsif ($diffinfo{'status'} eq "D") { # deleted
1581                                print "<div class=\"diff_info\">" . file_type($diffinfo{'from_mode'}) . ":" .
1582                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1583                                                             hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1584                                              $diffinfo{'from_id'}) . "(deleted)" .
1585                                      "</div>\n"; # class="diff_info"
1586
1587                        } elsif ($diffinfo{'status'} eq "R" || # renamed
1588                                 $diffinfo{'status'} eq "C") { # copied
1589                                print "<div class=\"diff_info\">" .
1590                                      file_type($diffinfo{'from_mode'}) . ":" .
1591                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1592                                                             hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'from_file'})},
1593                                              $diffinfo{'from_id'}) .
1594                                      " -> " .
1595                                      file_type($diffinfo{'to_mode'}) . ":" .
1596                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1597                                                             hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'to_file'})},
1598                                              $diffinfo{'to_id'});
1599                                print "</div>\n"; # class="diff_info"
1600
1601                        } else { # modified, mode changed, ...
1602                                print "<div class=\"diff_info\">" .
1603                                      file_type($diffinfo{'from_mode'}) . ":" .
1604                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1605                                                             hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1606                                              $diffinfo{'from_id'}) .
1607                                      " -> " .
1608                                      file_type($diffinfo{'to_mode'}) . ":" .
1609                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1610                                                             hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1611                                              $diffinfo{'to_id'});
1612                                print "</div>\n"; # class="diff_info"
1613                        }
1614
1615                        #print "<div class=\"diff extended_header\">\n";
1616                        $in_header = 1;
1617                        next LINE;
1618                } # start of patch in patchset
1619
1620
1621                if ($in_header && $patch_line =~ m/^---/) {
1622                        #print "</div>\n"
1623                        $in_header = 0;
1624                }
1625                next LINE if $in_header;
1626
1627                print format_diff_line($patch_line);
1628        }
1629        print "</div>\n" if $patch_found; # class="patch"
1630
1631        print "</div>\n"; # class="patchset"
1632}
1633
1634# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1635
1636sub git_shortlog_body {
1637        # uses global variable $project
1638        my ($revlist, $from, $to, $refs, $extra) = @_;
1639
1640        my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1641        my $have_snapshot = (defined $ctype && defined $suffix);
1642
1643        $from = 0 unless defined $from;
1644        $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1645
1646        print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1647        my $alternate = 0;
1648        for (my $i = $from; $i <= $to; $i++) {
1649                my $commit = $revlist->[$i];
1650                #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1651                my $ref = format_ref_marker($refs, $commit);
1652                my %co = parse_commit($commit);
1653                if ($alternate) {
1654                        print "<tr class=\"dark\">\n";
1655                } else {
1656                        print "<tr class=\"light\">\n";
1657                }
1658                $alternate ^= 1;
1659                # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1660                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1661                      "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1662                      "<td>";
1663                print format_subject_html($co{'title'}, $co{'title_short'},
1664                                          href(action=>"commit", hash=>$commit), $ref);
1665                print "</td>\n" .
1666                      "<td class=\"link\">" .
1667                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1668                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1669                if ($have_snapshot) {
1670                        print " | " .  $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1671                }
1672                print "</td>\n" .
1673                      "</tr>\n";
1674        }
1675        if (defined $extra) {
1676                print "<tr>\n" .
1677                      "<td colspan=\"4\">$extra</td>\n" .
1678                      "</tr>\n";
1679        }
1680        print "</table>\n";
1681}
1682
1683sub git_history_body {
1684        # Warning: assumes constant type (blob or tree) during history
1685        my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1686
1687        print "<table class=\"history\" cellspacing=\"0\">\n";
1688        my $alternate = 0;
1689        while (my $line = <$fd>) {
1690                if ($line !~ m/^([0-9a-fA-F]{40})/) {
1691                        next;
1692                }
1693
1694                my $commit = $1;
1695                my %co = parse_commit($commit);
1696                if (!%co) {
1697                        next;
1698                }
1699
1700                my $ref = format_ref_marker($refs, $commit);
1701
1702                if ($alternate) {
1703                        print "<tr class=\"dark\">\n";
1704                } else {
1705                        print "<tr class=\"light\">\n";
1706                }
1707                $alternate ^= 1;
1708                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1709                      # shortlog uses      chop_str($co{'author_name'}, 10)
1710                      "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1711                      "<td>";
1712                # originally git_history used chop_str($co{'title'}, 50)
1713                print format_subject_html($co{'title'}, $co{'title_short'},
1714                                          href(action=>"commit", hash=>$commit), $ref);
1715                print "</td>\n" .
1716                      "<td class=\"link\">" .
1717                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1718                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1719                      $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1720
1721                if ($ftype eq 'blob') {
1722                        my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1723                        my $blob_parent  = git_get_hash_by_path($commit, $file_name);
1724                        if (defined $blob_current && defined $blob_parent &&
1725                                        $blob_current ne $blob_parent) {
1726                                print " | " .
1727                                        $cgi->a({-href => href(action=>"blobdiff", hash=>$blob_current, hash_parent=>$blob_parent,
1728                                                               hash_base=>$commit, file_name=>$file_name)},
1729                                                "diff to current");
1730                        }
1731                }
1732                print "</td>\n" .
1733                      "</tr>\n";
1734        }
1735        if (defined $extra) {
1736                print "<tr>\n" .
1737                      "<td colspan=\"4\">$extra</td>\n" .
1738                      "</tr>\n";
1739        }
1740        print "</table>\n";
1741}
1742
1743sub git_tags_body {
1744        # uses global variable $project
1745        my ($taglist, $from, $to, $extra) = @_;
1746        $from = 0 unless defined $from;
1747        $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1748
1749        print "<table class=\"tags\" cellspacing=\"0\">\n";
1750        my $alternate = 0;
1751        for (my $i = $from; $i <= $to; $i++) {
1752                my $entry = $taglist->[$i];
1753                my %tag = %$entry;
1754                my $comment_lines = $tag{'comment'};
1755                my $comment = shift @$comment_lines;
1756                my $comment_short;
1757                if (defined $comment) {
1758                        $comment_short = chop_str($comment, 30, 5);
1759                }
1760                if ($alternate) {
1761                        print "<tr class=\"dark\">\n";
1762                } else {
1763                        print "<tr class=\"light\">\n";
1764                }
1765                $alternate ^= 1;
1766                print "<td><i>$tag{'age'}</i></td>\n" .
1767                      "<td>" .
1768                      $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1769                               -class => "list name"}, esc_html($tag{'name'})) .
1770                      "</td>\n" .
1771                      "<td>";
1772                if (defined $comment) {
1773                        print format_subject_html($comment, $comment_short,
1774                                                  href(action=>"tag", hash=>$tag{'id'}));
1775                }
1776                print "</td>\n" .
1777                      "<td class=\"selflink\">";
1778                if ($tag{'type'} eq "tag") {
1779                        print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1780                } else {
1781                        print "&nbsp;";
1782                }
1783                print "</td>\n" .
1784                      "<td class=\"link\">" . " | " .
1785                      $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1786                if ($tag{'reftype'} eq "commit") {
1787                        print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1788                              " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1789                } elsif ($tag{'reftype'} eq "blob") {
1790                        print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1791                }
1792                print "</td>\n" .
1793                      "</tr>";
1794        }
1795        if (defined $extra) {
1796                print "<tr>\n" .
1797                      "<td colspan=\"5\">$extra</td>\n" .
1798                      "</tr>\n";
1799        }
1800        print "</table>\n";
1801}
1802
1803sub git_heads_body {
1804        # uses global variable $project
1805        my ($taglist, $head, $from, $to, $extra) = @_;
1806        $from = 0 unless defined $from;
1807        $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1808
1809        print "<table class=\"heads\" cellspacing=\"0\">\n";
1810        my $alternate = 0;
1811        for (my $i = $from; $i <= $to; $i++) {
1812                my $entry = $taglist->[$i];
1813                my %tag = %$entry;
1814                my $curr = $tag{'id'} eq $head;
1815                if ($alternate) {
1816                        print "<tr class=\"dark\">\n";
1817                } else {
1818                        print "<tr class=\"light\">\n";
1819                }
1820                $alternate ^= 1;
1821                print "<td><i>$tag{'age'}</i></td>\n" .
1822                      ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1823                      $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1824                               -class => "list name"},esc_html($tag{'name'})) .
1825                      "</td>\n" .
1826                      "<td class=\"link\">" .
1827                      $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1828                      $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1829                      "</td>\n" .
1830                      "</tr>";
1831        }
1832        if (defined $extra) {
1833                print "<tr>\n" .
1834                      "<td colspan=\"3\">$extra</td>\n" .
1835                      "</tr>\n";
1836        }
1837        print "</table>\n";
1838}
1839
1840## ----------------------------------------------------------------------
1841## functions printing large fragments, format as one of arguments
1842
1843sub git_diff_print {
1844        my $from = shift;
1845        my $from_name = shift;
1846        my $to = shift;
1847        my $to_name = shift;
1848        my $format = shift || "html";
1849
1850        my $from_tmp = "/dev/null";
1851        my $to_tmp = "/dev/null";
1852        my $pid = $$;
1853
1854        # create tmp from-file
1855        if (defined $from) {
1856                $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1857                open my $fd2, "> $from_tmp";
1858                open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1859                my @file = <$fd>;
1860                print $fd2 @file;
1861                close $fd2;
1862                close $fd;
1863        }
1864
1865        # create tmp to-file
1866        if (defined $to) {
1867                $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1868                open my $fd2, "> $to_tmp";
1869                open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1870                my @file = <$fd>;
1871                print $fd2 @file;
1872                close $fd2;
1873                close $fd;
1874        }
1875
1876        open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1877        if ($format eq "plain") {
1878                undef $/;
1879                print <$fd>;
1880                $/ = "\n";
1881        } else {
1882                while (my $line = <$fd>) {
1883                        chomp $line;
1884                        my $char = substr($line, 0, 1);
1885                        my $diff_class = "";
1886                        if ($char eq '+') {
1887                                $diff_class = " add";
1888                        } elsif ($char eq "-") {
1889                                $diff_class = " rem";
1890                        } elsif ($char eq "@") {
1891                                $diff_class = " chunk_header";
1892                        } elsif ($char eq "\\") {
1893                                # skip errors
1894                                next;
1895                        }
1896                        $line = untabify($line);
1897                        print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1898                }
1899        }
1900        close $fd;
1901
1902        if (defined $from) {
1903                unlink($from_tmp);
1904        }
1905        if (defined $to) {
1906                unlink($to_tmp);
1907        }
1908}
1909
1910
1911## ======================================================================
1912## ======================================================================
1913## actions
1914
1915sub git_project_list {
1916        my $order = $cgi->param('o');
1917        if (defined $order && $order !~ m/project|descr|owner|age/) {
1918                die_error(undef, "Unknown order parameter");
1919        }
1920
1921        my @list = git_get_projects_list();
1922        my @projects;
1923        if (!@list) {
1924                die_error(undef, "No projects found");
1925        }
1926        foreach my $pr (@list) {
1927                my $head = git_get_head_hash($pr->{'path'});
1928                if (!defined $head) {
1929                        next;
1930                }
1931                $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1932                my %co = parse_commit($head);
1933                if (!%co) {
1934                        next;
1935                }
1936                $pr->{'commit'} = \%co;
1937                if (!defined $pr->{'descr'}) {
1938                        my $descr = git_get_project_description($pr->{'path'}) || "";
1939                        $pr->{'descr'} = chop_str($descr, 25, 5);
1940                }
1941                if (!defined $pr->{'owner'}) {
1942                        $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1943                }
1944                push @projects, $pr;
1945        }
1946
1947        git_header_html();
1948        if (-f $home_text) {
1949                print "<div class=\"index_include\">\n";
1950                open (my $fd, $home_text);
1951                print <$fd>;
1952                close $fd;
1953                print "</div>\n";
1954        }
1955        print "<table class=\"project_list\">\n" .
1956              "<tr>\n";
1957        $order ||= "project";
1958        if ($order eq "project") {
1959                @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1960                print "<th>Project</th>\n";
1961        } else {
1962                print "<th>" .
1963                      $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
1964                               -class => "header"}, "Project") .
1965                      "</th>\n";
1966        }
1967        if ($order eq "descr") {
1968                @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1969                print "<th>Description</th>\n";
1970        } else {
1971                print "<th>" .
1972                      $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
1973                               -class => "header"}, "Description") .
1974                      "</th>\n";
1975        }
1976        if ($order eq "owner") {
1977                @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1978                print "<th>Owner</th>\n";
1979        } else {
1980                print "<th>" .
1981                      $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
1982                               -class => "header"}, "Owner") .
1983                      "</th>\n";
1984        }
1985        if ($order eq "age") {
1986                @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
1987                print "<th>Last Change</th>\n";
1988        } else {
1989                print "<th>" .
1990                      $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
1991                               -class => "header"}, "Last Change") .
1992                      "</th>\n";
1993        }
1994        print "<th></th>\n" .
1995              "</tr>\n";
1996        my $alternate = 0;
1997        foreach my $pr (@projects) {
1998                if ($alternate) {
1999                        print "<tr class=\"dark\">\n";
2000                } else {
2001                        print "<tr class=\"light\">\n";
2002                }
2003                $alternate ^= 1;
2004                print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2005                                        -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2006                      "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2007                      "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2008                print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2009                      $pr->{'commit'}{'age_string'} . "</td>\n" .
2010                      "<td class=\"link\">" .
2011                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
2012                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2013                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2014                      "</td>\n" .
2015                      "</tr>\n";
2016        }
2017        print "</table>\n";
2018        git_footer_html();
2019}
2020
2021sub git_summary {
2022        my $descr = git_get_project_description($project) || "none";
2023        my $head = git_get_head_hash($project);
2024        my %co = parse_commit($head);
2025        my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2026
2027        my $owner = git_get_project_owner($project);
2028
2029        my $refs = git_get_references();
2030        git_header_html();
2031        git_print_page_nav('summary','', $head);
2032
2033        print "<div class=\"title\">&nbsp;</div>\n";
2034        print "<table cellspacing=\"0\">\n" .
2035              "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2036              "<tr><td>owner</td><td>$owner</td></tr>\n" .
2037              "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2038        # use per project git URL list in $projectroot/$project/cloneurl
2039        # or make project git URL from git base URL and project name
2040        my $url_tag = "URL";
2041        my @url_list = git_get_project_url_list($project);
2042        @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2043        foreach my $git_url (@url_list) {
2044                next unless $git_url;
2045                print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2046                $url_tag = "";
2047        }
2048        print "</table>\n";
2049
2050        open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
2051                or die_error(undef, "Open git-rev-list failed");
2052        my @revlist = map { chomp; $_ } <$fd>;
2053        close $fd;
2054        git_print_header_div('shortlog');
2055        git_shortlog_body(\@revlist, 0, 15, $refs,
2056                          $cgi->a({-href => href(action=>"shortlog")}, "..."));
2057
2058        my $taglist = git_get_refs_list("refs/tags");
2059        if (defined @$taglist) {
2060                git_print_header_div('tags');
2061                git_tags_body($taglist, 0, 15,
2062                              $cgi->a({-href => href(action=>"tags")}, "..."));
2063        }
2064
2065        my $headlist = git_get_refs_list("refs/heads");
2066        if (defined @$headlist) {
2067                git_print_header_div('heads');
2068                git_heads_body($headlist, $head, 0, 15,
2069                               $cgi->a({-href => href(action=>"heads")}, "..."));
2070        }
2071
2072        git_footer_html();
2073}
2074
2075sub git_tag {
2076        my $head = git_get_head_hash($project);
2077        git_header_html();
2078        git_print_page_nav('','', $head,undef,$head);
2079        my %tag = parse_tag($hash);
2080        git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2081        print "<div class=\"title_text\">\n" .
2082              "<table cellspacing=\"0\">\n" .
2083              "<tr>\n" .
2084              "<td>object</td>\n" .
2085              "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2086                               $tag{'object'}) . "</td>\n" .
2087              "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2088                                              $tag{'type'}) . "</td>\n" .
2089              "</tr>\n";
2090        if (defined($tag{'author'})) {
2091                my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2092                print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2093                print "<tr><td></td><td>" . $ad{'rfc2822'} .
2094                        sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2095                        "</td></tr>\n";
2096        }
2097        print "</table>\n\n" .
2098              "</div>\n";
2099        print "<div class=\"page_body\">";
2100        my $comment = $tag{'comment'};
2101        foreach my $line (@$comment) {
2102                print esc_html($line) . "<br/>\n";
2103        }
2104        print "</div>\n";
2105        git_footer_html();
2106}
2107
2108sub git_blame2 {
2109        my $fd;
2110        my $ftype;
2111
2112        if (!gitweb_check_feature('blame')) {
2113                die_error('403 Permission denied', "Permission denied");
2114        }
2115        die_error('404 Not Found', "File name not defined") if (!$file_name);
2116        $hash_base ||= git_get_head_hash($project);
2117        die_error(undef, "Couldn't find base commit") unless ($hash_base);
2118        my %co = parse_commit($hash_base)
2119                or die_error(undef, "Reading commit failed");
2120        if (!defined $hash) {
2121                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2122                        or die_error(undef, "Error looking up file");
2123        }
2124        $ftype = git_get_type($hash);
2125        if ($ftype !~ "blob") {
2126                die_error("400 Bad Request", "Object is not a blob");
2127        }
2128        open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2129                or die_error(undef, "Open git-blame failed");
2130        git_header_html();
2131        my $formats_nav =
2132                $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2133                        "blob") .
2134                " | " .
2135                $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2136                        "head");
2137        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2138        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2139        git_print_page_path($file_name, $ftype, $hash_base);
2140        my @rev_color = (qw(light2 dark2));
2141        my $num_colors = scalar(@rev_color);
2142        my $current_color = 0;
2143        my $last_rev;
2144        print <<HTML;
2145<div class="page_body">
2146<table class="blame">
2147<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2148HTML
2149        while (<$fd>) {
2150                /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2151                my $full_rev = $1;
2152                my $rev = substr($full_rev, 0, 8);
2153                my $lineno = $2;
2154                my $data = $3;
2155
2156                if (!defined $last_rev) {
2157                        $last_rev = $full_rev;
2158                } elsif ($last_rev ne $full_rev) {
2159                        $last_rev = $full_rev;
2160                        $current_color = ++$current_color % $num_colors;
2161                }
2162                print "<tr class=\"$rev_color[$current_color]\">\n";
2163                print "<td class=\"sha1\">" .
2164                        $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2165                                esc_html($rev)) . "</td>\n";
2166                print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2167                      esc_html($lineno) . "</a></td>\n";
2168                print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2169                print "</tr>\n";
2170        }
2171        print "</table>\n";
2172        print "</div>";
2173        close $fd
2174                or print "Reading blob failed\n";
2175        git_footer_html();
2176}
2177
2178sub git_blame {
2179        my $fd;
2180
2181        if (!gitweb_check_feature('blame')) {
2182                die_error('403 Permission denied', "Permission denied");
2183        }
2184        die_error('404 Not Found', "File name not defined") if (!$file_name);
2185        $hash_base ||= git_get_head_hash($project);
2186        die_error(undef, "Couldn't find base commit") unless ($hash_base);
2187        my %co = parse_commit($hash_base)
2188                or die_error(undef, "Reading commit failed");
2189        if (!defined $hash) {
2190                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2191                        or die_error(undef, "Error lookup file");
2192        }
2193        open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2194                or die_error(undef, "Open git-annotate failed");
2195        git_header_html();
2196        my $formats_nav =
2197                $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2198                        "blob") .
2199                " | " .
2200                $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2201                        "head");
2202        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2203        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2204        git_print_page_path($file_name, 'blob', $hash_base);
2205        print "<div class=\"page_body\">\n";
2206        print <<HTML;
2207<table class="blame">
2208  <tr>
2209    <th>Commit</th>
2210    <th>Age</th>
2211    <th>Author</th>
2212    <th>Line</th>
2213    <th>Data</th>
2214  </tr>
2215HTML
2216        my @line_class = (qw(light dark));
2217        my $line_class_len = scalar (@line_class);
2218        my $line_class_num = $#line_class;
2219        while (my $line = <$fd>) {
2220                my $long_rev;
2221                my $short_rev;
2222                my $author;
2223                my $time;
2224                my $lineno;
2225                my $data;
2226                my $age;
2227                my $age_str;
2228                my $age_class;
2229
2230                chomp $line;
2231                $line_class_num = ($line_class_num + 1) % $line_class_len;
2232
2233                if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2234                        $long_rev = $1;
2235                        $author   = $2;
2236                        $time     = $3;
2237                        $lineno   = $4;
2238                        $data     = $5;
2239                } else {
2240                        print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2241                        next;
2242                }
2243                $short_rev  = substr ($long_rev, 0, 8);
2244                $age        = time () - $time;
2245                $age_str    = age_string ($age);
2246                $age_str    =~ s/ /&nbsp;/g;
2247                $age_class  = age_class($age);
2248                $author     = esc_html ($author);
2249                $author     =~ s/ /&nbsp;/g;
2250
2251                $data = untabify($data);
2252                $data = esc_html ($data);
2253
2254                print <<HTML;
2255  <tr class="$line_class[$line_class_num]">
2256    <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2257    <td class="$age_class">$age_str</td>
2258    <td>$author</td>
2259    <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2260    <td class="pre">$data</td>
2261  </tr>
2262HTML
2263        } # while (my $line = <$fd>)
2264        print "</table>\n\n";
2265        close $fd
2266                or print "Reading blob failed.\n";
2267        print "</div>";
2268        git_footer_html();
2269}
2270
2271sub git_tags {
2272        my $head = git_get_head_hash($project);
2273        git_header_html();
2274        git_print_page_nav('','', $head,undef,$head);
2275        git_print_header_div('summary', $project);
2276
2277        my $taglist = git_get_refs_list("refs/tags");
2278        if (defined @$taglist) {
2279                git_tags_body($taglist);
2280        }
2281        git_footer_html();
2282}
2283
2284sub git_heads {
2285        my $head = git_get_head_hash($project);
2286        git_header_html();
2287        git_print_page_nav('','', $head,undef,$head);
2288        git_print_header_div('summary', $project);
2289
2290        my $taglist = git_get_refs_list("refs/heads");
2291        if (defined @$taglist) {
2292                git_heads_body($taglist, $head);
2293        }
2294        git_footer_html();
2295}
2296
2297sub git_blob_plain {
2298        if (!defined $hash) {
2299                if (defined $file_name) {
2300                        my $base = $hash_base || git_get_head_hash($project);
2301                        $hash = git_get_hash_by_path($base, $file_name, "blob")
2302                                or die_error(undef, "Error lookup file");
2303                } else {
2304                        die_error(undef, "No file name defined");
2305                }
2306        }
2307        my $type = shift;
2308        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2309                or die_error(undef, "Couldn't cat $file_name, $hash");
2310
2311        $type ||= blob_mimetype($fd, $file_name);
2312
2313        # save as filename, even when no $file_name is given
2314        my $save_as = "$hash";
2315        if (defined $file_name) {
2316                $save_as = $file_name;
2317        } elsif ($type =~ m/^text\//) {
2318                $save_as .= '.txt';
2319        }
2320
2321        print $cgi->header(-type => "$type",
2322                           -content_disposition => "inline; filename=\"$save_as\"");
2323        undef $/;
2324        binmode STDOUT, ':raw';
2325        print <$fd>;
2326        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2327        $/ = "\n";
2328        close $fd;
2329}
2330
2331sub git_blob {
2332        if (!defined $hash) {
2333                if (defined $file_name) {
2334                        my $base = $hash_base || git_get_head_hash($project);
2335                        $hash = git_get_hash_by_path($base, $file_name, "blob")
2336                                or die_error(undef, "Error lookup file");
2337                } else {
2338                        die_error(undef, "No file name defined");
2339                }
2340        }
2341        my $have_blame = gitweb_check_feature('blame');
2342        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2343                or die_error(undef, "Couldn't cat $file_name, $hash");
2344        my $mimetype = blob_mimetype($fd, $file_name);
2345        if ($mimetype !~ m/^text\//) {
2346                close $fd;
2347                return git_blob_plain($mimetype);
2348        }
2349        git_header_html();
2350        my $formats_nav = '';
2351        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2352                if (defined $file_name) {
2353                        if ($have_blame) {
2354                                $formats_nav .=
2355                                        $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2356                                                               hash=>$hash, file_name=>$file_name)},
2357                                                "blame") .
2358                                        " | ";
2359                        }
2360                        $formats_nav .=
2361                                $cgi->a({-href => href(action=>"blob_plain",
2362                                                       hash=>$hash, file_name=>$file_name)},
2363                                        "plain") .
2364                                " | " .
2365                                $cgi->a({-href => href(action=>"blob",
2366                                                       hash_base=>"HEAD", file_name=>$file_name)},
2367                                        "head");
2368                } else {
2369                        $formats_nav .=
2370                                $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2371                }
2372                git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2373                git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2374        } else {
2375                print "<div class=\"page_nav\">\n" .
2376                      "<br/><br/></div>\n" .
2377                      "<div class=\"title\">$hash</div>\n";
2378        }
2379        git_print_page_path($file_name, "blob", $hash_base);
2380        print "<div class=\"page_body\">\n";
2381        my $nr;
2382        while (my $line = <$fd>) {
2383                chomp $line;
2384                $nr++;
2385                $line = untabify($line);
2386                printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2387                       $nr, $nr, $nr, esc_html($line);
2388        }
2389        close $fd
2390                or print "Reading blob failed.\n";
2391        print "</div>";
2392        git_footer_html();
2393}
2394
2395sub git_tree {
2396        if (!defined $hash) {
2397                $hash = git_get_head_hash($project);
2398                if (defined $file_name) {
2399                        my $base = $hash_base || $hash;
2400                        $hash = git_get_hash_by_path($base, $file_name, "tree");
2401                }
2402                if (!defined $hash_base) {
2403                        $hash_base = $hash;
2404                }
2405        }
2406        $/ = "\0";
2407        open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2408                or die_error(undef, "Open git-ls-tree failed");
2409        my @entries = map { chomp; $_ } <$fd>;
2410        close $fd or die_error(undef, "Reading tree failed");
2411        $/ = "\n";
2412
2413        my $refs = git_get_references();
2414        my $ref = format_ref_marker($refs, $hash_base);
2415        git_header_html();
2416        my %base_key = ();
2417        my $base = "";
2418        my $have_blame = gitweb_check_feature('blame');
2419        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2420                $base_key{hash_base} = $hash_base;
2421                git_print_page_nav('tree','', $hash_base);
2422                git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2423        } else {
2424                print "<div class=\"page_nav\">\n";
2425                print "<br/><br/></div>\n";
2426                print "<div class=\"title\">$hash</div>\n";
2427        }
2428        if (defined $file_name) {
2429                $base = esc_html("$file_name/");
2430        }
2431        git_print_page_path($file_name, 'tree', $hash_base);
2432        print "<div class=\"page_body\">\n";
2433        print "<table cellspacing=\"0\">\n";
2434        my $alternate = 0;
2435        foreach my $line (@entries) {
2436                #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
2437                $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2438                my $t_mode = $1;
2439                my $t_type = $2;
2440                my $t_hash = $3;
2441                my $t_name = validate_input($4);
2442                if ($alternate) {
2443                        print "<tr class=\"dark\">\n";
2444                } else {
2445                        print "<tr class=\"light\">\n";
2446                }
2447                $alternate ^= 1;
2448                print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2449                if ($t_type eq "blob") {
2450                        print "<td class=\"list\">" .
2451                              $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2452                                      -class => "list"}, esc_html($t_name)) .
2453                              "</td>\n" .
2454                              "<td class=\"link\">" .
2455                              $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2456                                      "blob");
2457                        if ($have_blame) {
2458                                print " | " .
2459                                        $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2460                                                "blame");
2461                        }
2462                        print " | " .
2463                              $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2464                                                     hash=>$t_hash, file_name=>"$base$t_name")},
2465                                      "history") .
2466                              " | " .
2467                              $cgi->a({-href => href(action=>"blob_plain",
2468                                                     hash=>$t_hash, file_name=>"$base$t_name")},
2469                                      "raw") .
2470                              "</td>\n";
2471                } elsif ($t_type eq "tree") {
2472                        print "<td class=\"list\">" .
2473                              $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2474                                      esc_html($t_name)) .
2475                              "</td>\n" .
2476                              "<td class=\"link\">" .
2477                              $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2478                                      "tree") .
2479                              " | " .
2480                              $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2481                                      "history") .
2482                              "</td>\n";
2483                }
2484                print "</tr>\n";
2485        }
2486        print "</table>\n" .
2487              "</div>";
2488        git_footer_html();
2489}
2490
2491sub git_snapshot {
2492
2493        my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2494        my $have_snapshot = (defined $ctype && defined $suffix);
2495        if (!$have_snapshot) {
2496                die_error('403 Permission denied', "Permission denied");
2497        }
2498
2499        if (!defined $hash) {
2500                $hash = git_get_head_hash($project);
2501        }
2502
2503        my $filename = basename($project) . "-$hash.tar.$suffix";
2504
2505        print $cgi->header(-type => 'application/x-tar',
2506                           -content_encoding => $ctype,
2507                           -content_disposition => "inline; filename=\"$filename\"",
2508                           -status => '200 OK');
2509
2510        open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2511                die_error(undef, "Execute git-tar-tree failed.");
2512        binmode STDOUT, ':raw';
2513        print <$fd>;
2514        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2515        close $fd;
2516
2517}
2518
2519sub git_log {
2520        my $head = git_get_head_hash($project);
2521        if (!defined $hash) {
2522                $hash = $head;
2523        }
2524        if (!defined $page) {
2525                $page = 0;
2526        }
2527        my $refs = git_get_references();
2528
2529        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2530        open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2531                or die_error(undef, "Open git-rev-list failed");
2532        my @revlist = map { chomp; $_ } <$fd>;
2533        close $fd;
2534
2535        my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2536
2537        git_header_html();
2538        git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2539
2540        if (!@revlist) {
2541                my %co = parse_commit($hash);
2542
2543                git_print_header_div('summary', $project);
2544                print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2545        }
2546        for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2547                my $commit = $revlist[$i];
2548                my $ref = format_ref_marker($refs, $commit);
2549                my %co = parse_commit($commit);
2550                next if !%co;
2551                my %ad = parse_date($co{'author_epoch'});
2552                git_print_header_div('commit',
2553                               "<span class=\"age\">$co{'age_string'}</span>" .
2554                               esc_html($co{'title'}) . $ref,
2555                               $commit);
2556                print "<div class=\"title_text\">\n" .
2557                      "<div class=\"log_link\">\n" .
2558                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2559                      " | " .
2560                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2561                      "<br/>\n" .
2562                      "</div>\n" .
2563                      "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
2564                      "</div>\n";
2565
2566                print "<div class=\"log_body\">\n";
2567                git_print_simplified_log($co{'comment'});
2568                print "</div>\n";
2569        }
2570        git_footer_html();
2571}
2572
2573sub git_commit {
2574        my %co = parse_commit($hash);
2575        if (!%co) {
2576                die_error(undef, "Unknown commit object");
2577        }
2578        my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2579        my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2580
2581        my $parent = $co{'parent'};
2582        if (!defined $parent) {
2583                $parent = "--root";
2584        }
2585        open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2586                or die_error(undef, "Open git-diff-tree failed");
2587        my @difftree = map { chomp; $_ } <$fd>;
2588        close $fd or die_error(undef, "Reading git-diff-tree failed");
2589
2590        # non-textual hash id's can be cached
2591        my $expires;
2592        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2593                $expires = "+1d";
2594        }
2595        my $refs = git_get_references();
2596        my $ref = format_ref_marker($refs, $co{'id'});
2597
2598        my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2599        my $have_snapshot = (defined $ctype && defined $suffix);
2600
2601        my $formats_nav = '';
2602        if (defined $file_name && defined $co{'parent'}) {
2603                my $parent = $co{'parent'};
2604                $formats_nav .=
2605                        $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2606                                "blame");
2607        }
2608        git_header_html(undef, $expires);
2609        git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2610                           $hash, $co{'tree'}, $hash,
2611                           $formats_nav);
2612
2613        if (defined $co{'parent'}) {
2614                git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2615        } else {
2616                git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2617        }
2618        print "<div class=\"title_text\">\n" .
2619              "<table cellspacing=\"0\">\n";
2620        print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2621              "<tr>" .
2622              "<td></td><td> $ad{'rfc2822'}";
2623        if ($ad{'hour_local'} < 6) {
2624                printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2625                       $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2626        } else {
2627                printf(" (%02d:%02d %s)",
2628                       $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2629        }
2630        print "</td>" .
2631              "</tr>\n";
2632        print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2633        print "<tr><td></td><td> $cd{'rfc2822'}" .
2634              sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2635              "</td></tr>\n";
2636        print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2637        print "<tr>" .
2638              "<td>tree</td>" .
2639              "<td class=\"sha1\">" .
2640              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2641                       class => "list"}, $co{'tree'}) .
2642              "</td>" .
2643              "<td class=\"link\">" .
2644              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2645                      "tree");
2646        if ($have_snapshot) {
2647                print " | " .
2648                      $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2649        }
2650        print "</td>" .
2651              "</tr>\n";
2652        my $parents = $co{'parents'};
2653        foreach my $par (@$parents) {
2654                print "<tr>" .
2655                      "<td>parent</td>" .
2656                      "<td class=\"sha1\">" .
2657                      $cgi->a({-href => href(action=>"commit", hash=>$par),
2658                               class => "list"}, $par) .
2659                      "</td>" .
2660                      "<td class=\"link\">" .
2661                      $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2662                      " | " .
2663                      $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2664                      "</td>" .
2665                      "</tr>\n";
2666        }
2667        print "</table>".
2668              "</div>\n";
2669
2670        print "<div class=\"page_body\">\n";
2671        git_print_log($co{'comment'});
2672        print "</div>\n";
2673
2674        git_difftree_body(\@difftree, $hash, $parent);
2675
2676        git_footer_html();
2677}
2678
2679sub git_blobdiff {
2680        mkdir($git_temp, 0700);
2681        git_header_html();
2682        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2683                my $formats_nav =
2684                        $cgi->a({-href => href(action=>"blobdiff_plain",
2685                                               hash=>$hash, hash_parent=>$hash_parent)},
2686                                "plain");
2687                git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2688                git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2689        } else {
2690                print <<HTML;
2691<div class="page_nav"><br/><br/></div>
2692<div class="title">$hash vs $hash_parent</div>
2693HTML
2694        }
2695        git_print_page_path($file_name, "blob", $hash_base);
2696        print "<div class=\"page_body\">\n" .
2697              "<div class=\"diff_info\">blob:" .
2698              $cgi->a({-href => href(action=>"blob", hash=>$hash_parent,
2699                                     hash_base=>$hash_base, file_name=>($file_parent || $file_name))},
2700                      $hash_parent) .
2701              " -> blob:" .
2702              $cgi->a({-href => href(action=>"blob", hash=>$hash,
2703                                     hash_base=>$hash_base, file_name=>$file_name)},
2704                      $hash) .
2705              "</div>\n";
2706        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2707        print "</div>"; # page_body
2708        git_footer_html();
2709}
2710
2711sub git_blobdiff_plain {
2712        mkdir($git_temp, 0700);
2713        print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2714        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2715}
2716
2717sub git_commitdiff {
2718        my $format = shift || 'html';
2719        my %co = parse_commit($hash);
2720        if (!%co) {
2721                die_error(undef, "Unknown commit object");
2722        }
2723        if (!defined $hash_parent) {
2724                $hash_parent = $co{'parent'} || '--root';
2725        }
2726
2727        # read commitdiff
2728        my $fd;
2729        my @difftree;
2730        my @patchset;
2731        if ($format eq 'html') {
2732                open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C',
2733                        "--patch-with-raw", "--full-index", $hash_parent, $hash
2734                        or die_error(undef, "Open git-diff-tree failed");
2735
2736                while (chomp(my $line = <$fd>)) {
2737                        # empty line ends raw part of diff-tree output
2738                        last unless $line;
2739                        push @difftree, $line;
2740                }
2741                @patchset = map { chomp; $_ } <$fd>;
2742
2743                close $fd
2744                        or die_error(undef, "Reading git-diff-tree failed");
2745        } elsif ($format eq 'plain') {
2746                open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-B', $hash_parent, $hash
2747                        or die_error(undef, "Open git-diff-tree failed");
2748        } else {
2749                die_error(undef, "Unknown commitdiff format");
2750        }
2751
2752        # non-textual hash id's can be cached
2753        my $expires;
2754        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2755                $expires = "+1d";
2756        }
2757
2758        # write commit message
2759        if ($format eq 'html') {
2760                my $refs = git_get_references();
2761                my $ref = format_ref_marker($refs, $co{'id'});
2762                my $formats_nav =
2763                        $cgi->a({-href => href(action=>"commitdiff_plain",
2764                                               hash=>$hash, hash_parent=>$hash_parent)},
2765                                "plain");
2766
2767                git_header_html(undef, $expires);
2768                git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2769                git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2770                print "<div class=\"page_body\">\n";
2771                print "<div class=\"log\">\n";
2772                git_print_simplified_log($co{'comment'}, 1); # skip title
2773                print "</div>\n"; # class="log"
2774
2775        } elsif ($format eq 'plain') {
2776                my $refs = git_get_references("tags");
2777                my @tagnames;
2778                if (exists $refs->{$hash}) {
2779                        @tagnames = map { s|^tags/|| } $refs->{$hash};
2780                }
2781                my $filename = basename($project) . "-$hash.patch";
2782
2783                print $cgi->header(
2784                        -type => 'text/plain',
2785                        -charset => 'utf-8',
2786                        -expires => $expires,
2787                        -content_disposition => qq(inline; filename="$filename"));
2788                my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2789                print <<TEXT;
2790From: $co{'author'}
2791Date: $ad{'rfc2822'} ($ad{'tz_local'})
2792Subject: $co{'title'}
2793TEXT
2794                foreach my $tag (@tagnames) {
2795                        print "X-Git-Tag: $tag\n";
2796                }
2797                print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2798                foreach my $line (@{$co{'comment'}}) {
2799                        print "$line\n";
2800                }
2801                print "---\n\n";
2802        }
2803
2804        # write patch
2805        if ($format eq 'html') {
2806                #git_difftree_body(\@difftree, $hash, $hash_parent);
2807                #print "<br/>\n";
2808
2809                git_patchset_body(\@patchset, \@difftree, $hash, $hash_parent);
2810
2811                print "</div>\n"; # class="page_body"
2812                git_footer_html();
2813
2814        } elsif ($format eq 'plain') {
2815                local $/ = undef;
2816                print <$fd>;
2817                close $fd
2818                        or print "Reading git-diff-tree failed\n";
2819        }
2820}
2821
2822sub git_commitdiff_plain {
2823        git_commitdiff('plain');
2824}
2825
2826sub git_history {
2827        if (!defined $hash_base) {
2828                $hash_base = git_get_head_hash($project);
2829        }
2830        my $ftype;
2831        my %co = parse_commit($hash_base);
2832        if (!%co) {
2833                die_error(undef, "Unknown commit object");
2834        }
2835        my $refs = git_get_references();
2836        git_header_html();
2837        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2838        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2839        if (!defined $hash && defined $file_name) {
2840                $hash = git_get_hash_by_path($hash_base, $file_name);
2841        }
2842        if (defined $hash) {
2843                $ftype = git_get_type($hash);
2844        }
2845        git_print_page_path($file_name, $ftype, $hash_base);
2846
2847        open my $fd, "-|",
2848                $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2849        git_history_body($fd, $refs, $hash_base, $ftype);
2850
2851        close $fd;
2852        git_footer_html();
2853}
2854
2855sub git_search {
2856        if (!defined $searchtext) {
2857                die_error(undef, "Text field empty");
2858        }
2859        if (!defined $hash) {
2860                $hash = git_get_head_hash($project);
2861        }
2862        my %co = parse_commit($hash);
2863        if (!%co) {
2864                die_error(undef, "Unknown commit object");
2865        }
2866        # pickaxe may take all resources of your box and run for several minutes
2867        # with every query - so decide by yourself how public you make this feature :)
2868        my $commit_search = 1;
2869        my $author_search = 0;
2870        my $committer_search = 0;
2871        my $pickaxe_search = 0;
2872        if ($searchtext =~ s/^author\\://i) {
2873                $author_search = 1;
2874        } elsif ($searchtext =~ s/^committer\\://i) {
2875                $committer_search = 1;
2876        } elsif ($searchtext =~ s/^pickaxe\\://i) {
2877                $commit_search = 0;
2878                $pickaxe_search = 1;
2879        }
2880        git_header_html();
2881        git_print_page_nav('','', $hash,$co{'tree'},$hash);
2882        git_print_header_div('commit', esc_html($co{'title'}), $hash);
2883
2884        print "<table cellspacing=\"0\">\n";
2885        my $alternate = 0;
2886        if ($commit_search) {
2887                $/ = "\0";
2888                open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2889                while (my $commit_text = <$fd>) {
2890                        if (!grep m/$searchtext/i, $commit_text) {
2891                                next;
2892                        }
2893                        if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2894                                next;
2895                        }
2896                        if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2897                                next;
2898                        }
2899                        my @commit_lines = split "\n", $commit_text;
2900                        my %co = parse_commit(undef, \@commit_lines);
2901                        if (!%co) {
2902                                next;
2903                        }
2904                        if ($alternate) {
2905                                print "<tr class=\"dark\">\n";
2906                        } else {
2907                                print "<tr class=\"light\">\n";
2908                        }
2909                        $alternate ^= 1;
2910                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2911                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2912                              "<td>" .
2913                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2914                                       esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2915                        my $comment = $co{'comment'};
2916                        foreach my $line (@$comment) {
2917                                if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2918                                        my $lead = esc_html($1) || "";
2919                                        $lead = chop_str($lead, 30, 10);
2920                                        my $match = esc_html($2) || "";
2921                                        my $trail = esc_html($3) || "";
2922                                        $trail = chop_str($trail, 30, 10);
2923                                        my $text = "$lead<span class=\"match\">$match</span>$trail";
2924                                        print chop_str($text, 80, 5) . "<br/>\n";
2925                                }
2926                        }
2927                        print "</td>\n" .
2928                              "<td class=\"link\">" .
2929                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2930                              " | " .
2931                              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2932                        print "</td>\n" .
2933                              "</tr>\n";
2934                }
2935                close $fd;
2936        }
2937
2938        if ($pickaxe_search) {
2939                $/ = "\n";
2940                open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2941                undef %co;
2942                my @files;
2943                while (my $line = <$fd>) {
2944                        if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2945                                my %set;
2946                                $set{'file'} = $6;
2947                                $set{'from_id'} = $3;
2948                                $set{'to_id'} = $4;
2949                                $set{'id'} = $set{'to_id'};
2950                                if ($set{'id'} =~ m/0{40}/) {
2951                                        $set{'id'} = $set{'from_id'};
2952                                }
2953                                if ($set{'id'} =~ m/0{40}/) {
2954                                        next;
2955                                }
2956                                push @files, \%set;
2957                        } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2958                                if (%co) {
2959                                        if ($alternate) {
2960                                                print "<tr class=\"dark\">\n";
2961                                        } else {
2962                                                print "<tr class=\"light\">\n";
2963                                        }
2964                                        $alternate ^= 1;
2965                                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2966                                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2967                                              "<td>" .
2968                                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
2969                                                      -class => "list subject"},
2970                                                      esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2971                                        while (my $setref = shift @files) {
2972                                                my %set = %$setref;
2973                                                print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
2974                                                                             hash=>$set{'id'}, file_name=>$set{'file'}),
2975                                                              -class => "list"},
2976                                                              "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2977                                                      "<br/>\n";
2978                                        }
2979                                        print "</td>\n" .
2980                                              "<td class=\"link\">" .
2981                                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2982                                              " | " .
2983                                              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2984                                        print "</td>\n" .
2985                                              "</tr>\n";
2986                                }
2987                                %co = parse_commit($1);
2988                        }
2989                }
2990                close $fd;
2991        }
2992        print "</table>\n";
2993        git_footer_html();
2994}
2995
2996sub git_shortlog {
2997        my $head = git_get_head_hash($project);
2998        if (!defined $hash) {
2999                $hash = $head;
3000        }
3001        if (!defined $page) {
3002                $page = 0;
3003        }
3004        my $refs = git_get_references();
3005
3006        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3007        open my $fd, "-|", $GIT, "rev-list", $limit, $hash
3008                or die_error(undef, "Open git-rev-list failed");
3009        my @revlist = map { chomp; $_ } <$fd>;
3010        close $fd;
3011
3012        my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3013        my $next_link = '';
3014        if ($#revlist >= (100 * ($page+1)-1)) {
3015                $next_link =
3016                        $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3017                                 -title => "Alt-n"}, "next");
3018        }
3019
3020
3021        git_header_html();
3022        git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3023        git_print_header_div('summary', $project);
3024
3025        git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3026
3027        git_footer_html();
3028}
3029
3030## ......................................................................
3031## feeds (RSS, OPML)
3032
3033sub git_rss {
3034        # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3035        open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
3036                or die_error(undef, "Open git-rev-list failed");
3037        my @revlist = map { chomp; $_ } <$fd>;
3038        close $fd or die_error(undef, "Reading git-rev-list failed");
3039        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3040        print <<XML;
3041<?xml version="1.0" encoding="utf-8"?>
3042<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3043<channel>
3044<title>$project $my_uri $my_url</title>
3045<link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3046<description>$project log</description>
3047<language>en</language>
3048XML
3049
3050        for (my $i = 0; $i <= $#revlist; $i++) {
3051                my $commit = $revlist[$i];
3052                my %co = parse_commit($commit);
3053                # we read 150, we always show 30 and the ones more recent than 48 hours
3054                if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3055                        last;
3056                }
3057                my %cd = parse_date($co{'committer_epoch'});
3058                open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
3059                my @difftree = map { chomp; $_ } <$fd>;
3060                close $fd or next;
3061                print "<item>\n" .
3062                      "<title>" .
3063                      sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3064                      "</title>\n" .
3065                      "<author>" . esc_html($co{'author'}) . "</author>\n" .
3066                      "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3067                      "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3068                      "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3069                      "<description>" . esc_html($co{'title'}) . "</description>\n" .
3070                      "<content:encoded>" .
3071                      "<![CDATA[\n";
3072                my $comment = $co{'comment'};
3073                foreach my $line (@$comment) {
3074                        $line = decode("utf8", $line, Encode::FB_DEFAULT);
3075                        print "$line<br/>\n";
3076                }
3077                print "<br/>\n";
3078                foreach my $line (@difftree) {
3079                        if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3080                                next;
3081                        }
3082                        my $file = validate_input(unquote($7));
3083                        $file = decode("utf8", $file, Encode::FB_DEFAULT);
3084                        print "$file<br/>\n";
3085                }
3086                print "]]>\n" .
3087                      "</content:encoded>\n" .
3088                      "</item>\n";
3089        }
3090        print "</channel></rss>";
3091}
3092
3093sub git_opml {
3094        my @list = git_get_projects_list();
3095
3096        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3097        print <<XML;
3098<?xml version="1.0" encoding="utf-8"?>
3099<opml version="1.0">
3100<head>
3101  <title>$site_name Git OPML Export</title>
3102</head>
3103<body>
3104<outline text="git RSS feeds">
3105XML
3106
3107        foreach my $pr (@list) {
3108                my %proj = %$pr;
3109                my $head = git_get_head_hash($proj{'path'});
3110                if (!defined $head) {
3111                        next;
3112                }
3113                $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3114                my %co = parse_commit($head);
3115                if (!%co) {
3116                        next;
3117                }
3118
3119                my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3120                my $rss  = "$my_url?p=$proj{'path'};a=rss";
3121                my $html = "$my_url?p=$proj{'path'};a=summary";
3122                print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3123        }
3124        print <<XML;
3125</outline>
3126</body>
3127</opml>
3128XML
3129}