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