1616import java .nio .file .Path ;
1717import java .nio .file .Paths ;
1818import java .nio .file .attribute .PosixFilePermission ;
19- import java .time .ZoneId ;
20- import java .time .format .DateTimeFormatter ;
2119import java .util .Arrays ;
2220import java .util .Collections ;
2321import java .util .HashSet ;
2826import java .util .concurrent .Executors ;
2927import 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 ();
0 commit comments