Search in sources :

Example 56 with RepositoryMapping

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

the class CompareUtils method getIndexTypedElement.

/**
 * Get a typed element for the file in the index.
 *
 * @param baseFile
 * @return typed element
 * @throws IOException
 */
public static ITypedElement getIndexTypedElement(@NonNull final IFile baseFile) throws IOException {
    final RepositoryMapping mapping = RepositoryMapping.getMapping(baseFile);
    if (mapping == null) {
        Activator.error(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, baseFile.getLocation(), null), null);
        return null;
    }
    final Repository repository = mapping.getRepository();
    final String gitPath = mapping.getRepoRelativePath(baseFile);
    final String encoding = CompareCoreUtils.getResourceEncoding(baseFile);
    return getIndexTypedElement(repository, gitPath, encoding);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping)

Example 57 with RepositoryMapping

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

the class GitRepositoriesViewTest method assertProjectIsShared.

private void assertProjectIsShared(String projectName, boolean shouldBeShared) {
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    RepositoryMapping mapping = RepositoryMapping.getMapping(project);
    if (shouldBeShared) {
        assertNotNull(mapping);
        assertNotNull(mapping.getRepository());
    } else
        assertNull(mapping);
}
Also used : RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IProject(org.eclipse.core.resources.IProject)

Example 58 with RepositoryMapping

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

the class SharingWizardTest method erase.

private void erase(String projectName, Set<File> dirs) throws CoreException, IOException {
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    if (project.exists()) {
        RepositoryMapping repo = RepositoryMapping.getMapping(project);
        if (repo != null) {
            IPath gitDirAbsolutePath = repo.getGitDirAbsolutePath();
            File canonicalFile = gitDirAbsolutePath.toFile().getCanonicalFile();
            dirs.add(canonicalFile);
            File workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getCanonicalFile();
            File gitDirParent = canonicalFile.getParentFile();
            if (!(gitDirParent.toString() + File.separator).startsWith(workspacePath.toString() + File.separator))
                if (!(gitDirParent.toString() + File.separator).startsWith(getTestDirectory().getAbsolutePath().toString() + File.separator))
                    fail("Attempting cleanup of directory neither in workspace nor test directory" + canonicalFile);
            new DisconnectProviderOperation(Collections.singleton(project)).execute(null);
        }
        project.close(null);
        project.delete(true, true, null);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) File(java.io.File) DisconnectProviderOperation(org.eclipse.egit.core.op.DisconnectProviderOperation) IProject(org.eclipse.core.resources.IProject)

Example 59 with RepositoryMapping

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

the class DynamicVariablesTest method setUp.

@Before
public void setUp() throws Exception {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    FileUtils.mkdir(new File(root.getLocation().toFile(), "Sub"), true);
    gitDir = new File(new File(root.getLocation().toFile(), "Sub"), Constants.DOT_GIT);
    repository = FileRepositoryBuilder.create(gitDir);
    repository.create();
    project = root.getProject(TEST_PROJECT);
    project.create(null);
    project.open(null);
    IProjectDescription description = project.getDescription();
    description.setLocation(root.getLocation().append(TEST_PROJECT_LOC));
    project.move(description, IResource.FORCE, null);
    project2 = root.getProject(TEST_PROJECT2);
    project2.create(null);
    project2.open(null);
    gitDir2 = new File(project2.getLocation().toFile().getAbsoluteFile(), Constants.DOT_GIT);
    repository2 = FileRepositoryBuilder.create(gitDir2);
    repository2.create();
    RepositoryMapping mapping = RepositoryMapping.create(project, gitDir);
    RepositoryMapping mapping2 = RepositoryMapping.create(project2, gitDir2);
    GitProjectData projectData = new GitProjectData(project);
    GitProjectData projectData2 = new GitProjectData(project2);
    projectData.setRepositoryMappings(Collections.singletonList(mapping));
    projectData.store();
    projectData2.setRepositoryMappings(Collections.singletonList(mapping2));
    projectData2.store();
    GitProjectData.add(project, projectData);
    GitProjectData.add(project2, projectData2);
    RepositoryProvider.map(project, GitProvider.class.getName());
    RepositoryProvider.map(project2, GitProvider.class.getName());
    JGitTestUtil.write(new File(repository.getWorkTree(), TEST_PROJECT + "/" + TEST_FILE), "Some data");
    JGitTestUtil.write(new File(repository2.getWorkTree(), TEST_FILE2), "Some other data");
    project.refreshLocal(IResource.DEPTH_INFINITE, null);
    project2.refreshLocal(IResource.DEPTH_INFINITE, null);
    git = new Git(repository);
    git.add().addFilepattern(".").call();
    git.commit().setMessage("Initial commit").call();
    git = new Git(repository2);
    git.add().addFilepattern(".").call();
    git.commit().setMessage("Initial commit").call();
    git.branchRename().setNewName("main").call();
}
Also used : Git(org.eclipse.jgit.api.Git) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IProjectDescription(org.eclipse.core.resources.IProjectDescription) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) File(java.io.File) GitProjectData(org.eclipse.egit.core.project.GitProjectData) GitProvider(org.eclipse.egit.core.GitProvider) Before(org.junit.Before)

Example 60 with RepositoryMapping

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

the class RepositoryUtil method isIgnored.

/**
 * Checks if existing resource with given path is to be ignored.
 * <p>
 * <b>Note:</b>The check makes sense only for files which exists in the
 * working directory. This method returns false for paths to not existing
 * files or directories.
 *
 * @param path
 *            Path to be checked, file or directory must exist on the disk
 * @return true if the path is either not inside git repository or exists
 *         and matches an ignore rule
 * @throws IOException
 * @since 2.3
 */
public static boolean isIgnored(IPath path) throws IOException {
    RepositoryMapping mapping = RepositoryMapping.getMapping(path);
    if (mapping == null) {
        // Linked resources may not be mapped
        return true;
    }
    Repository repository = mapping.getRepository();
    WorkingTreeIterator treeIterator = IteratorService.createInitialIterator(repository);
    if (treeIterator == null) {
        return true;
    }
    String repoRelativePath = mapping.getRepoRelativePath(path);
    if (repoRelativePath == null || repoRelativePath.isEmpty()) {
        return true;
    }
    try (TreeWalk walk = new TreeWalk(repository)) {
        walk.addTree(treeIterator);
        walk.setFilter(PathFilterGroup.createFromStrings(repoRelativePath));
        while (walk.next()) {
            WorkingTreeIterator workingTreeIterator = walk.getTree(0, WorkingTreeIterator.class);
            if (walk.getPathString().equals(repoRelativePath)) {
                return workingTreeIterator.isEntryIgnored();
            }
            if (workingTreeIterator.getEntryFileMode().equals(FileMode.TREE)) {
                walk.enterSubtree();
            }
        }
    }
    return false;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) WorkingTreeIterator(org.eclipse.jgit.treewalk.WorkingTreeIterator) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

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