Example usage for org.eclipse.jgit.lib StoredConfig getSubsections

List of usage examples for org.eclipse.jgit.lib StoredConfig getSubsections

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib StoredConfig getSubsections.

Prototype

public Set<String> getSubsections(String section) 

Source Link

Document

Get set of all subsections of specified section within this configuration and its base configuration

Usage

From source file:org.eclipse.egit.ui.view.repositories.GitRepositoriesViewRemoteHandlingTest.java

License:Open Source License

private void removeRemotesConfig(File file) throws Exception {
    Repository repo = lookupRepository(file);
    StoredConfig config = repo.getConfig();
    for (String remote : config.getSubsections("remote"))
        config.unsetSection("remote", remote);
    config.save();//w w w  .  java 2  s.  c o m
    waitInUI();
}

From source file:org.eclipse.mylyn.internal.gerrit.core.egit.GerritToGitMappingTest.java

License:Open Source License

private Repository createRepository(String project) {
    StoredConfig config = mock(StoredConfig.class);
    Set<String> configSubSections = new HashSet<String>();
    String remoteName = "remotename"; //$NON-NLS-1$
    configSubSections.add(remoteName);/*from w  ww.j a  va2  s  .c  o m*/
    String remoteSection = "remote"; //$NON-NLS-1$
    when(config.getSubsections(remoteSection)).thenReturn(configSubSections);
    when(config.getStringList(eq(remoteSection), eq(remoteName), anyString())).thenReturn(new String[0]);
    when(config.getStringList(eq(remoteSection), eq(remoteName), matches("url"))).thenReturn( //$NON-NLS-1$
            new String[] { "git://" + GERRIT_GIT_HOST + "/" + project }); //$NON-NLS-1$//$NON-NLS-2$
    Repository repo = mock(Repository.class);
    when(repo.getConfig()).thenReturn(config);
    return repo;
}

From source file:org.jboss.tools.openshift.egit.internal.test.EGitUtilsTest.java

License:Open Source License

@Test
public void canAddRemoteRepo() throws Exception {
    Repository repository = testRepository.getRepository();
    String remoteName = "redhat";
    String gitUri = "www.redhat.com";
    EGitUtils.addRemoteTo(remoteName, gitUri, repository);

    StoredConfig config = repository.getConfig();
    Set<String> subsections = config.getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION);
    assertEquals(1, subsections.size());
    assertTrue(subsections.contains(remoteName));
    assertEquals(gitUri, config.getString(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName,
            ConfigConstants.CONFIG_KEY_URL));
}

From source file:org.jboss.tools.openshift.test.util.ResourceMocks.java

License:Open Source License

public static org.eclipse.core.resources.IProject mockGitSharedProject(String name, String gitRemoteUri)
        throws CoreException {
    org.eclipse.core.resources.IProject project = createEclipseProject(name);

    when(project.getPersistentProperty(TeamPlugin.PROVIDER_PROP_KEY)).thenReturn(GitProvider.ID);

    when(project.getWorkingLocation(any()))
            .thenReturn(new Path(ResourcesPlugin.getWorkspace().getRoot().getFullPath().toString()));

    StoredConfig config = mock(StoredConfig.class);
    when(config.getSubsections("remote")).thenReturn(new HashSet<String>(Arrays.asList("origin")));
    when(config.getStringList(any(), any(), any())).thenReturn(new String[] { gitRemoteUri });
    when(config.getStringList("remote", "origin", "url")).thenReturn(new String[] { gitRemoteUri });

    Repository repository = mock(Repository.class);
    when(repository.getConfig()).thenReturn(config);

    RepositoryMapping mapping = mock(RepositoryMapping.class);
    when(mapping.getRepository()).thenReturn(repository);

    GitProjectData data = mock(GitProjectData.class);
    when(data.getRepositoryMapping(project)).thenReturn(mapping);

    GitProvider repositoryProvider = mock(GitProvider.class);
    when(repositoryProvider.getID()).thenReturn(GitProvider.ID);
    when(repositoryProvider.getData()).thenReturn(data);
    when(project.getSessionProperty(TeamPlugin.PROVIDER_PROP_KEY)).thenReturn(repositoryProvider);

    return project;
}

From source file:org.kie.eclipse.navigator.view.server.KieRepositoryHandler.java

License:Open Source License

