I have an application that does not fully support git. The requirement is that remote repository is always the single source of truth so
- Commits are always pushed right away
- Fetch is done before any interaction to update local repository state
This is how I am fetching given branch on request
private void fetchBranch(String branch) throws GitAPIException {
RefSpec ref = new RefSpec().setSourceDestination(Constants.R_HEADS + branch, Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + branch)
.setForceUpdate(true);
RefSpec ref2 = new RefSpec().setSourceDestination(Constants.R_HEADS + branch, Constants.R_HEADS + branch)
.setForceUpdate(true);
git.fetch()
.setRemote(Constants.DEFAULT_REMOTE_NAME)
.setRefSpecs(ref2,ref)
.setCredentialsProvider(credentials.orElse(null))
.setProgressMonitor(new FetchProgressMonitor(log, configuration))
.setForceUpdate(true)
.call();
// git.branchCreate()
// .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM)
// .setName(branch)
// .setStartPoint(Constants.DEFAULT_REMOTE_NAME + "/" + branch)
// .setForce(true)
// .call();
}
My intention here is clearly to update (locally) remote head and local head to the state that is on remote repository.
However, only first ref in setRefSpecs gets updated
My intention is clear here, update 2 references (remotes/origin/branchName, and heads/branchName) with one value (remotes/origin/branchName) with value. I have checked JGit code and I see that there is some sort of check that skips references based on source ref (that is why the first argument is processed and second is not).
How to do it properly using fetch command? Do I really have to do branchChreate with setForce(true) to update the heads/branchName?
It would be convenient to have multiple destination RefSpec, but that does not exist.