[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jgit-dev] How to Check if A Git Clone Has Been Done Already with JGit

On Tue, Nov 27, 2012 at 10:51 AM, Pyeron, Jason J CTR (US)
<jason.j.pyeron.ctr@xxxxxxxx> wrote:
[>> -----Original Message-----
>> From: Isuru Haththotuwa
>> Sent: Tuesday, November 27, 2012 10:09 AM
>>
>> Hi,
>>
>> While using jgit to access git repositories from java code, I came
>> across this issue. Since cloning to a non-empty directory is not
>> allowed, I need to check if the local repository has been cloned
>> before. If it has been cloned earlier, I perform only a git pull, else
>
> Count the number of objects in the history?

Heh, no.

Use RepositoryCache.FileKey.isGitRepository(File, FS) e.g.:

  if (RepositoryCache.FileKey.isGitRepository(new
File("path/to/place", FS.DETECTED)) {
    // Already cloned. Just need to open a repository here.
  } else {
    // Not present or not a Git repository.
  }

This doesn't tell you if there is content *in* the repository, just
that a repository exists at the given location or not. Since clone
starts out by doing an init of a directory to be a repository, a
partial clone that isn't cleaned up might report true from
isGitRepository() but still have no data from the remote peer in it.

For that you want to look for at least one reference that has an ObjectId:

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