Commit 384518d0 by Patrick Steinhardt

repository: restrict checking out checked out branches

If a branch is already checked out in a working tree we are not
allowed to check out that branch in another repository. Introduce
this restriction when setting a repository's HEAD.
parent 143e539f
......@@ -2529,6 +2529,12 @@ int git_repository_set_head(
if (error < 0 && error != GIT_ENOTFOUND)
goto cleanup;
if (ref && current->type == GIT_REF_SYMBOLIC && git__strcmp(current->target.symbolic, ref->name) &&
git_reference_is_branch(ref) && git_branch_is_checked_out(ref)) {
error = -1;
goto cleanup;
}
if (!error) {
if (git_reference_is_branch(ref)) {
error = git_reference_symbolic_create(&new_head, repo, GIT_HEAD_FILE,
......
......@@ -68,6 +68,41 @@ void test_worktree_refs__read_head(void)
git_reference_free(head);
}
void test_worktree_refs__set_head_fails_when_worktree_wants_linked_repos_HEAD(void)
{
git_reference *head;
cl_git_pass(git_repository_head(&head, fixture.repo));
cl_git_fail(git_repository_set_head(fixture.worktree, git_reference_name(head)));
git_reference_free(head);
}
void test_worktree_refs__set_head_fails_when_main_repo_wants_worktree_head(void)
{
git_reference *head;
cl_git_pass(git_repository_head(&head, fixture.worktree));
cl_git_fail(git_repository_set_head(fixture.repo, git_reference_name(head)));
git_reference_free(head);
}
void test_worktree_refs__set_head_works_for_current_HEAD(void)
{
git_reference *head;
cl_git_pass(git_repository_head(&head, fixture.repo));
cl_git_pass(git_repository_set_head(fixture.repo, git_reference_name(head)));
git_reference_free(head);
}
void test_worktree_refs__set_head_fails_when_already_checked_out(void)
{
cl_git_fail(git_repository_set_head(fixture.repo, "refs/heads/testrepo-worktree"));
}
void test_worktree_refs__delete_fails_for_checked_out_branch(void)
{
git_reference *branch;
......
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