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 if (server_capabilities
)
72 free(server_capabilities
);
73 server_capabilities
= xstrdup(name
+ name_len
+ 1);
76 if (!check_ref(name
, name_len
, flags
))
78 if (nr_match
&& !path_match(name
, nr_match
, match
))
80 ref
= alloc_ref(name_len
+ 1);
81 hashcpy(ref
->old_sha1
, old_sha1
);
82 memcpy(ref
->name
, buffer
+ 41, name_len
+ 1);
89 int server_supports(const char *feature
)
91 return server_capabilities
&&
92 strstr(server_capabilities
, feature
) != NULL
;
95 int get_ack(int fd
, unsigned char *result_sha1
)
97 static char line
[1000];
98 int len
= packet_read_line(fd
, line
, sizeof(line
));
101 die("git-fetch-pack: expected ACK/NAK, got EOF");
102 if (line
[len
-1] == '\n')
104 if (!strcmp(line
, "NAK"))
106 if (!prefixcmp(line
, "ACK ")) {
107 if (!get_sha1_hex(line
+4, result_sha1
)) {
108 if (strstr(line
+45, "continue"))
113 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
116 int path_match(const char *path
, int nr
, char **match
)
119 int pathlen
= strlen(path
);
121 for (i
= 0; i
< nr
; i
++) {
125 if (!len
|| len
> pathlen
)
127 if (memcmp(path
+ pathlen
- len
, s
, len
))
129 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
143 static enum protocol
get_protocol(const char *name
)
145 if (!strcmp(name
, "ssh"))
147 if (!strcmp(name
, "git"))
149 if (!strcmp(name
, "git+ssh"))
151 if (!strcmp(name
, "ssh+git"))
153 if (!strcmp(name
, "file"))
155 die("I don't handle protocol '%s'", name
);
159 #define STR(s) STR_(s)
163 static const char *ai_name(const struct addrinfo
*ai
)
165 static char addr
[INET_ADDRSTRLEN
];
166 if ( AF_INET
== ai
->ai_family
) {
167 struct sockaddr_in
*in
;
168 in
= (struct sockaddr_in
*)ai
->ai_addr
;
169 inet_ntop(ai
->ai_family
, &in
->sin_addr
, addr
, sizeof(addr
));
170 } else if ( AF_INET6
== ai
->ai_family
) {
171 struct sockaddr_in6
*in
;
172 in
= (struct sockaddr_in6
*)ai
->ai_addr
;
173 inet_ntop(ai
->ai_family
, &in
->sin6_addr
, addr
, sizeof(addr
));
175 strcpy(addr
, "(unknown)");
181 * Returns a connected socket() fd, or else die()s.
183 static int git_tcp_connect_sock(char *host
, int flags
)
185 int sockfd
= -1, saved_errno
= 0;
187 const char *port
= STR(DEFAULT_GIT_PORT
);
188 struct addrinfo hints
, *ai0
, *ai
;
192 if (host
[0] == '[') {
193 end
= strchr(host
+ 1, ']');
202 colon
= strchr(end
, ':');
211 memset(&hints
, 0, sizeof(hints
));
212 hints
.ai_socktype
= SOCK_STREAM
;
213 hints
.ai_protocol
= IPPROTO_TCP
;
215 if (flags
& CONNECT_VERBOSE
)
216 fprintf(stderr
, "Looking up %s ... ", host
);
218 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
220 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
222 if (flags
& CONNECT_VERBOSE
)
223 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
225 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
226 sockfd
= socket(ai
->ai_family
,
227 ai
->ai_socktype
, ai
->ai_protocol
);
232 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
234 fprintf(stderr
, "%s[%d: %s]: errno=%s\n",
238 strerror(saved_errno
));
243 if (flags
& CONNECT_VERBOSE
)
244 fprintf(stderr
, "%s ", ai_name(ai
));
251 die("unable to connect a socket (%s)", strerror(saved_errno
));
253 if (flags
& CONNECT_VERBOSE
)
254 fprintf(stderr
, "done.\n");
262 * Returns a connected socket() fd, or else die()s.
264 static int git_tcp_connect_sock(char *host
, int flags
)
266 int sockfd
= -1, saved_errno
= 0;
268 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
270 struct sockaddr_in sa
;
275 if (host
[0] == '[') {
276 end
= strchr(host
+ 1, ']');
285 colon
= strchr(end
, ':');
292 if (flags
& CONNECT_VERBOSE
)
293 fprintf(stderr
, "Looking up %s ... ", host
);
295 he
= gethostbyname(host
);
297 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
298 nport
= strtoul(port
, &ep
, 10);
299 if ( ep
== port
|| *ep
) {
301 struct servent
*se
= getservbyname(port
,"tcp");
303 die("Unknown port %s\n", port
);
307 if (flags
& CONNECT_VERBOSE
)
308 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
310 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
311 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
317 memset(&sa
, 0, sizeof sa
);
318 sa
.sin_family
= he
->h_addrtype
;
319 sa
.sin_port
= htons(nport
);
320 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
322 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
324 fprintf(stderr
, "%s[%d: %s]: errno=%s\n",
327 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
328 strerror(saved_errno
));
333 if (flags
& CONNECT_VERBOSE
)
334 fprintf(stderr
, "%s ",
335 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
340 die("unable to connect a socket (%s)", strerror(saved_errno
));
342 if (flags
& CONNECT_VERBOSE
)
343 fprintf(stderr
, "done.\n");
351 static void git_tcp_connect(int fd
[2], char *host
, int flags
)
353 int sockfd
= git_tcp_connect_sock(host
, flags
);
360 static char *git_proxy_command
;
361 static const char *rhost_name
;
362 static int rhost_len
;
364 static int git_proxy_command_options(const char *var
, const char *value
)
366 if (!strcmp(var
, "core.gitproxy")) {
371 if (git_proxy_command
)
374 * ;# matches www.kernel.org as well
375 * gitproxy = netcatter-1 for kernel.org
376 * gitproxy = netcatter-2 for sample.xz
377 * gitproxy = netcatter-default
379 for_pos
= strstr(value
, " for ");
381 /* matches everybody */
382 matchlen
= strlen(value
);
384 hostlen
= strlen(for_pos
+ 5);
385 if (rhost_len
< hostlen
)
387 else if (!strncmp(for_pos
+ 5,
388 rhost_name
+ rhost_len
- hostlen
,
390 ((rhost_len
== hostlen
) ||
391 rhost_name
[rhost_len
- hostlen
-1] == '.'))
392 matchlen
= for_pos
- value
;
397 /* core.gitproxy = none for kernel.org */
399 !memcmp(value
, "none", 4))
401 git_proxy_command
= xmemdupz(value
, matchlen
);
406 return git_default_config(var
, value
);
409 static int git_use_proxy(const char *host
)
412 rhost_len
= strlen(host
);
413 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
414 git_config(git_proxy_command_options
);
416 return (git_proxy_command
&& *git_proxy_command
);
419 static void git_proxy_connect(int fd
[2], char *host
)
421 const char *port
= STR(DEFAULT_GIT_PORT
);
424 struct child_process proxy
;
426 if (host
[0] == '[') {
427 end
= strchr(host
+ 1, ']');
436 colon
= strchr(end
, ':');
443 argv
[0] = git_proxy_command
;
447 memset(&proxy
, 0, sizeof(proxy
));
451 if (start_command(&proxy
))
452 die("cannot start proxy %s", argv
[0]);
453 fd
[0] = proxy
.out
; /* read from proxy stdout */
454 fd
[1] = proxy
.in
; /* write to proxy stdin */
457 #define MAX_CMD_LEN 1024
459 char *get_port(char *host
)
462 char *p
= strchr(host
, ':');
465 strtol(p
+1, &end
, 10);
476 * This returns NULL if the transport protocol does not need fork(2), or a
477 * struct child_process object if it does. Once done, finish the connection
478 * with finish_connect() with the value returned from this function
479 * (it is safe to call finish_connect() with NULL to support the former
482 * If it returns, the connect is successful; it just dies on errors.
484 struct child_process
*git_connect(int fd
[2], const char *url_orig
,
485 const char *prog
, int flags
)
487 char *url
= xstrdup(url_orig
);
488 char *host
, *path
= url
;
491 struct child_process
*conn
;
492 enum protocol protocol
= PROTO_LOCAL
;
498 /* Without this we cannot rely on waitpid() to tell
499 * what happened to our children.
501 signal(SIGCHLD
, SIG_DFL
);
503 host
= strstr(url
, "://");
506 protocol
= get_protocol(url
);
514 if (host
[0] == '[') {
515 end
= strchr(host
+ 1, ']');
525 path
= strchr(end
, c
);
528 protocol
= PROTO_SSH
;
535 die("No path specified. See 'man git-pull' for valid url syntax");
538 * null-terminate hostname and point path to ~ for URL's like this:
539 * ssh://host.xz/~user/repo
541 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
554 * Add support for ssh port: ssh://host.xy:<port>/...
556 if (protocol
== PROTO_SSH
&& host
!= url
)
557 port
= get_port(host
);
559 if (protocol
== PROTO_GIT
) {
560 /* These underlying connection commands die() if they
563 char *target_host
= xstrdup(host
);
564 if (git_use_proxy(host
))
565 git_proxy_connect(fd
, host
);
567 git_tcp_connect(fd
, host
, flags
);
569 * Separate original protocol components prog and path
570 * from extended components with a NUL byte.
583 conn
= xcalloc(1, sizeof(*conn
));
585 strbuf_init(&cmd
, MAX_CMD_LEN
);
586 strbuf_addstr(&cmd
, prog
);
587 strbuf_addch(&cmd
, ' ');
588 sq_quote_buf(&cmd
, path
);
589 if (cmd
.len
>= MAX_CMD_LEN
)
590 die("command line too long");
592 conn
->in
= conn
->out
= -1;
593 conn
->argv
= arg
= xcalloc(6, sizeof(*arg
));
594 if (protocol
== PROTO_SSH
) {
595 const char *ssh
= getenv("GIT_SSH");
596 if (!ssh
) ssh
= "ssh";
606 /* remove these from the environment */
607 const char *env
[] = {
608 ALTERNATE_DB_ENVIRONMENT
,
611 GIT_WORK_TREE_ENVIRONMENT
,
623 if (start_command(conn
))
624 die("unable to fork");
626 fd
[0] = conn
->out
; /* read from child's stdout */
627 fd
[1] = conn
->in
; /* write to child's stdin */
628 strbuf_release(&cmd
);
635 int finish_connect(struct child_process
*conn
)
641 code
= finish_command(conn
);