1 #include "git-compat-util.h"
7 #include <sys/socket.h>
8 #include <netinet/in.h>
13 static char *server_capabilities
= NULL
;
15 static int check_ref(const char *name
, int len
, unsigned int flags
)
20 if (len
> 45 || 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
= strdup(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 memcpy(ref
->old_sha1
, old_sha1
, 20);
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.
148 static struct refspec
*parse_ref_spec(int nr_refspec
, char **refspec
)
151 struct refspec
*rs
= xcalloc(sizeof(*rs
), (nr_refspec
+ 1));
152 for (i
= 0; i
< nr_refspec
; i
++) {
159 ep
= strchr(sp
, ':');
169 rs
[nr_refspec
].src
= rs
[nr_refspec
].dst
= NULL
;
173 static int count_refspec_match(const char *pattern
,
175 struct ref
**matched_ref
)
178 int patlen
= strlen(pattern
);
180 for (match
= 0; refs
; refs
= refs
->next
) {
181 char *name
= refs
->name
;
182 int namelen
= strlen(name
);
183 if (namelen
< patlen
||
184 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
186 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
194 static void link_dst_tail(struct ref
*ref
, struct ref
***tail
)
201 static struct ref
*try_explicit_object_name(const char *name
)
203 unsigned char sha1
[20];
206 if (get_sha1(name
, sha1
))
208 len
= strlen(name
) + 1;
209 ref
= xcalloc(1, sizeof(*ref
) + len
);
210 memcpy(ref
->name
, name
, len
);
211 memcpy(ref
->new_sha1
, sha1
, 20);
215 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
216 struct ref
***dst_tail
, struct refspec
*rs
)
219 for (i
= errs
= 0; rs
[i
].src
; i
++) {
220 struct ref
*matched_src
, *matched_dst
;
222 matched_src
= matched_dst
= NULL
;
223 switch (count_refspec_match(rs
[i
].src
, src
, &matched_src
)) {
227 /* The source could be in the get_sha1() format
228 * not a reference name.
230 matched_src
= try_explicit_object_name(rs
[i
].src
);
234 error("src refspec %s does not match any.",
239 error("src refspec %s matches more than one.",
243 switch (count_refspec_match(rs
[i
].dst
, dst
, &matched_dst
)) {
247 if (!memcmp(rs
[i
].dst
, "refs/", 5)) {
248 int len
= strlen(rs
[i
].dst
) + 1;
249 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
250 memcpy(matched_dst
->name
, rs
[i
].dst
, len
);
251 link_dst_tail(matched_dst
, dst_tail
);
253 else if (!strcmp(rs
[i
].src
, rs
[i
].dst
) &&
255 /* pushing "master:master" when
256 * remote does not have master yet.
258 int len
= strlen(matched_src
->name
) + 1;
259 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
260 memcpy(matched_dst
->name
, matched_src
->name
,
262 link_dst_tail(matched_dst
, dst_tail
);
266 error("dst refspec %s does not match any "
267 "existing ref on the remote and does "
268 "not start with refs/.", rs
[i
].dst
);
273 error("dst refspec %s matches more than one.",
279 if (matched_dst
->peer_ref
) {
281 error("dst ref %s receives from more than one src.",
285 matched_dst
->peer_ref
= matched_src
;
286 matched_dst
->force
= rs
[i
].force
;
292 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
294 for ( ; list
; list
= list
->next
)
295 if (!strcmp(list
->name
, name
))
300 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
301 int nr_refspec
, char **refspec
, int all
)
303 struct refspec
*rs
= parse_ref_spec(nr_refspec
, refspec
);
306 return match_explicit_refs(src
, dst
, dst_tail
, rs
);
308 /* pick the remainder */
309 for ( ; src
; src
= src
->next
) {
310 struct ref
*dst_peer
;
313 dst_peer
= find_ref_by_name(dst
, src
->name
);
314 if ((dst_peer
&& dst_peer
->peer_ref
) || (!dst_peer
&& !all
))
317 /* Create a new one and link it */
318 int len
= strlen(src
->name
) + 1;
319 dst_peer
= xcalloc(1, sizeof(*dst_peer
) + len
);
320 memcpy(dst_peer
->name
, src
->name
, len
);
321 memcpy(dst_peer
->new_sha1
, src
->new_sha1
, 20);
322 link_dst_tail(dst_peer
, dst_tail
);
324 dst_peer
->peer_ref
= src
;
335 static enum protocol
get_protocol(const char *name
)
337 if (!strcmp(name
, "ssh"))
339 if (!strcmp(name
, "git"))
341 if (!strcmp(name
, "git+ssh"))
343 if (!strcmp(name
, "ssh+git"))
345 die("I don't handle protocol '%s'", name
);
349 #define STR(s) STR_(s)
354 * Returns a connected socket() fd, or else die()s.
356 static int git_tcp_connect_sock(char *host
)
358 int sockfd
= -1, saved_errno
= 0;
360 const char *port
= STR(DEFAULT_GIT_PORT
);
361 struct addrinfo hints
, *ai0
, *ai
;
364 if (host
[0] == '[') {
365 end
= strchr(host
+ 1, ']');
374 colon
= strchr(end
, ':');
381 memset(&hints
, 0, sizeof(hints
));
382 hints
.ai_socktype
= SOCK_STREAM
;
383 hints
.ai_protocol
= IPPROTO_TCP
;
385 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
387 die("Unable to look up %s (%s)", host
, gai_strerror(gai
));
389 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
390 sockfd
= socket(ai
->ai_family
,
391 ai
->ai_socktype
, ai
->ai_protocol
);
396 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
408 die("unable to connect a socket (%s)", strerror(saved_errno
));
416 * Returns a connected socket() fd, or else die()s.
418 static int git_tcp_connect_sock(char *host
)
420 int sockfd
= -1, saved_errno
= 0;
422 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
424 struct sockaddr_in sa
;
428 if (host
[0] == '[') {
429 end
= strchr(host
+ 1, ']');
438 colon
= strchr(end
, ':');
445 he
= gethostbyname(host
);
447 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
448 nport
= strtoul(port
, &ep
, 10);
449 if ( ep
== port
|| *ep
) {
451 struct servent
*se
= getservbyname(port
,"tcp");
453 die("Unknown port %s\n", port
);
457 for (ap
= he
->h_addr_list
; *ap
; ap
++) {
458 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
464 memset(&sa
, 0, sizeof sa
);
465 sa
.sin_family
= he
->h_addrtype
;
466 sa
.sin_port
= htons(nport
);
467 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
469 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
479 die("unable to connect a socket (%s)", strerror(saved_errno
));
487 static void git_tcp_connect(int fd
[2], char *host
)
489 int sockfd
= git_tcp_connect_sock(host
);
496 static char *git_proxy_command
= NULL
;
497 static const char *rhost_name
= NULL
;
498 static int rhost_len
;
500 static int git_proxy_command_options(const char *var
, const char *value
)
502 if (!strcmp(var
, "core.gitproxy")) {
507 if (git_proxy_command
)
510 * ;# matches www.kernel.org as well
511 * gitproxy = netcatter-1 for kernel.org
512 * gitproxy = netcatter-2 for sample.xz
513 * gitproxy = netcatter-default
515 for_pos
= strstr(value
, " for ");
517 /* matches everybody */
518 matchlen
= strlen(value
);
520 hostlen
= strlen(for_pos
+ 5);
521 if (rhost_len
< hostlen
)
523 else if (!strncmp(for_pos
+ 5,
524 rhost_name
+ rhost_len
- hostlen
,
526 ((rhost_len
== hostlen
) ||
527 rhost_name
[rhost_len
- hostlen
-1] == '.'))
528 matchlen
= for_pos
- value
;
533 /* core.gitproxy = none for kernel.org */
535 !memcmp(value
, "none", 4))
537 git_proxy_command
= xmalloc(matchlen
+ 1);
538 memcpy(git_proxy_command
, value
, matchlen
);
539 git_proxy_command
[matchlen
] = 0;
544 return git_default_config(var
, value
);
547 static int git_use_proxy(const char *host
)
550 rhost_len
= strlen(host
);
551 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
552 git_config(git_proxy_command_options
);
554 return (git_proxy_command
&& *git_proxy_command
);
557 static void git_proxy_connect(int fd
[2], char *host
)
559 const char *port
= STR(DEFAULT_GIT_PORT
);
564 if (host
[0] == '[') {
565 end
= strchr(host
+ 1, ']');
574 colon
= strchr(end
, ':');
581 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
582 die("unable to create pipe pair for communication");
585 dup2(pipefd
[1][0], 0);
586 dup2(pipefd
[0][1], 1);
591 execlp(git_proxy_command
, git_proxy_command
, host
, port
, NULL
);
596 fd
[0] = pipefd
[0][0];
597 fd
[1] = pipefd
[1][1];
603 * Yeah, yeah, fixme. Need to pass in the heads etc.
605 int git_connect(int fd
[2], char *url
, const char *prog
)
608 char *host
, *path
= url
;
613 enum protocol protocol
= PROTO_LOCAL
;
616 /* Without this we cannot rely on waitpid() to tell
617 * what happened to our children.
619 signal(SIGCHLD
, SIG_DFL
);
621 host
= strstr(url
, "://");
624 protocol
= get_protocol(url
);
632 if (host
[0] == '[') {
633 end
= strchr(host
+ 1, ']');
643 path
= strchr(end
, c
);
646 protocol
= PROTO_SSH
;
653 die("No path specified. See 'man git-pull' for valid url syntax");
656 * null-terminate hostname and point path to ~ for URL's like this:
657 * ssh://host.xz/~user/repo
659 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
671 if (protocol
== PROTO_GIT
) {
672 /* These underlying connection commands die() if they
675 char *target_host
= strdup(host
);
676 if (git_use_proxy(host
))
677 git_proxy_connect(fd
, host
);
679 git_tcp_connect(fd
, host
);
681 * Separate original protocol components prog and path
682 * from extended components with a NUL byte.
694 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
695 die("unable to create pipe pair for communication");
698 die("unable to fork");
700 snprintf(command
, sizeof(command
), "%s %s", prog
,
702 dup2(pipefd
[1][0], 0);
703 dup2(pipefd
[0][1], 1);
708 if (protocol
== PROTO_SSH
) {
709 const char *ssh
, *ssh_basename
;
710 ssh
= getenv("GIT_SSH");
711 if (!ssh
) ssh
= "ssh";
712 ssh_basename
= strrchr(ssh
, '/');
717 execlp(ssh
, ssh_basename
, host
, command
, NULL
);
720 unsetenv(ALTERNATE_DB_ENVIRONMENT
);
721 unsetenv(DB_ENVIRONMENT
);
722 unsetenv(GIT_DIR_ENVIRONMENT
);
723 unsetenv(GRAFT_ENVIRONMENT
);
724 unsetenv(INDEX_ENVIRONMENT
);
725 execlp("sh", "sh", "-c", command
, NULL
);
729 fd
[0] = pipefd
[0][0];
730 fd
[1] = pipefd
[1][1];
738 int finish_connect(pid_t pid
)
740 while (waitpid(pid
, NULL
, 0) < 0) {