14 static const char fetch_pack_usage[] =
15 "git-fetch-pack [--all] [-q] [-v] [-k] [--thin] [--exec=upload-pack] [host:]directory <refs>...";
16 static const char *exec = "git-upload-pack";
18 #define COMPLETE (1U << 0)
19 #define COMMON (1U << 1)
20 #define COMMON_REF (1U << 2)
21 #define SEEN (1U << 3)
22 #define POPPED (1U << 4)
25 * After sending this many "have"s if we do not get any new ACK , we
26 * give up traversing our history.
28 #define MAX_IN_VAIN 256
30 static struct commit_list *rev_list;
31 static int non_common_revs, multi_ack, use_thin_pack, use_sideband;
33 static void rev_list_push(struct commit *commit, int mark)
35 if (!(commit->object.flags & mark)) {
36 commit->object.flags |= mark;
38 if (!(commit->object.parsed))
41 insert_by_date(commit, &rev_list);
43 if (!(commit->object.flags & COMMON))
48 static int rev_list_insert_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
50 struct object *o = deref_tag(parse_object(sha1), path, 0);
52 if (o && o->type == OBJ_COMMIT)
53 rev_list_push((struct commit *)o, SEEN);
59 This function marks a rev and its ancestors as common.
60 In some cases, it is desirable to mark only the ancestors (for example
61 when only the server does not yet know that they are common).
64 static void mark_common(struct commit *commit,
65 int ancestors_only, int dont_parse)
67 if (commit != NULL && !(commit->object.flags & COMMON)) {
68 struct object *o = (struct object *)commit;
73 if (!(o->flags & SEEN))
74 rev_list_push(commit, SEEN);
76 struct commit_list *parents;
78 if (!ancestors_only && !(o->flags & POPPED))
80 if (!o->parsed && !dont_parse)
83 for (parents = commit->parents;
85 parents = parents->next)
86 mark_common(parents->item, 0, dont_parse);
92 Get the next rev to send, ignoring the common.
95 static const unsigned char* get_rev(void)
97 struct commit *commit = NULL;
99 while (commit == NULL) {
101 struct commit_list* parents;
103 if (rev_list == NULL || non_common_revs == 0)
106 commit = rev_list->item;
107 if (!(commit->object.parsed))
108 parse_commit(commit);
109 commit->object.flags |= POPPED;
110 if (!(commit->object.flags & COMMON))
113 parents = commit->parents;
115 if (commit->object.flags & COMMON) {
116 /* do not send "have", and ignore ancestors */
118 mark = COMMON | SEEN;
119 } else if (commit->object.flags & COMMON_REF)
120 /* send "have", and ignore ancestors */
121 mark = COMMON | SEEN;
123 /* send "have", also for its ancestors */
127 if (!(parents->item->object.flags & SEEN))
128 rev_list_push(parents->item, mark);
130 mark_common(parents->item, 1, 0);
131 parents = parents->next;
134 rev_list = rev_list->next;
137 return commit->object.sha1;
140 static int find_common(int fd[2], unsigned char *result_sha1,
144 int count = 0, flushes = 0, retval;
145 const unsigned char *sha1;
146 unsigned in_vain = 0;
147 int got_continue = 0;
149 for_each_ref(rev_list_insert_ref, NULL);
152 for ( ; refs ; refs = refs->next) {
153 unsigned char *remote = refs->old_sha1;
157 * If that object is complete (i.e. it is an ancestor of a
158 * local ref), we tell them we have it but do not have to
159 * tell them about its ancestors, which they already know
162 * We use lookup_object here because we are only
163 * interested in the case we *know* the object is
164 * reachable and we have already scanned it.
166 if (((o = lookup_object(remote)) != NULL) &&
167 (o->flags & COMPLETE)) {
172 packet_write(fd[1], "want %s%s%s%s%s%s\n",
174 (multi_ack ? " multi_ack" : ""),
175 (use_sideband == 2 ? " side-band-64k" : ""),
176 (use_sideband == 1 ? " side-band" : ""),
177 (use_thin_pack ? " thin-pack" : ""),
180 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
189 while ((sha1 = get_rev())) {
190 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
192 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
194 if (!(31 & ++count)) {
201 * We keep one window "ahead" of the other side, and
202 * will wait for an ACK only on the next one
208 ack = get_ack(fd[0], result_sha1);
210 fprintf(stderr, "got ack %d %s\n", ack,
211 sha1_to_hex(result_sha1));
217 } else if (ack == 2) {
218 struct commit *commit =
219 lookup_commit(result_sha1);
220 mark_common(commit, 0, 1);
227 if (got_continue && MAX_IN_VAIN < in_vain) {
229 fprintf(stderr, "giving up\n");
235 packet_write(fd[1], "done\n");
237 fprintf(stderr, "done\n");
242 while (flushes || multi_ack) {
243 int ack = get_ack(fd[0], result_sha1);
246 fprintf(stderr, "got ack (%d) %s\n", ack,
247 sha1_to_hex(result_sha1));
258 static struct commit_list *complete;
260 static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
262 struct object *o = parse_object(sha1);
264 while (o && o->type == OBJ_TAG) {
265 struct tag *t = (struct tag *) o;
267 break; /* broken repository */
268 o->flags |= COMPLETE;
269 o = parse_object(t->tagged->sha1);
271 if (o && o->type == OBJ_COMMIT) {
272 struct commit *commit = (struct commit *)o;
273 commit->object.flags |= COMPLETE;
274 insert_by_date(commit, &complete);
279 static void mark_recent_complete_commits(unsigned long cutoff)
281 while (complete && cutoff <= complete->item->date) {
283 fprintf(stderr, "Marking %s as complete\n",
284 sha1_to_hex(complete->item->object.sha1));
285 pop_most_recent_commit(&complete, COMPLETE);
289 static void filter_refs(struct ref **refs, int nr_match, char **match)
291 struct ref **return_refs;
292 struct ref *newlist = NULL;
293 struct ref **newtail = &newlist;
294 struct ref *ref, *next;
295 struct ref *fastarray[32];
297 if (nr_match && !fetch_all) {
298 if (ARRAY_SIZE(fastarray) < nr_match)
299 return_refs = xcalloc(nr_match, sizeof(struct ref *));
301 return_refs = fastarray;
302 memset(return_refs, 0, sizeof(struct ref *) * nr_match);
308 for (ref = *refs; ref; ref = next) {
310 if (!memcmp(ref->name, "refs/", 5) &&
311 check_ref_format(ref->name + 5))
313 else if (fetch_all) {
316 newtail = &ref->next;
320 int order = path_match(ref->name, nr_match, match);
322 return_refs[order-1] = ref;
323 continue; /* we will link it later */
331 for (i = 0; i < nr_match; i++) {
332 ref = return_refs[i];
336 newtail = &ref->next;
339 if (return_refs != fastarray)
345 static int everything_local(struct ref **refs, int nr_match, char **match)
349 unsigned long cutoff = 0;
351 track_object_refs = 0;
352 save_commit_buffer = 0;
354 for (ref = *refs; ref; ref = ref->next) {
357 o = parse_object(ref->old_sha1);
361 /* We already have it -- which may mean that we were
362 * in sync with the other side at some time after
363 * that (it is OK if we guess wrong here).
365 if (o->type == OBJ_COMMIT) {
366 struct commit *commit = (struct commit *)o;
367 if (!cutoff || cutoff < commit->date)
368 cutoff = commit->date;
372 for_each_ref(mark_complete, NULL);
374 mark_recent_complete_commits(cutoff);
377 * Mark all complete remote refs as common refs.
378 * Don't mark them common yet; the server has to be told so first.
380 for (ref = *refs; ref; ref = ref->next) {
381 struct object *o = deref_tag(lookup_object(ref->old_sha1),
384 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
387 if (!(o->flags & SEEN)) {
388 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
390 mark_common((struct commit *)o, 1, 1);
394 filter_refs(refs, nr_match, match);
396 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
397 const unsigned char *remote = ref->old_sha1;
398 unsigned char local[20];
401 o = lookup_object(remote);
402 if (!o || !(o->flags & COMPLETE)) {
407 "want %s (%s)\n", sha1_to_hex(remote),
412 hashcpy(ref->new_sha1, local);
416 "already have %s (%s)\n", sha1_to_hex(remote),
422 static pid_t setup_sideband(int fd[2], int xd[2])
431 /* xd[] is talking with upload-pack; subprocess reads from
432 * xd[0], spits out band#2 to stderr, and feeds us band#1
436 die("fetch-pack: unable to set up pipe");
439 die("fetch-pack: unable to fork off sideband demultiplexer");
445 if (recv_sideband("fetch-pack", xd[0], fd[1], 2))
455 static int get_pack(int xd[2], const char **argv)
461 side_pid = setup_sideband(fd, xd);
464 die("fetch-pack: unable to fork off %s", argv[0]);
470 die("%s exec failed", argv[0]);
474 while (waitpid(pid, &status, 0) < 0) {
476 die("waiting for %s: %s", argv[0], strerror(errno));
478 if (WIFEXITED(status)) {
479 int code = WEXITSTATUS(status);
481 die("%s died with error code %d", argv[0], code);
484 if (WIFSIGNALED(status)) {
485 int sig = WTERMSIG(status);
486 die("%s died of signal %d", argv[0], sig);
488 die("%s died of unnatural causes %d", argv[0], status);
491 static int explode_rx_pack(int xd[2])
493 const char *argv[3] = { "unpack-objects", quiet ? "-q" : NULL, NULL };
494 return get_pack(xd, argv);
497 static int keep_rx_pack(int xd[2])
503 argv[n++] = "index-pack";
504 argv[n++] = "--stdin";
508 argv[n++] = "--fix-thin";
510 int s = sprintf(keep_arg, "--keep=fetch-pack %i on ", getpid());
511 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
512 strcpy(keep_arg + s, "localhost");
513 argv[n++] = keep_arg;
516 return get_pack(xd, argv);
519 static int fetch_pack(int fd[2], int nr_match, char **match)
522 unsigned char sha1[20];
525 get_remote_heads(fd[0], &ref, 0, NULL, 0);
526 if (server_supports("multi_ack")) {
528 fprintf(stderr, "Server supports multi_ack\n");
531 if (server_supports("side-band-64k")) {
533 fprintf(stderr, "Server supports side-band-64k\n");
536 else if (server_supports("side-band")) {
538 fprintf(stderr, "Server supports side-band\n");
543 die("no matching remote head");
545 if (everything_local(&ref, nr_match, match)) {
549 if (find_common(fd, sha1, ref) < 0)
551 /* When cloning, it is not unusual to have
554 fprintf(stderr, "warning: no common commits\n");
556 status = (keep_pack) ? keep_rx_pack(fd) : explode_rx_pack(fd);
558 die("git-fetch-pack: fetch failed.");
563 sha1_to_hex(ref->old_sha1), ref->name);
569 static int remove_duplicates(int nr_heads, char **heads)
573 for (src = dst = 0; src < nr_heads; src++) {
574 /* If heads[src] is different from any of
575 * heads[0..dst], push it in.
578 for (i = 0; i < dst; i++) {
579 if (!strcmp(heads[i], heads[src]))
585 heads[dst] = heads[src];
592 int main(int argc, char **argv)
594 int i, ret, nr_heads;
595 char *dest = NULL, **heads;
599 setup_git_directory();
603 for (i = 1; i < argc; i++) {
607 if (!strncmp("--exec=", arg, 7)) {
611 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
615 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
619 if (!strcmp("--thin", arg)) {
623 if (!strcmp("--all", arg)) {
627 if (!strcmp("-v", arg)) {
631 usage(fetch_pack_usage);
634 heads = argv + i + 1;
635 nr_heads = argc - i - 1;
639 usage(fetch_pack_usage);
640 pid = git_connect(fd, dest, exec);
643 if (heads && nr_heads)
644 nr_heads = remove_duplicates(nr_heads, heads);
645 ret = fetch_pack(fd, nr_heads, heads);
648 ret |= finish_connect(pid);
650 if (!ret && nr_heads) {
651 /* If the heads to pull were given, we should have
652 * consumed all of them by matching the remote.
653 * Otherwise, 'git-fetch remote no-such-ref' would
654 * silently succeed without issuing an error.
656 for (i = 0; i < nr_heads; i++)
657 if (heads[i] && heads[i][0]) {
658 error("no such remote ref %s", heads[i]);