]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin-fetch-pack.c
9 #include "fetch-pack.h"
12 static int transfer_unpack_limit
= -1;
13 static int fetch_unpack_limit
= -1;
14 static int unpack_limit
= 100;
19 static int no_progress
;
20 static const char fetch_pack_usage
[] =
21 "git-fetch-pack [--all] [--quiet|-q] [--keep|-k] [--thin] [--upload-pack=<git-upload-pack>] [--depth=<n>] [--no-progress] [-v] [<host>:]<directory> [<refs>...]";
22 static const char *uploadpack
= "git-upload-pack";
24 #define COMPLETE (1U << 0)
25 #define COMMON (1U << 1)
26 #define COMMON_REF (1U << 2)
27 #define SEEN (1U << 3)
28 #define POPPED (1U << 4)
31 * After sending this many "have"s if we do not get any new ACK , we
32 * give up traversing our history.
34 #define MAX_IN_VAIN 256
36 static struct commit_list
*rev_list
;
37 static int non_common_revs
, multi_ack
, use_thin_pack
, use_sideband
;
39 static void rev_list_push(struct commit
*commit
, int mark
)
41 if (!(commit
->object
.flags
& mark
)) {
42 commit
->object
.flags
|= mark
;
44 if (!(commit
->object
.parsed
))
47 insert_by_date(commit
, &rev_list
);
49 if (!(commit
->object
.flags
& COMMON
))
54 static int rev_list_insert_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
56 struct object
*o
= deref_tag(parse_object(sha1
), path
, 0);
58 if (o
&& o
->type
== OBJ_COMMIT
)
59 rev_list_push((struct commit
*)o
, SEEN
);
65 This function marks a rev and its ancestors as common.
66 In some cases, it is desirable to mark only the ancestors (for example
67 when only the server does not yet know that they are common).
70 static void mark_common(struct commit
*commit
,
71 int ancestors_only
, int dont_parse
)
73 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
74 struct object
*o
= (struct object
*)commit
;
79 if (!(o
->flags
& SEEN
))
80 rev_list_push(commit
, SEEN
);
82 struct commit_list
*parents
;
84 if (!ancestors_only
&& !(o
->flags
& POPPED
))
86 if (!o
->parsed
&& !dont_parse
)
89 for (parents
= commit
->parents
;
91 parents
= parents
->next
)
92 mark_common(parents
->item
, 0, dont_parse
);
98 Get the next rev to send, ignoring the common.
101 static const unsigned char* get_rev(void)
103 struct commit
*commit
= NULL
;
105 while (commit
== NULL
) {
107 struct commit_list
* parents
;
109 if (rev_list
== NULL
|| non_common_revs
== 0)
112 commit
= rev_list
->item
;
113 if (!(commit
->object
.parsed
))
114 parse_commit(commit
);
115 commit
->object
.flags
|= POPPED
;
116 if (!(commit
->object
.flags
& COMMON
))
119 parents
= commit
->parents
;
121 if (commit
->object
.flags
& COMMON
) {
122 /* do not send "have", and ignore ancestors */
124 mark
= COMMON
| SEEN
;
125 } else if (commit
->object
.flags
& COMMON_REF
)
126 /* send "have", and ignore ancestors */
127 mark
= COMMON
| SEEN
;
129 /* send "have", also for its ancestors */
133 if (!(parents
->item
->object
.flags
& SEEN
))
134 rev_list_push(parents
->item
, mark
);
136 mark_common(parents
->item
, 1, 0);
137 parents
= parents
->next
;
140 rev_list
= rev_list
->next
;
143 return commit
->object
.sha1
;
146 static int find_common(int fd
[2], unsigned char *result_sha1
,
150 int count
= 0, flushes
= 0, retval
;
151 const unsigned char *sha1
;
152 unsigned in_vain
= 0;
153 int got_continue
= 0;
155 for_each_ref(rev_list_insert_ref
, NULL
);
158 for ( ; refs
; refs
= refs
->next
) {
159 unsigned char *remote
= refs
->old_sha1
;
163 * If that object is complete (i.e. it is an ancestor of a
164 * local ref), we tell them we have it but do not have to
165 * tell them about its ancestors, which they already know
168 * We use lookup_object here because we are only
169 * interested in the case we *know* the object is
170 * reachable and we have already scanned it.
172 if (((o
= lookup_object(remote
)) != NULL
) &&
173 (o
->flags
& COMPLETE
)) {
178 packet_write(fd
[1], "want %s%s%s%s%s%s%s\n",
180 (multi_ack
? " multi_ack" : ""),
181 (use_sideband
== 2 ? " side-band-64k" : ""),
182 (use_sideband
== 1 ? " side-band" : ""),
183 (use_thin_pack
? " thin-pack" : ""),
184 (no_progress
? " no-progress" : ""),
187 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
190 if (is_repository_shallow())
191 write_shallow_commits(fd
[1], 1);
193 packet_write(fd
[1], "deepen %d", depth
);
200 unsigned char sha1
[20];
203 while ((len
= packet_read_line(fd
[0], line
, sizeof(line
)))) {
204 if (!prefixcmp(line
, "shallow ")) {
205 if (get_sha1_hex(line
+ 8, sha1
))
206 die("invalid shallow line: %s", line
);
207 register_shallow(sha1
);
210 if (!prefixcmp(line
, "unshallow ")) {
211 if (get_sha1_hex(line
+ 10, sha1
))
212 die("invalid unshallow line: %s", line
);
213 if (!lookup_object(sha1
))
214 die("object not found: %s", line
);
215 /* make sure that it is parsed as shallow */
217 if (unregister_shallow(sha1
))
218 die("no shallow found: %s", line
);
221 die("expected shallow/unshallow, got %s", line
);
227 while ((sha1
= get_rev())) {
228 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
230 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
232 if (!(31 & ++count
)) {
239 * We keep one window "ahead" of the other side, and
240 * will wait for an ACK only on the next one
246 ack
= get_ack(fd
[0], result_sha1
);
248 fprintf(stderr
, "got ack %d %s\n", ack
,
249 sha1_to_hex(result_sha1
));
255 } else if (ack
== 2) {
256 struct commit
*commit
=
257 lookup_commit(result_sha1
);
258 mark_common(commit
, 0, 1);
265 if (got_continue
&& MAX_IN_VAIN
< in_vain
) {
267 fprintf(stderr
, "giving up\n");
273 packet_write(fd
[1], "done\n");
275 fprintf(stderr
, "done\n");
280 while (flushes
|| multi_ack
) {
281 int ack
= get_ack(fd
[0], result_sha1
);
284 fprintf(stderr
, "got ack (%d) %s\n", ack
,
285 sha1_to_hex(result_sha1
));
296 static struct commit_list
*complete
;
298 static int mark_complete(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
300 struct object
*o
= parse_object(sha1
);
302 while (o
&& o
->type
== OBJ_TAG
) {
303 struct tag
*t
= (struct tag
*) o
;
305 break; /* broken repository */
306 o
->flags
|= COMPLETE
;
307 o
= parse_object(t
->tagged
->sha1
);
309 if (o
&& o
->type
== OBJ_COMMIT
) {
310 struct commit
*commit
= (struct commit
*)o
;
311 commit
->object
.flags
|= COMPLETE
;
312 insert_by_date(commit
, &complete
);
317 static void mark_recent_complete_commits(unsigned long cutoff
)
319 while (complete
&& cutoff
<= complete
->item
->date
) {
321 fprintf(stderr
, "Marking %s as complete\n",
322 sha1_to_hex(complete
->item
->object
.sha1
));
323 pop_most_recent_commit(&complete
, COMPLETE
);
327 static void filter_refs(struct ref
**refs
, int nr_match
, char **match
)
329 struct ref
**return_refs
;
330 struct ref
*newlist
= NULL
;
331 struct ref
**newtail
= &newlist
;
332 struct ref
*ref
, *next
;
333 struct ref
*fastarray
[32];
335 if (nr_match
&& !fetch_all
) {
336 if (ARRAY_SIZE(fastarray
) < nr_match
)
337 return_refs
= xcalloc(nr_match
, sizeof(struct ref
*));
339 return_refs
= fastarray
;
340 memset(return_refs
, 0, sizeof(struct ref
*) * nr_match
);
346 for (ref
= *refs
; ref
; ref
= next
) {
348 if (!memcmp(ref
->name
, "refs/", 5) &&
349 check_ref_format(ref
->name
+ 5))
351 else if (fetch_all
&&
352 (!depth
|| prefixcmp(ref
->name
, "refs/tags/") )) {
355 newtail
= &ref
->next
;
359 int order
= path_match(ref
->name
, nr_match
, match
);
361 return_refs
[order
-1] = ref
;
362 continue; /* we will link it later */
370 for (i
= 0; i
< nr_match
; i
++) {
371 ref
= return_refs
[i
];
375 newtail
= &ref
->next
;
378 if (return_refs
!= fastarray
)
384 static int everything_local(struct ref
**refs
, int nr_match
, char **match
)
388 unsigned long cutoff
= 0;
390 track_object_refs
= 0;
391 save_commit_buffer
= 0;
393 for (ref
= *refs
; ref
; ref
= ref
->next
) {
396 o
= parse_object(ref
->old_sha1
);
400 /* We already have it -- which may mean that we were
401 * in sync with the other side at some time after
402 * that (it is OK if we guess wrong here).
404 if (o
->type
== OBJ_COMMIT
) {
405 struct commit
*commit
= (struct commit
*)o
;
406 if (!cutoff
|| cutoff
< commit
->date
)
407 cutoff
= commit
->date
;
412 for_each_ref(mark_complete
, NULL
);
414 mark_recent_complete_commits(cutoff
);
418 * Mark all complete remote refs as common refs.
419 * Don't mark them common yet; the server has to be told so first.
421 for (ref
= *refs
; ref
; ref
= ref
->next
) {
422 struct object
*o
= deref_tag(lookup_object(ref
->old_sha1
),
425 if (!o
|| o
->type
!= OBJ_COMMIT
|| !(o
->flags
& COMPLETE
))
428 if (!(o
->flags
& SEEN
)) {
429 rev_list_push((struct commit
*)o
, COMMON_REF
| SEEN
);
431 mark_common((struct commit
*)o
, 1, 1);
435 filter_refs(refs
, nr_match
, match
);
437 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
438 const unsigned char *remote
= ref
->old_sha1
;
439 unsigned char local
[20];
442 o
= lookup_object(remote
);
443 if (!o
|| !(o
->flags
& COMPLETE
)) {
448 "want %s (%s)\n", sha1_to_hex(remote
),
453 hashcpy(ref
->new_sha1
, local
);
457 "already have %s (%s)\n", sha1_to_hex(remote
),
463 static pid_t
setup_sideband(int fd
[2], int xd
[2])
472 /* xd[] is talking with upload-pack; subprocess reads from
473 * xd[0], spits out band#2 to stderr, and feeds us band#1
477 die("fetch-pack: unable to set up pipe");
480 die("fetch-pack: unable to fork off sideband demultiplexer");
486 if (recv_sideband("fetch-pack", xd
[0], fd
[1], 2))
496 static int get_pack(int xd
[2], char **pack_lockfile
)
501 const char *argv
[20];
505 int do_keep
= keep_pack
;
508 side_pid
= setup_sideband(fd
, xd
);
513 struct pack_header header
;
515 if (read_pack_header(fd
[0], &header
))
516 die("protocol error: bad pack header");
517 snprintf(hdr_arg
, sizeof(hdr_arg
), "--pack_header=%u,%u",
518 ntohl(header
.hdr_version
), ntohl(header
.hdr_entries
));
519 if (ntohl(header
.hdr_entries
) < unpack_limit
)
526 if (pack_lockfile
&& pipe(keep_pipe
))
527 die("fetch-pack: pipe setup failure: %s", strerror(errno
));
528 *av
++ = "index-pack";
530 if (!quiet
&& !no_progress
)
533 *av
++ = "--fix-thin";
534 if (keep_pack
> 1 || unpack_limit
) {
535 int s
= sprintf(keep_arg
,
536 "--keep=fetch-pack %d on ", getpid());
537 if (gethostname(keep_arg
+ s
, sizeof(keep_arg
) - s
))
538 strcpy(keep_arg
+ s
, "localhost");
543 *av
++ = "unpack-objects";
553 die("fetch-pack: unable to fork off %s", argv
[0]);
556 if (do_keep
&& pack_lockfile
) {
557 dup2(keep_pipe
[1], 1);
564 die("%s exec failed", argv
[0]);
568 if (do_keep
&& pack_lockfile
) {
570 *pack_lockfile
= index_pack_lockfile(keep_pipe
[0]);
573 while (waitpid(pid
, &status
, 0) < 0) {
575 die("waiting for %s: %s", argv
[0], strerror(errno
));
577 if (WIFEXITED(status
)) {
578 int code
= WEXITSTATUS(status
);
580 die("%s died with error code %d", argv
[0], code
);
583 if (WIFSIGNALED(status
)) {
584 int sig
= WTERMSIG(status
);
585 die("%s died of signal %d", argv
[0], sig
);
587 die("%s died of unnatural causes %d", argv
[0], status
);
590 static struct ref
*do_fetch_pack(int fd
[2],
593 char **pack_lockfile
)
596 unsigned char sha1
[20];
598 get_remote_heads(fd
[0], &ref
, 0, NULL
, 0);
599 if (is_repository_shallow() && !server_supports("shallow"))
600 die("Server does not support shallow clients");
601 if (server_supports("multi_ack")) {
603 fprintf(stderr
, "Server supports multi_ack\n");
606 if (server_supports("side-band-64k")) {
608 fprintf(stderr
, "Server supports side-band-64k\n");
611 else if (server_supports("side-band")) {
613 fprintf(stderr
, "Server supports side-band\n");
618 die("no matching remote head");
620 if (everything_local(&ref
, nr_match
, match
)) {
624 if (find_common(fd
, sha1
, ref
) < 0)
626 /* When cloning, it is not unusual to have
629 fprintf(stderr
, "warning: no common commits\n");
631 if (get_pack(fd
, pack_lockfile
))
632 die("git-fetch-pack: fetch failed.");
638 static int remove_duplicates(int nr_heads
, char **heads
)
642 for (src
= dst
= 0; src
< nr_heads
; src
++) {
643 /* If heads[src] is different from any of
644 * heads[0..dst], push it in.
647 for (i
= 0; i
< dst
; i
++) {
648 if (!strcmp(heads
[i
], heads
[src
]))
654 heads
[dst
] = heads
[src
];
660 static int fetch_pack_config(const char *var
, const char *value
)
662 if (strcmp(var
, "fetch.unpacklimit") == 0) {
663 fetch_unpack_limit
= git_config_int(var
, value
);
667 if (strcmp(var
, "transfer.unpacklimit") == 0) {
668 transfer_unpack_limit
= git_config_int(var
, value
);
672 return git_default_config(var
, value
);
675 static struct lock_file lock
;
677 void setup_fetch_pack(struct fetch_pack_args
*args
)
679 uploadpack
= args
->uploadpack
;
681 keep_pack
= args
->keep_pack
;
682 if (args
->unpacklimit
>= 0)
683 unpack_limit
= args
->unpacklimit
;
686 use_thin_pack
= args
->use_thin_pack
;
687 fetch_all
= args
->fetch_all
;
688 verbose
= args
->verbose
;
690 no_progress
= args
->no_progress
;
693 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix
)
695 int i
, ret
, nr_heads
;
697 char *dest
= NULL
, **heads
;
699 git_config(fetch_pack_config
);
701 if (0 <= transfer_unpack_limit
)
702 unpack_limit
= transfer_unpack_limit
;
703 else if (0 <= fetch_unpack_limit
)
704 unpack_limit
= fetch_unpack_limit
;
708 for (i
= 1; i
< argc
; i
++) {
709 const char *arg
= argv
[i
];
712 if (!prefixcmp(arg
, "--upload-pack=")) {
713 uploadpack
= arg
+ 14;
716 if (!prefixcmp(arg
, "--exec=")) {
717 uploadpack
= arg
+ 7;
720 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
724 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
729 if (!strcmp("--thin", arg
)) {
733 if (!strcmp("--all", arg
)) {
737 if (!strcmp("-v", arg
)) {
741 if (!prefixcmp(arg
, "--depth=")) {
742 depth
= strtol(arg
+ 8, NULL
, 0);
745 if (!strcmp("--no-progress", arg
)) {
749 usage(fetch_pack_usage
);
752 heads
= (char **)(argv
+ i
+ 1);
753 nr_heads
= argc
- i
- 1;
757 usage(fetch_pack_usage
);
759 ref
= fetch_pack(dest
, nr_heads
, heads
, NULL
);
765 sha1_to_hex(ref
->old_sha1
), ref
->name
);
772 struct ref
*fetch_pack(const char *dest
,
775 char **pack_lockfile
)
784 if (stat(git_path("shallow"), &st
))
788 pid
= git_connect(fd
, (char *)dest
, uploadpack
,
789 verbose
? CONNECT_VERBOSE
: 0);
792 if (heads
&& nr_heads
)
793 nr_heads
= remove_duplicates(nr_heads
, heads
);
794 ref
= do_fetch_pack(fd
, nr_heads
, heads
, pack_lockfile
);
797 ret
= finish_connect(pid
);
799 if (!ret
&& nr_heads
) {
800 /* If the heads to pull were given, we should have
801 * consumed all of them by matching the remote.
802 * Otherwise, 'git-fetch remote no-such-ref' would
803 * silently succeed without issuing an error.
805 for (i
= 0; i
< nr_heads
; i
++)
806 if (heads
[i
] && heads
[i
][0]) {
807 error("no such remote ref %s", heads
[i
]);
812 if (!ret
&& depth
> 0) {
813 struct cache_time mtime
;
814 char *shallow
= git_path("shallow");
817 mtime
.sec
= st
.st_mtime
;
819 mtime
.usec
= st
.st_mtim
.usec
;
821 if (stat(shallow
, &st
)) {
823 die("shallow file was removed during fetch");
824 } else if (st
.st_mtime
!= mtime
.sec
826 || st
.st_mtim
.usec
!= mtime
.usec
829 die("shallow file was changed during fetch");
831 fd
= hold_lock_file_for_update(&lock
, shallow
, 1);
832 if (!write_shallow_commits(fd
, 0)) {
834 rollback_lock_file(&lock
);
837 commit_lock_file(&lock
);