9 #include "fetch-pack.h"
11 #include "run-command.h"
13 static int transfer_unpack_limit
= -1;
14 static int fetch_unpack_limit
= -1;
15 static int unpack_limit
= 100;
16 static struct fetch_pack_args args
= {
17 /* .uploadpack = */ "git-upload-pack",
20 static const char fetch_pack_usage
[] =
21 "git-fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]";
23 #define COMPLETE (1U << 0)
24 #define COMMON (1U << 1)
25 #define COMMON_REF (1U << 2)
26 #define SEEN (1U << 3)
27 #define POPPED (1U << 4)
30 * After sending this many "have"s if we do not get any new ACK , we
31 * give up traversing our history.
33 #define MAX_IN_VAIN 256
35 static struct commit_list
*rev_list
;
36 static int non_common_revs
, multi_ack
, use_sideband
;
38 static void rev_list_push(struct commit
*commit
, int mark
)
40 if (!(commit
->object
.flags
& mark
)) {
41 commit
->object
.flags
|= mark
;
43 if (!(commit
->object
.parsed
))
44 if (parse_commit(commit
))
47 insert_by_date(commit
, &rev_list
);
49 if (!(commit
->object
.flags
& COMMON
))
54 static int rev_list_insert_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
56 struct object
*o
= deref_tag(parse_object(sha1
), path
, 0);
58 if (o
&& o
->type
== OBJ_COMMIT
)
59 rev_list_push((struct commit
*)o
, SEEN
);
65 This function marks a rev and its ancestors as common.
66 In some cases, it is desirable to mark only the ancestors (for example
67 when only the server does not yet know that they are common).
70 static void mark_common(struct commit
*commit
,
71 int ancestors_only
, int dont_parse
)
73 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
74 struct object
*o
= (struct object
*)commit
;
79 if (!(o
->flags
& SEEN
))
80 rev_list_push(commit
, SEEN
);
82 struct commit_list
*parents
;
84 if (!ancestors_only
&& !(o
->flags
& POPPED
))
86 if (!o
->parsed
&& !dont_parse
)
87 if (parse_commit(commit
))
90 for (parents
= commit
->parents
;
92 parents
= parents
->next
)
93 mark_common(parents
->item
, 0, dont_parse
);
99 Get the next rev to send, ignoring the common.
102 static const unsigned char* get_rev(void)
104 struct commit
*commit
= NULL
;
106 while (commit
== NULL
) {
108 struct commit_list
*parents
= NULL
;
110 if (rev_list
== NULL
|| non_common_revs
== 0)
113 commit
= rev_list
->item
;
114 if (!(commit
->object
.parsed
))
115 if (!parse_commit(commit
))
116 parents
= commit
->parents
;
118 commit
->object
.flags
|= POPPED
;
119 if (!(commit
->object
.flags
& COMMON
))
122 if (commit
->object
.flags
& COMMON
) {
123 /* do not send "have", and ignore ancestors */
125 mark
= COMMON
| SEEN
;
126 } else if (commit
->object
.flags
& COMMON_REF
)
127 /* send "have", and ignore ancestors */
128 mark
= COMMON
| SEEN
;
130 /* send "have", also for its ancestors */
134 if (!(parents
->item
->object
.flags
& SEEN
))
135 rev_list_push(parents
->item
, mark
);
137 mark_common(parents
->item
, 1, 0);
138 parents
= parents
->next
;
141 rev_list
= rev_list
->next
;
144 return commit
->object
.sha1
;
147 static int find_common(int fd
[2], unsigned char *result_sha1
,
151 int count
= 0, flushes
= 0, retval
;
152 const unsigned char *sha1
;
153 unsigned in_vain
= 0;
154 int got_continue
= 0;
156 for_each_ref(rev_list_insert_ref
, NULL
);
159 for ( ; refs
; refs
= refs
->next
) {
160 unsigned char *remote
= refs
->old_sha1
;
164 * If that object is complete (i.e. it is an ancestor of a
165 * local ref), we tell them we have it but do not have to
166 * tell them about its ancestors, which they already know
169 * We use lookup_object here because we are only
170 * interested in the case we *know* the object is
171 * reachable and we have already scanned it.
173 if (((o
= lookup_object(remote
)) != NULL
) &&
174 (o
->flags
& COMPLETE
)) {
179 packet_write(fd
[1], "want %s%s%s%s%s%s%s%s\n",
181 (multi_ack
? " multi_ack" : ""),
182 (use_sideband
== 2 ? " side-band-64k" : ""),
183 (use_sideband
== 1 ? " side-band" : ""),
184 (args
.use_thin_pack
? " thin-pack" : ""),
185 (args
.no_progress
? " no-progress" : ""),
186 (args
.include_tag
? " include-tag" : ""),
189 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
192 if (is_repository_shallow())
193 write_shallow_commits(fd
[1], 1);
195 packet_write(fd
[1], "deepen %d", args
.depth
);
200 if (args
.depth
> 0) {
202 unsigned char sha1
[20];
205 while ((len
= packet_read_line(fd
[0], line
, sizeof(line
)))) {
206 if (!prefixcmp(line
, "shallow ")) {
207 if (get_sha1_hex(line
+ 8, sha1
))
208 die("invalid shallow line: %s", line
);
209 register_shallow(sha1
);
212 if (!prefixcmp(line
, "unshallow ")) {
213 if (get_sha1_hex(line
+ 10, sha1
))
214 die("invalid unshallow line: %s", line
);
215 if (!lookup_object(sha1
))
216 die("object not found: %s", line
);
217 /* make sure that it is parsed as shallow */
218 if (!parse_object(sha1
))
219 die("error in object: %s", line
);
220 if (unregister_shallow(sha1
))
221 die("no shallow found: %s", line
);
224 die("expected shallow/unshallow, got %s", line
);
230 while ((sha1
= get_rev())) {
231 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
233 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
235 if (!(31 & ++count
)) {
242 * We keep one window "ahead" of the other side, and
243 * will wait for an ACK only on the next one
249 ack
= get_ack(fd
[0], result_sha1
);
250 if (args
.verbose
&& ack
)
251 fprintf(stderr
, "got ack %d %s\n", ack
,
252 sha1_to_hex(result_sha1
));
258 } else if (ack
== 2) {
259 struct commit
*commit
=
260 lookup_commit(result_sha1
);
261 mark_common(commit
, 0, 1);
268 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
270 fprintf(stderr
, "giving up\n");
276 packet_write(fd
[1], "done\n");
278 fprintf(stderr
, "done\n");
283 while (flushes
|| multi_ack
) {
284 int ack
= get_ack(fd
[0], result_sha1
);
287 fprintf(stderr
, "got ack (%d) %s\n", ack
,
288 sha1_to_hex(result_sha1
));
299 static struct commit_list
*complete
;
301 static int mark_complete(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
303 struct object
*o
= parse_object(sha1
);
305 while (o
&& o
->type
== OBJ_TAG
) {
306 struct tag
*t
= (struct tag
*) o
;
308 break; /* broken repository */
309 o
->flags
|= COMPLETE
;
310 o
= parse_object(t
->tagged
->sha1
);
312 if (o
&& o
->type
== OBJ_COMMIT
) {
313 struct commit
*commit
= (struct commit
*)o
;
314 commit
->object
.flags
|= COMPLETE
;
315 insert_by_date(commit
, &complete
);
320 static void mark_recent_complete_commits(unsigned long cutoff
)
322 while (complete
&& cutoff
<= complete
->item
->date
) {
324 fprintf(stderr
, "Marking %s as complete\n",
325 sha1_to_hex(complete
->item
->object
.sha1
));
326 pop_most_recent_commit(&complete
, COMPLETE
);
330 static void filter_refs(struct ref
**refs
, int nr_match
, char **match
)
332 struct ref
**return_refs
;
333 struct ref
*newlist
= NULL
;
334 struct ref
**newtail
= &newlist
;
335 struct ref
*ref
, *next
;
336 struct ref
*fastarray
[32];
338 if (nr_match
&& !args
.fetch_all
) {
339 if (ARRAY_SIZE(fastarray
) < nr_match
)
340 return_refs
= xcalloc(nr_match
, sizeof(struct ref
*));
342 return_refs
= fastarray
;
343 memset(return_refs
, 0, sizeof(struct ref
*) * nr_match
);
349 for (ref
= *refs
; ref
; ref
= next
) {
351 if (!memcmp(ref
->name
, "refs/", 5) &&
352 check_ref_format(ref
->name
+ 5))
354 else if (args
.fetch_all
&&
355 (!args
.depth
|| prefixcmp(ref
->name
, "refs/tags/") )) {
358 newtail
= &ref
->next
;
362 int order
= path_match(ref
->name
, nr_match
, match
);
364 return_refs
[order
-1] = ref
;
365 continue; /* we will link it later */
371 if (!args
.fetch_all
) {
373 for (i
= 0; i
< nr_match
; i
++) {
374 ref
= return_refs
[i
];
378 newtail
= &ref
->next
;
381 if (return_refs
!= fastarray
)
387 static int everything_local(struct ref
**refs
, int nr_match
, char **match
)
391 unsigned long cutoff
= 0;
393 save_commit_buffer
= 0;
395 for (ref
= *refs
; ref
; ref
= ref
->next
) {
398 o
= parse_object(ref
->old_sha1
);
402 /* We already have it -- which may mean that we were
403 * in sync with the other side at some time after
404 * that (it is OK if we guess wrong here).
406 if (o
->type
== OBJ_COMMIT
) {
407 struct commit
*commit
= (struct commit
*)o
;
408 if (!cutoff
|| cutoff
< commit
->date
)
409 cutoff
= commit
->date
;
414 for_each_ref(mark_complete
, NULL
);
416 mark_recent_complete_commits(cutoff
);
420 * Mark all complete remote refs as common refs.
421 * Don't mark them common yet; the server has to be told so first.
423 for (ref
= *refs
; ref
; ref
= ref
->next
) {
424 struct object
*o
= deref_tag(lookup_object(ref
->old_sha1
),
427 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
430 if (!(o
->flags
& SEEN
)) {
431 rev_list_push((struct commit
*)o
, COMMON_REF
| SEEN
);
433 mark_common((struct commit
*)o
, 1, 1);
437 filter_refs(refs
, nr_match
, match
);
439 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
440 const unsigned char *remote
= ref
->old_sha1
;
441 unsigned char local
[20];
444 o
= lookup_object(remote
);
445 if (!o
|| !(o
->flags
& COMPLETE
)) {
450 "want %s (%s)\n", sha1_to_hex(remote
),
455 hashcpy(ref
->new_sha1
, local
);
459 "already have %s (%s)\n", sha1_to_hex(remote
),
465 static int sideband_demux(int fd
, void *data
)
469 return recv_sideband("fetch-pack", xd
[0], fd
, 2);
472 static int get_pack(int xd
[2], char **pack_lockfile
)
475 const char *argv
[20];
479 int do_keep
= args
.keep_pack
;
480 struct child_process cmd
;
482 memset(&demux
, 0, sizeof(demux
));
484 /* xd[] is talking with upload-pack; subprocess reads from
485 * xd[0], spits out band#2 to stderr, and feeds us band#1
486 * through demux->out.
488 demux
.proc
= sideband_demux
;
490 if (start_async(&demux
))
491 die("fetch-pack: unable to fork off sideband"
497 memset(&cmd
, 0, sizeof(cmd
));
501 if (!args
.keep_pack
&& unpack_limit
) {
502 struct pack_header header
;
504 if (read_pack_header(demux
.out
, &header
))
505 die("protocol error: bad pack header");
506 snprintf(hdr_arg
, sizeof(hdr_arg
), "--pack_header=%u,%u",
507 ntohl(header
.hdr_version
), ntohl(header
.hdr_entries
));
508 if (ntohl(header
.hdr_entries
) < unpack_limit
)
517 *av
++ = "index-pack";
519 if (!args
.quiet
&& !args
.no_progress
)
521 if (args
.use_thin_pack
)
522 *av
++ = "--fix-thin";
523 if (args
.lock_pack
|| unpack_limit
) {
524 int s
= sprintf(keep_arg
,
525 "--keep=fetch-pack %d on ", getpid());
526 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
527 strcpy(keep_arg
+ s
, "localhost");
532 *av
++ = "unpack-objects";
542 if (start_command(&cmd
))
543 die("fetch-pack: unable to fork off %s", argv
[0]);
544 if (do_keep
&& pack_lockfile
) {
545 *pack_lockfile
= index_pack_lockfile(cmd
.out
);
549 if (finish_command(&cmd
))
550 die("%s failed", argv
[0]);
551 if (use_sideband
&& finish_async(&demux
))
552 die("error in sideband demultiplexer");
556 static struct ref
*do_fetch_pack(int fd
[2],
557 const struct ref
*orig_ref
,
560 char **pack_lockfile
)
562 struct ref
*ref
= copy_ref_list(orig_ref
);
563 unsigned char sha1
[20];
565 if (is_repository_shallow() && !server_supports("shallow"))
566 die("Server does not support shallow clients");
567 if (server_supports("multi_ack")) {
569 fprintf(stderr
, "Server supports multi_ack\n");
572 if (server_supports("side-band-64k")) {
574 fprintf(stderr
, "Server supports side-band-64k\n");
577 else if (server_supports("side-band")) {
579 fprintf(stderr
, "Server supports side-band\n");
582 if (everything_local(&ref
, nr_match
, match
)) {
586 if (find_common(fd
, sha1
, ref
) < 0)
588 /* When cloning, it is not unusual to have
591 fprintf(stderr
, "warning: no common commits\n");
593 if (get_pack(fd
, pack_lockfile
))
594 die("git-fetch-pack: fetch failed.");
600 static int remove_duplicates(int nr_heads
, char **heads
)
604 for (src
= dst
= 0; src
< nr_heads
; src
++) {
605 /* If heads[src] is different from any of
606 * heads[0..dst], push it in.
609 for (i
= 0; i
< dst
; i
++) {
610 if (!strcmp(heads
[i
], heads
[src
]))
616 heads
[dst
] = heads
[src
];
622 static int fetch_pack_config(const char *var
, const char *value
)
624 if (strcmp(var
, "fetch.unpacklimit") == 0) {
625 fetch_unpack_limit
= git_config_int(var
, value
);
629 if (strcmp(var
, "transfer.unpacklimit") == 0) {
630 transfer_unpack_limit
= git_config_int(var
, value
);
634 return git_default_config(var
, value
);
637 static struct lock_file lock
;
639 static void fetch_pack_setup(void)
641 static int did_setup
;
644 git_config(fetch_pack_config
);
645 if (0 <= transfer_unpack_limit
)
646 unpack_limit
= transfer_unpack_limit
;
647 else if (0 <= fetch_unpack_limit
)
648 unpack_limit
= fetch_unpack_limit
;
652 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix
)
654 int i
, ret
, nr_heads
;
655 struct ref
*ref
= NULL
;
656 char *dest
= NULL
, **heads
;
658 struct child_process
*conn
;
662 for (i
= 1; i
< argc
; i
++) {
663 const char *arg
= argv
[i
];
666 if (!prefixcmp(arg
, "--upload-pack=")) {
667 args
.uploadpack
= arg
+ 14;
670 if (!prefixcmp(arg
, "--exec=")) {
671 args
.uploadpack
= arg
+ 7;
674 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
678 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
679 args
.lock_pack
= args
.keep_pack
;
683 if (!strcmp("--thin", arg
)) {
684 args
.use_thin_pack
= 1;
687 if (!strcmp("--include-tag", arg
)) {
688 args
.include_tag
= 1;
691 if (!strcmp("--all", arg
)) {
695 if (!strcmp("-v", arg
)) {
699 if (!prefixcmp(arg
, "--depth=")) {
700 args
.depth
= strtol(arg
+ 8, NULL
, 0);
703 if (!strcmp("--no-progress", arg
)) {
704 args
.no_progress
= 1;
707 usage(fetch_pack_usage
);
710 heads
= (char **)(argv
+ i
+ 1);
711 nr_heads
= argc
- i
- 1;
715 usage(fetch_pack_usage
);
717 conn
= git_connect(fd
, (char *)dest
, args
.uploadpack
,
718 args
.verbose
? CONNECT_VERBOSE
: 0);
720 get_remote_heads(fd
[0], &ref
, 0, NULL
, 0);
722 ref
= fetch_pack(&args
, fd
, conn
, ref
, dest
, nr_heads
, heads
, NULL
);
725 if (finish_connect(conn
))
732 if (!ret
&& nr_heads
) {
733 /* If the heads to pull were given, we should have
734 * consumed all of them by matching the remote.
735 * Otherwise, 'git-fetch remote no-such-ref' would
736 * silently succeed without issuing an error.
738 for (i
= 0; i
< nr_heads
; i
++)
739 if (heads
[i
] && heads
[i
][0]) {
740 error("no such remote ref %s", heads
[i
]);
746 sha1_to_hex(ref
->old_sha1
), ref
->name
);
753 struct ref
*fetch_pack(struct fetch_pack_args
*my_args
,
754 int fd
[], struct child_process
*conn
,
755 const struct ref
*ref
,
759 char **pack_lockfile
)
765 memcpy(&args
, my_args
, sizeof(args
));
766 if (args
.depth
> 0) {
767 if (stat(git_path("shallow"), &st
))
771 if (heads
&& nr_heads
)
772 nr_heads
= remove_duplicates(nr_heads
, heads
);
775 die("no matching remote head");
777 ref_cpy
= do_fetch_pack(fd
, ref
, nr_heads
, heads
, pack_lockfile
);
779 if (args
.depth
> 0) {
780 struct cache_time mtime
;
781 char *shallow
= git_path("shallow");
784 mtime
.sec
= st
.st_mtime
;
786 mtime
.usec
= st
.st_mtim
.usec
;
788 if (stat(shallow
, &st
)) {
790 die("shallow file was removed during fetch");
791 } else if (st
.st_mtime
!= mtime
.sec
793 || st
.st_mtim
.usec
!= mtime
.usec
796 die("shallow file was changed during fetch");
798 fd
= hold_lock_file_for_update(&lock
, shallow
, 1);
799 if (!write_shallow_commits(fd
, 0)) {
801 rollback_lock_file(&lock
);
803 commit_lock_file(&lock
);