]> source.dussan.org Git - jgit.git/commitdiff
Fix CleanCommand not to throw FileNotFoundExceptions 01/116801/2
authorChristian Halstrick <christian.halstrick@sap.com>
Tue, 6 Feb 2018 14:32:14 +0000 (15:32 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Thu, 8 Feb 2018 09:27:21 +0000 (10:27 +0100)
When CleanCommand is collecting the files and folders to be deleted
it may happen that the list of directories contains obsolete entries.
E.g. a folder and its parent folder may be in the list. Only the
parent folder would be sufficient.

This was a reason for hitting FileNotFoundExceptions when finally
trying to delete the files and folders. Improve CleanCommand
to ignore files to be deleted which are already gone.

Bug: 514434
Change-Id: I10caa01bfb9cec5967dfdaea50c6e4a713eeeabd
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CleanCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CleanCommand.java

index 85436db472baf98aba266b3dc6401e084e55e886..3921c96cdc79839622df4bb218c7a54e13ca2a2f 100644 (file)
@@ -48,10 +48,12 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Set;
 import java.util.TreeSet;
 
 import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.api.errors.NoFilepatternException;
 import org.eclipse.jgit.errors.NoWorkTreeException;
 import org.eclipse.jgit.junit.RepositoryTestCase;
 import org.eclipse.jgit.lib.Repository;
@@ -296,4 +298,18 @@ public class CleanCommandTest extends RepositoryTestCase {
                // The inner repository should be cleaned this time
                assertTrue(forceCleanedFiles.contains(innerRepoName + "/"));
        }
+
+       @Test
+       // To proof Bug 514434. No assertions, but before the bugfix
+       // this test was throwing Exceptions
+       public void testFilesShouldBeCleanedInSubSubFolders()
+                       throws IOException, NoFilepatternException, GitAPIException {
+               writeTrashFile(".gitignore",
+                               "/ignored-dir\n/sub-noclean/Ignored.txt\n/this_is_ok\n/this_is/not_ok\n");
+               git.add().addFilepattern(".gitignore").call();
+               git.commit().setMessage("adding .gitignore").call();
+               writeTrashFile("this_is_ok/more/subdirs/file.txt", "1");
+               writeTrashFile("this_is/not_ok/more/subdirs/file.txt", "2");
+               git.clean().setCleanDirectories(true).setIgnore(false).call();
+       }
 }
index c81425067c5cdfe91319f7ff0d710917086bc3ac..0d9fe41afb1644ab192ae09d5d9617650a635b80 100644 (file)
@@ -103,27 +103,25 @@ public class CleanCommand extends GitCommand<Set<String>> {
                        StatusCommand command = new StatusCommand(repo);
                        Status status = command.call();
 
-                       Set<String> untrackedAndIgnoredFiles = new TreeSet<>(
-                                       status.getUntracked());
-                       Set<String> untrackedAndIgnoredDirs = new TreeSet<>(
+                       Set<String> untrackedFiles = new TreeSet<>(status.getUntracked());
+                       Set<String> untrackedDirs = new TreeSet<>(
                                        status.getUntrackedFolders());
 
                        FS fs = getRepository().getFS();
                        for (String p : status.getIgnoredNotInIndex()) {
                                File f = new File(repo.getWorkTree(), p);
-                               if (fs.isFile(f) || fs.isSymLink(f))
-                                       untrackedAndIgnoredFiles.add(p);
-                               else if (fs.isDirectory(f))
-                                       untrackedAndIgnoredDirs.add(p);
+                               if (fs.isFile(f) || fs.isSymLink(f)) {
+                                       untrackedFiles.add(p);
+                               } else if (fs.isDirectory(f)) {
+                                       untrackedDirs.add(p);
+                               }
                        }
 
-                       Set<String> filtered = filterFolders(untrackedAndIgnoredFiles,
-                                       untrackedAndIgnoredDirs);
+                       Set<String> filtered = filterFolders(untrackedFiles, untrackedDirs);
 
                        Set<String> notIgnoredFiles = filterIgnorePaths(filtered,
                                        status.getIgnoredNotInIndex(), true);
-                       Set<String> notIgnoredDirs = filterIgnorePaths(
-                                       untrackedAndIgnoredDirs,
+                       Set<String> notIgnoredDirs = filterIgnorePaths(untrackedDirs,
                                        status.getIgnoredNotInIndex(), false);
 
                        for (String file : notIgnoredFiles)
@@ -174,20 +172,22 @@ public class CleanCommand extends GitCommand<Set<String>> {
                                if (new File(curFile, DOT_GIT).exists()) {
                                        if (force) {
                                                if (!dryRun) {
-                                                       FileUtils.delete(curFile, FileUtils.RECURSIVE);
+                                                       FileUtils.delete(curFile, FileUtils.RECURSIVE
+                                                                       | FileUtils.SKIP_MISSING);
                                                }
                                                inFiles.add(path + "/"); //$NON-NLS-1$
                                        }
                                } else {
                                        if (!dryRun) {
-                                               FileUtils.delete(curFile, FileUtils.RECURSIVE);
+                                               FileUtils.delete(curFile,
+                                                               FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
                                        }
                                        inFiles.add(path + "/"); //$NON-NLS-1$
                                }
                        }
                } else {
                        if (!dryRun) {
-                               FileUtils.delete(curFile, FileUtils.NONE);
+                               FileUtils.delete(curFile, FileUtils.SKIP_MISSING);
                        }
                        inFiles.add(path);
                }