4

I am attempting to use JGit to create a local non-bare repository. I create the directory, then create a Repository object for that directory, then I use repository.create(). My research here shows that this is supposed to create a local non-bare repository. However, the line repository.create() throws an exception

org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index. 

This would be the message that I would expect if I had created or referenced a bare repository, and then tried to add files to it. However, this error appears on the line that creates the repository, and I am very clearly making a non-bare repository. The documentation of the create method is: "Create a new Git repository. Repository with working tree is created using this method."

I have verified that the directory is created successfully.

What is going on here?

File repositoryPath = "test.git";
Repository repository = new FileRepository(repositoryPath);
repository.create(false); // This line throws the error
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Don Subert
  • 2,636
  • 4
  • 27
  • 37

1 Answers1

3

It seems that the documentation and FileRepository (meanwhile?) belongs to an internal package and access is discouraged.

You can use the InitCommand to create repositories like so

Git.init().setDirectory( directory ).call()

This creates a non-bare repository by default where directory is the working directory and directory/.git holds the Git object database.

For the sake of completeness: If you call the InitCommand with setBare( true ), a bare repository will be created with its object database located in directory.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • That was an amazing find. This is exactly right. It seems that the tutorials and documentation that I was reading predated their new, more intuitive interface. Also, thank you for showing me how to improve my question formatting. accepted +1 – Don Subert Mar 21 '15 at 09:50