|
| 1 | +package com.diffplug.spotless; |
| 2 | + |
| 3 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileWriter; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | + |
| 10 | +/** |
| 11 | + * Abstract class responsible for installing a Git pre-push hook in a repository. |
| 12 | + * This class ensures that specific checks and logic are run before a push operation in Git. |
| 13 | + * |
| 14 | + * Subclasses should define specific behavior for hook installation by implementing the required abstract methods. |
| 15 | + */ |
| 16 | +public abstract class GitPrePushHookInstaller { |
| 17 | + |
| 18 | + /** |
| 19 | + * Logger for recording informational and error messages during the installation process. |
| 20 | + */ |
| 21 | + protected final GitPreHookLogger logger; |
| 22 | + |
| 23 | + /** |
| 24 | + * The root directory of the Git repository where the hook will be installed. |
| 25 | + */ |
| 26 | + protected final File root; |
| 27 | + |
| 28 | + /** |
| 29 | + * Constructor to initialize the GitPrePushHookInstaller with a logger and repository root path. |
| 30 | + * |
| 31 | + * @param logger The logger for recording messages. |
| 32 | + * @param root The root directory of the Git repository. |
| 33 | + */ |
| 34 | + public GitPrePushHookInstaller(GitPreHookLogger logger, File root) { |
| 35 | + this.logger = logger; |
| 36 | + this.root = root; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Installs the Git pre-push hook into the repository. |
| 41 | + * |
| 42 | + * <p>This method checks for the following: |
| 43 | + * <ul> |
| 44 | + * <li>Ensures Git is installed and the `.git/config` file exists.</li> |
| 45 | + * <li>Checks if an executor required by the hook is available.</li> |
| 46 | + * <li>Creates and writes the pre-push hook file if it does not exist.</li> |
| 47 | + * <li>Skips installation if the hook is already installed.</li> |
| 48 | + * </ul> |
| 49 | + * If an issue occurs during installation, error messages are logged. |
| 50 | + * |
| 51 | + * @throws Exception if any error occurs during installation. |
| 52 | + */ |
| 53 | + public void install() throws Exception { |
| 54 | + logger.info("Installing git pre-push hook"); |
| 55 | + |
| 56 | + if (!isGitInstalled()) { |
| 57 | + logger.error("Git not found in root directory"); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (!isExecutorInstalled()) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + var hookContent = ""; |
| 66 | + final var gitHookFile = root.toPath().resolve(".git/hooks/pre-push").toFile(); |
| 67 | + if (!gitHookFile.exists()) { |
| 68 | + logger.info("Git pre-push hook not found, creating it"); |
| 69 | + gitHookFile.getParentFile().mkdirs(); |
| 70 | + if (!gitHookFile.createNewFile()) { |
| 71 | + logger.error("Failed to create pre-push hook file"); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (!gitHookFile.setExecutable(true, false)) { |
| 76 | + logger.error("Can not make file executable"); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + hookContent += "#!/bin/sh\n"; |
| 81 | + } |
| 82 | + |
| 83 | + if (isGitHookInstalled(gitHookFile)) { |
| 84 | + logger.info("Skipping, git pre-push hook already installed %s", gitHookFile.getAbsolutePath()); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + hookContent += preHookContent(); |
| 89 | + writeFile(gitHookFile, hookContent); |
| 90 | + |
| 91 | + logger.info("Git pre-push hook installed successfully to the file %s", gitHookFile.getAbsolutePath()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Checks if the required executor for performing the desired pre-push actions is installed. |
| 96 | + * |
| 97 | + * @return {@code true} if the executor is installed, {@code false} otherwise. |
| 98 | + */ |
| 99 | + protected abstract boolean isExecutorInstalled(); |
| 100 | + |
| 101 | + /** |
| 102 | + * Provides the content of the hook that should be inserted into the pre-push script. |
| 103 | + * |
| 104 | + * @return A string representing the content to include in the pre-push script. |
| 105 | + */ |
| 106 | + protected abstract String preHookContent(); |
| 107 | + |
| 108 | + /** |
| 109 | + * Checks if Git is installed by validating the existence of `.git/config` in the repository root. |
| 110 | + * |
| 111 | + * @return {@code true} if Git is installed, {@code false} otherwise. |
| 112 | + */ |
| 113 | + private boolean isGitInstalled() { |
| 114 | + return root.toPath().resolve(".git/config").toFile().exists(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Verifies if the pre-push hook file already contains the custom Spotless hook content. |
| 119 | + * |
| 120 | + * @param gitHookFile The file representing the Git hook. |
| 121 | + * @return {@code true} if the hook is already installed, {@code false} otherwise. |
| 122 | + * @throws Exception if an error occurs when reading the file. |
| 123 | + */ |
| 124 | + private boolean isGitHookInstalled(File gitHookFile) throws Exception { |
| 125 | + final var hook = Files.readString(gitHookFile.toPath(), UTF_8); |
| 126 | + return hook.contains("##### SPOTLESS HOOK START #####"); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Writes the specified content into a file. |
| 131 | + * |
| 132 | + * @param file The file to which the content should be written. |
| 133 | + * @param content The content to write into the file. |
| 134 | + * @throws IOException if an error occurs while writing to the file. |
| 135 | + */ |
| 136 | + private void writeFile(File file, String content) throws IOException { |
| 137 | + try (final var writer = new FileWriter(file, UTF_8, true)) { |
| 138 | + writer.write(content); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Generates a pre-push template script that defines the commands to check and apply changes |
| 144 | + * using an executor and Spotless. |
| 145 | + * |
| 146 | + * @param executor The tool to execute the check and apply commands. |
| 147 | + * @param commandCheck The command to check for issues. |
| 148 | + * @param commandApply The command to apply corrections. |
| 149 | + * @return A string template representing the Spotless Git pre-push hook content. |
| 150 | + */ |
| 151 | + protected String preHookTemplate(String executor, String commandCheck, String commandApply) { |
| 152 | + var spotlessHook = "\n"; |
| 153 | + spotlessHook += "\n##### SPOTLESS HOOK START #####"; |
| 154 | + spotlessHook += "\nSPOTLESS_EXECUTOR=" + executor; |
| 155 | + spotlessHook += "\nif ! $SPOTLESS_EXECUTOR " + commandCheck + " ; then"; |
| 156 | + spotlessHook += "\n echo 1>&2 \"spotless found problems, running " + commandApply + "; commit the result and re-push\""; |
| 157 | + spotlessHook += "\n $SPOTLESS_EXECUTOR " + commandApply; |
| 158 | + spotlessHook += "\n exit 1"; |
| 159 | + spotlessHook += "\nfi"; |
| 160 | + spotlessHook += "\n##### SPOTLESS HOOK END #####"; |
| 161 | + spotlessHook += "\n\n"; |
| 162 | + return spotlessHook; |
| 163 | + } |
| 164 | + |
| 165 | + public interface GitPreHookLogger { |
| 166 | + void info(String format, Object... arguments); |
| 167 | + void error(String format, Object... arguments); |
| 168 | + } |
| 169 | +} |
0 commit comments