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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 java.io.File;
  46. import java.io.IOException;
  47. import java.util.Collections;
  48. import java.util.Set;
  49. import java.util.TreeSet;
  50. import org.eclipse.jgit.api.errors.GitAPIException;
  51. import org.eclipse.jgit.api.errors.JGitInternalException;
  52. import org.eclipse.jgit.errors.NoWorkTreeException;
  53. import org.eclipse.jgit.lib.Repository;
  54. import org.eclipse.jgit.util.FS;
  55. import org.eclipse.jgit.util.FileUtils;
  56. /**
  57. * Remove untracked files from the working tree
  58. *
  59. * @see <a
  60. * href="http://www.kernel.org/pub/software/scm/git/docs/git-clean.html"
  61. * >Git documentation about Clean</a>
  62. */
  63. public class CleanCommand extends GitCommand<Set<String>> {
  64. private Set<String> paths = Collections.emptySet();
  65. private boolean dryRun;
  66. private boolean directories;
  67. private boolean ignore = true;
  68. /**
  69. * @param repo
  70. */
  71. protected CleanCommand(Repository repo) {
  72. super(repo);
  73. }
  74. /**
  75. * Executes the {@code clean} command with all the options and parameters
  76. * collected by the setter methods of this class. Each instance of this
  77. * class should only be used for one invocation of the command (means: one
  78. * call to {@link #call()})
  79. *
  80. * @return a set of strings representing each file cleaned.
  81. * @throws GitAPIException
  82. * @throws NoWorkTreeException
  83. */
  84. public Set<String> call() throws NoWorkTreeException, GitAPIException {
  85. Set<String> files = new TreeSet<String>();
  86. try {
  87. StatusCommand command = new StatusCommand(repo);
  88. Status status = command.call();
  89. Set<String> untrackedAndIgnoredFiles = new TreeSet<String>(
  90. status.getUntracked());
  91. Set<String> untrackedAndIgnoredDirs = new TreeSet<String>(
  92. status.getUntrackedFolders());
  93. FS fs = getRepository().getFS();
  94. for (String p : status.getIgnoredNotInIndex()) {
  95. File f = new File(repo.getWorkTree(), p);
  96. if (fs.isFile(f) || fs.isSymLink(f))
  97. untrackedAndIgnoredFiles.add(p);
  98. else if (fs.isDirectory(f))
  99. untrackedAndIgnoredDirs.add(p);
  100. }
  101. Set<String> filtered = filterFolders(untrackedAndIgnoredFiles,
  102. untrackedAndIgnoredDirs);
  103. Set<String> notIgnoredFiles = filterIgnorePaths(filtered,
  104. status.getIgnoredNotInIndex(), true);
  105. Set<String> notIgnoredDirs = filterIgnorePaths(
  106. untrackedAndIgnoredDirs,
  107. status.getIgnoredNotInIndex(), false);
  108. for (String file : notIgnoredFiles)
  109. if (paths.isEmpty() || paths.contains(file)) {
  110. if (!dryRun)
  111. FileUtils.delete(new File(repo.getWorkTree(), file));
  112. files.add(file);
  113. }
  114. if (directories)
  115. for (String dir : notIgnoredDirs)
  116. if (paths.isEmpty() || paths.contains(dir)) {
  117. if (!dryRun)
  118. FileUtils.delete(new File(repo.getWorkTree(), dir),
  119. FileUtils.RECURSIVE);
  120. files.add(dir + "/"); //$NON-NLS-1$
  121. }
  122. } catch (IOException e) {
  123. throw new JGitInternalException(e.getMessage(), e);
  124. }
  125. return files;
  126. }
  127. private Set<String> filterIgnorePaths(Set<String> inputPaths,
  128. Set<String> ignoredNotInIndex, boolean exact) {
  129. if (ignore) {
  130. Set<String> filtered = new TreeSet<String>(inputPaths);
  131. for (String path : inputPaths)
  132. for (String ignored : ignoredNotInIndex)
  133. if ((exact && path.equals(ignored))
  134. || (!exact && path.startsWith(ignored))) {
  135. filtered.remove(path);
  136. break;
  137. }
  138. return filtered;
  139. }
  140. return inputPaths;
  141. }
  142. private Set<String> filterFolders(Set<String> untracked,
  143. Set<String> untrackedFolders) {
  144. Set<String> filtered = new TreeSet<String>(untracked);
  145. for (String file : untracked)
  146. for (String folder : untrackedFolders)
  147. if (file.startsWith(folder)) {
  148. filtered.remove(file);
  149. break;
  150. }
  151. return filtered;
  152. }
  153. /**
  154. * If paths are set, only these paths are affected by the cleaning.
  155. *
  156. * @param paths
  157. * the paths to set (with <code>/</code> as separator)
  158. * @return {@code this}
  159. */
  160. public CleanCommand setPaths(Set<String> paths) {
  161. this.paths = paths;
  162. return this;
  163. }
  164. /**
  165. * If dryRun is set, the paths in question will not actually be deleted.
  166. *
  167. * @param dryRun
  168. * whether to do a dry run or not
  169. * @return {@code this}
  170. */
  171. public CleanCommand setDryRun(boolean dryRun) {
  172. this.dryRun = dryRun;
  173. return this;
  174. }
  175. /**
  176. * If dirs is set, in addition to files, also clean directories.
  177. *
  178. * @param dirs
  179. * whether to clean directories too, or only files.
  180. * @return {@code this}
  181. */
  182. public CleanCommand setCleanDirectories(boolean dirs) {
  183. directories = dirs;
  184. return this;
  185. }
  186. /**
  187. * If ignore is set, don't report/clean files/directories that are ignored
  188. * by a .gitignore. otherwise do handle them.
  189. *
  190. * @param ignore
  191. * whether to respect .gitignore or not.
  192. * @return {@code this}
  193. */
  194. public CleanCommand setIgnore(boolean ignore) {
  195. this.ignore = ignore;
  196. return this;
  197. }
  198. }