Search in sources :

Example 1 with StashedCommitNode

use of org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode in project egit by eclipse.

the class StashDropCommand method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final List<StashedCommitNode> nodes = getSelectedNodes(event);
    if (nodes.isEmpty())
        return null;
    final Repository repo = nodes.get(0).getRepository();
    if (repo == null)
        return null;
    // Confirm deletion of selected nodes
    final AtomicBoolean confirmed = new AtomicBoolean();
    final Shell shell = getActiveShell(event);
    shell.getDisplay().syncExec(new Runnable() {

        @Override
        public void run() {
            String message;
            if (nodes.size() > 1)
                message = MessageFormat.format(UIText.StashDropCommand_confirmMultiple, Integer.toString(nodes.size()));
            else
                message = MessageFormat.format(UIText.StashDropCommand_confirmSingle, Integer.toString(nodes.get(0).getIndex()));
            confirmed.set(MessageDialog.openConfirm(shell, UIText.StashDropCommand_confirmTitle, message));
        }
    });
    if (!confirmed.get())
        return null;
    Job job = new Job(UIText.StashDropCommand_jobTitle) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            monitor.beginTask(UIText.StashDropCommand_jobTitle, nodes.size());
            // Sort by highest to lowest stash commit index.
            // This avoids shifting problems that cause the indices of the
            // selected nodes not match the indices in the repository
            Collections.sort(nodes, new Comparator<StashedCommitNode>() {

                @Override
                public int compare(StashedCommitNode n1, StashedCommitNode n2) {
                    return n1.getIndex() < n2.getIndex() ? 1 : -1;
                }
            });
            for (StashedCommitNode node : nodes) {
                final int index = node.getIndex();
                if (index < 0)
                    return null;
                final RevCommit commit = node.getObject();
                if (commit == null)
                    return null;
                final String stashName = node.getObject().getName();
                final StashDropOperation op = new StashDropOperation(repo, node.getIndex());
                monitor.subTask(stashName);
                try {
                    op.execute(monitor);
                } catch (CoreException e) {
                    Activator.logError(MessageFormat.format(UIText.StashDropCommand_dropFailed, node.getObject().name()), e);
                }
                tryToCloseEditor(node);
                monitor.worked(1);
            }
            monitor.done();
            return Status.OK_STATUS;
        }

        private void tryToCloseEditor(final StashedCommitNode node) {
            Display.getDefault().asyncExec(new Runnable() {

                @Override
                public void run() {
                    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                    IEditorReference[] editorReferences = activePage.getEditorReferences();
                    for (IEditorReference editorReference : editorReferences) {
                        IEditorInput editorInput = null;
                        try {
                            editorInput = editorReference.getEditorInput();
                        } catch (PartInitException e) {
                            Activator.handleError(e.getMessage(), e, true);
                        }
                        if (editorInput instanceof CommitEditorInput) {
                            CommitEditorInput comEditorInput = (CommitEditorInput) editorInput;
                            if (comEditorInput.getCommit().getRevCommit().equals(node.getObject())) {
                                activePage.closeEditor(editorReference.getEditor(false), false);
                            }
                        }
                    }
                }
            });
        }

        @Override
        public boolean belongsTo(Object family) {
            if (JobFamilies.STASH.equals(family))
                return true;
            return super.belongsTo(family);
        }
    };
    job.setUser(true);
    job.setRule((new StashDropOperation(repo, nodes.get(0).getIndex())).getSchedulingRule());
    job.schedule();
    return null;
}
Also used : CommitEditorInput(org.eclipse.egit.ui.internal.commit.CommitEditorInput) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Repository(org.eclipse.jgit.lib.Repository) Shell(org.eclipse.swt.widgets.Shell) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IEditorReference(org.eclipse.ui.IEditorReference) CoreException(org.eclipse.core.runtime.CoreException) StashDropOperation(org.eclipse.egit.core.op.StashDropOperation) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) StashedCommitNode(org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode) PartInitException(org.eclipse.ui.PartInitException) Job(org.eclipse.core.runtime.jobs.Job) IEditorInput(org.eclipse.ui.IEditorInput) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with StashedCommitNode

use of org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode in project egit by eclipse.

