Search in sources :

Example 1 with MergeResult

use of org.eclipse.jgit.api.MergeResult in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method mergeProfileChanges.

@Override
public void mergeProfileChanges(Patch patch, File gitRepository, String versionBranch, String patchBranch) {
    Git git = null;
    try {
        git = Git.open(gitRepository);
        // merge version branch with patch branch - no fast forward, so we have nice named commit
        git.checkout().setCreateBranch(false).setName(versionBranch).call();
        MergeResult result = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(git.getRepository().resolve(patchBranch)).setCommit(false).call();
        boolean commit = true;
        if (result.getMergeStatus() == MergeResult.MergeStatus.CONFLICTING) {
            handleMergeConflict(git, result, versionBranch, patchBranch);
        } else if (result.getMergeStatus() != MergeResult.MergeStatus.MERGED_NOT_COMMITTED) {
            commit = false;
            Activator.log2(LogService.LOG_ERROR, "Can't merge version branch \"" + versionBranch + "\" with" + " patch branch \"" + patchBranch + "\". Resetting the branch.");
            git.reset().setMode(ResetCommand.ResetType.HARD).call();
        }
    // if (commit) {
    // git.commit()
    // .setMessage("Installing rollup patch \"" + patch.getPatchData().getId() + "\"")
    // .call();
    // }
    } catch (Exception e) {
        throw new PatchException(e.getMessage(), e);
    } finally {
        if (git != null) {
            try {
                git.branchDelete().setBranchNames(patchBranch).setForce(true).call();
            } catch (GitAPIException e) {
                Activator.log2(LogService.LOG_ERROR, e.getMessage());
            }
            gitPatchRepository.closeRepository(git, false);
        }
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) MergeResult(org.eclipse.jgit.api.MergeResult) PatchException(io.fabric8.patch.management.PatchException) PatchException(io.fabric8.patch.management.PatchException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with MergeResult

use of org.eclipse.jgit.api.MergeResult in project fabric8 by jboss-fuse.

the class GitConflictResolutionIT method mergeConflict.

@Test
public void mergeConflict() throws Exception {
    prepareChanges();
    RevWalk rw = new RevWalk(git.getRepository());
    git.checkout().setName("custom").setCreateBranch(false).call();
    MergeResult result = git.merge().setFastForward(MergeCommand.FastForwardMode.NO_FF).include(git.getRepository().resolve("patched")).call();
    assertThat(result.getMergeStatus(), equalTo(MergeResult.MergeStatus.CONFLICTING));
    assertThat(result.getConflicts().size(), equalTo(1));
    Map<String, IndexDiff.StageState> conflicts = git.status().call().getConflictingStageState();
    assertThat(conflicts.size(), equalTo(1));
    assertThat(conflicts.get("etc/org.ops4j.pax.logging.cfg"), equalTo(IndexDiff.StageState.BOTH_MODIFIED));
}
Also used : MergeResult(org.eclipse.jgit.api.MergeResult) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Test(org.junit.Test)

Example 3 with MergeResult

use of org.eclipse.jgit.api.MergeResult in project archi-modelrepository-plugin by archi-contribs.

the class RevertCommitAction method run.

@Override
public void run() {
    // Offer to save the model if open and dirty
    // We need to do this to keep grafico and temp files in sync
    IArchimateModel model = getRepository().locateModel();
    if (model != null && IEditorModelManager.INSTANCE.isModelDirty(model)) {
        if (!offerToSaveModel(model)) {
            return;
        }
    }
    // Do the Grafico Export first
    try {
        getRepository().exportModelToGraficoFiles();
    } catch (IOException ex) {
        displayErrorDialog(Messages.RevertCommitAction_0, ex);
        return;
    }
    // Then offer to Commit
    try {
        if (getRepository().hasChangesToCommit()) {
            if (!offerToCommitChanges()) {
                return;
            }
        }
    } catch (IOException | GitAPIException ex) {
        displayErrorDialog(Messages.RevertCommitAction_3, ex);
        return;
    }
    // Revert
    try (Git git = Git.open(getRepository().getLocalRepositoryFolder())) {
        RevertCommand revertCommand = doRevertCommand(git);
        MergeResult failingResult = revertCommand.getFailingResult();
        if (failingResult != null) {
            MergeConflictHandler handler = new MergeConflictHandler(failingResult, getRepository(), fWindow.getShell());
            boolean result = handler.checkForMergeConflicts();
            if (result) {
                handler.mergeAndCommit(Messages.RevertCommitAction_4, false);
            } else {
                // User cancelled - we assume user has committed all changes so we can reset
                handler.resetToLocalState();
            }
        } else {
            new GraficoModelLoader(getRepository()).loadModel();
        }
        // Save the checksum
        getRepository().saveChecksum();
        notifyChangeListeners(IRepositoryListener.HISTORY_CHANGED);
    } catch (IOException | GitAPIException ex) {
        displayErrorDialog(Messages.RevertCommitAction_1, ex);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) GraficoModelLoader(org.archicontribs.modelrepository.grafico.GraficoModelLoader) RevertCommand(org.eclipse.jgit.api.RevertCommand) MergeResult(org.eclipse.jgit.api.MergeResult) IOException(java.io.IOException) MergeConflictHandler(org.archicontribs.modelrepository.grafico.MergeConflictHandler) IArchimateModel(com.archimatetool.model.IArchimateModel)

Example 4 with MergeResult

use of org.eclipse.jgit.api.MergeResult in project egit by eclipse.

the class MergeOperation method execute.

@Override
public void execute(IProgressMonitor m) throws CoreException {
    if (mergeResult != null)
        throw new CoreException(new Status(IStatus.ERROR, Activator.getPluginId(), CoreText.OperationAlreadyExecuted));
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor mymonitor) throws CoreException {
            IProject[] validProjects = ProjectUtil.getValidOpenProjects(repository);
            SubMonitor progress = SubMonitor.convert(mymonitor, NLS.bind(CoreText.MergeOperation_ProgressMerge, refName), 3);
            try (Git git = new Git(repository)) {
                progress.worked(1);
                MergeCommand merge = git.merge().setProgressMonitor(new EclipseGitProgressTransformer(progress.newChild(1)));
                Ref ref = repository.findRef(refName);
                if (ref != null) {
                    merge.include(ref);
                } else {
                    merge.include(ObjectId.fromString(refName));
                }
                if (fastForwardMode != null) {
                    merge.setFastForward(fastForwardMode);
                }
                if (commit != null) {
                    merge.setCommit(commit.booleanValue());
                }
                if (squash != null) {
                    merge.setSquash(squash.booleanValue());
                }
                if (mergeStrategy != null) {
                    merge.setStrategy(mergeStrategy);
                }
                if (message != null) {
                    merge.setMessage(message);
                }
                mergeResult = merge.call();
                if (MergeResult.MergeStatus.NOT_SUPPORTED.equals(mergeResult.getMergeStatus())) {
                    throw new TeamException(new Status(IStatus.INFO, Activator.getPluginId(), mergeResult.toString()));
                }
            } catch (IOException e) {
                throw new TeamException(CoreText.MergeOperation_InternalError, e);
            } catch (NoHeadException e) {
                throw new TeamException(CoreText.MergeOperation_MergeFailedNoHead, e);
            } catch (ConcurrentRefUpdateException e) {
                throw new TeamException(CoreText.MergeOperation_MergeFailedRefUpdate, e);
            } catch (CheckoutConflictException e) {
                mergeResult = new MergeResult(e.getConflictingPaths());
                return;
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } finally {
                ProjectUtil.refreshValidProjects(validProjects, progress.newChild(1));
            }
        }
    };
    // lock workspace to protect working tree changes
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) NoHeadException(org.eclipse.jgit.api.errors.NoHeadException) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) SubMonitor(org.eclipse.core.runtime.SubMonitor) MergeCommand(org.eclipse.jgit.api.MergeCommand) MergeResult(org.eclipse.jgit.api.MergeResult) EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) IOException(java.io.IOException) CheckoutConflictException(org.eclipse.jgit.api.errors.CheckoutConflictException) IProject(org.eclipse.core.resources.IProject) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) TeamException(org.eclipse.team.core.TeamException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) CoreException(org.eclipse.core.runtime.CoreException) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException)

