1 #include "git-compat-util.h"
7 #include "run-command.h"
11 #include "string-list.h"
12 #include "sha1-array.h"
13 #include "transport.h"
17 static char *server_capabilities
;
18 static const char *parse_feature_value(const char *, const char *, int *);
20 static int check_ref(const char *name
, unsigned int flags
)
25 if (!skip_prefix(name
, "refs/", &name
))
28 /* REF_NORMAL means that we don't want the magic fake tag refs */
29 if ((flags
& REF_NORMAL
) && check_refname_format(name
, 0))
32 /* REF_HEADS means that we want regular branch heads */
33 if ((flags
& REF_HEADS
) && starts_with(name
, "heads/"))
36 /* REF_TAGS means that we want tags */
37 if ((flags
& REF_TAGS
) && starts_with(name
, "tags/"))
40 /* All type bits clear means that we are ok with anything */
41 return !(flags
& ~REF_NORMAL
);
44 int check_ref_type(const struct ref
*ref
, int flags
)
46 return check_ref(ref
->name
, flags
);
49 static void die_initial_contact(int unexpected
)
52 * A hang-up after seeing some response from the other end
53 * means that it is unexpected, as we know the other end is
54 * willing to talk to us. A hang-up before seeing any
55 * response does not necessarily mean an ACL problem, though.
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 enum protocol_version
discover_version(struct packet_reader
*reader
)
67 enum protocol_version version
= protocol_unknown_version
;
70 * Peek the first line of the server's response to
71 * determine the protocol version the server is speaking.
73 switch (packet_reader_peek(reader
)) {
75 die_initial_contact(0);
76 case PACKET_READ_FLUSH
:
77 case PACKET_READ_DELIM
:
78 version
= protocol_v0
;
80 case PACKET_READ_NORMAL
:
81 version
= determine_protocol_version_client(reader
->line
);
87 die("support for protocol v2 not implemented yet");
90 /* Read the peeked version line */
91 packet_reader_read(reader
);
95 case protocol_unknown_version
:
96 BUG("unknown protocol version");
102 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
105 struct string_list_item
*item
;
108 return; /* just "symref" */
109 /* e.g. "symref=HEAD:refs/heads/master" */
110 sym
= xmemdupz(val
, len
);
111 target
= strchr(sym
, ':');
113 /* just "symref=something" */
116 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
117 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
118 /* "symref=bogus:pair */
120 item
= string_list_append_nodup(symref
, sym
);
128 static void annotate_refs_with_symref_info(struct ref
*ref
)
130 struct string_list symref
= STRING_LIST_INIT_DUP
;
131 const char *feature_list
= server_capabilities
;
133 while (feature_list
) {
137 val
= parse_feature_value(feature_list
, "symref", &len
);
140 parse_one_symref_info(&symref
, val
, len
);
141 feature_list
= val
+ 1;
143 string_list_sort(&symref
);
145 for (; ref
; ref
= ref
->next
) {
146 struct string_list_item
*item
;
147 item
= string_list_lookup(&symref
, ref
->name
);
150 ref
->symref
= xstrdup((char *)item
->util
);
152 string_list_clear(&symref
, 0);
155 static void process_capabilities(const char *line
, int *len
)
157 int nul_location
= strlen(line
);
158 if (nul_location
== *len
)
160 server_capabilities
= xstrdup(line
+ nul_location
+ 1);
164 static int process_dummy_ref(const char *line
)
166 struct object_id oid
;
169 if (parse_oid_hex(line
, &oid
, &name
))
175 return !oidcmp(&null_oid
, &oid
) && !strcmp(name
, "capabilities^{}");
178 static void check_no_capabilities(const char *line
, int len
)
180 if (strlen(line
) != len
)
181 warning("Ignoring capabilities after first line '%s'",
182 line
+ strlen(line
));
185 static int process_ref(const char *line
, int len
, struct ref
***list
,
186 unsigned int flags
, struct oid_array
*extra_have
)
188 struct object_id old_oid
;
191 if (parse_oid_hex(line
, &old_oid
, &name
))
197 if (extra_have
&& !strcmp(name
, ".have")) {
198 oid_array_append(extra_have
, &old_oid
);
199 } else if (!strcmp(name
, "capabilities^{}")) {
200 die("protocol error: unexpected capabilities^{}");
201 } else if (check_ref(name
, flags
)) {
202 struct ref
*ref
= alloc_ref(name
);
203 oidcpy(&ref
->old_oid
, &old_oid
);
207 check_no_capabilities(line
, len
);
211 static int process_shallow(const char *line
, int len
,
212 struct oid_array
*shallow_points
)
215 struct object_id old_oid
;
217 if (!skip_prefix(line
, "shallow ", &arg
))
220 if (get_oid_hex(arg
, &old_oid
))
221 die("protocol error: expected shallow sha-1, got '%s'", arg
);
223 die("repository on the other end cannot be shallow");
224 oid_array_append(shallow_points
, &old_oid
);
225 check_no_capabilities(line
, len
);
229 enum get_remote_heads_state
{
230 EXPECTING_FIRST_REF
= 0,
237 * Read all the refs from the other end
239 struct ref
**get_remote_heads(struct packet_reader
*reader
,
240 struct ref
**list
, unsigned int flags
,
241 struct oid_array
*extra_have
,
242 struct oid_array
*shallow_points
)
244 struct ref
**orig_list
= list
;
246 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
251 while (state
!= EXPECTING_DONE
) {
252 switch (packet_reader_read(reader
)) {
253 case PACKET_READ_EOF
:
254 die_initial_contact(1);
255 case PACKET_READ_NORMAL
:
256 len
= reader
->pktlen
;
257 if (len
> 4 && skip_prefix(reader
->line
, "ERR ", &arg
))
258 die("remote error: %s", arg
);
260 case PACKET_READ_FLUSH
:
261 state
= EXPECTING_DONE
;
263 case PACKET_READ_DELIM
:
264 die("invalid packet");
268 case EXPECTING_FIRST_REF
:
269 process_capabilities(reader
->line
, &len
);
270 if (process_dummy_ref(reader
->line
)) {
271 state
= EXPECTING_SHALLOW
;
274 state
= EXPECTING_REF
;
277 if (process_ref(reader
->line
, len
, &list
, flags
, extra_have
))
279 state
= EXPECTING_SHALLOW
;
281 case EXPECTING_SHALLOW
:
282 if (process_shallow(reader
->line
, len
, shallow_points
))
284 die("protocol error: unexpected '%s'", reader
->line
);
290 annotate_refs_with_symref_info(*orig_list
);
295 static const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
)
302 len
= strlen(feature
);
303 while (*feature_list
) {
304 const char *found
= strstr(feature_list
, feature
);
307 if (feature_list
== found
|| isspace(found
[-1])) {
308 const char *value
= found
+ len
;
309 /* feature with no value (e.g., "thin-pack") */
310 if (!*value
|| isspace(*value
)) {
315 /* feature with a value (e.g., "agent=git/1.2.3") */
316 else if (*value
== '=') {
319 *lenp
= strcspn(value
, " \t\n");
323 * otherwise we matched a substring of another feature;
327 feature_list
= found
+ 1;
332 int parse_feature_request(const char *feature_list
, const char *feature
)
334 return !!parse_feature_value(feature_list
, feature
, NULL
);
337 const char *server_feature_value(const char *feature
, int *len
)
339 return parse_feature_value(server_capabilities
, feature
, len
);
342 int server_supports(const char *feature
)
344 return !!server_feature_value(feature
, NULL
);
354 int url_is_local_not_ssh(const char *url
)
356 const char *colon
= strchr(url
, ':');
357 const char *slash
= strchr(url
, '/');
358 return !colon
|| (slash
&& slash
< colon
) ||
359 has_dos_drive_prefix(url
);
362 static const char *prot_name(enum protocol protocol
)
373 return "unknown protocol";
377 static enum protocol
get_protocol(const char *name
)
379 if (!strcmp(name
, "ssh"))
381 if (!strcmp(name
, "git"))
383 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
385 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
387 if (!strcmp(name
, "file"))
389 die("I don't handle protocol '%s'", name
);
392 static char *host_end(char **hoststart
, int removebrackets
)
394 char *host
= *hoststart
;
396 char *start
= strstr(host
, "@[");
398 start
++; /* Jump over '@' */
401 if (start
[0] == '[') {
402 end
= strchr(start
+ 1, ']');
404 if (removebrackets
) {
406 memmove(start
, start
+ 1, end
- start
);
417 #define STR(s) STR_(s)
419 static void get_host_and_port(char **host
, const char **port
)
422 end
= host_end(host
, 1);
423 colon
= strchr(end
, ':');
425 long portnr
= strtol(colon
+ 1, &end
, 10);
426 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
429 } else if (!colon
[1]) {
435 static void enable_keepalive(int sockfd
)
439 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
440 fprintf(stderr
, "unable to set SO_KEEPALIVE on socket: %s\n",
446 static const char *ai_name(const struct addrinfo
*ai
)
448 static char addr
[NI_MAXHOST
];
449 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
450 NI_NUMERICHOST
) != 0)
451 xsnprintf(addr
, sizeof(addr
), "(unknown)");
457 * Returns a connected socket() fd, or else die()s.
459 static int git_tcp_connect_sock(char *host
, int flags
)
461 struct strbuf error_message
= STRBUF_INIT
;
463 const char *port
= STR(DEFAULT_GIT_PORT
);
464 struct addrinfo hints
, *ai0
, *ai
;
468 get_host_and_port(&host
, &port
);
472 memset(&hints
, 0, sizeof(hints
));
473 if (flags
& CONNECT_IPV4
)
474 hints
.ai_family
= AF_INET
;
475 else if (flags
& CONNECT_IPV6
)
476 hints
.ai_family
= AF_INET6
;
477 hints
.ai_socktype
= SOCK_STREAM
;
478 hints
.ai_protocol
= IPPROTO_TCP
;
480 if (flags
& CONNECT_VERBOSE
)
481 fprintf(stderr
, "Looking up %s ... ", host
);
483 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
485 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
487 if (flags
& CONNECT_VERBOSE
)
488 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
490 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
491 sockfd
= socket(ai
->ai_family
,
492 ai
->ai_socktype
, ai
->ai_protocol
);
494 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
495 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
496 host
, cnt
, ai_name(ai
), strerror(errno
));
502 if (flags
& CONNECT_VERBOSE
)
503 fprintf(stderr
, "%s ", ai_name(ai
));
510 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
512 enable_keepalive(sockfd
);
514 if (flags
& CONNECT_VERBOSE
)
515 fprintf(stderr
, "done.\n");
517 strbuf_release(&error_message
);
525 * Returns a connected socket() fd, or else die()s.
527 static int git_tcp_connect_sock(char *host
, int flags
)
529 struct strbuf error_message
= STRBUF_INIT
;
531 const char *port
= STR(DEFAULT_GIT_PORT
);
534 struct sockaddr_in sa
;
539 get_host_and_port(&host
, &port
);
541 if (flags
& CONNECT_VERBOSE
)
542 fprintf(stderr
, "Looking up %s ... ", host
);
544 he
= gethostbyname(host
);
546 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
547 nport
= strtoul(port
, &ep
, 10);
548 if ( ep
== port
|| *ep
) {
550 struct servent
*se
= getservbyname(port
,"tcp");
552 die("Unknown port %s", port
);
556 if (flags
& CONNECT_VERBOSE
)
557 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
559 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
560 memset(&sa
, 0, sizeof sa
);
561 sa
.sin_family
= he
->h_addrtype
;
562 sa
.sin_port
= htons(nport
);
563 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
565 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
567 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
568 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
571 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
578 if (flags
& CONNECT_VERBOSE
)
579 fprintf(stderr
, "%s ",
580 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
585 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
587 enable_keepalive(sockfd
);
589 if (flags
& CONNECT_VERBOSE
)
590 fprintf(stderr
, "done.\n");
599 * Dummy child_process returned by git_connect() if the transport protocol
600 * does not need fork(2).
602 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
604 int git_connection_is_socket(struct child_process
*conn
)
606 return conn
== &no_fork
;
609 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
611 int sockfd
= git_tcp_connect_sock(host
, flags
);
620 static char *git_proxy_command
;
622 static int git_proxy_command_options(const char *var
, const char *value
,
625 if (!strcmp(var
, "core.gitproxy")) {
629 const char *rhost_name
= cb
;
630 int rhost_len
= strlen(rhost_name
);
632 if (git_proxy_command
)
635 return config_error_nonbool(var
);
637 * ;# matches www.kernel.org as well
638 * gitproxy = netcatter-1 for kernel.org
639 * gitproxy = netcatter-2 for sample.xz
640 * gitproxy = netcatter-default
642 for_pos
= strstr(value
, " for ");
644 /* matches everybody */
645 matchlen
= strlen(value
);
647 hostlen
= strlen(for_pos
+ 5);
648 if (rhost_len
< hostlen
)
650 else if (!strncmp(for_pos
+ 5,
651 rhost_name
+ rhost_len
- hostlen
,
653 ((rhost_len
== hostlen
) ||
654 rhost_name
[rhost_len
- hostlen
-1] == '.'))
655 matchlen
= for_pos
- value
;
660 /* core.gitproxy = none for kernel.org */
662 !memcmp(value
, "none", 4))
664 git_proxy_command
= xmemdupz(value
, matchlen
);
669 return git_default_config(var
, value
, cb
);
672 static int git_use_proxy(const char *host
)
674 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
675 git_config(git_proxy_command_options
, (void*)host
);
676 return (git_proxy_command
&& *git_proxy_command
);
679 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
681 const char *port
= STR(DEFAULT_GIT_PORT
);
682 struct child_process
*proxy
;
684 get_host_and_port(&host
, &port
);
686 if (looks_like_command_line_option(host
))
687 die("strange hostname '%s' blocked", host
);
688 if (looks_like_command_line_option(port
))
689 die("strange port '%s' blocked", port
);
691 proxy
= xmalloc(sizeof(*proxy
));
692 child_process_init(proxy
);
693 argv_array_push(&proxy
->args
, git_proxy_command
);
694 argv_array_push(&proxy
->args
, host
);
695 argv_array_push(&proxy
->args
, port
);
698 if (start_command(proxy
))
699 die("cannot start proxy %s", git_proxy_command
);
700 fd
[0] = proxy
->out
; /* read from proxy stdout */
701 fd
[1] = proxy
->in
; /* write to proxy stdin */
705 static char *get_port(char *host
)
708 char *p
= strchr(host
, ':');
711 long port
= strtol(p
+ 1, &end
, 10);
712 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
722 * Extract protocol and relevant parts from the specified connection URL.
723 * The caller must free() the returned strings.
725 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
732 enum protocol protocol
= PROTO_LOCAL
;
734 if (is_url(url_orig
))
735 url
= url_decode(url_orig
);
737 url
= xstrdup(url_orig
);
739 host
= strstr(url
, "://");
742 protocol
= get_protocol(url
);
746 if (!url_is_local_not_ssh(url
)) {
747 protocol
= PROTO_SSH
;
753 * Don't do destructive transforms as protocol code does
754 * '[]' unwrapping in get_host_and_port()
756 end
= host_end(&host
, 0);
758 if (protocol
== PROTO_LOCAL
)
760 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
761 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
763 path
= strchr(end
, separator
);
766 die("No path specified. See 'man git-pull' for valid url syntax");
769 * null-terminate hostname and point path to ~ for URL's like this:
770 * ssh://host.xz/~user/repo
773 end
= path
; /* Need to \0 terminate host here */
774 if (separator
== ':')
775 path
++; /* path starts after ':' */
776 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
781 path
= xstrdup(path
);
784 *ret_host
= xstrdup(host
);
790 static const char *get_ssh_command(void)
794 if ((ssh
= getenv("GIT_SSH_COMMAND")))
797 if (!git_config_get_string_const("core.sshcommand", &ssh
))
809 VARIANT_TORTOISEPLINK
,
812 static void override_ssh_variant(enum ssh_variant
*ssh_variant
)
814 const char *variant
= getenv("GIT_SSH_VARIANT");
816 if (!variant
&& git_config_get_string_const("ssh.variant", &variant
))
819 if (!strcmp(variant
, "auto"))
820 *ssh_variant
= VARIANT_AUTO
;
821 else if (!strcmp(variant
, "plink"))
822 *ssh_variant
= VARIANT_PLINK
;
823 else if (!strcmp(variant
, "putty"))
824 *ssh_variant
= VARIANT_PUTTY
;
825 else if (!strcmp(variant
, "tortoiseplink"))
826 *ssh_variant
= VARIANT_TORTOISEPLINK
;
827 else if (!strcmp(variant
, "simple"))
828 *ssh_variant
= VARIANT_SIMPLE
;
830 *ssh_variant
= VARIANT_SSH
;
833 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
836 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
840 override_ssh_variant(&ssh_variant
);
842 if (ssh_variant
!= VARIANT_AUTO
)
846 p
= xstrdup(ssh_command
);
847 variant
= basename(p
);
849 const char **ssh_argv
;
851 p
= xstrdup(ssh_command
);
852 if (split_cmdline(p
, &ssh_argv
) > 0) {
853 variant
= basename((char *)ssh_argv
[0]);
855 * At this point, variant points into the buffer
856 * referenced by p, hence we do not need ssh_argv
866 if (!strcasecmp(variant
, "ssh") ||
867 !strcasecmp(variant
, "ssh.exe"))
868 ssh_variant
= VARIANT_SSH
;
869 else if (!strcasecmp(variant
, "plink") ||
870 !strcasecmp(variant
, "plink.exe"))
871 ssh_variant
= VARIANT_PLINK
;
872 else if (!strcasecmp(variant
, "tortoiseplink") ||
873 !strcasecmp(variant
, "tortoiseplink.exe"))
874 ssh_variant
= VARIANT_TORTOISEPLINK
;
881 * Open a connection using Git's native protocol.
883 * The caller is responsible for freeing hostandport, but this function may
884 * modify it (for example, to truncate it to remove the port part).
886 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
887 const char *path
, const char *prog
,
890 struct child_process
*conn
;
891 struct strbuf request
= STRBUF_INIT
;
893 * Set up virtual host information based on where we will
894 * connect, unless the user has overridden us in
897 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
899 target_host
= xstrdup(target_host
);
901 target_host
= xstrdup(hostandport
);
903 transport_check_allowed("git");
906 * These underlying connection commands die() if they
909 if (git_use_proxy(hostandport
))
910 conn
= git_proxy_connect(fd
, hostandport
);
912 conn
= git_tcp_connect(fd
, hostandport
, flags
);
914 * Separate original protocol components prog and path
915 * from extended host header with a NUL byte.
917 * Note: Do not add any other headers here! Doing so
918 * will cause older git-daemon servers to crash.
920 strbuf_addf(&request
,
925 /* If using a new version put that stuff here after a second null byte */
926 if (get_protocol_version_config() > 0) {
927 strbuf_addch(&request
, '\0');
928 strbuf_addf(&request
, "version=%d%c",
929 get_protocol_version_config(), '\0');
932 packet_write(fd
[1], request
.buf
, request
.len
);
935 strbuf_release(&request
);
940 * Append the appropriate environment variables to `env` and options to
941 * `args` for running ssh in Git's SSH-tunneled transport.
943 static void push_ssh_options(struct argv_array
*args
, struct argv_array
*env
,
944 enum ssh_variant variant
, const char *port
,
947 if (variant
== VARIANT_SSH
&&
948 get_protocol_version_config() > 0) {
949 argv_array_push(args
, "-o");
950 argv_array_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
951 argv_array_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
952 get_protocol_version_config());
955 if (flags
& CONNECT_IPV4
) {
958 BUG("VARIANT_AUTO passed to push_ssh_options");
960 die("ssh variant 'simple' does not support -4");
964 case VARIANT_TORTOISEPLINK
:
965 argv_array_push(args
, "-4");
967 } else if (flags
& CONNECT_IPV6
) {
970 BUG("VARIANT_AUTO passed to push_ssh_options");
972 die("ssh variant 'simple' does not support -6");
976 case VARIANT_TORTOISEPLINK
:
977 argv_array_push(args
, "-6");
981 if (variant
== VARIANT_TORTOISEPLINK
)
982 argv_array_push(args
, "-batch");
987 BUG("VARIANT_AUTO passed to push_ssh_options");
989 die("ssh variant 'simple' does not support setting port");
991 argv_array_push(args
, "-p");
995 case VARIANT_TORTOISEPLINK
:
996 argv_array_push(args
, "-P");
999 argv_array_push(args
, port
);
1003 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1004 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
1005 const char *port
, int flags
)
1008 enum ssh_variant variant
;
1010 if (looks_like_command_line_option(ssh_host
))
1011 die("strange hostname '%s' blocked", ssh_host
);
1013 ssh
= get_ssh_command();
1015 variant
= determine_ssh_variant(ssh
, 1);
1018 * GIT_SSH is the no-shell version of
1019 * GIT_SSH_COMMAND (and must remain so for
1020 * historical compatibility).
1022 conn
->use_shell
= 0;
1024 ssh
= getenv("GIT_SSH");
1027 variant
= determine_ssh_variant(ssh
, 0);
1030 if (variant
== VARIANT_AUTO
) {
1031 struct child_process detect
= CHILD_PROCESS_INIT
;
1033 detect
.use_shell
= conn
->use_shell
;
1034 detect
.no_stdin
= detect
.no_stdout
= detect
.no_stderr
= 1;
1036 argv_array_push(&detect
.args
, ssh
);
1037 argv_array_push(&detect
.args
, "-G");
1038 push_ssh_options(&detect
.args
, &detect
.env_array
,
1039 VARIANT_SSH
, port
, flags
);
1040 argv_array_push(&detect
.args
, ssh_host
);
1042 variant
= run_command(&detect
) ? VARIANT_SIMPLE
: VARIANT_SSH
;
1045 argv_array_push(&conn
->args
, ssh
);
1046 push_ssh_options(&conn
->args
, &conn
->env_array
, variant
, port
, flags
);
1047 argv_array_push(&conn
->args
, ssh_host
);
1051 * This returns the dummy child_process `no_fork` if the transport protocol
1052 * does not need fork(2), or a struct child_process object if it does. Once
1053 * done, finish the connection with finish_connect() with the value returned
1054 * from this function (it is safe to call finish_connect() with NULL to
1055 * support the former case).
1057 * If it returns, the connect is successful; it just dies on errors (this
1058 * will hopefully be changed in a libification effort, to return NULL when
1059 * the connection failed).
1061 struct child_process
*git_connect(int fd
[2], const char *url
,
1062 const char *prog
, int flags
)
1064 char *hostandport
, *path
;
1065 struct child_process
*conn
;
1066 enum protocol protocol
;
1068 /* Without this we cannot rely on waitpid() to tell
1069 * what happened to our children.
1071 signal(SIGCHLD
, SIG_DFL
);
1073 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1074 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1075 printf("Diag: url=%s\n", url
? url
: "NULL");
1076 printf("Diag: protocol=%s\n", prot_name(protocol
));
1077 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1078 printf("Diag: path=%s\n", path
? path
: "NULL");
1080 } else if (protocol
== PROTO_GIT
) {
1081 conn
= git_connect_git(fd
, hostandport
, path
, prog
, flags
);
1083 struct strbuf cmd
= STRBUF_INIT
;
1084 const char *const *var
;
1086 conn
= xmalloc(sizeof(*conn
));
1087 child_process_init(conn
);
1089 if (looks_like_command_line_option(path
))
1090 die("strange pathname '%s' blocked", path
);
1092 strbuf_addstr(&cmd
, prog
);
1093 strbuf_addch(&cmd
, ' ');
1094 sq_quote_buf(&cmd
, path
);
1096 /* remove repo-local variables from the environment */
1097 for (var
= local_repo_env
; *var
; var
++)
1098 argv_array_push(&conn
->env_array
, *var
);
1100 conn
->use_shell
= 1;
1101 conn
->in
= conn
->out
= -1;
1102 if (protocol
== PROTO_SSH
) {
1103 char *ssh_host
= hostandport
;
1104 const char *port
= NULL
;
1105 transport_check_allowed("ssh");
1106 get_host_and_port(&ssh_host
, &port
);
1109 port
= get_port(ssh_host
);
1111 if (flags
& CONNECT_DIAG_URL
) {
1112 printf("Diag: url=%s\n", url
? url
: "NULL");
1113 printf("Diag: protocol=%s\n", prot_name(protocol
));
1114 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1115 printf("Diag: port=%s\n", port
? port
: "NONE");
1116 printf("Diag: path=%s\n", path
? path
: "NULL");
1121 strbuf_release(&cmd
);
1124 fill_ssh_args(conn
, ssh_host
, port
, flags
);
1126 transport_check_allowed("file");
1127 if (get_protocol_version_config() > 0) {
1128 argv_array_pushf(&conn
->env_array
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1129 get_protocol_version_config());
1132 argv_array_push(&conn
->args
, cmd
.buf
);
1134 if (start_command(conn
))
1135 die("unable to fork");
1137 fd
[0] = conn
->out
; /* read from child's stdout */
1138 fd
[1] = conn
->in
; /* write to child's stdin */
1139 strbuf_release(&cmd
);
1146 int finish_connect(struct child_process
*conn
)
1149 if (!conn
|| git_connection_is_socket(conn
))
1152 code
= finish_command(conn
);