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