]> git.proxmox.com Git - libgit2.git/blame - tests/index/names.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / index / names.c
CommitLineData
0462fba5
ET
1#include "clar_libgit2.h"
2#include "index.h"
75d1c8c6 3#include "git2/sys/index.h"
0462fba5
ET
4#include "git2/repository.h"
5#include "../reset/reset_helpers.h"
6
7static git_repository *repo;
8static git_index *repo_index;
9
10#define TEST_REPO_PATH "mergedrepo"
11#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
12
ac3d33df 13/* Fixture setup and teardown */
0462fba5
ET
14void test_index_names__initialize(void)
15{
16 repo = cl_git_sandbox_init("mergedrepo");
17 git_repository_index(&repo_index, repo);
18}
19
20void test_index_names__cleanup(void)
21{
22 git_index_free(repo_index);
23 repo_index = NULL;
1fed6b07 24
0462fba5
ET
25 cl_git_sandbox_cleanup();
26}
27
ac3d33df
JK
28static 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
0462fba5
ET
64void test_index_names__add(void)
65{
66 const git_index_name_entry *conflict_name;
67
ac3d33df 68 index_add_conflicts();
0462fba5
ET
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"));
1fed6b07 72
0462fba5 73 cl_assert(git_index_name_entrycount(repo_index) == 3);
1fed6b07 74
0462fba5
ET
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);
1fed6b07 79
0462fba5
ET
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);
ac3d33df
JK
89
90 cl_git_pass(git_index_write(repo_index));
0462fba5
ET
91}
92
93void test_index_names__roundtrip(void)
94{
95 const git_index_name_entry *conflict_name;
1fed6b07 96
0462fba5
ET
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"));
1fed6b07 100
0462fba5
ET
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);
1fed6b07 104
8e5a8ef8 105 cl_git_pass(git_index_read(repo_index, true));
0462fba5 106 cl_assert(git_index_name_entrycount(repo_index) == 3);
1fed6b07 107
0462fba5
ET
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);
1fed6b07 112
0462fba5
ET
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);
1fed6b07 117
0462fba5
ET
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);
4f7897ab
ET
122}
123
124void test_index_names__cleaned_on_reset_hard(void)
125{
126 git_object *target;
127
0d847a31 128 cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
4f7897ab
ET
129
130 test_index_names__add();
23a17803 131 cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
4f7897ab
ET
132 cl_assert(git_index_name_entrycount(repo_index) == 0);
133
134 git_object_free(target);
135}
136
137void test_index_names__cleaned_on_reset_mixed(void)
138{
139 git_object *target;
140
0d847a31 141 cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
4f7897ab
ET
142
143 test_index_names__add();
23a17803 144 cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL));
4f7897ab
ET
145 cl_assert(git_index_name_entrycount(repo_index) == 0);
146
147 git_object_free(target);
148}
149
150void test_index_names__cleaned_on_checkout_tree(void)
151{
152 git_oid oid;
153 git_object *obj;
6affd71f 154 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
4f7897ab 155
ac3d33df 156 opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
4f7897ab
ET
157
158 test_index_names__add();
ac3d33df
JK
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));
4bf630b6 162 cl_assert_equal_sz(0, git_index_name_entrycount(repo_index));
4f7897ab
ET
163
164 git_object_free(obj);
165}
166
167void test_index_names__cleaned_on_checkout_head(void)
168{
6affd71f 169 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
4f7897ab 170
ac3d33df 171 opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
4f7897ab
ET
172
173 test_index_names__add();
ac3d33df 174 cl_git_pass(git_checkout_head(repo, &opts));
4bf630b6 175 cl_assert_equal_sz(0, git_index_name_entrycount(repo_index));
4f7897ab
ET
176}
177
178void test_index_names__retained_on_checkout_index(void)
179{
6affd71f 180 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
4f7897ab 181
ac3d33df 182 opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_UPDATE_ONLY;
1fed6b07 183
4f7897ab 184 test_index_names__add();
ac3d33df 185 cl_git_pass(git_checkout_index(repo, repo_index, &opts));
4f7897ab 186 cl_assert(git_index_name_entrycount(repo_index) > 0);
0462fba5 187}