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