]> git.proxmox.com Git - libgit2.git/blob - tests/network/remote/rename.c
Merge pull request #2211 from Yogu/retry-renaming-config
[libgit2.git] / tests / network / remote / rename.c
1 #include "clar_libgit2.h"
2 #include "config/config_helpers.h"
3
4 #include "repository.h"
5
6 static git_remote *_remote;
7 static git_repository *_repo;
8
9 void test_network_remote_rename__initialize(void)
10 {
11 _repo = cl_git_sandbox_init("testrepo.git");
12
13 cl_git_pass(git_remote_load(&_remote, _repo, "test"));
14 }
15
16 void test_network_remote_rename__cleanup(void)
17 {
18 git_remote_free(_remote);
19 _remote = NULL;
20
21 cl_git_sandbox_cleanup();
22 }
23
24 static int dont_call_me_cb(const char *fetch_refspec, void *payload)
25 {
26 GIT_UNUSED(fetch_refspec);
27 GIT_UNUSED(payload);
28
29 cl_assert(false);
30
31 return -1;
32 }
33
34 void test_network_remote_rename__renaming_a_remote_moves_related_configuration_section(void)
35 {
36 assert_config_entry_existence(_repo, "remote.test.fetch", true);
37 assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false);
38
39 cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
40
41 assert_config_entry_existence(_repo, "remote.test.fetch", false);
42 assert_config_entry_existence(_repo, "remote.just/renamed.fetch", true);
43 }
44
45 void test_network_remote_rename__renaming_a_remote_updates_branch_related_configuration_entries(void)
46 {
47 assert_config_entry_value(_repo, "branch.master.remote", "test");
48
49 cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
50
51 assert_config_entry_value(_repo, "branch.master.remote", "just/renamed");
52 }
53
54 void test_network_remote_rename__renaming_a_remote_updates_default_fetchrefspec(void)
55 {
56 cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
57
58 assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/heads/*:refs/remotes/just/renamed/*");
59 }
60
61 void test_network_remote_rename__renaming_a_remote_without_a_fetchrefspec_doesnt_create_one(void)
62 {
63 git_config *config;
64
65 git_remote_free(_remote);
66 cl_git_pass(git_repository_config__weakptr(&config, _repo));
67 cl_git_pass(git_config_delete_entry(config, "remote.test.fetch"));
68
69 cl_git_pass(git_remote_load(&_remote, _repo, "test"));
70
71 assert_config_entry_existence(_repo, "remote.test.fetch", false);
72
73 cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
74
75 assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false);
76 }
77
78 static int ensure_refspecs(const char* refspec_name, void *payload)
79 {
80 int i = 0;
81 bool found = false;
82 const char ** exp = (const char **)payload;
83
84 while (exp[i]) {
85 if (strcmp(exp[i++], refspec_name))
86 continue;
87
88 found = true;
89 break;
90 }
91
92 cl_assert(found);
93
94 return 0;
95 }
96
97 void test_network_remote_rename__renaming_a_remote_notifies_of_non_default_fetchrefspec(void)
98 {
99 git_config *config;
100
101 char *expected_refspecs[] = {
102 "+refs/*:refs/*",
103 NULL
104 };
105
106 git_remote_free(_remote);
107 cl_git_pass(git_repository_config__weakptr(&config, _repo));
108 cl_git_pass(git_config_set_string(config, "remote.test.fetch", "+refs/*:refs/*"));
109 cl_git_pass(git_remote_load(&_remote, _repo, "test"));
110
111 cl_git_pass(git_remote_rename(_remote, "just/renamed", ensure_refspecs, &expected_refspecs));
112
113 assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/*:refs/*");
114 }
115
116 void test_network_remote_rename__new_name_can_contain_dots(void)
117 {
118 cl_git_pass(git_remote_rename(_remote, "just.renamed", dont_call_me_cb, NULL));
119 cl_assert_equal_s("just.renamed", git_remote_name(_remote));
120 }
121
122 void test_network_remote_rename__new_name_must_conform_to_reference_naming_conventions(void)
123 {
124 cl_assert_equal_i(
125 GIT_EINVALIDSPEC,
126 git_remote_rename(_remote, "new@{name", dont_call_me_cb, NULL));
127 }
128
129 void test_network_remote_rename__renamed_name_is_persisted(void)
130 {
131 git_remote *renamed;
132 git_repository *another_repo;
133
134 cl_git_fail(git_remote_load(&renamed, _repo, "just/renamed"));
135
136 cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
137
138 cl_git_pass(git_repository_open(&another_repo, "testrepo.git"));
139 cl_git_pass(git_remote_load(&renamed, _repo, "just/renamed"));
140
141 git_remote_free(renamed);
142 git_repository_free(another_repo);
143 }
144
145 void test_network_remote_rename__cannot_overwrite_an_existing_remote(void)
146 {
147 cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(_remote, "test", dont_call_me_cb, NULL));
148 cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(_remote, "test_with_pushurl", dont_call_me_cb, NULL));
149 }
150
151 void test_network_remote_rename__renaming_a_remote_moves_the_underlying_reference(void)
152 {
153 git_reference *underlying;
154
155 cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed"));
156 cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/test/master"));
157 git_reference_free(underlying);
158
159 cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
160
161 cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/test/master"));
162 cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed/master"));
163 git_reference_free(underlying);
164 }
165
166 void test_network_remote_rename__cannot_rename_an_inmemory_remote(void)
167 {
168 git_remote *remote;
169
170 cl_git_pass(git_remote_create_anonymous(&remote, _repo, "file:///blah", NULL));
171 cl_git_fail(git_remote_rename(remote, "newname", NULL, NULL));
172
173 git_remote_free(remote);
174 }