1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.api;
45
46 import static org.eclipse.jgit.lib.Constants.DOT_GIT;
47
48 import java.io.File;
49 import java.io.IOException;
50 import java.util.Collections;
51 import java.util.Set;
52 import java.util.TreeSet;
53
54 import org.eclipse.jgit.api.errors.GitAPIException;
55 import org.eclipse.jgit.api.errors.JGitInternalException;
56 import org.eclipse.jgit.errors.NoWorkTreeException;
57 import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
58 import org.eclipse.jgit.lib.Repository;
59 import org.eclipse.jgit.util.FS;
60 import org.eclipse.jgit.util.FileUtils;
61
62
63
64
65
66
67
68
69 public class CleanCommand extends GitCommand<Set<String>> {
70
71 private Set<String> paths = Collections.emptySet();
72
73 private boolean dryRun;
74
75 private boolean directories;
76
77 private boolean ignore = true;
78
79 private boolean force = false;
80
81
82
83
84 protected CleanCommand(Repository repo) {
85 super(repo);
86 }
87
88
89
90
91
92
93
94
95
96
97
98 @Override
99 public Set<String> call() throws NoWorkTreeException, GitAPIException {
100 Set<String> files = new TreeSet<>();
101 try {
102 StatusCommand command = new StatusCommand(repo);
103 Status status = command.call();
104
105 Set<String> untrackedAndIgnoredFiles = new TreeSet<>(
106 status.getUntracked());
107 Set<String> untrackedAndIgnoredDirs = new TreeSet<>(
108 status.getUntrackedFolders());
109
110 FS fs = getRepository().getFS();
111 for (String p : status.getIgnoredNotInIndex()) {
112 File f = new File(repo.getWorkTree(), p);
113 if (fs.isFile(f) || fs.isSymLink(f))
114 untrackedAndIgnoredFiles.add(p);
115 else if (fs.isDirectory(f))
116 untrackedAndIgnoredDirs.add(p);
117 }
118
119 Set<String> filtered = filterFolders(untrackedAndIgnoredFiles,
120 untrackedAndIgnoredDirs);
121
122 Set<String> notIgnoredFiles = filterIgnorePaths(filtered,
123 status.getIgnoredNotInIndex(), true);
124 Set<String> notIgnoredDirs = filterIgnorePaths(
125 untrackedAndIgnoredDirs,
126 status.getIgnoredNotInIndex(), false);
127
128 for (String file : notIgnoredFiles)
129 if (paths.isEmpty() || paths.contains(file)) {
130 files = cleanPath(file, files);
131 }
132
133 for (String dir : notIgnoredDirs)
134 if (paths.isEmpty() || paths.contains(dir)) {
135 files = cleanPath(dir, files);
136 }
137 } catch (IOException e) {
138 throw new JGitInternalException(e.getMessage(), e);
139 } finally {
140 if (!files.isEmpty()) {
141 repo.fireEvent(new WorkingTreeModifiedEvent(null, files));
142 }
143 }
144 return files;
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 private Set<String> cleanPath(String path, Set<String> inFiles)
168 throws IOException {
169 File curFile = new File(repo.getWorkTree(), path);
170 if (curFile.isDirectory()) {
171 if (directories) {
172
173 if (new File(curFile, DOT_GIT).exists()) {
174 if (force) {
175 if (!dryRun) {
176 FileUtils.delete(curFile, FileUtils.RECURSIVE);
177 }
178 inFiles.add(path + "/");
179 }
180 } else {
181 if (!dryRun) {
182 FileUtils.delete(curFile, FileUtils.RECURSIVE);
183 }
184 inFiles.add(path + "/");
185 }
186 }
187 } else {
188 if (!dryRun) {
189 FileUtils.delete(curFile, FileUtils.NONE);
190 }
191 inFiles.add(path);
192 }
193
194 return inFiles;
195 }
196
197 private Set<String> filterIgnorePaths(Set<String> inputPaths,
198 Set<String> ignoredNotInIndex, boolean exact) {
199 if (ignore) {
200 Set<String> filtered = new TreeSet<>(inputPaths);
201 for (String path : inputPaths)
202 for (String ignored : ignoredNotInIndex)
203 if ((exact && path.equals(ignored))
204 || (!exact && path.startsWith(ignored))) {
205 filtered.remove(path);
206 break;
207 }
208
209 return filtered;
210 }
211 return inputPaths;
212 }
213
214 private Set<String> filterFolders(Set<String> untracked,
215 Set<String> untrackedFolders) {
216 Set<String> filtered = new TreeSet<>(untracked);
217 for (String file : untracked)
218 for (String folder : untrackedFolders)
219 if (file.startsWith(folder)) {
220 filtered.remove(file);
221 break;
222 }
223
224
225 return filtered;
226 }
227
228
229
230
231
232
233
234
235 public CleanCommand setPaths(Set<String> paths) {
236 this.paths = paths;
237 return this;
238 }
239
240
241
242
243
244
245
246
247 public CleanCommand setDryRun(boolean dryRun) {
248 this.dryRun = dryRun;
249 return this;
250 }
251
252
253
254
255
256
257
258
259
260
261 public CleanCommand setForce(boolean force) {
262 this.force = force;
263 return this;
264 }
265
266
267
268
269
270
271
272
273 public CleanCommand setCleanDirectories(boolean dirs) {
274 directories = dirs;
275 return this;
276 }
277
278
279
280
281
282
283
284
285
286 public CleanCommand setIgnore(boolean ignore) {
287 this.ignore = ignore;
288 return this;
289 }
290 }