6 #include "run-command.h"
11 #include "transport.h"
13 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
22 static int deny_deletes;
23 static int deny_non_fast_forwards;
24 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
25 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
26 static int receive_fsck_objects = -1;
27 static int transfer_fsck_objects = -1;
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, "transfer.fsckobjects") == 0) {
82 transfer_fsck_objects = git_config_bool(var, value);
86 if (!strcmp(var, "receive.denycurrentbranch")) {
87 deny_current_branch = parse_deny_action(var, value);
91 if (strcmp(var, "receive.denydeletecurrent") == 0) {
92 deny_delete_current = parse_deny_action(var, value);
96 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
97 prefer_ofs_delta = git_config_bool(var, value);
101 if (strcmp(var, "receive.updateserverinfo") == 0) {
102 auto_update_server_info = git_config_bool(var, value);
106 if (strcmp(var, "receive.autogc") == 0) {
107 auto_gc = git_config_bool(var, value);
111 return git_default_config(var, value, cb);
114 static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
116 if (sent_capabilities)
117 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
119 packet_write(1, "%s %s%c%s%s\n",
120 sha1_to_hex(sha1), path, 0,
121 " report-status delete-refs side-band-64k",
122 prefer_ofs_delta ? " ofs-delta" : "");
123 sent_capabilities = 1;
127 static void write_head_info(void)
129 for_each_ref(show_ref, NULL);
130 if (!sent_capabilities)
131 show_ref("capabilities^{}", null_sha1, 0, NULL);
136 struct command *next;
137 const char *error_string;
138 unsigned char old_sha1[20];
139 unsigned char new_sha1[20];
140 char ref_name[FLEX_ARRAY]; /* more */
143 static struct command *commands;
145 static const char pre_receive_hook[] = "hooks/pre-receive";
146 static const char post_receive_hook[] = "hooks/post-receive";
148 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
149 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
151 static void report_message(const char *prefix, const char *err, va_list params)
153 int sz = strlen(prefix);
156 strncpy(msg, prefix, sz);
157 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
158 if (sz > (sizeof(msg) - 1))
159 sz = sizeof(msg) - 1;
163 send_sideband(1, 2, msg, sz, use_sideband);
168 static void rp_warning(const char *err, ...)
171 va_start(params, err);
172 report_message("warning: ", err, params);
176 static void rp_error(const char *err, ...)
179 va_start(params, err);
180 report_message("error: ", err, params);
184 static int copy_to_sideband(int in, int out, void *arg)
188 ssize_t sz = xread(in, data, sizeof(data));
191 send_sideband(1, 2, data, sz, use_sideband);
197 static int run_receive_hook(const char *hook_name)
199 static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
201 struct child_process proc;
204 int have_input = 0, code;
206 for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
207 if (!cmd->error_string)
211 if (!have_input || access(hook_name, X_OK) < 0)
217 memset(&proc, 0, sizeof(proc));
220 proc.stdout_to_stderr = 1;
223 memset(&muxer, 0, sizeof(muxer));
224 muxer.proc = copy_to_sideband;
226 code = start_async(&muxer);
232 code = start_command(&proc);
235 finish_async(&muxer);
239 for (cmd = commands; cmd; cmd = cmd->next) {
240 if (!cmd->error_string) {
241 size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
242 sha1_to_hex(cmd->old_sha1),
243 sha1_to_hex(cmd->new_sha1),
245 if (write_in_full(proc.in, buf, n) != n)
251 finish_async(&muxer);
252 return finish_command(&proc);
255 static int run_update_hook(struct command *cmd)
257 static const char update_hook[] = "hooks/update";
259 struct child_process proc;
262 if (access(update_hook, X_OK) < 0)
265 argv[0] = update_hook;
266 argv[1] = cmd->ref_name;
267 argv[2] = sha1_to_hex(cmd->old_sha1);
268 argv[3] = sha1_to_hex(cmd->new_sha1);
271 memset(&proc, 0, sizeof(proc));
273 proc.stdout_to_stderr = 1;
274 proc.err = use_sideband ? -1 : 0;
277 code = start_command(&proc);
281 copy_to_sideband(proc.err, -1, NULL);
282 return finish_command(&proc);
285 static int is_ref_checked_out(const char *ref)
287 if (is_bare_repository())
292 return !strcmp(head_name, ref);
295 static char *refuse_unconfigured_deny_msg[] = {
296 "By default, updating the current branch in a non-bare repository",
297 "is denied, because it will make the index and work tree inconsistent",
298 "with what you pushed, and will require 'git reset --hard' to match",
299 "the work tree to HEAD.",
301 "You can set 'receive.denyCurrentBranch' configuration variable to",
302 "'ignore' or 'warn' in the remote repository to allow pushing into",
303 "its current branch; however, this is not recommended unless you",
304 "arranged to update its work tree to match what you pushed in some",
307 "To squelch this message and still keep the default behaviour, set",
308 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
311 static void refuse_unconfigured_deny(void)
314 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
315 rp_error("%s", refuse_unconfigured_deny_msg[i]);
318 static char *refuse_unconfigured_deny_delete_current_msg[] = {
319 "By default, deleting the current branch is denied, because the next",
320 "'git clone' won't result in any file checked out, causing confusion.",
322 "You can set 'receive.denyDeleteCurrent' configuration variable to",
323 "'warn' or 'ignore' in the remote repository to allow deleting the",
324 "current branch, with or without a warning message.",
326 "To squelch this message, you can set it to 'refuse'."
329 static void refuse_unconfigured_deny_delete_current(void)
333 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
335 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
338 static const char *update(struct command *cmd)
340 const char *name = cmd->ref_name;
341 unsigned char *old_sha1 = cmd->old_sha1;
342 unsigned char *new_sha1 = cmd->new_sha1;
343 struct ref_lock *lock;
345 /* only refs/... are allowed */
346 if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
347 rp_error("refusing to create funny ref '%s' remotely", name);
348 return "funny refname";
351 if (is_ref_checked_out(name)) {
352 switch (deny_current_branch) {
356 rp_warning("updating the current branch");
359 case DENY_UNCONFIGURED:
360 rp_error("refusing to update checked out branch: %s", name);
361 if (deny_current_branch == DENY_UNCONFIGURED)
362 refuse_unconfigured_deny();
363 return "branch is currently checked out";
367 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
368 error("unpack should have generated %s, "
369 "but I can't find it!", sha1_to_hex(new_sha1));
373 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
374 if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
375 rp_error("denying ref deletion for %s", name);
376 return "deletion prohibited";
379 if (!strcmp(name, head_name)) {
380 switch (deny_delete_current) {
384 rp_warning("deleting the current branch");
387 case DENY_UNCONFIGURED:
388 if (deny_delete_current == DENY_UNCONFIGURED)
389 refuse_unconfigured_deny_delete_current();
390 rp_error("refusing to delete the current branch: %s", name);
391 return "deletion of the current branch prohibited";
396 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
397 !is_null_sha1(old_sha1) &&
398 !prefixcmp(name, "refs/heads/")) {
399 struct object *old_object, *new_object;
400 struct commit *old_commit, *new_commit;
401 struct commit_list *bases, *ent;
403 old_object = parse_object(old_sha1);
404 new_object = parse_object(new_sha1);
406 if (!old_object || !new_object ||
407 old_object->type != OBJ_COMMIT ||
408 new_object->type != OBJ_COMMIT) {
409 error("bad sha1 objects for %s", name);
412 old_commit = (struct commit *)old_object;
413 new_commit = (struct commit *)new_object;
414 bases = get_merge_bases(old_commit, new_commit, 1);
415 for (ent = bases; ent; ent = ent->next)
416 if (!hashcmp(old_sha1, ent->item->object.sha1))
418 free_commit_list(bases);
420 rp_error("denying non-fast-forward %s"
421 " (you should pull first)", name);
422 return "non-fast-forward";
425 if (run_update_hook(cmd)) {
426 rp_error("hook declined to update %s", name);
427 return "hook declined";
430 if (is_null_sha1(new_sha1)) {
431 if (!parse_object(old_sha1)) {
432 rp_warning("Allowing deletion of corrupt ref.");
435 if (delete_ref(name, old_sha1, 0)) {
436 rp_error("failed to delete %s", name);
437 return "failed to delete";
439 return NULL; /* good */
442 lock = lock_any_ref_for_update(name, old_sha1, 0);
444 rp_error("failed to lock %s", name);
445 return "failed to lock";
447 if (write_ref_sha1(lock, new_sha1, "push")) {
448 return "failed to write"; /* error() already called */
450 return NULL; /* good */
454 static char update_post_hook[] = "hooks/post-update";
456 static void run_update_post_hook(struct command *cmd)
458 struct command *cmd_p;
461 struct child_process proc;
463 for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
464 if (cmd_p->error_string)
468 if (!argc || access(update_post_hook, X_OK) < 0)
470 argv = xmalloc(sizeof(*argv) * (2 + argc));
471 argv[0] = update_post_hook;
473 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
475 if (cmd_p->error_string)
477 p = xmalloc(strlen(cmd_p->ref_name) + 1);
478 strcpy(p, cmd_p->ref_name);
484 memset(&proc, 0, sizeof(proc));
486 proc.stdout_to_stderr = 1;
487 proc.err = use_sideband ? -1 : 0;
490 if (!start_command(&proc)) {
492 copy_to_sideband(proc.err, -1, NULL);
493 finish_command(&proc);
497 static void execute_commands(const char *unpacker_error)
499 struct command *cmd = commands;
500 unsigned char sha1[20];
502 if (unpacker_error) {
504 cmd->error_string = "n/a (unpacker error)";
510 if (run_receive_hook(pre_receive_hook)) {
512 cmd->error_string = "pre-receive hook declined";
518 head_name = resolve_ref("HEAD", sha1, 0, NULL);
521 cmd->error_string = update(cmd);
526 static void read_head_info(void)
528 struct command **p = &commands;
530 static char line[1000];
531 unsigned char old_sha1[20], new_sha1[20];
536 len = packet_read_line(0, line, sizeof(line));
539 if (line[len-1] == '\n')
544 get_sha1_hex(line, old_sha1) ||
545 get_sha1_hex(line + 41, new_sha1))
546 die("protocol error: expected old/new/ref, got '%s'",
550 reflen = strlen(refname);
551 if (reflen + 82 < len) {
552 if (strstr(refname + reflen + 1, "report-status"))
554 if (strstr(refname + reflen + 1, "side-band-64k"))
555 use_sideband = LARGE_PACKET_MAX;
557 cmd = xmalloc(sizeof(struct command) + len - 80);
558 hashcpy(cmd->old_sha1, old_sha1);
559 hashcpy(cmd->new_sha1, new_sha1);
560 memcpy(cmd->ref_name, line + 82, len - 81);
561 cmd->error_string = NULL;
568 static const char *parse_pack_header(struct pack_header *hdr)
570 switch (read_pack_header(0, hdr)) {
572 return "eof before pack header was fully read";
574 case PH_ERROR_PACK_SIGNATURE:
575 return "protocol error (pack signature mismatch detected)";
577 case PH_ERROR_PROTOCOL:
578 return "protocol error (pack version unsupported)";
581 return "unknown error in parse_pack_header";
588 static const char *pack_lockfile;
590 static const char *unpack(void)
592 struct pack_header hdr;
595 int fsck_objects = (receive_fsck_objects >= 0
596 ? receive_fsck_objects
597 : transfer_fsck_objects >= 0
598 ? transfer_fsck_objects
601 hdr_err = parse_pack_header(&hdr);
604 snprintf(hdr_arg, sizeof(hdr_arg),
605 "--pack_header=%"PRIu32",%"PRIu32,
606 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
608 if (ntohl(hdr.hdr_entries) < unpack_limit) {
610 const char *unpacker[4];
611 unpacker[i++] = "unpack-objects";
613 unpacker[i++] = "--strict";
614 unpacker[i++] = hdr_arg;
615 unpacker[i++] = NULL;
616 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
619 return "unpack-objects abnormal exit";
621 const char *keeper[7];
622 int s, status, i = 0;
624 struct child_process ip;
626 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
627 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
628 strcpy(keep_arg + s, "localhost");
630 keeper[i++] = "index-pack";
631 keeper[i++] = "--stdin";
633 keeper[i++] = "--strict";
634 keeper[i++] = "--fix-thin";
635 keeper[i++] = hdr_arg;
636 keeper[i++] = keep_arg;
638 memset(&ip, 0, sizeof(ip));
642 status = start_command(&ip);
644 return "index-pack fork failed";
646 pack_lockfile = index_pack_lockfile(ip.out);
648 status = finish_command(&ip);
650 reprepare_packed_git();
653 return "index-pack abnormal exit";
657 static void report(const char *unpack_status)
660 struct strbuf buf = STRBUF_INIT;
662 packet_buf_write(&buf, "unpack %s\n",
663 unpack_status ? unpack_status : "ok");
664 for (cmd = commands; cmd; cmd = cmd->next) {
665 if (!cmd->error_string)
666 packet_buf_write(&buf, "ok %s\n",
669 packet_buf_write(&buf, "ng %s %s\n",
670 cmd->ref_name, cmd->error_string);
672 packet_buf_flush(&buf);
675 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
677 safe_write(1, buf.buf, buf.len);
678 strbuf_release(&buf);
681 static int delete_only(struct command *cmd)
684 if (!is_null_sha1(cmd->new_sha1))
691 static int add_refs_from_alternate(struct alternate_object_database *e, void *unused)
695 struct remote *remote;
696 struct transport *transport;
697 const struct ref *extra;
700 other = xstrdup(make_absolute_path(e->base));
704 while (other[len-1] == '/')
706 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
708 /* Is this a git repository with refs? */
709 memcpy(other + len - 8, "/refs", 6);
710 if (!is_directory(other))
712 other[len - 8] = '\0';
713 remote = remote_get(other);
714 transport = transport_get(remote, other);
715 for (extra = transport_get_remote_refs(transport);
717 extra = extra->next) {
718 add_extra_ref(".have", extra->old_sha1, 0);
720 transport_disconnect(transport);
725 static void add_alternate_refs(void)
727 foreach_alt_odb(add_refs_from_alternate, NULL);
730 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
732 int advertise_refs = 0;
733 int stateless_rpc = 0;
738 for (i = 1; i < argc; i++) {
739 const char *arg = *argv++;
742 if (!strcmp(arg, "--advertise-refs")) {
746 if (!strcmp(arg, "--stateless-rpc")) {
751 usage(receive_pack_usage);
754 usage(receive_pack_usage);
758 usage(receive_pack_usage);
762 if (!enter_repo(dir, 0))
763 die("'%s' does not appear to be a git repository", dir);
765 if (is_repository_shallow())
766 die("attempt to push into a shallow repository");
768 git_config(receive_pack_config, NULL);
770 if (0 <= transfer_unpack_limit)
771 unpack_limit = transfer_unpack_limit;
772 else if (0 <= receive_unpack_limit)
773 unpack_limit = receive_unpack_limit;
775 if (advertise_refs || !stateless_rpc) {
776 add_alternate_refs();
788 const char *unpack_status = NULL;
790 if (!delete_only(commands))
791 unpack_status = unpack();
792 execute_commands(unpack_status);
794 unlink_or_warn(pack_lockfile);
796 report(unpack_status);
797 run_receive_hook(post_receive_hook);
798 run_update_post_hook(commands);
800 const char *argv_gc_auto[] = {
801 "gc", "--auto", "--quiet", NULL,
803 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
805 if (auto_update_server_info)
806 update_server_info(0);