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