]> source.dussan.org Git - jgit.git/commitdiff
[gitrepo] Support revision in remote tag. 26/65326/4
authorYuxuan 'fishy' Wang <fishywang@google.com>
Wed, 27 Jan 2016 23:01:47 +0000 (15:01 -0800)
committerYuxuan 'fishy' Wang <fishywang@google.com>
Thu, 28 Jan 2016 00:01:06 +0000 (16:01 -0800)
Repo manifest file allows revision attribute in <remote> tag. This change
teaches JGit to read that information.

Change-Id: I1c878a2505b9d09fa09fbd404a119b71f2fb8fdb
Signed-off-by: Yuxuan 'fishy' Wang <fishywang@google.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/gitrepo/RepoCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/ManifestParser.java

index 524d0b8e7ef7a98f95ef99f04177edfe062d4baf..164c4ba4e526dc696ae3fe60d6dc4890dcb9c11f 100644 (file)
@@ -747,6 +747,59 @@ public class RepoCommandTest extends RepositoryTestCase {
                }
        }
 
+       @Test
+       public void testRemoteRevision() throws Exception {
+               StringBuilder xmlContent = new StringBuilder();
+               xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
+                       .append("<manifest>")
+                       .append("<remote name=\"remote1\" fetch=\".\" />")
+                       .append("<remote name=\"remote2\" fetch=\".\" revision=\"")
+                       .append(BRANCH)
+                       .append("\" />")
+                       .append("<default remote=\"remote1\" revision=\"master\" />")
+                       .append("<project path=\"foo\" remote=\"remote2\" name=\"")
+                       .append(defaultUri)
+                       .append("\" />")
+                       .append("</manifest>");
+               writeTrashFile("manifest.xml", xmlContent.toString());
+               RepoCommand command = new RepoCommand(db);
+               command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
+                       .setURI(rootUri)
+                       .call();
+               File hello = new File(db.getWorkTree(), "foo/hello.txt");
+               BufferedReader reader = new BufferedReader(new FileReader(hello));
+               String content = reader.readLine();
+               reader.close();
+               assertEquals("submodule content should be as expected",
+                               "branch world", content);
+       }
+
+       @Test
+       public void testDefaultRemoteRevision() throws Exception {
+               StringBuilder xmlContent = new StringBuilder();
+               xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
+                       .append("<manifest>")
+                       .append("<remote name=\"remote1\" fetch=\".\" revision=\"")
+                       .append(BRANCH)
+                       .append("\" />")
+                       .append("<default remote=\"remote1\" />")
+                       .append("<project path=\"foo\" name=\"")
+                       .append(defaultUri)
+                       .append("\" />")
+                       .append("</manifest>");
+               writeTrashFile("manifest.xml", xmlContent.toString());
+               RepoCommand command = new RepoCommand(db);
+               command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
+                       .setURI(rootUri)
+                       .call();
+               File hello = new File(db.getWorkTree(), "foo/hello.txt");
+               BufferedReader reader = new BufferedReader(new FileReader(hello));
+               String content = reader.readLine();
+               reader.close();
+               assertEquals("submodule content should be as expected",
+                               "branch world", content);
+       }
+
        private void resolveRelativeUris() {
                // Find the longest common prefix ends with "/" as rootUri.
                defaultUri = defaultDb.getDirectory().toURI().toString();
index 7eb955006e4e57b3b86081a703c1956e041872f2..796b422bb211b72de9a45c4021f59a596e919725 100644 (file)
@@ -80,7 +80,7 @@ public class ManifestParser extends DefaultHandler {
        private final String baseUrl;
        private final String defaultBranch;
        private final Repository rootRepo;
-       private final Map<String, String> remotes;
+       private final Map<String, Remote> remotes;
        private final Set<String> plusGroups;
        private final Set<String> minusGroups;
        private final List<RepoProject> projects;
@@ -146,7 +146,7 @@ public class ManifestParser extends DefaultHandler {
                        }
                }
 
-               remotes = new HashMap<String, String>();
+               remotes = new HashMap<String, Remote>();
                projects = new ArrayList<RepoProject>();
                filteredProjects = new ArrayList<RepoProject>();
        }
@@ -195,14 +195,14 @@ public class ManifestParser extends DefaultHandler {
                } else if ("remote".equals(qName)) { //$NON-NLS-1$
                        String alias = attributes.getValue("alias"); //$NON-NLS-1$
                        String fetch = attributes.getValue("fetch"); //$NON-NLS-1$
-                       remotes.put(attributes.getValue("name"), fetch); //$NON-NLS-1$
+                       String revision = attributes.getValue("revision"); //$NON-NLS-1$
+                       Remote remote = new Remote(fetch, revision);
+                       remotes.put(attributes.getValue("name"), remote); //$NON-NLS-1$
                        if (alias != null)
-                               remotes.put(alias, fetch);
+                               remotes.put(alias, remote);
                } else if ("default".equals(qName)) { //$NON-NLS-1$
                        defaultRemote = attributes.getValue("remote"); //$NON-NLS-1$
                        defaultRevision = attributes.getValue("revision"); //$NON-NLS-1$
-                       if (defaultRevision == null)
-                               defaultRevision = defaultBranch;
                } else if ("copyfile".equals(qName)) { //$NON-NLS-1$
                        if (currentProject == null)
                                throw new SAXException(RepoText.get().invalidManifest);
@@ -268,8 +268,18 @@ public class ManifestParser extends DefaultHandler {
                } catch (URISyntaxException e) {
                        throw new SAXException(e);
                }
+               if (defaultRevision == null && defaultRemote != null) {
+                       Remote remote = remotes.get(defaultRemote);
+                       if (remote != null) {
+                               defaultRevision = remote.revision;
+                       }
+                       if (defaultRevision == null) {
+                               defaultRevision = defaultBranch;
+                       }
+               }
                for (RepoProject proj : projects) {
                        String remote = proj.getRemote();
+                       String revision = defaultRevision;
                        if (remote == null) {
                                if (defaultRemote == null) {
                                        if (filename != null)
@@ -281,16 +291,22 @@ public class ManifestParser extends DefaultHandler {
                                                                RepoText.get().errorNoDefault);
                                }
                                remote = defaultRemote;
+                       } else {
+                               Remote r = remotes.get(remote);
+                               if (r != null && r.revision != null) {
+                                       revision = r.revision;
+                               }
                        }
                        String remoteUrl = remoteUrls.get(remote);
                        if (remoteUrl == null) {
-                               remoteUrl = baseUri.resolve(remotes.get(remote)).toString();
+                               remoteUrl =
+                                               baseUri.resolve(remotes.get(remote).fetch).toString();
                                if (!remoteUrl.endsWith("/")) //$NON-NLS-1$
                                        remoteUrl = remoteUrl + "/"; //$NON-NLS-1$
                                remoteUrls.put(remote, remoteUrl);
                        }
                        proj.setUrl(remoteUrl + proj.getName())
-                                       .setDefaultRevision(defaultRevision);
+                                       .setDefaultRevision(revision);
                }
 
                filteredProjects.addAll(projects);
@@ -389,4 +405,14 @@ public class ManifestParser extends DefaultHandler {
                }
                return false;
        }
+
+       private static class Remote {
+               final String fetch;
+               final String revision;
+
+               Remote(String fetch, String revision) {
+                       this.fetch = fetch;
+                       this.revision = revision;
+               }
+       }
 }