Search in sources :

Example 61 with RepositoryMapping

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

the class GitMoveDeleteHook method deleteFile.

@Override
public boolean deleteFile(final IResourceTree tree, final IFile file, final int updateFlags, final IProgressMonitor monitor) {
    if (!org.eclipse.egit.core.Activator.autoStageDeletion()) {
        return false;
    }
    // Linked resources are not files, hence not tracked by git
    if (file.isLinked())
        return false;
    final boolean force = (updateFlags & IResource.FORCE) == IResource.FORCE;
    if (!force && !tree.isSynchronized(file, IResource.DEPTH_ZERO))
        return false;
    final RepositoryMapping map = RepositoryMapping.getMapping(file);
    if (map == null)
        return false;
    String repoRelativePath = map.getRepoRelativePath(file);
    IndexDiffCache indexDiffCache = Activator.getDefault().getIndexDiffCache();
    IndexDiffCacheEntry indexDiffCacheEntry = indexDiffCache.getIndexDiffCacheEntry(map.getRepository());
    if (indexDiffCacheEntry == null) {
        return false;
    }
    IndexDiffData indexDiff = indexDiffCacheEntry.getIndexDiff();
    if (indexDiff != null) {
        if (indexDiff.getUntracked().contains(repoRelativePath))
            return false;
        if (indexDiff.getIgnoredNotInIndex().contains(repoRelativePath))
            return false;
    }
    if (!file.exists())
        return false;
    if (file.isDerived())
        return false;
    DirCache dirc = null;
    try {
        dirc = map.getRepository().lockDirCache();
        final int first = dirc.findEntry(repoRelativePath);
        if (first < 0) {
            dirc.unlock();
            return false;
        }
        final DirCacheBuilder edit = dirc.builder();
        if (first > 0)
            edit.keep(0, first);
        final int next = dirc.nextEntry(first);
        if (next < dirc.getEntryCount())
            edit.keep(next, dirc.getEntryCount() - next);
        if (!edit.commit())
            tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0, CoreText.MoveDeleteHook_operationError, null));
        tree.standardDeleteFile(file, updateFlags, monitor);
    } catch (LockFailedException e) {
        // FIXME The index is currently locked. This notably happens during
        // rebase operations. auto-staging deletions should be queued... and
        // the queued job will have to double-check whether the file has
        // truly been deleted or if it was only deleted to be replaced by
        // another version.
        // This hook only exists to automatically add changes to the index.
        // If the index is currently locked, do not accept the
        // responsibility of deleting the file, return false to tell the
        // workspace it can continue with the standard deletion. The user
        // will have to stage the deletion later on _if_ this was truly
        // needed, which won't happen for calls triggered by merge
        // operations from the merge strategies.
        Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.getPluginId(), MessageFormat.format(CoreText.MoveDeleteHook_cannotAutoStageDeletion, file.getLocation())));
        return FINISH_FOR_ME;
    } catch (IOException e) {
        tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0, CoreText.MoveDeleteHook_operationError, e));
    } finally {
        if (dirc != null)
            dirc.unlock();
    }
    return true;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) DirCache(org.eclipse.jgit.dircache.DirCache) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) LockFailedException(org.eclipse.jgit.errors.LockFailedException) IndexDiffCacheEntry(org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IOException(java.io.IOException) IndexDiffCache(org.eclipse.egit.core.internal.indexdiff.IndexDiffCache) IndexDiffData(org.eclipse.egit.core.internal.indexdiff.IndexDiffData)

Example 62 with RepositoryMapping

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

the class GitMoveDeleteHook method mapProject.

private void mapProject(final IProject source, final IProjectDescription description, final IProgressMonitor monitor, IPath gitDir) throws CoreException, TeamException {
    IProject destination = source.getWorkspace().getRoot().getProject(description.getName());
    RepositoryMapping repositoryMapping = RepositoryMapping.create(destination, gitDir.toFile());
    if (repositoryMapping != null) {
        GitProjectData projectData = new GitProjectData(destination);
        projectData.setRepositoryMappings(Arrays.asList(repositoryMapping));
        projectData.store();
        GitProjectData.add(destination, projectData);
        RepositoryProvider.map(destination, GitProvider.class.getName());
        destination.refreshLocal(IResource.DEPTH_INFINITE, monitor);
    }
}
Also used : RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) GitProjectData(org.eclipse.egit.core.project.GitProjectData) IProject(org.eclipse.core.resources.IProject)

Example 63 with RepositoryMapping

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

the class GitMoveDeleteHook method moveProject.

