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