]>
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 IL |
173 | } else if (mandatory) { |
174 | die("Unknown madatory capability %s. This remote " | |
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; | |
486a3d71 | 282 | int no_progress = v < 0 || (!t->progress && !isatty(2)); |
ef08ef9e SP |
283 | |
284 | set_helper_option(t, "progress", !no_progress ? "true" : "false"); | |
285 | ||
286 | n = snprintf(buf, sizeof(buf), "%d", v + 1); | |
287 | if (n >= sizeof(buf)) | |
288 | die("impossibly large verbosity value"); | |
289 | set_helper_option(t, "verbosity", buf); | |
290 | } | |
291 | ||
f2a37151 DB |
292 | static int release_helper(struct transport *transport) |
293 | { | |
72ff8943 DB |
294 | struct helper_data *data = transport->data; |
295 | free_refspec(data->refspec_nr, data->refspecs); | |
296 | data->refspecs = NULL; | |
f2a37151 DB |
297 | disconnect_helper(transport); |
298 | free(transport->data); | |
299 | return 0; | |
300 | } | |
301 | ||
6eb996b5 | 302 | static int fetch_with_fetch(struct transport *transport, |
37148311 | 303 | int nr_heads, struct ref **to_fetch) |
6eb996b5 | 304 | { |
292ce46b | 305 | struct helper_data *data = transport->data; |
6eb996b5 DB |
306 | int i; |
307 | struct strbuf buf = STRBUF_INIT; | |
308 | ||
ef08ef9e SP |
309 | standard_options(transport); |
310 | ||
6eb996b5 | 311 | for (i = 0; i < nr_heads; i++) { |
1088261f | 312 | const struct ref *posn = to_fetch[i]; |
6eb996b5 DB |
313 | if (posn->status & REF_STATUS_UPTODATE) |
314 | continue; | |
2d14d65c DB |
315 | |
316 | strbuf_addf(&buf, "fetch %s %s\n", | |
317 | sha1_to_hex(posn->old_sha1), posn->name); | |
292ce46b | 318 | } |
2d14d65c | 319 | |
292ce46b | 320 | strbuf_addch(&buf, '\n'); |
bf3c523c | 321 | sendline(data, &buf); |
292ce46b SP |
322 | |
323 | while (1) { | |
bf3c523c | 324 | recvline(data, &buf); |
292ce46b SP |
325 | |
326 | if (!prefixcmp(buf.buf, "lock ")) { | |
327 | const char *name = buf.buf + 5; | |
328 | if (transport->pack_lockfile) | |
329 | warning("%s also locked %s", data->name, name); | |
330 | else | |
331 | transport->pack_lockfile = xstrdup(name); | |
332 | } | |
333 | else if (!buf.len) | |
334 | break; | |
335 | else | |
336 | warning("%s unexpectedly said: '%s'", data->name, buf.buf); | |
6eb996b5 | 337 | } |
292ce46b | 338 | strbuf_release(&buf); |
6eb996b5 DB |
339 | return 0; |
340 | } | |
341 | ||
e65e91ed DB |
342 | static int get_importer(struct transport *transport, struct child_process *fastimport) |
343 | { | |
344 | struct child_process *helper = get_helper(transport); | |
345 | memset(fastimport, 0, sizeof(*fastimport)); | |
346 | fastimport->in = helper->out; | |
347 | fastimport->argv = xcalloc(5, sizeof(*fastimport->argv)); | |
348 | fastimport->argv[0] = "fast-import"; | |
349 | fastimport->argv[1] = "--quiet"; | |
350 | ||
351 | fastimport->git_cmd = 1; | |
352 | return start_command(fastimport); | |
353 | } | |
354 | ||
355 | static int fetch_with_import(struct transport *transport, | |
356 | int nr_heads, struct ref **to_fetch) | |
357 | { | |
358 | struct child_process fastimport; | |
72ff8943 | 359 | struct helper_data *data = transport->data; |
e65e91ed DB |
360 | int i; |
361 | struct ref *posn; | |
362 | struct strbuf buf = STRBUF_INIT; | |
363 | ||
bf3c523c IL |
364 | get_helper(transport); |
365 | ||
e65e91ed DB |
366 | if (get_importer(transport, &fastimport)) |
367 | die("Couldn't run fast-import"); | |
368 | ||
369 | for (i = 0; i < nr_heads; i++) { | |
370 | posn = to_fetch[i]; | |
371 | if (posn->status & REF_STATUS_UPTODATE) | |
372 | continue; | |
373 | ||
374 | strbuf_addf(&buf, "import %s\n", posn->name); | |
bf3c523c | 375 | sendline(data, &buf); |
e65e91ed DB |
376 | strbuf_reset(&buf); |
377 | } | |
378 | disconnect_helper(transport); | |
379 | finish_command(&fastimport); | |
b962dbdc SR |
380 | free(fastimport.argv); |
381 | fastimport.argv = NULL; | |
e65e91ed DB |
382 | |
383 | for (i = 0; i < nr_heads; i++) { | |
72ff8943 | 384 | char *private; |
e65e91ed DB |
385 | posn = to_fetch[i]; |
386 | if (posn->status & REF_STATUS_UPTODATE) | |
387 | continue; | |
72ff8943 DB |
388 | if (data->refspecs) |
389 | private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name); | |
390 | else | |
391 | private = strdup(posn->name); | |
392 | read_ref(private, posn->old_sha1); | |
393 | free(private); | |
e65e91ed | 394 | } |
b962dbdc | 395 | strbuf_release(&buf); |
e65e91ed DB |
396 | return 0; |
397 | } | |
398 | ||
fa8c097c IL |
399 | static int process_connect_service(struct transport *transport, |
400 | const char *name, const char *exec) | |
401 | { | |
402 | struct helper_data *data = transport->data; | |
403 | struct strbuf cmdbuf = STRBUF_INIT; | |
404 | struct child_process *helper; | |
405 | int r, duped, ret = 0; | |
406 | FILE *input; | |
407 | ||
408 | helper = get_helper(transport); | |
409 | ||
410 | /* | |
411 | * Yes, dup the pipe another time, as we need unbuffered version | |
412 | * of input pipe as FILE*. fclose() closes the underlying fd and | |
413 | * stream buffering only can be changed before first I/O operation | |
414 | * on it. | |
415 | */ | |
416 | duped = dup(helper->out); | |
417 | if (duped < 0) | |
418 | die_errno("Can't dup helper output fd"); | |
419 | input = xfdopen(duped, "r"); | |
420 | setvbuf(input, NULL, _IONBF, 0); | |
421 | ||
422 | /* | |
423 | * Handle --upload-pack and friends. This is fire and forget... | |
424 | * just warn if it fails. | |
425 | */ | |
426 | if (strcmp(name, exec)) { | |
427 | r = set_helper_option(transport, "servpath", exec); | |
428 | if (r > 0) | |
429 | warning("Setting remote service path not supported by protocol."); | |
430 | else if (r < 0) | |
431 | warning("Invalid remote service path."); | |
432 | } | |
433 | ||
434 | if (data->connect) | |
435 | strbuf_addf(&cmdbuf, "connect %s\n", name); | |
436 | else | |
437 | goto exit; | |
438 | ||
439 | sendline(data, &cmdbuf); | |
440 | recvline_fh(input, &cmdbuf); | |
441 | if (!strcmp(cmdbuf.buf, "")) { | |
442 | data->no_disconnect_req = 1; | |
443 | if (debug) | |
444 | fprintf(stderr, "Debug: Smart transport connection " | |
445 | "ready.\n"); | |
446 | ret = 1; | |
447 | } else if (!strcmp(cmdbuf.buf, "fallback")) { | |
448 | if (debug) | |
449 | fprintf(stderr, "Debug: Falling back to dumb " | |
450 | "transport.\n"); | |
451 | } else | |
452 | die("Unknown response to connect: %s", | |
453 | cmdbuf.buf); | |
454 | ||
455 | exit: | |
456 | fclose(input); | |
457 | return ret; | |
458 | } | |
459 | ||
460 | static int process_connect(struct transport *transport, | |
461 | int for_push) | |
462 | { | |
463 | struct helper_data *data = transport->data; | |
464 | const char *name; | |
465 | const char *exec; | |
466 | ||
467 | name = for_push ? "git-receive-pack" : "git-upload-pack"; | |
468 | if (for_push) | |
469 | exec = data->transport_options.receivepack; | |
470 | else | |
471 | exec = data->transport_options.uploadpack; | |
472 | ||
473 | return process_connect_service(transport, name, exec); | |
474 | } | |
475 | ||
b236752a IL |
476 | static int connect_helper(struct transport *transport, const char *name, |
477 | const char *exec, int fd[2]) | |
478 | { | |
479 | struct helper_data *data = transport->data; | |
480 | ||
481 | /* Get_helper so connect is inited. */ | |
482 | get_helper(transport); | |
483 | if (!data->connect) | |
484 | die("Operation not supported by protocol."); | |
485 | ||
486 | if (!process_connect_service(transport, name, exec)) | |
487 | die("Can't connect to subservice %s.", name); | |
488 | ||
489 | fd[0] = data->helper->out; | |
490 | fd[1] = data->helper->in; | |
491 | return 0; | |
492 | } | |
493 | ||
6eb996b5 | 494 | static int fetch(struct transport *transport, |
37148311 | 495 | int nr_heads, struct ref **to_fetch) |
6eb996b5 DB |
496 | { |
497 | struct helper_data *data = transport->data; | |
498 | int i, count; | |
499 | ||
fa8c097c IL |
500 | if (process_connect(transport, 0)) { |
501 | do_take_over(transport); | |
502 | return transport->fetch(transport, nr_heads, to_fetch); | |
503 | } | |
504 | ||
6eb996b5 DB |
505 | count = 0; |
506 | for (i = 0; i < nr_heads; i++) | |
507 | if (!(to_fetch[i]->status & REF_STATUS_UPTODATE)) | |
508 | count++; | |
509 | ||
510 | if (!count) | |
511 | return 0; | |
512 | ||
513 | if (data->fetch) | |
514 | return fetch_with_fetch(transport, nr_heads, to_fetch); | |
515 | ||
e65e91ed DB |
516 | if (data->import) |
517 | return fetch_with_import(transport, nr_heads, to_fetch); | |
518 | ||
6eb996b5 DB |
519 | return -1; |
520 | } | |
521 | ||
ae4efe19 SP |
522 | static int push_refs(struct transport *transport, |
523 | struct ref *remote_refs, int flags) | |
524 | { | |
525 | int force_all = flags & TRANSPORT_PUSH_FORCE; | |
526 | int mirror = flags & TRANSPORT_PUSH_MIRROR; | |
527 | struct helper_data *data = transport->data; | |
528 | struct strbuf buf = STRBUF_INIT; | |
529 | struct child_process *helper; | |
530 | struct ref *ref; | |
531 | ||
fa8c097c IL |
532 | if (process_connect(transport, 1)) { |
533 | do_take_over(transport); | |
534 | return transport->push_refs(transport, remote_refs, flags); | |
535 | } | |
536 | ||
c1ceea1d TRC |
537 | if (!remote_refs) { |
538 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n" | |
539 | "Perhaps you should specify a branch such as 'master'.\n"); | |
ae4efe19 | 540 | return 0; |
c1ceea1d | 541 | } |
ae4efe19 SP |
542 | |
543 | helper = get_helper(transport); | |
544 | if (!data->push) | |
545 | return 1; | |
546 | ||
547 | for (ref = remote_refs; ref; ref = ref->next) { | |
20e8b465 | 548 | if (!ref->peer_ref && !mirror) |
ae4efe19 SP |
549 | continue; |
550 | ||
20e8b465 TRC |
551 | /* Check for statuses set by set_ref_status_for_push() */ |
552 | switch (ref->status) { | |
553 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
554 | case REF_STATUS_UPTODATE: | |
ae4efe19 | 555 | continue; |
20e8b465 TRC |
556 | default: |
557 | ; /* do nothing */ | |
ae4efe19 SP |
558 | } |
559 | ||
560 | if (force_all) | |
561 | ref->force = 1; | |
562 | ||
563 | strbuf_addstr(&buf, "push "); | |
564 | if (!ref->deletion) { | |
565 | if (ref->force) | |
566 | strbuf_addch(&buf, '+'); | |
567 | if (ref->peer_ref) | |
568 | strbuf_addstr(&buf, ref->peer_ref->name); | |
569 | else | |
570 | strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1)); | |
571 | } | |
572 | strbuf_addch(&buf, ':'); | |
573 | strbuf_addstr(&buf, ref->name); | |
574 | strbuf_addch(&buf, '\n'); | |
575 | } | |
d8f67d20 CB |
576 | if (buf.len == 0) |
577 | return 0; | |
ae4efe19 SP |
578 | |
579 | transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0; | |
580 | standard_options(transport); | |
581 | ||
582 | if (flags & TRANSPORT_PUSH_DRY_RUN) { | |
583 | if (set_helper_option(transport, "dry-run", "true") != 0) | |
584 | die("helper %s does not support dry-run", data->name); | |
585 | } | |
586 | ||
587 | strbuf_addch(&buf, '\n'); | |
bf3c523c | 588 | sendline(data, &buf); |
ae4efe19 SP |
589 | |
590 | ref = remote_refs; | |
591 | while (1) { | |
592 | char *refname, *msg; | |
593 | int status; | |
594 | ||
bf3c523c | 595 | recvline(data, &buf); |
ae4efe19 SP |
596 | if (!buf.len) |
597 | break; | |
598 | ||
599 | if (!prefixcmp(buf.buf, "ok ")) { | |
600 | status = REF_STATUS_OK; | |
601 | refname = buf.buf + 3; | |
602 | } else if (!prefixcmp(buf.buf, "error ")) { | |
603 | status = REF_STATUS_REMOTE_REJECT; | |
604 | refname = buf.buf + 6; | |
605 | } else | |
606 | die("expected ok/error, helper said '%s'\n", buf.buf); | |
607 | ||
608 | msg = strchr(refname, ' '); | |
609 | if (msg) { | |
610 | struct strbuf msg_buf = STRBUF_INIT; | |
611 | const char *end; | |
612 | ||
613 | *msg++ = '\0'; | |
614 | if (!unquote_c_style(&msg_buf, msg, &end)) | |
615 | msg = strbuf_detach(&msg_buf, NULL); | |
616 | else | |
617 | msg = xstrdup(msg); | |
618 | strbuf_release(&msg_buf); | |
619 | ||
620 | if (!strcmp(msg, "no match")) { | |
621 | status = REF_STATUS_NONE; | |
622 | free(msg); | |
623 | msg = NULL; | |
624 | } | |
625 | else if (!strcmp(msg, "up to date")) { | |
626 | status = REF_STATUS_UPTODATE; | |
627 | free(msg); | |
628 | msg = NULL; | |
629 | } | |
630 | else if (!strcmp(msg, "non-fast forward")) { | |
631 | status = REF_STATUS_REJECT_NONFASTFORWARD; | |
632 | free(msg); | |
633 | msg = NULL; | |
634 | } | |
635 | } | |
636 | ||
637 | if (ref) | |
638 | ref = find_ref_by_name(ref, refname); | |
639 | if (!ref) | |
640 | ref = find_ref_by_name(remote_refs, refname); | |
641 | if (!ref) { | |
642 | warning("helper reported unexpected status of %s", refname); | |
643 | continue; | |
644 | } | |
645 | ||
08d63a42 TRC |
646 | if (ref->status != REF_STATUS_NONE) { |
647 | /* | |
648 | * Earlier, the ref was marked not to be pushed, so ignore the ref | |
649 | * status reported by the remote helper if the latter is 'no match'. | |
650 | */ | |
651 | if (status == REF_STATUS_NONE) | |
652 | continue; | |
653 | } | |
654 | ||
ae4efe19 SP |
655 | ref->status = status; |
656 | ref->remote_status = msg; | |
657 | } | |
658 | strbuf_release(&buf); | |
659 | return 0; | |
660 | } | |
661 | ||
f8ec9167 DB |
662 | static int has_attribute(const char *attrs, const char *attr) { |
663 | int len; | |
664 | if (!attrs) | |
665 | return 0; | |
666 | ||
667 | len = strlen(attr); | |
668 | for (;;) { | |
669 | const char *space = strchrnul(attrs, ' '); | |
670 | if (len == space - attrs && !strncmp(attrs, attr, len)) | |
671 | return 1; | |
672 | if (!*space) | |
673 | return 0; | |
674 | attrs = space + 1; | |
675 | } | |
676 | } | |
677 | ||
6eb996b5 DB |
678 | static struct ref *get_refs_list(struct transport *transport, int for_push) |
679 | { | |
292ce46b | 680 | struct helper_data *data = transport->data; |
6eb996b5 DB |
681 | struct child_process *helper; |
682 | struct ref *ret = NULL; | |
683 | struct ref **tail = &ret; | |
684 | struct ref *posn; | |
685 | struct strbuf buf = STRBUF_INIT; | |
6eb996b5 DB |
686 | |
687 | helper = get_helper(transport); | |
2d14d65c | 688 | |
fa8c097c IL |
689 | if (process_connect(transport, for_push)) { |
690 | do_take_over(transport); | |
691 | return transport->get_refs_list(transport, for_push); | |
692 | } | |
693 | ||
ae4efe19 SP |
694 | if (data->push && for_push) |
695 | write_str_in_full(helper->in, "list for-push\n"); | |
696 | else | |
697 | write_str_in_full(helper->in, "list\n"); | |
6eb996b5 | 698 | |
6eb996b5 DB |
699 | while (1) { |
700 | char *eov, *eon; | |
bf3c523c | 701 | recvline(data, &buf); |
6eb996b5 DB |
702 | |
703 | if (!*buf.buf) | |
704 | break; | |
705 | ||
706 | eov = strchr(buf.buf, ' '); | |
707 | if (!eov) | |
708 | die("Malformed response in ref list: %s", buf.buf); | |
709 | eon = strchr(eov + 1, ' '); | |
710 | *eov = '\0'; | |
711 | if (eon) | |
712 | *eon = '\0'; | |
713 | *tail = alloc_ref(eov + 1); | |
714 | if (buf.buf[0] == '@') | |
715 | (*tail)->symref = xstrdup(buf.buf + 1); | |
716 | else if (buf.buf[0] != '?') | |
717 | get_sha1_hex(buf.buf, (*tail)->old_sha1); | |
f8ec9167 DB |
718 | if (eon) { |
719 | if (has_attribute(eon + 1, "unchanged")) { | |
720 | (*tail)->status |= REF_STATUS_UPTODATE; | |
721 | read_ref((*tail)->name, (*tail)->old_sha1); | |
722 | } | |
723 | } | |
6eb996b5 DB |
724 | tail = &((*tail)->next); |
725 | } | |
bf3c523c IL |
726 | if (debug) |
727 | fprintf(stderr, "Debug: Read ref listing.\n"); | |
6eb996b5 DB |
728 | strbuf_release(&buf); |
729 | ||
730 | for (posn = ret; posn; posn = posn->next) | |
731 | resolve_remote_symref(posn, ret); | |
732 | ||
733 | return ret; | |
734 | } | |
735 | ||
c9e388bb | 736 | int transport_helper_init(struct transport *transport, const char *name) |
6eb996b5 DB |
737 | { |
738 | struct helper_data *data = xcalloc(sizeof(*data), 1); | |
c9e388bb | 739 | data->name = name; |
6eb996b5 | 740 | |
bf3c523c IL |
741 | if (getenv("GIT_TRANSPORT_HELPER_DEBUG")) |
742 | debug = 1; | |
743 | ||
6eb996b5 | 744 | transport->data = data; |
ef08ef9e | 745 | transport->set_option = set_helper_option; |
6eb996b5 DB |
746 | transport->get_refs_list = get_refs_list; |
747 | transport->fetch = fetch; | |
ae4efe19 | 748 | transport->push_refs = push_refs; |
f2a37151 | 749 | transport->disconnect = release_helper; |
b236752a | 750 | transport->connect = connect_helper; |
61b075bd | 751 | transport->smart_options = &(data->transport_options); |
6eb996b5 DB |
752 | return 0; |
753 | } |