1 #include "git-compat-util.h"
6 #include "run-command.h"
9 static char *server_capabilities
;
11 static int check_ref(const char *name
, int len
, unsigned int flags
)
16 if (len
< 5 || memcmp(name
, "refs/", 5))
19 /* Skip the "refs/" part */
23 /* REF_NORMAL means that we don't want the magic fake tag refs */
24 if ((flags
& REF_NORMAL
) && check_ref_format(name
) < 0)
27 /* REF_HEADS means that we want regular branch heads */
28 if ((flags
& REF_HEADS
) && !memcmp(name
, "heads/", 6))
31 /* REF_TAGS means that we want tags */
32 if ((flags
& REF_TAGS
) && !memcmp(name
, "tags/", 5))
35 /* All type bits clear means that we are ok with anything */
36 return !(flags
& ~REF_NORMAL
);
40 * Read all the refs from the other end
42 struct ref
**get_remote_heads(int in
, struct ref
**list
,
43 int nr_match
, char **match
,
49 unsigned char old_sha1
[20];
50 static char buffer
[1000];
54 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
57 if (buffer
[len
-1] == '\n')
60 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
61 die("protocol error: expected sha/ref, got '%s'", buffer
);
64 name_len
= strlen(name
);
65 if (len
!= name_len
+ 41) {
66 if (server_capabilities
)
67 free(server_capabilities
);
68 server_capabilities
= xstrdup(name
+ name_len
+ 1);
71 if (!check_ref(name
, name_len
, flags
))
73 if (nr_match
&& !path_match(name
, nr_match
, match
))
75 ref
= xcalloc(1, sizeof(*ref
) + len
- 40);
76 hashcpy(ref
->old_sha1
, old_sha1
);
77 memcpy(ref
->name
, buffer
+ 41, len
- 40);
84 int server_supports(const char *feature
)
86 return server_capabilities
&&
87 strstr(server_capabilities
, feature
) != NULL
;
90 int get_ack(int fd
, unsigned char *result_sha1
)
92 static char line
[1000];
93 int len
= packet_read_line(fd
, line
, sizeof(line
));
96 die("git-fetch-pack: expected ACK/NAK, got EOF");
97 if (line
[len
-1] == '\n')
99 if (!strcmp(line
, "NAK"))
101 if (!prefixcmp(line
, "ACK ")) {
102 if (!get_sha1_hex(line
+4, result_sha1
)) {
103 if (strstr(line
+45, "continue"))
108 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
111 int path_match(const char *path
, int nr
, char **match
)
114 int pathlen
= strlen(path
);
116 for (i
= 0; i
< nr
; i
++) {
120 if (!len
|| len
> pathlen
)
122 if (memcmp(path
+ pathlen
- len
, s
, len
))
124 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
138 static enum protocol
get_protocol(const char *name
)
140 if (!strcmp(name
, "ssh"))
142 if (!strcmp(name
, "git"))
144 if (!strcmp(name
, "git+ssh"))
146 if (!strcmp(name
, "ssh+git"))
148 die("I don't handle protocol '%s'", name
);
152 #define STR(s) STR_(s)
157 * Returns a connected socket() fd, or else die()s.
159 static int git_tcp_connect_sock(char *host
, int flags
)
161 int sockfd
= -1, saved_errno
= 0;
163 const char *port
= STR(DEFAULT_GIT_PORT
);
164 struct addrinfo hints
, *ai0
, *ai
;
167 if (host
[0] == '[') {
168 end
= strchr(host
+ 1, ']');
177 colon
= strchr(end
, ':');
186 memset(&hints
, 0, sizeof(hints
));
187 hints
.ai_socktype
= SOCK_STREAM
;
188 hints
.ai_protocol
= IPPROTO_TCP
;
190 if (flags
& CONNECT_VERBOSE
)
191 fprintf(stderr
, "Looking up %s ... ", host
);
193 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
195 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
197 if (flags
& CONNECT_VERBOSE
)
198 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
200 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
201 sockfd
= socket(ai
->ai_family
,
202 ai
->ai_socktype
, ai
->ai_protocol
);
207 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
219 die("unable to connect a socket (%s)", strerror(saved_errno
));
221 if (flags
& CONNECT_VERBOSE
)
222 fprintf(stderr
, "done.\n");
230 * Returns a connected socket() fd, or else die()s.
232 static int git_tcp_connect_sock(char *host
, int flags
)
234 int sockfd
= -1, saved_errno
= 0;
236 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
238 struct sockaddr_in sa
;
242 if (host
[0] == '[') {
243 end
= strchr(host
+ 1, ']');
252 colon
= strchr(end
, ':');
259 if (flags
& CONNECT_VERBOSE
)
260 fprintf(stderr
, "Looking up %s ... ", host
);
262 he
= gethostbyname(host
);
264 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
265 nport
= strtoul(port
, &ep
, 10);
266 if ( ep
== port
|| *ep
) {
268 struct servent
*se
= getservbyname(port
,"tcp");
270 die("Unknown port %s\n", port
);
274 if (flags
& CONNECT_VERBOSE
)
275 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
277 for (ap
= he
->h_addr_list
; *ap
; ap
++) {
278 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
284 memset(&sa
, 0, sizeof sa
);
285 sa
.sin_family
= he
->h_addrtype
;
286 sa
.sin_port
= htons(nport
);
287 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
289 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
299 die("unable to connect a socket (%s)", strerror(saved_errno
));
301 if (flags
& CONNECT_VERBOSE
)
302 fprintf(stderr
, "done.\n");
310 static void git_tcp_connect(int fd
[2], char *host
, int flags
)
312 int sockfd
= git_tcp_connect_sock(host
, flags
);
319 static char *git_proxy_command
;
320 static const char *rhost_name
;
321 static int rhost_len
;
323 static int git_proxy_command_options(const char *var
, const char *value
)
325 if (!strcmp(var
, "core.gitproxy")) {
330 if (git_proxy_command
)
333 * ;# matches www.kernel.org as well
334 * gitproxy = netcatter-1 for kernel.org
335 * gitproxy = netcatter-2 for sample.xz
336 * gitproxy = netcatter-default
338 for_pos
= strstr(value
, " for ");
340 /* matches everybody */
341 matchlen
= strlen(value
);
343 hostlen
= strlen(for_pos
+ 5);
344 if (rhost_len
< hostlen
)
346 else if (!strncmp(for_pos
+ 5,
347 rhost_name
+ rhost_len
- hostlen
,
349 ((rhost_len
== hostlen
) ||
350 rhost_name
[rhost_len
- hostlen
-1] == '.'))
351 matchlen
= for_pos
- value
;
356 /* core.gitproxy = none for kernel.org */
358 !memcmp(value
, "none", 4))
360 git_proxy_command
= xmalloc(matchlen
+ 1);
361 memcpy(git_proxy_command
, value
, matchlen
);
362 git_proxy_command
[matchlen
] = 0;
367 return git_default_config(var
, value
);
370 static int git_use_proxy(const char *host
)
373 rhost_len
= strlen(host
);
374 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
375 git_config(git_proxy_command_options
);
377 return (git_proxy_command
&& *git_proxy_command
);
380 static void git_proxy_connect(int fd
[2], char *host
)
382 const char *port
= STR(DEFAULT_GIT_PORT
);
385 struct child_process proxy
;
387 if (host
[0] == '[') {
388 end
= strchr(host
+ 1, ']');
397 colon
= strchr(end
, ':');
404 argv
[0] = git_proxy_command
;
408 memset(&proxy
, 0, sizeof(proxy
));
412 if (start_command(&proxy
))
413 die("cannot start proxy %s", argv
[0]);
414 fd
[0] = proxy
.out
; /* read from proxy stdout */
415 fd
[1] = proxy
.in
; /* write to proxy stdin */
418 #define MAX_CMD_LEN 1024
421 * This returns 0 if the transport protocol does not need fork(2),
422 * or a process id if it does. Once done, finish the connection
423 * with finish_connect() with the value returned from this function
424 * (it is safe to call finish_connect() with 0 to support the former
427 * Does not return a negative value on error; it just dies.
429 pid_t
git_connect(int fd
[2], char *url
, const char *prog
, int flags
)
431 char *host
, *path
= url
;
436 enum protocol protocol
= PROTO_LOCAL
;
439 /* Without this we cannot rely on waitpid() to tell
440 * what happened to our children.
442 signal(SIGCHLD
, SIG_DFL
);
444 host
= strstr(url
, "://");
447 protocol
= get_protocol(url
);
455 if (host
[0] == '[') {
456 end
= strchr(host
+ 1, ']');
466 path
= strchr(end
, c
);
469 protocol
= PROTO_SSH
;
476 die("No path specified. See 'man git-pull' for valid url syntax");
479 * null-terminate hostname and point path to ~ for URL's like this:
480 * ssh://host.xz/~user/repo
482 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
494 if (protocol
== PROTO_GIT
) {
495 /* These underlying connection commands die() if they
498 char *target_host
= xstrdup(host
);
499 if (git_use_proxy(host
))
500 git_proxy_connect(fd
, host
);
502 git_tcp_connect(fd
, host
, flags
);
504 * Separate original protocol components prog and path
505 * from extended components with a NUL byte.
517 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
518 die("unable to create pipe pair for communication");
521 die("unable to fork");
523 char command
[MAX_CMD_LEN
];
524 char *posn
= command
;
525 int size
= MAX_CMD_LEN
;
528 of
|= add_to_string(&posn
, &size
, prog
, 0);
529 of
|= add_to_string(&posn
, &size
, " ", 0);
530 of
|= add_to_string(&posn
, &size
, path
, 1);
533 die("command line too long");
535 dup2(pipefd
[1][0], 0);
536 dup2(pipefd
[0][1], 1);
541 if (protocol
== PROTO_SSH
) {
542 const char *ssh
, *ssh_basename
;
543 ssh
= getenv("GIT_SSH");
544 if (!ssh
) ssh
= "ssh";
545 ssh_basename
= strrchr(ssh
, '/');
550 execlp(ssh
, ssh_basename
, host
, command
, NULL
);
553 unsetenv(ALTERNATE_DB_ENVIRONMENT
);
554 unsetenv(DB_ENVIRONMENT
);
555 unsetenv(GIT_DIR_ENVIRONMENT
);
556 unsetenv(GRAFT_ENVIRONMENT
);
557 unsetenv(INDEX_ENVIRONMENT
);
558 execlp("sh", "sh", "-c", command
, NULL
);
562 fd
[0] = pipefd
[0][0];
563 fd
[1] = pipefd
[1][1];
571 int finish_connect(pid_t pid
)
576 while (waitpid(pid
, NULL
, 0) < 0) {