pgm: Handle exceptions in RevParse 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: Iae510d8c6af9acd587822a28ad48eab0b2a96ccd
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
Matthias Sohn 2019-01-21 23:36:32 +01:00
parent 23af4452e8
commit 795c265c11
1 changed files with 23 additions and 18 deletions

View File

@ -45,6 +45,7 @@
package org.eclipse.jgit.pgm; package org.eclipse.jgit.pgm;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -69,27 +70,31 @@ class RevParse extends TextBuiltin {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
protected void run() throws Exception { protected void run() {
if (all) { try {
for (Ref r : db.getRefDatabase().getRefs()) { if (all) {
ObjectId objectId = r.getObjectId(); for (Ref r : db.getRefDatabase().getRefs()) {
// getRefs skips dangling symrefs, so objectId should never be ObjectId objectId = r.getObjectId();
// null. // getRefs skips dangling symrefs, so objectId should never
if (objectId == null) { // be null.
throw new NullPointerException(); if (objectId == null) {
throw new NullPointerException();
}
outw.println(objectId.name());
}
} else {
if (verify && commits.size() > 1) {
final CmdLineParser clp = new CmdLineParser(this);
throw new CmdLineException(clp,
CLIText.format(CLIText.get().needSingleRevision));
} }
outw.println(objectId.name());
}
} else {
if (verify && commits.size() > 1) {
final CmdLineParser clp = new CmdLineParser(this);
throw new CmdLineException(clp,
CLIText.format(CLIText.get().needSingleRevision));
}
for (ObjectId o : commits) { for (ObjectId o : commits) {
outw.println(o.name()); outw.println(o.name());
}
} }
} catch (IOException | CmdLineException e) {
throw die(e.getMessage(), e);
} }
} }
} }