]> source.dussan.org Git - jgit.git/commitdiff
CleanCommand: add the ability to do a dry run 53/3553/4
authorAbhishek Bhatnagar <abhatnag@redhat.com>
Fri, 27 May 2011 20:30:27 +0000 (16:30 -0400)
committerChris Aniszczyk <caniszczyk@gmail.com>
Tue, 31 May 2011 14:15:07 +0000 (09:15 -0500)
Change-Id: I7b81a7e34a771951e2e7b789b080b2bfb8656e5c
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CleanCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CleanCommand.java

index 760acbb57fa05a897d241b8933ec9ac00a5f1271..a660a5292b5428efaeb842804829bb152dffb939 100644 (file)
@@ -113,4 +113,23 @@ public class CleanCommandTest extends RepositoryTestCase {
                assertTrue(!cleanedFiles.contains("File2.txt"));
        }
 
+       @Test
+       public void testCleanWithDryRun() throws NoWorkTreeException, IOException {
+               // create status
+               StatusCommand command = git.status();
+               Status status = command.call();
+               Set<String> files = status.getUntracked();
+               assertTrue(files.size() > 0);
+
+               // run clean
+               Set<String> cleanedFiles = git.clean().setDryRun(true).call();
+
+               status = git.status().call();
+               files = status.getUntracked();
+
+               assertTrue(files.size() == 2);
+               assertTrue(cleanedFiles.contains("File2.txt"));
+               assertTrue(cleanedFiles.contains("File3.txt"));
+       }
+
 }
index d0d40c4679b94da30176bd009f047faf694c484d..e31f119cb210acf7ac50afecfa16771850594483 100644 (file)
@@ -64,6 +64,8 @@ public class CleanCommand extends GitCommand<Set<String>> {
 
        private Set<String> paths = Collections.emptySet();
 
+       private boolean dryRun;
+
        /**
         * @param repo
         */
@@ -86,7 +88,8 @@ public class CleanCommand extends GitCommand<Set<String>> {
                        Status status = command.call();
                        for (String file : status.getUntracked()) {
                                if (paths.isEmpty() || paths.contains(file)) {
-                                       FileUtils.delete(new File(repo.getWorkTree(), file));
+                                       if (!dryRun)
+                                               FileUtils.delete(new File(repo.getWorkTree(), file));
                                        files.add(file);
                                }
                        }
@@ -108,4 +111,15 @@ public class CleanCommand extends GitCommand<Set<String>> {
                return this;
        }
 
+       /**
+        * If dryRun is set, the paths in question will not actually be deleted.
+        * 
+        * @param dryRun
+        *            whether to do a dry run or not
+        * @return {@code this}
+        */
+       public CleanCommand setDryRun(boolean dryRun) {
+               this.dryRun = dryRun;
+               return this;
+       }
 }