]> git.proxmox.com Git - libgit2.git/blob - tests/refs/branches/create.c
Merge pull request #1920 from libgit2/cmn/ref-with-log
[libgit2.git] / tests / refs / branches / create.c
1 #include "clar_libgit2.h"
2 #include "refs.h"
3
4 static git_repository *repo;
5 static git_commit *target;
6 static git_reference *branch;
7
8 void test_refs_branches_create__initialize(void)
9 {
10 cl_fixture_sandbox("testrepo.git");
11 cl_git_pass(git_repository_open(&repo, "testrepo.git"));
12
13 branch = NULL;
14 }
15
16 void test_refs_branches_create__cleanup(void)
17 {
18 git_reference_free(branch);
19 branch = NULL;
20
21 git_commit_free(target);
22 target = NULL;
23
24 git_repository_free(repo);
25 repo = NULL;
26
27 cl_fixture_cleanup("testrepo.git");
28 }
29
30 static void retrieve_target_from_oid(git_commit **out, git_repository *repo, const char *sha)
31 {
32 git_oid oid;
33
34 cl_git_pass(git_oid_fromstr(&oid, sha));
35 cl_git_pass(git_commit_lookup(out, repo, &oid));
36 }
37
38 static void retrieve_known_commit(git_commit **commit, git_repository *repo)
39 {
40 retrieve_target_from_oid(commit, repo, "e90810b8df3e80c413d903f631643c716887138d");
41 }
42
43 #define NEW_BRANCH_NAME "new-branch-on-the-block"
44
45 void test_refs_branches_create__can_create_a_local_branch(void)
46 {
47 retrieve_known_commit(&target, repo);
48
49 cl_git_pass(git_branch_create(&branch, repo, NEW_BRANCH_NAME, target, 0));
50 cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
51 }
52
53 void test_refs_branches_create__can_not_create_a_branch_if_its_name_collide_with_an_existing_one(void)
54 {
55 retrieve_known_commit(&target, repo);
56
57 cl_assert_equal_i(GIT_EEXISTS, git_branch_create(&branch, repo, "br2", target, 0));
58 }
59
60 void test_refs_branches_create__can_force_create_over_an_existing_branch(void)
61 {
62 retrieve_known_commit(&target, repo);
63
64 cl_git_pass(git_branch_create(&branch, repo, "br2", target, 1));
65 cl_git_pass(git_oid_cmp(git_reference_target(branch), git_commit_id(target)));
66 cl_assert_equal_s("refs/heads/br2", git_reference_name(branch));
67 }
68
69
70 void test_refs_branches_create__creating_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
71 {
72 retrieve_known_commit(&target, repo);
73
74 cl_assert_equal_i(GIT_EINVALIDSPEC,
75 git_branch_create(&branch, repo, "inv@{id", target, 0));
76 }
77