[PATCH] Do not send "want" lines for complete objects
[git/dscho.git] / fetch-pack.c
blob9dfd072db786a62d7d971a1b98f539e68ec364fc
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include "commit.h"
5 #include "tag.h"
6 #include <time.h>
7 #include <sys/wait.h>
9 static int quiet;
10 static int verbose;
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,
18 struct ref *refs)
20 int fetching;
21 static char line[1000];
22 static char rev_command[1024];
23 int count = 0, flushes = 0, retval, rev_command_len;
24 FILE *revs;
26 strcpy(rev_command, "git-rev-list $(git-rev-parse --all)");
27 rev_command_len = strlen(rev_command);
28 fetching = 0;
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;
44 continue;
47 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
48 fetching++;
50 packet_flush(fd[1]);
51 if (!fetching)
52 return 1;
54 revs = popen(rev_command, "r");
55 if (!revs)
56 die("unable to run 'git-rev-list'");
58 flushes = 1;
59 retval = -1;
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));
65 if (verbose)
66 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
67 if (!(31 & ++count)) {
68 packet_flush(fd[1]);
69 flushes++;
72 * We keep one window "ahead" of the other side, and
73 * will wait for an ACK only on the next one
75 if (count == 32)
76 continue;
77 if (get_ack(fd[0], result_sha1)) {
78 flushes = 0;
79 retval = 0;
80 if (verbose)
81 fprintf(stderr, "got ack\n");
82 break;
84 flushes--;
87 pclose(revs);
88 packet_write(fd[1], "done\n");
89 if (verbose)
90 fprintf(stderr, "done\n");
91 while (flushes) {
92 flushes--;
93 if (get_ack(fd[0], result_sha1)) {
94 if (verbose)
95 fprintf(stderr, "got ack\n");
96 return 0;
99 return retval;
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);
117 return 0;
120 static void mark_recent_complete_commits(unsigned long cutoff)
122 while (complete && cutoff <= complete->item->date) {
123 if (verbose)
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)
132 struct ref *ref;
133 int retval;
134 unsigned long cutoff = 0;
136 track_object_refs = 0;
137 save_commit_buffer = 0;
139 for (ref = refs; ref; ref = ref->next) {
140 struct object *o;
142 o = parse_object(ref->old_sha1);
143 if (!o)
144 continue;
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);
158 if (cutoff)
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];
164 struct object *o;
166 o = parse_object(remote);
167 if (!o || !(o->flags & COMPLETE)) {
168 retval = 0;
169 if (!verbose)
170 continue;
171 fprintf(stderr,
172 "want %s (%s)\n", sha1_to_hex(remote),
173 refs->name);
174 continue;
177 memcpy(refs->new_sha1, local, 20);
178 if (!verbose)
179 continue;
180 fprintf(stderr,
181 "already have %s (%s)\n", sha1_to_hex(remote),
182 refs->name);
184 return retval;
187 static int fetch_pack(int fd[2], int nr_match, char **match)
189 struct ref *ref;
190 unsigned char sha1[20];
191 int status;
192 pid_t pid;
194 get_remote_heads(fd[0], &ref, nr_match, match, 1);
195 if (!ref) {
196 packet_flush(fd[1]);
197 die("no matching remote head");
199 if (everything_local(ref)) {
200 packet_flush(fd[1]);
201 goto all_done;
203 if (find_common(fd, sha1, ref) < 0)
204 fprintf(stderr, "warning: no common commits\n");
205 pid = fork();
206 if (pid < 0)
207 die("git-fetch-pack: unable to fork off git-unpack-objects");
208 if (!pid) {
209 dup2(fd[0], 0);
210 close(fd[0]);
211 close(fd[1]);
212 execlp("git-unpack-objects", "git-unpack-objects",
213 quiet ? "-q" : NULL, NULL);
214 die("git-unpack-objects exec failed");
216 close(fd[0]);
217 close(fd[1]);
218 while (waitpid(pid, &status, 0) < 0) {
219 if (errno != EINTR)
220 die("waiting for git-unpack-objects: %s", strerror(errno));
222 if (WIFEXITED(status)) {
223 int code = WEXITSTATUS(status);
224 if (code)
225 die("git-unpack-objects died with error code %d", code);
226 all_done:
227 while (ref) {
228 printf("%s %s\n",
229 sha1_to_hex(ref->old_sha1), ref->name);
230 ref = ref->next;
232 return 0;
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;
245 int fd[2];
246 pid_t pid;
248 nr_heads = 0;
249 heads = NULL;
250 for (i = 1; i < argc; i++) {
251 char *arg = argv[i];
253 if (*arg == '-') {
254 if (!strncmp("--exec=", arg, 7)) {
255 exec = arg + 7;
256 continue;
258 if (!strcmp("-q", arg)) {
259 quiet = 1;
260 continue;
262 if (!strcmp("-v", arg)) {
263 verbose = 1;
264 continue;
266 usage(fetch_pack_usage);
268 dest = arg;
269 heads = argv + i + 1;
270 nr_heads = argc - i - 1;
271 break;
273 if (!dest)
274 usage(fetch_pack_usage);
275 pid = git_connect(fd, dest, exec);
276 if (pid < 0)
277 return 1;
278 ret = fetch_pack(fd, nr_heads, heads);
279 close(fd[0]);
280 close(fd[1]);
281 finish_connect(pid);
282 return ret;