]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-bundle.c
git-bundle: avoid packing objects which are in the prerequisites
[thirdparty/git.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 void show_commit(struct commit *commit)
261 {
262 write_or_die(1, sha1_to_hex(commit->object.sha1), 40);
263 write_or_die(1, "\n", 1);
264 if (commit->parents) {
265 free_commit_list(commit->parents);
266 commit->parents = NULL;
267 }
268 }
269
270 static void show_object(struct object_array_entry *p)
271 {
272 /* An object with name "foo\n0000000..." can be used to
273 * confuse downstream git-pack-objects very badly.
274 */
275 const char *ep = strchr(p->name, '\n');
276 int len = ep ? ep - p->name : strlen(p->name);
277 write_or_die(1, sha1_to_hex(p->item->sha1), 40);
278 write_or_die(1, " ", 1);
279 if (len)
280 write_or_die(1, p->name, len);
281 write_or_die(1, "\n", 1);
282 }
283
284 static void show_edge(struct commit *commit)
285 {
286 ; /* nothing to do */
287 }
288
289 static int create_bundle(struct bundle_header *header, const char *path,
290 int argc, const char **argv)
291 {
292 int bundle_fd = -1;
293 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
294 const char **argv_pack = xmalloc(4 * sizeof(const char *));
295 int pid, in, out, i, status;
296 char buffer[1024];
297 struct rev_info revs;
298 struct object_array tips;
299
300 bundle_fd = (!strcmp(path, "-") ? 1 :
301 open(path, O_CREAT | O_WRONLY, 0666));
302 if (bundle_fd < 0)
303 return error("Could not write to '%s'", path);
304
305 /* write signature */
306 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
307
308 /* init revs to list objects for pack-objects later */
309 save_commit_buffer = 0;
310 init_revisions(&revs, NULL);
311
312 /* write prerequisites */
313 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
314 argv_boundary[0] = "rev-list";
315 argv_boundary[1] = "--boundary";
316 argv_boundary[2] = "--pretty=oneline";
317 argv_boundary[argc + 2] = NULL;
318 out = -1;
319 pid = fork_with_pipe(argv_boundary, NULL, &out);
320 if (pid < 0)
321 return -1;
322 while ((i = read_string(out, buffer, sizeof(buffer))) > 0)
323 if (buffer[0] == '-') {
324 unsigned char sha1[20];
325 write_or_die(bundle_fd, buffer, i);
326 if (!get_sha1_hex(buffer + 1, sha1)) {
327 struct object *object = parse_object(sha1);
328 object->flags |= UNINTERESTING;
329 add_pending_object(&revs, object, buffer);
330 }
331 }
332 while ((i = waitpid(pid, &status, 0)) < 0)
333 if (errno != EINTR)
334 return error("rev-list died");
335 if (!WIFEXITED(status) || WEXITSTATUS(status))
336 return error("rev-list died %d", WEXITSTATUS(status));
337
338 /* write references */
339 revs.tag_objects = 1;
340 revs.tree_objects = 1;
341 revs.blob_objects = 1;
342 argc = setup_revisions(argc, argv, &revs, NULL);
343 if (argc > 1)
344 return error("unrecognized argument: %s'", argv[1]);
345
346 memset(&tips, 0, sizeof(tips));
347 for (i = 0; i < revs.pending.nr; i++) {
348 struct object_array_entry *e = revs.pending.objects + i;
349 unsigned char sha1[20];
350 char *ref;
351
352 if (e->item->flags & UNINTERESTING)
353 continue;
354 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
355 continue;
356 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
357 write_or_die(bundle_fd, " ", 1);
358 write_or_die(bundle_fd, ref, strlen(ref));
359 write_or_die(bundle_fd, "\n", 1);
360 add_object_array(e->item, e->name, &tips);
361 free(ref);
362 }
363
364 /* end header */
365 write_or_die(bundle_fd, "\n", 1);
366
367 /* write pack */
368 argv_pack[0] = "pack-objects";
369 argv_pack[1] = "--all-progress";
370 argv_pack[2] = "--stdout";
371 argv_pack[3] = NULL;
372 in = -1;
373 out = bundle_fd;
374 pid = fork_with_pipe(argv_pack, &in, &out);
375 if (pid < 0)
376 return error("Could not spawn pack-objects");
377 close(1);
378 dup2(in, 1);
379 close(in);
380 prepare_revision_walk(&revs);
381 mark_edges_uninteresting(revs.commits, &revs, show_edge);
382 traverse_commit_list(&revs, show_commit, show_object);
383 close(1);
384 while (waitpid(pid, &status, 0) < 0)
385 if (errno != EINTR)
386 return -1;
387 if (!WIFEXITED(status) || WEXITSTATUS(status))
388 return error ("pack-objects died");
389
390 /*
391 * Make sure the refs we wrote out is correct; --max-count and
392 * other limiting options could have prevented all the tips
393 * from getting output.
394 */
395 status = 0;
396 for (i = 0; i < tips.nr; i++) {
397 if (!(tips.objects[i].item->flags & SHOWN)) {
398 status = 1;
399 error("%s: not included in the resulting pack",
400 tips.objects[i].name);
401 }
402 }
403
404 return status;
405 }
406
407 static int unbundle(struct bundle_header *header, int bundle_fd,
408 int argc, const char **argv)
409 {
410 const char *argv_index_pack[] = {"index-pack", "--stdin", NULL};
411 int pid, status, dev_null;
412
413 if (verify_bundle(header, 0))
414 return -1;
415 dev_null = open("/dev/null", O_WRONLY);
416 if (dev_null < 0)
417 return error("Could not open /dev/null");
418 pid = fork_with_pipe(argv_index_pack, &bundle_fd, &dev_null);
419 if (pid < 0)
420 return error("Could not spawn index-pack");
421 while (waitpid(pid, &status, 0) < 0)
422 if (errno != EINTR)
423 return error("index-pack died");
424 if (!WIFEXITED(status) || WEXITSTATUS(status))
425 return error("index-pack exited with status %d",
426 WEXITSTATUS(status));
427 return list_heads(header, argc, argv);
428 }
429
430 int cmd_bundle(int argc, const char **argv, const char *prefix)
431 {
432 struct bundle_header header;
433 int nongit = 0;
434 const char *cmd, *bundle_file;
435 int bundle_fd = -1;
436 char buffer[PATH_MAX];
437
438 if (argc < 3)
439 usage(bundle_usage);
440
441 cmd = argv[1];
442 bundle_file = argv[2];
443 argc -= 2;
444 argv += 2;
445
446 prefix = setup_git_directory_gently(&nongit);
447 if (prefix && bundle_file[0] != '/') {
448 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
449 bundle_file = buffer;
450 }
451
452 memset(&header, 0, sizeof(header));
453 if (strcmp(cmd, "create") &&
454 (bundle_fd = read_header(bundle_file, &header)) < 0)
455 return 1;
456
457 if (!strcmp(cmd, "verify")) {
458 close(bundle_fd);
459 if (verify_bundle(&header, 1))
460 return 1;
461 fprintf(stderr, "%s is okay\n", bundle_file);
462 return 0;
463 }
464 if (!strcmp(cmd, "list-heads")) {
465 close(bundle_fd);
466 return !!list_heads(&header, argc, argv);
467 }
468 if (!strcmp(cmd, "create")) {
469 if (nongit)
470 die("Need a repository to create a bundle.");
471 return !!create_bundle(&header, bundle_file, argc, argv);
472 } else if (!strcmp(cmd, "unbundle")) {
473 if (nongit)
474 die("Need a repository to unbundle.");
475 return !!unbundle(&header, bundle_fd, argc, argv);
476 } else
477 usage(bundle_usage);
478 }