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/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..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. @@ -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,28 @@ 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; + } 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); + } + } + /** 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..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. @@ -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,16 @@ 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"); + } } 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