]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/refs/branches/upstream.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / refs / branches / upstream.c
CommitLineData
fb910281 1#include "clar_libgit2.h"
9a97f49e 2#include "config/config_helpers.h"
fb910281 3#include "refs.h"
4
5static git_repository *repo;
a258d8e3 6static git_reference *branch, *upstream;
fb910281 7
a258d8e3 8void test_refs_branches_upstream__initialize(void)
fb910281 9{
10 cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
11
12 branch = NULL;
a258d8e3 13 upstream = NULL;
fb910281 14}
15
a258d8e3 16void test_refs_branches_upstream__cleanup(void)
fb910281 17{
a258d8e3 18 git_reference_free(upstream);
fb910281 19 git_reference_free(branch);
9094d30b 20 branch = NULL;
fb910281 21
22 git_repository_free(repo);
9094d30b 23 repo = NULL;
fb910281 24}
25
a258d8e3 26void test_refs_branches_upstream__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)
fb910281 27{
fb910281 28 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
29
a258d8e3 30 cl_git_pass(git_branch_upstream(&upstream, branch));
fb910281 31
a258d8e3 32 cl_assert_equal_s("refs/remotes/test/master", git_reference_name(upstream));
fb910281 33}
34
a258d8e3 35void test_refs_branches_upstream__can_retrieve_the_local_upstream_reference_of_a_local_branch(void)
fb910281 36{
fb910281 37 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/track-local"));
38
a258d8e3 39 cl_git_pass(git_branch_upstream(&upstream, branch));
fb910281 40
a258d8e3 41 cl_assert_equal_s("refs/heads/master", git_reference_name(upstream));
fb910281 42}
43
a258d8e3 44void test_refs_branches_upstream__cannot_retrieve_a_remote_upstream_reference_from_a_non_branch(void)
fb910281 45{
fb910281 46 cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));
47
a258d8e3 48 cl_git_fail(git_branch_upstream(&upstream, branch));
fb910281 49}
50
a258d8e3 51void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void)
fb910281 52{
fb910281 53 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees"));
54
a258d8e3 55 cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
fb910281 56}
e16fc07f 57
a258d8e3 58void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void)
e16fc07f 59{
e16fc07f 60 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/cannot-fetch"));
61
a258d8e3 62 cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
e16fc07f 63}
37849a8e 64
82374d98
CMN
65void 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);
ac3d33df 71 git_buf_dispose(&buf);
82374d98
CMN
72}
73
c25aa7cd
PP
74void 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
5915d700
CMN
99void 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
3da73c40 115static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name)
37849a8e 116{
117 git_reference *branch;
118
ac3d33df 119 cl_assert_equal_i(GIT_OBJECT_COMMIT, git_object_type((git_object*)target));
6bfb990d 120 cl_git_pass(git_branch_create(&branch, repository, entry_name, (git_commit*)target, 0));
37849a8e 121
a258d8e3 122 cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
37849a8e 123
124 git_reference_free(branch);
125}
126
a258d8e3 127void test_refs_branches_upstream__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void)
37849a8e 128{
129 git_reference *head;
130 git_repository *repository;
3da73c40 131 git_commit *target;
37849a8e 132
133 repository = cl_git_sandbox_init("testrepo.git");
134
135 cl_git_pass(git_repository_head(&head, repository));
ac3d33df 136 cl_git_pass(git_reference_peel(((git_object **)&target), head, GIT_OBJECT_COMMIT));
37849a8e 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
3da73c40 143 git_commit_free(target);
37849a8e 144
145 cl_git_sandbox_cleanup();
146}
d59942c2
CMN
147
148void test_refs_branches_upstream__set_unset_upstream(void)
149{
150 git_reference *branch;
151 git_repository *repository;
d59942c2
CMN
152
153 repository = cl_git_sandbox_init("testrepo.git");
154
3d42e9a3 155 /* remote */
d59942c2
CMN
156 cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
157 cl_git_pass(git_branch_set_upstream(branch, "test/master"));
158
9a97f49e
CMN
159 assert_config_entry_value(repository, "branch.test.remote", "test");
160 assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
d59942c2 161
24988894 162 git_reference_free(branch);
163
3d42e9a3
NV
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
9a97f49e
CMN
168 assert_config_entry_value(repository, "branch.test.remote", ".");
169 assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
3d42e9a3
NV
170
171 /* unset */
d59942c2 172 cl_git_pass(git_branch_set_upstream(branch, NULL));
9a97f49e
CMN
173 assert_config_entry_existence(repository, "branch.test.remote", false);
174 assert_config_entry_existence(repository, "branch.test.merge", false);
d59942c2
CMN
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));
9a97f49e
CMN
180 assert_config_entry_existence(repository, "branch.test.remote", false);
181 assert_config_entry_existence(repository, "branch.test.merge", false);
d59942c2
CMN
182
183 git_reference_free(branch);
184
d59942c2
CMN
185 cl_git_sandbox_cleanup();
186}
5014fe95
CMN
187
188void 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"));
8f0d5cde 203 cl_assert_equal_s("could not determine remote for 'refs/remotes/matching/master'",
ac3d33df 204 git_error_last()->message);
5014fe95
CMN
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}