git-cvsexportcommit: Fix calling Perl's rel2abs() on MSYS
[git/dscho.git] / builtin / receive-pack.c
blobde9cd9083d6b13b5b4a324f2e9631d24b26a230d
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"
14 #include "connected.h"
16 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
18 enum deny_action {
19 DENY_UNCONFIGURED,
20 DENY_IGNORE,
21 DENY_WARN,
22 DENY_REFUSE,
23 DENY_UPDATE_INSTEAD,
24 DENY_DETACH_INSTEAD,
27 static int deny_deletes;
28 static int deny_non_fast_forwards;
29 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
30 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
31 static int receive_fsck_objects = -1;
32 static int transfer_fsck_objects = -1;
33 static int receive_unpack_limit = -1;
34 static int transfer_unpack_limit = -1;
35 static int unpack_limit = 100;
36 static int report_status;
37 static int use_sideband;
38 static int prefer_ofs_delta = 1;
39 static int auto_update_server_info;
40 static int auto_gc = 1;
41 static const char *head_name;
42 static void *head_name_to_free;
43 static int sent_capabilities;
45 static enum deny_action parse_deny_action(const char *var, const char *value)
47 if (value) {
48 if (!strcasecmp(value, "ignore"))
49 return DENY_IGNORE;
50 if (!strcasecmp(value, "warn"))
51 return DENY_WARN;
52 if (!strcasecmp(value, "refuse"))
53 return DENY_REFUSE;
55 if (git_config_bool(var, value))
56 return DENY_REFUSE;
57 return DENY_IGNORE;
60 static int receive_pack_config(const char *var, const char *value, void *cb)
62 if (strcmp(var, "receive.denydeletes") == 0) {
63 deny_deletes = git_config_bool(var, value);
64 return 0;
67 if (strcmp(var, "receive.denynonfastforwards") == 0) {
68 deny_non_fast_forwards = git_config_bool(var, value);
69 return 0;
72 if (strcmp(var, "receive.unpacklimit") == 0) {
73 receive_unpack_limit = git_config_int(var, value);
74 return 0;
77 if (strcmp(var, "transfer.unpacklimit") == 0) {
78 transfer_unpack_limit = git_config_int(var, value);
79 return 0;
82 if (strcmp(var, "receive.fsckobjects") == 0) {
83 receive_fsck_objects = git_config_bool(var, value);
84 return 0;
87 if (strcmp(var, "transfer.fsckobjects") == 0) {
88 transfer_fsck_objects = git_config_bool(var, value);
89 return 0;
92 if (!strcmp(var, "receive.denycurrentbranch")) {
93 if (value && !strcasecmp(value, "updateinstead"))
94 deny_current_branch = DENY_UPDATE_INSTEAD;
95 else if (value && !strcasecmp(value, "detachinstead"))
96 deny_current_branch = DENY_DETACH_INSTEAD;
97 else
98 deny_current_branch = parse_deny_action(var, value);
99 return 0;
102 if (strcmp(var, "receive.denydeletecurrent") == 0) {
103 deny_delete_current = parse_deny_action(var, value);
104 return 0;
107 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
108 prefer_ofs_delta = git_config_bool(var, value);
109 return 0;
112 if (strcmp(var, "receive.updateserverinfo") == 0) {
113 auto_update_server_info = git_config_bool(var, value);
114 return 0;
117 if (strcmp(var, "receive.autogc") == 0) {
118 auto_gc = git_config_bool(var, value);
119 return 0;
122 return git_default_config(var, value, cb);
125 static void show_ref(const char *path, const unsigned char *sha1)
127 if (sent_capabilities)
128 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
129 else
130 packet_write(1, "%s %s%c%s%s\n",
131 sha1_to_hex(sha1), path, 0,
132 " report-status delete-refs side-band-64k",
133 prefer_ofs_delta ? " ofs-delta" : "");
134 sent_capabilities = 1;
137 static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused)
139 path = strip_namespace(path);
141 * Advertise refs outside our current namespace as ".have"
142 * refs, so that the client can use them to minimize data
143 * transfer but will otherwise ignore them. This happens to
144 * cover ".have" that are thrown in by add_one_alternate_ref()
145 * to mark histories that are complete in our alternates as
146 * well.
148 if (!path)
149 path = ".have";
150 show_ref(path, sha1);
151 return 0;
154 static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
156 show_ref(".have", sha1);
159 static void collect_one_alternate_ref(const struct ref *ref, void *data)
161 struct sha1_array *sa = data;
162 sha1_array_append(sa, ref->old_sha1);
165 static void write_head_info(void)
167 struct sha1_array sa = SHA1_ARRAY_INIT;
168 for_each_alternate_ref(collect_one_alternate_ref, &sa);
169 sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
170 sha1_array_clear(&sa);
171 for_each_ref(show_ref_cb, NULL);
172 if (!sent_capabilities)
173 show_ref("capabilities^{}", null_sha1);
175 /* EOF */
176 packet_flush(1);
179 struct command {
180 struct command *next;
181 const char *error_string;
182 unsigned int skip_update:1,
183 did_not_exist:1;
184 unsigned char old_sha1[20];
185 unsigned char new_sha1[20];
186 char ref_name[FLEX_ARRAY]; /* more */
189 static const char pre_receive_hook[] = "hooks/pre-receive";
190 static const char post_receive_hook[] = "hooks/post-receive";
192 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
193 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
195 static void report_message(const char *prefix, const char *err, va_list params)
197 int sz = strlen(prefix);
198 char msg[4096];
200 strncpy(msg, prefix, sz);
201 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
202 if (sz > (sizeof(msg) - 1))
203 sz = sizeof(msg) - 1;
204 msg[sz++] = '\n';
206 if (use_sideband)
207 send_sideband(1, 2, msg, sz, use_sideband);
208 else
209 xwrite(2, msg, sz);
212 static void rp_warning(const char *err, ...)
214 va_list params;
215 va_start(params, err);
216 report_message("warning: ", err, params);
217 va_end(params);
220 static void rp_error(const char *err, ...)
222 va_list params;
223 va_start(params, err);
224 report_message("error: ", err, params);
225 va_end(params);
228 static int copy_to_sideband(int in, int out, void *arg)
230 char data[128];
231 while (1) {
232 ssize_t sz = xread(in, data, sizeof(data));
233 if (sz <= 0)
234 break;
235 send_sideband(1, 2, data, sz, use_sideband);
237 close(in);
238 return 0;
241 typedef int (*feed_fn)(void *, const char **, size_t *);
242 static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
244 struct child_process proc;
245 struct async muxer;
246 const char *argv[2];
247 int code;
249 if (access(hook_name, X_OK) < 0)
250 return 0;
252 argv[0] = hook_name;
253 argv[1] = NULL;
255 memset(&proc, 0, sizeof(proc));
256 proc.argv = argv;
257 proc.in = -1;
258 proc.stdout_to_stderr = 1;
260 if (use_sideband) {
261 memset(&muxer, 0, sizeof(muxer));
262 muxer.proc = copy_to_sideband;
263 muxer.in = -1;
264 code = start_async(&muxer);
265 if (code)
266 return code;
267 proc.err = muxer.in;
270 code = start_command(&proc);
271 if (code) {
272 if (use_sideband)
273 finish_async(&muxer);
274 return code;
277 while (1) {
278 const char *buf;
279 size_t n;
280 if (feed(feed_state, &buf, &n))
281 break;
282 if (write_in_full(proc.in, buf, n) != n)
283 break;
285 close(proc.in);
286 if (use_sideband)
287 finish_async(&muxer);
288 return finish_command(&proc);
291 struct receive_hook_feed_state {
292 struct command *cmd;
293 int skip_broken;
294 struct strbuf buf;
297 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
299 struct receive_hook_feed_state *state = state_;
300 struct command *cmd = state->cmd;
302 while (cmd &&
303 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
304 cmd = cmd->next;
305 if (!cmd)
306 return -1; /* EOF */
307 strbuf_reset(&state->buf);
308 strbuf_addf(&state->buf, "%s %s %s\n",
309 sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
310 cmd->ref_name);
311 state->cmd = cmd->next;
312 if (bufp) {
313 *bufp = state->buf.buf;
314 *sizep = state->buf.len;
316 return 0;
319 static int run_receive_hook(struct command *commands, const char *hook_name,
320 int skip_broken)
322 struct receive_hook_feed_state state;
323 int status;
325 strbuf_init(&state.buf, 0);
326 state.cmd = commands;
327 state.skip_broken = skip_broken;
328 if (feed_receive_hook(&state, NULL, NULL))
329 return 0;
330 state.cmd = commands;
331 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
332 strbuf_release(&state.buf);
333 return status;
336 static int run_update_hook(struct command *cmd)
338 static const char update_hook[] = "hooks/update";
339 const char *argv[5];
340 struct child_process proc;
341 int code;
343 if (access(update_hook, X_OK) < 0)
344 return 0;
346 argv[0] = update_hook;
347 argv[1] = cmd->ref_name;
348 argv[2] = sha1_to_hex(cmd->old_sha1);
349 argv[3] = sha1_to_hex(cmd->new_sha1);
350 argv[4] = NULL;
352 memset(&proc, 0, sizeof(proc));
353 proc.no_stdin = 1;
354 proc.stdout_to_stderr = 1;
355 proc.err = use_sideband ? -1 : 0;
356 proc.argv = argv;
358 code = start_command(&proc);
359 if (code)
360 return code;
361 if (use_sideband)
362 copy_to_sideband(proc.err, -1, NULL);
363 return finish_command(&proc);
366 static int is_ref_checked_out(const char *ref)
368 if (is_bare_repository())
369 return 0;
371 if (!head_name)
372 return 0;
373 return !strcmp(head_name, ref);
376 static char *refuse_unconfigured_deny_msg[] = {
377 "By default, updating the current branch in a non-bare repository",
378 "is denied, because it will make the index and work tree inconsistent",
379 "with what you pushed, and will require 'git reset --hard' to match",
380 "the work tree to HEAD.",
382 "You can set 'receive.denyCurrentBranch' configuration variable to",
383 "'ignore' or 'warn' in the remote repository to allow pushing into",
384 "its current branch; however, this is not recommended unless you",
385 "arranged to update its work tree to match what you pushed in some",
386 "other way.",
388 "To squelch this message and still keep the default behaviour, set",
389 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
392 static void refuse_unconfigured_deny(void)
394 int i;
395 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
396 rp_error("%s", refuse_unconfigured_deny_msg[i]);
399 static char *refuse_unconfigured_deny_delete_current_msg[] = {
400 "By default, deleting the current branch is denied, because the next",
401 "'git clone' won't result in any file checked out, causing confusion.",
403 "You can set 'receive.denyDeleteCurrent' configuration variable to",
404 "'warn' or 'ignore' in the remote repository to allow deleting the",
405 "current branch, with or without a warning message.",
407 "To squelch this message, you can set it to 'refuse'."
410 static void refuse_unconfigured_deny_delete_current(void)
412 int i;
413 for (i = 0;
414 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
415 i++)
416 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
419 static void merge_worktree(unsigned char *sha1)
421 const char *update_refresh[] = {
422 "update-index", "--ignore-submodules", "--refresh", NULL
424 const char *read_tree[] = {
425 "read-tree", "-u", "-m", sha1_to_hex(sha1), NULL
427 struct child_process child;
428 struct strbuf git_env = STRBUF_INIT;
429 const char *env[2];
431 if (is_bare_repository())
432 die ("denyCurrentBranch = updateInstead needs a worktree");
434 strbuf_addf(&git_env, "GIT_DIR=%s", absolute_path(get_git_dir()));
435 env[0] = git_env.buf;
436 env[1] = NULL;
438 memset(&child, 0, sizeof(child));
439 child.argv = update_refresh;
440 child.env = env;
441 child.dir = git_work_tree_cfg ? git_work_tree_cfg : "..";
442 child.stdout_to_stderr = 1;
443 child.git_cmd = 1;
444 if (run_command(&child))
445 die ("Could not refresh the index");
447 child.argv = read_tree;
448 child.no_stdin = 1;
449 child.no_stdout = 1;
450 child.stdout_to_stderr = 0;
451 if (run_command(&child))
452 die ("Could not merge working tree with new HEAD. Good luck.");
454 strbuf_release(&git_env);
457 static const char *update(struct command *cmd)
459 const char *name = cmd->ref_name;
460 struct strbuf namespaced_name_buf = STRBUF_INIT;
461 const char *namespaced_name;
462 unsigned char *old_sha1 = cmd->old_sha1;
463 unsigned char *new_sha1 = cmd->new_sha1;
464 struct ref_lock *lock;
466 /* only refs/... are allowed */
467 if (prefixcmp(name, "refs/") || check_refname_format(name + 5, 0)) {
468 rp_error("refusing to create funny ref '%s' remotely", name);
469 return "funny refname";
472 strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
473 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
475 if (is_ref_checked_out(namespaced_name)) {
476 switch (deny_current_branch) {
477 case DENY_IGNORE:
478 break;
479 case DENY_WARN:
480 rp_warning("updating the current branch");
481 break;
482 case DENY_REFUSE:
483 case DENY_UNCONFIGURED:
484 rp_error("refusing to update checked out branch: %s", name);
485 if (deny_current_branch == DENY_UNCONFIGURED)
486 refuse_unconfigured_deny();
487 return "branch is currently checked out";
488 case DENY_UPDATE_INSTEAD:
489 merge_worktree(new_sha1);
490 break;
491 case DENY_DETACH_INSTEAD:
492 update_ref("push into current branch (detach)", "HEAD",
493 old_sha1, NULL, REF_NODEREF, DIE_ON_ERR);
494 break;
498 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
499 error("unpack should have generated %s, "
500 "but I can't find it!", sha1_to_hex(new_sha1));
501 return "bad pack";
504 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
505 if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
506 rp_error("denying ref deletion for %s", name);
507 return "deletion prohibited";
510 if (!strcmp(namespaced_name, head_name)) {
511 switch (deny_delete_current) {
512 case DENY_IGNORE:
513 break;
514 case DENY_WARN:
515 rp_warning("deleting the current branch");
516 break;
517 case DENY_REFUSE:
518 case DENY_UNCONFIGURED:
519 if (deny_delete_current == DENY_UNCONFIGURED)
520 refuse_unconfigured_deny_delete_current();
521 rp_error("refusing to delete the current branch: %s", name);
522 return "deletion of the current branch prohibited";
523 default:
524 die ("Invalid denyDeleteCurrent setting");
529 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
530 !is_null_sha1(old_sha1) &&
531 !prefixcmp(name, "refs/heads/")) {
532 struct object *old_object, *new_object;
533 struct commit *old_commit, *new_commit;
534 struct commit_list *bases, *ent;
536 old_object = parse_object(old_sha1);
537 new_object = parse_object(new_sha1);
539 if (!old_object || !new_object ||
540 old_object->type != OBJ_COMMIT ||
541 new_object->type != OBJ_COMMIT) {
542 error("bad sha1 objects for %s", name);
543 return "bad ref";
545 old_commit = (struct commit *)old_object;
546 new_commit = (struct commit *)new_object;
547 bases = get_merge_bases(old_commit, new_commit, 1);
548 for (ent = bases; ent; ent = ent->next)
549 if (!hashcmp(old_sha1, ent->item->object.sha1))
550 break;
551 free_commit_list(bases);
552 if (!ent) {
553 rp_error("denying non-fast-forward %s"
554 " (you should pull first)", name);
555 return "non-fast-forward";
558 if (run_update_hook(cmd)) {
559 rp_error("hook declined to update %s", name);
560 return "hook declined";
563 if (is_null_sha1(new_sha1)) {
564 if (!parse_object(old_sha1)) {
565 old_sha1 = NULL;
566 if (ref_exists(name)) {
567 rp_warning("Allowing deletion of corrupt ref.");
568 } else {
569 rp_warning("Deleting a non-existent ref.");
570 cmd->did_not_exist = 1;
573 if (delete_ref(namespaced_name, old_sha1, 0)) {
574 rp_error("failed to delete %s", name);
575 return "failed to delete";
577 return NULL; /* good */
579 else {
580 lock = lock_any_ref_for_update(namespaced_name, old_sha1, 0);
581 if (!lock) {
582 rp_error("failed to lock %s", name);
583 return "failed to lock";
585 if (write_ref_sha1(lock, new_sha1, "push")) {
586 return "failed to write"; /* error() already called */
588 return NULL; /* good */
592 static char update_post_hook[] = "hooks/post-update";
594 static void run_update_post_hook(struct command *commands)
596 struct command *cmd;
597 int argc;
598 const char **argv;
599 struct child_process proc;
601 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
602 if (cmd->error_string || cmd->did_not_exist)
603 continue;
604 argc++;
606 if (!argc || access(update_post_hook, X_OK) < 0)
607 return;
608 argv = xmalloc(sizeof(*argv) * (2 + argc));
609 argv[0] = update_post_hook;
611 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
612 char *p;
613 if (cmd->error_string || cmd->did_not_exist)
614 continue;
615 p = xmalloc(strlen(cmd->ref_name) + 1);
616 strcpy(p, cmd->ref_name);
617 argv[argc] = p;
618 argc++;
620 argv[argc] = NULL;
622 memset(&proc, 0, sizeof(proc));
623 proc.no_stdin = 1;
624 proc.stdout_to_stderr = 1;
625 proc.err = use_sideband ? -1 : 0;
626 proc.argv = argv;
628 if (!start_command(&proc)) {
629 if (use_sideband)
630 copy_to_sideband(proc.err, -1, NULL);
631 finish_command(&proc);
635 static void check_aliased_update(struct command *cmd, struct string_list *list)
637 struct strbuf buf = STRBUF_INIT;
638 const char *dst_name;
639 struct string_list_item *item;
640 struct command *dst_cmd;
641 unsigned char sha1[20];
642 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
643 int flag;
645 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
646 dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
647 strbuf_release(&buf);
649 if (!(flag & REF_ISSYMREF))
650 return;
652 dst_name = strip_namespace(dst_name);
653 if (!dst_name) {
654 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
655 cmd->skip_update = 1;
656 cmd->error_string = "broken symref";
657 return;
660 if ((item = string_list_lookup(list, dst_name)) == NULL)
661 return;
663 cmd->skip_update = 1;
665 dst_cmd = (struct command *) item->util;
667 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
668 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
669 return;
671 dst_cmd->skip_update = 1;
673 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
674 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
675 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
676 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
677 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
678 " its target '%s' (%s..%s)",
679 cmd->ref_name, cmd_oldh, cmd_newh,
680 dst_cmd->ref_name, dst_oldh, dst_newh);
682 cmd->error_string = dst_cmd->error_string =
683 "inconsistent aliased update";
686 static void check_aliased_updates(struct command *commands)
688 struct command *cmd;
689 struct string_list ref_list = STRING_LIST_INIT_NODUP;
691 for (cmd = commands; cmd; cmd = cmd->next) {
692 struct string_list_item *item =
693 string_list_append(&ref_list, cmd->ref_name);
694 item->util = (void *)cmd;
696 sort_string_list(&ref_list);
698 for (cmd = commands; cmd; cmd = cmd->next)
699 check_aliased_update(cmd, &ref_list);
701 string_list_clear(&ref_list, 0);
704 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
706 struct command **cmd_list = cb_data;
707 struct command *cmd = *cmd_list;
709 if (!cmd || is_null_sha1(cmd->new_sha1))
710 return -1; /* end of list */
711 *cmd_list = NULL; /* this returns only one */
712 hashcpy(sha1, cmd->new_sha1);
713 return 0;
716 static void set_connectivity_errors(struct command *commands)
718 struct command *cmd;
720 for (cmd = commands; cmd; cmd = cmd->next) {
721 struct command *singleton = cmd;
722 if (!check_everything_connected(command_singleton_iterator,
723 0, &singleton))
724 continue;
725 cmd->error_string = "missing necessary objects";
729 static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
731 struct command **cmd_list = cb_data;
732 struct command *cmd = *cmd_list;
734 while (cmd) {
735 if (!is_null_sha1(cmd->new_sha1)) {
736 hashcpy(sha1, cmd->new_sha1);
737 *cmd_list = cmd->next;
738 return 0;
740 cmd = cmd->next;
742 *cmd_list = NULL;
743 return -1; /* end of list */
746 static void execute_commands(struct command *commands, const char *unpacker_error)
748 struct command *cmd;
749 unsigned char sha1[20];
751 if (unpacker_error) {
752 for (cmd = commands; cmd; cmd = cmd->next)
753 cmd->error_string = "n/a (unpacker error)";
754 return;
757 cmd = commands;
758 if (check_everything_connected(iterate_receive_command_list,
759 0, &cmd))
760 set_connectivity_errors(commands);
762 if (run_receive_hook(commands, pre_receive_hook, 0)) {
763 for (cmd = commands; cmd; cmd = cmd->next)
764 cmd->error_string = "pre-receive hook declined";
765 return;
768 check_aliased_updates(commands);
770 free(head_name_to_free);
771 head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
773 for (cmd = commands; cmd; cmd = cmd->next)
774 if (!cmd->skip_update)
775 cmd->error_string = update(cmd);
778 static struct command *read_head_info(void)
780 struct command *commands = NULL;
781 struct command **p = &commands;
782 for (;;) {
783 static char line[1000];
784 unsigned char old_sha1[20], new_sha1[20];
785 struct command *cmd;
786 char *refname;
787 int len, reflen;
789 len = packet_read_line(0, line, sizeof(line));
790 if (!len)
791 break;
792 if (line[len-1] == '\n')
793 line[--len] = 0;
794 if (len < 83 ||
795 line[40] != ' ' ||
796 line[81] != ' ' ||
797 get_sha1_hex(line, old_sha1) ||
798 get_sha1_hex(line + 41, new_sha1))
799 die("protocol error: expected old/new/ref, got '%s'",
800 line);
802 refname = line + 82;
803 reflen = strlen(refname);
804 if (reflen + 82 < len) {
805 if (strstr(refname + reflen + 1, "report-status"))
806 report_status = 1;
807 if (strstr(refname + reflen + 1, "side-band-64k"))
808 use_sideband = LARGE_PACKET_MAX;
810 cmd = xcalloc(1, sizeof(struct command) + len - 80);
811 hashcpy(cmd->old_sha1, old_sha1);
812 hashcpy(cmd->new_sha1, new_sha1);
813 memcpy(cmd->ref_name, line + 82, len - 81);
814 *p = cmd;
815 p = &cmd->next;
817 return commands;
820 static const char *parse_pack_header(struct pack_header *hdr)
822 switch (read_pack_header(0, hdr)) {
823 case PH_ERROR_EOF:
824 return "eof before pack header was fully read";
826 case PH_ERROR_PACK_SIGNATURE:
827 return "protocol error (pack signature mismatch detected)";
829 case PH_ERROR_PROTOCOL:
830 return "protocol error (pack version unsupported)";
832 default:
833 return "unknown error in parse_pack_header";
835 case 0:
836 return NULL;
840 static const char *pack_lockfile;
842 static const char *unpack(void)
844 struct pack_header hdr;
845 const char *hdr_err;
846 char hdr_arg[38];
847 int fsck_objects = (receive_fsck_objects >= 0
848 ? receive_fsck_objects
849 : transfer_fsck_objects >= 0
850 ? transfer_fsck_objects
851 : 0);
853 hdr_err = parse_pack_header(&hdr);
854 if (hdr_err)
855 return hdr_err;
856 snprintf(hdr_arg, sizeof(hdr_arg),
857 "--pack_header=%"PRIu32",%"PRIu32,
858 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
860 if (ntohl(hdr.hdr_entries) < unpack_limit) {
861 int code, i = 0;
862 const char *unpacker[4];
863 unpacker[i++] = "unpack-objects";
864 if (fsck_objects)
865 unpacker[i++] = "--strict";
866 unpacker[i++] = hdr_arg;
867 unpacker[i++] = NULL;
868 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
869 if (!code)
870 return NULL;
871 return "unpack-objects abnormal exit";
872 } else {
873 const char *keeper[7];
874 int s, status, i = 0;
875 char keep_arg[256];
876 struct child_process ip;
878 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
879 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
880 strcpy(keep_arg + s, "localhost");
882 keeper[i++] = "index-pack";
883 keeper[i++] = "--stdin";
884 if (fsck_objects)
885 keeper[i++] = "--strict";
886 keeper[i++] = "--fix-thin";
887 keeper[i++] = hdr_arg;
888 keeper[i++] = keep_arg;
889 keeper[i++] = NULL;
890 memset(&ip, 0, sizeof(ip));
891 ip.argv = keeper;
892 ip.out = -1;
893 ip.git_cmd = 1;
894 status = start_command(&ip);
895 if (status) {
896 return "index-pack fork failed";
898 pack_lockfile = index_pack_lockfile(ip.out);
899 close(ip.out);
900 status = finish_command(&ip);
901 if (!status) {
902 reprepare_packed_git();
903 return NULL;
905 return "index-pack abnormal exit";
909 static void report(struct command *commands, const char *unpack_status)
911 struct command *cmd;
912 struct strbuf buf = STRBUF_INIT;
914 packet_buf_write(&buf, "unpack %s\n",
915 unpack_status ? unpack_status : "ok");
916 for (cmd = commands; cmd; cmd = cmd->next) {
917 if (!cmd->error_string)
918 packet_buf_write(&buf, "ok %s\n",
919 cmd->ref_name);
920 else
921 packet_buf_write(&buf, "ng %s %s\n",
922 cmd->ref_name, cmd->error_string);
924 packet_buf_flush(&buf);
926 if (use_sideband)
927 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
928 else
929 safe_write(1, buf.buf, buf.len);
930 strbuf_release(&buf);
933 static int delete_only(struct command *commands)
935 struct command *cmd;
936 for (cmd = commands; cmd; cmd = cmd->next) {
937 if (!is_null_sha1(cmd->new_sha1))
938 return 0;
940 return 1;
943 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
945 int advertise_refs = 0;
946 int stateless_rpc = 0;
947 int i;
948 char *dir = NULL;
949 struct command *commands;
951 packet_trace_identity("receive-pack");
953 argv++;
954 for (i = 1; i < argc; i++) {
955 const char *arg = *argv++;
957 if (*arg == '-') {
958 if (!strcmp(arg, "--advertise-refs")) {
959 advertise_refs = 1;
960 continue;
962 if (!strcmp(arg, "--stateless-rpc")) {
963 stateless_rpc = 1;
964 continue;
967 usage(receive_pack_usage);
969 if (dir)
970 usage(receive_pack_usage);
971 dir = xstrdup(arg);
973 if (!dir)
974 usage(receive_pack_usage);
976 setup_path();
978 if (!enter_repo(dir, 0))
979 die("'%s' does not appear to be a git repository", dir);
981 if (is_repository_shallow())
982 die("attempt to push into a shallow repository");
984 git_config(receive_pack_config, NULL);
986 if (0 <= transfer_unpack_limit)
987 unpack_limit = transfer_unpack_limit;
988 else if (0 <= receive_unpack_limit)
989 unpack_limit = receive_unpack_limit;
991 if (advertise_refs || !stateless_rpc) {
992 write_head_info();
994 if (advertise_refs)
995 return 0;
997 if ((commands = read_head_info()) != NULL) {
998 const char *unpack_status = NULL;
1000 if (!delete_only(commands))
1001 unpack_status = unpack();
1002 execute_commands(commands, unpack_status);
1003 if (pack_lockfile)
1004 unlink_or_warn(pack_lockfile);
1005 if (report_status)
1006 report(commands, unpack_status);
1007 run_receive_hook(commands, post_receive_hook, 1);
1008 run_update_post_hook(commands);
1009 if (auto_gc) {
1010 const char *argv_gc_auto[] = {
1011 "gc", "--auto", "--quiet", NULL,
1013 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
1015 if (auto_update_server_info)
1016 update_server_info(0);
1018 if (use_sideband)
1019 packet_flush(1);
1020 return 0;