]> source.dussan.org Git - jgit.git/commitdiff
RepoCommand: generate relative submodule URLs from absolute URLs. 88/117688/3
authorHan-Wen Nienhuys <hanwen@google.com>
Mon, 19 Feb 2018 18:59:35 +0000 (19:59 +0100)
committerHan-Wen Nienhuys <hanwen@google.com>
Wed, 21 Feb 2018 12:41:50 +0000 (13:41 +0100)
If a manifest file specifies an absolute URL on the same host on which
the superproject resides, rewrite the URLs to be relative.

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Change-Id: Id616611e5195998fb665c8e7806539a3a02e219a

org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoCommand.java

index f4b86e0ce67a985b52c81ce7d4d2bba3ff2e35c0..2253a0421fbd11dddefc13a69f6501ad7a53d0a8 100644 (file)
@@ -438,6 +438,63 @@ public class RepoCommandTest extends RepositoryTestCase {
                dest.close();
        }
 
+       @Test
+       public void absoluteRemoteURLAbsoluteTargetURL() 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();
+               String abs = "https://chromium.googlesource.com";
+               String repoUrl = "https://chromium.googlesource.com/chromium/src";
+               boolean fetchSlash = false;
+               boolean baseSlash = false;
+               do {
+                       do {
+                               String fetchUrl = fetchSlash ? abs + "/" : abs;
+                               String baseUrl = baseSlash ? abs + "/" : abs;
+
+                               StringBuilder xmlContent = new StringBuilder();
+                               xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
+                                       .append("<manifest>")
+                                       .append("<remote name=\"origin\" fetch=\"" + fetchUrl + "\" />")
+                                       .append("<default revision=\"master\" remote=\"origin\" />")
+                                       .append("<project path=\"src\" name=\"chromium/src\" />")
+                                       .append("</manifest>");
+                               RepoCommand cmd = new RepoCommand(dest);
+
+                               IndexedRepos repos = new IndexedRepos();
+                               repos.put(repoUrl, child);
+
+                               RevCommit commit = cmd
+                                       .setInputStream(new ByteArrayInputStream(xmlContent.toString().getBytes(UTF_8)))
+                                       .setRemoteReader(repos)
+                                       .setURI(baseUrl)
+                                       .setTargetURI(abs + "/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", "src", "url");
+                                       assertEquals("../chromium/src", subUrl);
+                               }
+                               fetchSlash = !fetchSlash;
+                       } while (fetchSlash);
+                       baseSlash = !baseSlash;
+               } while (baseSlash);
+               child.close();
+               dest.close();
+       }
+
        @Test
        public void testAddRepoManifest() throws Exception {
                StringBuilder xmlContent = new StringBuilder();
@@ -1188,5 +1245,6 @@ public class RepoCommandTest extends RepositoryTestCase {
                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");
+               testRelative("http://base.com/a/", "http://base.com/a/b", "b");
        }
 }
index 377fdf0199a8fc42c62ad07b3be271a86461804f..8b8df87978da27532f24000e457bcc54d488dd84 100644 (file)
@@ -54,6 +54,7 @@ import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 import java.util.StringJoiner;
 
@@ -749,12 +750,7 @@ public class RepoCommand extends GitCommand<RevCommit> {
         */
        private static final String SLASH = "/"; //$NON-NLS-1$
        static URI relativize(URI current, URI target) {
-
-               // We only handle bare paths for now.
-               if (!target.toString().equals(target.getPath())) {
-                       return target;
-               }
-               if (!current.toString().equals(current.getPath())) {
+               if (!Objects.equals(current.getHost(), target.getHost())) {
                        return target;
                }