Merge branch 'master' of git://repo.or.cz/git-gui
[git] / builtin-bundle.c
1 #include "cache.h"
2 #include "object.h"
3 #include "commit.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "list-objects.h"
7 #include "exec_cmd.h"
8
9 /*
10  * Basic handler for bundle files to connect repositories via sneakernet.
11  * Invocation must include action.
12  * This function can create a bundle or provide information on an existing
13  * bundle supporting git-fetch, git-pull, and git-ls-remote
14  */
15
16 static const char *bundle_usage="git-bundle (create <bundle> <git-rev-list args> | verify <bundle> | list-heads <bundle> [refname]... | unbundle <bundle> [refname]... )";
17
18 static const char bundle_signature[] = "# v2 git bundle\n";
19
20 struct ref_list {
21         unsigned int nr, alloc;
22         struct ref_list_entry {
23                 unsigned char sha1[20];
24                 char *name;
25         } *list;
26 };
27
28 static void add_to_ref_list(const unsigned char *sha1, const char *name,
29                 struct ref_list *list)
30 {
31         if (list->nr + 1 >= list->alloc) {
32                 list->alloc = alloc_nr(list->nr + 1);
33                 list->list = xrealloc(list->list,
34                                 list->alloc * sizeof(list->list[0]));
35         }
36         memcpy(list->list[list->nr].sha1, sha1, 20);
37         list->list[list->nr].name = xstrdup(name);
38         list->nr++;
39 }
40
41 struct bundle_header {
42         struct ref_list prerequisites;
43         struct ref_list references;
44 };
45
46 /* this function returns the length of the string */
47 static int read_string(int fd, char *buffer, int size)
48 {
49         int i;
50         for (i = 0; i < size - 1; i++) {
51                 int count = xread(fd, buffer + i, 1);
52                 if (count < 0)
53                         return error("Read error: %s", strerror(errno));
54                 if (count == 0) {
55                         i--;
56                         break;
57                 }
58                 if (buffer[i] == '\n')
59                         break;
60         }
61         buffer[i + 1] = '\0';
62         return i + 1;
63 }
64
65 /* returns an fd */
66 static int read_header(const char *path, struct bundle_header *header) {
67         char buffer[1024];
68         int fd = open(path, O_RDONLY);
69
70         if (fd < 0)
71                 return error("could not open '%s'", path);
72         if (read_string(fd, buffer, sizeof(buffer)) < 0 ||
73                         strcmp(buffer, bundle_signature)) {
74                 close(fd);
75                 return error("'%s' does not look like a v2 bundle file", path);
76         }
77         while (read_string(fd, buffer, sizeof(buffer)) > 0
78                         && buffer[0] != '\n') {
79                 int is_prereq = buffer[0] == '-';
80                 int offset = is_prereq ? 1 : 0;
81                 int len = strlen(buffer);
82                 unsigned char sha1[20];
83                 struct ref_list *list = is_prereq ? &header->prerequisites
84                         : &header->references;
85                 char delim;
86
87                 if (buffer[len - 1] == '\n')
88                         buffer[len - 1] = '\0';
89                 if (get_sha1_hex(buffer + offset, sha1)) {
90                         warn("unrecognized header: %s", buffer);
91                         continue;
92                 }
93                 delim = buffer[40 + offset];
94                 if (!isspace(delim) && (delim != '\0' || !is_prereq))
95                         die ("invalid header: %s", buffer);
96                 add_to_ref_list(sha1, isspace(delim) ?
97                                 buffer + 41 + offset : "", list);
98         }
99         return fd;
100 }
101
102 /* if in && *in >= 0, take that as input file descriptor instead */
103 static int fork_with_pipe(const char **argv, int *in, int *out)
104 {
105         int needs_in, needs_out;
106         int fdin[2], fdout[2], pid;
107
108         needs_in = in && *in < 0;
109         if (needs_in) {
110                 if (pipe(fdin) < 0)
111                         return error("could not setup pipe");
112                 *in = fdin[1];
113         }
114
115         needs_out = out && *out < 0;
116         if (needs_out) {
117                 if (pipe(fdout) < 0)
118                         return error("could not setup pipe");
119                 *out = fdout[0];
120         }
121
122         if ((pid = fork()) < 0) {
123                 if (needs_in) {
124                         close(fdin[0]);
125                         close(fdin[1]);
126                 }
127                 if (needs_out) {
128                         close(fdout[0]);
129                         close(fdout[1]);
130                 }
131                 return error("could not fork");
132         }
133         if (!pid) {
134                 if (needs_in) {
135                         dup2(fdin[0], 0);
136                         close(fdin[0]);
137                         close(fdin[1]);
138                 } else if (in) {
139                         dup2(*in, 0);
140                         close(*in);
141                 }
142                 if (needs_out) {
143                         dup2(fdout[1], 1);
144                         close(fdout[0]);
145                         close(fdout[1]);
146                 } else if (out) {
147                         dup2(*out, 1);
148                         close(*out);
149                 }
150                 exit(execv_git_cmd(argv));
151         }
152         if (needs_in)
153                 close(fdin[0]);
154         else if (in)
155                 close(*in);
156         if (needs_out)
157                 close(fdout[1]);
158         else if (out)
159                 close(*out);
160         return pid;
161 }
162
163 static int list_refs(struct ref_list *r, int argc, const char **argv)
164 {
165         int i;
166
167         for (i = 0; i < r->nr; i++) {
168                 if (argc > 1) {
169                         int j;
170                         for (j = 1; j < argc; j++)
171                                 if (!strcmp(r->list[i].name, argv[j]))
172                                         break;
173                         if (j == argc)
174                                 continue;
175                 }
176                 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
177                                 r->list[i].name);
178         }
179         return 0;
180 }
181
182 #define PREREQ_MARK (1u<<16)
183
184 static int verify_bundle(struct bundle_header *header, int verbose)
185 {
186         /*
187          * Do fast check, then if any prereqs are missing then go line by line
188          * to be verbose about the errors
189          */
190         struct ref_list *p = &header->prerequisites;
191         struct rev_info revs;
192         const char *argv[] = {NULL, "--all"};
193         struct object_array refs;
194         struct commit *commit;
195         int i, ret = 0, req_nr;
196         const char *message = "Repository lacks these prerequisite commits:";
197
198         init_revisions(&revs, NULL);
199         for (i = 0; i < p->nr; i++) {
200                 struct ref_list_entry *e = p->list + i;
201                 struct object *o = parse_object(e->sha1);
202                 if (o) {
203                         o->flags |= PREREQ_MARK;
204                         add_pending_object(&revs, o, e->name);
205                         continue;
206                 }
207                 if (++ret == 1)
208                         error(message);
209                 error("%s %s", sha1_to_hex(e->sha1), e->name);
210         }
211         if (revs.pending.nr != p->nr)
212                 return ret;
213         req_nr = revs.pending.nr;
214         setup_revisions(2, argv, &revs, NULL);
215
216         memset(&refs, 0, sizeof(struct object_array));
217         for (i = 0; i < revs.pending.nr; i++) {
218                 struct object_array_entry *e = revs.pending.objects + i;
219                 add_object_array(e->item, e->name, &refs);
220         }
221
222         prepare_revision_walk(&revs);
223
224         i = req_nr;
225         while (i && (commit = get_revision(&revs)))
226                 if (commit->object.flags & PREREQ_MARK)
227                         i--;
228
229         for (i = 0; i < req_nr; i++)
230                 if (!(refs.objects[i].item->flags & SHOWN)) {
231                         if (++ret == 1)
232                                 error(message);
233                         error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
234                                 refs.objects[i].name);
235                 }
236
237         for (i = 0; i < refs.nr; i++)
238                 clear_commit_marks((struct commit *)refs.objects[i].item, -1);
239
240         if (verbose) {
241                 struct ref_list *r;
242
243                 r = &header->references;
244                 printf("The bundle contains %d ref%s\n",
245                        r->nr, (1 < r->nr) ? "s" : "");
246                 list_refs(r, 0, NULL);
247                 r = &header->prerequisites;
248                 printf("The bundle requires these %d ref%s\n",
249                        r->nr, (1 < r->nr) ? "s" : "");
250                 list_refs(r, 0, NULL);
251         }
252         return ret;
253 }
254
255 static int list_heads(struct bundle_header *header, int argc, const char **argv)
256 {
257         return list_refs(&header->references, argc, argv);
258 }
259
260 static int create_bundle(struct bundle_header *header, const char *path,
261                 int argc, const char **argv)
262 {
263         int bundle_fd = -1;
264         const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
265         const char **argv_pack = xmalloc(5 * sizeof(const char *));
266         int pid, in, out, i, status, ref_count = 0;
267         char buffer[1024];
268         struct rev_info revs;
269
270         bundle_fd = (!strcmp(path, "-") ? 1 :
271                         open(path, O_CREAT | O_EXCL | O_WRONLY, 0666));
272         if (bundle_fd < 0)
273                 return error("Could not create '%s': %s", path, strerror(errno));
274
275         /* write signature */
276         write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
277
278         /* init revs to list objects for pack-objects later */
279         save_commit_buffer = 0;
280         init_revisions(&revs, NULL);
281
282         /* write prerequisites */
283         memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
284         argv_boundary[0] = "rev-list";
285         argv_boundary[1] = "--boundary";
286         argv_boundary[2] = "--pretty=oneline";
287         argv_boundary[argc + 2] = NULL;
288         out = -1;
289         pid = fork_with_pipe(argv_boundary, NULL, &out);
290         if (pid < 0)
291                 return -1;
292         while ((i = read_string(out, buffer, sizeof(buffer))) > 0) {
293                 unsigned char sha1[20];
294                 if (buffer[0] == '-') {
295                         write_or_die(bundle_fd, buffer, i);
296                         if (!get_sha1_hex(buffer + 1, sha1)) {
297                                 struct object *object = parse_object(sha1);
298                                 object->flags |= UNINTERESTING;
299                                 add_pending_object(&revs, object, buffer);
300                         }
301                 } else if (!get_sha1_hex(buffer, sha1)) {
302                         struct object *object = parse_object(sha1);
303                         object->flags |= SHOWN;
304                 }
305         }
306         while ((i = waitpid(pid, &status, 0)) < 0)
307                 if (errno != EINTR)
308                         return error("rev-list died");
309         if (!WIFEXITED(status) || WEXITSTATUS(status))
310                 return error("rev-list died %d", WEXITSTATUS(status));
311
312         /* write references */
313         argc = setup_revisions(argc, argv, &revs, NULL);
314         if (argc > 1)
315                 return error("unrecognized argument: %s'", argv[1]);
316
317         for (i = 0; i < revs.pending.nr; i++) {
318                 struct object_array_entry *e = revs.pending.objects + i;
319                 unsigned char sha1[20];
320                 char *ref;
321
322                 if (e->item->flags & UNINTERESTING)
323                         continue;
324                 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
325                         continue;
326                 /*
327                  * Make sure the refs we wrote out is correct; --max-count and
328                  * other limiting options could have prevented all the tips
329                  * from getting output.
330                  */
331                 if (!(e->item->flags & SHOWN)) {
332                         warn("ref '%s' is excluded by the rev-list options",
333                                 e->name);
334                         continue;
335                 }
336                 ref_count++;
337                 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
338                 write_or_die(bundle_fd, " ", 1);
339                 write_or_die(bundle_fd, ref, strlen(ref));
340                 write_or_die(bundle_fd, "\n", 1);
341                 free(ref);
342         }
343         if (!ref_count)
344                 die ("Refusing to create empty bundle.");
345
346         /* end header */
347         write_or_die(bundle_fd, "\n", 1);
348
349         /* write pack */
350         argv_pack[0] = "pack-objects";
351         argv_pack[1] = "--all-progress";
352         argv_pack[2] = "--stdout";
353         argv_pack[3] = "--thin";
354         argv_pack[4] = NULL;
355         in = -1;
356         out = bundle_fd;
357         pid = fork_with_pipe(argv_pack, &in, &out);
358         if (pid < 0)
359                 return error("Could not spawn pack-objects");
360         for (i = 0; i < revs.pending.nr; i++) {
361                 struct object *object = revs.pending.objects[i].item;
362                 if (object->flags & UNINTERESTING)
363                         write(in, "^", 1);
364                 write(in, sha1_to_hex(object->sha1), 40);
365                 write(in, "\n", 1);
366         }
367         close(in);
368         while (waitpid(pid, &status, 0) < 0)
369                 if (errno != EINTR)
370                         return -1;
371         if (!WIFEXITED(status) || WEXITSTATUS(status))
372                 return error ("pack-objects died");
373
374         return status;
375 }
376
377 static int unbundle(struct bundle_header *header, int bundle_fd,
378                 int argc, const char **argv)
379 {
380         const char *argv_index_pack[] = {"index-pack",
381                 "--fix-thin", "--stdin", NULL};
382         int pid, status, dev_null;
383
384         if (verify_bundle(header, 0))
385                 return -1;
386         dev_null = open("/dev/null", O_WRONLY);
387         if (dev_null < 0)
388                 return error("Could not open /dev/null");
389         pid = fork_with_pipe(argv_index_pack, &bundle_fd, &dev_null);
390         if (pid < 0)
391                 return error("Could not spawn index-pack");
392         while (waitpid(pid, &status, 0) < 0)
393                 if (errno != EINTR)
394                         return error("index-pack died");
395         if (!WIFEXITED(status) || WEXITSTATUS(status))
396                 return error("index-pack exited with status %d",
397                                 WEXITSTATUS(status));
398         return list_heads(header, argc, argv);
399 }
400
401 int cmd_bundle(int argc, const char **argv, const char *prefix)
402 {
403         struct bundle_header header;
404         int nongit = 0;
405         const char *cmd, *bundle_file;
406         int bundle_fd = -1;
407         char buffer[PATH_MAX];
408
409         if (argc < 3)
410                 usage(bundle_usage);
411
412         cmd = argv[1];
413         bundle_file = argv[2];
414         argc -= 2;
415         argv += 2;
416
417         prefix = setup_git_directory_gently(&nongit);
418         if (prefix && bundle_file[0] != '/') {
419                 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
420                 bundle_file = buffer;
421         }
422
423         memset(&header, 0, sizeof(header));
424         if (strcmp(cmd, "create") &&
425                         (bundle_fd = read_header(bundle_file, &header)) < 0)
426                 return 1;
427
428         if (!strcmp(cmd, "verify")) {
429                 close(bundle_fd);
430                 if (verify_bundle(&header, 1))
431                         return 1;
432                 fprintf(stderr, "%s is okay\n", bundle_file);
433                 return 0;
434         }
435         if (!strcmp(cmd, "list-heads")) {
436                 close(bundle_fd);
437                 return !!list_heads(&header, argc, argv);
438         }
439         if (!strcmp(cmd, "create")) {
440                 if (nongit)
441                         die("Need a repository to create a bundle.");
442                 return !!create_bundle(&header, bundle_file, argc, argv);
443         } else if (!strcmp(cmd, "unbundle")) {
444                 if (nongit)
445                         die("Need a repository to unbundle.");
446                 return !!unbundle(&header, bundle_fd, argc, argv);
447         } else
448                 usage(bundle_usage);
449 }