]>
Commit | Line | Data |
---|---|---|
eef65c71 | 1 | #include "git-compat-util.h" |
b2141fc1 | 2 | #include "config.h" |
f5d942e1 | 3 | #include "commit.h" |
d4a4f929 | 4 | #include "date.h" |
f394e093 | 5 | #include "gettext.h" |
41771fa4 | 6 | #include "hex.h" |
68cd492a | 7 | #include "object-store.h" |
f5d942e1 NTND |
8 | #include "pkt-line.h" |
9 | #include "sideband.h" | |
10 | #include "run-command.h" | |
11 | #include "remote.h" | |
47a59185 | 12 | #include "connect.h" |
f5d942e1 | 13 | #include "send-pack.h" |
f5d942e1 NTND |
14 | #include "transport.h" |
15 | #include "version.h" | |
fe299ec5 | 16 | #include "oid-array.h" |
a85b377d | 17 | #include "gpg-interface.h" |
120ad2b0 | 18 | #include "shallow.h" |
49fd5511 | 19 | #include "parse-options.h" |
d48be35c EN |
20 | #include "trace2.h" |
21 | #include "write-or-die.h" | |
30261094 DB |
22 | |
23 | int option_parse_push_signed(const struct option *opt, | |
24 | const char *arg, int unset) | |
25 | { | |
26 | if (unset) { | |
27 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER; | |
28 | return 0; | |
29 | } | |
30 | switch (git_parse_maybe_bool(arg)) { | |
31 | case 1: | |
32 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_ALWAYS; | |
33 | return 0; | |
34 | case 0: | |
35 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_NEVER; | |
36 | return 0; | |
37 | } | |
38 | if (!strcasecmp("if-asked", arg)) { | |
39 | *(int *)(opt->value) = SEND_PACK_PUSH_CERT_IF_ASKED; | |
40 | return 0; | |
41 | } | |
42 | die("bad %s argument: %s", opt->long_name, arg); | |
43 | } | |
f5d942e1 | 44 | |
5ee907bb PS |
45 | static void feed_object(struct repository *r, |
46 | const struct object_id *oid, FILE *fh, int negative) | |
f5d942e1 | 47 | { |
062b914c | 48 | if (negative && !has_object(r, oid, 0)) |
f0bca72d | 49 | return; |
f5d942e1 | 50 | |
f5d942e1 | 51 | if (negative) |
f0bca72d | 52 | putc('^', fh); |
246d7400 | 53 | fputs(oid_to_hex(oid), fh); |
f0bca72d | 54 | putc('\n', fh); |
f5d942e1 NTND |
55 | } |
56 | ||
57 | /* | |
58 | * Make a pack stream and spit it out into file descriptor fd | |
59 | */ | |
5ee907bb PS |
60 | static int pack_objects(struct repository *r, |
61 | int fd, struct ref *refs, struct oid_array *advertised, | |
477673d6 JT |
62 | struct oid_array *negotiated, |
63 | struct send_pack_args *args) | |
f5d942e1 NTND |
64 | { |
65 | /* | |
66 | * The child becomes pack-objects --revs; we feed | |
67 | * the revision parameters to it via its stdin and | |
68 | * let its stdout go back to the other end. | |
69 | */ | |
d3180279 | 70 | struct child_process po = CHILD_PROCESS_INIT; |
f0bca72d | 71 | FILE *po_in; |
d1a13d3f | 72 | int rc; |
f5d942e1 | 73 | |
5ee907bb | 74 | trace2_region_enter("send_pack", "pack_objects", r); |
c972bf4c JK |
75 | strvec_push(&po.args, "pack-objects"); |
76 | strvec_push(&po.args, "--all-progress-implied"); | |
77 | strvec_push(&po.args, "--revs"); | |
78 | strvec_push(&po.args, "--stdout"); | |
f5d942e1 | 79 | if (args->use_thin_pack) |
c972bf4c | 80 | strvec_push(&po.args, "--thin"); |
f5d942e1 | 81 | if (args->use_ofs_delta) |
c972bf4c | 82 | strvec_push(&po.args, "--delta-base-offset"); |
f5d942e1 | 83 | if (args->quiet || !args->progress) |
c972bf4c | 84 | strvec_push(&po.args, "-q"); |
f5d942e1 | 85 | if (args->progress) |
c972bf4c | 86 | strvec_push(&po.args, "--progress"); |
5ee907bb | 87 | if (is_repository_shallow(r)) |
c972bf4c | 88 | strvec_push(&po.args, "--shallow"); |
82f67ee1 KZ |
89 | if (args->disable_bitmaps) |
90 | strvec_push(&po.args, "--no-use-bitmap-index"); | |
f5d942e1 NTND |
91 | po.in = -1; |
92 | po.out = args->stateless_rpc ? -1 : fd; | |
93 | po.git_cmd = 1; | |
8b599351 | 94 | po.clean_on_exit = 1; |
f5d942e1 NTND |
95 | if (start_command(&po)) |
96 | die_errno("git pack-objects failed"); | |
97 | ||
98 | /* | |
99 | * We feed the pack-objects we just spawned with revision | |
100 | * parameters by writing to the pipe. | |
101 | */ | |
f0bca72d | 102 | po_in = xfdopen(po.in, "w"); |
80c9e70e | 103 | for (size_t i = 0; i < advertised->nr; i++) |
5ee907bb | 104 | feed_object(r, &advertised->oid[i], po_in, 1); |
80c9e70e | 105 | for (size_t i = 0; i < negotiated->nr; i++) |
5ee907bb | 106 | feed_object(r, &negotiated->oid[i], po_in, 1); |
f5d942e1 NTND |
107 | |
108 | while (refs) { | |
f0bca72d | 109 | if (!is_null_oid(&refs->old_oid)) |
5ee907bb | 110 | feed_object(r, &refs->old_oid, po_in, 1); |
f0bca72d | 111 | if (!is_null_oid(&refs->new_oid)) |
5ee907bb | 112 | feed_object(r, &refs->new_oid, po_in, 0); |
f5d942e1 NTND |
113 | refs = refs->next; |
114 | } | |
115 | ||
f0bca72d JK |
116 | fflush(po_in); |
117 | if (ferror(po_in)) | |
118 | die_errno("error writing to pack-objects"); | |
119 | fclose(po_in); | |
f5d942e1 NTND |
120 | |
121 | if (args->stateless_rpc) { | |
122 | char *buf = xmalloc(LARGE_PACKET_MAX); | |
123 | while (1) { | |
124 | ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX); | |
125 | if (n <= 0) | |
126 | break; | |
127 | send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX); | |
128 | } | |
129 | free(buf); | |
130 | close(po.out); | |
131 | po.out = -1; | |
132 | } | |
133 | ||
d1a13d3f JK |
134 | rc = finish_command(&po); |
135 | if (rc) { | |
136 | /* | |
137 | * For a normal non-zero exit, we assume pack-objects wrote | |
138 | * something useful to stderr. For death by signal, though, | |
139 | * we should mention it to the user. The exception is SIGPIPE | |
64127575 | 140 | * (141), because that's a normal occurrence if the remote end |
d1a13d3f JK |
141 | * hangs up (and we'll report that by trying to read the unpack |
142 | * status). | |
143 | */ | |
144 | if (rc > 128 && rc != 141) | |
145 | error("pack-objects died of signal %d", rc - 128); | |
5ee907bb | 146 | trace2_region_leave("send_pack", "pack_objects", r); |
f5d942e1 | 147 | return -1; |
d1a13d3f | 148 | } |
5ee907bb | 149 | trace2_region_leave("send_pack", "pack_objects", r); |
f5d942e1 NTND |
150 | return 0; |
151 | } | |
152 | ||
01f9ec64 | 153 | static int receive_unpack_status(struct packet_reader *reader) |
f5d942e1 | 154 | { |
01f9ec64 | 155 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
bb1356dc | 156 | return error(_("unexpected flush packet while reading remote unpack status")); |
01f9ec64 MS |
157 | if (!skip_prefix(reader->line, "unpack ", &reader->line)) |
158 | return error(_("unable to parse remote unpack status: %s"), reader->line); | |
159 | if (strcmp(reader->line, "ok")) | |
160 | return error(_("remote unpack failed: %s"), reader->line); | |
7c39df29 JK |
161 | return 0; |
162 | } | |
163 | ||
5ee907bb PS |
164 | static int receive_status(struct repository *r, |
165 | struct packet_reader *reader, struct ref *refs) | |
7c39df29 JK |
166 | { |
167 | struct ref *hint; | |
168 | int ret; | |
63518a57 JX |
169 | struct ref_push_report *report = NULL; |
170 | int new_report = 0; | |
171 | int once = 0; | |
7c39df29 | 172 | |
5ee907bb | 173 | trace2_region_enter("send_pack", "receive_status", r); |
f5d942e1 | 174 | hint = NULL; |
01f9ec64 | 175 | ret = receive_unpack_status(reader); |
f5d942e1 | 176 | while (1) { |
63518a57 JX |
177 | struct object_id old_oid, new_oid; |
178 | const char *head; | |
01f9ec64 | 179 | const char *refname; |
63518a57 | 180 | char *p; |
01f9ec64 | 181 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
f5d942e1 | 182 | break; |
63518a57 JX |
183 | head = reader->line; |
184 | p = strchr(head, ' '); | |
185 | if (!p) { | |
186 | error("invalid status line from remote: %s", reader->line); | |
f5d942e1 NTND |
187 | ret = -1; |
188 | break; | |
189 | } | |
63518a57 JX |
190 | *p++ = '\0'; |
191 | ||
192 | if (!strcmp(head, "option")) { | |
193 | const char *key, *val; | |
194 | ||
195 | if (!hint || !(report || new_report)) { | |
196 | if (!once++) | |
197 | error("'option' without a matching 'ok/ng' directive"); | |
198 | ret = -1; | |
199 | continue; | |
200 | } | |
201 | if (new_report) { | |
202 | if (!hint->report) { | |
ca56dadb | 203 | CALLOC_ARRAY(hint->report, 1); |
63518a57 JX |
204 | report = hint->report; |
205 | } else { | |
206 | report = hint->report; | |
207 | while (report->next) | |
208 | report = report->next; | |
ca56dadb | 209 | CALLOC_ARRAY(report->next, 1); |
63518a57 JX |
210 | report = report->next; |
211 | } | |
212 | new_report = 0; | |
213 | } | |
214 | key = p; | |
215 | p = strchr(key, ' '); | |
216 | if (p) | |
217 | *p++ = '\0'; | |
218 | val = p; | |
219 | if (!strcmp(key, "refname")) | |
220 | report->ref_name = xstrdup_or_null(val); | |
221 | else if (!strcmp(key, "old-oid") && val && | |
5ee907bb | 222 | !parse_oid_hex_algop(val, &old_oid, &val, r->hash_algo)) |
63518a57 JX |
223 | report->old_oid = oiddup(&old_oid); |
224 | else if (!strcmp(key, "new-oid") && val && | |
5ee907bb | 225 | !parse_oid_hex_algop(val, &new_oid, &val, r->hash_algo)) |
63518a57 JX |
226 | report->new_oid = oiddup(&new_oid); |
227 | else if (!strcmp(key, "forced-update")) | |
228 | report->forced_update = 1; | |
229 | continue; | |
230 | } | |
f5d942e1 | 231 | |
63518a57 JX |
232 | report = NULL; |
233 | new_report = 0; | |
234 | if (strcmp(head, "ok") && strcmp(head, "ng")) { | |
235 | error("invalid ref status from remote: %s", head); | |
236 | ret = -1; | |
237 | break; | |
238 | } | |
239 | refname = p; | |
240 | p = strchr(refname, ' '); | |
241 | if (p) | |
242 | *p++ = '\0'; | |
f5d942e1 NTND |
243 | /* first try searching at our hint, falling back to all refs */ |
244 | if (hint) | |
245 | hint = find_ref_by_name(hint, refname); | |
246 | if (!hint) | |
247 | hint = find_ref_by_name(refs, refname); | |
248 | if (!hint) { | |
249 | warning("remote reported status on unknown ref: %s", | |
63518a57 | 250 | refname); |
f5d942e1 NTND |
251 | continue; |
252 | } | |
63518a57 JX |
253 | if (hint->status != REF_STATUS_EXPECTING_REPORT && |
254 | hint->status != REF_STATUS_OK && | |
255 | hint->status != REF_STATUS_REMOTE_REJECT) { | |
f5d942e1 | 256 | warning("remote reported status on unexpected ref: %s", |
63518a57 | 257 | refname); |
f5d942e1 NTND |
258 | continue; |
259 | } | |
63518a57 | 260 | if (!strcmp(head, "ng")) { |
f5d942e1 | 261 | hint->status = REF_STATUS_REMOTE_REJECT; |
63518a57 JX |
262 | if (p) |
263 | hint->remote_status = xstrdup(p); | |
264 | else | |
5bd0851d | 265 | hint->remote_status = xstrdup("failed"); |
63518a57 JX |
266 | } else { |
267 | hint->status = REF_STATUS_OK; | |
268 | hint->remote_status = xstrdup_or_null(p); | |
269 | new_report = 1; | |
270 | } | |
f5d942e1 | 271 | } |
5ee907bb | 272 | trace2_region_leave("send_pack", "receive_status", r); |
f5d942e1 NTND |
273 | return ret; |
274 | } | |
275 | ||
5cf88fd8 | 276 | static int sideband_demux(int in UNUSED, int out, void *data) |
f5d942e1 NTND |
277 | { |
278 | int *fd = data, ret; | |
c0e40a2d NTND |
279 | if (async_with_fork()) |
280 | close(fd[1]); | |
f5d942e1 NTND |
281 | ret = recv_sideband("send-pack", fd[0], out); |
282 | close(out); | |
283 | return ret; | |
284 | } | |
285 | ||
f2c681cf NTND |
286 | static int advertise_shallow_grafts_cb(const struct commit_graft *graft, void *cb) |
287 | { | |
288 | struct strbuf *sb = cb; | |
289 | if (graft->nr_parent == -1) | |
7683e2e6 | 290 | packet_buf_write(sb, "shallow %s\n", oid_to_hex(&graft->oid)); |
f2c681cf NTND |
291 | return 0; |
292 | } | |
293 | ||
5ee907bb | 294 | static void advertise_shallow_grafts_buf(struct repository *r, struct strbuf *sb) |
f2c681cf | 295 | { |
5ee907bb | 296 | if (!is_repository_shallow(r)) |
f2c681cf NTND |
297 | return; |
298 | for_each_commit_graft(advertise_shallow_grafts_cb, sb); | |
299 | } | |
300 | ||
7582e939 SB |
301 | #define CHECK_REF_NO_PUSH -1 |
302 | #define CHECK_REF_STATUS_REJECTED -2 | |
303 | #define CHECK_REF_UPTODATE -3 | |
304 | static int check_to_send_update(const struct ref *ref, const struct send_pack_args *args) | |
e40671a3 JH |
305 | { |
306 | if (!ref->peer_ref && !args->send_mirror) | |
7582e939 | 307 | return CHECK_REF_NO_PUSH; |
e40671a3 JH |
308 | |
309 | /* Check for statuses set by set_ref_status_for_push() */ | |
310 | switch (ref->status) { | |
311 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
312 | case REF_STATUS_REJECT_ALREADY_EXISTS: | |
313 | case REF_STATUS_REJECT_FETCH_FIRST: | |
314 | case REF_STATUS_REJECT_NEEDS_FORCE: | |
315 | case REF_STATUS_REJECT_STALE: | |
99a1f9ae | 316 | case REF_STATUS_REJECT_REMOTE_UPDATED: |
e40671a3 | 317 | case REF_STATUS_REJECT_NODELETE: |
7582e939 | 318 | return CHECK_REF_STATUS_REJECTED; |
e40671a3 | 319 | case REF_STATUS_UPTODATE: |
7582e939 | 320 | return CHECK_REF_UPTODATE; |
a4f324a4 | 321 | |
e40671a3 | 322 | default: |
a4f324a4 HX |
323 | case REF_STATUS_EXPECTING_REPORT: |
324 | /* already passed checks on the local side */ | |
325 | case REF_STATUS_OK: | |
326 | /* of course this is OK */ | |
7582e939 | 327 | return 0; |
e40671a3 JH |
328 | } |
329 | } | |
330 | ||
a85b377d JH |
331 | /* |
332 | * the beginning of the next line, or the end of buffer. | |
333 | * | |
334 | * NEEDSWORK: perhaps move this to git-compat-util.h or somewhere and | |
335 | * convert many similar uses found by "git grep -A4 memchr". | |
336 | */ | |
337 | static const char *next_line(const char *line, size_t len) | |
338 | { | |
339 | const char *nl = memchr(line, '\n', len); | |
340 | if (!nl) | |
341 | return line + len; /* incomplete line */ | |
342 | return nl + 1; | |
343 | } | |
344 | ||
20a7558f JH |
345 | static int generate_push_cert(struct strbuf *req_buf, |
346 | const struct ref *remote_refs, | |
347 | struct send_pack_args *args, | |
b89363e4 JH |
348 | const char *cap_string, |
349 | const char *push_cert_nonce) | |
a85b377d JH |
350 | { |
351 | const struct ref *ref; | |
f6a4e61f | 352 | struct string_list_item *item; |
b8849e23 PS |
353 | char *signing_key_id = get_signing_key_id(); |
354 | char *signing_key = get_signing_key(); | |
a85b377d JH |
355 | const char *cp, *np; |
356 | struct strbuf cert = STRBUF_INIT; | |
357 | int update_seen = 0; | |
358 | ||
02962d36 | 359 | strbuf_addstr(&cert, "certificate version 0.1\n"); |
4838f62c | 360 | strbuf_addf(&cert, "pusher %s ", signing_key_id); |
fb06b528 JH |
361 | datestamp(&cert); |
362 | strbuf_addch(&cert, '\n'); | |
9be89160 JH |
363 | if (args->url && *args->url) { |
364 | char *anon_url = transport_anonymize_url(args->url); | |
365 | strbuf_addf(&cert, "pushee %s\n", anon_url); | |
366 | free(anon_url); | |
367 | } | |
b89363e4 JH |
368 | if (push_cert_nonce[0]) |
369 | strbuf_addf(&cert, "nonce %s\n", push_cert_nonce); | |
f6a4e61f SB |
370 | if (args->push_options) |
371 | for_each_string_list_item(item, args->push_options) | |
372 | strbuf_addf(&cert, "push-option %s\n", item->string); | |
a85b377d JH |
373 | strbuf_addstr(&cert, "\n"); |
374 | ||
375 | for (ref = remote_refs; ref; ref = ref->next) { | |
7582e939 | 376 | if (check_to_send_update(ref, args) < 0) |
a85b377d JH |
377 | continue; |
378 | update_seen = 1; | |
379 | strbuf_addf(&cert, "%s %s %s\n", | |
f4e54d02 | 380 | oid_to_hex(&ref->old_oid), |
381 | oid_to_hex(&ref->new_oid), | |
a85b377d JH |
382 | ref->name); |
383 | } | |
384 | if (!update_seen) | |
385 | goto free_return; | |
386 | ||
b8849e23 | 387 | if (sign_buffer(&cert, &cert, signing_key)) |
a85b377d JH |
388 | die(_("failed to sign the push certificate")); |
389 | ||
20a7558f | 390 | packet_buf_write(req_buf, "push-cert%c%s", 0, cap_string); |
a85b377d JH |
391 | for (cp = cert.buf; cp < cert.buf + cert.len; cp = np) { |
392 | np = next_line(cp, cert.buf + cert.len - cp); | |
393 | packet_buf_write(req_buf, | |
394 | "%.*s", (int)(np - cp), cp); | |
395 | } | |
396 | packet_buf_write(req_buf, "push-cert-end\n"); | |
397 | ||
398 | free_return: | |
4838f62c | 399 | free(signing_key_id); |
b8849e23 | 400 | free(signing_key); |
a85b377d | 401 | strbuf_release(&cert); |
20a7558f | 402 | return update_seen; |
a85b377d JH |
403 | } |
404 | ||
afcb6ee3 JH |
405 | #define NONCE_LEN_LIMIT 256 |
406 | ||
407 | static void reject_invalid_nonce(const char *nonce, int len) | |
408 | { | |
409 | int i = 0; | |
410 | ||
411 | if (NONCE_LEN_LIMIT <= len) | |
412 | die("the receiving end asked to sign an invalid nonce <%.*s>", | |
413 | len, nonce); | |
414 | ||
415 | for (i = 0; i < len; i++) { | |
416 | int ch = nonce[i] & 0xFF; | |
417 | if (isalnum(ch) || | |
418 | ch == '-' || ch == '.' || | |
419 | ch == '/' || ch == '+' || | |
420 | ch == '=' || ch == '_') | |
421 | continue; | |
422 | die("the receiving end asked to sign an invalid nonce <%.*s>", | |
423 | len, nonce); | |
424 | } | |
425 | } | |
426 | ||
5ee907bb PS |
427 | static void get_commons_through_negotiation(struct repository *r, |
428 | const char *url, | |
477673d6 JT |
429 | const struct ref *remote_refs, |
430 | struct oid_array *commons) | |
431 | { | |
432 | struct child_process child = CHILD_PROCESS_INIT; | |
433 | const struct ref *ref; | |
5ee907bb | 434 | int len = r->hash_algo->hexsz + 1; /* hash + NL */ |
4d8ee031 | 435 | int nr_negotiation_tip = 0; |
477673d6 JT |
436 | |
437 | child.git_cmd = 1; | |
438 | child.no_stdin = 1; | |
439 | child.out = -1; | |
440 | strvec_pushl(&child.args, "fetch", "--negotiate-only", NULL); | |
54a03bc7 | 441 | for (ref = remote_refs; ref; ref = ref->next) { |
4d8ee031 JH |
442 | if (!is_null_oid(&ref->new_oid)) { |
443 | strvec_pushf(&child.args, "--negotiation-tip=%s", | |
444 | oid_to_hex(&ref->new_oid)); | |
445 | nr_negotiation_tip++; | |
446 | } | |
54a03bc7 | 447 | } |
477673d6 JT |
448 | strvec_push(&child.args, url); |
449 | ||
4d8ee031 JH |
450 | if (!nr_negotiation_tip) { |
451 | child_process_clear(&child); | |
452 | return; | |
453 | } | |
454 | ||
477673d6 JT |
455 | if (start_command(&child)) |
456 | die(_("send-pack: unable to fork off fetch subprocess")); | |
457 | ||
458 | do { | |
459 | char hex_hash[GIT_MAX_HEXSZ + 1]; | |
460 | int read_len = read_in_full(child.out, hex_hash, len); | |
461 | struct object_id oid; | |
462 | const char *end; | |
463 | ||
464 | if (!read_len) | |
465 | break; | |
466 | if (read_len != len) | |
467 | die("invalid length read %d", read_len); | |
5ee907bb | 468 | if (parse_oid_hex_algop(hex_hash, &oid, &end, r->hash_algo) || *end != '\n') |
477673d6 JT |
469 | die("invalid hash"); |
470 | oid_array_append(commons, &oid); | |
471 | } while (1); | |
472 | ||
473 | if (finish_command(&child)) { | |
474 | /* | |
475 | * The information that push negotiation provides is useful but | |
476 | * not mandatory. | |
477 | */ | |
478 | warning(_("push negotiation failed; proceeding anyway with push")); | |
479 | } | |
480 | } | |
481 | ||
5ee907bb PS |
482 | int send_pack(struct repository *r, |
483 | struct send_pack_args *args, | |
f5d942e1 NTND |
484 | int fd[], struct child_process *conn, |
485 | struct ref *remote_refs, | |
910650d2 | 486 | struct oid_array *extra_have) |
f5d942e1 | 487 | { |
477673d6 | 488 | struct oid_array commons = OID_ARRAY_INIT; |
f5d942e1 NTND |
489 | int in = fd[0]; |
490 | int out = fd[1]; | |
491 | struct strbuf req_buf = STRBUF_INIT; | |
887f3533 | 492 | struct strbuf cap_buf = STRBUF_INIT; |
f5d942e1 | 493 | struct ref *ref; |
ab2b0c90 | 494 | int need_pack_data = 0; |
f5d942e1 NTND |
495 | int allow_deleting_refs = 0; |
496 | int status_report = 0; | |
497 | int use_sideband = 0; | |
498 | int quiet_supported = 0; | |
499 | int agent_supported = 0; | |
8c487002 | 500 | int advertise_sid = 0; |
477673d6 | 501 | int push_negotiate = 0; |
4ff17f10 RS |
502 | int use_atomic = 0; |
503 | int atomic_supported = 0; | |
f6a4e61f SB |
504 | int use_push_options = 0; |
505 | int push_options_supported = 0; | |
82db03ab | 506 | int object_format_supported = 0; |
f5d942e1 NTND |
507 | unsigned cmds_sent = 0; |
508 | int ret; | |
509 | struct async demux; | |
49d47eb5 | 510 | char *push_cert_nonce = NULL; |
01f9ec64 | 511 | struct packet_reader reader; |
82f67ee1 | 512 | int use_bitmaps; |
f5d942e1 | 513 | |
1e5b5ea5 ÆAB |
514 | if (!remote_refs) { |
515 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n" | |
516 | "Perhaps you should specify a branch.\n"); | |
e03004f7 PS |
517 | ret = 0; |
518 | goto out; | |
1e5b5ea5 ÆAB |
519 | } |
520 | ||
5ee907bb | 521 | repo_config_get_bool(r, "push.negotiate", &push_negotiate); |
db528127 | 522 | if (push_negotiate) { |
5ee907bb PS |
523 | trace2_region_enter("send_pack", "push_negotiate", r); |
524 | get_commons_through_negotiation(r, args->url, remote_refs, &commons); | |
525 | trace2_region_leave("send_pack", "push_negotiate", r); | |
db528127 | 526 | } |
477673d6 | 527 | |
5ee907bb | 528 | if (!repo_config_get_bool(r, "push.usebitmaps", &use_bitmaps)) |
82f67ee1 KZ |
529 | args->disable_bitmaps = !use_bitmaps; |
530 | ||
5ee907bb | 531 | repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid); |
8c487002 | 532 | |
f5d942e1 | 533 | /* Does the other end support the reporting? */ |
63518a57 JX |
534 | if (server_supports("report-status-v2")) |
535 | status_report = 2; | |
536 | else if (server_supports("report-status")) | |
f5d942e1 NTND |
537 | status_report = 1; |
538 | if (server_supports("delete-refs")) | |
539 | allow_deleting_refs = 1; | |
540 | if (server_supports("ofs-delta")) | |
541 | args->use_ofs_delta = 1; | |
542 | if (server_supports("side-band-64k")) | |
543 | use_sideband = 1; | |
544 | if (server_supports("quiet")) | |
545 | quiet_supported = 1; | |
546 | if (server_supports("agent")) | |
547 | agent_supported = 1; | |
8c487002 JS |
548 | if (!server_supports("session-id")) |
549 | advertise_sid = 0; | |
1ba98a79 CMN |
550 | if (server_supports("no-thin")) |
551 | args->use_thin_pack = 0; | |
4ff17f10 RS |
552 | if (server_supports("atomic")) |
553 | atomic_supported = 1; | |
f6a4e61f SB |
554 | if (server_supports("push-options")) |
555 | push_options_supported = 1; | |
b89363e4 | 556 | |
5ee907bb | 557 | if (!server_supports_hash(r->hash_algo->name, &object_format_supported)) |
82db03ab | 558 | die(_("the receiving end does not support this repository's hash algorithm")); |
559 | ||
30261094 | 560 | if (args->push_cert != SEND_PACK_PUSH_CERT_NEVER) { |
7ce4c8f7 | 561 | size_t len; |
49d47eb5 PS |
562 | const char *nonce = server_feature_value("push-cert", &len); |
563 | ||
564 | if (nonce) { | |
565 | reject_invalid_nonce(nonce, len); | |
566 | push_cert_nonce = xmemdupz(nonce, len); | |
30261094 | 567 | } else if (args->push_cert == SEND_PACK_PUSH_CERT_ALWAYS) { |
b89363e4 | 568 | die(_("the receiving end does not support --signed push")); |
30261094 DB |
569 | } else if (args->push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) { |
570 | warning(_("not sending a push certificate since the" | |
571 | " receiving end does not support --signed" | |
572 | " push")); | |
573 | } | |
b89363e4 | 574 | } |
f5d942e1 | 575 | |
4ff17f10 | 576 | if (args->atomic && !atomic_supported) |
c8b8f22a | 577 | die(_("the receiving end does not support --atomic push")); |
4ff17f10 RS |
578 | |
579 | use_atomic = atomic_supported && args->atomic; | |
f5d942e1 | 580 | |
f6a4e61f SB |
581 | if (args->push_options && !push_options_supported) |
582 | die(_("the receiving end does not support push options")); | |
583 | ||
584 | use_push_options = push_options_supported && args->push_options; | |
585 | ||
63518a57 | 586 | if (status_report == 1) |
887f3533 | 587 | strbuf_addstr(&cap_buf, " report-status"); |
63518a57 JX |
588 | else if (status_report == 2) |
589 | strbuf_addstr(&cap_buf, " report-status-v2"); | |
887f3533 JH |
590 | if (use_sideband) |
591 | strbuf_addstr(&cap_buf, " side-band-64k"); | |
592 | if (quiet_supported && (args->quiet || !args->progress)) | |
593 | strbuf_addstr(&cap_buf, " quiet"); | |
4ff17f10 RS |
594 | if (use_atomic) |
595 | strbuf_addstr(&cap_buf, " atomic"); | |
f6a4e61f SB |
596 | if (use_push_options) |
597 | strbuf_addstr(&cap_buf, " push-options"); | |
82db03ab | 598 | if (object_format_supported) |
5ee907bb | 599 | strbuf_addf(&cap_buf, " object-format=%s", r->hash_algo->name); |
887f3533 JH |
600 | if (agent_supported) |
601 | strbuf_addf(&cap_buf, " agent=%s", git_user_agent_sanitized()); | |
8c487002 JS |
602 | if (advertise_sid) |
603 | strbuf_addf(&cap_buf, " session-id=%s", trace2_session_id()); | |
887f3533 | 604 | |
621b0599 JH |
605 | /* |
606 | * NEEDSWORK: why does delete-refs have to be so specific to | |
607 | * send-pack machinery that set_ref_status_for_push() cannot | |
608 | * set this bit for us??? | |
609 | */ | |
610 | for (ref = remote_refs; ref; ref = ref->next) | |
611 | if (ref->deletion && !allow_deleting_refs) | |
612 | ref->status = REF_STATUS_REJECT_NODELETE; | |
613 | ||
f5d942e1 | 614 | /* |
b783aa71 JH |
615 | * Clear the status for each ref and see if we need to send |
616 | * the pack data. | |
f5d942e1 | 617 | */ |
f5d942e1 | 618 | for (ref = remote_refs; ref; ref = ref->next) { |
4ff17f10 RS |
619 | switch (check_to_send_update(ref, args)) { |
620 | case 0: /* no error */ | |
621 | break; | |
622 | case CHECK_REF_STATUS_REJECTED: | |
623 | /* | |
624 | * When we know the server would reject a ref update if | |
625 | * we were to send it and we're trying to send the refs | |
626 | * atomically, abort the whole operation. | |
627 | */ | |
872d651f | 628 | if (use_atomic) { |
dfe1b7f1 | 629 | reject_atomic_push(remote_refs, args->send_mirror); |
1a60f206 | 630 | error("atomic push failed for ref %s. status: %d", |
dfe1b7f1 | 631 | ref->name, ref->status); |
3028db4a | 632 | ret = ERROR_SEND_PACK_BAD_REF_STATUS; |
b81f8c8d | 633 | packet_flush(out); |
e03004f7 | 634 | goto out; |
872d651f | 635 | } |
1cf01a34 | 636 | /* else fallthrough */ |
4ff17f10 | 637 | default: |
f5d942e1 | 638 | continue; |
4ff17f10 | 639 | } |
f5d942e1 | 640 | if (!ref->deletion) |
ab2b0c90 | 641 | need_pack_data = 1; |
f5d942e1 | 642 | |
b783aa71 | 643 | if (args->dry_run || !status_report) |
f5d942e1 | 644 | ref->status = REF_STATUS_OK; |
b783aa71 JH |
645 | else |
646 | ref->status = REF_STATUS_EXPECTING_REPORT; | |
647 | } | |
648 | ||
a4f324a4 | 649 | if (!args->dry_run) |
5ee907bb | 650 | advertise_shallow_grafts_buf(r, &req_buf); |
a4f324a4 | 651 | |
b783aa71 JH |
652 | /* |
653 | * Finally, tell the other end! | |
654 | */ | |
db528127 | 655 | if (!args->dry_run && push_cert_nonce) { |
a4f324a4 HX |
656 | cmds_sent = generate_push_cert(&req_buf, remote_refs, args, |
657 | cap_buf.buf, push_cert_nonce); | |
db528127 CW |
658 | trace2_printf("Generated push certificate"); |
659 | } else if (!args->dry_run) { | |
a4f324a4 HX |
660 | for (ref = remote_refs; ref; ref = ref->next) { |
661 | char *old_hex, *new_hex; | |
f5d942e1 | 662 | |
a4f324a4 HX |
663 | if (check_to_send_update(ref, args) < 0) |
664 | continue; | |
f5d942e1 | 665 | |
a4f324a4 HX |
666 | old_hex = oid_to_hex(&ref->old_oid); |
667 | new_hex = oid_to_hex(&ref->new_oid); | |
668 | if (!cmds_sent) { | |
669 | packet_buf_write(&req_buf, | |
670 | "%s %s %s%c%s", | |
671 | old_hex, new_hex, ref->name, 0, | |
672 | cap_buf.buf); | |
673 | cmds_sent = 1; | |
674 | } else { | |
675 | packet_buf_write(&req_buf, "%s %s %s", | |
676 | old_hex, new_hex, ref->name); | |
677 | } | |
f5d942e1 | 678 | } |
db528127 | 679 | } |
f5d942e1 | 680 | |
eb7b9749 BW |
681 | if (use_push_options) { |
682 | struct string_list_item *item; | |
683 | ||
684 | packet_buf_flush(&req_buf); | |
685 | for_each_string_list_item(item, args->push_options) | |
686 | packet_buf_write(&req_buf, "%s", item->string); | |
687 | } | |
688 | ||
f5d942e1 | 689 | if (args->stateless_rpc) { |
5ee907bb | 690 | if (!args->dry_run && (cmds_sent || is_repository_shallow(r))) { |
f5d942e1 NTND |
691 | packet_buf_flush(&req_buf); |
692 | send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX); | |
693 | } | |
694 | } else { | |
cdf4fb8e | 695 | write_or_die(out, req_buf.buf, req_buf.len); |
f5d942e1 NTND |
696 | packet_flush(out); |
697 | } | |
f5d942e1 NTND |
698 | |
699 | if (use_sideband && cmds_sent) { | |
700 | memset(&demux, 0, sizeof(demux)); | |
701 | demux.proc = sideband_demux; | |
702 | demux.data = fd; | |
703 | demux.out = -1; | |
3e8b06d0 | 704 | demux.isolate_sigpipe = 1; |
f5d942e1 NTND |
705 | if (start_async(&demux)) |
706 | die("send-pack: unable to fork off sideband demultiplexer"); | |
707 | in = demux.out; | |
708 | } | |
709 | ||
2d103c31 MS |
710 | packet_reader_init(&reader, in, NULL, 0, |
711 | PACKET_READ_CHOMP_NEWLINE | | |
712 | PACKET_READ_DIE_ON_ERR_PACKET); | |
01f9ec64 | 713 | |
ab2b0c90 | 714 | if (need_pack_data && cmds_sent) { |
5ee907bb | 715 | if (pack_objects(r, out, remote_refs, extra_have, &commons, args) < 0) { |
f5d942e1 NTND |
716 | if (args->stateless_rpc) |
717 | close(out); | |
718 | if (git_connection_is_socket(conn)) | |
719 | shutdown(fd[0], SHUT_WR); | |
ba69f92d JK |
720 | |
721 | /* | |
722 | * Do not even bother with the return value; we know we | |
ad7a4032 JK |
723 | * are failing, and just want the error() side effects, |
724 | * as well as marking refs with their remote status (if | |
725 | * we get one). | |
ba69f92d JK |
726 | */ |
727 | if (status_report) | |
5ee907bb | 728 | receive_status(r, &reader, remote_refs); |
ba69f92d | 729 | |
739cf491 JK |
730 | if (use_sideband) { |
731 | close(demux.out); | |
f5d942e1 | 732 | finish_async(&demux); |
739cf491 | 733 | } |
37cb1dd6 | 734 | fd[1] = -1; |
e03004f7 PS |
735 | |
736 | ret = -1; | |
737 | goto out; | |
f5d942e1 | 738 | } |
37cb1dd6 JL |
739 | if (!args->stateless_rpc) |
740 | /* Closed by pack_objects() via start_command() */ | |
741 | fd[1] = -1; | |
f5d942e1 NTND |
742 | } |
743 | if (args->stateless_rpc && cmds_sent) | |
744 | packet_flush(out); | |
745 | ||
746 | if (status_report && cmds_sent) | |
5ee907bb | 747 | ret = receive_status(r, &reader, remote_refs); |
f5d942e1 NTND |
748 | else |
749 | ret = 0; | |
750 | if (args->stateless_rpc) | |
751 | packet_flush(out); | |
752 | ||
753 | if (use_sideband && cmds_sent) { | |
739cf491 | 754 | close(demux.out); |
f5d942e1 NTND |
755 | if (finish_async(&demux)) { |
756 | error("error in sideband demultiplexer"); | |
757 | ret = -1; | |
758 | } | |
f5d942e1 NTND |
759 | } |
760 | ||
761 | if (ret < 0) | |
e03004f7 | 762 | goto out; |
f5d942e1 | 763 | |
f5d942e1 NTND |
764 | for (ref = remote_refs; ref; ref = ref->next) { |
765 | switch (ref->status) { | |
766 | case REF_STATUS_NONE: | |
767 | case REF_STATUS_UPTODATE: | |
768 | case REF_STATUS_OK: | |
769 | break; | |
770 | default: | |
3028db4a | 771 | ret = ERROR_SEND_PACK_BAD_REF_STATUS; |
e03004f7 | 772 | goto out; |
f5d942e1 NTND |
773 | } |
774 | } | |
e03004f7 PS |
775 | |
776 | ret = 0; | |
777 | ||
778 | out: | |
779 | oid_array_clear(&commons); | |
780 | strbuf_release(&req_buf); | |
781 | strbuf_release(&cap_buf); | |
49d47eb5 | 782 | free(push_cert_nonce); |
e03004f7 | 783 | return ret; |
f5d942e1 | 784 | } |