From b69a31306642772323a7662e7442e1f2a2123f01 Mon Sep 17 00:00:00 2001 From: Roland Weisleder Date: Sun, 30 Jan 2022 20:33:13 +0100 Subject: [PATCH 1/4] Add support for git's `core.autocrlf` Among others, Spotless uses .gitattributes to determine the line ending of files. In the absence of this file, Spotless falls back to `core.eol` from git config. Before this commit, Spotless ignored the `core.autocrlf` property. Git behaves in such a way that it ignores `core.eol` if `core.autocrlf` is set to `true` or `input`. This commit aligns the behavior of Spotless to favor `core.autocrlf` over `core.eol`. Fixes #540 Signed-off-by: Roland Weisleder --- .../extra/GitAttributesLineEndings.java | 25 +++++++++++++++++-- .../spotless/extra/GitAttributesTest.java | 15 +++++++++++ 2 files changed, 38 insertions(+), 2 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 70fca79027..e70482e66e 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 @@ -38,6 +38,7 @@ import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.CoreConfig.AutoCRLF; import org.eclipse.jgit.lib.CoreConfig.EOL; import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; @@ -56,7 +57,7 @@ /** * Uses .gitattributes to determine - * the appropriate line ending. Falls back to the {@code core.eol} property in the + * the appropriate line ending. Falls back to the {@code core.eol} and {@code core.autocrlf} properties in the * git config if there are no applicable git attributes, then finally falls * back to the platform native. */ @@ -224,7 +225,7 @@ static class Runtime { private Runtime(List infoRules, @Nullable File workTree, Config config, List globalRules) { this.infoRules = Objects.requireNonNull(infoRules); this.workTree = workTree; - this.defaultEnding = fromEol(config.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_EOL, EOL.NATIVE)).str(); + this.defaultEnding = findDefaultLineEnding(config).str(); this.globalRules = Objects.requireNonNull(globalRules); } @@ -273,6 +274,26 @@ private static String convertEolToLineEnding(String eol, File file) { } } + private LineEnding findDefaultLineEnding(Config config) { + // handle core.autocrlf, whose values "true" and "input" override core.eol + AutoCRLF autoCRLF = config.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF, AutoCRLF.FALSE); + if (autoCRLF == AutoCRLF.TRUE) { + // autocrlf=true converts CRLF->LF during commit + // and converts LF->CRLF during checkout + // so CRLF is the default line ending + return LineEnding.WINDOWS; + } else if (autoCRLF == AutoCRLF.INPUT) { + // autocrlf=input converts CRLF->LF during commit + // and does no conversion during checkout + // mostly used on Unix, so LF is the default encoding + return LineEnding.UNIX; + } + + // handle core.eol + EOL eol = config.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_EOL, EOL.NATIVE); + return fromEol(eol); + } + /** Creates a LineEnding from an EOL. */ private static LineEnding fromEol(EOL eol) { // @formatter:off diff --git a/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java b/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java index 35ac58ee85..f6c3f7aac2 100644 --- a/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java +++ b/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java @@ -22,6 +22,8 @@ import java.util.List; import org.assertj.core.api.Assertions; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; import org.junit.jupiter.api.Test; import com.diffplug.common.base.Errors; @@ -87,4 +89,17 @@ void policyTest() throws IOException { Assertions.assertThat(policy.getEndingFor(newFile("MANIFEST.MF"))).isEqualTo("\r\n"); Assertions.assertThat(policy.getEndingFor(newFile("subfolder/MANIFEST.MF"))).isEqualTo("\r\n"); } + + @Test + void policyDefaultLineEndingTest() throws GitAPIException, IOException { + Git git = Git.init().setDirectory(rootFolder()).call(); + git.close(); + setFile(".git/config").toContent(StringPrinter.buildStringFromLines( + "[core]", + "autocrlf=true", + "eol=lf" + )); + LineEnding.Policy policy = LineEnding.GIT_ATTRIBUTES.createPolicy(rootFolder(), () -> testFiles()); + Assertions.assertThat(policy.getEndingFor(newFile("someFile"))).isEqualTo("\r\n"); + } } From 3e04c153bb2063e6b976b0a7f22baf2325b04fb6 Mon Sep 17 00:00:00 2001 From: Roland Weisleder Date: Sun, 30 Jan 2022 20:43:40 +0100 Subject: [PATCH 2/4] Add change to CHANGES.md Signed-off-by: Roland Weisleder --- CHANGES.md | 1 + plugin-gradle/CHANGES.md | 1 + plugin-maven/CHANGES.md | 1 + 3 files changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 0a9b6ff2fc..98ce03f6e8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,6 +15,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Bump default versions of formatters ([#1095](https://github.com/diffplug/spotless/pull/1095)). * google-java-format `1.12.0` -> `1.13.0` * ktfmt `0.29` -> `0.30` +* Added support for git property `core.autocrlf` ([#540](https://github.com/diffplug/spotless/issues/540)) ## [2.22.0] - 2022-01-13 ### Added diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 4d9d131e82..dfd3133f2f 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Bump default versions of formatters ([#1095](https://github.com/diffplug/spotless/pull/1095)). * google-java-format `1.12.0` -> `1.13.0` * ktfmt `0.29` -> `0.30` +* Added support for git property `core.autocrlf` ([#540](https://github.com/diffplug/spotless/issues/540)) ## [6.2.0] - 2022-01-13 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index c5ca50d936..0a0de0dbee 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -6,6 +6,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Bump default versions of formatters ([#1095](https://github.com/diffplug/spotless/pull/1095)). * google-java-format `1.12.0` -> `1.13.0` * ktfmt `0.29` -> `0.30` +* Added support for git property `core.autocrlf` ([#540](https://github.com/diffplug/spotless/issues/540)) ## [2.20.0] - 2022-01-13 ### Added From fb849bd8f5cc585dad0c50390fe4988d36a4b9e2 Mon Sep 17 00:00:00 2001 From: Roland Weisleder Date: Tue, 1 Feb 2022 21:05:32 +0100 Subject: [PATCH 3/4] Make all enum cases explicit Signed-off-by: Roland Weisleder --- .../spotless/extra/GitAttributesLineEndings.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 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 e70482e66e..b71fa80f2a 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 @@ -287,11 +287,13 @@ private LineEnding findDefaultLineEnding(Config config) { // and does no conversion during checkout // mostly used on Unix, so LF is the default encoding return LineEnding.UNIX; + } else if (autoCRLF == AutoCRLF.FALSE) { + // handle core.eol + EOL eol = config.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_EOL, EOL.NATIVE); + return fromEol(eol); + } else { + throw new IllegalStateException("Unexpected value for autoCRLF " + autoCRLF); } - - // handle core.eol - EOL eol = config.getEnum(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_EOL, EOL.NATIVE); - return fromEol(eol); } /** Creates a LineEnding from an EOL. */ From 2b0db7f6d20f5bcd6301b5cc6033b795e864dddd Mon Sep 17 00:00:00 2001 From: Roland Weisleder Date: Tue, 1 Feb 2022 21:09:01 +0100 Subject: [PATCH 4/4] Apply formatting Signed-off-by: Roland Weisleder --- .../spotless/extra/GitAttributesLineEndings.java | 2 +- .../com/diffplug/spotless/extra/GitAttributesTest.java | 9 ++++----- 2 files changed, 5 insertions(+), 6 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 b71fa80f2a..11ed730e6d 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 @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java b/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java index f6c3f7aac2..330a432d40 100644 --- a/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java +++ b/lib-extra/src/test/java/com/diffplug/spotless/extra/GitAttributesTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -95,10 +95,9 @@ void policyDefaultLineEndingTest() throws GitAPIException, IOException { Git git = Git.init().setDirectory(rootFolder()).call(); git.close(); setFile(".git/config").toContent(StringPrinter.buildStringFromLines( - "[core]", - "autocrlf=true", - "eol=lf" - )); + "[core]", + "autocrlf=true", + "eol=lf")); LineEnding.Policy policy = LineEnding.GIT_ATTRIBUTES.createPolicy(rootFolder(), () -> testFiles()); Assertions.assertThat(policy.getEndingFor(newFile("someFile"))).isEqualTo("\r\n"); }