object-store-ll.h: split this header out of object-store.h
[alt-git.git] / t / helper / test-repository.c
blob4cd8a952e5c6f55fbb3cfdbffe8c3be07bf008d7
1 #include "test-tool.h"
2 #include "commit-graph.h"
3 #include "commit.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "hex.h"
7 #include "object-store-ll.h"
8 #include "object.h"
9 #include "repository.h"
10 #include "setup.h"
11 #include "tree.h"
13 static void test_parse_commit_in_graph(const char *gitdir, const char *worktree,
14 const struct object_id *commit_oid)
16 struct repository r;
17 struct commit *c;
18 struct commit_list *parent;
20 setup_git_env(gitdir);
22 memset(the_repository, 0, sizeof(*the_repository));
24 if (repo_init(&r, gitdir, worktree))
25 die("Couldn't init repo");
27 repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
29 c = lookup_commit(&r, commit_oid);
31 if (!parse_commit_in_graph(&r, c))
32 die("Couldn't parse commit");
34 printf("%"PRItime, c->date);
35 for (parent = c->parents; parent; parent = parent->next)
36 printf(" %s", oid_to_hex(&parent->item->object.oid));
37 printf("\n");
39 repo_clear(&r);
42 static void test_get_commit_tree_in_graph(const char *gitdir,
43 const char *worktree,
44 const struct object_id *commit_oid)
46 struct repository r;
47 struct commit *c;
48 struct tree *tree;
50 setup_git_env(gitdir);
52 memset(the_repository, 0, sizeof(*the_repository));
54 if (repo_init(&r, gitdir, worktree))
55 die("Couldn't init repo");
57 repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
59 c = lookup_commit(&r, commit_oid);
62 * get_commit_tree_in_graph does not automatically parse the commit, so
63 * parse it first.
65 if (!parse_commit_in_graph(&r, c))
66 die("Couldn't parse commit");
67 tree = get_commit_tree_in_graph(&r, c);
68 if (!tree)
69 die("Couldn't get commit tree");
71 printf("%s\n", oid_to_hex(&tree->object.oid));
73 repo_clear(&r);
76 int cmd__repository(int argc, const char **argv)
78 int nongit_ok = 0;
80 setup_git_directory_gently(&nongit_ok);
82 if (argc < 2)
83 die("must have at least 2 arguments");
84 if (!strcmp(argv[1], "parse_commit_in_graph")) {
85 struct object_id oid;
86 if (argc < 5)
87 die("not enough arguments");
88 if (parse_oid_hex(argv[4], &oid, &argv[4]))
89 die("cannot parse oid '%s'", argv[4]);
90 test_parse_commit_in_graph(argv[2], argv[3], &oid);
91 } else if (!strcmp(argv[1], "get_commit_tree_in_graph")) {
92 struct object_id oid;
93 if (argc < 5)
94 die("not enough arguments");
95 if (parse_oid_hex(argv[4], &oid, &argv[4]))
96 die("cannot parse oid '%s'", argv[4]);
97 test_get_commit_tree_in_graph(argv[2], argv[3], &oid);
98 } else {
99 die("unrecognized '%s'", argv[1]);
101 return 0;