Search in sources :

Example 1 with WorkingDirNode

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

the class RepositoriesViewCommandHandler method isWorkingDirSelection.

/**
 * Enable the command if all of the following conditions are fulfilled: <li>
 * All selected nodes belong to the same repository <li>All selected nodes
 * are of type FileNode or FolderNode or WorkingTreeNode <li>Each node does
 * not represent a file / folder in the git directory
 *
 * @return true if selection is a working directory selection
 */
protected boolean isWorkingDirSelection() {
    List<T> selectedNodes = getSelectedNodes();
    if (selectedNodes.isEmpty())
        return false;
    Repository repository = null;
    for (T selectedNode : selectedNodes) {
        Repository nodeRepository = selectedNode.getRepository();
        if (repository == null)
            repository = nodeRepository;
        else if (repository != nodeRepository) {
            return false;
        }
        if (!(selectedNode instanceof WorkingDirNode)) {
            File file;
            if (selectedNode instanceof FolderNode)
                file = ((FolderNode) selectedNode).getObject().getAbsoluteFile();
            else if (selectedNode instanceof FileNode)
                file = ((FileNode) selectedNode).getObject().getAbsoluteFile();
            else {
                return false;
            }
            File gitDir = repository.getDirectory().getAbsoluteFile();
            if (file.toPath().startsWith(gitDir.toPath())) {
                return false;
            }
        }
    }
    return true;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) FolderNode(org.eclipse.egit.ui.internal.repository.tree.FolderNode) WorkingDirNode(org.eclipse.egit.ui.internal.repository.tree.WorkingDirNode) File(java.io.File) FileNode(org.eclipse.egit.ui.internal.repository.tree.FileNode)

Example 2 with WorkingDirNode

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

the class GitSelectWizardPage method createControl.

@Override
public void createControl(Composite parent) {
    Composite main = new Composite(parent, SWT.NO_RADIO_GROUP);
    main.setLayout(new GridLayout(1, false));
    SelectionListener sl = new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            tv.getTree().setEnabled(!newProjectWizard.getSelection());
            if (importExisting.getSelection())
                wizardSelection = EXISTING_PROJECTS_WIZARD;
            else if (newProjectWizard.getSelection())
                wizardSelection = NEW_WIZARD;
            else if (generalWizard.getSelection())
                wizardSelection = GENERAL_WIZARD;
            else
                wizardSelection = EXISTING_PROJECTS_WIZARD;
            checkPage();
        }
    };
    Group wizardType = new Group(main, SWT.SHADOW_ETCHED_IN);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(wizardType);
    wizardType.setText(UIText.GitSelectWizardPage_ProjectCreationHeader);
    wizardType.setLayout(new GridLayout(1, false));
    importExisting = new Button(wizardType, SWT.RADIO);
    importExisting.setText(UIText.GitSelectWizardPage_ImportExistingButton);
    importExisting.addSelectionListener(sl);
    decorateWithFilterCount(importExisting);
    newProjectWizard = new Button(wizardType, SWT.RADIO);
    newProjectWizard.setText(UIText.GitSelectWizardPage_UseNewProjectsWizardButton);
    newProjectWizard.addSelectionListener(sl);
    generalWizard = new Button(wizardType, SWT.RADIO);
    generalWizard.setText(UIText.GitSelectWizardPage_ImportAsGeneralButton);
    generalWizard.addSelectionListener(sl);
    IDialogSettings settings = Activator.getDefault().getDialogSettings();
    try {
        wizardSelection = settings.getInt(PREF_WIZ);
    } catch (NumberFormatException e) {
        wizardSelection = EXISTING_PROJECTS_WIZARD;
    }
    switch(wizardSelection) {
        default:
        case EXISTING_PROJECTS_WIZARD:
            importExisting.setSelection(true);
            break;
        case GENERAL_WIZARD:
            generalWizard.setSelection(true);
            break;
        case NEW_WIZARD:
            newProjectWizard.setSelection(true);
            break;
    }
    tv = new TreeViewer(main, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    RepositoriesViewContentProvider cp = new RepositoriesViewContentProvider();
    tv.setContentProvider(cp);
    GridDataFactory.fillDefaults().grab(true, true).hint(SWT.DEFAULT, 200).applyTo(tv.getTree());
    tv.setLabelProvider(new RepositoriesViewLabelProvider());
    tv.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            checkPage();
        }
    });
    if (initialRepository != null) {
        List<WorkingDirNode> input = new ArrayList<>();
        WorkingDirNode node = new WorkingDirNode(null, initialRepository);
        input.add(node);
        tv.setInput(input);
        // expand root node
        tv.expandToLevel(2);
        // select the working directory as default
        if (initialPath == null)
            tv.setSelection(new StructuredSelection(input.get(0)));
        else {
            RepositoryTreeNode parentNode = node;
            IPath fullPath = new Path(initialPath);
            IPath workdirPath = new Path(initialRepository.getWorkTree().getPath());
            if (workdirPath.isPrefixOf(fullPath)) {
                IPath relPath = fullPath.removeFirstSegments(workdirPath.segmentCount());
                for (String segment : relPath.segments()) {
                    for (Object child : cp.getChildren(parentNode)) {
                        if (child instanceof FolderNode) {
                            FolderNode childFolder = (FolderNode) child;
                            if (childFolder.getObject().getName().equals(segment)) {
                                parentNode = childFolder;
                                break;
                            }
                        }
                    }
                }
                tv.setSelection(new StructuredSelection(parentNode));
            }
        }
    }
    tv.getTree().setEnabled(!newProjectWizard.getSelection());
    Dialog.applyDialogFont(main);
    setControl(main);
}
Also used : Group(org.eclipse.swt.widgets.Group) FolderNode(org.eclipse.egit.ui.internal.repository.tree.FolderNode) TreeViewer(org.eclipse.jface.viewers.TreeViewer) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) RepositoriesViewLabelProvider(org.eclipse.egit.ui.internal.repository.RepositoriesViewLabelProvider) RepositoriesViewContentProvider(org.eclipse.egit.ui.internal.repository.RepositoriesViewContentProvider) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) WorkingDirNode(org.eclipse.egit.ui.internal.repository.tree.WorkingDirNode) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Composite(org.eclipse.swt.widgets.Composite) IPath(org.eclipse.core.runtime.IPath) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 3 with WorkingDirNode

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

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

