Example usage for org.eclipse.jgit.attributes AttributesNodeProvider getGlobalAttributesNode

List of usage examples for org.eclipse.jgit.attributes AttributesNodeProvider getGlobalAttributesNode

Introduction

In this page you can find the example usage for org.eclipse.jgit.attributes AttributesNodeProvider getGlobalAttributesNode.

Prototype

AttributesNode getGlobalAttributesNode() throws IOException;

Source Link

Document

Retrieve the org.eclipse.jgit.attributes.AttributesNode that holds the information located in the global gitattributes file.

Usage

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);//from   w ww .j av a 2s .  co  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);
        }
    });
}