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