| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package devops;
- import cn.reghao.bnt.web.devops.build.model.po.RepoAuthConfig;
- import cn.reghao.bnt.web.devops.builder.model.constant.RepoAuthType;
- import cn.reghao.bnt.web.devops.builder.tool.repo.GitImpl;
- import cn.reghao.bnt.web.devops.deployer.model.po.RemoteHost;
- import cn.reghao.bnt.web.devops.deployer.util.Sftp;
- import cn.reghao.jutil.jdk.shell.ShellExecutor;
- import cn.reghao.jutil.jdk.shell.ShellResult;
- import org.eclipse.jgit.api.Git;
- import org.eclipse.jgit.api.errors.GitAPIException;
- import org.eclipse.jgit.internal.storage.file.FileRepository;
- 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.junit.jupiter.api.Test;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author reghao
- * @date 2022-05-07 09:32:40
- */
- public class GitTest {
- @Test
- public void gitTest() throws Exception {
- String repo = "";
- String username = "";
- String password = "";
- RepoAuthConfig repoAuthConfig = new RepoAuthConfig();
- repoAuthConfig.setAuthType(RepoAuthType.http.getName());
- repoAuthConfig.setUsername(username);
- repoAuthConfig.setPassword(password);
- GitImpl git = new GitImpl(repoAuthConfig.getRepoAuth());
- List<String> list = git.remoteBranches(repo);
- System.out.println();
- }
- @Test
- public void repoTest() throws IOException, GitAPIException {
- String localRepo = "/home/reghao/code/java/jutil/.git";
- //checkout(localRepo);
- Repository repo = new FileRepository(localRepo);
- String branch = null;
- String commit = null;
- getLogsSinceCommit(repo, branch, commit);
- }
- private List<String> getLogsSinceCommit(Repository repository, String branch, String commit) throws IOException, GitAPIException {
- if (branch == null) {
- branch = repository.getBranch();
- }
- List<String> commits = new ArrayList<>();
- Ref head = repository.findRef("refs/heads/" + branch);
- if (head != null) {
- try (RevWalk revWalk = new RevWalk(repository)) {
- revWalk.markStart(revWalk.parseCommit(head.getObjectId()));
- for (RevCommit revCommit : revWalk) {
- String commitId = revCommit.getId().getName();
- if (commitId.equals(commit)) {
- break;
- }
- long commitTime = revCommit.getCommitTime()*1000L;
- String fullMessage = revCommit.getFullMessage();
- commits.add(fullMessage);
- System.out.println("\nCommit-Message: " + fullMessage);
- }
- revWalk.dispose();
- }
- }
- return commits;
- }
- private void checkout(String localRepo) throws IOException, GitAPIException {
- Git git = Git.open(new File(localRepo));
- String branch = git.getRepository().getBranch();
- for (RevCommit revCommit : git.log().call()) {
- String commitId = revCommit.getId().getName();
- git.checkout().setStartPoint(revCommit);
- }
- }
- @Test
- public void compilerTest() {
- ShellExecutor shell = new ShellExecutor();
- String versionCmd = "/opt/env/java/maven/maven-3.6.0/bin/mvn -v";
- versionCmd = "/opt/env/dotnet/dotnet-sdk-7.0.203-linux-x64/dotnet --info";
- versionCmd = "/opt/env/node/node-v14.19.3-linux-x64/bin/node -v";
- versionCmd = "/usr/bin/python --version";
- versionCmd = "/usr/bin/java -version";
- versionCmd = "/usr/bin/cmake --version";
- versionCmd = "/usr/bin/g++ -v";
- versionCmd = "/usr/bin/gcc -v";
- ShellResult shellResult = shell.exec(versionCmd.split("\\s+"));
- if (shellResult.getExitCode() == 0) {
- System.out.println(shellResult.getResult());
- }
- }
- @Test
- public void sftpTest() throws Exception {
- String host = "127.0.0.1";
- int port = 22;
- String username = "root";
- String password = "gsh";
- RemoteHost remoteHost = new RemoteHost(host, port, username, password);
- Sftp sftp = new Sftp();
- String command = "docker ps -a";
- sftp.exec(remoteHost, command);
- }
- }
|