4ee91f2ca2f7f06543a636f728adc8975cba6747
[git.git] / fetch-pack.c
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>
8
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";
14
15 #define COMPLETE        (1U << 0)
16
17 static int find_common(int fd[2], unsigned char *result_sha1,
18                        struct ref *refs)
19 {
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;
25
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;
31                 struct object *o;
32
33                 /*
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
37                  * about.
38                  */
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));
45                         if (!o)
46                                 goto repair;
47                         if (o->type != commit_type)
48                                 continue;
49                         p = commit->parents;
50                         while (p &&
51                                rev_command_len + 44 < sizeof(rev_command)) {
52                                 snprintf(rev_command + rev_command_len, 44,
53                                          " ^%s",
54                                          sha1_to_hex(p->item->object.sha1));
55                                 rev_command_len += 43;
56                                 p = p->next;
57                         }
58                         continue;
59                 }
60         repair:
61                 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
62                 fetching++;
63         }
64         packet_flush(fd[1]);
65         if (!fetching)
66                 return 1;
67
68         revs = popen(rev_command, "r");
69         if (!revs)
70                 die("unable to run 'git-rev-list'");
71
72         flushes = 1;
73         retval = -1;
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));
79                 if (verbose)
80                         fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
81                 if (!(31 & ++count)) {
82                         packet_flush(fd[1]);
83                         flushes++;
84
85                         /*
86                          * We keep one window "ahead" of the other side, and
87                          * will wait for an ACK only on the next one
88                          */
89                         if (count == 32)
90                                 continue;
91                         if (get_ack(fd[0], result_sha1)) {
92                                 flushes = 0;
93                                 retval = 0;
94                                 if (verbose)
95                                         fprintf(stderr, "got ack\n");
96                                 break;
97                         }
98                         flushes--;
99                 }
100         }
101         pclose(revs);
102         packet_write(fd[1], "done\n");
103         if (verbose)
104                 fprintf(stderr, "done\n");
105         while (flushes) {
106                 flushes--;
107                 if (get_ack(fd[0], result_sha1)) {
108                         if (verbose)
109                                 fprintf(stderr, "got ack\n");
110                         return 0;
111                 }
112         }
113         return retval;
114 }
115
116 static struct commit_list *complete = NULL;
117
118 static int mark_complete(const char *path, const unsigned char *sha1)
119 {
120         struct object *o = parse_object(sha1);
121
122         while (o && o->type == tag_type) {
123                 o->flags |= COMPLETE;
124                 o = parse_object(((struct tag *)o)->tagged->sha1);
125         }
126         if (o->type == commit_type) {
127                 struct commit *commit = (struct commit *)o;
128                 commit->object.flags |= COMPLETE;
129                 insert_by_date(commit, &complete);
130         }
131         return 0;
132 }
133
134 static void mark_recent_complete_commits(unsigned long cutoff)
135 {
136         while (complete && cutoff <= complete->item->date) {
137                 if (verbose)
138                         fprintf(stderr, "Marking %s as complete\n",
139                                 sha1_to_hex(complete->item->object.sha1));
140                 pop_most_recent_commit(&complete, COMPLETE);
141         }
142 }
143
144 static int everything_local(struct ref *refs)
145 {
146         struct ref *ref;
147         int retval;
148         unsigned long cutoff = 0;
149
150         track_object_refs = 0;
151         save_commit_buffer = 0;
152
153         for (ref = refs; ref; ref = ref->next) {
154                 struct object *o;
155
156                 o = parse_object(ref->old_sha1);
157                 if (!o)
158                         continue;
159
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).
163                  */
164                 if (o->type == commit_type) {
165                         struct commit *commit = (struct commit *)o;
166                         if (!cutoff || cutoff < commit->date)
167                                 cutoff = commit->date;
168                 }
169         }
170
171         for_each_ref(mark_complete);
172         if (cutoff)
173                 mark_recent_complete_commits(cutoff);
174
175         for (retval = 1; refs ; refs = refs->next) {
176                 const unsigned char *remote = refs->old_sha1;
177                 unsigned char local[20];
178                 struct object *o;
179
180                 o = parse_object(remote);
181                 if (!o || !(o->flags & COMPLETE)) {
182                         retval = 0;
183                         if (!verbose)
184                                 continue;
185                         fprintf(stderr,
186                                 "want %s (%s)\n", sha1_to_hex(remote),
187                                 refs->name);
188                         continue;
189                 }
190
191                 memcpy(refs->new_sha1, local, 20);
192                 if (!verbose)
193                         continue;
194                 fprintf(stderr,
195                         "already have %s (%s)\n", sha1_to_hex(remote),
196                         refs->name);
197         }
198         return retval;
199 }
200
201 static int fetch_pack(int fd[2], int nr_match, char **match)
202 {
203         struct ref *ref;
204         unsigned char sha1[20];
205         int status;
206         pid_t pid;
207
208         get_remote_heads(fd[0], &ref, nr_match, match, 1);
209         if (!ref) {
210                 packet_flush(fd[1]);
211                 die("no matching remote head");
212         }
213         if (everything_local(ref)) {
214                 packet_flush(fd[1]);
215                 goto all_done;
216         }
217         if (find_common(fd, sha1, ref) < 0)
218                 fprintf(stderr, "warning: no common commits\n");
219         pid = fork();
220         if (pid < 0)
221                 die("git-fetch-pack: unable to fork off git-unpack-objects");
222         if (!pid) {
223                 dup2(fd[0], 0);
224                 close(fd[0]);
225                 close(fd[1]);
226                 execlp("git-unpack-objects", "git-unpack-objects",
227                        quiet ? "-q" : NULL, NULL);
228                 die("git-unpack-objects exec failed");
229         }
230         close(fd[0]);
231         close(fd[1]);
232         while (waitpid(pid, &status, 0) < 0) {
233                 if (errno != EINTR)
234                         die("waiting for git-unpack-objects: %s", strerror(errno));
235         }
236         if (WIFEXITED(status)) {
237                 int code = WEXITSTATUS(status);
238                 if (code)
239                         die("git-unpack-objects died with error code %d", code);
240 all_done:
241                 while (ref) {
242                         printf("%s %s\n",
243                                sha1_to_hex(ref->old_sha1), ref->name);
244                         ref = ref->next;
245                 }
246                 return 0;
247         }
248         if (WIFSIGNALED(status)) {
249                 int sig = WTERMSIG(status);
250                 die("git-unpack-objects died of signal %d", sig);
251         }
252         die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
253 }
254
255 int main(int argc, char **argv)
256 {
257         int i, ret, nr_heads;
258         char *dest = NULL, **heads;
259         int fd[2];
260         pid_t pid;
261
262         nr_heads = 0;
263         heads = NULL;
264         for (i = 1; i < argc; i++) {
265                 char *arg = argv[i];
266
267                 if (*arg == '-') {
268                         if (!strncmp("--exec=", arg, 7)) {
269                                 exec = arg + 7;
270                                 continue;
271                         }
272                         if (!strcmp("-q", arg)) {
273                                 quiet = 1;
274                                 continue;
275                         }
276                         if (!strcmp("-v", arg)) {
277                                 verbose = 1;
278                                 continue;
279                         }
280                         usage(fetch_pack_usage);
281                 }
282                 dest = arg;
283                 heads = argv + i + 1;
284                 nr_heads = argc - i - 1;
285                 break;
286         }
287         if (!dest)
288                 usage(fetch_pack_usage);
289         pid = git_connect(fd, dest, exec);
290         if (pid < 0)
291                 return 1;
292         ret = fetch_pack(fd, nr_heads, heads);
293         close(fd[0]);
294         close(fd[1]);
295         finish_connect(pid);
296         return ret;
297 }