1 #include "git-compat-util.h"
7 #include <sys/socket.h>
8 #include <netinet/in.h>
13 static char *server_capabilities
;
15 static int check_ref(const char *name
, int len
, unsigned int flags
)
20 if (len
< 5 || memcmp(name
, "refs/", 5))
23 /* Skip the "refs/" part */
27 /* REF_NORMAL means that we don't want the magic fake tag refs */
28 if ((flags
& REF_NORMAL
) && check_ref_format(name
) < 0)
31 /* REF_HEADS means that we want regular branch heads */
32 if ((flags
& REF_HEADS
) && !memcmp(name
, "heads/", 6))
35 /* REF_TAGS means that we want tags */
36 if ((flags
& REF_TAGS
) && !memcmp(name
, "tags/", 5))
39 /* All type bits clear means that we are ok with anything */
40 return !(flags
& ~REF_NORMAL
);
44 * Read all the refs from the other end
46 struct ref
**get_remote_heads(int in
, struct ref
**list
,
47 int nr_match
, char **match
,
53 unsigned char old_sha1
[20];
54 static char buffer
[1000];
58 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
61 if (buffer
[len
-1] == '\n')
64 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
65 die("protocol error: expected sha/ref, got '%s'", buffer
);
68 name_len
= strlen(name
);
69 if (len
!= name_len
+ 41) {
70 if (server_capabilities
)
71 free(server_capabilities
);
72 server_capabilities
= xstrdup(name
+ name_len
+ 1);
75 if (!check_ref(name
, name_len
, flags
))
77 if (nr_match
&& !path_match(name
, nr_match
, match
))
79 ref
= xcalloc(1, sizeof(*ref
) + len
- 40);
80 hashcpy(ref
->old_sha1
, old_sha1
);
81 memcpy(ref
->name
, buffer
+ 41, len
- 40);
88 int server_supports(const char *feature
)
90 return server_capabilities
&&
91 strstr(server_capabilities
, feature
) != NULL
;
94 int get_ack(int fd
, unsigned char *result_sha1
)
96 static char line
[1000];
97 int len
= packet_read_line(fd
, line
, sizeof(line
));
100 die("git-fetch-pack: expected ACK/NAK, got EOF");
101 if (line
[len
-1] == '\n')
103 if (!strcmp(line
, "NAK"))
105 if (!strncmp(line
, "ACK ", 4)) {
106 if (!get_sha1_hex(line
+4, result_sha1
)) {
107 if (strstr(line
+45, "continue"))
112 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
115 int path_match(const char *path
, int nr
, char **match
)
118 int pathlen
= strlen(path
);
120 for (i
= 0; i
< nr
; i
++) {
124 if (!len
|| len
> pathlen
)
126 if (memcmp(path
+ pathlen
- len
, s
, len
))
128 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
143 * A:B means fast forward remote B with local A.
144 * +A:B means overwrite remote B with local A.
145 * +A is a shorthand for +A:A.
146 * A is a shorthand for A:A.
147 * :B means delete remote B.
149 static struct refspec
*parse_ref_spec(int nr_refspec
, char **refspec
)
152 struct refspec
*rs
= xcalloc(sizeof(*rs
), (nr_refspec
+ 1));
153 for (i
= 0; i
< nr_refspec
; i
++) {
160 ep
= strchr(sp
, ':');
170 rs
[nr_refspec
].src
= rs
[nr_refspec
].dst
= NULL
;
174 static int count_refspec_match(const char *pattern
,
176 struct ref
**matched_ref
)
178 int patlen
= strlen(pattern
);
179 struct ref
*matched_weak
= NULL
;
180 struct ref
*matched
= NULL
;
184 for (weak_match
= match
= 0; refs
; refs
= refs
->next
) {
185 char *name
= refs
->name
;
186 int namelen
= strlen(name
);
189 if (namelen
< patlen
||
190 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
192 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
195 /* A match is "weak" if it is with refs outside
196 * heads or tags, and did not specify the pattern
197 * in full (e.g. "refs/remotes/origin/master") or at
198 * least from the toplevel (e.g. "remotes/origin/master");
199 * otherwise "git push $URL master" would result in
200 * ambiguity between remotes/origin/master and heads/master
201 * at the remote site.
203 if (namelen
!= patlen
&&
204 patlen
!= namelen
- 5 &&
205 strncmp(name
, "refs/heads/", 11) &&
206 strncmp(name
, "refs/tags/", 10)) {
207 /* We want to catch the case where only weak
208 * matches are found and there are multiple
209 * matches, and where more than one strong
210 * matches are found, as ambiguous. One
211 * strong match with zero or more weak matches
212 * are acceptable as a unique match.
223 *matched_ref
= matched_weak
;
227 *matched_ref
= matched
;
232 static void link_dst_tail(struct ref
*ref
, struct ref
***tail
)
239 static struct ref
*try_explicit_object_name(const char *name
)
241 unsigned char sha1
[20];
246 ref
= xcalloc(1, sizeof(*ref
) + 20);
247 strcpy(ref
->name
, "(delete)");
248 hashclr(ref
->new_sha1
);
251 if (get_sha1(name
, sha1
))
253 len
= strlen(name
) + 1;
254 ref
= xcalloc(1, sizeof(*ref
) + len
);
255 memcpy(ref
->name
, name
, len
);
256 hashcpy(ref
->new_sha1
, sha1
);
260 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
261 struct ref
***dst_tail
, struct refspec
*rs
)
264 for (i
= errs
= 0; rs
[i
].src
; i
++) {
265 struct ref
*matched_src
, *matched_dst
;
267 matched_src
= matched_dst
= NULL
;
268 switch (count_refspec_match(rs
[i
].src
, src
, &matched_src
)) {
272 /* The source could be in the get_sha1() format
273 * not a reference name. :refs/other is a
274 * way to delete 'other' ref at the remote end.
276 matched_src
= try_explicit_object_name(rs
[i
].src
);
280 error("src refspec %s does not match any.",
285 error("src refspec %s matches more than one.",
289 switch (count_refspec_match(rs
[i
].dst
, dst
, &matched_dst
)) {
293 if (!memcmp(rs
[i
].dst
, "refs/", 5)) {
294 int len
= strlen(rs
[i
].dst
) + 1;
295 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
296 memcpy(matched_dst
->name
, rs
[i
].dst
, len
);
297 link_dst_tail(matched_dst
, dst_tail
);
299 else if (!strcmp(rs
[i
].src
, rs
[i
].dst
) &&
301 /* pushing "master:master" when
302 * remote does not have master yet.
304 int len
= strlen(matched_src
->name
) + 1;
305 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
306 memcpy(matched_dst
->name
, matched_src
->name
,
308 link_dst_tail(matched_dst
, dst_tail
);
312 error("dst refspec %s does not match any "
313 "existing ref on the remote and does "
314 "not start with refs/.", rs
[i
].dst
);
319 error("dst refspec %s matches more than one.",
325 if (matched_dst
->peer_ref
) {
327 error("dst ref %s receives from more than one src.",
331 matched_dst
->peer_ref
= matched_src
;
332 matched_dst
->force
= rs
[i
].force
;
338 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
340 for ( ; list
; list
= list
->next
)
341 if (!strcmp(list
->name
, name
))
346 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
347 int nr_refspec
, char **refspec
, int all
)
349 struct refspec
*rs
= parse_ref_spec(nr_refspec
, refspec
);
352 return match_explicit_refs(src
, dst
, dst_tail
, rs
);
354 /* pick the remainder */
355 for ( ; src
; src
= src
->next
) {
356 struct ref
*dst_peer
;
359 dst_peer
= find_ref_by_name(dst
, src
->name
);
360 if ((dst_peer
&& dst_peer
->peer_ref
) || (!dst_peer
&& !all
))
363 /* Create a new one and link it */
364 int len
= strlen(src
->name
) + 1;
365 dst_peer
= xcalloc(1, sizeof(*dst_peer
) + len
);
366 memcpy(dst_peer
->name
, src
->name
, len
);
367 hashcpy(dst_peer
->new_sha1
, src
->new_sha1
);
368 link_dst_tail(dst_peer
, dst_tail
);
370 dst_peer
->peer_ref
= src
;
381 static enum protocol
get_protocol(const char *name
)
383 if (!strcmp(name
, "ssh"))
385 if (!strcmp(name
, "git"))
387 if (!strcmp(name
, "git+ssh"))
389 if (!strcmp(name
, "ssh+git"))
391 die("I don't handle protocol '%s'", name
);
395 #define STR(s) STR_(s)
400 * Returns a connected socket() fd, or else die()s.
402 static int git_tcp_connect_sock(char *host
)
404 int sockfd
= -1, saved_errno
= 0;
406 const char *port
= STR(DEFAULT_GIT_PORT
);
407 struct addrinfo hints
, *ai0
, *ai
;
410 if (host
[0] == '[') {
411 end
= strchr(host
+ 1, ']');
420 colon
= strchr(end
, ':');
427 memset(&hints
, 0, sizeof(hints
));
428 hints
.ai_socktype
= SOCK_STREAM
;
429 hints
.ai_protocol
= IPPROTO_TCP
;
431 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
433 die("Unable to look up %s (%s)", host
, gai_strerror(gai
));
435 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
436 sockfd
= socket(ai
->ai_family
,
437 ai
->ai_socktype
, ai
->ai_protocol
);
442 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
454 die("unable to connect a socket (%s)", strerror(saved_errno
));
462 * Returns a connected socket() fd, or else die()s.
464 static int git_tcp_connect_sock(char *host
)
466 int sockfd
= -1, saved_errno
= 0;
468 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
470 struct sockaddr_in sa
;
474 if (host
[0] == '[') {
475 end
= strchr(host
+ 1, ']');
484 colon
= strchr(end
, ':');
491 he
= gethostbyname(host
);
493 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
494 nport
= strtoul(port
, &ep
, 10);
495 if ( ep
== port
|| *ep
) {
497 struct servent
*se
= getservbyname(port
,"tcp");
499 die("Unknown port %s\n", port
);
503 for (ap
= he
->h_addr_list
; *ap
; ap
++) {
504 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
510 memset(&sa
, 0, sizeof sa
);
511 sa
.sin_family
= he
->h_addrtype
;
512 sa
.sin_port
= htons(nport
);
513 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
515 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
525 die("unable to connect a socket (%s)", strerror(saved_errno
));
533 static void git_tcp_connect(int fd
[2], char *host
)
535 int sockfd
= git_tcp_connect_sock(host
);
542 static char *git_proxy_command
;
543 static const char *rhost_name
;
544 static int rhost_len
;
546 static int git_proxy_command_options(const char *var
, const char *value
)
548 if (!strcmp(var
, "core.gitproxy")) {
553 if (git_proxy_command
)
556 * ;# matches www.kernel.org as well
557 * gitproxy = netcatter-1 for kernel.org
558 * gitproxy = netcatter-2 for sample.xz
559 * gitproxy = netcatter-default
561 for_pos
= strstr(value
, " for ");
563 /* matches everybody */
564 matchlen
= strlen(value
);
566 hostlen
= strlen(for_pos
+ 5);
567 if (rhost_len
< hostlen
)
569 else if (!strncmp(for_pos
+ 5,
570 rhost_name
+ rhost_len
- hostlen
,
572 ((rhost_len
== hostlen
) ||
573 rhost_name
[rhost_len
- hostlen
-1] == '.'))
574 matchlen
= for_pos
- value
;
579 /* core.gitproxy = none for kernel.org */
581 !memcmp(value
, "none", 4))
583 git_proxy_command
= xmalloc(matchlen
+ 1);
584 memcpy(git_proxy_command
, value
, matchlen
);
585 git_proxy_command
[matchlen
] = 0;
590 return git_default_config(var
, value
);
593 static int git_use_proxy(const char *host
)
596 rhost_len
= strlen(host
);
597 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
598 git_config(git_proxy_command_options
);
600 return (git_proxy_command
&& *git_proxy_command
);
603 static void git_proxy_connect(int fd
[2], char *host
)
605 const char *port
= STR(DEFAULT_GIT_PORT
);
610 if (host
[0] == '[') {
611 end
= strchr(host
+ 1, ']');
620 colon
= strchr(end
, ':');
627 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
628 die("unable to create pipe pair for communication");
631 dup2(pipefd
[1][0], 0);
632 dup2(pipefd
[0][1], 1);
637 execlp(git_proxy_command
, git_proxy_command
, host
, port
, NULL
);
642 fd
[0] = pipefd
[0][0];
643 fd
[1] = pipefd
[1][1];
648 #define MAX_CMD_LEN 1024
651 * This returns 0 if the transport protocol does not need fork(2),
652 * or a process id if it does. Once done, finish the connection
653 * with finish_connect() with the value returned from this function
654 * (it is safe to call finish_connect() with 0 to support the former
657 * Does not return a negative value on error; it just dies.
659 pid_t
git_connect(int fd
[2], char *url
, const char *prog
)
661 char *host
, *path
= url
;
666 enum protocol protocol
= PROTO_LOCAL
;
669 /* Without this we cannot rely on waitpid() to tell
670 * what happened to our children.
672 signal(SIGCHLD
, SIG_DFL
);
674 host
= strstr(url
, "://");
677 protocol
= get_protocol(url
);
685 if (host
[0] == '[') {
686 end
= strchr(host
+ 1, ']');
696 path
= strchr(end
, c
);
699 protocol
= PROTO_SSH
;
706 die("No path specified. See 'man git-pull' for valid url syntax");
709 * null-terminate hostname and point path to ~ for URL's like this:
710 * ssh://host.xz/~user/repo
712 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
724 if (protocol
== PROTO_GIT
) {
725 /* These underlying connection commands die() if they
728 char *target_host
= xstrdup(host
);
729 if (git_use_proxy(host
))
730 git_proxy_connect(fd
, host
);
732 git_tcp_connect(fd
, host
);
734 * Separate original protocol components prog and path
735 * from extended components with a NUL byte.
747 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
748 die("unable to create pipe pair for communication");
751 die("unable to fork");
753 char command
[MAX_CMD_LEN
];
754 char *posn
= command
;
755 int size
= MAX_CMD_LEN
;
758 of
|= add_to_string(&posn
, &size
, prog
, 0);
759 of
|= add_to_string(&posn
, &size
, " ", 0);
760 of
|= add_to_string(&posn
, &size
, path
, 1);
763 die("command line too long");
765 dup2(pipefd
[1][0], 0);
766 dup2(pipefd
[0][1], 1);
771 if (protocol
== PROTO_SSH
) {
772 const char *ssh
, *ssh_basename
;
773 ssh
= getenv("GIT_SSH");
774 if (!ssh
) ssh
= "ssh";
775 ssh_basename
= strrchr(ssh
, '/');
780 execlp(ssh
, ssh_basename
, host
, command
, NULL
);
783 unsetenv(ALTERNATE_DB_ENVIRONMENT
);
784 unsetenv(DB_ENVIRONMENT
);
785 unsetenv(GIT_DIR_ENVIRONMENT
);
786 unsetenv(GRAFT_ENVIRONMENT
);
787 unsetenv(INDEX_ENVIRONMENT
);
788 execlp("sh", "sh", "-c", command
, NULL
);
792 fd
[0] = pipefd
[0][0];
793 fd
[1] = pipefd
[1][1];
801 int finish_connect(pid_t pid
)
806 while (waitpid(pid
, NULL
, 0) < 0) {