]> git.ipfire.org Git - thirdparty/git.git/blame - connect.c
l10n: fr.po v2.18 round 1
[thirdparty/git.git] / connect.c
CommitLineData
731043fd 1#include "git-compat-util.h"
f7192598 2#include "cache.h"
b2141fc1 3#include "config.h"
41cb7488 4#include "pkt-line.h"
b10d0ec7 5#include "quote.h"
6abf5c0c 6#include "refs.h"
15a1c012 7#include "run-command.h"
6b62816c 8#include "remote.h"
47a59185 9#include "connect.h"
9d2e9420 10#include "url.h"
a45b5f05 11#include "string-list.h"
13eb4626 12#include "sha1-array.h"
a5adaced 13#include "transport.h"
0cd83283 14#include "strbuf.h"
e52449b6 15#include "version.h"
2609043d 16#include "protocol.h"
f7192598 17
e52449b6
BW
18static char *server_capabilities_v1;
19static struct argv_array server_capabilities_v2 = ARGV_ARRAY_INIT;
5d54cffc 20static const char *parse_feature_value(const char *, const char *, int *);
211b5f9e 21
be0b3f82 22static int check_ref(const char *name, unsigned int flags)
2718ff09
LT
23{
24 if (!flags)
25 return 1;
26
be0b3f82 27 if (!skip_prefix(name, "refs/", &name))
2718ff09
LT
28 return 0;
29
2718ff09 30 /* REF_NORMAL means that we don't want the magic fake tag refs */
8d9c5010 31 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
2718ff09
LT
32 return 0;
33
34 /* REF_HEADS means that we want regular branch heads */
be0b3f82 35 if ((flags & REF_HEADS) && starts_with(name, "heads/"))
2718ff09
LT
36 return 1;
37
38 /* REF_TAGS means that we want tags */
be0b3f82 39 if ((flags & REF_TAGS) && starts_with(name, "tags/"))
2718ff09
LT
40 return 1;
41
42 /* All type bits clear means that we are ok with anything */
43 return !(flags & ~REF_NORMAL);
44}
45
4577370e
DB
46int check_ref_type(const struct ref *ref, int flags)
47{
be0b3f82 48 return check_ref(ref->name, flags);
4577370e
DB
49}
50
d2bff22c 51static NORETURN void die_initial_contact(int unexpected)
46284dd1 52{
7e3e479b
BW
53 /*
54 * A hang-up after seeing some response from the other end
55 * means that it is unexpected, as we know the other end is
56 * willing to talk to us. A hang-up before seeing any
57 * response does not necessarily mean an ACL problem, though.
58 */
55e4f936 59 if (unexpected)
f2b93b38 60 die(_("The remote end hung up upon initial contact"));
46284dd1 61 else
f2b93b38
VA
62 die(_("Could not read from remote repository.\n\n"
63 "Please make sure you have the correct access rights\n"
64 "and the repository exists."));
46284dd1
HV
65}
66
e52449b6
BW
67/* Checks if the server supports the capability 'c' */
68int server_supports_v2(const char *c, int die_on_error)
69{
70 int i;
71
72 for (i = 0; i < server_capabilities_v2.argc; i++) {
73 const char *out;
74 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
75 (!*out || *out == '='))
76 return 1;
77 }
78
79 if (die_on_error)
80 die("server doesn't support '%s'", c);
81
82 return 0;
83}
84
f7e20501
BW
85int server_supports_feature(const char *c, const char *feature,
86 int die_on_error)
87{
88 int i;
89
90 for (i = 0; i < server_capabilities_v2.argc; i++) {
91 const char *out;
92 if (skip_prefix(server_capabilities_v2.argv[i], c, &out) &&
93 (!*out || *(out++) == '=')) {
94 if (parse_feature_request(out, feature))
95 return 1;
96 else
97 break;
98 }
99 }
100
101 if (die_on_error)
102 die("server doesn't support feature '%s'", feature);
103
104 return 0;
105}
106
e52449b6
BW
107static void process_capabilities_v2(struct packet_reader *reader)
108{
109 while (packet_reader_read(reader) == PACKET_READ_NORMAL)
110 argv_array_push(&server_capabilities_v2, reader->line);
111
112 if (reader->status != PACKET_READ_FLUSH)
113 die("expected flush after capabilities");
114}
115
ad6ac124 116enum protocol_version discover_version(struct packet_reader *reader)
7e3e479b
BW
117{
118 enum protocol_version version = protocol_unknown_version;
119
120 /*
121 * Peek the first line of the server's response to
122 * determine the protocol version the server is speaking.
123 */
124 switch (packet_reader_peek(reader)) {
125 case PACKET_READ_EOF:
126 die_initial_contact(0);
127 case PACKET_READ_FLUSH:
128 case PACKET_READ_DELIM:
129 version = protocol_v0;
130 break;
131 case PACKET_READ_NORMAL:
132 version = determine_protocol_version_client(reader->line);
133 break;
134 }
135
136 switch (version) {
8f6982b4 137 case protocol_v2:
e52449b6 138 process_capabilities_v2(reader);
8f6982b4 139 break;
7e3e479b
BW
140 case protocol_v1:
141 /* Read the peeked version line */
142 packet_reader_read(reader);
143 break;
144 case protocol_v0:
145 break;
146 case protocol_unknown_version:
147 BUG("unknown protocol version");
148 }
149
150 return version;
151}
152
a45b5f05
JH
153static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
154{
155 char *sym, *target;
156 struct string_list_item *item;
157
158 if (!len)
159 return; /* just "symref" */
160 /* e.g. "symref=HEAD:refs/heads/master" */
5c0b13f8 161 sym = xmemdupz(val, len);
a45b5f05
JH
162 target = strchr(sym, ':');
163 if (!target)
164 /* just "symref=something" */
165 goto reject;
166 *(target++) = '\0';
167 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
168 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
169 /* "symref=bogus:pair */
170 goto reject;
ef4fe561 171 item = string_list_append_nodup(symref, sym);
a45b5f05
JH
172 item->util = target;
173 return;
174reject:
175 free(sym);
176 return;
177}
178
179static void annotate_refs_with_symref_info(struct ref *ref)
180{
181 struct string_list symref = STRING_LIST_INIT_DUP;
e52449b6 182 const char *feature_list = server_capabilities_v1;
a45b5f05
JH
183
184 while (feature_list) {
185 int len;
186 const char *val;
187
188 val = parse_feature_value(feature_list, "symref", &len);
189 if (!val)
190 break;
191 parse_one_symref_info(&symref, val, len);
192 feature_list = val + 1;
193 }
3383e199 194 string_list_sort(&symref);
a45b5f05
JH
195
196 for (; ref; ref = ref->next) {
197 struct string_list_item *item;
198 item = string_list_lookup(&symref, ref->name);
199 if (!item)
200 continue;
201 ref->symref = xstrdup((char *)item->util);
202 }
203 string_list_clear(&symref, 0);
204}
205
7e3e479b 206static void process_capabilities(const char *line, int *len)
2609043d 207{
7e3e479b 208 int nul_location = strlen(line);
0cd83283
JT
209 if (nul_location == *len)
210 return;
e52449b6 211 server_capabilities_v1 = xstrdup(line + nul_location + 1);
0cd83283
JT
212 *len = nul_location;
213}
214
7e3e479b 215static int process_dummy_ref(const char *line)
0cd83283
JT
216{
217 struct object_id oid;
218 const char *name;
219
7e3e479b 220 if (parse_oid_hex(line, &oid, &name))
0cd83283
JT
221 return 0;
222 if (*name != ' ')
223 return 0;
224 name++;
225
226 return !oidcmp(&null_oid, &oid) && !strcmp(name, "capabilities^{}");
227}
228
7e3e479b 229static void check_no_capabilities(const char *line, int len)
0cd83283 230{
7e3e479b 231 if (strlen(line) != len)
0cd83283 232 warning("Ignoring capabilities after first line '%s'",
7e3e479b 233 line + strlen(line));
0cd83283
JT
234}
235
7e3e479b
BW
236static int process_ref(const char *line, int len, struct ref ***list,
237 unsigned int flags, struct oid_array *extra_have)
0cd83283
JT
238{
239 struct object_id old_oid;
240 const char *name;
241
7e3e479b 242 if (parse_oid_hex(line, &old_oid, &name))
0cd83283
JT
243 return 0;
244 if (*name != ' ')
245 return 0;
246 name++;
247
248 if (extra_have && !strcmp(name, ".have")) {
249 oid_array_append(extra_have, &old_oid);
250 } else if (!strcmp(name, "capabilities^{}")) {
251 die("protocol error: unexpected capabilities^{}");
252 } else if (check_ref(name, flags)) {
253 struct ref *ref = alloc_ref(name);
254 oidcpy(&ref->old_oid, &old_oid);
255 **list = ref;
256 *list = &ref->next;
257 }
7e3e479b 258 check_no_capabilities(line, len);
0cd83283
JT
259 return 1;
260}
261
7e3e479b
BW
262static int process_shallow(const char *line, int len,
263 struct oid_array *shallow_points)
0cd83283
JT
264{
265 const char *arg;
266 struct object_id old_oid;
267
7e3e479b 268 if (!skip_prefix(line, "shallow ", &arg))
0cd83283
JT
269 return 0;
270
271 if (get_oid_hex(arg, &old_oid))
272 die("protocol error: expected shallow sha-1, got '%s'", arg);
273 if (!shallow_points)
274 die("repository on the other end cannot be shallow");
275 oid_array_append(shallow_points, &old_oid);
7e3e479b 276 check_no_capabilities(line, len);
0cd83283
JT
277 return 1;
278}
279
7e3e479b
BW
280enum get_remote_heads_state {
281 EXPECTING_FIRST_REF = 0,
282 EXPECTING_REF,
283 EXPECTING_SHALLOW,
284 EXPECTING_DONE,
285};
286
d1c133f5
LT
287/*
288 * Read all the refs from the other end
289 */
ad6ac124 290struct ref **get_remote_heads(struct packet_reader *reader,
85edf4f5 291 struct ref **list, unsigned int flags,
910650d2 292 struct oid_array *extra_have,
293 struct oid_array *shallow_points)
d1c133f5 294{
a45b5f05 295 struct ref **orig_list = list;
7e3e479b
BW
296 int len = 0;
297 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
7e3e479b 298 const char *arg;
55e4f936 299
d1c133f5 300 *list = NULL;
1a7141ff 301
7e3e479b 302 while (state != EXPECTING_DONE) {
ad6ac124 303 switch (packet_reader_read(reader)) {
7e3e479b
BW
304 case PACKET_READ_EOF:
305 die_initial_contact(1);
306 case PACKET_READ_NORMAL:
ad6ac124
BW
307 len = reader->pktlen;
308 if (len > 4 && skip_prefix(reader->line, "ERR ", &arg))
7e3e479b
BW
309 die("remote error: %s", arg);
310 break;
311 case PACKET_READ_FLUSH:
312 state = EXPECTING_DONE;
313 break;
314 case PACKET_READ_DELIM:
315 die("invalid packet");
316 }
317
0cd83283
JT
318 switch (state) {
319 case EXPECTING_FIRST_REF:
ad6ac124
BW
320 process_capabilities(reader->line, &len);
321 if (process_dummy_ref(reader->line)) {
0cd83283
JT
322 state = EXPECTING_SHALLOW;
323 break;
324 }
325 state = EXPECTING_REF;
326 /* fallthrough */
327 case EXPECTING_REF:
ad6ac124 328 if (process_ref(reader->line, len, &list, flags, extra_have))
0cd83283
JT
329 break;
330 state = EXPECTING_SHALLOW;
331 /* fallthrough */
332 case EXPECTING_SHALLOW:
ad6ac124 333 if (process_shallow(reader->line, len, shallow_points))
0cd83283 334 break;
ad6ac124 335 die("protocol error: unexpected '%s'", reader->line);
7e3e479b
BW
336 case EXPECTING_DONE:
337 break;
211b5f9e 338 }
d1c133f5 339 }
a45b5f05
JH
340
341 annotate_refs_with_symref_info(*orig_list);
342
d1c133f5
LT
343 return list;
344}
345
e52449b6
BW
346/* Returns 1 when a valid ref has been added to `list`, 0 otherwise */
347static int process_ref_v2(const char *line, struct ref ***list)
348{
349 int ret = 1;
350 int i = 0;
351 struct object_id old_oid;
352 struct ref *ref;
353 struct string_list line_sections = STRING_LIST_INIT_DUP;
354 const char *end;
355
356 /*
357 * Ref lines have a number of fields which are space deliminated. The
358 * first field is the OID of the ref. The second field is the ref
359 * name. Subsequent fields (symref-target and peeled) are optional and
360 * don't have a particular order.
361 */
362 if (string_list_split(&line_sections, line, ' ', -1) < 2) {
363 ret = 0;
364 goto out;
365 }
366
367 if (parse_oid_hex(line_sections.items[i++].string, &old_oid, &end) ||
368 *end) {
369 ret = 0;
370 goto out;
371 }
372
373 ref = alloc_ref(line_sections.items[i++].string);
374
375 oidcpy(&ref->old_oid, &old_oid);
376 **list = ref;
377 *list = &ref->next;
378
379 for (; i < line_sections.nr; i++) {
380 const char *arg = line_sections.items[i].string;
381 if (skip_prefix(arg, "symref-target:", &arg))
382 ref->symref = xstrdup(arg);
383
384 if (skip_prefix(arg, "peeled:", &arg)) {
385 struct object_id peeled_oid;
386 char *peeled_name;
387 struct ref *peeled;
388 if (parse_oid_hex(arg, &peeled_oid, &end) || *end) {
389 ret = 0;
390 goto out;
391 }
392
393 peeled_name = xstrfmt("%s^{}", ref->name);
394 peeled = alloc_ref(peeled_name);
395
396 oidcpy(&peeled->old_oid, &peeled_oid);
397 **list = peeled;
398 *list = &peeled->next;
399
400 free(peeled_name);
401 }
402 }
403
404out:
405 string_list_clear(&line_sections, 0);
406 return ret;
407}
408
409struct ref **get_remote_refs(int fd_out, struct packet_reader *reader,
410 struct ref **list, int for_push,
ff473221
BW
411 const struct argv_array *ref_prefixes,
412 const struct string_list *server_options)
e52449b6
BW
413{
414 int i;
415 *list = NULL;
416
417 if (server_supports_v2("ls-refs", 1))
418 packet_write_fmt(fd_out, "command=ls-refs\n");
419
420 if (server_supports_v2("agent", 0))
421 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
422
ff473221
BW
423 if (server_options && server_options->nr &&
424 server_supports_v2("server-option", 1))
425 for (i = 0; i < server_options->nr; i++)
426 packet_write_fmt(fd_out, "server-option=%s",
427 server_options->items[i].string);
428
e52449b6
BW
429 packet_delim(fd_out);
430 /* When pushing we don't want to request the peeled tags */
431 if (!for_push)
432 packet_write_fmt(fd_out, "peel\n");
433 packet_write_fmt(fd_out, "symrefs\n");
434 for (i = 0; ref_prefixes && i < ref_prefixes->argc; i++) {
435 packet_write_fmt(fd_out, "ref-prefix %s\n",
436 ref_prefixes->argv[i]);
437 }
438 packet_flush(fd_out);
439
440 /* Process response from server */
441 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
442 if (!process_ref_v2(reader->line, &list))
443 die("invalid ls-refs response: %s", reader->line);
444 }
445
446 if (reader->status != PACKET_READ_FLUSH)
447 die("expected flush after ref listing");
448
449 return list;
450}
451
5d54cffc 452static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
f47182c8
JH
453{
454 int len;
455
456 if (!feature_list)
457 return NULL;
458
459 len = strlen(feature);
460 while (*feature_list) {
461 const char *found = strstr(feature_list, feature);
462 if (!found)
463 return NULL;
94427108
JK
464 if (feature_list == found || isspace(found[-1])) {
465 const char *value = found + len;
466 /* feature with no value (e.g., "thin-pack") */
467 if (!*value || isspace(*value)) {
468 if (lenp)
469 *lenp = 0;
470 return value;
471 }
472 /* feature with a value (e.g., "agent=git/1.2.3") */
473 else if (*value == '=') {
474 value++;
475 if (lenp)
476 *lenp = strcspn(value, " \t\n");
477 return value;
478 }
479 /*
480 * otherwise we matched a substring of another feature;
481 * keep looking
482 */
483 }
f47182c8
JH
484 feature_list = found + 1;
485 }
486 return NULL;
211b5f9e
JS
487}
488
94427108
JK
489int parse_feature_request(const char *feature_list, const char *feature)
490{
491 return !!parse_feature_value(feature_list, feature, NULL);
492}
493
494const char *server_feature_value(const char *feature, int *len)
495{
e52449b6 496 return parse_feature_value(server_capabilities_v1, feature, len);
94427108
JK
497}
498
499int server_supports(const char *feature)
500{
501 return !!server_feature_value(feature, NULL);
502}
503
2386d658
LT
504enum protocol {
505 PROTO_LOCAL = 1,
c59ab2e5 506 PROTO_FILE,
2386d658 507 PROTO_SSH,
4b05548f 508 PROTO_GIT
2386d658
LT
509};
510
c59ab2e5
TB
511int url_is_local_not_ssh(const char *url)
512{
513 const char *colon = strchr(url, ':');
514 const char *slash = strchr(url, '/');
515 return !colon || (slash && slash < colon) ||
516 has_dos_drive_prefix(url);
517}
518
5610b7c0
TB
519static const char *prot_name(enum protocol protocol)
520{
521 switch (protocol) {
522 case PROTO_LOCAL:
c59ab2e5 523 case PROTO_FILE:
5610b7c0
TB
524 return "file";
525 case PROTO_SSH:
526 return "ssh";
527 case PROTO_GIT:
528 return "git";
529 default:
83e6bda3 530 return "unknown protocol";
5610b7c0
TB
531 }
532}
533
2386d658
LT
534static enum protocol get_protocol(const char *name)
535{
536 if (!strcmp(name, "ssh"))
537 return PROTO_SSH;
538 if (!strcmp(name, "git"))
539 return PROTO_GIT;
07c7782c 540 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
c05186cc 541 return PROTO_SSH;
07c7782c 542 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
c05186cc 543 return PROTO_SSH;
72a4f4b6 544 if (!strcmp(name, "file"))
c59ab2e5 545 return PROTO_FILE;
2386d658
LT
546 die("I don't handle protocol '%s'", name);
547}
548
86ceb337
TB
549static char *host_end(char **hoststart, int removebrackets)
550{
551 char *host = *hoststart;
552 char *end;
553 char *start = strstr(host, "@[");
554 if (start)
555 start++; /* Jump over '@' */
556 else
557 start = host;
558 if (start[0] == '[') {
559 end = strchr(start + 1, ']');
560 if (end) {
561 if (removebrackets) {
562 *end = 0;
563 memmove(start, start + 1, end - start);
564 end++;
565 }
566 } else
567 end = host;
568 } else
569 end = host;
570 return end;
571}
572
5ba88448
YH
573#define STR_(s) # s
574#define STR(s) STR_(s)
2386d658 575
72a534da
ML
576static void get_host_and_port(char **host, const char **port)
577{
578 char *colon, *end;
86ceb337 579 end = host_end(host, 1);
72a534da 580 colon = strchr(end, ':');
72a534da 581 if (colon) {
86ceb337
TB
582 long portnr = strtol(colon + 1, &end, 10);
583 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
584 *colon = 0;
585 *port = colon + 1;
6b6c5f7a
TB
586 } else if (!colon[1]) {
587 *colon = 0;
86ceb337 588 }
72a534da
ML
589 }
590}
591
e47a8583
EW
592static void enable_keepalive(int sockfd)
593{
594 int ka = 1;
595
596 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
597 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
598 strerror(errno));
599}
600
49744d63 601#ifndef NO_IPV6
4c505f71 602
ba505322
AR
603static const char *ai_name(const struct addrinfo *ai)
604{
785a9857
BK
605 static char addr[NI_MAXHOST];
606 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
607 NI_NUMERICHOST) != 0)
5096d490 608 xsnprintf(addr, sizeof(addr), "(unknown)");
785a9857 609
ba505322
AR
610 return addr;
611}
612
5ad312be
JL
613/*
614 * Returns a connected socket() fd, or else die()s.
615 */
7841ce79 616static int git_tcp_connect_sock(char *host, int flags)
2386d658 617{
63a995b6
DZ
618 struct strbuf error_message = STRBUF_INIT;
619 int sockfd = -1;
554fe20d 620 const char *port = STR(DEFAULT_GIT_PORT);
5ba88448
YH
621 struct addrinfo hints, *ai0, *ai;
622 int gai;
ba505322 623 int cnt = 0;
5ba88448 624
72a534da
ML
625 get_host_and_port(&host, &port);
626 if (!*port)
627 port = "<none>";
5ba88448
YH
628
629 memset(&hints, 0, sizeof(hints));
c915f11e
EW
630 if (flags & CONNECT_IPV4)
631 hints.ai_family = AF_INET;
632 else if (flags & CONNECT_IPV6)
633 hints.ai_family = AF_INET6;
5ba88448
YH
634 hints.ai_socktype = SOCK_STREAM;
635 hints.ai_protocol = IPPROTO_TCP;
636
7841ce79
MT
637 if (flags & CONNECT_VERBOSE)
638 fprintf(stderr, "Looking up %s ... ", host);
639
5ba88448
YH
640 gai = getaddrinfo(host, port, &hints, &ai);
641 if (gai)
608d48b2 642 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
5ba88448 643
7841ce79
MT
644 if (flags & CONNECT_VERBOSE)
645 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
646
e08afecd 647 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
5ad312be
JL
648 sockfd = socket(ai->ai_family,
649 ai->ai_socktype, ai->ai_protocol);
63a995b6
DZ
650 if ((sockfd < 0) ||
651 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
652 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
653 host, cnt, ai_name(ai), strerror(errno));
654 if (0 <= sockfd)
655 close(sockfd);
5ba88448
YH
656 sockfd = -1;
657 continue;
2386d658 658 }
ba505322
AR
659 if (flags & CONNECT_VERBOSE)
660 fprintf(stderr, "%s ", ai_name(ai));
5ba88448 661 break;
2386d658
LT
662 }
663
5ba88448 664 freeaddrinfo(ai0);
2386d658 665
2386d658 666 if (sockfd < 0)
63a995b6 667 die("unable to connect to %s:\n%s", host, error_message.buf);
5ba88448 668
e47a8583
EW
669 enable_keepalive(sockfd);
670
7841ce79
MT
671 if (flags & CONNECT_VERBOSE)
672 fprintf(stderr, "done.\n");
673
63a995b6
DZ
674 strbuf_release(&error_message);
675
5ad312be 676 return sockfd;
2386d658
LT
677}
678
49744d63 679#else /* NO_IPV6 */
4c505f71 680
5ad312be
JL
681/*
682 * Returns a connected socket() fd, or else die()s.
683 */
7841ce79 684static int git_tcp_connect_sock(char *host, int flags)
4c505f71 685{
7203a2d1
EFL
686 struct strbuf error_message = STRBUF_INIT;
687 int sockfd = -1;
72a534da
ML
688 const char *port = STR(DEFAULT_GIT_PORT);
689 char *ep;
4c505f71
PA
690 struct hostent *he;
691 struct sockaddr_in sa;
692 char **ap;
693 unsigned int nport;
ba505322 694 int cnt;
4c505f71 695
72a534da 696 get_host_and_port(&host, &port);
4c505f71 697
7841ce79
MT
698 if (flags & CONNECT_VERBOSE)
699 fprintf(stderr, "Looking up %s ... ", host);
700
4c505f71
PA
701 he = gethostbyname(host);
702 if (!he)
703 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
704 nport = strtoul(port, &ep, 10);
705 if ( ep == port || *ep ) {
706 /* Not numeric */
707 struct servent *se = getservbyname(port,"tcp");
708 if ( !se )
d7530708 709 die("Unknown port %s", port);
4c505f71
PA
710 nport = se->s_port;
711 }
712
7841ce79
MT
713 if (flags & CONNECT_VERBOSE)
714 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
715
ba505322 716 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
4c505f71
PA
717 memset(&sa, 0, sizeof sa);
718 sa.sin_family = he->h_addrtype;
6573faff 719 sa.sin_port = htons(nport);
c6164218 720 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71 721
7203a2d1
EFL
722 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
723 if ((sockfd < 0) ||
724 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
725 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
ba505322
AR
726 host,
727 cnt,
728 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
7203a2d1
EFL
729 strerror(errno));
730 if (0 <= sockfd)
731 close(sockfd);
4c505f71
PA
732 sockfd = -1;
733 continue;
734 }
ba505322
AR
735 if (flags & CONNECT_VERBOSE)
736 fprintf(stderr, "%s ",
737 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
4c505f71
PA
738 break;
739 }
740
741 if (sockfd < 0)
7203a2d1 742 die("unable to connect to %s:\n%s", host, error_message.buf);
4c505f71 743
e47a8583
EW
744 enable_keepalive(sockfd);
745
7841ce79
MT
746 if (flags & CONNECT_VERBOSE)
747 fprintf(stderr, "done.\n");
748
5ad312be
JL
749 return sockfd;
750}
751
752#endif /* NO_IPV6 */
753
754
8e349780
JN
755/*
756 * Dummy child_process returned by git_connect() if the transport protocol
757 * does not need fork(2).
758 */
759static struct child_process no_fork = CHILD_PROCESS_INIT;
760
761int git_connection_is_socket(struct child_process *conn)
762{
763 return conn == &no_fork;
764}
765
766static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
5ad312be 767{
7841ce79 768 int sockfd = git_tcp_connect_sock(host, flags);
5ad312be 769
4c505f71 770 fd[0] = sockfd;
ec587fde 771 fd[1] = dup(sockfd);
8e349780
JN
772
773 return &no_fork;
4c505f71
PA
774}
775
4c505f71 776
96f1e58f 777static char *git_proxy_command;
f8014776 778
ef90d6d4
JS
779static int git_proxy_command_options(const char *var, const char *value,
780 void *cb)
f8014776 781{
e814bc4d 782 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
783 const char *for_pos;
784 int matchlen = -1;
785 int hostlen;
15112c95
EFL
786 const char *rhost_name = cb;
787 int rhost_len = strlen(rhost_name);
c3df8568 788
e814bc4d 789 if (git_proxy_command)
f8014776 790 return 0;
c64b9ad0
JH
791 if (!value)
792 return config_error_nonbool(var);
e814bc4d
JH
793 /* [core]
794 * ;# matches www.kernel.org as well
795 * gitproxy = netcatter-1 for kernel.org
796 * gitproxy = netcatter-2 for sample.xz
797 * gitproxy = netcatter-default
798 */
c3df8568 799 for_pos = strstr(value, " for ");
e814bc4d
JH
800 if (!for_pos)
801 /* matches everybody */
802 matchlen = strlen(value);
803 else {
804 hostlen = strlen(for_pos + 5);
805 if (rhost_len < hostlen)
806 matchlen = -1;
807 else if (!strncmp(for_pos + 5,
808 rhost_name + rhost_len - hostlen,
809 hostlen) &&
810 ((rhost_len == hostlen) ||
811 rhost_name[rhost_len - hostlen -1] == '.'))
812 matchlen = for_pos - value;
813 else
814 matchlen = -1;
815 }
816 if (0 <= matchlen) {
817 /* core.gitproxy = none for kernel.org */
a6080a0a 818 if (matchlen == 4 &&
e814bc4d
JH
819 !memcmp(value, "none", 4))
820 matchlen = 0;
182af834 821 git_proxy_command = xmemdupz(value, matchlen);
f8014776 822 }
e814bc4d 823 return 0;
f8014776
PC
824 }
825
ef90d6d4 826 return git_default_config(var, value, cb);
f8014776
PC
827}
828
e814bc4d 829static int git_use_proxy(const char *host)
f8014776
PC
830{
831 git_proxy_command = getenv("GIT_PROXY_COMMAND");
15112c95 832 git_config(git_proxy_command_options, (void*)host);
e814bc4d 833 return (git_proxy_command && *git_proxy_command);
f8014776
PC
834}
835
5cbf8246 836static struct child_process *git_proxy_connect(int fd[2], char *host)
f8014776 837{
554fe20d 838 const char *port = STR(DEFAULT_GIT_PORT);
5cbf8246 839 struct child_process *proxy;
f8014776 840
72a534da 841 get_host_and_port(&host, &port);
f8014776 842
3be4cf09
JK
843 if (looks_like_command_line_option(host))
844 die("strange hostname '%s' blocked", host);
845 if (looks_like_command_line_option(port))
846 die("strange port '%s' blocked", port);
847
483bbd4e
RS
848 proxy = xmalloc(sizeof(*proxy));
849 child_process_init(proxy);
1823bea1
JK
850 argv_array_push(&proxy->args, git_proxy_command);
851 argv_array_push(&proxy->args, host);
852 argv_array_push(&proxy->args, port);
5cbf8246
JK
853 proxy->in = -1;
854 proxy->out = -1;
855 if (start_command(proxy))
1823bea1 856 die("cannot start proxy %s", git_proxy_command);
5cbf8246
JK
857 fd[0] = proxy->out; /* read from proxy stdout */
858 fd[1] = proxy->in; /* write to proxy stdin */
859 return proxy;
f8014776
PC
860}
861
86ceb337 862static char *get_port(char *host)
2e776665
LT
863{
864 char *end;
86ceb337
TB
865 char *p = strchr(host, ':');
866
2e776665 867 if (p) {
8f148253
RS
868 long port = strtol(p + 1, &end, 10);
869 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
86ceb337
TB
870 *p = '\0';
871 return p+1;
2e776665
LT
872 }
873 }
874
875 return NULL;
876}
877
f7192598 878/*
cabc3c12
JS
879 * Extract protocol and relevant parts from the specified connection URL.
880 * The caller must free() the returned strings.
f7192598 881 */
cabc3c12 882static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
83b05875 883 char **ret_path)
f7192598 884{
9d2e9420 885 char *url;
8e76bf3f 886 char *host, *path;
356bece0 887 char *end;
c59ab2e5 888 int separator = '/';
faea9ccb 889 enum protocol protocol = PROTO_LOCAL;
f0b7367c 890
9d2e9420
JK
891 if (is_url(url_orig))
892 url = url_decode(url_orig);
893 else
894 url = xstrdup(url_orig);
895
faea9ccb 896 host = strstr(url, "://");
eeefa7c9 897 if (host) {
faea9ccb
AE
898 *host = '\0';
899 protocol = get_protocol(url);
900 host += 3;
356bece0 901 } else {
f7192598 902 host = url;
c59ab2e5
TB
903 if (!url_is_local_not_ssh(url)) {
904 protocol = PROTO_SSH;
905 separator = ':';
906 }
356bece0
YH
907 }
908
9aa5053d 909 /*
83b05875
TB
910 * Don't do destructive transforms as protocol code does
911 * '[]' unwrapping in get_host_and_port()
9aa5053d 912 */
86ceb337 913 end = host_end(&host, 0);
356bece0 914
c59ab2e5 915 if (protocol == PROTO_LOCAL)
72a4f4b6 916 path = end;
c59ab2e5
TB
917 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
918 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
919 else
920 path = strchr(end, separator);
2386d658 921
faea9ccb
AE
922 if (!path || !*path)
923 die("No path specified. See 'man git-pull' for valid url syntax");
924
925 /*
926 * null-terminate hostname and point path to ~ for URL's like this:
927 * ssh://host.xz/~user/repo
928 */
c59ab2e5
TB
929
930 end = path; /* Need to \0 terminate host here */
931 if (separator == ':')
932 path++; /* path starts after ':' */
933 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
faea9ccb
AE
934 if (path[1] == '~')
935 path++;
faea9ccb
AE
936 }
937
c59ab2e5
TB
938 path = xstrdup(path);
939 *end = '\0';
940
cabc3c12 941 *ret_host = xstrdup(host);
c59ab2e5 942 *ret_path = path;
cabc3c12
JS
943 free(url);
944 return protocol;
945}
946
3c8ede3f
NTND
947static const char *get_ssh_command(void)
948{
949 const char *ssh;
950
951 if ((ssh = getenv("GIT_SSH_COMMAND")))
952 return ssh;
953
954 if (!git_config_get_string_const("core.sshcommand", &ssh))
955 return ssh;
956
957 return NULL;
958}
959
94b8ae5a 960enum ssh_variant {
0da0e49b 961 VARIANT_AUTO,
94b8ae5a
BW
962 VARIANT_SIMPLE,
963 VARIANT_SSH,
964 VARIANT_PLINK,
965 VARIANT_PUTTY,
966 VARIANT_TORTOISEPLINK,
967};
968
0da0e49b 969static void override_ssh_variant(enum ssh_variant *ssh_variant)
e2824e47 970{
94b8ae5a 971 const char *variant = getenv("GIT_SSH_VARIANT");
486c8e8c 972
94b8ae5a 973 if (!variant && git_config_get_string_const("ssh.variant", &variant))
0da0e49b 974 return;
486c8e8c 975
0da0e49b
JN
976 if (!strcmp(variant, "auto"))
977 *ssh_variant = VARIANT_AUTO;
978 else if (!strcmp(variant, "plink"))
94b8ae5a
BW
979 *ssh_variant = VARIANT_PLINK;
980 else if (!strcmp(variant, "putty"))
981 *ssh_variant = VARIANT_PUTTY;
982 else if (!strcmp(variant, "tortoiseplink"))
983 *ssh_variant = VARIANT_TORTOISEPLINK;
984 else if (!strcmp(variant, "simple"))
985 *ssh_variant = VARIANT_SIMPLE;
986 else
987 *ssh_variant = VARIANT_SSH;
486c8e8c
JH
988}
989
94b8ae5a
BW
990static enum ssh_variant determine_ssh_variant(const char *ssh_command,
991 int is_cmdline)
486c8e8c 992{
0da0e49b 993 enum ssh_variant ssh_variant = VARIANT_AUTO;
486c8e8c 994 const char *variant;
e2824e47
JS
995 char *p = NULL;
996
0da0e49b
JN
997 override_ssh_variant(&ssh_variant);
998
999 if (ssh_variant != VARIANT_AUTO)
94b8ae5a 1000 return ssh_variant;
486c8e8c
JH
1001
1002 if (!is_cmdline) {
e2824e47
JS
1003 p = xstrdup(ssh_command);
1004 variant = basename(p);
1005 } else {
1006 const char **ssh_argv;
1007
1008 p = xstrdup(ssh_command);
22e5ae5c 1009 if (split_cmdline(p, &ssh_argv) > 0) {
e2824e47
JS
1010 variant = basename((char *)ssh_argv[0]);
1011 /*
1012 * At this point, variant points into the buffer
1013 * referenced by p, hence we do not need ssh_argv
1014 * any longer.
1015 */
1016 free(ssh_argv);
5d2993b6
JK
1017 } else {
1018 free(p);
94b8ae5a 1019 return ssh_variant;
5d2993b6 1020 }
e2824e47
JS
1021 }
1022
94b8ae5a
BW
1023 if (!strcasecmp(variant, "ssh") ||
1024 !strcasecmp(variant, "ssh.exe"))
1025 ssh_variant = VARIANT_SSH;
1026 else if (!strcasecmp(variant, "plink") ||
1027 !strcasecmp(variant, "plink.exe"))
1028 ssh_variant = VARIANT_PLINK;
e2824e47 1029 else if (!strcasecmp(variant, "tortoiseplink") ||
94b8ae5a
BW
1030 !strcasecmp(variant, "tortoiseplink.exe"))
1031 ssh_variant = VARIANT_TORTOISEPLINK;
1032
e2824e47 1033 free(p);
94b8ae5a 1034 return ssh_variant;
e2824e47
JS
1035}
1036
2ac67cb6
JN
1037/*
1038 * Open a connection using Git's native protocol.
1039 *
1040 * The caller is responsible for freeing hostandport, but this function may
1041 * modify it (for example, to truncate it to remove the port part).
1042 */
1043static struct child_process *git_connect_git(int fd[2], char *hostandport,
1044 const char *path, const char *prog,
40fc51e3 1045 enum protocol_version version,
2ac67cb6
JN
1046 int flags)
1047{
1048 struct child_process *conn;
1049 struct strbuf request = STRBUF_INIT;
1050 /*
1051 * Set up virtual host information based on where we will
1052 * connect, unless the user has overridden us in
1053 * the environment.
1054 */
1055 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1056 if (target_host)
1057 target_host = xstrdup(target_host);
1058 else
1059 target_host = xstrdup(hostandport);
1060
1061 transport_check_allowed("git");
1062
233cd282
JN
1063 /*
1064 * These underlying connection commands die() if they
2ac67cb6
JN
1065 * cannot connect.
1066 */
1067 if (git_use_proxy(hostandport))
1068 conn = git_proxy_connect(fd, hostandport);
1069 else
1070 conn = git_tcp_connect(fd, hostandport, flags);
1071 /*
1072 * Separate original protocol components prog and path
1073 * from extended host header with a NUL byte.
1074 *
1075 * Note: Do not add any other headers here! Doing so
1076 * will cause older git-daemon servers to crash.
1077 */
1078 strbuf_addf(&request,
1079 "%s %s%chost=%s%c",
1080 prog, path, 0,
1081 target_host, 0);
1082
1083 /* If using a new version put that stuff here after a second null byte */
40fc51e3 1084 if (version > 0) {
2ac67cb6
JN
1085 strbuf_addch(&request, '\0');
1086 strbuf_addf(&request, "version=%d%c",
40fc51e3 1087 version, '\0');
2ac67cb6
JN
1088 }
1089
1090 packet_write(fd[1], request.buf, request.len);
1091
1092 free(target_host);
1093 strbuf_release(&request);
1094 return conn;
1095}
1096
957e2ad2
JN
1097/*
1098 * Append the appropriate environment variables to `env` and options to
1099 * `args` for running ssh in Git's SSH-tunneled transport.
1100 */
1101static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1102 enum ssh_variant variant, const char *port,
40fc51e3 1103 enum protocol_version version, int flags)
957e2ad2
JN
1104{
1105 if (variant == VARIANT_SSH &&
40fc51e3 1106 version > 0) {
957e2ad2
JN
1107 argv_array_push(args, "-o");
1108 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1109 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
40fc51e3 1110 version);
957e2ad2
JN
1111 }
1112
a3f5b66f
JN
1113 if (flags & CONNECT_IPV4) {
1114 switch (variant) {
1115 case VARIANT_AUTO:
1116 BUG("VARIANT_AUTO passed to push_ssh_options");
1117 case VARIANT_SIMPLE:
1118 die("ssh variant 'simple' does not support -4");
1119 case VARIANT_SSH:
1120 case VARIANT_PLINK:
1121 case VARIANT_PUTTY:
1122 case VARIANT_TORTOISEPLINK:
957e2ad2 1123 argv_array_push(args, "-4");
a3f5b66f
JN
1124 }
1125 } else if (flags & CONNECT_IPV6) {
1126 switch (variant) {
1127 case VARIANT_AUTO:
1128 BUG("VARIANT_AUTO passed to push_ssh_options");
1129 case VARIANT_SIMPLE:
1130 die("ssh variant 'simple' does not support -6");
1131 case VARIANT_SSH:
1132 case VARIANT_PLINK:
1133 case VARIANT_PUTTY:
1134 case VARIANT_TORTOISEPLINK:
957e2ad2 1135 argv_array_push(args, "-6");
a3f5b66f 1136 }
957e2ad2
JN
1137 }
1138
1139 if (variant == VARIANT_TORTOISEPLINK)
1140 argv_array_push(args, "-batch");
1141
3fa5e0d0
JN
1142 if (port) {
1143 switch (variant) {
1144 case VARIANT_AUTO:
1145 BUG("VARIANT_AUTO passed to push_ssh_options");
1146 case VARIANT_SIMPLE:
1147 die("ssh variant 'simple' does not support setting port");
1148 case VARIANT_SSH:
957e2ad2 1149 argv_array_push(args, "-p");
3fa5e0d0
JN
1150 break;
1151 case VARIANT_PLINK:
1152 case VARIANT_PUTTY:
1153 case VARIANT_TORTOISEPLINK:
957e2ad2 1154 argv_array_push(args, "-P");
3fa5e0d0 1155 }
957e2ad2
JN
1156
1157 argv_array_push(args, port);
1158 }
1159}
1160
fce54ce4
JN
1161/* Prepare a child_process for use by Git's SSH-tunneled transport. */
1162static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
40fc51e3
BW
1163 const char *port, enum protocol_version version,
1164 int flags)
fce54ce4
JN
1165{
1166 const char *ssh;
1167 enum ssh_variant variant;
1168
1169 if (looks_like_command_line_option(ssh_host))
1170 die("strange hostname '%s' blocked", ssh_host);
1171
1172 ssh = get_ssh_command();
1173 if (ssh) {
1174 variant = determine_ssh_variant(ssh, 1);
1175 } else {
1176 /*
1177 * GIT_SSH is the no-shell version of
1178 * GIT_SSH_COMMAND (and must remain so for
1179 * historical compatibility).
1180 */
1181 conn->use_shell = 0;
1182
1183 ssh = getenv("GIT_SSH");
1184 if (!ssh)
1185 ssh = "ssh";
1186 variant = determine_ssh_variant(ssh, 0);
1187 }
1188
0da0e49b
JN
1189 if (variant == VARIANT_AUTO) {
1190 struct child_process detect = CHILD_PROCESS_INIT;
1191
1192 detect.use_shell = conn->use_shell;
1193 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1194
1195 argv_array_push(&detect.args, ssh);
1196 argv_array_push(&detect.args, "-G");
1197 push_ssh_options(&detect.args, &detect.env_array,
40fc51e3 1198 VARIANT_SSH, port, version, flags);
0da0e49b
JN
1199 argv_array_push(&detect.args, ssh_host);
1200
1201 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1202 }
1203
fce54ce4 1204 argv_array_push(&conn->args, ssh);
40fc51e3 1205 push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
fce54ce4
JN
1206 argv_array_push(&conn->args, ssh_host);
1207}
1208
cabc3c12 1209/*
8e349780
JN
1210 * This returns the dummy child_process `no_fork` if the transport protocol
1211 * does not need fork(2), or a struct child_process object if it does. Once
1212 * done, finish the connection with finish_connect() with the value returned
1213 * from this function (it is safe to call finish_connect() with NULL to
1214 * support the former case).
cabc3c12
JS
1215 *
1216 * If it returns, the connect is successful; it just dies on errors (this
1217 * will hopefully be changed in a libification effort, to return NULL when
1218 * the connection failed).
1219 */
1220struct child_process *git_connect(int fd[2], const char *url,
1221 const char *prog, int flags)
1222{
a2036d7e 1223 char *hostandport, *path;
8e349780 1224 struct child_process *conn;
cabc3c12 1225 enum protocol protocol;
40fc51e3 1226 enum protocol_version version = get_protocol_version_config();
cabc3c12 1227
1aa8dded
BW
1228 /*
1229 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
1230 * to perform a push, then fallback to v0 since the client doesn't know
1231 * how to push yet using v2.
1232 */
1233 if (version == protocol_v2 && !strcmp("git-receive-pack", prog))
1234 version = protocol_v0;
1235
cabc3c12
JS
1236 /* Without this we cannot rely on waitpid() to tell
1237 * what happened to our children.
2e776665 1238 */
cabc3c12 1239 signal(SIGCHLD, SIG_DFL);
2e776665 1240
a2036d7e 1241 protocol = parse_connect_url(url, &hostandport, &path);
3f55ccab 1242 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
5610b7c0
TB
1243 printf("Diag: url=%s\n", url ? url : "NULL");
1244 printf("Diag: protocol=%s\n", prot_name(protocol));
a2036d7e 1245 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
5610b7c0 1246 printf("Diag: path=%s\n", path ? path : "NULL");
a2036d7e
TB
1247 conn = NULL;
1248 } else if (protocol == PROTO_GIT) {
40fc51e3 1249 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
a2036d7e 1250 } else {
f1399291 1251 struct strbuf cmd = STRBUF_INIT;
0c2f0d27 1252 const char *const *var;
f1399291 1253
483bbd4e
RS
1254 conn = xmalloc(sizeof(*conn));
1255 child_process_init(conn);
a2036d7e 1256
aeeb2d49
JK
1257 if (looks_like_command_line_option(path))
1258 die("strange pathname '%s' blocked", path);
1259
a2036d7e
TB
1260 strbuf_addstr(&cmd, prog);
1261 strbuf_addch(&cmd, ' ');
1262 sq_quote_buf(&cmd, path);
1263
aab40438 1264 /* remove repo-local variables from the environment */
0c2f0d27
BW
1265 for (var = local_repo_env; *var; var++)
1266 argv_array_push(&conn->env_array, *var);
1267
a48b409f 1268 conn->use_shell = 1;
a2036d7e 1269 conn->in = conn->out = -1;
a2036d7e 1270 if (protocol == PROTO_SSH) {
a2036d7e
TB
1271 char *ssh_host = hostandport;
1272 const char *port = NULL;
a5adaced 1273 transport_check_allowed("ssh");
a2036d7e 1274 get_host_and_port(&ssh_host, &port);
a2036d7e 1275
86ceb337
TB
1276 if (!port)
1277 port = get_port(ssh_host);
42da4840 1278
3f55ccab
TB
1279 if (flags & CONNECT_DIAG_URL) {
1280 printf("Diag: url=%s\n", url ? url : "NULL");
1281 printf("Diag: protocol=%s\n", prot_name(protocol));
1282 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1283 printf("Diag: port=%s\n", port ? port : "NONE");
1284 printf("Diag: path=%s\n", path ? path : "NULL");
a2036d7e 1285
3f55ccab
TB
1286 free(hostandport);
1287 free(path);
04f20c04 1288 free(conn);
f1399291 1289 strbuf_release(&cmd);
3f55ccab 1290 return NULL;
37ee646e 1291 }
40fc51e3 1292 fill_ssh_args(conn, ssh_host, port, version, flags);
c049b61d 1293 } else {
a5adaced 1294 transport_check_allowed("file");
40fc51e3 1295 if (version > 0) {
0c2f0d27 1296 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
40fc51e3 1297 version);
0c2f0d27 1298 }
4852f723 1299 }
1823bea1 1300 argv_array_push(&conn->args, cmd.buf);
f364cb88 1301
a2036d7e
TB
1302 if (start_command(conn))
1303 die("unable to fork");
f364cb88 1304
a2036d7e
TB
1305 fd[0] = conn->out; /* read from child's stdout */
1306 fd[1] = conn->in; /* write to child's stdin */
1307 strbuf_release(&cmd);
1308 }
1309 free(hostandport);
cabc3c12 1310 free(path);
98158e9c 1311 return conn;
f7192598
LT
1312}
1313
98158e9c 1314int finish_connect(struct child_process *conn)
f7192598 1315{
f364cb88 1316 int code;
7ffe853b 1317 if (!conn || git_connection_is_socket(conn))
f42a5c4e
FBH
1318 return 0;
1319
f364cb88 1320 code = finish_command(conn);
98158e9c 1321 free(conn);
f364cb88 1322 return code;
f7192598 1323}