Search in sources :

Example 1 with BranchInfo

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

the class ToolbarPanel method updateButtonsStates.

/**
 * Updates the presented information, like the Pull-behind, Pushes-ahead
 * and branch status.
 */
public void updateButtonsStates() {
    this.pullsBehind = GIT_ACCESS.getPullsBehind();
    try {
        this.pushesAhead = GIT_ACCESS.getPushesAhead();
    } catch (RepoNotInitializedException e) {
        this.pushesAhead = -1;
        LOGGER.debug(e.getMessage(), e);
    }
    Repository repo = null;
    try {
        repo = GIT_ACCESS.getRepository();
    } catch (NoRepositorySelected e) {
        LOGGER.debug(e.getMessage(), e);
    }
    final Collection<RevCommit> stashes = GIT_ACCESS.listStashes();
    if (stashes != null) {
        noOfStashes = stashes.size();
    }
    final boolean isPullMenuEnabled = isPullButtonEnabled();
    final boolean isStashButtonEnabled = isStashButtonEnabled();
    SwingUtilities.invokeLater(() -> {
        /* for @pushButton, @historyButton, @showBranchesButton ---> 
       * it is necessary to refresh, even if it has a single action, 
       * because there are other code sequences that directly change their state by calling the setEnabled(false) method
       */
        pushButton.setEnabled(gitActionsManager.getPushAction().isEnabled());
        pullMenuButton.setEnabled(isPullMenuEnabled);
        stashButton.setEnabled(isStashButtonEnabled);
        historyButton.setEnabled(gitActionsManager.getShowHistoryAction().isEnabled());
        showBranchesButton.setEnabled(gitActionsManager.getShowBranchesAction().isEnabled());
        pullMenuButton.repaint();
        pushButton.repaint();
        stashButton.repaint();
    });
    BranchInfo branchInfo = GIT_ACCESS.getBranchInfo();
    String currentBranchName = branchInfo.getBranchName();
    if (branchInfo.isDetached()) {
        SwingUtilities.invokeLater(() -> {
            pushButton.setToolTipText(TRANSLATOR.getTranslation(Tags.PUSH_BUTTON_TOOLTIP));
            pullMenuButton.setToolTipText(TRANSLATOR.getTranslation(Tags.PULL_BUTTON_TOOLTIP));
        });
        String tooltipText = TRANSLATOR.getTranslation(Tags.TOOLBAR_PANEL_INFORMATION_STATUS_DETACHED_HEAD) + " " + currentBranchName;
        if (repo != null && repo.getRepositoryState() == RepositoryState.REBASING_MERGE) {
            tooltipText += "<br>" + TRANSLATOR.getTranslation(Tags.REBASE_IN_PROGRESS) + ".";
        }
        tooltipText = TextFormatUtil.toHTML(tooltipText);
    } else {
        if (currentBranchName != null && !currentBranchName.isEmpty()) {
            String upstreamBranchFromConfig = GIT_ACCESS.getUpstreamBranchShortNameFromConfig(currentBranchName);
            boolean isAnUpstreamBranchDefinedInConfig = upstreamBranchFromConfig != null;
            String upstreamShortestName = isAnUpstreamBranchDefinedInConfig ? upstreamBranchFromConfig.substring(upstreamBranchFromConfig.lastIndexOf('/') + 1) : null;
            Ref remoteBranchRefForUpstreamFromConfig = isAnUpstreamBranchDefinedInConfig ? RepoUtil.getRemoteBranch(upstreamShortestName) : null;
            boolean existsRemoteBranchForUpstreamDefinedInConfig = remoteBranchRefForUpstreamFromConfig != null;
            String commitsBehindMessage = "";
            String commitsAheadMessage = "";
            if (isAnUpstreamBranchDefinedInConfig && existsRemoteBranchForUpstreamDefinedInConfig) {
                if (pullsBehind == 0) {
                    commitsBehindMessage = TRANSLATOR.getTranslation(Tags.TOOLBAR_PANEL_INFORMATION_STATUS_UP_TO_DATE);
                } else if (pullsBehind == 1) {
                    commitsBehindMessage = TRANSLATOR.getTranslation(Tags.ONE_COMMIT_BEHIND);
                } else {
                    commitsBehindMessage = MessageFormat.format(TRANSLATOR.getTranslation(Tags.COMMITS_BEHIND), pullsBehind);
                }
                if (pushesAhead == 0) {
                    commitsAheadMessage = TRANSLATOR.getTranslation(Tags.NOTHING_TO_PUSH);
                } else if (pushesAhead == 1) {
                    commitsAheadMessage = TRANSLATOR.getTranslation(Tags.ONE_COMMIT_AHEAD);
                } else {
                    commitsAheadMessage = MessageFormat.format(TRANSLATOR.getTranslation(Tags.COMMITS_AHEAD), pushesAhead);
                }
            }
            // ===================== Push button tooltip =====================
            String pushButtonTooltipFinal = updatePushToolTip(isAnUpstreamBranchDefinedInConfig, existsRemoteBranchForUpstreamDefinedInConfig, upstreamBranchFromConfig, commitsAheadMessage, currentBranchName, repo);
            SwingUtilities.invokeLater(() -> pushButton.setToolTipText(pushButtonTooltipFinal));
            // ===================== Pull button tooltip =====================
            String pullButtonTooltipFinal = updatePullToolTip(isAnUpstreamBranchDefinedInConfig, existsRemoteBranchForUpstreamDefinedInConfig, upstreamBranchFromConfig, commitsBehindMessage, remoteBranchRefForUpstreamFromConfig, repo);
            SwingUtilities.invokeLater(() -> pullMenuButton.setToolTipText(pullButtonTooltipFinal));
        }
    }
}
Also used : NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) BranchInfo(com.oxygenxml.git.service.BranchInfo) RepoNotInitializedException(com.oxygenxml.git.service.RepoNotInitializedException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with BranchInfo

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

the class BranchSelectionCombo method refresh.

/**
 * Refresh.
 */
public void refresh() {
    int pullsBehind = GIT_ACCESS.getPullsBehind();
    int pushesAhead = -1;
    try {
        pushesAhead = GIT_ACCESS.getPushesAhead();
    } catch (RepoNotInitializedException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    // update pop up
    updateBranchesPopup();
    Repository repo = null;
    try {
        repo = GIT_ACCESS.getRepository();
    } catch (NoRepositorySelected e) {
        LOGGER.debug(e.getMessage(), e);
    }
    this.setEnabled(repo != null);
    final BranchInfo branchInfo = GIT_ACCESS.getBranchInfo();
    final String currentBranchName = branchInfo.getBranchName();
    if (branchInfo.isDetached()) {
        detachedHeadId = currentBranchName;
        String tooltipText = TRANSLATOR.getTranslation(Tags.TOOLBAR_PANEL_INFORMATION_STATUS_DETACHED_HEAD) + " " + currentBranchName;
        if (repo != null && repo.getRepositoryState() == RepositoryState.REBASING_MERGE) {
            tooltipText += "<br>" + TRANSLATOR.getTranslation(Tags.REBASE_IN_PROGRESS) + ".";
        }
        tooltipText = TextFormatUtil.toHTML(tooltipText);
        String finalText = tooltipText;
        SwingUtilities.invokeLater(() -> this.setToolTipText(finalText));
    } else {
        detachedHeadId = null;
        String branchTooltip = null;
        if (currentBranchName != null && !currentBranchName.isEmpty()) {
            branchTooltip = getBranchTooltip(pullsBehind, pushesAhead, currentBranchName);
        }
        String branchTooltipFinal = branchTooltip;
        this.setToolTipText(branchTooltipFinal);
    }
}
Also used : NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) Repository(org.eclipse.jgit.lib.Repository) BranchInfo(com.oxygenxml.git.service.BranchInfo) RepoNotInitializedException(com.oxygenxml.git.service.RepoNotInitializedException)

Example 3 with BranchInfo

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

the class BranchSelectionCombo method treatBranchSelectedEvent.

/**
 * Treat a branch name selection event.
 *
 * @param event The event to treat.
 */
private void treatBranchSelectedEvent(final ItemEvent event) {
    final String branchName = (String) event.getItem();
    final BranchInfo currentBranchInfo = GIT_ACCESS.getBranchInfo();
    final String currentBranchName = currentBranchInfo.getBranchName();
    if (branchName.equals(currentBranchName)) {
        return;
    }
    final RepositoryState repoState = RepoUtil.getRepoState().orElse(null);
    if (RepoUtil.isNonConflictualRepoWithUncommittedChanges(repoState)) {
        SwingUtilities.invokeLater(() -> {
            BranchSwitchConfirmationDialog dialog = new BranchSwitchConfirmationDialog(branchName);
            dialog.setVisible(true);
            int answer = dialog.getResult();
            if (answer == OKOtherAndCancelDialog.RESULT_OTHER) {
                tryCheckingOutBranch(currentBranchInfo, branchName);
            } else if (answer == OKOtherAndCancelDialog.RESULT_OK) {
                boolean wasStashCreated = StashUtil.stashChanges();
                if (wasStashCreated) {
                    tryCheckingOutBranch(currentBranchInfo, branchName);
                }
            } else {
                restoreCurrentBranchSelectionInMenu();
            }
        });
    } else {
        tryCheckingOutBranch(currentBranchInfo, branchName);
    }
}
Also used : BranchSwitchConfirmationDialog(com.oxygenxml.git.view.dialog.BranchSwitchConfirmationDialog) BranchInfo(com.oxygenxml.git.service.BranchInfo) RepositoryState(org.eclipse.jgit.lib.RepositoryState)

Example 4 with BranchInfo

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

the class GitEditorVariables2Test method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    noOfShortBranchCalls = 0;
    noOfFullBranchCalls = 0;
    noOfWCCalls = 0;
    Repository repo = createRepository(LOCAL_TEST_REPOSITORY);
    GitAccess.getInstance().setRepositorySynchronously(LOCAL_TEST_REPOSITORY);
    GitAccess gitAccessMock = Mockito.mock(GitAccess.class);
    Mockito.doAnswer(new Answer<BranchInfo>() {

        @Override
        public BranchInfo answer(InvocationOnMock invocation) throws Throwable {
            noOfShortBranchCalls++;
            return new BranchInfo("main", false);
        }
    }).when(gitAccessMock).getBranchInfo();
    Mockito.doAnswer(new Answer<Repository>() {

        @Override
        public Repository answer(InvocationOnMock invocation) throws Throwable {
            noOfFullBranchCalls++;
            return repo;
        }
    }).when(gitAccessMock).getRepository();
    Mockito.doAnswer(new Answer<File>() {

        @Override
        public File answer(InvocationOnMock invocation) throws Throwable {
            noOfWCCalls++;
            return new File(LOCAL_TEST_REPOSITORY);
        }
    }).when(gitAccessMock).getWorkingCopy();
    editorVariablesResolver = new GitEditorVariablesResolver(new GitController(gitAccessMock));
}
Also used : Repository(org.eclipse.jgit.lib.Repository) GitAccess(com.oxygenxml.git.service.GitAccess) BranchInfo(com.oxygenxml.git.service.BranchInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GitController(com.oxygenxml.git.view.event.GitController) File(java.io.File)

Aggregations

BranchInfo (com.oxygenxml.git.service.BranchInfo)4 Repository (org.eclipse.jgit.lib.Repository)3 NoRepositorySelected (com.oxygenxml.git.service.NoRepositorySelected)2 RepoNotInitializedException (com.oxygenxml.git.service.RepoNotInitializedException)2 GitAccess (com.oxygenxml.git.service.GitAccess)1 BranchSwitchConfirmationDialog (com.oxygenxml.git.view.dialog.BranchSwitchConfirmationDialog)1 GitController (com.oxygenxml.git.view.event.GitController)1 File (java.io.File)1 Ref (org.eclipse.jgit.lib.Ref)1 RepositoryState (org.eclipse.jgit.lib.RepositoryState)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1