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