簡體   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