Skip to content

Commit

Permalink
Create new GIT_ATTRIBUTES_FAST_ALLSAME which is the same as GIT_ATTRI…
Browse files Browse the repository at this point in the history
…BUTES, except that:

- it is much faster
- it assumes that every file in a given format has the same line endings
  • Loading branch information
nedtwigg committed Sep 28, 2023
1 parent 5d9f3b4 commit 9c27d39
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions lib/src/main/java/com/diffplug/spotless/LineEnding.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 DiffPlug
* Copyright 2016-2023 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 @@ -37,6 +37,14 @@ public Policy createPolicy() {
return super.createPolicy();
}
},
/** Uses the same line endings as Git, and assumes that every single file being formatted will have the same line ending. */
GIT_ATTRIBUTES_FAST_ALLSAME {
/** .gitattributes is path-specific, so you must use {@link LineEnding#createPolicy(File, Supplier)}. */
@Override @Deprecated
public Policy createPolicy() {
return super.createPolicy();
}
},
/** {@code \n} on unix systems, {@code \r\n} on windows systems. */
PLATFORM_NATIVE,
/** {@code \r\n} */
Expand All @@ -51,7 +59,7 @@ public Policy createPolicy() {
public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
Objects.requireNonNull(projectDir, "projectDir");
Objects.requireNonNull(toFormat, "toFormat");
if (this != GIT_ATTRIBUTES) {
if (this != GIT_ATTRIBUTES && this != GIT_ATTRIBUTES_FAST_ALLSAME) {
return createPolicy();
} else {
if (gitAttributesPolicyCreator == null) {
Expand All @@ -64,7 +72,39 @@ public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
}
}
// gitAttributesPolicyCreator will always be nonnull at this point
return gitAttributesPolicyCreator.apply(projectDir, toFormat);
Policy policy = gitAttributesPolicyCreator.apply(projectDir, toFormat);
if (this == GIT_ATTRIBUTES) {
return policy;
} else if (this == GIT_ATTRIBUTES_FAST_ALLSAME) {
return new LazyAllTheSame(policy, toFormat);
} else {
throw new IllegalArgumentException("Unknown " + this);
}
}
}

static class LazyAllTheSame extends LazyForwardingEquality<String> implements Policy {
private transient Policy policy;
private transient Supplier<Iterable<File>> toFormat;

public LazyAllTheSame(Policy policy, Supplier<Iterable<File>> toFormat) {
this.policy = policy;
this.toFormat = toFormat;
}

@Override
protected String calculateState() throws Exception {
var files = toFormat.get().iterator();
if (files.hasNext()) {
return policy.getEndingFor(files.next());
} else {
return LineEnding.UNIX.str();
}
}

@Override
public String getEndingFor(File file) {
return state();
}
}

Expand Down

0 comments on commit 9c27d39

Please sign in to comment.