1 #include "git-compat-util.h"
6 #include "run-command.h"
10 #include "string-list.h"
12 static char *server_capabilities
;
13 static const char *parse_feature_value(const char *, const char *, int *);
15 static int check_ref(const char *name
, int len
, unsigned int flags
)
20 if (len
< 5 || memcmp(name
, "refs/", 5))
23 /* Skip the "refs/" part */
27 /* REF_NORMAL means that we don't want the magic fake tag refs */
28 if ((flags
& REF_NORMAL
) && check_refname_format(name
, 0))
31 /* REF_HEADS means that we want regular branch heads */
32 if ((flags
& REF_HEADS
) && !memcmp(name
, "heads/", 6))
35 /* REF_TAGS means that we want tags */
36 if ((flags
& REF_TAGS
) && !memcmp(name
, "tags/", 5))
39 /* All type bits clear means that we are ok with anything */
40 return !(flags
& ~REF_NORMAL
);
43 int check_ref_type(const struct ref
*ref
, int flags
)
45 return check_ref(ref
->name
, strlen(ref
->name
), flags
);
48 static void add_extra_have(struct extra_have_objects
*extra
, unsigned char *sha1
)
50 ALLOC_GROW(extra
->array
, extra
->nr
+ 1, extra
->alloc
);
51 hashcpy(&(extra
->array
[extra
->nr
][0]), sha1
);
55 static void die_initial_contact(int got_at_least_one_head
)
57 if (got_at_least_one_head
)
58 die("The remote end hung up upon initial contact");
60 die("Could not read from remote repository.\n\n"
61 "Please make sure you have the correct access rights\n"
62 "and the repository exists.");
65 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
68 struct string_list_item
*item
;
71 return; /* just "symref" */
72 /* e.g. "symref=HEAD:refs/heads/master" */
73 sym
= xmalloc(len
+ 1);
74 memcpy(sym
, val
, len
);
76 target
= strchr(sym
, ':');
78 /* just "symref=something" */
81 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
82 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
83 /* "symref=bogus:pair */
85 item
= string_list_append(symref
, sym
);
93 static void annotate_refs_with_symref_info(struct ref
*ref
)
95 struct string_list symref
= STRING_LIST_INIT_DUP
;
96 const char *feature_list
= server_capabilities
;
98 while (feature_list
) {
102 val
= parse_feature_value(feature_list
, "symref", &len
);
105 parse_one_symref_info(&symref
, val
, len
);
106 feature_list
= val
+ 1;
108 sort_string_list(&symref
);
110 for (; ref
; ref
= ref
->next
) {
111 struct string_list_item
*item
;
112 item
= string_list_lookup(&symref
, ref
->name
);
115 ref
->symref
= xstrdup((char *)item
->util
);
117 string_list_clear(&symref
, 0);
121 * Read all the refs from the other end
123 struct ref
**get_remote_heads(int in
, char *src_buf
, size_t src_len
,
124 struct ref
**list
, unsigned int flags
,
125 struct extra_have_objects
*extra_have
)
127 struct ref
**orig_list
= list
;
128 int got_at_least_one_head
= 0;
133 unsigned char old_sha1
[20];
136 char *buffer
= packet_buffer
;
138 len
= packet_read(in
, &src_buf
, &src_len
,
139 packet_buffer
, sizeof(packet_buffer
),
140 PACKET_READ_GENTLE_ON_EOF
|
141 PACKET_READ_CHOMP_NEWLINE
);
143 die_initial_contact(got_at_least_one_head
);
148 if (len
> 4 && !prefixcmp(buffer
, "ERR "))
149 die("remote error: %s", buffer
+ 4);
151 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
152 die("protocol error: expected sha/ref, got '%s'", buffer
);
155 name_len
= strlen(name
);
156 if (len
!= name_len
+ 41) {
157 free(server_capabilities
);
158 server_capabilities
= xstrdup(name
+ name_len
+ 1);
162 name_len
== 5 && !memcmp(".have", name
, 5)) {
163 add_extra_have(extra_have
, old_sha1
);
167 if (!check_ref(name
, name_len
, flags
))
169 ref
= alloc_ref(buffer
+ 41);
170 hashcpy(ref
->old_sha1
, old_sha1
);
173 got_at_least_one_head
= 1;
176 annotate_refs_with_symref_info(*orig_list
);
181 static const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
)
188 len
= strlen(feature
);
189 while (*feature_list
) {
190 const char *found
= strstr(feature_list
, feature
);
193 if (feature_list
== found
|| isspace(found
[-1])) {
194 const char *value
= found
+ len
;
195 /* feature with no value (e.g., "thin-pack") */
196 if (!*value
|| isspace(*value
)) {
201 /* feature with a value (e.g., "agent=git/1.2.3") */
202 else if (*value
== '=') {
205 *lenp
= strcspn(value
, " \t\n");
209 * otherwise we matched a substring of another feature;
213 feature_list
= found
+ 1;
218 int parse_feature_request(const char *feature_list
, const char *feature
)
220 return !!parse_feature_value(feature_list
, feature
, NULL
);
223 const char *server_feature_value(const char *feature
, int *len
)
225 return parse_feature_value(server_capabilities
, feature
, len
);
228 int server_supports(const char *feature
)
230 return !!server_feature_value(feature
, NULL
);
240 int url_is_local_not_ssh(const char *url
)
242 const char *colon
= strchr(url
, ':');
243 const char *slash
= strchr(url
, '/');
244 return !colon
|| (slash
&& slash
< colon
) ||
245 has_dos_drive_prefix(url
);
248 static const char *prot_name(enum protocol protocol
)
259 return "unkown protocol";
263 static enum protocol
get_protocol(const char *name
)
265 if (!strcmp(name
, "ssh"))
267 if (!strcmp(name
, "git"))
269 if (!strcmp(name
, "git+ssh"))
271 if (!strcmp(name
, "ssh+git"))
273 if (!strcmp(name
, "file"))
275 die("I don't handle protocol '%s'", name
);
279 #define STR(s) STR_(s)
281 static void get_host_and_port(char **host
, const char **port
)
285 if (*host
[0] == '[') {
286 end
= strchr(*host
+ 1, ']');
295 colon
= strchr(end
, ':');
303 static void enable_keepalive(int sockfd
)
307 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
308 fprintf(stderr
, "unable to set SO_KEEPALIVE on socket: %s\n",
314 static const char *ai_name(const struct addrinfo
*ai
)
316 static char addr
[NI_MAXHOST
];
317 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
318 NI_NUMERICHOST
) != 0)
319 strcpy(addr
, "(unknown)");
325 * Returns a connected socket() fd, or else die()s.
327 static int git_tcp_connect_sock(char *host
, int flags
)
329 struct strbuf error_message
= STRBUF_INIT
;
331 const char *port
= STR(DEFAULT_GIT_PORT
);
332 struct addrinfo hints
, *ai0
, *ai
;
336 get_host_and_port(&host
, &port
);
340 memset(&hints
, 0, sizeof(hints
));
341 hints
.ai_socktype
= SOCK_STREAM
;
342 hints
.ai_protocol
= IPPROTO_TCP
;
344 if (flags
& CONNECT_VERBOSE
)
345 fprintf(stderr
, "Looking up %s ... ", host
);
347 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
349 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
351 if (flags
& CONNECT_VERBOSE
)
352 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
354 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
355 sockfd
= socket(ai
->ai_family
,
356 ai
->ai_socktype
, ai
->ai_protocol
);
358 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
359 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
360 host
, cnt
, ai_name(ai
), strerror(errno
));
366 if (flags
& CONNECT_VERBOSE
)
367 fprintf(stderr
, "%s ", ai_name(ai
));
374 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
376 enable_keepalive(sockfd
);
378 if (flags
& CONNECT_VERBOSE
)
379 fprintf(stderr
, "done.\n");
381 strbuf_release(&error_message
);
389 * Returns a connected socket() fd, or else die()s.
391 static int git_tcp_connect_sock(char *host
, int flags
)
393 struct strbuf error_message
= STRBUF_INIT
;
395 const char *port
= STR(DEFAULT_GIT_PORT
);
398 struct sockaddr_in sa
;
403 get_host_and_port(&host
, &port
);
405 if (flags
& CONNECT_VERBOSE
)
406 fprintf(stderr
, "Looking up %s ... ", host
);
408 he
= gethostbyname(host
);
410 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
411 nport
= strtoul(port
, &ep
, 10);
412 if ( ep
== port
|| *ep
) {
414 struct servent
*se
= getservbyname(port
,"tcp");
416 die("Unknown port %s", port
);
420 if (flags
& CONNECT_VERBOSE
)
421 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
423 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
424 memset(&sa
, 0, sizeof sa
);
425 sa
.sin_family
= he
->h_addrtype
;
426 sa
.sin_port
= htons(nport
);
427 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
429 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
431 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
432 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
435 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
442 if (flags
& CONNECT_VERBOSE
)
443 fprintf(stderr
, "%s ",
444 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
449 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
451 enable_keepalive(sockfd
);
453 if (flags
& CONNECT_VERBOSE
)
454 fprintf(stderr
, "done.\n");
462 static void git_tcp_connect(int fd
[2], char *host
, int flags
)
464 int sockfd
= git_tcp_connect_sock(host
, flags
);
471 static char *git_proxy_command
;
473 static int git_proxy_command_options(const char *var
, const char *value
,
476 if (!strcmp(var
, "core.gitproxy")) {
480 const char *rhost_name
= cb
;
481 int rhost_len
= strlen(rhost_name
);
483 if (git_proxy_command
)
486 return config_error_nonbool(var
);
488 * ;# matches www.kernel.org as well
489 * gitproxy = netcatter-1 for kernel.org
490 * gitproxy = netcatter-2 for sample.xz
491 * gitproxy = netcatter-default
493 for_pos
= strstr(value
, " for ");
495 /* matches everybody */
496 matchlen
= strlen(value
);
498 hostlen
= strlen(for_pos
+ 5);
499 if (rhost_len
< hostlen
)
501 else if (!strncmp(for_pos
+ 5,
502 rhost_name
+ rhost_len
- hostlen
,
504 ((rhost_len
== hostlen
) ||
505 rhost_name
[rhost_len
- hostlen
-1] == '.'))
506 matchlen
= for_pos
- value
;
511 /* core.gitproxy = none for kernel.org */
513 !memcmp(value
, "none", 4))
515 git_proxy_command
= xmemdupz(value
, matchlen
);
520 return git_default_config(var
, value
, cb
);
523 static int git_use_proxy(const char *host
)
525 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
526 git_config(git_proxy_command_options
, (void*)host
);
527 return (git_proxy_command
&& *git_proxy_command
);
530 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
532 const char *port
= STR(DEFAULT_GIT_PORT
);
534 struct child_process
*proxy
;
536 get_host_and_port(&host
, &port
);
538 argv
= xmalloc(sizeof(*argv
) * 4);
539 argv
[0] = git_proxy_command
;
543 proxy
= xcalloc(1, sizeof(*proxy
));
547 if (start_command(proxy
))
548 die("cannot start proxy %s", argv
[0]);
549 fd
[0] = proxy
->out
; /* read from proxy stdout */
550 fd
[1] = proxy
->in
; /* write to proxy stdin */
554 static const char *get_port_numeric(const char *p
)
558 long port
= strtol(p
+ 1, &end
, 10);
559 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
568 * Extract protocol and relevant parts from the specified connection URL.
569 * The caller must free() the returned strings.
571 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
578 enum protocol protocol
= PROTO_LOCAL
;
580 if (is_url(url_orig
))
581 url
= url_decode(url_orig
);
583 url
= xstrdup(url_orig
);
585 host
= strstr(url
, "://");
588 protocol
= get_protocol(url
);
592 if (!url_is_local_not_ssh(url
)) {
593 protocol
= PROTO_SSH
;
599 * Don't do destructive transforms as protocol code does
600 * '[]' unwrapping in get_host_and_port()
602 if (host
[0] == '[') {
603 end
= strchr(host
+ 1, ']');
611 if (protocol
== PROTO_LOCAL
)
613 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
614 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
616 path
= strchr(end
, separator
);
619 die("No path specified. See 'man git-pull' for valid url syntax");
622 * null-terminate hostname and point path to ~ for URL's like this:
623 * ssh://host.xz/~user/repo
626 end
= path
; /* Need to \0 terminate host here */
627 if (separator
== ':')
628 path
++; /* path starts after ':' */
629 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
634 path
= xstrdup(path
);
637 *ret_host
= xstrdup(host
);
643 static struct child_process no_fork
;
646 * This returns a dummy child_process if the transport protocol does not
647 * need fork(2), or a struct child_process object if it does. Once done,
648 * finish the connection with finish_connect() with the value returned from
649 * this function (it is safe to call finish_connect() with NULL to support
652 * If it returns, the connect is successful; it just dies on errors (this
653 * will hopefully be changed in a libification effort, to return NULL when
654 * the connection failed).
656 struct child_process
*git_connect(int fd
[2], const char *url
,
657 const char *prog
, int flags
)
659 char *hostandport
, *path
;
660 struct child_process
*conn
= &no_fork
;
661 enum protocol protocol
;
663 struct strbuf cmd
= STRBUF_INIT
;
665 /* Without this we cannot rely on waitpid() to tell
666 * what happened to our children.
668 signal(SIGCHLD
, SIG_DFL
);
670 protocol
= parse_connect_url(url
, &hostandport
, &path
);
671 if (flags
& CONNECT_DIAG_URL
) {
672 printf("Diag: url=%s\n", url
? url
: "NULL");
673 printf("Diag: protocol=%s\n", prot_name(protocol
));
674 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
675 printf("Diag: path=%s\n", path
? path
: "NULL");
677 } else if (protocol
== PROTO_GIT
) {
678 /* These underlying connection commands die() if they
681 char *target_host
= xstrdup(hostandport
);
682 if (git_use_proxy(hostandport
))
683 conn
= git_proxy_connect(fd
, hostandport
);
685 git_tcp_connect(fd
, hostandport
, flags
);
687 * Separate original protocol components prog and path
688 * from extended host header with a NUL byte.
690 * Note: Do not add any other headers here! Doing so
691 * will cause older git-daemon servers to crash.
699 conn
= xcalloc(1, sizeof(*conn
));
701 strbuf_addstr(&cmd
, prog
);
702 strbuf_addch(&cmd
, ' ');
703 sq_quote_buf(&cmd
, path
);
705 conn
->in
= conn
->out
= -1;
706 conn
->argv
= arg
= xcalloc(7, sizeof(*arg
));
707 if (protocol
== PROTO_SSH
) {
708 const char *ssh
= getenv("GIT_SSH");
709 int putty
= ssh
&& strcasestr(ssh
, "plink");
710 char *ssh_host
= hostandport
;
711 const char *port
= NULL
;
712 get_host_and_port(&ssh_host
, &port
);
713 port
= get_port_numeric(port
);
715 if (!ssh
) ssh
= "ssh";
718 if (putty
&& !strcasestr(ssh
, "tortoiseplink"))
721 /* P is for PuTTY, p is for OpenSSH */
722 *arg
++ = putty
? "-P" : "-p";
727 /* remove repo-local variables from the environment */
728 conn
->env
= local_repo_env
;
734 if (start_command(conn
))
735 die("unable to fork");
737 fd
[0] = conn
->out
; /* read from child's stdout */
738 fd
[1] = conn
->in
; /* write to child's stdin */
739 strbuf_release(&cmd
);
746 int git_connection_is_socket(struct child_process
*conn
)
748 return conn
== &no_fork
;
751 int finish_connect(struct child_process
*conn
)
754 if (!conn
|| git_connection_is_socket(conn
))
757 code
= finish_command(conn
);