繁体   English   中英

JGit 通过.git-file 连接到单独的 git 存储库

[英]JGit connect to separate git repository by .git-file

我使用"git init --separate-git-dir=C:/repo/.git"并为存储库定义了另一个位置。 我的工作地点是"C:/work/"

在工作位置 git 创建一个.git-file并链接到 repo 位置。

当我使用 JGit 时,我无法连接到我的仓库:


Git
   .open(new File("C:\\work\\.git"))
   .reset()
   .setMode(ResetType.HARD)
   .call();

然后我得到一个例外:

Exception in thread "main" org.eclipse.jgit.errors.RepositoryNotFoundException: repository not found: C:\work\.git
    at org.eclipse.jgit.lib.BaseRepositoryBuilder.build(BaseRepositoryBuilder.java:582)
    at org.eclipse.jgit.api.Git.open(Git.java:117)
    at org.eclipse.jgit.api.Git.open(Git.java:99)
    at de.test.git.App.main(App.java:20)

如何连接到我的存储库?

感谢帮助。

编辑:

感谢 Joni 为我的问题找到了解决方案。

谢谢乔尼。 你太棒了。 我像您在设置工作树时写的那样尝试了它,它就像一个魅力:顺便说一句,我找到了一个选项来执行此步骤,而无需知道回购位置在哪里。 对于那些有兴趣的人:

Repository repo = new FileRepositoryBuilder() 
   .findGitDir(new File("C:\\work"))
   .setWorkTree(new File("C:\\work"))
   .build(); 

Git git  = new Git(repo); 
git
   .reset()
   .setMode(ResetType.HARD)
   .call(); 
git.close(); 

JGit 似乎(还)不支持--separate-git-dir选项。

作为一种解决方法,您可以直接打开链接的存储库:

Git
   .open(new File("C:/repo/.git"))
   .reset()
   .setMode(ResetType.HARD)
   .call();

像这样使用时,JGit 不会知道您的工作目录在哪里,所以我想与工作目录有关的任何事情都会失败。 按照用户指南,您可以像这样创建自定义Repository object:

FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File("C:/repo/.git"))
  .setWorkTree(new File("C:/work"))
  .readEnvironment() // scan environment GIT_* variables
  .build();

然后使用Git(Repository)构造函数而不是Git.open

Git git = new Git(repository);
git.reset()
   .setMode(ResetType.HARD)
   .call();

暂无
暂无

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

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