]> git.proxmox.com Git - libgit2.git/blob - tests/index/names.c
Merge pull request #2600 from libgit2/cmn/embed-ssh
[libgit2.git] / tests / index / names.c
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
14 void test_index_names__initialize(void)
15 {
16 repo = cl_git_sandbox_init("mergedrepo");
17 git_repository_index(&repo_index, repo);
18 }
19
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
28 void test_index_names__add(void)
29 {
30 const git_index_name_entry *conflict_name;
31
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"));
35
36 cl_assert(git_index_name_entrycount(repo_index) == 3);
37
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);
42
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);
47
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);
52 }
53
54 void test_index_names__roundtrip(void)
55 {
56 const git_index_name_entry *conflict_name;
57
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"));
61
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);
65
66 cl_git_pass(git_index_read(repo_index, true));
67 cl_assert(git_index_name_entrycount(repo_index) == 3);
68
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);
73
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);
78
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);
83 }
84
85 void test_index_names__cleaned_on_reset_hard(void)
86 {
87 git_object *target;
88
89 cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
90
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);
94
95 git_object_free(target);
96 }
97
98 void test_index_names__cleaned_on_reset_mixed(void)
99 {
100 git_object *target;
101
102 cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
103
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);
107
108 git_object_free(target);
109 }
110
111 void test_index_names__cleaned_on_checkout_tree(void)
112 {
113 git_oid oid;
114 git_object *obj;
115 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
116
117 opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
118
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));
124
125 git_object_free(obj);
126 }
127
128 void test_index_names__cleaned_on_checkout_head(void)
129 {
130 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
131
132 opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
133
134 test_index_names__add();
135 git_checkout_head(repo, &opts);
136 cl_assert_equal_sz(0, git_index_name_entrycount(repo_index));
137 }
138
139 void test_index_names__retained_on_checkout_index(void)
140 {
141 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
142
143 opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
144
145 test_index_names__add();
146 git_checkout_index(repo, repo_index, &opts);
147 cl_assert(git_index_name_entrycount(repo_index) > 0);
148 }