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