Skip to content

Commit 9c27d39

Browse files
committed
Create new GIT_ATTRIBUTES_FAST_ALLSAME which is the same as GIT_ATTRIBUTES, except that:
- it is much faster - it assumes that every file in a given format has the same line endings
1 parent 5d9f3b4 commit 9c27d39

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

lib/src/main/java/com/diffplug/spotless/LineEnding.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,14 @@ public Policy createPolicy() {
3737
return super.createPolicy();
3838
}
3939
},
40+
/** Uses the same line endings as Git, and assumes that every single file being formatted will have the same line ending. */
41+
GIT_ATTRIBUTES_FAST_ALLSAME {
42+
/** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
43+
@Override @Deprecated
44+
public Policy createPolicy() {
45+
return super.createPolicy();
46+
}
47+
},
4048
/** {@code \n} on unix systems, {@code \r\n} on windows systems. */
4149
PLATFORM_NATIVE,
4250
/** {@code \r\n} */
@@ -51,7 +59,7 @@ public Policy createPolicy() {
5159
public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
5260
Objects.requireNonNull(projectDir, "projectDir");
5361
Objects.requireNonNull(toFormat, "toFormat");
54-
if (this != GIT_ATTRIBUTES) {
62+
if (this != GIT_ATTRIBUTES && this != GIT_ATTRIBUTES_FAST_ALLSAME) {
5563
return createPolicy();
5664
} else {
5765
if (gitAttributesPolicyCreator == null) {
@@ -64,7 +72,39 @@ public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
6472
}
6573
}
6674
// gitAttributesPolicyCreator will always be nonnull at this point
67-
return gitAttributesPolicyCreator.apply(projectDir, toFormat);
75+
Policy policy = gitAttributesPolicyCreator.apply(projectDir, toFormat);
76+
if (this == GIT_ATTRIBUTES) {
77+
return policy;
78+
} else if (this == GIT_ATTRIBUTES_FAST_ALLSAME) {
79+
return new LazyAllTheSame(policy, toFormat);
80+
} else {
81+
throw new IllegalArgumentException("Unknown " + this);
82+
}
83+
}
84+
}
85+
86+
static class LazyAllTheSame extends LazyForwardingEquality<String> implements Policy {
87+
private transient Policy policy;
88+
private transient Supplier<Iterable<File>> toFormat;
89+
90+
public LazyAllTheSame(Policy policy, Supplier<Iterable<File>> toFormat) {
91+
this.policy = policy;
92+
this.toFormat = toFormat;
93+
}
94+
95+
@Override
96+
protected String calculateState() throws Exception {
97+
var files = toFormat.get().iterator();
98+
if (files.hasNext()) {
99+
return policy.getEndingFor(files.next());
100+
} else {
101+
return LineEnding.UNIX.str();
102+
}
103+
}
104+
105+
@Override
106+
public String getEndingFor(File file) {
107+
return state();
68108
}
69109
}
70110

0 commit comments

Comments
 (0)