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