Search in sources :

Example 1 with GitOperationScheduler

use of com.oxygenxml.git.service.GitOperationScheduler in project oxygen-git-client-addon by oxygenxml.

the class EditorPageMenuGitActionsProvider method getActionsForCurrentEditorPage.

/**
 * Get the Git-specific action for the current editor page.
 *
 * @param editorURL Editor URL
 *
 * @return the Git-specific action for the current editor page.
 */
public List<AbstractAction> getActionsForCurrentEditorPage(URL editorURL) {
    List<AbstractAction> actions = new ArrayList<>();
    File file = PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().locateFile(editorURL);
    if (file == null) {
        return Collections.emptyList();
    }
    boolean isFromGitRepo = FileUtil.isFromGitRepo(file);
    if (isFromGitRepo) {
        GitOperationScheduler gitOpScheduler = GitOperationScheduler.getInstance();
        AbstractAction showHistoryAction = new AbstractAction(translator.getTranslation(Tags.SHOW_HISTORY)) {

            @Override
            public void actionPerformed(ActionEvent e) {
                gitOpScheduler.schedule(() -> ProjectAndEditorPageMenuActionsUtil.showHistory(file, historyCtrl, getViewsToSetCursorOn()));
            }
        };
        AbstractAction showBlameAction = new AbstractAction(translator.getTranslation(Tags.SHOW_BLAME)) {

            @Override
            public void actionPerformed(ActionEvent e) {
                gitOpScheduler.schedule(() -> ProjectAndEditorPageMenuActionsUtil.showBlame(file, historyCtrl, getViewsToSetCursorOn()));
            }
        };
        showBlameAction.setEnabled(true);
        showHistoryAction.setEnabled(true);
        actions.add(showHistoryAction);
        actions.add(showBlameAction);
    }
    return actions;
}
Also used : GitOperationScheduler(com.oxygenxml.git.service.GitOperationScheduler) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) AbstractAction(javax.swing.AbstractAction) File(java.io.File)

Example 2 with GitOperationScheduler

use of com.oxygenxml.git.service.GitOperationScheduler in project oxygen-git-client-addon by oxygenxml.

the class ProjectMenuGitActionsProvider method createAllActions.

/**
 * Create all actions.
 */
private void createAllActions() {
    GitOperationScheduler gitOpScheduler = GitOperationScheduler.getInstance();
    gitDiffAction = new AbstractAction(translator.getTranslation(Tags.GIT_DIFF)) {

        @Override
        public void actionPerformed(ActionEvent e) {
            gitOpScheduler.schedule(ProjectMenuGitActionsProvider.this::doGitDiff);
        }
    };
    commitAction = new AbstractAction(translator.getTranslation(Tags.COMMIT)) {

        @Override
        public void actionPerformed(ActionEvent e) {
            gitOpScheduler.schedule(ProjectMenuGitActionsProvider.this::doPrepareCommit);
        }
    };
    showHistoryAction = new AbstractAction(translator.getTranslation(Tags.SHOW_HISTORY)) {

        @Override
        public void actionPerformed(ActionEvent e) {
            gitOpScheduler.schedule(() -> ProjectAndEditorPageMenuActionsUtil.showHistory(pluginWS.getProjectManager().getSelectedFiles()[0], historyCtrl, getViewsToSetCursorOn()));
        }
    };
    showBlameAction = new AbstractAction(translator.getTranslation(Tags.SHOW_BLAME)) {

        @Override
        public void actionPerformed(ActionEvent e) {
            gitOpScheduler.schedule(() -> ProjectAndEditorPageMenuActionsUtil.showBlame(pluginWS.getProjectManager().getSelectedFiles()[0], historyCtrl, getViewsToSetCursorOn()));
        }
    };
}
Also used : GitOperationScheduler(com.oxygenxml.git.service.GitOperationScheduler) ActionEvent(java.awt.event.ActionEvent) AbstractAction(javax.swing.AbstractAction)

Example 3 with GitOperationScheduler

use of com.oxygenxml.git.service.GitOperationScheduler in project oxygen-git-client-addon by oxygenxml.

the class HistoryContextualActionsTest method testCheckoutFileAction.

/**
 * <p>
 * <b>Description:</b> Tests the
 * com.oxygenxml.git.view.history.HistoryViewContextualMenuPresenter.createCheckoutFileAction(
 * String, FileStatus, boolean) API.
 * </p>
 *
 * <p>
 * <b>Bug ID:</b> EXM-46986
 * </p>
 *
 * @author Alex_Smarandache
 *
 * @throws Exception
 */
