James Moger
2014-03-26 617909819cd1b955647dd8584036fc7b2a014265
commit | author | age
f6109e 1 /*
DO 2  * Copyright 2014 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.gitblit.transport.ssh.git;
17
18 import java.io.IOException;
19 import java.util.Properties;
20
21 import org.eclipse.jgit.api.GarbageCollectCommand;
22 import org.eclipse.jgit.api.Git;
23 import org.eclipse.jgit.lib.NullProgressMonitor;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.gitblit.transport.ssh.commands.CommandMetaData;
617909 28 import com.gitblit.transport.ssh.commands.UsageExample;
f6109e 29
DO 30 @CommandMetaData(name = "gc", description = "Cleanup unnecessary files and optimize the local repository", admin = true)
617909 31 @UsageExample(syntax = "${cmd} test/myrepository.git", description = "Garbage collect \"test/myrepository.git\"")
f6109e 32 public class GarbageCollectionCommand extends BaseGitCommand {
DO 33
34     private static final Logger log = LoggerFactory.getLogger(GarbageCollectionCommand.class);
35
36     @Override
37     protected void runImpl() throws IOException, Failure {
38         try {
39             GarbageCollectCommand gc = Git.wrap(repo).gc();
40             logGcInfo("before:", gc.getStatistics());
41             gc.setProgressMonitor(NullProgressMonitor.INSTANCE);
42             Properties statistics = gc.call();
43             logGcInfo("after: ", statistics);
44         } catch (Exception e) {
45             throw new Failure(1, "fatal: Cannot run gc: ", e);
46         }
47     }
48
49     private static void logGcInfo(String msg,
50             Properties statistics) {
51         StringBuilder b = new StringBuilder();
52         b.append(msg);
53         if (statistics != null) {
54             b.append(" ");
55             String s = statistics.toString();
56             if (s.startsWith("{") && s.endsWith("}")) {
57                 s = s.substring(1, s.length() - 1);
58             }
59             b.append(s);
60         }
61         log.info(b.toString());
62     }
63 }