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

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