]>
Commit | Line | Data |
---|---|---|
f45ec1a0 ET |
1 | #include "clar_libgit2.h" |
2 | #include "index.h" | |
3 | #include "git2/repository.h" | |
4 | ||
5 | static git_repository *repo; | |
6 | static git_index *repo_index; | |
7 | ||
8 | #define TEST_REPO_PATH "mergedrepo" | |
9 | #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index" | |
10 | ||
11 | #define CONFLICTS_ONE_ANCESTOR_OID "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81" | |
12 | #define CONFLICTS_ONE_OUR_OID "6aea5f295304c36144ad6e9247a291b7f8112399" | |
13 | #define CONFLICTS_ONE_THEIR_OID "516bd85f78061e09ccc714561d7b504672cb52da" | |
14 | ||
15 | #define CONFLICTS_TWO_ANCESTOR_OID "84af62840be1b1c47b778a8a249f3ff45155038c" | |
16 | #define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2" | |
17 | #define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e" | |
18 | ||
19 | #define TEST_ANCESTOR_OID "f00ff00ff00ff00ff00ff00ff00ff00ff00ff00f" | |
20 | #define TEST_OUR_OID "b44bb44bb44bb44bb44bb44bb44bb44bb44bb44b" | |
21 | #define TEST_THEIR_OID "0123456789abcdef0123456789abcdef01234567" | |
22 | ||
23 | // Fixture setup and teardown | |
24 | void test_index_conflicts__initialize(void) | |
25 | { | |
26 | repo = cl_git_sandbox_init("mergedrepo"); | |
27 | git_repository_index(&repo_index, repo); | |
28 | } | |
29 | ||
30 | void test_index_conflicts__cleanup(void) | |
31 | { | |
32 | git_index_free(repo_index); | |
33 | cl_git_sandbox_cleanup(); | |
34 | } | |
35 | ||
36 | void test_index_conflicts__add(void) | |
37 | { | |
38 | git_index_entry ancestor_entry, our_entry, their_entry; | |
39 | ||
40 | cl_assert(git_index_entrycount(repo_index) == 8); | |
41 | ||
42 | memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); | |
43 | memset(&our_entry, 0x0, sizeof(git_index_entry)); | |
44 | memset(&their_entry, 0x0, sizeof(git_index_entry)); | |
45 | ||
46 | ancestor_entry.path = "test-one.txt"; | |
47 | ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT); | |
48 | git_oid_fromstr(&ancestor_entry.oid, TEST_ANCESTOR_OID); | |
49 | ||
50 | our_entry.path = "test-one.txt"; | |
51 | ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT); | |
52 | git_oid_fromstr(&our_entry.oid, TEST_OUR_OID); | |
53 | ||
54 | their_entry.path = "test-one.txt"; | |
55 | ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT); | |
56 | git_oid_fromstr(&their_entry.oid, TEST_THEIR_OID); | |
57 | ||
58 | cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry)); | |
59 | ||
60 | cl_assert(git_index_entrycount(repo_index) == 11); | |
61 | } | |
62 | ||
63 | void test_index_conflicts__add_fixes_incorrect_stage(void) | |
64 | { | |
65 | git_index_entry ancestor_entry, our_entry, their_entry; | |
66 | git_index_entry *conflict_entry[3]; | |
67 | ||
68 | cl_assert(git_index_entrycount(repo_index) == 8); | |
69 | ||
70 | memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); | |
71 | memset(&our_entry, 0x0, sizeof(git_index_entry)); | |
72 | memset(&their_entry, 0x0, sizeof(git_index_entry)); | |
73 | ||
74 | ancestor_entry.path = "test-one.txt"; | |
75 | ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT); | |
76 | git_oid_fromstr(&ancestor_entry.oid, TEST_ANCESTOR_OID); | |
77 | ||
78 | our_entry.path = "test-one.txt"; | |
79 | ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT); | |
80 | git_oid_fromstr(&our_entry.oid, TEST_OUR_OID); | |
81 | ||
82 | their_entry.path = "test-one.txt"; | |
83 | ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT); | |
84 | git_oid_fromstr(&their_entry.oid, TEST_THEIR_OID); | |
85 | ||
86 | cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry)); | |
87 | ||
88 | cl_assert(git_index_entrycount(repo_index) == 11); | |
89 | ||
90 | cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], repo_index, "test-one.txt")); | |
91 | ||
92 | cl_assert(git_index_entry_stage(conflict_entry[0]) == 1); | |
93 | cl_assert(git_index_entry_stage(conflict_entry[1]) == 2); | |
94 | cl_assert(git_index_entry_stage(conflict_entry[2]) == 3); | |
95 | } | |
96 | ||
97 | void test_index_conflicts__get(void) | |
98 | { | |
99 | git_index_entry *conflict_entry[3]; | |
100 | git_oid oid; | |
101 | ||
102 | cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], | |
103 | &conflict_entry[2], repo_index, "conflicts-one.txt")); | |
104 | ||
105 | cl_assert(strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0); | |
106 | ||
107 | git_oid_fromstr(&oid, CONFLICTS_ONE_ANCESTOR_OID); | |
108 | cl_assert(git_oid_cmp(&conflict_entry[0]->oid, &oid) == 0); | |
109 | ||
110 | git_oid_fromstr(&oid, CONFLICTS_ONE_OUR_OID); | |
111 | cl_assert(git_oid_cmp(&conflict_entry[1]->oid, &oid) == 0); | |
112 | ||
113 | git_oid_fromstr(&oid, CONFLICTS_ONE_THEIR_OID); | |
114 | cl_assert(git_oid_cmp(&conflict_entry[2]->oid, &oid) == 0); | |
115 | ||
116 | cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], | |
117 | &conflict_entry[2], repo_index, "conflicts-two.txt")); | |
118 | ||
119 | cl_assert(strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0); | |
120 | ||
121 | git_oid_fromstr(&oid, CONFLICTS_TWO_ANCESTOR_OID); | |
122 | cl_assert(git_oid_cmp(&conflict_entry[0]->oid, &oid) == 0); | |
123 | ||
124 | git_oid_fromstr(&oid, CONFLICTS_TWO_OUR_OID); | |
125 | cl_assert(git_oid_cmp(&conflict_entry[1]->oid, &oid) == 0); | |
126 | ||
127 | git_oid_fromstr(&oid, CONFLICTS_TWO_THEIR_OID); | |
128 | cl_assert(git_oid_cmp(&conflict_entry[2]->oid, &oid) == 0); | |
129 | } | |
130 | ||
131 | void test_index_conflicts__remove(void) | |
132 | { | |
133 | git_index_entry *entry; | |
134 | size_t i; | |
135 | ||
136 | cl_assert(git_index_entrycount(repo_index) == 8); | |
137 | ||
138 | cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-one.txt")); | |
139 | cl_assert(git_index_entrycount(repo_index) == 5); | |
140 | ||
141 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
142 | cl_assert(entry = git_index_get_byindex(repo_index, i)); | |
143 | cl_assert(strcmp(entry->path, "conflicts-one.txt") != 0); | |
144 | } | |
145 | ||
146 | cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-two.txt")); | |
147 | cl_assert(git_index_entrycount(repo_index) == 2); | |
148 | ||
149 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
150 | cl_assert(entry = git_index_get_byindex(repo_index, i)); | |
151 | cl_assert(strcmp(entry->path, "conflicts-two.txt") != 0); | |
152 | } | |
153 | } | |
154 | ||
155 | void test_index_conflicts__moved_to_reuc(void) | |
156 | { | |
157 | git_index_entry *entry; | |
158 | size_t i; | |
159 | ||
160 | cl_assert(git_index_entrycount(repo_index) == 8); | |
161 | ||
162 | cl_git_mkfile("./mergedrepo/conflicts-one.txt", "new-file\n"); | |
163 | ||
164 | cl_git_pass(git_index_add_from_workdir(repo_index, "conflicts-one.txt")); | |
165 | ||
166 | cl_assert(git_index_entrycount(repo_index) == 6); | |
167 | ||
168 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
169 | cl_assert(entry = git_index_get_byindex(repo_index, i)); | |
170 | ||
171 | if (strcmp(entry->path, "conflicts-one.txt") == 0) | |
172 | cl_assert(git_index_entry_stage(entry) == 0); | |
173 | } | |
174 | } | |
175 | ||
176 | void test_index_conflicts__remove_all_conflicts(void) | |
177 | { | |
178 | size_t i; | |
179 | git_index_entry *entry; | |
180 | ||
181 | cl_assert(git_index_entrycount(repo_index) == 8); | |
182 | ||
7cc1bf0f | 183 | cl_assert_equal_i(true, git_index_has_conflicts(repo_index)); |
184 | ||
f45ec1a0 ET |
185 | git_index_conflict_cleanup(repo_index); |
186 | ||
7cc1bf0f | 187 | cl_assert_equal_i(false, git_index_has_conflicts(repo_index)); |
188 | ||
f45ec1a0 ET |
189 | cl_assert(git_index_entrycount(repo_index) == 2); |
190 | ||
191 | for (i = 0; i < git_index_entrycount(repo_index); i++) { | |
192 | cl_assert(entry = git_index_get_byindex(repo_index, i)); | |
193 | cl_assert(git_index_entry_stage(entry) == 0); | |
194 | } | |
195 | } | |
196 | ||
197 | void test_index_conflicts__partial(void) | |
198 | { | |
199 | git_index_entry ancestor_entry, our_entry, their_entry; | |
200 | git_index_entry *conflict_entry[3]; | |
201 | ||
202 | cl_assert(git_index_entrycount(repo_index) == 8); | |
203 | ||
204 | memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); | |
205 | memset(&our_entry, 0x0, sizeof(git_index_entry)); | |
206 | memset(&their_entry, 0x0, sizeof(git_index_entry)); | |
207 | ||
208 | ancestor_entry.path = "test-one.txt"; | |
209 | ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT); | |
210 | git_oid_fromstr(&ancestor_entry.oid, TEST_ANCESTOR_OID); | |
211 | ||
212 | cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, NULL, NULL)); | |
213 | cl_assert(git_index_entrycount(repo_index) == 9); | |
214 | ||
215 | cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], | |
216 | &conflict_entry[2], repo_index, "test-one.txt")); | |
217 | ||
218 | cl_assert(git_oid_cmp(&ancestor_entry.oid, &conflict_entry[0]->oid) == 0); | |
219 | cl_assert(conflict_entry[1] == NULL); | |
220 | cl_assert(conflict_entry[2] == NULL); | |
221 | } |