commit-graph: pass a 'struct repository *' in more places
[git.git] / bundle.c
blob2a0d744d3fa51b2bbbb307a04f7e32a45b1b690d
1 #include "cache.h"
2 #include "lockfile.h"
3 #include "bundle.h"
4 #include "object-store.h"
5 #include "repository.h"
6 #include "object.h"
7 #include "commit.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "list-objects.h"
11 #include "run-command.h"
12 #include "refs.h"
13 #include "argv-array.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);
23 list->nr++;
26 static const struct git_hash_algo *detect_hash_algo(struct strbuf *buf)
28 size_t len = strcspn(buf->buf, " \n");
29 int algo;
31 algo = hash_algo_by_length(len / 2);
32 if (algo == GIT_HASH_UNKNOWN)
33 return NULL;
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;
41 int status = 0;
43 /* The bundle header begins with the signature */
44 if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
45 strcmp(buf.buf, bundle_signature)) {
46 if (report_path)
47 error(_("'%s' does not look like a v2 bundle file"),
48 report_path);
49 status = -1;
50 goto abort;
53 /* The bundle header ends with an empty line */
54 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
55 buf.len && buf.buf[0] != '\n') {
56 struct object_id oid;
57 int is_prereq = 0;
58 const char *p;
60 if (*buf.buf == '-') {
61 is_prereq = 1;
62 strbuf_remove(&buf, 0, 1);
64 strbuf_rtrim(&buf);
66 if (!header->hash_algo) {
67 header->hash_algo = detect_hash_algo(&buf);
68 if (!header->hash_algo) {
69 error(_("unknown hash algorithm length"));
70 status = -1;
71 break;
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)) {
83 if (report_path)
84 error(_("unrecognized header: %s%s (%d)"),
85 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
86 status = -1;
87 break;
88 } else {
89 if (is_prereq)
90 add_to_ref_list(&oid, "", &header->prerequisites);
91 else
92 add_to_ref_list(&oid, p + 1, &header->references);
96 abort:
97 if (status) {
98 close(fd);
99 fd = -1;
101 strbuf_release(&buf);
102 return fd;
105 int read_bundle_header(const char *path, struct bundle_header *header)
107 int fd = open(path, O_RDONLY);
109 if (fd < 0)
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);
119 if (fd < 0)
120 return 0;
121 memset(&header, 0, sizeof(header));
122 fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
123 if (fd >= 0)
124 close(fd);
125 return (fd >= 0);
128 static int list_refs(struct ref_list *r, int argc, const char **argv)
130 int i;
132 for (i = 0; i < r->nr; i++) {
133 if (argc > 1) {
134 int j;
135 for (j = 1; j < argc; j++)
136 if (!strcmp(r->list[i].name, argv[j]))
137 break;
138 if (j == argc)
139 continue;
141 printf("%s %s\n", oid_to_hex(&r->list[i].oid),
142 r->list[i].name);
144 return 0;
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,
152 int verbose)
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);
172 if (o) {
173 o->flags |= PREREQ_MARK;
174 add_pending_object(&revs, o, e->name);
175 continue;
177 if (++ret == 1)
178 error("%s", message);
179 error("%s %s", oid_to_hex(&e->oid), e->name);
181 if (revs.pending.nr != p->nr)
182 return ret;
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"));
189 i = req_nr;
190 while (i && (commit = get_revision(&revs)))
191 if (commit->object.flags & PREREQ_MARK)
192 i--;
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)
199 continue;
200 if (++ret == 1)
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);
209 if (commit)
210 clear_commit_marks(commit, ALL_REV_FLAGS);
213 if (verbose) {
214 struct ref_list *r;
216 r = &header->references;
217 printf_ln(Q_("The bundle contains this ref:",
218 "The bundle contains these %d refs:",
219 r->nr),
220 r->nr);
221 list_refs(r, 0, NULL);
222 r = &header->prerequisites;
223 if (!r->nr) {
224 printf_ln(_("The bundle records a complete history."));
225 } else {
226 printf_ln(Q_("The bundle requires this ref:",
227 "The bundle requires these %d refs:",
228 r->nr),
229 r->nr);
230 list_refs(r, 0, NULL);
233 return ret;
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)
243 unsigned long size;
244 enum object_type type;
245 char *buf = NULL, *line, *lineend;
246 timestamp_t date;
247 int result = 1;
249 if (revs->max_age == -1 && revs->min_age == -1)
250 goto out;
252 buf = read_object_file(&tag->oid, &type, &size);
253 if (!buf)
254 goto out;
255 line = memmem(buf, size, "\ntagger ", 8);
256 if (!line++)
257 goto out;
258 lineend = memchr(line, '\n', buf + size - line);
259 line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
260 if (!line++)
261 goto out;
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);
265 out:
266 free(buf);
267 return result;
271 /* Write the pack data to bundle_fd */
272 static int write_pack_data(int bundle_fd, struct rev_info *revs, struct argv_array *pack_options)
274 struct child_process pack_objects = CHILD_PROCESS_INIT;
275 int i;
277 argv_array_pushl(&pack_objects.args,
278 "pack-objects",
279 "--stdout", "--thin", "--delta-base-offset",
280 NULL);
281 argv_array_pushv(&pack_objects.args, pack_options->argv);
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);
295 return -1;
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"));
312 return 0;
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;
321 FILE *rls_fout;
322 int i;
324 argv_array_pushl(&rls.args,
325 "rev-list", "--boundary", "--pretty=oneline",
326 NULL);
327 for (i = 1; i < argc; i++)
328 argv_array_push(&rls.args, argv[i]);
329 rls.out = -1;
330 rls.git_cmd = 1;
331 if (start_command(&rls))
332 return -1;
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,
340 buf.buf);
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,
346 buf.buf);
347 object->flags |= SHOWN;
350 strbuf_release(&buf);
351 fclose(rls_fout);
352 if (finish_command(&rls))
353 return error(_("rev-list died"));
354 return 0;
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
364 * on error.
366 static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
368 int i;
369 int ref_count = 0;
371 for (i = 0; i < revs->pending.nr; i++) {
372 struct object_array_entry *e = revs->pending.objects + i;
373 struct object_id oid;
374 char *ref;
375 const char *display_ref;
376 int flag;
378 if (e->item->flags & UNINTERESTING)
379 continue;
380 if (dwim_ref(e->name, strlen(e->name), &oid, &ref) != 1)
381 goto skip_write_ref;
382 if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag))
383 flag = 0;
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;
389 goto skip_write_ref;
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
399 * constraints.
401 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
402 warning(_("ref '%s' is excluded by the rev-list options"),
403 e->name);
404 goto skip_write_ref;
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
410 * itself.
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
416 * "v1.0..v2.0")?
418 struct commit *one = lookup_commit_reference(revs->repo, &oid);
419 struct object *obj;
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"
428 * error.
430 obj = parse_object_or_die(&oid, e->name);
431 obj->flags |= SHOWN;
432 add_pending_object(revs, obj, e->name);
434 goto skip_write_ref;
437 ref_count++;
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);
442 skip_write_ref:
443 free(ref);
446 /* end header */
447 write_or_die(bundle_fd, "\n", 1);
448 return ref_count;
451 int create_bundle(struct repository *r, const char *path,
452 int argc, const char **argv, struct argv_array *pack_options)
454 struct lock_file lock = LOCK_INIT;
455 int bundle_fd = -1;
456 int bundle_to_stdout;
457 int ref_count = 0;
458 struct rev_info revs;
460 bundle_to_stdout = !strcmp(path, "-");
461 if (bundle_to_stdout)
462 bundle_fd = 1;
463 else
464 bundle_fd = hold_lock_file_for_update(&lock, path,
465 LOCK_DIE_ON_ERROR);
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))
476 goto err;
478 argc = setup_revisions(argc, argv, &revs, NULL);
480 if (argc > 1) {
481 error(_("unrecognized argument: %s"), argv[1]);
482 goto err;
485 object_array_remove_duplicates(&revs.pending);
487 ref_count = write_bundle_refs(bundle_fd, &revs);
488 if (!ref_count)
489 die(_("Refusing to create empty bundle."));
490 else if (ref_count < 0)
491 goto err;
493 /* write pack */
494 if (write_pack_data(bundle_fd, &revs, pack_options))
495 goto err;
497 if (!bundle_to_stdout) {
498 if (commit_lock_file(&lock))
499 die_errno(_("cannot create '%s'"), path);
501 return 0;
502 err:
503 rollback_lock_file(&lock);
504 return -1;
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))
518 return -1;
519 ip.argv = argv_index_pack;
520 ip.in = bundle_fd;
521 ip.no_stdout = 1;
522 ip.git_cmd = 1;
523 if (run_command(&ip))
524 return error(_("index-pack died"));
525 return 0;