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