blob: 4be035edb8b7c6d618b4dc78ea5de203a2831391 [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"
Jeff Kingdbbcd442020-07-28 16:23:39 -040012#include "strvec.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,
brian m. carlson8b85ee42020-05-25 19:59:03 +000035 no_private_update : 1,
36 object_format : 1;
Jonathan Tanac3fda82019-08-21 15:20:09 -070037
38 /*
39 * As an optimization, the transport code may invoke fetch before
40 * get_refs_list. If this happens, and if the transport helper doesn't
41 * support connect or stateless_connect, we need to invoke
42 * get_refs_list ourselves if we haven't already done so. Keep track of
43 * whether we have invoked get_refs_list.
44 */
45 unsigned get_refs_list_called : 1;
46
Sverre Rabbeliera515ebe2011-07-16 15:03:40 +020047 char *export_marks;
48 char *import_marks;
Daniel Barkalow72ff8942009-11-18 02:42:28 +010049 /* These go from remote name (as in "list") to private name */
Brandon Williams57f32ac2018-05-16 15:58:03 -070050 struct refspec rs;
Ilari Liusvaara61b075b2009-12-09 17:26:31 +020051 /* Transport options for fetch-pack/send-pack (should one of
52 * those be invoked).
53 */
54 struct git_transport_options transport_options;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -040055};
56
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020057static void sendline(struct helper_data *helper, struct strbuf *buffer)
58{
59 if (debug)
60 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
Jeff King06f46f22017-09-13 13:16:03 -040061 if (write_in_full(helper->helper->in, buffer->buf, buffer->len) < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +020062 die_errno(_("full write to remote helper failed"));
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020063}
64
Brandon Williamsb1c2edf2018-03-15 10:31:32 -070065static int recvline_fh(FILE *helper, struct strbuf *buffer)
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020066{
67 strbuf_reset(buffer);
68 if (debug)
69 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
Junio C Hamano692dfdf2015-10-28 13:36:00 -070070 if (strbuf_getline(buffer, helper) == EOF) {
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020071 if (debug)
72 fprintf(stderr, "Debug: Remote helper quit.\n");
Felipe Contreras5931b332014-04-12 15:33:29 -050073 return 1;
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020074 }
75
76 if (debug)
77 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
78 return 0;
79}
80
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +020081static int recvline(struct helper_data *helper, struct strbuf *buffer)
82{
Brandon Williamsb1c2edf2018-03-15 10:31:32 -070083 return recvline_fh(helper->out, buffer);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +020084}
85
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020086static void write_constant(int fd, const char *str)
87{
88 if (debug)
89 fprintf(stderr, "Debug: Remote helper: -> %s", str);
Jeff King06f46f22017-09-13 13:16:03 -040090 if (write_in_full(fd, str, strlen(str)) < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +020091 die_errno(_("full write to remote helper failed"));
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020092}
93
Stephen Boydc2e86ad2011-03-22 00:51:05 -070094static const char *remove_ext_force(const char *url)
Ilari Liusvaara25d5cc42009-12-09 17:26:29 +020095{
96 if (url) {
97 const char *colon = strchr(url, ':');
98 if (colon && colon[1] == ':')
99 return colon + 2;
100 }
101 return url;
102}
103
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200104static void do_take_over(struct transport *transport)
105{
106 struct helper_data *data;
107 data = (struct helper_data *)transport->data;
108 transport_take_over(transport, data->helper);
109 fclose(data->out);
110 free(data);
111}
112
Mike Hommey2879bc32015-02-13 14:24:45 +0900113static void standard_options(struct transport *t);
114
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400115static struct child_process *get_helper(struct transport *transport)
116{
117 struct helper_data *data = transport->data;
118 struct strbuf buf = STRBUF_INIT;
119 struct child_process *helper;
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200120 int duped;
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100121 int code;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400122
123 if (data->helper)
124 return data->helper;
125
René Scharfe483bbd42014-08-19 21:10:48 +0200126 helper = xmalloc(sizeof(*helper));
127 child_process_init(helper);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400128 helper->in = -1;
129 helper->out = -1;
130 helper->err = 0;
Junio C Hamano675df192020-08-26 12:46:49 -0700131 strvec_pushf(&helper->args, "remote-%s", data->name);
Jeff Kingc972bf42020-07-28 16:25:12 -0400132 strvec_push(&helper->args, transport->remote->name);
133 strvec_push(&helper->args, remove_ext_force(transport->url));
Junio C Hamano675df192020-08-26 12:46:49 -0700134 helper->git_cmd = 1;
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100135 helper->silent_exec_failure = 1;
Dmitry Ivankove1735872011-07-16 15:03:28 +0200136
Jonathan Nieder4b0c3c72017-02-14 15:36:19 -0500137 if (have_git_dir())
Jeff Kingc972bf42020-07-28 16:25:12 -0400138 strvec_pushf(&helper->env_array, "%s=%s",
Jeff Kingf6d89422020-07-28 16:26:31 -0400139 GIT_DIR_ENVIRONMENT, get_git_dir());
Dmitry Ivankove1735872011-07-16 15:03:28 +0200140
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400141 helper->trace2_child_class = helper->args.v[0]; /* "remote-<name>" */
Jeff Hostetlerabd81a32019-02-22 14:25:05 -0800142
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100143 code = start_command(helper);
144 if (code < 0 && errno == ENOENT)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200145 die(_("unable to find remote helper for '%s'"), data->name);
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100146 else if (code != 0)
147 exit(code);
148
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400149 data->helper = helper;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200150 data->no_disconnect_req = 0;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700151 refspec_init(&data->rs, REFSPEC_FETCH);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400152
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200153 /*
Junio C Hamano1a0c8df2016-01-13 18:32:23 -0800154 * Open the output as FILE* so strbuf_getline_*() family of
155 * functions can be used.
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200156 * Do this with duped fd because fclose() will close the fd,
157 * and stuff like taking over will require the fd to remain.
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200158 */
159 duped = dup(helper->out);
160 if (duped < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200161 die_errno(_("can't dup helper output fd"));
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200162 data->out = xfdopen(duped, "r");
163
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200164 write_constant(helper->in, "capabilities\n");
Daniel Barkalow2d14d652009-09-03 22:13:51 -0400165
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400166 while (1) {
Jeff King21a2d4a2014-06-18 15:47:17 -0400167 const char *capname, *arg;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200168 int mandatory = 0;
Felipe Contreras5931b332014-04-12 15:33:29 -0500169 if (recvline(data, &buf))
170 exit(128);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400171
172 if (!*buf.buf)
173 break;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200174
175 if (*buf.buf == '*') {
176 capname = buf.buf + 1;
177 mandatory = 1;
178 } else
179 capname = buf.buf;
180
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200181 if (debug)
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200182 fprintf(stderr, "Debug: Got cap %s\n", capname);
183 if (!strcmp(capname, "fetch"))
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400184 data->fetch = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200185 else if (!strcmp(capname, "option"))
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700186 data->option = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200187 else if (!strcmp(capname, "push"))
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700188 data->push = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200189 else if (!strcmp(capname, "import"))
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100190 data->import = 1;
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200191 else if (!strcmp(capname, "bidi-import"))
192 data->bidi_import = 1;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500193 else if (!strcmp(capname, "export"))
194 data->export = 1;
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700195 else if (!strcmp(capname, "check-connectivity"))
196 data->check_connectivity = 1;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700197 else if (skip_prefix(capname, "refspec ", &arg)) {
198 refspec_append(&data->rs, arg);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200199 } else if (!strcmp(capname, "connect")) {
200 data->connect = 1;
Brandon Williamsedc9caf2018-03-15 10:31:34 -0700201 } else if (!strcmp(capname, "stateless-connect")) {
202 data->stateless_connect = 1;
John Keeping0d957a42013-04-14 11:57:08 +0100203 } else if (!strcmp(capname, "signed-tags")) {
204 data->signed_tags = 1;
Jeff King21a2d4a2014-06-18 15:47:17 -0400205 } else if (skip_prefix(capname, "export-marks ", &arg)) {
206 data->export_marks = xstrdup(arg);
207 } else if (skip_prefix(capname, "import-marks ", &arg)) {
208 data->import_marks = xstrdup(arg);
Christian Couder59556542013-11-30 21:55:40 +0100209 } else if (starts_with(capname, "no-private-update")) {
Matthieu Moy597b8312013-09-03 17:45:14 +0200210 data->no_private_update = 1;
brian m. carlson8b85ee42020-05-25 19:59:03 +0000211 } else if (starts_with(capname, "object-format")) {
212 data->object_format = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200213 } else if (mandatory) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200214 die(_("unknown mandatory capability %s; this remote "
215 "helper probably needs newer version of Git"),
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200216 capname);
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100217 }
218 }
Brandon Williams57f32ac2018-05-16 15:58:03 -0700219 if (!data->rs.nr && (data->import || data->bidi_import || data->export)) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200220 warning(_("this remote helper should implement refspec capability"));
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400221 }
Sverre Rabbelierb962dbd2009-11-18 02:42:29 +0100222 strbuf_release(&buf);
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200223 if (debug)
224 fprintf(stderr, "Debug: Capabilities complete.\n");
Mike Hommey2879bc32015-02-13 14:24:45 +0900225 standard_options(transport);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400226 return data->helper;
227}
228
229static int disconnect_helper(struct transport *transport)
230{
231 struct helper_data *data = transport->data;
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200232 int res = 0;
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200233
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400234 if (data->helper) {
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200235 if (debug)
236 fprintf(stderr, "Debug: Disconnecting.\n");
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200237 if (!data->no_disconnect_req) {
Jeff Kingc34fe632012-02-23 05:04:34 -0500238 /*
239 * Ignore write errors; there's nothing we can do,
240 * since we're about to close the pipe anyway. And the
241 * most likely error is EPIPE due to the helper dying
242 * to report an error itself.
243 */
244 sigchain_push(SIGPIPE, SIG_IGN);
245 xwrite(data->helper->in, "\n", 1);
246 sigchain_pop(SIGPIPE);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200247 }
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400248 close(data->helper->in);
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200249 close(data->helper->out);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700250 fclose(data->out);
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200251 res = finish_command(data->helper);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000252 FREE_AND_NULL(data->helper);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400253 }
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200254 return res;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400255}
256
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700257static const char *unsupported_options[] = {
258 TRANS_OPT_UPLOADPACK,
259 TRANS_OPT_RECEIVEPACK,
260 TRANS_OPT_THIN,
261 TRANS_OPT_KEEP
262 };
Felipe Contreras23cd01e2013-10-31 03:25:40 -0600263
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700264static const char *boolean_options[] = {
265 TRANS_OPT_THIN,
266 TRANS_OPT_KEEP,
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700267 TRANS_OPT_FOLLOWTAGS,
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700268 TRANS_OPT_DEEPEN_RELATIVE
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700269 };
270
Nguyễn Thái Ngọc Duy9318c5d2016-06-12 17:53:44 +0700271static int strbuf_set_helper_option(struct helper_data *data,
272 struct strbuf *buf)
273{
274 int ret;
275
276 sendline(data, buf);
277 if (recvline(data, buf))
278 exit(128);
279
280 if (!strcmp(buf->buf, "ok"))
281 ret = 0;
282 else if (starts_with(buf->buf, "error"))
283 ret = -1;
284 else if (!strcmp(buf->buf, "unsupported"))
285 ret = 1;
286 else {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200287 warning(_("%s unexpectedly said: '%s'"), data->name, buf->buf);
Nguyễn Thái Ngọc Duy9318c5d2016-06-12 17:53:44 +0700288 ret = 1;
289 }
290 return ret;
291}
292
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700293static int string_list_set_helper_option(struct helper_data *data,
294 const char *name,
295 struct string_list *list)
296{
297 struct strbuf buf = STRBUF_INIT;
298 int i, ret = 0;
299
300 for (i = 0; i < list->nr; i++) {
301 strbuf_addf(&buf, "option %s ", name);
302 quote_c_style(list->items[i].string, &buf, NULL, 0);
303 strbuf_addch(&buf, '\n');
304
305 if ((ret = strbuf_set_helper_option(data, &buf)))
306 break;
307 strbuf_reset(&buf);
308 }
309 strbuf_release(&buf);
310 return ret;
311}
312
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700313static int set_helper_option(struct transport *transport,
314 const char *name, const char *value)
315{
316 struct helper_data *data = transport->data;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700317 struct strbuf buf = STRBUF_INIT;
318 int i, ret, is_bool = 0;
319
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200320 get_helper(transport);
321
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700322 if (!data->option)
323 return 1;
324
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700325 if (!strcmp(name, "deepen-not"))
326 return string_list_set_helper_option(data, name,
327 (struct string_list *)value);
328
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700329 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
330 if (!strcmp(name, unsupported_options[i]))
331 return 1;
332 }
333
334 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
335 if (!strcmp(name, boolean_options[i])) {
336 is_bool = 1;
337 break;
338 }
339 }
340
341 strbuf_addf(&buf, "option %s ", name);
342 if (is_bool)
343 strbuf_addstr(&buf, value ? "true" : "false");
344 else
345 quote_c_style(value, &buf, NULL, 0);
346 strbuf_addch(&buf, '\n');
347
Nguyễn Thái Ngọc Duy9318c5d2016-06-12 17:53:44 +0700348 ret = strbuf_set_helper_option(data, &buf);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700349 strbuf_release(&buf);
350 return ret;
351}
352
353static void standard_options(struct transport *t)
354{
355 char buf[16];
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700356 int v = t->verbose;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700357
Tay Ray Chuand01b3c02010-02-24 20:50:26 +0800358 set_helper_option(t, "progress", t->progress ? "true" : "false");
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700359
Jeff King8c5acfb2017-03-28 15:47:00 -0400360 xsnprintf(buf, sizeof(buf), "%d", v + 1);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700361 set_helper_option(t, "verbosity", buf);
Eric Wongc915f112016-02-03 04:09:14 +0000362
363 switch (t->family) {
364 case TRANSPORT_FAMILY_ALL:
365 /*
366 * this is already the default,
367 * do not break old remote helpers by setting "all" here
368 */
369 break;
370 case TRANSPORT_FAMILY_IPV4:
371 set_helper_option(t, "family", "ipv4");
372 break;
373 case TRANSPORT_FAMILY_IPV6:
374 set_helper_option(t, "family", "ipv6");
375 break;
376 }
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700377}
378
Daniel Barkalowf2a37152009-11-18 02:42:21 +0100379static int release_helper(struct transport *transport)
380{
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200381 int res = 0;
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100382 struct helper_data *data = transport->data;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700383 refspec_clear(&data->rs);
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200384 res = disconnect_helper(transport);
Daniel Barkalowf2a37152009-11-18 02:42:21 +0100385 free(transport->data);
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200386 return res;
Daniel Barkalowf2a37152009-11-18 02:42:21 +0100387}
388
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400389static int fetch_with_fetch(struct transport *transport,
Daniel Barkalow37148312009-11-18 02:42:24 +0100390 int nr_heads, struct ref **to_fetch)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400391{
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700392 struct helper_data *data = transport->data;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400393 int i;
394 struct strbuf buf = STRBUF_INIT;
395
396 for (i = 0; i < nr_heads; i++) {
Linus Torvalds10882612009-08-05 01:01:59 -0400397 const struct ref *posn = to_fetch[i];
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400398 if (posn->status & REF_STATUS_UPTODATE)
399 continue;
Daniel Barkalow2d14d652009-09-03 22:13:51 -0400400
401 strbuf_addf(&buf, "fetch %s %s\n",
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000402 oid_to_hex(&posn->old_oid),
Mike Hommey33cae542015-01-19 10:35:07 +0900403 posn->symref ? posn->symref : posn->name);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400404 }
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700405
406 strbuf_addch(&buf, '\n');
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200407 sendline(data, &buf);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700408
409 while (1) {
Junio C Hamano145136a2020-01-30 11:35:46 -0800410 const char *name;
411
Felipe Contreras5931b332014-04-12 15:33:29 -0500412 if (recvline(data, &buf))
413 exit(128);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700414
Junio C Hamano145136a2020-01-30 11:35:46 -0800415 if (skip_prefix(buf.buf, "lock ", &name)) {
Jonathan Tan9da69a62020-06-10 13:57:22 -0700416 if (transport->pack_lockfiles.nr)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200417 warning(_("%s also locked %s"), data->name, name);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700418 else
Jonathan Tan9da69a62020-06-10 13:57:22 -0700419 string_list_append(&transport->pack_lockfiles,
420 name);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700421 }
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700422 else if (data->check_connectivity &&
423 data->transport_options.check_self_contained_and_connected &&
424 !strcmp(buf.buf, "connectivity-ok"))
425 data->transport_options.self_contained_and_connected = 1;
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700426 else if (!buf.len)
427 break;
428 else
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200429 warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700430 }
431 strbuf_release(&buf);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400432 return 0;
433}
434
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100435static int get_importer(struct transport *transport, struct child_process *fastimport)
436{
437 struct child_process *helper = get_helper(transport);
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200438 struct helper_data *data = transport->data;
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200439 int cat_blob_fd, code;
René Scharfe483bbd42014-08-19 21:10:48 +0200440 child_process_init(fastimport);
Mike Hommey8b355422019-05-16 09:37:35 +0900441 fastimport->in = xdup(helper->out);
Jeff Kingc972bf42020-07-28 16:25:12 -0400442 strvec_push(&fastimport->args, "fast-import");
443 strvec_push(&fastimport->args, "--allow-unsafe-features");
444 strvec_push(&fastimport->args, debug ? "--stats" : "--quiet");
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100445
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200446 if (data->bidi_import) {
447 cat_blob_fd = xdup(helper->in);
Jeff Kingc972bf42020-07-28 16:25:12 -0400448 strvec_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200449 }
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100450 fastimport->git_cmd = 1;
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200451
452 code = start_command(fastimport);
453 return code;
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100454}
455
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500456static int get_exporter(struct transport *transport,
457 struct child_process *fastexport,
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500458 struct string_list *revlist_args)
459{
Sverre Rabbeliera515ebe2011-07-16 15:03:40 +0200460 struct helper_data *data = transport->data;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500461 struct child_process *helper = get_helper(transport);
Jeff King2aeae402014-05-15 04:34:44 -0400462 int i;
Felipe Contreras852e54b2014-04-12 15:33:31 -0500463
René Scharfe8828f292014-10-28 21:52:34 +0100464 child_process_init(fastexport);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500465
466 /* we need to duplicate helper->in because we want to use it after
467 * fastexport is done with it. */
468 fastexport->out = dup(helper->in);
Jeff Kingc972bf42020-07-28 16:25:12 -0400469 strvec_push(&fastexport->args, "fast-export");
470 strvec_push(&fastexport->args, "--use-done-feature");
471 strvec_push(&fastexport->args, data->signed_tags ?
Jeff King2aeae402014-05-15 04:34:44 -0400472 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
473 if (data->export_marks)
Jeff Kingc972bf42020-07-28 16:25:12 -0400474 strvec_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
Jeff King2aeae402014-05-15 04:34:44 -0400475 if (data->import_marks)
Jeff Kingc972bf42020-07-28 16:25:12 -0400476 strvec_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500477
478 for (i = 0; i < revlist_args->nr; i++)
Jeff Kingc972bf42020-07-28 16:25:12 -0400479 strvec_push(&fastexport->args, revlist_args->items[i].string);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500480
481 fastexport->git_cmd = 1;
482 return start_command(fastexport);
483}
484
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100485static int fetch_with_import(struct transport *transport,
486 int nr_heads, struct ref **to_fetch)
487{
488 struct child_process fastimport;
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100489 struct helper_data *data = transport->data;
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100490 int i;
491 struct ref *posn;
492 struct strbuf buf = STRBUF_INIT;
493
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200494 get_helper(transport);
495
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100496 if (get_importer(transport, &fastimport))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200497 die(_("couldn't run fast-import"));
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100498
499 for (i = 0; i < nr_heads; i++) {
500 posn = to_fetch[i];
501 if (posn->status & REF_STATUS_UPTODATE)
502 continue;
503
Mike Hommey33cae542015-01-19 10:35:07 +0900504 strbuf_addf(&buf, "import %s\n",
505 posn->symref ? posn->symref : posn->name);
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200506 sendline(data, &buf);
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100507 strbuf_reset(&buf);
508 }
Sverre Rabbelier9504bc92011-07-16 15:03:38 +0200509
510 write_constant(data->helper->in, "\n");
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200511 /*
512 * remote-helpers that advertise the bidi-import capability are required to
513 * buffer the complete batch of import commands until this newline before
514 * sending data to fast-import.
515 * These helpers read back data from fast-import on their stdin, which could
516 * be mixed with import commands, otherwise.
517 */
Sverre Rabbelier9504bc92011-07-16 15:03:38 +0200518
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200519 if (finish_command(&fastimport))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200520 die(_("error while running fast-import"));
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100521
Florian Achleitnerdff9d652012-07-30 16:31:18 +0200522 /*
523 * The fast-import stream of a remote helper that advertises
524 * the "refspec" capability writes to the refs named after the
525 * right hand side of the first refspec matching each ref we
526 * were fetching.
527 *
528 * (If no "refspec" capability was specified, for historical
Felipe Contreras7a43c552013-04-17 23:14:28 -0500529 * reasons we default to the equivalent of *:*.)
Florian Achleitnerdff9d652012-07-30 16:31:18 +0200530 *
531 * Store the result in to_fetch[i].old_sha1. Callers such
532 * as "git fetch" can use the value to write feedback to the
533 * terminal, populate FETCH_HEAD, and determine what new value
534 * should be written to peer_ref if the update is a
535 * fast-forward or this is a forced update.
536 */
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100537 for (i = 0; i < nr_heads; i++) {
Mike Hommey33cae542015-01-19 10:35:07 +0900538 char *private, *name;
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100539 posn = to_fetch[i];
540 if (posn->status & REF_STATUS_UPTODATE)
541 continue;
Mike Hommey33cae542015-01-19 10:35:07 +0900542 name = posn->symref ? posn->symref : posn->name;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700543 if (data->rs.nr)
Brandon Williamsd0004142018-05-16 15:58:11 -0700544 private = apply_refspecs(&data->rs, name);
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100545 else
Mike Hommey33cae542015-01-19 10:35:07 +0900546 private = xstrdup(name);
Michael Haggertyd51b7202011-09-15 23:10:38 +0200547 if (private) {
brian m. carlson34c290a2017-10-15 22:06:56 +0000548 if (read_ref(private, &posn->old_oid) < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200549 die(_("could not read ref %s"), private);
Michael Haggertyd51b7202011-09-15 23:10:38 +0200550 free(private);
551 }
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100552 }
Sverre Rabbelierb962dbd2009-11-18 02:42:29 +0100553 strbuf_release(&buf);
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100554 return 0;
555}
556
Brandon Williams176e85c2018-03-15 10:31:33 -0700557static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200558{
559 struct helper_data *data = transport->data;
Brandon Williams176e85c2018-03-15 10:31:33 -0700560 int ret = 0;
561 int duped;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200562 FILE *input;
Brandon Williams176e85c2018-03-15 10:31:33 -0700563 struct child_process *helper;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200564
565 helper = get_helper(transport);
566
567 /*
568 * Yes, dup the pipe another time, as we need unbuffered version
569 * of input pipe as FILE*. fclose() closes the underlying fd and
570 * stream buffering only can be changed before first I/O operation
571 * on it.
572 */
573 duped = dup(helper->out);
574 if (duped < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200575 die_errno(_("can't dup helper output fd"));
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200576 input = xfdopen(duped, "r");
577 setvbuf(input, NULL, _IONBF, 0);
578
Brandon Williams176e85c2018-03-15 10:31:33 -0700579 sendline(data, cmdbuf);
580 if (recvline_fh(input, cmdbuf))
581 exit(128);
582
583 if (!strcmp(cmdbuf->buf, "")) {
584 data->no_disconnect_req = 1;
585 if (debug)
586 fprintf(stderr, "Debug: Smart transport connection "
587 "ready.\n");
588 ret = 1;
589 } else if (!strcmp(cmdbuf->buf, "fallback")) {
590 if (debug)
591 fprintf(stderr, "Debug: Falling back to dumb "
592 "transport.\n");
593 } else {
Nguyễn Thái Ngọc Duy739fb712018-11-26 20:57:56 +0100594 die(_("unknown response to connect: %s"),
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200595 cmdbuf->buf);
Brandon Williams176e85c2018-03-15 10:31:33 -0700596 }
597
598 fclose(input);
599 return ret;
600}
601
602static int process_connect_service(struct transport *transport,
603 const char *name, const char *exec)
604{
605 struct helper_data *data = transport->data;
606 struct strbuf cmdbuf = STRBUF_INIT;
607 int ret = 0;
608
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200609 /*
610 * Handle --upload-pack and friends. This is fire and forget...
611 * just warn if it fails.
612 */
613 if (strcmp(name, exec)) {
Brandon Williams176e85c2018-03-15 10:31:33 -0700614 int r = set_helper_option(transport, "servpath", exec);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200615 if (r > 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200616 warning(_("setting remote service path not supported by protocol"));
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200617 else if (r < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200618 warning(_("invalid remote service path"));
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200619 }
620
Brandon Williams176e85c2018-03-15 10:31:33 -0700621 if (data->connect) {
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200622 strbuf_addf(&cmdbuf, "connect %s\n", name);
Brandon Williams176e85c2018-03-15 10:31:33 -0700623 ret = run_connect(transport, &cmdbuf);
Brandon Williamsedc9caf2018-03-15 10:31:34 -0700624 } else if (data->stateless_connect &&
625 (get_protocol_version_config() == protocol_v2) &&
626 !strcmp("git-upload-pack", name)) {
627 strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
628 ret = run_connect(transport, &cmdbuf);
629 if (ret)
630 transport->stateless_rpc = 1;
Brandon Williams176e85c2018-03-15 10:31:33 -0700631 }
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200632
Rene Scharfe4168be82017-08-30 20:20:15 +0200633 strbuf_release(&cmdbuf);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200634 return ret;
635}
636
637static int process_connect(struct transport *transport,
638 int for_push)
639{
640 struct helper_data *data = transport->data;
641 const char *name;
642 const char *exec;
643
644 name = for_push ? "git-receive-pack" : "git-upload-pack";
645 if (for_push)
646 exec = data->transport_options.receivepack;
647 else
648 exec = data->transport_options.uploadpack;
649
650 return process_connect_service(transport, name, exec);
651}
652
Ilari Liusvaarab2367522009-12-09 17:26:33 +0200653static int connect_helper(struct transport *transport, const char *name,
654 const char *exec, int fd[2])
655{
656 struct helper_data *data = transport->data;
657
658 /* Get_helper so connect is inited. */
659 get_helper(transport);
660 if (!data->connect)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200661 die(_("operation not supported by protocol"));
Ilari Liusvaarab2367522009-12-09 17:26:33 +0200662
663 if (!process_connect_service(transport, name, exec))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200664 die(_("can't connect to subservice %s"), name);
Ilari Liusvaarab2367522009-12-09 17:26:33 +0200665
666 fd[0] = data->helper->out;
667 fd[1] = data->helper->in;
668 return 0;
669}
670
Jonathan Tanac3fda82019-08-21 15:20:09 -0700671static struct ref *get_refs_list_using_list(struct transport *transport,
672 int for_push);
673
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400674static int fetch(struct transport *transport,
Jonathan Tane2842b32018-08-01 13:13:20 -0700675 int nr_heads, struct ref **to_fetch)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400676{
677 struct helper_data *data = transport->data;
678 int i, count;
679
Jonathan Tanac3fda82019-08-21 15:20:09 -0700680 get_helper(transport);
681
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200682 if (process_connect(transport, 0)) {
683 do_take_over(transport);
Jonathan Tane2842b32018-08-01 13:13:20 -0700684 return transport->vtable->fetch(transport, nr_heads, to_fetch);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200685 }
686
Jonathan Tan9c1e6572021-05-04 14:16:01 -0700687 /*
688 * If we reach here, then the server, the client, and/or the transport
689 * helper does not support protocol v2. --negotiate-only requires
690 * protocol v2.
691 */
692 if (data->transport_options.acked_commits) {
693 warning(_("--negotiate-only requires protocol v2"));
694 return -1;
695 }
696
Jonathan Tanac3fda82019-08-21 15:20:09 -0700697 if (!data->get_refs_list_called)
698 get_refs_list_using_list(transport, 0);
699
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400700 count = 0;
701 for (i = 0; i < nr_heads; i++)
702 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
703 count++;
704
705 if (!count)
706 return 0;
707
Mike Hommeyaab1beb2015-02-13 14:24:46 +0900708 if (data->check_connectivity &&
709 data->transport_options.check_self_contained_and_connected)
710 set_helper_option(transport, "check-connectivity", "true");
711
712 if (transport->cloning)
713 set_helper_option(transport, "cloning", "true");
714
715 if (data->transport_options.update_shallow)
716 set_helper_option(transport, "update-shallow", "true");
717
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800718 if (data->transport_options.filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700719 const char *spec = expand_list_objects_filter_spec(
720 &data->transport_options.filter_options);
721 set_helper_option(transport, "filter", spec);
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800722 }
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000723
Jonathan Tan3390e422018-07-02 15:39:44 -0700724 if (data->transport_options.negotiation_tips)
725 warning("Ignoring --negotiation-tip because the protocol does not support it.");
726
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400727 if (data->fetch)
728 return fetch_with_fetch(transport, nr_heads, to_fetch);
729
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100730 if (data->import)
731 return fetch_with_import(transport, nr_heads, to_fetch);
732
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400733 return -1;
734}
735
Jiang Xin63518a52020-08-27 11:45:46 -0400736struct push_update_ref_state {
737 struct ref *hint;
738 struct ref_push_report *report;
739 int new_report;
740};
741
Felipe Contreras664059f2013-04-17 23:14:33 -0500742static int push_update_ref_status(struct strbuf *buf,
Jiang Xin63518a52020-08-27 11:45:46 -0400743 struct push_update_ref_state *state,
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200744 struct ref *remote_refs)
745{
746 char *refname, *msg;
Felipe Contrerasf9e3c6b2013-11-12 14:56:57 -0600747 int status, forced = 0;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200748
Jiang Xin63518a52020-08-27 11:45:46 -0400749 if (starts_with(buf->buf, "option ")) {
750 struct object_id old_oid, new_oid;
751 const char *key, *val;
752 char *p;
753
754 if (!state->hint || !(state->report || state->new_report))
755 die(_("'option' without a matching 'ok/error' directive"));
756 if (state->new_report) {
757 if (!state->hint->report) {
René Scharfeca56dad2021-03-13 17:17:22 +0100758 CALLOC_ARRAY(state->hint->report, 1);
Jiang Xin63518a52020-08-27 11:45:46 -0400759 state->report = state->hint->report;
760 } else {
761 state->report = state->hint->report;
762 while (state->report->next)
763 state->report = state->report->next;
René Scharfeca56dad2021-03-13 17:17:22 +0100764 CALLOC_ARRAY(state->report->next, 1);
Jiang Xin63518a52020-08-27 11:45:46 -0400765 state->report = state->report->next;
766 }
767 state->new_report = 0;
768 }
769 key = buf->buf + 7;
770 p = strchr(key, ' ');
771 if (p)
772 *p++ = '\0';
773 val = p;
774 if (!strcmp(key, "refname"))
775 state->report->ref_name = xstrdup_or_null(val);
776 else if (!strcmp(key, "old-oid") && val &&
777 !parse_oid_hex(val, &old_oid, &val))
778 state->report->old_oid = oiddup(&old_oid);
779 else if (!strcmp(key, "new-oid") && val &&
780 !parse_oid_hex(val, &new_oid, &val))
781 state->report->new_oid = oiddup(&new_oid);
782 else if (!strcmp(key, "forced-update"))
783 state->report->forced_update = 1;
784 /* Not update remote namespace again. */
785 return 1;
786 }
787
788 state->report = NULL;
789 state->new_report = 0;
790
Christian Couder59556542013-11-30 21:55:40 +0100791 if (starts_with(buf->buf, "ok ")) {
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200792 status = REF_STATUS_OK;
793 refname = buf->buf + 3;
Christian Couder59556542013-11-30 21:55:40 +0100794 } else if (starts_with(buf->buf, "error ")) {
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200795 status = REF_STATUS_REMOTE_REJECT;
796 refname = buf->buf + 6;
797 } else
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200798 die(_("expected ok/error, helper said '%s'"), buf->buf);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200799
800 msg = strchr(refname, ' ');
801 if (msg) {
802 struct strbuf msg_buf = STRBUF_INIT;
803 const char *end;
804
805 *msg++ = '\0';
806 if (!unquote_c_style(&msg_buf, msg, &end))
807 msg = strbuf_detach(&msg_buf, NULL);
808 else
809 msg = xstrdup(msg);
810 strbuf_release(&msg_buf);
811
812 if (!strcmp(msg, "no match")) {
813 status = REF_STATUS_NONE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000814 FREE_AND_NULL(msg);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200815 }
816 else if (!strcmp(msg, "up to date")) {
817 status = REF_STATUS_UPTODATE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000818 FREE_AND_NULL(msg);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200819 }
820 else if (!strcmp(msg, "non-fast forward")) {
821 status = REF_STATUS_REJECT_NONFASTFORWARD;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000822 FREE_AND_NULL(msg);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200823 }
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600824 else if (!strcmp(msg, "already exists")) {
825 status = REF_STATUS_REJECT_ALREADY_EXISTS;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000826 FREE_AND_NULL(msg);
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600827 }
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800828 else if (!strcmp(msg, "fetch first")) {
829 status = REF_STATUS_REJECT_FETCH_FIRST;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000830 FREE_AND_NULL(msg);
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800831 }
832 else if (!strcmp(msg, "needs force")) {
833 status = REF_STATUS_REJECT_NEEDS_FORCE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000834 FREE_AND_NULL(msg);
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800835 }
Junio C Hamano631b5ef2013-07-08 14:42:40 -0700836 else if (!strcmp(msg, "stale info")) {
837 status = REF_STATUS_REJECT_STALE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000838 FREE_AND_NULL(msg);
Junio C Hamano631b5ef2013-07-08 14:42:40 -0700839 }
Srinidhi Kaushik99a1f9a2020-10-03 17:40:44 +0530840 else if (!strcmp(msg, "remote ref updated since checkout")) {
841 status = REF_STATUS_REJECT_REMOTE_UPDATED;
842 FREE_AND_NULL(msg);
843 }
Felipe Contrerasf9e3c6b2013-11-12 14:56:57 -0600844 else if (!strcmp(msg, "forced update")) {
845 forced = 1;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000846 FREE_AND_NULL(msg);
Felipe Contrerasf9e3c6b2013-11-12 14:56:57 -0600847 }
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200848 }
849
Jiang Xin63518a52020-08-27 11:45:46 -0400850 if (state->hint)
851 state->hint = find_ref_by_name(state->hint, refname);
852 if (!state->hint)
853 state->hint = find_ref_by_name(remote_refs, refname);
854 if (!state->hint) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200855 warning(_("helper reported unexpected status of %s"), refname);
Felipe Contreras664059f2013-04-17 23:14:33 -0500856 return 1;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200857 }
858
Jiang Xin63518a52020-08-27 11:45:46 -0400859 if (state->hint->status != REF_STATUS_NONE) {
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200860 /*
861 * Earlier, the ref was marked not to be pushed, so ignore the ref
862 * status reported by the remote helper if the latter is 'no match'.
863 */
864 if (status == REF_STATUS_NONE)
Felipe Contreras664059f2013-04-17 23:14:33 -0500865 return 1;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200866 }
867
Jiang Xin63518a52020-08-27 11:45:46 -0400868 if (status == REF_STATUS_OK)
869 state->new_report = 1;
870 state->hint->status = status;
871 state->hint->forced_update |= forced;
872 state->hint->remote_status = msg;
Felipe Contreras126aac52013-05-10 07:08:30 -0500873 return !(status == REF_STATUS_OK);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200874}
875
Felipe Contreras0551a062014-04-12 15:33:30 -0500876static int push_update_refs_status(struct helper_data *data,
Felipe Contreras5a753532013-10-31 03:36:37 -0600877 struct ref *remote_refs,
878 int flags)
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200879{
Jiang Xin63518a52020-08-27 11:45:46 -0400880 struct ref *ref;
881 struct ref_push_report *report;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200882 struct strbuf buf = STRBUF_INIT;
Jiang Xin63518a52020-08-27 11:45:46 -0400883 struct push_update_ref_state state = { remote_refs, NULL, 0 };
Felipe Contreras0551a062014-04-12 15:33:30 -0500884
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200885 for (;;) {
Felipe Contreras0551a062014-04-12 15:33:30 -0500886 if (recvline(data, &buf)) {
Jiang Xin63518a52020-08-27 11:45:46 -0400887 strbuf_release(&buf);
888 return 1;
Felipe Contreras0551a062014-04-12 15:33:30 -0500889 }
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200890 if (!buf.len)
891 break;
Jiang Xin63518a52020-08-27 11:45:46 -0400892 push_update_ref_status(&buf, &state, remote_refs);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200893 }
894 strbuf_release(&buf);
Jiang Xin63518a52020-08-27 11:45:46 -0400895
896 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update)
897 return 0;
898
899 /* propagate back the update to the remote namespace */
900 for (ref = remote_refs; ref; ref = ref->next) {
901 char *private;
902
903 if (ref->status != REF_STATUS_OK)
904 continue;
905
906 if (!ref->report) {
907 private = apply_refspecs(&data->rs, ref->name);
908 if (!private)
909 continue;
910 update_ref("update by helper", private, &(ref->new_oid),
911 NULL, 0, 0);
912 free(private);
913 } else {
914 for (report = ref->report; report; report = report->next) {
915 private = apply_refspecs(&data->rs,
916 report->ref_name
917 ? report->ref_name
918 : ref->name);
919 if (!private)
920 continue;
921 update_ref("update by helper", private,
922 report->new_oid
923 ? report->new_oid
924 : &(ref->new_oid),
925 NULL, 0, 0);
926 free(private);
927 }
928 }
929 }
930 return 0;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200931}
932
Dave Borowitz30261092015-08-19 11:26:46 -0400933static void set_common_push_options(struct transport *transport,
934 const char *name, int flags)
935{
936 if (flags & TRANSPORT_PUSH_DRY_RUN) {
937 if (set_helper_option(transport, "dry-run", "true") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200938 die(_("helper %s does not support dry-run"), name);
Dave Borowitz30261092015-08-19 11:26:46 -0400939 } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
940 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200941 die(_("helper %s does not support --signed"), name);
Dave Borowitz30261092015-08-19 11:26:46 -0400942 } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
943 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200944 die(_("helper %s does not support --signed=if-asked"), name);
Dave Borowitz30261092015-08-19 11:26:46 -0400945 }
Stefan Beller438fc682017-02-08 14:04:00 -0800946
brian m. carlson6f119422019-10-16 23:45:34 +0000947 if (flags & TRANSPORT_PUSH_ATOMIC)
948 if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
949 die(_("helper %s does not support --atomic"), name);
950
Srinidhi Kaushik3b990aa2020-10-03 17:40:45 +0530951 if (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES)
952 if (set_helper_option(transport, TRANS_OPT_FORCE_IF_INCLUDES, "true") != 0)
953 die(_("helper %s does not support --%s"),
954 name, TRANS_OPT_FORCE_IF_INCLUDES);
955
Stefan Beller438fc682017-02-08 14:04:00 -0800956 if (flags & TRANSPORT_PUSH_OPTIONS) {
957 struct string_list_item *item;
958 for_each_string_list_item(item, transport->push_options)
959 if (set_helper_option(transport, "push-option", item->string) != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200960 die(_("helper %s does not support 'push-option'"), name);
Stefan Beller438fc682017-02-08 14:04:00 -0800961 }
Dave Borowitz30261092015-08-19 11:26:46 -0400962}
963
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500964static int push_refs_with_push(struct transport *transport,
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700965 struct ref *remote_refs, int flags)
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700966{
967 int force_all = flags & TRANSPORT_PUSH_FORCE;
968 int mirror = flags & TRANSPORT_PUSH_MIRROR;
Emily Shaffer3bca1e72019-07-11 14:19:19 -0700969 int atomic = flags & TRANSPORT_PUSH_ATOMIC;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700970 struct helper_data *data = transport->data;
971 struct strbuf buf = STRBUF_INIT;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700972 struct ref *ref;
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700973 struct string_list cas_options = STRING_LIST_INIT_DUP;
974 struct string_list_item *cas_option;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700975
Johannes Schindelinc0aa3352011-03-22 13:50:08 +0100976 get_helper(transport);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700977 if (!data->push)
978 return 1;
979
980 for (ref = remote_refs; ref; ref = ref->next) {
Tay Ray Chuan20e8b462010-01-08 10:12:42 +0800981 if (!ref->peer_ref && !mirror)
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700982 continue;
983
Tay Ray Chuan20e8b462010-01-08 10:12:42 +0800984 /* Check for statuses set by set_ref_status_for_push() */
985 switch (ref->status) {
986 case REF_STATUS_REJECT_NONFASTFORWARD:
Junio C Hamano631b5ef2013-07-08 14:42:40 -0700987 case REF_STATUS_REJECT_STALE:
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600988 case REF_STATUS_REJECT_ALREADY_EXISTS:
Srinidhi Kaushik99a1f9a2020-10-03 17:40:44 +0530989 case REF_STATUS_REJECT_REMOTE_UPDATED:
Emily Shaffer3bca1e72019-07-11 14:19:19 -0700990 if (atomic) {
Jiang Xindfe1b7f2020-04-17 05:45:36 -0400991 reject_atomic_push(remote_refs, mirror);
Emily Shaffer3bca1e72019-07-11 14:19:19 -0700992 string_list_clear(&cas_options, 0);
993 return 0;
994 } else
995 continue;
Tay Ray Chuan20e8b462010-01-08 10:12:42 +0800996 case REF_STATUS_UPTODATE:
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700997 continue;
Tay Ray Chuan20e8b462010-01-08 10:12:42 +0800998 default:
999 ; /* do nothing */
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001000 }
1001
1002 if (force_all)
1003 ref->force = 1;
1004
1005 strbuf_addstr(&buf, "push ");
1006 if (!ref->deletion) {
1007 if (ref->force)
1008 strbuf_addch(&buf, '+');
1009 if (ref->peer_ref)
1010 strbuf_addstr(&buf, ref->peer_ref->name);
1011 else
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001012 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001013 }
1014 strbuf_addch(&buf, ':');
1015 strbuf_addstr(&buf, ref->name);
1016 strbuf_addch(&buf, '\n');
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001017
1018 /*
1019 * The "--force-with-lease" options without explicit
1020 * values to expect have already been expanded into
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001021 * the ref->old_oid_expect[] field; we can ignore
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001022 * transport->smart_options->cas altogether and instead
1023 * can enumerate them from the refs.
1024 */
1025 if (ref->expect_old_sha1) {
1026 struct strbuf cas = STRBUF_INIT;
1027 strbuf_addf(&cas, "%s:%s",
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001028 ref->name, oid_to_hex(&ref->old_oid_expect));
René Scharfe176cb972017-12-08 18:29:31 +01001029 string_list_append_nodup(&cas_options,
1030 strbuf_detach(&cas, NULL));
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001031 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001032 }
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001033 if (buf.len == 0) {
1034 string_list_clear(&cas_options, 0);
Clemens Buchacherd8f67d22009-10-30 17:47:31 -07001035 return 0;
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001036 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001037
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001038 for_each_string_list_item(cas_option, &cas_options)
1039 set_helper_option(transport, "cas", cas_option->string);
Dave Borowitz30261092015-08-19 11:26:46 -04001040 set_common_push_options(transport, data->name, flags);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001041
1042 strbuf_addch(&buf, '\n');
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +02001043 sendline(data, &buf);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001044 strbuf_release(&buf);
René Scharfe176cb972017-12-08 18:29:31 +01001045 string_list_clear(&cas_options, 0);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +02001046
Felipe Contreras0551a062014-04-12 15:33:30 -05001047 return push_update_refs_status(data, remote_refs, flags);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001048}
1049
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001050static int push_refs_with_export(struct transport *transport,
1051 struct ref *remote_refs, int flags)
1052{
1053 struct ref *ref;
1054 struct child_process *helper, exporter;
1055 struct helper_data *data = transport->data;
Felipe Contrerasd98c8152014-04-20 13:59:25 -05001056 struct string_list revlist_args = STRING_LIST_INIT_DUP;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001057 struct strbuf buf = STRBUF_INIT;
1058
Brandon Williams57f32ac2018-05-16 15:58:03 -07001059 if (!data->rs.nr)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001060 die(_("remote-helper doesn't support push; refspec needed"));
Felipe Contreras21610d82013-04-17 23:14:30 -05001061
Dave Borowitz30261092015-08-19 11:26:46 -04001062 set_common_push_options(transport, data->name, flags);
Felipe Contreras510fa6f2013-11-12 14:56:56 -06001063 if (flags & TRANSPORT_PUSH_FORCE) {
1064 if (set_helper_option(transport, "force", "true") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001065 warning(_("helper %s does not support 'force'"), data->name);
Felipe Contreras510fa6f2013-11-12 14:56:56 -06001066 }
1067
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001068 helper = get_helper(transport);
1069
1070 write_constant(helper->in, "export\n");
1071
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001072 for (ref = remote_refs; ref; ref = ref->next) {
1073 char *private;
brian m. carlson27912a02015-11-10 02:22:24 +00001074 struct object_id oid;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001075
Brandon Williamsd0004142018-05-16 15:58:11 -07001076 private = apply_refspecs(&data->rs, ref->name);
brian m. carlsone82caf32017-07-13 23:49:28 +00001077 if (private && !get_oid(private, &oid)) {
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001078 strbuf_addf(&buf, "^%s", private);
René Scharfe176cb972017-12-08 18:29:31 +01001079 string_list_append_nodup(&revlist_args,
1080 strbuf_detach(&buf, NULL));
brian m. carlson27912a02015-11-10 02:22:24 +00001081 oidcpy(&ref->old_oid, &oid);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001082 }
Jeff King2faa1522011-07-16 15:03:21 +02001083 free(private);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001084
Felipe Contreras67c9c782013-05-20 20:02:45 -05001085 if (ref->peer_ref) {
Felipe Contrerasd98c8152014-04-20 13:59:25 -05001086 if (strcmp(ref->name, ref->peer_ref->name)) {
Felipe Contrerasf3d03762014-04-20 13:59:29 -05001087 if (!ref->deletion) {
1088 const char *name;
1089 int flag;
Felipe Contreras9193f742014-04-20 13:59:26 -05001090
Felipe Contrerasf3d03762014-04-20 13:59:29 -05001091 /* Follow symbolic refs (mainly for HEAD). */
brian m. carlson49e61472017-10-15 22:07:09 +00001092 name = resolve_ref_unsafe(ref->peer_ref->name,
1093 RESOLVE_REF_READING,
1094 &oid, &flag);
Felipe Contrerasf3d03762014-04-20 13:59:29 -05001095 if (!name || !(flag & REF_ISSYMREF))
1096 name = ref->peer_ref->name;
1097
1098 strbuf_addf(&buf, "%s:%s", name, ref->name);
1099 } else
1100 strbuf_addf(&buf, ":%s", ref->name);
Felipe Contreras9193f742014-04-20 13:59:26 -05001101
Felipe Contrerasd98c8152014-04-20 13:59:25 -05001102 string_list_append(&revlist_args, "--refspec");
1103 string_list_append(&revlist_args, buf.buf);
1104 strbuf_release(&buf);
1105 }
Felipe Contrerasf3d03762014-04-20 13:59:29 -05001106 if (!ref->deletion)
1107 string_list_append(&revlist_args, ref->peer_ref->name);
Felipe Contreras67c9c782013-05-20 20:02:45 -05001108 }
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001109 }
1110
Sverre Rabbeliera515ebe2011-07-16 15:03:40 +02001111 if (get_exporter(transport, &exporter, &revlist_args))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001112 die(_("couldn't run fast-export"));
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001113
Felipe Contrerasd98c8152014-04-20 13:59:25 -05001114 string_list_clear(&revlist_args, 1);
1115
Sverre Rabbeliercc567322011-07-16 15:03:35 +02001116 if (finish_command(&exporter))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001117 die(_("error while running fast-export"));
Felipe Contreras3994e642014-04-12 15:33:32 -05001118 if (push_update_refs_status(data, remote_refs, flags))
1119 return 1;
1120
1121 if (data->export_marks) {
1122 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1123 rename(buf.buf, data->export_marks);
1124 strbuf_release(&buf);
1125 }
1126
1127 return 0;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001128}
1129
1130static int push_refs(struct transport *transport,
1131 struct ref *remote_refs, int flags)
1132{
1133 struct helper_data *data = transport->data;
1134
1135 if (process_connect(transport, 1)) {
1136 do_take_over(transport);
Jonathan Tane967ca32017-12-14 13:44:45 -08001137 return transport->vtable->push_refs(transport, remote_refs, flags);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001138 }
1139
1140 if (!remote_refs) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001141 fprintf(stderr,
1142 _("No refs in common and none specified; doing nothing.\n"
Johannes Schindelin4d046582020-06-24 14:46:29 +00001143 "Perhaps you should specify a branch.\n"));
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001144 return 0;
1145 }
1146
1147 if (data->push)
1148 return push_refs_with_push(transport, remote_refs, flags);
1149
1150 if (data->export)
1151 return push_refs_with_export(transport, remote_refs, flags);
1152
1153 return -1;
1154}
1155
1156
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01001157static int has_attribute(const char *attrs, const char *attr)
1158{
Daniel Barkalowf8ec9162009-11-18 02:42:30 +01001159 int len;
1160 if (!attrs)
1161 return 0;
1162
1163 len = strlen(attr);
1164 for (;;) {
1165 const char *space = strchrnul(attrs, ' ');
1166 if (len == space - attrs && !strncmp(attrs, attr, len))
1167 return 1;
1168 if (!*space)
1169 return 0;
1170 attrs = space + 1;
1171 }
1172}
1173
Brandon Williams834cf342018-03-15 10:31:22 -07001174static struct ref *get_refs_list(struct transport *transport, int for_push,
Jonathan Tan39835402021-02-05 12:48:48 -08001175 struct transport_ls_refs_options *transport_options)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001176{
Jonathan Tanac3fda82019-08-21 15:20:09 -07001177 get_helper(transport);
1178
1179 if (process_connect(transport, for_push)) {
1180 do_take_over(transport);
Jonathan Tan39835402021-02-05 12:48:48 -08001181 return transport->vtable->get_refs_list(transport, for_push,
1182 transport_options);
Jonathan Tanac3fda82019-08-21 15:20:09 -07001183 }
1184
1185 return get_refs_list_using_list(transport, for_push);
1186}
1187
1188static struct ref *get_refs_list_using_list(struct transport *transport,
1189 int for_push)
1190{
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001191 struct helper_data *data = transport->data;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001192 struct child_process *helper;
1193 struct ref *ret = NULL;
1194 struct ref **tail = &ret;
1195 struct ref *posn;
1196 struct strbuf buf = STRBUF_INIT;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001197
Jonathan Tanac3fda82019-08-21 15:20:09 -07001198 data->get_refs_list_called = 1;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001199 helper = get_helper(transport);
Daniel Barkalow2d14d652009-09-03 22:13:51 -04001200
brian m. carlson8b85ee42020-05-25 19:59:03 +00001201 if (data->object_format) {
1202 write_str_in_full(helper->in, "option object-format\n");
1203 if (recvline(data, &buf) || strcmp(buf.buf, "ok"))
1204 exit(128);
1205 }
1206
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001207 if (data->push && for_push)
1208 write_str_in_full(helper->in, "list for-push\n");
1209 else
1210 write_str_in_full(helper->in, "list\n");
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001211
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001212 while (1) {
1213 char *eov, *eon;
Felipe Contreras5931b332014-04-12 15:33:29 -05001214 if (recvline(data, &buf))
1215 exit(128);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001216
1217 if (!*buf.buf)
1218 break;
brian m. carlson8b85ee42020-05-25 19:59:03 +00001219 else if (buf.buf[0] == ':') {
1220 const char *value;
1221 if (skip_prefix(buf.buf, ":object-format ", &value)) {
1222 int algo = hash_algo_by_name(value);
1223 if (algo == GIT_HASH_UNKNOWN)
1224 die(_("unsupported object format '%s'"),
1225 value);
1226 transport->hash_algo = &hash_algos[algo];
1227 }
1228 continue;
1229 }
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001230
1231 eov = strchr(buf.buf, ' ');
1232 if (!eov)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001233 die(_("malformed response in ref list: %s"), buf.buf);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001234 eon = strchr(eov + 1, ' ');
1235 *eov = '\0';
1236 if (eon)
1237 *eon = '\0';
1238 *tail = alloc_ref(eov + 1);
1239 if (buf.buf[0] == '@')
1240 (*tail)->symref = xstrdup(buf.buf + 1);
1241 else if (buf.buf[0] != '?')
brian m. carlson8b85ee42020-05-25 19:59:03 +00001242 get_oid_hex_algop(buf.buf, &(*tail)->old_oid, transport->hash_algo);
Daniel Barkalowf8ec9162009-11-18 02:42:30 +01001243 if (eon) {
1244 if (has_attribute(eon + 1, "unchanged")) {
1245 (*tail)->status |= REF_STATUS_UPTODATE;
brian m. carlson34c290a2017-10-15 22:06:56 +00001246 if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02001247 die(_("could not read ref %s"),
Stefan Bellerae25fd32015-07-31 16:57:57 -07001248 (*tail)->name);
Daniel Barkalowf8ec9162009-11-18 02:42:30 +01001249 }
1250 }
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001251 tail = &((*tail)->next);
1252 }
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +02001253 if (debug)
1254 fprintf(stderr, "Debug: Read ref listing.\n");
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001255 strbuf_release(&buf);
1256
1257 for (posn = ret; posn; posn = posn->next)
1258 resolve_remote_symref(posn, ret);
1259
1260 return ret;
1261}
1262
Jonathan Tane967ca32017-12-14 13:44:45 -08001263static struct transport_vtable vtable = {
1264 set_helper_option,
1265 get_refs_list,
1266 fetch,
1267 push_refs,
1268 connect_helper,
1269 release_helper
1270};
1271
Daniel Barkalowc9e388b2009-09-03 22:13:49 -04001272int transport_helper_init(struct transport *transport, const char *name)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001273{
Brian Gesiak92e25b62014-05-27 00:33:56 +09001274 struct helper_data *data = xcalloc(1, sizeof(*data));
Daniel Barkalowc9e388b2009-09-03 22:13:49 -04001275 data->name = name;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001276
Jeff Kinga5adace2015-09-16 13:12:52 -04001277 transport_check_allowed(name);
1278
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +02001279 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1280 debug = 1;
1281
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001282 transport->data = data;
Jonathan Tane967ca32017-12-14 13:44:45 -08001283 transport->vtable = &vtable;
Ilari Liusvaara61b075b2009-12-09 17:26:31 +02001284 transport->smart_options = &(data->transport_options);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001285 return 0;
1286}
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001287
1288/*
1289 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1290 * buffer less), so attempt reads and writes with up to that size.
1291 */
1292#define BUFFERSIZE 65536
1293/* This should be enough to hold debugging message. */
1294#define PBUFFERSIZE 8192
1295
1296/* Print bidirectional transfer loop debug message. */
Jeff King46210852013-07-09 20:18:40 -04001297__attribute__((format (printf, 1, 2)))
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001298static void transfer_debug(const char *fmt, ...)
1299{
Martin Ågren6cdf8a72017-08-21 19:43:48 +02001300 /*
1301 * NEEDSWORK: This function is sometimes used from multiple threads, and
1302 * we end up using debug_enabled racily. That "should not matter" since
1303 * we always write the same value, but it's still wrong. This function
1304 * is listed in .tsan-suppressions for the time being.
1305 */
1306
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001307 va_list args;
1308 char msgbuf[PBUFFERSIZE];
1309 static int debug_enabled = -1;
1310
1311 if (debug_enabled < 0)
1312 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1313 if (!debug_enabled)
1314 return;
1315
1316 va_start(args, fmt);
1317 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1318 va_end(args);
1319 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1320}
1321
1322/* Stream state: More data may be coming in this direction. */
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001323#define SSTATE_TRANSFERRING 0
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001324/*
1325 * Stream state: No more data coming in this direction, flushing rest of
1326 * data.
1327 */
1328#define SSTATE_FLUSHING 1
1329/* Stream state: Transfer in this direction finished. */
1330#define SSTATE_FINISHED 2
1331
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001332#define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001333#define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1334#define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1335
1336/* Unidirectional transfer. */
1337struct unidirectional_transfer {
1338 /* Source */
1339 int src;
1340 /* Destination */
1341 int dest;
1342 /* Is source socket? */
1343 int src_is_sock;
1344 /* Is destination socket? */
1345 int dest_is_sock;
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02001346 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001347 int state;
1348 /* Buffer. */
1349 char buf[BUFFERSIZE];
1350 /* Buffer used. */
1351 size_t bufuse;
1352 /* Name of source. */
1353 const char *src_name;
1354 /* Name of destination. */
1355 const char *dest_name;
1356};
1357
1358/* Closes the target (for writing) if transfer has finished. */
1359static void udt_close_if_finished(struct unidirectional_transfer *t)
1360{
1361 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1362 t->state = SSTATE_FINISHED;
1363 if (t->dest_is_sock)
1364 shutdown(t->dest, SHUT_WR);
1365 else
1366 close(t->dest);
1367 transfer_debug("Closed %s.", t->dest_name);
1368 }
1369}
1370
1371/*
Li Peng832c0e52016-05-06 20:36:46 +08001372 * Tries to read data from source into buffer. If buffer is full,
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001373 * no data is read. Returns 0 on success, -1 on error.
1374 */
1375static int udt_do_read(struct unidirectional_transfer *t)
1376{
1377 ssize_t bytes;
1378
1379 if (t->bufuse == BUFFERSIZE)
1380 return 0; /* No space for more. */
1381
1382 transfer_debug("%s is readable", t->src_name);
Randall S. Beckerc14e5a12019-01-03 16:03:48 -05001383 bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
Jeff Kingd4c81362018-01-11 01:31:10 -05001384 if (bytes < 0) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001385 error_errno(_("read(%s) failed"), t->src_name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001386 return -1;
1387 } else if (bytes == 0) {
1388 transfer_debug("%s EOF (with %i bytes in buffer)",
Jeff King46210852013-07-09 20:18:40 -04001389 t->src_name, (int)t->bufuse);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001390 t->state = SSTATE_FLUSHING;
1391 } else if (bytes > 0) {
1392 t->bufuse += bytes;
1393 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1394 (int)bytes, t->src_name, (int)t->bufuse);
1395 }
1396 return 0;
1397}
1398
1399/* Tries to write data from buffer into destination. If buffer is empty,
1400 * no data is written. Returns 0 on success, -1 on error.
1401 */
1402static int udt_do_write(struct unidirectional_transfer *t)
1403{
Nicolas Kaiser803dbdb2011-03-05 00:16:26 +01001404 ssize_t bytes;
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001405
1406 if (t->bufuse == 0)
1407 return 0; /* Nothing to write. */
1408
1409 transfer_debug("%s is writable", t->dest_name);
Erik Faye-Lund7edc02f2014-01-17 15:17:09 +01001410 bytes = xwrite(t->dest, t->buf, t->bufuse);
Jeff Kingd4c81362018-01-11 01:31:10 -05001411 if (bytes < 0) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001412 error_errno(_("write(%s) failed"), t->dest_name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001413 return -1;
1414 } else if (bytes > 0) {
1415 t->bufuse -= bytes;
1416 if (t->bufuse)
1417 memmove(t->buf, t->buf + bytes, t->bufuse);
1418 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1419 (int)bytes, t->dest_name, (int)t->bufuse);
1420 }
1421 return 0;
1422}
1423
1424
1425/* State of bidirectional transfer loop. */
1426struct bidirectional_transfer_state {
1427 /* Direction from program to git. */
1428 struct unidirectional_transfer ptg;
1429 /* Direction from git to program. */
1430 struct unidirectional_transfer gtp;
1431};
1432
1433static void *udt_copy_task_routine(void *udt)
1434{
1435 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1436 while (t->state != SSTATE_FINISHED) {
1437 if (STATE_NEEDS_READING(t->state))
1438 if (udt_do_read(t))
1439 return NULL;
1440 if (STATE_NEEDS_WRITING(t->state))
1441 if (udt_do_write(t))
1442 return NULL;
1443 if (STATE_NEEDS_CLOSING(t->state))
1444 udt_close_if_finished(t);
1445 }
1446 return udt; /* Just some non-NULL value. */
1447}
1448
1449#ifndef NO_PTHREADS
1450
1451/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +02001452 * Join thread, with appropriate errors on failure. Name is name for the
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001453 * thread (for error messages). Returns 0 on success, 1 on failure.
1454 */
1455static int tloop_join(pthread_t thread, const char *name)
1456{
1457 int err;
1458 void *tret;
1459 err = pthread_join(thread, &tret);
1460 if (!tret) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001461 error(_("%s thread failed"), name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001462 return 1;
1463 }
1464 if (err) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001465 error(_("%s thread failed to join: %s"), name, strerror(err));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001466 return 1;
1467 }
1468 return 0;
1469}
1470
1471/*
1472 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1473 * -1 on failure.
1474 */
1475static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1476{
1477 pthread_t gtp_thread;
1478 pthread_t ptg_thread;
1479 int err;
1480 int ret = 0;
1481 err = pthread_create(&gtp_thread, NULL, udt_copy_task_routine,
1482 &s->gtp);
1483 if (err)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001484 die(_("can't start thread for copying data: %s"), strerror(err));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001485 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1486 &s->ptg);
1487 if (err)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001488 die(_("can't start thread for copying data: %s"), strerror(err));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001489
1490 ret |= tloop_join(gtp_thread, "Git to program copy");
1491 ret |= tloop_join(ptg_thread, "Program to git copy");
1492 return ret;
1493}
1494#else
1495
1496/* Close the source and target (for writing) for transfer. */
1497static void udt_kill_transfer(struct unidirectional_transfer *t)
1498{
1499 t->state = SSTATE_FINISHED;
1500 /*
1501 * Socket read end left open isn't a disaster if nobody
1502 * attempts to read from it (mingw compat headers do not
1503 * have SHUT_RD)...
1504 *
1505 * We can't fully close the socket since otherwise gtp
1506 * task would first close the socket it sends data to
1507 * while closing the ptg file descriptors.
1508 */
1509 if (!t->src_is_sock)
1510 close(t->src);
1511 if (t->dest_is_sock)
1512 shutdown(t->dest, SHUT_WR);
1513 else
1514 close(t->dest);
1515}
1516
1517/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +02001518 * Join process, with appropriate errors on failure. Name is name for the
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001519 * process (for error messages). Returns 0 on success, 1 on failure.
1520 */
1521static int tloop_join(pid_t pid, const char *name)
1522{
1523 int tret;
1524 if (waitpid(pid, &tret, 0) < 0) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001525 error_errno(_("%s process failed to wait"), name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001526 return 1;
1527 }
1528 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001529 error(_("%s process failed"), name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001530 return 1;
1531 }
1532 return 0;
1533}
1534
1535/*
1536 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1537 * -1 on failure.
1538 */
1539static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1540{
1541 pid_t pid1, pid2;
1542 int ret = 0;
1543
1544 /* Fork thread #1: git to program. */
1545 pid1 = fork();
1546 if (pid1 < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001547 die_errno(_("can't start thread for copying data"));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001548 else if (pid1 == 0) {
1549 udt_kill_transfer(&s->ptg);
1550 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1551 }
1552
1553 /* Fork thread #2: program to git. */
1554 pid2 = fork();
1555 if (pid2 < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001556 die_errno(_("can't start thread for copying data"));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001557 else if (pid2 == 0) {
1558 udt_kill_transfer(&s->gtp);
1559 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1560 }
1561
1562 /*
1563 * Close both streams in parent as to not interfere with
1564 * end of file detection and wait for both tasks to finish.
1565 */
1566 udt_kill_transfer(&s->gtp);
1567 udt_kill_transfer(&s->ptg);
1568 ret |= tloop_join(pid1, "Git to program copy");
1569 ret |= tloop_join(pid2, "Program to git copy");
1570 return ret;
1571}
1572#endif
1573
1574/*
1575 * Copies data from stdin to output and from input to stdout simultaneously.
1576 * Additionally filtering through given filter. If filter is NULL, uses
1577 * identity filter.
1578 */
1579int bidirectional_transfer_loop(int input, int output)
1580{
1581 struct bidirectional_transfer_state state;
1582
1583 /* Fill the state fields. */
1584 state.ptg.src = input;
1585 state.ptg.dest = 1;
1586 state.ptg.src_is_sock = (input == output);
1587 state.ptg.dest_is_sock = 0;
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001588 state.ptg.state = SSTATE_TRANSFERRING;
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001589 state.ptg.bufuse = 0;
1590 state.ptg.src_name = "remote input";
1591 state.ptg.dest_name = "stdout";
1592
1593 state.gtp.src = 0;
1594 state.gtp.dest = output;
1595 state.gtp.src_is_sock = 0;
1596 state.gtp.dest_is_sock = (input == output);
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001597 state.gtp.state = SSTATE_TRANSFERRING;
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001598 state.gtp.bufuse = 0;
1599 state.gtp.src_name = "stdin";
1600 state.gtp.dest_name = "remote output";
1601
1602 return tloop_spawnwait_tasks(&state);
1603}
Jiang Xindfe1b7f2020-04-17 05:45:36 -04001604
1605void reject_atomic_push(struct ref *remote_refs, int mirror_mode)
1606{
1607 struct ref *ref;
1608
1609 /* Mark other refs as failed */
1610 for (ref = remote_refs; ref; ref = ref->next) {
1611 if (!ref->peer_ref && !mirror_mode)
1612 continue;
1613
1614 switch (ref->status) {
1615 case REF_STATUS_NONE:
1616 case REF_STATUS_OK:
1617 case REF_STATUS_EXPECTING_REPORT:
1618 ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
1619 continue;
1620 default:
1621 break; /* do nothing */
1622 }
1623 }
1624 return;
1625}