1 #include "clar_libgit2.h"
3 #include "git2/sys/index.h"
4 #include "git2/repository.h"
5 #include "../reset/reset_helpers.h"
7 static git_repository
*repo
;
8 static git_index
*repo_index
;
10 #define TEST_REPO_PATH "mergedrepo"
11 #define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
13 // Fixture setup and teardown
14 void test_index_names__initialize(void)
16 repo
= cl_git_sandbox_init("mergedrepo");
17 git_repository_index(&repo_index
, repo
);
20 void test_index_names__cleanup(void)
22 git_index_free(repo_index
);
25 cl_git_sandbox_cleanup();
28 void test_index_names__add(void)
30 const git_index_name_entry
*conflict_name
;
32 cl_git_pass(git_index_name_add(repo_index
, "ancestor", "ours", "theirs"));
33 cl_git_pass(git_index_name_add(repo_index
, "ancestor2", "ours2", NULL
));
34 cl_git_pass(git_index_name_add(repo_index
, "ancestor3", NULL
, "theirs3"));
36 cl_assert(git_index_name_entrycount(repo_index
) == 3);
38 conflict_name
= git_index_name_get_byindex(repo_index
, 0);
39 cl_assert(strcmp(conflict_name
->ancestor
, "ancestor") == 0);
40 cl_assert(strcmp(conflict_name
->ours
, "ours") == 0);
41 cl_assert(strcmp(conflict_name
->theirs
, "theirs") == 0);
43 conflict_name
= git_index_name_get_byindex(repo_index
, 1);
44 cl_assert(strcmp(conflict_name
->ancestor
, "ancestor2") == 0);
45 cl_assert(strcmp(conflict_name
->ours
, "ours2") == 0);
46 cl_assert(conflict_name
->theirs
== NULL
);
48 conflict_name
= git_index_name_get_byindex(repo_index
, 2);
49 cl_assert(strcmp(conflict_name
->ancestor
, "ancestor3") == 0);
50 cl_assert(conflict_name
->ours
== NULL
);
51 cl_assert(strcmp(conflict_name
->theirs
, "theirs3") == 0);
54 void test_index_names__roundtrip(void)
56 const git_index_name_entry
*conflict_name
;
58 cl_git_pass(git_index_name_add(repo_index
, "ancestor", "ours", "theirs"));
59 cl_git_pass(git_index_name_add(repo_index
, "ancestor2", "ours2", NULL
));
60 cl_git_pass(git_index_name_add(repo_index
, "ancestor3", NULL
, "theirs3"));
62 cl_git_pass(git_index_write(repo_index
));
63 git_index_clear(repo_index
);
64 cl_assert(git_index_name_entrycount(repo_index
) == 0);
66 cl_git_pass(git_index_read(repo_index
, true));
67 cl_assert(git_index_name_entrycount(repo_index
) == 3);
69 conflict_name
= git_index_name_get_byindex(repo_index
, 0);
70 cl_assert(strcmp(conflict_name
->ancestor
, "ancestor") == 0);
71 cl_assert(strcmp(conflict_name
->ours
, "ours") == 0);
72 cl_assert(strcmp(conflict_name
->theirs
, "theirs") == 0);
74 conflict_name
= git_index_name_get_byindex(repo_index
, 1);
75 cl_assert(strcmp(conflict_name
->ancestor
, "ancestor2") == 0);
76 cl_assert(strcmp(conflict_name
->ours
, "ours2") == 0);
77 cl_assert(conflict_name
->theirs
== NULL
);
79 conflict_name
= git_index_name_get_byindex(repo_index
, 2);
80 cl_assert(strcmp(conflict_name
->ancestor
, "ancestor3") == 0);
81 cl_assert(conflict_name
->ours
== NULL
);
82 cl_assert(strcmp(conflict_name
->theirs
, "theirs3") == 0);
85 void test_index_names__cleaned_on_reset_hard(void)
89 cl_git_pass(git_revparse_single(&target
, repo
, "3a34580"));
91 test_index_names__add();
92 cl_git_pass(git_reset(repo
, target
, GIT_RESET_HARD
, NULL
, NULL
, NULL
));
93 cl_assert(git_index_name_entrycount(repo_index
) == 0);
95 git_object_free(target
);
98 void test_index_names__cleaned_on_reset_mixed(void)
102 cl_git_pass(git_revparse_single(&target
, repo
, "3a34580"));
104 test_index_names__add();
105 cl_git_pass(git_reset(repo
, target
, GIT_RESET_MIXED
, NULL
, NULL
, NULL
));
106 cl_assert(git_index_name_entrycount(repo_index
) == 0);
108 git_object_free(target
);
111 void test_index_names__cleaned_on_checkout_tree(void)
115 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
117 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_UPDATE_ONLY
;
119 test_index_names__add();
120 git_reference_name_to_id(&oid
, repo
, "refs/heads/master");
121 git_object_lookup(&obj
, repo
, &oid
, GIT_OBJ_ANY
);
122 git_checkout_tree(repo
, obj
, &opts
);
123 cl_assert_equal_sz(0, git_index_name_entrycount(repo_index
));
125 git_object_free(obj
);
128 void test_index_names__cleaned_on_checkout_head(void)
130 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
132 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_UPDATE_ONLY
;
134 test_index_names__add();
135 git_checkout_head(repo
, &opts
);
136 cl_assert_equal_sz(0, git_index_name_entrycount(repo_index
));
139 void test_index_names__retained_on_checkout_index(void)
141 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
143 opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
| GIT_CHECKOUT_UPDATE_ONLY
;
145 test_index_names__add();
146 git_checkout_index(repo
, repo_index
, &opts
);
147 cl_assert(git_index_name_entrycount(repo_index
) > 0);