Merge branch 'ds/trace-log-progress-fix'
[git.git] / t / helper / test-repository.c
blob56f0e3c1bef293dd505e28af4b48dfce11490b3c
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "commit-graph.h"
4 #include "commit.h"
5 #include "config.h"
6 #include "object-store.h"
7 #include "object.h"
8 #include "repository.h"
9 #include "tree.h"
11 static void test_parse_commit_in_graph(const char *gitdir, const char *worktree,
12 const struct object_id *commit_oid)
14 struct repository r;
15 struct commit *c;
16 struct commit_list *parent;
18 setup_git_env(gitdir);
20 memset(the_repository, 0, sizeof(*the_repository));
22 if (repo_init(&r, gitdir, worktree))
23 die("Couldn't init repo");
25 repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
27 c = lookup_commit(&r, commit_oid);
29 if (!parse_commit_in_graph(&r, c))
30 die("Couldn't parse commit");
32 printf("%"PRItime, c->date);
33 for (parent = c->parents; parent; parent = parent->next)
34 printf(" %s", oid_to_hex(&parent->item->object.oid));
35 printf("\n");
37 repo_clear(&r);
40 static void test_get_commit_tree_in_graph(const char *gitdir,
41 const char *worktree,
42 const struct object_id *commit_oid)
44 struct repository r;
45 struct commit *c;
46 struct tree *tree;
48 setup_git_env(gitdir);
50 memset(the_repository, 0, sizeof(*the_repository));
52 if (repo_init(&r, gitdir, worktree))
53 die("Couldn't init repo");
55 repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
57 c = lookup_commit(&r, commit_oid);
60 * get_commit_tree_in_graph does not automatically parse the commit, so
61 * parse it first.
63 if (!parse_commit_in_graph(&r, c))
64 die("Couldn't parse commit");
65 tree = get_commit_tree_in_graph(&r, c);
66 if (!tree)
67 die("Couldn't get commit tree");
69 printf("%s\n", oid_to_hex(&tree->object.oid));
71 repo_clear(&r);
74 int cmd__repository(int argc, const char **argv)
76 int nongit_ok = 0;
78 setup_git_directory_gently(&nongit_ok);
80 if (argc < 2)
81 die("must have at least 2 arguments");
82 if (!strcmp(argv[1], "parse_commit_in_graph")) {
83 struct object_id oid;
84 if (argc < 5)
85 die("not enough arguments");
86 if (parse_oid_hex(argv[4], &oid, &argv[4]))
87 die("cannot parse oid '%s'", argv[4]);
88 test_parse_commit_in_graph(argv[2], argv[3], &oid);
89 } else if (!strcmp(argv[1], "get_commit_tree_in_graph")) {
90 struct object_id oid;
91 if (argc < 5)
92 die("not enough arguments");
93 if (parse_oid_hex(argv[4], &oid, &argv[4]))
94 die("cannot parse oid '%s'", argv[4]);
95 test_get_commit_tree_in_graph(argv[2], argv[3], &oid);
96 } else {
97 die("unrecognized '%s'", argv[1]);
99 return 0;