]> git.proxmox.com Git - libgit2.git/blob - tests/refs/branches/move.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / refs / branches / move.c
1 #include "clar_libgit2.h"
2 #include "refs.h"
3 #include "config/config_helpers.h"
4
5 static git_repository *repo;
6
7 void test_refs_branches_move__initialize(void)
8 {
9 repo = cl_git_sandbox_init("testrepo.git");
10 }
11
12 void test_refs_branches_move__cleanup(void)
13 {
14 cl_git_sandbox_cleanup();
15 }
16
17 #define NEW_BRANCH_NAME "new-branch-on-the-block"
18
19 void test_refs_branches_move__can_move_a_local_branch(void)
20 {
21 git_reference *original_ref, *new_ref;
22
23 cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
24
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));
27
28 git_reference_free(original_ref);
29 git_reference_free(new_ref);
30 }
31
32 void test_refs_branches_move__can_move_a_local_branch_to_a_different_namespace(void)
33 {
34 git_reference *original_ref, *new_ref, *newer_ref;
35
36 cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
37
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);
45
46 git_reference_free(newer_ref);
47 }
48
49 void test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_namespace(void)
50 {
51 git_reference *original_ref, *new_ref, *newer_ref;
52
53 cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
54
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);
62
63 git_reference_free(newer_ref);
64 }
65
66 void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void)
67 {
68 git_reference *original_ref, *new_ref;
69 git_config *config;
70 git_buf original_remote = GIT_BUF_INIT,
71 original_merge = GIT_BUF_INIT;
72 const char *str;
73
74 cl_git_pass(git_repository_config_snapshot(&config, repo));
75
76 cl_git_pass(git_config_get_string_buf(&original_remote, config, "branch.master.remote"));
77 cl_git_pass(git_config_get_string_buf(&original_merge, config, "branch.master.merge"));
78 git_config_free(config);
79
80 cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
81
82 cl_assert_equal_i(GIT_EEXISTS,
83 git_branch_move(&new_ref, original_ref, "master", 0));
84
85 cl_assert(git_error_last()->message != NULL);
86
87 cl_git_pass(git_repository_config_snapshot(&config, repo));
88 cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
89 cl_assert_equal_s(original_remote.ptr, str);
90 cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
91 cl_assert_equal_s(original_merge.ptr, str);
92 git_config_free(config);
93
94 cl_assert_equal_i(GIT_EEXISTS,
95 git_branch_move(&new_ref, original_ref, "cannot-fetch", 0));
96
97 cl_assert(git_error_last()->message != NULL);
98
99 cl_git_pass(git_repository_config_snapshot(&config, repo));
100 cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
101 cl_assert_equal_s(original_remote.ptr, str);
102 cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
103 cl_assert_equal_s(original_merge.ptr, str);
104 git_config_free(config);
105
106 git_reference_free(original_ref);
107 cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/track-local"));
108
109 cl_assert_equal_i(GIT_EEXISTS,
110 git_branch_move(&new_ref, original_ref, "master", 0));
111
112 cl_assert(git_error_last()->message != NULL);
113
114 cl_git_pass(git_repository_config_snapshot(&config, repo));
115 cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
116 cl_assert_equal_s(original_remote.ptr, str);
117 cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
118 cl_assert_equal_s(original_merge.ptr, str);
119
120 git_buf_dispose(&original_remote);
121 git_buf_dispose(&original_merge);
122 git_reference_free(original_ref);
123 git_config_free(config);
124 }
125
126 void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
127 {
128 git_reference *original_ref, *new_ref;
129
130 cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
131
132 cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(&new_ref, original_ref, "Inv@{id", 0));
133
134 git_reference_free(original_ref);
135 }
136
137 void test_refs_branches_move__can_not_move_a_non_branch(void)
138 {
139 git_reference *tag, *new_ref;
140
141 cl_git_pass(git_reference_lookup(&tag, repo, "refs/tags/e90810b"));
142 cl_git_fail(git_branch_move(&new_ref, tag, NEW_BRANCH_NAME, 0));
143
144 git_reference_free(tag);
145 }
146
147 void test_refs_branches_move__can_force_move_over_an_existing_branch(void)
148 {
149 git_reference *original_ref, *new_ref;
150
151 cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
152
153 cl_git_pass(git_branch_move(&new_ref, original_ref, "master", 1));
154
155 git_reference_free(original_ref);
156 git_reference_free(new_ref);
157 }
158
159 void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(void)
160 {
161 git_reference *branch;
162 git_reference *new_branch;
163
164 cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
165
166 assert_config_entry_existence(repo, "branch.track-local.remote", true);
167 assert_config_entry_existence(repo, "branch.track-local.merge", true);
168 assert_config_entry_existence(repo, "branch.moved.remote", false);
169 assert_config_entry_existence(repo, "branch.moved.merge", false);
170
171 cl_git_pass(git_branch_move(&new_branch, branch, "moved", 0));
172 git_reference_free(branch);
173
174 assert_config_entry_existence(repo, "branch.track-local.remote", false);
175 assert_config_entry_existence(repo, "branch.track-local.merge", false);
176 assert_config_entry_existence(repo, "branch.moved.remote", true);
177 assert_config_entry_existence(repo, "branch.moved.merge", true);
178
179 git_reference_free(new_branch);
180 }
181
182 void test_refs_branches_move__moving_the_branch_pointed_at_by_HEAD_updates_HEAD(void)
183 {
184 git_reference *branch;
185 git_reference *new_branch;
186
187 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
188 cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0));
189 git_reference_free(branch);
190 git_reference_free(new_branch);
191
192 cl_git_pass(git_repository_head(&branch, repo));
193 cl_assert_equal_s("refs/heads/master2", git_reference_name(branch));
194 git_reference_free(branch);
195 }
196
197 void test_refs_branches_move__can_move_with_unicode(void)
198 {
199 git_reference *original_ref, *new_ref;
200 const char *new_branch_name = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
201
202 cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
203 cl_git_pass(git_branch_move(&new_ref, original_ref, new_branch_name, 0));
204
205 if (cl_repo_get_bool(repo, "core.precomposeunicode"))
206 cl_assert_equal_s(GIT_REFS_HEADS_DIR "\xC3\x85\x73\x74\x72\xC3\xB6\x6D", git_reference_name(new_ref));
207 else
208 cl_assert_equal_s(GIT_REFS_HEADS_DIR "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D", git_reference_name(new_ref));
209
210 git_reference_free(original_ref);
211 git_reference_free(new_ref);
212 }