fe44b188cae2da0eabae06704ee59e8bd47e25a6
   1#!/usr/bin/perl
   2
   3# This file is licensed under the GPL v2, or a later version
   4# (C) 2005, Kay Sievers <kay.sievers@vrfy.org>
   5# (C) 2005, Christian Gierke <ch@gierke.de>
   6
   7use strict;
   8use warnings;
   9use CGI qw(:standard :escapeHTML);
  10use CGI::Carp qw(fatalsToBrowser);
  11
  12my $cgi = new CGI;
  13my $gitbin = "/home/kay/bin/git";
  14my $gitroot = "/home/kay/public_html";
  15my $gittmp = "/tmp";
  16my $myself = $cgi->url(-relative => 1);
  17
  18my $project = $cgi->param("project") || "";
  19my $action = $cgi->param("action") || "";
  20my $hash = $cgi->param("hash") || "";
  21my $parent = $cgi->param("parent") || "";
  22my $view_back = $cgi->param("view_back") || 60*60*24;
  23my $projectroot = "$gitroot/$project";
  24$ENV{'SHA1_FILE_DIRECTORY'} = "$projectroot/.git/objects";
  25
  26$hash =~ s/[^0-9a-fA-F]//g;
  27$parent =~ s/[^0-9a-fA-F]//g;
  28$project =~ s/[^0-9a-zA-Z\-\._]//g;
  29
  30sub git_header {
  31        print $cgi->header(-type => 'text/html; charset: utf-8');
  32print <<EOF;
  33<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  34<html>
  35<head>
  36        <title>git - $project $action</title>
  37        <style type="text/css">
  38                body { font-family: sans-serif; font-size: 12px; margin:25px; }
  39                div.body { border-width:1px; border-style:solid; border-color:#D9D8D1; }
  40                div.head1 { font-size:20px; padding:8px; background-color: #D9D8D1; font-weight:bold; }
  41                div.head1 a:visited { color:#0000cc; }
  42                div.head1 a:hover { color:#880000; }
  43                div.head1 a:active { color:#880000; }
  44                div.head2 { padding:8px; }
  45                div.head2 a:visited { color:#0000cc; }
  46                div.head2 a:hover { color:#880000; }
  47                div.head2 a:active { color:#880000; }
  48                div.main { padding:8px; font-family: sans-serif; font-size: 12px; }
  49                table { padding:0px; margin:0px; width:100%; }
  50                tr { vertical-align:top; }
  51                td { padding:8px; margin:0px; font-family: sans-serif; font-size: 12px; }
  52                td.head1 { background-color: #D9D8D1; font-weight:bold; }
  53                td.head1 a { color:#000000; text-decoration:none; }
  54                td.head1 a:hover { color:#880000; text-decoration:underline; }
  55                td.head1 a:visited { color:#000000; }
  56                td.head2 { background-color: #EDECE6; font-family: monospace; font-size:12px; }
  57                td.head3 { background-color: #EDECE6; font-size:10px; }
  58                div.add { color: #008800; }
  59                div.subtract { color: #CC0000; }
  60                div.diff_head { color: #000099; }
  61                div.diff_head a:visited { color:#0000cc; }
  62                div.diff_line { color: #990099; }
  63                a { color:#0000cc; }
  64                a:hover { color:#880000; }
  65                a:visited { color:#880000; }
  66                a:active { color:#880000; }
  67        </style>
  68</head>
  69<body>
  70EOF
  71        print "<div class=\"body\">\n";
  72        print "<div class=\"head1\">";
  73        print "<a href=\"http://kernel.org/pub/software/scm/git/\"><img src=\"git_logo.png\" width=\"72\" height=\"27\" alt=\"git\" style=\"float:right; border-width:0px;\"/></a>";
  74        print $cgi->a({-href => "$myself"}, "projects");
  75        if ($project ne "") {
  76                print " / " . $cgi->a({-href => "$myself?project=$project;action=log;view_back=" . 60*60*24}, $project);
  77        }
  78        if ($action ne "") {
  79                print " / $action";
  80        }
  81        print "</div>\n";
  82}
  83
  84sub git_footer {
  85        print "</div>";
  86        print $cgi->end_html();
  87}
  88
  89sub git_diff {
  90        my $old_name = shift || "/dev/null";
  91        my $new_name = shift || "/dev/null";
  92        my $old = shift;
  93        my $new = shift;
  94
  95        my $tmp_old = "/dev/null";
  96        my $tmp_new = "/dev/null";
  97        my $old_label = "/dev/null";
  98        my $new_label = "/dev/null";
  99
 100        # create temp from-file
 101        if ($old ne "") {
 102                open my $fd2, "> $gittmp/$old";
 103                open my $fd, "-|", "$gitbin/cat-file", "blob", $old;
 104                while (my $line = <$fd>) {
 105                        print $fd2 $line;
 106                }
 107                close $fd2;
 108                close $fd;
 109                $tmp_old = "$gittmp/$old";
 110                $old_label = "a/$old_name";
 111        }
 112
 113        # create tmp to-file
 114        if ($new ne "") {
 115                open my $fd2, "> $gittmp/$new";
 116                open my $fd, "-|", "$gitbin/cat-file", "blob", $new;
 117                while (my $line = <$fd>) {
 118                        print $fd2 $line;
 119                }
 120                close $fd2;
 121                close $fd;
 122                $tmp_new = "$gittmp/$new";
 123                $new_label = "a/$new_name";
 124        }
 125
 126        open my $fd, "-|", "/usr/bin/diff", "-L", $old_label, "-L", $new_label, "-u", "-p", $tmp_old, $tmp_new;
 127        print '<div class="diff_head">===== ';
 128        if ($old ne "") {
 129                print $cgi->a({-href => "$myself?project=$project;action=blob;hash=$old"}, $old);
 130        } else {
 131                print $old_name;
 132        }
 133        print " vs ";
 134        if ($new ne "") {
 135                print $cgi->a({-href => "$myself?project=$project;action=blob;hash=$new"}, $new);
 136        } else {
 137                print $new_name;
 138        }
 139        print ' =====</div>';
 140        while (my $line = <$fd>) {
 141                my $char = substr($line,0,1);
 142                print '<div class="add">' if $char eq '+';
 143                print '<div class="subtract">' if $char eq '-';
 144                print '<div class="diff_line">' if $char eq '@';
 145                print escapeHTML($line);
 146                print '</div>' if $char eq '+' or $char eq '-' or $char eq '@';
 147        }
 148        close $fd;
 149        unlink("$gittmp/$new");
 150        unlink("$gittmp/$old");
 151}
 152
 153if ($project eq "") {
 154        open my $fd, "-|", "ls", "-1", $gitroot;
 155        my (@path) = map { chomp; $_ } <$fd>;
 156        close $fd;
 157        git_header();
 158        print "<br/><br/><div class=\"main\">\n";
 159        foreach my $line (@path) {
 160                if (-e "$gitroot/$line/.git/HEAD") {
 161                        print $cgi->a({-href => "$myself?project=$line"}, $line) . "<br/>\n";
 162                }
 163        }
 164        print "<br/></div>";
 165        git_footer();
 166        exit;
 167}
 168
 169if ($action eq "") {
 170        print $cgi->redirect("$myself?project=$project;action=log;view_back=$view_back");
 171        exit;
 172}
 173
 174if ($action eq "blob") {
 175        git_header();
 176        print "<br/><br/><div class=\"main\">\n";
 177        print "<pre>\n";
 178        open my $fd, "-|", "$gitbin/cat-file", "blob", $hash;
 179        my $nr;
 180        while (my $line = <$fd>) {
 181                $nr++;
 182                print "$nr\t" . escapeHTML($line);;
 183        }
 184        close $fd;
 185        print "</pre>\n";
 186        print "<br/></div>";
 187        git_footer();
 188} elsif ($action eq "tree") {
 189        if ($hash eq "") {
 190                open my $fd, "$projectroot/.git/HEAD";
 191                my $head = <$fd>;
 192                chomp $head;
 193                close $fd;
 194                $hash = $head;
 195        }
 196        open my $fd, "-|", "$gitbin/ls-tree", $hash;
 197        my (@entries) = map { chomp; $_ } <$fd>;
 198        close $fd;
 199        git_header();
 200        print "<br/><br/><div class=\"main\">\n";
 201        print "<pre>\n";
 202        foreach my $line (@entries) {
 203                #'100644        blob    0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa        panic.c'
 204                $line =~ m/^([0-9]+)\t(.*)\t(.*)\t(.*)$/;
 205                my $t_type = $2;
 206                my $t_hash = $3;
 207                my $t_name = $4;
 208                if ($t_type eq "blob") {
 209                        print "BLOB\t" . $cgi->a({-href => "$myself?project=$project;action=blob;hash=$3"}, $4) . "\n";
 210                } elsif ($t_type eq "tree") {
 211                        print "TREE\t" . $cgi->a({-href => "$myself?project=$project;action=tree;hash=$3"}, $4) . "\n";
 212                }
 213        }
 214        print "</pre>\n";
 215        print "<br/></div>";
 216        git_footer();
 217} elsif ($action eq "log" || $action eq "show_log" ) {
 218        open my $fd, "$projectroot/.git/HEAD";
 219        my $head = <$fd>;
 220        chomp $head;
 221        close $fd;
 222        open $fd, "-|", "$gitbin/rev-tree", $head;
 223        my (@revtree) = map { chomp; $_ } <$fd>;
 224        close $fd;
 225        git_header();
 226        print "<div class=\"head2\">\n";
 227        print "view  ";
 228        print $cgi->a({-href => "$myself?project=$project;action=log;view_back=" . 60*60*24}, "last day") . " | ";
 229        print $cgi->a({-href => "$myself?project=$project;action=log;view_back=" . 60*60*24*7}, "week") . " | ";
 230        print $cgi->a({-href => "$myself?project=$project;action=log;view_back=" . 60*60*24*30}, "month") . " | ";
 231        print $cgi->a({-href => "$myself?project=$project;action=log;view_back=" . 60*60*24*365}, "year") . " | ";
 232        print $cgi->a({-href => "$myself?project=$project;action=log;view_back=-1"}, "all") . "<br/>\n";
 233        print "<br/><br/>\n";
 234        print "</div>\n";
 235        print "<table cellspacing=\"0\" class=\"log\">\n";
 236        foreach my $rev (reverse sort @revtree) {
 237                last if !($rev =~ m/^([0-9]+) ([0-9a-fA-F]+).* ([0-9a-fA-F]+)/);
 238                my $time = $1;
 239                my $commit = $2;
 240                my $parent = $3;
 241                my @parents;
 242                my $author;
 243                my $author_name;
 244                my $author_time;
 245                my $author_timezone;
 246                my $committer;
 247                my $committer_time;
 248                my $committer_timezone;
 249                my $tree;
 250                my $comment;
 251                my $shortlog;
 252                open my $fd, "-|", "$gitbin/cat-file", "commit", $commit;
 253                while (my $line = <$fd>) {
 254                        chomp($line);
 255                        last if $line eq "";
 256                        if ($line =~ m/^tree (.*)$/) {
 257                                $tree = $1;
 258                        } elsif ($line =~ m/^parent (.*)$/) {
 259                                push @parents, $1;
 260                        } elsif ($line =~ m/^committer (.*>) ([0-9]+) (.*)$/) {
 261                                $committer = $1;
 262                                $committer_time = $2;
 263                                $committer_timezone = $3;
 264                        } elsif ($line =~ m/^author (.*>) ([0-9]+) (.*)$/) {
 265                                $author = $1;
 266                                $author_time = $2;
 267                                $author_timezone = $3;
 268                                $author =~ m/^(.*) </;
 269                                $author_name = $1;
 270                        }
 271                }
 272                $shortlog = <$fd>;
 273                $shortlog = escapeHTML($shortlog);
 274                $comment = $shortlog . "<br/>";
 275                while (my $line = <$fd>) {
 276                                chomp($line);
 277                                $comment .= escapeHTML($line) . "<br/>\n";
 278                }
 279                close $fd;
 280                my $age = time-$committer_time;
 281                last if ($view_back > 0 && $age > $view_back);
 282
 283                my $age_string;
 284                if ($age > 60*60*24*365*2) {
 285                        $age_string = int $age/60/60/24/365;
 286                        $age_string .= " years ago";
 287                } elsif ($age > 60*60*24*365/12*2) {
 288                        $age_string = int $age/60/60/24/365/12;
 289                        $age_string .= " months ago";
 290                } elsif ($age > 60*60*24*7*2) {
 291                        $age_string = int $age/60/60/24/7;
 292                        $age_string .= " weeks ago";
 293                } elsif ($age > 60*60*24*2) {
 294                        $age_string = int $age/60/60/24;
 295                        $age_string .= " days ago";
 296                } elsif ($age > 60*60*2) {
 297                        $age_string = int $age/60/60;
 298                        $age_string .= " hours ago";
 299                } elsif ($age > 60*2) {
 300                        $age_string = int $age/60;
 301                        $age_string .= " minutes ago";
 302                }
 303                print "<tr>\n";
 304                print "<td class=\"head1\">" . $age_string . "</td>\n";
 305                print "<td class=\"head1\">" . $cgi->a({-href => "$myself?project=$project;action=commit;hash=$commit;parent=$parent"}, $shortlog) . "</td>";
 306                print "</tr>\n";
 307                print "<tr>\n";
 308                print "<td class=\"head3\">";
 309                print $cgi->a({-href => "$myself?project=$project;action=diffs;hash=$commit;parent=$parent"}, "view diff") . "<br/>\n";
 310                print $cgi->a({-href => "$myself?project=$project;action=commit;hash=$commit;parent=$parent"}, "view commit") . "<br/>\n";
 311                print $cgi->a({-href => "$myself?project=$project;action=tree;hash=$tree"}, "view tree") . "<br/>\n";
 312                print "</td>\n";
 313                print "<td class=\"head2\">\n";
 314                print "author &nbsp; &nbsp;" . escapeHTML($author) . " [" . gmtime($author_time) . " " . $author_timezone . "]<br/>\n";
 315                print "committer " . escapeHTML($committer) . " [" . gmtime($committer_time) . " " . $committer_timezone . "]<br/>\n";
 316                print "commit &nbsp; &nbsp;$commit<br/>\n";
 317                print "tree &nbsp; &nbsp; &nbsp;$tree<br/>\n";
 318                foreach my $par (@parents) {
 319                        print "parent &nbsp; &nbsp;$par<br/>\n";
 320                }
 321                print "</td>";
 322                print "</tr>\n";
 323                print "<tr>\n";
 324                print "<td></td>\n";
 325                print "<td>\n";
 326                print "$comment<br/><br/>\n";
 327                print "</td>";
 328                print "</tr>\n";
 329        }
 330        print "</table>\n";
 331        git_footer();
 332} elsif ($action eq "commit") {
 333        open my $fd, "-|", "$gitbin/diff-tree", "-r", $parent, $hash;
 334        my (@difftree) = map { chomp; $_ } <$fd>;
 335        close $fd;
 336
 337        git_header();
 338        print "<div class=\"head2\">\n";
 339        print "view " . $cgi->a({-href => "$myself?project=$project;action=diffs;hash=$hash;parent=$parent"}, "diff") . "<br/><br/>\n";
 340        print "<pre>\n";
 341        foreach my $line (@difftree) {
 342                # '*100644->100644      blob    9f91a116d91926df3ba936a80f020a6ab1084d2b->bb90a0c3a91eb52020d0db0e8b4f94d30e02d596      net/ipv4/route.c'
 343                # '+100644      blob    4a83ab6cd565d21ab0385bac6643826b83c2fcd4        arch/arm/lib/bitops.h'
 344                $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
 345                my $op = $1;
 346                my $mode = $2;
 347                my $type = $3;
 348                my $id = $4;
 349                my $file = $5;
 350                if ($type eq "blob") {
 351                        if ($op eq "+") {
 352                                print "NEW\t" . $cgi->a({-href => "$myself?project=$project;action=blob;hash=$id"}, $file) . "\n";
 353                        } elsif ($op eq "-") {
 354                                print "DEL\t" . $cgi->a({-href => "$myself?project=$project;action=blob;hash=$id"}, $file) . "\n";
 355                        } elsif ($op eq "*") {
 356                                $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
 357                                my $old = $1;
 358                                my $new = $2;
 359                                print "CHANGED\t" . $cgi->a({-href => "$myself?project=$project;action=diff;hash=$old;parent=$new"}, $file) . "\n";
 360                        }
 361                }
 362        }
 363        print "</pre>\n";
 364        print "<br/></div>";
 365        git_footer();
 366} elsif ($action eq "diff") {
 367        git_header();
 368        print "<br/><br/><div class=\"main\">\n";
 369        print "<pre>\n";
 370        git_diff($hash, $parent, $hash, $parent);
 371        print "</pre>\n";
 372        print "<br/></div>";
 373        git_footer();
 374} elsif ($action eq "diffs") {
 375        open my $fd, "-|", "$gitbin/diff-tree", "-r", $parent, $hash;
 376        my (@difftree) = map { chomp; $_ } <$fd>;
 377        close $fd;
 378
 379        git_header();
 380        print "<div class=\"head2\">\n";
 381        print "view " . $cgi->a({-href => "$myself?project=$project;action=commit;hash=$hash;parent=$parent"}, "commit") . "<br/><br/>\n";
 382        print "<pre>\n";
 383        foreach my $line (@difftree) {
 384                # '*100644->100644      blob    8e5f9bbdf4de94a1bc4b4da8cb06677ce0a57716->8da3a306d0c0c070d87048d14a033df02f40a154      Makefile'
 385                $line =~ m/^(.)(.*)\t(.*)\t(.*)\t(.*)$/;
 386                my $op = $1;
 387                my $mode = $2;
 388                my $type = $3;
 389                my $id = $4;
 390                my $file = $5;
 391                if ($type eq "blob") {
 392                        if ($op eq "+") {
 393                                git_diff("", $file, "", $id);
 394                        } elsif ($op eq "-") {
 395                                git_diff($file, "", $id, "");
 396                        } elsif ($op eq "*") {
 397                                $id =~ m/([0-9a-fA-F]+)->([0-9a-fA-F]+)/;
 398                                git_diff($file, $file, $1, $2);
 399                        }
 400                }
 401        }
 402        print "</pre>\n";
 403        print "<br/></div>";
 404        git_footer();
 405} else {
 406        git_header();
 407        print "<br/><br/><div class=\"main\">\n";
 408        print "unknown action\n";
 409        print "<br/></div>";
 410        git_footer();
 411}