modify.c 8.49 KB
Newer Older
Russell Belfer committed
1 2 3 4 5 6 7 8 9
#include "clar_libgit2.h"
#include "posix.h"
#include "path.h"
#include "submodule_helpers.h"

static git_repository *g_repo = NULL;

#define SM_LIBGIT2_URL "https://github.com/libgit2/libgit2.git"
#define SM_LIBGIT2     "sm_libgit2"
10

Russell Belfer committed
11 12
void test_submodule_modify__initialize(void)
{
13
	g_repo = setup_fixture_submod2();
Russell Belfer committed
14 15
}

16
static int delete_one_config(const git_config_entry *entry, void *payload)
Russell Belfer committed
17 18
{
	git_config *cfg = payload;
Ben Straub committed
19
	return git_config_delete_entry(cfg, entry->name);
Russell Belfer committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
}

static int init_one_submodule(
	git_submodule *sm, const char *name, void *payload)
{
	GIT_UNUSED(name);
	GIT_UNUSED(payload);
	return git_submodule_init(sm, false);
}

void test_submodule_modify__init(void)
{
	git_config *cfg;
	const char *str;

	/* erase submodule data from .git/config */
	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_pass(
		git_config_foreach_match(cfg, "submodule\\..*", delete_one_config, cfg));
	git_config_free(cfg);

	/* confirm no submodule data in config */
	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_unchanged.url"));
	cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_changed_head.url"));
	cl_git_fail(git_config_get_string(&str, cfg, "submodule.sm_added_and_uncommited.url"));
	git_config_free(cfg);

	/* call init and see that settings are copied */
	cl_git_pass(git_submodule_foreach(g_repo, init_one_submodule, NULL));

51
	git_submodule_reload_all(g_repo, 1);
Russell Belfer committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

	/* confirm submodule data in config */
	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_unchanged.url"));
	cl_assert(git__suffixcmp(str, "/submod2_target") == 0);
	cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_changed_head.url"));
	cl_assert(git__suffixcmp(str, "/submod2_target") == 0);
	cl_git_pass(git_config_get_string(&str, cfg, "submodule.sm_added_and_uncommited.url"));
	cl_assert(git__suffixcmp(str, "/submod2_target") == 0);
	git_config_free(cfg);
}

static int sync_one_submodule(
	git_submodule *sm, const char *name, void *payload)
{
	GIT_UNUSED(name);
	GIT_UNUSED(payload);
	return git_submodule_sync(sm);
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static void assert_submodule_url_is_synced(
	git_submodule *sm, const char *parent_key, const char *child_key)
{
	git_config *cfg;
	const char *str;
	git_repository *smrepo;

	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_pass(git_config_get_string(&str, cfg, parent_key));
	cl_assert_equal_s(git_submodule_url(sm), str);
	git_config_free(cfg);

	cl_git_pass(git_submodule_open(&smrepo, sm));
	cl_git_pass(git_repository_config(&cfg, smrepo));
	cl_git_pass(git_config_get_string(&str, cfg, child_key));
	cl_assert_equal_s(git_submodule_url(sm), str);
	git_config_free(cfg);
	git_repository_free(smrepo);
}

Russell Belfer committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
void test_submodule_modify__sync(void)
{
	git_submodule *sm1, *sm2, *sm3;
	git_config *cfg;
	const char *str;

#define SM1 "sm_unchanged"
#define SM2 "sm_changed_head"
#define SM3 "sm_added_and_uncommited"

	/* look up some submodules */
	cl_git_pass(git_submodule_lookup(&sm1, g_repo, SM1));
	cl_git_pass(git_submodule_lookup(&sm2, g_repo, SM2));
	cl_git_pass(git_submodule_lookup(&sm3, g_repo, SM3));

	/* At this point, the .git/config URLs for the submodules have
	 * not be rewritten with the absolute paths (although the
	 * .gitmodules have.  Let's confirm that they DO NOT match
	 * yet, then we can do a sync to make them match...
	 */

	/* check submodule info does not match before sync */
	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM1".url"));
	cl_assert(strcmp(git_submodule_url(sm1), str) != 0);
	cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM2".url"));
	cl_assert(strcmp(git_submodule_url(sm2), str) != 0);
	cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM3".url"));
	cl_assert(strcmp(git_submodule_url(sm3), str) != 0);
	git_config_free(cfg);

	/* sync all the submodules */
	cl_git_pass(git_submodule_foreach(g_repo, sync_one_submodule, NULL));

	/* check that submodule config is updated */
127 128 129 130 131 132
	assert_submodule_url_is_synced(
		sm1, "submodule."SM1".url", "branch.origin.remote");
	assert_submodule_url_is_synced(
		sm2, "submodule."SM2".url", "branch.origin.remote");
	assert_submodule_url_is_synced(
		sm3, "submodule."SM3".url", "branch.origin.remote");
133 134 135 136

	git_submodule_free(sm1);
	git_submodule_free(sm2);
	git_submodule_free(sm3);
Russell Belfer committed
137 138 139 140 141 142 143 144 145
}

