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