revision traversal: retire BOUNDARY_SHOW
[git/jnareb-git.git] / builtin-bundle.c
blob76bd204de89172ca8fadc716b4d7daecae3ef4a7
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"
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
16 static const char *bundle_usage="git-bundle (create <bundle> <git-rev-list args> | verify <bundle> | list-heads <bundle> [refname]... | unbundle <bundle> [refname]... )";
18 static const char bundle_signature[] = "# v2 git bundle\n";
20 struct ref_list {
21 unsigned int nr, alloc;
22 struct ref_list_entry {
23 unsigned char sha1[20];
24 char *name;
25 } *list;
28 static void add_to_ref_list(const unsigned char *sha1, const char *name,
29 struct ref_list *list)
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]));
36 memcpy(list->list[list->nr].sha1, sha1, 20);
37 list->list[list->nr].name = xstrdup(name);
38 list->nr++;
41 struct bundle_header {
42 struct ref_list prerequisites;
43 struct ref_list references;
46 /* this function returns the length of the string */
47 static int read_string(int fd, char *buffer, int size)
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;
58 if (buffer[i] == '\n')
59 break;
61 buffer[i + 1] = '\0';
62 return i + 1;
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);
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);
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;
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;
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);
99 return fd;
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)
105 int needs_in, needs_out;
106 int fdin[2], fdout[2], pid;
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];
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];
122 if ((pid = fork()) < 0) {
123 if (needs_in) {
124 close(fdin[0]);
125 close(fdin[1]);
127 if (needs_out) {
128 close(fdout[0]);
129 close(fdout[1]);
131 return error("could not fork");
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);
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);
150 exit(execv_git_cmd(argv));
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;
163 #define PREREQ_MARK (1u<<16)
165 static int verify_bundle(struct bundle_header *header)
168 * Do fast check, then if any prereqs are missing then go line by line
169 * to be verbose about the errors
171 struct ref_list *p = &header->prerequisites;
172 struct rev_info revs;
173 const char *argv[] = {NULL, "--all"};
174 struct object_array refs;
175 struct commit *commit;
176 int i, ret = 0, req_nr;
177 const char *message = "Repository lacks these prerequisite commits:";
179 init_revisions(&revs, NULL);
180 for (i = 0; i < p->nr; i++) {
181 struct ref_list_entry *e = p->list + i;
182 struct object *o = parse_object(e->sha1);
183 if (o) {
184 o->flags |= PREREQ_MARK;
185 add_pending_object(&revs, o, e->name);
186 continue;
188 if (++ret == 1)
189 error(message);
190 error("%s %s", sha1_to_hex(e->sha1), e->name);
192 if (revs.pending.nr == 0)
193 return ret;
194 req_nr = revs.pending.nr;
195 setup_revisions(2, argv, &revs, NULL);
197 memset(&refs, 0, sizeof(struct object_array));
198 for (i = 0; i < revs.pending.nr; i++) {
199 struct object_array_entry *e = revs.pending.objects + i;
200 add_object_array(e->item, e->name, &refs);
203 prepare_revision_walk(&revs);
205 i = req_nr;
206 while (i && (commit = get_revision(&revs)))
207 if (commit->object.flags & PREREQ_MARK)
208 i--;
210 for (i = 0; i < req_nr; i++)
211 if (!(refs.objects[i].item->flags & SHOWN)) {
212 if (++ret == 1)
213 error(message);
214 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
215 refs.objects[i].name);
218 for (i = 0; i < refs.nr; i++)
219 clear_commit_marks((struct commit *)refs.objects[i].item, -1);
221 return ret;
224 static int list_heads(struct bundle_header *header, int argc, const char **argv)
226 int i;
227 struct ref_list *r = &header->references;
229 for (i = 0; i < r->nr; i++) {
230 if (argc > 1) {
231 int j;
232 for (j = 1; j < argc; j++)
233 if (!strcmp(r->list[i].name, argv[j]))
234 break;
235 if (j == argc)
236 continue;
238 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
239 r->list[i].name);
241 return 0;
244 static void show_commit(struct commit *commit)
246 write_or_die(1, sha1_to_hex(commit->object.sha1), 40);
247 write_or_die(1, "\n", 1);
248 if (commit->parents) {
249 free_commit_list(commit->parents);
250 commit->parents = NULL;
254 static void show_object(struct object_array_entry *p)
256 /* An object with name "foo\n0000000..." can be used to
257 * confuse downstream git-pack-objects very badly.
259 const char *ep = strchr(p->name, '\n');
260 int len = ep ? ep - p->name : strlen(p->name);
261 write_or_die(1, sha1_to_hex(p->item->sha1), 40);
262 write_or_die(1, " ", 1);
263 if (len)
264 write_or_die(1, p->name, len);
265 write_or_die(1, "\n", 1);
268 static void show_edge(struct commit *commit)
270 ; /* nothing to do */
273 static int create_bundle(struct bundle_header *header, const char *path,
274 int argc, const char **argv)
276 int bundle_fd = -1;
277 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
278 const char **argv_pack = xmalloc(4 * sizeof(const char *));
279 int pid, in, out, i, status;
280 char buffer[1024];
281 struct rev_info revs;
283 bundle_fd = (!strcmp(path, "-") ? 1 :
284 open(path, O_CREAT | O_WRONLY, 0666));
285 if (bundle_fd < 0)
286 return error("Could not write to '%s'", path);
288 /* write signature */
289 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
291 /* write prerequisites */
292 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
293 argv_boundary[0] = "rev-list";
294 argv_boundary[1] = "--boundary";
295 argv_boundary[2] = "--pretty=oneline";
296 argv_boundary[argc + 2] = NULL;
297 out = -1;
298 pid = fork_with_pipe(argv_boundary, NULL, &out);
299 if (pid < 0)
300 return -1;
301 while ((i = read_string(out, buffer, sizeof(buffer))) > 0)
302 if (buffer[0] == '-')
303 write_or_die(bundle_fd, buffer, i);
304 while ((i = waitpid(pid, &status, 0)) < 0)
305 if (errno != EINTR)
306 return error("rev-list died");
307 if (!WIFEXITED(status) || WEXITSTATUS(status))
308 return error("rev-list died %d", WEXITSTATUS(status));
310 /* write references */
311 save_commit_buffer = 0;
312 init_revisions(&revs, NULL);
313 revs.tag_objects = 1;
314 revs.tree_objects = 1;
315 revs.blob_objects = 1;
316 argc = setup_revisions(argc, argv, &revs, NULL);
317 if (argc > 1)
318 return error("unrecognized argument: %s'", argv[1]);
319 for (i = 0; i < revs.pending.nr; i++) {
320 struct object_array_entry *e = revs.pending.objects + i;
321 if (!(e->item->flags & UNINTERESTING)) {
322 unsigned char sha1[20];
323 char *ref;
324 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
325 continue;
326 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
327 write_or_die(bundle_fd, " ", 1);
328 write_or_die(bundle_fd, ref, strlen(ref));
329 write_or_die(bundle_fd, "\n", 1);
330 free(ref);
334 /* end header */
335 write_or_die(bundle_fd, "\n", 1);
337 /* write pack */
338 argv_pack[0] = "pack-objects";
339 argv_pack[1] = "--all-progress";
340 argv_pack[2] = "--stdout";
341 argv_pack[3] = NULL;
342 in = -1;
343 out = bundle_fd;
344 pid = fork_with_pipe(argv_pack, &in, &out);
345 if (pid < 0)
346 return error("Could not spawn pack-objects");
347 close(1);
348 dup2(in, 1);
349 close(in);
350 prepare_revision_walk(&revs);
351 mark_edges_uninteresting(revs.commits, &revs, show_edge);
352 traverse_commit_list(&revs, show_commit, show_object);
353 close(1);
354 while (waitpid(pid, &status, 0) < 0)
355 if (errno != EINTR)
356 return -1;
357 if (!WIFEXITED(status) || WEXITSTATUS(status))
358 return error ("pack-objects died");
359 return 0;
362 static int unbundle(struct bundle_header *header, int bundle_fd,
363 int argc, const char **argv)
365 const char *argv_index_pack[] = {"index-pack", "--stdin", NULL};
366 int pid, status, dev_null;
368 if (verify_bundle(header))
369 return -1;
370 dev_null = open("/dev/null", O_WRONLY);
371 pid = fork_with_pipe(argv_index_pack, &bundle_fd, &dev_null);
372 if (pid < 0)
373 return error("Could not spawn index-pack");
374 while (waitpid(pid, &status, 0) < 0)
375 if (errno != EINTR)
376 return error("index-pack died");
377 if (!WIFEXITED(status) || WEXITSTATUS(status))
378 return error("index-pack exited with status %d",
379 WEXITSTATUS(status));
380 return list_heads(header, argc, argv);
383 int cmd_bundle(int argc, const char **argv, const char *prefix)
385 struct bundle_header header;
386 int nongit = 0;
387 const char *cmd, *bundle_file;
388 int bundle_fd = -1;
389 char buffer[PATH_MAX];
391 if (argc < 3)
392 usage(bundle_usage);
394 cmd = argv[1];
395 bundle_file = argv[2];
396 argc -= 2;
397 argv += 2;
399 prefix = setup_git_directory_gently(&nongit);
400 if (prefix && bundle_file[0] != '/') {
401 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
402 bundle_file = buffer;
405 memset(&header, 0, sizeof(header));
406 if (strcmp(cmd, "create") &&
407 !(bundle_fd = read_header(bundle_file, &header)))
408 return 1;
410 if (!strcmp(cmd, "verify")) {
411 close(bundle_fd);
412 if (verify_bundle(&header))
413 return 1;
414 fprintf(stderr, "%s is okay\n", bundle_file);
415 return 0;
417 if (!strcmp(cmd, "list-heads")) {
418 close(bundle_fd);
419 return !!list_heads(&header, argc, argv);
421 if (!strcmp(cmd, "create")) {
422 if (nongit)
423 die("Need a repository to create a bundle.");
424 return !!create_bundle(&header, bundle_file, argc, argv);
425 } else if (!strcmp(cmd, "unbundle")) {
426 if (nongit)
427 die("Need a repository to unbundle.");
428 return !!unbundle(&header, bundle_fd, argc, argv);
429 } else
430 usage(bundle_usage);