]>
git.proxmox.com Git - libgit2.git/blob - tests/refs/branches/create.c
1 #include "clar_libgit2.h"
4 static git_repository
*repo
;
5 static git_commit
*target
;
6 static git_reference
*branch
;
8 void test_refs_branches_create__initialize(void)
10 cl_fixture_sandbox("testrepo.git");
11 cl_git_pass(git_repository_open(&repo
, "testrepo.git"));
16 void test_refs_branches_create__cleanup(void)
18 git_reference_free(branch
);
21 git_commit_free(target
);
24 git_repository_free(repo
);
27 cl_fixture_cleanup("testrepo.git");
30 static void retrieve_target_from_oid(git_commit
**out
, git_repository
*repo
, const char *sha
)
34 cl_git_pass(git_revparse_single(&obj
, repo
, sha
));
35 cl_git_pass(git_commit_lookup(out
, repo
, git_object_id(obj
)));
39 static void retrieve_known_commit(git_commit
**commit
, git_repository
*repo
)
41 retrieve_target_from_oid(commit
, repo
, "e90810b8df3");
44 #define NEW_BRANCH_NAME "new-branch-on-the-block"
46 void test_refs_branches_create__can_create_a_local_branch(void)
48 retrieve_known_commit(&target
, repo
);
50 cl_git_pass(git_branch_create(&branch
, repo
, NEW_BRANCH_NAME
, target
, 0, NULL
, NULL
));
51 cl_git_pass(git_oid_cmp(git_reference_target(branch
), git_commit_id(target
)));
54 void test_refs_branches_create__can_not_create_a_branch_if_its_name_collide_with_an_existing_one(void)
56 retrieve_known_commit(&target
, repo
);
58 cl_assert_equal_i(GIT_EEXISTS
, git_branch_create(&branch
, repo
, "br2", target
, 0, NULL
, NULL
));
61 void test_refs_branches_create__can_force_create_over_an_existing_branch(void)
63 retrieve_known_commit(&target
, repo
);
65 cl_git_pass(git_branch_create(&branch
, repo
, "br2", target
, 1, NULL
, NULL
));
66 cl_git_pass(git_oid_cmp(git_reference_target(branch
), git_commit_id(target
)));
67 cl_assert_equal_s("refs/heads/br2", git_reference_name(branch
));
70 void test_refs_branches_create__cannot_force_create_over_current_branch(void)
73 git_reference
*branch2
;
74 retrieve_known_commit(&target
, repo
);
76 cl_git_pass(git_branch_lookup(&branch2
, repo
, "master", GIT_BRANCH_LOCAL
));
77 cl_assert_equal_s("refs/heads/master", git_reference_name(branch2
));
78 cl_assert_equal_i(true, git_branch_is_head(branch2
));
79 oid
= git_reference_target(branch2
);
81 cl_git_fail_with(-1, git_branch_create(&branch
, repo
, "master", target
, 1, NULL
, NULL
));
83 cl_git_pass(git_branch_lookup(&branch
, repo
, "master", GIT_BRANCH_LOCAL
));
84 cl_assert_equal_s("refs/heads/master", git_reference_name(branch
));
85 cl_git_pass(git_oid_cmp(git_reference_target(branch
), oid
));
86 git_reference_free(branch2
);
89 void test_refs_branches_create__creating_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
91 retrieve_known_commit(&target
, repo
);
93 cl_assert_equal_i(GIT_EINVALIDSPEC
,
94 git_branch_create(&branch
, repo
, "inv@{id", target
, 0, NULL
, NULL
));
97 void test_refs_branches_create__creation_creates_new_reflog(void)
100 const git_reflog_entry
*entry
;
103 cl_git_pass(git_signature_now(&sig
, "me", "foo@example.com"));
105 retrieve_known_commit(&target
, repo
);
106 cl_git_pass(git_branch_create(&branch
, repo
, NEW_BRANCH_NAME
, target
, false, sig
, "create!"));
107 cl_git_pass(git_reflog_read(&log
, repo
, "refs/heads/" NEW_BRANCH_NAME
));
109 cl_assert_equal_i(1, git_reflog_entrycount(log
));
110 entry
= git_reflog_entry_byindex(log
, 0);
111 cl_assert_equal_s("create!", git_reflog_entry_message(entry
));
112 cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry
)->email
);
114 git_reflog_free(log
);
115 git_signature_free(sig
);
118 void test_refs_branches_create__default_reflog_message(void)
121 const git_reflog_entry
*entry
;
125 cl_git_pass(git_repository_config(&cfg
, repo
));
126 cl_git_pass(git_config_set_string(cfg
, "user.name", "Foo Bar"));
127 cl_git_pass(git_config_set_string(cfg
, "user.email", "foo@example.com"));
128 git_config_free(cfg
);
130 cl_git_pass(git_signature_default(&sig
, repo
));
132 retrieve_known_commit(&target
, repo
);
133 cl_git_pass(git_branch_create(&branch
, repo
, NEW_BRANCH_NAME
, target
, false, NULL
, NULL
));
134 cl_git_pass(git_reflog_read(&log
, repo
, "refs/heads/" NEW_BRANCH_NAME
));
136 entry
= git_reflog_entry_byindex(log
, 0);
137 cl_assert_equal_s("Branch: created", git_reflog_entry_message(entry
));
138 cl_assert_equal_s(sig
->email
, git_reflog_entry_committer(entry
)->email
);
140 git_reflog_free(log
);
141 git_signature_free(sig
);