Fix merge-base from getting confused.
[git/dscho.git] / connect.c
bloba910af93d8ccf4305dd7e3400f4602184a7c6810
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include "quote.h"
4 #include <sys/wait.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <arpa/inet.h>
8 #include <netdb.h>
11 * Read all the refs from the other end
13 struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match)
15 *list = NULL;
16 for (;;) {
17 struct ref *ref;
18 unsigned char old_sha1[20];
19 static char buffer[1000];
20 char *name;
21 int len;
23 len = packet_read_line(in, buffer, sizeof(buffer));
24 if (!len)
25 break;
26 if (buffer[len-1] == '\n')
27 buffer[--len] = 0;
29 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
30 die("protocol error: expected sha/ref, got '%s'", buffer);
31 name = buffer + 41;
32 if (nr_match && !path_match(name, nr_match, match))
33 continue;
34 ref = xmalloc(sizeof(*ref) + len - 40);
35 memcpy(ref->old_sha1, old_sha1, 20);
36 memset(ref->new_sha1, 0, 20);
37 memcpy(ref->name, buffer + 41, len - 40);
38 ref->next = NULL;
39 *list = ref;
40 list = &ref->next;
42 return list;
45 int get_ack(int fd, unsigned char *result_sha1)
47 static char line[1000];
48 int len = packet_read_line(fd, line, sizeof(line));
50 if (!len)
51 die("git-fetch-pack: expected ACK/NAK, got EOF");
52 if (line[len-1] == '\n')
53 line[--len] = 0;
54 if (!strcmp(line, "NAK"))
55 return 0;
56 if (!strncmp(line, "ACK ", 3)) {
57 if (!get_sha1_hex(line+4, result_sha1))
58 return 1;
60 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
63 int path_match(const char *path, int nr, char **match)
65 int i;
66 int pathlen = strlen(path);
68 for (i = 0; i < nr; i++) {
69 char *s = match[i];
70 int len = strlen(s);
72 if (!len || len > pathlen)
73 continue;
74 if (memcmp(path + pathlen - len, s, len))
75 continue;
76 if (pathlen > len && path[pathlen - len - 1] != '/')
77 continue;
78 *s = 0;
79 return 1;
81 return 0;
84 enum protocol {
85 PROTO_LOCAL = 1,
86 PROTO_SSH,
87 PROTO_GIT,
90 static enum protocol get_protocol(const char *name)
92 if (!strcmp(name, "ssh"))
93 return PROTO_SSH;
94 if (!strcmp(name, "git"))
95 return PROTO_GIT;
96 die("I don't handle protocol '%s'", name);
99 #define STR_(s) # s
100 #define STR(s) STR_(s)
102 static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
104 int sockfd = -1;
105 char *colon, *end;
106 char *port = STR(DEFAULT_GIT_PORT);
107 struct addrinfo hints, *ai0, *ai;
108 int gai;
110 if (host[0] == '[') {
111 end = strchr(host + 1, ']');
112 if (end) {
113 *end = 0;
114 end++;
115 host++;
116 } else
117 end = host;
118 } else
119 end = host;
120 colon = strchr(end, ':');
122 if (colon) {
123 *colon = 0;
124 port = colon + 1;
127 memset(&hints, 0, sizeof(hints));
128 hints.ai_socktype = SOCK_STREAM;
129 hints.ai_protocol = IPPROTO_TCP;
131 gai = getaddrinfo(host, port, &hints, &ai);
132 if (gai)
133 die("Unable to look up %s (%s)", host, gai_strerror(gai));
135 for (ai0 = ai; ai; ai = ai->ai_next) {
136 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
137 if (sockfd < 0)
138 continue;
139 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
140 close(sockfd);
141 sockfd = -1;
142 continue;
144 break;
147 freeaddrinfo(ai0);
149 if (sockfd < 0)
150 die("unable to connect a socket (%s)", strerror(errno));
152 fd[0] = sockfd;
153 fd[1] = sockfd;
154 packet_write(sockfd, "%s %s\n", prog, path);
155 return 0;
159 * Yeah, yeah, fixme. Need to pass in the heads etc.
161 int git_connect(int fd[2], char *url, const char *prog)
163 char command[1024];
164 char *host, *path;
165 char *colon;
166 int pipefd[2][2];
167 pid_t pid;
168 enum protocol protocol;
170 host = NULL;
171 path = url;
172 colon = strchr(url, ':');
173 protocol = PROTO_LOCAL;
174 if (colon) {
175 *colon = 0;
176 host = url;
177 path = colon+1;
178 protocol = PROTO_SSH;
179 if (!memcmp(path, "//", 2)) {
180 char *slash = strchr(path + 2, '/');
181 if (slash) {
182 int nr = slash - path - 2;
183 memmove(path, path+2, nr);
184 path[nr] = 0;
185 protocol = get_protocol(url);
186 host = path;
187 path = slash;
192 if (protocol == PROTO_GIT)
193 return git_tcp_connect(fd, prog, host, path);
195 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
196 die("unable to create pipe pair for communication");
197 pid = fork();
198 if (!pid) {
199 snprintf(command, sizeof(command), "%s %s", prog,
200 sq_quote(path));
201 dup2(pipefd[1][0], 0);
202 dup2(pipefd[0][1], 1);
203 close(pipefd[0][0]);
204 close(pipefd[0][1]);
205 close(pipefd[1][0]);
206 close(pipefd[1][1]);
207 if (protocol == PROTO_SSH)
208 execlp("ssh", "ssh", host, command, NULL);
209 else
210 execlp("sh", "sh", "-c", command, NULL);
211 die("exec failed");
213 fd[0] = pipefd[0][0];
214 fd[1] = pipefd[1][1];
215 close(pipefd[0][1]);
216 close(pipefd[1][0]);
217 return pid;
220 int finish_connect(pid_t pid)
222 int ret;
224 for (;;) {
225 ret = waitpid(pid, NULL, 0);
226 if (!ret)
227 break;
228 if (errno != EINTR)
229 break;
231 return ret;