Commit c5837cad85c2730d30cd3c8b1018bd392ca8115a

Carlos Martín Nieto 2014-07-04T09:03:33

remote: implement opportunistic remote-tracking branch updates 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.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index b23e07d..8ad323c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -56,5 +56,11 @@ v0.21 + 1
 * Add support for refspecs with the asterisk in the middle of a
   pattern.
 
+* Fetching now performs opportunistic updates. To achieve this, we
+  introduce a difference between active and passive refspecs, which
+  make git_remote_download and git_remote_fetch to take a list of
+  resfpecs to be the active list, similarly to how git fetch accepts a
+  list on the command-line.
+
 * Introduce git_merge_bases() and the git_oidarray type to expose all
   merge bases between two commits.
diff --git a/src/remote.c b/src/remote.c
index c164db9..432367d 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 73c1614..b79ace4 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);
diff --git a/tests/network/remote/local.c b/tests/network/remote/local.c
index ceccf45..c6c9e4c 100644
--- a/tests/network/remote/local.c
+++ b/tests/network/remote/local.c
@@ -408,3 +408,24 @@ void test_network_remote_local__fetch_default_reflog_message(void)
 	git_reflog_free(log);
 	git_signature_free(sig);
 }
+
+void test_network_remote_local__opportunistic_update(void)
+{
+	git_reference *ref;
+	char *refspec_strings[] = {
+		"master",
+	};
+	git_strarray array = {
+		refspec_strings,
+		1,
+	};
+
+	/* this remote has a passive refspec of "refs/heads/<star>:refs/remotes/origin/<star>" */
+	cl_git_pass(git_remote_create(&remote, repo, "origin", cl_git_fixture_url("testrepo.git")));
+	/* and we pass the active refspec "master" */
+	cl_git_pass(git_remote_fetch(remote, &array, NULL, NULL));
+
+	/* and we expect that to update our copy of origin's master */
+	cl_git_pass(git_reference_lookup(&ref, repo, "refs/remotes/origin/master"));
+	git_reference_free(ref);
+}