Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72bca13

Browse files
committed
remote: return problem refspecs instead of using a callback
There is no reason why we need to use a callback here. A string array fits better with the usage, as this is not an event and we don't need anything from the user.
1 parent 61dcfe1 commit 72bca13

File tree

4 files changed

+87
-56
lines changed

4 files changed

+87
-56
lines changed

include/git2/remote.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,9 @@ GIT_EXTERN(void) git_remote_set_autotag(
572572
*
573573
* A temporary in-memory remote cannot be given a name with this method.
574574
*
575+
* @param problems non-default refspecs cannot be renamed and will be
576+
* stored here for further processing by the caller. Always free this
577+
* strarray on succesful return.
575578
* @param remote the remote to rename
576579
* @param new_name the new name the remote should bear
577580
* @param callback Optional callback to notify the consumer of fetch refspecs
@@ -580,10 +583,9 @@ GIT_EXTERN(void) git_remote_set_autotag(
580583
* @return 0, GIT_EINVALIDSPEC, GIT_EEXISTS or an error code
581584
*/
582585
GIT_EXTERN(int) git_remote_rename(
586+
git_strarray *problems,
583587
git_remote *remote,
584-
const char *new_name,
585-
git_remote_rename_problem_cb callback,
586-
void *payload);
588+
const char *new_name);
587589

588590
/**
589591
* Retrieve the update FETCH_HEAD setting.

src/remote.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,11 +1419,7 @@ static int rename_remote_references(
14191419
return (error == GIT_ITEROVER) ? 0 : error;
14201420
}
14211421

1422-
static int rename_fetch_refspecs(
1423-
git_remote *remote,
1424-
const char *new_name,
1425-
int (*callback)(const char *problematic_refspec, void *payload),
1426-
void *payload)
1422+
static int rename_fetch_refspecs(git_vector *problems, git_remote *remote, const char *new_name)
14271423
{
14281424
git_config *config;
14291425
git_buf base = GIT_BUF_INIT, var = GIT_BUF_INIT, val = GIT_BUF_INIT;
@@ -1434,6 +1430,9 @@ static int rename_fetch_refspecs(
14341430
if ((error = git_repository_config__weakptr(&config, remote->repo)) < 0)
14351431
return error;
14361432

1433+
if ((error = git_vector_init(problems, 1, NULL)) < 0)
1434+
return error;
1435+
14371436
if ((error = git_buf_printf(
14381437
&base, "+refs/heads/*:refs/remotes/%s/*", remote->name)) < 0)
14391438
return error;
@@ -1444,11 +1443,13 @@ static int rename_fetch_refspecs(
14441443

14451444
/* Does the dst part of the refspec follow the expected format? */
14461445
if (strcmp(git_buf_cstr(&base), spec->string)) {
1446+
char *dup;
14471447

1448-
if ((error = callback(spec->string, payload)) != 0) {
1449-
giterr_set_after_callback(error);
1448+
dup = git__strdup(spec->string);
1449+
GITERR_CHECK_ALLOC(dup);
1450+
1451+
if ((error = git_vector_insert(problems, dup)) < 0)
14501452
break;
1451-
}
14521453

14531454
continue;
14541455
}
@@ -1474,19 +1475,25 @@ static int rename_fetch_refspecs(
14741475
git_buf_free(&base);
14751476
git_buf_free(&var);
14761477
git_buf_free(&val);
1478+
1479+
if (error < 0) {
1480+
char *str;
1481+
git_vector_foreach(problems, i, str)
1482+
git__free(str);
1483+
1484+
git_vector_free(problems);
1485+
}
1486+
14771487
return error;
14781488
}
14791489

1480-
int git_remote_rename(
1481-
git_remote *remote,
1482-
const char *new_name,
1483-
git_remote_rename_problem_cb callback,
1484-
void *payload)
1490+
int git_remote_rename(git_strarray *out, git_remote *remote, const char *new_name)
14851491
{
14861492
int error;
1493+
git_vector problem_refspecs;
14871494
char *tmp, *dup;
14881495

1489-
assert(remote && new_name);
1496+
assert(out && remote && new_name);
14901497

14911498
if (!remote->name) {
14921499
giterr_set(GITERR_INVALID, "Can't rename an anonymous remote.");
@@ -1508,9 +1515,12 @@ int git_remote_rename(
15081515
if ((error = rename_remote_references(remote->repo, remote->name, new_name)) < 0)
15091516
return error;
15101517

1511-
if ((error = rename_fetch_refspecs(remote, new_name, callback, payload)) < 0)
1518+
if ((error = rename_fetch_refspecs(&problem_refspecs, remote, new_name)) < 0)
15121519
return error;
15131520

1521+
out->count = problem_refspecs.length;
1522+
out->strings = (char **) problem_refspecs.contents;
1523+
15141524
dup = git__strdup(new_name);
15151525
GITERR_CHECK_ALLOC(dup);
15161526

tests/network/remote/rename.c

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,47 @@ static int dont_call_me_cb(const char *fetch_refspec, void *payload)
3333

3434
void test_network_remote_rename__renaming_a_remote_moves_related_configuration_section(void)
3535
{
36+
git_strarray problems = {0};
37+
3638
assert_config_entry_existence(_repo, "remote.test.fetch", true);
3739
assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false);
3840

39-
cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
41+
cl_git_pass(git_remote_rename(&problems, _remote, "just/renamed"));
42+
cl_assert_equal_i(0, problems.count);
43+
git_strarray_free(&problems);
4044

4145
assert_config_entry_existence(_repo, "remote.test.fetch", false);
4246
assert_config_entry_existence(_repo, "remote.just/renamed.fetch", true);
4347
}
4448

4549
void test_network_remote_rename__renaming_a_remote_updates_branch_related_configuration_entries(void)
4650
{
51+
git_strarray problems = {0};
52+
4753
assert_config_entry_value(_repo, "branch.master.remote", "test");
4854

49-
cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
55+
cl_git_pass(git_remote_rename(&problems, _remote, "just/renamed"));
56+
cl_assert_equal_i(0, problems.count);
57+
git_strarray_free(&problems);
5058

5159
assert_config_entry_value(_repo, "branch.master.remote", "just/renamed");
5260
}
5361

5462
void test_network_remote_rename__renaming_a_remote_updates_default_fetchrefspec(void)
5563
{
56-
cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
64+
git_strarray problems = {0};
65+
66+
cl_git_pass(git_remote_rename(&problems, _remote, "just/renamed"));
67+
cl_assert_equal_i(0, problems.count);
68+
git_strarray_free(&problems);
5769

5870
assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/heads/*:refs/remotes/just/renamed/*");
5971
}
6072

6173
void test_network_remote_rename__renaming_a_remote_without_a_fetchrefspec_doesnt_create_one(void)
6274
{
6375
git_config *config;
76+
git_strarray problems = {0};
6477

6578
git_remote_free(_remote);
6679
cl_git_pass(git_repository_config__weakptr(&config, _repo));
@@ -70,70 +83,64 @@ void test_network_remote_rename__renaming_a_remote_without_a_fetchrefspec_doesnt
7083

7184
assert_config_entry_existence(_repo, "remote.test.fetch", false);
7285

73-
cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
86+
cl_git_pass(git_remote_rename(&problems, _remote, "just/renamed"));
87+
cl_assert_equal_i(0, problems.count);
88+
git_strarray_free(&problems);
7489

7590
assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false);
7691
}
7792

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-
9793
void test_network_remote_rename__renaming_a_remote_notifies_of_non_default_fetchrefspec(void)
9894
{
9995
git_config *config;
10096

101-
char *expected_refspecs[] = {
102-
"+refs/*:refs/*",
103-
NULL
104-
};
97+
git_strarray problems = {0};
10598

10699
git_remote_free(_remote);
107100
cl_git_pass(git_repository_config__weakptr(&config, _repo));
108101
cl_git_pass(git_config_set_string(config, "remote.test.fetch", "+refs/*:refs/*"));
109102
cl_git_pass(git_remote_load(&_remote, _repo, "test"));
110103

111-
cl_git_pass(git_remote_rename(_remote, "just/renamed", ensure_refspecs, &expected_refspecs));
104+
cl_git_pass(git_remote_rename(&problems, _remote, "just/renamed"));
105+
cl_assert_equal_i(1, problems.count);
106+
cl_assert_equal_s("+refs/*:refs/*", problems.strings[0]);
107+
git_strarray_free(&problems);
112108

113109
assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/*:refs/*");
110+
111+
git_strarray_free(&problems);
114112
}
115113

116114
void test_network_remote_rename__new_name_can_contain_dots(void)
117115
{
118-
cl_git_pass(git_remote_rename(_remote, "just.renamed", dont_call_me_cb, NULL));
116+
git_strarray problems = {0};
117+
118+
cl_git_pass(git_remote_rename(&problems, _remote, "just.renamed"));
119+
cl_assert_equal_i(0, problems.count);
120+
git_strarray_free(&problems);
119121
cl_assert_equal_s("just.renamed", git_remote_name(_remote));
120122
}
121123

122124
void test_network_remote_rename__new_name_must_conform_to_reference_naming_conventions(void)
123125
{
126+
git_strarray problems = {0};
127+
124128
cl_assert_equal_i(
125129
GIT_EINVALIDSPEC,
126-
git_remote_rename(_remote, "new@{name", dont_call_me_cb, NULL));
130+
git_remote_rename(&problems, _remote, "new@{name"));
127131
}
128132

129133
void test_network_remote_rename__renamed_name_is_persisted(void)
130134
{
131135
git_remote *renamed;
132136
git_repository *another_repo;
137+
git_strarray problems = {0};
133138

134139
cl_git_fail(git_remote_load(&renamed, _repo, "just/renamed"));
135140

136-
cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
141+
cl_git_pass(git_remote_rename(&problems, _remote, "just/renamed"));
142+
cl_assert_equal_i(0, problems.count);
143+
git_strarray_free(&problems);
137144

138145
cl_git_pass(git_repository_open(&another_repo, "testrepo.git"));
139146
cl_git_pass(git_remote_load(&renamed, _repo, "just/renamed"));
@@ -144,19 +151,24 @@ void test_network_remote_rename__renamed_name_is_persisted(void)
144151

145152
void test_network_remote_rename__cannot_overwrite_an_existing_remote(void)
146153
{
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));
154+
git_strarray problems = {0};
155+
156+
cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(&problems, _remote, "test"));
157+
cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(&problems, _remote, "test_with_pushurl"));
149158
}
150159

151160
void test_network_remote_rename__renaming_a_remote_moves_the_underlying_reference(void)
152161
{
153162
git_reference *underlying;
163+
git_strarray problems = {0};
154164

155165
cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed"));
156166
cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/test/master"));
157167
git_reference_free(underlying);
158168

159-
cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL));
169+
cl_git_pass(git_remote_rename(&problems, _remote, "just/renamed"));
170+
cl_assert_equal_i(0, problems.count);
171+
git_strarray_free(&problems);
160172

161173
cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/test/master"));
162174
cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed/master"));
@@ -166,10 +178,12 @@ void test_network_remote_rename__renaming_a_remote_moves_the_underlying_referenc
166178
void test_network_remote_rename__cannot_rename_an_inmemory_remote(void)
167179
{
168180
git_remote *remote;
181+
git_strarray problems = {0};
169182

170183
cl_git_pass(git_remote_create_anonymous(&remote, _repo, "file:///blah", NULL));
171-
cl_git_fail(git_remote_rename(remote, "newname", NULL, NULL));
184+
cl_git_fail(git_remote_rename(&problems, remote, "newname"));
172185

186+
git_strarray_free(&problems);
173187
git_remote_free(remote);
174188
}
175189

@@ -181,15 +195,17 @@ void test_network_remote_rename__overwrite_ref_in_target(void)
181195
git_reference *ref;
182196
git_branch_t btype;
183197
git_branch_iterator *iter;
198+
git_strarray problems = {0};
184199

185200
cl_git_pass(git_oid_fromstr(&id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
186201
cl_git_pass(git_reference_create(&ref, _repo, "refs/remotes/renamed/master", &id, 1, NULL, NULL));
187202
git_reference_free(ref);
188203

189204
cl_git_pass(git_remote_load(&remote, _repo, "test"));
190-
cl_git_pass(git_remote_rename(remote, "renamed", dont_call_me_cb, NULL));
205+
cl_git_pass(git_remote_rename(&problems, remote, "renamed"));
191206
git_remote_free(remote);
192-
207+
cl_assert_equal_i(0, problems.count);
208+
git_strarray_free(&problems);
193209

194210
/* make sure there's only one remote-tracking branch */
195211
cl_git_pass(git_branch_iterator_new(&iter, _repo, GIT_BRANCH_REMOTE));

tests/submodule/add.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ void test_submodule_add__url_relative(void)
6868
{
6969
git_submodule *sm;
7070
git_remote *remote;
71+
git_strarray problems = {0};
7172

7273
/* default remote url is https://github.com/libgit2/false.git */
7374
g_repo = cl_git_sandbox_init("testrepo2");
7475

7576
/* make sure we don't default to origin - rename origin -> test_remote */
7677
cl_git_pass(git_remote_load(&remote, g_repo, "origin"));
77-
cl_git_pass(git_remote_rename(remote, "test_remote", NULL, NULL));
78+
cl_git_pass(git_remote_rename(&problems, remote, "test_remote"));
79+
cl_assert_equal_i(0, problems.count);
80+
git_strarray_free(&problems);
7881
cl_git_fail(git_remote_load(&remote, g_repo, "origin"));
7982
git_remote_free(remote);
8083

0 commit comments

Comments
 (0)