Skip to content

Commit

Permalink
Use a dedicated SystemReader preventing JGit from using system config (
Browse files Browse the repository at this point in the history
  • Loading branch information
SmialyKot authored Nov 28, 2023
1 parent 9895809 commit d60d084
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
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;
}
};
}
}

0 comments on commit d60d084

Please sign in to comment.