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

static git_repository *g_repo = NULL;

9 10 11
#define SM_LIBGIT2_URL    "https://github.com/libgit2/libgit2.git"
#define SM_LIBGIT2_BRANCH "github-branch"
#define SM_LIBGIT2        "sm_libgit2"
12

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

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

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 */
44 45 46 47
	cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
	cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string(&str, cfg, "submodule.sm_unchanged.url"));
	cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string(&str, cfg, "submodule.sm_changed_head.url"));
	cl_git_fail_with(GIT_ENOTFOUND, git_config_get_string(&str, cfg, "submodule.sm_added_and_uncommited.url"));
Russell Belfer committed
48 49 50 51 52 53
	git_config_free(cfg);

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

	/* confirm submodule data in config */
54
	cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
Russell Belfer committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	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
static void assert_submodule_url_is_synced(
	git_submodule *sm, const char *parent_key, const char *child_key)
{
	git_repository *smrepo;

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 82 83
	git_repository_free(smrepo);
}

Russell Belfer committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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 */
106
	cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
Russell Belfer committed
107 108 109 110 111 112 113 114 115 116 117 118
	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 */
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 127 128

	git_submodule_free(sm1);
	git_submodule_free(sm2);
	git_submodule_free(sm3);
Russell Belfer committed
129 130
}

131
void assert_ignore_change(git_submodule_ignore_t ignore)
132 133 134
{
	git_submodule *sm;

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 140 141
	git_submodule_free(sm);
}

142 143 144 145 146 147 148 149
void test_submodule_modify__set_ignore(void)
{
	assert_ignore_change(GIT_SUBMODULE_IGNORE_UNTRACKED);
	assert_ignore_change(GIT_SUBMODULE_IGNORE_NONE);
	assert_ignore_change(GIT_SUBMODULE_IGNORE_ALL);
}

void assert_update_change(git_submodule_update_t update)
150 151 152
{
	git_submodule *sm;

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 158 159
	git_submodule_free(sm);
}

160 161 162 163 164 165 166 167
void test_submodule_modify__set_update(void)
{
	assert_update_change(GIT_SUBMODULE_UPDATE_REBASE);
	assert_update_change(GIT_SUBMODULE_UPDATE_NONE);
	assert_update_change(GIT_SUBMODULE_UPDATE_CHECKOUT);
}

void assert_recurse_change(git_submodule_recurse_t recurse)
168 169 170
{
	git_submodule *sm;

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 179 180 181 182
void test_submodule_modify__set_fetch_recurse_submodules(void)
{
	assert_recurse_change(GIT_SUBMODULE_RECURSE_YES);
	assert_recurse_change(GIT_SUBMODULE_RECURSE_NO);
	assert_recurse_change(GIT_SUBMODULE_RECURSE_ONDEMAND);
183 184
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
void test_submodule_modify__set_branch(void)
{
	git_submodule *sm;

	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
	cl_assert(git_submodule_branch(sm) == NULL);
	git_submodule_free(sm);

	cl_git_pass(git_submodule_set_branch(g_repo, "sm_changed_head", SM_LIBGIT2_BRANCH));
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
	cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm));
	git_submodule_free(sm);

	cl_git_pass(git_submodule_set_branch(g_repo, "sm_changed_head", NULL));
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
	cl_assert(git_submodule_branch(sm) == NULL);
	git_submodule_free(sm);
}

204
void test_submodule_modify__set_url(void)
Russell Belfer committed
205
{
206
	git_submodule *sm;
Russell Belfer committed
207

208 209 210 211
	cl_git_pass(git_submodule_set_url(g_repo, "sm_changed_head", SM_LIBGIT2_URL));
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
	cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm));
	git_submodule_free(sm);
Russell Belfer committed
212
}