builtin-bundle.c - use stream buffered input for rev-list
[git/libgit-gsoc.git] / builtin-bundle.c
blobb954213f702a5ec8ee0cadf6cb79295272b73ca5
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 /* this function returns the length of the string */
48 static int read_string(int fd, char *buffer, int size)
50 int i;
51 for (i = 0; i < size - 1; i++) {
52 ssize_t count = xread(fd, buffer + i, 1);
53 if (count < 0)
54 return error("Read error: %s", strerror(errno));
55 if (count == 0) {
56 i--;
57 break;
59 if (buffer[i] == '\n')
60 break;
62 buffer[i + 1] = '\0';
63 return i + 1;
66 /* returns an fd */
67 static int read_header(const char *path, struct bundle_header *header) {
68 char buffer[1024];
69 int fd = open(path, O_RDONLY);
71 if (fd < 0)
72 return error("could not open '%s'", path);
73 if (read_string(fd, buffer, sizeof(buffer)) < 0 ||
74 strcmp(buffer, bundle_signature)) {
75 close(fd);
76 return error("'%s' does not look like a v2 bundle file", path);
78 while (read_string(fd, buffer, sizeof(buffer)) > 0
79 && buffer[0] != '\n') {
80 int is_prereq = buffer[0] == '-';
81 int offset = is_prereq ? 1 : 0;
82 int len = strlen(buffer);
83 unsigned char sha1[20];
84 struct ref_list *list = is_prereq ? &header->prerequisites
85 : &header->references;
86 char delim;
88 if (buffer[len - 1] == '\n')
89 buffer[len - 1] = '\0';
90 if (get_sha1_hex(buffer + offset, sha1)) {
91 warning("unrecognized header: %s", buffer);
92 continue;
94 delim = buffer[40 + offset];
95 if (!isspace(delim) && (delim != '\0' || !is_prereq))
96 die ("invalid header: %s", buffer);
97 add_to_ref_list(sha1, isspace(delim) ?
98 buffer + 41 + offset : "", list);
100 return fd;
103 static int list_refs(struct ref_list *r, int argc, const char **argv)
105 int i;
107 for (i = 0; i < r->nr; i++) {
108 if (argc > 1) {
109 int j;
110 for (j = 1; j < argc; j++)
111 if (!strcmp(r->list[i].name, argv[j]))
112 break;
113 if (j == argc)
114 continue;
116 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
117 r->list[i].name);
119 return 0;
122 #define PREREQ_MARK (1u<<16)
124 static int verify_bundle(struct bundle_header *header, int verbose)
127 * Do fast check, then if any prereqs are missing then go line by line
128 * to be verbose about the errors
130 struct ref_list *p = &header->prerequisites;
131 struct rev_info revs;
132 const char *argv[] = {NULL, "--all"};
133 struct object_array refs;
134 struct commit *commit;
135 int i, ret = 0, req_nr;
136 const char *message = "Repository lacks these prerequisite commits:";
138 init_revisions(&revs, NULL);
139 for (i = 0; i < p->nr; i++) {
140 struct ref_list_entry *e = p->list + i;
141 struct object *o = parse_object(e->sha1);
142 if (o) {
143 o->flags |= PREREQ_MARK;
144 add_pending_object(&revs, o, e->name);
145 continue;
147 if (++ret == 1)
148 error(message);
149 error("%s %s", sha1_to_hex(e->sha1), e->name);
151 if (revs.pending.nr != p->nr)
152 return ret;
153 req_nr = revs.pending.nr;
154 setup_revisions(2, argv, &revs, NULL);
156 memset(&refs, 0, sizeof(struct object_array));
157 for (i = 0; i < revs.pending.nr; i++) {
158 struct object_array_entry *e = revs.pending.objects + i;
159 add_object_array(e->item, e->name, &refs);
162 prepare_revision_walk(&revs);
164 i = req_nr;
165 while (i && (commit = get_revision(&revs)))
166 if (commit->object.flags & PREREQ_MARK)
167 i--;
169 for (i = 0; i < req_nr; i++)
170 if (!(refs.objects[i].item->flags & SHOWN)) {
171 if (++ret == 1)
172 error(message);
173 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
174 refs.objects[i].name);
177 for (i = 0; i < refs.nr; i++)
178 clear_commit_marks((struct commit *)refs.objects[i].item, -1);
180 if (verbose) {
181 struct ref_list *r;
183 r = &header->references;
184 printf("The bundle contains %d ref%s\n",
185 r->nr, (1 < r->nr) ? "s" : "");
186 list_refs(r, 0, NULL);
187 r = &header->prerequisites;
188 printf("The bundle requires these %d ref%s\n",
189 r->nr, (1 < r->nr) ? "s" : "");
190 list_refs(r, 0, NULL);
192 return ret;
195 static int list_heads(struct bundle_header *header, int argc, const char **argv)
197 return list_refs(&header->references, argc, argv);
200 static int create_bundle(struct bundle_header *header, const char *path,
201 int argc, const char **argv)
203 int bundle_fd = -1;
204 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
205 const char **argv_pack = xmalloc(5 * sizeof(const char *));
206 int i, ref_count = 0;
207 char buffer[1024];
208 struct rev_info revs;
209 struct child_process rls;
210 FILE *rls_fout;
213 * NEEDSWORK: this should use something like lock-file
214 * to create temporary that is cleaned up upon error.
216 bundle_fd = (!strcmp(path, "-") ? 1 :
217 open(path, O_CREAT | O_EXCL | O_WRONLY, 0666));
218 if (bundle_fd < 0)
219 return error("Could not create '%s': %s", path, strerror(errno));
221 /* write signature */
222 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
224 /* init revs to list objects for pack-objects later */
225 save_commit_buffer = 0;
226 init_revisions(&revs, NULL);
228 /* write prerequisites */
229 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
230 argv_boundary[0] = "rev-list";
231 argv_boundary[1] = "--boundary";
232 argv_boundary[2] = "--pretty=oneline";
233 argv_boundary[argc + 2] = NULL;
234 memset(&rls, 0, sizeof(rls));
235 rls.argv = argv_boundary;
236 rls.out = -1;
237 rls.git_cmd = 1;
238 if (start_command(&rls))
239 return -1;
240 rls_fout = fdopen(rls.out, "r");
241 while (fgets(buffer, sizeof(buffer), rls_fout)) {
242 unsigned char sha1[20];
243 if (buffer[0] == '-') {
244 write_or_die(bundle_fd, buffer, strlen(buffer));
245 if (!get_sha1_hex(buffer + 1, sha1)) {
246 struct object *object = parse_object(sha1);
247 object->flags |= UNINTERESTING;
248 add_pending_object(&revs, object, buffer);
250 } else if (!get_sha1_hex(buffer, sha1)) {
251 struct object *object = parse_object(sha1);
252 object->flags |= SHOWN;
255 fclose(rls_fout);
256 if (finish_command(&rls))
257 return error("rev-list died");
259 /* write references */
260 argc = setup_revisions(argc, argv, &revs, NULL);
261 if (argc > 1)
262 return error("unrecognized argument: %s'", argv[1]);
264 for (i = 0; i < revs.pending.nr; i++) {
265 struct object_array_entry *e = revs.pending.objects + i;
266 unsigned char sha1[20];
267 char *ref;
269 if (e->item->flags & UNINTERESTING)
270 continue;
271 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
272 continue;
274 * Make sure the refs we wrote out is correct; --max-count and
275 * other limiting options could have prevented all the tips
276 * from getting output.
278 * Non commit objects such as tags and blobs do not have
279 * this issue as they are not affected by those extra
280 * constraints.
282 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
283 warning("ref '%s' is excluded by the rev-list options",
284 e->name);
285 free(ref);
286 continue;
289 * If you run "git bundle create bndl v1.0..v2.0", the
290 * name of the positive ref is "v2.0" but that is the
291 * commit that is referenced by the tag, and not the tag
292 * itself.
294 if (hashcmp(sha1, e->item->sha1)) {
296 * Is this the positive end of a range expressed
297 * in terms of a tag (e.g. v2.0 from the range
298 * "v1.0..v2.0")?
300 struct commit *one = lookup_commit_reference(sha1);
301 struct object *obj;
303 if (e->item == &(one->object)) {
305 * Need to include e->name as an
306 * independent ref to the pack-objects
307 * input, so that the tag is included
308 * in the output; otherwise we would
309 * end up triggering "empty bundle"
310 * error.
312 obj = parse_object(sha1);
313 obj->flags |= SHOWN;
314 add_pending_object(&revs, obj, e->name);
316 free(ref);
317 continue;
320 ref_count++;
321 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
322 write_or_die(bundle_fd, " ", 1);
323 write_or_die(bundle_fd, ref, strlen(ref));
324 write_or_die(bundle_fd, "\n", 1);
325 free(ref);
327 if (!ref_count)
328 die ("Refusing to create empty bundle.");
330 /* end header */
331 write_or_die(bundle_fd, "\n", 1);
333 /* write pack */
334 argv_pack[0] = "pack-objects";
335 argv_pack[1] = "--all-progress";
336 argv_pack[2] = "--stdout";
337 argv_pack[3] = "--thin";
338 argv_pack[4] = NULL;
339 memset(&rls, 0, sizeof(rls));
340 rls.argv = argv_pack;
341 rls.in = -1;
342 rls.out = bundle_fd;
343 rls.git_cmd = 1;
344 if (start_command(&rls))
345 return error("Could not spawn pack-objects");
346 for (i = 0; i < revs.pending.nr; i++) {
347 struct object *object = revs.pending.objects[i].item;
348 if (object->flags & UNINTERESTING)
349 write(rls.in, "^", 1);
350 write(rls.in, sha1_to_hex(object->sha1), 40);
351 write(rls.in, "\n", 1);
353 if (finish_command(&rls))
354 return error ("pack-objects died");
355 return 0;
358 static int unbundle(struct bundle_header *header, int bundle_fd,
359 int argc, const char **argv)
361 const char *argv_index_pack[] = {"index-pack",
362 "--fix-thin", "--stdin", NULL};
363 struct child_process ip;
365 if (verify_bundle(header, 0))
366 return -1;
367 memset(&ip, 0, sizeof(ip));
368 ip.argv = argv_index_pack;
369 ip.in = bundle_fd;
370 ip.no_stdout = 1;
371 ip.git_cmd = 1;
372 if (run_command(&ip))
373 return error("index-pack died");
374 return list_heads(header, argc, argv);
377 int cmd_bundle(int argc, const char **argv, const char *prefix)
379 struct bundle_header header;
380 int nongit = 0;
381 const char *cmd, *bundle_file;
382 int bundle_fd = -1;
383 char buffer[PATH_MAX];
385 if (argc < 3)
386 usage(bundle_usage);
388 cmd = argv[1];
389 bundle_file = argv[2];
390 argc -= 2;
391 argv += 2;
393 prefix = setup_git_directory_gently(&nongit);
394 if (prefix && bundle_file[0] != '/') {
395 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
396 bundle_file = buffer;
399 memset(&header, 0, sizeof(header));
400 if (strcmp(cmd, "create") &&
401 (bundle_fd = read_header(bundle_file, &header)) < 0)
402 return 1;
404 if (!strcmp(cmd, "verify")) {
405 close(bundle_fd);
406 if (verify_bundle(&header, 1))
407 return 1;
408 fprintf(stderr, "%s is okay\n", bundle_file);
409 return 0;
411 if (!strcmp(cmd, "list-heads")) {
412 close(bundle_fd);
413 return !!list_heads(&header, argc, argv);
415 if (!strcmp(cmd, "create")) {
416 if (nongit)
417 die("Need a repository to create a bundle.");
418 return !!create_bundle(&header, bundle_file, argc, argv);
419 } else if (!strcmp(cmd, "unbundle")) {
420 if (nongit)
421 die("Need a repository to unbundle.");
422 return !!unbundle(&header, bundle_fd, argc, argv);
423 } else
424 usage(bundle_usage);