git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] fetch: ignore wildcarded refspecs that update local symbolic refs

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 11, 2012, 22:32 UTC
Message-ID
<7vmwxk6x0a.fsf_-_@alter.siamese.dyndns.org>
In-Reply-To
<7v62488j8a.fsf_-_@alter.siamese.dyndns.org>

In a repository cloned from somewhere else, you typically have a symbolic ref refs/remotes/origin/HEAD pointing at the 'master' remote-tracking ref that is next to it. When fetching into such a repository with "git fetch --mirror" from another repository that was similarly cloned, the implied wildcard refspec refs/*:refs/* will end up asking to update refs/remotes/origin/HEAD with the object at refs/remotes/origin/HEAD at the remote side, while asking to update refs/remotes/origin/master the same way. Depending on the order the two updates happen, the latter one would find that the value of the ref before it is updated has changed from what the code expects.

When the user asks to update the underlying ref via the symbolic ref explicitly without using a wildcard refspec, e.g. "git fetch $there refs/heads/master:refs/remotes/origin/HEAD", we should still let him do so, but when expanding wildcard refs, it will result in a more intuitive outcome if we simply ignore local symbolic refs.

As the purpose of the symbolic ref refs/remotes/origin/HEAD is to follow the ref it points at (e.g. refs/remotes/origin/master), its value would change when the underlying ref is updated.

Earlier commit da3efdb (receive-pack: detect aliased updates which can occur with symrefs, 2010-04-19) fixed a similar issue for "git push".

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * This time with minimal tests and an updated log message.
 remote.c                     | 13 ++++++++++++-
 t/t5535-fetch-push-symref.sh | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100755 t/t5535-fetch-push-symref.sh
diff --git a/remote.c b/remote.c
index 04fd9ea..a72748c 100644
--- a/remote.c
+++ b/remote.c
@@ -1370,6 +1370,16 @@ int branch_merge_matches(struct branch *branch,
 	return refname_match(branch->merge[i]->src, refname, ref_fetch_rules);
 }
 
+static int ignore_symref_update(const char *refname)
+{
+	unsigned char sha1[20];
+	int flag;
+
+	if (!resolve_ref_unsafe(refname, sha1, 0, &flag))
+		return 0; /* non-existing refs are OK */
+	return (flag & REF_ISSYMREF);
+}
+
 static struct ref *get_expanded_map(const struct ref *remote_refs,
 				    const struct refspec *refspec)
 {
@@ -1383,7 +1393,8 @@ static struct ref *get_expanded_map(const struct ref *remote_refs,
 		if (strchr(ref->name, '^'))
 			continue; /* a dereference item */
 		if (match_name_with_pattern(refspec->src, ref->name,
-					    refspec->dst, &expn_name)) {
+					    refspec->dst, &expn_name) &&
+		    !ignore_symref_update(expn_name)) {
 			struct ref *cpy = copy_ref(ref);
 
 			cpy->peer_ref = alloc_ref(expn_name);
diff --git a/t/t5535-fetch-push-symref.sh b/t/t5535-fetch-push-symref.sh
new file mode 100755
index 0000000..8ed58d2
--- /dev/null
+++ b/t/t5535-fetch-push-symref.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description='avoiding conflicting update thru symref aliasing'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	test_commit one &&
+	git clone . src &&
+	git clone src dst1 &&
+	git clone src dst2 &&
+	test_commit two &&
+	( cd src && git pull )
+'
+
+test_expect_success 'push' '
+	(
+		cd src &&
+		git push ../dst1 "refs/remotes/*:refs/remotes/*"
+	) &&
+	git ls-remote src "refs/remotes/*" >expect &&
+	git ls-remote dst1 "refs/remotes/*" >actual &&
+	test_cmp expect actual &&
+	( cd src && git symbolic-ref refs/remotes/origin/HEAD ) >expect &&
+	( cd dst1 && git symbolic-ref refs/remotes/origin/HEAD ) >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'fetch' '
+	(
+		cd dst2 &&
+		git fetch ../src "refs/remotes/*:refs/remotes/*"
+	) &&
+	git ls-remote src "refs/remotes/*" >expect &&
+	git ls-remote dst2 "refs/remotes/*" >actual &&
+	test_cmp expect actual &&
+	( cd src && git symbolic-ref refs/remotes/origin/HEAD ) >expect &&
+	( cd dst2 && git symbolic-ref refs/remotes/origin/HEAD ) >actual &&
+	test_cmp expect actual
+'
+
+test_done
-- 
1.8.1.rc1.128.gd8d1528
Previous: Junio C HamanoNext: Jay Soffian
Message 12 of 17 in “receive-pack: switch global variable 'commands' to a parameter”
  1. 1/2 receive-pack: switch global variable 'commands' to a parameterJay Soffian, Apr 19, 2010
  2. 2/2 receive-pack: detect aliased updates which can occur with symrefsJay Soffian, Apr 19, 2010
  3. Jay SoffianApr 19, 2010
  4. 2/2 receive-pack: detect aliased updates which can occur with symrefsJay Soffian, Apr 19, 2010
  5. Junio C HamanoApr 19, 2010
  6. Jay SoffianApr 19, 2010
  7. 0/3 js/maint-receive-pack-symref-aliasJay Soffian, Apr 19, 2010
  8. 1/3 receive-pack: switch global variable 'commands' to a parameterJay Soffian, Apr 19, 2010
  9. 2/3 t5516-fetch-push.sh: style cleanupJay Soffian, Apr 19, 2010
  10. 3/3 receive-pack: detect aliased updates which can occur with symrefsJay Soffian, Apr 19, 2010
  11. ignoring a fetch that overwrites local symrefJunio C Hamano, Dec 11, 2012
  12. fetch: ignore wildcarded refspecs that update local symbolic refsJunio C Hamano, Dec 11, 2012
  13. Jay SoffianDec 12, 2012
  14. Shawn PearceDec 12, 2012
  15. Junio C HamanoDec 12, 2012
  16. 3/3 receive-pack: detect aliased updates which can occur with symrefsJay Soffian, Apr 19, 2010
  17. Ævar Arnfjörð BjarmasonJun 10, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.