1 #include "git-compat-util.h"
7 #include <sys/socket.h>
8 #include <netinet/in.h>
12 static char *server_capabilities = NULL;
15 * Read all the refs from the other end
17 struct ref **get_remote_heads(int in, struct ref **list,
18 int nr_match, char **match, int ignore_funny)
23 unsigned char old_sha1[20];
24 static char buffer[1000];
28 len = packet_read_line(in, buffer, sizeof(buffer));
31 if (buffer[len-1] == '\n')
34 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
35 die("protocol error: expected sha/ref, got '%s'", buffer);
38 name_len = strlen(name);
39 if (len != name_len + 41) {
40 if (server_capabilities)
41 free(server_capabilities);
42 server_capabilities = strdup(name + name_len + 1);
45 if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) &&
46 check_ref_format(name + 5))
49 if (nr_match && !path_match(name, nr_match, match))
51 ref = xcalloc(1, sizeof(*ref) + len - 40);
52 memcpy(ref->old_sha1, old_sha1, 20);
53 memcpy(ref->name, buffer + 41, len - 40);
60 int server_supports(const char *feature)
62 return server_capabilities &&
63 strstr(server_capabilities, feature) != NULL;
66 int get_ack(int fd, unsigned char *result_sha1)
68 static char line[1000];
69 int len = packet_read_line(fd, line, sizeof(line));
72 die("git-fetch-pack: expected ACK/NAK, got EOF");
73 if (line[len-1] == '\n')
75 if (!strcmp(line, "NAK"))
77 if (!strncmp(line, "ACK ", 3)) {
78 if (!get_sha1_hex(line+4, result_sha1)) {
79 if (strstr(line+45, "continue"))
84 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
87 int path_match(const char *path, int nr, char **match)
90 int pathlen = strlen(path);
92 for (i = 0; i < nr; i++) {
96 if (!len || len > pathlen)
98 if (memcmp(path + pathlen - len, s, len))
100 if (pathlen > len && path[pathlen - len - 1] != '/')
115 * A:B means fast forward remote B with local A.
116 * +A:B means overwrite remote B with local A.
117 * +A is a shorthand for +A:A.
118 * A is a shorthand for A:A.
120 static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
123 struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
124 for (i = 0; i < nr_refspec; i++) {
131 ep = strchr(sp, ':');
141 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
145 static int count_refspec_match(const char *pattern,
147 struct ref **matched_ref)
150 int patlen = strlen(pattern);
152 for (match = 0; refs; refs = refs->next) {
153 char *name = refs->name;
154 int namelen = strlen(name);
155 if (namelen < patlen ||
156 memcmp(name + namelen - patlen, pattern, patlen))
158 if (namelen != patlen && name[namelen - patlen - 1] != '/')
166 static void link_dst_tail(struct ref *ref, struct ref ***tail)
173 static struct ref *try_explicit_object_name(const char *name)
175 unsigned char sha1[20];
178 if (get_sha1(name, sha1))
180 len = strlen(name) + 1;
181 ref = xcalloc(1, sizeof(*ref) + len);
182 memcpy(ref->name, name, len);
183 memcpy(ref->new_sha1, sha1, 20);
187 static int match_explicit_refs(struct ref *src, struct ref *dst,
188 struct ref ***dst_tail, struct refspec *rs)
191 for (i = errs = 0; rs[i].src; i++) {
192 struct ref *matched_src, *matched_dst;
194 matched_src = matched_dst = NULL;
195 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
199 /* The source could be in the get_sha1() format
200 * not a reference name.
202 matched_src = try_explicit_object_name(rs[i].src);
206 error("src refspec %s does not match any.",
211 error("src refspec %s matches more than one.",
215 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
219 if (!memcmp(rs[i].dst, "refs/", 5)) {
220 int len = strlen(rs[i].dst) + 1;
221 matched_dst = xcalloc(1, sizeof(*dst) + len);
222 memcpy(matched_dst->name, rs[i].dst, len);
223 link_dst_tail(matched_dst, dst_tail);
225 else if (!strcmp(rs[i].src, rs[i].dst) &&
227 /* pushing "master:master" when
228 * remote does not have master yet.
230 int len = strlen(matched_src->name) + 1;
231 matched_dst = xcalloc(1, sizeof(*dst) + len);
232 memcpy(matched_dst->name, matched_src->name,
234 link_dst_tail(matched_dst, dst_tail);
238 error("dst refspec %s does not match any "
239 "existing ref on the remote and does "
240 "not start with refs/.", rs[i].dst);
245 error("dst refspec %s matches more than one.",
251 if (matched_dst->peer_ref) {
253 error("dst ref %s receives from more than one src.",
257 matched_dst->peer_ref = matched_src;
258 matched_dst->force = rs[i].force;
264 static struct ref *find_ref_by_name(struct ref *list, const char *name)
266 for ( ; list; list = list->next)
267 if (!strcmp(list->name, name))
272 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
273 int nr_refspec, char **refspec, int all)
275 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
278 return match_explicit_refs(src, dst, dst_tail, rs);
280 /* pick the remainder */
281 for ( ; src; src = src->next) {
282 struct ref *dst_peer;
285 dst_peer = find_ref_by_name(dst, src->name);
286 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
289 /* Create a new one and link it */
290 int len = strlen(src->name) + 1;
291 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
292 memcpy(dst_peer->name, src->name, len);
293 memcpy(dst_peer->new_sha1, src->new_sha1, 20);
294 link_dst_tail(dst_peer, dst_tail);
296 dst_peer->peer_ref = src;
307 static enum protocol get_protocol(const char *name)
309 if (!strcmp(name, "ssh"))
311 if (!strcmp(name, "git"))
313 if (!strcmp(name, "git+ssh"))
315 if (!strcmp(name, "ssh+git"))
317 die("I don't handle protocol '%s'", name);
321 #define STR(s) STR_(s)
325 static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
329 char *port = STR(DEFAULT_GIT_PORT);
330 struct addrinfo hints, *ai0, *ai;
333 if (host[0] == '[') {
334 end = strchr(host + 1, ']');
343 colon = strchr(end, ':');
350 memset(&hints, 0, sizeof(hints));
351 hints.ai_socktype = SOCK_STREAM;
352 hints.ai_protocol = IPPROTO_TCP;
354 gai = getaddrinfo(host, port, &hints, &ai);
356 die("Unable to look up %s (%s)", host, gai_strerror(gai));
358 for (ai0 = ai; ai; ai = ai->ai_next) {
359 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
362 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
373 die("unable to connect a socket (%s)", strerror(errno));
377 packet_write(sockfd, "%s %s\n", prog, path);
383 static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
387 char *port = STR(DEFAULT_GIT_PORT), *ep;
389 struct sockaddr_in sa;
393 if (host[0] == '[') {
394 end = strchr(host + 1, ']');
403 colon = strchr(end, ':');
411 he = gethostbyname(host);
413 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
414 nport = strtoul(port, &ep, 10);
415 if ( ep == port || *ep ) {
417 struct servent *se = getservbyname(port,"tcp");
419 die("Unknown port %s\n", port);
423 for (ap = he->h_addr_list; *ap; ap++) {
424 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
428 memset(&sa, 0, sizeof sa);
429 sa.sin_family = he->h_addrtype;
430 sa.sin_port = htons(nport);
431 memcpy(&sa.sin_addr, *ap, he->h_length);
433 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
442 die("unable to connect a socket (%s)", strerror(errno));
446 packet_write(sockfd, "%s %s\n", prog, path);
452 static char *git_proxy_command = NULL;
453 static const char *rhost_name = NULL;
454 static int rhost_len;
456 static int git_proxy_command_options(const char *var, const char *value)
458 if (!strcmp(var, "core.gitproxy")) {
463 if (git_proxy_command)
466 * ;# matches www.kernel.org as well
467 * gitproxy = netcatter-1 for kernel.org
468 * gitproxy = netcatter-2 for sample.xz
469 * gitproxy = netcatter-default
471 for_pos = strstr(value, " for ");
473 /* matches everybody */
474 matchlen = strlen(value);
476 hostlen = strlen(for_pos + 5);
477 if (rhost_len < hostlen)
479 else if (!strncmp(for_pos + 5,
480 rhost_name + rhost_len - hostlen,
482 ((rhost_len == hostlen) ||
483 rhost_name[rhost_len - hostlen -1] == '.'))
484 matchlen = for_pos - value;
489 /* core.gitproxy = none for kernel.org */
491 !memcmp(value, "none", 4))
493 git_proxy_command = xmalloc(matchlen + 1);
494 memcpy(git_proxy_command, value, matchlen);
495 git_proxy_command[matchlen] = 0;
500 return git_default_config(var, value);
503 static int git_use_proxy(const char *host)
506 rhost_len = strlen(host);
507 git_proxy_command = getenv("GIT_PROXY_COMMAND");
508 git_config(git_proxy_command_options);
510 return (git_proxy_command && *git_proxy_command);
513 static int git_proxy_connect(int fd[2], const char *prog, char *host, char *path)
515 char *port = STR(DEFAULT_GIT_PORT);
520 if (host[0] == '[') {
521 end = strchr(host + 1, ']');
530 colon = strchr(end, ':');
537 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
538 die("unable to create pipe pair for communication");
541 dup2(pipefd[1][0], 0);
542 dup2(pipefd[0][1], 1);
547 execlp(git_proxy_command, git_proxy_command, host, port, NULL);
550 fd[0] = pipefd[0][0];
551 fd[1] = pipefd[1][1];
554 packet_write(fd[1], "%s %s\n", prog, path);
559 * Yeah, yeah, fixme. Need to pass in the heads etc.
561 int git_connect(int fd[2], char *url, const char *prog)
564 char *host, *path = url;
569 enum protocol protocol = PROTO_LOCAL;
571 host = strstr(url, "://");
574 protocol = get_protocol(url);
582 if (host[0] == '[') {
583 end = strchr(host + 1, ']');
593 path = strchr(end, c);
596 protocol = PROTO_SSH;
603 die("No path specified. See 'man git-pull' for valid url syntax");
606 * null-terminate hostname and point path to ~ for URL's like this:
607 * ssh://host.xz/~user/repo
609 if (protocol != PROTO_LOCAL && host != url) {
619 if (protocol == PROTO_GIT) {
620 if (git_use_proxy(host))
621 return git_proxy_connect(fd, prog, host, path);
622 return git_tcp_connect(fd, prog, host, path);
625 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
626 die("unable to create pipe pair for communication");
629 snprintf(command, sizeof(command), "%s %s", prog,
631 dup2(pipefd[1][0], 0);
632 dup2(pipefd[0][1], 1);
637 if (protocol == PROTO_SSH) {
638 const char *ssh, *ssh_basename;
639 ssh = getenv("GIT_SSH");
640 if (!ssh) ssh = "ssh";
641 ssh_basename = strrchr(ssh, '/');
646 execlp(ssh, ssh_basename, host, command, NULL);
649 unsetenv(ALTERNATE_DB_ENVIRONMENT);
650 unsetenv(DB_ENVIRONMENT);
651 unsetenv(GIT_DIR_ENVIRONMENT);
652 unsetenv(GRAFT_ENVIRONMENT);
653 unsetenv(INDEX_ENVIRONMENT);
654 execlp("sh", "sh", "-c", command, NULL);
658 fd[0] = pipefd[0][0];
659 fd[1] = pipefd[1][1];
665 int finish_connect(pid_t pid)
670 ret = waitpid(pid, NULL, 0);