2012-11-27 49 views
2

我学习git并使用JGit从Java代码访问Git repos。 Git默认情况下不允许克隆到非空目录。我们如何知道git clone已经在本地机器上执行了一个特定的git repo,因此我们只能在随后执行Git pull?如何检查Git克隆是否已与JGit一起完成

目前我使用这种方法:

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

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

谢谢。

+1

在任何git仓库的底部,都有一个.git/config文件。那足以决定的存在吗? –

+0

@JasonD:是的,我也想过使用.git文件夹。也许.git/conf是一个更好的主意。将尝试一下。 – Izza

回答

6

这是我使用的方法,如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; 
} 

感谢肖恩·皮尔斯的回答!

+0

你可以给邮件列表链接吗? –

+0

原文出处:https://dev.eclipse.org/mhonarc/lists/jgit-dev/msg01892.html – marcusds