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