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