Package com.diffplug.spotless
Class GitPrePushHookInstaller
java.lang.Object
com.diffplug.spotless.GitPrePushHookInstaller
- Direct Known Subclasses:
GitPrePushHookInstallerGradle
,GitPrePushHookInstallerMaven
Abstract class responsible for installing a Git pre-push hook in a repository.
This class ensures that specific checks and logic are run before a push operation in Git.
Subclasses should define specific behavior for hook installation by implementing the required abstract methods.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
static interface
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final GitPrePushHookInstaller.GitPreHookLogger
Logger for recording informational and error messages during the installation process.protected final File
The root directory of the Git repository where the hook will be installed. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor to initialize the GitPrePushHookInstaller with a logger and repository root path. -
Method Summary
Modifier and TypeMethodDescriptionvoid
install()
Installs the Git pre-push hook while ensuring thread safety and preventing parallel installations.protected abstract String
Provides the content of the hook that should be inserted into the pre-push script.protected String
preHookTemplate
(GitPrePushHookInstaller.Executor executor, String commandCheck, String commandApply) Generates a pre-push template script that defines the commands to check and apply changes using an executor and Spotless.
-
Field Details
-
logger
Logger for recording informational and error messages during the installation process. -
root
The root directory of the Git repository where the hook will be installed.
-
-
Constructor Details
-
GitPrePushHookInstaller
Constructor to initialize the GitPrePushHookInstaller with a logger and repository root path.- Parameters:
logger
- The logger for recording messages.root
- The root directory of the Git repository.
-
-
Method Details
-
install
Installs the Git pre-push hook while ensuring thread safety and preventing parallel installations. The method: 1. Uses a thread-safe mechanism to prevent concurrent installations 2. If a parallel installation is detected, logs a warning and skips the installation 3. Uses a synchronized block with a static lock object to ensure thread safety The installation process sets a flag during installation and resets it afterwards, using thedoInstall()
method to perform the actual installation.- Throws:
Exception
- if any error occurs during installation
-
preHookContent
Provides the content of the hook that should be inserted into the pre-push script.- Returns:
- A string representing the content to include in the pre-push script.
-
preHookTemplate
protected String preHookTemplate(GitPrePushHookInstaller.Executor executor, String commandCheck, String commandApply) Generates a pre-push template script that defines the commands to check and apply changes using an executor and Spotless.- Parameters:
executor
- The tool to execute the check and apply commands.commandCheck
- The command to check for issues.commandApply
- The command to apply corrections.- Returns:
- A string template representing the Spotless Git pre-push hook content.
-