1 #include "git-compat-util.h"
7 #include <sys/socket.h>
8 #include <netinet/in.h>
13 static char *server_capabilities
= NULL
;
16 * Read all the refs from the other end
18 struct ref
**get_remote_heads(int in
, struct ref
**list
,
19 int nr_match
, char **match
, int ignore_funny
)
24 unsigned char old_sha1
[20];
25 static char buffer
[1000];
29 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
32 if (buffer
[len
-1] == '\n')
35 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
36 die("protocol error: expected sha/ref, got '%s'", buffer
);
39 name_len
= strlen(name
);
40 if (len
!= name_len
+ 41) {
41 if (server_capabilities
)
42 free(server_capabilities
);
43 server_capabilities
= strdup(name
+ name_len
+ 1);
46 if (ignore_funny
&& 45 < len
&& !memcmp(name
, "refs/", 5) &&
47 check_ref_format(name
+ 5))
50 if (nr_match
&& !path_match(name
, nr_match
, match
))
52 ref
= xcalloc(1, sizeof(*ref
) + len
- 40);
53 memcpy(ref
->old_sha1
, old_sha1
, 20);
54 memcpy(ref
->name
, buffer
+ 41, len
- 40);
61 int server_supports(const char *feature
)
63 return server_capabilities
&&
64 strstr(server_capabilities
, feature
) != NULL
;
67 int get_ack(int fd
, unsigned char *result_sha1
)
69 static char line
[1000];
70 int len
= packet_read_line(fd
, line
, sizeof(line
));
73 die("git-fetch-pack: expected ACK/NAK, got EOF");
74 if (line
[len
-1] == '\n')
76 if (!strcmp(line
, "NAK"))
78 if (!strncmp(line
, "ACK ", 4)) {
79 if (!get_sha1_hex(line
+4, result_sha1
)) {
80 if (strstr(line
+45, "continue"))
85 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
88 int path_match(const char *path
, int nr
, char **match
)
91 int pathlen
= strlen(path
);
93 for (i
= 0; i
< nr
; i
++) {
97 if (!len
|| len
> pathlen
)
99 if (memcmp(path
+ pathlen
- len
, s
, len
))
101 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
116 * A:B means fast forward remote B with local A.
117 * +A:B means overwrite remote B with local A.
118 * +A is a shorthand for +A:A.
119 * A is a shorthand for A:A.
121 static struct refspec
*parse_ref_spec(int nr_refspec
, char **refspec
)
124 struct refspec
*rs
= xcalloc(sizeof(*rs
), (nr_refspec
+ 1));
125 for (i
= 0; i
< nr_refspec
; i
++) {
132 ep
= strchr(sp
, ':');
142 rs
[nr_refspec
].src
= rs
[nr_refspec
].dst
= NULL
;
146 static int count_refspec_match(const char *pattern
,
148 struct ref
**matched_ref
)
151 int patlen
= strlen(pattern
);
153 for (match
= 0; refs
; refs
= refs
->next
) {
154 char *name
= refs
->name
;
155 int namelen
= strlen(name
);
156 if (namelen
< patlen
||
157 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
159 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
167 static void link_dst_tail(struct ref
*ref
, struct ref
***tail
)
174 static struct ref
*try_explicit_object_name(const char *name
)
176 unsigned char sha1
[20];
179 if (get_sha1(name
, sha1
))
181 len
= strlen(name
) + 1;
182 ref
= xcalloc(1, sizeof(*ref
) + len
);
183 memcpy(ref
->name
, name
, len
);
184 memcpy(ref
->new_sha1
, sha1
, 20);
188 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
189 struct ref
***dst_tail
, struct refspec
*rs
)
192 for (i
= errs
= 0; rs
[i
].src
; i
++) {
193 struct ref
*matched_src
, *matched_dst
;
195 matched_src
= matched_dst
= NULL
;
196 switch (count_refspec_match(rs
[i
].src
, src
, &matched_src
)) {
200 /* The source could be in the get_sha1() format
201 * not a reference name.
203 matched_src
= try_explicit_object_name(rs
[i
].src
);
207 error("src refspec %s does not match any.",
212 error("src refspec %s matches more than one.",
216 switch (count_refspec_match(rs
[i
].dst
, dst
, &matched_dst
)) {
220 if (!memcmp(rs
[i
].dst
, "refs/", 5)) {
221 int len
= strlen(rs
[i
].dst
) + 1;
222 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
223 memcpy(matched_dst
->name
, rs
[i
].dst
, len
);
224 link_dst_tail(matched_dst
, dst_tail
);
226 else if (!strcmp(rs
[i
].src
, rs
[i
].dst
) &&
228 /* pushing "master:master" when
229 * remote does not have master yet.
231 int len
= strlen(matched_src
->name
) + 1;
232 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
233 memcpy(matched_dst
->name
, matched_src
->name
,
235 link_dst_tail(matched_dst
, dst_tail
);
239 error("dst refspec %s does not match any "
240 "existing ref on the remote and does "
241 "not start with refs/.", rs
[i
].dst
);
246 error("dst refspec %s matches more than one.",
252 if (matched_dst
->peer_ref
) {
254 error("dst ref %s receives from more than one src.",
258 matched_dst
->peer_ref
= matched_src
;
259 matched_dst
->force
= rs
[i
].force
;
265 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
267 for ( ; list
; list
= list
->next
)
268 if (!strcmp(list
->name
, name
))
273 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
274 int nr_refspec
, char **refspec
, int all
)
276 struct refspec
*rs
= parse_ref_spec(nr_refspec
, refspec
);
279 return match_explicit_refs(src
, dst
, dst_tail
, rs
);
281 /* pick the remainder */
282 for ( ; src
; src
= src
->next
) {
283 struct ref
*dst_peer
;
286 dst_peer
= find_ref_by_name(dst
, src
->name
);
287 if ((dst_peer
&& dst_peer
->peer_ref
) || (!dst_peer
&& !all
))
290 /* Create a new one and link it */
291 int len
= strlen(src
->name
) + 1;
292 dst_peer
= xcalloc(1, sizeof(*dst_peer
) + len
);
293 memcpy(dst_peer
->name
, src
->name
, len
);
294 memcpy(dst_peer
->new_sha1
, src
->new_sha1
, 20);
295 link_dst_tail(dst_peer
, dst_tail
);
297 dst_peer
->peer_ref
= src
;
308 static enum protocol
get_protocol(const char *name
)
310 if (!strcmp(name
, "ssh"))
312 if (!strcmp(name
, "git"))
314 if (!strcmp(name
, "git+ssh"))
316 if (!strcmp(name
, "ssh+git"))
318 die("I don't handle protocol '%s'", name
);
322 #define STR(s) STR_(s)
327 * Returns a connected socket() fd, or else die()s.
329 static int git_tcp_connect_sock(char *host
)
333 char *port
= STR(DEFAULT_GIT_PORT
);
334 struct addrinfo hints
, *ai0
, *ai
;
337 if (host
[0] == '[') {
338 end
= strchr(host
+ 1, ']');
347 colon
= strchr(end
, ':');
354 memset(&hints
, 0, sizeof(hints
));
355 hints
.ai_socktype
= SOCK_STREAM
;
356 hints
.ai_protocol
= IPPROTO_TCP
;
358 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
360 die("Unable to look up %s (%s)", host
, gai_strerror(gai
));
362 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
363 sockfd
= socket(ai
->ai_family
,
364 ai
->ai_socktype
, ai
->ai_protocol
);
367 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
378 die("unable to connect a socket (%s)", strerror(errno
));
386 * Returns a connected socket() fd, or else die()s.
388 static int git_tcp_connect_sock(char *host
)
392 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
394 struct sockaddr_in sa
;
398 if (host
[0] == '[') {
399 end
= strchr(host
+ 1, ']');
408 colon
= strchr(end
, ':');
415 he
= gethostbyname(host
);
417 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
418 nport
= strtoul(port
, &ep
, 10);
419 if ( ep
== port
|| *ep
) {
421 struct servent
*se
= getservbyname(port
,"tcp");
423 die("Unknown port %s\n", port
);
427 for (ap
= he
->h_addr_list
; *ap
; ap
++) {
428 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
432 memset(&sa
, 0, sizeof sa
);
433 sa
.sin_family
= he
->h_addrtype
;
434 sa
.sin_port
= htons(nport
);
435 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
437 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
446 die("unable to connect a socket (%s)", strerror(errno
));
454 static void git_tcp_connect(int fd
[2],
455 const char *prog
, char *host
, char *path
)
457 int sockfd
= git_tcp_connect_sock(host
);
464 static char *git_proxy_command
= NULL
;
465 static const char *rhost_name
= NULL
;
466 static int rhost_len
;
468 static int git_proxy_command_options(const char *var
, const char *value
)
470 if (!strcmp(var
, "core.gitproxy")) {
475 if (git_proxy_command
)
478 * ;# matches www.kernel.org as well
479 * gitproxy = netcatter-1 for kernel.org
480 * gitproxy = netcatter-2 for sample.xz
481 * gitproxy = netcatter-default
483 for_pos
= strstr(value
, " for ");
485 /* matches everybody */
486 matchlen
= strlen(value
);
488 hostlen
= strlen(for_pos
+ 5);
489 if (rhost_len
< hostlen
)
491 else if (!strncmp(for_pos
+ 5,
492 rhost_name
+ rhost_len
- hostlen
,
494 ((rhost_len
== hostlen
) ||
495 rhost_name
[rhost_len
- hostlen
-1] == '.'))
496 matchlen
= for_pos
- value
;
501 /* core.gitproxy = none for kernel.org */
503 !memcmp(value
, "none", 4))
505 git_proxy_command
= xmalloc(matchlen
+ 1);
506 memcpy(git_proxy_command
, value
, matchlen
);
507 git_proxy_command
[matchlen
] = 0;
512 return git_default_config(var
, value
);
515 static int git_use_proxy(const char *host
)
518 rhost_len
= strlen(host
);
519 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
520 git_config(git_proxy_command_options
);
522 return (git_proxy_command
&& *git_proxy_command
);
525 static void git_proxy_connect(int fd
[2],
526 const char *prog
, char *host
, char *path
)
528 char *port
= STR(DEFAULT_GIT_PORT
);
533 if (host
[0] == '[') {
534 end
= strchr(host
+ 1, ']');
543 colon
= strchr(end
, ':');
550 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
551 die("unable to create pipe pair for communication");
554 dup2(pipefd
[1][0], 0);
555 dup2(pipefd
[0][1], 1);
560 execlp(git_proxy_command
, git_proxy_command
, host
, port
, NULL
);
565 fd
[0] = pipefd
[0][0];
566 fd
[1] = pipefd
[1][1];
572 * Yeah, yeah, fixme. Need to pass in the heads etc.
574 int git_connect(int fd
[2], char *url
, const char *prog
)
577 char *host
, *path
= url
;
582 enum protocol protocol
= PROTO_LOCAL
;
585 /* Without this we cannot rely on waitpid() to tell
586 * what happened to our children.
588 signal(SIGCHLD
, SIG_DFL
);
590 host
= strstr(url
, "://");
593 protocol
= get_protocol(url
);
601 if (host
[0] == '[') {
602 end
= strchr(host
+ 1, ']');
612 path
= strchr(end
, c
);
615 protocol
= PROTO_SSH
;
622 die("No path specified. See 'man git-pull' for valid url syntax");
625 * null-terminate hostname and point path to ~ for URL's like this:
626 * ssh://host.xz/~user/repo
628 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
640 if (protocol
== PROTO_GIT
) {
641 /* These underlying connection commands die() if they
644 char *target_host
= strdup(host
);
645 if (git_use_proxy(host
))
646 git_proxy_connect(fd
, prog
, host
, path
);
648 git_tcp_connect(fd
, prog
, host
, path
);
650 * Separate original protocol components prog and path
651 * from extended components with a NUL byte.
663 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
664 die("unable to create pipe pair for communication");
667 die("unable to fork");
669 snprintf(command
, sizeof(command
), "%s %s", prog
,
671 dup2(pipefd
[1][0], 0);
672 dup2(pipefd
[0][1], 1);
677 if (protocol
== PROTO_SSH
) {
678 const char *ssh
, *ssh_basename
;
679 ssh
= getenv("GIT_SSH");
680 if (!ssh
) ssh
= "ssh";
681 ssh_basename
= strrchr(ssh
, '/');
686 execlp(ssh
, ssh_basename
, host
, command
, NULL
);
689 unsetenv(ALTERNATE_DB_ENVIRONMENT
);
690 unsetenv(DB_ENVIRONMENT
);
691 unsetenv(GIT_DIR_ENVIRONMENT
);
692 unsetenv(GRAFT_ENVIRONMENT
);
693 unsetenv(INDEX_ENVIRONMENT
);
694 execlp("sh", "sh", "-c", command
, NULL
);
698 fd
[0] = pipefd
[0][0];
699 fd
[1] = pipefd
[1][1];
707 int finish_connect(pid_t pid
)
712 ret
= waitpid(pid
, NULL
, 0);