summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2014-07-04 09:03:33 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2014-09-30 15:43:24 +0200
commitc5837cad85c2730d30cd3c8b1018bd392ca8115a (patch)
tree52c0d3d9d80e213556020313a6409bffc6ede7a4 /src
parent2cdd5c5752f1dea43cd31be9b75be7f7f5e4eab1 (diff)
downloadlibgit2-c5837cad85c2730d30cd3c8b1018bd392ca8115a.tar.gz
remote: implement opportunistic remote-tracking branch updatescmn/remote-fetch-refs
When a list of refspecs is passed to fetch (what git would consider refspec passed on the command-line), we not only need to perform the updates described in that refspec, but also update the remote-tracking branch of the fetched remote heads according to the remote's configured refspecs. These "fetches" are not however to be written to FETCH_HEAD as they would be duplicate data, and it's not what the user asked for.
Diffstat (limited to 'src')
-rw-r--r--src/remote.c96
-rw-r--r--src/remote.h1
2 files changed, 97 insertions, 0 deletions
diff --git a/src/remote.c b/src/remote.c
index c164db909..432367deb 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -833,6 +833,7 @@ int git_remote_download(git_remote *remote, const git_strarray *refspecs)
if ((git_vector_init(&specs, 0, NULL)) < 0)
goto on_error;
+ remote->passed_refspecs = 0;
if (!refspecs) {
to_active = &remote->refspecs;
} else {
@@ -842,6 +843,7 @@ int git_remote_download(git_remote *remote, const git_strarray *refspecs)
}
to_active = &specs;
+ remote->passed_refspecs = 1;
}
free_refspecs(&remote->passive_refspecs);
@@ -1140,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,
@@ -1170,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);
diff --git a/src/remote.h b/src/remote.h
index 73c1614f6..b79ace438 100644
--- a/src/remote.h
+++ b/src/remote.h
@@ -33,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);