blob: 20a7185ec40e1cf4612c259019f325208632373b [file] [log] [blame]
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001#include "cache.h"
2#include "transport.h"
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07003#include "quote.h"
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04004#include "run-command.h"
5#include "commit.h"
6#include "diff.h"
7#include "revision.h"
Daniel Barkalow72ff8942009-11-18 02:42:28 +01008#include "remote.h"
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05009#include "string-list.h"
Ilari Liusvaara7851b1e2010-11-17 09:15:34 -080010#include "thread-utils.h"
Jeff Kingc34fe632012-02-23 05:04:34 -050011#include "sigchain.h"
Florian Achleitnerbfc366d2012-09-19 17:21:19 +020012#include "argv-array.h"
Felipe Contreras664059f2013-04-17 23:14:33 -050013#include "refs.h"
Brandon Williamsec0cb492018-05-16 15:57:48 -070014#include "refspec.h"
Jonathan Tane967ca32017-12-14 13:44:45 -080015#include "transport-internal.h"
Brandon Williamsedc9caf2018-03-15 10:31:34 -070016#include "protocol.h"
Ilari Liusvaara7851b1e2010-11-17 09:15:34 -080017
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020018static int debug;
19
Jonathan Nieder9cba13c2011-03-16 02:08:34 -050020struct helper_data {
Daniel Barkalow6eb996b2009-08-05 01:01:53 -040021 const char *name;
22 struct child_process *helper;
Shawn O. Pearce292ce462009-10-30 17:47:28 -070023 FILE *out;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070024 unsigned fetch : 1,
Junio C Hamanoa24a32d2009-12-06 22:40:16 -080025 import : 1,
Florian Achleitnerbfc366d2012-09-19 17:21:19 +020026 bidi_import : 1,
Sverre Rabbelier73b49a72010-03-29 11:48:27 -050027 export : 1,
Shawn O. Pearceae4efe12009-10-30 17:47:30 -070028 option : 1,
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +020029 push : 1,
30 connect : 1,
Brandon Williamsedc9caf2018-03-15 10:31:34 -070031 stateless_connect : 1,
John Keeping0d957a42013-04-14 11:57:08 +010032 signed_tags : 1,
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +070033 check_connectivity : 1,
Matthieu Moy597b8312013-09-03 17:45:14 +020034 no_disconnect_req : 1,
35 no_private_update : 1;
Jonathan Tanac3fda82019-08-21 15:20:09 -070036
37 /*
38 * As an optimization, the transport code may invoke fetch before
39 * get_refs_list. If this happens, and if the transport helper doesn't
40 * support connect or stateless_connect, we need to invoke
41 * get_refs_list ourselves if we haven't already done so. Keep track of
42 * whether we have invoked get_refs_list.
43 */
44 unsigned get_refs_list_called : 1;
45
Sverre Rabbeliera515ebe2011-07-16 15:03:40 +020046 char *export_marks;
47 char *import_marks;
Daniel Barkalow72ff8942009-11-18 02:42:28 +010048 /* These go from remote name (as in "list") to private name */
Brandon Williams57f32ac2018-05-16 15:58:03 -070049 struct refspec rs;
Ilari Liusvaara61b075b2009-12-09 17:26:31 +020050 /* Transport options for fetch-pack/send-pack (should one of
51 * those be invoked).
52 */
53 struct git_transport_options transport_options;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -040054};
55
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020056static void sendline(struct helper_data *helper, struct strbuf *buffer)
57{
58 if (debug)
59 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
Jeff King06f46f22017-09-13 13:16:03 -040060 if (write_in_full(helper->helper->in, buffer->buf, buffer->len) < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +020061 die_errno(_("full write to remote helper failed"));
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020062}
63
Brandon Williamsb1c2edf2018-03-15 10:31:32 -070064static int recvline_fh(FILE *helper, struct strbuf *buffer)
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020065{
66 strbuf_reset(buffer);
67 if (debug)
68 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
Junio C Hamano692dfdf2015-10-28 13:36:00 -070069 if (strbuf_getline(buffer, helper) == EOF) {
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020070 if (debug)
71 fprintf(stderr, "Debug: Remote helper quit.\n");
Felipe Contreras5931b332014-04-12 15:33:29 -050072 return 1;
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020073 }
74
75 if (debug)
76 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
77 return 0;
78}
79
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +020080static int recvline(struct helper_data *helper, struct strbuf *buffer)
81{
Brandon Williamsb1c2edf2018-03-15 10:31:32 -070082 return recvline_fh(helper->out, buffer);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +020083}
84
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020085static void write_constant(int fd, const char *str)
86{
87 if (debug)
88 fprintf(stderr, "Debug: Remote helper: -> %s", str);
Jeff King06f46f22017-09-13 13:16:03 -040089 if (write_in_full(fd, str, strlen(str)) < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +020090 die_errno(_("full write to remote helper failed"));
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020091}
92
Stephen Boydc2e86ad2011-03-22 00:51:05 -070093static const char *remove_ext_force(const char *url)
Ilari Liusvaara25d5cc42009-12-09 17:26:29 +020094{
95 if (url) {
96 const char *colon = strchr(url, ':');
97 if (colon && colon[1] == ':')
98 return colon + 2;
99 }
100 return url;
101}
102
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200103static void do_take_over(struct transport *transport)
104{
105 struct helper_data *data;
106 data = (struct helper_data *)transport->data;
107 transport_take_over(transport, data->helper);
108 fclose(data->out);
109 free(data);
110}
111
Mike Hommey2879bc32015-02-13 14:24:45 +0900112static void standard_options(struct transport *t);
113
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400114static struct child_process *get_helper(struct transport *transport)
115{
116 struct helper_data *data = transport->data;
117 struct strbuf buf = STRBUF_INIT;
118 struct child_process *helper;
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200119 int duped;
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100120 int code;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400121
122 if (data->helper)
123 return data->helper;
124
René Scharfe483bbd42014-08-19 21:10:48 +0200125 helper = xmalloc(sizeof(*helper));
126 child_process_init(helper);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400127 helper->in = -1;
128 helper->out = -1;
129 helper->err = 0;
Jeff Kinge0ab2ac2014-05-15 04:34:18 -0400130 argv_array_pushf(&helper->args, "git-remote-%s", data->name);
131 argv_array_push(&helper->args, transport->remote->name);
132 argv_array_push(&helper->args, remove_ext_force(transport->url));
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100133 helper->git_cmd = 0;
134 helper->silent_exec_failure = 1;
Dmitry Ivankove1735872011-07-16 15:03:28 +0200135
Jonathan Nieder4b0c3c72017-02-14 15:36:19 -0500136 if (have_git_dir())
137 argv_array_pushf(&helper->env_array, "%s=%s",
138 GIT_DIR_ENVIRONMENT, get_git_dir());
Dmitry Ivankove1735872011-07-16 15:03:28 +0200139
Jeff Hostetlerabd81a32019-02-22 14:25:05 -0800140 helper->trace2_child_class = helper->args.argv[0]; /* "remote-<name>" */
141
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100142 code = start_command(helper);
143 if (code < 0 && errno == ENOENT)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200144 die(_("unable to find remote helper for '%s'"), data->name);
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100145 else if (code != 0)
146 exit(code);
147
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400148 data->helper = helper;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200149 data->no_disconnect_req = 0;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700150 refspec_init(&data->rs, REFSPEC_FETCH);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400151
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200152 /*
Junio C Hamano1a0c8df2016-01-13 18:32:23 -0800153 * Open the output as FILE* so strbuf_getline_*() family of
154 * functions can be used.
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200155 * Do this with duped fd because fclose() will close the fd,
156 * and stuff like taking over will require the fd to remain.
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200157 */
158 duped = dup(helper->out);
159 if (duped < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200160 die_errno(_("can't dup helper output fd"));
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200161 data->out = xfdopen(duped, "r");
162
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200163 write_constant(helper->in, "capabilities\n");
Daniel Barkalow2d14d652009-09-03 22:13:51 -0400164
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400165 while (1) {
Jeff King21a2d4a2014-06-18 15:47:17 -0400166 const char *capname, *arg;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200167 int mandatory = 0;
Felipe Contreras5931b332014-04-12 15:33:29 -0500168 if (recvline(data, &buf))
169 exit(128);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400170
171 if (!*buf.buf)
172 break;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200173
174 if (*buf.buf == '*') {
175 capname = buf.buf + 1;
176 mandatory = 1;
177 } else
178 capname = buf.buf;
179
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200180 if (debug)
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200181 fprintf(stderr, "Debug: Got cap %s\n", capname);
182 if (!strcmp(capname, "fetch"))
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400183 data->fetch = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200184 else if (!strcmp(capname, "option"))
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700185 data->option = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200186 else if (!strcmp(capname, "push"))
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700187 data->push = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200188 else if (!strcmp(capname, "import"))
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100189 data->import = 1;
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200190 else if (!strcmp(capname, "bidi-import"))
191 data->bidi_import = 1;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500192 else if (!strcmp(capname, "export"))
193 data->export = 1;
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700194 else if (!strcmp(capname, "check-connectivity"))
195 data->check_connectivity = 1;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700196 else if (skip_prefix(capname, "refspec ", &arg)) {
197 refspec_append(&data->rs, arg);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200198 } else if (!strcmp(capname, "connect")) {
199 data->connect = 1;
Brandon Williamsedc9caf2018-03-15 10:31:34 -0700200 } else if (!strcmp(capname, "stateless-connect")) {
201 data->stateless_connect = 1;
John Keeping0d957a42013-04-14 11:57:08 +0100202 } else if (!strcmp(capname, "signed-tags")) {
203 data->signed_tags = 1;
Jeff King21a2d4a2014-06-18 15:47:17 -0400204 } else if (skip_prefix(capname, "export-marks ", &arg)) {
205 data->export_marks = xstrdup(arg);
206 } else if (skip_prefix(capname, "import-marks ", &arg)) {
207 data->import_marks = xstrdup(arg);
Christian Couder59556542013-11-30 21:55:40 +0100208 } else if (starts_with(capname, "no-private-update")) {
Matthieu Moy597b8312013-09-03 17:45:14 +0200209 data->no_private_update = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200210 } else if (mandatory) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200211 die(_("unknown mandatory capability %s; this remote "
212 "helper probably needs newer version of Git"),
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200213 capname);
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100214 }
215 }
Brandon Williams57f32ac2018-05-16 15:58:03 -0700216 if (!data->rs.nr && (data->import || data->bidi_import || data->export)) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200217 warning(_("this remote helper should implement refspec capability"));
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400218 }
Sverre Rabbelierb962dbd2009-11-18 02:42:29 +0100219 strbuf_release(&buf);
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200220 if (debug)
221 fprintf(stderr, "Debug: Capabilities complete.\n");
Mike Hommey2879bc32015-02-13 14:24:45 +0900222 standard_options(transport);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400223 return data->helper;
224}
225
226static int disconnect_helper(struct transport *transport)
227{
228 struct helper_data *data = transport->data;
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200229 int res = 0;
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200230
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400231 if (data->helper) {
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200232 if (debug)
233 fprintf(stderr, "Debug: Disconnecting.\n");
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200234 if (!data->no_disconnect_req) {
Jeff Kingc34fe632012-02-23 05:04:34 -0500235 /*
236 * Ignore write errors; there's nothing we can do,
237 * since we're about to close the pipe anyway. And the
238 * most likely error is EPIPE due to the helper dying
239 * to report an error itself.
240 */
241 sigchain_push(SIGPIPE, SIG_IGN);
242 xwrite(data->helper->in, "\n", 1);
243 sigchain_pop(SIGPIPE);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200244 }
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400245 close(data->helper->in);
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200246 close(data->helper->out);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700247 fclose(data->out);
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200248 res = finish_command(data->helper);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000249 FREE_AND_NULL(data->helper);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400250 }
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200251 return res;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400252}
253
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700254static const char *unsupported_options[] = {
255 TRANS_OPT_UPLOADPACK,
256 TRANS_OPT_RECEIVEPACK,
257 TRANS_OPT_THIN,
258 TRANS_OPT_KEEP
259 };
Felipe Contreras23cd01e2013-10-31 03:25:40 -0600260
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700261static const char *boolean_options[] = {
262 TRANS_OPT_THIN,
263 TRANS_OPT_KEEP,
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700264 TRANS_OPT_FOLLOWTAGS,
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700265 TRANS_OPT_DEEPEN_RELATIVE
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700266 };
267
Nguyễn Thái Ngọc Duy9318c5d2016-06-12 17:53:44 +0700268static int strbuf_set_helper_option(struct helper_data *data,
269 struct strbuf *buf)
270{
271 int ret;
272
273 sendline(data, buf);
274 if (recvline(data, buf))
275 exit(128);
276
277 if (!strcmp(buf->buf, "ok"))
278 ret = 0;
279 else if (starts_with(buf->buf, "error"))
280 ret = -1;
281 else if (!strcmp(buf->buf, "unsupported"))
282 ret = 1;
283 else {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200284 warning(_("%s unexpectedly said: '%s'"), data->name, buf->buf);
Nguyễn Thái Ngọc Duy9318c5d2016-06-12 17:53:44 +0700285 ret = 1;
286 }
287 return ret;
288}
289
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700290static int string_list_set_helper_option(struct helper_data *data,
291 const char *name,
292 struct string_list *list)
293{
294 struct strbuf buf = STRBUF_INIT;
295 int i, ret = 0;
296
297 for (i = 0; i < list->nr; i++) {
298 strbuf_addf(&buf, "option %s ", name);
299 quote_c_style(list->items[i].string, &buf, NULL, 0);
300 strbuf_addch(&buf, '\n');
301
302 if ((ret = strbuf_set_helper_option(data, &buf)))
303 break;
304 strbuf_reset(&buf);
305 }
306 strbuf_release(&buf);
307 return ret;
308}
309
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700310static int set_helper_option(struct transport *transport,
311 const char *name, const char *value)
312{
313 struct helper_data *data = transport->data;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700314 struct strbuf buf = STRBUF_INIT;
315 int i, ret, is_bool = 0;
316
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200317 get_helper(transport);
318
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700319 if (!data->option)
320 return 1;
321
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700322 if (!strcmp(name, "deepen-not"))
323 return string_list_set_helper_option(data, name,
324 (struct string_list *)value);
325
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700326 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
327 if (!strcmp(name, unsupported_options[i]))
328 return 1;
329 }
330
331 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
332 if (!strcmp(name, boolean_options[i])) {
333 is_bool = 1;
334 break;
335 }
336 }
337
338 strbuf_addf(&buf, "option %s ", name);
339 if (is_bool)
340 strbuf_addstr(&buf, value ? "true" : "false");
341 else
342 quote_c_style(value, &buf, NULL, 0);
343 strbuf_addch(&buf, '\n');
344
Nguyễn Thái Ngọc Duy9318c5d2016-06-12 17:53:44 +0700345 ret = strbuf_set_helper_option(data, &buf);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700346 strbuf_release(&buf);
347 return ret;
348}
349
350static void standard_options(struct transport *t)
351{
352 char buf[16];
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700353 int v = t->verbose;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700354
Tay Ray Chuand01b3c02010-02-24 20:50:26 +0800355 set_helper_option(t, "progress", t->progress ? "true" : "false");
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700356
Jeff King8c5acfb2017-03-28 15:47:00 -0400357 xsnprintf(buf, sizeof(buf), "%d", v + 1);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700358 set_helper_option(t, "verbosity", buf);
Eric Wongc915f112016-02-03 04:09:14 +0000359
360 switch (t->family) {
361 case TRANSPORT_FAMILY_ALL:
362 /*
363 * this is already the default,
364 * do not break old remote helpers by setting "all" here
365 */
366 break;
367 case TRANSPORT_FAMILY_IPV4:
368 set_helper_option(t, "family", "ipv4");
369 break;
370 case TRANSPORT_FAMILY_IPV6:
371 set_helper_option(t, "family", "ipv6");
372 break;
373 }
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700374}
375
Daniel Barkalowf2a37152009-11-18 02:42:21 +0100376static int release_helper(struct transport *transport)
377{
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200378 int res = 0;
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100379 struct helper_data *data = transport->data;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700380 refspec_clear(&data->rs);
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200381 res = disconnect_helper(transport);
Daniel Barkalowf2a37152009-11-18 02:42:21 +0100382 free(transport->data);
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200383 return res;
Daniel Barkalowf2a37152009-11-18 02:42:21 +0100384}
385
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400386static int fetch_with_fetch(struct transport *transport,
Daniel Barkalow37148312009-11-18 02:42:24 +0100387 int nr_heads, struct ref **to_fetch)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400388{
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700389 struct helper_data *data = transport->data;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400390 int i;
391 struct strbuf buf = STRBUF_INIT;
392
393 for (i = 0; i < nr_heads; i++) {
Linus Torvalds10882612009-08-05 01:01:59 -0400394 const struct ref *posn = to_fetch[i];
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400395 if (posn->status & REF_STATUS_UPTODATE)
396 continue;
Daniel Barkalow2d14d652009-09-03 22:13:51 -0400397
398 strbuf_addf(&buf, "fetch %s %s\n",
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000399 oid_to_hex(&posn->old_oid),
Mike Hommey33cae542015-01-19 10:35:07 +0900400 posn->symref ? posn->symref : posn->name);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400401 }
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700402
403 strbuf_addch(&buf, '\n');
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200404 sendline(data, &buf);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700405
406 while (1) {
Junio C Hamano145136a2020-01-30 11:35:46 -0800407 const char *name;
408
Felipe Contreras5931b332014-04-12 15:33:29 -0500409 if (recvline(data, &buf))
410 exit(128);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700411
Junio C Hamano145136a2020-01-30 11:35:46 -0800412 if (skip_prefix(buf.buf, "lock ", &name)) {
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700413 if (transport->pack_lockfile)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200414 warning(_("%s also locked %s"), data->name, name);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700415 else
416 transport->pack_lockfile = xstrdup(name);
417 }
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700418 else if (data->check_connectivity &&
419 data->transport_options.check_self_contained_and_connected &&
420 !strcmp(buf.buf, "connectivity-ok"))
421 data->transport_options.self_contained_and_connected = 1;
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700422 else if (!buf.len)
423 break;
424 else
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200425 warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700426 }
427 strbuf_release(&buf);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400428 return 0;
429}
430
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100431static int get_importer(struct transport *transport, struct child_process *fastimport)
432{
433 struct child_process *helper = get_helper(transport);
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200434 struct helper_data *data = transport->data;
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200435 int cat_blob_fd, code;
René Scharfe483bbd42014-08-19 21:10:48 +0200436 child_process_init(fastimport);
Mike Hommey8b355422019-05-16 09:37:35 +0900437 fastimport->in = xdup(helper->out);
Jeff King173fd1a2014-05-15 04:35:06 -0400438 argv_array_push(&fastimport->args, "fast-import");
Jeff King68061e32019-08-29 14:37:26 -0400439 argv_array_push(&fastimport->args, "--allow-unsafe-features");
Jeff King173fd1a2014-05-15 04:35:06 -0400440 argv_array_push(&fastimport->args, debug ? "--stats" : "--quiet");
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100441
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200442 if (data->bidi_import) {
443 cat_blob_fd = xdup(helper->in);
Jeff King173fd1a2014-05-15 04:35:06 -0400444 argv_array_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200445 }
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100446 fastimport->git_cmd = 1;
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200447
448 code = start_command(fastimport);
449 return code;
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100450}
451
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500452static int get_exporter(struct transport *transport,
453 struct child_process *fastexport,
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500454 struct string_list *revlist_args)
455{
Sverre Rabbeliera515ebe2011-07-16 15:03:40 +0200456 struct helper_data *data = transport->data;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500457 struct child_process *helper = get_helper(transport);
Jeff King2aeae402014-05-15 04:34:44 -0400458 int i;
Felipe Contreras852e54b2014-04-12 15:33:31 -0500459
René Scharfe8828f292014-10-28 21:52:34 +0100460 child_process_init(fastexport);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500461
462 /* we need to duplicate helper->in because we want to use it after
463 * fastexport is done with it. */
464 fastexport->out = dup(helper->in);
Jeff King2aeae402014-05-15 04:34:44 -0400465 argv_array_push(&fastexport->args, "fast-export");
466 argv_array_push(&fastexport->args, "--use-done-feature");
467 argv_array_push(&fastexport->args, data->signed_tags ?
468 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
469 if (data->export_marks)
470 argv_array_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
471 if (data->import_marks)
472 argv_array_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500473
474 for (i = 0; i < revlist_args->nr; i++)
Jeff King2aeae402014-05-15 04:34:44 -0400475 argv_array_push(&fastexport->args, revlist_args->items[i].string);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500476
477 fastexport->git_cmd = 1;
478 return start_command(fastexport);
479}
480
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100481static int fetch_with_import(struct transport *transport,
482 int nr_heads, struct ref **to_fetch)
483{
484 struct child_process fastimport;
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100485 struct helper_data *data = transport->data;
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100486 int i;
487 struct ref *posn;
488 struct strbuf buf = STRBUF_INIT;
489
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200490 get_helper(transport);
491
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100492 if (get_importer(transport, &fastimport))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200493 die(_("couldn't run fast-import"));
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100494
495 for (i = 0; i < nr_heads; i++) {
496 posn = to_fetch[i];
497 if (posn->status & REF_STATUS_UPTODATE)
498 continue;
499
Mike Hommey33cae542015-01-19 10:35:07 +0900500 strbuf_addf(&buf, "import %s\n",
501 posn->symref ? posn->symref : posn->name);
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200502 sendline(data, &buf);
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100503 strbuf_reset(&buf);
504 }
Sverre Rabbelier9504bc92011-07-16 15:03:38 +0200505
506 write_constant(data->helper->in, "\n");
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200507 /*
508 * remote-helpers that advertise the bidi-import capability are required to
509 * buffer the complete batch of import commands until this newline before
510 * sending data to fast-import.
511 * These helpers read back data from fast-import on their stdin, which could
512 * be mixed with import commands, otherwise.
513 */
Sverre Rabbelier9504bc92011-07-16 15:03:38 +0200514
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200515 if (finish_command(&fastimport))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200516 die(_("error while running fast-import"));
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100517
Florian Achleitnerdff9d652012-07-30 16:31:18 +0200518 /*
519 * The fast-import stream of a remote helper that advertises
520 * the "refspec" capability writes to the refs named after the
521 * right hand side of the first refspec matching each ref we
522 * were fetching.
523 *
524 * (If no "refspec" capability was specified, for historical
Felipe Contreras7a43c552013-04-17 23:14:28 -0500525 * reasons we default to the equivalent of *:*.)
Florian Achleitnerdff9d652012-07-30 16:31:18 +0200526 *
527 * Store the result in to_fetch[i].old_sha1. Callers such
528 * as "git fetch" can use the value to write feedback to the
529 * terminal, populate FETCH_HEAD, and determine what new value
530 * should be written to peer_ref if the update is a
531 * fast-forward or this is a forced update.
532 */
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100533 for (i = 0; i < nr_heads; i++) {
Mike Hommey33cae542015-01-19 10:35:07 +0900534 char *private, *name;
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100535 posn = to_fetch[i];
536 if (posn->status & REF_STATUS_UPTODATE)
537 continue;
Mike Hommey33cae542015-01-19 10:35:07 +0900538 name = posn->symref ? posn->symref : posn->name;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700539 if (data->rs.nr)
Brandon Williamsd0004142018-05-16 15:58:11 -0700540 private = apply_refspecs(&data->rs, name);
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100541 else
Mike Hommey33cae542015-01-19 10:35:07 +0900542 private = xstrdup(name);
Michael Haggertyd51b7202011-09-15 23:10:38 +0200543 if (private) {
brian m. carlson34c290a2017-10-15 22:06:56 +0000544 if (read_ref(private, &posn->old_oid) < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200545 die(_("could not read ref %s"), private);
Michael Haggertyd51b7202011-09-15 23:10:38 +0200546 free(private);
547 }
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100548 }
Sverre Rabbelierb962dbd2009-11-18 02:42:29 +0100549 strbuf_release(&buf);
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100550 return 0;
551}
552
Brandon Williams176e85c2018-03-15 10:31:33 -0700553static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200554{
555 struct helper_data *data = transport->data;
Brandon Williams176e85c2018-03-15 10:31:33 -0700556 int ret = 0;
557 int duped;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200558 FILE *input;
Brandon Williams176e85c2018-03-15 10:31:33 -0700559 struct child_process *helper;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200560
561 helper = get_helper(transport);
562
563 /*
564 * Yes, dup the pipe another time, as we need unbuffered version
565 * of input pipe as FILE*. fclose() closes the underlying fd and
566 * stream buffering only can be changed before first I/O operation
567 * on it.
568 */
569 duped = dup(helper->out);
570 if (duped < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200571 die_errno(_("can't dup helper output fd"));
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200572 input = xfdopen(duped, "r");
573 setvbuf(input, NULL, _IONBF, 0);
574
Brandon Williams176e85c2018-03-15 10:31:33 -0700575 sendline(data, cmdbuf);
576 if (recvline_fh(input, cmdbuf))
577 exit(128);
578
579 if (!strcmp(cmdbuf->buf, "")) {
580 data->no_disconnect_req = 1;
581 if (debug)
582 fprintf(stderr, "Debug: Smart transport connection "
583 "ready.\n");
584 ret = 1;
585 } else if (!strcmp(cmdbuf->buf, "fallback")) {
586 if (debug)
587 fprintf(stderr, "Debug: Falling back to dumb "
588 "transport.\n");
589 } else {
Nguyễn Thái Ngọc Duy739fb712018-11-26 20:57:56 +0100590 die(_("unknown response to connect: %s"),
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200591 cmdbuf->buf);
Brandon Williams176e85c2018-03-15 10:31:33 -0700592 }
593
594 fclose(input);
595 return ret;
596}
597
598static int process_connect_service(struct transport *transport,
599 const char *name, const char *exec)
600{
601 struct helper_data *data = transport->data;
602 struct strbuf cmdbuf = STRBUF_INIT;
603 int ret = 0;
604
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200605 /*
606 * Handle --upload-pack and friends. This is fire and forget...
607 * just warn if it fails.
608 */
609 if (strcmp(name, exec)) {
Brandon Williams176e85c2018-03-15 10:31:33 -0700610 int r = set_helper_option(transport, "servpath", exec);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200611 if (r > 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200612 warning(_("setting remote service path not supported by protocol"));
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200613 else if (r < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200614 warning(_("invalid remote service path"));
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200615 }
616
Brandon Williams176e85c2018-03-15 10:31:33 -0700617 if (data->connect) {
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200618 strbuf_addf(&cmdbuf, "connect %s\n", name);
Brandon Williams176e85c2018-03-15 10:31:33 -0700619 ret = run_connect(transport, &cmdbuf);
Brandon Williamsedc9caf2018-03-15 10:31:34 -0700620 } else if (data->stateless_connect &&
621 (get_protocol_version_config() == protocol_v2) &&
622 !strcmp("git-upload-pack", name)) {
623 strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
624 ret = run_connect(transport, &cmdbuf);
625 if (ret)
626 transport->stateless_rpc = 1;
Brandon Williams176e85c2018-03-15 10:31:33 -0700627 }
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200628
Rene Scharfe4168be82017-08-30 20:20:15 +0200629 strbuf_release(&cmdbuf);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200630 return ret;
631}
632
633static int process_connect(struct transport *transport,
634 int for_push)
635{
636 struct helper_data *data = transport->data;
637 const char *name;
638 const char *exec;
639
640 name = for_push ? "git-receive-pack" : "git-upload-pack";
641 if (for_push)
642 exec = data->transport_options.receivepack;
643 else
644 exec = data->transport_options.uploadpack;
645
646 return process_connect_service(transport, name, exec);
647}
648
Ilari Liusvaarab2367522009-12-09 17:26:33 +0200649static int connect_helper(struct transport *transport, const char *name,
650 const char *exec, int fd[2])
651{
652 struct helper_data *data = transport->data;
653
654 /* Get_helper so connect is inited. */
655 get_helper(transport);
656 if (!data->connect)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200657 die(_("operation not supported by protocol"));
Ilari Liusvaarab2367522009-12-09 17:26:33 +0200658
659 if (!process_connect_service(transport, name, exec))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200660 die(_("can't connect to subservice %s"), name);
Ilari Liusvaarab2367522009-12-09 17:26:33 +0200661
662 fd[0] = data->helper->out;
663 fd[1] = data->helper->in;
664 return 0;
665}
666
Jonathan Tanac3fda82019-08-21 15:20:09 -0700667static struct ref *get_refs_list_using_list(struct transport *transport,
668 int for_push);
669
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400670static int fetch(struct transport *transport,
Jonathan Tane2842b32018-08-01 13:13:20 -0700671 int nr_heads, struct ref **to_fetch)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400672{
673 struct helper_data *data = transport->data;
674 int i, count;
675
Jonathan Tanac3fda82019-08-21 15:20:09 -0700676 get_helper(transport);
677
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200678 if (process_connect(transport, 0)) {
679 do_take_over(transport);
Jonathan Tane2842b32018-08-01 13:13:20 -0700680 return transport->vtable->fetch(transport, nr_heads, to_fetch);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200681 }
682
Jonathan Tanac3fda82019-08-21 15:20:09 -0700683 if (!data->get_refs_list_called)
684 get_refs_list_using_list(transport, 0);
685
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400686 count = 0;
687 for (i = 0; i < nr_heads; i++)
688 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
689 count++;
690
691 if (!count)
692 return 0;
693
Mike Hommeyaab1beb2015-02-13 14:24:46 +0900694 if (data->check_connectivity &&
695 data->transport_options.check_self_contained_and_connected)
696 set_helper_option(transport, "check-connectivity", "true");
697
698 if (transport->cloning)
699 set_helper_option(transport, "cloning", "true");
700
701 if (data->transport_options.update_shallow)
702 set_helper_option(transport, "update-shallow", "true");
703
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800704 if (data->transport_options.filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700705 const char *spec = expand_list_objects_filter_spec(
706 &data->transport_options.filter_options);
707 set_helper_option(transport, "filter", spec);
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800708 }
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000709
Jonathan Tan3390e422018-07-02 15:39:44 -0700710 if (data->transport_options.negotiation_tips)
711 warning("Ignoring --negotiation-tip because the protocol does not support it.");
712
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400713 if (data->fetch)
714 return fetch_with_fetch(transport, nr_heads, to_fetch);
715
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100716 if (data->import)
717 return fetch_with_import(transport, nr_heads, to_fetch);
718
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400719 return -1;
720}
721
Felipe Contreras664059f2013-04-17 23:14:33 -0500722static int push_update_ref_status(struct strbuf *buf,
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200723 struct ref **ref,
724 struct ref *remote_refs)
725{
726 char *refname, *msg;
Felipe Contrerasf9e3c6b2013-11-12 14:56:57 -0600727 int status, forced = 0;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200728
Christian Couder59556542013-11-30 21:55:40 +0100729 if (starts_with(buf->buf, "ok ")) {
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200730 status = REF_STATUS_OK;
731 refname = buf->buf + 3;
Christian Couder59556542013-11-30 21:55:40 +0100732 } else if (starts_with(buf->buf, "error ")) {
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200733 status = REF_STATUS_REMOTE_REJECT;
734 refname = buf->buf + 6;
735 } else
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200736 die(_("expected ok/error, helper said '%s'"), buf->buf);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200737
738 msg = strchr(refname, ' ');
739 if (msg) {
740 struct strbuf msg_buf = STRBUF_INIT;
741 const char *end;
742
743 *msg++ = '\0';
744 if (!unquote_c_style(&msg_buf, msg, &end))
745 msg = strbuf_detach(&msg_buf, NULL);
746 else
747 msg = xstrdup(msg);
748 strbuf_release(&msg_buf);
749
750 if (!strcmp(msg, "no match")) {
751 status = REF_STATUS_NONE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000752 FREE_AND_NULL(msg);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200753 }
754 else if (!strcmp(msg, "up to date")) {
755 status = REF_STATUS_UPTODATE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000756 FREE_AND_NULL(msg);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200757 }
758 else if (!strcmp(msg, "non-fast forward")) {
759 status = REF_STATUS_REJECT_NONFASTFORWARD;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000760 FREE_AND_NULL(msg);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200761 }
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600762 else if (!strcmp(msg, "already exists")) {
763 status = REF_STATUS_REJECT_ALREADY_EXISTS;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000764 FREE_AND_NULL(msg);
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600765 }
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800766 else if (!strcmp(msg, "fetch first")) {
767 status = REF_STATUS_REJECT_FETCH_FIRST;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000768 FREE_AND_NULL(msg);
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800769 }
770 else if (!strcmp(msg, "needs force")) {
771 status = REF_STATUS_REJECT_NEEDS_FORCE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000772 FREE_AND_NULL(msg);
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800773 }
Junio C Hamano631b5ef2013-07-08 14:42:40 -0700774 else if (!strcmp(msg, "stale info")) {
775 status = REF_STATUS_REJECT_STALE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000776 FREE_AND_NULL(msg);
Junio C Hamano631b5ef2013-07-08 14:42:40 -0700777 }
Felipe Contrerasf9e3c6b2013-11-12 14:56:57 -0600778 else if (!strcmp(msg, "forced update")) {
779 forced = 1;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000780 FREE_AND_NULL(msg);
Felipe Contrerasf9e3c6b2013-11-12 14:56:57 -0600781 }
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200782 }
783
784 if (*ref)
785 *ref = find_ref_by_name(*ref, refname);
786 if (!*ref)
787 *ref = find_ref_by_name(remote_refs, refname);
788 if (!*ref) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200789 warning(_("helper reported unexpected status of %s"), refname);
Felipe Contreras664059f2013-04-17 23:14:33 -0500790 return 1;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200791 }
792
793 if ((*ref)->status != REF_STATUS_NONE) {
794 /*
795 * Earlier, the ref was marked not to be pushed, so ignore the ref
796 * status reported by the remote helper if the latter is 'no match'.
797 */
798 if (status == REF_STATUS_NONE)
Felipe Contreras664059f2013-04-17 23:14:33 -0500799 return 1;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200800 }
801
802 (*ref)->status = status;
Max Horncf31f702014-02-21 10:55:59 +0100803 (*ref)->forced_update |= forced;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200804 (*ref)->remote_status = msg;
Felipe Contreras126aac52013-05-10 07:08:30 -0500805 return !(status == REF_STATUS_OK);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200806}
807
Felipe Contreras0551a062014-04-12 15:33:30 -0500808static int push_update_refs_status(struct helper_data *data,
Felipe Contreras5a753532013-10-31 03:36:37 -0600809 struct ref *remote_refs,
810 int flags)
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200811{
812 struct strbuf buf = STRBUF_INIT;
813 struct ref *ref = remote_refs;
Felipe Contreras0551a062014-04-12 15:33:30 -0500814 int ret = 0;
815
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200816 for (;;) {
Felipe Contreras664059f2013-04-17 23:14:33 -0500817 char *private;
818
Felipe Contreras0551a062014-04-12 15:33:30 -0500819 if (recvline(data, &buf)) {
820 ret = 1;
821 break;
822 }
823
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200824 if (!buf.len)
825 break;
826
Felipe Contreras664059f2013-04-17 23:14:33 -0500827 if (push_update_ref_status(&buf, &ref, remote_refs))
828 continue;
829
Brandon Williams57f32ac2018-05-16 15:58:03 -0700830 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update)
Felipe Contreras664059f2013-04-17 23:14:33 -0500831 continue;
832
833 /* propagate back the update to the remote namespace */
Brandon Williamsd0004142018-05-16 15:58:11 -0700834 private = apply_refspecs(&data->rs, ref->name);
Felipe Contreras664059f2013-04-17 23:14:33 -0500835 if (!private)
836 continue;
brian m. carlsonae077772017-10-15 22:06:51 +0000837 update_ref("update by helper", private, &ref->new_oid, NULL,
838 0, 0);
Felipe Contreras664059f2013-04-17 23:14:33 -0500839 free(private);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200840 }
841 strbuf_release(&buf);
Felipe Contreras0551a062014-04-12 15:33:30 -0500842 return ret;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200843}
844
Dave Borowitz30261092015-08-19 11:26:46 -0400845static void set_common_push_options(struct transport *transport,
846 const char *name, int flags)
847{
848 if (flags & TRANSPORT_PUSH_DRY_RUN) {
849 if (set_helper_option(transport, "dry-run", "true") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200850 die(_("helper %s does not support dry-run"), name);
Dave Borowitz30261092015-08-19 11:26:46 -0400851 } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
852 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200853 die(_("helper %s does not support --signed"), name);
Dave Borowitz30261092015-08-19 11:26:46 -0400854 } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
855 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200856 die(_("helper %s does not support --signed=if-asked"), name);
Dave Borowitz30261092015-08-19 11:26:46 -0400857 }
Stefan Beller438fc682017-02-08 14:04:00 -0800858
brian m. carlson6f119422019-10-16 23:45:34 +0000859 if (flags & TRANSPORT_PUSH_ATOMIC)
860 if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
861 die(_("helper %s does not support --atomic"), name);
862
Stefan Beller438fc682017-02-08 14:04:00 -0800863 if (flags & TRANSPORT_PUSH_OPTIONS) {
864 struct string_list_item *item;
865 for_each_string_list_item(item, transport->push_options)
866 if (set_helper_option(transport, "push-option", item->string) != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200867 die(_("helper %s does not support 'push-option'"), name);
Stefan Beller438fc682017-02-08 14:04:00 -0800868 }
Dave Borowitz30261092015-08-19 11:26:46 -0400869}
870
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500871static int push_refs_with_push(struct transport *transport,
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700872 struct ref *remote_refs, int flags)
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700873{
874 int force_all = flags & TRANSPORT_PUSH_FORCE;
875 int mirror = flags & TRANSPORT_PUSH_MIRROR;
Emily Shaffer3bca1e72019-07-11 14:19:19 -0700876 int atomic = flags & TRANSPORT_PUSH_ATOMIC;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700877 struct helper_data *data = transport->data;
878 struct strbuf buf = STRBUF_INIT;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700879 struct ref *ref;
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700880 struct string_list cas_options = STRING_LIST_INIT_DUP;
881 struct string_list_item *cas_option;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700882
Johannes Schindelinc0aa3352011-03-22 13:50:08 +0100883 get_helper(transport);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700884 if (!data->push)
885 return 1;
886
887 for (ref = remote_refs; ref; ref = ref->next) {
Tay Ray Chuan20e8b462010-01-08 10:12:42 +0800888 if (!ref->peer_ref && !mirror)
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700889 continue;
890
Tay Ray Chuan20e8b462010-01-08 10:12:42 +0800891 /* Check for statuses set by set_ref_status_for_push() */
892 switch (ref->status) {
893 case REF_STATUS_REJECT_NONFASTFORWARD:
Junio C Hamano631b5ef2013-07-08 14:42:40 -0700894 case REF_STATUS_REJECT_STALE:
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600895 case REF_STATUS_REJECT_ALREADY_EXISTS:
Emily Shaffer3bca1e72019-07-11 14:19:19 -0700896 if (atomic) {
897 string_list_clear(&cas_options, 0);
898 return 0;
899 } else
900 continue;
Tay Ray Chuan20e8b462010-01-08 10:12:42 +0800901 case REF_STATUS_UPTODATE:
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700902 continue;
Tay Ray Chuan20e8b462010-01-08 10:12:42 +0800903 default:
904 ; /* do nothing */
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700905 }
906
907 if (force_all)
908 ref->force = 1;
909
910 strbuf_addstr(&buf, "push ");
911 if (!ref->deletion) {
912 if (ref->force)
913 strbuf_addch(&buf, '+');
914 if (ref->peer_ref)
915 strbuf_addstr(&buf, ref->peer_ref->name);
916 else
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000917 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700918 }
919 strbuf_addch(&buf, ':');
920 strbuf_addstr(&buf, ref->name);
921 strbuf_addch(&buf, '\n');
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700922
923 /*
924 * The "--force-with-lease" options without explicit
925 * values to expect have already been expanded into
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000926 * the ref->old_oid_expect[] field; we can ignore
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700927 * transport->smart_options->cas altogether and instead
928 * can enumerate them from the refs.
929 */
930 if (ref->expect_old_sha1) {
931 struct strbuf cas = STRBUF_INIT;
932 strbuf_addf(&cas, "%s:%s",
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000933 ref->name, oid_to_hex(&ref->old_oid_expect));
René Scharfe176cb972017-12-08 18:29:31 +0100934 string_list_append_nodup(&cas_options,
935 strbuf_detach(&cas, NULL));
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700936 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700937 }
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700938 if (buf.len == 0) {
939 string_list_clear(&cas_options, 0);
Clemens Buchacherd8f67d22009-10-30 17:47:31 -0700940 return 0;
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700941 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700942
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700943 for_each_string_list_item(cas_option, &cas_options)
944 set_helper_option(transport, "cas", cas_option->string);
Dave Borowitz30261092015-08-19 11:26:46 -0400945 set_common_push_options(transport, data->name, flags);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700946
947 strbuf_addch(&buf, '\n');
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200948 sendline(data, &buf);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700949 strbuf_release(&buf);
René Scharfe176cb972017-12-08 18:29:31 +0100950 string_list_clear(&cas_options, 0);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200951
Felipe Contreras0551a062014-04-12 15:33:30 -0500952 return push_update_refs_status(data, remote_refs, flags);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700953}
954
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500955static int push_refs_with_export(struct transport *transport,
956 struct ref *remote_refs, int flags)
957{
958 struct ref *ref;
959 struct child_process *helper, exporter;
960 struct helper_data *data = transport->data;
Felipe Contrerasd98c8152014-04-20 13:59:25 -0500961 struct string_list revlist_args = STRING_LIST_INIT_DUP;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500962 struct strbuf buf = STRBUF_INIT;
963
Brandon Williams57f32ac2018-05-16 15:58:03 -0700964 if (!data->rs.nr)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200965 die(_("remote-helper doesn't support push; refspec needed"));
Felipe Contreras21610d82013-04-17 23:14:30 -0500966
Dave Borowitz30261092015-08-19 11:26:46 -0400967 set_common_push_options(transport, data->name, flags);
Felipe Contreras510fa6f2013-11-12 14:56:56 -0600968 if (flags & TRANSPORT_PUSH_FORCE) {
969 if (set_helper_option(transport, "force", "true") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200970 warning(_("helper %s does not support 'force'"), data->name);
Felipe Contreras510fa6f2013-11-12 14:56:56 -0600971 }
972
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500973 helper = get_helper(transport);
974
975 write_constant(helper->in, "export\n");
976
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500977 for (ref = remote_refs; ref; ref = ref->next) {
978 char *private;
brian m. carlson27912a02015-11-10 02:22:24 +0000979 struct object_id oid;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500980
Brandon Williamsd0004142018-05-16 15:58:11 -0700981 private = apply_refspecs(&data->rs, ref->name);
brian m. carlsone82caf32017-07-13 23:49:28 +0000982 if (private && !get_oid(private, &oid)) {
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500983 strbuf_addf(&buf, "^%s", private);
René Scharfe176cb972017-12-08 18:29:31 +0100984 string_list_append_nodup(&revlist_args,
985 strbuf_detach(&buf, NULL));
brian m. carlson27912a02015-11-10 02:22:24 +0000986 oidcpy(&ref->old_oid, &oid);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500987 }
Jeff King2faa1522011-07-16 15:03:21 +0200988 free(private);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500989
Felipe Contreras67c9c782013-05-20 20:02:45 -0500990 if (ref->peer_ref) {
Felipe Contrerasd98c8152014-04-20 13:59:25 -0500991 if (strcmp(ref->name, ref->peer_ref->name)) {
Felipe Contrerasf3d03762014-04-20 13:59:29 -0500992 if (!ref->deletion) {
993 const char *name;
994 int flag;
Felipe Contreras9193f742014-04-20 13:59:26 -0500995
Felipe Contrerasf3d03762014-04-20 13:59:29 -0500996 /* Follow symbolic refs (mainly for HEAD). */
brian m. carlson49e61472017-10-15 22:07:09 +0000997 name = resolve_ref_unsafe(ref->peer_ref->name,
998 RESOLVE_REF_READING,
999 &oid, &flag);
Felipe Contrerasf3d03762014-04-20 13:59:29 -05001000 if (!name || !(flag & REF_ISSYMREF))
1001 name = ref->peer_ref->name;
1002
1003 strbuf_addf(&buf, "%s:%s", name, ref->name);
1004 } else
1005 strbuf_addf(&buf, ":%s", ref->name);
Felipe Contreras9193f742014-04-20 13:59:26 -05001006
Felipe Contrerasd98c8152014-04-20 13:59:25 -05001007 string_list_append(&revlist_args, "--refspec");
1008 string_list_append(&revlist_args, buf.buf);
1009 strbuf_release(&buf);
1010 }
Felipe Contrerasf3d03762014-04-20 13:59:29 -05001011 if (!ref->deletion)
1012 string_list_append(&revlist_args, ref->peer_ref->name);
Felipe Contreras67c9c782013-05-20 20:02:45 -05001013 }
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001014 }
1015
Sverre Rabbeliera515ebe2011-07-16 15:03:40 +02001016 if (get_exporter(transport, &exporter, &revlist_args))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001017 die(_("couldn't run fast-export"));
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001018
Felipe Contrerasd98c8152014-04-20 13:59:25 -05001019 string_list_clear(&revlist_args, 1);
1020
Sverre Rabbeliercc567322011-07-16 15:03:35 +02001021 if (finish_command(&exporter))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001022 die(_("error while running fast-export"));
Felipe Contreras3994e642014-04-12 15:33:32 -05001023 if (push_update_refs_status(data, remote_refs, flags))
1024 return 1;
1025
1026 if (data->export_marks) {
1027 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1028 rename(buf.buf, data->export_marks);
1029 strbuf_release(&buf);
1030 }
1031
1032 return 0;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001033}
1034
1035static int push_refs(struct transport *transport,
1036 struct ref *remote_refs, int flags)
1037{
1038 struct helper_data *data = transport->data;
1039
1040 if (process_connect(transport, 1)) {
1041 do_take_over(transport);
Jonathan Tane967ca32017-12-14 13:44:45 -08001042 return transport->vtable->push_refs(transport, remote_refs, flags);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001043 }
1044
1045 if (!remote_refs) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001046 fprintf(stderr,
1047 _("No refs in common and none specified; doing nothing.\n"
1048 "Perhaps you should specify a branch such as 'master'.\n"));
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001049 return 0;
1050 }
1051
1052 if (data->push)
1053 return push_refs_with_push(transport, remote_refs, flags);
1054
1055 if (data->export)
1056 return push_refs_with_export(transport, remote_refs, flags);
1057
1058 return -1;
1059}
1060
1061
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01001062static int has_attribute(const char *attrs, const char *attr)
1063{
Daniel Barkalowf8ec9162009-11-18 02:42:30 +01001064 int len;
1065 if (!attrs)
1066 return 0;
1067
1068 len = strlen(attr);
1069 for (;;) {
1070 const char *space = strchrnul(attrs, ' ');
1071 if (len == space - attrs && !strncmp(attrs, attr, len))
1072 return 1;
1073 if (!*space)
1074 return 0;
1075 attrs = space + 1;
1076 }
1077}
1078
Brandon Williams834cf342018-03-15 10:31:22 -07001079static struct ref *get_refs_list(struct transport *transport, int for_push,
1080 const struct argv_array *ref_prefixes)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001081{
Jonathan Tanac3fda82019-08-21 15:20:09 -07001082 get_helper(transport);
1083
1084 if (process_connect(transport, for_push)) {
1085 do_take_over(transport);
1086 return transport->vtable->get_refs_list(transport, for_push, ref_prefixes);
1087 }
1088
1089 return get_refs_list_using_list(transport, for_push);
1090}
1091
1092static struct ref *get_refs_list_using_list(struct transport *transport,
1093 int for_push)
1094{
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001095 struct helper_data *data = transport->data;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001096 struct child_process *helper;
1097 struct ref *ret = NULL;
1098 struct ref **tail = &ret;
1099 struct ref *posn;
1100 struct strbuf buf = STRBUF_INIT;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001101
Jonathan Tanac3fda82019-08-21 15:20:09 -07001102 data->get_refs_list_called = 1;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001103 helper = get_helper(transport);
Daniel Barkalow2d14d652009-09-03 22:13:51 -04001104
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001105 if (data->push && for_push)
1106 write_str_in_full(helper->in, "list for-push\n");
1107 else
1108 write_str_in_full(helper->in, "list\n");
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001109
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001110 while (1) {
1111 char *eov, *eon;
Felipe Contreras5931b332014-04-12 15:33:29 -05001112 if (recvline(data, &buf))
1113 exit(128);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001114
1115 if (!*buf.buf)
1116 break;
1117
1118 eov = strchr(buf.buf, ' ');
1119 if (!eov)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001120 die(_("malformed response in ref list: %s"), buf.buf);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001121 eon = strchr(eov + 1, ' ');
1122 *eov = '\0';
1123 if (eon)
1124 *eon = '\0';
1125 *tail = alloc_ref(eov + 1);
1126 if (buf.buf[0] == '@')
1127 (*tail)->symref = xstrdup(buf.buf + 1);
1128 else if (buf.buf[0] != '?')
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001129 get_oid_hex(buf.buf, &(*tail)->old_oid);
Daniel Barkalowf8ec9162009-11-18 02:42:30 +01001130 if (eon) {
1131 if (has_attribute(eon + 1, "unchanged")) {
1132 (*tail)->status |= REF_STATUS_UPTODATE;
brian m. carlson34c290a2017-10-15 22:06:56 +00001133 if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02001134 die(_("could not read ref %s"),
Stefan Bellerae25fd32015-07-31 16:57:57 -07001135 (*tail)->name);
Daniel Barkalowf8ec9162009-11-18 02:42:30 +01001136 }
1137 }
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001138 tail = &((*tail)->next);
1139 }
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +02001140 if (debug)
1141 fprintf(stderr, "Debug: Read ref listing.\n");
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001142 strbuf_release(&buf);
1143
1144 for (posn = ret; posn; posn = posn->next)
1145 resolve_remote_symref(posn, ret);
1146
1147 return ret;
1148}
1149
Jonathan Tane967ca32017-12-14 13:44:45 -08001150static struct transport_vtable vtable = {
1151 set_helper_option,
1152 get_refs_list,
1153 fetch,
1154 push_refs,
1155 connect_helper,
1156 release_helper
1157};
1158
Daniel Barkalowc9e388b2009-09-03 22:13:49 -04001159int transport_helper_init(struct transport *transport, const char *name)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001160{
Brian Gesiak92e25b62014-05-27 00:33:56 +09001161 struct helper_data *data = xcalloc(1, sizeof(*data));
Daniel Barkalowc9e388b2009-09-03 22:13:49 -04001162 data->name = name;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001163
Jeff Kinga5adace2015-09-16 13:12:52 -04001164 transport_check_allowed(name);
1165
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +02001166 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1167 debug = 1;
1168
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001169 transport->data = data;
Jonathan Tane967ca32017-12-14 13:44:45 -08001170 transport->vtable = &vtable;
Ilari Liusvaara61b075b2009-12-09 17:26:31 +02001171 transport->smart_options = &(data->transport_options);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001172 return 0;
1173}
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001174
1175/*
1176 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1177 * buffer less), so attempt reads and writes with up to that size.
1178 */
1179#define BUFFERSIZE 65536
1180/* This should be enough to hold debugging message. */
1181#define PBUFFERSIZE 8192
1182
1183/* Print bidirectional transfer loop debug message. */
Jeff King46210852013-07-09 20:18:40 -04001184__attribute__((format (printf, 1, 2)))
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001185static void transfer_debug(const char *fmt, ...)
1186{
Martin Ågren6cdf8a72017-08-21 19:43:48 +02001187 /*
1188 * NEEDSWORK: This function is sometimes used from multiple threads, and
1189 * we end up using debug_enabled racily. That "should not matter" since
1190 * we always write the same value, but it's still wrong. This function
1191 * is listed in .tsan-suppressions for the time being.
1192 */
1193
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001194 va_list args;
1195 char msgbuf[PBUFFERSIZE];
1196 static int debug_enabled = -1;
1197
1198 if (debug_enabled < 0)
1199 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1200 if (!debug_enabled)
1201 return;
1202
1203 va_start(args, fmt);
1204 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1205 va_end(args);
1206 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1207}
1208
1209/* Stream state: More data may be coming in this direction. */
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001210#define SSTATE_TRANSFERRING 0
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001211/*
1212 * Stream state: No more data coming in this direction, flushing rest of
1213 * data.
1214 */
1215#define SSTATE_FLUSHING 1
1216/* Stream state: Transfer in this direction finished. */
1217#define SSTATE_FINISHED 2
1218
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001219#define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001220#define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1221#define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1222
1223/* Unidirectional transfer. */
1224struct unidirectional_transfer {
1225 /* Source */
1226 int src;
1227 /* Destination */
1228 int dest;
1229 /* Is source socket? */
1230 int src_is_sock;
1231 /* Is destination socket? */
1232 int dest_is_sock;
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02001233 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001234 int state;
1235 /* Buffer. */
1236 char buf[BUFFERSIZE];
1237 /* Buffer used. */
1238 size_t bufuse;
1239 /* Name of source. */
1240 const char *src_name;
1241 /* Name of destination. */
1242 const char *dest_name;
1243};
1244
1245/* Closes the target (for writing) if transfer has finished. */
1246static void udt_close_if_finished(struct unidirectional_transfer *t)
1247{
1248 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1249 t->state = SSTATE_FINISHED;
1250 if (t->dest_is_sock)
1251 shutdown(t->dest, SHUT_WR);
1252 else
1253 close(t->dest);
1254 transfer_debug("Closed %s.", t->dest_name);
1255 }
1256}
1257
1258/*
Li Peng832c0e52016-05-06 20:36:46 +08001259 * Tries to read data from source into buffer. If buffer is full,
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001260 * no data is read. Returns 0 on success, -1 on error.
1261 */
1262static int udt_do_read(struct unidirectional_transfer *t)
1263{
1264 ssize_t bytes;
1265
1266 if (t->bufuse == BUFFERSIZE)
1267 return 0; /* No space for more. */
1268
1269 transfer_debug("%s is readable", t->src_name);
Randall S. Beckerc14e5a12019-01-03 16:03:48 -05001270 bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
Jeff Kingd4c81362018-01-11 01:31:10 -05001271 if (bytes < 0) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001272 error_errno(_("read(%s) failed"), t->src_name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001273 return -1;
1274 } else if (bytes == 0) {
1275 transfer_debug("%s EOF (with %i bytes in buffer)",
Jeff King46210852013-07-09 20:18:40 -04001276 t->src_name, (int)t->bufuse);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001277 t->state = SSTATE_FLUSHING;
1278 } else if (bytes > 0) {
1279 t->bufuse += bytes;
1280 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1281 (int)bytes, t->src_name, (int)t->bufuse);
1282 }
1283 return 0;
1284}
1285
1286/* Tries to write data from buffer into destination. If buffer is empty,
1287 * no data is written. Returns 0 on success, -1 on error.
1288 */
1289static int udt_do_write(struct unidirectional_transfer *t)
1290{
Nicolas Kaiser803dbdb2011-03-05 00:16:26 +01001291 ssize_t bytes;
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001292
1293 if (t->bufuse == 0)
1294 return 0; /* Nothing to write. */
1295
1296 transfer_debug("%s is writable", t->dest_name);
Erik Faye-Lund7edc02f2014-01-17 15:17:09 +01001297 bytes = xwrite(t->dest, t->buf, t->bufuse);
Jeff Kingd4c81362018-01-11 01:31:10 -05001298 if (bytes < 0) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001299 error_errno(_("write(%s) failed"), t->dest_name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001300 return -1;
1301 } else if (bytes > 0) {
1302 t->bufuse -= bytes;
1303 if (t->bufuse)
1304 memmove(t->buf, t->buf + bytes, t->bufuse);
1305 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1306 (int)bytes, t->dest_name, (int)t->bufuse);
1307 }
1308 return 0;
1309}
1310
1311
1312/* State of bidirectional transfer loop. */
1313struct bidirectional_transfer_state {
1314 /* Direction from program to git. */
1315 struct unidirectional_transfer ptg;
1316 /* Direction from git to program. */
1317 struct unidirectional_transfer gtp;
1318};
1319
1320static void *udt_copy_task_routine(void *udt)
1321{
1322 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1323 while (t->state != SSTATE_FINISHED) {
1324 if (STATE_NEEDS_READING(t->state))
1325 if (udt_do_read(t))
1326 return NULL;
1327 if (STATE_NEEDS_WRITING(t->state))
1328 if (udt_do_write(t))
1329 return NULL;
1330 if (STATE_NEEDS_CLOSING(t->state))
1331 udt_close_if_finished(t);
1332 }
1333 return udt; /* Just some non-NULL value. */
1334}
1335
1336#ifndef NO_PTHREADS
1337
1338/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +02001339 * Join thread, with appropriate errors on failure. Name is name for the
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001340 * thread (for error messages). Returns 0 on success, 1 on failure.
1341 */
1342static int tloop_join(pthread_t thread, const char *name)
1343{
1344 int err;
1345 void *tret;
1346 err = pthread_join(thread, &tret);
1347 if (!tret) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001348 error(_("%s thread failed"), name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001349 return 1;
1350 }
1351 if (err) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001352 error(_("%s thread failed to join: %s"), name, strerror(err));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001353 return 1;
1354 }
1355 return 0;
1356}
1357
1358/*
1359 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1360 * -1 on failure.
1361 */
1362static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1363{
1364 pthread_t gtp_thread;
1365 pthread_t ptg_thread;
1366 int err;
1367 int ret = 0;
1368 err = pthread_create(&gtp_thread, NULL, udt_copy_task_routine,
1369 &s->gtp);
1370 if (err)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001371 die(_("can't start thread for copying data: %s"), strerror(err));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001372 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1373 &s->ptg);
1374 if (err)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001375 die(_("can't start thread for copying data: %s"), strerror(err));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001376
1377 ret |= tloop_join(gtp_thread, "Git to program copy");
1378 ret |= tloop_join(ptg_thread, "Program to git copy");
1379 return ret;
1380}
1381#else
1382
1383/* Close the source and target (for writing) for transfer. */
1384static void udt_kill_transfer(struct unidirectional_transfer *t)
1385{
1386 t->state = SSTATE_FINISHED;
1387 /*
1388 * Socket read end left open isn't a disaster if nobody
1389 * attempts to read from it (mingw compat headers do not
1390 * have SHUT_RD)...
1391 *
1392 * We can't fully close the socket since otherwise gtp
1393 * task would first close the socket it sends data to
1394 * while closing the ptg file descriptors.
1395 */
1396 if (!t->src_is_sock)
1397 close(t->src);
1398 if (t->dest_is_sock)
1399 shutdown(t->dest, SHUT_WR);
1400 else
1401 close(t->dest);
1402}
1403
1404/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +02001405 * Join process, with appropriate errors on failure. Name is name for the
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001406 * process (for error messages). Returns 0 on success, 1 on failure.
1407 */
1408static int tloop_join(pid_t pid, const char *name)
1409{
1410 int tret;
1411 if (waitpid(pid, &tret, 0) < 0) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001412 error_errno(_("%s process failed to wait"), name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001413 return 1;
1414 }
1415 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001416 error(_("%s process failed"), name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001417 return 1;
1418 }
1419 return 0;
1420}
1421
1422/*
1423 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1424 * -1 on failure.
1425 */
1426static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1427{
1428 pid_t pid1, pid2;
1429 int ret = 0;
1430
1431 /* Fork thread #1: git to program. */
1432 pid1 = fork();
1433 if (pid1 < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001434 die_errno(_("can't start thread for copying data"));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001435 else if (pid1 == 0) {
1436 udt_kill_transfer(&s->ptg);
1437 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1438 }
1439
1440 /* Fork thread #2: program to git. */
1441 pid2 = fork();
1442 if (pid2 < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001443 die_errno(_("can't start thread for copying data"));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001444 else if (pid2 == 0) {
1445 udt_kill_transfer(&s->gtp);
1446 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1447 }
1448
1449 /*
1450 * Close both streams in parent as to not interfere with
1451 * end of file detection and wait for both tasks to finish.
1452 */
1453 udt_kill_transfer(&s->gtp);
1454 udt_kill_transfer(&s->ptg);
1455 ret |= tloop_join(pid1, "Git to program copy");
1456 ret |= tloop_join(pid2, "Program to git copy");
1457 return ret;
1458}
1459#endif
1460
1461/*
1462 * Copies data from stdin to output and from input to stdout simultaneously.
1463 * Additionally filtering through given filter. If filter is NULL, uses
1464 * identity filter.
1465 */
1466int bidirectional_transfer_loop(int input, int output)
1467{
1468 struct bidirectional_transfer_state state;
1469
1470 /* Fill the state fields. */
1471 state.ptg.src = input;
1472 state.ptg.dest = 1;
1473 state.ptg.src_is_sock = (input == output);
1474 state.ptg.dest_is_sock = 0;
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001475 state.ptg.state = SSTATE_TRANSFERRING;
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001476 state.ptg.bufuse = 0;
1477 state.ptg.src_name = "remote input";
1478 state.ptg.dest_name = "stdout";
1479
1480 state.gtp.src = 0;
1481 state.gtp.dest = output;
1482 state.gtp.src_is_sock = 0;
1483 state.gtp.dest_is_sock = (input == output);
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001484 state.gtp.state = SSTATE_TRANSFERRING;
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001485 state.gtp.bufuse = 0;
1486 state.gtp.src_name = "stdin";
1487 state.gtp.dest_name = "remote output";
1488
1489 return tloop_spawnwait_tasks(&state);
1490}