简体   繁体   English

使用JGit创建新的本地存储库失败“使用Bare存储库既没有工作树......”

[英]Creating a new local repository with JGit fails “with Bare Repository has neither a working tree…”

I am attempting to use JGit to create a local non-bare repository. 我试图使用JGit创建一个本地非裸存储库。 I create the directory, then create a Repository object for that directory, then I use repository.create() . 我创建目录,然后为该目录创建一个Repository对象,然后我使用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 但是,行repository.create()会抛出异常

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." create方法的文档是:“创建一个新的Git存储库。使用此方法创建具有工作树的存储库。”

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

It seems that the documentation and FileRepository (meanwhile?) belongs to an internal package and access is discouraged. 似乎文档和FileRepository (同时?)属于内部包,不鼓励访问。

You can use the InitCommand to create repositories like so 您可以使用InitCommand创建这样的存储库

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. 默认情况下,这会创建一个非裸存储库,其中directory是工作目录,而directory/.git包含Git对象数据库。

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 . 为了完整起见:如果使用setBare( true )调用InitCommand ,将创建一个裸存储库,其对象数据库位于directory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM