Initial commit - full huishou project

This commit is contained in:
jiapengyu
2026-07-27 14:07:26 +08:00
commit 60790ad1b3
39127 changed files with 5989265 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
#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();
}
void test_refs_branches_checkedout__master_in_bare_repo_is_not_checked_out(void)
{
repo = cl_git_sandbox_init("testrepo.git");
assert_checked_out(repo, "refs/heads/master", 0);
cl_git_sandbox_cleanup();
}
+279
View File
@@ -0,0 +1,279 @@
#include "clar_libgit2.h"
#include "refs.h"
#include "path.h"
static git_repository *repo;
static git_commit *target;
static git_reference *branch;
void test_refs_branches_create__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
branch = NULL;
target = NULL;
}
void test_refs_branches_create__cleanup(void)
{
git_reference_free(branch);
branch = NULL;
git_commit_free(target);
target = NULL;
cl_git_sandbox_cleanup();
repo = NULL;
}
static void retrieve_target_from_oid(git_commit **out, git_repository *repo, const char *sha)
{
git_object *obj;
cl_git_pass(git_revparse_single(&obj, repo, sha));
cl_git_pass(git_commit_lookup(out, repo, git_object_id(obj)));
git_object_free(obj);
}
static void retrieve_known_commit(git_commit **commit, git_repository *repo)
{
retrieve_target_from_oid(commit, repo, "e90810b8df3");
}
#define NEW_BRANCH_NAME "new-branch-on-the-block"
void test_refs_branches_create__can_create_a_local_branch(void)
{
retrieve_known_commit(&target, repo);
cl_git_pass(git_branch_create(&branch, repo, NEW_BRANCH_NAME, target, 0));
cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
}
void test_refs_branches_create__can_not_create_a_branch_if_its_name_collide_with_an_existing_one(void)
{
retrieve_known_commit(&target, repo);
cl_assert_equal_i(GIT_EEXISTS, git_branch_create(&branch, repo, "br2", target, 0));
}
void test_refs_branches_create__can_force_create_over_an_existing_branch(void)
{
retrieve_known_commit(&target, repo);
cl_git_pass(git_branch_create(&branch, repo, "br2", target, 1));
cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
cl_assert_equal_s("refs/heads/br2", git_reference_name(branch));
}
void test_refs_branches_create__cannot_force_create_over_current_branch_in_nonbare_repo(void)
{
const git_oid *oid;
git_reference *branch2;
/* Default repo for these tests is a bare repo, but this test requires a non-bare one */
cl_git_sandbox_cleanup();
repo = cl_git_sandbox_init("testrepo");
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));
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__can_force_create_over_current_branch_in_bare_repo(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_commit_id(target);
cl_git_pass(git_branch_create(&branch, repo, "master", target, 1));
git_reference_free(branch);
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)
{
retrieve_known_commit(&target, repo);
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_branch_create(&branch, repo, "inv@{id", target, 0));
}
static void assert_branch_matches_name(
const char *expected, const char *lookup_as)
{
git_reference *ref;
git_buf b = GIT_BUF_INIT;
cl_git_pass(git_branch_lookup(&ref, repo, lookup_as, GIT_BRANCH_LOCAL));
cl_git_pass(git_buf_sets(&b, "refs/heads/"));
cl_git_pass(git_buf_puts(&b, expected));
cl_assert_equal_s(b.ptr, git_reference_name(ref));
cl_git_pass(
git_oid_cmp(git_reference_target(ref), git_commit_id(target)));
git_reference_free(ref);
git_buf_dispose(&b);
}
void test_refs_branches_create__can_create_branch_with_unicode(void)
{
const char *nfc = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D";
const char *nfd = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
const char *emoji = "\xF0\x9F\x8D\xB7";
const char *names[] = { nfc, nfd, emoji };
const char *alt[] = { nfd, nfc, NULL };
const char *expected[] = { nfc, nfd, emoji };
unsigned int i;
bool fs_decompose_unicode =
git_path_does_fs_decompose_unicode(git_repository_path(repo));
retrieve_known_commit(&target, repo);
if (cl_repo_get_bool(repo, "core.precomposeunicode"))
expected[1] = nfc;
/* test decomp. because not all Mac filesystems decompose unicode */
else if (fs_decompose_unicode)
expected[0] = nfd;
for (i = 0; i < ARRAY_SIZE(names); ++i) {
const char *name;
cl_git_pass(git_branch_create(
&branch, repo, names[i], target, 0));
cl_git_pass(git_oid_cmp(
git_reference_target(branch), git_commit_id(target)));
cl_git_pass(git_branch_name(&name, branch));
cl_assert_equal_s(expected[i], name);
assert_branch_matches_name(expected[i], names[i]);
if (fs_decompose_unicode && alt[i])
assert_branch_matches_name(expected[i], alt[i]);
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
branch = NULL;
}
}
/**
* Verify that we can create a branch with a name that matches the
* namespace of a previously delete branch.
*
* git branch level_one/level_two
* git branch -D level_one/level_two
* git branch level_one
*
* We expect the delete to have deleted the files:
* ".git/refs/heads/level_one/level_two"
* ".git/logs/refs/heads/level_one/level_two"
* It may or may not have deleted the (now empty)
* containing directories. To match git.git behavior,
* the second create needs to implicilty delete the
* directories and create the new files.
* "refs/heads/level_one"
* "logs/refs/heads/level_one"
*
* We should not fail to create the branch or its
* reflog because of an obsolete namespace container
* directory.
*/
void test_refs_branches_create__name_vs_namespace(void)
{
const char * name;
struct item {
const char *first;
const char *second;
};
static const struct item item[] = {
{ "level_one/level_two", "level_one" },
{ "a/b/c/d/e", "a/b/c/d" },
{ "ss/tt/uu/vv/ww", "ss" },
/* And one test case that is deeper. */
{ "xx1/xx2/xx3/xx4", "xx1/xx2/xx3/xx4/xx5/xx6" },
{ NULL, NULL },
};
const struct item *p;
retrieve_known_commit(&target, repo);
for (p=item; p->first; p++) {
cl_git_pass(git_branch_create(&branch, repo, p->first, target, 0));
cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
cl_git_pass(git_branch_name(&name, branch));
cl_assert_equal_s(name, p->first);
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
branch = NULL;
cl_git_pass(git_branch_create(&branch, repo, p->second, target, 0));
git_reference_free(branch);
branch = NULL;
}
}
/**
* We still need to fail if part of the namespace is
* still in use.
*/
void test_refs_branches_create__name_vs_namespace_fail(void)
{
const char * name;
struct item {
const char *first;
const char *first_alternate;
const char *second;
};
static const struct item item[] = {
{ "level_one/level_two", "level_one/alternate", "level_one" },
{ "a/b/c/d/e", "a/b/c/d/alternate", "a/b/c/d" },
{ "ss/tt/uu/vv/ww", "ss/alternate", "ss" },
{ NULL, NULL, NULL },
};
const struct item *p;
retrieve_known_commit(&target, repo);
for (p=item; p->first; p++) {
cl_git_pass(git_branch_create(&branch, repo, p->first, target, 0));
cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
cl_git_pass(git_branch_name(&name, branch));
cl_assert_equal_s(name, p->first);
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
branch = NULL;
cl_git_pass(git_branch_create(&branch, repo, p->first_alternate, target, 0));
cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
cl_git_pass(git_branch_name(&name, branch));
cl_assert_equal_s(name, p->first_alternate);
/* we do not delete the alternate. */
git_reference_free(branch);
branch = NULL;
cl_git_fail(git_branch_create(&branch, repo, p->second, target, 0));
git_reference_free(branch);
branch = NULL;
}
}
+188
View File
@@ -0,0 +1,188 @@
#include "clar_libgit2.h"
#include "refs.h"
#include "repo/repo_helpers.h"
#include "config/config_helpers.h"
#include "futils.h"
#include "reflog.h"
static git_repository *repo;
static git_reference *fake_remote;
void test_refs_branches_delete__initialize(void)
{
git_oid id;
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL));
}
void test_refs_branches_delete__cleanup(void)
{
git_reference_free(fake_remote);
fake_remote = NULL;
cl_git_sandbox_cleanup();
repo = NULL;
}
void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void)
{
git_reference *head;
git_reference *branch;
/* Ensure HEAD targets the local master branch */
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
git_reference_free(head);
cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
cl_git_fail(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void)
{
git_reference *head;
git_reference *branch;
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
git_reference_delete(head);
git_reference_free(head);
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_unborn(void)
{
git_reference *branch;
make_head_unborn(repo, NON_EXISTING_HEAD);
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
{
git_reference *head, *branch;
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(head));
cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
git_reference_free(head);
/* Detach HEAD and make it target the commit that "master" points to */
git_repository_detach_head(repo);
cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_local_branch(void)
{
git_reference *branch;
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__can_delete_a_remote_branch(void)
{
git_reference *branch;
cl_git_pass(git_branch_lookup(&branch, repo, "nulltoken/master", GIT_BRANCH_REMOTE));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
}
void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_data(void)
{
git_reference *branch;
assert_config_entry_existence(repo, "branch.track-local.remote", true);
assert_config_entry_existence(repo, "branch.track-local.merge", true);
cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
assert_config_entry_existence(repo, "branch.track-local.remote", false);
assert_config_entry_existence(repo, "branch.track-local.merge", false);
}
void test_refs_branches_delete__removes_reflog(void)
{
git_reference *branch;
git_reflog *log;
git_oid oidzero = {{0}};
git_signature *sig;
/* Ensure the reflog has at least one entry */
cl_git_pass(git_signature_now(&sig, "Me", "user@example.com"));
cl_git_pass(git_reflog_read(&log, repo, "refs/heads/track-local"));
cl_git_pass(git_reflog_append(log, &oidzero, sig, "message"));
cl_assert(git_reflog_entrycount(log) > 0);
git_signature_free(sig);
git_reflog_free(log);
cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
git_reference_free(branch);
cl_assert_equal_i(false, git_reference_has_log(repo, "refs/heads/track-local"));
/* Reading a nonexistant reflog creates it, but it should be empty */
cl_git_pass(git_reflog_read(&log, repo, "refs/heads/track-local"));
cl_assert_equal_i(0, git_reflog_entrycount(log));
git_reflog_free(log);
}
void test_refs_branches_delete__removes_empty_folders(void)
{
const char *commondir = git_repository_commondir(repo);
git_oid commit_id;
git_commit *commit;
git_reference *branch;
git_reflog *log;
git_oid oidzero = {{0}};
git_signature *sig;
git_buf ref_folder = GIT_BUF_INIT;
git_buf reflog_folder = GIT_BUF_INIT;
/* Create a new branch with a nested name */
cl_git_pass(git_oid_fromstr(&commit_id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
cl_git_pass(git_branch_create(&branch, repo, "some/deep/ref", commit, 0));
git_commit_free(commit);
/* Ensure the reflog has at least one entry */
cl_git_pass(git_signature_now(&sig, "Me", "user@example.com"));
cl_git_pass(git_reflog_read(&log, repo, "refs/heads/some/deep/ref"));
cl_git_pass(git_reflog_append(log, &oidzero, sig, "message"));
cl_assert(git_reflog_entrycount(log) > 0);
git_signature_free(sig);
git_reflog_free(log);
cl_git_pass(git_buf_joinpath(&ref_folder, commondir, "refs/heads/some/deep"));
cl_git_pass(git_buf_join3(&reflog_folder, '/', commondir, GIT_REFLOG_DIR, "refs/heads/some/deep"));
cl_assert(git_path_exists(git_buf_cstr(&ref_folder)) == true);
cl_assert(git_path_exists(git_buf_cstr(&reflog_folder)) == true);
cl_git_pass(git_branch_delete(branch));
cl_assert(git_path_exists(git_buf_cstr(&ref_folder)) == false);
cl_assert(git_path_exists(git_buf_cstr(&reflog_folder)) == false);
git_reference_free(branch);
git_buf_dispose(&ref_folder);
git_buf_dispose(&reflog_folder);
}
+98
View File
@@ -0,0 +1,98 @@
#include "clar_libgit2.h"
#include "refs.h"
#include "repo/repo_helpers.h"
static git_repository *repo;
static git_reference *branch;
void test_refs_branches_ishead__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
branch = NULL;
}
void test_refs_branches_ishead__cleanup(void)
{
git_reference_free(branch);
branch = NULL;
cl_git_sandbox_cleanup();
repo = NULL;
}
void test_refs_branches_ishead__can_tell_if_a_branch_is_pointed_at_by_HEAD(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_assert_equal_i(true, git_branch_is_head(branch));
}
void test_refs_branches_ishead__can_properly_handle_unborn_HEAD(void)
{
make_head_unborn(repo, NON_EXISTING_HEAD);
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_assert_equal_i(false, git_branch_is_head(branch));
}
void test_refs_branches_ishead__can_properly_handle_missing_HEAD(void)
{
delete_head(repo);
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_assert_equal_i(false, git_branch_is_head(branch));
}
void test_refs_branches_ishead__can_tell_if_a_branch_is_not_pointed_at_by_HEAD(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/br2"));
cl_assert_equal_i(false, git_branch_is_head(branch));
}
void test_refs_branches_ishead__wont_be_fooled_by_a_non_branch(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));
cl_assert_equal_i(false, git_branch_is_head(branch));
}
/*
* $ git init .
* Initialized empty Git repository in d:/temp/tempee/.git/
*
* $ touch a && git add a
* $ git commit -m" boom"
* [master (root-commit) b47b758] boom
* 0 files changed
* create mode 100644 a
*
* $ echo "ref: refs/heads/master" > .git/refs/heads/linked
* $ echo "ref: refs/heads/linked" > .git/refs/heads/super
* $ echo "ref: refs/heads/super" > .git/HEAD
*
* $ git branch
* linked -> master
* * master
* super -> master
*/
void test_refs_branches_ishead__only_direct_references_are_considered(void)
{
git_reference *linked, *super, *head;
cl_git_pass(git_reference_symbolic_create(&linked, repo, "refs/heads/linked", "refs/heads/master", 0, NULL));
cl_git_pass(git_reference_symbolic_create(&super, repo, "refs/heads/super", "refs/heads/linked", 0, NULL));
cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, "refs/heads/super", 1, NULL));
cl_assert_equal_i(false, git_branch_is_head(linked));
cl_assert_equal_i(false, git_branch_is_head(super));
cl_git_pass(git_repository_head(&branch, repo));
cl_assert_equal_s("refs/heads/master", git_reference_name(branch));
git_reference_free(linked);
git_reference_free(super);
git_reference_free(head);
}
+151
View File
@@ -0,0 +1,151 @@
#include "clar_libgit2.h"
#include "refs.h"
static git_repository *repo;
static git_reference *fake_remote;
void test_refs_branches_iterator__initialize(void)
{
git_oid id;
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL));
}
void test_refs_branches_iterator__cleanup(void)
{
git_reference_free(fake_remote);
fake_remote = NULL;
git_repository_free(repo);
repo = NULL;
cl_fixture_cleanup("testrepo.git");
cl_git_sandbox_cleanup();
}
static void assert_retrieval(unsigned int flags, unsigned int expected_count)
{
git_branch_iterator *iter;
git_reference *ref;
int count = 0, error;
git_branch_t type;
cl_git_pass(git_branch_iterator_new(&iter, repo, flags));
while ((error = git_branch_next(&ref, &type, iter)) == 0) {
count++;
git_reference_free(ref);
}
git_branch_iterator_free(iter);
cl_assert_equal_i(error, GIT_ITEROVER);
cl_assert_equal_i(expected_count, count);
}
void test_refs_branches_iterator__retrieve_all_branches(void)
{
assert_retrieval(GIT_BRANCH_ALL, 14);
}
void test_refs_branches_iterator__retrieve_remote_branches(void)
{
assert_retrieval(GIT_BRANCH_REMOTE, 2);
}
void test_refs_branches_iterator__retrieve_local_branches(void)
{
assert_retrieval(GIT_BRANCH_LOCAL, 12);
}
struct expectations {
const char *branch_name;
int encounters;
};
static void assert_branch_has_been_found(struct expectations *findings, const char* expected_branch_name)
{
int pos = 0;
for (pos = 0; findings[pos].branch_name; ++pos) {
if (strcmp(expected_branch_name, findings[pos].branch_name) == 0) {
cl_assert_equal_i(1, findings[pos].encounters);
return;
}
}
cl_fail("expected branch not found in list.");
}
static void contains_branches(struct expectations exp[], git_branch_iterator *iter)
{
git_reference *ref;
git_branch_t type;
int error, pos = 0;
while ((error = git_branch_next(&ref, &type, iter)) == 0) {
for (pos = 0; exp[pos].branch_name; ++pos) {
if (strcmp(git_reference_shorthand(ref), exp[pos].branch_name) == 0)
exp[pos].encounters++;
}
git_reference_free(ref);
}
cl_assert_equal_i(error, GIT_ITEROVER);
}
/*
* $ git branch -r
* nulltoken/HEAD -> nulltoken/master
* nulltoken/master
*/
void test_refs_branches_iterator__retrieve_remote_symbolic_HEAD_when_present(void)
{
git_branch_iterator *iter;
struct expectations exp[] = {
{ "nulltoken/HEAD", 0 },
{ "nulltoken/master", 0 },
{ NULL, 0 }
};
git_reference_free(fake_remote);
cl_git_pass(git_reference_symbolic_create(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0, NULL));
assert_retrieval(GIT_BRANCH_REMOTE, 3);
cl_git_pass(git_branch_iterator_new(&iter, repo, GIT_BRANCH_REMOTE));
contains_branches(exp, iter);
git_branch_iterator_free(iter);
assert_branch_has_been_found(exp, "nulltoken/HEAD");
assert_branch_has_been_found(exp, "nulltoken/master");
}
void test_refs_branches_iterator__mix_of_packed_and_loose(void)
{
git_branch_iterator *iter;
struct expectations exp[] = {
{ "master", 0 },
{ "origin/HEAD", 0 },
{ "origin/master", 0 },
{ "origin/packed", 0 },
{ NULL, 0 }
};
git_repository *r2;
r2 = cl_git_sandbox_init("testrepo2");
cl_git_pass(git_branch_iterator_new(&iter, r2, GIT_BRANCH_ALL));
contains_branches(exp, iter);
git_branch_iterator_free(iter);
assert_branch_has_been_found(exp, "master");
assert_branch_has_been_found(exp, "origin/HEAD");
assert_branch_has_been_found(exp, "origin/master");
assert_branch_has_been_found(exp, "origin/packed");
}
+68
View File
@@ -0,0 +1,68 @@
#include "clar_libgit2.h"
#include "refs.h"
static git_repository *repo;
static git_reference *branch;
void test_refs_branches_lookup__initialize(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
branch = NULL;
}
void test_refs_branches_lookup__cleanup(void)
{
git_reference_free(branch);
branch = NULL;
git_repository_free(repo);
repo = NULL;
}
void test_refs_branches_lookup__can_retrieve_a_local_branch_local(void)
{
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
}
void test_refs_branches_lookup__can_retrieve_a_local_branch_all(void)
{
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_ALL));
}
void test_refs_branches_lookup__trying_to_retrieve_a_local_branch_remote(void)
{
cl_git_fail(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_REMOTE));
}
void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch_remote(void)
{
cl_git_pass(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_REMOTE));
}
void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch_all(void)
{
cl_git_pass(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_ALL));
}
void test_refs_branches_lookup__trying_to_retrieve_a_remote_tracking_branch_local(void)
{
cl_git_fail(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_LOCAL));
}
void test_refs_branches_lookup__trying_to_retrieve_an_unknown_branch_returns_ENOTFOUND(void)
{
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "where/are/you", GIT_BRANCH_LOCAL));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "over/here", GIT_BRANCH_REMOTE));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "maybe/here", GIT_BRANCH_ALL));
}
void test_refs_branches_lookup__trying_to_retrieve_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_branch_lookup(&branch, repo, "are/you/inv@{id", GIT_BRANCH_LOCAL));
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_branch_lookup(&branch, repo, "yes/i am", GIT_BRANCH_REMOTE));
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_branch_lookup(&branch, repo, "inv al/id", GIT_BRANCH_ALL));
}
+213
View File
@@ -0,0 +1,213 @@
#include "clar_libgit2.h"
#include "refs.h"
#include "config/config_helpers.h"
static git_repository *repo;
void test_refs_branches_move__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
}
void test_refs_branches_move__cleanup(void)
{
cl_git_sandbox_cleanup();
}
#define NEW_BRANCH_NAME "new-branch-on-the-block"
void test_refs_branches_move__can_move_a_local_branch(void)
{
git_reference *original_ref, *new_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_git_pass(git_branch_move(&new_ref, original_ref, NEW_BRANCH_NAME, 0));
cl_assert_equal_s(GIT_REFS_HEADS_DIR NEW_BRANCH_NAME, git_reference_name(new_ref));
git_reference_free(original_ref);
git_reference_free(new_ref);
}
void test_refs_branches_move__can_move_a_local_branch_to_a_different_namespace(void)
{
git_reference *original_ref, *new_ref, *newer_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
/* Downward */
cl_git_pass(git_branch_move(&new_ref, original_ref, "somewhere/" NEW_BRANCH_NAME, 0));
git_reference_free(original_ref);
/* Upward */
cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
git_reference_free(new_ref);
git_reference_free(newer_ref);
}
void test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_namespace(void)
{
git_reference *original_ref, *new_ref, *newer_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
/* Downward */
cl_git_pass(git_branch_move(&new_ref, original_ref, "br2/" NEW_BRANCH_NAME, 0));
git_reference_free(original_ref);
/* Upward */
cl_git_pass(git_branch_move(&newer_ref, new_ref, "br2", 0));
git_reference_free(new_ref);
git_reference_free(newer_ref);
}
void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void)
{
git_reference *original_ref, *new_ref;
git_config *config;
git_buf buf = GIT_BUF_INIT;
char *original_remote, *original_merge;
const char *str;
cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_string_buf(&buf, config, "branch.master.remote"));
original_remote = git_buf_detach(&buf);
cl_git_pass(git_config_get_string_buf(&buf, config, "branch.master.merge"));
original_merge = git_buf_detach(&buf);
git_config_free(config);
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_assert_equal_i(GIT_EEXISTS,
git_branch_move(&new_ref, original_ref, "master", 0));
cl_assert(git_error_last()->message != NULL);
cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
cl_assert_equal_s(original_remote, str);
cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
cl_assert_equal_s(original_merge, str);
git_config_free(config);
cl_assert_equal_i(GIT_EEXISTS,
git_branch_move(&new_ref, original_ref, "cannot-fetch", 0));
cl_assert(git_error_last()->message != NULL);
cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
cl_assert_equal_s(original_remote, str);
cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
cl_assert_equal_s(original_merge, str);
git_config_free(config);
git_reference_free(original_ref);
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/track-local"));
cl_assert_equal_i(GIT_EEXISTS,
git_branch_move(&new_ref, original_ref, "master", 0));
cl_assert(git_error_last()->message != NULL);
cl_git_pass(git_repository_config_snapshot(&config, repo));
cl_git_pass(git_config_get_string(&str, config, "branch.master.remote"));
cl_assert_equal_s(original_remote, str);
cl_git_pass(git_config_get_string(&str, config, "branch.master.merge"));
cl_assert_equal_s(original_merge, str);
git__free(original_remote); git__free(original_merge);
git_reference_free(original_ref);
git_config_free(config);
}
void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
{
git_reference *original_ref, *new_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_assert_equal_i(GIT_EINVALIDSPEC, git_branch_move(&new_ref, original_ref, "Inv@{id", 0));
git_reference_free(original_ref);
}
void test_refs_branches_move__can_not_move_a_non_branch(void)
{
git_reference *tag, *new_ref;
cl_git_pass(git_reference_lookup(&tag, repo, "refs/tags/e90810b"));
cl_git_fail(git_branch_move(&new_ref, tag, NEW_BRANCH_NAME, 0));
git_reference_free(tag);
}
void test_refs_branches_move__can_force_move_over_an_existing_branch(void)
{
git_reference *original_ref, *new_ref;
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_git_pass(git_branch_move(&new_ref, original_ref, "master", 1));
git_reference_free(original_ref);
git_reference_free(new_ref);
}
void test_refs_branches_move__moving_a_branch_moves_related_configuration_data(void)
{
git_reference *branch;
git_reference *new_branch;
cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
assert_config_entry_existence(repo, "branch.track-local.remote", true);
assert_config_entry_existence(repo, "branch.track-local.merge", true);
assert_config_entry_existence(repo, "branch.moved.remote", false);
assert_config_entry_existence(repo, "branch.moved.merge", false);
cl_git_pass(git_branch_move(&new_branch, branch, "moved", 0));
git_reference_free(branch);
assert_config_entry_existence(repo, "branch.track-local.remote", false);
assert_config_entry_existence(repo, "branch.track-local.merge", false);
assert_config_entry_existence(repo, "branch.moved.remote", true);
assert_config_entry_existence(repo, "branch.moved.merge", true);
git_reference_free(new_branch);
}
void test_refs_branches_move__moving_the_branch_pointed_at_by_HEAD_updates_HEAD(void)
{
git_reference *branch;
git_reference *new_branch;
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0));
git_reference_free(branch);
git_reference_free(new_branch);
cl_git_pass(git_repository_head(&branch, repo));
cl_assert_equal_s("refs/heads/master2", git_reference_name(branch));
git_reference_free(branch);
}
void test_refs_branches_move__can_move_with_unicode(void)
{
git_reference *original_ref, *new_ref;
const char *new_branch_name = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
cl_git_pass(git_branch_move(&new_ref, original_ref, new_branch_name, 0));
if (cl_repo_get_bool(repo, "core.precomposeunicode"))
cl_assert_equal_s(GIT_REFS_HEADS_DIR "\xC3\x85\x73\x74\x72\xC3\xB6\x6D", git_reference_name(new_ref));
else
cl_assert_equal_s(GIT_REFS_HEADS_DIR "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D", git_reference_name(new_ref));
git_reference_free(original_ref);
git_reference_free(new_ref);
}
+45
View File
@@ -0,0 +1,45 @@
#include "clar_libgit2.h"
#include "branch.h"
static git_repository *repo;
static git_reference *ref;
void test_refs_branches_name__initialize(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
}
void test_refs_branches_name__cleanup(void)
{
git_reference_free(ref);
ref = NULL;
git_repository_free(repo);
repo = NULL;
}
void test_refs_branches_name__can_get_local_branch_name(void)
{
const char *name;
cl_git_pass(git_branch_lookup(&ref,repo,"master",GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_name(&name,ref));
cl_assert_equal_s("master",name);
}
void test_refs_branches_name__can_get_remote_branch_name(void)
{
const char *name;
cl_git_pass(git_branch_lookup(&ref,repo,"test/master",GIT_BRANCH_REMOTE));
cl_git_pass(git_branch_name(&name,ref));
cl_assert_equal_s("test/master",name);
}
void test_refs_branches_name__error_when_ref_is_no_branch(void)
{
const char *name;
cl_git_pass(git_reference_lookup(&ref,repo,"refs/notes/fanout"));
cl_git_fail(git_branch_name(&name,ref));
}
+68
View File
@@ -0,0 +1,68 @@
#include "clar_libgit2.h"
#include "branch.h"
#include "remote.h"
static git_repository *g_repo;
static const char *remote_tracking_branch_name = "refs/remotes/test/master";
static const char *expected_remote_name = "test";
static int expected_remote_name_length;
void test_refs_branches_remote__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
expected_remote_name_length = (int)strlen(expected_remote_name) + 1;
}
void test_refs_branches_remote__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_refs_branches_remote__can_get_remote_for_branch(void)
{
git_buf remotename = {0};
cl_git_pass(git_branch_remote_name(&remotename, g_repo, remote_tracking_branch_name));
cl_assert_equal_s("test", remotename.ptr);
git_buf_dispose(&remotename);
}
void test_refs_branches_remote__no_matching_remote_returns_error(void)
{
const char *unknown = "refs/remotes/nonexistent/master";
git_buf buf;
git_error_clear();
memset(&buf, 0, sizeof(git_buf));
cl_git_fail_with(git_branch_remote_name(&buf, g_repo, unknown), GIT_ENOTFOUND);
cl_assert(git_error_last() != NULL);
}
void test_refs_branches_remote__local_remote_returns_error(void)
{
const char *local = "refs/heads/master";
git_buf buf;
git_error_clear();
memset(&buf, 0, sizeof(git_buf));
cl_git_fail_with(git_branch_remote_name(&buf, g_repo, local), GIT_ERROR);
cl_assert(git_error_last() != NULL);
}
void test_refs_branches_remote__ambiguous_remote_returns_error(void)
{
git_remote *remote;
git_buf buf;
/* Create the remote */
cl_git_pass(git_remote_create_with_fetchspec(&remote, g_repo, "addtest", "http://github.com/libgit2/libgit2", "refs/heads/*:refs/remotes/test/*"));
git_remote_free(remote);
git_error_clear();
memset(&buf, 0, sizeof(git_buf));
cl_git_fail_with(git_branch_remote_name(&buf, g_repo, remote_tracking_branch_name), GIT_EAMBIGUOUS);
cl_assert(git_error_last() != NULL);
}
+193
View File
@@ -0,0 +1,193 @@
#include "clar_libgit2.h"
#include "config/config_helpers.h"
#include "refs.h"
static git_repository *repo;
static git_reference *branch, *upstream;
void test_refs_branches_upstream__initialize(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
branch = NULL;
upstream = NULL;
}
void test_refs_branches_upstream__cleanup(void)
{
git_reference_free(upstream);
git_reference_free(branch);
branch = NULL;
git_repository_free(repo);
repo = NULL;
}
void test_refs_branches_upstream__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_git_pass(git_branch_upstream(&upstream, branch));
cl_assert_equal_s("refs/remotes/test/master", git_reference_name(upstream));
}
void test_refs_branches_upstream__can_retrieve_the_local_upstream_reference_of_a_local_branch(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/track-local"));
cl_git_pass(git_branch_upstream(&upstream, branch));
cl_assert_equal_s("refs/heads/master", git_reference_name(upstream));
}
void test_refs_branches_upstream__cannot_retrieve_a_remote_upstream_reference_from_a_non_branch(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));
cl_git_fail(git_branch_upstream(&upstream, branch));
}
void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees"));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
}
void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/cannot-fetch"));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
}
void test_refs_branches_upstream__upstream_remote(void)
{
git_buf buf = GIT_BUF_INIT;
cl_git_pass(git_branch_upstream_remote(&buf, repo, "refs/heads/master"));
cl_assert_equal_s("test", buf.ptr);
git_buf_dispose(&buf);
}
void test_refs_branches_upstream__upstream_remote_empty_value(void)
{
git_repository *repository;
git_config *cfg;
git_buf buf = GIT_BUF_INIT;
repository = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_config(&cfg, repository));
cl_git_pass(git_config_set_string(cfg, "branch.master.remote", ""));
cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream_remote(&buf, repository, "refs/heads/master"));
cl_git_pass(git_config_delete_entry(cfg, "branch.master.remote"));
cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream_remote(&buf, repository, "refs/heads/master"));
cl_git_sandbox_cleanup();
}
static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name)
{
git_reference *branch;
cl_assert_equal_i(GIT_OBJECT_COMMIT, git_object_type((git_object*)target));
cl_git_pass(git_branch_create(&branch, repository, entry_name, (git_commit*)target, 0));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
git_reference_free(branch);
}
void test_refs_branches_upstream__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void)
{
git_reference *head;
git_repository *repository;
git_commit *target;
repository = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_repository_head(&head, repository));
cl_git_pass(git_reference_peel(((git_object **)&target), head, GIT_OBJECT_COMMIT));
git_reference_free(head);
assert_merge_and_or_remote_key_missing(repository, target, "remoteless");
assert_merge_and_or_remote_key_missing(repository, target, "mergeless");
assert_merge_and_or_remote_key_missing(repository, target, "mergeandremoteless");
git_commit_free(target);
cl_git_sandbox_cleanup();
}
void test_refs_branches_upstream__set_unset_upstream(void)
{
git_reference *branch;
git_repository *repository;
repository = cl_git_sandbox_init("testrepo.git");
/* remote */
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
cl_git_pass(git_branch_set_upstream(branch, "test/master"));
assert_config_entry_value(repository, "branch.test.remote", "test");
assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
git_reference_free(branch);
/* local */
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
cl_git_pass(git_branch_set_upstream(branch, "master"));
assert_config_entry_value(repository, "branch.test.remote", ".");
assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
/* unset */
cl_git_pass(git_branch_set_upstream(branch, NULL));
assert_config_entry_existence(repository, "branch.test.remote", false);
assert_config_entry_existence(repository, "branch.test.merge", false);
git_reference_free(branch);
cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/master"));
cl_git_pass(git_branch_set_upstream(branch, NULL));
assert_config_entry_existence(repository, "branch.test.remote", false);
assert_config_entry_existence(repository, "branch.test.merge", false);
git_reference_free(branch);
cl_git_sandbox_cleanup();
}
void test_refs_branches_upstream__no_fetch_refspec(void)
{
git_reference *ref, *branch;
git_repository *repo;
git_remote *remote;
git_config *cfg;
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_remote_create_with_fetchspec(&remote, repo, "matching", ".", NULL));
cl_git_pass(git_remote_add_push(repo, "matching", ":"));
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/test"));
cl_git_pass(git_reference_create(&ref, repo, "refs/remotes/matching/master", git_reference_target(branch), 1, "fetch"));
cl_git_fail(git_branch_set_upstream(branch, "matching/master"));
cl_assert_equal_s("could not determine remote for 'refs/remotes/matching/master'",
git_error_last()->message);
/* we can't set it automatically, so let's test the user setting it by hand */
cl_git_pass(git_repository_config(&cfg, repo));
cl_git_pass(git_config_set_string(cfg, "branch.test.remote", "matching"));
cl_git_pass(git_config_set_string(cfg, "branch.test.merge", "refs/heads/master"));
/* we still can't find it because there is no rule for that reference */
cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream(&ref, branch));
git_reference_free(ref);
git_reference_free(branch);
git_remote_free(remote);
cl_git_sandbox_cleanup();
}
+36
View File
@@ -0,0 +1,36 @@
#include "clar_libgit2.h"
#include "branch.h"
static git_repository *repo;
static git_buf upstream_name;
void test_refs_branches_upstreamname__initialize(void)
{
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
git_buf_init(&upstream_name, 0);
}
void test_refs_branches_upstreamname__cleanup(void)
{
git_buf_dispose(&upstream_name);
git_repository_free(repo);
repo = NULL;
}
void test_refs_branches_upstreamname__can_retrieve_the_remote_tracking_reference_name_of_a_local_branch(void)
{
cl_git_pass(git_branch_upstream_name(
&upstream_name, repo, "refs/heads/master"));
cl_assert_equal_s("refs/remotes/test/master", git_buf_cstr(&upstream_name));
}
void test_refs_branches_upstreamname__can_retrieve_the_local_upstream_reference_name_of_a_local_branch(void)
{
cl_git_pass(git_branch_upstream_name(
&upstream_name, repo, "refs/heads/track-local"));
cl_assert_equal_s("refs/heads/master", git_buf_cstr(&upstream_name));
}