6 #include "run-command.h"
12 #include "transport.h"
13 #include "string-list.h"
14 #include "sha1-array.h"
15 #include "connected.h"
16 #include "argv-array.h"
19 static const char receive_pack_usage
[] = "git receive-pack <git-dir>";
30 static int deny_deletes
;
31 static int deny_non_fast_forwards
;
32 static enum deny_action deny_current_branch
= DENY_UNCONFIGURED
;
33 static enum deny_action deny_delete_current
= DENY_UNCONFIGURED
;
34 static int receive_fsck_objects
= -1;
35 static int transfer_fsck_objects
= -1;
36 static int receive_unpack_limit
= -1;
37 static int transfer_unpack_limit
= -1;
38 static int unpack_limit
= 100;
39 static int report_status
;
40 static int use_sideband
;
42 static int prefer_ofs_delta
= 1;
43 static int auto_update_server_info
;
44 static int auto_gc
= 1;
45 static int fix_thin
= 1;
46 static const char *head_name
;
47 static void *head_name_to_free
;
48 static int sent_capabilities
;
49 static int shallow_update
;
50 static const char *alt_shallow_file
;
52 static enum deny_action
parse_deny_action(const char *var
, const char *value
)
55 if (!strcasecmp(value
, "ignore"))
57 if (!strcasecmp(value
, "warn"))
59 if (!strcasecmp(value
, "refuse"))
62 if (git_config_bool(var
, value
))
67 static int receive_pack_config(const char *var
, const char *value
, void *cb
)
69 int status
= parse_hide_refs_config(var
, value
, "receive");
74 if (strcmp(var
, "receive.denydeletes") == 0) {
75 deny_deletes
= git_config_bool(var
, value
);
79 if (strcmp(var
, "receive.denynonfastforwards") == 0) {
80 deny_non_fast_forwards
= git_config_bool(var
, value
);
84 if (strcmp(var
, "receive.unpacklimit") == 0) {
85 receive_unpack_limit
= git_config_int(var
, value
);
89 if (strcmp(var
, "transfer.unpacklimit") == 0) {
90 transfer_unpack_limit
= git_config_int(var
, value
);
94 if (strcmp(var
, "receive.fsckobjects") == 0) {
95 receive_fsck_objects
= git_config_bool(var
, value
);
99 if (strcmp(var
, "transfer.fsckobjects") == 0) {
100 transfer_fsck_objects
= git_config_bool(var
, value
);
104 if (!strcmp(var
, "receive.denycurrentbranch")) {
105 if (value
&& !strcasecmp(value
, "updateinstead"))
106 deny_current_branch
= DENY_UPDATE_INSTEAD
;
107 else if (value
&& !strcasecmp(value
, "detachinstead"))
108 deny_current_branch
= DENY_DETACH_INSTEAD
;
110 deny_current_branch
= parse_deny_action(var
, value
);
114 if (strcmp(var
, "receive.denydeletecurrent") == 0) {
115 deny_delete_current
= parse_deny_action(var
, value
);
119 if (strcmp(var
, "repack.usedeltabaseoffset") == 0) {
120 prefer_ofs_delta
= git_config_bool(var
, value
);
124 if (strcmp(var
, "receive.updateserverinfo") == 0) {
125 auto_update_server_info
= git_config_bool(var
, value
);
129 if (strcmp(var
, "receive.autogc") == 0) {
130 auto_gc
= git_config_bool(var
, value
);
134 if (strcmp(var
, "receive.shallowupdate") == 0) {
135 shallow_update
= git_config_bool(var
, value
);
139 return git_default_config(var
, value
, cb
);
142 static void show_ref(const char *path
, const unsigned char *sha1
)
144 if (ref_is_hidden(path
))
147 if (sent_capabilities
)
148 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
150 packet_write(1, "%s %s%c%s%s agent=%s\n",
151 sha1_to_hex(sha1
), path
, 0,
152 " report-status delete-refs side-band-64k quiet",
153 prefer_ofs_delta
? " ofs-delta" : "",
154 git_user_agent_sanitized());
155 sent_capabilities
= 1;
158 static int show_ref_cb(const char *path
, const unsigned char *sha1
, int flag
, void *unused
)
160 path
= strip_namespace(path
);
162 * Advertise refs outside our current namespace as ".have"
163 * refs, so that the client can use them to minimize data
164 * transfer but will otherwise ignore them. This happens to
165 * cover ".have" that are thrown in by add_one_alternate_ref()
166 * to mark histories that are complete in our alternates as
171 show_ref(path
, sha1
);
175 static void show_one_alternate_sha1(const unsigned char sha1
[20], void *unused
)
177 show_ref(".have", sha1
);
180 static void collect_one_alternate_ref(const struct ref
*ref
, void *data
)
182 struct sha1_array
*sa
= data
;
183 sha1_array_append(sa
, ref
->old_sha1
);
186 static void write_head_info(void)
188 struct sha1_array sa
= SHA1_ARRAY_INIT
;
189 for_each_alternate_ref(collect_one_alternate_ref
, &sa
);
190 sha1_array_for_each_unique(&sa
, show_one_alternate_sha1
, NULL
);
191 sha1_array_clear(&sa
);
192 for_each_ref(show_ref_cb
, NULL
);
193 if (!sent_capabilities
)
194 show_ref("capabilities^{}", null_sha1
);
196 advertise_shallow_grafts(1);
203 struct command
*next
;
204 const char *error_string
;
205 unsigned int skip_update
:1,
208 unsigned char old_sha1
[20];
209 unsigned char new_sha1
[20];
210 char ref_name
[FLEX_ARRAY
]; /* more */
213 static void rp_error(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
214 static void rp_warning(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
216 static void report_message(const char *prefix
, const char *err
, va_list params
)
218 int sz
= strlen(prefix
);
221 strncpy(msg
, prefix
, sz
);
222 sz
+= vsnprintf(msg
+ sz
, sizeof(msg
) - sz
, err
, params
);
223 if (sz
> (sizeof(msg
) - 1))
224 sz
= sizeof(msg
) - 1;
228 send_sideband(1, 2, msg
, sz
, use_sideband
);
233 static void rp_warning(const char *err
, ...)
236 va_start(params
, err
);
237 report_message("warning: ", err
, params
);
241 static void rp_error(const char *err
, ...)
244 va_start(params
, err
);
245 report_message("error: ", err
, params
);
249 static int copy_to_sideband(int in
, int out
, void *arg
)
253 ssize_t sz
= xread(in
, data
, sizeof(data
));
256 send_sideband(1, 2, data
, sz
, use_sideband
);
262 typedef int (*feed_fn
)(void *, const char **, size_t *);
263 static int run_and_feed_hook(const char *hook_name
, feed_fn feed
, void *feed_state
)
265 struct child_process proc
;
270 argv
[0] = find_hook(hook_name
);
276 memset(&proc
, 0, sizeof(proc
));
279 proc
.stdout_to_stderr
= 1;
282 memset(&muxer
, 0, sizeof(muxer
));
283 muxer
.proc
= copy_to_sideband
;
285 code
= start_async(&muxer
);
291 code
= start_command(&proc
);
294 finish_async(&muxer
);
301 if (feed(feed_state
, &buf
, &n
))
303 if (write_in_full(proc
.in
, buf
, n
) != n
)
308 finish_async(&muxer
);
309 return finish_command(&proc
);
312 struct receive_hook_feed_state
{
318 static int feed_receive_hook(void *state_
, const char **bufp
, size_t *sizep
)
320 struct receive_hook_feed_state
*state
= state_
;
321 struct command
*cmd
= state
->cmd
;
324 state
->skip_broken
&& (cmd
->error_string
|| cmd
->did_not_exist
))
328 strbuf_reset(&state
->buf
);
329 strbuf_addf(&state
->buf
, "%s %s %s\n",
330 sha1_to_hex(cmd
->old_sha1
), sha1_to_hex(cmd
->new_sha1
),
332 state
->cmd
= cmd
->next
;
334 *bufp
= state
->buf
.buf
;
335 *sizep
= state
->buf
.len
;
340 static int run_receive_hook(struct command
*commands
, const char *hook_name
,
343 struct receive_hook_feed_state state
;
346 strbuf_init(&state
.buf
, 0);
347 state
.cmd
= commands
;
348 state
.skip_broken
= skip_broken
;
349 if (feed_receive_hook(&state
, NULL
, NULL
))
351 state
.cmd
= commands
;
352 status
= run_and_feed_hook(hook_name
, feed_receive_hook
, &state
);
353 strbuf_release(&state
.buf
);
357 static int run_update_hook(struct command
*cmd
)
360 struct child_process proc
;
363 argv
[0] = find_hook("update");
367 argv
[1] = cmd
->ref_name
;
368 argv
[2] = sha1_to_hex(cmd
->old_sha1
);
369 argv
[3] = sha1_to_hex(cmd
->new_sha1
);
372 memset(&proc
, 0, sizeof(proc
));
374 proc
.stdout_to_stderr
= 1;
375 proc
.err
= use_sideband
? -1 : 0;
378 code
= start_command(&proc
);
382 copy_to_sideband(proc
.err
, -1, NULL
);
383 return finish_command(&proc
);
386 static int is_ref_checked_out(const char *ref
)
388 if (is_bare_repository())
393 return !strcmp(head_name
, ref
);
396 static char *refuse_unconfigured_deny_msg
[] = {
397 "By default, updating the current branch in a non-bare repository",
398 "is denied, because it will make the index and work tree inconsistent",
399 "with what you pushed, and will require 'git reset --hard' to match",
400 "the work tree to HEAD.",
402 "You can set 'receive.denyCurrentBranch' configuration variable to",
403 "'ignore' or 'warn' in the remote repository to allow pushing into",
404 "its current branch; however, this is not recommended unless you",
405 "arranged to update its work tree to match what you pushed in some",
408 "To squelch this message and still keep the default behaviour, set",
409 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
412 static void refuse_unconfigured_deny(void)
415 for (i
= 0; i
< ARRAY_SIZE(refuse_unconfigured_deny_msg
); i
++)
416 rp_error("%s", refuse_unconfigured_deny_msg
[i
]);
419 static char *refuse_unconfigured_deny_delete_current_msg
[] = {
420 "By default, deleting the current branch is denied, because the next",
421 "'git clone' won't result in any file checked out, causing confusion.",
423 "You can set 'receive.denyDeleteCurrent' configuration variable to",
424 "'warn' or 'ignore' in the remote repository to allow deleting the",
425 "current branch, with or without a warning message.",
427 "To squelch this message, you can set it to 'refuse'."
430 static void refuse_unconfigured_deny_delete_current(void)
434 i
< ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg
);
436 rp_error("%s", refuse_unconfigured_deny_delete_current_msg
[i
]);
439 static int command_singleton_iterator(void *cb_data
, unsigned char sha1
[20]);
440 static int update_shallow_ref(struct command
*cmd
, struct shallow_info
*si
)
442 static struct lock_file shallow_lock
;
443 struct sha1_array extra
= SHA1_ARRAY_INIT
;
444 const char *alt_file
;
445 uint32_t mask
= 1 << (cmd
->index
% 32);
448 trace_printf_key(&trace_shallow
,
449 "shallow: update_shallow_ref %s\n", cmd
->ref_name
);
450 for (i
= 0; i
< si
->shallow
->nr
; i
++)
451 if (si
->used_shallow
[i
] &&
452 (si
->used_shallow
[i
][cmd
->index
/ 32] & mask
) &&
453 !delayed_reachability_test(si
, i
))
454 sha1_array_append(&extra
, si
->shallow
->sha1
[i
]);
456 setup_alternate_shallow(&shallow_lock
, &alt_file
, &extra
);
457 if (check_shallow_connected(command_singleton_iterator
,
459 rollback_lock_file(&shallow_lock
);
460 sha1_array_clear(&extra
);
464 commit_lock_file(&shallow_lock
);
467 * Make sure setup_alternate_shallow() for the next ref does
468 * not lose these new roots..
470 for (i
= 0; i
< extra
.nr
; i
++)
471 register_shallow(extra
.sha1
[i
]);
473 si
->shallow_ref
[cmd
->index
] = 0;
474 sha1_array_clear(&extra
);
478 static void merge_worktree(unsigned char *sha1
)
480 const char *update_refresh
[] = {
481 "update-index", "--ignore-submodules", "--refresh", NULL
483 const char *read_tree
[] = {
484 "read-tree", "-u", "-m", sha1_to_hex(sha1
), NULL
486 struct child_process child
;
487 struct strbuf git_env
= STRBUF_INIT
;
490 if (is_bare_repository())
491 die ("denyCurrentBranch = updateInstead needs a worktree");
493 strbuf_addf(&git_env
, "GIT_DIR=%s", absolute_path(get_git_dir()));
494 env
[0] = git_env
.buf
;
497 memset(&child
, 0, sizeof(child
));
498 child
.argv
= update_refresh
;
500 child
.dir
= git_work_tree_cfg
? git_work_tree_cfg
: "..";
501 child
.stdout_to_stderr
= 1;
503 if (run_command(&child
))
504 die ("Could not refresh the index");
506 child
.argv
= read_tree
;
509 child
.stdout_to_stderr
= 0;
510 if (run_command(&child
))
511 die ("Could not merge working tree with new HEAD. Good luck.");
513 strbuf_release(&git_env
);
516 static const char *update(struct command
*cmd
, struct shallow_info
*si
)
518 const char *name
= cmd
->ref_name
;
519 struct strbuf namespaced_name_buf
= STRBUF_INIT
;
520 const char *namespaced_name
;
521 unsigned char *old_sha1
= cmd
->old_sha1
;
522 unsigned char *new_sha1
= cmd
->new_sha1
;
523 struct ref_lock
*lock
;
525 /* only refs/... are allowed */
526 if (!starts_with(name
, "refs/") || check_refname_format(name
+ 5, 0)) {
527 rp_error("refusing to create funny ref '%s' remotely", name
);
528 return "funny refname";
531 strbuf_addf(&namespaced_name_buf
, "%s%s", get_git_namespace(), name
);
532 namespaced_name
= strbuf_detach(&namespaced_name_buf
, NULL
);
534 if (is_ref_checked_out(namespaced_name
)) {
535 switch (deny_current_branch
) {
539 rp_warning("updating the current branch");
542 case DENY_UNCONFIGURED
:
543 rp_error("refusing to update checked out branch: %s", name
);
544 if (deny_current_branch
== DENY_UNCONFIGURED
)
545 refuse_unconfigured_deny();
546 return "branch is currently checked out";
547 case DENY_UPDATE_INSTEAD
:
548 merge_worktree(new_sha1
);
550 case DENY_DETACH_INSTEAD
:
551 update_ref("push into current branch (detach)", "HEAD",
552 old_sha1
, NULL
, REF_NODEREF
, UPDATE_REFS_DIE_ON_ERR
);
557 if (!is_null_sha1(new_sha1
) && !has_sha1_file(new_sha1
)) {
558 error("unpack should have generated %s, "
559 "but I can't find it!", sha1_to_hex(new_sha1
));
563 if (!is_null_sha1(old_sha1
) && is_null_sha1(new_sha1
)) {
564 if (deny_deletes
&& starts_with(name
, "refs/heads/")) {
565 rp_error("denying ref deletion for %s", name
);
566 return "deletion prohibited";
569 if (!strcmp(namespaced_name
, head_name
)) {
570 switch (deny_delete_current
) {
574 rp_warning("deleting the current branch");
577 case DENY_UNCONFIGURED
:
578 if (deny_delete_current
== DENY_UNCONFIGURED
)
579 refuse_unconfigured_deny_delete_current();
580 rp_error("refusing to delete the current branch: %s", name
);
581 return "deletion of the current branch prohibited";
583 die ("Invalid denyDeleteCurrent setting");
588 if (deny_non_fast_forwards
&& !is_null_sha1(new_sha1
) &&
589 !is_null_sha1(old_sha1
) &&
590 starts_with(name
, "refs/heads/")) {
591 struct object
*old_object
, *new_object
;
592 struct commit
*old_commit
, *new_commit
;
594 old_object
= parse_object(old_sha1
);
595 new_object
= parse_object(new_sha1
);
597 if (!old_object
|| !new_object
||
598 old_object
->type
!= OBJ_COMMIT
||
599 new_object
->type
!= OBJ_COMMIT
) {
600 error("bad sha1 objects for %s", name
);
603 old_commit
= (struct commit
*)old_object
;
604 new_commit
= (struct commit
*)new_object
;
605 if (!in_merge_bases(old_commit
, new_commit
)) {
606 rp_error("denying non-fast-forward %s"
607 " (you should pull first)", name
);
608 return "non-fast-forward";
611 if (run_update_hook(cmd
)) {
612 rp_error("hook declined to update %s", name
);
613 return "hook declined";
616 if (is_null_sha1(new_sha1
)) {
617 if (!parse_object(old_sha1
)) {
619 if (ref_exists(name
)) {
620 rp_warning("Allowing deletion of corrupt ref.");
622 rp_warning("Deleting a non-existent ref.");
623 cmd
->did_not_exist
= 1;
626 if (delete_ref(namespaced_name
, old_sha1
, 0)) {
627 rp_error("failed to delete %s", name
);
628 return "failed to delete";
630 return NULL
; /* good */
633 if (shallow_update
&& si
->shallow_ref
[cmd
->index
] &&
634 update_shallow_ref(cmd
, si
))
635 return "shallow error";
637 lock
= lock_any_ref_for_update(namespaced_name
, old_sha1
,
640 rp_error("failed to lock %s", name
);
641 return "failed to lock";
643 if (write_ref_sha1(lock
, new_sha1
, "push")) {
644 return "failed to write"; /* error() already called */
646 return NULL
; /* good */
650 static void run_update_post_hook(struct command
*commands
)
655 struct child_process proc
;
658 hook
= find_hook("post-update");
659 for (argc
= 0, cmd
= commands
; cmd
; cmd
= cmd
->next
) {
660 if (cmd
->error_string
|| cmd
->did_not_exist
)
667 argv
= xmalloc(sizeof(*argv
) * (2 + argc
));
670 for (argc
= 1, cmd
= commands
; cmd
; cmd
= cmd
->next
) {
671 if (cmd
->error_string
|| cmd
->did_not_exist
)
673 argv
[argc
] = xstrdup(cmd
->ref_name
);
678 memset(&proc
, 0, sizeof(proc
));
680 proc
.stdout_to_stderr
= 1;
681 proc
.err
= use_sideband
? -1 : 0;
684 if (!start_command(&proc
)) {
686 copy_to_sideband(proc
.err
, -1, NULL
);
687 finish_command(&proc
);
691 static void check_aliased_update(struct command
*cmd
, struct string_list
*list
)
693 struct strbuf buf
= STRBUF_INIT
;
694 const char *dst_name
;
695 struct string_list_item
*item
;
696 struct command
*dst_cmd
;
697 unsigned char sha1
[20];
698 char cmd_oldh
[41], cmd_newh
[41], dst_oldh
[41], dst_newh
[41];
701 strbuf_addf(&buf
, "%s%s", get_git_namespace(), cmd
->ref_name
);
702 dst_name
= resolve_ref_unsafe(buf
.buf
, sha1
, 0, &flag
);
703 strbuf_release(&buf
);
705 if (!(flag
& REF_ISSYMREF
))
708 dst_name
= strip_namespace(dst_name
);
710 rp_error("refusing update to broken symref '%s'", cmd
->ref_name
);
711 cmd
->skip_update
= 1;
712 cmd
->error_string
= "broken symref";
716 if ((item
= string_list_lookup(list
, dst_name
)) == NULL
)
719 cmd
->skip_update
= 1;
721 dst_cmd
= (struct command
*) item
->util
;
723 if (!hashcmp(cmd
->old_sha1
, dst_cmd
->old_sha1
) &&
724 !hashcmp(cmd
->new_sha1
, dst_cmd
->new_sha1
))
727 dst_cmd
->skip_update
= 1;
729 strcpy(cmd_oldh
, find_unique_abbrev(cmd
->old_sha1
, DEFAULT_ABBREV
));
730 strcpy(cmd_newh
, find_unique_abbrev(cmd
->new_sha1
, DEFAULT_ABBREV
));
731 strcpy(dst_oldh
, find_unique_abbrev(dst_cmd
->old_sha1
, DEFAULT_ABBREV
));
732 strcpy(dst_newh
, find_unique_abbrev(dst_cmd
->new_sha1
, DEFAULT_ABBREV
));
733 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
734 " its target '%s' (%s..%s)",
735 cmd
->ref_name
, cmd_oldh
, cmd_newh
,
736 dst_cmd
->ref_name
, dst_oldh
, dst_newh
);
738 cmd
->error_string
= dst_cmd
->error_string
=
739 "inconsistent aliased update";
742 static void check_aliased_updates(struct command
*commands
)
745 struct string_list ref_list
= STRING_LIST_INIT_NODUP
;
747 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
748 struct string_list_item
*item
=
749 string_list_append(&ref_list
, cmd
->ref_name
);
750 item
->util
= (void *)cmd
;
752 sort_string_list(&ref_list
);
754 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
755 if (!cmd
->error_string
)
756 check_aliased_update(cmd
, &ref_list
);
759 string_list_clear(&ref_list
, 0);
762 static int command_singleton_iterator(void *cb_data
, unsigned char sha1
[20])
764 struct command
**cmd_list
= cb_data
;
765 struct command
*cmd
= *cmd_list
;
767 if (!cmd
|| is_null_sha1(cmd
->new_sha1
))
768 return -1; /* end of list */
769 *cmd_list
= NULL
; /* this returns only one */
770 hashcpy(sha1
, cmd
->new_sha1
);
774 static void set_connectivity_errors(struct command
*commands
,
775 struct shallow_info
*si
)
779 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
780 struct command
*singleton
= cmd
;
781 if (shallow_update
&& si
->shallow_ref
[cmd
->index
])
782 /* to be checked in update_shallow_ref() */
784 if (!check_everything_connected(command_singleton_iterator
,
787 cmd
->error_string
= "missing necessary objects";
791 struct iterate_data
{
792 struct command
*cmds
;
793 struct shallow_info
*si
;
796 static int iterate_receive_command_list(void *cb_data
, unsigned char sha1
[20])
798 struct iterate_data
*data
= cb_data
;
799 struct command
**cmd_list
= &data
->cmds
;
800 struct command
*cmd
= *cmd_list
;
802 for (; cmd
; cmd
= cmd
->next
) {
803 if (shallow_update
&& data
->si
->shallow_ref
[cmd
->index
])
804 /* to be checked in update_shallow_ref() */
806 if (!is_null_sha1(cmd
->new_sha1
) && !cmd
->skip_update
) {
807 hashcpy(sha1
, cmd
->new_sha1
);
808 *cmd_list
= cmd
->next
;
813 return -1; /* end of list */
816 static void reject_updates_to_hidden(struct command
*commands
)
820 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
821 if (cmd
->error_string
|| !ref_is_hidden(cmd
->ref_name
))
823 if (is_null_sha1(cmd
->new_sha1
))
824 cmd
->error_string
= "deny deleting a hidden ref";
826 cmd
->error_string
= "deny updating a hidden ref";
830 static void execute_commands(struct command
*commands
,
831 const char *unpacker_error
,
832 struct shallow_info
*si
)
834 int checked_connectivity
;
836 unsigned char sha1
[20];
837 struct iterate_data data
;
839 if (unpacker_error
) {
840 for (cmd
= commands
; cmd
; cmd
= cmd
->next
)
841 cmd
->error_string
= "unpacker error";
845 data
.cmds
= commands
;
847 if (check_everything_connected(iterate_receive_command_list
, 0, &data
))
848 set_connectivity_errors(commands
, si
);
850 reject_updates_to_hidden(commands
);
852 if (run_receive_hook(commands
, "pre-receive", 0)) {
853 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
854 if (!cmd
->error_string
)
855 cmd
->error_string
= "pre-receive hook declined";
860 check_aliased_updates(commands
);
862 free(head_name_to_free
);
863 head_name
= head_name_to_free
= resolve_refdup("HEAD", sha1
, 0, NULL
);
865 checked_connectivity
= 1;
866 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
867 if (cmd
->error_string
)
870 if (cmd
->skip_update
)
873 cmd
->error_string
= update(cmd
, si
);
874 if (shallow_update
&& !cmd
->error_string
&&
875 si
->shallow_ref
[cmd
->index
]) {
876 error("BUG: connectivity check has not been run on ref %s",
878 checked_connectivity
= 0;
882 if (shallow_update
&& !checked_connectivity
)
883 error("BUG: run 'git fsck' for safety.\n"
884 "If there are errors, try to remove "
885 "the reported refs above");
888 static struct command
*read_head_info(struct sha1_array
*shallow
)
890 struct command
*commands
= NULL
;
891 struct command
**p
= &commands
;
894 unsigned char old_sha1
[20], new_sha1
[20];
899 line
= packet_read_line(0, &len
);
903 if (len
== 48 && starts_with(line
, "shallow ")) {
904 if (get_sha1_hex(line
+ 8, old_sha1
))
905 die("protocol error: expected shallow sha, got '%s'", line
+ 8);
906 sha1_array_append(shallow
, old_sha1
);
913 get_sha1_hex(line
, old_sha1
) ||
914 get_sha1_hex(line
+ 41, new_sha1
))
915 die("protocol error: expected old/new/ref, got '%s'",
919 reflen
= strlen(refname
);
920 if (reflen
+ 82 < len
) {
921 const char *feature_list
= refname
+ reflen
+ 1;
922 if (parse_feature_request(feature_list
, "report-status"))
924 if (parse_feature_request(feature_list
, "side-band-64k"))
925 use_sideband
= LARGE_PACKET_MAX
;
926 if (parse_feature_request(feature_list
, "quiet"))
929 cmd
= xcalloc(1, sizeof(struct command
) + len
- 80);
930 hashcpy(cmd
->old_sha1
, old_sha1
);
931 hashcpy(cmd
->new_sha1
, new_sha1
);
932 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
939 static const char *parse_pack_header(struct pack_header
*hdr
)
941 switch (read_pack_header(0, hdr
)) {
943 return "eof before pack header was fully read";
945 case PH_ERROR_PACK_SIGNATURE
:
946 return "protocol error (pack signature mismatch detected)";
948 case PH_ERROR_PROTOCOL
:
949 return "protocol error (pack version unsupported)";
952 return "unknown error in parse_pack_header";
959 static const char *pack_lockfile
;
961 static const char *unpack(int err_fd
, struct shallow_info
*si
)
963 struct pack_header hdr
;
964 struct argv_array av
= ARGV_ARRAY_INIT
;
968 struct child_process child
;
969 int fsck_objects
= (receive_fsck_objects
>= 0
970 ? receive_fsck_objects
971 : transfer_fsck_objects
>= 0
972 ? transfer_fsck_objects
975 hdr_err
= parse_pack_header(&hdr
);
981 snprintf(hdr_arg
, sizeof(hdr_arg
),
982 "--pack_header=%"PRIu32
",%"PRIu32
,
983 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
985 if (si
->nr_ours
|| si
->nr_theirs
) {
986 alt_shallow_file
= setup_temporary_shallow(si
->shallow
);
987 argv_array_pushl(&av
, "--shallow-file", alt_shallow_file
, NULL
);
990 memset(&child
, 0, sizeof(child
));
991 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
992 argv_array_pushl(&av
, "unpack-objects", hdr_arg
, NULL
);
994 argv_array_push(&av
, "-q");
996 argv_array_push(&av
, "--strict");
997 child
.argv
= av
.argv
;
1001 status
= run_command(&child
);
1003 return "unpack-objects abnormal exit";
1008 s
= sprintf(keep_arg
, "--keep=receive-pack %"PRIuMAX
" on ", (uintmax_t) getpid());
1009 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
1010 strcpy(keep_arg
+ s
, "localhost");
1012 argv_array_pushl(&av
, "index-pack",
1013 "--stdin", hdr_arg
, keep_arg
, NULL
);
1015 argv_array_push(&av
, "--strict");
1017 argv_array_push(&av
, "--fix-thin");
1018 child
.argv
= av
.argv
;
1022 status
= start_command(&child
);
1024 return "index-pack fork failed";
1025 pack_lockfile
= index_pack_lockfile(child
.out
);
1027 status
= finish_command(&child
);
1029 return "index-pack abnormal exit";
1030 reprepare_packed_git();
1035 static const char *unpack_with_sideband(struct shallow_info
*si
)
1041 return unpack(0, si
);
1043 memset(&muxer
, 0, sizeof(muxer
));
1044 muxer
.proc
= copy_to_sideband
;
1046 if (start_async(&muxer
))
1049 ret
= unpack(muxer
.in
, si
);
1051 finish_async(&muxer
);
1055 static void prepare_shallow_update(struct command
*commands
,
1056 struct shallow_info
*si
)
1058 int i
, j
, k
, bitmap_size
= (si
->ref
->nr
+ 31) / 32;
1060 si
->used_shallow
= xmalloc(sizeof(*si
->used_shallow
) *
1062 assign_shallow_commits_to_refs(si
, si
->used_shallow
, NULL
);
1064 si
->need_reachability_test
=
1065 xcalloc(si
->shallow
->nr
, sizeof(*si
->need_reachability_test
));
1067 xcalloc(si
->shallow
->nr
, sizeof(*si
->reachable
));
1068 si
->shallow_ref
= xcalloc(si
->ref
->nr
, sizeof(*si
->shallow_ref
));
1070 for (i
= 0; i
< si
->nr_ours
; i
++)
1071 si
->need_reachability_test
[si
->ours
[i
]] = 1;
1073 for (i
= 0; i
< si
->shallow
->nr
; i
++) {
1074 if (!si
->used_shallow
[i
])
1076 for (j
= 0; j
< bitmap_size
; j
++) {
1077 if (!si
->used_shallow
[i
][j
])
1079 si
->need_reachability_test
[i
]++;
1080 for (k
= 0; k
< 32; k
++)
1081 if (si
->used_shallow
[i
][j
] & (1 << k
))
1082 si
->shallow_ref
[j
* 32 + k
]++;
1086 * true for those associated with some refs and belong
1087 * in "ours" list aka "step 7 not done yet"
1089 si
->need_reachability_test
[i
] =
1090 si
->need_reachability_test
[i
] > 1;
1094 * keep hooks happy by forcing a temporary shallow file via
1095 * env variable because we can't add --shallow-file to every
1096 * command. check_everything_connected() will be done with
1097 * true .git/shallow though.
1099 setenv(GIT_SHALLOW_FILE_ENVIRONMENT
, alt_shallow_file
, 1);
1102 static void update_shallow_info(struct command
*commands
,
1103 struct shallow_info
*si
,
1104 struct sha1_array
*ref
)
1106 struct command
*cmd
;
1108 remove_nonexistent_theirs_shallow(si
);
1109 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1114 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1115 if (is_null_sha1(cmd
->new_sha1
))
1117 sha1_array_append(ref
, cmd
->new_sha1
);
1118 cmd
->index
= ref
->nr
- 1;
1122 if (shallow_update
) {
1123 prepare_shallow_update(commands
, si
);
1127 ref_status
= xmalloc(sizeof(*ref_status
) * ref
->nr
);
1128 assign_shallow_commits_to_refs(si
, NULL
, ref_status
);
1129 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1130 if (is_null_sha1(cmd
->new_sha1
))
1132 if (ref_status
[cmd
->index
]) {
1133 cmd
->error_string
= "shallow update not allowed";
1134 cmd
->skip_update
= 1;
1140 static void report(struct command
*commands
, const char *unpack_status
)
1142 struct command
*cmd
;
1143 struct strbuf buf
= STRBUF_INIT
;
1145 packet_buf_write(&buf
, "unpack %s\n",
1146 unpack_status
? unpack_status
: "ok");
1147 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1148 if (!cmd
->error_string
)
1149 packet_buf_write(&buf
, "ok %s\n",
1152 packet_buf_write(&buf
, "ng %s %s\n",
1153 cmd
->ref_name
, cmd
->error_string
);
1155 packet_buf_flush(&buf
);
1158 send_sideband(1, 1, buf
.buf
, buf
.len
, use_sideband
);
1160 write_or_die(1, buf
.buf
, buf
.len
);
1161 strbuf_release(&buf
);
1164 static int delete_only(struct command
*commands
)
1166 struct command
*cmd
;
1167 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1168 if (!is_null_sha1(cmd
->new_sha1
))
1174 int cmd_receive_pack(int argc
, const char **argv
, const char *prefix
)
1176 int advertise_refs
= 0;
1177 int stateless_rpc
= 0;
1179 const char *dir
= NULL
;
1180 struct command
*commands
;
1181 struct sha1_array shallow
= SHA1_ARRAY_INIT
;
1182 struct sha1_array ref
= SHA1_ARRAY_INIT
;
1183 struct shallow_info si
;
1185 packet_trace_identity("receive-pack");
1188 for (i
= 1; i
< argc
; i
++) {
1189 const char *arg
= *argv
++;
1192 if (!strcmp(arg
, "--quiet")) {
1197 if (!strcmp(arg
, "--advertise-refs")) {
1201 if (!strcmp(arg
, "--stateless-rpc")) {
1205 if (!strcmp(arg
, "--reject-thin-pack-for-testing")) {
1210 usage(receive_pack_usage
);
1213 usage(receive_pack_usage
);
1217 usage(receive_pack_usage
);
1221 if (!enter_repo(dir
, 0))
1222 die("'%s' does not appear to be a git repository", dir
);
1224 git_config(receive_pack_config
, NULL
);
1226 if (0 <= transfer_unpack_limit
)
1227 unpack_limit
= transfer_unpack_limit
;
1228 else if (0 <= receive_unpack_limit
)
1229 unpack_limit
= receive_unpack_limit
;
1231 if (advertise_refs
|| !stateless_rpc
) {
1237 if ((commands
= read_head_info(&shallow
)) != NULL
) {
1238 const char *unpack_status
= NULL
;
1240 prepare_shallow_info(&si
, &shallow
);
1241 if (!si
.nr_ours
&& !si
.nr_theirs
)
1243 if (!delete_only(commands
)) {
1244 unpack_status
= unpack_with_sideband(&si
);
1245 update_shallow_info(commands
, &si
, &ref
);
1247 execute_commands(commands
, unpack_status
, &si
);
1249 unlink_or_warn(pack_lockfile
);
1251 report(commands
, unpack_status
);
1252 run_receive_hook(commands
, "post-receive", 1);
1253 run_update_post_hook(commands
);
1255 const char *argv_gc_auto
[] = {
1256 "gc", "--auto", "--quiet", NULL
,
1258 int opt
= RUN_GIT_CMD
| RUN_COMMAND_STDOUT_TO_STDERR
;
1259 run_command_v_opt(argv_gc_auto
, opt
);
1261 if (auto_update_server_info
)
1262 update_server_info(0);
1263 clear_shallow_info(&si
);
1267 sha1_array_clear(&shallow
);
1268 sha1_array_clear(&ref
);