无法使用jgit将本地目录克隆到远程github存储库

时间:2017-07-05 12:36:25

标签: java github-api jgit

我需要在我的本地机器上创建一个github目录,并且我正在尝试使用我的远程github存储库来克隆它。我尝试了很多方法使用以下代码克隆一个带有jGit的repo:

        package github;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
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 PushToRemoteRepository {

    private static final String REMOTE_URL = "https://github.com/syndy1989/Modify.git";

    public static void main(String[] args) throws IOException, GitAPIException {
        // prepare a new folder for the cloned repository
        File localPath = File.createTempFile("TestGitRepository", "");
        if(!localPath.delete()) {
            throw new IOException("Could not delete temporary file " + localPath);
        }
        UsernamePasswordCredentialsProvider upc = new UsernamePasswordCredentialsProvider("syndy1989", "xxxxx");
        // then clone
        System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
        Git result = Git.cloneRepository()
                .setURI(REMOTE_URL)
                .setDirectory(localPath)
                .setCredentialsProvider(upc)
                .call(); {

                    System.out.println(result);
                // now open the created repository
                FileRepositoryBuilder builder = new FileRepositoryBuilder();
                Repository repository = builder.setGitDir(localPath)
                        .readEnvironment() // scan environment GIT_* variables
                        .findGitDir() // scan up the file system tree
                        .build(); {
                    Git git = new Git(repository);{
                        git.push()
                                .call();
                    }

                    System.out.println("Pushed from repository: " + repository.getDirectory() + " to remote repository at " + REMOTE_URL);
                }
            }
        }
    }

我收到以下错误:

 Cloning from https://github.com/syndy1989/Modify.git to C:\Users\ADMINI~1\AppData\Local\Temp\2\TestGitRepository9138453641649961455
org.eclipse.jgit.api.Git@255b53dc
Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: origin: not found.
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:171)
    at github.PushToRemoteRepository.main(PushToRemoteRepository.java:47)
Caused by: org.eclipse.jgit.errors.NoRemoteRepositoryException: origin: not found.
    at org.eclipse.jgit.transport.TransportLocal$1.open(TransportLocal.java:131)
    at org.eclipse.jgit.transport.TransportBundleFile$1.open(TransportBundleFile.java:106)
    at org.eclipse.jgit.transport.Transport.open(Transport.java:556)
    at org.eclipse.jgit.transport.Transport.openAll(Transport.java:374)
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:144)
    ... 1 more

我看到了一些使用JGit和其他引用的示例,例如http://www.codeaffine.com/2015/11/30/jgit-clone-repository/ https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/CloneRemoteRepository.java

使用Jar:

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

<dependency>
    <groupId>org.eclipse.jgit</groupId>
    <artifactId>corg.eclipse.jgit</artifactId>
    <version>3.4.0.201406041058-rc3</version>
</dependency>

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>javax.validation</artifactId>
    <version>1.0.0.GA</version>
</dependency>

raw.json文件(位于docker容器内的“C:\ Git \ raw.json”文件夹中)我想要做的就是将Git目录克隆到远程github存储库以使用我的raw.json文件来推送java代码以自动方式。请帮助我使用工作示例在本地计算机上创建一个git目录,连接到远程git hub存储库&amp;使用java将.json文件推送到远程存储库。提前致谢

0 个答案:

没有答案