fsck (receive-pack): allow demoting errors to warnings
[git/git.git] / builtin / receive-pack.c
CommitLineData
1e4cd68c 1#include "builtin.h"
697cc8ef 2#include "lockfile.h"
fc04c412 3#include "pack.h"
8a65ff76 4#include "refs.h"
f3a3214e 5#include "pkt-line.h"
38a81b4e 6#include "sideband.h"
b1bf95bb 7#include "run-command.h"
576162a4 8#include "exec_cmd.h"
11031d7e
JS
9#include "commit.h"
10#include "object.h"
d79796bc 11#include "remote.h"
47a59185 12#include "connect.h"
d79796bc 13#include "transport.h"
da3efdb1 14#include "string-list.h"
cff38a5e 15#include "sha1-array.h"
52fed6e1 16#include "connected.h"
31c42bff 17#include "argv-array.h"
ff5effdf 18#include "version.h"
d05b9618
JH
19#include "tag.h"
20#include "gpg-interface.h"
ec7dbd14 21#include "sigchain.h"
5d477a33 22#include "fsck.h"
575f4974 23
34263de0 24static const char receive_pack_usage[] = "git receive-pack <git-dir>";
575f4974 25
986e8239 26enum deny_action {
3d95d92b 27 DENY_UNCONFIGURED,
986e8239
JK
28 DENY_IGNORE,
29 DENY_WARN,
1404bcbb
JS
30 DENY_REFUSE,
31 DENY_UPDATE_INSTEAD
986e8239
JK
32};
33
1b53a076
JH
34static int deny_deletes;
35static int deny_non_fast_forwards;
3d95d92b 36static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
747ca245 37static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
dab76d3a
JH
38static int receive_fsck_objects = -1;
39static int transfer_fsck_objects = -1;
5d477a33 40static struct strbuf fsck_msg_types = STRBUF_INIT;
e28714c5
JH
41static int receive_unpack_limit = -1;
42static int transfer_unpack_limit = -1;
1b70fe5d 43static int advertise_atomic_push = 1;
46732fae 44static int unpack_limit = 100;
96f1e58f 45static int report_status;
38a81b4e 46static int use_sideband;
68deed29 47static int use_atomic;
c207e34f 48static int quiet;
b74fce16 49static int prefer_ofs_delta = 1;
77e3efbf
JH
50static int auto_update_server_info;
51static int auto_gc = 1;
f7c815c3 52static int fix_thin = 1;
5732373d
JH
53static int stateless_rpc;
54static const char *service_dir;
747ca245 55static const char *head_name;
96ec7b1e 56static void *head_name_to_free;
185c04e0 57static int sent_capabilities;
0a1bc12b 58static int shallow_update;
5dbd7676 59static const char *alt_shallow_file;
a85b377d
JH
60static struct strbuf push_cert = STRBUF_INIT;
61static unsigned char push_cert_sha1[20];
d05b9618 62static struct signature_check sigcheck;
b89363e4
JH
63static const char *push_cert_nonce;
64static const char *cert_nonce_seed;
65
66static const char *NONCE_UNSOLICITED = "UNSOLICITED";
67static const char *NONCE_BAD = "BAD";
68static const char *NONCE_MISSING = "MISSING";
69static const char *NONCE_OK = "OK";
5732373d 70static const char *NONCE_SLOP = "SLOP";
b89363e4 71static const char *nonce_status;
5732373d
JH
72static long nonce_stamp_slop;
73static unsigned long nonce_stamp_slop_limit;
222368c6 74static struct ref_transaction *transaction;
cfee10a7 75
986e8239
JK
76static enum deny_action parse_deny_action(const char *var, const char *value)
77{
78 if (value) {
79 if (!strcasecmp(value, "ignore"))
80 return DENY_IGNORE;
81 if (!strcasecmp(value, "warn"))
82 return DENY_WARN;
83 if (!strcasecmp(value, "refuse"))
84 return DENY_REFUSE;
1404bcbb
JS
85 if (!strcasecmp(value, "updateinstead"))
86 return DENY_UPDATE_INSTEAD;
986e8239
JK
87 }
88 if (git_config_bool(var, value))
89 return DENY_REFUSE;
90 return DENY_IGNORE;
91}
92
ef90d6d4 93static int receive_pack_config(const char *var, const char *value, void *cb)
6fb75bed 94{
daebaa78
JH
95 int status = parse_hide_refs_config(var, value, "receive");
96
97 if (status)
98 return status;
99
a240de11
JK
100 if (strcmp(var, "receive.denydeletes") == 0) {
101 deny_deletes = git_config_bool(var, value);
102 return 0;
103 }
104
e28714c5 105 if (strcmp(var, "receive.denynonfastforwards") == 0) {
6fb75bed
SP
106 deny_non_fast_forwards = git_config_bool(var, value);
107 return 0;
108 }
109
e28714c5
JH
110 if (strcmp(var, "receive.unpacklimit") == 0) {
111 receive_unpack_limit = git_config_int(var, value);
fc04c412
SP
112 return 0;
113 }
114
e28714c5
JH
115 if (strcmp(var, "transfer.unpacklimit") == 0) {
116 transfer_unpack_limit = git_config_int(var, value);
117 return 0;
118 }
119
5d477a33
JS
120 if (skip_prefix(var, "receive.fsck.", &var)) {
121 if (is_valid_msg_type(var, value))
122 strbuf_addf(&fsck_msg_types, "%c%s=%s",
123 fsck_msg_types.len ? ',' : '=', var, value);
124 else
125 warning("Skipping unknown msg id '%s'", var);
126 return 0;
127 }
128
20dc0016
MK
129 if (strcmp(var, "receive.fsckobjects") == 0) {
130 receive_fsck_objects = git_config_bool(var, value);
131 return 0;
132 }
133
dab76d3a
JH
134 if (strcmp(var, "transfer.fsckobjects") == 0) {
135 transfer_fsck_objects = git_config_bool(var, value);
136 return 0;
137 }
138
986e8239
JK
139 if (!strcmp(var, "receive.denycurrentbranch")) {
140 deny_current_branch = parse_deny_action(var, value);
141 return 0;
142 }
143
747ca245
JH
144 if (strcmp(var, "receive.denydeletecurrent") == 0) {
145 deny_delete_current = parse_deny_action(var, value);
146 return 0;
147 }
148
b74fce16
NP
149 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
150 prefer_ofs_delta = git_config_bool(var, value);
151 return 0;
152 }
153
77e3efbf
JH
154 if (strcmp(var, "receive.updateserverinfo") == 0) {
155 auto_update_server_info = git_config_bool(var, value);
156 return 0;
157 }
158
159 if (strcmp(var, "receive.autogc") == 0) {
160 auto_gc = git_config_bool(var, value);
161 return 0;
162 }
163
0a1bc12b
NTND
164 if (strcmp(var, "receive.shallowupdate") == 0) {
165 shallow_update = git_config_bool(var, value);
166 return 0;
167 }
168
b89363e4
JH
169 if (strcmp(var, "receive.certnonceseed") == 0)
170 return git_config_string(&cert_nonce_seed, var, value);
a85b377d 171
5732373d
JH
172 if (strcmp(var, "receive.certnonceslop") == 0) {
173 nonce_stamp_slop_limit = git_config_ulong(var, value);
174 return 0;
175 }
176
1b70fe5d
RS
177 if (strcmp(var, "receive.advertiseatomic") == 0) {
178 advertise_atomic_push = git_config_bool(var, value);
179 return 0;
180 }
181
ef90d6d4 182 return git_default_config(var, value, cb);
6fb75bed
SP
183}
184
bc98201d 185static void show_ref(const char *path, const unsigned char *sha1)
575f4974 186{
daebaa78
JH
187 if (ref_is_hidden(path))
188 return;
189
52d2ae58 190 if (sent_capabilities) {
cfee10a7 191 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
52d2ae58
JH
192 } else {
193 struct strbuf cap = STRBUF_INIT;
194
195 strbuf_addstr(&cap,
196 "report-status delete-refs side-band-64k quiet");
1b70fe5d
RS
197 if (advertise_atomic_push)
198 strbuf_addstr(&cap, " atomic");
52d2ae58
JH
199 if (prefer_ofs_delta)
200 strbuf_addstr(&cap, " ofs-delta");
b89363e4
JH
201 if (push_cert_nonce)
202 strbuf_addf(&cap, " push-cert=%s", push_cert_nonce);
52d2ae58
JH
203 strbuf_addf(&cap, " agent=%s", git_user_agent_sanitized());
204 packet_write(1, "%s %s%c%s\n",
205 sha1_to_hex(sha1), path, 0, cap.buf);
206 strbuf_release(&cap);
207 sent_capabilities = 1;
208 }
575f4974
LT
209}
210
ce2a9873 211static int show_ref_cb(const char *path, const struct object_id *oid, int flag, void *unused)
6b01ecfe
JT
212{
213 path = strip_namespace(path);
214 /*
215 * Advertise refs outside our current namespace as ".have"
216 * refs, so that the client can use them to minimize data
217 * transfer but will otherwise ignore them. This happens to
218 * cover ".have" that are thrown in by add_one_alternate_ref()
219 * to mark histories that are complete in our alternates as
220 * well.
221 */
222 if (!path)
223 path = ".have";
ce2a9873 224 show_ref(path, oid->hash);
bc98201d 225 return 0;
6b01ecfe
JT
226}
227
85f25104 228static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
b7a025d9 229{
85f25104 230 show_ref(".have", sha1);
b7a025d9
MH
231}
232
233static void collect_one_alternate_ref(const struct ref *ref, void *data)
234{
235 struct sha1_array *sa = data;
236 sha1_array_append(sa, ref->old_sha1);
6b01ecfe
JT
237}
238
8a65ff76 239static void write_head_info(void)
575f4974 240{
b7a025d9 241 struct sha1_array sa = SHA1_ARRAY_INIT;
2b2a5be3 242
b7a025d9 243 for_each_alternate_ref(collect_one_alternate_ref, &sa);
85f25104 244 sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
b7a025d9 245 sha1_array_clear(&sa);
ce2a9873 246 for_each_ref(show_ref_cb, NULL);
185c04e0 247 if (!sent_capabilities)
bc98201d 248 show_ref("capabilities^{}", null_sha1);
cfee10a7 249
ad491366
NTND
250 advertise_shallow_grafts(1);
251
b7a025d9
MH
252 /* EOF */
253 packet_flush(1);
575f4974
LT
254}
255
eb1af2df
LT
256struct command {
257 struct command *next;
cfee10a7 258 const char *error_string;
160b81ed
PYH
259 unsigned int skip_update:1,
260 did_not_exist:1;
5dbd7676 261 int index;
eb1af2df
LT
262 unsigned char old_sha1[20];
263 unsigned char new_sha1[20];
8f1d2e6f 264 char ref_name[FLEX_ARRAY]; /* more */
575f4974
LT
265};
266
466dbc42
SP
267static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
268static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
269
270static void report_message(const char *prefix, const char *err, va_list params)
271{
272 int sz = strlen(prefix);
273 char msg[4096];
274
275 strncpy(msg, prefix, sz);
276 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
277 if (sz > (sizeof(msg) - 1))
278 sz = sizeof(msg) - 1;
279 msg[sz++] = '\n';
280
281 if (use_sideband)
282 send_sideband(1, 2, msg, sz, use_sideband);
283 else
284 xwrite(2, msg, sz);
285}
286
287static void rp_warning(const char *err, ...)
288{
289 va_list params;
290 va_start(params, err);
291 report_message("warning: ", err, params);
292 va_end(params);
293}
294
295static void rp_error(const char *err, ...)
296{
297 va_list params;
298 va_start(params, err);
299 report_message("error: ", err, params);
300 va_end(params);
301}
302
6d525d38
SP
303static int copy_to_sideband(int in, int out, void *arg)
304{
305 char data[128];
306 while (1) {
307 ssize_t sz = xread(in, data, sizeof(data));
308 if (sz <= 0)
309 break;
310 send_sideband(1, 2, data, sz, use_sideband);
311 }
312 close(in);
313 return 0;
314}
315
b89363e4
JH
316#define HMAC_BLOCK_SIZE 64
317
6f5ef44e 318static void hmac_sha1(unsigned char *out,
b89363e4
JH
319 const char *key_in, size_t key_len,
320 const char *text, size_t text_len)
321{
322 unsigned char key[HMAC_BLOCK_SIZE];
323 unsigned char k_ipad[HMAC_BLOCK_SIZE];
324 unsigned char k_opad[HMAC_BLOCK_SIZE];
325 int i;
326 git_SHA_CTX ctx;
327
328 /* RFC 2104 2. (1) */
329 memset(key, '\0', HMAC_BLOCK_SIZE);
330 if (HMAC_BLOCK_SIZE < key_len) {
331 git_SHA1_Init(&ctx);
332 git_SHA1_Update(&ctx, key_in, key_len);
333 git_SHA1_Final(key, &ctx);
334 } else {
335 memcpy(key, key_in, key_len);
336 }
337
338 /* RFC 2104 2. (2) & (5) */
339 for (i = 0; i < sizeof(key); i++) {
340 k_ipad[i] = key[i] ^ 0x36;
341 k_opad[i] = key[i] ^ 0x5c;
342 }
343
344 /* RFC 2104 2. (3) & (4) */
345 git_SHA1_Init(&ctx);
346 git_SHA1_Update(&ctx, k_ipad, sizeof(k_ipad));
347 git_SHA1_Update(&ctx, text, text_len);
348 git_SHA1_Final(out, &ctx);
349
350 /* RFC 2104 2. (6) & (7) */
351 git_SHA1_Init(&ctx);
352 git_SHA1_Update(&ctx, k_opad, sizeof(k_opad));
6f5ef44e 353 git_SHA1_Update(&ctx, out, 20);
b89363e4
JH
354 git_SHA1_Final(out, &ctx);
355}
356
357static char *prepare_push_cert_nonce(const char *path, unsigned long stamp)
358{
359 struct strbuf buf = STRBUF_INIT;
360 unsigned char sha1[20];
361
362 strbuf_addf(&buf, "%s:%lu", path, stamp);
363 hmac_sha1(sha1, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));;
364 strbuf_release(&buf);
365
366 /* RFC 2104 5. HMAC-SHA1-80 */
367 strbuf_addf(&buf, "%lu-%.*s", stamp, 20, sha1_to_hex(sha1));
368 return strbuf_detach(&buf, NULL);
369}
370
371/*
372 * NEEDSWORK: reuse find_commit_header() from jk/commit-author-parsing
373 * after dropping "_commit" from its name and possibly moving it out
374 * of commit.c
375 */
376static char *find_header(const char *msg, size_t len, const char *key)
377{
378 int key_len = strlen(key);
379 const char *line = msg;
380
381 while (line && line < msg + len) {
382 const char *eol = strchrnul(line, '\n');
383
384 if ((msg + len <= eol) || line == eol)
385 return NULL;
386 if (line + key_len < eol &&
387 !memcmp(line, key, key_len) && line[key_len] == ' ') {
388 int offset = key_len + 1;
389 return xmemdupz(line + offset, (eol - line) - offset);
390 }
391 line = *eol ? eol + 1 : NULL;
392 }
393 return NULL;
394}
395
396static const char *check_nonce(const char *buf, size_t len)
397{
398 char *nonce = find_header(buf, len, "nonce");
5732373d
JH
399 unsigned long stamp, ostamp;
400 char *bohmac, *expect = NULL;
b89363e4
JH
401 const char *retval = NONCE_BAD;
402
403 if (!nonce) {
404 retval = NONCE_MISSING;
405 goto leave;
406 } else if (!push_cert_nonce) {
407 retval = NONCE_UNSOLICITED;
408 goto leave;
409 } else if (!strcmp(push_cert_nonce, nonce)) {
410 retval = NONCE_OK;
411 goto leave;
412 }
413
5732373d
JH
414 if (!stateless_rpc) {
415 /* returned nonce MUST match what we gave out earlier */
416 retval = NONCE_BAD;
417 goto leave;
418 }
419
420 /*
421 * In stateless mode, we may be receiving a nonce issued by
422 * another instance of the server that serving the same
423 * repository, and the timestamps may not match, but the
424 * nonce-seed and dir should match, so we can recompute and
425 * report the time slop.
426 *
427 * In addition, when a nonce issued by another instance has
428 * timestamp within receive.certnonceslop seconds, we pretend
429 * as if we issued that nonce when reporting to the hook.
430 */
431
432 /* nonce is concat(<seconds-since-epoch>, "-", <hmac>) */
433 if (*nonce <= '0' || '9' < *nonce) {
434 retval = NONCE_BAD;
435 goto leave;
436 }
437 stamp = strtoul(nonce, &bohmac, 10);
438 if (bohmac == nonce || bohmac[0] != '-') {
439 retval = NONCE_BAD;
440 goto leave;
441 }
442
443 expect = prepare_push_cert_nonce(service_dir, stamp);
444 if (strcmp(expect, nonce)) {
445 /* Not what we would have signed earlier */
446 retval = NONCE_BAD;
447 goto leave;
448 }
449
450 /*
451 * By how many seconds is this nonce stale? Negative value
452 * would mean it was issued by another server with its clock
453 * skewed in the future.
454 */
455 ostamp = strtoul(push_cert_nonce, NULL, 10);
456 nonce_stamp_slop = (long)ostamp - (long)stamp;
457
458 if (nonce_stamp_slop_limit &&
31a8aa1e 459 labs(nonce_stamp_slop) <= nonce_stamp_slop_limit) {
5732373d
JH
460 /*
461 * Pretend as if the received nonce (which passes the
462 * HMAC check, so it is not a forged by third-party)
463 * is what we issued.
464 */
465 free((void *)push_cert_nonce);
466 push_cert_nonce = xstrdup(nonce);
467 retval = NONCE_OK;
468 } else {
469 retval = NONCE_SLOP;
470 }
b89363e4
JH
471
472leave:
473 free(nonce);
5732373d 474 free(expect);
b89363e4
JH
475 return retval;
476}
477
a85b377d
JH
478static void prepare_push_cert_sha1(struct child_process *proc)
479{
480 static int already_done;
a85b377d
JH
481
482 if (!push_cert.len)
483 return;
484
485 if (!already_done) {
d05b9618
JH
486 struct strbuf gpg_output = STRBUF_INIT;
487 struct strbuf gpg_status = STRBUF_INIT;
488 int bogs /* beginning_of_gpg_sig */;
489
a85b377d
JH
490 already_done = 1;
491 if (write_sha1_file(push_cert.buf, push_cert.len, "blob", push_cert_sha1))
492 hashclr(push_cert_sha1);
d05b9618
JH
493
494 memset(&sigcheck, '\0', sizeof(sigcheck));
495 sigcheck.result = 'N';
496
497 bogs = parse_signature(push_cert.buf, push_cert.len);
498 if (verify_signed_buffer(push_cert.buf, bogs,
499 push_cert.buf + bogs, push_cert.len - bogs,
500 &gpg_output, &gpg_status) < 0) {
501 ; /* error running gpg */
502 } else {
503 sigcheck.payload = push_cert.buf;
504 sigcheck.gpg_output = gpg_output.buf;
505 sigcheck.gpg_status = gpg_status.buf;
506 parse_gpg_output(&sigcheck);
507 }
508
509 strbuf_release(&gpg_output);
510 strbuf_release(&gpg_status);
b89363e4 511 nonce_status = check_nonce(push_cert.buf, bogs);
a85b377d
JH
512 }
513 if (!is_null_sha1(push_cert_sha1)) {
a9154590
RS
514 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT=%s",
515 sha1_to_hex(push_cert_sha1));
516 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_SIGNER=%s",
d05b9618 517 sigcheck.signer ? sigcheck.signer : "");
a9154590 518 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_KEY=%s",
d05b9618 519 sigcheck.key ? sigcheck.key : "");
a9154590
RS
520 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_STATUS=%c",
521 sigcheck.result);
b89363e4 522 if (push_cert_nonce) {
a9154590
RS
523 argv_array_pushf(&proc->env_array,
524 "GIT_PUSH_CERT_NONCE=%s",
525 push_cert_nonce);
526 argv_array_pushf(&proc->env_array,
527 "GIT_PUSH_CERT_NONCE_STATUS=%s",
528 nonce_status);
5732373d 529 if (nonce_status == NONCE_SLOP)
a9154590
RS
530 argv_array_pushf(&proc->env_array,
531 "GIT_PUSH_CERT_NONCE_SLOP=%ld",
5732373d 532 nonce_stamp_slop);
b89363e4 533 }
a85b377d
JH
534 }
535}
536
9684e44a
JH
537typedef int (*feed_fn)(void *, const char **, size_t *);
538static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
b1bf95bb 539{
d3180279 540 struct child_process proc = CHILD_PROCESS_INIT;
6d525d38 541 struct async muxer;
f43cd49f 542 const char *argv[2];
9684e44a 543 int code;
b1bf95bb 544
5a7da2dc
AS
545 argv[0] = find_hook(hook_name);
546 if (!argv[0])
b1bf95bb 547 return 0;
c8dd2771 548
f43cd49f
SP
549 argv[1] = NULL;
550
f43cd49f
SP
551 proc.argv = argv;
552 proc.in = -1;
553 proc.stdout_to_stderr = 1;
554
6d525d38
SP
555 if (use_sideband) {
556 memset(&muxer, 0, sizeof(muxer));
557 muxer.proc = copy_to_sideband;
558 muxer.in = -1;
559 code = start_async(&muxer);
560 if (code)
561 return code;
562 proc.err = muxer.in;
563 }
564
5d222c09
RS
565 prepare_push_cert_sha1(&proc);
566
f43cd49f 567 code = start_command(&proc);
6d525d38
SP
568 if (code) {
569 if (use_sideband)
570 finish_async(&muxer);
90e41a89 571 return code;
6d525d38
SP
572 }
573
ec7dbd14
JH
574 sigchain_push(SIGPIPE, SIG_IGN);
575
9684e44a
JH
576 while (1) {
577 const char *buf;
578 size_t n;
579 if (feed(feed_state, &buf, &n))
580 break;
581 if (write_in_full(proc.in, buf, n) != n)
582 break;
c8dd2771 583 }
e72ae288 584 close(proc.in);
6d525d38
SP
585 if (use_sideband)
586 finish_async(&muxer);
ec7dbd14
JH
587
588 sigchain_pop(SIGPIPE);
589
90e41a89 590 return finish_command(&proc);
b1bf95bb
JW
591}
592
9684e44a
JH
593struct receive_hook_feed_state {
594 struct command *cmd;
cdc2b2f3 595 int skip_broken;
9684e44a
JH
596 struct strbuf buf;
597};
598
599static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
600{
601 struct receive_hook_feed_state *state = state_;
602 struct command *cmd = state->cmd;
603
cdc2b2f3
JH
604 while (cmd &&
605 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
9684e44a
JH
606 cmd = cmd->next;
607 if (!cmd)
608 return -1; /* EOF */
609 strbuf_reset(&state->buf);
610 strbuf_addf(&state->buf, "%s %s %s\n",
611 sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
612 cmd->ref_name);
613 state->cmd = cmd->next;
614 if (bufp) {
615 *bufp = state->buf.buf;
616 *sizep = state->buf.len;
617 }
618 return 0;
619}
620
cdc2b2f3
JH
621static int run_receive_hook(struct command *commands, const char *hook_name,
622 int skip_broken)
9684e44a
JH
623{
624 struct receive_hook_feed_state state;
625 int status;
626
627 strbuf_init(&state.buf, 0);
628 state.cmd = commands;
cdc2b2f3 629 state.skip_broken = skip_broken;
9684e44a
JH
630 if (feed_receive_hook(&state, NULL, NULL))
631 return 0;
632 state.cmd = commands;
633 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
634 strbuf_release(&state.buf);
635 return status;
636}
637
1d9e8b56
SP
638static int run_update_hook(struct command *cmd)
639{
1d9e8b56 640 const char *argv[5];
d3180279 641 struct child_process proc = CHILD_PROCESS_INIT;
6d525d38 642 int code;
1d9e8b56 643
5a7da2dc
AS
644 argv[0] = find_hook("update");
645 if (!argv[0])
1d9e8b56
SP
646 return 0;
647
1d9e8b56
SP
648 argv[1] = cmd->ref_name;
649 argv[2] = sha1_to_hex(cmd->old_sha1);
650 argv[3] = sha1_to_hex(cmd->new_sha1);
651 argv[4] = NULL;
652
6d525d38
SP
653 proc.no_stdin = 1;
654 proc.stdout_to_stderr = 1;
655 proc.err = use_sideband ? -1 : 0;
656 proc.argv = argv;
657
658 code = start_command(&proc);
659 if (code)
660 return code;
661 if (use_sideband)
662 copy_to_sideband(proc.err, -1, NULL);
663 return finish_command(&proc);
1d9e8b56
SP
664}
665
986e8239
JK
666static int is_ref_checked_out(const char *ref)
667{
986e8239
JK
668 if (is_bare_repository())
669 return 0;
670
747ca245 671 if (!head_name)
986e8239 672 return 0;
747ca245 673 return !strcmp(head_name, ref);
986e8239
JK
674}
675
acd2a45b
JH
676static char *refuse_unconfigured_deny_msg[] = {
677 "By default, updating the current branch in a non-bare repository",
678 "is denied, because it will make the index and work tree inconsistent",
679 "with what you pushed, and will require 'git reset --hard' to match",
680 "the work tree to HEAD.",
3d95d92b
JH
681 "",
682 "You can set 'receive.denyCurrentBranch' configuration variable to",
acd2a45b
JH
683 "'ignore' or 'warn' in the remote repository to allow pushing into",
684 "its current branch; however, this is not recommended unless you",
685 "arranged to update its work tree to match what you pushed in some",
686 "other way.",
3d95d92b 687 "",
acd2a45b
JH
688 "To squelch this message and still keep the default behaviour, set",
689 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
3d95d92b
JH
690};
691
acd2a45b 692static void refuse_unconfigured_deny(void)
3d95d92b
JH
693{
694 int i;
acd2a45b 695 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
a886ba28 696 rp_error("%s", refuse_unconfigured_deny_msg[i]);
3d95d92b
JH
697}
698
375881fa
JH
699static char *refuse_unconfigured_deny_delete_current_msg[] = {
700 "By default, deleting the current branch is denied, because the next",
701 "'git clone' won't result in any file checked out, causing confusion.",
747ca245
JH
702 "",
703 "You can set 'receive.denyDeleteCurrent' configuration variable to",
375881fa
JH
704 "'warn' or 'ignore' in the remote repository to allow deleting the",
705 "current branch, with or without a warning message.",
747ca245 706 "",
375881fa 707 "To squelch this message, you can set it to 'refuse'."
747ca245
JH
708};
709
375881fa 710static void refuse_unconfigured_deny_delete_current(void)
747ca245
JH
711{
712 int i;
713 for (i = 0;
375881fa 714 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
747ca245 715 i++)
a886ba28 716 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
747ca245
JH
717}
718
0a1bc12b
NTND
719static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]);
720static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
721{
722 static struct lock_file shallow_lock;
723 struct sha1_array extra = SHA1_ARRAY_INIT;
724 const char *alt_file;
725 uint32_t mask = 1 << (cmd->index % 32);
726 int i;
727
6aa30857 728 trace_printf_key(&trace_shallow,
0a1bc12b
NTND
729 "shallow: update_shallow_ref %s\n", cmd->ref_name);
730 for (i = 0; i < si->shallow->nr; i++)
731 if (si->used_shallow[i] &&
732 (si->used_shallow[i][cmd->index / 32] & mask) &&
733 !delayed_reachability_test(si, i))
734 sha1_array_append(&extra, si->shallow->sha1[i]);
735
736 setup_alternate_shallow(&shallow_lock, &alt_file, &extra);
737 if (check_shallow_connected(command_singleton_iterator,
738 0, cmd, alt_file)) {
739 rollback_lock_file(&shallow_lock);
740 sha1_array_clear(&extra);
741 return -1;
742 }
743
744 commit_lock_file(&shallow_lock);
745
746 /*
747 * Make sure setup_alternate_shallow() for the next ref does
748 * not lose these new roots..
749 */
750 for (i = 0; i < extra.nr; i++)
751 register_shallow(extra.sha1[i]);
752
753 si->shallow_ref[cmd->index] = 0;
754 sha1_array_clear(&extra);
755 return 0;
756}
757
1a51b524
JH
758/*
759 * NEEDSWORK: we should consolidate various implementions of "are we
760 * on an unborn branch?" test into one, and make the unified one more
761 * robust. !get_sha1() based check used here and elsewhere would not
762 * allow us to tell an unborn branch from corrupt ref, for example.
763 * For the purpose of fixing "deploy-to-update does not work when
764 * pushing into an empty repository" issue, this should suffice for
765 * now.
766 */
767static int head_has_history(void)
768{
769 unsigned char sha1[20];
770
771 return !get_sha1("HEAD", sha1);
772}
773
21b138d0
JH
774static const char *push_to_deploy(unsigned char *sha1,
775 struct argv_array *env,
776 const char *work_tree)
1404bcbb
JS
777{
778 const char *update_refresh[] = {
779 "update-index", "-q", "--ignore-submodules", "--refresh", NULL
780 };
781 const char *diff_files[] = {
782 "diff-files", "--quiet", "--ignore-submodules", "--", NULL
783 };
784 const char *diff_index[] = {
785 "diff-index", "--quiet", "--cached", "--ignore-submodules",
1a51b524 786 NULL, "--", NULL
1404bcbb
JS
787 };
788 const char *read_tree[] = {
789 "read-tree", "-u", "-m", NULL, NULL
790 };
1404bcbb
JS
791 struct child_process child = CHILD_PROCESS_INIT;
792
1404bcbb 793 child.argv = update_refresh;
21b138d0 794 child.env = env->argv;
1404bcbb
JS
795 child.dir = work_tree;
796 child.no_stdin = 1;
797 child.stdout_to_stderr = 1;
798 child.git_cmd = 1;
21b138d0 799 if (run_command(&child))
1404bcbb 800 return "Up-to-date check failed";
1404bcbb
JS
801
802 /* run_command() does not clean up completely; reinitialize */
803 child_process_init(&child);
804 child.argv = diff_files;
21b138d0 805 child.env = env->argv;
1404bcbb
JS
806 child.dir = work_tree;
807 child.no_stdin = 1;
808 child.stdout_to_stderr = 1;
809 child.git_cmd = 1;
21b138d0 810 if (run_command(&child))
1404bcbb 811 return "Working directory has unstaged changes";
1404bcbb 812
1a51b524
JH
813 /* diff-index with either HEAD or an empty tree */
814 diff_index[4] = head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX;
815
1404bcbb
JS
816 child_process_init(&child);
817 child.argv = diff_index;
21b138d0 818 child.env = env->argv;
1404bcbb
JS
819 child.no_stdin = 1;
820 child.no_stdout = 1;
821 child.stdout_to_stderr = 0;
822 child.git_cmd = 1;
21b138d0 823 if (run_command(&child))
1404bcbb 824 return "Working directory has staged changes";
1404bcbb
JS
825
826 read_tree[3] = sha1_to_hex(sha1);
827 child_process_init(&child);
828 child.argv = read_tree;
21b138d0 829 child.env = env->argv;
1404bcbb
JS
830 child.dir = work_tree;
831 child.no_stdin = 1;
832 child.no_stdout = 1;
833 child.stdout_to_stderr = 0;
834 child.git_cmd = 1;
21b138d0 835 if (run_command(&child))
1404bcbb 836 return "Could not update working tree to new HEAD";
1404bcbb 837
1404bcbb
JS
838 return NULL;
839}
840
08553319
JH
841static const char *push_to_checkout_hook = "push-to-checkout";
842
843static const char *push_to_checkout(unsigned char *sha1,
844 struct argv_array *env,
845 const char *work_tree)
846{
847 argv_array_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
848 if (run_hook_le(env->argv, push_to_checkout_hook,
849 sha1_to_hex(sha1), NULL))
850 return "push-to-checkout hook declined";
851 else
852 return NULL;
853}
854
21b138d0
JH
855static const char *update_worktree(unsigned char *sha1)
856{
857 const char *retval;
858 const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : "..";
859 struct argv_array env = ARGV_ARRAY_INIT;
860
861 if (is_bare_repository())
862 return "denyCurrentBranch = updateInstead needs a worktree";
863
864 argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir()));
865
08553319
JH
866 if (!find_hook(push_to_checkout_hook))
867 retval = push_to_deploy(sha1, &env, work_tree);
868 else
869 retval = push_to_checkout(sha1, &env, work_tree);
21b138d0
JH
870
871 argv_array_clear(&env);
872 return retval;
873}
874
0a1bc12b 875static const char *update(struct command *cmd, struct shallow_info *si)
2eca23da 876{
cfee10a7 877 const char *name = cmd->ref_name;
6b01ecfe 878 struct strbuf namespaced_name_buf = STRBUF_INIT;
1404bcbb 879 const char *namespaced_name, *ret;
cfee10a7
JH
880 unsigned char *old_sha1 = cmd->old_sha1;
881 unsigned char *new_sha1 = cmd->new_sha1;
2eca23da 882
061d6b9a 883 /* only refs/... are allowed */
59556548 884 if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
466dbc42 885 rp_error("refusing to create funny ref '%s' remotely", name);
8aaf7d64 886 return "funny refname";
cfee10a7 887 }
d8a1deec 888
6b01ecfe
JT
889 strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
890 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
891
892 if (is_ref_checked_out(namespaced_name)) {
3d95d92b
JH
893 switch (deny_current_branch) {
894 case DENY_IGNORE:
986e8239 895 break;
3d95d92b 896 case DENY_WARN:
466dbc42 897 rp_warning("updating the current branch");
986e8239 898 break;
3d95d92b 899 case DENY_REFUSE:
acd2a45b 900 case DENY_UNCONFIGURED:
466dbc42 901 rp_error("refusing to update checked out branch: %s", name);
acd2a45b
JH
902 if (deny_current_branch == DENY_UNCONFIGURED)
903 refuse_unconfigured_deny();
3d95d92b 904 return "branch is currently checked out";
1404bcbb
JS
905 case DENY_UPDATE_INSTEAD:
906 ret = update_worktree(new_sha1);
907 if (ret)
908 return ret;
909 break;
3d95d92b 910 }
986e8239
JK
911 }
912
d4f694ba 913 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
8aaf7d64
SP
914 error("unpack should have generated %s, "
915 "but I can't find it!", sha1_to_hex(new_sha1));
916 return "bad pack";
cfee10a7 917 }
747ca245
JH
918
919 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
59556548 920 if (deny_deletes && starts_with(name, "refs/heads/")) {
466dbc42 921 rp_error("denying ref deletion for %s", name);
747ca245
JH
922 return "deletion prohibited";
923 }
924
6b01ecfe 925 if (!strcmp(namespaced_name, head_name)) {
747ca245
JH
926 switch (deny_delete_current) {
927 case DENY_IGNORE:
928 break;
929 case DENY_WARN:
466dbc42 930 rp_warning("deleting the current branch");
747ca245
JH
931 break;
932 case DENY_REFUSE:
375881fa 933 case DENY_UNCONFIGURED:
1404bcbb 934 case DENY_UPDATE_INSTEAD:
375881fa
JH
935 if (deny_delete_current == DENY_UNCONFIGURED)
936 refuse_unconfigured_deny_delete_current();
466dbc42 937 rp_error("refusing to delete the current branch: %s", name);
747ca245 938 return "deletion of the current branch prohibited";
1404bcbb
JS
939 default:
940 return "Invalid denyDeleteCurrent setting";
747ca245
JH
941 }
942 }
a240de11 943 }
747ca245 944
d4f694ba 945 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
ba988a83 946 !is_null_sha1(old_sha1) &&
59556548 947 starts_with(name, "refs/heads/")) {
eab82707 948 struct object *old_object, *new_object;
11031d7e 949 struct commit *old_commit, *new_commit;
11031d7e 950
eab82707
MK
951 old_object = parse_object(old_sha1);
952 new_object = parse_object(new_sha1);
953
954 if (!old_object || !new_object ||
955 old_object->type != OBJ_COMMIT ||
956 new_object->type != OBJ_COMMIT) {
957 error("bad sha1 objects for %s", name);
958 return "bad ref";
959 }
960 old_commit = (struct commit *)old_object;
961 new_commit = (struct commit *)new_object;
5d55915c 962 if (!in_merge_bases(old_commit, new_commit)) {
466dbc42
SP
963 rp_error("denying non-fast-forward %s"
964 " (you should pull first)", name);
a75d7b54 965 return "non-fast-forward";
8aaf7d64 966 }
11031d7e 967 }
1d9e8b56 968 if (run_update_hook(cmd)) {
466dbc42 969 rp_error("hook declined to update %s", name);
8aaf7d64 970 return "hook declined";
b1bf95bb 971 }
3159c8dc 972
d4f694ba 973 if (is_null_sha1(new_sha1)) {
222368c6 974 struct strbuf err = STRBUF_INIT;
28391a80 975 if (!parse_object(old_sha1)) {
28391a80 976 old_sha1 = NULL;
160b81ed
PYH
977 if (ref_exists(name)) {
978 rp_warning("Allowing deletion of corrupt ref.");
979 } else {
980 rp_warning("Deleting a non-existent ref.");
981 cmd->did_not_exist = 1;
982 }
28391a80 983 }
222368c6
SB
984 if (ref_transaction_delete(transaction,
985 namespaced_name,
986 old_sha1,
fb5a6bb6 987 0, "push", &err)) {
222368c6
SB
988 rp_error("%s", err.buf);
989 strbuf_release(&err);
8aaf7d64 990 return "failed to delete";
d4f694ba 991 }
222368c6 992 strbuf_release(&err);
8aaf7d64 993 return NULL; /* good */
d4f694ba
JH
994 }
995 else {
6629ea2d 996 struct strbuf err = STRBUF_INIT;
0a1bc12b
NTND
997 if (shallow_update && si->shallow_ref[cmd->index] &&
998 update_shallow_ref(cmd, si))
999 return "shallow error";
1000
222368c6
SB
1001 if (ref_transaction_update(transaction,
1002 namespaced_name,
1003 new_sha1, old_sha1,
1d147bdf 1004 0, "push",
222368c6 1005 &err)) {
6629ea2d
RS
1006 rp_error("%s", err.buf);
1007 strbuf_release(&err);
222368c6 1008
6629ea2d 1009 return "failed to update ref";
ef203f08 1010 }
6629ea2d 1011 strbuf_release(&err);
222368c6 1012
8aaf7d64 1013 return NULL; /* good */
19614330 1014 }
2eca23da
LT
1015}
1016
5e1c71fd 1017static void run_update_post_hook(struct command *commands)
19614330 1018{
5e1c71fd 1019 struct command *cmd;
6d525d38 1020 int argc;
9201c707 1021 const char **argv;
d3180279 1022 struct child_process proc = CHILD_PROCESS_INIT;
dcf69262 1023 const char *hook;
19614330 1024
5a7da2dc 1025 hook = find_hook("post-update");
5e1c71fd 1026 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
160b81ed 1027 if (cmd->error_string || cmd->did_not_exist)
19614330
JH
1028 continue;
1029 argc++;
1030 }
5a7da2dc 1031 if (!argc || !hook)
3e6e152c 1032 return;
5a7da2dc 1033
3e6e152c 1034 argv = xmalloc(sizeof(*argv) * (2 + argc));
5a7da2dc 1035 argv[0] = hook;
19614330 1036
5e1c71fd 1037 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
160b81ed 1038 if (cmd->error_string || cmd->did_not_exist)
19614330 1039 continue;
95244ae3 1040 argv[argc] = xstrdup(cmd->ref_name);
19614330
JH
1041 argc++;
1042 }
1043 argv[argc] = NULL;
6d525d38 1044
6d525d38
SP
1045 proc.no_stdin = 1;
1046 proc.stdout_to_stderr = 1;
1047 proc.err = use_sideband ? -1 : 0;
1048 proc.argv = argv;
1049
1050 if (!start_command(&proc)) {
1051 if (use_sideband)
1052 copy_to_sideband(proc.err, -1, NULL);
1053 finish_command(&proc);
1054 }
19614330 1055}
2eca23da 1056
da3efdb1
JS
1057static void check_aliased_update(struct command *cmd, struct string_list *list)
1058{
6b01ecfe
JT
1059 struct strbuf buf = STRBUF_INIT;
1060 const char *dst_name;
da3efdb1
JS
1061 struct string_list_item *item;
1062 struct command *dst_cmd;
1063 unsigned char sha1[20];
1064 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
1065 int flag;
1066
6b01ecfe 1067 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
7695d118 1068 dst_name = resolve_ref_unsafe(buf.buf, 0, sha1, &flag);
6b01ecfe 1069 strbuf_release(&buf);
da3efdb1
JS
1070
1071 if (!(flag & REF_ISSYMREF))
1072 return;
1073
6b01ecfe
JT
1074 dst_name = strip_namespace(dst_name);
1075 if (!dst_name) {
1076 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
1077 cmd->skip_update = 1;
1078 cmd->error_string = "broken symref";
1079 return;
1080 }
1081
e8c8b713 1082 if ((item = string_list_lookup(list, dst_name)) == NULL)
da3efdb1
JS
1083 return;
1084
1085 cmd->skip_update = 1;
1086
1087 dst_cmd = (struct command *) item->util;
1088
1089 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
1090 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
1091 return;
1092
1093 dst_cmd->skip_update = 1;
1094
1095 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
0e71bc30 1096 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
da3efdb1 1097 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
0e71bc30 1098 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
da3efdb1
JS
1099 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
1100 " its target '%s' (%s..%s)",
1101 cmd->ref_name, cmd_oldh, cmd_newh,
1102 dst_cmd->ref_name, dst_oldh, dst_newh);
1103
1104 cmd->error_string = dst_cmd->error_string =
1105 "inconsistent aliased update";
1106}
1107
1108static void check_aliased_updates(struct command *commands)
1109{
1110 struct command *cmd;
183113a5 1111 struct string_list ref_list = STRING_LIST_INIT_NODUP;
da3efdb1
JS
1112
1113 for (cmd = commands; cmd; cmd = cmd->next) {
1114 struct string_list_item *item =
1d2f80fa 1115 string_list_append(&ref_list, cmd->ref_name);
da3efdb1
JS
1116 item->util = (void *)cmd;
1117 }
3383e199 1118 string_list_sort(&ref_list);
da3efdb1 1119
ef7e93d9
CB
1120 for (cmd = commands; cmd; cmd = cmd->next) {
1121 if (!cmd->error_string)
1122 check_aliased_update(cmd, &ref_list);
1123 }
da3efdb1
JS
1124
1125 string_list_clear(&ref_list, 0);
1126}
1127
52fed6e1
JH
1128static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
1129{
1130 struct command **cmd_list = cb_data;
1131 struct command *cmd = *cmd_list;
1132
ee6dfb2d 1133 if (!cmd || is_null_sha1(cmd->new_sha1))
52fed6e1
JH
1134 return -1; /* end of list */
1135 *cmd_list = NULL; /* this returns only one */
1136 hashcpy(sha1, cmd->new_sha1);
1137 return 0;
1138}
1139
0a1bc12b
NTND
1140static void set_connectivity_errors(struct command *commands,
1141 struct shallow_info *si)
52fed6e1
JH
1142{
1143 struct command *cmd;
1144
1145 for (cmd = commands; cmd; cmd = cmd->next) {
1146 struct command *singleton = cmd;
0a1bc12b
NTND
1147 if (shallow_update && si->shallow_ref[cmd->index])
1148 /* to be checked in update_shallow_ref() */
1149 continue;
52fed6e1
JH
1150 if (!check_everything_connected(command_singleton_iterator,
1151 0, &singleton))
1152 continue;
1153 cmd->error_string = "missing necessary objects";
1154 }
1155}
1156
0a1bc12b
NTND
1157struct iterate_data {
1158 struct command *cmds;
1159 struct shallow_info *si;
1160};
1161
52fed6e1
JH
1162static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
1163{
0a1bc12b
NTND
1164 struct iterate_data *data = cb_data;
1165 struct command **cmd_list = &data->cmds;
52fed6e1
JH
1166 struct command *cmd = *cmd_list;
1167
0a1bc12b
NTND
1168 for (; cmd; cmd = cmd->next) {
1169 if (shallow_update && data->si->shallow_ref[cmd->index])
1170 /* to be checked in update_shallow_ref() */
1171 continue;
5dbd7676 1172 if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
ee6dfb2d
JH
1173 hashcpy(sha1, cmd->new_sha1);
1174 *cmd_list = cmd->next;
1175 return 0;
1176 }
ee6dfb2d
JH
1177 }
1178 *cmd_list = NULL;
1179 return -1; /* end of list */
52fed6e1
JH
1180}
1181
daebaa78
JH
1182static void reject_updates_to_hidden(struct command *commands)
1183{
1184 struct command *cmd;
1185
1186 for (cmd = commands; cmd; cmd = cmd->next) {
1187 if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
1188 continue;
1189 if (is_null_sha1(cmd->new_sha1))
1190 cmd->error_string = "deny deleting a hidden ref";
1191 else
1192 cmd->error_string = "deny updating a hidden ref";
1193 }
1194}
1195
a6a84319
SB
1196static int should_process_cmd(struct command *cmd)
1197{
1198 return !cmd->error_string && !cmd->skip_update;
1199}
1200
1201static void warn_if_skipped_connectivity_check(struct command *commands,
1202 struct shallow_info *si)
1203{
1204 struct command *cmd;
1205 int checked_connectivity = 1;
1206
1207 for (cmd = commands; cmd; cmd = cmd->next) {
1208 if (should_process_cmd(cmd) && si->shallow_ref[cmd->index]) {
1209 error("BUG: connectivity check has not been run on ref %s",
1210 cmd->ref_name);
1211 checked_connectivity = 0;
1212 }
1213 }
1214 if (!checked_connectivity)
b6a47885 1215 die("BUG: connectivity check skipped???");
a6a84319
SB
1216}
1217
a1a26145
SB
1218static void execute_commands_non_atomic(struct command *commands,
1219 struct shallow_info *si)
1220{
1221 struct command *cmd;
222368c6
SB
1222 struct strbuf err = STRBUF_INIT;
1223
a1a26145
SB
1224 for (cmd = commands; cmd; cmd = cmd->next) {
1225 if (!should_process_cmd(cmd))
1226 continue;
1227
222368c6
SB
1228 transaction = ref_transaction_begin(&err);
1229 if (!transaction) {
1230 rp_error("%s", err.buf);
1231 strbuf_reset(&err);
1232 cmd->error_string = "transaction failed to start";
1233 continue;
1234 }
1235
a1a26145 1236 cmd->error_string = update(cmd, si);
222368c6
SB
1237
1238 if (!cmd->error_string
1239 && ref_transaction_commit(transaction, &err)) {
1240 rp_error("%s", err.buf);
1241 strbuf_reset(&err);
1242 cmd->error_string = "failed to update ref";
1243 }
1244 ref_transaction_free(transaction);
a1a26145 1245 }
68deed29
SB
1246 strbuf_release(&err);
1247}
222368c6 1248
68deed29
SB
1249static void execute_commands_atomic(struct command *commands,
1250 struct shallow_info *si)
1251{
1252 struct command *cmd;
1253 struct strbuf err = STRBUF_INIT;
1254 const char *reported_error = "atomic push failure";
1255
1256 transaction = ref_transaction_begin(&err);
1257 if (!transaction) {
1258 rp_error("%s", err.buf);
1259 strbuf_reset(&err);
1260 reported_error = "transaction failed to start";
1261 goto failure;
1262 }
1263
1264 for (cmd = commands; cmd; cmd = cmd->next) {
1265 if (!should_process_cmd(cmd))
1266 continue;
1267
1268 cmd->error_string = update(cmd, si);
1269
1270 if (cmd->error_string)
1271 goto failure;
1272 }
1273
1274 if (ref_transaction_commit(transaction, &err)) {
1275 rp_error("%s", err.buf);
1276 reported_error = "atomic transaction failed";
1277 goto failure;
1278 }
1279 goto cleanup;
1280
1281failure:
1282 for (cmd = commands; cmd; cmd = cmd->next)
1283 if (!cmd->error_string)
1284 cmd->error_string = reported_error;
1285
1286cleanup:
1287 ref_transaction_free(transaction);
222368c6 1288 strbuf_release(&err);
a1a26145
SB
1289}
1290
0a1bc12b
NTND
1291static void execute_commands(struct command *commands,
1292 const char *unpacker_error,
1293 struct shallow_info *si)
575f4974 1294{
5e1c71fd 1295 struct command *cmd;
747ca245 1296 unsigned char sha1[20];
0a1bc12b 1297 struct iterate_data data;
8aaf7d64
SP
1298
1299 if (unpacker_error) {
5e1c71fd 1300 for (cmd = commands; cmd; cmd = cmd->next)
74eb32d3 1301 cmd->error_string = "unpacker error";
8aaf7d64
SP
1302 return;
1303 }
1304
0a1bc12b
NTND
1305 data.cmds = commands;
1306 data.si = si;
1307 if (check_everything_connected(iterate_receive_command_list, 0, &data))
1308 set_connectivity_errors(commands, si);
52fed6e1 1309
daebaa78
JH
1310 reject_updates_to_hidden(commands);
1311
5a7da2dc 1312 if (run_receive_hook(commands, "pre-receive", 0)) {
ef7e93d9
CB
1313 for (cmd = commands; cmd; cmd = cmd->next) {
1314 if (!cmd->error_string)
1315 cmd->error_string = "pre-receive hook declined";
1316 }
05ef58ec
SP
1317 return;
1318 }
1319
da3efdb1
JS
1320 check_aliased_updates(commands);
1321
96ec7b1e 1322 free(head_name_to_free);
7695d118 1323 head_name = head_name_to_free = resolve_refdup("HEAD", 0, sha1, NULL);
747ca245 1324
68deed29
SB
1325 if (use_atomic)
1326 execute_commands_atomic(commands, si);
1327 else
1328 execute_commands_non_atomic(commands, si);
0a1bc12b 1329
a6a84319
SB
1330 if (shallow_update)
1331 warn_if_skipped_connectivity_check(commands, si);
575f4974
LT
1332}
1333
39895c74
JH
1334static struct command **queue_command(struct command **tail,
1335 const char *line,
1336 int linelen)
1337{
1338 unsigned char old_sha1[20], new_sha1[20];
1339 struct command *cmd;
1340 const char *refname;
1341 int reflen;
1342
1343 if (linelen < 83 ||
1344 line[40] != ' ' ||
1345 line[81] != ' ' ||
1346 get_sha1_hex(line, old_sha1) ||
1347 get_sha1_hex(line + 41, new_sha1))
1348 die("protocol error: expected old/new/ref, got '%s'", line);
1349
1350 refname = line + 82;
1351 reflen = linelen - 82;
1352 cmd = xcalloc(1, sizeof(struct command) + reflen + 1);
1353 hashcpy(cmd->old_sha1, old_sha1);
1354 hashcpy(cmd->new_sha1, new_sha1);
1355 memcpy(cmd->ref_name, refname, reflen);
1356 cmd->ref_name[reflen] = '\0';
1357 *tail = cmd;
1358 return &cmd->next;
1359}
1360
4adf569d
JH
1361static void queue_commands_from_cert(struct command **tail,
1362 struct strbuf *push_cert)
1363{
1364 const char *boc, *eoc;
1365
1366 if (*tail)
1367 die("protocol error: got both push certificate and unsigned commands");
1368
1369 boc = strstr(push_cert->buf, "\n\n");
1370 if (!boc)
1371 die("malformed push certificate %.*s", 100, push_cert->buf);
1372 else
1373 boc += 2;
1374 eoc = push_cert->buf + parse_signature(push_cert->buf, push_cert->len);
1375
1376 while (boc < eoc) {
1377 const char *eol = memchr(boc, '\n', eoc - boc);
1378 tail = queue_command(tail, boc, eol ? eol - boc : eoc - eol);
1379 boc = eol ? eol + 1 : eoc;
1380 }
1381}
1382
5dbd7676 1383static struct command *read_head_info(struct sha1_array *shallow)
575f4974 1384{
5e1c71fd 1385 struct command *commands = NULL;
eb1af2df 1386 struct command **p = &commands;
575f4974 1387 for (;;) {
74543a04 1388 char *line;
39895c74 1389 int len, linelen;
eb1af2df 1390
74543a04
JK
1391 line = packet_read_line(0, &len);
1392 if (!line)
575f4974 1393 break;
5dbd7676 1394
92251b1b 1395 if (len == 48 && starts_with(line, "shallow ")) {
c09b71cc
JH
1396 unsigned char sha1[20];
1397 if (get_sha1_hex(line + 8, sha1))
1398 die("protocol error: expected shallow sha, got '%s'",
1399 line + 8);
1400 sha1_array_append(shallow, sha1);
5dbd7676
NTND
1401 continue;
1402 }
1403
0e3c339b
JH
1404 linelen = strlen(line);
1405 if (linelen < len) {
1406 const char *feature_list = line + linelen + 1;
f47182c8 1407 if (parse_feature_request(feature_list, "report-status"))
cfee10a7 1408 report_status = 1;
f47182c8 1409 if (parse_feature_request(feature_list, "side-band-64k"))
38a81b4e 1410 use_sideband = LARGE_PACKET_MAX;
c207e34f
CB
1411 if (parse_feature_request(feature_list, "quiet"))
1412 quiet = 1;
1b70fe5d
RS
1413 if (advertise_atomic_push
1414 && parse_feature_request(feature_list, "atomic"))
1415 use_atomic = 1;
cfee10a7 1416 }
0e3c339b 1417
a85b377d
JH
1418 if (!strcmp(line, "push-cert")) {
1419 int true_flush = 0;
1420 char certbuf[1024];
1421
1422 for (;;) {
1423 len = packet_read(0, NULL, NULL,
1424 certbuf, sizeof(certbuf), 0);
1425 if (!len) {
1426 true_flush = 1;
1427 break;
1428 }
1429 if (!strcmp(certbuf, "push-cert-end\n"))
1430 break; /* end of cert */
1431 strbuf_addstr(&push_cert, certbuf);
1432 }
1433
1434 if (true_flush)
1435 break;
1436 continue;
1437 }
1438
39895c74 1439 p = queue_command(p, line, linelen);
575f4974 1440 }
4adf569d
JH
1441
1442 if (push_cert.len)
1443 queue_commands_from_cert(p, &push_cert);
1444
5e1c71fd 1445 return commands;
575f4974
LT
1446}
1447
fc04c412
SP
1448static const char *parse_pack_header(struct pack_header *hdr)
1449{
a69e5429
JH
1450 switch (read_pack_header(0, hdr)) {
1451 case PH_ERROR_EOF:
1452 return "eof before pack header was fully read";
1453
1454 case PH_ERROR_PACK_SIGNATURE:
fc04c412 1455 return "protocol error (pack signature mismatch detected)";
a69e5429
JH
1456
1457 case PH_ERROR_PROTOCOL:
fc04c412 1458 return "protocol error (pack version unsupported)";
a69e5429
JH
1459
1460 default:
1461 return "unknown error in parse_pack_header";
1462
1463 case 0:
1464 return NULL;
1465 }
fc04c412
SP
1466}
1467
576162a4
NP
1468static const char *pack_lockfile;
1469
5dbd7676 1470static const char *unpack(int err_fd, struct shallow_info *si)
575f4974 1471{
fc04c412
SP
1472 struct pack_header hdr;
1473 const char *hdr_err;
31c42bff 1474 int status;
fc04c412 1475 char hdr_arg[38];
d3180279 1476 struct child_process child = CHILD_PROCESS_INIT;
dab76d3a
JH
1477 int fsck_objects = (receive_fsck_objects >= 0
1478 ? receive_fsck_objects
1479 : transfer_fsck_objects >= 0
1480 ? transfer_fsck_objects
1481 : 0);
fc04c412
SP
1482
1483 hdr_err = parse_pack_header(&hdr);
49ecfa13
JK
1484 if (hdr_err) {
1485 if (err_fd > 0)
1486 close(err_fd);
fc04c412 1487 return hdr_err;
49ecfa13 1488 }
6e1c2344
RJ
1489 snprintf(hdr_arg, sizeof(hdr_arg),
1490 "--pack_header=%"PRIu32",%"PRIu32,
fc04c412
SP
1491 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
1492
5dbd7676
NTND
1493 if (si->nr_ours || si->nr_theirs) {
1494 alt_shallow_file = setup_temporary_shallow(si->shallow);
64a7e92f
RS
1495 argv_array_push(&child.args, "--shallow-file");
1496 argv_array_push(&child.args, alt_shallow_file);
5dbd7676
NTND
1497 }
1498
fc04c412 1499 if (ntohl(hdr.hdr_entries) < unpack_limit) {
64a7e92f 1500 argv_array_pushl(&child.args, "unpack-objects", hdr_arg, NULL);
c207e34f 1501 if (quiet)
64a7e92f 1502 argv_array_push(&child.args, "-q");
dab76d3a 1503 if (fsck_objects)
5d477a33
JS
1504 argv_array_pushf(&child.args, "--strict%s",
1505 fsck_msg_types.buf);
59bfdfb8 1506 child.no_stdout = 1;
a22e6f85 1507 child.err = err_fd;
59bfdfb8 1508 child.git_cmd = 1;
31c42bff
NTND
1509 status = run_command(&child);
1510 if (status)
1511 return "unpack-objects abnormal exit";
576162a4 1512 } else {
31c42bff 1513 int s;
576162a4 1514 char keep_arg[256];
576162a4 1515
85e72830 1516 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
576162a4
NP
1517 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
1518 strcpy(keep_arg + s, "localhost");
1519
64a7e92f 1520 argv_array_pushl(&child.args, "index-pack",
31c42bff 1521 "--stdin", hdr_arg, keep_arg, NULL);
dab76d3a 1522 if (fsck_objects)
5d477a33
JS
1523 argv_array_pushf(&child.args, "--strict%s",
1524 fsck_msg_types.buf);
f7c815c3 1525 if (fix_thin)
64a7e92f 1526 argv_array_push(&child.args, "--fix-thin");
31c42bff
NTND
1527 child.out = -1;
1528 child.err = err_fd;
1529 child.git_cmd = 1;
1530 status = start_command(&child);
1531 if (status)
576162a4 1532 return "index-pack fork failed";
31c42bff
NTND
1533 pack_lockfile = index_pack_lockfile(child.out);
1534 close(child.out);
1535 status = finish_command(&child);
1536 if (status)
1537 return "index-pack abnormal exit";
1538 reprepare_packed_git();
cfee10a7 1539 }
31c42bff 1540 return NULL;
cfee10a7
JH
1541}
1542
5dbd7676 1543static const char *unpack_with_sideband(struct shallow_info *si)
a22e6f85
JK
1544{
1545 struct async muxer;
1546 const char *ret;
1547
1548 if (!use_sideband)
5dbd7676 1549 return unpack(0, si);
a22e6f85
JK
1550
1551 memset(&muxer, 0, sizeof(muxer));
1552 muxer.proc = copy_to_sideband;
1553 muxer.in = -1;
1554 if (start_async(&muxer))
1555 return NULL;
1556
5dbd7676 1557 ret = unpack(muxer.in, si);
a22e6f85
JK
1558
1559 finish_async(&muxer);
1560 return ret;
1561}
1562
0a1bc12b
NTND
1563static void prepare_shallow_update(struct command *commands,
1564 struct shallow_info *si)
1565{
1566 int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1567
1568 si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
1569 si->shallow->nr);
1570 assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
1571
1572 si->need_reachability_test =
1573 xcalloc(si->shallow->nr, sizeof(*si->need_reachability_test));
1574 si->reachable =
1575 xcalloc(si->shallow->nr, sizeof(*si->reachable));
1576 si->shallow_ref = xcalloc(si->ref->nr, sizeof(*si->shallow_ref));
1577
1578 for (i = 0; i < si->nr_ours; i++)
1579 si->need_reachability_test[si->ours[i]] = 1;
1580
1581 for (i = 0; i < si->shallow->nr; i++) {
1582 if (!si->used_shallow[i])
1583 continue;
1584 for (j = 0; j < bitmap_size; j++) {
1585 if (!si->used_shallow[i][j])
1586 continue;
1587 si->need_reachability_test[i]++;
1588 for (k = 0; k < 32; k++)
1589 if (si->used_shallow[i][j] & (1 << k))
1590 si->shallow_ref[j * 32 + k]++;
1591 }
1592
1593 /*
1594 * true for those associated with some refs and belong
1595 * in "ours" list aka "step 7 not done yet"
1596 */
1597 si->need_reachability_test[i] =
1598 si->need_reachability_test[i] > 1;
1599 }
1600
1601 /*
1602 * keep hooks happy by forcing a temporary shallow file via
1603 * env variable because we can't add --shallow-file to every
1604 * command. check_everything_connected() will be done with
1605 * true .git/shallow though.
1606 */
1607 setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
1608}
1609
5dbd7676
NTND
1610static void update_shallow_info(struct command *commands,
1611 struct shallow_info *si,
1612 struct sha1_array *ref)
1613{
1614 struct command *cmd;
1615 int *ref_status;
1616 remove_nonexistent_theirs_shallow(si);
0a1bc12b
NTND
1617 if (!si->nr_ours && !si->nr_theirs) {
1618 shallow_update = 0;
5dbd7676 1619 return;
0a1bc12b 1620 }
5dbd7676
NTND
1621
1622 for (cmd = commands; cmd; cmd = cmd->next) {
1623 if (is_null_sha1(cmd->new_sha1))
1624 continue;
1625 sha1_array_append(ref, cmd->new_sha1);
1626 cmd->index = ref->nr - 1;
1627 }
1628 si->ref = ref;
1629
0a1bc12b
NTND
1630 if (shallow_update) {
1631 prepare_shallow_update(commands, si);
1632 return;
1633 }
1634
5dbd7676
NTND
1635 ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
1636 assign_shallow_commits_to_refs(si, NULL, ref_status);
1637 for (cmd = commands; cmd; cmd = cmd->next) {
1638 if (is_null_sha1(cmd->new_sha1))
1639 continue;
1640 if (ref_status[cmd->index]) {
1641 cmd->error_string = "shallow update not allowed";
1642 cmd->skip_update = 1;
1643 }
1644 }
5dbd7676
NTND
1645 free(ref_status);
1646}
1647
5e1c71fd 1648static void report(struct command *commands, const char *unpack_status)
cfee10a7
JH
1649{
1650 struct command *cmd;
38a81b4e
SP
1651 struct strbuf buf = STRBUF_INIT;
1652
1653 packet_buf_write(&buf, "unpack %s\n",
1654 unpack_status ? unpack_status : "ok");
cfee10a7
JH
1655 for (cmd = commands; cmd; cmd = cmd->next) {
1656 if (!cmd->error_string)
38a81b4e
SP
1657 packet_buf_write(&buf, "ok %s\n",
1658 cmd->ref_name);
cfee10a7 1659 else
38a81b4e
SP
1660 packet_buf_write(&buf, "ng %s %s\n",
1661 cmd->ref_name, cmd->error_string);
575f4974 1662 }
38a81b4e
SP
1663 packet_buf_flush(&buf);
1664
1665 if (use_sideband)
1666 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
1667 else
cdf4fb8e 1668 write_or_die(1, buf.buf, buf.len);
38a81b4e 1669 strbuf_release(&buf);
575f4974
LT
1670}
1671
5e1c71fd 1672static int delete_only(struct command *commands)
d4f694ba 1673{
5e1c71fd
JS
1674 struct command *cmd;
1675 for (cmd = commands; cmd; cmd = cmd->next) {
d4f694ba
JH
1676 if (!is_null_sha1(cmd->new_sha1))
1677 return 0;
d4f694ba
JH
1678 }
1679 return 1;
1680}
1681
be5908ae 1682int cmd_receive_pack(int argc, const char **argv, const char *prefix)
575f4974 1683{
42526b47 1684 int advertise_refs = 0;
d0efc8a7 1685 int i;
5e1c71fd 1686 struct command *commands;
5dbd7676
NTND
1687 struct sha1_array shallow = SHA1_ARRAY_INIT;
1688 struct sha1_array ref = SHA1_ARRAY_INIT;
1689 struct shallow_info si;
575f4974 1690
bbc30f99
JK
1691 packet_trace_identity("receive-pack");
1692
575f4974
LT
1693 argv++;
1694 for (i = 1; i < argc; i++) {
be5908ae 1695 const char *arg = *argv++;
575f4974
LT
1696
1697 if (*arg == '-') {
c207e34f
CB
1698 if (!strcmp(arg, "--quiet")) {
1699 quiet = 1;
1700 continue;
1701 }
1702
42526b47
SP
1703 if (!strcmp(arg, "--advertise-refs")) {
1704 advertise_refs = 1;
1705 continue;
1706 }
1707 if (!strcmp(arg, "--stateless-rpc")) {
1708 stateless_rpc = 1;
1709 continue;
1710 }
f7c815c3
NTND
1711 if (!strcmp(arg, "--reject-thin-pack-for-testing")) {
1712 fix_thin = 0;
1713 continue;
1714 }
42526b47 1715
575f4974
LT
1716 usage(receive_pack_usage);
1717 }
5732373d 1718 if (service_dir)
d0efc8a7 1719 usage(receive_pack_usage);
5732373d 1720 service_dir = arg;
575f4974 1721 }
5732373d 1722 if (!service_dir)
575f4974
LT
1723 usage(receive_pack_usage);
1724
e1464ca7 1725 setup_path();
5c09f321 1726
5732373d
JH
1727 if (!enter_repo(service_dir, 0))
1728 die("'%s' does not appear to be a git repository", service_dir);
575f4974 1729
ef90d6d4 1730 git_config(receive_pack_config, NULL);
b89363e4 1731 if (cert_nonce_seed)
5732373d 1732 push_cert_nonce = prepare_push_cert_nonce(service_dir, time(NULL));
6fb75bed 1733
e28714c5
JH
1734 if (0 <= transfer_unpack_limit)
1735 unpack_limit = transfer_unpack_limit;
1736 else if (0 <= receive_unpack_limit)
1737 unpack_limit = receive_unpack_limit;
1738
42526b47 1739 if (advertise_refs || !stateless_rpc) {
42526b47 1740 write_head_info();
42526b47
SP
1741 }
1742 if (advertise_refs)
1743 return 0;
575f4974 1744
5dbd7676 1745 if ((commands = read_head_info(&shallow)) != NULL) {
d4f694ba
JH
1746 const char *unpack_status = NULL;
1747
5dbd7676 1748 prepare_shallow_info(&si, &shallow);
0a1bc12b
NTND
1749 if (!si.nr_ours && !si.nr_theirs)
1750 shallow_update = 0;
5dbd7676
NTND
1751 if (!delete_only(commands)) {
1752 unpack_status = unpack_with_sideband(&si);
1753 update_shallow_info(commands, &si, &ref);
1754 }
0a1bc12b 1755 execute_commands(commands, unpack_status, &si);
576162a4 1756 if (pack_lockfile)
691f1a28 1757 unlink_or_warn(pack_lockfile);
cfee10a7 1758 if (report_status)
5e1c71fd 1759 report(commands, unpack_status);
5a7da2dc 1760 run_receive_hook(commands, "post-receive", 1);
8e663d9e 1761 run_update_post_hook(commands);
77e3efbf
JH
1762 if (auto_gc) {
1763 const char *argv_gc_auto[] = {
1764 "gc", "--auto", "--quiet", NULL,
1765 };
4b7f2fa4
JH
1766 int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR;
1767 run_command_v_opt(argv_gc_auto, opt);
77e3efbf
JH
1768 }
1769 if (auto_update_server_info)
1770 update_server_info(0);
5dbd7676 1771 clear_shallow_info(&si);
7f8e9828 1772 }
38a81b4e
SP
1773 if (use_sideband)
1774 packet_flush(1);
5dbd7676
NTND
1775 sha1_array_clear(&shallow);
1776 sha1_array_clear(&ref);
b89363e4 1777 free((void *)push_cert_nonce);
575f4974
LT
1778 return 0;
1779}