add Expires: +1d header to commit and commitdiff pages
[git.git] / gitweb.cgi
index 3a89d82..873b7ce 100755 (executable)
@@ -5,7 +5,7 @@
 # (C) 2005, Kay Sievers <kay.sievers@vrfy.org>
 # (C) 2005, Christian Gierke <ch@gierke.de>
 #
-# This program is licensed under the GPL v2, or a later version
+# This program is licensed under the GPLv2
 
 use strict;
 use warnings;
@@ -15,7 +15,7 @@ use CGI::Carp qw(fatalsToBrowser);
 use Fcntl ':mode';
 
 my $cgi = new CGI;
-my $version =          "245";
+my $version =          "247";
 my $my_url =           $cgi->url();
 my $my_uri =           $cgi->url(-absolute => 1);
 my $rss_link = "";
@@ -152,8 +152,8 @@ sub validate_input {
 if (!defined $action || $action eq "summary") {
        git_summary();
        exit;
-} elsif ($action eq "branches") {
-       git_branches();
+} elsif ($action eq "heads") {
+       git_heads();
        exit;
 } elsif ($action eq "tags") {
        git_tags();
@@ -208,6 +208,7 @@ if (!defined $action || $action eq "summary") {
 
 sub git_header_html {
        my $status = shift || "200 OK";
+       my $expires = shift;
 
        my $title = "git";
        if (defined $project) {
@@ -216,7 +217,7 @@ sub git_header_html {
                        $title .= "/$action";
                }
        }
-       print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status);
+       print $cgi->header(-type=>'text/html',  -charset => 'utf-8', -status=> $status, -expires => $expires);
        print <<EOF;
 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
@@ -1067,15 +1068,15 @@ sub git_summary {
                print "</table\n>";
        }
 
-       my $branchlist = git_read_refs("refs/heads");
-       if (defined @$branchlist) {
+       my $headlist = git_read_refs("refs/heads");
+       if (defined @$headlist) {
                print "<div>\n" .
-                     $cgi->a({-href => "$my_uri?p=$project;a=branches", -class => "title"}, "branches") .
+                     $cgi->a({-href => "$my_uri?p=$project;a=heads", -class => "title"}, "heads") .
                      "</div>\n";
                my $i = 16;
                print "<table cellspacing=\"0\">\n";
                my $alternate = 0;
-               foreach my $entry (@$branchlist) {
+               foreach my $entry (@$headlist) {
                        my %tag = %$entry;
                        if ($alternate) {
                                print "<tr class=\"dark\">\n";
@@ -1095,7 +1096,7 @@ sub git_summary {
                                      "</td>\n" .
                                      "</tr>";
                        } else {
-                               print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=branches"}, "...") . "</td>\n" .
+                               print "<td>" . $cgi->a({-href => "$my_uri?p=$project;a=heads"}, "...") . "</td>\n" .
                                "</tr>";
                                last;
                        }
@@ -1203,7 +1204,7 @@ sub git_tags {
        git_footer_html();
 }
 
-sub git_branches {
+sub git_heads {
        my $head = git_read_hash("$project/HEAD");
        git_header_html();
        print "<div class=\"page_nav\">\n" .
@@ -1281,7 +1282,6 @@ sub git_blob {
                $hash = git_get_hash_by_path($base, $file_name, "blob");
        }
        open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
-       my $base = $file_name || "";
        git_header_html();
        if (defined $hash_base && (my %co = git_read_commit($hash_base))) {
                print "<div class=\"page_nav\">\n" .
@@ -1291,9 +1291,13 @@ sub git_blob {
                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base"}, "commit") .
                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=commitdiff;h=$hash_base"}, "commitdiff") .
                      " | " . $cgi->a({-href => "$my_uri?p=$project;a=tree;h=$co{'tree'};hb=$hash_base"}, "tree") . "<br/>\n";
-               print $cgi->a({-href => "$my_uri?p=$project;a=blob_plain;h=$hash"}, "plain") . "<br/>\n" .
-                     "</div>\n";
-               print "<div>" .
+               if (defined $file_name) {
+                       print $cgi->a({-href => "$my_uri?p=$project;a=blob_plain;h=$hash;f=$file_name"}, "plain") . "<br/>\n";
+               } else {
+                       print $cgi->a({-href => "$my_uri?p=$project;a=blob_plain;h=$hash"}, "plain") . "<br/>\n";
+               }
+               print "</div>\n".
+                      "<div>" .
                      $cgi->a({-href => "$my_uri?p=$project;a=commit;h=$hash_base", -class => "title"}, escapeHTML($co{'title'})) .
                      "</div>\n";
        } else {
@@ -1323,7 +1327,11 @@ sub git_blob {
 }
 
 sub git_blob_plain {
-       print $cgi->header(-type => "text/plain", -charset => 'utf-8');
+       my $save_as = "$hash.txt";
+       if (defined $file_name) {
+               $save_as = $file_name;
+       }
+       print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"$save_as\"");
        open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or return;
        undef $/;
        print <$fd>;
@@ -1613,7 +1621,13 @@ sub git_commit {
        open my $fd, "-|", "$gitbin/git-diff-tree -r -M $root $parent $hash" or die_error(undef, "Open failed.");
        @difftree = map { chomp; $_ } <$fd>;
        close $fd or die_error(undef, "Reading diff-tree failed.");
-       git_header_html();
+
+       # non-textual hash id's can be cached
+       my $expires;
+       if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
+               $expires = "+1d";
+       }
+       git_header_html(undef, $expires);
        print "<div class=\"page_nav\">\n" .
              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
@@ -1845,7 +1859,12 @@ sub git_commitdiff {
        my (@difftree) = map { chomp; $_ } <$fd>;
        close $fd or die_error(undef, "Reading diff-tree failed.");
 
-       git_header_html();
+       # non-textual hash id's can be cached
+       my $expires;
+       if ($hash =~ m/^[0-9a-fA-F]{40}$/) {
+               $expires = "+1d";
+       }
+       git_header_html(undef, $expires);
        print "<div class=\"page_nav\">\n" .
              $cgi->a({-href => "$my_uri?p=$project;a=summary"}, "summary") .
              " | " . $cgi->a({-href => "$my_uri?p=$project;a=shortlog;h=$hash"}, "shortlog") .
@@ -1945,7 +1964,7 @@ sub git_commitdiff_plain {
        }
        close $fd;
 
-       print $cgi->header(-type => "text/plain", -charset => 'utf-8');
+       print $cgi->header(-type => "text/plain", -charset => 'utf-8', '-content-disposition' => "inline; filename=\"git-$hash.patch\"");
        my %co = git_read_commit($hash);
        my %ad = date_str($co{'author_epoch'}, $co{'author_tz'});
        my $comment = $co{'comment'};