Makefile: do not depend on curl-config
[git/mingw/4msysgit.git] / builtin / receive-pack.c
blob83fbececf57da476e106d7a016079a783f4381cc
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,
26 DENY_UPDATE_INSTEAD,
27 DENY_DETACH_INSTEAD,
30 static int deny_deletes;
31 static int deny_non_fast_forwards;
32 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
33 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
34 static int receive_fsck_objects = -1;
35 static int transfer_fsck_objects = -1;
36 static int receive_unpack_limit = -1;
37 static int transfer_unpack_limit = -1;
38 static int unpack_limit = 100;
39 static int report_status;
40 static int use_sideband;
41 static int quiet;
42 static int prefer_ofs_delta = 1;
43 static int auto_update_server_info;
44 static int auto_gc = 1;
45 static int fix_thin = 1;
46 static const char *head_name;
47 static void *head_name_to_free;
48 static int sent_capabilities;
49 static int shallow_update;
50 static const char *alt_shallow_file;
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 if (value && !strcasecmp(value, "updateinstead"))
106 deny_current_branch = DENY_UPDATE_INSTEAD;
107 else if (value && !strcasecmp(value, "detachinstead"))
108 deny_current_branch = DENY_DETACH_INSTEAD;
109 else
110 deny_current_branch = parse_deny_action(var, value);
111 return 0;
114 if (strcmp(var, "receive.denydeletecurrent") == 0) {
115 deny_delete_current = parse_deny_action(var, value);
116 return 0;
119 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
120 prefer_ofs_delta = git_config_bool(var, value);
121 return 0;
124 if (strcmp(var, "receive.updateserverinfo") == 0) {
125 auto_update_server_info = git_config_bool(var, value);
126 return 0;
129 if (strcmp(var, "receive.autogc") == 0) {
130 auto_gc = git_config_bool(var, value);
131 return 0;
134 if (strcmp(var, "receive.shallowupdate") == 0) {
135 shallow_update = git_config_bool(var, value);
136 return 0;
139 return git_default_config(var, value, cb);
142 static void show_ref(const char *path, const unsigned char *sha1)
144 if (ref_is_hidden(path))
145 return;
147 if (sent_capabilities)
148 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
149 else
150 packet_write(1, "%s %s%c%s%s agent=%s\n",
151 sha1_to_hex(sha1), path, 0,
152 " report-status delete-refs side-band-64k quiet",
153 prefer_ofs_delta ? " ofs-delta" : "",
154 git_user_agent_sanitized());
155 sent_capabilities = 1;
158 static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused)
160 path = strip_namespace(path);
162 * Advertise refs outside our current namespace as ".have"
163 * refs, so that the client can use them to minimize data
164 * transfer but will otherwise ignore them. This happens to
165 * cover ".have" that are thrown in by add_one_alternate_ref()
166 * to mark histories that are complete in our alternates as
167 * well.
169 if (!path)
170 path = ".have";
171 show_ref(path, sha1);
172 return 0;
175 static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
177 show_ref(".have", sha1);
180 static void collect_one_alternate_ref(const struct ref *ref, void *data)
182 struct sha1_array *sa = data;
183 sha1_array_append(sa, ref->old_sha1);
186 static void write_head_info(void)
188 struct sha1_array sa = SHA1_ARRAY_INIT;
189 for_each_alternate_ref(collect_one_alternate_ref, &sa);
190 sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
191 sha1_array_clear(&sa);
192 for_each_ref(show_ref_cb, NULL);
193 if (!sent_capabilities)
194 show_ref("capabilities^{}", null_sha1);
196 advertise_shallow_grafts(1);
198 /* EOF */
199 packet_flush(1);
202 struct command {
203 struct command *next;
204 const char *error_string;
205 unsigned int skip_update:1,
206 did_not_exist:1;
207 int index;
208 unsigned char old_sha1[20];
209 unsigned char new_sha1[20];
210 char ref_name[FLEX_ARRAY]; /* more */
213 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
214 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
216 static void report_message(const char *prefix, const char *err, va_list params)
218 int sz = strlen(prefix);
219 char msg[4096];
221 strncpy(msg, prefix, sz);
222 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
223 if (sz > (sizeof(msg) - 1))
224 sz = sizeof(msg) - 1;
225 msg[sz++] = '\n';
227 if (use_sideband)
228 send_sideband(1, 2, msg, sz, use_sideband);
229 else
230 xwrite(2, msg, sz);
233 static void rp_warning(const char *err, ...)
235 va_list params;
236 va_start(params, err);
237 report_message("warning: ", err, params);
238 va_end(params);
241 static void rp_error(const char *err, ...)
243 va_list params;
244 va_start(params, err);
245 report_message("error: ", err, params);
246 va_end(params);
249 static int copy_to_sideband(int in, int out, void *arg)
251 char data[128];
252 while (1) {
253 ssize_t sz = xread(in, data, sizeof(data));
254 if (sz <= 0)
255 break;
256 send_sideband(1, 2, data, sz, use_sideband);
258 close(in);
259 return 0;
262 typedef int (*feed_fn)(void *, const char **, size_t *);
263 static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
265 struct child_process proc;
266 struct async muxer;
267 const char *argv[2];
268 int code;
270 argv[0] = find_hook(hook_name);
271 if (!argv[0])
272 return 0;
274 argv[1] = NULL;
276 memset(&proc, 0, sizeof(proc));
277 proc.argv = argv;
278 proc.in = -1;
279 proc.stdout_to_stderr = 1;
281 if (use_sideband) {
282 memset(&muxer, 0, sizeof(muxer));
283 muxer.proc = copy_to_sideband;
284 muxer.in = -1;
285 code = start_async(&muxer);
286 if (code)
287 return code;
288 proc.err = muxer.in;
291 code = start_command(&proc);
292 if (code) {
293 if (use_sideband)
294 finish_async(&muxer);
295 return code;
298 while (1) {
299 const char *buf;
300 size_t n;
301 if (feed(feed_state, &buf, &n))
302 break;
303 if (write_in_full(proc.in, buf, n) != n)
304 break;
306 close(proc.in);
307 if (use_sideband)
308 finish_async(&muxer);
309 return finish_command(&proc);
312 struct receive_hook_feed_state {
313 struct command *cmd;
314 int skip_broken;
315 struct strbuf buf;
318 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
320 struct receive_hook_feed_state *state = state_;
321 struct command *cmd = state->cmd;
323 while (cmd &&
324 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
325 cmd = cmd->next;
326 if (!cmd)
327 return -1; /* EOF */
328 strbuf_reset(&state->buf);
329 strbuf_addf(&state->buf, "%s %s %s\n",
330 sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
331 cmd->ref_name);
332 state->cmd = cmd->next;
333 if (bufp) {
334 *bufp = state->buf.buf;
335 *sizep = state->buf.len;
337 return 0;
340 static int run_receive_hook(struct command *commands, const char *hook_name,
341 int skip_broken)
343 struct receive_hook_feed_state state;
344 int status;
346 strbuf_init(&state.buf, 0);
347 state.cmd = commands;
348 state.skip_broken = skip_broken;
349 if (feed_receive_hook(&state, NULL, NULL))
350 return 0;
351 state.cmd = commands;
352 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
353 strbuf_release(&state.buf);
354 return status;
357 static int run_update_hook(struct command *cmd)
359 const char *argv[5];
360 struct child_process proc;
361 int code;
363 argv[0] = find_hook("update");
364 if (!argv[0])
365 return 0;
367 argv[1] = cmd->ref_name;
368 argv[2] = sha1_to_hex(cmd->old_sha1);
369 argv[3] = sha1_to_hex(cmd->new_sha1);
370 argv[4] = NULL;
372 memset(&proc, 0, sizeof(proc));
373 proc.no_stdin = 1;
374 proc.stdout_to_stderr = 1;
375 proc.err = use_sideband ? -1 : 0;
376 proc.argv = argv;
378 code = start_command(&proc);
379 if (code)
380 return code;
381 if (use_sideband)
382 copy_to_sideband(proc.err, -1, NULL);
383 return finish_command(&proc);
386 static int is_ref_checked_out(const char *ref)
388 if (is_bare_repository())
389 return 0;
391 if (!head_name)
392 return 0;
393 return !strcmp(head_name, ref);
396 static char *refuse_unconfigured_deny_msg[] = {
397 "By default, updating the current branch in a non-bare repository",
398 "is denied, because it will make the index and work tree inconsistent",
399 "with what you pushed, and will require 'git reset --hard' to match",
400 "the work tree to HEAD.",
402 "You can set 'receive.denyCurrentBranch' configuration variable to",
403 "'ignore' or 'warn' in the remote repository to allow pushing into",
404 "its current branch; however, this is not recommended unless you",
405 "arranged to update its work tree to match what you pushed in some",
406 "other way.",
408 "To squelch this message and still keep the default behaviour, set",
409 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
412 static void refuse_unconfigured_deny(void)
414 int i;
415 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
416 rp_error("%s", refuse_unconfigured_deny_msg[i]);
419 static char *refuse_unconfigured_deny_delete_current_msg[] = {
420 "By default, deleting the current branch is denied, because the next",
421 "'git clone' won't result in any file checked out, causing confusion.",
423 "You can set 'receive.denyDeleteCurrent' configuration variable to",
424 "'warn' or 'ignore' in the remote repository to allow deleting the",
425 "current branch, with or without a warning message.",
427 "To squelch this message, you can set it to 'refuse'."
430 static void refuse_unconfigured_deny_delete_current(void)
432 int i;
433 for (i = 0;
434 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
435 i++)
436 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
439 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]);
440 static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
442 static struct lock_file shallow_lock;
443 struct sha1_array extra = SHA1_ARRAY_INIT;
444 const char *alt_file;
445 uint32_t mask = 1 << (cmd->index % 32);
446 int i;
448 trace_printf_key("GIT_TRACE_SHALLOW",
449 "shallow: update_shallow_ref %s\n", cmd->ref_name);
450 for (i = 0; i < si->shallow->nr; i++)
451 if (si->used_shallow[i] &&
452 (si->used_shallow[i][cmd->index / 32] & mask) &&
453 !delayed_reachability_test(si, i))
454 sha1_array_append(&extra, si->shallow->sha1[i]);
456 setup_alternate_shallow(&shallow_lock, &alt_file, &extra);
457 if (check_shallow_connected(command_singleton_iterator,
458 0, cmd, alt_file)) {
459 rollback_lock_file(&shallow_lock);
460 sha1_array_clear(&extra);
461 return -1;
464 commit_lock_file(&shallow_lock);
467 * Make sure setup_alternate_shallow() for the next ref does
468 * not lose these new roots..
470 for (i = 0; i < extra.nr; i++)
471 register_shallow(extra.sha1[i]);
473 si->shallow_ref[cmd->index] = 0;
474 sha1_array_clear(&extra);
475 return 0;
478 static void merge_worktree(unsigned char *sha1)
480 const char *update_refresh[] = {
481 "update-index", "--ignore-submodules", "--refresh", NULL
483 const char *read_tree[] = {
484 "read-tree", "-u", "-m", sha1_to_hex(sha1), NULL
486 struct child_process child;
487 struct strbuf git_env = STRBUF_INIT;
488 const char *env[2];
490 if (is_bare_repository())
491 die ("denyCurrentBranch = updateInstead needs a worktree");
493 strbuf_addf(&git_env, "GIT_DIR=%s", absolute_path(get_git_dir()));
494 env[0] = git_env.buf;
495 env[1] = NULL;
497 memset(&child, 0, sizeof(child));
498 child.argv = update_refresh;
499 child.env = env;
500 child.dir = git_work_tree_cfg ? git_work_tree_cfg : "..";
501 child.stdout_to_stderr = 1;
502 child.git_cmd = 1;
503 if (run_command(&child))
504 die ("Could not refresh the index");
506 child.argv = read_tree;
507 child.no_stdin = 1;
508 child.no_stdout = 1;
509 child.stdout_to_stderr = 0;
510 if (run_command(&child))
511 die ("Could not merge working tree with new HEAD. Good luck.");
513 strbuf_release(&git_env);
516 static const char *update(struct command *cmd, struct shallow_info *si)
518 const char *name = cmd->ref_name;
519 struct strbuf namespaced_name_buf = STRBUF_INIT;
520 const char *namespaced_name;
521 unsigned char *old_sha1 = cmd->old_sha1;
522 unsigned char *new_sha1 = cmd->new_sha1;
523 struct ref_lock *lock;
525 /* only refs/... are allowed */
526 if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
527 rp_error("refusing to create funny ref '%s' remotely", name);
528 return "funny refname";
531 strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
532 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
534 if (is_ref_checked_out(namespaced_name)) {
535 switch (deny_current_branch) {
536 case DENY_IGNORE:
537 break;
538 case DENY_WARN:
539 rp_warning("updating the current branch");
540 break;
541 case DENY_REFUSE:
542 case DENY_UNCONFIGURED:
543 rp_error("refusing to update checked out branch: %s", name);
544 if (deny_current_branch == DENY_UNCONFIGURED)
545 refuse_unconfigured_deny();
546 return "branch is currently checked out";
547 case DENY_UPDATE_INSTEAD:
548 merge_worktree(new_sha1);
549 break;
550 case DENY_DETACH_INSTEAD:
551 update_ref("push into current branch (detach)", "HEAD",
552 old_sha1, NULL, REF_NODEREF, DIE_ON_ERR);
553 break;
557 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
558 error("unpack should have generated %s, "
559 "but I can't find it!", sha1_to_hex(new_sha1));
560 return "bad pack";
563 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
564 if (deny_deletes && starts_with(name, "refs/heads/")) {
565 rp_error("denying ref deletion for %s", name);
566 return "deletion prohibited";
569 if (!strcmp(namespaced_name, head_name)) {
570 switch (deny_delete_current) {
571 case DENY_IGNORE:
572 break;
573 case DENY_WARN:
574 rp_warning("deleting the current branch");
575 break;
576 case DENY_REFUSE:
577 case DENY_UNCONFIGURED:
578 if (deny_delete_current == DENY_UNCONFIGURED)
579 refuse_unconfigured_deny_delete_current();
580 rp_error("refusing to delete the current branch: %s", name);
581 return "deletion of the current branch prohibited";
582 default:
583 die ("Invalid denyDeleteCurrent setting");
588 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
589 !is_null_sha1(old_sha1) &&
590 starts_with(name, "refs/heads/")) {
591 struct object *old_object, *new_object;
592 struct commit *old_commit, *new_commit;
594 old_object = parse_object(old_sha1);
595 new_object = parse_object(new_sha1);
597 if (!old_object || !new_object ||
598 old_object->type != OBJ_COMMIT ||
599 new_object->type != OBJ_COMMIT) {
600 error("bad sha1 objects for %s", name);
601 return "bad ref";
603 old_commit = (struct commit *)old_object;
604 new_commit = (struct commit *)new_object;
605 if (!in_merge_bases(old_commit, new_commit)) {
606 rp_error("denying non-fast-forward %s"
607 " (you should pull first)", name);
608 return "non-fast-forward";
611 if (run_update_hook(cmd)) {
612 rp_error("hook declined to update %s", name);
613 return "hook declined";
616 if (is_null_sha1(new_sha1)) {
617 if (!parse_object(old_sha1)) {
618 old_sha1 = NULL;
619 if (ref_exists(name)) {
620 rp_warning("Allowing deletion of corrupt ref.");
621 } else {
622 rp_warning("Deleting a non-existent ref.");
623 cmd->did_not_exist = 1;
626 if (delete_ref(namespaced_name, old_sha1, 0)) {
627 rp_error("failed to delete %s", name);
628 return "failed to delete";
630 return NULL; /* good */
632 else {
633 if (shallow_update && si->shallow_ref[cmd->index] &&
634 update_shallow_ref(cmd, si))
635 return "shallow error";
637 lock = lock_any_ref_for_update(namespaced_name, old_sha1,
638 0, NULL);
639 if (!lock) {
640 rp_error("failed to lock %s", name);
641 return "failed to lock";
643 if (write_ref_sha1(lock, new_sha1, "push")) {
644 return "failed to write"; /* error() already called */
646 return NULL; /* good */
650 static void run_update_post_hook(struct command *commands)
652 struct command *cmd;
653 int argc;
654 const char **argv;
655 struct child_process proc;
656 char *hook;
658 hook = find_hook("post-update");
659 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
660 if (cmd->error_string || cmd->did_not_exist)
661 continue;
662 argc++;
664 if (!argc || !hook)
665 return;
667 argv = xmalloc(sizeof(*argv) * (2 + argc));
668 argv[0] = hook;
670 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
671 char *p;
672 if (cmd->error_string || cmd->did_not_exist)
673 continue;
674 p = xmalloc(strlen(cmd->ref_name) + 1);
675 strcpy(p, cmd->ref_name);
676 argv[argc] = p;
677 argc++;
679 argv[argc] = NULL;
681 memset(&proc, 0, sizeof(proc));
682 proc.no_stdin = 1;
683 proc.stdout_to_stderr = 1;
684 proc.err = use_sideband ? -1 : 0;
685 proc.argv = argv;
687 if (!start_command(&proc)) {
688 if (use_sideband)
689 copy_to_sideband(proc.err, -1, NULL);
690 finish_command(&proc);
694 static void check_aliased_update(struct command *cmd, struct string_list *list)
696 struct strbuf buf = STRBUF_INIT;
697 const char *dst_name;
698 struct string_list_item *item;
699 struct command *dst_cmd;
700 unsigned char sha1[20];
701 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
702 int flag;
704 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
705 dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
706 strbuf_release(&buf);
708 if (!(flag & REF_ISSYMREF))
709 return;
711 dst_name = strip_namespace(dst_name);
712 if (!dst_name) {
713 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
714 cmd->skip_update = 1;
715 cmd->error_string = "broken symref";
716 return;
719 if ((item = string_list_lookup(list, dst_name)) == NULL)
720 return;
722 cmd->skip_update = 1;
724 dst_cmd = (struct command *) item->util;
726 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
727 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
728 return;
730 dst_cmd->skip_update = 1;
732 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
733 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
734 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
735 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
736 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
737 " its target '%s' (%s..%s)",
738 cmd->ref_name, cmd_oldh, cmd_newh,
739 dst_cmd->ref_name, dst_oldh, dst_newh);
741 cmd->error_string = dst_cmd->error_string =
742 "inconsistent aliased update";
745 static void check_aliased_updates(struct command *commands)
747 struct command *cmd;
748 struct string_list ref_list = STRING_LIST_INIT_NODUP;
750 for (cmd = commands; cmd; cmd = cmd->next) {
751 struct string_list_item *item =
752 string_list_append(&ref_list, cmd->ref_name);
753 item->util = (void *)cmd;
755 sort_string_list(&ref_list);
757 for (cmd = commands; cmd; cmd = cmd->next) {
758 if (!cmd->error_string)
759 check_aliased_update(cmd, &ref_list);
762 string_list_clear(&ref_list, 0);
765 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
767 struct command **cmd_list = cb_data;
768 struct command *cmd = *cmd_list;
770 if (!cmd || is_null_sha1(cmd->new_sha1))
771 return -1; /* end of list */
772 *cmd_list = NULL; /* this returns only one */
773 hashcpy(sha1, cmd->new_sha1);
774 return 0;
777 static void set_connectivity_errors(struct command *commands,
778 struct shallow_info *si)
780 struct command *cmd;
782 for (cmd = commands; cmd; cmd = cmd->next) {
783 struct command *singleton = cmd;
784 if (shallow_update && si->shallow_ref[cmd->index])
785 /* to be checked in update_shallow_ref() */
786 continue;
787 if (!check_everything_connected(command_singleton_iterator,
788 0, &singleton))
789 continue;
790 cmd->error_string = "missing necessary objects";
794 struct iterate_data {
795 struct command *cmds;
796 struct shallow_info *si;
799 static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
801 struct iterate_data *data = cb_data;
802 struct command **cmd_list = &data->cmds;
803 struct command *cmd = *cmd_list;
805 for (; cmd; cmd = cmd->next) {
806 if (shallow_update && data->si->shallow_ref[cmd->index])
807 /* to be checked in update_shallow_ref() */
808 continue;
809 if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
810 hashcpy(sha1, cmd->new_sha1);
811 *cmd_list = cmd->next;
812 return 0;
815 *cmd_list = NULL;
816 return -1; /* end of list */
819 static void reject_updates_to_hidden(struct command *commands)
821 struct command *cmd;
823 for (cmd = commands; cmd; cmd = cmd->next) {
824 if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
825 continue;
826 if (is_null_sha1(cmd->new_sha1))
827 cmd->error_string = "deny deleting a hidden ref";
828 else
829 cmd->error_string = "deny updating a hidden ref";
833 static void execute_commands(struct command *commands,
834 const char *unpacker_error,
835 struct shallow_info *si)
837 int checked_connectivity;
838 struct command *cmd;
839 unsigned char sha1[20];
840 struct iterate_data data;
842 if (unpacker_error) {
843 for (cmd = commands; cmd; cmd = cmd->next)
844 cmd->error_string = "unpacker error";
845 return;
848 data.cmds = commands;
849 data.si = si;
850 if (check_everything_connected(iterate_receive_command_list, 0, &data))
851 set_connectivity_errors(commands, si);
853 reject_updates_to_hidden(commands);
855 if (run_receive_hook(commands, "pre-receive", 0)) {
856 for (cmd = commands; cmd; cmd = cmd->next) {
857 if (!cmd->error_string)
858 cmd->error_string = "pre-receive hook declined";
860 return;
863 check_aliased_updates(commands);
865 free(head_name_to_free);
866 head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
868 checked_connectivity = 1;
869 for (cmd = commands; cmd; cmd = cmd->next) {
870 if (cmd->error_string)
871 continue;
873 if (cmd->skip_update)
874 continue;
876 cmd->error_string = update(cmd, si);
877 if (shallow_update && !cmd->error_string &&
878 si->shallow_ref[cmd->index]) {
879 error("BUG: connectivity check has not been run on ref %s",
880 cmd->ref_name);
881 checked_connectivity = 0;
885 if (shallow_update && !checked_connectivity)
886 error("BUG: run 'git fsck' for safety.\n"
887 "If there are errors, try to remove "
888 "the reported refs above");
891 static struct command *read_head_info(struct sha1_array *shallow)
893 struct command *commands = NULL;
894 struct command **p = &commands;
895 for (;;) {
896 char *line;
897 unsigned char old_sha1[20], new_sha1[20];
898 struct command *cmd;
899 char *refname;
900 int len, reflen;
902 line = packet_read_line(0, &len);
903 if (!line)
904 break;
906 if (len == 48 && starts_with(line, "shallow ")) {
907 if (get_sha1_hex(line + 8, old_sha1))
908 die("protocol error: expected shallow sha, got '%s'", line + 8);
909 sha1_array_append(shallow, old_sha1);
910 continue;
913 if (len < 83 ||
914 line[40] != ' ' ||
915 line[81] != ' ' ||
916 get_sha1_hex(line, old_sha1) ||
917 get_sha1_hex(line + 41, new_sha1))
918 die("protocol error: expected old/new/ref, got '%s'",
919 line);
921 refname = line + 82;
922 reflen = strlen(refname);
923 if (reflen + 82 < len) {
924 const char *feature_list = refname + reflen + 1;
925 if (parse_feature_request(feature_list, "report-status"))
926 report_status = 1;
927 if (parse_feature_request(feature_list, "side-band-64k"))
928 use_sideband = LARGE_PACKET_MAX;
929 if (parse_feature_request(feature_list, "quiet"))
930 quiet = 1;
932 cmd = xcalloc(1, sizeof(struct command) + len - 80);
933 hashcpy(cmd->old_sha1, old_sha1);
934 hashcpy(cmd->new_sha1, new_sha1);
935 memcpy(cmd->ref_name, line + 82, len - 81);
936 *p = cmd;
937 p = &cmd->next;
939 return commands;
942 static const char *parse_pack_header(struct pack_header *hdr)
944 switch (read_pack_header(0, hdr)) {
945 case PH_ERROR_EOF:
946 return "eof before pack header was fully read";
948 case PH_ERROR_PACK_SIGNATURE:
949 return "protocol error (pack signature mismatch detected)";
951 case PH_ERROR_PROTOCOL:
952 return "protocol error (pack version unsupported)";
954 default:
955 return "unknown error in parse_pack_header";
957 case 0:
958 return NULL;
962 static const char *pack_lockfile;
964 static const char *unpack(int err_fd, struct shallow_info *si)
966 struct pack_header hdr;
967 struct argv_array av = ARGV_ARRAY_INIT;
968 const char *hdr_err;
969 int status;
970 char hdr_arg[38];
971 struct child_process child;
972 int fsck_objects = (receive_fsck_objects >= 0
973 ? receive_fsck_objects
974 : transfer_fsck_objects >= 0
975 ? transfer_fsck_objects
976 : 0);
978 hdr_err = parse_pack_header(&hdr);
979 if (hdr_err) {
980 if (err_fd > 0)
981 close(err_fd);
982 return hdr_err;
984 snprintf(hdr_arg, sizeof(hdr_arg),
985 "--pack_header=%"PRIu32",%"PRIu32,
986 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
988 if (si->nr_ours || si->nr_theirs) {
989 alt_shallow_file = setup_temporary_shallow(si->shallow);
990 argv_array_pushl(&av, "--shallow-file", alt_shallow_file, NULL);
993 memset(&child, 0, sizeof(child));
994 if (ntohl(hdr.hdr_entries) < unpack_limit) {
995 argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
996 if (quiet)
997 argv_array_push(&av, "-q");
998 if (fsck_objects)
999 argv_array_push(&av, "--strict");
1000 child.argv = av.argv;
1001 child.no_stdout = 1;
1002 child.err = err_fd;
1003 child.git_cmd = 1;
1004 status = run_command(&child);
1005 if (status)
1006 return "unpack-objects abnormal exit";
1007 } else {
1008 int s;
1009 char keep_arg[256];
1011 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
1012 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
1013 strcpy(keep_arg + s, "localhost");
1015 argv_array_pushl(&av, "index-pack",
1016 "--stdin", hdr_arg, keep_arg, NULL);
1017 if (fsck_objects)
1018 argv_array_push(&av, "--strict");
1019 if (fix_thin)
1020 argv_array_push(&av, "--fix-thin");
1021 child.argv = av.argv;
1022 child.out = -1;
1023 child.err = err_fd;
1024 child.git_cmd = 1;
1025 status = start_command(&child);
1026 if (status)
1027 return "index-pack fork failed";
1028 pack_lockfile = index_pack_lockfile(child.out);
1029 close(child.out);
1030 status = finish_command(&child);
1031 if (status)
1032 return "index-pack abnormal exit";
1033 reprepare_packed_git();
1035 return NULL;
1038 static const char *unpack_with_sideband(struct shallow_info *si)
1040 struct async muxer;
1041 const char *ret;
1043 if (!use_sideband)
1044 return unpack(0, si);
1046 memset(&muxer, 0, sizeof(muxer));
1047 muxer.proc = copy_to_sideband;
1048 muxer.in = -1;
1049 if (start_async(&muxer))
1050 return NULL;
1052 ret = unpack(muxer.in, si);
1054 finish_async(&muxer);
1055 return ret;
1058 static void prepare_shallow_update(struct command *commands,
1059 struct shallow_info *si)
1061 int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1063 si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
1064 si->shallow->nr);
1065 assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
1067 si->need_reachability_test =
1068 xcalloc(si->shallow->nr, sizeof(*si->need_reachability_test));
1069 si->reachable =
1070 xcalloc(si->shallow->nr, sizeof(*si->reachable));
1071 si->shallow_ref = xcalloc(si->ref->nr, sizeof(*si->shallow_ref));
1073 for (i = 0; i < si->nr_ours; i++)
1074 si->need_reachability_test[si->ours[i]] = 1;
1076 for (i = 0; i < si->shallow->nr; i++) {
1077 if (!si->used_shallow[i])
1078 continue;
1079 for (j = 0; j < bitmap_size; j++) {
1080 if (!si->used_shallow[i][j])
1081 continue;
1082 si->need_reachability_test[i]++;
1083 for (k = 0; k < 32; k++)
1084 if (si->used_shallow[i][j] & (1 << k))
1085 si->shallow_ref[j * 32 + k]++;
1089 * true for those associated with some refs and belong
1090 * in "ours" list aka "step 7 not done yet"
1092 si->need_reachability_test[i] =
1093 si->need_reachability_test[i] > 1;
1097 * keep hooks happy by forcing a temporary shallow file via
1098 * env variable because we can't add --shallow-file to every
1099 * command. check_everything_connected() will be done with
1100 * true .git/shallow though.
1102 setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
1105 static void update_shallow_info(struct command *commands,
1106 struct shallow_info *si,
1107 struct sha1_array *ref)
1109 struct command *cmd;
1110 int *ref_status;
1111 remove_nonexistent_theirs_shallow(si);
1112 if (!si->nr_ours && !si->nr_theirs) {
1113 shallow_update = 0;
1114 return;
1117 for (cmd = commands; cmd; cmd = cmd->next) {
1118 if (is_null_sha1(cmd->new_sha1))
1119 continue;
1120 sha1_array_append(ref, cmd->new_sha1);
1121 cmd->index = ref->nr - 1;
1123 si->ref = ref;
1125 if (shallow_update) {
1126 prepare_shallow_update(commands, si);
1127 return;
1130 ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
1131 assign_shallow_commits_to_refs(si, NULL, ref_status);
1132 for (cmd = commands; cmd; cmd = cmd->next) {
1133 if (is_null_sha1(cmd->new_sha1))
1134 continue;
1135 if (ref_status[cmd->index]) {
1136 cmd->error_string = "shallow update not allowed";
1137 cmd->skip_update = 1;
1140 free(ref_status);
1143 static void report(struct command *commands, const char *unpack_status)
1145 struct command *cmd;
1146 struct strbuf buf = STRBUF_INIT;
1148 packet_buf_write(&buf, "unpack %s\n",
1149 unpack_status ? unpack_status : "ok");
1150 for (cmd = commands; cmd; cmd = cmd->next) {
1151 if (!cmd->error_string)
1152 packet_buf_write(&buf, "ok %s\n",
1153 cmd->ref_name);
1154 else
1155 packet_buf_write(&buf, "ng %s %s\n",
1156 cmd->ref_name, cmd->error_string);
1158 packet_buf_flush(&buf);
1160 if (use_sideband)
1161 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
1162 else
1163 write_or_die(1, buf.buf, buf.len);
1164 strbuf_release(&buf);
1167 static int delete_only(struct command *commands)
1169 struct command *cmd;
1170 for (cmd = commands; cmd; cmd = cmd->next) {
1171 if (!is_null_sha1(cmd->new_sha1))
1172 return 0;
1174 return 1;
1177 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
1179 int advertise_refs = 0;
1180 int stateless_rpc = 0;
1181 int i;
1182 char *dir = NULL;
1183 struct command *commands;
1184 struct sha1_array shallow = SHA1_ARRAY_INIT;
1185 struct sha1_array ref = SHA1_ARRAY_INIT;
1186 struct shallow_info si;
1188 packet_trace_identity("receive-pack");
1190 argv++;
1191 for (i = 1; i < argc; i++) {
1192 const char *arg = *argv++;
1194 if (*arg == '-') {
1195 if (!strcmp(arg, "--quiet")) {
1196 quiet = 1;
1197 continue;
1200 if (!strcmp(arg, "--advertise-refs")) {
1201 advertise_refs = 1;
1202 continue;
1204 if (!strcmp(arg, "--stateless-rpc")) {
1205 stateless_rpc = 1;
1206 continue;
1208 if (!strcmp(arg, "--reject-thin-pack-for-testing")) {
1209 fix_thin = 0;
1210 continue;
1213 usage(receive_pack_usage);
1215 if (dir)
1216 usage(receive_pack_usage);
1217 dir = xstrdup(arg);
1219 if (!dir)
1220 usage(receive_pack_usage);
1222 setup_path();
1224 if (!enter_repo(dir, 0))
1225 die("'%s' does not appear to be a git repository", dir);
1227 git_config(receive_pack_config, NULL);
1229 if (0 <= transfer_unpack_limit)
1230 unpack_limit = transfer_unpack_limit;
1231 else if (0 <= receive_unpack_limit)
1232 unpack_limit = receive_unpack_limit;
1234 if (advertise_refs || !stateless_rpc) {
1235 write_head_info();
1237 if (advertise_refs)
1238 return 0;
1240 if ((commands = read_head_info(&shallow)) != NULL) {
1241 const char *unpack_status = NULL;
1243 prepare_shallow_info(&si, &shallow);
1244 if (!si.nr_ours && !si.nr_theirs)
1245 shallow_update = 0;
1246 if (!delete_only(commands)) {
1247 unpack_status = unpack_with_sideband(&si);
1248 update_shallow_info(commands, &si, &ref);
1250 execute_commands(commands, unpack_status, &si);
1251 if (pack_lockfile)
1252 unlink_or_warn(pack_lockfile);
1253 if (report_status)
1254 report(commands, unpack_status);
1255 run_receive_hook(commands, "post-receive", 1);
1256 run_update_post_hook(commands);
1257 if (auto_gc) {
1258 const char *argv_gc_auto[] = {
1259 "gc", "--auto", "--quiet", NULL,
1261 int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR;
1262 run_command_v_opt(argv_gc_auto, opt);
1264 if (auto_update_server_info)
1265 update_server_info(0);
1266 clear_shallow_info(&si);
1268 if (use_sideband)
1269 packet_flush(1);
1270 sha1_array_clear(&shallow);
1271 sha1_array_clear(&ref);
1272 return 0;