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"
20 static const char receive_pack_usage
[] = "git receive-pack <git-dir>";
29 static int deny_deletes
;
30 static int deny_non_fast_forwards
;
31 static enum deny_action deny_current_branch
= DENY_UNCONFIGURED
;
32 static enum deny_action deny_delete_current
= DENY_UNCONFIGURED
;
33 static int receive_fsck_objects
= -1;
34 static int transfer_fsck_objects
= -1;
35 static int receive_unpack_limit
= -1;
36 static int transfer_unpack_limit
= -1;
37 static int unpack_limit
= 100;
38 static int report_status
;
39 static int use_sideband
;
41 static int prefer_ofs_delta
= 1;
42 static int auto_update_server_info
;
43 static int auto_gc
= 1;
44 static int fix_thin
= 1;
45 static const char *head_name
;
46 static void *head_name_to_free
;
47 static int sent_capabilities
;
48 static int shallow_update
;
49 static const char *alt_shallow_file
;
51 static enum deny_action
parse_deny_action(const char *var
, const char *value
)
54 if (!strcasecmp(value
, "ignore"))
56 if (!strcasecmp(value
, "warn"))
58 if (!strcasecmp(value
, "refuse"))
61 if (git_config_bool(var
, value
))
66 static int receive_pack_config(const char *var
, const char *value
, void *cb
)
68 int status
= parse_hide_refs_config(var
, value
, "receive");
73 if (strcmp(var
, "receive.denydeletes") == 0) {
74 deny_deletes
= git_config_bool(var
, value
);
78 if (strcmp(var
, "receive.denynonfastforwards") == 0) {
79 deny_non_fast_forwards
= git_config_bool(var
, value
);
83 if (strcmp(var
, "receive.unpacklimit") == 0) {
84 receive_unpack_limit
= git_config_int(var
, value
);
88 if (strcmp(var
, "transfer.unpacklimit") == 0) {
89 transfer_unpack_limit
= git_config_int(var
, value
);
93 if (strcmp(var
, "receive.fsckobjects") == 0) {
94 receive_fsck_objects
= git_config_bool(var
, value
);
98 if (strcmp(var
, "transfer.fsckobjects") == 0) {
99 transfer_fsck_objects
= git_config_bool(var
, value
);
103 if (!strcmp(var
, "receive.denycurrentbranch")) {
104 deny_current_branch
= parse_deny_action(var
, value
);
108 if (strcmp(var
, "receive.denydeletecurrent") == 0) {
109 deny_delete_current
= parse_deny_action(var
, value
);
113 if (strcmp(var
, "repack.usedeltabaseoffset") == 0) {
114 prefer_ofs_delta
= git_config_bool(var
, value
);
118 if (strcmp(var
, "receive.updateserverinfo") == 0) {
119 auto_update_server_info
= git_config_bool(var
, value
);
123 if (strcmp(var
, "receive.autogc") == 0) {
124 auto_gc
= git_config_bool(var
, value
);
128 if (strcmp(var
, "receive.shallowupdate") == 0) {
129 shallow_update
= git_config_bool(var
, value
);
133 return git_default_config(var
, value
, cb
);
136 static void show_ref(const char *path
, const unsigned char *sha1
)
138 if (ref_is_hidden(path
))
141 if (sent_capabilities
)
142 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
144 packet_write(1, "%s %s%c%s%s agent=%s\n",
145 sha1_to_hex(sha1
), path
, 0,
146 " report-status delete-refs side-band-64k quiet",
147 prefer_ofs_delta
? " ofs-delta" : "",
148 git_user_agent_sanitized());
149 sent_capabilities
= 1;
152 static int show_ref_cb(const char *path
, const unsigned char *sha1
, int flag
, void *unused
)
154 path
= strip_namespace(path
);
156 * Advertise refs outside our current namespace as ".have"
157 * refs, so that the client can use them to minimize data
158 * transfer but will otherwise ignore them. This happens to
159 * cover ".have" that are thrown in by add_one_alternate_ref()
160 * to mark histories that are complete in our alternates as
165 show_ref(path
, sha1
);
169 static void show_one_alternate_sha1(const unsigned char sha1
[20], void *unused
)
171 show_ref(".have", sha1
);
174 static void collect_one_alternate_ref(const struct ref
*ref
, void *data
)
176 struct sha1_array
*sa
= data
;
177 sha1_array_append(sa
, ref
->old_sha1
);
180 static void write_head_info(void)
182 struct sha1_array sa
= SHA1_ARRAY_INIT
;
183 for_each_alternate_ref(collect_one_alternate_ref
, &sa
);
184 sha1_array_for_each_unique(&sa
, show_one_alternate_sha1
, NULL
);
185 sha1_array_clear(&sa
);
186 for_each_ref(show_ref_cb
, NULL
);
187 if (!sent_capabilities
)
188 show_ref("capabilities^{}", null_sha1
);
190 advertise_shallow_grafts(1);
197 struct command
*next
;
198 const char *error_string
;
199 unsigned int skip_update
:1,
202 unsigned char old_sha1
[20];
203 unsigned char new_sha1
[20];
204 char ref_name
[FLEX_ARRAY
]; /* more */
207 static void rp_error(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
208 static void rp_warning(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
210 static void report_message(const char *prefix
, const char *err
, va_list params
)
212 int sz
= strlen(prefix
);
215 strncpy(msg
, prefix
, sz
);
216 sz
+= vsnprintf(msg
+ sz
, sizeof(msg
) - sz
, err
, params
);
217 if (sz
> (sizeof(msg
) - 1))
218 sz
= sizeof(msg
) - 1;
222 send_sideband(1, 2, msg
, sz
, use_sideband
);
227 static void rp_warning(const char *err
, ...)
230 va_start(params
, err
);
231 report_message("warning: ", err
, params
);
235 static void rp_error(const char *err
, ...)
238 va_start(params
, err
);
239 report_message("error: ", err
, params
);
243 static int copy_to_sideband(int in
, int out
, void *arg
)
247 ssize_t sz
= xread(in
, data
, sizeof(data
));
250 send_sideband(1, 2, data
, sz
, use_sideband
);
256 typedef int (*feed_fn
)(void *, const char **, size_t *);
257 static int run_and_feed_hook(const char *hook_name
, feed_fn feed
, void *feed_state
)
259 struct child_process proc
= CHILD_PROCESS_INIT
;
264 argv
[0] = find_hook(hook_name
);
272 proc
.stdout_to_stderr
= 1;
275 memset(&muxer
, 0, sizeof(muxer
));
276 muxer
.proc
= copy_to_sideband
;
278 code
= start_async(&muxer
);
284 code
= start_command(&proc
);
287 finish_async(&muxer
);
291 sigchain_push(SIGPIPE
, SIG_IGN
);
296 if (feed(feed_state
, &buf
, &n
))
298 if (write_in_full(proc
.in
, buf
, n
) != n
)
303 finish_async(&muxer
);
305 sigchain_pop(SIGPIPE
);
307 return finish_command(&proc
);
310 struct receive_hook_feed_state
{
316 static int feed_receive_hook(void *state_
, const char **bufp
, size_t *sizep
)
318 struct receive_hook_feed_state
*state
= state_
;
319 struct command
*cmd
= state
->cmd
;
322 state
->skip_broken
&& (cmd
->error_string
|| cmd
->did_not_exist
))
326 strbuf_reset(&state
->buf
);
327 strbuf_addf(&state
->buf
, "%s %s %s\n",
328 sha1_to_hex(cmd
->old_sha1
), sha1_to_hex(cmd
->new_sha1
),
330 state
->cmd
= cmd
->next
;
332 *bufp
= state
->buf
.buf
;
333 *sizep
= state
->buf
.len
;
338 static int run_receive_hook(struct command
*commands
, const char *hook_name
,
341 struct receive_hook_feed_state state
;
344 strbuf_init(&state
.buf
, 0);
345 state
.cmd
= commands
;
346 state
.skip_broken
= skip_broken
;
347 if (feed_receive_hook(&state
, NULL
, NULL
))
349 state
.cmd
= commands
;
350 status
= run_and_feed_hook(hook_name
, feed_receive_hook
, &state
);
351 strbuf_release(&state
.buf
);
355 static int run_update_hook(struct command
*cmd
)
358 struct child_process proc
= CHILD_PROCESS_INIT
;
361 argv
[0] = find_hook("update");
365 argv
[1] = cmd
->ref_name
;
366 argv
[2] = sha1_to_hex(cmd
->old_sha1
);
367 argv
[3] = sha1_to_hex(cmd
->new_sha1
);
371 proc
.stdout_to_stderr
= 1;
372 proc
.err
= use_sideband
? -1 : 0;
375 code
= start_command(&proc
);
379 copy_to_sideband(proc
.err
, -1, NULL
);
380 return finish_command(&proc
);
383 static int is_ref_checked_out(const char *ref
)
385 if (is_bare_repository())
390 return !strcmp(head_name
, ref
);
393 static char *refuse_unconfigured_deny_msg
[] = {
394 "By default, updating the current branch in a non-bare repository",
395 "is denied, because it will make the index and work tree inconsistent",
396 "with what you pushed, and will require 'git reset --hard' to match",
397 "the work tree to HEAD.",
399 "You can set 'receive.denyCurrentBranch' configuration variable to",
400 "'ignore' or 'warn' in the remote repository to allow pushing into",
401 "its current branch; however, this is not recommended unless you",
402 "arranged to update its work tree to match what you pushed in some",
405 "To squelch this message and still keep the default behaviour, set",
406 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
409 static void refuse_unconfigured_deny(void)
412 for (i
= 0; i
< ARRAY_SIZE(refuse_unconfigured_deny_msg
); i
++)
413 rp_error("%s", refuse_unconfigured_deny_msg
[i
]);
416 static char *refuse_unconfigured_deny_delete_current_msg
[] = {
417 "By default, deleting the current branch is denied, because the next",
418 "'git clone' won't result in any file checked out, causing confusion.",
420 "You can set 'receive.denyDeleteCurrent' configuration variable to",
421 "'warn' or 'ignore' in the remote repository to allow deleting the",
422 "current branch, with or without a warning message.",
424 "To squelch this message, you can set it to 'refuse'."
427 static void refuse_unconfigured_deny_delete_current(void)
431 i
< ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg
);
433 rp_error("%s", refuse_unconfigured_deny_delete_current_msg
[i
]);
436 static int command_singleton_iterator(void *cb_data
, unsigned char sha1
[20]);
437 static int update_shallow_ref(struct command
*cmd
, struct shallow_info
*si
)
439 static struct lock_file shallow_lock
;
440 struct sha1_array extra
= SHA1_ARRAY_INIT
;
441 const char *alt_file
;
442 uint32_t mask
= 1 << (cmd
->index
% 32);
445 trace_printf_key(&trace_shallow
,
446 "shallow: update_shallow_ref %s\n", cmd
->ref_name
);
447 for (i
= 0; i
< si
->shallow
->nr
; i
++)
448 if (si
->used_shallow
[i
] &&
449 (si
->used_shallow
[i
][cmd
->index
/ 32] & mask
) &&
450 !delayed_reachability_test(si
, i
))
451 sha1_array_append(&extra
, si
->shallow
->sha1
[i
]);
453 setup_alternate_shallow(&shallow_lock
, &alt_file
, &extra
);
454 if (check_shallow_connected(command_singleton_iterator
,
456 rollback_lock_file(&shallow_lock
);
457 sha1_array_clear(&extra
);
461 commit_lock_file(&shallow_lock
);
464 * Make sure setup_alternate_shallow() for the next ref does
465 * not lose these new roots..
467 for (i
= 0; i
< extra
.nr
; i
++)
468 register_shallow(extra
.sha1
[i
]);
470 si
->shallow_ref
[cmd
->index
] = 0;
471 sha1_array_clear(&extra
);
475 static const char *update(struct command
*cmd
, struct shallow_info
*si
)
477 const char *name
= cmd
->ref_name
;
478 struct strbuf namespaced_name_buf
= STRBUF_INIT
;
479 const char *namespaced_name
;
480 unsigned char *old_sha1
= cmd
->old_sha1
;
481 unsigned char *new_sha1
= cmd
->new_sha1
;
483 /* only refs/... are allowed */
484 if (!starts_with(name
, "refs/") || check_refname_format(name
+ 5, 0)) {
485 rp_error("refusing to create funny ref '%s' remotely", name
);
486 return "funny refname";
489 strbuf_addf(&namespaced_name_buf
, "%s%s", get_git_namespace(), name
);
490 namespaced_name
= strbuf_detach(&namespaced_name_buf
, NULL
);
492 if (is_ref_checked_out(namespaced_name
)) {
493 switch (deny_current_branch
) {
497 rp_warning("updating the current branch");
500 case DENY_UNCONFIGURED
:
501 rp_error("refusing to update checked out branch: %s", name
);
502 if (deny_current_branch
== DENY_UNCONFIGURED
)
503 refuse_unconfigured_deny();
504 return "branch is currently checked out";
508 if (!is_null_sha1(new_sha1
) && !has_sha1_file(new_sha1
)) {
509 error("unpack should have generated %s, "
510 "but I can't find it!", sha1_to_hex(new_sha1
));
514 if (!is_null_sha1(old_sha1
) && is_null_sha1(new_sha1
)) {
515 if (deny_deletes
&& starts_with(name
, "refs/heads/")) {
516 rp_error("denying ref deletion for %s", name
);
517 return "deletion prohibited";
520 if (!strcmp(namespaced_name
, head_name
)) {
521 switch (deny_delete_current
) {
525 rp_warning("deleting the current branch");
528 case DENY_UNCONFIGURED
:
529 if (deny_delete_current
== DENY_UNCONFIGURED
)
530 refuse_unconfigured_deny_delete_current();
531 rp_error("refusing to delete the current branch: %s", name
);
532 return "deletion of the current branch prohibited";
537 if (deny_non_fast_forwards
&& !is_null_sha1(new_sha1
) &&
538 !is_null_sha1(old_sha1
) &&
539 starts_with(name
, "refs/heads/")) {
540 struct object
*old_object
, *new_object
;
541 struct commit
*old_commit
, *new_commit
;
543 old_object
= parse_object(old_sha1
);
544 new_object
= parse_object(new_sha1
);
546 if (!old_object
|| !new_object
||
547 old_object
->type
!= OBJ_COMMIT
||
548 new_object
->type
!= OBJ_COMMIT
) {
549 error("bad sha1 objects for %s", name
);
552 old_commit
= (struct commit
*)old_object
;
553 new_commit
= (struct commit
*)new_object
;
554 if (!in_merge_bases(old_commit
, new_commit
)) {
555 rp_error("denying non-fast-forward %s"
556 " (you should pull first)", name
);
557 return "non-fast-forward";
560 if (run_update_hook(cmd
)) {
561 rp_error("hook declined to update %s", name
);
562 return "hook declined";
565 if (is_null_sha1(new_sha1
)) {
566 if (!parse_object(old_sha1
)) {
568 if (ref_exists(name
)) {
569 rp_warning("Allowing deletion of corrupt ref.");
571 rp_warning("Deleting a non-existent ref.");
572 cmd
->did_not_exist
= 1;
575 if (delete_ref(namespaced_name
, old_sha1
, 0)) {
576 rp_error("failed to delete %s", name
);
577 return "failed to delete";
579 return NULL
; /* good */
582 struct strbuf err
= STRBUF_INIT
;
583 struct ref_transaction
*transaction
;
585 if (shallow_update
&& si
->shallow_ref
[cmd
->index
] &&
586 update_shallow_ref(cmd
, si
))
587 return "shallow error";
589 transaction
= ref_transaction_begin(&err
);
591 ref_transaction_update(transaction
, namespaced_name
,
592 new_sha1
, old_sha1
, 0, 1, &err
) ||
593 ref_transaction_commit(transaction
, "push", &err
)) {
594 ref_transaction_free(transaction
);
596 rp_error("%s", err
.buf
);
597 strbuf_release(&err
);
598 return "failed to update ref";
601 ref_transaction_free(transaction
);
602 strbuf_release(&err
);
603 return NULL
; /* good */
607 static void run_update_post_hook(struct command
*commands
)
612 struct child_process proc
= CHILD_PROCESS_INIT
;
615 hook
= find_hook("post-update");
616 for (argc
= 0, cmd
= commands
; cmd
; cmd
= cmd
->next
) {
617 if (cmd
->error_string
|| cmd
->did_not_exist
)
624 argv
= xmalloc(sizeof(*argv
) * (2 + argc
));
627 for (argc
= 1, cmd
= commands
; cmd
; cmd
= cmd
->next
) {
628 if (cmd
->error_string
|| cmd
->did_not_exist
)
630 argv
[argc
] = xstrdup(cmd
->ref_name
);
636 proc
.stdout_to_stderr
= 1;
637 proc
.err
= use_sideband
? -1 : 0;
640 if (!start_command(&proc
)) {
642 copy_to_sideband(proc
.err
, -1, NULL
);
643 finish_command(&proc
);
647 static void check_aliased_update(struct command
*cmd
, struct string_list
*list
)
649 struct strbuf buf
= STRBUF_INIT
;
650 const char *dst_name
;
651 struct string_list_item
*item
;
652 struct command
*dst_cmd
;
653 unsigned char sha1
[20];
654 char cmd_oldh
[41], cmd_newh
[41], dst_oldh
[41], dst_newh
[41];
657 strbuf_addf(&buf
, "%s%s", get_git_namespace(), cmd
->ref_name
);
658 dst_name
= resolve_ref_unsafe(buf
.buf
, sha1
, 0, &flag
);
659 strbuf_release(&buf
);
661 if (!(flag
& REF_ISSYMREF
))
664 dst_name
= strip_namespace(dst_name
);
666 rp_error("refusing update to broken symref '%s'", cmd
->ref_name
);
667 cmd
->skip_update
= 1;
668 cmd
->error_string
= "broken symref";
672 if ((item
= string_list_lookup(list
, dst_name
)) == NULL
)
675 cmd
->skip_update
= 1;
677 dst_cmd
= (struct command
*) item
->util
;
679 if (!hashcmp(cmd
->old_sha1
, dst_cmd
->old_sha1
) &&
680 !hashcmp(cmd
->new_sha1
, dst_cmd
->new_sha1
))
683 dst_cmd
->skip_update
= 1;
685 strcpy(cmd_oldh
, find_unique_abbrev(cmd
->old_sha1
, DEFAULT_ABBREV
));
686 strcpy(cmd_newh
, find_unique_abbrev(cmd
->new_sha1
, DEFAULT_ABBREV
));
687 strcpy(dst_oldh
, find_unique_abbrev(dst_cmd
->old_sha1
, DEFAULT_ABBREV
));
688 strcpy(dst_newh
, find_unique_abbrev(dst_cmd
->new_sha1
, DEFAULT_ABBREV
));
689 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
690 " its target '%s' (%s..%s)",
691 cmd
->ref_name
, cmd_oldh
, cmd_newh
,
692 dst_cmd
->ref_name
, dst_oldh
, dst_newh
);
694 cmd
->error_string
= dst_cmd
->error_string
=
695 "inconsistent aliased update";
698 static void check_aliased_updates(struct command
*commands
)
701 struct string_list ref_list
= STRING_LIST_INIT_NODUP
;
703 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
704 struct string_list_item
*item
=
705 string_list_append(&ref_list
, cmd
->ref_name
);
706 item
->util
= (void *)cmd
;
708 sort_string_list(&ref_list
);
710 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
711 if (!cmd
->error_string
)
712 check_aliased_update(cmd
, &ref_list
);
715 string_list_clear(&ref_list
, 0);
718 static int command_singleton_iterator(void *cb_data
, unsigned char sha1
[20])
720 struct command
**cmd_list
= cb_data
;
721 struct command
*cmd
= *cmd_list
;
723 if (!cmd
|| is_null_sha1(cmd
->new_sha1
))
724 return -1; /* end of list */
725 *cmd_list
= NULL
; /* this returns only one */
726 hashcpy(sha1
, cmd
->new_sha1
);
730 static void set_connectivity_errors(struct command
*commands
,
731 struct shallow_info
*si
)
735 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
736 struct command
*singleton
= cmd
;
737 if (shallow_update
&& si
->shallow_ref
[cmd
->index
])
738 /* to be checked in update_shallow_ref() */
740 if (!check_everything_connected(command_singleton_iterator
,
743 cmd
->error_string
= "missing necessary objects";
747 struct iterate_data
{
748 struct command
*cmds
;
749 struct shallow_info
*si
;
752 static int iterate_receive_command_list(void *cb_data
, unsigned char sha1
[20])
754 struct iterate_data
*data
= cb_data
;
755 struct command
**cmd_list
= &data
->cmds
;
756 struct command
*cmd
= *cmd_list
;
758 for (; cmd
; cmd
= cmd
->next
) {
759 if (shallow_update
&& data
->si
->shallow_ref
[cmd
->index
])
760 /* to be checked in update_shallow_ref() */
762 if (!is_null_sha1(cmd
->new_sha1
) && !cmd
->skip_update
) {
763 hashcpy(sha1
, cmd
->new_sha1
);
764 *cmd_list
= cmd
->next
;
769 return -1; /* end of list */
772 static void reject_updates_to_hidden(struct command
*commands
)
776 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
777 if (cmd
->error_string
|| !ref_is_hidden(cmd
->ref_name
))
779 if (is_null_sha1(cmd
->new_sha1
))
780 cmd
->error_string
= "deny deleting a hidden ref";
782 cmd
->error_string
= "deny updating a hidden ref";
786 static void execute_commands(struct command
*commands
,
787 const char *unpacker_error
,
788 struct shallow_info
*si
)
790 int checked_connectivity
;
792 unsigned char sha1
[20];
793 struct iterate_data data
;
795 if (unpacker_error
) {
796 for (cmd
= commands
; cmd
; cmd
= cmd
->next
)
797 cmd
->error_string
= "unpacker error";
801 data
.cmds
= commands
;
803 if (check_everything_connected(iterate_receive_command_list
, 0, &data
))
804 set_connectivity_errors(commands
, si
);
806 reject_updates_to_hidden(commands
);
808 if (run_receive_hook(commands
, "pre-receive", 0)) {
809 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
810 if (!cmd
->error_string
)
811 cmd
->error_string
= "pre-receive hook declined";
816 check_aliased_updates(commands
);
818 free(head_name_to_free
);
819 head_name
= head_name_to_free
= resolve_refdup("HEAD", sha1
, 0, NULL
);
821 checked_connectivity
= 1;
822 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
823 if (cmd
->error_string
)
826 if (cmd
->skip_update
)
829 cmd
->error_string
= update(cmd
, si
);
830 if (shallow_update
&& !cmd
->error_string
&&
831 si
->shallow_ref
[cmd
->index
]) {
832 error("BUG: connectivity check has not been run on ref %s",
834 checked_connectivity
= 0;
838 if (shallow_update
&& !checked_connectivity
)
839 error("BUG: run 'git fsck' for safety.\n"
840 "If there are errors, try to remove "
841 "the reported refs above");
844 static struct command
*read_head_info(struct sha1_array
*shallow
)
846 struct command
*commands
= NULL
;
847 struct command
**p
= &commands
;
850 unsigned char old_sha1
[20], new_sha1
[20];
855 line
= packet_read_line(0, &len
);
859 if (len
== 48 && starts_with(line
, "shallow ")) {
860 if (get_sha1_hex(line
+ 8, old_sha1
))
861 die("protocol error: expected shallow sha, got '%s'", line
+ 8);
862 sha1_array_append(shallow
, old_sha1
);
869 get_sha1_hex(line
, old_sha1
) ||
870 get_sha1_hex(line
+ 41, new_sha1
))
871 die("protocol error: expected old/new/ref, got '%s'",
875 reflen
= strlen(refname
);
876 if (reflen
+ 82 < len
) {
877 const char *feature_list
= refname
+ reflen
+ 1;
878 if (parse_feature_request(feature_list
, "report-status"))
880 if (parse_feature_request(feature_list
, "side-band-64k"))
881 use_sideband
= LARGE_PACKET_MAX
;
882 if (parse_feature_request(feature_list
, "quiet"))
885 cmd
= xcalloc(1, sizeof(struct command
) + len
- 80);
886 hashcpy(cmd
->old_sha1
, old_sha1
);
887 hashcpy(cmd
->new_sha1
, new_sha1
);
888 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
895 static const char *parse_pack_header(struct pack_header
*hdr
)
897 switch (read_pack_header(0, hdr
)) {
899 return "eof before pack header was fully read";
901 case PH_ERROR_PACK_SIGNATURE
:
902 return "protocol error (pack signature mismatch detected)";
904 case PH_ERROR_PROTOCOL
:
905 return "protocol error (pack version unsupported)";
908 return "unknown error in parse_pack_header";
915 static const char *pack_lockfile
;
917 static const char *unpack(int err_fd
, struct shallow_info
*si
)
919 struct pack_header hdr
;
920 struct argv_array av
= ARGV_ARRAY_INIT
;
924 struct child_process child
= CHILD_PROCESS_INIT
;
925 int fsck_objects
= (receive_fsck_objects
>= 0
926 ? receive_fsck_objects
927 : transfer_fsck_objects
>= 0
928 ? transfer_fsck_objects
931 hdr_err
= parse_pack_header(&hdr
);
937 snprintf(hdr_arg
, sizeof(hdr_arg
),
938 "--pack_header=%"PRIu32
",%"PRIu32
,
939 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
941 if (si
->nr_ours
|| si
->nr_theirs
) {
942 alt_shallow_file
= setup_temporary_shallow(si
->shallow
);
943 argv_array_pushl(&av
, "--shallow-file", alt_shallow_file
, NULL
);
946 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
947 argv_array_pushl(&av
, "unpack-objects", hdr_arg
, NULL
);
949 argv_array_push(&av
, "-q");
951 argv_array_push(&av
, "--strict");
952 child
.argv
= av
.argv
;
956 status
= run_command(&child
);
958 return "unpack-objects abnormal exit";
963 s
= sprintf(keep_arg
, "--keep=receive-pack %"PRIuMAX
" on ", (uintmax_t) getpid());
964 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
965 strcpy(keep_arg
+ s
, "localhost");
967 argv_array_pushl(&av
, "index-pack",
968 "--stdin", hdr_arg
, keep_arg
, NULL
);
970 argv_array_push(&av
, "--strict");
972 argv_array_push(&av
, "--fix-thin");
973 child
.argv
= av
.argv
;
977 status
= start_command(&child
);
979 return "index-pack fork failed";
980 pack_lockfile
= index_pack_lockfile(child
.out
);
982 status
= finish_command(&child
);
984 return "index-pack abnormal exit";
985 reprepare_packed_git();
990 static const char *unpack_with_sideband(struct shallow_info
*si
)
996 return unpack(0, si
);
998 memset(&muxer
, 0, sizeof(muxer
));
999 muxer
.proc
= copy_to_sideband
;
1001 if (start_async(&muxer
))
1004 ret
= unpack(muxer
.in
, si
);
1006 finish_async(&muxer
);
1010 static void prepare_shallow_update(struct command
*commands
,
1011 struct shallow_info
*si
)
1013 int i
, j
, k
, bitmap_size
= (si
->ref
->nr
+ 31) / 32;
1015 si
->used_shallow
= xmalloc(sizeof(*si
->used_shallow
) *
1017 assign_shallow_commits_to_refs(si
, si
->used_shallow
, NULL
);
1019 si
->need_reachability_test
=
1020 xcalloc(si
->shallow
->nr
, sizeof(*si
->need_reachability_test
));
1022 xcalloc(si
->shallow
->nr
, sizeof(*si
->reachable
));
1023 si
->shallow_ref
= xcalloc(si
->ref
->nr
, sizeof(*si
->shallow_ref
));
1025 for (i
= 0; i
< si
->nr_ours
; i
++)
1026 si
->need_reachability_test
[si
->ours
[i
]] = 1;
1028 for (i
= 0; i
< si
->shallow
->nr
; i
++) {
1029 if (!si
->used_shallow
[i
])
1031 for (j
= 0; j
< bitmap_size
; j
++) {
1032 if (!si
->used_shallow
[i
][j
])
1034 si
->need_reachability_test
[i
]++;
1035 for (k
= 0; k
< 32; k
++)
1036 if (si
->used_shallow
[i
][j
] & (1 << k
))
1037 si
->shallow_ref
[j
* 32 + k
]++;
1041 * true for those associated with some refs and belong
1042 * in "ours" list aka "step 7 not done yet"
1044 si
->need_reachability_test
[i
] =
1045 si
->need_reachability_test
[i
] > 1;
1049 * keep hooks happy by forcing a temporary shallow file via
1050 * env variable because we can't add --shallow-file to every
1051 * command. check_everything_connected() will be done with
1052 * true .git/shallow though.
1054 setenv(GIT_SHALLOW_FILE_ENVIRONMENT
, alt_shallow_file
, 1);
1057 static void update_shallow_info(struct command
*commands
,
1058 struct shallow_info
*si
,
1059 struct sha1_array
*ref
)
1061 struct command
*cmd
;
1063 remove_nonexistent_theirs_shallow(si
);
1064 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1069 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1070 if (is_null_sha1(cmd
->new_sha1
))
1072 sha1_array_append(ref
, cmd
->new_sha1
);
1073 cmd
->index
= ref
->nr
- 1;
1077 if (shallow_update
) {
1078 prepare_shallow_update(commands
, si
);
1082 ref_status
= xmalloc(sizeof(*ref_status
) * ref
->nr
);
1083 assign_shallow_commits_to_refs(si
, NULL
, ref_status
);
1084 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1085 if (is_null_sha1(cmd
->new_sha1
))
1087 if (ref_status
[cmd
->index
]) {
1088 cmd
->error_string
= "shallow update not allowed";
1089 cmd
->skip_update
= 1;
1095 static void report(struct command
*commands
, const char *unpack_status
)
1097 struct command
*cmd
;
1098 struct strbuf buf
= STRBUF_INIT
;
1100 packet_buf_write(&buf
, "unpack %s\n",
1101 unpack_status
? unpack_status
: "ok");
1102 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1103 if (!cmd
->error_string
)
1104 packet_buf_write(&buf
, "ok %s\n",
1107 packet_buf_write(&buf
, "ng %s %s\n",
1108 cmd
->ref_name
, cmd
->error_string
);
1110 packet_buf_flush(&buf
);
1113 send_sideband(1, 1, buf
.buf
, buf
.len
, use_sideband
);
1115 write_or_die(1, buf
.buf
, buf
.len
);
1116 strbuf_release(&buf
);
1119 static int delete_only(struct command
*commands
)
1121 struct command
*cmd
;
1122 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1123 if (!is_null_sha1(cmd
->new_sha1
))
1129 int cmd_receive_pack(int argc
, const char **argv
, const char *prefix
)
1131 int advertise_refs
= 0;
1132 int stateless_rpc
= 0;
1134 const char *dir
= NULL
;
1135 struct command
*commands
;
1136 struct sha1_array shallow
= SHA1_ARRAY_INIT
;
1137 struct sha1_array ref
= SHA1_ARRAY_INIT
;
1138 struct shallow_info si
;
1140 packet_trace_identity("receive-pack");
1143 for (i
= 1; i
< argc
; i
++) {
1144 const char *arg
= *argv
++;
1147 if (!strcmp(arg
, "--quiet")) {
1152 if (!strcmp(arg
, "--advertise-refs")) {
1156 if (!strcmp(arg
, "--stateless-rpc")) {
1160 if (!strcmp(arg
, "--reject-thin-pack-for-testing")) {
1165 usage(receive_pack_usage
);
1168 usage(receive_pack_usage
);
1172 usage(receive_pack_usage
);
1176 if (!enter_repo(dir
, 0))
1177 die("'%s' does not appear to be a git repository", dir
);
1179 git_config(receive_pack_config
, NULL
);
1181 if (0 <= transfer_unpack_limit
)
1182 unpack_limit
= transfer_unpack_limit
;
1183 else if (0 <= receive_unpack_limit
)
1184 unpack_limit
= receive_unpack_limit
;
1186 if (advertise_refs
|| !stateless_rpc
) {
1192 if ((commands
= read_head_info(&shallow
)) != NULL
) {
1193 const char *unpack_status
= NULL
;
1195 prepare_shallow_info(&si
, &shallow
);
1196 if (!si
.nr_ours
&& !si
.nr_theirs
)
1198 if (!delete_only(commands
)) {
1199 unpack_status
= unpack_with_sideband(&si
);
1200 update_shallow_info(commands
, &si
, &ref
);
1202 execute_commands(commands
, unpack_status
, &si
);
1204 unlink_or_warn(pack_lockfile
);
1206 report(commands
, unpack_status
);
1207 run_receive_hook(commands
, "post-receive", 1);
1208 run_update_post_hook(commands
);
1210 const char *argv_gc_auto
[] = {
1211 "gc", "--auto", "--quiet", NULL
,
1213 int opt
= RUN_GIT_CMD
| RUN_COMMAND_STDOUT_TO_STDERR
;
1214 run_command_v_opt(argv_gc_auto
, opt
);
1216 if (auto_update_server_info
)
1217 update_server_info(0);
1218 clear_shallow_info(&si
);
1222 sha1_array_clear(&shallow
);
1223 sha1_array_clear(&ref
);