Add "git branch" script
[git.git] / rsh.c
blobfe87e581d21d0465da9e3fadef0454c69640caa7
1 #include "rsh.h"
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
7 #include "cache.h"
9 #define COMMAND_SIZE 4096
11 int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
12 char *url, int rmt_argc, char **rmt_argv)
14 char *host;
15 char *path;
16 int sv[2];
17 char command[COMMAND_SIZE];
18 char *posn;
19 int i;
21 if (!strcmp(url, "-")) {
22 *fd_in = 0;
23 *fd_out = 1;
24 return 0;
27 host = strstr(url, "//");
28 if (host) {
29 host += 2;
30 path = strchr(host, '/');
31 } else {
32 host = url;
33 path = strchr(host, ':');
34 if (path)
35 *(path++) = '\0';
37 if (!path) {
38 return error("Bad URL: %s", url);
40 /* ssh <host> 'cd <path>; stdio-pull <arg...> <commit-id>' */
41 snprintf(command, COMMAND_SIZE,
42 "%s='%s' %s",
43 GIT_DIR_ENVIRONMENT, path, remote_prog);
44 *path = '\0';
45 posn = command + strlen(command);
46 for (i = 0; i < rmt_argc; i++) {
47 *(posn++) = ' ';
48 strncpy(posn, rmt_argv[i], COMMAND_SIZE - (posn - command));
49 posn += strlen(rmt_argv[i]);
50 if (posn - command + 4 >= COMMAND_SIZE) {
51 return error("Command line too long");
54 strcpy(posn, " -");
55 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) {
56 return error("Couldn't create socket");
58 if (!fork()) {
59 close(sv[1]);
60 dup2(sv[0], 0);
61 dup2(sv[0], 1);
62 execlp("ssh", "ssh", host, command, NULL);
64 close(sv[0]);
65 *fd_in = sv[1];
66 *fd_out = sv[1];
67 return 0;