move.c 7.83 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, NULL, NULL));
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, NULL, NULL));
40
	git_reference_free(original_ref);
41 42

	/* Upward */
43
	cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0, NULL, NULL));
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, NULL, NULL));
57
	git_reference_free(original_ref);
58 59

	/* Upward */
60
	cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0, NULL, NULL));
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 70 71 72 73 74 75 76 77 78 79
	git_config *config;
	const git_config_entry *ce;
	char *original_remote, *original_merge;

	cl_git_pass(git_repository_config(&config, repo));

	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
	original_remote = strdup(ce->value);
	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
	original_merge  = strdup(ce->value);

nulltoken committed
80

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

83
	cl_assert_equal_i(GIT_EEXISTS,
84
		git_branch_move(&new_ref, original_ref, "master", 0, NULL, NULL));
85 86 87 88 89 90 91 92 93

	cl_assert(giterr_last()->message != NULL);
	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
	cl_assert_equal_s(original_remote, ce->value);
	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
	cl_assert_equal_s(original_merge,  ce->value);


	cl_assert_equal_i(GIT_EEXISTS,
94
		git_branch_move(&new_ref, original_ref, "cannot-fetch", 0, NULL, NULL));
95 96 97 98 99 100 101 102 103 104 105

	cl_assert(giterr_last()->message != NULL);
	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
	cl_assert_equal_s(original_remote, ce->value);
	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
	cl_assert_equal_s(original_merge,  ce->value);

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

	cl_assert_equal_i(GIT_EEXISTS,
106
		git_branch_move(&new_ref, original_ref, "master", 0, NULL, NULL));
107 108 109 110 111 112

	cl_assert(giterr_last()->message != NULL);
	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
	cl_assert_equal_s(original_remote, ce->value);
	cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
	cl_assert_equal_s(original_merge,  ce->value);
nulltoken committed
113

114
	free(original_remote); free(original_merge);
115
	git_reference_free(original_ref);
116
	git_config_free(config);
117 118
}

119 120
void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
121
	git_reference *original_ref, *new_ref;
nulltoken committed
122

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

125
	cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(&new_ref, original_ref, "Inv@{id", 0, NULL, NULL));
nulltoken committed
126

127
	git_reference_free(original_ref);
128 129
}

130
void test_refs_branches_move__can_not_move_a_non_branch(void)
131
{
132
	git_reference *tag, *new_ref;
133

134
	cl_git_pass(git_reference_lookup(&tag, repo, "refs/tags/e90810b"));
135
	cl_git_fail(git_branch_move(&new_ref, tag, NEW_BRANCH_NAME, 0, NULL, NULL));
136

137
	git_reference_free(tag);
138
}
139

140
void test_refs_branches_move__can_force_move_over_an_existing_branch(void)
141
{
142
	git_reference *original_ref, *new_ref;
nulltoken committed
143

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

146
	cl_git_pass(git_branch_move(&new_ref, original_ref, "master", 1, NULL, NULL));
nulltoken committed
147

148 149
	git_reference_free(original_ref);
	git_reference_free(new_ref);
150
}
151

152 153 154
void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(void)
{
	git_reference *branch;
155
	git_reference *new_branch;
156 157 158 159 160 161 162 163

	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);

164
	cl_git_pass(git_branch_move(&new_branch, branch, "moved", 0, NULL, NULL));
165
	git_reference_free(branch);
166 167 168 169 170 171

	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);

172
	git_reference_free(new_branch);
173 174
}

175 176 177
void test_refs_branches_move__moving_the_branch_pointed_at_by_HEAD_updates_HEAD(void)
{
	git_reference *branch;
178
	git_reference *new_branch;
179 180

	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
181
	cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0, NULL, NULL));
182
	git_reference_free(branch);
183
	git_reference_free(new_branch);
184 185 186 187 188

	cl_git_pass(git_repository_head(&branch, repo));
	cl_assert_equal_s("refs/heads/master2", git_reference_name(branch));
	git_reference_free(branch);
}
189 190 191 192 193 194 195

void test_refs_branches_move__updates_the_reflog(void)
{
	git_reference *branch;
	git_reference *new_branch;
	git_reflog *log;
	const git_reflog_entry *entry;
196 197 198
	git_signature *sig;

	cl_git_pass(git_signature_now(&sig, "me", "foo@example.com"));
199 200

	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
201
	cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0, sig, "message"));
202 203 204 205

	cl_git_pass(git_reflog_read(&log, repo, git_reference_name(new_branch)));
	entry = git_reflog_entry_byindex(log, 0);
	cl_assert_equal_s("message", git_reflog_entry_message(entry));
206
	cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
207 208 209 210

	git_reference_free(branch);
	git_reference_free(new_branch);
	git_reflog_free(log);
211
	git_signature_free(sig);
212
}
213 214 215 216 217 218 219

void test_refs_branches_move__default_reflog_message(void)
{
	git_reference *branch;
	git_reference *new_branch;
	git_reflog *log;
	const git_reflog_entry *entry;
220
	git_signature *sig;
221 222 223 224 225 226
	git_config *cfg;

	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);
227 228

	cl_git_pass(git_signature_default(&sig, repo));
229 230 231 232 233 234 235 236

	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
	cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0, NULL, NULL));

	cl_git_pass(git_reflog_read(&log, repo, git_reference_name(new_branch)));
	entry = git_reflog_entry_byindex(log, 0);
	cl_assert_equal_s("Branch: renamed refs/heads/master to refs/heads/master2",
			git_reflog_entry_message(entry));
237
	cl_assert_equal_s(sig->email, git_reflog_entry_committer(entry)->email);
238 239 240 241

	git_reference_free(branch);
	git_reference_free(new_branch);
	git_reflog_free(log);
242
	git_signature_free(sig);
243
}