JGit is a Java library to programmatically do actions versus a local or remote Git repository. It is quite powerful, but it comes with an issue: when trying to connect to a remote repository it doesn't handle URL redirection. When you try to connect to a remote repo like in the example below (the connection attempt is to one of my repos in GutHub):
everything is fine because there is no redirection. But trying to connect to another remote repository URL which performs redirection the following exception happens:
Caused by: org.eclipse.jgit.errors.TransportException: https://<url>/<repo_name>.git: 302 Found
Unfortunately the JGit library can't handle this, but a workaround is possible. Using curl with the following options:
From the output of this command you can retrieve the URL to which this one redirects and then replace the original one in your application code. This way the connection is successful and you can do all the actions you need to the remote repo.
String uri = "https://github.com/virtualramblas/publishtokafka-plugin.git";
String userName ="XXXXXXX"
String password = "YYYYYYY"
LsRemoteCommand remoteCommand = Git.lsRemoteRepository();
try {
Collection <Ref> refs =
remoteCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName, password))
.setHeads(true)
.setRemote(uri)
.call();
} catch(GitAPIException e) {
e.printStackTrace();
}
everything is fine because there is no redirection. But trying to connect to another remote repository URL which performs redirection the following exception happens:
Caused by: org.eclipse.jgit.errors.TransportException: https://<url>/<repo_name>.git: 302 Found
Unfortunately the JGit library can't handle this, but a workaround is possible. Using curl with the following options:
curl -u <username>:<password> -v https://<url>/<repo_name>.git
From the output of this command you can retrieve the URL to which this one redirects and then replace the original one in your application code. This way the connection is successful and you can do all the actions you need to the remote repo.
Comments
Post a Comment