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