msysGit-based Git for Windows 1.x was superseded by Git for Windows 2.x
[git/mingw/4msysgit.git] / builtin / receive-pack.c
blob2fb7ed1defd17d42e0901c5004b9ab51fef9b82c
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(&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, UPDATE_REFS_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 if (cmd->error_string || cmd->did_not_exist)
672 continue;
673 argv[argc] = xstrdup(cmd->ref_name);
674 argc++;
676 argv[argc] = NULL;
678 memset(&proc, 0, sizeof(proc));
679 proc.no_stdin = 1;
680 proc.stdout_to_stderr = 1;
681 proc.err = use_sideband ? -1 : 0;
682 proc.argv = argv;
684 if (!start_command(&proc)) {
685 if (use_sideband)
686 copy_to_sideband(proc.err, -1, NULL);
687 finish_command(&proc);
691 static void check_aliased_update(struct command *cmd, struct string_list *list)
693 struct strbuf buf = STRBUF_INIT;
694 const char *dst_name;
695 struct string_list_item *item;
696 struct command *dst_cmd;
697 unsigned char sha1[20];
698 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
699 int flag;
701 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
702 dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
703 strbuf_release(&buf);
705 if (!(flag & REF_ISSYMREF))
706 return;
708 dst_name = strip_namespace(dst_name);
709 if (!dst_name) {
710 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
711 cmd->skip_update = 1;
712 cmd->error_string = "broken symref";
713 return;
716 if ((item = string_list_lookup(list, dst_name)) == NULL)
717 return;
719 cmd->skip_update = 1;
721 dst_cmd = (struct command *) item->util;
723 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
724 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
725 return;
727 dst_cmd->skip_update = 1;
729 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
730 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
731 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
732 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
733 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
734 " its target '%s' (%s..%s)",
735 cmd->ref_name, cmd_oldh, cmd_newh,
736 dst_cmd->ref_name, dst_oldh, dst_newh);
738 cmd->error_string = dst_cmd->error_string =
739 "inconsistent aliased update";
742 static void check_aliased_updates(struct command *commands)
744 struct command *cmd;
745 struct string_list ref_list = STRING_LIST_INIT_NODUP;
747 for (cmd = commands; cmd; cmd = cmd->next) {
748 struct string_list_item *item =
749 string_list_append(&ref_list, cmd->ref_name);
750 item->util = (void *)cmd;
752 sort_string_list(&ref_list);
754 for (cmd = commands; cmd; cmd = cmd->next) {
755 if (!cmd->error_string)
756 check_aliased_update(cmd, &ref_list);
759 string_list_clear(&ref_list, 0);
762 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
764 struct command **cmd_list = cb_data;
765 struct command *cmd = *cmd_list;
767 if (!cmd || is_null_sha1(cmd->new_sha1))
768 return -1; /* end of list */
769 *cmd_list = NULL; /* this returns only one */
770 hashcpy(sha1, cmd->new_sha1);
771 return 0;
774 static void set_connectivity_errors(struct command *commands,
775 struct shallow_info *si)
777 struct command *cmd;
779 for (cmd = commands; cmd; cmd = cmd->next) {
780 struct command *singleton = cmd;
781 if (shallow_update && si->shallow_ref[cmd->index])
782 /* to be checked in update_shallow_ref() */
783 continue;
784 if (!check_everything_connected(command_singleton_iterator,
785 0, &singleton))
786 continue;
787 cmd->error_string = "missing necessary objects";
791 struct iterate_data {
792 struct command *cmds;
793 struct shallow_info *si;
796 static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
798 struct iterate_data *data = cb_data;
799 struct command **cmd_list = &data->cmds;
800 struct command *cmd = *cmd_list;
802 for (; cmd; cmd = cmd->next) {
803 if (shallow_update && data->si->shallow_ref[cmd->index])
804 /* to be checked in update_shallow_ref() */
805 continue;
806 if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
807 hashcpy(sha1, cmd->new_sha1);
808 *cmd_list = cmd->next;
809 return 0;
812 *cmd_list = NULL;
813 return -1; /* end of list */
816 static void reject_updates_to_hidden(struct command *commands)
818 struct command *cmd;
820 for (cmd = commands; cmd; cmd = cmd->next) {
821 if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
822 continue;
823 if (is_null_sha1(cmd->new_sha1))
824 cmd->error_string = "deny deleting a hidden ref";
825 else
826 cmd->error_string = "deny updating a hidden ref";
830 static void execute_commands(struct command *commands,
831 const char *unpacker_error,
832 struct shallow_info *si)
834 int checked_connectivity;
835 struct command *cmd;
836 unsigned char sha1[20];
837 struct iterate_data data;
839 if (unpacker_error) {
840 for (cmd = commands; cmd; cmd = cmd->next)
841 cmd->error_string = "unpacker error";
842 return;
845 data.cmds = commands;
846 data.si = si;
847 if (check_everything_connected(iterate_receive_command_list, 0, &data))
848 set_connectivity_errors(commands, si);
850 reject_updates_to_hidden(commands);
852 if (run_receive_hook(commands, "pre-receive", 0)) {
853 for (cmd = commands; cmd; cmd = cmd->next) {
854 if (!cmd->error_string)
855 cmd->error_string = "pre-receive hook declined";
857 return;
860 check_aliased_updates(commands);
862 free(head_name_to_free);
863 head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
865 checked_connectivity = 1;
866 for (cmd = commands; cmd; cmd = cmd->next) {
867 if (cmd->error_string)
868 continue;
870 if (cmd->skip_update)
871 continue;
873 cmd->error_string = update(cmd, si);
874 if (shallow_update && !cmd->error_string &&
875 si->shallow_ref[cmd->index]) {
876 error("BUG: connectivity check has not been run on ref %s",
877 cmd->ref_name);
878 checked_connectivity = 0;
882 if (shallow_update && !checked_connectivity)
883 error("BUG: run 'git fsck' for safety.\n"
884 "If there are errors, try to remove "
885 "the reported refs above");
888 static struct command *read_head_info(struct sha1_array *shallow)
890 struct command *commands = NULL;
891 struct command **p = &commands;
892 for (;;) {
893 char *line;
894 unsigned char old_sha1[20], new_sha1[20];
895 struct command *cmd;
896 char *refname;
897 int len, reflen;
899 line = packet_read_line(0, &len);
900 if (!line)
901 break;
903 if (len == 48 && starts_with(line, "shallow ")) {
904 if (get_sha1_hex(line + 8, old_sha1))
905 die("protocol error: expected shallow sha, got '%s'", line + 8);
906 sha1_array_append(shallow, old_sha1);
907 continue;
910 if (len < 83 ||
911 line[40] != ' ' ||
912 line[81] != ' ' ||
913 get_sha1_hex(line, old_sha1) ||
914 get_sha1_hex(line + 41, new_sha1))
915 die("protocol error: expected old/new/ref, got '%s'",
916 line);
918 refname = line + 82;
919 reflen = strlen(refname);
920 if (reflen + 82 < len) {
921 const char *feature_list = refname + reflen + 1;
922 if (parse_feature_request(feature_list, "report-status"))
923 report_status = 1;
924 if (parse_feature_request(feature_list, "side-band-64k"))
925 use_sideband = LARGE_PACKET_MAX;
926 if (parse_feature_request(feature_list, "quiet"))
927 quiet = 1;
929 cmd = xcalloc(1, sizeof(struct command) + len - 80);
930 hashcpy(cmd->old_sha1, old_sha1);
931 hashcpy(cmd->new_sha1, new_sha1);
932 memcpy(cmd->ref_name, line + 82, len - 81);
933 *p = cmd;
934 p = &cmd->next;
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;