]> git.proxmox.com Git - libgit2.git/blob - tests/refs/branches/create.c
Merge pull request #1997 from mgbowen/merge-options-init-fix
[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_object *obj;
33
34 cl_git_pass(git_revparse_single(&obj, repo, sha));
35 cl_git_pass(git_commit_lookup(out, repo, git_object_id(obj)));
36 git_object_free(obj);
37 }
38
39 static void retrieve_known_commit(git_commit **commit, git_repository *repo)
40 {
41 retrieve_target_from_oid(commit, repo, "e90810b8df3");
42 }
43
44 #define NEW_BRANCH_NAME "new-branch-on-the-block"
45
46 void test_refs_branches_create__can_create_a_local_branch(void)
47 {
48 retrieve_known_commit(&target, repo);
49
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)));
52 }
53
54 void test_refs_branches_create__can_not_create_a_branch_if_its_name_collide_with_an_existing_one(void)
55 {
56 retrieve_known_commit(&target, repo);
57
58 cl_assert_equal_i(GIT_EEXISTS, git_branch_create(&branch, repo, "br2", target, 0, NULL, NULL));
59 }
60
61 void test_refs_branches_create__can_force_create_over_an_existing_branch(void)
62 {
63 retrieve_known_commit(&target, repo);
64
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));
68 }
69
70 void test_refs_branches_create__cannot_force_create_over_current_branch(void)
71 {
72 const git_oid *oid;
73 git_reference *branch2;
74 retrieve_known_commit(&target, repo);
75
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);
80
81 cl_git_fail_with(-1, git_branch_create(&branch, repo, "master", target, 1, NULL, NULL));
82 branch = 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);
87 }
88
89 void test_refs_branches_create__creating_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
90 {
91 retrieve_known_commit(&target, repo);
92
93 cl_assert_equal_i(GIT_EINVALIDSPEC,
94 git_branch_create(&branch, repo, "inv@{id", target, 0, NULL, NULL));
95 }
96
97 void test_refs_branches_create__creation_creates_new_reflog(void)
98 {
99 git_reflog *log;
100 const git_reflog_entry *entry;
101 git_signature *sig;
102
103 cl_git_pass(git_signature_now(&sig, "me", "foo@example.com"));
104
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));
108
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);
113
114 git_reflog_free(log);
115 git_signature_free(sig);
116 }
117
118 void test_refs_branches_create__default_reflog_message(void)
119 {
120 git_reflog *log;
121 const git_reflog_entry *entry;
122 git_signature *sig;
123 git_config *cfg;
124
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);
129
130 cl_git_pass(git_signature_default(&sig, repo));
131
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));
135
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);
139
140 git_reflog_free(log);
141 git_signature_free(sig);
142 }