diff options
author | Lee Worrall <worrall.la@gmail.com> | 2020-10-10 11:31:19 +0800 |
---|---|---|
committer | Thomas Wolf <thomas.wolf@paranor.ch> | 2020-11-26 21:14:08 +0100 |
commit | 9ebbfe93bb3ba0ea170d330fe227f1d8182d7d64 (patch) | |
tree | 52d2401beac6b58d2eb485cd9aebe7958d3469b2 /org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java | |
parent | dfa29458e7b187b45e9fb48791ddb71ffac5ad93 (diff) | |
download | jgit-9ebbfe93bb3ba0ea170d330fe227f1d8182d7d64.tar.gz jgit-9ebbfe93bb3ba0ea170d330fe227f1d8182d7d64.zip |
Add support for reading symrefs from pack capabilities
A SymbolicRef is added to the advertised refs for any symref in
capabilities whose target is an advertised ref; this may replace an
existing entry, such as HEAD.
When cloning, if any advertised HEAD is symbolic then use the target
rather than looking for an advertised ref with a matching objectId.
Add --symref option to LsRemote command.
Bug: 514052
Change-Id: Idfb48e6f6e8dcfe57a6896883fe6d84d533aa9d0
Signed-off-by: Lee Worrall <worrall.la@gmail.com>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java new file mode 100644 index 0000000000..64b16f659a --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/BasePackConnectionTest.java @@ -0,0 +1,233 @@ +/* + * Copyright (C) 2020, Lee Worrall and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.transport; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ObjectIdRef; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.SymbolicRef; +import org.junit.Test; + +public class BasePackConnectionTest { + + @Test + public void testExtractSymRefsFromCapabilities() { + final Map<String, String> symRefs = BasePackConnection + .extractSymRefsFromCapabilities( + Arrays.asList("symref=HEAD:refs/heads/main", + "symref=refs/heads/sym:refs/heads/other")); + + assertEquals(2, symRefs.size()); + assertEquals("refs/heads/main", symRefs.get("HEAD")); + assertEquals("refs/heads/other", symRefs.get("refs/heads/sym")); + } + + @Test + public void testUpdateWithSymRefsAdds() { + final Ref mainRef = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, + "refs/heads/main", ObjectId.fromString( + "0000000000000000000000000000000000000001")); + + final Map<String, Ref> refMap = new HashMap<>(); + refMap.put(mainRef.getName(), mainRef); + refMap.put("refs/heads/other", + new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/other", + ObjectId.fromString( + "0000000000000000000000000000000000000002"))); + + final Map<String, String> symRefs = new HashMap<>(); + symRefs.put("HEAD", "refs/heads/main"); + + BasePackConnection.updateWithSymRefs(refMap, symRefs); + + assertThat(refMap, hasKey("HEAD")); + final Ref headRef = refMap.get("HEAD"); + assertThat(headRef, instanceOf(SymbolicRef.class)); + final SymbolicRef headSymRef = (SymbolicRef) headRef; + assertEquals("HEAD", headSymRef.getName()); + assertSame(mainRef, headSymRef.getTarget()); + } + + @Test + public void testUpdateWithSymRefsReplaces() { + final Ref mainRef = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, + "refs/heads/main", ObjectId.fromString( + "0000000000000000000000000000000000000001")); + + final Map<String, Ref> refMap = new HashMap<>(); + refMap.put(mainRef.getName(), mainRef); + refMap.put("HEAD", new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "HEAD", + mainRef.getObjectId())); + refMap.put("refs/heads/other", + new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/other", + ObjectId.fromString( + "0000000000000000000000000000000000000002"))); + + final Map<String, String> symRefs = new HashMap<>(); + symRefs.put("HEAD", "refs/heads/main"); + + BasePackConnection.updateWithSymRefs(refMap, symRefs); + + assertThat(refMap, hasKey("HEAD")); + final Ref headRef = refMap.get("HEAD"); + assertThat(headRef, instanceOf(SymbolicRef.class)); + final SymbolicRef headSymRef = (SymbolicRef) headRef; + assertEquals("HEAD", headSymRef.getName()); + assertSame(mainRef, headSymRef.getTarget()); + } + + @Test + public void testUpdateWithSymRefsWithIndirectsAdds() { + final Ref mainRef = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, + "refs/heads/main", ObjectId.fromString( + "0000000000000000000000000000000000000001")); + + final Map<String, Ref> refMap = new HashMap<>(); + refMap.put(mainRef.getName(), mainRef); + refMap.put("refs/heads/other", + new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/other", + ObjectId.fromString( + "0000000000000000000000000000000000000002"))); + + final Map<String, String> symRefs = new LinkedHashMap<>(); // Ordered + symRefs.put("refs/heads/sym3", "refs/heads/sym2"); // Forward reference + symRefs.put("refs/heads/sym1", "refs/heads/main"); + symRefs.put("refs/heads/sym2", "refs/heads/sym1"); // Backward reference + + BasePackConnection.updateWithSymRefs(refMap, symRefs); + + assertThat(refMap, hasKey("refs/heads/sym1")); + final Ref sym1Ref = refMap.get("refs/heads/sym1"); + assertThat(sym1Ref, instanceOf(SymbolicRef.class)); + final SymbolicRef sym1SymRef = (SymbolicRef) sym1Ref; + assertEquals("refs/heads/sym1", sym1SymRef.getName()); + assertSame(mainRef, sym1SymRef.getTarget()); + + assertThat(refMap, hasKey("refs/heads/sym2")); + final Ref sym2Ref = refMap.get("refs/heads/sym2"); + assertThat(sym2Ref, instanceOf(SymbolicRef.class)); + final SymbolicRef sym2SymRef = (SymbolicRef) sym2Ref; + assertEquals("refs/heads/sym2", sym2SymRef.getName()); + assertSame(sym1SymRef, sym2SymRef.getTarget()); + + assertThat(refMap, hasKey("refs/heads/sym3")); + final Ref sym3Ref = refMap.get("refs/heads/sym3"); + assertThat(sym3Ref, instanceOf(SymbolicRef.class)); + final SymbolicRef sym3SymRef = (SymbolicRef) sym3Ref; + assertEquals("refs/heads/sym3", sym3SymRef.getName()); + assertSame(sym2SymRef, sym3SymRef.getTarget()); + } + + @Test + public void testUpdateWithSymRefsWithIndirectsReplaces() { + final Ref mainRef = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, + "refs/heads/main", ObjectId.fromString( + "0000000000000000000000000000000000000001")); + + final Map<String, Ref> refMap = new HashMap<>(); + refMap.put(mainRef.getName(), mainRef); + refMap.put("refs/heads/sym1", new ObjectIdRef.Unpeeled( + Ref.Storage.LOOSE, "refs/heads/sym1", mainRef.getObjectId())); + refMap.put("refs/heads/sym2", new ObjectIdRef.Unpeeled( + Ref.Storage.LOOSE, "refs/heads/sym2", mainRef.getObjectId())); + refMap.put("refs/heads/sym3", new ObjectIdRef.Unpeeled( + Ref.Storage.LOOSE, "refs/heads/sym3", mainRef.getObjectId())); + refMap.put("refs/heads/other", + new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/other", + ObjectId.fromString( + "0000000000000000000000000000000000000002"))); + + final Map<String, String> symRefs = new LinkedHashMap<>(); // Ordered + symRefs.put("refs/heads/sym3", "refs/heads/sym2"); // Forward reference + symRefs.put("refs/heads/sym1", "refs/heads/main"); + symRefs.put("refs/heads/sym2", "refs/heads/sym1"); // Backward reference + + BasePackConnection.updateWithSymRefs(refMap, symRefs); + + assertThat(refMap, hasKey("refs/heads/sym1")); + final Ref sym1Ref = refMap.get("refs/heads/sym1"); + assertThat(sym1Ref, instanceOf(SymbolicRef.class)); + final SymbolicRef sym1SymRef = (SymbolicRef) sym1Ref; + assertEquals("refs/heads/sym1", sym1SymRef.getName()); + assertSame(mainRef, sym1SymRef.getTarget()); + + assertThat(refMap, hasKey("refs/heads/sym2")); + final Ref sym2Ref = refMap.get("refs/heads/sym2"); + assertThat(sym2Ref, instanceOf(SymbolicRef.class)); + final SymbolicRef sym2SymRef = (SymbolicRef) sym2Ref; + assertEquals("refs/heads/sym2", sym2SymRef.getName()); + assertSame(sym1SymRef, sym2SymRef.getTarget()); + + assertThat(refMap, hasKey("refs/heads/sym3")); + final Ref sym3Ref = refMap.get("refs/heads/sym3"); + assertThat(sym3Ref, instanceOf(SymbolicRef.class)); + final SymbolicRef sym3SymRef = (SymbolicRef) sym3Ref; + assertEquals("refs/heads/sym3", sym3SymRef.getName()); + assertSame(sym2SymRef, sym3SymRef.getTarget()); + } + + @Test + public void testUpdateWithSymRefsIgnoresSelfReference() { + final Ref mainRef = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, + "refs/heads/main", ObjectId.fromString( + "0000000000000000000000000000000000000001")); + + final Map<String, Ref> refMap = new HashMap<>(); + refMap.put(mainRef.getName(), mainRef); + refMap.put("refs/heads/other", + new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/other", + ObjectId.fromString( + "0000000000000000000000000000000000000002"))); + + final Map<String, String> symRefs = new LinkedHashMap<>(); + symRefs.put("refs/heads/sym1", "refs/heads/sym1"); + + BasePackConnection.updateWithSymRefs(refMap, symRefs); + + assertEquals(2, refMap.size()); + assertThat(refMap, not(hasKey("refs/heads/sym1"))); + } + + @Test + public void testUpdateWithSymRefsIgnoreCircularReference() { + final Ref mainRef = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, + "refs/heads/main", ObjectId.fromString( + "0000000000000000000000000000000000000001")); + + final Map<String, Ref> refMap = new HashMap<>(); + refMap.put(mainRef.getName(), mainRef); + refMap.put("refs/heads/other", + new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/other", + ObjectId.fromString( + "0000000000000000000000000000000000000002"))); + + final Map<String, String> symRefs = new LinkedHashMap<>(); + symRefs.put("refs/heads/sym2", "refs/heads/sym1"); + symRefs.put("refs/heads/sym1", "refs/heads/sym2"); + + BasePackConnection.updateWithSymRefs(refMap, symRefs); + + assertEquals(2, refMap.size()); + assertThat(refMap, not(hasKey("refs/heads/sym1"))); + assertThat(refMap, not(hasKey("refs/heads/sym2"))); + } +}
\ No newline at end of file |