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