]> git.proxmox.com Git - libgit2.git/blob - tests/refs/branches/create.c
reset: remove reflog message override
[libgit2.git] / tests / refs / branches / create.c
1 #include "clar_libgit2.h"
2 #include "refs.h"
3 #include "path.h"
4
5 static git_repository *repo;
6 static git_commit *target;
7 static git_reference *branch;
8
9 void test_refs_branches_create__initialize(void)
10 {
11 repo = cl_git_sandbox_init("testrepo.git");
12 branch = NULL;
13 target = NULL;
14 }
15
16 void test_refs_branches_create__cleanup(void)
17 {
18 git_reference_free(branch);
19 branch = NULL;
20
21 git_commit_free(target);
22 target = NULL;
23
24 cl_git_sandbox_cleanup();
25 repo = NULL;
26 }
27
28 static void retrieve_target_from_oid(git_commit **out, git_repository *repo, const char *sha)
29 {
30 git_object *obj;
31
32 cl_git_pass(git_revparse_single(&obj, repo, sha));
33 cl_git_pass(git_commit_lookup(out, repo, git_object_id(obj)));
34 git_object_free(obj);
35 }
36
37 static void retrieve_known_commit(git_commit **commit, git_repository *repo)
38 {
39 retrieve_target_from_oid(commit, repo, "e90810b8df3");
40 }
41
42 #define NEW_BRANCH_NAME "new-branch-on-the-block"
43
44 void test_refs_branches_create__can_create_a_local_branch(void)
45 {
46 retrieve_known_commit(&target, repo);
47
48 cl_git_pass(git_branch_create(&branch, repo, NEW_BRANCH_NAME, target, 0, NULL));
49 cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
50 }
51
52 void test_refs_branches_create__can_not_create_a_branch_if_its_name_collide_with_an_existing_one(void)
53 {
54 retrieve_known_commit(&target, repo);
55
56 cl_assert_equal_i(GIT_EEXISTS, git_branch_create(&branch, repo, "br2", target, 0, NULL));
57 }
58
59 void test_refs_branches_create__can_force_create_over_an_existing_branch(void)
60 {
61 retrieve_known_commit(&target, repo);
62
63 cl_git_pass(git_branch_create(&branch, repo, "br2", target, 1, NULL));
64 cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
65 cl_assert_equal_s("refs/heads/br2", git_reference_name(branch));
66 }
67
68 void test_refs_branches_create__cannot_force_create_over_current_branch(void)
69 {
70 const git_oid *oid;
71 git_reference *branch2;
72 retrieve_known_commit(&target, repo);
73
74 cl_git_pass(git_branch_lookup(&branch2, repo, "master", GIT_BRANCH_LOCAL));
75 cl_assert_equal_s("refs/heads/master", git_reference_name(branch2));
76 cl_assert_equal_i(true, git_branch_is_head(branch2));
77 oid = git_reference_target(branch2);
78
79 cl_git_fail_with(-1, git_branch_create(&branch, repo, "master", target, 1, NULL));
80 branch = NULL;
81 cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
82 cl_assert_equal_s("refs/heads/master", git_reference_name(branch));
83 cl_git_pass(git_oid_cmp(git_reference_target(branch), oid));
84 git_reference_free(branch2);
85 }
86
87 void test_refs_branches_create__creating_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
88 {
89 retrieve_known_commit(&target, repo);
90
91 cl_assert_equal_i(GIT_EINVALIDSPEC,
92 git_branch_create(&branch, repo, "inv@{id", target, 0, NULL));
93 }
94
95 void test_refs_branches_create__creation_creates_new_reflog(void)
96 {
97 git_reflog *log;
98 const git_reflog_entry *entry;
99 git_signature *sig;
100
101 cl_git_pass(git_signature_now(&sig, "me", "foo@example.com"));
102
103 retrieve_known_commit(&target, repo);
104 cl_git_pass(git_branch_create(&branch, repo, NEW_BRANCH_NAME, target, false, "create!"));
105 cl_git_pass(git_reflog_read(&log, repo, "refs/heads/" NEW_BRANCH_NAME));
106
107 cl_assert_equal_i(1, git_reflog_entrycount(log));
108 entry = git_reflog_entry_byindex(log, 0);
109 cl_assert_equal_s("create!", git_reflog_entry_message(entry));
110
111 git_reflog_free(log);
112 git_signature_free(sig);
113 }
114
115 void test_refs_branches_create__default_reflog_message(void)
116 {
117 git_reflog *log;
118 const git_reflog_entry *entry;
119 git_signature *sig;
120 git_config *cfg;
121
122 cl_git_pass(git_repository_config(&cfg, repo));
123 cl_git_pass(git_config_set_string(cfg, "user.name", "Foo Bar"));
124 cl_git_pass(git_config_set_string(cfg, "user.email", "foo@example.com"));
125 git_config_free(cfg);
126
127 cl_git_pass(git_signature_default(&sig, repo));
128
129 retrieve_known_commit(&target, repo);
130 cl_git_pass(git_branch_create(&branch, repo, NEW_BRANCH_NAME, target, false, NULL));
131 cl_git_pass(git_reflog_read(&log, repo, "refs/heads/" NEW_BRANCH_NAME));
132
133 entry = git_reflog_entry_byindex(log, 0);
134 cl_assert_equal_s("Branch: created", git_reflog_entry_message(entry));
135 cl_assert_equal_s(sig->email, git_reflog_entry_committer(entry)->email);
136
137 git_reflog_free(log);
138 git_signature_free(sig);
139 }
140
141 static void assert_branch_matches_name(
142 const char *expected, const char *lookup_as)
143 {
144 git_reference *ref;
145 git_buf b = GIT_BUF_INIT;
146
147 cl_git_pass(git_branch_lookup(&ref, repo, lookup_as, GIT_BRANCH_LOCAL));
148
149 cl_git_pass(git_buf_sets(&b, "refs/heads/"));
150 cl_git_pass(git_buf_puts(&b, expected));
151 cl_assert_equal_s(b.ptr, git_reference_name(ref));
152
153 cl_git_pass(
154 git_oid_cmp(git_reference_target(ref), git_commit_id(target)));
155
156 git_reference_free(ref);
157 git_buf_free(&b);
158 }
159
160 void test_refs_branches_create__can_create_branch_with_unicode(void)
161 {
162 const char *nfc = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D";
163 const char *nfd = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
164 const char *emoji = "\xF0\x9F\x8D\xB7";
165 const char *names[] = { nfc, nfd, emoji };
166 const char *alt[] = { nfd, nfc, NULL };
167 const char *expected[] = { nfc, nfd, emoji };
168 unsigned int i;
169 bool fs_decompose_unicode =
170 git_path_does_fs_decompose_unicode(git_repository_path(repo));
171
172 retrieve_known_commit(&target, repo);
173
174 if (cl_repo_get_bool(repo, "core.precomposeunicode"))
175 expected[1] = nfc;
176 /* test decomp. because not all Mac filesystems decompose unicode */
177 else if (fs_decompose_unicode)
178 expected[0] = nfd;
179
180 for (i = 0; i < ARRAY_SIZE(names); ++i) {
181 const char *name;
182 cl_git_pass(git_branch_create(
183 &branch, repo, names[i], target, 0, NULL));
184 cl_git_pass(git_oid_cmp(
185 git_reference_target(branch), git_commit_id(target)));
186
187 cl_git_pass(git_branch_name(&name, branch));
188 cl_assert_equal_s(expected[i], name);
189 assert_branch_matches_name(expected[i], names[i]);
190 if (fs_decompose_unicode && alt[i])
191 assert_branch_matches_name(expected[i], alt[i]);
192
193 cl_git_pass(git_branch_delete(branch));
194 git_reference_free(branch);
195 branch = NULL;
196 }
197 }
198
199 /**
200 * Verify that we can create a branch with a name that matches the
201 * namespace of a previously delete branch.
202 *
203 * git branch level_one/level_two
204 * git branch -D level_one/level_two
205 * git branch level_one
206 *
207 * We expect the delete to have deleted the files:
208 * ".git/refs/heads/level_one/level_two"
209 * ".git/logs/refs/heads/level_one/level_two"
210 * It may or may not have deleted the (now empty)
211 * containing directories. To match git.git behavior,
212 * the second create needs to implicilty delete the
213 * directories and create the new files.
214 * "refs/heads/level_one"
215 * "logs/refs/heads/level_one"
216 *
217 * We should not fail to create the branch or its
218 * reflog because of an obsolete namespace container
219 * directory.
220 */
221 void test_refs_branches_create__name_vs_namespace(void)
222 {
223 const char * name;
224 struct item {
225 const char *first;
226 const char *second;
227 };
228 static const struct item item[] = {
229 { "level_one/level_two", "level_one" },
230 { "a/b/c/d/e", "a/b/c/d" },
231 { "ss/tt/uu/vv/ww", "ss" },
232 /* And one test case that is deeper. */
233 { "xx1/xx2/xx3/xx4", "xx1/xx2/xx3/xx4/xx5/xx6" },
234 { NULL, NULL },
235 };
236 const struct item *p;
237
238 retrieve_known_commit(&target, repo);
239
240 for (p=item; p->first; p++) {
241 cl_git_pass(git_branch_create(&branch, repo, p->first, target, 0, NULL));
242 cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
243 cl_git_pass(git_branch_name(&name, branch));
244 cl_assert_equal_s(name, p->first);
245
246 cl_git_pass(git_branch_delete(branch));
247 git_reference_free(branch);
248 branch = NULL;
249
250 cl_git_pass(git_branch_create(&branch, repo, p->second, target, 0, NULL));
251 git_reference_free(branch);
252 branch = NULL;
253 }
254 }
255
256 /**
257 * We still need to fail if part of the namespace is
258 * still in use.
259 */
260 void test_refs_branches_create__name_vs_namespace_fail(void)
261 {
262 const char * name;
263 struct item {
264 const char *first;
265 const char *first_alternate;
266 const char *second;
267 };
268 static const struct item item[] = {
269 { "level_one/level_two", "level_one/alternate", "level_one" },
270 { "a/b/c/d/e", "a/b/c/d/alternate", "a/b/c/d" },
271 { "ss/tt/uu/vv/ww", "ss/alternate", "ss" },
272 { NULL, NULL, NULL },
273 };
274 const struct item *p;
275
276 retrieve_known_commit(&target, repo);
277
278 for (p=item; p->first; p++) {
279 cl_git_pass(git_branch_create(&branch, repo, p->first, target, 0, NULL));
280 cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
281 cl_git_pass(git_branch_name(&name, branch));
282 cl_assert_equal_s(name, p->first);
283
284 cl_git_pass(git_branch_delete(branch));
285 git_reference_free(branch);
286 branch = NULL;
287
288 cl_git_pass(git_branch_create(&branch, repo, p->first_alternate, target, 0, NULL));
289 cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
290 cl_git_pass(git_branch_name(&name, branch));
291 cl_assert_equal_s(name, p->first_alternate);
292
293 /* we do not delete the alternate. */
294 git_reference_free(branch);
295 branch = NULL;
296
297 cl_git_fail(git_branch_create(&branch, repo, p->second, target, 0, NULL));
298 git_reference_free(branch);
299 branch = NULL;
300 }
301 }