/** * Create a MergeOperation object * * @param repository * @param refName name of a commit which should be merged * @param mergeStrategyName the strategy to use for merge. If not registered, the default merge * strategy according to preferences will be used. */ public MergeOperation( @NonNull Repository repository, @NonNull String refName, @NonNull String mergeStrategyName) { this.repository = repository; this.refName = refName; MergeStrategy strategy = null; strategy = MergeStrategy.get(mergeStrategyName); this.mergeStrategy = strategy != null ? strategy : Activator.getDefault().getPreferredMergeStrategy(); }
public static ThreeWayMerger newThreeWayMerger( Repository repo, final ObjectInserter inserter, String strategyName) { MergeStrategy strategy = MergeStrategy.get(strategyName); checkArgument(strategy != null, "invalid merge strategy: %s", strategyName); Merger m = strategy.newMerger(repo, true); checkArgument( m instanceof ThreeWayMerger, "merge strategy %s does not support three-way merging", strategyName); m.setObjectInserter( new ObjectInserter.Filter() { @Override protected ObjectInserter delegate() { return inserter; } @Override public void flush() {} @Override public void close() {} }); return (ThreeWayMerger) m; }
@Override public String toString() { boolean first = true; StringBuilder commits = new StringBuilder(); for (ObjectId commit : mergedCommits) { if (!first) commits.append(", "); else first = false; commits.append(ObjectId.toString(commit)); } return MessageFormat.format( JGitText.get().mergeUsingStrategyResultedInDescription, commits, ObjectId.toString(base), mergeStrategy.getName(), mergeStatus, (description == null ? "" : ", " + description)); }
@Override protected void run() throws Exception { if (squash && ff == FastForwardMode.NO_FF) throw die(CLIText.get().cannotCombineSquashWithNoff); // determine the merge strategy if (strategyName != null) { mergeStrategy = MergeStrategy.get(strategyName); if (mergeStrategy == null) throw die(MessageFormat.format(CLIText.get().unknownMergeStrategy, strategyName)); } // determine the other revision we want to merge with HEAD final Ref srcRef = db.getRef(ref); final ObjectId src = db.resolve(ref + "^{commit}"); // $NON-NLS-1$ if (src == null) throw die(MessageFormat.format(CLIText.get().refDoesNotExistOrNoCommit, ref)); Ref oldHead = db.getRef(Constants.HEAD); Git git = new Git(db); MergeCommand mergeCmd = git.merge() .setStrategy(mergeStrategy) .setSquash(squash) .setFastForward(ff) .setCommit(!noCommit); if (srcRef != null) mergeCmd.include(srcRef); else mergeCmd.include(src); MergeResult result; try { result = mergeCmd.call(); } catch (CheckoutConflictException e) { 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(); outw.println( MessageFormat.format( CLIText.get().updating, oldHeadId.abbreviate(7).name(), result.getNewHead().abbreviate(7).name())); 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; 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())); } }
/** * Create a MergeOperation object * * @param repository * @param refName name of a commit which should be merged * @param mergeStrategy the strategy to use for merge */ public MergeOperation(Repository repository, String refName, String mergeStrategy) { this.repository = repository; this.refName = refName; if (mergeStrategy != null) this.mergeStrategy = MergeStrategy.get(mergeStrategy); }