1
1
/*
2
- * Copyright 2016-2022 DiffPlug
2
+ * Copyright 2016-2023 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -37,6 +37,14 @@ public Policy createPolicy() {
37
37
return super .createPolicy ();
38
38
}
39
39
},
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
+ },
40
48
/** {@code \n} on unix systems, {@code \r\n} on windows systems. */
41
49
PLATFORM_NATIVE ,
42
50
/** {@code \r\n} */
@@ -51,7 +59,7 @@ public Policy createPolicy() {
51
59
public Policy createPolicy (File projectDir , Supplier <Iterable <File >> toFormat ) {
52
60
Objects .requireNonNull (projectDir , "projectDir" );
53
61
Objects .requireNonNull (toFormat , "toFormat" );
54
- if (this != GIT_ATTRIBUTES ) {
62
+ if (this != GIT_ATTRIBUTES && this != GIT_ATTRIBUTES_FAST_ALLSAME ) {
55
63
return createPolicy ();
56
64
} else {
57
65
if (gitAttributesPolicyCreator == null ) {
@@ -64,7 +72,39 @@ public Policy createPolicy(File projectDir, Supplier<Iterable<File>> toFormat) {
64
72
}
65
73
}
66
74
// 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 ();
68
108
}
69
109
}
70
110
0 commit comments