{"thread":{"id":"25392","subject":"[RFC PATCH v5 0/3] git-remote-fd & git-remote-ext","startedAt":"2010-10-08T18:39:57Z","lastAt":"2010-10-08T20:07:22Z","messageCount":6,"participants":["Ilari Liusvaara","Junio C Hamano","Pat Notz"],"isPatch":true,"patchVersion":5,"patchTotal":3},"messages":[{"id":"152993","messageId":"1286563200-7270-1-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"25392","inReplyTo":null,"subject":"[RFC PATCH v5 0/3] git-remote-fd & git-remote-ext","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-10-08T18:39:57Z","receivedAt":"2010-10-08T18:39:57Z","isPatch":true,"sender":{"key":"ilari.liusvaara@elisanet.fi","avatar":null},"body":"This adds two new remote helpers.\n\n* git-remote-fd, which connects to git service on given file descriptor(s),\nuseful for graphical user interfaces that want to use internal ssh client.\n\n* git-remote-ext, which connect to git service using external program. Useful\nfor connecting using odd one-off ssh options, to services in abstract\nnamespace, using unix domain sockets, using TLS, etc...\n\nChanges from last time:\n\n* Rewrite the bidirectional copy loop to use threads or processes. The\nresulting code is bit simpler and doesn't rely on poll/select working\non sockets/pipes.\n\nIlari Liusvaara (3):\n  Add bidirectional_transfer_loop()\n  git-remote-fd\n  git-remote-ext\n\n .gitignore                       |    2 +\n Documentation/git-remote-ext.txt |  125 ++++++++++++++++++\n Documentation/git-remote-fd.txt  |   59 +++++++++\n Makefile                         |    2 +\n builtin.h                        |    2 +\n builtin/remote-ext.c             |  243 +++++++++++++++++++++++++++++++++++\n builtin/remote-fd.c              |   80 ++++++++++++\n compat/mingw.h                   |    5 +\n git.c                            |    2 +\n transport-helper.c               |  261 ++++++++++++++++++++++++++++++++++++++\n transport.h                      |    1 +\n 11 files changed, 782 insertions(+), 0 deletions(-)\n create mode 100644 Documentation/git-remote-ext.txt\n create mode 100644 Documentation/git-remote-fd.txt\n create mode 100644 builtin/remote-ext.c\n create mode 100644 builtin/remote-fd.c\n\n-- \n1.7.3.1.53.gb58bd\n"},{"id":"152995","messageId":"1286563200-7270-2-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"25392","inReplyTo":"1286563200-7270-1-git-send-email-ilari.liusvaara@elisanet.fi","subject":"[RFC PATCH v5 1/3] Add bidirectional_transfer_loop()","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-10-08T18:39:58Z","receivedAt":"2010-10-08T18:39:58Z","isPatch":true,"sender":{"key":"ilari.liusvaara@elisanet.fi","avatar":null},"body":"This helper function copies bidirectional stream of data between\nstdin/stdout and specified file descriptors.\n\nSigned-off-by: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>\n---\n compat/mingw.h     |    5 +\n transport-helper.c |  261 ++++++++++++++++++++++++++++++++++++++++++++++++++++\n transport.h        |    1 +\n 3 files changed, 267 insertions(+), 0 deletions(-)\n\ndiff --git a/compat/mingw.h b/compat/mingw.h\nindex 3b2477b..f27a7b6 100644\n--- a/compat/mingw.h\n+++ b/compat/mingw.h\n@@ -23,6 +23,9 @@ typedef int pid_t;\n #define WEXITSTATUS(x) ((x) & 0xff)\n #define WTERMSIG(x) SIGTERM\n \n+#define EWOULDBLOCK EAGAIN\n+#define SHUT_WR SD_SEND\n+\n #define SIGHUP 1\n #define SIGQUIT 3\n #define SIGKILL 9\n@@ -50,6 +53,8 @@ struct pollfd {\n };\n #define POLLIN 1\n #define POLLHUP 2\n+#define POLLOUT 4\n+#define POLLNVAL 8\n #endif\n \n typedef void (__cdecl *sig_handler_t)(int);\ndiff --git a/transport-helper.c b/transport-helper.c\nindex acfc88e..a5732eb 100644\n--- a/transport-helper.c\n+++ b/transport-helper.c\n@@ -862,3 +862,264 @@ int transport_helper_init(struct transport *transport, const char *name)\n \ttransport->smart_options = &(data->transport_options);\n \treturn 0;\n }\n+\n+/*\n+ * Linux pipes can buffer 65536 bytes at once (and most platforms can\n+ * buffer less), so attempt reads and writes with up to that size.\n+ */\n+#define BUFFERSIZE 65536\n+/* This should be enough to hold debugging message. */\n+#define PBUFFERSIZE 8192\n+\n+/* Print bidirectional transfer loop debug message. */\n+static void transfer_debug(const char *fmt, ...)\n+{\n+\tva_list args;\n+\tchar msgbuf[PBUFFERSIZE];\n+\tstatic int debug_enabled = -1;\n+\n+\tif (debug_enabled < 0)\n+\t\tdebug_enabled = getenv(\"GIT_TRANSLOOP_DEBUG\") ? 1 : 0;\n+\tif (!debug_enabled)\n+\t\treturn;\n+\n+\tva_start(args, fmt);\n+\tvsnprintf(msgbuf, PBUFFERSIZE, fmt, args);\n+\tva_end(args);\n+\tfprintf(stderr, \"Transfer loop debugging: %s\\n\", msgbuf);\n+}\n+\n+/* Stream state: More data may be coming in this direction. */\n+#define SSTATE_TRANSFERING 0\n+/*\n+ * Stream state: No more data coming in this direction, flushing rest of\n+ * data.\n+ */\n+#define SSTATE_FLUSHING 1\n+/* Stream state: Transfer in this direction finished. */\n+#define SSTATE_FINISHED 2\n+\n+#define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)\n+#define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)\n+#define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)\n+\n+/* Unidirectional transfer. */\n+struct unidirectional_transfer {\n+\t/* Source */\n+\tint src;\n+\t/* Destination */\n+\tint dest;\n+\t/* Is destination socket? */\n+\tint dest_is_sock;\n+\t/* Transfer state (TRANSFERING/FLUSHING/FINISHED) */\n+\tint state;\n+\t/* Buffer. */\n+\tchar buf[BUFFERSIZE];\n+\t/* Buffer used. */\n+\tsize_t bufuse;\n+\t/* Name of source. */\n+\tconst char *src_name;\n+\t/* Name of destination. */\n+\tconst char *dest_name;\n+};\n+\n+static void udt_close_if_finished(struct unidirectional_transfer *t)\n+{\n+\tif (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {\n+\t\tt->state = SSTATE_FINISHED;\n+\t\tif (t->dest_is_sock)\n+\t\t\tshutdown(t->dest, SHUT_WR);\n+\t\telse\n+\t\t\tclose(t->dest);\n+\t\ttransfer_debug(\"Closed %s.\", t->dest_name);\n+\t}\n+}\n+\n+static int udt_do_read(struct unidirectional_transfer *t)\n+{\n+\tint r;\n+\ttransfer_debug(\"%s is readable\", t->src_name);\n+\tr = read(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);\n+\tif (r < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&\n+\t\terrno != EINTR) {\n+\t\terror(\"read(%s) failed: %s\", t->src_name, strerror(errno));\n+\t\treturn -1;\n+\t} else if (r == 0) {\n+\t\ttransfer_debug(\"%s EOF (with %i bytes in buffer)\",\n+\t\t\tt->src_name, t->bufuse);\n+\t\tt->state = SSTATE_FLUSHING;\n+\t} else if (r > 0) {\n+\t\tt->bufuse += r;\n+\t\ttransfer_debug(\"Read %i bytes from %s (buffer now at %i)\",\n+\t\t\tr, t->src_name, (int)t->bufuse);\n+\t}\n+\treturn 0;\n+}\n+\n+static int udt_do_write(struct unidirectional_transfer *t)\n+{\n+\tint r;\n+\ttransfer_debug(\"%s is writable\", t->dest_name);\n+\tr = write(t->dest, t->buf, t->bufuse);\n+\tif (r < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&\n+\t\terrno != EINTR) {\n+\t\terror(\"write(%s) failed: %s\", t->dest_name, strerror(errno));\n+\t\treturn -1;\n+\t} else if (r > 0) {\n+\t\tt->bufuse -= r;\n+\t\tif (t->bufuse)\n+\t\t\tmemmove(t->buf, t->buf + r, t->bufuse);\n+\t\ttransfer_debug(\"Wrote %i bytes to %s (buffer now at %i)\",\n+\t\t\tr, t->dest_name, (int)t->bufuse);\n+\t}\n+\treturn 0;\n+}\n+\n+\n+/* State of bidirectional transfer loop. */\n+struct bidirectional_transfer_state {\n+\t/* Direction from program to git. */\n+\tstruct unidirectional_transfer ptg;\n+\t/* Direction from git to program. */\n+\tstruct unidirectional_transfer gtp;\n+};\n+\n+static void *udt_copy_thread_routine(void *udt)\n+{\n+\tstruct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;\n+\twhile(t->state != SSTATE_FINISHED) {\n+\t\tif (STATE_NEEDS_READING(t->state))\n+\t\t\tif (udt_do_read(t))\n+\t\t\t\treturn NULL;\n+\t\tif (STATE_NEEDS_WRITING(t->state) && t->bufuse)\n+\t\t\tif (udt_do_write(t))\n+\t\t\t\treturn NULL;\n+\t\tif (STATE_NEEDS_CLOSING(t->state))\n+\t\t\tudt_close_if_finished(t);\n+\t}\n+\treturn udt;\n+}\n+\n+#ifndef NO_PTHREADS\n+static int transloop_with_threads(struct bidirectional_transfer_state *s)\n+{\n+\tpthread_t gtp_thread;\n+\tpthread_t ptg_thread;\n+\tint err;\n+\tint ret = 0;\n+\tvoid *dummy;\n+\terr = pthread_create(&gtp_thread, NULL, udt_copy_thread_routine,\n+\t\t&s->gtp);\n+\tif (err)\n+\t\tdie(\"Can't start thread for copying data: %s\", strerror(err));\n+\terr = pthread_create(&ptg_thread, NULL, udt_copy_thread_routine,\n+\t\t&s->ptg);\n+\tif (err)\n+\t\tdie(\"Can't start thread for copying data: %s\", strerror(err));\n+\terr = pthread_join(gtp_thread, &dummy);\n+\tif (!dummy) {\n+\t\terror(\"Git to program copy thread failed\");\n+\t\tret = 1;\n+\t}\n+\tif (err) {\n+\t\terror(\"Error joining data copying thread: %s\", strerror(err));\n+\t\tret = 1;\n+\t}\n+\terr = pthread_join(ptg_thread, &dummy);\n+\tif (!dummy) {\n+\t\terror(\"Program to git copy thread failed\");\n+\t\tret = 1;\n+\t}\n+\tif (err) {\n+\t\terror(\"Error joining data copying thread: %s\", strerror(err));\n+\t\tret = 1;\n+\t}\n+\treturn ret;\n+}\n+#else\n+\n+static void udt_kill_transfer(struct unidirectional_transfer *t)\n+{\n+\tt->state = SSTATE_FINISHED;\n+\tclose(t->src);\n+\tif (t->dest_is_sock)\n+\t\tshutdown(t->dest, SHUT_WR);\n+\telse\n+\t\tclose(t->dest);\n+}\n+\n+static int transloop_with_threads(struct bidirectional_transfer_state *s)\n+{\n+\tint tret;\n+\tpid_t pid1, pid2;\n+\tint ret = 0;\n+\tint err;\n+\n+\t/* Fork thread #1: git to program. */\n+\tpid1 = fork();\n+\tif (pid1 < 0)\n+\t\tdie_errno(\"Can't start thread for copying data\");\n+\telse if (pid1 == 0) {\n+\t\tudt_kill_transfer(&s->ptg);\n+\t\texit(udt_copy_thread_routine(&s->gtp) ? 0 : 1);\n+\t}\n+\n+\t/* Fork thread #2: program to git. */\n+\tpid2 = fork();\n+\tif (pid2 < 0)\n+\t\tdie_errno(\"Can't start thread for copying data\");\n+\telse if (pid2 == 0) {\n+\t\tudt_kill_transfer(&s->gtp);\n+\t\texit(udt_copy_thread_routine(&s->ptg) ? 0 : 1);\n+\t}\n+\n+\tudt_kill_transfer(&s->gtp);\n+\tudt_kill_transfer(&s->ptg);\n+\n+\t/* There are two child processes, so wait both of them. */\n+\terr = waitpid(pid1, &tret, 0);\n+\tif (err < 0)\n+\t\terror(\"Error joining data copying thread: %s\", strerror(errno));\n+\tif (err < 0 || !WIFEXITED(tret) || WEXITSTATUS(tret)) {\n+\t\terror(\"Git to program copy thread failed\");\n+\t\tret = 1;\n+\t}\n+\terr = waitpid(pid2, &tret, 0);\n+\tif (err < 0)\n+\t\terror(\"Error joining data copying thread: %s\", strerror(errno));\n+\tif (err < 0 || !WIFEXITED(tret) || WEXITSTATUS(tret)) {\n+\t\terror(\"Program to git copy thread failed\");\n+\t\tret = 1;\n+\t}\n+\treturn ret;\n+}\n+#endif\n+\n+/*\n+ * Copies data from stdin to output and from input to stdout simultaneously.\n+ * Additionally filtering through given filter. If filter is NULL, uses\n+ * identity filter.\n+ */\n+int bidirectional_transfer_loop(int input, int output)\n+{\n+\tstruct bidirectional_transfer_state state;\n+\n+\t/* Fill the state fields. */\n+\tstate.ptg.src = input;\n+\tstate.ptg.dest = 1;\n+\tstate.ptg.dest_is_sock = 0;\n+\tstate.ptg.state = SSTATE_TRANSFERING;\n+\tstate.ptg.bufuse = 0;\n+\tstate.ptg.src_name = \"remote input\";\n+\tstate.ptg.dest_name = \"stdout\";\n+\n+\tstate.gtp.src = 0;\n+\tstate.gtp.dest = output;\n+\tstate.gtp.dest_is_sock = (input == output);\n+\tstate.gtp.state = SSTATE_TRANSFERING;\n+\tstate.gtp.bufuse = 0;\n+\tstate.gtp.src_name = \"stdin\";\n+\tstate.gtp.dest_name = \"remote output\";\n+\n+\treturn transloop_with_threads(&state);\n+}\ndiff --git a/transport.h b/transport.h\nindex c59d973..e803c0e 100644\n--- a/transport.h\n+++ b/transport.h\n@@ -154,6 +154,7 @@ int transport_connect(struct transport *transport, const char *name,\n \n /* Transport methods defined outside transport.c */\n int transport_helper_init(struct transport *transport, const char *name);\n+int bidirectional_transfer_loop(int input, int output);\n \n /* common methods used by transport.c and builtin-send-pack.c */\n void transport_verify_remote_names(int nr_heads, const char **heads);\n-- \n1.7.3.1.53.gb58bd\n"},{"id":"152994","messageId":"1286563200-7270-3-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"25392","inReplyTo":"1286563200-7270-1-git-send-email-ilari.liusvaara@elisanet.fi","subject":"[RFC PATCH v5 2/3] git-remote-fd","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-10-08T18:39:59Z","receivedAt":"2010-10-08T18:39:59Z","isPatch":true,"sender":{"key":"ilari.liusvaara@elisanet.fi","avatar":null},"body":"This remote helper reflects raw smart remote transport stream back to the\ncalling program. This is useful for example if some UI wants to handle\nssh itself and not use hacks via GIT_SSH.\n\nSigned-off-by: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>\n---\n .gitignore                      |    1 +\n Documentation/git-remote-fd.txt |   59 ++++++++++++++++++++++++++++\n Makefile                        |    1 +\n builtin.h                       |    1 +\n builtin/remote-fd.c             |   80 +++++++++++++++++++++++++++++++++++++++\n git.c                           |    1 +\n 6 files changed, 143 insertions(+), 0 deletions(-)\n create mode 100644 Documentation/git-remote-fd.txt\n create mode 100644 builtin/remote-fd.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 20560b8..89f37f4 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -112,6 +112,7 @@\n /git-remote-https\n /git-remote-ftp\n /git-remote-ftps\n+/git-remote-fd\n /git-remote-testgit\n /git-repack\n /git-replace\ndiff --git a/Documentation/git-remote-fd.txt b/Documentation/git-remote-fd.txt\nnew file mode 100644\nindex 0000000..1c1a179\n--- /dev/null\n+++ b/Documentation/git-remote-fd.txt\n@@ -0,0 +1,59 @@\n+git-remote-fd(1)\n+=================\n+\n+NAME\n+----\n+git-remote-fd - Reflect smart transport stream back to caller\n+\n+SYNOPSIS\n+--------\n+\"fd::<infd>[,<outfd>][/<anything>]\" (as URL)\n+\n+DESCRIPTION\n+-----------\n+This helper uses specified file descriptors to connect to remote git server.\n+This is not meant for end users but for programs and scripts calling git\n+fetch, push or archive.\n+\n+If only <infd> is given, it is assumed to be bidirectional socket connected\n+to remote git server (git-upload-pack, git-receive-pack or\n+git-upload-achive). If both <infd> and <outfd> are given, they are assumed\n+to be pipes connected to remote git server (<infd> being the inbound pipe\n+and <outfd> being the outbound pipe.\n+\n+It is assumed that any handshaking procedures have already been completed\n+(such as sending service request for git://) before this helper is started.\n+\n+<anything> can be any string. It is ignored. It is meant for provoding\n+information to user in the URL in case that URL is displayed in some\n+context.\n+\n+ENVIRONMENT VARIABLES:\n+----------------------\n+GIT_TRANSLOOP_DEBUG::\n+\tIf set, prints debugging information about various reads/writes.\n+\n+EXAMPLES:\n+---------\n+git fetch fd::17 master::\n+\tFetch master, using file descriptor #17 to communicate with\n+\tgit-upload-pack.\n+\n+git fetch fd::17/foo master::\n+\tSame as above.\n+\n+git push fd::7,8 master (as URL)::\n+\tPush master, using file descriptor #7 to read data from\n+\tgit-receive-pack and file descriptor #8 to write data to\n+\tsame service.\n+\n+git push fd::7,8/bar master::\n+\tSame as above.\n+\n+Documentation\n+--------------\n+Documentation by Ilari Liusvaara and the git list <git@vger.kernel.org>\n+\n+GIT\n+---\n+Part of the linkgit:git[1] suite\ndiff --git a/Makefile b/Makefile\nindex 8a56b9a..7da54d7 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -728,6 +728,7 @@ BUILTIN_OBJS += builtin/read-tree.o\n BUILTIN_OBJS += builtin/receive-pack.o\n BUILTIN_OBJS += builtin/reflog.o\n BUILTIN_OBJS += builtin/remote.o\n+BUILTIN_OBJS += builtin/remote-fd.o\n BUILTIN_OBJS += builtin/replace.o\n BUILTIN_OBJS += builtin/rerere.o\n BUILTIN_OBJS += builtin/reset.o\ndiff --git a/builtin.h b/builtin.h\nindex f2a25a0..1a816e1 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -108,6 +108,7 @@ extern int cmd_read_tree(int argc, const char **argv, const char *prefix);\n extern int cmd_receive_pack(int argc, const char **argv, const char *prefix);\n extern int cmd_reflog(int argc, const char **argv, const char *prefix);\n extern int cmd_remote(int argc, const char **argv, const char *prefix);\n+extern int cmd_remote_fd(int argc, const char **argv, const char *prefix);\n extern int cmd_config(int argc, const char **argv, const char *prefix);\n extern int cmd_rerere(int argc, const char **argv, const char *prefix);\n extern int cmd_reset(int argc, const char **argv, const char *prefix);\ndiff --git a/builtin/remote-fd.c b/builtin/remote-fd.c\nnew file mode 100644\nindex 0000000..bb7eadc\n--- /dev/null\n+++ b/builtin/remote-fd.c\n@@ -0,0 +1,80 @@\n+#include \"git-compat-util.h\"\n+#include \"transport.h\"\n+\n+/*\n+ * URL syntax:\n+ *\t'fd::<inoutfd>[/<anything>]'\t\tRead/write socket pair\n+ *\t\t\t\t\t\t<inoutfd>.\n+ *\t'fd::<infd>,<outfd>[/<anything>]'\tRead pipe <infd> and write\n+ *\t\t\t\t\t\tpipe <outfd>.\n+ *\t[foo] indicates 'foo' is optional. <anything> is any string.\n+ *\n+ * The data output to <outfd>/<inoutfd> should be passed unmolested to\n+ * git-receive-pack/git-upload-pack/git-upload-archive and output of\n+ * git-receive-pack/git-upload-pack/git-upload-archive should be passed\n+ * unmolested to <infd>/<inoutfd>.\n+ *\n+ */\n+\n+int input_fd = -1;\n+int output_fd = -1;\n+\n+#define MAXCOMMAND 4096\n+\n+static void command_loop(void)\n+{\n+\tchar buffer[MAXCOMMAND];\n+\n+\twhile (1) {\n+\t\tsize_t i;\n+\t\tif (!fgets(buffer, MAXCOMMAND - 1, stdin)) {\n+\t\t\tif (ferror(stdin))\n+\t\t\t\tdie(\"Input error\");\n+\t\t\treturn;\n+\t\t}\n+\t\t/* Strip end of line characters. */\n+\t\ti = strlen(buffer);\n+\t\twhile (isspace(buffer[i - 1]))\n+\t\t\tbuffer[--i] = 0;\n+\n+\t\tif (!strcmp(buffer, \"capabilities\")) {\n+\t\t\tprintf(\"*connect\\n\\n\");\n+\t\t\tfflush(stdout);\n+\t\t} else if (!strncmp(buffer, \"connect \", 8)) {\n+\t\t\tprintf(\"\\n\");\n+\t\t\tfflush(stdout);\n+\t\t\tif (bidirectional_transfer_loop(input_fd,\n+\t\t\t\toutput_fd))\n+\t\t\t\tdie(\"Copying data between file descriptors failed\");\n+\t\t\treturn;\n+\t\t} else {\n+\t\t\tdie(\"Bad command: %s\", buffer);\n+\t\t}\n+\t}\n+}\n+\n+int cmd_remote_fd(int argc, const char **argv, const char *prefix)\n+{\n+\tchar* end;\n+\n+\tif (argc < 3)\n+\t\tdie(\"URL missing\");\n+\n+\tinput_fd = (int)strtoul(argv[2], &end, 10);\n+\n+\tif ((end == argv[2]) || (*end != ',' && *end !='/' && *end))\n+\t\tdie(\"Bad URL syntax\");\n+\n+\tif (*end == '/' || !*end) {\n+\t\toutput_fd = input_fd;\n+\t} else {\n+\t\tchar* end2;\n+\t\toutput_fd = (int)strtoul(end + 1, &end2, 10);\n+\n+\t\tif ((end2 == end + 1) || (*end2 !='/' && *end2))\n+\t\t\tdie(\"Bad URL syntax\");\n+\t}\n+\n+\tcommand_loop();\n+\treturn 0;\n+}\ndiff --git a/git.c b/git.c\nindex 50a1401..b7b96b0 100644\n--- a/git.c\n+++ b/git.c\n@@ -374,6 +374,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"receive-pack\", cmd_receive_pack },\n \t\t{ \"reflog\", cmd_reflog, RUN_SETUP },\n \t\t{ \"remote\", cmd_remote, RUN_SETUP },\n+\t\t{ \"remote-fd\", cmd_remote_fd },\n \t\t{ \"replace\", cmd_replace, RUN_SETUP },\n \t\t{ \"repo-config\", cmd_config, RUN_SETUP_GENTLY },\n \t\t{ \"rerere\", cmd_rerere, RUN_SETUP },\n-- \n1.7.3.1.53.gb58bd\n"},{"id":"152996","messageId":"1286563200-7270-4-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"25392","inReplyTo":"1286563200-7270-1-git-send-email-ilari.liusvaara@elisanet.fi","subject":"[RFC PATCH v5 3/3] git-remote-ext","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-10-08T18:40:00Z","receivedAt":"2010-10-08T18:40:00Z","isPatch":true,"sender":{"key":"ilari.liusvaara@elisanet.fi","avatar":null},"body":"This remote helper invokes external command and passes raw smart transport\nstream through it. This is useful for instance for invoking ssh with\none-off odd options, connecting to git services in unix domain\nsockets, in abstract namespace, using TLS or other secure protocols,\netc...\n\nSigned-off-by: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>\n---\n .gitignore                       |    1 +\n Documentation/git-remote-ext.txt |  125 +++++++++++++++++++\n Makefile                         |    1 +\n builtin.h                        |    1 +\n builtin/remote-ext.c             |  243 ++++++++++++++++++++++++++++++++++++++\n git.c                            |    1 +\n 6 files changed, 372 insertions(+), 0 deletions(-)\n create mode 100644 Documentation/git-remote-ext.txt\n create mode 100644 builtin/remote-ext.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 89f37f4..87b833c 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -113,6 +113,7 @@\n /git-remote-ftp\n /git-remote-ftps\n /git-remote-fd\n+/git-remote-ext\n /git-remote-testgit\n /git-repack\n /git-replace\ndiff --git a/Documentation/git-remote-ext.txt b/Documentation/git-remote-ext.txt\nnew file mode 100644\nindex 0000000..8fb260b\n--- /dev/null\n+++ b/Documentation/git-remote-ext.txt\n@@ -0,0 +1,125 @@\n+git-remote-ext(1)\n+=================\n+\n+NAME\n+----\n+git-remote-ext - Bridge smart transport to external command.\n+\n+SYNOPSIS\n+--------\n+git remote add nick \"ext::<command>[ <arguments>...]\"\n+\n+DESCRIPTION\n+-----------\n+This remote helper uses the specified 'program' to connect\n+to a remote git server.\n+\n+Data written to stdin of this specified 'program' is assumed\n+to be sent to git:// server, git-upload-pack, git-receive-pack\n+or git-upload-archive (depending on situation), and data read\n+from stdout of this program is assumed to be received from\n+the same service.\n+\n+Command and arguments are separated by unescaped space.\n+\n+The following sequences have a special meaning:\n+\n+'% '::\n+\tLiteral space in command or argument.\n+\n+'%%'::\n+\tLiteral percent sign.\n+\n+'%s'::\n+\tReplaced with name (receive-pack, upload-pack, or\n+\tupload-archive) of the service git wants to invoke.\n+\n+'%S'::\n+\tReplaced with long name (git-receive-pack,\n+\tgit-upload-pack, or git-upload-archive) of the service\n+\tgit wants to invoke.\n+\n+'%G<repository>' (as argument)::\n+\tThis argument will not be passed to 'program'. Instead, it\n+\twill cause helper to start by sending git:// service request to\n+\tremote side with service field set to approiate value and\n+\trepository field set to <repository>. Default is not to send\n+\tsuch request.\n++\n+This is useful if remote side is git:// server accessed over\n+some tunnel.\n+\n+'%V<host>' (as argument)::\n+\tThis argument will not be passed to 'program'. Instead it sets\n+\tthe vhost field in git:// service request. Default is not to\n+\tsend vhost in such request (if sent).\n+\n+ENVIRONMENT VARIABLES:\n+----------------------\n+\n+GIT_TRANSLOOP_DEBUG::\n+\tIf set, prints debugging information about various reads/writes.\n+\n+ENVIRONMENT VARIABLES PASSED TO COMMAND:\n+----------------------------------------\n+\n+GIT_EXT_SERVICE::\n+\tSet to long name (git-upload-pack, etc...) of service helper needs\n+\tto invoke.\n+\n+GIT_EXT_SERVICE_NOPREFIX::\n+\tSet to long name (upload-pack, etc...) of service helper needs\n+\tto invoke.\n+\n+\n+EXAMPLES:\n+---------\n+This remote helper is transparently used by git when\n+you use commands such as \"git fetch <URL>\", \"git clone <URL>\",\n+, \"git push <URL>\" or \"git remote add nick <URL>\", where <URL>\n+begins with `ext::`.  Examples:\n+\n+\"ext::ssh -i /home/foo/.ssh/somekey user&#64;host.example %S 'foo/repo'\"::\n+\tLike host.example:foo/repo, but use /home/foo/.ssh/somekey as\n+\tkeypair and user as user on remote side. This avoids needing to\n+\tedit .ssh/config.\n+\n+\"ext::socat -t3600 - ABSTRACT-CONNECT:/git-server %G/somerepo\"::\n+\tRepresents repository with path /somerepo accessable over\n+\tgit protocol at abstract namespace address /git-server.\n+\n+\"ext::git-server-alias foo %G/repo\"::\n+\tRepresents a repository with path /repo accessed using the\n+\thelper program \"git-server-alias foo\".  The path to the\n+\trepository and type of request are not passed on the command\n+\tline but as part of the protocol stream, as usual with git://\n+\tprotocol.\n+\n+\"ext::git-server-alias foo %G/repo %Vfoo\"::\n+\tRepresents a repository with path /repo accessed using the\n+\thelper program \"git-server-alias foo\".  The hostname for the\n+\tremote server passed in the protocol stream will be \"foo\"\n+\t(this allows multiple virtual git servers to share a\n+\tlink-level address).\n+\n+\"ext::git-server-alias foo %G/repo% with% spaces %Vfoo\"::\n+\tRepresents a repository with path '/repo with spaces' accessed\n+\tusing the helper program \"git-server-alias foo\".  The hostname for\n+\tthe remote server passed in the protocol stream will be \"foo\"\n+\t(this allows multiple virtual git servers to share a\n+\tlink-level address).\n+\n+\"ext::git-ssl foo.example /bar\"::\n+\tRepresents a repository accessed using the helper program\n+\t\"git-ssl foo.example /bar\".  The type of request can be\n+\tdetermined by the helper using environment variables (see\n+\tabove).\n+\n+Documentation\n+--------------\n+Documentation by Ilari Liusvaara, Jonathan Nieder and the git list\n+<git@vger.kernel.org>\n+\n+GIT\n+---\n+Part of the linkgit:git[1] suite\ndiff --git a/Makefile b/Makefile\nindex 7da54d7..9909ca1 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -728,6 +728,7 @@ BUILTIN_OBJS += builtin/read-tree.o\n BUILTIN_OBJS += builtin/receive-pack.o\n BUILTIN_OBJS += builtin/reflog.o\n BUILTIN_OBJS += builtin/remote.o\n+BUILTIN_OBJS += builtin/remote-ext.o\n BUILTIN_OBJS += builtin/remote-fd.o\n BUILTIN_OBJS += builtin/replace.o\n BUILTIN_OBJS += builtin/rerere.o\ndiff --git a/builtin.h b/builtin.h\nindex 1a816e1..a4bba61 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -108,6 +108,7 @@ extern int cmd_read_tree(int argc, const char **argv, const char *prefix);\n extern int cmd_receive_pack(int argc, const char **argv, const char *prefix);\n extern int cmd_reflog(int argc, const char **argv, const char *prefix);\n extern int cmd_remote(int argc, const char **argv, const char *prefix);\n+extern int cmd_remote_ext(int argc, const char **argv, const char *prefix);\n extern int cmd_remote_fd(int argc, const char **argv, const char *prefix);\n extern int cmd_config(int argc, const char **argv, const char *prefix);\n extern int cmd_rerere(int argc, const char **argv, const char *prefix);\ndiff --git a/builtin/remote-ext.c b/builtin/remote-ext.c\nnew file mode 100644\nindex 0000000..b2041ff\n--- /dev/null\n+++ b/builtin/remote-ext.c\n@@ -0,0 +1,243 @@\n+#include \"git-compat-util.h\"\n+#include \"transport.h\"\n+#include \"run-command.h\"\n+\n+/*\n+ * URL syntax:\n+ *\t'command [arg1 [arg2 [...]]]'\tInvoke command with given arguments.\n+ *\tSpecial characters:\n+ *\t'% ': Literal space in argument.\n+ *\t'%%': Literal percent sign.\n+ *\t'%S': Name of service (git-upload-pack/git-upload-archive/\n+ *\t\tgit-receive-pack.\n+ *\t'%s': Same as \\s, but with possible git- prefix stripped.\n+ *\t'%G': Only allowed as first 'character' of argument. Do not pass this\n+ *\t\tArgument to command, instead send this as name of repository\n+ *\t\tin in-line git://-style request (also activates sending this\n+ *\t\tstyle of request).\n+ *\t'%V': Only allowed as first 'character' of argument. Used in\n+ *\t\tconjunction with '%G': Do not pass this argument to command,\n+ *\t\tinstead send this as vhost in git://-style request (note: does\n+ *\t\tnot activate sending git:// style request).\n+ */\n+\n+char* git_req = NULL;\n+char* git_req_vhost = NULL;\n+\n+static char *strip_escapes(const char *str, const char *service,\n+\tconst char **next)\n+{\n+\tsize_t rpos = 0;\n+\tint escape = 0;\n+\tchar special = 0;\n+\tsize_t pslen = 0;\n+\tsize_t pSlen = 0;\n+\tsize_t psoff = 0;\n+\tstruct strbuf ret = STRBUF_INIT;\n+\n+\t/* Calculate prefix length for \\s and lengths for \\s and \\S */\n+\tif (!strncmp(service, \"git-\", 4))\n+\t\tpsoff = 4;\n+\tpSlen = strlen(service);\n+\tpslen = pSlen - psoff;\n+\n+\t/* Pass the service to command. */\n+\tsetenv(\"GIT_EXT_SERVICE\", service, 1);\n+\tsetenv(\"GIT_EXT_SERVICE_NOPREFIX\", service + psoff, 1);\n+\n+\t/* Scan the length of argument. */\n+\twhile (str[rpos] && (escape || str[rpos] != ' ')) {\n+\t\tif (escape) {\n+\t\t\tswitch (str[rpos]) {\n+\t\t\tcase ' ':\n+\t\t\tcase '%':\n+\t\t\tcase 's':\n+\t\t\tcase 'S':\n+\t\t\t\tbreak;\n+\t\t\tcase 'G':\n+\t\t\tcase 'V':\n+\t\t\t\tspecial = str[rpos];\n+\t\t\t\tif (rpos == 1)\n+\t\t\t\t\tbreak;\n+\t\t\t\t/* Fall-through to error. */\n+\t\t\tdefault:\n+\t\t\t\tdie(\"Bad remote-ext placeholder '\\\\%c'.\",\n+\t\t\t\t\tstr[rpos]);\n+\t\t\t}\n+\t\t\tescape = 0;\n+\t\t} else\n+\t\t\tescape = (str[rpos] == '%');\n+\t\trpos++;\n+\t}\n+\tif (escape && !str[rpos])\n+\t\tdie(\"remote-ext command has incomplete placeholder\");\n+\t*next = str + rpos;\n+\tif (**next == ' ')\n+\t\t++*next;\t/* Skip over space */\n+\n+\t/*\n+\t * Do the actual placeholder substitution. The string will be short\n+\t * enough not to overflow integers.\n+\t */\n+\trpos = special ? 2 : 0;\t\t/* Skip first 2 bytes in specials. */\n+\tescape = 0;\n+\twhile (str[rpos] && (escape || str[rpos] != ' ')) {\n+\t\tif (escape) {\n+\t\t\tswitch(str[rpos]) {\n+\t\t\tcase ' ':\n+\t\t\tcase '%':\n+\t\t\t\tstrbuf_addch(&ret, str[rpos]);\n+\t\t\t\tbreak;\n+\t\t\tcase 's':\n+\t\t\t\tstrbuf_addstr(&ret, service + psoff);\n+\t\t\t\tbreak;\n+\t\t\tcase 'S':\n+\t\t\t\tstrbuf_addstr(&ret, service);\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tescape = 0;\n+\t\t} else\n+\t\t\tswitch(str[rpos]) {\n+\t\t\tcase '%':\n+\t\t\t\tescape = 1;\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tstrbuf_addch(&ret, str[rpos]);\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\trpos++;\n+\t}\n+\tswitch(special) {\n+\tcase 'G':\n+\t\tgit_req = strbuf_detach(&ret, NULL);\n+\t\treturn NULL;\n+\tcase 'V':\n+\t\tgit_req_vhost = strbuf_detach(&ret, NULL);\n+\t\treturn NULL;\n+\tdefault:\n+\t\treturn strbuf_detach(&ret, NULL);\n+\t}\n+}\n+\n+/* Should be enough... */\n+#define MAXARGUMENTS 256\n+\n+static const char **parse_argv(const char *arg, const char *service)\n+{\n+\tint arguments = 0;\n+\tint i;\n+\tchar** ret;\n+\tchar *(temparray[MAXARGUMENTS + 1]);\n+\n+\twhile (*arg) {\n+\t\tchar* ret;\n+\t\tif (arguments == MAXARGUMENTS)\n+\t\t\tdie(\"remote-ext command has too many arguments\");\n+\t\tret = strip_escapes(arg, service, &arg);\n+\t\tif (ret)\n+\t\t\ttemparray[arguments++] = ret;\n+\t}\n+\n+\tret = xcalloc(arguments + 1, sizeof(char*));\n+\tfor (i = 0; i < arguments; i++)\n+\t\tret[i] = temparray[i];\n+\n+\treturn (const char**)ret;\n+}\n+\n+static void send_git_request(int stdin_fd, const char *serv, const char *repo,\n+\tconst char *vhost)\n+{\n+\tsize_t bufferspace;\n+\tsize_t wpos = 0;\n+\tchar* buffer;\n+\n+\t/*\n+\t * Request needs 12 bytes extra if there is vhost (xxxx \\0host=\\0) and\n+\t * 6 bytes extra (xxxx \\0) if there is no vhost.\n+\t */\n+\tif (vhost)\n+\t\tbufferspace = strlen(serv) + strlen(repo) + strlen(vhost) + 12;\n+\telse\n+\t\tbufferspace = strlen(serv) + strlen(repo) + 6;\n+\n+\tif (bufferspace > 0xFFFF)\n+\t\tdie(\"Request too large to send\");\n+\tbuffer = xmalloc(bufferspace);\n+\n+\t/* Make the packet. */\n+\twpos = sprintf(buffer, \"%04x%s %s%c\", (unsigned)bufferspace,\n+\t\tserv, repo, 0);\n+\n+\t/* Add vhost if any. */\n+\tif (vhost)\n+\t\tsprintf(buffer + wpos, \"host=%s%c\", vhost, 0);\n+\n+\t/* Send the request */\n+\tif (write_in_full(stdin_fd, buffer, bufferspace) < 0)\n+\t\tdie_errno(\"Failed to send request\");\n+\n+\tfree(buffer);\n+}\n+\n+static int run_child(const char *arg, const char *service)\n+{\n+\tint r;\n+\tstruct child_process child;\n+\n+\tmemset(&child, 0, sizeof(child));\n+\tchild.in = -1;\n+\tchild.out = -1;\n+\tchild.err = 0;\n+\tchild.argv = parse_argv(arg, service);\n+\n+\tif (start_command(&child) < 0)\n+\t\tdie(\"Can't run specified command\");\n+\n+\tif (git_req)\n+\t\tsend_git_request(child.in, service, git_req, git_req_vhost);\n+\n+\tr = bidirectional_transfer_loop(child.out, child.in);\n+\tif (!r)\n+\t\tr = finish_command(&child);\n+\telse\n+\t\tfinish_command(&child);\n+\treturn r;\n+}\n+\n+#define MAXCOMMAND 4096\n+\n+static int command_loop(const char *child)\n+{\n+\tchar buffer[MAXCOMMAND];\n+\n+\twhile (1) {\n+\t\tif (!fgets(buffer, MAXCOMMAND - 1, stdin))\n+\t\t\texit(0);\n+\t\t/* Strip end of line characters. */\n+\t\twhile (isspace((unsigned char)buffer[strlen(buffer) - 1]))\n+\t\t\tbuffer[strlen(buffer) - 1] = 0;\n+\n+\t\tif (!strcmp(buffer, \"capabilities\")) {\n+\t\t\tprintf(\"*connect\\n\\n\");\n+\t\t\tfflush(stdout);\n+\t\t} else if (!strncmp(buffer, \"connect \", 8)) {\n+\t\t\tprintf(\"\\n\");\n+\t\t\tfflush(stdout);\n+\t\t\treturn run_child(child, buffer + 8);\n+\t\t} else {\n+\t\t\tfprintf(stderr, \"Bad command\");\n+\t\t\treturn 1;\n+\t\t}\n+\t}\n+}\n+\n+int cmd_remote_ext(int argc, const char **argv, const char *prefix)\n+{\n+\tif (argc < 3) {\n+\t\tfprintf(stderr, \"Error: URL missing\");\n+\t\texit(1);\n+\t}\n+\n+\treturn command_loop(argv[2]);\n+}\ndiff --git a/git.c b/git.c\nindex b7b96b0..e95a1ba 100644\n--- a/git.c\n+++ b/git.c\n@@ -374,6 +374,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"receive-pack\", cmd_receive_pack },\n \t\t{ \"reflog\", cmd_reflog, RUN_SETUP },\n \t\t{ \"remote\", cmd_remote, RUN_SETUP },\n+\t\t{ \"remote-ext\", cmd_remote_ext },\n \t\t{ \"remote-fd\", cmd_remote_fd },\n \t\t{ \"replace\", cmd_replace, RUN_SETUP },\n \t\t{ \"repo-config\", cmd_config, RUN_SETUP_GENTLY },\n-- \n1.7.3.1.53.gb58bd\n"},{"id":"153002","messageId":"7vtykwv4os.fsf@alter.siamese.dyndns.org","threadId":"25392","inReplyTo":"1286563200-7270-1-git-send-email-ilari.liusvaara@elisanet.fi","subject":"Re: [RFC PATCH v5 0/3] git-remote-fd & git-remote-ext","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-10-08T19:56:51Z","receivedAt":"2010-10-08T19:56:51Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Every time I queued this series, I've been fixing up styles without\ncomplaining.  It is boring and in general not very fun to spend time\nfixing style violations, and the worst part of the story is that by the\ntime I am done, I don't have time left for addressing issues in the\nsubstance of the patch or energy and concentration required for handling\npatches from other people.\n\nI think I probably should have complained earlier.  I am assuming that you\nare rewriting and rebasing, so once they are fixed at your end, I will\nhopefully have to re-fix everything over and over again in the future\nround.\n\nCould you fix these up at your end please?\n\nERROR: space required before the open parenthesis '('\n#216: FILE: transport-helper.c:990:\n+\twhile(t->state != SSTATE_FINISHED) {\n\nERROR: \"foo* bar\" should be \"foo *bar\"\n#596: FILE: builtin/remote-fd.c:58:\n+\tchar* end;\n\nERROR: spaces required around that '!=' (ctx:WxV)\n#603: FILE: builtin/remote-fd.c:65:\n+\tif ((end == argv[2]) || (*end != ',' && *end !='/' && *end))\n \t                                             ^\n\nERROR: \"foo* bar\" should be \"foo *bar\"\n#609: FILE: builtin/remote-fd.c:71:\n+\t\tchar* end2;\n\nERROR: spaces required around that '!=' (ctx:WxV)\n#612: FILE: builtin/remote-fd.c:74:\n+\t\tif ((end2 == end + 1) || (*end2 !='/' && *end2))\n \t\t                                ^\n\nERROR: do not initialise globals to 0 or NULL\n#897: FILE: builtin/remote-ext.c:24:\n+char* git_req = NULL;\n\nERROR: \"foo* bar\" should be \"foo *bar\"\n#897: FILE: builtin/remote-ext.c:24:\n+char* git_req = NULL;\n\nERROR: do not initialise globals to 0 or NULL\n#898: FILE: builtin/remote-ext.c:25:\n+char* git_req_vhost = NULL;\n\nERROR: \"foo* bar\" should be \"foo *bar\"\n#898: FILE: builtin/remote-ext.c:25:\n+char* git_req_vhost = NULL;\n\nERROR: space required before the open parenthesis '('\n#959: FILE: builtin/remote-ext.c:86:\n+\t\t\tswitch(str[rpos]) {\n\nERROR: space required before the open parenthesis '('\n#973: FILE: builtin/remote-ext.c:100:\n+\t\t\tswitch(str[rpos]) {\n\nERROR: space required before the open parenthesis '('\n#983: FILE: builtin/remote-ext.c:110:\n+\tswitch(special) {\n\nERROR: \"foo** bar\" should be \"foo **bar\"\n#1002: FILE: builtin/remote-ext.c:129:\n+\tchar** ret;\n\nERROR: \"foo* bar\" should be \"foo *bar\"\n#1006: FILE: builtin/remote-ext.c:133:\n+\t\tchar* ret;\n\nERROR: \"(foo*)\" should be \"(foo *)\"\n#1014: FILE: builtin/remote-ext.c:141:\n+\tret = xcalloc(arguments + 1, sizeof(char*));\n\nERROR: \"(foo**)\" should be \"(foo **)\"\n#1018: FILE: builtin/remote-ext.c:145:\n+\treturn (const char**)ret;\n\nERROR: \"foo* bar\" should be \"foo *bar\"\n#1026: FILE: builtin/remote-ext.c:153:\n+\tchar* buffer;\n\ntotal: 17 errors, 0 warnings, 851 lines checked\n"},{"id":"153006","messageId":"AANLkTik3SC5p2nvPMoj5-MWk0HnJ1WNTtEwPK4L8Hrae@mail.gmail.com","threadId":"25392","inReplyTo":"7vtykwv4os.fsf@alter.siamese.dyndns.org","subject":"Re: [RFC PATCH v5 0/3] git-remote-fd & git-remote-ext","fromName":"Pat Notz","fromEmail":"patnotz@gmail.com","sentAt":"2010-10-08T20:07:22Z","receivedAt":"2010-10-08T20:07:22Z","isPatch":true,"sender":{"key":"patnotz@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45364?v=4"},"body":"On Fri, Oct 8, 2010 at 1:56 PM, Junio C Hamano <gitster@pobox.com> wrote:\n[snip]\n> Could you fix these up at your end please?\n>\n> ERROR: space required before the open parenthesis '('\n> #216: FILE: transport-helper.c:990:\n> +       while(t->state != SSTATE_FINISHED) {\n>\n[snip]\n\n\nPlease forgive my newness. What are you using to do the style check?\nI'd love to run that myself, preferably in a pre-commit hook or\nsomething.  Maybe we can use astyle to help with this too.\n\nThanks ~ Pat\n"}]}