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