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