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