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;
109 helper
= xcalloc(1, sizeof(*helper
));
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
;
117 helper
->argv
[2] = remove_ext_force(transport
->url
);
119 if (start_command(helper
))
120 die("Unable to run helper: git %s", helper
->argv
[0]);
121 data
->helper
= helper
;
122 data
->no_disconnect_req
= 0;
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.
129 duped
= dup(helper
->out
);
131 die_errno("Can't dup helper output fd");
132 data
->out
= xfdopen(duped
, "r");
134 write_constant(helper
->in
, "capabilities\n");
139 recvline(data
, &buf
);
144 if (*buf
.buf
== '*') {
145 capname
= buf
.buf
+ 1;
151 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
152 if (!strcmp(capname
, "fetch"))
154 else if (!strcmp(capname
, "option"))
156 else if (!strcmp(capname
, "push"))
158 else if (!strcmp(capname
, "import"))
160 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
164 refspecs
[refspec_nr
++] = strdup(buf
.buf
+ strlen("refspec "));
165 } else if (!strcmp(capname
, "connect")) {
167 } else if (mandatory
) {
168 die("Unknown madatory capability %s. This remote "
169 "helper probably needs newer version of Git.\n",
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
]);
182 strbuf_release(&buf
);
184 fprintf(stderr
, "Debug: Capabilities complete.\n");
188 static int disconnect_helper(struct transport
*transport
)
190 struct helper_data
*data
= transport
->data
;
191 struct strbuf buf
= STRBUF_INIT
;
195 fprintf(stderr
, "Debug: Disconnecting.\n");
196 if (!data
->no_disconnect_req
) {
197 strbuf_addf(&buf
, "\n");
198 sendline(data
, &buf
);
200 close(data
->helper
->in
);
201 close(data
->helper
->out
);
203 finish_command(data
->helper
);
204 free((char *)data
->helper
->argv
[0]);
205 free(data
->helper
->argv
);
212 static const char *unsupported_options
[] = {
213 TRANS_OPT_UPLOADPACK
,
214 TRANS_OPT_RECEIVEPACK
,
218 static const char *boolean_options
[] = {
224 static int set_helper_option(struct transport
*transport
,
225 const char *name
, const char *value
)
227 struct helper_data
*data
= transport
->data
;
228 struct strbuf buf
= STRBUF_INIT
;
229 int i
, ret
, is_bool
= 0;
231 get_helper(transport
);
236 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
237 if (!strcmp(name
, unsupported_options
[i
]))
241 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
242 if (!strcmp(name
, boolean_options
[i
])) {
248 strbuf_addf(&buf
, "option %s ", name
);
250 strbuf_addstr(&buf
, value
? "true" : "false");
252 quote_c_style(value
, &buf
, NULL
, 0);
253 strbuf_addch(&buf
, '\n');
255 xchgline(data
, &buf
);
257 if (!strcmp(buf
.buf
, "ok"))
259 else if (!prefixcmp(buf
.buf
, "error")) {
261 } else if (!strcmp(buf
.buf
, "unsupported"))
264 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
267 strbuf_release(&buf
);
271 static void standard_options(struct transport
*t
)
276 int no_progress
= v
< 0 || (!t
->progress
&& !isatty(2));
278 set_helper_option(t
, "progress", !no_progress
? "true" : "false");
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
);
286 static int release_helper(struct transport
*transport
)
288 struct helper_data
*data
= transport
->data
;
289 free_refspec(data
->refspec_nr
, data
->refspecs
);
290 data
->refspecs
= NULL
;
291 disconnect_helper(transport
);
292 free(transport
->data
);
296 static int fetch_with_fetch(struct transport
*transport
,
297 int nr_heads
, struct ref
**to_fetch
)
299 struct helper_data
*data
= transport
->data
;
301 struct strbuf buf
= STRBUF_INIT
;
303 standard_options(transport
);
305 for (i
= 0; i
< nr_heads
; i
++) {
306 const struct ref
*posn
= to_fetch
[i
];
307 if (posn
->status
& REF_STATUS_UPTODATE
)
310 strbuf_addf(&buf
, "fetch %s %s\n",
311 sha1_to_hex(posn
->old_sha1
), posn
->name
);
314 strbuf_addch(&buf
, '\n');
315 sendline(data
, &buf
);
318 recvline(data
, &buf
);
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
);
325 transport
->pack_lockfile
= xstrdup(name
);
330 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
332 strbuf_release(&buf
);
336 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
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";
345 fastimport
->git_cmd
= 1;
346 return start_command(fastimport
);
349 static int fetch_with_import(struct transport
*transport
,
350 int nr_heads
, struct ref
**to_fetch
)
352 struct child_process fastimport
;
353 struct helper_data
*data
= transport
->data
;
356 struct strbuf buf
= STRBUF_INIT
;
358 get_helper(transport
);
360 if (get_importer(transport
, &fastimport
))
361 die("Couldn't run fast-import");
363 for (i
= 0; i
< nr_heads
; i
++) {
365 if (posn
->status
& REF_STATUS_UPTODATE
)
368 strbuf_addf(&buf
, "import %s\n", posn
->name
);
369 sendline(data
, &buf
);
372 disconnect_helper(transport
);
373 finish_command(&fastimport
);
374 free(fastimport
.argv
);
375 fastimport
.argv
= NULL
;
377 for (i
= 0; i
< nr_heads
; i
++) {
380 if (posn
->status
& REF_STATUS_UPTODATE
)
383 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
385 private = strdup(posn
->name
);
386 read_ref(private, posn
->old_sha1
);
389 strbuf_release(&buf
);
393 static int process_connect_service(struct transport
*transport
,
394 const char *name
, const char *exec
)
396 struct helper_data
*data
= transport
->data
;
397 struct strbuf cmdbuf
= STRBUF_INIT
;
398 struct child_process
*helper
;
399 int r
, duped
, ret
= 0;
402 helper
= get_helper(transport
);
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
410 duped
= dup(helper
->out
);
412 die_errno("Can't dup helper output fd");
413 input
= xfdopen(duped
, "r");
414 setvbuf(input
, NULL
, _IONBF
, 0);
417 * Handle --upload-pack and friends. This is fire and forget...
418 * just warn if it fails.
420 if (strcmp(name
, exec
)) {
421 r
= set_helper_option(transport
, "servpath", exec
);
423 warning("Setting remote service path not supported by protocol.");
425 warning("Invalid remote service path.");
429 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
433 sendline(data
, &cmdbuf
);
434 recvline_fh(input
, &cmdbuf
);
435 if (!strcmp(cmdbuf
.buf
, "")) {
436 data
->no_disconnect_req
= 1;
438 fprintf(stderr
, "Debug: Smart transport connection "
441 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
443 fprintf(stderr
, "Debug: Falling back to dumb "
446 die("Unknown response to connect: %s",
454 static int process_connect(struct transport
*transport
,
457 struct helper_data
*data
= transport
->data
;
461 name
= for_push
? "git-receive-pack" : "git-upload-pack";
463 exec
= data
->transport_options
.receivepack
;
465 exec
= data
->transport_options
.uploadpack
;
467 return process_connect_service(transport
, name
, exec
);
470 static int connect_helper(struct transport
*transport
, const char *name
,
471 const char *exec
, int fd
[2])
473 struct helper_data
*data
= transport
->data
;
475 /* Get_helper so connect is inited. */
476 get_helper(transport
);
478 die("Operation not supported by protocol.");
480 if (!process_connect_service(transport
, name
, exec
))
481 die("Can't connect to subservice %s.", name
);
483 fd
[0] = data
->helper
->out
;
484 fd
[1] = data
->helper
->in
;
488 static int fetch(struct transport
*transport
,
489 int nr_heads
, struct ref
**to_fetch
)
491 struct helper_data
*data
= transport
->data
;
494 if (process_connect(transport
, 0)) {
495 do_take_over(transport
);
496 return transport
->fetch(transport
, nr_heads
, to_fetch
);
500 for (i
= 0; i
< nr_heads
; i
++)
501 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
508 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
511 return fetch_with_import(transport
, nr_heads
, to_fetch
);
516 static int push_refs(struct transport
*transport
,
517 struct ref
*remote_refs
, int flags
)
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
;
526 if (process_connect(transport
, 1)) {
527 do_take_over(transport
);
528 return transport
->push_refs(transport
, remote_refs
, flags
);
532 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
533 "Perhaps you should specify a branch such as 'master'.\n");
537 helper
= get_helper(transport
);
541 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
542 if (!ref
->peer_ref
&& !mirror
)
545 /* Check for statuses set by set_ref_status_for_push() */
546 switch (ref
->status
) {
547 case REF_STATUS_REJECT_NONFASTFORWARD
:
548 case REF_STATUS_UPTODATE
:
557 strbuf_addstr(&buf
, "push ");
558 if (!ref
->deletion
) {
560 strbuf_addch(&buf
, '+');
562 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
564 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
566 strbuf_addch(&buf
, ':');
567 strbuf_addstr(&buf
, ref
->name
);
568 strbuf_addch(&buf
, '\n');
573 transport
->verbose
= flags
& TRANSPORT_PUSH_VERBOSE
? 1 : 0;
574 standard_options(transport
);
576 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
577 if (set_helper_option(transport
, "dry-run", "true") != 0)
578 die("helper %s does not support dry-run", data
->name
);
581 strbuf_addch(&buf
, '\n');
582 sendline(data
, &buf
);
589 recvline(data
, &buf
);
593 if (!prefixcmp(buf
.buf
, "ok ")) {
594 status
= REF_STATUS_OK
;
595 refname
= buf
.buf
+ 3;
596 } else if (!prefixcmp(buf
.buf
, "error ")) {
597 status
= REF_STATUS_REMOTE_REJECT
;
598 refname
= buf
.buf
+ 6;
600 die("expected ok/error, helper said '%s'\n", buf
.buf
);
602 msg
= strchr(refname
, ' ');
604 struct strbuf msg_buf
= STRBUF_INIT
;
608 if (!unquote_c_style(&msg_buf
, msg
, &end
))
609 msg
= strbuf_detach(&msg_buf
, NULL
);
612 strbuf_release(&msg_buf
);
614 if (!strcmp(msg
, "no match")) {
615 status
= REF_STATUS_NONE
;
619 else if (!strcmp(msg
, "up to date")) {
620 status
= REF_STATUS_UPTODATE
;
624 else if (!strcmp(msg
, "non-fast forward")) {
625 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
632 ref
= find_ref_by_name(ref
, refname
);
634 ref
= find_ref_by_name(remote_refs
, refname
);
636 warning("helper reported unexpected status of %s", refname
);
640 if (ref
->status
!= REF_STATUS_NONE
) {
642 * Earlier, the ref was marked not to be pushed, so ignore the ref
643 * status reported by the remote helper if the latter is 'no match'.
645 if (status
== REF_STATUS_NONE
)
649 ref
->status
= status
;
650 ref
->remote_status
= msg
;
652 strbuf_release(&buf
);
656 static int has_attribute(const char *attrs
, const char *attr
) {
663 const char *space
= strchrnul(attrs
, ' ');
664 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
672 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
674 struct helper_data
*data
= transport
->data
;
675 struct child_process
*helper
;
676 struct ref
*ret
= NULL
;
677 struct ref
**tail
= &ret
;
679 struct strbuf buf
= STRBUF_INIT
;
681 helper
= get_helper(transport
);
683 if (process_connect(transport
, for_push
)) {
684 do_take_over(transport
);
685 return transport
->get_refs_list(transport
, for_push
);
688 if (data
->push
&& for_push
)
689 write_str_in_full(helper
->in
, "list for-push\n");
691 write_str_in_full(helper
->in
, "list\n");
695 recvline(data
, &buf
);
700 eov
= strchr(buf
.buf
, ' ');
702 die("Malformed response in ref list: %s", buf
.buf
);
703 eon
= strchr(eov
+ 1, ' ');
707 *tail
= alloc_ref(eov
+ 1);
708 if (buf
.buf
[0] == '@')
709 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
710 else if (buf
.buf
[0] != '?')
711 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
713 if (has_attribute(eon
+ 1, "unchanged")) {
714 (*tail
)->status
|= REF_STATUS_UPTODATE
;
715 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
718 tail
= &((*tail
)->next
);
721 fprintf(stderr
, "Debug: Read ref listing.\n");
722 strbuf_release(&buf
);
724 for (posn
= ret
; posn
; posn
= posn
->next
)
725 resolve_remote_symref(posn
, ret
);
730 int transport_helper_init(struct transport
*transport
, const char *name
)
732 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
735 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
738 transport
->data
= data
;
739 transport
->set_option
= set_helper_option
;
740 transport
->get_refs_list
= get_refs_list
;
741 transport
->fetch
= fetch
;
742 transport
->push_refs
= push_refs
;
743 transport
->disconnect
= release_helper
;
744 transport
->connect
= connect_helper
;
745 transport
->smart_options
= &(data
->transport_options
);