Skip to content

Commit d365029

Browse files
committed
remote: add a flag to prevent generation of the default fetchspec
1 parent fdb116b commit d365029

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

include/git2/remote.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ GIT_EXTERN(int) git_remote_create(
4747
typedef enum {
4848
/** Ignore the repository apply.insteadOf configuration */
4949
GIT_REMOTE_CREATE_SKIP_INSTEADOF = (1 << 0),
50+
51+
/** Don't build a fetchspec from the name if none is set */
52+
GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC = (1 << 1),
5053
} git_remote_create_flags;
5154

5255
/**

src/remote.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ static int create_internal(git_remote **out, const char *url, const git_remote_c
230230
git_config *config_ro = NULL, *config_rw;
231231
git_buf canonical_url = GIT_BUF_INIT;
232232
git_buf var = GIT_BUF_INIT;
233+
git_buf specbuf = GIT_BUF_INIT;
233234
const git_remote_create_options dummy_opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
234235
int error = -1;
235236

@@ -282,13 +283,24 @@ static int create_internal(git_remote **out, const char *url, const git_remote_c
282283
goto on_error;
283284
}
284285

285-
if (opts->fetchspec != NULL) {
286-
if ((error = add_refspec(remote, opts->fetchspec, true)) < 0)
286+
if (opts->fetchspec != NULL ||
287+
(opts->name && !(opts->flags & GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC))) {
288+
const char *fetch = NULL;
289+
if (opts->fetchspec) {
290+
fetch = opts->fetchspec;
291+
} else {
292+
if ((error = default_fetchspec_for_name(&specbuf, opts->name)) < 0)
293+
goto on_error;
294+
295+
fetch = git_buf_cstr(&specbuf);
296+
}
297+
298+
if ((error = add_refspec(remote, fetch, true)) < 0)
287299
goto on_error;
288300

289301
/* only write for named remotes with a repository */
290302
if (opts->repository && opts->name &&
291-
((error = write_add_refspec(opts->repository, opts->name, opts->fetchspec, true)) < 0 ||
303+
((error = write_add_refspec(opts->repository, opts->name, fetch, true)) < 0 ||
292304
(error = lookup_remote_prune_config(remote, config_ro, opts->name)) < 0))
293305
goto on_error;
294306

@@ -314,6 +326,7 @@ static int create_internal(git_remote **out, const char *url, const git_remote_c
314326
git_remote_free(remote);
315327

316328
git_config_free(config_ro);
329+
git_buf_dispose(&specbuf);
317330
git_buf_dispose(&canonical_url);
318331
git_buf_dispose(&var);
319332
return error;
@@ -336,7 +349,6 @@ int git_remote_create(git_remote **out, git_repository *repo, const char *name,
336349

337350
opts.repository = repo;
338351
opts.name = name;
339-
opts.fetchspec = git_buf_cstr(&buf);
340352

341353
error = create_internal(out, url, &opts);
342354

@@ -361,6 +373,7 @@ int git_remote_create_with_fetchspec(git_remote **out, git_repository *repo, con
361373
opts.repository = repo;
362374
opts.name = name;
363375
opts.fetchspec = fetch;
376+
opts.flags = GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC;
364377

365378
return create_internal(out, url, &opts);
366379
}

tests/remote/create.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ void test_remote_create__named(void)
5252
git_config *cfg;
5353
const char *cfg_val;
5454

55-
cl_skip(); // Later
56-
5755
size_t section_count = count_config_entries_match(_repo, "remote\\.");
5856

5957
cl_git_pass(git_remote_create(&remote, _repo, "valid-name", TEST_URL));
@@ -200,8 +198,6 @@ void test_remote_create__with_opts_named(void)
200198
opts.name = "test-new";
201199
opts.repository = _repo;
202200

203-
cl_skip(); // Later
204-
205201
cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
206202
cl_assert_equal_s(git_remote_name(remote), "test-new");
207203
cl_assert_equal_s(git_remote_url(remote), TEST_URL);
@@ -246,7 +242,7 @@ void test_remote_create__with_opts_named_no_fetchspec(void)
246242

247243
opts.name = "test-new";
248244
opts.repository = _repo;
249-
// opts.flags = GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC; // Later
245+
opts.flags = GIT_REMOTE_CREATE_SKIP_DEFAULT_FETCHSPEC;
250246

251247
cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
252248
cl_assert_equal_s(git_remote_name(remote), "test-new");

0 commit comments

Comments
 (0)