@Override
public boolean moveProject(final IResourceTree tree, final IProject source, final IProjectDescription description, final int updateFlags, final IProgressMonitor monitor) {
    final RepositoryMapping srcm = RepositoryMapping.getMapping(source);
    if (srcm == null)
        return false;
    IPath newLocation = null;
    if (description.getLocationURI() != null)
        newLocation = URIUtil.toPath(description.getLocationURI());
    else
        newLocation = source.getWorkspace().getRoot().getLocation().append(description.getName());
    IPath sourceLocation = source.getLocation();
    // Prevent a serious error.
    if (sourceLocation.isPrefixOf(newLocation) && sourceLocation.segmentCount() != newLocation.segmentCount() && !"true".equals(System.getProperty("egit.assume_307140_fixed"))) {
        // $NON-NLS-1$//$NON-NLS-2$
        // Graceful handling of bug, i.e. refuse to destroy your code
        tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0, // $NON-NLS-1$
        "Cannot move project. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=307140 (not resolved in 3.7)", null));
        return true;
    }
    File newLocationFile = newLocation.toFile();
    // check if new location is below the same repository
    Path workTree = new Path(srcm.getRepository().getWorkTree().getAbsolutePath());
    int matchingFirstSegments = workTree.matchingFirstSegments(newLocation);
    if (matchingFirstSegments == workTree.segmentCount()) {
        return moveProjectHelperMoveOnlyProject(tree, source, description, updateFlags, monitor, srcm, newLocationFile);
    } else {
        int dstAboveSrcRepo = newLocation.matchingFirstSegments(srcm.getGitDirAbsolutePath());
        int srcAboveSrcRepo = sourceLocation.matchingFirstSegments(srcm.getGitDirAbsolutePath());
        if (dstAboveSrcRepo > 0 && srcAboveSrcRepo > 0) {
            return moveProjectHelperMoveRepo(tree, source, description, updateFlags, monitor, srcm, newLocation, sourceLocation);
        } else {
            return FINISH_FOR_ME;
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 64 with RepositoryMapping

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

the class GitProjectSetCapability method asReference.

@Nullable
private String asReference(IProject project) throws TeamException {
    RepositoryMapping mapping = RepositoryMapping.getMapping(project);
    if (mapping == null) {
        return null;
    }
    String branch;
    try {
        branch = mapping.getRepository().getBranch();
    } catch (IOException e) {
        throw new TeamException(NLS.bind(CoreText.GitProjectSetCapability_ExportCouldNotGetBranch, project.getName()));
    }
    StoredConfig config = mapping.getRepository().getConfig();
    String remote = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_REMOTE);
    String url = config.getString(ConfigConstants.CONFIG_REMOTE_SECTION, remote, ConfigConstants.CONFIG_KEY_URL);
    if (url == null)
        throw new TeamException(NLS.bind(CoreText.GitProjectSetCapability_ExportNoRemote, project.getName()));
    String projectPath = mapping.getRepoRelativePath(project);
    if (projectPath == null) {
        return null;
    }
    if (// $NON-NLS-1$
    projectPath.equals(""))
        // $NON-NLS-1$
        projectPath = ".";
    return asReference(url, branch, projectPath);
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) TeamException(org.eclipse.team.core.TeamException) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IOException(java.io.IOException) Nullable(org.eclipse.jgit.annotations.Nullable)

Example 65 with RepositoryMapping

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

the class GitModelSynchronizeParticipant method saveState.

@Override
public void saveState(IMemento memento) {
    super.saveState(memento);
    for (GitSynchronizeData gsd : gsds) {
        Repository repo = gsd.getRepository();
        RepositoryMapping mapping = RepositoryMapping.findRepositoryMapping(repo);
        if (mapping != null) {
            IMemento child = memento.createChild(DATA_NODE_KEY);
            child.putString(CONTAINER_PATH_KEY, getPathForResource(mapping.getContainer()));
            child.putString(SRC_REV_KEY, gsd.getSrcRev());
            child.putString(DST_REV_KEY, gsd.getDstRev());
            child.putBoolean(INCLUDE_LOCAL_KEY, gsd.shouldIncludeLocal());
            Set<IResource> includedResources = gsd.getIncludedResources();
            if (includedResources != null && !includedResources.isEmpty()) {
                IMemento paths = child.createChild(INCLUDED_PATHS_NODE_KEY);
                for (IResource resource : includedResources) {
                    String path = getPathForResource(resource);
                    paths.createChild(INCLUDED_PATH_KEY).putString(INCLUDED_PATH_KEY, path);
                }
            }
        }
    }
    memento.putBoolean(FORCE_FETCH_KEY, gsds.forceFetch());
}
Also used : GitSynchronizeData(org.eclipse.egit.core.synchronize.dto.GitSynchronizeData) Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IMemento(org.eclipse.ui.IMemento) 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