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