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 die(_("The remote end hung up upon initial contact"));
54 die(_("Could not read from remote repository.\n\n"
55 "Please make sure you have the correct access rights\n"
56 "and the repository exists."));
59 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
62 struct string_list_item
*item
;
65 return; /* just "symref" */
66 /* e.g. "symref=HEAD:refs/heads/master" */
67 sym
= xmemdupz(val
, len
);
68 target
= strchr(sym
, ':');
70 /* just "symref=something" */
73 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
74 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
75 /* "symref=bogus:pair */
77 item
= string_list_append_nodup(symref
, sym
);
85 static void annotate_refs_with_symref_info(struct ref
*ref
)
87 struct string_list symref
= STRING_LIST_INIT_DUP
;
88 const char *feature_list
= server_capabilities
;
90 while (feature_list
) {
94 val
= parse_feature_value(feature_list
, "symref", &len
);
97 parse_one_symref_info(&symref
, val
, len
);
98 feature_list
= val
+ 1;
100 string_list_sort(&symref
);
102 for (; ref
; ref
= ref
->next
) {
103 struct string_list_item
*item
;
104 item
= string_list_lookup(&symref
, ref
->name
);
107 ref
->symref
= xstrdup((char *)item
->util
);
109 string_list_clear(&symref
, 0);
113 * Read one line of a server's ref advertisement into packet_buffer.
115 static int read_remote_ref(int in
, char **src_buf
, size_t *src_len
,
118 int len
= packet_read(in
, src_buf
, src_len
,
119 packet_buffer
, sizeof(packet_buffer
),
120 PACKET_READ_GENTLE_ON_EOF
|
121 PACKET_READ_CHOMP_NEWLINE
);
124 die_initial_contact(*responded
);
125 if (len
> 4 && skip_prefix(packet_buffer
, "ERR ", &arg
))
126 die("remote error: %s", arg
);
133 #define EXPECTING_PROTOCOL_VERSION 0
134 #define EXPECTING_FIRST_REF 1
135 #define EXPECTING_REF 2
136 #define EXPECTING_SHALLOW 3
138 /* Returns 1 if packet_buffer is a protocol version pkt-line, 0 otherwise. */
139 static int process_protocol_version(void)
141 switch (determine_protocol_version_client(packet_buffer
)) {
147 die("server is speaking an unknown protocol");
151 static void process_capabilities(int *len
)
153 int nul_location
= strlen(packet_buffer
);
154 if (nul_location
== *len
)
156 server_capabilities
= xstrdup(packet_buffer
+ nul_location
+ 1);
160 static int process_dummy_ref(void)
162 struct object_id oid
;
165 if (parse_oid_hex(packet_buffer
, &oid
, &name
))
171 return !oidcmp(&null_oid
, &oid
) && !strcmp(name
, "capabilities^{}");
174 static void check_no_capabilities(int len
)
176 if (strlen(packet_buffer
) != len
)
177 warning("Ignoring capabilities after first line '%s'",
178 packet_buffer
+ strlen(packet_buffer
));
181 static int process_ref(int len
, struct ref
***list
, unsigned int flags
,
182 struct oid_array
*extra_have
)
184 struct object_id old_oid
;
187 if (parse_oid_hex(packet_buffer
, &old_oid
, &name
))
193 if (extra_have
&& !strcmp(name
, ".have")) {
194 oid_array_append(extra_have
, &old_oid
);
195 } else if (!strcmp(name
, "capabilities^{}")) {
196 die("protocol error: unexpected capabilities^{}");
197 } else if (check_ref(name
, flags
)) {
198 struct ref
*ref
= alloc_ref(name
);
199 oidcpy(&ref
->old_oid
, &old_oid
);
203 check_no_capabilities(len
);
207 static int process_shallow(int len
, struct oid_array
*shallow_points
)
210 struct object_id old_oid
;
212 if (!skip_prefix(packet_buffer
, "shallow ", &arg
))
215 if (get_oid_hex(arg
, &old_oid
))
216 die("protocol error: expected shallow sha-1, got '%s'", arg
);
218 die("repository on the other end cannot be shallow");
219 oid_array_append(shallow_points
, &old_oid
);
220 check_no_capabilities(len
);
225 * Read all the refs from the other end
227 struct ref
**get_remote_heads(int in
, char *src_buf
, size_t src_len
,
228 struct ref
**list
, unsigned int flags
,
229 struct oid_array
*extra_have
,
230 struct oid_array
*shallow_points
)
232 struct ref
**orig_list
= list
;
235 * A hang-up after seeing some response from the other end
236 * means that it is unexpected, as we know the other end is
237 * willing to talk to us. A hang-up before seeing any
238 * response does not necessarily mean an ACL problem, though.
242 int state
= EXPECTING_PROTOCOL_VERSION
;
246 while ((len
= read_remote_ref(in
, &src_buf
, &src_len
, &responded
))) {
248 case EXPECTING_PROTOCOL_VERSION
:
249 if (process_protocol_version()) {
250 state
= EXPECTING_FIRST_REF
;
253 state
= EXPECTING_FIRST_REF
;
255 case EXPECTING_FIRST_REF
:
256 process_capabilities(&len
);
257 if (process_dummy_ref()) {
258 state
= EXPECTING_SHALLOW
;
261 state
= EXPECTING_REF
;
264 if (process_ref(len
, &list
, flags
, extra_have
))
266 state
= EXPECTING_SHALLOW
;
268 case EXPECTING_SHALLOW
:
269 if (process_shallow(len
, shallow_points
))
271 die("protocol error: unexpected '%s'", packet_buffer
);
273 die("unexpected state %d", state
);
277 annotate_refs_with_symref_info(*orig_list
);
282 static const char *parse_feature_value(const char *feature_list
, const char *feature
, int *lenp
)
289 len
= strlen(feature
);
290 while (*feature_list
) {
291 const char *found
= strstr(feature_list
, feature
);
294 if (feature_list
== found
|| isspace(found
[-1])) {
295 const char *value
= found
+ len
;
296 /* feature with no value (e.g., "thin-pack") */
297 if (!*value
|| isspace(*value
)) {
302 /* feature with a value (e.g., "agent=git/1.2.3") */
303 else if (*value
== '=') {
306 *lenp
= strcspn(value
, " \t\n");
310 * otherwise we matched a substring of another feature;
314 feature_list
= found
+ 1;
319 int parse_feature_request(const char *feature_list
, const char *feature
)
321 return !!parse_feature_value(feature_list
, feature
, NULL
);
324 const char *server_feature_value(const char *feature
, int *len
)
326 return parse_feature_value(server_capabilities
, feature
, len
);
329 int server_supports(const char *feature
)
331 return !!server_feature_value(feature
, NULL
);
341 int url_is_local_not_ssh(const char *url
)
343 const char *colon
= strchr(url
, ':');
344 const char *slash
= strchr(url
, '/');
345 return !colon
|| (slash
&& slash
< colon
) ||
346 has_dos_drive_prefix(url
);
349 static const char *prot_name(enum protocol protocol
)
360 return "unknown protocol";
364 static enum protocol
get_protocol(const char *name
)
366 if (!strcmp(name
, "ssh"))
368 if (!strcmp(name
, "git"))
370 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
372 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
374 if (!strcmp(name
, "file"))
376 die("I don't handle protocol '%s'", name
);
379 static char *host_end(char **hoststart
, int removebrackets
)
381 char *host
= *hoststart
;
383 char *start
= strstr(host
, "@[");
385 start
++; /* Jump over '@' */
388 if (start
[0] == '[') {
389 end
= strchr(start
+ 1, ']');
391 if (removebrackets
) {
393 memmove(start
, start
+ 1, end
- start
);
404 #define STR(s) STR_(s)
406 static void get_host_and_port(char **host
, const char **port
)
409 end
= host_end(host
, 1);
410 colon
= strchr(end
, ':');
412 long portnr
= strtol(colon
+ 1, &end
, 10);
413 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
416 } else if (!colon
[1]) {
422 static void enable_keepalive(int sockfd
)
426 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
427 fprintf(stderr
, "unable to set SO_KEEPALIVE on socket: %s\n",
433 static const char *ai_name(const struct addrinfo
*ai
)
435 static char addr
[NI_MAXHOST
];
436 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
437 NI_NUMERICHOST
) != 0)
438 xsnprintf(addr
, sizeof(addr
), "(unknown)");
444 * Returns a connected socket() fd, or else die()s.
446 static int git_tcp_connect_sock(char *host
, int flags
)
448 struct strbuf error_message
= STRBUF_INIT
;
450 const char *port
= STR(DEFAULT_GIT_PORT
);
451 struct addrinfo hints
, *ai0
, *ai
;
455 get_host_and_port(&host
, &port
);
459 memset(&hints
, 0, sizeof(hints
));
460 if (flags
& CONNECT_IPV4
)
461 hints
.ai_family
= AF_INET
;
462 else if (flags
& CONNECT_IPV6
)
463 hints
.ai_family
= AF_INET6
;
464 hints
.ai_socktype
= SOCK_STREAM
;
465 hints
.ai_protocol
= IPPROTO_TCP
;
467 if (flags
& CONNECT_VERBOSE
)
468 fprintf(stderr
, "Looking up %s ... ", host
);
470 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
472 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
474 if (flags
& CONNECT_VERBOSE
)
475 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
477 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
478 sockfd
= socket(ai
->ai_family
,
479 ai
->ai_socktype
, ai
->ai_protocol
);
481 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
482 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
483 host
, cnt
, ai_name(ai
), strerror(errno
));
489 if (flags
& CONNECT_VERBOSE
)
490 fprintf(stderr
, "%s ", ai_name(ai
));
497 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
499 enable_keepalive(sockfd
);
501 if (flags
& CONNECT_VERBOSE
)
502 fprintf(stderr
, "done.\n");
504 strbuf_release(&error_message
);
512 * Returns a connected socket() fd, or else die()s.
514 static int git_tcp_connect_sock(char *host
, int flags
)
516 struct strbuf error_message
= STRBUF_INIT
;
518 const char *port
= STR(DEFAULT_GIT_PORT
);
521 struct sockaddr_in sa
;
526 get_host_and_port(&host
, &port
);
528 if (flags
& CONNECT_VERBOSE
)
529 fprintf(stderr
, "Looking up %s ... ", host
);
531 he
= gethostbyname(host
);
533 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
534 nport
= strtoul(port
, &ep
, 10);
535 if ( ep
== port
|| *ep
) {
537 struct servent
*se
= getservbyname(port
,"tcp");
539 die("Unknown port %s", port
);
543 if (flags
& CONNECT_VERBOSE
)
544 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
546 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
547 memset(&sa
, 0, sizeof sa
);
548 sa
.sin_family
= he
->h_addrtype
;
549 sa
.sin_port
= htons(nport
);
550 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
552 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
554 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
555 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
558 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
565 if (flags
& CONNECT_VERBOSE
)
566 fprintf(stderr
, "%s ",
567 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
572 die("unable to connect to %s:\n%s", host
, error_message
.buf
);
574 enable_keepalive(sockfd
);
576 if (flags
& CONNECT_VERBOSE
)
577 fprintf(stderr
, "done.\n");
586 * Dummy child_process returned by git_connect() if the transport protocol
587 * does not need fork(2).
589 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
591 int git_connection_is_socket(struct child_process
*conn
)
593 return conn
== &no_fork
;
596 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
598 int sockfd
= git_tcp_connect_sock(host
, flags
);
607 static char *git_proxy_command
;
609 static int git_proxy_command_options(const char *var
, const char *value
,
612 if (!strcmp(var
, "core.gitproxy")) {
616 const char *rhost_name
= cb
;
617 int rhost_len
= strlen(rhost_name
);
619 if (git_proxy_command
)
622 return config_error_nonbool(var
);
624 * ;# matches www.kernel.org as well
625 * gitproxy = netcatter-1 for kernel.org
626 * gitproxy = netcatter-2 for sample.xz
627 * gitproxy = netcatter-default
629 for_pos
= strstr(value
, " for ");
631 /* matches everybody */
632 matchlen
= strlen(value
);
634 hostlen
= strlen(for_pos
+ 5);
635 if (rhost_len
< hostlen
)
637 else if (!strncmp(for_pos
+ 5,
638 rhost_name
+ rhost_len
- hostlen
,
640 ((rhost_len
== hostlen
) ||
641 rhost_name
[rhost_len
- hostlen
-1] == '.'))
642 matchlen
= for_pos
- value
;
647 /* core.gitproxy = none for kernel.org */
649 !memcmp(value
, "none", 4))
651 git_proxy_command
= xmemdupz(value
, matchlen
);
656 return git_default_config(var
, value
, cb
);
659 static int git_use_proxy(const char *host
)
661 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
662 git_config(git_proxy_command_options
, (void*)host
);
663 return (git_proxy_command
&& *git_proxy_command
);
666 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
668 const char *port
= STR(DEFAULT_GIT_PORT
);
669 struct child_process
*proxy
;
671 get_host_and_port(&host
, &port
);
673 if (looks_like_command_line_option(host
))
674 die("strange hostname '%s' blocked", host
);
675 if (looks_like_command_line_option(port
))
676 die("strange port '%s' blocked", port
);
678 proxy
= xmalloc(sizeof(*proxy
));
679 child_process_init(proxy
);
680 argv_array_push(&proxy
->args
, git_proxy_command
);
681 argv_array_push(&proxy
->args
, host
);
682 argv_array_push(&proxy
->args
, port
);
685 if (start_command(proxy
))
686 die("cannot start proxy %s", git_proxy_command
);
687 fd
[0] = proxy
->out
; /* read from proxy stdout */
688 fd
[1] = proxy
->in
; /* write to proxy stdin */
692 static char *get_port(char *host
)
695 char *p
= strchr(host
, ':');
698 long port
= strtol(p
+ 1, &end
, 10);
699 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
709 * Extract protocol and relevant parts from the specified connection URL.
710 * The caller must free() the returned strings.
712 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
719 enum protocol protocol
= PROTO_LOCAL
;
721 if (is_url(url_orig
))
722 url
= url_decode(url_orig
);
724 url
= xstrdup(url_orig
);
726 host
= strstr(url
, "://");
729 protocol
= get_protocol(url
);
733 if (!url_is_local_not_ssh(url
)) {
734 protocol
= PROTO_SSH
;
740 * Don't do destructive transforms as protocol code does
741 * '[]' unwrapping in get_host_and_port()
743 end
= host_end(&host
, 0);
745 if (protocol
== PROTO_LOCAL
)
747 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
748 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
750 path
= strchr(end
, separator
);
753 die("No path specified. See 'man git-pull' for valid url syntax");
756 * null-terminate hostname and point path to ~ for URL's like this:
757 * ssh://host.xz/~user/repo
760 end
= path
; /* Need to \0 terminate host here */
761 if (separator
== ':')
762 path
++; /* path starts after ':' */
763 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
768 path
= xstrdup(path
);
771 *ret_host
= xstrdup(host
);
777 static const char *get_ssh_command(void)
781 if ((ssh
= getenv("GIT_SSH_COMMAND")))
784 if (!git_config_get_string_const("core.sshcommand", &ssh
))
795 VARIANT_TORTOISEPLINK
,
798 static int override_ssh_variant(enum ssh_variant
*ssh_variant
)
800 const char *variant
= getenv("GIT_SSH_VARIANT");
802 if (!variant
&& git_config_get_string_const("ssh.variant", &variant
))
805 if (!strcmp(variant
, "plink"))
806 *ssh_variant
= VARIANT_PLINK
;
807 else if (!strcmp(variant
, "putty"))
808 *ssh_variant
= VARIANT_PUTTY
;
809 else if (!strcmp(variant
, "tortoiseplink"))
810 *ssh_variant
= VARIANT_TORTOISEPLINK
;
811 else if (!strcmp(variant
, "simple"))
812 *ssh_variant
= VARIANT_SIMPLE
;
814 *ssh_variant
= VARIANT_SSH
;
819 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
822 enum ssh_variant ssh_variant
= VARIANT_SIMPLE
;
826 if (override_ssh_variant(&ssh_variant
))
830 p
= xstrdup(ssh_command
);
831 variant
= basename(p
);
833 const char **ssh_argv
;
835 p
= xstrdup(ssh_command
);
836 if (split_cmdline(p
, &ssh_argv
) > 0) {
837 variant
= basename((char *)ssh_argv
[0]);
839 * At this point, variant points into the buffer
840 * referenced by p, hence we do not need ssh_argv
850 if (!strcasecmp(variant
, "ssh") ||
851 !strcasecmp(variant
, "ssh.exe"))
852 ssh_variant
= VARIANT_SSH
;
853 else if (!strcasecmp(variant
, "plink") ||
854 !strcasecmp(variant
, "plink.exe"))
855 ssh_variant
= VARIANT_PLINK
;
856 else if (!strcasecmp(variant
, "tortoiseplink") ||
857 !strcasecmp(variant
, "tortoiseplink.exe"))
858 ssh_variant
= VARIANT_TORTOISEPLINK
;
865 * Open a connection using Git's native protocol.
867 * The caller is responsible for freeing hostandport, but this function may
868 * modify it (for example, to truncate it to remove the port part).
870 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
871 const char *path
, const char *prog
,
874 struct child_process
*conn
;
875 struct strbuf request
= STRBUF_INIT
;
877 * Set up virtual host information based on where we will
878 * connect, unless the user has overridden us in
881 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
883 target_host
= xstrdup(target_host
);
885 target_host
= xstrdup(hostandport
);
887 transport_check_allowed("git");
889 /* These underlying connection commands die() if they
892 if (git_use_proxy(hostandport
))
893 conn
= git_proxy_connect(fd
, hostandport
);
895 conn
= git_tcp_connect(fd
, hostandport
, flags
);
897 * Separate original protocol components prog and path
898 * from extended host header with a NUL byte.
900 * Note: Do not add any other headers here! Doing so
901 * will cause older git-daemon servers to crash.
903 strbuf_addf(&request
,
908 /* If using a new version put that stuff here after a second null byte */
909 if (get_protocol_version_config() > 0) {
910 strbuf_addch(&request
, '\0');
911 strbuf_addf(&request
, "version=%d%c",
912 get_protocol_version_config(), '\0');
915 packet_write(fd
[1], request
.buf
, request
.len
);
918 strbuf_release(&request
);
923 * Append the appropriate environment variables to `env` and options to
924 * `args` for running ssh in Git's SSH-tunneled transport.
926 static void push_ssh_options(struct argv_array
*args
, struct argv_array
*env
,
927 enum ssh_variant variant
, const char *port
,
930 if (variant
== VARIANT_SSH
&&
931 get_protocol_version_config() > 0) {
932 argv_array_push(args
, "-o");
933 argv_array_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
934 argv_array_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
935 get_protocol_version_config());
938 if (variant
!= VARIANT_SIMPLE
) {
939 if (flags
& CONNECT_IPV4
)
940 argv_array_push(args
, "-4");
941 else if (flags
& CONNECT_IPV6
)
942 argv_array_push(args
, "-6");
945 if (variant
== VARIANT_TORTOISEPLINK
)
946 argv_array_push(args
, "-batch");
948 if (port
&& variant
!= VARIANT_SIMPLE
) {
949 if (variant
== VARIANT_SSH
)
950 argv_array_push(args
, "-p");
952 argv_array_push(args
, "-P");
954 argv_array_push(args
, port
);
958 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
959 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
960 const char *port
, int flags
)
963 enum ssh_variant variant
;
965 if (looks_like_command_line_option(ssh_host
))
966 die("strange hostname '%s' blocked", ssh_host
);
968 ssh
= get_ssh_command();
970 variant
= determine_ssh_variant(ssh
, 1);
973 * GIT_SSH is the no-shell version of
974 * GIT_SSH_COMMAND (and must remain so for
975 * historical compatibility).
979 ssh
= getenv("GIT_SSH");
982 variant
= determine_ssh_variant(ssh
, 0);
985 argv_array_push(&conn
->args
, ssh
);
986 push_ssh_options(&conn
->args
, &conn
->env_array
, variant
, port
, flags
);
987 argv_array_push(&conn
->args
, ssh_host
);
991 * This returns the dummy child_process `no_fork` if the transport protocol
992 * does not need fork(2), or a struct child_process object if it does. Once
993 * done, finish the connection with finish_connect() with the value returned
994 * from this function (it is safe to call finish_connect() with NULL to
995 * support the former case).
997 * If it returns, the connect is successful; it just dies on errors (this
998 * will hopefully be changed in a libification effort, to return NULL when
999 * the connection failed).
1001 struct child_process
*git_connect(int fd
[2], const char *url
,
1002 const char *prog
, int flags
)
1004 char *hostandport
, *path
;
1005 struct child_process
*conn
;
1006 enum protocol protocol
;
1008 /* Without this we cannot rely on waitpid() to tell
1009 * what happened to our children.
1011 signal(SIGCHLD
, SIG_DFL
);
1013 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1014 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1015 printf("Diag: url=%s\n", url
? url
: "NULL");
1016 printf("Diag: protocol=%s\n", prot_name(protocol
));
1017 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1018 printf("Diag: path=%s\n", path
? path
: "NULL");
1020 } else if (protocol
== PROTO_GIT
) {
1021 conn
= git_connect_git(fd
, hostandport
, path
, prog
, flags
);
1023 struct strbuf cmd
= STRBUF_INIT
;
1024 const char *const *var
;
1026 conn
= xmalloc(sizeof(*conn
));
1027 child_process_init(conn
);
1029 if (looks_like_command_line_option(path
))
1030 die("strange pathname '%s' blocked", path
);
1032 strbuf_addstr(&cmd
, prog
);
1033 strbuf_addch(&cmd
, ' ');
1034 sq_quote_buf(&cmd
, path
);
1036 /* remove repo-local variables from the environment */
1037 for (var
= local_repo_env
; *var
; var
++)
1038 argv_array_push(&conn
->env_array
, *var
);
1040 conn
->use_shell
= 1;
1041 conn
->in
= conn
->out
= -1;
1042 if (protocol
== PROTO_SSH
) {
1043 char *ssh_host
= hostandport
;
1044 const char *port
= NULL
;
1045 transport_check_allowed("ssh");
1046 get_host_and_port(&ssh_host
, &port
);
1049 port
= get_port(ssh_host
);
1051 if (flags
& CONNECT_DIAG_URL
) {
1052 printf("Diag: url=%s\n", url
? url
: "NULL");
1053 printf("Diag: protocol=%s\n", prot_name(protocol
));
1054 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1055 printf("Diag: port=%s\n", port
? port
: "NONE");
1056 printf("Diag: path=%s\n", path
? path
: "NULL");
1061 strbuf_release(&cmd
);
1064 fill_ssh_args(conn
, ssh_host
, port
, flags
);
1066 transport_check_allowed("file");
1067 if (get_protocol_version_config() > 0) {
1068 argv_array_pushf(&conn
->env_array
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1069 get_protocol_version_config());
1072 argv_array_push(&conn
->args
, cmd
.buf
);
1074 if (start_command(conn
))
1075 die("unable to fork");
1077 fd
[0] = conn
->out
; /* read from child's stdout */
1078 fd
[1] = conn
->in
; /* write to child's stdin */
1079 strbuf_release(&cmd
);
1086 int finish_connect(struct child_process
*conn
)
1089 if (!conn
|| git_connection_is_socket(conn
))
1092 code
= finish_command(conn
);