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 /* Read the peeked version line */
88 packet_reader_read(reader
);
92 case protocol_unknown_version
:
93 BUG("unknown protocol version");
99 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
102 struct string_list_item
*item
;
105 return; /* just "symref" */
106 /* e.g. "symref=HEAD:refs/heads/master" */
107 sym
= xmemdupz(val
, len
);
108 target
= strchr(sym
, ':');
110 /* just "symref=something" */
113 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
114 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
115 /* "symref=bogus:pair */
117 item
= string_list_append_nodup(symref
, sym
);
125 static void annotate_refs_with_symref_info(struct ref
*ref
)
127 struct string_list symref
= STRING_LIST_INIT_DUP
;
128 const char *feature_list
= server_capabilities
;
130 while (feature_list
) {
134 val
= parse_feature_value(feature_list
, "symref", &len
);
137 parse_one_symref_info(&symref
, val
, len
);
138 feature_list
= val
+ 1;
140 string_list_sort(&symref
);
142 for (; ref
; ref
= ref
->next
) {
143 struct string_list_item
*item
;
144 item
= string_list_lookup(&symref
, ref
->name
);
147 ref
->symref
= xstrdup((char *)item
->util
);
149 string_list_clear(&symref
, 0);
152 static void process_capabilities(const char *line
, int *len
)
154 int nul_location
= strlen(line
);
155 if (nul_location
== *len
)
157 server_capabilities
= xstrdup(line
+ nul_location
+ 1);
161 static int process_dummy_ref(const char *line
)
163 struct object_id oid
;
166 if (parse_oid_hex(line
, &oid
, &name
))
172 return !oidcmp(&null_oid
, &oid
) && !strcmp(name
, "capabilities^{}");
175 static void check_no_capabilities(const char *line
, int len
)
177 if (strlen(line
) != len
)
178 warning("Ignoring capabilities after first line '%s'",
179 line
+ strlen(line
));
182 static int process_ref(const char *line
, int len
, struct ref
***list
,
183 unsigned int flags
, struct oid_array
*extra_have
)
185 struct object_id old_oid
;
188 if (parse_oid_hex(line
, &old_oid
, &name
))
194 if (extra_have
&& !strcmp(name
, ".have")) {
195 oid_array_append(extra_have
, &old_oid
);
196 } else if (!strcmp(name
, "capabilities^{}")) {
197 die("protocol error: unexpected capabilities^{}");
198 } else if (check_ref(name
, flags
)) {
199 struct ref
*ref
= alloc_ref(name
);
200 oidcpy(&ref
->old_oid
, &old_oid
);
204 check_no_capabilities(line
, len
);
208 static int process_shallow(const char *line
, int len
,
209 struct oid_array
*shallow_points
)
212 struct object_id old_oid
;
214 if (!skip_prefix(line
, "shallow ", &arg
))
217 if (get_oid_hex(arg
, &old_oid
))
218 die("protocol error: expected shallow sha-1, got '%s'", arg
);
220 die("repository on the other end cannot be shallow");
221 oid_array_append(shallow_points
, &old_oid
);
222 check_no_capabilities(line
, len
);
226 enum get_remote_heads_state
{
227 EXPECTING_FIRST_REF
= 0,
234 * Read all the refs from the other end
236 struct ref
**get_remote_heads(struct packet_reader
*reader
,
237 struct ref
**list
, unsigned int flags
,
238 struct oid_array
*extra_have
,
239 struct oid_array
*shallow_points
)
241 struct ref
**orig_list
= list
;
243 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
248 while (state
!= EXPECTING_DONE
) {
249 switch (packet_reader_read(reader
)) {
250 case PACKET_READ_EOF
:
251 die_initial_contact(1);
252 case PACKET_READ_NORMAL
:
253 len
= reader
->pktlen
;
254 if (len
> 4 && skip_prefix(reader
->line
, "ERR ", &arg
))
255 die("remote error: %s", arg
);
257 case PACKET_READ_FLUSH
:
258 state
= EXPECTING_DONE
;
260 case PACKET_READ_DELIM
:
261 die("invalid packet");
265 case EXPECTING_FIRST_REF
:
266 process_capabilities(reader
->line
, &len
);
267 if (process_dummy_ref(reader
->line
)) {
268 state
= EXPECTING_SHALLOW
;
271 state
= EXPECTING_REF
;
274 if (process_ref(reader
->line
, len
, &list
, flags
, extra_have
))
276 state
= EXPECTING_SHALLOW
;
278 case EXPECTING_SHALLOW
:
279 if (process_shallow(reader
->line
, len
, shallow_points
))
281 die("protocol error: unexpected '%s'", reader
->line
);
287 annotate_refs_with_symref_info(*orig_list
);
292 static const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
)
299 len
= strlen(feature
);
300 while (*feature_list
) {
301 const char *found
= strstr(feature_list
, feature
);
304 if (feature_list
== found
|| isspace(found
[-1])) {
305 const char *value
= found
+ len
;
306 /* feature with no value (e.g., "thin-pack") */
307 if (!*value
|| isspace(*value
)) {
312 /* feature with a value (e.g., "agent=git/1.2.3") */
313 else if (*value
== '=') {
316 *lenp
= strcspn(value
, " \t\n");
320 * otherwise we matched a substring of another feature;
324 feature_list
= found
+ 1;
329 int parse_feature_request(const char *feature_list
, const char *feature
)
331 return !!parse_feature_value(feature_list
, feature
, NULL
);
334 const char *server_feature_value(const char *feature
, int *len
)
336 return parse_feature_value(server_capabilities
, feature
, len
);
339 int server_supports(const char *feature
)
341 return !!server_feature_value(feature
, NULL
);
351 int url_is_local_not_ssh(const char *url
)
353 const char *colon
= strchr(url
, ':');
354 const char *slash
= strchr(url
, '/');
355 return !colon
|| (slash
&& slash
< colon
) ||
356 has_dos_drive_prefix(url
);
359 static const char *prot_name(enum protocol protocol
)
370 return "unknown protocol";
374 static enum protocol
get_protocol(const char *name
)
376 if (!strcmp(name
, "ssh"))
378 if (!strcmp(name
, "git"))
380 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
382 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
384 if (!strcmp(name
, "file"))
386 die("I don't handle protocol '%s'", name
);
389 static char *host_end(char **hoststart
, int removebrackets
)
391 char *host
= *hoststart
;
393 char *start
= strstr(host
, "@[");
395 start
++; /* Jump over '@' */
398 if (start
[0] == '[') {
399 end
= strchr(start
+ 1, ']');
401 if (removebrackets
) {
403 memmove(start
, start
+ 1, end
- start
);
414 #define STR(s) STR_(s)
416 static void get_host_and_port(char **host
, const char **port
)
419 end
= host_end(host
, 1);
420 colon
= strchr(end
, ':');
422 long portnr
= strtol(colon
+ 1, &end
, 10);
423 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
426 } else if (!colon
[1]) {
432 static void enable_keepalive(int sockfd
)
436 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
437 fprintf(stderr
, "unable to set SO_KEEPALIVE on socket: %s\n",
443 static const char *ai_name(const struct addrinfo
*ai
)
445 static char addr
[NI_MAXHOST
];
446 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
447 NI_NUMERICHOST
) != 0)
448 xsnprintf(addr
, sizeof(addr
), "(unknown)");
454 * Returns a connected socket() fd, or else die()s.
456 static int git_tcp_connect_sock(char *host
, int flags
)
458 struct strbuf error_message
= STRBUF_INIT
;
460 const char *port
= STR(DEFAULT_GIT_PORT
);
461 struct addrinfo hints
, *ai0
, *ai
;
465 get_host_and_port(&host
, &port
);
469 memset(&hints
, 0, sizeof(hints
));
470 if (flags
& CONNECT_IPV4
)
471 hints
.ai_family
= AF_INET
;
472 else if (flags
& CONNECT_IPV6
)
473 hints
.ai_family
= AF_INET6
;
474 hints
.ai_socktype
= SOCK_STREAM
;
475 hints
.ai_protocol
= IPPROTO_TCP
;
477 if (flags
& CONNECT_VERBOSE
)
478 fprintf(stderr
, "Looking up %s ... ", host
);
480 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
482 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
484 if (flags
& CONNECT_VERBOSE
)
485 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
487 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
488 sockfd
= socket(ai
->ai_family
,
489 ai
->ai_socktype
, ai
->ai_protocol
);
491 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
492 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
493 host
, cnt
, ai_name(ai
), strerror(errno
));
499 if (flags
& CONNECT_VERBOSE
)
500 fprintf(stderr
, "%s ", ai_name(ai
));
507 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
509 enable_keepalive(sockfd
);
511 if (flags
& CONNECT_VERBOSE
)
512 fprintf(stderr
, "done.\n");
514 strbuf_release(&error_message
);
522 * Returns a connected socket() fd, or else die()s.
524 static int git_tcp_connect_sock(char *host
, int flags
)
526 struct strbuf error_message
= STRBUF_INIT
;
528 const char *port
= STR(DEFAULT_GIT_PORT
);
531 struct sockaddr_in sa
;
536 get_host_and_port(&host
, &port
);
538 if (flags
& CONNECT_VERBOSE
)
539 fprintf(stderr
, "Looking up %s ... ", host
);
541 he
= gethostbyname(host
);
543 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
544 nport
= strtoul(port
, &ep
, 10);
545 if ( ep
== port
|| *ep
) {
547 struct servent
*se
= getservbyname(port
,"tcp");
549 die("Unknown port %s", port
);
553 if (flags
& CONNECT_VERBOSE
)
554 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
556 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
557 memset(&sa
, 0, sizeof sa
);
558 sa
.sin_family
= he
->h_addrtype
;
559 sa
.sin_port
= htons(nport
);
560 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
562 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
564 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
565 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
568 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
575 if (flags
& CONNECT_VERBOSE
)
576 fprintf(stderr
, "%s ",
577 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
582 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
584 enable_keepalive(sockfd
);
586 if (flags
& CONNECT_VERBOSE
)
587 fprintf(stderr
, "done.\n");
596 * Dummy child_process returned by git_connect() if the transport protocol
597 * does not need fork(2).
599 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
601 int git_connection_is_socket(struct child_process
*conn
)
603 return conn
== &no_fork
;
606 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
608 int sockfd
= git_tcp_connect_sock(host
, flags
);
617 static char *git_proxy_command
;
619 static int git_proxy_command_options(const char *var
, const char *value
,
622 if (!strcmp(var
, "core.gitproxy")) {
626 const char *rhost_name
= cb
;
627 int rhost_len
= strlen(rhost_name
);
629 if (git_proxy_command
)
632 return config_error_nonbool(var
);
634 * ;# matches www.kernel.org as well
635 * gitproxy = netcatter-1 for kernel.org
636 * gitproxy = netcatter-2 for sample.xz
637 * gitproxy = netcatter-default
639 for_pos
= strstr(value
, " for ");
641 /* matches everybody */
642 matchlen
= strlen(value
);
644 hostlen
= strlen(for_pos
+ 5);
645 if (rhost_len
< hostlen
)
647 else if (!strncmp(for_pos
+ 5,
648 rhost_name
+ rhost_len
- hostlen
,
650 ((rhost_len
== hostlen
) ||
651 rhost_name
[rhost_len
- hostlen
-1] == '.'))
652 matchlen
= for_pos
- value
;
657 /* core.gitproxy = none for kernel.org */
659 !memcmp(value
, "none", 4))
661 git_proxy_command
= xmemdupz(value
, matchlen
);
666 return git_default_config(var
, value
, cb
);
669 static int git_use_proxy(const char *host
)
671 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
672 git_config(git_proxy_command_options
, (void*)host
);
673 return (git_proxy_command
&& *git_proxy_command
);
676 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
678 const char *port
= STR(DEFAULT_GIT_PORT
);
679 struct child_process
*proxy
;
681 get_host_and_port(&host
, &port
);
683 if (looks_like_command_line_option(host
))
684 die("strange hostname '%s' blocked", host
);
685 if (looks_like_command_line_option(port
))
686 die("strange port '%s' blocked", port
);
688 proxy
= xmalloc(sizeof(*proxy
));
689 child_process_init(proxy
);
690 argv_array_push(&proxy
->args
, git_proxy_command
);
691 argv_array_push(&proxy
->args
, host
);
692 argv_array_push(&proxy
->args
, port
);
695 if (start_command(proxy
))
696 die("cannot start proxy %s", git_proxy_command
);
697 fd
[0] = proxy
->out
; /* read from proxy stdout */
698 fd
[1] = proxy
->in
; /* write to proxy stdin */
702 static char *get_port(char *host
)
705 char *p
= strchr(host
, ':');
708 long port
= strtol(p
+ 1, &end
, 10);
709 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
719 * Extract protocol and relevant parts from the specified connection URL.
720 * The caller must free() the returned strings.
722 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
729 enum protocol protocol
= PROTO_LOCAL
;
731 if (is_url(url_orig
))
732 url
= url_decode(url_orig
);
734 url
= xstrdup(url_orig
);
736 host
= strstr(url
, "://");
739 protocol
= get_protocol(url
);
743 if (!url_is_local_not_ssh(url
)) {
744 protocol
= PROTO_SSH
;
750 * Don't do destructive transforms as protocol code does
751 * '[]' unwrapping in get_host_and_port()
753 end
= host_end(&host
, 0);
755 if (protocol
== PROTO_LOCAL
)
757 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
758 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
760 path
= strchr(end
, separator
);
763 die("No path specified. See 'man git-pull' for valid url syntax");
766 * null-terminate hostname and point path to ~ for URL's like this:
767 * ssh://host.xz/~user/repo
770 end
= path
; /* Need to \0 terminate host here */
771 if (separator
== ':')
772 path
++; /* path starts after ':' */
773 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
778 path
= xstrdup(path
);
781 *ret_host
= xstrdup(host
);
787 static const char *get_ssh_command(void)
791 if ((ssh
= getenv("GIT_SSH_COMMAND")))
794 if (!git_config_get_string_const("core.sshcommand", &ssh
))
806 VARIANT_TORTOISEPLINK
,
809 static void override_ssh_variant(enum ssh_variant
*ssh_variant
)
811 const char *variant
= getenv("GIT_SSH_VARIANT");
813 if (!variant
&& git_config_get_string_const("ssh.variant", &variant
))
816 if (!strcmp(variant
, "auto"))
817 *ssh_variant
= VARIANT_AUTO
;
818 else if (!strcmp(variant
, "plink"))
819 *ssh_variant
= VARIANT_PLINK
;
820 else if (!strcmp(variant
, "putty"))
821 *ssh_variant
= VARIANT_PUTTY
;
822 else if (!strcmp(variant
, "tortoiseplink"))
823 *ssh_variant
= VARIANT_TORTOISEPLINK
;
824 else if (!strcmp(variant
, "simple"))
825 *ssh_variant
= VARIANT_SIMPLE
;
827 *ssh_variant
= VARIANT_SSH
;
830 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
833 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
837 override_ssh_variant(&ssh_variant
);
839 if (ssh_variant
!= VARIANT_AUTO
)
843 p
= xstrdup(ssh_command
);
844 variant
= basename(p
);
846 const char **ssh_argv
;
848 p
= xstrdup(ssh_command
);
849 if (split_cmdline(p
, &ssh_argv
) > 0) {
850 variant
= basename((char *)ssh_argv
[0]);
852 * At this point, variant points into the buffer
853 * referenced by p, hence we do not need ssh_argv
863 if (!strcasecmp(variant
, "ssh") ||
864 !strcasecmp(variant
, "ssh.exe"))
865 ssh_variant
= VARIANT_SSH
;
866 else if (!strcasecmp(variant
, "plink") ||
867 !strcasecmp(variant
, "plink.exe"))
868 ssh_variant
= VARIANT_PLINK
;
869 else if (!strcasecmp(variant
, "tortoiseplink") ||
870 !strcasecmp(variant
, "tortoiseplink.exe"))
871 ssh_variant
= VARIANT_TORTOISEPLINK
;
878 * Open a connection using Git's native protocol.
880 * The caller is responsible for freeing hostandport, but this function may
881 * modify it (for example, to truncate it to remove the port part).
883 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
884 const char *path
, const char *prog
,
887 struct child_process
*conn
;
888 struct strbuf request
= STRBUF_INIT
;
890 * Set up virtual host information based on where we will
891 * connect, unless the user has overridden us in
894 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
896 target_host
= xstrdup(target_host
);
898 target_host
= xstrdup(hostandport
);
900 transport_check_allowed("git");
903 * These underlying connection commands die() if they
906 if (git_use_proxy(hostandport
))
907 conn
= git_proxy_connect(fd
, hostandport
);
909 conn
= git_tcp_connect(fd
, hostandport
, flags
);
911 * Separate original protocol components prog and path
912 * from extended host header with a NUL byte.
914 * Note: Do not add any other headers here! Doing so
915 * will cause older git-daemon servers to crash.
917 strbuf_addf(&request
,
922 /* If using a new version put that stuff here after a second null byte */
923 if (get_protocol_version_config() > 0) {
924 strbuf_addch(&request
, '\0');
925 strbuf_addf(&request
, "version=%d%c",
926 get_protocol_version_config(), '\0');
929 packet_write(fd
[1], request
.buf
, request
.len
);
932 strbuf_release(&request
);
937 * Append the appropriate environment variables to `env` and options to
938 * `args` for running ssh in Git's SSH-tunneled transport.
940 static void push_ssh_options(struct argv_array
*args
, struct argv_array
*env
,
941 enum ssh_variant variant
, const char *port
,
944 if (variant
== VARIANT_SSH
&&
945 get_protocol_version_config() > 0) {
946 argv_array_push(args
, "-o");
947 argv_array_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
948 argv_array_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
949 get_protocol_version_config());
952 if (flags
& CONNECT_IPV4
) {
955 BUG("VARIANT_AUTO passed to push_ssh_options");
957 die("ssh variant 'simple' does not support -4");
961 case VARIANT_TORTOISEPLINK
:
962 argv_array_push(args
, "-4");
964 } else if (flags
& CONNECT_IPV6
) {
967 BUG("VARIANT_AUTO passed to push_ssh_options");
969 die("ssh variant 'simple' does not support -6");
973 case VARIANT_TORTOISEPLINK
:
974 argv_array_push(args
, "-6");
978 if (variant
== VARIANT_TORTOISEPLINK
)
979 argv_array_push(args
, "-batch");
984 BUG("VARIANT_AUTO passed to push_ssh_options");
986 die("ssh variant 'simple' does not support setting port");
988 argv_array_push(args
, "-p");
992 case VARIANT_TORTOISEPLINK
:
993 argv_array_push(args
, "-P");
996 argv_array_push(args
, port
);
1000 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1001 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
1002 const char *port
, int flags
)
1005 enum ssh_variant variant
;
1007 if (looks_like_command_line_option(ssh_host
))
1008 die("strange hostname '%s' blocked", ssh_host
);
1010 ssh
= get_ssh_command();
1012 variant
= determine_ssh_variant(ssh
, 1);
1015 * GIT_SSH is the no-shell version of
1016 * GIT_SSH_COMMAND (and must remain so for
1017 * historical compatibility).
1019 conn
->use_shell
= 0;
1021 ssh
= getenv("GIT_SSH");
1024 variant
= determine_ssh_variant(ssh
, 0);
1027 if (variant
== VARIANT_AUTO
) {
1028 struct child_process detect
= CHILD_PROCESS_INIT
;
1030 detect
.use_shell
= conn
->use_shell
;
1031 detect
.no_stdin
= detect
.no_stdout
= detect
.no_stderr
= 1;
1033 argv_array_push(&detect
.args
, ssh
);
1034 argv_array_push(&detect
.args
, "-G");
1035 push_ssh_options(&detect
.args
, &detect
.env_array
,
1036 VARIANT_SSH
, port
, flags
);
1037 argv_array_push(&detect
.args
, ssh_host
);
1039 variant
= run_command(&detect
) ? VARIANT_SIMPLE
: VARIANT_SSH
;
1042 argv_array_push(&conn
->args
, ssh
);
1043 push_ssh_options(&conn
->args
, &conn
->env_array
, variant
, port
, flags
);
1044 argv_array_push(&conn
->args
, ssh_host
);
1048 * This returns the dummy child_process `no_fork` if the transport protocol
1049 * does not need fork(2), or a struct child_process object if it does. Once
1050 * done, finish the connection with finish_connect() with the value returned
1051 * from this function (it is safe to call finish_connect() with NULL to
1052 * support the former case).
1054 * If it returns, the connect is successful; it just dies on errors (this
1055 * will hopefully be changed in a libification effort, to return NULL when
1056 * the connection failed).
1058 struct child_process
*git_connect(int fd
[2], const char *url
,
1059 const char *prog
, int flags
)
1061 char *hostandport
, *path
;
1062 struct child_process
*conn
;
1063 enum protocol protocol
;
1065 /* Without this we cannot rely on waitpid() to tell
1066 * what happened to our children.
1068 signal(SIGCHLD
, SIG_DFL
);
1070 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1071 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1072 printf("Diag: url=%s\n", url
? url
: "NULL");
1073 printf("Diag: protocol=%s\n", prot_name(protocol
));
1074 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1075 printf("Diag: path=%s\n", path
? path
: "NULL");
1077 } else if (protocol
== PROTO_GIT
) {
1078 conn
= git_connect_git(fd
, hostandport
, path
, prog
, flags
);
1080 struct strbuf cmd
= STRBUF_INIT
;
1081 const char *const *var
;
1083 conn
= xmalloc(sizeof(*conn
));
1084 child_process_init(conn
);
1086 if (looks_like_command_line_option(path
))
1087 die("strange pathname '%s' blocked", path
);
1089 strbuf_addstr(&cmd
, prog
);
1090 strbuf_addch(&cmd
, ' ');
1091 sq_quote_buf(&cmd
, path
);
1093 /* remove repo-local variables from the environment */
1094 for (var
= local_repo_env
; *var
; var
++)
1095 argv_array_push(&conn
->env_array
, *var
);
1097 conn
->use_shell
= 1;
1098 conn
->in
= conn
->out
= -1;
1099 if (protocol
== PROTO_SSH
) {
1100 char *ssh_host
= hostandport
;
1101 const char *port
= NULL
;
1102 transport_check_allowed("ssh");
1103 get_host_and_port(&ssh_host
, &port
);
1106 port
= get_port(ssh_host
);
1108 if (flags
& CONNECT_DIAG_URL
) {
1109 printf("Diag: url=%s\n", url
? url
: "NULL");
1110 printf("Diag: protocol=%s\n", prot_name(protocol
));
1111 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1112 printf("Diag: port=%s\n", port
? port
: "NONE");
1113 printf("Diag: path=%s\n", path
? path
: "NULL");
1118 strbuf_release(&cmd
);
1121 fill_ssh_args(conn
, ssh_host
, port
, flags
);
1123 transport_check_allowed("file");
1124 if (get_protocol_version_config() > 0) {
1125 argv_array_pushf(&conn
->env_array
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1126 get_protocol_version_config());
1129 argv_array_push(&conn
->args
, cmd
.buf
);
1131 if (start_command(conn
))
1132 die("unable to fork");
1134 fd
[0] = conn
->out
; /* read from child's stdout */
1135 fd
[1] = conn
->in
; /* write to child's stdin */
1136 strbuf_release(&cmd
);
1143 int finish_connect(struct child_process
*conn
)
1146 if (!conn
|| git_connection_is_socket(conn
))
1149 code
= finish_command(conn
);