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>";
28 static int deny_deletes
;
29 static int deny_non_fast_forwards
;
30 static enum deny_action deny_current_branch
= DENY_UNCONFIGURED
;
31 static enum deny_action deny_delete_current
= DENY_UNCONFIGURED
;
32 static int receive_fsck_objects
= -1;
33 static int transfer_fsck_objects
= -1;
34 static int receive_unpack_limit
= -1;
35 static int transfer_unpack_limit
= -1;
36 static int unpack_limit
= 100;
37 static int report_status
;
38 static int use_sideband
;
40 static int prefer_ofs_delta
= 1;
41 static int auto_update_server_info
;
42 static int auto_gc
= 1;
43 static int fix_thin
= 1;
44 static const char *head_name
;
45 static void *head_name_to_free
;
46 static int sent_capabilities
;
47 static const char *alt_shallow_file
;
49 static enum deny_action
parse_deny_action(const char *var
, const char *value
)
52 if (!strcasecmp(value
, "ignore"))
54 if (!strcasecmp(value
, "warn"))
56 if (!strcasecmp(value
, "refuse"))
59 if (git_config_bool(var
, value
))
64 static int receive_pack_config(const char *var
, const char *value
, void *cb
)
66 int status
= parse_hide_refs_config(var
, value
, "receive");
71 if (strcmp(var
, "receive.denydeletes") == 0) {
72 deny_deletes
= git_config_bool(var
, value
);
76 if (strcmp(var
, "receive.denynonfastforwards") == 0) {
77 deny_non_fast_forwards
= git_config_bool(var
, value
);
81 if (strcmp(var
, "receive.unpacklimit") == 0) {
82 receive_unpack_limit
= git_config_int(var
, value
);
86 if (strcmp(var
, "transfer.unpacklimit") == 0) {
87 transfer_unpack_limit
= git_config_int(var
, value
);
91 if (strcmp(var
, "receive.fsckobjects") == 0) {
92 receive_fsck_objects
= git_config_bool(var
, value
);
96 if (strcmp(var
, "transfer.fsckobjects") == 0) {
97 transfer_fsck_objects
= git_config_bool(var
, value
);
101 if (!strcmp(var
, "receive.denycurrentbranch")) {
102 deny_current_branch
= parse_deny_action(var
, value
);
106 if (strcmp(var
, "receive.denydeletecurrent") == 0) {
107 deny_delete_current
= parse_deny_action(var
, value
);
111 if (strcmp(var
, "repack.usedeltabaseoffset") == 0) {
112 prefer_ofs_delta
= git_config_bool(var
, value
);
116 if (strcmp(var
, "receive.updateserverinfo") == 0) {
117 auto_update_server_info
= git_config_bool(var
, value
);
121 if (strcmp(var
, "receive.autogc") == 0) {
122 auto_gc
= git_config_bool(var
, value
);
126 return git_default_config(var
, value
, cb
);
129 static void show_ref(const char *path
, const unsigned char *sha1
)
131 if (ref_is_hidden(path
))
134 if (sent_capabilities
)
135 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
137 packet_write(1, "%s %s%c%s%s agent=%s\n",
138 sha1_to_hex(sha1
), path
, 0,
139 " report-status delete-refs side-band-64k quiet",
140 prefer_ofs_delta
? " ofs-delta" : "",
141 git_user_agent_sanitized());
142 sent_capabilities
= 1;
145 static int show_ref_cb(const char *path
, const unsigned char *sha1
, int flag
, void *unused
)
147 path
= strip_namespace(path
);
149 * Advertise refs outside our current namespace as ".have"
150 * refs, so that the client can use them to minimize data
151 * transfer but will otherwise ignore them. This happens to
152 * cover ".have" that are thrown in by add_one_alternate_ref()
153 * to mark histories that are complete in our alternates as
158 show_ref(path
, sha1
);
162 static void show_one_alternate_sha1(const unsigned char sha1
[20], void *unused
)
164 show_ref(".have", sha1
);
167 static void collect_one_alternate_ref(const struct ref
*ref
, void *data
)
169 struct sha1_array
*sa
= data
;
170 sha1_array_append(sa
, ref
->old_sha1
);
173 static void write_head_info(void)
175 struct sha1_array sa
= SHA1_ARRAY_INIT
;
176 for_each_alternate_ref(collect_one_alternate_ref
, &sa
);
177 sha1_array_for_each_unique(&sa
, show_one_alternate_sha1
, NULL
);
178 sha1_array_clear(&sa
);
179 for_each_ref(show_ref_cb
, NULL
);
180 if (!sent_capabilities
)
181 show_ref("capabilities^{}", null_sha1
);
183 advertise_shallow_grafts(1);
190 struct command
*next
;
191 const char *error_string
;
192 unsigned int skip_update
:1,
195 unsigned char old_sha1
[20];
196 unsigned char new_sha1
[20];
197 char ref_name
[FLEX_ARRAY
]; /* more */
200 static void rp_error(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
201 static void rp_warning(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
203 static void report_message(const char *prefix
, const char *err
, va_list params
)
205 int sz
= strlen(prefix
);
208 strncpy(msg
, prefix
, sz
);
209 sz
+= vsnprintf(msg
+ sz
, sizeof(msg
) - sz
, err
, params
);
210 if (sz
> (sizeof(msg
) - 1))
211 sz
= sizeof(msg
) - 1;
215 send_sideband(1, 2, msg
, sz
, use_sideband
);
220 static void rp_warning(const char *err
, ...)
223 va_start(params
, err
);
224 report_message("warning: ", err
, params
);
228 static void rp_error(const char *err
, ...)
231 va_start(params
, err
);
232 report_message("error: ", err
, params
);
236 static int copy_to_sideband(int in
, int out
, void *arg
)
240 ssize_t sz
= xread(in
, data
, sizeof(data
));
243 send_sideband(1, 2, data
, sz
, use_sideband
);
249 typedef int (*feed_fn
)(void *, const char **, size_t *);
250 static int run_and_feed_hook(const char *hook_name
, feed_fn feed
, void *feed_state
)
252 struct child_process proc
;
257 argv
[0] = find_hook(hook_name
);
263 memset(&proc
, 0, sizeof(proc
));
266 proc
.stdout_to_stderr
= 1;
269 memset(&muxer
, 0, sizeof(muxer
));
270 muxer
.proc
= copy_to_sideband
;
272 code
= start_async(&muxer
);
278 code
= start_command(&proc
);
281 finish_async(&muxer
);
288 if (feed(feed_state
, &buf
, &n
))
290 if (write_in_full(proc
.in
, buf
, n
) != n
)
295 finish_async(&muxer
);
296 return finish_command(&proc
);
299 struct receive_hook_feed_state
{
305 static int feed_receive_hook(void *state_
, const char **bufp
, size_t *sizep
)
307 struct receive_hook_feed_state
*state
= state_
;
308 struct command
*cmd
= state
->cmd
;
311 state
->skip_broken
&& (cmd
->error_string
|| cmd
->did_not_exist
))
315 strbuf_reset(&state
->buf
);
316 strbuf_addf(&state
->buf
, "%s %s %s\n",
317 sha1_to_hex(cmd
->old_sha1
), sha1_to_hex(cmd
->new_sha1
),
319 state
->cmd
= cmd
->next
;
321 *bufp
= state
->buf
.buf
;
322 *sizep
= state
->buf
.len
;
327 static int run_receive_hook(struct command
*commands
, const char *hook_name
,
330 struct receive_hook_feed_state state
;
333 strbuf_init(&state
.buf
, 0);
334 state
.cmd
= commands
;
335 state
.skip_broken
= skip_broken
;
336 if (feed_receive_hook(&state
, NULL
, NULL
))
338 state
.cmd
= commands
;
339 status
= run_and_feed_hook(hook_name
, feed_receive_hook
, &state
);
340 strbuf_release(&state
.buf
);
344 static int run_update_hook(struct command
*cmd
)
347 struct child_process proc
;
350 argv
[0] = find_hook("update");
354 argv
[1] = cmd
->ref_name
;
355 argv
[2] = sha1_to_hex(cmd
->old_sha1
);
356 argv
[3] = sha1_to_hex(cmd
->new_sha1
);
359 memset(&proc
, 0, sizeof(proc
));
361 proc
.stdout_to_stderr
= 1;
362 proc
.err
= use_sideband
? -1 : 0;
365 code
= start_command(&proc
);
369 copy_to_sideband(proc
.err
, -1, NULL
);
370 return finish_command(&proc
);
373 static int is_ref_checked_out(const char *ref
)
375 if (is_bare_repository())
380 return !strcmp(head_name
, ref
);
383 static char *refuse_unconfigured_deny_msg
[] = {
384 "By default, updating the current branch in a non-bare repository",
385 "is denied, because it will make the index and work tree inconsistent",
386 "with what you pushed, and will require 'git reset --hard' to match",
387 "the work tree to HEAD.",
389 "You can set 'receive.denyCurrentBranch' configuration variable to",
390 "'ignore' or 'warn' in the remote repository to allow pushing into",
391 "its current branch; however, this is not recommended unless you",
392 "arranged to update its work tree to match what you pushed in some",
395 "To squelch this message and still keep the default behaviour, set",
396 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
399 static void refuse_unconfigured_deny(void)
402 for (i
= 0; i
< ARRAY_SIZE(refuse_unconfigured_deny_msg
); i
++)
403 rp_error("%s", refuse_unconfigured_deny_msg
[i
]);
406 static char *refuse_unconfigured_deny_delete_current_msg
[] = {
407 "By default, deleting the current branch is denied, because the next",
408 "'git clone' won't result in any file checked out, causing confusion.",
410 "You can set 'receive.denyDeleteCurrent' configuration variable to",
411 "'warn' or 'ignore' in the remote repository to allow deleting the",
412 "current branch, with or without a warning message.",
414 "To squelch this message, you can set it to 'refuse'."
417 static void refuse_unconfigured_deny_delete_current(void)
421 i
< ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg
);
423 rp_error("%s", refuse_unconfigured_deny_delete_current_msg
[i
]);
426 static const char *update(struct command
*cmd
)
428 const char *name
= cmd
->ref_name
;
429 struct strbuf namespaced_name_buf
= STRBUF_INIT
;
430 const char *namespaced_name
;
431 unsigned char *old_sha1
= cmd
->old_sha1
;
432 unsigned char *new_sha1
= cmd
->new_sha1
;
433 struct ref_lock
*lock
;
435 /* only refs/... are allowed */
436 if (prefixcmp(name
, "refs/") || check_refname_format(name
+ 5, 0)) {
437 rp_error("refusing to create funny ref '%s' remotely", name
);
438 return "funny refname";
441 strbuf_addf(&namespaced_name_buf
, "%s%s", get_git_namespace(), name
);
442 namespaced_name
= strbuf_detach(&namespaced_name_buf
, NULL
);
444 if (is_ref_checked_out(namespaced_name
)) {
445 switch (deny_current_branch
) {
449 rp_warning("updating the current branch");
452 case DENY_UNCONFIGURED
:
453 rp_error("refusing to update checked out branch: %s", name
);
454 if (deny_current_branch
== DENY_UNCONFIGURED
)
455 refuse_unconfigured_deny();
456 return "branch is currently checked out";
460 if (!is_null_sha1(new_sha1
) && !has_sha1_file(new_sha1
)) {
461 error("unpack should have generated %s, "
462 "but I can't find it!", sha1_to_hex(new_sha1
));
466 if (!is_null_sha1(old_sha1
) && is_null_sha1(new_sha1
)) {
467 if (deny_deletes
&& !prefixcmp(name
, "refs/heads/")) {
468 rp_error("denying ref deletion for %s", name
);
469 return "deletion prohibited";
472 if (!strcmp(namespaced_name
, head_name
)) {
473 switch (deny_delete_current
) {
477 rp_warning("deleting the current branch");
480 case DENY_UNCONFIGURED
:
481 if (deny_delete_current
== DENY_UNCONFIGURED
)
482 refuse_unconfigured_deny_delete_current();
483 rp_error("refusing to delete the current branch: %s", name
);
484 return "deletion of the current branch prohibited";
489 if (deny_non_fast_forwards
&& !is_null_sha1(new_sha1
) &&
490 !is_null_sha1(old_sha1
) &&
491 !prefixcmp(name
, "refs/heads/")) {
492 struct object
*old_object
, *new_object
;
493 struct commit
*old_commit
, *new_commit
;
495 old_object
= parse_object(old_sha1
);
496 new_object
= parse_object(new_sha1
);
498 if (!old_object
|| !new_object
||
499 old_object
->type
!= OBJ_COMMIT
||
500 new_object
->type
!= OBJ_COMMIT
) {
501 error("bad sha1 objects for %s", name
);
504 old_commit
= (struct commit
*)old_object
;
505 new_commit
= (struct commit
*)new_object
;
506 if (!in_merge_bases(old_commit
, new_commit
)) {
507 rp_error("denying non-fast-forward %s"
508 " (you should pull first)", name
);
509 return "non-fast-forward";
512 if (run_update_hook(cmd
)) {
513 rp_error("hook declined to update %s", name
);
514 return "hook declined";
517 if (is_null_sha1(new_sha1
)) {
518 if (!parse_object(old_sha1
)) {
520 if (ref_exists(name
)) {
521 rp_warning("Allowing deletion of corrupt ref.");
523 rp_warning("Deleting a non-existent ref.");
524 cmd
->did_not_exist
= 1;
527 if (delete_ref(namespaced_name
, old_sha1
, 0)) {
528 rp_error("failed to delete %s", name
);
529 return "failed to delete";
531 return NULL
; /* good */
534 lock
= lock_any_ref_for_update(namespaced_name
, old_sha1
,
537 rp_error("failed to lock %s", name
);
538 return "failed to lock";
540 if (write_ref_sha1(lock
, new_sha1
, "push")) {
541 return "failed to write"; /* error() already called */
543 return NULL
; /* good */
547 static void run_update_post_hook(struct command
*commands
)
552 struct child_process proc
;
555 hook
= find_hook("post-update");
556 for (argc
= 0, cmd
= commands
; cmd
; cmd
= cmd
->next
) {
557 if (cmd
->error_string
|| cmd
->did_not_exist
)
564 argv
= xmalloc(sizeof(*argv
) * (2 + argc
));
567 for (argc
= 1, cmd
= commands
; cmd
; cmd
= cmd
->next
) {
569 if (cmd
->error_string
|| cmd
->did_not_exist
)
571 p
= xmalloc(strlen(cmd
->ref_name
) + 1);
572 strcpy(p
, cmd
->ref_name
);
578 memset(&proc
, 0, sizeof(proc
));
580 proc
.stdout_to_stderr
= 1;
581 proc
.err
= use_sideband
? -1 : 0;
584 if (!start_command(&proc
)) {
586 copy_to_sideband(proc
.err
, -1, NULL
);
587 finish_command(&proc
);
591 static void check_aliased_update(struct command
*cmd
, struct string_list
*list
)
593 struct strbuf buf
= STRBUF_INIT
;
594 const char *dst_name
;
595 struct string_list_item
*item
;
596 struct command
*dst_cmd
;
597 unsigned char sha1
[20];
598 char cmd_oldh
[41], cmd_newh
[41], dst_oldh
[41], dst_newh
[41];
601 strbuf_addf(&buf
, "%s%s", get_git_namespace(), cmd
->ref_name
);
602 dst_name
= resolve_ref_unsafe(buf
.buf
, sha1
, 0, &flag
);
603 strbuf_release(&buf
);
605 if (!(flag
& REF_ISSYMREF
))
608 dst_name
= strip_namespace(dst_name
);
610 rp_error("refusing update to broken symref '%s'", cmd
->ref_name
);
611 cmd
->skip_update
= 1;
612 cmd
->error_string
= "broken symref";
616 if ((item
= string_list_lookup(list
, dst_name
)) == NULL
)
619 cmd
->skip_update
= 1;
621 dst_cmd
= (struct command
*) item
->util
;
623 if (!hashcmp(cmd
->old_sha1
, dst_cmd
->old_sha1
) &&
624 !hashcmp(cmd
->new_sha1
, dst_cmd
->new_sha1
))
627 dst_cmd
->skip_update
= 1;
629 strcpy(cmd_oldh
, find_unique_abbrev(cmd
->old_sha1
, DEFAULT_ABBREV
));
630 strcpy(cmd_newh
, find_unique_abbrev(cmd
->new_sha1
, DEFAULT_ABBREV
));
631 strcpy(dst_oldh
, find_unique_abbrev(dst_cmd
->old_sha1
, DEFAULT_ABBREV
));
632 strcpy(dst_newh
, find_unique_abbrev(dst_cmd
->new_sha1
, DEFAULT_ABBREV
));
633 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
634 " its target '%s' (%s..%s)",
635 cmd
->ref_name
, cmd_oldh
, cmd_newh
,
636 dst_cmd
->ref_name
, dst_oldh
, dst_newh
);
638 cmd
->error_string
= dst_cmd
->error_string
=
639 "inconsistent aliased update";
642 static void check_aliased_updates(struct command
*commands
)
645 struct string_list ref_list
= STRING_LIST_INIT_NODUP
;
647 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
648 struct string_list_item
*item
=
649 string_list_append(&ref_list
, cmd
->ref_name
);
650 item
->util
= (void *)cmd
;
652 sort_string_list(&ref_list
);
654 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
655 if (!cmd
->error_string
)
656 check_aliased_update(cmd
, &ref_list
);
659 string_list_clear(&ref_list
, 0);
662 static int command_singleton_iterator(void *cb_data
, unsigned char sha1
[20])
664 struct command
**cmd_list
= cb_data
;
665 struct command
*cmd
= *cmd_list
;
667 if (!cmd
|| is_null_sha1(cmd
->new_sha1
))
668 return -1; /* end of list */
669 *cmd_list
= NULL
; /* this returns only one */
670 hashcpy(sha1
, cmd
->new_sha1
);
674 static void set_connectivity_errors(struct command
*commands
)
678 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
679 struct command
*singleton
= cmd
;
680 if (!check_everything_connected(command_singleton_iterator
,
683 cmd
->error_string
= "missing necessary objects";
687 static int iterate_receive_command_list(void *cb_data
, unsigned char sha1
[20])
689 struct command
**cmd_list
= cb_data
;
690 struct command
*cmd
= *cmd_list
;
693 if (!is_null_sha1(cmd
->new_sha1
) && !cmd
->skip_update
) {
694 hashcpy(sha1
, cmd
->new_sha1
);
695 *cmd_list
= cmd
->next
;
701 return -1; /* end of list */
704 static void reject_updates_to_hidden(struct command
*commands
)
708 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
709 if (cmd
->error_string
|| !ref_is_hidden(cmd
->ref_name
))
711 if (is_null_sha1(cmd
->new_sha1
))
712 cmd
->error_string
= "deny deleting a hidden ref";
714 cmd
->error_string
= "deny updating a hidden ref";
718 static void execute_commands(struct command
*commands
, const char *unpacker_error
)
721 unsigned char sha1
[20];
723 if (unpacker_error
) {
724 for (cmd
= commands
; cmd
; cmd
= cmd
->next
)
725 cmd
->error_string
= "unpacker error";
730 if (check_everything_connected(iterate_receive_command_list
,
732 set_connectivity_errors(commands
);
734 reject_updates_to_hidden(commands
);
736 if (run_receive_hook(commands
, "pre-receive", 0)) {
737 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
738 if (!cmd
->error_string
)
739 cmd
->error_string
= "pre-receive hook declined";
744 check_aliased_updates(commands
);
746 free(head_name_to_free
);
747 head_name
= head_name_to_free
= resolve_refdup("HEAD", sha1
, 0, NULL
);
749 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
750 if (cmd
->error_string
)
753 if (cmd
->skip_update
)
756 cmd
->error_string
= update(cmd
);
760 static struct command
*read_head_info(struct sha1_array
*shallow
)
762 struct command
*commands
= NULL
;
763 struct command
**p
= &commands
;
766 unsigned char old_sha1
[20], new_sha1
[20];
771 line
= packet_read_line(0, &len
);
775 if (len
== 48 && !prefixcmp(line
, "shallow ")) {
776 if (get_sha1_hex(line
+ 8, old_sha1
))
777 die("protocol error: expected shallow sha, got '%s'", line
+ 8);
778 sha1_array_append(shallow
, old_sha1
);
785 get_sha1_hex(line
, old_sha1
) ||
786 get_sha1_hex(line
+ 41, new_sha1
))
787 die("protocol error: expected old/new/ref, got '%s'",
791 reflen
= strlen(refname
);
792 if (reflen
+ 82 < len
) {
793 const char *feature_list
= refname
+ reflen
+ 1;
794 if (parse_feature_request(feature_list
, "report-status"))
796 if (parse_feature_request(feature_list
, "side-band-64k"))
797 use_sideband
= LARGE_PACKET_MAX
;
798 if (parse_feature_request(feature_list
, "quiet"))
801 cmd
= xcalloc(1, sizeof(struct command
) + len
- 80);
802 hashcpy(cmd
->old_sha1
, old_sha1
);
803 hashcpy(cmd
->new_sha1
, new_sha1
);
804 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
811 static const char *parse_pack_header(struct pack_header
*hdr
)
813 switch (read_pack_header(0, hdr
)) {
815 return "eof before pack header was fully read";
817 case PH_ERROR_PACK_SIGNATURE
:
818 return "protocol error (pack signature mismatch detected)";
820 case PH_ERROR_PROTOCOL
:
821 return "protocol error (pack version unsupported)";
824 return "unknown error in parse_pack_header";
831 static const char *pack_lockfile
;
833 static const char *unpack(int err_fd
, struct shallow_info
*si
)
835 struct pack_header hdr
;
836 struct argv_array av
= ARGV_ARRAY_INIT
;
840 struct child_process child
;
841 int fsck_objects
= (receive_fsck_objects
>= 0
842 ? receive_fsck_objects
843 : transfer_fsck_objects
>= 0
844 ? transfer_fsck_objects
847 hdr_err
= parse_pack_header(&hdr
);
853 snprintf(hdr_arg
, sizeof(hdr_arg
),
854 "--pack_header=%"PRIu32
",%"PRIu32
,
855 ntohl(hdr
.hdr_version
), ntohl(hdr
.hdr_entries
));
857 if (si
->nr_ours
|| si
->nr_theirs
) {
858 alt_shallow_file
= setup_temporary_shallow(si
->shallow
);
859 argv_array_pushl(&av
, "--shallow-file", alt_shallow_file
, NULL
);
862 memset(&child
, 0, sizeof(child
));
863 if (ntohl(hdr
.hdr_entries
) < unpack_limit
) {
864 argv_array_pushl(&av
, "unpack-objects", hdr_arg
, NULL
);
866 argv_array_push(&av
, "-q");
868 argv_array_push(&av
, "--strict");
869 child
.argv
= av
.argv
;
873 status
= run_command(&child
);
875 return "unpack-objects abnormal exit";
880 s
= sprintf(keep_arg
, "--keep=receive-pack %"PRIuMAX
" on ", (uintmax_t) getpid());
881 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
882 strcpy(keep_arg
+ s
, "localhost");
884 argv_array_pushl(&av
, "index-pack",
885 "--stdin", hdr_arg
, keep_arg
, NULL
);
887 argv_array_push(&av
, "--strict");
889 argv_array_push(&av
, "--fix-thin");
890 child
.argv
= av
.argv
;
894 status
= start_command(&child
);
896 return "index-pack fork failed";
897 pack_lockfile
= index_pack_lockfile(child
.out
);
899 status
= finish_command(&child
);
901 return "index-pack abnormal exit";
902 reprepare_packed_git();
907 static const char *unpack_with_sideband(struct shallow_info
*si
)
913 return unpack(0, si
);
915 memset(&muxer
, 0, sizeof(muxer
));
916 muxer
.proc
= copy_to_sideband
;
918 if (start_async(&muxer
))
921 ret
= unpack(muxer
.in
, si
);
923 finish_async(&muxer
);
927 static void update_shallow_info(struct command
*commands
,
928 struct shallow_info
*si
,
929 struct sha1_array
*ref
)
933 remove_nonexistent_theirs_shallow(si
);
934 /* XXX remove_nonexistent_ours_in_pack() */
935 if (!si
->nr_ours
&& !si
->nr_theirs
)
938 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
939 if (is_null_sha1(cmd
->new_sha1
))
941 sha1_array_append(ref
, cmd
->new_sha1
);
942 cmd
->index
= ref
->nr
- 1;
946 ref_status
= xmalloc(sizeof(*ref_status
) * ref
->nr
);
947 assign_shallow_commits_to_refs(si
, NULL
, ref_status
);
948 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
949 if (is_null_sha1(cmd
->new_sha1
))
951 if (ref_status
[cmd
->index
]) {
952 cmd
->error_string
= "shallow update not allowed";
953 cmd
->skip_update
= 1;
956 if (alt_shallow_file
&& *alt_shallow_file
) {
957 unlink(alt_shallow_file
);
958 alt_shallow_file
= NULL
;
963 static void report(struct command
*commands
, const char *unpack_status
)
966 struct strbuf buf
= STRBUF_INIT
;
968 packet_buf_write(&buf
, "unpack %s\n",
969 unpack_status
? unpack_status
: "ok");
970 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
971 if (!cmd
->error_string
)
972 packet_buf_write(&buf
, "ok %s\n",
975 packet_buf_write(&buf
, "ng %s %s\n",
976 cmd
->ref_name
, cmd
->error_string
);
978 packet_buf_flush(&buf
);
981 send_sideband(1, 1, buf
.buf
, buf
.len
, use_sideband
);
983 write_or_die(1, buf
.buf
, buf
.len
);
984 strbuf_release(&buf
);
987 static int delete_only(struct command
*commands
)
990 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
991 if (!is_null_sha1(cmd
->new_sha1
))
997 int cmd_receive_pack(int argc
, const char **argv
, const char *prefix
)
999 int advertise_refs
= 0;
1000 int stateless_rpc
= 0;
1003 struct command
*commands
;
1004 struct sha1_array shallow
= SHA1_ARRAY_INIT
;
1005 struct sha1_array ref
= SHA1_ARRAY_INIT
;
1006 struct shallow_info si
;
1008 packet_trace_identity("receive-pack");
1011 for (i
= 1; i
< argc
; i
++) {
1012 const char *arg
= *argv
++;
1015 if (!strcmp(arg
, "--quiet")) {
1020 if (!strcmp(arg
, "--advertise-refs")) {
1024 if (!strcmp(arg
, "--stateless-rpc")) {
1028 if (!strcmp(arg
, "--reject-thin-pack-for-testing")) {
1033 usage(receive_pack_usage
);
1036 usage(receive_pack_usage
);
1040 usage(receive_pack_usage
);
1044 if (!enter_repo(dir
, 0))
1045 die("'%s' does not appear to be a git repository", dir
);
1047 if (is_repository_shallow() && stateless_rpc
)
1048 die("attempt to push into a shallow repository");
1050 git_config(receive_pack_config
, NULL
);
1052 if (0 <= transfer_unpack_limit
)
1053 unpack_limit
= transfer_unpack_limit
;
1054 else if (0 <= receive_unpack_limit
)
1055 unpack_limit
= receive_unpack_limit
;
1057 if (advertise_refs
|| !stateless_rpc
) {
1063 if ((commands
= read_head_info(&shallow
)) != NULL
) {
1064 const char *unpack_status
= NULL
;
1066 prepare_shallow_info(&si
, &shallow
);
1067 if (!delete_only(commands
)) {
1068 unpack_status
= unpack_with_sideband(&si
);
1069 update_shallow_info(commands
, &si
, &ref
);
1071 execute_commands(commands
, unpack_status
);
1073 unlink_or_warn(pack_lockfile
);
1075 report(commands
, unpack_status
);
1076 run_receive_hook(commands
, "post-receive", 1);
1077 run_update_post_hook(commands
);
1079 const char *argv_gc_auto
[] = {
1080 "gc", "--auto", "--quiet", NULL
,
1082 int opt
= RUN_GIT_CMD
| RUN_COMMAND_STDOUT_TO_STDERR
;
1083 run_command_v_opt(argv_gc_auto
, opt
);
1085 if (auto_update_server_info
)
1086 update_server_info(0);
1087 clear_shallow_info(&si
);
1091 sha1_array_clear(&shallow
);
1092 sha1_array_clear(&ref
);