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