gitweb / gitweb.perlon commit gitweb: Streamify patch output in git_commitdiff (157e43b)
   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 ($fd, $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:
1552        while (my $patch_line @$fd>) {
1553                chomp $patch_line;
1554
1555                if ($patch_line =~ m/^diff /) { # "git diff" header
1556                        # beginning of patch (in patchset)
1557                        if ($patch_found) {
1558                                # close previous patch
1559                                print "</div>\n"; # class="patch"
1560                        } else {
1561                                # first patch in patchset
1562                                $patch_found = 1;
1563                        }
1564                        print "<div class=\"patch\">\n";
1565
1566                        %diffinfo = parse_difftree_raw_line($difftree->[$patch_idx++]);
1567
1568                        # for now, no extended header, hence we skip empty patches
1569                        # companion to  next LINE if $in_header;
1570                        if ($diffinfo{'from_id'} eq $diffinfo{'to_id'}) { # no change
1571                                $in_header = 1;
1572                                next LINE;
1573                        }
1574
1575                        if ($diffinfo{'status'} eq "A") { # added
1576                                print "<div class=\"diff_info\">" . file_type($diffinfo{'to_mode'}) . ":" .
1577                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1578                                                             hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1579                                              $diffinfo{'to_id'}) . "(new)" .
1580                                      "</div>\n"; # class="diff_info"
1581
1582                        } elsif ($diffinfo{'status'} eq "D") { # deleted
1583                                print "<div class=\"diff_info\">" . file_type($diffinfo{'from_mode'}) . ":" .
1584                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1585                                                             hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1586                                              $diffinfo{'from_id'}) . "(deleted)" .
1587                                      "</div>\n"; # class="diff_info"
1588
1589                        } elsif ($diffinfo{'status'} eq "R" || # renamed
1590                                 $diffinfo{'status'} eq "C") { # copied
1591                                print "<div class=\"diff_info\">" .
1592                                      file_type($diffinfo{'from_mode'}) . ":" .
1593                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1594                                                             hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'from_file'})},
1595                                              $diffinfo{'from_id'}) .
1596                                      " -> " .
1597                                      file_type($diffinfo{'to_mode'}) . ":" .
1598                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1599                                                             hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'to_file'})},
1600                                              $diffinfo{'to_id'});
1601                                print "</div>\n"; # class="diff_info"
1602
1603                        } else { # modified, mode changed, ...
1604                                print "<div class=\"diff_info\">" .
1605                                      file_type($diffinfo{'from_mode'}) . ":" .
1606                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash_parent,
1607                                                             hash=>$diffinfo{'from_id'}, file_name=>$diffinfo{'file'})},
1608                                              $diffinfo{'from_id'}) .
1609                                      " -> " .
1610                                      file_type($diffinfo{'to_mode'}) . ":" .
1611                                      $cgi->a({-href => href(action=>"blob", hash_base=>$hash,
1612                                                             hash=>$diffinfo{'to_id'}, file_name=>$diffinfo{'file'})},
1613                                              $diffinfo{'to_id'});
1614                                print "</div>\n"; # class="diff_info"
1615                        }
1616
1617                        #print "<div class=\"diff extended_header\">\n";
1618                        $in_header = 1;
1619                        next LINE;
1620                } # start of patch in patchset
1621
1622
1623                if ($in_header && $patch_line =~ m/^---/) {
1624                        #print "</div>\n"
1625                        $in_header = 0;
1626                }
1627                next LINE if $in_header;
1628
1629                print format_diff_line($patch_line);
1630        }
1631        print "</div>\n" if $patch_found; # class="patch"
1632
1633        print "</div>\n"; # class="patchset"
1634}
1635
1636# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1637
1638sub git_shortlog_body {
1639        # uses global variable $project
1640        my ($revlist, $from, $to, $refs, $extra) = @_;
1641
1642        my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1643        my $have_snapshot = (defined $ctype && defined $suffix);
1644
1645        $from = 0 unless defined $from;
1646        $to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1647
1648        print "<table class=\"shortlog\" cellspacing=\"0\">\n";
1649        my $alternate = 0;
1650        for (my $i = $from; $i <= $to; $i++) {
1651                my $commit = $revlist->[$i];
1652                #my $ref = defined $refs ? format_ref_marker($refs, $commit) : '';
1653                my $ref = format_ref_marker($refs, $commit);
1654                my %co = parse_commit($commit);
1655                if ($alternate) {
1656                        print "<tr class=\"dark\">\n";
1657                } else {
1658                        print "<tr class=\"light\">\n";
1659                }
1660                $alternate ^= 1;
1661                # git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
1662                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1663                      "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1664                      "<td>";
1665                print format_subject_html($co{'title'}, $co{'title_short'},
1666                                          href(action=>"commit", hash=>$commit), $ref);
1667                print "</td>\n" .
1668                      "<td class=\"link\">" .
1669                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1670                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff");
1671                if ($have_snapshot) {
1672                        print " | " .  $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
1673                }
1674                print "</td>\n" .
1675                      "</tr>\n";
1676        }
1677        if (defined $extra) {
1678                print "<tr>\n" .
1679                      "<td colspan=\"4\">$extra</td>\n" .
1680                      "</tr>\n";
1681        }
1682        print "</table>\n";
1683}
1684
1685sub git_history_body {
1686        # Warning: assumes constant type (blob or tree) during history
1687        my ($fd, $refs, $hash_base, $ftype, $extra) = @_;
1688
1689        print "<table class=\"history\" cellspacing=\"0\">\n";
1690        my $alternate = 0;
1691        while (my $line = <$fd>) {
1692                if ($line !~ m/^([0-9a-fA-F]{40})/) {
1693                        next;
1694                }
1695
1696                my $commit = $1;
1697                my %co = parse_commit($commit);
1698                if (!%co) {
1699                        next;
1700                }
1701
1702                my $ref = format_ref_marker($refs, $commit);
1703
1704                if ($alternate) {
1705                        print "<tr class=\"dark\">\n";
1706                } else {
1707                        print "<tr class=\"light\">\n";
1708                }
1709                $alternate ^= 1;
1710                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
1711                      # shortlog uses      chop_str($co{'author_name'}, 10)
1712                      "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
1713                      "<td>";
1714                # originally git_history used chop_str($co{'title'}, 50)
1715                print format_subject_html($co{'title'}, $co{'title_short'},
1716                                          href(action=>"commit", hash=>$commit), $ref);
1717                print "</td>\n" .
1718                      "<td class=\"link\">" .
1719                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
1720                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
1721                      $cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)}, $ftype);
1722
1723                if ($ftype eq 'blob') {
1724                        my $blob_current = git_get_hash_by_path($hash_base, $file_name);
1725                        my $blob_parent  = git_get_hash_by_path($commit, $file_name);
1726                        if (defined $blob_current && defined $blob_parent &&
1727                                        $blob_current ne $blob_parent) {
1728                                print " | " .
1729                                        $cgi->a({-href => href(action=>"blobdiff", hash=>$blob_current, hash_parent=>$blob_parent,
1730                                                               hash_base=>$commit, file_name=>$file_name)},
1731                                                "diff to current");
1732                        }
1733                }
1734                print "</td>\n" .
1735                      "</tr>\n";
1736        }
1737        if (defined $extra) {
1738                print "<tr>\n" .
1739                      "<td colspan=\"4\">$extra</td>\n" .
1740                      "</tr>\n";
1741        }
1742        print "</table>\n";
1743}
1744
1745sub git_tags_body {
1746        # uses global variable $project
1747        my ($taglist, $from, $to, $extra) = @_;
1748        $from = 0 unless defined $from;
1749        $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1750
1751        print "<table class=\"tags\" cellspacing=\"0\">\n";
1752        my $alternate = 0;
1753        for (my $i = $from; $i <= $to; $i++) {
1754                my $entry = $taglist->[$i];
1755                my %tag = %$entry;
1756                my $comment_lines = $tag{'comment'};
1757                my $comment = shift @$comment_lines;
1758                my $comment_short;
1759                if (defined $comment) {
1760                        $comment_short = chop_str($comment, 30, 5);
1761                }
1762                if ($alternate) {
1763                        print "<tr class=\"dark\">\n";
1764                } else {
1765                        print "<tr class=\"light\">\n";
1766                }
1767                $alternate ^= 1;
1768                print "<td><i>$tag{'age'}</i></td>\n" .
1769                      "<td>" .
1770                      $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),
1771                               -class => "list name"}, esc_html($tag{'name'})) .
1772                      "</td>\n" .
1773                      "<td>";
1774                if (defined $comment) {
1775                        print format_subject_html($comment, $comment_short,
1776                                                  href(action=>"tag", hash=>$tag{'id'}));
1777                }
1778                print "</td>\n" .
1779                      "<td class=\"selflink\">";
1780                if ($tag{'type'} eq "tag") {
1781                        print $cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})}, "tag");
1782                } else {
1783                        print "&nbsp;";
1784                }
1785                print "</td>\n" .
1786                      "<td class=\"link\">" . " | " .
1787                      $cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})}, $tag{'reftype'});
1788                if ($tag{'reftype'} eq "commit") {
1789                        print " | " . $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") .
1790                              " | " . $cgi->a({-href => href(action=>"log", hash=>$tag{'refid'})}, "log");
1791                } elsif ($tag{'reftype'} eq "blob") {
1792                        print " | " . $cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})}, "raw");
1793                }
1794                print "</td>\n" .
1795                      "</tr>";
1796        }
1797        if (defined $extra) {
1798                print "<tr>\n" .
1799                      "<td colspan=\"5\">$extra</td>\n" .
1800                      "</tr>\n";
1801        }
1802        print "</table>\n";
1803}
1804
1805sub git_heads_body {
1806        # uses global variable $project
1807        my ($taglist, $head, $from, $to, $extra) = @_;
1808        $from = 0 unless defined $from;
1809        $to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);
1810
1811        print "<table class=\"heads\" cellspacing=\"0\">\n";
1812        my $alternate = 0;
1813        for (my $i = $from; $i <= $to; $i++) {
1814                my $entry = $taglist->[$i];
1815                my %tag = %$entry;
1816                my $curr = $tag{'id'} eq $head;
1817                if ($alternate) {
1818                        print "<tr class=\"dark\">\n";
1819                } else {
1820                        print "<tr class=\"light\">\n";
1821                }
1822                $alternate ^= 1;
1823                print "<td><i>$tag{'age'}</i></td>\n" .
1824                      ($tag{'id'} eq $head ? "<td class=\"current_head\">" : "<td>") .
1825                      $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'}),
1826                               -class => "list name"},esc_html($tag{'name'})) .
1827                      "</td>\n" .
1828                      "<td class=\"link\">" .
1829                      $cgi->a({-href => href(action=>"shortlog", hash=>$tag{'name'})}, "shortlog") . " | " .
1830                      $cgi->a({-href => href(action=>"log", hash=>$tag{'name'})}, "log") .
1831                      "</td>\n" .
1832                      "</tr>";
1833        }
1834        if (defined $extra) {
1835                print "<tr>\n" .
1836                      "<td colspan=\"3\">$extra</td>\n" .
1837                      "</tr>\n";
1838        }
1839        print "</table>\n";
1840}
1841
1842## ----------------------------------------------------------------------
1843## functions printing large fragments, format as one of arguments
1844
1845sub git_diff_print {
1846        my $from = shift;
1847        my $from_name = shift;
1848        my $to = shift;
1849        my $to_name = shift;
1850        my $format = shift || "html";
1851
1852        my $from_tmp = "/dev/null";
1853        my $to_tmp = "/dev/null";
1854        my $pid = $$;
1855
1856        # create tmp from-file
1857        if (defined $from) {
1858                $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
1859                open my $fd2, "> $from_tmp";
1860                open my $fd, "-|", $GIT, "cat-file", "blob", $from;
1861                my @file = <$fd>;
1862                print $fd2 @file;
1863                close $fd2;
1864                close $fd;
1865        }
1866
1867        # create tmp to-file
1868        if (defined $to) {
1869                $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
1870                open my $fd2, "> $to_tmp";
1871                open my $fd, "-|", $GIT, "cat-file", "blob", $to;
1872                my @file = <$fd>;
1873                print $fd2 @file;
1874                close $fd2;
1875                close $fd;
1876        }
1877
1878        open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
1879        if ($format eq "plain") {
1880                undef $/;
1881                print <$fd>;
1882                $/ = "\n";
1883        } else {
1884                while (my $line = <$fd>) {
1885                        chomp $line;
1886                        my $char = substr($line, 0, 1);
1887                        my $diff_class = "";
1888                        if ($char eq '+') {
1889                                $diff_class = " add";
1890                        } elsif ($char eq "-") {
1891                                $diff_class = " rem";
1892                        } elsif ($char eq "@") {
1893                                $diff_class = " chunk_header";
1894                        } elsif ($char eq "\\") {
1895                                # skip errors
1896                                next;
1897                        }
1898                        $line = untabify($line);
1899                        print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
1900                }
1901        }
1902        close $fd;
1903
1904        if (defined $from) {
1905                unlink($from_tmp);
1906        }
1907        if (defined $to) {
1908                unlink($to_tmp);
1909        }
1910}
1911
1912
1913## ======================================================================
1914## ======================================================================
1915## actions
1916
1917sub git_project_list {
1918        my $order = $cgi->param('o');
1919        if (defined $order && $order !~ m/project|descr|owner|age/) {
1920                die_error(undef, "Unknown order parameter");
1921        }
1922
1923        my @list = git_get_projects_list();
1924        my @projects;
1925        if (!@list) {
1926                die_error(undef, "No projects found");
1927        }
1928        foreach my $pr (@list) {
1929                my $head = git_get_head_hash($pr->{'path'});
1930                if (!defined $head) {
1931                        next;
1932                }
1933                $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
1934                my %co = parse_commit($head);
1935                if (!%co) {
1936                        next;
1937                }
1938                $pr->{'commit'} = \%co;
1939                if (!defined $pr->{'descr'}) {
1940                        my $descr = git_get_project_description($pr->{'path'}) || "";
1941                        $pr->{'descr'} = chop_str($descr, 25, 5);
1942                }
1943                if (!defined $pr->{'owner'}) {
1944                        $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
1945                }
1946                push @projects, $pr;
1947        }
1948
1949        git_header_html();
1950        if (-f $home_text) {
1951                print "<div class=\"index_include\">\n";
1952                open (my $fd, $home_text);
1953                print <$fd>;
1954                close $fd;
1955                print "</div>\n";
1956        }
1957        print "<table class=\"project_list\">\n" .
1958              "<tr>\n";
1959        $order ||= "project";
1960        if ($order eq "project") {
1961                @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
1962                print "<th>Project</th>\n";
1963        } else {
1964                print "<th>" .
1965                      $cgi->a({-href => "$my_uri?" . esc_param("o=project"),
1966                               -class => "header"}, "Project") .
1967                      "</th>\n";
1968        }
1969        if ($order eq "descr") {
1970                @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
1971                print "<th>Description</th>\n";
1972        } else {
1973                print "<th>" .
1974                      $cgi->a({-href => "$my_uri?" . esc_param("o=descr"),
1975                               -class => "header"}, "Description") .
1976                      "</th>\n";
1977        }
1978        if ($order eq "owner") {
1979                @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
1980                print "<th>Owner</th>\n";
1981        } else {
1982                print "<th>" .
1983                      $cgi->a({-href => "$my_uri?" . esc_param("o=owner"),
1984                               -class => "header"}, "Owner") .
1985                      "</th>\n";
1986        }
1987        if ($order eq "age") {
1988                @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
1989                print "<th>Last Change</th>\n";
1990        } else {
1991                print "<th>" .
1992                      $cgi->a({-href => "$my_uri?" . esc_param("o=age"),
1993                               -class => "header"}, "Last Change") .
1994                      "</th>\n";
1995        }
1996        print "<th></th>\n" .
1997              "</tr>\n";
1998        my $alternate = 0;
1999        foreach my $pr (@projects) {
2000                if ($alternate) {
2001                        print "<tr class=\"dark\">\n";
2002                } else {
2003                        print "<tr class=\"light\">\n";
2004                }
2005                $alternate ^= 1;
2006                print "<td>" . $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),
2007                                        -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
2008                      "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
2009                      "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
2010                print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" .
2011                      $pr->{'commit'}{'age_string'} . "</td>\n" .
2012                      "<td class=\"link\">" .
2013                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")}, "summary")   . " | " .
2014                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")}, "shortlog") . " | " .
2015                      $cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")}, "log") .
2016                      "</td>\n" .
2017                      "</tr>\n";
2018        }
2019        print "</table>\n";
2020        git_footer_html();
2021}
2022
2023sub git_summary {
2024        my $descr = git_get_project_description($project) || "none";
2025        my $head = git_get_head_hash($project);
2026        my %co = parse_commit($head);
2027        my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2028
2029        my $owner = git_get_project_owner($project);
2030
2031        my $refs = git_get_references();
2032        git_header_html();
2033        git_print_page_nav('summary','', $head);
2034
2035        print "<div class=\"title\">&nbsp;</div>\n";
2036        print "<table cellspacing=\"0\">\n" .
2037              "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
2038              "<tr><td>owner</td><td>$owner</td></tr>\n" .
2039              "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";
2040        # use per project git URL list in $projectroot/$project/cloneurl
2041        # or make project git URL from git base URL and project name
2042        my $url_tag = "URL";
2043        my @url_list = git_get_project_url_list($project);
2044        @url_list = map { "$_/$project" } @git_base_url_list unless @url_list;
2045        foreach my $git_url (@url_list) {
2046                next unless $git_url;
2047                print "<tr><td>$url_tag</td><td>$git_url</td></tr>\n";
2048                $url_tag = "";
2049        }
2050        print "</table>\n";
2051
2052        open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_get_head_hash($project)
2053                or die_error(undef, "Open git-rev-list failed");
2054        my @revlist = map { chomp; $_ } <$fd>;
2055        close $fd;
2056        git_print_header_div('shortlog');
2057        git_shortlog_body(\@revlist, 0, 15, $refs,
2058                          $cgi->a({-href => href(action=>"shortlog")}, "..."));
2059
2060        my $taglist = git_get_refs_list("refs/tags");
2061        if (defined @$taglist) {
2062                git_print_header_div('tags');
2063                git_tags_body($taglist, 0, 15,
2064                              $cgi->a({-href => href(action=>"tags")}, "..."));
2065        }
2066
2067        my $headlist = git_get_refs_list("refs/heads");
2068        if (defined @$headlist) {
2069                git_print_header_div('heads');
2070                git_heads_body($headlist, $head, 0, 15,
2071                               $cgi->a({-href => href(action=>"heads")}, "..."));
2072        }
2073
2074        git_footer_html();
2075}
2076
2077sub git_tag {
2078        my $head = git_get_head_hash($project);
2079        git_header_html();
2080        git_print_page_nav('','', $head,undef,$head);
2081        my %tag = parse_tag($hash);
2082        git_print_header_div('commit', esc_html($tag{'name'}), $hash);
2083        print "<div class=\"title_text\">\n" .
2084              "<table cellspacing=\"0\">\n" .
2085              "<tr>\n" .
2086              "<td>object</td>\n" .
2087              "<td>" . $cgi->a({-class => "list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2088                               $tag{'object'}) . "</td>\n" .
2089              "<td class=\"link\">" . $cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},
2090                                              $tag{'type'}) . "</td>\n" .
2091              "</tr>\n";
2092        if (defined($tag{'author'})) {
2093                my %ad = parse_date($tag{'epoch'}, $tag{'tz'});
2094                print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
2095                print "<tr><td></td><td>" . $ad{'rfc2822'} .
2096                        sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) .
2097                        "</td></tr>\n";
2098        }
2099        print "</table>\n\n" .
2100              "</div>\n";
2101        print "<div class=\"page_body\">";
2102        my $comment = $tag{'comment'};
2103        foreach my $line (@$comment) {
2104                print esc_html($line) . "<br/>\n";
2105        }
2106        print "</div>\n";
2107        git_footer_html();
2108}
2109
2110sub git_blame2 {
2111        my $fd;
2112        my $ftype;
2113
2114        if (!gitweb_check_feature('blame')) {
2115                die_error('403 Permission denied', "Permission denied");
2116        }
2117        die_error('404 Not Found', "File name not defined") if (!$file_name);
2118        $hash_base ||= git_get_head_hash($project);
2119        die_error(undef, "Couldn't find base commit") unless ($hash_base);
2120        my %co = parse_commit($hash_base)
2121                or die_error(undef, "Reading commit failed");
2122        if (!defined $hash) {
2123                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2124                        or die_error(undef, "Error looking up file");
2125        }
2126        $ftype = git_get_type($hash);
2127        if ($ftype !~ "blob") {
2128                die_error("400 Bad Request", "Object is not a blob");
2129        }
2130        open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
2131                or die_error(undef, "Open git-blame failed");
2132        git_header_html();
2133        my $formats_nav =
2134                $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2135                        "blob") .
2136                " | " .
2137                $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2138                        "head");
2139        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2140        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2141        git_print_page_path($file_name, $ftype, $hash_base);
2142        my @rev_color = (qw(light2 dark2));
2143        my $num_colors = scalar(@rev_color);
2144        my $current_color = 0;
2145        my $last_rev;
2146        print <<HTML;
2147<div class="page_body">
2148<table class="blame">
2149<tr><th>Commit</th><th>Line</th><th>Data</th></tr>
2150HTML
2151        while (<$fd>) {
2152                /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
2153                my $full_rev = $1;
2154                my $rev = substr($full_rev, 0, 8);
2155                my $lineno = $2;
2156                my $data = $3;
2157
2158                if (!defined $last_rev) {
2159                        $last_rev = $full_rev;
2160                } elsif ($last_rev ne $full_rev) {
2161                        $last_rev = $full_rev;
2162                        $current_color = ++$current_color % $num_colors;
2163                }
2164                print "<tr class=\"$rev_color[$current_color]\">\n";
2165                print "<td class=\"sha1\">" .
2166                        $cgi->a({-href => href(action=>"commit", hash=>$full_rev, file_name=>$file_name)},
2167                                esc_html($rev)) . "</td>\n";
2168                print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" .
2169                      esc_html($lineno) . "</a></td>\n";
2170                print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
2171                print "</tr>\n";
2172        }
2173        print "</table>\n";
2174        print "</div>";
2175        close $fd
2176                or print "Reading blob failed\n";
2177        git_footer_html();
2178}
2179
2180sub git_blame {
2181        my $fd;
2182
2183        if (!gitweb_check_feature('blame')) {
2184                die_error('403 Permission denied', "Permission denied");
2185        }
2186        die_error('404 Not Found', "File name not defined") if (!$file_name);
2187        $hash_base ||= git_get_head_hash($project);
2188        die_error(undef, "Couldn't find base commit") unless ($hash_base);
2189        my %co = parse_commit($hash_base)
2190                or die_error(undef, "Reading commit failed");
2191        if (!defined $hash) {
2192                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
2193                        or die_error(undef, "Error lookup file");
2194        }
2195        open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
2196                or die_error(undef, "Open git-annotate failed");
2197        git_header_html();
2198        my $formats_nav =
2199                $cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
2200                        "blob") .
2201                " | " .
2202                $cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
2203                        "head");
2204        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2205        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2206        git_print_page_path($file_name, 'blob', $hash_base);
2207        print "<div class=\"page_body\">\n";
2208        print <<HTML;
2209<table class="blame">
2210  <tr>
2211    <th>Commit</th>
2212    <th>Age</th>
2213    <th>Author</th>
2214    <th>Line</th>
2215    <th>Data</th>
2216  </tr>
2217HTML
2218        my @line_class = (qw(light dark));
2219        my $line_class_len = scalar (@line_class);
2220        my $line_class_num = $#line_class;
2221        while (my $line = <$fd>) {
2222                my $long_rev;
2223                my $short_rev;
2224                my $author;
2225                my $time;
2226                my $lineno;
2227                my $data;
2228                my $age;
2229                my $age_str;
2230                my $age_class;
2231
2232                chomp $line;
2233                $line_class_num = ($line_class_num + 1) % $line_class_len;
2234
2235                if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
2236                        $long_rev = $1;
2237                        $author   = $2;
2238                        $time     = $3;
2239                        $lineno   = $4;
2240                        $data     = $5;
2241                } else {
2242                        print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
2243                        next;
2244                }
2245                $short_rev  = substr ($long_rev, 0, 8);
2246                $age        = time () - $time;
2247                $age_str    = age_string ($age);
2248                $age_str    =~ s/ /&nbsp;/g;
2249                $age_class  = age_class($age);
2250                $author     = esc_html ($author);
2251                $author     =~ s/ /&nbsp;/g;
2252
2253                $data = untabify($data);
2254                $data = esc_html ($data);
2255
2256                print <<HTML;
2257  <tr class="$line_class[$line_class_num]">
2258    <td class="sha1"><a href="${\href (action=>"commit", hash=>$long_rev)}" class="text">$short_rev..</a></td>
2259    <td class="$age_class">$age_str</td>
2260    <td>$author</td>
2261    <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
2262    <td class="pre">$data</td>
2263  </tr>
2264HTML
2265        } # while (my $line = <$fd>)
2266        print "</table>\n\n";
2267        close $fd
2268                or print "Reading blob failed.\n";
2269        print "</div>";
2270        git_footer_html();
2271}
2272
2273sub git_tags {
2274        my $head = git_get_head_hash($project);
2275        git_header_html();
2276        git_print_page_nav('','', $head,undef,$head);
2277        git_print_header_div('summary', $project);
2278
2279        my $taglist = git_get_refs_list("refs/tags");
2280        if (defined @$taglist) {
2281                git_tags_body($taglist);
2282        }
2283        git_footer_html();
2284}
2285
2286sub git_heads {
2287        my $head = git_get_head_hash($project);
2288        git_header_html();
2289        git_print_page_nav('','', $head,undef,$head);
2290        git_print_header_div('summary', $project);
2291
2292        my $taglist = git_get_refs_list("refs/heads");
2293        if (defined @$taglist) {
2294                git_heads_body($taglist, $head);
2295        }
2296        git_footer_html();
2297}
2298
2299sub git_blob_plain {
2300        if (!defined $hash) {
2301                if (defined $file_name) {
2302                        my $base = $hash_base || git_get_head_hash($project);
2303                        $hash = git_get_hash_by_path($base, $file_name, "blob")
2304                                or die_error(undef, "Error lookup file");
2305                } else {
2306                        die_error(undef, "No file name defined");
2307                }
2308        }
2309        my $type = shift;
2310        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2311                or die_error(undef, "Couldn't cat $file_name, $hash");
2312
2313        $type ||= blob_mimetype($fd, $file_name);
2314
2315        # save as filename, even when no $file_name is given
2316        my $save_as = "$hash";
2317        if (defined $file_name) {
2318                $save_as = $file_name;
2319        } elsif ($type =~ m/^text\//) {
2320                $save_as .= '.txt';
2321        }
2322
2323        print $cgi->header(-type => "$type",
2324                           -content_disposition => "inline; filename=\"$save_as\"");
2325        undef $/;
2326        binmode STDOUT, ':raw';
2327        print <$fd>;
2328        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2329        $/ = "\n";
2330        close $fd;
2331}
2332
2333sub git_blob {
2334        if (!defined $hash) {
2335                if (defined $file_name) {
2336                        my $base = $hash_base || git_get_head_hash($project);
2337                        $hash = git_get_hash_by_path($base, $file_name, "blob")
2338                                or die_error(undef, "Error lookup file");
2339                } else {
2340                        die_error(undef, "No file name defined");
2341                }
2342        }
2343        my $have_blame = gitweb_check_feature('blame');
2344        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2345                or die_error(undef, "Couldn't cat $file_name, $hash");
2346        my $mimetype = blob_mimetype($fd, $file_name);
2347        if ($mimetype !~ m/^text\//) {
2348                close $fd;
2349                return git_blob_plain($mimetype);
2350        }
2351        git_header_html();
2352        my $formats_nav = '';
2353        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2354                if (defined $file_name) {
2355                        if ($have_blame) {
2356                                $formats_nav .=
2357                                        $cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
2358                                                               hash=>$hash, file_name=>$file_name)},
2359                                                "blame") .
2360                                        " | ";
2361                        }
2362                        $formats_nav .=
2363                                $cgi->a({-href => href(action=>"blob_plain",
2364                                                       hash=>$hash, file_name=>$file_name)},
2365                                        "plain") .
2366                                " | " .
2367                                $cgi->a({-href => href(action=>"blob",
2368                                                       hash_base=>"HEAD", file_name=>$file_name)},
2369                                        "head");
2370                } else {
2371                        $formats_nav .=
2372                                $cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "plain");
2373                }
2374                git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2375                git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2376        } else {
2377                print "<div class=\"page_nav\">\n" .
2378                      "<br/><br/></div>\n" .
2379                      "<div class=\"title\">$hash</div>\n";
2380        }
2381        git_print_page_path($file_name, "blob", $hash_base);
2382        print "<div class=\"page_body\">\n";
2383        my $nr;
2384        while (my $line = <$fd>) {
2385                chomp $line;
2386                $nr++;
2387                $line = untabify($line);
2388                printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n",
2389                       $nr, $nr, $nr, esc_html($line);
2390        }
2391        close $fd
2392                or print "Reading blob failed.\n";
2393        print "</div>";
2394        git_footer_html();
2395}
2396
2397sub git_tree {
2398        if (!defined $hash) {
2399                $hash = git_get_head_hash($project);
2400                if (defined $file_name) {
2401                        my $base = $hash_base || $hash;
2402                        $hash = git_get_hash_by_path($base, $file_name, "tree");
2403                }
2404                if (!defined $hash_base) {
2405                        $hash_base = $hash;
2406                }
2407        }
2408        $/ = "\0";
2409        open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
2410                or die_error(undef, "Open git-ls-tree failed");
2411        my @entries = map { chomp; $_ } <$fd>;
2412        close $fd or die_error(undef, "Reading tree failed");
2413        $/ = "\n";
2414
2415        my $refs = git_get_references();
2416        my $ref = format_ref_marker($refs, $hash_base);
2417        git_header_html();
2418        my %base_key = ();
2419        my $base = "";
2420        my $have_blame = gitweb_check_feature('blame');
2421        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2422                $base_key{hash_base} = $hash_base;
2423                git_print_page_nav('tree','', $hash_base);
2424                git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
2425        } else {
2426                print "<div class=\"page_nav\">\n";
2427                print "<br/><br/></div>\n";
2428                print "<div class=\"title\">$hash</div>\n";
2429        }
2430        if (defined $file_name) {
2431                $base = esc_html("$file_name/");
2432        }
2433        git_print_page_path($file_name, 'tree', $hash_base);
2434        print "<div class=\"page_body\">\n";
2435        print "<table cellspacing=\"0\">\n";
2436        my $alternate = 0;
2437        foreach my $line (@entries) {
2438                #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
2439                $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
2440                my $t_mode = $1;
2441                my $t_type = $2;
2442                my $t_hash = $3;
2443                my $t_name = validate_input($4);
2444                if ($alternate) {
2445                        print "<tr class=\"dark\">\n";
2446                } else {
2447                        print "<tr class=\"light\">\n";
2448                }
2449                $alternate ^= 1;
2450                print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
2451                if ($t_type eq "blob") {
2452                        print "<td class=\"list\">" .
2453                              $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key),
2454                                      -class => "list"}, esc_html($t_name)) .
2455                              "</td>\n" .
2456                              "<td class=\"link\">" .
2457                              $cgi->a({-href => href(action=>"blob", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2458                                      "blob");
2459                        if ($have_blame) {
2460                                print " | " .
2461                                        $cgi->a({-href => href(action=>"blame", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2462                                                "blame");
2463                        }
2464                        print " | " .
2465                              $cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
2466                                                     hash=>$t_hash, file_name=>"$base$t_name")},
2467                                      "history") .
2468                              " | " .
2469                              $cgi->a({-href => href(action=>"blob_plain",
2470                                                     hash=>$t_hash, file_name=>"$base$t_name")},
2471                                      "raw") .
2472                              "</td>\n";
2473                } elsif ($t_type eq "tree") {
2474                        print "<td class=\"list\">" .
2475                              $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2476                                      esc_html($t_name)) .
2477                              "</td>\n" .
2478                              "<td class=\"link\">" .
2479                              $cgi->a({-href => href(action=>"tree", hash=>$t_hash, file_name=>"$base$t_name", %base_key)},
2480                                      "tree") .
2481                              " | " .
2482                              $cgi->a({-href => href(action=>"history", hash_base=>$hash_base, file_name=>"$base$t_name")},
2483                                      "history") .
2484                              "</td>\n";
2485                }
2486                print "</tr>\n";
2487        }
2488        print "</table>\n" .
2489              "</div>";
2490        git_footer_html();
2491}
2492
2493sub git_snapshot {
2494
2495        my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2496        my $have_snapshot = (defined $ctype && defined $suffix);
2497        if (!$have_snapshot) {
2498                die_error('403 Permission denied', "Permission denied");
2499        }
2500
2501        if (!defined $hash) {
2502                $hash = git_get_head_hash($project);
2503        }
2504
2505        my $filename = basename($project) . "-$hash.tar.$suffix";
2506
2507        print $cgi->header(-type => 'application/x-tar',
2508                           -content_encoding => $ctype,
2509                           -content_disposition => "inline; filename=\"$filename\"",
2510                           -status => '200 OK');
2511
2512        open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2513                die_error(undef, "Execute git-tar-tree failed.");
2514        binmode STDOUT, ':raw';
2515        print <$fd>;
2516        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2517        close $fd;
2518
2519}
2520
2521sub git_log {
2522        my $head = git_get_head_hash($project);
2523        if (!defined $hash) {
2524                $hash = $head;
2525        }
2526        if (!defined $page) {
2527                $page = 0;
2528        }
2529        my $refs = git_get_references();
2530
2531        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2532        open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2533                or die_error(undef, "Open git-rev-list failed");
2534        my @revlist = map { chomp; $_ } <$fd>;
2535        close $fd;
2536
2537        my $paging_nav = format_paging_nav('log', $hash, $head, $page, $#revlist);
2538
2539        git_header_html();
2540        git_print_page_nav('log','', $hash,undef,undef, $paging_nav);
2541
2542        if (!@revlist) {
2543                my %co = parse_commit($hash);
2544
2545                git_print_header_div('summary', $project);
2546                print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
2547        }
2548        for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2549                my $commit = $revlist[$i];
2550                my $ref = format_ref_marker($refs, $commit);
2551                my %co = parse_commit($commit);
2552                next if !%co;
2553                my %ad = parse_date($co{'author_epoch'});
2554                git_print_header_div('commit',
2555                               "<span class=\"age\">$co{'age_string'}</span>" .
2556                               esc_html($co{'title'}) . $ref,
2557                               $commit);
2558                print "<div class=\"title_text\">\n" .
2559                      "<div class=\"log_link\">\n" .
2560                      $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") .
2561                      " | " .
2562                      $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") .
2563                      "<br/>\n" .
2564                      "</div>\n" .
2565                      "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
2566                      "</div>\n";
2567
2568                print "<div class=\"log_body\">\n";
2569                git_print_simplified_log($co{'comment'});
2570                print "</div>\n";
2571        }
2572        git_footer_html();
2573}
2574
2575sub git_commit {
2576        my %co = parse_commit($hash);
2577        if (!%co) {
2578                die_error(undef, "Unknown commit object");
2579        }
2580        my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2581        my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
2582
2583        my $parent = $co{'parent'};
2584        if (!defined $parent) {
2585                $parent = "--root";
2586        }
2587        open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
2588                or die_error(undef, "Open git-diff-tree failed");
2589        my @difftree = map { chomp; $_ } <$fd>;
2590        close $fd or die_error(undef, "Reading git-diff-tree failed");
2591
2592        # non-textual hash id's can be cached
2593        my $expires;
2594        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2595                $expires = "+1d";
2596        }
2597        my $refs = git_get_references();
2598        my $ref = format_ref_marker($refs, $co{'id'});
2599
2600        my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2601        my $have_snapshot = (defined $ctype && defined $suffix);
2602
2603        my $formats_nav = '';
2604        if (defined $file_name && defined $co{'parent'}) {
2605                my $parent = $co{'parent'};
2606                $formats_nav .=
2607                        $cgi->a({-href => href(action=>"blame", hash_parent=>$parent, file_name=>$file_name)},
2608                                "blame");
2609        }
2610        git_header_html(undef, $expires);
2611        git_print_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
2612                           $hash, $co{'tree'}, $hash,
2613                           $formats_nav);
2614
2615        if (defined $co{'parent'}) {
2616                git_print_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);
2617        } else {
2618                git_print_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);
2619        }
2620        print "<div class=\"title_text\">\n" .
2621              "<table cellspacing=\"0\">\n";
2622        print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2623              "<tr>" .
2624              "<td></td><td> $ad{'rfc2822'}";
2625        if ($ad{'hour_local'} < 6) {
2626                printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
2627                       $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2628        } else {
2629                printf(" (%02d:%02d %s)",
2630                       $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2631        }
2632        print "</td>" .
2633              "</tr>\n";
2634        print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2635        print "<tr><td></td><td> $cd{'rfc2822'}" .
2636              sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
2637              "</td></tr>\n";
2638        print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2639        print "<tr>" .
2640              "<td>tree</td>" .
2641              "<td class=\"sha1\">" .
2642              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),
2643                       class => "list"}, $co{'tree'}) .
2644              "</td>" .
2645              "<td class=\"link\">" .
2646              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
2647                      "tree");
2648        if ($have_snapshot) {
2649                print " | " .
2650                      $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
2651        }
2652        print "</td>" .
2653              "</tr>\n";
2654        my $parents = $co{'parents'};
2655        foreach my $par (@$parents) {
2656                print "<tr>" .
2657                      "<td>parent</td>" .
2658                      "<td class=\"sha1\">" .
2659                      $cgi->a({-href => href(action=>"commit", hash=>$par),
2660                               class => "list"}, $par) .
2661                      "</td>" .
2662                      "<td class=\"link\">" .
2663                      $cgi->a({-href => href(action=>"commit", hash=>$par)}, "commit") .
2664                      " | " .
2665                      $cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)}, "commitdiff") .
2666                      "</td>" .
2667                      "</tr>\n";
2668        }
2669        print "</table>".
2670              "</div>\n";
2671
2672        print "<div class=\"page_body\">\n";
2673        git_print_log($co{'comment'});
2674        print "</div>\n";
2675
2676        git_difftree_body(\@difftree, $hash, $parent);
2677
2678        git_footer_html();
2679}
2680
2681sub git_blobdiff {
2682        mkdir($git_temp, 0700);
2683        git_header_html();
2684        if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2685                my $formats_nav =
2686                        $cgi->a({-href => href(action=>"blobdiff_plain",
2687                                               hash=>$hash, hash_parent=>$hash_parent)},
2688                                "plain");
2689                git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2690                git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2691        } else {
2692                print <<HTML;
2693<div class="page_nav"><br/><br/></div>
2694<div class="title">$hash vs $hash_parent</div>
2695HTML
2696        }
2697        git_print_page_path($file_name, "blob", $hash_base);
2698        print "<div class=\"page_body\">\n" .
2699              "<div class=\"diff_info\">blob:" .
2700              $cgi->a({-href => href(action=>"blob", hash=>$hash_parent,
2701                                     hash_base=>$hash_base, file_name=>($file_parent || $file_name))},
2702                      $hash_parent) .
2703              " -> blob:" .
2704              $cgi->a({-href => href(action=>"blob", hash=>$hash,
2705                                     hash_base=>$hash_base, file_name=>$file_name)},
2706                      $hash) .
2707              "</div>\n";
2708        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2709        print "</div>"; # page_body
2710        git_footer_html();
2711}
2712
2713sub git_blobdiff_plain {
2714        mkdir($git_temp, 0700);
2715        print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2716        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2717}
2718
2719sub git_commitdiff {
2720        my $format = shift || 'html';
2721        my %co = parse_commit($hash);
2722        if (!%co) {
2723                die_error(undef, "Unknown commit object");
2724        }
2725        if (!defined $hash_parent) {
2726                $hash_parent = $co{'parent'} || '--root';
2727        }
2728
2729        # read commitdiff
2730        my $fd;
2731        my @difftree;
2732        if ($format eq 'html') {
2733                open $fd, "-|", $GIT, "diff-tree", '-r', '-M', '-C',
2734                        "--patch-with-raw", "--full-index", $hash_parent, $hash
2735                        or die_error(undef, "Open git-diff-tree failed");
2736
2737                while (chomp(my $line = <$fd>)) {
2738                        # empty line ends raw part of diff-tree output
2739                        last unless $line;
2740                        push @difftree, $line;
2741                }
2742
2743        } elsif ($format eq 'plain') {
2744                open $fd, "-|", $GIT, "diff-tree", '-r', '-p', '-B', $hash_parent, $hash
2745                        or die_error(undef, "Open git-diff-tree failed");
2746
2747        } else {
2748                die_error(undef, "Unknown commitdiff format");
2749        }
2750
2751        # non-textual hash id's can be cached
2752        my $expires;
2753        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2754                $expires = "+1d";
2755        }
2756
2757        # write commit message
2758        if ($format eq 'html') {
2759                my $refs = git_get_references();
2760                my $ref = format_ref_marker($refs, $co{'id'});
2761                my $formats_nav =
2762                        $cgi->a({-href => href(action=>"commitdiff_plain",
2763                                               hash=>$hash, hash_parent=>$hash_parent)},
2764                                "plain");
2765
2766                git_header_html(undef, $expires);
2767                git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2768                git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
2769                print "<div class=\"page_body\">\n";
2770                print "<div class=\"log\">\n";
2771                git_print_simplified_log($co{'comment'}, 1); # skip title
2772                print "</div>\n"; # class="log"
2773
2774        } elsif ($format eq 'plain') {
2775                my $refs = git_get_references("tags");
2776                my @tagnames;
2777                if (exists $refs->{$hash}) {
2778                        @tagnames = map { s|^tags/|| } $refs->{$hash};
2779                }
2780                my $filename = basename($project) . "-$hash.patch";
2781
2782                print $cgi->header(
2783                        -type => 'text/plain',
2784                        -charset => 'utf-8',
2785                        -expires => $expires,
2786                        -content_disposition => qq(inline; filename="$filename"));
2787                my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
2788                print <<TEXT;
2789From: $co{'author'}
2790Date: $ad{'rfc2822'} ($ad{'tz_local'})
2791Subject: $co{'title'}
2792TEXT
2793                foreach my $tag (@tagnames) {
2794                        print "X-Git-Tag: $tag\n";
2795                }
2796                print "X-Git-Url: " . $cgi->self_url() . "\n\n";
2797                foreach my $line (@{$co{'comment'}}) {
2798                        print "$line\n";
2799                }
2800                print "---\n\n";
2801        }
2802
2803        # write patch
2804        if ($format eq 'html') {
2805                #git_difftree_body(\@difftree, $hash, $hash_parent);
2806                #print "<br/>\n";
2807
2808                git_patchset_body($fd, \@difftree, $hash, $hash_parent);
2809                close $fd;
2810                print "</div>\n"; # class="page_body"
2811                git_footer_html();
2812
2813        } elsif ($format eq 'plain') {
2814                local $/ = undef;
2815                print <$fd>;
2816                close $fd
2817                        or print "Reading git-diff-tree failed\n";
2818        }
2819}
2820
2821sub git_commitdiff_plain {
2822        git_commitdiff('plain');
2823}
2824
2825sub git_history {
2826        if (!defined $hash_base) {
2827                $hash_base = git_get_head_hash($project);
2828        }
2829        my $ftype;
2830        my %co = parse_commit($hash_base);
2831        if (!%co) {
2832                die_error(undef, "Unknown commit object");
2833        }
2834        my $refs = git_get_references();
2835        git_header_html();
2836        git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2837        git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
2838        if (!defined $hash && defined $file_name) {
2839                $hash = git_get_hash_by_path($hash_base, $file_name);
2840        }
2841        if (defined $hash) {
2842                $ftype = git_get_type($hash);
2843        }
2844        git_print_page_path($file_name, $ftype, $hash_base);
2845
2846        open my $fd, "-|",
2847                $GIT, "rev-list", "--full-history", $hash_base, "--", $file_name;
2848        git_history_body($fd, $refs, $hash_base, $ftype);
2849
2850        close $fd;
2851        git_footer_html();
2852}
2853
2854sub git_search {
2855        if (!defined $searchtext) {
2856                die_error(undef, "Text field empty");
2857        }
2858        if (!defined $hash) {
2859                $hash = git_get_head_hash($project);
2860        }
2861        my %co = parse_commit($hash);
2862        if (!%co) {
2863                die_error(undef, "Unknown commit object");
2864        }
2865        # pickaxe may take all resources of your box and run for several minutes
2866        # with every query - so decide by yourself how public you make this feature :)
2867        my $commit_search = 1;
2868        my $author_search = 0;
2869        my $committer_search = 0;
2870        my $pickaxe_search = 0;
2871        if ($searchtext =~ s/^author\\://i) {
2872                $author_search = 1;
2873        } elsif ($searchtext =~ s/^committer\\://i) {
2874                $committer_search = 1;
2875        } elsif ($searchtext =~ s/^pickaxe\\://i) {
2876                $commit_search = 0;
2877                $pickaxe_search = 1;
2878        }
2879        git_header_html();
2880        git_print_page_nav('','', $hash,$co{'tree'},$hash);
2881        git_print_header_div('commit', esc_html($co{'title'}), $hash);
2882
2883        print "<table cellspacing=\"0\">\n";
2884        my $alternate = 0;
2885        if ($commit_search) {
2886                $/ = "\0";
2887                open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2888                while (my $commit_text = <$fd>) {
2889                        if (!grep m/$searchtext/i, $commit_text) {
2890                                next;
2891                        }
2892                        if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2893                                next;
2894                        }
2895                        if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2896                                next;
2897                        }
2898                        my @commit_lines = split "\n", $commit_text;
2899                        my %co = parse_commit(undef, \@commit_lines);
2900                        if (!%co) {
2901                                next;
2902                        }
2903                        if ($alternate) {
2904                                print "<tr class=\"dark\">\n";
2905                        } else {
2906                                print "<tr class=\"light\">\n";
2907                        }
2908                        $alternate ^= 1;
2909                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2910                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2911                              "<td>" .
2912                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}), -class => "list subject"},
2913                                       esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2914                        my $comment = $co{'comment'};
2915                        foreach my $line (@$comment) {
2916                                if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2917                                        my $lead = esc_html($1) || "";
2918                                        $lead = chop_str($lead, 30, 10);
2919                                        my $match = esc_html($2) || "";
2920                                        my $trail = esc_html($3) || "";
2921                                        $trail = chop_str($trail, 30, 10);
2922                                        my $text = "$lead<span class=\"match\">$match</span>$trail";
2923                                        print chop_str($text, 80, 5) . "<br/>\n";
2924                                }
2925                        }
2926                        print "</td>\n" .
2927                              "<td class=\"link\">" .
2928                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2929                              " | " .
2930                              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2931                        print "</td>\n" .
2932                              "</tr>\n";
2933                }
2934                close $fd;
2935        }
2936
2937        if ($pickaxe_search) {
2938                $/ = "\n";
2939                open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2940                undef %co;
2941                my @files;
2942                while (my $line = <$fd>) {
2943                        if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2944                                my %set;
2945                                $set{'file'} = $6;
2946                                $set{'from_id'} = $3;
2947                                $set{'to_id'} = $4;
2948                                $set{'id'} = $set{'to_id'};
2949                                if ($set{'id'} =~ m/0{40}/) {
2950                                        $set{'id'} = $set{'from_id'};
2951                                }
2952                                if ($set{'id'} =~ m/0{40}/) {
2953                                        next;
2954                                }
2955                                push @files, \%set;
2956                        } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2957                                if (%co) {
2958                                        if ($alternate) {
2959                                                print "<tr class=\"dark\">\n";
2960                                        } else {
2961                                                print "<tr class=\"light\">\n";
2962                                        }
2963                                        $alternate ^= 1;
2964                                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2965                                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2966                                              "<td>" .
2967                                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
2968                                                      -class => "list subject"},
2969                                                      esc_html(chop_str($co{'title'}, 50)) . "<br/>");
2970                                        while (my $setref = shift @files) {
2971                                                my %set = %$setref;
2972                                                print $cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},
2973                                                                             hash=>$set{'id'}, file_name=>$set{'file'}),
2974                                                              -class => "list"},
2975                                                              "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2976                                                      "<br/>\n";
2977                                        }
2978                                        print "</td>\n" .
2979                                              "<td class=\"link\">" .
2980                                              $cgi->a({-href => href(action=>"commit", hash=>$co{'id'})}, "commit") .
2981                                              " | " .
2982                                              $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})}, "tree");
2983                                        print "</td>\n" .
2984                                              "</tr>\n";
2985                                }
2986                                %co = parse_commit($1);
2987                        }
2988                }
2989                close $fd;
2990        }
2991        print "</table>\n";
2992        git_footer_html();
2993}
2994
2995sub git_shortlog {
2996        my $head = git_get_head_hash($project);
2997        if (!defined $hash) {
2998                $hash = $head;
2999        }
3000        if (!defined $page) {
3001                $page = 0;
3002        }
3003        my $refs = git_get_references();
3004
3005        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
3006        open my $fd, "-|", $GIT, "rev-list", $limit, $hash
3007                or die_error(undef, "Open git-rev-list failed");
3008        my @revlist = map { chomp; $_ } <$fd>;
3009        close $fd;
3010
3011        my $paging_nav = format_paging_nav('shortlog', $hash, $head, $page, $#revlist);
3012        my $next_link = '';
3013        if ($#revlist >= (100 * ($page+1)-1)) {
3014                $next_link =
3015                        $cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
3016                                 -title => "Alt-n"}, "next");
3017        }
3018
3019
3020        git_header_html();
3021        git_print_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
3022        git_print_header_div('summary', $project);
3023
3024        git_shortlog_body(\@revlist, ($page * 100), $#revlist, $refs, $next_link);
3025
3026        git_footer_html();
3027}
3028
3029## ......................................................................
3030## feeds (RSS, OPML)
3031
3032sub git_rss {
3033        # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
3034        open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_get_head_hash($project)
3035                or die_error(undef, "Open git-rev-list failed");
3036        my @revlist = map { chomp; $_ } <$fd>;
3037        close $fd or die_error(undef, "Reading git-rev-list failed");
3038        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3039        print <<XML;
3040<?xml version="1.0" encoding="utf-8"?>
3041<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
3042<channel>
3043<title>$project $my_uri $my_url</title>
3044<link>${\esc_html("$my_url?p=$project;a=summary")}</link>
3045<description>$project log</description>
3046<language>en</language>
3047XML
3048
3049        for (my $i = 0; $i <= $#revlist; $i++) {
3050                my $commit = $revlist[$i];
3051                my %co = parse_commit($commit);
3052                # we read 150, we always show 30 and the ones more recent than 48 hours
3053                if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
3054                        last;
3055                }
3056                my %cd = parse_date($co{'committer_epoch'});
3057                open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
3058                my @difftree = map { chomp; $_ } <$fd>;
3059                close $fd or next;
3060                print "<item>\n" .
3061                      "<title>" .
3062                      sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
3063                      "</title>\n" .
3064                      "<author>" . esc_html($co{'author'}) . "</author>\n" .
3065                      "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
3066                      "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
3067                      "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
3068                      "<description>" . esc_html($co{'title'}) . "</description>\n" .
3069                      "<content:encoded>" .
3070                      "<![CDATA[\n";
3071                my $comment = $co{'comment'};
3072                foreach my $line (@$comment) {
3073                        $line = decode("utf8", $line, Encode::FB_DEFAULT);
3074                        print "$line<br/>\n";
3075                }
3076                print "<br/>\n";
3077                foreach my $line (@difftree) {
3078                        if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
3079                                next;
3080                        }
3081                        my $file = validate_input(unquote($7));
3082                        $file = decode("utf8", $file, Encode::FB_DEFAULT);
3083                        print "$file<br/>\n";
3084                }
3085                print "]]>\n" .
3086                      "</content:encoded>\n" .
3087                      "</item>\n";
3088        }
3089        print "</channel></rss>";
3090}
3091
3092sub git_opml {
3093        my @list = git_get_projects_list();
3094
3095        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
3096        print <<XML;
3097<?xml version="1.0" encoding="utf-8"?>
3098<opml version="1.0">
3099<head>
3100  <title>$site_name Git OPML Export</title>
3101</head>
3102<body>
3103<outline text="git RSS feeds">
3104XML
3105
3106        foreach my $pr (@list) {
3107                my %proj = %$pr;
3108                my $head = git_get_head_hash($proj{'path'});
3109                if (!defined $head) {
3110                        next;
3111                }
3112                $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
3113                my %co = parse_commit($head);
3114                if (!%co) {
3115                        next;
3116                }
3117
3118                my $path = esc_html(chop_str($proj{'path'}, 25, 5));
3119                my $rss  = "$my_url?p=$proj{'path'};a=rss";
3120                my $html = "$my_url?p=$proj{'path'};a=summary";
3121                print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
3122        }
3123        print <<XML;
3124</outline>
3125</body>
3126</opml>
3127XML
3128}