blob: 8d284b24d5d5e79434932a12e38d06f15ac665da [file] [log] [blame]
Elijah Newrene93fc5d2023-04-11 00:41:50 -07001#include "git-compat-util.h"
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04002#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"
Elijah Newren32a8f512023-03-21 06:26:03 +00006#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00007#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00008#include "hex.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -07009#include "object-name.h"
Elijah Newrendf6e8742023-05-16 06:34:00 +000010#include "repository.h"
Daniel Barkalow72ff8942009-11-18 02:42:28 +010011#include "remote.h"
Sverre Rabbelier73b49a72010-03-29 11:48:27 -050012#include "string-list.h"
Ilari Liusvaara7851b1e2010-11-17 09:15:34 -080013#include "thread-utils.h"
Jeff Kingc34fe632012-02-23 05:04:34 -050014#include "sigchain.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040015#include "strvec.h"
Felipe Contreras664059f2013-04-17 23:14:33 -050016#include "refs.h"
Brandon Williamsec0cb492018-05-16 15:57:48 -070017#include "refspec.h"
Jonathan Tane967ca32017-12-14 13:44:45 -080018#include "transport-internal.h"
Brandon Williamsedc9caf2018-03-15 10:31:34 -070019#include "protocol.h"
Jeff Kingfba732c2024-01-23 20:00:56 -050020#include "packfile.h"
Ilari Liusvaara7851b1e2010-11-17 09:15:34 -080021
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020022static int debug;
23
Jonathan Nieder9cba13c2011-03-16 02:08:34 -050024struct helper_data {
Daniel Barkalow6eb996b2009-08-05 01:01:53 -040025 const char *name;
26 struct child_process *helper;
Shawn O. Pearce292ce462009-10-30 17:47:28 -070027 FILE *out;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070028 unsigned fetch : 1,
Junio C Hamanoa24a32d2009-12-06 22:40:16 -080029 import : 1,
Florian Achleitnerbfc366d2012-09-19 17:21:19 +020030 bidi_import : 1,
Sverre Rabbelier73b49a72010-03-29 11:48:27 -050031 export : 1,
Shawn O. Pearceae4efe12009-10-30 17:47:30 -070032 option : 1,
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +020033 push : 1,
34 connect : 1,
Brandon Williamsedc9caf2018-03-15 10:31:34 -070035 stateless_connect : 1,
John Keeping0d957a42013-04-14 11:57:08 +010036 signed_tags : 1,
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +070037 check_connectivity : 1,
Matthieu Moy597b8312013-09-03 17:45:14 +020038 no_disconnect_req : 1,
brian m. carlson8b85ee42020-05-25 19:59:03 +000039 no_private_update : 1,
40 object_format : 1;
Jonathan Tanac3fda82019-08-21 15:20:09 -070041
42 /*
43 * As an optimization, the transport code may invoke fetch before
44 * get_refs_list. If this happens, and if the transport helper doesn't
45 * support connect or stateless_connect, we need to invoke
46 * get_refs_list ourselves if we haven't already done so. Keep track of
47 * whether we have invoked get_refs_list.
48 */
49 unsigned get_refs_list_called : 1;
50
Sverre Rabbeliera515ebe2011-07-16 15:03:40 +020051 char *export_marks;
52 char *import_marks;
Daniel Barkalow72ff8942009-11-18 02:42:28 +010053 /* These go from remote name (as in "list") to private name */
Brandon Williams57f32ac2018-05-16 15:58:03 -070054 struct refspec rs;
Ilari Liusvaara61b075b2009-12-09 17:26:31 +020055 /* Transport options for fetch-pack/send-pack (should one of
56 * those be invoked).
57 */
58 struct git_transport_options transport_options;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -040059};
60
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020061static void sendline(struct helper_data *helper, struct strbuf *buffer)
62{
63 if (debug)
64 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
Jeff King06f46f22017-09-13 13:16:03 -040065 if (write_in_full(helper->helper->in, buffer->buf, buffer->len) < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +020066 die_errno(_("full write to remote helper failed"));
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020067}
68
Brandon Williamsb1c2edf2018-03-15 10:31:32 -070069static int recvline_fh(FILE *helper, struct strbuf *buffer)
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020070{
71 strbuf_reset(buffer);
72 if (debug)
73 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
Junio C Hamano692dfdf2015-10-28 13:36:00 -070074 if (strbuf_getline(buffer, helper) == EOF) {
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020075 if (debug)
76 fprintf(stderr, "Debug: Remote helper quit.\n");
Felipe Contreras5931b332014-04-12 15:33:29 -050077 return 1;
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020078 }
79
80 if (debug)
81 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
82 return 0;
83}
84
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +020085static int recvline(struct helper_data *helper, struct strbuf *buffer)
86{
Brandon Williamsb1c2edf2018-03-15 10:31:32 -070087 return recvline_fh(helper->out, buffer);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +020088}
89
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020090static void write_constant(int fd, const char *str)
91{
92 if (debug)
93 fprintf(stderr, "Debug: Remote helper: -> %s", str);
Jeff King06f46f22017-09-13 13:16:03 -040094 if (write_in_full(fd, str, strlen(str)) < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +020095 die_errno(_("full write to remote helper failed"));
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +020096}
97
Stephen Boydc2e86ad2011-03-22 00:51:05 -070098static const char *remove_ext_force(const char *url)
Ilari Liusvaara25d5cc42009-12-09 17:26:29 +020099{
100 if (url) {
101 const char *colon = strchr(url, ':');
102 if (colon && colon[1] == ':')
103 return colon + 2;
104 }
105 return url;
106}
107
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200108static void do_take_over(struct transport *transport)
109{
110 struct helper_data *data;
111 data = (struct helper_data *)transport->data;
112 transport_take_over(transport, data->helper);
113 fclose(data->out);
114 free(data);
115}
116
Mike Hommey2879bc32015-02-13 14:24:45 +0900117static void standard_options(struct transport *t);
118
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400119static struct child_process *get_helper(struct transport *transport)
120{
121 struct helper_data *data = transport->data;
122 struct strbuf buf = STRBUF_INIT;
123 struct child_process *helper;
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200124 int duped;
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100125 int code;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400126
127 if (data->helper)
128 return data->helper;
129
René Scharfe483bbd42014-08-19 21:10:48 +0200130 helper = xmalloc(sizeof(*helper));
131 child_process_init(helper);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400132 helper->in = -1;
133 helper->out = -1;
134 helper->err = 0;
Junio C Hamano675df192020-08-26 12:46:49 -0700135 strvec_pushf(&helper->args, "remote-%s", data->name);
Jeff Kingc972bf42020-07-28 16:25:12 -0400136 strvec_push(&helper->args, transport->remote->name);
137 strvec_push(&helper->args, remove_ext_force(transport->url));
Junio C Hamano675df192020-08-26 12:46:49 -0700138 helper->git_cmd = 1;
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100139 helper->silent_exec_failure = 1;
Dmitry Ivankove1735872011-07-16 15:03:28 +0200140
Jonathan Nieder4b0c3c72017-02-14 15:36:19 -0500141 if (have_git_dir())
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +0200142 strvec_pushf(&helper->env, "%s=%s",
Jeff Kingf6d89422020-07-28 16:26:31 -0400143 GIT_DIR_ENVIRONMENT, get_git_dir());
Dmitry Ivankove1735872011-07-16 15:03:28 +0200144
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400145 helper->trace2_child_class = helper->args.v[0]; /* "remote-<name>" */
Jeff Hostetlerabd81a32019-02-22 14:25:05 -0800146
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100147 code = start_command(helper);
148 if (code < 0 && errno == ENOENT)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200149 die(_("unable to find remote helper for '%s'"), data->name);
Ilari Liusvaara6b02de32010-01-12 20:53:29 +0100150 else if (code != 0)
151 exit(code);
152
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400153 data->helper = helper;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200154 data->no_disconnect_req = 0;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700155 refspec_init(&data->rs, REFSPEC_FETCH);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400156
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200157 /*
Junio C Hamano1a0c8df2016-01-13 18:32:23 -0800158 * Open the output as FILE* so strbuf_getline_*() family of
159 * functions can be used.
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200160 * Do this with duped fd because fclose() will close the fd,
161 * and stuff like taking over will require the fd to remain.
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200162 */
163 duped = dup(helper->out);
164 if (duped < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200165 die_errno(_("can't dup helper output fd"));
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200166 data->out = xfdopen(duped, "r");
167
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200168 write_constant(helper->in, "capabilities\n");
Daniel Barkalow2d14d652009-09-03 22:13:51 -0400169
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400170 while (1) {
Jeff King21a2d4a2014-06-18 15:47:17 -0400171 const char *capname, *arg;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200172 int mandatory = 0;
Felipe Contreras5931b332014-04-12 15:33:29 -0500173 if (recvline(data, &buf))
174 exit(128);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400175
176 if (!*buf.buf)
177 break;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200178
179 if (*buf.buf == '*') {
180 capname = buf.buf + 1;
181 mandatory = 1;
182 } else
183 capname = buf.buf;
184
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200185 if (debug)
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200186 fprintf(stderr, "Debug: Got cap %s\n", capname);
187 if (!strcmp(capname, "fetch"))
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400188 data->fetch = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200189 else if (!strcmp(capname, "option"))
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700190 data->option = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200191 else if (!strcmp(capname, "push"))
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700192 data->push = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200193 else if (!strcmp(capname, "import"))
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100194 data->import = 1;
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200195 else if (!strcmp(capname, "bidi-import"))
196 data->bidi_import = 1;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500197 else if (!strcmp(capname, "export"))
198 data->export = 1;
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700199 else if (!strcmp(capname, "check-connectivity"))
200 data->check_connectivity = 1;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700201 else if (skip_prefix(capname, "refspec ", &arg)) {
202 refspec_append(&data->rs, arg);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200203 } else if (!strcmp(capname, "connect")) {
204 data->connect = 1;
Brandon Williamsedc9caf2018-03-15 10:31:34 -0700205 } else if (!strcmp(capname, "stateless-connect")) {
206 data->stateless_connect = 1;
John Keeping0d957a42013-04-14 11:57:08 +0100207 } else if (!strcmp(capname, "signed-tags")) {
208 data->signed_tags = 1;
Jeff King21a2d4a2014-06-18 15:47:17 -0400209 } else if (skip_prefix(capname, "export-marks ", &arg)) {
210 data->export_marks = xstrdup(arg);
211 } else if (skip_prefix(capname, "import-marks ", &arg)) {
212 data->import_marks = xstrdup(arg);
Christian Couder59556542013-11-30 21:55:40 +0100213 } else if (starts_with(capname, "no-private-update")) {
Matthieu Moy597b8312013-09-03 17:45:14 +0200214 data->no_private_update = 1;
brian m. carlson8b85ee42020-05-25 19:59:03 +0000215 } else if (starts_with(capname, "object-format")) {
216 data->object_format = 1;
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200217 } else if (mandatory) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200218 die(_("unknown mandatory capability %s; this remote "
219 "helper probably needs newer version of Git"),
Ilari Liusvaara28ed5b32009-12-09 17:26:28 +0200220 capname);
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100221 }
222 }
Brandon Williams57f32ac2018-05-16 15:58:03 -0700223 if (!data->rs.nr && (data->import || data->bidi_import || data->export)) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200224 warning(_("this remote helper should implement refspec capability"));
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400225 }
Sverre Rabbelierb962dbd2009-11-18 02:42:29 +0100226 strbuf_release(&buf);
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200227 if (debug)
228 fprintf(stderr, "Debug: Capabilities complete.\n");
Mike Hommey2879bc32015-02-13 14:24:45 +0900229 standard_options(transport);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400230 return data->helper;
231}
232
233static int disconnect_helper(struct transport *transport)
234{
235 struct helper_data *data = transport->data;
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200236 int res = 0;
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200237
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400238 if (data->helper) {
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200239 if (debug)
240 fprintf(stderr, "Debug: Disconnecting.\n");
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200241 if (!data->no_disconnect_req) {
Jeff Kingc34fe632012-02-23 05:04:34 -0500242 /*
243 * Ignore write errors; there's nothing we can do,
244 * since we're about to close the pipe anyway. And the
245 * most likely error is EPIPE due to the helper dying
246 * to report an error itself.
247 */
248 sigchain_push(SIGPIPE, SIG_IGN);
249 xwrite(data->helper->in, "\n", 1);
250 sigchain_pop(SIGPIPE);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200251 }
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400252 close(data->helper->in);
Ilari Liusvaara61b075b2009-12-09 17:26:31 +0200253 close(data->helper->out);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700254 fclose(data->out);
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200255 res = finish_command(data->helper);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000256 FREE_AND_NULL(data->helper);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400257 }
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200258 return res;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400259}
260
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700261static const char *unsupported_options[] = {
262 TRANS_OPT_UPLOADPACK,
263 TRANS_OPT_RECEIVEPACK,
264 TRANS_OPT_THIN,
265 TRANS_OPT_KEEP
266 };
Felipe Contreras23cd01e2013-10-31 03:25:40 -0600267
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700268static const char *boolean_options[] = {
269 TRANS_OPT_THIN,
270 TRANS_OPT_KEEP,
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700271 TRANS_OPT_FOLLOWTAGS,
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700272 TRANS_OPT_DEEPEN_RELATIVE
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700273 };
274
Nguyễn Thái Ngọc Duy9318c5d2016-06-12 17:53:44 +0700275static int strbuf_set_helper_option(struct helper_data *data,
276 struct strbuf *buf)
277{
278 int ret;
279
280 sendline(data, buf);
281 if (recvline(data, buf))
282 exit(128);
283
284 if (!strcmp(buf->buf, "ok"))
285 ret = 0;
286 else if (starts_with(buf->buf, "error"))
287 ret = -1;
288 else if (!strcmp(buf->buf, "unsupported"))
289 ret = 1;
290 else {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200291 warning(_("%s unexpectedly said: '%s'"), data->name, buf->buf);
Nguyễn Thái Ngọc Duy9318c5d2016-06-12 17:53:44 +0700292 ret = 1;
293 }
294 return ret;
295}
296
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700297static int string_list_set_helper_option(struct helper_data *data,
298 const char *name,
299 struct string_list *list)
300{
301 struct strbuf buf = STRBUF_INIT;
302 int i, ret = 0;
303
304 for (i = 0; i < list->nr; i++) {
305 strbuf_addf(&buf, "option %s ", name);
306 quote_c_style(list->items[i].string, &buf, NULL, 0);
307 strbuf_addch(&buf, '\n');
308
309 if ((ret = strbuf_set_helper_option(data, &buf)))
310 break;
311 strbuf_reset(&buf);
312 }
313 strbuf_release(&buf);
314 return ret;
315}
316
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700317static int set_helper_option(struct transport *transport,
318 const char *name, const char *value)
319{
320 struct helper_data *data = transport->data;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700321 struct strbuf buf = STRBUF_INIT;
322 int i, ret, is_bool = 0;
323
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200324 get_helper(transport);
325
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700326 if (!data->option)
327 return 1;
328
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700329 if (!strcmp(name, "deepen-not"))
330 return string_list_set_helper_option(data, name,
331 (struct string_list *)value);
332
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700333 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
334 if (!strcmp(name, unsupported_options[i]))
335 return 1;
336 }
337
338 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
339 if (!strcmp(name, boolean_options[i])) {
340 is_bool = 1;
341 break;
342 }
343 }
344
345 strbuf_addf(&buf, "option %s ", name);
346 if (is_bool)
347 strbuf_addstr(&buf, value ? "true" : "false");
348 else
349 quote_c_style(value, &buf, NULL, 0);
350 strbuf_addch(&buf, '\n');
351
Nguyễn Thái Ngọc Duy9318c5d2016-06-12 17:53:44 +0700352 ret = strbuf_set_helper_option(data, &buf);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700353 strbuf_release(&buf);
354 return ret;
355}
356
357static void standard_options(struct transport *t)
358{
359 char buf[16];
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700360 int v = t->verbose;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700361
Tay Ray Chuand01b3c02010-02-24 20:50:26 +0800362 set_helper_option(t, "progress", t->progress ? "true" : "false");
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700363
Jeff King8c5acfb2017-03-28 15:47:00 -0400364 xsnprintf(buf, sizeof(buf), "%d", v + 1);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700365 set_helper_option(t, "verbosity", buf);
Eric Wongc915f112016-02-03 04:09:14 +0000366
367 switch (t->family) {
368 case TRANSPORT_FAMILY_ALL:
369 /*
370 * this is already the default,
371 * do not break old remote helpers by setting "all" here
372 */
373 break;
374 case TRANSPORT_FAMILY_IPV4:
375 set_helper_option(t, "family", "ipv4");
376 break;
377 case TRANSPORT_FAMILY_IPV6:
378 set_helper_option(t, "family", "ipv6");
379 break;
380 }
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700381}
382
Daniel Barkalowf2a37152009-11-18 02:42:21 +0100383static int release_helper(struct transport *transport)
384{
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200385 int res = 0;
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100386 struct helper_data *data = transport->data;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700387 refspec_clear(&data->rs);
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200388 res = disconnect_helper(transport);
Daniel Barkalowf2a37152009-11-18 02:42:21 +0100389 free(transport->data);
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200390 return res;
Daniel Barkalowf2a37152009-11-18 02:42:21 +0100391}
392
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400393static int fetch_with_fetch(struct transport *transport,
Daniel Barkalow37148312009-11-18 02:42:24 +0100394 int nr_heads, struct ref **to_fetch)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400395{
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700396 struct helper_data *data = transport->data;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400397 int i;
398 struct strbuf buf = STRBUF_INIT;
399
400 for (i = 0; i < nr_heads; i++) {
Linus Torvalds10882612009-08-05 01:01:59 -0400401 const struct ref *posn = to_fetch[i];
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400402 if (posn->status & REF_STATUS_UPTODATE)
403 continue;
Daniel Barkalow2d14d652009-09-03 22:13:51 -0400404
405 strbuf_addf(&buf, "fetch %s %s\n",
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000406 oid_to_hex(&posn->old_oid),
Mike Hommey33cae542015-01-19 10:35:07 +0900407 posn->symref ? posn->symref : posn->name);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400408 }
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700409
410 strbuf_addch(&buf, '\n');
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200411 sendline(data, &buf);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700412
413 while (1) {
Junio C Hamano145136a2020-01-30 11:35:46 -0800414 const char *name;
415
Felipe Contreras5931b332014-04-12 15:33:29 -0500416 if (recvline(data, &buf))
417 exit(128);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700418
Junio C Hamano145136a2020-01-30 11:35:46 -0800419 if (skip_prefix(buf.buf, "lock ", &name)) {
Jonathan Tan9da69a62020-06-10 13:57:22 -0700420 if (transport->pack_lockfiles.nr)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200421 warning(_("%s also locked %s"), data->name, name);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700422 else
Jonathan Tan9da69a62020-06-10 13:57:22 -0700423 string_list_append(&transport->pack_lockfiles,
424 name);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700425 }
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700426 else if (data->check_connectivity &&
427 data->transport_options.check_self_contained_and_connected &&
428 !strcmp(buf.buf, "connectivity-ok"))
429 data->transport_options.self_contained_and_connected = 1;
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700430 else if (!buf.len)
431 break;
432 else
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200433 warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf);
Shawn O. Pearce292ce462009-10-30 17:47:28 -0700434 }
435 strbuf_release(&buf);
Jeff Kingfba732c2024-01-23 20:00:56 -0500436
437 reprepare_packed_git(the_repository);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400438 return 0;
439}
440
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100441static int get_importer(struct transport *transport, struct child_process *fastimport)
442{
443 struct child_process *helper = get_helper(transport);
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200444 struct helper_data *data = transport->data;
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200445 int cat_blob_fd, code;
René Scharfe483bbd42014-08-19 21:10:48 +0200446 child_process_init(fastimport);
Mike Hommey8b355422019-05-16 09:37:35 +0900447 fastimport->in = xdup(helper->out);
Jeff Kingc972bf42020-07-28 16:25:12 -0400448 strvec_push(&fastimport->args, "fast-import");
449 strvec_push(&fastimport->args, "--allow-unsafe-features");
450 strvec_push(&fastimport->args, debug ? "--stats" : "--quiet");
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100451
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200452 if (data->bidi_import) {
453 cat_blob_fd = xdup(helper->in);
Jeff Kingc972bf42020-07-28 16:25:12 -0400454 strvec_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200455 }
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100456 fastimport->git_cmd = 1;
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200457
458 code = start_command(fastimport);
459 return code;
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100460}
461
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500462static int get_exporter(struct transport *transport,
463 struct child_process *fastexport,
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500464 struct string_list *revlist_args)
465{
Sverre Rabbeliera515ebe2011-07-16 15:03:40 +0200466 struct helper_data *data = transport->data;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500467 struct child_process *helper = get_helper(transport);
Jeff King2aeae402014-05-15 04:34:44 -0400468 int i;
Felipe Contreras852e54b2014-04-12 15:33:31 -0500469
René Scharfe8828f292014-10-28 21:52:34 +0100470 child_process_init(fastexport);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500471
472 /* we need to duplicate helper->in because we want to use it after
473 * fastexport is done with it. */
474 fastexport->out = dup(helper->in);
Jeff Kingc972bf42020-07-28 16:25:12 -0400475 strvec_push(&fastexport->args, "fast-export");
476 strvec_push(&fastexport->args, "--use-done-feature");
477 strvec_push(&fastexport->args, data->signed_tags ?
Jeff King2aeae402014-05-15 04:34:44 -0400478 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
479 if (data->export_marks)
Jeff Kingc972bf42020-07-28 16:25:12 -0400480 strvec_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
Jeff King2aeae402014-05-15 04:34:44 -0400481 if (data->import_marks)
Jeff Kingc972bf42020-07-28 16:25:12 -0400482 strvec_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500483
484 for (i = 0; i < revlist_args->nr; i++)
Jeff Kingc972bf42020-07-28 16:25:12 -0400485 strvec_push(&fastexport->args, revlist_args->items[i].string);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500486
487 fastexport->git_cmd = 1;
488 return start_command(fastexport);
489}
490
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100491static int fetch_with_import(struct transport *transport,
492 int nr_heads, struct ref **to_fetch)
493{
494 struct child_process fastimport;
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100495 struct helper_data *data = transport->data;
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100496 int i;
497 struct ref *posn;
498 struct strbuf buf = STRBUF_INIT;
499
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200500 get_helper(transport);
501
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100502 if (get_importer(transport, &fastimport))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200503 die(_("couldn't run fast-import"));
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100504
505 for (i = 0; i < nr_heads; i++) {
506 posn = to_fetch[i];
507 if (posn->status & REF_STATUS_UPTODATE)
508 continue;
509
Mike Hommey33cae542015-01-19 10:35:07 +0900510 strbuf_addf(&buf, "import %s\n",
511 posn->symref ? posn->symref : posn->name);
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +0200512 sendline(data, &buf);
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100513 strbuf_reset(&buf);
514 }
Sverre Rabbelier9504bc92011-07-16 15:03:38 +0200515
516 write_constant(data->helper->in, "\n");
Florian Achleitnerbfc366d2012-09-19 17:21:19 +0200517 /*
518 * remote-helpers that advertise the bidi-import capability are required to
519 * buffer the complete batch of import commands until this newline before
520 * sending data to fast-import.
521 * These helpers read back data from fast-import on their stdin, which could
522 * be mixed with import commands, otherwise.
523 */
Sverre Rabbelier9504bc92011-07-16 15:03:38 +0200524
Sverre Rabbeliercc567322011-07-16 15:03:35 +0200525 if (finish_command(&fastimport))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200526 die(_("error while running fast-import"));
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100527
Florian Achleitnerdff9d652012-07-30 16:31:18 +0200528 /*
529 * The fast-import stream of a remote helper that advertises
530 * the "refspec" capability writes to the refs named after the
531 * right hand side of the first refspec matching each ref we
532 * were fetching.
533 *
534 * (If no "refspec" capability was specified, for historical
Felipe Contreras7a43c552013-04-17 23:14:28 -0500535 * reasons we default to the equivalent of *:*.)
Florian Achleitnerdff9d652012-07-30 16:31:18 +0200536 *
537 * Store the result in to_fetch[i].old_sha1. Callers such
538 * as "git fetch" can use the value to write feedback to the
539 * terminal, populate FETCH_HEAD, and determine what new value
540 * should be written to peer_ref if the update is a
541 * fast-forward or this is a forced update.
542 */
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100543 for (i = 0; i < nr_heads; i++) {
Mike Hommey33cae542015-01-19 10:35:07 +0900544 char *private, *name;
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100545 posn = to_fetch[i];
546 if (posn->status & REF_STATUS_UPTODATE)
547 continue;
Mike Hommey33cae542015-01-19 10:35:07 +0900548 name = posn->symref ? posn->symref : posn->name;
Brandon Williams57f32ac2018-05-16 15:58:03 -0700549 if (data->rs.nr)
Brandon Williamsd0004142018-05-16 15:58:11 -0700550 private = apply_refspecs(&data->rs, name);
Daniel Barkalow72ff8942009-11-18 02:42:28 +0100551 else
Mike Hommey33cae542015-01-19 10:35:07 +0900552 private = xstrdup(name);
Michael Haggertyd51b7202011-09-15 23:10:38 +0200553 if (private) {
brian m. carlson34c290a2017-10-15 22:06:56 +0000554 if (read_ref(private, &posn->old_oid) < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200555 die(_("could not read ref %s"), private);
Michael Haggertyd51b7202011-09-15 23:10:38 +0200556 free(private);
557 }
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100558 }
Sverre Rabbelierb962dbd2009-11-18 02:42:29 +0100559 strbuf_release(&buf);
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100560 return 0;
561}
562
Brandon Williams176e85c2018-03-15 10:31:33 -0700563static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200564{
565 struct helper_data *data = transport->data;
Brandon Williams176e85c2018-03-15 10:31:33 -0700566 int ret = 0;
567 int duped;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200568 FILE *input;
Brandon Williams176e85c2018-03-15 10:31:33 -0700569 struct child_process *helper;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200570
571 helper = get_helper(transport);
572
573 /*
574 * Yes, dup the pipe another time, as we need unbuffered version
575 * of input pipe as FILE*. fclose() closes the underlying fd and
576 * stream buffering only can be changed before first I/O operation
577 * on it.
578 */
579 duped = dup(helper->out);
580 if (duped < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200581 die_errno(_("can't dup helper output fd"));
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200582 input = xfdopen(duped, "r");
583 setvbuf(input, NULL, _IONBF, 0);
584
Brandon Williams176e85c2018-03-15 10:31:33 -0700585 sendline(data, cmdbuf);
586 if (recvline_fh(input, cmdbuf))
587 exit(128);
588
589 if (!strcmp(cmdbuf->buf, "")) {
590 data->no_disconnect_req = 1;
591 if (debug)
592 fprintf(stderr, "Debug: Smart transport connection "
593 "ready.\n");
594 ret = 1;
595 } else if (!strcmp(cmdbuf->buf, "fallback")) {
596 if (debug)
597 fprintf(stderr, "Debug: Falling back to dumb "
598 "transport.\n");
599 } else {
Nguyễn Thái Ngọc Duy739fb712018-11-26 20:57:56 +0100600 die(_("unknown response to connect: %s"),
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200601 cmdbuf->buf);
Brandon Williams176e85c2018-03-15 10:31:33 -0700602 }
603
604 fclose(input);
605 return ret;
606}
607
608static int process_connect_service(struct transport *transport,
609 const char *name, const char *exec)
610{
611 struct helper_data *data = transport->data;
612 struct strbuf cmdbuf = STRBUF_INIT;
613 int ret = 0;
614
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200615 /*
616 * Handle --upload-pack and friends. This is fire and forget...
617 * just warn if it fails.
618 */
619 if (strcmp(name, exec)) {
Brandon Williams176e85c2018-03-15 10:31:33 -0700620 int r = set_helper_option(transport, "servpath", exec);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200621 if (r > 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200622 warning(_("setting remote service path not supported by protocol"));
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200623 else if (r < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200624 warning(_("invalid remote service path"));
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200625 }
626
Brandon Williams176e85c2018-03-15 10:31:33 -0700627 if (data->connect) {
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200628 strbuf_addf(&cmdbuf, "connect %s\n", name);
Brandon Williams176e85c2018-03-15 10:31:33 -0700629 ret = run_connect(transport, &cmdbuf);
Brandon Williamsedc9caf2018-03-15 10:31:34 -0700630 } else if (data->stateless_connect &&
631 (get_protocol_version_config() == protocol_v2) &&
Jiang Xin5c858362024-01-21 21:15:35 +0800632 (!strcmp("git-upload-pack", name) ||
633 !strcmp("git-upload-archive", name))) {
Brandon Williamsedc9caf2018-03-15 10:31:34 -0700634 strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
635 ret = run_connect(transport, &cmdbuf);
636 if (ret)
637 transport->stateless_rpc = 1;
Brandon Williams176e85c2018-03-15 10:31:33 -0700638 }
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200639
Rene Scharfe4168be82017-08-30 20:20:15 +0200640 strbuf_release(&cmdbuf);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200641 return ret;
642}
643
644static int process_connect(struct transport *transport,
645 int for_push)
646{
647 struct helper_data *data = transport->data;
648 const char *name;
649 const char *exec;
Jiang Xin176cd682024-01-21 21:15:38 +0800650 int ret;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200651
652 name = for_push ? "git-receive-pack" : "git-upload-pack";
653 if (for_push)
654 exec = data->transport_options.receivepack;
655 else
656 exec = data->transport_options.uploadpack;
657
Jiang Xin176cd682024-01-21 21:15:38 +0800658 ret = process_connect_service(transport, name, exec);
659 if (ret)
660 do_take_over(transport);
661 return ret;
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200662}
663
Ilari Liusvaarab2367522009-12-09 17:26:33 +0200664static int connect_helper(struct transport *transport, const char *name,
665 const char *exec, int fd[2])
666{
667 struct helper_data *data = transport->data;
668
669 /* Get_helper so connect is inited. */
670 get_helper(transport);
Ilari Liusvaarab2367522009-12-09 17:26:33 +0200671
672 if (!process_connect_service(transport, name, exec))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200673 die(_("can't connect to subservice %s"), name);
Ilari Liusvaarab2367522009-12-09 17:26:33 +0200674
675 fd[0] = data->helper->out;
676 fd[1] = data->helper->in;
Jiang Xin35d26e72024-01-21 21:15:37 +0800677
678 do_take_over(transport);
Ilari Liusvaarab2367522009-12-09 17:26:33 +0200679 return 0;
680}
681
Jonathan Tanac3fda82019-08-21 15:20:09 -0700682static struct ref *get_refs_list_using_list(struct transport *transport,
683 int for_push);
684
Ævar Arnfjörð Bjarmason9b1cdd32021-08-05 03:25:35 +0200685static int fetch_refs(struct transport *transport,
686 int nr_heads, struct ref **to_fetch)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400687{
688 struct helper_data *data = transport->data;
689 int i, count;
690
Jonathan Tanac3fda82019-08-21 15:20:09 -0700691 get_helper(transport);
692
Jiang Xin176cd682024-01-21 21:15:38 +0800693 if (process_connect(transport, 0))
Ævar Arnfjörð Bjarmason9b1cdd32021-08-05 03:25:35 +0200694 return transport->vtable->fetch_refs(transport, nr_heads, to_fetch);
Ilari Liusvaarafa8c0972009-12-09 17:26:32 +0200695
Jonathan Tan9c1e6572021-05-04 14:16:01 -0700696 /*
697 * If we reach here, then the server, the client, and/or the transport
698 * helper does not support protocol v2. --negotiate-only requires
699 * protocol v2.
700 */
701 if (data->transport_options.acked_commits) {
702 warning(_("--negotiate-only requires protocol v2"));
703 return -1;
704 }
705
Jonathan Tanac3fda82019-08-21 15:20:09 -0700706 if (!data->get_refs_list_called)
707 get_refs_list_using_list(transport, 0);
708
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400709 count = 0;
710 for (i = 0; i < nr_heads; i++)
711 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
712 count++;
713
714 if (!count)
715 return 0;
716
Mike Hommeyaab1beb2015-02-13 14:24:46 +0900717 if (data->check_connectivity &&
718 data->transport_options.check_self_contained_and_connected)
719 set_helper_option(transport, "check-connectivity", "true");
720
721 if (transport->cloning)
722 set_helper_option(transport, "cloning", "true");
723
724 if (data->transport_options.update_shallow)
725 set_helper_option(transport, "update-shallow", "true");
726
Robert Coup3c7bab02022-03-28 14:02:08 +0000727 if (data->transport_options.refetch)
728 set_helper_option(transport, "refetch", "true");
729
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800730 if (data->transport_options.filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700731 const char *spec = expand_list_objects_filter_spec(
732 &data->transport_options.filter_options);
733 set_helper_option(transport, "filter", spec);
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800734 }
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000735
Jonathan Tan3390e422018-07-02 15:39:44 -0700736 if (data->transport_options.negotiation_tips)
737 warning("Ignoring --negotiation-tip because the protocol does not support it.");
738
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400739 if (data->fetch)
740 return fetch_with_fetch(transport, nr_heads, to_fetch);
741
Daniel Barkalowe65e91e2009-11-18 02:42:27 +0100742 if (data->import)
743 return fetch_with_import(transport, nr_heads, to_fetch);
744
Daniel Barkalow6eb996b2009-08-05 01:01:53 -0400745 return -1;
746}
747
Jiang Xin63518a52020-08-27 11:45:46 -0400748struct push_update_ref_state {
749 struct ref *hint;
750 struct ref_push_report *report;
751 int new_report;
752};
753
Felipe Contreras664059f2013-04-17 23:14:33 -0500754static int push_update_ref_status(struct strbuf *buf,
Jiang Xin63518a52020-08-27 11:45:46 -0400755 struct push_update_ref_state *state,
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200756 struct ref *remote_refs)
757{
758 char *refname, *msg;
Felipe Contrerasf9e3c6b2013-11-12 14:56:57 -0600759 int status, forced = 0;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200760
Jiang Xin63518a52020-08-27 11:45:46 -0400761 if (starts_with(buf->buf, "option ")) {
762 struct object_id old_oid, new_oid;
763 const char *key, *val;
764 char *p;
765
766 if (!state->hint || !(state->report || state->new_report))
767 die(_("'option' without a matching 'ok/error' directive"));
768 if (state->new_report) {
769 if (!state->hint->report) {
René Scharfeca56dad2021-03-13 17:17:22 +0100770 CALLOC_ARRAY(state->hint->report, 1);
Jiang Xin63518a52020-08-27 11:45:46 -0400771 state->report = state->hint->report;
772 } else {
773 state->report = state->hint->report;
774 while (state->report->next)
775 state->report = state->report->next;
René Scharfeca56dad2021-03-13 17:17:22 +0100776 CALLOC_ARRAY(state->report->next, 1);
Jiang Xin63518a52020-08-27 11:45:46 -0400777 state->report = state->report->next;
778 }
779 state->new_report = 0;
780 }
781 key = buf->buf + 7;
782 p = strchr(key, ' ');
783 if (p)
784 *p++ = '\0';
785 val = p;
786 if (!strcmp(key, "refname"))
787 state->report->ref_name = xstrdup_or_null(val);
788 else if (!strcmp(key, "old-oid") && val &&
789 !parse_oid_hex(val, &old_oid, &val))
790 state->report->old_oid = oiddup(&old_oid);
791 else if (!strcmp(key, "new-oid") && val &&
792 !parse_oid_hex(val, &new_oid, &val))
793 state->report->new_oid = oiddup(&new_oid);
794 else if (!strcmp(key, "forced-update"))
795 state->report->forced_update = 1;
796 /* Not update remote namespace again. */
797 return 1;
798 }
799
800 state->report = NULL;
801 state->new_report = 0;
802
Christian Couder59556542013-11-30 21:55:40 +0100803 if (starts_with(buf->buf, "ok ")) {
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200804 status = REF_STATUS_OK;
805 refname = buf->buf + 3;
Christian Couder59556542013-11-30 21:55:40 +0100806 } else if (starts_with(buf->buf, "error ")) {
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200807 status = REF_STATUS_REMOTE_REJECT;
808 refname = buf->buf + 6;
809 } else
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200810 die(_("expected ok/error, helper said '%s'"), buf->buf);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200811
812 msg = strchr(refname, ' ');
813 if (msg) {
814 struct strbuf msg_buf = STRBUF_INIT;
815 const char *end;
816
817 *msg++ = '\0';
818 if (!unquote_c_style(&msg_buf, msg, &end))
819 msg = strbuf_detach(&msg_buf, NULL);
820 else
821 msg = xstrdup(msg);
822 strbuf_release(&msg_buf);
823
824 if (!strcmp(msg, "no match")) {
825 status = REF_STATUS_NONE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000826 FREE_AND_NULL(msg);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200827 }
828 else if (!strcmp(msg, "up to date")) {
829 status = REF_STATUS_UPTODATE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000830 FREE_AND_NULL(msg);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200831 }
832 else if (!strcmp(msg, "non-fast forward")) {
833 status = REF_STATUS_REJECT_NONFASTFORWARD;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000834 FREE_AND_NULL(msg);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200835 }
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600836 else if (!strcmp(msg, "already exists")) {
837 status = REF_STATUS_REJECT_ALREADY_EXISTS;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000838 FREE_AND_NULL(msg);
Chris Rorvickdbfeddb2012-11-29 19:41:37 -0600839 }
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800840 else if (!strcmp(msg, "fetch first")) {
841 status = REF_STATUS_REJECT_FETCH_FIRST;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000842 FREE_AND_NULL(msg);
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800843 }
844 else if (!strcmp(msg, "needs force")) {
845 status = REF_STATUS_REJECT_NEEDS_FORCE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000846 FREE_AND_NULL(msg);
Junio C Hamano75e5c0d2013-01-23 13:55:30 -0800847 }
Junio C Hamano631b5ef2013-07-08 14:42:40 -0700848 else if (!strcmp(msg, "stale info")) {
849 status = REF_STATUS_REJECT_STALE;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000850 FREE_AND_NULL(msg);
Junio C Hamano631b5ef2013-07-08 14:42:40 -0700851 }
Srinidhi Kaushik99a1f9a2020-10-03 17:40:44 +0530852 else if (!strcmp(msg, "remote ref updated since checkout")) {
853 status = REF_STATUS_REJECT_REMOTE_UPDATED;
854 FREE_AND_NULL(msg);
855 }
Felipe Contrerasf9e3c6b2013-11-12 14:56:57 -0600856 else if (!strcmp(msg, "forced update")) {
857 forced = 1;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000858 FREE_AND_NULL(msg);
Felipe Contrerasf9e3c6b2013-11-12 14:56:57 -0600859 }
Jeff Kingc5c34862021-10-18 15:45:56 -0400860 else if (!strcmp(msg, "expecting report")) {
861 status = REF_STATUS_EXPECTING_REPORT;
862 FREE_AND_NULL(msg);
863 }
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200864 }
865
Jiang Xin63518a52020-08-27 11:45:46 -0400866 if (state->hint)
867 state->hint = find_ref_by_name(state->hint, refname);
868 if (!state->hint)
869 state->hint = find_ref_by_name(remote_refs, refname);
870 if (!state->hint) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200871 warning(_("helper reported unexpected status of %s"), refname);
Felipe Contreras664059f2013-04-17 23:14:33 -0500872 return 1;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200873 }
874
Jiang Xin63518a52020-08-27 11:45:46 -0400875 if (state->hint->status != REF_STATUS_NONE) {
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200876 /*
877 * Earlier, the ref was marked not to be pushed, so ignore the ref
878 * status reported by the remote helper if the latter is 'no match'.
879 */
880 if (status == REF_STATUS_NONE)
Felipe Contreras664059f2013-04-17 23:14:33 -0500881 return 1;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200882 }
883
Jiang Xin63518a52020-08-27 11:45:46 -0400884 if (status == REF_STATUS_OK)
885 state->new_report = 1;
886 state->hint->status = status;
887 state->hint->forced_update |= forced;
888 state->hint->remote_status = msg;
Felipe Contreras126aac52013-05-10 07:08:30 -0500889 return !(status == REF_STATUS_OK);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200890}
891
Felipe Contreras0551a062014-04-12 15:33:30 -0500892static int push_update_refs_status(struct helper_data *data,
Felipe Contreras5a753532013-10-31 03:36:37 -0600893 struct ref *remote_refs,
894 int flags)
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200895{
Jiang Xin63518a52020-08-27 11:45:46 -0400896 struct ref *ref;
897 struct ref_push_report *report;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200898 struct strbuf buf = STRBUF_INIT;
Jiang Xin63518a52020-08-27 11:45:46 -0400899 struct push_update_ref_state state = { remote_refs, NULL, 0 };
Felipe Contreras0551a062014-04-12 15:33:30 -0500900
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200901 for (;;) {
Felipe Contreras0551a062014-04-12 15:33:30 -0500902 if (recvline(data, &buf)) {
Jiang Xin63518a52020-08-27 11:45:46 -0400903 strbuf_release(&buf);
904 return 1;
Felipe Contreras0551a062014-04-12 15:33:30 -0500905 }
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200906 if (!buf.len)
907 break;
Jiang Xin63518a52020-08-27 11:45:46 -0400908 push_update_ref_status(&buf, &state, remote_refs);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200909 }
910 strbuf_release(&buf);
Jiang Xin63518a52020-08-27 11:45:46 -0400911
912 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update)
913 return 0;
914
915 /* propagate back the update to the remote namespace */
916 for (ref = remote_refs; ref; ref = ref->next) {
917 char *private;
918
919 if (ref->status != REF_STATUS_OK)
920 continue;
921
922 if (!ref->report) {
923 private = apply_refspecs(&data->rs, ref->name);
924 if (!private)
925 continue;
926 update_ref("update by helper", private, &(ref->new_oid),
927 NULL, 0, 0);
928 free(private);
929 } else {
930 for (report = ref->report; report; report = report->next) {
931 private = apply_refspecs(&data->rs,
932 report->ref_name
933 ? report->ref_name
934 : ref->name);
935 if (!private)
936 continue;
937 update_ref("update by helper", private,
938 report->new_oid
939 ? report->new_oid
940 : &(ref->new_oid),
941 NULL, 0, 0);
942 free(private);
943 }
944 }
945 }
946 return 0;
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +0200947}
948
Dave Borowitz30261092015-08-19 11:26:46 -0400949static void set_common_push_options(struct transport *transport,
950 const char *name, int flags)
951{
952 if (flags & TRANSPORT_PUSH_DRY_RUN) {
953 if (set_helper_option(transport, "dry-run", "true") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200954 die(_("helper %s does not support dry-run"), name);
Dave Borowitz30261092015-08-19 11:26:46 -0400955 } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
956 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200957 die(_("helper %s does not support --signed"), name);
Dave Borowitz30261092015-08-19 11:26:46 -0400958 } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
959 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200960 die(_("helper %s does not support --signed=if-asked"), name);
Dave Borowitz30261092015-08-19 11:26:46 -0400961 }
Stefan Beller438fc682017-02-08 14:04:00 -0800962
brian m. carlson6f119422019-10-16 23:45:34 +0000963 if (flags & TRANSPORT_PUSH_ATOMIC)
964 if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
965 die(_("helper %s does not support --atomic"), name);
966
Srinidhi Kaushik3b990aa2020-10-03 17:40:45 +0530967 if (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES)
968 if (set_helper_option(transport, TRANS_OPT_FORCE_IF_INCLUDES, "true") != 0)
969 die(_("helper %s does not support --%s"),
970 name, TRANS_OPT_FORCE_IF_INCLUDES);
971
Stefan Beller438fc682017-02-08 14:04:00 -0800972 if (flags & TRANSPORT_PUSH_OPTIONS) {
973 struct string_list_item *item;
974 for_each_string_list_item(item, transport->push_options)
975 if (set_helper_option(transport, "push-option", item->string) != 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +0200976 die(_("helper %s does not support 'push-option'"), name);
Stefan Beller438fc682017-02-08 14:04:00 -0800977 }
Dave Borowitz30261092015-08-19 11:26:46 -0400978}
979
Sverre Rabbelier73b49a72010-03-29 11:48:27 -0500980static int push_refs_with_push(struct transport *transport,
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700981 struct ref *remote_refs, int flags)
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700982{
983 int force_all = flags & TRANSPORT_PUSH_FORCE;
984 int mirror = flags & TRANSPORT_PUSH_MIRROR;
Emily Shaffer3bca1e72019-07-11 14:19:19 -0700985 int atomic = flags & TRANSPORT_PUSH_ATOMIC;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700986 struct helper_data *data = transport->data;
987 struct strbuf buf = STRBUF_INIT;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700988 struct ref *ref;
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700989 struct string_list cas_options = STRING_LIST_INIT_DUP;
990 struct string_list_item *cas_option;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700991
Johannes Schindelinc0aa3352011-03-22 13:50:08 +0100992 get_helper(transport);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700993 if (!data->push)
994 return 1;
995
996 for (ref = remote_refs; ref; ref = ref->next) {
Tay Ray Chuan20e8b462010-01-08 10:12:42 +0800997 if (!ref->peer_ref && !mirror)
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700998 continue;
999
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001000 /* Check for statuses set by set_ref_status_for_push() */
1001 switch (ref->status) {
1002 case REF_STATUS_REJECT_NONFASTFORWARD:
Junio C Hamano631b5ef2013-07-08 14:42:40 -07001003 case REF_STATUS_REJECT_STALE:
Chris Rorvickdbfeddb2012-11-29 19:41:37 -06001004 case REF_STATUS_REJECT_ALREADY_EXISTS:
Srinidhi Kaushik99a1f9a2020-10-03 17:40:44 +05301005 case REF_STATUS_REJECT_REMOTE_UPDATED:
Emily Shaffer3bca1e72019-07-11 14:19:19 -07001006 if (atomic) {
Jiang Xindfe1b7f2020-04-17 05:45:36 -04001007 reject_atomic_push(remote_refs, mirror);
Emily Shaffer3bca1e72019-07-11 14:19:19 -07001008 string_list_clear(&cas_options, 0);
1009 return 0;
1010 } else
1011 continue;
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001012 case REF_STATUS_UPTODATE:
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001013 continue;
Tay Ray Chuan20e8b462010-01-08 10:12:42 +08001014 default:
1015 ; /* do nothing */
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001016 }
1017
1018 if (force_all)
1019 ref->force = 1;
1020
1021 strbuf_addstr(&buf, "push ");
1022 if (!ref->deletion) {
1023 if (ref->force)
1024 strbuf_addch(&buf, '+');
1025 if (ref->peer_ref)
1026 strbuf_addstr(&buf, ref->peer_ref->name);
1027 else
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001028 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001029 }
1030 strbuf_addch(&buf, ':');
1031 strbuf_addstr(&buf, ref->name);
1032 strbuf_addch(&buf, '\n');
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001033
1034 /*
1035 * The "--force-with-lease" options without explicit
1036 * values to expect have already been expanded into
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001037 * the ref->old_oid_expect[] field; we can ignore
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001038 * transport->smart_options->cas altogether and instead
1039 * can enumerate them from the refs.
1040 */
1041 if (ref->expect_old_sha1) {
1042 struct strbuf cas = STRBUF_INIT;
1043 strbuf_addf(&cas, "%s:%s",
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001044 ref->name, oid_to_hex(&ref->old_oid_expect));
René Scharfe176cb972017-12-08 18:29:31 +01001045 string_list_append_nodup(&cas_options,
1046 strbuf_detach(&cas, NULL));
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001047 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001048 }
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001049 if (buf.len == 0) {
1050 string_list_clear(&cas_options, 0);
Clemens Buchacherd8f67d22009-10-30 17:47:31 -07001051 return 0;
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001052 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001053
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001054 for_each_string_list_item(cas_option, &cas_options)
1055 set_helper_option(transport, "cas", cas_option->string);
Dave Borowitz30261092015-08-19 11:26:46 -04001056 set_common_push_options(transport, data->name, flags);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001057
1058 strbuf_addch(&buf, '\n');
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +02001059 sendline(data, &buf);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001060 strbuf_release(&buf);
René Scharfe176cb972017-12-08 18:29:31 +01001061 string_list_clear(&cas_options, 0);
Sverre Rabbelierd2e73c62011-07-16 15:03:34 +02001062
Felipe Contreras0551a062014-04-12 15:33:30 -05001063 return push_update_refs_status(data, remote_refs, flags);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001064}
1065
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001066static int push_refs_with_export(struct transport *transport,
1067 struct ref *remote_refs, int flags)
1068{
1069 struct ref *ref;
1070 struct child_process *helper, exporter;
1071 struct helper_data *data = transport->data;
Felipe Contrerasd98c8152014-04-20 13:59:25 -05001072 struct string_list revlist_args = STRING_LIST_INIT_DUP;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001073 struct strbuf buf = STRBUF_INIT;
1074
Brandon Williams57f32ac2018-05-16 15:58:03 -07001075 if (!data->rs.nr)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001076 die(_("remote-helper doesn't support push; refspec needed"));
Felipe Contreras21610d82013-04-17 23:14:30 -05001077
Dave Borowitz30261092015-08-19 11:26:46 -04001078 set_common_push_options(transport, data->name, flags);
Felipe Contreras510fa6f2013-11-12 14:56:56 -06001079 if (flags & TRANSPORT_PUSH_FORCE) {
1080 if (set_helper_option(transport, "force", "true") != 0)
Alexander Shopov3a127492024-02-16 11:15:34 +01001081 warning(_("helper %s does not support '--force'"), data->name);
Felipe Contreras510fa6f2013-11-12 14:56:56 -06001082 }
1083
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001084 helper = get_helper(transport);
1085
1086 write_constant(helper->in, "export\n");
1087
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001088 for (ref = remote_refs; ref; ref = ref->next) {
1089 char *private;
brian m. carlson27912a02015-11-10 02:22:24 +00001090 struct object_id oid;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001091
Brandon Williamsd0004142018-05-16 15:58:11 -07001092 private = apply_refspecs(&data->rs, ref->name);
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001093 if (private && !repo_get_oid(the_repository, private, &oid)) {
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001094 strbuf_addf(&buf, "^%s", private);
René Scharfe176cb972017-12-08 18:29:31 +01001095 string_list_append_nodup(&revlist_args,
1096 strbuf_detach(&buf, NULL));
brian m. carlson27912a02015-11-10 02:22:24 +00001097 oidcpy(&ref->old_oid, &oid);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001098 }
Jeff King2faa1522011-07-16 15:03:21 +02001099 free(private);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001100
Felipe Contreras67c9c782013-05-20 20:02:45 -05001101 if (ref->peer_ref) {
Felipe Contrerasd98c8152014-04-20 13:59:25 -05001102 if (strcmp(ref->name, ref->peer_ref->name)) {
Felipe Contrerasf3d03762014-04-20 13:59:29 -05001103 if (!ref->deletion) {
1104 const char *name;
1105 int flag;
Felipe Contreras9193f742014-04-20 13:59:26 -05001106
Felipe Contrerasf3d03762014-04-20 13:59:29 -05001107 /* Follow symbolic refs (mainly for HEAD). */
brian m. carlson49e61472017-10-15 22:07:09 +00001108 name = resolve_ref_unsafe(ref->peer_ref->name,
1109 RESOLVE_REF_READING,
1110 &oid, &flag);
Felipe Contrerasf3d03762014-04-20 13:59:29 -05001111 if (!name || !(flag & REF_ISSYMREF))
1112 name = ref->peer_ref->name;
1113
1114 strbuf_addf(&buf, "%s:%s", name, ref->name);
1115 } else
1116 strbuf_addf(&buf, ":%s", ref->name);
Felipe Contreras9193f742014-04-20 13:59:26 -05001117
Felipe Contrerasd98c8152014-04-20 13:59:25 -05001118 string_list_append(&revlist_args, "--refspec");
1119 string_list_append(&revlist_args, buf.buf);
1120 strbuf_release(&buf);
1121 }
Felipe Contrerasf3d03762014-04-20 13:59:29 -05001122 if (!ref->deletion)
1123 string_list_append(&revlist_args, ref->peer_ref->name);
Felipe Contreras67c9c782013-05-20 20:02:45 -05001124 }
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001125 }
1126
Sverre Rabbeliera515ebe2011-07-16 15:03:40 +02001127 if (get_exporter(transport, &exporter, &revlist_args))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001128 die(_("couldn't run fast-export"));
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001129
Felipe Contrerasd98c8152014-04-20 13:59:25 -05001130 string_list_clear(&revlist_args, 1);
1131
Sverre Rabbeliercc567322011-07-16 15:03:35 +02001132 if (finish_command(&exporter))
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001133 die(_("error while running fast-export"));
Felipe Contreras3994e642014-04-12 15:33:32 -05001134 if (push_update_refs_status(data, remote_refs, flags))
1135 return 1;
1136
1137 if (data->export_marks) {
1138 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1139 rename(buf.buf, data->export_marks);
1140 strbuf_release(&buf);
1141 }
1142
1143 return 0;
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001144}
1145
1146static int push_refs(struct transport *transport,
1147 struct ref *remote_refs, int flags)
1148{
1149 struct helper_data *data = transport->data;
1150
Jiang Xin176cd682024-01-21 21:15:38 +08001151 if (process_connect(transport, 1))
Jonathan Tane967ca32017-12-14 13:44:45 -08001152 return transport->vtable->push_refs(transport, remote_refs, flags);
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001153
1154 if (!remote_refs) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001155 fprintf(stderr,
1156 _("No refs in common and none specified; doing nothing.\n"
Johannes Schindelin4d046582020-06-24 14:46:29 +00001157 "Perhaps you should specify a branch.\n"));
Sverre Rabbelier73b49a72010-03-29 11:48:27 -05001158 return 0;
1159 }
1160
1161 if (data->push)
1162 return push_refs_with_push(transport, remote_refs, flags);
1163
1164 if (data->export)
1165 return push_refs_with_export(transport, remote_refs, flags);
1166
1167 return -1;
1168}
1169
1170
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +01001171static int has_attribute(const char *attrs, const char *attr)
1172{
Daniel Barkalowf8ec9162009-11-18 02:42:30 +01001173 int len;
1174 if (!attrs)
1175 return 0;
1176
1177 len = strlen(attr);
1178 for (;;) {
1179 const char *space = strchrnul(attrs, ' ');
1180 if (len == space - attrs && !strncmp(attrs, attr, len))
1181 return 1;
1182 if (!*space)
1183 return 0;
1184 attrs = space + 1;
1185 }
1186}
1187
Brandon Williams834cf342018-03-15 10:31:22 -07001188static struct ref *get_refs_list(struct transport *transport, int for_push,
Jonathan Tan39835402021-02-05 12:48:48 -08001189 struct transport_ls_refs_options *transport_options)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001190{
Jonathan Tanac3fda82019-08-21 15:20:09 -07001191 get_helper(transport);
1192
Jiang Xin176cd682024-01-21 21:15:38 +08001193 if (process_connect(transport, for_push))
Jonathan Tan39835402021-02-05 12:48:48 -08001194 return transport->vtable->get_refs_list(transport, for_push,
1195 transport_options);
Jonathan Tanac3fda82019-08-21 15:20:09 -07001196
1197 return get_refs_list_using_list(transport, for_push);
1198}
1199
1200static struct ref *get_refs_list_using_list(struct transport *transport,
1201 int for_push)
1202{
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001203 struct helper_data *data = transport->data;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001204 struct child_process *helper;
1205 struct ref *ret = NULL;
1206 struct ref **tail = &ret;
1207 struct ref *posn;
1208 struct strbuf buf = STRBUF_INIT;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001209
Jonathan Tanac3fda82019-08-21 15:20:09 -07001210 data->get_refs_list_called = 1;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001211 helper = get_helper(transport);
Daniel Barkalow2d14d652009-09-03 22:13:51 -04001212
Jeff Kingb5b7b172024-03-20 05:41:03 -04001213 if (data->object_format)
1214 set_helper_option(transport, "object-format", "true");
brian m. carlson8b85ee42020-05-25 19:59:03 +00001215
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001216 if (data->push && for_push)
Jeff Kingcf7335f2024-03-20 05:34:17 -04001217 write_constant(helper->in, "list for-push\n");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001218 else
Jeff Kingcf7335f2024-03-20 05:34:17 -04001219 write_constant(helper->in, "list\n");
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001220
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001221 while (1) {
1222 char *eov, *eon;
Felipe Contreras5931b332014-04-12 15:33:29 -05001223 if (recvline(data, &buf))
1224 exit(128);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001225
1226 if (!*buf.buf)
1227 break;
brian m. carlson8b85ee42020-05-25 19:59:03 +00001228 else if (buf.buf[0] == ':') {
1229 const char *value;
1230 if (skip_prefix(buf.buf, ":object-format ", &value)) {
1231 int algo = hash_algo_by_name(value);
1232 if (algo == GIT_HASH_UNKNOWN)
1233 die(_("unsupported object format '%s'"),
1234 value);
1235 transport->hash_algo = &hash_algos[algo];
1236 }
1237 continue;
1238 }
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001239
1240 eov = strchr(buf.buf, ' ');
1241 if (!eov)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001242 die(_("malformed response in ref list: %s"), buf.buf);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001243 eon = strchr(eov + 1, ' ');
1244 *eov = '\0';
1245 if (eon)
1246 *eon = '\0';
1247 *tail = alloc_ref(eov + 1);
1248 if (buf.buf[0] == '@')
1249 (*tail)->symref = xstrdup(buf.buf + 1);
1250 else if (buf.buf[0] != '?')
brian m. carlson8b85ee42020-05-25 19:59:03 +00001251 get_oid_hex_algop(buf.buf, &(*tail)->old_oid, transport->hash_algo);
Daniel Barkalowf8ec9162009-11-18 02:42:30 +01001252 if (eon) {
1253 if (has_attribute(eon + 1, "unchanged")) {
1254 (*tail)->status |= REF_STATUS_UPTODATE;
brian m. carlson34c290a2017-10-15 22:06:56 +00001255 if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +02001256 die(_("could not read ref %s"),
Stefan Bellerae25fd32015-07-31 16:57:57 -07001257 (*tail)->name);
Daniel Barkalowf8ec9162009-11-18 02:42:30 +01001258 }
1259 }
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001260 tail = &((*tail)->next);
1261 }
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +02001262 if (debug)
1263 fprintf(stderr, "Debug: Read ref listing.\n");
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001264 strbuf_release(&buf);
1265
1266 for (posn = ret; posn; posn = posn->next)
1267 resolve_remote_symref(posn, ret);
1268
1269 return ret;
1270}
1271
Ævar Arnfjörð Bjarmason0cfde742022-12-22 15:14:09 +00001272static int get_bundle_uri(struct transport *transport)
1273{
1274 get_helper(transport);
1275
Jiang Xin176cd682024-01-21 21:15:38 +08001276 if (process_connect(transport, 0))
Ævar Arnfjörð Bjarmason0cfde742022-12-22 15:14:09 +00001277 return transport->vtable->get_bundle_uri(transport);
Ævar Arnfjörð Bjarmason0cfde742022-12-22 15:14:09 +00001278
1279 return -1;
1280}
1281
Jonathan Tane967ca32017-12-14 13:44:45 -08001282static struct transport_vtable vtable = {
Ævar Arnfjörð Bjarmason1fd88222021-08-05 03:25:36 +02001283 .set_option = set_helper_option,
1284 .get_refs_list = get_refs_list,
Ævar Arnfjörð Bjarmason0cfde742022-12-22 15:14:09 +00001285 .get_bundle_uri = get_bundle_uri,
Ævar Arnfjörð Bjarmason1fd88222021-08-05 03:25:36 +02001286 .fetch_refs = fetch_refs,
1287 .push_refs = push_refs,
1288 .connect = connect_helper,
1289 .disconnect = release_helper
Jonathan Tane967ca32017-12-14 13:44:45 -08001290};
1291
Daniel Barkalowc9e388b2009-09-03 22:13:49 -04001292int transport_helper_init(struct transport *transport, const char *name)
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001293{
Brian Gesiak92e25b62014-05-27 00:33:56 +09001294 struct helper_data *data = xcalloc(1, sizeof(*data));
Daniel Barkalowc9e388b2009-09-03 22:13:49 -04001295 data->name = name;
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001296
Jeff Kinga5adace2015-09-16 13:12:52 -04001297 transport_check_allowed(name);
1298
Ilari Liusvaarabf3c5232009-12-09 17:26:27 +02001299 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1300 debug = 1;
1301
Jeff King2a01bde2022-09-11 01:03:07 -04001302 list_objects_filter_init(&data->transport_options.filter_options);
1303
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001304 transport->data = data;
Jonathan Tane967ca32017-12-14 13:44:45 -08001305 transport->vtable = &vtable;
Ilari Liusvaara61b075b2009-12-09 17:26:31 +02001306 transport->smart_options = &(data->transport_options);
Daniel Barkalow6eb996b2009-08-05 01:01:53 -04001307 return 0;
1308}
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001309
1310/*
1311 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1312 * buffer less), so attempt reads and writes with up to that size.
1313 */
1314#define BUFFERSIZE 65536
1315/* This should be enough to hold debugging message. */
1316#define PBUFFERSIZE 8192
1317
1318/* Print bidirectional transfer loop debug message. */
Jeff King46210852013-07-09 20:18:40 -04001319__attribute__((format (printf, 1, 2)))
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001320static void transfer_debug(const char *fmt, ...)
1321{
Martin Ågren6cdf8a72017-08-21 19:43:48 +02001322 /*
1323 * NEEDSWORK: This function is sometimes used from multiple threads, and
1324 * we end up using debug_enabled racily. That "should not matter" since
1325 * we always write the same value, but it's still wrong. This function
1326 * is listed in .tsan-suppressions for the time being.
1327 */
1328
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001329 va_list args;
1330 char msgbuf[PBUFFERSIZE];
1331 static int debug_enabled = -1;
1332
1333 if (debug_enabled < 0)
1334 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1335 if (!debug_enabled)
1336 return;
1337
1338 va_start(args, fmt);
1339 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1340 va_end(args);
1341 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1342}
1343
1344/* Stream state: More data may be coming in this direction. */
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001345#define SSTATE_TRANSFERRING 0
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001346/*
1347 * Stream state: No more data coming in this direction, flushing rest of
1348 * data.
1349 */
1350#define SSTATE_FLUSHING 1
1351/* Stream state: Transfer in this direction finished. */
1352#define SSTATE_FINISHED 2
1353
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001354#define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001355#define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1356#define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1357
1358/* Unidirectional transfer. */
1359struct unidirectional_transfer {
1360 /* Source */
1361 int src;
1362 /* Destination */
1363 int dest;
1364 /* Is source socket? */
1365 int src_is_sock;
1366 /* Is destination socket? */
1367 int dest_is_sock;
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02001368 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001369 int state;
1370 /* Buffer. */
1371 char buf[BUFFERSIZE];
1372 /* Buffer used. */
1373 size_t bufuse;
1374 /* Name of source. */
1375 const char *src_name;
1376 /* Name of destination. */
1377 const char *dest_name;
1378};
1379
1380/* Closes the target (for writing) if transfer has finished. */
1381static void udt_close_if_finished(struct unidirectional_transfer *t)
1382{
1383 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1384 t->state = SSTATE_FINISHED;
1385 if (t->dest_is_sock)
1386 shutdown(t->dest, SHUT_WR);
1387 else
1388 close(t->dest);
1389 transfer_debug("Closed %s.", t->dest_name);
1390 }
1391}
1392
1393/*
Li Peng832c0e52016-05-06 20:36:46 +08001394 * Tries to read data from source into buffer. If buffer is full,
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001395 * no data is read. Returns 0 on success, -1 on error.
1396 */
1397static int udt_do_read(struct unidirectional_transfer *t)
1398{
1399 ssize_t bytes;
1400
1401 if (t->bufuse == BUFFERSIZE)
1402 return 0; /* No space for more. */
1403
1404 transfer_debug("%s is readable", t->src_name);
Randall S. Beckerc14e5a12019-01-03 16:03:48 -05001405 bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
Jeff Kingd4c81362018-01-11 01:31:10 -05001406 if (bytes < 0) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001407 error_errno(_("read(%s) failed"), t->src_name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001408 return -1;
1409 } else if (bytes == 0) {
1410 transfer_debug("%s EOF (with %i bytes in buffer)",
Jeff King46210852013-07-09 20:18:40 -04001411 t->src_name, (int)t->bufuse);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001412 t->state = SSTATE_FLUSHING;
1413 } else if (bytes > 0) {
1414 t->bufuse += bytes;
1415 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1416 (int)bytes, t->src_name, (int)t->bufuse);
1417 }
1418 return 0;
1419}
1420
1421/* Tries to write data from buffer into destination. If buffer is empty,
1422 * no data is written. Returns 0 on success, -1 on error.
1423 */
1424static int udt_do_write(struct unidirectional_transfer *t)
1425{
Nicolas Kaiser803dbdb2011-03-05 00:16:26 +01001426 ssize_t bytes;
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001427
1428 if (t->bufuse == 0)
1429 return 0; /* Nothing to write. */
1430
1431 transfer_debug("%s is writable", t->dest_name);
Erik Faye-Lund7edc02f2014-01-17 15:17:09 +01001432 bytes = xwrite(t->dest, t->buf, t->bufuse);
Jeff Kingd4c81362018-01-11 01:31:10 -05001433 if (bytes < 0) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001434 error_errno(_("write(%s) failed"), t->dest_name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001435 return -1;
1436 } else if (bytes > 0) {
1437 t->bufuse -= bytes;
1438 if (t->bufuse)
1439 memmove(t->buf, t->buf + bytes, t->bufuse);
1440 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1441 (int)bytes, t->dest_name, (int)t->bufuse);
1442 }
1443 return 0;
1444}
1445
1446
1447/* State of bidirectional transfer loop. */
1448struct bidirectional_transfer_state {
1449 /* Direction from program to git. */
1450 struct unidirectional_transfer ptg;
1451 /* Direction from git to program. */
1452 struct unidirectional_transfer gtp;
1453};
1454
1455static void *udt_copy_task_routine(void *udt)
1456{
1457 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1458 while (t->state != SSTATE_FINISHED) {
1459 if (STATE_NEEDS_READING(t->state))
1460 if (udt_do_read(t))
1461 return NULL;
1462 if (STATE_NEEDS_WRITING(t->state))
1463 if (udt_do_write(t))
1464 return NULL;
1465 if (STATE_NEEDS_CLOSING(t->state))
1466 udt_close_if_finished(t);
1467 }
1468 return udt; /* Just some non-NULL value. */
1469}
1470
1471#ifndef NO_PTHREADS
1472
1473/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +02001474 * Join thread, with appropriate errors on failure. Name is name for the
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001475 * thread (for error messages). Returns 0 on success, 1 on failure.
1476 */
1477static int tloop_join(pthread_t thread, const char *name)
1478{
1479 int err;
1480 void *tret;
1481 err = pthread_join(thread, &tret);
1482 if (!tret) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001483 error(_("%s thread failed"), name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001484 return 1;
1485 }
1486 if (err) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001487 error(_("%s thread failed to join: %s"), name, strerror(err));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001488 return 1;
1489 }
1490 return 0;
1491}
1492
1493/*
1494 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1495 * -1 on failure.
1496 */
1497static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1498{
1499 pthread_t gtp_thread;
1500 pthread_t ptg_thread;
1501 int err;
1502 int ret = 0;
1503 err = pthread_create(&gtp_thread, NULL, udt_copy_task_routine,
1504 &s->gtp);
1505 if (err)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001506 die(_("can't start thread for copying data: %s"), strerror(err));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001507 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1508 &s->ptg);
1509 if (err)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001510 die(_("can't start thread for copying data: %s"), strerror(err));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001511
1512 ret |= tloop_join(gtp_thread, "Git to program copy");
1513 ret |= tloop_join(ptg_thread, "Program to git copy");
1514 return ret;
1515}
1516#else
1517
1518/* Close the source and target (for writing) for transfer. */
1519static void udt_kill_transfer(struct unidirectional_transfer *t)
1520{
1521 t->state = SSTATE_FINISHED;
1522 /*
1523 * Socket read end left open isn't a disaster if nobody
1524 * attempts to read from it (mingw compat headers do not
1525 * have SHUT_RD)...
1526 *
1527 * We can't fully close the socket since otherwise gtp
1528 * task would first close the socket it sends data to
1529 * while closing the ptg file descriptors.
1530 */
1531 if (!t->src_is_sock)
1532 close(t->src);
1533 if (t->dest_is_sock)
1534 shutdown(t->dest, SHUT_WR);
1535 else
1536 close(t->dest);
1537}
1538
1539/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +02001540 * Join process, with appropriate errors on failure. Name is name for the
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001541 * process (for error messages). Returns 0 on success, 1 on failure.
1542 */
1543static int tloop_join(pid_t pid, const char *name)
1544{
1545 int tret;
1546 if (waitpid(pid, &tret, 0) < 0) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001547 error_errno(_("%s process failed to wait"), name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001548 return 1;
1549 }
1550 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001551 error(_("%s process failed"), name);
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001552 return 1;
1553 }
1554 return 0;
1555}
1556
1557/*
1558 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1559 * -1 on failure.
1560 */
1561static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1562{
1563 pid_t pid1, pid2;
1564 int ret = 0;
1565
1566 /* Fork thread #1: git to program. */
1567 pid1 = fork();
1568 if (pid1 < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001569 die_errno(_("can't start thread for copying data"));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001570 else if (pid1 == 0) {
1571 udt_kill_transfer(&s->ptg);
1572 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1573 }
1574
1575 /* Fork thread #2: program to git. */
1576 pid2 = fork();
1577 if (pid2 < 0)
Nguyễn Thái Ngọc Duy6b5b3092018-07-21 09:49:41 +02001578 die_errno(_("can't start thread for copying data"));
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001579 else if (pid2 == 0) {
1580 udt_kill_transfer(&s->gtp);
1581 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1582 }
1583
1584 /*
1585 * Close both streams in parent as to not interfere with
1586 * end of file detection and wait for both tasks to finish.
1587 */
1588 udt_kill_transfer(&s->gtp);
1589 udt_kill_transfer(&s->ptg);
1590 ret |= tloop_join(pid1, "Git to program copy");
1591 ret |= tloop_join(pid2, "Program to git copy");
1592 return ret;
1593}
1594#endif
1595
1596/*
1597 * Copies data from stdin to output and from input to stdout simultaneously.
1598 * Additionally filtering through given filter. If filter is NULL, uses
1599 * identity filter.
1600 */
1601int bidirectional_transfer_loop(int input, int output)
1602{
1603 struct bidirectional_transfer_state state;
1604
1605 /* Fill the state fields. */
1606 state.ptg.src = input;
1607 state.ptg.dest = 1;
1608 state.ptg.src_is_sock = (input == output);
1609 state.ptg.dest_is_sock = 0;
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001610 state.ptg.state = SSTATE_TRANSFERRING;
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001611 state.ptg.bufuse = 0;
1612 state.ptg.src_name = "remote input";
1613 state.ptg.dest_name = "stdout";
1614
1615 state.gtp.src = 0;
1616 state.gtp.dest = output;
1617 state.gtp.src_is_sock = 0;
1618 state.gtp.dest_is_sock = (input == output);
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001619 state.gtp.state = SSTATE_TRANSFERRING;
Ilari Liusvaara419f37d2010-10-12 19:39:41 +03001620 state.gtp.bufuse = 0;
1621 state.gtp.src_name = "stdin";
1622 state.gtp.dest_name = "remote output";
1623
1624 return tloop_spawnwait_tasks(&state);
1625}
Jiang Xindfe1b7f2020-04-17 05:45:36 -04001626
1627void reject_atomic_push(struct ref *remote_refs, int mirror_mode)
1628{
1629 struct ref *ref;
1630
1631 /* Mark other refs as failed */
1632 for (ref = remote_refs; ref; ref = ref->next) {
1633 if (!ref->peer_ref && !mirror_mode)
1634 continue;
1635
1636 switch (ref->status) {
1637 case REF_STATUS_NONE:
1638 case REF_STATUS_OK:
1639 case REF_STATUS_EXPECTING_REPORT:
1640 ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
1641 continue;
1642 default:
1643 break; /* do nothing */
1644 }
1645 }
1646 return;
1647}