summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorMatthaus Owens <matthaus@puppetlabs.com>2016-07-29 15:21:20 -0700
committerMatthaus Owens <matthaus@puppetlabs.com>2016-08-04 13:38:52 -0700
commite1ffab1cac55179011777b9a60bac447b0a62ccf (patch)
tree157fc203415b4f85e86f1e75ee026451138e8af5 /org.eclipse.jgit
parent0ee89f01be7174d61ac3ac6af9805936b18818e5 (diff)
downloadjgit-e1ffab1cac55179011777b9a60bac447b0a62ccf.tar.gz
jgit-e1ffab1cac55179011777b9a60bac447b0a62ccf.zip
Skip cleaning inner repositories by default in CleanCommand
Previously jgit would attempt to clean git repositories that had not been committed by calling a non-recursive delete on them, which would fail as they are directories. This commit addresses that issue in the following ways. Repositories are skipped in a default clean, similarly to cgit and only cleaned when the force flag is applied. When the force flag is applied repositories are deleted using a recursive delete call. The force flag and setForce method are added here to CleanCommand to support this change. Bug: 498367 Change-Id: Ib6cfff65a033d0d0f76395060bf76719e13fc467 Signed-off-by: Matthaus Owens <matthaus@puppetlabs.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/CleanCommand.java84
1 files changed, 73 insertions, 11 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CleanCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CleanCommand.java
index 5967128113..7e331fd844 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CleanCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CleanCommand.java
@@ -43,6 +43,8 @@
*/
package org.eclipse.jgit.api;
+import static org.eclipse.jgit.lib.Constants.DOT_GIT;
+
import java.io.File;
import java.io.IOException;
import java.util.Collections;
@@ -73,6 +75,8 @@ public class CleanCommand extends GitCommand<Set<String>> {
private boolean ignore = true;
+ private boolean force = false;
+
/**
* @param repo
*/
@@ -121,25 +125,69 @@ public class CleanCommand extends GitCommand<Set<String>> {
for (String file : notIgnoredFiles)
if (paths.isEmpty() || paths.contains(file)) {
- if (!dryRun)
- FileUtils.delete(new File(repo.getWorkTree(), file));
- files.add(file);
+ files = cleanPath(file, files);
}
- if (directories)
- for (String dir : notIgnoredDirs)
- if (paths.isEmpty() || paths.contains(dir)) {
- if (!dryRun)
- FileUtils.delete(new File(repo.getWorkTree(), dir),
- FileUtils.RECURSIVE);
- files.add(dir + "/"); //$NON-NLS-1$
- }
+ for (String dir : notIgnoredDirs)
+ if (paths.isEmpty() || paths.contains(dir)) {
+ files = cleanPath(dir, files);
+ }
} catch (IOException e) {
throw new JGitInternalException(e.getMessage(), e);
}
return files;
}
+ /**
+ * When dryRun is false, deletes the specified path from disk. If dryRun
+ * is true, no paths are actually deleted. In both cases, the paths that
+ * would have been deleted are added to inFiles and returned.
+ *
+ * Paths that are directories are recursively deleted when
+ * {@link #directories} is true.
+ * Paths that are git repositories are recursively deleted when
+ * {@link #directories} and {@link #force} are both true.
+ *
+ * @param path
+ * The path to be cleaned
+ * @param inFiles
+ * A set of strings representing the files that have been cleaned
+ * already, the path to be cleaned will be added to this set
+ * before being returned.
+ *
+ * @return a set of strings with the cleaned path added to it
+ * @throws IOException
+ */
+ private Set<String> cleanPath(String path, Set<String> inFiles)
+ throws IOException {
+ File curFile = new File(repo.getWorkTree(), path);
+ if (curFile.isDirectory()) {
+ if (directories) {
+ // Is this directory a git repository?
+ if (new File(curFile, DOT_GIT).exists()) {
+ if (force) {
+ if (!dryRun) {
+ FileUtils.delete(curFile, FileUtils.RECURSIVE);
+ }
+ inFiles.add(path + "/"); //$NON-NLS-1$
+ }
+ } else {
+ if (!dryRun) {
+ FileUtils.delete(curFile, FileUtils.RECURSIVE);
+ }
+ inFiles.add(path + "/"); //$NON-NLS-1$
+ }
+ }
+ } else {
+ if (!dryRun) {
+ FileUtils.delete(curFile, FileUtils.NONE);
+ }
+ inFiles.add(path);
+ }
+
+ return inFiles;
+ }
+
private Set<String> filterIgnorePaths(Set<String> inputPaths,
Set<String> ignoredNotInIndex, boolean exact) {
if (ignore) {
@@ -196,6 +244,20 @@ public class CleanCommand extends GitCommand<Set<String>> {
}
/**
+ * If force is set, directories that are git repositories will also be
+ * deleted.
+ *
+ * @param force
+ * whether or not to delete git repositories
+ * @return {@code this}
+ * @since 4.5
+ */
+ public CleanCommand setForce(boolean force) {
+ this.force = force;
+ return this;
+ }
+
+ /**
* If dirs is set, in addition to files, also clean directories.
*
* @param dirs