Skip to content

Commit 917adc0

Browse files
MarkLodatogitster
authored andcommitted
http-backend: add GIT_PROJECT_ROOT environment var
Add a new environment variable, GIT_PROJECT_ROOT, to override the method of using PATH_TRANSLATED to find the git repository on disk. This makes it much easier to configure the web server, especially when the web server's DocumentRoot does not contain the git repositories, which is the usual case. Signed-off-by: Mark Lodato <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 556cfa3 commit 917adc0

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

Documentation/git-http-backend.txt

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,24 @@ http.receivepack::
4141

4242
URL TRANSLATION
4343
---------------
44-
'git-http-backend' relies on the invoking web server to perform
45-
URL to path translation, and store the repository path into the
46-
PATH_TRANSLATED environment variable. Most web servers will do
47-
this translation automatically, resolving the suffix after the
48-
CGI name relative to the server's document root.
44+
To determine the location of the repository on disk, 'git-http-backend'
45+
concatenates the environment variables PATH_INFO, which is set
46+
automatically by the web server, and GIT_PROJECT_ROOT, which must be set
47+
manually in the web server configuration. If GIT_PROJECT_ROOT is not
48+
set, 'git-http-backend' reads PATH_TRANSLATED, which is also set
49+
automatically by the web server.
4950

5051
EXAMPLES
5152
--------
5253

5354
Apache 2.x::
54-
To serve all Git repositories contained within the '/git/'
55-
subdirectory of the DocumentRoot, ensure mod_cgi and
56-
mod_alias are enabled, and create a ScriptAlias to the CGI:
55+
Ensure mod_cgi, mod_alias, and mod_env are enabled, set
56+
GIT_PROJECT_ROOT (or DocumentRoot) appropriately, and
57+
create a ScriptAlias to the CGI:
5758
+
5859
----------------------------------------------------------------
59-
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/git/
60-
61-
<Directory /usr/libexec/git-core>
62-
Options None
63-
</Directory>
64-
<Files /usr/libexec/git-core/git-http-backend>
65-
Options ExecCGI
66-
</Files>
60+
SetEnv GIT_PROJECT_ROOT /var/www/git
61+
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
6762
----------------------------------------------------------------
6863
+
6964
To enable anonymous read access but authenticated write access,
@@ -78,16 +73,16 @@ require authorization with a LocationMatch directive:
7873
</LocationMatch>
7974
----------------------------------------------------------------
8075
+
81-
To require authentication for both reads and writes, use a Directory
76+
To require authentication for both reads and writes, use a Location
8277
directive around the repository, or one of its parent directories:
8378
+
8479
----------------------------------------------------------------
85-
<Directory /var/www/git/private>
80+
<Location /git/private>
8681
AuthType Basic
8782
AuthName "Private Git Access"
8883
Require group committers
8984
...
90-
</Directory>
85+
</Location>
9186
----------------------------------------------------------------
9287

9388
Accelerated static Apache 2.x::
@@ -97,9 +92,9 @@ Accelerated static Apache 2.x::
9792
file contents from the file system directly to the network:
9893
+
9994
----------------------------------------------------------------
100-
DocumentRoot /var/www
95+
SetEnv GIT_PROJECT_ROOT /var/www/git
10196

102-
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/git/
97+
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
10398
Alias /git_static/ /var/www/git/
10499

105100
RewriteEngine on
@@ -114,7 +109,7 @@ ENVIRONMENT
114109
'git-http-backend' relies upon the CGI environment variables set
115110
by the invoking web server, including:
116111

117-
* PATH_TRANSLATED
112+
* PATH_INFO (if GIT_PROJECT_ROOT is set, otherwise PATH_TRANSLATED)
118113
* REMOTE_USER
119114
* REMOTE_ADDR
120115
* CONTENT_TYPE

http-backend.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,26 @@ static NORETURN void die_webcgi(const char *err, va_list params)
528528
exit(0);
529529
}
530530

531+
static char* getdir(void)
532+
{
533+
struct strbuf buf = STRBUF_INIT;
534+
char *pathinfo = getenv("PATH_INFO");
535+
char *root = getenv("GIT_PROJECT_ROOT");
536+
char *path = getenv("PATH_TRANSLATED");
537+
538+
if (root && *root) {
539+
if (!pathinfo || !*pathinfo)
540+
die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
541+
strbuf_addstr(&buf, root);
542+
strbuf_addstr(&buf, pathinfo);
543+
return strbuf_detach(&buf, NULL);
544+
} else if (path && *path) {
545+
return xstrdup(path);
546+
} else
547+
die("No GIT_PROJECT_ROOT or PATH_TRANSLATED from server");
548+
return NULL;
549+
}
550+
531551
static struct service_cmd {
532552
const char *method;
533553
const char *pattern;
@@ -549,7 +569,7 @@ static struct service_cmd {
549569
int main(int argc, char **argv)
550570
{
551571
char *method = getenv("REQUEST_METHOD");
552-
char *dir = getenv("PATH_TRANSLATED");
572+
char *dir;
553573
struct service_cmd *cmd = NULL;
554574
char *cmd_arg = NULL;
555575
int i;
@@ -561,8 +581,7 @@ int main(int argc, char **argv)
561581
die("No REQUEST_METHOD from server");
562582
if (!strcmp(method, "HEAD"))
563583
method = "GET";
564-
if (!dir)
565-
die("No PATH_TRANSLATED from server");
584+
dir = getdir();
566585

567586
for (i = 0; i < ARRAY_SIZE(services); i++) {
568587
struct service_cmd *c = &services[i];

0 commit comments

Comments
 (0)