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>
This commit is contained in:
parent
0eb0d6c58f
commit
67910b0570
|
@ -52,6 +52,7 @@
|
||||||
import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
|
import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
|
||||||
import org.eclipse.jgit.api.MergeResult;
|
import org.eclipse.jgit.api.MergeResult;
|
||||||
import org.eclipse.jgit.api.errors.CheckoutConflictException;
|
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.AnyObjectId;
|
||||||
import org.eclipse.jgit.lib.Constants;
|
import org.eclipse.jgit.lib.Constants;
|
||||||
import org.eclipse.jgit.lib.ObjectId;
|
import org.eclipse.jgit.lib.ObjectId;
|
||||||
|
@ -103,7 +104,7 @@ void ffonly(@SuppressWarnings("unused") final boolean ignored) {
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
protected void run() throws Exception {
|
protected void run() {
|
||||||
if (squash && ff == FastForwardMode.NO_FF)
|
if (squash && ff == FastForwardMode.NO_FF)
|
||||||
throw die(CLIText.get().cannotCombineSquashWithNoff);
|
throw die(CLIText.get().cannotCombineSquashWithNoff);
|
||||||
// determine the merge strategy
|
// determine the merge strategy
|
||||||
|
@ -114,99 +115,108 @@ protected void run() throws Exception {
|
||||||
CLIText.get().unknownMergeStrategy, strategyName));
|
CLIText.get().unknownMergeStrategy, strategyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine the other revision we want to merge with HEAD
|
try {
|
||||||
final Ref srcRef = db.findRef(ref);
|
// determine the other revision we want to merge with HEAD
|
||||||
final ObjectId src = db.resolve(ref + "^{commit}"); //$NON-NLS-1$
|
final Ref srcRef = db.findRef(ref);
|
||||||
if (src == null)
|
final ObjectId src = db.resolve(ref + "^{commit}"); //$NON-NLS-1$
|
||||||
throw die(MessageFormat.format(
|
if (src == null)
|
||||||
CLIText.get().refDoesNotExistOrNoCommit, ref));
|
throw die(MessageFormat
|
||||||
|
.format(CLIText.get().refDoesNotExistOrNoCommit, ref));
|
||||||
|
|
||||||
Ref oldHead = getOldHead();
|
Ref oldHead = getOldHead();
|
||||||
MergeResult result;
|
MergeResult result;
|
||||||
try (Git git = new Git(db)) {
|
try (Git git = new Git(db)) {
|
||||||
MergeCommand mergeCmd = git.merge().setStrategy(mergeStrategy)
|
MergeCommand mergeCmd = git.merge().setStrategy(mergeStrategy)
|
||||||
.setSquash(squash).setFastForward(ff).setCommit(!noCommit);
|
.setSquash(squash).setFastForward(ff)
|
||||||
if (srcRef != null)
|
.setCommit(!noCommit);
|
||||||
mergeCmd.include(srcRef);
|
if (srcRef != null)
|
||||||
else
|
mergeCmd.include(srcRef);
|
||||||
mergeCmd.include(src);
|
else
|
||||||
|
mergeCmd.include(src);
|
||||||
|
|
||||||
if (message != null)
|
if (message != null)
|
||||||
mergeCmd.setMessage(message);
|
mergeCmd.setMessage(message);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = mergeCmd.call();
|
result = mergeCmd.call();
|
||||||
} catch (CheckoutConflictException e) {
|
} catch (CheckoutConflictException e) {
|
||||||
result = new MergeResult(e.getConflictingPaths()); // CHECKOUT_CONFLICT
|
result = new MergeResult(e.getConflictingPaths()); // CHECKOUT_CONFLICT
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (result.getMergeStatus()) {
|
|
||||||
case ALREADY_UP_TO_DATE:
|
|
||||||
if (squash)
|
|
||||||
outw.print(CLIText.get().nothingToSquash);
|
|
||||||
outw.println(CLIText.get().alreadyUpToDate);
|
|
||||||
break;
|
|
||||||
case FAST_FORWARD:
|
|
||||||
ObjectId oldHeadId = oldHead.getObjectId();
|
|
||||||
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(result.getMergeStatus().toString());
|
|
||||||
break;
|
|
||||||
case CHECKOUT_CONFLICT:
|
|
||||||
outw.println(CLIText.get().mergeCheckoutConflict);
|
|
||||||
for (String collidingPath : result.getCheckoutConflicts())
|
|
||||||
outw.println("\t" + collidingPath); //$NON-NLS-1$
|
|
||||||
outw.println(CLIText.get().mergeCheckoutFailed);
|
|
||||||
break;
|
|
||||||
case CONFLICTING:
|
|
||||||
for (String collidingPath : result.getConflicts().keySet())
|
|
||||||
outw.println(MessageFormat.format(CLIText.get().mergeConflict,
|
|
||||||
collidingPath));
|
|
||||||
outw.println(CLIText.get().mergeFailed);
|
|
||||||
break;
|
|
||||||
case FAILED:
|
|
||||||
for (Map.Entry<String, MergeFailureReason> entry : result
|
|
||||||
.getFailingPaths().entrySet())
|
|
||||||
switch (entry.getValue()) {
|
|
||||||
case DIRTY_WORKTREE:
|
|
||||||
case DIRTY_INDEX:
|
|
||||||
outw.println(CLIText.get().dontOverwriteLocalChanges);
|
|
||||||
outw.println(" " + entry.getKey()); //$NON-NLS-1$
|
|
||||||
break;
|
|
||||||
case COULD_NOT_DELETE:
|
|
||||||
outw.println(CLIText.get().cannotDeleteFile);
|
|
||||||
outw.println(" " + entry.getKey()); //$NON-NLS-1$
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
case MERGED:
|
|
||||||
String name;
|
switch (result.getMergeStatus()) {
|
||||||
if (!isMergedInto(oldHead, src))
|
case ALREADY_UP_TO_DATE:
|
||||||
name = mergeStrategy.getName();
|
if (squash)
|
||||||
else
|
outw.print(CLIText.get().nothingToSquash);
|
||||||
name = "recursive"; //$NON-NLS-1$
|
outw.println(CLIText.get().alreadyUpToDate);
|
||||||
outw.println(MessageFormat.format(CLIText.get().mergeMadeBy, name));
|
break;
|
||||||
break;
|
case FAST_FORWARD:
|
||||||
case MERGED_NOT_COMMITTED:
|
ObjectId oldHeadId = oldHead.getObjectId();
|
||||||
outw.println(CLIText.get().mergeWentWellStoppedBeforeCommitting);
|
if (oldHeadId != null) {
|
||||||
break;
|
String oldId = oldHeadId.abbreviate(7).name();
|
||||||
case MERGED_SQUASHED:
|
String newId = result.getNewHead().abbreviate(7).name();
|
||||||
case FAST_FORWARD_SQUASHED:
|
outw.println(MessageFormat.format(CLIText.get().updating,
|
||||||
case MERGED_SQUASHED_NOT_COMMITTED:
|
oldId, newId));
|
||||||
outw.println(CLIText.get().mergedSquashed);
|
}
|
||||||
outw.println(CLIText.get().mergeWentWellStoppedBeforeCommitting);
|
outw.println(result.getMergeStatus().toString());
|
||||||
break;
|
break;
|
||||||
case ABORTED:
|
case CHECKOUT_CONFLICT:
|
||||||
throw die(CLIText.get().ffNotPossibleAborting);
|
outw.println(CLIText.get().mergeCheckoutConflict);
|
||||||
case NOT_SUPPORTED:
|
for (String collidingPath : result.getCheckoutConflicts())
|
||||||
outw.println(MessageFormat.format(
|
outw.println("\t" + collidingPath); //$NON-NLS-1$
|
||||||
CLIText.get().unsupportedOperation, result.toString()));
|
outw.println(CLIText.get().mergeCheckoutFailed);
|
||||||
|
break;
|
||||||
|
case CONFLICTING:
|
||||||
|
for (String collidingPath : result.getConflicts().keySet())
|
||||||
|
outw.println(MessageFormat.format(
|
||||||
|
CLIText.get().mergeConflict, collidingPath));
|
||||||
|
outw.println(CLIText.get().mergeFailed);
|
||||||
|
break;
|
||||||
|
case FAILED:
|
||||||
|
for (Map.Entry<String, MergeFailureReason> entry : result
|
||||||
|
.getFailingPaths().entrySet())
|
||||||
|
switch (entry.getValue()) {
|
||||||
|
case DIRTY_WORKTREE:
|
||||||
|
case DIRTY_INDEX:
|
||||||
|
outw.println(CLIText.get().dontOverwriteLocalChanges);
|
||||||
|
outw.println(" " + entry.getKey()); //$NON-NLS-1$
|
||||||
|
break;
|
||||||
|
case COULD_NOT_DELETE:
|
||||||
|
outw.println(CLIText.get().cannotDeleteFile);
|
||||||
|
outw.println(" " + entry.getKey()); //$NON-NLS-1$
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case MERGED:
|
||||||
|
String name;
|
||||||
|
if (!isMergedInto(oldHead, src))
|
||||||
|
name = mergeStrategy.getName();
|
||||||
|
else
|
||||||
|
name = "recursive"; //$NON-NLS-1$
|
||||||
|
outw.println(
|
||||||
|
MessageFormat.format(CLIText.get().mergeMadeBy, name));
|
||||||
|
break;
|
||||||
|
case MERGED_NOT_COMMITTED:
|
||||||
|
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);
|
||||||
|
break;
|
||||||
|
case ABORTED:
|
||||||
|
throw die(CLIText.get().ffNotPossibleAborting);
|
||||||
|
case NOT_SUPPORTED:
|
||||||
|
outw.println(MessageFormat.format(
|
||||||
|
CLIText.get().unsupportedOperation, result.toString()));
|
||||||
|
}
|
||||||
|
} catch (GitAPIException | IOException e) {
|
||||||
|
throw die(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Ref getOldHead() throws IOException {
|
private Ref getOldHead() throws IOException {
|
||||||
|
|
Loading…
Reference in New Issue