rhinmass
rhinmass

Reputation: 194

JGit - Can't track newly created branch

I would like to create a branch in an existing repository, then track that branch. The create branch succeeds, the the newly created branch is still tracking master. I have tried several different solutions, but same result - branch is created, but tracks master.

First I clone the repository:

Git.cloneRepository()./*set creds*/.setURI(..).setDirectory(...).call

So far so good.

Next, build the repository from the git file that was produced by the clone.

FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(gitFile).readEnvironment().findGitDir()
    .build();

At this point, I have tried both checkingOut the branch with createBranch set to true, and doing it in two steps - create, then check out. Here is the two-step method:

git.branchCreate()
        .setForce(true)
        .setName(branchName)
        .setStartPoint("origin/master")
        .call();
git.checkout()
        .setName(branchName)
        .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
        .setStartPoint("origin/"+branchName)
        .call();

Other things I have tried:

The result is always .git/config file that looks like:

[remote "origin"]
    url = ssh://..
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
    rebase = true
[branch "newbranch1"]
    remote = origin
    merge = refs/heads/master << TRACKING master, not newbranch1

On the branches I create with regular git (not jgit) the config file looks like:

[remote "origin"]
    url = ssh:...
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
    rebase = true
[branch "newbranch2"]
    remote = origin
    merge = refs/heads/newbranch2 << WANT THIS

Any thoughts out there on how I can make my new branch track the branch instead of master?

Using jgit-4.6.0.201612231935

Upvotes: 1

Views: 441

Answers (2)

rhinmass
rhinmass

Reputation: 194

Here is the final working code: (thank you Rüdiger Herrmann )

Git git = Git.cloneRepository...

git.checkout().setCreateBranch(true).setName(branchName).call();

pushCmd.setRemote( "origin" )
  .setRefSpecs( new RefSpec( branchName+":"+branchName )).call();

StoredConfig config = git.getRepository().getConfig();
config.setString( "branch", branchName, "remote", "origin" );
config.setString( "branch", branchName, "merge", "refs/heads/" + branchName );
config.save();

Upvotes: 0

R&#252;diger Herrmann
R&#252;diger Herrmann

Reputation: 20985

I think you cannot track a non-existing branch with JGit's CreateBranchCommand. setStartPoint() is only useful to specify where the new branch should point to initially.

However, you can directly manipulate the repository configuration with

StoredConfig config = repository.getConfig();
config.setString( "branch", "newbranch", "remote", "origin" );
config.setString( "branch", "newbranch", "merge", "refs/heads/newbranch" );
config.save();

Does that solve your problem?

Upvotes: 1

Related Questions