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("GIT_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
, 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
) {
672 if (cmd
->error_string
|| cmd
->did_not_exist
)
674 p
= xmalloc(strlen(cmd
->ref_name
) + 1);
675 strcpy(p
, cmd
->ref_name
);
681 memset(&proc
, 0, sizeof(proc
));
683 proc
.stdout_to_stderr
= 1;
684 proc
.err
= use_sideband
? -1 : 0;
687 if (!start_command(&proc
)) {
689 copy_to_sideband(proc
.err
, -1, NULL
);
690 finish_command(&proc
);
694 static void check_aliased_update(struct command
*cmd
, struct string_list
*list
)
696 struct strbuf buf
= STRBUF_INIT
;
697 const char *dst_name
;
698 struct string_list_item
*item
;
699 struct command
*dst_cmd
;
700 unsigned char sha1
[20];
701 char cmd_oldh
[41], cmd_newh
[41], dst_oldh
[41], dst_newh
[41];
704 strbuf_addf(&buf
, "%s%s", get_git_namespace(), cmd
->ref_name
);
705 dst_name
= resolve_ref_unsafe(buf
.buf
, sha1
, 0, &flag
);
706 strbuf_release(&buf
);
708 if (!(flag
& REF_ISSYMREF
))
711 dst_name
= strip_namespace(dst_name
);
713 rp_error("refusing update to broken symref '%s'", cmd
->ref_name
);
714 cmd
->skip_update
= 1;
715 cmd
->error_string
= "broken symref";
719 if ((item
= string_list_lookup(list
, dst_name
)) == NULL
)
722 cmd
->skip_update
= 1;
724 dst_cmd
= (struct command
*) item
->util
;
726 if (!hashcmp(cmd
->old_sha1
, dst_cmd
->old_sha1
) &&
727 !hashcmp(cmd
->new_sha1
, dst_cmd
->new_sha1
))
730 dst_cmd
->skip_update
= 1;
732 strcpy(cmd_oldh
, find_unique_abbrev(cmd
->old_sha1
, DEFAULT_ABBREV
));
733 strcpy(cmd_newh
, find_unique_abbrev(cmd
->new_sha1
, DEFAULT_ABBREV
));
734 strcpy(dst_oldh
, find_unique_abbrev(dst_cmd
->old_sha1
, DEFAULT_ABBREV
));
735 strcpy(dst_newh
, find_unique_abbrev(dst_cmd
->new_sha1
, DEFAULT_ABBREV
));
736 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
737 " its target '%s' (%s..%s)",
738 cmd
->ref_name
, cmd_oldh
, cmd_newh
,
739 dst_cmd
->ref_name
, dst_oldh
, dst_newh
);
741 cmd
->error_string
= dst_cmd
->error_string
=
742 "inconsistent aliased update";
745 static void check_aliased_updates(struct command
*commands
)
748 struct string_list ref_list
= STRING_LIST_INIT_NODUP
;
750 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
751 struct string_list_item
*item
=
752 string_list_append(&ref_list
, cmd
->ref_name
);
753 item
->util
= (void *)cmd
;
755 sort_string_list(&ref_list
);
757 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
758 if (!cmd
->error_string
)
759 check_aliased_update(cmd
, &ref_list
);
762 string_list_clear(&ref_list
, 0);
765 static int command_singleton_iterator(void *cb_data
, unsigned char sha1
[20])
767 struct command
**cmd_list
= cb_data
;
768 struct command
*cmd
= *cmd_list
;
770 if (!cmd
|| is_null_sha1(cmd
->new_sha1
))
771 return -1; /* end of list */
772 *cmd_list
= NULL
; /* this returns only one */
773 hashcpy(sha1
, cmd
->new_sha1
);
777 static void set_connectivity_errors(struct command
*commands
,
778 struct shallow_info
*si
)
782 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
783 struct command
*singleton
= cmd
;
784 if (shallow_update
&& si
->shallow_ref
[cmd
->index
])
785 /* to be checked in update_shallow_ref() */
787 if (!check_everything_connected(command_singleton_iterator
,
790 cmd
->error_string
= "missing necessary objects";
794 struct iterate_data
{
795 struct command
*cmds
;
796 struct shallow_info
*si
;
799 static int iterate_receive_command_list(void *cb_data
, unsigned char sha1
[20])
801 struct iterate_data
*data
= cb_data
;
802 struct command
**cmd_list
= &data
->cmds
;
803 struct command
*cmd
= *cmd_list
;
805 for (; cmd
; cmd
= cmd
->next
) {
806 if (shallow_update
&& data
->si
->shallow_ref
[cmd
->index
])
807 /* to be checked in update_shallow_ref() */
809 if (!is_null_sha1(cmd
->new_sha1
) && !cmd
->skip_update
) {
810 hashcpy(sha1
, cmd
->new_sha1
);
811 *cmd_list
= cmd
->next
;
816 return -1; /* end of list */
819 static void reject_updates_to_hidden(struct command
*commands
)
823 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
824 if (cmd
->error_string
|| !ref_is_hidden(cmd
->ref_name
))
826 if (is_null_sha1(cmd
->new_sha1
))
827 cmd
->error_string
= "deny deleting a hidden ref";
829 cmd
->error_string
= "deny updating a hidden ref";
833 static void execute_commands(struct command
*commands
,
834 const char *unpacker_error
,
835 struct shallow_info
*si
)
837 int checked_connectivity
;
839 unsigned char sha1
[20];
840 struct iterate_data data
;
842 if (unpacker_error
) {
843 for (cmd
= commands
; cmd
; cmd
= cmd
->next
)
844 cmd
->error_string
= "unpacker error";
848 data
.cmds
= commands
;
850 if (check_everything_connected(iterate_receive_command_list
, 0, &data
))
851 set_connectivity_errors(commands
, si
);
853 reject_updates_to_hidden(commands
);
855 if (run_receive_hook(commands
, "pre-receive", 0)) {
856 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
857 if (!cmd
->error_string
)
858 cmd
->error_string
= "pre-receive hook declined";
863 check_aliased_updates(commands
);
865 free(head_name_to_free
);
866 head_name
= head_name_to_free
= resolve_refdup("HEAD", sha1
, 0, NULL
);
868 checked_connectivity
= 1;
869 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
870 if (cmd
->error_string
)
873 if (cmd
->skip_update
)
876 cmd
->error_string
= update(cmd
, si
);
877 if (shallow_update
&& !cmd
->error_string
&&
878 si
->shallow_ref
[cmd
->index
]) {
879 error("BUG: connectivity check has not been run on ref %s",
881 checked_connectivity
= 0;
885 if (shallow_update
&& !checked_connectivity
)
886 error("BUG: run 'git fsck' for safety.\n"
887 "If there are errors, try to remove "
888 "the reported refs above");
891 static struct command
*read_head_info(struct sha1_array
*shallow
)
893 struct command
*commands
= NULL
;
894 struct command
**p
= &commands
;
897 unsigned char old_sha1
[20], new_sha1
[20];
902 line
= packet_read_line(0, &len
);
906 if (len
== 48 && starts_with(line
, "shallow ")) {
907 if (get_sha1_hex(line
+ 8, old_sha1
))
908 die("protocol error: expected shallow sha, got '%s'", line
+ 8);
909 sha1_array_append(shallow
, old_sha1
);
916 get_sha1_hex(line
, old_sha1
) ||
917 get_sha1_hex(line
+ 41, new_sha1
))
918 die("protocol error: expected old/new/ref, got '%s'",
922 reflen
= strlen(refname
);
923 if (reflen
+ 82 < len
) {
924 const char *feature_list
= refname
+ reflen
+ 1;
925 if (parse_feature_request(feature_list
, "report-status"))
927 if (parse_feature_request(feature_list
, "side-band-64k"))
928 use_sideband
= LARGE_PACKET_MAX
;
929 if (parse_feature_request(feature_list
, "quiet"))
932 cmd
= xcalloc(1, sizeof(struct command
) + len
- 80);
933 hashcpy(cmd
->old_sha1
, old_sha1
);
934 hashcpy(cmd
->new_sha1
, new_sha1
);
935 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
942 static const char *parse_pack_header(struct pack_header
*hdr
)
944 switch (read_pack_header(0, hdr
)) {
946 return "eof before pack header was fully read";
948 case PH_ERROR_PACK_SIGNATURE
:
949 return "protocol error (pack signature mismatch detected)";
951 case PH_ERROR_PROTOCOL
:
952 return "protocol error (pack version unsupported)";
955 return "unknown error in parse_pack_header";
962 static const char *pack_lockfile
;
964 static const char *unpack(int err_fd
, struct shallow_info
*si
)
966 struct pack_header hdr
;
967 struct argv_array av
= ARGV_ARRAY_INIT
;
971 struct child_process child
;
972 int fsck_objects
= (receive_fsck_objects
>= 0
973 ? receive_fsck_objects
974 : transfer_fsck_objects
>= 0
975 ? transfer_fsck_objects
978 hdr_err
= parse_pack_header(&hdr
);
984 snprintf(hdr_arg
, sizeof(hdr_arg
),
985 "--pack_header=%"PRIu32
",%"PRIu32
,
986 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
988 if (si
->nr_ours
|| si
->nr_theirs
) {
989 alt_shallow_file
= setup_temporary_shallow(si
->shallow
);
990 argv_array_pushl(&av
, "--shallow-file", alt_shallow_file
, NULL
);
993 memset(&child
, 0, sizeof(child
));
994 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
995 argv_array_pushl(&av
, "unpack-objects", hdr_arg
, NULL
);
997 argv_array_push(&av
, "-q");
999 argv_array_push(&av
, "--strict");
1000 child
.argv
= av
.argv
;
1001 child
.no_stdout
= 1;
1004 status
= run_command(&child
);
1006 return "unpack-objects abnormal exit";
1011 s
= sprintf(keep_arg
, "--keep=receive-pack %"PRIuMAX
" on ", (uintmax_t) getpid());
1012 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
1013 strcpy(keep_arg
+ s
, "localhost");
1015 argv_array_pushl(&av
, "index-pack",
1016 "--stdin", hdr_arg
, keep_arg
, NULL
);
1018 argv_array_push(&av
, "--strict");
1020 argv_array_push(&av
, "--fix-thin");
1021 child
.argv
= av
.argv
;
1025 status
= start_command(&child
);
1027 return "index-pack fork failed";
1028 pack_lockfile
= index_pack_lockfile(child
.out
);
1030 status
= finish_command(&child
);
1032 return "index-pack abnormal exit";
1033 reprepare_packed_git();
1038 static const char *unpack_with_sideband(struct shallow_info
*si
)
1044 return unpack(0, si
);
1046 memset(&muxer
, 0, sizeof(muxer
));
1047 muxer
.proc
= copy_to_sideband
;
1049 if (start_async(&muxer
))
1052 ret
= unpack(muxer
.in
, si
);
1054 finish_async(&muxer
);
1058 static void prepare_shallow_update(struct command
*commands
,
1059 struct shallow_info
*si
)
1061 int i
, j
, k
, bitmap_size
= (si
->ref
->nr
+ 31) / 32;
1063 si
->used_shallow
= xmalloc(sizeof(*si
->used_shallow
) *
1065 assign_shallow_commits_to_refs(si
, si
->used_shallow
, NULL
);
1067 si
->need_reachability_test
=
1068 xcalloc(si
->shallow
->nr
, sizeof(*si
->need_reachability_test
));
1070 xcalloc(si
->shallow
->nr
, sizeof(*si
->reachable
));
1071 si
->shallow_ref
= xcalloc(si
->ref
->nr
, sizeof(*si
->shallow_ref
));
1073 for (i
= 0; i
< si
->nr_ours
; i
++)
1074 si
->need_reachability_test
[si
->ours
[i
]] = 1;
1076 for (i
= 0; i
< si
->shallow
->nr
; i
++) {
1077 if (!si
->used_shallow
[i
])
1079 for (j
= 0; j
< bitmap_size
; j
++) {
1080 if (!si
->used_shallow
[i
][j
])
1082 si
->need_reachability_test
[i
]++;
1083 for (k
= 0; k
< 32; k
++)
1084 if (si
->used_shallow
[i
][j
] & (1 << k
))
1085 si
->shallow_ref
[j
* 32 + k
]++;
1089 * true for those associated with some refs and belong
1090 * in "ours" list aka "step 7 not done yet"
1092 si
->need_reachability_test
[i
] =
1093 si
->need_reachability_test
[i
] > 1;
1097 * keep hooks happy by forcing a temporary shallow file via
1098 * env variable because we can't add --shallow-file to every
1099 * command. check_everything_connected() will be done with
1100 * true .git/shallow though.
1102 setenv(GIT_SHALLOW_FILE_ENVIRONMENT
, alt_shallow_file
, 1);
1105 static void update_shallow_info(struct command
*commands
,
1106 struct shallow_info
*si
,
1107 struct sha1_array
*ref
)
1109 struct command
*cmd
;
1111 remove_nonexistent_theirs_shallow(si
);
1112 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1117 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1118 if (is_null_sha1(cmd
->new_sha1
))
1120 sha1_array_append(ref
, cmd
->new_sha1
);
1121 cmd
->index
= ref
->nr
- 1;
1125 if (shallow_update
) {
1126 prepare_shallow_update(commands
, si
);
1130 ref_status
= xmalloc(sizeof(*ref_status
) * ref
->nr
);
1131 assign_shallow_commits_to_refs(si
, NULL
, ref_status
);
1132 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1133 if (is_null_sha1(cmd
->new_sha1
))
1135 if (ref_status
[cmd
->index
]) {
1136 cmd
->error_string
= "shallow update not allowed";
1137 cmd
->skip_update
= 1;
1143 static void report(struct command
*commands
, const char *unpack_status
)
1145 struct command
*cmd
;
1146 struct strbuf buf
= STRBUF_INIT
;
1148 packet_buf_write(&buf
, "unpack %s\n",
1149 unpack_status
? unpack_status
: "ok");
1150 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1151 if (!cmd
->error_string
)
1152 packet_buf_write(&buf
, "ok %s\n",
1155 packet_buf_write(&buf
, "ng %s %s\n",
1156 cmd
->ref_name
, cmd
->error_string
);
1158 packet_buf_flush(&buf
);
1161 send_sideband(1, 1, buf
.buf
, buf
.len
, use_sideband
);
1163 write_or_die(1, buf
.buf
, buf
.len
);
1164 strbuf_release(&buf
);
1167 static int delete_only(struct command
*commands
)
1169 struct command
*cmd
;
1170 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1171 if (!is_null_sha1(cmd
->new_sha1
))
1177 int cmd_receive_pack(int argc
, const char **argv
, const char *prefix
)
1179 int advertise_refs
= 0;
1180 int stateless_rpc
= 0;
1183 struct command
*commands
;
1184 struct sha1_array shallow
= SHA1_ARRAY_INIT
;
1185 struct sha1_array ref
= SHA1_ARRAY_INIT
;
1186 struct shallow_info si
;
1188 packet_trace_identity("receive-pack");
1191 for (i
= 1; i
< argc
; i
++) {
1192 const char *arg
= *argv
++;
1195 if (!strcmp(arg
, "--quiet")) {
1200 if (!strcmp(arg
, "--advertise-refs")) {
1204 if (!strcmp(arg
, "--stateless-rpc")) {
1208 if (!strcmp(arg
, "--reject-thin-pack-for-testing")) {
1213 usage(receive_pack_usage
);
1216 usage(receive_pack_usage
);
1220 usage(receive_pack_usage
);
1224 if (!enter_repo(dir
, 0))
1225 die("'%s' does not appear to be a git repository", dir
);
1227 git_config(receive_pack_config
, NULL
);
1229 if (0 <= transfer_unpack_limit
)
1230 unpack_limit
= transfer_unpack_limit
;
1231 else if (0 <= receive_unpack_limit
)
1232 unpack_limit
= receive_unpack_limit
;
1234 if (advertise_refs
|| !stateless_rpc
) {
1240 if ((commands
= read_head_info(&shallow
)) != NULL
) {
1241 const char *unpack_status
= NULL
;
1243 prepare_shallow_info(&si
, &shallow
);
1244 if (!si
.nr_ours
&& !si
.nr_theirs
)
1246 if (!delete_only(commands
)) {
1247 unpack_status
= unpack_with_sideband(&si
);
1248 update_shallow_info(commands
, &si
, &ref
);
1250 execute_commands(commands
, unpack_status
, &si
);
1252 unlink_or_warn(pack_lockfile
);
1254 report(commands
, unpack_status
);
1255 run_receive_hook(commands
, "post-receive", 1);
1256 run_update_post_hook(commands
);
1258 const char *argv_gc_auto
[] = {
1259 "gc", "--auto", "--quiet", NULL
,
1261 int opt
= RUN_GIT_CMD
| RUN_COMMAND_STDOUT_TO_STDERR
;
1262 run_command_v_opt(argv_gc_auto
, opt
);
1264 if (auto_update_server_info
)
1265 update_server_info(0);
1266 clear_shallow_info(&si
);
1270 sha1_array_clear(&shallow
);
1271 sha1_array_clear(&ref
);