summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2012-07-19 15:39:16 +0200
committernulltoken <emeric.fermas@gmail.com>2012-07-24 16:09:43 +0200
commitb308c11e4ee7d05df4906e04b4008615f41e069c (patch)
tree405f8438f67330755541c935c52f4f0fd3014951
parent326ca710a04cc43d3d5abba6ceb0452c126b7d1a (diff)
downloadlibgit2-b308c11e4ee7d05df4906e04b4008615f41e069c.tar.gz
branch: change git_branch_create() to make it return a reference
-rw-r--r--include/git2/branch.h7
-rw-r--r--src/branch.c14
-rw-r--r--tests-clar/refs/branches/create.c56
3 files changed, 23 insertions, 54 deletions
diff --git a/include/git2/branch.h b/include/git2/branch.h
index 8884df15a..7442ece03 100644
--- a/include/git2/branch.h
+++ b/include/git2/branch.h
@@ -26,9 +26,9 @@ GIT_BEGIN_DECL
* this target commit. If `force` is true and a reference
* already exists with the given name, it'll be replaced.
*
- * @param oid_out Pointer where to store the OID of the target commit.
+ * The returned reference must be freed by the user.
*
- * @param repo Repository where to store the branch.
+ * @param ref_out Pointer where to store the underlying reference.
*
* @param branch_name Name for the branch; this name is
* validated for consistency. It should also not conflict with
@@ -46,8 +46,7 @@ GIT_BEGIN_DECL
* pointing to the provided target commit.
*/
GIT_EXTERN(int) git_branch_create(
- git_oid *oid_out,
- git_repository *repo,
+ git_reference **ref_out,
const char *branch_name,
const git_object *target,
int force);
diff --git a/src/branch.c b/src/branch.c
index a59577d67..789f52bb6 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -49,8 +49,7 @@ static int create_error_invalid(const char *msg)
}
int git_branch_create(
- git_oid *oid_out,
- git_repository *repo,
+ git_reference **ref_out,
const char *branch_name,
const git_object *target,
int force)
@@ -61,10 +60,7 @@ int git_branch_create(
git_buf canonical_branch_name = GIT_BUF_INIT;
int error = -1;
- assert(repo && branch_name && target && oid_out);
-
- if (git_object_owner(target) != repo)
- return create_error_invalid("The given target does not belong to this repository");
+ assert(branch_name && target && ref_out);
target_type = git_object_type(target);
@@ -91,17 +87,17 @@ int git_branch_create(
if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0)
goto cleanup;
- if (git_reference_create_oid(&branch, repo, git_buf_cstr(&canonical_branch_name), git_object_id(commit), force) < 0)
+ if (git_reference_create_oid(&branch, git_object_owner(commit),
+ git_buf_cstr(&canonical_branch_name), git_object_id(commit), force) < 0)
goto cleanup;
- git_oid_cpy(oid_out, git_reference_oid(branch));
+ *ref_out = branch;
error = 0;
cleanup:
if (target_type == GIT_OBJ_TAG)
git_object_free(commit);
- git_reference_free(branch);
git_buf_free(&canonical_branch_name);
return error;
}
diff --git a/tests-clar/refs/branches/create.c b/tests-clar/refs/branches/create.c
index 4f5a61b40..d53904303 100644
--- a/tests-clar/refs/branches/create.c
+++ b/tests-clar/refs/branches/create.c
@@ -2,17 +2,21 @@
#include "refs.h"
static git_repository *repo;
-static git_oid branch_target_oid;
static git_object *target;
+static git_reference *branch;
void test_refs_branches_create__initialize(void)
{
cl_fixture_sandbox("testrepo.git");
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
+
+ branch = NULL;
}
void test_refs_branches_create__cleanup(void)
{
+ git_reference_free(branch);
+
git_object_free(target);
git_repository_free(repo);
@@ -38,54 +42,24 @@ void test_refs_branches_create__can_create_a_local_branch(void)
{
retrieve_known_commit(&target, repo);
- cl_git_pass(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
- cl_git_pass(git_oid_cmp(&branch_target_oid, git_object_id(target)));
-}
-
-void test_refs_branches_create__creating_a_local_branch_triggers_the_creation_of_a_new_direct_reference(void)
-{
- git_reference *branch;
-
- retrieve_known_commit(&target, repo);
-
- cl_git_fail(git_reference_lookup(&branch, repo, GIT_REFS_HEADS_DIR NEW_BRANCH_NAME));
-
- cl_git_pass(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
-
- cl_git_pass(git_reference_lookup(&branch, repo, GIT_REFS_HEADS_DIR NEW_BRANCH_NAME));
- cl_assert(git_reference_type(branch) == GIT_REF_OID);
-
- git_reference_free(branch);
+ cl_git_pass(git_branch_create(&branch, NEW_BRANCH_NAME, target, 0));
+ cl_git_pass(git_oid_cmp(git_reference_oid(branch), git_object_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_git_fail(git_branch_create(&branch_target_oid, repo, "br2", target, 0));
+ cl_git_fail(git_branch_create(&branch, "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_target_oid, repo, "br2", target, 1));
- cl_git_pass(git_oid_cmp(&branch_target_oid, git_object_id(target)));
-}
-
-void test_refs_branches_create__can_not_create_a_branch_pointing_at_an_object_unknown_from_the_repository(void)
-{
- git_repository *repo2;
-
- /* Open another instance of the same repository */
- cl_git_pass(git_repository_open(&repo2, cl_fixture("testrepo.git")));
-
- /* Retrieve a commit object from this different repository */
- retrieve_known_commit(&target, repo2);
-
- cl_git_fail(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
-
- git_repository_free(repo2);
+ cl_git_pass(git_branch_create(&branch, "br2", target, 1));
+ cl_git_pass(git_oid_cmp(git_reference_oid(branch), git_object_id(target)));
+ cl_assert_equal_s("refs/heads/br2", git_reference_name(branch));
}
void test_refs_branches_create__creating_a_branch_targeting_a_tag_dereferences_it_to_its_commit(void)
@@ -93,8 +67,8 @@ void test_refs_branches_create__creating_a_branch_targeting_a_tag_dereferences_i
/* b25fa35 is a tag, pointing to another tag which points to a commit */
retrieve_target_from_oid(&target, repo, "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
- cl_git_pass(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
- cl_git_pass(git_oid_streq(&branch_target_oid, "e90810b8df3e80c413d903f631643c716887138d"));
+ cl_git_pass(git_branch_create(&branch, NEW_BRANCH_NAME, target, 0));
+ cl_git_pass(git_oid_streq(git_reference_oid(branch), "e90810b8df3e80c413d903f631643c716887138d"));
}
void test_refs_branches_create__can_not_create_a_branch_pointing_to_a_non_commit_object(void)
@@ -102,11 +76,11 @@ void test_refs_branches_create__can_not_create_a_branch_pointing_to_a_non_commit
/* 53fc32d is the tree of commit e90810b */
retrieve_target_from_oid(&target, repo, "53fc32d17276939fc79ed05badaef2db09990016");
- cl_git_fail(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
+ cl_git_fail(git_branch_create(&branch, NEW_BRANCH_NAME, target, 0));
git_object_free(target);
/* 521d87c is an annotated tag pointing to a blob */
retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91");
- cl_git_fail(git_branch_create(&branch_target_oid, repo, NEW_BRANCH_NAME, target, 0));
+ cl_git_fail(git_branch_create(&branch, NEW_BRANCH_NAME, target, 0));
}