1 #include "clar_libgit2.h"
2 #include "config/config_helpers.h"
3 #include "refs.h"
4
5 static git_repository *repo;
6 static git_reference *branch, *upstream;
7
test_refs_branches_upstream__initialize(void)8 void test_refs_branches_upstream__initialize(void)
9 {
10 cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
11
12 branch = NULL;
13 upstream = NULL;
14 }
15
test_refs_branches_upstream__cleanup(void)16 void test_refs_branches_upstream__cleanup(void)
17 {
18 git_reference_free(upstream);
19 git_reference_free(branch);
20 branch = NULL;
21
22 git_repository_free(repo);
23 repo = NULL;
24 }
25
test_refs_branches_upstream__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)26 void test_refs_branches_upstream__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)
27 {
28 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
29
30 cl_git_pass(git_branch_upstream(&upstream, branch));
31
32 cl_assert_equal_s("refs/remotes/test/master", git_reference_name(upstream));
33 }
34
test_refs_branches_upstream__can_retrieve_the_local_upstream_reference_of_a_local_branch(void)35 void test_refs_branches_upstream__can_retrieve_the_local_upstream_reference_of_a_local_branch(void)
36 {
37 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/track-local"));
38
39 cl_git_pass(git_branch_upstream(&upstream, branch));
40
41 cl_assert_equal_s("refs/heads/master", git_reference_name(upstream));
42 }
43
test_refs_branches_upstream__cannot_retrieve_a_remote_upstream_reference_from_a_non_branch(void)44 void test_refs_branches_upstream__cannot_retrieve_a_remote_upstream_reference_from_a_non_branch(void)
45 {
46 cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));
47
48 cl_git_fail(git_branch_upstream(&upstream, branch));
49 }
50
test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void)51 void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void)
52 {
53 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees"));
54
55 cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
56 }
57
test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void)58 void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void)
59 {
60 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/cannot-fetch"));
61
62 cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
63 }
64
test_refs_branches_upstream__upstream_remote(void)65 void test_refs_branches_upstream__upstream_remote(void)
66 {
67 git_buf buf = GIT_BUF_INIT;
68
69 cl_git_pass(git_branch_upstream_remote(&buf, repo, "refs/heads/master"));
70 cl_assert_equal_s("test", buf.ptr);
71 git_buf_dispose(&buf);
72 }
73
test_refs_branches_upstream__upstream_merge(void)74 void test_refs_branches_upstream__upstream_merge(void)
75 {
76 git_reference *branch;
77 git_repository *repository;
78 git_buf buf = GIT_BUF_INIT;
79
80 repository = cl_git_sandbox_init("testrepo.git");
81
82 /* check repository */
83 cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
84 cl_git_pass(git_branch_set_upstream(branch, "test/master"));
85
86 assert_config_entry_value(repository, "branch.test.remote", "test");
87 assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
88
89 git_reference_free(branch);
90
91 /* check merge branch */
92 cl_git_pass(git_branch_upstream_merge(&buf, repository, "refs/heads/test"));
93 cl_assert_equal_s("refs/heads/master", buf.ptr);
94 git_buf_dispose(&buf);
95
96 cl_git_sandbox_cleanup();
97 }
98
test_refs_branches_upstream__upstream_remote_empty_value(void)99 void test_refs_branches_upstream__upstream_remote_empty_value(void)
100 {
101 git_repository *repository;
102 git_config *cfg;
103 git_buf buf = GIT_BUF_INIT;
104
105 repository = cl_git_sandbox_init("testrepo.git");
106 cl_git_pass(git_repository_config(&cfg, repository));
107 cl_git_pass(git_config_set_string(cfg, "branch.master.remote", ""));
108 cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream_remote(&buf, repository, "refs/heads/master"));
109
110 cl_git_pass(git_config_delete_entry(cfg, "branch.master.remote"));
111 cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream_remote(&buf, repository, "refs/heads/master"));
112 cl_git_sandbox_cleanup();
113 }
114
assert_merge_and_or_remote_key_missing(git_repository * repository,const git_commit * target,const char * entry_name)115 static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name)
116 {
117 git_reference *branch;
118
119 cl_assert_equal_i(GIT_OBJECT_COMMIT, git_object_type((git_object*)target));
120 cl_git_pass(git_branch_create(&branch, repository, entry_name, (git_commit*)target, 0));
121
122 cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
123
124 git_reference_free(branch);
125 }
126
test_refs_branches_upstream__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void)127 void test_refs_branches_upstream__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void)
128 {
129 git_reference *head;
130 git_repository *repository;
131 git_commit *target;
132
133 repository = cl_git_sandbox_init("testrepo.git");
134
135 cl_git_pass(git_repository_head(&head, repository));
136 cl_git_pass(git_reference_peel(((git_object **)&target), head, GIT_OBJECT_COMMIT));
137 git_reference_free(head);
138
139 assert_merge_and_or_remote_key_missing(repository, target, "remoteless");
140 assert_merge_and_or_remote_key_missing(repository, target, "mergeless");
141 assert_merge_and_or_remote_key_missing(repository, target, "mergeandremoteless");
142
143 git_commit_free(target);
144
145 cl_git_sandbox_cleanup();
146 }
147
test_refs_branches_upstream__set_unset_upstream(void)148 void test_refs_branches_upstream__set_unset_upstream(void)
149 {
150 git_reference *branch;
151 git_repository *repository;
152
153 repository = cl_git_sandbox_init("testrepo.git");
154
155 /* remote */
156 cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
157 cl_git_pass(git_branch_set_upstream(branch, "test/master"));
158
159 assert_config_entry_value(repository, "branch.test.remote", "test");
160 assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
161
162 git_reference_free(branch);
163
164 /* local */
165 cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
166 cl_git_pass(git_branch_set_upstream(branch, "master"));
167
168 assert_config_entry_value(repository, "branch.test.remote", ".");
169 assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
170
171 /* unset */
172 cl_git_pass(git_branch_set_upstream(branch, NULL));
173 assert_config_entry_existence(repository, "branch.test.remote", false);
174 assert_config_entry_existence(repository, "branch.test.merge", false);
175
176 git_reference_free(branch);
177
178 cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/master"));
179 cl_git_pass(git_branch_set_upstream(branch, NULL));
180 assert_config_entry_existence(repository, "branch.test.remote", false);
181 assert_config_entry_existence(repository, "branch.test.merge", false);
182
183 git_reference_free(branch);
184
185 cl_git_sandbox_cleanup();
186 }
187
test_refs_branches_upstream__no_fetch_refspec(void)188 void test_refs_branches_upstream__no_fetch_refspec(void)
189 {
190 git_reference *ref, *branch;
191 git_repository *repo;
192 git_remote *remote;
193 git_config *cfg;
194
195 repo = cl_git_sandbox_init("testrepo.git");
196
197 cl_git_pass(git_remote_create_with_fetchspec(&remote, repo, "matching", ".", NULL));
198 cl_git_pass(git_remote_add_push(repo, "matching", ":"));
199
200 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/test"));
201 cl_git_pass(git_reference_create(&ref, repo, "refs/remotes/matching/master", git_reference_target(branch), 1, "fetch"));
202 cl_git_fail(git_branch_set_upstream(branch, "matching/master"));
203 cl_assert_equal_s("could not determine remote for 'refs/remotes/matching/master'",
204 git_error_last()->message);
205
206 /* we can't set it automatically, so let's test the user setting it by hand */
207 cl_git_pass(git_repository_config(&cfg, repo));
208 cl_git_pass(git_config_set_string(cfg, "branch.test.remote", "matching"));
209 cl_git_pass(git_config_set_string(cfg, "branch.test.merge", "refs/heads/master"));
210 /* we still can't find it because there is no rule for that reference */
211 cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream(&ref, branch));
212
213 git_reference_free(ref);
214 git_reference_free(branch);
215 git_remote_free(remote);
216
217 cl_git_sandbox_cleanup();
218 }
219