9 #include "fetch-pack.h"
11 #include "run-command.h"
12 #include "transport.h"
15 static int transfer_unpack_limit
= -1;
16 static int fetch_unpack_limit
= -1;
17 static int unpack_limit
= 100;
18 static int prefer_ofs_delta
= 1;
20 static int fetch_fsck_objects
= -1;
21 static int transfer_fsck_objects
= -1;
22 static struct fetch_pack_args args
= {
23 /* .uploadpack = */ "git-upload-pack",
26 static const char fetch_pack_usage
[] =
27 "git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
28 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
29 "[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
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 commit_list
*rev_list
;
46 static int non_common_revs
, multi_ack
, use_sideband
;
48 static void rev_list_push(struct commit
*commit
, int mark
)
50 if (!(commit
->object
.flags
& mark
)) {
51 commit
->object
.flags
|= mark
;
53 if (!(commit
->object
.parsed
))
54 if (parse_commit(commit
))
57 commit_list_insert_by_date(commit
, &rev_list
);
59 if (!(commit
->object
.flags
& COMMON
))
64 static int rev_list_insert_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
66 struct object
*o
= deref_tag(parse_object(sha1
), refname
, 0);
68 if (o
&& o
->type
== OBJ_COMMIT
)
69 rev_list_push((struct commit
*)o
, SEEN
);
74 static int clear_marks(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
76 struct object
*o
= deref_tag(parse_object(sha1
), 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
== NULL
|| non_common_revs
== 0)
133 commit
= rev_list
->item
;
134 if (!commit
->object
.parsed
)
135 parse_commit(commit
);
136 parents
= commit
->parents
;
138 commit
->object
.flags
|= POPPED
;
139 if (!(commit
->object
.flags
& COMMON
))
142 if (commit
->object
.flags
& COMMON
) {
143 /* do not send "have", and ignore ancestors */
145 mark
= COMMON
| SEEN
;
146 } else if (commit
->object
.flags
& COMMON_REF
)
147 /* send "have", and ignore ancestors */
148 mark
= COMMON
| SEEN
;
150 /* send "have", also for its ancestors */
154 if (!(parents
->item
->object
.flags
& SEEN
))
155 rev_list_push(parents
->item
, mark
);
157 mark_common(parents
->item
, 1, 0);
158 parents
= parents
->next
;
161 rev_list
= rev_list
->next
;
164 return commit
->object
.sha1
;
175 static void consume_shallow_list(int fd
)
177 if (args
.stateless_rpc
&& args
.depth
> 0) {
178 /* If we sent a depth we will get back "duplicate"
179 * shallow and unshallow commands every time there
180 * is a block of have lines exchanged.
183 while (packet_read_line(fd
, line
, sizeof(line
))) {
184 if (!prefixcmp(line
, "shallow "))
186 if (!prefixcmp(line
, "unshallow "))
188 die("git fetch-pack: expected shallow list");
193 struct write_shallow_data
{
195 int use_pack_protocol
;
199 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
201 struct write_shallow_data
*data
= cb_data
;
202 const char *hex
= sha1_to_hex(graft
->sha1
);
204 if (data
->use_pack_protocol
)
205 packet_buf_write(data
->out
, "shallow %s", hex
);
207 strbuf_addstr(data
->out
, hex
);
208 strbuf_addch(data
->out
, '\n');
213 static int write_shallow_commits(struct strbuf
*out
, int use_pack_protocol
)
215 struct write_shallow_data data
;
217 data
.use_pack_protocol
= use_pack_protocol
;
219 for_each_commit_graft(write_one_shallow
, &data
);
223 static enum ack_type
get_ack(int fd
, unsigned char *result_sha1
)
225 static char line
[1000];
226 int len
= packet_read_line(fd
, line
, sizeof(line
));
229 die("git fetch-pack: expected ACK/NAK, got EOF");
230 if (line
[len
-1] == '\n')
232 if (!strcmp(line
, "NAK"))
234 if (!prefixcmp(line
, "ACK ")) {
235 if (!get_sha1_hex(line
+4, result_sha1
)) {
236 if (strstr(line
+45, "continue"))
238 if (strstr(line
+45, "common"))
240 if (strstr(line
+45, "ready"))
245 die("git fetch_pack: expected ACK/NAK, got '%s'", line
);
248 static void send_request(int fd
, struct strbuf
*buf
)
250 if (args
.stateless_rpc
) {
251 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
254 safe_write(fd
, buf
->buf
, buf
->len
);
257 static void insert_one_alternate_ref(const struct ref
*ref
, void *unused
)
259 rev_list_insert_ref(NULL
, ref
->old_sha1
, 0, NULL
);
262 #define INITIAL_FLUSH 16
263 #define PIPESAFE_FLUSH 32
264 #define LARGE_FLUSH 1024
266 static int next_flush(int count
)
268 int flush_limit
= args
.stateless_rpc
? LARGE_FLUSH
: PIPESAFE_FLUSH
;
270 if (count
< flush_limit
)
273 count
+= flush_limit
;
277 static int find_common(int fd
[2], unsigned char *result_sha1
,
281 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
282 const unsigned char *sha1
;
283 unsigned in_vain
= 0;
284 int got_continue
= 0;
286 struct strbuf req_buf
= STRBUF_INIT
;
287 size_t state_len
= 0;
289 if (args
.stateless_rpc
&& multi_ack
== 1)
290 die("--stateless-rpc requires multi_ack_detailed");
292 for_each_ref(clear_marks
, NULL
);
295 for_each_ref(rev_list_insert_ref
, NULL
);
296 for_each_alternate_ref(insert_one_alternate_ref
, NULL
);
299 for ( ; refs
; refs
= refs
->next
) {
300 unsigned char *remote
= refs
->old_sha1
;
301 const char *remote_hex
;
305 * If that object is complete (i.e. it is an ancestor of a
306 * local ref), we tell them we have it but do not have to
307 * tell them about its ancestors, which they already know
310 * We use lookup_object here because we are only
311 * interested in the case we *know* the object is
312 * reachable and we have already scanned it.
314 if (((o
= lookup_object(remote
)) != NULL
) &&
315 (o
->flags
& COMPLETE
)) {
319 remote_hex
= sha1_to_hex(remote
);
321 struct strbuf c
= STRBUF_INIT
;
322 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
323 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
324 if (no_done
) strbuf_addstr(&c
, " no-done");
325 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
326 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
327 if (args
.use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
328 if (args
.no_progress
) strbuf_addstr(&c
, " no-progress");
329 if (args
.include_tag
) strbuf_addstr(&c
, " include-tag");
330 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
331 strbuf_addf(&c
, " agent=%s", git_user_agent_sanitized());
332 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
335 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
340 strbuf_release(&req_buf
);
345 if (is_repository_shallow())
346 write_shallow_commits(&req_buf
, 1);
348 packet_buf_write(&req_buf
, "deepen %d", args
.depth
);
349 packet_buf_flush(&req_buf
);
350 state_len
= req_buf
.len
;
352 if (args
.depth
> 0) {
354 unsigned char sha1
[20];
356 send_request(fd
[1], &req_buf
);
357 while (packet_read_line(fd
[0], line
, sizeof(line
))) {
358 if (!prefixcmp(line
, "shallow ")) {
359 if (get_sha1_hex(line
+ 8, sha1
))
360 die("invalid shallow line: %s", line
);
361 register_shallow(sha1
);
364 if (!prefixcmp(line
, "unshallow ")) {
365 if (get_sha1_hex(line
+ 10, sha1
))
366 die("invalid unshallow line: %s", line
);
367 if (!lookup_object(sha1
))
368 die("object not found: %s", line
);
369 /* make sure that it is parsed as shallow */
370 if (!parse_object(sha1
))
371 die("error in object: %s", line
);
372 if (unregister_shallow(sha1
))
373 die("no shallow found: %s", line
);
376 die("expected shallow/unshallow, got %s", line
);
378 } else if (!args
.stateless_rpc
)
379 send_request(fd
[1], &req_buf
);
381 if (!args
.stateless_rpc
) {
382 /* If we aren't using the stateless-rpc interface
383 * we don't need to retain the headers.
385 strbuf_setlen(&req_buf
, 0);
391 while ((sha1
= get_rev())) {
392 packet_buf_write(&req_buf
, "have %s\n", sha1_to_hex(sha1
));
394 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
396 if (flush_at
<= ++count
) {
399 packet_buf_flush(&req_buf
);
400 send_request(fd
[1], &req_buf
);
401 strbuf_setlen(&req_buf
, state_len
);
403 flush_at
= next_flush(count
);
406 * We keep one window "ahead" of the other side, and
407 * will wait for an ACK only on the next one
409 if (!args
.stateless_rpc
&& count
== INITIAL_FLUSH
)
412 consume_shallow_list(fd
[0]);
414 ack
= get_ack(fd
[0], result_sha1
);
415 if (args
.verbose
&& ack
)
416 fprintf(stderr
, "got ack %d %s\n", ack
,
417 sha1_to_hex(result_sha1
));
427 struct commit
*commit
=
428 lookup_commit(result_sha1
);
430 die("invalid commit %s", sha1_to_hex(result_sha1
));
431 if (args
.stateless_rpc
433 && !(commit
->object
.flags
& COMMON
)) {
434 /* We need to replay the have for this object
435 * on the next RPC request so the peer knows
436 * it is in common with us.
438 const char *hex
= sha1_to_hex(result_sha1
);
439 packet_buf_write(&req_buf
, "have %s\n", hex
);
440 state_len
= req_buf
.len
;
442 mark_common(commit
, 0, 1);
446 if (ack
== ACK_ready
) {
455 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
457 fprintf(stderr
, "giving up\n");
463 if (!got_ready
|| !no_done
) {
464 packet_buf_write(&req_buf
, "done\n");
465 send_request(fd
[1], &req_buf
);
468 fprintf(stderr
, "done\n");
473 strbuf_release(&req_buf
);
475 consume_shallow_list(fd
[0]);
476 while (flushes
|| multi_ack
) {
477 int ack
= get_ack(fd
[0], result_sha1
);
480 fprintf(stderr
, "got ack (%d) %s\n", ack
,
481 sha1_to_hex(result_sha1
));
489 /* it is no error to fetch into a completely empty repo */
490 return count
? retval
: 0;
493 static struct commit_list
*complete
;
495 static int mark_complete(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
497 struct object
*o
= parse_object(sha1
);
499 while (o
&& o
->type
== OBJ_TAG
) {
500 struct tag
*t
= (struct tag
*) o
;
502 break; /* broken repository */
503 o
->flags
|= COMPLETE
;
504 o
= parse_object(t
->tagged
->sha1
);
506 if (o
&& o
->type
== OBJ_COMMIT
) {
507 struct commit
*commit
= (struct commit
*)o
;
508 if (!(commit
->object
.flags
& COMPLETE
)) {
509 commit
->object
.flags
|= COMPLETE
;
510 commit_list_insert_by_date(commit
, &complete
);
516 static void mark_recent_complete_commits(unsigned long cutoff
)
518 while (complete
&& cutoff
<= complete
->item
->date
) {
520 fprintf(stderr
, "Marking %s as complete\n",
521 sha1_to_hex(complete
->item
->object
.sha1
));
522 pop_most_recent_commit(&complete
, COMPLETE
);
526 static void filter_refs(struct ref
**refs
, int nr_match
, char **match
)
528 struct ref
**return_refs
;
529 struct ref
*newlist
= NULL
;
530 struct ref
**newtail
= &newlist
;
531 struct ref
*ref
, *next
;
532 struct ref
*fastarray
[32];
535 if (nr_match
&& !args
.fetch_all
) {
536 if (ARRAY_SIZE(fastarray
) < nr_match
)
537 return_refs
= xcalloc(nr_match
, sizeof(struct ref
*));
539 return_refs
= fastarray
;
540 memset(return_refs
, 0, sizeof(struct ref
*) * nr_match
);
547 for (ref
= *refs
; ref
; ref
= next
) {
549 if (!memcmp(ref
->name
, "refs/", 5) &&
550 check_refname_format(ref
->name
+ 5, 0))
552 else if (args
.fetch_all
&&
553 (!args
.depth
|| prefixcmp(ref
->name
, "refs/tags/") )) {
556 newtail
= &ref
->next
;
561 while (match_pos
< nr_match
) {
562 cmp
= strcmp(ref
->name
, match
[match_pos
]);
563 if (cmp
< 0) /* definitely do not have it */
565 else if (cmp
== 0) { /* definitely have it */
566 match
[match_pos
][0] = '\0';
567 return_refs
[match_pos
] = ref
;
570 else /* might have it; keep looking */
574 continue; /* we will link it later */
579 if (!args
.fetch_all
) {
581 for (i
= 0; i
< nr_match
; i
++) {
582 ref
= return_refs
[i
];
586 newtail
= &ref
->next
;
589 if (return_refs
!= fastarray
)
595 static void mark_alternate_complete(const struct ref
*ref
, void *unused
)
597 mark_complete(NULL
, ref
->old_sha1
, 0, NULL
);
600 static int everything_local(struct ref
**refs
, int nr_match
, char **match
)
604 unsigned long cutoff
= 0;
606 save_commit_buffer
= 0;
608 for (ref
= *refs
; ref
; ref
= ref
->next
) {
611 o
= parse_object(ref
->old_sha1
);
615 /* We already have it -- which may mean that we were
616 * in sync with the other side at some time after
617 * that (it is OK if we guess wrong here).
619 if (o
->type
== OBJ_COMMIT
) {
620 struct commit
*commit
= (struct commit
*)o
;
621 if (!cutoff
|| cutoff
< commit
->date
)
622 cutoff
= commit
->date
;
627 for_each_ref(mark_complete
, NULL
);
628 for_each_alternate_ref(mark_alternate_complete
, NULL
);
630 mark_recent_complete_commits(cutoff
);
634 * Mark all complete remote refs as common refs.
635 * Don't mark them common yet; the server has to be told so first.
637 for (ref
= *refs
; ref
; ref
= ref
->next
) {
638 struct object
*o
= deref_tag(lookup_object(ref
->old_sha1
),
641 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
644 if (!(o
->flags
& SEEN
)) {
645 rev_list_push((struct commit
*)o
, COMMON_REF
| SEEN
);
647 mark_common((struct commit
*)o
, 1, 1);
651 filter_refs(refs
, nr_match
, match
);
653 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
654 const unsigned char *remote
= ref
->old_sha1
;
655 unsigned char local
[20];
658 o
= lookup_object(remote
);
659 if (!o
|| !(o
->flags
& COMPLETE
)) {
664 "want %s (%s)\n", sha1_to_hex(remote
),
669 hashcpy(ref
->new_sha1
, local
);
673 "already have %s (%s)\n", sha1_to_hex(remote
),
679 static int sideband_demux(int in
, int out
, void *data
)
683 int ret
= recv_sideband("fetch-pack", xd
[0], out
);
688 static int get_pack(int xd
[2], char **pack_lockfile
)
691 const char *argv
[20];
695 int do_keep
= args
.keep_pack
;
696 struct child_process cmd
;
698 memset(&demux
, 0, sizeof(demux
));
700 /* xd[] is talking with upload-pack; subprocess reads from
701 * xd[0], spits out band#2 to stderr, and feeds us band#1
702 * through demux->out.
704 demux
.proc
= sideband_demux
;
707 if (start_async(&demux
))
708 die("fetch-pack: unable to fork off sideband"
714 memset(&cmd
, 0, sizeof(cmd
));
718 if (!args
.keep_pack
&& unpack_limit
) {
719 struct pack_header header
;
721 if (read_pack_header(demux
.out
, &header
))
722 die("protocol error: bad pack header");
723 snprintf(hdr_arg
, sizeof(hdr_arg
),
724 "--pack_header=%"PRIu32
",%"PRIu32
,
725 ntohl(header
.hdr_version
), ntohl(header
.hdr_entries
));
726 if (ntohl(header
.hdr_entries
) < unpack_limit
)
735 *av
++ = "index-pack";
737 if (!args
.quiet
&& !args
.no_progress
)
739 if (args
.use_thin_pack
)
740 *av
++ = "--fix-thin";
741 if (args
.lock_pack
|| unpack_limit
) {
742 int s
= sprintf(keep_arg
,
743 "--keep=fetch-pack %"PRIuMAX
" on ", (uintmax_t) getpid());
744 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
745 strcpy(keep_arg
+ s
, "localhost");
750 *av
++ = "unpack-objects";
751 if (args
.quiet
|| args
.no_progress
)
756 if (fetch_fsck_objects
>= 0
758 : transfer_fsck_objects
>= 0
759 ? transfer_fsck_objects
766 if (start_command(&cmd
))
767 die("fetch-pack: unable to fork off %s", argv
[0]);
768 if (do_keep
&& pack_lockfile
) {
769 *pack_lockfile
= index_pack_lockfile(cmd
.out
);
773 if (finish_command(&cmd
))
774 die("%s failed", argv
[0]);
775 if (use_sideband
&& finish_async(&demux
))
776 die("error in sideband demultiplexer");
780 static struct ref
*do_fetch_pack(int fd
[2],
781 const struct ref
*orig_ref
,
784 char **pack_lockfile
)
786 struct ref
*ref
= copy_ref_list(orig_ref
);
787 unsigned char sha1
[20];
789 sort_ref_list(&ref
, ref_compare_name
);
791 if (is_repository_shallow() && !server_supports("shallow"))
792 die("Server does not support shallow clients");
793 if (server_supports("multi_ack_detailed")) {
795 fprintf(stderr
, "Server supports multi_ack_detailed\n");
797 if (server_supports("no-done")) {
799 fprintf(stderr
, "Server supports no-done\n");
800 if (args
.stateless_rpc
)
804 else if (server_supports("multi_ack")) {
806 fprintf(stderr
, "Server supports multi_ack\n");
809 if (server_supports("side-band-64k")) {
811 fprintf(stderr
, "Server supports side-band-64k\n");
814 else if (server_supports("side-band")) {
816 fprintf(stderr
, "Server supports side-band\n");
819 if (server_supports("ofs-delta")) {
821 fprintf(stderr
, "Server supports ofs-delta\n");
823 prefer_ofs_delta
= 0;
824 if (everything_local(&ref
, nr_match
, match
)) {
828 if (find_common(fd
, sha1
, ref
) < 0)
830 /* When cloning, it is not unusual to have
833 warning("no common commits");
835 if (args
.stateless_rpc
)
837 if (get_pack(fd
, pack_lockfile
))
838 die("git fetch-pack: fetch failed.");
844 static int remove_duplicates(int nr_heads
, char **heads
)
851 for (src
= dst
= 1; src
< nr_heads
; src
++)
852 if (strcmp(heads
[src
], heads
[dst
-1]))
853 heads
[dst
++] = heads
[src
];
857 static int fetch_pack_config(const char *var
, const char *value
, void *cb
)
859 if (strcmp(var
, "fetch.unpacklimit") == 0) {
860 fetch_unpack_limit
= git_config_int(var
, value
);
864 if (strcmp(var
, "transfer.unpacklimit") == 0) {
865 transfer_unpack_limit
= git_config_int(var
, value
);
869 if (strcmp(var
, "repack.usedeltabaseoffset") == 0) {
870 prefer_ofs_delta
= git_config_bool(var
, value
);
874 if (!strcmp(var
, "fetch.fsckobjects")) {
875 fetch_fsck_objects
= git_config_bool(var
, value
);
879 if (!strcmp(var
, "transfer.fsckobjects")) {
880 transfer_fsck_objects
= git_config_bool(var
, value
);
884 return git_default_config(var
, value
, cb
);
887 static struct lock_file lock
;
889 static void fetch_pack_setup(void)
891 static int did_setup
;
894 git_config(fetch_pack_config
, NULL
);
895 if (0 <= transfer_unpack_limit
)
896 unpack_limit
= transfer_unpack_limit
;
897 else if (0 <= fetch_unpack_limit
)
898 unpack_limit
= fetch_unpack_limit
;
902 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix
)
905 struct ref
*ref
= NULL
;
906 const char *dest
= NULL
;
907 int alloc_heads
= 0, nr_heads
= 0;
910 char *pack_lockfile
= NULL
;
911 char **pack_lockfile_ptr
= NULL
;
912 struct child_process
*conn
;
914 packet_trace_identity("fetch-pack");
916 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++) {
917 const char *arg
= argv
[i
];
919 if (!prefixcmp(arg
, "--upload-pack=")) {
920 args
.uploadpack
= arg
+ 14;
923 if (!prefixcmp(arg
, "--exec=")) {
924 args
.uploadpack
= arg
+ 7;
927 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
931 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
932 args
.lock_pack
= args
.keep_pack
;
936 if (!strcmp("--thin", arg
)) {
937 args
.use_thin_pack
= 1;
940 if (!strcmp("--include-tag", arg
)) {
941 args
.include_tag
= 1;
944 if (!strcmp("--all", arg
)) {
948 if (!strcmp("--stdin", arg
)) {
952 if (!strcmp("-v", arg
)) {
956 if (!prefixcmp(arg
, "--depth=")) {
957 args
.depth
= strtol(arg
+ 8, NULL
, 0);
960 if (!strcmp("--no-progress", arg
)) {
961 args
.no_progress
= 1;
964 if (!strcmp("--stateless-rpc", arg
)) {
965 args
.stateless_rpc
= 1;
968 if (!strcmp("--lock-pack", arg
)) {
970 pack_lockfile_ptr
= &pack_lockfile
;
973 usage(fetch_pack_usage
);
979 usage(fetch_pack_usage
);
982 * Copy refs from cmdline to growable list, then append any
983 * refs from the standard input:
985 ALLOC_GROW(heads
, argc
- i
, alloc_heads
);
986 for (; i
< argc
; i
++)
987 heads
[nr_heads
++] = xstrdup(argv
[i
]);
988 if (args
.stdin_refs
) {
989 if (args
.stateless_rpc
) {
990 /* in stateless RPC mode we use pkt-line to read
991 * from stdin, until we get a flush packet
993 static char line
[1000];
995 int n
= packet_read_line(0, line
, sizeof(line
));
998 if (line
[n
-1] == '\n')
1000 ALLOC_GROW(heads
, nr_heads
+ 1, alloc_heads
);
1001 heads
[nr_heads
++] = xmemdupz(line
, n
);
1005 /* read from stdin one ref per line, until EOF */
1006 struct strbuf line
= STRBUF_INIT
;
1007 while (strbuf_getline(&line
, stdin
, '\n') != EOF
) {
1008 ALLOC_GROW(heads
, nr_heads
+ 1, alloc_heads
);
1009 heads
[nr_heads
++] = strbuf_detach(&line
, NULL
);
1011 strbuf_release(&line
);
1015 if (args
.stateless_rpc
) {
1020 conn
= git_connect(fd
, dest
, args
.uploadpack
,
1021 args
.verbose
? CONNECT_VERBOSE
: 0);
1024 get_remote_heads(fd
[0], &ref
, 0, NULL
);
1026 ref
= fetch_pack(&args
, fd
, conn
, ref
, dest
,
1027 nr_heads
, heads
, pack_lockfile_ptr
);
1028 if (pack_lockfile
) {
1029 printf("lock %s\n", pack_lockfile
);
1034 if (finish_connect(conn
))
1038 if (!ret
&& nr_heads
) {
1039 /* If the heads to pull were given, we should have
1040 * consumed all of them by matching the remote.
1041 * Otherwise, 'git fetch remote no-such-ref' would
1042 * silently succeed without issuing an error.
1044 for (i
= 0; i
< nr_heads
; i
++)
1045 if (heads
[i
] && heads
[i
][0]) {
1046 error("no such remote ref %s", heads
[i
]);
1052 sha1_to_hex(ref
->old_sha1
), ref
->name
);
1059 static int compare_heads(const void *a
, const void *b
)
1061 return strcmp(*(const char **)a
, *(const char **)b
);
1064 struct ref
*fetch_pack(struct fetch_pack_args
*my_args
,
1065 int fd
[], struct child_process
*conn
,
1066 const struct ref
*ref
,
1070 char **pack_lockfile
)
1073 struct ref
*ref_cpy
;
1076 if (&args
!= my_args
)
1077 memcpy(&args
, my_args
, sizeof(args
));
1078 if (args
.depth
> 0) {
1079 if (stat(git_path("shallow"), &st
))
1083 if (heads
&& nr_heads
) {
1084 qsort(heads
, nr_heads
, sizeof(*heads
), compare_heads
);
1085 nr_heads
= remove_duplicates(nr_heads
, heads
);
1089 packet_flush(fd
[1]);
1090 die("no matching remote head");
1092 ref_cpy
= do_fetch_pack(fd
, ref
, nr_heads
, heads
, pack_lockfile
);
1094 if (args
.depth
> 0) {
1095 struct cache_time mtime
;
1096 struct strbuf sb
= STRBUF_INIT
;
1097 char *shallow
= git_path("shallow");
1100 mtime
.sec
= st
.st_mtime
;
1101 mtime
.nsec
= ST_MTIME_NSEC(st
);
1102 if (stat(shallow
, &st
)) {
1104 die("shallow file was removed during fetch");
1105 } else if (st
.st_mtime
!= mtime
.sec
1107 || ST_MTIME_NSEC(st
) != mtime
.nsec
1110 die("shallow file was changed during fetch");
1112 fd
= hold_lock_file_for_update(&lock
, shallow
,
1114 if (!write_shallow_commits(&sb
, 0)
1115 || write_in_full(fd
, sb
.buf
, sb
.len
) != sb
.len
) {
1116 unlink_or_warn(shallow
);
1117 rollback_lock_file(&lock
);
1119 commit_lock_file(&lock
);
1121 strbuf_release(&sb
);
1124 reprepare_packed_git();