threads / patch / 31249

patchgitweb: Add option to limit repositories to be shown by ServerName

Subject: [PATCH] gitweb: Add option to limit repositories to be shown by ServerName

## tl;dr

3 messages between Aug 14, 2012 and Aug 14, 2012. Diffs are folded; open one to read it.

replies: 2people: 3as markdown or json

Michal Novotny· Aug 14, 2012, 08:47 UTC · lore

Hi, this is the patch to limit repositories to be shown by the ServerName in the gitweb.cgi script. This is useful for cases you're hosting multiple websites on a single machine and you don't want all the repos to be shown in all of them.

Functionality: Simply create file called 'server' in
               the git repository (the same location
               like for 'description' file) and put
               the server name to show this repo for.
Testing: It's been tested on a test server and everything
         was working fine/as expected. If there's no
         'server' file in the git repository it falls back
         to the default option to show the repository as
         it was working before this patch applied not to
         introduce any regressive behaviour.
Use case scenario: Imagine you have one server running HTTPd
                   for 3 domains, let's call them domain1,
                   domain2 and domain3, and you want all of
                   them to have a git server accessible via
                   gitweb at these URLs:
                   1) http://domain1/git
                   2) http://domain2/git
                   3) http://domain3/git
                   Before this patch, all the URLs would be
                   showing the same repos however we would
                   like to limit repo1 to be shown only on
                   domain1, repo2 to be shown only on domain2
                   and repo3 to be shown only on domain3. Here
                   my patch comes handy because you simply edit
                   the 'repo1/server' file and put 'domain1' in
                   here and it won't get shown on any other URL.
                   The same for repo2 and repo3 and it will be
                   shown only for the domain it belongs to.
                   Also, if no 'server' file exists in the repo
                   the repository is being shown so upgrading
                   gitweb with no creation of 'server' files
                   will preserve the old behaviour.
Hope this will help, at least it did help me on my test server.
Any feedback appreciated!

Thanks, Michal

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 gitweb/gitweb.perl | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
Show changes to gitweb/gitweb.perl +22 −2
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 3d6a705..1b74239 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -2804,6 +2804,11 @@ sub git_get_project_description {
 	return git_get_file_or_project_config($path, 'description');
 }
 
+sub git_get_project_server {
+	my $path = shift;
+	return git_get_file_or_project_config($path, 'server');
+}
+
 sub git_get_project_category {
 	my $path = shift;
 	return git_get_file_or_project_config($path, 'category');
@@ -2947,6 +2952,19 @@ sub git_get_project_url_list {
 	return wantarray ? @git_project_url_list : \@git_project_url_list;
 }
 
+sub get_project_can_export {
+	my ($path) = @_;
+
+	if (defined $ENV{'SERVER_NAME'}) {
+		my $server = git_get_project_server($path) || "";
+
+		return (($server eq $ENV{'SERVER_NAME'}) or ($server eq ''));
+	}
+	else {
+		return 1;
+	}
+}
+
 sub git_get_projects_list {
 	my $filter = shift || '';
 	my $paranoid = shift;
@@ -2990,7 +3008,8 @@ sub git_get_projects_list {
 					next;
 				}
 				# we check related file in $projectroot
-				if (check_export_ok("$projectroot/$path")) {
+				if (check_export_ok("$projectroot/$path") and
+					get_project_can_export($path)) {
 					push @list, { path => $path };
 					$File::Find::prune = 1;
 				}
@@ -3016,7 +3035,8 @@ sub git_get_projects_list {
 			if ($filter && $path !~ m!^\Q$filter\E/!) {
 				next;
 			}
-			if (check_export_ok("$projectroot/$path")) {
+			if (check_export_ok("$projectroot/$path") and
+				get_project_can_export($path)) {
 				my $pr = {
 					path => $path
 				};
-- 
1.7.11.2
Junio C Hamano· Aug 14, 2012, 16:04 UTC · re: Michal Novotny · lore

Re: [PATCH] gitweb: Add option to limit repositories to be shown by ServerName

Michal Novotny <minovotn@redhat.com> writes:
Show 13 quoted lines
> this is the patch to limit repositories to be shown by the
> ServerName in the gitweb.cgi script. This is useful for cases
> you're hosting multiple websites on a single machine and you don't
> want all the repos to be shown in all of them.
> ...
> Use case scenario: Imagine you have one server running HTTPd
>                    for 3 domains, let's call them domain1,
>                    domain2 and domain3, and you want all of
>                    them to have a git server accessible via
>                    gitweb at these URLs:
>                    1) http://domain1/git
>                    2) http://domain2/git
>                    3) http://domain3/git

I do not run gitweb myself, but isn't the problem you are describing merely a symptom caused by your <VirtualHost /> sections that are not configured correctly, and instead having a single instance of gitweb cgi enabled for all the virtual hosts? Why does such a physical host want to have git repositories for different domains in a single place that is covered by a single instance of gitweb (hence a single $projectroot) in the first place? After all, domain1's notion of "kernel git repository" http://domain1/git/kernel.git might be totally different from that of domain2's, so wouldn't it be far more natural to have one $projectroot (hence one instance of gitweb) per such domains, configured in their own <VirtualHost /> sections?

Also, there may even be http://domain3/git that does not want to be served by gitweb but something else, e.g. cgit, on the same physical host, and in such a case, the approach taken by this patch still uses gitweb only to fail the request without letting cgit have its chance, no?

J.H.· Aug 14, 2012, 16:11 UTC · re: Junio C Hamano · lore

Re: [PATCH] gitweb: Add option to limit repositories to be shown by ServerName

On 08/14/2012 10:04 AM, Junio C Hamano wrote:
Show 35 quoted lines
> Michal Novotny <minovotn@redhat.com> writes:
> 
>> this is the patch to limit repositories to be shown by the
>> ServerName in the gitweb.cgi script. This is useful for cases
>> you're hosting multiple websites on a single machine and you don't
>> want all the repos to be shown in all of them.
>> ...
>> Use case scenario: Imagine you have one server running HTTPd
>>                    for 3 domains, let's call them domain1,
>>                    domain2 and domain3, and you want all of
>>                    them to have a git server accessible via
>>                    gitweb at these URLs:
>>                    1) http://domain1/git
>>                    2) http://domain2/git
>>                    3) http://domain3/git
> 
> 
> I do not run gitweb myself, but isn't the problem you are describing
> merely a symptom caused by your <VirtualHost /> sections that are
> not configured correctly, and instead having a single instance of
> gitweb cgi enabled for all the virtual hosts?  Why does such a
> physical host want to have git repositories for different domains in
> a single place that is covered by a single instance of gitweb (hence
> a single $projectroot) in the first place?  After all, domain1's
> notion of "kernel git repository" http://domain1/git/kernel.git
> might be totally different from that of domain2's, so wouldn't it be
> far more natural to have one $projectroot (hence one instance of
> gitweb) per such domains, configured in their own <VirtualHost />
> sections?
> 
> Also, there may even be http://domain3/git that does not want to be
> served by gitweb but something else, e.g. cgit, on the same physical
> host, and in such a case, the approach taken by this patch still uses
> gitweb only to fail the request without letting cgit have its
> chance, no?

The use case scenario is doable with Apache's vhost directives, and is likely a much better way to accomplish this then trying to teach gitweb about vhosting.

http://httpd.apache.org/docs/2.2/vhosts/mass.html

should give some hints on how this can be done, but the long and the short is, single install of gitweb with the config file using, say %0, to dynamically figure out which domain to work from.

- John

← back to recent threads