Java 获取远程地址的存储库对象
我尝试从URL地址获取Java 获取远程地址的存储库对象,java,git,jgit,Java,Git,Jgit,我尝试从URL地址获取存储库对象,JGit使用如下代码: Repository repository = Git.lsRemoteRepository() .setHeads(true) .setTags(true) .setRemote(url) .setCredentialsProvider(credentials) .getRepository(); 但是,对于该代码,存储库是null。另一方面,使用此代码 Collection<Ref>
存储库
对象,JGit使用如下代码:
Repository repository = Git.lsRemoteRepository()
.setHeads(true)
.setTags(true)
.setRemote(url)
.setCredentialsProvider(credentials)
.getRepository();
但是,对于该代码,存储库
是null
。另一方面,使用此代码
Collection<Ref> refs = Git.lsRemoteRepository()
.setHeads(true)
.setTags(true)
.setRemote(urlString)
.setCredentialsProvider(credentials)
.call();
Collection refs=Git.lsRemoteRepository()
.setHeads(正确)
.setTags(真)
.setRemote(urlString)
.setCredentialsProvider(凭据)
.call();
可以获得Ref对象的集合,该方法似乎适用于远程URL
我可以从Ref对象获取存储库对象吗?如何从Ref对象开始查找文件?尝试以下操作:
String repoUrl = "https://github.com/GovindParashar136/SpringBootWithRestOpenIdClientAuthentication.git";
String cloneDirectoryPath = "/path/to/directory/"; // Ex.in windows c:\\gitProjects\SpringBootWithRestOpenIdClientAuthentication\
try {
System.out.println("Cloning "+repoUrl+" into "+repoUrl);
Git.cloneRepository()
.setURI(repoUrl)
.setDirectory(Paths.get(cloneDirectoryPath).toFile())
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("username", "password"))
.call();
System.out.println("Completed Cloning");
} catch (GitAPIException e) {
System.out.println("Exception occurred while cloning repo");
e.printStackTrace();
}
试试这个:
String repoUrl = "https://github.com/GovindParashar136/SpringBootWithRestOpenIdClientAuthentication.git";
String cloneDirectoryPath = "/path/to/directory/"; // Ex.in windows c:\\gitProjects\SpringBootWithRestOpenIdClientAuthentication\
try {
System.out.println("Cloning "+repoUrl+" into "+repoUrl);
Git.cloneRepository()
.setURI(repoUrl)
.setDirectory(Paths.get(cloneDirectoryPath).toFile())
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("username", "password"))
.call();
System.out.println("Completed Cloning");
} catch (GitAPIException e) {
System.out.println("Exception occurred while cloning repo");
e.printStackTrace();
}
JGit的
Repository
类表示本地存储库,通常是远程存储库的克隆
Git::lsRemoteRepository
返回的LsRemoteCommand
在本地存储库的上下文之外运行,因此对于getRepository
返回null
JGit中的Ref
也没有对存储库的引用,因为它们可能来自没有本地表示的存储库。请记住,例如,LsRemoteCommand
返回的引用没有本地存储库
要对存储库进行任何有用的操作,首先需要对其进行克隆。例如,使用:
Git Git=Git.cloneRepository().setURI(url.call();
//通过git.getRepostory()访问存储库
git.close();
该代码相当于git clone。如果url是https://host.org/repo.git
,该命令将在当前工作目录的repo
子目录中创建克隆
有关使用JGit克隆存储库的更多详细信息,请参见此处:JGit的
存储库
类表示本地存储库,通常是远程存储库的克隆
Git::lsRemoteRepository
返回的LsRemoteCommand
在本地存储库的上下文之外运行,因此对于getRepository
返回null
JGit中的Ref
也没有对存储库的引用,因为它们可能来自没有本地表示的存储库。请记住,例如,LsRemoteCommand
返回的引用没有本地存储库
要对存储库进行任何有用的操作,首先需要对其进行克隆。例如,使用:
Git Git=Git.cloneRepository().setURI(url.call();
//通过git.getRepostory()访问存储库
git.close();
该代码相当于git clone。如果url是https://host.org/repo.git
,该命令将在当前工作目录的repo
子目录中创建克隆
有关使用JGit克隆存储库的更多详细信息,请参见: