gitweb / gitweb.cgion commit gitweb: Show project descriptions with utf-8 characters in project list correctly (7d91010)
   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';
  17binmode STDOUT, ':utf8';
  18
  19our $cgi = new CGI;
  20our $version = "267";
  21our $my_url = $cgi->url();
  22our $my_uri = $cgi->url(-absolute => 1);
  23our $rss_link = "";
  24
  25# core git executable to use
  26# this can just be "git" if your webserver has a sensible PATH
  27our $GIT = "/usr/bin/git";
  28
  29# absolute fs-path which will be prepended to the project path
  30#our $projectroot = "/pub/scm";
  31our $projectroot = "/home/kay/public_html/pub/scm";
  32
  33# version of the core git binary
  34our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown";
  35
  36# location for temporary files needed for diffs
  37our $git_temp = "/tmp/gitweb";
  38if (! -d $git_temp) {
  39        mkdir($git_temp, 0700) || die_error("Couldn't mkdir $git_temp");
  40}
  41
  42# target of the home link on top of all pages
  43our $home_link = $my_uri;
  44
  45# name of your site or organization to appear in page titles
  46# replace this with something more descriptive for clearer bookmarks
  47our $site_name = $ENV{'SERVER_NAME'} || "Untitled";
  48
  49# html text to include at home page
  50our $home_text = "indextext.html";
  51
  52# URI of default stylesheet
  53our $stylesheet = "gitweb.css";
  54
  55# source of projects list
  56#our $projects_list = $projectroot;
  57our $projects_list = "index/index.aux";
  58
  59# default blob_plain mimetype and default charset for text/plain blob
  60our $default_blob_plain_mimetype = 'text/plain';
  61our $default_text_plain_charset  = undef;
  62
  63# file to use for guessing MIME types before trying /etc/mime.types
  64# (relative to the current git repository)
  65our $mimetypes_file = undef;
  66
  67# input validation and dispatch
  68our $action = $cgi->param('a');
  69if (defined $action) {
  70        if ($action =~ m/[^0-9a-zA-Z\.\-_]/) {
  71                undef $action;
  72                die_error(undef, "Invalid action parameter.");
  73        }
  74        if ($action eq "git-logo.png") {
  75                git_logo();
  76                exit;
  77        } elsif ($action eq "opml") {
  78                git_opml();
  79                exit;
  80        }
  81}
  82
  83our $order = $cgi->param('o');
  84if (defined $order) {
  85        if ($order =~ m/[^0-9a-zA-Z_]/) {
  86                undef $order;
  87                die_error(undef, "Invalid order parameter.");
  88        }
  89}
  90
  91our $project = ($cgi->param('p') || $ENV{'PATH_INFO'});
  92if (defined $project) {
  93        $project =~ s|^/||; $project =~ s|/$||;
  94        $project = validate_input($project);
  95        if (!defined($project)) {
  96                die_error(undef, "Invalid project parameter.");
  97        }
  98        if (!(-d "$projectroot/$project")) {
  99                undef $project;
 100                die_error(undef, "No such directory.");
 101        }
 102        if (!(-e "$projectroot/$project/HEAD")) {
 103                undef $project;
 104                die_error(undef, "No such project.");
 105        }
 106        $rss_link = "<link rel=\"alternate\" title=\"" . esc_param($project) . " log\" href=\"" .
 107                    "$my_uri?" . esc_param("p=$project;a=rss") . "\" type=\"application/rss+xml\"/>";
 108        $ENV{'GIT_DIR'} = "$projectroot/$project";
 109} else {
 110        git_project_list();
 111        exit;
 112}
 113
 114our $file_name = $cgi->param('f');
 115if (defined $file_name) {
 116        $file_name = validate_input($file_name);
 117        if (!defined($file_name)) {
 118                die_error(undef, "Invalid file parameter.");
 119        }
 120}
 121
 122our $hash = $cgi->param('h');
 123if (defined $hash) {
 124        $hash = validate_input($hash);
 125        if (!defined($hash)) {
 126                die_error(undef, "Invalid hash parameter.");
 127        }
 128}
 129
 130our $hash_parent = $cgi->param('hp');
 131if (defined $hash_parent) {
 132        $hash_parent = validate_input($hash_parent);
 133        if (!defined($hash_parent)) {
 134                die_error(undef, "Invalid hash parent parameter.");
 135        }
 136}
 137
 138our $hash_base = $cgi->param('hb');
 139if (defined $hash_base) {
 140        $hash_base = validate_input($hash_base);
 141        if (!defined($hash_base)) {
 142                die_error(undef, "Invalid hash base parameter.");
 143        }
 144}
 145
 146our $page = $cgi->param('pg');
 147if (defined $page) {
 148        if ($page =~ m/[^0-9]$/) {
 149                undef $page;
 150                die_error(undef, "Invalid page parameter.");
 151        }
 152}
 153
 154our $searchtext = $cgi->param('s');
 155if (defined $searchtext) {
 156        if ($searchtext =~ m/[^a-zA-Z0-9_\.\/\-\+\:\@ ]/) {
 157                undef $searchtext;
 158                die_error(undef, "Invalid search parameter.");
 159        }
 160        $searchtext = quotemeta $searchtext;
 161}
 162
 163sub validate_input {
 164        my $input = shift;
 165
 166        if ($input =~ m/^[0-9a-fA-F]{40}$/) {
 167                return $input;
 168        }
 169        if ($input =~ m/(^|\/)(|\.|\.\.)($|\/)/) {
 170                return undef;
 171        }
 172        if ($input =~ m/[^a-zA-Z0-9_\x80-\xff\ \t\.\/\-\+\#\~\%]/) {
 173                return undef;
 174        }
 175        return $input;
 176}
 177
 178if (!defined $action || $action eq "summary") {
 179        git_summary();
 180        exit;
 181} elsif ($action eq "heads") {
 182        git_heads();
 183        exit;
 184} elsif ($action eq "tags") {
 185        git_tags();
 186        exit;
 187} elsif ($action eq "blob") {
 188        git_blob();
 189        exit;
 190} elsif ($action eq "blob_plain") {
 191        git_blob_plain();
 192        exit;
 193} elsif ($action eq "tree") {
 194        git_tree();
 195        exit;
 196} elsif ($action eq "rss") {
 197        git_rss();
 198        exit;
 199} elsif ($action eq "commit") {
 200        git_commit();
 201        exit;
 202} elsif ($action eq "log") {
 203        git_log();
 204        exit;
 205} elsif ($action eq "blobdiff") {
 206        git_blobdiff();
 207        exit;
 208} elsif ($action eq "blobdiff_plain") {
 209        git_blobdiff_plain();
 210        exit;
 211} elsif ($action eq "commitdiff") {
 212        git_commitdiff();
 213        exit;
 214} elsif ($action eq "commitdiff_plain") {
 215        git_commitdiff_plain();
 216        exit;
 217} elsif ($action eq "history") {
 218        git_history();
 219        exit;
 220} elsif ($action eq "search") {
 221        git_search();
 222        exit;
 223} elsif ($action eq "shortlog") {
 224        git_shortlog();
 225        exit;
 226} elsif ($action eq "tag") {
 227        git_tag();
 228        exit;
 229} elsif ($action eq "blame") {
 230        git_blame2();
 231        exit;
 232} else {
 233        undef $action;
 234        die_error(undef, "Unknown action.");
 235        exit;
 236}
 237
 238# quote unsafe chars, but keep the slash, even when it's not
 239# correct, but quoted slashes look too horrible in bookmarks
 240sub esc_param {
 241        my $str = shift;
 242        $str =~ s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X", ord($1))/eg;
 243        $str =~ s/\+/%2B/g;
 244        $str =~ s/ /\+/g;
 245        return $str;
 246}
 247
 248# replace invalid utf8 character with SUBSTITUTION sequence
 249sub esc_html {
 250        my $str = shift;
 251        $str = decode("utf8", $str, Encode::FB_DEFAULT);
 252        $str = escapeHTML($str);
 253        $str =~ s/\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)
 254        return $str;
 255}
 256
 257# git may return quoted and escaped filenames
 258sub unquote {
 259        my $str = shift;
 260        if ($str =~ m/^"(.*)"$/) {
 261                $str = $1;
 262                $str =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
 263        }
 264        return $str;
 265}
 266
 267# CSS class for given age value (in seconds)
 268sub age_class {
 269        my $age = shift;
 270
 271        if ($age < 60*60*2) {
 272                return "age0";
 273        } elsif ($age < 60*60*24*2) {
 274                return "age1";
 275        } else {
 276                return "age2";
 277        }
 278}
 279
 280sub git_header_html {
 281        my $status = shift || "200 OK";
 282        my $expires = shift;
 283
 284        my $title = "$site_name git";
 285        if (defined $project) {
 286                $title .= " - $project";
 287                if (defined $action) {
 288                        $title .= "/$action";
 289                        if (defined $file_name) {
 290                                $title .= " - $file_name";
 291                                if ($action eq "tree" && $file_name !~ m|/$|) {
 292                                        $title .= "/";
 293                                }
 294                        }
 295                }
 296        }
 297        my $content_type;
 298        # require explicit support from the UA if we are to send the page as
 299        # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
 300        # we have to do this because MSIE sometimes globs '*/*', pretending to
 301        # support xhtml+xml but choking when it gets what it asked for.
 302        if ($cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) {
 303                $content_type = 'application/xhtml+xml';
 304        } else {
 305                $content_type = 'text/html';
 306        }
 307        print $cgi->header(-type=>$content_type, -charset => 'utf-8', -status=> $status, -expires => $expires);
 308        print <<EOF;
 309<?xml version="1.0" encoding="utf-8"?>
 310<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 311<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
 312<!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->
 313<!-- git core binaries version $git_version -->
 314<head>
 315<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>
 316<meta name="robots" content="index, nofollow"/>
 317<title>$title</title>
 318<link rel="stylesheet" type="text/css" href="$stylesheet"/>
 319$rss_link
 320</head>
 321<body>
 322EOF
 323        print "<div class=\"page_header\">\n" .
 324              "<a href=\"http://www.kernel.org/pub/software/scm/git/docs/\" title=\"git documentation\">" .
 325              "<img src=\"$my_uri?" . esc_param("a=git-logo.png") . "\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/>" .
 326              "</a>\n";
 327        print $cgi->a({-href => esc_param($home_link)}, "projects") . " / ";
 328        if (defined $project) {
 329                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, esc_html($project));
 330                if (defined $action) {
 331                        print " / $action";
 332                }
 333                print "\n";
 334                if (!defined $searchtext) {
 335                        $searchtext = "";
 336                }
 337                my $search_hash;
 338                if (defined $hash_base) {
 339                        $search_hash = $hash_base;
 340                } elsif (defined $hash) {
 341                        $search_hash = $hash;
 342                } else {
 343                        $search_hash = "HEAD";
 344                }
 345                $cgi->param("a", "search");
 346                $cgi->param("h", $search_hash);
 347                print $cgi->startform(-method => "get", -action => $my_uri) .
 348                      "<div class=\"search\">\n" .
 349                      $cgi->hidden(-name => "p") . "\n" .
 350                      $cgi->hidden(-name => "a") . "\n" .
 351                      $cgi->hidden(-name => "h") . "\n" .
 352                      $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
 353                      "</div>" .
 354                      $cgi->end_form() . "\n";
 355        }
 356        print "</div>\n";
 357}
 358
 359sub git_footer_html {
 360        print "<div class=\"page_footer\">\n";
 361        if (defined $project) {
 362                my $descr = git_read_description($project);
 363                if (defined $descr) {
 364                        print "<div class=\"page_footer_text\">" . esc_html($descr) . "</div>\n";
 365                }
 366                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=rss"), -class => "rss_logo"}, "RSS") . "\n";
 367        } else {
 368                print $cgi->a({-href => "$my_uri?" . esc_param("a=opml"), -class => "rss_logo"}, "OPML") . "\n";
 369        }
 370        print "</div>\n" .
 371              "</body>\n" .
 372              "</html>";
 373}
 374
 375sub die_error {
 376        my $status = shift || "403 Forbidden";
 377        my $error = shift || "Malformed query, file missing or permission denied";
 378
 379        git_header_html($status);
 380        print "<div class=\"page_body\">\n" .
 381              "<br/><br/>\n" .
 382              "$status - $error\n" .
 383              "<br/>\n" .
 384              "</div>\n";
 385        git_footer_html();
 386        exit;
 387}
 388
 389sub git_page_nav {
 390        my ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;
 391        $extra = '' if !defined $extra; # pager or formats
 392
 393        my @navs = qw(summary shortlog log commit commitdiff tree);
 394        if ($suppress) {
 395                @navs = grep { $_ ne $suppress } @navs;
 396        }
 397
 398        my %arg = map { $_, ''} @navs;
 399        if (defined $head) {
 400                for (qw(commit commitdiff)) {
 401                        $arg{$_} = ";h=$head";
 402                }
 403                if ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {
 404                        for (qw(shortlog log)) {
 405                                $arg{$_} = ";h=$head";
 406                        }
 407                }
 408        }
 409        $arg{tree} .= ";h=$treehead" if defined $treehead;
 410        $arg{tree} .= ";hb=$treebase" if defined $treebase;
 411
 412        print "<div class=\"page_nav\">\n" .
 413                (join " | ",
 414                 map { $_ eq $current
 415                                         ? $_
 416                                         : $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$_$arg{$_}")}, "$_")
 417                                 }
 418                 @navs);
 419        print "<br/>$extra<br/>\n" .
 420              "</div>\n";
 421}
 422
 423sub git_get_type {
 424        my $hash = shift;
 425
 426        open my $fd, "-|", $GIT, "cat-file", '-t', $hash or return;
 427        my $type = <$fd>;
 428        close $fd or return;
 429        chomp $type;
 430        return $type;
 431}
 432
 433sub git_read_head {
 434        my $project = shift;
 435        my $oENV = $ENV{'GIT_DIR'};
 436        my $retval = undef;
 437        $ENV{'GIT_DIR'} = "$projectroot/$project";
 438        if (open my $fd, "-|", $GIT, "rev-parse", "--verify", "HEAD") {
 439                my $head = <$fd>;
 440                close $fd;
 441                if (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {
 442                        $retval = $1;
 443                }
 444        }
 445        if (defined $oENV) {
 446                $ENV{'GIT_DIR'} = $oENV;
 447        }
 448        return $retval;
 449}
 450
 451sub git_read_hash {
 452        my $path = shift;
 453
 454        open my $fd, "$projectroot/$path" or return undef;
 455        my $head = <$fd>;
 456        close $fd;
 457        chomp $head;
 458        if ($head =~ m/^[0-9a-fA-F]{40}$/) {
 459                return $head;
 460        }
 461}
 462
 463sub git_read_description {
 464        my $path = shift;
 465
 466        open my $fd, "$projectroot/$path/description" or return undef;
 467        my $descr = <$fd>;
 468        close $fd;
 469        chomp $descr;
 470        return $descr;
 471}
 472
 473sub git_read_tag {
 474        my $tag_id = shift;
 475        my %tag;
 476        my @comment;
 477
 478        open my $fd, "-|", $GIT, "cat-file", "tag", $tag_id or return;
 479        $tag{'id'} = $tag_id;
 480        while (my $line = <$fd>) {
 481                chomp $line;
 482                if ($line =~ m/^object ([0-9a-fA-F]{40})$/) {
 483                        $tag{'object'} = $1;
 484                } elsif ($line =~ m/^type (.+)$/) {
 485                        $tag{'type'} = $1;
 486                } elsif ($line =~ m/^tag (.+)$/) {
 487                        $tag{'name'} = $1;
 488                } elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {
 489                        $tag{'author'} = $1;
 490                        $tag{'epoch'} = $2;
 491                        $tag{'tz'} = $3;
 492                } elsif ($line =~ m/--BEGIN/) {
 493                        push @comment, $line;
 494                        last;
 495                } elsif ($line eq "") {
 496                        last;
 497                }
 498        }
 499        push @comment, <$fd>;
 500        $tag{'comment'} = \@comment;
 501        close $fd or return;
 502        if (!defined $tag{'name'}) {
 503                return
 504        };
 505        return %tag
 506}
 507
 508sub age_string {
 509        my $age = shift;
 510        my $age_str;
 511
 512        if ($age > 60*60*24*365*2) {
 513                $age_str = (int $age/60/60/24/365);
 514                $age_str .= " years ago";
 515        } elsif ($age > 60*60*24*(365/12)*2) {
 516                $age_str = int $age/60/60/24/(365/12);
 517                $age_str .= " months ago";
 518        } elsif ($age > 60*60*24*7*2) {
 519                $age_str = int $age/60/60/24/7;
 520                $age_str .= " weeks ago";
 521        } elsif ($age > 60*60*24*2) {
 522                $age_str = int $age/60/60/24;
 523                $age_str .= " days ago";
 524        } elsif ($age > 60*60*2) {
 525                $age_str = int $age/60/60;
 526                $age_str .= " hours ago";
 527        } elsif ($age > 60*2) {
 528                $age_str = int $age/60;
 529                $age_str .= " min ago";
 530        } elsif ($age > 2) {
 531                $age_str = int $age;
 532                $age_str .= " sec ago";
 533        } else {
 534                $age_str .= " right now";
 535        }
 536        return $age_str;
 537}
 538
 539sub git_read_commit {
 540        my $commit_id = shift;
 541        my $commit_text = shift;
 542
 543        my @commit_lines;
 544        my %co;
 545
 546        if (defined $commit_text) {
 547                @commit_lines = @$commit_text;
 548        } else {
 549                $/ = "\0";
 550                open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", "--max-count=1", $commit_id or return;
 551                @commit_lines = split '\n', <$fd>;
 552                close $fd or return;
 553                $/ = "\n";
 554                pop @commit_lines;
 555        }
 556        my $header = shift @commit_lines;
 557        if (!($header =~ m/^[0-9a-fA-F]{40}/)) {
 558                return;
 559        }
 560        ($co{'id'}, my @parents) = split ' ', $header;
 561        $co{'parents'} = \@parents;
 562        $co{'parent'} = $parents[0];
 563        while (my $line = shift @commit_lines) {
 564                last if $line eq "\n";
 565                if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
 566                        $co{'tree'} = $1;
 567                } elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {
 568                        $co{'author'} = $1;
 569                        $co{'author_epoch'} = $2;
 570                        $co{'author_tz'} = $3;
 571                        if ($co{'author'} =~ m/^([^<]+) </) {
 572                                $co{'author_name'} = $1;
 573                        } else {
 574                                $co{'author_name'} = $co{'author'};
 575                        }
 576                } elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {
 577                        $co{'committer'} = $1;
 578                        $co{'committer_epoch'} = $2;
 579                        $co{'committer_tz'} = $3;
 580                        $co{'committer_name'} = $co{'committer'};
 581                        $co{'committer_name'} =~ s/ <.*//;
 582                }
 583        }
 584        if (!defined $co{'tree'}) {
 585                return;
 586        };
 587
 588        foreach my $title (@commit_lines) {
 589                $title =~ s/^    //;
 590                if ($title ne "") {
 591                        $co{'title'} = chop_str($title, 80, 5);
 592                        # remove leading stuff of merges to make the interesting part visible
 593                        if (length($title) > 50) {
 594                                $title =~ s/^Automatic //;
 595                                $title =~ s/^merge (of|with) /Merge ... /i;
 596                                if (length($title) > 50) {
 597                                        $title =~ s/(http|rsync):\/\///;
 598                                }
 599                                if (length($title) > 50) {
 600                                        $title =~ s/(master|www|rsync)\.//;
 601                                }
 602                                if (length($title) > 50) {
 603                                        $title =~ s/kernel.org:?//;
 604                                }
 605                                if (length($title) > 50) {
 606                                        $title =~ s/\/pub\/scm//;
 607                                }
 608                        }
 609                        $co{'title_short'} = chop_str($title, 50, 5);
 610                        last;
 611                }
 612        }
 613        # remove added spaces
 614        foreach my $line (@commit_lines) {
 615                $line =~ s/^    //;
 616        }
 617        $co{'comment'} = \@commit_lines;
 618
 619        my $age = time - $co{'committer_epoch'};
 620        $co{'age'} = $age;
 621        $co{'age_string'} = age_string($age);
 622        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});
 623        if ($age > 60*60*24*7*2) {
 624                $co{'age_string_date'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
 625                $co{'age_string_age'} = $co{'age_string'};
 626        } else {
 627                $co{'age_string_date'} = $co{'age_string'};
 628                $co{'age_string_age'} = sprintf "%4i-%02u-%02i", 1900 + $year, $mon+1, $mday;
 629        }
 630        return %co;
 631}
 632
 633sub git_diff_print {
 634        my $from = shift;
 635        my $from_name = shift;
 636        my $to = shift;
 637        my $to_name = shift;
 638        my $format = shift || "html";
 639
 640        my $from_tmp = "/dev/null";
 641        my $to_tmp = "/dev/null";
 642        my $pid = $$;
 643
 644        # create tmp from-file
 645        if (defined $from) {
 646                $from_tmp = "$git_temp/gitweb_" . $$ . "_from";
 647                open my $fd2, "> $from_tmp";
 648                open my $fd, "-|", $GIT, "cat-file", "blob", $from;
 649                my @file = <$fd>;
 650                print $fd2 @file;
 651                close $fd2;
 652                close $fd;
 653        }
 654
 655        # create tmp to-file
 656        if (defined $to) {
 657                $to_tmp = "$git_temp/gitweb_" . $$ . "_to";
 658                open my $fd2, "> $to_tmp";
 659                open my $fd, "-|", $GIT, "cat-file", "blob", $to;
 660                my @file = <$fd>;
 661                print $fd2 @file;
 662                close $fd2;
 663                close $fd;
 664        }
 665
 666        open my $fd, "-|", "/usr/bin/diff -u -p -L \'$from_name\' -L \'$to_name\' $from_tmp $to_tmp";
 667        if ($format eq "plain") {
 668                undef $/;
 669                print <$fd>;
 670                $/ = "\n";
 671        } else {
 672                while (my $line = <$fd>) {
 673                        chomp $line;
 674                        my $char = substr($line, 0, 1);
 675                        my $diff_class = "";
 676                        if ($char eq '+') {
 677                                $diff_class = " add";
 678                        } elsif ($char eq "-") {
 679                                $diff_class = " rem";
 680                        } elsif ($char eq "@") {
 681                                $diff_class = " chunk_header";
 682                        } elsif ($char eq "\\") {
 683                                # skip errors
 684                                next;
 685                        }
 686                        while ((my $pos = index($line, "\t")) != -1) {
 687                                if (my $count = (8 - (($pos-1) % 8))) {
 688                                        my $spaces = ' ' x $count;
 689                                        $line =~ s/\t/$spaces/;
 690                                }
 691                        }
 692                        print "<div class=\"diff$diff_class\">" . esc_html($line) . "</div>\n";
 693                }
 694        }
 695        close $fd;
 696
 697        if (defined $from) {
 698                unlink($from_tmp);
 699        }
 700        if (defined $to) {
 701                unlink($to_tmp);
 702        }
 703}
 704
 705sub mode_str {
 706        my $mode = oct shift;
 707
 708        if (S_ISDIR($mode & S_IFMT)) {
 709                return 'drwxr-xr-x';
 710        } elsif (S_ISLNK($mode)) {
 711                return 'lrwxrwxrwx';
 712        } elsif (S_ISREG($mode)) {
 713                # git cares only about the executable bit
 714                if ($mode & S_IXUSR) {
 715                        return '-rwxr-xr-x';
 716                } else {
 717                        return '-rw-r--r--';
 718                };
 719        } else {
 720                return '----------';
 721        }
 722}
 723
 724sub chop_str {
 725        my $str = shift;
 726        my $len = shift;
 727        my $add_len = shift || 10;
 728
 729        # allow only $len chars, but don't cut a word if it would fit in $add_len
 730        # if it doesn't fit, cut it if it's still longer than the dots we would add
 731        $str =~ m/^(.{0,$len}[^ \/\-_:\.@]{0,$add_len})(.*)/;
 732        my $body = $1;
 733        my $tail = $2;
 734        if (length($tail) > 4) {
 735                $tail = " ...";
 736        }
 737        return "$body$tail";
 738}
 739
 740sub file_type {
 741        my $mode = oct shift;
 742
 743        if (S_ISDIR($mode & S_IFMT)) {
 744                return "directory";
 745        } elsif (S_ISLNK($mode)) {
 746                return "symlink";
 747        } elsif (S_ISREG($mode)) {
 748                return "file";
 749        } else {
 750                return "unknown";
 751        }
 752}
 753
 754sub format_log_line_html {
 755        my $line = shift;
 756
 757        $line = esc_html($line);
 758        $line =~ s/ /&nbsp;/g;
 759        if ($line =~ m/([0-9a-fA-F]{40})/) {
 760                my $hash_text = $1;
 761                if (git_get_type($hash_text) eq "commit") {
 762                        my $link = $cgi->a({-class => "text", -href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_text")}, $hash_text);
 763                        $line =~ s/$hash_text/$link/;
 764                }
 765        }
 766        return $line;
 767}
 768
 769sub date_str {
 770        my $epoch = shift;
 771        my $tz = shift || "-0000";
 772
 773        my %date;
 774        my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
 775        my @days = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 776        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);
 777        $date{'hour'} = $hour;
 778        $date{'minute'} = $min;
 779        $date{'mday'} = $mday;
 780        $date{'day'} = $days[$wday];
 781        $date{'month'} = $months[$mon];
 782        $date{'rfc2822'} = sprintf "%s, %d %s %4d %02d:%02d:%02d +0000", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;
 783        $date{'mday-time'} = sprintf "%d %s %02d:%02d", $mday, $months[$mon], $hour ,$min;
 784
 785        $tz =~ m/^([+\-][0-9][0-9])([0-9][0-9])$/;
 786        my $local = $epoch + ((int $1 + ($2/60)) * 3600);
 787        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);
 788        $date{'hour_local'} = $hour;
 789        $date{'minute_local'} = $min;
 790        $date{'tz_local'} = $tz;
 791        return %date;
 792}
 793
 794# git-logo (cached in browser for one day)
 795sub git_logo {
 796        binmode STDOUT, ':raw';
 797        print $cgi->header(-type => 'image/png', -expires => '+1d');
 798        # cat git-logo.png | hexdump -e '16/1 " %02x"  "\n"' | sed 's/ /\\x/g'
 799        print   "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52" .
 800                "\x00\x00\x00\x48\x00\x00\x00\x1b\x04\x03\x00\x00\x00\x2d\xd9\xd4" .
 801                "\x2d\x00\x00\x00\x18\x50\x4c\x54\x45\xff\xff\xff\x60\x60\x5d\xb0" .
 802                "\xaf\xaa\x00\x80\x00\xce\xcd\xc7\xc0\x00\x00\xe8\xe8\xe6\xf7\xf7" .
 803                "\xf6\x95\x0c\xa7\x47\x00\x00\x00\x73\x49\x44\x41\x54\x28\xcf\x63" .
 804                "\x48\x67\x20\x04\x4a\x5c\x18\x0a\x08\x2a\x62\x53\x61\x20\x02\x08" .
 805                "\x0d\x69\x45\xac\xa1\xa1\x01\x30\x0c\x93\x60\x36\x26\x52\x91\xb1" .
 806                "\x01\x11\xd6\xe1\x55\x64\x6c\x6c\xcc\x6c\x6c\x0c\xa2\x0c\x70\x2a" .
 807                "\x62\x06\x2a\xc1\x62\x1d\xb3\x01\x02\x53\xa4\x08\xe8\x00\x03\x18" .
 808                "\x26\x56\x11\xd4\xe1\x20\x97\x1b\xe0\xb4\x0e\x35\x24\x71\x29\x82" .
 809                "\x99\x30\xb8\x93\x0a\x11\xb9\x45\x88\xc1\x8d\xa0\xa2\x44\x21\x06" .
 810                "\x27\x41\x82\x40\x85\xc1\x45\x89\x20\x70\x01\x00\xa4\x3d\x21\xc5" .
 811                "\x12\x1c\x9a\xfe\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82";
 812}
 813
 814sub get_file_owner {
 815        my $path = shift;
 816
 817        my ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);
 818        my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);
 819        if (!defined $gcos) {
 820                return undef;
 821        }
 822        my $owner = $gcos;
 823        $owner =~ s/[,;].*$//;
 824        return decode("utf8", $owner, Encode::FB_DEFAULT);
 825}
 826
 827sub git_read_projects {
 828        my @list;
 829
 830        if (-d $projects_list) {
 831                # search in directory
 832                my $dir = $projects_list;
 833                opendir my ($dh), $dir or return undef;
 834                while (my $dir = readdir($dh)) {
 835                        if (-e "$projectroot/$dir/HEAD") {
 836                                my $pr = {
 837                                        path => $dir,
 838                                };
 839                                push @list, $pr
 840                        }
 841                }
 842                closedir($dh);
 843        } elsif (-f $projects_list) {
 844                # read from file(url-encoded):
 845                # 'git%2Fgit.git Linus+Torvalds'
 846                # 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'
 847                # 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'
 848                open my ($fd), $projects_list or return undef;
 849                while (my $line = <$fd>) {
 850                        chomp $line;
 851                        my ($path, $owner) = split ' ', $line;
 852                        $path = unescape($path);
 853                        $owner = unescape($owner);
 854                        if (!defined $path) {
 855                                next;
 856                        }
 857                        if (-e "$projectroot/$path/HEAD") {
 858                                my $pr = {
 859                                        path => $path,
 860                                        owner => decode("utf8", $owner, Encode::FB_DEFAULT),
 861                                };
 862                                push @list, $pr
 863                        }
 864                }
 865                close $fd;
 866        }
 867        @list = sort {$a->{'path'} cmp $b->{'path'}} @list;
 868        return @list;
 869}
 870
 871sub git_get_project_config {
 872        my $key = shift;
 873
 874        return unless ($key);
 875        $key =~ s/^gitweb\.//;
 876        return if ($key =~ m/\W/);
 877
 878        my $val = qx($GIT repo-config --get gitweb.$key);
 879        return ($val);
 880}
 881
 882sub git_get_project_config_bool {
 883        my $val = git_get_project_config (@_);
 884        if ($val and $val =~ m/true|yes|on/) {
 885                return (1);
 886        }
 887        return; # implicit false
 888}
 889
 890sub git_project_list {
 891        my @list = git_read_projects();
 892        my @projects;
 893        if (!@list) {
 894                die_error(undef, "No project found.");
 895        }
 896        foreach my $pr (@list) {
 897                my $head = git_read_head($pr->{'path'});
 898                if (!defined $head) {
 899                        next;
 900                }
 901                $ENV{'GIT_DIR'} = "$projectroot/$pr->{'path'}";
 902                my %co = git_read_commit($head);
 903                if (!%co) {
 904                        next;
 905                }
 906                $pr->{'commit'} = \%co;
 907                if (!defined $pr->{'descr'}) {
 908                        my $descr = git_read_description($pr->{'path'}) || "";
 909                        $pr->{'descr'} = chop_str($descr, 25, 5);
 910                }
 911                if (!defined $pr->{'owner'}) {
 912                        $pr->{'owner'} = get_file_owner("$projectroot/$pr->{'path'}") || "";
 913                }
 914                push @projects, $pr;
 915        }
 916        git_header_html();
 917        if (-f $home_text) {
 918                print "<div class=\"index_include\">\n";
 919                open (my $fd, $home_text);
 920                print <$fd>;
 921                close $fd;
 922                print "</div>\n";
 923        }
 924        print "<table class=\"project_list\">\n" .
 925              "<tr>\n";
 926        if (!defined($order) || (defined($order) && ($order eq "project"))) {
 927                @projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;
 928                print "<th>Project</th>\n";
 929        } else {
 930                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=project")}, "Project") . "</th>\n";
 931        }
 932        if (defined($order) && ($order eq "descr")) {
 933                @projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;
 934                print "<th>Description</th>\n";
 935        } else {
 936                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=descr")}, "Description") . "</th>\n";
 937        }
 938        if (defined($order) && ($order eq "owner")) {
 939                @projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;
 940                print "<th>Owner</th>\n";
 941        } else {
 942                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=owner")}, "Owner") . "</th>\n";
 943        }
 944        if (defined($order) && ($order eq "age")) {
 945                @projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;
 946                print "<th>Last Change</th>\n";
 947        } else {
 948                print "<th>" . $cgi->a({-class => "header", -href => "$my_uri?" . esc_param("o=age")}, "Last Change") . "</th>\n";
 949        }
 950        print "<th></th>\n" .
 951              "</tr>\n";
 952        my $alternate = 0;
 953        foreach my $pr (@projects) {
 954                if ($alternate) {
 955                        print "<tr class=\"dark\">\n";
 956                } else {
 957                        print "<tr class=\"light\">\n";
 958                }
 959                $alternate ^= 1;
 960                print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary"), -class => "list"}, esc_html($pr->{'path'})) . "</td>\n" .
 961                      "<td>" . esc_html($pr->{'descr'}) . "</td>\n" .
 962                      "<td><i>" . chop_str($pr->{'owner'}, 15) . "</i></td>\n";
 963                print "<td class=\"". age_class($pr->{'commit'}{'age'}) . "\">" . $pr->{'commit'}{'age_string'} . "</td>\n" .
 964                      "<td class=\"link\">" .
 965                      $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=summary")}, "summary") .
 966                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=shortlog")}, "shortlog") .
 967                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$pr->{'path'};a=log")}, "log") .
 968                      "</td>\n" .
 969                      "</tr>\n";
 970        }
 971        print "</table>\n";
 972        git_footer_html();
 973}
 974
 975sub read_info_ref {
 976        my $type = shift || "";
 977        my %refs;
 978        # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c      refs/tags/v2.6.11
 979        # c39ae07f393806ccf406ef966e9a15afc43cc36a      refs/tags/v2.6.11^{}
 980        open my $fd, "$projectroot/$project/info/refs" or return;
 981        while (my $line = <$fd>) {
 982                chomp $line;
 983                if ($line =~ m/^([0-9a-fA-F]{40})\t.*$type\/([^\^]+)/) {
 984                        if (defined $refs{$1}) {
 985                                $refs{$1} .= " / $2";
 986                        } else {
 987                                $refs{$1} = $2;
 988                        }
 989                }
 990        }
 991        close $fd or return;
 992        return \%refs;
 993}
 994
 995sub git_read_refs {
 996        my $ref_dir = shift;
 997        my @reflist;
 998
 999        my @refs;
1000        opendir my $dh, "$projectroot/$project/$ref_dir";
1001        while (my $dir = readdir($dh)) {
1002                if ($dir =~ m/^\./) {
1003                        next;
1004                }
1005                if (-d "$projectroot/$project/$ref_dir/$dir") {
1006                        opendir my $dh2, "$projectroot/$project/$ref_dir/$dir";
1007                        my @subdirs = grep !m/^\./, readdir $dh2;
1008                        closedir($dh2);
1009                        foreach my $subdir (@subdirs) {
1010                                push @refs, "$dir/$subdir"
1011                        }
1012                        next;
1013                }
1014                push @refs, $dir;
1015        }
1016        closedir($dh);
1017        foreach my $ref_file (@refs) {
1018                my $ref_id = git_read_hash("$project/$ref_dir/$ref_file");
1019                my $type = git_get_type($ref_id) || next;
1020                my %ref_item;
1021                my %co;
1022                $ref_item{'type'} = $type;
1023                $ref_item{'id'} = $ref_id;
1024                $ref_item{'epoch'} = 0;
1025                $ref_item{'age'} = "unknown";
1026                if ($type eq "tag") {
1027                        my %tag = git_read_tag($ref_id);
1028                        $ref_item{'comment'} = $tag{'comment'};
1029                        if ($tag{'type'} eq "commit") {
1030                                %co = git_read_commit($tag{'object'});
1031                                $ref_item{'epoch'} = $co{'committer_epoch'};
1032                                $ref_item{'age'} = $co{'age_string'};
1033                        } elsif (defined($tag{'epoch'})) {
1034                                my $age = time - $tag{'epoch'};
1035                                $ref_item{'epoch'} = $tag{'epoch'};
1036                                $ref_item{'age'} = age_string($age);
1037                        }
1038                        $ref_item{'reftype'} = $tag{'type'};
1039                        $ref_item{'name'} = $tag{'name'};
1040                        $ref_item{'refid'} = $tag{'object'};
1041                } elsif ($type eq "commit"){
1042                        %co = git_read_commit($ref_id);
1043                        $ref_item{'reftype'} = "commit";
1044                        $ref_item{'name'} = $ref_file;
1045                        $ref_item{'title'} = $co{'title'};
1046                        $ref_item{'refid'} = $ref_id;
1047                        $ref_item{'epoch'} = $co{'committer_epoch'};
1048                        $ref_item{'age'} = $co{'age_string'};
1049                }
1050
1051                push @reflist, \%ref_item;
1052        }
1053        # sort tags by age
1054        @reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;
1055        return \@reflist;
1056}
1057
1058sub git_summary {
1059        my $descr = git_read_description($project) || "none";
1060        my $head = git_read_head($project);
1061        my %co = git_read_commit($head);
1062        my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1063
1064        my $owner;
1065        if (-f $projects_list) {
1066                open (my $fd , $projects_list);
1067                while (my $line = <$fd>) {
1068                        chomp $line;
1069                        my ($pr, $ow) = split ' ', $line;
1070                        $pr = unescape($pr);
1071                        $ow = unescape($ow);
1072                        if ($pr eq $project) {
1073                                $owner = decode("utf8", $ow, Encode::FB_DEFAULT);
1074                                last;
1075                        }
1076                }
1077                close $fd;
1078        }
1079        if (!defined $owner) {
1080                $owner = get_file_owner("$projectroot/$project");
1081        }
1082
1083        my $refs = read_info_ref();
1084        git_header_html();
1085        git_page_nav('summary','', $head);
1086        print "<div class=\"title\">&nbsp;</div>\n";
1087        print "<table cellspacing=\"0\">\n" .
1088              "<tr><td>description</td><td>" . esc_html($descr) . "</td></tr>\n" .
1089              "<tr><td>owner</td><td>$owner</td></tr>\n" .
1090              "<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
1091              "</table>\n";
1092        open my $fd, "-|", $GIT, "rev-list", "--max-count=17", git_read_head($project)
1093                or die_error(undef, "Open git-rev-list failed.");
1094        my @revlist = map { chomp; $_ } <$fd>;
1095        close $fd;
1096        print "<div>\n" .
1097              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog"), -class => "title"}, "shortlog") .
1098              "</div>\n";
1099        my $i = 16;
1100        print "<table cellspacing=\"0\">\n";
1101        my $alternate = 0;
1102        foreach my $commit (@revlist) {
1103                my %co = git_read_commit($commit);
1104                my %ad = date_str($co{'author_epoch'});
1105                if ($alternate) {
1106                        print "<tr class=\"dark\">\n";
1107                } else {
1108                        print "<tr class=\"light\">\n";
1109                }
1110                $alternate ^= 1;
1111                if ($i-- > 0) {
1112                        my $ref = "";
1113                        if (defined $refs->{$commit}) {
1114                                $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
1115                        }
1116                        print "<td><i>$co{'age_string'}</i></td>\n" .
1117                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
1118                              "<td>";
1119                        if (length($co{'title_short'}) < length($co{'title'})) {
1120                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
1121                                      "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
1122                        } else {
1123                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
1124                                      "<b>" . esc_html($co{'title'}) . "$ref</b>");
1125                        }
1126                        print "</td>\n" .
1127                              "<td class=\"link\">" .
1128                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1129                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1130                              "</td>\n" .
1131                              "</tr>";
1132                } else {
1133                        print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "...") . "</td>\n" .
1134                        "</tr>";
1135                        last;
1136                }
1137        }
1138        print "</table\n>";
1139
1140        my $taglist = git_read_refs("refs/tags");
1141        if (defined @$taglist) {
1142                print "<div>\n" .
1143                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags"), -class => "title"}, "tags") .
1144                      "</div>\n";
1145                my $i = 16;
1146                print "<table cellspacing=\"0\">\n";
1147                my $alternate = 0;
1148                foreach my $entry (@$taglist) {
1149                        my %tag = %$entry;
1150                        my $comment_lines = $tag{'comment'};
1151                        my $comment = shift @$comment_lines;
1152                        if (defined($comment)) {
1153                                $comment = chop_str($comment, 30, 5);
1154                        }
1155                        if ($alternate) {
1156                                print "<tr class=\"dark\">\n";
1157                        } else {
1158                                print "<tr class=\"light\">\n";
1159                        }
1160                        $alternate ^= 1;
1161                        if ($i-- > 0) {
1162                                print "<td><i>$tag{'age'}</i></td>\n" .
1163                                      "<td>" .
1164                                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
1165                                      "<b>" . esc_html($tag{'name'}) . "</b>") .
1166                                      "</td>\n" .
1167                                      "<td>";
1168                                if (defined($comment)) {
1169                                        print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, esc_html($comment));
1170                                }
1171                                print "</td>\n" .
1172                                      "<td class=\"link\">";
1173                                if ($tag{'type'} eq "tag") {
1174                                        print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
1175                                }
1176                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1177                                if ($tag{'reftype'} eq "commit") {
1178                                        print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1179                                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1180                                }
1181                                print "</td>\n" .
1182                                      "</tr>";
1183                        } else {
1184                                print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tags")}, "...") . "</td>\n" .
1185                                "</tr>";
1186                                last;
1187                        }
1188                }
1189                print "</table\n>";
1190        }
1191
1192        my $headlist = git_read_refs("refs/heads");
1193        if (defined @$headlist) {
1194                print "<div>\n" .
1195                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads"), -class => "title"}, "heads") .
1196                      "</div>\n";
1197                my $i = 16;
1198                print "<table cellspacing=\"0\">\n";
1199                my $alternate = 0;
1200                foreach my $entry (@$headlist) {
1201                        my %tag = %$entry;
1202                        if ($alternate) {
1203                                print "<tr class=\"dark\">\n";
1204                        } else {
1205                                print "<tr class=\"light\">\n";
1206                        }
1207                        $alternate ^= 1;
1208                        if ($i-- > 0) {
1209                                print "<td><i>$tag{'age'}</i></td>\n" .
1210                                      "<td>" .
1211                                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"},
1212                                      "<b>" . esc_html($tag{'name'}) . "</b>") .
1213                                      "</td>\n" .
1214                                      "<td class=\"link\">" .
1215                                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1216                                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1217                                      "</td>\n" .
1218                                      "</tr>";
1219                        } else {
1220                                print "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=heads")}, "...") . "</td>\n" .
1221                                "</tr>";
1222                                last;
1223                        }
1224                }
1225                print "</table\n>";
1226        }
1227        git_footer_html();
1228}
1229
1230sub git_print_page_path {
1231        my $name = shift;
1232        my $type = shift;
1233
1234        if (!defined $name) {
1235                print "<div class=\"page_path\"><b>/</b></div>\n";
1236        } elsif ($type =~ "blob") {
1237                print "<div class=\"page_path\"><b>" .
1238                        $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;f=$file_name")}, esc_html($name)) . "</b><br/></div>\n";
1239        } else {
1240                print "<div class=\"page_path\"><b>" . esc_html($name) . "</b><br/></div>\n";
1241        }
1242}
1243
1244sub git_tag {
1245        my $head = git_read_head($project);
1246        git_header_html();
1247        git_page_nav('','', $head,undef,$head);
1248        my %tag = git_read_tag($hash);
1249        print "<div>\n" .
1250              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($tag{'name'})) . "\n" .
1251              "</div>\n";
1252        print "<div class=\"title_text\">\n" .
1253              "<table cellspacing=\"0\">\n" .
1254              "<tr>\n" .
1255              "<td>object</td>\n" .
1256              "<td>" . $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'object'}) . "</td>\n" .
1257              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'type'};h=$tag{'object'}")}, $tag{'type'}) . "</td>\n" .
1258              "</tr>\n";
1259        if (defined($tag{'author'})) {
1260                my %ad = date_str($tag{'epoch'}, $tag{'tz'});
1261                print "<tr><td>author</td><td>" . esc_html($tag{'author'}) . "</td></tr>\n";
1262                print "<tr><td></td><td>" . $ad{'rfc2822'} . sprintf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . "</td></tr>\n";
1263        }
1264        print "</table>\n\n" .
1265              "</div>\n";
1266        print "<div class=\"page_body\">";
1267        my $comment = $tag{'comment'};
1268        foreach my $line (@$comment) {
1269                print esc_html($line) . "<br/>\n";
1270        }
1271        print "</div>\n";
1272        git_footer_html();
1273}
1274
1275sub git_blame2 {
1276        my $fd;
1277        my $ftype;
1278        die_error(undef, "Permission denied.") if (!git_get_project_config_bool ('blame'));
1279        die_error('404 Not Found', "File name not defined") if (!$file_name);
1280        $hash_base ||= git_read_head($project);
1281        die_error(undef, "Reading commit failed") unless ($hash_base);
1282        my %co = git_read_commit($hash_base)
1283                or die_error(undef, "Reading commit failed");
1284        if (!defined $hash) {
1285                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1286                        or die_error(undef, "Error looking up file");
1287        }
1288        $ftype = git_get_type($hash);
1289        if ($ftype !~ "blob") {
1290                die_error("400 Bad Request", "object is not a blob");
1291        }
1292        open ($fd, "-|", $GIT, "blame", '-l', $file_name, $hash_base)
1293                or die_error(undef, "Open git-blame failed.");
1294        git_header_html();
1295        my $formats_nav =
1296                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1297                " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1298        git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1299        print "<div>" .
1300              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1301              "</div>\n";
1302        git_print_page_path($file_name, $ftype);
1303        my @rev_color = (qw(light dark));
1304        my $num_colors = scalar(@rev_color);
1305        my $current_color = 0;
1306        my $last_rev;
1307        print "<div class=\"page_body\">\n";
1308        print "<table class=\"blame\">\n";
1309        print "<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n";
1310        while (<$fd>) {
1311                /^([0-9a-fA-F]{40}).*?(\d+)\)\s{1}(\s*.*)/;
1312                my $full_rev = $1;
1313                my $rev = substr($full_rev, 0, 8);
1314                my $lineno = $2;
1315                my $data = $3;
1316
1317                if (!defined $last_rev) {
1318                        $last_rev = $full_rev;
1319                } elsif ($last_rev ne $full_rev) {
1320                        $last_rev = $full_rev;
1321                        $current_color = ++$current_color % $num_colors;
1322                }
1323                print "<tr class=\"$rev_color[$current_color]\">\n";
1324                print "<td class=\"sha1\">" .
1325                        $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$full_rev;f=$file_name")}, esc_html($rev)) . "</td>\n";
1326                print "<td class=\"linenr\"><a id=\"l$lineno\" href=\"#l$lineno\" class=\"linenr\">" . esc_html($lineno) . "</a></td>\n";
1327                print "<td class=\"pre\">" . esc_html($data) . "</td>\n";
1328                print "</tr>\n";
1329        }
1330        print "</table>\n";
1331        print "</div>";
1332        close $fd or print "Reading blob failed\n";
1333        git_footer_html();
1334}
1335
1336sub git_blame {
1337        my $fd;
1338        die_error('403 Permission denied', "Permission denied.") if (!git_get_project_config_bool ('blame'));
1339        die_error('404 Not Found', "What file will it be, master?") if (!$file_name);
1340        $hash_base ||= git_read_head($project);
1341        die_error(undef, "Reading commit failed.") unless ($hash_base);
1342        my %co = git_read_commit($hash_base)
1343                or die_error(undef, "Reading commit failed.");
1344        if (!defined $hash) {
1345                $hash = git_get_hash_by_path($hash_base, $file_name, "blob")
1346                        or die_error(undef, "Error lookup file.");
1347        }
1348        open ($fd, "-|", $GIT, "annotate", '-l', '-t', '-r', $file_name, $hash_base)
1349                or die_error(undef, "Open git-annotate failed.");
1350        git_header_html();
1351        my $formats_nav =
1352                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, "blob") .
1353                " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;f=$file_name")}, "head");
1354        git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1355        print "<div>" .
1356              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1357              "</div>\n";
1358        git_print_page_path($file_name);
1359        print "<div class=\"page_body\">\n";
1360        print <<HTML;
1361<table class="blame">
1362  <tr>
1363    <th>Commit</th>
1364    <th>Age</th>
1365    <th>Author</th>
1366    <th>Line</th>
1367    <th>Data</th>
1368  </tr>
1369HTML
1370        my @line_class = (qw(light dark));
1371        my $line_class_len = scalar (@line_class);
1372        my $line_class_num = $#line_class;
1373        while (my $line = <$fd>) {
1374                my $long_rev;
1375                my $short_rev;
1376                my $author;
1377                my $time;
1378                my $lineno;
1379                my $data;
1380                my $age;
1381                my $age_str;
1382                my $age_class;
1383
1384                chomp $line;
1385                $line_class_num = ($line_class_num + 1) % $line_class_len;
1386
1387                if ($line =~ m/^([0-9a-fA-F]{40})\t\(\s*([^\t]+)\t(\d+) \+\d\d\d\d\t(\d+)\)(.*)$/) {
1388                        $long_rev = $1;
1389                        $author   = $2;
1390                        $time     = $3;
1391                        $lineno   = $4;
1392                        $data     = $5;
1393                } else {
1394                        print qq(  <tr><td colspan="5" class="error">Unable to parse: $line</td></tr>\n);
1395                        next;
1396                }
1397                $short_rev  = substr ($long_rev, 0, 8);
1398                $age        = time () - $time;
1399                $age_str    = age_string ($age);
1400                $age_str    =~ s/ /&nbsp;/g;
1401                $age_class  = age_class($age);
1402                $author     = esc_html ($author);
1403                $author     =~ s/ /&nbsp;/g;
1404                # escape tabs
1405                while ((my $pos = index($data, "\t")) != -1) {
1406                        if (my $count = (8 - ($pos % 8))) {
1407                                my $spaces = ' ' x $count;
1408                                $data =~ s/\t/$spaces/;
1409                        }
1410                }
1411                $data = esc_html ($data);
1412
1413                print <<HTML;
1414  <tr class="$line_class[$line_class_num]">
1415    <td class="sha1"><a href="$my_uri?${\esc_param ("p=$project;a=commit;h=$long_rev")}" class="text">$short_rev..</a></td>
1416    <td class="$age_class">$age_str</td>
1417    <td>$author</td>
1418    <td class="linenr"><a id="$lineno" href="#$lineno" class="linenr">$lineno</a></td>
1419    <td class="pre">$data</td>
1420  </tr>
1421HTML
1422        } # while (my $line = <$fd>)
1423        print "</table>\n\n";
1424        close $fd or print "Reading blob failed.\n";
1425        print "</div>";
1426        git_footer_html();
1427}
1428
1429sub git_tags {
1430        my $head = git_read_head($project);
1431        git_header_html();
1432        git_page_nav('','', $head,undef,$head);
1433        my $taglist = git_read_refs("refs/tags");
1434        print "<div>\n" .
1435              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
1436              "</div>\n";
1437        print "<table cellspacing=\"0\">\n";
1438        my $alternate = 0;
1439        if (defined @$taglist) {
1440                foreach my $entry (@$taglist) {
1441                        my %tag = %$entry;
1442                        my $comment_lines = $tag{'comment'};
1443                        my $comment = shift @$comment_lines;
1444                        if (defined($comment)) {
1445                                $comment = chop_str($comment, 30, 5);
1446                        }
1447                        if ($alternate) {
1448                                print "<tr class=\"dark\">\n";
1449                        } else {
1450                                print "<tr class=\"light\">\n";
1451                        }
1452                        $alternate ^= 1;
1453                        print "<td><i>$tag{'age'}</i></td>\n" .
1454                              "<td>" .
1455                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}"), -class => "list"},
1456                              "<b>" . esc_html($tag{'name'}) . "</b>") .
1457                              "</td>\n" .
1458                              "<td>";
1459                        if (defined($comment)) {
1460                                print $cgi->a({-class => "list", -href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, $comment);
1461                        }
1462                        print "</td>\n" .
1463                              "<td class=\"link\">";
1464                        if ($tag{'type'} eq "tag") {
1465                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tag;h=$tag{'id'}")}, "tag") . " | ";
1466                        }
1467                        print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=$tag{'reftype'};h=$tag{'refid'}")}, $tag{'reftype'});
1468                        if ($tag{'reftype'} eq "commit") {
1469                                print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1470                                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'refid'}")}, "log");
1471                        }
1472                        print "</td>\n" .
1473                              "</tr>";
1474                }
1475        }
1476        print "</table\n>";
1477        git_footer_html();
1478}
1479
1480sub git_heads {
1481        my $head = git_read_head($project);
1482        git_header_html();
1483        git_page_nav('','', $head,undef,$head);
1484        my $taglist = git_read_refs("refs/heads");
1485        print "<div>\n" .
1486              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
1487              "</div>\n";
1488        print "<table cellspacing=\"0\">\n";
1489        my $alternate = 0;
1490        if (defined @$taglist) {
1491                foreach my $entry (@$taglist) {
1492                        my %tag = %$entry;
1493                        if ($alternate) {
1494                                print "<tr class=\"dark\">\n";
1495                        } else {
1496                                print "<tr class=\"light\">\n";
1497                        }
1498                        $alternate ^= 1;
1499                        print "<td><i>$tag{'age'}</i></td>\n" .
1500                              "<td>" .
1501                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}"), -class => "list"}, "<b>" . esc_html($tag{'name'}) . "</b>") .
1502                              "</td>\n" .
1503                              "<td class=\"link\">" .
1504                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$tag{'name'}")}, "shortlog") .
1505                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$tag{'name'}")}, "log") .
1506                              "</td>\n" .
1507                              "</tr>";
1508                }
1509        }
1510        print "</table\n>";
1511        git_footer_html();
1512}
1513
1514sub git_get_hash_by_path {
1515        my $base = shift;
1516        my $path = shift || return undef;
1517
1518        my $tree = $base;
1519
1520        open my $fd, "-|", $GIT, "ls-tree", $base, "--", $path
1521                or die_error(undef, "Open git-ls-tree failed.");
1522        my $line = <$fd>;
1523        close $fd or return undef;
1524
1525        #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa  panic.c'
1526        $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1527        return $3;
1528}
1529
1530sub mimetype_guess_file {
1531        my $filename = shift;
1532        my $mimemap = shift;
1533        -r $mimemap or return undef;
1534
1535        my %mimemap;
1536        open(MIME, $mimemap) or return undef;
1537        while (<MIME>) {
1538                my ($mime, $exts) = split(/\t+/);
1539                my @exts = split(/\s+/, $exts);
1540                foreach my $ext (@exts) {
1541                        $mimemap{$ext} = $mime;
1542                }
1543        }
1544        close(MIME);
1545
1546        $filename =~ /\.(.*?)$/;
1547        return $mimemap{$1};
1548}
1549
1550sub mimetype_guess {
1551        my $filename = shift;
1552        my $mime;
1553        $filename =~ /\./ or return undef;
1554
1555        if ($mimetypes_file) {
1556                my $file = $mimetypes_file;
1557                #$file =~ m#^/# or $file = "$projectroot/$path/$file";
1558                $mime = mimetype_guess_file($filename, $file);
1559        }
1560        $mime ||= mimetype_guess_file($filename, '/etc/mime.types');
1561        return $mime;
1562}
1563
1564sub git_blob_plain_mimetype {
1565        my $fd = shift;
1566        my $filename = shift;
1567
1568        if ($filename) {
1569                my $mime = mimetype_guess($filename);
1570                $mime and return $mime;
1571        }
1572
1573        # just in case
1574        return $default_blob_plain_mimetype unless $fd;
1575
1576        if (-T $fd) {
1577                return 'text/plain' .
1578                       ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');
1579        } elsif (! $filename) {
1580                return 'application/octet-stream';
1581        } elsif ($filename =~ m/\.png$/i) {
1582                return 'image/png';
1583        } elsif ($filename =~ m/\.gif$/i) {
1584                return 'image/gif';
1585        } elsif ($filename =~ m/\.jpe?g$/i) {
1586                return 'image/jpeg';
1587        } else {
1588                return 'application/octet-stream';
1589        }
1590}
1591
1592sub git_blob_plain {
1593        if (!defined $hash) {
1594                if (defined $file_name) {
1595                        my $base = $hash_base || git_read_head($project);
1596                        $hash = git_get_hash_by_path($base, $file_name, "blob")
1597                                or die_error(undef, "Error lookup file.");
1598                } else {
1599                        die_error(undef, "No file name defined.");
1600                }
1601        }
1602        my $type = shift;
1603        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1604                or die_error("Couldn't cat $file_name, $hash");
1605
1606        $type ||= git_blob_plain_mimetype($fd, $file_name);
1607
1608        # save as filename, even when no $file_name is given
1609        my $save_as = "$hash";
1610        if (defined $file_name) {
1611                $save_as = $file_name;
1612        } elsif ($type =~ m/^text\//) {
1613                $save_as .= '.txt';
1614        }
1615
1616        print $cgi->header(-type => "$type", '-content-disposition' => "inline; filename=\"$save_as\"");
1617        undef $/;
1618        binmode STDOUT, ':raw';
1619        print <$fd>;
1620        binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
1621        $/ = "\n";
1622        close $fd;
1623}
1624
1625sub git_blob {
1626        if (!defined $hash) {
1627                if (defined $file_name) {
1628                        my $base = $hash_base || git_read_head($project);
1629                        $hash = git_get_hash_by_path($base, $file_name, "blob")
1630                                or die_error(undef, "Error lookup file.");
1631                } else {
1632                        die_error(undef, "No file name defined.");
1633                }
1634        }
1635        my $have_blame = git_get_project_config_bool ('blame');
1636        open my $fd, "-|", $GIT, "cat-file", "blob", $hash
1637                or die_error(undef, "Couldn't cat $file_name, $hash.");
1638        my $mimetype = git_blob_plain_mimetype($fd, $file_name);
1639        if ($mimetype !~ m/^text\//) {
1640                close $fd;
1641                return git_blob_plain($mimetype);
1642        }
1643        git_header_html();
1644        my $formats_nav = '';
1645        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1646                if (defined $file_name) {
1647                        if ($have_blame) {
1648                                $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name")}, "blame") . " | ";
1649                        }
1650                        $formats_nav .=
1651                                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash;f=$file_name")}, "plain") .
1652                                " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=HEAD;f=$file_name")}, "head");
1653                } else {
1654                        $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$hash")}, "plain");
1655                }
1656                git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
1657                print "<div>" .
1658                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) .
1659                      "</div>\n";
1660        } else {
1661                print "<div class=\"page_nav\">\n" .
1662                      "<br/><br/></div>\n" .
1663                      "<div class=\"title\">$hash</div>\n";
1664        }
1665        git_print_page_path($file_name, "blob");
1666        print "<div class=\"page_body\">\n";
1667        my $nr;
1668        while (my $line = <$fd>) {
1669                chomp $line;
1670                $nr++;
1671                while ((my $pos = index($line, "\t")) != -1) {
1672                        if (my $count = (8 - ($pos % 8))) {
1673                                my $spaces = ' ' x $count;
1674                                $line =~ s/\t/$spaces/;
1675                        }
1676                }
1677                printf "<div class=\"pre\"><a id=\"l%i\" href=\"#l%i\" class=\"linenr\">%4i</a> %s</div>\n", $nr, $nr, $nr, esc_html($line);
1678        }
1679        close $fd or print "Reading blob failed.\n";
1680        print "</div>";
1681        git_footer_html();
1682}
1683
1684sub git_tree {
1685        if (!defined $hash) {
1686                $hash = git_read_head($project);
1687                if (defined $file_name) {
1688                        my $base = $hash_base || $hash;
1689                        $hash = git_get_hash_by_path($base, $file_name, "tree");
1690                }
1691                if (!defined $hash_base) {
1692                        $hash_base = $hash;
1693                }
1694        }
1695        $/ = "\0";
1696        open my $fd, "-|", $GIT, "ls-tree", '-z', $hash
1697                or die_error(undef, "Open git-ls-tree failed.");
1698        my @entries = map { chomp; $_ } <$fd>;
1699        close $fd or die_error(undef, "Reading tree failed.");
1700        $/ = "\n";
1701
1702        my $refs = read_info_ref();
1703        my $ref = "";
1704        if (defined $refs->{$hash_base}) {
1705                $ref = " <span class=\"tag\">" . esc_html($refs->{$hash_base}) . "</span>";
1706        }
1707        git_header_html();
1708        my $base_key = "";
1709        my $base = "";
1710        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
1711                $base_key = ";hb=$hash_base";
1712                git_page_nav('tree','', $hash_base);
1713                print "<div>\n" .
1714                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
1715                      "</div>\n";
1716        } else {
1717                print "<div class=\"page_nav\">\n";
1718                print "<br/><br/></div>\n";
1719                print "<div class=\"title\">$hash</div>\n";
1720        }
1721        if (defined $file_name) {
1722                $base = esc_html("$file_name/");
1723        }
1724        git_print_page_path($file_name);
1725        print "<div class=\"page_body\">\n";
1726        print "<table cellspacing=\"0\">\n";
1727        my $alternate = 0;
1728        foreach my $line (@entries) {
1729                #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
1730                $line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/;
1731                my $t_mode = $1;
1732                my $t_type = $2;
1733                my $t_hash = $3;
1734                my $t_name = validate_input($4);
1735                if ($alternate) {
1736                        print "<tr class=\"dark\">\n";
1737                } else {
1738                        print "<tr class=\"light\">\n";
1739                }
1740                $alternate ^= 1;
1741                print "<td class=\"mode\">" . mode_str($t_mode) . "</td>\n";
1742                if ($t_type eq "blob") {
1743                        print "<td class=\"list\">" .
1744                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name"), -class => "list"}, esc_html($t_name)) .
1745                              "</td>\n" .
1746                              "<td class=\"link\">" .
1747                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name")}, "blob") .
1748#                             " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name")}, "blame") .
1749                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;h=$t_hash;hb=$hash_base;f=$base$t_name")}, "history") .
1750                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob_plain;h=$t_hash;f=$base$t_name")}, "raw") .
1751                              "</td>\n";
1752                } elsif ($t_type eq "tree") {
1753                        print "<td class=\"list\">" .
1754                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, esc_html($t_name)) .
1755                              "</td>\n" .
1756                              "<td class=\"link\">" .
1757                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name")}, "tree") .
1758                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash_base;f=$base$t_name")}, "history") .
1759                              "</td>\n";
1760                }
1761                print "</tr>\n";
1762        }
1763        print "</table>\n" .
1764              "</div>";
1765        git_footer_html();
1766}
1767
1768sub git_rss {
1769        # http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1770        open my $fd, "-|", $GIT, "rev-list", "--max-count=150", git_read_head($project)
1771                or die_error(undef, "Open git-rev-list failed.");
1772        my @revlist = map { chomp; $_ } <$fd>;
1773        close $fd or die_error(undef, "Reading rev-list failed.");
1774        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1775        print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1776              "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">\n";
1777        print "<channel>\n";
1778        print "<title>$project</title>\n".
1779              "<link>" . esc_html("$my_url?p=$project;a=summary") . "</link>\n".
1780              "<description>$project log</description>\n".
1781              "<language>en</language>\n";
1782
1783        for (my $i = 0; $i <= $#revlist; $i++) {
1784                my $commit = $revlist[$i];
1785                my %co = git_read_commit($commit);
1786                # we read 150, we always show 30 and the ones more recent than 48 hours
1787                if (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {
1788                        last;
1789                }
1790                my %cd = date_str($co{'committer_epoch'});
1791                open $fd, "-|", $GIT, "diff-tree", '-r', $co{'parent'}, $co{'id'} or next;
1792                my @difftree = map { chomp; $_ } <$fd>;
1793                close $fd or next;
1794                print "<item>\n" .
1795                      "<title>" .
1796                      sprintf("%d %s %02d:%02d", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . " - " . esc_html($co{'title'}) .
1797                      "</title>\n" .
1798                      "<author>" . esc_html($co{'author'}) . "</author>\n" .
1799                      "<pubDate>$cd{'rfc2822'}</pubDate>\n" .
1800                      "<guid isPermaLink=\"true\">" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</guid>\n" .
1801                      "<link>" . esc_html("$my_url?p=$project;a=commit;h=$commit") . "</link>\n" .
1802                      "<description>" . esc_html($co{'title'}) . "</description>\n" .
1803                      "<content:encoded>" .
1804                      "<![CDATA[\n";
1805                my $comment = $co{'comment'};
1806                foreach my $line (@$comment) {
1807                        $line = decode("utf8", $line, Encode::FB_DEFAULT);
1808                        print "$line<br/>\n";
1809                }
1810                print "<br/>\n";
1811                foreach my $line (@difftree) {
1812                        if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
1813                                next;
1814                        }
1815                        my $file = validate_input(unquote($7));
1816                        $file = decode("utf8", $file, Encode::FB_DEFAULT);
1817                        print "$file<br/>\n";
1818                }
1819                print "]]>\n" .
1820                      "</content:encoded>\n" .
1821                      "</item>\n";
1822        }
1823        print "</channel></rss>";
1824}
1825
1826sub git_opml {
1827        my @list = git_read_projects();
1828
1829        print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
1830        print "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
1831              "<opml version=\"1.0\">\n".
1832              "<head>".
1833              "  <title>$site_name Git OPML Export</title>\n".
1834              "</head>\n".
1835              "<body>\n".
1836              "<outline text=\"git RSS feeds\">\n";
1837
1838        foreach my $pr (@list) {
1839                my %proj = %$pr;
1840                my $head = git_read_head($proj{'path'});
1841                if (!defined $head) {
1842                        next;
1843                }
1844                $ENV{'GIT_DIR'} = "$projectroot/$proj{'path'}";
1845                my %co = git_read_commit($head);
1846                if (!%co) {
1847                        next;
1848                }
1849
1850                my $path = esc_html(chop_str($proj{'path'}, 25, 5));
1851                my $rss  = "$my_url?p=$proj{'path'};a=rss";
1852                my $html = "$my_url?p=$proj{'path'};a=summary";
1853                print "<outline type=\"rss\" text=\"$path\" title=\"$path\" xmlUrl=\"$rss\" htmlUrl=\"$html\"/>\n";
1854        }
1855        print "</outline>\n".
1856              "</body>\n".
1857              "</opml>\n";
1858}
1859
1860sub git_log {
1861        my $head = git_read_head($project);
1862        if (!defined $hash) {
1863                $hash = $head;
1864        }
1865        if (!defined $page) {
1866                $page = 0;
1867        }
1868        my $refs = read_info_ref();
1869
1870        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
1871        open my $fd, "-|", $GIT, "rev-list", $limit, $hash
1872                or die_error(undef, "Open git-rev-list failed.");
1873        my @revlist = map { chomp; $_ } <$fd>;
1874        close $fd;
1875
1876        my $paging_nav = '';
1877        if ($hash ne $head || $page) {
1878                $paging_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log")}, "HEAD");
1879        } else {
1880                $paging_nav .= "HEAD";
1881        }
1882        if ($page > 0) {
1883                $paging_nav .= " &sdot; " .
1884                        $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
1885        } else {
1886                $paging_nav .= " &sdot; prev";
1887        }
1888        if ($#revlist >= (100 * ($page+1)-1)) {
1889                $paging_nav .= " &sdot; " .
1890                        $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=log;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
1891        } else {
1892                $paging_nav .= " &sdot; next";
1893        }
1894
1895        git_header_html();
1896        git_page_nav('log','', $hash,undef,undef, $paging_nav);
1897
1898        if (!@revlist) {
1899                print "<div>\n" .
1900                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
1901                      "</div>\n";
1902                my %co = git_read_commit($hash);
1903                print "<div class=\"page_body\"> Last change $co{'age_string'}.<br/><br/></div>\n";
1904        }
1905        for (my $i = ($page * 100); $i <= $#revlist; $i++) {
1906                my $commit = $revlist[$i];
1907                my $ref = "";
1908                if (defined $refs->{$commit}) {
1909                        $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
1910                }
1911                my %co = git_read_commit($commit);
1912                next if !%co;
1913                my %ad = date_str($co{'author_epoch'});
1914                print "<div>\n" .
1915                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "title"},
1916                      "<span class=\"age\">$co{'age_string'}</span>" . esc_html($co{'title'}) . $ref) . "\n";
1917                print "</div>\n";
1918                print "<div class=\"title_text\">\n" .
1919                      "<div class=\"log_link\">\n" .
1920                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
1921                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
1922                      "<br/>\n" .
1923                      "</div>\n" .
1924                      "<i>" . esc_html($co{'author_name'}) .  " [$ad{'rfc2822'}]</i><br/>\n" .
1925                      "</div>\n" .
1926                      "<div class=\"log_body\">\n";
1927                my $comment = $co{'comment'};
1928                my $empty = 0;
1929                foreach my $line (@$comment) {
1930                        if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
1931                                next;
1932                        }
1933                        if ($line eq "") {
1934                                if ($empty) {
1935                                        next;
1936                                }
1937                                $empty = 1;
1938                        } else {
1939                                $empty = 0;
1940                        }
1941                        print format_log_line_html($line) . "<br/>\n";
1942                }
1943                if (!$empty) {
1944                        print "<br/>\n";
1945                }
1946                print "</div>\n";
1947        }
1948        git_footer_html();
1949}
1950
1951sub git_commit {
1952        my %co = git_read_commit($hash);
1953        if (!%co) {
1954                die_error(undef, "Unknown commit object.");
1955        }
1956        my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
1957        my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
1958
1959        my $parent = $co{'parent'};
1960        if (!defined $parent) {
1961                $parent = "--root";
1962        }
1963        open my $fd, "-|", $GIT, "diff-tree", '-r', '-M', $parent, $hash
1964                or die_error(undef, "Open git-diff-tree failed.");
1965        my @difftree = map { chomp; $_ } <$fd>;
1966        close $fd or die_error(undef, "Reading git-diff-tree failed.");
1967
1968        # non-textual hash id's can be cached
1969        my $expires;
1970        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
1971                $expires = "+1d";
1972        }
1973        my $refs = read_info_ref();
1974        my $ref = "";
1975        if (defined $refs->{$co{'id'}}) {
1976                $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
1977        }
1978        git_header_html(undef, $expires);
1979        my $formats_nav = '';
1980        if (defined $file_name && defined $co{'parent'}) {
1981                my $parent = $co{'parent'};
1982                $formats_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blame;hb=$parent;f=$file_name")}, "blame");
1983        }
1984        git_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',
1985                                                         $hash, $co{'tree'}, $hash,
1986                                                         $formats_nav);
1987
1988        if (defined $co{'parent'}) {
1989                print "<div>\n" .
1990                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
1991                      "</div>\n";
1992        } else {
1993                print "<div>\n" .
1994                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
1995                      "</div>\n";
1996        }
1997        print "<div class=\"title_text\">\n" .
1998              "<table cellspacing=\"0\">\n";
1999        print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
2000              "<tr>" .
2001              "<td></td><td> $ad{'rfc2822'}";
2002        if ($ad{'hour_local'} < 6) {
2003                printf(" (<span class=\"atnight\">%02d:%02d</span> %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2004        } else {
2005                printf(" (%02d:%02d %s)", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
2006        }
2007        print "</td>" .
2008              "</tr>\n";
2009        print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
2010        print "<tr><td></td><td> $cd{'rfc2822'}" . sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . "</td></tr>\n";
2011        print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
2012        print "<tr>" .
2013              "<td>tree</td>" .
2014              "<td class=\"sha1\">" .
2015              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash"), class => "list"}, $co{'tree'}) .
2016              "</td>" .
2017              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$hash")}, "tree") .
2018              "</td>" .
2019              "</tr>\n";
2020        my $parents = $co{'parents'};
2021        foreach my $par (@$parents) {
2022                print "<tr>" .
2023                      "<td>parent</td>" .
2024                      "<td class=\"sha1\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par"), class => "list"}, $par) . "</td>" .
2025                      "<td class=\"link\">" .
2026                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$par")}, "commit") .
2027                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$hash;hp=$par")}, "commitdiff") .
2028                      "</td>" .
2029                      "</tr>\n";
2030        }
2031        print "</table>".
2032              "</div>\n";
2033        print "<div class=\"page_body\">\n";
2034        my $comment = $co{'comment'};
2035        my $empty = 0;
2036        my $signed = 0;
2037        foreach my $line (@$comment) {
2038                # print only one empty line
2039                if ($line eq "") {
2040                        if ($empty || $signed) {
2041                                next;
2042                        }
2043                        $empty = 1;
2044                } else {
2045                        $empty = 0;
2046                }
2047                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2048                        $signed = 1;
2049                        print "<span class=\"signoff\">" . esc_html($line) . "</span><br/>\n";
2050                } else {
2051                        $signed = 0;
2052                        print format_log_line_html($line) . "<br/>\n";
2053                }
2054        }
2055        print "</div>\n";
2056        print "<div class=\"list_head\">\n";
2057        if ($#difftree > 10) {
2058                print(($#difftree + 1) . " files changed:\n");
2059        }
2060        print "</div>\n";
2061        print "<table class=\"diff_tree\">\n";
2062        my $alternate = 0;
2063        foreach my $line (@difftree) {
2064                # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
2065                # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
2066                if (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/)) {
2067                        next;
2068                }
2069                my $from_mode = $1;
2070                my $to_mode = $2;
2071                my $from_id = $3;
2072                my $to_id = $4;
2073                my $status = $5;
2074                my $similarity = $6;
2075                my $file = validate_input(unquote($7));
2076                if ($alternate) {
2077                        print "<tr class=\"dark\">\n";
2078                } else {
2079                        print "<tr class=\"light\">\n";
2080                }
2081                $alternate ^= 1;
2082                if ($status eq "A") {
2083                        my $mode_chng = "";
2084                        if (S_ISREG(oct $to_mode)) {
2085                                $mode_chng = sprintf(" with mode: %04o", (oct $to_mode) & 0777);
2086                        }
2087                        print "<td>" .
2088                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
2089                              "<td><span class=\"file_status new\">[new " . file_type($to_mode) . "$mode_chng]</span></td>\n" .
2090                              "<td class=\"link\">" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob") . "</td>\n";
2091                } elsif ($status eq "D") {
2092                        print "<td>" .
2093                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file)) . "</td>\n" .
2094                              "<td><span class=\"file_status deleted\">[deleted " . file_type($from_mode). "]</span></td>\n" .
2095                              "<td class=\"link\">" .
2096                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, "blob") .
2097                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") .
2098                              "</td>\n"
2099                } elsif ($status eq "M" || $status eq "T") {
2100                        my $mode_chnge = "";
2101                        if ($from_mode != $to_mode) {
2102                                $mode_chnge = " <span class=\"file_status mode_chnge\">[changed";
2103                                if (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {
2104                                        $mode_chnge .= " from " . file_type($from_mode) . " to " . file_type($to_mode);
2105                                }
2106                                if (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {
2107                                        if (S_ISREG($from_mode) && S_ISREG($to_mode)) {
2108                                                $mode_chnge .= sprintf(" mode: %04o->%04o", (oct $from_mode) & 0777, (oct $to_mode) & 0777);
2109                                        } elsif (S_ISREG($to_mode)) {
2110                                                $mode_chnge .= sprintf(" mode: %04o", (oct $to_mode) & 0777);
2111                                        }
2112                                }
2113                                $mode_chnge .= "]</span>\n";
2114                        }
2115                        print "<td>";
2116                        if ($to_id ne $from_id) {
2117                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
2118                        } else {
2119                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file"), -class => "list"}, esc_html($file));
2120                        }
2121                        print "</td>\n" .
2122                              "<td>$mode_chnge</td>\n" .
2123                              "<td class=\"link\">";
2124                        print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, "blob");
2125                        if ($to_id ne $from_id) {
2126                                print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file")}, "diff");
2127                        }
2128                        print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=history;hb=$hash;f=$file")}, "history") . "\n";
2129                        print "</td>\n";
2130                } elsif ($status eq "R") {
2131                        my ($from_file, $to_file) = split "\t", $file;
2132                        my $mode_chng = "";
2133                        if ($from_mode != $to_mode) {
2134                                $mode_chng = sprintf(", mode: %04o", (oct $to_mode) & 0777);
2135                        }
2136                        print "<td>" .
2137                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file"), -class => "list"}, esc_html($to_file)) . "</td>\n" .
2138                              "<td><span class=\"file_status moved\">[moved from " .
2139                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$from_file"), -class => "list"}, esc_html($from_file)) .
2140                              " with " . (int $similarity) . "% similarity$mode_chng]</span></td>\n" .
2141                              "<td class=\"link\">" .
2142                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file")}, "blob");
2143                        if ($to_id ne $from_id) {
2144                                print " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file")}, "diff");
2145                        }
2146                        print "</td>\n";
2147                }
2148                print "</tr>\n";
2149        }
2150        print "</table>\n";
2151        git_footer_html();
2152}
2153
2154sub git_blobdiff {
2155        mkdir($git_temp, 0700);
2156        git_header_html();
2157        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
2158                my $formats_nav =
2159                        $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2160                git_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
2161                print "<div>\n" .
2162                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2163                      "</div>\n";
2164        } else {
2165                print "<div class=\"page_nav\">\n" .
2166                      "<br/><br/></div>\n" .
2167                      "<div class=\"title\">$hash vs $hash_parent</div>\n";
2168        }
2169        git_print_page_path($file_name, "blob");
2170        print "<div class=\"page_body\">\n" .
2171              "<div class=\"diff_info\">blob:" .
2172              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name")}, $hash_parent) .
2173              " -> blob:" .
2174              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name")}, $hash) .
2175              "</div>\n";
2176        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);
2177        print "</div>";
2178        git_footer_html();
2179}
2180
2181sub git_blobdiff_plain {
2182        mkdir($git_temp, 0700);
2183        print $cgi->header(-type => "text/plain", -charset => 'utf-8');
2184        git_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, "plain");
2185}
2186
2187sub git_commitdiff {
2188        mkdir($git_temp, 0700);
2189        my %co = git_read_commit($hash);
2190        if (!%co) {
2191                die_error(undef, "Unknown commit object.");
2192        }
2193        if (!defined $hash_parent) {
2194                $hash_parent = $co{'parent'};
2195        }
2196        open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2197                or die_error(undef, "Open git-diff-tree failed.");
2198        my @difftree = map { chomp; $_ } <$fd>;
2199        close $fd or die_error(undef, "Reading diff-tree failed.");
2200
2201        # non-textual hash id's can be cached
2202        my $expires;
2203        if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
2204                $expires = "+1d";
2205        }
2206        my $refs = read_info_ref();
2207        my $ref = "";
2208        if (defined $refs->{$co{'id'}}) {
2209                $ref = " <span class=\"tag\">" . esc_html($refs->{$co{'id'}}) . "</span>";
2210        }
2211        git_header_html(undef, $expires);
2212        my $formats_nav =
2213                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent")}, "plain");
2214        git_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
2215        print "<div>\n" .
2216              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'}) . $ref) . "\n" .
2217              "</div>\n";
2218        print "<div class=\"page_body\">\n";
2219        my $comment = $co{'comment'};
2220        my $empty = 0;
2221        my $signed = 0;
2222        my @log = @$comment;
2223        # remove first and empty lines after that
2224        shift @log;
2225        while (defined $log[0] && $log[0] eq "") {
2226                shift @log;
2227        }
2228        foreach my $line (@log) {
2229                if ($line =~ m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {
2230                        next;
2231                }
2232                if ($line eq "") {
2233                        if ($empty) {
2234                                next;
2235                        }
2236                        $empty = 1;
2237                } else {
2238                        $empty = 0;
2239                }
2240                print format_log_line_html($line) . "<br/>\n";
2241        }
2242        print "<br/>\n";
2243        foreach my $line (@difftree) {
2244                # ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'
2245                # ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'
2246                $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2247                my $from_mode = $1;
2248                my $to_mode = $2;
2249                my $from_id = $3;
2250                my $to_id = $4;
2251                my $status = $5;
2252                my $file = validate_input(unquote($6));
2253                if ($status eq "A") {
2254                        print "<div class=\"diff_info\">" . file_type($to_mode) . ":" .
2255                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id) . "(new)" .
2256                              "</div>\n";
2257                        git_diff_print(undef, "/dev/null", $to_id, "b/$file");
2258                } elsif ($status eq "D") {
2259                        print "<div class=\"diff_info\">" . file_type($from_mode) . ":" .
2260                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) . "(deleted)" .
2261                              "</div>\n";
2262                        git_diff_print($from_id, "a/$file", undef, "/dev/null");
2263                } elsif ($status eq "M") {
2264                        if ($from_id ne $to_id) {
2265                                print "<div class=\"diff_info\">" .
2266                                      file_type($from_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$from_id;hb=$hash;f=$file")}, $from_id) .
2267                                      " -> " .
2268                                      file_type($to_mode) . ":" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$to_id;hb=$hash;f=$file")}, $to_id);
2269                                print "</div>\n";
2270                                git_diff_print($from_id, "a/$file",  $to_id, "b/$file");
2271                        }
2272                }
2273        }
2274        print "<br/>\n" .
2275              "</div>";
2276        git_footer_html();
2277}
2278
2279sub git_commitdiff_plain {
2280        mkdir($git_temp, 0700);
2281        open my $fd, "-|", $GIT, "diff-tree", '-r', $hash_parent, $hash
2282                or die_error(undef, "Open git-diff-tree failed.");
2283        my @difftree = map { chomp; $_ } <$fd>;
2284        close $fd or die_error(undef, "Reading diff-tree failed.");
2285
2286        # try to figure out the next tag after this commit
2287        my $tagname;
2288        my $refs = read_info_ref("tags");
2289        open $fd, "-|", $GIT, "rev-list", "HEAD";
2290        my @commits = map { chomp; $_ } <$fd>;
2291        close $fd;
2292        foreach my $commit (@commits) {
2293                if (defined $refs->{$commit}) {
2294                        $tagname = $refs->{$commit}
2295                }
2296                if ($commit eq $hash) {
2297                        last;
2298                }
2299        }
2300
2301        print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
2302        my %co = git_read_commit($hash);
2303        my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
2304        my $comment = $co{'comment'};
2305        print "From: $co{'author'}\n" .
2306              "Date: $ad{'rfc2822'} ($ad{'tz_local'})\n".
2307              "Subject: $co{'title'}\n";
2308        if (defined $tagname) {
2309                print "X-Git-Tag: $tagname\n";
2310        }
2311        print "X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\n" .
2312              "\n";
2313
2314        foreach my $line (@$comment) {;
2315                print "$line\n";
2316        }
2317        print "---\n\n";
2318
2319        foreach my $line (@difftree) {
2320                $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/;
2321                my $from_id = $3;
2322                my $to_id = $4;
2323                my $status = $5;
2324                my $file = $6;
2325                if ($status eq "A") {
2326                        git_diff_print(undef, "/dev/null", $to_id, "b/$file", "plain");
2327                } elsif ($status eq "D") {
2328                        git_diff_print($from_id, "a/$file", undef, "/dev/null", "plain");
2329                } elsif ($status eq "M") {
2330                        git_diff_print($from_id, "a/$file",  $to_id, "b/$file", "plain");
2331                }
2332        }
2333}
2334
2335sub git_history {
2336        if (!defined $hash_base) {
2337                $hash_base = git_read_head($project);
2338        }
2339        my $ftype;
2340        my %co = git_read_commit($hash_base);
2341        if (!%co) {
2342                die_error(undef, "Unknown commit object.");
2343        }
2344        my $refs = read_info_ref();
2345        git_header_html();
2346        git_page_nav('','', $hash_base,$co{'tree'},$hash_base);
2347        print "<div>\n" .
2348              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash_base"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2349              "</div>\n";
2350        if (!defined $hash && defined $file_name) {
2351                $hash = git_get_hash_by_path($hash_base, $file_name);
2352        }
2353        if (defined $hash) {
2354                $ftype = git_get_type($hash);
2355        }
2356        git_print_page_path($file_name, $ftype);
2357
2358        open my $fd, "-|",
2359                $GIT, "rev-list", "--full-history", $hash_base, "--", "\'$file_name\'";
2360        print "<table cellspacing=\"0\">\n";
2361        my $alternate = 0;
2362        while (my $line = <$fd>) {
2363                if ($line =~ m/^([0-9a-fA-F]{40})/){
2364                        my $commit = $1;
2365                        my %co = git_read_commit($commit);
2366                        if (!%co) {
2367                                next;
2368                        }
2369                        my $ref = "";
2370                        if (defined $refs->{$commit}) {
2371                                $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
2372                        }
2373                        if ($alternate) {
2374                                print "<tr class=\"dark\">\n";
2375                        } else {
2376                                print "<tr class=\"light\">\n";
2377                        }
2378                        $alternate ^= 1;
2379                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2380                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 3)) . "</i></td>\n" .
2381                              "<td>" . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"}, "<b>" .
2382                              esc_html(chop_str($co{'title'}, 50)) . "$ref</b>") . "</td>\n" .
2383                              "<td class=\"link\">" .
2384                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2385                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2386                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;hb=$commit;f=$file_name")}, "blob");
2387                        my $blob = git_get_hash_by_path($hash_base, $file_name);
2388                        my $blob_parent = git_get_hash_by_path($commit, $file_name);
2389                        if (defined $blob && defined $blob_parent && $blob ne $blob_parent) {
2390                                print " | " .
2391                                $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name")},
2392                                "diff to current");
2393                        }
2394                        print "</td>\n" .
2395                              "</tr>\n";
2396                }
2397        }
2398        print "</table>\n";
2399        close $fd;
2400        git_footer_html();
2401}
2402
2403sub git_search {
2404        if (!defined $searchtext) {
2405                die_error("", "Text field empty.");
2406        }
2407        if (!defined $hash) {
2408                $hash = git_read_head($project);
2409        }
2410        my %co = git_read_commit($hash);
2411        if (!%co) {
2412                die_error(undef, "Unknown commit object.");
2413        }
2414        # pickaxe may take all resources of your box and run for several minutes
2415        # with every query - so decide by yourself how public you make this feature :)
2416        my $commit_search = 1;
2417        my $author_search = 0;
2418        my $committer_search = 0;
2419        my $pickaxe_search = 0;
2420        if ($searchtext =~ s/^author\\://i) {
2421                $author_search = 1;
2422        } elsif ($searchtext =~ s/^committer\\://i) {
2423                $committer_search = 1;
2424        } elsif ($searchtext =~ s/^pickaxe\\://i) {
2425                $commit_search = 0;
2426                $pickaxe_search = 1;
2427        }
2428        git_header_html();
2429        git_page_nav('','', $hash,$co{'tree'},$hash);
2430
2431        print "<div>\n" .
2432              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$hash"), -class => "title"}, esc_html($co{'title'})) . "\n" .
2433              "</div>\n";
2434        print "<table cellspacing=\"0\">\n";
2435        my $alternate = 0;
2436        if ($commit_search) {
2437                $/ = "\0";
2438                open my $fd, "-|", $GIT, "rev-list", "--header", "--parents", $hash or next;
2439                while (my $commit_text = <$fd>) {
2440                        if (!grep m/$searchtext/i, $commit_text) {
2441                                next;
2442                        }
2443                        if ($author_search && !grep m/\nauthor .*$searchtext/i, $commit_text) {
2444                                next;
2445                        }
2446                        if ($committer_search && !grep m/\ncommitter .*$searchtext/i, $commit_text) {
2447                                next;
2448                        }
2449                        my @commit_lines = split "\n", $commit_text;
2450                        my %co = git_read_commit(undef, \@commit_lines);
2451                        if (!%co) {
2452                                next;
2453                        }
2454                        if ($alternate) {
2455                                print "<tr class=\"dark\">\n";
2456                        } else {
2457                                print "<tr class=\"light\">\n";
2458                        }
2459                        $alternate ^= 1;
2460                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2461                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2462                              "<td>" .
2463                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" . esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2464                        my $comment = $co{'comment'};
2465                        foreach my $line (@$comment) {
2466                                if ($line =~ m/^(.*)($searchtext)(.*)$/i) {
2467                                        my $lead = esc_html($1) || "";
2468                                        $lead = chop_str($lead, 30, 10);
2469                                        my $match = esc_html($2) || "";
2470                                        my $trail = esc_html($3) || "";
2471                                        $trail = chop_str($trail, 30, 10);
2472                                        my $text = "$lead<span class=\"match\">$match</span>$trail";
2473                                        print chop_str($text, 80, 5) . "<br/>\n";
2474                                }
2475                        }
2476                        print "</td>\n" .
2477                              "<td class=\"link\">" .
2478                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2479                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2480                        print "</td>\n" .
2481                              "</tr>\n";
2482                }
2483                close $fd;
2484        }
2485
2486        if ($pickaxe_search) {
2487                $/ = "\n";
2488                open my $fd, "-|", "$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\'$searchtext\'";
2489                undef %co;
2490                my @files;
2491                while (my $line = <$fd>) {
2492                        if (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\t(.*)$/) {
2493                                my %set;
2494                                $set{'file'} = $6;
2495                                $set{'from_id'} = $3;
2496                                $set{'to_id'} = $4;
2497                                $set{'id'} = $set{'to_id'};
2498                                if ($set{'id'} =~ m/0{40}/) {
2499                                        $set{'id'} = $set{'from_id'};
2500                                }
2501                                if ($set{'id'} =~ m/0{40}/) {
2502                                        next;
2503                                }
2504                                push @files, \%set;
2505                        } elsif ($line =~ m/^([0-9a-fA-F]{40})$/){
2506                                if (%co) {
2507                                        if ($alternate) {
2508                                                print "<tr class=\"dark\">\n";
2509                                        } else {
2510                                                print "<tr class=\"light\">\n";
2511                                        }
2512                                        $alternate ^= 1;
2513                                        print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2514                                              "<td><i>" . esc_html(chop_str($co{'author_name'}, 15, 5)) . "</i></td>\n" .
2515                                              "<td>" .
2516                                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}"), -class => "list"}, "<b>" .
2517                                              esc_html(chop_str($co{'title'}, 50)) . "</b><br/>");
2518                                        while (my $setref = shift @files) {
2519                                                my %set = %$setref;
2520                                                print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}"), class => "list"},
2521                                                      "<span class=\"match\">" . esc_html($set{'file'}) . "</span>") .
2522                                                      "<br/>\n";
2523                                        }
2524                                        print "</td>\n" .
2525                                              "<td class=\"link\">" .
2526                                              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$co{'id'}")}, "commit") .
2527                                              " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}")}, "tree");
2528                                        print "</td>\n" .
2529                                              "</tr>\n";
2530                                }
2531                                %co = git_read_commit($1);
2532                        }
2533                }
2534                close $fd;
2535        }
2536        print "</table>\n";
2537        git_footer_html();
2538}
2539
2540sub git_shortlog {
2541        my $head = git_read_head($project);
2542        if (!defined $hash) {
2543                $hash = $head;
2544        }
2545        if (!defined $page) {
2546                $page = 0;
2547        }
2548        my $refs = read_info_ref();
2549
2550        my $limit = sprintf("--max-count=%i", (100 * ($page+1)));
2551        open my $fd, "-|", $GIT, "rev-list", $limit, $hash
2552                or die_error(undef, "Open git-rev-list failed.");
2553        my @revlist = map { chomp; $_ } <$fd>;
2554        close $fd;
2555
2556        my $paging_nav = '';
2557        if ($hash ne $head || $page) {
2558                $paging_nav .= $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog")}, "HEAD");
2559        } else {
2560                $paging_nav .= "HEAD";
2561        }
2562        if ($page > 0) {
2563                $paging_nav .= " &sdot; " .
2564                        $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page-1)), -accesskey => "p", -title => "Alt-p"}, "prev");
2565        } else {
2566                $paging_nav .= " &sdot; prev";
2567        }
2568        if ($#revlist >= (100 * ($page+1)-1)) {
2569                $paging_nav .= " &sdot; " .
2570                        $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -accesskey => "n", -title => "Alt-n"}, "next");
2571        } else {
2572                $paging_nav .= " &sdot; next";
2573        }
2574
2575        git_header_html();
2576        git_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);
2577
2578        print "<div>\n" .
2579              $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary"), -class => "title"}, "&nbsp;") .
2580              "</div>\n";
2581        print "<table cellspacing=\"0\">\n";
2582        my $alternate = 0;
2583        for (my $i = ($page * 100); $i <= $#revlist; $i++) {
2584                my $commit = $revlist[$i];
2585                my $ref = "";
2586                if (defined $refs->{$commit}) {
2587                        $ref = " <span class=\"tag\">" . esc_html($refs->{$commit}) . "</span>";
2588                }
2589                my %co = git_read_commit($commit);
2590                my %ad = date_str($co{'author_epoch'});
2591                if ($alternate) {
2592                        print "<tr class=\"dark\">\n";
2593                } else {
2594                        print "<tr class=\"light\">\n";
2595                }
2596                $alternate ^= 1;
2597                print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
2598                      "<td><i>" . esc_html(chop_str($co{'author_name'}, 10)) . "</i></td>\n" .
2599                      "<td>";
2600                if (length($co{'title_short'}) < length($co{'title'})) {
2601                        print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list", -title => "$co{'title'}"},
2602                              "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
2603                } else {
2604                        print $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit"), -class => "list"},
2605                              "<b>" . esc_html($co{'title_short'}) . "$ref</b>");
2606                }
2607                print "</td>\n" .
2608                      "<td class=\"link\">" .
2609                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commit;h=$commit")}, "commit") .
2610                      " | " . $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=commitdiff;h=$commit")}, "commitdiff") .
2611                      "</td>\n" .
2612                      "</tr>";
2613        }
2614        if ($#revlist >= (100 * ($page+1)-1)) {
2615                print "<tr>\n" .
2616                      "<td>" .
2617                      $cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=shortlog;h=$hash;pg=" . ($page+1)), -title => "Alt-n"}, "next") .
2618                      "</td>\n" .
2619                      "</tr>\n";
2620        }
2621        print "</table\n>";
2622        git_footer_html();
2623}