receive/send-pack: support pushing from a shallow clone
[git/mingw.git] / builtin / receive-pack.c
blobb9de2e8ff6292e1d38f6b145c087c75a9e2b8907
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 const char *alt_shallow_file;
49 static enum deny_action parse_deny_action(const char *var, const char *value)
51 if (value) {
52 if (!strcasecmp(value, "ignore"))
53 return DENY_IGNORE;
54 if (!strcasecmp(value, "warn"))
55 return DENY_WARN;
56 if (!strcasecmp(value, "refuse"))
57 return DENY_REFUSE;
59 if (git_config_bool(var, value))
60 return DENY_REFUSE;
61 return DENY_IGNORE;
64 static int receive_pack_config(const char *var, const char *value, void *cb)
66 int status = parse_hide_refs_config(var, value, "receive");
68 if (status)
69 return status;
71 if (strcmp(var, "receive.denydeletes") == 0) {
72 deny_deletes = git_config_bool(var, value);
73 return 0;
76 if (strcmp(var, "receive.denynonfastforwards") == 0) {
77 deny_non_fast_forwards = git_config_bool(var, value);
78 return 0;
81 if (strcmp(var, "receive.unpacklimit") == 0) {
82 receive_unpack_limit = git_config_int(var, value);
83 return 0;
86 if (strcmp(var, "transfer.unpacklimit") == 0) {
87 transfer_unpack_limit = git_config_int(var, value);
88 return 0;
91 if (strcmp(var, "receive.fsckobjects") == 0) {
92 receive_fsck_objects = git_config_bool(var, value);
93 return 0;
96 if (strcmp(var, "transfer.fsckobjects") == 0) {
97 transfer_fsck_objects = git_config_bool(var, value);
98 return 0;
101 if (!strcmp(var, "receive.denycurrentbranch")) {
102 deny_current_branch = parse_deny_action(var, value);
103 return 0;
106 if (strcmp(var, "receive.denydeletecurrent") == 0) {
107 deny_delete_current = parse_deny_action(var, value);
108 return 0;
111 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
112 prefer_ofs_delta = git_config_bool(var, value);
113 return 0;
116 if (strcmp(var, "receive.updateserverinfo") == 0) {
117 auto_update_server_info = git_config_bool(var, value);
118 return 0;
121 if (strcmp(var, "receive.autogc") == 0) {
122 auto_gc = git_config_bool(var, value);
123 return 0;
126 return git_default_config(var, value, cb);
129 static void show_ref(const char *path, const unsigned char *sha1)
131 if (ref_is_hidden(path))
132 return;
134 if (sent_capabilities)
135 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
136 else
137 packet_write(1, "%s %s%c%s%s agent=%s\n",
138 sha1_to_hex(sha1), path, 0,
139 " report-status delete-refs side-band-64k quiet",
140 prefer_ofs_delta ? " ofs-delta" : "",
141 git_user_agent_sanitized());
142 sent_capabilities = 1;
145 static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused)
147 path = strip_namespace(path);
149 * Advertise refs outside our current namespace as ".have"
150 * refs, so that the client can use them to minimize data
151 * transfer but will otherwise ignore them. This happens to
152 * cover ".have" that are thrown in by add_one_alternate_ref()
153 * to mark histories that are complete in our alternates as
154 * well.
156 if (!path)
157 path = ".have";
158 show_ref(path, sha1);
159 return 0;
162 static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
164 show_ref(".have", sha1);
167 static void collect_one_alternate_ref(const struct ref *ref, void *data)
169 struct sha1_array *sa = data;
170 sha1_array_append(sa, ref->old_sha1);
173 static void write_head_info(void)
175 struct sha1_array sa = SHA1_ARRAY_INIT;
176 for_each_alternate_ref(collect_one_alternate_ref, &sa);
177 sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
178 sha1_array_clear(&sa);
179 for_each_ref(show_ref_cb, NULL);
180 if (!sent_capabilities)
181 show_ref("capabilities^{}", null_sha1);
183 advertise_shallow_grafts(1);
185 /* EOF */
186 packet_flush(1);
189 struct command {
190 struct command *next;
191 const char *error_string;
192 unsigned int skip_update:1,
193 did_not_exist:1;
194 int index;
195 unsigned char old_sha1[20];
196 unsigned char new_sha1[20];
197 char ref_name[FLEX_ARRAY]; /* more */
200 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
201 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
203 static void report_message(const char *prefix, const char *err, va_list params)
205 int sz = strlen(prefix);
206 char msg[4096];
208 strncpy(msg, prefix, sz);
209 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
210 if (sz > (sizeof(msg) - 1))
211 sz = sizeof(msg) - 1;
212 msg[sz++] = '\n';
214 if (use_sideband)
215 send_sideband(1, 2, msg, sz, use_sideband);
216 else
217 xwrite(2, msg, sz);
220 static void rp_warning(const char *err, ...)
222 va_list params;
223 va_start(params, err);
224 report_message("warning: ", err, params);
225 va_end(params);
228 static void rp_error(const char *err, ...)
230 va_list params;
231 va_start(params, err);
232 report_message("error: ", err, params);
233 va_end(params);
236 static int copy_to_sideband(int in, int out, void *arg)
238 char data[128];
239 while (1) {
240 ssize_t sz = xread(in, data, sizeof(data));
241 if (sz <= 0)
242 break;
243 send_sideband(1, 2, data, sz, use_sideband);
245 close(in);
246 return 0;
249 typedef int (*feed_fn)(void *, const char **, size_t *);
250 static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
252 struct child_process proc;
253 struct async muxer;
254 const char *argv[2];
255 int code;
257 argv[0] = find_hook(hook_name);
258 if (!argv[0])
259 return 0;
261 argv[1] = NULL;
263 memset(&proc, 0, sizeof(proc));
264 proc.argv = argv;
265 proc.in = -1;
266 proc.stdout_to_stderr = 1;
268 if (use_sideband) {
269 memset(&muxer, 0, sizeof(muxer));
270 muxer.proc = copy_to_sideband;
271 muxer.in = -1;
272 code = start_async(&muxer);
273 if (code)
274 return code;
275 proc.err = muxer.in;
278 code = start_command(&proc);
279 if (code) {
280 if (use_sideband)
281 finish_async(&muxer);
282 return code;
285 while (1) {
286 const char *buf;
287 size_t n;
288 if (feed(feed_state, &buf, &n))
289 break;
290 if (write_in_full(proc.in, buf, n) != n)
291 break;
293 close(proc.in);
294 if (use_sideband)
295 finish_async(&muxer);
296 return finish_command(&proc);
299 struct receive_hook_feed_state {
300 struct command *cmd;
301 int skip_broken;
302 struct strbuf buf;
305 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
307 struct receive_hook_feed_state *state = state_;
308 struct command *cmd = state->cmd;
310 while (cmd &&
311 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
312 cmd = cmd->next;
313 if (!cmd)
314 return -1; /* EOF */
315 strbuf_reset(&state->buf);
316 strbuf_addf(&state->buf, "%s %s %s\n",
317 sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
318 cmd->ref_name);
319 state->cmd = cmd->next;
320 if (bufp) {
321 *bufp = state->buf.buf;
322 *sizep = state->buf.len;
324 return 0;
327 static int run_receive_hook(struct command *commands, const char *hook_name,
328 int skip_broken)
330 struct receive_hook_feed_state state;
331 int status;
333 strbuf_init(&state.buf, 0);
334 state.cmd = commands;
335 state.skip_broken = skip_broken;
336 if (feed_receive_hook(&state, NULL, NULL))
337 return 0;
338 state.cmd = commands;
339 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
340 strbuf_release(&state.buf);
341 return status;
344 static int run_update_hook(struct command *cmd)
346 const char *argv[5];
347 struct child_process proc;
348 int code;
350 argv[0] = find_hook("update");
351 if (!argv[0])
352 return 0;
354 argv[1] = cmd->ref_name;
355 argv[2] = sha1_to_hex(cmd->old_sha1);
356 argv[3] = sha1_to_hex(cmd->new_sha1);
357 argv[4] = NULL;
359 memset(&proc, 0, sizeof(proc));
360 proc.no_stdin = 1;
361 proc.stdout_to_stderr = 1;
362 proc.err = use_sideband ? -1 : 0;
363 proc.argv = argv;
365 code = start_command(&proc);
366 if (code)
367 return code;
368 if (use_sideband)
369 copy_to_sideband(proc.err, -1, NULL);
370 return finish_command(&proc);
373 static int is_ref_checked_out(const char *ref)
375 if (is_bare_repository())
376 return 0;
378 if (!head_name)
379 return 0;
380 return !strcmp(head_name, ref);
383 static char *refuse_unconfigured_deny_msg[] = {
384 "By default, updating the current branch in a non-bare repository",
385 "is denied, because it will make the index and work tree inconsistent",
386 "with what you pushed, and will require 'git reset --hard' to match",
387 "the work tree to HEAD.",
389 "You can set 'receive.denyCurrentBranch' configuration variable to",
390 "'ignore' or 'warn' in the remote repository to allow pushing into",
391 "its current branch; however, this is not recommended unless you",
392 "arranged to update its work tree to match what you pushed in some",
393 "other way.",
395 "To squelch this message and still keep the default behaviour, set",
396 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
399 static void refuse_unconfigured_deny(void)
401 int i;
402 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
403 rp_error("%s", refuse_unconfigured_deny_msg[i]);
406 static char *refuse_unconfigured_deny_delete_current_msg[] = {
407 "By default, deleting the current branch is denied, because the next",
408 "'git clone' won't result in any file checked out, causing confusion.",
410 "You can set 'receive.denyDeleteCurrent' configuration variable to",
411 "'warn' or 'ignore' in the remote repository to allow deleting the",
412 "current branch, with or without a warning message.",
414 "To squelch this message, you can set it to 'refuse'."
417 static void refuse_unconfigured_deny_delete_current(void)
419 int i;
420 for (i = 0;
421 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
422 i++)
423 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
426 static const char *update(struct command *cmd)
428 const char *name = cmd->ref_name;
429 struct strbuf namespaced_name_buf = STRBUF_INIT;
430 const char *namespaced_name;
431 unsigned char *old_sha1 = cmd->old_sha1;
432 unsigned char *new_sha1 = cmd->new_sha1;
433 struct ref_lock *lock;
435 /* only refs/... are allowed */
436 if (prefixcmp(name, "refs/") || check_refname_format(name + 5, 0)) {
437 rp_error("refusing to create funny ref '%s' remotely", name);
438 return "funny refname";
441 strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
442 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
444 if (is_ref_checked_out(namespaced_name)) {
445 switch (deny_current_branch) {
446 case DENY_IGNORE:
447 break;
448 case DENY_WARN:
449 rp_warning("updating the current branch");
450 break;
451 case DENY_REFUSE:
452 case DENY_UNCONFIGURED:
453 rp_error("refusing to update checked out branch: %s", name);
454 if (deny_current_branch == DENY_UNCONFIGURED)
455 refuse_unconfigured_deny();
456 return "branch is currently checked out";
460 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
461 error("unpack should have generated %s, "
462 "but I can't find it!", sha1_to_hex(new_sha1));
463 return "bad pack";
466 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
467 if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
468 rp_error("denying ref deletion for %s", name);
469 return "deletion prohibited";
472 if (!strcmp(namespaced_name, head_name)) {
473 switch (deny_delete_current) {
474 case DENY_IGNORE:
475 break;
476 case DENY_WARN:
477 rp_warning("deleting the current branch");
478 break;
479 case DENY_REFUSE:
480 case DENY_UNCONFIGURED:
481 if (deny_delete_current == DENY_UNCONFIGURED)
482 refuse_unconfigured_deny_delete_current();
483 rp_error("refusing to delete the current branch: %s", name);
484 return "deletion of the current branch prohibited";
489 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
490 !is_null_sha1(old_sha1) &&
491 !prefixcmp(name, "refs/heads/")) {
492 struct object *old_object, *new_object;
493 struct commit *old_commit, *new_commit;
495 old_object = parse_object(old_sha1);
496 new_object = parse_object(new_sha1);
498 if (!old_object || !new_object ||
499 old_object->type != OBJ_COMMIT ||
500 new_object->type != OBJ_COMMIT) {
501 error("bad sha1 objects for %s", name);
502 return "bad ref";
504 old_commit = (struct commit *)old_object;
505 new_commit = (struct commit *)new_object;
506 if (!in_merge_bases(old_commit, new_commit)) {
507 rp_error("denying non-fast-forward %s"
508 " (you should pull first)", name);
509 return "non-fast-forward";
512 if (run_update_hook(cmd)) {
513 rp_error("hook declined to update %s", name);
514 return "hook declined";
517 if (is_null_sha1(new_sha1)) {
518 if (!parse_object(old_sha1)) {
519 old_sha1 = NULL;
520 if (ref_exists(name)) {
521 rp_warning("Allowing deletion of corrupt ref.");
522 } else {
523 rp_warning("Deleting a non-existent ref.");
524 cmd->did_not_exist = 1;
527 if (delete_ref(namespaced_name, old_sha1, 0)) {
528 rp_error("failed to delete %s", name);
529 return "failed to delete";
531 return NULL; /* good */
533 else {
534 lock = lock_any_ref_for_update(namespaced_name, old_sha1,
535 0, NULL);
536 if (!lock) {
537 rp_error("failed to lock %s", name);
538 return "failed to lock";
540 if (write_ref_sha1(lock, new_sha1, "push")) {
541 return "failed to write"; /* error() already called */
543 return NULL; /* good */
547 static void run_update_post_hook(struct command *commands)
549 struct command *cmd;
550 int argc;
551 const char **argv;
552 struct child_process proc;
553 char *hook;
555 hook = find_hook("post-update");
556 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
557 if (cmd->error_string || cmd->did_not_exist)
558 continue;
559 argc++;
561 if (!argc || !hook)
562 return;
564 argv = xmalloc(sizeof(*argv) * (2 + argc));
565 argv[0] = hook;
567 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
568 char *p;
569 if (cmd->error_string || cmd->did_not_exist)
570 continue;
571 p = xmalloc(strlen(cmd->ref_name) + 1);
572 strcpy(p, cmd->ref_name);
573 argv[argc] = p;
574 argc++;
576 argv[argc] = NULL;
578 memset(&proc, 0, sizeof(proc));
579 proc.no_stdin = 1;
580 proc.stdout_to_stderr = 1;
581 proc.err = use_sideband ? -1 : 0;
582 proc.argv = argv;
584 if (!start_command(&proc)) {
585 if (use_sideband)
586 copy_to_sideband(proc.err, -1, NULL);
587 finish_command(&proc);
591 static void check_aliased_update(struct command *cmd, struct string_list *list)
593 struct strbuf buf = STRBUF_INIT;
594 const char *dst_name;
595 struct string_list_item *item;
596 struct command *dst_cmd;
597 unsigned char sha1[20];
598 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
599 int flag;
601 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
602 dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
603 strbuf_release(&buf);
605 if (!(flag & REF_ISSYMREF))
606 return;
608 dst_name = strip_namespace(dst_name);
609 if (!dst_name) {
610 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
611 cmd->skip_update = 1;
612 cmd->error_string = "broken symref";
613 return;
616 if ((item = string_list_lookup(list, dst_name)) == NULL)
617 return;
619 cmd->skip_update = 1;
621 dst_cmd = (struct command *) item->util;
623 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
624 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
625 return;
627 dst_cmd->skip_update = 1;
629 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
630 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
631 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
632 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
633 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
634 " its target '%s' (%s..%s)",
635 cmd->ref_name, cmd_oldh, cmd_newh,
636 dst_cmd->ref_name, dst_oldh, dst_newh);
638 cmd->error_string = dst_cmd->error_string =
639 "inconsistent aliased update";
642 static void check_aliased_updates(struct command *commands)
644 struct command *cmd;
645 struct string_list ref_list = STRING_LIST_INIT_NODUP;
647 for (cmd = commands; cmd; cmd = cmd->next) {
648 struct string_list_item *item =
649 string_list_append(&ref_list, cmd->ref_name);
650 item->util = (void *)cmd;
652 sort_string_list(&ref_list);
654 for (cmd = commands; cmd; cmd = cmd->next) {
655 if (!cmd->error_string)
656 check_aliased_update(cmd, &ref_list);
659 string_list_clear(&ref_list, 0);
662 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
664 struct command **cmd_list = cb_data;
665 struct command *cmd = *cmd_list;
667 if (!cmd || is_null_sha1(cmd->new_sha1))
668 return -1; /* end of list */
669 *cmd_list = NULL; /* this returns only one */
670 hashcpy(sha1, cmd->new_sha1);
671 return 0;
674 static void set_connectivity_errors(struct command *commands)
676 struct command *cmd;
678 for (cmd = commands; cmd; cmd = cmd->next) {
679 struct command *singleton = cmd;
680 if (!check_everything_connected(command_singleton_iterator,
681 0, &singleton))
682 continue;
683 cmd->error_string = "missing necessary objects";
687 static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
689 struct command **cmd_list = cb_data;
690 struct command *cmd = *cmd_list;
692 while (cmd) {
693 if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
694 hashcpy(sha1, cmd->new_sha1);
695 *cmd_list = cmd->next;
696 return 0;
698 cmd = cmd->next;
700 *cmd_list = NULL;
701 return -1; /* end of list */
704 static void reject_updates_to_hidden(struct command *commands)
706 struct command *cmd;
708 for (cmd = commands; cmd; cmd = cmd->next) {
709 if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
710 continue;
711 if (is_null_sha1(cmd->new_sha1))
712 cmd->error_string = "deny deleting a hidden ref";
713 else
714 cmd->error_string = "deny updating a hidden ref";
718 static void execute_commands(struct command *commands, const char *unpacker_error)
720 struct command *cmd;
721 unsigned char sha1[20];
723 if (unpacker_error) {
724 for (cmd = commands; cmd; cmd = cmd->next)
725 cmd->error_string = "unpacker error";
726 return;
729 cmd = commands;
730 if (check_everything_connected(iterate_receive_command_list,
731 0, &cmd))
732 set_connectivity_errors(commands);
734 reject_updates_to_hidden(commands);
736 if (run_receive_hook(commands, "pre-receive", 0)) {
737 for (cmd = commands; cmd; cmd = cmd->next) {
738 if (!cmd->error_string)
739 cmd->error_string = "pre-receive hook declined";
741 return;
744 check_aliased_updates(commands);
746 free(head_name_to_free);
747 head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
749 for (cmd = commands; cmd; cmd = cmd->next) {
750 if (cmd->error_string)
751 continue;
753 if (cmd->skip_update)
754 continue;
756 cmd->error_string = update(cmd);
760 static struct command *read_head_info(struct sha1_array *shallow)
762 struct command *commands = NULL;
763 struct command **p = &commands;
764 for (;;) {
765 char *line;
766 unsigned char old_sha1[20], new_sha1[20];
767 struct command *cmd;
768 char *refname;
769 int len, reflen;
771 line = packet_read_line(0, &len);
772 if (!line)
773 break;
775 if (len == 48 && !prefixcmp(line, "shallow ")) {
776 if (get_sha1_hex(line + 8, old_sha1))
777 die("protocol error: expected shallow sha, got '%s'", line + 8);
778 sha1_array_append(shallow, old_sha1);
779 continue;
782 if (len < 83 ||
783 line[40] != ' ' ||
784 line[81] != ' ' ||
785 get_sha1_hex(line, old_sha1) ||
786 get_sha1_hex(line + 41, new_sha1))
787 die("protocol error: expected old/new/ref, got '%s'",
788 line);
790 refname = line + 82;
791 reflen = strlen(refname);
792 if (reflen + 82 < len) {
793 const char *feature_list = refname + reflen + 1;
794 if (parse_feature_request(feature_list, "report-status"))
795 report_status = 1;
796 if (parse_feature_request(feature_list, "side-band-64k"))
797 use_sideband = LARGE_PACKET_MAX;
798 if (parse_feature_request(feature_list, "quiet"))
799 quiet = 1;
801 cmd = xcalloc(1, sizeof(struct command) + len - 80);
802 hashcpy(cmd->old_sha1, old_sha1);
803 hashcpy(cmd->new_sha1, new_sha1);
804 memcpy(cmd->ref_name, line + 82, len - 81);
805 *p = cmd;
806 p = &cmd->next;
808 return commands;
811 static const char *parse_pack_header(struct pack_header *hdr)
813 switch (read_pack_header(0, hdr)) {
814 case PH_ERROR_EOF:
815 return "eof before pack header was fully read";
817 case PH_ERROR_PACK_SIGNATURE:
818 return "protocol error (pack signature mismatch detected)";
820 case PH_ERROR_PROTOCOL:
821 return "protocol error (pack version unsupported)";
823 default:
824 return "unknown error in parse_pack_header";
826 case 0:
827 return NULL;
831 static const char *pack_lockfile;
833 static const char *unpack(int err_fd, struct shallow_info *si)
835 struct pack_header hdr;
836 struct argv_array av = ARGV_ARRAY_INIT;
837 const char *hdr_err;
838 int status;
839 char hdr_arg[38];
840 struct child_process child;
841 int fsck_objects = (receive_fsck_objects >= 0
842 ? receive_fsck_objects
843 : transfer_fsck_objects >= 0
844 ? transfer_fsck_objects
845 : 0);
847 hdr_err = parse_pack_header(&hdr);
848 if (hdr_err) {
849 if (err_fd > 0)
850 close(err_fd);
851 return hdr_err;
853 snprintf(hdr_arg, sizeof(hdr_arg),
854 "--pack_header=%"PRIu32",%"PRIu32,
855 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
857 if (si->nr_ours || si->nr_theirs) {
858 alt_shallow_file = setup_temporary_shallow(si->shallow);
859 argv_array_pushl(&av, "--shallow-file", alt_shallow_file, NULL);
862 memset(&child, 0, sizeof(child));
863 if (ntohl(hdr.hdr_entries) < unpack_limit) {
864 argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
865 if (quiet)
866 argv_array_push(&av, "-q");
867 if (fsck_objects)
868 argv_array_push(&av, "--strict");
869 child.argv = av.argv;
870 child.no_stdout = 1;
871 child.err = err_fd;
872 child.git_cmd = 1;
873 status = run_command(&child);
874 if (status)
875 return "unpack-objects abnormal exit";
876 } else {
877 int s;
878 char keep_arg[256];
880 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
881 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
882 strcpy(keep_arg + s, "localhost");
884 argv_array_pushl(&av, "index-pack",
885 "--stdin", hdr_arg, keep_arg, NULL);
886 if (fsck_objects)
887 argv_array_push(&av, "--strict");
888 if (fix_thin)
889 argv_array_push(&av, "--fix-thin");
890 child.argv = av.argv;
891 child.out = -1;
892 child.err = err_fd;
893 child.git_cmd = 1;
894 status = start_command(&child);
895 if (status)
896 return "index-pack fork failed";
897 pack_lockfile = index_pack_lockfile(child.out);
898 close(child.out);
899 status = finish_command(&child);
900 if (status)
901 return "index-pack abnormal exit";
902 reprepare_packed_git();
904 return NULL;
907 static const char *unpack_with_sideband(struct shallow_info *si)
909 struct async muxer;
910 const char *ret;
912 if (!use_sideband)
913 return unpack(0, si);
915 memset(&muxer, 0, sizeof(muxer));
916 muxer.proc = copy_to_sideband;
917 muxer.in = -1;
918 if (start_async(&muxer))
919 return NULL;
921 ret = unpack(muxer.in, si);
923 finish_async(&muxer);
924 return ret;
927 static void update_shallow_info(struct command *commands,
928 struct shallow_info *si,
929 struct sha1_array *ref)
931 struct command *cmd;
932 int *ref_status;
933 remove_nonexistent_theirs_shallow(si);
934 /* XXX remove_nonexistent_ours_in_pack() */
935 if (!si->nr_ours && !si->nr_theirs)
936 return;
938 for (cmd = commands; cmd; cmd = cmd->next) {
939 if (is_null_sha1(cmd->new_sha1))
940 continue;
941 sha1_array_append(ref, cmd->new_sha1);
942 cmd->index = ref->nr - 1;
944 si->ref = ref;
946 ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
947 assign_shallow_commits_to_refs(si, NULL, ref_status);
948 for (cmd = commands; cmd; cmd = cmd->next) {
949 if (is_null_sha1(cmd->new_sha1))
950 continue;
951 if (ref_status[cmd->index]) {
952 cmd->error_string = "shallow update not allowed";
953 cmd->skip_update = 1;
956 if (alt_shallow_file && *alt_shallow_file) {
957 unlink(alt_shallow_file);
958 alt_shallow_file = NULL;
960 free(ref_status);
963 static void report(struct command *commands, const char *unpack_status)
965 struct command *cmd;
966 struct strbuf buf = STRBUF_INIT;
968 packet_buf_write(&buf, "unpack %s\n",
969 unpack_status ? unpack_status : "ok");
970 for (cmd = commands; cmd; cmd = cmd->next) {
971 if (!cmd->error_string)
972 packet_buf_write(&buf, "ok %s\n",
973 cmd->ref_name);
974 else
975 packet_buf_write(&buf, "ng %s %s\n",
976 cmd->ref_name, cmd->error_string);
978 packet_buf_flush(&buf);
980 if (use_sideband)
981 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
982 else
983 write_or_die(1, buf.buf, buf.len);
984 strbuf_release(&buf);
987 static int delete_only(struct command *commands)
989 struct command *cmd;
990 for (cmd = commands; cmd; cmd = cmd->next) {
991 if (!is_null_sha1(cmd->new_sha1))
992 return 0;
994 return 1;
997 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
999 int advertise_refs = 0;
1000 int stateless_rpc = 0;
1001 int i;
1002 char *dir = NULL;
1003 struct command *commands;
1004 struct sha1_array shallow = SHA1_ARRAY_INIT;
1005 struct sha1_array ref = SHA1_ARRAY_INIT;
1006 struct shallow_info si;
1008 packet_trace_identity("receive-pack");
1010 argv++;
1011 for (i = 1; i < argc; i++) {
1012 const char *arg = *argv++;
1014 if (*arg == '-') {
1015 if (!strcmp(arg, "--quiet")) {
1016 quiet = 1;
1017 continue;
1020 if (!strcmp(arg, "--advertise-refs")) {
1021 advertise_refs = 1;
1022 continue;
1024 if (!strcmp(arg, "--stateless-rpc")) {
1025 stateless_rpc = 1;
1026 continue;
1028 if (!strcmp(arg, "--reject-thin-pack-for-testing")) {
1029 fix_thin = 0;
1030 continue;
1033 usage(receive_pack_usage);
1035 if (dir)
1036 usage(receive_pack_usage);
1037 dir = xstrdup(arg);
1039 if (!dir)
1040 usage(receive_pack_usage);
1042 setup_path();
1044 if (!enter_repo(dir, 0))
1045 die("'%s' does not appear to be a git repository", dir);
1047 if (is_repository_shallow() && stateless_rpc)
1048 die("attempt to push into a shallow repository");
1050 git_config(receive_pack_config, NULL);
1052 if (0 <= transfer_unpack_limit)
1053 unpack_limit = transfer_unpack_limit;
1054 else if (0 <= receive_unpack_limit)
1055 unpack_limit = receive_unpack_limit;
1057 if (advertise_refs || !stateless_rpc) {
1058 write_head_info();
1060 if (advertise_refs)
1061 return 0;
1063 if ((commands = read_head_info(&shallow)) != NULL) {
1064 const char *unpack_status = NULL;
1066 prepare_shallow_info(&si, &shallow);
1067 if (!delete_only(commands)) {
1068 unpack_status = unpack_with_sideband(&si);
1069 update_shallow_info(commands, &si, &ref);
1071 execute_commands(commands, unpack_status);
1072 if (pack_lockfile)
1073 unlink_or_warn(pack_lockfile);
1074 if (report_status)
1075 report(commands, unpack_status);
1076 run_receive_hook(commands, "post-receive", 1);
1077 run_update_post_hook(commands);
1078 if (auto_gc) {
1079 const char *argv_gc_auto[] = {
1080 "gc", "--auto", "--quiet", NULL,
1082 int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR;
1083 run_command_v_opt(argv_gc_auto, opt);
1085 if (auto_update_server_info)
1086 update_server_info(0);
1087 clear_shallow_info(&si);
1089 if (use_sideband)
1090 packet_flush(1);
1091 sha1_array_clear(&shallow);
1092 sha1_array_clear(&ref);
1093 return 0;