Don't inline included configs on FileBasedConfig.save()

Bug: 529825
Change-Id: Id23d4602aa2082d7f2dfe15ae92d7b175b1b8944
Signed-off-by: Marc Strapetz <marc.strapetz@syntevo.com>
This commit is contained in:
Marc Strapetz 2018-08-25 11:33:50 +02:00
parent 31abb329b4
commit f5614d471d
2 changed files with 54 additions and 0 deletions

View File

@ -52,6 +52,7 @@
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 void testIncludeWithTilde()
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);
}

View File

@ -1011,6 +1011,8 @@ private static int findSectionEnd(final List<ConfigLine> entries,
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) {