diff --git a/src/main/java/jenkins/plugins/git/DisableHooks.java b/src/main/java/jenkins/plugins/git/DisableHooks.java new file mode 100644 index 0000000000..4edb8e51c3 --- /dev/null +++ b/src/main/java/jenkins/plugins/git/DisableHooks.java @@ -0,0 +1,27 @@ +package jenkins.plugins.git; + +import hudson.Functions; +import hudson.remoting.VirtualChannel; +import java.io.IOException; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.StoredConfig; +import org.jenkinsci.plugins.gitclient.RepositoryCallback; + +/** + * Disables git hooks. This can get remotely executed on agents. + */ +class DisableHooks implements RepositoryCallback { + private static final long serialVersionUID = 1L; + + static final String DISABLED_WIN = "NUL:"; + static final String DISABLED_NIX = "/dev/null"; + + @Override + public Object invoke(Repository repo, VirtualChannel channel) throws IOException, InterruptedException { + final String VAL = Functions.isWindows() ? DISABLED_WIN : DISABLED_NIX; + final StoredConfig repoConfig = repo.getConfig(); + repoConfig.setString("core", null, "hooksPath", VAL); + repoConfig.save(); + return null; + } +} diff --git a/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java b/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java index 1e8c8a849b..5b3f03d3cf 100644 --- a/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java +++ b/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java @@ -26,31 +26,23 @@ import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; -import hudson.Functions; import hudson.model.PersistentDescriptor; import hudson.plugins.git.GitException; import hudson.remoting.Channel; import jenkins.model.GlobalConfiguration; import jenkins.model.GlobalConfigurationCategory; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.StoredConfig; import org.jenkinsci.Symbol; import org.jenkinsci.plugins.gitclient.GitClient; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; import java.io.IOException; -import java.util.logging.Logger; - @Extension @Symbol("gitHooks") @Restricted(NoExternalUse.class) public class GitHooksConfiguration extends GlobalConfiguration implements PersistentDescriptor { - public static final String DISABLED_WIN = "NUL:"; - public static final String DISABLED_NIX = "/dev/null"; - static final Logger LOGGER = Logger.getLogger(GitHooksConfiguration.class.getName()); - private boolean allowedOnController = false; private boolean allowedOnAgents = false; @@ -109,33 +101,10 @@ public static void configure(GitClient client, final boolean allowedOnController public static void configure(GitClient client, final boolean allowed) throws GitException, IOException, InterruptedException { if (!allowed) { - client.withRepository((repo, channel) -> { - disable(repo); - return null; - }); + client.withRepository(new DisableHooks()); } else { - client.withRepository((repo, channel) -> { - unset(repo); - return null; - }); + client.withRepository(new UnsetHooks()); } } - private static void unset(final Repository repo) throws IOException { - final StoredConfig repoConfig = repo.getConfig(); - final String val = repoConfig.getString("core", null, "hooksPath"); - if (val != null && !val.isEmpty() && !DISABLED_NIX.equals(val) && !DISABLED_WIN.equals(val)) { - LOGGER.warning(() -> String.format("core.hooksPath explicitly set to %s and will be left intact on %s.", val, repo.getDirectory())); - } else { - repoConfig.unset("core", null, "hooksPath"); - repoConfig.save(); - } - } - - private static void disable(final Repository repo) throws IOException { - final String VAL = Functions.isWindows() ? DISABLED_WIN : DISABLED_NIX; - final StoredConfig repoConfig = repo.getConfig(); - repoConfig.setString("core", null, "hooksPath", VAL); - repoConfig.save(); - } } diff --git a/src/main/java/jenkins/plugins/git/UnsetHooks.java b/src/main/java/jenkins/plugins/git/UnsetHooks.java new file mode 100644 index 0000000000..453cb41a1c --- /dev/null +++ b/src/main/java/jenkins/plugins/git/UnsetHooks.java @@ -0,0 +1,30 @@ +package jenkins.plugins.git; + +import hudson.remoting.VirtualChannel; +import java.io.IOException; +import java.util.logging.Logger; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.StoredConfig; +import org.jenkinsci.plugins.gitclient.RepositoryCallback; + +/** + * Unsets git hooks. This can get remotely executed on agents. + */ +class UnsetHooks implements RepositoryCallback { + private static final Logger LOGGER = Logger.getLogger(UnsetHooks.class.getName()); + + private static final long serialVersionUID = 1L; + + @Override + public Object invoke(Repository repo, VirtualChannel channel) throws IOException, InterruptedException { + final StoredConfig repoConfig = repo.getConfig(); + final String val = repoConfig.getString("core", null, "hooksPath"); + if (val != null && !val.isEmpty() && !DisableHooks.DISABLED_NIX.equals(val) && !DisableHooks.DISABLED_WIN.equals(val)) { + LOGGER.warning(() -> String.format("core.hooksPath explicitly set to %s and will be left intact on %s.", val, repo.getDirectory())); + } else { + repoConfig.unset("core", null, "hooksPath"); + repoConfig.save(); + } + return null; + } +}