9 #include "fetch-pack.h"
10 #include "run-command.h"
12 static int transfer_unpack_limit
= -1;
13 static int fetch_unpack_limit
= -1;
14 static int unpack_limit
= 100;
15 static struct fetch_pack_args args
;
17 static const char fetch_pack_usage
[] =
18 "git-fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]";
19 static const char *uploadpack
= "git-upload-pack";
21 #define COMPLETE (1U << 0)
22 #define COMMON (1U << 1)
23 #define COMMON_REF (1U << 2)
24 #define SEEN (1U << 3)
25 #define POPPED (1U << 4)
28 * After sending this many "have"s if we do not get any new ACK , we
29 * give up traversing our history.
31 #define MAX_IN_VAIN 256
33 static struct commit_list
*rev_list
;
34 static int non_common_revs
, multi_ack
, use_thin_pack
, use_sideband
;
36 static void rev_list_push(struct commit
*commit
, int mark
)
38 if (!(commit
->object
.flags
& mark
)) {
39 commit
->object
.flags
|= mark
;
41 if (!(commit
->object
.parsed
))
44 insert_by_date(commit
, &rev_list
);
46 if (!(commit
->object
.flags
& COMMON
))
51 static int rev_list_insert_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
53 struct object
*o
= deref_tag(parse_object(sha1
), path
, 0);
55 if (o
&& o
->type
== OBJ_COMMIT
)
56 rev_list_push((struct commit
*)o
, SEEN
);
62 This function marks a rev and its ancestors as common.
63 In some cases, it is desirable to mark only the ancestors (for example
64 when only the server does not yet know that they are common).
67 static void mark_common(struct commit
*commit
,
68 int ancestors_only
, int dont_parse
)
70 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
71 struct object
*o
= (struct object
*)commit
;
76 if (!(o
->flags
& SEEN
))
77 rev_list_push(commit
, SEEN
);
79 struct commit_list
*parents
;
81 if (!ancestors_only
&& !(o
->flags
& POPPED
))
83 if (!o
->parsed
&& !dont_parse
)
86 for (parents
= commit
->parents
;
88 parents
= parents
->next
)
89 mark_common(parents
->item
, 0, dont_parse
);
95 Get the next rev to send, ignoring the common.
98 static const unsigned char* get_rev(void)
100 struct commit
*commit
= NULL
;
102 while (commit
== NULL
) {
104 struct commit_list
* parents
;
106 if (rev_list
== NULL
|| non_common_revs
== 0)
109 commit
= rev_list
->item
;
110 if (!(commit
->object
.parsed
))
111 parse_commit(commit
);
112 commit
->object
.flags
|= POPPED
;
113 if (!(commit
->object
.flags
& COMMON
))
116 parents
= commit
->parents
;
118 if (commit
->object
.flags
& COMMON
) {
119 /* do not send "have", and ignore ancestors */
121 mark
= COMMON
| SEEN
;
122 } else if (commit
->object
.flags
& COMMON_REF
)
123 /* send "have", and ignore ancestors */
124 mark
= COMMON
| SEEN
;
126 /* send "have", also for its ancestors */
130 if (!(parents
->item
->object
.flags
& SEEN
))
131 rev_list_push(parents
->item
, mark
);
133 mark_common(parents
->item
, 1, 0);
134 parents
= parents
->next
;
137 rev_list
= rev_list
->next
;
140 return commit
->object
.sha1
;
143 static int find_common(int fd
[2], unsigned char *result_sha1
,
147 int count
= 0, flushes
= 0, retval
;
148 const unsigned char *sha1
;
149 unsigned in_vain
= 0;
150 int got_continue
= 0;
152 for_each_ref(rev_list_insert_ref
, NULL
);
155 for ( ; refs
; refs
= refs
->next
) {
156 unsigned char *remote
= refs
->old_sha1
;
160 * If that object is complete (i.e. it is an ancestor of a
161 * local ref), we tell them we have it but do not have to
162 * tell them about its ancestors, which they already know
165 * We use lookup_object here because we are only
166 * interested in the case we *know* the object is
167 * reachable and we have already scanned it.
169 if (((o
= lookup_object(remote
)) != NULL
) &&
170 (o
->flags
& COMPLETE
)) {
175 packet_write(fd
[1], "want %s%s%s%s%s%s%s\n",
177 (multi_ack
? " multi_ack" : ""),
178 (use_sideband
== 2 ? " side-band-64k" : ""),
179 (use_sideband
== 1 ? " side-band" : ""),
180 (use_thin_pack
? " thin-pack" : ""),
181 (args
.no_progress
? " no-progress" : ""),
184 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
187 if (is_repository_shallow())
188 write_shallow_commits(fd
[1], 1);
190 packet_write(fd
[1], "deepen %d", args
.depth
);
195 if (args
.depth
> 0) {
197 unsigned char sha1
[20];
200 while ((len
= packet_read_line(fd
[0], line
, sizeof(line
)))) {
201 if (!prefixcmp(line
, "shallow ")) {
202 if (get_sha1_hex(line
+ 8, sha1
))
203 die("invalid shallow line: %s", line
);
204 register_shallow(sha1
);
207 if (!prefixcmp(line
, "unshallow ")) {
208 if (get_sha1_hex(line
+ 10, sha1
))
209 die("invalid unshallow line: %s", line
);
210 if (!lookup_object(sha1
))
211 die("object not found: %s", line
);
212 /* make sure that it is parsed as shallow */
214 if (unregister_shallow(sha1
))
215 die("no shallow found: %s", line
);
218 die("expected shallow/unshallow, got %s", line
);
224 while ((sha1
= get_rev())) {
225 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
227 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
229 if (!(31 & ++count
)) {
236 * We keep one window "ahead" of the other side, and
237 * will wait for an ACK only on the next one
243 ack
= get_ack(fd
[0], result_sha1
);
244 if (args
.verbose
&& ack
)
245 fprintf(stderr
, "got ack %d %s\n", ack
,
246 sha1_to_hex(result_sha1
));
252 } else if (ack
== 2) {
253 struct commit
*commit
=
254 lookup_commit(result_sha1
);
255 mark_common(commit
, 0, 1);
262 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
264 fprintf(stderr
, "giving up\n");
270 packet_write(fd
[1], "done\n");
272 fprintf(stderr
, "done\n");
277 while (flushes
|| multi_ack
) {
278 int ack
= get_ack(fd
[0], result_sha1
);
281 fprintf(stderr
, "got ack (%d) %s\n", ack
,
282 sha1_to_hex(result_sha1
));
293 static struct commit_list
*complete
;
295 static int mark_complete(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
297 struct object
*o
= parse_object(sha1
);
299 while (o
&& o
->type
== OBJ_TAG
) {
300 struct tag
*t
= (struct tag
*) o
;
302 break; /* broken repository */
303 o
->flags
|= COMPLETE
;
304 o
= parse_object(t
->tagged
->sha1
);
306 if (o
&& o
->type
== OBJ_COMMIT
) {
307 struct commit
*commit
= (struct commit
*)o
;
308 commit
->object
.flags
|= COMPLETE
;
309 insert_by_date(commit
, &complete
);
314 static void mark_recent_complete_commits(unsigned long cutoff
)
316 while (complete
&& cutoff
<= complete
->item
->date
) {
318 fprintf(stderr
, "Marking %s as complete\n",
319 sha1_to_hex(complete
->item
->object
.sha1
));
320 pop_most_recent_commit(&complete
, COMPLETE
);
324 static void filter_refs(struct ref
**refs
, int nr_match
, char **match
)
326 struct ref
**return_refs
;
327 struct ref
*newlist
= NULL
;
328 struct ref
**newtail
= &newlist
;
329 struct ref
*ref
, *next
;
330 struct ref
*fastarray
[32];
332 if (nr_match
&& !args
.fetch_all
) {
333 if (ARRAY_SIZE(fastarray
) < nr_match
)
334 return_refs
= xcalloc(nr_match
, sizeof(struct ref
*));
336 return_refs
= fastarray
;
337 memset(return_refs
, 0, sizeof(struct ref
*) * nr_match
);
343 for (ref
= *refs
; ref
; ref
= next
) {
345 if (!memcmp(ref
->name
, "refs/", 5) &&
346 check_ref_format(ref
->name
+ 5))
348 else if (args
.fetch_all
&&
349 (!args
.depth
|| prefixcmp(ref
->name
, "refs/tags/") )) {
352 newtail
= &ref
->next
;
356 int order
= path_match(ref
->name
, nr_match
, match
);
358 return_refs
[order
-1] = ref
;
359 continue; /* we will link it later */
365 if (!args
.fetch_all
) {
367 for (i
= 0; i
< nr_match
; i
++) {
368 ref
= return_refs
[i
];
372 newtail
= &ref
->next
;
375 if (return_refs
!= fastarray
)
381 static int everything_local(struct ref
**refs
, int nr_match
, char **match
)
385 unsigned long cutoff
= 0;
387 track_object_refs
= 0;
388 save_commit_buffer
= 0;
390 for (ref
= *refs
; ref
; ref
= ref
->next
) {
393 o
= parse_object(ref
->old_sha1
);
397 /* We already have it -- which may mean that we were
398 * in sync with the other side at some time after
399 * that (it is OK if we guess wrong here).
401 if (o
->type
== OBJ_COMMIT
) {
402 struct commit
*commit
= (struct commit
*)o
;
403 if (!cutoff
|| cutoff
< commit
->date
)
404 cutoff
= commit
->date
;
409 for_each_ref(mark_complete
, NULL
);
411 mark_recent_complete_commits(cutoff
);
415 * Mark all complete remote refs as common refs.
416 * Don't mark them common yet; the server has to be told so first.
418 for (ref
= *refs
; ref
; ref
= ref
->next
) {
419 struct object
*o
= deref_tag(lookup_object(ref
->old_sha1
),
422 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
425 if (!(o
->flags
& SEEN
)) {
426 rev_list_push((struct commit
*)o
, COMMON_REF
| SEEN
);
428 mark_common((struct commit
*)o
, 1, 1);
432 filter_refs(refs
, nr_match
, match
);
434 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
435 const unsigned char *remote
= ref
->old_sha1
;
436 unsigned char local
[20];
439 o
= lookup_object(remote
);
440 if (!o
|| !(o
->flags
& COMPLETE
)) {
445 "want %s (%s)\n", sha1_to_hex(remote
),
450 hashcpy(ref
->new_sha1
, local
);
454 "already have %s (%s)\n", sha1_to_hex(remote
),
460 static int sideband_demux(int fd
, void *data
)
465 return recv_sideband("fetch-pack", xd
[0], fd
, 2);
468 static void setup_sideband(int fd
[2], int xd
[2], struct async
*demux
)
475 /* xd[] is talking with upload-pack; subprocess reads from
476 * xd[0], spits out band#2 to stderr, and feeds us band#1
477 * through demux->out.
479 demux
->proc
= sideband_demux
;
481 if (start_async(demux
))
482 die("fetch-pack: unable to fork off sideband demultiplexer");
488 static int get_pack(int xd
[2], char **pack_lockfile
)
492 const char *argv
[20];
496 int do_keep
= args
.keep_pack
;
497 struct child_process cmd
;
499 setup_sideband(fd
, xd
, &demux
);
501 memset(&cmd
, 0, sizeof(cmd
));
505 if (!args
.keep_pack
&& unpack_limit
) {
506 struct pack_header header
;
508 if (read_pack_header(fd
[0], &header
))
509 die("protocol error: bad pack header");
510 snprintf(hdr_arg
, sizeof(hdr_arg
), "--pack_header=%u,%u",
511 ntohl(header
.hdr_version
), ntohl(header
.hdr_entries
));
512 if (ntohl(header
.hdr_entries
) < unpack_limit
)
521 *av
++ = "index-pack";
523 if (!args
.quiet
&& !args
.no_progress
)
525 if (args
.use_thin_pack
)
526 *av
++ = "--fix-thin";
527 if (args
.lock_pack
|| unpack_limit
) {
528 int s
= sprintf(keep_arg
,
529 "--keep=fetch-pack %d on ", getpid());
530 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
531 strcpy(keep_arg
+ s
, "localhost");
536 *av
++ = "unpack-objects";
546 if (start_command(&cmd
))
547 die("fetch-pack: unable to fork off %s", argv
[0]);
549 if (do_keep
&& pack_lockfile
)
550 *pack_lockfile
= index_pack_lockfile(cmd
.out
);
552 if (finish_command(&cmd
))
553 die("%s failed", argv
[0]);
554 if (use_sideband
&& finish_async(&demux
))
555 die("error in sideband demultiplexer");
559 static struct ref
*do_fetch_pack(int fd
[2],
562 char **pack_lockfile
)
565 unsigned char sha1
[20];
567 get_remote_heads(fd
[0], &ref
, 0, NULL
, 0);
568 if (is_repository_shallow() && !server_supports("shallow"))
569 die("Server does not support shallow clients");
570 if (server_supports("multi_ack")) {
572 fprintf(stderr
, "Server supports multi_ack\n");
575 if (server_supports("side-band-64k")) {
577 fprintf(stderr
, "Server supports side-band-64k\n");
580 else if (server_supports("side-band")) {
582 fprintf(stderr
, "Server supports side-band\n");
587 die("no matching remote head");
589 if (everything_local(&ref
, nr_match
, match
)) {
593 if (find_common(fd
, sha1
, ref
) < 0)
595 /* When cloning, it is not unusual to have
598 fprintf(stderr
, "warning: no common commits\n");
600 if (get_pack(fd
, pack_lockfile
))
601 die("git-fetch-pack: fetch failed.");
607 static int remove_duplicates(int nr_heads
, char **heads
)
611 for (src
= dst
= 0; src
< nr_heads
; src
++) {
612 /* If heads[src] is different from any of
613 * heads[0..dst], push it in.
616 for (i
= 0; i
< dst
; i
++) {
617 if (!strcmp(heads
[i
], heads
[src
]))
623 heads
[dst
] = heads
[src
];
629 static int fetch_pack_config(const char *var
, const char *value
)
631 if (strcmp(var
, "fetch.unpacklimit") == 0) {
632 fetch_unpack_limit
= git_config_int(var
, value
);
636 if (strcmp(var
, "transfer.unpacklimit") == 0) {
637 transfer_unpack_limit
= git_config_int(var
, value
);
641 return git_default_config(var
, value
);
644 static struct lock_file lock
;
646 static void fetch_pack_setup(void)
648 static int did_setup
;
651 git_config(fetch_pack_config
);
652 if (0 <= transfer_unpack_limit
)
653 unpack_limit
= transfer_unpack_limit
;
654 else if (0 <= fetch_unpack_limit
)
655 unpack_limit
= fetch_unpack_limit
;
659 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix
)
661 int i
, ret
, nr_heads
;
663 char *dest
= NULL
, **heads
;
667 for (i
= 1; i
< argc
; i
++) {
668 const char *arg
= argv
[i
];
671 if (!prefixcmp(arg
, "--upload-pack=")) {
672 args
.uploadpack
= arg
+ 14;
675 if (!prefixcmp(arg
, "--exec=")) {
676 args
.uploadpack
= arg
+ 7;
679 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
683 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
684 args
.lock_pack
= args
.keep_pack
;
688 if (!strcmp("--thin", arg
)) {
689 args
.use_thin_pack
= 1;
692 if (!strcmp("--all", arg
)) {
696 if (!strcmp("-v", arg
)) {
700 if (!prefixcmp(arg
, "--depth=")) {
701 args
.depth
= strtol(arg
+ 8, NULL
, 0);
704 if (!strcmp("--no-progress", arg
)) {
705 args
.no_progress
= 1;
708 usage(fetch_pack_usage
);
711 heads
= (char **)(argv
+ i
+ 1);
712 nr_heads
= argc
- i
- 1;
716 usage(fetch_pack_usage
);
718 ref
= fetch_pack(&args
, dest
, nr_heads
, heads
, NULL
);
723 sha1_to_hex(ref
->old_sha1
), ref
->name
);
730 struct ref
*fetch_pack(struct fetch_pack_args
*my_args
,
734 char **pack_lockfile
)
738 struct child_process
*conn
;
743 memcpy(&args
, my_args
, sizeof(args
));
744 if (args
.depth
> 0) {
745 if (stat(git_path("shallow"), &st
))
749 conn
= git_connect(fd
, (char *)dest
, uploadpack
,
750 args
.verbose
? CONNECT_VERBOSE
: 0);
751 if (heads
&& nr_heads
)
752 nr_heads
= remove_duplicates(nr_heads
, heads
);
753 ref
= do_fetch_pack(fd
, nr_heads
, heads
, pack_lockfile
);
756 ret
= finish_connect(conn
);
758 if (!ret
&& nr_heads
) {
759 /* If the heads to pull were given, we should have
760 * consumed all of them by matching the remote.
761 * Otherwise, 'git-fetch remote no-such-ref' would
762 * silently succeed without issuing an error.
764 for (i
= 0; i
< nr_heads
; i
++)
765 if (heads
[i
] && heads
[i
][0]) {
766 error("no such remote ref %s", heads
[i
]);
771 if (!ret
&& args
.depth
> 0) {
772 struct cache_time mtime
;
773 char *shallow
= git_path("shallow");
776 mtime
.sec
= st
.st_mtime
;
778 mtime
.usec
= st
.st_mtim
.usec
;
780 if (stat(shallow
, &st
)) {
782 die("shallow file was removed during fetch");
783 } else if (st
.st_mtime
!= mtime
.sec
785 || st
.st_mtim
.usec
!= mtime
.usec
788 die("shallow file was changed during fetch");
790 fd
= hold_lock_file_for_update(&lock
, shallow
, 1);
791 if (!write_shallow_commits(fd
, 0)) {
793 rollback_lock_file(&lock
);
796 commit_lock_file(&lock
);