克隆分支时前缀或后缀无效-Jgit

时间:2019-07-15 07:28:05

标签: java git github bitbucket jgit

我已经在代码中实现了 Jgit ,并且想从git服务器克隆存储库,所以我遇到了一个问题-如果我克隆普通master或其他分支,克隆成功完成,但是如果分支包含其中的 “ /” “功能/开发” ,则会出现以下错误-无效的前缀或后缀。

有人可以帮我吗?

下面是我编写的代码。

克隆分支方法-

public static void cloneSpecificBranchUsingSSH(String gitURL, Path file, String branchName, KeyPair kpair) throws Exception {
    try {
        LOGGER.info(file.toString());
        SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
            @Override
            protected void configure(Host host, Session session) {
                /**session.setConfig("StrictHostKeyChecking", "no");*/
                session.setUserInfo(new UserInfo() {

                    @Override
                    public void showMessage(String message) {
                        LOGGER.info("Default message.");
                    }

                    @Override
                    public boolean promptYesNo(String message) {
                        return false;
                    }

                    @Override
                    public boolean promptPassword(String message) {
                        return false;
                    }

                    @Override
                    public boolean promptPassphrase(String message) {
                        return false;
                    }

                    @Override
                    public String getPassword() {
                        return null;
                    }

                    @Override
                    public String getPassphrase() {
                        return null;
                    }
                });
            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                JSch jSch = super.createDefaultJSch(fs);
                jSch.removeAllIdentity();
                OutputStream pub_key = null;
                OutputStream pr_key = null;
                try
                {
                    pub_key = new ByteArrayOutputStream();
                    pr_key = new ByteArrayOutputStream();
                    kpair.writePrivateKey(pr_key);
                    String privateKey = pr_key.toString();
                    kpair.writePublicKey(pub_key,"");
                    String publicKey = pub_key.toString();
                    jSch.addIdentity("", privateKey.getBytes(), publicKey.getBytes(), "".getBytes());   
                }catch (Exception e) {
                    e.printStackTrace();
                    throw e;
                }finally {
                        try {
                            if(pub_key!=null)
                                pub_key.close();
                            if(pr_key!=null)
                                pr_key.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }
                return jSch;
            }
        };

        try (Git result = Git.cloneRepository().setURI(gitURL).setBranch(branchName)
                .setTransportConfigCallback(transport -> {
                    SshTransport sshTransport = (SshTransport) transport;
                    sshTransport.setSshSessionFactory(sshSessionFactory);
                }).setDirectory(file.toFile()).call()) {
            LOGGER.info("Having repository: " + result.getRepository().getDirectory());
        }
        LOGGER.info("Cloning completed");
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
        e.printStackTrace();
        throw e;

    }

}

在此处调用克隆分支方法-

public static void main(String[] args) throws Exception {
    String tempFilePath = System.getProperty("java.io.tmpdir");
    System.out.println(tempFilePath);
    Path file = Files.createTempDirectory(tempFilePath);
    cloneSpecificBranchUsingSSH(<Repository uri>,file ,"feature/staging",<SshKeyPair>);
}

谢谢!

0 个答案:

没有答案
相关问题