我学习 git 并使用 JGit 从 java 代码访问 Git 存储库。 Git 默认情况下不允许克隆到非空目录。我们如何确定本地计算机中的特定 git 存储库已经完成了 git 克隆,以便我们随后只能执行 Git pull 操作?
目前我正在使用这种方法:
if a root folder is existing in the specified location
clone has been done
pull
else
clone
但不确定这是否正确。还有更好的想法吗?
谢谢。
这是我使用的方法,如 Jgit 邮件列表中所指定:
检查 git 存储库是否存在:
if (RepositoryCache.FileKey.isGitRepository(new File(<path_to_repo>), FS.DETECTED)) {
// Already cloned. Just need to open a repository here.
} else {
// Not present or not a Git repository.
}
但这不足以检查 git 克隆是否“成功”。部分克隆可能会使 isGitRepository() 评估为 true。要检查 git 克隆是否成功完成,至少需要检查一个引用是否不为空:
private static boolean hasAtLeastOneReference(Repository repo) {
for (Ref ref : repo.getAllRefs().values()) {
if (ref.getObjectId() == null)
continue;
return true;
}
return false;
}
感谢肖恩·皮尔斯的回答!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)