]>
Commit | Line | Data |
---|---|---|
00a48934 BS |
1 | #include "clar_libgit2.h" |
2 | ||
3 | #include "repository.h" | |
4 | #include "git2/reflog.h" | |
5 | #include "reflog.h" | |
6 | ||
7 | static const char *current_master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"; | |
8 | static const char *current_head_target = "refs/heads/master"; | |
9 | ||
10 | static git_repository *g_repo; | |
11 | ||
471bb8b1 | 12 | void test_refs_create__initialize(void) |
00a48934 BS |
13 | { |
14 | g_repo = cl_git_sandbox_init("testrepo"); | |
15 | } | |
16 | ||
471bb8b1 | 17 | void test_refs_create__cleanup(void) |
00a48934 BS |
18 | { |
19 | cl_git_sandbox_cleanup(); | |
20 | } | |
21 | ||
471bb8b1 | 22 | void test_refs_create__symbolic(void) |
00a48934 BS |
23 | { |
24 | // create a new symbolic reference | |
25 | git_reference *new_reference, *looked_up_ref, *resolved_ref; | |
26 | git_repository *repo2; | |
27 | git_oid id; | |
28 | git_buf ref_path = GIT_BUF_INIT; | |
29 | ||
30 | const char *new_head_tracker = "another-head-tracker"; | |
31 | ||
32 | git_oid_fromstr(&id, current_master_tip); | |
33 | ||
34 | /* Retrieve the physical path to the symbolic ref for further cleaning */ | |
35 | cl_git_pass(git_buf_joinpath(&ref_path, g_repo->path_repository, new_head_tracker)); | |
36 | git_buf_free(&ref_path); | |
37 | ||
38 | /* Create and write the new symbolic reference */ | |
39 | cl_git_pass(git_reference_create_symbolic(&new_reference, g_repo, new_head_tracker, current_head_target, 0)); | |
40 | ||
41 | /* Ensure the reference can be looked-up... */ | |
42 | cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker)); | |
43 | cl_assert(git_reference_type(looked_up_ref) & GIT_REF_SYMBOLIC); | |
44 | cl_assert(git_reference_is_packed(looked_up_ref) == 0); | |
946a6dc4 | 45 | cl_assert_equal_s(looked_up_ref->name, new_head_tracker); |
00a48934 BS |
46 | |
47 | /* ...peeled.. */ | |
48 | cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref)); | |
49 | cl_assert(git_reference_type(resolved_ref) == GIT_REF_OID); | |
50 | ||
51 | /* ...and that it points to the current master tip */ | |
52 | cl_assert(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0); | |
53 | git_reference_free(looked_up_ref); | |
54 | git_reference_free(resolved_ref); | |
55 | ||
56 | /* Similar test with a fresh new repository */ | |
57 | cl_git_pass(git_repository_open(&repo2, "testrepo")); | |
58 | ||
59 | cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head_tracker)); | |
60 | cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref)); | |
61 | cl_assert(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0); | |
62 | ||
63 | git_repository_free(repo2); | |
64 | ||
65 | git_reference_free(new_reference); | |
66 | git_reference_free(looked_up_ref); | |
67 | git_reference_free(resolved_ref); | |
68 | } | |
69 | ||
471bb8b1 | 70 | void test_refs_create__deep_symbolic(void) |
00a48934 BS |
71 | { |
72 | // create a deep symbolic reference | |
73 | git_reference *new_reference, *looked_up_ref, *resolved_ref; | |
74 | git_oid id; | |
75 | git_buf ref_path = GIT_BUF_INIT; | |
76 | ||
77 | const char *new_head_tracker = "deep/rooted/tracker"; | |
78 | ||
79 | git_oid_fromstr(&id, current_master_tip); | |
80 | ||
81 | cl_git_pass(git_buf_joinpath(&ref_path, g_repo->path_repository, new_head_tracker)); | |
82 | cl_git_pass(git_reference_create_symbolic(&new_reference, g_repo, new_head_tracker, current_head_target, 0)); | |
83 | cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head_tracker)); | |
84 | cl_git_pass(git_reference_resolve(&resolved_ref, looked_up_ref)); | |
85 | cl_assert(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0); | |
86 | ||
87 | git_reference_free(new_reference); | |
88 | git_reference_free(looked_up_ref); | |
89 | git_reference_free(resolved_ref); | |
90 | git_buf_free(&ref_path); | |
91 | } | |
92 | ||
471bb8b1 | 93 | void test_refs_create__oid(void) |
00a48934 BS |
94 | { |
95 | // create a new OID reference | |
96 | git_reference *new_reference, *looked_up_ref; | |
97 | git_repository *repo2; | |
98 | git_oid id; | |
99 | git_buf ref_path = GIT_BUF_INIT; | |
100 | ||
101 | const char *new_head = "refs/heads/new-head"; | |
102 | ||
103 | git_oid_fromstr(&id, current_master_tip); | |
104 | ||
105 | /* Retrieve the physical path to the symbolic ref for further cleaning */ | |
106 | cl_git_pass(git_buf_joinpath(&ref_path, g_repo->path_repository, new_head)); | |
107 | ||
108 | /* Create and write the new object id reference */ | |
109 | cl_git_pass(git_reference_create_oid(&new_reference, g_repo, new_head, &id, 0)); | |
110 | ||
111 | /* Ensure the reference can be looked-up... */ | |
112 | cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head)); | |
113 | cl_assert(git_reference_type(looked_up_ref) & GIT_REF_OID); | |
114 | cl_assert(git_reference_is_packed(looked_up_ref) == 0); | |
946a6dc4 | 115 | cl_assert_equal_s(looked_up_ref->name, new_head); |
00a48934 BS |
116 | |
117 | /* ...and that it points to the current master tip */ | |
118 | cl_assert(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0); | |
119 | git_reference_free(looked_up_ref); | |
120 | ||
121 | /* Similar test with a fresh new repository */ | |
122 | cl_git_pass(git_repository_open(&repo2, "testrepo")); | |
123 | ||
124 | cl_git_pass(git_reference_lookup(&looked_up_ref, repo2, new_head)); | |
125 | cl_assert(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0); | |
126 | ||
127 | git_repository_free(repo2); | |
128 | ||
129 | git_reference_free(new_reference); | |
130 | git_reference_free(looked_up_ref); | |
131 | git_buf_free(&ref_path); | |
132 | } | |
133 | ||
471bb8b1 | 134 | void test_refs_create__oid_unknown(void) |
00a48934 BS |
135 | { |
136 | // Can not create a new OID reference which targets at an unknown id | |
137 | git_reference *new_reference, *looked_up_ref; | |
138 | git_oid id; | |
139 | ||
140 | const char *new_head = "refs/heads/new-head"; | |
141 | ||
142 | git_oid_fromstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644"); | |
143 | ||
144 | /* Create and write the new object id reference */ | |
145 | cl_git_fail(git_reference_create_oid(&new_reference, g_repo, new_head, &id, 0)); | |
146 | ||
147 | /* Ensure the reference can't be looked-up... */ | |
148 | cl_git_fail(git_reference_lookup(&looked_up_ref, g_repo, new_head)); | |
149 | } |