10 #include "fetch-pack.h"
12 #include "run-command.h"
14 #include "transport.h"
16 #include "prio-queue.h"
17 #include "sha1-array.h"
19 static int transfer_unpack_limit
= -1;
20 static int fetch_unpack_limit
= -1;
21 static int unpack_limit
= 100;
22 static int prefer_ofs_delta
= 1;
24 static int fetch_fsck_objects
= -1;
25 static int transfer_fsck_objects
= -1;
26 static int agent_supported
;
27 static struct lock_file shallow_lock
;
28 static const char *alternate_shallow_file
;
30 /* Remember to update object flag allocation in object.h */
31 #define COMPLETE (1U << 0)
32 #define COMMON (1U << 1)
33 #define COMMON_REF (1U << 2)
34 #define SEEN (1U << 3)
35 #define POPPED (1U << 4)
40 * After sending this many "have"s if we do not get any new ACK , we
41 * give up traversing our history.
43 #define MAX_IN_VAIN 256
45 static struct prio_queue rev_list
= { compare_commits_by_commit_date
};
46 static int non_common_revs
, multi_ack
, use_sideband
, allow_tip_sha1_in_want
;
48 static void rev_list_push(struct commit
*commit
, int mark
)
50 if (!(commit
->object
.flags
& mark
)) {
51 commit
->object
.flags
|= mark
;
53 if (parse_commit(commit
))
56 prio_queue_put(&rev_list
, commit
);
58 if (!(commit
->object
.flags
& COMMON
))
63 static int rev_list_insert_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
65 struct object
*o
= deref_tag(parse_object(sha1
), refname
, 0);
67 if (o
&& o
->type
== OBJ_COMMIT
)
68 rev_list_push((struct commit
*)o
, SEEN
);
73 static int clear_marks(const char *refname
, const struct object_id
*oid
,
74 int flag
, void *cb_data
)
76 struct object
*o
= deref_tag(parse_object(oid
->hash
), refname
, 0);
78 if (o
&& o
->type
== OBJ_COMMIT
)
79 clear_commit_marks((struct commit
*)o
,
80 COMMON
| COMMON_REF
| SEEN
| POPPED
);
85 This function marks a rev and its ancestors as common.
86 In some cases, it is desirable to mark only the ancestors (for example
87 when only the server does not yet know that they are common).
90 static void mark_common(struct commit
*commit
,
91 int ancestors_only
, int dont_parse
)
93 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
94 struct object
*o
= (struct object
*)commit
;
99 if (!(o
->flags
& SEEN
))
100 rev_list_push(commit
, SEEN
);
102 struct commit_list
*parents
;
104 if (!ancestors_only
&& !(o
->flags
& POPPED
))
106 if (!o
->parsed
&& !dont_parse
)
107 if (parse_commit(commit
))
110 for (parents
= commit
->parents
;
112 parents
= parents
->next
)
113 mark_common(parents
->item
, 0, dont_parse
);
119 Get the next rev to send, ignoring the common.
122 static const unsigned char *get_rev(void)
124 struct commit
*commit
= NULL
;
126 while (commit
== NULL
) {
128 struct commit_list
*parents
;
130 if (rev_list
.nr
== 0 || non_common_revs
== 0)
133 commit
= prio_queue_get(&rev_list
);
134 parse_commit(commit
);
135 parents
= commit
->parents
;
137 commit
->object
.flags
|= POPPED
;
138 if (!(commit
->object
.flags
& COMMON
))
141 if (commit
->object
.flags
& COMMON
) {
142 /* do not send "have", and ignore ancestors */
144 mark
= COMMON
| SEEN
;
145 } else if (commit
->object
.flags
& COMMON_REF
)
146 /* send "have", and ignore ancestors */
147 mark
= COMMON
| SEEN
;
149 /* send "have", also for its ancestors */
153 if (!(parents
->item
->object
.flags
& SEEN
))
154 rev_list_push(parents
->item
, mark
);
156 mark_common(parents
->item
, 1, 0);
157 parents
= parents
->next
;
161 return commit
->object
.sha1
;
172 static void consume_shallow_list(struct fetch_pack_args
*args
, int fd
)
174 if (args
->stateless_rpc
&& args
->depth
> 0) {
175 /* If we sent a depth we will get back "duplicate"
176 * shallow and unshallow commands every time there
177 * is a block of have lines exchanged.
180 while ((line
= packet_read_line(fd
, NULL
))) {
181 if (starts_with(line
, "shallow "))
183 if (starts_with(line
, "unshallow "))
185 die("git fetch-pack: expected shallow list");
190 static enum ack_type
get_ack(int fd
, unsigned char *result_sha1
)
193 char *line
= packet_read_line(fd
, &len
);
197 die("git fetch-pack: expected ACK/NAK, got EOF");
198 if (!strcmp(line
, "NAK"))
200 if (skip_prefix(line
, "ACK ", &arg
)) {
201 if (!get_sha1_hex(arg
, result_sha1
)) {
206 if (strstr(arg
, "continue"))
208 if (strstr(arg
, "common"))
210 if (strstr(arg
, "ready"))
215 die("git fetch_pack: expected ACK/NAK, got '%s'", line
);
218 static void send_request(struct fetch_pack_args
*args
,
219 int fd
, struct strbuf
*buf
)
221 if (args
->stateless_rpc
) {
222 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
225 write_or_die(fd
, buf
->buf
, buf
->len
);
228 static void insert_one_alternate_ref(const struct ref
*ref
, void *unused
)
230 rev_list_insert_ref(NULL
, ref
->old_sha1
, 0, NULL
);
233 #define INITIAL_FLUSH 16
234 #define PIPESAFE_FLUSH 32
235 #define LARGE_FLUSH 1024
237 static int next_flush(struct fetch_pack_args
*args
, int count
)
239 int flush_limit
= args
->stateless_rpc
? LARGE_FLUSH
: PIPESAFE_FLUSH
;
241 if (count
< flush_limit
)
244 count
+= flush_limit
;
248 static int find_common(struct fetch_pack_args
*args
,
249 int fd
[2], unsigned char *result_sha1
,
253 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
254 const unsigned char *sha1
;
255 unsigned in_vain
= 0;
256 int got_continue
= 0;
258 struct strbuf req_buf
= STRBUF_INIT
;
259 size_t state_len
= 0;
260 struct each_ref_fn_sha1_adapter wrapped_rev_list_insert_ref
=
261 {rev_list_insert_ref
, NULL
};
263 if (args
->stateless_rpc
&& multi_ack
== 1)
264 die("--stateless-rpc requires multi_ack_detailed");
266 for_each_ref(clear_marks
, NULL
);
269 for_each_ref(each_ref_fn_adapter
, &wrapped_rev_list_insert_ref
);
270 for_each_alternate_ref(insert_one_alternate_ref
, NULL
);
273 for ( ; refs
; refs
= refs
->next
) {
274 unsigned char *remote
= refs
->old_sha1
;
275 const char *remote_hex
;
279 * If that object is complete (i.e. it is an ancestor of a
280 * local ref), we tell them we have it but do not have to
281 * tell them about its ancestors, which they already know
284 * We use lookup_object here because we are only
285 * interested in the case we *know* the object is
286 * reachable and we have already scanned it.
288 if (((o
= lookup_object(remote
)) != NULL
) &&
289 (o
->flags
& COMPLETE
)) {
293 remote_hex
= sha1_to_hex(remote
);
295 struct strbuf c
= STRBUF_INIT
;
296 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
297 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
298 if (no_done
) strbuf_addstr(&c
, " no-done");
299 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
300 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
301 if (args
->use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
302 if (args
->no_progress
) strbuf_addstr(&c
, " no-progress");
303 if (args
->include_tag
) strbuf_addstr(&c
, " include-tag");
304 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
305 if (agent_supported
) strbuf_addf(&c
, " agent=%s",
306 git_user_agent_sanitized());
307 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
310 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
315 strbuf_release(&req_buf
);
320 if (is_repository_shallow())
321 write_shallow_commits(&req_buf
, 1, NULL
);
323 packet_buf_write(&req_buf
, "deepen %d", args
->depth
);
324 packet_buf_flush(&req_buf
);
325 state_len
= req_buf
.len
;
327 if (args
->depth
> 0) {
330 unsigned char sha1
[20];
332 send_request(args
, fd
[1], &req_buf
);
333 while ((line
= packet_read_line(fd
[0], NULL
))) {
334 if (skip_prefix(line
, "shallow ", &arg
)) {
335 if (get_sha1_hex(arg
, sha1
))
336 die("invalid shallow line: %s", line
);
337 register_shallow(sha1
);
340 if (skip_prefix(line
, "unshallow ", &arg
)) {
341 if (get_sha1_hex(arg
, sha1
))
342 die("invalid unshallow line: %s", line
);
343 if (!lookup_object(sha1
))
344 die("object not found: %s", line
);
345 /* make sure that it is parsed as shallow */
346 if (!parse_object(sha1
))
347 die("error in object: %s", line
);
348 if (unregister_shallow(sha1
))
349 die("no shallow found: %s", line
);
352 die("expected shallow/unshallow, got %s", line
);
354 } else if (!args
->stateless_rpc
)
355 send_request(args
, fd
[1], &req_buf
);
357 if (!args
->stateless_rpc
) {
358 /* If we aren't using the stateless-rpc interface
359 * we don't need to retain the headers.
361 strbuf_setlen(&req_buf
, 0);
367 while ((sha1
= get_rev())) {
368 packet_buf_write(&req_buf
, "have %s\n", sha1_to_hex(sha1
));
370 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
372 if (flush_at
<= ++count
) {
375 packet_buf_flush(&req_buf
);
376 send_request(args
, fd
[1], &req_buf
);
377 strbuf_setlen(&req_buf
, state_len
);
379 flush_at
= next_flush(args
, count
);
382 * We keep one window "ahead" of the other side, and
383 * will wait for an ACK only on the next one
385 if (!args
->stateless_rpc
&& count
== INITIAL_FLUSH
)
388 consume_shallow_list(args
, fd
[0]);
390 ack
= get_ack(fd
[0], result_sha1
);
391 if (args
->verbose
&& ack
)
392 fprintf(stderr
, "got ack %d %s\n", ack
,
393 sha1_to_hex(result_sha1
));
403 struct commit
*commit
=
404 lookup_commit(result_sha1
);
406 die("invalid commit %s", sha1_to_hex(result_sha1
));
407 if (args
->stateless_rpc
409 && !(commit
->object
.flags
& COMMON
)) {
410 /* We need to replay the have for this object
411 * on the next RPC request so the peer knows
412 * it is in common with us.
414 const char *hex
= sha1_to_hex(result_sha1
);
415 packet_buf_write(&req_buf
, "have %s\n", hex
);
416 state_len
= req_buf
.len
;
418 mark_common(commit
, 0, 1);
422 if (ack
== ACK_ready
) {
423 clear_prio_queue(&rev_list
);
431 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
433 fprintf(stderr
, "giving up\n");
439 if (!got_ready
|| !no_done
) {
440 packet_buf_write(&req_buf
, "done\n");
441 send_request(args
, fd
[1], &req_buf
);
444 fprintf(stderr
, "done\n");
449 strbuf_release(&req_buf
);
451 if (!got_ready
|| !no_done
)
452 consume_shallow_list(args
, fd
[0]);
453 while (flushes
|| multi_ack
) {
454 int ack
= get_ack(fd
[0], result_sha1
);
457 fprintf(stderr
, "got ack (%d) %s\n", ack
,
458 sha1_to_hex(result_sha1
));
466 /* it is no error to fetch into a completely empty repo */
467 return count
? retval
: 0;
470 static struct commit_list
*complete
;
472 static int mark_complete(const unsigned char *sha1
)
474 struct object
*o
= parse_object(sha1
);
476 while (o
&& o
->type
== OBJ_TAG
) {
477 struct tag
*t
= (struct tag
*) o
;
479 break; /* broken repository */
480 o
->flags
|= COMPLETE
;
481 o
= parse_object(t
->tagged
->sha1
);
483 if (o
&& o
->type
== OBJ_COMMIT
) {
484 struct commit
*commit
= (struct commit
*)o
;
485 if (!(commit
->object
.flags
& COMPLETE
)) {
486 commit
->object
.flags
|= COMPLETE
;
487 commit_list_insert(commit
, &complete
);
493 static int mark_complete_oid(const char *refname
, const struct object_id
*oid
,
494 int flag
, void *cb_data
)
496 return mark_complete(oid
->hash
);
499 static void mark_recent_complete_commits(struct fetch_pack_args
*args
,
500 unsigned long cutoff
)
502 while (complete
&& cutoff
<= complete
->item
->date
) {
504 fprintf(stderr
, "Marking %s as complete\n",
505 sha1_to_hex(complete
->item
->object
.sha1
));
506 pop_most_recent_commit(&complete
, COMPLETE
);
510 static void filter_refs(struct fetch_pack_args
*args
,
512 struct ref
**sought
, int nr_sought
)
514 struct ref
*newlist
= NULL
;
515 struct ref
**newtail
= &newlist
;
516 struct ref
*ref
, *next
;
520 for (ref
= *refs
; ref
; ref
= next
) {
524 if (starts_with(ref
->name
, "refs/") &&
525 check_refname_format(ref
->name
, 0))
528 while (i
< nr_sought
) {
529 int cmp
= strcmp(ref
->name
, sought
[i
]->name
);
531 break; /* definitely do not have it */
533 keep
= 1; /* definitely have it */
534 sought
[i
]->matched
= 1;
540 if (!keep
&& args
->fetch_all
&&
541 (!args
->depth
|| !starts_with(ref
->name
, "refs/tags/")))
547 newtail
= &ref
->next
;
553 /* Append unmatched requests to the list */
554 if (allow_tip_sha1_in_want
) {
555 for (i
= 0; i
< nr_sought
; i
++) {
556 unsigned char sha1
[20];
561 if (get_sha1_hex(ref
->name
, sha1
) ||
562 ref
->name
[40] != '\0' ||
563 hashcmp(sha1
, ref
->old_sha1
))
567 *newtail
= copy_ref(ref
);
568 newtail
= &(*newtail
)->next
;
574 static void mark_alternate_complete(const struct ref
*ref
, void *unused
)
576 mark_complete(ref
->old_sha1
);
579 static int everything_local(struct fetch_pack_args
*args
,
581 struct ref
**sought
, int nr_sought
)
585 unsigned long cutoff
= 0;
587 save_commit_buffer
= 0;
589 for (ref
= *refs
; ref
; ref
= ref
->next
) {
592 if (!has_sha1_file(ref
->old_sha1
))
595 o
= parse_object(ref
->old_sha1
);
599 /* We already have it -- which may mean that we were
600 * in sync with the other side at some time after
601 * that (it is OK if we guess wrong here).
603 if (o
->type
== OBJ_COMMIT
) {
604 struct commit
*commit
= (struct commit
*)o
;
605 if (!cutoff
|| cutoff
< commit
->date
)
606 cutoff
= commit
->date
;
611 for_each_ref(mark_complete_oid
, NULL
);
612 for_each_alternate_ref(mark_alternate_complete
, NULL
);
613 commit_list_sort_by_date(&complete
);
615 mark_recent_complete_commits(args
, cutoff
);
619 * Mark all complete remote refs as common refs.
620 * Don't mark them common yet; the server has to be told so first.
622 for (ref
= *refs
; ref
; ref
= ref
->next
) {
623 struct object
*o
= deref_tag(lookup_object(ref
->old_sha1
),
626 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
629 if (!(o
->flags
& SEEN
)) {
630 rev_list_push((struct commit
*)o
, COMMON_REF
| SEEN
);
632 mark_common((struct commit
*)o
, 1, 1);
636 filter_refs(args
, refs
, sought
, nr_sought
);
638 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
639 const unsigned char *remote
= ref
->old_sha1
;
642 o
= lookup_object(remote
);
643 if (!o
|| !(o
->flags
& COMPLETE
)) {
648 "want %s (%s)\n", sha1_to_hex(remote
),
655 "already have %s (%s)\n", sha1_to_hex(remote
),
661 static int sideband_demux(int in
, int out
, void *data
)
665 int ret
= recv_sideband("fetch-pack", xd
[0], out
);
670 static int get_pack(struct fetch_pack_args
*args
,
671 int xd
[2], char **pack_lockfile
)
674 const char *argv
[22];
677 const char **av
, *cmd_name
;
678 int do_keep
= args
->keep_pack
;
679 struct child_process cmd
= CHILD_PROCESS_INIT
;
682 memset(&demux
, 0, sizeof(demux
));
684 /* xd[] is talking with upload-pack; subprocess reads from
685 * xd[0], spits out band#2 to stderr, and feeds us band#1
686 * through demux->out.
688 demux
.proc
= sideband_demux
;
691 if (start_async(&demux
))
692 die("fetch-pack: unable to fork off sideband"
701 if (!args
->keep_pack
&& unpack_limit
) {
702 struct pack_header header
;
704 if (read_pack_header(demux
.out
, &header
))
705 die("protocol error: bad pack header");
706 snprintf(hdr_arg
, sizeof(hdr_arg
),
707 "--pack_header=%"PRIu32
",%"PRIu32
,
708 ntohl(header
.hdr_version
), ntohl(header
.hdr_entries
));
709 if (ntohl(header
.hdr_entries
) < unpack_limit
)
715 if (alternate_shallow_file
) {
716 *av
++ = "--shallow-file";
717 *av
++ = alternate_shallow_file
;
723 *av
++ = cmd_name
= "index-pack";
725 if (!args
->quiet
&& !args
->no_progress
)
727 if (args
->use_thin_pack
)
728 *av
++ = "--fix-thin";
729 if (args
->lock_pack
|| unpack_limit
) {
730 int s
= sprintf(keep_arg
,
731 "--keep=fetch-pack %"PRIuMAX
" on ", (uintmax_t) getpid());
732 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
733 strcpy(keep_arg
+ s
, "localhost");
736 if (args
->check_self_contained_and_connected
)
737 *av
++ = "--check-self-contained-and-connected";
740 *av
++ = cmd_name
= "unpack-objects";
741 if (args
->quiet
|| args
->no_progress
)
743 args
->check_self_contained_and_connected
= 0;
747 if (fetch_fsck_objects
>= 0
749 : transfer_fsck_objects
>= 0
750 ? transfer_fsck_objects
757 if (start_command(&cmd
))
758 die("fetch-pack: unable to fork off %s", cmd_name
);
759 if (do_keep
&& pack_lockfile
) {
760 *pack_lockfile
= index_pack_lockfile(cmd
.out
);
765 /* Closed by start_command() */
768 ret
= finish_command(&cmd
);
769 if (!ret
|| (args
->check_self_contained_and_connected
&& ret
== 1))
770 args
->self_contained_and_connected
=
771 args
->check_self_contained_and_connected
&&
774 die("%s failed", cmd_name
);
775 if (use_sideband
&& finish_async(&demux
))
776 die("error in sideband demultiplexer");
780 static int cmp_ref_by_name(const void *a_
, const void *b_
)
782 const struct ref
*a
= *((const struct ref
**)a_
);
783 const struct ref
*b
= *((const struct ref
**)b_
);
784 return strcmp(a
->name
, b
->name
);
787 static struct ref
*do_fetch_pack(struct fetch_pack_args
*args
,
789 const struct ref
*orig_ref
,
790 struct ref
**sought
, int nr_sought
,
791 struct shallow_info
*si
,
792 char **pack_lockfile
)
794 struct ref
*ref
= copy_ref_list(orig_ref
);
795 unsigned char sha1
[20];
796 const char *agent_feature
;
799 sort_ref_list(&ref
, ref_compare_name
);
800 qsort(sought
, nr_sought
, sizeof(*sought
), cmp_ref_by_name
);
802 if (is_repository_shallow() && !server_supports("shallow"))
803 die("Server does not support shallow clients");
804 if (server_supports("multi_ack_detailed")) {
806 fprintf(stderr
, "Server supports multi_ack_detailed\n");
808 if (server_supports("no-done")) {
810 fprintf(stderr
, "Server supports no-done\n");
811 if (args
->stateless_rpc
)
815 else if (server_supports("multi_ack")) {
817 fprintf(stderr
, "Server supports multi_ack\n");
820 if (server_supports("side-band-64k")) {
822 fprintf(stderr
, "Server supports side-band-64k\n");
825 else if (server_supports("side-band")) {
827 fprintf(stderr
, "Server supports side-band\n");
830 if (server_supports("allow-tip-sha1-in-want")) {
832 fprintf(stderr
, "Server supports allow-tip-sha1-in-want\n");
833 allow_tip_sha1_in_want
= 1;
835 if (!server_supports("thin-pack"))
836 args
->use_thin_pack
= 0;
837 if (!server_supports("no-progress"))
838 args
->no_progress
= 0;
839 if (!server_supports("include-tag"))
840 args
->include_tag
= 0;
841 if (server_supports("ofs-delta")) {
843 fprintf(stderr
, "Server supports ofs-delta\n");
845 prefer_ofs_delta
= 0;
847 if ((agent_feature
= server_feature_value("agent", &agent_len
))) {
849 if (args
->verbose
&& agent_len
)
850 fprintf(stderr
, "Server version is %.*s\n",
851 agent_len
, agent_feature
);
854 if (everything_local(args
, &ref
, sought
, nr_sought
)) {
858 if (find_common(args
, fd
, sha1
, ref
) < 0)
859 if (!args
->keep_pack
)
860 /* When cloning, it is not unusual to have
863 warning("no common commits");
865 if (args
->stateless_rpc
)
868 setup_alternate_shallow(&shallow_lock
, &alternate_shallow_file
,
870 else if (si
->nr_ours
|| si
->nr_theirs
)
871 alternate_shallow_file
= setup_temporary_shallow(si
->shallow
);
873 alternate_shallow_file
= NULL
;
874 if (get_pack(args
, fd
, pack_lockfile
))
875 die("git fetch-pack: fetch failed.");
881 static void fetch_pack_config(void)
883 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit
);
884 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit
);
885 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta
);
886 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects
);
887 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects
);
889 git_config(git_default_config
, NULL
);
892 static void fetch_pack_setup(void)
894 static int did_setup
;
898 if (0 <= transfer_unpack_limit
)
899 unpack_limit
= transfer_unpack_limit
;
900 else if (0 <= fetch_unpack_limit
)
901 unpack_limit
= fetch_unpack_limit
;
905 static int remove_duplicates_in_refs(struct ref
**ref
, int nr
)
907 struct string_list names
= STRING_LIST_INIT_NODUP
;
910 for (src
= dst
= 0; src
< nr
; src
++) {
911 struct string_list_item
*item
;
912 item
= string_list_insert(&names
, ref
[src
]->name
);
914 continue; /* already have it */
915 item
->util
= ref
[src
];
920 for (src
= dst
; src
< nr
; src
++)
922 string_list_clear(&names
, 0);
926 static void update_shallow(struct fetch_pack_args
*args
,
927 struct ref
**sought
, int nr_sought
,
928 struct shallow_info
*si
)
930 struct sha1_array ref
= SHA1_ARRAY_INIT
;
934 if (args
->depth
> 0 && alternate_shallow_file
) {
935 if (*alternate_shallow_file
== '\0') { /* --unshallow */
936 unlink_or_warn(git_path("shallow"));
937 rollback_lock_file(&shallow_lock
);
939 commit_lock_file(&shallow_lock
);
943 if (!si
->shallow
|| !si
->shallow
->nr
)
948 * remote is shallow, but this is a clone, there are
949 * no objects in repo to worry about. Accept any
950 * shallow points that exist in the pack (iow in repo
951 * after get_pack() and reprepare_packed_git())
953 struct sha1_array extra
= SHA1_ARRAY_INIT
;
954 unsigned char (*sha1
)[20] = si
->shallow
->sha1
;
955 for (i
= 0; i
< si
->shallow
->nr
; i
++)
956 if (has_sha1_file(sha1
[i
]))
957 sha1_array_append(&extra
, sha1
[i
]);
959 setup_alternate_shallow(&shallow_lock
,
960 &alternate_shallow_file
,
962 commit_lock_file(&shallow_lock
);
964 sha1_array_clear(&extra
);
968 if (!si
->nr_ours
&& !si
->nr_theirs
)
971 remove_nonexistent_theirs_shallow(si
);
972 if (!si
->nr_ours
&& !si
->nr_theirs
)
974 for (i
= 0; i
< nr_sought
; i
++)
975 sha1_array_append(&ref
, sought
[i
]->old_sha1
);
978 if (args
->update_shallow
) {
980 * remote is also shallow, .git/shallow may be updated
981 * so all refs can be accepted. Make sure we only add
982 * shallow roots that are actually reachable from new
985 struct sha1_array extra
= SHA1_ARRAY_INIT
;
986 unsigned char (*sha1
)[20] = si
->shallow
->sha1
;
987 assign_shallow_commits_to_refs(si
, NULL
, NULL
);
988 if (!si
->nr_ours
&& !si
->nr_theirs
) {
989 sha1_array_clear(&ref
);
992 for (i
= 0; i
< si
->nr_ours
; i
++)
993 sha1_array_append(&extra
, sha1
[si
->ours
[i
]]);
994 for (i
= 0; i
< si
->nr_theirs
; i
++)
995 sha1_array_append(&extra
, sha1
[si
->theirs
[i
]]);
996 setup_alternate_shallow(&shallow_lock
,
997 &alternate_shallow_file
,
999 commit_lock_file(&shallow_lock
);
1000 sha1_array_clear(&extra
);
1001 sha1_array_clear(&ref
);
1006 * remote is also shallow, check what ref is safe to update
1007 * without updating .git/shallow
1009 status
= xcalloc(nr_sought
, sizeof(*status
));
1010 assign_shallow_commits_to_refs(si
, NULL
, status
);
1011 if (si
->nr_ours
|| si
->nr_theirs
) {
1012 for (i
= 0; i
< nr_sought
; i
++)
1014 sought
[i
]->status
= REF_STATUS_REJECT_SHALLOW
;
1017 sha1_array_clear(&ref
);
1020 struct ref
*fetch_pack(struct fetch_pack_args
*args
,
1021 int fd
[], struct child_process
*conn
,
1022 const struct ref
*ref
,
1024 struct ref
**sought
, int nr_sought
,
1025 struct sha1_array
*shallow
,
1026 char **pack_lockfile
)
1028 struct ref
*ref_cpy
;
1029 struct shallow_info si
;
1033 nr_sought
= remove_duplicates_in_refs(sought
, nr_sought
);
1036 packet_flush(fd
[1]);
1037 die("no matching remote head");
1039 prepare_shallow_info(&si
, shallow
);
1040 ref_cpy
= do_fetch_pack(args
, fd
, ref
, sought
, nr_sought
,
1041 &si
, pack_lockfile
);
1042 reprepare_packed_git();
1043 update_shallow(args
, sought
, nr_sought
, &si
);
1044 clear_shallow_info(&si
);