Search in sources :

Example 11 with RepositoryMapping

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

the class GitAdapterFactory method getRepository.

@Nullable
private static Repository getRepository(IURIEditorInput uriInput) {
    File file = getFile(uriInput);
    if (file == null) {
        return null;
    }
    Path path = new Path(file.getAbsolutePath());
    RepositoryMapping mapping = RepositoryMapping.getMapping(path);
    if (mapping != null) {
        return mapping.getRepository();
    }
    Repository repository = org.eclipse.egit.core.Activator.getDefault().getRepositoryCache().getRepository(path);
    return repository;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) File(java.io.File) Nullable(org.eclipse.jgit.annotations.Nullable)

Example 12 with RepositoryMapping

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

the class CommitSelectionDialog method createTreeFilter.

private TreeFilter createTreeFilter() {
    if (filterResources == null)
        return TreeFilter.ALL;
    List<TreeFilter> filters = new ArrayList<>();
    for (IResource resource : filterResources) {
        RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
        if (mapping != null) {
            DiffConfig diffConfig = mapping.getRepository().getConfig().get(DiffConfig.KEY);
            String path = mapping.getRepoRelativePath(resource);
            if (path != null && !"".equals(path)) {
                // $NON-NLS-1$
                if (resource.getType() == IResource.FILE)
                    filters.add(FollowFilter.create(path, diffConfig));
                else
                    filters.add(AndTreeFilter.create(PathFilter.create(path), TreeFilter.ANY_DIFF));
            }
        }
    }
    if (filters.isEmpty())
        return TreeFilter.ALL;
    else if (filters.size() == 1)
        return filters.get(0);
    else
        return OrTreeFilter.create(filters);
}
Also used : ArrayList(java.util.ArrayList) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) DiffConfig(org.eclipse.jgit.diff.DiffConfig) IResource(org.eclipse.core.resources.IResource)

Example 13 with RepositoryMapping

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

the class GitCompareEditorInput method prepareInput.

@Override
protected Object prepareInput(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
    // make sure all resources belong to the same repository
    try (RevWalk rw = new RevWalk(repository)) {
        monitor.beginTask(UIText.GitCompareEditorInput_CompareResourcesTaskName, IProgressMonitor.UNKNOWN);
        for (IResource resource : resources) {
            RepositoryMapping map = RepositoryMapping.getMapping(resource);
            if (map == null) {
                throw new InvocationTargetException(new IllegalStateException(UIText.GitCompareEditorInput_ResourcesInDifferentReposMessagge));
            }
            if (repository != null && repository != map.getRepository())
                throw new InvocationTargetException(new IllegalStateException(UIText.GitCompareEditorInput_ResourcesInDifferentReposMessagge));
            String repoRelativePath = map.getRepoRelativePath(resource);
            filterPathStrings.add(repoRelativePath);
            DiffNode node = new DiffNode(Differencer.NO_CHANGE) {

                @Override
                public Image getImage() {
                    return FOLDER_IMAGE;
                }
            };
            diffRoots.put(new Path(map.getRepoRelativePath(resource)), node);
            repository = map.getRepository();
        }
        if (repository == null)
            throw new InvocationTargetException(new IllegalStateException(UIText.GitCompareEditorInput_ResourcesInDifferentReposMessagge));
        if (monitor.isCanceled())
            throw new InterruptedException();
        final RevCommit baseCommit;
        try {
            try {
                baseCommit = rw.parseCommit(repository.resolve(baseVersion));
            } catch (IOException e) {
                throw new InvocationTargetException(e);
            }
            final RevCommit compareCommit;
            if (compareVersion == null) {
                compareCommit = null;
            } else {
                try {
                    compareCommit = rw.parseCommit(repository.resolve(compareVersion));
                } catch (IOException e) {
                    throw new InvocationTargetException(e);
                }
            }
            if (monitor.isCanceled())
                throw new InterruptedException();
            // set the labels
            CompareConfiguration config = getCompareConfiguration();
            config.setLeftLabel(compareVersion);
            config.setRightLabel(baseVersion);
            // set title and icon
            if (resources.length == 0) {
                Object[] titleParameters = new Object[] { Activator.getDefault().getRepositoryUtil().getRepositoryName(repository), CompareUtils.truncatedRevision(compareVersion), CompareUtils.truncatedRevision(baseVersion) };
                setTitle(NLS.bind(UIText.GitCompareEditorInput_EditorTitle, titleParameters));
            } else if (resources.length == 1) {
                Object[] titleParameters = new Object[] { resources[0].getFullPath().makeRelative().toString(), CompareUtils.truncatedRevision(compareVersion), CompareUtils.truncatedRevision(baseVersion) };
                setTitle(NLS.bind(UIText.GitCompareEditorInput_EditorTitleSingleResource, titleParameters));
            } else {
                setTitle(NLS.bind(UIText.GitCompareEditorInput_EditorTitleMultipleResources, CompareUtils.truncatedRevision(compareVersion), CompareUtils.truncatedRevision(baseVersion)));
            }
            // build the nodes
            try {
                return buildDiffContainer(baseCommit, compareCommit, monitor);
            } catch (IOException e) {
                throw new InvocationTargetException(e);
            }
        } finally {
            monitor.done();
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) CompareConfiguration(org.eclipse.compare.CompareConfiguration) DiffNode(org.eclipse.compare.structuremergeviewer.DiffNode) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) InvocationTargetException(java.lang.reflect.InvocationTargetException) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IResource(org.eclipse.core.resources.IResource) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 14 with RepositoryMapping

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

the class CompareVersionsHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = getSelection(event);
    if (selection.size() == 2) {
        Iterator<?> it = selection.iterator();
        RevCommit commit1 = (RevCommit) it.next();
        RevCommit commit2 = (RevCommit) it.next();
        Object input = getPage(event).getInputInternal().getSingleItem();
        Repository repo = getRepository(event);
        IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage();
        if (input instanceof IFile) {
            IFile resource = (IFile) input;
            final RepositoryMapping map = RepositoryMapping.getMapping(resource);
            if (map != null) {
                final String gitPath = map.getRepoRelativePath(resource);
                final String commit1Path = getRenamedPath(gitPath, commit1);
                final String commit2Path = getRenamedPath(gitPath, commit2);
                CompareUtils.openInCompare(commit1, commit2, commit1Path, commit2Path, map.getRepository(), workBenchPage);
            }
        } else if (input instanceof File) {
            File fileInput = (File) input;
            final String gitPath = getRepoRelativePath(repo, fileInput);
            final String commit1Path = getRenamedPath(gitPath, commit1);
            final String commit2Path = getRenamedPath(gitPath, commit2);
            CompareUtils.openInCompare(commit1, commit2, commit1Path, commit2Path, repo, workBenchPage);
        } else if (input instanceof IResource) {
            GitCompareEditorInput compareInput = new GitCompareEditorInput(commit1.name(), commit2.name(), repo, (IResource) input);
            CompareUtils.openInCompare(workBenchPage, compareInput);
        } else if (input == null) {
            GitCompareEditorInput compareInput = new GitCompareEditorInput(commit1.name(), commit2.name(), repo);
            CompareUtils.openInCompare(workBenchPage, compareInput);
        }
    }
    return null;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) IFile(org.eclipse.core.resources.IFile) GitCompareEditorInput(org.eclipse.egit.ui.internal.merge.GitCompareEditorInput) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 15 with RepositoryMapping

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

