Search in sources :

Example 66 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class GitSynchronizeWizard method getSelectedResources.

private Set<IResource> getSelectedResources(Repository repo) {
    ISelectionService selectionService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
    ISelection selection = selectionService.getSelection();
    if (selection instanceof IStructuredSelection) {
        Set<IResource> result = new HashSet<>();
        IStructuredSelection sel = (IStructuredSelection) selection;
        if (sel.size() == 0)
            return null;
        File workTree = repo.getWorkTree();
        for (Object o : sel.toArray()) {
            if (o == null) {
                continue;
            }
            IResource res = AdapterUtils.adaptToAnyResource(o);
            if (res == null) {
                continue;
            }
            int type = res.getType();
            if (type == IResource.FOLDER) {
                RepositoryMapping mapping = RepositoryMapping.getMapping(res);
                if (mapping == null) {
                    continue;
                }
                Repository selRepo = mapping.getRepository();
                if (workTree.equals(selRepo.getWorkTree()))
                    result.add(res);
            }
        }
        return result;
    }
    return null;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ISelection(org.eclipse.jface.viewers.ISelection) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) ISelectionService(org.eclipse.ui.ISelectionService) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) File(java.io.File) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

Example 67 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class GitSynchronizeWizardPage method createControl.

@Override
public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(1, false);
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    composite.setLayout(layout);
    for (IProject project : ROOT.getProjects()) {
        RepositoryMapping repositoryMapping = RepositoryMapping.getMapping(project);
        if (repositoryMapping == null)
            continue;
        Repository repo = repositoryMapping.getRepository();
        Set<IProject> projects = resources.get(repo);
        if (projects == null) {
            projects = new HashSet<>();
            resources.put(repo, projects);
        }
        projects.add(project);
    }
    treeViewer = new TreeViewer(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
    treeViewer.getTree().setLinesVisible(true);
    treeViewer.getTree().setHeaderVisible(true);
    treeViewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    TreeViewerColumn projectsColumn = new TreeViewerColumn(treeViewer, SWT.LEAD);
    projectsColumn.getColumn().setText(UIText.GitBranchSynchronizeWizardPage_repository);
    projectsColumn.getColumn().setImage(repositoryImage);
    projectsColumn.setLabelProvider(new StyledCellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            Object element = cell.getElement();
            if (element instanceof Repository) {
                Repository repo = (Repository) element;
                // $NON-NLS-1$
                String descr = "";
                try {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    descr += " [" + repo.getBranch() + "]";
                } catch (IOException e) {
                    Activator.logError(e.getMessage(), e);
                }
                Color decorationsColor = JFaceResources.getColorRegistry().get(JFacePreferences.DECORATIONS_COLOR);
                String repoName = repo.getWorkTree().getName();
                int repoNameLen = repoName.length();
                StyleRange styleRange = new StyleRange(repoNameLen, repoNameLen + descr.length(), decorationsColor, null);
                cell.setImage(repositoryImage);
                cell.setText(repoName + descr);
                cell.setStyleRanges(new StyleRange[] { styleRange });
            }
            super.update(cell);
        }
    });
    TreeViewerColumn dstColumn = new TreeViewerColumn(treeViewer, SWT.LEAD);
    dstColumn.getColumn().setText(UIText.GitBranchSynchronizeWizardPage_destination);
    dstColumn.getColumn().setImage(branchImage);
    dstColumn.getColumn().setWidth(200);
    final ComboBoxCellEditor branchesEditor = new ComboBoxCellEditor(treeViewer.getTree(), new String[0]);
    branchesEditor.setActivationStyle(ComboBoxCellEditor.DROP_DOWN_ON_KEY_ACTIVATION | ComboBoxCellEditor.DROP_DOWN_ON_MOUSE_ACTIVATION);
    ((CCombo) branchesEditor.getControl()).addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            CCombo combo = (CCombo) e.widget;
            TreeSelection sel = (TreeSelection) treeViewer.getSelection();
            int selectedIdx = combo.getSelectionIndex();
            Repository repo = (Repository) sel.getFirstElement();
            if (selectedIdx != -1) {
                selectedBranches.put(repo, combo.getItem(selectedIdx));
                setPageComplete(true);
            } else {
                selectedBranches.put(repo, null);
                setPageComplete(false);
            }
        }
    });
    dstColumn.setEditingSupport(new EditingSupport(treeViewer) {

        @Override
        protected void setValue(Object element, Object value) {
            int intValue = ((Integer) value).intValue();
            if (intValue == -1)
                return;
            CCombo combo = (CCombo) branchesEditor.getControl();
            String branch = combo.getItem(intValue);
            selectedBranches.put((Repository) element, branch);
            treeViewer.refresh(element, true);
            validatePage();
        }

        @Override
        protected Object getValue(Object element) {
            String branch = selectedBranches.get(element);
            CCombo combo = (CCombo) branchesEditor.getControl();
            int index = branch == null ? 0 : combo.indexOf(branch);
            return Integer.valueOf(index);
        }

        @Override
        protected CellEditor getCellEditor(Object element) {
            Repository repo = (Repository) element;
            List<String> refs = new LinkedList<>(repo.getAllRefs().keySet());
            List<Ref> additionalRefs;
            try {
                additionalRefs = repo.getRefDatabase().getAdditionalRefs();
            } catch (IOException e) {
                additionalRefs = null;
            }
            if (additionalRefs != null)
                for (Ref ref : additionalRefs) refs.add(ref.getName());
            Collections.sort(refs, CommonUtils.STRING_ASCENDING_COMPARATOR);
            branchesEditor.setItems(refs.toArray(new String[refs.size()]));
            return branchesEditor;
        }

        @Override
        protected boolean canEdit(Object element) {
            return true;
        }
    });
    dstColumn.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            String branch = selectedBranches.get(element);
            // $NON-NLS-1$
            return branch == null ? "" : branch;
        }
    });
    treeViewer.setContentProvider(new ITreeContentProvider() {

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        // nothing to do
        }

        @Override
        public void dispose() {
        // nothing to do
        }

        @Override
        public Object[] getElements(Object inputElement) {
            return (Object[]) inputElement;
        }

        @Override
        public boolean hasChildren(Object element) {
            return false;
        }

        @Override
        public Object getParent(Object element) {
            return null;
        }

        @Override
        public Object[] getChildren(Object parentElement) {
            return new Object[0];
        }
    });
    List<Repository> repositoriesList = new ArrayList<>(resources.keySet());
    Collections.sort(repositoriesList, new Comparator<Repository>() {

        @Override
        public int compare(Repository o1, Repository o2) {
            String name1 = o1.getWorkTree().getName();
            String name2 = o2.getWorkTree().getName();
            return name1.compareToIgnoreCase(name2);
        }
    });
    treeViewer.setInput(repositoriesList.toArray(new Repository[repositoriesList.size()]));
    projectsColumn.getColumn().pack();
    Composite buttonsComposite = new Composite(composite, SWT.NONE);
    layout = new GridLayout(4, false);
    layout.numColumns = 1;
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    buttonsComposite.setLayout(layout);
    buttonsComposite.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
    final Button fetchChanges = new Button(buttonsComposite, SWT.CHECK);
    fetchChanges.setText(UIText.GitBranchSynchronizeWizardPage_fetchChangesFromRemote);
    fetchChanges.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            forceFetch = fetchChanges.getSelection();
        }
    });
    fetchChanges.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
    fetchChanges.setSelection(Activator.getDefault().getPreferenceStore().getBoolean(UIPreferences.SYNC_VIEW_FETCH_BEFORE_LAUNCH));
    final Button includeLocal = new Button(buttonsComposite, SWT.CHECK);
    includeLocal.setText(UIText.GitBranchSynchronizeWizardPage_includeUncommitedChanges);
    includeLocal.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            shouldIncludeLocal = includeLocal.getSelection();
        }
    });
    includeLocal.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
    Dialog.applyDialogFont(composite);
    setPageComplete(false);
    setControl(composite);
}
Also used : ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) TreeViewer(org.eclipse.jface.viewers.TreeViewer) CellEditor(org.eclipse.jface.viewers.CellEditor) ComboBoxCellEditor(org.eclipse.jface.viewers.ComboBoxCellEditor) StyleRange(org.eclipse.swt.custom.StyleRange) ArrayList(java.util.ArrayList) EditingSupport(org.eclipse.jface.viewers.EditingSupport) Viewer(org.eclipse.jface.viewers.Viewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) TreeSelection(org.eclipse.jface.viewers.TreeSelection) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) StyledCellLabelProvider(org.eclipse.jface.viewers.StyledCellLabelProvider) Composite(org.eclipse.swt.widgets.Composite) Color(org.eclipse.swt.graphics.Color) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ComboBoxCellEditor(org.eclipse.jface.viewers.ComboBoxCellEditor) IOException(java.io.IOException) ViewerCell(org.eclipse.jface.viewers.ViewerCell) IProject(org.eclipse.core.resources.IProject) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) CCombo(org.eclipse.swt.custom.CCombo) GridData(org.eclipse.swt.layout.GridData)

