4 #include "run-command.h"
16 struct child_process
*helper
;
23 no_disconnect_req
: 1;
24 /* These go from remote name (as in "list") to private name */
25 struct refspec
*refspecs
;
27 /* Transport options for fetch-pack/send-pack (should one of
30 struct git_transport_options transport_options
;
33 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
36 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
37 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
)
39 die_errno("Full write to remote helper failed");
42 static int recvline_fh(FILE *helper
, struct strbuf
*buffer
)
46 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
47 if (strbuf_getline(buffer
, helper
, '\n') == EOF
) {
49 fprintf(stderr
, "Debug: Remote helper quit.\n");
54 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
58 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
60 return recvline_fh(helper
->out
, buffer
);
63 static void xchgline(struct helper_data
*helper
, struct strbuf
*buffer
)
65 sendline(helper
, buffer
);
66 recvline(helper
, buffer
);
69 static void write_constant(int fd
, const char *str
)
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");
77 const char *remove_ext_force(const char *url
)
80 const char *colon
= strchr(url
, ':');
81 if (colon
&& colon
[1] == ':')
87 static void do_take_over(struct transport
*transport
)
89 struct helper_data
*data
;
90 data
= (struct helper_data
*)transport
->data
;
91 transport_take_over(transport
, data
->helper
);
96 static struct child_process
*get_helper(struct transport
*transport
)
98 struct helper_data
*data
= transport
->data
;
99 struct strbuf buf
= STRBUF_INIT
;
100 struct child_process
*helper
;
101 const char **refspecs
= NULL
;
103 int refspec_alloc
= 0;
110 helper
= xcalloc(1, sizeof(*helper
));
114 helper
->argv
= xcalloc(4, sizeof(*helper
->argv
));
115 strbuf_addf(&buf
, "git-remote-%s", data
->name
);
116 helper
->argv
[0] = strbuf_detach(&buf
, NULL
);
117 helper
->argv
[1] = transport
->remote
->name
;
118 helper
->argv
[2] = remove_ext_force(transport
->url
);
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
);
127 data
->helper
= helper
;
128 data
->no_disconnect_req
= 0;
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.
135 duped
= dup(helper
->out
);
137 die_errno("Can't dup helper output fd");
138 data
->out
= xfdopen(duped
, "r");
140 write_constant(helper
->in
, "capabilities\n");
145 recvline(data
, &buf
);
150 if (*buf
.buf
== '*') {
151 capname
= buf
.buf
+ 1;
157 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
158 if (!strcmp(capname
, "fetch"))
160 else if (!strcmp(capname
, "option"))
162 else if (!strcmp(capname
, "push"))
164 else if (!strcmp(capname
, "import"))
166 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
170 refspecs
[refspec_nr
++] = strdup(buf
.buf
+ strlen("refspec "));
171 } else if (!strcmp(capname
, "connect")) {
173 } else if (mandatory
) {
174 die("Unknown mandatory capability %s. This remote "
175 "helper probably needs newer version of Git.\n",
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
]);
188 strbuf_release(&buf
);
190 fprintf(stderr
, "Debug: Capabilities complete.\n");
194 static int disconnect_helper(struct transport
*transport
)
196 struct helper_data
*data
= transport
->data
;
197 struct strbuf buf
= STRBUF_INIT
;
201 fprintf(stderr
, "Debug: Disconnecting.\n");
202 if (!data
->no_disconnect_req
) {
203 strbuf_addf(&buf
, "\n");
204 sendline(data
, &buf
);
206 close(data
->helper
->in
);
207 close(data
->helper
->out
);
209 finish_command(data
->helper
);
210 free((char *)data
->helper
->argv
[0]);
211 free(data
->helper
->argv
);
218 static const char *unsupported_options
[] = {
219 TRANS_OPT_UPLOADPACK
,
220 TRANS_OPT_RECEIVEPACK
,
224 static const char *boolean_options
[] = {
230 static int set_helper_option(struct transport
*transport
,
231 const char *name
, const char *value
)
233 struct helper_data
*data
= transport
->data
;
234 struct strbuf buf
= STRBUF_INIT
;
235 int i
, ret
, is_bool
= 0;
237 get_helper(transport
);
242 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
243 if (!strcmp(name
, unsupported_options
[i
]))
247 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
248 if (!strcmp(name
, boolean_options
[i
])) {
254 strbuf_addf(&buf
, "option %s ", name
);
256 strbuf_addstr(&buf
, value
? "true" : "false");
258 quote_c_style(value
, &buf
, NULL
, 0);
259 strbuf_addch(&buf
, '\n');
261 xchgline(data
, &buf
);
263 if (!strcmp(buf
.buf
, "ok"))
265 else if (!prefixcmp(buf
.buf
, "error")) {
267 } else if (!strcmp(buf
.buf
, "unsupported"))
270 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
273 strbuf_release(&buf
);
277 static void standard_options(struct transport
*t
)
283 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
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
);
291 static int release_helper(struct transport
*transport
)
293 struct helper_data
*data
= transport
->data
;
294 free_refspec(data
->refspec_nr
, data
->refspecs
);
295 data
->refspecs
= NULL
;
296 disconnect_helper(transport
);
297 free(transport
->data
);
301 static int fetch_with_fetch(struct transport
*transport
,
302 int nr_heads
, struct ref
**to_fetch
)
304 struct helper_data
*data
= transport
->data
;
306 struct strbuf buf
= STRBUF_INIT
;
308 standard_options(transport
);
310 for (i
= 0; i
< nr_heads
; i
++) {
311 const struct ref
*posn
= to_fetch
[i
];
312 if (posn
->status
& REF_STATUS_UPTODATE
)
315 strbuf_addf(&buf
, "fetch %s %s\n",
316 sha1_to_hex(posn
->old_sha1
), posn
->name
);
319 strbuf_addch(&buf
, '\n');
320 sendline(data
, &buf
);
323 recvline(data
, &buf
);
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
);
330 transport
->pack_lockfile
= xstrdup(name
);
335 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
337 strbuf_release(&buf
);
341 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
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";
350 fastimport
->git_cmd
= 1;
351 return start_command(fastimport
);
354 static int fetch_with_import(struct transport
*transport
,
355 int nr_heads
, struct ref
**to_fetch
)
357 struct child_process fastimport
;
358 struct helper_data
*data
= transport
->data
;
361 struct strbuf buf
= STRBUF_INIT
;
363 get_helper(transport
);
365 if (get_importer(transport
, &fastimport
))
366 die("Couldn't run fast-import");
368 for (i
= 0; i
< nr_heads
; i
++) {
370 if (posn
->status
& REF_STATUS_UPTODATE
)
373 strbuf_addf(&buf
, "import %s\n", posn
->name
);
374 sendline(data
, &buf
);
377 disconnect_helper(transport
);
378 finish_command(&fastimport
);
379 free(fastimport
.argv
);
380 fastimport
.argv
= NULL
;
382 for (i
= 0; i
< nr_heads
; i
++) {
385 if (posn
->status
& REF_STATUS_UPTODATE
)
388 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
390 private = strdup(posn
->name
);
391 read_ref(private, posn
->old_sha1
);
394 strbuf_release(&buf
);
398 static int process_connect_service(struct transport
*transport
,
399 const char *name
, const char *exec
)
401 struct helper_data
*data
= transport
->data
;
402 struct strbuf cmdbuf
= STRBUF_INIT
;
403 struct child_process
*helper
;
404 int r
, duped
, ret
= 0;
407 helper
= get_helper(transport
);
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
415 duped
= dup(helper
->out
);
417 die_errno("Can't dup helper output fd");
418 input
= xfdopen(duped
, "r");
419 setvbuf(input
, NULL
, _IONBF
, 0);
422 * Handle --upload-pack and friends. This is fire and forget...
423 * just warn if it fails.
425 if (strcmp(name
, exec
)) {
426 r
= set_helper_option(transport
, "servpath", exec
);
428 warning("Setting remote service path not supported by protocol.");
430 warning("Invalid remote service path.");
434 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
438 sendline(data
, &cmdbuf
);
439 recvline_fh(input
, &cmdbuf
);
440 if (!strcmp(cmdbuf
.buf
, "")) {
441 data
->no_disconnect_req
= 1;
443 fprintf(stderr
, "Debug: Smart transport connection "
446 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
448 fprintf(stderr
, "Debug: Falling back to dumb "
451 die("Unknown response to connect: %s",
459 static int process_connect(struct transport
*transport
,
462 struct helper_data
*data
= transport
->data
;
466 name
= for_push
? "git-receive-pack" : "git-upload-pack";
468 exec
= data
->transport_options
.receivepack
;
470 exec
= data
->transport_options
.uploadpack
;
472 return process_connect_service(transport
, name
, exec
);
475 static int connect_helper(struct transport
*transport
, const char *name
,
476 const char *exec
, int fd
[2])
478 struct helper_data
*data
= transport
->data
;
480 /* Get_helper so connect is inited. */
481 get_helper(transport
);
483 die("Operation not supported by protocol.");
485 if (!process_connect_service(transport
, name
, exec
))
486 die("Can't connect to subservice %s.", name
);
488 fd
[0] = data
->helper
->out
;
489 fd
[1] = data
->helper
->in
;
493 static int fetch(struct transport
*transport
,
494 int nr_heads
, struct ref
**to_fetch
)
496 struct helper_data
*data
= transport
->data
;
499 if (process_connect(transport
, 0)) {
500 do_take_over(transport
);
501 return transport
->fetch(transport
, nr_heads
, to_fetch
);
505 for (i
= 0; i
< nr_heads
; i
++)
506 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
513 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
516 return fetch_with_import(transport
, nr_heads
, to_fetch
);
521 static int push_refs(struct transport
*transport
,
522 struct ref
*remote_refs
, int flags
)
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
;
531 if (process_connect(transport
, 1)) {
532 do_take_over(transport
);
533 return transport
->push_refs(transport
, remote_refs
, flags
);
537 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
538 "Perhaps you should specify a branch such as 'master'.\n");
542 helper
= get_helper(transport
);
546 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
547 if (!ref
->peer_ref
&& !mirror
)
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
:
562 strbuf_addstr(&buf
, "push ");
563 if (!ref
->deletion
) {
565 strbuf_addch(&buf
, '+');
567 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
569 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
571 strbuf_addch(&buf
, ':');
572 strbuf_addstr(&buf
, ref
->name
);
573 strbuf_addch(&buf
, '\n');
578 standard_options(transport
);
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
);
585 strbuf_addch(&buf
, '\n');
586 sendline(data
, &buf
);
593 recvline(data
, &buf
);
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;
604 die("expected ok/error, helper said '%s'\n", buf
.buf
);
606 msg
= strchr(refname
, ' ');
608 struct strbuf msg_buf
= STRBUF_INIT
;
612 if (!unquote_c_style(&msg_buf
, msg
, &end
))
613 msg
= strbuf_detach(&msg_buf
, NULL
);
616 strbuf_release(&msg_buf
);
618 if (!strcmp(msg
, "no match")) {
619 status
= REF_STATUS_NONE
;
623 else if (!strcmp(msg
, "up to date")) {
624 status
= REF_STATUS_UPTODATE
;
628 else if (!strcmp(msg
, "non-fast forward")) {
629 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
636 ref
= find_ref_by_name(ref
, refname
);
638 ref
= find_ref_by_name(remote_refs
, refname
);
640 warning("helper reported unexpected status of %s", refname
);
644 if (ref
->status
!= REF_STATUS_NONE
) {
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'.
649 if (status
== REF_STATUS_NONE
)
653 ref
->status
= status
;
654 ref
->remote_status
= msg
;
656 strbuf_release(&buf
);
660 static int has_attribute(const char *attrs
, const char *attr
) {
667 const char *space
= strchrnul(attrs
, ' ');
668 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
676 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
678 struct helper_data
*data
= transport
->data
;
679 struct child_process
*helper
;
680 struct ref
*ret
= NULL
;
681 struct ref
**tail
= &ret
;
683 struct strbuf buf
= STRBUF_INIT
;
685 helper
= get_helper(transport
);
687 if (process_connect(transport
, for_push
)) {
688 do_take_over(transport
);
689 return transport
->get_refs_list(transport
, for_push
);
692 if (data
->push
&& for_push
)
693 write_str_in_full(helper
->in
, "list for-push\n");
695 write_str_in_full(helper
->in
, "list\n");
699 recvline(data
, &buf
);
704 eov
= strchr(buf
.buf
, ' ');
706 die("Malformed response in ref list: %s", buf
.buf
);
707 eon
= strchr(eov
+ 1, ' ');
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
);
717 if (has_attribute(eon
+ 1, "unchanged")) {
718 (*tail
)->status
|= REF_STATUS_UPTODATE
;
719 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
722 tail
= &((*tail
)->next
);
725 fprintf(stderr
, "Debug: Read ref listing.\n");
726 strbuf_release(&buf
);
728 for (posn
= ret
; posn
; posn
= posn
->next
)
729 resolve_remote_symref(posn
, ret
);
734 int transport_helper_init(struct transport
*transport
, const char *name
)
736 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
739 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
742 transport
->data
= data
;
743 transport
->set_option
= set_helper_option
;
744 transport
->get_refs_list
= get_refs_list
;
745 transport
->fetch
= fetch
;
746 transport
->push_refs
= push_refs
;
747 transport
->disconnect
= release_helper
;
748 transport
->connect
= connect_helper
;
749 transport
->smart_options
= &(data
->transport_options
);