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