mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-19 20:25:31 +00:00
normalized line endings
This commit is contained in:
@@ -1,28 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>com.googlecode.java-diff-utils.test</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.ManifestBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.SchemaBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.pde.PluginNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>com.googlecode.java-diff-utils.test</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.ManifestBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.pde.SchemaBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.pde.PluginNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#Fri Jul 29 09:33:56 CEST 2011
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
||||
#Fri Jul 29 09:33:56 CEST 2011
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#Fri Jun 10 10:54:00 CEST 2011
|
||||
eclipse.preferences.version=1
|
||||
pluginProject.extensions=false
|
||||
resolve.requirebundle=false
|
||||
#Fri Jun 10 10:54:00 CEST 2011
|
||||
eclipse.preferences.version=1
|
||||
pluginProject.extensions=false
|
||||
resolve.requirebundle=false
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
source.. = src/
|
||||
output.. = bin/,\
|
||||
test
|
||||
bin.includes = META-INF/,\
|
||||
.,\
|
||||
test/
|
||||
source.. = src/
|
||||
output.. = bin/,\
|
||||
test
|
||||
bin.includes = META-INF/,\
|
||||
.,\
|
||||
test/
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>root</artifactId>
|
||||
<groupId>com.sap.sailing</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>com.googlecode.java-diff-utils.test</artifactId>
|
||||
<packaging>eclipse-test-plugin</packaging>
|
||||
</project>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>root</artifactId>
|
||||
<groupId>com.sap.sailing</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>com.googlecode.java-diff-utils.test</artifactId>
|
||||
<packaging>eclipse-test-plugin</packaging>
|
||||
</project>
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
package diffutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import difflib.DiffUtils;
|
||||
import difflib.Patch;
|
||||
import difflib.PatchFailedException;
|
||||
|
||||
public class ApplyPatch extends Example {
|
||||
static final String FS = File.separator;
|
||||
static final String ORIGINAL = "test" + FS + "mocks" + FS + "issue10_base.txt";
|
||||
static final String PATCH = "test" + FS + "mocks" + FS + "issue10_patch.txt";
|
||||
|
||||
public static void main(String[] args) throws PatchFailedException {
|
||||
List<String> original = fileToLines(ORIGINAL);
|
||||
List<String> patched = fileToLines(PATCH);
|
||||
|
||||
// At first, parse the unified diff file and get the patch
|
||||
Patch<String> patch = DiffUtils.parseUnifiedDiff(patched);
|
||||
|
||||
// Then apply the computed patch to the given text
|
||||
List<String> result = DiffUtils.patch(original, patch);
|
||||
System.out.println(result);
|
||||
/// Or we can call patch.applyTo(original). There is no difference.
|
||||
}
|
||||
}
|
||||
package diffutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import difflib.DiffUtils;
|
||||
import difflib.Patch;
|
||||
import difflib.PatchFailedException;
|
||||
|
||||
public class ApplyPatch extends Example {
|
||||
static final String FS = File.separator;
|
||||
static final String ORIGINAL = "test" + FS + "mocks" + FS + "issue10_base.txt";
|
||||
static final String PATCH = "test" + FS + "mocks" + FS + "issue10_patch.txt";
|
||||
|
||||
public static void main(String[] args) throws PatchFailedException {
|
||||
List<String> original = fileToLines(ORIGINAL);
|
||||
List<String> patched = fileToLines(PATCH);
|
||||
|
||||
// At first, parse the unified diff file and get the patch
|
||||
Patch<String> patch = DiffUtils.parseUnifiedDiff(patched);
|
||||
|
||||
// Then apply the computed patch to the given text
|
||||
List<String> result = DiffUtils.patch(original, patch);
|
||||
System.out.println(result);
|
||||
/// Or we can call patch.applyTo(original). There is no difference.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
package diffutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import difflib.Delta;
|
||||
import difflib.DiffUtils;
|
||||
import difflib.Patch;
|
||||
|
||||
public class ComputeDifference extends Example {
|
||||
static final String FS = File.separator;
|
||||
static final String ORIGINAL = "test" + FS + "mocks" + FS + "original.txt";
|
||||
static final String REVISED = "test" + FS + "mocks" + FS + "revised.txt";
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<String> original = fileToLines(ORIGINAL);
|
||||
List<String> revised = fileToLines(REVISED);
|
||||
|
||||
// Compute diff. Get the Patch object. Patch is the container for computed deltas.
|
||||
Patch<String> patch = DiffUtils.diff(original, revised);
|
||||
|
||||
for (Delta<String> delta: patch.getDeltas()) {
|
||||
System.out.println(delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
package diffutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import difflib.Delta;
|
||||
import difflib.DiffUtils;
|
||||
import difflib.Patch;
|
||||
|
||||
public class ComputeDifference extends Example {
|
||||
static final String FS = File.separator;
|
||||
static final String ORIGINAL = "test" + FS + "mocks" + FS + "original.txt";
|
||||
static final String REVISED = "test" + FS + "mocks" + FS + "revised.txt";
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<String> original = fileToLines(ORIGINAL);
|
||||
List<String> revised = fileToLines(REVISED);
|
||||
|
||||
// Compute diff. Get the Patch object. Patch is the container for computed deltas.
|
||||
Patch<String> patch = DiffUtils.diff(original, revised);
|
||||
|
||||
for (Delta<String> delta: patch.getDeltas()) {
|
||||
System.out.println(delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +1,61 @@
|
||||
package diffutils;
|
||||
|
||||
import difflib.*;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class DiffTest extends TestCase {
|
||||
|
||||
public void testDiff_Insert() {
|
||||
final Patch<String> patch = DiffUtils.diff(Arrays.asList("hhh"), Arrays.asList("hhh", "jjj", "kkk"));
|
||||
assertNotNull(patch);
|
||||
assertEquals(1, patch.getDeltas().size());
|
||||
final Delta<String> delta = patch.getDeltas().get(0);
|
||||
assertEquals(InsertDelta.class, delta.getClass());
|
||||
List<String> emptyList = Collections.emptyList();
|
||||
assertEquals(new Chunk<String>(1, emptyList), delta.getOriginal());
|
||||
assertEquals(new Chunk<String>(1, Arrays.asList("jjj", "kkk")), delta.getRevised());
|
||||
}
|
||||
|
||||
public void testDiff_Delete() {
|
||||
final Patch<String> patch = DiffUtils.diff(Arrays.asList("ddd", "fff", "ggg"), Arrays.asList("ggg"));
|
||||
assertNotNull(patch);
|
||||
assertEquals(1, patch.getDeltas().size());
|
||||
final Delta<String> delta = patch.getDeltas().get(0);
|
||||
assertEquals(DeleteDelta.class, delta.getClass());
|
||||
assertEquals(new Chunk<String>(0, Arrays.asList("ddd", "fff")), delta.getOriginal());
|
||||
List<String> emptyList = Collections.emptyList();
|
||||
assertEquals(new Chunk<String>(0, emptyList), delta.getRevised());
|
||||
}
|
||||
|
||||
public void testDiff_Change() {
|
||||
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc");
|
||||
final List<String> changeTest_to = Arrays.asList("aaa", "zzz", "ccc");
|
||||
|
||||
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
|
||||
assertNotNull(patch);
|
||||
assertEquals(1, patch.getDeltas().size());
|
||||
final Delta<String> delta = patch.getDeltas().get(0);
|
||||
assertEquals(ChangeDelta.class, delta.getClass());
|
||||
assertEquals(new Chunk<String>(1, Arrays.asList("bbb")), delta.getOriginal());
|
||||
assertEquals(new Chunk<String>(1, Arrays.asList("zzz")), delta.getRevised());
|
||||
}
|
||||
|
||||
public void testDiff_EmptyList() {
|
||||
final Patch<String> patch = DiffUtils.diff(new ArrayList<String>(), new ArrayList<String>());
|
||||
assertNotNull(patch);
|
||||
assertEquals(0, patch.getDeltas().size());
|
||||
}
|
||||
|
||||
public void testDiff_EmptyListWithNonEmpty() {
|
||||
final Patch<String> patch = DiffUtils.diff(new ArrayList<String>(), Arrays.asList("aaa"));
|
||||
assertNotNull(patch);
|
||||
assertEquals(1, patch.getDeltas().size());
|
||||
final Delta<String> delta = patch.getDeltas().get(0);
|
||||
assertEquals(InsertDelta.class, delta.getClass());
|
||||
}
|
||||
}
|
||||
package diffutils;
|
||||
|
||||
import difflib.*;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class DiffTest extends TestCase {
|
||||
|
||||
public void testDiff_Insert() {
|
||||
final Patch<String> patch = DiffUtils.diff(Arrays.asList("hhh"), Arrays.asList("hhh", "jjj", "kkk"));
|
||||
assertNotNull(patch);
|
||||
assertEquals(1, patch.getDeltas().size());
|
||||
final Delta<String> delta = patch.getDeltas().get(0);
|
||||
assertEquals(InsertDelta.class, delta.getClass());
|
||||
List<String> emptyList = Collections.emptyList();
|
||||
assertEquals(new Chunk<String>(1, emptyList), delta.getOriginal());
|
||||
assertEquals(new Chunk<String>(1, Arrays.asList("jjj", "kkk")), delta.getRevised());
|
||||
}
|
||||
|
||||
public void testDiff_Delete() {
|
||||
final Patch<String> patch = DiffUtils.diff(Arrays.asList("ddd", "fff", "ggg"), Arrays.asList("ggg"));
|
||||
assertNotNull(patch);
|
||||
assertEquals(1, patch.getDeltas().size());
|
||||
final Delta<String> delta = patch.getDeltas().get(0);
|
||||
assertEquals(DeleteDelta.class, delta.getClass());
|
||||
assertEquals(new Chunk<String>(0, Arrays.asList("ddd", "fff")), delta.getOriginal());
|
||||
List<String> emptyList = Collections.emptyList();
|
||||
assertEquals(new Chunk<String>(0, emptyList), delta.getRevised());
|
||||
}
|
||||
|
||||
public void testDiff_Change() {
|
||||
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc");
|
||||
final List<String> changeTest_to = Arrays.asList("aaa", "zzz", "ccc");
|
||||
|
||||
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
|
||||
assertNotNull(patch);
|
||||
assertEquals(1, patch.getDeltas().size());
|
||||
final Delta<String> delta = patch.getDeltas().get(0);
|
||||
assertEquals(ChangeDelta.class, delta.getClass());
|
||||
assertEquals(new Chunk<String>(1, Arrays.asList("bbb")), delta.getOriginal());
|
||||
assertEquals(new Chunk<String>(1, Arrays.asList("zzz")), delta.getRevised());
|
||||
}
|
||||
|
||||
public void testDiff_EmptyList() {
|
||||
final Patch<String> patch = DiffUtils.diff(new ArrayList<String>(), new ArrayList<String>());
|
||||
assertNotNull(patch);
|
||||
assertEquals(0, patch.getDeltas().size());
|
||||
}
|
||||
|
||||
public void testDiff_EmptyListWithNonEmpty() {
|
||||
final Patch<String> patch = DiffUtils.diff(new ArrayList<String>(), Arrays.asList("aaa"));
|
||||
assertNotNull(patch);
|
||||
assertEquals(1, patch.getDeltas().size());
|
||||
final Delta<String> delta = patch.getDeltas().get(0);
|
||||
assertEquals(InsertDelta.class, delta.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
package diffutils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Example {
|
||||
public static List<String> fileToLines(String filename) {
|
||||
List<String> lines = new LinkedList<String>();
|
||||
String line = "";
|
||||
try {
|
||||
BufferedReader in = new BufferedReader(new FileReader(filename));
|
||||
while ((line = in.readLine()) != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
package diffutils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Example {
|
||||
public static List<String> fileToLines(String filename) {
|
||||
List<String> lines = new LinkedList<String>();
|
||||
String line = "";
|
||||
try {
|
||||
BufferedReader in = new BufferedReader(new FileReader(filename));
|
||||
while ((line = in.readLine()) != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
|
||||
+129
-129
@@ -1,129 +1,129 @@
|
||||
package diffutils;
|
||||
|
||||
import difflib.DiffUtils;
|
||||
import difflib.Patch;
|
||||
import difflib.PatchFailedException;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class GenerateUnifiedDiffTest extends TestCase {
|
||||
private static final String FS = File.separator;
|
||||
private static final String MOCK_FOLDER = "test" + FS + "mocks" + FS;
|
||||
|
||||
public List<String> fileToLines(String filename) {
|
||||
List<String> lines = new LinkedList<String>();
|
||||
String line = "";
|
||||
try {
|
||||
BufferedReader in = new BufferedReader(new FileReader(filename));
|
||||
while ((line = in.readLine()) != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
public void testGenerateUnified() {
|
||||
List<String> origLines = fileToLines(MOCK_FOLDER + "original.txt");
|
||||
List<String> revLines = fileToLines(MOCK_FOLDER + "revised.txt");
|
||||
|
||||
verify(origLines, revLines, "original.txt", "revised.txt");
|
||||
}
|
||||
|
||||
public void testGenerateUnifiedWithOneDelta() {
|
||||
List<String> origLines = fileToLines(MOCK_FOLDER + "one_delta_test_original.txt");
|
||||
List<String> revLines = fileToLines(MOCK_FOLDER + "one_delta_test_revised.txt");
|
||||
|
||||
verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt");
|
||||
}
|
||||
|
||||
public void testGenerateUnifiedDiffWithoutAnyDeltas() {
|
||||
List<String> test = Arrays.asList("abc");
|
||||
Patch<String> patch = DiffUtils.diff(test, test);
|
||||
DiffUtils.generateUnifiedDiff("abc", "abc", test, patch, 0);
|
||||
}
|
||||
|
||||
public void testDiff_Issue10() {
|
||||
final List<String> baseLines = fileToLines(MOCK_FOLDER + "issue10_base.txt");
|
||||
final List<String> patchLines = fileToLines(MOCK_FOLDER + "issue10_patch.txt");
|
||||
final Patch<String> p = DiffUtils.parseUnifiedDiff(patchLines);
|
||||
try {
|
||||
DiffUtils.patch(baseLines, p);
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Issue 12
|
||||
*/
|
||||
public void testPatchWithNoDeltas() {
|
||||
final List<String> lines1 = fileToLines(MOCK_FOLDER + "issue11_1.txt");
|
||||
final List<String> lines2 = fileToLines(MOCK_FOLDER + "issue11_2.txt");
|
||||
verify(lines1, lines2, "issue11_1.txt", "issue11_2.txt");
|
||||
}
|
||||
|
||||
public void testDiff5() {
|
||||
final List<String> lines1 = fileToLines(MOCK_FOLDER + "5A.txt");
|
||||
final List<String> lines2 = fileToLines(MOCK_FOLDER + "5B.txt");
|
||||
verify(lines1, lines2, "5A.txt", "5B.txt");
|
||||
}
|
||||
|
||||
/**
|
||||
* Issue 19
|
||||
*/
|
||||
public void testDiffWithHeaderLineInText() {
|
||||
List<String> original = new ArrayList<String>();
|
||||
List<String> revised = new ArrayList<String>();
|
||||
|
||||
original.add("test line1");
|
||||
original.add("test line2");
|
||||
original.add("test line 4");
|
||||
original.add("test line 5");
|
||||
|
||||
revised.add("test line1");
|
||||
revised.add("test line2");
|
||||
revised.add("@@ -2,6 +2,7 @@");
|
||||
revised.add("test line 4");
|
||||
revised.add("test line 5");
|
||||
|
||||
Patch<String> patch = DiffUtils.diff(original, revised);
|
||||
List<String> udiff = DiffUtils.generateUnifiedDiff("original", "revised",
|
||||
original, patch, 10);
|
||||
DiffUtils.parseUnifiedDiff(udiff);
|
||||
}
|
||||
|
||||
private void verify(List<String> origLines, List<String> revLines,
|
||||
String originalFile, String revisedFile) {
|
||||
Patch<String> patch = DiffUtils.diff(origLines, revLines);
|
||||
List<String> unifiedDiff = DiffUtils.generateUnifiedDiff(originalFile, revisedFile,
|
||||
origLines, patch, 10);
|
||||
|
||||
Patch<String> fromUnifiedPatch = DiffUtils.parseUnifiedDiff(unifiedDiff);
|
||||
List<String> patchedLines;
|
||||
try {
|
||||
patchedLines = (List<String>) fromUnifiedPatch.applyTo(origLines);
|
||||
assertTrue(revLines.size() == patchedLines.size());
|
||||
for (int i = 0; i < revLines.size(); i++) {
|
||||
String l1 = revLines.get(i);
|
||||
String l2 = patchedLines.get(i);
|
||||
if (!l1.equals(l2)) {
|
||||
fail("Line " + (i + 1) + " of the patched file did not match the revised original");
|
||||
}
|
||||
}
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
package diffutils;
|
||||
|
||||
import difflib.DiffUtils;
|
||||
import difflib.Patch;
|
||||
import difflib.PatchFailedException;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class GenerateUnifiedDiffTest extends TestCase {
|
||||
private static final String FS = File.separator;
|
||||
private static final String MOCK_FOLDER = "test" + FS + "mocks" + FS;
|
||||
|
||||
public List<String> fileToLines(String filename) {
|
||||
List<String> lines = new LinkedList<String>();
|
||||
String line = "";
|
||||
try {
|
||||
BufferedReader in = new BufferedReader(new FileReader(filename));
|
||||
while ((line = in.readLine()) != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.getMessage());
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
public void testGenerateUnified() {
|
||||
List<String> origLines = fileToLines(MOCK_FOLDER + "original.txt");
|
||||
List<String> revLines = fileToLines(MOCK_FOLDER + "revised.txt");
|
||||
|
||||
verify(origLines, revLines, "original.txt", "revised.txt");
|
||||
}
|
||||
|
||||
public void testGenerateUnifiedWithOneDelta() {
|
||||
List<String> origLines = fileToLines(MOCK_FOLDER + "one_delta_test_original.txt");
|
||||
List<String> revLines = fileToLines(MOCK_FOLDER + "one_delta_test_revised.txt");
|
||||
|
||||
verify(origLines, revLines, "one_delta_test_original.txt", "one_delta_test_revised.txt");
|
||||
}
|
||||
|
||||
public void testGenerateUnifiedDiffWithoutAnyDeltas() {
|
||||
List<String> test = Arrays.asList("abc");
|
||||
Patch<String> patch = DiffUtils.diff(test, test);
|
||||
DiffUtils.generateUnifiedDiff("abc", "abc", test, patch, 0);
|
||||
}
|
||||
|
||||
public void testDiff_Issue10() {
|
||||
final List<String> baseLines = fileToLines(MOCK_FOLDER + "issue10_base.txt");
|
||||
final List<String> patchLines = fileToLines(MOCK_FOLDER + "issue10_patch.txt");
|
||||
final Patch<String> p = DiffUtils.parseUnifiedDiff(patchLines);
|
||||
try {
|
||||
DiffUtils.patch(baseLines, p);
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Issue 12
|
||||
*/
|
||||
public void testPatchWithNoDeltas() {
|
||||
final List<String> lines1 = fileToLines(MOCK_FOLDER + "issue11_1.txt");
|
||||
final List<String> lines2 = fileToLines(MOCK_FOLDER + "issue11_2.txt");
|
||||
verify(lines1, lines2, "issue11_1.txt", "issue11_2.txt");
|
||||
}
|
||||
|
||||
public void testDiff5() {
|
||||
final List<String> lines1 = fileToLines(MOCK_FOLDER + "5A.txt");
|
||||
final List<String> lines2 = fileToLines(MOCK_FOLDER + "5B.txt");
|
||||
verify(lines1, lines2, "5A.txt", "5B.txt");
|
||||
}
|
||||
|
||||
/**
|
||||
* Issue 19
|
||||
*/
|
||||
public void testDiffWithHeaderLineInText() {
|
||||
List<String> original = new ArrayList<String>();
|
||||
List<String> revised = new ArrayList<String>();
|
||||
|
||||
original.add("test line1");
|
||||
original.add("test line2");
|
||||
original.add("test line 4");
|
||||
original.add("test line 5");
|
||||
|
||||
revised.add("test line1");
|
||||
revised.add("test line2");
|
||||
revised.add("@@ -2,6 +2,7 @@");
|
||||
revised.add("test line 4");
|
||||
revised.add("test line 5");
|
||||
|
||||
Patch<String> patch = DiffUtils.diff(original, revised);
|
||||
List<String> udiff = DiffUtils.generateUnifiedDiff("original", "revised",
|
||||
original, patch, 10);
|
||||
DiffUtils.parseUnifiedDiff(udiff);
|
||||
}
|
||||
|
||||
private void verify(List<String> origLines, List<String> revLines,
|
||||
String originalFile, String revisedFile) {
|
||||
Patch<String> patch = DiffUtils.diff(origLines, revLines);
|
||||
List<String> unifiedDiff = DiffUtils.generateUnifiedDiff(originalFile, revisedFile,
|
||||
origLines, patch, 10);
|
||||
|
||||
Patch<String> fromUnifiedPatch = DiffUtils.parseUnifiedDiff(unifiedDiff);
|
||||
List<String> patchedLines;
|
||||
try {
|
||||
patchedLines = (List<String>) fromUnifiedPatch.applyTo(origLines);
|
||||
assertTrue(revLines.size() == patchedLines.size());
|
||||
for (int i = 0; i < revLines.size(); i++) {
|
||||
String l1 = revLines.get(i);
|
||||
String l2 = patchedLines.get(i);
|
||||
if (!l1.equals(l2)) {
|
||||
fail("Line " + (i + 1) + " of the patched file did not match the revised original");
|
||||
}
|
||||
}
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,64 @@
|
||||
package diffutils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import difflib.DiffUtils;
|
||||
import difflib.Patch;
|
||||
import difflib.PatchFailedException;
|
||||
|
||||
public class PatchTest extends TestCase {
|
||||
|
||||
public void testPatch_Insert() {
|
||||
final List<String> insertTest_from = Arrays.asList("hhh");
|
||||
final List<String> insertTest_to = Arrays.asList("hhh", "jjj", "kkk", "lll");
|
||||
|
||||
final Patch<String> patch = DiffUtils.diff(insertTest_from, insertTest_to);
|
||||
try {
|
||||
assertEquals(insertTest_to, DiffUtils.patch(insertTest_from, patch));
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testPatch_Delete() {
|
||||
final List<String> deleteTest_from = Arrays.asList("ddd", "fff", "ggg", "hhh");
|
||||
final List<String> deleteTest_to = Arrays.asList("ggg");
|
||||
|
||||
final Patch<String> patch = DiffUtils.diff(deleteTest_from, deleteTest_to);
|
||||
try {
|
||||
assertEquals(deleteTest_to, DiffUtils.patch(deleteTest_from, patch));
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testPatch_Change() {
|
||||
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
|
||||
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
|
||||
|
||||
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
|
||||
try {
|
||||
assertEquals(changeTest_to, DiffUtils.patch(changeTest_from, patch));
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testPatch_EntirelyDifferent() {
|
||||
final List<String> changeTest_from = new ArrayList<String>();
|
||||
changeTest_from.add("aaa");
|
||||
changeTest_from.add("bbb");
|
||||
final List<String> changeTest_to = Arrays.asList("ccc", "ddd");
|
||||
|
||||
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
|
||||
try {
|
||||
patch.applyToInPlace(changeTest_from);
|
||||
assertEquals(changeTest_to, changeTest_from);
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
package diffutils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import difflib.DiffUtils;
|
||||
import difflib.Patch;
|
||||
import difflib.PatchFailedException;
|
||||
|
||||
public class PatchTest extends TestCase {
|
||||
|
||||
public void testPatch_Insert() {
|
||||
final List<String> insertTest_from = Arrays.asList("hhh");
|
||||
final List<String> insertTest_to = Arrays.asList("hhh", "jjj", "kkk", "lll");
|
||||
|
||||
final Patch<String> patch = DiffUtils.diff(insertTest_from, insertTest_to);
|
||||
try {
|
||||
assertEquals(insertTest_to, DiffUtils.patch(insertTest_from, patch));
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testPatch_Delete() {
|
||||
final List<String> deleteTest_from = Arrays.asList("ddd", "fff", "ggg", "hhh");
|
||||
final List<String> deleteTest_to = Arrays.asList("ggg");
|
||||
|
||||
final Patch<String> patch = DiffUtils.diff(deleteTest_from, deleteTest_to);
|
||||
try {
|
||||
assertEquals(deleteTest_to, DiffUtils.patch(deleteTest_from, patch));
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testPatch_Change() {
|
||||
final List<String> changeTest_from = Arrays.asList("aaa", "bbb", "ccc", "ddd");
|
||||
final List<String> changeTest_to = Arrays.asList("aaa", "bxb", "cxc", "ddd");
|
||||
|
||||
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
|
||||
try {
|
||||
assertEquals(changeTest_to, DiffUtils.patch(changeTest_from, patch));
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testPatch_EntirelyDifferent() {
|
||||
final List<String> changeTest_from = new ArrayList<String>();
|
||||
changeTest_from.add("aaa");
|
||||
changeTest_from.add("bbb");
|
||||
final List<String> changeTest_to = Arrays.asList("ccc", "ddd");
|
||||
|
||||
final Patch<String> patch = DiffUtils.diff(changeTest_from, changeTest_to);
|
||||
try {
|
||||
patch.applyToInPlace(changeTest_from);
|
||||
assertEquals(changeTest_to, changeTest_from);
|
||||
} catch (PatchFailedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,372 +1,372 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright (c) 2006 Johannes E. Schindelin
|
||||
#
|
||||
|
||||
test_description='Test special whitespace in diff engine.
|
||||
|
||||
'
|
||||
. ./test-lib.sh
|
||||
. ../diff-lib.sh
|
||||
|
||||
# Ray Lehtiniemi's example
|
||||
|
||||
cat << EOF > x
|
||||
do {
|
||||
nothing;
|
||||
} while (0);
|
||||
EOF
|
||||
|
||||
git update-index --add x
|
||||
|
||||
cat << EOF > x
|
||||
do
|
||||
{
|
||||
nothing;
|
||||
}
|
||||
while (0);
|
||||
EOF
|
||||
|
||||
cat << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index adf3937..6edc172 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,3 +1,5 @@
|
||||
-do {
|
||||
+do
|
||||
+{
|
||||
nothing;
|
||||
-} while (0);
|
||||
+}
|
||||
+while (0);
|
||||
EOF
|
||||
|
||||
git diff > out
|
||||
test_expect_success "Ray's example without options" 'test_cmp expect out'
|
||||
|
||||
git diff -w > out
|
||||
test_expect_success "Ray's example with -w" 'test_cmp expect out'
|
||||
|
||||
git diff -b > out
|
||||
test_expect_success "Ray's example with -b" 'test_cmp expect out'
|
||||
|
||||
tr 'Q' '\015' << EOF > x
|
||||
whitespace at beginning
|
||||
whitespace change
|
||||
whitespace in the middle
|
||||
whitespace at end
|
||||
unchanged line
|
||||
CR at endQ
|
||||
EOF
|
||||
|
||||
git update-index x
|
||||
|
||||
tr '_' ' ' << EOF > x
|
||||
whitespace at beginning
|
||||
whitespace change
|
||||
white space in the middle
|
||||
whitespace at end__
|
||||
unchanged line
|
||||
CR at end
|
||||
EOF
|
||||
|
||||
tr 'Q_' '\015 ' << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,6 +1,6 @@
|
||||
-whitespace at beginning
|
||||
-whitespace change
|
||||
-whitespace in the middle
|
||||
-whitespace at end
|
||||
+ whitespace at beginning
|
||||
+whitespace change
|
||||
+white space in the middle
|
||||
+whitespace at end__
|
||||
unchanged line
|
||||
-CR at endQ
|
||||
+CR at end
|
||||
EOF
|
||||
git diff > out
|
||||
test_expect_success 'another test, without options' 'test_cmp expect out'
|
||||
|
||||
cat << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
EOF
|
||||
git diff -w > out
|
||||
test_expect_success 'another test, with -w' 'test_cmp expect out'
|
||||
|
||||
tr 'Q' '\015' << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,6 +1,6 @@
|
||||
-whitespace at beginning
|
||||
+ whitespace at beginning
|
||||
whitespace change
|
||||
-whitespace in the middle
|
||||
+white space in the middle
|
||||
whitespace at end
|
||||
unchanged line
|
||||
CR at endQ
|
||||
EOF
|
||||
git diff -b > out
|
||||
test_expect_success 'another test, with -b' 'test_cmp expect out'
|
||||
|
||||
test_expect_success 'check mixed spaces and tabs in indent' '
|
||||
|
||||
# This is indented with SP HT SP.
|
||||
echo " foo();" > x &&
|
||||
git diff --check | grep "space before tab in indent"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check mixed tabs and spaces in indent' '
|
||||
|
||||
# This is indented with HT SP HT.
|
||||
echo " foo();" > x &&
|
||||
git diff --check | grep "space before tab in indent"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors' '
|
||||
|
||||
git commit -m "snapshot" &&
|
||||
echo "foo();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success '--check and --exit-code are not exclusive' '
|
||||
|
||||
git checkout x &&
|
||||
git diff --check --exit-code
|
||||
|
||||
'
|
||||
|
||||
test_expect_success '--check and --quiet are not exclusive' '
|
||||
|
||||
git diff --check --quiet
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with no whitespace errors' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with trailing whitespace' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with space before tab in indent' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors (diff-index)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace (diff-index)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent (diff-index)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with no whitespace errors (diff-index)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with trailing whitespace (diff-index)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with space before tab in indent (diff-index)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors (diff-tree)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git commit -m "new commit" x &&
|
||||
git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace (diff-tree)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git commit -m "another commit" x &&
|
||||
test_must_fail git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent (diff-tree)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git commit -m "yet another" x &&
|
||||
test_must_fail git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check trailing whitespace (trailing-space: off)' '
|
||||
|
||||
git config core.whitespace "-trailing-space" &&
|
||||
echo "foo (); " > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check trailing whitespace (trailing-space: on)' '
|
||||
|
||||
git config core.whitespace "trailing-space" &&
|
||||
echo "foo (); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check space before tab in indent (space-before-tab: off)' '
|
||||
|
||||
# indent contains space followed by HT
|
||||
git config core.whitespace "-space-before-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check space before tab in indent (space-before-tab: on)' '
|
||||
|
||||
# indent contains space followed by HT
|
||||
git config core.whitespace "space-before-tab" &&
|
||||
echo " foo (); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
|
||||
|
||||
git config core.whitespace "-indent-with-non-tab"
|
||||
echo " foo ();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
|
||||
|
||||
git config core.whitespace "indent-with-non-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
|
||||
|
||||
git config core.whitespace "indent-with-non-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'line numbers in --check output are correct' '
|
||||
|
||||
echo "" > x &&
|
||||
echo "foo(); " >> x &&
|
||||
git diff --check | grep "x:2:"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'checkdiff detects trailing blank lines' '
|
||||
echo "foo();" >x &&
|
||||
echo "" >>x &&
|
||||
git diff --check | grep "ends with blank"
|
||||
'
|
||||
|
||||
test_expect_success 'checkdiff allows new blank lines' '
|
||||
git checkout x &&
|
||||
mv x y &&
|
||||
(
|
||||
echo "/* This is new */" &&
|
||||
echo "" &&
|
||||
cat y
|
||||
) >x &&
|
||||
git diff --check
|
||||
'
|
||||
|
||||
test_expect_success 'combined diff with autocrlf conversion' '
|
||||
|
||||
git reset --hard &&
|
||||
echo >x hello &&
|
||||
git commit -m "one side" x &&
|
||||
git checkout HEAD^ &&
|
||||
echo >x goodbye &&
|
||||
git commit -m "the other side" x &&
|
||||
git config core.autocrlf true &&
|
||||
test_must_fail git merge master &&
|
||||
|
||||
git diff | sed -e "1,/^@@@/d" >actual &&
|
||||
! grep "^-" actual
|
||||
|
||||
'
|
||||
|
||||
test_done
|
||||
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright (c) 2006 Johannes E. Schindelin
|
||||
#
|
||||
|
||||
test_description='Test special whitespace in diff engine.
|
||||
|
||||
'
|
||||
. ./test-lib.sh
|
||||
. ../diff-lib.sh
|
||||
|
||||
# Ray Lehtiniemi's example
|
||||
|
||||
cat << EOF > x
|
||||
do {
|
||||
nothing;
|
||||
} while (0);
|
||||
EOF
|
||||
|
||||
git update-index --add x
|
||||
|
||||
cat << EOF > x
|
||||
do
|
||||
{
|
||||
nothing;
|
||||
}
|
||||
while (0);
|
||||
EOF
|
||||
|
||||
cat << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index adf3937..6edc172 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,3 +1,5 @@
|
||||
-do {
|
||||
+do
|
||||
+{
|
||||
nothing;
|
||||
-} while (0);
|
||||
+}
|
||||
+while (0);
|
||||
EOF
|
||||
|
||||
git diff > out
|
||||
test_expect_success "Ray's example without options" 'test_cmp expect out'
|
||||
|
||||
git diff -w > out
|
||||
test_expect_success "Ray's example with -w" 'test_cmp expect out'
|
||||
|
||||
git diff -b > out
|
||||
test_expect_success "Ray's example with -b" 'test_cmp expect out'
|
||||
|
||||
tr 'Q' '\015' << EOF > x
|
||||
whitespace at beginning
|
||||
whitespace change
|
||||
whitespace in the middle
|
||||
whitespace at end
|
||||
unchanged line
|
||||
CR at endQ
|
||||
EOF
|
||||
|
||||
git update-index x
|
||||
|
||||
tr '_' ' ' << EOF > x
|
||||
whitespace at beginning
|
||||
whitespace change
|
||||
white space in the middle
|
||||
whitespace at end__
|
||||
unchanged line
|
||||
CR at end
|
||||
EOF
|
||||
|
||||
tr 'Q_' '\015 ' << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,6 +1,6 @@
|
||||
-whitespace at beginning
|
||||
-whitespace change
|
||||
-whitespace in the middle
|
||||
-whitespace at end
|
||||
+ whitespace at beginning
|
||||
+whitespace change
|
||||
+white space in the middle
|
||||
+whitespace at end__
|
||||
unchanged line
|
||||
-CR at endQ
|
||||
+CR at end
|
||||
EOF
|
||||
git diff > out
|
||||
test_expect_success 'another test, without options' 'test_cmp expect out'
|
||||
|
||||
cat << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
EOF
|
||||
git diff -w > out
|
||||
test_expect_success 'another test, with -w' 'test_cmp expect out'
|
||||
|
||||
tr 'Q' '\015' << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,6 +1,6 @@
|
||||
-whitespace at beginning
|
||||
+ whitespace at beginning
|
||||
whitespace change
|
||||
-whitespace in the middle
|
||||
+white space in the middle
|
||||
whitespace at end
|
||||
unchanged line
|
||||
CR at endQ
|
||||
EOF
|
||||
git diff -b > out
|
||||
test_expect_success 'another test, with -b' 'test_cmp expect out'
|
||||
|
||||
test_expect_success 'check mixed spaces and tabs in indent' '
|
||||
|
||||
# This is indented with SP HT SP.
|
||||
echo " foo();" > x &&
|
||||
git diff --check | grep "space before tab in indent"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check mixed tabs and spaces in indent' '
|
||||
|
||||
# This is indented with HT SP HT.
|
||||
echo " foo();" > x &&
|
||||
git diff --check | grep "space before tab in indent"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors' '
|
||||
|
||||
git commit -m "snapshot" &&
|
||||
echo "foo();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success '--check and --exit-code are not exclusive' '
|
||||
|
||||
git checkout x &&
|
||||
git diff --check --exit-code
|
||||
|
||||
'
|
||||
|
||||
test_expect_success '--check and --quiet are not exclusive' '
|
||||
|
||||
git diff --check --quiet
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with no whitespace errors' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with trailing whitespace' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with space before tab in indent' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors (diff-index)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace (diff-index)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent (diff-index)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with no whitespace errors (diff-index)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with trailing whitespace (diff-index)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with space before tab in indent (diff-index)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors (diff-tree)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git commit -m "new commit" x &&
|
||||
git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace (diff-tree)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git commit -m "another commit" x &&
|
||||
test_must_fail git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent (diff-tree)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git commit -m "yet another" x &&
|
||||
test_must_fail git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check trailing whitespace (trailing-space: off)' '
|
||||
|
||||
git config core.whitespace "-trailing-space" &&
|
||||
echo "foo (); " > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check trailing whitespace (trailing-space: on)' '
|
||||
|
||||
git config core.whitespace "trailing-space" &&
|
||||
echo "foo (); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check space before tab in indent (space-before-tab: off)' '
|
||||
|
||||
# indent contains space followed by HT
|
||||
git config core.whitespace "-space-before-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check space before tab in indent (space-before-tab: on)' '
|
||||
|
||||
# indent contains space followed by HT
|
||||
git config core.whitespace "space-before-tab" &&
|
||||
echo " foo (); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
|
||||
|
||||
git config core.whitespace "-indent-with-non-tab"
|
||||
echo " foo ();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
|
||||
|
||||
git config core.whitespace "indent-with-non-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
|
||||
|
||||
git config core.whitespace "indent-with-non-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'line numbers in --check output are correct' '
|
||||
|
||||
echo "" > x &&
|
||||
echo "foo(); " >> x &&
|
||||
git diff --check | grep "x:2:"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'checkdiff detects trailing blank lines' '
|
||||
echo "foo();" >x &&
|
||||
echo "" >>x &&
|
||||
git diff --check | grep "ends with blank"
|
||||
'
|
||||
|
||||
test_expect_success 'checkdiff allows new blank lines' '
|
||||
git checkout x &&
|
||||
mv x y &&
|
||||
(
|
||||
echo "/* This is new */" &&
|
||||
echo "" &&
|
||||
cat y
|
||||
) >x &&
|
||||
git diff --check
|
||||
'
|
||||
|
||||
test_expect_success 'combined diff with autocrlf conversion' '
|
||||
|
||||
git reset --hard &&
|
||||
echo >x hello &&
|
||||
git commit -m "one side" x &&
|
||||
git checkout HEAD^ &&
|
||||
echo >x goodbye &&
|
||||
git commit -m "the other side" x &&
|
||||
git config core.autocrlf true &&
|
||||
test_must_fail git merge master &&
|
||||
|
||||
git diff | sed -e "1,/^@@@/d" >actual &&
|
||||
! grep "^-" actual
|
||||
|
||||
'
|
||||
|
||||
test_done
|
||||
|
||||
|
||||
@@ -1,381 +1,381 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright (c) 2006 Johannes E. Schindelin
|
||||
#
|
||||
|
||||
test_description='Test special whitespace in diff engine.
|
||||
|
||||
'
|
||||
. ./test-lib.sh
|
||||
. ../diff-lib.sh
|
||||
|
||||
# Ray Lehtiniemi's example
|
||||
|
||||
cat << EOF > x
|
||||
do {
|
||||
nothing;
|
||||
} while (0);
|
||||
EOF
|
||||
|
||||
git update-index --add x
|
||||
|
||||
cat << EOF > x
|
||||
do
|
||||
{
|
||||
nothing;
|
||||
}
|
||||
while (0);
|
||||
EOF
|
||||
|
||||
cat << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index adf3937..6edc172 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,3 +1,5 @@
|
||||
-do {
|
||||
+do
|
||||
+{
|
||||
nothing;
|
||||
-} while (0);
|
||||
+}
|
||||
+while (0);
|
||||
EOF
|
||||
|
||||
git diff > out
|
||||
test_expect_success "Ray's example without options" 'test_cmp expect out'
|
||||
|
||||
git diff -w > out
|
||||
test_expect_success "Ray's example with -w" 'test_cmp expect out'
|
||||
|
||||
git diff -b > out
|
||||
test_expect_success "Ray's example with -b" 'test_cmp expect out'
|
||||
|
||||
tr 'Q' '\015' << EOF > x
|
||||
whitespace at beginning
|
||||
whitespace change
|
||||
whitespace in the middle
|
||||
whitespace at end
|
||||
unchanged line
|
||||
CR at endQ
|
||||
EOF
|
||||
|
||||
git update-index x
|
||||
|
||||
tr '_' ' ' << EOF > x
|
||||
whitespace at beginning
|
||||
whitespace change
|
||||
white space in the middle
|
||||
whitespace at end__
|
||||
unchanged line
|
||||
CR at end
|
||||
EOF
|
||||
|
||||
tr 'Q_' '\015 ' << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,6 +1,6 @@
|
||||
-whitespace at beginning
|
||||
-whitespace change
|
||||
-whitespace in the middle
|
||||
-whitespace at end
|
||||
+ whitespace at beginning
|
||||
+whitespace change
|
||||
+white space in the middle
|
||||
+whitespace at end__
|
||||
unchanged line
|
||||
-CR at endQ
|
||||
+CR at end
|
||||
EOF
|
||||
git diff > out
|
||||
test_expect_success 'another test, without options' 'test_cmp expect out'
|
||||
|
||||
cat << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
EOF
|
||||
git diff -w > out
|
||||
test_expect_success 'another test, with -w' 'test_cmp expect out'
|
||||
|
||||
tr 'Q' '\015' << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,6 +1,6 @@
|
||||
-whitespace at beginning
|
||||
+ whitespace at beginning
|
||||
whitespace change
|
||||
-whitespace in the middle
|
||||
+white space in the middle
|
||||
whitespace at end
|
||||
unchanged line
|
||||
CR at endQ
|
||||
git diff -b --ignore-space-at-eol > out
|
||||
test_expect_failure 'another test, with -b --ignore-space-at-eol' 'test_cmp expect out'
|
||||
|
||||
tr 'Q' '\015' << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
EOF
|
||||
git diff -b > out
|
||||
test_expect_success 'another test, with -b' 'test_cmp expect out'
|
||||
|
||||
test_expect_success 'check mixed spaces and tabs in indent' '
|
||||
|
||||
# This is indented with SP HT SP.
|
||||
echo " foo();" > x &&
|
||||
git diff --check | grep "space before tab in indent"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check mixed tabs and spaces in indent' '
|
||||
|
||||
# This is indented with HT SP HT.
|
||||
echo " foo();" > x &&
|
||||
git diff --check | grep "space before tab in indent"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors' '
|
||||
|
||||
git commit -m "snapshot" &&
|
||||
echo "foo();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success '--check and --exit-code are not exclusive' '
|
||||
|
||||
git checkout x &&
|
||||
git diff --check --exit-code
|
||||
|
||||
'
|
||||
|
||||
test_expect_success '--check and --quiet are not exclusive' '
|
||||
|
||||
git diff --check --quiet
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with no whitespace errors' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with trailing whitespace' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with space before tab in indent' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors (diff-index)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace (diff-index)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent (diff-index)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with no whitespace errors (diff-index)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with trailing whitespace (diff-index)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with space before tab in indent (diff-index)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors (diff-tree)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git commit -m "new commit" x &&
|
||||
git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace (diff-tree)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git commit -m "another commit" x &&
|
||||
test_must_fail git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent (diff-tree)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git commit -m "yet another" x &&
|
||||
test_must_fail git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check trailing whitespace (trailing-space: off)' '
|
||||
|
||||
git config core.whitespace "-trailing-space" &&
|
||||
echo "foo (); " > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check trailing whitespace (trailing-space: on)' '
|
||||
|
||||
git config core.whitespace "trailing-space" &&
|
||||
echo "foo (); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check space before tab in indent (space-before-tab: off)' '
|
||||
|
||||
# indent contains space followed by HT
|
||||
git config core.whitespace "-space-before-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check space before tab in indent (space-before-tab: on)' '
|
||||
|
||||
# indent contains space followed by HT
|
||||
git config core.whitespace "space-before-tab" &&
|
||||
echo " foo (); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
|
||||
|
||||
git config core.whitespace "-indent-with-non-tab"
|
||||
echo " foo ();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
|
||||
|
||||
git config core.whitespace "indent-with-non-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
|
||||
|
||||
git config core.whitespace "indent-with-non-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'line numbers in --check output are correct' '
|
||||
|
||||
echo "" > x &&
|
||||
echo "foo(); " >> x &&
|
||||
git diff --check | grep "x:2:"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'checkdiff detects trailing blank lines' '
|
||||
echo "foo();" >x &&
|
||||
echo "" >>x &&
|
||||
git diff --check | grep "ends with blank"
|
||||
'
|
||||
|
||||
test_expect_success 'checkdiff allows new blank lines' '
|
||||
git checkout x &&
|
||||
mv x y &&
|
||||
(
|
||||
echo "/* This is new */" &&
|
||||
echo "" &&
|
||||
cat y
|
||||
) >x &&
|
||||
git diff --check
|
||||
'
|
||||
|
||||
test_expect_success 'combined diff with autocrlf conversion' '
|
||||
|
||||
git reset --hard &&
|
||||
echo >x hello &&
|
||||
git commit -m "one side" x &&
|
||||
git checkout HEAD^ &&
|
||||
echo >x goodbye &&
|
||||
git commit -m "the other side" x &&
|
||||
git config core.autocrlf true &&
|
||||
test_must_fail git merge master &&
|
||||
|
||||
git diff | sed -e "1,/^@@@/d" >actual &&
|
||||
! grep "^-" actual
|
||||
|
||||
'
|
||||
|
||||
test_done
|
||||
|
||||
|
||||
#!/bin/bash
|
||||
#
|
||||
# Copyright (c) 2006 Johannes E. Schindelin
|
||||
#
|
||||
|
||||
test_description='Test special whitespace in diff engine.
|
||||
|
||||
'
|
||||
. ./test-lib.sh
|
||||
. ../diff-lib.sh
|
||||
|
||||
# Ray Lehtiniemi's example
|
||||
|
||||
cat << EOF > x
|
||||
do {
|
||||
nothing;
|
||||
} while (0);
|
||||
EOF
|
||||
|
||||
git update-index --add x
|
||||
|
||||
cat << EOF > x
|
||||
do
|
||||
{
|
||||
nothing;
|
||||
}
|
||||
while (0);
|
||||
EOF
|
||||
|
||||
cat << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index adf3937..6edc172 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,3 +1,5 @@
|
||||
-do {
|
||||
+do
|
||||
+{
|
||||
nothing;
|
||||
-} while (0);
|
||||
+}
|
||||
+while (0);
|
||||
EOF
|
||||
|
||||
git diff > out
|
||||
test_expect_success "Ray's example without options" 'test_cmp expect out'
|
||||
|
||||
git diff -w > out
|
||||
test_expect_success "Ray's example with -w" 'test_cmp expect out'
|
||||
|
||||
git diff -b > out
|
||||
test_expect_success "Ray's example with -b" 'test_cmp expect out'
|
||||
|
||||
tr 'Q' '\015' << EOF > x
|
||||
whitespace at beginning
|
||||
whitespace change
|
||||
whitespace in the middle
|
||||
whitespace at end
|
||||
unchanged line
|
||||
CR at endQ
|
||||
EOF
|
||||
|
||||
git update-index x
|
||||
|
||||
tr '_' ' ' << EOF > x
|
||||
whitespace at beginning
|
||||
whitespace change
|
||||
white space in the middle
|
||||
whitespace at end__
|
||||
unchanged line
|
||||
CR at end
|
||||
EOF
|
||||
|
||||
tr 'Q_' '\015 ' << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,6 +1,6 @@
|
||||
-whitespace at beginning
|
||||
-whitespace change
|
||||
-whitespace in the middle
|
||||
-whitespace at end
|
||||
+ whitespace at beginning
|
||||
+whitespace change
|
||||
+white space in the middle
|
||||
+whitespace at end__
|
||||
unchanged line
|
||||
-CR at endQ
|
||||
+CR at end
|
||||
EOF
|
||||
git diff > out
|
||||
test_expect_success 'another test, without options' 'test_cmp expect out'
|
||||
|
||||
cat << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
EOF
|
||||
git diff -w > out
|
||||
test_expect_success 'another test, with -w' 'test_cmp expect out'
|
||||
|
||||
tr 'Q' '\015' << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
@@ -1,6 +1,6 @@
|
||||
-whitespace at beginning
|
||||
+ whitespace at beginning
|
||||
whitespace change
|
||||
-whitespace in the middle
|
||||
+white space in the middle
|
||||
whitespace at end
|
||||
unchanged line
|
||||
CR at endQ
|
||||
git diff -b --ignore-space-at-eol > out
|
||||
test_expect_failure 'another test, with -b --ignore-space-at-eol' 'test_cmp expect out'
|
||||
|
||||
tr 'Q' '\015' << EOF > expect
|
||||
diff --git a/x b/x
|
||||
index d99af23..8b32fb5 100644
|
||||
--- a/x
|
||||
+++ b/x
|
||||
EOF
|
||||
git diff -b > out
|
||||
test_expect_success 'another test, with -b' 'test_cmp expect out'
|
||||
|
||||
test_expect_success 'check mixed spaces and tabs in indent' '
|
||||
|
||||
# This is indented with SP HT SP.
|
||||
echo " foo();" > x &&
|
||||
git diff --check | grep "space before tab in indent"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check mixed tabs and spaces in indent' '
|
||||
|
||||
# This is indented with HT SP HT.
|
||||
echo " foo();" > x &&
|
||||
git diff --check | grep "space before tab in indent"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors' '
|
||||
|
||||
git commit -m "snapshot" &&
|
||||
echo "foo();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success '--check and --exit-code are not exclusive' '
|
||||
|
||||
git checkout x &&
|
||||
git diff --check --exit-code
|
||||
|
||||
'
|
||||
|
||||
test_expect_success '--check and --quiet are not exclusive' '
|
||||
|
||||
git diff --check --quiet
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with no whitespace errors' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with trailing whitespace' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with space before tab in indent' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff --cached --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors (diff-index)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace (diff-index)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent (diff-index)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with no whitespace errors (diff-index)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git add x &&
|
||||
git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with trailing whitespace (diff-index)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check staged with space before tab in indent (diff-index)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git add x &&
|
||||
test_must_fail git diff-index --cached --check HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with no whitespace errors (diff-tree)' '
|
||||
|
||||
echo "foo();" > x &&
|
||||
git commit -m "new commit" x &&
|
||||
git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with trailing whitespace (diff-tree)' '
|
||||
|
||||
echo "foo(); " > x &&
|
||||
git commit -m "another commit" x &&
|
||||
test_must_fail git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check with space before tab in indent (diff-tree)' '
|
||||
|
||||
# indent has space followed by hard tab
|
||||
echo " foo();" > x &&
|
||||
git commit -m "yet another" x &&
|
||||
test_must_fail git diff-tree --check HEAD^ HEAD
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check trailing whitespace (trailing-space: off)' '
|
||||
|
||||
git config core.whitespace "-trailing-space" &&
|
||||
echo "foo (); " > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check trailing whitespace (trailing-space: on)' '
|
||||
|
||||
git config core.whitespace "trailing-space" &&
|
||||
echo "foo (); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check space before tab in indent (space-before-tab: off)' '
|
||||
|
||||
# indent contains space followed by HT
|
||||
git config core.whitespace "-space-before-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check space before tab in indent (space-before-tab: on)' '
|
||||
|
||||
# indent contains space followed by HT
|
||||
git config core.whitespace "space-before-tab" &&
|
||||
echo " foo (); " > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
|
||||
|
||||
git config core.whitespace "-indent-with-non-tab"
|
||||
echo " foo ();" > x &&
|
||||
git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
|
||||
|
||||
git config core.whitespace "indent-with-non-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
|
||||
|
||||
git config core.whitespace "indent-with-non-tab" &&
|
||||
echo " foo ();" > x &&
|
||||
test_must_fail git diff --check
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'line numbers in --check output are correct' '
|
||||
|
||||
echo "" > x &&
|
||||
echo "foo(); " >> x &&
|
||||
git diff --check | grep "x:2:"
|
||||
|
||||
'
|
||||
|
||||
test_expect_success 'checkdiff detects trailing blank lines' '
|
||||
echo "foo();" >x &&
|
||||
echo "" >>x &&
|
||||
git diff --check | grep "ends with blank"
|
||||
'
|
||||
|
||||
test_expect_success 'checkdiff allows new blank lines' '
|
||||
git checkout x &&
|
||||
mv x y &&
|
||||
(
|
||||
echo "/* This is new */" &&
|
||||
echo "" &&
|
||||
cat y
|
||||
) >x &&
|
||||
git diff --check
|
||||
'
|
||||
|
||||
test_expect_success 'combined diff with autocrlf conversion' '
|
||||
|
||||
git reset --hard &&
|
||||
echo >x hello &&
|
||||
git commit -m "one side" x &&
|
||||
git checkout HEAD^ &&
|
||||
echo >x goodbye &&
|
||||
git commit -m "the other side" x &&
|
||||
git config core.autocrlf true &&
|
||||
test_must_fail git merge master &&
|
||||
|
||||
git diff | sed -e "1,/^@@@/d" >actual &&
|
||||
! grep "^-" actual
|
||||
|
||||
'
|
||||
|
||||
test_done
|
||||
|
||||
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="Python" name="Python">
|
||||
<configuration sdkName="Python 2.7 (C:/Python27/python.exe)" />
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/resources" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Python 2.7 (C:/Python27/python.exe) interpreter library" level="application" />
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/lib/commons-io-1.4.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/lib/java-diff-utils-1.2.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/lib/jython.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="Python" name="Python">
|
||||
<configuration sdkName="Python 2.7 (C:/Python27/python.exe)" />
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/resources" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Python 2.7 (C:/Python27/python.exe) interpreter library" level="application" />
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/lib/commons-io-1.4.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/lib/java-diff-utils-1.2.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module-library">
|
||||
<library>
|
||||
<CLASSES>
|
||||
<root url="jar://$MODULE_DIR$/lib/jython.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
--- /bonobo/bonobo.iml
|
||||
+++ /bonobo/bonobo.iml
|
||||
@@ -41,6 +40,7 @@
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
+ <orderEntry type="library" name="Python 2.6.6 (/usr/bin/python2.6) interpreter library" level="application" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
--- /bonobo/bonobo.iml
|
||||
+++ /bonobo/bonobo.iml
|
||||
@@ -41,6 +40,7 @@
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
+ <orderEntry type="library" name="Python 2.6.6 (/usr/bin/python2.6) interpreter library" level="application" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
svn: '77954' path not found
|
||||
svn: '77954' path not found
|
||||
|
||||
@@ -1 +1 @@
|
||||
svn: '77954' path not found
|
||||
svn: '77954' path not found
|
||||
|
||||
@@ -1,54 +1,54 @@
|
||||
<p>Esta é uma obra Online.</p>
|
||||
<p> </p>
|
||||
<p><strong>Este texto é negrito</strong></p>
|
||||
<p><em>Este texto é itálico</em></p>
|
||||
<p><span style="text-decoration:underline;">Este texto está sublinhado</span></p>
|
||||
<p><span style="text-decoration: line-through;">Este texto está riscado</span></p>
|
||||
<p style="text-align: center;">Este texto está centralizado</p>
|
||||
<p style="text-align: right;">Este texto está alinhado a direita</p>
|
||||
<ol>
|
||||
<li>Este texto está em uma lista numérica<ol>
|
||||
<li>Este texto está identado</li>
|
||||
</ol></li>
|
||||
</ol>
|
||||
<p><a title="uol" href="http://www.uol.com.br" target="_blank">Este
|
||||
aqui é um link</a></p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>,
|
||||
<p> </p>
|
||||
<p>Página 1</p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p>Página 2</p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p>Página 3</p>
|
||||
<p>Esta é uma obra Online.</p>
|
||||
<p> </p>
|
||||
<p><strong>Este texto é negrito</strong></p>
|
||||
<p><em>Este texto é itálico</em></p>
|
||||
<p><span style="text-decoration:underline;">Este texto está sublinhado</span></p>
|
||||
<p><span style="text-decoration: line-through;">Este texto está riscado</span></p>
|
||||
<p style="text-align: center;">Este texto está centralizado</p>
|
||||
<p style="text-align: right;">Este texto está alinhado a direita</p>
|
||||
<ol>
|
||||
<li>Este texto está em uma lista numérica<ol>
|
||||
<li>Este texto está identado</li>
|
||||
</ol></li>
|
||||
</ol>
|
||||
<p><a title="uol" href="http://www.uol.com.br" target="_blank">Este
|
||||
aqui é um link</a></p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>,
|
||||
<p> </p>
|
||||
<p>Página 1</p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p>Página 2</p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p> </p>
|
||||
<p>Página 3</p>
|
||||
<p> </p>
|
||||
@@ -1,11 +1,11 @@
|
||||
<p>Revisão 3</p><p> </p><p>Esta é uma obra
|
||||
Online.</p><p> </p><p><strong>Este texto é
|
||||
negrit</strong></p><p>Este texto é itálico/p><p><span
|
||||
style="text-decoration: undeline;">Este texto está
|
||||
sublinhado</span></p><p>Este texto está riscado agora não
|
||||
está mais</p>p style="text-align: left;">Este texto está
|
||||
centralizado nem este</p><p style="text-align: right;">Este texto
|
||||
está alinhado a direita</p><ol><li>Este texto está em uma
|
||||
lista numérica</li></ol><p><a title="uol"
|
||||
href="http://www.uol.com.br" target="_blank">Este aqui é um
|
||||
<p>Revisão 3</p><p> </p><p>Esta é uma obra
|
||||
Online.</p><p> </p><p><strong>Este texto é
|
||||
negrit</strong></p><p>Este texto é itálico/p><p><span
|
||||
style="text-decoration: undeline;">Este texto está
|
||||
sublinhado</span></p><p>Este texto está riscado agora não
|
||||
está mais</p>p style="text-align: left;">Este texto está
|
||||
centralizado nem este</p><p style="text-align: right;">Este texto
|
||||
está alinhado a direita</p><ol><li>Este texto está em uma
|
||||
lista numérica</li></ol><p><a title="uol"
|
||||
href="http://www.uol.com.br" target="_blank">Este aqui é um
|
||||
link</a></p>
|
||||
@@ -1,174 +1,174 @@
|
||||
/*
|
||||
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
|
||||
|
||||
This file is part of Java Diff Utills Library.
|
||||
|
||||
Java Diff Utills Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Java Diff Utills Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Java Diff Utills Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package difflib;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import difflib.myers.*;
|
||||
|
||||
/**
|
||||
* Implements the difference and patching engine
|
||||
*
|
||||
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
|
||||
* @version 0.4.1
|
||||
*/
|
||||
public class DiffUtils {
|
||||
private static DiffAlgorithm defaultDiffAlgorithm = new MyersDiff();
|
||||
private static Pattern unifiedDiffChunkRe =
|
||||
Pattern.compile("@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@");
|
||||
|
||||
/**
|
||||
* Compute the difference between the original and revised texts with default diff algorithm
|
||||
*
|
||||
* @param original the original text
|
||||
* @param revised the revised text
|
||||
* @return the patch describing the difference between the original and revised texts
|
||||
*/
|
||||
public static Patch diff(List<?> original, List<?> revised) {
|
||||
return DiffUtils.diff(original, revised, defaultDiffAlgorithm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the difference between the original and revised texts with given diff algorithm
|
||||
*
|
||||
* @param original the original text
|
||||
* @param revised the revised text
|
||||
* @param algorithm the given algorithm
|
||||
* @return the patch describing the difference between the original and revised texts
|
||||
*/
|
||||
public static Patch diff(List<?> original, List<?> revised, DiffAlgorithm algorithm) {
|
||||
return algorithm.diff(original, revised);
|
||||
}
|
||||
|
||||
/**
|
||||
* Patch the original text with given patch
|
||||
*
|
||||
* @param original the original text
|
||||
* @param patch the given patch
|
||||
* @return the revised text
|
||||
* @throws PatchFailedException if can't apply patch
|
||||
*/
|
||||
public static List<?> patch(List<?> original, Patch patch) throws PatchFailedException {
|
||||
return patch.applyTo(original);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpatch the revised text for a given patch
|
||||
*
|
||||
* @param revised the revised text
|
||||
* @param patch the given patch
|
||||
* @return the original text
|
||||
*/
|
||||
public static List<?> unpatch(List<?> revised, Patch patch) {
|
||||
return patch.restore(revised);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given text in unified format and creates the list of deltas for it.
|
||||
*
|
||||
* @param diff the text in unified format
|
||||
* @return the patch with deltas.
|
||||
*/
|
||||
public static Patch parseUnifiedDiff(List<String> diff) {
|
||||
boolean inPrelude = true;
|
||||
List<Object[]> rawChunk = new ArrayList<Object[]>();
|
||||
Patch patch = new Patch();
|
||||
|
||||
int old_ln = 0, old_n = 0, new_ln = 0, new_n = 0;
|
||||
String tag = "", rest = "";
|
||||
for (String line: diff) {
|
||||
// Skip leading lines until after we've seen one starting with '+++'
|
||||
if (inPrelude) {
|
||||
if (line.startsWith("+++")) {
|
||||
inPrelude = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Matcher m = unifiedDiffChunkRe.matcher(line);
|
||||
if (m.find()) {
|
||||
// Process the lines in the previous chunk
|
||||
if (rawChunk.size() != 0) {
|
||||
List<String> oldChunkLines = new ArrayList<String>();
|
||||
List<String> newChunkLines = new ArrayList<String>();
|
||||
|
||||
for (Object[] raw_line: rawChunk) {
|
||||
tag = (String)raw_line[0];
|
||||
rest = (String)raw_line[1];
|
||||
if (tag.equals(" ") || tag.equals("-")) {
|
||||
oldChunkLines.add(rest);
|
||||
}
|
||||
if (tag.equals(" ") || tag.equals("+")) {
|
||||
newChunkLines.add(rest);
|
||||
}
|
||||
}
|
||||
patch.addDelta(new ChangeDelta(new Chunk(old_ln - 1, old_n, oldChunkLines),
|
||||
new Chunk(new_ln - 1, new_n, newChunkLines)));
|
||||
rawChunk.clear();
|
||||
}
|
||||
// Parse the @@ header
|
||||
old_ln = m.group(1) == null ? 1 : Integer.parseInt(m.group(1));
|
||||
old_n = m.group(2) == null ? 1 : Integer.parseInt(m.group(2));
|
||||
new_ln = m.group(3) == null ? 1 : Integer.parseInt(m.group(3));
|
||||
new_n = m.group(4) == null ? 1 : Integer.parseInt(m.group(4));
|
||||
old_ln = Integer.parseInt(m.group(1));
|
||||
|
||||
if (old_ln == 0) {
|
||||
old_ln += 1;
|
||||
}
|
||||
if (new_ln == 0) {
|
||||
new_ln += 1;
|
||||
}
|
||||
} else {
|
||||
if (line.length() > 0) {
|
||||
tag = line.substring(0, 1);
|
||||
rest = line.substring(1);
|
||||
if (tag.equals(" ") || tag.equals("+") || tag.equals("-")) {
|
||||
rawChunk.add(new Object[] {tag, rest});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process the lines in the last chunk
|
||||
if (rawChunk.size() != 0) {
|
||||
List<String> oldChunkLines = new ArrayList<String>();
|
||||
List<String> newChunkLines = new ArrayList<String>();
|
||||
|
||||
for (Object[] raw_line: rawChunk) {
|
||||
tag = (String)raw_line[0];
|
||||
rest = (String)raw_line[1];
|
||||
if (tag.equals(" ") || tag.equals("-")) {
|
||||
oldChunkLines.add(rest);
|
||||
}
|
||||
if (tag.equals(" ") || tag.equals("+")) {
|
||||
newChunkLines.add(rest);
|
||||
}
|
||||
}
|
||||
|
||||
patch.addDelta(new ChangeDelta(new Chunk(old_ln - 1, old_n, oldChunkLines),
|
||||
new Chunk(new_ln - 1, new_n, newChunkLines)));
|
||||
rawChunk.clear();
|
||||
}
|
||||
|
||||
return patch;
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
|
||||
|
||||
This file is part of Java Diff Utills Library.
|
||||
|
||||
Java Diff Utills Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Java Diff Utills Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Java Diff Utills Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package difflib;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import difflib.myers.*;
|
||||
|
||||
/**
|
||||
* Implements the difference and patching engine
|
||||
*
|
||||
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
|
||||
* @version 0.4.1
|
||||
*/
|
||||
public class DiffUtils {
|
||||
private static DiffAlgorithm defaultDiffAlgorithm = new MyersDiff();
|
||||
private static Pattern unifiedDiffChunkRe =
|
||||
Pattern.compile("@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@");
|
||||
|
||||
/**
|
||||
* Compute the difference between the original and revised texts with default diff algorithm
|
||||
*
|
||||
* @param original the original text
|
||||
* @param revised the revised text
|
||||
* @return the patch describing the difference between the original and revised texts
|
||||
*/
|
||||
public static Patch diff(List<?> original, List<?> revised) {
|
||||
return DiffUtils.diff(original, revised, defaultDiffAlgorithm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the difference between the original and revised texts with given diff algorithm
|
||||
*
|
||||
* @param original the original text
|
||||
* @param revised the revised text
|
||||
* @param algorithm the given algorithm
|
||||
* @return the patch describing the difference between the original and revised texts
|
||||
*/
|
||||
public static Patch diff(List<?> original, List<?> revised, DiffAlgorithm algorithm) {
|
||||
return algorithm.diff(original, revised);
|
||||
}
|
||||
|
||||
/**
|
||||
* Patch the original text with given patch
|
||||
*
|
||||
* @param original the original text
|
||||
* @param patch the given patch
|
||||
* @return the revised text
|
||||
* @throws PatchFailedException if can't apply patch
|
||||
*/
|
||||
public static List<?> patch(List<?> original, Patch patch) throws PatchFailedException {
|
||||
return patch.applyTo(original);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpatch the revised text for a given patch
|
||||
*
|
||||
* @param revised the revised text
|
||||
* @param patch the given patch
|
||||
* @return the original text
|
||||
*/
|
||||
public static List<?> unpatch(List<?> revised, Patch patch) {
|
||||
return patch.restore(revised);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given text in unified format and creates the list of deltas for it.
|
||||
*
|
||||
* @param diff the text in unified format
|
||||
* @return the patch with deltas.
|
||||
*/
|
||||
public static Patch parseUnifiedDiff(List<String> diff) {
|
||||
boolean inPrelude = true;
|
||||
List<Object[]> rawChunk = new ArrayList<Object[]>();
|
||||
Patch patch = new Patch();
|
||||
|
||||
int old_ln = 0, old_n = 0, new_ln = 0, new_n = 0;
|
||||
String tag = "", rest = "";
|
||||
for (String line: diff) {
|
||||
// Skip leading lines until after we've seen one starting with '+++'
|
||||
if (inPrelude) {
|
||||
if (line.startsWith("+++")) {
|
||||
inPrelude = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Matcher m = unifiedDiffChunkRe.matcher(line);
|
||||
if (m.find()) {
|
||||
// Process the lines in the previous chunk
|
||||
if (rawChunk.size() != 0) {
|
||||
List<String> oldChunkLines = new ArrayList<String>();
|
||||
List<String> newChunkLines = new ArrayList<String>();
|
||||
|
||||
for (Object[] raw_line: rawChunk) {
|
||||
tag = (String)raw_line[0];
|
||||
rest = (String)raw_line[1];
|
||||
if (tag.equals(" ") || tag.equals("-")) {
|
||||
oldChunkLines.add(rest);
|
||||
}
|
||||
if (tag.equals(" ") || tag.equals("+")) {
|
||||
newChunkLines.add(rest);
|
||||
}
|
||||
}
|
||||
patch.addDelta(new ChangeDelta(new Chunk(old_ln - 1, old_n, oldChunkLines),
|
||||
new Chunk(new_ln - 1, new_n, newChunkLines)));
|
||||
rawChunk.clear();
|
||||
}
|
||||
// Parse the @@ header
|
||||
old_ln = m.group(1) == null ? 1 : Integer.parseInt(m.group(1));
|
||||
old_n = m.group(2) == null ? 1 : Integer.parseInt(m.group(2));
|
||||
new_ln = m.group(3) == null ? 1 : Integer.parseInt(m.group(3));
|
||||
new_n = m.group(4) == null ? 1 : Integer.parseInt(m.group(4));
|
||||
old_ln = Integer.parseInt(m.group(1));
|
||||
|
||||
if (old_ln == 0) {
|
||||
old_ln += 1;
|
||||
}
|
||||
if (new_ln == 0) {
|
||||
new_ln += 1;
|
||||
}
|
||||
} else {
|
||||
if (line.length() > 0) {
|
||||
tag = line.substring(0, 1);
|
||||
rest = line.substring(1);
|
||||
if (tag.equals(" ") || tag.equals("+") || tag.equals("-")) {
|
||||
rawChunk.add(new Object[] {tag, rest});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process the lines in the last chunk
|
||||
if (rawChunk.size() != 0) {
|
||||
List<String> oldChunkLines = new ArrayList<String>();
|
||||
List<String> newChunkLines = new ArrayList<String>();
|
||||
|
||||
for (Object[] raw_line: rawChunk) {
|
||||
tag = (String)raw_line[0];
|
||||
rest = (String)raw_line[1];
|
||||
if (tag.equals(" ") || tag.equals("-")) {
|
||||
oldChunkLines.add(rest);
|
||||
}
|
||||
if (tag.equals(" ") || tag.equals("+")) {
|
||||
newChunkLines.add(rest);
|
||||
}
|
||||
}
|
||||
|
||||
patch.addDelta(new ChangeDelta(new Chunk(old_ln - 1, old_n, oldChunkLines),
|
||||
new Chunk(new_ln - 1, new_n, newChunkLines)));
|
||||
rawChunk.clear();
|
||||
}
|
||||
|
||||
return patch;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,308 +1,308 @@
|
||||
/*
|
||||
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
|
||||
|
||||
This file is part of Java Diff Utils Library.
|
||||
|
||||
Java Diff Utils Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Java Diff Utils Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Java Diff Utils Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package difflib;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import difflib.myers.*;
|
||||
|
||||
/**
|
||||
* Implements the difference and patching engine
|
||||
*
|
||||
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
|
||||
* @version 0.4.1
|
||||
*/
|
||||
public class DiffUtils {
|
||||
private static DiffAlgorithm defaultDiffAlgorithm = new MyersDiff();
|
||||
private static Pattern unifiedDiffChunkRe =
|
||||
Pattern.compile("@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@");
|
||||
|
||||
/**
|
||||
* Compute the difference between the original and revised texts with default diff algorithm
|
||||
*
|
||||
* @param original the original text
|
||||
* @param revised the revised text
|
||||
* @return the patch describing the difference between the original and revised texts
|
||||
*/
|
||||
public static Patch diff(List<?> original, List<?> revised) {
|
||||
return DiffUtils.diff(original, revised, defaultDiffAlgorithm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the difference between the original and revised texts with given diff algorithm
|
||||
*
|
||||
* @param original the original text
|
||||
* @param revised the revised text
|
||||
* @param algorithm the given algorithm
|
||||
* @return the patch describing the difference between the original and revised texts
|
||||
*/
|
||||
public static Patch diff(List<?> original, List<?> revised, DiffAlgorithm algorithm) {
|
||||
return algorithm.diff(original, revised);
|
||||
}
|
||||
|
||||
/**
|
||||
* Patch the original text with given patch
|
||||
*
|
||||
* @param original the original text
|
||||
* @param patch the given patch
|
||||
* @return the revised text
|
||||
* @throws PatchFailedException if can't apply patch
|
||||
*/
|
||||
public static List<?> patch(List<?> original, Patch patch) throws PatchFailedException {
|
||||
return patch.applyTo(original);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpatch the revised text for a given patch
|
||||
*
|
||||
* @param revised the revised text
|
||||
* @param patch the given patch
|
||||
* @return the original text
|
||||
*/
|
||||
public static List<?> unpatch(List<?> revised, Patch patch) {
|
||||
return patch.restore(revised); // bla-bla-bla
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given text in unified format and creates the list of deltas for it.
|
||||
*
|
||||
* @param diff the text in unified format
|
||||
* @return the patch with deltas.
|
||||
*/
|
||||
public static Patch parseUnifiedDiff(List<String> diff) {
|
||||
boolean inPrelude = true;
|
||||
List<Object[]> rawChunk = new ArrayList<Object[]>();
|
||||
Patch patch = new Patch();
|
||||
|
||||
int old_ln = 0, old_n = 0, new_ln = 0, new_n = 0;
|
||||
String tag = "", rest = "";
|
||||
for (String line: diff) {
|
||||
// Skip leading lines until after we've seen one starting with '+++'
|
||||
if (inPrelude) {
|
||||
if (line.startsWith("+++")) {
|
||||
inPrelude = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Matcher m = unifiedDiffChunkRe.matcher(line);
|
||||
if (m.find()) {
|
||||
// Process the lines in the previous chunk
|
||||
if (rawChunk.size() != 0) {
|
||||
List<String> oldChunkLines = new ArrayList<String>();
|
||||
List<String> newChunkLines = new ArrayList<String>();
|
||||
|
||||
for (Object[] raw_line: rawChunk) {
|
||||
tag = (String)raw_line[0];
|
||||
rest = (String)raw_line[1];
|
||||
if (tag.equals(" ") || tag.equals("-")) {
|
||||
oldChunkLines.add(rest);
|
||||
}
|
||||
if (tag.equals(" ") || tag.equals("+")) {
|
||||
newChunkLines.add(rest);
|
||||
}
|
||||
}
|
||||
patch.addDelta(new ChangeDelta(new Chunk(old_ln - 1, old_n, oldChunkLines),
|
||||
new Chunk(new_ln - 1, new_n, newChunkLines)));
|
||||
rawChunk.clear();
|
||||
}
|
||||
// Parse the @@ header
|
||||
old_ln = m.group(1) == null ? 1 : Integer.parseInt(m.group(1));
|
||||
old_n = m.group(2) == null ? 1 : Integer.parseInt(m.group(2));
|
||||
new_ln = m.group(3) == null ? 1 : Integer.parseInt(m.group(3));
|
||||
new_n = m.group(4) == null ? 1 : Integer.parseInt(m.group(4));
|
||||
old_ln = Integer.parseInt(m.group(1));
|
||||
|
||||
if (old_ln == 0) {
|
||||
old_ln += 1;
|
||||
}
|
||||
if (new_ln == 0) {
|
||||
new_ln += 1;
|
||||
}
|
||||
} else {
|
||||
if (line.length() > 0) {
|
||||
tag = line.substring(0, 1);
|
||||
rest = line.substring(1);
|
||||
if (tag.equals(" ") || tag.equals("+") || tag.equals("-")) {
|
||||
rawChunk.add(new Object[] {tag, rest});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process the lines in the last chunk
|
||||
if (rawChunk.size() != 0) {
|
||||
List<String> oldChunkLines = new ArrayList<String>();
|
||||
List<String> newChunkLines = new ArrayList<String>();
|
||||
|
||||
for (Object[] raw_line: rawChunk)
|
||||
{
|
||||
tag = (String)raw_line[0];
|
||||
rest = (String)raw_line[1];
|
||||
if (tag.equals(" ") || tag.equals("-"))
|
||||
{
|
||||
oldChunkLines.add(rest);
|
||||
}
|
||||
if (tag.equals(" ") || tag.equals("+"))
|
||||
{
|
||||
newChunkLines.add(rest);
|
||||
}
|
||||
}
|
||||
|
||||
patch.addDelta(new ChangeDelta(new Chunk(old_ln - 1, old_n, oldChunkLines),
|
||||
new Chunk(new_ln - 1, new_n, newChunkLines)));
|
||||
rawChunk.clear();
|
||||
}
|
||||
|
||||
return patch;
|
||||
}
|
||||
|
||||
/**
|
||||
* generateUnifiedDiff takes a Patch and some other arguments, returning the Unified Diff format text representing the Patch.
|
||||
* @author Bill James (tankerbay@gmail.com)
|
||||
*
|
||||
* @param fname1 - Filename of the original (unrevised file)
|
||||
* @param fname2 - Filename of the revised file
|
||||
* @param originalLines - Lines of the original file
|
||||
* @param patch - Patch created by the diff() function
|
||||
* @param contextSize - number of lines of context output around each difference in the file.
|
||||
* @return List of strings representing the Unified Diff representation of the Patch argument.
|
||||
*/
|
||||
public static List<String> generateUnifiedDiff(String fname1, String fname2, List<String> originalLines, Patch patch, int contextSize ) {
|
||||
List<String> ret = new ArrayList<String>();
|
||||
ret.add( "--- " + fname1 );
|
||||
ret.add( "+++ " + fname2 );
|
||||
|
||||
List<Delta> cur = new ArrayList<Delta>(); // current list of Delta's to process
|
||||
int deltact = patch.getDeltas().size();
|
||||
// if there's more than 1 Delta, we may need to output them together
|
||||
if ( deltact > 1 ) {
|
||||
Delta curDelta = patch.getDelta(0);
|
||||
cur.add( curDelta ); // add the first Delta to the current set
|
||||
for ( int i = 1; i < deltact; i++ ) {
|
||||
int curpos = curDelta.getOriginal().getPosition(); // store the current position of the first Delta
|
||||
Delta nextDelta = patch.getDelta(i); // Check if the next Delta is too close to the current position
|
||||
if ( (curpos + curDelta.getOriginal().getSize() + contextSize) >= ( nextDelta.getOriginal().getPosition()-contextSize ) ) {
|
||||
cur.add( nextDelta ); // if it is, add it to the current set
|
||||
} else {
|
||||
List<String> curBlock = processDeltas( originalLines, cur, contextSize );
|
||||
ret.addAll( curBlock ); // if it isn't, output the current set, then create a new
|
||||
cur.clear(); // set and add the current Delta to it.
|
||||
cur.add( nextDelta );
|
||||
}
|
||||
curDelta = nextDelta;
|
||||
}
|
||||
List<String> curBlock = processDeltas( originalLines, cur, contextSize ); // don't forget to process the last set of Deltas
|
||||
ret.addAll( curBlock );
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* processDeltas takes a list of Deltas and outputs them together in a single block of Unified-Diff-format text.
|
||||
* @author Bill James (tankerbay@gmail.com)
|
||||
*
|
||||
* @param origLines - the lines of the original file
|
||||
* @param deltas - the Deltas to be output as a single block
|
||||
* @param contextSize - the number of lines of context to place around block
|
||||
* @return
|
||||
*/
|
||||
private static List<String> processDeltas( List<String> origLines, List<Delta> deltas, int contextSize ) {
|
||||
List<String> buffer = new ArrayList<String>();
|
||||
int origTotal = 0; // counter for total lines output from Original
|
||||
int revTotal = 0; // counter for total lines output from Original
|
||||
int line;
|
||||
|
||||
Delta curDelta = deltas.get(0); // start with the first Delta
|
||||
int origStart = curDelta.getOriginal().getPosition()+1 - contextSize; // note the +1 to overcome the 0-offset Position
|
||||
if ( origStart < 1 ) origStart = 1; // clamp to the start of the file
|
||||
int revStart = curDelta.getRevised().getPosition()+1 - contextSize; // note the +1 to overcome the 0-offset Position
|
||||
if ( revStart < 1 ) revStart = 1; // clamp to the start of the file
|
||||
int contextStart = curDelta.getOriginal().getPosition() - contextSize; // find the start of the wrapper context code
|
||||
if ( contextStart < 0 ) contextStart = 0; // clamp to the start of the file
|
||||
for ( line = contextStart; line < curDelta.getOriginal().getPosition(); line++ ) { // output the context before the first Delta
|
||||
buffer.add( " " + origLines.get( line ) );
|
||||
origTotal++;
|
||||
revTotal++;
|
||||
}
|
||||
buffer.addAll( getDeltaText( curDelta ) ); // output the first Delta
|
||||
origTotal += curDelta.getOriginal().getLines().size();
|
||||
revTotal += curDelta.getRevised().getLines().size();
|
||||
|
||||
int deltaIndex = 1;
|
||||
while ( deltaIndex < deltas.size() ) { // for each of the other Deltas
|
||||
Delta nextDelta = deltas.get( deltaIndex );
|
||||
int intermediateStart = curDelta.getOriginal().getPosition() + curDelta.getOriginal().getLines().size();
|
||||
for ( line = intermediateStart; line < nextDelta.getOriginal().getPosition(); line++ ) {
|
||||
buffer.add( " " + origLines.get( line ) ); // output the code between the last Delta and this one
|
||||
origTotal++;
|
||||
revTotal++;
|
||||
}
|
||||
buffer.addAll( getDeltaText( nextDelta ) ); // output the Delta
|
||||
origTotal += nextDelta.getOriginal().getLines().size();
|
||||
revTotal += nextDelta.getRevised().getLines().size();
|
||||
curDelta = nextDelta;
|
||||
deltaIndex++; // increment the iterator
|
||||
}
|
||||
|
||||
// Now output the post-Delta context code, clamping the end of the file
|
||||
contextStart = curDelta.getOriginal().getPosition() + curDelta.getOriginal().getLines().size();
|
||||
for ( line = contextStart; ( line < (contextStart + contextSize )) && ( line < origLines.size() ); line++ ) {
|
||||
buffer.add( " " + origLines.get( line ) );
|
||||
origTotal++;
|
||||
revTotal++;
|
||||
}
|
||||
|
||||
// Create and insert the block header, conforming to the Unified Diff standard
|
||||
StringBuffer header = new StringBuffer();
|
||||
header.append( "@@ -" );
|
||||
header.append( origStart );
|
||||
header.append( "," );
|
||||
header.append( origTotal );
|
||||
header.append( " +" );
|
||||
header.append( revStart );
|
||||
header.append( "," );
|
||||
header.append( revTotal );
|
||||
header.append( " @@" );
|
||||
buffer.add( 0, header.toString() );
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDeltaText returns the lines to be added to the Unified Diff text from the Delta parameter
|
||||
* @author Bill James (tankerbay@gmail.com)
|
||||
*
|
||||
* @param delta - the Delta to output
|
||||
* @return list of String lines of code.
|
||||
*/
|
||||
private static List<String> getDeltaText( Delta delta ) {
|
||||
List<String> buffer = new ArrayList<String>();
|
||||
for ( Object line: delta.getOriginal().getLines() ) {
|
||||
buffer.add( "-" + line );
|
||||
}
|
||||
for ( Object line: delta.getRevised().getLines() ) {
|
||||
buffer.add( "+" + line );
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
|
||||
|
||||
This file is part of Java Diff Utils Library.
|
||||
|
||||
Java Diff Utils Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Java Diff Utils Library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Java Diff Utils Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package difflib;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import difflib.myers.*;
|
||||
|
||||
/**
|
||||
* Implements the difference and patching engine
|
||||
*
|
||||
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
|
||||
* @version 0.4.1
|
||||
*/
|
||||
public class DiffUtils {
|
||||
private static DiffAlgorithm defaultDiffAlgorithm = new MyersDiff();
|
||||
private static Pattern unifiedDiffChunkRe =
|
||||
Pattern.compile("@@\\s+-(?:(\\d+)(?:,(\\d+))?)\\s+\\+(?:(\\d+)(?:,(\\d+))?)\\s+@@");
|
||||
|
||||
/**
|
||||
* Compute the difference between the original and revised texts with default diff algorithm
|
||||
*
|
||||
* @param original the original text
|
||||
* @param revised the revised text
|
||||
* @return the patch describing the difference between the original and revised texts
|
||||
*/
|
||||
public static Patch diff(List<?> original, List<?> revised) {
|
||||
return DiffUtils.diff(original, revised, defaultDiffAlgorithm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the difference between the original and revised texts with given diff algorithm
|
||||
*
|
||||
* @param original the original text
|
||||
* @param revised the revised text
|
||||
* @param algorithm the given algorithm
|
||||
* @return the patch describing the difference between the original and revised texts
|
||||
*/
|
||||
public static Patch diff(List<?> original, List<?> revised, DiffAlgorithm algorithm) {
|
||||
return algorithm.diff(original, revised);
|
||||
}
|
||||
|
||||
/**
|
||||
* Patch the original text with given patch
|
||||
*
|
||||
* @param original the original text
|
||||
* @param patch the given patch
|
||||
* @return the revised text
|
||||
* @throws PatchFailedException if can't apply patch
|
||||
*/
|
||||
public static List<?> patch(List<?> original, Patch patch) throws PatchFailedException {
|
||||
return patch.applyTo(original);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpatch the revised text for a given patch
|
||||
*
|
||||
* @param revised the revised text
|
||||
* @param patch the given patch
|
||||
* @return the original text
|
||||
*/
|
||||
public static List<?> unpatch(List<?> revised, Patch patch) {
|
||||
return patch.restore(revised); // bla-bla-bla
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given text in unified format and creates the list of deltas for it.
|
||||
*
|
||||
* @param diff the text in unified format
|
||||
* @return the patch with deltas.
|
||||
*/
|
||||
public static Patch parseUnifiedDiff(List<String> diff) {
|
||||
boolean inPrelude = true;
|
||||
List<Object[]> rawChunk = new ArrayList<Object[]>();
|
||||
Patch patch = new Patch();
|
||||
|
||||
int old_ln = 0, old_n = 0, new_ln = 0, new_n = 0;
|
||||
String tag = "", rest = "";
|
||||
for (String line: diff) {
|
||||
// Skip leading lines until after we've seen one starting with '+++'
|
||||
if (inPrelude) {
|
||||
if (line.startsWith("+++")) {
|
||||
inPrelude = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Matcher m = unifiedDiffChunkRe.matcher(line);
|
||||
if (m.find()) {
|
||||
// Process the lines in the previous chunk
|
||||
if (rawChunk.size() != 0) {
|
||||
List<String> oldChunkLines = new ArrayList<String>();
|
||||
List<String> newChunkLines = new ArrayList<String>();
|
||||
|
||||
for (Object[] raw_line: rawChunk) {
|
||||
tag = (String)raw_line[0];
|
||||
rest = (String)raw_line[1];
|
||||
if (tag.equals(" ") || tag.equals("-")) {
|
||||
oldChunkLines.add(rest);
|
||||
}
|
||||
if (tag.equals(" ") || tag.equals("+")) {
|
||||
newChunkLines.add(rest);
|
||||
}
|
||||
}
|
||||
patch.addDelta(new ChangeDelta(new Chunk(old_ln - 1, old_n, oldChunkLines),
|
||||
new Chunk(new_ln - 1, new_n, newChunkLines)));
|
||||
rawChunk.clear();
|
||||
}
|
||||
// Parse the @@ header
|
||||
old_ln = m.group(1) == null ? 1 : Integer.parseInt(m.group(1));
|
||||
old_n = m.group(2) == null ? 1 : Integer.parseInt(m.group(2));
|
||||
new_ln = m.group(3) == null ? 1 : Integer.parseInt(m.group(3));
|
||||
new_n = m.group(4) == null ? 1 : Integer.parseInt(m.group(4));
|
||||
old_ln = Integer.parseInt(m.group(1));
|
||||
|
||||
if (old_ln == 0) {
|
||||
old_ln += 1;
|
||||
}
|
||||
if (new_ln == 0) {
|
||||
new_ln += 1;
|
||||
}
|
||||
} else {
|
||||
if (line.length() > 0) {
|
||||
tag = line.substring(0, 1);
|
||||
rest = line.substring(1);
|
||||
if (tag.equals(" ") || tag.equals("+") || tag.equals("-")) {
|
||||
rawChunk.add(new Object[] {tag, rest});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process the lines in the last chunk
|
||||
if (rawChunk.size() != 0) {
|
||||
List<String> oldChunkLines = new ArrayList<String>();
|
||||
List<String> newChunkLines = new ArrayList<String>();
|
||||
|
||||
for (Object[] raw_line: rawChunk)
|
||||
{
|
||||
tag = (String)raw_line[0];
|
||||
rest = (String)raw_line[1];
|
||||
if (tag.equals(" ") || tag.equals("-"))
|
||||
{
|
||||
oldChunkLines.add(rest);
|
||||
}
|
||||
if (tag.equals(" ") || tag.equals("+"))
|
||||
{
|
||||
newChunkLines.add(rest);
|
||||
}
|
||||
}
|
||||
|
||||
patch.addDelta(new ChangeDelta(new Chunk(old_ln - 1, old_n, oldChunkLines),
|
||||
new Chunk(new_ln - 1, new_n, newChunkLines)));
|
||||
rawChunk.clear();
|
||||
}
|
||||
|
||||
return patch;
|
||||
}
|
||||
|
||||
/**
|
||||
* generateUnifiedDiff takes a Patch and some other arguments, returning the Unified Diff format text representing the Patch.
|
||||
* @author Bill James (tankerbay@gmail.com)
|
||||
*
|
||||
* @param fname1 - Filename of the original (unrevised file)
|
||||
* @param fname2 - Filename of the revised file
|
||||
* @param originalLines - Lines of the original file
|
||||
* @param patch - Patch created by the diff() function
|
||||
* @param contextSize - number of lines of context output around each difference in the file.
|
||||
* @return List of strings representing the Unified Diff representation of the Patch argument.
|
||||
*/
|
||||
public static List<String> generateUnifiedDiff(String fname1, String fname2, List<String> originalLines, Patch patch, int contextSize ) {
|
||||
List<String> ret = new ArrayList<String>();
|
||||
ret.add( "--- " + fname1 );
|
||||
ret.add( "+++ " + fname2 );
|
||||
|
||||
List<Delta> cur = new ArrayList<Delta>(); // current list of Delta's to process
|
||||
int deltact = patch.getDeltas().size();
|
||||
// if there's more than 1 Delta, we may need to output them together
|
||||
if ( deltact > 1 ) {
|
||||
Delta curDelta = patch.getDelta(0);
|
||||
cur.add( curDelta ); // add the first Delta to the current set
|
||||
for ( int i = 1; i < deltact; i++ ) {
|
||||
int curpos = curDelta.getOriginal().getPosition(); // store the current position of the first Delta
|
||||
Delta nextDelta = patch.getDelta(i); // Check if the next Delta is too close to the current position
|
||||
if ( (curpos + curDelta.getOriginal().getSize() + contextSize) >= ( nextDelta.getOriginal().getPosition()-contextSize ) ) {
|
||||
cur.add( nextDelta ); // if it is, add it to the current set
|
||||
} else {
|
||||
List<String> curBlock = processDeltas( originalLines, cur, contextSize );
|
||||
ret.addAll( curBlock ); // if it isn't, output the current set, then create a new
|
||||
cur.clear(); // set and add the current Delta to it.
|
||||
cur.add( nextDelta );
|
||||
}
|
||||
curDelta = nextDelta;
|
||||
}
|
||||
List<String> curBlock = processDeltas( originalLines, cur, contextSize ); // don't forget to process the last set of Deltas
|
||||
ret.addAll( curBlock );
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* processDeltas takes a list of Deltas and outputs them together in a single block of Unified-Diff-format text.
|
||||
* @author Bill James (tankerbay@gmail.com)
|
||||
*
|
||||
* @param origLines - the lines of the original file
|
||||
* @param deltas - the Deltas to be output as a single block
|
||||
* @param contextSize - the number of lines of context to place around block
|
||||
* @return
|
||||
*/
|
||||
private static List<String> processDeltas( List<String> origLines, List<Delta> deltas, int contextSize ) {
|
||||
List<String> buffer = new ArrayList<String>();
|
||||
int origTotal = 0; // counter for total lines output from Original
|
||||
int revTotal = 0; // counter for total lines output from Original
|
||||
int line;
|
||||
|
||||
Delta curDelta = deltas.get(0); // start with the first Delta
|
||||
int origStart = curDelta.getOriginal().getPosition()+1 - contextSize; // note the +1 to overcome the 0-offset Position
|
||||
if ( origStart < 1 ) origStart = 1; // clamp to the start of the file
|
||||
int revStart = curDelta.getRevised().getPosition()+1 - contextSize; // note the +1 to overcome the 0-offset Position
|
||||
if ( revStart < 1 ) revStart = 1; // clamp to the start of the file
|
||||
int contextStart = curDelta.getOriginal().getPosition() - contextSize; // find the start of the wrapper context code
|
||||
if ( contextStart < 0 ) contextStart = 0; // clamp to the start of the file
|
||||
for ( line = contextStart; line < curDelta.getOriginal().getPosition(); line++ ) { // output the context before the first Delta
|
||||
buffer.add( " " + origLines.get( line ) );
|
||||
origTotal++;
|
||||
revTotal++;
|
||||
}
|
||||
buffer.addAll( getDeltaText( curDelta ) ); // output the first Delta
|
||||
origTotal += curDelta.getOriginal().getLines().size();
|
||||
revTotal += curDelta.getRevised().getLines().size();
|
||||
|
||||
int deltaIndex = 1;
|
||||
while ( deltaIndex < deltas.size() ) { // for each of the other Deltas
|
||||
Delta nextDelta = deltas.get( deltaIndex );
|
||||
int intermediateStart = curDelta.getOriginal().getPosition() + curDelta.getOriginal().getLines().size();
|
||||
for ( line = intermediateStart; line < nextDelta.getOriginal().getPosition(); line++ ) {
|
||||
buffer.add( " " + origLines.get( line ) ); // output the code between the last Delta and this one
|
||||
origTotal++;
|
||||
revTotal++;
|
||||
}
|
||||
buffer.addAll( getDeltaText( nextDelta ) ); // output the Delta
|
||||
origTotal += nextDelta.getOriginal().getLines().size();
|
||||
revTotal += nextDelta.getRevised().getLines().size();
|
||||
curDelta = nextDelta;
|
||||
deltaIndex++; // increment the iterator
|
||||
}
|
||||
|
||||
// Now output the post-Delta context code, clamping the end of the file
|
||||
contextStart = curDelta.getOriginal().getPosition() + curDelta.getOriginal().getLines().size();
|
||||
for ( line = contextStart; ( line < (contextStart + contextSize )) && ( line < origLines.size() ); line++ ) {
|
||||
buffer.add( " " + origLines.get( line ) );
|
||||
origTotal++;
|
||||
revTotal++;
|
||||
}
|
||||
|
||||
// Create and insert the block header, conforming to the Unified Diff standard
|
||||
StringBuffer header = new StringBuffer();
|
||||
header.append( "@@ -" );
|
||||
header.append( origStart );
|
||||
header.append( "," );
|
||||
header.append( origTotal );
|
||||
header.append( " +" );
|
||||
header.append( revStart );
|
||||
header.append( "," );
|
||||
header.append( revTotal );
|
||||
header.append( " @@" );
|
||||
buffer.add( 0, header.toString() );
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* getDeltaText returns the lines to be added to the Unified Diff text from the Delta parameter
|
||||
* @author Bill James (tankerbay@gmail.com)
|
||||
*
|
||||
* @param delta - the Delta to output
|
||||
* @return list of String lines of code.
|
||||
*/
|
||||
private static List<String> getDeltaText( Delta delta ) {
|
||||
List<String> buffer = new ArrayList<String>();
|
||||
for ( Object line: delta.getOriginal().getLines() ) {
|
||||
buffer.add( "-" + line );
|
||||
}
|
||||
for ( Object line: delta.getRevised().getLines() ) {
|
||||
buffer.add( "+" + line );
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user