-1

I am using JGit library to push my changes to the repository. However, I want to provide the URL as well in the push command. I want the equivalent of following command in JGit:

git push <url>

Is this possible with JGit?

Note: Changing the config file is not desirable. Want to set the URL everytime I push.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Anu Chawla
  • 415
  • 1
  • 4
  • 19

1 Answers1

1

PushCommand::setRemote also allows setting the remote URL. From the JavaDoc:

The remote (uri or name) used for the push operation. If no remote is set, the default value of Constants.DEFAULT_REMOTE_NAME will be used.

For example:

push.setRemote( "http://examle.org/repo.git" ).call();

will push new commits on the current branch to a branch of the same name on the remote repository.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • I tried this earlier. Actually I want to provide the authentication itself in the url. If I do git push "https://username:password@" through cmd, it doesn't asks for authentication. But If I am doing push.setRemote( "https://username:password@" ).call(); in java, It is asking for authentication. It should behave same as in cmd, isn't it? – Anu Chawla Sep 15 '17 at 10:32
  • AFAIK JGit supports the `username:password` scheme. How does the rest of the URL look like? How do you recognize that JGit is _asking for authentication_? – Rüdiger Herrmann Sep 15 '17 at 12:40
  • JGIT is throwing exception : [git.GitUtilities : perfromPull()] - Exception : org.eclipse.jgit.api.errors.TransportException: https://git.web/repo.git/repo.git: Authentication is required but no CredentialsProvider has been registered. However I am providing the username and password in the URL itself (push.setRemote( "username:password@https://git.web/repo.git").call()) – Anu Chawla Sep 15 '17 at 13:06
  • What happens if you set a CredentialsProvider? – Rüdiger Herrmann Sep 15 '17 at 15:50