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
);
39 int check_ref_type(const struct ref
*ref
, int flags
)
41 return check_ref(ref
->name
, strlen(ref
->name
), flags
);
45 * Read all the refs from the other end
47 struct ref
**get_remote_heads(int in
, struct ref
**list
,
48 int nr_match
, char **match
,
54 unsigned char old_sha1
[20];
55 static char buffer
[1000];
59 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
62 if (buffer
[len
-1] == '\n')
65 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
66 die("protocol error: expected sha/ref, got '%s'", buffer
);
69 name_len
= strlen(name
);
70 if (len
!= name_len
+ 41) {
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
= alloc_ref(name_len
+ 1);
80 hashcpy(ref
->old_sha1
, old_sha1
);
81 memcpy(ref
->name
, buffer
+ 41, name_len
+ 1);
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 (!prefixcmp(line
, "ACK ")) {
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] != '/')
142 static enum protocol
get_protocol(const char *name
)
144 if (!strcmp(name
, "ssh"))
146 if (!strcmp(name
, "git"))
148 if (!strcmp(name
, "git+ssh"))
150 if (!strcmp(name
, "ssh+git"))
152 if (!strcmp(name
, "file"))
154 die("I don't handle protocol '%s'", name
);
158 #define STR(s) STR_(s)
162 static const char *ai_name(const struct addrinfo
*ai
)
164 static char addr
[INET_ADDRSTRLEN
];
165 if ( AF_INET
== ai
->ai_family
) {
166 struct sockaddr_in
*in
;
167 in
= (struct sockaddr_in
*)ai
->ai_addr
;
168 inet_ntop(ai
->ai_family
, &in
->sin_addr
, addr
, sizeof(addr
));
169 } else if ( AF_INET6
== ai
->ai_family
) {
170 struct sockaddr_in6
*in
;
171 in
= (struct sockaddr_in6
*)ai
->ai_addr
;
172 inet_ntop(ai
->ai_family
, &in
->sin6_addr
, addr
, sizeof(addr
));
174 strcpy(addr
, "(unknown)");
180 * Returns a connected socket() fd, or else die()s.
182 static int git_tcp_connect_sock(char *host
, int flags
)
184 int sockfd
= -1, saved_errno
= 0;
186 const char *port
= STR(DEFAULT_GIT_PORT
);
187 struct addrinfo hints
, *ai0
, *ai
;
191 if (host
[0] == '[') {
192 end
= strchr(host
+ 1, ']');
201 colon
= strchr(end
, ':');
210 memset(&hints
, 0, sizeof(hints
));
211 hints
.ai_socktype
= SOCK_STREAM
;
212 hints
.ai_protocol
= IPPROTO_TCP
;
214 if (flags
& CONNECT_VERBOSE
)
215 fprintf(stderr
, "Looking up %s ... ", host
);
217 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
219 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
221 if (flags
& CONNECT_VERBOSE
)
222 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
224 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
225 sockfd
= socket(ai
->ai_family
,
226 ai
->ai_socktype
, ai
->ai_protocol
);
231 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
233 fprintf(stderr
, "%s[%d: %s]: errno=%s\n",
237 strerror(saved_errno
));
242 if (flags
& CONNECT_VERBOSE
)
243 fprintf(stderr
, "%s ", ai_name(ai
));
250 die("unable to connect a socket (%s)", strerror(saved_errno
));
252 if (flags
& CONNECT_VERBOSE
)
253 fprintf(stderr
, "done.\n");
261 * Returns a connected socket() fd, or else die()s.
263 static int git_tcp_connect_sock(char *host
, int flags
)
265 int sockfd
= -1, saved_errno
= 0;
267 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
269 struct sockaddr_in sa
;
274 if (host
[0] == '[') {
275 end
= strchr(host
+ 1, ']');
284 colon
= strchr(end
, ':');
291 if (flags
& CONNECT_VERBOSE
)
292 fprintf(stderr
, "Looking up %s ... ", host
);
294 he
= gethostbyname(host
);
296 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
297 nport
= strtoul(port
, &ep
, 10);
298 if ( ep
== port
|| *ep
) {
300 struct servent
*se
= getservbyname(port
,"tcp");
302 die("Unknown port %s\n", port
);
306 if (flags
& CONNECT_VERBOSE
)
307 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
309 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
310 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
316 memset(&sa
, 0, sizeof sa
);
317 sa
.sin_family
= he
->h_addrtype
;
318 sa
.sin_port
= htons(nport
);
319 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
321 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
323 fprintf(stderr
, "%s[%d: %s]: errno=%s\n",
326 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
327 strerror(saved_errno
));
332 if (flags
& CONNECT_VERBOSE
)
333 fprintf(stderr
, "%s ",
334 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
339 die("unable to connect a socket (%s)", strerror(saved_errno
));
341 if (flags
& CONNECT_VERBOSE
)
342 fprintf(stderr
, "done.\n");
350 static void git_tcp_connect(int fd
[2], char *host
, int flags
)
352 int sockfd
= git_tcp_connect_sock(host
, flags
);
359 static char *git_proxy_command
;
360 static const char *rhost_name
;
361 static int rhost_len
;
363 static int git_proxy_command_options(const char *var
, const char *value
)
365 if (!strcmp(var
, "core.gitproxy")) {
370 if (git_proxy_command
)
373 return config_error_nonbool(var
);
375 * ;# matches www.kernel.org as well
376 * gitproxy = netcatter-1 for kernel.org
377 * gitproxy = netcatter-2 for sample.xz
378 * gitproxy = netcatter-default
380 for_pos
= strstr(value
, " for ");
382 /* matches everybody */
383 matchlen
= strlen(value
);
385 hostlen
= strlen(for_pos
+ 5);
386 if (rhost_len
< hostlen
)
388 else if (!strncmp(for_pos
+ 5,
389 rhost_name
+ rhost_len
- hostlen
,
391 ((rhost_len
== hostlen
) ||
392 rhost_name
[rhost_len
- hostlen
-1] == '.'))
393 matchlen
= for_pos
- value
;
398 /* core.gitproxy = none for kernel.org */
400 !memcmp(value
, "none", 4))
402 git_proxy_command
= xmemdupz(value
, matchlen
);
407 return git_default_config(var
, value
);
410 static int git_use_proxy(const char *host
)
413 rhost_len
= strlen(host
);
414 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
415 git_config(git_proxy_command_options
);
417 return (git_proxy_command
&& *git_proxy_command
);
420 static void git_proxy_connect(int fd
[2], char *host
)
422 const char *port
= STR(DEFAULT_GIT_PORT
);
425 struct child_process proxy
;
427 if (host
[0] == '[') {
428 end
= strchr(host
+ 1, ']');
437 colon
= strchr(end
, ':');
444 argv
[0] = git_proxy_command
;
448 memset(&proxy
, 0, sizeof(proxy
));
452 if (start_command(&proxy
))
453 die("cannot start proxy %s", argv
[0]);
454 fd
[0] = proxy
.out
; /* read from proxy stdout */
455 fd
[1] = proxy
.in
; /* write to proxy stdin */
458 #define MAX_CMD_LEN 1024
460 char *get_port(char *host
)
463 char *p
= strchr(host
, ':');
466 strtol(p
+1, &end
, 10);
476 static struct child_process no_fork
;
479 * This returns a dummy child_process if the transport protocol does not
480 * need fork(2), or a struct child_process object if it does. Once done,
481 * finish the connection with finish_connect() with the value returned from
482 * this function (it is safe to call finish_connect() with NULL to support
485 * If it returns, the connect is successful; it just dies on errors (this
486 * will hopefully be changed in a libification effort, to return NULL when
487 * the connection failed).
489 struct child_process
*git_connect(int fd
[2], const char *url_orig
,
490 const char *prog
, int flags
)
492 char *url
= xstrdup(url_orig
);
493 char *host
, *path
= url
;
496 struct child_process
*conn
;
497 enum protocol protocol
= PROTO_LOCAL
;
503 /* Without this we cannot rely on waitpid() to tell
504 * what happened to our children.
506 signal(SIGCHLD
, SIG_DFL
);
508 host
= strstr(url
, "://");
511 protocol
= get_protocol(url
);
519 if (host
[0] == '[') {
520 end
= strchr(host
+ 1, ']');
530 path
= strchr(end
, c
);
533 protocol
= PROTO_SSH
;
540 die("No path specified. See 'man git-pull' for valid url syntax");
543 * null-terminate hostname and point path to ~ for URL's like this:
544 * ssh://host.xz/~user/repo
546 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
559 * Add support for ssh port: ssh://host.xy:<port>/...
561 if (protocol
== PROTO_SSH
&& host
!= url
)
562 port
= get_port(host
);
564 if (protocol
== PROTO_GIT
) {
565 /* These underlying connection commands die() if they
568 char *target_host
= xstrdup(host
);
569 if (git_use_proxy(host
))
570 git_proxy_connect(fd
, host
);
572 git_tcp_connect(fd
, host
, flags
);
574 * Separate original protocol components prog and path
575 * from extended components with a NUL byte.
588 conn
= xcalloc(1, sizeof(*conn
));
590 strbuf_init(&cmd
, MAX_CMD_LEN
);
591 strbuf_addstr(&cmd
, prog
);
592 strbuf_addch(&cmd
, ' ');
593 sq_quote_buf(&cmd
, path
);
594 if (cmd
.len
>= MAX_CMD_LEN
)
595 die("command line too long");
597 conn
->in
= conn
->out
= -1;
598 conn
->argv
= arg
= xcalloc(6, sizeof(*arg
));
599 if (protocol
== PROTO_SSH
) {
600 const char *ssh
= getenv("GIT_SSH");
601 if (!ssh
) ssh
= "ssh";
611 /* remove these from the environment */
612 const char *env
[] = {
613 ALTERNATE_DB_ENVIRONMENT
,
616 GIT_WORK_TREE_ENVIRONMENT
,
628 if (start_command(conn
))
629 die("unable to fork");
631 fd
[0] = conn
->out
; /* read from child's stdout */
632 fd
[1] = conn
->in
; /* write to child's stdin */
633 strbuf_release(&cmd
);
640 int finish_connect(struct child_process
*conn
)
643 if (!conn
|| conn
== &no_fork
)
646 code
= finish_command(conn
);