Example 68 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class GitOpenInCompareAction method getCachedFileElement.

private ITypedElement getCachedFileElement(GitModelBlob blob) {
    try {
        IPath location = blob.getLocation();
        RepositoryMapping mapping = RepositoryMapping.getMapping(location);
        if (mapping == null) {
            return null;
        }
        Repository repo = mapping.getRepository();
        String repoRelativePath = mapping.getRepoRelativePath(location);
        return CompareUtils.getIndexTypedElement(repo, repoRelativePath);
    } catch (IOException e) {
        return null;
    }
}
Also used : GitModelRepository(org.eclipse.egit.ui.internal.synchronize.model.GitModelRepository) Repository(org.eclipse.jgit.lib.Repository) IPath(org.eclipse.core.runtime.IPath) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IOException(java.io.IOException)

Example 69 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class LocalNonWorkspaceTypedElement method fireChanges.

private void fireChanges() {
    fireContentChanged();
    // external file change must be reported explicitly, see bug 481682
    Repository myRepository = repository;
    boolean updated = false;
    if (!myRepository.isBare()) {
        updated = refreshRepositoryState(myRepository);
    }
    if (!updated) {
        RepositoryMapping mapping = RepositoryMapping.getMapping(path);
        if (mapping != null) {
            mapping.getRepository().fireEvent(new IndexChangedEvent());
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IndexChangedEvent(org.eclipse.jgit.events.IndexChangedEvent)

Example 70 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class GitVariableResolver method getGitDir.

private String getGitDir(String argument) throws CoreException {
    IResource res = getResource(argument);
    RepositoryMapping mapping = RepositoryMapping.getMapping(res);
    if (mapping != null)
        return mapping.getRepository().getDirectory().getAbsolutePath();
    else
        // $NON-NLS-1$
        return "";
}
Also used : RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IResource(org.eclipse.core.resources.IResource)

Aggregations

RepositoryMapping (org.eclipse.egit.core.project.RepositoryMapping)87 Repository (org.eclipse.jgit.lib.Repository)40 IResource (org.eclipse.core.resources.IResource)31 IProject (org.eclipse.core.resources.IProject)23 IPath (org.eclipse.core.runtime.IPath)20 IOException (java.io.IOException)18 File (java.io.File)17 CoreException (org.eclipse.core.runtime.CoreException)15 IFile (org.eclipse.core.resources.IFile)12 Path (org.eclipse.core.runtime.Path)12 RevCommit (org.eclipse.jgit.revwalk.RevCommit)11 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)10 GitProjectData (org.eclipse.egit.core.project.GitProjectData)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 HashSet (java.util.HashSet)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 RevWalk (org.eclipse.jgit.revwalk.RevWalk)6 HashMap (java.util.HashMap)5