11 static const char fetch_pack_usage[] =
12 "git-fetch-pack [--all] [-q] [-v] [-k] [--thin] [--exec=upload-pack] [host:]directory <refs>...";
13 static const char *exec = "git-upload-pack";
15 #define COMPLETE (1U << 0)
16 #define COMMON (1U << 1)
17 #define COMMON_REF (1U << 2)
18 #define SEEN (1U << 3)
19 #define POPPED (1U << 4)
21 static struct commit_list *rev_list = NULL;
22 static int non_common_revs = 0, multi_ack = 0, use_thin_pack = 0;
24 static void rev_list_push(struct commit *commit, int mark)
26 if (!(commit->object.flags & mark)) {
27 commit->object.flags |= mark;
29 if (!(commit->object.parsed))
32 insert_by_date(commit, &rev_list);
34 if (!(commit->object.flags & COMMON))
39 static int rev_list_insert_ref(const char *path, const unsigned char *sha1)
41 struct object *o = deref_tag(parse_object(sha1), path, 0);
43 if (o && o->type == commit_type)
44 rev_list_push((struct commit *)o, SEEN);
50 This function marks a rev and its ancestors as common.
51 In some cases, it is desirable to mark only the ancestors (for example
52 when only the server does not yet know that they are common).
55 static void mark_common(struct commit *commit,
56 int ancestors_only, int dont_parse)
58 if (commit != NULL && !(commit->object.flags & COMMON)) {
59 struct object *o = (struct object *)commit;
64 if (!(o->flags & SEEN))
65 rev_list_push(commit, SEEN);
67 struct commit_list *parents;
69 if (!ancestors_only && !(o->flags & POPPED))
71 if (!o->parsed && !dont_parse)
74 for (parents = commit->parents;
76 parents = parents->next)
77 mark_common(parents->item, 0, dont_parse);
83 Get the next rev to send, ignoring the common.
86 static const unsigned char* get_rev(void)
88 struct commit *commit = NULL;
90 while (commit == NULL) {
92 struct commit_list* parents;
94 if (rev_list == NULL || non_common_revs == 0)
97 commit = rev_list->item;
98 if (!(commit->object.parsed))
100 commit->object.flags |= POPPED;
101 if (!(commit->object.flags & COMMON))
104 parents = commit->parents;
106 if (commit->object.flags & COMMON) {
107 /* do not send "have", and ignore ancestors */
109 mark = COMMON | SEEN;
110 } else if (commit->object.flags & COMMON_REF)
111 /* send "have", and ignore ancestors */
112 mark = COMMON | SEEN;
114 /* send "have", also for its ancestors */
118 if (!(parents->item->object.flags & SEEN))
119 rev_list_push(parents->item, mark);
121 mark_common(parents->item, 1, 0);
122 parents = parents->next;
125 rev_list = rev_list->next;
128 return commit->object.sha1;
131 static int find_common(int fd[2], unsigned char *result_sha1,
135 int count = 0, flushes = 0, retval;
136 const unsigned char *sha1;
138 for_each_ref(rev_list_insert_ref);
141 for ( ; refs ; refs = refs->next) {
142 unsigned char *remote = refs->old_sha1;
146 * If that object is complete (i.e. it is an ancestor of a
147 * local ref), we tell them we have it but do not have to
148 * tell them about its ancestors, which they already know
151 * We use lookup_object here because we are only
152 * interested in the case we *know* the object is
153 * reachable and we have already scanned it.
155 if (((o = lookup_object(remote)) != NULL) &&
156 (o->flags & COMPLETE)) {
160 packet_write(fd[1], "want %s%s%s\n", sha1_to_hex(remote),
161 (multi_ack ? " multi_ack" : ""),
162 (use_thin_pack ? " thin-pack" : ""));
171 while ((sha1 = get_rev())) {
172 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
174 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
175 if (!(31 & ++count)) {
182 * We keep one window "ahead" of the other side, and
183 * will wait for an ACK only on the next one
189 ack = get_ack(fd[0], result_sha1);
191 fprintf(stderr, "got ack %d %s\n", ack,
192 sha1_to_hex(result_sha1));
198 } else if (ack == 2) {
199 struct commit *commit =
200 lookup_commit(result_sha1);
201 mark_common(commit, 0, 1);
209 packet_write(fd[1], "done\n");
211 fprintf(stderr, "done\n");
216 while (flushes || multi_ack) {
217 int ack = get_ack(fd[0], result_sha1);
220 fprintf(stderr, "got ack (%d) %s\n", ack,
221 sha1_to_hex(result_sha1));
232 static struct commit_list *complete = NULL;
234 static int mark_complete(const char *path, const unsigned char *sha1)
236 struct object *o = parse_object(sha1);
238 while (o && o->type == tag_type) {
239 struct tag *t = (struct tag *) o;
241 break; /* broken repository */
242 o->flags |= COMPLETE;
243 o = parse_object(t->tagged->sha1);
245 if (o && o->type == commit_type) {
246 struct commit *commit = (struct commit *)o;
247 commit->object.flags |= COMPLETE;
248 insert_by_date(commit, &complete);
253 static void mark_recent_complete_commits(unsigned long cutoff)
255 while (complete && cutoff <= complete->item->date) {
257 fprintf(stderr, "Marking %s as complete\n",
258 sha1_to_hex(complete->item->object.sha1));
259 pop_most_recent_commit(&complete, COMPLETE);
263 static void filter_refs(struct ref **refs, int nr_match, char **match)
265 struct ref **return_refs;
266 struct ref *newlist = NULL;
267 struct ref **newtail = &newlist;
268 struct ref *ref, *next;
269 struct ref *fastarray[32];
271 if (nr_match && !fetch_all) {
272 if (ARRAY_SIZE(fastarray) < nr_match)
273 return_refs = xcalloc(nr_match, sizeof(struct ref *));
275 return_refs = fastarray;
276 memset(return_refs, 0, sizeof(struct ref *) * nr_match);
282 for (ref = *refs; ref; ref = next) {
284 if (!memcmp(ref->name, "refs/", 5) &&
285 check_ref_format(ref->name + 5))
287 else if (fetch_all) {
290 newtail = &ref->next;
294 int order = path_match(ref->name, nr_match, match);
296 return_refs[order-1] = ref;
297 continue; /* we will link it later */
305 for (i = 0; i < nr_match; i++) {
306 ref = return_refs[i];
310 newtail = &ref->next;
313 if (return_refs != fastarray)
319 static int everything_local(struct ref **refs, int nr_match, char **match)
323 unsigned long cutoff = 0;
325 track_object_refs = 0;
326 save_commit_buffer = 0;
328 for (ref = *refs; ref; ref = ref->next) {
331 o = parse_object(ref->old_sha1);
335 /* We already have it -- which may mean that we were
336 * in sync with the other side at some time after
337 * that (it is OK if we guess wrong here).
339 if (o->type == commit_type) {
340 struct commit *commit = (struct commit *)o;
341 if (!cutoff || cutoff < commit->date)
342 cutoff = commit->date;
346 for_each_ref(mark_complete);
348 mark_recent_complete_commits(cutoff);
351 * Mark all complete remote refs as common refs.
352 * Don't mark them common yet; the server has to be told so first.
354 for (ref = *refs; ref; ref = ref->next) {
355 struct object *o = deref_tag(lookup_object(ref->old_sha1),
358 if (!o || o->type != commit_type || !(o->flags & COMPLETE))
361 if (!(o->flags & SEEN)) {
362 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
364 mark_common((struct commit *)o, 1, 1);
368 filter_refs(refs, nr_match, match);
370 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
371 const unsigned char *remote = ref->old_sha1;
372 unsigned char local[20];
375 o = lookup_object(remote);
376 if (!o || !(o->flags & COMPLETE)) {
381 "want %s (%s)\n", sha1_to_hex(remote),
386 memcpy(ref->new_sha1, local, 20);
390 "already have %s (%s)\n", sha1_to_hex(remote),
396 static int fetch_pack(int fd[2], int nr_match, char **match)
399 unsigned char sha1[20];
402 get_remote_heads(fd[0], &ref, 0, NULL, 0);
403 if (server_supports("multi_ack")) {
405 fprintf(stderr, "Server supports multi_ack\n");
410 die("no matching remote head");
412 if (everything_local(&ref, nr_match, match)) {
416 if (find_common(fd, sha1, ref) < 0)
418 /* When cloning, it is not unusual to have
421 fprintf(stderr, "warning: no common commits\n");
424 status = receive_keep_pack(fd, "git-fetch-pack", quiet);
426 status = receive_unpack_pack(fd, "git-fetch-pack", quiet);
429 die("git-fetch-pack: fetch failed.");
434 sha1_to_hex(ref->old_sha1), ref->name);
440 int main(int argc, char **argv)
442 int i, ret, nr_heads;
443 char *dest = NULL, **heads;
447 setup_git_directory();
451 for (i = 1; i < argc; i++) {
455 if (!strncmp("--exec=", arg, 7)) {
459 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
463 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
467 if (!strcmp("--thin", arg)) {
471 if (!strcmp("--all", arg)) {
475 if (!strcmp("-v", arg)) {
479 usage(fetch_pack_usage);
482 heads = argv + i + 1;
483 nr_heads = argc - i - 1;
487 usage(fetch_pack_usage);
490 pid = git_connect(fd, dest, exec);
493 ret = fetch_pack(fd, nr_heads, heads);
498 if (!ret && nr_heads) {
499 /* If the heads to pull were given, we should have
500 * consumed all of them by matching the remote.
501 * Otherwise, 'git-fetch remote no-such-ref' would
502 * silently succeed without issuing an error.
504 for (i = 0; i < nr_heads; i++)
505 if (heads[i] && heads[i][0]) {
506 error("no such remote ref %s", heads[i]);