@Test
public void testCheckoutFileAction() throws Exception {
    URL script = getClass().getClassLoader().getResource("scripts/history_script_actions.txt");
    File wcTree = new File("target/gen/GitHistoryActionsTest_testHistoryContextualActions");
    RepoGenerationScript.generateRepository(script, wcTree);
    GitAccess.getInstance().setRepositorySynchronously(wcTree.getAbsolutePath());
    List<CommitCharacteristics> commitsCharacteristics = GitAccess.getInstance().getCommitsCharacteristics(HistoryStrategy.CURRENT_BRANCH, null, null);
    Iterator<CommitCharacteristics> iterator = commitsCharacteristics.iterator();
    CommitCharacteristics commitCharacteristic = iterator.next();
    HistoryViewContextualMenuPresenter presenter = new HistoryViewContextualMenuPresenter(null);
    List<FileStatus> changedFiles = RevCommitUtil.getChangedFiles(commitCharacteristic.getCommitId());
    List<Action> actions = presenter.getFileContextualActions(changedFiles.get(0), commitCharacteristic, true);
    actions.removeIf(e -> e == null || !e.getValue(Action.NAME).toString().contains("Reset_file"));
    String[] checkoutFile = new String[2];
    try (MockedStatic<GitAccess> git = Mockito.mockStatic(GitAccess.class)) {
        GitAccess gitAcc = Mockito.mock(GitAccess.class);
        Mockito.doAnswer((Answer<Void>) invocation -> {
            checkoutFile[0] = invocation.getArgument(0);
            checkoutFile[1] = invocation.getArgument(1);
            return null;
        }).when(gitAcc).checkoutCommitForFile(Mockito.any(String.class), Mockito.any(String.class));
        git.when(GitAccess::getInstance).thenReturn(gitAcc);
        try (MockedStatic<GitOperationScheduler> sch = Mockito.mockStatic(GitOperationScheduler.class)) {
            GitOperationScheduler scheduler = Mockito.mock(GitOperationScheduler.class);
            Mockito.when(scheduler.schedule(Mockito.any(Runnable.class))).thenAnswer((Answer<ScheduledFuture<?>>) invocation -> {
                ((Runnable) invocation.getArgument(0)).run();
                return null;
            });
            sch.when(GitOperationScheduler::getInstance).thenReturn(scheduler);
            actions.get(0).actionPerformed(null);
        }
    }
    assertEquals(checkoutFile[0], changedFiles.get(0).getFileLocation());
    assertEquals(checkoutFile[1], commitCharacteristic.getCommitId());
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) PluginWorkspace(ro.sync.exml.workspace.api.PluginWorkspace) URL(java.net.URL) Action(javax.swing.Action) GitTestBase(com.oxygenxml.git.service.GitTestBase) FileStatus(com.oxygenxml.git.service.entities.FileStatus) Answer(org.mockito.stubbing.Answer) GitAccess(com.oxygenxml.git.service.GitAccess) PrintWriter(java.io.PrintWriter) RepoGenerationScript(com.oxygenxml.git.utils.script.RepoGenerationScript) Iterator(java.util.Iterator) Test(org.junit.Test) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Mockito(org.mockito.Mockito) List(java.util.List) MockedStatic(org.mockito.MockedStatic) GitOperationScheduler(com.oxygenxml.git.service.GitOperationScheduler) PluginWorkspaceProvider(ro.sync.exml.workspace.api.PluginWorkspaceProvider) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Git(org.eclipse.jgit.api.Git) GitChangeType(com.oxygenxml.git.service.entities.GitChangeType) RevCommitUtil(com.oxygenxml.git.service.RevCommitUtil) Action(javax.swing.Action) FileStatus(com.oxygenxml.git.service.entities.FileStatus) GitAccess(com.oxygenxml.git.service.GitAccess) URL(java.net.URL) ScheduledFuture(java.util.concurrent.ScheduledFuture) GitOperationScheduler(com.oxygenxml.git.service.GitOperationScheduler) File(java.io.File) Test(org.junit.Test)

Aggregations

GitOperationScheduler (com.oxygenxml.git.service.GitOperationScheduler)3 ActionEvent (java.awt.event.ActionEvent)2 File (java.io.File)2 AbstractAction (javax.swing.AbstractAction)2 GitAccess (com.oxygenxml.git.service.GitAccess)1 GitTestBase (com.oxygenxml.git.service.GitTestBase)1 RevCommitUtil (com.oxygenxml.git.service.RevCommitUtil)1 FileStatus (com.oxygenxml.git.service.entities.FileStatus)1 GitChangeType (com.oxygenxml.git.service.entities.GitChangeType)1 RepoGenerationScript (com.oxygenxml.git.utils.script.RepoGenerationScript)1 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 PrintWriter (java.io.PrintWriter)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 Collectors (java.util.stream.Collectors)1