mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-06 13:25:18 +00:00

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.
131 lines
2.9 KiB
C
131 lines
2.9 KiB
C
#include "clar_libgit2.h"
|
|
#include "worktree.h"
|
|
#include "worktree_helpers.h"
|
|
|
|
#define COMMON_REPO "testrepo"
|
|
#define WORKTREE_REPO "testrepo-worktree"
|
|
|
|
static worktree_fixture fixture =
|
|
WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
|
|
|
|
void test_worktree_refs__initialize(void)
|
|
{
|
|
setup_fixture_worktree(&fixture);
|
|
}
|
|
|
|
void test_worktree_refs__cleanup(void)
|
|
{
|
|
cleanup_fixture_worktree(&fixture);
|
|
}
|
|
|
|
void test_worktree_refs__list(void)
|
|
{
|
|
git_strarray refs, wtrefs;
|
|
unsigned i, j;
|
|
int error = 0;
|
|
|
|
cl_git_pass(git_reference_list(&refs, fixture.repo));
|
|
cl_git_pass(git_reference_list(&wtrefs, fixture.worktree));
|
|
|
|
if (refs.count != wtrefs.count)
|
|
{
|
|
error = GIT_ERROR;
|
|
goto exit;
|
|
}
|
|
|
|
for (i = 0; i < refs.count; i++)
|
|
{
|
|
int found = 0;
|
|
|
|
for (j = 0; j < wtrefs.count; j++)
|
|
{
|
|
if (!strcmp(refs.strings[i], wtrefs.strings[j]))
|
|
{
|
|
found = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!found)
|
|
{
|
|
error = GIT_ERROR;
|
|
goto exit;
|
|
}
|
|
}
|
|
|
|
exit:
|
|
git_strarray_free(&refs);
|
|
git_strarray_free(&wtrefs);
|
|
cl_git_pass(error);
|
|
}
|
|
|
|
void test_worktree_refs__read_head(void)
|
|
{
|
|
git_reference *head;
|
|
|
|
cl_git_pass(git_repository_head(&head, fixture.worktree));
|
|
|
|
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;
|
|
|
|
cl_git_pass(git_branch_lookup(&branch, fixture.repo,
|
|
"testrepo-worktree", GIT_BRANCH_LOCAL));
|
|
cl_git_fail(git_branch_delete(branch));
|
|
|
|
git_reference_free(branch);
|
|
}
|
|
|
|
void test_worktree_refs__delete_succeeds_after_pruning_worktree(void)
|
|
{
|
|
git_reference *branch;
|
|
git_worktree *worktree;
|
|
|
|
cl_git_pass(git_worktree_lookup(&worktree, fixture.repo, fixture.worktreename));
|
|
cl_git_pass(git_worktree_prune(worktree, GIT_WORKTREE_PRUNE_VALID));
|
|
git_worktree_free(worktree);
|
|
|
|
cl_git_pass(git_branch_lookup(&branch, fixture.repo,
|
|
"testrepo-worktree", GIT_BRANCH_LOCAL));
|
|
cl_git_pass(git_branch_delete(branch));
|
|
git_reference_free(branch);
|
|
}
|