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