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;
43 struct ref_list references;
44};
45
46/* this function returns the length of the string */
47static 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 */
66static 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 */
103static 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
163static int verify_bundle(struct bundle_header *header)
164{
165 /*
166 * Do fast check, then if any prereqs are missing then go line by line
167 * to be verbose about the errors
168 */
169 struct ref_list *p = &header->prerequisites;
170 char **argv;
171 int pid, out, i, ret = 0;
172 char buffer[1024];
173
174 argv = xmalloc((p->nr + 4) * sizeof(const char *));
175 argv[0] = "rev-list";
176 argv[1] = "--not";
177 argv[2] = "--all";
178 for (i = 0; i < p->nr; i++)
179 argv[i + 3] = xstrdup(sha1_to_hex(p->list[i].sha1));
180 argv[p->nr + 3] = NULL;
181 out = -1;
182 pid = fork_with_pipe((const char **)argv, NULL, &out);
183 if (pid < 0)
184 return error("Could not fork rev-list");
185 while (read_string(out, buffer, sizeof(buffer)) > 0)
186 ; /* do nothing */
187 close(out);
188 for (i = 0; i < p->nr; i++)
189 free(argv[i + 3]);
190 free(argv);
191
192 while (waitpid(pid, &i, 0) < 0)
193 if (errno != EINTR)
194 return -1;
195 if (!ret && (!WIFEXITED(i) || WEXITSTATUS(i)))
196 return error("At least one prerequisite is lacking.");
197
198 return ret;
199}
200
201static int list_heads(struct bundle_header *header, int argc, const char **argv)
202{
203 int i;
204 struct ref_list *r = &header->references;
205
206 for (i = 0; i < r->nr; i++) {
207 if (argc > 1) {
208 int j;
209 for (j = 1; j < argc; j++)
210 if (!strcmp(r->list[i].name, argv[j]))
211 break;
212 if (j == argc)
213 continue;
214 }
215 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
216 r->list[i].name);
217 }
218 return 0;
219}
220
221static void show_commit(struct commit *commit)
222{
223 write_or_die(1, sha1_to_hex(commit->object.sha1), 40);
224 write_or_die(1, "\n", 1);
225 if (commit->parents) {
226 free_commit_list(commit->parents);
227 commit->parents = NULL;
228 }
229}
230
231static void show_object(struct object_array_entry *p)
232{
233 /* An object with name "foo\n0000000..." can be used to
234 * confuse downstream git-pack-objects very badly.
235 */
236 const char *ep = strchr(p->name, '\n');
237 int len = ep ? ep - p->name : strlen(p->name);
238 write_or_die(1, sha1_to_hex(p->item->sha1), 40);
239 write_or_die(1, " ", 1);
240 if (len)
241 write_or_die(1, p->name, len);
242 write_or_die(1, "\n", 1);
243}
244
245static int create_bundle(struct bundle_header *header, const char *path,
246 int argc, const char **argv)
247{
248 int bundle_fd = -1;
249 const char **argv_boundary = xmalloc((argc + 3) * sizeof(const char *));
250 const char **argv_pack = xmalloc(4 * sizeof(const char *));
251 int pid, in, out, i, status;
252 char buffer[1024];
253 struct rev_info revs;
254
255 bundle_fd = (!strcmp(path, "-") ? 1 :
256 open(path, O_CREAT | O_WRONLY, 0666));
257 if (bundle_fd < 0)
258 return error("Could not write to '%s'", path);
259
260 /* write signature */
261 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
262
263 /* write prerequisites */
264 memcpy(argv_boundary + 2, argv + 1, argc * sizeof(const char *));
265 argv_boundary[0] = "rev-list";
266 argv_boundary[1] = "--boundary";
267 argv_boundary[argc + 1] = NULL;
268 out = -1;
269 pid = fork_with_pipe(argv_boundary, NULL, &out);
270 if (pid < 0)
271 return -1;
272 while ((i = read_string(out, buffer, sizeof(buffer))) > 0)
273 if (buffer[0] == '-')
274 write_or_die(bundle_fd, buffer, i);
275 while ((i = waitpid(pid, &status, 0)) < 0)
276 if (errno != EINTR)
277 return error("rev-list died");
278 if (!WIFEXITED(status) || WEXITSTATUS(status))
279 return error("rev-list died %d", WEXITSTATUS(status));
280
281 /* write references */
282 save_commit_buffer = 0;
283 init_revisions(&revs, NULL);
284 revs.tag_objects = 1;
285 revs.tree_objects = 1;
286 revs.blob_objects = 1;
287 argc = setup_revisions(argc, argv, &revs, NULL);
288 if (argc > 1)
289 return error("unrecognized argument: %s'", argv[1]);
290 for (i = 0; i < revs.pending.nr; i++) {
291 struct object_array_entry *e = revs.pending.objects + i;
292 if (!(e->item->flags & UNINTERESTING)) {
293 unsigned char sha1[20];
294 char *ref;
295 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
296 continue;
297 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
298 write_or_die(bundle_fd, " ", 1);
299 write_or_die(bundle_fd, ref, strlen(ref));
300 write_or_die(bundle_fd, "\n", 1);
301 free(ref);
302 }
303 }
304
305 /* end header */
306 write_or_die(bundle_fd, "\n", 1);
307
308 /* write pack */
309 argv_pack[0] = "pack-objects";
310 argv_pack[1] = "--all-progress";
311 argv_pack[2] = "--stdout";
312 argv_pack[3] = NULL;
313 in = -1;
314 out = bundle_fd;
315 pid = fork_with_pipe(argv_pack, &in, &out);
316 if (pid < 0)
317 return error("Could not spawn pack-objects");
318 close(1);
319 dup2(in, 1);
320 close(in);
321 prepare_revision_walk(&revs);
322 traverse_commit_list(&revs, show_commit, show_object);
323 close(1);
324 while (waitpid(pid, &status, 0) < 0)
325 if (errno != EINTR)
326 return -1;
327 if (!WIFEXITED(status) || WEXITSTATUS(status))
328 return error ("pack-objects died");
329 return 0;
330}
331
332static int unbundle(struct bundle_header *header, int bundle_fd,
333 int argc, const char **argv)
334{
335 const char *argv_index_pack[] = {"index-pack", "--stdin", NULL};
336 int pid, status, dev_null;
337
338 if (verify_bundle(header))
339 return -1;
340 dev_null = open("/dev/null", O_WRONLY);
341 pid = fork_with_pipe(argv_index_pack, &bundle_fd, &dev_null);
342 if (pid < 0)
343 return error("Could not spawn index-pack");
344 while (waitpid(pid, &status, 0) < 0)
345 if (errno != EINTR)
346 return error("index-pack died");
347 if (!WIFEXITED(status) || WEXITSTATUS(status))
348 return error("index-pack exited with status %d",
349 WEXITSTATUS(status));
350 return list_heads(header, argc, argv);
351}
352
353int cmd_bundle(int argc, const char **argv, const char *prefix)
354{
355 struct bundle_header header;
356 int nongit = 0;
357 const char *cmd, *bundle_file;
358 int bundle_fd = -1;
359 char buffer[PATH_MAX];
360
361 if (argc < 3)
362 usage(bundle_usage);
363
364 cmd = argv[1];
365 bundle_file = argv[2];
366 argc -= 2;
367 argv += 2;
368
369 prefix = setup_git_directory_gently(&nongit);
370 if (prefix && bundle_file[0] != '/') {
371 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
372 bundle_file = buffer;
373 }
374
375 memset(&header, 0, sizeof(header));
376 if (strcmp(cmd, "create") &&
377 !(bundle_fd = read_header(bundle_file, &header)))
378 return 1;
379
380 if (!strcmp(cmd, "verify")) {
381 close(bundle_fd);
382 if (verify_bundle(&header))
383 return 1;
384 fprintf(stderr, "%s is okay\n", bundle_file);
385 return 0;
386 }
387 if (!strcmp(cmd, "list-heads")) {
388 close(bundle_fd);
389 return !!list_heads(&header, argc, argv);
390 }
391 if (!strcmp(cmd, "create")) {
392 if (nongit)
393 die("Need a repository to create a bundle.");
394 return !!create_bundle(&header, bundle_file, argc, argv);
395 } else if (!strcmp(cmd, "unbundle")) {
396 if (nongit)
397 die("Need a repository to unbundle.");
398 return !!unbundle(&header, bundle_fd, argc, argv);
399 } else
400 usage(bundle_usage);
401}
402