Example usage for org.eclipse.jgit.lib Config getString

List of usage examples for org.eclipse.jgit.lib Config getString

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Config getString.

Prototype

public String getString(final String section, String subsection, final String name) 

Source Link

Document

Get string value or null if not found.

Usage

From source file:br.edu.ifpb.scm.api.git.Git.java

/**
 * Recupera a url do repositorio remoto//w  w  w .  j  ava  2s  . co  m
 *
 * @param repository {@link org.eclipse.jgit.lib.Repository} JGit
 * @return {@link String} Url do repositorio remoto
 */
private String getUrlFromLocalRepository() {
    Config config = repoJGit.getConfig();
    return config.getString("remote", "origin", "url");
}

From source file:com.buildautomation.jgit.api.PrintRemotes.java

License:Apache License

public static void printRemotes() throws IOException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        Config storedConfig = repository.getConfig();
        Set<String> remotes = storedConfig.getSubsections("remote");

        for (String remoteName : remotes) {
            String url = storedConfig.getString("remote", remoteName, "url");
            System.out.println(remoteName + " " + url);
        }//ww  w  .j  a va2  s . c  om
    }
}

From source file:com.buildautomation.jgit.api.ReadUserConfig.java

License:Apache License

public static void readUserConfig() throws IOException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        Config config = repository.getConfig();
        String name = config.getString("user", null, "name");
        String email = config.getString("user", null, "email");
        if (name == null || email == null) {
            System.out.println("User identity is unknown!");
        } else {//from w  w w  .j av a  2  s .  c  om
            System.out.println("User identity is " + name + " <" + email + ">");
        }

        String url = config.getString("remote", "origin", "url");
        if (url != null) {
            System.out.println("Origin comes from " + url);
        }
    }
}

From source file:com.diffplug.gradle.spotless.GitAttributesLineEndingPolicy.java

License:Apache License

public static GitAttributesLineEndingPolicy create(File rootFolder) {
    return Errors.rethrow().get(() -> {
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        builder.findGitDir(rootFolder);/* w w w  .  ja va2  s .  c o m*/
        if (builder.getGitDir() != null) {
            // we found a repository, so we can grab all the values we need from it
            Repository repo = builder.build();
            AttributesNodeProvider nodeProvider = repo.createAttributesNodeProvider();
            Function<AttributesNode, List<AttributesRule>> getRules = node -> node == null
                    ? Collections.emptyList()
                    : node.getRules();
            return new GitAttributesLineEndingPolicy(repo.getConfig(),
                    getRules.apply(nodeProvider.getInfoAttributesNode()), repo.getWorkTree(),
                    getRules.apply(nodeProvider.getGlobalAttributesNode()));
        } else {
            // there's no repo, so it takes some work to grab the system-wide values
            Config systemConfig = SystemReader.getInstance().openSystemConfig(null, FS.DETECTED);
            Config userConfig = SystemReader.getInstance().openUserConfig(systemConfig, FS.DETECTED);
            if (userConfig == null) {
                userConfig = new Config();
            }

            List<AttributesRule> globalRules = Collections.emptyList();
            // copy-pasted from org.eclipse.jgit.lib.CoreConfig
            String globalAttributesPath = userConfig.getString(ConfigConstants.CONFIG_CORE_SECTION, null,
                    ConfigConstants.CONFIG_KEY_ATTRIBUTESFILE);
            // copy-pasted from org.eclipse.jgit.internal.storage.file.GlobalAttributesNode
            if (globalAttributesPath != null) {
                FS fs = FS.detect();
                File attributesFile;
                if (globalAttributesPath.startsWith("~/")) { //$NON-NLS-1$
                    attributesFile = fs.resolve(fs.userHome(), globalAttributesPath.substring(2));
                } else {
                    attributesFile = fs.resolve(null, globalAttributesPath);
                }
                globalRules = parseRules(attributesFile);
            }
            return new GitAttributesLineEndingPolicy(userConfig,
                    // no git info file
                    Collections.emptyList(), null, globalRules);
        }
    });
}

From source file:com.gitblit.authority.NewCertificateConfig.java

License:Apache License

private NewCertificateConfig(final Config c) {
    duration = c.getInt("new", null, "duration", 0);
    OU = c.getString("new", null, "organizationalUnit");
    O = c.getString("new", null, "organization");
    L = c.getString("new", null, "locality");
    ST = c.getString("new", null, "stateProvince");
    C = c.getString("new", null, "countryCode");
}

From source file:com.gitblit.authority.UserCertificateConfig.java

License:Apache License

private UserCertificateConfig(final Config c) {
    SimpleDateFormat df = new SimpleDateFormat(Constants.ISO8601);
    list = new ArrayList<UserCertificateModel>();
    for (String username : c.getSubsections("user")) {
        UserCertificateModel uc = new UserCertificateModel(new UserModel(username));
        try {/* w w w  . j ava 2 s  .com*/
            uc.expires = df.parse(c.getString("user", username, "expires"));
        } catch (ParseException e) {
            LoggerFactory.getLogger(UserCertificateConfig.class).error("Failed to parse date!", e);
        } catch (NullPointerException e) {
        }
        uc.notes = c.getString("user", username, "notes");
        uc.revoked = new ArrayList<String>(Arrays.asList(c.getStringList("user", username, "revoked")));
        list.add(uc);
    }
}

From source file:com.google.gerrit.acceptance.GerritServer.java

License:Apache License

