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