| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "test-tool.h" |
| 4 | #include "object-name.h" |
| 5 | #include "odb.h" |
| 6 | #include "packfile.h" |
| 7 | #include "parse-options.h" |
| 8 | #include "setup.h" |
| 9 | |
| 10 | /* |
| 11 | * Display the path(s), one per line, of the packfile(s) containing |
| 12 | * the given object. |
| 13 | * |
| 14 | * With '--show-offset', display the offset selected by |
| 15 | * find_pack_entry_one() instead of the packfile path. |
| 16 | * |
| 17 | * If '--check-count <n>' is passed, then error out if the number of |
| 18 | * packfiles containing the object is not <n>. |
| 19 | */ |
| 20 | |
| 21 | static const char *const find_pack_usage[] = { |
| 22 | "test-tool find-pack [--check-count <n>] [--show-offset] <object>", |
| 23 | NULL |
| 24 | }; |
| 25 | |
| 26 | int cmd__find_pack(int argc, const char **argv) |
| 27 | { |
| 28 | struct object_id oid; |
| 29 | struct packed_git *p; |
| 30 | int count = -1, actual_count = 0, show_offset = 0; |
| 31 | const char *prefix = setup_git_directory(the_repository); |
| 32 | |
| 33 | struct option options[] = { |
| 34 | OPT_INTEGER('c', "check-count", &count, "expected number of packs"), |
| 35 | OPT_BOOL(0, "show-offset", &show_offset, |
| 36 | "show matching pack offsets"), |
| 37 | OPT_END(), |
| 38 | }; |
| 39 | |
| 40 | argc = parse_options(argc, argv, prefix, options, find_pack_usage, 0); |
| 41 | if (argc != 1) |
| 42 | usage(find_pack_usage[0]); |
| 43 | |
| 44 | if (repo_get_oid(the_repository, argv[0], &oid)) |
| 45 | die("cannot parse %s as an object name", argv[0]); |
| 46 | |
| 47 | repo_for_each_pack(the_repository, p) { |
| 48 | off_t offset = find_pack_entry_one(&oid, p); |
| 49 | |
| 50 | if (offset) { |
| 51 | if (show_offset) |
| 52 | printf("%"PRIuMAX"\n", (uintmax_t)offset); |
| 53 | else |
| 54 | printf("%s\n", p->pack_name); |
| 55 | actual_count++; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (count > -1 && count != actual_count) |
| 60 | die("bad packfile count %d instead of %d", actual_count, count); |
| 61 | |
| 62 | return 0; |
| 63 | } |