1 #include "git-compat-util.h"
6 #include "run-command.h"
8 static char *server_capabilities;
10 static int check_ref(const char *name, int len, unsigned int flags)
15 if (len < 5 || memcmp(name, "refs/", 5))
18 /* Skip the "refs/" part */
22 /* REF_NORMAL means that we don't want the magic fake tag refs */
23 if ((flags & REF_NORMAL) && check_ref_format(name) < 0)
26 /* REF_HEADS means that we want regular branch heads */
27 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
30 /* REF_TAGS means that we want tags */
31 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
34 /* All type bits clear means that we are ok with anything */
35 return !(flags & ~REF_NORMAL);
39 * Read all the refs from the other end
41 struct ref **get_remote_heads(int in, struct ref **list,
42 int nr_match, char **match,
48 unsigned char old_sha1[20];
49 static char buffer[1000];
53 len = packet_read_line(in, buffer, sizeof(buffer));
56 if (buffer[len-1] == '\n')
59 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
60 die("protocol error: expected sha/ref, got '%s'", buffer);
63 name_len = strlen(name);
64 if (len != name_len + 41) {
65 if (server_capabilities)
66 free(server_capabilities);
67 server_capabilities = xstrdup(name + name_len + 1);
70 if (!check_ref(name, name_len, flags))
72 if (nr_match && !path_match(name, nr_match, match))
74 ref = xcalloc(1, sizeof(*ref) + len - 40);
75 hashcpy(ref->old_sha1, old_sha1);
76 memcpy(ref->name, buffer + 41, len - 40);
83 int server_supports(const char *feature)
85 return server_capabilities &&
86 strstr(server_capabilities, feature) != NULL;
89 int get_ack(int fd, unsigned char *result_sha1)
91 static char line[1000];
92 int len = packet_read_line(fd, line, sizeof(line));
95 die("git-fetch-pack: expected ACK/NAK, got EOF");
96 if (line[len-1] == '\n')
98 if (!strcmp(line, "NAK"))
100 if (!prefixcmp(line, "ACK ")) {
101 if (!get_sha1_hex(line+4, result_sha1)) {
102 if (strstr(line+45, "continue"))
107 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
110 int path_match(const char *path, int nr, char **match)
113 int pathlen = strlen(path);
115 for (i = 0; i < nr; i++) {
119 if (!len || len > pathlen)
121 if (memcmp(path + pathlen - len, s, len))
123 if (pathlen > len && path[pathlen - len - 1] != '/')
138 * A:B means fast forward remote B with local A.
139 * +A:B means overwrite remote B with local A.
140 * +A is a shorthand for +A:A.
141 * A is a shorthand for A:A.
142 * :B means delete remote B.
144 static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
147 struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
148 for (i = 0; i < nr_refspec; i++) {
155 ep = strchr(sp, ':');
165 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
169 static int count_refspec_match(const char *pattern,
171 struct ref **matched_ref)
173 int patlen = strlen(pattern);
174 struct ref *matched_weak = NULL;
175 struct ref *matched = NULL;
179 for (weak_match = match = 0; refs; refs = refs->next) {
180 char *name = refs->name;
181 int namelen = strlen(name);
184 if (namelen < patlen ||
185 memcmp(name + namelen - patlen, pattern, patlen))
187 if (namelen != patlen && name[namelen - patlen - 1] != '/')
190 /* A match is "weak" if it is with refs outside
191 * heads or tags, and did not specify the pattern
192 * in full (e.g. "refs/remotes/origin/master") or at
193 * least from the toplevel (e.g. "remotes/origin/master");
194 * otherwise "git push $URL master" would result in
195 * ambiguity between remotes/origin/master and heads/master
196 * at the remote site.
198 if (namelen != patlen &&
199 patlen != namelen - 5 &&
200 prefixcmp(name, "refs/heads/") &&
201 prefixcmp(name, "refs/tags/")) {
202 /* We want to catch the case where only weak
203 * matches are found and there are multiple
204 * matches, and where more than one strong
205 * matches are found, as ambiguous. One
206 * strong match with zero or more weak matches
207 * are acceptable as a unique match.
218 *matched_ref = matched_weak;
222 *matched_ref = matched;
227 static void link_dst_tail(struct ref *ref, struct ref ***tail)
234 static struct ref *try_explicit_object_name(const char *name)
236 unsigned char sha1[20];
241 ref = xcalloc(1, sizeof(*ref) + 20);
242 strcpy(ref->name, "(delete)");
243 hashclr(ref->new_sha1);
246 if (get_sha1(name, sha1))
248 len = strlen(name) + 1;
249 ref = xcalloc(1, sizeof(*ref) + len);
250 memcpy(ref->name, name, len);
251 hashcpy(ref->new_sha1, sha1);
255 static int match_explicit_refs(struct ref *src, struct ref *dst,
256 struct ref ***dst_tail, struct refspec *rs)
259 for (i = errs = 0; rs[i].src; i++) {
260 struct ref *matched_src, *matched_dst;
262 matched_src = matched_dst = NULL;
263 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
267 /* The source could be in the get_sha1() format
268 * not a reference name. :refs/other is a
269 * way to delete 'other' ref at the remote end.
271 matched_src = try_explicit_object_name(rs[i].src);
275 error("src refspec %s does not match any.",
280 error("src refspec %s matches more than one.",
284 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
288 if (!memcmp(rs[i].dst, "refs/", 5)) {
289 int len = strlen(rs[i].dst) + 1;
290 matched_dst = xcalloc(1, sizeof(*dst) + len);
291 memcpy(matched_dst->name, rs[i].dst, len);
292 link_dst_tail(matched_dst, dst_tail);
294 else if (!strcmp(rs[i].src, rs[i].dst) &&
296 /* pushing "master:master" when
297 * remote does not have master yet.
299 int len = strlen(matched_src->name) + 1;
300 matched_dst = xcalloc(1, sizeof(*dst) + len);
301 memcpy(matched_dst->name, matched_src->name,
303 link_dst_tail(matched_dst, dst_tail);
307 error("dst refspec %s does not match any "
308 "existing ref on the remote and does "
309 "not start with refs/.", rs[i].dst);
314 error("dst refspec %s matches more than one.",
320 if (matched_dst->peer_ref) {
322 error("dst ref %s receives from more than one src.",
326 matched_dst->peer_ref = matched_src;
327 matched_dst->force = rs[i].force;
333 static struct ref *find_ref_by_name(struct ref *list, const char *name)
335 for ( ; list; list = list->next)
336 if (!strcmp(list->name, name))
341 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
342 int nr_refspec, char **refspec, int all)
344 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
347 return match_explicit_refs(src, dst, dst_tail, rs);
349 /* pick the remainder */
350 for ( ; src; src = src->next) {
351 struct ref *dst_peer;
354 dst_peer = find_ref_by_name(dst, src->name);
355 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
358 /* Create a new one and link it */
359 int len = strlen(src->name) + 1;
360 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
361 memcpy(dst_peer->name, src->name, len);
362 hashcpy(dst_peer->new_sha1, src->new_sha1);
363 link_dst_tail(dst_peer, dst_tail);
365 dst_peer->peer_ref = src;
376 static enum protocol get_protocol(const char *name)
378 if (!strcmp(name, "ssh"))
380 if (!strcmp(name, "git"))
382 if (!strcmp(name, "git+ssh"))
384 if (!strcmp(name, "ssh+git"))
386 die("I don't handle protocol '%s'", name);
390 #define STR(s) STR_(s)
395 * Returns a connected socket() fd, or else die()s.
397 static int git_tcp_connect_sock(char *host)
399 int sockfd = -1, saved_errno = 0;
401 const char *port = STR(DEFAULT_GIT_PORT);
402 struct addrinfo hints, *ai0, *ai;
405 if (host[0] == '[') {
406 end = strchr(host + 1, ']');
415 colon = strchr(end, ':');
424 memset(&hints, 0, sizeof(hints));
425 hints.ai_socktype = SOCK_STREAM;
426 hints.ai_protocol = IPPROTO_TCP;
428 gai = getaddrinfo(host, port, &hints, &ai);
430 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
432 for (ai0 = ai; ai; ai = ai->ai_next) {
433 sockfd = socket(ai->ai_family,
434 ai->ai_socktype, ai->ai_protocol);
439 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
451 die("unable to connect a socket (%s)", strerror(saved_errno));
459 * Returns a connected socket() fd, or else die()s.
461 static int git_tcp_connect_sock(char *host)
463 int sockfd = -1, saved_errno = 0;
465 char *port = STR(DEFAULT_GIT_PORT), *ep;
467 struct sockaddr_in sa;
471 if (host[0] == '[') {
472 end = strchr(host + 1, ']');
481 colon = strchr(end, ':');
488 he = gethostbyname(host);
490 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
491 nport = strtoul(port, &ep, 10);
492 if ( ep == port || *ep ) {
494 struct servent *se = getservbyname(port,"tcp");
496 die("Unknown port %s\n", port);
500 for (ap = he->h_addr_list; *ap; ap++) {
501 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
507 memset(&sa, 0, sizeof sa);
508 sa.sin_family = he->h_addrtype;
509 sa.sin_port = htons(nport);
510 memcpy(&sa.sin_addr, *ap, he->h_length);
512 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
522 die("unable to connect a socket (%s)", strerror(saved_errno));
530 static void git_tcp_connect(int fd[2], char *host)
532 int sockfd = git_tcp_connect_sock(host);
539 static char *git_proxy_command;
540 static const char *rhost_name;
541 static int rhost_len;
543 static int git_proxy_command_options(const char *var, const char *value)
545 if (!strcmp(var, "core.gitproxy")) {
550 if (git_proxy_command)
553 * ;# matches www.kernel.org as well
554 * gitproxy = netcatter-1 for kernel.org
555 * gitproxy = netcatter-2 for sample.xz
556 * gitproxy = netcatter-default
558 for_pos = strstr(value, " for ");
560 /* matches everybody */
561 matchlen = strlen(value);
563 hostlen = strlen(for_pos + 5);
564 if (rhost_len < hostlen)
566 else if (!strncmp(for_pos + 5,
567 rhost_name + rhost_len - hostlen,
569 ((rhost_len == hostlen) ||
570 rhost_name[rhost_len - hostlen -1] == '.'))
571 matchlen = for_pos - value;
576 /* core.gitproxy = none for kernel.org */
578 !memcmp(value, "none", 4))
580 git_proxy_command = xmalloc(matchlen + 1);
581 memcpy(git_proxy_command, value, matchlen);
582 git_proxy_command[matchlen] = 0;
587 return git_default_config(var, value);
590 static int git_use_proxy(const char *host)
593 rhost_len = strlen(host);
594 git_proxy_command = getenv("GIT_PROXY_COMMAND");
595 git_config(git_proxy_command_options);
597 return (git_proxy_command && *git_proxy_command);
600 static void git_proxy_connect(int fd[2], char *host)
602 const char *port = STR(DEFAULT_GIT_PORT);
605 struct child_process proxy;
607 if (host[0] == '[') {
608 end = strchr(host + 1, ']');
617 colon = strchr(end, ':');
624 argv[0] = git_proxy_command;
628 memset(&proxy, 0, sizeof(proxy));
632 if (start_command(&proxy))
633 die("cannot start proxy %s", argv[0]);
634 fd[0] = proxy.out; /* read from proxy stdout */
635 fd[1] = proxy.in; /* write to proxy stdin */
638 #define MAX_CMD_LEN 1024
641 * This returns 0 if the transport protocol does not need fork(2),
642 * or a process id if it does. Once done, finish the connection
643 * with finish_connect() with the value returned from this function
644 * (it is safe to call finish_connect() with 0 to support the former
647 * Does not return a negative value on error; it just dies.
649 pid_t git_connect(int fd[2], char *url, const char *prog)
651 char *host, *path = url;
656 enum protocol protocol = PROTO_LOCAL;
659 /* Without this we cannot rely on waitpid() to tell
660 * what happened to our children.
662 signal(SIGCHLD, SIG_DFL);
664 host = strstr(url, "://");
667 protocol = get_protocol(url);
675 if (host[0] == '[') {
676 end = strchr(host + 1, ']');
686 path = strchr(end, c);
689 protocol = PROTO_SSH;
696 die("No path specified. See 'man git-pull' for valid url syntax");
699 * null-terminate hostname and point path to ~ for URL's like this:
700 * ssh://host.xz/~user/repo
702 if (protocol != PROTO_LOCAL && host != url) {
714 if (protocol == PROTO_GIT) {
715 /* These underlying connection commands die() if they
718 char *target_host = xstrdup(host);
719 if (git_use_proxy(host))
720 git_proxy_connect(fd, host);
722 git_tcp_connect(fd, host);
724 * Separate original protocol components prog and path
725 * from extended components with a NUL byte.
737 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
738 die("unable to create pipe pair for communication");
741 die("unable to fork");
743 char command[MAX_CMD_LEN];
744 char *posn = command;
745 int size = MAX_CMD_LEN;
748 of |= add_to_string(&posn, &size, prog, 0);
749 of |= add_to_string(&posn, &size, " ", 0);
750 of |= add_to_string(&posn, &size, path, 1);
753 die("command line too long");
755 dup2(pipefd[1][0], 0);
756 dup2(pipefd[0][1], 1);
761 if (protocol == PROTO_SSH) {
762 const char *ssh, *ssh_basename;
763 ssh = getenv("GIT_SSH");
764 if (!ssh) ssh = "ssh";
765 ssh_basename = strrchr(ssh, '/');
770 execlp(ssh, ssh_basename, host, command, NULL);
773 unsetenv(ALTERNATE_DB_ENVIRONMENT);
774 unsetenv(DB_ENVIRONMENT);
775 unsetenv(GIT_DIR_ENVIRONMENT);
776 unsetenv(GRAFT_ENVIRONMENT);
777 unsetenv(INDEX_ENVIRONMENT);
778 execlp("sh", "sh", "-c", command, NULL);
782 fd[0] = pipefd[0][0];
783 fd[1] = pipefd[1][1];
791 int finish_connect(pid_t pid)
796 while (waitpid(pid, NULL, 0) < 0) {