Browse Source

pgm: Handle exceptions in Merge 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: I7afd038d8def9d2f409c0ed297b9db8ea2fc75c1
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-5.4
Matthias Sohn 6 years ago
parent
commit
67910b0570
  1. 32
      org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java

32
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java

@ -52,6 +52,7 @@ import org.eclipse.jgit.api.MergeCommand;
import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.api.errors.CheckoutConflictException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
@ -103,7 +104,7 @@ class Merge extends TextBuiltin {
/** {@inheritDoc} */
@Override
protected void run() throws Exception {
protected void run() {
if (squash && ff == FastForwardMode.NO_FF)
throw die(CLIText.get().cannotCombineSquashWithNoff);
// determine the merge strategy
@ -114,18 +115,20 @@ class Merge extends TextBuiltin {
CLIText.get().unknownMergeStrategy, strategyName));
}
try {
// determine the other revision we want to merge with HEAD
final Ref srcRef = db.findRef(ref);
final ObjectId src = db.resolve(ref + "^{commit}"); //$NON-NLS-1$
if (src == null)
throw die(MessageFormat.format(
CLIText.get().refDoesNotExistOrNoCommit, ref));
throw die(MessageFormat
.format(CLIText.get().refDoesNotExistOrNoCommit, ref));
Ref oldHead = getOldHead();
MergeResult result;
try (Git git = new Git(db)) {
MergeCommand mergeCmd = git.merge().setStrategy(mergeStrategy)
.setSquash(squash).setFastForward(ff).setCommit(!noCommit);
.setSquash(squash).setFastForward(ff)
.setCommit(!noCommit);
if (srcRef != null)
mergeCmd.include(srcRef);
else
@ -152,8 +155,8 @@ class Merge extends TextBuiltin {
if (oldHeadId != null) {
String oldId = oldHeadId.abbreviate(7).name();
String newId = result.getNewHead().abbreviate(7).name();
outw.println(MessageFormat.format(CLIText.get().updating, oldId,
newId));
outw.println(MessageFormat.format(CLIText.get().updating,
oldId, newId));
}
outw.println(result.getMergeStatus().toString());
break;
@ -165,8 +168,8 @@ class Merge extends TextBuiltin {
break;
case CONFLICTING:
for (String collidingPath : result.getConflicts().keySet())
outw.println(MessageFormat.format(CLIText.get().mergeConflict,
collidingPath));
outw.println(MessageFormat.format(
CLIText.get().mergeConflict, collidingPath));
outw.println(CLIText.get().mergeFailed);
break;
case FAILED:
@ -190,16 +193,19 @@ class Merge extends TextBuiltin {
name = mergeStrategy.getName();
else
name = "recursive"; //$NON-NLS-1$
outw.println(MessageFormat.format(CLIText.get().mergeMadeBy, name));
outw.println(
MessageFormat.format(CLIText.get().mergeMadeBy, name));
break;
case MERGED_NOT_COMMITTED:
outw.println(CLIText.get().mergeWentWellStoppedBeforeCommitting);
outw.println(
CLIText.get().mergeWentWellStoppedBeforeCommitting);
break;
case MERGED_SQUASHED:
case FAST_FORWARD_SQUASHED:
case MERGED_SQUASHED_NOT_COMMITTED:
outw.println(CLIText.get().mergedSquashed);
outw.println(CLIText.get().mergeWentWellStoppedBeforeCommitting);
outw.println(
CLIText.get().mergeWentWellStoppedBeforeCommitting);
break;
case ABORTED:
throw die(CLIText.get().ffNotPossibleAborting);
@ -207,6 +213,10 @@ class Merge extends TextBuiltin {
outw.println(MessageFormat.format(
CLIText.get().unsupportedOperation, result.toString()));
}
} catch (GitAPIException | IOException e) {
throw die(e.getMessage(), e);
}
}
private Ref getOldHead() throws IOException {

Loading…
Cancel
Save