the class GitRepositoriesViewTestUtils method getWorkdirItem.

public SWTBotTreeItem getWorkdirItem(SWTBotTree tree, File repositoryFile) throws Exception {
    Repository repository = lookupRepository(repositoryFile);
    RepositoryNode root = new RepositoryNode(null, repository);
    WorkingDirNode workdir = new WorkingDirNode(root, repository);
    String rootText = labelProvider.getStyledText(root).getString();
    SWTBotTreeItem rootItem = tree.getTreeItem(rootText);
    SWTBotTreeItem workdirItem = TestUtil.expandAndWait(rootItem).getNode(labelProvider.getStyledText(workdir).getString());
    return workdirItem;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) SWTBotTreeItem(org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem) RepositoryNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryNode) WorkingDirNode(org.eclipse.egit.ui.internal.repository.tree.WorkingDirNode)

Example 5 with WorkingDirNode

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

the class GitSelectWizardPage method setRepository.

/**
 * @param repo
 */
public void setRepository(Repository repo) {
    List<WorkingDirNode> input = new ArrayList<>();
    if (repo != null)
        input.add(new WorkingDirNode(null, repo));
    tv.setInput(input);
    // expand root node
    tv.expandToLevel(2);
    // select the working directory as default
    tv.setSelection(new StructuredSelection(input.get(0)));
}
Also used : ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) WorkingDirNode(org.eclipse.egit.ui.internal.repository.tree.WorkingDirNode)

Aggregations

WorkingDirNode (org.eclipse.egit.ui.internal.repository.tree.WorkingDirNode)6 ArrayList (java.util.ArrayList)4 FolderNode (org.eclipse.egit.ui.internal.repository.tree.FolderNode)4 IPath (org.eclipse.core.runtime.IPath)3 FileNode (org.eclipse.egit.ui.internal.repository.tree.FileNode)3 RepositoryNode (org.eclipse.egit.ui.internal.repository.tree.RepositoryNode)3 RepositoryTreeNode (org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode)3 Repository (org.eclipse.jgit.lib.Repository)3 File (java.io.File)2 Path (org.eclipse.core.runtime.Path)2 FetchNode (org.eclipse.egit.ui.internal.repository.tree.FetchNode)2 PushNode (org.eclipse.egit.ui.internal.repository.tree.PushNode)2 RefNode (org.eclipse.egit.ui.internal.repository.tree.RefNode)2 RemoteNode (org.eclipse.egit.ui.internal.repository.tree.RemoteNode)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 Comparator (java.util.Comparator)1 List (java.util.List)1