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_BRANCH "github-branch"
11 #define SM_LIBGIT2 "sm_libgit2"
13 void test_submodule_modify__initialize(void)
15 g_repo
= setup_fixture_submod2();
18 static int delete_one_config(const git_config_entry
*entry
, void *payload
)
20 git_config
*cfg
= payload
;
21 return git_config_delete_entry(cfg
, entry
->name
);
24 static int init_one_submodule(
25 git_submodule
*sm
, const char *name
, void *payload
)
29 return git_submodule_init(sm
, false);
32 void test_submodule_modify__init(void)
37 /* erase submodule data from .git/config */
38 cl_git_pass(git_repository_config(&cfg
, g_repo
));
40 git_config_foreach_match(cfg
, "submodule\\..*", delete_one_config
, cfg
));
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"));
50 /* call init and see that settings are copied */
51 cl_git_pass(git_submodule_foreach(g_repo
, init_one_submodule
, NULL
));
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);
64 static int sync_one_submodule(
65 git_submodule
*sm
, const char *name
, void *payload
)
69 return git_submodule_sync(sm
);
72 static void assert_submodule_url_is_synced(
73 git_submodule
*sm
, const char *parent_key
, const char *child_key
)
75 git_repository
*smrepo
;
77 assert_config_entry_value(g_repo
, parent_key
, git_submodule_url(sm
));
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
);
84 void test_submodule_modify__sync(void)
86 git_submodule
*sm1
, *sm2
, *sm3
;
90 #define SM1 "sm_unchanged"
91 #define SM2 "sm_changed_head"
92 #define SM3 "sm_added_and_uncommited"
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
));
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...
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
);
115 /* sync all the submodules */
116 cl_git_pass(git_submodule_foreach(g_repo
, sync_one_submodule
, NULL
));
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");
126 git_submodule_free(sm1
);
127 git_submodule_free(sm2
);
128 git_submodule_free(sm3
);
131 static void assert_ignore_change(git_submodule_ignore_t ignore
)
135 cl_git_pass(git_submodule_set_ignore(g_repo
, "sm_changed_head", ignore
));
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
);
142 void test_submodule_modify__set_ignore(void)
144 assert_ignore_change(GIT_SUBMODULE_IGNORE_UNTRACKED
);
145 assert_ignore_change(GIT_SUBMODULE_IGNORE_NONE
);
146 assert_ignore_change(GIT_SUBMODULE_IGNORE_ALL
);
149 static void assert_update_change(git_submodule_update_t update
)
153 cl_git_pass(git_submodule_set_update(g_repo
, "sm_changed_head", update
));
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
);
160 void test_submodule_modify__set_update(void)
162 assert_update_change(GIT_SUBMODULE_UPDATE_REBASE
);
163 assert_update_change(GIT_SUBMODULE_UPDATE_NONE
);
164 assert_update_change(GIT_SUBMODULE_UPDATE_CHECKOUT
);
167 static void assert_recurse_change(git_submodule_recurse_t recurse
)
171 cl_git_pass(git_submodule_set_fetch_recurse_submodules(g_repo
, "sm_changed_head", recurse
));
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
);
178 void test_submodule_modify__set_fetch_recurse_submodules(void)
180 assert_recurse_change(GIT_SUBMODULE_RECURSE_YES
);
181 assert_recurse_change(GIT_SUBMODULE_RECURSE_NO
);
182 assert_recurse_change(GIT_SUBMODULE_RECURSE_ONDEMAND
);
185 void test_submodule_modify__set_branch(void)
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
);
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
);
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
);
204 void test_submodule_modify__set_url(void)
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
);
214 void test_submodule_modify__set_relative_url(void)
216 git_str path
= GIT_STR_INIT
;
217 git_repository
*repo
;
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
));
225 cl_git_pass(git_str_joinpath(&path
, clar_sandbox_path(), "relative-url"));
227 assert_config_entry_value(g_repo
, "submodule."SM1
".url", path
.ptr
);
228 assert_config_entry_value(repo
, "remote.origin.url", path
.ptr
);
230 git_repository_free(repo
);
231 git_submodule_free(sm
);
232 git_str_dispose(&path
);