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