| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "test-tool.h" |
| 4 | #include "commit-graph.h" |
| 5 | #include "commit.h" |
| 6 | #include "environment.h" |
| 7 | #include "hex.h" |
| 8 | #include "object.h" |
| 9 | #include "repository.h" |
| 10 | #include "setup.h" |
| 11 | #include "tree.h" |
| 12 | |
| 13 | static void test_parse_commit_in_graph(const char *gitdir, const char *worktree, |
| 14 | const struct object_id *commit_oid) |
| 15 | { |
| 16 | struct repository r; |
| 17 | struct commit *c; |
| 18 | struct commit_list *parent; |
| 19 | |
| 20 | if (repo_init(&r, gitdir, worktree)) |
| 21 | die("Couldn't init repo"); |
| 22 | |
| 23 | repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo)); |
| 24 | |
| 25 | c = lookup_commit(&r, commit_oid); |
| 26 | |
| 27 | if (!parse_commit_in_graph(&r, c)) |
| 28 | die("Couldn't parse commit"); |
| 29 | |
| 30 | printf("%"PRItime, c->date); |
| 31 | for (parent = c->parents; parent; parent = parent->next) |
| 32 | printf(" %s", oid_to_hex(&parent->item->object.oid)); |
| 33 | printf("\n"); |
| 34 | |
| 35 | repo_clear(&r); |
| 36 | } |
| 37 | |
| 38 | static void test_get_commit_tree_in_graph(const char *gitdir, |
| 39 | const char *worktree, |
| 40 | const struct object_id *commit_oid) |
| 41 | { |
| 42 | struct repository r; |
| 43 | struct commit *c; |
| 44 | struct tree *tree; |
| 45 | |
| 46 | if (repo_init(&r, gitdir, worktree)) |
| 47 | die("Couldn't init repo"); |
| 48 | |
| 49 | repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo)); |
| 50 | |
| 51 | c = lookup_commit(&r, commit_oid); |
| 52 | |
| 53 | /* |
| 54 | * get_commit_tree_in_graph does not automatically parse the commit, so |
| 55 | * parse it first. |
| 56 | */ |
| 57 | if (!parse_commit_in_graph(&r, c)) |
| 58 | die("Couldn't parse commit"); |
| 59 | tree = get_commit_tree_in_graph(&r, c); |
| 60 | if (!tree) |
| 61 | die("Couldn't get commit tree"); |
| 62 | |
| 63 | printf("%s\n", oid_to_hex(&tree->object.oid)); |
| 64 | |
| 65 | repo_clear(&r); |
| 66 | } |
| 67 | |
| 68 | int cmd__repository(int argc, const char **argv) |
| 69 | { |
| 70 | if (argc < 2) |
| 71 | die("must have at least 2 arguments"); |
| 72 | if (!strcmp(argv[1], "parse_commit_in_graph")) { |
| 73 | struct object_id oid; |
| 74 | if (argc < 5) |
| 75 | die("not enough arguments"); |
| 76 | if (parse_oid_hex_any(argv[4], &oid, &argv[4]) == GIT_HASH_UNKNOWN) |
| 77 | die("cannot parse oid '%s'", argv[4]); |
| 78 | test_parse_commit_in_graph(argv[2], argv[3], &oid); |
| 79 | } else if (!strcmp(argv[1], "get_commit_tree_in_graph")) { |
| 80 | struct object_id oid; |
| 81 | if (argc < 5) |
| 82 | die("not enough arguments"); |
| 83 | if (parse_oid_hex_any(argv[4], &oid, &argv[4]) == GIT_HASH_UNKNOWN) |
| 84 | die("cannot parse oid '%s'", argv[4]); |
| 85 | test_get_commit_tree_in_graph(argv[2], argv[3], &oid); |
| 86 | } else { |
| 87 | die("unrecognized '%s'", argv[1]); |
| 88 | } |
| 89 | return 0; |
| 90 | } |