Search in sources :

Example 31 with RepositoryMapping

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

the class SubmoduleFolderTest method testChildProjectMapsToSubRepo.

@Test
public void testChildProjectMapsToSubRepo() {
    RepositoryMapping mapping = RepositoryMapping.getMapping(childProject);
    assertNotNull("Child project should have a mapping", mapping);
    assertEquals(subRepository, mapping.getRepository());
}
Also used : RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) Test(org.junit.Test)

Example 32 with RepositoryMapping

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

the class SubmoduleFolderTest method testParentFolderMapsToParentRepo.

@Test
public void testParentFolderMapsToParentRepo() {
    RepositoryMapping mapping = RepositoryMapping.getMapping(childFolder.getParent());
    assertNotNull("Child folder's parent should have a mapping", mapping);
    assertEquals(parentRepository, mapping.getRepository());
}
Also used : RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) Test(org.junit.Test)

Example 33 with RepositoryMapping

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

the class GitDocument method populate.

void populate() throws IOException {
    if (GitTraceLocation.QUICKDIFF.isActive())
        GitTraceLocation.getTrace().traceEntry(GitTraceLocation.QUICKDIFF.getLocation(), resource);
    // Do not populate if already disposed
    if (disposed)
        return;
    RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
    if (mapping == null) {
        // $NON-NLS-1$
        setResolved(null, null, null, "");
        return;
    }
    final String gitPath = mapping.getRepoRelativePath(resource);
    if (gitPath == null) {
        // $NON-NLS-1$
        setResolved(null, null, null, "");
        return;
    }
    final Repository repository = mapping.getRepository();
    String baseline = GitQuickDiffProvider.baseline.get(repository);
    if (baseline == null)
        baseline = Constants.HEAD;
    ObjectId commitId = repository.resolve(baseline);
    if (commitId != null) {
        if (commitId.equals(lastCommit)) {
            if (GitTraceLocation.QUICKDIFF.isActive())
                GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
                "(GitDocument) already resolved");
            return;
        }
    } else {
        if (repository.exactRef(Constants.HEAD) == null) {
            // Complain only if not an unborn branch
            String msg = NLS.bind(UIText.GitDocument_errorResolveQuickdiff, new Object[] { baseline, resource, repository });
            Activator.logError(msg, new Throwable());
        }
        // $NON-NLS-1$
        setResolved(null, null, null, "");
        return;
    }
    RevCommit baselineCommit;
    String oldPath = gitPath;
    try (RevWalk rw = new RevWalk(repository);
        ObjectReader reader = repository.newObjectReader()) {
        baselineCommit = rw.parseCommit(commitId);
        DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
        if (diffConfig.getRenameDetectionType() != RenameDetectionType.FALSE) {
            TreeWalk walk = new TreeWalk(repository);
            CanonicalTreeParser baseLineIterator = new CanonicalTreeParser();
            baseLineIterator.reset(reader, baselineCommit.getTree());
            walk.addTree(baseLineIterator);
            walk.addTree(new DirCacheIterator(repository.readDirCache()));
            List<DiffEntry> diffs = DiffEntry.scan(walk, true);
            RenameDetector renameDetector = new RenameDetector(repository);
            renameDetector.addAll(diffs);
            List<DiffEntry> renames = renameDetector.compute();
            for (DiffEntry e : renames) {
                if (e.getNewPath().equals(gitPath)) {
                    oldPath = e.getOldPath();
                    break;
                }
            }
        }
    } catch (IOException err) {
        String msg = NLS.bind(UIText.GitDocument_errorLoadCommit, new Object[] { commitId, baseline, resource, repository });
        Activator.logError(msg, err);
        // $NON-NLS-1$
        setResolved(null, null, null, "");
        return;
    }
    RevTree treeId = baselineCommit.getTree();
    if (treeId.equals(lastTree)) {
        if (GitTraceLocation.QUICKDIFF.isActive())
            GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
            "(GitDocument) already resolved");
        return;
    }
    try (TreeWalk tw = TreeWalk.forPath(repository, oldPath, treeId)) {
        if (tw == null) {
            if (GitTraceLocation.QUICKDIFF.isActive())
                GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                "(GitDocument) resource " + resource + " not found in " + treeId + " in " + repository + ", baseline=" + baseline);
            // $NON-NLS-1$
            setResolved(null, null, null, "");
            return;
        }
        ObjectId id = tw.getObjectId(0);
        if (id.equals(ObjectId.zeroId())) {
            // $NON-NLS-1$
            setResolved(null, null, null, "");
            String msg = NLS.bind(UIText.GitDocument_errorLoadTree, new Object[] { treeId.getName(), baseline, resource, repository });
            Activator.logError(msg, new Throwable());
            // $NON-NLS-1$
            setResolved(null, null, null, "");
            return;
        }
        if (!id.equals(lastBlob)) {
            if (GitTraceLocation.QUICKDIFF.isActive())
                GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
                "(GitDocument) compareTo: " + baseline);
            ObjectLoader loader = repository.open(id, Constants.OBJ_BLOB);
            byte[] bytes = loader.getBytes();
            String charset;
            charset = CompareCoreUtils.getResourceEncoding(resource);
            // Finally we could consider validating the content with respect
            // to the content. We don't do that here.
            String s = new String(bytes, charset);
            setResolved(commitId, treeId, id, s);
            if (GitTraceLocation.QUICKDIFF.isActive())
                GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$ //$NON-NLS-2$
                "(GitDocument) has reference doc, size=" + s.length() + " bytes");
        } else {
            if (GitTraceLocation.QUICKDIFF.isActive())
                GitTraceLocation.getTrace().trace(GitTraceLocation.QUICKDIFF.getLocation(), // $NON-NLS-1$
                "(GitDocument) already resolved");
        }
    } finally {
        if (GitTraceLocation.QUICKDIFF.isActive()) {
            GitTraceLocation.getTrace().traceExit(GitTraceLocation.QUICKDIFF.getLocation());
        }
    }
}
Also used : AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffConfig(org.eclipse.jgit.diff.DiffConfig) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) Repository(org.eclipse.jgit.lib.Repository) RenameDetector(org.eclipse.jgit.diff.RenameDetector) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 34 with RepositoryMapping

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

