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