Change {pre,post}-receive hooks to use stdin
[git/kirr.git] / builtin-bundle.c
blob279b8f8e582665dda3c99d800094db2f5cf5af99
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 static int verify_bundle(struct bundle_header *header)
166 * Do fast check, then if any prereqs are missing then go line by line
167 * to be verbose about the errors
169 struct ref_list *p = &header->prerequisites;
170 struct rev_info revs;
171 const char *argv[] = {NULL, "--all"};
172 struct object_array refs;
173 struct commit *commit;
174 int i, ret = 0, req_nr;
175 const char *message = "Repository lacks these prerequisite commits:";
177 init_revisions(&revs, NULL);
178 for (i = 0; i < p->nr; i++) {
179 struct ref_list_entry *e = p->list + i;
180 struct object *o = parse_object(e->sha1);
181 if (o) {
182 o->flags |= BOUNDARY_SHOW;
183 add_pending_object(&revs, o, e->name);
184 continue;
186 if (++ret == 1)
187 error(message);
188 error("%s %s", sha1_to_hex(e->sha1), e->name);
190 if (revs.pending.nr == 0)
191 return ret;
192 req_nr = revs.pending.nr;
193 setup_revisions(2, argv, &revs, NULL);
195 memset(&refs, 0, sizeof(struct object_array));
196 for (i = 0; i < revs.pending.nr; i++) {
197 struct object_array_entry *e = revs.pending.objects + i;
198 add_object_array(e->item, e->name, &refs);
201 prepare_revision_walk(&revs);
203 i = req_nr;
204 while (i && (commit = get_revision(&revs)))
205 if (commit->object.flags & BOUNDARY_SHOW)
206 i--;
208 for (i = 0; i < req_nr; i++)
209 if (!(refs.objects[i].item->flags & SHOWN)) {
210 if (++ret == 1)
211 error(message);
212 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
213 refs.objects[i].name);
216 for (i = 0; i < refs.nr; i++)
217 clear_commit_marks((struct commit *)refs.objects[i].item, -1);
219 return ret;
222 static int list_heads(struct bundle_header *header, int argc, const char **argv)
224 int i;
225 struct ref_list *r = &header->references;
227 for (i = 0; i < r->nr; i++) {
228 if (argc > 1) {
229 int j;
230 for (j = 1; j < argc; j++)
231 if (!strcmp(r->list[i].name, argv[j]))
232 break;
233 if (j == argc)
234 continue;
236 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
237 r->list[i].name);
239 return 0;
242 static void show_commit(struct commit *commit)
244 write_or_die(1, sha1_to_hex(commit->object.sha1), 40);
245 write_or_die(1, "\n", 1);
246 if (commit->parents) {
247 free_commit_list(commit->parents);
248 commit->parents = NULL;
252 static void show_object(struct object_array_entry *p)
254 /* An object with name "foo\n0000000..." can be used to
255 * confuse downstream git-pack-objects very badly.
257 const char *ep = strchr(p->name, '\n');
258 int len = ep ? ep - p->name : strlen(p->name);
259 write_or_die(1, sha1_to_hex(p->item->sha1), 40);
260 write_or_die(1, " ", 1);
261 if (len)
262 write_or_die(1, p->name, len);
263 write_or_die(1, "\n", 1);
266 static void show_edge(struct commit *commit)
268 ; /* nothing to do */
271 static int create_bundle(struct bundle_header *header, const char *path,
272 int argc, const char **argv)
274 int bundle_fd = -1;
275 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
276 const char **argv_pack = xmalloc(4 * sizeof(const char *));
277 int pid, in, out, i, status;
278 char buffer[1024];
279 struct rev_info revs;
281 bundle_fd = (!strcmp(path, "-") ? 1 :
282 open(path, O_CREAT | O_WRONLY, 0666));
283 if (bundle_fd < 0)
284 return error("Could not write to '%s'", path);
286 /* write signature */
287 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
289 /* write prerequisites */
290 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
291 argv_boundary[0] = "rev-list";
292 argv_boundary[1] = "--boundary";
293 argv_boundary[2] = "--pretty=oneline";
294 argv_boundary[argc + 2] = NULL;
295 out = -1;
296 pid = fork_with_pipe(argv_boundary, NULL, &out);
297 if (pid < 0)
298 return -1;
299 while ((i = read_string(out, buffer, sizeof(buffer))) > 0)
300 if (buffer[0] == '-')
301 write_or_die(bundle_fd, buffer, i);
302 while ((i = waitpid(pid, &status, 0)) < 0)
303 if (errno != EINTR)
304 return error("rev-list died");
305 if (!WIFEXITED(status) || WEXITSTATUS(status))
306 return error("rev-list died %d", WEXITSTATUS(status));
308 /* write references */
309 save_commit_buffer = 0;
310 init_revisions(&revs, NULL);
311 revs.tag_objects = 1;
312 revs.tree_objects = 1;
313 revs.blob_objects = 1;
314 argc = setup_revisions(argc, argv, &revs, NULL);
315 if (argc > 1)
316 return error("unrecognized argument: %s'", argv[1]);
317 for (i = 0; i < revs.pending.nr; i++) {
318 struct object_array_entry *e = revs.pending.objects + i;
319 if (!(e->item->flags & UNINTERESTING)) {
320 unsigned char sha1[20];
321 char *ref;
322 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
323 continue;
324 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
325 write_or_die(bundle_fd, " ", 1);
326 write_or_die(bundle_fd, ref, strlen(ref));
327 write_or_die(bundle_fd, "\n", 1);
328 free(ref);
332 /* end header */
333 write_or_die(bundle_fd, "\n", 1);
335 /* write pack */
336 argv_pack[0] = "pack-objects";
337 argv_pack[1] = "--all-progress";
338 argv_pack[2] = "--stdout";
339 argv_pack[3] = NULL;
340 in = -1;
341 out = bundle_fd;
342 pid = fork_with_pipe(argv_pack, &in, &out);
343 if (pid < 0)
344 return error("Could not spawn pack-objects");
345 close(1);
346 dup2(in, 1);
347 close(in);
348 prepare_revision_walk(&revs);
349 mark_edges_uninteresting(revs.commits, &revs, show_edge);
350 traverse_commit_list(&revs, show_commit, show_object);
351 close(1);
352 while (waitpid(pid, &status, 0) < 0)
353 if (errno != EINTR)
354 return -1;
355 if (!WIFEXITED(status) || WEXITSTATUS(status))
356 return error ("pack-objects died");
357 return 0;
360 static int unbundle(struct bundle_header *header, int bundle_fd,
361 int argc, const char **argv)
363 const char *argv_index_pack[] = {"index-pack", "--stdin", NULL};
364 int pid, status, dev_null;
366 if (verify_bundle(header))
367 return -1;
368 dev_null = open("/dev/null", O_WRONLY);
369 pid = fork_with_pipe(argv_index_pack, &bundle_fd, &dev_null);
370 if (pid < 0)
371 return error("Could not spawn index-pack");
372 while (waitpid(pid, &status, 0) < 0)
373 if (errno != EINTR)
374 return error("index-pack died");
375 if (!WIFEXITED(status) || WEXITSTATUS(status))
376 return error("index-pack exited with status %d",
377 WEXITSTATUS(status));
378 return list_heads(header, argc, argv);
381 int cmd_bundle(int argc, const char **argv, const char *prefix)
383 struct bundle_header header;
384 int nongit = 0;
385 const char *cmd, *bundle_file;
386 int bundle_fd = -1;
387 char buffer[PATH_MAX];
389 if (argc < 3)
390 usage(bundle_usage);
392 cmd = argv[1];
393 bundle_file = argv[2];
394 argc -= 2;
395 argv += 2;
397 prefix = setup_git_directory_gently(&nongit);
398 if (prefix && bundle_file[0] != '/') {
399 snprintf(buffer, sizeof(buffer), "%s/%s", prefix, bundle_file);
400 bundle_file = buffer;
403 memset(&header, 0, sizeof(header));
404 if (strcmp(cmd, "create") &&
405 !(bundle_fd = read_header(bundle_file, &header)))
406 return 1;
408 if (!strcmp(cmd, "verify")) {
409 close(bundle_fd);
410 if (verify_bundle(&header))
411 return 1;
412 fprintf(stderr, "%s is okay\n", bundle_file);
413 return 0;
415 if (!strcmp(cmd, "list-heads")) {
416 close(bundle_fd);
417 return !!list_heads(&header, argc, argv);
419 if (!strcmp(cmd, "create")) {
420 if (nongit)
421 die("Need a repository to create a bundle.");
422 return !!create_bundle(&header, bundle_file, argc, argv);
423 } else if (!strcmp(cmd, "unbundle")) {
424 if (nongit)
425 die("Need a repository to unbundle.");
426 return !!unbundle(&header, bundle_fd, argc, argv);
427 } else
428 usage(bundle_usage);