8

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.

Izza
  • 2,389
  • 8
  • 38
  • 60

3 Answers3

12

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!

Izza
  • 2,389
  • 8
  • 38
  • 60
  • Could you give the link o the mailing list ? – Mr_and_Mrs_D Jul 12 '14 at 22:55
  • https://www.eclipse.org/lists/jgit-dev/msg01892.html appears to be the mailing list reference talked about in the answer. – mkobit Aug 17 '20 at 16:32
  • 2
    I don't feel as though this answer is totally complete. The needs to include the .git directory and should most likely be < or /.git. You can see how git uses this method directly by looking at the source code and they pass in the .git directory: https://github.com/eclipse/jgit/blob/master/org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java#L540 – hooknc Jun 10 '21 at 16:57
4

A different approach would be to use the JGit FileRepositoryBuilder class.

public boolean repositoryExists(File directory) {

    boolean gitDirExists = false;

    FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
    repositoryBuilder.findGitDir(directory);

    if (repositoryBuilder.getGitDir() != null) {

        gitDirExists = true;
    }

    return gitDirExists;
}

This solution indirectly uses the same solution as the accepted answer, but it removes the direct usage of the RepositoryCache, FileKey and FS (FileSystem). While those classes are public in scope, they feel fairly low level and personally make me feel uneasy to use.

I do not remember exactly we came up with this solution. It might be from How to Access a Git Repository with JGit.

hooknc
  • 4,854
  • 5
  • 31
  • 60
1

The trick mentioned in @Izza's answer didn't work for me. I rather did this:

Java Sample:

 import org.eclipse.jgit.api.Git
 import org.eclipse.jgit.lib.Repository
 boolean isRepo(String fileDir) {
    try {
      Git Git = Git.open(new File(fileDir));
      Repository repo = jGIt.getRepository();
      for (Ref ref : repo.getAllRefs().values()) {
            if (ref.getObjectId() == null)
                continue;
            return true;
     }
     return false;
    } catch(Exception e) {
     return false;
    }
 }
Ravinder Payal
  • 2,884
  • 31
  • 40