You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CleanCommand.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C) 2011, Chris Aniszczyk <zx@redhat.com>
  3. * Copyright (C) 2011, Abhishek Bhatnagar <abhatnag@redhat.com> and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.api;
  12. import static org.eclipse.jgit.lib.Constants.DOT_GIT;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.Collections;
  16. import java.util.Set;
  17. import java.util.TreeSet;
  18. import org.eclipse.jgit.api.errors.GitAPIException;
  19. import org.eclipse.jgit.api.errors.JGitInternalException;
  20. import org.eclipse.jgit.errors.NoWorkTreeException;
  21. import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.util.FS;
  24. import org.eclipse.jgit.util.FileUtils;
  25. /**
  26. * Remove untracked files from the working tree
  27. *
  28. * @see <a
  29. * href="http://www.kernel.org/pub/software/scm/git/docs/git-clean.html"
  30. * >Git documentation about Clean</a>
  31. */
  32. public class CleanCommand extends GitCommand<Set<String>> {
  33. private Set<String> paths = Collections.emptySet();
  34. private boolean dryRun;
  35. private boolean directories;
  36. private boolean ignore = true;
  37. private boolean force = false;
  38. /**
  39. * Constructor for CleanCommand
  40. *
  41. * @param repo
  42. * the {@link org.eclipse.jgit.lib.Repository}
  43. */
  44. protected CleanCommand(Repository repo) {
  45. super(repo);
  46. }
  47. /**
  48. * {@inheritDoc}
  49. * <p>
  50. * Executes the {@code clean} command with all the options and parameters
  51. * collected by the setter methods of this class. Each instance of this
  52. * class should only be used for one invocation of the command (means: one
  53. * call to {@link #call()})
  54. */
  55. @Override
  56. public Set<String> call() throws NoWorkTreeException, GitAPIException {
  57. Set<String> files = new TreeSet<>();
  58. try {
  59. StatusCommand command = new StatusCommand(repo);
  60. Status status = command.call();
  61. Set<String> untrackedFiles = new TreeSet<>(status.getUntracked());
  62. Set<String> untrackedDirs = new TreeSet<>(
  63. status.getUntrackedFolders());
  64. FS fs = getRepository().getFS();
  65. for (String p : status.getIgnoredNotInIndex()) {
  66. File f = new File(repo.getWorkTree(), p);
  67. if (fs.isFile(f) || fs.isSymLink(f)) {
  68. untrackedFiles.add(p);
  69. } else if (fs.isDirectory(f)) {
  70. untrackedDirs.add(p);
  71. }
  72. }
  73. Set<String> filtered = filterFolders(untrackedFiles, untrackedDirs);
  74. Set<String> notIgnoredFiles = filterIgnorePaths(filtered,
  75. status.getIgnoredNotInIndex(), true);
  76. Set<String> notIgnoredDirs = filterIgnorePaths(untrackedDirs,
  77. status.getIgnoredNotInIndex(), false);
  78. for (String file : notIgnoredFiles)
  79. if (paths.isEmpty() || paths.contains(file) || isFileInPaths(file) ) {
  80. files = cleanPath(file, files);
  81. }
  82. for (String dir : notIgnoredDirs)
  83. if (paths.isEmpty() || paths.contains(dir) || isFileInPaths(dir) ) {
  84. files = cleanPath(dir, files);
  85. }
  86. } catch (IOException e) {
  87. throw new JGitInternalException(e.getMessage(), e);
  88. } finally {
  89. if (!dryRun && !files.isEmpty()) {
  90. repo.fireEvent(new WorkingTreeModifiedEvent(null, files));
  91. }
  92. }
  93. return files;
  94. }
  95. protected boolean isFileInPaths(final String file) {
  96. if(paths.isEmpty()){
  97. return false;
  98. }
  99. String path=file;
  100. final Set<String> dirs = new HashSet<String>();
  101. while(true){
  102. int idx = path.lastIndexOf("/");
  103. if( idx> 0 ){
  104. path=file.substring(0,idx);
  105. if(paths.contains(path)){
  106. paths.addAll(dirs);
  107. return true;
  108. }
  109. dirs.add(path);
  110. }else{
  111. break;
  112. }
  113. return idx == -1 ? false : ;
  114. }
  115. return false;
  116. }
  117. /**
  118. * When dryRun is false, deletes the specified path from disk. If dryRun
  119. * is true, no paths are actually deleted. In both cases, the paths that
  120. * would have been deleted are added to inFiles and returned.
  121. *
  122. * Paths that are directories are recursively deleted when
  123. * {@link #directories} is true.
  124. * Paths that are git repositories are recursively deleted when
  125. * {@link #directories} and {@link #force} are both true.
  126. *
  127. * @param path
  128. * The path to be cleaned
  129. * @param inFiles
  130. * A set of strings representing the files that have been cleaned
  131. * already, the path to be cleaned will be added to this set
  132. * before being returned.
  133. *
  134. * @return a set of strings with the cleaned path added to it
  135. * @throws IOException
  136. */
  137. private Set<String> cleanPath(String path, Set<String> inFiles)
  138. throws IOException {
  139. File curFile = new File(repo.getWorkTree(), path);
  140. if (curFile.isDirectory()) {
  141. if (directories) {
  142. // Is this directory a git repository?
  143. if (new File(curFile, DOT_GIT).exists()) {
  144. if (force) {
  145. if (!dryRun) {
  146. FileUtils.delete(curFile, FileUtils.RECURSIVE
  147. | FileUtils.SKIP_MISSING);
  148. }
  149. inFiles.add(path + "/"); //$NON-NLS-1$
  150. }
  151. } else {
  152. if (!dryRun) {
  153. FileUtils.delete(curFile,
  154. FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
  155. }
  156. inFiles.add(path + "/"); //$NON-NLS-1$
  157. }
  158. }
  159. } else {
  160. if (!dryRun) {
  161. FileUtils.delete(curFile, FileUtils.SKIP_MISSING);
  162. }
  163. inFiles.add(path);
  164. }
  165. return inFiles;
  166. }
  167. private Set<String> filterIgnorePaths(Set<String> inputPaths,
  168. Set<String> ignoredNotInIndex, boolean exact) {
  169. if (ignore) {
  170. Set<String> filtered = new TreeSet<>(inputPaths);
  171. for (String path : inputPaths)
  172. for (String ignored : ignoredNotInIndex)
  173. if ((exact && path.equals(ignored))
  174. || (!exact && path.startsWith(ignored))) {
  175. filtered.remove(path);
  176. break;
  177. }
  178. return filtered;
  179. }
  180. return inputPaths;
  181. }
  182. private Set<String> filterFolders(Set<String> untracked,
  183. Set<String> untrackedFolders) {
  184. Set<String> filtered = new TreeSet<>(untracked);
  185. for (String file : untracked)
  186. for (String folder : untrackedFolders)
  187. if (file.startsWith(folder)) {
  188. filtered.remove(file);
  189. break;
  190. }
  191. return filtered;
  192. }
  193. /**
  194. * If paths are set, only these paths are affected by the cleaning.
  195. *
  196. * @param paths
  197. * the paths to set (with <code>/</code> as separator)
  198. * @return {@code this}
  199. */
  200. public CleanCommand setPaths(Set<String> paths) {
  201. this.paths = paths;
  202. return this;
  203. }
  204. /**
  205. * If dryRun is set, the paths in question will not actually be deleted.
  206. *
  207. * @param dryRun
  208. * whether to do a dry run or not
  209. * @return {@code this}
  210. */
  211. public CleanCommand setDryRun(boolean dryRun) {
  212. this.dryRun = dryRun;
  213. return this;
  214. }
  215. /**
  216. * If force is set, directories that are git repositories will also be
  217. * deleted.
  218. *
  219. * @param force
  220. * whether or not to delete git repositories
  221. * @return {@code this}
  222. * @since 4.5
  223. */
  224. public CleanCommand setForce(boolean force) {
  225. this.force = force;
  226. return this;
  227. }
  228. /**
  229. * If dirs is set, in addition to files, also clean directories.
  230. *
  231. * @param dirs
  232. * whether to clean directories too, or only files.
  233. * @return {@code this}
  234. */
  235. public CleanCommand setCleanDirectories(boolean dirs) {
  236. directories = dirs;
  237. return this;
  238. }
  239. /**
  240. * If ignore is set, don't report/clean files/directories that are ignored
  241. * by a .gitignore. otherwise do handle them.
  242. *
  243. * @param ignore
  244. * whether to respect .gitignore or not.
  245. * @return {@code this}
  246. */
  247. public CleanCommand setIgnore(boolean ignore) {
  248. this.ignore = ignore;
  249. return this;
  250. }
  251. }