Below code is of git push command through java everything if I run without using try catch block it successfully pushing the files, But im not getting that how to throw error if user enter wrong url and username and password by using try and catch block can anyone do correct edit in the code for it.
public void pushRepo (String repoUrl, String gitdirectory, String username, String password) throws GitAPIException
{
Git git = null;
try {
git = Git.open(new File(gitdirectory));
} catch (IOException e1) {
System.out.println("it is not git directory");
}
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin");
try {
remoteAddCommand.setUri(new URIish(repoUrl));
System.out.println("file added");
}catch (Exception e) {
System.out.println("Invalid RemoteUrl");
}
remoteAddCommand.call();
git.add().addFilepattern(".").call();
git.commit().setMessage("commited").call();
PushCommand pushCommand = git.push();
try {
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
pushCommand.setRemote("origin").add("master").call();
System.out.println("push file");
}catch(Exception e)
{
System.out.println("Invalid username and password");
}
}
}
If I enter value incorrect of any of url ,username and password it always give the message like "invalid username and password."