]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/submodule/modify.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / submodule / modify.c
CommitLineData
aa13bf05
RB
1#include "clar_libgit2.h"
2#include "posix.h"
3#include "path.h"
4#include "submodule_helpers.h"
9a97f49e 5#include "config/config_helpers.h"
aa13bf05
RB
6
7static git_repository *g_repo = NULL;
8
129788a6
PS
9#define SM_LIBGIT2_URL "https://github.com/libgit2/libgit2.git"
10#define SM_LIBGIT2_BRANCH "github-branch"
11#define SM_LIBGIT2 "sm_libgit2"
12d4ed4d 12
aa13bf05
RB
13void test_submodule_modify__initialize(void)
14{
14997dc5 15 g_repo = setup_fixture_submod2();
aa13bf05
RB
16}
17
a1abe66a 18static int delete_one_config(const git_config_entry *entry, void *payload)
aa13bf05
RB
19{
20 git_config *cfg = payload;
54b2a37a 21 return git_config_delete_entry(cfg, entry->name);
aa13bf05
RB
22}
23
24static 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
32void 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 */
4536574a
CMN
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"));
aa13bf05
RB
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
aa13bf05 53 /* confirm submodule data in config */
9a97f49e 54 cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
aa13bf05
RB
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{
eedeeb9e
RB
75 git_repository *smrepo;
76
9a97f49e 77 assert_config_entry_value(g_repo, parent_key, git_submodule_url(sm));
eedeeb9e
RB
78
79 cl_git_pass(git_submodule_open(&smrepo, sm));
9a97f49e 80 assert_config_entry_value(smrepo, child_key, git_submodule_url(sm));
eedeeb9e
RB
81 git_repository_free(smrepo);
82}
83
aa13bf05
RB
84void 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 */
9a97f49e 106 cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
aa13bf05
RB
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 */
eedeeb9e 119 assert_submodule_url_is_synced(
e6903ea2 120 sm1, "submodule."SM1".url", "remote.origin.url");
eedeeb9e 121 assert_submodule_url_is_synced(
e6903ea2 122 sm2, "submodule."SM2".url", "remote.origin.url");
eedeeb9e 123 assert_submodule_url_is_synced(
e6903ea2 124 sm3, "submodule."SM3".url", "remote.origin.url");
a15c7802
RB
125
126 git_submodule_free(sm1);
127 git_submodule_free(sm2);
128 git_submodule_free(sm3);
aa13bf05
RB
129}
130
e579e0f7 131static void assert_ignore_change(git_submodule_ignore_t ignore)
5a9fc6c8
CMN
132{
133 git_submodule *sm;
134
04963300 135 cl_git_pass(git_submodule_set_ignore(g_repo, "sm_changed_head", ignore));
5a9fc6c8
CMN
136
137 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
04963300 138 cl_assert_equal_i(ignore, git_submodule_ignore(sm));
5a9fc6c8
CMN
139 git_submodule_free(sm);
140}
141
04963300
CMN
142void 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
e579e0f7 149static void assert_update_change(git_submodule_update_t update)
e8a39f8e
CMN
150{
151 git_submodule *sm;
152
04963300 153 cl_git_pass(git_submodule_set_update(g_repo, "sm_changed_head", update));
e8a39f8e
CMN
154
155 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
04963300 156 cl_assert_equal_i(update, git_submodule_update_strategy(sm));
e8a39f8e
CMN
157 git_submodule_free(sm);
158}
159
04963300
CMN
160void 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
e579e0f7 167static void assert_recurse_change(git_submodule_recurse_t recurse)
4e636423
CMN
168{
169 git_submodule *sm;
170
04963300 171 cl_git_pass(git_submodule_set_fetch_recurse_submodules(g_repo, "sm_changed_head", recurse));
4e636423
CMN
172
173 cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
04963300 174 cl_assert_equal_i(recurse, git_submodule_fetch_recurse_submodules(sm));
4e636423 175 git_submodule_free(sm);
04963300 176}
4e636423 177
04963300
CMN
178void 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);
4e636423
CMN
183}
184
486ba4cd
CMN
185void 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
d6073b30 204void test_submodule_modify__set_url(void)
aa13bf05 205{
d6073b30 206 git_submodule *sm;
aa13bf05 207
d6073b30
CMN
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);
aa13bf05 212}
22a2d3d5
UG
213
214void test_submodule_modify__set_relative_url(void)
215{
e579e0f7 216 git_str path = GIT_STR_INIT;
22a2d3d5
UG
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
e579e0f7 225 cl_git_pass(git_str_joinpath(&path, clar_sandbox_path(), "relative-url"));
22a2d3d5
UG
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);
e579e0f7 232 git_str_dispose(&path);
22a2d3d5 233}