]>
Commit | Line | Data |
---|---|---|
6eb996b5 DB |
1 | #include "cache.h" |
2 | #include "transport.h" | |
ae4efe19 | 3 | #include "quote.h" |
6eb996b5 DB |
4 | #include "run-command.h" |
5 | #include "commit.h" | |
6 | #include "diff.h" | |
7 | #include "revision.h" | |
ef08ef9e | 8 | #include "quote.h" |
72ff8943 | 9 | #include "remote.h" |
6eb996b5 | 10 | |
bf3c523c IL |
11 | static int debug; |
12 | ||
6eb996b5 DB |
13 | struct helper_data |
14 | { | |
15 | const char *name; | |
16 | struct child_process *helper; | |
292ce46b | 17 | FILE *out; |
ef08ef9e | 18 | unsigned fetch : 1, |
a24a32dd | 19 | import : 1, |
ae4efe19 | 20 | option : 1, |
fa8c097c IL |
21 | push : 1, |
22 | connect : 1, | |
23 | no_disconnect_req : 1; | |
72ff8943 DB |
24 | /* These go from remote name (as in "list") to private name */ |
25 | struct refspec *refspecs; | |
26 | int refspec_nr; | |
61b075bd IL |
27 | /* Transport options for fetch-pack/send-pack (should one of |
28 | * those be invoked). | |
29 | */ | |
30 | struct git_transport_options transport_options; | |
6eb996b5 DB |
31 | }; |
32 | ||
bf3c523c IL |
33 | static void sendline(struct helper_data *helper, struct strbuf *buffer) |
34 | { | |
35 | if (debug) | |
36 | fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf); | |
37 | if (write_in_full(helper->helper->in, buffer->buf, buffer->len) | |
38 | != buffer->len) | |
39 | die_errno("Full write to remote helper failed"); | |
40 | } | |
41 | ||
fa8c097c | 42 | static int recvline_fh(FILE *helper, struct strbuf *buffer) |
bf3c523c IL |
43 | { |
44 | strbuf_reset(buffer); | |
45 | if (debug) | |
46 | fprintf(stderr, "Debug: Remote helper: Waiting...\n"); | |
fa8c097c | 47 | if (strbuf_getline(buffer, helper, '\n') == EOF) { |
bf3c523c IL |
48 | if (debug) |
49 | fprintf(stderr, "Debug: Remote helper quit.\n"); | |
50 | exit(128); | |
51 | } | |
52 | ||
53 | if (debug) | |
54 | fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf); | |
55 | return 0; | |
56 | } | |
57 | ||
fa8c097c IL |
58 | static int recvline(struct helper_data *helper, struct strbuf *buffer) |
59 | { | |
60 | return recvline_fh(helper->out, buffer); | |
61 | } | |
62 | ||
bf3c523c IL |
63 | static void xchgline(struct helper_data *helper, struct strbuf *buffer) |
64 | { | |
65 | sendline(helper, buffer); | |
66 | recvline(helper, buffer); | |
67 | } | |
68 | ||
69 | static void write_constant(int fd, const char *str) | |
70 | { | |
71 | if (debug) | |
72 | fprintf(stderr, "Debug: Remote helper: -> %s", str); | |
73 | if (write_in_full(fd, str, strlen(str)) != strlen(str)) | |
74 | die_errno("Full write to remote helper failed"); | |
75 | } | |
76 | ||
25d5cc48 IL |
77 | const char *remove_ext_force(const char *url) |
78 | { | |
79 | if (url) { | |
80 | const char *colon = strchr(url, ':'); | |
81 | if (colon && colon[1] == ':') | |
82 | return colon + 2; | |
83 | } | |
84 | return url; | |
85 | } | |
86 | ||
fa8c097c IL |
87 | static void do_take_over(struct transport *transport) |
88 | { | |
89 | struct helper_data *data; | |
90 | data = (struct helper_data *)transport->data; | |
91 | transport_take_over(transport, data->helper); | |
92 | fclose(data->out); | |
93 | free(data); | |
94 | } | |
95 | ||
6eb996b5 DB |
96 | static struct child_process *get_helper(struct transport *transport) |
97 | { | |
98 | struct helper_data *data = transport->data; | |
99 | struct strbuf buf = STRBUF_INIT; | |
100 | struct child_process *helper; | |
72ff8943 DB |
101 | const char **refspecs = NULL; |
102 | int refspec_nr = 0; | |
103 | int refspec_alloc = 0; | |
61b075bd | 104 | int duped; |
6b02de3b | 105 | int code; |
6eb996b5 DB |
106 | |
107 | if (data->helper) | |
108 | return data->helper; | |
109 | ||
110 | helper = xcalloc(1, sizeof(*helper)); | |
111 | helper->in = -1; | |
112 | helper->out = -1; | |
113 | helper->err = 0; | |
114 | helper->argv = xcalloc(4, sizeof(*helper->argv)); | |
6b02de3b | 115 | strbuf_addf(&buf, "git-remote-%s", data->name); |
6eb996b5 DB |
116 | helper->argv[0] = strbuf_detach(&buf, NULL); |
117 | helper->argv[1] = transport->remote->name; | |
25d5cc48 | 118 | helper->argv[2] = remove_ext_force(transport->url); |
6b02de3b IL |
119 | helper->git_cmd = 0; |
120 | helper->silent_exec_failure = 1; | |
121 | code = start_command(helper); | |
122 | if (code < 0 && errno == ENOENT) | |
123 | die("Unable to find remote helper for '%s'", data->name); | |
124 | else if (code != 0) | |
125 | exit(code); | |
126 | ||
6eb996b5 | 127 | data->helper = helper; |
fa8c097c | 128 | data->no_disconnect_req = 0; |
6eb996b5 | 129 | |
61b075bd IL |
130 | /* |
131 | * Open the output as FILE* so strbuf_getline() can be used. | |
132 | * Do this with duped fd because fclose() will close the fd, | |
133 | * and stuff like taking over will require the fd to remain. | |
61b075bd IL |
134 | */ |
135 | duped = dup(helper->out); | |
136 | if (duped < 0) | |
137 | die_errno("Can't dup helper output fd"); | |
138 | data->out = xfdopen(duped, "r"); | |
139 | ||
bf3c523c | 140 | write_constant(helper->in, "capabilities\n"); |
2d14d65c | 141 | |
6eb996b5 | 142 | while (1) { |
28ed5b35 IL |
143 | const char *capname; |
144 | int mandatory = 0; | |
bf3c523c | 145 | recvline(data, &buf); |
6eb996b5 DB |
146 | |
147 | if (!*buf.buf) | |
148 | break; | |
28ed5b35 IL |
149 | |
150 | if (*buf.buf == '*') { | |
151 | capname = buf.buf + 1; | |
152 | mandatory = 1; | |
153 | } else | |
154 | capname = buf.buf; | |
155 | ||
bf3c523c | 156 | if (debug) |
28ed5b35 IL |
157 | fprintf(stderr, "Debug: Got cap %s\n", capname); |
158 | if (!strcmp(capname, "fetch")) | |
6eb996b5 | 159 | data->fetch = 1; |
28ed5b35 | 160 | else if (!strcmp(capname, "option")) |
ef08ef9e | 161 | data->option = 1; |
28ed5b35 | 162 | else if (!strcmp(capname, "push")) |
ae4efe19 | 163 | data->push = 1; |
28ed5b35 | 164 | else if (!strcmp(capname, "import")) |
e65e91ed | 165 | data->import = 1; |
28ed5b35 | 166 | else if (!data->refspecs && !prefixcmp(capname, "refspec ")) { |
72ff8943 DB |
167 | ALLOC_GROW(refspecs, |
168 | refspec_nr + 1, | |
169 | refspec_alloc); | |
170 | refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec ")); | |
fa8c097c IL |
171 | } else if (!strcmp(capname, "connect")) { |
172 | data->connect = 1; | |
28ed5b35 | 173 | } else if (mandatory) { |
9517e6b8 | 174 | die("Unknown mandatory capability %s. This remote " |
28ed5b35 IL |
175 | "helper probably needs newer version of Git.\n", |
176 | capname); | |
72ff8943 DB |
177 | } |
178 | } | |
179 | if (refspecs) { | |
180 | int i; | |
181 | data->refspec_nr = refspec_nr; | |
182 | data->refspecs = parse_fetch_refspec(refspec_nr, refspecs); | |
183 | for (i = 0; i < refspec_nr; i++) { | |
184 | free((char *)refspecs[i]); | |
185 | } | |
186 | free(refspecs); | |
6eb996b5 | 187 | } |
b962dbdc | 188 | strbuf_release(&buf); |
bf3c523c IL |
189 | if (debug) |
190 | fprintf(stderr, "Debug: Capabilities complete.\n"); | |
6eb996b5 DB |
191 | return data->helper; |
192 | } | |
193 | ||
194 | static int disconnect_helper(struct transport *transport) | |
195 | { | |
196 | struct helper_data *data = transport->data; | |
bf3c523c IL |
197 | struct strbuf buf = STRBUF_INIT; |
198 | ||
6eb996b5 | 199 | if (data->helper) { |
bf3c523c IL |
200 | if (debug) |
201 | fprintf(stderr, "Debug: Disconnecting.\n"); | |
fa8c097c IL |
202 | if (!data->no_disconnect_req) { |
203 | strbuf_addf(&buf, "\n"); | |
204 | sendline(data, &buf); | |
205 | } | |
6eb996b5 | 206 | close(data->helper->in); |
61b075bd | 207 | close(data->helper->out); |
292ce46b | 208 | fclose(data->out); |
6eb996b5 DB |
209 | finish_command(data->helper); |
210 | free((char *)data->helper->argv[0]); | |
211 | free(data->helper->argv); | |
212 | free(data->helper); | |
213 | data->helper = NULL; | |
214 | } | |
215 | return 0; | |
216 | } | |
217 | ||
ef08ef9e SP |
218 | static const char *unsupported_options[] = { |
219 | TRANS_OPT_UPLOADPACK, | |
220 | TRANS_OPT_RECEIVEPACK, | |
221 | TRANS_OPT_THIN, | |
222 | TRANS_OPT_KEEP | |
223 | }; | |
224 | static const char *boolean_options[] = { | |
225 | TRANS_OPT_THIN, | |
226 | TRANS_OPT_KEEP, | |
227 | TRANS_OPT_FOLLOWTAGS | |
228 | }; | |
229 | ||
230 | static int set_helper_option(struct transport *transport, | |
231 | const char *name, const char *value) | |
232 | { | |
233 | struct helper_data *data = transport->data; | |
ef08ef9e SP |
234 | struct strbuf buf = STRBUF_INIT; |
235 | int i, ret, is_bool = 0; | |
236 | ||
bf3c523c IL |
237 | get_helper(transport); |
238 | ||
ef08ef9e SP |
239 | if (!data->option) |
240 | return 1; | |
241 | ||
242 | for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) { | |
243 | if (!strcmp(name, unsupported_options[i])) | |
244 | return 1; | |
245 | } | |
246 | ||
247 | for (i = 0; i < ARRAY_SIZE(boolean_options); i++) { | |
248 | if (!strcmp(name, boolean_options[i])) { | |
249 | is_bool = 1; | |
250 | break; | |
251 | } | |
252 | } | |
253 | ||
254 | strbuf_addf(&buf, "option %s ", name); | |
255 | if (is_bool) | |
256 | strbuf_addstr(&buf, value ? "true" : "false"); | |
257 | else | |
258 | quote_c_style(value, &buf, NULL, 0); | |
259 | strbuf_addch(&buf, '\n'); | |
260 | ||
bf3c523c | 261 | xchgline(data, &buf); |
ef08ef9e SP |
262 | |
263 | if (!strcmp(buf.buf, "ok")) | |
264 | ret = 0; | |
265 | else if (!prefixcmp(buf.buf, "error")) { | |
266 | ret = -1; | |
267 | } else if (!strcmp(buf.buf, "unsupported")) | |
268 | ret = 1; | |
269 | else { | |
270 | warning("%s unexpectedly said: '%s'", data->name, buf.buf); | |
271 | ret = 1; | |
272 | } | |
273 | strbuf_release(&buf); | |
274 | return ret; | |
275 | } | |
276 | ||
277 | static void standard_options(struct transport *t) | |
278 | { | |
279 | char buf[16]; | |
280 | int n; | |
281 | int v = t->verbose; | |
ef08ef9e | 282 | |
d01b3c02 | 283 | set_helper_option(t, "progress", t->progress ? "true" : "false"); |
ef08ef9e SP |
284 | |
285 | n = snprintf(buf, sizeof(buf), "%d", v + 1); | |
286 | if (n >= sizeof(buf)) | |
287 | die("impossibly large verbosity value"); | |
288 | set_helper_option(t, "verbosity", buf); | |
289 | } | |
290 | ||
f2a37151 DB |
291 | static int release_helper(struct transport *transport) |
292 | { | |
72ff8943 DB |
293 | struct helper_data *data = transport->data; |
294 | free_refspec(data->refspec_nr, data->refspecs); | |
295 | data->refspecs = NULL; | |
f2a37151 DB |
296 | disconnect_helper(transport); |
297 | free(transport->data); | |
298 | return 0; | |
299 | } | |
300 | ||
6eb996b5 | 301 | static int fetch_with_fetch(struct transport *transport, |
37148311 | 302 | int nr_heads, struct ref **to_fetch) |
6eb996b5 | 303 | { |
292ce46b | 304 | struct helper_data *data = transport->data; |
6eb996b5 DB |
305 | int i; |
306 | struct strbuf buf = STRBUF_INIT; | |
307 | ||
ef08ef9e SP |
308 | standard_options(transport); |
309 | ||
6eb996b5 | 310 | for (i = 0; i < nr_heads; i++) { |
1088261f | 311 | const struct ref *posn = to_fetch[i]; |
6eb996b5 DB |
312 | if (posn->status & REF_STATUS_UPTODATE) |
313 | continue; | |
2d14d65c DB |
314 | |
315 | strbuf_addf(&buf, "fetch %s %s\n", | |
316 | sha1_to_hex(posn->old_sha1), posn->name); | |
292ce46b | 317 | } |
2d14d65c | 318 | |
292ce46b | 319 | strbuf_addch(&buf, '\n'); |
bf3c523c | 320 | sendline(data, &buf); |
292ce46b SP |
321 | |
322 | while (1) { | |
bf3c523c | 323 | recvline(data, &buf); |
292ce46b SP |
324 | |
325 | if (!prefixcmp(buf.buf, "lock ")) { | |
326 | const char *name = buf.buf + 5; | |
327 | if (transport->pack_lockfile) | |
328 | warning("%s also locked %s", data->name, name); | |
329 | else | |
330 | transport->pack_lockfile = xstrdup(name); | |
331 | } | |
332 | else if (!buf.len) | |
333 | break; | |
334 | else | |
335 | warning("%s unexpectedly said: '%s'", data->name, buf.buf); | |
6eb996b5 | 336 | } |
292ce46b | 337 | strbuf_release(&buf); |
6eb996b5 DB |
338 | return 0; |
339 | } | |
340 | ||
e65e91ed DB |
341 | static int get_importer(struct transport *transport, struct child_process *fastimport) |
342 | { | |
343 | struct child_process *helper = get_helper(transport); | |
344 | memset(fastimport, 0, sizeof(*fastimport)); | |
345 | fastimport->in = helper->out; | |
346 | fastimport->argv = xcalloc(5, sizeof(*fastimport->argv)); | |
347 | fastimport->argv[0] = "fast-import"; | |
348 | fastimport->argv[1] = "--quiet"; | |
349 | ||
350 | fastimport->git_cmd = 1; | |
351 | return start_command(fastimport); | |
352 | } | |
353 | ||
354 | static int fetch_with_import(struct transport *transport, | |
355 | int nr_heads, struct ref **to_fetch) | |
356 | { | |
357 | struct child_process fastimport; | |
72ff8943 | 358 | struct helper_data *data = transport->data; |
e65e91ed DB |
359 | int i; |
360 | struct ref *posn; | |
361 | struct strbuf buf = STRBUF_INIT; | |
362 | ||
bf3c523c IL |
363 | get_helper(transport); |
364 | ||
e65e91ed DB |
365 | if (get_importer(transport, &fastimport)) |
366 | die("Couldn't run fast-import"); | |
367 | ||
368 | for (i = 0; i < nr_heads; i++) { | |
369 | posn = to_fetch[i]; | |
370 | if (posn->status & REF_STATUS_UPTODATE) | |
371 | continue; | |
372 | ||
373 | strbuf_addf(&buf, "import %s\n", posn->name); | |
bf3c523c | 374 | sendline(data, &buf); |
e65e91ed DB |
375 | strbuf_reset(&buf); |
376 | } | |
377 | disconnect_helper(transport); | |
378 | finish_command(&fastimport); | |
b962dbdc SR |
379 | free(fastimport.argv); |
380 | fastimport.argv = NULL; | |
e65e91ed DB |
381 | |
382 | for (i = 0; i < nr_heads; i++) { | |
72ff8943 | 383 | char *private; |
e65e91ed DB |
384 | posn = to_fetch[i]; |
385 | if (posn->status & REF_STATUS_UPTODATE) | |
386 | continue; | |
72ff8943 DB |
387 | if (data->refspecs) |
388 | private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name); | |
389 | else | |
390 | private = strdup(posn->name); | |
391 | read_ref(private, posn->old_sha1); | |
392 | free(private); | |
e65e91ed | 393 | } |
b962dbdc | 394 | strbuf_release(&buf); |
e65e91ed DB |
395 | return 0; |
396 | } | |
397 | ||
fa8c097c IL |
398 | static int process_connect_service(struct transport *transport, |
399 | const char *name, const char *exec) | |
400 | { | |
401 | struct helper_data *data = transport->data; | |
402 | struct strbuf cmdbuf = STRBUF_INIT; | |
403 | struct child_process *helper; | |
404 | int r, duped, ret = 0; | |
405 | FILE *input; | |
406 | ||
407 | helper = get_helper(transport); | |
408 | ||
409 | /* | |
410 | * Yes, dup the pipe another time, as we need unbuffered version | |
411 | * of input pipe as FILE*. fclose() closes the underlying fd and | |
412 | * stream buffering only can be changed before first I/O operation | |
413 | * on it. | |
414 | */ | |
415 | duped = dup(helper->out); | |
416 | if (duped < 0) | |
417 | die_errno("Can't dup helper output fd"); | |
418 | input = xfdopen(duped, "r"); | |
419 | setvbuf(input, NULL, _IONBF, 0); | |
420 | ||
421 | /* | |
422 | * Handle --upload-pack and friends. This is fire and forget... | |
423 | * just warn if it fails. | |
424 | */ | |
425 | if (strcmp(name, exec)) { | |
426 | r = set_helper_option(transport, "servpath", exec); | |
427 | if (r > 0) | |
428 | warning("Setting remote service path not supported by protocol."); | |
429 | else if (r < 0) | |
430 | warning("Invalid remote service path."); | |
431 | } | |
432 | ||
433 | if (data->connect) | |
434 | strbuf_addf(&cmdbuf, "connect %s\n", name); | |
435 | else | |
436 | goto exit; | |
437 | ||
438 | sendline(data, &cmdbuf); | |
439 | recvline_fh(input, &cmdbuf); | |
440 | if (!strcmp(cmdbuf.buf, "")) { | |
441 | data->no_disconnect_req = 1; | |
442 | if (debug) | |
443 | fprintf(stderr, "Debug: Smart transport connection " | |
444 | "ready.\n"); | |
445 | ret = 1; | |
446 | } else if (!strcmp(cmdbuf.buf, "fallback")) { | |
447 | if (debug) | |
448 | fprintf(stderr, "Debug: Falling back to dumb " | |
449 | "transport.\n"); | |
450 | } else | |
451 | die("Unknown response to connect: %s", | |
452 | cmdbuf.buf); | |
453 | ||
454 | exit: | |
455 | fclose(input); | |
456 | return ret; | |
457 | } | |
458 | ||
459 | static int process_connect(struct transport *transport, | |
460 | int for_push) | |
461 | { | |
462 | struct helper_data *data = transport->data; | |
463 | const char *name; | |
464 | const char *exec; | |
465 | ||
466 | name = for_push ? "git-receive-pack" : "git-upload-pack"; | |
467 | if (for_push) | |
468 | exec = data->transport_options.receivepack; | |
469 | else | |
470 | exec = data->transport_options.uploadpack; | |
471 | ||
472 | return process_connect_service(transport, name, exec); | |
473 | } | |
474 | ||
b236752a IL |
475 | static int connect_helper(struct transport *transport, const char *name, |
476 | const char *exec, int fd[2]) | |
477 | { | |
478 | struct helper_data *data = transport->data; | |
479 | ||
480 | /* Get_helper so connect is inited. */ | |
481 | get_helper(transport); | |
482 | if (!data->connect) | |
483 | die("Operation not supported by protocol."); | |
484 | ||
485 | if (!process_connect_service(transport, name, exec)) | |
486 | die("Can't connect to subservice %s.", name); | |
487 | ||
488 | fd[0] = data->helper->out; | |
489 | fd[1] = data->helper->in; | |
490 | return 0; | |
491 | } | |
492 | ||
6eb996b5 | 493 | static int fetch(struct transport *transport, |
37148311 | 494 | int nr_heads, struct ref **to_fetch) |
6eb996b5 DB |
495 | { |
496 | struct helper_data *data = transport->data; | |
497 | int i, count; | |
498 | ||
fa8c097c IL |
499 | if (process_connect(transport, 0)) { |
500 | do_take_over(transport); | |
501 | return transport->fetch(transport, nr_heads, to_fetch); | |
502 | } | |
503 | ||
6eb996b5 DB |
504 | count = 0; |
505 | for (i = 0; i < nr_heads; i++) | |
506 | if (!(to_fetch[i]->status & REF_STATUS_UPTODATE)) | |
507 | count++; | |
508 | ||
509 | if (!count) | |
510 | return 0; | |
511 | ||
512 | if (data->fetch) | |
513 | return fetch_with_fetch(transport, nr_heads, to_fetch); | |
514 | ||
e65e91ed DB |
515 | if (data->import) |
516 | return fetch_with_import(transport, nr_heads, to_fetch); | |
517 | ||
6eb996b5 DB |
518 | return -1; |
519 | } | |
520 | ||
ae4efe19 SP |
521 | static int push_refs(struct transport *transport, |
522 | struct ref *remote_refs, int flags) | |
523 | { | |
524 | int force_all = flags & TRANSPORT_PUSH_FORCE; | |
525 | int mirror = flags & TRANSPORT_PUSH_MIRROR; | |
526 | struct helper_data *data = transport->data; | |
527 | struct strbuf buf = STRBUF_INIT; | |
528 | struct child_process *helper; | |
529 | struct ref *ref; | |
530 | ||
fa8c097c IL |
531 | if (process_connect(transport, 1)) { |
532 | do_take_over(transport); | |
533 | return transport->push_refs(transport, remote_refs, flags); | |
534 | } | |
535 | ||
c1ceea1d TRC |
536 | if (!remote_refs) { |
537 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n" | |
538 | "Perhaps you should specify a branch such as 'master'.\n"); | |
ae4efe19 | 539 | return 0; |
c1ceea1d | 540 | } |
ae4efe19 SP |
541 | |
542 | helper = get_helper(transport); | |
543 | if (!data->push) | |
544 | return 1; | |
545 | ||
546 | for (ref = remote_refs; ref; ref = ref->next) { | |
20e8b465 | 547 | if (!ref->peer_ref && !mirror) |
ae4efe19 SP |
548 | continue; |
549 | ||
20e8b465 TRC |
550 | /* Check for statuses set by set_ref_status_for_push() */ |
551 | switch (ref->status) { | |
552 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
553 | case REF_STATUS_UPTODATE: | |
ae4efe19 | 554 | continue; |
20e8b465 TRC |
555 | default: |
556 | ; /* do nothing */ | |
ae4efe19 SP |
557 | } |
558 | ||
559 | if (force_all) | |
560 | ref->force = 1; | |
561 | ||
562 | strbuf_addstr(&buf, "push "); | |
563 | if (!ref->deletion) { | |
564 | if (ref->force) | |
565 | strbuf_addch(&buf, '+'); | |
566 | if (ref->peer_ref) | |
567 | strbuf_addstr(&buf, ref->peer_ref->name); | |
568 | else | |
569 | strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1)); | |
570 | } | |
571 | strbuf_addch(&buf, ':'); | |
572 | strbuf_addstr(&buf, ref->name); | |
573 | strbuf_addch(&buf, '\n'); | |
574 | } | |
d8f67d20 CB |
575 | if (buf.len == 0) |
576 | return 0; | |
ae4efe19 | 577 | |
ae4efe19 SP |
578 | standard_options(transport); |
579 | ||
580 | if (flags & TRANSPORT_PUSH_DRY_RUN) { | |
581 | if (set_helper_option(transport, "dry-run", "true") != 0) | |
582 | die("helper %s does not support dry-run", data->name); | |
583 | } | |
584 | ||
585 | strbuf_addch(&buf, '\n'); | |
bf3c523c | 586 | sendline(data, &buf); |
ae4efe19 SP |
587 | |
588 | ref = remote_refs; | |
589 | while (1) { | |
590 | char *refname, *msg; | |
591 | int status; | |
592 | ||
bf3c523c | 593 | recvline(data, &buf); |
ae4efe19 SP |
594 | if (!buf.len) |
595 | break; | |
596 | ||
597 | if (!prefixcmp(buf.buf, "ok ")) { | |
598 | status = REF_STATUS_OK; | |
599 | refname = buf.buf + 3; | |
600 | } else if (!prefixcmp(buf.buf, "error ")) { | |
601 | status = REF_STATUS_REMOTE_REJECT; | |
602 | refname = buf.buf + 6; | |
603 | } else | |
604 | die("expected ok/error, helper said '%s'\n", buf.buf); | |
605 | ||
606 | msg = strchr(refname, ' '); | |
607 | if (msg) { | |
608 | struct strbuf msg_buf = STRBUF_INIT; | |
609 | const char *end; | |
610 | ||
611 | *msg++ = '\0'; | |
612 | if (!unquote_c_style(&msg_buf, msg, &end)) | |
613 | msg = strbuf_detach(&msg_buf, NULL); | |
614 | else | |
615 | msg = xstrdup(msg); | |
616 | strbuf_release(&msg_buf); | |
617 | ||
618 | if (!strcmp(msg, "no match")) { | |
619 | status = REF_STATUS_NONE; | |
620 | free(msg); | |
621 | msg = NULL; | |
622 | } | |
623 | else if (!strcmp(msg, "up to date")) { | |
624 | status = REF_STATUS_UPTODATE; | |
625 | free(msg); | |
626 | msg = NULL; | |
627 | } | |
628 | else if (!strcmp(msg, "non-fast forward")) { | |
629 | status = REF_STATUS_REJECT_NONFASTFORWARD; | |
630 | free(msg); | |
631 | msg = NULL; | |
632 | } | |
633 | } | |
634 | ||
635 | if (ref) | |
636 | ref = find_ref_by_name(ref, refname); | |
637 | if (!ref) | |
638 | ref = find_ref_by_name(remote_refs, refname); | |
639 | if (!ref) { | |
640 | warning("helper reported unexpected status of %s", refname); | |
641 | continue; | |
642 | } | |
643 | ||
08d63a42 TRC |
644 | if (ref->status != REF_STATUS_NONE) { |
645 | /* | |
646 | * Earlier, the ref was marked not to be pushed, so ignore the ref | |
647 | * status reported by the remote helper if the latter is 'no match'. | |
648 | */ | |
649 | if (status == REF_STATUS_NONE) | |
650 | continue; | |
651 | } | |
652 | ||
ae4efe19 SP |
653 | ref->status = status; |
654 | ref->remote_status = msg; | |
655 | } | |
656 | strbuf_release(&buf); | |
657 | return 0; | |
658 | } | |
659 | ||
f8ec9167 DB |
660 | static int has_attribute(const char *attrs, const char *attr) { |
661 | int len; | |
662 | if (!attrs) | |
663 | return 0; | |
664 | ||
665 | len = strlen(attr); | |
666 | for (;;) { | |
667 | const char *space = strchrnul(attrs, ' '); | |
668 | if (len == space - attrs && !strncmp(attrs, attr, len)) | |
669 | return 1; | |
670 | if (!*space) | |
671 | return 0; | |
672 | attrs = space + 1; | |
673 | } | |
674 | } | |
675 | ||
6eb996b5 DB |
676 | static struct ref *get_refs_list(struct transport *transport, int for_push) |
677 | { | |
292ce46b | 678 | struct helper_data *data = transport->data; |
6eb996b5 DB |
679 | struct child_process *helper; |
680 | struct ref *ret = NULL; | |
681 | struct ref **tail = &ret; | |
682 | struct ref *posn; | |
683 | struct strbuf buf = STRBUF_INIT; | |
6eb996b5 DB |
684 | |
685 | helper = get_helper(transport); | |
2d14d65c | 686 | |
fa8c097c IL |
687 | if (process_connect(transport, for_push)) { |
688 | do_take_over(transport); | |
689 | return transport->get_refs_list(transport, for_push); | |
690 | } | |
691 | ||
ae4efe19 SP |
692 | if (data->push && for_push) |
693 | write_str_in_full(helper->in, "list for-push\n"); | |
694 | else | |
695 | write_str_in_full(helper->in, "list\n"); | |
6eb996b5 | 696 | |
6eb996b5 DB |
697 | while (1) { |
698 | char *eov, *eon; | |
bf3c523c | 699 | recvline(data, &buf); |
6eb996b5 DB |
700 | |
701 | if (!*buf.buf) | |
702 | break; | |
703 | ||
704 | eov = strchr(buf.buf, ' '); | |
705 | if (!eov) | |
706 | die("Malformed response in ref list: %s", buf.buf); | |
707 | eon = strchr(eov + 1, ' '); | |
708 | *eov = '\0'; | |
709 | if (eon) | |
710 | *eon = '\0'; | |
711 | *tail = alloc_ref(eov + 1); | |
712 | if (buf.buf[0] == '@') | |
713 | (*tail)->symref = xstrdup(buf.buf + 1); | |
714 | else if (buf.buf[0] != '?') | |
715 | get_sha1_hex(buf.buf, (*tail)->old_sha1); | |
f8ec9167 DB |
716 | if (eon) { |
717 | if (has_attribute(eon + 1, "unchanged")) { | |
718 | (*tail)->status |= REF_STATUS_UPTODATE; | |
719 | read_ref((*tail)->name, (*tail)->old_sha1); | |
720 | } | |
721 | } | |
6eb996b5 DB |
722 | tail = &((*tail)->next); |
723 | } | |
bf3c523c IL |
724 | if (debug) |
725 | fprintf(stderr, "Debug: Read ref listing.\n"); | |
6eb996b5 DB |
726 | strbuf_release(&buf); |
727 | ||
728 | for (posn = ret; posn; posn = posn->next) | |
729 | resolve_remote_symref(posn, ret); | |
730 | ||
731 | return ret; | |
732 | } | |
733 | ||
c9e388bb | 734 | int transport_helper_init(struct transport *transport, const char *name) |
6eb996b5 DB |
735 | { |
736 | struct helper_data *data = xcalloc(sizeof(*data), 1); | |
c9e388bb | 737 | data->name = name; |
6eb996b5 | 738 | |
bf3c523c IL |
739 | if (getenv("GIT_TRANSPORT_HELPER_DEBUG")) |
740 | debug = 1; | |
741 | ||
6eb996b5 | 742 | transport->data = data; |
ef08ef9e | 743 | transport->set_option = set_helper_option; |
6eb996b5 DB |
744 | transport->get_refs_list = get_refs_list; |
745 | transport->fetch = fetch; | |
ae4efe19 | 746 | transport->push_refs = push_refs; |
f2a37151 | 747 | transport->disconnect = release_helper; |
b236752a | 748 | transport->connect = connect_helper; |
61b075bd | 749 | transport->smart_options = &(data->transport_options); |
6eb996b5 DB |
750 | return 0; |
751 | } |