]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/receive-pack.c
Merge branch 'ar/config-count-tests-updates'
[thirdparty/git.git] / builtin / receive-pack.c
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "repository.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "lockfile.h"
9 #include "pack.h"
10 #include "refs.h"
11 #include "pkt-line.h"
12 #include "sideband.h"
13 #include "run-command.h"
14 #include "hook.h"
15 #include "exec-cmd.h"
16 #include "commit.h"
17 #include "object.h"
18 #include "remote.h"
19 #include "connect.h"
20 #include "string-list.h"
21 #include "oid-array.h"
22 #include "connected.h"
23 #include "strvec.h"
24 #include "version.h"
25 #include "tag.h"
26 #include "gpg-interface.h"
27 #include "sigchain.h"
28 #include "fsck.h"
29 #include "tmp-objdir.h"
30 #include "oidset.h"
31 #include "packfile.h"
32 #include "object-name.h"
33 #include "object-store.h"
34 #include "protocol.h"
35 #include "commit-reach.h"
36 #include "server-info.h"
37 #include "trace.h"
38 #include "trace2.h"
39 #include "worktree.h"
40 #include "shallow.h"
41 #include "parse-options.h"
42 #include "wrapper.h"
43
44 static const char * const receive_pack_usage[] = {
45 N_("git receive-pack <git-dir>"),
46 NULL
47 };
48
49 enum deny_action {
50 DENY_UNCONFIGURED,
51 DENY_IGNORE,
52 DENY_WARN,
53 DENY_REFUSE,
54 DENY_UPDATE_INSTEAD
55 };
56
57 static int deny_deletes;
58 static int deny_non_fast_forwards;
59 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
60 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
61 static int receive_fsck_objects = -1;
62 static int transfer_fsck_objects = -1;
63 static struct strbuf fsck_msg_types = STRBUF_INIT;
64 static int receive_unpack_limit = -1;
65 static int transfer_unpack_limit = -1;
66 static int advertise_atomic_push = 1;
67 static int advertise_push_options;
68 static int advertise_sid;
69 static int unpack_limit = 100;
70 static off_t max_input_size;
71 static int report_status;
72 static int report_status_v2;
73 static int use_sideband;
74 static int use_atomic;
75 static int use_push_options;
76 static int quiet;
77 static int prefer_ofs_delta = 1;
78 static int auto_update_server_info;
79 static int auto_gc = 1;
80 static int reject_thin;
81 static int stateless_rpc;
82 static const char *service_dir;
83 static const char *head_name;
84 static void *head_name_to_free;
85 static int sent_capabilities;
86 static int shallow_update;
87 static const char *alt_shallow_file;
88 static struct strbuf push_cert = STRBUF_INIT;
89 static struct object_id push_cert_oid;
90 static struct signature_check sigcheck;
91 static const char *push_cert_nonce;
92 static const char *cert_nonce_seed;
93 static struct string_list hidden_refs = STRING_LIST_INIT_DUP;
94
95 static const char *NONCE_UNSOLICITED = "UNSOLICITED";
96 static const char *NONCE_BAD = "BAD";
97 static const char *NONCE_MISSING = "MISSING";
98 static const char *NONCE_OK = "OK";
99 static const char *NONCE_SLOP = "SLOP";
100 static const char *nonce_status;
101 static long nonce_stamp_slop;
102 static timestamp_t nonce_stamp_slop_limit;
103 static struct ref_transaction *transaction;
104
105 static enum {
106 KEEPALIVE_NEVER = 0,
107 KEEPALIVE_AFTER_NUL,
108 KEEPALIVE_ALWAYS
109 } use_keepalive;
110 static int keepalive_in_sec = 5;
111
112 static struct tmp_objdir *tmp_objdir;
113
114 static struct proc_receive_ref {
115 unsigned int want_add:1,
116 want_delete:1,
117 want_modify:1,
118 negative_ref:1;
119 char *ref_prefix;
120 struct proc_receive_ref *next;
121 } *proc_receive_ref;
122
123 static void proc_receive_ref_append(const char *prefix);
124
125 static enum deny_action parse_deny_action(const char *var, const char *value)
126 {
127 if (value) {
128 if (!strcasecmp(value, "ignore"))
129 return DENY_IGNORE;
130 if (!strcasecmp(value, "warn"))
131 return DENY_WARN;
132 if (!strcasecmp(value, "refuse"))
133 return DENY_REFUSE;
134 if (!strcasecmp(value, "updateinstead"))
135 return DENY_UPDATE_INSTEAD;
136 }
137 if (git_config_bool(var, value))
138 return DENY_REFUSE;
139 return DENY_IGNORE;
140 }
141
142 static int receive_pack_config(const char *var, const char *value, void *cb)
143 {
144 int status = parse_hide_refs_config(var, value, "receive", &hidden_refs);
145
146 if (status)
147 return status;
148
149 if (strcmp(var, "receive.denydeletes") == 0) {
150 deny_deletes = git_config_bool(var, value);
151 return 0;
152 }
153
154 if (strcmp(var, "receive.denynonfastforwards") == 0) {
155 deny_non_fast_forwards = git_config_bool(var, value);
156 return 0;
157 }
158
159 if (strcmp(var, "receive.unpacklimit") == 0) {
160 receive_unpack_limit = git_config_int(var, value);
161 return 0;
162 }
163
164 if (strcmp(var, "transfer.unpacklimit") == 0) {
165 transfer_unpack_limit = git_config_int(var, value);
166 return 0;
167 }
168
169 if (strcmp(var, "receive.fsck.skiplist") == 0) {
170 const char *path;
171
172 if (git_config_pathname(&path, var, value))
173 return 1;
174 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
175 fsck_msg_types.len ? ',' : '=', path);
176 free((char *)path);
177 return 0;
178 }
179
180 if (skip_prefix(var, "receive.fsck.", &var)) {
181 if (is_valid_msg_type(var, value))
182 strbuf_addf(&fsck_msg_types, "%c%s=%s",
183 fsck_msg_types.len ? ',' : '=', var, value);
184 else
185 warning("skipping unknown msg id '%s'", var);
186 return 0;
187 }
188
189 if (strcmp(var, "receive.fsckobjects") == 0) {
190 receive_fsck_objects = git_config_bool(var, value);
191 return 0;
192 }
193
194 if (strcmp(var, "transfer.fsckobjects") == 0) {
195 transfer_fsck_objects = git_config_bool(var, value);
196 return 0;
197 }
198
199 if (!strcmp(var, "receive.denycurrentbranch")) {
200 deny_current_branch = parse_deny_action(var, value);
201 return 0;
202 }
203
204 if (strcmp(var, "receive.denydeletecurrent") == 0) {
205 deny_delete_current = parse_deny_action(var, value);
206 return 0;
207 }
208
209 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
210 prefer_ofs_delta = git_config_bool(var, value);
211 return 0;
212 }
213
214 if (strcmp(var, "receive.updateserverinfo") == 0) {
215 auto_update_server_info = git_config_bool(var, value);
216 return 0;
217 }
218
219 if (strcmp(var, "receive.autogc") == 0) {
220 auto_gc = git_config_bool(var, value);
221 return 0;
222 }
223
224 if (strcmp(var, "receive.shallowupdate") == 0) {
225 shallow_update = git_config_bool(var, value);
226 return 0;
227 }
228
229 if (strcmp(var, "receive.certnonceseed") == 0)
230 return git_config_string(&cert_nonce_seed, var, value);
231
232 if (strcmp(var, "receive.certnonceslop") == 0) {
233 nonce_stamp_slop_limit = git_config_ulong(var, value);
234 return 0;
235 }
236
237 if (strcmp(var, "receive.advertiseatomic") == 0) {
238 advertise_atomic_push = git_config_bool(var, value);
239 return 0;
240 }
241
242 if (strcmp(var, "receive.advertisepushoptions") == 0) {
243 advertise_push_options = git_config_bool(var, value);
244 return 0;
245 }
246
247 if (strcmp(var, "receive.keepalive") == 0) {
248 keepalive_in_sec = git_config_int(var, value);
249 return 0;
250 }
251
252 if (strcmp(var, "receive.maxinputsize") == 0) {
253 max_input_size = git_config_int64(var, value);
254 return 0;
255 }
256
257 if (strcmp(var, "receive.procreceiverefs") == 0) {
258 if (!value)
259 return config_error_nonbool(var);
260 proc_receive_ref_append(value);
261 return 0;
262 }
263
264 if (strcmp(var, "transfer.advertisesid") == 0) {
265 advertise_sid = git_config_bool(var, value);
266 return 0;
267 }
268
269 return git_default_config(var, value, cb);
270 }
271
272 static void show_ref(const char *path, const struct object_id *oid)
273 {
274 if (sent_capabilities) {
275 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), path);
276 } else {
277 struct strbuf cap = STRBUF_INIT;
278
279 strbuf_addstr(&cap,
280 "report-status report-status-v2 delete-refs side-band-64k quiet");
281 if (advertise_atomic_push)
282 strbuf_addstr(&cap, " atomic");
283 if (prefer_ofs_delta)
284 strbuf_addstr(&cap, " ofs-delta");
285 if (push_cert_nonce)
286 strbuf_addf(&cap, " push-cert=%s", push_cert_nonce);
287 if (advertise_push_options)
288 strbuf_addstr(&cap, " push-options");
289 if (advertise_sid)
290 strbuf_addf(&cap, " session-id=%s", trace2_session_id());
291 strbuf_addf(&cap, " object-format=%s", the_hash_algo->name);
292 strbuf_addf(&cap, " agent=%s", git_user_agent_sanitized());
293 packet_write_fmt(1, "%s %s%c%s\n",
294 oid_to_hex(oid), path, 0, cap.buf);
295 strbuf_release(&cap);
296 sent_capabilities = 1;
297 }
298 }
299
300 static int show_ref_cb(const char *path_full, const struct object_id *oid,
301 int flag UNUSED, void *data)
302 {
303 struct oidset *seen = data;
304 const char *path = strip_namespace(path_full);
305
306 if (ref_is_hidden(path, path_full, &hidden_refs))
307 return 0;
308
309 /*
310 * Advertise refs outside our current namespace as ".have"
311 * refs, so that the client can use them to minimize data
312 * transfer but will otherwise ignore them.
313 */
314 if (!path) {
315 if (oidset_insert(seen, oid))
316 return 0;
317 path = ".have";
318 } else {
319 oidset_insert(seen, oid);
320 }
321 show_ref(path, oid);
322 return 0;
323 }
324
325 static void show_one_alternate_ref(const struct object_id *oid,
326 void *data)
327 {
328 struct oidset *seen = data;
329
330 if (oidset_insert(seen, oid))
331 return;
332
333 show_ref(".have", oid);
334 }
335
336 static void write_head_info(void)
337 {
338 static struct oidset seen = OIDSET_INIT;
339
340 for_each_ref(show_ref_cb, &seen);
341 for_each_alternate_ref(show_one_alternate_ref, &seen);
342 oidset_clear(&seen);
343 if (!sent_capabilities)
344 show_ref("capabilities^{}", null_oid());
345
346 advertise_shallow_grafts(1);
347
348 /* EOF */
349 packet_flush(1);
350 }
351
352 #define RUN_PROC_RECEIVE_SCHEDULED 1
353 #define RUN_PROC_RECEIVE_RETURNED 2
354 struct command {
355 struct command *next;
356 const char *error_string;
357 struct ref_push_report *report;
358 unsigned int skip_update:1,
359 did_not_exist:1,
360 run_proc_receive:2;
361 int index;
362 struct object_id old_oid;
363 struct object_id new_oid;
364 char ref_name[FLEX_ARRAY]; /* more */
365 };
366
367 static void proc_receive_ref_append(const char *prefix)
368 {
369 struct proc_receive_ref *ref_pattern;
370 char *p;
371 int len;
372
373 CALLOC_ARRAY(ref_pattern, 1);
374 p = strchr(prefix, ':');
375 if (p) {
376 while (prefix < p) {
377 if (*prefix == 'a')
378 ref_pattern->want_add = 1;
379 else if (*prefix == 'd')
380 ref_pattern->want_delete = 1;
381 else if (*prefix == 'm')
382 ref_pattern->want_modify = 1;
383 else if (*prefix == '!')
384 ref_pattern->negative_ref = 1;
385 prefix++;
386 }
387 prefix++;
388 } else {
389 ref_pattern->want_add = 1;
390 ref_pattern->want_delete = 1;
391 ref_pattern->want_modify = 1;
392 }
393 len = strlen(prefix);
394 while (len && prefix[len - 1] == '/')
395 len--;
396 ref_pattern->ref_prefix = xmemdupz(prefix, len);
397 if (!proc_receive_ref) {
398 proc_receive_ref = ref_pattern;
399 } else {
400 struct proc_receive_ref *end;
401
402 end = proc_receive_ref;
403 while (end->next)
404 end = end->next;
405 end->next = ref_pattern;
406 }
407 }
408
409 static int proc_receive_ref_matches(struct command *cmd)
410 {
411 struct proc_receive_ref *p;
412
413 if (!proc_receive_ref)
414 return 0;
415
416 for (p = proc_receive_ref; p; p = p->next) {
417 const char *match = p->ref_prefix;
418 const char *remains;
419
420 if (!p->want_add && is_null_oid(&cmd->old_oid))
421 continue;
422 else if (!p->want_delete && is_null_oid(&cmd->new_oid))
423 continue;
424 else if (!p->want_modify &&
425 !is_null_oid(&cmd->old_oid) &&
426 !is_null_oid(&cmd->new_oid))
427 continue;
428
429 if (skip_prefix(cmd->ref_name, match, &remains) &&
430 (!*remains || *remains == '/')) {
431 if (!p->negative_ref)
432 return 1;
433 } else if (p->negative_ref) {
434 return 1;
435 }
436 }
437 return 0;
438 }
439
440 static void report_message(const char *prefix, const char *err, va_list params)
441 {
442 int sz;
443 char msg[4096];
444
445 sz = xsnprintf(msg, sizeof(msg), "%s", prefix);
446 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
447 if (sz > (sizeof(msg) - 1))
448 sz = sizeof(msg) - 1;
449 msg[sz++] = '\n';
450
451 if (use_sideband)
452 send_sideband(1, 2, msg, sz, use_sideband);
453 else
454 xwrite(2, msg, sz);
455 }
456
457 __attribute__((format (printf, 1, 2)))
458 static void rp_warning(const char *err, ...)
459 {
460 va_list params;
461 va_start(params, err);
462 report_message("warning: ", err, params);
463 va_end(params);
464 }
465
466 __attribute__((format (printf, 1, 2)))
467 static void rp_error(const char *err, ...)
468 {
469 va_list params;
470 va_start(params, err);
471 report_message("error: ", err, params);
472 va_end(params);
473 }
474
475 static int copy_to_sideband(int in, int out UNUSED, void *arg UNUSED)
476 {
477 char data[128];
478 int keepalive_active = 0;
479
480 if (keepalive_in_sec <= 0)
481 use_keepalive = KEEPALIVE_NEVER;
482 if (use_keepalive == KEEPALIVE_ALWAYS)
483 keepalive_active = 1;
484
485 while (1) {
486 ssize_t sz;
487
488 if (keepalive_active) {
489 struct pollfd pfd;
490 int ret;
491
492 pfd.fd = in;
493 pfd.events = POLLIN;
494 ret = poll(&pfd, 1, 1000 * keepalive_in_sec);
495
496 if (ret < 0) {
497 if (errno == EINTR)
498 continue;
499 else
500 break;
501 } else if (ret == 0) {
502 /* no data; send a keepalive packet */
503 static const char buf[] = "0005\1";
504 write_or_die(1, buf, sizeof(buf) - 1);
505 continue;
506 } /* else there is actual data to read */
507 }
508
509 sz = xread(in, data, sizeof(data));
510 if (sz <= 0)
511 break;
512
513 if (use_keepalive == KEEPALIVE_AFTER_NUL && !keepalive_active) {
514 const char *p = memchr(data, '\0', sz);
515 if (p) {
516 /*
517 * The NUL tells us to start sending keepalives. Make
518 * sure we send any other data we read along
519 * with it.
520 */
521 keepalive_active = 1;
522 send_sideband(1, 2, data, p - data, use_sideband);
523 send_sideband(1, 2, p + 1, sz - (p - data + 1), use_sideband);
524 continue;
525 }
526 }
527
528 /*
529 * Either we're not looking for a NUL signal, or we didn't see
530 * it yet; just pass along the data.
531 */
532 send_sideband(1, 2, data, sz, use_sideband);
533 }
534 close(in);
535 return 0;
536 }
537
538 static void hmac_hash(unsigned char *out,
539 const char *key_in, size_t key_len,
540 const char *text, size_t text_len)
541 {
542 unsigned char key[GIT_MAX_BLKSZ];
543 unsigned char k_ipad[GIT_MAX_BLKSZ];
544 unsigned char k_opad[GIT_MAX_BLKSZ];
545 int i;
546 git_hash_ctx ctx;
547
548 /* RFC 2104 2. (1) */
549 memset(key, '\0', GIT_MAX_BLKSZ);
550 if (the_hash_algo->blksz < key_len) {
551 the_hash_algo->init_fn(&ctx);
552 the_hash_algo->update_fn(&ctx, key_in, key_len);
553 the_hash_algo->final_fn(key, &ctx);
554 } else {
555 memcpy(key, key_in, key_len);
556 }
557
558 /* RFC 2104 2. (2) & (5) */
559 for (i = 0; i < sizeof(key); i++) {
560 k_ipad[i] = key[i] ^ 0x36;
561 k_opad[i] = key[i] ^ 0x5c;
562 }
563
564 /* RFC 2104 2. (3) & (4) */
565 the_hash_algo->init_fn(&ctx);
566 the_hash_algo->update_fn(&ctx, k_ipad, sizeof(k_ipad));
567 the_hash_algo->update_fn(&ctx, text, text_len);
568 the_hash_algo->final_fn(out, &ctx);
569
570 /* RFC 2104 2. (6) & (7) */
571 the_hash_algo->init_fn(&ctx);
572 the_hash_algo->update_fn(&ctx, k_opad, sizeof(k_opad));
573 the_hash_algo->update_fn(&ctx, out, the_hash_algo->rawsz);
574 the_hash_algo->final_fn(out, &ctx);
575 }
576
577 static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
578 {
579 struct strbuf buf = STRBUF_INIT;
580 unsigned char hash[GIT_MAX_RAWSZ];
581
582 strbuf_addf(&buf, "%s:%"PRItime, path, stamp);
583 hmac_hash(hash, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));
584 strbuf_release(&buf);
585
586 /* RFC 2104 5. HMAC-SHA1 or HMAC-SHA256 */
587 strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, (int)the_hash_algo->hexsz, hash_to_hex(hash));
588 return strbuf_detach(&buf, NULL);
589 }
590
591 static char *find_header(const char *msg, size_t len, const char *key,
592 const char **next_line)
593 {
594 size_t out_len;
595 const char *val = find_header_mem(msg, len, key, &out_len);
596
597 if (!val)
598 return NULL;
599
600 if (next_line)
601 *next_line = val + out_len + 1;
602
603 return xmemdupz(val, out_len);
604 }
605
606 /*
607 * Return zero if a and b are equal up to n bytes and nonzero if they are not.
608 * This operation is guaranteed to run in constant time to avoid leaking data.
609 */
610 static int constant_memequal(const char *a, const char *b, size_t n)
611 {
612 int res = 0;
613 size_t i;
614
615 for (i = 0; i < n; i++)
616 res |= a[i] ^ b[i];
617 return res;
618 }
619
620 static const char *check_nonce(const char *buf, size_t len)
621 {
622 char *nonce = find_header(buf, len, "nonce", NULL);
623 timestamp_t stamp, ostamp;
624 char *bohmac, *expect = NULL;
625 const char *retval = NONCE_BAD;
626 size_t noncelen;
627
628 if (!nonce) {
629 retval = NONCE_MISSING;
630 goto leave;
631 } else if (!push_cert_nonce) {
632 retval = NONCE_UNSOLICITED;
633 goto leave;
634 } else if (!strcmp(push_cert_nonce, nonce)) {
635 retval = NONCE_OK;
636 goto leave;
637 }
638
639 if (!stateless_rpc) {
640 /* returned nonce MUST match what we gave out earlier */
641 retval = NONCE_BAD;
642 goto leave;
643 }
644
645 /*
646 * In stateless mode, we may be receiving a nonce issued by
647 * another instance of the server that serving the same
648 * repository, and the timestamps may not match, but the
649 * nonce-seed and dir should match, so we can recompute and
650 * report the time slop.
651 *
652 * In addition, when a nonce issued by another instance has
653 * timestamp within receive.certnonceslop seconds, we pretend
654 * as if we issued that nonce when reporting to the hook.
655 */
656
657 /* nonce is concat(<seconds-since-epoch>, "-", <hmac>) */
658 if (*nonce <= '0' || '9' < *nonce) {
659 retval = NONCE_BAD;
660 goto leave;
661 }
662 stamp = parse_timestamp(nonce, &bohmac, 10);
663 if (bohmac == nonce || bohmac[0] != '-') {
664 retval = NONCE_BAD;
665 goto leave;
666 }
667
668 noncelen = strlen(nonce);
669 expect = prepare_push_cert_nonce(service_dir, stamp);
670 if (noncelen != strlen(expect)) {
671 /* This is not even the right size. */
672 retval = NONCE_BAD;
673 goto leave;
674 }
675 if (constant_memequal(expect, nonce, noncelen)) {
676 /* Not what we would have signed earlier */
677 retval = NONCE_BAD;
678 goto leave;
679 }
680
681 /*
682 * By how many seconds is this nonce stale? Negative value
683 * would mean it was issued by another server with its clock
684 * skewed in the future.
685 */
686 ostamp = parse_timestamp(push_cert_nonce, NULL, 10);
687 nonce_stamp_slop = (long)ostamp - (long)stamp;
688
689 if (nonce_stamp_slop_limit &&
690 labs(nonce_stamp_slop) <= nonce_stamp_slop_limit) {
691 /*
692 * Pretend as if the received nonce (which passes the
693 * HMAC check, so it is not a forged by third-party)
694 * is what we issued.
695 */
696 free((void *)push_cert_nonce);
697 push_cert_nonce = xstrdup(nonce);
698 retval = NONCE_OK;
699 } else {
700 retval = NONCE_SLOP;
701 }
702
703 leave:
704 free(nonce);
705 free(expect);
706 return retval;
707 }
708
709 /*
710 * Return 1 if there is no push_cert or if the push options in push_cert are
711 * the same as those in the argument; 0 otherwise.
712 */
713 static int check_cert_push_options(const struct string_list *push_options)
714 {
715 const char *buf = push_cert.buf;
716 int len = push_cert.len;
717
718 char *option;
719 const char *next_line;
720 int options_seen = 0;
721
722 int retval = 1;
723
724 if (!len)
725 return 1;
726
727 while ((option = find_header(buf, len, "push-option", &next_line))) {
728 len -= (next_line - buf);
729 buf = next_line;
730 options_seen++;
731 if (options_seen > push_options->nr
732 || strcmp(option,
733 push_options->items[options_seen - 1].string)) {
734 retval = 0;
735 goto leave;
736 }
737 free(option);
738 }
739
740 if (options_seen != push_options->nr)
741 retval = 0;
742
743 leave:
744 free(option);
745 return retval;
746 }
747
748 static void prepare_push_cert_sha1(struct child_process *proc)
749 {
750 static int already_done;
751
752 if (!push_cert.len)
753 return;
754
755 if (!already_done) {
756 int bogs /* beginning_of_gpg_sig */;
757
758 already_done = 1;
759 if (write_object_file(push_cert.buf, push_cert.len, OBJ_BLOB,
760 &push_cert_oid))
761 oidclr(&push_cert_oid);
762
763 memset(&sigcheck, '\0', sizeof(sigcheck));
764
765 bogs = parse_signed_buffer(push_cert.buf, push_cert.len);
766 sigcheck.payload = xmemdupz(push_cert.buf, bogs);
767 sigcheck.payload_len = bogs;
768 check_signature(&sigcheck, push_cert.buf + bogs,
769 push_cert.len - bogs);
770
771 nonce_status = check_nonce(push_cert.buf, bogs);
772 }
773 if (!is_null_oid(&push_cert_oid)) {
774 strvec_pushf(&proc->env, "GIT_PUSH_CERT=%s",
775 oid_to_hex(&push_cert_oid));
776 strvec_pushf(&proc->env, "GIT_PUSH_CERT_SIGNER=%s",
777 sigcheck.signer ? sigcheck.signer : "");
778 strvec_pushf(&proc->env, "GIT_PUSH_CERT_KEY=%s",
779 sigcheck.key ? sigcheck.key : "");
780 strvec_pushf(&proc->env, "GIT_PUSH_CERT_STATUS=%c",
781 sigcheck.result);
782 if (push_cert_nonce) {
783 strvec_pushf(&proc->env,
784 "GIT_PUSH_CERT_NONCE=%s",
785 push_cert_nonce);
786 strvec_pushf(&proc->env,
787 "GIT_PUSH_CERT_NONCE_STATUS=%s",
788 nonce_status);
789 if (nonce_status == NONCE_SLOP)
790 strvec_pushf(&proc->env,
791 "GIT_PUSH_CERT_NONCE_SLOP=%ld",
792 nonce_stamp_slop);
793 }
794 }
795 }
796
797 struct receive_hook_feed_state {
798 struct command *cmd;
799 struct ref_push_report *report;
800 int skip_broken;
801 struct strbuf buf;
802 const struct string_list *push_options;
803 };
804
805 typedef int (*feed_fn)(void *, const char **, size_t *);
806 static int run_and_feed_hook(const char *hook_name, feed_fn feed,
807 struct receive_hook_feed_state *feed_state)
808 {
809 struct child_process proc = CHILD_PROCESS_INIT;
810 struct async muxer;
811 int code;
812 const char *hook_path = find_hook(hook_name);
813
814 if (!hook_path)
815 return 0;
816
817 strvec_push(&proc.args, hook_path);
818 proc.in = -1;
819 proc.stdout_to_stderr = 1;
820 proc.trace2_hook_name = hook_name;
821
822 if (feed_state->push_options) {
823 size_t i;
824 for (i = 0; i < feed_state->push_options->nr; i++)
825 strvec_pushf(&proc.env,
826 "GIT_PUSH_OPTION_%"PRIuMAX"=%s",
827 (uintmax_t)i,
828 feed_state->push_options->items[i].string);
829 strvec_pushf(&proc.env, "GIT_PUSH_OPTION_COUNT=%"PRIuMAX"",
830 (uintmax_t)feed_state->push_options->nr);
831 } else
832 strvec_pushf(&proc.env, "GIT_PUSH_OPTION_COUNT");
833
834 if (tmp_objdir)
835 strvec_pushv(&proc.env, tmp_objdir_env(tmp_objdir));
836
837 if (use_sideband) {
838 memset(&muxer, 0, sizeof(muxer));
839 muxer.proc = copy_to_sideband;
840 muxer.in = -1;
841 code = start_async(&muxer);
842 if (code)
843 return code;
844 proc.err = muxer.in;
845 }
846
847 prepare_push_cert_sha1(&proc);
848
849 code = start_command(&proc);
850 if (code) {
851 if (use_sideband)
852 finish_async(&muxer);
853 return code;
854 }
855
856 sigchain_push(SIGPIPE, SIG_IGN);
857
858 while (1) {
859 const char *buf;
860 size_t n;
861 if (feed(feed_state, &buf, &n))
862 break;
863 if (write_in_full(proc.in, buf, n) < 0)
864 break;
865 }
866 close(proc.in);
867 if (use_sideband)
868 finish_async(&muxer);
869
870 sigchain_pop(SIGPIPE);
871
872 return finish_command(&proc);
873 }
874
875 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
876 {
877 struct receive_hook_feed_state *state = state_;
878 struct command *cmd = state->cmd;
879
880 while (cmd &&
881 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
882 cmd = cmd->next;
883 if (!cmd)
884 return -1; /* EOF */
885 if (!bufp)
886 return 0; /* OK, can feed something. */
887 strbuf_reset(&state->buf);
888 if (!state->report)
889 state->report = cmd->report;
890 if (state->report) {
891 struct object_id *old_oid;
892 struct object_id *new_oid;
893 const char *ref_name;
894
895 old_oid = state->report->old_oid ? state->report->old_oid : &cmd->old_oid;
896 new_oid = state->report->new_oid ? state->report->new_oid : &cmd->new_oid;
897 ref_name = state->report->ref_name ? state->report->ref_name : cmd->ref_name;
898 strbuf_addf(&state->buf, "%s %s %s\n",
899 oid_to_hex(old_oid), oid_to_hex(new_oid),
900 ref_name);
901 state->report = state->report->next;
902 if (!state->report)
903 state->cmd = cmd->next;
904 } else {
905 strbuf_addf(&state->buf, "%s %s %s\n",
906 oid_to_hex(&cmd->old_oid), oid_to_hex(&cmd->new_oid),
907 cmd->ref_name);
908 state->cmd = cmd->next;
909 }
910 if (bufp) {
911 *bufp = state->buf.buf;
912 *sizep = state->buf.len;
913 }
914 return 0;
915 }
916
917 static int run_receive_hook(struct command *commands,
918 const char *hook_name,
919 int skip_broken,
920 const struct string_list *push_options)
921 {
922 struct receive_hook_feed_state state;
923 int status;
924
925 strbuf_init(&state.buf, 0);
926 state.cmd = commands;
927 state.skip_broken = skip_broken;
928 state.report = NULL;
929 if (feed_receive_hook(&state, NULL, NULL))
930 return 0;
931 state.cmd = commands;
932 state.push_options = push_options;
933 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
934 strbuf_release(&state.buf);
935 return status;
936 }
937
938 static int run_update_hook(struct command *cmd)
939 {
940 struct child_process proc = CHILD_PROCESS_INIT;
941 int code;
942 const char *hook_path = find_hook("update");
943
944 if (!hook_path)
945 return 0;
946
947 strvec_push(&proc.args, hook_path);
948 strvec_push(&proc.args, cmd->ref_name);
949 strvec_push(&proc.args, oid_to_hex(&cmd->old_oid));
950 strvec_push(&proc.args, oid_to_hex(&cmd->new_oid));
951
952 proc.no_stdin = 1;
953 proc.stdout_to_stderr = 1;
954 proc.err = use_sideband ? -1 : 0;
955 proc.trace2_hook_name = "update";
956
957 code = start_command(&proc);
958 if (code)
959 return code;
960 if (use_sideband)
961 copy_to_sideband(proc.err, -1, NULL);
962 return finish_command(&proc);
963 }
964
965 static struct command *find_command_by_refname(struct command *list,
966 const char *refname)
967 {
968 for (; list; list = list->next)
969 if (!strcmp(list->ref_name, refname))
970 return list;
971 return NULL;
972 }
973
974 static int read_proc_receive_report(struct packet_reader *reader,
975 struct command *commands,
976 struct strbuf *errmsg)
977 {
978 struct command *cmd;
979 struct command *hint = NULL;
980 struct ref_push_report *report = NULL;
981 int new_report = 0;
982 int code = 0;
983 int once = 0;
984 int response = 0;
985
986 for (;;) {
987 struct object_id old_oid, new_oid;
988 const char *head;
989 const char *refname;
990 char *p;
991 enum packet_read_status status;
992
993 status = packet_reader_read(reader);
994 if (status != PACKET_READ_NORMAL) {
995 /* Check whether proc-receive exited abnormally */
996 if (status == PACKET_READ_EOF && !response) {
997 strbuf_addstr(errmsg, "proc-receive exited abnormally");
998 return -1;
999 }
1000 break;
1001 }
1002 response++;
1003
1004 head = reader->line;
1005 p = strchr(head, ' ');
1006 if (!p) {
1007 strbuf_addf(errmsg, "proc-receive reported incomplete status line: '%s'\n", head);
1008 code = -1;
1009 continue;
1010 }
1011 *p++ = '\0';
1012 if (!strcmp(head, "option")) {
1013 const char *key, *val;
1014
1015 if (!hint || !(report || new_report)) {
1016 if (!once++)
1017 strbuf_addstr(errmsg, "proc-receive reported 'option' without a matching 'ok/ng' directive\n");
1018 code = -1;
1019 continue;
1020 }
1021 if (new_report) {
1022 if (!hint->report) {
1023 CALLOC_ARRAY(hint->report, 1);
1024 report = hint->report;
1025 } else {
1026 report = hint->report;
1027 while (report->next)
1028 report = report->next;
1029 report->next = xcalloc(1, sizeof(struct ref_push_report));
1030 report = report->next;
1031 }
1032 new_report = 0;
1033 }
1034 key = p;
1035 p = strchr(key, ' ');
1036 if (p)
1037 *p++ = '\0';
1038 val = p;
1039 if (!strcmp(key, "refname"))
1040 report->ref_name = xstrdup_or_null(val);
1041 else if (!strcmp(key, "old-oid") && val &&
1042 !parse_oid_hex(val, &old_oid, &val))
1043 report->old_oid = oiddup(&old_oid);
1044 else if (!strcmp(key, "new-oid") && val &&
1045 !parse_oid_hex(val, &new_oid, &val))
1046 report->new_oid = oiddup(&new_oid);
1047 else if (!strcmp(key, "forced-update"))
1048 report->forced_update = 1;
1049 else if (!strcmp(key, "fall-through"))
1050 /* Fall through, let 'receive-pack' to execute it. */
1051 hint->run_proc_receive = 0;
1052 continue;
1053 }
1054
1055 report = NULL;
1056 new_report = 0;
1057 refname = p;
1058 p = strchr(refname, ' ');
1059 if (p)
1060 *p++ = '\0';
1061 if (strcmp(head, "ok") && strcmp(head, "ng")) {
1062 strbuf_addf(errmsg, "proc-receive reported bad status '%s' on ref '%s'\n",
1063 head, refname);
1064 code = -1;
1065 continue;
1066 }
1067
1068 /* first try searching at our hint, falling back to all refs */
1069 if (hint)
1070 hint = find_command_by_refname(hint, refname);
1071 if (!hint)
1072 hint = find_command_by_refname(commands, refname);
1073 if (!hint) {
1074 strbuf_addf(errmsg, "proc-receive reported status on unknown ref: %s\n",
1075 refname);
1076 code = -1;
1077 continue;
1078 }
1079 if (!hint->run_proc_receive) {
1080 strbuf_addf(errmsg, "proc-receive reported status on unexpected ref: %s\n",
1081 refname);
1082 code = -1;
1083 continue;
1084 }
1085 hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED;
1086 if (!strcmp(head, "ng")) {
1087 if (p)
1088 hint->error_string = xstrdup(p);
1089 else
1090 hint->error_string = "failed";
1091 code = -1;
1092 continue;
1093 }
1094 new_report = 1;
1095 }
1096
1097 for (cmd = commands; cmd; cmd = cmd->next)
1098 if (cmd->run_proc_receive && !cmd->error_string &&
1099 !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED)) {
1100 cmd->error_string = "proc-receive failed to report status";
1101 code = -1;
1102 }
1103 return code;
1104 }
1105
1106 static int run_proc_receive_hook(struct command *commands,
1107 const struct string_list *push_options)
1108 {
1109 struct child_process proc = CHILD_PROCESS_INIT;
1110 struct async muxer;
1111 struct command *cmd;
1112 struct packet_reader reader;
1113 struct strbuf cap = STRBUF_INIT;
1114 struct strbuf errmsg = STRBUF_INIT;
1115 int hook_use_push_options = 0;
1116 int version = 0;
1117 int code;
1118 const char *hook_path = find_hook("proc-receive");
1119
1120 if (!hook_path) {
1121 rp_error("cannot find hook 'proc-receive'");
1122 return -1;
1123 }
1124
1125 strvec_push(&proc.args, hook_path);
1126 proc.in = -1;
1127 proc.out = -1;
1128 proc.trace2_hook_name = "proc-receive";
1129
1130 if (use_sideband) {
1131 memset(&muxer, 0, sizeof(muxer));
1132 muxer.proc = copy_to_sideband;
1133 muxer.in = -1;
1134 code = start_async(&muxer);
1135 if (code)
1136 return code;
1137 proc.err = muxer.in;
1138 } else {
1139 proc.err = 0;
1140 }
1141
1142 code = start_command(&proc);
1143 if (code) {
1144 if (use_sideband)
1145 finish_async(&muxer);
1146 return code;
1147 }
1148
1149 sigchain_push(SIGPIPE, SIG_IGN);
1150
1151 /* Version negotiaton */
1152 packet_reader_init(&reader, proc.out, NULL, 0,
1153 PACKET_READ_CHOMP_NEWLINE |
1154 PACKET_READ_GENTLE_ON_EOF);
1155 if (use_atomic)
1156 strbuf_addstr(&cap, " atomic");
1157 if (use_push_options)
1158 strbuf_addstr(&cap, " push-options");
1159 if (cap.len) {
1160 code = packet_write_fmt_gently(proc.in, "version=1%c%s\n", '\0', cap.buf + 1);
1161 strbuf_release(&cap);
1162 } else {
1163 code = packet_write_fmt_gently(proc.in, "version=1\n");
1164 }
1165 if (!code)
1166 code = packet_flush_gently(proc.in);
1167
1168 if (!code)
1169 for (;;) {
1170 int linelen;
1171 enum packet_read_status status;
1172
1173 status = packet_reader_read(&reader);
1174 if (status != PACKET_READ_NORMAL) {
1175 /* Check whether proc-receive exited abnormally */
1176 if (status == PACKET_READ_EOF)
1177 code = -1;
1178 break;
1179 }
1180
1181 if (reader.pktlen > 8 && starts_with(reader.line, "version=")) {
1182 version = atoi(reader.line + 8);
1183 linelen = strlen(reader.line);
1184 if (linelen < reader.pktlen) {
1185 const char *feature_list = reader.line + linelen + 1;
1186 if (parse_feature_request(feature_list, "push-options"))
1187 hook_use_push_options = 1;
1188 }
1189 }
1190 }
1191
1192 if (code) {
1193 strbuf_addstr(&errmsg, "fail to negotiate version with proc-receive hook");
1194 goto cleanup;
1195 }
1196
1197 switch (version) {
1198 case 0:
1199 /* fallthrough */
1200 case 1:
1201 break;
1202 default:
1203 strbuf_addf(&errmsg, "proc-receive version '%d' is not supported",
1204 version);
1205 code = -1;
1206 goto cleanup;
1207 }
1208
1209 /* Send commands */
1210 for (cmd = commands; cmd; cmd = cmd->next) {
1211 if (!cmd->run_proc_receive || cmd->skip_update || cmd->error_string)
1212 continue;
1213 code = packet_write_fmt_gently(proc.in, "%s %s %s",
1214 oid_to_hex(&cmd->old_oid),
1215 oid_to_hex(&cmd->new_oid),
1216 cmd->ref_name);
1217 if (code)
1218 break;
1219 }
1220 if (!code)
1221 code = packet_flush_gently(proc.in);
1222 if (code) {
1223 strbuf_addstr(&errmsg, "fail to write commands to proc-receive hook");
1224 goto cleanup;
1225 }
1226
1227 /* Send push options */
1228 if (hook_use_push_options) {
1229 struct string_list_item *item;
1230
1231 for_each_string_list_item(item, push_options) {
1232 code = packet_write_fmt_gently(proc.in, "%s", item->string);
1233 if (code)
1234 break;
1235 }
1236 if (!code)
1237 code = packet_flush_gently(proc.in);
1238 if (code) {
1239 strbuf_addstr(&errmsg,
1240 "fail to write push-options to proc-receive hook");
1241 goto cleanup;
1242 }
1243 }
1244
1245 /* Read result from proc-receive */
1246 code = read_proc_receive_report(&reader, commands, &errmsg);
1247
1248 cleanup:
1249 close(proc.in);
1250 close(proc.out);
1251 if (use_sideband)
1252 finish_async(&muxer);
1253 if (finish_command(&proc))
1254 code = -1;
1255 if (errmsg.len >0) {
1256 char *p = errmsg.buf;
1257
1258 p += errmsg.len - 1;
1259 if (*p == '\n')
1260 *p = '\0';
1261 rp_error("%s", errmsg.buf);
1262 strbuf_release(&errmsg);
1263 }
1264 sigchain_pop(SIGPIPE);
1265
1266 return code;
1267 }
1268
1269 static char *refuse_unconfigured_deny_msg =
1270 N_("By default, updating the current branch in a non-bare repository\n"
1271 "is denied, because it will make the index and work tree inconsistent\n"
1272 "with what you pushed, and will require 'git reset --hard' to match\n"
1273 "the work tree to HEAD.\n"
1274 "\n"
1275 "You can set the 'receive.denyCurrentBranch' configuration variable\n"
1276 "to 'ignore' or 'warn' in the remote repository to allow pushing into\n"
1277 "its current branch; however, this is not recommended unless you\n"
1278 "arranged to update its work tree to match what you pushed in some\n"
1279 "other way.\n"
1280 "\n"
1281 "To squelch this message and still keep the default behaviour, set\n"
1282 "'receive.denyCurrentBranch' configuration variable to 'refuse'.");
1283
1284 static void refuse_unconfigured_deny(void)
1285 {
1286 rp_error("%s", _(refuse_unconfigured_deny_msg));
1287 }
1288
1289 static char *refuse_unconfigured_deny_delete_current_msg =
1290 N_("By default, deleting the current branch is denied, because the next\n"
1291 "'git clone' won't result in any file checked out, causing confusion.\n"
1292 "\n"
1293 "You can set 'receive.denyDeleteCurrent' configuration variable to\n"
1294 "'warn' or 'ignore' in the remote repository to allow deleting the\n"
1295 "current branch, with or without a warning message.\n"
1296 "\n"
1297 "To squelch this message, you can set it to 'refuse'.");
1298
1299 static void refuse_unconfigured_deny_delete_current(void)
1300 {
1301 rp_error("%s", _(refuse_unconfigured_deny_delete_current_msg));
1302 }
1303
1304 static const struct object_id *command_singleton_iterator(void *cb_data);
1305 static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
1306 {
1307 struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
1308 struct oid_array extra = OID_ARRAY_INIT;
1309 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1310 uint32_t mask = 1 << (cmd->index % 32);
1311 int i;
1312
1313 trace_printf_key(&trace_shallow,
1314 "shallow: update_shallow_ref %s\n", cmd->ref_name);
1315 for (i = 0; i < si->shallow->nr; i++)
1316 if (si->used_shallow[i] &&
1317 (si->used_shallow[i][cmd->index / 32] & mask) &&
1318 !delayed_reachability_test(si, i))
1319 oid_array_append(&extra, &si->shallow->oid[i]);
1320
1321 opt.env = tmp_objdir_env(tmp_objdir);
1322 setup_alternate_shallow(&shallow_lock, &opt.shallow_file, &extra);
1323 if (check_connected(command_singleton_iterator, cmd, &opt)) {
1324 rollback_shallow_file(the_repository, &shallow_lock);
1325 oid_array_clear(&extra);
1326 return -1;
1327 }
1328
1329 commit_shallow_file(the_repository, &shallow_lock);
1330
1331 /*
1332 * Make sure setup_alternate_shallow() for the next ref does
1333 * not lose these new roots..
1334 */
1335 for (i = 0; i < extra.nr; i++)
1336 register_shallow(the_repository, &extra.oid[i]);
1337
1338 si->shallow_ref[cmd->index] = 0;
1339 oid_array_clear(&extra);
1340 return 0;
1341 }
1342
1343 /*
1344 * NEEDSWORK: we should consolidate various implementions of "are we
1345 * on an unborn branch?" test into one, and make the unified one more
1346 * robust. !get_sha1() based check used here and elsewhere would not
1347 * allow us to tell an unborn branch from corrupt ref, for example.
1348 * For the purpose of fixing "deploy-to-update does not work when
1349 * pushing into an empty repository" issue, this should suffice for
1350 * now.
1351 */
1352 static int head_has_history(void)
1353 {
1354 struct object_id oid;
1355
1356 return !repo_get_oid(the_repository, "HEAD", &oid);
1357 }
1358
1359 static const char *push_to_deploy(unsigned char *sha1,
1360 struct strvec *env,
1361 const char *work_tree)
1362 {
1363 struct child_process child = CHILD_PROCESS_INIT;
1364
1365 strvec_pushl(&child.args, "update-index", "-q", "--ignore-submodules",
1366 "--refresh", NULL);
1367 strvec_pushv(&child.env, env->v);
1368 child.dir = work_tree;
1369 child.no_stdin = 1;
1370 child.stdout_to_stderr = 1;
1371 child.git_cmd = 1;
1372 if (run_command(&child))
1373 return "Up-to-date check failed";
1374
1375 /* run_command() does not clean up completely; reinitialize */
1376 child_process_init(&child);
1377 strvec_pushl(&child.args, "diff-files", "--quiet",
1378 "--ignore-submodules", "--", NULL);
1379 strvec_pushv(&child.env, env->v);
1380 child.dir = work_tree;
1381 child.no_stdin = 1;
1382 child.stdout_to_stderr = 1;
1383 child.git_cmd = 1;
1384 if (run_command(&child))
1385 return "Working directory has unstaged changes";
1386
1387 child_process_init(&child);
1388 strvec_pushl(&child.args, "diff-index", "--quiet", "--cached",
1389 "--ignore-submodules",
1390 /* diff-index with either HEAD or an empty tree */
1391 head_has_history() ? "HEAD" : empty_tree_oid_hex(),
1392 "--", NULL);
1393 strvec_pushv(&child.env, env->v);
1394 child.no_stdin = 1;
1395 child.no_stdout = 1;
1396 child.stdout_to_stderr = 0;
1397 child.git_cmd = 1;
1398 if (run_command(&child))
1399 return "Working directory has staged changes";
1400
1401 child_process_init(&child);
1402 strvec_pushl(&child.args, "read-tree", "-u", "-m", hash_to_hex(sha1),
1403 NULL);
1404 strvec_pushv(&child.env, env->v);
1405 child.dir = work_tree;
1406 child.no_stdin = 1;
1407 child.no_stdout = 1;
1408 child.stdout_to_stderr = 0;
1409 child.git_cmd = 1;
1410 if (run_command(&child))
1411 return "Could not update working tree to new HEAD";
1412
1413 return NULL;
1414 }
1415
1416 static const char *push_to_checkout_hook = "push-to-checkout";
1417
1418 static const char *push_to_checkout(unsigned char *hash,
1419 int *invoked_hook,
1420 struct strvec *env,
1421 const char *work_tree)
1422 {
1423 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
1424 opt.invoked_hook = invoked_hook;
1425
1426 strvec_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
1427 strvec_pushv(&opt.env, env->v);
1428 strvec_push(&opt.args, hash_to_hex(hash));
1429 if (run_hooks_opt(push_to_checkout_hook, &opt))
1430 return "push-to-checkout hook declined";
1431 else
1432 return NULL;
1433 }
1434
1435 static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree)
1436 {
1437 const char *retval, *git_dir;
1438 struct strvec env = STRVEC_INIT;
1439 int invoked_hook;
1440
1441 if (!worktree || !worktree->path)
1442 BUG("worktree->path must be non-NULL");
1443
1444 if (worktree->is_bare)
1445 return "denyCurrentBranch = updateInstead needs a worktree";
1446 git_dir = get_worktree_git_dir(worktree);
1447
1448 strvec_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir));
1449
1450 retval = push_to_checkout(sha1, &invoked_hook, &env, worktree->path);
1451 if (!invoked_hook)
1452 retval = push_to_deploy(sha1, &env, worktree->path);
1453
1454 strvec_clear(&env);
1455 return retval;
1456 }
1457
1458 static const char *update(struct command *cmd, struct shallow_info *si)
1459 {
1460 const char *name = cmd->ref_name;
1461 struct strbuf namespaced_name_buf = STRBUF_INIT;
1462 static char *namespaced_name;
1463 const char *ret;
1464 struct object_id *old_oid = &cmd->old_oid;
1465 struct object_id *new_oid = &cmd->new_oid;
1466 int do_update_worktree = 0;
1467 struct worktree **worktrees = get_worktrees();
1468 const struct worktree *worktree =
1469 find_shared_symref(worktrees, "HEAD", name);
1470
1471 /* only refs/... are allowed */
1472 if (!starts_with(name, "refs/") ||
1473 check_refname_format(name + 5, is_null_oid(new_oid) ?
1474 REFNAME_ALLOW_ONELEVEL : 0)) {
1475 rp_error("refusing to update funny ref '%s' remotely", name);
1476 ret = "funny refname";
1477 goto out;
1478 }
1479
1480 strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
1481 free(namespaced_name);
1482 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
1483
1484 if (worktree && !worktree->is_bare) {
1485 switch (deny_current_branch) {
1486 case DENY_IGNORE:
1487 break;
1488 case DENY_WARN:
1489 rp_warning("updating the current branch");
1490 break;
1491 case DENY_REFUSE:
1492 case DENY_UNCONFIGURED:
1493 rp_error("refusing to update checked out branch: %s", name);
1494 if (deny_current_branch == DENY_UNCONFIGURED)
1495 refuse_unconfigured_deny();
1496 ret = "branch is currently checked out";
1497 goto out;
1498 case DENY_UPDATE_INSTEAD:
1499 /* pass -- let other checks intervene first */
1500 do_update_worktree = 1;
1501 break;
1502 }
1503 }
1504
1505 if (!is_null_oid(new_oid) && !repo_has_object_file(the_repository, new_oid)) {
1506 error("unpack should have generated %s, "
1507 "but I can't find it!", oid_to_hex(new_oid));
1508 ret = "bad pack";
1509 goto out;
1510 }
1511
1512 if (!is_null_oid(old_oid) && is_null_oid(new_oid)) {
1513 if (deny_deletes && starts_with(name, "refs/heads/")) {
1514 rp_error("denying ref deletion for %s", name);
1515 ret = "deletion prohibited";
1516 goto out;
1517 }
1518
1519 if (worktree || (head_name && !strcmp(namespaced_name, head_name))) {
1520 switch (deny_delete_current) {
1521 case DENY_IGNORE:
1522 break;
1523 case DENY_WARN:
1524 rp_warning("deleting the current branch");
1525 break;
1526 case DENY_REFUSE:
1527 case DENY_UNCONFIGURED:
1528 case DENY_UPDATE_INSTEAD:
1529 if (deny_delete_current == DENY_UNCONFIGURED)
1530 refuse_unconfigured_deny_delete_current();
1531 rp_error("refusing to delete the current branch: %s", name);
1532 ret = "deletion of the current branch prohibited";
1533 goto out;
1534 default:
1535 ret = "Invalid denyDeleteCurrent setting";
1536 goto out;
1537 }
1538 }
1539 }
1540
1541 if (deny_non_fast_forwards && !is_null_oid(new_oid) &&
1542 !is_null_oid(old_oid) &&
1543 starts_with(name, "refs/heads/")) {
1544 struct object *old_object, *new_object;
1545 struct commit *old_commit, *new_commit;
1546
1547 old_object = parse_object(the_repository, old_oid);
1548 new_object = parse_object(the_repository, new_oid);
1549
1550 if (!old_object || !new_object ||
1551 old_object->type != OBJ_COMMIT ||
1552 new_object->type != OBJ_COMMIT) {
1553 error("bad sha1 objects for %s", name);
1554 ret = "bad ref";
1555 goto out;
1556 }
1557 old_commit = (struct commit *)old_object;
1558 new_commit = (struct commit *)new_object;
1559 if (!repo_in_merge_bases(the_repository, old_commit, new_commit)) {
1560 rp_error("denying non-fast-forward %s"
1561 " (you should pull first)", name);
1562 ret = "non-fast-forward";
1563 goto out;
1564 }
1565 }
1566 if (run_update_hook(cmd)) {
1567 rp_error("hook declined to update %s", name);
1568 ret = "hook declined";
1569 goto out;
1570 }
1571
1572 if (do_update_worktree) {
1573 ret = update_worktree(new_oid->hash, worktree);
1574 if (ret)
1575 goto out;
1576 }
1577
1578 if (is_null_oid(new_oid)) {
1579 struct strbuf err = STRBUF_INIT;
1580 if (!parse_object(the_repository, old_oid)) {
1581 old_oid = NULL;
1582 if (ref_exists(name)) {
1583 rp_warning("allowing deletion of corrupt ref");
1584 } else {
1585 rp_warning("deleting a non-existent ref");
1586 cmd->did_not_exist = 1;
1587 }
1588 }
1589 if (ref_transaction_delete(transaction,
1590 namespaced_name,
1591 old_oid,
1592 0, "push", &err)) {
1593 rp_error("%s", err.buf);
1594 ret = "failed to delete";
1595 } else {
1596 ret = NULL; /* good */
1597 }
1598 strbuf_release(&err);
1599 }
1600 else {
1601 struct strbuf err = STRBUF_INIT;
1602 if (shallow_update && si->shallow_ref[cmd->index] &&
1603 update_shallow_ref(cmd, si)) {
1604 ret = "shallow error";
1605 goto out;
1606 }
1607
1608 if (ref_transaction_update(transaction,
1609 namespaced_name,
1610 new_oid, old_oid,
1611 0, "push",
1612 &err)) {
1613 rp_error("%s", err.buf);
1614 ret = "failed to update ref";
1615 } else {
1616 ret = NULL; /* good */
1617 }
1618 strbuf_release(&err);
1619 }
1620
1621 out:
1622 free_worktrees(worktrees);
1623 return ret;
1624 }
1625
1626 static void run_update_post_hook(struct command *commands)
1627 {
1628 struct command *cmd;
1629 struct child_process proc = CHILD_PROCESS_INIT;
1630 const char *hook;
1631
1632 hook = find_hook("post-update");
1633 if (!hook)
1634 return;
1635
1636 for (cmd = commands; cmd; cmd = cmd->next) {
1637 if (cmd->error_string || cmd->did_not_exist)
1638 continue;
1639 if (!proc.args.nr)
1640 strvec_push(&proc.args, hook);
1641 strvec_push(&proc.args, cmd->ref_name);
1642 }
1643 if (!proc.args.nr)
1644 return;
1645
1646 proc.no_stdin = 1;
1647 proc.stdout_to_stderr = 1;
1648 proc.err = use_sideband ? -1 : 0;
1649 proc.trace2_hook_name = "post-update";
1650
1651 if (!start_command(&proc)) {
1652 if (use_sideband)
1653 copy_to_sideband(proc.err, -1, NULL);
1654 finish_command(&proc);
1655 }
1656 }
1657
1658 static void check_aliased_update_internal(struct command *cmd,
1659 struct string_list *list,
1660 const char *dst_name, int flag)
1661 {
1662 struct string_list_item *item;
1663 struct command *dst_cmd;
1664
1665 if (!(flag & REF_ISSYMREF))
1666 return;
1667
1668 if (!dst_name) {
1669 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
1670 cmd->skip_update = 1;
1671 cmd->error_string = "broken symref";
1672 return;
1673 }
1674 dst_name = strip_namespace(dst_name);
1675
1676 if (!(item = string_list_lookup(list, dst_name)))
1677 return;
1678
1679 cmd->skip_update = 1;
1680
1681 dst_cmd = (struct command *) item->util;
1682
1683 if (oideq(&cmd->old_oid, &dst_cmd->old_oid) &&
1684 oideq(&cmd->new_oid, &dst_cmd->new_oid))
1685 return;
1686
1687 dst_cmd->skip_update = 1;
1688
1689 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
1690 " its target '%s' (%s..%s)",
1691 cmd->ref_name,
1692 repo_find_unique_abbrev(the_repository, &cmd->old_oid, DEFAULT_ABBREV),
1693 repo_find_unique_abbrev(the_repository, &cmd->new_oid, DEFAULT_ABBREV),
1694 dst_cmd->ref_name,
1695 repo_find_unique_abbrev(the_repository, &dst_cmd->old_oid, DEFAULT_ABBREV),
1696 repo_find_unique_abbrev(the_repository, &dst_cmd->new_oid, DEFAULT_ABBREV));
1697
1698 cmd->error_string = dst_cmd->error_string =
1699 "inconsistent aliased update";
1700 }
1701
1702 static void check_aliased_update(struct command *cmd, struct string_list *list)
1703 {
1704 struct strbuf buf = STRBUF_INIT;
1705 const char *dst_name;
1706 int flag;
1707
1708 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
1709 dst_name = resolve_ref_unsafe(buf.buf, 0, NULL, &flag);
1710 check_aliased_update_internal(cmd, list, dst_name, flag);
1711 strbuf_release(&buf);
1712 }
1713
1714 static void check_aliased_updates(struct command *commands)
1715 {
1716 struct command *cmd;
1717 struct string_list ref_list = STRING_LIST_INIT_NODUP;
1718
1719 for (cmd = commands; cmd; cmd = cmd->next) {
1720 struct string_list_item *item =
1721 string_list_append(&ref_list, cmd->ref_name);
1722 item->util = (void *)cmd;
1723 }
1724 string_list_sort(&ref_list);
1725
1726 for (cmd = commands; cmd; cmd = cmd->next) {
1727 if (!cmd->error_string)
1728 check_aliased_update(cmd, &ref_list);
1729 }
1730
1731 string_list_clear(&ref_list, 0);
1732 }
1733
1734 static const struct object_id *command_singleton_iterator(void *cb_data)
1735 {
1736 struct command **cmd_list = cb_data;
1737 struct command *cmd = *cmd_list;
1738
1739 if (!cmd || is_null_oid(&cmd->new_oid))
1740 return NULL;
1741 *cmd_list = NULL; /* this returns only one */
1742 return &cmd->new_oid;
1743 }
1744
1745 static void set_connectivity_errors(struct command *commands,
1746 struct shallow_info *si)
1747 {
1748 struct command *cmd;
1749
1750 for (cmd = commands; cmd; cmd = cmd->next) {
1751 struct command *singleton = cmd;
1752 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1753
1754 if (shallow_update && si->shallow_ref[cmd->index])
1755 /* to be checked in update_shallow_ref() */
1756 continue;
1757
1758 opt.env = tmp_objdir_env(tmp_objdir);
1759 if (!check_connected(command_singleton_iterator, &singleton,
1760 &opt))
1761 continue;
1762
1763 cmd->error_string = "missing necessary objects";
1764 }
1765 }
1766
1767 struct iterate_data {
1768 struct command *cmds;
1769 struct shallow_info *si;
1770 };
1771
1772 static const struct object_id *iterate_receive_command_list(void *cb_data)
1773 {
1774 struct iterate_data *data = cb_data;
1775 struct command **cmd_list = &data->cmds;
1776 struct command *cmd = *cmd_list;
1777
1778 for (; cmd; cmd = cmd->next) {
1779 if (shallow_update && data->si->shallow_ref[cmd->index])
1780 /* to be checked in update_shallow_ref() */
1781 continue;
1782 if (!is_null_oid(&cmd->new_oid) && !cmd->skip_update) {
1783 *cmd_list = cmd->next;
1784 return &cmd->new_oid;
1785 }
1786 }
1787 return NULL;
1788 }
1789
1790 static void reject_updates_to_hidden(struct command *commands)
1791 {
1792 struct strbuf refname_full = STRBUF_INIT;
1793 size_t prefix_len;
1794 struct command *cmd;
1795
1796 strbuf_addstr(&refname_full, get_git_namespace());
1797 prefix_len = refname_full.len;
1798
1799 for (cmd = commands; cmd; cmd = cmd->next) {
1800 if (cmd->error_string)
1801 continue;
1802
1803 strbuf_setlen(&refname_full, prefix_len);
1804 strbuf_addstr(&refname_full, cmd->ref_name);
1805
1806 if (!ref_is_hidden(cmd->ref_name, refname_full.buf, &hidden_refs))
1807 continue;
1808 if (is_null_oid(&cmd->new_oid))
1809 cmd->error_string = "deny deleting a hidden ref";
1810 else
1811 cmd->error_string = "deny updating a hidden ref";
1812 }
1813
1814 strbuf_release(&refname_full);
1815 }
1816
1817 static int should_process_cmd(struct command *cmd)
1818 {
1819 return !cmd->error_string && !cmd->skip_update;
1820 }
1821
1822 static void BUG_if_skipped_connectivity_check(struct command *commands,
1823 struct shallow_info *si)
1824 {
1825 struct command *cmd;
1826
1827 for (cmd = commands; cmd; cmd = cmd->next) {
1828 if (should_process_cmd(cmd) && si->shallow_ref[cmd->index])
1829 bug("connectivity check has not been run on ref %s",
1830 cmd->ref_name);
1831 }
1832 BUG_if_bug("connectivity check skipped???");
1833 }
1834
1835 static void execute_commands_non_atomic(struct command *commands,
1836 struct shallow_info *si)
1837 {
1838 struct command *cmd;
1839 struct strbuf err = STRBUF_INIT;
1840
1841 for (cmd = commands; cmd; cmd = cmd->next) {
1842 if (!should_process_cmd(cmd) || cmd->run_proc_receive)
1843 continue;
1844
1845 transaction = ref_transaction_begin(&err);
1846 if (!transaction) {
1847 rp_error("%s", err.buf);
1848 strbuf_reset(&err);
1849 cmd->error_string = "transaction failed to start";
1850 continue;
1851 }
1852
1853 cmd->error_string = update(cmd, si);
1854
1855 if (!cmd->error_string
1856 && ref_transaction_commit(transaction, &err)) {
1857 rp_error("%s", err.buf);
1858 strbuf_reset(&err);
1859 cmd->error_string = "failed to update ref";
1860 }
1861 ref_transaction_free(transaction);
1862 }
1863 strbuf_release(&err);
1864 }
1865
1866 static void execute_commands_atomic(struct command *commands,
1867 struct shallow_info *si)
1868 {
1869 struct command *cmd;
1870 struct strbuf err = STRBUF_INIT;
1871 const char *reported_error = "atomic push failure";
1872
1873 transaction = ref_transaction_begin(&err);
1874 if (!transaction) {
1875 rp_error("%s", err.buf);
1876 strbuf_reset(&err);
1877 reported_error = "transaction failed to start";
1878 goto failure;
1879 }
1880
1881 for (cmd = commands; cmd; cmd = cmd->next) {
1882 if (!should_process_cmd(cmd) || cmd->run_proc_receive)
1883 continue;
1884
1885 cmd->error_string = update(cmd, si);
1886
1887 if (cmd->error_string)
1888 goto failure;
1889 }
1890
1891 if (ref_transaction_commit(transaction, &err)) {
1892 rp_error("%s", err.buf);
1893 reported_error = "atomic transaction failed";
1894 goto failure;
1895 }
1896 goto cleanup;
1897
1898 failure:
1899 for (cmd = commands; cmd; cmd = cmd->next)
1900 if (!cmd->error_string)
1901 cmd->error_string = reported_error;
1902
1903 cleanup:
1904 ref_transaction_free(transaction);
1905 strbuf_release(&err);
1906 }
1907
1908 static void execute_commands(struct command *commands,
1909 const char *unpacker_error,
1910 struct shallow_info *si,
1911 const struct string_list *push_options)
1912 {
1913 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1914 struct command *cmd;
1915 struct iterate_data data;
1916 struct async muxer;
1917 int err_fd = 0;
1918 int run_proc_receive = 0;
1919
1920 if (unpacker_error) {
1921 for (cmd = commands; cmd; cmd = cmd->next)
1922 cmd->error_string = "unpacker error";
1923 return;
1924 }
1925
1926 if (use_sideband) {
1927 memset(&muxer, 0, sizeof(muxer));
1928 muxer.proc = copy_to_sideband;
1929 muxer.in = -1;
1930 if (!start_async(&muxer))
1931 err_fd = muxer.in;
1932 /* ...else, continue without relaying sideband */
1933 }
1934
1935 data.cmds = commands;
1936 data.si = si;
1937 opt.err_fd = err_fd;
1938 opt.progress = err_fd && !quiet;
1939 opt.env = tmp_objdir_env(tmp_objdir);
1940 opt.exclude_hidden_refs_section = "receive";
1941
1942 if (check_connected(iterate_receive_command_list, &data, &opt))
1943 set_connectivity_errors(commands, si);
1944
1945 if (use_sideband)
1946 finish_async(&muxer);
1947
1948 reject_updates_to_hidden(commands);
1949
1950 /*
1951 * Try to find commands that have special prefix in their reference names,
1952 * and mark them to run an external "proc-receive" hook later.
1953 */
1954 if (proc_receive_ref) {
1955 for (cmd = commands; cmd; cmd = cmd->next) {
1956 if (!should_process_cmd(cmd))
1957 continue;
1958
1959 if (proc_receive_ref_matches(cmd)) {
1960 cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED;
1961 run_proc_receive = 1;
1962 }
1963 }
1964 }
1965
1966 if (run_receive_hook(commands, "pre-receive", 0, push_options)) {
1967 for (cmd = commands; cmd; cmd = cmd->next) {
1968 if (!cmd->error_string)
1969 cmd->error_string = "pre-receive hook declined";
1970 }
1971 return;
1972 }
1973
1974 /*
1975 * If there is no command ready to run, should return directly to destroy
1976 * temporary data in the quarantine area.
1977 */
1978 for (cmd = commands; cmd && cmd->error_string; cmd = cmd->next)
1979 ; /* nothing */
1980 if (!cmd)
1981 return;
1982
1983 /*
1984 * Now we'll start writing out refs, which means the objects need
1985 * to be in their final positions so that other processes can see them.
1986 */
1987 if (tmp_objdir_migrate(tmp_objdir) < 0) {
1988 for (cmd = commands; cmd; cmd = cmd->next) {
1989 if (!cmd->error_string)
1990 cmd->error_string = "unable to migrate objects to permanent storage";
1991 }
1992 return;
1993 }
1994 tmp_objdir = NULL;
1995
1996 check_aliased_updates(commands);
1997
1998 free(head_name_to_free);
1999 head_name = head_name_to_free = resolve_refdup("HEAD", 0, NULL, NULL);
2000
2001 if (run_proc_receive &&
2002 run_proc_receive_hook(commands, push_options))
2003 for (cmd = commands; cmd; cmd = cmd->next)
2004 if (!cmd->error_string &&
2005 !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED) &&
2006 (cmd->run_proc_receive || use_atomic))
2007 cmd->error_string = "fail to run proc-receive hook";
2008
2009 if (use_atomic)
2010 execute_commands_atomic(commands, si);
2011 else
2012 execute_commands_non_atomic(commands, si);
2013
2014 if (shallow_update)
2015 BUG_if_skipped_connectivity_check(commands, si);
2016 }
2017
2018 static struct command **queue_command(struct command **tail,
2019 const char *line,
2020 int linelen)
2021 {
2022 struct object_id old_oid, new_oid;
2023 struct command *cmd;
2024 const char *refname;
2025 int reflen;
2026 const char *p;
2027
2028 if (parse_oid_hex(line, &old_oid, &p) ||
2029 *p++ != ' ' ||
2030 parse_oid_hex(p, &new_oid, &p) ||
2031 *p++ != ' ')
2032 die("protocol error: expected old/new/ref, got '%s'", line);
2033
2034 refname = p;
2035 reflen = linelen - (p - line);
2036 FLEX_ALLOC_MEM(cmd, ref_name, refname, reflen);
2037 oidcpy(&cmd->old_oid, &old_oid);
2038 oidcpy(&cmd->new_oid, &new_oid);
2039 *tail = cmd;
2040 return &cmd->next;
2041 }
2042
2043 static void free_commands(struct command *commands)
2044 {
2045 while (commands) {
2046 struct command *next = commands->next;
2047
2048 free(commands);
2049 commands = next;
2050 }
2051 }
2052
2053 static void queue_commands_from_cert(struct command **tail,
2054 struct strbuf *push_cert)
2055 {
2056 const char *boc, *eoc;
2057
2058 if (*tail)
2059 die("protocol error: got both push certificate and unsigned commands");
2060
2061 boc = strstr(push_cert->buf, "\n\n");
2062 if (!boc)
2063 die("malformed push certificate %.*s", 100, push_cert->buf);
2064 else
2065 boc += 2;
2066 eoc = push_cert->buf + parse_signed_buffer(push_cert->buf, push_cert->len);
2067
2068 while (boc < eoc) {
2069 const char *eol = memchr(boc, '\n', eoc - boc);
2070 tail = queue_command(tail, boc, eol ? eol - boc : eoc - boc);
2071 boc = eol ? eol + 1 : eoc;
2072 }
2073 }
2074
2075 static struct command *read_head_info(struct packet_reader *reader,
2076 struct oid_array *shallow)
2077 {
2078 struct command *commands = NULL;
2079 struct command **p = &commands;
2080 for (;;) {
2081 int linelen;
2082
2083 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
2084 break;
2085
2086 if (reader->pktlen > 8 && starts_with(reader->line, "shallow ")) {
2087 struct object_id oid;
2088 if (get_oid_hex(reader->line + 8, &oid))
2089 die("protocol error: expected shallow sha, got '%s'",
2090 reader->line + 8);
2091 oid_array_append(shallow, &oid);
2092 continue;
2093 }
2094
2095 linelen = strlen(reader->line);
2096 if (linelen < reader->pktlen) {
2097 const char *feature_list = reader->line + linelen + 1;
2098 const char *hash = NULL;
2099 const char *client_sid;
2100 size_t len = 0;
2101 if (parse_feature_request(feature_list, "report-status"))
2102 report_status = 1;
2103 if (parse_feature_request(feature_list, "report-status-v2"))
2104 report_status_v2 = 1;
2105 if (parse_feature_request(feature_list, "side-band-64k"))
2106 use_sideband = LARGE_PACKET_MAX;
2107 if (parse_feature_request(feature_list, "quiet"))
2108 quiet = 1;
2109 if (advertise_atomic_push
2110 && parse_feature_request(feature_list, "atomic"))
2111 use_atomic = 1;
2112 if (advertise_push_options
2113 && parse_feature_request(feature_list, "push-options"))
2114 use_push_options = 1;
2115 hash = parse_feature_value(feature_list, "object-format", &len, NULL);
2116 if (!hash) {
2117 hash = hash_algos[GIT_HASH_SHA1].name;
2118 len = strlen(hash);
2119 }
2120 if (xstrncmpz(the_hash_algo->name, hash, len))
2121 die("error: unsupported object format '%s'", hash);
2122 client_sid = parse_feature_value(feature_list, "session-id", &len, NULL);
2123 if (client_sid) {
2124 char *sid = xstrndup(client_sid, len);
2125 trace2_data_string("transfer", NULL, "client-sid", client_sid);
2126 free(sid);
2127 }
2128 }
2129
2130 if (!strcmp(reader->line, "push-cert")) {
2131 int true_flush = 0;
2132 int saved_options = reader->options;
2133 reader->options &= ~PACKET_READ_CHOMP_NEWLINE;
2134
2135 for (;;) {
2136 packet_reader_read(reader);
2137 if (reader->status == PACKET_READ_FLUSH) {
2138 true_flush = 1;
2139 break;
2140 }
2141 if (reader->status != PACKET_READ_NORMAL) {
2142 die("protocol error: got an unexpected packet");
2143 }
2144 if (!strcmp(reader->line, "push-cert-end\n"))
2145 break; /* end of cert */
2146 strbuf_addstr(&push_cert, reader->line);
2147 }
2148 reader->options = saved_options;
2149
2150 if (true_flush)
2151 break;
2152 continue;
2153 }
2154
2155 p = queue_command(p, reader->line, linelen);
2156 }
2157
2158 if (push_cert.len)
2159 queue_commands_from_cert(p, &push_cert);
2160
2161 return commands;
2162 }
2163
2164 static void read_push_options(struct packet_reader *reader,
2165 struct string_list *options)
2166 {
2167 while (1) {
2168 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
2169 break;
2170
2171 string_list_append(options, reader->line);
2172 }
2173 }
2174
2175 static const char *parse_pack_header(struct pack_header *hdr)
2176 {
2177 switch (read_pack_header(0, hdr)) {
2178 case PH_ERROR_EOF:
2179 return "eof before pack header was fully read";
2180
2181 case PH_ERROR_PACK_SIGNATURE:
2182 return "protocol error (pack signature mismatch detected)";
2183
2184 case PH_ERROR_PROTOCOL:
2185 return "protocol error (pack version unsupported)";
2186
2187 default:
2188 return "unknown error in parse_pack_header";
2189
2190 case 0:
2191 return NULL;
2192 }
2193 }
2194
2195 static struct tempfile *pack_lockfile;
2196
2197 static void push_header_arg(struct strvec *args, struct pack_header *hdr)
2198 {
2199 strvec_pushf(args, "--pack_header=%"PRIu32",%"PRIu32,
2200 ntohl(hdr->hdr_version), ntohl(hdr->hdr_entries));
2201 }
2202
2203 static const char *unpack(int err_fd, struct shallow_info *si)
2204 {
2205 struct pack_header hdr;
2206 const char *hdr_err;
2207 int status;
2208 struct child_process child = CHILD_PROCESS_INIT;
2209 int fsck_objects = (receive_fsck_objects >= 0
2210 ? receive_fsck_objects
2211 : transfer_fsck_objects >= 0
2212 ? transfer_fsck_objects
2213 : 0);
2214
2215 hdr_err = parse_pack_header(&hdr);
2216 if (hdr_err) {
2217 if (err_fd > 0)
2218 close(err_fd);
2219 return hdr_err;
2220 }
2221
2222 if (si->nr_ours || si->nr_theirs) {
2223 alt_shallow_file = setup_temporary_shallow(si->shallow);
2224 strvec_push(&child.args, "--shallow-file");
2225 strvec_push(&child.args, alt_shallow_file);
2226 }
2227
2228 tmp_objdir = tmp_objdir_create("incoming");
2229 if (!tmp_objdir) {
2230 if (err_fd > 0)
2231 close(err_fd);
2232 return "unable to create temporary object directory";
2233 }
2234 strvec_pushv(&child.env, tmp_objdir_env(tmp_objdir));
2235
2236 /*
2237 * Normally we just pass the tmp_objdir environment to the child
2238 * processes that do the heavy lifting, but we may need to see these
2239 * objects ourselves to set up shallow information.
2240 */
2241 tmp_objdir_add_as_alternate(tmp_objdir);
2242
2243 if (ntohl(hdr.hdr_entries) < unpack_limit) {
2244 strvec_push(&child.args, "unpack-objects");
2245 push_header_arg(&child.args, &hdr);
2246 if (quiet)
2247 strvec_push(&child.args, "-q");
2248 if (fsck_objects)
2249 strvec_pushf(&child.args, "--strict%s",
2250 fsck_msg_types.buf);
2251 if (max_input_size)
2252 strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
2253 (uintmax_t)max_input_size);
2254 child.no_stdout = 1;
2255 child.err = err_fd;
2256 child.git_cmd = 1;
2257 status = run_command(&child);
2258 if (status)
2259 return "unpack-objects abnormal exit";
2260 } else {
2261 char hostname[HOST_NAME_MAX + 1];
2262 char *lockfile;
2263
2264 strvec_pushl(&child.args, "index-pack", "--stdin", NULL);
2265 push_header_arg(&child.args, &hdr);
2266
2267 if (xgethostname(hostname, sizeof(hostname)))
2268 xsnprintf(hostname, sizeof(hostname), "localhost");
2269 strvec_pushf(&child.args,
2270 "--keep=receive-pack %"PRIuMAX" on %s",
2271 (uintmax_t)getpid(),
2272 hostname);
2273
2274 if (!quiet && err_fd)
2275 strvec_push(&child.args, "--show-resolving-progress");
2276 if (use_sideband)
2277 strvec_push(&child.args, "--report-end-of-input");
2278 if (fsck_objects)
2279 strvec_pushf(&child.args, "--strict%s",
2280 fsck_msg_types.buf);
2281 if (!reject_thin)
2282 strvec_push(&child.args, "--fix-thin");
2283 if (max_input_size)
2284 strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX,
2285 (uintmax_t)max_input_size);
2286 child.out = -1;
2287 child.err = err_fd;
2288 child.git_cmd = 1;
2289 status = start_command(&child);
2290 if (status)
2291 return "index-pack fork failed";
2292
2293 lockfile = index_pack_lockfile(child.out, NULL);
2294 if (lockfile) {
2295 pack_lockfile = register_tempfile(lockfile);
2296 free(lockfile);
2297 }
2298 close(child.out);
2299
2300 status = finish_command(&child);
2301 if (status)
2302 return "index-pack abnormal exit";
2303 reprepare_packed_git(the_repository);
2304 }
2305 return NULL;
2306 }
2307
2308 static const char *unpack_with_sideband(struct shallow_info *si)
2309 {
2310 struct async muxer;
2311 const char *ret;
2312
2313 if (!use_sideband)
2314 return unpack(0, si);
2315
2316 use_keepalive = KEEPALIVE_AFTER_NUL;
2317 memset(&muxer, 0, sizeof(muxer));
2318 muxer.proc = copy_to_sideband;
2319 muxer.in = -1;
2320 if (start_async(&muxer))
2321 return NULL;
2322
2323 ret = unpack(muxer.in, si);
2324
2325 finish_async(&muxer);
2326 return ret;
2327 }
2328
2329 static void prepare_shallow_update(struct shallow_info *si)
2330 {
2331 int i, j, k, bitmap_size = DIV_ROUND_UP(si->ref->nr, 32);
2332
2333 ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
2334 assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
2335
2336 CALLOC_ARRAY(si->need_reachability_test, si->shallow->nr);
2337 CALLOC_ARRAY(si->reachable, si->shallow->nr);
2338 CALLOC_ARRAY(si->shallow_ref, si->ref->nr);
2339
2340 for (i = 0; i < si->nr_ours; i++)
2341 si->need_reachability_test[si->ours[i]] = 1;
2342
2343 for (i = 0; i < si->shallow->nr; i++) {
2344 if (!si->used_shallow[i])
2345 continue;
2346 for (j = 0; j < bitmap_size; j++) {
2347 if (!si->used_shallow[i][j])
2348 continue;
2349 si->need_reachability_test[i]++;
2350 for (k = 0; k < 32; k++)
2351 if (si->used_shallow[i][j] & (1U << k))
2352 si->shallow_ref[j * 32 + k]++;
2353 }
2354
2355 /*
2356 * true for those associated with some refs and belong
2357 * in "ours" list aka "step 7 not done yet"
2358 */
2359 si->need_reachability_test[i] =
2360 si->need_reachability_test[i] > 1;
2361 }
2362
2363 /*
2364 * keep hooks happy by forcing a temporary shallow file via
2365 * env variable because we can't add --shallow-file to every
2366 * command. check_connected() will be done with
2367 * true .git/shallow though.
2368 */
2369 setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
2370 }
2371
2372 static void update_shallow_info(struct command *commands,
2373 struct shallow_info *si,
2374 struct oid_array *ref)
2375 {
2376 struct command *cmd;
2377 int *ref_status;
2378 remove_nonexistent_theirs_shallow(si);
2379 if (!si->nr_ours && !si->nr_theirs) {
2380 shallow_update = 0;
2381 return;
2382 }
2383
2384 for (cmd = commands; cmd; cmd = cmd->next) {
2385 if (is_null_oid(&cmd->new_oid))
2386 continue;
2387 oid_array_append(ref, &cmd->new_oid);
2388 cmd->index = ref->nr - 1;
2389 }
2390 si->ref = ref;
2391
2392 if (shallow_update) {
2393 prepare_shallow_update(si);
2394 return;
2395 }
2396
2397 ALLOC_ARRAY(ref_status, ref->nr);
2398 assign_shallow_commits_to_refs(si, NULL, ref_status);
2399 for (cmd = commands; cmd; cmd = cmd->next) {
2400 if (is_null_oid(&cmd->new_oid))
2401 continue;
2402 if (ref_status[cmd->index]) {
2403 cmd->error_string = "shallow update not allowed";
2404 cmd->skip_update = 1;
2405 }
2406 }
2407 free(ref_status);
2408 }
2409
2410 static void report(struct command *commands, const char *unpack_status)
2411 {
2412 struct command *cmd;
2413 struct strbuf buf = STRBUF_INIT;
2414
2415 packet_buf_write(&buf, "unpack %s\n",
2416 unpack_status ? unpack_status : "ok");
2417 for (cmd = commands; cmd; cmd = cmd->next) {
2418 if (!cmd->error_string)
2419 packet_buf_write(&buf, "ok %s\n",
2420 cmd->ref_name);
2421 else
2422 packet_buf_write(&buf, "ng %s %s\n",
2423 cmd->ref_name, cmd->error_string);
2424 }
2425 packet_buf_flush(&buf);
2426
2427 if (use_sideband)
2428 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
2429 else
2430 write_or_die(1, buf.buf, buf.len);
2431 strbuf_release(&buf);
2432 }
2433
2434 static void report_v2(struct command *commands, const char *unpack_status)
2435 {
2436 struct command *cmd;
2437 struct strbuf buf = STRBUF_INIT;
2438 struct ref_push_report *report;
2439
2440 packet_buf_write(&buf, "unpack %s\n",
2441 unpack_status ? unpack_status : "ok");
2442 for (cmd = commands; cmd; cmd = cmd->next) {
2443 int count = 0;
2444
2445 if (cmd->error_string) {
2446 packet_buf_write(&buf, "ng %s %s\n",
2447 cmd->ref_name,
2448 cmd->error_string);
2449 continue;
2450 }
2451 packet_buf_write(&buf, "ok %s\n",
2452 cmd->ref_name);
2453 for (report = cmd->report; report; report = report->next) {
2454 if (count++ > 0)
2455 packet_buf_write(&buf, "ok %s\n",
2456 cmd->ref_name);
2457 if (report->ref_name)
2458 packet_buf_write(&buf, "option refname %s\n",
2459 report->ref_name);
2460 if (report->old_oid)
2461 packet_buf_write(&buf, "option old-oid %s\n",
2462 oid_to_hex(report->old_oid));
2463 if (report->new_oid)
2464 packet_buf_write(&buf, "option new-oid %s\n",
2465 oid_to_hex(report->new_oid));
2466 if (report->forced_update)
2467 packet_buf_write(&buf, "option forced-update\n");
2468 }
2469 }
2470 packet_buf_flush(&buf);
2471
2472 if (use_sideband)
2473 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
2474 else
2475 write_or_die(1, buf.buf, buf.len);
2476 strbuf_release(&buf);
2477 }
2478
2479 static int delete_only(struct command *commands)
2480 {
2481 struct command *cmd;
2482 for (cmd = commands; cmd; cmd = cmd->next) {
2483 if (!is_null_oid(&cmd->new_oid))
2484 return 0;
2485 }
2486 return 1;
2487 }
2488
2489 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
2490 {
2491 int advertise_refs = 0;
2492 struct command *commands;
2493 struct oid_array shallow = OID_ARRAY_INIT;
2494 struct oid_array ref = OID_ARRAY_INIT;
2495 struct shallow_info si;
2496 struct packet_reader reader;
2497
2498 struct option options[] = {
2499 OPT__QUIET(&quiet, N_("quiet")),
2500 OPT_HIDDEN_BOOL(0, "stateless-rpc", &stateless_rpc, NULL),
2501 OPT_HIDDEN_BOOL(0, "http-backend-info-refs", &advertise_refs, NULL),
2502 OPT_ALIAS(0, "advertise-refs", "http-backend-info-refs"),
2503 OPT_HIDDEN_BOOL(0, "reject-thin-pack-for-testing", &reject_thin, NULL),
2504 OPT_END()
2505 };
2506
2507 packet_trace_identity("receive-pack");
2508
2509 argc = parse_options(argc, argv, prefix, options, receive_pack_usage, 0);
2510
2511 if (argc > 1)
2512 usage_msg_opt(_("too many arguments"), receive_pack_usage, options);
2513 if (argc == 0)
2514 usage_msg_opt(_("you must specify a directory"), receive_pack_usage, options);
2515
2516 service_dir = argv[0];
2517
2518 setup_path();
2519
2520 if (!enter_repo(service_dir, 0))
2521 die("'%s' does not appear to be a git repository", service_dir);
2522
2523 git_config(receive_pack_config, NULL);
2524 if (cert_nonce_seed)
2525 push_cert_nonce = prepare_push_cert_nonce(service_dir, time(NULL));
2526
2527 if (0 <= transfer_unpack_limit)
2528 unpack_limit = transfer_unpack_limit;
2529 else if (0 <= receive_unpack_limit)
2530 unpack_limit = receive_unpack_limit;
2531
2532 switch (determine_protocol_version_server()) {
2533 case protocol_v2:
2534 /*
2535 * push support for protocol v2 has not been implemented yet,
2536 * so ignore the request to use v2 and fallback to using v0.
2537 */
2538 break;
2539 case protocol_v1:
2540 /*
2541 * v1 is just the original protocol with a version string,
2542 * so just fall through after writing the version string.
2543 */
2544 if (advertise_refs || !stateless_rpc)
2545 packet_write_fmt(1, "version 1\n");
2546
2547 /* fallthrough */
2548 case protocol_v0:
2549 break;
2550 case protocol_unknown_version:
2551 BUG("unknown protocol version");
2552 }
2553
2554 if (advertise_refs || !stateless_rpc) {
2555 write_head_info();
2556 }
2557 if (advertise_refs)
2558 return 0;
2559
2560 packet_reader_init(&reader, 0, NULL, 0,
2561 PACKET_READ_CHOMP_NEWLINE |
2562 PACKET_READ_DIE_ON_ERR_PACKET);
2563
2564 if ((commands = read_head_info(&reader, &shallow))) {
2565 const char *unpack_status = NULL;
2566 struct string_list push_options = STRING_LIST_INIT_DUP;
2567
2568 if (use_push_options)
2569 read_push_options(&reader, &push_options);
2570 if (!check_cert_push_options(&push_options)) {
2571 struct command *cmd;
2572 for (cmd = commands; cmd; cmd = cmd->next)
2573 cmd->error_string = "inconsistent push options";
2574 }
2575
2576 prepare_shallow_info(&si, &shallow);
2577 if (!si.nr_ours && !si.nr_theirs)
2578 shallow_update = 0;
2579 if (!delete_only(commands)) {
2580 unpack_status = unpack_with_sideband(&si);
2581 update_shallow_info(commands, &si, &ref);
2582 }
2583 use_keepalive = KEEPALIVE_ALWAYS;
2584 execute_commands(commands, unpack_status, &si,
2585 &push_options);
2586 delete_tempfile(&pack_lockfile);
2587 sigchain_push(SIGPIPE, SIG_IGN);
2588 if (report_status_v2)
2589 report_v2(commands, unpack_status);
2590 else if (report_status)
2591 report(commands, unpack_status);
2592 sigchain_pop(SIGPIPE);
2593 run_receive_hook(commands, "post-receive", 1,
2594 &push_options);
2595 run_update_post_hook(commands);
2596 free_commands(commands);
2597 string_list_clear(&push_options, 0);
2598 if (auto_gc) {
2599 struct child_process proc = CHILD_PROCESS_INIT;
2600
2601 proc.no_stdin = 1;
2602 proc.stdout_to_stderr = 1;
2603 proc.err = use_sideband ? -1 : 0;
2604 proc.git_cmd = proc.close_object_store = 1;
2605 strvec_pushl(&proc.args, "gc", "--auto", "--quiet",
2606 NULL);
2607
2608 if (!start_command(&proc)) {
2609 if (use_sideband)
2610 copy_to_sideband(proc.err, -1, NULL);
2611 finish_command(&proc);
2612 }
2613 }
2614 if (auto_update_server_info)
2615 update_server_info(0);
2616 clear_shallow_info(&si);
2617 }
2618 if (use_sideband)
2619 packet_flush(1);
2620 oid_array_clear(&shallow);
2621 oid_array_clear(&ref);
2622 string_list_clear(&hidden_refs, 0);
2623 free((void *)push_cert_nonce);
2624 return 0;
2625 }