7 #include "list-objects.h"
8 #include "run-command.h"
11 static const char bundle_signature[] = "# v2 git bundle\n";
13 static void add_to_ref_list(const unsigned char *sha1, const char *name,
14 struct ref_list *list)
16 if (list->nr + 1 >= list->alloc) {
17 list->alloc = alloc_nr(list->nr + 1);
18 list->list = xrealloc(list->list,
19 list->alloc * sizeof(list->list[0]));
21 memcpy(list->list[list->nr].sha1, sha1, 20);
22 list->list[list->nr].name = xstrdup(name);
26 /* Eventually this should go to strbuf.[ch] */
27 static int strbuf_readline_fd(struct strbuf *sb, int fd)
33 ssize_t len = xread(fd, &ch, 1);
43 static int parse_bundle_header(int fd, struct bundle_header *header,
44 const char *report_path)
46 struct strbuf buf = STRBUF_INIT;
49 /* The bundle header begins with the signature */
50 if (strbuf_readline_fd(&buf, fd) ||
51 strcmp(buf.buf, bundle_signature)) {
53 error("'%s' does not look like a v2 bundle file",
59 /* The bundle header ends with an empty line */
60 while (!strbuf_readline_fd(&buf, fd) &&
61 buf.len && buf.buf[0] != '\n') {
62 unsigned char sha1[20];
65 if (*buf.buf == '-') {
67 strbuf_remove(&buf, 0, 1);
72 * Tip lines have object name, SP, and refname.
73 * Prerequisites have object name that is optionally
74 * followed by SP and subject line.
76 if (get_sha1_hex(buf.buf, sha1) ||
77 (40 <= buf.len && !isspace(buf.buf[40])) ||
78 (!is_prereq && buf.len <= 40)) {
80 error("unrecognized header: %s%s (%d)",
81 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
86 add_to_ref_list(sha1, "", &header->prerequisites);
88 add_to_ref_list(sha1, buf.buf + 41, &header->references);
101 int read_bundle_header(const char *path, struct bundle_header *header)
103 int fd = open(path, O_RDONLY);
106 return error("could not open '%s'", path);
107 return parse_bundle_header(fd, header, path);
110 int is_bundle(const char *path, int quiet)
112 struct bundle_header header;
113 int fd = open(path, O_RDONLY);
117 memset(&header, 0, sizeof(header));
118 fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
124 static int list_refs(struct ref_list *r, int argc, const char **argv)
128 for (i = 0; i < r->nr; i++) {
131 for (j = 1; j < argc; j++)
132 if (!strcmp(r->list[i].name, argv[j]))
137 printf("%s %s\n", sha1_to_hex(r->list[i].sha1),
143 #define PREREQ_MARK (1u<<16)
145 int verify_bundle(struct bundle_header *header, int verbose)
148 * Do fast check, then if any prereqs are missing then go line by line
149 * to be verbose about the errors
151 struct ref_list *p = &header->prerequisites;
152 struct rev_info revs;
153 const char *argv[] = {NULL, "--all", NULL};
154 struct object_array refs;
155 struct commit *commit;
156 int i, ret = 0, req_nr;
157 const char *message = "Repository lacks these prerequisite commits:";
159 init_revisions(&revs, NULL);
160 for (i = 0; i < p->nr; i++) {
161 struct ref_list_entry *e = p->list + i;
162 struct object *o = parse_object(e->sha1);
164 o->flags |= PREREQ_MARK;
165 add_pending_object(&revs, o, e->name);
169 error("%s", message);
170 error("%s %s", sha1_to_hex(e->sha1), e->name);
172 if (revs.pending.nr != p->nr)
174 req_nr = revs.pending.nr;
175 setup_revisions(2, argv, &revs, NULL);
178 revs.leak_pending = 1;
180 if (prepare_revision_walk(&revs))
181 die("revision walk setup failed");
184 while (i && (commit = get_revision(&revs)))
185 if (commit->object.flags & PREREQ_MARK)
188 for (i = 0; i < req_nr; i++)
189 if (!(refs.objects[i].item->flags & SHOWN)) {
191 error("%s", message);
192 error("%s %s", sha1_to_hex(refs.objects[i].item->sha1),
193 refs.objects[i].name);
196 clear_commit_marks_for_object_array(&refs, ALL_REV_FLAGS);
202 r = &header->references;
203 printf("The bundle contains %d ref%s\n",
204 r->nr, (1 < r->nr) ? "s" : "");
205 list_refs(r, 0, NULL);
206 r = &header->prerequisites;
207 printf("The bundle requires these %d ref%s\n",
208 r->nr, (1 < r->nr) ? "s" : "");
209 list_refs(r, 0, NULL);
214 int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
216 return list_refs(&header->references, argc, argv);
219 static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
222 enum object_type type;
223 char *buf, *line, *lineend;
226 if (revs->max_age == -1 && revs->min_age == -1)
229 buf = read_sha1_file(tag->sha1, &type, &size);
232 line = memmem(buf, size, "\ntagger ", 8);
235 lineend = memchr(line, buf + size - line, '\n');
236 line = memchr(line, lineend ? lineend - line : buf + size - line, '>');
239 date = strtoul(line, NULL, 10);
241 return (revs->max_age == -1 || revs->max_age < date) &&
242 (revs->min_age == -1 || revs->min_age > date);
245 int create_bundle(struct bundle_header *header, const char *path,
246 int argc, const char **argv)
248 static struct lock_file lock;
250 int bundle_to_stdout;
251 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
252 const char **argv_pack = xmalloc(6 * sizeof(const char *));
253 int i, ref_count = 0;
255 struct rev_info revs;
256 struct child_process rls;
259 bundle_to_stdout = !strcmp(path, "-");
260 if (bundle_to_stdout)
263 bundle_fd = hold_lock_file_for_update(&lock, path,
266 /* write signature */
267 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
269 /* init revs to list objects for pack-objects later */
270 save_commit_buffer = 0;
271 init_revisions(&revs, NULL);
273 /* write prerequisites */
274 memcpy(argv_boundary + 3, argv + 1, argc * sizeof(const char *));
275 argv_boundary[0] = "rev-list";
276 argv_boundary[1] = "--boundary";
277 argv_boundary[2] = "--pretty=oneline";
278 argv_boundary[argc + 2] = NULL;
279 memset(&rls, 0, sizeof(rls));
280 rls.argv = argv_boundary;
283 if (start_command(&rls))
285 rls_fout = xfdopen(rls.out, "r");
286 while (fgets(buffer, sizeof(buffer), rls_fout)) {
287 unsigned char sha1[20];
288 if (buffer[0] == '-') {
289 write_or_die(bundle_fd, buffer, strlen(buffer));
290 if (!get_sha1_hex(buffer + 1, sha1)) {
291 struct object *object = parse_object(sha1);
292 object->flags |= UNINTERESTING;
293 add_pending_object(&revs, object, buffer);
295 } else if (!get_sha1_hex(buffer, sha1)) {
296 struct object *object = parse_object(sha1);
297 object->flags |= SHOWN;
301 if (finish_command(&rls))
302 return error("rev-list died");
304 /* write references */
305 argc = setup_revisions(argc, argv, &revs, NULL);
308 return error("unrecognized argument: %s'", argv[1]);
310 object_array_remove_duplicates(&revs.pending);
312 for (i = 0; i < revs.pending.nr; i++) {
313 struct object_array_entry *e = revs.pending.objects + i;
314 unsigned char sha1[20];
316 const char *display_ref;
319 if (e->item->flags & UNINTERESTING)
321 if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
323 if (!resolve_ref(e->name, sha1, 1, &flag))
325 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
327 if (e->item->type == OBJ_TAG &&
328 !is_tag_in_date_range(e->item, &revs)) {
329 e->item->flags |= UNINTERESTING;
334 * Make sure the refs we wrote out is correct; --max-count and
335 * other limiting options could have prevented all the tips
336 * from getting output.
338 * Non commit objects such as tags and blobs do not have
339 * this issue as they are not affected by those extra
342 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
343 warning("ref '%s' is excluded by the rev-list options",
349 * If you run "git bundle create bndl v1.0..v2.0", the
350 * name of the positive ref is "v2.0" but that is the
351 * commit that is referenced by the tag, and not the tag
354 if (hashcmp(sha1, e->item->sha1)) {
356 * Is this the positive end of a range expressed
357 * in terms of a tag (e.g. v2.0 from the range
360 struct commit *one = lookup_commit_reference(sha1);
363 if (e->item == &(one->object)) {
365 * Need to include e->name as an
366 * independent ref to the pack-objects
367 * input, so that the tag is included
368 * in the output; otherwise we would
369 * end up triggering "empty bundle"
372 obj = parse_object(sha1);
374 add_pending_object(&revs, obj, e->name);
381 write_or_die(bundle_fd, sha1_to_hex(e->item->sha1), 40);
382 write_or_die(bundle_fd, " ", 1);
383 write_or_die(bundle_fd, display_ref, strlen(display_ref));
384 write_or_die(bundle_fd, "\n", 1);
388 die ("Refusing to create empty bundle.");
391 write_or_die(bundle_fd, "\n", 1);
394 argv_pack[0] = "pack-objects";
395 argv_pack[1] = "--all-progress-implied";
396 argv_pack[2] = "--stdout";
397 argv_pack[3] = "--thin";
398 argv_pack[4] = "--delta-base-offset";
400 memset(&rls, 0, sizeof(rls));
401 rls.argv = argv_pack;
405 if (start_command(&rls))
406 return error("Could not spawn pack-objects");
409 * start_command closed bundle_fd if it was > 1
410 * so set the lock fd to -1 so commit_lock_file()
411 * won't fail trying to close it.
415 for (i = 0; i < revs.pending.nr; i++) {
416 struct object *object = revs.pending.objects[i].item;
417 if (object->flags & UNINTERESTING)
418 write_or_die(rls.in, "^", 1);
419 write_or_die(rls.in, sha1_to_hex(object->sha1), 40);
420 write_or_die(rls.in, "\n", 1);
423 if (finish_command(&rls))
424 return error ("pack-objects died");
425 if (!bundle_to_stdout) {
426 if (commit_lock_file(&lock))
427 die_errno("cannot create '%s'", path);
432 int unbundle(struct bundle_header *header, int bundle_fd, int flags)
434 const char *argv_index_pack[] = {"index-pack",
435 "--fix-thin", "--stdin", NULL, NULL};
436 struct child_process ip;
438 if (flags & BUNDLE_VERBOSE)
439 argv_index_pack[3] = "-v";
441 if (verify_bundle(header, 0))
443 memset(&ip, 0, sizeof(ip));
444 ip.argv = argv_index_pack;
448 if (run_command(&ip))
449 return error("index-pack died");