Commit 494be429 by Ben Straub

Merge pull request #2144 from linquize/branch-f-current

Do not allow git_branch_create() to force update branch
parents 9776cb84 1d08b72e
...@@ -58,6 +58,7 @@ int git_branch_create( ...@@ -58,6 +58,7 @@ int git_branch_create(
const git_signature *signature, const git_signature *signature,
const char *log_message) const char *log_message)
{ {
int is_head = 0;
git_reference *branch = NULL; git_reference *branch = NULL;
git_buf canonical_branch_name = GIT_BUF_INIT, git_buf canonical_branch_name = GIT_BUF_INIT,
log_message_buf = GIT_BUF_INIT; log_message_buf = GIT_BUF_INIT;
...@@ -65,6 +66,18 @@ int git_branch_create( ...@@ -65,6 +66,18 @@ int git_branch_create(
assert(branch_name && commit && ref_out); assert(branch_name && commit && ref_out);
assert(git_object_owner((const git_object *)commit) == repository); assert(git_object_owner((const git_object *)commit) == repository);
if (git_branch_lookup(&branch, repository, branch_name, GIT_BRANCH_LOCAL) == 0) {
if ((is_head = git_branch_is_head(branch)) < 0) {
error = is_head;
goto cleanup;
}
}
if (is_head && force) {
giterr_set(GITERR_REFERENCE, "Cannot force update branch '%s' as it is "
"the current HEAD of the repository.", git_reference_name(branch));
goto cleanup;
}
if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0) if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0)
goto cleanup; goto cleanup;
......
...@@ -67,6 +67,25 @@ void test_refs_branches_create__can_force_create_over_an_existing_branch(void) ...@@ -67,6 +67,25 @@ void test_refs_branches_create__can_force_create_over_an_existing_branch(void)
cl_assert_equal_s("refs/heads/br2", git_reference_name(branch)); cl_assert_equal_s("refs/heads/br2", git_reference_name(branch));
} }
void test_refs_branches_create__cannot_force_create_over_current_branch(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_reference_target(branch2);
cl_git_fail_with(-1, git_branch_create(&branch, repo, "master", target, 1, NULL, NULL));
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);
}
void test_refs_branches_create__creating_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void) void test_refs_branches_create__creating_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{ {
retrieve_known_commit(&target, repo); retrieve_known_commit(&target, repo);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment