libgit-thin: Update QUESTIONS file
[git/libgit-gsoc.git] / builtin-bundle.c
blobf4b4f034f4cd49f15551e92eb20a2b5412373d90
1 #include "builtin.h"
2 #include "cache.h"
3 #include "object.h"
4 #include "commit.h"
5 #include "diff.h"
6 #include "revision.h"
7 #include "list-objects.h"
8 #include "run-command.h"
11 * Basic handler for bundle files to connect repositories via sneakernet.
12 * Invocation must include action.
13 * This function can create a bundle or provide information on an existing
14 * bundle supporting git-fetch, git-pull, and git-ls-remote
17 static const char *bundle_usage="git-bundle (create <bundle> <git-rev-list args> | verify <bundle> | list-heads <bundle> [refname]... | unbundle <bundle> [refname]... )";
19 static const char bundle_signature[] = "# v2 git bundle\n";
21 struct ref_list {
22 unsigned int nr, alloc;
23 struct ref_list_entry {
24 unsigned char sha1[20];
25 char *name;
26 } *list;
29 static void add_to_ref_list(const unsigned char *sha1, const char *name,
30 struct ref_list *list)
32 if (list->nr + 1 >= list->alloc) {
33 list->alloc = alloc_nr(list->nr + 1);
34 list->list = xrealloc(list->list,
35 list->alloc * sizeof(list->list[0]));
37 memcpy(list->list[list->nr].sha1, sha1, 20);
38 list->list[list->nr].name = xstrdup(name);
39 list->nr++;
42 struct bundle_header {
43 struct ref_list prerequisites;
44 struct ref_list references;
47 /* returns an fd */
48 static int read_header(const char *path, struct bundle_header *header) {
49 char buffer[1024];
50 int fd;
51 long fpos;
52 FILE *ffd = fopen(path, "rb");
54 if (!ffd)
55 return error("could not open '%s'", path);
56 if (!fgets(buffer, sizeof(buffer), ffd) ||
57 strcmp(buffer, bundle_signature)) {
58 fclose(ffd);
59 return error("'%s' does not look like a v2 bundle file", path);
61 while (fgets(buffer, sizeof(buffer), ffd)
62 && buffer[0] != '\n') {
63 int is_prereq = buffer[0] == '-';
64 int offset = is_prereq ? 1 : 0;
65 int len = strlen(buffer);
66 unsigned char sha1[20];
67 struct ref_list *list = is_prereq ? &header->prerequisites
68 : &header->references;
69 char delim;
71 if (buffer[len - 1] == '\n')
72 buffer[len - 1] = '\0';
73 if (get_sha1_hex(buffer + offset, sha1)) {
74 warning("unrecognized header: %s", buffer);
75 continue;
77 delim = buffer[40 + offset];
78 if (!isspace(delim) && (delim != '\0' || !is_prereq))
79 die ("invalid header: %s", buffer);
80 add_to_ref_list(sha1, isspace(delim) ?
81 buffer + 41 + offset : "", list);
83 fpos = ftell(ffd);
84 fclose(ffd);
85 fd = open(path, O_RDONLY);
86 if (fd < 0)
87 return error("could not open '%s'", path);
88 lseek(fd, fpos, SEEK_SET);
89 return fd;
92 static int list_refs(struct ref_list *r, int argc, const char **argv)
94 int i;
96 for (i = 0; i < r->nr; i++) {
97 if (argc > 1) {
98 int j;
99 for (j = 1; j < argc; j++)
100 if (!strcmp(r->list[i].name, argv[j]))
101 break;
102 if (j == argc)
103 continue;
105 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
106 r->list[i].name);
108 return 0;
111 #define PREREQ_MARK (1u<<16)
113 static int verify_bundle(struct bundle_header *header, int verbose)
116 * Do fast check, then if any prereqs are missing then go line by line
117 * to be verbose about the errors
119 struct ref_list *p = &header->prerequisites;
120 struct rev_info revs;
121 const char *argv[] = {NULL, "--all"};
122 struct object_array refs;
123 struct commit *commit;
124 int i, ret = 0, req_nr;
125 const char *message = "Repository lacks these prerequisite commits:";
127 init_revisions(&revs, NULL);
128 for (i = 0; i < p->nr; i++) {
129 struct ref_list_entry *e = p->list + i;
130 struct object *o = parse_object(e->sha1);
131 if (o) {
132 o->flags |= PREREQ_MARK;
133 add_pending_object(&revs, o, e->name);
134 continue;
136 if (++ret == 1)
137 error(message);
138 error("%s %s", sha1_to_hex(e->sha1), e->name);
140 if (revs.pending.nr != p->nr)
141 return ret;
142 req_nr = revs.pending.nr;
143 setup_revisions(2, argv, &revs, NULL);
145 memset(&refs, 0, sizeof(struct object_array));
146 for (i = 0; i < revs.pending.nr; i++) {
147 struct object_array_entry *e = revs.pending.objects + i;
148 add_object_array(e->item, e->name, &refs);
151 prepare_revision_walk(&revs);
153 i = req_nr;
154 while (i && (commit = get_revision(&revs)))
155 if (commit->object.flags & PREREQ_MARK)
156 i--;
158 for (i = 0; i < req_nr; i++)
159 if (!(refs.objects[i].item->flags & SHOWN)) {
160 if (++ret == 1)
161 error(message);
162 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
163 refs.objects[i].name);
166 for (i = 0; i < refs.nr; i++)
167 clear_commit_marks((struct commit *)refs.objects[i].item, -1);
169 if (verbose) {
170 struct ref_list *r;
172 r = &header->references;
173 printf("The bundle contains %d ref%s\n",
174 r->nr, (1 < r->nr) ? "s" : "");
175 list_refs(r, 0, NULL);
176 r = &header->prerequisites;
177 printf("The bundle requires these %d ref%s\n",
178 r->nr, (1 < r->nr) ? "s" : "");
179 list_refs(r, 0, NULL);
181 return ret;
184 static int list_heads(struct bundle_header *header, int argc, const char **argv)
186 return list_refs(&header->references, argc, argv);
189 static int create_bundle(struct bundle_header *header, const char *path,
190 int argc, const char **argv)
192 int bundle_fd = -1;
193 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
194 const char **argv_pack = xmalloc(5 * sizeof(const char *));
195 int i, ref_count = 0;
196 char buffer[1024];
197 struct rev_info revs;
198 struct child_process rls;
199 FILE *rls_fout;
202 * NEEDSWORK: this should use something like lock-file
203 * to create temporary that is cleaned up upon error.
205 bundle_fd = (!strcmp(path, "-") ? 1 :
206 open(path, O_CREAT | O_EXCL | O_WRONLY, 0666));
207 if (bundle_fd < 0)
208 return error("Could not create '%s': %s", path, strerror(errno));
210 /* write signature */
211 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
213 /* init revs to list objects for pack-objects later */
214 save_commit_buffer = 0;
215 init_revisions(&revs, NULL);
217 /* write prerequisites */
218 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
219 argv_boundary[0] = "rev-list";
220 argv_boundary[1] = "--boundary";
221 argv_boundary[2] = "--pretty=oneline";
222 argv_boundary[argc + 2] = NULL;
223 memset(&rls, 0, sizeof(rls));
224 rls.argv = argv_boundary;
225 rls.out = -1;
226 rls.git_cmd = 1;
227 if (start_command(&rls))
228 return -1;
229 rls_fout = fdopen(rls.out, "r");
230 while (fgets(buffer, sizeof(buffer), rls_fout)) {
231 unsigned char sha1[20];
232 if (buffer[0] == '-') {
233 write_or_die(bundle_fd, buffer, strlen(buffer));
234 if (!get_sha1_hex(buffer + 1, sha1)) {
235 struct object *object = parse_object(sha1);
236 object->flags |= UNINTERESTING;
237 add_pending_object(&revs, object, buffer);
239 } else if (!get_sha1_hex(buffer, sha1)) {
240 struct object *object = parse_object(sha1);
241 object->flags |= SHOWN;
244 fclose(rls_fout);
245 if (finish_command(&rls))
246 return error("rev-list died");
248 /* write references */
249 argc = setup_revisions(argc, argv, &revs, NULL);
250 if (argc > 1)
251 return error("unrecognized argument: %s'", argv[1]);
253 for (i = 0; i < revs.pending.nr; i++) {
254 struct object_array_entry *e = revs.pending.objects + i;
255 unsigned char sha1[20];
256 char *ref;
258 if (e->item->flags & UNINTERESTING)
259 continue;
260 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
261 continue;
263 * Make sure the refs we wrote out is correct; --max-count and
264 * other limiting options could have prevented all the tips
265 * from getting output.
267 * Non commit objects such as tags and blobs do not have
268 * this issue as they are not affected by those extra
269 * constraints.
271 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
272 warning("ref '%s' is excluded by the rev-list options",
273 e->name);
274 free(ref);
275 continue;
278 * If you run "git bundle create bndl v1.0..v2.0", the
279 * name of the positive ref is "v2.0" but that is the
280 * commit that is referenced by the tag, and not the tag
281 * itself.
283 if (hashcmp(sha1, e->item->sha1)) {
285 * Is this the positive end of a range expressed
286 * in terms of a tag (e.g. v2.0 from the range
287 * "v1.0..v2.0")?
289 struct commit *one = lookup_commit_reference(sha1);
290 struct object *obj;
292 if (e->item == &(one->object)) {
294 * Need to include e->name as an
295 * independent ref to the pack-objects
296 * input, so that the tag is included
297 * in the output; otherwise we would
298 * end up triggering "empty bundle"
299 * error.
301 obj = parse_object(sha1);
302 obj->flags |= SHOWN;
303 add_pending_object(&revs, obj, e->name);
305 free(ref);
306 continue;
309 ref_count++;
310 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
311 write_or_die(bundle_fd, " ", 1);
312 write_or_die(bundle_fd, ref, strlen(ref));
313 write_or_die(bundle_fd, "\n", 1);
314 free(ref);
316 if (!ref_count)
317 die ("Refusing to create empty bundle.");
319 /* end header */
320 write_or_die(bundle_fd, "\n", 1);
322 /* write pack */
323 argv_pack[0] = "pack-objects";
324 argv_pack[1] = "--all-progress";
325 argv_pack[2] = "--stdout";
326 argv_pack[3] = "--thin";
327 argv_pack[4] = NULL;
328 memset(&rls, 0, sizeof(rls));
329 rls.argv = argv_pack;
330 rls.in = -1;
331 rls.out = bundle_fd;
332 rls.git_cmd = 1;
333 if (start_command(&rls))
334 return error("Could not spawn pack-objects");
335 for (i = 0; i < revs.pending.nr; i++) {
336 struct object *object = revs.pending.objects[i].item;
337 if (object->flags & UNINTERESTING)
338 write(rls.in, "^", 1);
339 write(rls.in, sha1_to_hex(object->sha1), 40);
340 write(rls.in, "\n", 1);
342 if (finish_command(&rls))
343 return error ("pack-objects died");
344 return 0;
347 static int unbundle(struct bundle_header *header, int bundle_fd,
348 int argc, const char **argv)
350 const char *argv_index_pack[] = {"index-pack",
351 "--fix-thin", "--stdin", NULL};
352 struct child_process ip;
354 if (verify_bundle(header, 0))
355 return -1;
356 memset(&ip, 0, sizeof(ip));
357 ip.argv = argv_index_pack;
358 ip.in = bundle_fd;
359 ip.no_stdout = 1;
360 ip.git_cmd = 1;
361 if (run_command(&ip))
362 return error("index-pack died");
363 return list_heads(header, argc, argv);
366 int cmd_bundle(int argc, const char **argv, const char *prefix)
368 struct bundle_header header;
369 int nongit = 0;
370 const char *cmd, *bundle_file;
371 int bundle_fd = -1;
372 char buffer[PATH_MAX];
374 if (argc < 3)
375 usage(bundle_usage);
377 cmd = argv[1];
378 bundle_file = argv[2];
379 argc -= 2;
380 argv += 2;
382 prefix = setup_git_directory_gently(&nongit);
383 if (prefix && bundle_file[0] != '/') {
384 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
385 bundle_file = buffer;
388 memset(&header, 0, sizeof(header));
389 if (strcmp(cmd, "create") &&
390 (bundle_fd = read_header(bundle_file, &header)) < 0)
391 return 1;
393 if (!strcmp(cmd, "verify")) {
394 close(bundle_fd);
395 if (verify_bundle(&header, 1))
396 return 1;
397 fprintf(stderr, "%s is okay\n", bundle_file);
398 return 0;
400 if (!strcmp(cmd, "list-heads")) {
401 close(bundle_fd);
402 return !!list_heads(&header, argc, argv);
404 if (!strcmp(cmd, "create")) {
405 if (nongit)
406 die("Need a repository to create a bundle.");
407 return !!create_bundle(&header, bundle_file, argc, argv);
408 } else if (!strcmp(cmd, "unbundle")) {
409 if (nongit)
410 die("Need a repository to unbundle.");
411 return !!unbundle(&header, bundle_fd, argc, argv);
412 } else
413 usage(bundle_usage);