如何检查Git克隆是否已与JGit一起完成 [英] How to Check if A Git Clone Has Been Done Already with JGit

查看:648
本文介绍了如何检查Git克隆是否已与JGit一起完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习git并使用JGit从Java代码访问Git仓库。 Git默认情况下不允许克隆到非空目录。我们如何知道git克隆已经在本地机器上完成了一个特定的git repo,这样我们只能随后做一个git pull?



目前我'使用这种方法:

 如果指定位置存在根文件夹
克隆已完成
pull
else
clone

不知道这是否正确。任何更好的想法?



谢谢。

解决方案

我使用了Jgit邮件列表中指定的方式:

检查一个git存储库是否存在:



<$ p (RepositoryCache.FileKey.isGitRepository(new File(< path_to_repo>),FS.DETECTED)){

//已经被克隆。只需要在这里打开一个仓库。
} else {

//不存在或不是Git存储库。
}

但这不足以检查git克隆是否'成功'。部分克隆可能会使isGitRepository()评估为true。要检查git克隆是否成功完成,至少需要检查一个引用是否为空:

  private static boolean hasAtLeastOneReference (Repository){

for(Ref ref:repo.getAllRefs()。values()){
if(ref.getObjectId()== null)
continue;
返回true;
}

return false;
}

感谢Shawn Pearce给出答案!


I learning git and using JGit to access Git repos from java code. Git by default does not allow to clone to a non-empty directory. How do we figure out that a git clone has already been done for a particular git repo in the local machine so that we can only do a Git pull subsequently?

Currently I'm using this approach:

 if a root folder is existing in the specified location
     clone has been done
     pull 
 else
     clone

Not sure if this is correct though. Any better ideas?

Thank you.

解决方案

This is the approach I used, as specified in the Jgit mailing list:

Check if a git repository is existing:

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.
}

But this is insufficient to check if the git clone was 'successful'. A partial clone could make isGitRepository() evaluate to true. To check if the git clone was done successfully, need to check at least if one reference is not null:

private static boolean hasAtLeastOneReference(Repository repo) {

    for (Ref ref : repo.getAllRefs().values()) {
        if (ref.getObjectId() == null)
            continue;
        return true;
    }

    return false;
}

Thanks Shawn Pearce for the answer!

这篇关于如何检查Git克隆是否已与JGit一起完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