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