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