cod*_*red 7 java validation jgit
有没有一种方法可以杀死中间的克隆操作?我将使用克隆来验证存储库?有没有其他方法来测试远程url/repository是否有效?
小智 5
您可以使用 JGIT 调用“git ls-remote”。看这里
示例代码如下:
    final LsRemoteCommand lsCmd = new LsRemoteCommand(null);
    final List<String> repos = Arrays.asList(
            "https://github.com/MuchContact/java.git",
            "git@github.com:MuchContact/java.git");
    for (String gitRepo: repos){
        lsCmd.setRemote(gitRepo);
        System.out.println(lsCmd.call().toString());
    }
我查看了 JGit 源代码,似乎没有提供检查远程存储库有效性的方法。
这是call以下方法CloneCommand:
public Git call() throws JGitInternalException {
    try {
        URIish u = new URIish(uri);
        Repository repository = init(u);
        FetchResult result = fetch(repository, u);
        if (!noCheckout)
            checkout(repository, result);
        return new Git(repository);
    } catch (IOException ioe) {
        throw new JGitInternalException(ioe.getMessage(), ioe);
    } catch (InvalidRemoteException e) {
        throw new JGitInternalException(e.getMessage(), e);
    } catch (URISyntaxException e) {
        throw new JGitInternalException(e.getMessage(), e);
    }
}
为了确定远程 URL 是否无效,在捕获 a 时,您可能会通过查找或 甚至JGitInternalException e得到实际原因,但正如您所指出的,如果它实际上有效,您最终会进行克隆;该库不允许您中断操作。e.getCause()InvalidRemoteExceptionURISyntaxException
更深入地研究 JGit 代码,该类TransportLocal具有open(URIsh,Repository,String)您可以用来检查是否InvalidRemoteException抛出异常的方法,但它的构造函数不是公共的。唉,需要一个自己动手的解决方案。你也许可以从我提到的方法的内容开始TransportLocal.open。