9 #include "fetch-pack.h"
11 #include "run-command.h"
12 #include "transport.h"
14 static int transfer_unpack_limit
= -1;
15 static int fetch_unpack_limit
= -1;
16 static int unpack_limit
= 100;
17 static int prefer_ofs_delta
= 1;
19 static int fetch_fsck_objects
= -1;
20 static int transfer_fsck_objects
= -1;
21 static struct fetch_pack_args args
= {
22 /* .uploadpack = */ "git-upload-pack",
25 static const char fetch_pack_usage
[] =
26 "git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
27 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
28 "[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
30 #define COMPLETE (1U << 0)
31 #define COMMON (1U << 1)
32 #define COMMON_REF (1U << 2)
33 #define SEEN (1U << 3)
34 #define POPPED (1U << 4)
39 * After sending this many "have"s if we do not get any new ACK , we
40 * give up traversing our history.
42 #define MAX_IN_VAIN 256
44 static struct commit_list
*rev_list
;
45 static int non_common_revs
, multi_ack
, use_sideband
;
47 static void rev_list_push(struct commit
*commit
, int mark
)
49 if (!(commit
->object
.flags
& mark
)) {
50 commit
->object
.flags
|= mark
;
52 if (!(commit
->object
.parsed
))
53 if (parse_commit(commit
))
56 commit_list_insert_by_date(commit
, &rev_list
);
58 if (!(commit
->object
.flags
& COMMON
))
63 static int rev_list_insert_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
65 struct object
*o
= deref_tag(parse_object(sha1
), path
, 0);
67 if (o
&& o
->type
== OBJ_COMMIT
)
68 rev_list_push((struct commit
*)o
, SEEN
);
73 static int clear_marks(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
75 struct object
*o
= deref_tag(parse_object(sha1
), path
, 0);
77 if (o
&& o
->type
== OBJ_COMMIT
)
78 clear_commit_marks((struct commit
*)o
,
79 COMMON
| COMMON_REF
| SEEN
| POPPED
);
84 This function marks a rev and its ancestors as common.
85 In some cases, it is desirable to mark only the ancestors (for example
86 when only the server does not yet know that they are common).
89 static void mark_common(struct commit
*commit
,
90 int ancestors_only
, int dont_parse
)
92 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
93 struct object
*o
= (struct object
*)commit
;
98 if (!(o
->flags
& SEEN
))
99 rev_list_push(commit
, SEEN
);
101 struct commit_list
*parents
;
103 if (!ancestors_only
&& !(o
->flags
& POPPED
))
105 if (!o
->parsed
&& !dont_parse
)
106 if (parse_commit(commit
))
109 for (parents
= commit
->parents
;
111 parents
= parents
->next
)
112 mark_common(parents
->item
, 0, dont_parse
);
118 Get the next rev to send, ignoring the common.
121 static const unsigned char *get_rev(void)
123 struct commit
*commit
= NULL
;
125 while (commit
== NULL
) {
127 struct commit_list
*parents
;
129 if (rev_list
== NULL
|| non_common_revs
== 0)
132 commit
= rev_list
->item
;
133 if (!commit
->object
.parsed
)
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
;
160 rev_list
= rev_list
->next
;
163 return commit
->object
.sha1
;
174 static void consume_shallow_list(int fd
)
176 if (args
.stateless_rpc
&& args
.depth
> 0) {
177 /* If we sent a depth we will get back "duplicate"
178 * shallow and unshallow commands every time there
179 * is a block of have lines exchanged.
182 while (packet_read_line(fd
, line
, sizeof(line
))) {
183 if (!prefixcmp(line
, "shallow "))
185 if (!prefixcmp(line
, "unshallow "))
187 die("git fetch-pack: expected shallow list");
192 struct write_shallow_data
{
194 int use_pack_protocol
;
198 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
200 struct write_shallow_data
*data
= cb_data
;
201 const char *hex
= sha1_to_hex(graft
->sha1
);
203 if (data
->use_pack_protocol
)
204 packet_buf_write(data
->out
, "shallow %s", hex
);
206 strbuf_addstr(data
->out
, hex
);
207 strbuf_addch(data
->out
, '\n');
212 static int write_shallow_commits(struct strbuf
*out
, int use_pack_protocol
)
214 struct write_shallow_data data
;
216 data
.use_pack_protocol
= use_pack_protocol
;
218 for_each_commit_graft(write_one_shallow
, &data
);
222 static enum ack_type
get_ack(int fd
, unsigned char *result_sha1
)
224 static char line
[1000];
225 int len
= packet_read_line(fd
, line
, sizeof(line
));
228 die("git fetch-pack: expected ACK/NAK, got EOF");
229 if (line
[len
-1] == '\n')
231 if (!strcmp(line
, "NAK"))
233 if (!prefixcmp(line
, "ACK ")) {
234 if (!get_sha1_hex(line
+4, result_sha1
)) {
235 if (strstr(line
+45, "continue"))
237 if (strstr(line
+45, "common"))
239 if (strstr(line
+45, "ready"))
244 die("git fetch_pack: expected ACK/NAK, got '%s'", line
);
247 static void send_request(int fd
, struct strbuf
*buf
)
249 if (args
.stateless_rpc
) {
250 send_sideband(fd
, -1, buf
->buf
, buf
->len
, LARGE_PACKET_MAX
);
253 safe_write(fd
, buf
->buf
, buf
->len
);
256 static void insert_one_alternate_ref(const struct ref
*ref
, void *unused
)
258 rev_list_insert_ref(NULL
, ref
->old_sha1
, 0, NULL
);
261 static void insert_alternate_refs(void)
263 for_each_alternate_ref(insert_one_alternate_ref
, NULL
);
266 #define INITIAL_FLUSH 16
267 #define PIPESAFE_FLUSH 32
268 #define LARGE_FLUSH 1024
270 static int next_flush(int count
)
272 int flush_limit
= args
.stateless_rpc
? LARGE_FLUSH
: PIPESAFE_FLUSH
;
274 if (count
< flush_limit
)
277 count
+= flush_limit
;
281 static int find_common(int fd
[2], unsigned char *result_sha1
,
285 int count
= 0, flushes
= 0, flush_at
= INITIAL_FLUSH
, retval
;
286 const unsigned char *sha1
;
287 unsigned in_vain
= 0;
288 int got_continue
= 0;
290 struct strbuf req_buf
= STRBUF_INIT
;
291 size_t state_len
= 0;
293 if (args
.stateless_rpc
&& multi_ack
== 1)
294 die("--stateless-rpc requires multi_ack_detailed");
296 for_each_ref(clear_marks
, NULL
);
299 for_each_ref(rev_list_insert_ref
, NULL
);
300 insert_alternate_refs();
303 for ( ; refs
; refs
= refs
->next
) {
304 unsigned char *remote
= refs
->old_sha1
;
305 const char *remote_hex
;
309 * If that object is complete (i.e. it is an ancestor of a
310 * local ref), we tell them we have it but do not have to
311 * tell them about its ancestors, which they already know
314 * We use lookup_object here because we are only
315 * interested in the case we *know* the object is
316 * reachable and we have already scanned it.
318 if (((o
= lookup_object(remote
)) != NULL
) &&
319 (o
->flags
& COMPLETE
)) {
323 remote_hex
= sha1_to_hex(remote
);
325 struct strbuf c
= STRBUF_INIT
;
326 if (multi_ack
== 2) strbuf_addstr(&c
, " multi_ack_detailed");
327 if (multi_ack
== 1) strbuf_addstr(&c
, " multi_ack");
328 if (no_done
) strbuf_addstr(&c
, " no-done");
329 if (use_sideband
== 2) strbuf_addstr(&c
, " side-band-64k");
330 if (use_sideband
== 1) strbuf_addstr(&c
, " side-band");
331 if (args
.use_thin_pack
) strbuf_addstr(&c
, " thin-pack");
332 if (args
.no_progress
) strbuf_addstr(&c
, " no-progress");
333 if (args
.include_tag
) strbuf_addstr(&c
, " include-tag");
334 if (prefer_ofs_delta
) strbuf_addstr(&c
, " ofs-delta");
335 packet_buf_write(&req_buf
, "want %s%s\n", remote_hex
, c
.buf
);
338 packet_buf_write(&req_buf
, "want %s\n", remote_hex
);
343 strbuf_release(&req_buf
);
348 if (is_repository_shallow())
349 write_shallow_commits(&req_buf
, 1);
351 packet_buf_write(&req_buf
, "deepen %d", args
.depth
);
352 packet_buf_flush(&req_buf
);
353 state_len
= req_buf
.len
;
355 if (args
.depth
> 0) {
357 unsigned char sha1
[20];
359 send_request(fd
[1], &req_buf
);
360 while (packet_read_line(fd
[0], line
, sizeof(line
))) {
361 if (!prefixcmp(line
, "shallow ")) {
362 if (get_sha1_hex(line
+ 8, sha1
))
363 die("invalid shallow line: %s", line
);
364 register_shallow(sha1
);
367 if (!prefixcmp(line
, "unshallow ")) {
368 if (get_sha1_hex(line
+ 10, sha1
))
369 die("invalid unshallow line: %s", line
);
370 if (!lookup_object(sha1
))
371 die("object not found: %s", line
);
372 /* make sure that it is parsed as shallow */
373 if (!parse_object(sha1
))
374 die("error in object: %s", line
);
375 if (unregister_shallow(sha1
))
376 die("no shallow found: %s", line
);
379 die("expected shallow/unshallow, got %s", line
);
381 } else if (!args
.stateless_rpc
)
382 send_request(fd
[1], &req_buf
);
384 if (!args
.stateless_rpc
) {
385 /* If we aren't using the stateless-rpc interface
386 * we don't need to retain the headers.
388 strbuf_setlen(&req_buf
, 0);
394 while ((sha1
= get_rev())) {
395 packet_buf_write(&req_buf
, "have %s\n", sha1_to_hex(sha1
));
397 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
399 if (flush_at
<= ++count
) {
402 packet_buf_flush(&req_buf
);
403 send_request(fd
[1], &req_buf
);
404 strbuf_setlen(&req_buf
, state_len
);
406 flush_at
= next_flush(count
);
409 * We keep one window "ahead" of the other side, and
410 * will wait for an ACK only on the next one
412 if (!args
.stateless_rpc
&& count
== INITIAL_FLUSH
)
415 consume_shallow_list(fd
[0]);
417 ack
= get_ack(fd
[0], result_sha1
);
418 if (args
.verbose
&& ack
)
419 fprintf(stderr
, "got ack %d %s\n", ack
,
420 sha1_to_hex(result_sha1
));
430 struct commit
*commit
=
431 lookup_commit(result_sha1
);
433 die("invalid commit %s", sha1_to_hex(result_sha1
));
434 if (args
.stateless_rpc
436 && !(commit
->object
.flags
& COMMON
)) {
437 /* We need to replay the have for this object
438 * on the next RPC request so the peer knows
439 * it is in common with us.
441 const char *hex
= sha1_to_hex(result_sha1
);
442 packet_buf_write(&req_buf
, "have %s\n", hex
);
443 state_len
= req_buf
.len
;
445 mark_common(commit
, 0, 1);
449 if (ack
== ACK_ready
) {
458 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
460 fprintf(stderr
, "giving up\n");
466 if (!got_ready
|| !no_done
) {
467 packet_buf_write(&req_buf
, "done\n");
468 send_request(fd
[1], &req_buf
);
471 fprintf(stderr
, "done\n");
476 strbuf_release(&req_buf
);
478 consume_shallow_list(fd
[0]);
479 while (flushes
|| multi_ack
) {
480 int ack
= get_ack(fd
[0], result_sha1
);
483 fprintf(stderr
, "got ack (%d) %s\n", ack
,
484 sha1_to_hex(result_sha1
));
492 /* it is no error to fetch into a completely empty repo */
493 return count
? retval
: 0;
496 static struct commit_list
*complete
;
498 static int mark_complete(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
500 struct object
*o
= parse_object(sha1
);
502 while (o
&& o
->type
== OBJ_TAG
) {
503 struct tag
*t
= (struct tag
*) o
;
505 break; /* broken repository */
506 o
->flags
|= COMPLETE
;
507 o
= parse_object(t
->tagged
->sha1
);
509 if (o
&& o
->type
== OBJ_COMMIT
) {
510 struct commit
*commit
= (struct commit
*)o
;
511 if (!(commit
->object
.flags
& COMPLETE
)) {
512 commit
->object
.flags
|= COMPLETE
;
513 commit_list_insert_by_date(commit
, &complete
);
519 static void mark_recent_complete_commits(unsigned long cutoff
)
521 while (complete
&& cutoff
<= complete
->item
->date
) {
523 fprintf(stderr
, "Marking %s as complete\n",
524 sha1_to_hex(complete
->item
->object
.sha1
));
525 pop_most_recent_commit(&complete
, COMPLETE
);
529 static void filter_refs(struct ref
**refs
, int nr_match
, char **match
)
531 struct ref
**return_refs
;
532 struct ref
*newlist
= NULL
;
533 struct ref
**newtail
= &newlist
;
534 struct ref
*ref
, *next
;
535 struct ref
*fastarray
[32];
537 if (nr_match
&& !args
.fetch_all
) {
538 if (ARRAY_SIZE(fastarray
) < nr_match
)
539 return_refs
= xcalloc(nr_match
, sizeof(struct ref
*));
541 return_refs
= fastarray
;
542 memset(return_refs
, 0, sizeof(struct ref
*) * nr_match
);
548 for (ref
= *refs
; ref
; ref
= next
) {
550 if (!memcmp(ref
->name
, "refs/", 5) &&
551 check_refname_format(ref
->name
+ 5, 0))
553 else if (args
.fetch_all
&&
554 (!args
.depth
|| prefixcmp(ref
->name
, "refs/tags/") )) {
557 newtail
= &ref
->next
;
562 for (i
= 0; i
< nr_match
; i
++) {
563 if (!strcmp(ref
->name
, match
[i
])) {
565 return_refs
[i
] = ref
;
570 continue; /* we will link it later */
575 if (!args
.fetch_all
) {
577 for (i
= 0; i
< nr_match
; i
++) {
578 ref
= return_refs
[i
];
582 newtail
= &ref
->next
;
585 if (return_refs
!= fastarray
)
591 static int everything_local(struct ref
**refs
, int nr_match
, char **match
)
595 unsigned long cutoff
= 0;
597 save_commit_buffer
= 0;
599 for (ref
= *refs
; ref
; ref
= ref
->next
) {
602 o
= parse_object(ref
->old_sha1
);
606 /* We already have it -- which may mean that we were
607 * in sync with the other side at some time after
608 * that (it is OK if we guess wrong here).
610 if (o
->type
== OBJ_COMMIT
) {
611 struct commit
*commit
= (struct commit
*)o
;
612 if (!cutoff
|| cutoff
< commit
->date
)
613 cutoff
= commit
->date
;
618 for_each_ref(mark_complete
, NULL
);
620 mark_recent_complete_commits(cutoff
);
624 * Mark all complete remote refs as common refs.
625 * Don't mark them common yet; the server has to be told so first.
627 for (ref
= *refs
; ref
; ref
= ref
->next
) {
628 struct object
*o
= deref_tag(lookup_object(ref
->old_sha1
),
631 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
634 if (!(o
->flags
& SEEN
)) {
635 rev_list_push((struct commit
*)o
, COMMON_REF
| SEEN
);
637 mark_common((struct commit
*)o
, 1, 1);
641 filter_refs(refs
, nr_match
, match
);
643 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
644 const unsigned char *remote
= ref
->old_sha1
;
645 unsigned char local
[20];
648 o
= lookup_object(remote
);
649 if (!o
|| !(o
->flags
& COMPLETE
)) {
654 "want %s (%s)\n", sha1_to_hex(remote
),
659 hashcpy(ref
->new_sha1
, local
);
663 "already have %s (%s)\n", sha1_to_hex(remote
),
669 static int sideband_demux(int in
, int out
, void *data
)
673 int ret
= recv_sideband("fetch-pack", xd
[0], out
);
678 static int get_pack(int xd
[2], char **pack_lockfile
)
681 const char *argv
[20];
685 int do_keep
= args
.keep_pack
;
686 struct child_process cmd
;
688 memset(&demux
, 0, sizeof(demux
));
690 /* xd[] is talking with upload-pack; subprocess reads from
691 * xd[0], spits out band#2 to stderr, and feeds us band#1
692 * through demux->out.
694 demux
.proc
= sideband_demux
;
697 if (start_async(&demux
))
698 die("fetch-pack: unable to fork off sideband"
704 memset(&cmd
, 0, sizeof(cmd
));
708 if (!args
.keep_pack
&& unpack_limit
) {
709 struct pack_header header
;
711 if (read_pack_header(demux
.out
, &header
))
712 die("protocol error: bad pack header");
713 snprintf(hdr_arg
, sizeof(hdr_arg
),
714 "--pack_header=%"PRIu32
",%"PRIu32
,
715 ntohl(header
.hdr_version
), ntohl(header
.hdr_entries
));
716 if (ntohl(header
.hdr_entries
) < unpack_limit
)
725 *av
++ = "index-pack";
727 if (!args
.quiet
&& !args
.no_progress
)
729 if (args
.use_thin_pack
)
730 *av
++ = "--fix-thin";
731 if (args
.lock_pack
|| unpack_limit
) {
732 int s
= sprintf(keep_arg
,
733 "--keep=fetch-pack %"PRIuMAX
" on ", (uintmax_t) getpid());
734 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
735 strcpy(keep_arg
+ s
, "localhost");
740 *av
++ = "unpack-objects";
746 if (fetch_fsck_objects
>= 0
748 : transfer_fsck_objects
>= 0
749 ? transfer_fsck_objects
756 if (start_command(&cmd
))
757 die("fetch-pack: unable to fork off %s", argv
[0]);
758 if (do_keep
&& pack_lockfile
) {
759 *pack_lockfile
= index_pack_lockfile(cmd
.out
);
763 if (finish_command(&cmd
))
764 die("%s failed", argv
[0]);
765 if (use_sideband
&& finish_async(&demux
))
766 die("error in sideband demultiplexer");
770 static struct ref
*do_fetch_pack(int fd
[2],
771 const struct ref
*orig_ref
,
774 char **pack_lockfile
)
776 struct ref
*ref
= copy_ref_list(orig_ref
);
777 unsigned char sha1
[20];
779 if (is_repository_shallow() && !server_supports("shallow"))
780 die("Server does not support shallow clients");
781 if (server_supports("multi_ack_detailed")) {
783 fprintf(stderr
, "Server supports multi_ack_detailed\n");
785 if (server_supports("no-done")) {
787 fprintf(stderr
, "Server supports no-done\n");
788 if (args
.stateless_rpc
)
792 else if (server_supports("multi_ack")) {
794 fprintf(stderr
, "Server supports multi_ack\n");
797 if (server_supports("side-band-64k")) {
799 fprintf(stderr
, "Server supports side-band-64k\n");
802 else if (server_supports("side-band")) {
804 fprintf(stderr
, "Server supports side-band\n");
807 if (server_supports("ofs-delta")) {
809 fprintf(stderr
, "Server supports ofs-delta\n");
811 prefer_ofs_delta
= 0;
812 if (everything_local(&ref
, nr_match
, match
)) {
816 if (find_common(fd
, sha1
, ref
) < 0)
818 /* When cloning, it is not unusual to have
821 warning("no common commits");
823 if (args
.stateless_rpc
)
825 if (get_pack(fd
, pack_lockfile
))
826 die("git fetch-pack: fetch failed.");
832 static int remove_duplicates(int nr_heads
, char **heads
)
836 for (src
= dst
= 0; src
< nr_heads
; src
++) {
837 /* If heads[src] is different from any of
838 * heads[0..dst], push it in.
841 for (i
= 0; i
< dst
; i
++) {
842 if (!strcmp(heads
[i
], heads
[src
]))
848 heads
[dst
] = heads
[src
];
854 static int fetch_pack_config(const char *var
, const char *value
, void *cb
)
856 if (strcmp(var
, "fetch.unpacklimit") == 0) {
857 fetch_unpack_limit
= git_config_int(var
, value
);
861 if (strcmp(var
, "transfer.unpacklimit") == 0) {
862 transfer_unpack_limit
= git_config_int(var
, value
);
866 if (strcmp(var
, "repack.usedeltabaseoffset") == 0) {
867 prefer_ofs_delta
= git_config_bool(var
, value
);
871 if (!strcmp(var
, "fetch.fsckobjects")) {
872 fetch_fsck_objects
= git_config_bool(var
, value
);
876 if (!strcmp(var
, "transfer.fsckobjects")) {
877 transfer_fsck_objects
= git_config_bool(var
, value
);
881 return git_default_config(var
, value
, cb
);
884 static struct lock_file lock
;
886 static void fetch_pack_setup(void)
888 static int did_setup
;
891 git_config(fetch_pack_config
, NULL
);
892 if (0 <= transfer_unpack_limit
)
893 unpack_limit
= transfer_unpack_limit
;
894 else if (0 <= fetch_unpack_limit
)
895 unpack_limit
= fetch_unpack_limit
;
899 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix
)
901 int i
, ret
, nr_heads
;
902 struct ref
*ref
= NULL
;
903 const char *dest
= NULL
;
906 char *pack_lockfile
= NULL
;
907 char **pack_lockfile_ptr
= NULL
;
908 struct child_process
*conn
;
910 packet_trace_identity("fetch-pack");
914 for (i
= 1; i
< argc
; i
++) {
915 const char *arg
= argv
[i
];
918 if (!prefixcmp(arg
, "--upload-pack=")) {
919 args
.uploadpack
= arg
+ 14;
922 if (!prefixcmp(arg
, "--exec=")) {
923 args
.uploadpack
= arg
+ 7;
926 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
930 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
931 args
.lock_pack
= args
.keep_pack
;
935 if (!strcmp("--thin", arg
)) {
936 args
.use_thin_pack
= 1;
939 if (!strcmp("--include-tag", arg
)) {
940 args
.include_tag
= 1;
943 if (!strcmp("--all", arg
)) {
947 if (!strcmp("--stdin", arg
)) {
951 if (!strcmp("-v", arg
)) {
955 if (!prefixcmp(arg
, "--depth=")) {
956 args
.depth
= strtol(arg
+ 8, NULL
, 0);
959 if (!strcmp("--no-progress", arg
)) {
960 args
.no_progress
= 1;
963 if (!strcmp("--stateless-rpc", arg
)) {
964 args
.stateless_rpc
= 1;
967 if (!strcmp("--lock-pack", arg
)) {
969 pack_lockfile_ptr
= &pack_lockfile
;
972 usage(fetch_pack_usage
);
975 heads
= (char **)(argv
+ i
+ 1);
976 nr_heads
= argc
- i
- 1;
980 usage(fetch_pack_usage
);
982 if (args
.stdin_refs
) {
984 * Copy refs from cmdline to new growable list, then
985 * append the refs from the standard input.
987 int alloc_heads
= nr_heads
;
988 int size
= nr_heads
* sizeof(*heads
);
989 heads
= memcpy(xmalloc(size
), heads
, size
);
990 if (args
.stateless_rpc
) {
991 /* in stateless RPC mode we use pkt-line to read
992 * from stdin, until we get a flush packet
994 static char line
[1000];
996 int n
= packet_read_line(0, line
, sizeof(line
));
999 if (line
[n
-1] == '\n')
1001 ALLOC_GROW(heads
, nr_heads
+ 1, alloc_heads
);
1002 heads
[nr_heads
++] = xmemdupz(line
, n
);
1006 /* read from stdin one ref per line, until EOF */
1007 struct strbuf line
= STRBUF_INIT
;
1008 while (strbuf_getline(&line
, stdin
, '\n') != EOF
) {
1009 ALLOC_GROW(heads
, nr_heads
+ 1, alloc_heads
);
1010 heads
[nr_heads
++] = strbuf_detach(&line
, NULL
);
1012 strbuf_release(&line
);
1016 if (args
.stateless_rpc
) {
1021 conn
= git_connect(fd
, dest
, args
.uploadpack
,
1022 args
.verbose
? CONNECT_VERBOSE
: 0);
1025 get_remote_heads(fd
[0], &ref
, 0, NULL
);
1027 ref
= fetch_pack(&args
, fd
, conn
, ref
, dest
,
1028 nr_heads
, heads
, pack_lockfile_ptr
);
1029 if (pack_lockfile
) {
1030 printf("lock %s\n", pack_lockfile
);
1035 if (finish_connect(conn
))
1039 if (!ret
&& nr_heads
) {
1040 /* If the heads to pull were given, we should have
1041 * consumed all of them by matching the remote.
1042 * Otherwise, 'git fetch remote no-such-ref' would
1043 * silently succeed without issuing an error.
1045 for (i
= 0; i
< nr_heads
; i
++)
1046 if (heads
[i
] && heads
[i
][0]) {
1047 error("no such remote ref %s", heads
[i
]);
1053 sha1_to_hex(ref
->old_sha1
), ref
->name
);
1060 struct ref
*fetch_pack(struct fetch_pack_args
*my_args
,
1061 int fd
[], struct child_process
*conn
,
1062 const struct ref
*ref
,
1066 char **pack_lockfile
)
1069 struct ref
*ref_cpy
;
1072 if (&args
!= my_args
)
1073 memcpy(&args
, my_args
, sizeof(args
));
1074 if (args
.depth
> 0) {
1075 if (stat(git_path("shallow"), &st
))
1079 if (heads
&& nr_heads
)
1080 nr_heads
= remove_duplicates(nr_heads
, heads
);
1082 packet_flush(fd
[1]);
1083 die("no matching remote head");
1085 ref_cpy
= do_fetch_pack(fd
, ref
, nr_heads
, heads
, pack_lockfile
);
1087 if (args
.depth
> 0) {
1088 struct cache_time mtime
;
1089 struct strbuf sb
= STRBUF_INIT
;
1090 char *shallow
= git_path("shallow");
1093 mtime
.sec
= st
.st_mtime
;
1094 mtime
.nsec
= ST_MTIME_NSEC(st
);
1095 if (stat(shallow
, &st
)) {
1097 die("shallow file was removed during fetch");
1098 } else if (st
.st_mtime
!= mtime
.sec
1100 || ST_MTIME_NSEC(st
) != mtime
.nsec
1103 die("shallow file was changed during fetch");
1105 fd
= hold_lock_file_for_update(&lock
, shallow
,
1107 if (!write_shallow_commits(&sb
, 0)
1108 || write_in_full(fd
, sb
.buf
, sb
.len
) != sb
.len
) {
1109 unlink_or_warn(shallow
);
1110 rollback_lock_file(&lock
);
1112 commit_lock_file(&lock
);
1114 strbuf_release(&sb
);
1117 reprepare_packed_git();