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 (!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
);
178 } else if (mandatory
) {
179 die("Unknown mandatory capability %s. This remote "
180 "helper probably needs newer version of Git.\n",
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
]);
193 strbuf_release(&buf
);
195 fprintf(stderr
, "Debug: Capabilities complete.\n");
199 static int disconnect_helper(struct transport
*transport
)
201 struct helper_data
*data
= transport
->data
;
202 struct strbuf buf
= STRBUF_INIT
;
206 fprintf(stderr
, "Debug: Disconnecting.\n");
207 if (!data
->no_disconnect_req
) {
208 strbuf_addf(&buf
, "\n");
209 sendline(data
, &buf
);
211 close(data
->helper
->in
);
212 close(data
->helper
->out
);
214 finish_command(data
->helper
);
215 free((char *)data
->helper
->argv
[0]);
216 free(data
->helper
->argv
);
223 static const char *unsupported_options
[] = {
224 TRANS_OPT_UPLOADPACK
,
225 TRANS_OPT_RECEIVEPACK
,
229 static const char *boolean_options
[] = {
235 static int set_helper_option(struct transport
*transport
,
236 const char *name
, const char *value
)
238 struct helper_data
*data
= transport
->data
;
239 struct strbuf buf
= STRBUF_INIT
;
240 int i
, ret
, is_bool
= 0;
242 get_helper(transport
);
247 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
248 if (!strcmp(name
, unsupported_options
[i
]))
252 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
253 if (!strcmp(name
, boolean_options
[i
])) {
259 strbuf_addf(&buf
, "option %s ", name
);
261 strbuf_addstr(&buf
, value
? "true" : "false");
263 quote_c_style(value
, &buf
, NULL
, 0);
264 strbuf_addch(&buf
, '\n');
266 xchgline(data
, &buf
);
268 if (!strcmp(buf
.buf
, "ok"))
270 else if (!prefixcmp(buf
.buf
, "error")) {
272 } else if (!strcmp(buf
.buf
, "unsupported"))
275 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
278 strbuf_release(&buf
);
282 static void standard_options(struct transport
*t
)
288 set_helper_option(t
, "progress", t
->progress
? "true" : "false");
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
);
296 static int release_helper(struct transport
*transport
)
298 struct helper_data
*data
= transport
->data
;
299 free_refspec(data
->refspec_nr
, data
->refspecs
);
300 data
->refspecs
= NULL
;
301 disconnect_helper(transport
);
302 free(transport
->data
);
306 static int fetch_with_fetch(struct transport
*transport
,
307 int nr_heads
, struct ref
**to_fetch
)
309 struct helper_data
*data
= transport
->data
;
311 struct strbuf buf
= STRBUF_INIT
;
313 standard_options(transport
);
315 for (i
= 0; i
< nr_heads
; i
++) {
316 const struct ref
*posn
= to_fetch
[i
];
317 if (posn
->status
& REF_STATUS_UPTODATE
)
320 strbuf_addf(&buf
, "fetch %s %s\n",
321 sha1_to_hex(posn
->old_sha1
), posn
->name
);
324 strbuf_addch(&buf
, '\n');
325 sendline(data
, &buf
);
328 recvline(data
, &buf
);
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
);
335 transport
->pack_lockfile
= xstrdup(name
);
340 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
342 strbuf_release(&buf
);
346 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
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";
355 fastimport
->git_cmd
= 1;
356 return start_command(fastimport
);
359 static int fetch_with_import(struct transport
*transport
,
360 int nr_heads
, struct ref
**to_fetch
)
362 struct child_process fastimport
;
363 struct helper_data
*data
= transport
->data
;
366 struct strbuf buf
= STRBUF_INIT
;
368 get_helper(transport
);
370 if (get_importer(transport
, &fastimport
))
371 die("Couldn't run fast-import");
373 for (i
= 0; i
< nr_heads
; i
++) {
375 if (posn
->status
& REF_STATUS_UPTODATE
)
378 strbuf_addf(&buf
, "import %s\n", posn
->name
);
379 sendline(data
, &buf
);
382 disconnect_helper(transport
);
383 finish_command(&fastimport
);
384 free(fastimport
.argv
);
385 fastimport
.argv
= NULL
;
387 for (i
= 0; i
< nr_heads
; i
++) {
390 if (posn
->status
& REF_STATUS_UPTODATE
)
393 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
395 private = strdup(posn
->name
);
396 read_ref(private, posn
->old_sha1
);
399 strbuf_release(&buf
);
403 static int process_connect_service(struct transport
*transport
,
404 const char *name
, const char *exec
)
406 struct helper_data
*data
= transport
->data
;
407 struct strbuf cmdbuf
= STRBUF_INIT
;
408 struct child_process
*helper
;
409 int r
, duped
, ret
= 0;
412 helper
= get_helper(transport
);
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
420 duped
= dup(helper
->out
);
422 die_errno("Can't dup helper output fd");
423 input
= xfdopen(duped
, "r");
424 setvbuf(input
, NULL
, _IONBF
, 0);
427 * Handle --upload-pack and friends. This is fire and forget...
428 * just warn if it fails.
430 if (strcmp(name
, exec
)) {
431 r
= set_helper_option(transport
, "servpath", exec
);
433 warning("Setting remote service path not supported by protocol.");
435 warning("Invalid remote service path.");
439 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
443 sendline(data
, &cmdbuf
);
444 recvline_fh(input
, &cmdbuf
);
445 if (!strcmp(cmdbuf
.buf
, "")) {
446 data
->no_disconnect_req
= 1;
448 fprintf(stderr
, "Debug: Smart transport connection "
451 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
453 fprintf(stderr
, "Debug: Falling back to dumb "
456 die("Unknown response to connect: %s",
464 static int process_connect(struct transport
*transport
,
467 struct helper_data
*data
= transport
->data
;
471 name
= for_push
? "git-receive-pack" : "git-upload-pack";
473 exec
= data
->transport_options
.receivepack
;
475 exec
= data
->transport_options
.uploadpack
;
477 return process_connect_service(transport
, name
, exec
);
480 static int connect_helper(struct transport
*transport
, const char *name
,
481 const char *exec
, int fd
[2])
483 struct helper_data
*data
= transport
->data
;
485 /* Get_helper so connect is inited. */
486 get_helper(transport
);
488 die("Operation not supported by protocol.");
490 if (!process_connect_service(transport
, name
, exec
))
491 die("Can't connect to subservice %s.", name
);
493 fd
[0] = data
->helper
->out
;
494 fd
[1] = data
->helper
->in
;
498 static int fetch(struct transport
*transport
,
499 int nr_heads
, struct ref
**to_fetch
)
501 struct helper_data
*data
= transport
->data
;
504 if (process_connect(transport
, 0)) {
505 do_take_over(transport
);
506 return transport
->fetch(transport
, nr_heads
, to_fetch
);
510 for (i
= 0; i
< nr_heads
; i
++)
511 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
518 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
521 return fetch_with_import(transport
, nr_heads
, to_fetch
);
526 static int push_refs(struct transport
*transport
,
527 struct ref
*remote_refs
, int flags
)
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
;
536 if (process_connect(transport
, 1)) {
537 do_take_over(transport
);
538 return transport
->push_refs(transport
, remote_refs
, flags
);
542 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
543 "Perhaps you should specify a branch such as 'master'.\n");
547 helper
= get_helper(transport
);
551 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
552 if (!ref
->peer_ref
&& !mirror
)
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
:
567 strbuf_addstr(&buf
, "push ");
568 if (!ref
->deletion
) {
570 strbuf_addch(&buf
, '+');
572 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
574 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
576 strbuf_addch(&buf
, ':');
577 strbuf_addstr(&buf
, ref
->name
);
578 strbuf_addch(&buf
, '\n');
583 standard_options(transport
);
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
);
590 strbuf_addch(&buf
, '\n');
591 sendline(data
, &buf
);
598 recvline(data
, &buf
);
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;
609 die("expected ok/error, helper said '%s'\n", buf
.buf
);
611 msg
= strchr(refname
, ' ');
613 struct strbuf msg_buf
= STRBUF_INIT
;
617 if (!unquote_c_style(&msg_buf
, msg
, &end
))
618 msg
= strbuf_detach(&msg_buf
, NULL
);
621 strbuf_release(&msg_buf
);
623 if (!strcmp(msg
, "no match")) {
624 status
= REF_STATUS_NONE
;
628 else if (!strcmp(msg
, "up to date")) {
629 status
= REF_STATUS_UPTODATE
;
633 else if (!strcmp(msg
, "non-fast forward")) {
634 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
641 ref
= find_ref_by_name(ref
, refname
);
643 ref
= find_ref_by_name(remote_refs
, refname
);
645 warning("helper reported unexpected status of %s", refname
);
649 if (ref
->status
!= REF_STATUS_NONE
) {
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'.
654 if (status
== REF_STATUS_NONE
)
658 ref
->status
= status
;
659 ref
->remote_status
= msg
;
661 strbuf_release(&buf
);
665 static int has_attribute(const char *attrs
, const char *attr
) {
672 const char *space
= strchrnul(attrs
, ' ');
673 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
681 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
683 struct helper_data
*data
= transport
->data
;
684 struct child_process
*helper
;
685 struct ref
*ret
= NULL
;
686 struct ref
**tail
= &ret
;
688 struct strbuf buf
= STRBUF_INIT
;
690 helper
= get_helper(transport
);
692 if (process_connect(transport
, for_push
)) {
693 do_take_over(transport
);
694 return transport
->get_refs_list(transport
, for_push
);
697 if (data
->push
&& for_push
)
698 write_str_in_full(helper
->in
, "list for-push\n");
700 write_str_in_full(helper
->in
, "list\n");
704 recvline(data
, &buf
);
709 eov
= strchr(buf
.buf
, ' ');
711 die("Malformed response in ref list: %s", buf
.buf
);
712 eon
= strchr(eov
+ 1, ' ');
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
);
722 if (has_attribute(eon
+ 1, "unchanged")) {
723 (*tail
)->status
|= REF_STATUS_UPTODATE
;
724 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
727 tail
= &((*tail
)->next
);
730 fprintf(stderr
, "Debug: Read ref listing.\n");
731 strbuf_release(&buf
);
733 for (posn
= ret
; posn
; posn
= posn
->next
)
734 resolve_remote_symref(posn
, ret
);
739 int transport_helper_init(struct transport
*transport
, const char *name
)
741 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
744 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
747 transport
->data
= data
;
748 transport
->set_option
= set_helper_option
;
749 transport
->get_refs_list
= get_refs_list
;
750 transport
->fetch
= fetch
;
751 transport
->push_refs
= push_refs
;
752 transport
->disconnect
= release_helper
;
753 transport
->connect
= connect_helper
;
754 transport
->smart_options
= &(data
->transport_options
);