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
;
33 If that object is complete (i.e. it is a descendant of a
34 local ref), we don't want it, nor its descendants.
36 if (has_sha1_file(remote
)
37 && parse_object(remote
)->flags
& COMPLETE
) {
38 if (rev_command_len
+ 44 < sizeof(rev_command
)) {
39 snprintf(rev_command
+ rev_command_len
, 44,
40 " ^%s^", sha1_to_hex(remote
));
41 rev_command_len
+= 43;
47 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
54 revs
= popen(rev_command
, "r");
56 die("unable to run 'git-rev-list'");
60 while (fgets(line
, sizeof(line
), revs
) != NULL
) {
61 unsigned char sha1
[20];
62 if (get_sha1_hex(line
, sha1
))
63 die("git-fetch-pack: expected object name, got crud");
64 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
66 fprintf(stderr
, "have %s\n", sha1_to_hex(sha1
));
67 if (!(31 & ++count
)) {
72 * We keep one window "ahead" of the other side, and
73 * will wait for an ACK only on the next one
77 if (get_ack(fd
[0], result_sha1
)) {
81 fprintf(stderr
, "got ack\n");
88 packet_write(fd
[1], "done\n");
90 fprintf(stderr
, "done\n");
93 if (get_ack(fd
[0], result_sha1
)) {
95 fprintf(stderr
, "got ack\n");
102 static struct commit_list
*complete
= NULL
;
104 static int mark_complete(const char *path
, const unsigned char *sha1
)
106 struct object
*o
= parse_object(sha1
);
108 while (o
&& o
->type
== tag_type
) {
109 o
->flags
|= COMPLETE
;
110 o
= parse_object(((struct tag
*)o
)->tagged
->sha1
);
112 if (o
->type
== commit_type
) {
113 struct commit
*commit
= (struct commit
*)o
;
114 commit
->object
.flags
|= COMPLETE
;
115 insert_by_date(commit
, &complete
);
120 static void mark_recent_complete_commits(unsigned long cutoff
)
122 while (complete
&& cutoff
<= complete
->item
->date
) {
124 fprintf(stderr
, "Marking %s as complete\n",
125 sha1_to_hex(complete
->item
->object
.sha1
));
126 pop_most_recent_commit(&complete
, COMPLETE
);
130 static int everything_local(struct ref
*refs
)
134 unsigned long cutoff
= 0;
136 track_object_refs
= 0;
137 save_commit_buffer
= 0;
139 for (ref
= refs
; ref
; ref
= ref
->next
) {
142 o
= parse_object(ref
->old_sha1
);
146 /* We already have it -- which may mean that we were
147 * in sync with the other side at some time after
148 * that (it is OK if we guess wrong here).
150 if (o
->type
== commit_type
) {
151 struct commit
*commit
= (struct commit
*)o
;
152 if (!cutoff
|| cutoff
< commit
->date
)
153 cutoff
= commit
->date
;
157 for_each_ref(mark_complete
);
159 mark_recent_complete_commits(cutoff
);
161 for (retval
= 1; refs
; refs
= refs
->next
) {
162 const unsigned char *remote
= refs
->old_sha1
;
163 unsigned char local
[20];
166 o
= parse_object(remote
);
167 if (!o
|| !(o
->flags
& COMPLETE
)) {
172 "want %s (%s)\n", sha1_to_hex(remote
),
177 memcpy(refs
->new_sha1
, local
, 20);
181 "already have %s (%s)\n", sha1_to_hex(remote
),
187 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
190 unsigned char sha1
[20];
194 get_remote_heads(fd
[0], &ref
, nr_match
, match
, 1);
197 die("no matching remote head");
199 if (everything_local(ref
)) {
203 if (find_common(fd
, sha1
, ref
) < 0)
204 fprintf(stderr
, "warning: no common commits\n");
207 die("git-fetch-pack: unable to fork off git-unpack-objects");
212 execlp("git-unpack-objects", "git-unpack-objects",
213 quiet
? "-q" : NULL
, NULL
);
214 die("git-unpack-objects exec failed");
218 while (waitpid(pid
, &status
, 0) < 0) {
220 die("waiting for git-unpack-objects: %s", strerror(errno
));
222 if (WIFEXITED(status
)) {
223 int code
= WEXITSTATUS(status
);
225 die("git-unpack-objects died with error code %d", code
);
229 sha1_to_hex(ref
->old_sha1
), ref
->name
);
234 if (WIFSIGNALED(status
)) {
235 int sig
= WTERMSIG(status
);
236 die("git-unpack-objects died of signal %d", sig
);
238 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
241 int main(int argc
, char **argv
)
243 int i
, ret
, nr_heads
;
244 char *dest
= NULL
, **heads
;
250 for (i
= 1; i
< argc
; i
++) {
254 if (!strncmp("--exec=", arg
, 7)) {
258 if (!strcmp("-q", arg
)) {
262 if (!strcmp("-v", arg
)) {
266 usage(fetch_pack_usage
);
269 heads
= argv
+ i
+ 1;
270 nr_heads
= argc
- i
- 1;
274 usage(fetch_pack_usage
);
275 pid
= git_connect(fd
, dest
, exec
);
278 ret
= fetch_pack(fd
, nr_heads
, heads
);