Search in sources :

Example 76 with RepositoryMapping

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

the class GitHistoryPage method buildFilterPaths.

private ArrayList<FilterPath> buildFilterPaths(final IResource[] inResources, final File[] inFiles, final Repository db) throws IllegalStateException {
    final ArrayList<FilterPath> paths;
    if (inResources != null) {
        paths = new ArrayList<>(inResources.length);
        for (final IResource r : inResources) {
            final RepositoryMapping map = RepositoryMapping.getMapping(r);
            if (map == null)
                continue;
            if (db != map.getRepository())
                throw new IllegalStateException(UIText.RepositoryAction_multiRepoSelection);
            if (showAllFilter == ShowFilter.SHOWALLFOLDER) {
                final String path;
                // check here and use the project instead
                if (r.getParent() instanceof IWorkspaceRoot)
                    path = map.getRepoRelativePath(r.getProject());
                else
                    path = map.getRepoRelativePath(r.getParent());
                if (path != null && path.length() > 0)
                    paths.add(new FilterPath(path, false));
            } else if (showAllFilter == ShowFilter.SHOWALLPROJECT) {
                final String path = map.getRepoRelativePath(r.getProject());
                if (path != null && path.length() > 0)
                    paths.add(new FilterPath(path, false));
            } else if (showAllFilter == ShowFilter.SHOWALLREPO) {
            // nothing
            } else /* if (showAllFilter == ShowFilter.SHOWALLRESOURCE) */
            {
                final String path = map.getRepoRelativePath(r);
                if (path != null && path.length() > 0)
                    paths.add(new FilterPath(path, r.getType() == IResource.FILE));
            }
        }
    } else if (inFiles != null) {
        IPath workdirPath = new Path(db.getWorkTree().getPath());
        IPath gitDirPath = new Path(db.getDirectory().getPath());
        int segmentCount = workdirPath.segmentCount();
        paths = new ArrayList<>(inFiles.length);
        for (File file : inFiles) {
            IPath filePath;
            boolean isRegularFile;
            if (showAllFilter == ShowFilter.SHOWALLFOLDER) {
                filePath = new Path(file.getParentFile().getPath());
                isRegularFile = false;
            } else if (showAllFilter == ShowFilter.SHOWALLPROJECT || showAllFilter == ShowFilter.SHOWALLREPO)
                // we don't know of projects here -> treat as SHOWALLREPO
                continue;
            else /* if (showAllFilter == ShowFilter.SHOWALLRESOURCE) */
            {
                filePath = new Path(file.getPath());
                isRegularFile = file.isFile();
            }
            if (gitDirPath.isPrefixOf(filePath))
                throw new IllegalStateException(NLS.bind(UIText.GitHistoryPage_FileOrFolderPartOfGitDirMessage, filePath.toOSString()));
            IPath pathToAdd = filePath.removeFirstSegments(segmentCount).setDevice(null);
            if (!pathToAdd.isEmpty())
                paths.add(new FilterPath(pathToAdd.toString(), isRegularFile));
        }
    } else
        paths = new ArrayList<>(0);
    return paths;
}
Also used : Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IPath(org.eclipse.core.runtime.IPath) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) ArrayList(java.util.ArrayList) File(java.io.File) IResource(org.eclipse.core.resources.IResource)

Example 77 with RepositoryMapping

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

