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 @Override
163 public Properties call() throws GitAPIException {
164 checkCallable();
165
166 try {
167 if (repo instanceof FileRepository) {
168 GC gc = new GC((FileRepository) repo);
169 gc.setPackConfig(pconfig);
170 gc.setProgressMonitor(monitor);
171 if (this.expire != null)
172 gc.setExpire(expire);
173
174 try {
175 gc.gc();
176 return toProperties(gc.getStatistics());
177 } catch (ParseException e) {
178 throw new JGitInternalException(JGitText.get().gcFailed, e);
179 }
180 } else if (repo instanceof DfsRepository) {
181 DfsGarbageCollector gc =
182 new DfsGarbageCollector((DfsRepository) repo);
183 gc.setPackConfig(pconfig);
184 gc.pack(monitor);
185 return new Properties();
186 } else {
187 throw new UnsupportedOperationException(MessageFormat.format(
188 JGitText.get().unsupportedGC,
189 repo.getClass().toString()));
190 }
191 } catch (IOException e) {
192 throw new JGitInternalException(JGitText.get().gcFailed, e);
193 }
194 }
195
196
197
198
199
200
201
202
203
204 public Properties getStatistics() throws GitAPIException {
205 try {
206 if (repo instanceof FileRepository) {
207 GC gc = new GC((FileRepository) repo);
208 return toProperties(gc.getStatistics());
209 } else {
210 return new Properties();
211 }
212 } catch (IOException e) {
213 throw new JGitInternalException(
214 JGitText.get().couldNotGetRepoStatistics, e);
215 }
216 }
217
218 @SuppressWarnings("boxing")
219 private static Properties toProperties(RepoStatistics stats) {
220 Properties p = new Properties();
221 p.put("numberOfLooseObjects", stats.numberOfLooseObjects);
222 p.put("numberOfLooseRefs", stats.numberOfLooseRefs);
223 p.put("numberOfPackedObjects", stats.numberOfPackedObjects);
224 p.put("numberOfPackedRefs", stats.numberOfPackedRefs);
225 p.put("numberOfPackFiles", stats.numberOfPackFiles);
226 p.put("sizeOfLooseObjects", stats.sizeOfLooseObjects);
227 p.put("sizeOfPackedObjects", stats.sizeOfPackedObjects);
228 return p;
229 }
230 }