Skip to content

Commit

Permalink
Fix save order (#9875)
Browse files Browse the repository at this point in the history
Co-authored-by: Christoph <siedlerkiller@gmail.com>
  • Loading branch information
koppor and Siedlerchr authored May 10, 2023
1 parent 024c927 commit 96467cd
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ private void saveDatabase(BibDatabase newBase, String subName) {
try (AtomicFileWriter fileWriter = new AtomicFileWriter(Path.of(subName), StandardCharsets.UTF_8)) {
BibWriter bibWriter = new BibWriter(fileWriter, OS.NEWLINE);
SaveConfiguration saveConfiguration = new SaveConfiguration()
.withMetadataSaveOrder(true)
.withReformatOnSave(preferencesService.getExportPreferences().shouldAlwaysReformatOnSave());

BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding,

SaveConfiguration saveConfiguration = new SaveConfiguration()
.withSaveType(saveType)
.withMetadataSaveOrder(true)
.withReformatOnSave(preferences.getExportPreferences().shouldAlwaysReformatOnSave());
BibDatabaseContext bibDatabaseContext = libraryTab.getBibDatabaseContext();
synchronized (bibDatabaseContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ Optional<Path> determineBackupPathForNewBackup(Path backupDir) {
*
* <em>SIDE EFFECT: Deletes oldest backup file</em>
*
*
* @param backupPath the full path to the file where the library should be backed up to
*/
void performBackup(Path backupPath) {
Expand All @@ -226,6 +225,7 @@ void performBackup(Path backupPath) {
// code similar to org.jabref.gui.exporter.SaveDatabaseAction.saveDatabase
SaveConfiguration saveConfiguration = new SaveConfiguration()
.withMakeBackup(false)
.withMetadataSaveOrder(true)
.withReformatOnSave(preferences.getExportPreferences().shouldAlwaysReformatOnSave());

Charset encoding = bibDatabaseContext.getMetaData().getEncoding().orElse(StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ private void generateCiteKeys(BibDatabaseContext existingEntries, BibDatabase ta
private void writeResultToFile(Path pathToFile, BibDatabase entries) throws SaveException {
try (AtomicFileWriter fileWriter = new AtomicFileWriter(pathToFile, StandardCharsets.UTF_8)) {
SaveConfiguration saveConfiguration = new SaveConfiguration()
.withMetadataSaveOrder(true)
.withReformatOnSave(preferencesService.getExportPreferences().shouldAlwaysReformatOnSave());
BibWriter bibWriter = new BibWriter(fileWriter, OS.NEWLINE);
BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/org/jabref/logic/exporter/BibDatabaseWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static List<Comparator<BibEntry>> getSaveComparators(MetaData metaData,
// ones. This is a necessary requirement for BibTeX to be able to resolve referenced entries correctly.
comparators.add(new CrossRefEntryComparator());

if (saveOrder.isEmpty()) {
if (saveOrder.isEmpty() || saveOrder.get().getOrderType() == SaveOrder.OrderType.ORIGINAL) {
// entries will be sorted based on their internal IDs
comparators.add(new IdComparator());
} else {
Expand Down Expand Up @@ -146,22 +146,21 @@ public static List<BibEntry> getSortedEntries(BibDatabaseContext bibDatabaseCont
return sorted;
}

private static Optional<SaveOrder> getSaveOrder(MetaData metaData, SaveConfiguration preferences) {
/* three options:
* 1. original order
* 2. order specified in metaData
* 3. order specified in preferences
private static Optional<SaveOrder> getSaveOrder(MetaData metaData, SaveConfiguration saveConfiguration) {
/* two options:
* 1. order specified in metaData
* 2. original order
*/

if (preferences.getSaveOrder().getOrderType() == SaveOrder.OrderType.ORIGINAL) {
return Optional.empty();
if (saveConfiguration.useMetadataSaveOrder()) {
return metaData.getSaveOrderConfig();
}

if (preferences.useMetadataSaveOrder()) {
return metaData.getSaveOrderConfig();
if (saveConfiguration.getSaveOrder().getOrderType() == SaveOrder.OrderType.ORIGINAL) {
return Optional.empty();
}

return Optional.ofNullable(preferences.getSaveOrder());
return Optional.ofNullable(saveConfiguration.getSaveOrder());
}

public List<FieldChange> getSaveActionsFieldChanges() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -29,8 +27,9 @@
/**
* Tests whole citation key patterns such as <code>[authorsAlpha][year]</code>.
* The concrete patterns such as <code>authorsAlpha</code> should better be tested at {@link BracketedPatternTest}.
*
* Concurrent execution leads to issues on GitHub actions.
*/
@Execution(ExecutionMode.CONCURRENT)
class CitationKeyGeneratorTest {

private static final BibEntry AUTHOR_EMPTY = createABibEntryAuthor("");
Expand Down

0 comments on commit 96467cd

Please sign in to comment.