]> source.dussan.org Git - jgit.git/commitdiff
Return a new instance from openSystemConfig and openUserConfig 73/148373/4
authorMatthias Sohn <matthias.sohn@sap.com>
Mon, 26 Aug 2019 20:43:11 +0000 (22:43 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Fri, 30 Aug 2019 00:34:13 +0000 (02:34 +0200)
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>
org.eclipse.jgit.test/META-INF/MANIFEST.MF
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/SystemReaderTest.java [new file with mode: 0644]
org.eclipse.jgit/.settings/.api_filters
org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java

index 71a6fc199b941244df6306120d79c134f56e7543..b74d8dacc4c00350cc90086f504468c4ab82739f 100644 (file)
@@ -70,6 +70,7 @@ Import-Package: com.googlecode.javaewah;version="[1.1.6,2.0.0)",
  org.junit.runner;version="[4.12,5.0.0)",
  org.junit.runners;version="[4.12,5.0.0)",
  org.mockito;version="[2.23.0,3.0.0)",
+ org.mockito.junit;version="[2.23.0,3.0.0)",
  org.mockito.stubbing;version="2.23.0",
  org.objenesis;version="2.6.0",
  org.slf4j;version="[1.7.0,2.0.0)",
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/SystemReaderTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/SystemReaderTest.java
new file mode 100644 (file)
index 0000000..1fe1ba9
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2019, Matthias Sohn <matthias.sohn@sap.com>
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.eclipse.jgit.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.eclipse.jgit.storage.file.FileBasedConfig;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class SystemReaderTest {
+       private Path trash;
+
+       private Path mockSystemConfig;
+
+       private Path mockUserConfig;
+
+       @Mock
+       private FS fs;
+
+       @Before
+       public void setup() throws Exception {
+               trash = Files.createTempDirectory("jgit_test");
+               mockSystemConfig = trash.resolve("systemgitconfig");
+               Files.write(mockSystemConfig, "[core]\n  trustFolderStat = false\n"
+                               .getBytes(StandardCharsets.UTF_8));
+               mockUserConfig = trash.resolve(".gitconfig");
+               Files.write(mockUserConfig,
+                               "[core]\n  bare = false\n".getBytes(StandardCharsets.UTF_8));
+               when(fs.getGitSystemConfig()).thenReturn(mockSystemConfig.toFile());
+               when(fs.userHome()).thenReturn(trash.toFile());
+               SystemReader.setInstance(null);
+       }
+
+       @After
+       public void teardown() throws Exception {
+               FileUtils.delete(trash.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
+       }
+
+       @Test
+       public void openSystemConfigReturnsDifferentInstances() throws Exception {
+               FileBasedConfig system1 = SystemReader.getInstance()
+                               .openSystemConfig(null, fs);
+               system1.load();
+               assertEquals("false",
+                               system1.getString("core", null, "trustFolderStat"));
+
+               FileBasedConfig system2 = SystemReader.getInstance()
+                               .openSystemConfig(null, fs);
+               system2.load();
+               assertEquals("false",
+                               system2.getString("core", null, "trustFolderStat"));
+
+               system1.setString("core", null, "trustFolderStat", "true");
+               assertEquals("true",
+                               system1.getString("core", null, "trustFolderStat"));
+               assertEquals("false",
+                               system2.getString("core", null, "trustFolderStat"));
+       }
+
+       @Test
+       public void openUserConfigReturnsDifferentInstances() throws Exception {
+               FileBasedConfig user1 = SystemReader.getInstance().openUserConfig(null,
+                               fs);
+               user1.load();
+               assertEquals("false", user1.getString("core", null, "bare"));
+
+               FileBasedConfig user2 = SystemReader.getInstance().openUserConfig(null,
+                               fs);
+               user2.load();
+               assertEquals("false", user2.getString("core", null, "bare"));
+
+               user1.setString("core", null, "bare", "true");
+               assertEquals("true", user1.getString("core", null, "bare"));
+               assertEquals("false", user2.getString("core", null, "bare"));
+       }
+}
index bffd5c4c0198f4b14eaa179465e86d0443b42460..238dcc7210ea719e5324fd698f4e6058087a2478 100644 (file)
         </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"/>
index 310bc83a66c3c7c3e0af48fc12aee03631739803..cccac662c439da106091964b5123bb21613f3e76 100644 (file)
@@ -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