]> git.proxmox.com Git - libgit2.git/blob - tests/submodule/modify.c
Merge pull request #1962 from libgit2/rename-tests
[libgit2.git] / tests / submodule / modify.c
1 #include "clar_libgit2.h"
2 #include "posix.h"
3 #include "path.h"
4 #include "submodule_helpers.h"
5
6 static git_repository *g_repo = NULL;
7
8 #define SM_LIBGIT2_URL "https://github.com/libgit2/libgit2.git"
9 #define SM_LIBGIT2 "sm_libgit2"
10 #define SM_LIBGIT2B "sm_libgit2b"
11
12 void test_submodule_modify__initialize(void)
13 {
14 g_repo = setup_fixture_submod2();
15 }
16
17 void test_submodule_modify__add(void)
18 {
19 git_submodule *sm;
20 git_config *cfg;
21 const char *s;
22
23 /* re-add existing submodule */
24 cl_assert(
25 git_submodule_add_setup(NULL, g_repo, "whatever", "sm_unchanged", 1) ==
26 GIT_EEXISTS );
27
28 /* add a submodule using a gitlink */
29
30 cl_git_pass(
31 git_submodule_add_setup(&sm, g_repo, SM_LIBGIT2_URL, SM_LIBGIT2, 1)
32 );
33
34 cl_assert(git_path_isfile("submod2/" SM_LIBGIT2 "/.git"));
35
36 cl_assert(git_path_isdir("submod2/.git/modules"));
37 cl_assert(git_path_isdir("submod2/.git/modules/" SM_LIBGIT2));
38 cl_assert(git_path_isfile("submod2/.git/modules/" SM_LIBGIT2 "/HEAD"));
39
40 cl_git_pass(git_repository_config(&cfg, g_repo));
41 cl_git_pass(
42 git_config_get_string(&s, cfg, "submodule." SM_LIBGIT2 ".url"));
43 cl_assert_equal_s(s, SM_LIBGIT2_URL);
44 git_config_free(cfg);
45
46 /* add a submodule not using a gitlink */
47
48 cl_git_pass(
49 git_submodule_add_setup(&sm, g_repo, SM_LIBGIT2_URL, SM_LIBGIT2B, 0)
50 );
51
52 cl_assert(git_path_isdir("submod2/" SM_LIBGIT2B "/.git"));
53 cl_assert(git_path_isfile("submod2/" SM_LIBGIT2B "/.git/HEAD"));
54 cl_assert(!git_path_exists("submod2/.git/modules/" SM_LIBGIT2B));
55
56 cl_git_pass(git_repository_config(&cfg, g_repo));
57 cl_git_pass(
58 git_config_get_string(&s, cfg, "submodule." SM_LIBGIT2B ".url"));
59 cl_assert_equal_s(s, SM_LIBGIT2_URL);
60 git_config_free(cfg);
61 }
62
63 static int delete_one_config(const git_config_entry *entry, void *payload)
64 {
65 git_config *cfg = payload;
66 return git_config_delete_entry(cfg, entry->name);
67 }
68
69 static int init_one_submodule(
70 git_submodule *sm, const char *name, void *payload)
71 {
72 GIT_UNUSED(name);
73 GIT_UNUSED(payload);
74 return git_submodule_init(sm, false);
75 }
76
77 void test_submodule_modify__init(void)
78 {
79 git_config *cfg;
80 const char *str;
81
82 /* erase submodule data from .git/config */
83 cl_git_pass(git_repository_config(&cfg, g_repo));
84 cl_git_pass(
85 git_config_foreach_match(cfg, "submodule\\..*", delete_one_config, cfg));
86 git_config_free(cfg);
87
88 /* confirm no submodule data in config */
89 cl_git_pass(git_repository_config(&cfg, g_repo));
90 cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_unchanged.url"));
91 cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_changed_head.url"));
92 cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_added_and_uncommited.url"));
93 git_config_free(cfg);
94
95 /* call init and see that settings are copied */
96 cl_git_pass(git_submodule_foreach(g_repo, init_one_submodule, NULL));
97
98 git_submodule_reload_all(g_repo);
99
100 /* confirm submodule data in config */
101 cl_git_pass(git_repository_config(&cfg, g_repo));
102 cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_unchanged.url"));
103 cl_assert(git__suffixcmp(str, "/submod2_target") == 0);
104 cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_changed_head.url"));
105 cl_assert(git__suffixcmp(str, "/submod2_target") == 0);
106 cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_added_and_uncommited.url"));
107 cl_assert(git__suffixcmp(str, "/submod2_target") == 0);
108 git_config_free(cfg);
109 }
110
111 static int sync_one_submodule(
112 git_submodule *sm, const char *name, void *payload)
113 {
114 GIT_UNUSED(name);
115 GIT_UNUSED(payload);
116 return git_submodule_sync(sm);
117 }
118
119 void test_submodule_modify__sync(void)
120 {
121 git_submodule *sm1, *sm2, *sm3;
122 git_config *cfg;
123 const char *str;
124
125 #define SM1 "sm_unchanged"
126 #define SM2 "sm_changed_head"
127 #define SM3 "sm_added_and_uncommited"
128
129 /* look up some submodules */
130 cl_git_pass(git_submodule_lookup(&sm1, g_repo, SM1));
131 cl_git_pass(git_submodule_lookup(&sm2, g_repo, SM2));
132 cl_git_pass(git_submodule_lookup(&sm3, g_repo, SM3));
133
134 /* At this point, the .git/config URLs for the submodules have
135 * not be rewritten with the absolute paths (although the
136 * .gitmodules have. Let's confirm that they DO NOT match
137 * yet, then we can do a sync to make them match...
138 */
139
140 /* check submodule info does not match before sync */
141 cl_git_pass(git_repository_config(&cfg, g_repo));
142 cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM1".url"));
143 cl_assert(strcmp(git_submodule_url(sm1), str) != 0);
144 cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM2".url"));
145 cl_assert(strcmp(git_submodule_url(sm2), str) != 0);
146 cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM3".url"));
147 cl_assert(strcmp(git_submodule_url(sm3), str) != 0);
148 git_config_free(cfg);
149
150 /* sync all the submodules */
151 cl_git_pass(git_submodule_foreach(g_repo, sync_one_submodule, NULL));
152
153 /* check that submodule config is updated */
154 cl_git_pass(git_repository_config(&cfg, g_repo));
155 cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM1".url"));
156 cl_assert_equal_s(git_submodule_url(sm1), str);
157 cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM2".url"));
158 cl_assert_equal_s(git_submodule_url(sm2), str);
159 cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM3".url"));
160 cl_assert_equal_s(git_submodule_url(sm3), str);
161 git_config_free(cfg);
162 }
163
164 void test_submodule_modify__edit_and_save(void)
165 {
166 git_submodule *sm1, *sm2;
167 char *old_url;
168 git_submodule_ignore_t old_ignore;
169 git_submodule_update_t old_update;
170 git_repository *r2;
171 int old_fetchrecurse;
172
173 cl_git_pass(git_submodule_lookup(&sm1, g_repo, "sm_changed_head"));
174
175 old_url = git__strdup(git_submodule_url(sm1));
176
177 /* modify properties of submodule */
178 cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
179 old_ignore = git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED);
180 old_update = git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
181 old_fetchrecurse = git_submodule_set_fetch_recurse_submodules(sm1, 1);
182
183 cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
184 cl_assert_equal_i(
185 (int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
186 cl_assert_equal_i(
187 (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1));
188 cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm1));
189
190 /* revert without saving (and confirm setters return old value) */
191 cl_git_pass(git_submodule_set_url(sm1, old_url));
192 cl_assert_equal_i(
193 (int)GIT_SUBMODULE_IGNORE_UNTRACKED,
194 (int)git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_RESET));
195 cl_assert_equal_i(
196 (int)GIT_SUBMODULE_UPDATE_REBASE,
197 (int)git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET));
198 cl_assert_equal_i(
199 1, git_submodule_set_fetch_recurse_submodules(sm1, old_fetchrecurse));
200
201 /* check that revert was successful */
202 cl_assert_equal_s(old_url, git_submodule_url(sm1));
203 cl_assert_equal_i((int)old_ignore, (int)git_submodule_ignore(sm1));
204 cl_assert_equal_i((int)old_update, (int)git_submodule_update(sm1));
205 cl_assert_equal_i(
206 old_fetchrecurse, git_submodule_fetch_recurse_submodules(sm1));
207
208 /* modify properties of submodule (again) */
209 cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
210 git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED);
211 git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
212 git_submodule_set_fetch_recurse_submodules(sm1, 1);
213
214 /* call save */
215 cl_git_pass(git_submodule_save(sm1));
216
217 /* attempt to "revert" values */
218 git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_RESET);
219 git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET);
220
221 /* but ignore and update should NOT revert because the RESET
222 * should now be the newly saved value...
223 */
224 cl_assert_equal_i(
225 (int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
226 cl_assert_equal_i(
227 (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1));
228 cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm1));
229
230 /* call reload and check that the new values are loaded */
231 cl_git_pass(git_submodule_reload(sm1));
232
233 cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
234 cl_assert_equal_i(
235 (int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
236 cl_assert_equal_i(
237 (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1));
238 cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm1));
239
240 /* open a second copy of the repo and compare submodule */
241 cl_git_pass(git_repository_open(&r2, "submod2"));
242 cl_git_pass(git_submodule_lookup(&sm2, r2, "sm_changed_head"));
243
244 cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm2));
245 cl_assert_equal_i(
246 (int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm2));
247 cl_assert_equal_i(
248 (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm2));
249 cl_assert_equal_i(1, git_submodule_fetch_recurse_submodules(sm2));
250
251 git_repository_free(r2);
252 git__free(old_url);
253 }