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