1 #include "clar_libgit2.h"
4 #include "submodule_helpers.h"
5 #include "config/config_helpers.h"
7 static git_repository
*g_repo
= NULL
;
9 #define SM_LIBGIT2_URL "https://github.com/libgit2/libgit2.git"
10 #define SM_LIBGIT2 "sm_libgit2"
12 void test_submodule_modify__initialize(void)
14 g_repo
= setup_fixture_submod2();
17 static int delete_one_config(const git_config_entry
*entry
, void *payload
)
19 git_config
*cfg
= payload
;
20 return git_config_delete_entry(cfg
, entry
->name
);
23 static int init_one_submodule(
24 git_submodule
*sm
, const char *name
, void *payload
)
28 return git_submodule_init(sm
, false);
31 void test_submodule_modify__init(void)
36 /* erase submodule data from .git/config */
37 cl_git_pass(git_repository_config(&cfg
, g_repo
));
39 git_config_foreach_match(cfg
, "submodule\\..*", delete_one_config
, cfg
));
42 /* confirm no submodule data in config */
43 cl_git_pass(git_repository_config(&cfg
, g_repo
));
44 cl_git_fail(git_config_get_string(&str
, cfg
, "submodule.sm_unchanged.url"));
45 cl_git_fail(git_config_get_string(&str
, cfg
, "submodule.sm_changed_head.url"));
46 cl_git_fail(git_config_get_string(&str
, cfg
, "submodule.sm_added_and_uncommited.url"));
49 /* call init and see that settings are copied */
50 cl_git_pass(git_submodule_foreach(g_repo
, init_one_submodule
, NULL
));
52 git_submodule_reload_all(g_repo
, 1);
54 /* confirm submodule data in config */
55 cl_git_pass(git_repository_config_snapshot(&cfg
, g_repo
));
56 cl_git_pass(git_config_get_string(&str
, cfg
, "submodule.sm_unchanged.url"));
57 cl_assert(git__suffixcmp(str
, "/submod2_target") == 0);
58 cl_git_pass(git_config_get_string(&str
, cfg
, "submodule.sm_changed_head.url"));
59 cl_assert(git__suffixcmp(str
, "/submod2_target") == 0);
60 cl_git_pass(git_config_get_string(&str
, cfg
, "submodule.sm_added_and_uncommited.url"));
61 cl_assert(git__suffixcmp(str
, "/submod2_target") == 0);
65 static int sync_one_submodule(
66 git_submodule
*sm
, const char *name
, void *payload
)
70 return git_submodule_sync(sm
);
73 static void assert_submodule_url_is_synced(
74 git_submodule
*sm
, const char *parent_key
, const char *child_key
)
76 git_repository
*smrepo
;
78 assert_config_entry_value(g_repo
, parent_key
, git_submodule_url(sm
));
80 cl_git_pass(git_submodule_open(&smrepo
, sm
));
81 assert_config_entry_value(smrepo
, child_key
, git_submodule_url(sm
));
82 git_repository_free(smrepo
);
85 void test_submodule_modify__sync(void)
87 git_submodule
*sm1
, *sm2
, *sm3
;
91 #define SM1 "sm_unchanged"
92 #define SM2 "sm_changed_head"
93 #define SM3 "sm_added_and_uncommited"
95 /* look up some submodules */
96 cl_git_pass(git_submodule_lookup(&sm1
, g_repo
, SM1
));
97 cl_git_pass(git_submodule_lookup(&sm2
, g_repo
, SM2
));
98 cl_git_pass(git_submodule_lookup(&sm3
, g_repo
, SM3
));
100 /* At this point, the .git/config URLs for the submodules have
101 * not be rewritten with the absolute paths (although the
102 * .gitmodules have. Let's confirm that they DO NOT match
103 * yet, then we can do a sync to make them match...
106 /* check submodule info does not match before sync */
107 cl_git_pass(git_repository_config_snapshot(&cfg
, g_repo
));
108 cl_git_pass(git_config_get_string(&str
, cfg
, "submodule."SM1
".url"));
109 cl_assert(strcmp(git_submodule_url(sm1
), str
) != 0);
110 cl_git_pass(git_config_get_string(&str
, cfg
, "submodule."SM2
".url"));
111 cl_assert(strcmp(git_submodule_url(sm2
), str
) != 0);
112 cl_git_pass(git_config_get_string(&str
, cfg
, "submodule."SM3
".url"));
113 cl_assert(strcmp(git_submodule_url(sm3
), str
) != 0);
114 git_config_free(cfg
);
116 /* sync all the submodules */
117 cl_git_pass(git_submodule_foreach(g_repo
, sync_one_submodule
, NULL
));
119 /* check that submodule config is updated */
120 assert_submodule_url_is_synced(
121 sm1
, "submodule."SM1
".url", "remote.origin.url");
122 assert_submodule_url_is_synced(
123 sm2
, "submodule."SM2
".url", "remote.origin.url");
124 assert_submodule_url_is_synced(
125 sm3
, "submodule."SM3
".url", "remote.origin.url");
127 git_submodule_free(sm1
);
128 git_submodule_free(sm2
);
129 git_submodule_free(sm3
);
132 void test_submodule_modify__edit_and_save(void)
134 git_submodule
*sm1
, *sm2
;
136 git_submodule_ignore_t old_ignore
;
137 git_submodule_update_t old_update
;
139 git_submodule_recurse_t old_fetchrecurse
;
141 cl_git_pass(git_submodule_lookup(&sm1
, g_repo
, "sm_changed_head"));
143 old_url
= git__strdup(git_submodule_url(sm1
));
145 /* modify properties of submodule */
146 cl_git_pass(git_submodule_set_url(sm1
, SM_LIBGIT2_URL
));
147 old_ignore
= git_submodule_set_ignore(sm1
, GIT_SUBMODULE_IGNORE_UNTRACKED
);
148 old_update
= git_submodule_set_update(sm1
, GIT_SUBMODULE_UPDATE_REBASE
);
149 old_fetchrecurse
= git_submodule_set_fetch_recurse_submodules(
150 sm1
, GIT_SUBMODULE_RECURSE_YES
);
152 cl_assert_equal_s(SM_LIBGIT2_URL
, git_submodule_url(sm1
));
154 GIT_SUBMODULE_IGNORE_UNTRACKED
, git_submodule_ignore(sm1
));
156 GIT_SUBMODULE_UPDATE_REBASE
, git_submodule_update_strategy(sm1
));
158 GIT_SUBMODULE_RECURSE_YES
, git_submodule_fetch_recurse_submodules(sm1
));
160 /* revert without saving (and confirm setters return old value) */
161 cl_git_pass(git_submodule_set_url(sm1
, old_url
));
163 GIT_SUBMODULE_IGNORE_UNTRACKED
,
164 git_submodule_set_ignore(sm1
, GIT_SUBMODULE_IGNORE_RESET
));
166 GIT_SUBMODULE_UPDATE_REBASE
,
167 git_submodule_set_update(sm1
, GIT_SUBMODULE_UPDATE_RESET
));
169 GIT_SUBMODULE_RECURSE_YES
, git_submodule_set_fetch_recurse_submodules(
170 sm1
, GIT_SUBMODULE_RECURSE_RESET
));
172 /* check that revert was successful */
173 cl_assert_equal_s(old_url
, git_submodule_url(sm1
));
174 cl_assert_equal_i((int)old_ignore
, (int)git_submodule_ignore(sm1
));
175 cl_assert_equal_i((int)old_update
, (int)git_submodule_update_strategy(sm1
));
177 old_fetchrecurse
, git_submodule_fetch_recurse_submodules(sm1
));
179 /* modify properties of submodule (again) */
180 cl_git_pass(git_submodule_set_url(sm1
, SM_LIBGIT2_URL
));
181 git_submodule_set_ignore(sm1
, GIT_SUBMODULE_IGNORE_UNTRACKED
);
182 git_submodule_set_update(sm1
, GIT_SUBMODULE_UPDATE_REBASE
);
183 git_submodule_set_fetch_recurse_submodules(sm1
, GIT_SUBMODULE_RECURSE_YES
);
186 cl_git_pass(git_submodule_save(sm1
));
188 /* attempt to "revert" values */
189 git_submodule_set_ignore(sm1
, GIT_SUBMODULE_IGNORE_RESET
);
190 git_submodule_set_update(sm1
, GIT_SUBMODULE_UPDATE_RESET
);
192 /* but ignore and update should NOT revert because the RESET
193 * should now be the newly saved value...
196 (int)GIT_SUBMODULE_IGNORE_UNTRACKED
, (int)git_submodule_ignore(sm1
));
198 (int)GIT_SUBMODULE_UPDATE_REBASE
, (int)git_submodule_update_strategy(sm1
));
199 cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES
, git_submodule_fetch_recurse_submodules(sm1
));
201 /* call reload and check that the new values are loaded */
202 cl_git_pass(git_submodule_reload(sm1
, 0));
204 cl_assert_equal_s(SM_LIBGIT2_URL
, git_submodule_url(sm1
));
206 (int)GIT_SUBMODULE_IGNORE_UNTRACKED
, (int)git_submodule_ignore(sm1
));
208 (int)GIT_SUBMODULE_UPDATE_REBASE
, (int)git_submodule_update_strategy(sm1
));
209 cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES
, git_submodule_fetch_recurse_submodules(sm1
));
211 /* open a second copy of the repo and compare submodule */
212 cl_git_pass(git_repository_open(&r2
, "submod2"));
213 cl_git_pass(git_submodule_lookup(&sm2
, r2
, "sm_changed_head"));
215 cl_assert_equal_s(SM_LIBGIT2_URL
, git_submodule_url(sm2
));
217 GIT_SUBMODULE_IGNORE_UNTRACKED
, git_submodule_ignore(sm2
));
219 GIT_SUBMODULE_UPDATE_REBASE
, git_submodule_update_strategy(sm2
));
221 GIT_SUBMODULE_RECURSE_NO
, git_submodule_fetch_recurse_submodules(sm2
));
223 /* set fetchRecurseSubmodules on-demand */
224 cl_git_pass(git_submodule_reload(sm1
, 0));
225 git_submodule_set_fetch_recurse_submodules(sm1
, GIT_SUBMODULE_RECURSE_ONDEMAND
);
227 GIT_SUBMODULE_RECURSE_ONDEMAND
, git_submodule_fetch_recurse_submodules(sm1
));
229 cl_git_pass(git_submodule_save(sm1
));
230 cl_git_pass(git_submodule_reload(sm1
, 0));
232 GIT_SUBMODULE_RECURSE_ONDEMAND
, git_submodule_fetch_recurse_submodules(sm1
));
234 git_submodule_free(sm1
);
235 git_submodule_free(sm2
);
236 git_repository_free(r2
);