4 #include "object-store.h"
5 #include "repository.h"
10 #include "list-objects.h"
11 #include "run-command.h"
15 static const char bundle_signature[] = "# v2 git bundle\n";
17 static void add_to_ref_list(const struct object_id *oid, const char *name,
18 struct ref_list *list)
20 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
21 oidcpy(&list->list[list->nr].oid, oid);
22 list->list[list->nr].name = xstrdup(name);
26 static const struct git_hash_algo *detect_hash_algo(struct strbuf *buf)
28 size_t len = strcspn(buf->buf, " \n");
31 algo = hash_algo_by_length(len / 2);
32 if (algo == GIT_HASH_UNKNOWN)
34 return &hash_algos[algo];
37 static int parse_bundle_header(int fd, struct bundle_header *header,
38 const char *report_path)
40 struct strbuf buf = STRBUF_INIT;
43 /* The bundle header begins with the signature */
44 if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
45 strcmp(buf.buf, bundle_signature)) {
47 error(_("'%s' does not look like a v2 bundle file"),
53 /* The bundle header ends with an empty line */
54 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
55 buf.len && buf.buf[0] != '\n') {
60 if (*buf.buf == '-') {
62 strbuf_remove(&buf, 0, 1);
66 if (!header->hash_algo) {
67 header->hash_algo = detect_hash_algo(&buf);
68 if (!header->hash_algo) {
69 error(_("unknown hash algorithm length"));
76 * Tip lines have object name, SP, and refname.
77 * Prerequisites have object name that is optionally
78 * followed by SP and subject line.
80 if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) ||
81 (*p && !isspace(*p)) ||
82 (!is_prereq && !*p)) {
84 error(_("unrecognized header: %s%s (%d)"),
85 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
90 add_to_ref_list(&oid, "", &header->prerequisites);
92 add_to_ref_list(&oid, p + 1, &header->references);
101 strbuf_release(&buf);
105 int read_bundle_header(const char *path, struct bundle_header *header)
107 int fd = open(path, O_RDONLY);
110 return error(_("could not open '%s'"), path);
111 return parse_bundle_header(fd, header, path);
114 int is_bundle(const char *path, int quiet)
116 struct bundle_header header;
117 int fd = open(path, O_RDONLY);
121 memset(&header, 0, sizeof(header));
122 fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
128 static int list_refs(struct ref_list *r, int argc, const char **argv)
132 for (i = 0; i < r->nr; i++) {
135 for (j = 1; j < argc; j++)
136 if (!strcmp(r->list[i].name, argv[j]))
141 printf("%s %s\n", oid_to_hex(&r->list[i].oid),
147 /* Remember to update object flag allocation in object.h */
148 #define PREREQ_MARK (1u<<16)
150 int verify_bundle(struct repository *r,
151 struct bundle_header *header,
155 * Do fast check, then if any prereqs are missing then go line by line
156 * to be verbose about the errors
158 struct ref_list *p = &header->prerequisites;
159 struct rev_info revs;
160 const char *argv[] = {NULL, "--all", NULL};
161 struct commit *commit;
162 int i, ret = 0, req_nr;
163 const char *message = _("Repository lacks these prerequisite commits:");
165 if (!r || !r->objects || !r->objects->odb)
166 return error(_("need a repository to verify a bundle"));
168 repo_init_revisions(r, &revs, NULL);
169 for (i = 0; i < p->nr; i++) {
170 struct ref_list_entry *e = p->list + i;
171 struct object *o = parse_object(r, &e->oid);
173 o->flags |= PREREQ_MARK;
174 add_pending_object(&revs, o, e->name);
178 error("%s", message);
179 error("%s %s", oid_to_hex(&e->oid), e->name);
181 if (revs.pending.nr != p->nr)
183 req_nr = revs.pending.nr;
184 setup_revisions(2, argv, &revs, NULL);
186 if (prepare_revision_walk(&revs))
187 die(_("revision walk setup failed"));
190 while (i && (commit = get_revision(&revs)))
191 if (commit->object.flags & PREREQ_MARK)
194 for (i = 0; i < p->nr; i++) {
195 struct ref_list_entry *e = p->list + i;
196 struct object *o = parse_object(r, &e->oid);
197 assert(o); /* otherwise we'd have returned early */
198 if (o->flags & SHOWN)
201 error("%s", message);
202 error("%s %s", oid_to_hex(&e->oid), e->name);
205 /* Clean up objects used, as they will be reused. */
206 for (i = 0; i < p->nr; i++) {
207 struct ref_list_entry *e = p->list + i;
208 commit = lookup_commit_reference_gently(r, &e->oid, 1);
210 clear_commit_marks(commit, ALL_REV_FLAGS);
216 r = &header->references;
217 printf_ln(Q_("The bundle contains this ref:",
218 "The bundle contains these %d refs:",
221 list_refs(r, 0, NULL);
222 r = &header->prerequisites;
224 printf_ln(_("The bundle records a complete history."));
226 printf_ln(Q_("The bundle requires this ref:",
227 "The bundle requires these %d refs:",
230 list_refs(r, 0, NULL);
236 int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
238 return list_refs(&header->references, argc, argv);
241 static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
244 enum object_type type;
245 char *buf = NULL, *line, *lineend;
249 if (revs->max_age == -1 && revs->min_age == -1)
252 buf = read_object_file(&tag->oid, &type, &size);
255 line = memmem(buf, size, "\ntagger ", 8);
258 lineend = memchr(line, '\n', buf + size - line);
259 line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
262 date = parse_timestamp(line, NULL, 10);
263 result = (revs->max_age == -1 || revs->max_age < date) &&
264 (revs->min_age == -1 || revs->min_age > date);
271 /* Write the pack data to bundle_fd */
272 static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options)
274 struct child_process pack_objects = CHILD_PROCESS_INIT;
277 strvec_pushl(&pack_objects.args,
279 "--stdout", "--thin", "--delta-base-offset",
281 strvec_pushv(&pack_objects.args, pack_options->v);
282 pack_objects.in = -1;
283 pack_objects.out = bundle_fd;
284 pack_objects.git_cmd = 1;
287 * start_command() will close our descriptor if it's >1. Duplicate it
288 * to avoid surprising the caller.
290 if (pack_objects.out > 1) {
291 pack_objects.out = dup(pack_objects.out);
292 if (pack_objects.out < 0) {
293 error_errno(_("unable to dup bundle descriptor"));
294 child_process_clear(&pack_objects);
299 if (start_command(&pack_objects))
300 return error(_("Could not spawn pack-objects"));
302 for (i = 0; i < revs->pending.nr; i++) {
303 struct object *object = revs->pending.objects[i].item;
304 if (object->flags & UNINTERESTING)
305 write_or_die(pack_objects.in, "^", 1);
306 write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
307 write_or_die(pack_objects.in, "\n", 1);
309 close(pack_objects.in);
310 if (finish_command(&pack_objects))
311 return error(_("pack-objects died"));
315 static int compute_and_write_prerequisites(int bundle_fd,
316 struct rev_info *revs,
317 int argc, const char **argv)
319 struct child_process rls = CHILD_PROCESS_INIT;
320 struct strbuf buf = STRBUF_INIT;
324 strvec_pushl(&rls.args,
325 "rev-list", "--boundary", "--pretty=oneline",
327 for (i = 1; i < argc; i++)
328 strvec_push(&rls.args, argv[i]);
331 if (start_command(&rls))
333 rls_fout = xfdopen(rls.out, "r");
334 while (strbuf_getwholeline(&buf, rls_fout, '\n') != EOF) {
335 struct object_id oid;
336 if (buf.len > 0 && buf.buf[0] == '-') {
337 write_or_die(bundle_fd, buf.buf, buf.len);
338 if (!get_oid_hex(buf.buf + 1, &oid)) {
339 struct object *object = parse_object_or_die(&oid,
341 object->flags |= UNINTERESTING;
342 add_pending_object(revs, object, buf.buf);
344 } else if (!get_oid_hex(buf.buf, &oid)) {
345 struct object *object = parse_object_or_die(&oid,
347 object->flags |= SHOWN;
350 strbuf_release(&buf);
352 if (finish_command(&rls))
353 return error(_("rev-list died"));
358 * Write out bundle refs based on the tips already
359 * parsed into revs.pending. As a side effect, may
360 * manipulate revs.pending to include additional
361 * necessary objects (like tags).
363 * Returns the number of refs written, or negative
366 static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
371 for (i = 0; i < revs->pending.nr; i++) {
372 struct object_array_entry *e = revs->pending.objects + i;
373 struct object_id oid;
375 const char *display_ref;
378 if (e->item->flags & UNINTERESTING)
380 if (dwim_ref(e->name, strlen(e->name), &oid, &ref) != 1)
382 if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag))
384 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
386 if (e->item->type == OBJ_TAG &&
387 !is_tag_in_date_range(e->item, revs)) {
388 e->item->flags |= UNINTERESTING;
393 * Make sure the refs we wrote out is correct; --max-count and
394 * other limiting options could have prevented all the tips
395 * from getting output.
397 * Non commit objects such as tags and blobs do not have
398 * this issue as they are not affected by those extra
401 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
402 warning(_("ref '%s' is excluded by the rev-list options"),
407 * If you run "git bundle create bndl v1.0..v2.0", the
408 * name of the positive ref is "v2.0" but that is the
409 * commit that is referenced by the tag, and not the tag
412 if (!oideq(&oid, &e->item->oid)) {
414 * Is this the positive end of a range expressed
415 * in terms of a tag (e.g. v2.0 from the range
418 struct commit *one = lookup_commit_reference(revs->repo, &oid);
421 if (e->item == &(one->object)) {
423 * Need to include e->name as an
424 * independent ref to the pack-objects
425 * input, so that the tag is included
426 * in the output; otherwise we would
427 * end up triggering "empty bundle"
430 obj = parse_object_or_die(&oid, e->name);
432 add_pending_object(revs, obj, e->name);
438 write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
439 write_or_die(bundle_fd, " ", 1);
440 write_or_die(bundle_fd, display_ref, strlen(display_ref));
441 write_or_die(bundle_fd, "\n", 1);
447 write_or_die(bundle_fd, "\n", 1);
451 int create_bundle(struct repository *r, const char *path,
452 int argc, const char **argv, struct strvec *pack_options)
454 struct lock_file lock = LOCK_INIT;
456 int bundle_to_stdout;
458 struct rev_info revs;
460 bundle_to_stdout = !strcmp(path, "-");
461 if (bundle_to_stdout)
464 bundle_fd = hold_lock_file_for_update(&lock, path,
467 /* write signature */
468 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
470 /* init revs to list objects for pack-objects later */
471 save_commit_buffer = 0;
472 repo_init_revisions(r, &revs, NULL);
474 /* write prerequisites */
475 if (compute_and_write_prerequisites(bundle_fd, &revs, argc, argv))
478 argc = setup_revisions(argc, argv, &revs, NULL);
481 error(_("unrecognized argument: %s"), argv[1]);
485 object_array_remove_duplicates(&revs.pending);
487 ref_count = write_bundle_refs(bundle_fd, &revs);
489 die(_("Refusing to create empty bundle."));
490 else if (ref_count < 0)
494 if (write_pack_data(bundle_fd, &revs, pack_options))
497 if (!bundle_to_stdout) {
498 if (commit_lock_file(&lock))
499 die_errno(_("cannot create '%s'"), path);
503 rollback_lock_file(&lock);
507 int unbundle(struct repository *r, struct bundle_header *header,
508 int bundle_fd, int flags)
510 const char *argv_index_pack[] = {"index-pack",
511 "--fix-thin", "--stdin", NULL, NULL};
512 struct child_process ip = CHILD_PROCESS_INIT;
514 if (flags & BUNDLE_VERBOSE)
515 argv_index_pack[3] = "-v";
517 if (verify_bundle(r, header, 0))
519 ip.argv = argv_index_pack;
523 if (run_command(&ip))
524 return error(_("index-pack died"));