move.c 4.34 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 25 26
	git_reference *original_ref, *new_ref;

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

	cl_git_pass(git_branch_move(&new_ref, original_ref, NEW_BRANCH_NAME, 0));
	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 40
	cl_git_pass(git_branch_move(&new_ref, original_ref, "somewhere/" NEW_BRANCH_NAME, 0));
	git_reference_free(original_ref);
41 42

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

	/* Upward */
60 61
	cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
	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;
nulltoken committed
69

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

	cl_assert_equal_i(GIT_EEXISTS, git_branch_move(&new_ref, original_ref, "master", 0));
nulltoken committed
73

74
	git_reference_free(original_ref);
75 76
}

77 78
void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
79
	git_reference *original_ref, *new_ref;
nulltoken committed
80

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

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

85
	git_reference_free(original_ref);
86 87
}

88
void test_refs_branches_move__can_not_move_a_non_branch(void)
89
{
90
	git_reference *tag, *new_ref;
91

92
	cl_git_pass(git_reference_lookup(&tag, repo, "refs/tags/e90810b"));
93
	cl_git_fail(git_branch_move(&new_ref, tag, NEW_BRANCH_NAME, 0));
94

95
	git_reference_free(tag);
96
}
97

98
void test_refs_branches_move__can_force_move_over_an_existing_branch(void)
99
{
100
	git_reference *original_ref, *new_ref;
nulltoken committed
101

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

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

106 107
	git_reference_free(original_ref);
	git_reference_free(new_ref);
108
}
109

110 111 112
void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(void)
{
	git_reference *branch;
113
	git_reference *new_branch;
114 115 116 117 118 119 120 121

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

122 123
	cl_git_pass(git_branch_move(&new_branch, branch, "moved", 0));
	git_reference_free(branch);
124 125 126 127 128 129

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

130
	git_reference_free(new_branch);
131 132
}

133 134 135
void test_refs_branches_move__moving_the_branch_pointed_at_by_HEAD_updates_HEAD(void)
{
	git_reference *branch;
136
	git_reference *new_branch;
137 138

	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
139
	cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0));
140
	git_reference_free(branch);
141
	git_reference_free(new_branch);
142 143 144 145 146

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