summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2014-10-09 10:41:38 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2014-10-09 10:41:38 -0400
commit8be28acfcd26b66275abe225700b24c838bbfd69 (patch)
tree9a293eca108b6c08fe986057ae3a06dbbf110936 /src
parent8bd747cfb4e6223490a71a0f3beecbed1a1a344a (diff)
parentc5837cad85c2730d30cd3c8b1018bd392ca8115a (diff)
downloadlibgit2-8be28acfcd26b66275abe225700b24c838bbfd69.tar.gz
Merge pull request #2462 from libgit2/cmn/remote-fetch-refs
Implement opportunistic ref updates
Diffstat (limited to 'src')
-rw-r--r--src/clone.c4
-rw-r--r--src/remote.c167
-rw-r--r--src/remote.h2
3 files changed, 146 insertions, 27 deletions
diff --git a/src/clone.c b/src/clone.c
index 43b839003..f18f07611 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -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_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;
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_
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;
error = checkout_branch(repo, remote, co_opts, branch, signature, git_buf_cstr(&reflog_message));
diff --git a/src/remote.c b/src/remote.c
index dfad946d5..432367deb 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -21,7 +21,7 @@
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;
@@ -34,7 +34,7 @@ static int add_refspec(git_remote *remote, const char *string, bool 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__free(spec);
return -1;
@@ -43,6 +43,11 @@ static int add_refspec(git_remote *remote, const char *string, bool is_fetch)
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)
{
const git_config_entry *ce;
@@ -370,6 +375,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
if (git_vector_init(&remote->refs, 32, NULL) < 0 ||
git_vector_init(&remote->refspecs, 2, NULL) < 0 ||
+ git_vector_init(&remote->passive_refspecs, 2, NULL) < 0 ||
git_vector_init(&remote->active_refspecs, 2, NULL) < 0) {
error = -1;
goto cleanup;
@@ -813,20 +819,43 @@ static int ls_to_vector(git_vector *out, git_remote *remote)
return 0;
}
-int git_remote_download(git_remote *remote)
+int git_remote_download(git_remote *remote, const git_strarray *refspecs)
{
int error;
- git_vector refs;
+ size_t i;
+ git_vector refs, specs, *to_active;
assert(remote);
if (ls_to_vector(&refs, remote) < 0)
return -1;
+ if ((git_vector_init(&specs, 0, NULL)) < 0)
+ goto on_error;
+
+ remote->passed_refspecs = 0;
+ 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;
+ remote->passed_refspecs = 1;
+ }
+
+ free_refspecs(&remote->passive_refspecs);
+ if ((error = dwim_refspecs(&remote->passive_refspecs, &remote->refspecs, &refs)) < 0)
+ goto on_error;
+
free_refspecs(&remote->active_refspecs);
+ error = dwim_refspecs(&remote->active_refspecs, to_active, &refs);
- error = dwim_refspecs(&remote->active_refspecs, &remote->refspecs, &refs);
git_vector_free(&refs);
+ free_refspecs(&specs);
+ git_vector_free(&specs);
if (error < 0)
return error;
@@ -835,10 +864,17 @@ int git_remote_download(git_remote *remote)
return error;
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(
git_remote *remote,
+ const git_strarray *refspecs,
const git_signature *signature,
const char *reflog_message)
{
@@ -849,7 +885,7 @@ int git_remote_fetch(
if ((error = git_remote_connect(remote, GIT_DIRECTION_FETCH)) != 0)
return error;
- error = git_remote_download(remote);
+ error = git_remote_download(remote, refspecs);
/* We don't need to be connected anymore */
git_remote_disconnect(remote);
@@ -1106,6 +1142,96 @@ on_error:
}
+/**
+ * Iteration over the three vectors, with a pause whenever we find a match
+ *
+ * On each stop, we store the iteration stat in the inout i,j,k
+ * parameters, and return the currently matching passive refspec as
+ * well as the head which we matched.
+ */
+static int next_head(const git_remote *remote, git_vector *refs,
+ git_refspec **out_spec, git_remote_head **out_head,
+ size_t *out_i, size_t *out_j, size_t *out_k)
+{
+ const git_vector *active, *passive;
+ git_remote_head *head;
+ git_refspec *spec, *passive_spec;
+ size_t i, j, k;
+
+ active = &remote->active_refspecs;
+ passive = &remote->passive_refspecs;
+
+ i = *out_i;
+ j = *out_j;
+ k = *out_k;
+
+ for (; i < refs->length; i++) {
+ head = git_vector_get(refs, i);
+
+ if (!git_reference_is_valid_name(head->name))
+ continue;
+
+ for (; j < active->length; j++) {
+ spec = git_vector_get(active, j);
+
+ if (!git_refspec_src_matches(spec, head->name))
+ continue;
+
+ for (; k < passive->length; k++) {
+ passive_spec = git_vector_get(passive, k);
+
+ if (!git_refspec_src_matches(passive_spec, head->name))
+ continue;
+
+ *out_spec = passive_spec;
+ *out_head = head;
+ *out_i = i;
+ *out_j = j;
+ *out_k = k + 1;
+ return 0;
+
+ }
+ k = 0;
+ }
+ j = 0;
+ }
+
+ return GIT_ITEROVER;
+}
+
+static int opportunistic_updates(const git_remote *remote, git_vector *refs, const git_signature *sig, const char *msg)
+{
+ size_t i, j, k;
+ git_refspec *spec;
+ git_remote_head *head;
+ git_reference *ref;
+ git_buf refname = GIT_BUF_INIT;
+ int error;
+
+ i = j = k = 0;
+
+ while ((error = next_head(remote, refs, &spec, &head, &i, &j, &k)) == 0) {
+ /*
+ * If we got here, there is a refspec which was used
+ * for fetching which matches the source of one of the
+ * passive refspecs, so we should update that
+ * remote-tracking branch, but not add it to
+ * FETCH_HEAD
+ */
+
+ if ((error = git_refspec_transform(&refname, spec, head->name)) < 0)
+ return error;
+
+ error = git_reference_create(&ref, remote->repo, refname.ptr, &head->oid, true, sig, msg);
+ git_buf_free(&refname);
+
+ if (error < 0)
+ return error;
+ }
+
+ return 0;
+}
+
int git_remote_update_tips(
git_remote *remote,
const git_signature *signature,
@@ -1136,6 +1262,10 @@ int git_remote_update_tips(
goto out;
}
+ /* only try to do opportunisitic updates if the refpec lists differ */
+ if (remote->passed_refspecs)
+ error = opportunistic_updates(remote, &refs, signature, reflog_message);
+
out:
git_vector_free(&refs);
git_refspec__free(&tagspec);
@@ -1189,6 +1319,9 @@ void git_remote_free(git_remote *remote)
free_refspecs(&remote->active_refspecs);
git_vector_free(&remote->active_refspecs);
+ free_refspecs(&remote->passive_refspecs);
+ git_vector_free(&remote->passive_refspecs);
+
git__free(remote->url);
git__free(remote->pushurl);
git__free(remote->name);
@@ -1640,27 +1773,14 @@ void git_remote_clear_refspecs(git_remote *remote)
git_vector_clear(&remote->refspecs);
}
-static int add_and_dwim(git_remote *remote, const char *str, int push)
-{
- git_refspec *spec;
- git_vector *vec;
-
- if (add_refspec(remote, str, !push) < 0)
- return -1;
-
- vec = &remote->refspecs;
- spec = git_vector_get(vec, vec->length - 1);
- return git_refspec__dwim_one(&remote->active_refspecs, spec, &remote->refs);
-}
-
int git_remote_add_fetch(git_remote *remote, const char *refspec)
{
- return add_and_dwim(remote, refspec, false);
+ return add_refspec(remote, refspec, true);
}
int git_remote_add_push(git_remote *remote, const char *refspec)
{
- return add_and_dwim(remote, refspec, true);
+ return add_refspec(remote, refspec, false);
}
static int set_refspecs(git_remote *remote, git_strarray *array, int push)
@@ -1688,10 +1808,7 @@ static int set_refspecs(git_remote *remote, git_strarray *array, int push)
return -1;
}
- free_refspecs(&remote->active_refspecs);
- git_vector_clear(&remote->active_refspecs);
-
- return dwim_refspecs(&remote->active_refspecs, &remote->refspecs, &remote->refs);
+ return 0;
}
int git_remote_set_fetch_refspecs(git_remote *remote, git_strarray *array)
diff --git a/src/remote.h b/src/remote.h
index f88601e9b..b79ace438 100644
--- a/src/remote.h
+++ b/src/remote.h
@@ -23,6 +23,7 @@ struct git_remote {
git_vector refs;
git_vector refspecs;
git_vector active_refspecs;
+ git_vector passive_refspecs;
git_transport_cb transport_cb;
void *transport_cb_payload;
git_transport *transport;
@@ -32,6 +33,7 @@ struct git_remote {
unsigned int need_pack;
git_remote_autotag_option_t download_tags;
int update_fetchhead;
+ int passed_refspecs;
};
const char* git_remote__urlfordirection(struct git_remote *remote, int direction);