]> source.dussan.org Git - jgit.git/commitdiff
Don't inline included configs on FileBasedConfig.save() 47/127847/5
authorMarc Strapetz <marc.strapetz@syntevo.com>
Sat, 25 Aug 2018 09:33:50 +0000 (11:33 +0200)
committerMarc Strapetz <marc.strapetz@syntevo.com>
Thu, 20 Sep 2018 15:05:39 +0000 (17:05 +0200)
Bug: 529825
Change-Id: Id23d4602aa2082d7f2dfe15ae92d7b175b1b8944
Signed-off-by: Marc Strapetz <marc.strapetz@syntevo.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/FileBasedConfigTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Config.java

index 7f0d60295c9936029877b8cbcb5f1cf8f51673b5..777a784c33ac7c531753962aa7a08736322f4390 100644 (file)
@@ -52,6 +52,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.StringTokenizer;
 
 import org.eclipse.jgit.errors.ConfigInvalidException;
 import org.eclipse.jgit.util.FS;
@@ -67,16 +68,23 @@ public class FileBasedConfigTest {
 
        private static final String NAME = "name";
 
+       private static final String EMAIL = "email";
+
        private static final String ALICE = "Alice";
 
        private static final String BOB = "Bob";
 
+       private static final String ALICE_EMAIL = "alice@home";
+
        private static final String CONTENT1 = "[" + USER + "]\n\t" + NAME + " = "
                        + ALICE + "\n";
 
        private static final String CONTENT2 = "[" + USER + "]\n\t" + NAME + " = "
                        + BOB + "\n";
 
+       private static final String CONTENT3 = "[" + USER + "]\n\t" + NAME + " = "
+                       + ALICE + "\n" + "[" + USER + "]\n\t" + EMAIL + " = " + ALICE_EMAIL;
+
        private File trash;
 
        @Before
@@ -233,6 +241,50 @@ public class FileBasedConfigTest {
                assertEquals(ALICE, config.getString(USER, null, NAME));
        }
 
+       @Test
+       public void testIncludeDontInlineIncludedLinesOnSave()
+                       throws IOException, ConfigInvalidException {
+               // use a content with multiple sections and multiple key/value pairs
+               // because code for first line works different than for subsequent lines
+               final File includedFile = createFile(CONTENT3.getBytes(), "dir1");
+
+               final File file = createFile(new byte[0], "dir2");
+               FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
+               config.setString("include", null, "path",
+                               ("../" + includedFile.getParentFile().getName() + "/"
+                                               + includedFile.getName()));
+
+               // just by setting the include.path, it won't be included
+               assertEquals(null, config.getString(USER, null, NAME));
+               assertEquals(null, config.getString(USER, null, EMAIL));
+               config.save();
+
+               // and it won't be included after saving
+               assertEquals(null, config.getString(USER, null, NAME));
+               assertEquals(null, config.getString(USER, null, EMAIL));
+
+               final String expectedText = config.toText();
+               assertEquals(2,
+                               new StringTokenizer(expectedText, "\n", false).countTokens());
+
+               config = new FileBasedConfig(file, FS.DETECTED);
+               config.load();
+
+               String actualText = config.toText();
+               assertEquals(expectedText, actualText);
+               // but it will be included after (re)loading
+               assertEquals(ALICE, config.getString(USER, null, NAME));
+               assertEquals(ALICE_EMAIL, config.getString(USER, null, EMAIL));
+
+               config.save();
+
+               actualText = config.toText();
+               assertEquals(expectedText, actualText);
+               // and of course preserved after saving
+               assertEquals(ALICE, config.getString(USER, null, NAME));
+               assertEquals(ALICE_EMAIL, config.getString(USER, null, EMAIL));
+       }
+
        private File createFile(byte[] content) throws IOException {
                return createFile(content, null);
        }
index 6a8ff5ade49a7ae48b208db69fbd7ed705e129ab..97acac286fef091ecba24ed06eaaaec0cf146e2b 100644 (file)
@@ -1011,6 +1011,8 @@ public class Config {
        public String toText() {
                final StringBuilder out = new StringBuilder();
                for (ConfigLine e : state.get().entryList) {
+                       if (e.includedFrom != null)
+                               continue;
                        if (e.prefix != null)
                                out.append(e.prefix);
                        if (e.section != null && e.name == null) {