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(1));
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
);
534 helper
= get_helper(transport
);
538 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
540 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
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
;
554 strbuf_addstr(&buf
, "push ");
555 if (!ref
->deletion
) {
557 strbuf_addch(&buf
, '+');
559 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
561 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
563 strbuf_addch(&buf
, ':');
564 strbuf_addstr(&buf
, ref
->name
);
565 strbuf_addch(&buf
, '\n');
570 transport
->verbose
= flags
& TRANSPORT_PUSH_VERBOSE
? 1 : 0;
571 standard_options(transport
);
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
);
578 strbuf_addch(&buf
, '\n');
579 sendline(data
, &buf
);
586 recvline(data
, &buf
);
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;
597 die("expected ok/error, helper said '%s'\n", buf
.buf
);
599 msg
= strchr(refname
, ' ');
601 struct strbuf msg_buf
= STRBUF_INIT
;
605 if (!unquote_c_style(&msg_buf
, msg
, &end
))
606 msg
= strbuf_detach(&msg_buf
, NULL
);
609 strbuf_release(&msg_buf
);
611 if (!strcmp(msg
, "no match")) {
612 status
= REF_STATUS_NONE
;
616 else if (!strcmp(msg
, "up to date")) {
617 status
= REF_STATUS_UPTODATE
;
621 else if (!strcmp(msg
, "non-fast forward")) {
622 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
629 ref
= find_ref_by_name(ref
, refname
);
631 ref
= find_ref_by_name(remote_refs
, refname
);
633 warning("helper reported unexpected status of %s", refname
);
637 ref
->status
= status
;
638 ref
->remote_status
= msg
;
640 strbuf_release(&buf
);
644 static int has_attribute(const char *attrs
, const char *attr
) {
651 const char *space
= strchrnul(attrs
, ' ');
652 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
660 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
662 struct helper_data
*data
= transport
->data
;
663 struct child_process
*helper
;
664 struct ref
*ret
= NULL
;
665 struct ref
**tail
= &ret
;
667 struct strbuf buf
= STRBUF_INIT
;
669 helper
= get_helper(transport
);
671 if (process_connect(transport
, for_push
)) {
672 do_take_over(transport
);
673 return transport
->get_refs_list(transport
, for_push
);
676 if (data
->push
&& for_push
)
677 write_str_in_full(helper
->in
, "list for-push\n");
679 write_str_in_full(helper
->in
, "list\n");
683 recvline(data
, &buf
);
688 eov
= strchr(buf
.buf
, ' ');
690 die("Malformed response in ref list: %s", buf
.buf
);
691 eon
= strchr(eov
+ 1, ' ');
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
);
701 if (has_attribute(eon
+ 1, "unchanged")) {
702 (*tail
)->status
|= REF_STATUS_UPTODATE
;
703 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
706 tail
= &((*tail
)->next
);
709 fprintf(stderr
, "Debug: Read ref listing.\n");
710 strbuf_release(&buf
);
712 for (posn
= ret
; posn
; posn
= posn
->next
)
713 resolve_remote_symref(posn
, ret
);
718 int transport_helper_init(struct transport
*transport
, const char *name
)
720 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
723 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
726 transport
->data
= data
;
727 transport
->set_option
= set_helper_option
;
728 transport
->get_refs_list
= get_refs_list
;
729 transport
->fetch
= fetch
;
730 transport
->push_refs
= push_refs
;
731 transport
->disconnect
= release_helper
;
732 transport
->connect
= connect_helper
;
733 transport
->smart_options
= &(data
->transport_options
);