1
1
/*
2
- * Copyright 2016-2021 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.
33
33
import java .util .Locale ;
34
34
import java .util .Map ;
35
35
36
+ import edu .umd .cs .findbugs .annotations .Nullable ;
36
37
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
37
38
38
39
/** Computes a signature for any needed files. */
@@ -43,6 +44,8 @@ public final class FileSignature implements Serializable {
43
44
* Transient because not needed to uniquely identify a FileSignature instance, and also because
44
45
* Gradle only needs this class to be Serializable so it can compare FileSignature instances for
45
46
* incremental builds.
47
+ *
48
+ * We don't want these absolute paths to screw up buildcache keys.
46
49
*/
47
50
@ SuppressFBWarnings ("SE_TRANSIENT_FIELD_NOT_RESTORED" )
48
51
private final transient List <File > files ;
@@ -93,6 +96,45 @@ private FileSignature(final List<File> files) throws IOException {
93
96
}
94
97
}
95
98
99
+ /** A view of `FileSignature` which can be safely roundtripped. */
100
+ public static class RoundTrippable implements Serializable {
101
+ private final List <File > files ;
102
+ private transient @ Nullable FileSignature cached ;
103
+
104
+ private RoundTrippable (List <File > files , FileSignature cached ) {
105
+ this .files = files ;
106
+ this .cached = cached ;
107
+ }
108
+
109
+ public FileSignature stripAbsolutePaths () throws IOException {
110
+ if (cached == null ) {
111
+ // null when restored via serialization
112
+ cached = new FileSignature (files );
113
+ }
114
+ return cached ;
115
+ }
116
+ }
117
+
118
+ public RoundTrippable roundTrippable () {
119
+ return new RoundTrippable (files , this );
120
+ }
121
+
122
+ public static @ Nullable RoundTrippable roundTrippableNullable (@ Nullable FileSignature signature ) {
123
+ if (signature != null ) {
124
+ return signature .roundTrippable ();
125
+ } else {
126
+ return null ;
127
+ }
128
+ }
129
+
130
+ public static @ Nullable FileSignature stripAbsolutePathsNullable (@ Nullable RoundTrippable roundTrippable ) throws IOException {
131
+ if (roundTrippable != null ) {
132
+ return roundTrippable .stripAbsolutePaths ();
133
+ } else {
134
+ return null ;
135
+ }
136
+ }
137
+
96
138
/** Returns all of the files in this signature, throwing an exception if there are more or less than 1 file. */
97
139
public Collection <File > files () {
98
140
return Collections .unmodifiableList (files );
0 commit comments