create.c 10 KB
Newer Older
1 2
#include "clar_libgit2.h"
#include "refs.h"
3
#include "path.h"
4 5

static git_repository *repo;
Vicent Marti committed
6
static git_commit *target;
7
static git_reference *branch;
8 9 10

void test_refs_branches_create__initialize(void)
{
11
	repo = cl_git_sandbox_init("testrepo.git");
12
	branch = NULL;
13
	target = NULL;
14 15 16 17
}

void test_refs_branches_create__cleanup(void)
{
18
	git_reference_free(branch);
19
	branch = NULL;
20

Ben Straub committed
21
	git_commit_free(target);
22 23
	target = NULL;

24
	cl_git_sandbox_cleanup();
25
	repo = NULL;
26 27
}

Vicent Marti committed
28
static void retrieve_target_from_oid(git_commit **out, git_repository *repo, const char *sha)
29
{
30
	git_object *obj;
31

32 33 34
	cl_git_pass(git_revparse_single(&obj, repo, sha));
	cl_git_pass(git_commit_lookup(out, repo, git_object_id(obj)));
	git_object_free(obj);
35 36
}

Vicent Marti committed
37
static void retrieve_known_commit(git_commit **commit, git_repository *repo)
38
{
39
	retrieve_target_from_oid(commit, repo, "e90810b8df3");
40 41 42 43 44 45 46 47
}

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

void test_refs_branches_create__can_create_a_local_branch(void)
{
	retrieve_known_commit(&target, repo);

48
	cl_git_pass(git_branch_create(&branch, repo, NEW_BRANCH_NAME, target, 0));
Vicent Marti committed
49
	cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
50 51 52 53 54 55
}

void test_refs_branches_create__can_not_create_a_branch_if_its_name_collide_with_an_existing_one(void)
{
	retrieve_known_commit(&target, repo);

56
	cl_assert_equal_i(GIT_EEXISTS, git_branch_create(&branch, repo, "br2", target, 0));
57 58 59 60 61 62
}