void test_submodule_modify__edit_and_save(void)
{
	git_submodule *sm1, *sm2;
	char *old_url;
	git_submodule_ignore_t old_ignore;
	git_submodule_update_t old_update;
	git_repository *r2;
146
	git_submodule_recurse_t old_fetchrecurse;
Russell Belfer committed
147 148 149 150 151 152 153 154 155

	cl_git_pass(git_submodule_lookup(&sm1, g_repo, "sm_changed_head"));

	old_url = git__strdup(git_submodule_url(sm1));

	/* modify properties of submodule */
	cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
	old_ignore = git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED);
	old_update = git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
156 157
	old_fetchrecurse = git_submodule_set_fetch_recurse_submodules(
		sm1, GIT_SUBMODULE_RECURSE_YES);
Russell Belfer committed
158 159 160

	cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
	cl_assert_equal_i(
161
		GIT_SUBMODULE_IGNORE_UNTRACKED, git_submodule_ignore(sm1));
Russell Belfer committed
162
	cl_assert_equal_i(
163
		GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update_strategy(sm1));
164 165
	cl_assert_equal_i(
		GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
Russell Belfer committed
166 167 168 169

	/* revert without saving (and confirm setters return old value) */
	cl_git_pass(git_submodule_set_url(sm1, old_url));
	cl_assert_equal_i(
170 171
		GIT_SUBMODULE_IGNORE_UNTRACKED,
		git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_RESET));
Russell Belfer committed
172
	cl_assert_equal_i(
173 174
		GIT_SUBMODULE_UPDATE_REBASE,
		git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET));
175
	cl_assert_equal_i(
176 177
		GIT_SUBMODULE_RECURSE_YES, git_submodule_set_fetch_recurse_submodules(
			sm1, GIT_SUBMODULE_RECURSE_RESET));
Russell Belfer committed
178 179 180 181

	/* check that revert was successful */
	cl_assert_equal_s(old_url, git_submodule_url(sm1));
	cl_assert_equal_i((int)old_ignore, (int)git_submodule_ignore(sm1));
182
	cl_assert_equal_i((int)old_update, (int)git_submodule_update_strategy(sm1));
183 184
	cl_assert_equal_i(
		old_fetchrecurse, git_submodule_fetch_recurse_submodules(sm1));
Russell Belfer committed
185 186 187 188 189

	/* modify properties of submodule (again) */
	cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
	git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_UNTRACKED);
	git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
190
	git_submodule_set_fetch_recurse_submodules(sm1, GIT_SUBMODULE_RECURSE_YES);
Russell Belfer committed
191 192 193 194 195

	/* call save */
	cl_git_pass(git_submodule_save(sm1));

	/* attempt to "revert" values */
196 197
	git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_RESET);
	git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET);
Russell Belfer committed
198

199
	/* but ignore and update should NOT revert because the RESET
Russell Belfer committed
200 201 202 203 204
	 * should now be the newly saved value...
	 */
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
	cl_assert_equal_i(
205
		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update_strategy(sm1));
206
	cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
Russell Belfer committed
207 208

	/* call reload and check that the new values are loaded */
209
	cl_git_pass(git_submodule_reload(sm1, 0));
Russell Belfer committed
210 211 212 213 214

	cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
	cl_assert_equal_i(
		(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
	cl_assert_equal_i(
215
		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update_strategy(sm1));
216
	cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
Russell Belfer committed
217 218 219 220 221 222 223

	/* open a second copy of the repo and compare submodule */
	cl_git_pass(git_repository_open(&r2, "submod2"));
	cl_git_pass(git_submodule_lookup(&sm2, r2, "sm_changed_head"));

	cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm2));
	cl_assert_equal_i(
224
		GIT_SUBMODULE_IGNORE_UNTRACKED, git_submodule_ignore(sm2));
Russell Belfer committed
225
	cl_assert_equal_i(
226
		GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update_strategy(sm2));
227 228
	cl_assert_equal_i(
		GIT_SUBMODULE_RECURSE_NO, git_submodule_fetch_recurse_submodules(sm2));
229 230

	/* set fetchRecurseSubmodules on-demand */
231
	cl_git_pass(git_submodule_reload(sm1, 0));
232
	git_submodule_set_fetch_recurse_submodules(sm1, GIT_SUBMODULE_RECURSE_ONDEMAND);
233 234
	cl_assert_equal_i(
		GIT_SUBMODULE_RECURSE_ONDEMAND, git_submodule_fetch_recurse_submodules(sm1));
235 236
	/* call save */
	cl_git_pass(git_submodule_save(sm1));
237
	cl_git_pass(git_submodule_reload(sm1, 0));
238 239
	cl_assert_equal_i(
		GIT_SUBMODULE_RECURSE_ONDEMAND, git_submodule_fetch_recurse_submodules(sm1));
Russell Belfer committed
240

241 242
	git_submodule_free(sm1);
	git_submodule_free(sm2);
Russell Belfer committed
243
	git_repository_free(r2);
244
	git__free(old_url);
Russell Belfer committed
245
}