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