the beginning of the signed push
[git/jrn.git] / builtin / receive-pack.c
blobf30df8ab603d88fb9c22117c35c991a9ece353cd
1 #include "builtin.h"
2 #include "pack.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "run-command.h"
7 #include "exec_cmd.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "remote.h"
11 #include "connect.h"
12 #include "transport.h"
13 #include "string-list.h"
14 #include "sha1-array.h"
15 #include "connected.h"
16 #include "argv-array.h"
17 #include "version.h"
19 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
21 enum deny_action {
22 DENY_UNCONFIGURED,
23 DENY_IGNORE,
24 DENY_WARN,
25 DENY_REFUSE
28 static int deny_deletes;
29 static int deny_non_fast_forwards;
30 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
31 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
32 static int receive_fsck_objects = -1;
33 static int transfer_fsck_objects = -1;
34 static int receive_unpack_limit = -1;
35 static int transfer_unpack_limit = -1;
36 static int unpack_limit = 100;
37 static int report_status;
38 static int use_sideband;
39 static int quiet;
40 static int prefer_ofs_delta = 1;
41 static int auto_update_server_info;
42 static int auto_gc = 1;
43 static int fix_thin = 1;
44 static const char *head_name;
45 static void *head_name_to_free;
46 static int sent_capabilities;
47 static int shallow_update;
48 static const char *alt_shallow_file;
49 static struct strbuf push_cert = STRBUF_INIT;
50 static unsigned char push_cert_sha1[20];
52 static enum deny_action parse_deny_action(const char *var, const char *value)
54 if (value) {
55 if (!strcasecmp(value, "ignore"))
56 return DENY_IGNORE;
57 if (!strcasecmp(value, "warn"))
58 return DENY_WARN;
59 if (!strcasecmp(value, "refuse"))
60 return DENY_REFUSE;
62 if (git_config_bool(var, value))
63 return DENY_REFUSE;
64 return DENY_IGNORE;
67 static int receive_pack_config(const char *var, const char *value, void *cb)
69 int status = parse_hide_refs_config(var, value, "receive");
71 if (status)
72 return status;
74 if (strcmp(var, "receive.denydeletes") == 0) {
75 deny_deletes = git_config_bool(var, value);
76 return 0;
79 if (strcmp(var, "receive.denynonfastforwards") == 0) {
80 deny_non_fast_forwards = git_config_bool(var, value);
81 return 0;
84 if (strcmp(var, "receive.unpacklimit") == 0) {
85 receive_unpack_limit = git_config_int(var, value);
86 return 0;
89 if (strcmp(var, "transfer.unpacklimit") == 0) {
90 transfer_unpack_limit = git_config_int(var, value);
91 return 0;
94 if (strcmp(var, "receive.fsckobjects") == 0) {
95 receive_fsck_objects = git_config_bool(var, value);
96 return 0;
99 if (strcmp(var, "transfer.fsckobjects") == 0) {
100 transfer_fsck_objects = git_config_bool(var, value);
101 return 0;
104 if (!strcmp(var, "receive.denycurrentbranch")) {
105 deny_current_branch = parse_deny_action(var, value);
106 return 0;
109 if (strcmp(var, "receive.denydeletecurrent") == 0) {
110 deny_delete_current = parse_deny_action(var, value);
111 return 0;
114 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
115 prefer_ofs_delta = git_config_bool(var, value);
116 return 0;
119 if (strcmp(var, "receive.updateserverinfo") == 0) {
120 auto_update_server_info = git_config_bool(var, value);
121 return 0;
124 if (strcmp(var, "receive.autogc") == 0) {
125 auto_gc = git_config_bool(var, value);
126 return 0;
129 if (strcmp(var, "receive.shallowupdate") == 0) {
130 shallow_update = git_config_bool(var, value);
131 return 0;
134 return git_default_config(var, value, cb);
137 static void show_ref(const char *path, const unsigned char *sha1)
139 if (ref_is_hidden(path))
140 return;
142 if (sent_capabilities)
143 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
144 else
145 packet_write(1, "%s %s%c%s%s agent=%s\n",
146 sha1_to_hex(sha1), path, 0,
147 " report-status delete-refs side-band-64k quiet push-cert",
148 prefer_ofs_delta ? " ofs-delta" : "",
149 git_user_agent_sanitized());
150 sent_capabilities = 1;
153 static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused)
155 path = strip_namespace(path);
157 * Advertise refs outside our current namespace as ".have"
158 * refs, so that the client can use them to minimize data
159 * transfer but will otherwise ignore them. This happens to
160 * cover ".have" that are thrown in by add_one_alternate_ref()
161 * to mark histories that are complete in our alternates as
162 * well.
164 if (!path)
165 path = ".have";
166 show_ref(path, sha1);
167 return 0;
170 static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
172 show_ref(".have", sha1);
175 static void collect_one_alternate_ref(const struct ref *ref, void *data)
177 struct sha1_array *sa = data;
178 sha1_array_append(sa, ref->old_sha1);
181 static void write_head_info(void)
183 struct sha1_array sa = SHA1_ARRAY_INIT;
184 for_each_alternate_ref(collect_one_alternate_ref, &sa);
185 sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
186 sha1_array_clear(&sa);
187 for_each_ref(show_ref_cb, NULL);
188 if (!sent_capabilities)
189 show_ref("capabilities^{}", null_sha1);
191 advertise_shallow_grafts(1);
193 /* EOF */
194 packet_flush(1);
197 struct command {
198 struct command *next;
199 const char *error_string;
200 unsigned int skip_update:1,
201 did_not_exist:1;
202 int index;
203 unsigned char old_sha1[20];
204 unsigned char new_sha1[20];
205 char ref_name[FLEX_ARRAY]; /* more */
208 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
209 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
211 static void report_message(const char *prefix, const char *err, va_list params)
213 int sz = strlen(prefix);
214 char msg[4096];
216 strncpy(msg, prefix, sz);
217 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
218 if (sz > (sizeof(msg) - 1))
219 sz = sizeof(msg) - 1;
220 msg[sz++] = '\n';
222 if (use_sideband)
223 send_sideband(1, 2, msg, sz, use_sideband);
224 else
225 xwrite(2, msg, sz);
228 static void rp_warning(const char *err, ...)
230 va_list params;
231 va_start(params, err);
232 report_message("warning: ", err, params);
233 va_end(params);
236 static void rp_error(const char *err, ...)
238 va_list params;
239 va_start(params, err);
240 report_message("error: ", err, params);
241 va_end(params);
244 static int copy_to_sideband(int in, int out, void *arg)
246 char data[128];
247 while (1) {
248 ssize_t sz = xread(in, data, sizeof(data));
249 if (sz <= 0)
250 break;
251 send_sideband(1, 2, data, sz, use_sideband);
253 close(in);
254 return 0;
257 static void prepare_push_cert_sha1(struct child_process *proc)
259 static int already_done;
260 struct argv_array env = ARGV_ARRAY_INIT;
262 if (!already_done) {
263 already_done = 1;
264 if (write_sha1_file(push_cert.buf, push_cert.len, "blob", push_cert_sha1))
265 hashclr(push_cert_sha1);
267 if (!is_null_sha1(push_cert_sha1)) {
268 argv_array_pushf(&env, "GIT_PUSH_CERT=%s", sha1_to_hex(push_cert_sha1));
269 proc->env = env.argv;
273 typedef int (*feed_fn)(void *, const char **, size_t *);
274 static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
276 struct child_process proc;
277 struct async muxer;
278 const char *argv[2];
279 int code;
281 argv[0] = find_hook(hook_name);
282 if (!argv[0])
283 return 0;
285 argv[1] = NULL;
287 memset(&proc, 0, sizeof(proc));
288 proc.argv = argv;
289 proc.in = -1;
290 proc.stdout_to_stderr = 1;
292 prepare_push_cert_sha1(&proc);
294 if (use_sideband) {
295 memset(&muxer, 0, sizeof(muxer));
296 muxer.proc = copy_to_sideband;
297 muxer.in = -1;
298 code = start_async(&muxer);
299 if (code)
300 return code;
301 proc.err = muxer.in;
304 code = start_command(&proc);
305 if (code) {
306 if (use_sideband)
307 finish_async(&muxer);
308 return code;
311 while (1) {
312 const char *buf;
313 size_t n;
314 if (feed(feed_state, &buf, &n))
315 break;
316 if (write_in_full(proc.in, buf, n) != n)
317 break;
319 close(proc.in);
320 if (use_sideband)
321 finish_async(&muxer);
322 return finish_command(&proc);
325 struct receive_hook_feed_state {
326 struct command *cmd;
327 int skip_broken;
328 struct strbuf buf;
331 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
333 struct receive_hook_feed_state *state = state_;
334 struct command *cmd = state->cmd;
336 while (cmd &&
337 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
338 cmd = cmd->next;
339 if (!cmd)
340 return -1; /* EOF */
341 strbuf_reset(&state->buf);
342 strbuf_addf(&state->buf, "%s %s %s\n",
343 sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
344 cmd->ref_name);
345 state->cmd = cmd->next;
346 if (bufp) {
347 *bufp = state->buf.buf;
348 *sizep = state->buf.len;
350 return 0;
353 static int run_receive_hook(struct command *commands, const char *hook_name,
354 int skip_broken)
356 struct receive_hook_feed_state state;
357 int status;
359 strbuf_init(&state.buf, 0);
360 state.cmd = commands;
361 state.skip_broken = skip_broken;
362 if (feed_receive_hook(&state, NULL, NULL))
363 return 0;
364 state.cmd = commands;
365 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
366 strbuf_release(&state.buf);
367 return status;
370 static int run_update_hook(struct command *cmd)
372 const char *argv[5];
373 struct child_process proc;
374 int code;
376 argv[0] = find_hook("update");
377 if (!argv[0])
378 return 0;
380 argv[1] = cmd->ref_name;
381 argv[2] = sha1_to_hex(cmd->old_sha1);
382 argv[3] = sha1_to_hex(cmd->new_sha1);
383 argv[4] = NULL;
385 memset(&proc, 0, sizeof(proc));
386 proc.no_stdin = 1;
387 proc.stdout_to_stderr = 1;
388 proc.err = use_sideband ? -1 : 0;
389 proc.argv = argv;
391 code = start_command(&proc);
392 if (code)
393 return code;
394 if (use_sideband)
395 copy_to_sideband(proc.err, -1, NULL);
396 return finish_command(&proc);
399 static int is_ref_checked_out(const char *ref)
401 if (is_bare_repository())
402 return 0;
404 if (!head_name)
405 return 0;
406 return !strcmp(head_name, ref);
409 static char *refuse_unconfigured_deny_msg[] = {
410 "By default, updating the current branch in a non-bare repository",
411 "is denied, because it will make the index and work tree inconsistent",
412 "with what you pushed, and will require 'git reset --hard' to match",
413 "the work tree to HEAD.",
415 "You can set 'receive.denyCurrentBranch' configuration variable to",
416 "'ignore' or 'warn' in the remote repository to allow pushing into",
417 "its current branch; however, this is not recommended unless you",
418 "arranged to update its work tree to match what you pushed in some",
419 "other way.",
421 "To squelch this message and still keep the default behaviour, set",
422 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
425 static void refuse_unconfigured_deny(void)
427 int i;
428 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
429 rp_error("%s", refuse_unconfigured_deny_msg[i]);
432 static char *refuse_unconfigured_deny_delete_current_msg[] = {
433 "By default, deleting the current branch is denied, because the next",
434 "'git clone' won't result in any file checked out, causing confusion.",
436 "You can set 'receive.denyDeleteCurrent' configuration variable to",
437 "'warn' or 'ignore' in the remote repository to allow deleting the",
438 "current branch, with or without a warning message.",
440 "To squelch this message, you can set it to 'refuse'."
443 static void refuse_unconfigured_deny_delete_current(void)
445 int i;
446 for (i = 0;
447 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
448 i++)
449 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
452 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]);
453 static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
455 static struct lock_file shallow_lock;
456 struct sha1_array extra = SHA1_ARRAY_INIT;
457 const char *alt_file;
458 uint32_t mask = 1 << (cmd->index % 32);
459 int i;
461 trace_printf_key(&trace_shallow,
462 "shallow: update_shallow_ref %s\n", cmd->ref_name);
463 for (i = 0; i < si->shallow->nr; i++)
464 if (si->used_shallow[i] &&
465 (si->used_shallow[i][cmd->index / 32] & mask) &&
466 !delayed_reachability_test(si, i))
467 sha1_array_append(&extra, si->shallow->sha1[i]);
469 setup_alternate_shallow(&shallow_lock, &alt_file, &extra);
470 if (check_shallow_connected(command_singleton_iterator,
471 0, cmd, alt_file)) {
472 rollback_lock_file(&shallow_lock);
473 sha1_array_clear(&extra);
474 return -1;
477 commit_lock_file(&shallow_lock);
480 * Make sure setup_alternate_shallow() for the next ref does
481 * not lose these new roots..
483 for (i = 0; i < extra.nr; i++)
484 register_shallow(extra.sha1[i]);
486 si->shallow_ref[cmd->index] = 0;
487 sha1_array_clear(&extra);
488 return 0;
491 static const char *update(struct command *cmd, struct shallow_info *si)
493 const char *name = cmd->ref_name;
494 struct strbuf namespaced_name_buf = STRBUF_INIT;
495 const char *namespaced_name;
496 unsigned char *old_sha1 = cmd->old_sha1;
497 unsigned char *new_sha1 = cmd->new_sha1;
498 struct ref_lock *lock;
500 /* only refs/... are allowed */
501 if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
502 rp_error("refusing to create funny ref '%s' remotely", name);
503 return "funny refname";
506 strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
507 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
509 if (is_ref_checked_out(namespaced_name)) {
510 switch (deny_current_branch) {
511 case DENY_IGNORE:
512 break;
513 case DENY_WARN:
514 rp_warning("updating the current branch");
515 break;
516 case DENY_REFUSE:
517 case DENY_UNCONFIGURED:
518 rp_error("refusing to update checked out branch: %s", name);
519 if (deny_current_branch == DENY_UNCONFIGURED)
520 refuse_unconfigured_deny();
521 return "branch is currently checked out";
525 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
526 error("unpack should have generated %s, "
527 "but I can't find it!", sha1_to_hex(new_sha1));
528 return "bad pack";
531 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
532 if (deny_deletes && starts_with(name, "refs/heads/")) {
533 rp_error("denying ref deletion for %s", name);
534 return "deletion prohibited";
537 if (!strcmp(namespaced_name, head_name)) {
538 switch (deny_delete_current) {
539 case DENY_IGNORE:
540 break;
541 case DENY_WARN:
542 rp_warning("deleting the current branch");
543 break;
544 case DENY_REFUSE:
545 case DENY_UNCONFIGURED:
546 if (deny_delete_current == DENY_UNCONFIGURED)
547 refuse_unconfigured_deny_delete_current();
548 rp_error("refusing to delete the current branch: %s", name);
549 return "deletion of the current branch prohibited";
554 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
555 !is_null_sha1(old_sha1) &&
556 starts_with(name, "refs/heads/")) {
557 struct object *old_object, *new_object;
558 struct commit *old_commit, *new_commit;
560 old_object = parse_object(old_sha1);
561 new_object = parse_object(new_sha1);
563 if (!old_object || !new_object ||
564 old_object->type != OBJ_COMMIT ||
565 new_object->type != OBJ_COMMIT) {
566 error("bad sha1 objects for %s", name);
567 return "bad ref";
569 old_commit = (struct commit *)old_object;
570 new_commit = (struct commit *)new_object;
571 if (!in_merge_bases(old_commit, new_commit)) {
572 rp_error("denying non-fast-forward %s"
573 " (you should pull first)", name);
574 return "non-fast-forward";
577 if (run_update_hook(cmd)) {
578 rp_error("hook declined to update %s", name);
579 return "hook declined";
582 if (is_null_sha1(new_sha1)) {
583 if (!parse_object(old_sha1)) {
584 old_sha1 = NULL;
585 if (ref_exists(name)) {
586 rp_warning("Allowing deletion of corrupt ref.");
587 } else {
588 rp_warning("Deleting a non-existent ref.");
589 cmd->did_not_exist = 1;
592 if (delete_ref(namespaced_name, old_sha1, 0)) {
593 rp_error("failed to delete %s", name);
594 return "failed to delete";
596 return NULL; /* good */
598 else {
599 if (shallow_update && si->shallow_ref[cmd->index] &&
600 update_shallow_ref(cmd, si))
601 return "shallow error";
603 lock = lock_any_ref_for_update(namespaced_name, old_sha1,
604 0, NULL);
605 if (!lock) {
606 rp_error("failed to lock %s", name);
607 return "failed to lock";
609 if (write_ref_sha1(lock, new_sha1, "push")) {
610 return "failed to write"; /* error() already called */
612 return NULL; /* good */
616 static void run_update_post_hook(struct command *commands)
618 struct command *cmd;
619 int argc;
620 const char **argv;
621 struct child_process proc;
622 char *hook;
624 hook = find_hook("post-update");
625 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
626 if (cmd->error_string || cmd->did_not_exist)
627 continue;
628 argc++;
630 if (!argc || !hook)
631 return;
633 argv = xmalloc(sizeof(*argv) * (2 + argc));
634 argv[0] = hook;
636 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
637 if (cmd->error_string || cmd->did_not_exist)
638 continue;
639 argv[argc] = xstrdup(cmd->ref_name);
640 argc++;
642 argv[argc] = NULL;
644 memset(&proc, 0, sizeof(proc));
645 proc.no_stdin = 1;
646 proc.stdout_to_stderr = 1;
647 proc.err = use_sideband ? -1 : 0;
648 proc.argv = argv;
650 if (!start_command(&proc)) {
651 if (use_sideband)
652 copy_to_sideband(proc.err, -1, NULL);
653 finish_command(&proc);
657 static void check_aliased_update(struct command *cmd, struct string_list *list)
659 struct strbuf buf = STRBUF_INIT;
660 const char *dst_name;
661 struct string_list_item *item;
662 struct command *dst_cmd;
663 unsigned char sha1[20];
664 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
665 int flag;
667 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
668 dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
669 strbuf_release(&buf);
671 if (!(flag & REF_ISSYMREF))
672 return;
674 dst_name = strip_namespace(dst_name);
675 if (!dst_name) {
676 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
677 cmd->skip_update = 1;
678 cmd->error_string = "broken symref";
679 return;
682 if ((item = string_list_lookup(list, dst_name)) == NULL)
683 return;
685 cmd->skip_update = 1;
687 dst_cmd = (struct command *) item->util;
689 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
690 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
691 return;
693 dst_cmd->skip_update = 1;
695 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
696 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
697 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
698 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
699 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
700 " its target '%s' (%s..%s)",
701 cmd->ref_name, cmd_oldh, cmd_newh,
702 dst_cmd->ref_name, dst_oldh, dst_newh);
704 cmd->error_string = dst_cmd->error_string =
705 "inconsistent aliased update";
708 static void check_aliased_updates(struct command *commands)
710 struct command *cmd;
711 struct string_list ref_list = STRING_LIST_INIT_NODUP;
713 for (cmd = commands; cmd; cmd = cmd->next) {
714 struct string_list_item *item =
715 string_list_append(&ref_list, cmd->ref_name);
716 item->util = (void *)cmd;
718 sort_string_list(&ref_list);
720 for (cmd = commands; cmd; cmd = cmd->next) {
721 if (!cmd->error_string)
722 check_aliased_update(cmd, &ref_list);
725 string_list_clear(&ref_list, 0);
728 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
730 struct command **cmd_list = cb_data;
731 struct command *cmd = *cmd_list;
733 if (!cmd || is_null_sha1(cmd->new_sha1))
734 return -1; /* end of list */
735 *cmd_list = NULL; /* this returns only one */
736 hashcpy(sha1, cmd->new_sha1);
737 return 0;
740 static void set_connectivity_errors(struct command *commands,
741 struct shallow_info *si)
743 struct command *cmd;
745 for (cmd = commands; cmd; cmd = cmd->next) {
746 struct command *singleton = cmd;
747 if (shallow_update && si->shallow_ref[cmd->index])
748 /* to be checked in update_shallow_ref() */
749 continue;
750 if (!check_everything_connected(command_singleton_iterator,
751 0, &singleton))
752 continue;
753 cmd->error_string = "missing necessary objects";
757 struct iterate_data {
758 struct command *cmds;
759 struct shallow_info *si;
762 static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
764 struct iterate_data *data = cb_data;
765 struct command **cmd_list = &data->cmds;
766 struct command *cmd = *cmd_list;
768 for (; cmd; cmd = cmd->next) {
769 if (shallow_update && data->si->shallow_ref[cmd->index])
770 /* to be checked in update_shallow_ref() */
771 continue;
772 if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
773 hashcpy(sha1, cmd->new_sha1);
774 *cmd_list = cmd->next;
775 return 0;
778 *cmd_list = NULL;
779 return -1; /* end of list */
782 static void reject_updates_to_hidden(struct command *commands)
784 struct command *cmd;
786 for (cmd = commands; cmd; cmd = cmd->next) {
787 if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
788 continue;
789 if (is_null_sha1(cmd->new_sha1))
790 cmd->error_string = "deny deleting a hidden ref";
791 else
792 cmd->error_string = "deny updating a hidden ref";
796 static void execute_commands(struct command *commands,
797 const char *unpacker_error,
798 struct shallow_info *si)
800 int checked_connectivity;
801 struct command *cmd;
802 unsigned char sha1[20];
803 struct iterate_data data;
805 if (unpacker_error) {
806 for (cmd = commands; cmd; cmd = cmd->next)
807 cmd->error_string = "unpacker error";
808 return;
811 data.cmds = commands;
812 data.si = si;
813 if (check_everything_connected(iterate_receive_command_list, 0, &data))
814 set_connectivity_errors(commands, si);
816 reject_updates_to_hidden(commands);
818 if (run_receive_hook(commands, "pre-receive", 0)) {
819 for (cmd = commands; cmd; cmd = cmd->next) {
820 if (!cmd->error_string)
821 cmd->error_string = "pre-receive hook declined";
823 return;
826 check_aliased_updates(commands);
828 free(head_name_to_free);
829 head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
831 checked_connectivity = 1;
832 for (cmd = commands; cmd; cmd = cmd->next) {
833 if (cmd->error_string)
834 continue;
836 if (cmd->skip_update)
837 continue;
839 cmd->error_string = update(cmd, si);
840 if (shallow_update && !cmd->error_string &&
841 si->shallow_ref[cmd->index]) {
842 error("BUG: connectivity check has not been run on ref %s",
843 cmd->ref_name);
844 checked_connectivity = 0;
848 if (shallow_update && !checked_connectivity)
849 error("BUG: run 'git fsck' for safety.\n"
850 "If there are errors, try to remove "
851 "the reported refs above");
854 static struct command **queue_command(struct command **p,
855 const char *line,
856 int linelen)
858 unsigned char old_sha1[20], new_sha1[20];
859 struct command *cmd;
860 const char *refname;
861 int reflen;
863 if (linelen < 83 ||
864 line[40] != ' ' ||
865 line[81] != ' ' ||
866 get_sha1_hex(line, old_sha1) ||
867 get_sha1_hex(line + 41, new_sha1))
868 die("protocol error: expected old/new/ref, got '%s'", line);
870 refname = line + 82;
871 reflen = linelen - 82;
872 cmd = xcalloc(1, sizeof(struct command) + reflen + 1);
873 hashcpy(cmd->old_sha1, old_sha1);
874 hashcpy(cmd->new_sha1, new_sha1);
875 memcpy(cmd->ref_name, refname, reflen);
876 cmd->ref_name[reflen] = '\0';
877 *p = cmd;
878 return &cmd->next;
881 static struct command *read_head_info(struct sha1_array *shallow)
883 struct command *commands = NULL;
884 struct command **p = &commands;
885 for (;;) {
886 char *line;
887 int len, linelen;
889 line = packet_read_line(0, &len);
890 if (!line)
891 break;
893 if (len == 48 && starts_with(line, "shallow ")) {
894 unsigned char sha1[20];
895 if (get_sha1_hex(line + 8, sha1))
896 die("protocol error: expected shallow sha, got '%s'",
897 line + 8);
898 sha1_array_append(shallow, sha1);
899 continue;
902 linelen = strlen(line);
903 if (linelen < len) {
904 const char *feature_list = line + linelen + 1;
905 if (parse_feature_request(feature_list, "report-status"))
906 report_status = 1;
907 if (parse_feature_request(feature_list, "side-band-64k"))
908 use_sideband = LARGE_PACKET_MAX;
909 if (parse_feature_request(feature_list, "quiet"))
910 quiet = 1;
913 if (!strcmp(line, "push-cert")) {
914 int true_flush = 0;
915 char certbuf[1024];
917 for (;;) {
918 len = packet_read(0, NULL, NULL,
919 certbuf, sizeof(certbuf), 0);
920 if (!len) {
921 true_flush = 1;
922 break;
924 if (!strcmp(certbuf, "push-cert-end\n"))
925 break; /* end of cert */
926 strbuf_addstr(&push_cert, certbuf);
929 if (true_flush)
930 break;
931 continue;
934 p = queue_command(p, line, linelen);
936 return commands;
939 static const char *parse_pack_header(struct pack_header *hdr)
941 switch (read_pack_header(0, hdr)) {
942 case PH_ERROR_EOF:
943 return "eof before pack header was fully read";
945 case PH_ERROR_PACK_SIGNATURE:
946 return "protocol error (pack signature mismatch detected)";
948 case PH_ERROR_PROTOCOL:
949 return "protocol error (pack version unsupported)";
951 default:
952 return "unknown error in parse_pack_header";
954 case 0:
955 return NULL;
959 static const char *pack_lockfile;
961 static const char *unpack(int err_fd, struct shallow_info *si)
963 struct pack_header hdr;
964 struct argv_array av = ARGV_ARRAY_INIT;
965 const char *hdr_err;
966 int status;
967 char hdr_arg[38];
968 struct child_process child;
969 int fsck_objects = (receive_fsck_objects >= 0
970 ? receive_fsck_objects
971 : transfer_fsck_objects >= 0
972 ? transfer_fsck_objects
973 : 0);
975 hdr_err = parse_pack_header(&hdr);
976 if (hdr_err) {
977 if (err_fd > 0)
978 close(err_fd);
979 return hdr_err;
981 snprintf(hdr_arg, sizeof(hdr_arg),
982 "--pack_header=%"PRIu32",%"PRIu32,
983 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
985 if (si->nr_ours || si->nr_theirs) {
986 alt_shallow_file = setup_temporary_shallow(si->shallow);
987 argv_array_pushl(&av, "--shallow-file", alt_shallow_file, NULL);
990 memset(&child, 0, sizeof(child));
991 if (ntohl(hdr.hdr_entries) < unpack_limit) {
992 argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
993 if (quiet)
994 argv_array_push(&av, "-q");
995 if (fsck_objects)
996 argv_array_push(&av, "--strict");
997 child.argv = av.argv;
998 child.no_stdout = 1;
999 child.err = err_fd;
1000 child.git_cmd = 1;
1001 status = run_command(&child);
1002 if (status)
1003 return "unpack-objects abnormal exit";
1004 } else {
1005 int s;
1006 char keep_arg[256];
1008 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
1009 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
1010 strcpy(keep_arg + s, "localhost");
1012 argv_array_pushl(&av, "index-pack",
1013 "--stdin", hdr_arg, keep_arg, NULL);
1014 if (fsck_objects)
1015 argv_array_push(&av, "--strict");
1016 if (fix_thin)
1017 argv_array_push(&av, "--fix-thin");
1018 child.argv = av.argv;
1019 child.out = -1;
1020 child.err = err_fd;
1021 child.git_cmd = 1;
1022 status = start_command(&child);
1023 if (status)
1024 return "index-pack fork failed";
1025 pack_lockfile = index_pack_lockfile(child.out);
1026 close(child.out);
1027 status = finish_command(&child);
1028 if (status)
1029 return "index-pack abnormal exit";
1030 reprepare_packed_git();
1032 return NULL;
1035 static const char *unpack_with_sideband(struct shallow_info *si)
1037 struct async muxer;
1038 const char *ret;
1040 if (!use_sideband)
1041 return unpack(0, si);
1043 memset(&muxer, 0, sizeof(muxer));
1044 muxer.proc = copy_to_sideband;
1045 muxer.in = -1;
1046 if (start_async(&muxer))
1047 return NULL;
1049 ret = unpack(muxer.in, si);
1051 finish_async(&muxer);
1052 return ret;
1055 static void prepare_shallow_update(struct command *commands,
1056 struct shallow_info *si)
1058 int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1060 si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
1061 si->shallow->nr);
1062 assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
1064 si->need_reachability_test =
1065 xcalloc(si->shallow->nr, sizeof(*si->need_reachability_test));
1066 si->reachable =
1067 xcalloc(si->shallow->nr, sizeof(*si->reachable));
1068 si->shallow_ref = xcalloc(si->ref->nr, sizeof(*si->shallow_ref));
1070 for (i = 0; i < si->nr_ours; i++)
1071 si->need_reachability_test[si->ours[i]] = 1;
1073 for (i = 0; i < si->shallow->nr; i++) {
1074 if (!si->used_shallow[i])
1075 continue;
1076 for (j = 0; j < bitmap_size; j++) {
1077 if (!si->used_shallow[i][j])
1078 continue;
1079 si->need_reachability_test[i]++;
1080 for (k = 0; k < 32; k++)
1081 if (si->used_shallow[i][j] & (1 << k))
1082 si->shallow_ref[j * 32 + k]++;
1086 * true for those associated with some refs and belong
1087 * in "ours" list aka "step 7 not done yet"
1089 si->need_reachability_test[i] =
1090 si->need_reachability_test[i] > 1;
1094 * keep hooks happy by forcing a temporary shallow file via
1095 * env variable because we can't add --shallow-file to every
1096 * command. check_everything_connected() will be done with
1097 * true .git/shallow though.
1099 setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
1102 static void update_shallow_info(struct command *commands,
1103 struct shallow_info *si,
1104 struct sha1_array *ref)
1106 struct command *cmd;
1107 int *ref_status;
1108 remove_nonexistent_theirs_shallow(si);
1109 if (!si->nr_ours && !si->nr_theirs) {
1110 shallow_update = 0;
1111 return;
1114 for (cmd = commands; cmd; cmd = cmd->next) {
1115 if (is_null_sha1(cmd->new_sha1))
1116 continue;
1117 sha1_array_append(ref, cmd->new_sha1);
1118 cmd->index = ref->nr - 1;
1120 si->ref = ref;
1122 if (shallow_update) {
1123 prepare_shallow_update(commands, si);
1124 return;
1127 ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
1128 assign_shallow_commits_to_refs(si, NULL, ref_status);
1129 for (cmd = commands; cmd; cmd = cmd->next) {
1130 if (is_null_sha1(cmd->new_sha1))
1131 continue;
1132 if (ref_status[cmd->index]) {
1133 cmd->error_string = "shallow update not allowed";
1134 cmd->skip_update = 1;
1137 free(ref_status);
1140 static void report(struct command *commands, const char *unpack_status)
1142 struct command *cmd;
1143 struct strbuf buf = STRBUF_INIT;
1145 packet_buf_write(&buf, "unpack %s\n",
1146 unpack_status ? unpack_status : "ok");
1147 for (cmd = commands; cmd; cmd = cmd->next) {
1148 if (!cmd->error_string)
1149 packet_buf_write(&buf, "ok %s\n",
1150 cmd->ref_name);
1151 else
1152 packet_buf_write(&buf, "ng %s %s\n",
1153 cmd->ref_name, cmd->error_string);
1155 packet_buf_flush(&buf);
1157 if (use_sideband)
1158 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
1159 else
1160 write_or_die(1, buf.buf, buf.len);
1161 strbuf_release(&buf);
1164 static int delete_only(struct command *commands)
1166 struct command *cmd;
1167 for (cmd = commands; cmd; cmd = cmd->next) {
1168 if (!is_null_sha1(cmd->new_sha1))
1169 return 0;
1171 return 1;
1174 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
1176 int advertise_refs = 0;
1177 int stateless_rpc = 0;
1178 int i;
1179 const char *dir = NULL;
1180 struct command *commands;
1181 struct sha1_array shallow = SHA1_ARRAY_INIT;
1182 struct sha1_array ref = SHA1_ARRAY_INIT;
1183 struct shallow_info si;
1185 packet_trace_identity("receive-pack");
1187 argv++;
1188 for (i = 1; i < argc; i++) {
1189 const char *arg = *argv++;
1191 if (*arg == '-') {
1192 if (!strcmp(arg, "--quiet")) {
1193 quiet = 1;
1194 continue;
1197 if (!strcmp(arg, "--advertise-refs")) {
1198 advertise_refs = 1;
1199 continue;
1201 if (!strcmp(arg, "--stateless-rpc")) {
1202 stateless_rpc = 1;
1203 continue;
1205 if (!strcmp(arg, "--reject-thin-pack-for-testing")) {
1206 fix_thin = 0;
1207 continue;
1210 usage(receive_pack_usage);
1212 if (dir)
1213 usage(receive_pack_usage);
1214 dir = arg;
1216 if (!dir)
1217 usage(receive_pack_usage);
1219 setup_path();
1221 if (!enter_repo(dir, 0))
1222 die("'%s' does not appear to be a git repository", dir);
1224 git_config(receive_pack_config, NULL);
1226 if (0 <= transfer_unpack_limit)
1227 unpack_limit = transfer_unpack_limit;
1228 else if (0 <= receive_unpack_limit)
1229 unpack_limit = receive_unpack_limit;
1231 if (advertise_refs || !stateless_rpc) {
1232 write_head_info();
1234 if (advertise_refs)
1235 return 0;
1237 if ((commands = read_head_info(&shallow)) != NULL) {
1238 const char *unpack_status = NULL;
1240 prepare_shallow_info(&si, &shallow);
1241 if (!si.nr_ours && !si.nr_theirs)
1242 shallow_update = 0;
1243 if (!delete_only(commands)) {
1244 unpack_status = unpack_with_sideband(&si);
1245 update_shallow_info(commands, &si, &ref);
1247 execute_commands(commands, unpack_status, &si);
1248 if (pack_lockfile)
1249 unlink_or_warn(pack_lockfile);
1250 if (report_status)
1251 report(commands, unpack_status);
1252 run_receive_hook(commands, "post-receive", 1);
1253 run_update_post_hook(commands);
1254 if (auto_gc) {
1255 const char *argv_gc_auto[] = {
1256 "gc", "--auto", "--quiet", NULL,
1258 int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR;
1259 run_command_v_opt(argv_gc_auto, opt);
1261 if (auto_update_server_info)
1262 update_server_info(0);
1263 clear_shallow_info(&si);
1265 if (use_sideband)
1266 packet_flush(1);
1267 sha1_array_clear(&shallow);
1268 sha1_array_clear(&ref);
1269 return 0;