Search in sources :

Example 6 with NoRepositorySelected

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

the class StagingPanel method treatEditorSavedEvent.

/**
 * Treat editor saved event.
 *
 * @param editorLocation Editor URL.
 */
private void treatEditorSavedEvent(final URL editorLocation) {
    File locateFile = null;
    if ("file".equals(editorLocation.getProtocol())) {
        locateFile = PluginWorkspaceProvider.getPluginWorkspace().getUtilAccess().locateFile(editorLocation);
        if (locateFile != null) {
            String fileInWorkPath = locateFile.toString();
            fileInWorkPath = FileUtil.rewriteSeparator(fileInWorkPath);
            try {
                String selectedRepositoryPath = GitAccess.getInstance().getWorkingCopy().getAbsolutePath();
                selectedRepositoryPath = FileUtil.rewriteSeparator(selectedRepositoryPath);
                if (fileInWorkPath.startsWith(selectedRepositoryPath)) {
                    if (gitActionsManager != null) {
                        gitActionsManager.refreshActionsStates();
                    }
                    updateToolbarsButtonsStates();
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Notify " + fileInWorkPath);
                        LOGGER.debug("WC " + selectedRepositoryPath);
                    }
                    Collection<String> affectedFiles = Collections.singletonList(fileInWorkPath.substring(selectedRepositoryPath.length() + 1));
                    FileGitEventInfo changeEvent = new FileGitEventInfo(GitOperation.UNSTAGE, affectedFiles);
                    SwingUtilities.invokeLater(() -> unstagedChangesPanel.fileStatesChanged(changeEvent));
                }
            } catch (NoRepositorySelected e) {
                LOGGER.debug(e.getMessage(), e);
            }
        }
    }
}
Also used : NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) FileGitEventInfo(com.oxygenxml.git.view.event.FileGitEventInfo) File(java.io.File)

Example 7 with NoRepositorySelected

use of com.oxygenxml.git.service.NoRepositorySelected 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 8 with NoRepositorySelected

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

the class TagsDialog method createTableComponentMenu.

/**
 * Create the popup menu for the tagsTable
 *
 * @return a JPopUpMenu for the tags table
 */
private JPopupMenu createTableComponentMenu() {
    JPopupMenu contextualActions = new JPopupMenu();
    JMenuItem menuItemDetails = new JMenuItem(TRANSLATOR.getTranslation(Tags.TAGS_DIALOG_POPUP_MENU_DETAILS));
    menuItemDetails.addActionListener(e -> {
        try {
            int selectedRow = tagsTable.getSelectedRow();
            TagsTableModel model = (TagsTableModel) tagsTable.getModel();
            GitTag tag = model.getItemAt(selectedRow);
            TagDetailsDialog dialog = new TagDetailsDialog(tag);
            dialog.setVisible(true);
        } catch (NoRepositorySelected | IOException ex) {
            PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(ex.getMessage(), ex);
        }
    });
    contextualActions.add(menuItemDetails);
    contextualActions.addPopupMenuListener(new PopupMenuListener() {

        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            SwingUtilities.invokeLater(() -> {
                int rowAtPoint = tagsTable.rowAtPoint(SwingUtilities.convertPoint(contextualActions, new Point(0, 0), tagsTable));
                if (rowAtPoint > -1) {
                    tagsTable.setRowSelectionInterval(rowAtPoint, rowAtPoint);
                }
            });
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        /*//Not necessary to be implemented*/
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
        /*//Not necessary to be implemented*/
        }
    });
    return contextualActions;
}
Also used : PopupMenuListener(javax.swing.event.PopupMenuListener) IOException(java.io.IOException) Point(java.awt.Point) PopupMenuEvent(javax.swing.event.PopupMenuEvent) JPopupMenu(javax.swing.JPopupMenu) Point(java.awt.Point) NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) JMenuItem(javax.swing.JMenuItem)

Example 9 with NoRepositorySelected

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

the class HistoryViewContextualMenuPresenter method getFileURL.

/**
 * Builds an URL that identifies a file at a specific revision.
 *
 * @param revisionID Revision ID.
 * @param fileStatus FIle info.
 *
 * @return The URL, if one was built.
 *
 * @throws NoRepositorySelected No repository is loaded.
 * @throws IOException Problems identifying the revision.
 */
