这可能重复,但我没有看到任何解决方案或适当的文档或示例。我试图将Git Hub存储库下载到本地计算机。以下是我的尝试。我使用 org.kohsuke.github API。我没有看到任何适当的方法下载到本地。
public void cloneRep() {
String login = "userid";
String password = "password";
String rep = "repository";
String localDir = "/home/myuser/repository/";
try {
System.out.println("Connecting...." + login + " : " + password);
GitHub gitHub = GitHub.connectUsingPassword(login, password);
boolean isValid = gitHub.isCredentialValid();
System.out.println("is Valid ? " + isValid);
if (isValid) {
GHRepository repository = gitHub.getRepository(rep);
//how to clone the repository to local directory?
}
} catch (Exception e) {
System.out.println("EXCEPTION....");
e.printStackTrace();
}
}
我知道 org.eclipse.jgit.api.Git 是可能的。
String remoteUrl = new StringBuffer().append("https").append(login)
.append(":").append(password).append("@github.com/")
.append(login).append("/").append(rep).toString();
org.eclipse.jgit.api.Git.cloneRepository().setURI(remoteUrl).setDirectory(localDir)
.call();
但这似乎更复杂。首先,我只需要进行身份验证。然后我需要下载到本地。我没兴趣同时使用它们。如果API提供了更好的解决方案,我可以使用它。
有没有解决方案?
答案 0 :(得分:2)
我可能错了,但 org.kohsuke.github API是关于在github上操纵存储库和要点。它不是在本地克隆存储库或执行其他与git相关的活动。
答案 1 :(得分:1)
已经在stackoverflow https://stackoverflow.com/questions/tagged/jgit
中讨论了JGit使用pom.xml获取JGit库或手动下载JAR文件
09:11:07,781 INFO - Myserver(1) << A-ABORT[source=0, reason=0]
09:11:07,852 INFO - Myserver(1): close Socket[addr=localhost/127.0.0.1,port=11112,localport=45714]
09:11:07,854 WARN - i/o exception in State Sta13
java.io.EOFException
at org.dcm4che2.net.PDUDecoder.readFully(PDUDecoder.java:100)
at org.dcm4che2.net.PDUDecoder.nextPDU(PDUDecoder.java:152)
at org.dcm4che2.net.Association.run(Association.java:790)
at java.lang.Thread.run(Thread.java:595)
java.io.EOFException
at org.dcm4che2.net.PDUDecoder.readFully(PDUDecoder.java:100)
at org.dcm4che2.net.PDUDecoder.nextPDU(PDUDecoder.java:152)
at org.dcm4che2.net.Association.run(Association.java:790)
at java.lang.Thread.run(Thread.java:595)
09:11:07,857 WARN - unable to send A-RELEASE-RQ in state: Sta1
Released connection to Myserver@localhost:11112
试试这个例子
JGit: Cannot find a tutorial or simple example或相应地跟随讨论中的网址
要进行身份验证,您可以将CloneCommand与setNoCheckout(true);
一起使用<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.0.0.201506090130-r</version>
</dependency>
不应手动创建<:> c:\ myfolder,它将自动创建
答案 2 :(得分:1)
我为此创建了一个实用程序类:
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.junit.Test;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.net.URL;
public class GithubClient {
/**
* @param githubRemoteUrl Remote git http url which ends with .git.
* @param accessToken Personal access token.
* @param branchName Name of the branch which should be downloaded
* @param destinationDir Destination directory where the downloaded files should be present.
* @return
* @throws Exception
*/
public boolean downloadRepoContent(@NotNull String githubRemoteUrl, @NotNull String accessToken, @NotNull String branchName, @NotNull String destinationDir) throws Exception {
//String githubSourceUrl, String accessToken
CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(accessToken, "");
URL fileUrl = new URL("file://"+destinationDir);
File destinationFile = FileUtils.toFile(fileUrl);
//delete any existing file
FileUtils.deleteDirectory(destinationFile);
Git.cloneRepository().setURI(githubSourceUrl)
.setBranch(branchName)
.setDirectory(destinationFile)
.setCredentialsProvider(credentialsProvider)
.call();
if(destinationFile.length() > 0){
return true;
}else{
return false;
}
}
}