Search in sources :

Example 6 with RepositoryUtil

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

the class GitProjectSetCapabilityTest method testImportWhereRepoAlreadyExistsAtDifferentLocation.

@Test
public void testImportWhereRepoAlreadyExistsAtDifferentLocation() throws Exception {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IPath reposPath = new Path(testDirectory.getPath()).append("repo");
    pathsToClean.add(reposPath.toFile());
    IPath repoPath = reposPath.append("existingbutdifferent");
    IProject project = createProject(repoPath, "project");
    project.delete(false, true, null);
    String url = createUrl(repoPath);
    File repoDir = createRepository(repoPath, url, "master");
    IPath otherRepoPath = reposPath.append("other");
    File otherRepoDir = createRepository(otherRepoPath, "other-url", "master");
    RepositoryUtil util = Activator.getDefault().getRepositoryUtil();
    util.addConfiguredRepository(repoDir);
    util.addConfiguredRepository(otherRepoDir);
    String reference = createProjectReference(repoPath, "master", "project");
    addToWorkspace(new String[] { reference });
    IProject imported = root.getProject("project");
    assertEquals("Expected imported project to be from already existing repository", reposPath.append("existingbutdifferent/project").toOSString(), imported.getLocation().toOSString());
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IPath(org.eclipse.core.runtime.IPath) File(java.io.File) IProject(org.eclipse.core.resources.IProject) RepositoryUtil(org.eclipse.egit.core.RepositoryUtil) Test(org.junit.Test)

Example 7 with RepositoryUtil

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

the class GitProjectSetCapabilityTest method testImportFromRepoWithUrlOnlyDifferingInUserName.

@Test
public void testImportFromRepoWithUrlOnlyDifferingInUserName() throws Exception {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IPath reposPath = root.getLocation().append("repos");
    pathsToClean.add(reposPath.toFile());
    IPath repoPath = reposPath.append("repo");
    IProject project = createProject(repoPath, "project");
    project.delete(false, true, null);
    String url = createUrl(repoPath, "ssh", "userName");
    File repoDir = createRepository(repoPath, url, "master");
    RepositoryUtil util = Activator.getDefault().getRepositoryUtil();
    util.addConfiguredRepository(repoDir);
    String reference = createProjectReference(repoPath, "ssh", /* no user */
    null, "master", "project");
    addToWorkspace(new String[] { reference });
    IProject imported = root.getProject("project");
    assertEquals("Expected imported project to be from already existing repository. User name must be ignored in URL.", root.getLocation().append("repos/repo/project"), imported.getLocation());
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IPath(org.eclipse.core.runtime.IPath) File(java.io.File) IProject(org.eclipse.core.resources.IProject) RepositoryUtil(org.eclipse.egit.core.RepositoryUtil) Test(org.junit.Test)

Example 8 with RepositoryUtil

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

the class SelectionForViewsTest method before.

@Before
public void before() throws Exception {
    localRepositoryDir = createProjectAndCommitToRepository();
    remoteRepositoryDir = createRemoteRepository(localRepositoryDir);
    URIish uri = new URIish("file:///" + remoteRepositoryDir.getPath());
    File workdir = new File(getTestDirectory(), "ClonedRepo");
    CloneOperation op = new CloneOperation(uri, true, null, workdir, "refs/heads/master", "origin", 0);
    op.run(null);
    clonedRepositoryDir = new File(workdir, Constants.DOT_GIT);
    RepositoryUtil repoUtil = Activator.getDefault().getRepositoryUtil();
    repoUtil.addConfiguredRepository(localRepositoryDir);
    repoUtil.addConfiguredRepository(clonedRepositoryDir);
    // it's bare
    repoUtil.addConfiguredRepository(remoteRepositoryDir);
    stagingView = TestUtil.showView(StagingView.VIEW_ID);
    reflogView = TestUtil.showView(ReflogView.VIEW_ID);
    rebaseInteractiveView = TestUtil.showView(RebaseInteractiveView.VIEW_ID);
    repoView = TestUtil.showView(RepositoriesView.VIEW_ID);
    RepositoriesView repos = (RepositoriesView) repoView.getViewReference().getView(false);
    repos.setReactOnSelection(true);
    historyView = TestUtil.showHistoryView();
    IHistoryView history = (IHistoryView) historyView.getViewReference().getView(false);
    ((GenericHistoryView) history).setLinkingEnabled(true);
    // Ensure that the git history page is active
    Exception[] exception = { null };
    PlatformUI.getWorkbench().getDisplay().syncExec(() -> {
        try {
            history.showHistoryFor(new RepositoryNode(null, lookupRepository(localRepositoryDir)), true);
        } catch (Exception e) {
            exception[0] = e;
        }
    });
    if (exception[0] != null) {
        throw exception[0];
    }
    waitForRefreshes();
}
Also used : URIish(org.eclipse.jgit.transport.URIish) GenericHistoryView(org.eclipse.team.internal.ui.history.GenericHistoryView) RepositoriesView(org.eclipse.egit.ui.internal.repository.RepositoriesView) RepositoryNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryNode) File(java.io.File) IHistoryView(org.eclipse.team.ui.history.IHistoryView) CloneOperation(org.eclipse.egit.core.op.CloneOperation) RepositoryUtil(org.eclipse.egit.core.RepositoryUtil) Before(org.junit.Before)

Example 9 with RepositoryUtil

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

the class RepositoryMenuUtil method getRepositoryActions.

/**
 * Creates for each configured repository an {@link IAction} that will
 * perform the given {@code action} when invoked.
 *
 * @param includeBare
 *            {@code true} if bare repositories should be included in the
 *            list, {@code false} otherwise
 * @param currentRepoDir
 *            git directory of a repository that is to be marked as
 *            "current"; may be {@code null}.
 * @param action
 *            to perform on the chosen repository
 * @return the (possibly empty) list of actions
 */
public static Collection<IAction> getRepositoryActions(boolean includeBare, @Nullable File currentRepoDir, @NonNull Consumer<Repository> action) {
    RepositoryUtil util = org.eclipse.egit.core.Activator.getDefault().getRepositoryUtil();
    RepositoryCache cache = org.eclipse.egit.core.Activator.getDefault().getRepositoryCache();
    Set<String> repositories = util.getRepositories();
    Map<String, Set<File>> repos = new HashMap<>();
    for (String repo : repositories) {
        File gitDir = new File(repo);
        String name = null;
        try {
            Repository r = cache.lookupRepository(gitDir);
            if (!includeBare && r.isBare()) {
                continue;
            }
            name = util.getRepositoryName(r);
        } catch (IOException e) {
            continue;
        }
        Set<File> files = repos.get(name);
        if (files == null) {
            files = new HashSet<>();
            files.add(gitDir);
            repos.put(name, files);
        } else {
            files.add(gitDir);
        }
    }
    String[] repoNames = repos.keySet().toArray(new String[repos.size()]);
    Arrays.sort(repoNames, CommonUtils.STRING_ASCENDING_COMPARATOR);
    List<IAction> result = new ArrayList<>();
    for (String repoName : repoNames) {
        Set<File> files = repos.get(repoName);
        File[] gitDirs = files.toArray(new File[files.size()]);
        Arrays.sort(gitDirs);
        for (File f : gitDirs) {
            IAction menuItem = new Action(repoName, IAction.AS_RADIO_BUTTON) {

                @Override
                public void run() {
                    try {
                        Repository r = cache.lookupRepository(f);
                        action.accept(r);
                    } catch (IOException e) {
                        Activator.showError(e.getLocalizedMessage(), e);
                    }
                }
            };
            menuItem.setToolTipText(f.getPath());
            if (f.equals(currentRepoDir)) {
                menuItem.setChecked(true);
            }
            result.add(menuItem);
        }
    }
    return result;
}
Also used : IAction(org.eclipse.jface.action.IAction) IWorkbenchAction(org.eclipse.ui.actions.ActionFactory.IWorkbenchAction) Action(org.eclipse.jface.action.Action) HashSet(java.util.HashSet) Set(java.util.Set) IAction(org.eclipse.jface.action.IAction) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RepositoryUtil(org.eclipse.egit.core.RepositoryUtil) Repository(org.eclipse.jgit.lib.Repository) RepositoryCache(org.eclipse.egit.core.RepositoryCache) File(java.io.File)

Example 10 with RepositoryUtil

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

the class LinkHelper method findSelection.

/**
 * TODO javadoc missing
 */
@Override
@SuppressWarnings("unchecked")
public IStructuredSelection findSelection(IEditorInput anInput) {
    if (!(anInput instanceof IURIEditorInput)) {
        return null;
    }
    URI uri = ((IURIEditorInput) anInput).getURI();
    if (// $NON-NLS-1$
    !uri.getScheme().equals("file"))
        return null;
    File file = new File(uri.getPath());
    if (!file.exists())
        return null;
    RepositoryUtil config = Activator.getDefault().getRepositoryUtil();
    List<String> repos = config.getConfiguredRepositories();
    for (String repo : repos) {
        Repository repository;
        try {
            repository = FileRepositoryBuilder.create(new File(repo));
        } catch (IOException e) {
            continue;
        }
        if (repository.isBare())
            continue;
        if (file.getPath().startsWith(repository.getWorkTree().getPath())) {
            RepositoriesViewContentProvider cp = new RepositoriesViewContentProvider();
            RepositoryNode repoNode = new RepositoryNode(null, repository);
            RepositoryTreeNode result = null;
            for (Object child : cp.getChildren(repoNode)) {
                if (child instanceof WorkingDirNode) {
                    result = (WorkingDirNode) child;
                    break;
                }
            }
            if (result == null)
                return null;
            IPath remainingPath = new Path(file.getPath().substring(repository.getWorkTree().getPath().length()));
            for (String segment : remainingPath.segments()) {
                for (Object child : cp.getChildren(result)) {
                    RepositoryTreeNode<File> fileNode;
                    try {
                        fileNode = (RepositoryTreeNode<File>) child;
                    } catch (ClassCastException e) {
                        return null;
                    }
                    if (fileNode.getObject().getName().equals(segment)) {
                        result = fileNode;
                        break;
                    }
                }
            }
            return new StructuredSelection(result);
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IURIEditorInput(org.eclipse.ui.IURIEditorInput) IPath(org.eclipse.core.runtime.IPath) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IOException(java.io.IOException) RepositoriesViewContentProvider(org.eclipse.egit.ui.internal.repository.RepositoriesViewContentProvider) URI(java.net.URI) RepositoryUtil(org.eclipse.egit.core.RepositoryUtil) Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) IFile(org.eclipse.core.resources.IFile)

Aggregations

RepositoryUtil (org.eclipse.egit.core.RepositoryUtil)16 File (java.io.File)8 IOException (java.io.IOException)6 IPath (org.eclipse.core.runtime.IPath)6 Path (org.eclipse.core.runtime.Path)5 Repository (org.eclipse.jgit.lib.Repository)4 IProject (org.eclipse.core.resources.IProject)3 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)3 URIish (org.eclipse.jgit.transport.URIish)3 Before (org.junit.Before)3 ArrayList (java.util.ArrayList)2 CoreException (org.eclipse.core.runtime.CoreException)2 CloneOperation (org.eclipse.egit.core.op.CloneOperation)2 RepositoryNode (org.eclipse.egit.ui.internal.repository.tree.RepositoryNode)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 Test (org.junit.Test)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1