2015-06-17 117 views
0

这可能是重复的,但我没有看到任何解决方案或适当的文档或示例。我正尝试将Git Hub存储库下载到本地计算机。以下是我试过的。我正在使用org.kohsuke.github API。我没有看到任何适当的方法下载到本地。Java GitHub API -org.kohsuke.github将存储库克隆到本地计算机

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提供了更好的解决方案,我可以使用它。

有没有解决方案?

回答

2

我可能是错的,但org.kohsuke.github API是关于操纵github上的存储库和要点。这不是关于在本地克隆存储库或执行其他git相关活动。

1

JGit是已经在计算器讨论https://stackoverflow.com/questions/tagged/jgit

使用你的pom.xml得到JGit库或手动下载JAR文件

<dependency> 
    <groupId>org.eclipse.jgit</groupId> 
    <artifactId>org.eclipse.jgit</artifactId> 
    <version>4.0.0.201506090130-r</version> 
</dependency> 

并尝试这个例子

JGit: Cannot find a tutorial or simple example或相应跟随讨论区内的网址

要进行身份验证,您可以使用CloneCommand setNoCheckout(true);

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.InvalidRemoteException; 
import org.eclipse.jgit.api.errors.TransportException; 
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; 
....... 
CloneCommand cloneCommand = Git.cloneRepository(); 
cloneCommand.setDirectory(new File("C:\\myfolder")); 
cloneCommand.setNoCheckout(true); 
cloneCommand.setRemote("https://github.com/<username>/<repositoru>.git"); 
cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("<username>", "<password>")); 
cloneCommand.call(); 

C:\ MyFolder文件不应手动创建的,它会自动

+0

感谢。有什么方法可以简单验证吗? – iCode

+0

嗨对不起,它正在抛出'JGitInternalException:目标路径“.git”已经存在并且不是一个空目录,即使凭据正确并且存储库也正确 – iCode

+1

实际上它在我的主目录中创建了存储库名称为 – iCode

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; 
    } 
    } 
} 
相关问题