Class GitPrePushHookInstaller

java.lang.Object
com.diffplug.spotless.GitPrePushHookInstaller
Direct Known Subclasses:
GitPrePushHookInstallerGradle, GitPrePushHookInstallerMaven

public abstract class GitPrePushHookInstaller extends Object
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.
  • Field Details

    • logger

      Logger for recording informational and error messages during the installation process.
    • root

      protected final File root
      The root directory of the Git repository where the hook will be installed.
  • Constructor Details

    • GitPrePushHookInstaller

      public GitPrePushHookInstaller(GitPrePushHookInstaller.GitPreHookLogger logger, File root)
      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

      public void install() throws Exception
      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 the doInstall() method to perform the actual installation.
      Throws:
      Exception - if any error occurs during installation
    • preHookContent

      protected abstract String 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.