15 static const char fetch_pack_usage[] =
16 "git-fetch-pack [--all] [-q] [-v] [-k] [--thin] [--exec=upload-pack] [--depth=<n>] [host:]directory <refs>...";
17 static const char *exec = "git-upload-pack";
19 #define COMPLETE (1U << 0)
20 #define COMMON (1U << 1)
21 #define COMMON_REF (1U << 2)
22 #define SEEN (1U << 3)
23 #define POPPED (1U << 4)
26 * After sending this many "have"s if we do not get any new ACK , we
27 * give up traversing our history.
29 #define MAX_IN_VAIN 256
31 static struct commit_list *rev_list;
32 static int non_common_revs, multi_ack, use_thin_pack, use_sideband;
34 static void rev_list_push(struct commit *commit, int mark)
36 if (!(commit->object.flags & mark)) {
37 commit->object.flags |= mark;
39 if (!(commit->object.parsed))
42 insert_by_date(commit, &rev_list);
44 if (!(commit->object.flags & COMMON))
49 static int rev_list_insert_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
51 struct object *o = deref_tag(parse_object(sha1), path, 0);
53 if (o && o->type == OBJ_COMMIT)
54 rev_list_push((struct commit *)o, SEEN);
60 This function marks a rev and its ancestors as common.
61 In some cases, it is desirable to mark only the ancestors (for example
62 when only the server does not yet know that they are common).
65 static void mark_common(struct commit *commit,
66 int ancestors_only, int dont_parse)
68 if (commit != NULL && !(commit->object.flags & COMMON)) {
69 struct object *o = (struct object *)commit;
74 if (!(o->flags & SEEN))
75 rev_list_push(commit, SEEN);
77 struct commit_list *parents;
79 if (!ancestors_only && !(o->flags & POPPED))
81 if (!o->parsed && !dont_parse)
84 for (parents = commit->parents;
86 parents = parents->next)
87 mark_common(parents->item, 0, dont_parse);
93 Get the next rev to send, ignoring the common.
96 static const unsigned char* get_rev(void)
98 struct commit *commit = NULL;
100 while (commit == NULL) {
102 struct commit_list* parents;
104 if (rev_list == NULL || non_common_revs == 0)
107 commit = rev_list->item;
108 if (!(commit->object.parsed))
109 parse_commit(commit);
110 commit->object.flags |= POPPED;
111 if (!(commit->object.flags & COMMON))
114 parents = commit->parents;
116 if (commit->object.flags & COMMON) {
117 /* do not send "have", and ignore ancestors */
119 mark = COMMON | SEEN;
120 } else if (commit->object.flags & COMMON_REF)
121 /* send "have", and ignore ancestors */
122 mark = COMMON | SEEN;
124 /* send "have", also for its ancestors */
128 if (!(parents->item->object.flags & SEEN))
129 rev_list_push(parents->item, mark);
131 mark_common(parents->item, 1, 0);
132 parents = parents->next;
135 rev_list = rev_list->next;
138 return commit->object.sha1;
141 static int find_common(int fd[2], unsigned char *result_sha1,
145 int count = 0, flushes = 0, retval;
146 const unsigned char *sha1;
147 unsigned in_vain = 0;
148 int got_continue = 0;
150 for_each_ref(rev_list_insert_ref, NULL);
153 for ( ; refs ; refs = refs->next) {
154 unsigned char *remote = refs->old_sha1;
158 * If that object is complete (i.e. it is an ancestor of a
159 * local ref), we tell them we have it but do not have to
160 * tell them about its ancestors, which they already know
163 * We use lookup_object here because we are only
164 * interested in the case we *know* the object is
165 * reachable and we have already scanned it.
167 if (((o = lookup_object(remote)) != NULL) &&
168 (o->flags & COMPLETE)) {
173 packet_write(fd[1], "want %s%s%s%s%s%s\n",
175 (multi_ack ? " multi_ack" : ""),
176 (use_sideband == 2 ? " side-band-64k" : ""),
177 (use_sideband == 1 ? " side-band" : ""),
178 (use_thin_pack ? " thin-pack" : ""),
181 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
184 if (is_repository_shallow())
185 write_shallow_commits(fd[1], 1);
187 packet_write(fd[1], "deepen %d", depth);
194 unsigned char sha1[20];
197 while ((len = packet_read_line(fd[0], line, sizeof(line)))) {
198 if (!strncmp("shallow ", line, 8)) {
199 if (get_sha1_hex(line + 8, sha1))
200 die("invalid shallow line: %s", line);
201 /* no need making it shallow if we have it already */
202 if (lookup_object(sha1))
204 register_shallow(sha1);
211 while ((sha1 = get_rev())) {
212 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
214 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
216 if (!(31 & ++count)) {
223 * We keep one window "ahead" of the other side, and
224 * will wait for an ACK only on the next one
230 ack = get_ack(fd[0], result_sha1);
232 fprintf(stderr, "got ack %d %s\n", ack,
233 sha1_to_hex(result_sha1));
239 } else if (ack == 2) {
240 struct commit *commit =
241 lookup_commit(result_sha1);
242 mark_common(commit, 0, 1);
249 if (got_continue && MAX_IN_VAIN < in_vain) {
251 fprintf(stderr, "giving up\n");
257 packet_write(fd[1], "done\n");
259 fprintf(stderr, "done\n");
264 while (flushes || multi_ack) {
265 int ack = get_ack(fd[0], result_sha1);
268 fprintf(stderr, "got ack (%d) %s\n", ack,
269 sha1_to_hex(result_sha1));
280 static struct commit_list *complete;
282 static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
284 struct object *o = parse_object(sha1);
286 while (o && o->type == OBJ_TAG) {
287 struct tag *t = (struct tag *) o;
289 break; /* broken repository */
290 o->flags |= COMPLETE;
291 o = parse_object(t->tagged->sha1);
293 if (o && o->type == OBJ_COMMIT) {
294 struct commit *commit = (struct commit *)o;
295 commit->object.flags |= COMPLETE;
296 insert_by_date(commit, &complete);
301 static void mark_recent_complete_commits(unsigned long cutoff)
303 while (complete && cutoff <= complete->item->date) {
305 fprintf(stderr, "Marking %s as complete\n",
306 sha1_to_hex(complete->item->object.sha1));
307 pop_most_recent_commit(&complete, COMPLETE);
311 static void filter_refs(struct ref **refs, int nr_match, char **match)
313 struct ref **return_refs;
314 struct ref *newlist = NULL;
315 struct ref **newtail = &newlist;
316 struct ref *ref, *next;
317 struct ref *fastarray[32];
319 if (nr_match && !fetch_all) {
320 if (ARRAY_SIZE(fastarray) < nr_match)
321 return_refs = xcalloc(nr_match, sizeof(struct ref *));
323 return_refs = fastarray;
324 memset(return_refs, 0, sizeof(struct ref *) * nr_match);
330 for (ref = *refs; ref; ref = next) {
332 if (!memcmp(ref->name, "refs/", 5) &&
333 check_ref_format(ref->name + 5))
335 else if (fetch_all) {
338 newtail = &ref->next;
342 int order = path_match(ref->name, nr_match, match);
344 return_refs[order-1] = ref;
345 continue; /* we will link it later */
353 for (i = 0; i < nr_match; i++) {
354 ref = return_refs[i];
358 newtail = &ref->next;
361 if (return_refs != fastarray)
367 static int everything_local(struct ref **refs, int nr_match, char **match)
371 unsigned long cutoff = 0;
373 track_object_refs = 0;
374 save_commit_buffer = 0;
376 for (ref = *refs; ref; ref = ref->next) {
379 o = parse_object(ref->old_sha1);
383 /* We already have it -- which may mean that we were
384 * in sync with the other side at some time after
385 * that (it is OK if we guess wrong here).
387 if (o->type == OBJ_COMMIT) {
388 struct commit *commit = (struct commit *)o;
389 if (!cutoff || cutoff < commit->date)
390 cutoff = commit->date;
394 for_each_ref(mark_complete, NULL);
396 mark_recent_complete_commits(cutoff);
399 * Mark all complete remote refs as common refs.
400 * Don't mark them common yet; the server has to be told so first.
402 for (ref = *refs; ref; ref = ref->next) {
403 struct object *o = deref_tag(lookup_object(ref->old_sha1),
406 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
409 if (!(o->flags & SEEN)) {
410 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
412 mark_common((struct commit *)o, 1, 1);
416 filter_refs(refs, nr_match, match);
418 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
419 const unsigned char *remote = ref->old_sha1;
420 unsigned char local[20];
423 o = lookup_object(remote);
424 if (!o || !(o->flags & COMPLETE)) {
429 "want %s (%s)\n", sha1_to_hex(remote),
434 hashcpy(ref->new_sha1, local);
438 "already have %s (%s)\n", sha1_to_hex(remote),
444 static pid_t setup_sideband(int fd[2], int xd[2])
453 /* xd[] is talking with upload-pack; subprocess reads from
454 * xd[0], spits out band#2 to stderr, and feeds us band#1
458 die("fetch-pack: unable to set up pipe");
461 die("fetch-pack: unable to fork off sideband demultiplexer");
467 if (recv_sideband("fetch-pack", xd[0], fd[1], 2))
477 static int get_pack(int xd[2], const char **argv)
483 side_pid = setup_sideband(fd, xd);
486 die("fetch-pack: unable to fork off %s", argv[0]);
492 die("%s exec failed", argv[0]);
496 while (waitpid(pid, &status, 0) < 0) {
498 die("waiting for %s: %s", argv[0], strerror(errno));
500 if (WIFEXITED(status)) {
501 int code = WEXITSTATUS(status);
503 die("%s died with error code %d", argv[0], code);
506 if (WIFSIGNALED(status)) {
507 int sig = WTERMSIG(status);
508 die("%s died of signal %d", argv[0], sig);
510 die("%s died of unnatural causes %d", argv[0], status);
513 static int explode_rx_pack(int xd[2])
515 const char *argv[3] = { "unpack-objects", quiet ? "-q" : NULL, NULL };
516 return get_pack(xd, argv);
519 static int keep_rx_pack(int xd[2])
525 argv[n++] = "index-pack";
526 argv[n++] = "--stdin";
530 argv[n++] = "--fix-thin";
532 int s = sprintf(keep_arg, "--keep=fetch-pack %i on ", getpid());
533 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
534 strcpy(keep_arg + s, "localhost");
535 argv[n++] = keep_arg;
538 return get_pack(xd, argv);
541 static int fetch_pack(int fd[2], int nr_match, char **match)
544 unsigned char sha1[20];
547 get_remote_heads(fd[0], &ref, 0, NULL, 0);
548 if (is_repository_shallow() && !server_supports("shallow"))
549 die("Server does not support shallow clients");
550 if (server_supports("multi_ack")) {
552 fprintf(stderr, "Server supports multi_ack\n");
555 if (server_supports("side-band-64k")) {
557 fprintf(stderr, "Server supports side-band-64k\n");
560 else if (server_supports("side-band")) {
562 fprintf(stderr, "Server supports side-band\n");
567 die("no matching remote head");
569 if (everything_local(&ref, nr_match, match)) {
573 if (find_common(fd, sha1, ref) < 0)
575 /* When cloning, it is not unusual to have
578 fprintf(stderr, "warning: no common commits\n");
580 status = (keep_pack) ? keep_rx_pack(fd) : explode_rx_pack(fd);
582 die("git-fetch-pack: fetch failed.");
587 sha1_to_hex(ref->old_sha1), ref->name);
593 int main(int argc, char **argv)
595 int i, ret, nr_heads;
596 char *dest = NULL, **heads;
600 struct lock_file lock;
602 setup_git_directory();
606 for (i = 1; i < argc; i++) {
610 if (!strncmp("--exec=", arg, 7)) {
614 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
618 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
622 if (!strcmp("--thin", arg)) {
626 if (!strcmp("--all", arg)) {
630 if (!strcmp("-v", arg)) {
634 if (!strncmp("--depth=", arg, 8)) {
635 depth = strtol(arg + 8, NULL, 0);
636 if (stat(git_path("shallow"), &st))
640 usage(fetch_pack_usage);
643 heads = argv + i + 1;
644 nr_heads = argc - i - 1;
648 usage(fetch_pack_usage);
649 if (is_repository_shallow() && depth > 0)
650 die("Deepening of a shallow repository not yet supported!");
651 pid = git_connect(fd, dest, exec);
654 ret = fetch_pack(fd, nr_heads, heads);
657 ret |= finish_connect(pid);
659 if (!ret && nr_heads) {
660 /* If the heads to pull were given, we should have
661 * consumed all of them by matching the remote.
662 * Otherwise, 'git-fetch remote no-such-ref' would
663 * silently succeed without issuing an error.
665 for (i = 0; i < nr_heads; i++)
666 if (heads[i] && heads[i][0]) {
667 error("no such remote ref %s", heads[i]);
672 if (!ret && depth > 0) {
673 struct cache_time mtime;
674 char *shallow = git_path("shallow");
677 mtime.sec = st.st_mtime;
679 mtime.usec = st.st_mtim.usec;
681 if (stat(shallow, &st)) {
683 die("shallow file was removed during fetch");
684 } else if (st.st_mtime != mtime.sec
686 || st.st_mtim.usec != mtime.usec
689 die("shallow file was changed during fetch");
691 fd = hold_lock_file_for_update(&lock, shallow, 1);
692 if (!write_shallow_commits(fd, 0)) {
693 unlink(lock.filename);
694 rollback_lock_file(&lock);
697 commit_lock_file(&lock);