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