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 if (has_sha1_file(remote) &&
40 ((o = parse_object(remote)) != NULL) &&
41 (o->flags & COMPLETE)) {
42 struct commit_list *p;
43 struct commit *commit =
44 (struct commit *) (o = deref_tag(o));
47 if (o->type != commit_type)
51 rev_command_len + 44 < sizeof(rev_command)) {
52 snprintf(rev_command + rev_command_len, 44,
54 sha1_to_hex(p->item->object.sha1));
55 rev_command_len += 43;
61 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
68 revs = popen(rev_command, "r");
70 die("unable to run 'git-rev-list'");
74 while (fgets(line, sizeof(line), revs) != NULL) {
75 unsigned char sha1[20];
76 if (get_sha1_hex(line, sha1))
77 die("git-fetch-pack: expected object name, got crud");
78 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
80 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
81 if (!(31 & ++count)) {
86 * We keep one window "ahead" of the other side, and
87 * will wait for an ACK only on the next one
91 if (get_ack(fd[0], result_sha1)) {
95 fprintf(stderr, "got ack\n");
102 packet_write(fd[1], "done\n");
104 fprintf(stderr, "done\n");
107 if (get_ack(fd[0], result_sha1)) {
109 fprintf(stderr, "got ack\n");
116 static struct commit_list *complete = NULL;
118 static int mark_complete(const char *path, const unsigned char *sha1)
120 struct object *o = parse_object(sha1);
122 while (o && o->type == tag_type) {
123 o->flags |= COMPLETE;
124 o = parse_object(((struct tag *)o)->tagged->sha1);
126 if (o->type == commit_type) {
127 struct commit *commit = (struct commit *)o;
128 commit->object.flags |= COMPLETE;
129 insert_by_date(commit, &complete);
134 static void mark_recent_complete_commits(unsigned long cutoff)
136 while (complete && cutoff <= complete->item->date) {
138 fprintf(stderr, "Marking %s as complete\n",
139 sha1_to_hex(complete->item->object.sha1));
140 pop_most_recent_commit(&complete, COMPLETE);
144 static int everything_local(struct ref *refs)
148 unsigned long cutoff = 0;
150 track_object_refs = 0;
151 save_commit_buffer = 0;
153 for (ref = refs; ref; ref = ref->next) {
156 o = parse_object(ref->old_sha1);
160 /* We already have it -- which may mean that we were
161 * in sync with the other side at some time after
162 * that (it is OK if we guess wrong here).
164 if (o->type == commit_type) {
165 struct commit *commit = (struct commit *)o;
166 if (!cutoff || cutoff < commit->date)
167 cutoff = commit->date;
171 for_each_ref(mark_complete);
173 mark_recent_complete_commits(cutoff);
175 for (retval = 1; refs ; refs = refs->next) {
176 const unsigned char *remote = refs->old_sha1;
177 unsigned char local[20];
180 o = parse_object(remote);
181 if (!o || !(o->flags & COMPLETE)) {
186 "want %s (%s)\n", sha1_to_hex(remote),
191 memcpy(refs->new_sha1, local, 20);
195 "already have %s (%s)\n", sha1_to_hex(remote),
201 static int fetch_pack(int fd[2], int nr_match, char **match)
204 unsigned char sha1[20];
208 get_remote_heads(fd[0], &ref, nr_match, match, 1);
211 die("no matching remote head");
213 if (everything_local(ref)) {
217 if (find_common(fd, sha1, ref) < 0)
218 fprintf(stderr, "warning: no common commits\n");
221 die("git-fetch-pack: unable to fork off git-unpack-objects");
226 execlp("git-unpack-objects", "git-unpack-objects",
227 quiet ? "-q" : NULL, NULL);
228 die("git-unpack-objects exec failed");
232 while (waitpid(pid, &status, 0) < 0) {
234 die("waiting for git-unpack-objects: %s", strerror(errno));
236 if (WIFEXITED(status)) {
237 int code = WEXITSTATUS(status);
239 die("git-unpack-objects died with error code %d", code);
243 sha1_to_hex(ref->old_sha1), ref->name);
248 if (WIFSIGNALED(status)) {
249 int sig = WTERMSIG(status);
250 die("git-unpack-objects died of signal %d", sig);
252 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
255 int main(int argc, char **argv)
257 int i, ret, nr_heads;
258 char *dest = NULL, **heads;
264 for (i = 1; i < argc; i++) {
268 if (!strncmp("--exec=", arg, 7)) {
272 if (!strcmp("-q", arg)) {
276 if (!strcmp("-v", arg)) {
280 usage(fetch_pack_usage);
283 heads = argv + i + 1;
284 nr_heads = argc - i - 1;
288 usage(fetch_pack_usage);
289 pid = git_connect(fd, dest, exec);
292 ret = fetch_pack(fd, nr_heads, heads);