16 static const char fetch_pack_usage
[] =
17 "git-fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--upload-pack=<git-upload-pack>] [--depth=<n>] [-v] [<host>:]<directory> [<refs>...]";
18 static const char *uploadpack
= "git-upload-pack";
20 #define COMPLETE (1U << 0)
21 #define COMMON (1U << 1)
22 #define COMMON_REF (1U << 2)
23 #define SEEN (1U << 3)
24 #define POPPED (1U << 4)
27 * After sending this many "have"s if we do not get any new ACK , we
28 * give up traversing our history.
30 #define MAX_IN_VAIN 256
32 static struct commit_list
*rev_list
;
33 static int non_common_revs
, multi_ack
, use_thin_pack
, use_sideband
;
35 static void rev_list_push(struct commit
*commit
, int mark
)
37 if (!(commit
->object
.flags
& mark
)) {
38 commit
->object
.flags
|= mark
;
40 if (!(commit
->object
.parsed
))
43 insert_by_date(commit
, &rev_list
);
45 if (!(commit
->object
.flags
& COMMON
))
50 static int rev_list_insert_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
52 struct object
*o
= deref_tag(parse_object(sha1
), path
, 0);
54 if (o
&& o
->type
== OBJ_COMMIT
)
55 rev_list_push((struct commit
*)o
, SEEN
);
61 This function marks a rev and its ancestors as common.
62 In some cases, it is desirable to mark only the ancestors (for example
63 when only the server does not yet know that they are common).
66 static void mark_common(struct commit
*commit
,
67 int ancestors_only
, int dont_parse
)
69 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
70 struct object
*o
= (struct object
*)commit
;
75 if (!(o
->flags
& SEEN
))
76 rev_list_push(commit
, SEEN
);
78 struct commit_list
*parents
;
80 if (!ancestors_only
&& !(o
->flags
& POPPED
))
82 if (!o
->parsed
&& !dont_parse
)
85 for (parents
= commit
->parents
;
87 parents
= parents
->next
)
88 mark_common(parents
->item
, 0, dont_parse
);
94 Get the next rev to send, ignoring the common.
97 static const unsigned char* get_rev(void)
99 struct commit
*commit
= NULL
;
101 while (commit
== NULL
) {
103 struct commit_list
* parents
;
105 if (rev_list
== NULL
|| non_common_revs
== 0)
108 commit
= rev_list
->item
;
109 if (!(commit
->object
.parsed
))
110 parse_commit(commit
);
111 commit
->object
.flags
|= POPPED
;
112 if (!(commit
->object
.flags
& COMMON
))
115 parents
= commit
->parents
;
117 if (commit
->object
.flags
& COMMON
) {
118 /* do not send "have", and ignore ancestors */
120 mark
= COMMON
| SEEN
;
121 } else if (commit
->object
.flags
& COMMON_REF
)
122 /* send "have", and ignore ancestors */
123 mark
= COMMON
| SEEN
;
125 /* send "have", also for its ancestors */
129 if (!(parents
->item
->object
.flags
& SEEN
))
130 rev_list_push(parents
->item
, mark
);
132 mark_common(parents
->item
, 1, 0);
133 parents
= parents
->next
;
136 rev_list
= rev_list
->next
;
139 return commit
->object
.sha1
;
142 static int find_common(int fd
[2], unsigned char *result_sha1
,
146 int count
= 0, flushes
= 0, retval
;
147 const unsigned char *sha1
;
148 unsigned in_vain
= 0;
149 int got_continue
= 0;
151 for_each_ref(rev_list_insert_ref
, NULL
);
154 for ( ; refs
; refs
= refs
->next
) {
155 unsigned char *remote
= refs
->old_sha1
;
159 * If that object is complete (i.e. it is an ancestor of a
160 * local ref), we tell them we have it but do not have to
161 * tell them about its ancestors, which they already know
164 * We use lookup_object here because we are only
165 * interested in the case we *know* the object is
166 * reachable and we have already scanned it.
168 if (((o
= lookup_object(remote
)) != NULL
) &&
169 (o
->flags
& COMPLETE
)) {
174 packet_write(fd
[1], "want %s%s%s%s%s%s\n",
176 (multi_ack
? " multi_ack" : ""),
177 (use_sideband
== 2 ? " side-band-64k" : ""),
178 (use_sideband
== 1 ? " side-band" : ""),
179 (use_thin_pack
? " thin-pack" : ""),
182 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
185 if (is_repository_shallow())
186 write_shallow_commits(fd
[1], 1);
188 packet_write(fd
[1], "deepen %d", depth
);
195 unsigned char sha1
[20];
198 while ((len
= packet_read_line(fd
[0], line
, sizeof(line
)))) {
199 if (!strncmp("shallow ", line
, 8)) {
200 if (get_sha1_hex(line
+ 8, sha1
))
201 die("invalid shallow line: %s", line
);
202 register_shallow(sha1
);
205 if (!strncmp("unshallow ", line
, 10)) {
206 if (get_sha1_hex(line
+ 10, sha1
))
207 die("invalid unshallow line: %s", line
);
208 if (!lookup_object(sha1
))
209 die("object not found: %s", line
);
210 /* make sure that it is parsed as shallow */
212 if (unregister_shallow(sha1
))
213 die("no shallow found: %s", line
);
216 die("expected shallow/unshallow, got %s", line
);
222 while ((sha1
= get_rev())) {
223 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
225 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
227 if (!(31 & ++count
)) {
234 * We keep one window "ahead" of the other side, and
235 * will wait for an ACK only on the next one
241 ack
= get_ack(fd
[0], result_sha1
);
243 fprintf(stderr
, "got ack %d %s\n", ack
,
244 sha1_to_hex(result_sha1
));
250 } else if (ack
== 2) {
251 struct commit
*commit
=
252 lookup_commit(result_sha1
);
253 mark_common(commit
, 0, 1);
260 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
262 fprintf(stderr
, "giving up\n");
268 packet_write(fd
[1], "done\n");
270 fprintf(stderr
, "done\n");
275 while (flushes
|| multi_ack
) {
276 int ack
= get_ack(fd
[0], result_sha1
);
279 fprintf(stderr
, "got ack (%d) %s\n", ack
,
280 sha1_to_hex(result_sha1
));
291 static struct commit_list
*complete
;
293 static int mark_complete(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
295 struct object
*o
= parse_object(sha1
);
297 while (o
&& o
->type
== OBJ_TAG
) {
298 struct tag
*t
= (struct tag
*) o
;
300 break; /* broken repository */
301 o
->flags
|= COMPLETE
;
302 o
= parse_object(t
->tagged
->sha1
);
304 if (o
&& o
->type
== OBJ_COMMIT
) {
305 struct commit
*commit
= (struct commit
*)o
;
306 commit
->object
.flags
|= COMPLETE
;
307 insert_by_date(commit
, &complete
);
312 static void mark_recent_complete_commits(unsigned long cutoff
)
314 while (complete
&& cutoff
<= complete
->item
->date
) {
316 fprintf(stderr
, "Marking %s as complete\n",
317 sha1_to_hex(complete
->item
->object
.sha1
));
318 pop_most_recent_commit(&complete
, COMPLETE
);
322 static void filter_refs(struct ref
**refs
, int nr_match
, char **match
)
324 struct ref
**return_refs
;
325 struct ref
*newlist
= NULL
;
326 struct ref
**newtail
= &newlist
;
327 struct ref
*ref
, *next
;
328 struct ref
*fastarray
[32];
330 if (nr_match
&& !fetch_all
) {
331 if (ARRAY_SIZE(fastarray
) < nr_match
)
332 return_refs
= xcalloc(nr_match
, sizeof(struct ref
*));
334 return_refs
= fastarray
;
335 memset(return_refs
, 0, sizeof(struct ref
*) * nr_match
);
341 for (ref
= *refs
; ref
; ref
= next
) {
343 if (!memcmp(ref
->name
, "refs/", 5) &&
344 check_ref_format(ref
->name
+ 5))
346 else if (fetch_all
&&
347 (!depth
|| strncmp(ref
->name
, "refs/tags/", 10) )) {
350 newtail
= &ref
->next
;
354 int order
= path_match(ref
->name
, nr_match
, match
);
356 return_refs
[order
-1] = ref
;
357 continue; /* we will link it later */
365 for (i
= 0; i
< nr_match
; i
++) {
366 ref
= return_refs
[i
];
370 newtail
= &ref
->next
;
373 if (return_refs
!= fastarray
)
379 static int everything_local(struct ref
**refs
, int nr_match
, char **match
)
383 unsigned long cutoff
= 0;
385 track_object_refs
= 0;
386 save_commit_buffer
= 0;
388 for (ref
= *refs
; ref
; ref
= ref
->next
) {
391 o
= parse_object(ref
->old_sha1
);
395 /* We already have it -- which may mean that we were
396 * in sync with the other side at some time after
397 * that (it is OK if we guess wrong here).
399 if (o
->type
== OBJ_COMMIT
) {
400 struct commit
*commit
= (struct commit
*)o
;
401 if (!cutoff
|| cutoff
< commit
->date
)
402 cutoff
= commit
->date
;
407 for_each_ref(mark_complete
, NULL
);
409 mark_recent_complete_commits(cutoff
);
413 * Mark all complete remote refs as common refs.
414 * Don't mark them common yet; the server has to be told so first.
416 for (ref
= *refs
; ref
; ref
= ref
->next
) {
417 struct object
*o
= deref_tag(lookup_object(ref
->old_sha1
),
420 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
423 if (!(o
->flags
& SEEN
)) {
424 rev_list_push((struct commit
*)o
, COMMON_REF
| SEEN
);
426 mark_common((struct commit
*)o
, 1, 1);
430 filter_refs(refs
, nr_match
, match
);
432 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
433 const unsigned char *remote
= ref
->old_sha1
;
434 unsigned char local
[20];
437 o
= lookup_object(remote
);
438 if (!o
|| !(o
->flags
& COMPLETE
)) {
443 "want %s (%s)\n", sha1_to_hex(remote
),
448 hashcpy(ref
->new_sha1
, local
);
452 "already have %s (%s)\n", sha1_to_hex(remote
),
458 static pid_t
setup_sideband(int fd
[2], int xd
[2])
467 /* xd[] is talking with upload-pack; subprocess reads from
468 * xd[0], spits out band#2 to stderr, and feeds us band#1
472 die("fetch-pack: unable to set up pipe");
475 die("fetch-pack: unable to fork off sideband demultiplexer");
481 if (recv_sideband("fetch-pack", xd
[0], fd
[1], 2))
491 static int get_pack(int xd
[2])
496 const char *argv
[20];
500 int do_keep
= keep_pack
;
502 side_pid
= setup_sideband(fd
, xd
);
507 struct pack_header header
;
509 if (read_pack_header(fd
[0], &header
))
510 die("protocol error: bad pack header");
511 snprintf(hdr_arg
, sizeof(hdr_arg
), "--pack_header=%u,%u",
512 ntohl(header
.hdr_version
), ntohl(header
.hdr_entries
));
513 if (ntohl(header
.hdr_entries
) < keep_auto
)
520 *av
++ = "index-pack";
525 *av
++ = "--fix-thin";
526 if (keep_pack
> 1 || keep_auto
) {
527 int s
= sprintf(keep_arg
,
528 "--keep=fetch-pack %d on ", getpid());
529 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
530 strcpy(keep_arg
+ s
, "localhost");
535 *av
++ = "unpack-objects";
545 die("fetch-pack: unable to fork off %s", argv
[0]);
551 die("%s exec failed", argv
[0]);
555 while (waitpid(pid
, &status
, 0) < 0) {
557 die("waiting for %s: %s", argv
[0], strerror(errno
));
559 if (WIFEXITED(status
)) {
560 int code
= WEXITSTATUS(status
);
562 die("%s died with error code %d", argv
[0], code
);
565 if (WIFSIGNALED(status
)) {
566 int sig
= WTERMSIG(status
);
567 die("%s died of signal %d", argv
[0], sig
);
569 die("%s died of unnatural causes %d", argv
[0], status
);
572 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
575 unsigned char sha1
[20];
577 get_remote_heads(fd
[0], &ref
, 0, NULL
, 0);
578 if (is_repository_shallow() && !server_supports("shallow"))
579 die("Server does not support shallow clients");
580 if (server_supports("multi_ack")) {
582 fprintf(stderr
, "Server supports multi_ack\n");
585 if (server_supports("side-band-64k")) {
587 fprintf(stderr
, "Server supports side-band-64k\n");
590 else if (server_supports("side-band")) {
592 fprintf(stderr
, "Server supports side-band\n");
597 die("no matching remote head");
599 if (everything_local(&ref
, nr_match
, match
)) {
603 if (find_common(fd
, sha1
, ref
) < 0)
605 /* When cloning, it is not unusual to have
608 fprintf(stderr
, "warning: no common commits\n");
611 die("git-fetch-pack: fetch failed.");
616 sha1_to_hex(ref
->old_sha1
), ref
->name
);
622 static int remove_duplicates(int nr_heads
, char **heads
)
626 for (src
= dst
= 0; src
< nr_heads
; src
++) {
627 /* If heads[src] is different from any of
628 * heads[0..dst], push it in.
631 for (i
= 0; i
< dst
; i
++) {
632 if (!strcmp(heads
[i
], heads
[src
]))
638 heads
[dst
] = heads
[src
];
645 static struct lock_file lock
;
647 int main(int argc
, char **argv
)
649 int i
, ret
, nr_heads
;
650 char *dest
= NULL
, **heads
;
655 setup_git_directory();
659 for (i
= 1; i
< argc
; i
++) {
663 if (!strncmp("--upload-pack=", arg
, 14)) {
664 uploadpack
= arg
+ 14;
667 if (!strncmp("--exec=", arg
, 7)) {
668 uploadpack
= arg
+ 7;
671 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
675 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
679 if (!strcmp("--keep-auto", arg
)) {
683 if (!strncmp("--keep-auto=", arg
, 12)) {
684 keep_auto
= strtoul(arg
+ 12, NULL
, 0);
689 if (!strcmp("--thin", arg
)) {
693 if (!strcmp("--all", arg
)) {
697 if (!strcmp("-v", arg
)) {
701 if (!strncmp("--depth=", arg
, 8)) {
702 depth
= strtol(arg
+ 8, NULL
, 0);
703 if (stat(git_path("shallow"), &st
))
707 usage(fetch_pack_usage
);
710 heads
= argv
+ i
+ 1;
711 nr_heads
= argc
- i
- 1;
715 usage(fetch_pack_usage
);
716 pid
= git_connect(fd
, dest
, uploadpack
);
719 if (heads
&& nr_heads
)
720 nr_heads
= remove_duplicates(nr_heads
, heads
);
721 ret
= fetch_pack(fd
, nr_heads
, heads
);
724 ret
|= finish_connect(pid
);
726 if (!ret
&& nr_heads
) {
727 /* If the heads to pull were given, we should have
728 * consumed all of them by matching the remote.
729 * Otherwise, 'git-fetch remote no-such-ref' would
730 * silently succeed without issuing an error.
732 for (i
= 0; i
< nr_heads
; i
++)
733 if (heads
[i
] && heads
[i
][0]) {
734 error("no such remote ref %s", heads
[i
]);
739 if (!ret
&& depth
> 0) {
740 struct cache_time mtime
;
741 char *shallow
= git_path("shallow");
744 mtime
.sec
= st
.st_mtime
;
746 mtime
.usec
= st
.st_mtim
.usec
;
748 if (stat(shallow
, &st
)) {
750 die("shallow file was removed during fetch");
751 } else if (st
.st_mtime
!= mtime
.sec
753 || st
.st_mtim
.usec
!= mtime
.usec
756 die("shallow file was changed during fetch");
758 fd
= hold_lock_file_for_update(&lock
, shallow
, 1);
759 if (!write_shallow_commits(fd
, 0)) {
761 rollback_lock_file(&lock
);
764 commit_lock_file(&lock
);