Skip to content
This repository was archived by the owner on Nov 12, 2019. It is now read-only.

Fix inline diff bug #1

Merged
merged 3 commits into from
Oct 24, 2016
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
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Contributing

To contribute do the following:
1. Check existing open issues.
2. If not found, file an issue with any relevant information (versions, steps to reproduce, stacktrace, screenshots, etc).
3. Fork repository.
4. Create a branch on your fork and add the issue # to he name. Like feature/issue-23 or bug/issue-65.
5. Try to match the style code.
6. Create a pull request to develop branch.

Feel free to send any question.
46 changes: 37 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
A fork of [java-diff-utils](https://code.google.com/p/java-diff-utils/)
# java-diff-utils

# Changelog
The java-diff-utils library is for computing diffs, applying patches, generation side-by-side view in Java.

## 2.1.0
It is an OpenSource library for performing the comparison operations between texts: computing diffs, applying patches, generating unified diffs or parsing them, generating diff output for easy future displaying (like side-by-side view) and so on.

- Removes the dependency on Guava
Main reason to build this library was the lack of easy-to-use libraries with all the usual stuff you need while working with diff files. Originally it was inspired by JRCS library and it's nice design of diff module.

## 2.0.0
**Original code and docs were forked from:** [java-diff-utils](https://code.google.com/p/java-diff-utils/)

## Main Features

* computing the difference between two texts.
* capable to hand more than plain ascci. Arrays or List of any type that implements hashCode() and equals() correctly can be subject to differencing using this library
* patch and unpatch the text with the given patch
* parsing the unified diff format
* producing human-readable differences

## Algorithms

This library implements Myer's diff algorithm. But it can easily replaced by any other which is better for handing your texts.

# Tutorial

* In Spanish: [Comparar Ficheros java-diff-utils](https://www.adictosaltrabajo.com/tutoriales/comparar-ficheros-java-diff-utils/)

## Changelog

### 2.1.1

- Bugfix: Fix issue showing inline diffs.
- Added some unit tests.

### 2.1.0

- Removes the dependency on Guavatime

### 2.0.0

- Change groupId and artifactId to prevent conflict with origin library: now 'com.github.java-diff-utils:java-diff-utils' instead of 'jp.skypencil.java-diff-utils:diffutils'
- Adds the ability to differentiate the inserted and deleted tags and class-names in inline-diff
Expand All @@ -18,18 +47,17 @@ A fork of [java-diff-utils](https://code.google.com/p/java-diff-utils/)
- fix imbrication tag bug in lineDiff (when inline is on a multi-line chunk)
- Adds tha ability to skip data

## 1.5.0
### 1.5.0

- make Equalizer configurable. ([pull #1](https://github.com/eller86/java-diff-utils/pull/1))

## 1.4.1
### 1.4.1

- bugfix: parse method should be public

## 1.4.0
### 1.4.0

- switch from JDK5 to JDK7
- add Guava to dependency
- let user uses other string to represent line which does not exist
- implement event based parser like SAX (in difflib.event package)

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'java'
apply plugin: 'maven'

group = 'com.github.java-diff-utils'
version = '2.1.0-SNAPSHOT'
version = '2.1.1'

description = """java-diff-utils"""

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/difflib/DiffRowGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,19 @@ private void addInlineDiffs(Delta<String> delta) {
}
}

delta.getOriginal().setLines(Arrays.asList(Utils.join(origList, "").split(NEW_LINE)));
delta.getRevised().setLines(Arrays.asList(Utils.join(revList, "").split(NEW_LINE)));
delta.getOriginal().setLines(addMissingLines(origList, orig.size()));
delta.getRevised().setLines(addMissingLines(revList, rev.size()));
}

private List<String> addMissingLines(final List<String> lines, final int targetSize) {
List<String> tempList = Arrays.asList(Utils.join(lines, "").split(NEW_LINE));
if (tempList.size() < targetSize) {
tempList = new ArrayList<>(tempList);
while (tempList.size() < targetSize) {
tempList.add("");
}
}
return tempList;
}

private static final LinkedList<String> charArrayToStringList(char[] cs) {
Expand Down
107 changes: 102 additions & 5 deletions src/test/java/diffutils/DiffRowGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package diffutils;

import java.util.Arrays;
import java.util.List;

import difflib.DiffRow;
import difflib.DiffRowGenerator;

import junit.framework.TestCase;

import java.util.Arrays;
import java.util.List;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

public class DiffRowGeneratorTest extends TestCase {

public void testGenerator_Default() {
Expand Down Expand Up @@ -35,7 +37,7 @@ public void testGenerator_InlineDiff() {
print(rows);

assertEquals(3, rows.size());
assertTrue(rows.get(0).getOldLine().indexOf("<span") > 0);
assertTrue(rows.get(0).getOldLine().indexOf("<del>") > 0);
}

public void testGenerator_IgnoreWhitespaces() {
Expand All @@ -56,6 +58,101 @@ public void testGenerator_IgnoreWhitespaces() {
assertEquals(rows.get(3).getTag(), DiffRow.Tag.CHANGE);
}

public void testChangeToEmptyLine() {
String first = "Test \n \no\n";
String second ="Test\n\no\n";

DiffRowGenerator generator = new DiffRowGenerator.Builder()
.showInlineDiffs(true)
.columnWidth(Integer.MAX_VALUE) // do not wrap
.build();
List<DiffRow> rows = generator.generateDiffRows(split(first), split(second));
print(rows);

assertEquals(3, rows.size());
assertThat(rows.size(), is(3));
assertThat(rows.get(0).getTag(), is(DiffRow.Tag.CHANGE));
assertThat(rows.get(0).getOldLine().indexOf("<del>"), is(4));
assertThat(rows.get(1).getTag(), is(DiffRow.Tag.CHANGE));
assertThat(rows.get(1).getOldLine().indexOf("<del>"), is(0));
assertThat(rows.get(2).getTag(), is(DiffRow.Tag.EQUAL));
}

public void testChangeToTwoEmptyLine() {
String first = "One\n \nTwo\n \nThree\n";
String second ="One\n\nTwo\n\nThree\n";

DiffRowGenerator generator = new DiffRowGenerator.Builder()
.showInlineDiffs(true)
.columnWidth(Integer.MAX_VALUE) // do not wrap
.build();
List<DiffRow> rows = generator.generateDiffRows(split(first), split(second));
print(rows);

assertEquals(5, rows.size());
assertThat(rows.get(0).getTag(), is(DiffRow.Tag.EQUAL));

assertThat(rows.get(1).getTag(), is(DiffRow.Tag.CHANGE));
assertThat(rows.get(1).getOldLine().indexOf("<del>"), is(0));

assertThat(rows.get(2).getTag(), is(DiffRow.Tag.EQUAL));

assertThat(rows.get(3).getTag(), is(DiffRow.Tag.CHANGE));
assertThat(rows.get(3).getOldLine().indexOf("<del>"), is(0));

}

public void testDeleteLine() {
String first ="Equal Line\nDeleted Line\nEqual Line 2\n";
String second = "Equal Line\nEqual Line 2\n";

DiffRowGenerator generator = new DiffRowGenerator.Builder()
.showInlineDiffs(true)
.columnWidth(Integer.MAX_VALUE) // do not wrap
.build();
List<DiffRow> rows = generator.generateDiffRows(split(first), split(second));
print(rows);

assertThat(rows.size(), is(3));
assertThat(rows.get(0).getTag(), is(DiffRow.Tag.EQUAL));
assertThat(rows.get(1).getTag(), is(DiffRow.Tag.DELETE));
assertThat(rows.get(2).getTag(), is(DiffRow.Tag.EQUAL));
}

public void testInsertedLine() {
String first = "Equal Line\nEqual Line 2\n";
String second = "Equal Line\nDeleted Line\nEqual Line 2\n";

DiffRowGenerator generator = new DiffRowGenerator.Builder()
.showInlineDiffs(true)
.columnWidth(Integer.MAX_VALUE) // do not wrap
.build();
List<DiffRow> rows = generator.generateDiffRows(split(first), split(second));
print(rows);

assertThat(rows.size(), is(3));
assertThat(rows.get(0).getTag(), is(DiffRow.Tag.EQUAL));
assertThat(rows.get(1).getTag(), is(DiffRow.Tag.INSERT));
assertThat(rows.get(2).getTag(), is(DiffRow.Tag.EQUAL));
}

public void testChangedLine() {
String first = "Equal Line\nLine to be changed\nEqual Line 2\n";
String second = "Equal Line\nLine changed test\nEqual Line 2\n";

DiffRowGenerator generator = new DiffRowGenerator.Builder()
.showInlineDiffs(true)
.columnWidth(Integer.MAX_VALUE) // do not wrap
.build();
List<DiffRow> rows = generator.generateDiffRows(split(first), split(second));
print(rows);

assertThat(rows.size(), is(3));
assertThat(rows.get(0).getTag(), is(DiffRow.Tag.EQUAL));
assertThat(rows.get(1).getTag(), is(DiffRow.Tag.CHANGE));
assertThat(rows.get(2).getTag(), is(DiffRow.Tag.EQUAL));
}

private List<String> split(String content) {
return Arrays.asList(content.split("\n"));
}
Expand Down