6 #include "run-command.h"
11 #include "transport.h"
12 #include "string-list.h"
14 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
23 static int deny_deletes;
24 static int deny_non_fast_forwards;
25 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
26 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
27 static int receive_fsck_objects;
28 static int receive_unpack_limit = -1;
29 static int transfer_unpack_limit = -1;
30 static int unpack_limit = 100;
31 static int report_status;
32 static int use_sideband;
33 static int prefer_ofs_delta = 1;
34 static int auto_update_server_info;
35 static int auto_gc = 1;
36 static const char *head_name;
37 static int sent_capabilities;
39 static enum deny_action parse_deny_action(const char *var, const char *value)
42 if (!strcasecmp(value, "ignore"))
44 if (!strcasecmp(value, "warn"))
46 if (!strcasecmp(value, "refuse"))
49 if (git_config_bool(var, value))
54 static int receive_pack_config(const char *var, const char *value, void *cb)
56 if (strcmp(var, "receive.denydeletes") == 0) {
57 deny_deletes = git_config_bool(var, value);
61 if (strcmp(var, "receive.denynonfastforwards") == 0) {
62 deny_non_fast_forwards = git_config_bool(var, value);
66 if (strcmp(var, "receive.unpacklimit") == 0) {
67 receive_unpack_limit = git_config_int(var, value);
71 if (strcmp(var, "transfer.unpacklimit") == 0) {
72 transfer_unpack_limit = git_config_int(var, value);
76 if (strcmp(var, "receive.fsckobjects") == 0) {
77 receive_fsck_objects = git_config_bool(var, value);
81 if (!strcmp(var, "receive.denycurrentbranch")) {
82 deny_current_branch = parse_deny_action(var, value);
86 if (strcmp(var, "receive.denydeletecurrent") == 0) {
87 deny_delete_current = parse_deny_action(var, value);
91 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
92 prefer_ofs_delta = git_config_bool(var, value);
96 if (strcmp(var, "receive.updateserverinfo") == 0) {
97 auto_update_server_info = git_config_bool(var, value);
101 if (strcmp(var, "receive.autogc") == 0) {
102 auto_gc = git_config_bool(var, value);
106 return git_default_config(var, value, cb);
109 static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
111 if (sent_capabilities)
112 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
114 packet_write(1, "%s %s%c%s%s\n",
115 sha1_to_hex(sha1), path, 0,
116 " report-status delete-refs side-band-64k",
117 prefer_ofs_delta ? " ofs-delta" : "");
118 sent_capabilities = 1;
122 static void write_head_info(void)
124 for_each_ref(show_ref, NULL);
125 if (!sent_capabilities)
126 show_ref("capabilities^{}", null_sha1, 0, NULL);
131 struct command *next;
132 const char *error_string;
133 unsigned int skip_update;
134 unsigned char old_sha1[20];
135 unsigned char new_sha1[20];
136 char ref_name[FLEX_ARRAY]; /* more */
139 static const char pre_receive_hook[] = "hooks/pre-receive";
140 static const char post_receive_hook[] = "hooks/post-receive";
142 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
143 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
145 static void report_message(const char *prefix, const char *err, va_list params)
147 int sz = strlen(prefix);
150 strncpy(msg, prefix, sz);
151 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
152 if (sz > (sizeof(msg) - 1))
153 sz = sizeof(msg) - 1;
157 send_sideband(1, 2, msg, sz, use_sideband);
162 static void rp_warning(const char *err, ...)
165 va_start(params, err);
166 report_message("warning: ", err, params);
170 static void rp_error(const char *err, ...)
173 va_start(params, err);
174 report_message("error: ", err, params);
178 static int copy_to_sideband(int in, int out, void *arg)
182 ssize_t sz = xread(in, data, sizeof(data));
185 send_sideband(1, 2, data, sz, use_sideband);
191 static int run_receive_hook(struct command *commands, const char *hook_name)
193 static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
195 struct child_process proc;
198 int have_input = 0, code;
200 for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
201 if (!cmd->error_string)
205 if (!have_input || access(hook_name, X_OK) < 0)
211 memset(&proc, 0, sizeof(proc));
214 proc.stdout_to_stderr = 1;
217 memset(&muxer, 0, sizeof(muxer));
218 muxer.proc = copy_to_sideband;
220 code = start_async(&muxer);
226 code = start_command(&proc);
229 finish_async(&muxer);
233 for (cmd = commands; cmd; cmd = cmd->next) {
234 if (!cmd->error_string) {
235 size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
236 sha1_to_hex(cmd->old_sha1),
237 sha1_to_hex(cmd->new_sha1),
239 if (write_in_full(proc.in, buf, n) != n)
245 finish_async(&muxer);
246 return finish_command(&proc);
249 static int run_update_hook(struct command *cmd)
251 static const char update_hook[] = "hooks/update";
253 struct child_process proc;
256 if (access(update_hook, X_OK) < 0)
259 argv[0] = update_hook;
260 argv[1] = cmd->ref_name;
261 argv[2] = sha1_to_hex(cmd->old_sha1);
262 argv[3] = sha1_to_hex(cmd->new_sha1);
265 memset(&proc, 0, sizeof(proc));
267 proc.stdout_to_stderr = 1;
268 proc.err = use_sideband ? -1 : 0;
271 code = start_command(&proc);
275 copy_to_sideband(proc.err, -1, NULL);
276 return finish_command(&proc);
279 static int is_ref_checked_out(const char *ref)
281 if (is_bare_repository())
286 return !strcmp(head_name, ref);
289 static char *refuse_unconfigured_deny_msg[] = {
290 "By default, updating the current branch in a non-bare repository",
291 "is denied, because it will make the index and work tree inconsistent",
292 "with what you pushed, and will require 'git reset --hard' to match",
293 "the work tree to HEAD.",
295 "You can set 'receive.denyCurrentBranch' configuration variable to",
296 "'ignore' or 'warn' in the remote repository to allow pushing into",
297 "its current branch; however, this is not recommended unless you",
298 "arranged to update its work tree to match what you pushed in some",
301 "To squelch this message and still keep the default behaviour, set",
302 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
305 static void refuse_unconfigured_deny(void)
308 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
309 rp_error("%s", refuse_unconfigured_deny_msg[i]);
312 static char *refuse_unconfigured_deny_delete_current_msg[] = {
313 "By default, deleting the current branch is denied, because the next",
314 "'git clone' won't result in any file checked out, causing confusion.",
316 "You can set 'receive.denyDeleteCurrent' configuration variable to",
317 "'warn' or 'ignore' in the remote repository to allow deleting the",
318 "current branch, with or without a warning message.",
320 "To squelch this message, you can set it to 'refuse'."
323 static void refuse_unconfigured_deny_delete_current(void)
327 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
329 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
332 static const char *update(struct command *cmd)
334 const char *name = cmd->ref_name;
335 unsigned char *old_sha1 = cmd->old_sha1;
336 unsigned char *new_sha1 = cmd->new_sha1;
337 struct ref_lock *lock;
339 /* only refs/... are allowed */
340 if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
341 rp_error("refusing to create funny ref '%s' remotely", name);
342 return "funny refname";
345 if (is_ref_checked_out(name)) {
346 switch (deny_current_branch) {
350 rp_warning("updating the current branch");
353 case DENY_UNCONFIGURED:
354 rp_error("refusing to update checked out branch: %s", name);
355 if (deny_current_branch == DENY_UNCONFIGURED)
356 refuse_unconfigured_deny();
357 return "branch is currently checked out";
361 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
362 error("unpack should have generated %s, "
363 "but I can't find it!", sha1_to_hex(new_sha1));
367 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
368 if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
369 rp_error("denying ref deletion for %s", name);
370 return "deletion prohibited";
373 if (!strcmp(name, head_name)) {
374 switch (deny_delete_current) {
378 rp_warning("deleting the current branch");
381 case DENY_UNCONFIGURED:
382 if (deny_delete_current == DENY_UNCONFIGURED)
383 refuse_unconfigured_deny_delete_current();
384 rp_error("refusing to delete the current branch: %s", name);
385 return "deletion of the current branch prohibited";
390 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
391 !is_null_sha1(old_sha1) &&
392 !prefixcmp(name, "refs/heads/")) {
393 struct object *old_object, *new_object;
394 struct commit *old_commit, *new_commit;
395 struct commit_list *bases, *ent;
397 old_object = parse_object(old_sha1);
398 new_object = parse_object(new_sha1);
400 if (!old_object || !new_object ||
401 old_object->type != OBJ_COMMIT ||
402 new_object->type != OBJ_COMMIT) {
403 error("bad sha1 objects for %s", name);
406 old_commit = (struct commit *)old_object;
407 new_commit = (struct commit *)new_object;
408 bases = get_merge_bases(old_commit, new_commit, 1);
409 for (ent = bases; ent; ent = ent->next)
410 if (!hashcmp(old_sha1, ent->item->object.sha1))
412 free_commit_list(bases);
414 rp_error("denying non-fast-forward %s"
415 " (you should pull first)", name);
416 return "non-fast-forward";
419 if (run_update_hook(cmd)) {
420 rp_error("hook declined to update %s", name);
421 return "hook declined";
424 if (is_null_sha1(new_sha1)) {
425 if (!parse_object(old_sha1)) {
426 rp_warning("Allowing deletion of corrupt ref.");
429 if (delete_ref(name, old_sha1, 0)) {
430 rp_error("failed to delete %s", name);
431 return "failed to delete";
433 return NULL; /* good */
436 lock = lock_any_ref_for_update(name, old_sha1, 0);
438 rp_error("failed to lock %s", name);
439 return "failed to lock";
441 if (write_ref_sha1(lock, new_sha1, "push")) {
442 return "failed to write"; /* error() already called */
444 return NULL; /* good */
448 static char update_post_hook[] = "hooks/post-update";
450 static void run_update_post_hook(struct command *commands)
455 struct child_process proc;
457 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
458 if (cmd->error_string)
462 if (!argc || access(update_post_hook, X_OK) < 0)
464 argv = xmalloc(sizeof(*argv) * (2 + argc));
465 argv[0] = update_post_hook;
467 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
469 if (cmd->error_string)
471 p = xmalloc(strlen(cmd->ref_name) + 1);
472 strcpy(p, cmd->ref_name);
478 memset(&proc, 0, sizeof(proc));
480 proc.stdout_to_stderr = 1;
481 proc.err = use_sideband ? -1 : 0;
484 if (!start_command(&proc)) {
486 copy_to_sideband(proc.err, -1, NULL);
487 finish_command(&proc);
491 static void check_aliased_update(struct command *cmd, struct string_list *list)
493 struct string_list_item *item;
494 struct command *dst_cmd;
495 unsigned char sha1[20];
496 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
499 const char *dst_name = resolve_ref(cmd->ref_name, sha1, 0, &flag);
501 if (!(flag & REF_ISSYMREF))
504 if ((item = string_list_lookup(list, dst_name)) == NULL)
507 cmd->skip_update = 1;
509 dst_cmd = (struct command *) item->util;
511 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
512 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
515 dst_cmd->skip_update = 1;
517 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
518 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
519 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
520 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
521 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
522 " its target '%s' (%s..%s)",
523 cmd->ref_name, cmd_oldh, cmd_newh,
524 dst_cmd->ref_name, dst_oldh, dst_newh);
526 cmd->error_string = dst_cmd->error_string =
527 "inconsistent aliased update";
530 static void check_aliased_updates(struct command *commands)
533 struct string_list ref_list = STRING_LIST_INIT_NODUP;
535 for (cmd = commands; cmd; cmd = cmd->next) {
536 struct string_list_item *item =
537 string_list_append(&ref_list, cmd->ref_name);
538 item->util = (void *)cmd;
540 sort_string_list(&ref_list);
542 for (cmd = commands; cmd; cmd = cmd->next)
543 check_aliased_update(cmd, &ref_list);
545 string_list_clear(&ref_list, 0);
548 static void execute_commands(struct command *commands, const char *unpacker_error)
551 unsigned char sha1[20];
553 if (unpacker_error) {
554 for (cmd = commands; cmd; cmd = cmd->next)
555 cmd->error_string = "n/a (unpacker error)";
559 if (run_receive_hook(commands, pre_receive_hook)) {
560 for (cmd = commands; cmd; cmd = cmd->next)
561 cmd->error_string = "pre-receive hook declined";
565 check_aliased_updates(commands);
567 head_name = resolve_ref("HEAD", sha1, 0, NULL);
569 for (cmd = commands; cmd; cmd = cmd->next)
570 if (!cmd->skip_update)
571 cmd->error_string = update(cmd);
574 static struct command *read_head_info(void)
576 struct command *commands = NULL;
577 struct command **p = &commands;
579 static char line[1000];
580 unsigned char old_sha1[20], new_sha1[20];
585 len = packet_read_line(0, line, sizeof(line));
588 if (line[len-1] == '\n')
593 get_sha1_hex(line, old_sha1) ||
594 get_sha1_hex(line + 41, new_sha1))
595 die("protocol error: expected old/new/ref, got '%s'",
599 reflen = strlen(refname);
600 if (reflen + 82 < len) {
601 if (strstr(refname + reflen + 1, "report-status"))
603 if (strstr(refname + reflen + 1, "side-band-64k"))
604 use_sideband = LARGE_PACKET_MAX;
606 cmd = xcalloc(1, sizeof(struct command) + len - 80);
607 hashcpy(cmd->old_sha1, old_sha1);
608 hashcpy(cmd->new_sha1, new_sha1);
609 memcpy(cmd->ref_name, line + 82, len - 81);
616 static const char *parse_pack_header(struct pack_header *hdr)
618 switch (read_pack_header(0, hdr)) {
620 return "eof before pack header was fully read";
622 case PH_ERROR_PACK_SIGNATURE:
623 return "protocol error (pack signature mismatch detected)";
625 case PH_ERROR_PROTOCOL:
626 return "protocol error (pack version unsupported)";
629 return "unknown error in parse_pack_header";
636 static const char *pack_lockfile;
638 static const char *unpack(void)
640 struct pack_header hdr;
644 hdr_err = parse_pack_header(&hdr);
647 snprintf(hdr_arg, sizeof(hdr_arg),
648 "--pack_header=%"PRIu32",%"PRIu32,
649 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
651 if (ntohl(hdr.hdr_entries) < unpack_limit) {
653 const char *unpacker[4];
654 unpacker[i++] = "unpack-objects";
655 if (receive_fsck_objects)
656 unpacker[i++] = "--strict";
657 unpacker[i++] = hdr_arg;
658 unpacker[i++] = NULL;
659 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
662 return "unpack-objects abnormal exit";
664 const char *keeper[7];
665 int s, status, i = 0;
667 struct child_process ip;
669 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
670 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
671 strcpy(keep_arg + s, "localhost");
673 keeper[i++] = "index-pack";
674 keeper[i++] = "--stdin";
675 if (receive_fsck_objects)
676 keeper[i++] = "--strict";
677 keeper[i++] = "--fix-thin";
678 keeper[i++] = hdr_arg;
679 keeper[i++] = keep_arg;
681 memset(&ip, 0, sizeof(ip));
685 status = start_command(&ip);
687 return "index-pack fork failed";
689 pack_lockfile = index_pack_lockfile(ip.out);
691 status = finish_command(&ip);
693 reprepare_packed_git();
696 return "index-pack abnormal exit";
700 static void report(struct command *commands, const char *unpack_status)
703 struct strbuf buf = STRBUF_INIT;
705 packet_buf_write(&buf, "unpack %s\n",
706 unpack_status ? unpack_status : "ok");
707 for (cmd = commands; cmd; cmd = cmd->next) {
708 if (!cmd->error_string)
709 packet_buf_write(&buf, "ok %s\n",
712 packet_buf_write(&buf, "ng %s %s\n",
713 cmd->ref_name, cmd->error_string);
715 packet_buf_flush(&buf);
718 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
720 safe_write(1, buf.buf, buf.len);
721 strbuf_release(&buf);
724 static int delete_only(struct command *commands)
727 for (cmd = commands; cmd; cmd = cmd->next) {
728 if (!is_null_sha1(cmd->new_sha1))
734 static int add_refs_from_alternate(struct alternate_object_database *e, void *unused)
738 struct remote *remote;
739 struct transport *transport;
740 const struct ref *extra;
743 other = xstrdup(real_path(e->base));
747 while (other[len-1] == '/')
749 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
751 /* Is this a git repository with refs? */
752 memcpy(other + len - 8, "/refs", 6);
753 if (!is_directory(other))
755 other[len - 8] = '\0';
756 remote = remote_get(other);
757 transport = transport_get(remote, other);
758 for (extra = transport_get_remote_refs(transport);
760 extra = extra->next) {
761 add_extra_ref(".have", extra->old_sha1, 0);
763 transport_disconnect(transport);
768 static void add_alternate_refs(void)
770 foreach_alt_odb(add_refs_from_alternate, NULL);
773 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
775 int advertise_refs = 0;
776 int stateless_rpc = 0;
779 struct command *commands;
781 packet_trace_identity("receive-pack");
784 for (i = 1; i < argc; i++) {
785 const char *arg = *argv++;
788 if (!strcmp(arg, "--advertise-refs")) {
792 if (!strcmp(arg, "--stateless-rpc")) {
797 usage(receive_pack_usage);
800 usage(receive_pack_usage);
804 usage(receive_pack_usage);
808 if (!enter_repo(dir, 0))
809 die("'%s' does not appear to be a git repository", dir);
811 if (is_repository_shallow())
812 die("attempt to push into a shallow repository");
814 git_config(receive_pack_config, NULL);
816 if (0 <= transfer_unpack_limit)
817 unpack_limit = transfer_unpack_limit;
818 else if (0 <= receive_unpack_limit)
819 unpack_limit = receive_unpack_limit;
821 if (advertise_refs || !stateless_rpc) {
822 add_alternate_refs();
832 if ((commands = read_head_info()) != NULL) {
833 const char *unpack_status = NULL;
835 if (!delete_only(commands))
836 unpack_status = unpack();
837 execute_commands(commands, unpack_status);
839 unlink_or_warn(pack_lockfile);
841 report(commands, unpack_status);
842 run_receive_hook(commands, post_receive_hook);
843 run_update_post_hook(commands);
845 const char *argv_gc_auto[] = {
846 "gc", "--auto", "--quiet", NULL,
848 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
850 if (auto_update_server_info)
851 update_server_info(0);