James Moger
2011-05-26 2a7306a1d92522569a8bb6e5a7c0bcdd5cf4cfaa
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.gitblit.tests;
 
import java.io.File;
 
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
 
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepository;
 
public class GitBlitSuite extends TestSetup {
    public static final File REPOSITORIES = new File("git");
 
    private GitBlitSuite(TestSuite suite) {
        super(suite);
    }
 
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTestSuite(JGitUtilsTest.class);
        return new GitBlitSuite(suite);
    }
    
    public static Repository getHelloworldRepository() throws Exception {
        return new FileRepository(new File(REPOSITORIES, "helloworld.git"));
    }
 
    public static Repository getTicgitRepository() throws Exception {
        return new FileRepository(new File(REPOSITORIES, "ticgit.git"));
    }
 
    @Override
    protected void setUp() throws Exception {
        if (REPOSITORIES.exists() || REPOSITORIES.mkdirs()) {
            cloneOrFetch("helloworld.git", "https://github.com/git/hello-world.git", true);
            cloneOrFetch("nested/helloworld.git", "https://github.com/git/hello-world.git", true);
            cloneOrFetch("ticgit.git", "https://github.com/jeffWelling/ticgit.git", true);
        }
    }
 
    private void cloneOrFetch(String toFolder, String fromUrl, boolean bare) throws Exception {
        File folder = new File(REPOSITORIES, toFolder + (bare ? "" : "/.git"));
        if (folder.exists()) {
            System.out.print("Updating " + (bare ? "bare " : " ") + toFolder + "... ");
            FileRepository repository = new FileRepository(new File(REPOSITORIES, toFolder));
            Git git = new Git(repository);
            git.fetch().call();
            repository.close();
            System.out.println("done.");
        } else {
            System.out.println("Cloning " + (bare ? "bare " : " ") + toFolder + "... ");
            CloneCommand clone = new CloneCommand();
            clone.setBare(bare);
            clone.setCloneAllBranches(true);
            clone.setURI(fromUrl);
            clone.setDirectory(folder);
            clone.call();
            System.out.println("done.");
        }
    }
}