Merge with js/unlink.
[git/mingw.git] / fetch-pack.c
blob10683bee9a9972d7cdd6c45da8933d31986c82f9
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "commit.h"
5 #include "tag.h"
6 #include "exec_cmd.h"
7 #include "sideband.h"
9 static int keep_pack;
10 static int quiet;
11 static int verbose;
12 static int fetch_all;
13 static int depth;
14 static const char fetch_pack_usage[] =
15 "git-fetch-pack [--all] [-q] [-v] [-k] [--thin] [--exec=upload-pack] [--depth=<n>] [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))
39 parse_commit(commit);
41 insert_by_date(commit, &rev_list);
43 if (!(commit->object.flags & COMMON))
44 non_common_revs++;
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);
55 return 0;
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;
70 if (!ancestors_only)
71 o->flags |= COMMON;
73 if (!(o->flags & SEEN))
74 rev_list_push(commit, SEEN);
75 else {
76 struct commit_list *parents;
78 if (!ancestors_only && !(o->flags & POPPED))
79 non_common_revs--;
80 if (!o->parsed && !dont_parse)
81 parse_commit(commit);
83 for (parents = commit->parents;
84 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) {
100 unsigned int mark;
101 struct commit_list* parents;
103 if (rev_list == NULL || non_common_revs == 0)
104 return NULL;
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))
111 non_common_revs--;
113 parents = commit->parents;
115 if (commit->object.flags & COMMON) {
116 /* do not send "have", and ignore ancestors */
117 commit = NULL;
118 mark = COMMON | SEEN;
119 } else if (commit->object.flags & COMMON_REF)
120 /* send "have", and ignore ancestors */
121 mark = COMMON | SEEN;
122 else
123 /* send "have", also for its ancestors */
124 mark = SEEN;
126 while (parents) {
127 if (!(parents->item->object.flags & SEEN))
128 rev_list_push(parents->item, mark);
129 if (mark & COMMON)
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,
141 struct ref *refs)
143 int fetching;
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);
151 fetching = 0;
152 for ( ; refs ; refs = refs->next) {
153 unsigned char *remote = refs->old_sha1;
154 struct object *o;
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
160 * about.
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)) {
168 continue;
171 if (!fetching)
172 packet_write(fd[1], "want %s%s%s%s%s%s\n",
173 sha1_to_hex(remote),
174 (multi_ack ? " multi_ack" : ""),
175 (use_sideband == 2 ? " side-band-64k" : ""),
176 (use_sideband == 1 ? " side-band" : ""),
177 (use_thin_pack ? " thin-pack" : ""),
178 " ofs-delta");
179 else
180 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
181 fetching++;
183 if (is_repository_shallow())
184 write_shallow_commits(fd[1], 1);
185 if (depth > 0)
186 packet_write(fd[1], "deepen %d", depth);
187 packet_flush(fd[1]);
188 if (!fetching)
189 return 1;
191 if (depth > 0) {
192 char line[1024];
193 unsigned char sha1[20];
194 int len;
196 while ((len = packet_read_line(fd[0], line, sizeof(line)))) {
197 if (!strncmp("shallow ", line, 8)) {
198 if (get_sha1_hex(line + 8, sha1))
199 die("invalid shallow line: %s", line);
200 register_shallow(sha1);
201 continue;
203 if (!strncmp("unshallow ", line, 10)) {
204 if (get_sha1_hex(line + 10, sha1))
205 die("invalid unshallow line: %s", line);
206 if (!lookup_object(sha1))
207 die("object not found: %s", line);
208 /* make sure that it is parsed as shallow */
209 parse_object(sha1);
210 if (unregister_shallow(sha1))
211 die("no shallow found: %s", line);
212 continue;
214 die("expected shallow/unshallow, got %s", line);
218 flushes = 0;
219 retval = -1;
220 while ((sha1 = get_rev())) {
221 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
222 if (verbose)
223 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
224 in_vain++;
225 if (!(31 & ++count)) {
226 int ack;
228 packet_flush(fd[1]);
229 flushes++;
232 * We keep one window "ahead" of the other side, and
233 * will wait for an ACK only on the next one
235 if (count == 32)
236 continue;
238 do {
239 ack = get_ack(fd[0], result_sha1);
240 if (verbose && ack)
241 fprintf(stderr, "got ack %d %s\n", ack,
242 sha1_to_hex(result_sha1));
243 if (ack == 1) {
244 flushes = 0;
245 multi_ack = 0;
246 retval = 0;
247 goto done;
248 } else if (ack == 2) {
249 struct commit *commit =
250 lookup_commit(result_sha1);
251 mark_common(commit, 0, 1);
252 retval = 0;
253 in_vain = 0;
254 got_continue = 1;
256 } while (ack);
257 flushes--;
258 if (got_continue && MAX_IN_VAIN < in_vain) {
259 if (verbose)
260 fprintf(stderr, "giving up\n");
261 break; /* give up */
265 done:
266 packet_write(fd[1], "done\n");
267 if (verbose)
268 fprintf(stderr, "done\n");
269 if (retval != 0) {
270 multi_ack = 0;
271 flushes++;
273 while (flushes || multi_ack) {
274 int ack = get_ack(fd[0], result_sha1);
275 if (ack) {
276 if (verbose)
277 fprintf(stderr, "got ack (%d) %s\n", ack,
278 sha1_to_hex(result_sha1));
279 if (ack == 1)
280 return 0;
281 multi_ack = 1;
282 continue;
284 flushes--;
286 return retval;
289 static struct commit_list *complete;
291 static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
293 struct object *o = parse_object(sha1);
295 while (o && o->type == OBJ_TAG) {
296 struct tag *t = (struct tag *) o;
297 if (!t->tagged)
298 break; /* broken repository */
299 o->flags |= COMPLETE;
300 o = parse_object(t->tagged->sha1);
302 if (o && o->type == OBJ_COMMIT) {
303 struct commit *commit = (struct commit *)o;
304 commit->object.flags |= COMPLETE;
305 insert_by_date(commit, &complete);
307 return 0;
310 static void mark_recent_complete_commits(unsigned long cutoff)
312 while (complete && cutoff <= complete->item->date) {
313 if (verbose)
314 fprintf(stderr, "Marking %s as complete\n",
315 sha1_to_hex(complete->item->object.sha1));
316 pop_most_recent_commit(&complete, COMPLETE);
320 static void filter_refs(struct ref **refs, int nr_match, char **match)
322 struct ref **return_refs;
323 struct ref *newlist = NULL;
324 struct ref **newtail = &newlist;
325 struct ref *ref, *next;
326 struct ref *fastarray[32];
328 if (nr_match && !fetch_all) {
329 if (ARRAY_SIZE(fastarray) < nr_match)
330 return_refs = xcalloc(nr_match, sizeof(struct ref *));
331 else {
332 return_refs = fastarray;
333 memset(return_refs, 0, sizeof(struct ref *) * nr_match);
336 else
337 return_refs = NULL;
339 for (ref = *refs; ref; ref = next) {
340 next = ref->next;
341 if (!memcmp(ref->name, "refs/", 5) &&
342 check_ref_format(ref->name + 5))
343 ; /* trash */
344 else if (fetch_all &&
345 (!depth || strncmp(ref->name, "refs/tags/", 10) )) {
346 *newtail = ref;
347 ref->next = NULL;
348 newtail = &ref->next;
349 continue;
351 else {
352 int order = path_match(ref->name, nr_match, match);
353 if (order) {
354 return_refs[order-1] = ref;
355 continue; /* we will link it later */
358 free(ref);
361 if (!fetch_all) {
362 int i;
363 for (i = 0; i < nr_match; i++) {
364 ref = return_refs[i];
365 if (ref) {
366 *newtail = ref;
367 ref->next = NULL;
368 newtail = &ref->next;
371 if (return_refs != fastarray)
372 free(return_refs);
374 *refs = newlist;
377 static int everything_local(struct ref **refs, int nr_match, char **match)
379 struct ref *ref;
380 int retval;
381 unsigned long cutoff = 0;
383 track_object_refs = 0;
384 save_commit_buffer = 0;
386 for (ref = *refs; ref; ref = ref->next) {
387 struct object *o;
389 o = parse_object(ref->old_sha1);
390 if (!o)
391 continue;
393 /* We already have it -- which may mean that we were
394 * in sync with the other side at some time after
395 * that (it is OK if we guess wrong here).
397 if (o->type == OBJ_COMMIT) {
398 struct commit *commit = (struct commit *)o;
399 if (!cutoff || cutoff < commit->date)
400 cutoff = commit->date;
404 if (!depth) {
405 for_each_ref(mark_complete, NULL);
406 if (cutoff)
407 mark_recent_complete_commits(cutoff);
411 * Mark all complete remote refs as common refs.
412 * Don't mark them common yet; the server has to be told so first.
414 for (ref = *refs; ref; ref = ref->next) {
415 struct object *o = deref_tag(lookup_object(ref->old_sha1),
416 NULL, 0);
418 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
419 continue;
421 if (!(o->flags & SEEN)) {
422 rev_list_push((struct commit *)o, COMMON_REF | SEEN);
424 mark_common((struct commit *)o, 1, 1);
428 filter_refs(refs, nr_match, match);
430 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
431 const unsigned char *remote = ref->old_sha1;
432 unsigned char local[20];
433 struct object *o;
435 o = lookup_object(remote);
436 if (!o || !(o->flags & COMPLETE)) {
437 retval = 0;
438 if (!verbose)
439 continue;
440 fprintf(stderr,
441 "want %s (%s)\n", sha1_to_hex(remote),
442 ref->name);
443 continue;
446 hashcpy(ref->new_sha1, local);
447 if (!verbose)
448 continue;
449 fprintf(stderr,
450 "already have %s (%s)\n", sha1_to_hex(remote),
451 ref->name);
453 return retval;
456 static pid_t setup_sideband(int fd[2], int xd[2])
458 pid_t side_pid;
460 if (!use_sideband) {
461 fd[0] = xd[0];
462 fd[1] = xd[1];
463 return 0;
465 /* xd[] is talking with upload-pack; subprocess reads from
466 * xd[0], spits out band#2 to stderr, and feeds us band#1
467 * through our fd[0].
469 if (pipe(fd) < 0)
470 die("fetch-pack: unable to set up pipe");
471 side_pid = fork();
472 if (side_pid < 0)
473 die("fetch-pack: unable to fork off sideband demultiplexer");
474 if (!side_pid) {
475 /* subprocess */
476 close(fd[0]);
477 if (xd[0] != xd[1])
478 close(xd[1]);
479 if (recv_sideband("fetch-pack", xd[0], fd[1], 2))
480 exit(1);
481 exit(0);
483 close(xd[0]);
484 close(fd[1]);
485 fd[1] = xd[1];
486 return side_pid;
489 static int get_pack(int xd[2], const char **argv)
491 int status;
492 pid_t pid, side_pid;
493 int fd[2];
495 side_pid = setup_sideband(fd, xd);
496 pid = spawnv_git_cmd(argv, fd, NULL);
497 if (pid < 0)
498 die("fetch-pack: unable to fork off %s", argv[0]);
499 close(fd[1]);
500 while (waitpid(pid, &status, 0) < 0) {
501 if (errno != EINTR)
502 die("waiting for %s: %s", argv[0], strerror(errno));
504 if (WIFEXITED(status)) {
505 int code = WEXITSTATUS(status);
506 if (code)
507 die("%s died with error code %d", argv[0], code);
508 return 0;
510 if (WIFSIGNALED(status)) {
511 int sig = WTERMSIG(status);
512 die("%s died of signal %d", argv[0], sig);
514 die("%s died of unnatural causes %d", argv[0], status);
517 static int explode_rx_pack(int xd[2])
519 const char *argv[3] = { "unpack-objects", quiet ? "-q" : NULL, NULL };
520 return get_pack(xd, argv);
523 static int keep_rx_pack(int xd[2])
525 const char *argv[6];
526 char keep_arg[256];
527 int n = 0;
529 argv[n++] = "index-pack";
530 argv[n++] = "--stdin";
531 if (!quiet)
532 argv[n++] = "-v";
533 if (use_thin_pack)
534 argv[n++] = "--fix-thin";
535 if (keep_pack > 1) {
536 int s = sprintf(keep_arg, "--keep=fetch-pack %i on ", getpid());
537 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
538 strcpy(keep_arg + s, "localhost");
539 argv[n++] = keep_arg;
541 argv[n] = NULL;
542 return get_pack(xd, argv);
545 static int fetch_pack(int fd[2], int nr_match, char **match)
547 struct ref *ref;
548 unsigned char sha1[20];
549 int status;
551 get_remote_heads(fd[0], &ref, 0, NULL, 0);
552 if (is_repository_shallow() && !server_supports("shallow"))
553 die("Server does not support shallow clients");
554 if (server_supports("multi_ack")) {
555 if (verbose)
556 fprintf(stderr, "Server supports multi_ack\n");
557 multi_ack = 1;
559 #ifndef __MINGW32__
560 if (server_supports("side-band-64k")) {
561 if (verbose)
562 fprintf(stderr, "Server supports side-band-64k\n");
563 use_sideband = 2;
565 else if (server_supports("side-band")) {
566 if (verbose)
567 fprintf(stderr, "Server supports side-band\n");
568 use_sideband = 1;
570 #endif
571 if (!ref) {
572 packet_flush(fd[1]);
573 die("no matching remote head");
575 if (everything_local(&ref, nr_match, match)) {
576 packet_flush(fd[1]);
577 goto all_done;
579 if (find_common(fd, sha1, ref) < 0)
580 if (keep_pack != 1)
581 /* When cloning, it is not unusual to have
582 * no common commit.
584 fprintf(stderr, "warning: no common commits\n");
586 status = (keep_pack) ? keep_rx_pack(fd) : explode_rx_pack(fd);
587 if (status)
588 die("git-fetch-pack: fetch failed.");
590 all_done:
591 while (ref) {
592 printf("%s %s\n",
593 sha1_to_hex(ref->old_sha1), ref->name);
594 ref = ref->next;
596 return 0;
599 static int remove_duplicates(int nr_heads, char **heads)
601 int src, dst;
603 for (src = dst = 0; src < nr_heads; src++) {
604 /* If heads[src] is different from any of
605 * heads[0..dst], push it in.
607 int i;
608 for (i = 0; i < dst; i++) {
609 if (!strcmp(heads[i], heads[src]))
610 break;
612 if (i < dst)
613 continue;
614 if (src != dst)
615 heads[dst] = heads[src];
616 dst++;
618 heads[dst] = 0;
619 return dst;
622 static struct lock_file lock;
624 int main(int argc, char **argv)
626 int i, ret, nr_heads;
627 char *dest = NULL, **heads;
628 int fd[2];
629 pid_t pid;
630 struct stat st;
632 setup_git_directory();
634 nr_heads = 0;
635 heads = NULL;
636 for (i = 1; i < argc; i++) {
637 char *arg = argv[i];
639 if (*arg == '-') {
640 if (!strncmp("--exec=", arg, 7)) {
641 exec = arg + 7;
642 continue;
644 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
645 quiet = 1;
646 continue;
648 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
649 keep_pack++;
650 continue;
652 if (!strcmp("--thin", arg)) {
653 use_thin_pack = 1;
654 continue;
656 if (!strcmp("--all", arg)) {
657 fetch_all = 1;
658 continue;
660 if (!strcmp("-v", arg)) {
661 verbose = 1;
662 continue;
664 if (!strncmp("--depth=", arg, 8)) {
665 depth = strtol(arg + 8, NULL, 0);
666 if (stat(git_path("shallow"), &st))
667 st.st_mtime = 0;
668 continue;
670 usage(fetch_pack_usage);
672 dest = arg;
673 heads = argv + i + 1;
674 nr_heads = argc - i - 1;
675 break;
677 if (!dest)
678 usage(fetch_pack_usage);
679 pid = git_connect(fd, dest, exec);
680 if (pid < 0)
681 return 1;
682 if (heads && nr_heads)
683 nr_heads = remove_duplicates(nr_heads, heads);
684 ret = fetch_pack(fd, nr_heads, heads);
685 close(fd[0]);
686 close(fd[1]);
687 ret |= finish_connect(pid);
689 if (!ret && nr_heads) {
690 /* If the heads to pull were given, we should have
691 * consumed all of them by matching the remote.
692 * Otherwise, 'git-fetch remote no-such-ref' would
693 * silently succeed without issuing an error.
695 for (i = 0; i < nr_heads; i++)
696 if (heads[i] && heads[i][0]) {
697 error("no such remote ref %s", heads[i]);
698 ret = 1;
702 if (!ret && depth > 0) {
703 struct cache_time mtime;
704 char *shallow = git_path("shallow");
705 int fd;
707 mtime.sec = st.st_mtime;
708 #ifdef USE_NSEC
709 mtime.usec = st.st_mtim.usec;
710 #endif
711 if (stat(shallow, &st)) {
712 if (mtime.sec)
713 die("shallow file was removed during fetch");
714 } else if (st.st_mtime != mtime.sec
715 #ifdef USE_NSEC
716 || st.st_mtim.usec != mtime.usec
717 #endif
719 die("shallow file was changed during fetch");
721 fd = hold_lock_file_for_update(&lock, shallow, 1);
722 if (!write_shallow_commits(fd, 0)) {
723 unlink(shallow);
724 rollback_lock_file(&lock);
725 } else {
726 close(fd);
727 commit_lock_file(&lock);
731 return !!ret;