4 #include "run-command.h"
16 struct child_process
*helper
;
22 /* These go from remote name (as in "list") to private name */
23 struct refspec
*refspecs
;
25 /* Transport options for fetch-pack/send-pack (should one of
28 struct git_transport_options transport_options
;
31 static void sendline(struct helper_data
*helper
, struct strbuf
*buffer
)
34 fprintf(stderr
, "Debug: Remote helper: -> %s", buffer
->buf
);
35 if (write_in_full(helper
->helper
->in
, buffer
->buf
, buffer
->len
)
37 die_errno("Full write to remote helper failed");
40 static int recvline(struct helper_data
*helper
, struct strbuf
*buffer
)
44 fprintf(stderr
, "Debug: Remote helper: Waiting...\n");
45 if (strbuf_getline(buffer
, helper
->out
, '\n') == EOF
) {
47 fprintf(stderr
, "Debug: Remote helper quit.\n");
52 fprintf(stderr
, "Debug: Remote helper: <- %s\n", buffer
->buf
);
56 static void xchgline(struct helper_data
*helper
, struct strbuf
*buffer
)
58 sendline(helper
, buffer
);
59 recvline(helper
, buffer
);
62 static void write_constant(int fd
, const char *str
)
65 fprintf(stderr
, "Debug: Remote helper: -> %s", str
);
66 if (write_in_full(fd
, str
, strlen(str
)) != strlen(str
))
67 die_errno("Full write to remote helper failed");
70 const char *remove_ext_force(const char *url
)
73 const char *colon
= strchr(url
, ':');
74 if (colon
&& colon
[1] == ':')
80 static struct child_process
*get_helper(struct transport
*transport
)
82 struct helper_data
*data
= transport
->data
;
83 struct strbuf buf
= STRBUF_INIT
;
84 struct child_process
*helper
;
85 const char **refspecs
= NULL
;
87 int refspec_alloc
= 0;
93 helper
= xcalloc(1, sizeof(*helper
));
97 helper
->argv
= xcalloc(4, sizeof(*helper
->argv
));
98 strbuf_addf(&buf
, "remote-%s", data
->name
);
99 helper
->argv
[0] = strbuf_detach(&buf
, NULL
);
100 helper
->argv
[1] = transport
->remote
->name
;
101 helper
->argv
[2] = remove_ext_force(transport
->url
);
103 if (start_command(helper
))
104 die("Unable to run helper: git %s", helper
->argv
[0]);
105 data
->helper
= helper
;
108 * Open the output as FILE* so strbuf_getline() can be used.
109 * Do this with duped fd because fclose() will close the fd,
110 * and stuff like taking over will require the fd to remain.
113 duped
= dup(helper
->out
);
115 die_errno("Can't dup helper output fd");
116 data
->out
= xfdopen(duped
, "r");
118 write_constant(helper
->in
, "capabilities\n");
123 recvline(data
, &buf
);
128 if (*buf
.buf
== '*') {
129 capname
= buf
.buf
+ 1;
135 fprintf(stderr
, "Debug: Got cap %s\n", capname
);
136 if (!strcmp(capname
, "fetch"))
138 else if (!strcmp(capname
, "option"))
140 else if (!strcmp(capname
, "push"))
142 else if (!strcmp(capname
, "import"))
144 else if (!data
->refspecs
&& !prefixcmp(capname
, "refspec ")) {
148 refspecs
[refspec_nr
++] = strdup(buf
.buf
+ strlen("refspec "));
149 } else if (mandatory
) {
150 die("Unknown madatory capability %s. This remote "
151 "helper probably needs newer version of Git.\n",
157 data
->refspec_nr
= refspec_nr
;
158 data
->refspecs
= parse_fetch_refspec(refspec_nr
, refspecs
);
159 for (i
= 0; i
< refspec_nr
; i
++) {
160 free((char *)refspecs
[i
]);
164 strbuf_release(&buf
);
166 fprintf(stderr
, "Debug: Capabilities complete.\n");
170 static int disconnect_helper(struct transport
*transport
)
172 struct helper_data
*data
= transport
->data
;
173 struct strbuf buf
= STRBUF_INIT
;
177 fprintf(stderr
, "Debug: Disconnecting.\n");
178 strbuf_addf(&buf
, "\n");
179 sendline(data
, &buf
);
180 close(data
->helper
->in
);
181 close(data
->helper
->out
);
183 finish_command(data
->helper
);
184 free((char *)data
->helper
->argv
[0]);
185 free(data
->helper
->argv
);
192 static const char *unsupported_options
[] = {
193 TRANS_OPT_UPLOADPACK
,
194 TRANS_OPT_RECEIVEPACK
,
198 static const char *boolean_options
[] = {
204 static int set_helper_option(struct transport
*transport
,
205 const char *name
, const char *value
)
207 struct helper_data
*data
= transport
->data
;
208 struct strbuf buf
= STRBUF_INIT
;
209 int i
, ret
, is_bool
= 0;
211 get_helper(transport
);
216 for (i
= 0; i
< ARRAY_SIZE(unsupported_options
); i
++) {
217 if (!strcmp(name
, unsupported_options
[i
]))
221 for (i
= 0; i
< ARRAY_SIZE(boolean_options
); i
++) {
222 if (!strcmp(name
, boolean_options
[i
])) {
228 strbuf_addf(&buf
, "option %s ", name
);
230 strbuf_addstr(&buf
, value
? "true" : "false");
232 quote_c_style(value
, &buf
, NULL
, 0);
233 strbuf_addch(&buf
, '\n');
235 xchgline(data
, &buf
);
237 if (!strcmp(buf
.buf
, "ok"))
239 else if (!prefixcmp(buf
.buf
, "error")) {
241 } else if (!strcmp(buf
.buf
, "unsupported"))
244 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
247 strbuf_release(&buf
);
251 static void standard_options(struct transport
*t
)
256 int no_progress
= v
< 0 || (!t
->progress
&& !isatty(1));
258 set_helper_option(t
, "progress", !no_progress
? "true" : "false");
260 n
= snprintf(buf
, sizeof(buf
), "%d", v
+ 1);
261 if (n
>= sizeof(buf
))
262 die("impossibly large verbosity value");
263 set_helper_option(t
, "verbosity", buf
);
266 static int release_helper(struct transport
*transport
)
268 struct helper_data
*data
= transport
->data
;
269 free_refspec(data
->refspec_nr
, data
->refspecs
);
270 data
->refspecs
= NULL
;
271 disconnect_helper(transport
);
272 free(transport
->data
);
276 static int fetch_with_fetch(struct transport
*transport
,
277 int nr_heads
, struct ref
**to_fetch
)
279 struct helper_data
*data
= transport
->data
;
281 struct strbuf buf
= STRBUF_INIT
;
283 standard_options(transport
);
285 for (i
= 0; i
< nr_heads
; i
++) {
286 const struct ref
*posn
= to_fetch
[i
];
287 if (posn
->status
& REF_STATUS_UPTODATE
)
290 strbuf_addf(&buf
, "fetch %s %s\n",
291 sha1_to_hex(posn
->old_sha1
), posn
->name
);
294 strbuf_addch(&buf
, '\n');
295 sendline(data
, &buf
);
298 recvline(data
, &buf
);
300 if (!prefixcmp(buf
.buf
, "lock ")) {
301 const char *name
= buf
.buf
+ 5;
302 if (transport
->pack_lockfile
)
303 warning("%s also locked %s", data
->name
, name
);
305 transport
->pack_lockfile
= xstrdup(name
);
310 warning("%s unexpectedly said: '%s'", data
->name
, buf
.buf
);
312 strbuf_release(&buf
);
316 static int get_importer(struct transport
*transport
, struct child_process
*fastimport
)
318 struct child_process
*helper
= get_helper(transport
);
319 memset(fastimport
, 0, sizeof(*fastimport
));
320 fastimport
->in
= helper
->out
;
321 fastimport
->argv
= xcalloc(5, sizeof(*fastimport
->argv
));
322 fastimport
->argv
[0] = "fast-import";
323 fastimport
->argv
[1] = "--quiet";
325 fastimport
->git_cmd
= 1;
326 return start_command(fastimport
);
329 static int fetch_with_import(struct transport
*transport
,
330 int nr_heads
, struct ref
**to_fetch
)
332 struct child_process fastimport
;
333 struct helper_data
*data
= transport
->data
;
336 struct strbuf buf
= STRBUF_INIT
;
338 get_helper(transport
);
340 if (get_importer(transport
, &fastimport
))
341 die("Couldn't run fast-import");
343 for (i
= 0; i
< nr_heads
; i
++) {
345 if (posn
->status
& REF_STATUS_UPTODATE
)
348 strbuf_addf(&buf
, "import %s\n", posn
->name
);
349 sendline(data
, &buf
);
352 disconnect_helper(transport
);
353 finish_command(&fastimport
);
354 free(fastimport
.argv
);
355 fastimport
.argv
= NULL
;
357 for (i
= 0; i
< nr_heads
; i
++) {
360 if (posn
->status
& REF_STATUS_UPTODATE
)
363 private = apply_refspecs(data
->refspecs
, data
->refspec_nr
, posn
->name
);
365 private = strdup(posn
->name
);
366 read_ref(private, posn
->old_sha1
);
369 strbuf_release(&buf
);
373 static int fetch(struct transport
*transport
,
374 int nr_heads
, struct ref
**to_fetch
)
376 struct helper_data
*data
= transport
->data
;
380 for (i
= 0; i
< nr_heads
; i
++)
381 if (!(to_fetch
[i
]->status
& REF_STATUS_UPTODATE
))
388 return fetch_with_fetch(transport
, nr_heads
, to_fetch
);
391 return fetch_with_import(transport
, nr_heads
, to_fetch
);
396 static int push_refs(struct transport
*transport
,
397 struct ref
*remote_refs
, int flags
)
399 int force_all
= flags
& TRANSPORT_PUSH_FORCE
;
400 int mirror
= flags
& TRANSPORT_PUSH_MIRROR
;
401 struct helper_data
*data
= transport
->data
;
402 struct strbuf buf
= STRBUF_INIT
;
403 struct child_process
*helper
;
409 helper
= get_helper(transport
);
413 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
415 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
419 ref
->deletion
= is_null_sha1(ref
->new_sha1
);
420 if (!ref
->deletion
&&
421 !hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
422 ref
->status
= REF_STATUS_UPTODATE
;
429 strbuf_addstr(&buf
, "push ");
430 if (!ref
->deletion
) {
432 strbuf_addch(&buf
, '+');
434 strbuf_addstr(&buf
, ref
->peer_ref
->name
);
436 strbuf_addstr(&buf
, sha1_to_hex(ref
->new_sha1
));
438 strbuf_addch(&buf
, ':');
439 strbuf_addstr(&buf
, ref
->name
);
440 strbuf_addch(&buf
, '\n');
445 transport
->verbose
= flags
& TRANSPORT_PUSH_VERBOSE
? 1 : 0;
446 standard_options(transport
);
448 if (flags
& TRANSPORT_PUSH_DRY_RUN
) {
449 if (set_helper_option(transport
, "dry-run", "true") != 0)
450 die("helper %s does not support dry-run", data
->name
);
453 strbuf_addch(&buf
, '\n');
454 sendline(data
, &buf
);
461 recvline(data
, &buf
);
465 if (!prefixcmp(buf
.buf
, "ok ")) {
466 status
= REF_STATUS_OK
;
467 refname
= buf
.buf
+ 3;
468 } else if (!prefixcmp(buf
.buf
, "error ")) {
469 status
= REF_STATUS_REMOTE_REJECT
;
470 refname
= buf
.buf
+ 6;
472 die("expected ok/error, helper said '%s'\n", buf
.buf
);
474 msg
= strchr(refname
, ' ');
476 struct strbuf msg_buf
= STRBUF_INIT
;
480 if (!unquote_c_style(&msg_buf
, msg
, &end
))
481 msg
= strbuf_detach(&msg_buf
, NULL
);
484 strbuf_release(&msg_buf
);
486 if (!strcmp(msg
, "no match")) {
487 status
= REF_STATUS_NONE
;
491 else if (!strcmp(msg
, "up to date")) {
492 status
= REF_STATUS_UPTODATE
;
496 else if (!strcmp(msg
, "non-fast forward")) {
497 status
= REF_STATUS_REJECT_NONFASTFORWARD
;
504 ref
= find_ref_by_name(ref
, refname
);
506 ref
= find_ref_by_name(remote_refs
, refname
);
508 warning("helper reported unexpected status of %s", refname
);
512 ref
->status
= status
;
513 ref
->remote_status
= msg
;
515 strbuf_release(&buf
);
519 static int has_attribute(const char *attrs
, const char *attr
) {
526 const char *space
= strchrnul(attrs
, ' ');
527 if (len
== space
- attrs
&& !strncmp(attrs
, attr
, len
))
535 static struct ref
*get_refs_list(struct transport
*transport
, int for_push
)
537 struct helper_data
*data
= transport
->data
;
538 struct child_process
*helper
;
539 struct ref
*ret
= NULL
;
540 struct ref
**tail
= &ret
;
542 struct strbuf buf
= STRBUF_INIT
;
544 helper
= get_helper(transport
);
546 if (data
->push
&& for_push
)
547 write_str_in_full(helper
->in
, "list for-push\n");
549 write_str_in_full(helper
->in
, "list\n");
553 recvline(data
, &buf
);
558 eov
= strchr(buf
.buf
, ' ');
560 die("Malformed response in ref list: %s", buf
.buf
);
561 eon
= strchr(eov
+ 1, ' ');
565 *tail
= alloc_ref(eov
+ 1);
566 if (buf
.buf
[0] == '@')
567 (*tail
)->symref
= xstrdup(buf
.buf
+ 1);
568 else if (buf
.buf
[0] != '?')
569 get_sha1_hex(buf
.buf
, (*tail
)->old_sha1
);
571 if (has_attribute(eon
+ 1, "unchanged")) {
572 (*tail
)->status
|= REF_STATUS_UPTODATE
;
573 read_ref((*tail
)->name
, (*tail
)->old_sha1
);
576 tail
= &((*tail
)->next
);
579 fprintf(stderr
, "Debug: Read ref listing.\n");
580 strbuf_release(&buf
);
582 for (posn
= ret
; posn
; posn
= posn
->next
)
583 resolve_remote_symref(posn
, ret
);
588 int transport_helper_init(struct transport
*transport
, const char *name
)
590 struct helper_data
*data
= xcalloc(sizeof(*data
), 1);
593 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
596 transport
->data
= data
;
597 transport
->set_option
= set_helper_option
;
598 transport
->get_refs_list
= get_refs_list
;
599 transport
->fetch
= fetch
;
600 transport
->push_refs
= push_refs
;
601 transport
->disconnect
= release_helper
;
602 transport
->smart_options
= &(data
->transport_options
);