Skip to content

Commit 0f0494d

Browse files
authored
Not rewrites hooks if no changes
1 parent 47450fa commit 0f0494d

File tree

6 files changed

+34
-40
lines changed

6 files changed

+34
-40
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ the project is rebuild. Be aware that for hooks to be installed to git, any `mvn
1313
like `compile` or `test`. It means that after editing git-hooks-maven-plugin configuration in `pom.xml`,
1414
it's necessary to manually run `mvn compile` or any other maven goal, on the initializing step of which git hooks will be installed.
1515

16-
The example with *pre-commit* and *pre-push* hooks configured, will look like it:
16+
The example with *pre-commit* and *pre-push* hooks configured, will look like this:
1717
```xml
1818
<plugins>
1919
<plugin>

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.pepperkit</groupId>
88
<artifactId>git-hooks-maven-plugin</artifactId>
9-
<version>1.0.0</version>
9+
<version>1.1.0</version>
1010
<packaging>maven-plugin</packaging>
1111
<name>Git Hooks Maven Plugin</name>
1212
<description>Maven plugin, which enables control on git hooks via easy configuration.</description>
@@ -321,7 +321,7 @@
321321
<plugin>
322322
<groupId>io.github.pepperkit</groupId>
323323
<artifactId>git-hooks-maven-plugin</artifactId>
324-
<version>1.0.0</version>
324+
<version>1.1.0</version>
325325
<configuration>
326326
<hooks>
327327
<pre-commit>mvn -B checkstyle:checkstyle</pre-commit>

