Skip to content

Commit

Permalink
Revert "Move hunks getting executed on agents to separate classes (#1652
Browse files Browse the repository at this point in the history
)"

This reverts commit beda22c.
  • Loading branch information
MarkEWaite committed Sep 20, 2024
1 parent ca0612e commit 97effbe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 59 deletions.
27 changes: 0 additions & 27 deletions src/main/java/jenkins/plugins/git/DisableHooks.java

This file was deleted.

35 changes: 33 additions & 2 deletions src/main/java/jenkins/plugins/git/GitHooksConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,31 @@

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;

Expand Down Expand Up @@ -101,10 +109,33 @@ 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(new DisableHooks());
client.withRepository((repo, channel) -> {
disable(repo);
return null;
});
} else {
client.withRepository(new UnsetHooks());
client.withRepository((repo, channel) -> {
unset(repo);
return null;
});
}
}

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)) {

Check warning on line 127 in src/main/java/jenkins/plugins/git/GitHooksConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 127 is only partially covered, 2 branches are missing
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;

Check warning on line 136 in src/main/java/jenkins/plugins/git/GitHooksConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 136 is only partially covered, one branch is missing
final StoredConfig repoConfig = repo.getConfig();
repoConfig.setString("core", null, "hooksPath", VAL);
repoConfig.save();
}
}
30 changes: 0 additions & 30 deletions src/main/java/jenkins/plugins/git/UnsetHooks.java

This file was deleted.

0 comments on commit 97effbe

Please sign in to comment.