首页 > 解决方案 > 在代码中传递 git url 时出现 InvalidPathException

问题描述

在我的代码中传递 url 时,我收到 java.nio.file.InvalidPathException 。我尝试了正斜杠和反斜杠,但仍然遇到同样的错误。代码-

import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

public class testInAction 
{
public static void main(String[] args) throws IOException 
{
FileRepositoryBuilder builder = new FileRepositoryBuilder();
String repoUrl = "http://stash.gto.intranet.db.com:8081/projects/PARAGON/repos/paragongit/browse";
Repository repository = builder.setGitDir(new File(repoUrl)).readEnvironment().findGitDir().build();
listRepositoryContents(repository);
        repository.close();
    }

private static void listRepositoryContents(Repository repository) throws IOException {
        for (Ref head : repository.getAllRefs().values()) {
            String refName = head.getName();
            ObjectId objId = head.getObjectId();
            System.out.println("refName " + refName);
            System.out.println("objId " + objId);
        }
    }
}

这可能是什么原因......建议一些

标签: javafile

解决方案


根据我的阅读,输入setGitDir应该是本地Git 存储库的相对或绝对路径,而不是远程存储库。因此,在那里使用远程 URL 没有任何意义,File(...)使用 URL 调用也没有任何意义。尝试以下方式:

FileRepositoryBuilder builder = new FileRepositoryBuilder();
String gitPath = "\path\to\your\git\folder";
Repository repository = builder.setGitDir(new File(gitPath))
    .readEnvironment().findGitDir().build();
listRepositoryContents(repository);
repository.close();

推荐阅读