Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #387 - prevent JGit from accessing system config to support Gradle configuration cache #683

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.jgit.transport.TagOpt;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.util.SystemReader;
import org.eclipse.jgit.util.io.DisabledOutputStream;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
Expand Down Expand Up @@ -66,6 +67,8 @@ public class GitRepository implements ScmRepository {
private final ScmProperties properties;

public GitRepository(ScmProperties properties) {
SystemReader.setInstance(new SystemReaderWithoutSystemConfig());

try {
this.repositoryDir = properties.getDirectory();
this.jgitRepository = Git.open(repositoryDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package pl.allegro.tech.build.axion.release.infrastructure.git;

import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.SystemReader;

public class SystemReaderWithoutSystemConfig extends SystemReader
{
private static final SystemReader DefaultSystemReader = SystemReader.getInstance();

@Override
public String getenv(String variable)
{
return DefaultSystemReader.getenv(variable);
}

@Override
public String getHostname()
{
return DefaultSystemReader.getHostname();
}

@Override
public String getProperty(String key)
{
return DefaultSystemReader.getProperty(key);
}

@Override
public long getCurrentTime()
{
return DefaultSystemReader.getCurrentTime();
}

@Override
public int getTimezone(long when)
{
return DefaultSystemReader.getTimezone(when);
}

@Override
public FileBasedConfig openUserConfig(Config parent, FS fs)
{
return DefaultSystemReader.openUserConfig(parent, fs);
}

@Override
public FileBasedConfig openJGitConfig(Config parent, FS fs) {
return DefaultSystemReader.openJGitConfig(parent, fs);
}

// Return an empty system configuration to prevent JGit from accessing it
// This resolves issues with Gradle being unable to save configuration cache
// Based on https://stackoverflow.com/a/59110721
@Override
public FileBasedConfig openSystemConfig(Config parent, FS fs)
{
return new FileBasedConfig(parent, null, fs)
{
@Override
public void load()
{
}

@Override
public boolean isOutdated()
{
return false;
}
};
}
}