Merge branch 'js/no-builtins-on-disk-option'
[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         if (repo_init(&r, gitdir, worktree))
23                 die("Couldn't init repo");
24
25         repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
26
27         c = lookup_commit(&r, commit_oid);
28
29         if (!parse_commit_in_graph(&r, c))
30                 die("Couldn't parse commit");
31
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");
36
37         repo_clear(&r);
38 }
39
40 static void test_get_commit_tree_in_graph(const char *gitdir,
41                                           const char *worktree,
42                                           const struct object_id *commit_oid)
43 {
44         struct repository r;
45         struct commit *c;
46         struct tree *tree;
47
48         setup_git_env(gitdir);
49
50         memset(the_repository, 0, sizeof(*the_repository));
51
52         if (repo_init(&r, gitdir, worktree))
53                 die("Couldn't init repo");
54
55         repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
56
57         c = lookup_commit(&r, commit_oid);
58
59         /*
60          * get_commit_tree_in_graph does not automatically parse the commit, so
61          * parse it first.
62          */
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");
68
69         printf("%s\n", oid_to_hex(&tree->object.oid));
70
71         repo_clear(&r);
72 }
73
74 int cmd__repository(int argc, const char **argv)
75 {
76         int nongit_ok = 0;
77
78         setup_git_directory_gently(&nongit_ok);
79
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]);
98         }
99         return 0;
100 }