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