Search in sources :

Example 16 with RepositoryMapping

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

the class CompareWithWorkingTreeHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = getSelection(event);
    if (selection.isEmpty())
        return null;
    // Even if there's more than one element, only consider the first
    RevCommit commit = (RevCommit) selection.getFirstElement();
    Object input = getPage(event).getInputInternal().getSingleFile();
    IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage();
    if (input instanceof IFile) {
        IFile file = (IFile) input;
        final RepositoryMapping mapping = RepositoryMapping.getMapping(file);
        if (mapping != null) {
            final String gitPath = mapping.getRepoRelativePath(file);
            final String commitPath = getRenamedPath(gitPath, commit);
            ITypedElement right = CompareUtils.getFileRevisionTypedElement(commitPath, commit, mapping.getRepository());
            final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(SaveableCompareEditorInput.createFileElement(file), right, null);
            CompareUtils.openInCompare(workBenchPage, in);
        }
    } else if (input instanceof File) {
        File file = (File) input;
        // TODO can we create a ITypedElement from the local file?
        Repository repo = getRepository(event);
        RevCommit leftCommit;
        try (RevWalk rw = new RevWalk(repo)) {
            leftCommit = rw.parseCommit(repo.resolve(Constants.HEAD));
        } catch (Exception e) {
            throw new ExecutionException(e.getMessage(), e);
        }
        final String leftCommitPath = getRepoRelativePath(repo, file);
        final String rightCommitPath = getRenamedPath(leftCommitPath, commit);
        CompareUtils.openInCompare(leftCommit, commit, leftCommitPath, rightCommitPath, repo, workBenchPage);
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ExecutionException(org.eclipse.core.commands.ExecutionException) Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ExecutionException(org.eclipse.core.commands.ExecutionException) File(java.io.File) IFile(org.eclipse.core.resources.IFile) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 17 with RepositoryMapping

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

the class ShowBlameHandler method getPath.

private String getPath(Repository repo, GitHistoryPage page) {
    Object input = page.getInputInternal().getSingleItem();
    if (input == null) {
        return null;
    }
    if (input instanceof IFile) {
        IFile file = (IFile) input;
        RepositoryMapping mapping = RepositoryMapping.getMapping(file);
        if (mapping != null) {
            return mapping.getRepoRelativePath(file);
        }
    } else if (input instanceof File) {
        return getRepoRelativePath(repo, (File) input);
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) File(java.io.File) IFile(org.eclipse.core.resources.IFile)

Example 18 with RepositoryMapping

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

the class ShowVersionsHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    boolean compareMode = Boolean.TRUE.toString().equals(event.getParameter(COMPARE_MODE_PARAM));
    IStructuredSelection selection = getSelection(getPage());
    if (selection.size() < 1)
        return null;
    Object input = getPage().getInputInternal().getSingleFile();
    if (input == null)
        return null;
    IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage();
    boolean errorOccurred = false;
    List<ObjectId> ids = new ArrayList<>();
    String gitPath = null;
    if (input instanceof IFile) {
        IFile resource = (IFile) input;
        final RepositoryMapping map = RepositoryMapping.getMapping(resource);
        if (map != null) {
            gitPath = map.getRepoRelativePath(resource);
            Iterator<?> it = selection.iterator();
            while (it.hasNext()) {
                RevCommit commit = (RevCommit) it.next();
                String commitPath = getRenamedPath(gitPath, commit);
                IFileRevision rev = null;
                try {
                    rev = CompareUtils.getFileRevision(commitPath, commit, map.getRepository(), null);
                } catch (IOException e) {
                    Activator.logError(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, gitPath, commit.getId()), e);
                    errorOccurred = true;
                }
                if (rev != null) {
                    if (compareMode) {
                        ITypedElement right = CompareUtils.getFileRevisionTypedElement(commitPath, commit, map.getRepository());
                        final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(SaveableCompareEditorInput.createFileElement(resource), right, null);
                        try {
                            CompareUtils.openInCompare(workBenchPage, in);
                        } catch (Exception e) {
                            errorOccurred = true;
                        }
                    } else
                        try {
                            EgitUiEditorUtils.openEditor(getPart(event).getSite().getPage(), rev, new NullProgressMonitor());
                        } catch (CoreException e) {
                            Activator.logError(UIText.GitHistoryPage_openFailed, e);
                            errorOccurred = true;
                        }
                } else {
                    ids.add(commit.getId());
                }
            }
        }
    }
    if (input instanceof File) {
        File fileInput = (File) input;
        Repository repo = getRepository(event);
        gitPath = getRepoRelativePath(repo, fileInput);
        Iterator<?> it = selection.iterator();
        while (it.hasNext()) {
            RevCommit commit = (RevCommit) it.next();
            String commitPath = getRenamedPath(gitPath, commit);
            IFileRevision rev = null;
            try {
                rev = CompareUtils.getFileRevision(commitPath, commit, repo, null);
            } catch (IOException e) {
                Activator.logError(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, commitPath, commit.getId()), e);
                errorOccurred = true;
            }
            if (rev != null) {
                if (compareMode)
                    try (RevWalk rw = new RevWalk(repo)) {
                        ITypedElement left = CompareUtils.getFileRevisionTypedElement(gitPath, rw.parseCommit(repo.resolve(Constants.HEAD)), repo);
                        ITypedElement right = CompareUtils.getFileRevisionTypedElement(commitPath, commit, repo);
                        final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(left, right, null);
                        CompareUtils.openInCompare(workBenchPage, in);
                    } catch (IOException e) {
                        errorOccurred = true;
                    }
                else
                    try {
                        EgitUiEditorUtils.openEditor(getPart(event).getSite().getPage(), rev, new NullProgressMonitor());
                    } catch (CoreException e) {
                        Activator.logError(UIText.GitHistoryPage_openFailed, e);
                        errorOccurred = true;
                    }
            } else
                ids.add(commit.getId());
        }
    }
    if (errorOccurred)
        Activator.showError(UIText.GitHistoryPage_openFailed, null);
    if (ids.size() > 0) {
        // $NON-NLS-1$
        StringBuilder idList = new StringBuilder("");
        for (ObjectId objectId : ids) idList.append(objectId.getName()).append(' ');
        MessageDialog.openError(getPart(event).getSite().getShell(), UIText.GitHistoryPage_fileNotFound, NLS.bind(UIText.GitHistoryPage_notContainedInCommits, gitPath, idList.toString()));
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) IFileRevision(org.eclipse.team.core.history.IFileRevision) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException) Repository(org.eclipse.jgit.lib.Repository) CoreException(org.eclipse.core.runtime.CoreException) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IFile(org.eclipse.core.resources.IFile) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 19 with RepositoryMapping

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