the class OpenCommand method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final List<RepositoryTreeNode> nodes = getSelectedNodes(event);
    if (nodes.isEmpty())
        return null;
    final RepositoryTreeNode node = nodes.get(0);
    if (node instanceof RefNode || node instanceof TagNode)
        return new CheckoutCommand().execute(event);
    if (node instanceof FileNode)
        return new OpenInEditorCommand().execute(event);
    if (node instanceof StashedCommitNode) {
        RepositoryCommit repositoryCommit = new RepositoryCommit(node.getRepository(), ((StashedCommitNode) node).getObject());
        repositoryCommit.setStash(true);
        CommitEditor.openQuiet(repositoryCommit);
    }
    return null;
}
Also used : RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) StashedCommitNode(org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode) RepositoryCommit(org.eclipse.egit.ui.internal.commit.RepositoryCommit) TagNode(org.eclipse.egit.ui.internal.repository.tree.TagNode) FileNode(org.eclipse.egit.ui.internal.repository.tree.FileNode) RefNode(org.eclipse.egit.ui.internal.repository.tree.RefNode)

Example 3 with StashedCommitNode

use of org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode in project egit by eclipse.

the class RepositoriesViewContentProvider method getChildren.

@Override
public Object[] getChildren(Object parentElement) {
    RepositoryTreeNode node = (RepositoryTreeNode) parentElement;
    Repository repo = node.getRepository();
    switch(node.getType()) {
        case BRANCHES:
            {
                List<RepositoryTreeNode> nodes = new ArrayList<>();
                nodes.add(new LocalNode(node, repo));
                nodes.add(new RemoteTrackingNode(node, repo));
                return nodes.toArray();
            }
        case LOCAL:
            {
                if (branchHierarchyMode) {
                    BranchHierarchyNode hierNode = new BranchHierarchyNode(node, repo, new Path(Constants.R_HEADS));
                    List<RepositoryTreeNode> children = new ArrayList<>();
                    try {
                        for (IPath path : hierNode.getChildPaths()) {
                            children.add(new BranchHierarchyNode(node, node.getRepository(), path));
                        }
                        for (Ref ref : hierNode.getChildRefs()) {
                            children.add(new RefNode(node, node.getRepository(), ref));
                        }
                    } catch (Exception e) {
                        return handleException(e, node);
                    }
                    return children.toArray();
                } else {
                    List<RepositoryTreeNode<Ref>> refs = new ArrayList<>();
                    try {
                        for (Entry<String, Ref> refEntry : getRefs(repo, Constants.R_HEADS).entrySet()) {
                            if (!refEntry.getValue().isSymbolic())
                                refs.add(new RefNode(node, repo, refEntry.getValue()));
                        }
                    } catch (Exception e) {
                        return handleException(e, node);
                    }
                    return refs.toArray();
                }
            }
        case REMOTETRACKING:
            {
                if (branchHierarchyMode) {
                    BranchHierarchyNode hierNode = new BranchHierarchyNode(node, repo, new Path(Constants.R_REMOTES));
                    List<RepositoryTreeNode> children = new ArrayList<>();
                    try {
                        for (IPath path : hierNode.getChildPaths()) {
                            children.add(new BranchHierarchyNode(node, node.getRepository(), path));
                        }
                        for (Ref ref : hierNode.getChildRefs()) {
                            children.add(new RefNode(node, node.getRepository(), ref));
                        }
                    } catch (Exception e) {
                        return handleException(e, node);
                    }
                    return children.toArray();
                } else {
                    List<RepositoryTreeNode<Ref>> refs = new ArrayList<>();
                    try {
                        for (Entry<String, Ref> refEntry : getRefs(repo, Constants.R_REMOTES).entrySet()) {
                            if (!refEntry.getValue().isSymbolic())
                                refs.add(new RefNode(node, repo, refEntry.getValue()));
                        }
                    } catch (Exception e) {
                        return handleException(e, node);
                    }
                    return refs.toArray();
                }
            }
        case BRANCHHIERARCHY:
            {
                BranchHierarchyNode hierNode = (BranchHierarchyNode) node;
                List<RepositoryTreeNode> children = new ArrayList<>();
                try {
                    for (IPath path : hierNode.getChildPaths()) {
                        children.add(new BranchHierarchyNode(node, node.getRepository(), path));
                    }
                    for (Ref ref : hierNode.getChildRefs()) {
                        children.add(new RefNode(node, node.getRepository(), ref));
                    }
                } catch (IOException e) {
                    return handleException(e, node);
                }
                return children.toArray();
            }
        case TAGS:
            {
                return getTagsChildren(node, repo);
            }
        case ADDITIONALREFS:
            {
                List<RepositoryTreeNode<Ref>> refs = new ArrayList<>();
                try {
                    for (Entry<String, Ref> refEntry : getRefs(repo, RefDatabase.ALL).entrySet()) {
                        String name = refEntry.getKey();
                        if (!(name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS) || name.startsWith(Constants.R_REMOTES)))
                            refs.add(new AdditionalRefNode(node, repo, refEntry.getValue()));
                    }
                    for (Ref r : repo.getRefDatabase().getAdditionalRefs()) refs.add(new AdditionalRefNode(node, repo, r));
                } catch (Exception e) {
                    return handleException(e, node);
                }
                return refs.toArray();
            }
        case REMOTES:
            {
                List<RepositoryTreeNode<String>> remotes = new ArrayList<>();
                Repository rep = node.getRepository();
                Set<String> configNames = rep.getConfig().getSubsections(RepositoriesView.REMOTE);
                for (String configName : configNames) {
                    remotes.add(new RemoteNode(node, repo, configName));
                }
                return remotes.toArray();
            }
        case REPO:
            {
                List<RepositoryTreeNode<? extends Object>> nodeList = new ArrayList<>();
                nodeList.add(new BranchesNode(node, repo));
                nodeList.add(new TagsNode(node, repo));
                nodeList.add(new AdditionalRefsNode(node, repo));
                final boolean bare = repo.isBare();
                if (!bare)
                    nodeList.add(new WorkingDirNode(node, repo));
                nodeList.add(new RemotesNode(node, repo));
                if (!bare && hasStashedCommits(repo))
                    nodeList.add(new StashNode(node, repo));
                if (!bare && hasConfiguredSubmodules(repo))
                    nodeList.add(new SubmodulesNode(node, repo));
                return nodeList.toArray();
            }
        case WORKINGDIR:
            {
                List<RepositoryTreeNode<File>> children = new ArrayList<>();
                if (node.getRepository().isBare())
                    return children.toArray();
                File workingDir = repo.getWorkTree();
                if (!workingDir.exists())
                    return children.toArray();
                File[] childFiles = workingDir.listFiles();
                Arrays.sort(childFiles, new Comparator<File>() {

                    @Override
                    public int compare(File o1, File o2) {
                        if (o1.isDirectory()) {
                            if (o2.isDirectory()) {
                                return o1.compareTo(o2);
                            }
                            return -1;
                        } else if (o2.isDirectory()) {
                            return 1;
                        }
                        return o1.compareTo(o2);
                    }
                });
                for (File file : childFiles) {
                    if (file.isDirectory()) {
                        children.add(new FolderNode(node, repo, file));
                    } else {
                        children.add(new FileNode(node, repo, file));
                    }
                }
                return children.toArray();
            }
        case FOLDER:
            {
                List<RepositoryTreeNode<File>> children = new ArrayList<>();
                File parent = ((File) node.getObject());
                File[] childFiles = parent.listFiles();
                if (childFiles == null)
                    return children.toArray();
                Arrays.sort(childFiles, new Comparator<File>() {

                    @Override
                    public int compare(File o1, File o2) {
                        if (o1.isDirectory()) {
                            if (o2.isDirectory()) {
                                return o1.compareTo(o2);
                            }
                            return -1;
                        } else if (o2.isDirectory()) {
                            return 1;
                        }
                        return o1.compareTo(o2);
                    }
                });
                for (File file : childFiles) {
                    if (file.isDirectory()) {
                        children.add(new FolderNode(node, repo, file));
                    } else {
                        children.add(new FileNode(node, repo, file));
                    }
                }
                return children.toArray();
            }
        case REMOTE:
            {
                List<RepositoryTreeNode<String>> children = new ArrayList<>();
                String remoteName = (String) node.getObject();
                RemoteConfig rc;
                try {
                    rc = new RemoteConfig(node.getRepository().getConfig(), remoteName);
                } catch (URISyntaxException e) {
                    return handleException(e, node);
                }
                if (!rc.getURIs().isEmpty())
                    children.add(new FetchNode(node, node.getRepository(), rc.getURIs().get(0).toPrivateString()));
                int uriCount = rc.getPushURIs().size();
                if (uriCount == 0 && !rc.getURIs().isEmpty())
                    uriCount++;
                // at least one push specification
                if (uriCount > 0) {
                    URIish firstUri;
                    if (!rc.getPushURIs().isEmpty())
                        firstUri = rc.getPushURIs().get(0);
                    else
                        firstUri = rc.getURIs().get(0);
                    if (uriCount == 1)
                        children.add(new PushNode(node, node.getRepository(), firstUri.toPrivateString()));
                    else
                        children.add(new PushNode(node, node.getRepository(), // $NON-NLS-1$
                        firstUri.toPrivateString() + "..."));
                }
                return children.toArray();
            }
        case SUBMODULES:
            List<RepositoryNode> children = new ArrayList<>();
            try {
                SubmoduleWalk walk = SubmoduleWalk.forIndex(node.getRepository());
                while (walk.next()) {
                    Repository subRepo = walk.getRepository();
                    if (subRepo != null) {
                        Repository cachedRepo = null;
                        try {
                            cachedRepo = repositoryCache.lookupRepository(subRepo.getDirectory());
                        } finally {
                            subRepo.close();
                        }
                        if (cachedRepo != null)
                            children.add(new RepositoryNode(node, cachedRepo));
                    }
                }
            } catch (IOException e) {
                handleException(e, node);
            }
            return children.toArray();
        case STASH:
            List<StashedCommitNode> stashNodes = new ArrayList<>();
            int index = 0;
            try {
                for (RevCommit commit : Git.wrap(repo).stashList().call()) stashNodes.add(new StashedCommitNode(node, repo, index++, commit));
            } catch (Exception e) {
                handleException(e, node);
            }
            return stashNodes.toArray();
        case FILE:
        // fall through
        case REF:
        // fall through
        case PUSH:
        // fall through
        case TAG:
        // fall through
        case FETCH:
        // fall through
        case ERROR:
        // fall through
        case STASHED_COMMIT:
        // fall through
        case ADDITIONALREF:
            return null;
    }
    return null;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Set(java.util.Set) FolderNode(org.eclipse.egit.ui.internal.repository.tree.FolderNode) TagsNode(org.eclipse.egit.ui.internal.repository.tree.TagsNode) ArrayList(java.util.ArrayList) RemotesNode(org.eclipse.egit.ui.internal.repository.tree.RemotesNode) URISyntaxException(java.net.URISyntaxException) RefNode(org.eclipse.egit.ui.internal.repository.tree.RefNode) AdditionalRefNode(org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode) Comparator(java.util.Comparator) Entry(java.util.Map.Entry) FetchNode(org.eclipse.egit.ui.internal.repository.tree.FetchNode) SubmodulesNode(org.eclipse.egit.ui.internal.repository.tree.SubmodulesNode) List(java.util.List) ArrayList(java.util.ArrayList) WorkingDirNode(org.eclipse.egit.ui.internal.repository.tree.WorkingDirNode) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) BranchesNode(org.eclipse.egit.ui.internal.repository.tree.BranchesNode) AdditionalRefsNode(org.eclipse.egit.ui.internal.repository.tree.AdditionalRefsNode) RevCommit(org.eclipse.jgit.revwalk.RevCommit) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) StashNode(org.eclipse.egit.ui.internal.repository.tree.StashNode) RemoteNode(org.eclipse.egit.ui.internal.repository.tree.RemoteNode) IOException(java.io.IOException) AdditionalRefNode(org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode) RepositoryNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryNode) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) SubmoduleWalk(org.eclipse.jgit.submodule.SubmoduleWalk) PushNode(org.eclipse.egit.ui.internal.repository.tree.PushNode) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RemoteTrackingNode(org.eclipse.egit.ui.internal.repository.tree.RemoteTrackingNode) RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) LocalNode(org.eclipse.egit.ui.internal.repository.tree.LocalNode) StashedCommitNode(org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode) File(java.io.File) BranchHierarchyNode(org.eclipse.egit.ui.internal.repository.tree.BranchHierarchyNode) FileNode(org.eclipse.egit.ui.internal.repository.tree.FileNode)

