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