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 madatory 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
)
282 int no_progress
= v
< 0 || (!t
->progress
&& !isatty(2));
284 set_helper_option(t
, "progress", !no_progress
? "true" : "false");
286 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
287 if (n
>= sizeof(buf
))
288 die("impossibly large verbosity value");
289 set_helper_option(t
, "verbosity", buf
);
292 static int release_helper(struct transport
*transport
)
294 struct helper_data
*data
= transport
->data
;
295 free_refspec(data
->refspec_nr
, data
->refspecs
);
296 data
->refspecs
= NULL
;
297 disconnect_helper(transport
);
298 free(transport
->data
);
302 static int fetch_with_fetch(struct transport
*transport
,
303 int nr_heads
, struct ref
**to_fetch
)
305 struct helper_data
*data
= transport
->data
;
307 struct strbuf buf
= STRBUF_INIT
;
309 standard_options(transport
);
311 for (i
= 0; i
< nr_heads
; i
++) {
312 const struct ref
*posn
= to_fetch
[i
];
313 if (posn
->status
& REF_STATUS_UPTODATE
)
316 strbuf_addf(&buf
, "fetch %s %s\n",
317 sha1_to_hex(posn
->old_sha1
), posn
->name
);
320 strbuf_addch(&buf
, '\n');
321 sendline(data
, &buf
);
324 recvline(data
, &buf
);
326 if (!prefixcmp(buf
.buf
, "lock ")) {
327 const char *name
= buf
.buf
+ 5;
328 if (transport
->pack_lockfile
)
329 warning("%s also locked %s", data
->name
, name
);
331 transport
->pack_lockfile
= xstrdup(name
);
336 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
338 strbuf_release(&buf
);
342 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
344 struct child_process
*helper
= get_helper(transport
);
345 memset(fastimport
, 0, sizeof(*fastimport
));
346 fastimport
->in
= helper
->out
;
347 fastimport
->argv
= xcalloc(5, sizeof(*fastimport
->argv
));
348 fastimport
->argv
[0] = "fast-import";
349 fastimport
->argv
[1] = "--quiet";
351 fastimport
->git_cmd
= 1;
352 return start_command(fastimport
);
355 static int fetch_with_import(struct transport
*transport
,
356 int nr_heads
, struct ref
**to_fetch
)
358 struct child_process fastimport
;
359 struct helper_data
*data
= transport
->data
;
362 struct strbuf buf
= STRBUF_INIT
;
364 get_helper(transport
);
366 if (get_importer(transport
, &fastimport
))
367 die("Couldn't run fast-import");
369 for (i
= 0; i
< nr_heads
; i
++) {
371 if (posn
->status
& REF_STATUS_UPTODATE
)
374 strbuf_addf(&buf
, "import %s\n", posn
->name
);
375 sendline(data
, &buf
);
378 disconnect_helper(transport
);
379 finish_command(&fastimport
);
380 free(fastimport
.argv
);
381 fastimport
.argv
= NULL
;
383 for (i
= 0; i
< nr_heads
; i
++) {
386 if (posn
->status
& REF_STATUS_UPTODATE
)
389 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
391 private = strdup(posn
->name
);
392 read_ref(private, posn
->old_sha1
);
395 strbuf_release(&buf
);
399 static int process_connect_service(struct transport
*transport
,
400 const char *name
, const char *exec
)
402 struct helper_data
*data
= transport
->data
;
403 struct strbuf cmdbuf
= STRBUF_INIT
;
404 struct child_process
*helper
;
405 int r
, duped
, ret
= 0;
408 helper
= get_helper(transport
);
411 * Yes, dup the pipe another time, as we need unbuffered version
412 * of input pipe as FILE*. fclose() closes the underlying fd and
413 * stream buffering only can be changed before first I/O operation
416 duped
= dup(helper
->out
);
418 die_errno("Can't dup helper output fd");
419 input
= xfdopen(duped
, "r");
420 setvbuf(input
, NULL
, _IONBF
, 0);
423 * Handle --upload-pack and friends. This is fire and forget...
424 * just warn if it fails.
426 if (strcmp(name
, exec
)) {
427 r
= set_helper_option(transport
, "servpath", exec
);
429 warning("Setting remote service path not supported by protocol.");
431 warning("Invalid remote service path.");
435 strbuf_addf(&cmdbuf
, "connect %s\n", name
);
439 sendline(data
, &cmdbuf
);
440 recvline_fh(input
, &cmdbuf
);
441 if (!strcmp(cmdbuf
.buf
, "")) {
442 data
->no_disconnect_req
= 1;
444 fprintf(stderr
, "Debug: Smart transport connection "
447 } else if (!strcmp(cmdbuf
.buf
, "fallback")) {
449 fprintf(stderr
, "Debug: Falling back to dumb "
452 die("Unknown response to connect: %s",
460 static int process_connect(struct transport
*transport
,
463 struct helper_data
*data
= transport
->data
;
467 name
= for_push
? "git-receive-pack" : "git-upload-pack";
469 exec
= data
->transport_options
.receivepack
;
471 exec
= data
->transport_options
.uploadpack
;
473 return process_connect_service(transport
, name
, exec
);
476 static int connect_helper(struct transport
*transport
, const char *name
,
477 const char *exec
, int fd
[2])
479 struct helper_data
*data
= transport
->data
;
481 /* Get_helper so connect is inited. */
482 get_helper(transport
);
484 die("Operation not supported by protocol.");
486 if (!process_connect_service(transport
, name
, exec
))
487 die("Can't connect to subservice %s.", name
);
489 fd
[0] = data
->helper
->out
;
490 fd
[1] = data
->helper
->in
;
494 static int fetch(struct transport
*transport
,
495 int nr_heads
, struct ref
**to_fetch
)
497 struct helper_data
*data
= transport
->data
;
500 if (process_connect(transport
, 0)) {
501 do_take_over(transport
);
502 return transport
->fetch(transport
, nr_heads
, to_fetch
);
506 for (i
= 0; i
< nr_heads
; i
++)
507 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
514 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
517 return fetch_with_import(transport
, nr_heads
, to_fetch
);
522 static int push_refs(struct transport
*transport
,
523 struct ref
*remote_refs
, int flags
)
525 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
526 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
527 struct helper_data
*data
= transport
->data
;
528 struct strbuf buf
= STRBUF_INIT
;
529 struct child_process
*helper
;
532 if (process_connect(transport
, 1)) {
533 do_take_over(transport
);
534 return transport
->push_refs(transport
, remote_refs
, flags
);
538 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n"
539 "Perhaps you should specify a branch such as 'master'.\n");
543 helper
= get_helper(transport
);
547 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
548 if (!ref
->peer_ref
&& !mirror
)
551 /* Check for statuses set by set_ref_status_for_push() */
552 switch (ref
->status
) {
553 case REF_STATUS_REJECT_NONFASTFORWARD
:
554 case REF_STATUS_UPTODATE
:
563 strbuf_addstr(&buf
, "push ");
564 if (!ref
->deletion
) {
566 strbuf_addch(&buf
, '+');
568 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
570 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
572 strbuf_addch(&buf
, ':');
573 strbuf_addstr(&buf
, ref
->name
);
574 strbuf_addch(&buf
, '\n');
579 transport
->verbose
= flags
& TRANSPORT_PUSH_VERBOSE
? 1 : 0;
580 standard_options(transport
);
582 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
583 if (set_helper_option(transport
, "dry-run", "true") != 0)
584 die("helper %s does not support dry-run", data
->name
);
587 strbuf_addch(&buf
, '\n');
588 sendline(data
, &buf
);
595 recvline(data
, &buf
);
599 if (!prefixcmp(buf
.buf
, "ok ")) {
600 status
= REF_STATUS_OK
;
601 refname
= buf
.buf
+ 3;
602 } else if (!prefixcmp(buf
.buf
, "error ")) {
603 status
= REF_STATUS_REMOTE_REJECT
;
604 refname
= buf
.buf
+ 6;
606 die("expected ok/error, helper said '%s'\n", buf
.buf
);
608 msg
= strchr(refname
, ' ');
610 struct strbuf msg_buf
= STRBUF_INIT
;
614 if (!unquote_c_style(&msg_buf
, msg
, &end
))
615 msg
= strbuf_detach(&msg_buf
, NULL
);
618 strbuf_release(&msg_buf
);
620 if (!strcmp(msg
, "no match")) {
621 status
= REF_STATUS_NONE
;
625 else if (!strcmp(msg
, "up to date")) {
626 status
= REF_STATUS_UPTODATE
;
630 else if (!strcmp(msg
, "non-fast forward")) {
631 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
638 ref
= find_ref_by_name(ref
, refname
);
640 ref
= find_ref_by_name(remote_refs
, refname
);
642 warning("helper reported unexpected status of %s", refname
);
646 if (ref
->status
!= REF_STATUS_NONE
) {
648 * Earlier, the ref was marked not to be pushed, so ignore the ref
649 * status reported by the remote helper if the latter is 'no match'.
651 if (status
== REF_STATUS_NONE
)
655 ref
->status
= status
;
656 ref
->remote_status
= msg
;
658 strbuf_release(&buf
);
662 static int has_attribute(const char *attrs
, const char *attr
) {
669 const char *space
= strchrnul(attrs
, ' ');
670 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
678 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
680 struct helper_data
*data
= transport
->data
;
681 struct child_process
*helper
;
682 struct ref
*ret
= NULL
;
683 struct ref
**tail
= &ret
;
685 struct strbuf buf
= STRBUF_INIT
;
687 helper
= get_helper(transport
);
689 if (process_connect(transport
, for_push
)) {
690 do_take_over(transport
);
691 return transport
->get_refs_list(transport
, for_push
);
694 if (data
->push
&& for_push
)
695 write_str_in_full(helper
->in
, "list for-push\n");
697 write_str_in_full(helper
->in
, "list\n");
701 recvline(data
, &buf
);
706 eov
= strchr(buf
.buf
, ' ');
708 die("Malformed response in ref list: %s", buf
.buf
);
709 eon
= strchr(eov
+ 1, ' ');
713 *tail
= alloc_ref(eov
+ 1);
714 if (buf
.buf
[0] == '@')
715 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
716 else if (buf
.buf
[0] != '?')
717 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
719 if (has_attribute(eon
+ 1, "unchanged")) {
720 (*tail
)->status
|= REF_STATUS_UPTODATE
;
721 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
724 tail
= &((*tail
)->next
);
727 fprintf(stderr
, "Debug: Read ref listing.\n");
728 strbuf_release(&buf
);
730 for (posn
= ret
; posn
; posn
= posn
->next
)
731 resolve_remote_symref(posn
, ret
);
736 int transport_helper_init(struct transport
*transport
, const char *name
)
738 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
741 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
744 transport
->data
= data
;
745 transport
->set_option
= set_helper_option
;
746 transport
->get_refs_list
= get_refs_list
;
747 transport
->fetch
= fetch
;
748 transport
->push_refs
= push_refs
;
749 transport
->disconnect
= release_helper
;
750 transport
->connect
= connect_helper
;
751 transport
->smart_options
= &(data
->transport_options
);