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