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