Skip to content

Commit 6acbfe2

Browse files
authored
spotlessApply no longer clobbers file permissions (pr #656, fixes #654)
2 parents 43e4e97 + 6fe2c55 commit 6acbfe2

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

plugin-gradle/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
* Depending on the file system, executing `gradle spotlessApply` might change permission on the changed files from `644` to `755`; fixes ([#656](https://github.com/diffplug/spotless/pull/656))
67
* Bump default ktfmt from 0.15 to 0.16, and remove duplicated logic for the --dropbox-style option ([#642](https://github.com/diffplug/spotless/pull/648))
78

89
## [5.1.0] - 2020-07-13

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessApply.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void visitFile(FileVisitDetails fileVisitDetails) {
6868
File originalSource = new File(getProject().getProjectDir(), path);
6969
try {
7070
getLogger().debug("Copying " + fileVisitDetails.getFile() + " to " + originalSource);
71-
Files.copy(fileVisitDetails.getFile().toPath(), originalSource.toPath(), StandardCopyOption.REPLACE_EXISTING);
71+
Files.copy(fileVisitDetails.getFile().toPath(), originalSource.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
7272
} catch (IOException e) {
7373
throw new RuntimeException(e);
7474
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
22+
import java.nio.file.StandardCopyOption;
2223
import java.util.Comparator;
2324

2425
import org.gradle.api.GradleException;
@@ -80,6 +81,8 @@ private void processInputFile(Formatter formatter, File input) throws IOExceptio
8081
throw new IllegalStateException("Every file has a parent folder.");
8182
}
8283
Files.createDirectories(parentDir);
84+
// Need to copy the original file to the tmp location just to remember the file attributes
85+
Files.copy(input.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
8386
dirtyState.writeCanonicalTo(output);
8487
}
8588
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2020 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless;
17+
18+
import java.io.IOException;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.nio.file.attribute.PosixFilePermissions;
22+
23+
import org.assertj.core.api.AbstractStringAssert;
24+
import org.assertj.core.api.Assertions;
25+
import org.junit.Test;
26+
27+
import com.diffplug.spotless.FileSignature;
28+
29+
public class FilePermissionsTest extends GradleIntegrationHarness {
30+
@Test
31+
public void spotlessApplyShouldPreservePermissions() throws IOException {
32+
if (FileSignature.machineIsWin()) {
33+
// need unix filesystem
34+
return;
35+
}
36+
setFile("build.gradle").toLines(
37+
"buildscript { repositories { mavenCentral() } }",
38+
"plugins {",
39+
" id 'com.diffplug.spotless'",
40+
"}",
41+
"",
42+
"spotless {",
43+
" java {",
44+
" target file('test.java')",
45+
" googleJavaFormat('1.2')",
46+
" }",
47+
"}");
48+
setFile("test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test");
49+
50+
Path path = rootFolder().toPath().resolve("test.java");
51+
Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxr--r--"));
52+
assertPermissions(path).isEqualTo("rwxr--r--");
53+
54+
gradleRunner().withArguments("spotlessApply").build();
55+
assertFile("test.java").sameAsResource("java/googlejavaformat/JavaCodeFormatted.test");
56+
assertPermissions(path).isEqualTo("rwxr--r--");
57+
}
58+
59+
private AbstractStringAssert<?> assertPermissions(Path path) throws IOException {
60+
return Assertions.assertThat(PosixFilePermissions.toString(Files.getPosixFilePermissions(path)));
61+
}
62+
}

0 commit comments

Comments
 (0)