-
Notifications
You must be signed in to change notification settings - Fork 27
/
GitRule.java
48 lines (38 loc) · 1.2 KB
/
GitRule.java
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
package book.twju.timeline.test.util;
import static book.twju.timeline.test.util.FileHelper.delete;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.InitCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.junit.rules.ExternalResource;
public class GitRule extends ExternalResource {
private final Set<File> repositories;
public GitRule() {
repositories = new HashSet<>();
}
@Override
protected void after() {
repositories.forEach( repository -> delete( repository ) );
}
public GitRepository create( File location ) {
createRepositoryOnDisk( location );
GitRepository result = new GitRepository( location );
repositories.add( location );
return result;
}
private void createRepositoryOnDisk( File location ) {
InitCommand init = Git.init();
init.setDirectory( location );
init.setBare( false );
callInit( init );
}
private static void callInit( InitCommand init ) {
try {
init.call().close();
} catch( GitAPIException exception ) {
throw new GitOperationException( exception );
}
}
}