{"thread":{"id":"25430","subject":"[RFC PATCH v7 0/3] git-remote-fd & git-remote-ext","startedAt":"2010-10-12T16:39:40Z","lastAt":"2011-01-16T03:49:40Z","messageCount":7,"participants":["Ilari Liusvaara","Junio C Hamano","Jonathan Nieder"],"isPatch":true,"patchVersion":7,"patchTotal":3},"messages":[{"id":"153327","messageId":"1286901583-30088-1-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"25430","inReplyTo":null,"subject":"[RFC PATCH v7 0/3] git-remote-fd & git-remote-ext","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-10-12T16:39:40Z","receivedAt":"2010-10-12T16:39:40Z","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* Some misc cleanups.\n* Fix bug in transfer loop that manifests when remote end is socket and\ntasks are implemented as processes.\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             |  248 ++++++++++++++++++++++++++++++\n builtin/remote-fd.c              |   79 ++++++++++\n compat/mingw.h                   |    5 +\n git.c                            |    2 +\n transport-helper.c               |  311 ++++++++++++++++++++++++++++++++++++++\n transport.h                      |    1 +\n 11 files changed, 836 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.g597b29.dirty\n"},{"id":"153328","messageId":"1286901583-30088-2-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"25430","inReplyTo":"1286901583-30088-1-git-send-email-ilari.liusvaara@elisanet.fi","subject":"[RFC PATCH v7 1/3] Add bidirectional_transfer_loop()","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-10-12T16:39:41Z","receivedAt":"2010-10-12T16:39:41Z","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 |  311 ++++++++++++++++++++++++++++++++++++++++++++++++++++\n transport.h        |    1 +\n 3 files changed, 317 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..0c094d6 100644\n--- a/transport-helper.c\n+++ b/transport-helper.c\n@@ -862,3 +862,314 @@ 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 source socket? */\n+\tint src_is_sock;\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+/* Closes the target (for writing) if transfer has finished. */\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+/*\n+ * Tries to read read data from source into buffer. If buffer is full,\n+ * no data is read. Returns 0 on success, -1 on error.\n+ */\n+static int udt_do_read(struct unidirectional_transfer *t)\n+{\n+\tssize_t bytes;\n+\n+\tif (t->bufuse == BUFFERSIZE)\n+\t\treturn 0;\t/* No space for more. */\n+\n+\ttransfer_debug(\"%s is readable\", t->src_name);\n+\tbytes = read(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);\n+\tif (bytes < 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 (bytes == 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 (bytes > 0) {\n+\t\tt->bufuse += bytes;\n+\t\ttransfer_debug(\"Read %i bytes from %s (buffer now at %i)\",\n+\t\t\t(int)bytes, t->src_name, (int)t->bufuse);\n+\t}\n+\treturn 0;\n+}\n+\n+/* Tries to write data from buffer into destination. If buffer is empty,\n+ * no data is written. Returns 0 on success, -1 on error.\n+ */\n+static int udt_do_write(struct unidirectional_transfer *t)\n+{\n+\tsize_t bytes;\n+\n+\tif (t->bufuse == 0)\n+\t\treturn 0;\t/* Nothing to write. */\n+\n+\ttransfer_debug(\"%s is writable\", t->dest_name);\n+\tbytes = write(t->dest, t->buf, t->bufuse);\n+\tif (bytes < 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 (bytes > 0) {\n+\t\tt->bufuse -= bytes;\n+\t\tif (t->bufuse)\n+\t\t\tmemmove(t->buf, t->buf + bytes, t->bufuse);\n+\t\ttransfer_debug(\"Wrote %i bytes to %s (buffer now at %i)\",\n+\t\t\t(int)bytes, 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_task_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))\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;\t/* Just some non-NULL value. */\n+}\n+\n+#ifndef NO_PTHREADS\n+\n+/*\n+ * Join thread, with apporiate errors on failure. Name is name for the\n+ * thread (for error messages). Returns 0 on success, 1 on failure.\n+ */\n+static int tloop_join(pthread_t thread, const char *name)\n+{\n+\tint err;\n+\tvoid *tret;\n+\terr = pthread_join(thread, &tret);\n+\tif (!tret) {\n+\t\terror(\"%s thread failed\", name);\n+\t\treturn 1;\n+\t}\n+\tif (err) {\n+\t\terror(\"%s thread failed to join: %s\", name, strerror(err));\n+\t\treturn 1;\n+\t}\n+\treturn 0;\n+}\n+\n+/*\n+ * Spawn the transfer tasks and then wait for them. Returns 0 on success,\n+ * -1 on failure.\n+ */\n+static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)\n+{\n+\tpthread_t gtp_thread;\n+\tpthread_t ptg_thread;\n+\tint err;\n+\tint ret = 0;\n+\terr = pthread_create(&gtp_thread, NULL, udt_copy_task_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_task_routine,\n+\t\t&s->ptg);\n+\tif (err)\n+\t\tdie(\"Can't start thread for copying data: %s\", strerror(err));\n+\n+\tret |= tloop_join(gtp_thread, \"Git to program copy\");\n+\tret |= tloop_join(ptg_thread, \"Program to git copy\");\n+\treturn ret;\n+}\n+#else\n+\n+/* Close the source and target (for writing) for transfer. */\n+static void udt_kill_transfer(struct unidirectional_transfer *t)\n+{\n+\tt->state = SSTATE_FINISHED;\n+\t/*\n+\t * Socket read end left open isn't a disaster if nobody\n+\t * attempts to read from it (mingw compat headers do not\n+\t * have SHUT_RD)...\n+\t *\n+\t * We can't fully close the socket since otherwise gtp\n+\t * task would first close the socket it sends data to\n+\t * while closing the ptg file descriptors.\n+\t */\n+\tif (!t->src_is_sock)\n+\t\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+/*\n+ * Join process, with apporiate errors on failure. Name is name for the\n+ * process (for error messages). Returns 0 on success, 1 on failure.\n+ */\n+static int tloop_join(pid_t pid, const char *name)\n+{\n+\tint tret;\n+\tif (waitpid(pid, &tret, 0) < 0) {\n+\t\terror(\"%s process failed to wait: %s\", name, strerror(errno));\n+\t\treturn 1;\n+\t}\n+\tif (!WIFEXITED(tret) || WEXITSTATUS(tret)) {\n+\t\terror(\"%s process failed\", name);\n+\t\treturn 1;\n+\t}\n+\treturn 0;\n+}\n+\n+/*\n+ * Spawn the transfer tasks and then wait for them. Returns 0 on success,\n+ * -1 on failure.\n+ */\n+static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)\n+{\n+\tpid_t pid1, pid2;\n+\tint ret = 0;\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_task_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_task_routine(&s->ptg) ? 0 : 1);\n+\t}\n+\n+\t/*\n+\t * Close both streams in parent as to not interfere with\n+\t * end of file detection and wait for both tasks to finish.\n+\t */\n+\tudt_kill_transfer(&s->gtp);\n+\tudt_kill_transfer(&s->ptg);\n+\tret |= tloop_join(pid1, \"Git to program copy\");\n+\tret |= tloop_join(pid2, \"Program to git copy\");\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.src_is_sock = (input == output);\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.src_is_sock = 0;\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 tloop_spawnwait_tasks(&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.g597b29.dirty\n"},{"id":"153329","messageId":"1286901583-30088-3-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"25430","inReplyTo":"1286901583-30088-1-git-send-email-ilari.liusvaara@elisanet.fi","subject":"[RFC PATCH v7 2/3] git-remote-fd","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-10-12T16:39:42Z","receivedAt":"2010-10-12T16:39:42Z","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             |   79 +++++++++++++++++++++++++++++++++++++++\n git.c                           |    1 +\n 6 files changed, 142 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..7517f24\n--- /dev/null\n+++ b/builtin/remote-fd.c\n@@ -0,0 +1,79 @@\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+#define MAXCOMMAND 4096\n+\n+static void command_loop(int input_fd, int output_fd)\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+\tint input_fd = -1;\n+\tint output_fd = -1;\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(input_fd, output_fd);\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.g597b29.dirty\n"},{"id":"153330","messageId":"1286901583-30088-4-git-send-email-ilari.liusvaara@elisanet.fi","threadId":"25430","inReplyTo":"1286901583-30088-1-git-send-email-ilari.liusvaara@elisanet.fi","subject":"[RFC PATCH v7 3/3] git-remote-ext","fromName":"Ilari Liusvaara","fromEmail":"ilari.liusvaara@elisanet.fi","sentAt":"2010-10-12T16:39:43Z","receivedAt":"2010-10-12T16:39:43Z","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             |  248 ++++++++++++++++++++++++++++++++++++++\n git.c                            |    1 +\n 6 files changed, 377 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..1d3f1cf\n--- /dev/null\n+++ b/builtin/remote-ext.c\n@@ -0,0 +1,248 @@\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+static char *git_req;\n+static char *git_req_vhost;\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+\tconst char **ret;\n+\tchar *temparray[MAXARGUMENTS + 1];\n+\n+\twhile (*arg) {\n+\t\tchar *expanded;\n+\t\tif (arguments == MAXARGUMENTS)\n+\t\t\tdie(\"remote-ext command has too many arguments\");\n+\t\texpanded = strip_escapes(arg, service, &arg);\n+\t\tif (expanded)\n+\t\t\ttemparray[arguments++] = expanded;\n+\t}\n+\n+\tret = xmalloc((arguments + 1) * sizeof(char *));\n+\tfor (i = 0; i < arguments; i++)\n+\t\tret[i] = temparray[i];\n+\tret[arguments] = NULL;\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\tsize_t length;\n+\t\tif (!fgets(buffer, MAXCOMMAND - 1, stdin)) {\n+\t\t\tif (ferror(stdin))\n+\t\t\t\tdie(\"Comammand input error\");\n+\t\t\texit(0);\n+\t\t}\n+\t\t/* Strip end of line characters. */\n+\t\tlength = strlen(buffer);\n+\t\twhile (isspace((unsigned char)buffer[length - 1]))\n+\t\t\tbuffer[--length] = 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.g597b29.dirty\n"},{"id":"153473","messageId":"7v7hhlhdnt.fsf@alter.siamese.dyndns.org","threadId":"25430","inReplyTo":"1286901583-30088-4-git-send-email-ilari.liusvaara@elisanet.fi","subject":"Re: [RFC PATCH v7 3/3] git-remote-ext","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-10-13T23:32:38Z","receivedAt":"2010-10-13T23:32:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ilari Liusvaara <ilari.liusvaara@elisanet.fi> writes:\n\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\nThe above does not say anything about being the first 'character' in an\nargument, like the in-code comment does.  Is that intended?\n\n> +static const char **parse_argv(const char *arg, const char *service)\n> +{\n> +\tint arguments = 0;\n> +\tint i;\n> +\tconst char **ret;\n> +\tchar *temparray[MAXARGUMENTS + 1];\n> +\n> +\twhile (*arg) {\n> +\t\tchar *expanded;\n> +\t\tif (arguments == MAXARGUMENTS)\n> +\t\t\tdie(\"remote-ext command has too many arguments\");\n> +\t\texpanded = strip_escapes(arg, service, &arg);\n> +\t\tif (expanded)\n> +\t\t\ttemparray[arguments++] = expanded;\n> +\t}\n> +\n> +\tret = xmalloc((arguments + 1) * sizeof(char *));\n> +\tfor (i = 0; i < arguments; i++)\n> +\t\tret[i] = temparray[i];\n> +\tret[arguments] = NULL;\n> +\treturn (const char **)ret;\n> +}\n\nIsn't \"ret\" declared to be of that type?\n"},{"id":"153472","messageId":"7vzkuhfz1i.fsf@alter.siamese.dyndns.org","threadId":"25430","inReplyTo":"1286901583-30088-3-git-send-email-ilari.liusvaara@elisanet.fi","subject":"Re: [RFC PATCH v7 2/3] git-remote-fd","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-10-13T23:33:45Z","receivedAt":"2010-10-13T23:33:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ilari Liusvaara <ilari.liusvaara@elisanet.fi> writes:\n\n> diff --git a/Documentation/git-remote-fd.txt b/Documentation/git-remote-fd.txt\n> new file mode 100644\n> index 0000000..1c1a179\n> --- /dev/null\n> +++ b/Documentation/git-remote-fd.txt\n> @@ -0,0 +1,59 @@\n> +git-remote-fd(1)\n> +=================\n\nIt appears to me that it has one equal sign too many.  Does this format\ncorrectly?\n\n> +ENVIRONMENT VARIABLES:\n> +----------------------\n\nI didn't follow the discussion closely but I recall somebody saying that\nthe final colon is out of place, and I think I agree.\n\n> +EXAMPLES:\n> +---------\n\nLikewise.\n\n> diff --git a/builtin/remote-fd.c b/builtin/remote-fd.c\n> new file mode 100644\n> index 0000000..7517f24\n> --- /dev/null\n> +++ b/builtin/remote-fd.c\n> @@ -0,0 +1,79 @@\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> +#define MAXCOMMAND 4096\n> +\n> +static void command_loop(int input_fd, int output_fd)\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\nHopefully you won't get a line with all space, going beyond the beginning\nof the buffer?\n\n> +int cmd_remote_fd(int argc, const char **argv, const char *prefix)\n> +{\n> +\tint input_fd = -1;\n> +\tint output_fd = -1;\n> +\tchar *end;\n> +\n> +\tif (argc < 3)\n> +\t\tdie(\"URL missing\");\n\nIs it Ok to have excess arguments to be ignored?\n"},{"id":"159542","messageId":"20110116034940.GA29072@burratino","threadId":"25430","inReplyTo":"1286901583-30088-4-git-send-email-ilari.liusvaara@elisanet.fi","subject":"[PATCH] remote-ext: do not segfault for blank lines","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2011-01-16T03:49:40Z","receivedAt":"2011-01-16T03:49:40Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Instead of stripping space characters past the beginning of the\nline and overflowing a buffer, stop at the beginning of the line\n(mimicking the corresponding fix in remote-fd).\n\nThe argument to isspace does not need to be cast explicitly because\ngit isspace takes care of that already.\n\nNoticed-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Jonathan Nieder <jrnieder@gmail.com>\n---\n builtin/remote-ext.c |    8 ++++----\n 1 files changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/remote-ext.c b/builtin/remote-ext.c\nindex 1f77317..ea71977 100644\n--- a/builtin/remote-ext.c\n+++ b/builtin/remote-ext.c\n@@ -212,16 +212,16 @@ static int command_loop(const char *child)\n \tchar buffer[MAXCOMMAND];\n \n \twhile (1) {\n-\t\tsize_t length;\n+\t\tsize_t i;\n \t\tif (!fgets(buffer, MAXCOMMAND - 1, stdin)) {\n \t\t\tif (ferror(stdin))\n \t\t\t\tdie(\"Comammand input error\");\n \t\t\texit(0);\n \t\t}\n \t\t/* Strip end of line characters. */\n-\t\tlength = strlen(buffer);\n-\t\twhile (isspace((unsigned char)buffer[length - 1]))\n-\t\t\tbuffer[--length] = 0;\n+\t\ti = strlen(buffer);\n+\t\twhile (i > 0 && 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-- \n1.7.4.rc2\n"}]}