-
Notifications
You must be signed in to change notification settings - Fork 245
/
DevicesGit.java
118 lines (108 loc) · 3.89 KB
/
DevicesGit.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package org.system;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.NoHeadException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig.Host;
import org.eclipse.jgit.transport.SshSessionFactory;
import com.jcraft.jsch.Session;
public class DevicesGit {
private static String remotePath="https://github.com/Androxyde/devices.git";
private static String localPath=OS.getFolderDevices()+File.separator+".git";
private static Repository localRepo;
private static Git git;
static final Logger logger = LogManager.getLogger(DevicesGit.class);
public static void gitSync() throws IOException, InvalidRemoteException, org.eclipse.jgit.api.errors.TransportException, GitAPIException {
SshSessionFactory.setInstance(new JschConfigSessionFactory() {
public void configure(Host hc, Session session) {
session.setConfig("StrictHostKeyChecking", "no");
};
}
);
if (openRepository()) {
pullRepository();
}
else cloneRepository();
closeRepository();
}
public static void cloneRepository() {
try {
logger.info("Cloning devices repository to "+OS.getFolderDevices());
File lPath = new File(OS.getFolderDevices());
FileUtils.deleteDirectory(lPath);
lPath.mkdir();
Git result = Git.cloneRepository()
.setURI(remotePath)
.setDirectory(lPath)
.call();
result.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean openRepository() {
try {
logger.info("Opening devices repository.");
FileRepositoryBuilder builder = new FileRepositoryBuilder();
localRepo = builder.setGitDir(new File(localPath))
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
git = new Git(localRepo);
return true;
} catch (Exception e) {
e.printStackTrace();
logger.info("Error opening devices repository.");
closeRepository();
return false;
}
}
public static void pullRepository() {
try {
logger.info("Scanning devices folder for changes.");
git.add().addFilepattern(".").call();
Status status = git.status().call();
if (status.getChanged().size()>0 || status.getAdded().size()>0 || status.getModified().size()>0) {
logger.info("Changes have been found. Doing a hard reset (removing user modifications).");
ResetCommand reset = git.reset();
reset.setMode(ResetType.HARD);
reset.setRef(Constants.HEAD);
reset.call();
}
logger.info("Pulling changes from github.");
git.pull().call();
} catch (NoHeadException e) {
logger.info("Pull failed. Trying to clone repository instead");
closeRepository();
cloneRepository();
}
catch (Exception e1) {
closeRepository();
}
}
public static void closeRepository() {
logger.info("Quietly closing devices repository.");
try {
git.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
localRepo.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}