1 #include "clar_libgit2.h"
2 #include "index.h"
3 #include "git2/sys/index.h"
4 #include "git2/repository.h"
5 #include "../reset/reset_helpers.h"
6
7 static git_repository *repo;
8 static git_index *repo_index;
9
10 #define TEST_REPO_PATH "mergedrepo"
11 #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
12
13 /* Fixture setup and teardown */
test_index_names__initialize(void)14 void test_index_names__initialize(void)
15 {
16 repo = cl_git_sandbox_init("mergedrepo");
17 git_repository_index(&repo_index, repo);
18 }
19
test_index_names__cleanup(void)20 void test_index_names__cleanup(void)
21 {
22 git_index_free(repo_index);
23 repo_index = NULL;
24
25 cl_git_sandbox_cleanup();
26 }
27
index_add_conflicts(void)28 static void index_add_conflicts(void)
29 {
30 git_index_entry entry = {{0}};
31 const char *paths[][3] = {
32 { "ancestor", "ours", "theirs" },
33 { "ancestor2", "ours2", "theirs2" },
34 { "ancestor3", "ours3", "theirs3" } };
35 const char **conflict;
36 size_t i;
37
38 for (i = 0; i < ARRAY_SIZE(paths); i++) {
39 conflict = paths[i];
40
41 /* ancestor */
42 entry.path = conflict[0];
43 entry.mode = GIT_FILEMODE_BLOB;
44 GIT_INDEX_ENTRY_STAGE_SET(&entry, GIT_INDEX_STAGE_ANCESTOR);
45 git_oid_fromstr(&entry.id, "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81");
46 cl_git_pass(git_index_add(repo_index, &entry));
47
48 /* ours */
49 entry.path = conflict[1];
50 entry.mode = GIT_FILEMODE_BLOB;
51 GIT_INDEX_ENTRY_STAGE_SET(&entry, GIT_INDEX_STAGE_OURS);
52 git_oid_fromstr(&entry.id, "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81");
53 cl_git_pass(git_index_add(repo_index, &entry));
54
55 /* theirs */
56 entry.path = conflict[2];
57 entry.mode = GIT_FILEMODE_BLOB;
58 GIT_INDEX_ENTRY_STAGE_SET(&entry, GIT_INDEX_STAGE_THEIRS);
59 git_oid_fromstr(&entry.id, "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81");
60 cl_git_pass(git_index_add(repo_index, &entry));
61 }
62 }
63
test_index_names__add(void)64 void test_index_names__add(void)
65 {
66 const git_index_name_entry *conflict_name;
67
68 index_add_conflicts();
69 cl_git_pass(git_index_name_add(repo_index, "ancestor", "ours", "theirs"));
70 cl_git_pass(git_index_name_add(repo_index, "ancestor2", "ours2", NULL));
71 cl_git_pass(git_index_name_add(repo_index, "ancestor3", NULL, "theirs3"));
72
73 cl_assert(git_index_name_entrycount(repo_index) == 3);
74
75 conflict_name = git_index_name_get_byindex(repo_index, 0);
76 cl_assert(strcmp(conflict_name->ancestor, "ancestor") == 0);
77 cl_assert(strcmp(conflict_name->ours, "ours") == 0);
78 cl_assert(strcmp(conflict_name->theirs, "theirs") == 0);
79
80 conflict_name = git_index_name_get_byindex(repo_index, 1);
81 cl_assert(strcmp(conflict_name->ancestor, "ancestor2") == 0);
82 cl_assert(strcmp(conflict_name->ours, "ours2") == 0);
83 cl_assert(conflict_name->theirs == NULL);
84
85 conflict_name = git_index_name_get_byindex(repo_index, 2);
86 cl_assert(strcmp(conflict_name->ancestor, "ancestor3") == 0);
87 cl_assert(conflict_name->ours == NULL);
88 cl_assert(strcmp(conflict_name->theirs, "theirs3") == 0);
89
90 cl_git_pass(git_index_write(repo_index));
91 }
92
test_index_names__roundtrip(void)93 void test_index_names__roundtrip(void)
94 {
95 const git_index_name_entry *conflict_name;
96
97 cl_git_pass(git_index_name_add(repo_index, "ancestor", "ours", "theirs"));
98 cl_git_pass(git_index_name_add(repo_index, "ancestor2", "ours2", NULL));
99 cl_git_pass(git_index_name_add(repo_index, "ancestor3", NULL, "theirs3"));
100
101 cl_git_pass(git_index_write(repo_index));
102 git_index_clear(repo_index);
103 cl_assert(git_index_name_entrycount(repo_index) == 0);
104
105 cl_git_pass(git_index_read(repo_index, true));
106 cl_assert(git_index_name_entrycount(repo_index) == 3);
107
108 conflict_name = git_index_name_get_byindex(repo_index, 0);
109 cl_assert(strcmp(conflict_name->ancestor, "ancestor") == 0);
110 cl_assert(strcmp(conflict_name->ours, "ours") == 0);
111 cl_assert(strcmp(conflict_name->theirs, "theirs") == 0);
112
113 conflict_name = git_index_name_get_byindex(repo_index, 1);
114 cl_assert(strcmp(conflict_name->ancestor, "ancestor2") == 0);
115 cl_assert(strcmp(conflict_name->ours, "ours2") == 0);
116 cl_assert(conflict_name->theirs == NULL);
117
118 conflict_name = git_index_name_get_byindex(repo_index, 2);
119 cl_assert(strcmp(conflict_name->ancestor, "ancestor3") == 0);
120 cl_assert(conflict_name->ours == NULL);
121 cl_assert(strcmp(conflict_name->theirs, "theirs3") == 0);
122 }
123
test_index_names__cleaned_on_reset_hard(void)124 void test_index_names__cleaned_on_reset_hard(void)
125 {
126 git_object *target;
127
128 cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
129
130 test_index_names__add();
131 cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
132 cl_assert(git_index_name_entrycount(repo_index) == 0);
133
134 git_object_free(target);
135 }
136
test_index_names__cleaned_on_reset_mixed(void)137 void test_index_names__cleaned_on_reset_mixed(void)
138 {
139 git_object *target;
140
141 cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
142
143 test_index_names__add();
144 cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL));
145 cl_assert(git_index_name_entrycount(repo_index) == 0);
146
147 git_object_free(target);
148 }
149
test_index_names__cleaned_on_checkout_tree(void)150 void test_index_names__cleaned_on_checkout_tree(void)
151 {
152 git_oid oid;
153 git_object *obj;
154 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
155
156 opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
157
158 test_index_names__add();
159 cl_git_pass(git_reference_name_to_id(&oid, repo, "refs/heads/master"));
160 cl_git_pass(git_object_lookup(&obj, repo, &oid, GIT_OBJECT_ANY));
161 cl_git_pass(git_checkout_tree(repo, obj, &opts));
162 cl_assert_equal_sz(0, git_index_name_entrycount(repo_index));
163
164 git_object_free(obj);
165 }
166
test_index_names__cleaned_on_checkout_head(void)167 void test_index_names__cleaned_on_checkout_head(void)
168 {
169 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
170
171 opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
172
173 test_index_names__add();
174 cl_git_pass(git_checkout_head(repo, &opts));
175 cl_assert_equal_sz(0, git_index_name_entrycount(repo_index));
176 }
177
test_index_names__retained_on_checkout_index(void)178 void test_index_names__retained_on_checkout_index(void)
179 {
180 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
181
182 opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
183
184 test_index_names__add();
185 cl_git_pass(git_checkout_index(repo, repo_index, &opts));
186 cl_assert(git_index_name_entrycount(repo_index) > 0);
187 }
188