Commit 3f894205 by Carlos Martín Nieto

remote: allow overriding the refspecs for download and fetch

With opportunistic ref updates, git has introduced the concept of having
base refspecs *and* refspecs that are active for a particular fetch.

Let's start by letting the user override the refspecs for download.
parent a2a23322
...@@ -36,7 +36,7 @@ static void *download(void *ptr) ...@@ -36,7 +36,7 @@ static void *download(void *ptr)
// Download the packfile and index it. This function updates the // Download the packfile and index it. This function updates the
// amount of received data and the indexer stats which lets you // amount of received data and the indexer stats which lets you
// inform the user about progress. // inform the user about progress.
if (git_remote_download(data->remote) < 0) { if (git_remote_download(data->remote, NULL) < 0) {
data->ret = -1; data->ret = -1;
goto exit; goto exit;
} }
......
...@@ -306,9 +306,12 @@ GIT_EXTERN(int) git_remote_ls(const git_remote_head ***out, size_t *size, git_r ...@@ -306,9 +306,12 @@ GIT_EXTERN(int) git_remote_ls(const git_remote_head ***out, size_t *size, git_r
* The .idx file will be created and both it and the packfile with be * The .idx file will be created and both it and the packfile with be
* renamed to their final name. * renamed to their final name.
* *
* @param remote the remote
* @param refspecs the refspecs to use for this negotiation and
* download. Use NULL to use the base refspecs
* @return 0 or an error code * @return 0 or an error code
*/ */
GIT_EXTERN(int) git_remote_download(git_remote *remote); GIT_EXTERN(int) git_remote_download(git_remote *remote, const git_strarray *refspecs);
/** /**
* Check whether the remote is connected * Check whether the remote is connected
...@@ -373,6 +376,8 @@ GIT_EXTERN(int) git_remote_update_tips( ...@@ -373,6 +376,8 @@ GIT_EXTERN(int) git_remote_update_tips(
* disconnect and update the remote-tracking branches. * disconnect and update the remote-tracking branches.
* *
* @param remote the remote to fetch from * @param remote the remote to fetch from
* @param refspecs the refspecs to use for this fetch. Pass NULL to
* use the base refspecs.
* @param signature The identity to use when updating reflogs * @param signature The identity to use when updating reflogs
* @param reflog_message The message to insert into the reflogs. If NULL, the * @param reflog_message The message to insert into the reflogs. If NULL, the
* default is "fetch" * default is "fetch"
...@@ -380,6 +385,7 @@ GIT_EXTERN(int) git_remote_update_tips( ...@@ -380,6 +385,7 @@ GIT_EXTERN(int) git_remote_update_tips(
*/ */
GIT_EXTERN(int) git_remote_fetch( GIT_EXTERN(int) git_remote_fetch(
git_remote *remote, git_remote *remote,
const git_strarray *refspecs,
const git_signature *signature, const git_signature *signature,
const char *reflog_message); const char *reflog_message);
......
...@@ -358,7 +358,7 @@ static int clone_into(git_repository *repo, git_remote *_remote, const git_check ...@@ -358,7 +358,7 @@ static int clone_into(git_repository *repo, git_remote *_remote, const git_check
git_remote_set_update_fetchhead(remote, 0); git_remote_set_update_fetchhead(remote, 0);
git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
if ((error = git_remote_fetch(remote, signature, git_buf_cstr(&reflog_message))) != 0) if ((error = git_remote_fetch(remote, NULL, signature, git_buf_cstr(&reflog_message))) != 0)
goto cleanup; goto cleanup;
error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message)); error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message));
...@@ -553,7 +553,7 @@ static int clone_local_into(git_repository *repo, git_remote *remote, const git_ ...@@ -553,7 +553,7 @@ static int clone_local_into(git_repository *repo, git_remote *remote, const git_
git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote)); git_buf_printf(&reflog_message, "clone: from %s", git_remote_url(remote));
if ((error = git_remote_fetch(remote, signature, git_buf_cstr(&reflog_message))) != 0) if ((error = git_remote_fetch(remote, NULL, signature, git_buf_cstr(&reflog_message))) != 0)
goto cleanup; goto cleanup;
error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message)); error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message));
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
static int dwim_refspecs(git_vector *out, git_vector *refspecs, git_vector *refs); static int dwim_refspecs(git_vector *out, git_vector *refspecs, git_vector *refs);
static int add_refspec(git_remote *remote, const char *string, bool is_fetch) static int add_refspec_to(git_vector *vector, const char *string, bool is_fetch)
{ {
git_refspec *spec; git_refspec *spec;
...@@ -34,7 +34,7 @@ static int add_refspec(git_remote *remote, const char *string, bool is_fetch) ...@@ -34,7 +34,7 @@ static int add_refspec(git_remote *remote, const char *string, bool is_fetch)
} }
spec->push = !is_fetch; spec->push = !is_fetch;
if (git_vector_insert(&remote->refspecs, spec) < 0) { if (git_vector_insert(vector, spec) < 0) {
git_refspec__free(spec); git_refspec__free(spec);
git__free(spec); git__free(spec);
return -1; return -1;
...@@ -43,6 +43,11 @@ static int add_refspec(git_remote *remote, const char *string, bool is_fetch) ...@@ -43,6 +43,11 @@ static int add_refspec(git_remote *remote, const char *string, bool is_fetch)
return 0; return 0;
} }
static int add_refspec(git_remote *remote, const char *string, bool is_fetch)
{
return add_refspec_to(&remote->refspecs, string, is_fetch);
}
static int download_tags_value(git_remote *remote, git_config *cfg) static int download_tags_value(git_remote *remote, git_config *cfg)
{ {
const git_config_entry *ce; const git_config_entry *ce;
...@@ -813,20 +818,37 @@ static int ls_to_vector(git_vector *out, git_remote *remote) ...@@ -813,20 +818,37 @@ static int ls_to_vector(git_vector *out, git_remote *remote)
return 0; return 0;
} }
int git_remote_download(git_remote *remote) int git_remote_download(git_remote *remote, const git_strarray *refspecs)
{ {
int error; int error;
git_vector refs; size_t i;
git_vector refs, specs, *to_active;
assert(remote); assert(remote);
if (ls_to_vector(&refs, remote) < 0) if (ls_to_vector(&refs, remote) < 0)
return -1; return -1;
if ((git_vector_init(&specs, 0, NULL)) < 0)
goto on_error;
if (!refspecs) {
to_active = &remote->refspecs;
} else {
for (i = 0; i < refspecs->count; i++) {
if ((error = add_refspec_to(&specs, refspecs->strings[i], true)) < 0)
goto on_error;
}
to_active = &specs;
}
free_refspecs(&remote->active_refspecs); free_refspecs(&remote->active_refspecs);
error = dwim_refspecs(&remote->active_refspecs, &remote->refspecs, &refs); error = dwim_refspecs(&remote->active_refspecs, to_active, &refs);
git_vector_free(&refs); git_vector_free(&refs);
free_refspecs(&specs);
git_vector_free(&specs);
if (error < 0) if (error < 0)
return error; return error;
...@@ -835,10 +857,17 @@ int git_remote_download(git_remote *remote) ...@@ -835,10 +857,17 @@ int git_remote_download(git_remote *remote)
return error; return error;
return git_fetch_download_pack(remote); return git_fetch_download_pack(remote);
on_error:
git_vector_free(&refs);
free_refspecs(&specs);
git_vector_free(&specs);
return error;
} }
int git_remote_fetch( int git_remote_fetch(
git_remote *remote, git_remote *remote,
const git_strarray *refspecs,
const git_signature *signature, const git_signature *signature,
const char *reflog_message) const char *reflog_message)
{ {
...@@ -849,7 +878,7 @@ int git_remote_fetch( ...@@ -849,7 +878,7 @@ int git_remote_fetch(
if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH)) != 0) if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH)) != 0)
return error; return error;
error = git_remote_download(remote); error = git_remote_download(remote, refspecs);
/* We don't need to be connected anymore */ /* We don't need to be connected anymore */
git_remote_disconnect(remote); git_remote_disconnect(remote);
......
...@@ -335,7 +335,7 @@ void test_fetchhead_nonetwork__unborn_with_upstream(void) ...@@ -335,7 +335,7 @@ void test_fetchhead_nonetwork__unborn_with_upstream(void)
cl_git_pass(git_remote_set_url(remote, cl_fixture("testrepo.git"))); cl_git_pass(git_remote_set_url(remote, cl_fixture("testrepo.git")));
cl_git_pass(git_remote_save(remote)); cl_git_pass(git_remote_save(remote));
cl_git_pass(git_remote_fetch(remote, NULL, NULL)); cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
git_remote_free(remote); git_remote_free(remote);
cl_git_pass(git_repository_fetchhead_foreach(repo, assert_master_for_merge, NULL)); cl_git_pass(git_repository_fetchhead_foreach(repo, assert_master_for_merge, NULL));
......
...@@ -36,7 +36,7 @@ void test_network_fetchlocal__complete(void) ...@@ -36,7 +36,7 @@ void test_network_fetchlocal__complete(void)
cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url));
git_remote_set_callbacks(origin, &callbacks); git_remote_set_callbacks(origin, &callbacks);
cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(origin)); cl_git_pass(git_remote_download(origin, NULL));
cl_git_pass(git_remote_update_tips(origin, NULL, NULL)); cl_git_pass(git_remote_update_tips(origin, NULL, NULL));
cl_git_pass(git_reference_list(&refnames, repo)); cl_git_pass(git_reference_list(&refnames, repo));
...@@ -74,7 +74,7 @@ void test_network_fetchlocal__partial(void) ...@@ -74,7 +74,7 @@ void test_network_fetchlocal__partial(void)
cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url));
git_remote_set_callbacks(origin, &callbacks); git_remote_set_callbacks(origin, &callbacks);
cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(origin)); cl_git_pass(git_remote_download(origin, NULL));
cl_git_pass(git_remote_update_tips(origin, NULL, NULL)); cl_git_pass(git_remote_update_tips(origin, NULL, NULL));
git_strarray_free(&refnames); git_strarray_free(&refnames);
......
...@@ -125,7 +125,7 @@ void test_network_remote_local__shorthand_fetch_refspec0(void) ...@@ -125,7 +125,7 @@ void test_network_remote_local__shorthand_fetch_refspec0(void)
cl_git_pass(git_remote_add_fetch(remote, refspec)); cl_git_pass(git_remote_add_fetch(remote, refspec));
cl_git_pass(git_remote_add_fetch(remote, refspec2)); cl_git_pass(git_remote_add_fetch(remote, refspec2));
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master")); cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master"));
...@@ -147,7 +147,7 @@ void test_network_remote_local__shorthand_fetch_refspec1(void) ...@@ -147,7 +147,7 @@ void test_network_remote_local__shorthand_fetch_refspec1(void)
cl_git_pass(git_remote_add_fetch(remote, refspec)); cl_git_pass(git_remote_add_fetch(remote, refspec));
cl_git_pass(git_remote_add_fetch(remote, refspec2)); cl_git_pass(git_remote_add_fetch(remote, refspec2));
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
cl_git_fail(git_reference_lookup(&ref, repo, "refs/remotes/master")); cl_git_fail(git_reference_lookup(&ref, repo, "refs/remotes/master"));
...@@ -162,7 +162,7 @@ void test_network_remote_local__tagopt(void) ...@@ -162,7 +162,7 @@ void test_network_remote_local__tagopt(void)
connect_to_local_repository(cl_fixture("testrepo.git")); connect_to_local_repository(cl_fixture("testrepo.git"));
git_remote_set_autotag(remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL); git_remote_set_autotag(remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL);
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
...@@ -181,7 +181,7 @@ void test_network_remote_local__push_to_bare_remote(void) ...@@ -181,7 +181,7 @@ void test_network_remote_local__push_to_bare_remote(void)
/* Get some commits */ /* Get some commits */
connect_to_local_repository(cl_fixture("testrepo.git")); connect_to_local_repository(cl_fixture("testrepo.git"));
cl_git_pass(git_remote_add_fetch(remote, "master:master")); cl_git_pass(git_remote_add_fetch(remote, "master:master"));
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote); git_remote_disconnect(remote);
...@@ -218,7 +218,7 @@ void test_network_remote_local__push_to_bare_remote_with_file_url(void) ...@@ -218,7 +218,7 @@ void test_network_remote_local__push_to_bare_remote_with_file_url(void)
/* Get some commits */ /* Get some commits */
connect_to_local_repository(cl_fixture("testrepo.git")); connect_to_local_repository(cl_fixture("testrepo.git"));
cl_git_pass(git_remote_add_fetch(remote, "master:master")); cl_git_pass(git_remote_add_fetch(remote, "master:master"));
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote); git_remote_disconnect(remote);
...@@ -258,7 +258,7 @@ void test_network_remote_local__push_to_non_bare_remote(void) ...@@ -258,7 +258,7 @@ void test_network_remote_local__push_to_non_bare_remote(void)
/* Get some commits */ /* Get some commits */
connect_to_local_repository(cl_fixture("testrepo.git")); connect_to_local_repository(cl_fixture("testrepo.git"));
cl_git_pass(git_remote_add_fetch(remote, "master:master")); cl_git_pass(git_remote_add_fetch(remote, "master:master"));
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote); git_remote_disconnect(remote);
...@@ -299,7 +299,7 @@ void test_network_remote_local__fetch(void) ...@@ -299,7 +299,7 @@ void test_network_remote_local__fetch(void)
connect_to_local_repository(cl_fixture("testrepo.git")); connect_to_local_repository(cl_fixture("testrepo.git"));
cl_git_pass(git_remote_add_fetch(remote, refspec)); cl_git_pass(git_remote_add_fetch(remote, refspec));
cl_git_pass(git_remote_fetch(remote, sig, "UPDAAAAAATE!!")); cl_git_pass(git_remote_fetch(remote, NULL, sig, "UPDAAAAAATE!!"));
cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master")); cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/sloppy/master"));
git_reference_free(ref); git_reference_free(ref);
...@@ -327,7 +327,7 @@ void test_network_remote_local__reflog(void) ...@@ -327,7 +327,7 @@ void test_network_remote_local__reflog(void)
connect_to_local_repository(cl_fixture("testrepo.git")); connect_to_local_repository(cl_fixture("testrepo.git"));
cl_git_pass(git_remote_add_fetch(remote, refspec)); cl_git_pass(git_remote_add_fetch(remote, refspec));
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
cl_git_pass(git_remote_update_tips(remote, sig, "UPDAAAAAATE!!")); cl_git_pass(git_remote_update_tips(remote, sig, "UPDAAAAAATE!!"));
cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master")); cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master"));
...@@ -354,7 +354,7 @@ void test_network_remote_local__fetch_default_reflog_message(void) ...@@ -354,7 +354,7 @@ void test_network_remote_local__fetch_default_reflog_message(void)
connect_to_local_repository(cl_fixture("testrepo.git")); connect_to_local_repository(cl_fixture("testrepo.git"));
cl_git_pass(git_remote_add_fetch(remote, refspec)); cl_git_pass(git_remote_add_fetch(remote, refspec));
cl_git_pass(git_remote_fetch(remote, sig, NULL)); cl_git_pass(git_remote_fetch(remote, NULL, sig, NULL));
cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master")); cl_git_pass(git_reflog_read(&log, repo, "refs/remotes/sloppy/master"));
cl_assert_equal_i(1, git_reflog_entrycount(log)); cl_assert_equal_i(1, git_reflog_entrycount(log));
......
...@@ -47,7 +47,7 @@ static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n) ...@@ -47,7 +47,7 @@ static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n)
git_remote_set_callbacks(remote, &callbacks); git_remote_set_callbacks(remote, &callbacks);
git_remote_set_autotag(remote, flag); git_remote_set_autotag(remote, flag);
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote); git_remote_disconnect(remote);
cl_assert_equal_i(counter, n); cl_assert_equal_i(counter, n);
...@@ -86,11 +86,11 @@ void test_online_fetch__fetch_twice(void) ...@@ -86,11 +86,11 @@ void test_online_fetch__fetch_twice(void)
git_remote *remote; git_remote *remote;
cl_git_pass(git_remote_create(&remote, _repo, "test", "git://github.com/libgit2/TestGitRepository.git")); cl_git_pass(git_remote_create(&remote, _repo, "test", "git://github.com/libgit2/TestGitRepository.git"));
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
git_remote_disconnect(remote); git_remote_disconnect(remote);
git_remote_connect(remote, GIT_DIRECTION_FETCH); git_remote_connect(remote, GIT_DIRECTION_FETCH);
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
git_remote_disconnect(remote); git_remote_disconnect(remote);
git_remote_free(remote); git_remote_free(remote);
...@@ -128,7 +128,7 @@ void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date ...@@ -128,7 +128,7 @@ void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date
callbacks.transfer_progress = &transferProgressCallback; callbacks.transfer_progress = &transferProgressCallback;
callbacks.payload = &invoked; callbacks.payload = &invoked;
git_remote_set_callbacks(remote, &callbacks); git_remote_set_callbacks(remote, &callbacks);
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
cl_assert_equal_i(false, invoked); cl_assert_equal_i(false, invoked);
...@@ -162,7 +162,7 @@ void test_online_fetch__can_cancel(void) ...@@ -162,7 +162,7 @@ void test_online_fetch__can_cancel(void)
git_remote_set_callbacks(remote, &callbacks); git_remote_set_callbacks(remote, &callbacks);
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
cl_git_fail_with(git_remote_download(remote), -4321); cl_git_fail_with(git_remote_download(remote, NULL), -4321);
git_remote_disconnect(remote); git_remote_disconnect(remote);
git_remote_free(remote); git_remote_free(remote);
} }
......
...@@ -50,7 +50,7 @@ static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fet ...@@ -50,7 +50,7 @@ static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fet
} }
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(remote)); cl_git_pass(git_remote_download(remote, NULL));
cl_git_pass(git_remote_update_tips(remote, NULL, NULL)); cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
git_remote_disconnect(remote); git_remote_disconnect(remote);
git_remote_free(remote); git_remote_free(remote);
......
...@@ -408,7 +408,7 @@ void test_online_push__initialize(void) ...@@ -408,7 +408,7 @@ void test_online_push__initialize(void)
/* Now that we've deleted everything, fetch from the remote */ /* Now that we've deleted everything, fetch from the remote */
cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_FETCH)); cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_FETCH));
cl_git_pass(git_remote_download(_remote)); cl_git_pass(git_remote_download(_remote, NULL));
cl_git_pass(git_remote_update_tips(_remote, NULL, NULL)); cl_git_pass(git_remote_update_tips(_remote, NULL, NULL));
git_remote_disconnect(_remote); git_remote_disconnect(_remote);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment