This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Worker.java
69 lines (57 loc) · 2.19 KB
/
Worker.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package net.berla.aws;
import net.berla.aws.environment.LambdaConfig;
import net.berla.aws.git.SyncableRepository;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.URIish;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Callable;
/**
* Worker to update a S3 repository with the Github repository.
*
* @author Matthias Berla (matthias@berla.net)
* @version $Revision$ $Date$
*/
class Worker implements Callable<Status> {
private static final Logger LOG = LoggerFactory.getLogger(Worker.class);
private final Config config;
private final URIish fetchUrl;
private final SyncableRepository pushRepository;
Worker(Config config) {
this.config = config;
this.fetchUrl = config.getFetchUrl();
this.pushRepository = config.getPushRepository();
}
public static void main(String[] args) throws Exception {
new Worker(LambdaConfig.get()).call();
}
@Override
public Status call() throws Exception {
LOG.info("Starting repository synchronization");
try (Git git = getWorkingFileRepository()) {
// Set fetch repo
config.configure(git.remoteSetUrl(), fetchUrl, false).call();
config.configure(git.remoteSetUrl(), pushRepository, true).call();
// Fetch repo
config.configure(git.fetch()).setCheckFetchedObjects(true).call();
// Push repo
config.configure(git.push()).setForce(true).call();
// Sync working tree
return pushRepository.call();
}
finally {
LOG.info("Finished repository synchronization");
// Close repository and free resources
config.getWorkingFileRepository().close();
}
}
private Git getWorkingFileRepository() throws GitAPIException {
// Check for existing repo
if (config.getWorkingFileRepository().getObjectDatabase().exists()) {
return Git.wrap(config.getWorkingFileRepository());
}
// Clone new repo
return config.configure(pushRepository.clone(Git.cloneRepository())).setBare(true).call();
}
}