使用 JGit 创建一个从属性中克隆存储库的工具
Using JGit to create a tool that clones repositories out of properties
我正准备开发一个工具来读取属性文件,然后从中创建文件夹并从这些 URL 中提取存储库。
实际上我设法让它创建了一个 folder/subfolder 并将一个项目克隆到其中:
File repoOne = new File("repositories/repo1");
Git git = Git.cloneRepository()
.setURI("https://github.com/xetra11/renderay.git")
.setDirectory(repoOne)
.call();
当我第二次 运行 时,我收到以下错误消息:
Exception in thread "main" org.eclipse.jgit.api.errors.JGitInternalException: Destination path "repo1" already exists and is not an empty directory
我不太明白为什么 JGit 会尝试覆盖现有目录。我知道在那里重新克隆是没有意义的 - 但让我困扰的是 JGit 不只是 "move" 进入这个文件夹并执行命令。我即将在现有存储库中执行 FetchCommand
。在当前状态下,我假设 JGit 总是会告诉我目录已经存在。
任何想法如何让他简单地在文件夹中执行命令(如果它存在)?
测试repo(目录)是否已经存在并适当处理:
File repoOne = new File("repositories/repo1");
if (repoOne.exists()) {
// Check if it's a repo or do something else.
} else {
// Repository doesn't exist, create it.
}
要将文件夹用作克隆的本地目标,它必须不存在或为空。
您可能想要检测给定文件夹中是否存在存储库,然后只获取新的更改,否则克隆。
为了确定存储库是否存在,请使用此代码:
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
repositoryBuilder.setGitDir( folder );
Repository repository = repositoryBuilder.build();
boolean repositoryExists = repository.getRef( "HEAD" ) != null );
现在您可以克隆或获取,具体取决于存储库是否存在。
有关使用 JGit 访问存储库的更详细讨论,请参见此处:http://www.codeaffine.com/2014/09/22/access-git-repository-with-jgit/
我正准备开发一个工具来读取属性文件,然后从中创建文件夹并从这些 URL 中提取存储库。
实际上我设法让它创建了一个 folder/subfolder 并将一个项目克隆到其中:
File repoOne = new File("repositories/repo1");
Git git = Git.cloneRepository()
.setURI("https://github.com/xetra11/renderay.git")
.setDirectory(repoOne)
.call();
当我第二次 运行 时,我收到以下错误消息:
Exception in thread "main" org.eclipse.jgit.api.errors.JGitInternalException: Destination path "repo1" already exists and is not an empty directory
我不太明白为什么 JGit 会尝试覆盖现有目录。我知道在那里重新克隆是没有意义的 - 但让我困扰的是 JGit 不只是 "move" 进入这个文件夹并执行命令。我即将在现有存储库中执行 FetchCommand
。在当前状态下,我假设 JGit 总是会告诉我目录已经存在。
任何想法如何让他简单地在文件夹中执行命令(如果它存在)?
测试repo(目录)是否已经存在并适当处理:
File repoOne = new File("repositories/repo1");
if (repoOne.exists()) {
// Check if it's a repo or do something else.
} else {
// Repository doesn't exist, create it.
}
要将文件夹用作克隆的本地目标,它必须不存在或为空。
您可能想要检测给定文件夹中是否存在存储库,然后只获取新的更改,否则克隆。
为了确定存储库是否存在,请使用此代码:
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
repositoryBuilder.setGitDir( folder );
Repository repository = repositoryBuilder.build();
boolean repositoryExists = repository.getRef( "HEAD" ) != null );
现在您可以克隆或获取,具体取决于存储库是否存在。
有关使用 JGit 访问存储库的更详细讨论,请参见此处:http://www.codeaffine.com/2014/09/22/access-git-repository-with-jgit/