0

I'm using JGit to checkout a branch from my git repository and modify a file. After I committed the changes I try to push it but running into TransportException:

Caused by: org.eclipse.jgit.errors.TransportException: Nothing to push.
at org.eclipse.jgit.transport.Transport.push(Transport.java:1332)
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:169)
...

My code looks like this:

this.checkoutBranch(branchName);
try (Writer writer = new OutputStreamWriter(new FileOutputStream(getFilePath(filepath).toFile(), true),
                    StandardCharsets.UTF_8))
{
    Yaml yaml = this.initializeYaml();
    Map<String, Object> parameterMap = this.getParameterMap(filepath, yaml);
    parameterMap.put(key, value);

    // write file
    yaml.dump(parameterMap, writer);

    // commit and push
    git.add().addFilepattern(filepath).call();
    git.commit().setMessage("Added parameter with key: '" + key + "'").call();

    git.push().setCredentialsProvider(getCredentialProvider()).call();
}
catch(GitAPIException | IOException e)
{
    throw new GitClientException("Cannot write parameters.", e);
}

Here is the method for checking out the current branch:

public void checkoutBranch(String branchName)
{
    try
    {
        git.checkout().setName("origin/" + branchName).call();
    }
    catch(GitAPIException e)
    {
        throw new GitClientException("Cannot checkout branch.", e);
    }
}

I looked for JGit examples and I found many of them, but no example handles file changes. Has anybody a hint what could be wrong?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
pDer666
  • 707
  • 5
  • 22
  • Have you tried to explicitly set the remote name (or URL) and the refspec? To make sure that there is actually a commit to push, you may want to use the `BranchTrackingStatus` to see that the local branch is ahead of the remote. In case the branch tracking isn't set up, you can adapt the code to compare to a given branch. – Rüdiger Herrmann Jul 24 '18 at 11:40
  • If I fetch the local status I can see that there are changes and these changes are committed. – pDer666 Jul 24 '18 at 11:49
  • How do you 'fetch the local status'? – Rüdiger Herrmann Jul 24 '18 at 15:03
  • @RüdigerHerrmann I updated my question with the method I fretch the branch. – pDer666 Jul 25 '18 at 05:04
  • I still don't understand what 'fetch the local status' means. The code checks out a remote branch and as it seems you end up with a detached HEAD. Can you confirm this? – Rüdiger Herrmann Jul 25 '18 at 06:04
  • I mean I check out a remote branch to my local mashine and if I check the status (in terminal (git status) or via code (git.status().call())) I can see that my changes are commited. Detached HEAD would mean that I am not working on a branch. But I don't get why. I checked out a remote branch and only modify a file. – pDer666 Jul 25 '18 at 06:16

1 Answers1

1

Calling checkoutBranch will result in a detached HEAD and this may be the reason why the push command fails.

git.getRepository().getFullBranch() now returns the object id (SHA-1) of the commit to which the remote branch points to.

To check out a remote branch without detaching HEAD, Git needs to create a local branch that acts as a proxy and tracks the remote branch. See JGit: Checkout a remote branch for how to achieve this with JGit.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79