the class AddCommand method autoShareProjects.

private void autoShareProjects(File repositoryDir) {
    // Don't even try to auto-share for bare repositories.
    IPath workingDirPath;
    try {
        Repository repo = Activator.getDefault().getRepositoryCache().lookupRepository(repositoryDir);
        if (repo.isBare()) {
            return;
        }
        workingDirPath = new Path(repo.getWorkTree().getAbsolutePath());
    } catch (IOException e) {
        org.eclipse.egit.ui.Activator.logError(e.getLocalizedMessage(), e);
        return;
    }
    Map<IProject, File> connections = new HashMap<>();
    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    for (IProject project : projects) {
        // Skip closed projects
        if (!project.isAccessible()) {
            continue;
        }
        RepositoryProvider provider = RepositoryProvider.getProvider(project);
        if (provider != null) {
            continue;
        }
        IPath location = project.getLocation();
        if (location == null) {
            continue;
        }
        // even search for a mapping.
        if (!workingDirPath.isPrefixOf(location)) {
            continue;
        }
        RepositoryFinder f = new RepositoryFinder(project);
        f.setFindInChildren(false);
        try {
            List<RepositoryMapping> mappings = f.find(new NullProgressMonitor());
            if (!mappings.isEmpty()) {
                // Connect to the first one; it's the innermost.
                IPath gitDir = mappings.get(0).getGitDirAbsolutePath();
                if (gitDir != null) {
                    connections.put(project, gitDir.toFile());
                }
            }
        } catch (CoreException e) {
            // Ignore this project in that case
            continue;
        }
    }
    if (!connections.isEmpty()) {
        ConnectProviderOperation operation = new ConnectProviderOperation(connections);
        operation.setRefreshResources(false);
        JobUtil.scheduleUserJob(operation, CoreText.Activator_AutoShareJobName, JobFamilies.AUTO_SHARE);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) RepositoryFinder(org.eclipse.egit.core.project.RepositoryFinder) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) Repository(org.eclipse.jgit.lib.Repository) CoreException(org.eclipse.core.runtime.CoreException) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) ConnectProviderOperation(org.eclipse.egit.core.op.ConnectProviderOperation) File(java.io.File) RepositoryProvider(org.eclipse.team.core.RepositoryProvider)

Example 35 with RepositoryMapping

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

the class GitSynchronizeData method setIncludedResources.

/**
 * @param includedResources
 *            list of resources to be synchronized
 */
public void setIncludedResources(Set<IResource> includedResources) {
    this.includedResources = includedResources;
    Set<String> paths = new HashSet<>();
    RepositoryMapping rm = RepositoryMapping.findRepositoryMapping(repo);
    if (rm != null) {
        for (IResource resource : includedResources) {
            String repoRelativePath = rm.getRepoRelativePath(resource);
            if (repoRelativePath != null && repoRelativePath.length() > 0)
                paths.add(repoRelativePath);
        }
    }
    if (!paths.isEmpty())
        pathFilter = PathFilterGroup.createFromStrings(paths);
}
Also used : RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

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