the class StagingView method stage.

private void stage(IStructuredSelection selection) {
    StagingViewContentProvider contentProvider = getContentProvider(unstagedViewer);
    final Repository repository = currentRepository;
    Iterator iterator = selection.iterator();
    final List<String> addPaths = new ArrayList<>();
    final List<String> rmPaths = new ArrayList<>();
    resetPathsToExpand();
    while (iterator.hasNext()) {
        Object element = iterator.next();
        if (element instanceof StagingEntry) {
            StagingEntry entry = (StagingEntry) element;
            selectEntryForStaging(entry, addPaths, rmPaths);
            addPathAndParentPaths(entry.getParentPath(), pathsToExpandInStaged);
        } else if (element instanceof StagingFolderEntry) {
            StagingFolderEntry folder = (StagingFolderEntry) element;
            List<StagingEntry> entries = contentProvider.getStagingEntriesFiltered(folder);
            for (StagingEntry entry : entries) selectEntryForStaging(entry, addPaths, rmPaths);
            addExpandedPathsBelowFolder(folder, unstagedViewer, pathsToExpandInStaged);
        } else {
            IResource resource = AdapterUtils.adaptToAnyResource(element);
            if (resource != null) {
                RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
                // submodule of the mapped repo
                if (mapping != null && mapping.getRepository() == currentRepository) {
                    String path = mapping.getRepoRelativePath(resource);
                    // If resource corresponds to root of working directory
                    if (// $NON-NLS-1$
                    "".equals(path))
                        // $NON-NLS-1$
                        addPaths.add(".");
                    else
                        addPaths.add(path);
                }
            }
        }
    }
    // start long running operations
    if (!addPaths.isEmpty()) {
        Job addJob = new Job(UIText.StagingView_AddJob) {

            @Override
            protected IStatus run(IProgressMonitor monitor) {
                try (Git git = new Git(repository)) {
                    AddCommand add = git.add();
                    for (String addPath : addPaths) add.addFilepattern(addPath);
                    add.call();
                } catch (NoFilepatternException e1) {
                // cannot happen
                } catch (JGitInternalException e1) {
                    Activator.handleError(e1.getCause().getMessage(), e1.getCause(), true);
                } catch (Exception e1) {
                    Activator.handleError(e1.getMessage(), e1, true);
                }
                return Status.OK_STATUS;
            }

            @Override
            public boolean belongsTo(Object family) {
                return family == JobFamilies.ADD_TO_INDEX;
            }
        };
        schedule(addJob, true);
    }
    if (!rmPaths.isEmpty()) {
        Job removeJob = new Job(UIText.StagingView_RemoveJob) {

            @Override
            protected IStatus run(IProgressMonitor monitor) {
                try (Git git = new Git(repository)) {
                    RmCommand rm = git.rm().setCached(true);
                    for (String rmPath : rmPaths) rm.addFilepattern(rmPath);
                    rm.call();
                } catch (NoFilepatternException e) {
                // cannot happen
                } catch (JGitInternalException e) {
                    Activator.handleError(e.getCause().getMessage(), e.getCause(), true);
                } catch (Exception e) {
                    Activator.handleError(e.getMessage(), e, true);
                }
                return Status.OK_STATUS;
            }

            @Override
            public boolean belongsTo(Object family) {
                return family == JobFamilies.REMOVE_FROM_INDEX;
            }
        };
        schedule(removeJob, true);
    }
}
Also used : ArrayList(java.util.ArrayList) NoFilepatternException(org.eclipse.jgit.api.errors.NoFilepatternException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) NoFilepatternException(org.eclipse.jgit.api.errors.NoFilepatternException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) ExecutionException(org.eclipse.core.commands.ExecutionException) Repository(org.eclipse.jgit.lib.Repository) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Git(org.eclipse.jgit.api.Git) Iterator(java.util.Iterator) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) ArrayList(java.util.ArrayList) List(java.util.List) RmCommand(org.eclipse.jgit.api.RmCommand) WorkbenchJob(org.eclipse.ui.progress.WorkbenchJob) CommitJob(org.eclipse.egit.ui.internal.commit.CommitJob) Job(org.eclipse.core.runtime.jobs.Job) IResource(org.eclipse.core.resources.IResource) AddCommand(org.eclipse.jgit.api.AddCommand)

Example 20 with RepositoryMapping

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

the class StagingView method showResource.

private void showResource(final IResource resource) {
    if (resource == null || !resource.isAccessible()) {
        return;
    }
    Job.getJobManager().cancel(JobFamilies.UPDATE_SELECTION);
    Job job = new Job(UIText.StagingView_GetRepo) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            if (monitor.isCanceled()) {
                return Status.CANCEL_STATUS;
            }
            RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
            if (mapping != null) {
                Repository newRep = mapping.getRepository();
                if (newRep != null && newRep != currentRepository) {
                    if (monitor.isCanceled()) {
                        return Status.CANCEL_STATUS;
                    }
                    reload(newRep);
                }
            }
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            return JobFamilies.UPDATE_SELECTION == family;
        }

        @Override
        public boolean shouldRun() {
            return shouldUpdateSelection();
        }
    };
    job.setSystem(true);
    schedule(job, false);
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) WorkbenchJob(org.eclipse.ui.progress.WorkbenchJob) CommitJob(org.eclipse.egit.ui.internal.commit.CommitJob) Job(org.eclipse.core.runtime.jobs.Job)

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