Revert "MinGW: Add missing file mode bit defines"
[git/mingw/4msysgit/gitPS1fix.git] / builtin / receive-pack.c
bloba638ecd815c91c7b87a3f45ac5f952a4449bfca6
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 "transport.h"
12 #include "string-list.h"
13 #include "sha1-array.h"
15 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
17 enum deny_action {
18 DENY_UNCONFIGURED,
19 DENY_IGNORE,
20 DENY_WARN,
21 DENY_REFUSE,
22 DENY_UPDATE_INSTEAD,
23 DENY_DETACH_INSTEAD,
26 static int deny_deletes;
27 static int deny_non_fast_forwards;
28 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
29 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
30 static int receive_fsck_objects;
31 static int receive_unpack_limit = -1;
32 static int transfer_unpack_limit = -1;
33 static int unpack_limit = 100;
34 static int report_status;
35 static int use_sideband;
36 static int prefer_ofs_delta = 1;
37 static int auto_update_server_info;
38 static int auto_gc = 1;
39 static const char *head_name;
40 static int sent_capabilities;
42 static enum deny_action parse_deny_action(const char *var, const char *value)
44 if (value) {
45 if (!strcasecmp(value, "ignore"))
46 return DENY_IGNORE;
47 if (!strcasecmp(value, "warn"))
48 return DENY_WARN;
49 if (!strcasecmp(value, "refuse"))
50 return DENY_REFUSE;
52 if (git_config_bool(var, value))
53 return DENY_REFUSE;
54 return DENY_IGNORE;
57 static int receive_pack_config(const char *var, const char *value, void *cb)
59 if (strcmp(var, "receive.denydeletes") == 0) {
60 deny_deletes = git_config_bool(var, value);
61 return 0;
64 if (strcmp(var, "receive.denynonfastforwards") == 0) {
65 deny_non_fast_forwards = git_config_bool(var, value);
66 return 0;
69 if (strcmp(var, "receive.unpacklimit") == 0) {
70 receive_unpack_limit = git_config_int(var, value);
71 return 0;
74 if (strcmp(var, "transfer.unpacklimit") == 0) {
75 transfer_unpack_limit = git_config_int(var, value);
76 return 0;
79 if (strcmp(var, "receive.fsckobjects") == 0) {
80 receive_fsck_objects = git_config_bool(var, value);
81 return 0;
84 if (!strcmp(var, "receive.denycurrentbranch")) {
85 if (value && !strcasecmp(value, "updateinstead"))
86 deny_current_branch = DENY_UPDATE_INSTEAD;
87 else if (value && !strcasecmp(value, "detachinstead"))
88 deny_current_branch = DENY_DETACH_INSTEAD;
89 else
90 deny_current_branch = parse_deny_action(var, value);
91 return 0;
94 if (strcmp(var, "receive.denydeletecurrent") == 0) {
95 deny_delete_current = parse_deny_action(var, value);
96 return 0;
99 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
100 prefer_ofs_delta = git_config_bool(var, value);
101 return 0;
104 if (strcmp(var, "receive.updateserverinfo") == 0) {
105 auto_update_server_info = git_config_bool(var, value);
106 return 0;
109 if (strcmp(var, "receive.autogc") == 0) {
110 auto_gc = git_config_bool(var, value);
111 return 0;
114 return git_default_config(var, value, cb);
117 static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
119 if (sent_capabilities)
120 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
121 else
122 packet_write(1, "%s %s%c%s%s\n",
123 sha1_to_hex(sha1), path, 0,
124 " report-status delete-refs side-band-64k",
125 prefer_ofs_delta ? " ofs-delta" : "");
126 sent_capabilities = 1;
127 return 0;
130 static void write_head_info(void)
132 for_each_ref(show_ref, NULL);
133 if (!sent_capabilities)
134 show_ref("capabilities^{}", null_sha1, 0, NULL);
138 struct command {
139 struct command *next;
140 const char *error_string;
141 unsigned int skip_update;
142 unsigned char old_sha1[20];
143 unsigned char new_sha1[20];
144 char ref_name[FLEX_ARRAY]; /* more */
147 static const char pre_receive_hook[] = "hooks/pre-receive";
148 static const char post_receive_hook[] = "hooks/post-receive";
150 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
151 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
153 static void report_message(const char *prefix, const char *err, va_list params)
155 int sz = strlen(prefix);
156 char msg[4096];
158 strncpy(msg, prefix, sz);
159 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
160 if (sz > (sizeof(msg) - 1))
161 sz = sizeof(msg) - 1;
162 msg[sz++] = '\n';
164 if (use_sideband)
165 send_sideband(1, 2, msg, sz, use_sideband);
166 else
167 xwrite(2, msg, sz);
170 static void rp_warning(const char *err, ...)
172 va_list params;
173 va_start(params, err);
174 report_message("warning: ", err, params);
175 va_end(params);
178 static void rp_error(const char *err, ...)
180 va_list params;
181 va_start(params, err);
182 report_message("error: ", err, params);
183 va_end(params);
186 static int copy_to_sideband(int in, int out, void *arg)
188 char data[128];
189 while (1) {
190 ssize_t sz = xread(in, data, sizeof(data));
191 if (sz <= 0)
192 break;
193 send_sideband(1, 2, data, sz, use_sideband);
195 close(in);
196 return 0;
199 static int run_receive_hook(struct command *commands, const char *hook_name)
201 static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
202 struct command *cmd;
203 struct child_process proc;
204 struct async muxer;
205 const char *argv[2];
206 int have_input = 0, code;
208 for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
209 if (!cmd->error_string)
210 have_input = 1;
213 if (!have_input || access(hook_name, X_OK) < 0)
214 return 0;
216 argv[0] = hook_name;
217 argv[1] = NULL;
219 memset(&proc, 0, sizeof(proc));
220 proc.argv = argv;
221 proc.in = -1;
222 proc.stdout_to_stderr = 1;
224 if (use_sideband) {
225 memset(&muxer, 0, sizeof(muxer));
226 muxer.proc = copy_to_sideband;
227 muxer.in = -1;
228 code = start_async(&muxer);
229 if (code)
230 return code;
231 proc.err = muxer.in;
234 code = start_command(&proc);
235 if (code) {
236 if (use_sideband)
237 finish_async(&muxer);
238 return code;
241 for (cmd = commands; cmd; cmd = cmd->next) {
242 if (!cmd->error_string) {
243 size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
244 sha1_to_hex(cmd->old_sha1),
245 sha1_to_hex(cmd->new_sha1),
246 cmd->ref_name);
247 if (write_in_full(proc.in, buf, n) != n)
248 break;
251 close(proc.in);
252 if (use_sideband)
253 finish_async(&muxer);
254 return finish_command(&proc);
257 static int run_update_hook(struct command *cmd)
259 static const char update_hook[] = "hooks/update";
260 const char *argv[5];
261 struct child_process proc;
262 int code;
264 if (access(update_hook, X_OK) < 0)
265 return 0;
267 argv[0] = update_hook;
268 argv[1] = cmd->ref_name;
269 argv[2] = sha1_to_hex(cmd->old_sha1);
270 argv[3] = sha1_to_hex(cmd->new_sha1);
271 argv[4] = NULL;
273 memset(&proc, 0, sizeof(proc));
274 proc.no_stdin = 1;
275 proc.stdout_to_stderr = 1;
276 proc.err = use_sideband ? -1 : 0;
277 proc.argv = argv;
279 code = start_command(&proc);
280 if (code)
281 return code;
282 if (use_sideband)
283 copy_to_sideband(proc.err, -1, NULL);
284 return finish_command(&proc);
287 static int is_ref_checked_out(const char *ref)
289 if (is_bare_repository())
290 return 0;
292 if (!head_name)
293 return 0;
294 return !strcmp(head_name, ref);
297 static char *refuse_unconfigured_deny_msg[] = {
298 "By default, updating the current branch in a non-bare repository",
299 "is denied, because it will make the index and work tree inconsistent",
300 "with what you pushed, and will require 'git reset --hard' to match",
301 "the work tree to HEAD.",
303 "You can set 'receive.denyCurrentBranch' configuration variable to",
304 "'ignore' or 'warn' in the remote repository to allow pushing into",
305 "its current branch; however, this is not recommended unless you",
306 "arranged to update its work tree to match what you pushed in some",
307 "other way.",
309 "To squelch this message and still keep the default behaviour, set",
310 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
313 static void refuse_unconfigured_deny(void)
315 int i;
316 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
317 rp_error("%s", refuse_unconfigured_deny_msg[i]);
320 static char *refuse_unconfigured_deny_delete_current_msg[] = {
321 "By default, deleting the current branch is denied, because the next",
322 "'git clone' won't result in any file checked out, causing confusion.",
324 "You can set 'receive.denyDeleteCurrent' configuration variable to",
325 "'warn' or 'ignore' in the remote repository to allow deleting the",
326 "current branch, with or without a warning message.",
328 "To squelch this message, you can set it to 'refuse'."
331 static void refuse_unconfigured_deny_delete_current(void)
333 int i;
334 for (i = 0;
335 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
336 i++)
337 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
340 static void merge_worktree(unsigned char *sha1)
342 const char *update_refresh[] = {
343 "update-index", "--ignore-submodules", "--refresh", NULL
345 const char *read_tree[] = {
346 "read-tree", "-u", "-m", sha1_to_hex(sha1), NULL
348 struct child_process child;
349 struct strbuf git_env = STRBUF_INIT;
350 const char *env[2];
352 if (is_bare_repository())
353 die ("denyCurrentBranch = updateInstead needs a worktree");
355 strbuf_addf(&git_env, "GIT_DIR=%s", absolute_path(get_git_dir()));
356 env[0] = git_env.buf;
357 env[1] = NULL;
359 memset(&child, 0, sizeof(child));
360 child.argv = update_refresh;
361 child.env = env;
362 child.dir = git_work_tree_cfg ? git_work_tree_cfg : "..";
363 child.stdout_to_stderr = 1;
364 child.git_cmd = 1;
365 if (run_command(&child))
366 die ("Could not refresh the index");
368 child.argv = read_tree;
369 child.no_stdin = 1;
370 child.no_stdout = 1;
371 child.stdout_to_stderr = 0;
372 if (run_command(&child))
373 die ("Could not merge working tree with new HEAD. Good luck.");
375 strbuf_release(&git_env);
378 static const char *update(struct command *cmd)
380 const char *name = cmd->ref_name;
381 unsigned char *old_sha1 = cmd->old_sha1;
382 unsigned char *new_sha1 = cmd->new_sha1;
383 struct ref_lock *lock;
385 /* only refs/... are allowed */
386 if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
387 rp_error("refusing to create funny ref '%s' remotely", name);
388 return "funny refname";
391 if (is_ref_checked_out(name)) {
392 switch (deny_current_branch) {
393 case DENY_IGNORE:
394 break;
395 case DENY_WARN:
396 rp_warning("updating the current branch");
397 break;
398 case DENY_REFUSE:
399 case DENY_UNCONFIGURED:
400 rp_error("refusing to update checked out branch: %s", name);
401 if (deny_current_branch == DENY_UNCONFIGURED)
402 refuse_unconfigured_deny();
403 return "branch is currently checked out";
404 case DENY_UPDATE_INSTEAD:
405 merge_worktree(new_sha1);
406 break;
407 case DENY_DETACH_INSTEAD:
408 update_ref("push into current branch (detach)", "HEAD",
409 old_sha1, NULL, REF_NODEREF, DIE_ON_ERR);
410 break;
414 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
415 error("unpack should have generated %s, "
416 "but I can't find it!", sha1_to_hex(new_sha1));
417 return "bad pack";
420 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
421 if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
422 rp_error("denying ref deletion for %s", name);
423 return "deletion prohibited";
426 if (!strcmp(name, head_name)) {
427 switch (deny_delete_current) {
428 case DENY_IGNORE:
429 break;
430 case DENY_WARN:
431 rp_warning("deleting the current branch");
432 break;
433 case DENY_REFUSE:
434 case DENY_UNCONFIGURED:
435 if (deny_delete_current == DENY_UNCONFIGURED)
436 refuse_unconfigured_deny_delete_current();
437 rp_error("refusing to delete the current branch: %s", name);
438 return "deletion of the current branch prohibited";
439 default:
440 die ("Invalid denyDeleteCurrent setting");
445 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
446 !is_null_sha1(old_sha1) &&
447 !prefixcmp(name, "refs/heads/")) {
448 struct object *old_object, *new_object;
449 struct commit *old_commit, *new_commit;
450 struct commit_list *bases, *ent;
452 old_object = parse_object(old_sha1);
453 new_object = parse_object(new_sha1);
455 if (!old_object || !new_object ||
456 old_object->type != OBJ_COMMIT ||
457 new_object->type != OBJ_COMMIT) {
458 error("bad sha1 objects for %s", name);
459 return "bad ref";
461 old_commit = (struct commit *)old_object;
462 new_commit = (struct commit *)new_object;
463 bases = get_merge_bases(old_commit, new_commit, 1);
464 for (ent = bases; ent; ent = ent->next)
465 if (!hashcmp(old_sha1, ent->item->object.sha1))
466 break;
467 free_commit_list(bases);
468 if (!ent) {
469 rp_error("denying non-fast-forward %s"
470 " (you should pull first)", name);
471 return "non-fast-forward";
474 if (run_update_hook(cmd)) {
475 rp_error("hook declined to update %s", name);
476 return "hook declined";
479 if (is_null_sha1(new_sha1)) {
480 if (!parse_object(old_sha1)) {
481 rp_warning("Allowing deletion of corrupt ref.");
482 old_sha1 = NULL;
484 if (delete_ref(name, old_sha1, 0)) {
485 rp_error("failed to delete %s", name);
486 return "failed to delete";
488 return NULL; /* good */
490 else {
491 lock = lock_any_ref_for_update(name, old_sha1, 0);
492 if (!lock) {
493 rp_error("failed to lock %s", name);
494 return "failed to lock";
496 if (write_ref_sha1(lock, new_sha1, "push")) {
497 return "failed to write"; /* error() already called */
499 return NULL; /* good */
503 static char update_post_hook[] = "hooks/post-update";
505 static void run_update_post_hook(struct command *commands)
507 struct command *cmd;
508 int argc;
509 const char **argv;
510 struct child_process proc;
512 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
513 if (cmd->error_string)
514 continue;
515 argc++;
517 if (!argc || access(update_post_hook, X_OK) < 0)
518 return;
519 argv = xmalloc(sizeof(*argv) * (2 + argc));
520 argv[0] = update_post_hook;
522 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
523 char *p;
524 if (cmd->error_string)
525 continue;
526 p = xmalloc(strlen(cmd->ref_name) + 1);
527 strcpy(p, cmd->ref_name);
528 argv[argc] = p;
529 argc++;
531 argv[argc] = NULL;
533 memset(&proc, 0, sizeof(proc));
534 proc.no_stdin = 1;
535 proc.stdout_to_stderr = 1;
536 proc.err = use_sideband ? -1 : 0;
537 proc.argv = argv;
539 if (!start_command(&proc)) {
540 if (use_sideband)
541 copy_to_sideband(proc.err, -1, NULL);
542 finish_command(&proc);
546 static void check_aliased_update(struct command *cmd, struct string_list *list)
548 struct string_list_item *item;
549 struct command *dst_cmd;
550 unsigned char sha1[20];
551 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
552 int flag;
554 const char *dst_name = resolve_ref(cmd->ref_name, sha1, 0, &flag);
556 if (!(flag & REF_ISSYMREF))
557 return;
559 if ((item = string_list_lookup(list, dst_name)) == NULL)
560 return;
562 cmd->skip_update = 1;
564 dst_cmd = (struct command *) item->util;
566 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
567 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
568 return;
570 dst_cmd->skip_update = 1;
572 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
573 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
574 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
575 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
576 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
577 " its target '%s' (%s..%s)",
578 cmd->ref_name, cmd_oldh, cmd_newh,
579 dst_cmd->ref_name, dst_oldh, dst_newh);
581 cmd->error_string = dst_cmd->error_string =
582 "inconsistent aliased update";
585 static void check_aliased_updates(struct command *commands)
587 struct command *cmd;
588 struct string_list ref_list = STRING_LIST_INIT_NODUP;
590 for (cmd = commands; cmd; cmd = cmd->next) {
591 struct string_list_item *item =
592 string_list_append(&ref_list, cmd->ref_name);
593 item->util = (void *)cmd;
595 sort_string_list(&ref_list);
597 for (cmd = commands; cmd; cmd = cmd->next)
598 check_aliased_update(cmd, &ref_list);
600 string_list_clear(&ref_list, 0);
603 static void execute_commands(struct command *commands, const char *unpacker_error)
605 struct command *cmd;
606 unsigned char sha1[20];
608 if (unpacker_error) {
609 for (cmd = commands; cmd; cmd = cmd->next)
610 cmd->error_string = "n/a (unpacker error)";
611 return;
614 if (run_receive_hook(commands, pre_receive_hook)) {
615 for (cmd = commands; cmd; cmd = cmd->next)
616 cmd->error_string = "pre-receive hook declined";
617 return;
620 check_aliased_updates(commands);
622 head_name = resolve_ref("HEAD", sha1, 0, NULL);
624 for (cmd = commands; cmd; cmd = cmd->next)
625 if (!cmd->skip_update)
626 cmd->error_string = update(cmd);
629 static struct command *read_head_info(void)
631 struct command *commands = NULL;
632 struct command **p = &commands;
633 for (;;) {
634 static char line[1000];
635 unsigned char old_sha1[20], new_sha1[20];
636 struct command *cmd;
637 char *refname;
638 int len, reflen;
640 len = packet_read_line(0, line, sizeof(line));
641 if (!len)
642 break;
643 if (line[len-1] == '\n')
644 line[--len] = 0;
645 if (len < 83 ||
646 line[40] != ' ' ||
647 line[81] != ' ' ||
648 get_sha1_hex(line, old_sha1) ||
649 get_sha1_hex(line + 41, new_sha1))
650 die("protocol error: expected old/new/ref, got '%s'",
651 line);
653 refname = line + 82;
654 reflen = strlen(refname);
655 if (reflen + 82 < len) {
656 if (strstr(refname + reflen + 1, "report-status"))
657 report_status = 1;
658 if (strstr(refname + reflen + 1, "side-band-64k"))
659 use_sideband = LARGE_PACKET_MAX;
661 cmd = xcalloc(1, sizeof(struct command) + len - 80);
662 hashcpy(cmd->old_sha1, old_sha1);
663 hashcpy(cmd->new_sha1, new_sha1);
664 memcpy(cmd->ref_name, line + 82, len - 81);
665 *p = cmd;
666 p = &cmd->next;
668 return commands;
671 static const char *parse_pack_header(struct pack_header *hdr)
673 switch (read_pack_header(0, hdr)) {
674 case PH_ERROR_EOF:
675 return "eof before pack header was fully read";
677 case PH_ERROR_PACK_SIGNATURE:
678 return "protocol error (pack signature mismatch detected)";
680 case PH_ERROR_PROTOCOL:
681 return "protocol error (pack version unsupported)";
683 default:
684 return "unknown error in parse_pack_header";
686 case 0:
687 return NULL;
691 static const char *pack_lockfile;
693 static const char *unpack(void)
695 struct pack_header hdr;
696 const char *hdr_err;
697 char hdr_arg[38];
699 hdr_err = parse_pack_header(&hdr);
700 if (hdr_err)
701 return hdr_err;
702 snprintf(hdr_arg, sizeof(hdr_arg),
703 "--pack_header=%"PRIu32",%"PRIu32,
704 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
706 if (ntohl(hdr.hdr_entries) < unpack_limit) {
707 int code, i = 0;
708 const char *unpacker[4];
709 unpacker[i++] = "unpack-objects";
710 if (receive_fsck_objects)
711 unpacker[i++] = "--strict";
712 unpacker[i++] = hdr_arg;
713 unpacker[i++] = NULL;
714 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
715 if (!code)
716 return NULL;
717 return "unpack-objects abnormal exit";
718 } else {
719 const char *keeper[7];
720 int s, status, i = 0;
721 char keep_arg[256];
722 struct child_process ip;
724 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
725 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
726 strcpy(keep_arg + s, "localhost");
728 keeper[i++] = "index-pack";
729 keeper[i++] = "--stdin";
730 if (receive_fsck_objects)
731 keeper[i++] = "--strict";
732 keeper[i++] = "--fix-thin";
733 keeper[i++] = hdr_arg;
734 keeper[i++] = keep_arg;
735 keeper[i++] = NULL;
736 memset(&ip, 0, sizeof(ip));
737 ip.argv = keeper;
738 ip.out = -1;
739 ip.git_cmd = 1;
740 status = start_command(&ip);
741 if (status) {
742 return "index-pack fork failed";
744 pack_lockfile = index_pack_lockfile(ip.out);
745 close(ip.out);
746 status = finish_command(&ip);
747 if (!status) {
748 reprepare_packed_git();
749 return NULL;
751 return "index-pack abnormal exit";
755 static void report(struct command *commands, const char *unpack_status)
757 struct command *cmd;
758 struct strbuf buf = STRBUF_INIT;
760 packet_buf_write(&buf, "unpack %s\n",
761 unpack_status ? unpack_status : "ok");
762 for (cmd = commands; cmd; cmd = cmd->next) {
763 if (!cmd->error_string)
764 packet_buf_write(&buf, "ok %s\n",
765 cmd->ref_name);
766 else
767 packet_buf_write(&buf, "ng %s %s\n",
768 cmd->ref_name, cmd->error_string);
770 packet_buf_flush(&buf);
772 if (use_sideband)
773 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
774 else
775 safe_write(1, buf.buf, buf.len);
776 strbuf_release(&buf);
779 static int delete_only(struct command *commands)
781 struct command *cmd;
782 for (cmd = commands; cmd; cmd = cmd->next) {
783 if (!is_null_sha1(cmd->new_sha1))
784 return 0;
786 return 1;
789 static void add_one_alternate_sha1(const unsigned char sha1[20], void *unused)
791 add_extra_ref(".have", sha1, 0);
794 static void collect_one_alternate_ref(const struct ref *ref, void *data)
796 struct sha1_array *sa = data;
797 sha1_array_append(sa, ref->old_sha1);
800 static void add_alternate_refs(void)
802 struct sha1_array sa = SHA1_ARRAY_INIT;
803 for_each_alternate_ref(collect_one_alternate_ref, &sa);
804 sha1_array_for_each_unique(&sa, add_one_alternate_sha1, NULL);
805 sha1_array_clear(&sa);
808 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
810 int advertise_refs = 0;
811 int stateless_rpc = 0;
812 int i;
813 char *dir = NULL;
814 struct command *commands;
816 packet_trace_identity("receive-pack");
818 argv++;
819 for (i = 1; i < argc; i++) {
820 const char *arg = *argv++;
822 if (*arg == '-') {
823 if (!strcmp(arg, "--advertise-refs")) {
824 advertise_refs = 1;
825 continue;
827 if (!strcmp(arg, "--stateless-rpc")) {
828 stateless_rpc = 1;
829 continue;
832 usage(receive_pack_usage);
834 if (dir)
835 usage(receive_pack_usage);
836 dir = xstrdup(arg);
838 if (!dir)
839 usage(receive_pack_usage);
841 setup_path();
843 if (!enter_repo(dir, 0))
844 die("'%s' does not appear to be a git repository", dir);
846 if (is_repository_shallow())
847 die("attempt to push into a shallow repository");
849 git_config(receive_pack_config, NULL);
851 if (0 <= transfer_unpack_limit)
852 unpack_limit = transfer_unpack_limit;
853 else if (0 <= receive_unpack_limit)
854 unpack_limit = receive_unpack_limit;
856 if (advertise_refs || !stateless_rpc) {
857 add_alternate_refs();
858 write_head_info();
859 clear_extra_refs();
861 /* EOF */
862 packet_flush(1);
864 if (advertise_refs)
865 return 0;
867 if ((commands = read_head_info()) != NULL) {
868 const char *unpack_status = NULL;
870 if (!delete_only(commands))
871 unpack_status = unpack();
872 execute_commands(commands, unpack_status);
873 if (pack_lockfile)
874 unlink_or_warn(pack_lockfile);
875 if (report_status)
876 report(commands, unpack_status);
877 run_receive_hook(commands, post_receive_hook);
878 run_update_post_hook(commands);
879 if (auto_gc) {
880 const char *argv_gc_auto[] = {
881 "gc", "--auto", "--quiet", NULL,
883 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
885 if (auto_update_server_info)
886 update_server_info(0);
888 if (use_sideband)
889 packet_flush(1);
890 return 0;