简体   繁体   中英

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. My work location is "C:/work/" .

In the work location git creates a .git-file with linking to the repo location.

When i use JGit i can not connect to my repo:


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.

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.

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. Going by the user guide you can create a customized Repository object like this:

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 git = new Git(repository);
git.reset()
   .setMode(ResetType.HARD)
   .call();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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