]> git.ipfire.org Git - thirdparty/git.git/blob - bundle.c
treewide: be explicit about dependence on gettext.h
[thirdparty/git.git] / bundle.c
1 #include "cache.h"
2 #include "lockfile.h"
3 #include "bundle.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "object-store.h"
7 #include "repository.h"
8 #include "object.h"
9 #include "commit.h"
10 #include "diff.h"
11 #include "revision.h"
12 #include "list-objects.h"
13 #include "run-command.h"
14 #include "refs.h"
15 #include "strvec.h"
16 #include "list-objects-filter-options.h"
17 #include "connected.h"
18
19 static const char v2_bundle_signature[] = "# v2 git bundle\n";
20 static const char v3_bundle_signature[] = "# v3 git bundle\n";
21 static struct {
22 int version;
23 const char *signature;
24 } bundle_sigs[] = {
25 { 2, v2_bundle_signature },
26 { 3, v3_bundle_signature },
27 };
28
29 void bundle_header_init(struct bundle_header *header)
30 {
31 struct bundle_header blank = BUNDLE_HEADER_INIT;
32 memcpy(header, &blank, sizeof(*header));
33 }
34
35 void bundle_header_release(struct bundle_header *header)
36 {
37 string_list_clear(&header->prerequisites, 1);
38 string_list_clear(&header->references, 1);
39 list_objects_filter_release(&header->filter);
40 }
41
42 static int parse_capability(struct bundle_header *header, const char *capability)
43 {
44 const char *arg;
45 if (skip_prefix(capability, "object-format=", &arg)) {
46 int algo = hash_algo_by_name(arg);
47 if (algo == GIT_HASH_UNKNOWN)
48 return error(_("unrecognized bundle hash algorithm: %s"), arg);
49 header->hash_algo = &hash_algos[algo];
50 return 0;
51 }
52 if (skip_prefix(capability, "filter=", &arg)) {
53 parse_list_objects_filter(&header->filter, arg);
54 return 0;
55 }
56 return error(_("unknown capability '%s'"), capability);
57 }
58
59 static int parse_bundle_signature(struct bundle_header *header, const char *line)
60 {
61 int i;
62
63 for (i = 0; i < ARRAY_SIZE(bundle_sigs); i++) {
64 if (!strcmp(line, bundle_sigs[i].signature)) {
65 header->version = bundle_sigs[i].version;
66 return 0;
67 }
68 }
69 return -1;
70 }
71
72 int read_bundle_header_fd(int fd, struct bundle_header *header,
73 const char *report_path)
74 {
75 struct strbuf buf = STRBUF_INIT;
76 int status = 0;
77
78 /* The bundle header begins with the signature */
79 if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
80 parse_bundle_signature(header, buf.buf)) {
81 if (report_path)
82 error(_("'%s' does not look like a v2 or v3 bundle file"),
83 report_path);
84 status = -1;
85 goto abort;
86 }
87
88 header->hash_algo = the_hash_algo;
89
90 /* The bundle header ends with an empty line */
91 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
92 buf.len && buf.buf[0] != '\n') {
93 struct object_id oid;
94 int is_prereq = 0;
95 const char *p;
96
97 strbuf_rtrim(&buf);
98
99 if (header->version == 3 && *buf.buf == '@') {
100 if (parse_capability(header, buf.buf + 1)) {
101 status = -1;
102 break;
103 }
104 continue;
105 }
106
107 if (*buf.buf == '-') {
108 is_prereq = 1;
109 strbuf_remove(&buf, 0, 1);
110 }
111
112 /*
113 * Tip lines have object name, SP, and refname.
114 * Prerequisites have object name that is optionally
115 * followed by SP and subject line.
116 */
117 if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) ||
118 (*p && !isspace(*p)) ||
119 (!is_prereq && !*p)) {
120 if (report_path)
121 error(_("unrecognized header: %s%s (%d)"),
122 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
123 status = -1;
124 break;
125 } else {
126 struct object_id *dup = oiddup(&oid);
127 if (is_prereq)
128 string_list_append(&header->prerequisites, "")->util = dup;
129 else
130 string_list_append(&header->references, p + 1)->util = dup;
131 }
132 }
133
134 abort:
135 if (status) {
136 close(fd);
137 fd = -1;
138 }
139 strbuf_release(&buf);
140 return fd;
141 }
142
143 int read_bundle_header(const char *path, struct bundle_header *header)
144 {
145 int fd = open(path, O_RDONLY);
146
147 if (fd < 0)
148 return error(_("could not open '%s'"), path);
149 return read_bundle_header_fd(fd, header, path);
150 }
151
152 int is_bundle(const char *path, int quiet)
153 {
154 struct bundle_header header = BUNDLE_HEADER_INIT;
155 int fd = open(path, O_RDONLY);
156
157 if (fd < 0)
158 return 0;
159 fd = read_bundle_header_fd(fd, &header, quiet ? NULL : path);
160 if (fd >= 0)
161 close(fd);
162 bundle_header_release(&header);
163 return (fd >= 0);
164 }
165
166 static int list_refs(struct string_list *r, int argc, const char **argv)
167 {
168 int i;
169
170 for (i = 0; i < r->nr; i++) {
171 struct object_id *oid;
172 const char *name;
173
174 if (argc > 1) {
175 int j;
176 for (j = 1; j < argc; j++)
177 if (!strcmp(r->items[i].string, argv[j]))
178 break;
179 if (j == argc)
180 continue;
181 }
182
183 oid = r->items[i].util;
184 name = r->items[i].string;
185 printf("%s %s\n", oid_to_hex(oid), name);
186 }
187 return 0;
188 }
189
190 /* Remember to update object flag allocation in object.h */
191 #define PREREQ_MARK (1u<<16)
192
193 struct string_list_iterator {
194 struct string_list *list;
195 size_t cur;
196 };
197
198 static const struct object_id *iterate_ref_map(void *cb_data)
199 {
200 struct string_list_iterator *iter = cb_data;
201
202 if (iter->cur >= iter->list->nr)
203 return NULL;
204
205 return iter->list->items[iter->cur++].util;
206 }
207
208 int verify_bundle(struct repository *r,
209 struct bundle_header *header,
210 enum verify_bundle_flags flags)
211 {
212 /*
213 * Do fast check, then if any prereqs are missing then go line by line
214 * to be verbose about the errors
215 */
216 struct string_list *p = &header->prerequisites;
217 int i, ret = 0;
218 const char *message = _("Repository lacks these prerequisite commits:");
219 struct string_list_iterator iter = {
220 .list = p,
221 };
222 struct check_connected_options opts = {
223 .quiet = 1,
224 };
225
226 if (!r || !r->objects || !r->objects->odb)
227 return error(_("need a repository to verify a bundle"));
228
229 for (i = 0; i < p->nr; i++) {
230 struct string_list_item *e = p->items + i;
231 const char *name = e->string;
232 struct object_id *oid = e->util;
233 struct object *o = parse_object(r, oid);
234 if (o)
235 continue;
236 ret++;
237 if (flags & VERIFY_BUNDLE_QUIET)
238 continue;
239 if (ret == 1)
240 error("%s", message);
241 error("%s %s", oid_to_hex(oid), name);
242 }
243 if (ret)
244 goto cleanup;
245
246 if ((ret = check_connected(iterate_ref_map, &iter, &opts)))
247 error(_("some prerequisite commits exist in the object store, "
248 "but are not connected to the repository's history"));
249
250 /* TODO: preserve this verbose language. */
251 if (flags & VERIFY_BUNDLE_VERBOSE) {
252 struct string_list *r;
253
254 r = &header->references;
255 printf_ln(Q_("The bundle contains this ref:",
256 "The bundle contains these %"PRIuMAX" refs:",
257 r->nr),
258 (uintmax_t)r->nr);
259 list_refs(r, 0, NULL);
260
261 r = &header->prerequisites;
262 if (!r->nr) {
263 printf_ln(_("The bundle records a complete history."));
264 } else {
265 printf_ln(Q_("The bundle requires this ref:",
266 "The bundle requires these %"PRIuMAX" refs:",
267 r->nr),
268 (uintmax_t)r->nr);
269 list_refs(r, 0, NULL);
270 }
271
272 printf_ln("The bundle uses this hash algorithm: %s",
273 header->hash_algo->name);
274 if (header->filter.choice)
275 printf_ln("The bundle uses this filter: %s",
276 list_objects_filter_spec(&header->filter));
277 }
278 cleanup:
279 return ret;
280 }
281
282 int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
283 {
284 return list_refs(&header->references, argc, argv);
285 }
286
287 static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
288 {
289 unsigned long size;
290 enum object_type type;
291 char *buf = NULL, *line, *lineend;
292 timestamp_t date;
293 int result = 1;
294
295 if (revs->max_age == -1 && revs->min_age == -1)
296 goto out;
297
298 buf = read_object_file(&tag->oid, &type, &size);
299 if (!buf)
300 goto out;
301 line = memmem(buf, size, "\ntagger ", 8);
302 if (!line++)
303 goto out;
304 lineend = memchr(line, '\n', buf + size - line);
305 line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
306 if (!line++)
307 goto out;
308 date = parse_timestamp(line, NULL, 10);
309 result = (revs->max_age == -1 || revs->max_age < date) &&
310 (revs->min_age == -1 || revs->min_age > date);
311 out:
312 free(buf);
313 return result;
314 }
315
316
317 /* Write the pack data to bundle_fd */
318 static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options)
319 {
320 struct child_process pack_objects = CHILD_PROCESS_INIT;
321 int i;
322
323 strvec_pushl(&pack_objects.args,
324 "pack-objects",
325 "--stdout", "--thin", "--delta-base-offset",
326 NULL);
327 strvec_pushv(&pack_objects.args, pack_options->v);
328 if (revs->filter.choice)
329 strvec_pushf(&pack_objects.args, "--filter=%s",
330 list_objects_filter_spec(&revs->filter));
331 pack_objects.in = -1;
332 pack_objects.out = bundle_fd;
333 pack_objects.git_cmd = 1;
334
335 /*
336 * start_command() will close our descriptor if it's >1. Duplicate it
337 * to avoid surprising the caller.
338 */
339 if (pack_objects.out > 1) {
340 pack_objects.out = dup(pack_objects.out);
341 if (pack_objects.out < 0) {
342 error_errno(_("unable to dup bundle descriptor"));
343 child_process_clear(&pack_objects);
344 return -1;
345 }
346 }
347
348 if (start_command(&pack_objects))
349 return error(_("Could not spawn pack-objects"));
350
351 for (i = 0; i < revs->pending.nr; i++) {
352 struct object *object = revs->pending.objects[i].item;
353 if (object->flags & UNINTERESTING)
354 write_or_die(pack_objects.in, "^", 1);
355 write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
356 write_or_die(pack_objects.in, "\n", 1);
357 }
358 close(pack_objects.in);
359 if (finish_command(&pack_objects))
360 return error(_("pack-objects died"));
361 return 0;
362 }
363
364 /*
365 * Write out bundle refs based on the tips already
366 * parsed into revs.pending. As a side effect, may
367 * manipulate revs.pending to include additional
368 * necessary objects (like tags).
369 *
370 * Returns the number of refs written, or negative
371 * on error.
372 */
373 static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
374 {
375 int i;
376 int ref_count = 0;
377
378 for (i = 0; i < revs->pending.nr; i++) {
379 struct object_array_entry *e = revs->pending.objects + i;
380 struct object_id oid;
381 char *ref;
382 const char *display_ref;
383 int flag;
384
385 if (e->item->flags & UNINTERESTING)
386 continue;
387 if (dwim_ref(e->name, strlen(e->name), &oid, &ref, 0) != 1)
388 goto skip_write_ref;
389 if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag))
390 flag = 0;
391 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
392
393 if (e->item->type == OBJ_TAG &&
394 !is_tag_in_date_range(e->item, revs)) {
395 e->item->flags |= UNINTERESTING;
396 goto skip_write_ref;
397 }
398
399 /*
400 * Make sure the refs we wrote out is correct; --max-count and
401 * other limiting options could have prevented all the tips
402 * from getting output.
403 *
404 * Non commit objects such as tags and blobs do not have
405 * this issue as they are not affected by those extra
406 * constraints.
407 */
408 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
409 warning(_("ref '%s' is excluded by the rev-list options"),
410 e->name);
411 goto skip_write_ref;
412 }
413 /*
414 * If you run "git bundle create bndl v1.0..v2.0", the
415 * name of the positive ref is "v2.0" but that is the
416 * commit that is referenced by the tag, and not the tag
417 * itself.
418 */
419 if (!oideq(&oid, &e->item->oid)) {
420 /*
421 * Is this the positive end of a range expressed
422 * in terms of a tag (e.g. v2.0 from the range
423 * "v1.0..v2.0")?
424 */
425 struct commit *one = lookup_commit_reference(revs->repo, &oid);
426 struct object *obj;
427
428 if (e->item == &(one->object)) {
429 /*
430 * Need to include e->name as an
431 * independent ref to the pack-objects
432 * input, so that the tag is included
433 * in the output; otherwise we would
434 * end up triggering "empty bundle"
435 * error.
436 */
437 obj = parse_object_or_die(&oid, e->name);
438 obj->flags |= SHOWN;
439 add_pending_object(revs, obj, e->name);
440 }
441 goto skip_write_ref;
442 }
443
444 ref_count++;
445 write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
446 write_or_die(bundle_fd, " ", 1);
447 write_or_die(bundle_fd, display_ref, strlen(display_ref));
448 write_or_die(bundle_fd, "\n", 1);
449 skip_write_ref:
450 free(ref);
451 }
452
453 /* end header */
454 write_or_die(bundle_fd, "\n", 1);
455 return ref_count;
456 }
457
458 struct bundle_prerequisites_info {
459 struct object_array *pending;
460 int fd;
461 };
462
463 static void write_bundle_prerequisites(struct commit *commit, void *data)
464 {
465 struct bundle_prerequisites_info *bpi = data;
466 struct object *object;
467 struct pretty_print_context ctx = { 0 };
468 struct strbuf buf = STRBUF_INIT;
469
470 if (!(commit->object.flags & BOUNDARY))
471 return;
472 strbuf_addf(&buf, "-%s ", oid_to_hex(&commit->object.oid));
473 write_or_die(bpi->fd, buf.buf, buf.len);
474
475 ctx.fmt = CMIT_FMT_ONELINE;
476 ctx.output_encoding = get_log_output_encoding();
477 strbuf_reset(&buf);
478 pretty_print_commit(&ctx, commit, &buf);
479 strbuf_trim(&buf);
480
481 object = (struct object *)commit;
482 object->flags |= UNINTERESTING;
483 add_object_array_with_path(object, buf.buf, bpi->pending, S_IFINVALID,
484 NULL);
485 strbuf_addch(&buf, '\n');
486 write_or_die(bpi->fd, buf.buf, buf.len);
487 strbuf_release(&buf);
488 }
489
490 int create_bundle(struct repository *r, const char *path,
491 int argc, const char **argv, struct strvec *pack_options, int version)
492 {
493 struct lock_file lock = LOCK_INIT;
494 int bundle_fd = -1;
495 int bundle_to_stdout;
496 int ref_count = 0;
497 struct rev_info revs, revs_copy;
498 int min_version = 2;
499 struct bundle_prerequisites_info bpi;
500 int i;
501
502 /* init revs to list objects for pack-objects later */
503 save_commit_buffer = 0;
504 repo_init_revisions(r, &revs, NULL);
505
506 /*
507 * Pre-initialize the '--objects' flag so we can parse a
508 * --filter option successfully.
509 */
510 revs.tree_objects = revs.blob_objects = 1;
511
512 argc = setup_revisions(argc, argv, &revs, NULL);
513
514 /*
515 * Reasons to require version 3:
516 *
517 * 1. @object-format is required because our hash algorithm is not
518 * SHA1.
519 * 2. @filter is required because we parsed an object filter.
520 */
521 if (the_hash_algo != &hash_algos[GIT_HASH_SHA1] || revs.filter.choice)
522 min_version = 3;
523
524 if (argc > 1) {
525 error(_("unrecognized argument: %s"), argv[1]);
526 goto err;
527 }
528
529 bundle_to_stdout = !strcmp(path, "-");
530 if (bundle_to_stdout)
531 bundle_fd = 1;
532 else
533 bundle_fd = hold_lock_file_for_update(&lock, path,
534 LOCK_DIE_ON_ERROR);
535
536 if (version == -1)
537 version = min_version;
538
539 if (version < 2 || version > 3) {
540 die(_("unsupported bundle version %d"), version);
541 } else if (version < min_version) {
542 die(_("cannot write bundle version %d with algorithm %s"), version, the_hash_algo->name);
543 } else if (version == 2) {
544 write_or_die(bundle_fd, v2_bundle_signature, strlen(v2_bundle_signature));
545 } else {
546 const char *capability = "@object-format=";
547 write_or_die(bundle_fd, v3_bundle_signature, strlen(v3_bundle_signature));
548 write_or_die(bundle_fd, capability, strlen(capability));
549 write_or_die(bundle_fd, the_hash_algo->name, strlen(the_hash_algo->name));
550 write_or_die(bundle_fd, "\n", 1);
551
552 if (revs.filter.choice) {
553 const char *value = expand_list_objects_filter_spec(&revs.filter);
554 capability = "@filter=";
555 write_or_die(bundle_fd, capability, strlen(capability));
556 write_or_die(bundle_fd, value, strlen(value));
557 write_or_die(bundle_fd, "\n", 1);
558 }
559 }
560
561 /* save revs.pending in revs_copy for later use */
562 memcpy(&revs_copy, &revs, sizeof(revs));
563 revs_copy.pending.nr = 0;
564 revs_copy.pending.alloc = 0;
565 revs_copy.pending.objects = NULL;
566 for (i = 0; i < revs.pending.nr; i++) {
567 struct object_array_entry *e = revs.pending.objects + i;
568 if (e)
569 add_object_array_with_path(e->item, e->name,
570 &revs_copy.pending,
571 e->mode, e->path);
572 }
573
574 /* write prerequisites */
575 revs.boundary = 1;
576 if (prepare_revision_walk(&revs))
577 die("revision walk setup failed");
578 bpi.fd = bundle_fd;
579 bpi.pending = &revs_copy.pending;
580
581 /*
582 * Remove any object walking here. We only care about commits and
583 * tags here. The revs_copy has the right instances of these values.
584 */
585 revs.blob_objects = revs.tree_objects = 0;
586 traverse_commit_list(&revs, write_bundle_prerequisites, NULL, &bpi);
587 object_array_remove_duplicates(&revs_copy.pending);
588
589 /* write bundle refs */
590 ref_count = write_bundle_refs(bundle_fd, &revs_copy);
591 if (!ref_count)
592 die(_("Refusing to create empty bundle."));
593 else if (ref_count < 0)
594 goto err;
595
596 /* write pack */
597 if (write_pack_data(bundle_fd, &revs_copy, pack_options))
598 goto err;
599
600 if (!bundle_to_stdout) {
601 if (commit_lock_file(&lock))
602 die_errno(_("cannot create '%s'"), path);
603 }
604 return 0;
605 err:
606 rollback_lock_file(&lock);
607 return -1;
608 }
609
610 int unbundle(struct repository *r, struct bundle_header *header,
611 int bundle_fd, struct strvec *extra_index_pack_args,
612 enum verify_bundle_flags flags)
613 {
614 struct child_process ip = CHILD_PROCESS_INIT;
615
616 if (verify_bundle(r, header, flags))
617 return -1;
618
619 strvec_pushl(&ip.args, "index-pack", "--fix-thin", "--stdin", NULL);
620
621 /* If there is a filter, then we need to create the promisor pack. */
622 if (header->filter.choice)
623 strvec_push(&ip.args, "--promisor=from-bundle");
624
625 if (extra_index_pack_args) {
626 strvec_pushv(&ip.args, extra_index_pack_args->v);
627 strvec_clear(extra_index_pack_args);
628 }
629
630 ip.in = bundle_fd;
631 ip.no_stdout = 1;
632 ip.git_cmd = 1;
633 if (run_command(&ip))
634 return error(_("index-pack died"));
635 return 0;
636 }