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