the class CompareVersionsInTreeHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = getSelection(event);
    if (selection.size() == 2) {
        Iterator<?> it = selection.iterator();
        RevCommit commit1 = (RevCommit) it.next();
        RevCommit commit2 = (RevCommit) it.next();
        HistoryPageInput pageInput = getPage(event).getInputInternal();
        Object input = pageInput.getSingleItem();
        Repository repository = pageInput.getRepository();
        IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage();
        // available in this case in the UI
        if (input instanceof IFile) {
            IFile resource = (IFile) input;
            final RepositoryMapping map = RepositoryMapping.getMapping(resource);
            if (map != null) {
                final String gitPath = map.getRepoRelativePath(resource);
                final String commit1Path = getRenamedPath(gitPath, commit1);
                final String commit2Path = getRenamedPath(gitPath, commit2);
                CompareUtils.openInCompare(commit1, commit2, commit1Path, commit2Path, map.getRepository(), workBenchPage);
            }
        } else if (input instanceof File) {
            File fileInput = (File) input;
            Repository repo = getRepository(event);
            final String gitPath = getRepoRelativePath(repo, fileInput);
            final String commit1Path = getRenamedPath(gitPath, commit1);
            final String commit2Path = getRenamedPath(gitPath, commit2);
            CompareUtils.openInCompare(commit1, commit2, commit1Path, commit2Path, repo, workBenchPage);
        } else if (input instanceof IResource) {
            CompareTreeView view;
            try {
                view = (CompareTreeView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(CompareTreeView.ID);
                view.setInput(new IResource[] { (IResource) input }, commit1.getId().name(), commit2.getId().name());
            } catch (PartInitException e) {
                Activator.handleError(e.getMessage(), e, true);
            }
        } else if (input == null) {
            CompareTreeView view;
            try {
                view = (CompareTreeView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(CompareTreeView.ID);
                view.setInput(repository, commit1.getId().name(), commit2.getId().name());
            } catch (PartInitException e) {
                Activator.handleError(e.getMessage(), e, true);
            }
        }
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) HistoryPageInput(org.eclipse.egit.ui.internal.history.HistoryPageInput) CompareTreeView(org.eclipse.egit.ui.internal.dialogs.CompareTreeView) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

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