setup.h: move declarations for setup.c functions from cache.h
[git.git] / t / helper / test-repository.c
blob6774f6245f0f29de1a59f3d9f9660006d547824c
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "commit-graph.h"
4 #include "commit.h"
5 #include "config.h"
6 #include "environment.h"
7 #include "hex.h"
8 #include "object-store.h"
9 #include "object.h"
10 #include "repository.h"
11 #include "setup.h"
12 #include "tree.h"
14 static void test_parse_commit_in_graph(const char *gitdir, const char *worktree,
15 const struct object_id *commit_oid)
17 struct repository r;
18 struct commit *c;
19 struct commit_list *parent;
21 setup_git_env(gitdir);
23 memset(the_repository, 0, sizeof(*the_repository));
25 if (repo_init(&r, gitdir, worktree))
26 die("Couldn't init repo");
28 repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
30 c = lookup_commit(&r, commit_oid);
32 if (!parse_commit_in_graph(&r, c))
33 die("Couldn't parse commit");
35 printf("%"PRItime, c->date);
36 for (parent = c->parents; parent; parent = parent->next)
37 printf(" %s", oid_to_hex(&parent->item->object.oid));
38 printf("\n");
40 repo_clear(&r);
43 static void test_get_commit_tree_in_graph(const char *gitdir,
44 const char *worktree,
45 const struct object_id *commit_oid)
47 struct repository r;
48 struct commit *c;
49 struct tree *tree;
51 setup_git_env(gitdir);
53 memset(the_repository, 0, sizeof(*the_repository));
55 if (repo_init(&r, gitdir, worktree))
56 die("Couldn't init repo");
58 repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
60 c = lookup_commit(&r, commit_oid);
63 * get_commit_tree_in_graph does not automatically parse the commit, so
64 * parse it first.
66 if (!parse_commit_in_graph(&r, c))
67 die("Couldn't parse commit");
68 tree = get_commit_tree_in_graph(&r, c);
69 if (!tree)
70 die("Couldn't get commit tree");
72 printf("%s\n", oid_to_hex(&tree->object.oid));
74 repo_clear(&r);
77 int cmd__repository(int argc, const char **argv)
79 int nongit_ok = 0;
81 setup_git_directory_gently(&nongit_ok);
83 if (argc < 2)
84 die("must have at least 2 arguments");
85 if (!strcmp(argv[1], "parse_commit_in_graph")) {
86 struct object_id oid;
87 if (argc < 5)
88 die("not enough arguments");
89 if (parse_oid_hex(argv[4], &oid, &argv[4]))
90 die("cannot parse oid '%s'", argv[4]);
91 test_parse_commit_in_graph(argv[2], argv[3], &oid);
92 } else if (!strcmp(argv[1], "get_commit_tree_in_graph")) {
93 struct object_id oid;
94 if (argc < 5)
95 die("not enough arguments");
96 if (parse_oid_hex(argv[4], &oid, &argv[4]))
97 die("cannot parse oid '%s'", argv[4]);
98 test_get_commit_tree_in_graph(argv[2], argv[3], &oid);
99 } else {
100 die("unrecognized '%s'", argv[1]);
102 return 0;