2

I am trying to create a remote branch with jgit, which executes exactly the following git commands:

  1. git clone git@gitlab.com:my-project/test.git
  2. git checkout -b superBranch
  3. git push --set-upstream origin superBranch

After these executions, I can change and push the files of the branch without merge request.

jGit:

Unfortunately jgit does not know the command "push -u" (Upstream). So I found some maybe solution. But all solutions does not work really.

First in StackOverflow:

// git clone done, than:
git.branchCreate()
   .setName("superBranch")
   .setForce(true)
   .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
   .setStartPoint("origin/superBranch").call(); // <-- Ref not found
RefSpec refSpec = new RefSpec().setSourceDestination("superBranch", "superBranch");
git.push()
   .setRefSpecs(refSpec)
   .setCredentialsProvider(provider).call();
git.checkout().setName("superBranch").call();

Exception:

org.eclipse.jgit.api.errors.RefNotFoundException: Ref origin/superBranch cannot be resolved

Another solution I found here Eclipse Forum:

git.branchCreate().setName("superBranch").call();
git.push()
   .setRemote("origin")
   .setRefSpecs(new RefSpec("superBranch" + ":" + "superBranch")) //<-- Ref not found
   .setCredentialsProvider(provider).call();
git.branchCreate()
   .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM)
   .setStartPoint("origin/" + "superBranch")
   .setForce(true).call();
git.checkout().setName("superBranch").call();

Exception:

org.eclipse.jgit.api.errors.InvalidRefNameException: Branch name <null> is not allowed

Does anyone know how can I create a remote and local branch, without call an api or make a merge request like my git example on the top?

Qeychon
  • 477
  • 1
  • 5
  • 15
  • I think you need to use fully qualified ref names. In your first snippet this would be `refs/remotes/origin/superBranch` to create the branch and `refs/heads/superBranch:refs/remotes/origin/superBranch` to push. – Rüdiger Herrmann Nov 27 '21 at 12:21
  • @RüdigerHerrmann thank you for your response. If I use the fully qualified ref names so I receive the same exception "RefNotFoundException: Ref refs/remotes/origin/superBranch cannot be resolved" – Qeychon Nov 27 '21 at 13:09
  • It looks like jgit is repeating Git's trick of denying you the ability to use the remote-tracking name (`refs/remotes/origin/superBranch` in this case) until it actually *exists*. That's not completely unreasonable, though there would be ways around it, just as there are in Git. – torek Nov 28 '21 at 00:54
  • It's also worth mentioning that the phrase "remote branch" is either nonsensical, or ambiguous: does it mean `superBranch` as it exists on the other (remote) Git repo, or does it mean `origin/superBranch` as it exists in your own Git repo? Hence I use the term *remote-tracking name*, which clearly means the one in your repository. – torek Nov 28 '21 at 00:56
  • i got the same problem,how did you solve this? – Freddy Feb 15 '22 at 06:33

2 Answers2

1

Following code works for me:

Git git = Git.cloneRepository()
             .setURI("https://gitlab.com/my-project/test.git")
             .setDirectory(new File("scratch/test"))
             .setCloneAllBranches(true)
             .setCredentialsProvider(provider).call();
git.checkout()
   .setCreateBranch(true)
   .setName(BRANCH).call();
git.push()
   .setRemote("origin")
   .setRefSpecs(new RefSpec(BRANCH + ":" + BRANCH))
   .setCredentialsProvider(provider).call();

So I clone the repository, checkout the branch or create the branch if not exits, then I push the new branch to the remote repository.

Qeychon
  • 477
  • 1
  • 5
  • 15
0

Finally, I tried this, that works for me.

git.branchCreate().setName(localBranchName).call();
git.push().setRemote("origin")
          .setCredentialsProvider(createCredential(name, password))
          .setRefSpecs(new RefSpec(localBranchName + ":" + localBranchName)).call();
//delete is necessary        
git.branchDelete().setBranchNames(localBranchName).call();
git.checkout().setCreateBranch(true).setName(localBranchName)
              .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
              .setStartPoint("origin/" + localBranchName)
              .call();
Freddy
  • 764
  • 2
  • 6
  • 21