receive-pack: do not reuse old_sha1[] for other things
[git/jrn.git] / builtin / receive-pack.c
blobc9b92bffda22c1b5340c0a61a50f894d8e9dad5b
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;
50 static enum deny_action parse_deny_action(const char *var, const char *value)
52 if (value) {
53 if (!strcasecmp(value, "ignore"))
54 return DENY_IGNORE;
55 if (!strcasecmp(value, "warn"))
56 return DENY_WARN;
57 if (!strcasecmp(value, "refuse"))
58 return DENY_REFUSE;
60 if (git_config_bool(var, value))
61 return DENY_REFUSE;
62 return DENY_IGNORE;
65 static int receive_pack_config(const char *var, const char *value, void *cb)
67 int status = parse_hide_refs_config(var, value, "receive");
69 if (status)
70 return status;
72 if (strcmp(var, "receive.denydeletes") == 0) {
73 deny_deletes = git_config_bool(var, value);
74 return 0;
77 if (strcmp(var, "receive.denynonfastforwards") == 0) {
78 deny_non_fast_forwards = git_config_bool(var, value);
79 return 0;
82 if (strcmp(var, "receive.unpacklimit") == 0) {
83 receive_unpack_limit = git_config_int(var, value);
84 return 0;
87 if (strcmp(var, "transfer.unpacklimit") == 0) {
88 transfer_unpack_limit = git_config_int(var, value);
89 return 0;
92 if (strcmp(var, "receive.fsckobjects") == 0) {
93 receive_fsck_objects = git_config_bool(var, value);
94 return 0;
97 if (strcmp(var, "transfer.fsckobjects") == 0) {
98 transfer_fsck_objects = git_config_bool(var, value);
99 return 0;
102 if (!strcmp(var, "receive.denycurrentbranch")) {
103 deny_current_branch = parse_deny_action(var, value);
104 return 0;
107 if (strcmp(var, "receive.denydeletecurrent") == 0) {
108 deny_delete_current = parse_deny_action(var, value);
109 return 0;
112 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
113 prefer_ofs_delta = git_config_bool(var, value);
114 return 0;
117 if (strcmp(var, "receive.updateserverinfo") == 0) {
118 auto_update_server_info = git_config_bool(var, value);
119 return 0;
122 if (strcmp(var, "receive.autogc") == 0) {
123 auto_gc = git_config_bool(var, value);
124 return 0;
127 if (strcmp(var, "receive.shallowupdate") == 0) {
128 shallow_update = git_config_bool(var, value);
129 return 0;
132 return git_default_config(var, value, cb);
135 static void show_ref(const char *path, const unsigned char *sha1)
137 if (ref_is_hidden(path))
138 return;
140 if (sent_capabilities)
141 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
142 else
143 packet_write(1, "%s %s%c%s%s agent=%s\n",
144 sha1_to_hex(sha1), path, 0,
145 " report-status delete-refs side-band-64k quiet",
146 prefer_ofs_delta ? " ofs-delta" : "",
147 git_user_agent_sanitized());
148 sent_capabilities = 1;
151 static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused)
153 path = strip_namespace(path);
155 * Advertise refs outside our current namespace as ".have"
156 * refs, so that the client can use them to minimize data
157 * transfer but will otherwise ignore them. This happens to
158 * cover ".have" that are thrown in by add_one_alternate_ref()
159 * to mark histories that are complete in our alternates as
160 * well.
162 if (!path)
163 path = ".have";
164 show_ref(path, sha1);
165 return 0;
168 static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
170 show_ref(".have", sha1);
173 static void collect_one_alternate_ref(const struct ref *ref, void *data)
175 struct sha1_array *sa = data;
176 sha1_array_append(sa, ref->old_sha1);
179 static void write_head_info(void)
181 struct sha1_array sa = SHA1_ARRAY_INIT;
182 for_each_alternate_ref(collect_one_alternate_ref, &sa);
183 sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
184 sha1_array_clear(&sa);
185 for_each_ref(show_ref_cb, NULL);
186 if (!sent_capabilities)
187 show_ref("capabilities^{}", null_sha1);
189 advertise_shallow_grafts(1);
191 /* EOF */
192 packet_flush(1);
195 struct command {
196 struct command *next;
197 const char *error_string;
198 unsigned int skip_update:1,
199 did_not_exist:1;
200 int index;
201 unsigned char old_sha1[20];
202 unsigned char new_sha1[20];
203 char ref_name[FLEX_ARRAY]; /* more */
206 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
207 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
209 static void report_message(const char *prefix, const char *err, va_list params)
211 int sz = strlen(prefix);
212 char msg[4096];
214 strncpy(msg, prefix, sz);
215 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
216 if (sz > (sizeof(msg) - 1))
217 sz = sizeof(msg) - 1;
218 msg[sz++] = '\n';
220 if (use_sideband)
221 send_sideband(1, 2, msg, sz, use_sideband);
222 else
223 xwrite(2, msg, sz);
226 static void rp_warning(const char *err, ...)
228 va_list params;
229 va_start(params, err);
230 report_message("warning: ", err, params);
231 va_end(params);
234 static void rp_error(const char *err, ...)
236 va_list params;
237 va_start(params, err);
238 report_message("error: ", err, params);
239 va_end(params);
242 static int copy_to_sideband(int in, int out, void *arg)
244 char data[128];
245 while (1) {
246 ssize_t sz = xread(in, data, sizeof(data));
247 if (sz <= 0)
248 break;
249 send_sideband(1, 2, data, sz, use_sideband);
251 close(in);
252 return 0;
255 typedef int (*feed_fn)(void *, const char **, size_t *);
256 static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
258 struct child_process proc;
259 struct async muxer;
260 const char *argv[2];
261 int code;
263 argv[0] = find_hook(hook_name);
264 if (!argv[0])
265 return 0;
267 argv[1] = NULL;
269 memset(&proc, 0, sizeof(proc));
270 proc.argv = argv;
271 proc.in = -1;
272 proc.stdout_to_stderr = 1;
274 if (use_sideband) {
275 memset(&muxer, 0, sizeof(muxer));
276 muxer.proc = copy_to_sideband;
277 muxer.in = -1;
278 code = start_async(&muxer);
279 if (code)
280 return code;
281 proc.err = muxer.in;
284 code = start_command(&proc);
285 if (code) {
286 if (use_sideband)
287 finish_async(&muxer);
288 return code;
291 while (1) {
292 const char *buf;
293 size_t n;
294 if (feed(feed_state, &buf, &n))
295 break;
296 if (write_in_full(proc.in, buf, n) != n)
297 break;
299 close(proc.in);
300 if (use_sideband)
301 finish_async(&muxer);
302 return finish_command(&proc);
305 struct receive_hook_feed_state {
306 struct command *cmd;
307 int skip_broken;
308 struct strbuf buf;
311 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
313 struct receive_hook_feed_state *state = state_;
314 struct command *cmd = state->cmd;
316 while (cmd &&
317 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
318 cmd = cmd->next;
319 if (!cmd)
320 return -1; /* EOF */
321 strbuf_reset(&state->buf);
322 strbuf_addf(&state->buf, "%s %s %s\n",
323 sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
324 cmd->ref_name);
325 state->cmd = cmd->next;
326 if (bufp) {
327 *bufp = state->buf.buf;
328 *sizep = state->buf.len;
330 return 0;
333 static int run_receive_hook(struct command *commands, const char *hook_name,
334 int skip_broken)
336 struct receive_hook_feed_state state;
337 int status;
339 strbuf_init(&state.buf, 0);
340 state.cmd = commands;
341 state.skip_broken = skip_broken;
342 if (feed_receive_hook(&state, NULL, NULL))
343 return 0;
344 state.cmd = commands;
345 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
346 strbuf_release(&state.buf);
347 return status;
350 static int run_update_hook(struct command *cmd)
352 const char *argv[5];
353 struct child_process proc;
354 int code;
356 argv[0] = find_hook("update");
357 if (!argv[0])
358 return 0;
360 argv[1] = cmd->ref_name;
361 argv[2] = sha1_to_hex(cmd->old_sha1);
362 argv[3] = sha1_to_hex(cmd->new_sha1);
363 argv[4] = NULL;
365 memset(&proc, 0, sizeof(proc));
366 proc.no_stdin = 1;
367 proc.stdout_to_stderr = 1;
368 proc.err = use_sideband ? -1 : 0;
369 proc.argv = argv;
371 code = start_command(&proc);
372 if (code)
373 return code;
374 if (use_sideband)
375 copy_to_sideband(proc.err, -1, NULL);
376 return finish_command(&proc);
379 static int is_ref_checked_out(const char *ref)
381 if (is_bare_repository())
382 return 0;
384 if (!head_name)
385 return 0;
386 return !strcmp(head_name, ref);
389 static char *refuse_unconfigured_deny_msg[] = {
390 "By default, updating the current branch in a non-bare repository",
391 "is denied, because it will make the index and work tree inconsistent",
392 "with what you pushed, and will require 'git reset --hard' to match",
393 "the work tree to HEAD.",
395 "You can set 'receive.denyCurrentBranch' configuration variable to",
396 "'ignore' or 'warn' in the remote repository to allow pushing into",
397 "its current branch; however, this is not recommended unless you",
398 "arranged to update its work tree to match what you pushed in some",
399 "other way.",
401 "To squelch this message and still keep the default behaviour, set",
402 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
405 static void refuse_unconfigured_deny(void)
407 int i;
408 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
409 rp_error("%s", refuse_unconfigured_deny_msg[i]);
412 static char *refuse_unconfigured_deny_delete_current_msg[] = {
413 "By default, deleting the current branch is denied, because the next",
414 "'git clone' won't result in any file checked out, causing confusion.",
416 "You can set 'receive.denyDeleteCurrent' configuration variable to",
417 "'warn' or 'ignore' in the remote repository to allow deleting the",
418 "current branch, with or without a warning message.",
420 "To squelch this message, you can set it to 'refuse'."
423 static void refuse_unconfigured_deny_delete_current(void)
425 int i;
426 for (i = 0;
427 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
428 i++)
429 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
432 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]);
433 static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
435 static struct lock_file shallow_lock;
436 struct sha1_array extra = SHA1_ARRAY_INIT;
437 const char *alt_file;
438 uint32_t mask = 1 << (cmd->index % 32);
439 int i;
441 trace_printf_key(&trace_shallow,
442 "shallow: update_shallow_ref %s\n", cmd->ref_name);
443 for (i = 0; i < si->shallow->nr; i++)
444 if (si->used_shallow[i] &&
445 (si->used_shallow[i][cmd->index / 32] & mask) &&
446 !delayed_reachability_test(si, i))
447 sha1_array_append(&extra, si->shallow->sha1[i]);
449 setup_alternate_shallow(&shallow_lock, &alt_file, &extra);
450 if (check_shallow_connected(command_singleton_iterator,
451 0, cmd, alt_file)) {
452 rollback_lock_file(&shallow_lock);
453 sha1_array_clear(&extra);
454 return -1;
457 commit_lock_file(&shallow_lock);
460 * Make sure setup_alternate_shallow() for the next ref does
461 * not lose these new roots..
463 for (i = 0; i < extra.nr; i++)
464 register_shallow(extra.sha1[i]);
466 si->shallow_ref[cmd->index] = 0;
467 sha1_array_clear(&extra);
468 return 0;
471 static const char *update(struct command *cmd, struct shallow_info *si)
473 const char *name = cmd->ref_name;
474 struct strbuf namespaced_name_buf = STRBUF_INIT;
475 const char *namespaced_name;
476 unsigned char *old_sha1 = cmd->old_sha1;
477 unsigned char *new_sha1 = cmd->new_sha1;
478 struct ref_lock *lock;
480 /* only refs/... are allowed */
481 if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
482 rp_error("refusing to create funny ref '%s' remotely", name);
483 return "funny refname";
486 strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
487 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
489 if (is_ref_checked_out(namespaced_name)) {
490 switch (deny_current_branch) {
491 case DENY_IGNORE:
492 break;
493 case DENY_WARN:
494 rp_warning("updating the current branch");
495 break;
496 case DENY_REFUSE:
497 case DENY_UNCONFIGURED:
498 rp_error("refusing to update checked out branch: %s", name);
499 if (deny_current_branch == DENY_UNCONFIGURED)
500 refuse_unconfigured_deny();
501 return "branch is currently checked out";
505 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
506 error("unpack should have generated %s, "
507 "but I can't find it!", sha1_to_hex(new_sha1));
508 return "bad pack";
511 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
512 if (deny_deletes && starts_with(name, "refs/heads/")) {
513 rp_error("denying ref deletion for %s", name);
514 return "deletion prohibited";
517 if (!strcmp(namespaced_name, head_name)) {
518 switch (deny_delete_current) {
519 case DENY_IGNORE:
520 break;
521 case DENY_WARN:
522 rp_warning("deleting the current branch");
523 break;
524 case DENY_REFUSE:
525 case DENY_UNCONFIGURED:
526 if (deny_delete_current == DENY_UNCONFIGURED)
527 refuse_unconfigured_deny_delete_current();
528 rp_error("refusing to delete the current branch: %s", name);
529 return "deletion of the current branch prohibited";
534 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
535 !is_null_sha1(old_sha1) &&
536 starts_with(name, "refs/heads/")) {
537 struct object *old_object, *new_object;
538 struct commit *old_commit, *new_commit;
540 old_object = parse_object(old_sha1);
541 new_object = parse_object(new_sha1);
543 if (!old_object || !new_object ||
544 old_object->type != OBJ_COMMIT ||
545 new_object->type != OBJ_COMMIT) {
546 error("bad sha1 objects for %s", name);
547 return "bad ref";
549 old_commit = (struct commit *)old_object;
550 new_commit = (struct commit *)new_object;
551 if (!in_merge_bases(old_commit, new_commit)) {
552 rp_error("denying non-fast-forward %s"
553 " (you should pull first)", name);
554 return "non-fast-forward";
557 if (run_update_hook(cmd)) {
558 rp_error("hook declined to update %s", name);
559 return "hook declined";
562 if (is_null_sha1(new_sha1)) {
563 if (!parse_object(old_sha1)) {
564 old_sha1 = NULL;
565 if (ref_exists(name)) {
566 rp_warning("Allowing deletion of corrupt ref.");
567 } else {
568 rp_warning("Deleting a non-existent ref.");
569 cmd->did_not_exist = 1;
572 if (delete_ref(namespaced_name, old_sha1, 0)) {
573 rp_error("failed to delete %s", name);
574 return "failed to delete";
576 return NULL; /* good */
578 else {
579 if (shallow_update && si->shallow_ref[cmd->index] &&
580 update_shallow_ref(cmd, si))
581 return "shallow error";
583 lock = lock_any_ref_for_update(namespaced_name, old_sha1,
584 0, NULL);
585 if (!lock) {
586 rp_error("failed to lock %s", name);
587 return "failed to lock";
589 if (write_ref_sha1(lock, new_sha1, "push")) {
590 return "failed to write"; /* error() already called */
592 return NULL; /* good */
596 static void run_update_post_hook(struct command *commands)
598 struct command *cmd;
599 int argc;
600 const char **argv;
601 struct child_process proc;
602 char *hook;
604 hook = find_hook("post-update");
605 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
606 if (cmd->error_string || cmd->did_not_exist)
607 continue;
608 argc++;
610 if (!argc || !hook)
611 return;
613 argv = xmalloc(sizeof(*argv) * (2 + argc));
614 argv[0] = hook;
616 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
617 if (cmd->error_string || cmd->did_not_exist)
618 continue;
619 argv[argc] = xstrdup(cmd->ref_name);
620 argc++;
622 argv[argc] = NULL;
624 memset(&proc, 0, sizeof(proc));
625 proc.no_stdin = 1;
626 proc.stdout_to_stderr = 1;
627 proc.err = use_sideband ? -1 : 0;
628 proc.argv = argv;
630 if (!start_command(&proc)) {
631 if (use_sideband)
632 copy_to_sideband(proc.err, -1, NULL);
633 finish_command(&proc);
637 static void check_aliased_update(struct command *cmd, struct string_list *list)
639 struct strbuf buf = STRBUF_INIT;
640 const char *dst_name;
641 struct string_list_item *item;
642 struct command *dst_cmd;
643 unsigned char sha1[20];
644 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
645 int flag;
647 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
648 dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
649 strbuf_release(&buf);
651 if (!(flag & REF_ISSYMREF))
652 return;
654 dst_name = strip_namespace(dst_name);
655 if (!dst_name) {
656 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
657 cmd->skip_update = 1;
658 cmd->error_string = "broken symref";
659 return;
662 if ((item = string_list_lookup(list, dst_name)) == NULL)
663 return;
665 cmd->skip_update = 1;
667 dst_cmd = (struct command *) item->util;
669 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
670 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
671 return;
673 dst_cmd->skip_update = 1;
675 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
676 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
677 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
678 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
679 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
680 " its target '%s' (%s..%s)",
681 cmd->ref_name, cmd_oldh, cmd_newh,
682 dst_cmd->ref_name, dst_oldh, dst_newh);
684 cmd->error_string = dst_cmd->error_string =
685 "inconsistent aliased update";
688 static void check_aliased_updates(struct command *commands)
690 struct command *cmd;
691 struct string_list ref_list = STRING_LIST_INIT_NODUP;
693 for (cmd = commands; cmd; cmd = cmd->next) {
694 struct string_list_item *item =
695 string_list_append(&ref_list, cmd->ref_name);
696 item->util = (void *)cmd;
698 sort_string_list(&ref_list);
700 for (cmd = commands; cmd; cmd = cmd->next) {
701 if (!cmd->error_string)
702 check_aliased_update(cmd, &ref_list);
705 string_list_clear(&ref_list, 0);
708 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
710 struct command **cmd_list = cb_data;
711 struct command *cmd = *cmd_list;
713 if (!cmd || is_null_sha1(cmd->new_sha1))
714 return -1; /* end of list */
715 *cmd_list = NULL; /* this returns only one */
716 hashcpy(sha1, cmd->new_sha1);
717 return 0;
720 static void set_connectivity_errors(struct command *commands,
721 struct shallow_info *si)
723 struct command *cmd;
725 for (cmd = commands; cmd; cmd = cmd->next) {
726 struct command *singleton = cmd;
727 if (shallow_update && si->shallow_ref[cmd->index])
728 /* to be checked in update_shallow_ref() */
729 continue;
730 if (!check_everything_connected(command_singleton_iterator,
731 0, &singleton))
732 continue;
733 cmd->error_string = "missing necessary objects";
737 struct iterate_data {
738 struct command *cmds;
739 struct shallow_info *si;
742 static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
744 struct iterate_data *data = cb_data;
745 struct command **cmd_list = &data->cmds;
746 struct command *cmd = *cmd_list;
748 for (; cmd; cmd = cmd->next) {
749 if (shallow_update && data->si->shallow_ref[cmd->index])
750 /* to be checked in update_shallow_ref() */
751 continue;
752 if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
753 hashcpy(sha1, cmd->new_sha1);
754 *cmd_list = cmd->next;
755 return 0;
758 *cmd_list = NULL;
759 return -1; /* end of list */
762 static void reject_updates_to_hidden(struct command *commands)
764 struct command *cmd;
766 for (cmd = commands; cmd; cmd = cmd->next) {
767 if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
768 continue;
769 if (is_null_sha1(cmd->new_sha1))
770 cmd->error_string = "deny deleting a hidden ref";
771 else
772 cmd->error_string = "deny updating a hidden ref";
776 static void execute_commands(struct command *commands,
777 const char *unpacker_error,
778 struct shallow_info *si)
780 int checked_connectivity;
781 struct command *cmd;
782 unsigned char sha1[20];
783 struct iterate_data data;
785 if (unpacker_error) {
786 for (cmd = commands; cmd; cmd = cmd->next)
787 cmd->error_string = "unpacker error";
788 return;
791 data.cmds = commands;
792 data.si = si;
793 if (check_everything_connected(iterate_receive_command_list, 0, &data))
794 set_connectivity_errors(commands, si);
796 reject_updates_to_hidden(commands);
798 if (run_receive_hook(commands, "pre-receive", 0)) {
799 for (cmd = commands; cmd; cmd = cmd->next) {
800 if (!cmd->error_string)
801 cmd->error_string = "pre-receive hook declined";
803 return;
806 check_aliased_updates(commands);
808 free(head_name_to_free);
809 head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
811 checked_connectivity = 1;
812 for (cmd = commands; cmd; cmd = cmd->next) {
813 if (cmd->error_string)
814 continue;
816 if (cmd->skip_update)
817 continue;
819 cmd->error_string = update(cmd, si);
820 if (shallow_update && !cmd->error_string &&
821 si->shallow_ref[cmd->index]) {
822 error("BUG: connectivity check has not been run on ref %s",
823 cmd->ref_name);
824 checked_connectivity = 0;
828 if (shallow_update && !checked_connectivity)
829 error("BUG: run 'git fsck' for safety.\n"
830 "If there are errors, try to remove "
831 "the reported refs above");
834 static struct command *read_head_info(struct sha1_array *shallow)
836 struct command *commands = NULL;
837 struct command **p = &commands;
838 for (;;) {
839 char *line;
840 unsigned char old_sha1[20], new_sha1[20];
841 struct command *cmd;
842 char *refname;
843 int len, reflen, linelen;
845 line = packet_read_line(0, &len);
846 if (!line)
847 break;
849 if (len == 48 && starts_with(line, "shallow ")) {
850 unsigned char sha1[20];
851 if (get_sha1_hex(line + 8, sha1))
852 die("protocol error: expected shallow sha, got '%s'",
853 line + 8);
854 sha1_array_append(shallow, sha1);
855 continue;
858 linelen = strlen(line);
859 if (linelen < len) {
860 const char *feature_list = line + linelen + 1;
861 if (parse_feature_request(feature_list, "report-status"))
862 report_status = 1;
863 if (parse_feature_request(feature_list, "side-band-64k"))
864 use_sideband = LARGE_PACKET_MAX;
865 if (parse_feature_request(feature_list, "quiet"))
866 quiet = 1;
869 if (linelen < 83 ||
870 line[40] != ' ' ||
871 line[81] != ' ' ||
872 get_sha1_hex(line, old_sha1) ||
873 get_sha1_hex(line + 41, new_sha1))
874 die("protocol error: expected old/new/ref, got '%s'",
875 line);
877 refname = line + 82;
878 reflen = linelen - 82;
879 cmd = xcalloc(1, sizeof(struct command) + reflen + 1);
880 hashcpy(cmd->old_sha1, old_sha1);
881 hashcpy(cmd->new_sha1, new_sha1);
882 memcpy(cmd->ref_name, refname, reflen);
883 cmd->ref_name[reflen] = '\0';
884 *p = cmd;
885 p = &cmd->next;
887 return commands;
890 static const char *parse_pack_header(struct pack_header *hdr)
892 switch (read_pack_header(0, hdr)) {
893 case PH_ERROR_EOF:
894 return "eof before pack header was fully read";
896 case PH_ERROR_PACK_SIGNATURE:
897 return "protocol error (pack signature mismatch detected)";
899 case PH_ERROR_PROTOCOL:
900 return "protocol error (pack version unsupported)";
902 default:
903 return "unknown error in parse_pack_header";
905 case 0:
906 return NULL;
910 static const char *pack_lockfile;
912 static const char *unpack(int err_fd, struct shallow_info *si)
914 struct pack_header hdr;
915 struct argv_array av = ARGV_ARRAY_INIT;
916 const char *hdr_err;
917 int status;
918 char hdr_arg[38];
919 struct child_process child;
920 int fsck_objects = (receive_fsck_objects >= 0
921 ? receive_fsck_objects
922 : transfer_fsck_objects >= 0
923 ? transfer_fsck_objects
924 : 0);
926 hdr_err = parse_pack_header(&hdr);
927 if (hdr_err) {
928 if (err_fd > 0)
929 close(err_fd);
930 return hdr_err;
932 snprintf(hdr_arg, sizeof(hdr_arg),
933 "--pack_header=%"PRIu32",%"PRIu32,
934 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
936 if (si->nr_ours || si->nr_theirs) {
937 alt_shallow_file = setup_temporary_shallow(si->shallow);
938 argv_array_pushl(&av, "--shallow-file", alt_shallow_file, NULL);
941 memset(&child, 0, sizeof(child));
942 if (ntohl(hdr.hdr_entries) < unpack_limit) {
943 argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
944 if (quiet)
945 argv_array_push(&av, "-q");
946 if (fsck_objects)
947 argv_array_push(&av, "--strict");
948 child.argv = av.argv;
949 child.no_stdout = 1;
950 child.err = err_fd;
951 child.git_cmd = 1;
952 status = run_command(&child);
953 if (status)
954 return "unpack-objects abnormal exit";
955 } else {
956 int s;
957 char keep_arg[256];
959 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
960 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
961 strcpy(keep_arg + s, "localhost");
963 argv_array_pushl(&av, "index-pack",
964 "--stdin", hdr_arg, keep_arg, NULL);
965 if (fsck_objects)
966 argv_array_push(&av, "--strict");
967 if (fix_thin)
968 argv_array_push(&av, "--fix-thin");
969 child.argv = av.argv;
970 child.out = -1;
971 child.err = err_fd;
972 child.git_cmd = 1;
973 status = start_command(&child);
974 if (status)
975 return "index-pack fork failed";
976 pack_lockfile = index_pack_lockfile(child.out);
977 close(child.out);
978 status = finish_command(&child);
979 if (status)
980 return "index-pack abnormal exit";
981 reprepare_packed_git();
983 return NULL;
986 static const char *unpack_with_sideband(struct shallow_info *si)
988 struct async muxer;
989 const char *ret;
991 if (!use_sideband)
992 return unpack(0, si);
994 memset(&muxer, 0, sizeof(muxer));
995 muxer.proc = copy_to_sideband;
996 muxer.in = -1;
997 if (start_async(&muxer))
998 return NULL;
1000 ret = unpack(muxer.in, si);
1002 finish_async(&muxer);
1003 return ret;
1006 static void prepare_shallow_update(struct command *commands,
1007 struct shallow_info *si)
1009 int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1011 si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
1012 si->shallow->nr);
1013 assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
1015 si->need_reachability_test =
1016 xcalloc(si->shallow->nr, sizeof(*si->need_reachability_test));
1017 si->reachable =
1018 xcalloc(si->shallow->nr, sizeof(*si->reachable));
1019 si->shallow_ref = xcalloc(si->ref->nr, sizeof(*si->shallow_ref));
1021 for (i = 0; i < si->nr_ours; i++)
1022 si->need_reachability_test[si->ours[i]] = 1;
1024 for (i = 0; i < si->shallow->nr; i++) {
1025 if (!si->used_shallow[i])
1026 continue;
1027 for (j = 0; j < bitmap_size; j++) {
1028 if (!si->used_shallow[i][j])
1029 continue;
1030 si->need_reachability_test[i]++;
1031 for (k = 0; k < 32; k++)
1032 if (si->used_shallow[i][j] & (1 << k))
1033 si->shallow_ref[j * 32 + k]++;
1037 * true for those associated with some refs and belong
1038 * in "ours" list aka "step 7 not done yet"
1040 si->need_reachability_test[i] =
1041 si->need_reachability_test[i] > 1;
1045 * keep hooks happy by forcing a temporary shallow file via
1046 * env variable because we can't add --shallow-file to every
1047 * command. check_everything_connected() will be done with
1048 * true .git/shallow though.
1050 setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
1053 static void update_shallow_info(struct command *commands,
1054 struct shallow_info *si,
1055 struct sha1_array *ref)
1057 struct command *cmd;
1058 int *ref_status;
1059 remove_nonexistent_theirs_shallow(si);
1060 if (!si->nr_ours && !si->nr_theirs) {
1061 shallow_update = 0;
1062 return;
1065 for (cmd = commands; cmd; cmd = cmd->next) {
1066 if (is_null_sha1(cmd->new_sha1))
1067 continue;
1068 sha1_array_append(ref, cmd->new_sha1);
1069 cmd->index = ref->nr - 1;
1071 si->ref = ref;
1073 if (shallow_update) {
1074 prepare_shallow_update(commands, si);
1075 return;
1078 ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
1079 assign_shallow_commits_to_refs(si, NULL, ref_status);
1080 for (cmd = commands; cmd; cmd = cmd->next) {
1081 if (is_null_sha1(cmd->new_sha1))
1082 continue;
1083 if (ref_status[cmd->index]) {
1084 cmd->error_string = "shallow update not allowed";
1085 cmd->skip_update = 1;
1088 free(ref_status);
1091 static void report(struct command *commands, const char *unpack_status)
1093 struct command *cmd;
1094 struct strbuf buf = STRBUF_INIT;
1096 packet_buf_write(&buf, "unpack %s\n",
1097 unpack_status ? unpack_status : "ok");
1098 for (cmd = commands; cmd; cmd = cmd->next) {
1099 if (!cmd->error_string)
1100 packet_buf_write(&buf, "ok %s\n",
1101 cmd->ref_name);
1102 else
1103 packet_buf_write(&buf, "ng %s %s\n",
1104 cmd->ref_name, cmd->error_string);
1106 packet_buf_flush(&buf);
1108 if (use_sideband)
1109 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
1110 else
1111 write_or_die(1, buf.buf, buf.len);
1112 strbuf_release(&buf);
1115 static int delete_only(struct command *commands)
1117 struct command *cmd;
1118 for (cmd = commands; cmd; cmd = cmd->next) {
1119 if (!is_null_sha1(cmd->new_sha1))
1120 return 0;
1122 return 1;
1125 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
1127 int advertise_refs = 0;
1128 int stateless_rpc = 0;
1129 int i;
1130 const char *dir = NULL;
1131 struct command *commands;
1132 struct sha1_array shallow = SHA1_ARRAY_INIT;
1133 struct sha1_array ref = SHA1_ARRAY_INIT;
1134 struct shallow_info si;
1136 packet_trace_identity("receive-pack");
1138 argv++;
1139 for (i = 1; i < argc; i++) {
1140 const char *arg = *argv++;
1142 if (*arg == '-') {
1143 if (!strcmp(arg, "--quiet")) {
1144 quiet = 1;
1145 continue;
1148 if (!strcmp(arg, "--advertise-refs")) {
1149 advertise_refs = 1;
1150 continue;
1152 if (!strcmp(arg, "--stateless-rpc")) {
1153 stateless_rpc = 1;
1154 continue;
1156 if (!strcmp(arg, "--reject-thin-pack-for-testing")) {
1157 fix_thin = 0;
1158 continue;
1161 usage(receive_pack_usage);
1163 if (dir)
1164 usage(receive_pack_usage);
1165 dir = arg;
1167 if (!dir)
1168 usage(receive_pack_usage);
1170 setup_path();
1172 if (!enter_repo(dir, 0))
1173 die("'%s' does not appear to be a git repository", dir);
1175 git_config(receive_pack_config, NULL);
1177 if (0 <= transfer_unpack_limit)
1178 unpack_limit = transfer_unpack_limit;
1179 else if (0 <= receive_unpack_limit)
1180 unpack_limit = receive_unpack_limit;
1182 if (advertise_refs || !stateless_rpc) {
1183 write_head_info();
1185 if (advertise_refs)
1186 return 0;
1188 if ((commands = read_head_info(&shallow)) != NULL) {
1189 const char *unpack_status = NULL;
1191 prepare_shallow_info(&si, &shallow);
1192 if (!si.nr_ours && !si.nr_theirs)
1193 shallow_update = 0;
1194 if (!delete_only(commands)) {
1195 unpack_status = unpack_with_sideband(&si);
1196 update_shallow_info(commands, &si, &ref);
1198 execute_commands(commands, unpack_status, &si);
1199 if (pack_lockfile)
1200 unlink_or_warn(pack_lockfile);
1201 if (report_status)
1202 report(commands, unpack_status);
1203 run_receive_hook(commands, "post-receive", 1);
1204 run_update_post_hook(commands);
1205 if (auto_gc) {
1206 const char *argv_gc_auto[] = {
1207 "gc", "--auto", "--quiet", NULL,
1209 int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR;
1210 run_command_v_opt(argv_gc_auto, opt);
1212 if (auto_update_server_info)
1213 update_server_info(0);
1214 clear_shallow_info(&si);
1216 if (use_sideband)
1217 packet_flush(1);
1218 sha1_array_clear(&shallow);
1219 sha1_array_clear(&ref);
1220 return 0;