Skip to content

Commit

Permalink
Fix configuration cache misses caused by JGit shelling out to git (#1837
Browse files Browse the repository at this point in the history
 fixes #1806)
  • Loading branch information
nedtwigg authored Sep 28, 2023
2 parents 6c1df6d + c320ffe commit c4e5be7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Support configuration of mirrors for P2 repositories in maven DSL ([#1697](https://github.com/diffplug/spotless/issues/1697)).
### Fixed
* Fix support for plugins when using Prettier version `3.0.0` and newer. ([#1802](https://github.com/diffplug/spotless/pull/1802))
* Fix configuration cache issue around `external process started '/usr/bin/git --version'`. ([#1806](https://github.com/diffplug/spotless/issues/1806))
### Changes
* Bump default `flexmark` version to latest `0.64.0` -> `0.64.8`. ([#1801](https://github.com/diffplug/spotless/pull/1801))
* Bump default `ktlint` version to latest `0.50.0` -> `1.0.0`. ([#1808](https://github.com/diffplug/spotless/pull/1808))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 DiffPlug
* Copyright 2020-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,10 +19,33 @@

import javax.annotation.Nullable;

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;

import com.diffplug.spotless.extra.GitRatchet;

/** Gradle implementation of GitRatchet. */
public class GitRatchetGradle extends GitRatchet<File> {
static {
preventJGitFromCallingExecutables();
}

static void preventJGitFromCallingExecutables() {
SystemReader reader = SystemReader.getInstance();
SystemReader.setInstance(new DelegatingSystemReader(reader) {
@Override
public String getenv(String variable) {
if ("PATH".equals(variable)) {
return "";
} else {
return super.getenv(variable);
}
}
});
}

@Override
protected File getDir(File project) {
return project;
Expand All @@ -32,4 +55,52 @@ protected File getDir(File project) {
protected @Nullable File getParent(File project) {
return project.getParentFile();
}

static class DelegatingSystemReader extends SystemReader {
final SystemReader reader;

DelegatingSystemReader(SystemReader reader) {
this.reader = reader;
}

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

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

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

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

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

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

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

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

0 comments on commit c4e5be7

Please sign in to comment.