private void completeUndo(Git git) { try { if (isUndo(git)) { git.checkout().setName("master").call(); git.merge() .setStrategy(MergeStrategy.THEIRS) .include(git.getRepository().resolve("undo")) .setCommit(true) .call(); git.branchDelete().setBranchNames("undo").call(); } } catch (RuntimeException ex) { } catch (CheckoutConflictException e) { throw new RuntimeException(e); } catch (RefAlreadyExistsException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } catch (InvalidRefNameException e) { throw new RuntimeException(e); } catch (RefNotFoundException e) { throw new RuntimeException(e); } catch (GitAPIException e) { throw new RuntimeException(e); } }
@Test public void testStageState_mergeAndReset_bug() throws Exception { try (Git git = new Git(db)) { writeTrashFile("a", "content"); git.add().addFilepattern("a").call(); RevCommit initialCommit = git.commit().setMessage("initial commit").call(); // create branch and add a new file final String branchName = Constants.R_HEADS + "branch"; createBranch(initialCommit, branchName); checkoutBranch(branchName); writeTrashFile("b", "second file content - branch"); git.add().addFilepattern("b").call(); RevCommit branchCommit = git.commit().setMessage("branch commit").call(); // checkout master and add the same new file checkoutBranch(Constants.R_HEADS + Constants.MASTER); writeTrashFile("b", "second file content - master"); git.add().addFilepattern("b").call(); git.commit().setMessage("master commit").call(); // try and merge MergeResult result = git.merge().include(branchCommit).call(); assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator); diff.diff(); assertTrue(diff.getChanged().isEmpty()); assertTrue(diff.getAdded().isEmpty()); assertTrue(diff.getRemoved().isEmpty()); assertTrue(diff.getMissing().isEmpty()); assertTrue(diff.getModified().isEmpty()); assertEquals(1, diff.getConflicting().size()); assertTrue(diff.getConflicting().contains("b")); assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates().get("b")); assertTrue(diff.getUntrackedFolders().isEmpty()); // reset file b to its master state without altering the index writeTrashFile("b", "second file content - master"); // we should have the same result iterator = new FileTreeIterator(db); diff = new IndexDiff(db, Constants.HEAD, iterator); diff.diff(); assertTrue(diff.getChanged().isEmpty()); assertTrue(diff.getAdded().isEmpty()); assertTrue(diff.getRemoved().isEmpty()); assertTrue(diff.getMissing().isEmpty()); assertTrue(diff.getModified().isEmpty()); assertEquals(1, diff.getConflicting().size()); assertTrue(diff.getConflicting().contains("b")); assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates().get("b")); assertTrue(diff.getUntrackedFolders().isEmpty()); } }
private static MergeResult merge(GitInput input, Ref ref) throws GitException { try { Git git = openRepository(input.directory); MergeCommand merge = git.merge(); merge.include(ref); merge.setCommit(input.createMergeCommit); merge.setSquash(input.squashCommit); merge.setFastForward(input.ffMode.getMode()); merge.setStrategy(input.mergeStrategy.getStrategy()); return merge.call(); } catch (GitAPIException e) { throw new GitException("Merge of " + ref + " in " + input.directory + " failed", e); } }
@Test public void testConflicting() throws Exception { try (Git git = new Git(db)) { writeTrashFile("a", "1\na\n3\n"); writeTrashFile("b", "1\nb\n3\n"); git.add().addFilepattern("a").addFilepattern("b").call(); RevCommit initialCommit = git.commit().setMessage("initial").call(); // create side branch with two modifications createBranch(initialCommit, "refs/heads/side"); checkoutBranch("refs/heads/side"); writeTrashFile("a", "1\na(side)\n3\n"); writeTrashFile("b", "1\nb\n3\n(side)"); git.add().addFilepattern("a").addFilepattern("b").call(); RevCommit secondCommit = git.commit().setMessage("side").call(); // update a on master to generate conflict checkoutBranch("refs/heads/master"); writeTrashFile("a", "1\na(main)\n3\n"); git.add().addFilepattern("a").call(); git.commit().setMessage("main").call(); // merge side with master MergeResult result = git.merge().include(secondCommit.getId()).setStrategy(MergeStrategy.RESOLVE).call(); assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus()); } FileTreeIterator iterator = new FileTreeIterator(db); IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator); diff.diff(); assertEquals("[b]", new TreeSet<String>(diff.getChanged()).toString()); assertEquals("[]", diff.getAdded().toString()); assertEquals("[]", diff.getRemoved().toString()); assertEquals("[]", diff.getMissing().toString()); assertEquals("[]", diff.getModified().toString()); assertEquals("[a]", diff.getConflicting().toString()); assertEquals(StageState.BOTH_MODIFIED, diff.getConflictingStageStates().get("a")); assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders()); }
private boolean merge( HttpServletRequest request, HttpServletResponse response, Repository db, String commitToMerge, boolean squash) throws ServletException, JSONException { try { ObjectId objectId = db.resolve(commitToMerge); Git git = new Git(db); MergeResult mergeResult = git.merge().setSquash(squash).include(objectId).call(); JSONObject result = new JSONObject(); result.put(GitConstants.KEY_RESULT, mergeResult.getMergeStatus().name()); if (mergeResult.getFailingPaths() != null && !mergeResult.getFailingPaths().isEmpty()) result.put(GitConstants.KEY_FAILING_PATHS, mergeResult.getFailingPaths()); OrionServlet.writeJSONResponse( request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT); return true; } catch (CheckoutConflictException e) { return workaroundBug356918(request, response, e); } catch (IOException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when merging.", e)); } catch (GitAPIException e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when merging.", e)); } }
@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())); } }