diff options
author | Han-Wen Nienhuys <hanwen@google.com> | 2017-03-28 14:00:38 +0200 |
---|---|---|
committer | David Pursehouse <david.pursehouse@gmail.com> | 2017-04-13 10:53:58 +0900 |
commit | fe5437e96b91222e30d9105e7bab490fd68c2d52 (patch) | |
tree | 69ed7c1809f904f246eaf5836ec26a72581fbd67 /org.eclipse.jgit.test | |
parent | e730fcce776b795692bbae086f0333bb4ae38a6c (diff) | |
download | jgit-fe5437e96b91222e30d9105e7bab490fd68c2d52.tar.gz jgit-fe5437e96b91222e30d9105e7bab490fd68c2d52.zip |
Fix RepoCommand to allow for relative URLs
This is necessary for deploying submodules on android.googlesource.com.
* Allow an empty base URL. This is useful if the 'fetch' field is "."
and all names are relative to some host root.
* The URLs in the resulting superproject are relative to the
superproject's URL. Add RepoCommand#setDestinationURI to
set this. If unset, the existing behavior is maintained.
* Add two tests for the Android and Gerrit case, checking the URL
format in .gitmodules; the tests use a custom RemoteReader which is
representative of the use of this class in Gerrit's Supermanifest
plugin.
Change-Id: Ia75530226120d75aa0017c5410fd65d0563e91b
Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java | 127 |
1 files changed, 127 insertions, 0 deletions
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 9cf4569d66..24b5ad7dab 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 @@ -46,12 +46,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -184,6 +186,107 @@ public class RepoCommandTest extends RepositoryTestCase { } @Test + public void androidSetup() throws Exception { + Repository child = Git.cloneRepository() + .setURI(groupADb.getDirectory().toURI().toString()) + .setDirectory(createUniqueTestGitDir(true)).setBare(true).call() + .getRepository(); + + Repository dest = Git.cloneRepository() + .setURI(db.getDirectory().toURI().toString()) + .setDirectory(createUniqueTestGitDir(true)).setBare(true).call() + .getRepository(); + + assertTrue(dest.isBare()); + assertTrue(child.isBare()); + + 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=\"base\" name=\"platform/base\" />") + .append("</manifest>"); + RepoCommand cmd = new RepoCommand(dest); + + IndexedRepos repos = new IndexedRepos(); + repos.put("platform/base", child); + + RevCommit commit = cmd + .setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(StandardCharsets.UTF_8))) + .setRemoteReader(repos) + .setURI("platform/") + .setTargetURI("platform/superproject") + .setRecordRemoteBranch(true) + .setRecordSubmoduleLabels(true) + .call(); + + String idStr = commit.getId().name() + ":" + ".gitmodules"; + ObjectId modId = dest.resolve(idStr); + + try (ObjectReader reader = dest.newObjectReader()) { + byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE); + Config base = new Config(); + BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); + String subUrl = cfg.getString("submodule", "base", "url"); + assertEquals(subUrl, "../base"); + } + + child.close(); + dest.close(); + } + + @Test + public void gerritSetup() throws Exception { + Repository child = + Git.cloneRepository().setURI(groupADb.getDirectory().toURI().toString()) + .setDirectory(createUniqueTestGitDir(true)) + .setBare(true).call().getRepository(); + + Repository dest = Git.cloneRepository() + .setURI(db.getDirectory().toURI().toString()).setDirectory(createUniqueTestGitDir(true)) + .setBare(true).call().getRepository(); + + assertTrue(dest.isBare()); + assertTrue(child.isBare()); + + 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=\"plugins/cookbook\" name=\"plugins/cookbook\" />") + .append("</manifest>"); + RepoCommand cmd = new RepoCommand(dest); + + IndexedRepos repos = new IndexedRepos(); + repos.put("plugins/cookbook", child); + + RevCommit commit = cmd + .setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(StandardCharsets.UTF_8))) + .setRemoteReader(repos) + .setURI("") + .setTargetURI("gerrit") + .setRecordRemoteBranch(true) + .setRecordSubmoduleLabels(true) + .call(); + + String idStr = commit.getId().name() + ":" + ".gitmodules"; + ObjectId modId = dest.resolve(idStr); + + try (ObjectReader reader = dest.newObjectReader()) { + byte[] bytes = reader.open(modId).getCachedBytes(Integer.MAX_VALUE); + Config base = new Config(); + BlobBasedConfig cfg = new BlobBasedConfig(base, bytes); + String subUrl = cfg.getString("submodule", "plugins/cookbook", "url"); + assertEquals(subUrl, "../plugins/cookbook"); + } + + child.close(); + dest.close(); + } + + @Test public void absoluteRemoteURL() throws Exception { Repository child = Git.cloneRepository().setURI(groupADb.getDirectory().toURI().toString()) @@ -217,6 +320,7 @@ public class RepoCommandTest extends RepositoryTestCase { .setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(StandardCharsets.UTF_8))) .setRemoteReader(repos) .setURI(baseUrl) + .setTargetURI("gerrit") .setRecordRemoteBranch(true) .setRecordSubmoduleLabels(true) .call(); @@ -997,4 +1101,27 @@ public class RepoCommandTest extends RepositoryTestCase { start = newStart; } } + + void testRelative(String a, String b, String want) { + String got = RepoCommand.relativize(URI.create(a), URI.create(b)).toString(); + + if (!got.equals(want)) { + fail(String.format("relative('%s', '%s') = '%s', want '%s'", a, b, got, want)); + } + } + + @Test + public void relative() { + testRelative("a/b/", "a/", "../"); + // Normalization: + testRelative("a/p/..//b/", "a/", "../"); + testRelative("a/b", "a/", ""); + testRelative("a/", "a/b/", "b/"); + testRelative("a/", "a/b", "b"); + testRelative("/a/b/c", "/b/c", "../../b/c"); + testRelative("/abc", "bcd", "bcd"); + testRelative("abc", "/bcd", "/bcd"); + testRelative("http://a", "a/b", "a/b"); + testRelative("http://base.com/a/", "http://child.com/a/b", "http://child.com/a/b"); + } } |