the class OpenInTextEditorHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = getSelection(event);
    if (selection.size() < 1)
        return null;
    Object input = getPage(event).getInputInternal().getSingleFile();
    if (input == null)
        return null;
    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, commitPath, commit.getId()), e);
                    errorOccurred = true;
                }
                if (rev != null) {
                    try {
                        EgitUiEditorUtils.openTextEditor(getPart(event).getSite().getPage(), rev, null);
                    } catch (CoreException e) {
                        Activator.logError(e.getMessage(), e);
                        errorOccurred = true;
                    }
                } else {
                    ids.add(commit.getId());
                }
            }
        }
    }
    if (input instanceof File) {
        Repository repo = getRepository(event);
        File fileInput = (File) input;
        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)
                try {
                    EgitUiEditorUtils.openTextEditor(getPart(event).getSite().getPage(), rev, null);
                } catch (CoreException e) {
                    Activator.logError(e.getMessage(), 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 : IFile(org.eclipse.core.resources.IFile) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) IFileRevision(org.eclipse.team.core.history.IFileRevision) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IOException(java.io.IOException) Repository(org.eclipse.jgit.lib.Repository) CoreException(org.eclipse.core.runtime.CoreException) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) File(java.io.File) IFile(org.eclipse.core.resources.IFile) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 78 with RepositoryMapping

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

the class CreatePatchOperation method computeWorkspacePath.

/**
 * Returns a workspace path
 *
 * @param path
 * @param project
 * @return path
 */
public static IPath computeWorkspacePath(final IPath path, final IProject project) {
    RepositoryMapping rm = RepositoryMapping.getMapping(project);
    if (rm == null) {
        return path;
    }
    String repoRelativePath = rm.getRepoRelativePath(project);
    // the relative path cannot be determined, return unchanged
    if (repoRelativePath == null)
        return path;
    // repository and project at the same level
    if (// $NON-NLS-1$
    repoRelativePath.equals(""))
        return path;
    return path.removeFirstSegments(path.matchingFirstSegments(new Path(repoRelativePath)));
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping)

Example 79 with RepositoryMapping

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

the class GitResourceVariantTreeSubscriber method refresh.

@Override
public void refresh(IResource[] resources, int depth, IProgressMonitor monitor) throws TeamException {
    for (IResource resource : resources) {
        // check to see if there is a full refresh
        if (resource.getType() == IResource.ROOT) {
            // refresh entire cache
            cache = GitSyncCache.getAllData(gsds, monitor);
            super.refresh(resources, depth, monitor);
            return;
        }
    }
    // not refreshing the workspace, locate and collect target resources
    Map<GitSynchronizeData, Collection<String>> updateRequests = new HashMap<GitSynchronizeData, Collection<String>>();
    for (IResource resource : resources) {
        IProject project = resource.getProject();
        GitSynchronizeData data = gsds.getData(project.getName());
        if (data != null) {
            RepositoryMapping mapping = RepositoryMapping.getMapping(project);
            // mapping may be null if the project has been closed
            if (mapping != null) {
                Collection<String> paths = updateRequests.get(data);
                if (paths == null) {
                    paths = new ArrayList<String>();
                    updateRequests.put(data, paths);
                }
                String path = mapping.getRepoRelativePath(resource);
                // null path may be returned, check for this
                if (path == null)
                    // unknown, force a refresh of the whole repository
                    // $NON-NLS-1$
                    path = "";
                paths.add(path);
            }
        }
    }
    // scan only the repositories that were affected
    if (!updateRequests.isEmpty()) {
        // refresh cache
        GitSyncCache.mergeAllDataIntoCache(updateRequests, monitor, cache);
    }
    super.refresh(resources, depth, monitor);
}
Also used : GitSynchronizeData(org.eclipse.egit.core.synchronize.dto.GitSynchronizeData) HashMap(java.util.HashMap) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) Collection(java.util.Collection) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject)

Example 80 with RepositoryMapping

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

the class GitSubscriberMergeContext method handleResourceChange.

private void handleResourceChange(IResourceDelta delta) {
    IResourceDelta[] children = delta.getAffectedChildren();
    for (IResourceDelta resourceDelta : children) {
        IResource resource = resourceDelta.getResource();
        RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
        if (mapping == null)
            continue;
        scanDeltaAndRefresh(mapping, resourceDelta);
    }
}
Also used : RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IResource(org.eclipse.core.resources.IResource) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

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