简体   繁体   English

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

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

I used "git init --separate-git-dir=C:/repo/.git" and define another location for the repository.我使用"git init --separate-git-dir=C:/repo/.git"并为存储库定义了另一个位置。 My work location is "C:/work/" .我的工作地点是"C:/work/"

In the work location git creates a .git-file with linking to the repo location.在工作位置 git 创建一个.git-file并链接到 repo 位置。

When i use JGit i can not connect to my repo:当我使用 JGit 时,我无法连接到我的仓库:


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

Then i get an exception:然后我得到一个例外:

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)

How to connect to my repository?如何连接到我的存储库?

Thanks for help.感谢帮助。

Edit:编辑:

Thanks to Joni who found a solution for my issue.感谢 Joni 为我的问题找到了解决方案。

Thank you Joni.谢谢乔尼。 You are awesome.你太棒了。 I tried it like you wrote with setting the work tree and it works like a charm: By the way i found an option to do this steps without to know where the repo location is.我像您在设置工作树时写的那样尝试了它,它就像一个魅力:顺便说一句,我找到了一个选项来执行此步骤,而无需知道回购位置在哪里。 For those who are interesed:对于那些有兴趣的人:

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(); 

It seems that JGit does not (yet) support the --separate-git-dir option. JGit 似乎(还)不支持--separate-git-dir选项。

As a work-around, you can open the linked repo directly:作为一种解决方法,您可以直接打开链接的存储库:

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

When used like this, JGit won't know where your working directory is so I imagine anything that has to do with the working directory will fail.像这样使用时,JGit 不会知道您的工作目录在哪里,所以我想与工作目录有关的任何事情都会失败。 Going by the user guide you can create a customized Repository object like this:按照用户指南,您可以像这样创建自定义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();

And then use the Git(Repository) constructor instead of Git.open :然后使用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