簡體   English   中英

Java GitHub API -org.kohsuke.github將存儲庫克隆到本地計算機

[英]Java GitHub API -org.kohsuke.github Clone repository to Local Machine

這可能是重復的,但是我看不到任何解決方案或適當的文檔或示例。 我正在嘗試將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提供了更好的解決方案,我可以使用它。

有什么解決辦法嗎?

我可能是錯的,但是org.kohsuke.github API是關於操縱github上的存儲庫和要點的 這與在本地克隆存儲庫或執行其他與git相關的活動無關。

JGit已經在stackoverflow 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:找不到教程或簡單的示例,或者相應地跟隨討論中的URL

要進行身份驗證,可以將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不應手動創建,它將自動創建

我為此創建了一個實用程序類:

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;
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM