Add git-bundle: move objects and references by archive
[git/git.git] / builtin-bundle.c
CommitLineData
2e0afafe
JS
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
16static const char *bundle_usage="git-bundle (--create <bundle> <git-rev-list args> | --verify <bundle> | --list-heads <bundle> [refname]... | --unbundle <bundle> [refname]... )";
17
18static const char bundle_signature[] = "# v2 git bundle\n";
19
20struct ref_list {
21 unsigned int nr, alloc;
22 struct {
23 unsigned char sha1[20];
24 char *name;
25 } *list;
26};
27
28static 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
41struct bundle_header {
42 struct ref_list prerequisites, references;
43};
44
45/* this function returns the length of the string */
46static int read_string(int fd, char *buffer, int size)
47{
48 int i;
49 for (i = 0; i < size - 1; i++) {
50 int count = read(fd, buffer + i, 1);
51 if (count < 0)
52 return error("Read error: %s", strerror(errno));
53 if (count == 0) {
54 i--;
55 break;
56 }
57 if (buffer[i] == '\n')
58 break;
59 }
60 buffer[i + 1] = '\0';
61 return i + 1;
62}
63
64/* returns an fd */
65static int read_header(const char *path, struct bundle_header *header) {
66 char buffer[1024];
67 int fd = open(path, O_RDONLY);
68
69 if (fd < 0)
70 return error("could not open '%s'", path);
71 if (read_string(fd, buffer, sizeof(buffer)) < 0 ||
72 strcmp(buffer, bundle_signature)) {
73 close(fd);
74 return error("'%s' does not look like a v2 bundle file", path);
75 }
76 while (read_string(fd, buffer, sizeof(buffer)) > 0
77 && buffer[0] != '\n') {
78 int offset = buffer[0] == '-' ? 1 : 0;
79 int len = strlen(buffer);
80 unsigned char sha1[20];
81 struct ref_list *list = offset ? &header->prerequisites
82 : &header->references;
83 if (get_sha1_hex(buffer + offset, sha1)) {
84 close(fd);
85 return error("invalid SHA1: %s", buffer);
86 }
87 if (buffer[len - 1] == '\n')
88 buffer[len - 1] = '\0';
89 add_to_ref_list(sha1, buffer + 41 + offset, list);
90 }
91 return fd;
92}
93
94/* if in && *in >= 0, take that as input file descriptor instead */
95static int fork_with_pipe(const char **argv, int *in, int *out)
96{
97 int needs_in, needs_out;
98 int fdin[2], fdout[2], pid;
99
100 needs_in = in && *in < 0;
101 if (needs_in) {
102 if (pipe(fdin) < 0)
103 return error("could not setup pipe");
104 *in = fdin[1];
105 }
106
107 needs_out = out && *out < 0;
108 if (needs_out) {
109 if (pipe(fdout) < 0)
110 return error("could not setup pipe");
111 *out = fdout[0];
112 }
113
114 if ((pid = fork()) < 0) {
115 if (needs_in) {
116 close(fdin[0]);
117 close(fdin[1]);
118 }
119 if (needs_out) {
120 close(fdout[0]);
121 close(fdout[1]);
122 }
123 return error("could not fork");
124 }
125 if (!pid) {
126 if (needs_in) {
127 dup2(fdin[0], 0);
128 close(fdin[0]);
129 close(fdin[1]);
130 } else if (in)
131 dup2(*in, 0);
132 if (needs_out) {
133 dup2(fdout[1], 1);
134 close(fdout[0]);
135 close(fdout[1]);
136 } else if (out)
137 dup2(*out, 1);
138 exit(execv_git_cmd(argv));
139 }
140 if (needs_in)
141 close(fdin[0]);
142 if (needs_out)
143 close(fdout[1]);
144 return pid;
145}
146
147static int verify_bundle(struct bundle_header *header)
148{
149 /*
150 * Do fast check, then if any prereqs are missing then go line by line
151 * to be verbose about the errors
152 */
153 struct ref_list *p = &header->prerequisites;
154 const char *argv[5] = {"rev-list", "--stdin", "--not", "--all", NULL};
155 int pid, in, out, i, ret = 0;
156 char buffer[1024];
157
158 in = out = -1;
159 pid = fork_with_pipe(argv, &in, &out);
160 if (pid < 0)
161 return error("Could not fork rev-list");
162 if (!fork()) {
163 for (i = 0; i < p->nr; i++) {
164 write(in, sha1_to_hex(p->list[i].sha1), 40);
165 write(in, "\n", 1);
166 }
167 close(in);
168 exit(0);
169 }
170 close(in);
171 while (read_string(out, buffer, sizeof(buffer)) > 0) {
172 if (ret++ == 0)
173 error ("The bundle requires the following commits you lack:");
174 fprintf(stderr, "%s", buffer);
175 }
176 close(out);
177 while (waitpid(pid, &i, 0) < 0)
178 if (errno != EINTR)
179 return -1;
180 if (!ret && (!WIFEXITED(i) || WEXITSTATUS(i)))
181 return error("At least one prerequisite is lacking.");
182
183 return ret;
184}
185
186static int list_heads(struct bundle_header *header, int argc, const char **argv)
187{
188 int i;
189 struct ref_list *r = &header->references;
190
191 for (i = 0; i < r->nr; i++) {
192 if (argc > 1) {
193 int j;
194 for (j = 1; j < argc; j++)
195 if (!strcmp(r->list[i].name, argv[j]))
196 break;
197 if (j == argc)
198 continue;
199 }
200 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
201 r->list[i].name);
202 }
203 return 0;
204}
205
206static void show_commit(struct commit *commit)
207{
208 write(1, sha1_to_hex(commit->object.sha1), 40);
209 write(1, "\n", 1);
210 if (commit->parents) {
211 free_commit_list(commit->parents);
212 commit->parents = NULL;
213 }
214}
215
216static void show_object(struct object_array_entry *p)
217{
218 /* An object with name "foo\n0000000..." can be used to
219 * * confuse downstream git-pack-objects very badly.
220 * */
221 const char *ep = strchr(p->name, '\n');
222 int len = ep ? ep - p->name : strlen(p->name);
223 write(1, sha1_to_hex(p->item->sha1), 40);
224 write(1, " ", 1);
225 if (len)
226 write(1, p->name, len);
227 write(1, "\n", 1);
228}
229
230static int create_bundle(struct bundle_header *header, const char *path,
231 int argc, const char **argv)
232{
233 int bundle_fd = -1;
234 const char **argv_boundary = xmalloc((argc + 3) * sizeof(const char *));
235 const char **argv_pack = xmalloc(4 * sizeof(const char *));
236 int pid, in, out, i, status;
237 char buffer[1024];
238 struct rev_info revs;
239
240 bundle_fd = (!strcmp(path, "-") ? 1 :
241 open(path, O_CREAT | O_WRONLY, 0666));
242 if (bundle_fd < 0)
243 return error("Could not write to '%s'", path);
244
245 /* write signature */
246 write(bundle_fd, bundle_signature, strlen(bundle_signature));
247
248 /* write prerequisites */
249 memcpy(argv_boundary + 2, argv + 1, argc * sizeof(const char *));
250 argv_boundary[0] = "rev-list";
251 argv_boundary[1] = "--boundary";
252 argv_boundary[argc + 1] = NULL;
253 out = -1;
254 pid = fork_with_pipe(argv_boundary, NULL, &out);
255 if (pid < 0)
256 return -1;
257 while ((i = read_string(out, buffer, sizeof(buffer))) > 0)
258 if (buffer[0] == '-')
259 write(bundle_fd, buffer, i);
260 while ((i = waitpid(pid, &status, 0)) < 0)
261 if (errno != EINTR)
262 return error("rev-list died");
263 if (!WIFEXITED(status) || WEXITSTATUS(status))
264 return error("rev-list died %d", WEXITSTATUS(status));
265
266 /* write references */
267 save_commit_buffer = 0;
268 init_revisions(&revs, NULL);
269 revs.tag_objects = 1;
270 revs.tree_objects = 1;
271 revs.blob_objects = 1;
272 argc = setup_revisions(argc, argv, &revs, NULL);
273 if (argc > 1)
274 return error("unrecognized argument: %s'", argv[1]);
275 for (i = 0; i < revs.pending.nr; i++) {
276 struct object_array_entry *e = revs.pending.objects + i;
277 if (!(e->item->flags & UNINTERESTING)) {
278 unsigned char sha1[20];
279 char *ref;
280 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
281 continue;
282 write(bundle_fd, sha1_to_hex(e->item->sha1), 40);
283 write(bundle_fd, " ", 1);
284 write(bundle_fd, ref, strlen(ref));
285 write(bundle_fd, "\n", 1);
286 free(ref);
287 }
288 }
289
290 /* end header */
291 write(bundle_fd, "\n", 1);
292
293 /* write pack */
294 argv_pack[0] = "pack-objects";
295 argv_pack[1] = "--all-progress";
296 argv_pack[2] = "--stdout";
297 argv_pack[3] = NULL;
298 in = -1;
299 out = bundle_fd;
300 pid = fork_with_pipe(argv_pack, &in, &out);
301 if (pid < 0)
302 return error("Could not spawn pack-objects");
303 close(1);
304 close(bundle_fd);
305 dup2(in, 1);
306 close(in);
307 prepare_revision_walk(&revs);
308 traverse_commit_list(&revs, show_commit, show_object);
309 close(1);
310 while (waitpid(pid, &status, 0) < 0)
311 if (errno != EINTR)
312 return -1;
313 if (!WIFEXITED(status) || WEXITSTATUS(status))
314 return error ("pack-objects died");
315 return 0;
316}
317
318static int unbundle(struct bundle_header *header, int bundle_fd,
319 int argc, const char **argv)
320{
321 const char *argv_index_pack[] = {"index-pack", "--stdin", NULL};
322 int pid, status, dev_null;
323
324 if (verify_bundle(header))
325 return -1;
326 dev_null = open("/dev/null", O_WRONLY);
327 pid = fork_with_pipe(argv_index_pack, &bundle_fd, &dev_null);
328 if (pid < 0)
329 return error("Could not spawn index-pack");
330 close(bundle_fd);
331 while (waitpid(pid, &status, 0) < 0)
332 if (errno != EINTR)
333 return error("index-pack died");
334 if (!WIFEXITED(status) || WEXITSTATUS(status))
335 return error("index-pack exited with status %d",
336 WEXITSTATUS(status));
337 return list_heads(header, argc, argv);
338}
339
340int cmd_bundle(int argc, const char **argv, const char *prefix)
341{
342 struct bundle_header header;
343 int nongit = 0;
344 const char *cmd, *bundle_file;
345 int bundle_fd = -1;
346 char buffer[PATH_MAX];
347
348 if (argc < 3)
349 usage(bundle_usage);
350
351 cmd = argv[1];
352 bundle_file = argv[2];
353 argc -= 2;
354 argv += 2;
355
356 prefix = setup_git_directory_gently(&nongit);
357 if (prefix && bundle_file[0] != '/') {
358 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
359 bundle_file = buffer;
360 }
361
362 memset(&header, 0, sizeof(header));
363 if (strcmp(cmd, "create") &&
364 !(bundle_fd = read_header(bundle_file, &header)))
365 return 1;
366
367 if (!strcmp(cmd, "verify")) {
368 close(bundle_fd);
369 if (verify_bundle(&header))
370 return 1;
371 fprintf(stderr, "%s is okay\n", bundle_file);
372 return 0;
373 }
374 if (!strcmp(cmd, "list-heads")) {
375 close(bundle_fd);
376 return !!list_heads(&header, argc, argv);
377 }
378 if (!strcmp(cmd, "create")) {
379 if (nongit)
380 die("Need a repository to create a bundle.");
381 return !!create_bundle(&header, bundle_file, argc, argv);
382 } else if (!strcmp(cmd, "unbundle")) {
383 if (nongit)
384 die("Need a repository to unbundle.");
385 return !!unbundle(&header, bundle_fd, argc, argv);
386 } else
387 usage(bundle_usage);
388}
389