]> git.proxmox.com Git - libgit2.git/blob - tests/submodule/modify.c
7e7f0ca15923fdf27c23d3c6b3a3ee801deb18b7
[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 #include "config/config_helpers.h"
6
7 static git_repository *g_repo = NULL;
8
9 #define SM_LIBGIT2_URL "https://github.com/libgit2/libgit2.git"
10 #define SM_LIBGIT2_BRANCH "github-branch"
11 #define SM_LIBGIT2 "sm_libgit2"
12
13 void test_submodule_modify__initialize(void)
14 {
15 g_repo = setup_fixture_submod2();
16 }
17
18 static int delete_one_config(const git_config_entry *entry, void *payload)
19 {
20 git_config *cfg = payload;
21 return git_config_delete_entry(cfg, entry->name);
22 }
23
24 static int init_one_submodule(
25 git_submodule *sm, const char *name, void *payload)
26 {
27 GIT_UNUSED(name);
28 GIT_UNUSED(payload);
29 return git_submodule_init(sm, false);
30 }
31
32 void test_submodule_modify__init(void)
33 {
34 git_config *cfg;
35 const char *str;
36
37 /* erase submodule data from .git/config */
38 cl_git_pass(git_repository_config(&cfg, g_repo));
39 cl_git_pass(
40 git_config_foreach_match(cfg, "submodule\\..*", delete_one_config, cfg));
41 git_config_free(cfg);
42
43 /* confirm no submodule data in config */
44 cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
45 cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string(&str, cfg, "submodule.sm_unchanged.url"));
46 cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string(&str, cfg, "submodule.sm_changed_head.url"));
47 cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string(&str, cfg, "submodule.sm_added_and_uncommited.url"));
48 git_config_free(cfg);
49
50 /* call init and see that settings are copied */
51 cl_git_pass(git_submodule_foreach(g_repo, init_one_submodule, NULL));
52
53 /* confirm submodule data in config */
54 cl_git_pass(git_repository_config_snapshot(&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
64 static 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
72 static void assert_submodule_url_is_synced(
73 git_submodule *sm, const char *parent_key, const char *child_key)
74 {
75 git_repository *smrepo;
76
77 assert_config_entry_value(g_repo, parent_key, git_submodule_url(sm));
78
79 cl_git_pass(git_submodule_open(&smrepo, sm));
80 assert_config_entry_value(smrepo, child_key, git_submodule_url(sm));
81 git_repository_free(smrepo);
82 }
83
84 void test_submodule_modify__sync(void)
85 {
86 git_submodule *sm1, *sm2, *sm3;
87 git_config *cfg;
88 const char *str;
89
90 #define SM1 "sm_unchanged"
91 #define SM2 "sm_changed_head"
92 #define SM3 "sm_added_and_uncommited"
93
94 /* look up some submodules */
95 cl_git_pass(git_submodule_lookup(&sm1, g_repo, SM1));
96 cl_git_pass(git_submodule_lookup(&sm2, g_repo, SM2));
97 cl_git_pass(git_submodule_lookup(&sm3, g_repo, SM3));
98
99 /* At this point, the .git/config URLs for the submodules have
100 * not be rewritten with the absolute paths (although the
101 * .gitmodules have. Let's confirm that they DO NOT match
102 * yet, then we can do a sync to make them match...
103 */
104
105 /* check submodule info does not match before sync */
106 cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
107 cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM1".url"));
108 cl_assert(strcmp(git_submodule_url(sm1), str) != 0);
109 cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM2".url"));
110 cl_assert(strcmp(git_submodule_url(sm2), str) != 0);
111 cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM3".url"));
112 cl_assert(strcmp(git_submodule_url(sm3), str) != 0);
113 git_config_free(cfg);
114
115 /* sync all the submodules */
116 cl_git_pass(git_submodule_foreach(g_repo, sync_one_submodule, NULL));
117
118 /* check that submodule config is updated */
119 assert_submodule_url_is_synced(
120 sm1, "submodule."SM1".url", "remote.origin.url");
121 assert_submodule_url_is_synced(
122 sm2, "submodule."SM2".url", "remote.origin.url");
123 assert_submodule_url_is_synced(
124 sm3, "submodule."SM3".url", "remote.origin.url");
125
126 git_submodule_free(sm1);
127 git_submodule_free(sm2);
128 git_submodule_free(sm3);
129 }
130
131 static void assert_ignore_change(git_submodule_ignore_t ignore)
132 {
133 git_submodule *sm;
134
135 cl_git_pass(git_submodule_set_ignore(g_repo, "sm_changed_head", ignore));
136
137 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
138 cl_assert_equal_i(ignore, git_submodule_ignore(sm));
139 git_submodule_free(sm);
140 }
141
142 void test_submodule_modify__set_ignore(void)
143 {
144 assert_ignore_change(GIT_SUBMODULE_IGNORE_UNTRACKED);
145 assert_ignore_change(GIT_SUBMODULE_IGNORE_NONE);
146 assert_ignore_change(GIT_SUBMODULE_IGNORE_ALL);
147 }
148
149 static void assert_update_change(git_submodule_update_t update)
150 {
151 git_submodule *sm;
152
153 cl_git_pass(git_submodule_set_update(g_repo, "sm_changed_head", update));
154
155 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
156 cl_assert_equal_i(update, git_submodule_update_strategy(sm));
157 git_submodule_free(sm);
158 }
159
160 void test_submodule_modify__set_update(void)
161 {
162 assert_update_change(GIT_SUBMODULE_UPDATE_REBASE);
163 assert_update_change(GIT_SUBMODULE_UPDATE_NONE);
164 assert_update_change(GIT_SUBMODULE_UPDATE_CHECKOUT);
165 }
166
167 static void assert_recurse_change(git_submodule_recurse_t recurse)
168 {
169 git_submodule *sm;
170
171 cl_git_pass(git_submodule_set_fetch_recurse_submodules(g_repo, "sm_changed_head", recurse));
172
173 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
174 cl_assert_equal_i(recurse, git_submodule_fetch_recurse_submodules(sm));
175 git_submodule_free(sm);
176 }
177
178 void test_submodule_modify__set_fetch_recurse_submodules(void)
179 {
180 assert_recurse_change(GIT_SUBMODULE_RECURSE_YES);
181 assert_recurse_change(GIT_SUBMODULE_RECURSE_NO);
182 assert_recurse_change(GIT_SUBMODULE_RECURSE_ONDEMAND);
183 }
184
185 void test_submodule_modify__set_branch(void)
186 {
187 git_submodule *sm;
188
189 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
190 cl_assert(git_submodule_branch(sm) == NULL);
191 git_submodule_free(sm);
192
193 cl_git_pass(git_submodule_set_branch(g_repo, "sm_changed_head", SM_LIBGIT2_BRANCH));
194 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
195 cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm));
196 git_submodule_free(sm);
197
198 cl_git_pass(git_submodule_set_branch(g_repo, "sm_changed_head", NULL));
199 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
200 cl_assert(git_submodule_branch(sm) == NULL);
201 git_submodule_free(sm);
202 }
203
204 void test_submodule_modify__set_url(void)
205 {
206 git_submodule *sm;
207
208 cl_git_pass(git_submodule_set_url(g_repo, "sm_changed_head", SM_LIBGIT2_URL));
209 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
210 cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm));
211 git_submodule_free(sm);
212 }
213
214 void test_submodule_modify__set_relative_url(void)
215 {
216 git_str path = GIT_STR_INIT;
217 git_repository *repo;
218 git_submodule *sm;
219
220 cl_git_pass(git_submodule_set_url(g_repo, SM1, "../relative-url"));
221 cl_git_pass(git_submodule_lookup(&sm, g_repo, SM1));
222 cl_git_pass(git_submodule_sync(sm));
223 cl_git_pass(git_submodule_open(&repo, sm));
224
225 cl_git_pass(git_str_joinpath(&path, clar_sandbox_path(), "relative-url"));
226
227 assert_config_entry_value(g_repo, "submodule."SM1".url", path.ptr);
228 assert_config_entry_value(repo, "remote.origin.url", path.ptr);
229
230 git_repository_free(repo);
231 git_submodule_free(sm);
232 git_str_dispose(&path);
233 }