获取远程地址的 Repository 对象
Get Repository object for remote address
我尝试从 URL 地址获取一个 Repository
对象,JGit 使用如下代码:
Repository repository = Git.lsRemoteRepository()
.setHeads(true)
.setTags(true)
.setRemote(url)
.setCredentialsProvider(credentials)
.getRepository();
但是,使用该代码,repository
是 null
。另一方面,使用此代码
Collection<Ref> refs = Git.lsRemoteRepository()
.setHeads(true)
.setTags(true)
.setRemote(urlString)
.setCredentialsProvider(credentials)
.call();
可以获得Ref对象的集合,而且该方法似乎适用于远程URL。
我可以从 Ref 对象获取 Repository 对象吗?如何从 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();
}
JGit 的 Repository
class 表示本地存储库,通常是远程存储库的克隆。
Git::lsRemoteRepository
返回的 LsRemoteCommand
在本地存储库的上下文之外运行,因此 returns null
for getRepository
.
JGit 中的 Ref
也没有对存储库的引用,因为它们可能源自没有本地表示的存储库。请记住,例如 LsRemoteCommand
.
返回的引用没有本地存储库
要对存储库做任何有用的事情,首先需要克隆它。例如,使用:
Git git = Git.cloneRepository().setURI(url).call();
// do something with repository, access to repository through git.getRepostory()
git.close();
代码等同于git clone <url>
。如果 url 是 https://host.org/repo.git
,该命令将在当前工作目录的 repo
子目录中创建一个克隆。
可以在此处找到有关使用 JGit 克隆存储库的更多详细信息:https://www.codeaffine.com/2015/11/30/jgit-clone-repository/
我尝试从 URL 地址获取一个 Repository
对象,JGit 使用如下代码:
Repository repository = Git.lsRemoteRepository()
.setHeads(true)
.setTags(true)
.setRemote(url)
.setCredentialsProvider(credentials)
.getRepository();
但是,使用该代码,repository
是 null
。另一方面,使用此代码
Collection<Ref> refs = Git.lsRemoteRepository()
.setHeads(true)
.setTags(true)
.setRemote(urlString)
.setCredentialsProvider(credentials)
.call();
可以获得Ref对象的集合,而且该方法似乎适用于远程URL。
我可以从 Ref 对象获取 Repository 对象吗?如何从 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();
}
JGit 的 Repository
class 表示本地存储库,通常是远程存储库的克隆。
Git::lsRemoteRepository
返回的 LsRemoteCommand
在本地存储库的上下文之外运行,因此 returns null
for getRepository
.
JGit 中的 Ref
也没有对存储库的引用,因为它们可能源自没有本地表示的存储库。请记住,例如 LsRemoteCommand
.
要对存储库做任何有用的事情,首先需要克隆它。例如,使用:
Git git = Git.cloneRepository().setURI(url).call();
// do something with repository, access to repository through git.getRepostory()
git.close();
代码等同于git clone <url>
。如果 url 是 https://host.org/repo.git
,该命令将在当前工作目录的 repo
子目录中创建一个克隆。
可以在此处找到有关使用 JGit 克隆存储库的更多详细信息:https://www.codeaffine.com/2015/11/30/jgit-clone-repository/