@Test public void testHardResetAfterSquashMerge() throws Exception { Git g = new Git(db); writeTrashFile("file1", "file1"); g.add().addFilepattern("file1").call(); RevCommit first = g.commit().setMessage("initial commit").call(); assertTrue(resolve(db.getWorkTree(), "file1").exists()); createBranch(first, "refs/heads/branch1"); checkoutBranch("refs/heads/branch1"); writeTrashFile("file2", "file2"); g.add().addFilepattern("file2").call(); g.commit().setMessage("second commit").call(); assertTrue(resolve(db.getWorkTree(), "file2").exists()); checkoutBranch("refs/heads/master"); MergeResult result = g.merge().include(db.getRef("branch1")).setSquash(true).call(); assertEquals(MergeResult.MergeStatus.FAST_FORWARD_SQUASHED, result.getMergeStatus()); assertNotNull(db.readSquashCommitMsg()); g.reset().setMode(ResetType.HARD).setRef(first.getName()).call(); assertNull(db.readSquashCommitMsg()); }
public static void pull(GitInput input) throws GitException { FetchResult fetchResult = fetch(input.directory, input.url); Ref ref = fetchResult.getAdvertisedRef("HEAD"); MergeResult mergeResult = merge(input, ref); if (!mergeResult.getMergeStatus().isSuccessful()) { throw new GitException("Merge of " + ref + " in " + input.directory + " failed"); } }
@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()); } }
@Override public String toString() { StringBuilder sb = new StringBuilder(); if (fetchResult != null) sb.append(fetchResult.toString()); else sb.append("No fetch result"); sb.append("\n"); if (mergeResult != null) sb.append(mergeResult.toString()); else if (rebaseResult != null) sb.append(rebaseResult.toString()); else sb.append("No update result"); return sb.toString(); }
@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())); } }
private boolean hasMergeResults() { final MergeResult mergeResult = result.getMergeResult(); return mergeResult != null && mergeResult.getMergeStatus() != MergeStatus.ALREADY_UP_TO_DATE; }