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 package org.eclipse.jgit.api;
44
45 import java.io.IOException;
46 import java.text.MessageFormat;
47 import java.text.ParseException;
48 import java.util.Date;
49 import java.util.Properties;
50
51 import org.eclipse.jgit.api.errors.GitAPIException;
52 import org.eclipse.jgit.api.errors.JGitInternalException;
53 import org.eclipse.jgit.internal.JGitText;
54 import org.eclipse.jgit.internal.storage.dfs.DfsGarbageCollector;
55 import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
56 import org.eclipse.jgit.internal.storage.file.FileRepository;
57 import org.eclipse.jgit.internal.storage.file.GC;
58 import org.eclipse.jgit.internal.storage.file.GC.RepoStatistics;
59 import org.eclipse.jgit.lib.ConfigConstants;
60 import org.eclipse.jgit.lib.ProgressMonitor;
61 import org.eclipse.jgit.lib.Repository;
62 import org.eclipse.jgit.lib.StoredConfig;
63 import org.eclipse.jgit.storage.pack.PackConfig;
64 import org.eclipse.jgit.util.GitDateParser;
65
66
67
68
69
70
71
72
73
74
75
76 public class GarbageCollectCommand extends GitCommand<Properties> {
77
78
79
80
81
82
83 public static final int DEFAULT_GC_AGGRESSIVE_DEPTH = 250;
84
85
86
87
88
89
90
91 public static final int DEFAULT_GC_AGGRESSIVE_WINDOW = 250;
92
93 private ProgressMonitor monitor;
94
95 private Date expire;
96
97 private PackConfig pconfig;
98
99
100
101
102 protected GarbageCollectCommand(Repository repo) {
103 super(repo);
104 pconfig = new PackConfig(repo);
105 }
106
107
108
109
110
111
112 public GarbageCollectCommand setProgressMonitor(ProgressMonitor monitor) {
113 this.monitor = monitor;
114 return this;
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128 public GarbageCollectCommand setExpire(Date expire) {
129 this.expire = expire;
130 return this;
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public GarbageCollectCommand setAggressive(boolean aggressive) {
146 if (aggressive) {
147 StoredConfig repoConfig = repo.getConfig();
148 pconfig.setDeltaSearchWindowSize(repoConfig.getInt(
149 ConfigConstants.CONFIG_GC_SECTION,
150 ConfigConstants.CONFIG_KEY_AGGRESSIVE_WINDOW,
151 DEFAULT_GC_AGGRESSIVE_WINDOW));
152 pconfig.setMaxDeltaDepth(repoConfig.getInt(
153 ConfigConstants.CONFIG_GC_SECTION,
154 ConfigConstants.CONFIG_KEY_AGGRESSIVE_DEPTH,
155 DEFAULT_GC_AGGRESSIVE_DEPTH));
156 pconfig.setReuseObjects(false);
157 } else
158 pconfig = new PackConfig(repo);
159 return this;
160 }
161
162
163
164
165
166
167
168
169
170 public GarbageCollectCommand setPreserveOldPacks(boolean preserveOldPacks) {
171 if (pconfig == null)
172 pconfig = new PackConfig(repo);
173
174 pconfig.setPreserveOldPacks(preserveOldPacks);
175 return this;
176 }
177
178
179
180
181
182
183
184
185
186 public GarbageCollectCommand setPrunePreserved(boolean prunePreserved) {
187 if (pconfig == null)
188 pconfig = new PackConfig(repo);
189
190 pconfig.setPrunePreserved(prunePreserved);
191 return this;
192 }
193
194 @Override
195 public Properties call() throws GitAPIException {
196 checkCallable();
197
198 try {
199 if (repo instanceof FileRepository) {
200 GC gc = new GC((FileRepository) repo);
201 gc.setPackConfig(pconfig);
202 gc.setProgressMonitor(monitor);
203 if (this.expire != null)
204 gc.setExpire(expire);
205
206 try {
207 gc.gc();
208 return toProperties(gc.getStatistics());
209 } catch (ParseException e) {
210 throw new JGitInternalException(JGitText.get().gcFailed, e);
211 }
212 } else if (repo instanceof DfsRepository) {
213 DfsGarbageCollector gc =
214 new DfsGarbageCollector((DfsRepository) repo);
215 gc.setPackConfig(pconfig);
216 gc.pack(monitor);
217 return new Properties();
218 } else {
219 throw new UnsupportedOperationException(MessageFormat.format(
220 JGitText.get().unsupportedGC,
221 repo.getClass().toString()));
222 }
223 } catch (IOException e) {
224 throw new JGitInternalException(JGitText.get().gcFailed, e);
225 }
226 }
227
228
229
230
231
232
233
234
235
236 public Properties getStatistics() throws GitAPIException {
237 try {
238 if (repo instanceof FileRepository) {
239 GC gc = new GC((FileRepository) repo);
240 return toProperties(gc.getStatistics());
241 } else {
242 return new Properties();
243 }
244 } catch (IOException e) {
245 throw new JGitInternalException(
246 JGitText.get().couldNotGetRepoStatistics, e);
247 }
248 }
249
250 @SuppressWarnings("boxing")
251 private static Properties toProperties(RepoStatistics stats) {
252 Properties p = new Properties();
253 p.put("numberOfLooseObjects", stats.numberOfLooseObjects);
254 p.put("numberOfLooseRefs", stats.numberOfLooseRefs);
255 p.put("numberOfPackedObjects", stats.numberOfPackedObjects);
256 p.put("numberOfPackedRefs", stats.numberOfPackedRefs);
257 p.put("numberOfPackFiles", stats.numberOfPackFiles);
258 p.put("sizeOfLooseObjects", stats.sizeOfLooseObjects);
259 p.put("sizeOfPackedObjects", stats.sizeOfPackedObjects);
260 return p;
261 }
262 }