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