make the sender advertise shallow commits to the receiver
[git/git.git] / connect.c
CommitLineData
731043fd 1#include "git-compat-util.h"
f7192598 2#include "cache.h"
41cb7488 3#include "pkt-line.h"
b10d0ec7 4#include "quote.h"
6abf5c0c 5#include "refs.h"
15a1c012 6#include "run-command.h"
6b62816c 7#include "remote.h"
47a59185 8#include "connect.h"
9d2e9420 9#include "url.h"
a45b5f05 10#include "string-list.h"
13eb4626 11#include "sha1-array.h"
f7192598 12
96f1e58f 13static char *server_capabilities;
5d54cffc 14static const char *parse_feature_value(const char *, const char *, int *);
211b5f9e 15
2718ff09
LT
16static int check_ref(const char *name, int len, unsigned int flags)
17{
18 if (!flags)
19 return 1;
20
c41e20b3 21 if (len < 5 || memcmp(name, "refs/", 5))
2718ff09
LT
22 return 0;
23
24 /* Skip the "refs/" part */
25 name += 5;
26 len -= 5;
27
28 /* REF_NORMAL means that we don't want the magic fake tag refs */
8d9c5010 29 if ((flags & REF_NORMAL) && check_refname_format(name, 0))
2718ff09
LT
30 return 0;
31
32 /* REF_HEADS means that we want regular branch heads */
33 if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
34 return 1;
35
36 /* REF_TAGS means that we want tags */
37 if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
38 return 1;
39
40 /* All type bits clear means that we are ok with anything */
41 return !(flags & ~REF_NORMAL);
42}
43
4577370e
DB
44int check_ref_type(const struct ref *ref, int flags)
45{
46 return check_ref(ref->name, strlen(ref->name), flags);
47}
48
46284dd1
HV
49static void die_initial_contact(int got_at_least_one_head)
50{
51 if (got_at_least_one_head)
52 die("The remote end hung up upon initial contact");
53 else
54 die("Could not read from remote repository.\n\n"
55 "Please make sure you have the correct access rights\n"
56 "and the repository exists.");
57}
58
a45b5f05
JH
59static void parse_one_symref_info(struct string_list *symref, const char *val, int len)
60{
61 char *sym, *target;
62 struct string_list_item *item;
63
64 if (!len)
65 return; /* just "symref" */
66 /* e.g. "symref=HEAD:refs/heads/master" */
67 sym = xmalloc(len + 1);
68 memcpy(sym, val, len);
69 sym[len] = '\0';
70 target = strchr(sym, ':');
71 if (!target)
72 /* just "symref=something" */
73 goto reject;
74 *(target++) = '\0';
75 if (check_refname_format(sym, REFNAME_ALLOW_ONELEVEL) ||
76 check_refname_format(target, REFNAME_ALLOW_ONELEVEL))
77 /* "symref=bogus:pair */
78 goto reject;
79 item = string_list_append(symref, sym);
80 item->util = target;
81 return;
82reject:
83 free(sym);
84 return;
85}
86
87static void annotate_refs_with_symref_info(struct ref *ref)
88{
89 struct string_list symref = STRING_LIST_INIT_DUP;
90 const char *feature_list = server_capabilities;
91
92 while (feature_list) {
93 int len;
94 const char *val;
95
96 val = parse_feature_value(feature_list, "symref", &len);
97 if (!val)
98 break;
99 parse_one_symref_info(&symref, val, len);
100 feature_list = val + 1;
101 }
102 sort_string_list(&symref);
103
104 for (; ref; ref = ref->next) {
105 struct string_list_item *item;
106 item = string_list_lookup(&symref, ref->name);
107 if (!item)
108 continue;
109 ref->symref = xstrdup((char *)item->util);
110 }
111 string_list_clear(&symref, 0);
112}
113
d1c133f5
LT
114/*
115 * Read all the refs from the other end
116 */
85edf4f5
JK
117struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
118 struct ref **list, unsigned int flags,
13eb4626 119 struct sha1_array *extra_have)
d1c133f5 120{
a45b5f05 121 struct ref **orig_list = list;
46284dd1
HV
122 int got_at_least_one_head = 0;
123
d1c133f5
LT
124 *list = NULL;
125 for (;;) {
126 struct ref *ref;
127 unsigned char old_sha1[20];
d1c133f5 128 char *name;
211b5f9e 129 int len, name_len;
74543a04 130 char *buffer = packet_buffer;
d1c133f5 131
85edf4f5 132 len = packet_read(in, &src_buf, &src_len,
4981fe75 133 packet_buffer, sizeof(packet_buffer),
819b929d
JK
134 PACKET_READ_GENTLE_ON_EOF |
135 PACKET_READ_CHOMP_NEWLINE);
46284dd1
HV
136 if (len < 0)
137 die_initial_contact(got_at_least_one_head);
138
d1c133f5
LT
139 if (!len)
140 break;
d1c133f5 141
a8073289
TPW
142 if (len > 4 && !prefixcmp(buffer, "ERR "))
143 die("remote error: %s", buffer + 4);
144
d1c133f5
LT
145 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
146 die("protocol error: expected sha/ref, got '%s'", buffer);
147 name = buffer + 41;
1a7141ff 148
211b5f9e
JS
149 name_len = strlen(name);
150 if (len != name_len + 41) {
8e0f7003 151 free(server_capabilities);
9befac47 152 server_capabilities = xstrdup(name + name_len + 1);
211b5f9e
JS
153 }
154
40c155ff
JH
155 if (extra_have &&
156 name_len == 5 && !memcmp(".have", name, 5)) {
13eb4626 157 sha1_array_append(extra_have, old_sha1);
40c155ff
JH
158 continue;
159 }
160
2718ff09 161 if (!check_ref(name, name_len, flags))
cfee10a7 162 continue;
59c69c0c 163 ref = alloc_ref(buffer + 41);
e702496e 164 hashcpy(ref->old_sha1, old_sha1);
d1c133f5
LT
165 *list = ref;
166 list = &ref->next;
46284dd1 167 got_at_least_one_head = 1;
d1c133f5 168 }
a45b5f05
JH
169
170 annotate_refs_with_symref_info(*orig_list);
171
d1c133f5
LT
172 return list;
173}
174
5d54cffc 175static const char *parse_feature_value(const char *feature_list, const char *feature, int *lenp)
f47182c8
JH
176{
177 int len;
178
179 if (!feature_list)
180 return NULL;
181
182 len = strlen(feature);
183 while (*feature_list) {
184 const char *found = strstr(feature_list, feature);
185 if (!found)
186 return NULL;
94427108
JK
187 if (feature_list == found || isspace(found[-1])) {
188 const char *value = found + len;
189 /* feature with no value (e.g., "thin-pack") */
190 if (!*value || isspace(*value)) {
191 if (lenp)
192 *lenp = 0;
193 return value;
194 }
195 /* feature with a value (e.g., "agent=git/1.2.3") */
196 else if (*value == '=') {
197 value++;
198 if (lenp)
199 *lenp = strcspn(value, " \t\n");
200 return value;
201 }
202 /*
203 * otherwise we matched a substring of another feature;
204 * keep looking
205 */
206 }
f47182c8
JH
207 feature_list = found + 1;
208 }
209 return NULL;
211b5f9e
JS
210}
211
94427108
JK
212int parse_feature_request(const char *feature_list, const char *feature)
213{
214 return !!parse_feature_value(feature_list, feature, NULL);
215}
216
217const char *server_feature_value(const char *feature, int *len)
218{
219 return parse_feature_value(server_capabilities, feature, len);
220}
221
222int server_supports(const char *feature)
223{
224 return !!server_feature_value(feature, NULL);
225}
226
2386d658
LT
227enum protocol {
228 PROTO_LOCAL = 1,
229 PROTO_SSH,
4b05548f 230 PROTO_GIT
2386d658
LT
231};
232
233static enum protocol get_protocol(const char *name)
234{
235 if (!strcmp(name, "ssh"))
236 return PROTO_SSH;
237 if (!strcmp(name, "git"))
238 return PROTO_GIT;
c05186cc
LT
239 if (!strcmp(name, "git+ssh"))
240 return PROTO_SSH;
241 if (!strcmp(name, "ssh+git"))
242 return PROTO_SSH;
72a4f4b6
LT
243 if (!strcmp(name, "file"))
244 return PROTO_LOCAL;
2386d658
LT
245 die("I don't handle protocol '%s'", name);
246}
247
5ba88448
YH
248#define STR_(s) # s
249#define STR(s) STR_(s)
2386d658 250
72a534da
ML
251static void get_host_and_port(char **host, const char **port)
252{
253 char *colon, *end;
254
255 if (*host[0] == '[') {
256 end = strchr(*host + 1, ']');
257 if (end) {
258 *end = 0;
259 end++;
260 (*host)++;
261 } else
262 end = *host;
263 } else
264 end = *host;
265 colon = strchr(end, ':');
266
267 if (colon) {
268 *colon = 0;
269 *port = colon + 1;
270 }
271}
272
e47a8583
EW
273static void enable_keepalive(int sockfd)
274{
275 int ka = 1;
276
277 if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
278 fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
279 strerror(errno));
280}
281
49744d63 282#ifndef NO_IPV6
4c505f71 283
ba505322
AR
284static const char *ai_name(const struct addrinfo *ai)
285{
785a9857
BK
286 static char addr[NI_MAXHOST];
287 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr, sizeof(addr), NULL, 0,
288 NI_NUMERICHOST) != 0)
ba505322 289 strcpy(addr, "(unknown)");
785a9857 290
ba505322
AR
291 return addr;
292}
293
5ad312be
JL
294/*
295 * Returns a connected socket() fd, or else die()s.
296 */
7841ce79 297static int git_tcp_connect_sock(char *host, int flags)
2386d658 298{
63a995b6
DZ
299 struct strbuf error_message = STRBUF_INIT;
300 int sockfd = -1;
554fe20d 301 const char *port = STR(DEFAULT_GIT_PORT);
5ba88448
YH
302 struct addrinfo hints, *ai0, *ai;
303 int gai;
ba505322 304 int cnt = 0;
5ba88448 305
72a534da
ML
306 get_host_and_port(&host, &port);
307 if (!*port)
308 port = "<none>";
5ba88448
YH
309
310 memset(&hints, 0, sizeof(hints));
311 hints.ai_socktype = SOCK_STREAM;
312 hints.ai_protocol = IPPROTO_TCP;
313
7841ce79
MT
314 if (flags & CONNECT_VERBOSE)
315 fprintf(stderr, "Looking up %s ... ", host);
316
5ba88448
YH
317 gai = getaddrinfo(host, port, &hints, &ai);
318 if (gai)
608d48b2 319 die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
5ba88448 320
7841ce79
MT
321 if (flags & CONNECT_VERBOSE)
322 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
323
e08afecd 324 for (ai0 = ai; ai; ai = ai->ai_next, cnt++) {
5ad312be
JL
325 sockfd = socket(ai->ai_family,
326 ai->ai_socktype, ai->ai_protocol);
63a995b6
DZ
327 if ((sockfd < 0) ||
328 (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
329 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
330 host, cnt, ai_name(ai), strerror(errno));
331 if (0 <= sockfd)
332 close(sockfd);
5ba88448
YH
333 sockfd = -1;
334 continue;
2386d658 335 }
ba505322
AR
336 if (flags & CONNECT_VERBOSE)
337 fprintf(stderr, "%s ", ai_name(ai));
5ba88448 338 break;
2386d658
LT
339 }
340
5ba88448 341 freeaddrinfo(ai0);
2386d658 342
2386d658 343 if (sockfd < 0)
63a995b6 344 die("unable to connect to %s:\n%s", host, error_message.buf);
5ba88448 345
e47a8583
EW
346 enable_keepalive(sockfd);
347
7841ce79
MT
348 if (flags & CONNECT_VERBOSE)
349 fprintf(stderr, "done.\n");
350
63a995b6
DZ
351 strbuf_release(&error_message);
352
5ad312be 353 return sockfd;
2386d658
LT
354}
355
49744d63 356#else /* NO_IPV6 */
4c505f71 357
5ad312be
JL
358/*
359 * Returns a connected socket() fd, or else die()s.
360 */
7841ce79 361static int git_tcp_connect_sock(char *host, int flags)
4c505f71 362{
7203a2d1
EFL
363 struct strbuf error_message = STRBUF_INIT;
364 int sockfd = -1;
72a534da
ML
365 const char *port = STR(DEFAULT_GIT_PORT);
366 char *ep;
4c505f71
PA
367 struct hostent *he;
368 struct sockaddr_in sa;
369 char **ap;
370 unsigned int nport;
ba505322 371 int cnt;
4c505f71 372
72a534da 373 get_host_and_port(&host, &port);
4c505f71 374
7841ce79
MT
375 if (flags & CONNECT_VERBOSE)
376 fprintf(stderr, "Looking up %s ... ", host);
377
4c505f71
PA
378 he = gethostbyname(host);
379 if (!he)
380 die("Unable to look up %s (%s)", host, hstrerror(h_errno));
381 nport = strtoul(port, &ep, 10);
382 if ( ep == port || *ep ) {
383 /* Not numeric */
384 struct servent *se = getservbyname(port,"tcp");
385 if ( !se )
d7530708 386 die("Unknown port %s", port);
4c505f71
PA
387 nport = se->s_port;
388 }
389
7841ce79
MT
390 if (flags & CONNECT_VERBOSE)
391 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
392
ba505322 393 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
4c505f71
PA
394 memset(&sa, 0, sizeof sa);
395 sa.sin_family = he->h_addrtype;
6573faff 396 sa.sin_port = htons(nport);
c6164218 397 memcpy(&sa.sin_addr, *ap, he->h_length);
4c505f71 398
7203a2d1
EFL
399 sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
400 if ((sockfd < 0) ||
401 connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
402 strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
ba505322
AR
403 host,
404 cnt,
405 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
7203a2d1
EFL
406 strerror(errno));
407 if (0 <= sockfd)
408 close(sockfd);
4c505f71
PA
409 sockfd = -1;
410 continue;
411 }
ba505322
AR
412 if (flags & CONNECT_VERBOSE)
413 fprintf(stderr, "%s ",
414 inet_ntoa(*(struct in_addr *)&sa.sin_addr));
4c505f71
PA
415 break;
416 }
417
418 if (sockfd < 0)
7203a2d1 419 die("unable to connect to %s:\n%s", host, error_message.buf);
4c505f71 420
e47a8583
EW
421 enable_keepalive(sockfd);
422
7841ce79
MT
423 if (flags & CONNECT_VERBOSE)
424 fprintf(stderr, "done.\n");
425
5ad312be
JL
426 return sockfd;
427}
428
429#endif /* NO_IPV6 */
430
431
7841ce79 432static void git_tcp_connect(int fd[2], char *host, int flags)
5ad312be 433{
7841ce79 434 int sockfd = git_tcp_connect_sock(host, flags);
5ad312be 435
4c505f71 436 fd[0] = sockfd;
ec587fde 437 fd[1] = dup(sockfd);
4c505f71
PA
438}
439
4c505f71 440
96f1e58f 441static char *git_proxy_command;
f8014776 442
ef90d6d4
JS
443static int git_proxy_command_options(const char *var, const char *value,
444 void *cb)
f8014776 445{
e814bc4d 446 if (!strcmp(var, "core.gitproxy")) {
c3df8568
YH
447 const char *for_pos;
448 int matchlen = -1;
449 int hostlen;
15112c95
EFL
450 const char *rhost_name = cb;
451 int rhost_len = strlen(rhost_name);
c3df8568 452
e814bc4d 453 if (git_proxy_command)
f8014776 454 return 0;
c64b9ad0
JH
455 if (!value)
456 return config_error_nonbool(var);
e814bc4d
JH
457 /* [core]
458 * ;# matches www.kernel.org as well
459 * gitproxy = netcatter-1 for kernel.org
460 * gitproxy = netcatter-2 for sample.xz
461 * gitproxy = netcatter-default
462 */
c3df8568 463 for_pos = strstr(value, " for ");
e814bc4d
JH
464 if (!for_pos)
465 /* matches everybody */
466 matchlen = strlen(value);
467 else {
468 hostlen = strlen(for_pos + 5);
469 if (rhost_len < hostlen)
470 matchlen = -1;
471 else if (!strncmp(for_pos + 5,
472 rhost_name + rhost_len - hostlen,
473 hostlen) &&
474 ((rhost_len == hostlen) ||
475 rhost_name[rhost_len - hostlen -1] == '.'))
476 matchlen = for_pos - value;
477 else
478 matchlen = -1;
479 }
480 if (0 <= matchlen) {
481 /* core.gitproxy = none for kernel.org */
a6080a0a 482 if (matchlen == 4 &&
e814bc4d
JH
483 !memcmp(value, "none", 4))
484 matchlen = 0;
182af834 485 git_proxy_command = xmemdupz(value, matchlen);
f8014776 486 }
e814bc4d 487 return 0;
f8014776
PC
488 }
489
ef90d6d4 490 return git_default_config(var, value, cb);
f8014776
PC
491}
492
e814bc4d 493static int git_use_proxy(const char *host)
f8014776
PC
494{
495 git_proxy_command = getenv("GIT_PROXY_COMMAND");
15112c95 496 git_config(git_proxy_command_options, (void*)host);
e814bc4d 497 return (git_proxy_command && *git_proxy_command);
f8014776
PC
498}
499
5cbf8246 500static struct child_process *git_proxy_connect(int fd[2], char *host)
f8014776 501{
554fe20d 502 const char *port = STR(DEFAULT_GIT_PORT);
5cbf8246
JK
503 const char **argv;
504 struct child_process *proxy;
f8014776 505
72a534da 506 get_host_and_port(&host, &port);
f8014776 507
5cbf8246 508 argv = xmalloc(sizeof(*argv) * 4);
15a1c012
SP
509 argv[0] = git_proxy_command;
510 argv[1] = host;
511 argv[2] = port;
512 argv[3] = NULL;
5cbf8246
JK
513 proxy = xcalloc(1, sizeof(*proxy));
514 proxy->argv = argv;
515 proxy->in = -1;
516 proxy->out = -1;
517 if (start_command(proxy))
15a1c012 518 die("cannot start proxy %s", argv[0]);
5cbf8246
JK
519 fd[0] = proxy->out; /* read from proxy stdout */
520 fd[1] = proxy->in; /* write to proxy stdin */
521 return proxy;
f8014776
PC
522}
523
0f503d77
CC
524#define MAX_CMD_LEN 1024
525
2af202be 526static char *get_port(char *host)
2e776665
LT
527{
528 char *end;
529 char *p = strchr(host, ':');
530
531 if (p) {
8f148253
RS
532 long port = strtol(p + 1, &end, 10);
533 if (end != p + 1 && *end == '\0' && 0 <= port && port < 65536) {
2e776665
LT
534 *p = '\0';
535 return p+1;
536 }
537 }
538
539 return NULL;
540}
541
fb32c917
JS
542static struct child_process no_fork;
543
f7192598 544/*
fb32c917
JS
545 * This returns a dummy child_process if the transport protocol does not
546 * need fork(2), or a struct child_process object if it does. Once done,
547 * finish the connection with finish_connect() with the value returned from
548 * this function (it is safe to call finish_connect() with NULL to support
549 * the former case).
f42a5c4e 550 *
fb32c917
JS
551 * If it returns, the connect is successful; it just dies on errors (this
552 * will hopefully be changed in a libification effort, to return NULL when
553 * the connection failed).
f7192598 554 */
4577370e 555struct child_process *git_connect(int fd[2], const char *url_orig,
98158e9c 556 const char *prog, int flags)
f7192598 557{
9d2e9420 558 char *url;
8e76bf3f 559 char *host, *path;
356bece0
YH
560 char *end;
561 int c;
5cbf8246 562 struct child_process *conn = &no_fork;
faea9ccb 563 enum protocol protocol = PROTO_LOCAL;
da2a95b2 564 int free_path = 0;
2e776665 565 char *port = NULL;
f364cb88
JS
566 const char **arg;
567 struct strbuf cmd;
faea9ccb 568
f0b7367c
JH
569 /* Without this we cannot rely on waitpid() to tell
570 * what happened to our children.
571 */
572 signal(SIGCHLD, SIG_DFL);
573
9d2e9420
JK
574 if (is_url(url_orig))
575 url = url_decode(url_orig);
576 else
577 url = xstrdup(url_orig);
578
faea9ccb 579 host = strstr(url, "://");
eeefa7c9 580 if (host) {
faea9ccb
AE
581 *host = '\0';
582 protocol = get_protocol(url);
583 host += 3;
356bece0
YH
584 c = '/';
585 } else {
f7192598 586 host = url;
356bece0
YH
587 c = ':';
588 }
589
9aa5053d
IL
590 /*
591 * Don't do destructive transforms with git:// as that
9517e6b8 592 * protocol code does '[]' unwrapping of its own.
9aa5053d 593 */
356bece0
YH
594 if (host[0] == '[') {
595 end = strchr(host + 1, ']');
596 if (end) {
9aa5053d
IL
597 if (protocol != PROTO_GIT) {
598 *end = 0;
599 host++;
600 }
356bece0 601 end++;
356bece0
YH
602 } else
603 end = host;
604 } else
605 end = host;
606
607 path = strchr(end, c);
be501813 608 if (path && !has_dos_drive_prefix(end)) {
72a4f4b6 609 if (c == ':') {
8d3d28f5 610 if (host != url || path < strchrnul(host, '/')) {
60003340
NTND
611 protocol = PROTO_SSH;
612 *path++ = '\0';
613 } else /* '/' in the host part, assume local path */
614 path = end;
72a4f4b6
LT
615 }
616 } else
617 path = end;
2386d658 618
faea9ccb
AE
619 if (!path || !*path)
620 die("No path specified. See 'man git-pull' for valid url syntax");
621
622 /*
623 * null-terminate hostname and point path to ~ for URL's like this:
624 * ssh://host.xz/~user/repo
625 */
626 if (protocol != PROTO_LOCAL && host != url) {
627 char *ptr = path;
628 if (path[1] == '~')
629 path++;
da2a95b2 630 else {
9befac47 631 path = xstrdup(ptr);
da2a95b2
SH
632 free_path = 1;
633 }
faea9ccb
AE
634
635 *ptr = '\0';
636 }
637
2e776665
LT
638 /*
639 * Add support for ssh port: ssh://host.xy:<port>/...
640 */
641 if (protocol == PROTO_SSH && host != url)
7acf4382 642 port = get_port(end);
2e776665 643
f8014776 644 if (protocol == PROTO_GIT) {
5ad312be
JL
645 /* These underlying connection commands die() if they
646 * cannot connect.
647 */
9befac47 648 char *target_host = xstrdup(host);
e814bc4d 649 if (git_use_proxy(host))
5cbf8246 650 conn = git_proxy_connect(fd, host);
da2a95b2 651 else
7841ce79 652 git_tcp_connect(fd, host, flags);
5ad312be
JL
653 /*
654 * Separate original protocol components prog and path
73bb33a9
SP
655 * from extended host header with a NUL byte.
656 *
657 * Note: Do not add any other headers here! Doing so
658 * will cause older git-daemon servers to crash.
5ad312be
JL
659 */
660 packet_write(fd[1],
661 "%s %s%chost=%s%c",
662 prog, path, 0,
663 target_host, 0);
664 free(target_host);
4577370e 665 free(url);
da2a95b2
SH
666 if (free_path)
667 free(path);
5cbf8246 668 return conn;
f8014776 669 }
2386d658 670
98158e9c 671 conn = xcalloc(1, sizeof(*conn));
2e776665 672
f364cb88
JS
673 strbuf_init(&cmd, MAX_CMD_LEN);
674 strbuf_addstr(&cmd, prog);
675 strbuf_addch(&cmd, ' ');
676 sq_quote_buf(&cmd, path);
677 if (cmd.len >= MAX_CMD_LEN)
678 die("command line too long");
679
680 conn->in = conn->out = -1;
36ad53ff 681 conn->argv = arg = xcalloc(7, sizeof(*arg));
f364cb88
JS
682 if (protocol == PROTO_SSH) {
683 const char *ssh = getenv("GIT_SSH");
36ad53ff 684 int putty = ssh && strcasestr(ssh, "plink");
f364cb88
JS
685 if (!ssh) ssh = "ssh";
686
687 *arg++ = ssh;
36ad53ff
EY
688 if (putty && !strcasestr(ssh, "tortoiseplink"))
689 *arg++ = "-batch";
f364cb88 690 if (port) {
36ad53ff
EY
691 /* P is for PuTTY, p is for OpenSSH */
692 *arg++ = putty ? "-P" : "-p";
f364cb88 693 *arg++ = port;
4852f723 694 }
f364cb88
JS
695 *arg++ = host;
696 }
697 else {
48a7c1c4
GB
698 /* remove repo-local variables from the environment */
699 conn->env = local_repo_env;
4cfb2a44 700 conn->use_shell = 1;
016fb48b 701 }
f364cb88
JS
702 *arg++ = cmd.buf;
703 *arg = NULL;
704
705 if (start_command(conn))
706 die("unable to fork");
707
708 fd[0] = conn->out; /* read from child's stdout */
709 fd[1] = conn->in; /* write to child's stdin */
710 strbuf_release(&cmd);
4577370e 711 free(url);
da2a95b2
SH
712 if (free_path)
713 free(path);
98158e9c 714 return conn;
f7192598
LT
715}
716
7ffe853b
JK
717int git_connection_is_socket(struct child_process *conn)
718{
719 return conn == &no_fork;
720}
721
98158e9c 722int finish_connect(struct child_process *conn)
f7192598 723{
f364cb88 724 int code;
7ffe853b 725 if (!conn || git_connection_is_socket(conn))
f42a5c4e
FBH
726 return 0;
727
f364cb88
JS
728 code = finish_command(conn);
729 free(conn->argv);
98158e9c 730 free(conn);
f364cb88 731 return code;
f7192598 732}