1 #include "git-compat-util.h"
6 #include "run-command.h"
10 static char *server_capabilities
;
12 static int check_ref(const char *name
, int len
, unsigned int flags
)
17 if (len
< 5 || memcmp(name
, "refs/", 5))
20 /* Skip the "refs/" part */
24 /* REF_NORMAL means that we don't want the magic fake tag refs */
25 if ((flags
& REF_NORMAL
) && check_refname_format(name
, 0))
28 /* REF_HEADS means that we want regular branch heads */
29 if ((flags
& REF_HEADS
) && !memcmp(name
, "heads/", 6))
32 /* REF_TAGS means that we want tags */
33 if ((flags
& REF_TAGS
) && !memcmp(name
, "tags/", 5))
36 /* All type bits clear means that we are ok with anything */
37 return !(flags
& ~REF_NORMAL
);
40 int check_ref_type(const struct ref
*ref
, int flags
)
42 return check_ref(ref
->name
, strlen(ref
->name
), flags
);
45 static void add_extra_have(struct extra_have_objects
*extra
, unsigned char *sha1
)
47 ALLOC_GROW(extra
->array
, extra
->nr
+ 1, extra
->alloc
);
48 hashcpy(&(extra
->array
[extra
->nr
][0]), sha1
);
53 * Read all the refs from the other end
55 struct ref
**get_remote_heads(int in
, struct ref
**list
,
57 struct extra_have_objects
*extra_have
)
62 unsigned char old_sha1
[20];
63 static char buffer
[1000];
67 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
70 if (buffer
[len
-1] == '\n')
73 if (len
> 4 && !prefixcmp(buffer
, "ERR "))
74 die("remote error: %s", buffer
+ 4);
76 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
77 die("protocol error: expected sha/ref, got '%s'", buffer
);
80 name_len
= strlen(name
);
81 if (len
!= name_len
+ 41) {
82 free(server_capabilities
);
83 server_capabilities
= xstrdup(name
+ name_len
+ 1);
87 name_len
== 5 && !memcmp(".have", name
, 5)) {
88 add_extra_have(extra_have
, old_sha1
);
92 if (!check_ref(name
, name_len
, flags
))
94 ref
= alloc_ref(buffer
+ 41);
95 hashcpy(ref
->old_sha1
, old_sha1
);
102 const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
)
109 len
= strlen(feature
);
110 while (*feature_list
) {
111 const char *found
= strstr(feature_list
, feature
);
114 if (feature_list
== found
|| isspace(found
[-1])) {
115 const char *value
= found
+ len
;
116 /* feature with no value (e.g., "thin-pack") */
117 if (!*value
|| isspace(*value
)) {
122 /* feature with a value (e.g., "agent=git/1.2.3") */
123 else if (*value
== '=') {
126 *lenp
= strcspn(value
, " \t\n");
130 * otherwise we matched a substring of another feature;
134 feature_list
= found
+ 1;
139 int parse_feature_request(const char *feature_list
, const char *feature
)
141 return !!parse_feature_value(feature_list
, feature
, NULL
);
144 const char *server_feature_value(const char *feature
, int *len
)
146 return parse_feature_value(server_capabilities
, feature
, len
);
149 int server_supports(const char *feature
)
151 return !!server_feature_value(feature
, NULL
);
160 static enum protocol
get_protocol(const char *name
)
162 if (!strcmp(name
, "ssh"))
164 if (!strcmp(name
, "git"))
166 if (!strcmp(name
, "git+ssh"))
168 if (!strcmp(name
, "ssh+git"))
170 if (!strcmp(name
, "file"))
172 die("I don't handle protocol '%s'", name
);
176 #define STR(s) STR_(s)
178 static void get_host_and_port(char **host
, const char **port
)
182 if (*host
[0] == '[') {
183 end
= strchr(*host
+ 1, ']');
192 colon
= strchr(end
, ':');
200 static void enable_keepalive(int sockfd
)
204 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
205 fprintf(stderr
, "unable to set SO_KEEPALIVE on socket: %s\n",
211 static const char *ai_name(const struct addrinfo
*ai
)
213 static char addr
[NI_MAXHOST
];
214 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
215 NI_NUMERICHOST
) != 0)
216 strcpy(addr
, "(unknown)");
222 * Returns a connected socket() fd, or else die()s.
224 static int git_tcp_connect_sock(char *host
, int flags
)
226 struct strbuf error_message
= STRBUF_INIT
;
228 const char *port
= STR(DEFAULT_GIT_PORT
);
229 struct addrinfo hints
, *ai0
, *ai
;
233 get_host_and_port(&host
, &port
);
237 memset(&hints
, 0, sizeof(hints
));
238 hints
.ai_socktype
= SOCK_STREAM
;
239 hints
.ai_protocol
= IPPROTO_TCP
;
241 if (flags
& CONNECT_VERBOSE
)
242 fprintf(stderr
, "Looking up %s ... ", host
);
244 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
246 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
248 if (flags
& CONNECT_VERBOSE
)
249 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
251 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
252 sockfd
= socket(ai
->ai_family
,
253 ai
->ai_socktype
, ai
->ai_protocol
);
255 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
256 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
257 host
, cnt
, ai_name(ai
), strerror(errno
));
263 if (flags
& CONNECT_VERBOSE
)
264 fprintf(stderr
, "%s ", ai_name(ai
));
271 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
273 enable_keepalive(sockfd
);
275 if (flags
& CONNECT_VERBOSE
)
276 fprintf(stderr
, "done.\n");
278 strbuf_release(&error_message
);
286 * Returns a connected socket() fd, or else die()s.
288 static int git_tcp_connect_sock(char *host
, int flags
)
290 struct strbuf error_message
= STRBUF_INIT
;
292 const char *port
= STR(DEFAULT_GIT_PORT
);
295 struct sockaddr_in sa
;
300 get_host_and_port(&host
, &port
);
302 if (flags
& CONNECT_VERBOSE
)
303 fprintf(stderr
, "Looking up %s ... ", host
);
305 he
= gethostbyname(host
);
307 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
308 nport
= strtoul(port
, &ep
, 10);
309 if ( ep
== port
|| *ep
) {
311 struct servent
*se
= getservbyname(port
,"tcp");
313 die("Unknown port %s", port
);
317 if (flags
& CONNECT_VERBOSE
)
318 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
320 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
321 memset(&sa
, 0, sizeof sa
);
322 sa
.sin_family
= he
->h_addrtype
;
323 sa
.sin_port
= htons(nport
);
324 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
326 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
328 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
329 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
332 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
339 if (flags
& CONNECT_VERBOSE
)
340 fprintf(stderr
, "%s ",
341 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
346 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
348 enable_keepalive(sockfd
);
350 if (flags
& CONNECT_VERBOSE
)
351 fprintf(stderr
, "done.\n");
359 static void git_tcp_connect(int fd
[2], char *host
, int flags
)
361 int sockfd
= git_tcp_connect_sock(host
, flags
);
368 static char *git_proxy_command
;
370 static int git_proxy_command_options(const char *var
, const char *value
,
373 if (!strcmp(var
, "core.gitproxy")) {
377 const char *rhost_name
= cb
;
378 int rhost_len
= strlen(rhost_name
);
380 if (git_proxy_command
)
383 return config_error_nonbool(var
);
385 * ;# matches www.kernel.org as well
386 * gitproxy = netcatter-1 for kernel.org
387 * gitproxy = netcatter-2 for sample.xz
388 * gitproxy = netcatter-default
390 for_pos
= strstr(value
, " for ");
392 /* matches everybody */
393 matchlen
= strlen(value
);
395 hostlen
= strlen(for_pos
+ 5);
396 if (rhost_len
< hostlen
)
398 else if (!strncmp(for_pos
+ 5,
399 rhost_name
+ rhost_len
- hostlen
,
401 ((rhost_len
== hostlen
) ||
402 rhost_name
[rhost_len
- hostlen
-1] == '.'))
403 matchlen
= for_pos
- value
;
408 /* core.gitproxy = none for kernel.org */
410 !memcmp(value
, "none", 4))
412 git_proxy_command
= xmemdupz(value
, matchlen
);
417 return git_default_config(var
, value
, cb
);
420 static int git_use_proxy(const char *host
)
422 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
423 git_config(git_proxy_command_options
, (void*)host
);
424 return (git_proxy_command
&& *git_proxy_command
);
427 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
429 const char *port
= STR(DEFAULT_GIT_PORT
);
431 struct child_process
*proxy
;
433 get_host_and_port(&host
, &port
);
435 argv
= xmalloc(sizeof(*argv
) * 4);
436 argv
[0] = git_proxy_command
;
440 proxy
= xcalloc(1, sizeof(*proxy
));
444 if (start_command(proxy
))
445 die("cannot start proxy %s", argv
[0]);
446 fd
[0] = proxy
->out
; /* read from proxy stdout */
447 fd
[1] = proxy
->in
; /* write to proxy stdin */
451 #define MAX_CMD_LEN 1024
453 static char *get_port(char *host
)
456 char *p
= strchr(host
, ':');
459 long port
= strtol(p
+ 1, &end
, 10);
460 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
469 static struct child_process no_fork
;
472 * This returns a dummy child_process if the transport protocol does not
473 * need fork(2), or a struct child_process object if it does. Once done,
474 * finish the connection with finish_connect() with the value returned from
475 * this function (it is safe to call finish_connect() with NULL to support
478 * If it returns, the connect is successful; it just dies on errors (this
479 * will hopefully be changed in a libification effort, to return NULL when
480 * the connection failed).
482 struct child_process
*git_connect(int fd
[2], const char *url_orig
,
483 const char *prog
, int flags
)
489 struct child_process
*conn
= &no_fork
;
490 enum protocol protocol
= PROTO_LOCAL
;
496 /* Without this we cannot rely on waitpid() to tell
497 * what happened to our children.
499 signal(SIGCHLD
, SIG_DFL
);
501 if (is_url(url_orig
))
502 url
= url_decode(url_orig
);
504 url
= xstrdup(url_orig
);
506 host
= strstr(url
, "://");
509 protocol
= get_protocol(url
);
518 * Don't do destructive transforms with git:// as that
519 * protocol code does '[]' unwrapping of its own.
521 if (host
[0] == '[') {
522 end
= strchr(host
+ 1, ']');
524 if (protocol
!= PROTO_GIT
) {
534 path
= strchr(end
, c
);
535 if (path
&& !has_dos_drive_prefix(end
)) {
537 protocol
= PROTO_SSH
;
544 die("No path specified. See 'man git-pull' for valid url syntax");
547 * null-terminate hostname and point path to ~ for URL's like this:
548 * ssh://host.xz/~user/repo
550 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
563 * Add support for ssh port: ssh://host.xy:<port>/...
565 if (protocol
== PROTO_SSH
&& host
!= url
)
566 port
= get_port(host
);
568 if (protocol
== PROTO_GIT
) {
569 /* These underlying connection commands die() if they
572 char *target_host
= xstrdup(host
);
573 if (git_use_proxy(host
))
574 conn
= git_proxy_connect(fd
, host
);
576 git_tcp_connect(fd
, host
, flags
);
578 * Separate original protocol components prog and path
579 * from extended host header with a NUL byte.
581 * Note: Do not add any other headers here! Doing so
582 * will cause older git-daemon servers to crash.
595 conn
= xcalloc(1, sizeof(*conn
));
597 strbuf_init(&cmd
, MAX_CMD_LEN
);
598 strbuf_addstr(&cmd
, prog
);
599 strbuf_addch(&cmd
, ' ');
600 sq_quote_buf(&cmd
, path
);
601 if (cmd
.len
>= MAX_CMD_LEN
)
602 die("command line too long");
604 conn
->in
= conn
->out
= -1;
605 conn
->argv
= arg
= xcalloc(7, sizeof(*arg
));
606 if (protocol
== PROTO_SSH
) {
607 const char *ssh
= getenv("GIT_SSH");
608 int putty
= ssh
&& strcasestr(ssh
, "plink");
609 if (!ssh
) ssh
= "ssh";
612 if (putty
&& !strcasestr(ssh
, "tortoiseplink"))
615 /* P is for PuTTY, p is for OpenSSH */
616 *arg
++ = putty
? "-P" : "-p";
622 /* remove repo-local variables from the environment */
623 conn
->env
= local_repo_env
;
629 if (start_command(conn
))
630 die("unable to fork");
632 fd
[0] = conn
->out
; /* read from child's stdout */
633 fd
[1] = conn
->in
; /* write to child's stdin */
634 strbuf_release(&cmd
);
641 int git_connection_is_socket(struct child_process
*conn
)
643 return conn
== &no_fork
;
646 int finish_connect(struct child_process
*conn
)
649 if (!conn
|| git_connection_is_socket(conn
))
652 code
= finish_command(conn
);