Skip to content
Advertisement

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:

JavaScript

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

Thank you.

Advertisement

Answer

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

Check if a git repository is existing:

JavaScript

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:

JavaScript

Thanks Shawn Pearce for the answer!

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement