From 9c27d39f642fcee2afbd032d0179a30074ea96f9 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 28 Sep 2023 15:09:11 -0700 Subject: [PATCH] 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 --- .../com/diffplug/spotless/LineEnding.java | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/LineEnding.java b/lib/src/main/java/com/diffplug/spotless/LineEnding.java index 72b2532f2a..fa487a03b7 100644 --- a/lib/src/main/java/com/diffplug/spotless/LineEnding.java +++ b/lib/src/main/java/com/diffplug/spotless/LineEnding.java @@ -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. @@ -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} */ @@ -51,7 +59,7 @@ public Policy createPolicy() { public Policy createPolicy(File projectDir, Supplier> 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) { @@ -64,7 +72,39 @@ public Policy createPolicy(File projectDir, Supplier> 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 implements Policy { + private transient Policy policy; + private transient Supplier> toFormat; + + public LazyAllTheSame(Policy policy, Supplier> 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(); } }