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