diff options
author | Matthias Sohn <matthias.sohn@sap.com> | 2019-08-26 22:43:11 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2019-08-30 02:34:13 +0200 |
commit | af0296b42c150cf2191e2279cb50cfec8ff8a270 (patch) | |
tree | fe4c67caf170c69eac2282a148424c0327b057da /org.eclipse.jgit | |
parent | 418722df34ca007362e23be60ef57dbe6f1d1b7a (diff) | |
download | jgit-af0296b42c150cf2191e2279cb50cfec8ff8a270.tar.gz jgit-af0296b42c150cf2191e2279cb50cfec8ff8a270.zip |
Return a new instance from openSystemConfig and openUserConfig
Move the handling of cached user and system config to getSystemConfig
and getUserConfig methods and revert the implementation of
openSystemConfig and openUserConfig to the old stateless
implementation.
This ensures the open methods respect the passed-in parent config, which
may be different on each invocation. Additionally, returning a new
instance matches the behavior of the previous implementation of the
default system reader, which downstream callers may be depending on.
Move the implementation of the new caching methods getSystemConfig and
getUserConfig up to SystemReader. This avoids that we break the ABI for
subclasses of SystemReader.
Also see [1] which fixed a similar problem with Gerrit's custom
SystemReader.
[1] https://gerrit-review.googlesource.com/c/gerrit/+/225458
Change-Id: If54a2491932d8fc914d4649cb73c9e837c5b8ad0
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/.settings/.api_filters | 12 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java | 89 |
2 files changed, 40 insertions, 61 deletions
diff --git a/org.eclipse.jgit/.settings/.api_filters b/org.eclipse.jgit/.settings/.api_filters index bffd5c4c01..238dcc7210 100644 --- a/org.eclipse.jgit/.settings/.api_filters +++ b/org.eclipse.jgit/.settings/.api_filters @@ -249,18 +249,6 @@ </filter> </resource> <resource path="src/org/eclipse/jgit/util/SystemReader.java" type="org.eclipse.jgit.util.SystemReader"> - <filter id="336695337"> - <message_arguments> - <message_argument value="org.eclipse.jgit.util.SystemReader"/> - <message_argument value="getSystemConfig()"/> - </message_arguments> - </filter> - <filter id="336695337"> - <message_arguments> - <message_argument value="org.eclipse.jgit.util.SystemReader"/> - <message_argument value="getUserConfig()"/> - </message_arguments> - </filter> <filter id="1142947843"> <message_arguments> <message_argument value="5.1.9"/> diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java index 310bc83a66..cccac662c4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java @@ -98,10 +98,6 @@ public abstract class SystemReader { private static class Default extends SystemReader { private volatile String hostname; - private AtomicReference<FileBasedConfig> systemConfig = new AtomicReference<>(); - - private volatile AtomicReference<FileBasedConfig> userConfig = new AtomicReference<>(); - @Override public String getenv(String variable) { return System.getenv(variable); @@ -114,23 +110,14 @@ public abstract class SystemReader { @Override public FileBasedConfig openSystemConfig(Config parent, FS fs) { - FileBasedConfig c = systemConfig.get(); - if (c == null) { - systemConfig.compareAndSet(null, - createSystemConfig(parent, fs)); - c = systemConfig.get(); - } - return c; - } - - protected FileBasedConfig createSystemConfig(Config parent, FS fs) { - if (StringUtils.isEmptyOrNull(getenv(Constants.GIT_CONFIG_NOSYSTEM_KEY))) { + if (StringUtils + .isEmptyOrNull(getenv(Constants.GIT_CONFIG_NOSYSTEM_KEY))) { File configFile = fs.getGitSystemConfig(); if (configFile != null) { return new FileBasedConfig(parent, configFile, fs); } } - return new FileBasedConfig(null, fs) { + return new FileBasedConfig(parent, null, fs) { @Override public void load() { // empty, do not load @@ -146,35 +133,8 @@ public abstract class SystemReader { @Override public FileBasedConfig openUserConfig(Config parent, FS fs) { - FileBasedConfig c = userConfig.get(); - if (c == null) { - userConfig.compareAndSet(null, new FileBasedConfig(parent, - new File(fs.userHome(), ".gitconfig"), fs)); //$NON-NLS-1$ - c = userConfig.get(); - } - return c; - } - - @Override - public StoredConfig getSystemConfig() - throws IOException, ConfigInvalidException { - FileBasedConfig c = openSystemConfig(null, FS.DETECTED); - if (c.isOutdated()) { - LOG.debug("loading system config {}", systemConfig); //$NON-NLS-1$ - c.load(); - } - return c; - } - - @Override - public StoredConfig getUserConfig() - throws IOException, ConfigInvalidException { - FileBasedConfig c = openUserConfig(getSystemConfig(), FS.DETECTED); - if (c.isOutdated()) { - LOG.debug("loading user config {}", userConfig); //$NON-NLS-1$ - c.load(); - } - return c; + return new FileBasedConfig(parent, new File(fs.userHome(), ".gitconfig"), //$NON-NLS-1$ + fs); } @Override @@ -234,6 +194,10 @@ public abstract class SystemReader { private ObjectChecker platformChecker; + private AtomicReference<FileBasedConfig> systemConfig = new AtomicReference<>(); + + private AtomicReference<FileBasedConfig> userConfig = new AtomicReference<>(); + private void init() { // Creating ObjectChecker must be deferred. Unit tests change // behavior of is{Windows,MacOS} in constructor of subclass. @@ -323,8 +287,23 @@ public abstract class SystemReader { * if something went wrong when reading files * @since 5.1.9 */ - public abstract StoredConfig getUserConfig() - throws IOException, ConfigInvalidException; + public StoredConfig getUserConfig() + throws IOException, ConfigInvalidException { + FileBasedConfig c = userConfig.get(); + if (c == null) { + userConfig.compareAndSet(null, + openUserConfig(getSystemConfig(), FS.DETECTED)); + c = userConfig.get(); + } else { + // Ensure the parent is up to date + getSystemConfig(); + } + if (c.isOutdated()) { + LOG.debug("loading user config {}", userConfig); //$NON-NLS-1$ + c.load(); + } + return c; + } /** * Get the gitconfig configuration found in the system-wide "etc" directory. @@ -339,8 +318,20 @@ public abstract class SystemReader { * if something went wrong when reading files * @since 5.1.9 */ - public abstract StoredConfig getSystemConfig() - throws IOException, ConfigInvalidException; + public StoredConfig getSystemConfig() + throws IOException, ConfigInvalidException { + FileBasedConfig c = systemConfig.get(); + if (c == null) { + systemConfig.compareAndSet(null, + openSystemConfig(null, FS.DETECTED)); + c = systemConfig.get(); + } + if (c.isOutdated()) { + LOG.debug("loading system config {}", systemConfig); //$NON-NLS-1$ + c.load(); + } + return c; + } /** * Get the current system time |