2015-06-17 190 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; 
    } 
    } 
} 
相關問題