l10n: po-id for 2.35 (round 2)
[alt-git.git] / bundle.c
bloba0bb687b0f4ea30d44883dcb9d4de5bf1e5963ea
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 "strvec.h"
16 static const char v2_bundle_signature[] = "# v2 git bundle\n";
17 static const char v3_bundle_signature[] = "# v3 git bundle\n";
18 static struct {
19 int version;
20 const char *signature;
21 } bundle_sigs[] = {
22 { 2, v2_bundle_signature },
23 { 3, v3_bundle_signature },
26 void bundle_header_init(struct bundle_header *header)
28 struct bundle_header blank = BUNDLE_HEADER_INIT;
29 memcpy(header, &blank, sizeof(*header));
32 void bundle_header_release(struct bundle_header *header)
34 string_list_clear(&header->prerequisites, 1);
35 string_list_clear(&header->references, 1);
38 static int parse_capability(struct bundle_header *header, const char *capability)
40 const char *arg;
41 if (skip_prefix(capability, "object-format=", &arg)) {
42 int algo = hash_algo_by_name(arg);
43 if (algo == GIT_HASH_UNKNOWN)
44 return error(_("unrecognized bundle hash algorithm: %s"), arg);
45 header->hash_algo = &hash_algos[algo];
46 return 0;
48 return error(_("unknown capability '%s'"), capability);
51 static int parse_bundle_signature(struct bundle_header *header, const char *line)
53 int i;
55 for (i = 0; i < ARRAY_SIZE(bundle_sigs); i++) {
56 if (!strcmp(line, bundle_sigs[i].signature)) {
57 header->version = bundle_sigs[i].version;
58 return 0;
61 return -1;
64 static int parse_bundle_header(int fd, struct bundle_header *header,
65 const char *report_path)
67 struct strbuf buf = STRBUF_INIT;
68 int status = 0;
70 /* The bundle header begins with the signature */
71 if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
72 parse_bundle_signature(header, buf.buf)) {
73 if (report_path)
74 error(_("'%s' does not look like a v2 or v3 bundle file"),
75 report_path);
76 status = -1;
77 goto abort;
80 header->hash_algo = the_hash_algo;
82 /* The bundle header ends with an empty line */
83 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
84 buf.len && buf.buf[0] != '\n') {
85 struct object_id oid;
86 int is_prereq = 0;
87 const char *p;
89 strbuf_rtrim(&buf);
91 if (header->version == 3 && *buf.buf == '@') {
92 if (parse_capability(header, buf.buf + 1)) {
93 status = -1;
94 break;
96 continue;
99 if (*buf.buf == '-') {
100 is_prereq = 1;
101 strbuf_remove(&buf, 0, 1);
105 * Tip lines have object name, SP, and refname.
106 * Prerequisites have object name that is optionally
107 * followed by SP and subject line.
109 if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) ||
110 (*p && !isspace(*p)) ||
111 (!is_prereq && !*p)) {
112 if (report_path)
113 error(_("unrecognized header: %s%s (%d)"),
114 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
115 status = -1;
116 break;
117 } else {
118 struct object_id *dup = oiddup(&oid);
119 if (is_prereq)
120 string_list_append(&header->prerequisites, "")->util = dup;
121 else
122 string_list_append(&header->references, p + 1)->util = dup;
126 abort:
127 if (status) {
128 close(fd);
129 fd = -1;
131 strbuf_release(&buf);
132 return fd;
135 int read_bundle_header(const char *path, struct bundle_header *header)
137 int fd = open(path, O_RDONLY);
139 if (fd < 0)
140 return error(_("could not open '%s'"), path);
141 return parse_bundle_header(fd, header, path);
144 int is_bundle(const char *path, int quiet)
146 struct bundle_header header = BUNDLE_HEADER_INIT;
147 int fd = open(path, O_RDONLY);
149 if (fd < 0)
150 return 0;
151 fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
152 if (fd >= 0)
153 close(fd);
154 bundle_header_release(&header);
155 return (fd >= 0);
158 static int list_refs(struct string_list *r, int argc, const char **argv)
160 int i;
162 for (i = 0; i < r->nr; i++) {
163 struct object_id *oid;
164 const char *name;
166 if (argc > 1) {
167 int j;
168 for (j = 1; j < argc; j++)
169 if (!strcmp(r->items[i].string, argv[j]))
170 break;
171 if (j == argc)
172 continue;
175 oid = r->items[i].util;
176 name = r->items[i].string;
177 printf("%s %s\n", oid_to_hex(oid), name);
179 return 0;
182 /* Remember to update object flag allocation in object.h */
183 #define PREREQ_MARK (1u<<16)
185 int verify_bundle(struct repository *r,
186 struct bundle_header *header,
187 int verbose)
190 * Do fast check, then if any prereqs are missing then go line by line
191 * to be verbose about the errors
193 struct string_list *p = &header->prerequisites;
194 struct rev_info revs;
195 const char *argv[] = {NULL, "--all", NULL};
196 struct commit *commit;
197 int i, ret = 0, req_nr;
198 const char *message = _("Repository lacks these prerequisite commits:");
200 if (!r || !r->objects || !r->objects->odb)
201 return error(_("need a repository to verify a bundle"));
203 repo_init_revisions(r, &revs, NULL);
204 for (i = 0; i < p->nr; i++) {
205 struct string_list_item *e = p->items + i;
206 const char *name = e->string;
207 struct object_id *oid = e->util;
208 struct object *o = parse_object(r, oid);
209 if (o) {
210 o->flags |= PREREQ_MARK;
211 add_pending_object(&revs, o, name);
212 continue;
214 if (++ret == 1)
215 error("%s", message);
216 error("%s %s", oid_to_hex(oid), name);
218 if (revs.pending.nr != p->nr)
219 return ret;
220 req_nr = revs.pending.nr;
221 setup_revisions(2, argv, &revs, NULL);
223 if (prepare_revision_walk(&revs))
224 die(_("revision walk setup failed"));
226 i = req_nr;
227 while (i && (commit = get_revision(&revs)))
228 if (commit->object.flags & PREREQ_MARK)
229 i--;
231 for (i = 0; i < p->nr; i++) {
232 struct string_list_item *e = p->items + i;
233 const char *name = e->string;
234 const struct object_id *oid = e->util;
235 struct object *o = parse_object(r, oid);
236 assert(o); /* otherwise we'd have returned early */
237 if (o->flags & SHOWN)
238 continue;
239 if (++ret == 1)
240 error("%s", message);
241 error("%s %s", oid_to_hex(oid), name);
244 /* Clean up objects used, as they will be reused. */
245 for (i = 0; i < p->nr; i++) {
246 struct string_list_item *e = p->items + i;
247 struct object_id *oid = e->util;
248 commit = lookup_commit_reference_gently(r, oid, 1);
249 if (commit)
250 clear_commit_marks(commit, ALL_REV_FLAGS);
253 if (verbose) {
254 struct string_list *r;
256 r = &header->references;
257 printf_ln(Q_("The bundle contains this ref:",
258 "The bundle contains these %d refs:",
259 r->nr),
260 r->nr);
261 list_refs(r, 0, NULL);
262 r = &header->prerequisites;
263 if (!r->nr) {
264 printf_ln(_("The bundle records a complete history."));
265 } else {
266 printf_ln(Q_("The bundle requires this ref:",
267 "The bundle requires these %d refs:",
268 r->nr),
269 r->nr);
270 list_refs(r, 0, NULL);
273 return ret;
276 int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
278 return list_refs(&header->references, argc, argv);
281 static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
283 unsigned long size;
284 enum object_type type;
285 char *buf = NULL, *line, *lineend;
286 timestamp_t date;
287 int result = 1;
289 if (revs->max_age == -1 && revs->min_age == -1)
290 goto out;
292 buf = read_object_file(&tag->oid, &type, &size);
293 if (!buf)
294 goto out;
295 line = memmem(buf, size, "\ntagger ", 8);
296 if (!line++)
297 goto out;
298 lineend = memchr(line, '\n', buf + size - line);
299 line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
300 if (!line++)
301 goto out;
302 date = parse_timestamp(line, NULL, 10);
303 result = (revs->max_age == -1 || revs->max_age < date) &&
304 (revs->min_age == -1 || revs->min_age > date);
305 out:
306 free(buf);
307 return result;
311 /* Write the pack data to bundle_fd */
312 static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options)
314 struct child_process pack_objects = CHILD_PROCESS_INIT;
315 int i;
317 strvec_pushl(&pack_objects.args,
318 "pack-objects",
319 "--stdout", "--thin", "--delta-base-offset",
320 NULL);
321 strvec_pushv(&pack_objects.args, pack_options->v);
322 pack_objects.in = -1;
323 pack_objects.out = bundle_fd;
324 pack_objects.git_cmd = 1;
327 * start_command() will close our descriptor if it's >1. Duplicate it
328 * to avoid surprising the caller.
330 if (pack_objects.out > 1) {
331 pack_objects.out = dup(pack_objects.out);
332 if (pack_objects.out < 0) {
333 error_errno(_("unable to dup bundle descriptor"));
334 child_process_clear(&pack_objects);
335 return -1;
339 if (start_command(&pack_objects))
340 return error(_("Could not spawn pack-objects"));
342 for (i = 0; i < revs->pending.nr; i++) {
343 struct object *object = revs->pending.objects[i].item;
344 if (object->flags & UNINTERESTING)
345 write_or_die(pack_objects.in, "^", 1);
346 write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
347 write_or_die(pack_objects.in, "\n", 1);
349 close(pack_objects.in);
350 if (finish_command(&pack_objects))
351 return error(_("pack-objects died"));
352 return 0;
356 * Write out bundle refs based on the tips already
357 * parsed into revs.pending. As a side effect, may
358 * manipulate revs.pending to include additional
359 * necessary objects (like tags).
361 * Returns the number of refs written, or negative
362 * on error.
364 static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
366 int i;
367 int ref_count = 0;
369 for (i = 0; i < revs->pending.nr; i++) {
370 struct object_array_entry *e = revs->pending.objects + i;
371 struct object_id oid;
372 char *ref;
373 const char *display_ref;
374 int flag;
376 if (e->item->flags & UNINTERESTING)
377 continue;
378 if (dwim_ref(e->name, strlen(e->name), &oid, &ref, 0) != 1)
379 goto skip_write_ref;
380 if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag))
381 flag = 0;
382 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
384 if (e->item->type == OBJ_TAG &&
385 !is_tag_in_date_range(e->item, revs)) {
386 e->item->flags |= UNINTERESTING;
387 goto skip_write_ref;
391 * Make sure the refs we wrote out is correct; --max-count and
392 * other limiting options could have prevented all the tips
393 * from getting output.
395 * Non commit objects such as tags and blobs do not have
396 * this issue as they are not affected by those extra
397 * constraints.
399 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
400 warning(_("ref '%s' is excluded by the rev-list options"),
401 e->name);
402 goto skip_write_ref;
405 * If you run "git bundle create bndl v1.0..v2.0", the
406 * name of the positive ref is "v2.0" but that is the
407 * commit that is referenced by the tag, and not the tag
408 * itself.
410 if (!oideq(&oid, &e->item->oid)) {
412 * Is this the positive end of a range expressed
413 * in terms of a tag (e.g. v2.0 from the range
414 * "v1.0..v2.0")?
416 struct commit *one = lookup_commit_reference(revs->repo, &oid);
417 struct object *obj;
419 if (e->item == &(one->object)) {
421 * Need to include e->name as an
422 * independent ref to the pack-objects
423 * input, so that the tag is included
424 * in the output; otherwise we would
425 * end up triggering "empty bundle"
426 * error.
428 obj = parse_object_or_die(&oid, e->name);
429 obj->flags |= SHOWN;
430 add_pending_object(revs, obj, e->name);
432 goto skip_write_ref;
435 ref_count++;
436 write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
437 write_or_die(bundle_fd, " ", 1);
438 write_or_die(bundle_fd, display_ref, strlen(display_ref));
439 write_or_die(bundle_fd, "\n", 1);
440 skip_write_ref:
441 free(ref);
444 /* end header */
445 write_or_die(bundle_fd, "\n", 1);
446 return ref_count;
449 struct bundle_prerequisites_info {
450 struct object_array *pending;
451 int fd;
454 static void write_bundle_prerequisites(struct commit *commit, void *data)
456 struct bundle_prerequisites_info *bpi = data;
457 struct object *object;
458 struct pretty_print_context ctx = { 0 };
459 struct strbuf buf = STRBUF_INIT;
461 if (!(commit->object.flags & BOUNDARY))
462 return;
463 strbuf_addf(&buf, "-%s ", oid_to_hex(&commit->object.oid));
464 write_or_die(bpi->fd, buf.buf, buf.len);
466 ctx.fmt = CMIT_FMT_ONELINE;
467 ctx.output_encoding = get_log_output_encoding();
468 strbuf_reset(&buf);
469 pretty_print_commit(&ctx, commit, &buf);
470 strbuf_trim(&buf);
472 object = (struct object *)commit;
473 object->flags |= UNINTERESTING;
474 add_object_array_with_path(object, buf.buf, bpi->pending, S_IFINVALID,
475 NULL);
476 strbuf_addch(&buf, '\n');
477 write_or_die(bpi->fd, buf.buf, buf.len);
478 strbuf_release(&buf);
481 int create_bundle(struct repository *r, const char *path,
482 int argc, const char **argv, struct strvec *pack_options, int version)
484 struct lock_file lock = LOCK_INIT;
485 int bundle_fd = -1;
486 int bundle_to_stdout;
487 int ref_count = 0;
488 struct rev_info revs, revs_copy;
489 int min_version = the_hash_algo == &hash_algos[GIT_HASH_SHA1] ? 2 : 3;
490 struct bundle_prerequisites_info bpi;
491 int i;
493 bundle_to_stdout = !strcmp(path, "-");
494 if (bundle_to_stdout)
495 bundle_fd = 1;
496 else
497 bundle_fd = hold_lock_file_for_update(&lock, path,
498 LOCK_DIE_ON_ERROR);
500 if (version == -1)
501 version = min_version;
503 if (version < 2 || version > 3) {
504 die(_("unsupported bundle version %d"), version);
505 } else if (version < min_version) {
506 die(_("cannot write bundle version %d with algorithm %s"), version, the_hash_algo->name);
507 } else if (version == 2) {
508 write_or_die(bundle_fd, v2_bundle_signature, strlen(v2_bundle_signature));
509 } else {
510 const char *capability = "@object-format=";
511 write_or_die(bundle_fd, v3_bundle_signature, strlen(v3_bundle_signature));
512 write_or_die(bundle_fd, capability, strlen(capability));
513 write_or_die(bundle_fd, the_hash_algo->name, strlen(the_hash_algo->name));
514 write_or_die(bundle_fd, "\n", 1);
517 /* init revs to list objects for pack-objects later */
518 save_commit_buffer = 0;
519 repo_init_revisions(r, &revs, NULL);
521 argc = setup_revisions(argc, argv, &revs, NULL);
523 if (argc > 1) {
524 error(_("unrecognized argument: %s"), argv[1]);
525 goto err;
528 /* save revs.pending in revs_copy for later use */
529 memcpy(&revs_copy, &revs, sizeof(revs));
530 revs_copy.pending.nr = 0;
531 revs_copy.pending.alloc = 0;
532 revs_copy.pending.objects = NULL;
533 for (i = 0; i < revs.pending.nr; i++) {
534 struct object_array_entry *e = revs.pending.objects + i;
535 if (e)
536 add_object_array_with_path(e->item, e->name,
537 &revs_copy.pending,
538 e->mode, e->path);
541 /* write prerequisites */
542 revs.boundary = 1;
543 if (prepare_revision_walk(&revs))
544 die("revision walk setup failed");
545 bpi.fd = bundle_fd;
546 bpi.pending = &revs_copy.pending;
547 traverse_commit_list(&revs, write_bundle_prerequisites, NULL, &bpi);
548 object_array_remove_duplicates(&revs_copy.pending);
550 /* write bundle refs */
551 ref_count = write_bundle_refs(bundle_fd, &revs_copy);
552 if (!ref_count)
553 die(_("Refusing to create empty bundle."));
554 else if (ref_count < 0)
555 goto err;
557 /* write pack */
558 if (write_pack_data(bundle_fd, &revs_copy, pack_options))
559 goto err;
561 if (!bundle_to_stdout) {
562 if (commit_lock_file(&lock))
563 die_errno(_("cannot create '%s'"), path);
565 return 0;
566 err:
567 rollback_lock_file(&lock);
568 return -1;
571 int unbundle(struct repository *r, struct bundle_header *header,
572 int bundle_fd, struct strvec *extra_index_pack_args)
574 struct child_process ip = CHILD_PROCESS_INIT;
575 strvec_pushl(&ip.args, "index-pack", "--fix-thin", "--stdin", NULL);
577 if (extra_index_pack_args) {
578 strvec_pushv(&ip.args, extra_index_pack_args->v);
579 strvec_clear(extra_index_pack_args);
582 if (verify_bundle(r, header, 0))
583 return -1;
584 ip.in = bundle_fd;
585 ip.no_stdout = 1;
586 ip.git_cmd = 1;
587 if (run_command(&ip))
588 return error(_("index-pack died"));
589 return 0;