Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
public final class UnifiedDiffUtils {

private static final Pattern UNIFIED_DIFF_CHUNK_REGEXP = Pattern
.compile("^@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@$");
.compile("^@@\\s+-(\\d+)(?:,(\\d+))?\\s+\\+(\\d+)(?:,(\\d+))?\\s+@@.*$");
private static final String NULL_FILE_INDICATOR = "/dev/null";

/**
Expand Down Expand Up @@ -352,7 +352,7 @@ public static List<String> generateOriginalAndDiff(List<String> original, List<S
*/
public static List<String> generateOriginalAndDiff(List<String> original, List<String> revised, String originalFileName, String revisedFileName) {
String originalFileNameTemp = originalFileName;
String revisedFileNameTemp = originalFileName;
String revisedFileNameTemp = revisedFileName;
if (originalFileNameTemp == null) {
originalFileNameTemp = "original";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.github.difflib.patch.InsertDelta;
import com.github.difflib.patch.Patch;
import com.github.difflib.text.DiffRow.Tag;
import com.github.difflib.text.deltamerge.DeltaMergeUtils;
import com.github.difflib.text.deltamerge.InlineDeltaMergeInfo;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
Expand Down Expand Up @@ -75,6 +77,14 @@ public final class DiffRowGenerator {
public static final Function<String, List<String>> SPLITTER_BY_WORD = line -> splitStringPreserveDelimiter(line, SPLIT_BY_WORD_PATTERN);
public static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");

public static final Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> DEFAULT_INLINE_DELTA_MERGER = InlineDeltaMergeInfo::getDeltas;

/**
* Merge diffs which are separated by equalities consisting of whitespace only.
*/
public static final Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> WHITESPACE_EQUALITIES_MERGER = deltaMergeInfo -> DeltaMergeUtils
.mergeInlineDeltas(deltaMergeInfo, equalities -> equalities.stream().allMatch(s -> s==null || s.replaceAll("\\s+", "").equals("")));

public static Builder create() {
return new Builder();
}
Expand Down Expand Up @@ -170,6 +180,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
private final boolean reportLinesUnchanged;
private final Function<String, String> lineNormalizer;
private final Function<String, String> processDiffs;
private final Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> inlineDeltaMerger;

private final boolean showInlineDiffs;
private final boolean replaceOriginalLinefeedInChangesWithSpaces;
Expand All @@ -194,11 +205,13 @@ private DiffRowGenerator(Builder builder) {
reportLinesUnchanged = builder.reportLinesUnchanged;
lineNormalizer = builder.lineNormalizer;
processDiffs = builder.processDiffs;
inlineDeltaMerger = builder.inlineDeltaMerger;

replaceOriginalLinefeedInChangesWithSpaces = builder.replaceOriginalLinefeedInChangesWithSpaces;

Objects.requireNonNull(inlineDiffSplitter);
Objects.requireNonNull(lineNormalizer);
Objects.requireNonNull(inlineDeltaMerger);
}

/**
Expand Down Expand Up @@ -370,7 +383,10 @@ private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta) {
origList = inlineDiffSplitter.apply(joinedOrig);
revList = inlineDiffSplitter.apply(joinedRev);

List<AbstractDelta<String>> inlineDeltas = DiffUtils.diff(origList, revList, equalizer).getDeltas();
List<AbstractDelta<String>> originalInlineDeltas = DiffUtils.diff(origList, revList, equalizer)
.getDeltas();
List<AbstractDelta<String>> inlineDeltas = inlineDeltaMerger
.apply(new InlineDeltaMergeInfo(originalInlineDeltas, origList, revList));

Collections.reverse(inlineDeltas);
for (AbstractDelta<String> inlineDelta : inlineDeltas) {
Expand Down Expand Up @@ -465,6 +481,7 @@ public static class Builder {
private Function<String, String> processDiffs = null;
private BiPredicate<String, String> equalizer = null;
private boolean replaceOriginalLinefeedInChangesWithSpaces = false;
private Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> inlineDeltaMerger = DEFAULT_INLINE_DELTA_MERGER;

private Builder() {
}
Expand Down Expand Up @@ -673,5 +690,17 @@ public Builder replaceOriginalLinefeedInChangesWithSpaces(boolean replace) {
this.replaceOriginalLinefeedInChangesWithSpaces = replace;
return this;
}

/**
* Provide an inline delta merger for use case specific delta optimizations.
*
* @param inlineDeltaMerger
* @return
*/
public Builder inlineDeltaMerger(
Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> inlineDeltaMerger) {
this.inlineDeltaMerger = inlineDeltaMerger;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2009-2024 java-diff-utils.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.difflib.text.deltamerge;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.patch.ChangeDelta;
import com.github.difflib.patch.Chunk;

/**
* Provides utility features for merge inline deltas
*
* @author <a href="christian.meier@epictec.ch">Christian Meier</a>
*/
final public class DeltaMergeUtils {

public static List<AbstractDelta<String>> mergeInlineDeltas(InlineDeltaMergeInfo deltaMergeInfo,
Predicate<List<String>> replaceEquality) {
final List<AbstractDelta<String>> originalDeltas = deltaMergeInfo.getDeltas();
if (originalDeltas.size() < 2) {
return originalDeltas;
}

final List<AbstractDelta<String>> newDeltas = new ArrayList<>();
newDeltas.add(originalDeltas.get(0));
for (int i = 1; i < originalDeltas.size(); i++) {
final AbstractDelta<String> previousDelta = newDeltas.get(newDeltas.size()-1);
final AbstractDelta<String> currentDelta = originalDeltas.get(i);

final List<String> equalities = deltaMergeInfo.getOrigList().subList(
previousDelta.getSource().getPosition() + previousDelta.getSource().size(),
currentDelta.getSource().getPosition());

if (replaceEquality.test(equalities)) {
// Merge the previous delta, the equality and the current delta into one
// ChangeDelta and replace the previous delta by this new ChangeDelta.
final List<String> allSourceLines = new ArrayList<>();
allSourceLines.addAll(previousDelta.getSource().getLines());
allSourceLines.addAll(equalities);
allSourceLines.addAll(currentDelta.getSource().getLines());

final List<String> allTargetLines = new ArrayList<>();
allTargetLines.addAll(previousDelta.getTarget().getLines());
allTargetLines.addAll(equalities);
allTargetLines.addAll(currentDelta.getTarget().getLines());

final ChangeDelta<String> replacement = new ChangeDelta<>(
new Chunk<>(previousDelta.getSource().getPosition(), allSourceLines),
new Chunk<>(previousDelta.getTarget().getPosition(), allTargetLines));

newDeltas.remove(newDeltas.size()-1);
newDeltas.add(replacement);
} else {
newDeltas.add(currentDelta);
}
}

return newDeltas;
}

private DeltaMergeUtils() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2009-2024 java-diff-utils.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.difflib.text.deltamerge;

import java.util.List;

import com.github.difflib.patch.AbstractDelta;

/**
* Holds the information required to merge deltas originating from an inline
* diff
*
* @author <a href="christian.meier@epictec.ch">Christian Meier</a>
*/
public final class InlineDeltaMergeInfo {

private final List<AbstractDelta<String>> deltas;
private final List<String> origList;
private final List<String> revList;

public InlineDeltaMergeInfo(List<AbstractDelta<String>> deltas, List<String> origList, List<String> revList) {
this.deltas = deltas;
this.origList = origList;
this.revList = revList;
}

public List<AbstractDelta<String>> getDeltas() {
return deltas;
}

public List<String> getOrigList() {
return origList;
}

public List<String> getRevList() {
return revList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ public void testGenerateUnifiedWithOneDelta() throws IOException {
@Test
public void testGenerateUnifiedDiffWithoutAnyDeltas() {
List<String> test = Arrays.asList("abc");
Patch<String> patch = DiffUtils.diff(test, test);
UnifiedDiffUtils.generateUnifiedDiff("abc", "abc", test, patch, 0);
List<String> testRevised = Arrays.asList("abc2");
Patch<String> patch = DiffUtils.diff(test, testRevised);
String unifiedDiffTxt = String.join("\n", UnifiedDiffUtils.generateUnifiedDiff("abc1", "abc2", test, patch, 0));
System.out.println(unifiedDiffTxt);

assertThat(unifiedDiffTxt)
.as("original filename should be abc1").contains("--- abc1")
.as("revised filename should be abc2").contains("+++ abc2");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Pattern;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
Expand All @@ -20,6 +21,10 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.text.deltamerge.DeltaMergeUtils;
import com.github.difflib.text.deltamerge.InlineDeltaMergeInfo;

public class DiffRowGeneratorTest {

@Test
Expand Down Expand Up @@ -791,9 +796,50 @@ public void testIssue129SkipWhitespaceChanges() throws IOException {
.forEach(System.out::println);
}

@Test
public void testGeneratorWithWhitespaceDeltaMerge() {
final DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).mergeOriginalRevised(true)
.inlineDiffByWord(true).oldTag(f -> "~").newTag(f -> "**") //
.lineNormalizer(StringUtils::htmlEntites) // do not replace tabs
.inlineDeltaMerger(DiffRowGenerator.WHITESPACE_EQUALITIES_MERGER).build();

assertInlineDiffResult(generator, "No diff", "No diff", "No diff");
assertInlineDiffResult(generator, " x whitespace before diff", " y whitespace before diff",
" ~x~**y** whitespace before diff");
assertInlineDiffResult(generator, "Whitespace after diff x ", "Whitespace after diff y ",
"Whitespace after diff ~x~**y** ");
assertInlineDiffResult(generator, "Diff x x between", "Diff y y between", "Diff ~x x~**y y** between");
assertInlineDiffResult(generator, "Hello \t world", "Hi \t universe", "~Hello \t world~**Hi \t universe**");
assertInlineDiffResult(generator, "The quick brown fox jumps over the lazy dog", "A lazy dog jumps over a fox",
"~The quick brown fox ~**A lazy dog **jumps over ~the lazy dog~**a fox**");
}

@Test
public void testGeneratorWithMergingDeltasForShortEqualities() {
final Function<InlineDeltaMergeInfo, List<AbstractDelta<String>>> shortEqualitiesMerger = deltaMergeInfo -> DeltaMergeUtils
.mergeInlineDeltas(deltaMergeInfo,
equalities -> equalities.stream().mapToInt(String::length).sum() < 6);

final DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).mergeOriginalRevised(true)
.inlineDiffByWord(true).oldTag(f -> "~").newTag(f -> "**").inlineDeltaMerger(shortEqualitiesMerger)
.build();

assertInlineDiffResult(generator, "No diff", "No diff", "No diff");
assertInlineDiffResult(generator, "aaa bbb ccc", "xxx bbb zzz", "~aaa bbb ccc~**xxx bbb zzz**");
assertInlineDiffResult(generator, "aaa bbbb ccc", "xxx bbbb zzz", "~aaa~**xxx** bbbb ~ccc~**zzz**");
}

private void assertInlineDiffResult(DiffRowGenerator generator, String original, String revised, String expected) {
final List<DiffRow> rows = generator.generateDiffRows(Arrays.asList(original), Arrays.asList(revised));
print(rows);

assertEquals(1, rows.size());
assertEquals(expected, rows.get(0).getOldLine().toString());
}

@Test
public void testIssue188HangOnExamples() throws IOException, URISyntaxException {
try (FileSystem zipFs = FileSystems.newFileSystem(Paths.get("target/test-classes/com/github/difflib/text/test.zip"), null);) {
try (FileSystem zipFs = FileSystems.newFileSystem(Paths.get("target/test-classes/com/github/difflib/text/test.zip"), (ClassLoader) null);) {
List<String> original = Files.readAllLines(zipFs.getPath("old.html"));
List<String> revised = Files.readAllLines(zipFs.getPath("new.html"));

Expand Down