Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for git's core.autocrlf #1114

Merged
merged 4 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -56,7 +57,7 @@

/**
* Uses <a href="https://git-scm.com/docs/gitattributes">.gitattributes</a> 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.
*/
Expand Down Expand Up @@ -224,7 +225,7 @@ static class Runtime {
private Runtime(List<AttributesRule> infoRules, @Nullable File workTree, Config config, List<AttributesRule> 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);
}

Expand Down Expand Up @@ -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);
}
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved
}

/** Creates a LineEnding from an EOL. */
private static LineEnding fromEol(EOL eol) {
// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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");
}
}
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down