Search in sources :

Example 1 with AdditionalRefNode

use of org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode 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 2 with AdditionalRefNode

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

the class RepositoriesViewLabelProvider method getToolTipText.

@Override
public String getToolTipText(Object element) {
    if (element instanceof AdditionalRefNode) {
        AdditionalRefNode additionalRefNode = (AdditionalRefNode) element;
        Ref ref = additionalRefNode.getObject();
        return GitLabels.getRefDescription(ref);
    }
    return null;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) AdditionalRefNode(org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode)

Example 3 with AdditionalRefNode

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

the class AbstractBranchSelectionDialog method markRef.

/**
 * Set the selection to a {@link Ref} if possible
 *
 * @param refName
 *            the name of the {@link Ref}
 * @return <code>true</code> if the {@link Ref} with the given name was
 *         found
 */
protected boolean markRef(String refName) {
    // selects the entry specified by the name
    if (refName == null)
        return false;
    RepositoryTreeNode node;
    try {
        if (refName.startsWith(Constants.R_HEADS)) {
            Ref ref = repo.exactRef(refName);
            if (ref == null)
                return false;
            node = createRefNode(localBranches, repo, ref);
        } else if (refName.startsWith(Constants.R_REMOTES)) {
            Ref ref = repo.exactRef(refName);
            if (ref == null)
                return false;
            node = createRefNode(remoteBranches, repo, ref);
        } else if (Constants.HEAD.equals(refName)) {
            Ref ref = repo.exactRef(refName);
            if (ref == null)
                return false;
            node = new AdditionalRefNode(references, repo, ref);
        } else {
            String mappedRef = Activator.getDefault().getRepositoryUtil().mapCommitToRef(repo, refName, false);
            if (mappedRef != null && mappedRef.startsWith(Constants.R_REMOTES)) {
                Ref ref = repo.exactRef(mappedRef);
                if (ref == null)
                    return false;
                node = createRefNode(remoteBranches, repo, ref);
            } else if (mappedRef != null && mappedRef.startsWith(Constants.R_TAGS)) {
                Ref ref = repo.exactRef(mappedRef);
                if (ref == null)
                    return false;
                node = new TagNode(tags, repo, ref);
            } else
                return false;
        }
    } catch (IOException e) {
        return false;
    }
    branchTree.setSelection(new StructuredSelection(node), true);
    return true;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) AdditionalRefNode(org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode) IOException(java.io.IOException) TagNode(org.eclipse.egit.ui.internal.repository.tree.TagNode)

Example 4 with AdditionalRefNode

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

the class GitHistoryPage method inputSet.

@Override
public boolean inputSet() {
    try {
        if (trace)
            GitTraceLocation.getTrace().traceEntry(GitTraceLocation.HISTORYVIEW.getLocation());
        if (this.input != null)
            return true;
        Object o = super.getInput();
        if (o == null) {
            setErrorMessage(UIText.GitHistoryPage_NoInputMessage);
            return false;
        }
        boolean showHead = false;
        boolean showRef = false;
        boolean showTag = false;
        Repository repo = null;
        RevCommit selection = null;
        Ref ref = null;
        if (o instanceof IResource) {
            RepositoryMapping mapping = RepositoryMapping.getMapping((IResource) o);
            if (mapping != null) {
                repo = mapping.getRepository();
                input = new HistoryPageInput(repo, new IResource[] { (IResource) o });
                showHead = true;
            }
        } else if (o instanceof RepositoryTreeNode) {
            RepositoryTreeNode repoNode = (RepositoryTreeNode) o;
            repo = repoNode.getRepository();
            switch(repoNode.getType()) {
                case FILE:
                    File file = ((FileNode) repoNode).getObject();
                    input = new HistoryPageInput(repo, new File[] { file });
                    showHead = true;
                    break;
                case FOLDER:
                    File folder = ((FolderNode) repoNode).getObject();
                    input = new HistoryPageInput(repo, new File[] { folder });
                    showHead = true;
                    break;
                case REF:
                    input = new HistoryPageInput(repo);
                    ref = ((RefNode) repoNode).getObject();
                    showRef = true;
                    break;
                case ADDITIONALREF:
                    input = new HistoryPageInput(repo);
                    ref = ((AdditionalRefNode) repoNode).getObject();
                    showRef = true;
                    break;
                case TAG:
                    input = new HistoryPageInput(repo);
                    ref = ((TagNode) repoNode).getObject();
                    showTag = true;
                    break;
                default:
                    input = new HistoryPageInput(repo);
                    showHead = true;
                    break;
            }
        } else if (o instanceof HistoryPageInput) {
            input = (HistoryPageInput) o;
        } else {
            IResource resource = AdapterUtils.adaptToAnyResource(o);
            if (resource != null) {
                RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
                if (mapping != null) {
                    repo = mapping.getRepository();
                    input = new HistoryPageInput(repo, new IResource[] { resource });
                }
            }
        }
        if (repo == null) {
            repo = AdapterUtils.adapt(o, Repository.class);
            if (repo != null) {
                File file = AdapterUtils.adapt(o, File.class);
                if (file == null) {
                    input = new HistoryPageInput(repo);
                } else {
                    input = new HistoryPageInput(repo, new File[] { file });
                }
            }
        }
        selection = AdapterUtils.adapt(o, RevCommit.class);
        if (input == null) {
            // $NON-NLS-1$
            this.name = "";
            setErrorMessage(UIText.GitHistoryPage_NoInputMessage);
            return false;
        }
        final IResource[] inResources = input.getItems();
        final File[] inFiles = input.getFileList();
        this.name = calculateName(input);
        // disable the filters if we have a Repository as input
        boolean filtersActive = inResources != null || inFiles != null;
        actions.showAllRepoVersionsAction.setEnabled(filtersActive);
        actions.showAllProjectVersionsAction.setEnabled(filtersActive);
        // the repository itself has no notion of projects
        actions.showAllFolderVersionsAction.setEnabled(inResources != null);
        actions.showAllResourceVersionsAction.setEnabled(filtersActive);
        setErrorMessage(null);
        try {
            initAndStartRevWalk(false);
        } catch (IllegalStateException e) {
            Activator.handleError(e.getMessage(), e, true);
            return false;
        }
        if (showHead)
            showHead(repo);
        if (showRef)
            showRef(ref, repo);
        if (showTag)
            showTag(ref, repo);
        if (selection != null)
            graph.selectCommitStored(selection);
        return true;
    } finally {
        if (trace)
            GitTraceLocation.getTrace().traceExit(GitTraceLocation.HISTORYVIEW.getLocation());
    }
}
Also used : AdditionalRefNode(org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode) RefNode(org.eclipse.egit.ui.internal.repository.tree.RefNode) AdditionalRefNode(org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) RevObject(org.eclipse.jgit.revwalk.RevObject) File(java.io.File) IResource(org.eclipse.core.resources.IResource) TagNode(org.eclipse.egit.ui.internal.repository.tree.TagNode) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

AdditionalRefNode (org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode)4 Ref (org.eclipse.jgit.lib.Ref)4 RepositoryTreeNode (org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode)3 File (java.io.File)2 IOException (java.io.IOException)2 RefNode (org.eclipse.egit.ui.internal.repository.tree.RefNode)2 TagNode (org.eclipse.egit.ui.internal.repository.tree.TagNode)2 Repository (org.eclipse.jgit.lib.Repository)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 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 IResource (org.eclipse.core.resources.IResource)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 RepositoryMapping (org.eclipse.egit.core.project.RepositoryMapping)1 AdditionalRefsNode (org.eclipse.egit.ui.internal.repository.tree.AdditionalRefsNode)1