Example 4 with StashedCommitNode

use of org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode in project egit by eclipse.

the class RepositoriesView method createCommonViewer.

@Override
protected CommonViewer createCommonViewer(Composite aParent) {
    CommonViewer viewer = super.createCommonViewer(aParent);
    // handle the double-click event for tags and branches
    viewer.addDoubleClickListener(new IDoubleClickListener() {

        @Override
        public void doubleClick(DoubleClickEvent event) {
            TreeSelection sel = (TreeSelection) event.getSelection();
            RepositoryTreeNode element = (RepositoryTreeNode) sel.getFirstElement();
            // Disable checkout for bare repositories
            if (element.getRepository().isBare()) {
                return;
            }
            if (element instanceof RefNode) {
                executeOpenCommandWithConfirmation(element, ((RefNode) element).getObject().getName());
            } else if (element instanceof TagNode) {
                executeOpenCommandWithConfirmation(element, ((TagNode) element).getObject().getName());
            }
        }
    });
    // handle open event for the working directory
    viewer.addOpenListener(new IOpenListener() {

        @Override
        public void open(OpenEvent event) {
            TreeSelection sel = (TreeSelection) event.getSelection();
            RepositoryTreeNode element = (RepositoryTreeNode) sel.getFirstElement();
            if (element instanceof FileNode || element instanceof StashedCommitNode)
                executeOpenCommand(element);
        }
    });
    // react on selection changes
    ISelectionService srv = CommonUtils.getService(getSite(), ISelectionService.class);
    srv.addPostSelectionListener(selectionChangedListener);
    // react on changes in the configured repositories
    repositoryUtil.getPreferences().addPreferenceChangeListener(configurationListener);
    initRepositoriesAndListeners();
    activateContextService();
    emptyArea.setBackground(viewer.getControl().getBackground());
    if (!repositories.isEmpty())
        layout.topControl = viewer.getControl();
    else
        layout.topControl = emptyArea;
    return viewer;
}
Also used : CommonViewer(org.eclipse.ui.navigator.CommonViewer) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) RefNode(org.eclipse.egit.ui.internal.repository.tree.RefNode) IOpenListener(org.eclipse.jface.viewers.IOpenListener) RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) TreeSelection(org.eclipse.jface.viewers.TreeSelection) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) ISelectionService(org.eclipse.ui.ISelectionService) StashedCommitNode(org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode) TagNode(org.eclipse.egit.ui.internal.repository.tree.TagNode) FileNode(org.eclipse.egit.ui.internal.repository.tree.FileNode) OpenEvent(org.eclipse.jface.viewers.OpenEvent)

