]> git.ipfire.org Git - thirdparty/git.git/blame - connect.c
connect: refactor git_connect to only get the protocol version once
[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
55e4f936 51static 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,
411 const struct argv_array *ref_prefixes)
412{
413 int i;
414 *list = NULL;
415
416 if (server_supports_v2("ls-refs", 1))
417 packet_write_fmt(fd_out, "command=ls-refs\n");
418
419 if (server_supports_v2("agent", 0))
420 packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
421
422 packet_delim(fd_out);
423 /* When pushing we don't want to request the peeled tags */
424 if (!for_push)
425 packet_write_fmt(fd_out, "peel\n");
426 packet_write_fmt(fd_out, "symrefs\n");
427 for (i = 0; ref_prefixes && i < ref_prefixes->argc; i++) {
428 packet_write_fmt(fd_out, "ref-prefix %s\n",
429 ref_prefixes->argv[i]);
430 }
431 packet_flush(fd_out);
432
433 /* Process response from server */
434 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
435 if (!process_ref_v2(reader->line, &list))
436 die("invalid ls-refs response: %s", reader->line);
437 }
438
439 if (reader->status != PACKET_READ_FLUSH)
440 die("expected flush after ref listing");
441
442 return list;
443}
444
5d54cffc 445static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
f47182c8
JH
446{
447 int len;
448
449 if (!feature_list)
450 return NULL;
451
452 len = strlen(feature);
453 while (*feature_list) {
454 const char *found = strstr(feature_list, feature);
455 if (!found)
456 return NULL;
94427108
JK
457 if (feature_list == found || isspace(found[-1])) {
458 const char *value = found + len;
459 /* feature with no value (e.g., "thin-pack") */
460 if (!*value || isspace(*value)) {
461 if (lenp)
462 *lenp = 0;
463 return value;
464 }
465 /* feature with a value (e.g., "agent=git/1.2.3") */
466 else if (*value == '=') {
467 value++;
468 if (lenp)
469 *lenp = strcspn(value, " \t\n");
470 return value;
471 }
472 /*
473 * otherwise we matched a substring of another feature;
474 * keep looking
475 */
476 }
f47182c8
JH
477 feature_list = found + 1;
478 }
479 return NULL;
211b5f9e
JS
480}
481
94427108
JK
482int parse_feature_request(const char *feature_list, const char *feature)
483{
484 return !!parse_feature_value(feature_list, feature, NULL);
485}
486
487const char *server_feature_value(const char *feature, int *len)
488{
e52449b6 489 return parse_feature_value(server_capabilities_v1, feature, len);
94427108
JK
490}
491
492int server_supports(const char *feature)
493{
494 return !!server_feature_value(feature, NULL);
495}
496
2386d658
LT
497enum protocol {
498 PROTO_LOCAL = 1,
c59ab2e5 499 PROTO_FILE,
2386d658 500 PROTO_SSH,
4b05548f 501 PROTO_GIT
2386d658
LT
502};
503
c59ab2e5
TB
504int url_is_local_not_ssh(const char *url)
505{
506 const char *colon = strchr(url, ':');
507 const char *slash = strchr(url, '/');
508 return !colon || (slash && slash < colon) ||
509 has_dos_drive_prefix(url);
510}
511
5610b7c0
TB
512static const char *prot_name(enum protocol protocol)
513{
514 switch (protocol) {
515 case PROTO_LOCAL:
c59ab2e5 516 case PROTO_FILE:
5610b7c0
TB
517 return "file";
518 case PROTO_SSH:
519 return "ssh";
520 case PROTO_GIT:
521 return "git";
522 default:
83e6bda3 523 return "unknown protocol";
5610b7c0
TB
524 }
525}
526
2386d658
LT
527static enum protocol get_protocol(const char *name)
528{
529 if (!strcmp(name, "ssh"))
530 return PROTO_SSH;
531 if (!strcmp(name, "git"))
532 return PROTO_GIT;
07c7782c 533 if (!strcmp(name, "git+ssh")) /* deprecated - do not use */
c05186cc 534 return PROTO_SSH;
07c7782c 535 if (!strcmp(name, "ssh+git")) /* deprecated - do not use */
c05186cc 536 return PROTO_SSH;
72a4f4b6 537 if (!strcmp(name, "file"))
c59ab2e5 538 return PROTO_FILE;
2386d658
LT
539 die("I don't handle protocol '%s'", name);
540}
541
86ceb337
TB
542static char *host_end(char **hoststart, int removebrackets)
543{
544 char *host = *hoststart;
545 char *end;
546 char *start = strstr(host, "@[");
547 if (start)
548 start++; /* Jump over '@' */
549 else
550 start = host;
551 if (start[0] == '[') {
552 end = strchr(start + 1, ']');
553 if (end) {
554 if (removebrackets) {
555 *end = 0;
556 memmove(start, start + 1, end - start);
557 end++;
558 }
559 } else
560 end = host;
561 } else
562 end = host;
563 return end;
564}
565
5ba88448
YH
566#define STR_(s) # s
567#define STR(s) STR_(s)
2386d658 568
72a534da
ML
569static void get_host_and_port(char **host, const char **port)
570{
571 char *colon, *end;
86ceb337 572 end = host_end(host, 1);
72a534da 573 colon = strchr(end, ':');
72a534da 574 if (colon) {
86ceb337
TB
575 long portnr = strtol(colon + 1, &end, 10);
576 if (end != colon + 1 && *end == '\0' && 0 <= portnr && portnr < 65536) {
577 *colon = 0;
578 *port = colon + 1;
6b6c5f7a
TB
579 } else if (!colon[1]) {
580 *colon = 0;
86ceb337 581 }
72a534da
ML
582 }
583}
584
e47a8583
EW
585static void enable_keepalive(int sockfd)
586{
587 int ka = 1;
588
589 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
590 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
591 strerror(errno));
592}
593
49744d63 594#ifndef NO_IPV6
4c505f71 595
ba505322
AR
596static const char *ai_name(const struct addrinfo *ai)
597{
785a9857
BK
598 static char addr[NI_MAXHOST];
599 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
600 NI_NUMERICHOST) != 0)
5096d490 601 xsnprintf(addr, sizeof(addr), "(unknown)");
785a9857 602
ba505322
AR
603 return addr;
604}
605
5ad312be
JL
606/*
607 * Returns a connected socket() fd, or else die()s.
608 */
7841ce79 609static int git_tcp_connect_sock(char *host, int flags)
2386d658 610{
63a995b6
DZ
611 struct strbuf error_message = STRBUF_INIT;
612 int sockfd = -1;
554fe20d 613 const char *port = STR(DEFAULT_GIT_PORT);
5ba88448
YH
614 struct addrinfo hints, *ai0, *ai;
615 int gai;
ba505322 616 int cnt = 0;
5ba88448 617
72a534da
ML
618 get_host_and_port(&host, &port);
619 if (!*port)
620 port = "<none>";
5ba88448
YH
621
622 memset(&hints, 0, sizeof(hints));
c915f11e
EW
623 if (flags & CONNECT_IPV4)
624 hints.ai_family = AF_INET;
625 else if (flags & CONNECT_IPV6)
626 hints.ai_family = AF_INET6;
5ba88448
YH
627 hints.ai_socktype = SOCK_STREAM;
628 hints.ai_protocol = IPPROTO_TCP;
629
7841ce79
MT
630 if (flags & CONNECT_VERBOSE)
631 fprintf(stderr, "Looking up %s ... ", host);
632
5ba88448
YH
633 gai = getaddrinfo(host, port, &hints, &ai);
634 if (gai)
608d48b2 635 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
5ba88448 636
7841ce79
MT
637 if (flags & CONNECT_VERBOSE)
638 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
639
e08afecd 640 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
5ad312be
JL
641 sockfd = socket(ai->ai_family,
642 ai->ai_socktype, ai->ai_protocol);
63a995b6
DZ
643 if ((sockfd < 0) ||
644 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
645 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
646 host, cnt, ai_name(ai), strerror(errno));
647 if (0 <= sockfd)
648 close(sockfd);
5ba88448
YH
649 sockfd = -1;
650 continue;
2386d658 651 }
ba505322
AR
652 if (flags & CONNECT_VERBOSE)
653 fprintf(stderr, "%s ", ai_name(ai));
5ba88448 654 break;
2386d658
LT
655 }
656
5ba88448 657 freeaddrinfo(ai0);
2386d658 658
2386d658 659 if (sockfd < 0)
63a995b6 660 die("unable to connect to %s:\n%s", host, error_message.buf);
5ba88448 661
e47a8583
EW
662 enable_keepalive(sockfd);
663
7841ce79
MT
664 if (flags & CONNECT_VERBOSE)
665 fprintf(stderr, "done.\n");
666
63a995b6
DZ
667 strbuf_release(&error_message);
668
5ad312be 669 return sockfd;
2386d658
LT
670}
671
49744d63 672#else /* NO_IPV6 */
4c505f71 673
5ad312be
JL
674/*
675 * Returns a connected socket() fd, or else die()s.
676 */
7841ce79 677static int git_tcp_connect_sock(char *host, int flags)
4c505f71 678{
7203a2d1
EFL
679 struct strbuf error_message = STRBUF_INIT;
680 int sockfd = -1;
72a534da
ML
681 const char *port = STR(DEFAULT_GIT_PORT);
682 char *ep;
4c505f71
PA
683 struct hostent *he;
684 struct sockaddr_in sa;
685 char **ap;
686 unsigned int nport;
ba505322 687 int cnt;
4c505f71 688
72a534da 689 get_host_and_port(&host, &port);
4c505f71 690
7841ce79
MT
691 if (flags & CONNECT_VERBOSE)
692 fprintf(stderr, "Looking up %s ... ", host);
693
4c505f71
PA
694 he = gethostbyname(host);
695 if (!he)
696 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
697 nport = strtoul(port, &ep, 10);
698 if ( ep == port || *ep ) {
699 /* Not numeric */
700 struct servent *se = getservbyname(port,"tcp");
701 if ( !se )
d7530708 702 die("Unknown port %s", port);
4c505f71
PA
703 nport = se->s_port;
704 }
705
7841ce79
MT
706 if (flags & CONNECT_VERBOSE)
707 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
708
ba505322 709 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
4c505f71
PA
710 memset(&sa, 0, sizeof sa);
711 sa.sin_family = he->h_addrtype;
6573faff 712 sa.sin_port = htons(nport);
c6164218 713 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71 714
7203a2d1
EFL
715 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
716 if ((sockfd < 0) ||
717 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
718 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
ba505322
AR
719 host,
720 cnt,
721 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
7203a2d1
EFL
722 strerror(errno));
723 if (0 <= sockfd)
724 close(sockfd);
4c505f71
PA
725 sockfd = -1;
726 continue;
727 }
ba505322
AR
728 if (flags & CONNECT_VERBOSE)
729 fprintf(stderr, "%s ",
730 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
4c505f71
PA
731 break;
732 }
733
734 if (sockfd < 0)
7203a2d1 735 die("unable to connect to %s:\n%s", host, error_message.buf);
4c505f71 736
e47a8583
EW
737 enable_keepalive(sockfd);
738
7841ce79
MT
739 if (flags & CONNECT_VERBOSE)
740 fprintf(stderr, "done.\n");
741
5ad312be
JL
742 return sockfd;
743}
744
745#endif /* NO_IPV6 */
746
747
8e349780
JN
748/*
749 * Dummy child_process returned by git_connect() if the transport protocol
750 * does not need fork(2).
751 */
752static struct child_process no_fork = CHILD_PROCESS_INIT;
753
754int git_connection_is_socket(struct child_process *conn)
755{
756 return conn == &no_fork;
757}
758
759static struct child_process *git_tcp_connect(int fd[2], char *host, int flags)
5ad312be 760{
7841ce79 761 int sockfd = git_tcp_connect_sock(host, flags);
5ad312be 762
4c505f71 763 fd[0] = sockfd;
ec587fde 764 fd[1] = dup(sockfd);
8e349780
JN
765
766 return &no_fork;
4c505f71
PA
767}
768
4c505f71 769
96f1e58f 770static char *git_proxy_command;
f8014776 771
ef90d6d4
JS
772static int git_proxy_command_options(const char *var, const char *value,
773 void *cb)
f8014776 774{
e814bc4d 775 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
776 const char *for_pos;
777 int matchlen = -1;
778 int hostlen;
15112c95
EFL
779 const char *rhost_name = cb;
780 int rhost_len = strlen(rhost_name);
c3df8568 781
e814bc4d 782 if (git_proxy_command)
f8014776 783 return 0;
c64b9ad0
JH
784 if (!value)
785 return config_error_nonbool(var);
e814bc4d
JH
786 /* [core]
787 * ;# matches www.kernel.org as well
788 * gitproxy = netcatter-1 for kernel.org
789 * gitproxy = netcatter-2 for sample.xz
790 * gitproxy = netcatter-default
791 */
c3df8568 792 for_pos = strstr(value, " for ");
e814bc4d
JH
793 if (!for_pos)
794 /* matches everybody */
795 matchlen = strlen(value);
796 else {
797 hostlen = strlen(for_pos + 5);
798 if (rhost_len < hostlen)
799 matchlen = -1;
800 else if (!strncmp(for_pos + 5,
801 rhost_name + rhost_len - hostlen,
802 hostlen) &&
803 ((rhost_len == hostlen) ||
804 rhost_name[rhost_len - hostlen -1] == '.'))
805 matchlen = for_pos - value;
806 else
807 matchlen = -1;
808 }
809 if (0 <= matchlen) {
810 /* core.gitproxy = none for kernel.org */
a6080a0a 811 if (matchlen == 4 &&
e814bc4d
JH
812 !memcmp(value, "none", 4))
813 matchlen = 0;
182af834 814 git_proxy_command = xmemdupz(value, matchlen);
f8014776 815 }
e814bc4d 816 return 0;
f8014776
PC
817 }
818
ef90d6d4 819 return git_default_config(var, value, cb);
f8014776
PC
820}
821
e814bc4d 822static int git_use_proxy(const char *host)
f8014776
PC
823{
824 git_proxy_command = getenv("GIT_PROXY_COMMAND");
15112c95 825 git_config(git_proxy_command_options, (void*)host);
e814bc4d 826 return (git_proxy_command && *git_proxy_command);
f8014776
PC
827}
828
5cbf8246 829static struct child_process *git_proxy_connect(int fd[2], char *host)
f8014776 830{
554fe20d 831 const char *port = STR(DEFAULT_GIT_PORT);
5cbf8246 832 struct child_process *proxy;
f8014776 833
72a534da 834 get_host_and_port(&host, &port);
f8014776 835
3be4cf09
JK
836 if (looks_like_command_line_option(host))
837 die("strange hostname '%s' blocked", host);
838 if (looks_like_command_line_option(port))
839 die("strange port '%s' blocked", port);
840
483bbd4e
RS
841 proxy = xmalloc(sizeof(*proxy));
842 child_process_init(proxy);
1823bea1
JK
843 argv_array_push(&proxy->args, git_proxy_command);
844 argv_array_push(&proxy->args, host);
845 argv_array_push(&proxy->args, port);
5cbf8246
JK
846 proxy->in = -1;
847 proxy->out = -1;
848 if (start_command(proxy))
1823bea1 849 die("cannot start proxy %s", git_proxy_command);
5cbf8246
JK
850 fd[0] = proxy->out; /* read from proxy stdout */
851 fd[1] = proxy->in; /* write to proxy stdin */
852 return proxy;
f8014776
PC
853}
854
86ceb337 855static char *get_port(char *host)
2e776665
LT
856{
857 char *end;
86ceb337
TB
858 char *p = strchr(host, ':');
859
2e776665 860 if (p) {
8f148253
RS
861 long port = strtol(p + 1, &end, 10);
862 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
86ceb337
TB
863 *p = '\0';
864 return p+1;
2e776665
LT
865 }
866 }
867
868 return NULL;
869}
870
f7192598 871/*
cabc3c12
JS
872 * Extract protocol and relevant parts from the specified connection URL.
873 * The caller must free() the returned strings.
f7192598 874 */
cabc3c12 875static enum protocol parse_connect_url(const char *url_orig, char **ret_host,
83b05875 876 char **ret_path)
f7192598 877{
9d2e9420 878 char *url;
8e76bf3f 879 char *host, *path;
356bece0 880 char *end;
c59ab2e5 881 int separator = '/';
faea9ccb 882 enum protocol protocol = PROTO_LOCAL;
f0b7367c 883
9d2e9420
JK
884 if (is_url(url_orig))
885 url = url_decode(url_orig);
886 else
887 url = xstrdup(url_orig);
888
faea9ccb 889 host = strstr(url, "://");
eeefa7c9 890 if (host) {
faea9ccb
AE
891 *host = '\0';
892 protocol = get_protocol(url);
893 host += 3;
356bece0 894 } else {
f7192598 895 host = url;
c59ab2e5
TB
896 if (!url_is_local_not_ssh(url)) {
897 protocol = PROTO_SSH;
898 separator = ':';
899 }
356bece0
YH
900 }
901
9aa5053d 902 /*
83b05875
TB
903 * Don't do destructive transforms as protocol code does
904 * '[]' unwrapping in get_host_and_port()
9aa5053d 905 */
86ceb337 906 end = host_end(&host, 0);
356bece0 907
c59ab2e5 908 if (protocol == PROTO_LOCAL)
72a4f4b6 909 path = end;
c59ab2e5
TB
910 else if (protocol == PROTO_FILE && has_dos_drive_prefix(end))
911 path = end; /* "file://$(pwd)" may be "file://C:/projects/repo" */
912 else
913 path = strchr(end, separator);
2386d658 914
faea9ccb
AE
915 if (!path || !*path)
916 die("No path specified. See 'man git-pull' for valid url syntax");
917
918 /*
919 * null-terminate hostname and point path to ~ for URL's like this:
920 * ssh://host.xz/~user/repo
921 */
c59ab2e5
TB
922
923 end = path; /* Need to \0 terminate host here */
924 if (separator == ':')
925 path++; /* path starts after ':' */
926 if (protocol == PROTO_GIT || protocol == PROTO_SSH) {
faea9ccb
AE
927 if (path[1] == '~')
928 path++;
faea9ccb
AE
929 }
930
c59ab2e5
TB
931 path = xstrdup(path);
932 *end = '\0';
933
cabc3c12 934 *ret_host = xstrdup(host);
c59ab2e5 935 *ret_path = path;
cabc3c12
JS
936 free(url);
937 return protocol;
938}
939
3c8ede3f
NTND
940static const char *get_ssh_command(void)
941{
942 const char *ssh;
943
944 if ((ssh = getenv("GIT_SSH_COMMAND")))
945 return ssh;
946
947 if (!git_config_get_string_const("core.sshcommand", &ssh))
948 return ssh;
949
950 return NULL;
951}
952
94b8ae5a 953enum ssh_variant {
0da0e49b 954 VARIANT_AUTO,
94b8ae5a
BW
955 VARIANT_SIMPLE,
956 VARIANT_SSH,
957 VARIANT_PLINK,
958 VARIANT_PUTTY,
959 VARIANT_TORTOISEPLINK,
960};
961
0da0e49b 962static void override_ssh_variant(enum ssh_variant *ssh_variant)
e2824e47 963{
94b8ae5a 964 const char *variant = getenv("GIT_SSH_VARIANT");
486c8e8c 965
94b8ae5a 966 if (!variant && git_config_get_string_const("ssh.variant", &variant))
0da0e49b 967 return;
486c8e8c 968
0da0e49b
JN
969 if (!strcmp(variant, "auto"))
970 *ssh_variant = VARIANT_AUTO;
971 else if (!strcmp(variant, "plink"))
94b8ae5a
BW
972 *ssh_variant = VARIANT_PLINK;
973 else if (!strcmp(variant, "putty"))
974 *ssh_variant = VARIANT_PUTTY;
975 else if (!strcmp(variant, "tortoiseplink"))
976 *ssh_variant = VARIANT_TORTOISEPLINK;
977 else if (!strcmp(variant, "simple"))
978 *ssh_variant = VARIANT_SIMPLE;
979 else
980 *ssh_variant = VARIANT_SSH;
486c8e8c
JH
981}
982
94b8ae5a
BW
983static enum ssh_variant determine_ssh_variant(const char *ssh_command,
984 int is_cmdline)
486c8e8c 985{
0da0e49b 986 enum ssh_variant ssh_variant = VARIANT_AUTO;
486c8e8c 987 const char *variant;
e2824e47
JS
988 char *p = NULL;
989
0da0e49b
JN
990 override_ssh_variant(&ssh_variant);
991
992 if (ssh_variant != VARIANT_AUTO)
94b8ae5a 993 return ssh_variant;
486c8e8c
JH
994
995 if (!is_cmdline) {
e2824e47
JS
996 p = xstrdup(ssh_command);
997 variant = basename(p);
998 } else {
999 const char **ssh_argv;
1000
1001 p = xstrdup(ssh_command);
22e5ae5c 1002 if (split_cmdline(p, &ssh_argv) > 0) {
e2824e47
JS
1003 variant = basename((char *)ssh_argv[0]);
1004 /*
1005 * At this point, variant points into the buffer
1006 * referenced by p, hence we do not need ssh_argv
1007 * any longer.
1008 */
1009 free(ssh_argv);
5d2993b6
JK
1010 } else {
1011 free(p);
94b8ae5a 1012 return ssh_variant;
5d2993b6 1013 }
e2824e47
JS
1014 }
1015
94b8ae5a
BW
1016 if (!strcasecmp(variant, "ssh") ||
1017 !strcasecmp(variant, "ssh.exe"))
1018 ssh_variant = VARIANT_SSH;
1019 else if (!strcasecmp(variant, "plink") ||
1020 !strcasecmp(variant, "plink.exe"))
1021 ssh_variant = VARIANT_PLINK;
e2824e47 1022 else if (!strcasecmp(variant, "tortoiseplink") ||
94b8ae5a
BW
1023 !strcasecmp(variant, "tortoiseplink.exe"))
1024 ssh_variant = VARIANT_TORTOISEPLINK;
1025
e2824e47 1026 free(p);
94b8ae5a 1027 return ssh_variant;
e2824e47
JS
1028}
1029
2ac67cb6
JN
1030/*
1031 * Open a connection using Git's native protocol.
1032 *
1033 * The caller is responsible for freeing hostandport, but this function may
1034 * modify it (for example, to truncate it to remove the port part).
1035 */
1036static struct child_process *git_connect_git(int fd[2], char *hostandport,
1037 const char *path, const char *prog,
40fc51e3 1038 enum protocol_version version,
2ac67cb6
JN
1039 int flags)
1040{
1041 struct child_process *conn;
1042 struct strbuf request = STRBUF_INIT;
1043 /*
1044 * Set up virtual host information based on where we will
1045 * connect, unless the user has overridden us in
1046 * the environment.
1047 */
1048 char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
1049 if (target_host)
1050 target_host = xstrdup(target_host);
1051 else
1052 target_host = xstrdup(hostandport);
1053
1054 transport_check_allowed("git");
1055
233cd282
JN
1056 /*
1057 * These underlying connection commands die() if they
2ac67cb6
JN
1058 * cannot connect.
1059 */
1060 if (git_use_proxy(hostandport))
1061 conn = git_proxy_connect(fd, hostandport);
1062 else
1063 conn = git_tcp_connect(fd, hostandport, flags);
1064 /*
1065 * Separate original protocol components prog and path
1066 * from extended host header with a NUL byte.
1067 *
1068 * Note: Do not add any other headers here! Doing so
1069 * will cause older git-daemon servers to crash.
1070 */
1071 strbuf_addf(&request,
1072 "%s %s%chost=%s%c",
1073 prog, path, 0,
1074 target_host, 0);
1075
1076 /* If using a new version put that stuff here after a second null byte */
40fc51e3 1077 if (version > 0) {
2ac67cb6
JN
1078 strbuf_addch(&request, '\0');
1079 strbuf_addf(&request, "version=%d%c",
40fc51e3 1080 version, '\0');
2ac67cb6
JN
1081 }
1082
1083 packet_write(fd[1], request.buf, request.len);
1084
1085 free(target_host);
1086 strbuf_release(&request);
1087 return conn;
1088}
1089
957e2ad2
JN
1090/*
1091 * Append the appropriate environment variables to `env` and options to
1092 * `args` for running ssh in Git's SSH-tunneled transport.
1093 */
1094static void push_ssh_options(struct argv_array *args, struct argv_array *env,
1095 enum ssh_variant variant, const char *port,
40fc51e3 1096 enum protocol_version version, int flags)
957e2ad2
JN
1097{
1098 if (variant == VARIANT_SSH &&
40fc51e3 1099 version > 0) {
957e2ad2
JN
1100 argv_array_push(args, "-o");
1101 argv_array_push(args, "SendEnv=" GIT_PROTOCOL_ENVIRONMENT);
1102 argv_array_pushf(env, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
40fc51e3 1103 version);
957e2ad2
JN
1104 }
1105
a3f5b66f
JN
1106 if (flags & CONNECT_IPV4) {
1107 switch (variant) {
1108 case VARIANT_AUTO:
1109 BUG("VARIANT_AUTO passed to push_ssh_options");
1110 case VARIANT_SIMPLE:
1111 die("ssh variant 'simple' does not support -4");
1112 case VARIANT_SSH:
1113 case VARIANT_PLINK:
1114 case VARIANT_PUTTY:
1115 case VARIANT_TORTOISEPLINK:
957e2ad2 1116 argv_array_push(args, "-4");
a3f5b66f
JN
1117 }
1118 } else if (flags & CONNECT_IPV6) {
1119 switch (variant) {
1120 case VARIANT_AUTO:
1121 BUG("VARIANT_AUTO passed to push_ssh_options");
1122 case VARIANT_SIMPLE:
1123 die("ssh variant 'simple' does not support -6");
1124 case VARIANT_SSH:
1125 case VARIANT_PLINK:
1126 case VARIANT_PUTTY:
1127 case VARIANT_TORTOISEPLINK:
957e2ad2 1128 argv_array_push(args, "-6");
a3f5b66f 1129 }
957e2ad2
JN
1130 }
1131
1132 if (variant == VARIANT_TORTOISEPLINK)
1133 argv_array_push(args, "-batch");
1134
3fa5e0d0
JN
1135 if (port) {
1136 switch (variant) {
1137 case VARIANT_AUTO:
1138 BUG("VARIANT_AUTO passed to push_ssh_options");
1139 case VARIANT_SIMPLE:
1140 die("ssh variant 'simple' does not support setting port");
1141 case VARIANT_SSH:
957e2ad2 1142 argv_array_push(args, "-p");
3fa5e0d0
JN
1143 break;
1144 case VARIANT_PLINK:
1145 case VARIANT_PUTTY:
1146 case VARIANT_TORTOISEPLINK:
957e2ad2 1147 argv_array_push(args, "-P");
3fa5e0d0 1148 }
957e2ad2
JN
1149
1150 argv_array_push(args, port);
1151 }
1152}
1153
fce54ce4
JN
1154/* Prepare a child_process for use by Git's SSH-tunneled transport. */
1155static void fill_ssh_args(struct child_process *conn, const char *ssh_host,
40fc51e3
BW
1156 const char *port, enum protocol_version version,
1157 int flags)
fce54ce4
JN
1158{
1159 const char *ssh;
1160 enum ssh_variant variant;
1161
1162 if (looks_like_command_line_option(ssh_host))
1163 die("strange hostname '%s' blocked", ssh_host);
1164
1165 ssh = get_ssh_command();
1166 if (ssh) {
1167 variant = determine_ssh_variant(ssh, 1);
1168 } else {
1169 /*
1170 * GIT_SSH is the no-shell version of
1171 * GIT_SSH_COMMAND (and must remain so for
1172 * historical compatibility).
1173 */
1174 conn->use_shell = 0;
1175
1176 ssh = getenv("GIT_SSH");
1177 if (!ssh)
1178 ssh = "ssh";
1179 variant = determine_ssh_variant(ssh, 0);
1180 }
1181
0da0e49b
JN
1182 if (variant == VARIANT_AUTO) {
1183 struct child_process detect = CHILD_PROCESS_INIT;
1184
1185 detect.use_shell = conn->use_shell;
1186 detect.no_stdin = detect.no_stdout = detect.no_stderr = 1;
1187
1188 argv_array_push(&detect.args, ssh);
1189 argv_array_push(&detect.args, "-G");
1190 push_ssh_options(&detect.args, &detect.env_array,
40fc51e3 1191 VARIANT_SSH, port, version, flags);
0da0e49b
JN
1192 argv_array_push(&detect.args, ssh_host);
1193
1194 variant = run_command(&detect) ? VARIANT_SIMPLE : VARIANT_SSH;
1195 }
1196
fce54ce4 1197 argv_array_push(&conn->args, ssh);
40fc51e3 1198 push_ssh_options(&conn->args, &conn->env_array, variant, port, version, flags);
fce54ce4
JN
1199 argv_array_push(&conn->args, ssh_host);
1200}
1201
cabc3c12 1202/*
8e349780
JN
1203 * This returns the dummy child_process `no_fork` if the transport protocol
1204 * does not need fork(2), or a struct child_process object if it does. Once
1205 * done, finish the connection with finish_connect() with the value returned
1206 * from this function (it is safe to call finish_connect() with NULL to
1207 * support the former case).
cabc3c12
JS
1208 *
1209 * If it returns, the connect is successful; it just dies on errors (this
1210 * will hopefully be changed in a libification effort, to return NULL when
1211 * the connection failed).
1212 */
1213struct child_process *git_connect(int fd[2], const char *url,
1214 const char *prog, int flags)
1215{
a2036d7e 1216 char *hostandport, *path;
8e349780 1217 struct child_process *conn;
cabc3c12 1218 enum protocol protocol;
40fc51e3 1219 enum protocol_version version = get_protocol_version_config();
cabc3c12
JS
1220
1221 /* Without this we cannot rely on waitpid() to tell
1222 * what happened to our children.
2e776665 1223 */
cabc3c12 1224 signal(SIGCHLD, SIG_DFL);
2e776665 1225
a2036d7e 1226 protocol = parse_connect_url(url, &hostandport, &path);
3f55ccab 1227 if ((flags & CONNECT_DIAG_URL) && (protocol != PROTO_SSH)) {
5610b7c0
TB
1228 printf("Diag: url=%s\n", url ? url : "NULL");
1229 printf("Diag: protocol=%s\n", prot_name(protocol));
a2036d7e 1230 printf("Diag: hostandport=%s\n", hostandport ? hostandport : "NULL");
5610b7c0 1231 printf("Diag: path=%s\n", path ? path : "NULL");
a2036d7e
TB
1232 conn = NULL;
1233 } else if (protocol == PROTO_GIT) {
40fc51e3 1234 conn = git_connect_git(fd, hostandport, path, prog, version, flags);
a2036d7e 1235 } else {
f1399291 1236 struct strbuf cmd = STRBUF_INIT;
0c2f0d27 1237 const char *const *var;
f1399291 1238
483bbd4e
RS
1239 conn = xmalloc(sizeof(*conn));
1240 child_process_init(conn);
a2036d7e 1241
aeeb2d49
JK
1242 if (looks_like_command_line_option(path))
1243 die("strange pathname '%s' blocked", path);
1244
a2036d7e
TB
1245 strbuf_addstr(&cmd, prog);
1246 strbuf_addch(&cmd, ' ');
1247 sq_quote_buf(&cmd, path);
1248
aab40438 1249 /* remove repo-local variables from the environment */
0c2f0d27
BW
1250 for (var = local_repo_env; *var; var++)
1251 argv_array_push(&conn->env_array, *var);
1252
a48b409f 1253 conn->use_shell = 1;
a2036d7e 1254 conn->in = conn->out = -1;
a2036d7e 1255 if (protocol == PROTO_SSH) {
a2036d7e
TB
1256 char *ssh_host = hostandport;
1257 const char *port = NULL;
a5adaced 1258 transport_check_allowed("ssh");
a2036d7e 1259 get_host_and_port(&ssh_host, &port);
a2036d7e 1260
86ceb337
TB
1261 if (!port)
1262 port = get_port(ssh_host);
42da4840 1263
3f55ccab
TB
1264 if (flags & CONNECT_DIAG_URL) {
1265 printf("Diag: url=%s\n", url ? url : "NULL");
1266 printf("Diag: protocol=%s\n", prot_name(protocol));
1267 printf("Diag: userandhost=%s\n", ssh_host ? ssh_host : "NULL");
1268 printf("Diag: port=%s\n", port ? port : "NONE");
1269 printf("Diag: path=%s\n", path ? path : "NULL");
a2036d7e 1270
3f55ccab
TB
1271 free(hostandport);
1272 free(path);
04f20c04 1273 free(conn);
f1399291 1274 strbuf_release(&cmd);
3f55ccab 1275 return NULL;
37ee646e 1276 }
40fc51e3 1277 fill_ssh_args(conn, ssh_host, port, version, flags);
c049b61d 1278 } else {
a5adaced 1279 transport_check_allowed("file");
40fc51e3 1280 if (version > 0) {
0c2f0d27 1281 argv_array_pushf(&conn->env_array, GIT_PROTOCOL_ENVIRONMENT "=version=%d",
40fc51e3 1282 version);
0c2f0d27 1283 }
4852f723 1284 }
1823bea1 1285 argv_array_push(&conn->args, cmd.buf);
f364cb88 1286
a2036d7e
TB
1287 if (start_command(conn))
1288 die("unable to fork");
f364cb88 1289
a2036d7e
TB
1290 fd[0] = conn->out; /* read from child's stdout */
1291 fd[1] = conn->in; /* write to child's stdin */
1292 strbuf_release(&cmd);
1293 }
1294 free(hostandport);
cabc3c12 1295 free(path);
98158e9c 1296 return conn;
f7192598
LT
1297}
1298
98158e9c 1299int finish_connect(struct child_process *conn)
f7192598 1300{
f364cb88 1301 int code;
7ffe853b 1302 if (!conn || git_connection_is_socket(conn))
f42a5c4e
FBH
1303 return 0;
1304
f364cb88 1305 code = finish_command(conn);
98158e9c 1306 free(conn);
f364cb88 1307 return code;
f7192598 1308}