src/main/java/io/github/pepperkit/githooks/ExecuteHooksMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ExecuteHooksMojo extends AbstractMojo {
2424
@Parameter(property = "hookName")
2525
public String hookName;
2626

27-
GitHooksManager gitHooksManager = new GitHooksManager(getLog());
27+
GitHooksManager gitHooksManager = new GitHooksManager(this);
2828

2929
@Override
3030
public void execute() throws MojoExecutionException {

src/main/java/io/github/pepperkit/githooks/GitHooksManager.java

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import java.nio.file.Path;
1717
import java.nio.file.Paths;
1818
import java.nio.file.attribute.PosixFilePermission;
19-
import java.time.ZoneId;
20-
import java.time.format.DateTimeFormatter;
2119
import java.util.Arrays;
2220
import java.util.Collections;
2321
import java.util.HashSet;
@@ -28,8 +26,7 @@
2826
import java.util.concurrent.Executors;
2927
import java.util.stream.Collectors;
3028

31-
import org.apache.maven.plugin.logging.Log;
32-
import org.apache.maven.plugin.logging.SystemStreamLog;
29+
import org.apache.maven.plugin.Mojo;
3330

3431
/**
3532
* Manages all the work with git hooks.
@@ -69,24 +66,14 @@ public class GitHooksManager {
6966
PosixFilePermission.OWNER_EXECUTE
7067
)));
7168

72-
private final Log logger;
73-
74-
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss")
75-
.withZone(ZoneId.systemDefault());
69+
private final Mojo mojo;
7670

7771
/**
78-
* Creates GitHooksManager with a newly created logger.
72+
* Creates GitHooksManager with the provided mojo. Mojo is used to obtain the correct logger.
73+
* @param mojo mojo, which initiated the work with GitHooksManager
7974
*/
80-
public GitHooksManager() {
81-
logger = new SystemStreamLog();
82-
}
83-
84-
/**
85-
* Creates GitHooksManager with the provided mojo logger.
86-
* @param logger mojo logger
87-
*/
88-
public GitHooksManager(Log logger) {
89-
this.logger = logger;
75+
public GitHooksManager(Mojo mojo) {
76+
this.mojo = mojo;
9077
}
9178

9279
/**
@@ -143,16 +130,23 @@ List<File> getExistingHookFiles() {
143130
*/
144131
void createHook(String hookName, String hookValue) throws IOException {
145132
String hookPath = getHookPath(hookName);
146-
147-
try (BufferedWriter writer = new BufferedWriter(new FileWriter(hookPath))) {
148-
logger.info("Writing `" + hookName + "` hook");
149-
writer.write(SHEBANG + "\n" + hookValue.replaceAll("[ ]{2,}", ""));
150-
151-
Path hookFilePath = Paths.get(hookPath);
152-
if (hookFilePath.getFileSystem().supportedFileAttributeViews().contains("posix")) {
153-
Set<PosixFilePermission> currentPermissions = Files.getPosixFilePermissions(hookFilePath);
154-
if (!currentPermissions.containsAll(HOOK_FILE_PERMISSIONS)) {
155-
Files.setPosixFilePermissions(hookFilePath, HOOK_FILE_PERMISSIONS);
133+
String fullHookValue = SHEBANG + "\n" + hookValue.replaceAll("[ ]{2,}", "");
134+
135+
Optional<String> existingHookValue = readHook(hookName);
136+
if (existingHookValue.isPresent() && existingHookValue.get().equals(fullHookValue)) {
137+
mojo.getLog().info("The hook `" + hookName + "` has not changed, skipping");
138+
139+
} else {
140+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(hookPath))) {
141+
mojo.getLog().info("Writing `" + hookName + "` hook");
142+
writer.write(fullHookValue);
143+
144+
Path hookFilePath = Paths.get(hookPath);
145+
if (hookFilePath.getFileSystem().supportedFileAttributeViews().contains("posix")) {
146+
Set<PosixFilePermission> currentPermissions = Files.getPosixFilePermissions(hookFilePath);
147+
if (!currentPermissions.containsAll(HOOK_FILE_PERMISSIONS)) {
148+
Files.setPosixFilePermissions(hookFilePath, HOOK_FILE_PERMISSIONS);
149+
}
156150
}
157151
}
158152
}
@@ -170,7 +164,7 @@ private String getHookPath(String hookName) {
170164
*/
171165
boolean printHook(String hookName) throws IOException {
172166
Optional<String> hookValue = readHook(hookName);
173-
hookValue.ifPresent(h -> logger.info(
167+
hookValue.ifPresent(h -> mojo.getLog().info(
174168
"`" + hookName + "` -> The following commands will be invoked: \n" + h));
175169
return hookValue.isPresent();
176170
}
@@ -199,14 +193,14 @@ boolean executeHook(String hookName) throws InterruptedException, IOException {
199193

200194
Optional<String> hook = readHook(hookName);
201195
if (hook.isPresent()) {
202-
logger.info(">>>>> Executing hook `" + hookName + "` <<<<<");
196+
mojo.getLog().info(">>>>> Executing hook `" + hookName + "` <<<<<");
203197
Process process = Runtime.getRuntime().exec("sh -c " + getHookPath(hookName));
204198
Executors.newSingleThreadExecutor().submit(() -> new BufferedReader(
205-
new InputStreamReader(process.getInputStream())).lines().forEach(logger::info));
199+
new InputStreamReader(process.getInputStream())).lines().forEach(mojo.getLog()::info));
206200

207201
int exitCode = process.waitFor();
208-
logger.info("Exit code is " + exitCode);
209-
logger.info(">>>>> The hook `" + hookName + "` was executed with the "
202+
mojo.getLog().info("Exit code is " + exitCode);
203+
mojo.getLog().info(">>>>> The hook `" + hookName + "` was executed with the "
210204
+ (exitCode == 0 ? "SUCCESS" : "ERROR") + " result <<<<<");
211205
}
212206
return hook.isPresent();

src/main/java/io/github/pepperkit/githooks/InitHooksMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class InitHooksMojo extends AbstractMojo {
2929
@Parameter(defaultValue = "null")
3030
public Map<String, String> hooks;
3131

32-
GitHooksManager gitHooksManager = new GitHooksManager(getLog());
32+
GitHooksManager gitHooksManager = new GitHooksManager(this);
3333

3434
/**
3535
* Helper method, is used to set hooks to null if nothing is provided by the user.

src/main/java/io/github/pepperkit/githooks/PrintHooksMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class PrintHooksMojo extends AbstractMojo {
2323
@Parameter(property = "hookName")
2424
public String hookName;
2525

26-
GitHooksManager gitHooksManager = new GitHooksManager(getLog());
26+
GitHooksManager gitHooksManager = new GitHooksManager(this);
2727

2828
@Override
2929
public void execute() throws MojoExecutionException {

0 commit comments

Comments
 (0)