From 6dfbc93f67d7554c472a1c0fc0cc13355383d774 Mon Sep 17 00:00:00 2001 From: Arul Date: Sat, 2 Apr 2022 21:31:05 +0530 Subject: [PATCH] Fix #540 - Take care of system and user config --- .../extra/GitAttributesLineEndings.java | 2 +- .../spotless/extra/GitWorkarounds.java | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib-extra/src/main/java/com/diffplug/spotless/extra/GitAttributesLineEndings.java b/lib-extra/src/main/java/com/diffplug/spotless/extra/GitAttributesLineEndings.java index 4aab23e142..ae9f0ceec1 100644 --- a/lib-extra/src/main/java/com/diffplug/spotless/extra/GitAttributesLineEndings.java +++ b/lib-extra/src/main/java/com/diffplug/spotless/extra/GitAttributesLineEndings.java @@ -172,7 +172,7 @@ static class RuntimeInit { ////////////////////////// // REPO-SPECIFIC VALUES // ////////////////////////// - RepositorySpecificResolver repositoryResolver = GitWorkarounds.fileRepositoryResolverForProject(projectDir); + RepositorySpecificResolver repositoryResolver = GitWorkarounds.fileRepositoryResolverForProject(projectDir, userConfig); if (repositoryResolver.getGitDir() != null) { workTree = repositoryResolver.getWorkTree(); repoConfig = repositoryResolver.getRepositoryConfig(); diff --git a/lib-extra/src/main/java/com/diffplug/spotless/extra/GitWorkarounds.java b/lib-extra/src/main/java/com/diffplug/spotless/extra/GitWorkarounds.java index a66650b38f..15e8cdb5b8 100644 --- a/lib-extra/src/main/java/com/diffplug/spotless/extra/GitWorkarounds.java +++ b/lib-extra/src/main/java/com/diffplug/spotless/extra/GitWorkarounds.java @@ -50,7 +50,7 @@ private GitWorkarounds() {} * @return the path to the .git directory. */ static @Nullable File getDotGitDir(File projectDir) { - return fileRepositoryResolverForProject(projectDir).getGitDir(); + return fileRepositoryResolverForProject(projectDir, null).getGitDir(); } /** @@ -61,8 +61,8 @@ private GitWorkarounds() {} * @param projectDir the project directory. * @return the builder. */ - static RepositorySpecificResolver fileRepositoryResolverForProject(File projectDir) { - RepositorySpecificResolver repositoryResolver = new RepositorySpecificResolver(); + static RepositorySpecificResolver fileRepositoryResolverForProject(File projectDir, Config baseConfig) { + RepositorySpecificResolver repositoryResolver = new RepositorySpecificResolver(baseConfig); repositoryResolver.findGitDir(projectDir); repositoryResolver.readEnvironment(); if (repositoryResolver.getGitDir() != null || repositoryResolver.getWorkTree() != null) { @@ -94,6 +94,16 @@ static class RepositorySpecificResolver extends FileRepositoryBuilder { private File commonDirectory; + private Config baseConfig; + + public RepositorySpecificResolver() { + this(null); + } + + public RepositorySpecificResolver(Config baseConfig) { + this.baseConfig = baseConfig; + } + /** @return the repository specific configuration. */ Config getRepositoryConfig() { return Errors.rethrow().get(this::getConfig); @@ -108,7 +118,12 @@ Config getRepositoryConfig() { protected Config loadConfig() throws IOException { if (getGitDir() != null) { File path = resolveWithCommonDir(Constants.CONFIG); - FileBasedConfig cfg = new FileBasedConfig(path, safeFS()); + FileBasedConfig cfg = null; + if (this.baseConfig == null) { + cfg = new FileBasedConfig(path, safeFS()); + } else { + cfg = new FileBasedConfig(baseConfig, path, safeFS()); + } try { cfg.load();