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