Search in sources :

Example 1 with MergeConflictHandler

use of org.archicontribs.modelrepository.merge.MergeConflictHandler in project archi-modelrepository-plugin by archi-contribs.

the class MergeBranchAction method merge.

private int merge(BranchInfo currentBranch, BranchInfo branchToMerge, ProgressMonitorDialog pmDialog) throws GitAPIException, IOException {
    pmDialog.getProgressMonitor().subTask(Messages.MergeBranchAction_13);
    // update dialog
    Display.getCurrent().readAndDispatch();
    try (Git git = Git.open(getRepository().getLocalRepositoryFolder())) {
        ObjectId mergeBase = git.getRepository().resolve(branchToMerge.getShortName());
        String mergeMessage = NLS.bind(Messages.MergeBranchAction_2, branchToMerge.getShortName(), currentBranch.getShortName());
        MergeResult mergeResult = git.merge().include(mergeBase).setCommit(true).setFastForward(FastForwardMode.FF).setStrategy(MergeStrategy.RECURSIVE).setSquash(false).setMessage(mergeMessage).call();
        MergeStatus status = mergeResult.getMergeStatus();
        // Conflict
        if (status == MergeStatus.CONFLICTING) {
            // Try to handle the merge conflict
            MergeConflictHandler handler = new MergeConflictHandler(mergeResult, branchToMerge.getShortName(), getRepository(), fWindow.getShell());
            try {
                handler.init(pmDialog.getProgressMonitor());
            } catch (IOException | GitAPIException ex) {
                // Clean up
                handler.resetToLocalState();
                if (ex instanceof CanceledException) {
                    return MERGE_STATUS_MERGE_CANCEL;
                }
                throw ex;
            }
            String dialogMessage = NLS.bind(Messages.MergeBranchAction_10, branchToMerge.getShortName(), currentBranch.getShortName());
            pmDialog.getShell().setVisible(false);
            boolean result = handler.openConflictsDialog(dialogMessage);
            pmDialog.getShell().setVisible(true);
            if (result) {
                handler.merge();
            } else // User cancelled - so we reset
            {
                handler.resetToLocalState();
                return MERGE_STATUS_MERGE_CANCEL;
            }
        }
        // Reload the model from the Grafico XML files
        GraficoModelLoader loader = new GraficoModelLoader(getRepository());
        loader.loadModel();
        // Do a commit if needed
        if (getRepository().hasChangesToCommit()) {
            mergeMessage = NLS.bind(Messages.MergeBranchAction_3, branchToMerge.getShortName(), currentBranch.getShortName());
            // Did we restore any missing objects?
            String restoredObjects = loader.getRestoredObjectsAsString();
            // Add to commit message
            if (restoredObjects != null) {
                // $NON-NLS-1$ //$NON-NLS-2$
                mergeMessage += "\n\n" + Messages.RefreshModelAction_3 + "\n" + restoredObjects;
            }
            // IMPORTANT!!! "amend" has to be false after a merge conflict or else the commit will be orphaned
            getRepository().commitChanges(mergeMessage, false);
        }
    }
    return MERGE_STATUS_OK;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) CanceledException(org.eclipse.jgit.api.errors.CanceledException) Git(org.eclipse.jgit.api.Git) GraficoModelLoader(org.archicontribs.modelrepository.grafico.GraficoModelLoader) ObjectId(org.eclipse.jgit.lib.ObjectId) MergeStatus(org.eclipse.jgit.api.MergeResult.MergeStatus) MergeResult(org.eclipse.jgit.api.MergeResult) MergeConflictHandler(org.archicontribs.modelrepository.merge.MergeConflictHandler) IOException(java.io.IOException)

Example 2 with MergeConflictHandler

use of org.archicontribs.modelrepository.merge.MergeConflictHandler in project archi-modelrepository-plugin by archi-contribs.

the class RefreshModelAction method pull.

protected int pull(UsernamePassword npw, ProgressMonitorDialog pmDialog) throws IOException, GitAPIException {
    PullResult pullResult = null;
    pmDialog.getProgressMonitor().subTask(Messages.RefreshModelAction_6);
    // update dialog
    Display.getCurrent().readAndDispatch();
    try {
        pullResult = getRepository().pullFromRemote(npw, new ProgressMonitorWrapper(pmDialog.getProgressMonitor()));
    } catch (Exception ex) {
        // So quietly absorb this and return OK
        if (ex instanceof RefNotAdvertisedException) {
            return PULL_STATUS_OK;
        }
        throw ex;
    }
    // Check for tracking updates
    FetchResult fetchResult = pullResult.getFetchResult();
    boolean newTrackingRefUpdates = fetchResult != null && !fetchResult.getTrackingRefUpdates().isEmpty();
    // Merge is already up to date...
    if (pullResult.getMergeResult().getMergeStatus() == MergeStatus.ALREADY_UP_TO_DATE) {
        // Check if any tracked refs were updated
        if (newTrackingRefUpdates) {
            return PULL_STATUS_OK;
        }
        return PULL_STATUS_UP_TO_DATE;
    }
    pmDialog.getProgressMonitor().subTask(Messages.RefreshModelAction_7);
    BranchStatus branchStatus = getRepository().getBranchStatus();
    // Setup the Graphico Model Loader
    GraficoModelLoader loader = new GraficoModelLoader(getRepository());
    // Merge failure
    if (!pullResult.isSuccessful() && pullResult.getMergeResult().getMergeStatus() == MergeStatus.CONFLICTING) {
        // Get the remote ref name
        String remoteRef = branchStatus.getCurrentRemoteBranch().getFullName();
        // Try to handle the merge conflict
        MergeConflictHandler handler = new MergeConflictHandler(pullResult.getMergeResult(), remoteRef, getRepository(), fWindow.getShell());
        try {
            handler.init(pmDialog.getProgressMonitor());
        } catch (IOException | GitAPIException ex) {
            // Clean up
            handler.resetToLocalState();
            if (ex instanceof CanceledException) {
                return PULL_STATUS_MERGE_CANCEL;
            }
            throw ex;
        }
        String dialogMessage = NLS.bind(Messages.RefreshModelAction_4, branchStatus.getCurrentLocalBranch().getShortName());
        pmDialog.getShell().setVisible(false);
        boolean result = handler.openConflictsDialog(dialogMessage);
        pmDialog.getShell().setVisible(true);
        if (result) {
            handler.merge();
        } else // User cancelled - we assume they committed all changes so we can reset
        {
            handler.resetToLocalState();
            return PULL_STATUS_MERGE_CANCEL;
        }
        // We now have to check if model can be reloaded
        pmDialog.getProgressMonitor().subTask(Messages.RefreshModelAction_8);
        // Reload the model from the Grafico XML files
        try {
            loader.loadModel();
        } catch (IOException ex) {
            // Clean up
            handler.resetToLocalState();
            throw ex;
        }
    } else {
        // Reload the model from the Grafico XML files
        pmDialog.getProgressMonitor().subTask(Messages.RefreshModelAction_8);
        loader.loadModel();
    }
    // Do a commit if needed
    if (getRepository().hasChangesToCommit()) {
        pmDialog.getProgressMonitor().subTask(Messages.RefreshModelAction_9);
        String commitMessage = NLS.bind(Messages.RefreshModelAction_1, branchStatus.getCurrentLocalBranch().getShortName());
        // Did we restore any missing objects?
        String restoredObjects = loader.getRestoredObjectsAsString();
        // Add to commit message
        if (restoredObjects != null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            commitMessage += "\n\n" + Messages.RefreshModelAction_3 + "\n" + restoredObjects;
        }
        // TODO - not sure if amend should be false or true here?
        getRepository().commitChanges(commitMessage, false);
    }
    return PULL_STATUS_OK;
}
Also used : CanceledException(org.eclipse.jgit.api.errors.CanceledException) FetchResult(org.eclipse.jgit.transport.FetchResult) MergeConflictHandler(org.archicontribs.modelrepository.merge.MergeConflictHandler) IOException(java.io.IOException) PullResult(org.eclipse.jgit.api.PullResult) RefNotAdvertisedException(org.eclipse.jgit.api.errors.RefNotAdvertisedException) GeneralSecurityException(java.security.GeneralSecurityException) CanceledException(org.eclipse.jgit.api.errors.CanceledException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RefNotAdvertisedException(org.eclipse.jgit.api.errors.RefNotAdvertisedException) GraficoModelLoader(org.archicontribs.modelrepository.grafico.GraficoModelLoader) BranchStatus(org.archicontribs.modelrepository.grafico.BranchStatus)

Aggregations

IOException (java.io.IOException)2 GraficoModelLoader (org.archicontribs.modelrepository.grafico.GraficoModelLoader)2 MergeConflictHandler (org.archicontribs.modelrepository.merge.MergeConflictHandler)2 CanceledException (org.eclipse.jgit.api.errors.CanceledException)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 BranchStatus (org.archicontribs.modelrepository.grafico.BranchStatus)1 Git (org.eclipse.jgit.api.Git)1 MergeResult (org.eclipse.jgit.api.MergeResult)1 MergeStatus (org.eclipse.jgit.api.MergeResult.MergeStatus)1 PullResult (org.eclipse.jgit.api.PullResult)1 RefNotAdvertisedException (org.eclipse.jgit.api.errors.RefNotAdvertisedException)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 FetchResult (org.eclipse.jgit.transport.FetchResult)1