git
1#include "builtin.h"2#include "transport.h"3
4static const char usage_msg[] =5"git remote-fd <remote> <url>";6
7/*
8* URL syntax:
9* 'fd::<inoutfd>[/<anything>]' Read/write socket pair
10* <inoutfd>.
11* 'fd::<infd>,<outfd>[/<anything>]' Read pipe <infd> and write
12* pipe <outfd>.
13* [foo] indicates 'foo' is optional. <anything> is any string.
14*
15* The data output to <outfd>/<inoutfd> should be passed unmolested to
16* git-receive-pack/git-upload-pack/git-upload-archive and output of
17* git-receive-pack/git-upload-pack/git-upload-archive should be passed
18* unmolested to <infd>/<inoutfd>.
19*
20*/
21
22#define MAXCOMMAND 409623
24static void command_loop(int input_fd, int output_fd)25{
26char buffer[MAXCOMMAND];27
28while (1) {29size_t i;30if (!fgets(buffer, MAXCOMMAND - 1, stdin)) {31if (ferror(stdin))32die("Input error");33return;34}35/* Strip end of line characters. */36i = strlen(buffer);37while (i > 0 && isspace(buffer[i - 1]))38buffer[--i] = 0;39
40if (!strcmp(buffer, "capabilities")) {41printf("*connect\n\n");42fflush(stdout);43} else if (starts_with(buffer, "connect ")) {44printf("\n");45fflush(stdout);46if (bidirectional_transfer_loop(input_fd,47output_fd))48die("Copying data between file descriptors failed");49return;50} else {51die("Bad command: %s", buffer);52}53}54}
55
56int cmd_remote_fd(int argc, const char **argv, const char *prefix)57{
58int input_fd = -1;59int output_fd = -1;60char *end;61
62BUG_ON_NON_EMPTY_PREFIX(prefix);63
64if (argc != 3)65usage(usage_msg);66
67input_fd = (int)strtoul(argv[2], &end, 10);68
69if ((end == argv[2]) || (*end != ',' && *end != '/' && *end))70die("Bad URL syntax");71
72if (*end == '/' || !*end) {73output_fd = input_fd;74} else {75char *end2;76output_fd = (int)strtoul(end + 1, &end2, 10);77
78if ((end2 == end + 1) || (*end2 != '/' && *end2))79die("Bad URL syntax");80}81
82command_loop(input_fd, output_fd);83return 0;84}
85