From 6dfbc93f67d7554c472a1c0fc0cc13355383d774 Mon Sep 17 00:00:00 2001 From: Arul Date: Sat, 2 Apr 2022 21:31:05 +0530 Subject: [PATCH 1/3] 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(); From af6a3b388bcc11ad0984fafd9b4bea81b1dd577c Mon Sep 17 00:00:00 2001 From: Arul Date: Mon, 4 Apr 2022 04:23:40 +0530 Subject: [PATCH 2/3] - fixed test cases - updated changelogs --- CHANGES.md | 3 +++ .../diffplug/spotless/extra/GitWorkarounds.java | 15 ++++++++++++++- plugin-gradle/CHANGES.md | 3 +++ plugin-maven/CHANGES.md | 3 +++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 0b2af47551..ea34ee65b3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] +### Fixed +* Git user config and system config also included for defaultEndings configuration. ([#540](https://github.com/diffplug/spotless/issues/540)) + ## [2.24.1] - 2022-03-30 ### Fixed * Fixed access modifiers for setters in KtfmtStep configuration 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 15e8cdb5b8..993c207218 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, null).getGitDir(); + return fileRepositoryResolverForProject(projectDir).getGitDir(); } /** @@ -61,6 +61,19 @@ private GitWorkarounds() {} * @param projectDir the project directory. * @return the builder. */ + static RepositorySpecificResolver fileRepositoryResolverForProject(File projectDir) { + return fileRepositoryResolverForProject(projectDir, null); + } + + /** + * Creates a {@link RepositorySpecificResolver} for the given project directory. + * + * This applies a workaround for JGit not supporting worktrees properly. + * + * @param projectDir the project directory. + * @param baseConfig the user and system level git config. + * @return the builder. + */ static RepositorySpecificResolver fileRepositoryResolverForProject(File projectDir, Config baseConfig) { RepositorySpecificResolver repositoryResolver = new RepositorySpecificResolver(baseConfig); repositoryResolver.findGitDir(projectDir); diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 67413e9014..f420e83323 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -4,6 +4,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] +### Fixed +* Git user config and system config also included for defaultEndings configuration. ([#540](https://github.com/diffplug/spotless/issues/540)) + ## [6.4.1] - 2022-03-30 ### Fixed * Fixed ktfmt options configuration in Gradle plugin for Gradle Kotlin scripts (kts). diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 508e7f4997..8863af1825 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -4,6 +4,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] +### Fixed +* Git user config and system config also included for defaultEndings configuration. ([#540](https://github.com/diffplug/spotless/issues/540)) + ## [2.22.0] - 2022-03-28 ### Added * Added support for setting custom parameters for Kotlin ktfmt in Maven plugin. ([#1145](https://github.com/diffplug/spotless/pull/1145)) From 43dadf5bf025d84b240656cc056d239f79ba4cbe Mon Sep 17 00:00:00 2001 From: Arul Date: Mon, 4 Apr 2022 06:46:46 +0530 Subject: [PATCH 3/3] spotbugs fixes --- .../main/java/com/diffplug/spotless/extra/GitWorkarounds.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 993c207218..70f0b9cb99 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 @@ -74,7 +74,7 @@ static RepositorySpecificResolver fileRepositoryResolverForProject(File projectD * @param baseConfig the user and system level git config. * @return the builder. */ - static RepositorySpecificResolver fileRepositoryResolverForProject(File projectDir, Config baseConfig) { + static RepositorySpecificResolver fileRepositoryResolverForProject(File projectDir, @Nullable Config baseConfig) { RepositorySpecificResolver repositoryResolver = new RepositorySpecificResolver(baseConfig); repositoryResolver.findGitDir(projectDir); repositoryResolver.readEnvironment(); @@ -113,7 +113,7 @@ public RepositorySpecificResolver() { this(null); } - public RepositorySpecificResolver(Config baseConfig) { + public RepositorySpecificResolver(@Nullable Config baseConfig) { this.baseConfig = baseConfig; }