private Optional<URL> getFileURL(String revisionID, FileStatus fileStatus) throws NoRepositorySelected, IOException {
    URL fileURL = null;
    String fileStatusLocation = fileStatus.getFileLocation();
    if (fileStatus.getChangeType() == GitChangeType.REMOVED) {
        Repository repository = GitAccess.getInstance().getRepository();
        RevCommit[] parentsRevCommits = RevCommitUtil.getParents(repository, revisionID);
        // If it's a merge, we look for the one parent with the actual file in it.
        Optional<RevCommit> previousVersionCommit = Arrays.stream(parentsRevCommits).filter(revCommit -> {
            try {
                return RevCommitUtil.getObjectID(repository, revCommit.getId().getName(), fileStatusLocation) != null;
            } catch (IOException e1) {
                // Unable to find a parent with the given path.
                PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage("Unable to open file because of " + e1.getMessage());
            }
            return false;
        }).findFirst();
        if (previousVersionCommit.isPresent()) {
            fileURL = GitRevisionURLHandler.encodeURL(previousVersionCommit.get().getId().getName(), fileStatusLocation);
        }
    } else if (fileStatus.getChangeType() == GitChangeType.MISSING) {
        fileURL = GitRevisionURLHandler.encodeURL(VersionIdentifier.INDEX_OR_LAST_COMMIT, fileStatusLocation);
    } else if (!GitAccess.UNCOMMITED_CHANGES.getCommitId().equals(revisionID)) {
        fileURL = GitRevisionURLHandler.encodeURL(revisionID, fileStatusLocation);
    } else {
        fileURL = FileUtil.getFileURL(fileStatusLocation);
    }
    return Optional.ofNullable(fileURL);
}
Also used : CreateBranchFromCommitAction(com.oxygenxml.git.view.history.actions.CreateBranchFromCommitAction) EditorPageConstants(ro.sync.exml.editor.EditorPageConstants) Arrays(java.util.Arrays) RevCommit(org.eclipse.jgit.revwalk.RevCommit) VersionIdentifier(com.oxygenxml.git.protocol.VersionIdentifier) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) Action(javax.swing.Action) ResetBranchToCommitAction(com.oxygenxml.git.view.history.actions.ResetBranchToCommitAction) FileStatus(com.oxygenxml.git.service.entities.FileStatus) MessageFormat(java.text.MessageFormat) DiffPresenter(com.oxygenxml.git.view.DiffPresenter) Translator(com.oxygenxml.git.translator.Translator) GitAccess(com.oxygenxml.git.service.GitAccess) FileStatusOverDiffEntry(com.oxygenxml.git.service.entities.FileStatusOverDiffEntry) RevertCommitAction(com.oxygenxml.git.view.history.actions.RevertCommitAction) JMenuItem(javax.swing.JMenuItem) Tags(com.oxygenxml.git.translator.Tags) LinkedList(java.util.LinkedList) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) JPopupMenu(javax.swing.JPopupMenu) NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) IOException(java.io.IOException) RevCommitUtilBase(com.oxygenxml.git.service.RevCommitUtilBase) ActionEvent(java.awt.event.ActionEvent) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) GitOperationScheduler(com.oxygenxml.git.service.GitOperationScheduler) AbstractAction(javax.swing.AbstractAction) GitRevisionURLHandler(com.oxygenxml.git.protocol.GitRevisionURLHandler) PluginWorkspaceProvider(ro.sync.exml.workspace.api.PluginWorkspaceProvider) CreateTagAction(com.oxygenxml.git.view.history.actions.CreateTagAction) CheckoutCommitAction(com.oxygenxml.git.view.history.actions.CheckoutCommitAction) Optional(java.util.Optional) GitControllerBase(com.oxygenxml.git.service.GitControllerBase) FileUtil(com.oxygenxml.git.utils.FileUtil) GitChangeType(com.oxygenxml.git.service.entities.GitChangeType) Repository(org.eclipse.jgit.lib.Repository) RevCommitUtil(com.oxygenxml.git.service.RevCommitUtil) Repository(org.eclipse.jgit.lib.Repository) IOException(java.io.IOException) URL(java.net.URL) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 10 with NoRepositorySelected

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

the class CreateTagAction method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    CreateTagDialog dialog = new CreateTagDialog();
    String tagTitle = dialog.getTagTitle();
    String tagMessage = dialog.getTagMessage();
    if (dialog.getResult() == OKCancelDialog.RESULT_OK) {
        GitOperationScheduler.getInstance().schedule(() -> {
            try {
                GitAccess.getInstance().tagCommit(tagTitle, tagMessage, commitId);
                if (dialog.shouldPushNewTag()) {
                    GitAccess.getInstance().pushTag(tagTitle);
                }
            } catch (GitAPIException | RevisionSyntaxException | NoRepositorySelected | IOException ex) {
                LOGGER.debug(ex.getMessage(), ex);
                PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(ex.getMessage(), ex);
            }
        });
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) NoRepositorySelected(com.oxygenxml.git.service.NoRepositorySelected) CreateTagDialog(com.oxygenxml.git.view.tags.CreateTagDialog) RevisionSyntaxException(org.eclipse.jgit.errors.RevisionSyntaxException) IOException(java.io.IOException)

Aggregations

NoRepositorySelected (com.oxygenxml.git.service.NoRepositorySelected)28 IOException (java.io.IOException)15 File (java.io.File)10 URL (java.net.URL)8 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)8 Repository (org.eclipse.jgit.lib.Repository)6 FileStatus (com.oxygenxml.git.service.entities.FileStatus)4 MalformedURLException (java.net.MalformedURLException)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 RepoNotInitializedException (com.oxygenxml.git.service.RepoNotInitializedException)3 ActionEvent (java.awt.event.ActionEvent)3 AbstractAction (javax.swing.AbstractAction)3 BranchInfo (com.oxygenxml.git.service.BranchInfo)2 GitChangeType (com.oxygenxml.git.service.entities.GitChangeType)2 Translator (com.oxygenxml.git.translator.Translator)2 CheckoutCommitAction (com.oxygenxml.git.view.history.actions.CheckoutCommitAction)2 CreateBranchFromCommitAction (com.oxygenxml.git.view.history.actions.CreateBranchFromCommitAction)2 CreateTagAction (com.oxygenxml.git.view.history.actions.CreateTagAction)2 ResetBranchToCommitAction (com.oxygenxml.git.view.history.actions.ResetBranchToCommitAction)2 RevertCommitAction (com.oxygenxml.git.view.history.actions.RevertCommitAction)2