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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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) || isFileInPaths(file) ) {
  112. files = cleanPath(file, files);
  113. }
  114. for (String dir : notIgnoredDirs)
  115. if (paths.isEmpty() || paths.contains(dir) || isFileInPaths(dir) ) {
  116. files = cleanPath(dir, files);
  117. }
  118. } catch (IOException e) {
  119. throw new JGitInternalException(e.getMessage(), e);
  120. }
  121. return files;
  122. }
  123. protected boolean isFileInPaths(final String file) {
  124. if(paths.isEmpty()){
  125. return false;
  126. }
  127. String path=file;
  128. final Set<String> dirs = new HashSet<String>();
  129. while(true){
  130. int idx = path.lastIndexOf("/");
  131. if( idx> 0 ){
  132. path=file.substring(0,idx);
  133. if(paths.contains(path)){
  134. paths.addAll(dirs);
  135. return true;
  136. }
  137. dirs.add(path);
  138. }else{
  139. break;
  140. }
  141. return idx == -1 ? false : ;
  142. }
  143. return false;
  144. }
  145. /**
  146. * When dryRun is false, deletes the specified path from disk. If dryRun
  147. * is true, no paths are actually deleted. In both cases, the paths that
  148. * would have been deleted are added to inFiles and returned.
  149. *
  150. * Paths that are directories are recursively deleted when
  151. * {@link #directories} is true.
  152. * Paths that are git repositories are recursively deleted when
  153. * {@link #directories} and {@link #force} are both true.
  154. *
  155. * @param path
  156. * The path to be cleaned
  157. * @param inFiles
  158. * A set of strings representing the files that have been cleaned
  159. * already, the path to be cleaned will be added to this set
  160. * before being returned.
  161. *
  162. * @return a set of strings with the cleaned path added to it
  163. * @throws IOException
  164. */
  165. private Set<String> cleanPath(String path, Set<String> inFiles)
  166. throws IOException {
  167. File curFile = new File(repo.getWorkTree(), path);
  168. if (curFile.isDirectory()) {
  169. if (directories) {
  170. // Is this directory a git repository?
  171. if (new File(curFile, DOT_GIT).exists()) {
  172. if (force) {
  173. if (!dryRun) {
  174. FileUtils.delete(curFile, FileUtils.RECURSIVE);
  175. }
  176. inFiles.add(path + "/"); //$NON-NLS-1$
  177. }
  178. } else {
  179. if (!dryRun) {
  180. FileUtils.delete(curFile, FileUtils.RECURSIVE);
  181. }
  182. inFiles.add(path + "/"); //$NON-NLS-1$
  183. }
  184. }
  185. } else {
  186. if (!dryRun) {
  187. FileUtils.delete(curFile, FileUtils.NONE);
  188. }
  189. inFiles.add(path);
  190. }
  191. return inFiles;
  192. }
  193. private Set<String> filterIgnorePaths(Set<String> inputPaths,
  194. Set<String> ignoredNotInIndex, boolean exact) {
  195. if (ignore) {
  196. Set<String> filtered = new TreeSet<String>(inputPaths);
  197. for (String path : inputPaths)
  198. for (String ignored : ignoredNotInIndex)
  199. if ((exact && path.equals(ignored))
  200. || (!exact && path.startsWith(ignored))) {
  201. filtered.remove(path);
  202. break;
  203. }
  204. return filtered;
  205. }
  206. return inputPaths;
  207. }
  208. private Set<String> filterFolders(Set<String> untracked,
  209. Set<String> untrackedFolders) {
  210. Set<String> filtered = new TreeSet<String>(untracked);
  211. for (String file : untracked)
  212. for (String folder : untrackedFolders)
  213. if (file.startsWith(folder)) {
  214. filtered.remove(file);
  215. break;
  216. }
  217. return filtered;
  218. }
  219. /**
  220. * If paths are set, only these paths are affected by the cleaning.
  221. *
  222. * @param paths
  223. * the paths to set (with <code>/</code> as separator)
  224. * @return {@code this}
  225. */
  226. public CleanCommand setPaths(Set<String> paths) {
  227. this.paths = paths;
  228. return this;
  229. }
  230. /**
  231. * If dryRun is set, the paths in question will not actually be deleted.
  232. *
  233. * @param dryRun
  234. * whether to do a dry run or not
  235. * @return {@code this}
  236. */
  237. public CleanCommand setDryRun(boolean dryRun) {
  238. this.dryRun = dryRun;
  239. return this;
  240. }
  241. /**
  242. * If force is set, directories that are git repositories will also be
  243. * deleted.
  244. *
  245. * @param force
  246. * whether or not to delete git repositories
  247. * @return {@code this}
  248. * @since 4.5
  249. */
  250. public CleanCommand setForce(boolean force) {
  251. this.force = force;
  252. return this;
  253. }
  254. /**
  255. * If dirs is set, in addition to files, also clean directories.
  256. *
  257. * @param dirs
  258. * whether to clean directories too, or only files.
  259. * @return {@code this}
  260. */
  261. public CleanCommand setCleanDirectories(boolean dirs) {
  262. directories = dirs;
  263. return this;
  264. }
  265. /**
  266. * If ignore is set, don't report/clean files/directories that are ignored
  267. * by a .gitignore. otherwise do handle them.
  268. *
  269. * @param ignore
  270. * whether to respect .gitignore or not.
  271. * @return {@code this}
  272. */
  273. public CleanCommand setIgnore(boolean ignore) {
  274. this.ignore = ignore;
  275. return this;
  276. }
  277. }