1 /*
2 * Copyright (C) 2012, Matthias Sohn <matthias.sohn@sap.com>
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 * A class used to execute a {@code gc} command. It has setters for all
67 * supported options and arguments of this command and a {@link #call()} method
68 * to finally execute the command. Each instance of this class should only be
69 * used for one invocation of the command (means: one call to {@link #call()})
70 *
71 * @since 2.2
72 * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-gc.html"
73 * >Git documentation about gc</a>
74 */
75 public class GarbageCollectCommand extends GitCommand<Properties> {
76 /**
77 * Default value of maximum delta chain depth during aggressive garbage
78 * collection: {@value}
79 *
80 * @since 3.6
81 */
82 public static final int DEFAULT_GC_AGGRESSIVE_DEPTH = 250;
83
84 /**
85 * Default window size during packing during aggressive garbage collection:
86 * * {@value}
87 *
88 * @since 3.6
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 * Constructor for GarbageCollectCommand.
100 *
101 * @param repo
102 * a {@link org.eclipse.jgit.lib.Repository} object.
103 */
104 protected GarbageCollectCommand(Repository repo) {
105 super(repo);
106 pconfig = new PackConfig(repo);
107 }
108
109 /**
110 * Set progress monitor
111 *
112 * @param monitor
113 * a progress monitor
114 * @return this instance
115 */
116 public GarbageCollectCommand setProgressMonitor(ProgressMonitor monitor) {
117 this.monitor = monitor;
118 return this;
119 }
120
121 /**
122 * During gc() or prune() each unreferenced, loose object which has been
123 * created or modified after <code>expire</code> will not be pruned. Only
124 * older objects may be pruned. If set to null then every object is a
125 * candidate for pruning. Use {@link org.eclipse.jgit.util.GitDateParser} to
126 * parse time formats used by git gc.
127 *
128 * @param expire
129 * minimal age of objects to be pruned.
130 * @return this instance
131 */
132 public GarbageCollectCommand setExpire(Date expire) {
133 this.expire = expire;
134 return this;
135 }
136
137 /**
138 * Whether to use aggressive mode or not. If set to true JGit behaves more
139 * similar to native git's "git gc --aggressive". If set to
140 * <code>true</code> compressed objects found in old packs are not reused
141 * but every object is compressed again. Configuration variables
142 * pack.window and pack.depth are set to 250 for this GC.
143 *
144 * @since 3.6
145 * @param aggressive
146 * whether to turn on or off aggressive mode
147 * @return this instance
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 * Whether to preserve old pack files instead of deleting them.
168 *
169 * @since 4.7
170 * @param preserveOldPacks
171 * whether to preserve old pack files
172 * @return this instance
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 * Whether to prune preserved pack files in the preserved directory.
184 *
185 * @since 4.7
186 * @param prunePreserved
187 * whether to prune preserved pack files
188 * @return this instance
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 /** {@inheritDoc} */
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 * Computes and returns the repository statistics.
235 *
236 * @return the repository statistics
237 * @throws org.eclipse.jgit.api.errors.GitAPIException
238 * thrown if the repository statistics cannot be computed
239 * @since 3.0
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); //$NON-NLS-1$
259 p.put("numberOfLooseRefs", stats.numberOfLooseRefs); //$NON-NLS-1$
260 p.put("numberOfPackedObjects", stats.numberOfPackedObjects); //$NON-NLS-1$
261 p.put("numberOfPackedRefs", stats.numberOfPackedRefs); //$NON-NLS-1$
262 p.put("numberOfPackFiles", stats.numberOfPackFiles); //$NON-NLS-1$
263 p.put("sizeOfLooseObjects", stats.sizeOfLooseObjects); //$NON-NLS-1$
264 p.put("sizeOfPackedObjects", stats.sizeOfPackedObjects); //$NON-NLS-1$
265 return p;
266 }
267 }