How to Check if A Git Clone Has Been Done Already with JGit

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

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)
        return true;

    return false;

Thanks Shawn Pearce for the answer!

Source: stackoverflow