在github中添加head / ref / master路径的位置

时间:2014-08-14 18:20:13

标签: java github egit jgit

我正在使用jgit。我是新手我能够从github克隆代码但是当我试图在java中推送代码时它给了我错误。

这是代码: 公共类PullFromRemoteRepository {

private static final String REMOTE_URL = "https://github.com/raghav1/local.git";

public static void main(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("raghav345", "");
    localPath.delete();

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    Git.cloneRepository()
            .setURI(REMOTE_URL)
            .setDirectory(localPath)
            .call();

    // 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.pull()
            .call();

    System.out.println("Pulled from remote repository to local repository at " + repository.getDirectory());

    repository.close();
}

}

错误就是这样:

线程“main”中的异常org.eclipse.jgit.api.errors.NoHeadException:在没有HEAD的情况下拉入存储库当前不受支持     在org.eclipse.jgit.api.PullCommand.call(PullCommand.java:170)     at org.dstadler.jgit.unfinished.PullFromRemoteRepository.main(PullFromRemoteRepository.java:61)

任何人都可以帮助我设定头部路径。

由于

1 个答案:

答案 0 :(得分:0)

评论后的代码现在打开创建的存储库'实际上并没有打开克隆的存储库。因此pull命令作用于空存储库。

重新使用从Git返回的CloneCommand实例或将代码更改为以下内容:

Git git = Git.cloneRepository().setURI( REMOTE_URL ).setDirectory( new File( "local/path/to/repo" ) ).call();
File gitDir = git.getRepository().getDirectory();
git.close();
// ...
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir( gitDir ).setMustExist( true ).build();