]> source.dussan.org Git - jgit.git/commitdiff
Skip nested copyfiles in RepoCommand. 83/62983/8
authorYuxuan 'fishy' Wang <fishywang@google.com>
Fri, 18 Dec 2015 02:27:56 +0000 (18:27 -0800)
committerYuxuan 'fishy' Wang <fishywang@google.com>
Tue, 22 Dec 2015 22:05:35 +0000 (14:05 -0800)
Similar to nested directories, nested copyfiles won't work with git submodule
either.

Change-Id: Idbe965ec20a682fca0432802858162f8238f05de
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
org.eclipse.jgit/src/org/eclipse/jgit/gitrepo/RepoProject.java

index b6649b3f0510931ccb070aae577702ee8524b2c2..524d0b8e7ef7a98f95ef99f04177edfe062d4baf 100644 (file)
@@ -409,6 +409,7 @@ public class RepoCommandTest extends RepositoryTestCase {
                                        .append("<project path=\"foo\" name=\"").append(defaultUri)
                                        .append("\" revision=\"").append(BRANCH).append("\" >")
                                        .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
+                                       .append("<copyfile src=\"hello.txt\" dest=\"foo/Hello\" />")
                                        .append("</project>").append("</manifest>");
                        JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
                                        xmlContent.toString());
@@ -423,8 +424,12 @@ public class RepoCommandTest extends RepositoryTestCase {
                                        .getRepository();
                        // The Hello file should exist
                        File hello = new File(localDb.getWorkTree(), "Hello");
-                       localDb.close();
                        assertTrue("The Hello file should exist", hello.exists());
+                       // The foo/Hello file should be skipped.
+                       File foohello = new File(localDb.getWorkTree(), "foo/Hello");
+                       assertFalse(
+                                       "The foo/Hello file should be skipped", foohello.exists());
+                       localDb.close();
                        // The content of Hello file should be expected
                        BufferedReader reader = new BufferedReader(new FileReader(hello));
                        String content = reader.readLine();
index 891479d1f4eb994bc6f4c2e8b1349e417fb0f3f9..7eb955006e4e57b3b86081a703c1956e041872f2 100644 (file)
@@ -338,6 +338,20 @@ public class ManifestParser extends DefaultHandler {
                        else
                                last = p;
                }
+               removeNestedCopyfiles();
+       }
+
+       /** Remove copyfiles that sit in a subdirectory of any other project. */
+       void removeNestedCopyfiles() {
+               for (RepoProject proj : filteredProjects) {
+                       List<CopyFile> copyfiles = new ArrayList<>(proj.getCopyFiles());
+                       proj.clearCopyFiles();
+                       for (CopyFile copyfile : copyfiles) {
+                               if (!isNestedCopyfile(copyfile)) {
+                                       proj.addCopyFile(copyfile);
+                               }
+                       }
+               }
        }
 
        boolean inGroups(RepoProject proj) {
@@ -357,4 +371,22 @@ public class ManifestParser extends DefaultHandler {
                }
                return false;
        }
+
+       private boolean isNestedCopyfile(CopyFile copyfile) {
+               if (copyfile.dest.indexOf('/') == -1) {
+                       // If the copyfile is at root level then it won't be nested.
+                       return false;
+               }
+               for (RepoProject proj : filteredProjects) {
+                       if (proj.getPath().compareTo(copyfile.dest) > 0) {
+                               // Early return as remaining projects can't be ancestor of this
+                               // copyfile config (filteredProjects is sorted).
+                               return false;
+                       }
+                       if (proj.isAncestorOf(copyfile.dest)) {
+                               return true;
+                       }
+               }
+               return false;
+       }
 }
index 9a072114a7334bc53d0daeb402feca61cb8a4018..915066d58fc72f59219ec981b4dd7bf166f7ae2b 100644 (file)
@@ -258,6 +258,15 @@ public class RepoProject implements Comparable<RepoProject> {
                this.copyfiles.addAll(copyfiles);
        }
 
+       /**
+        * Clear all the copyfiles.
+        *
+        * @since 4.2
+        */
+       public void clearCopyFiles() {
+               this.copyfiles.clear();
+       }
+
        private String getPathWithSlash() {
                if (path.endsWith("/")) //$NON-NLS-1$
                        return path;
@@ -273,7 +282,19 @@ public class RepoProject implements Comparable<RepoProject> {
         * @return true if this sub repo is the ancestor of given sub repo.
         */
        public boolean isAncestorOf(RepoProject that) {
-               return that.getPathWithSlash().startsWith(this.getPathWithSlash());
+               return isAncestorOf(that.getPathWithSlash());
+       }
+
+       /**
+        * Check if this sub repo is an ancestor of the given path.
+        *
+        * @param path
+        *            path to be checked to see if it is within this repository
+        * @return true if this sub repo is an ancestor of the given path.
+        * @since 4.2
+        */
+       public boolean isAncestorOf(String path) {
+               return path.startsWith(getPathWithSlash());
        }
 
        @Override