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 #include "gpg-interface.h"
21 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
;
51 static int accept_push_cert
= 1;
52 static struct strbuf push_cert
= STRBUF_INIT
;
53 static unsigned char push_cert_sha1
[20];
54 static struct signature_check sigcheck
;
56 static enum deny_action
parse_deny_action(const char *var
, const char *value
)
59 if (!strcasecmp(value
, "ignore"))
61 if (!strcasecmp(value
, "warn"))
63 if (!strcasecmp(value
, "refuse"))
66 if (git_config_bool(var
, value
))
71 static int receive_pack_config(const char *var
, const char *value
, void *cb
)
73 int status
= parse_hide_refs_config(var
, value
, "receive");
78 if (strcmp(var
, "receive.denydeletes") == 0) {
79 deny_deletes
= git_config_bool(var
, value
);
83 if (strcmp(var
, "receive.denynonfastforwards") == 0) {
84 deny_non_fast_forwards
= git_config_bool(var
, value
);
88 if (strcmp(var
, "receive.unpacklimit") == 0) {
89 receive_unpack_limit
= git_config_int(var
, value
);
93 if (strcmp(var
, "transfer.unpacklimit") == 0) {
94 transfer_unpack_limit
= git_config_int(var
, value
);
98 if (strcmp(var
, "receive.fsckobjects") == 0) {
99 receive_fsck_objects
= git_config_bool(var
, value
);
103 if (strcmp(var
, "transfer.fsckobjects") == 0) {
104 transfer_fsck_objects
= git_config_bool(var
, value
);
108 if (!strcmp(var
, "receive.denycurrentbranch")) {
109 deny_current_branch
= parse_deny_action(var
, value
);
113 if (strcmp(var
, "receive.denydeletecurrent") == 0) {
114 deny_delete_current
= parse_deny_action(var
, value
);
118 if (strcmp(var
, "repack.usedeltabaseoffset") == 0) {
119 prefer_ofs_delta
= git_config_bool(var
, value
);
123 if (strcmp(var
, "receive.updateserverinfo") == 0) {
124 auto_update_server_info
= git_config_bool(var
, value
);
128 if (strcmp(var
, "receive.autogc") == 0) {
129 auto_gc
= git_config_bool(var
, value
);
133 if (strcmp(var
, "receive.shallowupdate") == 0) {
134 shallow_update
= git_config_bool(var
, value
);
138 if (strcmp(var
, "receive.acceptpushcert") == 0) {
139 accept_push_cert
= git_config_bool(var
, value
);
143 return git_default_config(var
, value
, cb
);
146 static void show_ref(const char *path
, const unsigned char *sha1
)
148 if (ref_is_hidden(path
))
151 if (sent_capabilities
) {
152 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
154 struct strbuf cap
= STRBUF_INIT
;
157 "report-status delete-refs side-band-64k quiet");
158 if (prefer_ofs_delta
)
159 strbuf_addstr(&cap
, " ofs-delta");
160 if (accept_push_cert
)
161 strbuf_addstr(&cap
, " push-cert");
162 strbuf_addf(&cap
, " agent=%s", git_user_agent_sanitized());
163 packet_write(1, "%s %s%c%s\n",
164 sha1_to_hex(sha1
), path
, 0, cap
.buf
);
165 strbuf_release(&cap
);
166 sent_capabilities
= 1;
170 static int show_ref_cb(const char *path
, const unsigned char *sha1
, int flag
, void *unused
)
172 path
= strip_namespace(path
);
174 * Advertise refs outside our current namespace as ".have"
175 * refs, so that the client can use them to minimize data
176 * transfer but will otherwise ignore them. This happens to
177 * cover ".have" that are thrown in by add_one_alternate_ref()
178 * to mark histories that are complete in our alternates as
183 show_ref(path
, sha1
);
187 static void show_one_alternate_sha1(const unsigned char sha1
[20], void *unused
)
189 show_ref(".have", sha1
);
192 static void collect_one_alternate_ref(const struct ref
*ref
, void *data
)
194 struct sha1_array
*sa
= data
;
195 sha1_array_append(sa
, ref
->old_sha1
);
198 static void write_head_info(void)
200 struct sha1_array sa
= SHA1_ARRAY_INIT
;
201 for_each_alternate_ref(collect_one_alternate_ref
, &sa
);
202 sha1_array_for_each_unique(&sa
, show_one_alternate_sha1
, NULL
);
203 sha1_array_clear(&sa
);
204 for_each_ref(show_ref_cb
, NULL
);
205 if (!sent_capabilities
)
206 show_ref("capabilities^{}", null_sha1
);
208 advertise_shallow_grafts(1);
215 struct command
*next
;
216 const char *error_string
;
217 unsigned int skip_update
:1,
220 unsigned char old_sha1
[20];
221 unsigned char new_sha1
[20];
222 char ref_name
[FLEX_ARRAY
]; /* more */
225 static void rp_error(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
226 static void rp_warning(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
228 static void report_message(const char *prefix
, const char *err
, va_list params
)
230 int sz
= strlen(prefix
);
233 strncpy(msg
, prefix
, sz
);
234 sz
+= vsnprintf(msg
+ sz
, sizeof(msg
) - sz
, err
, params
);
235 if (sz
> (sizeof(msg
) - 1))
236 sz
= sizeof(msg
) - 1;
240 send_sideband(1, 2, msg
, sz
, use_sideband
);
245 static void rp_warning(const char *err
, ...)
248 va_start(params
, err
);
249 report_message("warning: ", err
, params
);
253 static void rp_error(const char *err
, ...)
256 va_start(params
, err
);
257 report_message("error: ", err
, params
);
261 static int copy_to_sideband(int in
, int out
, void *arg
)
265 ssize_t sz
= xread(in
, data
, sizeof(data
));
268 send_sideband(1, 2, data
, sz
, use_sideband
);
274 static void prepare_push_cert_sha1(struct child_process
*proc
)
276 static int already_done
;
277 struct argv_array env
= ARGV_ARRAY_INIT
;
283 struct strbuf gpg_output
= STRBUF_INIT
;
284 struct strbuf gpg_status
= STRBUF_INIT
;
285 int bogs
/* beginning_of_gpg_sig */;
288 if (write_sha1_file(push_cert
.buf
, push_cert
.len
, "blob", push_cert_sha1
))
289 hashclr(push_cert_sha1
);
291 memset(&sigcheck
, '\0', sizeof(sigcheck
));
292 sigcheck
.result
= 'N';
294 bogs
= parse_signature(push_cert
.buf
, push_cert
.len
);
295 if (verify_signed_buffer(push_cert
.buf
, bogs
,
296 push_cert
.buf
+ bogs
, push_cert
.len
- bogs
,
297 &gpg_output
, &gpg_status
) < 0) {
298 ; /* error running gpg */
300 sigcheck
.payload
= push_cert
.buf
;
301 sigcheck
.gpg_output
= gpg_output
.buf
;
302 sigcheck
.gpg_status
= gpg_status
.buf
;
303 parse_gpg_output(&sigcheck
);
306 strbuf_release(&gpg_output
);
307 strbuf_release(&gpg_status
);
309 if (!is_null_sha1(push_cert_sha1
)) {
310 argv_array_pushf(&env
, "GIT_PUSH_CERT=%s", sha1_to_hex(push_cert_sha1
));
311 argv_array_pushf(&env
, "GIT_PUSH_CERT_SIGNER=%s",
312 sigcheck
.signer
? sigcheck
.signer
: "");
313 argv_array_pushf(&env
, "GIT_PUSH_CERT_KEY=%s",
314 sigcheck
.key
? sigcheck
.key
: "");
315 argv_array_pushf(&env
, "GIT_PUSH_CERT_STATUS=%c", sigcheck
.result
);
317 proc
->env
= env
.argv
;
321 typedef int (*feed_fn
)(void *, const char **, size_t *);
322 static int run_and_feed_hook(const char *hook_name
, feed_fn feed
, void *feed_state
)
324 struct child_process proc
;
329 argv
[0] = find_hook(hook_name
);
335 memset(&proc
, 0, sizeof(proc
));
338 proc
.stdout_to_stderr
= 1;
340 prepare_push_cert_sha1(&proc
);
343 memset(&muxer
, 0, sizeof(muxer
));
344 muxer
.proc
= copy_to_sideband
;
346 code
= start_async(&muxer
);
352 code
= start_command(&proc
);
355 finish_async(&muxer
);
362 if (feed(feed_state
, &buf
, &n
))
364 if (write_in_full(proc
.in
, buf
, n
) != n
)
369 finish_async(&muxer
);
370 return finish_command(&proc
);
373 struct receive_hook_feed_state
{
379 static int feed_receive_hook(void *state_
, const char **bufp
, size_t *sizep
)
381 struct receive_hook_feed_state
*state
= state_
;
382 struct command
*cmd
= state
->cmd
;
385 state
->skip_broken
&& (cmd
->error_string
|| cmd
->did_not_exist
))
389 strbuf_reset(&state
->buf
);
390 strbuf_addf(&state
->buf
, "%s %s %s\n",
391 sha1_to_hex(cmd
->old_sha1
), sha1_to_hex(cmd
->new_sha1
),
393 state
->cmd
= cmd
->next
;
395 *bufp
= state
->buf
.buf
;
396 *sizep
= state
->buf
.len
;
401 static int run_receive_hook(struct command
*commands
, const char *hook_name
,
404 struct receive_hook_feed_state state
;
407 strbuf_init(&state
.buf
, 0);
408 state
.cmd
= commands
;
409 state
.skip_broken
= skip_broken
;
410 if (feed_receive_hook(&state
, NULL
, NULL
))
412 state
.cmd
= commands
;
413 status
= run_and_feed_hook(hook_name
, feed_receive_hook
, &state
);
414 strbuf_release(&state
.buf
);
418 static int run_update_hook(struct command
*cmd
)
421 struct child_process proc
;
424 argv
[0] = find_hook("update");
428 argv
[1] = cmd
->ref_name
;
429 argv
[2] = sha1_to_hex(cmd
->old_sha1
);
430 argv
[3] = sha1_to_hex(cmd
->new_sha1
);
433 memset(&proc
, 0, sizeof(proc
));
435 proc
.stdout_to_stderr
= 1;
436 proc
.err
= use_sideband
? -1 : 0;
439 code
= start_command(&proc
);
443 copy_to_sideband(proc
.err
, -1, NULL
);
444 return finish_command(&proc
);
447 static int is_ref_checked_out(const char *ref
)
449 if (is_bare_repository())
454 return !strcmp(head_name
, ref
);
457 static char *refuse_unconfigured_deny_msg
[] = {
458 "By default, updating the current branch in a non-bare repository",
459 "is denied, because it will make the index and work tree inconsistent",
460 "with what you pushed, and will require 'git reset --hard' to match",
461 "the work tree to HEAD.",
463 "You can set 'receive.denyCurrentBranch' configuration variable to",
464 "'ignore' or 'warn' in the remote repository to allow pushing into",
465 "its current branch; however, this is not recommended unless you",
466 "arranged to update its work tree to match what you pushed in some",
469 "To squelch this message and still keep the default behaviour, set",
470 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
473 static void refuse_unconfigured_deny(void)
476 for (i
= 0; i
< ARRAY_SIZE(refuse_unconfigured_deny_msg
); i
++)
477 rp_error("%s", refuse_unconfigured_deny_msg
[i
]);
480 static char *refuse_unconfigured_deny_delete_current_msg
[] = {
481 "By default, deleting the current branch is denied, because the next",
482 "'git clone' won't result in any file checked out, causing confusion.",
484 "You can set 'receive.denyDeleteCurrent' configuration variable to",
485 "'warn' or 'ignore' in the remote repository to allow deleting the",
486 "current branch, with or without a warning message.",
488 "To squelch this message, you can set it to 'refuse'."
491 static void refuse_unconfigured_deny_delete_current(void)
495 i
< ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg
);
497 rp_error("%s", refuse_unconfigured_deny_delete_current_msg
[i
]);
500 static int command_singleton_iterator(void *cb_data
, unsigned char sha1
[20]);
501 static int update_shallow_ref(struct command
*cmd
, struct shallow_info
*si
)
503 static struct lock_file shallow_lock
;
504 struct sha1_array extra
= SHA1_ARRAY_INIT
;
505 const char *alt_file
;
506 uint32_t mask
= 1 << (cmd
->index
% 32);
509 trace_printf_key(&trace_shallow
,
510 "shallow: update_shallow_ref %s\n", cmd
->ref_name
);
511 for (i
= 0; i
< si
->shallow
->nr
; i
++)
512 if (si
->used_shallow
[i
] &&
513 (si
->used_shallow
[i
][cmd
->index
/ 32] & mask
) &&
514 !delayed_reachability_test(si
, i
))
515 sha1_array_append(&extra
, si
->shallow
->sha1
[i
]);
517 setup_alternate_shallow(&shallow_lock
, &alt_file
, &extra
);
518 if (check_shallow_connected(command_singleton_iterator
,
520 rollback_lock_file(&shallow_lock
);
521 sha1_array_clear(&extra
);
525 commit_lock_file(&shallow_lock
);
528 * Make sure setup_alternate_shallow() for the next ref does
529 * not lose these new roots..
531 for (i
= 0; i
< extra
.nr
; i
++)
532 register_shallow(extra
.sha1
[i
]);
534 si
->shallow_ref
[cmd
->index
] = 0;
535 sha1_array_clear(&extra
);
539 static const char *update(struct command
*cmd
, struct shallow_info
*si
)
541 const char *name
= cmd
->ref_name
;
542 struct strbuf namespaced_name_buf
= STRBUF_INIT
;
543 const char *namespaced_name
;
544 unsigned char *old_sha1
= cmd
->old_sha1
;
545 unsigned char *new_sha1
= cmd
->new_sha1
;
546 struct ref_lock
*lock
;
548 /* only refs/... are allowed */
549 if (!starts_with(name
, "refs/") || check_refname_format(name
+ 5, 0)) {
550 rp_error("refusing to create funny ref '%s' remotely", name
);
551 return "funny refname";
554 strbuf_addf(&namespaced_name_buf
, "%s%s", get_git_namespace(), name
);
555 namespaced_name
= strbuf_detach(&namespaced_name_buf
, NULL
);
557 if (is_ref_checked_out(namespaced_name
)) {
558 switch (deny_current_branch
) {
562 rp_warning("updating the current branch");
565 case DENY_UNCONFIGURED
:
566 rp_error("refusing to update checked out branch: %s", name
);
567 if (deny_current_branch
== DENY_UNCONFIGURED
)
568 refuse_unconfigured_deny();
569 return "branch is currently checked out";
573 if (!is_null_sha1(new_sha1
) && !has_sha1_file(new_sha1
)) {
574 error("unpack should have generated %s, "
575 "but I can't find it!", sha1_to_hex(new_sha1
));
579 if (!is_null_sha1(old_sha1
) && is_null_sha1(new_sha1
)) {
580 if (deny_deletes
&& starts_with(name
, "refs/heads/")) {
581 rp_error("denying ref deletion for %s", name
);
582 return "deletion prohibited";
585 if (!strcmp(namespaced_name
, head_name
)) {
586 switch (deny_delete_current
) {
590 rp_warning("deleting the current branch");
593 case DENY_UNCONFIGURED
:
594 if (deny_delete_current
== DENY_UNCONFIGURED
)
595 refuse_unconfigured_deny_delete_current();
596 rp_error("refusing to delete the current branch: %s", name
);
597 return "deletion of the current branch prohibited";
602 if (deny_non_fast_forwards
&& !is_null_sha1(new_sha1
) &&
603 !is_null_sha1(old_sha1
) &&
604 starts_with(name
, "refs/heads/")) {
605 struct object
*old_object
, *new_object
;
606 struct commit
*old_commit
, *new_commit
;
608 old_object
= parse_object(old_sha1
);
609 new_object
= parse_object(new_sha1
);
611 if (!old_object
|| !new_object
||
612 old_object
->type
!= OBJ_COMMIT
||
613 new_object
->type
!= OBJ_COMMIT
) {
614 error("bad sha1 objects for %s", name
);
617 old_commit
= (struct commit
*)old_object
;
618 new_commit
= (struct commit
*)new_object
;
619 if (!in_merge_bases(old_commit
, new_commit
)) {
620 rp_error("denying non-fast-forward %s"
621 " (you should pull first)", name
);
622 return "non-fast-forward";
625 if (run_update_hook(cmd
)) {
626 rp_error("hook declined to update %s", name
);
627 return "hook declined";
630 if (is_null_sha1(new_sha1
)) {
631 if (!parse_object(old_sha1
)) {
633 if (ref_exists(name
)) {
634 rp_warning("Allowing deletion of corrupt ref.");
636 rp_warning("Deleting a non-existent ref.");
637 cmd
->did_not_exist
= 1;
640 if (delete_ref(namespaced_name
, old_sha1
, 0)) {
641 rp_error("failed to delete %s", name
);
642 return "failed to delete";
644 return NULL
; /* good */
647 if (shallow_update
&& si
->shallow_ref
[cmd
->index
] &&
648 update_shallow_ref(cmd
, si
))
649 return "shallow error";
651 lock
= lock_any_ref_for_update(namespaced_name
, old_sha1
,
654 rp_error("failed to lock %s", name
);
655 return "failed to lock";
657 if (write_ref_sha1(lock
, new_sha1
, "push")) {
658 return "failed to write"; /* error() already called */
660 return NULL
; /* good */
664 static void run_update_post_hook(struct command
*commands
)
669 struct child_process proc
;
672 hook
= find_hook("post-update");
673 for (argc
= 0, cmd
= commands
; cmd
; cmd
= cmd
->next
) {
674 if (cmd
->error_string
|| cmd
->did_not_exist
)
681 argv
= xmalloc(sizeof(*argv
) * (2 + argc
));
684 for (argc
= 1, cmd
= commands
; cmd
; cmd
= cmd
->next
) {
685 if (cmd
->error_string
|| cmd
->did_not_exist
)
687 argv
[argc
] = xstrdup(cmd
->ref_name
);
692 memset(&proc
, 0, sizeof(proc
));
694 proc
.stdout_to_stderr
= 1;
695 proc
.err
= use_sideband
? -1 : 0;
698 if (!start_command(&proc
)) {
700 copy_to_sideband(proc
.err
, -1, NULL
);
701 finish_command(&proc
);
705 static void check_aliased_update(struct command
*cmd
, struct string_list
*list
)
707 struct strbuf buf
= STRBUF_INIT
;
708 const char *dst_name
;
709 struct string_list_item
*item
;
710 struct command
*dst_cmd
;
711 unsigned char sha1
[20];
712 char cmd_oldh
[41], cmd_newh
[41], dst_oldh
[41], dst_newh
[41];
715 strbuf_addf(&buf
, "%s%s", get_git_namespace(), cmd
->ref_name
);
716 dst_name
= resolve_ref_unsafe(buf
.buf
, sha1
, 0, &flag
);
717 strbuf_release(&buf
);
719 if (!(flag
& REF_ISSYMREF
))
722 dst_name
= strip_namespace(dst_name
);
724 rp_error("refusing update to broken symref '%s'", cmd
->ref_name
);
725 cmd
->skip_update
= 1;
726 cmd
->error_string
= "broken symref";
730 if ((item
= string_list_lookup(list
, dst_name
)) == NULL
)
733 cmd
->skip_update
= 1;
735 dst_cmd
= (struct command
*) item
->util
;
737 if (!hashcmp(cmd
->old_sha1
, dst_cmd
->old_sha1
) &&
738 !hashcmp(cmd
->new_sha1
, dst_cmd
->new_sha1
))
741 dst_cmd
->skip_update
= 1;
743 strcpy(cmd_oldh
, find_unique_abbrev(cmd
->old_sha1
, DEFAULT_ABBREV
));
744 strcpy(cmd_newh
, find_unique_abbrev(cmd
->new_sha1
, DEFAULT_ABBREV
));
745 strcpy(dst_oldh
, find_unique_abbrev(dst_cmd
->old_sha1
, DEFAULT_ABBREV
));
746 strcpy(dst_newh
, find_unique_abbrev(dst_cmd
->new_sha1
, DEFAULT_ABBREV
));
747 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
748 " its target '%s' (%s..%s)",
749 cmd
->ref_name
, cmd_oldh
, cmd_newh
,
750 dst_cmd
->ref_name
, dst_oldh
, dst_newh
);
752 cmd
->error_string
= dst_cmd
->error_string
=
753 "inconsistent aliased update";
756 static void check_aliased_updates(struct command
*commands
)
759 struct string_list ref_list
= STRING_LIST_INIT_NODUP
;
761 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
762 struct string_list_item
*item
=
763 string_list_append(&ref_list
, cmd
->ref_name
);
764 item
->util
= (void *)cmd
;
766 sort_string_list(&ref_list
);
768 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
769 if (!cmd
->error_string
)
770 check_aliased_update(cmd
, &ref_list
);
773 string_list_clear(&ref_list
, 0);
776 static int command_singleton_iterator(void *cb_data
, unsigned char sha1
[20])
778 struct command
**cmd_list
= cb_data
;
779 struct command
*cmd
= *cmd_list
;
781 if (!cmd
|| is_null_sha1(cmd
->new_sha1
))
782 return -1; /* end of list */
783 *cmd_list
= NULL
; /* this returns only one */
784 hashcpy(sha1
, cmd
->new_sha1
);
788 static void set_connectivity_errors(struct command
*commands
,
789 struct shallow_info
*si
)
793 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
794 struct command
*singleton
= cmd
;
795 if (shallow_update
&& si
->shallow_ref
[cmd
->index
])
796 /* to be checked in update_shallow_ref() */
798 if (!check_everything_connected(command_singleton_iterator
,
801 cmd
->error_string
= "missing necessary objects";
805 struct iterate_data
{
806 struct command
*cmds
;
807 struct shallow_info
*si
;
810 static int iterate_receive_command_list(void *cb_data
, unsigned char sha1
[20])
812 struct iterate_data
*data
= cb_data
;
813 struct command
**cmd_list
= &data
->cmds
;
814 struct command
*cmd
= *cmd_list
;
816 for (; cmd
; cmd
= cmd
->next
) {
817 if (shallow_update
&& data
->si
->shallow_ref
[cmd
->index
])
818 /* to be checked in update_shallow_ref() */
820 if (!is_null_sha1(cmd
->new_sha1
) && !cmd
->skip_update
) {
821 hashcpy(sha1
, cmd
->new_sha1
);
822 *cmd_list
= cmd
->next
;
827 return -1; /* end of list */
830 static void reject_updates_to_hidden(struct command
*commands
)
834 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
835 if (cmd
->error_string
|| !ref_is_hidden(cmd
->ref_name
))
837 if (is_null_sha1(cmd
->new_sha1
))
838 cmd
->error_string
= "deny deleting a hidden ref";
840 cmd
->error_string
= "deny updating a hidden ref";
844 static void execute_commands(struct command
*commands
,
845 const char *unpacker_error
,
846 struct shallow_info
*si
)
848 int checked_connectivity
;
850 unsigned char sha1
[20];
851 struct iterate_data data
;
853 if (unpacker_error
) {
854 for (cmd
= commands
; cmd
; cmd
= cmd
->next
)
855 cmd
->error_string
= "unpacker error";
859 data
.cmds
= commands
;
861 if (check_everything_connected(iterate_receive_command_list
, 0, &data
))
862 set_connectivity_errors(commands
, si
);
864 reject_updates_to_hidden(commands
);
866 if (run_receive_hook(commands
, "pre-receive", 0)) {
867 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
868 if (!cmd
->error_string
)
869 cmd
->error_string
= "pre-receive hook declined";
874 check_aliased_updates(commands
);
876 free(head_name_to_free
);
877 head_name
= head_name_to_free
= resolve_refdup("HEAD", sha1
, 0, NULL
);
879 checked_connectivity
= 1;
880 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
881 if (cmd
->error_string
)
884 if (cmd
->skip_update
)
887 cmd
->error_string
= update(cmd
, si
);
888 if (shallow_update
&& !cmd
->error_string
&&
889 si
->shallow_ref
[cmd
->index
]) {
890 error("BUG: connectivity check has not been run on ref %s",
892 checked_connectivity
= 0;
896 if (shallow_update
&& !checked_connectivity
)
897 error("BUG: run 'git fsck' for safety.\n"
898 "If there are errors, try to remove "
899 "the reported refs above");
902 static struct command
**queue_command(struct command
**tail
,
906 unsigned char old_sha1
[20], new_sha1
[20];
914 get_sha1_hex(line
, old_sha1
) ||
915 get_sha1_hex(line
+ 41, new_sha1
))
916 die("protocol error: expected old/new/ref, got '%s'", line
);
919 reflen
= linelen
- 82;
920 cmd
= xcalloc(1, sizeof(struct command
) + reflen
+ 1);
921 hashcpy(cmd
->old_sha1
, old_sha1
);
922 hashcpy(cmd
->new_sha1
, new_sha1
);
923 memcpy(cmd
->ref_name
, refname
, reflen
);
924 cmd
->ref_name
[reflen
] = '\0';
929 static struct command
*read_head_info(struct sha1_array
*shallow
)
931 struct command
*commands
= NULL
;
932 struct command
**p
= &commands
;
937 line
= packet_read_line(0, &len
);
941 if (len
== 48 && starts_with(line
, "shallow ")) {
942 unsigned char sha1
[20];
943 if (get_sha1_hex(line
+ 8, sha1
))
944 die("protocol error: expected shallow sha, got '%s'",
946 sha1_array_append(shallow
, sha1
);
950 linelen
= strlen(line
);
952 const char *feature_list
= line
+ linelen
+ 1;
953 if (parse_feature_request(feature_list
, "report-status"))
955 if (parse_feature_request(feature_list
, "side-band-64k"))
956 use_sideband
= LARGE_PACKET_MAX
;
957 if (parse_feature_request(feature_list
, "quiet"))
961 if (!strcmp(line
, "push-cert")) {
966 len
= packet_read(0, NULL
, NULL
,
967 certbuf
, sizeof(certbuf
), 0);
972 if (!strcmp(certbuf
, "push-cert-end\n"))
973 break; /* end of cert */
974 strbuf_addstr(&push_cert
, certbuf
);
982 p
= queue_command(p
, line
, linelen
);
987 static const char *parse_pack_header(struct pack_header
*hdr
)
989 switch (read_pack_header(0, hdr
)) {
991 return "eof before pack header was fully read";
993 case PH_ERROR_PACK_SIGNATURE
:
994 return "protocol error (pack signature mismatch detected)";
996 case PH_ERROR_PROTOCOL
:
997 return "protocol error (pack version unsupported)";
1000 return "unknown error in parse_pack_header";
1007 static const char *pack_lockfile
;
1009 static const char *unpack(int err_fd
, struct shallow_info
*si
)
1011 struct pack_header hdr
;
1012 struct argv_array av
= ARGV_ARRAY_INIT
;
1013 const char *hdr_err
;
1016 struct child_process child
;
1017 int fsck_objects
= (receive_fsck_objects
>= 0
1018 ? receive_fsck_objects
1019 : transfer_fsck_objects
>= 0
1020 ? transfer_fsck_objects
1023 hdr_err
= parse_pack_header(&hdr
);
1029 snprintf(hdr_arg
, sizeof(hdr_arg
),
1030 "--pack_header=%"PRIu32
",%"PRIu32
,
1031 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
1033 if (si
->nr_ours
|| si
->nr_theirs
) {
1034 alt_shallow_file
= setup_temporary_shallow(si
->shallow
);
1035 argv_array_pushl(&av
, "--shallow-file", alt_shallow_file
, NULL
);
1038 memset(&child
, 0, sizeof(child
));
1039 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
1040 argv_array_pushl(&av
, "unpack-objects", hdr_arg
, NULL
);
1042 argv_array_push(&av
, "-q");
1044 argv_array_push(&av
, "--strict");
1045 child
.argv
= av
.argv
;
1046 child
.no_stdout
= 1;
1049 status
= run_command(&child
);
1051 return "unpack-objects abnormal exit";
1056 s
= sprintf(keep_arg
, "--keep=receive-pack %"PRIuMAX
" on ", (uintmax_t) getpid());
1057 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
1058 strcpy(keep_arg
+ s
, "localhost");
1060 argv_array_pushl(&av
, "index-pack",
1061 "--stdin", hdr_arg
, keep_arg
, NULL
);
1063 argv_array_push(&av
, "--strict");
1065 argv_array_push(&av
, "--fix-thin");
1066 child
.argv
= av
.argv
;
1070 status
= start_command(&child
);
1072 return "index-pack fork failed";
1073 pack_lockfile
= index_pack_lockfile(child
.out
);
1075 status
= finish_command(&child
);
1077 return "index-pack abnormal exit";
1078 reprepare_packed_git();
1083 static const char *unpack_with_sideband(struct shallow_info
*si
)
1089 return unpack(0, si
);
1091 memset(&muxer
, 0, sizeof(muxer
));
1092 muxer
.proc
= copy_to_sideband
;
1094 if (start_async(&muxer
))
1097 ret
= unpack(muxer
.in
, si
);
1099 finish_async(&muxer
);
1103 static void prepare_shallow_update(struct command
*commands
,
1104 struct shallow_info
*si
)
1106 int i
, j
, k
, bitmap_size
= (si
->ref
->nr
+ 31) / 32;
1108 si
->used_shallow
= xmalloc(sizeof(*si
->used_shallow
) *
1110 assign_shallow_commits_to_refs(si
, si
->used_shallow
, NULL
);
1112 si
->need_reachability_test
=
1113 xcalloc(si
->shallow
->nr
, sizeof(*si
->need_reachability_test
));
1115 xcalloc(si
->shallow
->nr
, sizeof(*si
->reachable
));
1116 si
->shallow_ref
= xcalloc(si
->ref
->nr
, sizeof(*si
->shallow_ref
));
1118 for (i
= 0; i
< si
->nr_ours
; i
++)
1119 si
->need_reachability_test
[si
->ours
[i
]] = 1;
1121 for (i
= 0; i
< si
->shallow
->nr
; i
++) {
1122 if (!si
->used_shallow
[i
])
1124 for (j
= 0; j
< bitmap_size
; j
++) {
1125 if (!si
->used_shallow
[i
][j
])
1127 si
->need_reachability_test
[i
]++;
1128 for (k
= 0; k
< 32; k
++)
1129 if (si
->used_shallow
[i
][j
] & (1 << k
))
1130 si
->shallow_ref
[j
* 32 + k
]++;
1134 * true for those associated with some refs and belong
1135 * in "ours" list aka "step 7 not done yet"
1137 si
->need_reachability_test
[i
] =
1138 si
->need_reachability_test
[i
] > 1;
1142 * keep hooks happy by forcing a temporary shallow file via
1143 * env variable because we can't add --shallow-file to every
1144 * command. check_everything_connected() will be done with
1145 * true .git/shallow though.
1147 setenv(GIT_SHALLOW_FILE_ENVIRONMENT
, alt_shallow_file
, 1);
1150 static void update_shallow_info(struct command
*commands
,
1151 struct shallow_info
*si
,
1152 struct sha1_array
*ref
)
1154 struct command
*cmd
;
1156 remove_nonexistent_theirs_shallow(si
);
1157 if (!si
->nr_ours
&& !si
->nr_theirs
) {
1162 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1163 if (is_null_sha1(cmd
->new_sha1
))
1165 sha1_array_append(ref
, cmd
->new_sha1
);
1166 cmd
->index
= ref
->nr
- 1;
1170 if (shallow_update
) {
1171 prepare_shallow_update(commands
, si
);
1175 ref_status
= xmalloc(sizeof(*ref_status
) * ref
->nr
);
1176 assign_shallow_commits_to_refs(si
, NULL
, ref_status
);
1177 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1178 if (is_null_sha1(cmd
->new_sha1
))
1180 if (ref_status
[cmd
->index
]) {
1181 cmd
->error_string
= "shallow update not allowed";
1182 cmd
->skip_update
= 1;
1188 static void report(struct command
*commands
, const char *unpack_status
)
1190 struct command
*cmd
;
1191 struct strbuf buf
= STRBUF_INIT
;
1193 packet_buf_write(&buf
, "unpack %s\n",
1194 unpack_status
? unpack_status
: "ok");
1195 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1196 if (!cmd
->error_string
)
1197 packet_buf_write(&buf
, "ok %s\n",
1200 packet_buf_write(&buf
, "ng %s %s\n",
1201 cmd
->ref_name
, cmd
->error_string
);
1203 packet_buf_flush(&buf
);
1206 send_sideband(1, 1, buf
.buf
, buf
.len
, use_sideband
);
1208 write_or_die(1, buf
.buf
, buf
.len
);
1209 strbuf_release(&buf
);
1212 static int delete_only(struct command
*commands
)
1214 struct command
*cmd
;
1215 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
1216 if (!is_null_sha1(cmd
->new_sha1
))
1222 int cmd_receive_pack(int argc
, const char **argv
, const char *prefix
)
1224 int advertise_refs
= 0;
1225 int stateless_rpc
= 0;
1227 const char *dir
= NULL
;
1228 struct command
*commands
;
1229 struct sha1_array shallow
= SHA1_ARRAY_INIT
;
1230 struct sha1_array ref
= SHA1_ARRAY_INIT
;
1231 struct shallow_info si
;
1233 packet_trace_identity("receive-pack");
1236 for (i
= 1; i
< argc
; i
++) {
1237 const char *arg
= *argv
++;
1240 if (!strcmp(arg
, "--quiet")) {
1245 if (!strcmp(arg
, "--advertise-refs")) {
1249 if (!strcmp(arg
, "--stateless-rpc")) {
1253 if (!strcmp(arg
, "--reject-thin-pack-for-testing")) {
1258 usage(receive_pack_usage
);
1261 usage(receive_pack_usage
);
1265 usage(receive_pack_usage
);
1269 if (!enter_repo(dir
, 0))
1270 die("'%s' does not appear to be a git repository", dir
);
1272 git_config(receive_pack_config
, NULL
);
1274 if (0 <= transfer_unpack_limit
)
1275 unpack_limit
= transfer_unpack_limit
;
1276 else if (0 <= receive_unpack_limit
)
1277 unpack_limit
= receive_unpack_limit
;
1279 if (advertise_refs
|| !stateless_rpc
) {
1285 if ((commands
= read_head_info(&shallow
)) != NULL
) {
1286 const char *unpack_status
= NULL
;
1288 prepare_shallow_info(&si
, &shallow
);
1289 if (!si
.nr_ours
&& !si
.nr_theirs
)
1291 if (!delete_only(commands
)) {
1292 unpack_status
= unpack_with_sideband(&si
);
1293 update_shallow_info(commands
, &si
, &ref
);
1295 execute_commands(commands
, unpack_status
, &si
);
1297 unlink_or_warn(pack_lockfile
);
1299 report(commands
, unpack_status
);
1300 run_receive_hook(commands
, "post-receive", 1);
1301 run_update_post_hook(commands
);
1303 const char *argv_gc_auto
[] = {
1304 "gc", "--auto", "--quiet", NULL
,
1306 int opt
= RUN_GIT_CMD
| RUN_COMMAND_STDOUT_TO_STDERR
;
1307 run_command_v_opt(argv_gc_auto
, opt
);
1309 if (auto_update_server_info
)
1310 update_server_info(0);
1311 clear_shallow_info(&si
);
1315 sha1_array_clear(&shallow
);
1316 sha1_array_clear(&ref
);