summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Fultz <jfultz@wolfram.com>2016-10-28 14:32:01 -0500
committerPatrick Steinhardt <ps@pks.im>2016-11-04 18:12:35 +0100
commitf9793884a3fc895a17a2a70709d32cbc2214377b (patch)
tree3fb3a18416cfa1bbdb6472cfa1b9f62a4be2bd69 /src
parente3298a330835af8d4760bf593500c28728398747 (diff)
downloadlibgit2-f9793884a3fc895a17a2a70709d32cbc2214377b.tar.gz
branch: fix forced branch creation on HEAD of a bare repo
The code correctly detects that forced creation of a branch on a nonbare repo should not be able to overwrite a branch which is the HEAD reference. But there's no reason to prevent this on a bare repo, and in fact, git allows this. I.e., git branch -f master new_sha works on a bare repo with HEAD set to master. This change fixes that problem, and updates tests so that, for this case, both the bare and nonbare cases are checked for correct behavior.
Diffstat (limited to 'src')
-rw-r--r--src/branch.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/branch.c b/src/branch.c
index 51c35d7ff..8d1ed6577 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -58,16 +58,17 @@ static int create_branch(
const char *from,
int force)
{
- int is_head = 0;
+ int is_unmovable_head = 0;
git_reference *branch = NULL;
git_buf canonical_branch_name = GIT_BUF_INIT,
log_message = GIT_BUF_INIT;
int error = -1;
+ int bare = git_repository_is_bare(repository);
assert(branch_name && commit && ref_out);
assert(git_object_owner((const git_object *)commit) == repository);
- if (force && git_branch_lookup(&branch, repository, branch_name, GIT_BRANCH_LOCAL) == 0) {
+ if (force && !bare && git_branch_lookup(&branch, repository, branch_name, GIT_BRANCH_LOCAL) == 0) {
error = git_branch_is_head(branch);
git_reference_free(branch);
branch = NULL;
@@ -75,10 +76,10 @@ static int create_branch(
if (error < 0)
goto cleanup;
- is_head = error;
+ is_unmovable_head = error;
}
- if (is_head && force) {
+ if (is_unmovable_head && force) {
giterr_set(GITERR_REFERENCE, "Cannot force update branch '%s' as it is "
"the current HEAD of the repository.", branch_name);
error = -1;