문제

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

도움이 되었습니까?

해결책

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.

다른 팁

You need to define remote, which tracks the repository that you can fetch commits from, with valina git, you can do:

# suppose you have cloned C:\jGitClone\jGit\.git
$ cd C:\Users\user\Desktop\jGitClone\.git
$ git remote add upstream "C:\jGitClone\jGit\.git"
$ git fetch upstream

If you would like to try git24j, you can do this in java just like you would do with valina git:

Repository remote = Repository.open("C:\jGitClone\jGit\.git");
Repository local = Repository.open("C:\Users\user\Desktop\jGitClone\.git");
// set up remote
Remote upstream = Remote.create(local, "upstream", URI.create(remote.getPath()));
// fetch from remote
upstream.fetch(null, null, "reflog");

The two nulls are parameters that defines the refspecs to fetch and the options (where you can setup credentials etc).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top