public Object load() {
    if (repository == null) {
        final File repoRoot = new File(PreferencesUtils.getRepoRoot(this));
        final Set<File> gitDirs = new HashSet<File>();
        final IRunnableWithProgress runnable = new IRunnableWithProgress() {

            @Override//  w w w .  ja  va 2 s.c o m
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.beginTask("Searching for Repositories", IProgressMonitor.UNKNOWN);
                try {
                    findGitDirsRecursive(repoRoot, gitDirs, monitor, false);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                if (monitor.isCanceled()) {
                    throw new InterruptedException();
                }
            }
        };
        IProgressService ps = PlatformUI.getWorkbench().getProgressService();
        try {
            ps.busyCursorWhile(runnable);
        } catch (InvocationTargetException | InterruptedException e) {
            e.printStackTrace();
        }

        for (File dir : gitDirs) {
            if (getName().equals(dir.getParentFile().getName())) {
                try {
                    Repository repository = repositoryCache.lookupRepository(dir);
                    StoredConfig storedConfig = repository.getConfig();
                    Set<String> remotes = storedConfig.getSubsections("remote");
                    for (String remoteName : remotes) {
                        String url = storedConfig.getString("remote", remoteName, "url");
                        System.out.println(repository.getDirectory());
                        System.out.println(url);
                        try {
                            URI u = new URI(url);
                            int port = u.getPort();
                            String host = u.getHost();
                            String scheme = u.getScheme();
                            String path[] = u.getPath().split("/");
                            String repoName = path[path.length - 1];
                            if (name.equals(repoName) && host.equals(getServer().getHost())
                                    && port == getDelegate().getGitPort() && "ssh".equals(scheme)) {
                                this.repository = repository;
                                break;
                            }
                        } catch (URISyntaxException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        if (repository != null)
            // TODO: why doesn't this work?
            repository.getListenerList().addListener(ConfigChangedListener.class, this);

    }
    return repository;
}

From source file:org.kie.eclipse.server.KieRepositoryHandler.java

License:Open Source License

@Override
public Object load() {
    if (repository == null) {
        try {//from w w  w.j ava2  s .co m
            final File repoRoot = new File(PreferencesUtils.getRepoRoot(this));
            final Set<File> gitDirs = new HashSet<File>();
            GitUtils.findGitDirsRecursive(repoRoot, gitDirs, false);
            for (File dir : gitDirs) {
                if (getName().equals(dir.getParentFile().getName())) {
                    Git git = Git.open(dir);
                    Repository repository = git.getRepository();
                    StoredConfig storedConfig = repository.getConfig();
                    Set<String> remotes = storedConfig.getSubsections("remote");
                    for (String remoteName : remotes) {
                        try {
                            String url = storedConfig.getString("remote", remoteName, "url");
                            URI uri = new URI(url);
                            int port = uri.getPort();
                            String host = uri.getHost();
                            String scheme = uri.getScheme();
                            String path[] = uri.getPath().split("/");
                            String repoName = path[path.length - 1];
                            if (name.equals(repoName) && host.equals(getServer().getHost())
                                    && port == getDelegate().getGitPort() && "ssh".equals(scheme)) {
                                this.repository = repository;
                                break;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            if (git != null) {
                                git.close();
                                git = null;
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return repository;
}

From source file:pt.up.fe.specs.psfbuilder.GitBranch.java

License:Apache License

public static GitBranch newInstance(File location) {
    FileRepositoryBuilder repoBuilder = new FileRepositoryBuilder().findGitDir(location.getAbsoluteFile());

    if (repoBuilder.getGitDir() == null) {
        throw new RuntimeException("Could not find a git repository for folder '"
                + SpecsIo.getWorkingDir().getAbsolutePath() + "'");
    }//from  w w w .  jav  a 2s  . co  m

    // Open an existing repository
    try (Repository repo = repoBuilder.build()) {
        StoredConfig config = repo.getConfig();
        Set<String> remotes = config.getSubsections("remote");

        if (remotes.isEmpty()) {
            throw new RuntimeException("Could not find a remote in '" + repo.getWorkTree() + "'");
        }

        // Get a remote. Try origin first, if not found, get the first on the list
        String remoteName = getRemoteName(remotes);
        String remote = config.getString("remote", remoteName, "url");

        Set<String> branches = config.getSubsections("branch");
        if (branches.isEmpty()) {
            throw new RuntimeException("Could not find a branch in '" + repo.getWorkTree() + "'");
        }

        String branch = getBranchName(branches);

        return new GitBranch(repo.getWorkTree(), remote, branch);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

}