private GerritServer(Description desc, Injector testInjector, Daemon daemon, ExecutorService daemonService) {
    this.desc = desc;
    this.testInjector = testInjector;
    this.daemon = daemon;
    this.daemonService = daemonService;

    Config cfg = testInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
    url = cfg.getString("gerrit", null, "canonicalWebUrl");
    URI uri = URI.create(url);

    sshdAddress = SocketUtil.resolve(cfg.getString("sshd", null, "listenAddress"), 0);
    httpAddress = new InetSocketAddress(uri.getHost(), uri.getPort());
}

From source file:com.google.gerrit.acceptance.rest.change.ConfigChangeIT.java

License:Apache License

@Test
@TestProjectInput(cloneAs = "user")
public void updateProjectConfig() throws Exception {
    Config cfg = readProjectConfig();
    assertThat(cfg.getString("project", null, "description")).isNull();
    String desc = "new project description";
    cfg.setString("project", null, "description", desc);

    PushOneCommit.Result r = createConfigChange(cfg);
    String id = r.getChangeId();//from www. ja v  a  2s .co  m

    gApi.changes().id(id).current().review(ReviewInput.approve());
    gApi.changes().id(id).current().submit();

    assertThat(gApi.changes().id(id).info().status).isEqualTo(ChangeStatus.MERGED);
    assertThat(gApi.projects().name(project.get()).get().description).isEqualTo(desc);
    fetchRefsMetaConfig();
    assertThat(readProjectConfig().getString("project", null, "description")).isEqualTo(desc);
}

From source file:com.google.gerrit.acceptance.rest.change.ConfigChangeIT.java

License:Apache License

@Test
@TestProjectInput(cloneAs = "user")
public void onlyAdminMayUpdateProjectParent() throws Exception {
    setApiUser(admin);//from w  w  w  . j  av a 2  s .  c o  m
    ProjectInput parent = new ProjectInput();
    parent.name = name("parent");
    parent.permissionsOnly = true;
    gApi.projects().create(parent);

    setApiUser(user);
    Config cfg = readProjectConfig();
    assertThat(cfg.getString("access", null, "inheritFrom")).isAnyOf(null, allProjects.get());
    cfg.setString("access", null, "inheritFrom", parent.name);

    PushOneCommit.Result r = createConfigChange(cfg);
    String id = r.getChangeId();

    gApi.changes().id(id).current().review(ReviewInput.approve());
    try {
        gApi.changes().id(id).current().submit();
        fail("expected submit to fail");
    } catch (ResourceConflictException e) {
        int n = gApi.changes().id(id).info()._number;
        assertThat(e).hasMessage("Failed to submit 1 change due to the following problems:\n" + "Change " + n
                + ": Change contains a project configuration that" + " changes the parent project.\n"
                + "The change must be submitted by a Gerrit administrator.");
    }

    assertThat(gApi.projects().name(project.get()).get().parent).isEqualTo(allProjects.get());
    fetchRefsMetaConfig();
    assertThat(readProjectConfig().getString("access", null, "inheritFrom")).isAnyOf(null, allProjects.get());

    setApiUser(admin);
    gApi.changes().id(id).current().submit();
    assertThat(gApi.changes().id(id).info().status).isEqualTo(ChangeStatus.MERGED);
    assertThat(gApi.projects().name(project.get()).get().parent).isEqualTo(parent.name);
    fetchRefsMetaConfig();
    assertThat(readProjectConfig().getString("access", null, "inheritFrom")).isEqualTo(parent.name);
}

From source file:com.google.gerrit.acceptance.rest.project.AccessIT.java

License:Apache License

@Test
public void unknownPermissionRemainsUnchanged() throws Exception {
    String access = "access";
    String unknownPermission = "unknownPermission";
    String registeredUsers = "group Registered Users";
    String refsFor = "refs/for/*";
    // Clone repository to forcefully add permission
    TestRepository<InMemoryRepository> allProjectsRepo = cloneProject(allProjects, admin);

    // Fetch permission ref
    GitUtil.fetch(allProjectsRepo, "refs/meta/config:cfg");
    allProjectsRepo.reset("cfg");

    // Load current permissions
    String config = gApi.projects().name(allProjects.get()).branch(RefNames.REFS_CONFIG).file("project.config")
            .asString();//from   w  w  w . j av  a2s .co  m

    // Append and push unknown permission
    Config cfg = new Config();
    cfg.fromText(config);
    cfg.setString(access, refsFor, unknownPermission, registeredUsers);
    config = cfg.toText();
    PushOneCommit push = pushFactory.create(db, admin.getIdent(), allProjectsRepo, "Subject", "project.config",
            config);
    push.to(RefNames.REFS_CONFIG).assertOkStatus();

    // Verify that unknownPermission is present
    config = gApi.projects().name(allProjects.get()).branch(RefNames.REFS_CONFIG).file("project.config")
            .asString();
    cfg.fromText(config);
    assertThat(cfg.getString(access, refsFor, unknownPermission)).isEqualTo(registeredUsers);

    // Make permission change through API
    ProjectAccessInput accessInput = newProjectAccessInput();
    AccessSectionInfo accessSectionInfo = createDefaultAccessSectionInfo();
    accessInput.add.put(refsFor, accessSectionInfo);
    gApi.projects().name(allProjects.get()).access(accessInput);
    accessInput.add.clear();
    accessInput.remove.put(refsFor, accessSectionInfo);
    gApi.projects().name(allProjects.get()).access(accessInput);

    // Verify that unknownPermission is still present
    config = gApi.projects().name(allProjects.get()).branch(RefNames.REFS_CONFIG).file("project.config")
            .asString();
    cfg.fromText(config);
    assertThat(cfg.getString(access, refsFor, unknownPermission)).isEqualTo(registeredUsers);
}