JGit FetchCommand

这一生的挚爱 提交于 2019-12-22 12:37:36

问题


I understand the concept of Git fetch. I would like to fetch objects through the FetchCommand class in the JGit API.

I have a Git repository. For example C:\jGit\.git.

I have cloned this repository. For example: C:\jGitClone\jGit\.git.

I have cloned this repository again. For example C:\Users\user\Desktop\jGitClone\.git.

Using the JGit API I have a Git class:

Git git = new Git(repository); // this is the cloned repository. C:\jGitClone\jGit\.git

How would I set the cloned repository which is on the desktop so when the fetch is called it knows to fetch into the this repository.

I have looked on some websites including the 1 below but still stuck.

Git fetch failing using jgit: Remote does not have <branchname> available for fetch


回答1:


I think you missed something. in Git/jGit, a local copy is determined by a path (like in a file system), and a remote, which is a distant repo.

In your case, you want that your desktop cloned repo has a link to your jGitClone/jGit repo. So first, if the cloning is already done, then the desktop repo knows about its remote repo. So in your code, you just need to tell where your desktop repo is:

Builder builder = new FileRepositoryBuilder()
Git repo = builder.setGitDir(new File("C:\Users\user\Desktop\jGitClone.git"))
  .readEnvironment()
  .build()

The code might not be directly working (I didn't test it, the doubts I have are on the path to give, and the objects to use: Builder, Git), but it's based on this answer that I think will help you: JGit and finding the Head

By the way, your question is not that clear : did you already clone everything or is it what you want to do? in your code you mention a repository : did you obtain it using code similar to what I suggested? The names you chose are not very clear either : calling a repo jGit, unless it contains jGit source code, sounds a bit clumsy. Plus, give names to your different repos (A,B,C) to have a clearer understanding.



来源:https://stackoverflow.com/questions/12424825/jgit-fetchcommand

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!