Example 5 with MergeResult

use of org.eclipse.jgit.api.MergeResult in project egit by eclipse.

the class DecoratableWorkingSetTest method testAnyConflictsIsConflicts.

@Test
public void testAnyConflictsIsConflicts() throws Exception {
    // Create new file and commit
    IFile file = createFile(project1, TEST_FILE, "Something");
    gitAdd(git, file);
    RevCommit masterCommit = gitCommit(git);
    // Create and checkout new branch, change file content, add and commit
    // file
    git.checkout().setCreateBranch(true).setName("first_topic").call();
    file = findFile(project1, TEST_FILE);
    write(file.getLocation().toFile(), "First Topic Content");
    project1.refreshLocal(IResource.DEPTH_INFINITE, null);
    gitAdd(git, file);
    RevCommit firstTopicCommit = gitCommit(git);
    // Create and checkout new branch (from master), change file content,
    // add and commit file
    git.checkout().setCreateBranch(true).setStartPoint(masterCommit).setName("second_topic").call();
    file = findFile(project1, TEST_FILE);
    write(file.getLocation().toFile(), "Second Topic Content");
    project1.refreshLocal(IResource.DEPTH_INFINITE, null);
    gitAdd(git, file);
    gitCommit(git);
    // merge second_topic with first_topic
    MergeResult mergeResult = git.merge().include(firstTopicCommit).call();
    assertEquals(MergeStatus.CONFLICTING, mergeResult.getMergeStatus());
    IDecoratableResource[] expectedDRs = new IDecoratableResource[] { newExpectedDecoratableResource(project1).tracked().conflicts(), newExpectedDecoratableResource(file).tracked().conflicts(), newExpectedDecoratableWorkingSet(WORKING_SET).tracked().conflicts() };
    IndexDiffData indexDiffData = waitForIndexDiff(true);
    IDecoratableResource[] actualDRs = { newDecoratableResource(indexDiffData, project1), newDecoratableResource(indexDiffData, file), newDecoratableWorkingSet(resourceMapping) };
    assertArrayEquals(expectedDRs, actualDRs);
    assertHasUnstagedChanges(true, actualDRs);
    assertDecorationConflicts(resourceMapping);
}
Also used : IFile(org.eclipse.core.resources.IFile) MergeResult(org.eclipse.jgit.api.MergeResult) IndexDiffData(org.eclipse.egit.core.internal.indexdiff.IndexDiffData) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Aggregations

MergeResult (org.eclipse.jgit.api.MergeResult)22 IOException (java.io.IOException)10 Test (org.junit.Test)9 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)7 RevCommit (org.eclipse.jgit.revwalk.RevCommit)7 Git (org.eclipse.jgit.api.Git)6 CoreException (org.eclipse.core.runtime.CoreException)4 IndexDiffData (org.eclipse.egit.core.internal.indexdiff.IndexDiffData)4 MergeCommand (org.eclipse.jgit.api.MergeCommand)3 MergeStatus (org.eclipse.jgit.api.MergeResult.MergeStatus)3 Ref (org.eclipse.jgit.lib.Ref)3 File (java.io.File)2 IFile (org.eclipse.core.resources.IFile)2 MultiStatus (org.eclipse.core.runtime.MultiStatus)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)2 CloneCommand (org.eclipse.jgit.api.CloneCommand)2 NoRemoteRepositoryException (org.eclipse.jgit.errors.NoRemoteRepositoryException)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 Repository (org.eclipse.jgit.lib.Repository)2