7 #include "run-command.h"
13 #include "transport.h"
14 #include "string-list.h"
15 #include "sha1-array.h"
16 #include "connected.h"
17 #include "argv-array.h"
20 #include "gpg-interface.h"
24 static const char * const receive_pack_usage[] = {
25 N_("git receive-pack <git-dir>"),
37 static int deny_deletes;
38 static int deny_non_fast_forwards;
39 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
40 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
41 static int receive_fsck_objects = -1;
42 static int transfer_fsck_objects = -1;
43 static struct strbuf fsck_msg_types = STRBUF_INIT;
44 static int receive_unpack_limit = -1;
45 static int transfer_unpack_limit = -1;
46 static int advertise_atomic_push = 1;
47 static int advertise_push_options;
48 static int unpack_limit = 100;
49 static off_t max_input_size;
50 static int report_status;
51 static int use_sideband;
52 static int use_atomic;
53 static int use_push_options;
55 static int prefer_ofs_delta = 1;
56 static int auto_update_server_info;
57 static int auto_gc = 1;
58 static int reject_thin;
59 static int stateless_rpc;
60 static const char *service_dir;
61 static const char *head_name;
62 static void *head_name_to_free;
63 static int sent_capabilities;
64 static int shallow_update;
65 static const char *alt_shallow_file;
66 static struct strbuf push_cert = STRBUF_INIT;
67 static unsigned char push_cert_sha1[20];
68 static struct signature_check sigcheck;
69 static const char *push_cert_nonce;
70 static const char *cert_nonce_seed;
72 static const char *NONCE_UNSOLICITED = "UNSOLICITED";
73 static const char *NONCE_BAD = "BAD";
74 static const char *NONCE_MISSING = "MISSING";
75 static const char *NONCE_OK = "OK";
76 static const char *NONCE_SLOP = "SLOP";
77 static const char *nonce_status;
78 static long nonce_stamp_slop;
79 static unsigned long nonce_stamp_slop_limit;
80 static struct ref_transaction *transaction;
87 static int keepalive_in_sec = 5;
89 static enum deny_action parse_deny_action(const char *var, const char *value)
92 if (!strcasecmp(value, "ignore"))
94 if (!strcasecmp(value, "warn"))
96 if (!strcasecmp(value, "refuse"))
98 if (!strcasecmp(value, "updateinstead"))
99 return DENY_UPDATE_INSTEAD;
101 if (git_config_bool(var, value))
106 static int receive_pack_config(const char *var, const char *value, void *cb)
108 int status = parse_hide_refs_config(var, value, "receive");
113 if (strcmp(var, "receive.denydeletes") == 0) {
114 deny_deletes = git_config_bool(var, value);
118 if (strcmp(var, "receive.denynonfastforwards") == 0) {
119 deny_non_fast_forwards = git_config_bool(var, value);
123 if (strcmp(var, "receive.unpacklimit") == 0) {
124 receive_unpack_limit = git_config_int(var, value);
128 if (strcmp(var, "transfer.unpacklimit") == 0) {
129 transfer_unpack_limit = git_config_int(var, value);
133 if (strcmp(var, "receive.fsck.skiplist") == 0) {
136 if (git_config_pathname(&path, var, value))
138 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
139 fsck_msg_types.len ? ',' : '=', path);
144 if (skip_prefix(var, "receive.fsck.", &var)) {
145 if (is_valid_msg_type(var, value))
146 strbuf_addf(&fsck_msg_types, "%c%s=%s",
147 fsck_msg_types.len ? ',' : '=', var, value);
149 warning("Skipping unknown msg id '%s'", var);
153 if (strcmp(var, "receive.fsckobjects") == 0) {
154 receive_fsck_objects = git_config_bool(var, value);
158 if (strcmp(var, "transfer.fsckobjects") == 0) {
159 transfer_fsck_objects = git_config_bool(var, value);
163 if (!strcmp(var, "receive.denycurrentbranch")) {
164 deny_current_branch = parse_deny_action(var, value);
168 if (strcmp(var, "receive.denydeletecurrent") == 0) {
169 deny_delete_current = parse_deny_action(var, value);
173 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
174 prefer_ofs_delta = git_config_bool(var, value);
178 if (strcmp(var, "receive.updateserverinfo") == 0) {
179 auto_update_server_info = git_config_bool(var, value);
183 if (strcmp(var, "receive.autogc") == 0) {
184 auto_gc = git_config_bool(var, value);
188 if (strcmp(var, "receive.shallowupdate") == 0) {
189 shallow_update = git_config_bool(var, value);
193 if (strcmp(var, "receive.certnonceseed") == 0)
194 return git_config_string(&cert_nonce_seed, var, value);
196 if (strcmp(var, "receive.certnonceslop") == 0) {
197 nonce_stamp_slop_limit = git_config_ulong(var, value);
201 if (strcmp(var, "receive.advertiseatomic") == 0) {
202 advertise_atomic_push = git_config_bool(var, value);
206 if (strcmp(var, "receive.advertisepushoptions") == 0) {
207 advertise_push_options = git_config_bool(var, value);
211 if (strcmp(var, "receive.keepalive") == 0) {
212 keepalive_in_sec = git_config_int(var, value);
216 if (strcmp(var, "receive.maxinputsize") == 0) {
217 max_input_size = git_config_int64(var, value);
221 return git_default_config(var, value, cb);
224 static void show_ref(const char *path, const unsigned char *sha1)
226 if (sent_capabilities) {
227 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
229 struct strbuf cap = STRBUF_INIT;
232 "report-status delete-refs side-band-64k quiet");
233 if (advertise_atomic_push)
234 strbuf_addstr(&cap, " atomic");
235 if (prefer_ofs_delta)
236 strbuf_addstr(&cap, " ofs-delta");
238 strbuf_addf(&cap, " push-cert=%s", push_cert_nonce);
239 if (advertise_push_options)
240 strbuf_addstr(&cap, " push-options");
241 strbuf_addf(&cap, " agent=%s", git_user_agent_sanitized());
242 packet_write(1, "%s %s%c%s\n",
243 sha1_to_hex(sha1), path, 0, cap.buf);
244 strbuf_release(&cap);
245 sent_capabilities = 1;
249 static int show_ref_cb(const char *path_full, const struct object_id *oid,
250 int flag, void *unused)
252 const char *path = strip_namespace(path_full);
254 if (ref_is_hidden(path, path_full))
258 * Advertise refs outside our current namespace as ".have"
259 * refs, so that the client can use them to minimize data
260 * transfer but will otherwise ignore them. This happens to
261 * cover ".have" that are thrown in by add_one_alternate_ref()
262 * to mark histories that are complete in our alternates as
267 show_ref(path, oid->hash);
271 static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
273 show_ref(".have", sha1);
276 static void collect_one_alternate_ref(const struct ref *ref, void *data)
278 struct sha1_array *sa = data;
279 sha1_array_append(sa, ref->old_oid.hash);
282 static void write_head_info(void)
284 struct sha1_array sa = SHA1_ARRAY_INIT;
286 for_each_alternate_ref(collect_one_alternate_ref, &sa);
287 sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
288 sha1_array_clear(&sa);
289 for_each_ref(show_ref_cb, NULL);
290 if (!sent_capabilities)
291 show_ref("capabilities^{}", null_sha1);
293 advertise_shallow_grafts(1);
300 struct command *next;
301 const char *error_string;
302 unsigned int skip_update:1,
305 unsigned char old_sha1[20];
306 unsigned char new_sha1[20];
307 char ref_name[FLEX_ARRAY]; /* more */
310 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
311 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
313 static void report_message(const char *prefix, const char *err, va_list params)
318 sz = xsnprintf(msg, sizeof(msg), "%s", prefix);
319 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
320 if (sz > (sizeof(msg) - 1))
321 sz = sizeof(msg) - 1;
325 send_sideband(1, 2, msg, sz, use_sideband);
330 static void rp_warning(const char *err, ...)
333 va_start(params, err);
334 report_message("warning: ", err, params);
338 static void rp_error(const char *err, ...)
341 va_start(params, err);
342 report_message("error: ", err, params);
346 static int copy_to_sideband(int in, int out, void *arg)
349 int keepalive_active = 0;
351 if (keepalive_in_sec <= 0)
352 use_keepalive = KEEPALIVE_NEVER;
353 if (use_keepalive == KEEPALIVE_ALWAYS)
354 keepalive_active = 1;
359 if (keepalive_active) {
365 ret = poll(&pfd, 1, 1000 * keepalive_in_sec);
372 } else if (ret == 0) {
373 /* no data; send a keepalive packet */
374 static const char buf[] = "0005\1";
375 write_or_die(1, buf, sizeof(buf) - 1);
377 } /* else there is actual data to read */
380 sz = xread(in, data, sizeof(data));
384 if (use_keepalive == KEEPALIVE_AFTER_NUL && !keepalive_active) {
385 const char *p = memchr(data, '\0', sz);
388 * The NUL tells us to start sending keepalives. Make
389 * sure we send any other data we read along
392 keepalive_active = 1;
393 send_sideband(1, 2, data, p - data, use_sideband);
394 send_sideband(1, 2, p + 1, sz - (p - data + 1), use_sideband);
400 * Either we're not looking for a NUL signal, or we didn't see
401 * it yet; just pass along the data.
403 send_sideband(1, 2, data, sz, use_sideband);
409 #define HMAC_BLOCK_SIZE 64
411 static void hmac_sha1(unsigned char *out,
412 const char *key_in, size_t key_len,
413 const char *text, size_t text_len)
415 unsigned char key[HMAC_BLOCK_SIZE];
416 unsigned char k_ipad[HMAC_BLOCK_SIZE];
417 unsigned char k_opad[HMAC_BLOCK_SIZE];
421 /* RFC 2104 2. (1) */
422 memset(key, '\0', HMAC_BLOCK_SIZE);
423 if (HMAC_BLOCK_SIZE < key_len) {
425 git_SHA1_Update(&ctx, key_in, key_len);
426 git_SHA1_Final(key, &ctx);
428 memcpy(key, key_in, key_len);
431 /* RFC 2104 2. (2) & (5) */
432 for (i = 0; i < sizeof(key); i++) {
433 k_ipad[i] = key[i] ^ 0x36;
434 k_opad[i] = key[i] ^ 0x5c;
437 /* RFC 2104 2. (3) & (4) */
439 git_SHA1_Update(&ctx, k_ipad, sizeof(k_ipad));
440 git_SHA1_Update(&ctx, text, text_len);
441 git_SHA1_Final(out, &ctx);
443 /* RFC 2104 2. (6) & (7) */
445 git_SHA1_Update(&ctx, k_opad, sizeof(k_opad));
446 git_SHA1_Update(&ctx, out, 20);
447 git_SHA1_Final(out, &ctx);
450 static char *prepare_push_cert_nonce(const char *path, unsigned long stamp)
452 struct strbuf buf = STRBUF_INIT;
453 unsigned char sha1[20];
455 strbuf_addf(&buf, "%s:%lu", path, stamp);
456 hmac_sha1(sha1, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed));;
457 strbuf_release(&buf);
459 /* RFC 2104 5. HMAC-SHA1-80 */
460 strbuf_addf(&buf, "%lu-%.*s", stamp, 20, sha1_to_hex(sha1));
461 return strbuf_detach(&buf, NULL);
465 * NEEDSWORK: reuse find_commit_header() from jk/commit-author-parsing
466 * after dropping "_commit" from its name and possibly moving it out
469 static char *find_header(const char *msg, size_t len, const char *key)
471 int key_len = strlen(key);
472 const char *line = msg;
474 while (line && line < msg + len) {
475 const char *eol = strchrnul(line, '\n');
477 if ((msg + len <= eol) || line == eol)
479 if (line + key_len < eol &&
480 !memcmp(line, key, key_len) && line[key_len] == ' ') {
481 int offset = key_len + 1;
482 return xmemdupz(line + offset, (eol - line) - offset);
484 line = *eol ? eol + 1 : NULL;
489 static const char *check_nonce(const char *buf, size_t len)
491 char *nonce = find_header(buf, len, "nonce");
492 unsigned long stamp, ostamp;
493 char *bohmac, *expect = NULL;
494 const char *retval = NONCE_BAD;
497 retval = NONCE_MISSING;
499 } else if (!push_cert_nonce) {
500 retval = NONCE_UNSOLICITED;
502 } else if (!strcmp(push_cert_nonce, nonce)) {
507 if (!stateless_rpc) {
508 /* returned nonce MUST match what we gave out earlier */
514 * In stateless mode, we may be receiving a nonce issued by
515 * another instance of the server that serving the same
516 * repository, and the timestamps may not match, but the
517 * nonce-seed and dir should match, so we can recompute and
518 * report the time slop.
520 * In addition, when a nonce issued by another instance has
521 * timestamp within receive.certnonceslop seconds, we pretend
522 * as if we issued that nonce when reporting to the hook.
525 /* nonce is concat(<seconds-since-epoch>, "-", <hmac>) */
526 if (*nonce <= '0' || '9' < *nonce) {
530 stamp = strtoul(nonce, &bohmac, 10);
531 if (bohmac == nonce || bohmac[0] != '-') {
536 expect = prepare_push_cert_nonce(service_dir, stamp);
537 if (strcmp(expect, nonce)) {
538 /* Not what we would have signed earlier */
544 * By how many seconds is this nonce stale? Negative value
545 * would mean it was issued by another server with its clock
546 * skewed in the future.
548 ostamp = strtoul(push_cert_nonce, NULL, 10);
549 nonce_stamp_slop = (long)ostamp - (long)stamp;
551 if (nonce_stamp_slop_limit &&
552 labs(nonce_stamp_slop) <= nonce_stamp_slop_limit) {
554 * Pretend as if the received nonce (which passes the
555 * HMAC check, so it is not a forged by third-party)
558 free((void *)push_cert_nonce);
559 push_cert_nonce = xstrdup(nonce);
571 static void prepare_push_cert_sha1(struct child_process *proc)
573 static int already_done;
579 struct strbuf gpg_output = STRBUF_INIT;
580 struct strbuf gpg_status = STRBUF_INIT;
581 int bogs /* beginning_of_gpg_sig */;
584 if (write_sha1_file(push_cert.buf, push_cert.len, "blob", push_cert_sha1))
585 hashclr(push_cert_sha1);
587 memset(&sigcheck, '\0', sizeof(sigcheck));
588 sigcheck.result = 'N';
590 bogs = parse_signature(push_cert.buf, push_cert.len);
591 if (verify_signed_buffer(push_cert.buf, bogs,
592 push_cert.buf + bogs, push_cert.len - bogs,
593 &gpg_output, &gpg_status) < 0) {
594 ; /* error running gpg */
596 sigcheck.payload = push_cert.buf;
597 sigcheck.gpg_output = gpg_output.buf;
598 sigcheck.gpg_status = gpg_status.buf;
599 parse_gpg_output(&sigcheck);
602 strbuf_release(&gpg_output);
603 strbuf_release(&gpg_status);
604 nonce_status = check_nonce(push_cert.buf, bogs);
606 if (!is_null_sha1(push_cert_sha1)) {
607 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT=%s",
608 sha1_to_hex(push_cert_sha1));
609 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_SIGNER=%s",
610 sigcheck.signer ? sigcheck.signer : "");
611 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_KEY=%s",
612 sigcheck.key ? sigcheck.key : "");
613 argv_array_pushf(&proc->env_array, "GIT_PUSH_CERT_STATUS=%c",
615 if (push_cert_nonce) {
616 argv_array_pushf(&proc->env_array,
617 "GIT_PUSH_CERT_NONCE=%s",
619 argv_array_pushf(&proc->env_array,
620 "GIT_PUSH_CERT_NONCE_STATUS=%s",
622 if (nonce_status == NONCE_SLOP)
623 argv_array_pushf(&proc->env_array,
624 "GIT_PUSH_CERT_NONCE_SLOP=%ld",
630 struct receive_hook_feed_state {
634 const struct string_list *push_options;
637 typedef int (*feed_fn)(void *, const char **, size_t *);
638 static int run_and_feed_hook(const char *hook_name, feed_fn feed,
639 struct receive_hook_feed_state *feed_state)
641 struct child_process proc = CHILD_PROCESS_INIT;
646 argv[0] = find_hook(hook_name);
654 proc.stdout_to_stderr = 1;
655 if (feed_state->push_options) {
657 for (i = 0; i < feed_state->push_options->nr; i++)
658 argv_array_pushf(&proc.env_array,
659 "GIT_PUSH_OPTION_%d=%s", i,
660 feed_state->push_options->items[i].string);
661 argv_array_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT=%d",
662 feed_state->push_options->nr);
664 argv_array_pushf(&proc.env_array, "GIT_PUSH_OPTION_COUNT");
667 memset(&muxer, 0, sizeof(muxer));
668 muxer.proc = copy_to_sideband;
670 code = start_async(&muxer);
676 prepare_push_cert_sha1(&proc);
678 code = start_command(&proc);
681 finish_async(&muxer);
685 sigchain_push(SIGPIPE, SIG_IGN);
690 if (feed(feed_state, &buf, &n))
692 if (write_in_full(proc.in, buf, n) != n)
697 finish_async(&muxer);
699 sigchain_pop(SIGPIPE);
701 return finish_command(&proc);
704 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
706 struct receive_hook_feed_state *state = state_;
707 struct command *cmd = state->cmd;
710 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
714 strbuf_reset(&state->buf);
715 strbuf_addf(&state->buf, "%s %s %s\n",
716 sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
718 state->cmd = cmd->next;
720 *bufp = state->buf.buf;
721 *sizep = state->buf.len;
726 static int run_receive_hook(struct command *commands,
727 const char *hook_name,
729 const struct string_list *push_options)
731 struct receive_hook_feed_state state;
734 strbuf_init(&state.buf, 0);
735 state.cmd = commands;
736 state.skip_broken = skip_broken;
737 if (feed_receive_hook(&state, NULL, NULL))
739 state.cmd = commands;
740 state.push_options = push_options;
741 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
742 strbuf_release(&state.buf);
746 static int run_update_hook(struct command *cmd)
749 struct child_process proc = CHILD_PROCESS_INIT;
752 argv[0] = find_hook("update");
756 argv[1] = cmd->ref_name;
757 argv[2] = sha1_to_hex(cmd->old_sha1);
758 argv[3] = sha1_to_hex(cmd->new_sha1);
762 proc.stdout_to_stderr = 1;
763 proc.err = use_sideband ? -1 : 0;
766 code = start_command(&proc);
770 copy_to_sideband(proc.err, -1, NULL);
771 return finish_command(&proc);
774 static int is_ref_checked_out(const char *ref)
776 if (is_bare_repository())
781 return !strcmp(head_name, ref);
784 static char *refuse_unconfigured_deny_msg =
785 N_("By default, updating the current branch in a non-bare repository\n"
786 "is denied, because it will make the index and work tree inconsistent\n"
787 "with what you pushed, and will require 'git reset --hard' to match\n"
788 "the work tree to HEAD.\n"
790 "You can set 'receive.denyCurrentBranch' configuration variable to\n"
791 "'ignore' or 'warn' in the remote repository to allow pushing into\n"
792 "its current branch; however, this is not recommended unless you\n"
793 "arranged to update its work tree to match what you pushed in some\n"
796 "To squelch this message and still keep the default behaviour, set\n"
797 "'receive.denyCurrentBranch' configuration variable to 'refuse'.");
799 static void refuse_unconfigured_deny(void)
801 rp_error("%s", _(refuse_unconfigured_deny_msg));
804 static char *refuse_unconfigured_deny_delete_current_msg =
805 N_("By default, deleting the current branch is denied, because the next\n"
806 "'git clone' won't result in any file checked out, causing confusion.\n"
808 "You can set 'receive.denyDeleteCurrent' configuration variable to\n"
809 "'warn' or 'ignore' in the remote repository to allow deleting the\n"
810 "current branch, with or without a warning message.\n"
812 "To squelch this message, you can set it to 'refuse'.");
814 static void refuse_unconfigured_deny_delete_current(void)
816 rp_error("%s", _(refuse_unconfigured_deny_delete_current_msg));
819 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]);
820 static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
822 static struct lock_file shallow_lock;
823 struct sha1_array extra = SHA1_ARRAY_INIT;
824 struct check_connected_options opt = CHECK_CONNECTED_INIT;
825 uint32_t mask = 1 << (cmd->index % 32);
828 trace_printf_key(&trace_shallow,
829 "shallow: update_shallow_ref %s\n", cmd->ref_name);
830 for (i = 0; i < si->shallow->nr; i++)
831 if (si->used_shallow[i] &&
832 (si->used_shallow[i][cmd->index / 32] & mask) &&
833 !delayed_reachability_test(si, i))
834 sha1_array_append(&extra, si->shallow->sha1[i]);
836 setup_alternate_shallow(&shallow_lock, &opt.shallow_file, &extra);
837 if (check_connected(command_singleton_iterator, cmd, &opt)) {
838 rollback_lock_file(&shallow_lock);
839 sha1_array_clear(&extra);
843 commit_lock_file(&shallow_lock);
846 * Make sure setup_alternate_shallow() for the next ref does
847 * not lose these new roots..
849 for (i = 0; i < extra.nr; i++)
850 register_shallow(extra.sha1[i]);
852 si->shallow_ref[cmd->index] = 0;
853 sha1_array_clear(&extra);
858 * NEEDSWORK: we should consolidate various implementions of "are we
859 * on an unborn branch?" test into one, and make the unified one more
860 * robust. !get_sha1() based check used here and elsewhere would not
861 * allow us to tell an unborn branch from corrupt ref, for example.
862 * For the purpose of fixing "deploy-to-update does not work when
863 * pushing into an empty repository" issue, this should suffice for
866 static int head_has_history(void)
868 unsigned char sha1[20];
870 return !get_sha1("HEAD", sha1);
873 static const char *push_to_deploy(unsigned char *sha1,
874 struct argv_array *env,
875 const char *work_tree)
877 const char *update_refresh[] = {
878 "update-index", "-q", "--ignore-submodules", "--refresh", NULL
880 const char *diff_files[] = {
881 "diff-files", "--quiet", "--ignore-submodules", "--", NULL
883 const char *diff_index[] = {
884 "diff-index", "--quiet", "--cached", "--ignore-submodules",
887 const char *read_tree[] = {
888 "read-tree", "-u", "-m", NULL, NULL
890 struct child_process child = CHILD_PROCESS_INIT;
892 child.argv = update_refresh;
893 child.env = env->argv;
894 child.dir = work_tree;
896 child.stdout_to_stderr = 1;
898 if (run_command(&child))
899 return "Up-to-date check failed";
901 /* run_command() does not clean up completely; reinitialize */
902 child_process_init(&child);
903 child.argv = diff_files;
904 child.env = env->argv;
905 child.dir = work_tree;
907 child.stdout_to_stderr = 1;
909 if (run_command(&child))
910 return "Working directory has unstaged changes";
912 /* diff-index with either HEAD or an empty tree */
913 diff_index[4] = head_has_history() ? "HEAD" : EMPTY_TREE_SHA1_HEX;
915 child_process_init(&child);
916 child.argv = diff_index;
917 child.env = env->argv;
920 child.stdout_to_stderr = 0;
922 if (run_command(&child))
923 return "Working directory has staged changes";
925 read_tree[3] = sha1_to_hex(sha1);
926 child_process_init(&child);
927 child.argv = read_tree;
928 child.env = env->argv;
929 child.dir = work_tree;
932 child.stdout_to_stderr = 0;
934 if (run_command(&child))
935 return "Could not update working tree to new HEAD";
940 static const char *push_to_checkout_hook = "push-to-checkout";
942 static const char *push_to_checkout(unsigned char *sha1,
943 struct argv_array *env,
944 const char *work_tree)
946 argv_array_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree));
947 if (run_hook_le(env->argv, push_to_checkout_hook,
948 sha1_to_hex(sha1), NULL))
949 return "push-to-checkout hook declined";
954 static const char *update_worktree(unsigned char *sha1)
957 const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : "..";
958 struct argv_array env = ARGV_ARRAY_INIT;
960 if (is_bare_repository())
961 return "denyCurrentBranch = updateInstead needs a worktree";
963 argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir()));
965 if (!find_hook(push_to_checkout_hook))
966 retval = push_to_deploy(sha1, &env, work_tree);
968 retval = push_to_checkout(sha1, &env, work_tree);
970 argv_array_clear(&env);
974 static const char *update(struct command *cmd, struct shallow_info *si)
976 const char *name = cmd->ref_name;
977 struct strbuf namespaced_name_buf = STRBUF_INIT;
978 const char *namespaced_name, *ret;
979 unsigned char *old_sha1 = cmd->old_sha1;
980 unsigned char *new_sha1 = cmd->new_sha1;
982 /* only refs/... are allowed */
983 if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
984 rp_error("refusing to create funny ref '%s' remotely", name);
985 return "funny refname";
988 strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
989 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
991 if (is_ref_checked_out(namespaced_name)) {
992 switch (deny_current_branch) {
996 rp_warning("updating the current branch");
999 case DENY_UNCONFIGURED:
1000 rp_error("refusing to update checked out branch: %s", name);
1001 if (deny_current_branch == DENY_UNCONFIGURED)
1002 refuse_unconfigured_deny();
1003 return "branch is currently checked out";
1004 case DENY_UPDATE_INSTEAD:
1005 ret = update_worktree(new_sha1);
1012 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
1013 error("unpack should have generated %s, "
1014 "but I can't find it!", sha1_to_hex(new_sha1));
1018 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
1019 if (deny_deletes && starts_with(name, "refs/heads/")) {
1020 rp_error("denying ref deletion for %s", name);
1021 return "deletion prohibited";
1024 if (head_name && !strcmp(namespaced_name, head_name)) {
1025 switch (deny_delete_current) {
1029 rp_warning("deleting the current branch");
1032 case DENY_UNCONFIGURED:
1033 case DENY_UPDATE_INSTEAD:
1034 if (deny_delete_current == DENY_UNCONFIGURED)
1035 refuse_unconfigured_deny_delete_current();
1036 rp_error("refusing to delete the current branch: %s", name);
1037 return "deletion of the current branch prohibited";
1039 return "Invalid denyDeleteCurrent setting";
1044 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
1045 !is_null_sha1(old_sha1) &&
1046 starts_with(name, "refs/heads/")) {
1047 struct object *old_object, *new_object;
1048 struct commit *old_commit, *new_commit;
1050 old_object = parse_object(old_sha1);
1051 new_object = parse_object(new_sha1);
1053 if (!old_object || !new_object ||
1054 old_object->type != OBJ_COMMIT ||
1055 new_object->type != OBJ_COMMIT) {
1056 error("bad sha1 objects for %s", name);
1059 old_commit = (struct commit *)old_object;
1060 new_commit = (struct commit *)new_object;
1061 if (!in_merge_bases(old_commit, new_commit)) {
1062 rp_error("denying non-fast-forward %s"
1063 " (you should pull first)", name);
1064 return "non-fast-forward";
1067 if (run_update_hook(cmd)) {
1068 rp_error("hook declined to update %s", name);
1069 return "hook declined";
1072 if (is_null_sha1(new_sha1)) {
1073 struct strbuf err = STRBUF_INIT;
1074 if (!parse_object(old_sha1)) {
1076 if (ref_exists(name)) {
1077 rp_warning("Allowing deletion of corrupt ref.");
1079 rp_warning("Deleting a non-existent ref.");
1080 cmd->did_not_exist = 1;
1083 if (ref_transaction_delete(transaction,
1087 rp_error("%s", err.buf);
1088 strbuf_release(&err);
1089 return "failed to delete";
1091 strbuf_release(&err);
1092 return NULL; /* good */
1095 struct strbuf err = STRBUF_INIT;
1096 if (shallow_update && si->shallow_ref[cmd->index] &&
1097 update_shallow_ref(cmd, si))
1098 return "shallow error";
1100 if (ref_transaction_update(transaction,
1105 rp_error("%s", err.buf);
1106 strbuf_release(&err);
1108 return "failed to update ref";
1110 strbuf_release(&err);
1112 return NULL; /* good */
1116 static void run_update_post_hook(struct command *commands)
1118 struct command *cmd;
1120 struct child_process proc = CHILD_PROCESS_INIT;
1123 hook = find_hook("post-update");
1124 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
1125 if (cmd->error_string || cmd->did_not_exist)
1132 argv_array_push(&proc.args, hook);
1133 for (cmd = commands; cmd; cmd = cmd->next) {
1134 if (cmd->error_string || cmd->did_not_exist)
1136 argv_array_push(&proc.args, cmd->ref_name);
1140 proc.stdout_to_stderr = 1;
1141 proc.err = use_sideband ? -1 : 0;
1143 if (!start_command(&proc)) {
1145 copy_to_sideband(proc.err, -1, NULL);
1146 finish_command(&proc);
1150 static void check_aliased_update(struct command *cmd, struct string_list *list)
1152 struct strbuf buf = STRBUF_INIT;
1153 const char *dst_name;
1154 struct string_list_item *item;
1155 struct command *dst_cmd;
1156 unsigned char sha1[GIT_SHA1_RAWSZ];
1157 char cmd_oldh[GIT_SHA1_HEXSZ + 1],
1158 cmd_newh[GIT_SHA1_HEXSZ + 1],
1159 dst_oldh[GIT_SHA1_HEXSZ + 1],
1160 dst_newh[GIT_SHA1_HEXSZ + 1];
1163 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
1164 dst_name = resolve_ref_unsafe(buf.buf, 0, sha1, &flag);
1165 strbuf_release(&buf);
1167 if (!(flag & REF_ISSYMREF))
1171 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
1172 cmd->skip_update = 1;
1173 cmd->error_string = "broken symref";
1176 dst_name = strip_namespace(dst_name);
1178 if ((item = string_list_lookup(list, dst_name)) == NULL)
1181 cmd->skip_update = 1;
1183 dst_cmd = (struct command *) item->util;
1185 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
1186 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
1189 dst_cmd->skip_update = 1;
1191 find_unique_abbrev_r(cmd_oldh, cmd->old_sha1, DEFAULT_ABBREV);
1192 find_unique_abbrev_r(cmd_newh, cmd->new_sha1, DEFAULT_ABBREV);
1193 find_unique_abbrev_r(dst_oldh, dst_cmd->old_sha1, DEFAULT_ABBREV);
1194 find_unique_abbrev_r(dst_newh, dst_cmd->new_sha1, DEFAULT_ABBREV);
1195 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
1196 " its target '%s' (%s..%s)",
1197 cmd->ref_name, cmd_oldh, cmd_newh,
1198 dst_cmd->ref_name, dst_oldh, dst_newh);
1200 cmd->error_string = dst_cmd->error_string =
1201 "inconsistent aliased update";
1204 static void check_aliased_updates(struct command *commands)
1206 struct command *cmd;
1207 struct string_list ref_list = STRING_LIST_INIT_NODUP;
1209 for (cmd = commands; cmd; cmd = cmd->next) {
1210 struct string_list_item *item =
1211 string_list_append(&ref_list, cmd->ref_name);
1212 item->util = (void *)cmd;
1214 string_list_sort(&ref_list);
1216 for (cmd = commands; cmd; cmd = cmd->next) {
1217 if (!cmd->error_string)
1218 check_aliased_update(cmd, &ref_list);
1221 string_list_clear(&ref_list, 0);
1224 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
1226 struct command **cmd_list = cb_data;
1227 struct command *cmd = *cmd_list;
1229 if (!cmd || is_null_sha1(cmd->new_sha1))
1230 return -1; /* end of list */
1231 *cmd_list = NULL; /* this returns only one */
1232 hashcpy(sha1, cmd->new_sha1);
1236 static void set_connectivity_errors(struct command *commands,
1237 struct shallow_info *si)
1239 struct command *cmd;
1241 for (cmd = commands; cmd; cmd = cmd->next) {
1242 struct command *singleton = cmd;
1243 if (shallow_update && si->shallow_ref[cmd->index])
1244 /* to be checked in update_shallow_ref() */
1246 if (!check_connected(command_singleton_iterator, &singleton,
1249 cmd->error_string = "missing necessary objects";
1253 struct iterate_data {
1254 struct command *cmds;
1255 struct shallow_info *si;
1258 static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
1260 struct iterate_data *data = cb_data;
1261 struct command **cmd_list = &data->cmds;
1262 struct command *cmd = *cmd_list;
1264 for (; cmd; cmd = cmd->next) {
1265 if (shallow_update && data->si->shallow_ref[cmd->index])
1266 /* to be checked in update_shallow_ref() */
1268 if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
1269 hashcpy(sha1, cmd->new_sha1);
1270 *cmd_list = cmd->next;
1275 return -1; /* end of list */
1278 static void reject_updates_to_hidden(struct command *commands)
1280 struct strbuf refname_full = STRBUF_INIT;
1282 struct command *cmd;
1284 strbuf_addstr(&refname_full, get_git_namespace());
1285 prefix_len = refname_full.len;
1287 for (cmd = commands; cmd; cmd = cmd->next) {
1288 if (cmd->error_string)
1291 strbuf_setlen(&refname_full, prefix_len);
1292 strbuf_addstr(&refname_full, cmd->ref_name);
1294 if (!ref_is_hidden(cmd->ref_name, refname_full.buf))
1296 if (is_null_sha1(cmd->new_sha1))
1297 cmd->error_string = "deny deleting a hidden ref";
1299 cmd->error_string = "deny updating a hidden ref";
1302 strbuf_release(&refname_full);
1305 static int should_process_cmd(struct command *cmd)
1307 return !cmd->error_string && !cmd->skip_update;
1310 static void warn_if_skipped_connectivity_check(struct command *commands,
1311 struct shallow_info *si)
1313 struct command *cmd;
1314 int checked_connectivity = 1;
1316 for (cmd = commands; cmd; cmd = cmd->next) {
1317 if (should_process_cmd(cmd) && si->shallow_ref[cmd->index]) {
1318 error("BUG: connectivity check has not been run on ref %s",
1320 checked_connectivity = 0;
1323 if (!checked_connectivity)
1324 die("BUG: connectivity check skipped???");
1327 static void execute_commands_non_atomic(struct command *commands,
1328 struct shallow_info *si)
1330 struct command *cmd;
1331 struct strbuf err = STRBUF_INIT;
1333 for (cmd = commands; cmd; cmd = cmd->next) {
1334 if (!should_process_cmd(cmd))
1337 transaction = ref_transaction_begin(&err);
1339 rp_error("%s", err.buf);
1341 cmd->error_string = "transaction failed to start";
1345 cmd->error_string = update(cmd, si);
1347 if (!cmd->error_string
1348 && ref_transaction_commit(transaction, &err)) {
1349 rp_error("%s", err.buf);
1351 cmd->error_string = "failed to update ref";
1353 ref_transaction_free(transaction);
1355 strbuf_release(&err);
1358 static void execute_commands_atomic(struct command *commands,
1359 struct shallow_info *si)
1361 struct command *cmd;
1362 struct strbuf err = STRBUF_INIT;
1363 const char *reported_error = "atomic push failure";
1365 transaction = ref_transaction_begin(&err);
1367 rp_error("%s", err.buf);
1369 reported_error = "transaction failed to start";
1373 for (cmd = commands; cmd; cmd = cmd->next) {
1374 if (!should_process_cmd(cmd))
1377 cmd->error_string = update(cmd, si);
1379 if (cmd->error_string)
1383 if (ref_transaction_commit(transaction, &err)) {
1384 rp_error("%s", err.buf);
1385 reported_error = "atomic transaction failed";
1391 for (cmd = commands; cmd; cmd = cmd->next)
1392 if (!cmd->error_string)
1393 cmd->error_string = reported_error;
1396 ref_transaction_free(transaction);
1397 strbuf_release(&err);
1400 static void execute_commands(struct command *commands,
1401 const char *unpacker_error,
1402 struct shallow_info *si,
1403 const struct string_list *push_options)
1405 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1406 struct command *cmd;
1407 unsigned char sha1[20];
1408 struct iterate_data data;
1412 if (unpacker_error) {
1413 for (cmd = commands; cmd; cmd = cmd->next)
1414 cmd->error_string = "unpacker error";
1419 memset(&muxer, 0, sizeof(muxer));
1420 muxer.proc = copy_to_sideband;
1422 if (!start_async(&muxer))
1424 /* ...else, continue without relaying sideband */
1427 data.cmds = commands;
1429 opt.err_fd = err_fd;
1430 opt.progress = err_fd && !quiet;
1431 if (check_connected(iterate_receive_command_list, &data, &opt))
1432 set_connectivity_errors(commands, si);
1435 finish_async(&muxer);
1437 reject_updates_to_hidden(commands);
1439 if (run_receive_hook(commands, "pre-receive", 0, push_options)) {
1440 for (cmd = commands; cmd; cmd = cmd->next) {
1441 if (!cmd->error_string)
1442 cmd->error_string = "pre-receive hook declined";
1447 check_aliased_updates(commands);
1449 free(head_name_to_free);
1450 head_name = head_name_to_free = resolve_refdup("HEAD", 0, sha1, NULL);
1453 execute_commands_atomic(commands, si);
1455 execute_commands_non_atomic(commands, si);
1458 warn_if_skipped_connectivity_check(commands, si);
1461 static struct command **queue_command(struct command **tail,
1465 unsigned char old_sha1[20], new_sha1[20];
1466 struct command *cmd;
1467 const char *refname;
1473 get_sha1_hex(line, old_sha1) ||
1474 get_sha1_hex(line + 41, new_sha1))
1475 die("protocol error: expected old/new/ref, got '%s'", line);
1477 refname = line + 82;
1478 reflen = linelen - 82;
1479 FLEX_ALLOC_MEM(cmd, ref_name, refname, reflen);
1480 hashcpy(cmd->old_sha1, old_sha1);
1481 hashcpy(cmd->new_sha1, new_sha1);
1486 static void queue_commands_from_cert(struct command **tail,
1487 struct strbuf *push_cert)
1489 const char *boc, *eoc;
1492 die("protocol error: got both push certificate and unsigned commands");
1494 boc = strstr(push_cert->buf, "\n\n");
1496 die("malformed push certificate %.*s", 100, push_cert->buf);
1499 eoc = push_cert->buf + parse_signature(push_cert->buf, push_cert->len);
1502 const char *eol = memchr(boc, '\n', eoc - boc);
1503 tail = queue_command(tail, boc, eol ? eol - boc : eoc - eol);
1504 boc = eol ? eol + 1 : eoc;
1508 static struct command *read_head_info(struct sha1_array *shallow)
1510 struct command *commands = NULL;
1511 struct command **p = &commands;
1516 line = packet_read_line(0, &len);
1520 if (len == 48 && starts_with(line, "shallow ")) {
1521 unsigned char sha1[20];
1522 if (get_sha1_hex(line + 8, sha1))
1523 die("protocol error: expected shallow sha, got '%s'",
1525 sha1_array_append(shallow, sha1);
1529 linelen = strlen(line);
1530 if (linelen < len) {
1531 const char *feature_list = line + linelen + 1;
1532 if (parse_feature_request(feature_list, "report-status"))
1534 if (parse_feature_request(feature_list, "side-band-64k"))
1535 use_sideband = LARGE_PACKET_MAX;
1536 if (parse_feature_request(feature_list, "quiet"))
1538 if (advertise_atomic_push
1539 && parse_feature_request(feature_list, "atomic"))
1541 if (advertise_push_options
1542 && parse_feature_request(feature_list, "push-options"))
1543 use_push_options = 1;
1546 if (!strcmp(line, "push-cert")) {
1551 len = packet_read(0, NULL, NULL,
1552 certbuf, sizeof(certbuf), 0);
1557 if (!strcmp(certbuf, "push-cert-end\n"))
1558 break; /* end of cert */
1559 strbuf_addstr(&push_cert, certbuf);
1567 p = queue_command(p, line, linelen);
1571 queue_commands_from_cert(p, &push_cert);
1576 static void read_push_options(struct string_list *options)
1582 line = packet_read_line(0, &len);
1587 string_list_append(options, line);
1591 static const char *parse_pack_header(struct pack_header *hdr)
1593 switch (read_pack_header(0, hdr)) {
1595 return "eof before pack header was fully read";
1597 case PH_ERROR_PACK_SIGNATURE:
1598 return "protocol error (pack signature mismatch detected)";
1600 case PH_ERROR_PROTOCOL:
1601 return "protocol error (pack version unsupported)";
1604 return "unknown error in parse_pack_header";
1611 static const char *pack_lockfile;
1613 static const char *unpack(int err_fd, struct shallow_info *si)
1615 struct pack_header hdr;
1616 const char *hdr_err;
1619 struct child_process child = CHILD_PROCESS_INIT;
1620 int fsck_objects = (receive_fsck_objects >= 0
1621 ? receive_fsck_objects
1622 : transfer_fsck_objects >= 0
1623 ? transfer_fsck_objects
1626 hdr_err = parse_pack_header(&hdr);
1632 snprintf(hdr_arg, sizeof(hdr_arg),
1633 "--pack_header=%"PRIu32",%"PRIu32,
1634 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
1636 if (si->nr_ours || si->nr_theirs) {
1637 alt_shallow_file = setup_temporary_shallow(si->shallow);
1638 argv_array_push(&child.args, "--shallow-file");
1639 argv_array_push(&child.args, alt_shallow_file);
1642 if (ntohl(hdr.hdr_entries) < unpack_limit) {
1643 argv_array_pushl(&child.args, "unpack-objects", hdr_arg, NULL);
1645 argv_array_push(&child.args, "-q");
1647 argv_array_pushf(&child.args, "--strict%s",
1648 fsck_msg_types.buf);
1650 argv_array_pushf(&child.args, "--max-input-size=%"PRIuMAX,
1651 (uintmax_t)max_input_size);
1652 child.no_stdout = 1;
1655 status = run_command(&child);
1657 return "unpack-objects abnormal exit";
1661 argv_array_pushl(&child.args, "index-pack",
1662 "--stdin", hdr_arg, NULL);
1664 if (gethostname(hostname, sizeof(hostname)))
1665 xsnprintf(hostname, sizeof(hostname), "localhost");
1666 argv_array_pushf(&child.args,
1667 "--keep=receive-pack %"PRIuMAX" on %s",
1668 (uintmax_t)getpid(),
1671 if (!quiet && err_fd)
1672 argv_array_push(&child.args, "--show-resolving-progress");
1674 argv_array_push(&child.args, "--report-end-of-input");
1676 argv_array_pushf(&child.args, "--strict%s",
1677 fsck_msg_types.buf);
1679 argv_array_push(&child.args, "--fix-thin");
1681 argv_array_pushf(&child.args, "--max-input-size=%"PRIuMAX,
1682 (uintmax_t)max_input_size);
1686 status = start_command(&child);
1688 return "index-pack fork failed";
1689 pack_lockfile = index_pack_lockfile(child.out);
1691 status = finish_command(&child);
1693 return "index-pack abnormal exit";
1694 reprepare_packed_git();
1699 static const char *unpack_with_sideband(struct shallow_info *si)
1705 return unpack(0, si);
1707 use_keepalive = KEEPALIVE_AFTER_NUL;
1708 memset(&muxer, 0, sizeof(muxer));
1709 muxer.proc = copy_to_sideband;
1711 if (start_async(&muxer))
1714 ret = unpack(muxer.in, si);
1716 finish_async(&muxer);
1720 static void prepare_shallow_update(struct command *commands,
1721 struct shallow_info *si)
1723 int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1725 ALLOC_ARRAY(si->used_shallow, si->shallow->nr);
1726 assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
1728 si->need_reachability_test =
1729 xcalloc(si->shallow->nr, sizeof(*si->need_reachability_test));
1731 xcalloc(si->shallow->nr, sizeof(*si->reachable));
1732 si->shallow_ref = xcalloc(si->ref->nr, sizeof(*si->shallow_ref));
1734 for (i = 0; i < si->nr_ours; i++)
1735 si->need_reachability_test[si->ours[i]] = 1;
1737 for (i = 0; i < si->shallow->nr; i++) {
1738 if (!si->used_shallow[i])
1740 for (j = 0; j < bitmap_size; j++) {
1741 if (!si->used_shallow[i][j])
1743 si->need_reachability_test[i]++;
1744 for (k = 0; k < 32; k++)
1745 if (si->used_shallow[i][j] & (1U << k))
1746 si->shallow_ref[j * 32 + k]++;
1750 * true for those associated with some refs and belong
1751 * in "ours" list aka "step 7 not done yet"
1753 si->need_reachability_test[i] =
1754 si->need_reachability_test[i] > 1;
1758 * keep hooks happy by forcing a temporary shallow file via
1759 * env variable because we can't add --shallow-file to every
1760 * command. check_everything_connected() will be done with
1761 * true .git/shallow though.
1763 setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
1766 static void update_shallow_info(struct command *commands,
1767 struct shallow_info *si,
1768 struct sha1_array *ref)
1770 struct command *cmd;
1772 remove_nonexistent_theirs_shallow(si);
1773 if (!si->nr_ours && !si->nr_theirs) {
1778 for (cmd = commands; cmd; cmd = cmd->next) {
1779 if (is_null_sha1(cmd->new_sha1))
1781 sha1_array_append(ref, cmd->new_sha1);
1782 cmd->index = ref->nr - 1;
1786 if (shallow_update) {
1787 prepare_shallow_update(commands, si);
1791 ALLOC_ARRAY(ref_status, ref->nr);
1792 assign_shallow_commits_to_refs(si, NULL, ref_status);
1793 for (cmd = commands; cmd; cmd = cmd->next) {
1794 if (is_null_sha1(cmd->new_sha1))
1796 if (ref_status[cmd->index]) {
1797 cmd->error_string = "shallow update not allowed";
1798 cmd->skip_update = 1;
1804 static void report(struct command *commands, const char *unpack_status)
1806 struct command *cmd;
1807 struct strbuf buf = STRBUF_INIT;
1809 packet_buf_write(&buf, "unpack %s\n",
1810 unpack_status ? unpack_status : "ok");
1811 for (cmd = commands; cmd; cmd = cmd->next) {
1812 if (!cmd->error_string)
1813 packet_buf_write(&buf, "ok %s\n",
1816 packet_buf_write(&buf, "ng %s %s\n",
1817 cmd->ref_name, cmd->error_string);
1819 packet_buf_flush(&buf);
1822 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
1824 write_or_die(1, buf.buf, buf.len);
1825 strbuf_release(&buf);
1828 static int delete_only(struct command *commands)
1830 struct command *cmd;
1831 for (cmd = commands; cmd; cmd = cmd->next) {
1832 if (!is_null_sha1(cmd->new_sha1))
1838 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
1840 int advertise_refs = 0;
1841 struct command *commands;
1842 struct sha1_array shallow = SHA1_ARRAY_INIT;
1843 struct sha1_array ref = SHA1_ARRAY_INIT;
1844 struct shallow_info si;
1846 struct option options[] = {
1847 OPT__QUIET(&quiet, N_("quiet")),
1848 OPT_HIDDEN_BOOL(0, "stateless-rpc", &stateless_rpc, NULL),
1849 OPT_HIDDEN_BOOL(0, "advertise-refs", &advertise_refs, NULL),
1850 OPT_HIDDEN_BOOL(0, "reject-thin-pack-for-testing", &reject_thin, NULL),
1854 packet_trace_identity("receive-pack");
1856 argc = parse_options(argc, argv, prefix, options, receive_pack_usage, 0);
1859 usage_msg_opt(_("Too many arguments."), receive_pack_usage, options);
1861 usage_msg_opt(_("You must specify a directory."), receive_pack_usage, options);
1863 service_dir = argv[0];
1867 if (!enter_repo(service_dir, 0))
1868 die("'%s' does not appear to be a git repository", service_dir);
1870 git_config(receive_pack_config, NULL);
1871 if (cert_nonce_seed)
1872 push_cert_nonce = prepare_push_cert_nonce(service_dir, time(NULL));
1874 if (0 <= transfer_unpack_limit)
1875 unpack_limit = transfer_unpack_limit;
1876 else if (0 <= receive_unpack_limit)
1877 unpack_limit = receive_unpack_limit;
1879 if (advertise_refs || !stateless_rpc) {
1885 if ((commands = read_head_info(&shallow)) != NULL) {
1886 const char *unpack_status = NULL;
1887 struct string_list push_options = STRING_LIST_INIT_DUP;
1889 if (use_push_options)
1890 read_push_options(&push_options);
1892 prepare_shallow_info(&si, &shallow);
1893 if (!si.nr_ours && !si.nr_theirs)
1895 if (!delete_only(commands)) {
1896 unpack_status = unpack_with_sideband(&si);
1897 update_shallow_info(commands, &si, &ref);
1899 use_keepalive = KEEPALIVE_ALWAYS;
1900 execute_commands(commands, unpack_status, &si,
1903 unlink_or_warn(pack_lockfile);
1905 report(commands, unpack_status);
1906 run_receive_hook(commands, "post-receive", 1,
1908 run_update_post_hook(commands);
1909 if (push_options.nr)
1910 string_list_clear(&push_options, 0);
1912 const char *argv_gc_auto[] = {
1913 "gc", "--auto", "--quiet", NULL,
1915 struct child_process proc = CHILD_PROCESS_INIT;
1918 proc.stdout_to_stderr = 1;
1919 proc.err = use_sideband ? -1 : 0;
1921 proc.argv = argv_gc_auto;
1924 if (!start_command(&proc)) {
1926 copy_to_sideband(proc.err, -1, NULL);
1927 finish_command(&proc);
1930 if (auto_update_server_info)
1931 update_server_info(0);
1932 clear_shallow_info(&si);
1936 sha1_array_clear(&shallow);
1937 sha1_array_clear(&ref);
1938 free((void *)push_cert_nonce);