pgm: Handle exceptions in Init command

This avoids we show a stacktrace on the console by default when this
type of exception is thrown during the run method is executed.

Change-Id: Ib8b26a6a02903de63ef58687a4a0820649d59f99
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2019-01-21 00:48:33 +01:00
parent 8b15ff57c8
commit 2d7806da31
1 changed files with 12 additions and 5 deletions

View File

@ -49,10 +49,12 @@
package org.eclipse.jgit.pgm;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.InitCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.kohsuke.args4j.Argument;
@ -74,7 +76,7 @@ protected final boolean requiresRepository() {
/** {@inheritDoc} */
@Override
protected void run() throws Exception {
protected void run() {
InitCommand command = Git.init();
command.setBare(bare);
if (gitdir != null) {
@ -83,9 +85,14 @@ protected void run() throws Exception {
if (directory != null) {
command.setDirectory(new File(directory));
}
Repository repository = command.call().getRepository();
outw.println(MessageFormat.format(
CLIText.get().initializedEmptyGitRepositoryIn, repository
.getDirectory().getAbsolutePath()));
Repository repository;
try {
repository = command.call().getRepository();
outw.println(MessageFormat.format(
CLIText.get().initializedEmptyGitRepositoryIn,
repository.getDirectory().getAbsolutePath()));
} catch (GitAPIException | IOException e) {
throw die(e.getMessage(), e);
}
}
}