gpt4 book ai didi

java - Jgit:Bare Repository 既没有工作树,也没有索引

转载 作者:太空狗 更新时间:2023-10-29 14:28:56 24 4
gpt4 key购买 nike

我在 E 中创建了一个名为 gitrepo 的目录,完整路径为 (E:\gitrepo) 然后我使用以下代码在其中克隆了一个存储库

Git git=Git.cloneRepository()
.setURI("samplelink.git")
.setDirectory(new File("/E:/gitrepo"))
.call();

然后我使用这段代码打开了一个存储库

public Repository openRepository() throws IOException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();

Repository repository = builder.setGitDir(new File("/E:/gitrepo"))
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
log.info("Repository directory is {}", repository.getDirectory());

return repository;
}

到这里为止一切正常然后我尝试在这个本地存储库中添加一个文件

Repository repo = openRepository();
Git git = new Git(repo);
File myfile = new File(repo.getDirectory()/*.getParent()*/, "testfile");
if (!myfile.createNewFile()) {
throw new IOException("Could not create file " + myfile);
}
log.info("file created at{}", myfile.getPath());
git.add().addFilepattern("testfile").call();

然后我在这条线上得到了异常

git.add().addFilepattern("testfile").call();

这里是异常(exception)

Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index
at org.eclipse.jgit.lib.Repository.getIndexFile(Repository.java:1147)
at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:294)
at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:1205)
at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:149)
at com.km.GitAddFile.addFile(GitAddFile.java:26)

虽然文件代码创建在E:\gitrepo我已经通过这个命令检查了 gitrepo 是非裸存储库

/e/gitrepo (master)
$ git rev-parse --is-bare-repository

及其返回的false

请帮助我如何解决这个异常

最佳答案

使用 FileRepositoryBuilder 打开 Git 存储库很棘手。这是一个内部类。它的方法 setGitDir(File) 定义了存储库元数据的位置(.git 文件夹)。也就是说,它是用来构建一个 Git 裸仓库的。您可以通过调用 Repository#isBare() 来证明:

Repository repository = builder.setGitDir(new File("/E:/gitrepo"))
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
repository.isBare(); // returns true

你应该用 Git#open(File) 替换这个用法:

try (Git git = Git.open(new File("/E:/gitrepo"))) {
// Do sth ...
}

关于java - Jgit:Bare Repository 既没有工作树,也没有索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49282593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com