From: bluewind at xinu.at (Florian Pritz)
Subject: [PATCH v2] Support Git over HTTP using git-http-backend
Date: Mon, 29 Dec 2014 17:56:05 +0100 [thread overview]
Message-ID: <1419872165-23537-1-git-send-email-bluewind@xinu.at> (raw)
In-Reply-To: <1419869598-19217-1-git-send-email-bluewind@xinu.at>
This saves users from the hassle of setting up git-http-backend when
they already run cgit.
References: man git-http-backend
Signed-off-by: Florian Pritz <bluewind at xinu.at>
---
v2:
- git_root allocation was too small by 1
- change comment style to c89
cgit.c | 3 +++
cgit.h | 1 +
cgitrc.5.txt | 8 ++++++++
cmd.c | 12 ++++++++++++
ui-clone.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ui-clone.h | 2 ++
6 files changed, 87 insertions(+)
diff --git a/cgit.c b/cgit.c
index 79019c2..0c4a292 100644
--- a/cgit.c
+++ b/cgit.c
@@ -124,6 +124,8 @@ static void config_cb(const char *name, const char *value)
ctx.cfg.head_include = xstrdup(value);
else if (!strcmp(name, "header"))
ctx.cfg.header = xstrdup(value);
+ else if (!strcmp(name, "http-backend-path"))
+ ctx.cfg.http_backend_path = xstrdup(value);
else if (!strcmp(name, "logo"))
ctx.cfg.logo = xstrdup(value);
else if (!strcmp(name, "index-header"))
@@ -353,6 +355,7 @@ static void prepare_context(void)
ctx.cfg.css = "/cgit.css";
ctx.cfg.logo = "/cgit.png";
ctx.cfg.favicon = "/favicon.ico";
+ ctx.cfg.http_backend_path = NULL;
ctx.cfg.local_time = 0;
ctx.cfg.enable_http_clone = 1;
ctx.cfg.enable_index_owner = 1;
diff --git a/cgit.h b/cgit.h
index 42140ac..dcd44f0 100644
--- a/cgit.h
+++ b/cgit.h
@@ -190,6 +190,7 @@ struct cgit_config {
char *footer;
char *head_include;
char *header;
+ char *http_backend_path;
char *index_header;
char *index_info;
char *logo;
diff --git a/cgitrc.5.txt b/cgitrc.5.txt
index be6703f..62b73ba 100644
--- a/cgitrc.5.txt
+++ b/cgitrc.5.txt
@@ -218,6 +218,11 @@ header::
The content of the file specified with this option will be included
verbatim at the top of all pages. Default value: none.
+http-backend-path::
+ Path to the git-http-backend smart HTTP backend binary. Setting this
+ allows the git clone to fetch/push via Git over HTTP. You'll also
+ need to enable enable-http-clone for this to work. Default value: none.
+
include::
Name of a configfile to include before the rest of the current config-
file is parsed. Default value: none. See also: "MACRO EXPANSION".
@@ -766,6 +771,9 @@ enable-index-owner=1
# Allow http transport git clone
enable-http-clone=1
+# Use Git over HTTP
+http-backend-path=/usr/lib/git-core/git-http-backend
+
# Show extra links for each repository on the index page
enable-index-links=1
diff --git a/cmd.c b/cmd.c
index 188cd56..889ed05 100644
--- a/cmd.c
+++ b/cmd.c
@@ -136,6 +136,16 @@ static void tree_fn(void)
cgit_print_tree(ctx.qry.sha1, ctx.qry.path);
}
+static void git_upload_pack_fn(void)
+{
+ cgit_clone_git_upload_pack();
+}
+
+static void git_receive_pack_fn(void)
+{
+ cgit_clone_git_receive_pack();
+}
+
#define def_cmd(name, want_repo, want_layout, want_vpath, is_clone) \
{#name, name##_fn, want_repo, want_layout, want_vpath, is_clone}
@@ -162,6 +172,8 @@ struct cgit_cmd *cgit_get_cmd(void)
def_cmd(summary, 1, 1, 0, 0),
def_cmd(tag, 1, 1, 0, 0),
def_cmd(tree, 1, 1, 1, 0),
+ {"git-upload-pack", git_upload_pack_fn, 1, 0, 0, 1},
+ {"git-receive-pack", git_receive_pack_fn, 1, 0, 0, 1},
};
int i;
diff --git a/ui-clone.c b/ui-clone.c
index a4ffd6e..91d8306 100644
--- a/ui-clone.c
+++ b/ui-clone.c
@@ -69,8 +69,43 @@ static void send_file(char *path)
html_include(path);
}
+static void dispatch_to_git_http_backend(void)
+{
+ if (access(ctx.cfg.http_backend_path, X_OK) != -1) {
+ size_t git_root_len;
+ char *git_root = NULL;
+
+ /* git-http-backend does it's own URL parsing and concatenates
+ * GIT_PROJECT_ROOT and PATH_INFO to find the git repo.
+ * The root is the same as scan-path, but scan-path is not
+ * always available so we calculate the root path.
+ * Example:
+ * repo.path = /srv/git/some/more/dirs/
+ * qry.repo = some/more/dirs
+ * -> git_root = /srv/git/
+ */
+ strip_suffix(ctx.repo->path, "/", &git_root_len);
+ strip_suffix_mem(ctx.repo->path, &git_root_len, ctx.qry.repo);
+
+ git_root = xmalloc(git_root_len + 1);
+ strncpy(git_root, ctx.repo->path, git_root_len);
+ git_root[git_root_len] = '\0';
+
+ setenv("GIT_PROJECT_ROOT", git_root, 1);
+ execl(ctx.cfg.http_backend_path, "git-http-backend", NULL);
+ } else {
+ fprintf(stderr, "[cgit] http-backend-path (%s) is not executable: %s\n",
+ ctx.cfg.http_backend_path, strerror(errno));
+ html_status(500, "Internal Server Error", 0);
+ }
+}
+
void cgit_clone_info(void)
{
+ if (ctx.cfg.http_backend_path) {
+ return dispatch_to_git_http_backend();
+ }
+
if (!ctx.qry.path || strcmp(ctx.qry.path, "refs"))
return;
@@ -82,6 +117,10 @@ void cgit_clone_info(void)
void cgit_clone_objects(void)
{
+ if (ctx.cfg.http_backend_path) {
+ return dispatch_to_git_http_backend();
+ }
+
if (!ctx.qry.path) {
html_status(400, "Bad request", 0);
return;
@@ -97,5 +136,27 @@ void cgit_clone_objects(void)
void cgit_clone_head(void)
{
+ if (ctx.cfg.http_backend_path) {
+ return dispatch_to_git_http_backend();
+ }
+
send_file(git_path("%s", "HEAD"));
}
+
+void cgit_clone_git_upload_pack(void)
+{
+ if (ctx.cfg.http_backend_path) {
+ return dispatch_to_git_http_backend();
+ }
+
+ html_status(404, "Not found", 0);
+}
+
+void cgit_clone_git_receive_pack(void)
+{
+ if (ctx.cfg.http_backend_path) {
+ return dispatch_to_git_http_backend();
+ }
+
+ html_status(404, "Not found", 0);
+}
diff --git a/ui-clone.h b/ui-clone.h
index 3e460a3..b27087e 100644
--- a/ui-clone.h
+++ b/ui-clone.h
@@ -4,5 +4,7 @@
void cgit_clone_info(void);
void cgit_clone_objects(void);
void cgit_clone_head(void);
+void cgit_clone_git_upload_pack(void);
+void cgit_clone_git_receive_pack(void);
#endif /* UI_CLONE_H */
--
2.2.1
next prev parent reply other threads:[~2014-12-29 16:56 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-12-29 16:13 [PATCH] " bluewind
2014-12-29 16:56 ` bluewind [this message]
2014-12-29 17:16 ` [PATCH v2] " cgit
2015-01-15 23:57 ` bluewind
2015-01-19 16:55 ` Jason
2015-01-19 17:08 ` cgit
2015-01-19 16:58 ` Jason
2015-01-19 17:13 ` cgit
2015-01-19 17:19 ` Jason
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1419872165-23537-1-git-send-email-bluewind@xinu.at \
--to=cgit@lists.zx2c4.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).