Merge branch 'js/test-git-installed'
[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 "object-store.h"
7 #include "object.h"
8 #include "repository.h"
9 #include "tree.h"
10
11 static void test_parse_commit_in_graph(const char *gitdir, const char *worktree,
12                                        const struct object_id *commit_oid)
13 {
14         struct repository r;
15         struct commit *c;
16         struct commit_list *parent;
17
18         setup_git_env(gitdir);
19
20         memset(the_repository, 0, sizeof(*the_repository));
21
22         /* TODO: Needed for temporary hack in hashcmp, see 183a638b7da. */
23         repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
24
25         if (repo_init(&r, gitdir, worktree))
26                 die("Couldn't init repo");
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         /* TODO: Needed for temporary hack in hashcmp, see 183a638b7da. */
54         repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
55
56         if (repo_init(&r, gitdir, worktree))
57                 die("Couldn't init repo");
58
59         c = lookup_commit(&r, commit_oid);
60
61         /*
62          * get_commit_tree_in_graph does not automatically parse the commit, so
63          * parse it first.
64          */
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");
70
71         printf("%s\n", oid_to_hex(&tree->object.oid));
72
73         repo_clear(&r);
74 }
75
76 int cmd__repository(int argc, const char **argv)
77 {
78         if (argc < 2)
79                 die("must have at least 2 arguments");
80         if (!strcmp(argv[1], "parse_commit_in_graph")) {
81                 struct object_id oid;
82                 if (argc < 5)
83                         die("not enough arguments");
84                 if (parse_oid_hex(argv[4], &oid, &argv[4]))
85                         die("cannot parse oid '%s'", argv[4]);
86                 test_parse_commit_in_graph(argv[2], argv[3], &oid);
87         } else if (!strcmp(argv[1], "get_commit_tree_in_graph")) {
88                 struct object_id oid;
89                 if (argc < 5)
90                         die("not enough arguments");
91                 if (parse_oid_hex(argv[4], &oid, &argv[4]))
92                         die("cannot parse oid '%s'", argv[4]);
93                 test_get_commit_tree_in_graph(argv[2], argv[3], &oid);
94         } else {
95                 die("unrecognized '%s'", argv[1]);
96         }
97         return 0;
98 }