]> git.proxmox.com Git - libgit2.git/blame - tests/refs/branches/upstream.c
New upstream version 0.27.7+dfsg.1
[libgit2.git] / tests / 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);
71 git_buf_free(&buf);
72}
73
5915d700
CMN
74void test_refs_branches_upstream__upstream_remote_empty_value(void)
75{
76 git_repository *repository;
77 git_config *cfg;
78 git_buf buf = GIT_BUF_INIT;
79
80 repository = cl_git_sandbox_init("testrepo.git");
81 cl_git_pass(git_repository_config(&cfg, repository));
82 cl_git_pass(git_config_set_string(cfg, "branch.master.remote", ""));
83 cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream_remote(&buf, repository, "refs/heads/master"));
84
85 cl_git_pass(git_config_delete_entry(cfg, "branch.master.remote"));
86 cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream_remote(&buf, repository, "refs/heads/master"));
87 cl_git_sandbox_cleanup();
88}
89
3da73c40 90static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name)
37849a8e 91{
92 git_reference *branch;
93
72629a10 94 cl_assert_equal_i(GIT_OBJ_COMMIT, git_object_type((git_object*)target));
6bfb990d 95 cl_git_pass(git_branch_create(&branch, repository, entry_name, (git_commit*)target, 0));
37849a8e 96
a258d8e3 97 cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
37849a8e 98
99 git_reference_free(branch);
100}
101
a258d8e3 102void test_refs_branches_upstream__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void)
37849a8e 103{
104 git_reference *head;
105 git_repository *repository;
3da73c40 106 git_commit *target;
37849a8e 107
108 repository = cl_git_sandbox_init("testrepo.git");
109
110 cl_git_pass(git_repository_head(&head, repository));
3da73c40 111 cl_git_pass(git_reference_peel(((git_object **)&target), head, GIT_OBJ_COMMIT));
37849a8e 112 git_reference_free(head);
113
114 assert_merge_and_or_remote_key_missing(repository, target, "remoteless");
115 assert_merge_and_or_remote_key_missing(repository, target, "mergeless");
116 assert_merge_and_or_remote_key_missing(repository, target, "mergeandremoteless");
117
3da73c40 118 git_commit_free(target);
37849a8e 119
120 cl_git_sandbox_cleanup();
121}
d59942c2
CMN
122
123void test_refs_branches_upstream__set_unset_upstream(void)
124{
125 git_reference *branch;
126 git_repository *repository;
d59942c2
CMN
127
128 repository = cl_git_sandbox_init("testrepo.git");
129
3d42e9a3 130 /* remote */
d59942c2
CMN
131 cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
132 cl_git_pass(git_branch_set_upstream(branch, "test/master"));
133
9a97f49e
CMN
134 assert_config_entry_value(repository, "branch.test.remote", "test");
135 assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
d59942c2 136
24988894 137 git_reference_free(branch);
138
3d42e9a3
NV
139 /* local */
140 cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
141 cl_git_pass(git_branch_set_upstream(branch, "master"));
142
9a97f49e
CMN
143 assert_config_entry_value(repository, "branch.test.remote", ".");
144 assert_config_entry_value(repository, "branch.test.merge", "refs/heads/master");
3d42e9a3
NV
145
146 /* unset */
d59942c2 147 cl_git_pass(git_branch_set_upstream(branch, NULL));
9a97f49e
CMN
148 assert_config_entry_existence(repository, "branch.test.remote", false);
149 assert_config_entry_existence(repository, "branch.test.merge", false);
d59942c2
CMN
150
151 git_reference_free(branch);
152
153 cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/master"));
154 cl_git_pass(git_branch_set_upstream(branch, NULL));
9a97f49e
CMN
155 assert_config_entry_existence(repository, "branch.test.remote", false);
156 assert_config_entry_existence(repository, "branch.test.merge", false);
d59942c2
CMN
157
158 git_reference_free(branch);
159
d59942c2
CMN
160 cl_git_sandbox_cleanup();
161}
5014fe95
CMN
162
163void test_refs_branches_upstream__no_fetch_refspec(void)
164{
165 git_reference *ref, *branch;
166 git_repository *repo;
167 git_remote *remote;
168 git_config *cfg;
169
170 repo = cl_git_sandbox_init("testrepo.git");
171
172 cl_git_pass(git_remote_create_with_fetchspec(&remote, repo, "matching", ".", NULL));
173 cl_git_pass(git_remote_add_push(repo, "matching", ":"));
174
175 cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/test"));
176 cl_git_pass(git_reference_create(&ref, repo, "refs/remotes/matching/master", git_reference_target(branch), 1, "fetch"));
177 cl_git_fail(git_branch_set_upstream(branch, "matching/master"));
8f0d5cde 178 cl_assert_equal_s("could not determine remote for 'refs/remotes/matching/master'",
5014fe95
CMN
179 giterr_last()->message);
180
181 /* we can't set it automatically, so let's test the user setting it by hand */
182 cl_git_pass(git_repository_config(&cfg, repo));
183 cl_git_pass(git_config_set_string(cfg, "branch.test.remote", "matching"));
184 cl_git_pass(git_config_set_string(cfg, "branch.test.merge", "refs/heads/master"));
185 /* we still can't find it because there is no rule for that reference */
186 cl_git_fail_with(GIT_ENOTFOUND, git_branch_upstream(&ref, branch));
187
188 git_reference_free(ref);
189 git_reference_free(branch);
190 git_remote_free(remote);
191
192 cl_git_sandbox_cleanup();
193}