receive-pack: allow hooks to ignore its standard input stream
[git/debian.git] / builtin / receive-pack.c
blob516386fdd3b757ecde1402c1ce02e9be2f540b9f
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"
18 #include "sigchain.h"
20 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
22 enum deny_action {
23 DENY_UNCONFIGURED,
24 DENY_IGNORE,
25 DENY_WARN,
26 DENY_REFUSE
29 static int deny_deletes;
30 static int deny_non_fast_forwards;
31 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
32 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
33 static int receive_fsck_objects = -1;
34 static int transfer_fsck_objects = -1;
35 static int receive_unpack_limit = -1;
36 static int transfer_unpack_limit = -1;
37 static int unpack_limit = 100;
38 static int report_status;
39 static int use_sideband;
40 static int quiet;
41 static int prefer_ofs_delta = 1;
42 static int auto_update_server_info;
43 static int auto_gc = 1;
44 static int fix_thin = 1;
45 static const char *head_name;
46 static void *head_name_to_free;
47 static int sent_capabilities;
48 static int shallow_update;
49 static const char *alt_shallow_file;
51 static enum deny_action parse_deny_action(const char *var, const char *value)
53 if (value) {
54 if (!strcasecmp(value, "ignore"))
55 return DENY_IGNORE;
56 if (!strcasecmp(value, "warn"))
57 return DENY_WARN;
58 if (!strcasecmp(value, "refuse"))
59 return DENY_REFUSE;
61 if (git_config_bool(var, value))
62 return DENY_REFUSE;
63 return DENY_IGNORE;
66 static int receive_pack_config(const char *var, const char *value, void *cb)
68 int status = parse_hide_refs_config(var, value, "receive");
70 if (status)
71 return status;
73 if (strcmp(var, "receive.denydeletes") == 0) {
74 deny_deletes = git_config_bool(var, value);
75 return 0;
78 if (strcmp(var, "receive.denynonfastforwards") == 0) {
79 deny_non_fast_forwards = git_config_bool(var, value);
80 return 0;
83 if (strcmp(var, "receive.unpacklimit") == 0) {
84 receive_unpack_limit = git_config_int(var, value);
85 return 0;
88 if (strcmp(var, "transfer.unpacklimit") == 0) {
89 transfer_unpack_limit = git_config_int(var, value);
90 return 0;
93 if (strcmp(var, "receive.fsckobjects") == 0) {
94 receive_fsck_objects = git_config_bool(var, value);
95 return 0;
98 if (strcmp(var, "transfer.fsckobjects") == 0) {
99 transfer_fsck_objects = git_config_bool(var, value);
100 return 0;
103 if (!strcmp(var, "receive.denycurrentbranch")) {
104 deny_current_branch = parse_deny_action(var, value);
105 return 0;
108 if (strcmp(var, "receive.denydeletecurrent") == 0) {
109 deny_delete_current = parse_deny_action(var, value);
110 return 0;
113 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
114 prefer_ofs_delta = git_config_bool(var, value);
115 return 0;
118 if (strcmp(var, "receive.updateserverinfo") == 0) {
119 auto_update_server_info = git_config_bool(var, value);
120 return 0;
123 if (strcmp(var, "receive.autogc") == 0) {
124 auto_gc = git_config_bool(var, value);
125 return 0;
128 if (strcmp(var, "receive.shallowupdate") == 0) {
129 shallow_update = git_config_bool(var, value);
130 return 0;
133 return git_default_config(var, value, cb);
136 static void show_ref(const char *path, const unsigned char *sha1)
138 if (ref_is_hidden(path))
139 return;
141 if (sent_capabilities)
142 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
143 else
144 packet_write(1, "%s %s%c%s%s agent=%s\n",
145 sha1_to_hex(sha1), path, 0,
146 " report-status delete-refs side-band-64k quiet",
147 prefer_ofs_delta ? " ofs-delta" : "",
148 git_user_agent_sanitized());
149 sent_capabilities = 1;
152 static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused)
154 path = strip_namespace(path);
156 * Advertise refs outside our current namespace as ".have"
157 * refs, so that the client can use them to minimize data
158 * transfer but will otherwise ignore them. This happens to
159 * cover ".have" that are thrown in by add_one_alternate_ref()
160 * to mark histories that are complete in our alternates as
161 * well.
163 if (!path)
164 path = ".have";
165 show_ref(path, sha1);
166 return 0;
169 static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
171 show_ref(".have", sha1);
174 static void collect_one_alternate_ref(const struct ref *ref, void *data)
176 struct sha1_array *sa = data;
177 sha1_array_append(sa, ref->old_sha1);
180 static void write_head_info(void)
182 struct sha1_array sa = SHA1_ARRAY_INIT;
183 for_each_alternate_ref(collect_one_alternate_ref, &sa);
184 sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
185 sha1_array_clear(&sa);
186 for_each_ref(show_ref_cb, NULL);
187 if (!sent_capabilities)
188 show_ref("capabilities^{}", null_sha1);
190 advertise_shallow_grafts(1);
192 /* EOF */
193 packet_flush(1);
196 struct command {
197 struct command *next;
198 const char *error_string;
199 unsigned int skip_update:1,
200 did_not_exist:1;
201 int index;
202 unsigned char old_sha1[20];
203 unsigned char new_sha1[20];
204 char ref_name[FLEX_ARRAY]; /* more */
207 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
208 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
210 static void report_message(const char *prefix, const char *err, va_list params)
212 int sz = strlen(prefix);
213 char msg[4096];
215 strncpy(msg, prefix, sz);
216 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
217 if (sz > (sizeof(msg) - 1))
218 sz = sizeof(msg) - 1;
219 msg[sz++] = '\n';
221 if (use_sideband)
222 send_sideband(1, 2, msg, sz, use_sideband);
223 else
224 xwrite(2, msg, sz);
227 static void rp_warning(const char *err, ...)
229 va_list params;
230 va_start(params, err);
231 report_message("warning: ", err, params);
232 va_end(params);
235 static void rp_error(const char *err, ...)
237 va_list params;
238 va_start(params, err);
239 report_message("error: ", err, params);
240 va_end(params);
243 static int copy_to_sideband(int in, int out, void *arg)
245 char data[128];
246 while (1) {
247 ssize_t sz = xread(in, data, sizeof(data));
248 if (sz <= 0)
249 break;
250 send_sideband(1, 2, data, sz, use_sideband);
252 close(in);
253 return 0;
256 typedef int (*feed_fn)(void *, const char **, size_t *);
257 static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
259 struct child_process proc;
260 struct async muxer;
261 const char *argv[2];
262 int code;
264 argv[0] = find_hook(hook_name);
265 if (!argv[0])
266 return 0;
268 argv[1] = NULL;
270 memset(&proc, 0, sizeof(proc));
271 proc.argv = argv;
272 proc.in = -1;
273 proc.stdout_to_stderr = 1;
275 if (use_sideband) {
276 memset(&muxer, 0, sizeof(muxer));
277 muxer.proc = copy_to_sideband;
278 muxer.in = -1;
279 code = start_async(&muxer);
280 if (code)
281 return code;
282 proc.err = muxer.in;
285 code = start_command(&proc);
286 if (code) {
287 if (use_sideband)
288 finish_async(&muxer);
289 return code;
292 sigchain_push(SIGPIPE, SIG_IGN);
294 while (1) {
295 const char *buf;
296 size_t n;
297 if (feed(feed_state, &buf, &n))
298 break;
299 if (write_in_full(proc.in, buf, n) != n)
300 break;
302 close(proc.in);
303 if (use_sideband)
304 finish_async(&muxer);
306 sigchain_pop(SIGPIPE);
308 return finish_command(&proc);
311 struct receive_hook_feed_state {
312 struct command *cmd;
313 int skip_broken;
314 struct strbuf buf;
317 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
319 struct receive_hook_feed_state *state = state_;
320 struct command *cmd = state->cmd;
322 while (cmd &&
323 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
324 cmd = cmd->next;
325 if (!cmd)
326 return -1; /* EOF */
327 strbuf_reset(&state->buf);
328 strbuf_addf(&state->buf, "%s %s %s\n",
329 sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
330 cmd->ref_name);
331 state->cmd = cmd->next;
332 if (bufp) {
333 *bufp = state->buf.buf;
334 *sizep = state->buf.len;
336 return 0;
339 static int run_receive_hook(struct command *commands, const char *hook_name,
340 int skip_broken)
342 struct receive_hook_feed_state state;
343 int status;
345 strbuf_init(&state.buf, 0);
346 state.cmd = commands;
347 state.skip_broken = skip_broken;
348 if (feed_receive_hook(&state, NULL, NULL))
349 return 0;
350 state.cmd = commands;
351 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
352 strbuf_release(&state.buf);
353 return status;
356 static int run_update_hook(struct command *cmd)
358 const char *argv[5];
359 struct child_process proc;
360 int code;
362 argv[0] = find_hook("update");
363 if (!argv[0])
364 return 0;
366 argv[1] = cmd->ref_name;
367 argv[2] = sha1_to_hex(cmd->old_sha1);
368 argv[3] = sha1_to_hex(cmd->new_sha1);
369 argv[4] = NULL;
371 memset(&proc, 0, sizeof(proc));
372 proc.no_stdin = 1;
373 proc.stdout_to_stderr = 1;
374 proc.err = use_sideband ? -1 : 0;
375 proc.argv = argv;
377 code = start_command(&proc);
378 if (code)
379 return code;
380 if (use_sideband)
381 copy_to_sideband(proc.err, -1, NULL);
382 return finish_command(&proc);
385 static int is_ref_checked_out(const char *ref)
387 if (is_bare_repository())
388 return 0;
390 if (!head_name)
391 return 0;
392 return !strcmp(head_name, ref);
395 static char *refuse_unconfigured_deny_msg[] = {
396 "By default, updating the current branch in a non-bare repository",
397 "is denied, because it will make the index and work tree inconsistent",
398 "with what you pushed, and will require 'git reset --hard' to match",
399 "the work tree to HEAD.",
401 "You can set 'receive.denyCurrentBranch' configuration variable to",
402 "'ignore' or 'warn' in the remote repository to allow pushing into",
403 "its current branch; however, this is not recommended unless you",
404 "arranged to update its work tree to match what you pushed in some",
405 "other way.",
407 "To squelch this message and still keep the default behaviour, set",
408 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
411 static void refuse_unconfigured_deny(void)
413 int i;
414 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
415 rp_error("%s", refuse_unconfigured_deny_msg[i]);
418 static char *refuse_unconfigured_deny_delete_current_msg[] = {
419 "By default, deleting the current branch is denied, because the next",
420 "'git clone' won't result in any file checked out, causing confusion.",
422 "You can set 'receive.denyDeleteCurrent' configuration variable to",
423 "'warn' or 'ignore' in the remote repository to allow deleting the",
424 "current branch, with or without a warning message.",
426 "To squelch this message, you can set it to 'refuse'."
429 static void refuse_unconfigured_deny_delete_current(void)
431 int i;
432 for (i = 0;
433 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
434 i++)
435 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
438 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]);
439 static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
441 static struct lock_file shallow_lock;
442 struct sha1_array extra = SHA1_ARRAY_INIT;
443 const char *alt_file;
444 uint32_t mask = 1 << (cmd->index % 32);
445 int i;
447 trace_printf_key(&trace_shallow,
448 "shallow: update_shallow_ref %s\n", cmd->ref_name);
449 for (i = 0; i < si->shallow->nr; i++)
450 if (si->used_shallow[i] &&
451 (si->used_shallow[i][cmd->index / 32] & mask) &&
452 !delayed_reachability_test(si, i))
453 sha1_array_append(&extra, si->shallow->sha1[i]);
455 setup_alternate_shallow(&shallow_lock, &alt_file, &extra);
456 if (check_shallow_connected(command_singleton_iterator,
457 0, cmd, alt_file)) {
458 rollback_lock_file(&shallow_lock);
459 sha1_array_clear(&extra);
460 return -1;
463 commit_lock_file(&shallow_lock);
466 * Make sure setup_alternate_shallow() for the next ref does
467 * not lose these new roots..
469 for (i = 0; i < extra.nr; i++)
470 register_shallow(extra.sha1[i]);
472 si->shallow_ref[cmd->index] = 0;
473 sha1_array_clear(&extra);
474 return 0;
477 static const char *update(struct command *cmd, struct shallow_info *si)
479 const char *name = cmd->ref_name;
480 struct strbuf namespaced_name_buf = STRBUF_INIT;
481 const char *namespaced_name;
482 unsigned char *old_sha1 = cmd->old_sha1;
483 unsigned char *new_sha1 = cmd->new_sha1;
484 struct ref_lock *lock;
486 /* only refs/... are allowed */
487 if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
488 rp_error("refusing to create funny ref '%s' remotely", name);
489 return "funny refname";
492 strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
493 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
495 if (is_ref_checked_out(namespaced_name)) {
496 switch (deny_current_branch) {
497 case DENY_IGNORE:
498 break;
499 case DENY_WARN:
500 rp_warning("updating the current branch");
501 break;
502 case DENY_REFUSE:
503 case DENY_UNCONFIGURED:
504 rp_error("refusing to update checked out branch: %s", name);
505 if (deny_current_branch == DENY_UNCONFIGURED)
506 refuse_unconfigured_deny();
507 return "branch is currently checked out";
511 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
512 error("unpack should have generated %s, "
513 "but I can't find it!", sha1_to_hex(new_sha1));
514 return "bad pack";
517 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
518 if (deny_deletes && starts_with(name, "refs/heads/")) {
519 rp_error("denying ref deletion for %s", name);
520 return "deletion prohibited";
523 if (!strcmp(namespaced_name, head_name)) {
524 switch (deny_delete_current) {
525 case DENY_IGNORE:
526 break;
527 case DENY_WARN:
528 rp_warning("deleting the current branch");
529 break;
530 case DENY_REFUSE:
531 case DENY_UNCONFIGURED:
532 if (deny_delete_current == DENY_UNCONFIGURED)
533 refuse_unconfigured_deny_delete_current();
534 rp_error("refusing to delete the current branch: %s", name);
535 return "deletion of the current branch prohibited";
540 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
541 !is_null_sha1(old_sha1) &&
542 starts_with(name, "refs/heads/")) {
543 struct object *old_object, *new_object;
544 struct commit *old_commit, *new_commit;
546 old_object = parse_object(old_sha1);
547 new_object = parse_object(new_sha1);
549 if (!old_object || !new_object ||
550 old_object->type != OBJ_COMMIT ||
551 new_object->type != OBJ_COMMIT) {
552 error("bad sha1 objects for %s", name);
553 return "bad ref";
555 old_commit = (struct commit *)old_object;
556 new_commit = (struct commit *)new_object;
557 if (!in_merge_bases(old_commit, new_commit)) {
558 rp_error("denying non-fast-forward %s"
559 " (you should pull first)", name);
560 return "non-fast-forward";
563 if (run_update_hook(cmd)) {
564 rp_error("hook declined to update %s", name);
565 return "hook declined";
568 if (is_null_sha1(new_sha1)) {
569 if (!parse_object(old_sha1)) {
570 old_sha1 = NULL;
571 if (ref_exists(name)) {
572 rp_warning("Allowing deletion of corrupt ref.");
573 } else {
574 rp_warning("Deleting a non-existent ref.");
575 cmd->did_not_exist = 1;
578 if (delete_ref(namespaced_name, old_sha1, 0)) {
579 rp_error("failed to delete %s", name);
580 return "failed to delete";
582 return NULL; /* good */
584 else {
585 if (shallow_update && si->shallow_ref[cmd->index] &&
586 update_shallow_ref(cmd, si))
587 return "shallow error";
589 lock = lock_any_ref_for_update(namespaced_name, old_sha1,
590 0, NULL);
591 if (!lock) {
592 rp_error("failed to lock %s", name);
593 return "failed to lock";
595 if (write_ref_sha1(lock, new_sha1, "push")) {
596 return "failed to write"; /* error() already called */
598 return NULL; /* good */
602 static void run_update_post_hook(struct command *commands)
604 struct command *cmd;
605 int argc;
606 const char **argv;
607 struct child_process proc;
608 char *hook;
610 hook = find_hook("post-update");
611 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
612 if (cmd->error_string || cmd->did_not_exist)
613 continue;
614 argc++;
616 if (!argc || !hook)
617 return;
619 argv = xmalloc(sizeof(*argv) * (2 + argc));
620 argv[0] = hook;
622 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
623 if (cmd->error_string || cmd->did_not_exist)
624 continue;
625 argv[argc] = xstrdup(cmd->ref_name);
626 argc++;
628 argv[argc] = NULL;
630 memset(&proc, 0, sizeof(proc));
631 proc.no_stdin = 1;
632 proc.stdout_to_stderr = 1;
633 proc.err = use_sideband ? -1 : 0;
634 proc.argv = argv;
636 if (!start_command(&proc)) {
637 if (use_sideband)
638 copy_to_sideband(proc.err, -1, NULL);
639 finish_command(&proc);
643 static void check_aliased_update(struct command *cmd, struct string_list *list)
645 struct strbuf buf = STRBUF_INIT;
646 const char *dst_name;
647 struct string_list_item *item;
648 struct command *dst_cmd;
649 unsigned char sha1[20];
650 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
651 int flag;
653 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
654 dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
655 strbuf_release(&buf);
657 if (!(flag & REF_ISSYMREF))
658 return;
660 dst_name = strip_namespace(dst_name);
661 if (!dst_name) {
662 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
663 cmd->skip_update = 1;
664 cmd->error_string = "broken symref";
665 return;
668 if ((item = string_list_lookup(list, dst_name)) == NULL)
669 return;
671 cmd->skip_update = 1;
673 dst_cmd = (struct command *) item->util;
675 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
676 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
677 return;
679 dst_cmd->skip_update = 1;
681 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
682 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
683 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
684 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
685 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
686 " its target '%s' (%s..%s)",
687 cmd->ref_name, cmd_oldh, cmd_newh,
688 dst_cmd->ref_name, dst_oldh, dst_newh);
690 cmd->error_string = dst_cmd->error_string =
691 "inconsistent aliased update";
694 static void check_aliased_updates(struct command *commands)
696 struct command *cmd;
697 struct string_list ref_list = STRING_LIST_INIT_NODUP;
699 for (cmd = commands; cmd; cmd = cmd->next) {
700 struct string_list_item *item =
701 string_list_append(&ref_list, cmd->ref_name);
702 item->util = (void *)cmd;
704 sort_string_list(&ref_list);
706 for (cmd = commands; cmd; cmd = cmd->next) {
707 if (!cmd->error_string)
708 check_aliased_update(cmd, &ref_list);
711 string_list_clear(&ref_list, 0);
714 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
716 struct command **cmd_list = cb_data;
717 struct command *cmd = *cmd_list;
719 if (!cmd || is_null_sha1(cmd->new_sha1))
720 return -1; /* end of list */
721 *cmd_list = NULL; /* this returns only one */
722 hashcpy(sha1, cmd->new_sha1);
723 return 0;
726 static void set_connectivity_errors(struct command *commands,
727 struct shallow_info *si)
729 struct command *cmd;
731 for (cmd = commands; cmd; cmd = cmd->next) {
732 struct command *singleton = cmd;
733 if (shallow_update && si->shallow_ref[cmd->index])
734 /* to be checked in update_shallow_ref() */
735 continue;
736 if (!check_everything_connected(command_singleton_iterator,
737 0, &singleton))
738 continue;
739 cmd->error_string = "missing necessary objects";
743 struct iterate_data {
744 struct command *cmds;
745 struct shallow_info *si;
748 static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
750 struct iterate_data *data = cb_data;
751 struct command **cmd_list = &data->cmds;
752 struct command *cmd = *cmd_list;
754 for (; cmd; cmd = cmd->next) {
755 if (shallow_update && data->si->shallow_ref[cmd->index])
756 /* to be checked in update_shallow_ref() */
757 continue;
758 if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
759 hashcpy(sha1, cmd->new_sha1);
760 *cmd_list = cmd->next;
761 return 0;
764 *cmd_list = NULL;
765 return -1; /* end of list */
768 static void reject_updates_to_hidden(struct command *commands)
770 struct command *cmd;
772 for (cmd = commands; cmd; cmd = cmd->next) {
773 if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
774 continue;
775 if (is_null_sha1(cmd->new_sha1))
776 cmd->error_string = "deny deleting a hidden ref";
777 else
778 cmd->error_string = "deny updating a hidden ref";
782 static void execute_commands(struct command *commands,
783 const char *unpacker_error,
784 struct shallow_info *si)
786 int checked_connectivity;
787 struct command *cmd;
788 unsigned char sha1[20];
789 struct iterate_data data;
791 if (unpacker_error) {
792 for (cmd = commands; cmd; cmd = cmd->next)
793 cmd->error_string = "unpacker error";
794 return;
797 data.cmds = commands;
798 data.si = si;
799 if (check_everything_connected(iterate_receive_command_list, 0, &data))
800 set_connectivity_errors(commands, si);
802 reject_updates_to_hidden(commands);
804 if (run_receive_hook(commands, "pre-receive", 0)) {
805 for (cmd = commands; cmd; cmd = cmd->next) {
806 if (!cmd->error_string)
807 cmd->error_string = "pre-receive hook declined";
809 return;
812 check_aliased_updates(commands);
814 free(head_name_to_free);
815 head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
817 checked_connectivity = 1;
818 for (cmd = commands; cmd; cmd = cmd->next) {
819 if (cmd->error_string)
820 continue;
822 if (cmd->skip_update)
823 continue;
825 cmd->error_string = update(cmd, si);
826 if (shallow_update && !cmd->error_string &&
827 si->shallow_ref[cmd->index]) {
828 error("BUG: connectivity check has not been run on ref %s",
829 cmd->ref_name);
830 checked_connectivity = 0;
834 if (shallow_update && !checked_connectivity)
835 error("BUG: run 'git fsck' for safety.\n"
836 "If there are errors, try to remove "
837 "the reported refs above");
840 static struct command *read_head_info(struct sha1_array *shallow)
842 struct command *commands = NULL;
843 struct command **p = &commands;
844 for (;;) {
845 char *line;
846 unsigned char old_sha1[20], new_sha1[20];
847 struct command *cmd;
848 char *refname;
849 int len, reflen;
851 line = packet_read_line(0, &len);
852 if (!line)
853 break;
855 if (len == 48 && starts_with(line, "shallow ")) {
856 if (get_sha1_hex(line + 8, old_sha1))
857 die("protocol error: expected shallow sha, got '%s'", line + 8);
858 sha1_array_append(shallow, old_sha1);
859 continue;
862 if (len < 83 ||
863 line[40] != ' ' ||
864 line[81] != ' ' ||
865 get_sha1_hex(line, old_sha1) ||
866 get_sha1_hex(line + 41, new_sha1))
867 die("protocol error: expected old/new/ref, got '%s'",
868 line);
870 refname = line + 82;
871 reflen = strlen(refname);
872 if (reflen + 82 < len) {
873 const char *feature_list = refname + reflen + 1;
874 if (parse_feature_request(feature_list, "report-status"))
875 report_status = 1;
876 if (parse_feature_request(feature_list, "side-band-64k"))
877 use_sideband = LARGE_PACKET_MAX;
878 if (parse_feature_request(feature_list, "quiet"))
879 quiet = 1;
881 cmd = xcalloc(1, sizeof(struct command) + len - 80);
882 hashcpy(cmd->old_sha1, old_sha1);
883 hashcpy(cmd->new_sha1, new_sha1);
884 memcpy(cmd->ref_name, line + 82, len - 81);
885 *p = cmd;
886 p = &cmd->next;
888 return commands;
891 static const char *parse_pack_header(struct pack_header *hdr)
893 switch (read_pack_header(0, hdr)) {
894 case PH_ERROR_EOF:
895 return "eof before pack header was fully read";
897 case PH_ERROR_PACK_SIGNATURE:
898 return "protocol error (pack signature mismatch detected)";
900 case PH_ERROR_PROTOCOL:
901 return "protocol error (pack version unsupported)";
903 default:
904 return "unknown error in parse_pack_header";
906 case 0:
907 return NULL;
911 static const char *pack_lockfile;
913 static const char *unpack(int err_fd, struct shallow_info *si)
915 struct pack_header hdr;
916 struct argv_array av = ARGV_ARRAY_INIT;
917 const char *hdr_err;
918 int status;
919 char hdr_arg[38];
920 struct child_process child;
921 int fsck_objects = (receive_fsck_objects >= 0
922 ? receive_fsck_objects
923 : transfer_fsck_objects >= 0
924 ? transfer_fsck_objects
925 : 0);
927 hdr_err = parse_pack_header(&hdr);
928 if (hdr_err) {
929 if (err_fd > 0)
930 close(err_fd);
931 return hdr_err;
933 snprintf(hdr_arg, sizeof(hdr_arg),
934 "--pack_header=%"PRIu32",%"PRIu32,
935 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
937 if (si->nr_ours || si->nr_theirs) {
938 alt_shallow_file = setup_temporary_shallow(si->shallow);
939 argv_array_pushl(&av, "--shallow-file", alt_shallow_file, NULL);
942 memset(&child, 0, sizeof(child));
943 if (ntohl(hdr.hdr_entries) < unpack_limit) {
944 argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
945 if (quiet)
946 argv_array_push(&av, "-q");
947 if (fsck_objects)
948 argv_array_push(&av, "--strict");
949 child.argv = av.argv;
950 child.no_stdout = 1;
951 child.err = err_fd;
952 child.git_cmd = 1;
953 status = run_command(&child);
954 if (status)
955 return "unpack-objects abnormal exit";
956 } else {
957 int s;
958 char keep_arg[256];
960 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
961 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
962 strcpy(keep_arg + s, "localhost");
964 argv_array_pushl(&av, "index-pack",
965 "--stdin", hdr_arg, keep_arg, NULL);
966 if (fsck_objects)
967 argv_array_push(&av, "--strict");
968 if (fix_thin)
969 argv_array_push(&av, "--fix-thin");
970 child.argv = av.argv;
971 child.out = -1;
972 child.err = err_fd;
973 child.git_cmd = 1;
974 status = start_command(&child);
975 if (status)
976 return "index-pack fork failed";
977 pack_lockfile = index_pack_lockfile(child.out);
978 close(child.out);
979 status = finish_command(&child);
980 if (status)
981 return "index-pack abnormal exit";
982 reprepare_packed_git();
984 return NULL;
987 static const char *unpack_with_sideband(struct shallow_info *si)
989 struct async muxer;
990 const char *ret;
992 if (!use_sideband)
993 return unpack(0, si);
995 memset(&muxer, 0, sizeof(muxer));
996 muxer.proc = copy_to_sideband;
997 muxer.in = -1;
998 if (start_async(&muxer))
999 return NULL;
1001 ret = unpack(muxer.in, si);
1003 finish_async(&muxer);
1004 return ret;
1007 static void prepare_shallow_update(struct command *commands,
1008 struct shallow_info *si)
1010 int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1012 si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
1013 si->shallow->nr);
1014 assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
1016 si->need_reachability_test =
1017 xcalloc(si->shallow->nr, sizeof(*si->need_reachability_test));
1018 si->reachable =
1019 xcalloc(si->shallow->nr, sizeof(*si->reachable));
1020 si->shallow_ref = xcalloc(si->ref->nr, sizeof(*si->shallow_ref));
1022 for (i = 0; i < si->nr_ours; i++)
1023 si->need_reachability_test[si->ours[i]] = 1;
1025 for (i = 0; i < si->shallow->nr; i++) {
1026 if (!si->used_shallow[i])
1027 continue;
1028 for (j = 0; j < bitmap_size; j++) {
1029 if (!si->used_shallow[i][j])
1030 continue;
1031 si->need_reachability_test[i]++;
1032 for (k = 0; k < 32; k++)
1033 if (si->used_shallow[i][j] & (1 << k))
1034 si->shallow_ref[j * 32 + k]++;
1038 * true for those associated with some refs and belong
1039 * in "ours" list aka "step 7 not done yet"
1041 si->need_reachability_test[i] =
1042 si->need_reachability_test[i] > 1;
1046 * keep hooks happy by forcing a temporary shallow file via
1047 * env variable because we can't add --shallow-file to every
1048 * command. check_everything_connected() will be done with
1049 * true .git/shallow though.
1051 setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
1054 static void update_shallow_info(struct command *commands,
1055 struct shallow_info *si,
1056 struct sha1_array *ref)
1058 struct command *cmd;
1059 int *ref_status;
1060 remove_nonexistent_theirs_shallow(si);
1061 if (!si->nr_ours && !si->nr_theirs) {
1062 shallow_update = 0;
1063 return;
1066 for (cmd = commands; cmd; cmd = cmd->next) {
1067 if (is_null_sha1(cmd->new_sha1))
1068 continue;
1069 sha1_array_append(ref, cmd->new_sha1);
1070 cmd->index = ref->nr - 1;
1072 si->ref = ref;
1074 if (shallow_update) {
1075 prepare_shallow_update(commands, si);
1076 return;
1079 ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
1080 assign_shallow_commits_to_refs(si, NULL, ref_status);
1081 for (cmd = commands; cmd; cmd = cmd->next) {
1082 if (is_null_sha1(cmd->new_sha1))
1083 continue;
1084 if (ref_status[cmd->index]) {
1085 cmd->error_string = "shallow update not allowed";
1086 cmd->skip_update = 1;
1089 free(ref_status);
1092 static void report(struct command *commands, const char *unpack_status)
1094 struct command *cmd;
1095 struct strbuf buf = STRBUF_INIT;
1097 packet_buf_write(&buf, "unpack %s\n",
1098 unpack_status ? unpack_status : "ok");
1099 for (cmd = commands; cmd; cmd = cmd->next) {
1100 if (!cmd->error_string)
1101 packet_buf_write(&buf, "ok %s\n",
1102 cmd->ref_name);
1103 else
1104 packet_buf_write(&buf, "ng %s %s\n",
1105 cmd->ref_name, cmd->error_string);
1107 packet_buf_flush(&buf);
1109 if (use_sideband)
1110 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
1111 else
1112 write_or_die(1, buf.buf, buf.len);
1113 strbuf_release(&buf);
1116 static int delete_only(struct command *commands)
1118 struct command *cmd;
1119 for (cmd = commands; cmd; cmd = cmd->next) {
1120 if (!is_null_sha1(cmd->new_sha1))
1121 return 0;
1123 return 1;
1126 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
1128 int advertise_refs = 0;
1129 int stateless_rpc = 0;
1130 int i;
1131 const char *dir = NULL;
1132 struct command *commands;
1133 struct sha1_array shallow = SHA1_ARRAY_INIT;
1134 struct sha1_array ref = SHA1_ARRAY_INIT;
1135 struct shallow_info si;
1137 packet_trace_identity("receive-pack");
1139 argv++;
1140 for (i = 1; i < argc; i++) {
1141 const char *arg = *argv++;
1143 if (*arg == '-') {
1144 if (!strcmp(arg, "--quiet")) {
1145 quiet = 1;
1146 continue;
1149 if (!strcmp(arg, "--advertise-refs")) {
1150 advertise_refs = 1;
1151 continue;
1153 if (!strcmp(arg, "--stateless-rpc")) {
1154 stateless_rpc = 1;
1155 continue;
1157 if (!strcmp(arg, "--reject-thin-pack-for-testing")) {
1158 fix_thin = 0;
1159 continue;
1162 usage(receive_pack_usage);
1164 if (dir)
1165 usage(receive_pack_usage);
1166 dir = arg;
1168 if (!dir)
1169 usage(receive_pack_usage);
1171 setup_path();
1173 if (!enter_repo(dir, 0))
1174 die("'%s' does not appear to be a git repository", dir);
1176 git_config(receive_pack_config, NULL);
1178 if (0 <= transfer_unpack_limit)
1179 unpack_limit = transfer_unpack_limit;
1180 else if (0 <= receive_unpack_limit)
1181 unpack_limit = receive_unpack_limit;
1183 if (advertise_refs || !stateless_rpc) {
1184 write_head_info();
1186 if (advertise_refs)
1187 return 0;
1189 if ((commands = read_head_info(&shallow)) != NULL) {
1190 const char *unpack_status = NULL;
1192 prepare_shallow_info(&si, &shallow);
1193 if (!si.nr_ours && !si.nr_theirs)
1194 shallow_update = 0;
1195 if (!delete_only(commands)) {
1196 unpack_status = unpack_with_sideband(&si);
1197 update_shallow_info(commands, &si, &ref);
1199 execute_commands(commands, unpack_status, &si);
1200 if (pack_lockfile)
1201 unlink_or_warn(pack_lockfile);
1202 if (report_status)
1203 report(commands, unpack_status);
1204 run_receive_hook(commands, "post-receive", 1);
1205 run_update_post_hook(commands);
1206 if (auto_gc) {
1207 const char *argv_gc_auto[] = {
1208 "gc", "--auto", "--quiet", NULL,
1210 int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR;
1211 run_command_v_opt(argv_gc_auto, opt);
1213 if (auto_update_server_info)
1214 update_server_info(0);
1215 clear_shallow_info(&si);
1217 if (use_sideband)
1218 packet_flush(1);
1219 sha1_array_clear(&shallow);
1220 sha1_array_clear(&ref);
1221 return 0;