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