void test_refs_branches_create__can_force_create_over_an_existing_branch(void)
{
	retrieve_known_commit(&target, repo);

63
	cl_git_pass(git_branch_create(&branch, repo, "br2", target, 1));
Vicent Marti committed
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_in_nonbare_repo(void)
69 70 71
{
	const git_oid *oid;
	git_reference *branch2;
72 73 74 75

	/* Default repo for these tests is a bare repo, but this test requires a non-bare one */
	cl_git_sandbox_cleanup();
	repo = cl_git_sandbox_init("testrepo");
76 77 78 79 80 81 82
	retrieve_known_commit(&target, repo);

	cl_git_pass(git_branch_lookup(&branch2, repo, "master", GIT_BRANCH_LOCAL));
	cl_assert_equal_s("refs/heads/master", git_reference_name(branch2));
	cl_assert_equal_i(true, git_branch_is_head(branch2));
	oid = git_reference_target(branch2);

83
	cl_git_fail_with(-1, git_branch_create(&branch, repo, "master", target, 1));
84 85 86 87 88 89 90
	branch = NULL;
	cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
	cl_assert_equal_s("refs/heads/master", git_reference_name(branch));
	cl_git_pass(git_oid_cmp(git_reference_target(branch), oid));
	git_reference_free(branch2);
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
void test_refs_branches_create__can_force_create_over_current_branch_in_bare_repo(void)
{
	const git_oid *oid;
	git_reference *branch2;
	retrieve_known_commit(&target, repo);

	cl_git_pass(git_branch_lookup(&branch2, repo, "master", GIT_BRANCH_LOCAL));
	cl_assert_equal_s("refs/heads/master", git_reference_name(branch2));
	cl_assert_equal_i(true, git_branch_is_head(branch2));
	oid = git_commit_id(target);

	cl_git_pass(git_branch_create(&branch, repo, "master", target, 1));
	git_reference_free(branch);
	branch = NULL;
	cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
	cl_assert_equal_s("refs/heads/master", git_reference_name(branch));
	cl_git_pass(git_oid_cmp(git_reference_target(branch), oid));
	git_reference_free(branch2);
}

111 112 113 114 115
void test_refs_branches_create__creating_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
	retrieve_known_commit(&target, repo);

	cl_assert_equal_i(GIT_EINVALIDSPEC,
116
		git_branch_create(&branch, repo, "inv@{id", target, 0));
117 118 119 120 121
}

void test_refs_branches_create__default_reflog_message(void)
{
	git_reflog *log;
122
	git_buf buf = GIT_BUF_INIT;
123
	const git_reflog_entry *entry;
124
	git_annotated_commit *annotated;
125
	git_signature *sig;
126 127 128 129 130 131
	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);
132 133

	cl_git_pass(git_signature_default(&sig, repo));
134 135

	retrieve_known_commit(&target, repo);
136
	cl_git_pass(git_branch_create(&branch, repo, NEW_BRANCH_NAME, target, false));
137 138 139
	cl_git_pass(git_reflog_read(&log, repo, "refs/heads/" NEW_BRANCH_NAME));

	entry = git_reflog_entry_byindex(log, 0);
140
	cl_git_pass(git_buf_printf(&buf, "branch: Created from %s", git_oid_tostr_s(git_commit_id(target))));
141
	cl_assert_equal_s(git_buf_cstr(&buf), git_reflog_entry_message(entry));
142
	cl_assert_equal_s(sig->email, git_reflog_entry_committer(entry)->email);
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	cl_git_pass(git_reference_remove(repo, "refs/heads/" NEW_BRANCH_NAME));
	git_reference_free(branch);
	git_reflog_free(log);
	git_buf_clear(&buf);

	cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "e90810b8df3"));
	cl_git_pass(git_branch_create_from_annotated(&branch, repo, NEW_BRANCH_NAME, annotated, true));
	cl_git_pass(git_reflog_read(&log, repo, "refs/heads/" NEW_BRANCH_NAME));

	entry = git_reflog_entry_byindex(log, 0);
	cl_git_pass(git_buf_printf(&buf, "branch: Created from e90810b8df3"));
	cl_assert_equal_s(git_buf_cstr(&buf), git_reflog_entry_message(entry));
	cl_assert_equal_s(sig->email, git_reflog_entry_committer(entry)->email);

	git_annotated_commit_free(annotated);
159
	git_buf_free(&buf);
160
	git_reflog_free(log);
161
	git_signature_free(sig);
Ben Straub committed
162
}
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

static void assert_branch_matches_name(
	const char *expected, const char *lookup_as)
{
	git_reference *ref;
	git_buf b = GIT_BUF_INIT;

	cl_git_pass(git_branch_lookup(&ref, repo, lookup_as, GIT_BRANCH_LOCAL));

	cl_git_pass(git_buf_sets(&b, "refs/heads/"));
	cl_git_pass(git_buf_puts(&b, expected));
	cl_assert_equal_s(b.ptr, git_reference_name(ref));

	cl_git_pass(
		git_oid_cmp(git_reference_target(ref), git_commit_id(target)));

	git_reference_free(ref);
	git_buf_free(&b);
}

void test_refs_branches_create__can_create_branch_with_unicode(void)
{
	const char *nfc = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D";
	const char *nfd = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
	const char *emoji = "\xF0\x9F\x8D\xB7";
	const char *names[] = { nfc, nfd, emoji };
	const char *alt[] = { nfd, nfc, NULL };
	const char *expected[] = { nfc, nfd, emoji };
	unsigned int i;
192 193
	bool fs_decompose_unicode =
		git_path_does_fs_decompose_unicode(git_repository_path(repo));
194 195 196 197 198 199

	retrieve_known_commit(&target, repo);

	if (cl_repo_get_bool(repo, "core.precomposeunicode"))
		expected[1] = nfc;
	/* test decomp. because not all Mac filesystems decompose unicode */
200
	else if (fs_decompose_unicode)
201 202 203
		expected[0] = nfd;

	for (i = 0; i < ARRAY_SIZE(names); ++i) {
204
		const char *name;
205
		cl_git_pass(git_branch_create(
206
			&branch, repo, names[i], target, 0));
207 208 209
		cl_git_pass(git_oid_cmp(
			git_reference_target(branch), git_commit_id(target)));

210 211
		cl_git_pass(git_branch_name(&name, branch));
		cl_assert_equal_s(expected[i], name);
212
		assert_branch_matches_name(expected[i], names[i]);
213
		if (fs_decompose_unicode && alt[i])
214 215 216 217 218 219 220
			assert_branch_matches_name(expected[i], alt[i]);

		cl_git_pass(git_branch_delete(branch));
		git_reference_free(branch);
		branch = NULL;
	}
}
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

