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