1 #include "git-compat-util.h"
7 static char *server_capabilities;
9 static int check_ref(const char *name, int len, unsigned int flags)
14 if (len < 5 || memcmp(name, "refs/", 5))
17 /* Skip the "refs/" part */
21 /* REF_NORMAL means that we don't want the magic fake tag refs */
22 if ((flags & REF_NORMAL) && check_ref_format(name) < 0)
25 /* REF_HEADS means that we want regular branch heads */
26 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
29 /* REF_TAGS means that we want tags */
30 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
33 /* All type bits clear means that we are ok with anything */
34 return !(flags & ~REF_NORMAL);
38 * Read all the refs from the other end
40 struct ref **get_remote_heads(int in, struct ref **list,
41 int nr_match, char **match,
47 unsigned char old_sha1[20];
48 static char buffer[1000];
52 len = packet_read_line(in, buffer, sizeof(buffer));
55 if (buffer[len-1] == '\n')
58 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
59 die("protocol error: expected sha/ref, got '%s'", buffer);
62 name_len = strlen(name);
63 if (len != name_len + 41) {
64 if (server_capabilities)
65 free(server_capabilities);
66 server_capabilities = xstrdup(name + name_len + 1);
69 if (!check_ref(name, name_len, flags))
71 if (nr_match && !path_match(name, nr_match, match))
73 ref = xcalloc(1, sizeof(*ref) + len - 40);
74 hashcpy(ref->old_sha1, old_sha1);
75 memcpy(ref->name, buffer + 41, len - 40);
82 int server_supports(const char *feature)
84 return server_capabilities &&
85 strstr(server_capabilities, feature) != NULL;
88 int get_ack(int fd, unsigned char *result_sha1)
90 static char line[1000];
91 int len = packet_read_line(fd, line, sizeof(line));
94 die("git-fetch-pack: expected ACK/NAK, got EOF");
95 if (line[len-1] == '\n')
97 if (!strcmp(line, "NAK"))
99 if (!strncmp(line, "ACK ", 4)) {
100 if (!get_sha1_hex(line+4, result_sha1)) {
101 if (strstr(line+45, "continue"))
106 die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
109 int path_match(const char *path, int nr, char **match)
112 int pathlen = strlen(path);
114 for (i = 0; i < nr; i++) {
118 if (!len || len > pathlen)
120 if (memcmp(path + pathlen - len, s, len))
122 if (pathlen > len && path[pathlen - len - 1] != '/')
137 * A:B means fast forward remote B with local A.
138 * +A:B means overwrite remote B with local A.
139 * +A is a shorthand for +A:A.
140 * A is a shorthand for A:A.
141 * :B means delete remote B.
143 static struct refspec *parse_ref_spec(int nr_refspec, char **refspec)
146 struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1));
147 for (i = 0; i < nr_refspec; i++) {
154 ep = strchr(sp, ':');
164 rs[nr_refspec].src = rs[nr_refspec].dst = NULL;
168 static int count_refspec_match(const char *pattern,
170 struct ref **matched_ref)
172 int patlen = strlen(pattern);
173 struct ref *matched_weak = NULL;
174 struct ref *matched = NULL;
178 for (weak_match = match = 0; refs; refs = refs->next) {
179 char *name = refs->name;
180 int namelen = strlen(name);
183 if (namelen < patlen ||
184 memcmp(name + namelen - patlen, pattern, patlen))
186 if (namelen != patlen && name[namelen - patlen - 1] != '/')
189 /* A match is "weak" if it is with refs outside
190 * heads or tags, and did not specify the pattern
191 * in full (e.g. "refs/remotes/origin/master") or at
192 * least from the toplevel (e.g. "remotes/origin/master");
193 * otherwise "git push $URL master" would result in
194 * ambiguity between remotes/origin/master and heads/master
195 * at the remote site.
197 if (namelen != patlen &&
198 patlen != namelen - 5 &&
199 strncmp(name, "refs/heads/", 11) &&
200 strncmp(name, "refs/tags/", 10)) {
201 /* We want to catch the case where only weak
202 * matches are found and there are multiple
203 * matches, and where more than one strong
204 * matches are found, as ambiguous. One
205 * strong match with zero or more weak matches
206 * are acceptable as a unique match.
217 *matched_ref = matched_weak;
221 *matched_ref = matched;
226 static void link_dst_tail(struct ref *ref, struct ref ***tail)
233 static struct ref *try_explicit_object_name(const char *name)
235 unsigned char sha1[20];
240 ref = xcalloc(1, sizeof(*ref) + 20);
241 strcpy(ref->name, "(delete)");
242 hashclr(ref->new_sha1);
245 if (get_sha1(name, sha1))
247 len = strlen(name) + 1;
248 ref = xcalloc(1, sizeof(*ref) + len);
249 memcpy(ref->name, name, len);
250 hashcpy(ref->new_sha1, sha1);
254 static int match_explicit_refs(struct ref *src, struct ref *dst,
255 struct ref ***dst_tail, struct refspec *rs)
258 for (i = errs = 0; rs[i].src; i++) {
259 struct ref *matched_src, *matched_dst;
261 matched_src = matched_dst = NULL;
262 switch (count_refspec_match(rs[i].src, src, &matched_src)) {
266 /* The source could be in the get_sha1() format
267 * not a reference name. :refs/other is a
268 * way to delete 'other' ref at the remote end.
270 matched_src = try_explicit_object_name(rs[i].src);
274 error("src refspec %s does not match any.",
279 error("src refspec %s matches more than one.",
283 switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) {
287 if (!memcmp(rs[i].dst, "refs/", 5)) {
288 int len = strlen(rs[i].dst) + 1;
289 matched_dst = xcalloc(1, sizeof(*dst) + len);
290 memcpy(matched_dst->name, rs[i].dst, len);
291 link_dst_tail(matched_dst, dst_tail);
293 else if (!strcmp(rs[i].src, rs[i].dst) &&
295 /* pushing "master:master" when
296 * remote does not have master yet.
298 int len = strlen(matched_src->name) + 1;
299 matched_dst = xcalloc(1, sizeof(*dst) + len);
300 memcpy(matched_dst->name, matched_src->name,
302 link_dst_tail(matched_dst, dst_tail);
306 error("dst refspec %s does not match any "
307 "existing ref on the remote and does "
308 "not start with refs/.", rs[i].dst);
313 error("dst refspec %s matches more than one.",
319 if (matched_dst->peer_ref) {
321 error("dst ref %s receives from more than one src.",
325 matched_dst->peer_ref = matched_src;
326 matched_dst->force = rs[i].force;
332 static struct ref *find_ref_by_name(struct ref *list, const char *name)
334 for ( ; list; list = list->next)
335 if (!strcmp(list->name, name))
340 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
341 int nr_refspec, char **refspec, int all)
343 struct refspec *rs = parse_ref_spec(nr_refspec, refspec);
346 return match_explicit_refs(src, dst, dst_tail, rs);
348 /* pick the remainder */
349 for ( ; src; src = src->next) {
350 struct ref *dst_peer;
353 dst_peer = find_ref_by_name(dst, src->name);
354 if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all))
357 /* Create a new one and link it */
358 int len = strlen(src->name) + 1;
359 dst_peer = xcalloc(1, sizeof(*dst_peer) + len);
360 memcpy(dst_peer->name, src->name, len);
361 hashcpy(dst_peer->new_sha1, src->new_sha1);
362 link_dst_tail(dst_peer, dst_tail);
364 dst_peer->peer_ref = src;
375 static enum protocol get_protocol(const char *name)
377 if (!strcmp(name, "ssh"))
379 if (!strcmp(name, "git"))
381 if (!strcmp(name, "git+ssh"))
383 if (!strcmp(name, "ssh+git"))
385 die("I don't handle protocol '%s'", name);
389 #define STR(s) STR_(s)
394 * Returns a connected socket() fd, or else die()s.
396 static int git_tcp_connect_sock(char *host)
398 int sockfd = -1, saved_errno = 0;
400 const char *port = STR(DEFAULT_GIT_PORT);
401 struct addrinfo hints, *ai0, *ai;
404 if (host[0] == '[') {
405 end = strchr(host + 1, ']');
414 colon = strchr(end, ':');
421 memset(&hints, 0, sizeof(hints));
422 hints.ai_socktype = SOCK_STREAM;
423 hints.ai_protocol = IPPROTO_TCP;
425 gai = getaddrinfo(host, port, &hints, &ai);
427 die("Unable to look up %s (%s)", host, gai_strerror(gai));
429 for (ai0 = ai; ai; ai = ai->ai_next) {
430 sockfd = socket(ai->ai_family,
431 ai->ai_socktype, ai->ai_protocol);
436 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
448 die("unable to connect a socket (%s)", strerror(saved_errno));
456 * Returns a connected socket() fd, or else die()s.
458 static int git_tcp_connect_sock(char *host)
460 int sockfd = -1, saved_errno = 0;
462 char *port = STR(DEFAULT_GIT_PORT), *ep;
464 struct sockaddr_in sa;
468 if (host[0] == '[') {
469 end = strchr(host + 1, ']');
478 colon = strchr(end, ':');
485 he = gethostbyname(host);
487 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
488 nport = strtoul(port, &ep, 10);
489 if ( ep == port || *ep ) {
491 struct servent *se = getservbyname(port,"tcp");
493 die("Unknown port %s\n", port);
497 for (ap = he->h_addr_list; *ap; ap++) {
498 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
504 memset(&sa, 0, sizeof sa);
505 sa.sin_family = he->h_addrtype;
506 sa.sin_port = htons(nport);
507 memcpy(&sa.sin_addr, *ap, he->h_length);
509 if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
519 die("unable to connect a socket (%s)", strerror(saved_errno));
527 static void git_tcp_connect(int fd[2], char *host)
529 int sockfd = git_tcp_connect_sock(host);
536 static char *git_proxy_command;
537 static const char *rhost_name;
538 static int rhost_len;
540 static int git_proxy_command_options(const char *var, const char *value)
542 if (!strcmp(var, "core.gitproxy")) {
547 if (git_proxy_command)
550 * ;# matches www.kernel.org as well
551 * gitproxy = netcatter-1 for kernel.org
552 * gitproxy = netcatter-2 for sample.xz
553 * gitproxy = netcatter-default
555 for_pos = strstr(value, " for ");
557 /* matches everybody */
558 matchlen = strlen(value);
560 hostlen = strlen(for_pos + 5);
561 if (rhost_len < hostlen)
563 else if (!strncmp(for_pos + 5,
564 rhost_name + rhost_len - hostlen,
566 ((rhost_len == hostlen) ||
567 rhost_name[rhost_len - hostlen -1] == '.'))
568 matchlen = for_pos - value;
573 /* core.gitproxy = none for kernel.org */
575 !memcmp(value, "none", 4))
577 git_proxy_command = xmalloc(matchlen + 1);
578 memcpy(git_proxy_command, value, matchlen);
579 git_proxy_command[matchlen] = 0;
584 return git_default_config(var, value);
587 static int git_use_proxy(const char *host)
590 rhost_len = strlen(host);
591 git_proxy_command = getenv("GIT_PROXY_COMMAND");
592 git_config(git_proxy_command_options);
594 return (git_proxy_command && *git_proxy_command);
597 static void git_proxy_connect(int fd[2], char *host)
599 const char *port = STR(DEFAULT_GIT_PORT);
604 if (host[0] == '[') {
605 end = strchr(host + 1, ']');
614 colon = strchr(end, ':');
621 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
622 die("unable to create pipe pair for communication");
625 dup2(pipefd[1][0], 0);
626 dup2(pipefd[0][1], 1);
631 execlp(git_proxy_command, git_proxy_command, host, port, NULL);
636 fd[0] = pipefd[0][0];
637 fd[1] = pipefd[1][1];
642 #define MAX_CMD_LEN 1024
645 * This returns 0 if the transport protocol does not need fork(2),
646 * or a process id if it does. Once done, finish the connection
647 * with finish_connect() with the value returned from this function
648 * (it is safe to call finish_connect() with 0 to support the former
651 * Does not return a negative value on error; it just dies.
653 pid_t git_connect(int fd[2], char *url, const char *prog)
655 char *host, *path = url;
660 enum protocol protocol = PROTO_LOCAL;
663 /* Without this we cannot rely on waitpid() to tell
664 * what happened to our children.
666 signal(SIGCHLD, SIG_DFL);
668 host = strstr(url, "://");
671 protocol = get_protocol(url);
679 if (host[0] == '[') {
680 end = strchr(host + 1, ']');
690 path = strchr(end, c);
693 protocol = PROTO_SSH;
700 die("No path specified. See 'man git-pull' for valid url syntax");
703 * null-terminate hostname and point path to ~ for URL's like this:
704 * ssh://host.xz/~user/repo
706 if (protocol != PROTO_LOCAL && host != url) {
718 if (protocol == PROTO_GIT) {
719 /* These underlying connection commands die() if they
722 char *target_host = xstrdup(host);
723 if (git_use_proxy(host))
724 git_proxy_connect(fd, host);
726 git_tcp_connect(fd, host);
728 * Separate original protocol components prog and path
729 * from extended components with a NUL byte.
741 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
742 die("unable to create pipe pair for communication");
745 die("unable to fork");
747 char command[MAX_CMD_LEN];
748 char *posn = command;
749 int size = MAX_CMD_LEN;
752 of |= add_to_string(&posn, &size, prog, 0);
753 of |= add_to_string(&posn, &size, " ", 0);
754 of |= add_to_string(&posn, &size, path, 1);
757 die("command line too long");
759 dup2(pipefd[1][0], 0);
760 dup2(pipefd[0][1], 1);
765 if (protocol == PROTO_SSH) {
766 const char *ssh, *ssh_basename;
767 ssh = getenv("GIT_SSH");
768 if (!ssh) ssh = "ssh";
769 ssh_basename = strrchr(ssh, '/');
774 execlp(ssh, ssh_basename, host, command, NULL);
777 unsetenv(ALTERNATE_DB_ENVIRONMENT);
778 unsetenv(DB_ENVIRONMENT);
779 unsetenv(GIT_DIR_ENVIRONMENT);
780 unsetenv(GRAFT_ENVIRONMENT);
781 unsetenv(INDEX_ENVIRONMENT);
782 execlp("sh", "sh", "-c", command, NULL);
786 fd[0] = pipefd[0][0];
787 fd[1] = pipefd[1][1];
795 int finish_connect(pid_t pid)
800 while (waitpid(pid, NULL, 0) < 0) {