8

I am using JGit and want to pull from the remote repository to my local repository.

The first approch was to clone the repository and that worked fine:

CredentialsProvider cp = new UsernamePasswordCredentialsProvider(username, password);
try (Git result = Git.cloneRepository()
    .setURI("http://172.20.1.2/team/myrepo.git")
    .setDirectory(new File("c:\\temp\\gittest"))
    .setCredentialsProvider(cp)
    .call()) {
        System.out.println("Having repository: " + result.getRepository().getDirectory());
    }

But after the second call the repository does not need to be cloned again. Therefore I thought I need to pull

Git git = Git.open(new File("c:\\temp\\gittest"));
git.pull().call();

But I get the following error:

org.eclipse.jgit.api.errors.TransportException: http://172.20.1.2/team/myrepo.git: Authentication is required but no CredentialsProvider has been registered

I do not know where I can pass the pull command the credentials.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Hauke
  • 1,405
  • 5
  • 23
  • 44

2 Answers2

14

You can pass a CredentialsProvider to the PushCommand in the same way as with the CloneCommand.

For example:

CredentialsProvider cp = new UsernamePasswordCredentialsProvider(username, password);
git.pull().setCredentialsProvider(cp).call();

All commands that connect to a remote repository, have a common base class: TransportCommand. And this class provides the means to specify authentication providers.

To learn more about authentication with JGit you may also want to have a look at the JGit Authentication Explained article I wrote some time ago.

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

If you are using org.apache.maven.scm:maven-scm-provider-jgit you can configure credentials as a <server> in Maven settings.xml. Be sure the <id> matches host of Git server.

Rafis Ganeev
  • 1,021
  • 6
  • 7