Hash :
788cd2d5
Author :
Date :
2019-02-14T13:49:35
branches: do not assert that the given ref is a branch Libraries should use assert(3P) only very scarcely. First, we usually shouldn't cause the caller of our library to abort in case where the assert fails. Second, if code is compiled with -DNDEBUG, then the assert will not be included at all. In our `git_branch_is_checked_out` function, we have an assert that verifies that the given reference parameter is non-NULL and in fact a branch. While the first check is fine, the second is not. E.g. when compiled with -DNDEBUG, we'd proceed and treat the given reference as a branch in all cases. Fix the issue by instead treating a non-branch reference as not being checked out. This is the obvious solution, as references other than branches cannot be directly checked out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "clar_libgit2.h"
#include "refs.h"
#include "worktree/worktree_helpers.h"
static git_repository *repo;
static void assert_checked_out(git_repository *repo, const char *branch, int checked_out)
{
git_reference *ref;
cl_git_pass(git_reference_lookup(&ref, repo, branch));
cl_assert(git_branch_is_checked_out(ref) == checked_out);
git_reference_free(ref);
}
void test_refs_branches_checkedout__simple_repo(void)
{
repo = cl_git_sandbox_init("testrepo");
assert_checked_out(repo, "refs/heads/master", 1);
assert_checked_out(repo, "refs/heads/executable", 0);
cl_git_sandbox_cleanup();
}
void test_refs_branches_checkedout__worktree(void)
{
static worktree_fixture fixture =
WORKTREE_FIXTURE_INIT("testrepo", "testrepo-worktree");
setup_fixture_worktree(&fixture);
assert_checked_out(fixture.repo, "refs/heads/master", 1);
assert_checked_out(fixture.repo, "refs/heads/testrepo-worktree", 1);
assert_checked_out(fixture.worktree, "refs/heads/master", 1);
assert_checked_out(fixture.worktree, "refs/heads/testrepo-worktree", 1);
cleanup_fixture_worktree(&fixture);
}
void test_refs_branches_checkedout__head_is_not_checked_out(void)
{
repo = cl_git_sandbox_init("testrepo");
assert_checked_out(repo, "HEAD", 0);
cl_git_sandbox_cleanup();
}