How to check if A Git clone is completed with JGit

I am learning git and using JGit to access git repos from java code. git by default does not allow to clone a non-empty directory. How do we find out that the git clone has already been executed for a specific repo on git on the local machine, so that we can only do git pull afterwards?

I am currently 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 true. Any better ideas?

Thanks.

+2
source share
1 answer

This is the approach I used as indicated on the Jgit mailing list:

Check if git repository exists:

 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 not enough to check if the git clone was β€œsuccessful”. A partial clone could make isGitRepository () evaluate true. To check if the git clone was successfully executed, you need to check at least one non-null link:

 private static boolean hasAtLeastOneReference(Repository repo) { for (Ref ref : repo.getAllRefs().values()) { if (ref.getObjectId() == null) continue; return true; } return false; } 

Thanks to Shawn Pearce for the answer!

+6
source

Source: https://habr.com/ru/post/918088/


All Articles