[PATCH] gitweb: Add option to put a trailing slash on pathinfo-style project URLs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



My Web site uses pathinfo mode and some rewrite magic to show the gitweb
interface at the URL of the real repository directory (which users also
pull from).  In this case, it's desirable to end generated links to the
project in a trailing slash so the Web server doesn't have to redirect
the client to add the slash.  This patch adds a second element to the
"pathinfo" feature configuration to control the trailing slash.
---

What do you think of this?  I've been using it on my Web site for a
while now.

Matt

 gitweb/gitweb.perl |   28 ++++++++++++++++++++++------
 1 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 6eb370d..86511cf 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -270,6 +270,11 @@ our %feature = (
 	# $feature{'pathinfo'}{'default'} = [1];
 	# Project specific override is not supported.
 
+	# If you want a trailing slash on the project path (because, for
+	# example, you have a real directory at that URL and are using
+	# some rewrite magic to invoke gitweb), then set:
+	# $feature{'pathinfo'}{'default'} = [1, 1];
+
 	# Note that you will need to change the default location of CSS,
 	# favicon, logo and possibly other files to an absolute URL. Also,
 	# if gitweb.cgi serves as your indexfile, you will need to force
@@ -829,8 +834,8 @@ sub href (%) {
 		}
 	}
 
-	my $use_pathinfo = gitweb_check_feature('pathinfo');
-	if ($use_pathinfo) {
+	my @use_pathinfo = gitweb_get_feature('pathinfo');
+	if ($use_pathinfo[0]) {
 		# try to put as many parameters as possible in PATH_INFO:
 		#   - project name
 		#   - action
@@ -845,7 +850,12 @@ sub href (%) {
 		$href =~ s,/$,,;
 
 		# Then add the project name, if present
-		$href .= "/".esc_url($params{'project'}) if defined $params{'project'};
+		my $proj_href = undef;
+		if (defined $params{'project'}) {
+			$href .= "/".esc_url($params{'project'});
+			# Save for trailing-slash check below.
+			$proj_href = $href;
+		}
 		delete $params{'project'};
 
 		# since we destructively absorb parameters, we keep this
@@ -903,6 +913,10 @@ sub href (%) {
 			$href .= $known_snapshot_formats{$fmt}{'suffix'};
 			delete $params{'snapshot_format'};
 		}
+
+		# If requested in the configuration, add a trailing slash to a URL that
+		# has nothing appended after the project path.
+		$href .= '/' if ($use_pathinfo[1] && defined $proj_href && $href eq $proj_href);
 	}
 
 	# now encode the parameters explicitly
@@ -2987,13 +3001,15 @@ EOF
 			$search_hash = "HEAD";
 		}
 		my $action = $my_uri;
-		my $use_pathinfo = gitweb_check_feature('pathinfo');
-		if ($use_pathinfo) {
+		my @use_pathinfo = gitweb_get_feature('pathinfo');
+		if ($use_pathinfo[0]) {
 			$action .= "/".esc_url($project);
+			# Add a trailing slash if requested in the configuration.
+			$action .= '/' if ($use_pathinfo[1]);
 		}
 		print $cgi->startform(-method => "get", -action => $action) .
 		      "<div class=\"search\">\n" .
-		      (!$use_pathinfo &&
+		      (!$use_pathinfo[0] &&
 		      $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
 		      $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
 		      $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
-- 
1.6.1.rc2.24.gf17b3c.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux