Search in sources :

Example 11 with GitEventInfo

use of com.oxygenxml.git.view.event.GitEventInfo in project oxygen-git-client-addon by oxygenxml.

the class GitTestBase method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // Create the unstaged resources panel
    refreshSupport = new PanelRefresh(null) {

        @Override
        protected int getScheduleDelay() {
            // Execute refresh events immediately from tests.
            return 1;
        }
    };
    gitInit();
    ColorTheme colorTheme = Mockito.mock(ColorTheme.class);
    Mockito.when(colorTheme.isDarkTheme()).thenReturn(false);
    StandalonePluginWorkspace pluginWSMock = Mockito.mock(StandalonePluginWorkspace.class);
    Mockito.when(pluginWSMock.getColorTheme()).thenReturn(colorTheme);
    PluginWorkspaceProvider.setPluginWorkspace(pluginWSMock);
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            if (arguments.length == 2) {
                urls2compare.add((URL) arguments[0]);
                urls2compare.add((URL) arguments[1]);
            }
            return null;
        }
    }).when(pluginWSMock).openDiffFilesApplication(Mockito.any(), Mockito.any());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            if (arguments != null && arguments.length > 0) {
                toOpen.add((URL) invocation.getArguments()[0]);
            }
            return null;
        }
    }).when(pluginWSMock).open(Mockito.any());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            if (invocation.getArguments().length > 0) {
                WSEditorChangeListener listener = (WSEditorChangeListener) invocation.getArguments()[0];
                editorChangeListeners.add(listener);
            }
            return null;
        }
    }).when(pluginWSMock).addEditorChangeListener((WSEditorChangeListener) Mockito.any(), Mockito.anyInt());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WSEditorChangeListener listener = (WSEditorChangeListener) invocation.getArguments()[0];
            editorChangeListeners.remove(listener);
            return null;
        }
    }).when(pluginWSMock).removeEditorChangeListener((WSEditorChangeListener) Mockito.any(), Mockito.anyInt());
    Mockito.when(pluginWSMock.getEditorAccess((URL) Mockito.any(), Mockito.anyInt())).then(new Answer<WSEditor>() {

        @Override
        public WSEditor answer(InvocationOnMock invocation) throws Throwable {
            WSEditor wsEditorMock = createWSEditorMock((URL) invocation.getArguments()[0]);
            return wsEditorMock;
        }
    });
    XMLUtilAccess xmlUtilAccess = Mockito.mock(XMLUtilAccess.class);
    Mockito.when(xmlUtilAccess.escapeTextValue(Mockito.anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            Object object = invocation.getArguments()[0];
            return object != null ? (String) object : "";
        }
    });
    Mockito.when(xmlUtilAccess.unescapeAttributeValue(Mockito.anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            Object object = invocation.getArguments()[0];
            return object != null ? (String) object : "";
        }
    });
    Mockito.doReturn(xmlUtilAccess).when(pluginWSMock).getXMLUtilAccess();
    UtilAccess utilAccessMock = Mockito.mock(UtilAccess.class);
    Mockito.when(pluginWSMock.getUtilAccess()).thenReturn(utilAccessMock);
    Mockito.when(utilAccessMock.locateFile((URL) Mockito.any())).then(new Answer<File>() {

        @Override
        public File answer(InvocationOnMock invocation) throws Throwable {
            URL url = (URL) invocation.getArguments()[0];
            String path = url.getPath();
            if (PlatformDetectionUtil.isWin() && path.startsWith("/")) {
                path = path.substring(1, path.length());
            }
            return new File(url.getPath());
        }
    });
    // PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().getFileName()
    Mockito.when(utilAccessMock.getFileName(Mockito.anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            String file = (String) invocation.getArguments()[0];
            file = file.replace('\\', '/');
            int index = file.lastIndexOf("/");
            return index != -1 ? file.substring(index + 1) : file;
        }
    });
    Mockito.when(utilAccessMock.uncorrectURL(Mockito.anyString())).then(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0].toString().replace("%20", " ");
        }
    });
    ImageUtilities imgUtils = Mockito.mock(ImageUtilities.class);
    Mockito.when(pluginWSMock.getImageUtilities()).thenReturn(imgUtils);
    Mockito.when(imgUtils.loadIcon((URL) Mockito.any())).thenAnswer(new Answer<ImageIcon>() {

        @Override
        public ImageIcon answer(InvocationOnMock invocation) throws Throwable {
            URL url = (URL) invocation.getArguments()[0];
            return new ImageIcon(url);
        }
    });
    ProjectController projectCtrlMock = Mockito.mock(ProjectController.class);
    Mockito.when(pluginWSMock.getProjectManager()).thenReturn(projectCtrlMock);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            return null;
        }
    }).when(projectCtrlMock).refreshFolders(Mockito.any());
    WSOptionsStorage wsOptions = new TestWsOptionsStorage();
    Mockito.when(pluginWSMock.getOptionsStorage()).thenReturn(wsOptions);
    installGitProtocol();
    GitAccess gitAccess = GitAccess.getInstance();
    GitController ctrl = new GitController(gitAccess);
    ctrl.addGitListener(new GitEventAdapter() {

        private Repository oldRepository;

        @Override
        public void operationAboutToStart(GitEventInfo info) {
            if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
                try {
                    oldRepository = gitAccess.getRepository();
                } catch (NoRepositorySelected e) {
                // Ignore
                }
            }
        }

        @Override
        public void operationSuccessfullyEnded(GitEventInfo info) {
            if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
                if (oldRepository != null) {
                    loadedRepos.remove(oldRepository);
                }
                oldRepository = null;
            }
        }

        @Override
        public void operationFailed(GitEventInfo info, Throwable t) {
            if (info.getGitOperation() == GitOperation.OPEN_WORKING_COPY) {
                oldRepository = null;
            }
        }
    });
    OptionsManager.getInstance().loadOptions(wsOptions);
    gitAccess.getStatusCache().installEditorsHook(pluginWSMock);
}
Also used : ImageUtilities(ro.sync.exml.workspace.api.images.ImageUtilities) ImageIcon(javax.swing.ImageIcon) GitController(com.oxygenxml.git.view.event.GitController) UtilAccess(ro.sync.exml.workspace.api.util.UtilAccess) XMLUtilAccess(ro.sync.exml.workspace.api.util.XMLUtilAccess) URL(java.net.URL) ColorTheme(ro.sync.exml.workspace.api.util.ColorTheme) XMLUtilAccess(ro.sync.exml.workspace.api.util.XMLUtilAccess) WSOptionsStorage(ro.sync.exml.workspace.api.options.WSOptionsStorage) ProjectController(ro.sync.exml.workspace.api.standalone.project.ProjectController) Repository(org.eclipse.jgit.lib.Repository) GitEventInfo(com.oxygenxml.git.view.event.GitEventInfo) PanelRefresh(com.oxygenxml.git.view.refresh.PanelRefresh) WSEditorChangeListener(ro.sync.exml.workspace.api.listeners.WSEditorChangeListener) WSEditor(ro.sync.exml.workspace.api.editor.WSEditor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StandalonePluginWorkspace(ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace) File(java.io.File)

Example 12 with GitEventInfo

use of com.oxygenxml.git.view.event.GitEventInfo in project oxygen-git-client-addon by oxygenxml.

the class GitAccess method continueRebase.

/**
 * Continue rebase after a conflict resolution.
 */
public void continueRebase() {
    fireOperationAboutToStart(new GitEventInfo(GitOperation.CONTINUE_REBASE));
    GitOperationScheduler.getInstance().schedule(() -> {
        try {
            RebaseResult result = git.rebase().setOperation(Operation.CONTINUE).call();
            if (result.getStatus() == RebaseResult.Status.NOTHING_TO_COMMIT) {
                skipCommit();
            }
            fireOperationSuccessfullyEnded(new GitEventInfo(GitOperation.CONTINUE_REBASE));
        } catch (UnmergedPathsException e) {
            fireOperationFailed(new GitEventInfo(GitOperation.CONTINUE_REBASE), e);
            LOGGER.debug(e.getMessage(), e);
            PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(TRANSLATOR.getTranslation(Tags.CANNOT_CONTINUE_REBASE_BECAUSE_OF_CONFLICTS));
        } catch (GitAPIException e) {
            fireOperationFailed(new GitEventInfo(GitOperation.CONTINUE_REBASE), e);
            LOGGER.debug(e.getMessage(), e);
            PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(e.getMessage());
        }
    });
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitEventInfo(com.oxygenxml.git.view.event.GitEventInfo) BranchGitEventInfo(com.oxygenxml.git.view.event.BranchGitEventInfo) FileGitEventInfo(com.oxygenxml.git.view.event.FileGitEventInfo) WorkingCopyGitEventInfo(com.oxygenxml.git.view.event.WorkingCopyGitEventInfo) UnmergedPathsException(org.eclipse.jgit.api.errors.UnmergedPathsException) RebaseResult(org.eclipse.jgit.api.RebaseResult)

Example 13 with GitEventInfo

use of com.oxygenxml.git.view.event.GitEventInfo in project oxygen-git-client-addon by oxygenxml.

the class GitAccess method dropStash.

/**
 * Drops one stash item from the list of stashes.
 *
 * @param stashIndex The index of the stash item to be dropped.
 *
 * @throws GitAPIException
 */
public void dropStash(int stashIndex) throws GitAPIException {
    fireOperationAboutToStart(new GitEventInfo(GitOperation.STASH_DROP));
    try {
        git.stashDrop().setStashRef(stashIndex).call();
        fireOperationSuccessfullyEnded(new GitEventInfo(GitOperation.STASH_DROP));
    } catch (GitAPIException e) {
        LOGGER.error(e.getMessage(), e);
        fireOperationFailed(new GitEventInfo(GitOperation.STASH_DROP), e);
        throw e;
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitEventInfo(com.oxygenxml.git.view.event.GitEventInfo) BranchGitEventInfo(com.oxygenxml.git.view.event.BranchGitEventInfo) FileGitEventInfo(com.oxygenxml.git.view.event.FileGitEventInfo) WorkingCopyGitEventInfo(com.oxygenxml.git.view.event.WorkingCopyGitEventInfo)

Example 14 with GitEventInfo

use of com.oxygenxml.git.view.event.GitEventInfo in project oxygen-git-client-addon by oxygenxml.

the class GitAccess method displayStashApplyFailedCauseMessage.

/**
 * Display the stash apply failed cause message for user.
 *
 * @param isPop      <code>true</code> if the pop method.
 * @param status     stash status operation.
 * @param exception  the exception that cause the fail in apply stash.
 */
private void displayStashApplyFailedCauseMessage(boolean isPop, StashApplyStatus status, Exception exception) {
    List<String> conflictingList = new ArrayList<>(getConflictingFiles());
    if (!conflictingList.isEmpty() && status != StashApplyStatus.CANNOT_START_APPLY_BECAUSE_CONFLICTS) {
        status = StashApplyStatus.APPLIED_SUCCESSFULLY_WITH_CONFLICTS;
    }
    switch(status) {
        case APPLIED_SUCCESSFULLY_WITH_CONFLICTS:
            if (isPop) {
                FileStatusDialog.showWarningMessage(TRANSLATOR.getTranslation(Tags.APPLY_STASH), conflictingList, TRANSLATOR.getTranslation(Tags.STASH_GENERATE_CONFLICTS) + " " + TRANSLATOR.getTranslation(Tags.STASH_WAS_KEPT));
            } else {
                FileStatusDialog.showWarningMessage(TRANSLATOR.getTranslation(Tags.APPLY_STASH), conflictingList, TRANSLATOR.getTranslation(Tags.STASH_GENERATE_CONFLICTS));
            }
            fireOperationSuccessfullyEnded(new GitEventInfo(GitOperation.STASH_APPLY));
            break;
        case CANNOT_START_APPLY_BECAUSE_CONFLICTS:
            FileStatusDialog.showErrorMessage(TRANSLATOR.getTranslation(Tags.APPLY_STASH), new ArrayList<>(getConflictingFiles()), TRANSLATOR.getTranslation(Tags.UNABLE_TO_APPLY_STASH) + ". " + TRANSLATOR.getTranslation(Tags.RESOLVE_CONFLICTS_FIRST));
            fireOperationFailed(new GitEventInfo(GitOperation.STASH_APPLY), exception);
            LOGGER.error(exception.getMessage(), exception);
            break;
        case CANNOT_START_APPLY_BECAUSE_UNCOMMITTED_FILES:
            FileStatusDialog.showErrorMessage(TRANSLATOR.getTranslation(Tags.APPLY_STASH), null, TRANSLATOR.getTranslation(Tags.UNABLE_TO_APPLY_STASH) + ". " + TRANSLATOR.getTranslation(Tags.STASH_SOLUTIONS_TO_APPLY));
            fireOperationFailed(new GitEventInfo(GitOperation.STASH_APPLY), exception);
            LOGGER.error(exception.getMessage(), exception);
            break;
        case CANNOT_START_BECAUSE_STAGED_FILES:
            FileStatusDialog.showErrorMessage(TRANSLATOR.getTranslation(Tags.APPLY_STASH), null, TRANSLATOR.getTranslation(Tags.STASH_REMOVE_STAGED_CHANGES));
            fireOperationFailed(new GitEventInfo(GitOperation.STASH_APPLY), exception);
            LOGGER.error(exception.getMessage(), exception);
            break;
        default:
            PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(TRANSLATOR.getTranslation(Tags.UNABLE_TO_APPLY_STASH) + ".", exception);
            LOGGER.error(exception.getMessage(), exception);
            fireOperationFailed(new GitEventInfo(GitOperation.STASH_APPLY), exception);
            break;
    }
}
Also used : GitEventInfo(com.oxygenxml.git.view.event.GitEventInfo) BranchGitEventInfo(com.oxygenxml.git.view.event.BranchGitEventInfo) FileGitEventInfo(com.oxygenxml.git.view.event.FileGitEventInfo) WorkingCopyGitEventInfo(com.oxygenxml.git.view.event.WorkingCopyGitEventInfo) ArrayList(java.util.ArrayList)

Example 15 with GitEventInfo

use of com.oxygenxml.git.view.event.GitEventInfo in project oxygen-git-client-addon by oxygenxml.

the class GitAccess method revertCommit.

/**
 * Reverts the given commit.
 *
 * @param commitId  The commit id to which to reset.
 *
 * @throws IOException
 * @throws NoRepositorySelected
 * @throws GitAPIException
 */
public void revertCommit(String commitId) throws IOException, NoRepositorySelected, GitAPIException {
    Status gitStatus = git.status().call();
    if (!gitStatus.getConflicting().isEmpty()) {
        PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(Translator.getInstance().getTranslation(Tags.RESOLVE_CONFLICTS_FIRST));
    } else if (gitStatus.hasUncommittedChanges()) {
        FileStatusDialog.showErrorMessage(Translator.getInstance().getTranslation(Tags.REVERT_COMMIT), new ArrayList<>(gitStatus.getUncommittedChanges()), Translator.getInstance().getTranslation(Tags.REVERT_COMMIT_FAILED_UNCOMMITTED_CHANGES_MESSAGE));
    } else {
        fireOperationAboutToStart(new GitEventInfo(GitOperation.REVERT_COMMIT));
        Repository repo = git.getRepository();
        try (RevWalk revWalk = new RevWalk(repo)) {
            RevCommit revcom = revWalk.parseCommit(getRepository().resolve(commitId));
            git.revert().include(revcom).call();
            Set<String> conflictingFiles = getConflictingFiles();
            if (!conflictingFiles.isEmpty()) {
                FileStatusDialog.showWarningMessage(TRANSLATOR.getTranslation(Tags.REVERT_COMMIT), new ArrayList<>(conflictingFiles), TRANSLATOR.getTranslation(Tags.REVERT_COMMIT_RESULTED_IN_CONFLICTS));
            }
            fireOperationSuccessfullyEnded(new GitEventInfo(GitOperation.REVERT_COMMIT));
        } catch (GitAPIException | RevisionSyntaxException e) {
            fireOperationFailed(new GitEventInfo(GitOperation.REVERT_COMMIT), e);
            PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(e.getMessage(), e);
        }
    }
}
Also used : Status(org.eclipse.jgit.api.Status) StashApplyStatus(com.oxygenxml.git.view.stash.StashApplyStatus) BranchTrackingStatus(org.eclipse.jgit.lib.BranchTrackingStatus) FileStatus(com.oxygenxml.git.service.entities.FileStatus) MergeStatus(org.eclipse.jgit.api.MergeResult.MergeStatus) Repository(org.eclipse.jgit.lib.Repository) GitEventInfo(com.oxygenxml.git.view.event.GitEventInfo) BranchGitEventInfo(com.oxygenxml.git.view.event.BranchGitEventInfo) FileGitEventInfo(com.oxygenxml.git.view.event.FileGitEventInfo) WorkingCopyGitEventInfo(com.oxygenxml.git.view.event.WorkingCopyGitEventInfo) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

GitEventInfo (com.oxygenxml.git.view.event.GitEventInfo)23 WorkingCopyGitEventInfo (com.oxygenxml.git.view.event.WorkingCopyGitEventInfo)19 BranchGitEventInfo (com.oxygenxml.git.view.event.BranchGitEventInfo)18 FileGitEventInfo (com.oxygenxml.git.view.event.FileGitEventInfo)18 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)11 IOException (java.io.IOException)5 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 StashApplyStatus (com.oxygenxml.git.view.stash.StashApplyStatus)3 ArrayList (java.util.ArrayList)3 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)3 Repository (org.eclipse.jgit.lib.Repository)3 GitController (com.oxygenxml.git.view.event.GitController)2 GitOperation (com.oxygenxml.git.view.event.GitOperation)2 StashApplyFailureWithStatusException (com.oxygenxml.git.view.stash.StashApplyFailureWithStatusException)2 StashApplyFailureException (org.eclipse.jgit.api.errors.StashApplyFailureException)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 StandalonePluginWorkspace (ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace)2 OxygenGitPluginExtension (com.oxygenxml.git.OxygenGitPluginExtension)1 SSHCapableUserCredentialsProvider (com.oxygenxml.git.auth.SSHCapableUserCredentialsProvider)1 GitEventAdapter (com.oxygenxml.git.service.GitEventAdapter)1