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:jetbrains.buildServer.buildTriggers.vcs.git.agent.UpdaterImpl.java

License:Apache License

protected void removeUrlSections() throws VcsException {
    Repository r = null;/*  w  w  w  .  j  a v  a  2s .  c om*/
    try {
        r = new RepositoryBuilder().setWorkTree(myTargetDirectory).build();
        StoredConfig config = r.getConfig();
        Set<String> urlSubsections = config.getSubsections("url");
        for (String subsection : urlSubsections) {
            config.unsetSection("url", subsection);
        }
        config.save();
    } catch (IOException e) {
        String msg = "Error while remove url.* sections";
        LOG.error(msg, e);
        throw new VcsException(msg, e);
    } finally {
        if (r != null)
            r.close();
    }
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.tests.AgentVcsSupportTest.java

License:Apache License

public void stop_use_any_mirror_if_agent_property_changed_to_false() throws Exception {
    AgentRunningBuild build2 = createRunningBuild(false);
    GitVcsRoot root = new GitVcsRoot(myBuilder.getMirrorManager(), myRoot);
    myVcsSupport.updateSources(myRoot, new CheckoutRules(""), GitVcsSupportTest.VERSION_TEST_HEAD,
            myCheckoutDir, build2, false);

    //add some mirror
    Repository r = new RepositoryBuilder().setWorkTree(myCheckoutDir).build();
    StoredConfig config = r.getConfig();
    config.setString("url", "/some/path", "insteadOf", root.getRepositoryFetchURL().toString());
    config.save();/*from   ww w  .  j a  v a 2  s.  com*/

    myVcsSupport.updateSources(myRoot, new CheckoutRules(""), GitVcsSupportTest.VERSION_TEST_HEAD,
            myCheckoutDir, build2, false);
    config = new RepositoryBuilder().setWorkTree(myCheckoutDir).build().getConfig();
    assertTrue(config.getSubsections("url").isEmpty());
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.tests.AgentVcsSupportTest.java

License:Apache License

public void stop_using_mirrors_when_mirrors_are_disabled_in_vcs_root_option() throws Exception {
    myRoot = vcsRoot().withAgentGitPath(getGitPath()).withFetchUrl(GitUtils.toURL(myMainRepo))
            .withUseMirrors(true).build();
    AgentRunningBuild build1 = createRunningBuild(map(PluginConfigImpl.VCS_ROOT_MIRRORS_STRATEGY,
            PluginConfigImpl.VCS_ROOT_MIRRORS_STRATEGY_MIRRORS_ONLY));
    myVcsSupport.updateSources(myRoot, CheckoutRules.DEFAULT, "2276eaf76a658f96b5cf3eb25f3e1fda90f6b653",
            myCheckoutDir, build1, false);

    myRoot = vcsRoot().withAgentGitPath(getGitPath()).withFetchUrl(GitUtils.toURL(myMainRepo))
            .withUseMirrors(false).build();
    AgentRunningBuild build2 = createRunningBuild(false);
    myVcsSupport.updateSources(myRoot, CheckoutRules.DEFAULT, "2276eaf76a658f96b5cf3eb25f3e1fda90f6b653",
            myCheckoutDir, build2, false);

    StoredConfig config = new RepositoryBuilder().setWorkTree(myCheckoutDir).build().getConfig();
    assertTrue(config.getSubsections("url").isEmpty());
}

From source file:org.eclipse.che.git.impl.jgit.JGitConfigImpl.java

License:Open Source License

@Override
public List<String> getList() throws GitException {
    List<String> results = new ArrayList<>();
    // Iterate all sections and subsections, printing all values
    StoredConfig config = repository.getConfig();
    for (String section : config.getSections()) {
        for (String subsection : config.getSubsections(section)) {
            Set<String> names = config.getNames(section, subsection);
            addConfigValues(section, subsection, names, results);
        }//from   w w  w . java 2  s.com
        Set<String> names = config.getNames(section);
        addConfigValues(section, null, names, results);
    }
    return results;
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public void remoteAdd(RemoteAddRequest request) throws GitException {
    String remoteName = request.getName();
    if (isNullOrEmpty(remoteName)) {
        throw new IllegalArgumentException(ERROR_ADD_REMOTE_NAME_MISSING);
    }//from ww w .ja v  a  2 s.  c o  m

    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = config.getSubsections("remote");
    if (remoteNames.contains(remoteName)) {
        throw new IllegalArgumentException(String.format(ERROR_REMOTE_NAME_ALREADY_EXISTS, remoteName));
    }

    String url = request.getUrl();
    if (isNullOrEmpty(url)) {
        throw new IllegalArgumentException(ERROR_REMOTE_URL_MISSING);
    }

    RemoteConfig remoteConfig;
    try {
        remoteConfig = new RemoteConfig(config, remoteName);
    } catch (URISyntaxException exception) {
        // Not happen since it is newly created remote.
        throw new GitException(exception.getMessage(), exception);
    }

    try {
        remoteConfig.addURI(new URIish(url));
    } catch (URISyntaxException exception) {
        throw new IllegalArgumentException("Remote url " + url + " is invalid. ");
    }

    List<String> branches = request.getBranches();
    if (branches.isEmpty()) {
        remoteConfig.addFetchRefSpec(
                new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*")
                        .setForceUpdate(true));
    } else {
        for (String branch : branches) {
            remoteConfig.addFetchRefSpec(new RefSpec(
                    Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remoteName + "/" + branch)
                            .setForceUpdate(true));
        }
    }

    remoteConfig.update(config);

    try {
        config.save();
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public void remoteDelete(String name) throws GitException {
    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE);
    if (!remoteNames.contains(name)) {
        throw new GitException("error: Could not remove config section 'remote." + name + "'");
    }//from w  w w.j a v  a 2s. co  m

    config.unsetSection(ConfigConstants.CONFIG_REMOTE_SECTION, name);
    Set<String> branches = config.getSubsections(ConfigConstants.CONFIG_BRANCH_SECTION);

    for (String branch : branches) {
        String r = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch,
                ConfigConstants.CONFIG_KEY_REMOTE);
        if (name.equals(r)) {
            config.unset(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_REMOTE);
            config.unset(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_MERGE);
            List<Branch> remoteBranches = branchList(newDto(BranchListRequest.class).withListMode("r"));
            for (Branch remoteBranch : remoteBranches) {
                if (remoteBranch.getDisplayName().startsWith(name)) {
                    branchDelete(
                            newDto(BranchDeleteRequest.class).withName(remoteBranch.getName()).withForce(true));
                }
            }
        }
    }

    try {
        config.save();
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public List<Remote> remoteList(RemoteListRequest request) throws GitException {
    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = new HashSet<>(config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE));
    String remote = request.getRemote();

    if (remote != null && remoteNames.contains(remote)) {
        remoteNames.clear();/*from  w ww .  ja  va 2  s.c  o  m*/
        remoteNames.add(remote);
    }

    List<Remote> result = new ArrayList<>(remoteNames.size());
    for (String remoteName : remoteNames) {
        try {
            List<URIish> uris = new RemoteConfig(config, remoteName).getURIs();
            result.add(newDto(Remote.class).withName(remoteName)
                    .withUrl(uris.isEmpty() ? null : uris.get(0).toString()));
        } catch (URISyntaxException exception) {
            throw new GitException(exception.getMessage(), exception);
        }
    }
    return result;
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public void remoteUpdate(RemoteUpdateRequest request) throws GitException {
    String remoteName = request.getName();
    if (isNullOrEmpty(remoteName)) {
        throw new IllegalArgumentException(ERROR_UPDATE_REMOTE_NAME_MISSING);
    }//  w  ww  .  j  a  va 2s.c  o  m

    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE);
    if (!remoteNames.contains(remoteName)) {
        throw new IllegalArgumentException("Remote " + remoteName + " not found. ");
    }

    RemoteConfig remoteConfig;
    try {
        remoteConfig = new RemoteConfig(config, remoteName);
    } catch (URISyntaxException e) {
        throw new GitException(e.getMessage(), e);
    }

    List<String> branches = request.getBranches();
    if (!branches.isEmpty()) {
        if (!request.isAddBranches()) {
            remoteConfig.setFetchRefSpecs(Collections.emptyList());
            remoteConfig.setPushRefSpecs(Collections.emptyList());
        } else {
            // Replace wildcard refSpec if any.
            remoteConfig.removeFetchRefSpec(
                    new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*")
                            .setForceUpdate(true));
            remoteConfig.removeFetchRefSpec(
                    new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*"));
        }

        // Add new refSpec.
        for (String branch : branches) {
            remoteConfig.addFetchRefSpec(new RefSpec(
                    Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remoteName + "/" + branch)
                            .setForceUpdate(true));
        }
    }

    // Remove URLs first.
    for (String url : request.getRemoveUrl()) {
        try {
            remoteConfig.removeURI(new URIish(url));
        } catch (URISyntaxException e) {
            LOG.debug(ERROR_REMOVING_INVALID_URL);
        }
    }

    // Add new URLs.
    for (String url : request.getAddUrl()) {
        try {
            remoteConfig.addURI(new URIish(url));
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Remote url " + url + " is invalid. ");
        }
    }

    // Remove URLs for pushing.
    for (String url : request.getRemovePushUrl()) {
        try {
            remoteConfig.removePushURI(new URIish(url));
        } catch (URISyntaxException e) {
            LOG.debug(ERROR_REMOVING_INVALID_URL);
        }
    }

    // Add URLs for pushing.
    for (String url : request.getAddPushUrl()) {
        try {
            remoteConfig.addPushURI(new URIish(url));
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Remote push url " + url + " is invalid. ");
        }
    }

    remoteConfig.update(config);

    try {
        config.save();
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.egit.ui.internal.push.PushBranchWizardTest.java

License:Open Source License

private void removeExistingRemotes() throws IOException {
    StoredConfig config = repository.getConfig();
    Set<String> remotes = config.getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION);
    for (String remoteName : remotes)
        config.unsetSection(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName);
    config.save();/*from  w  w w . ja  v  a  2s .  co m*/
}

From source file:org.eclipse.egit.ui.internal.repository.RepositoryPropertySource.java

License:Open Source License

public IPropertyDescriptor[] getPropertyDescriptors() {
    try {//from   w w  w  .  j a  v a 2 s  .  c  om
        userHomeConfig.load();
        repositoryConfig.load();
        effectiveConfig.load();
    } catch (IOException e) {
        showExceptionMessage(e);
    } catch (ConfigInvalidException e) {
        showExceptionMessage(e);
    }

    List<IPropertyDescriptor> resultList = new ArrayList<IPropertyDescriptor>();

    StoredConfig config;
    String category;
    String prefix;
    switch (getCurrentMode()) {
    case EFFECTIVE:
        prefix = EFFECTIVE_ID_PREFIX;
        category = UIText.RepositoryPropertySource_EffectiveConfigurationCategory;
        config = effectiveConfig;
        break;
    case REPO: {
        prefix = REPO_ID_PREFIX;
        String location = ""; //$NON-NLS-1$
        if (repositoryConfig instanceof FileBasedConfig) {
            location = ((FileBasedConfig) repositoryConfig).getFile().getAbsolutePath();
        }
        category = NLS.bind(UIText.RepositoryPropertySource_RepositoryConfigurationCategory, location);
        config = repositoryConfig;
        break;
    }
    case USER: {
        prefix = USER_ID_PREFIX;
        String location = userHomeConfig.getFile().getAbsolutePath();
        category = NLS.bind(UIText.RepositoryPropertySource_GlobalConfigurationCategory, location);
        config = userHomeConfig;
        break;
    }
    default:
        return new IPropertyDescriptor[0];
    }
    for (String key : config.getSections()) {
        for (String sectionItem : config.getNames(key)) {
            String sectionId = key + "." + sectionItem; //$NON-NLS-1$
            PropertyDescriptor desc = new PropertyDescriptor(prefix + sectionId, sectionId);
            desc.setCategory(category);
            resultList.add(desc);
        }
        for (String sub : config.getSubsections(key)) {
            for (String sectionItem : config.getNames(key, sub)) {
                String sectionId = key + "." + sub + "." + sectionItem; //$NON-NLS-1$ //$NON-NLS-2$
                PropertyDescriptor desc = new PropertyDescriptor(prefix + sectionId, sectionId);
                desc.setCategory(category);
                resultList.add(desc);
            }
        }
    }

    return resultList.toArray(new IPropertyDescriptor[0]);
}