aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst
diff options
context:
space:
mode:
authorIvan Frade <ifrade@google.com>2024-05-30 14:04:56 -0700
committerMatthias Sohn <matthias.sohn@sap.com>2024-07-08 14:04:56 +0200
commit784f0baecbbb68f1e136efe862087634d10826eb (patch)
tree53ff701ce55d9b564873d2d436489d4d8f2bce26 /org.eclipse.jgit.test/tst
parente5534111577b0f9c9b50140461adea31f789c06c (diff)
downloadjgit-784f0baecbbb68f1e136efe862087634d10826eb.tar.gz
jgit-784f0baecbbb68f1e136efe862087634d10826eb.zip
RepoCommand: Copy manifest upstream into .gitmodules ref field
Project entries in the manifest with a specific sha1 as revision can use the "upstream" field to report the ref pointing to that sha1. This information is very valuable for downstream tools, as they can limit their search for a blob to the relevant ref, but it gets lost in the translation to .gitmodules. Save the value of the upstream field when available/relevant in the ref field of the .gitmodules entry. Change-Id: I14a2395925618d5e6b34be85466e32f5ef8fbf6e (cherry picked from commit 48465f84014904edddcdd48258c67bc19555d4c3)
Diffstat (limited to 'org.eclipse.jgit.test/tst')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/BareSuperprojectWriterTest.java45
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java88
2 files changed, 133 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/BareSuperprojectWriterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/BareSuperprojectWriterTest.java
index c3b93879b2..5065b57840 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/BareSuperprojectWriterTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/BareSuperprojectWriterTest.java
@@ -12,6 +12,7 @@ package org.eclipse.jgit.gitrepo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -22,6 +23,7 @@ import java.util.List;
import org.eclipse.jgit.gitrepo.BareSuperprojectWriter.BareWriterConfig;
import org.eclipse.jgit.gitrepo.RepoCommand.RemoteReader;
import org.eclipse.jgit.junit.RepositoryTestCase;
+import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
@@ -68,6 +70,49 @@ public class BareSuperprojectWriterTest extends RepositoryTestCase {
}
@Test
+ public void write_setGitModulesContents_pinned() throws Exception {
+ try (Repository bareRepo = createBareRepository()) {
+ RepoProject pinWithUpstream = new RepoProject("pinWithUpstream",
+ "path/x", "cbc0fae7e1911d27e1de37d364698dba4411c78b",
+ "remote", "");
+ pinWithUpstream.setUrl("http://example.com/a");
+ pinWithUpstream.setUpstream("branchX");
+
+ RepoProject pinWithoutUpstream = new RepoProject(
+ "pinWithoutUpstream", "path/y",
+ "cbc0fae7e1911d27e1de37d364698dba4411c78b", "remote", "");
+ pinWithoutUpstream.setUrl("http://example.com/b");
+
+ RemoteReader mockRemoteReader = mock(RemoteReader.class);
+
+ BareSuperprojectWriter w = new BareSuperprojectWriter(bareRepo,
+ null, "refs/heads/master", author, mockRemoteReader,
+ BareWriterConfig.getDefault(), List.of());
+
+ RevCommit commit = w
+ .write(Arrays.asList(pinWithUpstream, pinWithoutUpstream));
+
+ String contents = readContents(bareRepo, commit, ".gitmodules");
+ Config cfg = new Config();
+ cfg.fromText(contents);
+
+ assertThat(cfg.getString("submodule", "pinWithUpstream", "path"),
+ is("path/x"));
+ assertThat(cfg.getString("submodule", "pinWithUpstream", "url"),
+ is("http://example.com/a"));
+ assertThat(cfg.getString("submodule", "pinWithUpstream", "ref"),
+ is("branchX"));
+
+ assertThat(cfg.getString("submodule", "pinWithoutUpstream", "path"),
+ is("path/y"));
+ assertThat(cfg.getString("submodule", "pinWithoutUpstream", "url"),
+ is("http://example.com/b"));
+ assertThat(cfg.getString("submodule", "pinWithoutUpstream", "ref"),
+ nullValue());
+ }
+ }
+
+ @Test
public void write_setExtraContents() throws Exception {
try (Repository bareRepo = createBareRepository()) {
RepoProject repoProject = new RepoProject("subprojectX", "path/to",
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java
index ca6f2e1053..3162e7910b 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java
@@ -1171,6 +1171,94 @@ public class RepoCommandTest extends RepositoryTestCase {
}
}
+ @Test
+ public void testRecordRemoteBranch_pinned() throws Exception {
+ Repository remoteDb = createBareRepository();
+ Repository tempDb = createWorkRepository();
+
+ StringBuilder xmlContent = new StringBuilder();
+ xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
+ .append("<manifest>")
+ .append("<remote name=\"remote1\" fetch=\".\" />")
+ .append("<default revision=\"master\" remote=\"remote1\" />")
+ .append("<project path=\"pin-noupstream\"")
+ .append(" name=\"pin-noupstream\"")
+ .append(" revision=\"76ce6d91a2e07fdfcbfc8df6970c9e98a98e36a0\" />")
+ .append("<project path=\"pin-upstream\"")
+ .append(" name=\"pin-upstream\"")
+ .append(" upstream=\"branchX\"")
+ .append(" revision=\"76ce6d91a2e07fdfcbfc8df6970c9e98a98e36a0\" />")
+ .append("</manifest>");
+ JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
+ xmlContent.toString());
+
+ RepoCommand command = new RepoCommand(remoteDb);
+ command.setPath(
+ tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
+ .setURI(rootUri).setRecordRemoteBranch(true).call();
+ // Clone it
+ File directory = createTempDirectory("testBareRepo");
+ try (Repository localDb = Git.cloneRepository().setDirectory(directory)
+ .setURI(remoteDb.getDirectory().toURI().toString()).call()
+ .getRepository();) {
+ // The .gitmodules file should exist
+ File gitmodules = new File(localDb.getWorkTree(), ".gitmodules");
+ assertTrue("The .gitmodules file should exist",
+ gitmodules.exists());
+ FileBasedConfig c = new FileBasedConfig(gitmodules, FS.DETECTED);
+ c.load();
+ assertEquals("Pinned submodule with upstream records the ref",
+ "branchX", c.getString("submodule", "pin-upstream", "ref"));
+ assertNull("Pinned submodule without upstream don't have ref",
+ c.getString("submodule", "pin-noupstream", "ref"));
+ }
+ }
+
+ @Test
+ public void testRecordRemoteBranch_pinned_nameConflict() throws Exception {
+ Repository remoteDb = createBareRepository();
+ Repository tempDb = createWorkRepository();
+
+ StringBuilder xmlContent = new StringBuilder();
+ xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
+ .append("<manifest>")
+ .append("<remote name=\"remote1\" fetch=\".\" />")
+ .append("<default revision=\"master\" remote=\"remote1\" />")
+ .append("<project path=\"pin-upstream\"")
+ .append(" name=\"pin-upstream\"")
+ .append(" upstream=\"branchX\"")
+ .append(" revision=\"76ce6d91a2e07fdfcbfc8df6970c9e98a98e36a0\" />")
+ .append("<project path=\"pin-upstream-name-conflict\"")
+ .append(" name=\"pin-upstream\"")
+ .append(" upstream=\"branchX\"")
+ .append(" revision=\"76ce6d91a2e07fdfcbfc8df6970c9e98a98e36a0\" />")
+ .append("</manifest>");
+ JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
+ xmlContent.toString());
+
+ RepoCommand command = new RepoCommand(remoteDb);
+ command.setPath(
+ tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
+ .setURI(rootUri).setRecordRemoteBranch(true).call();
+ // Clone it
+ File directory = createTempDirectory("testBareRepo");
+ try (Repository localDb = Git.cloneRepository().setDirectory(directory)
+ .setURI(remoteDb.getDirectory().toURI().toString()).call()
+ .getRepository();) {
+ // The .gitmodules file should exist
+ File gitmodules = new File(localDb.getWorkTree(), ".gitmodules");
+ assertTrue("The .gitmodules file should exist",
+ gitmodules.exists());
+ FileBasedConfig c = new FileBasedConfig(gitmodules, FS.DETECTED);
+ c.load();
+ assertEquals("Upstream is preserved in name conflict", "branchX",
+ c.getString("submodule", "pin-upstream/pin-upstream",
+ "ref"));
+ assertEquals("Upstream is preserved in name conflict (other side)",
+ "branchX", c.getString("submodule",
+ "pin-upstream/pin-upstream-name-conflict", "ref"));
+ }
+ }
@Test
public void testRecordSubmoduleLabels() throws Exception {