assertEquals("[my]\n\tempty\n", c.toText());
}
+ @Test
+ public void testRemoveBranchSection() throws ConfigInvalidException {
+ Config c = parse("" //
+ + "[branch \"keep\"]\n"
+ + " merge = master.branch.to.keep.in.the.file\n"
+ + "\n"
+ + "[branch \"remove\"]\n"
+ + " merge = this.will.get.deleted\n"
+ + " remote = origin-for-some-long-gone-place\n"
+ + "\n"
+ + "\n"
+ + "[core-section-not-to-remove-in-test]\n"
+ + " packedGitLimit = 14\n"
+ + "\n"
+ + "[other]\n"
+ + " foo = bar\n");
+ assertFalse(c.removeSection("branch", "does.not.exist"));
+ assertTrue(c.removeSection("branch", "remove"));
+ assertEquals("" //
+ + "[branch \"keep\"]\n"
+ + " merge = master.branch.to.keep.in.the.file\n"
+ + "\n"
+ + "[core-section-not-to-remove-in-test]\n"
+ + " packedGitLimit = 14\n"
+ + "\n"
+ + "[other]\n"
+ + " foo = bar\n", c.toText());
+ }
+
@Test
public void testUnsetBranchSection() throws ConfigInvalidException {
Config c = parse("" //
+ " merge = this.will.get.deleted\n"
+ " remote = origin-for-some-long-gone-place\n"
+ "\n"
+ + "\n"
+ "[core-section-not-to-remove-in-test]\n"
- + " packedGitLimit = 14\n");
+ + " packedGitLimit = 14\n"
+ + "\n"
+ + "[other]\n"
+ + " foo = bar\n");
c.unsetSection("branch", "does.not.exist");
c.unsetSection("branch", "remove");
assertEquals("" //
+ " merge = master.branch.to.keep.in.the.file\n"
+ "\n"
+ "[core-section-not-to-remove-in-test]\n"
- + " packedGitLimit = 14\n", c.toText());
+ + " packedGitLimit = 14\n"
+ + "\n"
+ + "[other]\n"
+ + " foo = bar\n", c.toText());
}
@Test
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jgit.annotations.NonNull;
* optional subsection value, e.g. a branch name
*/
public void unsetSection(String section, String subsection) {
+ removeSection(section, subsection);
+ }
+
+ /**
+ * Removes all configuration values under a single section.
+ *
+ * @param section
+ * section name, e.g "branch"
+ * @param subsection
+ * optional subsection value, e.g. a branch name
+ * @return {@code true} if a section was present and was removed;
+ * {@code false} if the config was not changed (i.e., no such
+ * section was present)
+ * @since 6.8
+ */
+ public boolean removeSection(String section, String subsection) {
ConfigSnapshot src, res;
+ AtomicBoolean changed = new AtomicBoolean();
do {
src = state.get();
- res = unsetSection(src, section, subsection);
+ changed.set(false);
+ res = unsetSection(src, section, subsection, changed);
} while (!state.compareAndSet(src, res));
+ return changed.get();
}
- private ConfigSnapshot unsetSection(final ConfigSnapshot srcState,
- final String section,
- final String subsection) {
+ private ConfigSnapshot unsetSection(ConfigSnapshot srcState, String section,
+ String subsection, AtomicBoolean changed) {
final int max = srcState.entryList.size();
final ArrayList<ConfigLine> r = new ArrayList<>(max);
boolean lastWasMatch = false;
for (ConfigLine e : srcState.entryList) {
- if (e.includedFrom == null && e.match(section, subsection)) {
- // Skip this record, it's for the section we are removing.
- lastWasMatch = true;
+ if (e.includedFrom != null) {
+ r.add(e);
continue;
}
-
- if (lastWasMatch && e.section == null && e.subsection == null)
+ if (lastWasMatch && e.section == null && e.subsection == null) {
continue; // skip this padding line in the section.
+ }
+ lastWasMatch = e.match(section, subsection);
+ if (lastWasMatch) {
+ // Skip this record, it's for the section we are removing.
+ changed.set(true);
+ continue;
+ }
r.add(e);
}
* Copyright (C) 2009, JetBrains s.r.o.
* Copyright (C) 2008-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
- * Copyright (C) 2008, Thad Hughes <thadh@thad.corp.google.com> and others
+ * Copyright (C) 2008, 2023 Thad Hughes <thadh@thad.corp.google.com> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
-import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.jgit.errors.ConfigInvalidException;
return exists.get();
}
- @Override
- public void setStringList(String section, String subsection, String name,
- List<String> values) {
- super.setStringList(section, subsection, name, values);
- }
-
- @Override
- public void unsetSection(String section, String subsection) {
- super.unsetSection(section, subsection);
- }
-
/**
* {@inheritDoc}
* <p>
}
}
+ @Override
+ public boolean removeSection(String section, String subsection) {
+ if (exists() || !parent.exists()) {
+ return super.removeSection(section, subsection);
+ }
+ return parent.removeSection(section, subsection);
+ }
+
@Override
public boolean isOutdated() {
return super.isOutdated() || parent.isOutdated();