1. 我自己就用了几个可以,其它的没有测试
2. testClone 、gitPull
亲测可以
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import java.io.File;
import java.io.IOException;
public class GitUtils {
public String reUrl = "http://[email protected]:8080/project.git";
public String locPath = "D:\\project\\";
public String initPath = "D:\\test\\";
public static Boolean testClone(String username, String password, String branch, String localPath, String remoteUrl) throws IOException, GitAPIException {
UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider = new
UsernamePasswordCredentialsProvider(username, password);
CloneCommand cloneCommand = Git.cloneRepository();
Git git = cloneCommand.setURI(remoteUrl)
.setBranch("master")
.setDirectory(new File(localPath))
.setCredentialsProvider(usernamePasswordCredentialsProvider)
.call();
git.close();
return true;
}
public static Boolean testClone(String branch, String localPath, String remoteUrl) throws IOException, GitAPIException {
CloneCommand cloneCommand = Git.cloneRepository();
Git git = cloneCommand.setURI(remoteUrl)
.setBranch("master")
.setDirectory(new File(localPath))
.call();
git.close();
return true;
}
public void testCreate() throws IOException {
Repository newRepo = FileRepositoryBuilder.create(new File(initPath + "/.git"