11 static const char fetch_pack_usage
[] =
12 "git-fetch-pack [-q] [-v] [--exec=upload-pack] [host:]directory <refs>...";
13 static const char *exec
= "git-upload-pack";
15 #define COMPLETE (1U << 0)
16 #define COMMON (1U << 1)
17 #define COMMON_REF (1U << 2)
18 #define SEEN (1U << 3)
19 #define POPPED (1U << 4)
21 static struct commit_list
*rev_list
= NULL
;
22 static int non_common_revs
= 0;
24 static void rev_list_push(struct commit
*commit
, int mark
)
26 if (!(commit
->object
.flags
& mark
)) {
27 commit
->object
.flags
|= mark
;
29 if (!(commit
->object
.parsed
))
32 insert_by_date(commit
, &rev_list
);
34 if (!(commit
->object
.flags
& COMMON
))
39 static int rev_list_insert_ref(const char *path
, const unsigned char *sha1
)
41 struct object
*o
= deref_tag(parse_object(sha1
));
43 if (o
->type
== commit_type
)
44 rev_list_push((struct commit
*)o
, SEEN
);
50 This function marks a rev and its ancestors as common.
51 In some cases, it is desirable to mark only the ancestors (for example
52 when only the server does not yet know that they are common).
55 static void mark_common(struct commit
*commit
,
56 int ancestors_only
, int dont_parse
)
58 if (commit
!= NULL
&& !(commit
->object
.flags
& COMMON
)) {
59 struct object
*o
= (struct object
*)commit
;
64 if (!(o
->flags
& SEEN
))
65 rev_list_push(commit
, SEEN
);
67 struct commit_list
*parents
;
69 if (!ancestors_only
&& !(o
->flags
& POPPED
))
71 if (!o
->parsed
&& !dont_parse
)
74 for (parents
= commit
->parents
;
76 parents
= parents
->next
)
77 mark_common(parents
->item
, 0, dont_parse
);
83 Get the next rev to send, ignoring the common.
86 static const unsigned char* get_rev()
88 struct commit
*commit
= NULL
;
90 while (commit
== NULL
) {
92 struct commit_list
* parents
;
94 if (rev_list
== NULL
|| non_common_revs
== 0)
97 commit
= rev_list
->item
;
98 if (!(commit
->object
.parsed
))
100 commit
->object
.flags
|= POPPED
;
101 if (!(commit
->object
.flags
& COMMON
))
104 parents
= commit
->parents
;
106 if (commit
->object
.flags
& COMMON
) {
107 /* do not send "have", and ignore ancestors */
109 mark
= COMMON
| SEEN
;
110 } else if (commit
->object
.flags
& COMMON_REF
)
111 /* send "have", and ignore ancestors */
112 mark
= COMMON
| SEEN
;
114 /* send "have", also for its ancestors */
118 if (!(parents
->item
->object
.flags
& SEEN
))
119 rev_list_push(parents
->item
, mark
);
121 mark_common(parents
->item
, 1, 0);
122 parents
= parents
->next
;
125 rev_list
= rev_list
->next
;
128 return commit
->object
.sha1
;
131 static int find_common(int fd
[2], unsigned char *result_sha1
,
135 int count
= 0, flushes
= 0, retval
;
136 const unsigned char *sha1
;
138 for_each_ref(rev_list_insert_ref
);
141 for ( ; refs
; refs
= refs
->next
) {
142 unsigned char *remote
= refs
->old_sha1
;
146 * If that object is complete (i.e. it is an ancestor of a
147 * local ref), we tell them we have it but do not have to
148 * tell them about its ancestors, which they already know
151 * We use lookup_object here because we are only
152 * interested in the case we *know* the object is
153 * reachable and we have already scanned it.
155 if (((o
= lookup_object(remote
)) != NULL
) &&
156 (o
->flags
& COMPLETE
)) {
160 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
169 while ((sha1
= get_rev())) {
170 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
172 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
173 if (!(31 & ++count
)) {
178 * We keep one window "ahead" of the other side, and
179 * will wait for an ACK only on the next one
183 if (get_ack(fd
[0], result_sha1
)) {
187 fprintf(stderr
, "got ack\n");
194 packet_write(fd
[1], "done\n");
196 fprintf(stderr
, "done\n");
201 if (get_ack(fd
[0], result_sha1
)) {
203 fprintf(stderr
, "got ack\n");
210 static struct commit_list
*complete
= NULL
;
212 static int mark_complete(const char *path
, const unsigned char *sha1
)
214 struct object
*o
= parse_object(sha1
);
216 while (o
&& o
->type
== tag_type
) {
217 struct tag
*t
= (struct tag
*) o
;
219 break; /* broken repository */
220 o
->flags
|= COMPLETE
;
221 o
= parse_object(t
->tagged
->sha1
);
223 if (o
&& o
->type
== commit_type
) {
224 struct commit
*commit
= (struct commit
*)o
;
225 commit
->object
.flags
|= COMPLETE
;
226 insert_by_date(commit
, &complete
);
231 static void mark_recent_complete_commits(unsigned long cutoff
)
233 while (complete
&& cutoff
<= complete
->item
->date
) {
235 fprintf(stderr
, "Marking %s as complete\n",
236 sha1_to_hex(complete
->item
->object
.sha1
));
237 pop_most_recent_commit(&complete
, COMPLETE
);
241 static void filter_refs(struct ref
**refs
, int nr_match
, char **match
)
243 struct ref
*prev
, *current
, *next
;
248 for (prev
= NULL
, current
= *refs
; current
; current
= next
) {
249 next
= current
->next
;
250 if ((!memcmp(current
->name
, "refs/", 5) &&
251 check_ref_format(current
->name
+ 5)) ||
252 !path_match(current
->name
, nr_match
, match
)) {
263 static int everything_local(struct ref
**refs
, int nr_match
, char **match
)
267 unsigned long cutoff
= 0;
269 track_object_refs
= 0;
270 save_commit_buffer
= 0;
272 for (ref
= *refs
; ref
; ref
= ref
->next
) {
275 o
= parse_object(ref
->old_sha1
);
279 /* We already have it -- which may mean that we were
280 * in sync with the other side at some time after
281 * that (it is OK if we guess wrong here).
283 if (o
->type
== commit_type
) {
284 struct commit
*commit
= (struct commit
*)o
;
285 if (!cutoff
|| cutoff
< commit
->date
)
286 cutoff
= commit
->date
;
290 for_each_ref(mark_complete
);
292 mark_recent_complete_commits(cutoff
);
295 * Mark all complete remote refs as common refs.
296 * Don't mark them common yet; the server has to be told so first.
298 for (ref
= *refs
; ref
; ref
= ref
->next
) {
299 struct object
*o
= deref_tag(lookup_object(ref
->old_sha1
));
301 if (!o
|| o
->type
!= commit_type
|| !(o
->flags
& COMPLETE
))
304 if (!(o
->flags
& SEEN
)) {
305 rev_list_push((struct commit
*)o
, COMMON_REF
| SEEN
);
307 mark_common((struct commit
*)o
, 1, 1);
311 filter_refs(refs
, nr_match
, match
);
313 for (retval
= 1, ref
= *refs
; ref
; ref
= ref
->next
) {
314 const unsigned char *remote
= ref
->old_sha1
;
315 unsigned char local
[20];
318 o
= lookup_object(remote
);
319 if (!o
|| !(o
->flags
& COMPLETE
)) {
324 "want %s (%s)\n", sha1_to_hex(remote
),
329 memcpy(ref
->new_sha1
, local
, 20);
333 "already have %s (%s)\n", sha1_to_hex(remote
),
339 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
342 unsigned char sha1
[20];
346 get_remote_heads(fd
[0], &ref
, 0, NULL
, 0);
349 die("no matching remote head");
351 if (everything_local(&ref
, nr_match
, match
)) {
355 if (find_common(fd
, sha1
, ref
) < 0)
356 fprintf(stderr
, "warning: no common commits\n");
359 die("git-fetch-pack: unable to fork off git-unpack-objects");
364 execlp("git-unpack-objects", "git-unpack-objects",
365 quiet
? "-q" : NULL
, NULL
);
366 die("git-unpack-objects exec failed");
370 while (waitpid(pid
, &status
, 0) < 0) {
372 die("waiting for git-unpack-objects: %s", strerror(errno
));
374 if (WIFEXITED(status
)) {
375 int code
= WEXITSTATUS(status
);
377 die("git-unpack-objects died with error code %d", code
);
381 sha1_to_hex(ref
->old_sha1
), ref
->name
);
386 if (WIFSIGNALED(status
)) {
387 int sig
= WTERMSIG(status
);
388 die("git-unpack-objects died of signal %d", sig
);
390 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
393 int main(int argc
, char **argv
)
395 int i
, ret
, nr_heads
;
396 char *dest
= NULL
, **heads
;
402 for (i
= 1; i
< argc
; i
++) {
406 if (!strncmp("--exec=", arg
, 7)) {
410 if (!strcmp("-q", arg
)) {
414 if (!strcmp("-v", arg
)) {
418 usage(fetch_pack_usage
);
421 heads
= argv
+ i
+ 1;
422 nr_heads
= argc
- i
- 1;
426 usage(fetch_pack_usage
);
427 pid
= git_connect(fd
, dest
, exec
);
430 ret
= fetch_pack(fd
, nr_heads
, heads
);