Merge branch 'jh/json-writer'
[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         repo_init(&r, gitdir, worktree);
19
20         c = lookup_commit(&r, commit_oid);
21
22         if (!parse_commit_in_graph(&r, c))
23                 die("Couldn't parse commit");
24
25         printf("%"PRItime, c->date);
26         for (parent = c->parents; parent; parent = parent->next)
27                 printf(" %s", oid_to_hex(&parent->item->object.oid));
28         printf("\n");
29
30         repo_clear(&r);
31 }
32
33 static void test_get_commit_tree_in_graph(const char *gitdir,
34                                           const char *worktree,
35                                           const struct object_id *commit_oid)
36 {
37         struct repository r;
38         struct commit *c;
39         struct tree *tree;
40
41         repo_init(&r, gitdir, worktree);
42
43         c = lookup_commit(&r, commit_oid);
44
45         /*
46          * get_commit_tree_in_graph does not automatically parse the commit, so
47          * parse it first.
48          */
49         if (!parse_commit_in_graph(&r, c))
50                 die("Couldn't parse commit");
51         tree = get_commit_tree_in_graph(&r, c);
52         if (!tree)
53                 die("Couldn't get commit tree");
54
55         printf("%s\n", oid_to_hex(&tree->object.oid));
56
57         repo_clear(&r);
58 }
59
60 int cmd__repository(int argc, const char **argv)
61 {
62         if (argc < 2)
63                 die("must have at least 2 arguments");
64         if (!strcmp(argv[1], "parse_commit_in_graph")) {
65                 struct object_id oid;
66                 if (argc < 5)
67                         die("not enough arguments");
68                 if (parse_oid_hex(argv[4], &oid, &argv[4]))
69                         die("cannot parse oid '%s'", argv[4]);
70                 test_parse_commit_in_graph(argv[2], argv[3], &oid);
71         } else if (!strcmp(argv[1], "get_commit_tree_in_graph")) {
72                 struct object_id oid;
73                 if (argc < 5)
74                         die("not enough arguments");
75                 if (parse_oid_hex(argv[4], &oid, &argv[4]))
76                         die("cannot parse oid '%s'", argv[4]);
77                 test_get_commit_tree_in_graph(argv[2], argv[3], &oid);
78         } else {
79                 die("unrecognized '%s'", argv[1]);
80         }
81         return 0;
82 }