]> git.ipfire.org Git - thirdparty/git.git/blame - bundle.c
Merge branch 'bc/sha-256-part-2'
[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"
1e2371ea 13#include "argv-array.h"
30415d50
JS
14
15static const char bundle_signature[] = "# v2 git bundle\n";
16
b8607f35 17static void add_to_ref_list(const struct object_id *oid, const char *name,
30415d50
JS
18 struct ref_list *list)
19{
5cbbe13a 20 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
b8607f35 21 oidcpy(&list->list[list->nr].oid, oid);
30415d50
JS
22 list->list[list->nr].name = xstrdup(name);
23 list->nr++;
24}
25
6161ce7b 26static const struct git_hash_algo *detect_hash_algo(struct strbuf *buf)
27{
28 size_t len = strcspn(buf->buf, " \n");
29 int algo;
30
31 algo = hash_algo_by_length(len / 2);
32 if (algo == GIT_HASH_UNKNOWN)
33 return NULL;
34 return &hash_algos[algo];
35}
36
2727b71f
JH
37static int parse_bundle_header(int fd, struct bundle_header *header,
38 const char *report_path)
f3fa1838 39{
e9ee84cf 40 struct strbuf buf = STRBUF_INIT;
e9ee84cf 41 int status = 0;
30415d50 42
e9ee84cf 43 /* The bundle header begins with the signature */
5e8617f5 44 if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
e9ee84cf 45 strcmp(buf.buf, bundle_signature)) {
2727b71f 46 if (report_path)
8a1e7eac 47 error(_("'%s' does not look like a v2 bundle file"),
2727b71f 48 report_path);
e9ee84cf
JH
49 status = -1;
50 goto abort;
30415d50 51 }
e9ee84cf
JH
52
53 /* The bundle header ends with an empty line */
5e8617f5 54 while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
e9ee84cf 55 buf.len && buf.buf[0] != '\n') {
b8607f35 56 struct object_id oid;
e9ee84cf 57 int is_prereq = 0;
b8607f35 58 const char *p;
e9ee84cf
JH
59
60 if (*buf.buf == '-') {
61 is_prereq = 1;
62 strbuf_remove(&buf, 0, 1);
30415d50 63 }
e9ee84cf 64 strbuf_rtrim(&buf);
30415d50 65
6161ce7b 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;
72 }
73 }
74
e9ee84cf
JH
75 /*
76 * Tip lines have object name, SP, and refname.
77 * Prerequisites have object name that is optionally
78 * followed by SP and subject line.
79 */
6161ce7b 80 if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) ||
b8607f35 81 (*p && !isspace(*p)) ||
82 (!is_prereq && !*p)) {
2727b71f 83 if (report_path)
8a1e7eac 84 error(_("unrecognized header: %s%s (%d)"),
2727b71f 85 (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
e9ee84cf
JH
86 status = -1;
87 break;
88 } else {
89 if (is_prereq)
b8607f35 90 add_to_ref_list(&oid, "", &header->prerequisites);
e9ee84cf 91 else
b8607f35 92 add_to_ref_list(&oid, p + 1, &header->references);
30415d50 93 }
30415d50 94 }
e9ee84cf
JH
95
96 abort:
97 if (status) {
98 close(fd);
99 fd = -1;
30415d50 100 }
e9ee84cf 101 strbuf_release(&buf);
30415d50
JS
102 return fd;
103}
104
2727b71f
JH
105int read_bundle_header(const char *path, struct bundle_header *header)
106{
107 int fd = open(path, O_RDONLY);
108
30415d50 109 if (fd < 0)
8a1e7eac 110 return error(_("could not open '%s'"), path);
2727b71f
JH
111 return parse_bundle_header(fd, header, path);
112}
113
114int is_bundle(const char *path, int quiet)
115{
116 struct bundle_header header;
117 int fd = open(path, O_RDONLY);
118
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);
30415d50
JS
126}
127
128static int list_refs(struct ref_list *r, int argc, const char **argv)
129{
130 int i;
131
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;
140 }
b8607f35 141 printf("%s %s\n", oid_to_hex(&r->list[i].oid),
30415d50
JS
142 r->list[i].name);
143 }
144 return 0;
145}
146
208acbfb 147/* Remember to update object flag allocation in object.h */
30415d50
JS
148#define PREREQ_MARK (1u<<16)
149
74ae4b63
NTND
150int verify_bundle(struct repository *r,
151 struct bundle_header *header,
152 int verbose)
30415d50
JS
153{
154 /*
155 * Do fast check, then if any prereqs are missing then go line by line
156 * to be verbose about the errors
157 */
158 struct ref_list *p = &header->prerequisites;
159 struct rev_info revs;
a80aad7b 160 const char *argv[] = {NULL, "--all", NULL};
30415d50
JS
161 struct commit *commit;
162 int i, ret = 0, req_nr;
8a1e7eac 163 const char *message = _("Repository lacks these prerequisite commits:");
30415d50 164
3bbbe467
JS
165 if (!r || !r->objects || !r->objects->odb)
166 return error(_("need a repository to verify a bundle"));
167
74ae4b63 168 repo_init_revisions(r, &revs, NULL);
30415d50
JS
169 for (i = 0; i < p->nr; i++) {
170 struct ref_list_entry *e = p->list + i;
74ae4b63 171 struct object *o = parse_object(r, &e->oid);
30415d50
JS
172 if (o) {
173 o->flags |= PREREQ_MARK;
174 add_pending_object(&revs, o, e->name);
175 continue;
176 }
177 if (++ret == 1)
9db56f71 178 error("%s", message);
b8607f35 179 error("%s %s", oid_to_hex(&e->oid), e->name);
30415d50
JS
180 }
181 if (revs.pending.nr != p->nr)
182 return ret;
183 req_nr = revs.pending.nr;
184 setup_revisions(2, argv, &revs, NULL);
185
3d51e1b5 186 if (prepare_revision_walk(&revs))
8a1e7eac 187 die(_("revision walk setup failed"));
30415d50
JS
188
189 i = req_nr;
190 while (i && (commit = get_revision(&revs)))
191 if (commit->object.flags & PREREQ_MARK)
192 i--;
193
63647391
RS
194 for (i = 0; i < p->nr; i++) {
195 struct ref_list_entry *e = p->list + i;
74ae4b63 196 struct object *o = parse_object(r, &e->oid);
63647391
RS
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);
203 }
30415d50 204
b2ccdf7f 205 /* Clean up objects used, as they will be reused. */
63647391
RS
206 for (i = 0; i < p->nr; i++) {
207 struct ref_list_entry *e = p->list + i;
74ae4b63 208 commit = lookup_commit_reference_gently(r, &e->oid, 1);
63647391
RS
209 if (commit)
210 clear_commit_marks(commit, ALL_REV_FLAGS);
211 }
30415d50
JS
212
213 if (verbose) {
214 struct ref_list *r;
215
216 r = &header->references;
a02ffe0e
LF
217 printf_ln(Q_("The bundle contains this ref:",
218 "The bundle contains these %d refs:",
8a1e7eac
NTND
219 r->nr),
220 r->nr);
30415d50 221 list_refs(r, 0, NULL);
71ba6b10 222 r = &header->prerequisites;
8c3710fd
JH
223 if (!r->nr) {
224 printf_ln(_("The bundle records a complete history."));
225 } else {
a02ffe0e
LF
226 printf_ln(Q_("The bundle requires this ref:",
227 "The bundle requires these %d refs:",
8c3710fd
JH
228 r->nr),
229 r->nr);
230 list_refs(r, 0, NULL);
231 }
30415d50
JS
232 }
233 return ret;
234}
235
236int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
237{
238 return list_refs(&header->references, argc, argv);
239}
240
c9a42c4a
JS
241static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
242{
243 unsigned long size;
244 enum object_type type;
64045940 245 char *buf = NULL, *line, *lineend;
dddbad72 246 timestamp_t date;
64045940 247 int result = 1;
c9a42c4a
JS
248
249 if (revs->max_age == -1 && revs->min_age == -1)
64045940 250 goto out;
c9a42c4a 251
b4f5aca4 252 buf = read_object_file(&tag->oid, &type, &size);
c9a42c4a 253 if (!buf)
64045940 254 goto out;
c9a42c4a
JS
255 line = memmem(buf, size, "\ntagger ", 8);
256 if (!line++)
64045940 257 goto out;
2c8544ab
LF
258 lineend = memchr(line, '\n', buf + size - line);
259 line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
c9a42c4a 260 if (!line++)
64045940 261 goto out;
1aeb7e75 262 date = parse_timestamp(line, NULL, 10);
64045940 263 result = (revs->max_age == -1 || revs->max_age < date) &&
c9a42c4a 264 (revs->min_age == -1 || revs->min_age > date);
64045940
RS
265out:
266 free(buf);
267 return result;
c9a42c4a
JS
268}
269
e54c347c 270
2c8ee1f5 271/* Write the pack data to bundle_fd */
79862b6b 272static int write_pack_data(int bundle_fd, struct rev_info *revs, struct argv_array *pack_options)
5e626b91
JH
273{
274 struct child_process pack_objects = CHILD_PROCESS_INIT;
275 int i;
276
277 argv_array_pushl(&pack_objects.args,
79862b6b 278 "pack-objects",
5e626b91
JH
279 "--stdout", "--thin", "--delta-base-offset",
280 NULL);
79862b6b 281 argv_array_pushv(&pack_objects.args, pack_options->argv);
5e626b91
JH
282 pack_objects.in = -1;
283 pack_objects.out = bundle_fd;
284 pack_objects.git_cmd = 1;
2c8ee1f5
JK
285
286 /*
287 * start_command() will close our descriptor if it's >1. Duplicate it
288 * to avoid surprising the caller.
289 */
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;
296 }
297 }
298
5e626b91
JH
299 if (start_command(&pack_objects))
300 return error(_("Could not spawn pack-objects"));
301
5e626b91
JH
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);
703d2d41 306 write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
5e626b91
JH
307 write_or_die(pack_objects.in, "\n", 1);
308 }
309 close(pack_objects.in);
310 if (finish_command(&pack_objects))
311 return error(_("pack-objects died"));
312 return 0;
313}
314
e8eb2512
JH
315static int compute_and_write_prerequisites(int bundle_fd,
316 struct rev_info *revs,
317 int argc, const char **argv)
30415d50 318{
d3180279 319 struct child_process rls = CHILD_PROCESS_INIT;
e8eb2512 320 struct strbuf buf = STRBUF_INIT;
30415d50 321 FILE *rls_fout;
e8eb2512 322 int i;
30415d50 323
92859f3a 324 argv_array_pushl(&rls.args,
1e2371ea
JH
325 "rev-list", "--boundary", "--pretty=oneline",
326 NULL);
327 for (i = 1; i < argc; i++)
92859f3a 328 argv_array_push(&rls.args, argv[i]);
30415d50
JS
329 rls.out = -1;
330 rls.git_cmd = 1;
331 if (start_command(&rls))
332 return -1;
41698375 333 rls_fout = xfdopen(rls.out, "r");
bc2fed49 334 while (strbuf_getwholeline(&buf, rls_fout, '\n') != EOF) {
b8607f35 335 struct object_id oid;
bc2fed49
TR
336 if (buf.len > 0 && buf.buf[0] == '-') {
337 write_or_die(bundle_fd, buf.buf, buf.len);
b8607f35 338 if (!get_oid_hex(buf.buf + 1, &oid)) {
c251c83d 339 struct object *object = parse_object_or_die(&oid,
340 buf.buf);
30415d50 341 object->flags |= UNINTERESTING;
e8eb2512 342 add_pending_object(revs, object, buf.buf);
30415d50 343 }
b8607f35 344 } else if (!get_oid_hex(buf.buf, &oid)) {
c251c83d 345 struct object *object = parse_object_or_die(&oid,
346 buf.buf);
30415d50
JS
347 object->flags |= SHOWN;
348 }
349 }
bc2fed49 350 strbuf_release(&buf);
30415d50
JS
351 fclose(rls_fout);
352 if (finish_command(&rls))
8a1e7eac 353 return error(_("rev-list died"));
e8eb2512
JH
354 return 0;
355}
356
d9362ef9
JK
357/*
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).
362 *
363 * Returns the number of refs written, or negative
364 * on error.
365 */
366static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
e8eb2512 367{
d9362ef9
JK
368 int i;
369 int ref_count = 0;
b2a6d1c6 370
d9362ef9
JK
371 for (i = 0; i < revs->pending.nr; i++) {
372 struct object_array_entry *e = revs->pending.objects + i;
f2fd0760 373 struct object_id oid;
30415d50 374 char *ref;
fa303836
JH
375 const char *display_ref;
376 int flag;
30415d50
JS
377
378 if (e->item->flags & UNINTERESTING)
379 continue;
cca5fa64 380 if (dwim_ref(e->name, strlen(e->name), &oid, &ref) != 1)
c8a571d8 381 goto skip_write_ref;
34c290a6 382 if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag))
fa303836
JH
383 flag = 0;
384 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
385
c9a42c4a 386 if (e->item->type == OBJ_TAG &&
d9362ef9 387 !is_tag_in_date_range(e->item, revs)) {
c9a42c4a 388 e->item->flags |= UNINTERESTING;
c8a571d8 389 goto skip_write_ref;
c9a42c4a
JS
390 }
391
30415d50
JS
392 /*
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.
396 *
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.
400 */
401 if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
8a1e7eac 402 warning(_("ref '%s' is excluded by the rev-list options"),
30415d50 403 e->name);
c8a571d8 404 goto skip_write_ref;
30415d50
JS
405 }
406 /*
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.
411 */
9001dc2a 412 if (!oideq(&oid, &e->item->oid)) {
30415d50
JS
413 /*
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")?
417 */
74ae4b63 418 struct commit *one = lookup_commit_reference(revs->repo, &oid);
30415d50
JS
419 struct object *obj;
420
421 if (e->item == &(one->object)) {
422 /*
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.
429 */
c251c83d 430 obj = parse_object_or_die(&oid, e->name);
30415d50 431 obj->flags |= SHOWN;
d9362ef9 432 add_pending_object(revs, obj, e->name);
30415d50 433 }
c8a571d8 434 goto skip_write_ref;
30415d50
JS
435 }
436
437 ref_count++;
703d2d41 438 write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
30415d50 439 write_or_die(bundle_fd, " ", 1);
fa303836 440 write_or_die(bundle_fd, display_ref, strlen(display_ref));
30415d50 441 write_or_die(bundle_fd, "\n", 1);
c8a571d8 442 skip_write_ref:
30415d50
JS
443 free(ref);
444 }
30415d50
JS
445
446 /* end header */
447 write_or_die(bundle_fd, "\n", 1);
d9362ef9
JK
448 return ref_count;
449}
450
fcb133e9 451int create_bundle(struct repository *r, const char *path,
79862b6b 452 int argc, const char **argv, struct argv_array *pack_options)
d9362ef9 453{
b2275868 454 struct lock_file lock = LOCK_INIT;
d9362ef9
JK
455 int bundle_fd = -1;
456 int bundle_to_stdout;
457 int ref_count = 0;
458 struct rev_info revs;
459
460 bundle_to_stdout = !strcmp(path, "-");
461 if (bundle_to_stdout)
462 bundle_fd = 1;
2c8ee1f5 463 else
d9362ef9
JK
464 bundle_fd = hold_lock_file_for_update(&lock, path,
465 LOCK_DIE_ON_ERROR);
466
467 /* write signature */
468 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
469
470 /* init revs to list objects for pack-objects later */
471 save_commit_buffer = 0;
74ae4b63 472 repo_init_revisions(r, &revs, NULL);
d9362ef9
JK
473
474 /* write prerequisites */
475 if (compute_and_write_prerequisites(bundle_fd, &revs, argc, argv))
f5ff5fb5 476 goto err;
d9362ef9
JK
477
478 argc = setup_revisions(argc, argv, &revs, NULL);
479
f5ff5fb5
SB
480 if (argc > 1) {
481 error(_("unrecognized argument: %s"), argv[1]);
482 goto err;
483 }
d9362ef9
JK
484
485 object_array_remove_duplicates(&revs.pending);
486
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)
f5ff5fb5 491 goto err;
30415d50
JS
492
493 /* write pack */
79862b6b 494 if (write_pack_data(bundle_fd, &revs, pack_options))
f5ff5fb5 495 goto err;
4ed7cd3a 496
0f5cdf65
CH
497 if (!bundle_to_stdout) {
498 if (commit_lock_file(&lock))
8a1e7eac 499 die_errno(_("cannot create '%s'"), path);
0f5cdf65 500 }
c20181e3 501 return 0;
f5ff5fb5 502err:
2c8ee1f5 503 rollback_lock_file(&lock);
f5ff5fb5 504 return -1;
30415d50
JS
505}
506
74ae4b63
NTND
507int unbundle(struct repository *r, struct bundle_header *header,
508 int bundle_fd, int flags)
30415d50
JS
509{
510 const char *argv_index_pack[] = {"index-pack",
be042aff 511 "--fix-thin", "--stdin", NULL, NULL};
d3180279 512 struct child_process ip = CHILD_PROCESS_INIT;
30415d50 513
be042aff
JH
514 if (flags & BUNDLE_VERBOSE)
515 argv_index_pack[3] = "-v";
516
74ae4b63 517 if (verify_bundle(r, header, 0))
30415d50 518 return -1;
30415d50
JS
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))
8a1e7eac 524 return error(_("index-pack died"));
30415d50
JS
525 return 0;
526}