/** * Parses the repository configuration file and returns the default fast-forward merge * option set for the repository and its current branch. * * @return the default fast-forward option for the current repository and the active branch. * @throws GitException an error occurs * @since 1.26 */ public FastForwardOption getDefaultFastForwardOption () throws GitException { JGitRepository repository = getRepository(); repository.increaseClientUsage(); try { MergeConfig cfg = MergeConfig.getConfigForCurrentBranch(repository.getRepository()); MergeCommand.FastForwardMode mode = cfg.getFastForwardMode(); switch (mode) { case FF_ONLY: return FastForwardOption.FAST_FORWARD_ONLY; case NO_FF: return FastForwardOption.NO_FAST_FORWARD; default: return FastForwardOption.FAST_FORWARD; } } finally { repository.decreaseClientUsage(); } }
public Pair<Boolean, String> merge(String branchToUpdate, String branchHead, boolean commit) { Pair<Boolean, String> ret = new Pair<>(false, ""); MergeCommand command = _git.merge(); try { String refName = !branchHead.contains(REFS_HEADS) ? REFS_REMOTES + branchHead : branchHead; command.include(_repo.getRef(refName)); command.setCommit(commit); MergeResult mergeResult = command.call(); ret = checkResult(branchToUpdate, branchHead, ret, mergeResult); } catch (Throwable e) { VerigreenLogger.get().log( getClass().getName(), RuntimeUtils.getCurrentMethodName(), String.format( "Failed to update branch [%s] with parent branch [%s]", branchToUpdate, branchHead)); } return ret; }
private static void setupMergeCommand(MergeCommand merge, List<Ref> commitsByRef, List<AnyObjectId> commitsById, List<NamedCommit> commitsByNameAndId, ProgressMonitor monitor, MergeStrategy strategy, MergeCommand.FastForwardMode fastForwardMode) { commitsByRef.forEach(merge::include); commitsById.forEach(merge::include); commitsByNameAndId.forEach(nc -> merge.include(nc.getName(), nc.getObjectId())); if (monitor != null) { merge.setProgressMonitor(monitor); } merge .setFastForward(fastForwardMode) .setStrategy(strategy); }
public static MergeResult mergeWithSquash(Git git, MergeStrategy strategy, List<Ref> commitsByRef, List<AnyObjectId> commitsById, List<NamedCommit> commitsByNameAndId, MergeCommand.FastForwardMode fastForwardMode, ProgressMonitor monitor) throws GitAPIException { MergeCommand merge = git.merge(); setupMergeCommand(merge, commitsByRef, commitsById, commitsByNameAndId, monitor, strategy, fastForwardMode); return merge .setStrategy(strategy) .setFastForward(fastForwardMode) .setSquash(true) .call(); }
public static MergeResult mergeWithoutCommit(Git git, MergeStrategy strategy, List<Ref> commitsByRef, List<AnyObjectId> commitsById, List<NamedCommit> commitsByNameAndId, MergeCommand.FastForwardMode fastForwardMode, ProgressMonitor monitor) throws GitAPIException { MergeCommand merge = git.merge(); setupMergeCommand(merge, commitsByRef, commitsById, commitsByNameAndId, monitor, strategy, fastForwardMode); return merge .setStrategy(strategy) .setFastForward(fastForwardMode) .setCommit(false) .call(); }
public static MergeResult mergeWithCommit(Git git, MergeStrategy strategy, List<Ref> commitsByRef, List<AnyObjectId> commitsById, List<NamedCommit> commitsByNameAndId, String commitMessage, MergeCommand.FastForwardMode fastForwardMode, ProgressMonitor monitor) throws GitAPIException { MergeCommand merge = git.merge(); setupMergeCommand(merge, commitsByRef, commitsById, commitsByNameAndId, monitor, strategy, fastForwardMode); return git.merge() .setMessage(commitMessage) // message to be used for merge commit .call(); }
public void updateChanges(ProgressMonitor monitor) throws Exception { Git git = getGit(monitor); FetchCommand fetch = git.fetch(); fetch.setCredentialsProvider(credentialsProvider); if (monitor != null) fetch.setProgressMonitor(monitor); fetch.call(); SyncState state = getSyncState(git); Ref fetchHead = git.getRepository().getRef("FETCH_HEAD"); switch (state) { case Equal: // Do nothing Log.d("Git", "Local branch is up-to-date"); break; case Ahead: Log.d("Git", "Local branch ahead, pushing changes to remote"); git.push().setCredentialsProvider(credentialsProvider).setRemote(remotePath).call(); break; case Behind: Log.d("Git", "Local branch behind, fast forwarding changes"); MergeResult result = git.merge().include(fetchHead).setFastForward(MergeCommand.FastForwardMode.FF_ONLY).call(); if (result.getMergeStatus().isSuccessful() == false) throw new IllegalStateException("Fast forward failed on behind merge"); break; case Diverged: Log.d("Git", "Branches are diverged, merging with strategy " + mergeStrategy.getName()); MergeResult mergeResult = git.merge().include(fetchHead).setStrategy(mergeStrategy).call(); if (mergeResult.getMergeStatus().isSuccessful()) { git.push().setCredentialsProvider(credentialsProvider).setRemote(remotePath).call(); } else throw new IllegalStateException("Merge failed for diverged branches using strategy " + mergeStrategy.getName()); break; } }
@Override public void doExecute() { try { MergeCommand mergeCommand = git.merge().setSquash(squash); mergeCommand.include(mergeCommand.getRepository().getRef(branchname)); setupCredentials(mergeCommand); MergeResult mergeResult = null; try { mergeResult = mergeCommand.call(); } catch (CheckoutConflictException conflicts) { throw new BuildException(String.format("%s - Checkout conflicts: %s", MESSAGE_MERGE_FAILED, conflicts.getConflictingPaths())); } if (!mergeResult.getMergeStatus().isSuccessful()) { if (mergeResult.getCheckoutConflicts() != null && mergeResult.getCheckoutConflicts().size() > 0) { throw new BuildException(String.format("%s - Checkout conflicts: %s", MESSAGE_MERGE_FAILED, mergeResult.getCheckoutConflicts())); } if (mergeResult.getFailingPaths() != null && mergeResult.getFailingPaths().size() > 0) { throw new BuildException(String.format("%s - Failing paths: %s", MESSAGE_MERGE_FAILED, mergeResult.getFailingPaths())); } throw new BuildException(String.format(MESSAGE_MERGE_FAILED_WITH_STATUS, mergeResult.getMergeStatus().name())); } } catch (Exception e) { throw new GitBuildException(String.format(MESSAGE_MERGE_FAILED_WITH_URI, getUri()), e); } }
@Test public void testMergeSelectedBranchWithCurrent() throws Exception { // first fetch from remote RefSpec fetchSpec = new RefSpec ("+refs/heads/*:refs/remotes/origin/*"); new Git(helper.getRepo()).fetch().setRefSpecs(fetchSpec).call(); // start tracking the remote branch "random" new Git(helper.getRepo()).checkout(). setCreateBranch(true). setForce(true). setName("random"). setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK). setStartPoint("origin/random"). call(); // make changes to local file and commit File file = Paths.get(this.repoPath.toString(), "test.txt").toFile(); assertTrue(file.exists()); try(PrintWriter fileTextWriter = new PrintWriter( file )){ fileTextWriter.println("Add a line to the file"); } this.helper.addFilePathTest(file.toPath()); this.helper.commit("Modified test.txt in a unit test!"); new Git(helper.getRepo()).checkout().setName("master").call(); // merge master into random MergeCommand merge = new Git(helper.getRepo()).merge(); merge.include(helper.getRepo().resolve("refs/heads/random")); MergeResult mergeResult = merge.call(); assertEquals(mergeResult.getMergeStatus(), MergeResult.MergeStatus.FAST_FORWARD); // TODO: fix this if you ever run it //CommitTreeController.update(helper); // after update, should expect the heads of two branches // are the same commit helper.getBranchModel().updateLocalBranches(); assertEquals(helper.getBranchModel().getBranchListTyped(BranchModel.BranchType.LOCAL).size(), 2); String masterHead = helper.getBranchModel().getBranchListTyped(BranchModel.BranchType.LOCAL).get(0).getHeadId().getName(); String randomHead = helper.getBranchModel().getBranchListTyped(BranchModel.BranchType.LOCAL).get(1).getHeadId().getName(); assertEquals(masterHead, randomHead); }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.createNewRepository()) { try (Git git = new Git(repository)) { // create some commit on master createCommit(repository, git, "masterFile", "content12"); // create branch "changes" Ref changes = git.branchCreate().setName("changes").call(); System.out.println("Result of creating the branch: " + changes); // now start a change on master createCommit(repository, git, "sharedFile", "content12"); // check out branch "changes" Ref checkout = git.checkout().setName("changes").call(); System.out.println("Result of checking out the branch: " + checkout); // create some commit on branch "changes", one of them conflicting with the change on master createCommit(repository, git, "branchFile", "content98"); createCommit(repository, git, "sharedFile", "content98"); // check out "master" checkout = git.checkout().setName("master").call(); System.out.println("Result of checking out master: " + checkout); // retrieve the objectId of the latest commit on branch ObjectId mergeBase = repository.resolve("changes"); // perform the actual merge, here we disable FastForward to see the // actual merge-commit even though the merge is trivial MergeResult merge = git.merge(). include(mergeBase). setCommit(true). setFastForward(MergeCommand.FastForwardMode.NO_FF). //setSquash(false). setMessage("Merged changes"). call(); System.out.println("Merge-Results for id: " + mergeBase + ": " + merge); for (Map.Entry<String,int[][]> entry : merge.getConflicts().entrySet()) { System.out.println("Key: " + entry.getKey()); for(int[] arr : entry.getValue()) { System.out.println("value: " + Arrays.toString(arr)); } } } } }