1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
5 #include "environment.h"
11 #include "run-command.h"
15 #include "string-list.h"
16 #include "oid-array.h"
18 #include "transport.h"
24 #include "bundle-uri.h"
26 static char *server_capabilities_v1
;
27 static struct strvec server_capabilities_v2
= STRVEC_INIT
;
28 static const char *next_server_feature_value(const char *feature
, size_t *len
, size_t *offset
);
30 static int check_ref(const char *name
, unsigned int flags
)
35 if (!skip_prefix(name
, "refs/", &name
))
38 /* REF_NORMAL means that we don't want the magic fake tag refs */
39 if ((flags
& REF_NORMAL
) && check_refname_format(name
,
40 REFNAME_ALLOW_ONELEVEL
))
43 /* REF_BRANCHES means that we want regular branch heads */
44 if ((flags
& REF_BRANCHES
) && starts_with(name
, "heads/"))
47 /* REF_TAGS means that we want tags */
48 if ((flags
& REF_TAGS
) && starts_with(name
, "tags/"))
51 /* All type bits clear means that we are ok with anything */
52 return !(flags
& ~REF_NORMAL
);
55 int check_ref_type(const struct ref
*ref
, int flags
)
57 return check_ref(ref
->name
, flags
);
60 static NORETURN
void die_initial_contact(int unexpected
)
63 * A hang-up after seeing some response from the other end
64 * means that it is unexpected, as we know the other end is
65 * willing to talk to us. A hang-up before seeing any
66 * response does not necessarily mean an ACL problem, though.
69 die(_("the remote end hung up upon initial contact"));
71 die(_("Could not read from remote repository.\n\n"
72 "Please make sure you have the correct access rights\n"
73 "and the repository exists."));
76 /* Checks if the server supports the capability 'c' */
77 int server_supports_v2(const char *c
)
81 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
83 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
84 (!*out
|| *out
== '='))
90 void ensure_server_supports_v2(const char *c
)
92 if (!server_supports_v2(c
))
93 die(_("server doesn't support '%s'"), c
);
96 int server_feature_v2(const char *c
, const char **v
)
100 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
102 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
111 int server_supports_feature(const char *c
, const char *feature
,
116 for (i
= 0; i
< server_capabilities_v2
.nr
; i
++) {
118 if (skip_prefix(server_capabilities_v2
.v
[i
], c
, &out
) &&
119 (!*out
|| *(out
++) == '=')) {
120 if (parse_feature_request(out
, feature
))
128 die(_("server doesn't support feature '%s'"), feature
);
133 static void process_capabilities_v2(struct packet_reader
*reader
)
135 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
)
136 strvec_push(&server_capabilities_v2
, reader
->line
);
138 if (reader
->status
!= PACKET_READ_FLUSH
)
139 die(_("expected flush after capabilities"));
142 enum protocol_version
discover_version(struct packet_reader
*reader
)
144 enum protocol_version version
= protocol_unknown_version
;
147 * Peek the first line of the server's response to
148 * determine the protocol version the server is speaking.
150 switch (packet_reader_peek(reader
)) {
151 case PACKET_READ_EOF
:
152 die_initial_contact(0);
153 case PACKET_READ_FLUSH
:
154 case PACKET_READ_DELIM
:
155 case PACKET_READ_RESPONSE_END
:
156 version
= protocol_v0
;
158 case PACKET_READ_NORMAL
:
159 version
= determine_protocol_version_client(reader
->line
);
165 process_capabilities_v2(reader
);
168 /* Read the peeked version line */
169 packet_reader_read(reader
);
173 case protocol_unknown_version
:
174 BUG("unknown protocol version");
177 trace2_data_intmax("transfer", NULL
, "negotiated-version", version
);
182 static void parse_one_symref_info(struct string_list
*symref
, const char *val
, int len
)
185 struct string_list_item
*item
;
188 return; /* just "symref" */
189 /* e.g. "symref=HEAD:refs/heads/master" */
190 sym
= xmemdupz(val
, len
);
191 target
= strchr(sym
, ':');
193 /* just "symref=something" */
196 if (check_refname_format(sym
, REFNAME_ALLOW_ONELEVEL
) ||
197 check_refname_format(target
, REFNAME_ALLOW_ONELEVEL
))
198 /* "symref=bogus:pair */
200 item
= string_list_append_nodup(symref
, sym
);
208 static void annotate_refs_with_symref_info(struct ref
*ref
)
210 struct string_list symref
= STRING_LIST_INIT_DUP
;
217 val
= next_server_feature_value("symref", &len
, &offset
);
220 parse_one_symref_info(&symref
, val
, len
);
222 string_list_sort(&symref
);
224 for (; ref
; ref
= ref
->next
) {
225 struct string_list_item
*item
;
226 item
= string_list_lookup(&symref
, ref
->name
);
229 ref
->symref
= xstrdup((char *)item
->util
);
231 string_list_clear(&symref
, 0);
234 static void process_capabilities(struct packet_reader
*reader
, int *linelen
)
236 const char *feat_val
;
238 const char *line
= reader
->line
;
239 int nul_location
= strlen(line
);
240 if (nul_location
== *linelen
)
242 server_capabilities_v1
= xstrdup(line
+ nul_location
+ 1);
243 *linelen
= nul_location
;
245 feat_val
= server_feature_value("object-format", &feat_len
);
247 char *hash_name
= xstrndup(feat_val
, feat_len
);
248 int hash_algo
= hash_algo_by_name(hash_name
);
249 if (hash_algo
!= GIT_HASH_UNKNOWN
)
250 reader
->hash_algo
= &hash_algos
[hash_algo
];
253 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
257 static int process_dummy_ref(const struct packet_reader
*reader
)
259 const char *line
= reader
->line
;
260 struct object_id oid
;
263 if (parse_oid_hex_algop(line
, &oid
, &name
, reader
->hash_algo
))
269 return oideq(reader
->hash_algo
->null_oid
, &oid
) &&
270 !strcmp(name
, "capabilities^{}");
273 static void check_no_capabilities(const char *line
, int len
)
275 if (strlen(line
) != len
)
276 warning(_("ignoring capabilities after first line '%s'"),
277 line
+ strlen(line
));
280 static int process_ref(const struct packet_reader
*reader
, int len
,
281 struct ref
***list
, unsigned int flags
,
282 struct oid_array
*extra_have
)
284 const char *line
= reader
->line
;
285 struct object_id old_oid
;
288 if (parse_oid_hex_algop(line
, &old_oid
, &name
, reader
->hash_algo
))
294 if (extra_have
&& !strcmp(name
, ".have")) {
295 oid_array_append(extra_have
, &old_oid
);
296 } else if (!strcmp(name
, "capabilities^{}")) {
297 die(_("protocol error: unexpected capabilities^{}"));
298 } else if (check_ref(name
, flags
)) {
299 struct ref
*ref
= alloc_ref(name
);
300 oidcpy(&ref
->old_oid
, &old_oid
);
304 check_no_capabilities(line
, len
);
308 static int process_shallow(const struct packet_reader
*reader
, int len
,
309 struct oid_array
*shallow_points
)
311 const char *line
= reader
->line
;
313 struct object_id old_oid
;
315 if (!skip_prefix(line
, "shallow ", &arg
))
318 if (get_oid_hex_algop(arg
, &old_oid
, reader
->hash_algo
))
319 die(_("protocol error: expected shallow sha-1, got '%s'"), arg
);
321 die(_("repository on the other end cannot be shallow"));
322 oid_array_append(shallow_points
, &old_oid
);
323 check_no_capabilities(line
, len
);
327 enum get_remote_heads_state
{
328 EXPECTING_FIRST_REF
= 0,
335 * Read all the refs from the other end
337 struct ref
**get_remote_heads(struct packet_reader
*reader
,
338 struct ref
**list
, unsigned int flags
,
339 struct oid_array
*extra_have
,
340 struct oid_array
*shallow_points
)
342 struct ref
**orig_list
= list
;
344 enum get_remote_heads_state state
= EXPECTING_FIRST_REF
;
348 while (state
!= EXPECTING_DONE
) {
349 switch (packet_reader_read(reader
)) {
350 case PACKET_READ_EOF
:
351 die_initial_contact(1);
352 case PACKET_READ_NORMAL
:
353 len
= reader
->pktlen
;
355 case PACKET_READ_FLUSH
:
356 state
= EXPECTING_DONE
;
358 case PACKET_READ_DELIM
:
359 case PACKET_READ_RESPONSE_END
:
360 die(_("invalid packet"));
364 case EXPECTING_FIRST_REF
:
365 process_capabilities(reader
, &len
);
366 if (process_dummy_ref(reader
)) {
367 state
= EXPECTING_SHALLOW
;
370 state
= EXPECTING_REF
;
373 if (process_ref(reader
, len
, &list
, flags
, extra_have
))
375 state
= EXPECTING_SHALLOW
;
377 case EXPECTING_SHALLOW
:
378 if (process_shallow(reader
, len
, shallow_points
))
380 die(_("protocol error: unexpected '%s'"), reader
->line
);
386 annotate_refs_with_symref_info(*orig_list
);
391 /* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
392 static int process_ref_v2(struct packet_reader
*reader
, struct ref
***list
,
393 const char **unborn_head_target
)
397 struct object_id old_oid
;
399 struct string_list line_sections
= STRING_LIST_INIT_DUP
;
401 const char *line
= reader
->line
;
404 * Ref lines have a number of fields which are space deliminated. The
405 * first field is the OID of the ref. The second field is the ref
406 * name. Subsequent fields (symref-target and peeled) are optional and
407 * don't have a particular order.
409 if (string_list_split(&line_sections
, line
, ' ', -1) < 2) {
414 if (!strcmp("unborn", line_sections
.items
[i
].string
)) {
416 if (unborn_head_target
&&
417 !strcmp("HEAD", line_sections
.items
[i
++].string
)) {
419 * Look for the symref target (if any). If found,
420 * return it to the caller.
422 for (; i
< line_sections
.nr
; i
++) {
423 const char *arg
= line_sections
.items
[i
].string
;
425 if (skip_prefix(arg
, "symref-target:", &arg
)) {
426 *unborn_head_target
= xstrdup(arg
);
433 if (parse_oid_hex_algop(line_sections
.items
[i
++].string
, &old_oid
, &end
, reader
->hash_algo
) ||
439 ref
= alloc_ref(line_sections
.items
[i
++].string
);
441 memcpy(ref
->old_oid
.hash
, old_oid
.hash
, reader
->hash_algo
->rawsz
);
445 for (; i
< line_sections
.nr
; i
++) {
446 const char *arg
= line_sections
.items
[i
].string
;
447 if (skip_prefix(arg
, "symref-target:", &arg
))
448 ref
->symref
= xstrdup(arg
);
450 if (skip_prefix(arg
, "peeled:", &arg
)) {
451 struct object_id peeled_oid
;
454 if (parse_oid_hex_algop(arg
, &peeled_oid
, &end
,
455 reader
->hash_algo
) || *end
) {
460 peeled_name
= xstrfmt("%s^{}", ref
->name
);
461 peeled
= alloc_ref(peeled_name
);
463 memcpy(peeled
->old_oid
.hash
, peeled_oid
.hash
,
464 reader
->hash_algo
->rawsz
);
466 *list
= &peeled
->next
;
473 string_list_clear(&line_sections
, 0);
477 void check_stateless_delimiter(int stateless_rpc
,
478 struct packet_reader
*reader
,
482 return; /* not in stateless mode, no delimiter expected */
483 if (packet_reader_read(reader
) != PACKET_READ_RESPONSE_END
)
487 static void send_capabilities(int fd_out
, struct packet_reader
*reader
)
489 const char *hash_name
;
491 if (server_supports_v2("agent"))
492 packet_write_fmt(fd_out
, "agent=%s", git_user_agent_sanitized());
494 if (server_feature_v2("object-format", &hash_name
)) {
495 int hash_algo
= hash_algo_by_name(hash_name
);
496 if (hash_algo
== GIT_HASH_UNKNOWN
)
497 die(_("unknown object format '%s' specified by server"), hash_name
);
498 reader
->hash_algo
= &hash_algos
[hash_algo
];
499 packet_write_fmt(fd_out
, "object-format=%s", reader
->hash_algo
->name
);
501 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
505 int get_remote_bundle_uri(int fd_out
, struct packet_reader
*reader
,
506 struct bundle_list
*bundles
, int stateless_rpc
)
510 /* Assert bundle-uri support */
511 ensure_server_supports_v2("bundle-uri");
513 /* (Re-)send capabilities */
514 send_capabilities(fd_out
, reader
);
517 packet_write_fmt(fd_out
, "command=bundle-uri\n");
518 packet_delim(fd_out
);
520 packet_flush(fd_out
);
522 /* Process response from server */
523 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
524 const char *line
= reader
->line
;
527 if (!bundle_uri_parse_line(bundles
, line
))
530 return error(_("error on bundle-uri response line %d: %s"),
534 if (reader
->status
!= PACKET_READ_FLUSH
)
535 return error(_("expected flush after bundle-uri listing"));
538 * Might die(), but obscure enough that that's OK, e.g. in
539 * serve.c we'll call BUG() on its equivalent (the
540 * PACKET_READ_RESPONSE_END check).
542 check_stateless_delimiter(stateless_rpc
, reader
,
543 _("expected response end packet after ref listing"));
548 struct ref
**get_remote_refs(int fd_out
, struct packet_reader
*reader
,
549 struct ref
**list
, int for_push
,
550 struct transport_ls_refs_options
*transport_options
,
551 const struct string_list
*server_options
,
555 struct strvec
*ref_prefixes
= transport_options
?
556 &transport_options
->ref_prefixes
: NULL
;
557 const char **unborn_head_target
= transport_options
?
558 &transport_options
->unborn_head_target
: NULL
;
561 ensure_server_supports_v2("ls-refs");
562 packet_write_fmt(fd_out
, "command=ls-refs\n");
564 /* Send capabilities */
565 send_capabilities(fd_out
, reader
);
567 if (server_options
&& server_options
->nr
) {
568 ensure_server_supports_v2("server-option");
569 for (i
= 0; i
< server_options
->nr
; i
++)
570 packet_write_fmt(fd_out
, "server-option=%s",
571 server_options
->items
[i
].string
);
574 packet_delim(fd_out
);
575 /* When pushing we don't want to request the peeled tags */
577 packet_write_fmt(fd_out
, "peel\n");
578 packet_write_fmt(fd_out
, "symrefs\n");
579 if (server_supports_feature("ls-refs", "unborn", 0))
580 packet_write_fmt(fd_out
, "unborn\n");
581 for (i
= 0; ref_prefixes
&& i
< ref_prefixes
->nr
; i
++) {
582 packet_write_fmt(fd_out
, "ref-prefix %s\n",
585 packet_flush(fd_out
);
587 /* Process response from server */
588 while (packet_reader_read(reader
) == PACKET_READ_NORMAL
) {
589 if (!process_ref_v2(reader
, &list
, unborn_head_target
))
590 die(_("invalid ls-refs response: %s"), reader
->line
);
593 if (reader
->status
!= PACKET_READ_FLUSH
)
594 die(_("expected flush after ref listing"));
596 check_stateless_delimiter(stateless_rpc
, reader
,
597 _("expected response end packet after ref listing"));
602 const char *parse_feature_value(const char *feature_list
, const char *feature
, size_t *lenp
, size_t *offset
)
604 const char *orig_start
= feature_list
;
610 len
= strlen(feature
);
612 feature_list
+= *offset
;
613 while (*feature_list
) {
614 const char *found
= strstr(feature_list
, feature
);
617 if (feature_list
== found
|| isspace(found
[-1])) {
618 const char *value
= found
+ len
;
619 /* feature with no value (e.g., "thin-pack") */
620 if (!*value
|| isspace(*value
)) {
624 *offset
= found
+ len
- orig_start
;
627 /* feature with a value (e.g., "agent=git/1.2.3") */
628 else if (*value
== '=') {
632 end
= strcspn(value
, " \t\n");
636 *offset
= value
+ end
- orig_start
;
640 * otherwise we matched a substring of another feature;
644 feature_list
= found
+ 1;
649 int server_supports_hash(const char *desired
, int *feature_supported
)
655 hash
= next_server_feature_value("object-format", &len
, &offset
);
656 if (feature_supported
)
657 *feature_supported
= !!hash
;
659 hash
= hash_algos
[GIT_HASH_SHA1
].name
;
663 if (!xstrncmpz(desired
, hash
, len
))
666 hash
= next_server_feature_value("object-format", &len
, &offset
);
671 int parse_feature_request(const char *feature_list
, const char *feature
)
673 return !!parse_feature_value(feature_list
, feature
, NULL
, NULL
);
676 static const char *next_server_feature_value(const char *feature
, size_t *len
, size_t *offset
)
678 return parse_feature_value(server_capabilities_v1
, feature
, len
, offset
);
681 const char *server_feature_value(const char *feature
, size_t *len
)
683 return parse_feature_value(server_capabilities_v1
, feature
, len
, NULL
);
686 int server_supports(const char *feature
)
688 return !!server_feature_value(feature
, NULL
);
698 int url_is_local_not_ssh(const char *url
)
700 const char *colon
= strchr(url
, ':');
701 const char *slash
= strchr(url
, '/');
702 return !colon
|| (slash
&& slash
< colon
) ||
703 (has_dos_drive_prefix(url
) && is_valid_path(url
));
706 static const char *prot_name(enum protocol protocol
)
717 return "unknown protocol";
721 static enum protocol
get_protocol(const char *name
)
723 if (!strcmp(name
, "ssh"))
725 if (!strcmp(name
, "git"))
727 if (!strcmp(name
, "git+ssh")) /* deprecated - do not use */
729 if (!strcmp(name
, "ssh+git")) /* deprecated - do not use */
731 if (!strcmp(name
, "file"))
733 die(_("protocol '%s' is not supported"), name
);
736 static char *host_end(char **hoststart
, int removebrackets
)
738 char *host
= *hoststart
;
740 char *start
= strstr(host
, "@[");
742 start
++; /* Jump over '@' */
745 if (start
[0] == '[') {
746 end
= strchr(start
+ 1, ']');
748 if (removebrackets
) {
750 memmove(start
, start
+ 1, end
- start
);
761 #define STR(s) STR_(s)
763 static void get_host_and_port(char **host
, const char **port
)
766 end
= host_end(host
, 1);
767 colon
= strchr(end
, ':');
769 long portnr
= strtol(colon
+ 1, &end
, 10);
770 if (end
!= colon
+ 1 && *end
== '\0' && 0 <= portnr
&& portnr
< 65536) {
773 } else if (!colon
[1]) {
779 static void enable_keepalive(int sockfd
)
783 if (setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &ka
, sizeof(ka
)) < 0)
784 error_errno(_("unable to set SO_KEEPALIVE on socket"));
789 static const char *ai_name(const struct addrinfo
*ai
)
791 static char addr
[NI_MAXHOST
];
792 if (getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
, sizeof(addr
), NULL
, 0,
793 NI_NUMERICHOST
) != 0)
794 xsnprintf(addr
, sizeof(addr
), "(unknown)");
800 * Returns a connected socket() fd, or else die()s.
802 static int git_tcp_connect_sock(char *host
, int flags
)
804 struct strbuf error_message
= STRBUF_INIT
;
806 const char *port
= STR(DEFAULT_GIT_PORT
);
807 struct addrinfo hints
, *ai0
, *ai
;
811 get_host_and_port(&host
, &port
);
815 memset(&hints
, 0, sizeof(hints
));
816 if (flags
& CONNECT_IPV4
)
817 hints
.ai_family
= AF_INET
;
818 else if (flags
& CONNECT_IPV6
)
819 hints
.ai_family
= AF_INET6
;
820 hints
.ai_socktype
= SOCK_STREAM
;
821 hints
.ai_protocol
= IPPROTO_TCP
;
823 if (flags
& CONNECT_VERBOSE
)
824 fprintf(stderr
, _("Looking up %s ... "), host
);
826 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
828 die(_("unable to look up %s (port %s) (%s)"), host
, port
, gai_strerror(gai
));
830 if (flags
& CONNECT_VERBOSE
)
831 /* TRANSLATORS: this is the end of "Looking up %s ... " */
832 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
834 for (ai0
= ai
; ai
; ai
= ai
->ai_next
, cnt
++) {
835 sockfd
= socket(ai
->ai_family
,
836 ai
->ai_socktype
, ai
->ai_protocol
);
838 (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)) {
839 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
840 host
, cnt
, ai_name(ai
), strerror(errno
));
846 if (flags
& CONNECT_VERBOSE
)
847 fprintf(stderr
, "%s ", ai_name(ai
));
854 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
856 enable_keepalive(sockfd
);
858 if (flags
& CONNECT_VERBOSE
)
859 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
860 fprintf_ln(stderr
, _("done."));
862 strbuf_release(&error_message
);
870 * Returns a connected socket() fd, or else die()s.
872 static int git_tcp_connect_sock(char *host
, int flags
)
874 struct strbuf error_message
= STRBUF_INIT
;
876 const char *port
= STR(DEFAULT_GIT_PORT
);
879 struct sockaddr_in sa
;
884 get_host_and_port(&host
, &port
);
886 if (flags
& CONNECT_VERBOSE
)
887 fprintf(stderr
, _("Looking up %s ... "), host
);
889 he
= gethostbyname(host
);
891 die(_("unable to look up %s (%s)"), host
, hstrerror(h_errno
));
892 nport
= strtoul(port
, &ep
, 10);
893 if ( ep
== port
|| *ep
) {
895 struct servent
*se
= getservbyname(port
,"tcp");
897 die(_("unknown port %s"), port
);
901 if (flags
& CONNECT_VERBOSE
)
902 /* TRANSLATORS: this is the end of "Looking up %s ... " */
903 fprintf(stderr
, _("done.\nConnecting to %s (port %s) ... "), host
, port
);
905 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
906 memset(&sa
, 0, sizeof sa
);
907 sa
.sin_family
= he
->h_addrtype
;
908 sa
.sin_port
= htons(nport
);
909 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
911 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
913 connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
914 strbuf_addf(&error_message
, "%s[%d: %s]: errno=%s\n",
917 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
924 if (flags
& CONNECT_VERBOSE
)
925 fprintf(stderr
, "%s ",
926 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
931 die(_("unable to connect to %s:\n%s"), host
, error_message
.buf
);
933 enable_keepalive(sockfd
);
935 if (flags
& CONNECT_VERBOSE
)
936 /* TRANSLATORS: this is the end of "Connecting to %s (port %s) ... " */
937 fprintf_ln(stderr
, _("done."));
946 * Dummy child_process returned by git_connect() if the transport protocol
947 * does not need fork(2).
949 static struct child_process no_fork
= CHILD_PROCESS_INIT
;
951 int git_connection_is_socket(struct child_process
*conn
)
953 return conn
== &no_fork
;
956 static struct child_process
*git_tcp_connect(int fd
[2], char *host
, int flags
)
958 int sockfd
= git_tcp_connect_sock(host
, flags
);
967 static char *git_proxy_command
;
969 static int git_proxy_command_options(const char *var
, const char *value
,
970 const struct config_context
*ctx
, void *cb
)
972 if (!strcmp(var
, "core.gitproxy")) {
976 const char *rhost_name
= cb
;
977 int rhost_len
= strlen(rhost_name
);
979 if (git_proxy_command
)
982 return config_error_nonbool(var
);
984 * ;# matches www.kernel.org as well
985 * gitproxy = netcatter-1 for kernel.org
986 * gitproxy = netcatter-2 for sample.xz
987 * gitproxy = netcatter-default
989 for_pos
= strstr(value
, " for ");
991 /* matches everybody */
992 matchlen
= strlen(value
);
994 hostlen
= strlen(for_pos
+ 5);
995 if (rhost_len
< hostlen
)
997 else if (!strncmp(for_pos
+ 5,
998 rhost_name
+ rhost_len
- hostlen
,
1000 ((rhost_len
== hostlen
) ||
1001 rhost_name
[rhost_len
- hostlen
-1] == '.'))
1002 matchlen
= for_pos
- value
;
1006 if (0 <= matchlen
) {
1007 /* core.gitproxy = none for kernel.org */
1008 if (matchlen
== 4 &&
1009 !memcmp(value
, "none", 4))
1011 git_proxy_command
= xmemdupz(value
, matchlen
);
1016 return git_default_config(var
, value
, ctx
, cb
);
1019 static int git_use_proxy(const char *host
)
1021 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
1022 git_config(git_proxy_command_options
, (void*)host
);
1023 return (git_proxy_command
&& *git_proxy_command
);
1026 static struct child_process
*git_proxy_connect(int fd
[2], char *host
)
1028 const char *port
= STR(DEFAULT_GIT_PORT
);
1029 struct child_process
*proxy
;
1031 get_host_and_port(&host
, &port
);
1033 if (looks_like_command_line_option(host
))
1034 die(_("strange hostname '%s' blocked"), host
);
1035 if (looks_like_command_line_option(port
))
1036 die(_("strange port '%s' blocked"), port
);
1038 proxy
= xmalloc(sizeof(*proxy
));
1039 child_process_init(proxy
);
1040 strvec_push(&proxy
->args
, git_proxy_command
);
1041 strvec_push(&proxy
->args
, host
);
1042 strvec_push(&proxy
->args
, port
);
1045 if (start_command(proxy
))
1046 die(_("cannot start proxy %s"), git_proxy_command
);
1047 fd
[0] = proxy
->out
; /* read from proxy stdout */
1048 fd
[1] = proxy
->in
; /* write to proxy stdin */
1052 static char *get_port(char *host
)
1055 char *p
= strchr(host
, ':');
1058 long port
= strtol(p
+ 1, &end
, 10);
1059 if (end
!= p
+ 1 && *end
== '\0' && 0 <= port
&& port
< 65536) {
1069 * Extract protocol and relevant parts from the specified connection URL.
1070 * The caller must free() the returned strings.
1072 static enum protocol
parse_connect_url(const char *url_orig
, char **ret_host
,
1078 int separator
= '/';
1079 enum protocol protocol
= PROTO_LOCAL
;
1081 if (is_url(url_orig
))
1082 url
= url_decode(url_orig
);
1084 url
= xstrdup(url_orig
);
1086 host
= strstr(url
, "://");
1089 protocol
= get_protocol(url
);
1093 if (!url_is_local_not_ssh(url
)) {
1094 protocol
= PROTO_SSH
;
1100 * Don't do destructive transforms as protocol code does
1101 * '[]' unwrapping in get_host_and_port()
1103 end
= host_end(&host
, 0);
1105 if (protocol
== PROTO_LOCAL
)
1107 else if (protocol
== PROTO_FILE
&& *host
!= '/' &&
1108 !has_dos_drive_prefix(host
) &&
1109 offset_1st_component(host
- 2) > 1)
1110 path
= host
- 2; /* include the leading "//" */
1111 else if (protocol
== PROTO_FILE
&& has_dos_drive_prefix(end
))
1112 path
= end
; /* "file://$(pwd)" may be "file://C:/projects/repo" */
1114 path
= strchr(end
, separator
);
1116 if (!path
|| !*path
)
1117 die(_("no path specified; see 'git help pull' for valid url syntax"));
1120 * null-terminate hostname and point path to ~ for URL's like this:
1121 * ssh://host.xz/~user/repo
1124 end
= path
; /* Need to \0 terminate host here */
1125 if (separator
== ':')
1126 path
++; /* path starts after ':' */
1127 if (protocol
== PROTO_GIT
|| protocol
== PROTO_SSH
) {
1132 path
= xstrdup(path
);
1135 *ret_host
= xstrdup(host
);
1141 static const char *get_ssh_command(void)
1145 if ((ssh
= getenv("GIT_SSH_COMMAND")))
1148 if (!git_config_get_string_tmp("core.sshcommand", &ssh
))
1160 VARIANT_TORTOISEPLINK
,
1163 static void override_ssh_variant(enum ssh_variant
*ssh_variant
)
1165 const char *variant
= getenv("GIT_SSH_VARIANT");
1167 if (!variant
&& git_config_get_string_tmp("ssh.variant", &variant
))
1170 if (!strcmp(variant
, "auto"))
1171 *ssh_variant
= VARIANT_AUTO
;
1172 else if (!strcmp(variant
, "plink"))
1173 *ssh_variant
= VARIANT_PLINK
;
1174 else if (!strcmp(variant
, "putty"))
1175 *ssh_variant
= VARIANT_PUTTY
;
1176 else if (!strcmp(variant
, "tortoiseplink"))
1177 *ssh_variant
= VARIANT_TORTOISEPLINK
;
1178 else if (!strcmp(variant
, "simple"))
1179 *ssh_variant
= VARIANT_SIMPLE
;
1181 *ssh_variant
= VARIANT_SSH
;
1184 static enum ssh_variant
determine_ssh_variant(const char *ssh_command
,
1187 enum ssh_variant ssh_variant
= VARIANT_AUTO
;
1188 const char *variant
;
1191 override_ssh_variant(&ssh_variant
);
1193 if (ssh_variant
!= VARIANT_AUTO
)
1197 p
= xstrdup(ssh_command
);
1198 variant
= basename(p
);
1200 const char **ssh_argv
;
1202 p
= xstrdup(ssh_command
);
1203 if (split_cmdline(p
, &ssh_argv
) > 0) {
1204 variant
= basename((char *)ssh_argv
[0]);
1206 * At this point, variant points into the buffer
1207 * referenced by p, hence we do not need ssh_argv
1217 if (!strcasecmp(variant
, "ssh") ||
1218 !strcasecmp(variant
, "ssh.exe"))
1219 ssh_variant
= VARIANT_SSH
;
1220 else if (!strcasecmp(variant
, "plink") ||
1221 !strcasecmp(variant
, "plink.exe"))
1222 ssh_variant
= VARIANT_PLINK
;
1223 else if (!strcasecmp(variant
, "tortoiseplink") ||
1224 !strcasecmp(variant
, "tortoiseplink.exe"))
1225 ssh_variant
= VARIANT_TORTOISEPLINK
;
1232 * Open a connection using Git's native protocol.
1234 * The caller is responsible for freeing hostandport, but this function may
1235 * modify it (for example, to truncate it to remove the port part).
1237 static struct child_process
*git_connect_git(int fd
[2], char *hostandport
,
1238 const char *path
, const char *prog
,
1239 enum protocol_version version
,
1242 struct child_process
*conn
;
1243 struct strbuf request
= STRBUF_INIT
;
1245 * Set up virtual host information based on where we will
1246 * connect, unless the user has overridden us in
1249 char *target_host
= getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1251 target_host
= xstrdup(target_host
);
1253 target_host
= xstrdup(hostandport
);
1255 transport_check_allowed("git");
1256 if (strchr(target_host
, '\n') || strchr(path
, '\n'))
1257 die(_("newline is forbidden in git:// hosts and repo paths"));
1260 * These underlying connection commands die() if they
1263 if (git_use_proxy(hostandport
))
1264 conn
= git_proxy_connect(fd
, hostandport
);
1266 conn
= git_tcp_connect(fd
, hostandport
, flags
);
1268 * Separate original protocol components prog and path
1269 * from extended host header with a NUL byte.
1271 * Note: Do not add any other headers here! Doing so
1272 * will cause older git-daemon servers to crash.
1274 strbuf_addf(&request
,
1279 /* If using a new version put that stuff here after a second null byte */
1281 strbuf_addch(&request
, '\0');
1282 strbuf_addf(&request
, "version=%d%c",
1286 packet_write(fd
[1], request
.buf
, request
.len
);
1289 strbuf_release(&request
);
1294 * Append the appropriate environment variables to `env` and options to
1295 * `args` for running ssh in Git's SSH-tunneled transport.
1297 static void push_ssh_options(struct strvec
*args
, struct strvec
*env
,
1298 enum ssh_variant variant
, const char *port
,
1299 enum protocol_version version
, int flags
)
1301 if (variant
== VARIANT_SSH
&&
1303 strvec_push(args
, "-o");
1304 strvec_push(args
, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT
);
1305 strvec_pushf(env
, GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1309 if (flags
& CONNECT_IPV4
) {
1312 BUG("VARIANT_AUTO passed to push_ssh_options");
1313 case VARIANT_SIMPLE
:
1314 die(_("ssh variant 'simple' does not support -4"));
1318 case VARIANT_TORTOISEPLINK
:
1319 strvec_push(args
, "-4");
1321 } else if (flags
& CONNECT_IPV6
) {
1324 BUG("VARIANT_AUTO passed to push_ssh_options");
1325 case VARIANT_SIMPLE
:
1326 die(_("ssh variant 'simple' does not support -6"));
1330 case VARIANT_TORTOISEPLINK
:
1331 strvec_push(args
, "-6");
1335 if (variant
== VARIANT_TORTOISEPLINK
)
1336 strvec_push(args
, "-batch");
1341 BUG("VARIANT_AUTO passed to push_ssh_options");
1342 case VARIANT_SIMPLE
:
1343 die(_("ssh variant 'simple' does not support setting port"));
1345 strvec_push(args
, "-p");
1349 case VARIANT_TORTOISEPLINK
:
1350 strvec_push(args
, "-P");
1353 strvec_push(args
, port
);
1357 /* Prepare a child_process for use by Git's SSH-tunneled transport. */
1358 static void fill_ssh_args(struct child_process
*conn
, const char *ssh_host
,
1359 const char *port
, enum protocol_version version
,
1363 enum ssh_variant variant
;
1365 if (looks_like_command_line_option(ssh_host
))
1366 die(_("strange hostname '%s' blocked"), ssh_host
);
1368 ssh
= get_ssh_command();
1370 variant
= determine_ssh_variant(ssh
, 1);
1373 * GIT_SSH is the no-shell version of
1374 * GIT_SSH_COMMAND (and must remain so for
1375 * historical compatibility).
1377 conn
->use_shell
= 0;
1379 ssh
= getenv("GIT_SSH");
1382 variant
= determine_ssh_variant(ssh
, 0);
1385 if (variant
== VARIANT_AUTO
) {
1386 struct child_process detect
= CHILD_PROCESS_INIT
;
1388 detect
.use_shell
= conn
->use_shell
;
1389 detect
.no_stdin
= detect
.no_stdout
= detect
.no_stderr
= 1;
1391 strvec_push(&detect
.args
, ssh
);
1392 strvec_push(&detect
.args
, "-G");
1393 push_ssh_options(&detect
.args
, &detect
.env
,
1394 VARIANT_SSH
, port
, version
, flags
);
1395 strvec_push(&detect
.args
, ssh_host
);
1397 variant
= run_command(&detect
) ? VARIANT_SIMPLE
: VARIANT_SSH
;
1400 strvec_push(&conn
->args
, ssh
);
1401 push_ssh_options(&conn
->args
, &conn
->env
, variant
, port
, version
,
1403 strvec_push(&conn
->args
, ssh_host
);
1407 * This returns the dummy child_process `no_fork` if the transport protocol
1408 * does not need fork(2), or a struct child_process object if it does. Once
1409 * done, finish the connection with finish_connect() with the value returned
1410 * from this function (it is safe to call finish_connect() with NULL to
1411 * support the former case).
1413 * If it returns, the connect is successful; it just dies on errors (this
1414 * will hopefully be changed in a libification effort, to return NULL when
1415 * the connection failed).
1417 struct child_process
*git_connect(int fd
[2], const char *url
,
1419 const char *prog
, int flags
)
1421 char *hostandport
, *path
;
1422 struct child_process
*conn
;
1423 enum protocol protocol
;
1424 enum protocol_version version
= get_protocol_version_config();
1427 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1428 * to perform any operation that doesn't involve upload-pack (i.e., a
1429 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
1430 * to do anything else (like push or remote archive) via v2.
1432 if (version
== protocol_v2
&& strcmp("git-upload-pack", name
))
1433 version
= protocol_v0
;
1435 /* Without this we cannot rely on waitpid() to tell
1436 * what happened to our children.
1438 signal(SIGCHLD
, SIG_DFL
);
1440 protocol
= parse_connect_url(url
, &hostandport
, &path
);
1441 if ((flags
& CONNECT_DIAG_URL
) && (protocol
!= PROTO_SSH
)) {
1442 printf("Diag: url=%s\n", url
? url
: "NULL");
1443 printf("Diag: protocol=%s\n", prot_name(protocol
));
1444 printf("Diag: hostandport=%s\n", hostandport
? hostandport
: "NULL");
1445 printf("Diag: path=%s\n", path
? path
: "NULL");
1447 } else if (protocol
== PROTO_GIT
) {
1448 conn
= git_connect_git(fd
, hostandport
, path
, prog
, version
, flags
);
1449 conn
->trace2_child_class
= "transport/git";
1451 struct strbuf cmd
= STRBUF_INIT
;
1452 const char *const *var
;
1454 conn
= xmalloc(sizeof(*conn
));
1455 child_process_init(conn
);
1457 if (looks_like_command_line_option(path
))
1458 die(_("strange pathname '%s' blocked"), path
);
1460 strbuf_addstr(&cmd
, prog
);
1461 strbuf_addch(&cmd
, ' ');
1462 sq_quote_buf(&cmd
, path
);
1464 /* remove repo-local variables from the environment */
1465 for (var
= local_repo_env
; *var
; var
++)
1466 strvec_push(&conn
->env
, *var
);
1468 conn
->use_shell
= 1;
1469 conn
->in
= conn
->out
= -1;
1470 if (protocol
== PROTO_SSH
) {
1471 char *ssh_host
= hostandport
;
1472 const char *port
= NULL
;
1473 transport_check_allowed("ssh");
1474 get_host_and_port(&ssh_host
, &port
);
1477 port
= get_port(ssh_host
);
1479 if (flags
& CONNECT_DIAG_URL
) {
1480 printf("Diag: url=%s\n", url
? url
: "NULL");
1481 printf("Diag: protocol=%s\n", prot_name(protocol
));
1482 printf("Diag: userandhost=%s\n", ssh_host
? ssh_host
: "NULL");
1483 printf("Diag: port=%s\n", port
? port
: "NONE");
1484 printf("Diag: path=%s\n", path
? path
: "NULL");
1489 strbuf_release(&cmd
);
1492 conn
->trace2_child_class
= "transport/ssh";
1493 fill_ssh_args(conn
, ssh_host
, port
, version
, flags
);
1495 transport_check_allowed("file");
1496 conn
->trace2_child_class
= "transport/file";
1498 strvec_pushf(&conn
->env
,
1499 GIT_PROTOCOL_ENVIRONMENT
"=version=%d",
1503 strvec_push(&conn
->args
, cmd
.buf
);
1505 if (start_command(conn
))
1506 die(_("unable to fork"));
1508 fd
[0] = conn
->out
; /* read from child's stdout */
1509 fd
[1] = conn
->in
; /* write to child's stdin */
1510 strbuf_release(&cmd
);
1517 int finish_connect(struct child_process
*conn
)
1520 if (!conn
|| git_connection_is_socket(conn
))
1523 code
= finish_command(conn
);