import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.OpenSshConfig.Host;
import org.eclipse.jgit.util.FS;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class PlatSShSessionFactory extends JschConfigSessionFactory {
@Override
protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
JSch jsch = new JSch();
jsch.removeAllIdentity();
jsch.addIdentity(ConfigInfoUtil.getInstance().getGitIdentity());
jsch.setKnownHosts(ConfigInfoUtil.getInstance().getKnownHosts());
return jsch;
}
@Override
protected void configure(Host hc, Session session) {
}
}
import com.excloud.platform.util.ConfigInfoUtil;
import com.excloud.platform.util.PlatSShSessionFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PullCommand;
import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.StatusCommand;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.Transport;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@Slf4j
public class JGitUtil {
private static volatile JGitUtil instance;
private JGitUtil(){
}
public static JGitUtil getInstance(){
if (null == instance){
synchronized (JGitUtil.class){
if (null == instance){
instance = new JGitUtil();
instance.init();
}
}
}
return instance;
}
private static String gitConfig;
private void init(){
gitConfig = ConfigInfoUtil.getInstance().getGitConfig();
gitClone();
}
private static boolean gitCloneSuccess = false;
private void gitClone(){
try{
gitCloneSuccess = false;
if (ConfigInfoUtil.getInstance().isGitInit() == false) {
gitCloneSuccess = true;
return;
}
Thread.sleep(60 * 1000L);
File rootDir = new File(ConfigInfoUtil.getInstance().getGitConfigRootDir());
if (rootDir.exists()) {
FileUtils.deleteDirectory(rootDir);
}
rootDir = new File(ConfigInfoUtil.getInstance().getGitConfigRootDir());
if (!rootDir.exists()) {
CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setURI(ConfigInfoUtil.getInstance().getGitRemoteUrl());
cloneCommand.setBranch(ConfigInfoUtil.getInstance().getGitBranch());
cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(new PlatSShSessionFactory());
}
});
cloneCommand.setDirectory(rootDir).call();
// log.info("gitClone pull");
// git.pull().call();
// log.info("gitClone pull down");
gitCloneSuccess = true;
log.info("gitClone down");
}
} catch (Exception ex){
log.error("", ex);
}
}
public Git getGit() throws Exception {
while (true) {
if (gitCloneSuccess) {
if (StringUtils.isBlank(gitConfig)) {
gitConfig = "/home/pplive/webcdn_sn/.git";
}
Git git = Git.open(new File(gitConfig));
return git;
}
log.info("wait gitClone");
Thread.sleep(2000);
}
}
private static int pullCount = 0;
private AtomicBoolean pullable = new AtomicBoolean(true);
public void pull(Git git) throws Exception {
if (pullable.get()) {
pullable.set(false);
pullDetail(git);
}
}
public void pullDetail(Git git) throws Exception {
try {
PullCommand pull = git.pull().setTimeout(500);
pull.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(new PlatSShSessionFactory());
}
}).call();
} catch (Exception e) {
log.error("", e);
if (pullCount < 5) {
pullCount++;
Thread.sleep(2000L);
// faultTolerant(git);
pull(git);
} else {
gitClone();
}
throw e;
} finally {
pullable.set(true);
}
pullCount = 0;
}
public void faultTolerant(Git git) throws Exception {
FetchCommand fetchCommand = git.fetch();
fetchCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport)transport;
sshTransport.setSshSessionFactory(new PlatSShSessionFactory());
}
}).call();
git.reset().call();
}
private static int pushCount = 0;
private AtomicBoolean pushable = new AtomicBoolean(true);
public String batchPush(Git git, List filePatternLst, String message) throws Exception {
try{
if (CollectionUtils.isEmpty(filePatternLst)) {
return null;
}
if (pushable.get()) {
pushable.set(false);
String commitId = null;
for (int i = 0; i < 5; i++) {
commitId = batchPushDetail(git, filePatternLst, message);
if (StringUtils.isNotBlank(commitId)) {
break;
}
}
return commitId;
} else {
log.info("wait batchPush");
Thread.sleep(1000L);
return batchPush(git, filePatternLst, message);
}
} catch (Exception ex){
log.error("", ex);
throw ex;
}
}
private String batchPushDetail(Git git, List filePatternLst, String message) throws Exception {
try {
AddCommand addCommand = git.add();
for (String filePattern : filePatternLst) {
addCommand.addFilepattern(filePattern);
}
addCommand.call();
CommitCommand commit = git.commit();
commit.setAll(true);
RevCommit revCommit = commit.setMessage(message).call();
String commitId = revCommit.getId().name();
PushCommand push = git.push();
push.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(new PlatSShSessionFactory());
}
}).setTimeout(600).call();
pushCount = 0;
return commitId;
} catch (Exception e) {
log.error("", e);
if (pushCount < 5) {
pushCount++;
Thread.sleep(2000L);
return batchPushDetail(git, filePatternLst, message);
}
throw e;
} finally {
pushable.set(true);
}
}
public void clean(Git git) throws Exception {
File rootFile = new File(ConfigInfoUtil.getInstance().getGitConfigRootDir());
if (rootFile.exists()) {
StatusCommand statusCommand = git.status();
File[] files = rootFile.listFiles();
for (File file : files) {
if (!file.getName().equals(".git")) {
statusCommand.addPath(file.getName());
}
}
statusCommand.call().isClean();
}
}
public boolean isClean(Git git, String filePattern) throws Exception {
try {
Status status = git.status().addPath(filePattern).call();
return status.isClean();
} catch (Exception e) {
log.error("", e);
throw e;
}
}
}