]> git.ipfire.org Git - thirdparty/git.git/blame - bundle.c
bundle API: start writing API documentation
[thirdparty/git.git] / bundle.c
CommitLineData
30415d50 1#include "cache.h"
697cc8ef 2#include "lockfile.h"
30415d50 3#include "bundle.h"
cbd53a21 4#include "object-store.h"
109cd76d 5#include "repository.h"
30415d50
JS
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"
fa303836 12#include "refs.h"
dbbcd44f 13#include "strvec.h"
30415d50 14
c5aecfc8 15
16static const char v2_bundle_signature[] = "# v2 git bundle\n";
17static const char v3_bundle_signature[] = "# v3 git bundle\n";
18static struct {
19 int version;
20 const char *signature;
21} bundle_sigs[] = {
22 { 2, v2_bundle_signature },
23 { 3, v3_bundle_signature },
24};
30415d50 25
10b635b7 26void bundle_header_init(struct bundle_header *header)
30415d50 27{
10b635b7
ÆAB
28 struct bundle_header blank = BUNDLE_HEADER_INIT;
29 memcpy(header, &blank, sizeof(*header));
30}
31
32void bundle_header_release(struct bundle_header *header)
33{
34 string_list_clear(&header->prerequisites, 1);
35 string_list_clear(&header->references, 1);
30415d50
JS
36}
37
c5aecfc8 38static int parse_capability(struct bundle_header *header, const char *capability)
6161ce7b 39{
c5aecfc8 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;
47 }
48 return error(_("unknown capability '%s'"), capability);
49}
6161ce7b 50
c5aecfc8 51static int parse_bundle_signature(struct bundle_header *header, const char *line)
52{
53 int i;
54
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;
59 }
60 }
61 return -1;
6161ce7b 62}
63
2727b71f
JH
64static int parse_bundle_header(int fd, struct bundle_header *header,
65 const char *report_path)
f3fa1838 66{
e9ee84cf 67 struct strbuf buf = STRBUF_INIT;
e9ee84cf 68 int status = 0;
30415d50 69
e9ee84cf 70 /* The bundle header begins with the signature */
5e8617f5 71 if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
c5aecfc8 72 parse_bundle_signature(header, buf.buf)) {
2727b71f 73 if (report_path)
c5aecfc8 74 error(_("'%s' does not look like a v2 or v3 bundle file"),
2727b71f 75 report_path);
e9ee84cf
JH
76 status = -1;
77 goto abort;
30415d50 78 }
e9ee84cf 79
c5aecfc8 80 header->hash_algo = the_hash_algo;
81
e9ee84cf 82 /* The bundle header ends with an empty line */
5e8617f5 83 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
e9ee84cf 84 buf.len && buf.buf[0] != '\n') {
b8607f35 85 struct object_id oid;
e9ee84cf 86 int is_prereq = 0;
b8607f35 87 const char *p;
e9ee84cf 88
e9ee84cf 89 strbuf_rtrim(&buf);
30415d50 90
c5aecfc8 91 if (header->version == 3 && *buf.buf == '@') {
92 if (parse_capability(header, buf.buf + 1)) {
6161ce7b 93 status = -1;
94 break;
95 }
c5aecfc8 96 continue;
97 }
98
99 if (*buf.buf == '-') {
100 is_prereq = 1;
101 strbuf_remove(&buf, 0, 1);
6161ce7b 102 }
103
e9ee84cf
JH
104 /*
105 * Tip lines have object name, SP, and refname.
106 * Prerequisites have object name that is optionally
107 * followed by SP and subject line.
108 */
6161ce7b 109 if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) ||
b8607f35 110 (*p && !isspace(*p)) ||
111 (!is_prereq && !*p)) {
2727b71f 112 if (report_path)
8a1e7eac 113 error(_("unrecognized header: %s%s (%d)"),
2727b71f 114 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
e9ee84cf
JH
115 status = -1;
116 break;
117 } else {
10b635b7 118 struct object_id *dup = oiddup(&oid);
e9ee84cf 119 if (is_prereq)
10b635b7 120 string_list_append(&header->prerequisites, "")->util = dup;
e9ee84cf 121 else
10b635b7 122 string_list_append(&header->references, p + 1)->util = dup;
30415d50 123 }
30415d50 124 }
e9ee84cf
JH
125
126 abort:
127 if (status) {
128 close(fd);
129 fd = -1;
30415d50 130 }
e9ee84cf 131 strbuf_release(&buf);
30415d50
JS
132 return fd;
133}
134
2727b71f
JH
135int read_bundle_header(const char *path, struct bundle_header *header)
136{
137 int fd = open(path, O_RDONLY);
138
30415d50 139 if (fd < 0)
8a1e7eac 140 return error(_("could not open '%s'"), path);
2727b71f
JH
141 return parse_bundle_header(fd, header, path);
142}
143
144int is_bundle(const char *path, int quiet)
145{
10b635b7 146 struct bundle_header header = BUNDLE_HEADER_INIT;
2727b71f
JH
147 int fd = open(path, O_RDONLY);
148
149 if (fd < 0)
150 return 0;
2727b71f
JH
151 fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
152 if (fd >= 0)
153 close(fd);
10b635b7 154 bundle_header_release(&header);
2727b71f 155 return (fd >= 0);
30415d50
JS
156}
157
10b635b7 158static int list_refs(struct string_list *r, int argc, const char **argv)
30415d50
JS
159{
160 int i;
161
162 for (i = 0; i < r->nr; i++) {
15e7c7dc
ÆAB
163 struct object_id *oid;
164 const char *name;
165
30415d50
JS
166 if (argc > 1) {
167 int j;
168 for (j = 1; j < argc; j++)
10b635b7 169 if (!strcmp(r->items[i].string, argv[j]))
30415d50
JS
170 break;
171 if (j == argc)
172 continue;
173 }
15e7c7dc 174
10b635b7
ÆAB
175 oid = r->items[i].util;
176 name = r->items[i].string;
15e7c7dc 177 printf("%s %s\n", oid_to_hex(oid), name);
30415d50
JS
178 }
179 return 0;
180}
181
208acbfb 182/* Remember to update object flag allocation in object.h */
30415d50
JS
183#define PREREQ_MARK (1u<<16)
184
74ae4b63
NTND
185int verify_bundle(struct repository *r,
186 struct bundle_header *header,
187 int verbose)
30415d50
JS
188{
189 /*
190 * Do fast check, then if any prereqs are missing then go line by line
191 * to be verbose about the errors
192 */
10b635b7 193 struct string_list *p = &header->prerequisites;
30415d50 194 struct rev_info revs;
a80aad7b 195 const char *argv[] = {NULL, "--all", NULL};
30415d50
JS
196 struct commit *commit;
197 int i, ret = 0, req_nr;
8a1e7eac 198 const char *message = _("Repository lacks these prerequisite commits:");
30415d50 199
3bbbe467
JS
200 if (!r || !r->objects || !r->objects->odb)
201 return error(_("need a repository to verify a bundle"));
202
74ae4b63 203 repo_init_revisions(r, &revs, NULL);
30415d50 204 for (i = 0; i < p->nr; i++) {
10b635b7
ÆAB
205 struct string_list_item *e = p->items + i;
206 const char *name = e->string;
207 struct object_id *oid = e->util;
15e7c7dc 208 struct object *o = parse_object(r, oid);
30415d50
JS
209 if (o) {
210 o->flags |= PREREQ_MARK;
15e7c7dc 211 add_pending_object(&revs, o, name);
30415d50
JS
212 continue;
213 }
214 if (++ret == 1)
9db56f71 215 error("%s", message);
15e7c7dc 216 error("%s %s", oid_to_hex(oid), name);
30415d50
JS
217 }
218 if (revs.pending.nr != p->nr)
219 return ret;
220 req_nr = revs.pending.nr;
221 setup_revisions(2, argv, &revs, NULL);
222
3d51e1b5 223 if (prepare_revision_walk(&revs))
8a1e7eac 224 die(_("revision walk setup failed"));
30415d50
JS
225
226 i = req_nr;
227 while (i && (commit = get_revision(&revs)))
228 if (commit->object.flags & PREREQ_MARK)
229 i--;
230
63647391 231 for (i = 0; i < p->nr; i++) {
10b635b7
ÆAB
232 struct string_list_item *e = p->items + i;
233 const char *name = e->string;
234 const struct object_id *oid = e->util;
15e7c7dc 235 struct object *o = parse_object(r, oid);
63647391
RS
236 assert(o); /* otherwise we'd have returned early */
237 if (o->flags & SHOWN)
238 continue;
239 if (++ret == 1)
240 error("%s", message);
15e7c7dc 241 error("%s %s", oid_to_hex(oid), name);
63647391 242 }
30415d50 243
b2ccdf7f 244 /* Clean up objects used, as they will be reused. */
63647391 245 for (i = 0; i < p->nr; i++) {
10b635b7
ÆAB
246 struct string_list_item *e = p->items + i;
247 struct object_id *oid = e->util;
15e7c7dc 248 commit = lookup_commit_reference_gently(r, oid, 1);
63647391
RS
249 if (commit)
250 clear_commit_marks(commit, ALL_REV_FLAGS);
251 }
30415d50
JS
252
253 if (verbose) {
10b635b7 254 struct string_list *r;
30415d50
JS
255
256 r = &header->references;
a02ffe0e
LF
257 printf_ln(Q_("The bundle contains this ref:",
258 "The bundle contains these %d refs:",
8a1e7eac
NTND
259 r->nr),
260 r->nr);
30415d50 261 list_refs(r, 0, NULL);
71ba6b10 262 r = &header->prerequisites;
8c3710fd
JH
263 if (!r->nr) {
264 printf_ln(_("The bundle records a complete history."));
265 } else {
a02ffe0e
LF
266 printf_ln(Q_("The bundle requires this ref:",
267 "The bundle requires these %d refs:",
8c3710fd
JH
268 r->nr),
269 r->nr);
270 list_refs(r, 0, NULL);
271 }
30415d50
JS
272 }
273 return ret;
274}
275
276int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
277{
278 return list_refs(&header->references, argc, argv);
279}
280
c9a42c4a
JS
281static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
282{
283 unsigned long size;
284 enum object_type type;
64045940 285 char *buf = NULL, *line, *lineend;
dddbad72 286 timestamp_t date;
64045940 287 int result = 1;
c9a42c4a
JS
288
289 if (revs->max_age == -1 && revs->min_age == -1)
64045940 290 goto out;
c9a42c4a 291
b4f5aca4 292 buf = read_object_file(&tag->oid, &type, &size);
c9a42c4a 293 if (!buf)
64045940 294 goto out;
c9a42c4a
JS
295 line = memmem(buf, size, "\ntagger ", 8);
296 if (!line++)
64045940 297 goto out;
2c8544ab
LF
298 lineend = memchr(line, '\n', buf + size - line);
299 line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
c9a42c4a 300 if (!line++)
64045940 301 goto out;
1aeb7e75 302 date = parse_timestamp(line, NULL, 10);
64045940 303 result = (revs->max_age == -1 || revs->max_age < date) &&
c9a42c4a 304 (revs->min_age == -1 || revs->min_age > date);
64045940
RS
305out:
306 free(buf);
307 return result;
c9a42c4a
JS
308}
309
e54c347c 310
2c8ee1f5 311/* Write the pack data to bundle_fd */
ef8d7ac4 312static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options)
5e626b91
JH
313{
314 struct child_process pack_objects = CHILD_PROCESS_INIT;
315 int i;
316
ef8d7ac4 317 strvec_pushl(&pack_objects.args,
f6d8942b
JK
318 "pack-objects",
319 "--stdout", "--thin", "--delta-base-offset",
320 NULL);
d70a9eb6 321 strvec_pushv(&pack_objects.args, pack_options->v);
5e626b91
JH
322 pack_objects.in = -1;
323 pack_objects.out = bundle_fd;
324 pack_objects.git_cmd = 1;
2c8ee1f5
JK
325
326 /*
327 * start_command() will close our descriptor if it's >1. Duplicate it
328 * to avoid surprising the caller.
329 */
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;
336 }
337 }
338
5e626b91
JH
339 if (start_command(&pack_objects))
340 return error(_("Could not spawn pack-objects"));
341
5e626b91
JH
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);
703d2d41 346 write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
5e626b91
JH
347 write_or_die(pack_objects.in, "\n", 1);
348 }
349 close(pack_objects.in);
350 if (finish_command(&pack_objects))
351 return error(_("pack-objects died"));
352 return 0;
353}
354
d9362ef9
JK
355/*
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).
360 *
361 * Returns the number of refs written, or negative
362 * on error.
363 */
364static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
e8eb2512 365{
d9362ef9
JK
366 int i;
367 int ref_count = 0;
b2a6d1c6 368
d9362ef9
JK
369 for (i = 0; i < revs->pending.nr; i++) {
370 struct object_array_entry *e = revs->pending.objects + i;
f2fd0760 371 struct object_id oid;
30415d50 372 char *ref;
fa303836
JH
373 const char *display_ref;
374 int flag;
30415d50
JS
375
376 if (e->item->flags & UNINTERESTING)
377 continue;
f24c30e0 378 if (dwim_ref(e->name, strlen(e->name), &oid, &ref, 0) != 1)
c8a571d8 379 goto skip_write_ref;
34c290a6 380 if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag))
fa303836
JH
381 flag = 0;
382 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
383
c9a42c4a 384 if (e->item->type == OBJ_TAG &&
d9362ef9 385 !is_tag_in_date_range(e->item, revs)) {
c9a42c4a 386 e->item->flags |= UNINTERESTING;
c8a571d8 387 goto skip_write_ref;
c9a42c4a
JS
388 }
389
30415d50
JS
390 /*
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.
394 *
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.
398 */
399 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
8a1e7eac 400 warning(_("ref '%s' is excluded by the rev-list options"),
30415d50 401 e->name);
c8a571d8 402 goto skip_write_ref;
30415d50
JS
403 }
404 /*
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.
409 */
9001dc2a 410 if (!oideq(&oid, &e->item->oid)) {
30415d50
JS
411 /*
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")?
415 */
74ae4b63 416 struct commit *one = lookup_commit_reference(revs->repo, &oid);
30415d50
JS
417 struct object *obj;
418
419 if (e->item == &(one->object)) {
420 /*
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.
427 */
c251c83d 428 obj = parse_object_or_die(&oid, e->name);
30415d50 429 obj->flags |= SHOWN;
d9362ef9 430 add_pending_object(revs, obj, e->name);
30415d50 431 }
c8a571d8 432 goto skip_write_ref;
30415d50
JS
433 }
434
435 ref_count++;
703d2d41 436 write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
30415d50 437 write_or_die(bundle_fd, " ", 1);
fa303836 438 write_or_die(bundle_fd, display_ref, strlen(display_ref));
30415d50 439 write_or_die(bundle_fd, "\n", 1);
c8a571d8 440 skip_write_ref:
30415d50
JS
441 free(ref);
442 }
30415d50
JS
443
444 /* end header */
445 write_or_die(bundle_fd, "\n", 1);
d9362ef9
JK
446 return ref_count;
447}
448
5bb0fd2c
JX
449struct bundle_prerequisites_info {
450 struct object_array *pending;
451 int fd;
452};
453
454static void write_bundle_prerequisites(struct commit *commit, void *data)
455{
456 struct bundle_prerequisites_info *bpi = data;
457 struct object *object;
458 struct pretty_print_context ctx = { 0 };
459 struct strbuf buf = STRBUF_INIT;
460
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);
465
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);
471
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);
479}
480
fcb133e9 481int create_bundle(struct repository *r, const char *path,
e0ad9574 482 int argc, const char **argv, struct strvec *pack_options, int version)
d9362ef9 483{
b2275868 484 struct lock_file lock = LOCK_INIT;
d9362ef9
JK
485 int bundle_fd = -1;
486 int bundle_to_stdout;
487 int ref_count = 0;
5bb0fd2c 488 struct rev_info revs, revs_copy;
c5aecfc8 489 int min_version = the_hash_algo == &hash_algos[GIT_HASH_SHA1] ? 2 : 3;
5bb0fd2c
JX
490 struct bundle_prerequisites_info bpi;
491 int i;
d9362ef9
JK
492
493 bundle_to_stdout = !strcmp(path, "-");
494 if (bundle_to_stdout)
495 bundle_fd = 1;
2c8ee1f5 496 else
d9362ef9
JK
497 bundle_fd = hold_lock_file_for_update(&lock, path,
498 LOCK_DIE_ON_ERROR);
499
c5aecfc8 500 if (version == -1)
501 version = min_version;
502
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);
515 }
d9362ef9
JK
516
517 /* init revs to list objects for pack-objects later */
518 save_commit_buffer = 0;
74ae4b63 519 repo_init_revisions(r, &revs, NULL);
d9362ef9 520
d9362ef9
JK
521 argc = setup_revisions(argc, argv, &revs, NULL);
522
f5ff5fb5
SB
523 if (argc > 1) {
524 error(_("unrecognized argument: %s"), argv[1]);
525 goto err;
526 }
d9362ef9 527
5bb0fd2c
JX
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);
539 }
d9362ef9 540
5bb0fd2c
JX
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);
549
550 /* write bundle refs */
551 ref_count = write_bundle_refs(bundle_fd, &revs_copy);
d9362ef9
JK
552 if (!ref_count)
553 die(_("Refusing to create empty bundle."));
554 else if (ref_count < 0)
f5ff5fb5 555 goto err;
30415d50
JS
556
557 /* write pack */
5bb0fd2c 558 if (write_pack_data(bundle_fd, &revs_copy, pack_options))
f5ff5fb5 559 goto err;
4ed7cd3a 560
0f5cdf65
CH
561 if (!bundle_to_stdout) {
562 if (commit_lock_file(&lock))
8a1e7eac 563 die_errno(_("cannot create '%s'"), path);
0f5cdf65 564 }
c20181e3 565 return 0;
f5ff5fb5 566err:
2c8ee1f5 567 rollback_lock_file(&lock);
f5ff5fb5 568 return -1;
30415d50
JS
569}
570
74ae4b63
NTND
571int unbundle(struct repository *r, struct bundle_header *header,
572 int bundle_fd, int flags)
30415d50
JS
573{
574 const char *argv_index_pack[] = {"index-pack",
be042aff 575 "--fix-thin", "--stdin", NULL, NULL};
d3180279 576 struct child_process ip = CHILD_PROCESS_INIT;
30415d50 577
be042aff
JH
578 if (flags & BUNDLE_VERBOSE)
579 argv_index_pack[3] = "-v";
580
74ae4b63 581 if (verify_bundle(r, header, 0))
30415d50 582 return -1;
30415d50
JS
583 ip.argv = argv_index_pack;
584 ip.in = bundle_fd;
585 ip.no_stdout = 1;
586 ip.git_cmd = 1;
587 if (run_command(&ip))
8a1e7eac 588 return error(_("index-pack died"));
30415d50
JS
589 return 0;
590}