|
|
@@ -3,12 +3,19 @@ package cn.reghao.autodop.dmaster.app.service.tools.updater;
|
|
|
import org.eclipse.jgit.api.CloneCommand;
|
|
|
import org.eclipse.jgit.api.Git;
|
|
|
import org.eclipse.jgit.api.PullCommand;
|
|
|
+import org.eclipse.jgit.api.PullResult;
|
|
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
|
|
import org.eclipse.jgit.internal.storage.file.FileRepository;
|
|
|
+import org.eclipse.jgit.lib.ObjectId;
|
|
|
+import org.eclipse.jgit.lib.Ref;
|
|
|
import org.eclipse.jgit.lib.Repository;
|
|
|
+import org.eclipse.jgit.revwalk.RevCommit;
|
|
|
+import org.eclipse.jgit.revwalk.RevWalk;
|
|
|
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* git 实现
|
|
|
@@ -17,47 +24,106 @@ import java.io.File;
|
|
|
* @date 2019-10-12 22:22:00
|
|
|
*/
|
|
|
public class GitImpl implements CodeUpdater {
|
|
|
- private String GIT = "/.git";
|
|
|
- private String branch = "master";
|
|
|
+ private String branch;
|
|
|
|
|
|
- private final UsernamePasswordCredentialsProvider credentials;
|
|
|
+ //private final UsernamePasswordCredentialsProvider credentials;
|
|
|
|
|
|
- public GitImpl(String username, String password) {
|
|
|
- this.credentials = new UsernamePasswordCredentialsProvider(username, password);
|
|
|
+ public GitImpl(String branch) {
|
|
|
+ this.branch = branch;
|
|
|
+ //this.credentials = new UsernamePasswordCredentialsProvider(username, password);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 不存在则 clone,存在则 pull
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2020-05-13 下午9:22
|
|
|
+ */
|
|
|
@Override
|
|
|
public String update(String remote, String local) throws Exception {
|
|
|
- File localRepo = new File(local + GIT);
|
|
|
+ File localRepo = new File(local + "/.git");
|
|
|
if (!localRepo.exists()) {
|
|
|
- clone(remote, local);
|
|
|
+ CommitInfo commitInfo = clone(remote, local);
|
|
|
} else {
|
|
|
- Repository repo = new FileRepository(localRepo.getAbsolutePath());
|
|
|
- PullCommand gitPull = new Git(repo).pull();
|
|
|
- gitPull.call();
|
|
|
+ try (Repository repo = new FileRepository(localRepo.getAbsolutePath())) {
|
|
|
+ PullCommand gitPull = new Git(repo).pull();
|
|
|
+ gitPull.call();
|
|
|
+ CommitInfo commitInfo = commitInfo(repo);
|
|
|
+ commitInfo.setRepoUrl(remote);
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- private void clone(String remote, String local) throws GitAPIException {
|
|
|
- CloneCommand gitClone = Git.cloneRepository().setURI(remote).setBranch(branch);
|
|
|
- Git git = gitClone.setDirectory(new File(local)).call();
|
|
|
- System.out.println();
|
|
|
+ private CommitInfo clone(String remote, String local) {
|
|
|
+ CloneCommand gitClone = Git.cloneRepository()
|
|
|
+ .setURI(remote)
|
|
|
+ .setBranch(branch)
|
|
|
+ .setDirectory(new File(local));
|
|
|
+
|
|
|
+ try (Git git = gitClone.call()) {
|
|
|
+ Repository repo = git.getRepository();
|
|
|
+ Ref head = repo.findRef("refs/heads/master");
|
|
|
+ ObjectId objectId = head.getObjectId();
|
|
|
+
|
|
|
+ RevWalk walk = new RevWalk(repo);
|
|
|
+ RevCommit lastCommit = walk.parseCommit(objectId);
|
|
|
+
|
|
|
+ CommitInfo commitInfo = new CommitInfo();
|
|
|
+ commitInfo.setCommitId(objectId.name());
|
|
|
+ commitInfo.setRepoUrl(remote);
|
|
|
+
|
|
|
+ return commitInfo;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public String localVersion(String local) throws Exception {
|
|
|
+ private CommitInfo commitInfo(Repository repo) throws IOException {
|
|
|
+ Ref head = repo.findRef("refs/heads/master");
|
|
|
+ ObjectId objectId = head.getObjectId();
|
|
|
+ try (RevWalk walk = new RevWalk(repo)) {
|
|
|
+ RevCommit lastCommit = walk.parseCommit(objectId);
|
|
|
+ int commitTime = lastCommit.getCommitTime();
|
|
|
+ String commitAuthor = lastCommit.getAuthorIdent().getName();
|
|
|
+ String commitMsg = lastCommit.getFullMessage();
|
|
|
+
|
|
|
+ CommitInfo commitInfo = new CommitInfo();
|
|
|
+ commitInfo.setCommitId(objectId.name());
|
|
|
+ commitInfo.setCommitTime(commitTime*1000);
|
|
|
+ commitInfo.setCommitAuthor(commitAuthor);
|
|
|
+ commitInfo.setCommitMsg(commitMsg);
|
|
|
+ commitInfo.setBranch(repo.getBranch());
|
|
|
+ return commitInfo;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> changedFileList() {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String localVersion(String local) {
|
|
|
+ File localRepo = new File(local + "/.git");
|
|
|
+ try (Repository repo = new FileRepository(localRepo.getAbsolutePath())) {
|
|
|
+ Ref head = repo.findRef("refs/heads/master");
|
|
|
+ ObjectId objectId = head.getObjectId();
|
|
|
+ return objectId.name();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
- GitImpl git = new GitImpl("reghao", "gjs");
|
|
|
+ String branch = "master";
|
|
|
+ GitImpl git = new GitImpl(branch);
|
|
|
String remote = "http://git.reghao.cn/reghao/spiderlab.git";
|
|
|
String local = "/home/reghao/tmp/autodop/opt/data/git/spiderlab";
|
|
|
-
|
|
|
- /*CloneCommand clone = Git.cloneRepository().setURI(remote);
|
|
|
- clone.setDirectory(new File(local)).call();*/
|
|
|
git.update(remote, local);
|
|
|
}
|
|
|
}
|