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)
17 static int find_common(int fd
[2], unsigned char *result_sha1
,
21 static char line
[1000];
22 static char rev_command
[1024];
23 int count
= 0, flushes
= 0, retval
, rev_command_len
;
26 strcpy(rev_command
, "git-rev-list $(git-rev-parse --all)");
27 rev_command_len
= strlen(rev_command
);
29 for ( ; refs
; refs
= refs
->next
) {
30 unsigned char *remote
= refs
->old_sha1
;
34 * If that object is complete (i.e. it is an ancestor of a
35 * local ref), we tell them we have it but do not have to
36 * tell them about its ancestors, which they already know
39 * We use lookup_object here because we are only
40 * interested in the case we *know* the object is
41 * reachable and we have already scanned it.
43 if (((o
= lookup_object(remote
)) != NULL
) &&
44 (o
->flags
& COMPLETE
)) {
45 struct commit_list
*p
;
46 struct commit
*commit
=
47 (struct commit
*) (o
= deref_tag(o
));
50 if (o
->type
!= commit_type
)
54 rev_command_len
+ 44 < sizeof(rev_command
)) {
55 snprintf(rev_command
+ rev_command_len
, 44,
57 sha1_to_hex(p
->item
->object
.sha1
));
58 rev_command_len
+= 43;
64 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
71 revs
= popen(rev_command
, "r");
73 die("unable to run 'git-rev-list'");
77 while (fgets(line
, sizeof(line
), revs
) != NULL
) {
78 unsigned char sha1
[20];
79 if (get_sha1_hex(line
, sha1
))
80 die("git-fetch-pack: expected object name, got crud");
81 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
83 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
84 if (!(31 & ++count
)) {
89 * We keep one window "ahead" of the other side, and
90 * will wait for an ACK only on the next one
94 if (get_ack(fd
[0], result_sha1
)) {
98 fprintf(stderr
, "got ack\n");
105 packet_write(fd
[1], "done\n");
107 fprintf(stderr
, "done\n");
110 if (get_ack(fd
[0], result_sha1
)) {
112 fprintf(stderr
, "got ack\n");
119 static struct commit_list
*complete
= NULL
;
121 static int mark_complete(const char *path
, const unsigned char *sha1
)
123 struct object
*o
= parse_object(sha1
);
125 while (o
&& o
->type
== tag_type
) {
126 struct tag
*t
= (struct tag
*) o
;
128 break; /* broken repository */
129 o
->flags
|= COMPLETE
;
130 o
= parse_object(t
->tagged
->sha1
);
132 if (o
&& o
->type
== commit_type
) {
133 struct commit
*commit
= (struct commit
*)o
;
134 commit
->object
.flags
|= COMPLETE
;
135 insert_by_date(commit
, &complete
);
140 static void mark_recent_complete_commits(unsigned long cutoff
)
142 while (complete
&& cutoff
<= complete
->item
->date
) {
144 fprintf(stderr
, "Marking %s as complete\n",
145 sha1_to_hex(complete
->item
->object
.sha1
));
146 pop_most_recent_commit(&complete
, COMPLETE
);
150 static int everything_local(struct ref
*refs
)
154 unsigned long cutoff
= 0;
156 track_object_refs
= 0;
157 save_commit_buffer
= 0;
159 for (ref
= refs
; ref
; ref
= ref
->next
) {
162 o
= parse_object(ref
->old_sha1
);
166 /* We already have it -- which may mean that we were
167 * in sync with the other side at some time after
168 * that (it is OK if we guess wrong here).
170 if (o
->type
== commit_type
) {
171 struct commit
*commit
= (struct commit
*)o
;
172 if (!cutoff
|| cutoff
< commit
->date
)
173 cutoff
= commit
->date
;
177 for_each_ref(mark_complete
);
179 mark_recent_complete_commits(cutoff
);
181 for (retval
= 1; refs
; refs
= refs
->next
) {
182 const unsigned char *remote
= refs
->old_sha1
;
183 unsigned char local
[20];
186 o
= parse_object(remote
);
187 if (!o
|| !(o
->flags
& COMPLETE
)) {
192 "want %s (%s)\n", sha1_to_hex(remote
),
197 memcpy(refs
->new_sha1
, local
, 20);
201 "already have %s (%s)\n", sha1_to_hex(remote
),
207 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
210 unsigned char sha1
[20];
214 get_remote_heads(fd
[0], &ref
, nr_match
, match
, 1);
217 die("no matching remote head");
219 if (everything_local(ref
)) {
223 if (find_common(fd
, sha1
, ref
) < 0)
224 fprintf(stderr
, "warning: no common commits\n");
227 die("git-fetch-pack: unable to fork off git-unpack-objects");
232 execlp("git-unpack-objects", "git-unpack-objects",
233 quiet
? "-q" : NULL
, NULL
);
234 die("git-unpack-objects exec failed");
238 while (waitpid(pid
, &status
, 0) < 0) {
240 die("waiting for git-unpack-objects: %s", strerror(errno
));
242 if (WIFEXITED(status
)) {
243 int code
= WEXITSTATUS(status
);
245 die("git-unpack-objects died with error code %d", code
);
249 sha1_to_hex(ref
->old_sha1
), ref
->name
);
254 if (WIFSIGNALED(status
)) {
255 int sig
= WTERMSIG(status
);
256 die("git-unpack-objects died of signal %d", sig
);
258 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
261 int main(int argc
, char **argv
)
263 int i
, ret
, nr_heads
;
264 char *dest
= NULL
, **heads
;
270 for (i
= 1; i
< argc
; i
++) {
274 if (!strncmp("--exec=", arg
, 7)) {
278 if (!strcmp("-q", arg
)) {
282 if (!strcmp("-v", arg
)) {
286 usage(fetch_pack_usage
);
289 heads
= argv
+ i
+ 1;
290 nr_heads
= argc
- i
- 1;
294 usage(fetch_pack_usage
);
295 pid
= git_connect(fd
, dest
, exec
);
298 ret
= fetch_pack(fd
, nr_heads
, heads
);