Example 5 with StashedCommitNode

use of org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode in project egit by eclipse.

the class StashApplyCommand method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    List<StashedCommitNode> nodes = getSelectedNodes(event);
    if (nodes.isEmpty())
        return null;
    Repository repo = nodes.get(0).getRepository();
    if (repo == null)
        return null;
    final RevCommit commit = nodes.get(0).getObject();
    if (commit == null)
        return null;
    final StashApplyOperation op = new StashApplyOperation(repo, commit);
    Job job = new WorkspaceJob(MessageFormat.format(UIText.StashApplyCommand_jobTitle, commit.name())) {

        @Override
        public IStatus runInWorkspace(IProgressMonitor monitor) {
            try {
                op.execute(monitor);
            } catch (CoreException e) {
                return new Status(IStatus.ERROR, Activator.getPluginId(), MessageFormat.format(UIText.StashApplyCommand_applyFailed, commit.abbreviate(7).name(), e.getLocalizedMessage()), e);
            }
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            if (JobFamilies.STASH.equals(family))
                return true;
            return super.belongsTo(family);
        }
    };
    job.setUser(true);
    job.setRule(op.getSchedulingRule());
    job.schedule();
    return null;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) Repository(org.eclipse.jgit.lib.Repository) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) StashApplyOperation(org.eclipse.egit.core.op.StashApplyOperation) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) StashedCommitNode(org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode) Job(org.eclipse.core.runtime.jobs.Job) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

StashedCommitNode (org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode)5 FileNode (org.eclipse.egit.ui.internal.repository.tree.FileNode)3 RefNode (org.eclipse.egit.ui.internal.repository.tree.RefNode)3 RepositoryTreeNode (org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode)3 Repository (org.eclipse.jgit.lib.Repository)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 CoreException (org.eclipse.core.runtime.CoreException)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 Job (org.eclipse.core.runtime.jobs.Job)2 TagNode (org.eclipse.egit.ui.internal.repository.tree.TagNode)2 File (java.io.File)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)1