move.c 8.03 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "refs.h"
3
#include "config/config_helpers.h"
4 5 6 7 8

static git_repository *repo;

void test_refs_branches_move__initialize(void)
{
9
	repo = cl_git_sandbox_init("testrepo.git");
10 11 12 13
}

void test_refs_branches_move__cleanup(void)
{
14
	cl_git_sandbox_cleanup();
15 16 17 18 19 20
}

#define NEW_BRANCH_NAME "new-branch-on-the-block"

void test_refs_branches_move__can_move_a_local_branch(void)
{
21 22 23 24
	git_reference *original_ref, *new_ref;

	cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));

25
	cl_git_pass(git_branch_move(&new_ref, original_ref, NEW_BRANCH_NAME, 0));
26
	cl_assert_equal_s(GIT_REFS_HEADS_DIR NEW_BRANCH_NAME, git_reference_name(new_ref));
nulltoken committed
27

28 29
	git_reference_free(original_ref);
	git_reference_free(new_ref);
30 31 32 33
}

void test_refs_branches_move__can_move_a_local_branch_to_a_different_namespace(void)
{
34
	git_reference *original_ref, *new_ref, *newer_ref;
nulltoken committed
35

36 37
	cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));

38
	/* Downward */
39
	cl_git_pass(git_branch_move(&new_ref, original_ref, "somewhere/" NEW_BRANCH_NAME, 0));
40
	git_reference_free(original_ref);
41 42

	/* Upward */
43
	cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
44
	git_reference_free(new_ref);
nulltoken committed
45

46
	git_reference_free(newer_ref);
47 48 49 50
}

void test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_namespace(void)
{
51
	git_reference *original_ref, *new_ref, *newer_ref;
nulltoken committed
52

53 54
	cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));

55
	/* Downward */
56
	cl_git_pass(git_branch_move(&new_ref, original_ref, "br2/" NEW_BRANCH_NAME, 0));
57
	git_reference_free(original_ref);
58 59

	/* Upward */
60
	cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
61
	git_reference_free(new_ref);
nulltoken committed
62

63
	git_reference_free(newer_ref);
64 65 66 67
}

void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void)
{
68
	git_reference *original_ref, *new_ref;
69
	git_config *config;
70
	git_buf buf = GIT_BUF_INIT;
71
	char *original_remote, *original_merge;
72
	const char *str;
73

74
	cl_git_pass(git_repository_config_snapshot(&config, repo));
75

76 77 78 79 80
	cl_git_pass(git_config_get_string_buf(&buf, config, "branch.master.remote"));
	original_remote = git_buf_detach(&buf);
	cl_git_pass(git_config_get_string_buf(&buf, config, "branch.master.merge"));
	original_merge  = git_buf_detach(&buf);
	git_config_free(config);
nulltoken committed
81

82 83
	cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));

84
	cl_assert_equal_i(GIT_EEXISTS,
85
		git_branch_move(&new_ref, original_ref, "master", 0));
86 87 88

	cl_assert(giterr_last()->message != NULL);

89 90 91 92 93 94
	cl_git_pass(git_repository_config_snapshot(&config, repo));
	cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
	cl_assert_equal_s(original_remote, str);
	cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
	cl_assert_equal_s(original_merge,  str);
	git_config_free(config);
95 96

	cl_assert_equal_i(GIT_EEXISTS,
97
		git_branch_move(&new_ref, original_ref, "cannot-fetch", 0));
98 99

	cl_assert(giterr_last()->message != NULL);
100 101 102 103 104 105 106

	cl_git_pass(git_repository_config_snapshot(&config, repo));
	cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
	cl_assert_equal_s(original_remote, str);
	cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
	cl_assert_equal_s(original_merge,  str);
	git_config_free(config);
107 108 109 110 111

	git_reference_free(original_ref);
	cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/track-local"));

	cl_assert_equal_i(GIT_EEXISTS,
112
		git_branch_move(&new_ref, original_ref, "master", 0));
113 114

	cl_assert(giterr_last()->message != NULL);
nulltoken committed
115

116 117 118 119 120 121 122
	cl_git_pass(git_repository_config_snapshot(&config, repo));
	cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
	cl_assert_equal_s(original_remote, str);
	cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
	cl_assert_equal_s(original_merge,  str);

	git__free(original_remote); git__free(original_merge);
123
	git_reference_free(original_ref);
124
	git_config_free(config);
125 126
}

127 128
void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
129
	git_reference *original_ref, *new_ref;
nulltoken committed
130

131 132
	cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));

133
	cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(&new_ref, original_ref, "Inv@{id", 0));
