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 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (C) 2011, Chris Aniszczyk <zx@redhat.com>
  3. * Copyright (C) 2011, Abhishek Bhatnagar <abhatnag@redhat.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.api;
  45. import static org.eclipse.jgit.lib.Constants.DOT_GIT;
  46. import java.io.File;
  47. import java.io.IOException;
  48. import java.util.Collections;
  49. import java.util.Set;
  50. import java.util.TreeSet;
  51. import org.eclipse.jgit.api.errors.GitAPIException;
  52. import org.eclipse.jgit.api.errors.JGitInternalException;
  53. import org.eclipse.jgit.errors.NoWorkTreeException;
  54. import org.eclipse.jgit.lib.Repository;
  55. import org.eclipse.jgit.util.FS;
  56. import org.eclipse.jgit.util.FileUtils;
  57. /**
  58. * Remove untracked files from the working tree
  59. *
  60. * @see <a
  61. * href="http://www.kernel.org/pub/software/scm/git/docs/git-clean.html"
  62. * >Git documentation about Clean</a>
  63. */
  64. public class CleanCommand extends GitCommand<Set<String>> {
  65. private Set<String> paths = Collections.emptySet();
  66. private boolean dryRun;
  67. private boolean directories;
  68. private boolean ignore = true;
  69. private boolean force = false;
  70. /**
  71. * @param repo
  72. */
  73. protected CleanCommand(Repository repo) {
  74. super(repo);
  75. }
  76. /**
  77. * Executes the {@code clean} command with all the options and parameters
  78. * collected by the setter methods of this class. Each instance of this
  79. * class should only be used for one invocation of the command (means: one
  80. * call to {@link #call()})
  81. *
  82. * @return a set of strings representing each file cleaned.
  83. * @throws GitAPIException
  84. * @throws NoWorkTreeException
  85. */
  86. public Set<String> call() throws NoWorkTreeException, GitAPIException {
  87. Set<String> files = new TreeSet<String>();
  88. try {
  89. StatusCommand command = new StatusCommand(repo);
  90. Status status = command.call();
  91. Set<String> untrackedAndIgnoredFiles = new TreeSet<String>(
  92. status.getUntracked());
  93. Set<String> untrackedAndIgnoredDirs = new TreeSet<String>(
  94. status.getUntrackedFolders());
  95. FS fs = getRepository().getFS();
  96. for (String p : status.getIgnoredNotInIndex()) {
  97. File f = new File(repo.getWorkTree(), p);
  98. if (fs.isFile(f) || fs.isSymLink(f))
  99. untrackedAndIgnoredFiles.add(p);
  100. else if (fs.isDirectory(f))
  101. untrackedAndIgnoredDirs.add(p);
  102. }
  103. Set<String> filtered = filterFolders(untrackedAndIgnoredFiles,
  104. untrackedAndIgnoredDirs);
  105. Set<String> notIgnoredFiles = filterIgnorePaths(filtered,
  106. status.getIgnoredNotInIndex(), true);
  107. Set<String> notIgnoredDirs = filterIgnorePaths(
  108. untrackedAndIgnoredDirs,
  109. status.getIgnoredNotInIndex(), false);
  110. for (String file : notIgnoredFiles)
  111. if (paths.isEmpty() || paths.contains(file)) {
  112. files = cleanPath(file, files);
  113. }
  114. for (String dir : notIgnoredDirs)
  115. if (paths.isEmpty() || paths.contains(dir)) {
  116. files = cleanPath(dir, files);
  117. }
  118. } catch (IOException e) {
  119. throw new JGitInternalException(e.getMessage(), e);
  120. }
  121. return files;
  122. }
  123. /**
  124. * When dryRun is false, deletes the specified path from disk. If dryRun
  125. * is true, no paths are actually deleted. In both cases, the paths that
  126. * would have been deleted are added to inFiles and returned.
  127. *
  128. * Paths that are directories are recursively deleted when
  129. * {@link #directories} is true.
  130. * Paths that are git repositories are recursively deleted when
  131. * {@link #directories} and {@link #force} are both true.
  132. *
  133. * @param path
  134. * The path to be cleaned
  135. * @param inFiles
  136. * A set of strings representing the files that have been cleaned
  137. * already, the path to be cleaned will be added to this set
  138. * before being returned.
  139. *
  140. * @return a set of strings with the cleaned path added to it
  141. * @throws IOException
  142. */
  143. private Set<String> cleanPath(String path, Set<String> inFiles)
  144. throws IOException {
  145. File curFile = new File(repo.getWorkTree(), path);
  146. if (curFile.isDirectory()) {
  147. if (directories) {
  148. // Is this directory a git repository?
  149. if (new File(curFile, DOT_GIT).exists()) {
  150. if (force) {
  151. if (!dryRun) {
  152. FileUtils.delete(curFile, FileUtils.RECURSIVE);
  153. }
  154. inFiles.add(path + "/"); //$NON-NLS-1$
  155. }
  156. } else {
  157. if (!dryRun) {
  158. FileUtils.delete(curFile, FileUtils.RECURSIVE);
  159. }
  160. inFiles.add(path + "/"); //$NON-NLS-1$
  161. }
  162. }
  163. } else {
  164. if (!dryRun) {
  165. FileUtils.delete(curFile, FileUtils.NONE);
  166. }
  167. inFiles.add(path);
  168. }
  169. return inFiles;
  170. }
  171. private Set<String> filterIgnorePaths(Set<String> inputPaths,
  172. Set<String> ignoredNotInIndex, boolean exact) {
  173. if (ignore) {
  174. Set<String> filtered = new TreeSet<String>(inputPaths);
  175. for (String path : inputPaths)
  176. for (String ignored : ignoredNotInIndex)
  177. if ((exact && path.equals(ignored))
  178. || (!exact && path.startsWith(ignored))) {
  179. filtered.remove(path);
  180. break;
  181. }
  182. return filtered;
  183. }
  184. return inputPaths;
  185. }
  186. private Set<String> filterFolders(Set<String> untracked,
  187. Set<String> untrackedFolders) {
  188. Set<String> filtered = new TreeSet<String>(untracked);
  189. for (String file : untracked)
  190. for (String folder : untrackedFolders)
  191. if (file.startsWith(folder)) {
  192. filtered.remove(file);
  193. break;
  194. }
  195. return filtered;
  196. }
  197. /**
  198. * If paths are set, only these paths are affected by the cleaning.
  199. *
  200. * @param paths
  201. * the paths to set (with <code>/</code> as separator)
  202. * @return {@code this}
  203. */
  204. public CleanCommand setPaths(Set<String> paths) {
  205. this.paths = paths;
  206. return this;
  207. }
  208. /**
  209. * If dryRun is set, the paths in question will not actually be deleted.
  210. *
  211. * @param dryRun
  212. * whether to do a dry run or not
  213. * @return {@code this}
  214. */
  215. public CleanCommand setDryRun(boolean dryRun) {
  216. this.dryRun = dryRun;
  217. return this;
  218. }
  219. /**
  220. * If force is set, directories that are git repositories will also be
  221. * deleted.
  222. *
  223. * @param force
  224. * whether or not to delete git repositories
  225. * @return {@code this}
  226. * @since 4.5
  227. */
  228. public CleanCommand setForce(boolean force) {
  229. this.force = force;
  230. return this;
  231. }
  232. /**
  233. * If dirs is set, in addition to files, also clean directories.
  234. *
  235. * @param dirs
  236. * whether to clean directories too, or only files.
  237. * @return {@code this}
  238. */
  239. public CleanCommand setCleanDirectories(boolean dirs) {
  240. directories = dirs;
  241. return this;
  242. }
  243. /**
  244. * If ignore is set, don't report/clean files/directories that are ignored
  245. * by a .gitignore. otherwise do handle them.
  246. *
  247. * @param ignore
  248. * whether to respect .gitignore or not.
  249. * @return {@code this}
  250. */
  251. public CleanCommand setIgnore(boolean ignore) {
  252. this.ignore = ignore;
  253. return this;
  254. }
  255. }