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
= alloc_ref(name_len
+ 1);
76 hashcpy(ref
->old_sha1
, old_sha1
);
77 memcpy(ref
->name
, buffer
+ 41, name_len
+ 1);
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 if (!strcmp(name
, "file"))
150 die("I don't handle protocol '%s'", name
);
154 #define STR(s) STR_(s)
158 static const char *ai_name(const struct addrinfo
*ai
)
160 static char addr
[INET_ADDRSTRLEN
];
161 if ( AF_INET
== ai
->ai_family
) {
162 struct sockaddr_in
*in
;
163 in
= (struct sockaddr_in
*)ai
->ai_addr
;
164 inet_ntop(ai
->ai_family
, &in
->sin_addr
, addr
, sizeof(addr
));
165 } else if ( AF_INET6
== ai
->ai_family
) {
166 struct sockaddr_in6
*in
;
167 in
= (struct sockaddr_in6
*)ai
->ai_addr
;
168 inet_ntop(ai
->ai_family
, &in
->sin6_addr
, addr
, sizeof(addr
));
170 strcpy(addr
, "(unknown)");
176 * Returns a connected socket() fd, or else die()s.
178 static int git_tcp_connect_sock(char *host
, int flags
)
180 int sockfd
= -1, saved_errno
= 0;
182 const char *port
= STR(DEFAULT_GIT_PORT
);
183 struct addrinfo hints
, *ai0
, *ai
;
187 if (host
[0] == '[') {
188 end
= strchr(host
+ 1, ']');
197 colon
= strchr(end
, ':');
206 memset(&hints
, 0, sizeof(hints
));
207 hints
.ai_socktype
= SOCK_STREAM
;
208 hints
.ai_protocol
= IPPROTO_TCP
;
210 if (flags
& CONNECT_VERBOSE
)
211 fprintf(stderr
, "Looking up %s ... ", host
);
213 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
215 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
217 if (flags
& CONNECT_VERBOSE
)
218 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
220 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
221 sockfd
= socket(ai
->ai_family
,
222 ai
->ai_socktype
, ai
->ai_protocol
);
227 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
229 fprintf(stderr
, "%s[%d: %s]: errno=%s\n",
233 strerror(saved_errno
));
238 if (flags
& CONNECT_VERBOSE
)
239 fprintf(stderr
, "%s ", ai_name(ai
));
246 die("unable to connect a socket (%s)", strerror(saved_errno
));
248 if (flags
& CONNECT_VERBOSE
)
249 fprintf(stderr
, "done.\n");
257 * Returns a connected socket() fd, or else die()s.
259 static int git_tcp_connect_sock(char *host
, int flags
)
261 int sockfd
= -1, saved_errno
= 0;
263 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
265 struct sockaddr_in sa
;
270 if (host
[0] == '[') {
271 end
= strchr(host
+ 1, ']');
280 colon
= strchr(end
, ':');
287 if (flags
& CONNECT_VERBOSE
)
288 fprintf(stderr
, "Looking up %s ... ", host
);
290 he
= gethostbyname(host
);
292 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
293 nport
= strtoul(port
, &ep
, 10);
294 if ( ep
== port
|| *ep
) {
296 struct servent
*se
= getservbyname(port
,"tcp");
298 die("Unknown port %s\n", port
);
302 if (flags
& CONNECT_VERBOSE
)
303 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
305 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
306 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
312 memset(&sa
, 0, sizeof sa
);
313 sa
.sin_family
= he
->h_addrtype
;
314 sa
.sin_port
= htons(nport
);
315 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
317 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
319 fprintf(stderr
, "%s[%d: %s]: errno=%s\n",
322 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
323 strerror(saved_errno
));
328 if (flags
& CONNECT_VERBOSE
)
329 fprintf(stderr
, "%s ",
330 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
335 die("unable to connect a socket (%s)", strerror(saved_errno
));
337 if (flags
& CONNECT_VERBOSE
)
338 fprintf(stderr
, "done.\n");
346 static void git_tcp_connect(int fd
[2], char *host
, int flags
)
348 int sockfd
= git_tcp_connect_sock(host
, flags
);
355 static char *git_proxy_command
;
356 static const char *rhost_name
;
357 static int rhost_len
;
359 static int git_proxy_command_options(const char *var
, const char *value
)
361 if (!strcmp(var
, "core.gitproxy")) {
366 if (git_proxy_command
)
369 * ;# matches www.kernel.org as well
370 * gitproxy = netcatter-1 for kernel.org
371 * gitproxy = netcatter-2 for sample.xz
372 * gitproxy = netcatter-default
374 for_pos
= strstr(value
, " for ");
376 /* matches everybody */
377 matchlen
= strlen(value
);
379 hostlen
= strlen(for_pos
+ 5);
380 if (rhost_len
< hostlen
)
382 else if (!strncmp(for_pos
+ 5,
383 rhost_name
+ rhost_len
- hostlen
,
385 ((rhost_len
== hostlen
) ||
386 rhost_name
[rhost_len
- hostlen
-1] == '.'))
387 matchlen
= for_pos
- value
;
392 /* core.gitproxy = none for kernel.org */
394 !memcmp(value
, "none", 4))
396 git_proxy_command
= xmemdupz(value
, matchlen
);
401 return git_default_config(var
, value
);
404 static int git_use_proxy(const char *host
)
407 rhost_len
= strlen(host
);
408 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
409 git_config(git_proxy_command_options
);
411 return (git_proxy_command
&& *git_proxy_command
);
414 static void git_proxy_connect(int fd
[2], char *host
)
416 const char *port
= STR(DEFAULT_GIT_PORT
);
419 struct child_process proxy
;
421 if (host
[0] == '[') {
422 end
= strchr(host
+ 1, ']');
431 colon
= strchr(end
, ':');
438 argv
[0] = git_proxy_command
;
442 memset(&proxy
, 0, sizeof(proxy
));
446 if (start_command(&proxy
))
447 die("cannot start proxy %s", argv
[0]);
448 fd
[0] = proxy
.out
; /* read from proxy stdout */
449 fd
[1] = proxy
.in
; /* write to proxy stdin */
452 #define MAX_CMD_LEN 1024
454 char *get_port(char *host
)
457 char *p
= strchr(host
, ':');
460 strtol(p
+1, &end
, 10);
471 * This returns NULL if the transport protocol does not need fork(2), or a
472 * struct child_process object if it does. Once done, finish the connection
473 * with finish_connect() with the value returned from this function
474 * (it is safe to call finish_connect() with NULL to support the former
477 * If it returns, the connect is successful; it just dies on errors.
479 struct child_process
*git_connect(int fd
[2], char *url
,
480 const char *prog
, int flags
)
482 char *host
, *path
= url
;
486 struct child_process
*conn
;
487 enum protocol protocol
= PROTO_LOCAL
;
491 /* Without this we cannot rely on waitpid() to tell
492 * what happened to our children.
494 signal(SIGCHLD
, SIG_DFL
);
496 host
= strstr(url
, "://");
499 protocol
= get_protocol(url
);
507 if (host
[0] == '[') {
508 end
= strchr(host
+ 1, ']');
518 path
= strchr(end
, c
);
521 protocol
= PROTO_SSH
;
528 die("No path specified. See 'man git-pull' for valid url syntax");
531 * null-terminate hostname and point path to ~ for URL's like this:
532 * ssh://host.xz/~user/repo
534 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
547 * Add support for ssh port: ssh://host.xy:<port>/...
549 if (protocol
== PROTO_SSH
&& host
!= url
)
550 port
= get_port(host
);
552 if (protocol
== PROTO_GIT
) {
553 /* These underlying connection commands die() if they
556 char *target_host
= xstrdup(host
);
557 if (git_use_proxy(host
))
558 git_proxy_connect(fd
, host
);
560 git_tcp_connect(fd
, host
, flags
);
562 * Separate original protocol components prog and path
563 * from extended components with a NUL byte.
575 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
576 die("unable to create pipe pair for communication");
577 conn
= xcalloc(1, sizeof(*conn
));
580 die("unable to fork");
584 strbuf_init(&cmd
, MAX_CMD_LEN
);
585 strbuf_addstr(&cmd
, prog
);
586 strbuf_addch(&cmd
, ' ');
587 sq_quote_buf(&cmd
, path
);
588 if (cmd
.len
>= MAX_CMD_LEN
)
589 die("command line too long");
591 dup2(pipefd
[1][0], 0);
592 dup2(pipefd
[0][1], 1);
597 if (protocol
== PROTO_SSH
) {
598 const char *ssh
, *ssh_basename
;
599 ssh
= getenv("GIT_SSH");
600 if (!ssh
) ssh
= "ssh";
601 ssh_basename
= strrchr(ssh
, '/');
608 execlp(ssh
, ssh_basename
, host
, cmd
.buf
, NULL
);
610 execlp(ssh
, ssh_basename
, "-p", port
, host
,
614 unsetenv(ALTERNATE_DB_ENVIRONMENT
);
615 unsetenv(DB_ENVIRONMENT
);
616 unsetenv(GIT_DIR_ENVIRONMENT
);
617 unsetenv(GIT_WORK_TREE_ENVIRONMENT
);
618 unsetenv(GRAFT_ENVIRONMENT
);
619 unsetenv(INDEX_ENVIRONMENT
);
620 execlp("sh", "sh", "-c", cmd
.buf
, NULL
);
624 fd
[0] = pipefd
[0][0];
625 fd
[1] = pipefd
[1][1];
633 int finish_connect(struct child_process
*conn
)
638 while (waitpid(conn
->pid
, NULL
, 0) < 0) {