/**
 * Verify that we can create a branch with a name that matches the
 * namespace of a previously delete branch.
 *
 * git branch level_one/level_two
 * git branch -D level_one/level_two
 * git branch level_one
 *
 * We expect the delete to have deleted the files:
 *     ".git/refs/heads/level_one/level_two"
 *     ".git/logs/refs/heads/level_one/level_two"
 * It may or may not have deleted the (now empty)
 * containing directories.  To match git.git behavior,
 * the second create needs to implicilty delete the
 * directories and create the new files.
 *     "refs/heads/level_one"
 *     "logs/refs/heads/level_one"
 *
 * We should not fail to create the branch or its
 * reflog because of an obsolete namespace container
 * directory.
 */
void test_refs_branches_create__name_vs_namespace(void)
{
	const char * name;
	struct item {
		const char *first;
		const char *second;
	};
	static const struct item item[] = {
		{ "level_one/level_two", "level_one" },
		{ "a/b/c/d/e",           "a/b/c/d" },
		{ "ss/tt/uu/vv/ww",      "ss" },
		/* And one test case that is deeper. */
		{ "xx1/xx2/xx3/xx4",     "xx1/xx2/xx3/xx4/xx5/xx6" },
		{ NULL, NULL },
	};
	const struct item *p;

	retrieve_known_commit(&target, repo);

	for (p=item; p->first; p++) {
264
		cl_git_pass(git_branch_create(&branch, repo, p->first, target, 0));
265 266 267 268 269 270 271 272
		cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
		cl_git_pass(git_branch_name(&name, branch));
		cl_assert_equal_s(name, p->first);

		cl_git_pass(git_branch_delete(branch));
		git_reference_free(branch);
		branch = NULL;

273
		cl_git_pass(git_branch_create(&branch, repo, p->second, target, 0));
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
		git_reference_free(branch);
		branch = NULL;
	}
}

/**
 * We still need to fail if part of the namespace is
 * still in use.
 */
void test_refs_branches_create__name_vs_namespace_fail(void)
{
	const char * name;
	struct item {
		const char *first;
		const char *first_alternate;
		const char *second;
	};
	static const struct item item[] = {
		{ "level_one/level_two", "level_one/alternate", "level_one" },
		{ "a/b/c/d/e",           "a/b/c/d/alternate",   "a/b/c/d" },
		{ "ss/tt/uu/vv/ww",      "ss/alternate",        "ss" },
		{ NULL, NULL, NULL },
	};
	const struct item *p;

	retrieve_known_commit(&target, repo);

	for (p=item; p->first; p++) {
302
		cl_git_pass(git_branch_create(&branch, repo, p->first, target, 0));
303 304 305 306 307 308 309 310
		cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
		cl_git_pass(git_branch_name(&name, branch));
		cl_assert_equal_s(name, p->first);

		cl_git_pass(git_branch_delete(branch));
		git_reference_free(branch);
		branch = NULL;

311
		cl_git_pass(git_branch_create(&branch, repo, p->first_alternate, target, 0));
312 313 314 315 316 317 318 319
		cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
		cl_git_pass(git_branch_name(&name, branch));
		cl_assert_equal_s(name, p->first_alternate);

		/* we do not delete the alternate. */
		git_reference_free(branch);
		branch = NULL;

320
		cl_git_fail(git_branch_create(&branch, repo, p->second, target, 0));
321 322 323 324
		git_reference_free(branch);
		branch = NULL;
	}
}