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