nulltoken committed
134

135
	git_reference_free(original_ref);
136 137
}

138
void test_refs_branches_move__can_not_move_a_non_branch(void)
139
{
140
	git_reference *tag, *new_ref;
141

142
	cl_git_pass(git_reference_lookup(&tag, repo, "refs/tags/e90810b"));
143
	cl_git_fail(git_branch_move(&new_ref, tag, NEW_BRANCH_NAME, 0));
144

145
	git_reference_free(tag);
146
}
147

148
void test_refs_branches_move__can_force_move_over_an_existing_branch(void)
149
{
150
	git_reference *original_ref, *new_ref;
nulltoken committed
151

152 153
	cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));

154
	cl_git_pass(git_branch_move(&new_ref, original_ref, "master", 1));
nulltoken committed
155

156 157
	git_reference_free(original_ref);
	git_reference_free(new_ref);
158
}
159

160 161 162
void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(void)
{
	git_reference *branch;
163
	git_reference *new_branch;
164 165 166 167 168 169 170 171

	cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));

	assert_config_entry_existence(repo, "branch.track-local.remote", true);
	assert_config_entry_existence(repo, "branch.track-local.merge", true);
	assert_config_entry_existence(repo, "branch.moved.remote", false);
	assert_config_entry_existence(repo, "branch.moved.merge", false);

172
	cl_git_pass(git_branch_move(&new_branch, branch, "moved", 0));
173
	git_reference_free(branch);
174 175 176 177 178 179

	assert_config_entry_existence(repo, "branch.track-local.remote", false);
	assert_config_entry_existence(repo, "branch.track-local.merge", false);
	assert_config_entry_existence(repo, "branch.moved.remote", true);
	assert_config_entry_existence(repo, "branch.moved.merge", true);

180
	git_reference_free(new_branch);
181 182
}

183 184 185
void test_refs_branches_move__moving_the_branch_pointed_at_by_HEAD_updates_HEAD(void)
{
	git_reference *branch;
186
	git_reference *new_branch;
187 188

	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
189
	cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0));
190
	git_reference_free(branch);
191
	git_reference_free(new_branch);
192 193 194 195 196

	cl_git_pass(git_repository_head(&branch, repo));
	cl_assert_equal_s("refs/heads/master2", git_reference_name(branch));
	git_reference_free(branch);
}
197

198 199 200 201 202 203
void test_refs_branches_move__default_reflog_message(void)
{
	git_reference *branch;
	git_reference *new_branch;
	git_reflog *log;
	const git_reflog_entry *entry;
204
	git_signature *sig;
205
	git_config *cfg;
206
	git_oid id;
207 208 209 210 211

	cl_git_pass(git_repository_config(&cfg, repo));
	cl_git_pass(git_config_set_string(cfg, "user.name", "Foo Bar"));
	cl_git_pass(git_config_set_string(cfg, "user.email", "foo@example.com"));
	git_config_free(cfg);
212 213

	cl_git_pass(git_signature_default(&sig, repo));
214 215

	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
216
	git_oid_cpy(&id, git_reference_target(branch));
217
	cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0));
218 219 220

	cl_git_pass(git_reflog_read(&log, repo, git_reference_name(new_branch)));
	entry = git_reflog_entry_byindex(log, 0);
221
	cl_assert_equal_s("branch: renamed refs/heads/master to refs/heads/master2",
222
			git_reflog_entry_message(entry));
223
	cl_assert_equal_s(sig->email, git_reflog_entry_committer(entry)->email);
224 225
	cl_assert_equal_oid(&id, git_reflog_entry_id_old(entry));
	cl_assert_equal_oid(&id, git_reflog_entry_id_new(entry));
226 227 228 229

	git_reference_free(branch);
	git_reference_free(new_branch);
	git_reflog_free(log);
230
	git_signature_free(sig);
231
}
232 233 234 235 236 237 238

void test_refs_branches_move__can_move_with_unicode(void)
{
	git_reference *original_ref, *new_ref;
	const char *new_branch_name = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";

	cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
239
	cl_git_pass(git_branch_move(&new_ref, original_ref, new_branch_name, 0));
240 241 242 243 244 245 246 247 248

	if (cl_repo_get_bool(repo, "core.precomposeunicode"))
		cl_assert_equal_s(GIT_REFS_HEADS_DIR "\xC3\x85\x73\x74\x72\xC3\xB6\x6D", git_reference_name(new_ref));
	else
		cl_assert_equal_s(GIT_REFS_HEADS_DIR "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D", git_reference_name(new_ref));

	git_reference_free(original_ref);
	git_reference_free(new_ref);
}