11 static int transfer_unpack_limit
= -1;
12 static int fetch_unpack_limit
= -1;
13 static int unpack_limit
= 100;
18 static int no_progress
;
19 static const char fetch_pack_usage
[] =
20 "git-fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]";
21 static const char *uploadpack
= "git-upload-pack";
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_thin_pack
, 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
))
46 insert_by_date(commit
, &rev_list
);
48 if (!(commit
->object
.flags
& COMMON
))
53 static int rev_list_insert_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
55 struct object
*o
= deref_tag(parse_object(sha1
), path
, 0);
57 if (o
&& o
->type
== OBJ_COMMIT
)
58 rev_list_push((struct commit
*)o
, SEEN
);
64 This function marks a rev and its ancestors as common.
65 In some cases, it is desirable to mark only the ancestors (for example
66 when only the server does not yet know that they are common).
69 static void mark_common(struct commit
*commit
,
70 int ancestors_only
, int dont_parse
)
72 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
73 struct object
*o
= (struct object
*)commit
;
78 if (!(o
->flags
& SEEN
))
79 rev_list_push(commit
, SEEN
);
81 struct commit_list
*parents
;
83 if (!ancestors_only
&& !(o
->flags
& POPPED
))
85 if (!o
->parsed
&& !dont_parse
)
88 for (parents
= commit
->parents
;
90 parents
= parents
->next
)
91 mark_common(parents
->item
, 0, dont_parse
);
97 Get the next rev to send, ignoring the common.
100 static const unsigned char* get_rev(void)
102 struct commit
*commit
= NULL
;
104 while (commit
== NULL
) {
106 struct commit_list
* parents
;
108 if (rev_list
== NULL
|| non_common_revs
== 0)
111 commit
= rev_list
->item
;
112 if (!(commit
->object
.parsed
))
113 parse_commit(commit
);
114 commit
->object
.flags
|= POPPED
;
115 if (!(commit
->object
.flags
& COMMON
))
118 parents
= commit
->parents
;
120 if (commit
->object
.flags
& COMMON
) {
121 /* do not send "have", and ignore ancestors */
123 mark
= COMMON
| SEEN
;
124 } else if (commit
->object
.flags
& COMMON_REF
)
125 /* send "have", and ignore ancestors */
126 mark
= COMMON
| SEEN
;
128 /* send "have", also for its ancestors */
132 if (!(parents
->item
->object
.flags
& SEEN
))
133 rev_list_push(parents
->item
, mark
);
135 mark_common(parents
->item
, 1, 0);
136 parents
= parents
->next
;
139 rev_list
= rev_list
->next
;
142 return commit
->object
.sha1
;
145 static int find_common(int fd
[2], unsigned char *result_sha1
,
149 int count
= 0, flushes
= 0, retval
;
150 const unsigned char *sha1
;
151 unsigned in_vain
= 0;
152 int got_continue
= 0;
154 for_each_ref(rev_list_insert_ref
, NULL
);
157 for ( ; refs
; refs
= refs
->next
) {
158 unsigned char *remote
= refs
->old_sha1
;
162 * If that object is complete (i.e. it is an ancestor of a
163 * local ref), we tell them we have it but do not have to
164 * tell them about its ancestors, which they already know
167 * We use lookup_object here because we are only
168 * interested in the case we *know* the object is
169 * reachable and we have already scanned it.
171 if (((o
= lookup_object(remote
)) != NULL
) &&
172 (o
->flags
& COMPLETE
)) {
177 packet_write(fd
[1], "want %s%s%s%s%s%s%s\n",
179 (multi_ack
? " multi_ack" : ""),
180 (use_sideband
== 2 ? " side-band-64k" : ""),
181 (use_sideband
== 1 ? " side-band" : ""),
182 (use_thin_pack
? " thin-pack" : ""),
183 (no_progress
? " no-progress" : ""),
186 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
189 if (is_repository_shallow())
190 write_shallow_commits(fd
[1], 1);
192 packet_write(fd
[1], "deepen %d", depth
);
199 unsigned char sha1
[20];
202 while ((len
= packet_read_line(fd
[0], line
, sizeof(line
)))) {
203 if (!prefixcmp(line
, "shallow ")) {
204 if (get_sha1_hex(line
+ 8, sha1
))
205 die("invalid shallow line: %s", line
);
206 register_shallow(sha1
);
209 if (!prefixcmp(line
, "unshallow ")) {
210 if (get_sha1_hex(line
+ 10, sha1
))
211 die("invalid unshallow line: %s", line
);
212 if (!lookup_object(sha1
))
213 die("object not found: %s", line
);
214 /* make sure that it is parsed as shallow */
216 if (unregister_shallow(sha1
))
217 die("no shallow found: %s", line
);
220 die("expected shallow/unshallow, got %s", line
);
226 while ((sha1
= get_rev())) {
227 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
229 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
231 if (!(31 & ++count
)) {
238 * We keep one window "ahead" of the other side, and
239 * will wait for an ACK only on the next one
245 ack
= get_ack(fd
[0], result_sha1
);
247 fprintf(stderr
, "got ack %d %s\n", ack
,
248 sha1_to_hex(result_sha1
));
254 } else if (ack
== 2) {
255 struct commit
*commit
=
256 lookup_commit(result_sha1
);
257 mark_common(commit
, 0, 1);
264 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
266 fprintf(stderr
, "giving up\n");
272 packet_write(fd
[1], "done\n");
274 fprintf(stderr
, "done\n");
279 while (flushes
|| multi_ack
) {
280 int ack
= get_ack(fd
[0], result_sha1
);
283 fprintf(stderr
, "got ack (%d) %s\n", ack
,
284 sha1_to_hex(result_sha1
));
295 static struct commit_list
*complete
;
297 static int mark_complete(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
299 struct object
*o
= parse_object(sha1
);
301 while (o
&& o
->type
== OBJ_TAG
) {
302 struct tag
*t
= (struct tag
*) o
;
304 break; /* broken repository */
305 o
->flags
|= COMPLETE
;
306 o
= parse_object(t
->tagged
->sha1
);
308 if (o
&& o
->type
== OBJ_COMMIT
) {
309 struct commit
*commit
= (struct commit
*)o
;
310 commit
->object
.flags
|= COMPLETE
;
311 insert_by_date(commit
, &complete
);
316 static void mark_recent_complete_commits(unsigned long cutoff
)
318 while (complete
&& cutoff
<= complete
->item
->date
) {
320 fprintf(stderr
, "Marking %s as complete\n",
321 sha1_to_hex(complete
->item
->object
.sha1
));
322 pop_most_recent_commit(&complete
, COMPLETE
);
326 static void filter_refs(struct ref
**refs
, int nr_match
, char **match
)
328 struct ref
**return_refs
;
329 struct ref
*newlist
= NULL
;
330 struct ref
**newtail
= &newlist
;
331 struct ref
*ref
, *next
;
332 struct ref
*fastarray
[32];
334 if (nr_match
&& !fetch_all
) {
335 if (ARRAY_SIZE(fastarray
) < nr_match
)
336 return_refs
= xcalloc(nr_match
, sizeof(struct ref
*));
338 return_refs
= fastarray
;
339 memset(return_refs
, 0, sizeof(struct ref
*) * nr_match
);
345 for (ref
= *refs
; ref
; ref
= next
) {
347 if (!memcmp(ref
->name
, "refs/", 5) &&
348 check_ref_format(ref
->name
+ 5))
350 else if (fetch_all
&&
351 (!depth
|| prefixcmp(ref
->name
, "refs/tags/") )) {
354 newtail
= &ref
->next
;
358 int order
= path_match(ref
->name
, nr_match
, match
);
360 return_refs
[order
-1] = ref
;
361 continue; /* we will link it later */
369 for (i
= 0; i
< nr_match
; i
++) {
370 ref
= return_refs
[i
];
374 newtail
= &ref
->next
;
377 if (return_refs
!= fastarray
)
383 static int everything_local(struct ref
**refs
, int nr_match
, char **match
)
387 unsigned long cutoff
= 0;
389 track_object_refs
= 0;
390 save_commit_buffer
= 0;
392 for (ref
= *refs
; ref
; ref
= ref
->next
) {
395 o
= parse_object(ref
->old_sha1
);
399 /* We already have it -- which may mean that we were
400 * in sync with the other side at some time after
401 * that (it is OK if we guess wrong here).
403 if (o
->type
== OBJ_COMMIT
) {
404 struct commit
*commit
= (struct commit
*)o
;
405 if (!cutoff
|| cutoff
< commit
->date
)
406 cutoff
= commit
->date
;
411 for_each_ref(mark_complete
, NULL
);
413 mark_recent_complete_commits(cutoff
);
417 * Mark all complete remote refs as common refs.
418 * Don't mark them common yet; the server has to be told so first.
420 for (ref
= *refs
; ref
; ref
= ref
->next
) {
421 struct object
*o
= deref_tag(lookup_object(ref
->old_sha1
),
424 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
427 if (!(o
->flags
& SEEN
)) {
428 rev_list_push((struct commit
*)o
, COMMON_REF
| SEEN
);
430 mark_common((struct commit
*)o
, 1, 1);
434 filter_refs(refs
, nr_match
, match
);
436 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
437 const unsigned char *remote
= ref
->old_sha1
;
438 unsigned char local
[20];
441 o
= lookup_object(remote
);
442 if (!o
|| !(o
->flags
& COMPLETE
)) {
447 "want %s (%s)\n", sha1_to_hex(remote
),
452 hashcpy(ref
->new_sha1
, local
);
456 "already have %s (%s)\n", sha1_to_hex(remote
),
462 static pid_t
setup_sideband(int fd
[2], int xd
[2])
471 /* xd[] is talking with upload-pack; subprocess reads from
472 * xd[0], spits out band#2 to stderr, and feeds us band#1
476 die("fetch-pack: unable to set up pipe");
479 die("fetch-pack: unable to fork off sideband demultiplexer");
485 if (recv_sideband("fetch-pack", xd
[0], fd
[1], 2))
495 static int get_pack(int xd
[2])
500 const char *argv
[20];
504 int do_keep
= keep_pack
;
506 side_pid
= setup_sideband(fd
, xd
);
511 struct pack_header header
;
513 if (read_pack_header(fd
[0], &header
))
514 die("protocol error: bad pack header");
515 snprintf(hdr_arg
, sizeof(hdr_arg
), "--pack_header=%u,%u",
516 ntohl(header
.hdr_version
), ntohl(header
.hdr_entries
));
517 if (ntohl(header
.hdr_entries
) < unpack_limit
)
524 *av
++ = "index-pack";
526 if (!quiet
&& !no_progress
)
529 *av
++ = "--fix-thin";
530 if (keep_pack
> 1 || unpack_limit
) {
531 int s
= sprintf(keep_arg
,
532 "--keep=fetch-pack %d on ", getpid());
533 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
534 strcpy(keep_arg
+ s
, "localhost");
539 *av
++ = "unpack-objects";
547 pid
= spawnv_git_cmd(argv
, fd
, NULL
);
549 die("fetch-pack: unable to fork off %s", argv
[0]);
551 while (waitpid(pid
, &status
, 0) < 0) {
553 die("waiting for %s: %s", argv
[0], strerror(errno
));
555 if (WIFEXITED(status
)) {
556 int code
= WEXITSTATUS(status
);
558 die("%s died with error code %d", argv
[0], code
);
561 if (WIFSIGNALED(status
)) {
562 int sig
= WTERMSIG(status
);
563 die("%s died of signal %d", argv
[0], sig
);
565 die("%s died of unnatural causes %d", argv
[0], status
);
568 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
571 unsigned char sha1
[20];
573 get_remote_heads(fd
[0], &ref
, 0, NULL
, 0);
574 if (is_repository_shallow() && !server_supports("shallow"))
575 die("Server does not support shallow clients");
576 if (server_supports("multi_ack")) {
578 fprintf(stderr
, "Server supports multi_ack\n");
582 if (server_supports("side-band-64k")) {
584 fprintf(stderr
, "Server supports side-band-64k\n");
587 else if (server_supports("side-band")) {
589 fprintf(stderr
, "Server supports side-band\n");
595 die("no matching remote head");
597 if (everything_local(&ref
, nr_match
, match
)) {
601 if (find_common(fd
, sha1
, ref
) < 0)
603 /* When cloning, it is not unusual to have
606 fprintf(stderr
, "warning: no common commits\n");
609 die("git-fetch-pack: fetch failed.");
614 sha1_to_hex(ref
->old_sha1
), ref
->name
);
620 static int remove_duplicates(int nr_heads
, char **heads
)
624 for (src
= dst
= 0; src
< nr_heads
; src
++) {
625 /* If heads[src] is different from any of
626 * heads[0..dst], push it in.
629 for (i
= 0; i
< dst
; i
++) {
630 if (!strcmp(heads
[i
], heads
[src
]))
636 heads
[dst
] = heads
[src
];
643 static int fetch_pack_config(const char *var
, const char *value
)
645 if (strcmp(var
, "fetch.unpacklimit") == 0) {
646 fetch_unpack_limit
= git_config_int(var
, value
);
650 if (strcmp(var
, "transfer.unpacklimit") == 0) {
651 transfer_unpack_limit
= git_config_int(var
, value
);
655 return git_default_config(var
, value
);
658 static struct lock_file lock
;
660 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix
)
662 int i
, ret
, nr_heads
;
663 char *dest
= NULL
, **heads
;
668 git_config(fetch_pack_config
);
670 if (0 <= transfer_unpack_limit
)
671 unpack_limit
= transfer_unpack_limit
;
672 else if (0 <= fetch_unpack_limit
)
673 unpack_limit
= fetch_unpack_limit
;
677 for (i
= 1; i
< argc
; i
++) {
681 if (!prefixcmp(arg
, "--upload-pack=")) {
682 uploadpack
= arg
+ 14;
685 if (!prefixcmp(arg
, "--exec=")) {
686 uploadpack
= arg
+ 7;
689 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
693 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
698 if (!strcmp("--thin", arg
)) {
702 if (!strcmp("--all", arg
)) {
706 if (!strcmp("-v", arg
)) {
710 if (!prefixcmp(arg
, "--depth=")) {
711 depth
= strtol(arg
+ 8, NULL
, 0);
712 if (stat(git_path("shallow"), &st
))
716 if (!strcmp("--no-progress", arg
)) {
720 usage(fetch_pack_usage
);
723 heads
= argv
+ i
+ 1;
724 nr_heads
= argc
- i
- 1;
728 usage(fetch_pack_usage
);
729 pid
= git_connect(fd
, dest
, uploadpack
, verbose
? CONNECT_VERBOSE
: 0);
732 if (heads
&& nr_heads
)
733 nr_heads
= remove_duplicates(nr_heads
, heads
);
734 ret
= fetch_pack(fd
, nr_heads
, heads
);
737 ret
|= finish_connect(pid
);
739 if (!ret
&& nr_heads
) {
740 /* If the heads to pull were given, we should have
741 * consumed all of them by matching the remote.
742 * Otherwise, 'git-fetch remote no-such-ref' would
743 * silently succeed without issuing an error.
745 for (i
= 0; i
< nr_heads
; i
++)
746 if (heads
[i
] && heads
[i
][0]) {
747 error("no such remote ref %s", heads
[i
]);
752 if (!ret
&& depth
> 0) {
753 struct cache_time mtime
;
754 char *shallow
= git_path("shallow");
757 mtime
.sec
= st
.st_mtime
;
759 mtime
.usec
= st
.st_mtim
.usec
;
761 if (stat(shallow
, &st
)) {
763 die("shallow file was removed during fetch");
764 } else if (st
.st_mtime
!= mtime
.sec
766 || st
.st_mtim
.usec
!= mtime
.usec
769 die("shallow file was changed during fetch");
771 fd
= hold_lock_file_for_update(&lock
, shallow
, 1);
772 if (!write_shallow_commits(fd
, 0)) {
774 rollback_lock_file(&lock
);
777 commit_lock_file(&lock
);