1 #include "git-compat-util.h"
6 #include "run-command.h"
11 static char *server_capabilities
;
13 static int check_ref(const char *name
, int len
, unsigned int flags
)
18 if (len
< 5 || memcmp(name
, "refs/", 5))
21 /* Skip the "refs/" part */
25 /* REF_NORMAL means that we don't want the magic fake tag refs */
26 if ((flags
& REF_NORMAL
) && check_refname_format(name
, 0))
29 /* REF_HEADS means that we want regular branch heads */
30 if ((flags
& REF_HEADS
) && !memcmp(name
, "heads/", 6))
33 /* REF_TAGS means that we want tags */
34 if ((flags
& REF_TAGS
) && !memcmp(name
, "tags/", 5))
37 /* All type bits clear means that we are ok with anything */
38 return !(flags
& ~REF_NORMAL
);
41 int check_ref_type(const struct ref
*ref
, int flags
)
43 return check_ref(ref
->name
, strlen(ref
->name
), flags
);
46 static void add_extra_have(struct extra_have_objects
*extra
, unsigned char *sha1
)
48 ALLOC_GROW(extra
->array
, extra
->nr
+ 1, extra
->alloc
);
49 hashcpy(&(extra
->array
[extra
->nr
][0]), sha1
);
53 static void die_initial_contact(int got_at_least_one_head
)
55 if (got_at_least_one_head
)
56 die("The remote end hung up upon initial contact");
58 die("Could not read from remote repository.\n\n"
59 "Please make sure you have the correct access rights\n"
60 "and the repository exists.");
64 * Read all the refs from the other end
66 struct ref
**get_remote_heads(int in
, char *src_buf
, size_t src_len
,
67 struct ref
**list
, unsigned int flags
,
68 struct extra_have_objects
*extra_have
)
70 int got_at_least_one_head
= 0;
75 unsigned char old_sha1
[20];
78 char *buffer
= packet_buffer
;
80 len
= packet_read(in
, &src_buf
, &src_len
,
81 packet_buffer
, sizeof(packet_buffer
),
82 PACKET_READ_GENTLE_ON_EOF
|
83 PACKET_READ_CHOMP_NEWLINE
);
85 die_initial_contact(got_at_least_one_head
);
90 if (len
> 4 && !prefixcmp(buffer
, "ERR "))
91 die("remote error: %s", buffer
+ 4);
93 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
94 die("protocol error: expected sha/ref, got '%s'", buffer
);
97 name_len
= strlen(name
);
98 if (len
!= name_len
+ 41) {
99 free(server_capabilities
);
100 server_capabilities
= xstrdup(name
+ name_len
+ 1);
104 name_len
== 5 && !memcmp(".have", name
, 5)) {
105 add_extra_have(extra_have
, old_sha1
);
109 if (!check_ref(name
, name_len
, flags
))
111 ref
= alloc_ref(buffer
+ 41);
112 hashcpy(ref
->old_sha1
, old_sha1
);
115 got_at_least_one_head
= 1;
120 const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
)
127 len
= strlen(feature
);
128 while (*feature_list
) {
129 const char *found
= strstr(feature_list
, feature
);
132 if (feature_list
== found
|| isspace(found
[-1])) {
133 const char *value
= found
+ len
;
134 /* feature with no value (e.g., "thin-pack") */
135 if (!*value
|| isspace(*value
)) {
140 /* feature with a value (e.g., "agent=git/1.2.3") */
141 else if (*value
== '=') {
144 *lenp
= strcspn(value
, " \t\n");
148 * otherwise we matched a substring of another feature;
152 feature_list
= found
+ 1;
157 int parse_feature_request(const char *feature_list
, const char *feature
)
159 return !!parse_feature_value(feature_list
, feature
, NULL
);
162 const char *server_feature_value(const char *feature
, int *len
)
164 return parse_feature_value(server_capabilities
, feature
, len
);
167 int server_supports(const char *feature
)
169 return !!server_feature_value(feature
, NULL
);
178 static enum protocol
get_protocol(const char *name
)
180 if (!strcmp(name
, "ssh"))
182 if (!strcmp(name
, "git"))
184 if (!strcmp(name
, "git+ssh"))
186 if (!strcmp(name
, "ssh+git"))
188 if (!strcmp(name
, "file"))
190 die("I don't handle protocol '%s'", name
);
194 #define STR(s) STR_(s)
196 static void get_host_and_port(char **host
, const char **port
)
200 if (*host
[0] == '[') {
201 end
= strchr(*host
+ 1, ']');
210 colon
= strchr(end
, ':');
218 static void enable_keepalive(int sockfd
)
222 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
223 fprintf(stderr
, "unable to set SO_KEEPALIVE on socket: %s\n",
229 static const char *ai_name(const struct addrinfo
*ai
)
231 static char addr
[NI_MAXHOST
];
232 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
233 NI_NUMERICHOST
) != 0)
234 strcpy(addr
, "(unknown)");
240 * Returns a connected socket() fd, or else die()s.
242 static int git_tcp_connect_sock(char *host
, int flags
)
244 struct strbuf error_message
= STRBUF_INIT
;
246 const char *port
= STR(DEFAULT_GIT_PORT
);
247 struct addrinfo hints
, *ai0
, *ai
;
251 get_host_and_port(&host
, &port
);
255 memset(&hints
, 0, sizeof(hints
));
256 hints
.ai_socktype
= SOCK_STREAM
;
257 hints
.ai_protocol
= IPPROTO_TCP
;
259 if (flags
& CONNECT_VERBOSE
)
260 fprintf(stderr
, "Looking up %s ... ", host
);
262 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
264 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
266 if (flags
& CONNECT_VERBOSE
)
267 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
269 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
270 sockfd
= socket(ai
->ai_family
,
271 ai
->ai_socktype
, ai
->ai_protocol
);
273 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
274 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
275 host
, cnt
, ai_name(ai
), strerror(errno
));
281 if (flags
& CONNECT_VERBOSE
)
282 fprintf(stderr
, "%s ", ai_name(ai
));
289 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
291 enable_keepalive(sockfd
);
293 if (flags
& CONNECT_VERBOSE
)
294 fprintf(stderr
, "done.\n");
296 strbuf_release(&error_message
);
304 * Returns a connected socket() fd, or else die()s.
306 static int git_tcp_connect_sock(char *host
, int flags
)
308 struct strbuf error_message
= STRBUF_INIT
;
310 const char *port
= STR(DEFAULT_GIT_PORT
);
313 struct sockaddr_in sa
;
318 get_host_and_port(&host
, &port
);
320 if (flags
& CONNECT_VERBOSE
)
321 fprintf(stderr
, "Looking up %s ... ", host
);
323 he
= gethostbyname(host
);
325 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
326 nport
= strtoul(port
, &ep
, 10);
327 if ( ep
== port
|| *ep
) {
329 struct servent
*se
= getservbyname(port
,"tcp");
331 die("Unknown port %s", port
);
335 if (flags
& CONNECT_VERBOSE
)
336 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
338 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
339 memset(&sa
, 0, sizeof sa
);
340 sa
.sin_family
= he
->h_addrtype
;
341 sa
.sin_port
= htons(nport
);
342 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
344 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
346 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
347 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
350 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
357 if (flags
& CONNECT_VERBOSE
)
358 fprintf(stderr
, "%s ",
359 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
364 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
366 enable_keepalive(sockfd
);
368 if (flags
& CONNECT_VERBOSE
)
369 fprintf(stderr
, "done.\n");
377 static void git_tcp_connect(int fd
[2], char *host
, int flags
)
379 int sockfd
= git_tcp_connect_sock(host
, flags
);
386 static char *git_proxy_command
;
388 static int git_proxy_command_options(const char *var
, const char *value
,
391 if (!strcmp(var
, "core.gitproxy")) {
395 const char *rhost_name
= cb
;
396 int rhost_len
= strlen(rhost_name
);
398 if (git_proxy_command
)
401 return config_error_nonbool(var
);
403 * ;# matches www.kernel.org as well
404 * gitproxy = netcatter-1 for kernel.org
405 * gitproxy = netcatter-2 for sample.xz
406 * gitproxy = netcatter-default
408 for_pos
= strstr(value
, " for ");
410 /* matches everybody */
411 matchlen
= strlen(value
);
413 hostlen
= strlen(for_pos
+ 5);
414 if (rhost_len
< hostlen
)
416 else if (!strncmp(for_pos
+ 5,
417 rhost_name
+ rhost_len
- hostlen
,
419 ((rhost_len
== hostlen
) ||
420 rhost_name
[rhost_len
- hostlen
-1] == '.'))
421 matchlen
= for_pos
- value
;
426 /* core.gitproxy = none for kernel.org */
428 !memcmp(value
, "none", 4))
430 git_proxy_command
= xmemdupz(value
, matchlen
);
435 return git_default_config(var
, value
, cb
);
438 static int git_use_proxy(const char *host
)
440 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
441 git_config(git_proxy_command_options
, (void*)host
);
442 return (git_proxy_command
&& *git_proxy_command
);
445 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
447 const char *port
= STR(DEFAULT_GIT_PORT
);
449 struct child_process
*proxy
;
451 get_host_and_port(&host
, &port
);
453 argv
= xmalloc(sizeof(*argv
) * 4);
454 argv
[0] = git_proxy_command
;
458 proxy
= xcalloc(1, sizeof(*proxy
));
462 if (start_command(proxy
))
463 die("cannot start proxy %s", argv
[0]);
464 fd
[0] = proxy
->out
; /* read from proxy stdout */
465 fd
[1] = proxy
->in
; /* write to proxy stdin */
469 #define MAX_CMD_LEN 1024
471 static char *get_port(char *host
)
474 char *p
= strchr(host
, ':');
477 long port
= strtol(p
+ 1, &end
, 10);
478 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
487 static struct child_process no_fork
;
490 * This returns a dummy child_process if the transport protocol does not
491 * need fork(2), or a struct child_process object if it does. Once done,
492 * finish the connection with finish_connect() with the value returned from
493 * this function (it is safe to call finish_connect() with NULL to support
496 * If it returns, the connect is successful; it just dies on errors (this
497 * will hopefully be changed in a libification effort, to return NULL when
498 * the connection failed).
500 struct child_process
*git_connect(int fd
[2], const char *url_orig
,
501 const char *prog
, int flags
)
507 struct child_process
*conn
= &no_fork
;
508 enum protocol protocol
= PROTO_LOCAL
;
514 /* Without this we cannot rely on waitpid() to tell
515 * what happened to our children.
517 signal(SIGCHLD
, SIG_DFL
);
519 if (is_url(url_orig
))
520 url
= url_decode(url_orig
);
522 url
= xstrdup(url_orig
);
524 host
= strstr(url
, "://");
527 protocol
= get_protocol(url
);
536 * Don't do destructive transforms with git:// as that
537 * protocol code does '[]' unwrapping of its own.
539 if (host
[0] == '[') {
540 end
= strchr(host
+ 1, ']');
542 if (protocol
!= PROTO_GIT
) {
552 path
= strchr(end
, c
);
553 if (path
&& !has_dos_drive_prefix(end
)) {
555 if (host
!= url
|| path
< strchrnul(host
, '/')) {
556 protocol
= PROTO_SSH
;
558 } else /* '/' in the host part, assume local path */
565 die("No path specified. See 'man git-pull' for valid url syntax");
568 * null-terminate hostname and point path to ~ for URL's like this:
569 * ssh://host.xz/~user/repo
571 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
584 * Add support for ssh port: ssh://host.xy:<port>/...
586 if (protocol
== PROTO_SSH
&& host
!= url
)
587 port
= get_port(end
);
589 if (protocol
== PROTO_GIT
) {
590 /* These underlying connection commands die() if they
593 char *target_host
= xstrdup(host
);
594 if (git_use_proxy(host
))
595 conn
= git_proxy_connect(fd
, host
);
597 git_tcp_connect(fd
, host
, flags
);
599 * Separate original protocol components prog and path
600 * from extended host header with a NUL byte.
602 * Note: Do not add any other headers here! Doing so
603 * will cause older git-daemon servers to crash.
616 conn
= xcalloc(1, sizeof(*conn
));
618 strbuf_init(&cmd
, MAX_CMD_LEN
);
619 strbuf_addstr(&cmd
, prog
);
620 strbuf_addch(&cmd
, ' ');
621 sq_quote_buf(&cmd
, path
);
622 if (cmd
.len
>= MAX_CMD_LEN
)
623 die("command line too long");
625 conn
->in
= conn
->out
= -1;
626 conn
->argv
= arg
= xcalloc(7, sizeof(*arg
));
627 if (protocol
== PROTO_SSH
) {
628 const char *ssh
= getenv("GIT_SSH");
629 int putty
= ssh
&& strcasestr(ssh
, "plink");
630 if (!ssh
) ssh
= "ssh";
633 if (putty
&& !strcasestr(ssh
, "tortoiseplink"))
636 /* P is for PuTTY, p is for OpenSSH */
637 *arg
++ = putty
? "-P" : "-p";
643 /* remove repo-local variables from the environment */
644 conn
->env
= local_repo_env
;
650 if (start_command(conn
))
651 die("unable to fork");
653 fd
[0] = conn
->out
; /* read from child's stdout */
654 fd
[1] = conn
->in
; /* write to child's stdin */
655 strbuf_release(&cmd
);
662 int git_connection_is_socket(struct child_process
*conn
)
664 return conn
== &no_fork
;
667 int finish_connect(struct child_process
*conn
)
670 if (!conn
|| git_connection_is_socket(conn
))
673 code
= finish_command(conn
);