49 Commits

Author SHA1 Message Date
Tobias Warneke
5a11752126 [maven-release-plugin] prepare release java-diff-utils-parent-4.10 2021-04-28 23:21:51 +02:00
Tobias Warneke
94674a0ad7 fixes #119 2021-04-28 22:00:20 +02:00
Tobias Warneke
91a42b2793 fixes #120 2021-04-28 21:14:32 +02:00
Tobias Warneke
bf4ec94cc9 fixes #117 2021-03-14 23:42:10 +01:00
Tobias Warneke
df27b689f3 Merge origin/master into introduce-conflict-patching 2021-03-14 22:45:22 +01:00
Tobias Warneke
2a1239b3a2 fixes #117 2021-03-14 22:44:20 +01:00
Tobias Warneke
8d38e1012e first test with conflict output 2021-02-27 01:32:45 +01:00
Tobias Warneke
8880ec545b introduced conflict processing 2021-02-21 23:04:17 +01:00
Tobias Warneke
ce05cbf9c9 2021-02-10 07:34:55 +01:00
Tobias Warneke
f71275de08 upgraded some versions 2021-02-07 13:22:52 +01:00
Tobias Warneke
307574225a remove verification from applyTo 2021-02-07 13:22:34 +01:00
Tobias Warneke
b9fea86e43 remove verification from applyTo 2021-02-07 13:22:20 +01:00
Tobias Warneke
b17bbb12c3 fixes #115 2021-02-07 00:47:27 +01:00
Tobias
0905d0e5f7 Update issue templates 2021-02-06 23:35:55 +01:00
Tobias Warneke
57df629662 2021-02-06 21:41:11 +01:00
Tobias Warneke
73ec9e9320 fixes #114 - decompressing deltas - refactoring 2021-01-16 23:21:01 +01:00
Tobias Warneke
507ced4493 fixes #114 - decompressing deltas 2021-01-16 02:30:04 +01:00
Tobias Warneke
7e8d2867ad fixes #114 - decompressing deltas 2021-01-16 02:22:47 +01:00
Tobias Warneke
ffd14fb67d started decompression idea 2021-01-16 00:08:19 +01:00
Tobias Warneke
04b5c7e167 started decompression idea 2021-01-16 00:05:09 +01:00
Tobias Warneke
a9b5e5fa88 fixes #111 - replaced some GNU license including files 2020-12-30 22:30:50 +01:00
Tobias Warneke
4061ab6c57 fixes #110 support for similarity index, rename from, rename to 2020-12-24 00:51:29 +01:00
Tobias Warneke
7da297322b fixes #110 support for similarity index, rename from, rename to 2020-12-24 00:49:10 +01:00
Tobias Warneke
fb0837fd73 fixes #107 process "\ No newline at end of file" 2020-12-19 00:21:09 +01:00
Tobias Warneke
642dc659f1 fixes #107 process "\ No newline at end of file" 2020-12-19 00:17:54 +01:00
Tobias Warneke
e59e9e8379 fixes #107 process "\ No newline at end of file" 2020-12-19 00:12:55 +01:00
Tobias Warneke
6748451e94 Merge origin/master 2020-12-19 00:12:35 +01:00
Tobias Warneke
445381b277 fixes #107 process "\ No newline at end of file" 2020-12-19 00:11:32 +01:00
Jerry James
ab76d7229a Eliminate most unchecked and unsafe operation warnings (#108)
Thx for improving java-diff-utils.
2020-12-17 23:08:59 +01:00
Tobias Warneke
7647c0a39e removed author entry that was accidently introduced into the new implementation of UnifiedDiff tools 2020-12-17 23:02:05 +01:00
Tobias Warneke
759a2ba67a changed action to matrix build project using java 8 and 11 2020-12-17 22:55:11 +01:00
Tobias Warneke
270754fb8c 2020-12-13 23:54:12 +01:00
Tobias Warneke
06f1e0593e 2020-12-13 23:48:24 +01:00
Tobias
26be87091b Update README.md 2020-12-13 23:12:31 +01:00
Tobias
b927e6f477 Update README.md 2020-12-13 23:02:54 +01:00
Tobias
ba3c313440 Update README.md 2020-12-13 23:01:23 +01:00
Tobias
d62e9a9fb6 Update README.md 2020-12-13 23:00:10 +01:00
Tobias Warneke
d7a5247b1d 2020-12-13 22:58:42 +01:00
Tobias Warneke
b8aef6a3da 2020-12-13 22:47:15 +01:00
Tobias
7d8b87a0ec Create maven.yml 2020-12-13 22:41:38 +01:00
Tobias
1fdea352ad Create stale.yml 2020-12-13 22:34:53 +01:00
Tobias Warneke
c6d3f73d22 fixes #104 2020-12-12 00:35:11 +01:00
Mike Samuel
5338313a54 StringUtils.wrapText will not split surrogate pair. (#103)
Previously,

    StringUtils.wrapText(".\uD800\uDC00.", 2)
    //                           ^ index 2 points here

would insert a `<br/>` between two paired surrogates.

Now, it shifts the insertion point to a whole code-point boundary.
2020-11-22 19:48:05 +01:00
Tobias Warneke
55d71fea71 2020-11-01 22:06:19 +01:00
Tobias Warneke
e4ab41bf04 [maven-release-plugin] prepare for next development iteration 2020-11-01 21:32:22 +01:00
Tobias Warneke
1ec708fe30 [maven-release-plugin] prepare release java-diff-utils-parent-4.9 2020-11-01 21:32:21 +01:00
Tobias Warneke
fcf83e918f 2020-11-01 20:03:18 +01:00
Alon Harel
4ee56d2554 make patch serializable (#101) 2020-10-26 13:34:23 +01:00
Tobias Warneke
21f9e9d415 [maven-release-plugin] prepare for next development iteration 2020-09-27 22:08:04 +02:00
41 changed files with 56070 additions and 628 deletions

10
.github/ISSUE_TEMPLATE/simple.md vendored Normal file
View File

@@ -0,0 +1,10 @@
---
name: Simple
about: just a simple issue
title: ''
labels: ''
assignees: ''
---

26
.github/workflows/maven.yml vendored Normal file
View File

@@ -0,0 +1,26 @@
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
name: Java CI with Maven
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
java: [8, 11]
name: Java ${{ matrix.java }} building ...
steps:
- uses: actions/checkout@v2
- name: Set up Java ${{ matrix.java }}
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: Build with Maven
run: mvn -B package --file pom.xml

19
.github/workflows/stale.yml vendored Normal file
View File

@@ -0,0 +1,19 @@
name: Mark stale issues and pull requests
on:
schedule:
- cron: "30 1 * * *"
jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v3
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'Stale issue message'
stale-pr-message: 'Stale pull request message'
stale-issue-label: 'no-issue-activity'
stale-pr-label: 'no-pr-activity'

View File

@@ -9,6 +9,23 @@ This project uses a custom versioning scheme (and not [Semantic Versioning](http
### Changed
## [4.10]
### Changed
* bugfixing on new UnifiedDiff reader / writer for multifile usage
* bugfix for wrong DiffRow type while transforming from a patch that removed a line in one changeset
* introduced change position into UnifiedDiff reader
* introduced first version of conflict output possibility (like GIT merge conflict)
* moved verification to `AbstractDelta`
* introduced `ConflictOutput` to `Patch` to add optional behaviour to patch conflicts
## [4.9]
### Changed
* make patch serializable
## [4.8]
### Changed

View File

@@ -3,7 +3,11 @@
## Status
[![Build Status](https://travis-ci.org/java-diff-utils/java-diff-utils.svg?branch=master)](https://travis-ci.org/java-diff-utils/java-diff-utils)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/7eba77f10bed4c2a8d08ac8dc8da4a86)](https://www.codacy.com/app/wumpz/java-diff-utils?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=java-diff-utils/java-diff-utils&amp;utm_campaign=Badge_Grade)
[![Build Status using Github Actions](https://github.com/java-diff-utils/java-diff-utils/workflows/Java%20CI%20with%20Maven/badge.svg)](https://github.com/java-diff-utils/java-diff-utils/actions?query=workflow%3A%22Java+CI+with+Maven%22)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/002c53aa0c924f71ac80a2f65446dfdd)](https://www.codacy.com/gh/java-diff-utils/java-diff-utils/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=java-diff-utils/java-diff-utils&amp;utm_campaign=Badge_Grade)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.java-diff-utils/java-diff-utils/badge.svg)](http://maven-badges.herokuapp.com/maven-central/io.github.java-diff-utils/java-diff-utils)
## Intro
@@ -20,7 +24,7 @@ Javadocs of the actual release version: [JavaDocs java-diff-utils](https://java-
## Examples
Look [here](https://github.com/wumpz/java-diff-utils/wiki) to find more helpful informations and examples.
Look [here](https://github.com/java-diff-utils/java-diff-utils/wiki) to find more helpful informations and examples.
These two outputs are generated using this java-diff-utils. The source code can also be found at the *Examples* page:
@@ -84,7 +88,7 @@ Just add the code below to your maven dependencies:
<dependency>
<groupId>io.github.java-diff-utils</groupId>
<artifactId>java-diff-utils</artifactId>
<version>4.5</version>
<version>4.9</version>
</dependency>
```

View File

@@ -4,7 +4,7 @@
<parent>
<groupId>io.github.java-diff-utils</groupId>
<artifactId>java-diff-utils-parent</artifactId>
<version>4.8</version>
<version>4.10</version>
</parent>
<artifactId>java-diff-utils-jgit</artifactId>
<name>java-diff-utils-jgit</name>

View File

@@ -41,7 +41,7 @@ public class HistogramDiffTest {
public void testDiff() throws PatchFailedException {
List<String> orgList = Arrays.asList("A", "B", "C", "A", "B", "B", "A");
List<String> revList = Arrays.asList("C", "B", "A", "B", "A", "C");
final Patch<String> patch = Patch.generate(orgList, revList, new HistogramDiff().computeDiff(orgList, revList, null));
final Patch<String> patch = Patch.generate(orgList, revList, new HistogramDiff<String>().computeDiff(orgList, revList, null));
System.out.println(patch);
assertNotNull(patch);
assertEquals(3, patch.getDeltas().size());
@@ -57,7 +57,7 @@ public class HistogramDiffTest {
List<String> revList = Arrays.asList("C", "B", "A", "B", "A", "C");
List<String> logdata = new ArrayList<>();
final Patch<String> patch = Patch.generate(orgList, revList, new HistogramDiff().computeDiff(orgList, revList, new DiffAlgorithmListener() {
final Patch<String> patch = Patch.generate(orgList, revList, new HistogramDiff<String>().computeDiff(orgList, revList, new DiffAlgorithmListener() {
@Override
public void diffStart() {
logdata.add("start");

View File

@@ -45,7 +45,7 @@ public class LRHistogramDiffTest {
List<String> revised = readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb")));
List<String> logdata = new ArrayList<>();
Patch<String> patch = Patch.generate(original, revised, new HistogramDiff().computeDiff(original, revised, new DiffAlgorithmListener() {
Patch<String> patch = Patch.generate(original, revised, new HistogramDiff<String>().computeDiff(original, revised, new DiffAlgorithmListener() {
@Override
public void diffStart() {
logdata.add("start");

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>io.github.java-diff-utils</groupId>
<artifactId>java-diff-utils-parent</artifactId>
<version>4.8</version>
<version>4.10</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@@ -15,6 +15,7 @@
*/
package com.github.difflib.patch;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
@@ -22,7 +23,7 @@ import java.util.Objects;
* Abstract delta between a source and a target.
* @author Tobias Warneke (t.warneke@gmx.net)
*/
public abstract class AbstractDelta<T> {
public abstract class AbstractDelta<T> implements Serializable {
private final Chunk<T> source;
private final Chunk<T> target;
private final DeltaType type;
@@ -53,13 +54,21 @@ public abstract class AbstractDelta<T> {
* @param target
* @throws PatchFailedException
*/
protected void verifyChunk(List<T> target) throws PatchFailedException {
getSource().verify(target);
protected VerifyChunk verifyChunkToFitTarget(List<T> target) throws PatchFailedException {
return getSource().verifyChunk(target);
}
protected VerifyChunk verifyAntApplyTo(List<T> target) throws PatchFailedException {
final VerifyChunk verify = verifyChunkToFitTarget(target);
if (verify == VerifyChunk.OK) {
applyTo(target);
}
return verify;
}
public abstract void applyTo(List<T> target) throws PatchFailedException;
protected abstract void applyTo(List<T> target) throws PatchFailedException;
public abstract void restore(List<T> target);
protected abstract void restore(List<T> target);
/**
* Create a new delta of the actual instance with customized chunk data.
@@ -89,9 +98,6 @@ public abstract class AbstractDelta<T> {
if (!Objects.equals(this.target, other.target)) {
return false;
}
if (this.type != other.type) {
return false;
}
return true;
return this.type == other.type;
}
}

View File

@@ -39,8 +39,7 @@ public final class ChangeDelta<T> extends AbstractDelta<T> {
}
@Override
public void applyTo(List<T> target) throws PatchFailedException {
verifyChunk(target);
protected void applyTo(List<T> target) throws PatchFailedException {
int position = getSource().getPosition();
int size = getSource().size();
for (int i = 0; i < size; i++) {
@@ -54,7 +53,7 @@ public final class ChangeDelta<T> extends AbstractDelta<T> {
}
@Override
public void restore(List<T> target) {
protected void restore(List<T> target) {
int position = getTarget().getPosition();
int size = getTarget().size();
for (int i = 0; i < size; i++) {
@@ -75,6 +74,6 @@ public final class ChangeDelta<T> extends AbstractDelta<T> {
@Override
public AbstractDelta<T> withChunks(Chunk<T> original, Chunk<T> revised) {
return new ChangeDelta(original, revised);
return new ChangeDelta<T>(original, revised);
}
}

View File

@@ -15,6 +15,7 @@
*/
package com.github.difflib.patch;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -24,16 +25,17 @@ import java.util.Objects;
* Holds the information about the part of text involved in the diff process
*
* <p>
* Text is represented as <code>Object[]</code> because the diff engine is capable of handling more
* than plain ascci. In fact, arrays or lists of any type that implements
* {@link java.lang.Object#hashCode hashCode()} and {@link java.lang.Object#equals equals()}
* correctly can be subject to differencing using this library.
* Text is represented as <code>Object[]</code> because the diff engine is
* capable of handling more than plain ascci. In fact, arrays or lists of any
* type that implements {@link java.lang.Object#hashCode hashCode()} and
* {@link java.lang.Object#equals equals()} correctly can be subject to
* differencing using this library.
* </p>
*
* @author <a href="dm.naumenko@gmail.com>Dmitry Naumenko</a>
* @param <T> The type of the compared elements in the 'lines'.
*/
public final class Chunk<T> {
public final class Chunk<T> implements Serializable {
private final int position;
private List<T> lines;
@@ -49,7 +51,7 @@ public final class Chunk<T> {
public Chunk(int position, List<T> lines, List<Integer> changePosition) {
this.position = position;
this.lines = new ArrayList<>(lines);
this.changePosition = changePosition;
this.changePosition = changePosition != null ? new ArrayList<>(changePosition) : null;
}
/**
@@ -72,7 +74,7 @@ public final class Chunk<T> {
public Chunk(int position, T[] lines, List<Integer> changePosition) {
this.position = position;
this.lines = Arrays.asList(lines);
this.changePosition = changePosition;
this.changePosition = changePosition != null ? new ArrayList<>(changePosition) : null;
}
/**
@@ -86,21 +88,22 @@ public final class Chunk<T> {
}
/**
* Verifies that this chunk's saved text matches the corresponding text in the given sequence.
* Verifies that this chunk's saved text matches the corresponding text in
* the given sequence.
*
* @param target the sequence to verify against.
* @throws com.github.difflib.patch.PatchFailedException
*/
public void verify(List<T> target) throws PatchFailedException {
public VerifyChunk verifyChunk(List<T> target) throws PatchFailedException {
if (position > target.size() || last() > target.size()) {
throw new PatchFailedException("Incorrect Chunk: the position of chunk > target size");
return VerifyChunk.POSITION_OUT_OF_TARGET;
}
for (int i = 0; i < size(); i++) {
if (!target.get(position + i).equals(lines.get(i))) {
throw new PatchFailedException(
"Incorrect Chunk: the chunk content doesn't match the target");
return VerifyChunk.CONTENT_DOES_NOT_MATCH_TARGET;
}
}
return VerifyChunk.OK;
}
/**
@@ -155,7 +158,7 @@ public final class Chunk<T> {
if (getClass() != obj.getClass()) {
return false;
}
Chunk<T> other = (Chunk) obj;
Chunk<?> other = (Chunk<?>) obj;
if (lines == null) {
if (other.lines != null) {
return false;

View File

@@ -0,0 +1,33 @@
/*-
* #%L
* java-diff-utils
* %%
* Copyright (C) 2009 - 2017 java-diff-utils
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* #L%
*/
package com.github.difflib.patch;
import java.io.Serializable;
import java.util.List;
/**
*
* @author tw
*/
@FunctionalInterface
public interface ConflictOutput<T> extends Serializable {
public void processConflict(VerifyChunk verifyChunk, AbstractDelta<T> delta, List<T> result) throws PatchFailedException;
}

View File

@@ -36,8 +36,7 @@ public final class DeleteDelta<T> extends AbstractDelta<T> {
}
@Override
public void applyTo(List<T> target) throws PatchFailedException {
verifyChunk(target);
protected void applyTo(List<T> target) throws PatchFailedException {
int position = getSource().getPosition();
int size = getSource().size();
for (int i = 0; i < size; i++) {
@@ -46,7 +45,7 @@ public final class DeleteDelta<T> extends AbstractDelta<T> {
}
@Override
public void restore(List<T> target) {
protected void restore(List<T> target) {
int position = this.getTarget().getPosition();
List<T> lines = this.getSource().getLines();
for (int i = 0; i < lines.size(); i++) {
@@ -62,6 +61,6 @@ public final class DeleteDelta<T> extends AbstractDelta<T> {
@Override
public AbstractDelta<T> withChunks(Chunk<T> original, Chunk<T> revised) {
return new DeleteDelta(original, revised);
return new DeleteDelta<T>(original, revised);
}
}

View File

@@ -28,12 +28,11 @@ public class EqualDelta<T> extends AbstractDelta<T> {
}
@Override
public void applyTo(List<T> target) throws PatchFailedException {
verifyChunk(target);
protected void applyTo(List<T> target) throws PatchFailedException {
}
@Override
public void restore(List<T> target) {
protected void restore(List<T> target) {
}
@Override
@@ -44,6 +43,6 @@ public class EqualDelta<T> extends AbstractDelta<T> {
@Override
public AbstractDelta<T> withChunks(Chunk<T> original, Chunk<T> revised) {
return new EqualDelta(original, revised);
return new EqualDelta<T>(original, revised);
}
}

View File

@@ -36,8 +36,7 @@ public final class InsertDelta<T> extends AbstractDelta<T> {
}
@Override
public void applyTo(List<T> target) throws PatchFailedException {
verifyChunk(target);
protected void applyTo(List<T> target) throws PatchFailedException {
int position = this.getSource().getPosition();
List<T> lines = this.getTarget().getLines();
for (int i = 0; i < lines.size(); i++) {
@@ -46,7 +45,7 @@ public final class InsertDelta<T> extends AbstractDelta<T> {
}
@Override
public void restore(List<T> target) {
protected void restore(List<T> target) {
int position = getTarget().getPosition();
int size = getTarget().size();
for (int i = 0; i < size; i++) {
@@ -62,6 +61,6 @@ public final class InsertDelta<T> extends AbstractDelta<T> {
@Override
public AbstractDelta<T> withChunks(Chunk<T> original, Chunk<T> revised) {
return new InsertDelta(original, revised);
return new InsertDelta<T>(original, revised);
}
}

View File

@@ -21,18 +21,21 @@ package com.github.difflib.patch;
import static java.util.Comparator.comparing;
import com.github.difflib.algorithm.Change;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
/**
* Describes the patch holding all deltas between the original and revised texts.
* Describes the patch holding all deltas between the original and revised
* texts.
*
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
* @param <T> The type of the compared elements in the 'lines'.
*/
public final class Patch<T> {
public final class Patch<T> implements Serializable {
private final List<AbstractDelta<T>> deltas;
@@ -55,11 +58,56 @@ public final class Patch<T> {
ListIterator<AbstractDelta<T>> it = getDeltas().listIterator(deltas.size());
while (it.hasPrevious()) {
AbstractDelta<T> delta = it.previous();
delta.applyTo(result);
VerifyChunk valid = delta.verifyAntApplyTo(result);
if (valid != VerifyChunk.OK) {
conflictOutput.processConflict(valid, delta, result);
}
}
return result;
}
/**
* Standard Patch behaviour to throw an exception for pathching conflicts.
*/
public final ConflictOutput<T> CONFLICT_PRODUCES_EXCEPTION = (VerifyChunk verifyChunk, AbstractDelta<T> delta, List<T> result) -> {
throw new PatchFailedException("could not apply patch due to " + verifyChunk.toString());
};
/**
* Git like merge conflict output.
*/
public static final ConflictOutput<String> CONFLICT_PRODUCES_MERGE_CONFLICT = (VerifyChunk verifyChunk, AbstractDelta<String> delta, List<String> result) -> {
if (result.size() > delta.getSource().getPosition()) {
List<String> orgData = new ArrayList<>();
for (int i = 0; i < delta.getSource().size(); i++) {
orgData.add(result.get(delta.getSource().getPosition()));
result.remove(delta.getSource().getPosition());
}
orgData.add(0, "<<<<<< HEAD");
orgData.add("======");
orgData.addAll(delta.getSource().getLines());
orgData.add(">>>>>>> PATCH");
result.addAll(delta.getSource().getPosition(), orgData);
} else {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
};
private ConflictOutput<T> conflictOutput = CONFLICT_PRODUCES_EXCEPTION;
/**
* Alter normal conflict output behaviour to e.g. inclide some conflict
* statements in the result, like git does it.
*/
public Patch withConflictOutput(ConflictOutput<T> conflictOutput) {
this.conflictOutput = conflictOutput;
return this;
}
/**
* Restore the text to original. Opposite to applyTo() method.
*
@@ -112,18 +160,18 @@ public final class Patch<T> {
Patch<T> patch = new Patch<>(_changes.size());
int startOriginal = 0;
int startRevised = 0;
List<Change> changes = _changes;
if (includeEquals) {
changes = new ArrayList<Change>(_changes);
Collections.sort(changes, comparing(d -> d.startOriginal));
}
for (Change change : changes) {
if (includeEquals && startOriginal < change.startOriginal) {
patch.addDelta(new EqualDelta(
patch.addDelta(new EqualDelta<T>(
buildChunk(startOriginal, change.startOriginal, original),
buildChunk(startRevised, change.startRevised, revised)));
}
@@ -140,6 +188,7 @@ public final class Patch<T> {
case CHANGE:
patch.addDelta(new ChangeDelta<>(orgChunk, revChunk));
break;
default:
}
startOriginal = change.endOriginal;
@@ -147,7 +196,7 @@ public final class Patch<T> {
}
if (includeEquals && startOriginal < original.size()) {
patch.addDelta(new EqualDelta(
patch.addDelta(new EqualDelta<T>(
buildChunk(startOriginal, original.size(), original),
buildChunk(startRevised, revised.size(), revised)));
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2021 java-diff-utils.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.difflib.patch;
/**
*
* @author tw
*/
public enum VerifyChunk {
OK,
POSITION_OUT_OF_TARGET,
CONTENT_DOES_NOT_MATCH_TARGET
}

View File

@@ -17,8 +17,11 @@ package com.github.difflib.text;
import com.github.difflib.DiffUtils;
import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.patch.ChangeDelta;
import com.github.difflib.patch.Chunk;
import com.github.difflib.patch.DeleteDelta;
import com.github.difflib.patch.DeltaType;
import com.github.difflib.patch.InsertDelta;
import com.github.difflib.patch.Patch;
import com.github.difflib.text.DiffRow.Tag;
import java.util.*;
@@ -30,14 +33,16 @@ import java.util.regex.Pattern;
import static java.util.stream.Collectors.toList;
/**
* This class for generating DiffRows for side-by-sidy view. You can customize the way of
* generating. For example, show inline diffs on not, ignoring white spaces or/and blank lines and
* so on. All parameters for generating are optional. If you do not specify them, the class will use
* the default values.
* This class for generating DiffRows for side-by-sidy view. You can customize
* the way of generating. For example, show inline diffs on not, ignoring white
* spaces or/and blank lines and so on. All parameters for generating are
* optional. If you do not specify them, the class will use the default values.
*
* These values are: showInlineDiffs = false; ignoreWhiteSpaces = true; ignoreBlankLines = true; ...
* These values are: showInlineDiffs = false; ignoreWhiteSpaces = true;
* ignoreBlankLines = true; ...
*
* For instantiating the DiffRowGenerator you should use the its builder. Like in example <code>
* For instantiating the DiffRowGenerator you should use the its builder. Like
* in example <code>
* DiffRowGenerator generator = new DiffRowGenerator.Builder().showInlineDiffs(true).
* ignoreWhiteSpaces(true).columnWidth(100).build();
* </code>
@@ -100,8 +105,8 @@ public final class DiffRowGenerator {
/**
* Wrap the elements in the sequence with the given tag
*
* @param startPosition the position from which tag should start. The counting start from a
* zero.
* @param startPosition the position from which tag should start. The
* counting start from a zero.
* @param endPosition the position before which tag should should be closed.
* @param tagGenerator the tag generator
*/
@@ -187,7 +192,7 @@ public final class DiffRowGenerator {
reportLinesUnchanged = builder.reportLinesUnchanged;
lineNormalizer = builder.lineNormalizer;
processDiffs = builder.processDiffs;
replaceOriginalLinefeedInChangesWithSpaces = builder.replaceOriginalLinefeedInChangesWithSpaces;
Objects.requireNonNull(inlineDiffSplitter);
@@ -195,8 +200,8 @@ public final class DiffRowGenerator {
}
/**
* Get the DiffRows describing the difference between original and revised texts using the given
* patch. Useful for displaying side-by-side diff.
* Get the DiffRows describing the difference between original and revised
* texts using the given patch. Useful for displaying side-by-side diff.
*
* @param original the original text
* @param revised the revised text
@@ -207,8 +212,9 @@ public final class DiffRowGenerator {
}
/**
* Generates the DiffRows describing the difference between original and revised texts using the
* given patch. Useful for displaying side-by-side diff.
* Generates the DiffRows describing the difference between original and
* revised texts using the given patch. Useful for displaying side-by-side
* diff.
*
* @param original the original text
* @param patch the given patch
@@ -218,42 +224,11 @@ public final class DiffRowGenerator {
List<DiffRow> diffRows = new ArrayList<>();
int endPos = 0;
final List<AbstractDelta<String>> deltaList = patch.getDeltas();
for (AbstractDelta<String> delta : deltaList) {
Chunk<String> orig = delta.getSource();
Chunk<String> rev = delta.getTarget();
for (String line : original.subList(endPos, orig.getPosition())) {
diffRows.add(buildDiffRow(Tag.EQUAL, line, line));
for (AbstractDelta<String> originalDelta : deltaList) {
for (AbstractDelta<String> delta : decompressDeltas(originalDelta)) {
endPos = transformDeltaIntoDiffRow(original, endPos, diffRows, delta);
}
// Inserted DiffRow
if (delta.getType() == DeltaType.INSERT) {
endPos = orig.last() + 1;
for (String line : rev.getLines()) {
diffRows.add(buildDiffRow(Tag.INSERT, "", line));
}
continue;
}
// Deleted DiffRow
if (delta.getType() == DeltaType.DELETE) {
endPos = orig.last() + 1;
for (String line : orig.getLines()) {
diffRows.add(buildDiffRow(Tag.DELETE, line, ""));
}
continue;
}
if (showInlineDiffs) {
diffRows.addAll(generateInlineDiffs(delta));
} else {
for (int j = 0; j < Math.max(orig.size(), rev.size()); j++) {
diffRows.add(buildDiffRow(Tag.CHANGE,
orig.getLines().size() > j ? orig.getLines().get(j) : "",
rev.getLines().size() > j ? rev.getLines().get(j) : ""));
}
}
endPos = orig.last() + 1;
}
// Copy the final matching chunk if any.
@@ -263,6 +238,78 @@ public final class DiffRowGenerator {
return diffRows;
}
/**
* Transforms one patch delta into a DiffRow object.
*/
private int transformDeltaIntoDiffRow(final List<String> original, int endPos, List<DiffRow> diffRows, AbstractDelta<String> delta) {
Chunk<String> orig = delta.getSource();
Chunk<String> rev = delta.getTarget();
for (String line : original.subList(endPos, orig.getPosition())) {
diffRows.add(buildDiffRow(Tag.EQUAL, line, line));
}
switch (delta.getType()) {
case INSERT:
for (String line : rev.getLines()) {
diffRows.add(buildDiffRow(Tag.INSERT, "", line));
}
break;
case DELETE:
for (String line : orig.getLines()) {
diffRows.add(buildDiffRow(Tag.DELETE, line, ""));
}
break;
default:
if (showInlineDiffs) {
diffRows.addAll(generateInlineDiffs(delta));
} else {
for (int j = 0; j < Math.max(orig.size(), rev.size()); j++) {
diffRows.add(buildDiffRow(Tag.CHANGE,
orig.getLines().size() > j ? orig.getLines().get(j) : "",
rev.getLines().size() > j ? rev.getLines().get(j) : ""));
}
}
}
return orig.last() + 1;
}
/**
* Decompresses ChangeDeltas with different source and target size to a
* ChangeDelta with same size and a following InsertDelta or DeleteDelta.
* With this problems of building DiffRows getting smaller.
*
* @param deltaList
*/
private List<AbstractDelta<String>> decompressDeltas(AbstractDelta<String> delta) {
if (delta.getType() == DeltaType.CHANGE && delta.getSource().size() != delta.getTarget().size()) {
List<AbstractDelta<String>> deltas = new ArrayList<>();
//System.out.println("decompress this " + delta);
int minSize = Math.min(delta.getSource().size(), delta.getTarget().size());
Chunk<String> orig = delta.getSource();
Chunk<String> rev = delta.getTarget();
deltas.add(new ChangeDelta<String>(
new Chunk<>(orig.getPosition(), orig.getLines().subList(0, minSize)),
new Chunk<>(rev.getPosition(), rev.getLines().subList(0, minSize))));
if (orig.getLines().size() < rev.getLines().size()) {
deltas.add(new InsertDelta<String>(
new Chunk<>(orig.getPosition() + minSize, Collections.emptyList()),
new Chunk<>(rev.getPosition() + minSize, rev.getLines().subList(minSize, rev.getLines().size()))));
} else {
deltas.add(new DeleteDelta<String>(
new Chunk<>(orig.getPosition() + minSize, orig.getLines().subList(minSize, orig.getLines().size())),
new Chunk<>(rev.getPosition() + minSize, Collections.emptyList())));
}
return deltas;
}
return Collections.singletonList(delta);
}
private DiffRow buildDiffRow(Tag type, String orgline, String newline) {
if (reportLinesUnchanged) {
return new DiffRow(type, orgline, newline);
@@ -436,8 +483,8 @@ public final class DiffRowGenerator {
}
/**
* Give the originial old and new text lines to Diffrow without any additional processing
* and without any tags to highlight the change.
* Give the originial old and new text lines to Diffrow without any
* additional processing and without any tags to highlight the change.
*
* @param val the value to set. Default: false.
* @return builder with configured reportLinesUnWrapped parameter
@@ -492,8 +539,8 @@ public final class DiffRowGenerator {
}
/**
* Processor for diffed text parts. Here e.g. whitecharacters could be replaced by something
* visible.
* Processor for diffed text parts. Here e.g. whitecharacters could be
* replaced by something visible.
*
* @param processDiffs
* @return
@@ -504,10 +551,11 @@ public final class DiffRowGenerator {
}
/**
* Set the column width of generated lines of original and revised texts.
* Set the column width of generated lines of original and revised
* texts.
*
* @param width the width to set. Making it < 0 doesn't make any sense. Default 80.
* @return builder with config of column width
* @param width the width to set. Making it < 0 doesn't make any sense.
* Default 80. @return builder with config of column width
*/
public Builder columnWidth(int width) {
if (width >= 0) {
@@ -517,7 +565,8 @@ public final class DiffRowGenerator {
}
/**
* Build the DiffRowGenerator. If some parameters is not set, the default values are used.
* Build the DiffRowGenerator. If some parameters is not set, the
* default values are used.
*
* @return the customized DiffRowGenerator
*/
@@ -526,8 +575,8 @@ public final class DiffRowGenerator {
}
/**
* Merge the complete result within the original text. This makes sense for one line
* display.
* Merge the complete result within the original text. This makes sense
* for one line display.
*
* @param mergeOriginalRevised
* @return
@@ -538,9 +587,9 @@ public final class DiffRowGenerator {
}
/**
* Per default each character is separatly processed. This variant introduces processing by
* word, which does not deliver in word changes. Therefore the whole word will be tagged as
* changed:
* Per default each character is separatly processed. This variant
* introduces processing by word, which does not deliver in word
* changes. Therefore the whole word will be tagged as changed:
*
* <pre>
* false: (aBa : aba) -- changed: a(B)a : a(b)a
@@ -553,8 +602,9 @@ public final class DiffRowGenerator {
}
/**
* To provide some customized splitting a splitter can be provided. Here someone could think
* about sentence splitter, comma splitter or stuff like that.
* To provide some customized splitting a splitter can be provided. Here
* someone could think about sentence splitter, comma splitter or stuff
* like that.
*
* @param inlineDiffSplitter
* @return
@@ -565,9 +615,10 @@ public final class DiffRowGenerator {
}
/**
* By default DiffRowGenerator preprocesses lines for HTML output. Tabs and special HTML
* characters like "&lt;" are replaced with its encoded value. To change this you can
* provide a customized line normalizer here.
* By default DiffRowGenerator preprocesses lines for HTML output. Tabs
* and special HTML characters like "&lt;" are replaced with its encoded
* value. To change this you can provide a customized line normalizer
* here.
*
* @param lineNormalizer
* @return
@@ -587,13 +638,14 @@ public final class DiffRowGenerator {
this.equalizer = equalizer;
return this;
}
/**
* Sometimes it happens that a change contains multiple lines. If there is no correspondence
* in old and new. To keep the merged line more readable the linefeeds could be replaced
* by spaces.
* Sometimes it happens that a change contains multiple lines. If there
* is no correspondence in old and new. To keep the merged line more
* readable the linefeeds could be replaced by spaces.
*
* @param replace
* @return
* @return
*/
public Builder replaceOriginalLinefeedInChangesWithSpaces(boolean replace) {
this.replaceOriginalLinefeedInChangesWithSpaces = replace;

View File

@@ -21,10 +21,10 @@ import static java.util.stream.Collectors.toList;
final class StringUtils {
/**
* Replaces all opening an closing tags with <code>&lt;</code> or <code>&gt;</code>.
* Replaces all opening and closing tags with <code>&lt;</code> or <code>&gt;</code>.
*
* @param str
* @return
* @return str with some HTML meta characters escaped.
*/
public static String htmlEntites(String str) {
return str.replace("<", "&lt;").replace(">", "&gt;");
@@ -61,7 +61,17 @@ final class StringUtils {
StringBuilder b = new StringBuilder(line);
for (int count = 0; length > widthIndex; count++) {
b.insert(widthIndex + delimiter * count, "<br/>");
int breakPoint = widthIndex + delimiter * count;
if (Character.isHighSurrogate(b.charAt(breakPoint - 1)) &&
Character.isLowSurrogate(b.charAt(breakPoint))) {
// Shift a breakpoint that would split a supplemental code-point.
breakPoint += 1;
if (breakPoint == b.length()) {
// Break before instead of after if this is the last code-point.
breakPoint -= 2;
}
}
b.insert(breakPoint, "<br/>");
widthIndex += columnWidth;
}

View File

@@ -18,7 +18,8 @@ package com.github.difflib.unifieddiff;
import com.github.difflib.patch.Patch;
/**
*
* Data structure for one patched file from a unified diff file.
*
* @author Tobias Warneke (t.warneke@gmx.net)
*/
public final class UnifiedDiffFile {
@@ -27,11 +28,15 @@ public final class UnifiedDiffFile {
private String fromFile;
private String fromTimestamp;
private String toFile;
private String renameFrom;
private String renameTo;
private String toTimestamp;
private String index;
private String newFileMode;
private String deletedFileMode;
private Patch<String> patch = new Patch<>();
private boolean noNewLineAtTheEndOfTheFile = false;
private Integer similarityIndex;
public String getDiffCommand() {
return diffCommand;
@@ -84,8 +89,30 @@ public final class UnifiedDiffFile {
public void setToTimestamp(String toTimestamp) {
this.toTimestamp = toTimestamp;
}
public Integer getSimilarityIndex() {
return similarityIndex;
}
public void setSimilarityIndex(Integer similarityIndex) {
this.similarityIndex = similarityIndex;
}
public String getRenameFrom() {
return renameFrom;
}
public void setRenameFrom(String renameFrom) {
this.renameFrom = renameFrom;
}
public String getRenameTo() {
return renameTo;
}
public void setRenameTo(String renameTo) {
this.renameTo = renameTo;
}
public static UnifiedDiffFile from(String fromFile, String toFile, Patch<String> patch) {
UnifiedDiffFile file = new UnifiedDiffFile();
@@ -110,4 +137,12 @@ public final class UnifiedDiffFile {
public void setDeletedFileMode(String deletedFileMode) {
this.deletedFileMode = deletedFileMode;
}
public boolean isNoNewLineAtTheEndOfTheFile() {
return noNewLineAtTheEndOfTheFile;
}
public void setNoNewLineAtTheEndOfTheFile(boolean noNewLineAtTheEndOfTheFile) {
this.noNewLineAtTheEndOfTheFile = noNewLineAtTheEndOfTheFile;
}
}

View File

@@ -45,12 +45,15 @@ public final class UnifiedDiffReader {
private final UnifiedDiff data = new UnifiedDiff();
private final UnifiedDiffLine DIFF_COMMAND = new UnifiedDiffLine(true, "^diff\\s", this::processDiff);
private final UnifiedDiffLine SIMILARITY_INDEX = new UnifiedDiffLine(true, "^similarity index (\\d+)%$", this::processSimilarityIndex);
private final UnifiedDiffLine INDEX = new UnifiedDiffLine(true, "^index\\s[\\da-zA-Z]+\\.\\.[\\da-zA-Z]+(\\s(\\d+))?$", this::processIndex);
private final UnifiedDiffLine FROM_FILE = new UnifiedDiffLine(true, "^---\\s", this::processFromFile);
private final UnifiedDiffLine TO_FILE = new UnifiedDiffLine(true, "^\\+\\+\\+\\s", this::processToFile);
private final UnifiedDiffLine RENAME_FROM = new UnifiedDiffLine(true, "^rename\\sfrom\\s(.+)$", this::processRenameFrom);
private final UnifiedDiffLine RENAME_TO = new UnifiedDiffLine(true, "^rename\\sto\\s(.+)$", this::processRenameTo);
private final UnifiedDiffLine NEW_FILE_MODE = new UnifiedDiffLine(true, "^new\\sfile\\smode\\s(\\d+)", this::processNewFileMode);
private final UnifiedDiffLine DELETED_FILE_MODE = new UnifiedDiffLine(true, "^deleted\\sfile\\smode\\s(\\d+)", this::processDeletedFileMode);
private final UnifiedDiffLine CHUNK = new UnifiedDiffLine(false, UNIFIED_DIFF_CHUNK_REGEXP, this::processChunk);
@@ -91,7 +94,10 @@ public final class UnifiedDiffReader {
if (!CHUNK.validLine(line)) {
initFileIfNecessary();
while (line != null && !CHUNK.validLine(line)) {
if (processLine(line, DIFF_COMMAND, INDEX, FROM_FILE, TO_FILE, NEW_FILE_MODE, DELETED_FILE_MODE) == false) {
if (!processLine(line, DIFF_COMMAND, SIMILARITY_INDEX, INDEX,
FROM_FILE, TO_FILE,
RENAME_FROM, RENAME_TO,
NEW_FILE_MODE, DELETED_FILE_MODE)) {
throw new UnifiedDiffParserException("expected file start line not found");
}
line = READER.readLine();
@@ -100,7 +106,7 @@ public final class UnifiedDiffReader {
if (line != null) {
processLine(line, CHUNK);
while ((line = READER.readLine()) != null) {
if (processLine(line, LINE_NORMAL, LINE_ADD, LINE_DEL) == false) {
if (!processLine(line, LINE_NORMAL, LINE_ADD, LINE_DEL)) {
throw new UnifiedDiffParserException("expected data line not found");
}
if ((originalTxt.size() == old_size && revisedTxt.size() == new_size)
@@ -111,8 +117,13 @@ public final class UnifiedDiffReader {
}
}
line = READER.readLine();
if ("\\ No newline at end of file".equals(line)) {
actualFile.setNoNewLineAtTheEndOfTheFile(true);
line = READER.readLine();
}
}
if (line == null || line.startsWith("--")) {
if (line == null || (line.startsWith("--") && !line.startsWith("---"))) {
break;
}
}
@@ -141,6 +152,14 @@ public final class UnifiedDiffReader {
private static final Logger LOG = Logger.getLogger(UnifiedDiffReader.class.getName());
/**
* To parse a diff file use this method.
*
* @param stream This is the diff file data.
* @return In a UnifiedDiff structure this diff file data is returned.
* @throws IOException
* @throws UnifiedDiffParserException
*/
public static UnifiedDiff parseUnifiedDiff(InputStream stream) throws IOException, UnifiedDiffParserException {
UnifiedDiffReader parser = new UnifiedDiffReader(new BufferedReader(new InputStreamReader(stream)));
return parser.parse();
@@ -181,22 +200,34 @@ public final class UnifiedDiffReader {
actualFile.setDiffCommand(line);
}
private void processSimilarityIndex(MatchResult match, String line) {
actualFile.setSimilarityIndex(Integer.valueOf(match.group(1)));
}
private List<String> originalTxt = new ArrayList<>();
private List<String> revisedTxt = new ArrayList<>();
private List<Integer> addLineIdxList = new ArrayList<>();
private List<Integer> delLineIdxList = new ArrayList<>();
private int old_ln;
private int old_size;
private int new_ln;
private int new_size;
private int delLineIdx = 0;
private int addLineIdx = 0;
private void finalizeChunk() {
if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) {
actualFile.getPatch().addDelta(new ChangeDelta<>(new Chunk<>(
old_ln - 1, originalTxt), new Chunk<>(
new_ln - 1, revisedTxt)));
old_ln - 1, originalTxt, delLineIdxList), new Chunk<>(
new_ln - 1, revisedTxt, addLineIdxList)));
old_ln = 0;
new_ln = 0;
originalTxt.clear();
revisedTxt.clear();
addLineIdxList.clear();
delLineIdxList.clear();
delLineIdx = 0;
addLineIdx = 0;
}
}
@@ -204,24 +235,30 @@ public final class UnifiedDiffReader {
String cline = line.substring(1);
originalTxt.add(cline);
revisedTxt.add(cline);
delLineIdx++;
addLineIdx++;
}
private void processAddLine(MatchResult match, String line) {
String cline = line.substring(1);
revisedTxt.add(cline);
addLineIdx++;
addLineIdxList.add(new_ln - 1 + addLineIdx);
}
private void processDelLine(MatchResult match, String line) {
String cline = line.substring(1);
originalTxt.add(cline);
delLineIdx++;
delLineIdxList.add(old_ln - 1 + delLineIdx);
}
private void processChunk(MatchResult match, String chunkStart) {
// finalizeChunk();
old_ln = toInteger(match, 1, 1);
old_size = toInteger(match, 2, 0);
old_size = toInteger(match, 2, 1);
new_ln = toInteger(match, 3, 1);
new_size = toInteger(match, 4, 0);
new_size = toInteger(match, 4, 1);
if (old_ln == 0) {
old_ln = 1;
}
@@ -252,11 +289,19 @@ public final class UnifiedDiffReader {
actualFile.setToTimestamp(extractTimestamp(line));
}
private void processRenameFrom(MatchResult match, String line) {
actualFile.setRenameFrom(match.group(1));
}
private void processRenameTo(MatchResult match, String line) {
actualFile.setRenameTo(match.group(1));
}
private void processNewFileMode(MatchResult match, String line) {
//initFileIfNecessary();
actualFile.setNewFileMode(match.group(1));
}
private void processDeletedFileMode(MatchResult match, String line) {
//initFileIfNecessary();
actualFile.setDeletedFileMode(match.group(1));

View File

@@ -193,7 +193,6 @@ public class UnifiedDiffWriter {
*
* @param delta - the Delta to output
* @return list of String lines of code.
* @author Bill James (tankerbay@gmail.com)
*/
private static void getDeltaText(Consumer<String> writer, AbstractDelta<String> delta) {
for (String line : delta.getSource().getLines()) {

View File

@@ -14,7 +14,12 @@
* limitations under the License.
*/
/**
* This is the first test version of a multifile diff parser. The Api is still subject
* of change.
* This is the new implementation of UnifiedDiff Tools. This version is multi file aware.
* <p/>
* To read a unified diff file you should use {@link UnifiedDiffReader#parseUnifiedDiff}.
* You will get a {@link UnifiedDiff} that holds all informations about the
* diffs and the files.
* <p/>
* To process the UnifiedDiff use {@link UnifiedDiffWriter#write}.
*/
package com.github.difflib.unifieddiff;

View File

@@ -112,7 +112,7 @@ public class DiffUtilsTest {
final Patch<Integer> patch = DiffUtils.diff(original, revised);
for (AbstractDelta delta : patch.getDeltas()) {
for (AbstractDelta<Integer> delta : patch.getDeltas()) {
System.out.println(delta);
}

View File

@@ -12,7 +12,9 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.joining;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
@@ -133,7 +135,7 @@ public class GenerateUnifiedDiffTest {
* Issue 89
*/
@Test
public void testChagngePosition() throws IOException {
public void testChangePosition() throws IOException {
final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue89_patch.txt");
final Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
List<Integer> realRemoveListOne = Collections.singletonList(3);
@@ -147,7 +149,7 @@ public class GenerateUnifiedDiffTest {
private void validateChangePosition(Patch<String> patch, int index, List<Integer> realRemoveList,
List<Integer> realAddList ) {
final Chunk originChunk = patch.getDeltas().get(index).getSource();
final Chunk<String> originChunk = patch.getDeltas().get(index).getSource();
List<Integer> removeList = originChunk.getChangePosition();
assertEquals(realRemoveList.size(), removeList.size());
for (Integer ele: realRemoveList) {
@@ -156,7 +158,7 @@ public class GenerateUnifiedDiffTest {
for (Integer ele: removeList) {
assertTrue(realAddList.contains(ele));
}
final Chunk targetChunk = patch.getDeltas().get(index).getTarget();
final Chunk<String> targetChunk = patch.getDeltas().get(index).getTarget();
List<Integer> addList = targetChunk.getChangePosition();
assertEquals(realAddList.size(), addList.size());
for (Integer ele: realAddList) {
@@ -191,4 +193,31 @@ public class GenerateUnifiedDiffTest {
fail(e.getMessage());
}
}
@Test
public void testFailingPatchByException() throws IOException {
final List<String> baseLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_base.txt");
final List<String> patchLines = fileToLines(TestConstants.MOCK_FOLDER + "issue10_patch.txt");
final Patch<String> p = UnifiedDiffUtils.parseUnifiedDiff(patchLines);
//make original not fitting
baseLines.set(40, baseLines.get(40) + " corrupted ");
assertThrows(PatchFailedException.class, () -> DiffUtils.patch(baseLines, p));
}
@Test
public void testWrongContextLength() throws IOException {
List<String> original = fileToLines(TestConstants.BASE_FOLDER_RESOURCES + "com/github/difflib/text/issue_119_original.txt");
List<String> revised = fileToLines(TestConstants.BASE_FOLDER_RESOURCES + "com/github/difflib/text/issue_119_revised.txt");
Patch<String> patch = DiffUtils.diff(original, revised);
List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff("a/$filename", "b/$filename",
original, patch, 3);
//System.out.println(udiff.stream().collect(joining("\n")));
assertThat(udiff).contains("@@ -1,4 +1,4 @@");
}
}

View File

@@ -1,12 +1,20 @@
package com.github.difflib.patch;
import com.github.difflib.DiffUtils;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import com.github.difflib.DiffUtils;
public class PatchTest {
@Test
@@ -47,4 +55,49 @@ public class PatchTest {
fail(e.getMessage());
}
}
@Test
public void testPatch_Serializable() throws IOException, ClassNotFoundException {
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);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(patch);
out.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
Patch<String> result = (Patch<String>) in.readObject();
in.close();
try {
assertEquals(changeTest_to, DiffUtils.patch(changeTest_from, result));
} catch (PatchFailedException e) {
fail(e.getMessage());
}
}
@Test
public void testPatch_Change_withExceptionProcessor() {
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);
changeTest_from.set(2, "CDC");
patch.withConflictOutput(Patch.CONFLICT_PRODUCES_MERGE_CONFLICT);
try {
List<String> data = DiffUtils.patch(changeTest_from, patch);
assertEquals(9, data.size());
assertEquals(Arrays.asList("aaa", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data);
} catch (PatchFailedException e) {
fail(e.getMessage());
}
}
}

View File

@@ -163,7 +163,7 @@ public class DiffRowGeneratorTest {
assertEquals(6, rows.size());
assertEquals("[CHANGE,<span class=\"editOldInline\">test</span>,anything]", rows.get(0).toString());
assertEquals("[CHANGE,anything<span class=\"editOldInline\"> </span>,]", rows.get(1).toString());
assertEquals("[CHANGE,<span class=\"editOldInline\"> </span>,]", rows.get(2).toString());
assertEquals("[DELETE,<span class=\"editOldInline\"> </span>,]", rows.get(2).toString());
assertEquals("[EQUAL,other,other]", rows.get(3).toString());
assertEquals("[INSERT,<span class=\"editNewInline\">test</span>,test]", rows.get(4).toString());
assertEquals("[INSERT,<span class=\"editNewInline\">test2</span>,test2]", rows.get(5).toString());
@@ -343,7 +343,7 @@ public class DiffRowGeneratorTest {
Arrays.asList(aa.split("\n")),
Arrays.asList(bb.split("\n")));
assertEquals("[[CHANGE,This is a test ~senctence~.,This is a test **for diffutils**.], [CHANGE,,**This is the second line.**]]",
assertEquals("[[CHANGE,This is a test ~senctence~.,This is a test **for diffutils**.], [INSERT,,**This is the second line.**]]",
rows.toString());
System.out.println("|original|new|");
@@ -367,7 +367,7 @@ public class DiffRowGeneratorTest {
Arrays.asList(aa.split("\n")),
Arrays.asList(bb.split("\n")));
assertEquals("[[CHANGE,This is a test ~for diffutils~.,This is a test **senctence**.], [CHANGE,~This is the second line.~,]]",
assertEquals("[[CHANGE,This is a test ~for diffutils~.,This is a test **senctence**.], [DELETE,~This is the second line.~,]]",
rows.toString());
}
@@ -385,7 +385,7 @@ public class DiffRowGeneratorTest {
Arrays.asList(aa.split("\n")),
Arrays.asList(bb.split("\n")));
assertEquals("[[CHANGE,This is a test ~senctence~.,This is a test **for diffutils**.], [CHANGE,,**This is the second line.**], [CHANGE,,**And one more.**]]",
assertEquals("[[CHANGE,This is a test ~senctence~.,This is a test **for diffutils**.], [INSERT,,**This is the second line.**], [INSERT,,**And one more.**]]",
rows.toString());
}
@@ -600,25 +600,87 @@ public class DiffRowGeneratorTest {
System.out.println(deltas);
}
@Test
public void testIssue86WrongInlineDiff() throws IOException {
public void testIssue86WrongInlineDiff() throws IOException {
String original = Files.lines(Paths.get("target/test-classes/com/github/difflib/text/issue_86_original.txt")).collect(joining("\n"));
String revised = Files.lines(Paths.get("target/test-classes/com/github/difflib/text/issue_86_revised.txt")).collect(joining("\n"));
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.mergeOriginalRevised(false)
.inlineDiffByWord(true)
.oldTag( f -> "~" )
.newTag( f -> "**" )
.build();
.showInlineDiffs(true)
.mergeOriginalRevised(true)
.inlineDiffByWord(true)
.oldTag(f -> "~")
.newTag(f -> "**")
.build();
List<DiffRow> rows = generator.generateDiffRows(
Arrays.asList(original.split("\n")),
Arrays.asList(revised.split("\n")));
rows.stream()
.filter(item -> item.getTag() != DiffRow.Tag.EQUAL)
.forEach(System.out::println);
}
@Test
public void testCorrectChangeIssue114() throws IOException {
List<String> original = Arrays.asList("A", "B", "C", "D", "E");
List<String> revised = Arrays.asList("a", "C", "", "E");
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(false)
.inlineDiffByWord(true)
.oldTag(f -> "~")
.newTag(f -> "**")
.build();
List<DiffRow> rows = generator.generateDiffRows(original, revised);
for (DiffRow diff : rows) {
System.out.println(diff);
}
assertThat(rows).extracting(item -> item.getTag().name()).containsExactly("CHANGE", "DELETE", "EQUAL", "CHANGE", "EQUAL");
}
@Test
public void testCorrectChangeIssue114_2() throws IOException {
List<String> original = Arrays.asList("A", "B", "C", "D", "E");
List<String> revised = Arrays.asList("a", "C", "", "E");
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.oldTag(f -> "~")
.newTag(f -> "**")
.build();
List<DiffRow> rows = generator.generateDiffRows(original, revised);
for (DiffRow diff : rows) {
System.out.println(diff);
}
assertThat(rows).extracting(item -> item.getTag().name()).containsExactly("CHANGE", "DELETE", "EQUAL", "CHANGE", "EQUAL");
assertThat(rows.get(1).toString()).isEqualTo("[DELETE,~B~,]");
}
@Test
public void testIssue119WrongContextLength() throws IOException {
String original = Files.lines(Paths.get("target/test-classes/com/github/difflib/text/issue_119_original.txt")).collect(joining("\n"));
String revised = Files.lines(Paths.get("target/test-classes/com/github/difflib/text/issue_119_revised.txt")).collect(joining("\n"));
DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.mergeOriginalRevised(true)
.inlineDiffByWord(true)
.oldTag(f -> "~")
.newTag(f -> "**")
.build();
List<DiffRow> rows = generator.generateDiffRows(
Arrays.asList(original.split("\n")),
Arrays.asList(revised.split("\n")));
rows.stream()
.filter(item -> item.getTag() != DiffRow.Tag.EQUAL)
.forEach(System.out::println);
}
}

View File

@@ -49,6 +49,8 @@ public class StringUtilsTest {
assertEquals("te<br/>st", StringUtils.wrapText("test", 2));
assertEquals("tes<br/>t", StringUtils.wrapText("test", 3));
assertEquals("test", StringUtils.wrapText("test", 10));
assertEquals(".\uD800\uDC01<br/>.", StringUtils.wrapText(".\uD800\uDC01.", 2));
assertEquals("..<br/>\uD800\uDC01", StringUtils.wrapText("..\uD800\uDC01", 3));
}
@Test

View File

@@ -155,7 +155,7 @@ public class UnifiedDiffReaderTest {
UnifiedDiffFile file1 = diff.getFiles().get(0);
assertThat(file1.getFromFile()).isEqualTo("f1");
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(1);
UnifiedDiffFile file2 = diff.getFiles().get(1);
assertThat(file2.getFromFile()).isEqualTo("f2");
assertThat(file2.getPatch().getDeltas().size()).isEqualTo(1);
@@ -220,7 +220,7 @@ public class UnifiedDiffReaderTest {
public void testTimeStampRegexp() {
assertThat("2019-04-18 13:49:39.516149751 +0200").matches(UnifiedDiffReader.TIMESTAMP_REGEXP);
}
@Test
public void testParseIssue98() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
@@ -236,4 +236,100 @@ public class UnifiedDiffReaderTest {
assertEquals("src/test/java/se/bjurr/violations/lib/model/ViolationTest.java", file1.getFromFile());
assertThat(diff.getTail()).isEqualTo("2.25.1");
}
@Test
public void testParseIssue104() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
UnifiedDiffReaderTest.class.getResourceAsStream("problem_diff_parsing_issue104.diff"));
assertThat(diff.getFiles().size()).isEqualTo(6);
final UnifiedDiffFile file = diff.getFiles().get(2);
assertThat(file.getFromFile()).isEqualTo("/dev/null");
assertThat(file.getToFile()).isEqualTo("doc/samba_data_tool_path.xml.in");
assertThat(file.getPatch().toString()).isEqualTo("Patch{deltas=[[ChangeDelta, position: 0, lines: [] to [@SAMBA_DATA_TOOL@]]]}");
assertThat(diff.getTail()).isEqualTo("2.14.4");
}
@Test
public void testParseIssue107BazelDiff() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
UnifiedDiffReaderTest.class.getResourceAsStream("01-bazel-strip-unused.patch_issue107.diff"));
assertThat(diff.getFiles().size()).isEqualTo(450);
final UnifiedDiffFile file = diff.getFiles().get(0);
assertThat(file.getFromFile()).isEqualTo("./src/main/java/com/amazonaws/AbortedException.java");
assertThat(file.getToFile()).isEqualTo("/home/greg/projects/bazel/third_party/aws-sdk-auth-lite/src/main/java/com/amazonaws/AbortedException.java");
assertThat(diff.getFiles().stream()
.filter(f -> f.isNoNewLineAtTheEndOfTheFile())
.count())
.isEqualTo(48);
}
@Test
public void testParseIssue107_2() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
UnifiedDiffReaderTest.class.getResourceAsStream("problem_diff_issue107.diff"));
assertThat(diff.getFiles().size()).isEqualTo(2);
final UnifiedDiffFile file = diff.getFiles().get(0);
UnifiedDiffFile file1 = diff.getFiles().get(0);
assertThat(file1.getFromFile()).isEqualTo("Main.java");
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(1);
}
@Test
public void testParseIssue110() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
UnifiedDiffReaderTest.class.getResourceAsStream("0001-avahi-python-Use-the-agnostic-DBM-interface.patch"));
assertThat(diff.getFiles().size()).isEqualTo(5);
final UnifiedDiffFile file = diff.getFiles().get(4);
assertThat(file.getSimilarityIndex()).isEqualTo(87);
assertThat(file.getRenameFrom()).isEqualTo("service-type-database/build-db.in");
assertThat(file.getRenameTo()).isEqualTo("service-type-database/build-db");
assertThat(file.getFromFile()).isEqualTo("service-type-database/build-db.in");
assertThat(file.getToFile()).isEqualTo("service-type-database/build-db");
}
@Test
public void testParseIssue117() throws IOException {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(
UnifiedDiffReaderTest.class.getResourceAsStream("problem_diff_issue117.diff"));
assertThat(diff.getFiles().size()).isEqualTo(2);
assertThat(diff.getFiles().get(0).getPatch().getDeltas().get(0).getSource().getChangePosition())
.containsExactly(24, 27);
assertThat(diff.getFiles().get(0).getPatch().getDeltas().get(0).getTarget().getChangePosition())
.containsExactly(24, 27);
assertThat(diff.getFiles().get(0).getPatch().getDeltas().get(1).getSource().getChangePosition())
.containsExactly(64);
assertThat(diff.getFiles().get(0).getPatch().getDeltas().get(1).getTarget().getChangePosition())
.containsExactly(64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74);
// diff.getFiles().forEach(f -> {
// System.out.println("File: " + f.getFromFile());
// f.getPatch().getDeltas().forEach(delta -> {
//
// System.out.println(delta);
// System.out.println("Source: ");
// System.out.println(delta.getSource().getPosition());
// System.out.println(delta.getSource().getChangePosition());
//
// System.out.println("Target: ");
// System.out.println(delta.getTarget().getPosition());
// System.out.println(delta.getTarget().getChangePosition());
// });
// });
}
}

View File

@@ -46,7 +46,7 @@ public class UnifiedDiffWriterTest {
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(str.getBytes()));
StringWriter writer = new StringWriter();
UnifiedDiffWriter.write(diff, f -> Collections.EMPTY_LIST, writer, 5);
UnifiedDiffWriter.write(diff, f -> Collections.emptyList(), writer, 5);
System.out.println(writer.toString());
}

View File

@@ -0,0 +1,4 @@
const world: string = 'world',
p: number | undefined = 42;
console.log(`Hello, ${world}!`);

View File

@@ -0,0 +1,4 @@
const world: string = 'world';
const p: number | undefined = 42;
console.log(`Hello, ${world}!`);

View File

@@ -0,0 +1,231 @@
From be7992f35ab4ed7ed9907319b429dc079c2b7285 Mon Sep 17 00:00:00 2001
From: "Jan Alexander Steffens (heftig)" <jan.steffens@gmail.com>
Date: Tue, 11 Jul 2017 21:52:37 +0200
Subject: [PATCH] avahi-python: Use the agnostic DBM interface
Also fixes configure failing if Python 3 is the build python and GDBM is
enabled, since Py3 only has anydbm under the name of 'dbm'.
Not enough to make ServiceTypeDatabase.py compatible with Py3, but it's
a start.
(cherry picked from commit 63750f1be96ad08c407193b08bf3b9ee74310e2d)
Related: #1561019
---
avahi-python/avahi/Makefile.am | 15 +----------
avahi-python/avahi/ServiceTypeDatabase.py.in | 33 ++++++++++++++++++-------
configure.ac | 9 +++----
service-type-database/Makefile.am | 18 +++-----------
service-type-database/{build-db.in => build-db} | 13 +++++++---
5 files changed, 42 insertions(+), 46 deletions(-)
rename service-type-database/{build-db.in => build-db} (87%)
diff --git a/avahi-python/avahi/Makefile.am b/avahi-python/avahi/Makefile.am
index 3eb67d0..c906b9b 100644
--- a/avahi-python/avahi/Makefile.am
+++ b/avahi-python/avahi/Makefile.am
@@ -25,29 +25,16 @@ avahidir = $(pythondir)/avahi
if HAVE_GDBM
nodist_avahi_SCRIPTS = ServiceTypeDatabase.py
-
-ServiceTypeDatabase.py: ServiceTypeDatabase.py.in
- $(AM_V_GEN)sed -e 's,@PYTHON\@,$(PYTHON),g' \
- -e 's,@DBM\@,gdbm,g' \
- -e 's,@FIRST_KEY\@,key = self.db.firstkey(),g' \
- -e 's,@CHECK_KEY\@,while key is not None:,g' \
- -e 's,@NEXT_KEY\@,key = self.db.nextkey(key),g' \
- -e 's,@pkglibdatadir\@,$(pkglibdatadir),g' $< > $@ && \
- chmod +x $@
endif
if HAVE_DBM
nodist_avahi_SCRIPTS = ServiceTypeDatabase.py
+endif
ServiceTypeDatabase.py: ServiceTypeDatabase.py.in
$(AM_V_GEN)sed -e 's,@PYTHON\@,$(PYTHON),g' \
- -e 's,@DBM\@,dbm,g' \
- -e 's,@FIRST_KEY\@,keys = self.db.keys(),g' \
- -e 's,@CHECK_KEY\@,for key in keys:,g' \
- -e 's,@NEXT_KEY\@,,g' \
-e 's,@pkglibdatadir\@,$(pkglibdatadir),g' $< > $@ && \
chmod +x $@
-endif
avahi_PYTHON = $(avahi_SCRIPTS)
diff --git a/avahi-python/avahi/ServiceTypeDatabase.py.in b/avahi-python/avahi/ServiceTypeDatabase.py.in
index 4ddd654..d7f9969 100644
--- a/avahi-python/avahi/ServiceTypeDatabase.py.in
+++ b/avahi-python/avahi/ServiceTypeDatabase.py.in
@@ -17,7 +17,11 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
-import @DBM@
+try:
+ import anydbm as dbm
+except ImportError:
+ import dbm
+
import locale
import re
@@ -28,7 +32,7 @@ class ServiceTypeDatabase:
def __init__(self, filename = "@pkglibdatadir@/service-types.db"):
- self.db = @DBM@.open(filename, "r")
+ self.db = dbm.open(filename, "r")
l = locale.getlocale(locale.LC_MESSAGES)
@@ -90,13 +94,24 @@ class ServiceTypeDatabase:
def __iter__(self):
- @FIRST_KEY@
- @CHECK_KEY@
-
- if re.search('_[a-zA-Z0-9-]+\._[a-zA-Z0-9-]+', key) and not re.search('_[a-zA-Z0-9-]+\._[a-zA-Z0-9-]+\[.*\]', key):
- yield key
-
- @NEXT_KEY@
+ def want_key(key):
+ if not re.search('_[a-zA-Z0-9-]+\._[a-zA-Z0-9-]+', key):
+ return False
+ if re.search('_[a-zA-Z0-9-]+\._[a-zA-Z0-9-]+\[.*\]', key):
+ return False
+ return True
+
+ try:
+ key = self.db.firstkey()
+ except AttributeError:
+ for key in self.db.keys():
+ if want_key(key):
+ yield key
+ else:
+ while key is not None:
+ if want_key(key):
+ yield key
+ key = self.db.nextkey(key)
def __len__(self):
diff --git a/configure.ac b/configure.ac
index 6678971..fbbf7cf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -824,11 +824,10 @@ if test "x$HAVE_PYTHON" = "xyes" ; then
fi
AM_CHECK_PYMOD(socket,,,[AC_MSG_ERROR(Could not find Python module socket)])
- if test "x$HAVE_GDBM" = "xyes"; then
- AM_CHECK_PYMOD(gdbm,,,[AC_MSG_ERROR(Could not find Python module gdbm)])
- fi
- if test "x$HAVE_DBM" = "xyes"; then
- AM_CHECK_PYMOD(dbm,,,[AC_MSG_ERROR(Could not find Python module dbm)])
+ if test "x$HAVE_GDBM" = "xyes" || test "x$HAVE_DBM" = "xyes"; then
+ AM_CHECK_PYMOD(anydbm,,,[
+ AM_CHECK_PYMOD(dbm,,,[AC_MSG_ERROR(Could not find Python module dbm)])
+ ])
fi
fi
fi
diff --git a/service-type-database/Makefile.am b/service-type-database/Makefile.am
index d184fde..f9fa082 100644
--- a/service-type-database/Makefile.am
+++ b/service-type-database/Makefile.am
@@ -15,7 +15,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
-EXTRA_DIST=build-db.in service-types
+EXTRA_DIST=service-types
pkglibdatadir=$(libdir)/avahi
@@ -27,16 +27,11 @@ if HAVE_GDBM
noinst_SCRIPTS=build-db
pkglibdata_DATA+=service-types.db
-build-db: build-db.in
- $(AM_V_GEN)sed -e 's,@PYTHON\@,$(PYTHON),g' \
- -e 's,@DBM\@,gdbm,g' $< > $@ && \
- chmod +x $@
-
-service-types.db: service-types build-db
+service-types.db: service-types
$(AM_V_GEN)$(PYTHON) build-db $< $@.coming && \
mv $@.coming $@
-CLEANFILES = service-types.db build-db
+CLEANFILES = service-types.db
endif
if HAVE_DBM
@@ -44,11 +39,6 @@ if HAVE_DBM
noinst_SCRIPTS=build-db
pkglibdata_DATA+=service-types.db.pag service-types.db.dir
-build-db: build-db.in
- $(AM_V_GEN)sed -e 's,@PYTHON\@,$(PYTHON),g' \
- -e 's,@DBM\@,dbm,g' $< > $@ && \
- chmod +x $@
-
service-types.db.pag: service-types.db
$(AM_V_GEN)mv service-types.db.coming.pag service-types.db.pag
service-types.db.dir: service-types.db
@@ -57,7 +47,7 @@ service-types.db: service-types build-db
$(AM_V_GEN)$(PYTHON) build-db $< $@.coming && \
if test -f "$@.coming"; then mv $@.coming $@; fi
-CLEANFILES = service-types.db* build-db
+CLEANFILES = service-types.db*
endif
endif
diff --git a/service-type-database/build-db.in b/service-type-database/build-db
similarity index 87%
rename from service-type-database/build-db.in
rename to service-type-database/build-db
index 4cda425..78ee892 100755
--- a/service-type-database/build-db.in
+++ b/service-type-database/build-db
@@ -1,4 +1,4 @@
-#!@PYTHON@
+#!/usr/bin/env python
# -*-python-*-
# This file is part of avahi.
#
@@ -17,7 +17,12 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
-import @DBM@, sys
+try:
+ import anydbm as dbm
+except ImportError:
+ import dbm
+
+import sys
if len(sys.argv) > 1:
infn = sys.argv[1]
@@ -29,9 +34,9 @@ if len(sys.argv) > 2:
else:
outfn = infn + ".db"
-db = @DBM@.open(outfn, "n")
+db = dbm.open(outfn, "n")
-for ln in file(infn, "r"):
+for ln in open(infn, "r"):
ln = ln.strip(" \r\n\t")
if ln == "" or ln.startswith("#"):
--
2.14.3

View File

@@ -0,0 +1,13 @@
--- a/Main.java
+++ b/Main.java
@@ -2,2 +2,3 @@ public class Main {
public static void main(String[] args) {
+ System.out.println("Hello, world!");
}
\ No newline at end of file
--- a/Main2.java
+++ b/Main2.java
@@ -2,2 +2,3 @@ public class Main {
public static void main(String[] args) {
+ System.out.println("Hello, world!");
}

View File

@@ -0,0 +1,49 @@
diff --git a/java-diff-utils/src/main/java/com/github/difflib/text/StringUtils.java b/java-diff-utils/src/main/java/com/github/difflib/text/StringUtils.java
index a142548..b7e3549 100644
--- a/java-diff-utils/src/main/java/com/github/difflib/text/StringUtils.java
+++ b/java-diff-utils/src/main/java/com/github/difflib/text/StringUtils.java
@@ -21,10 +21,10 @@
final class StringUtils {
/**
- * Replaces all opening an closing tags with <code>&lt;</code> or <code>&gt;</code>.
+ * Replaces all opening and closing tags with <code>&lt;</code> or <code>&gt;</code>.
*
* @param str
- * @return
+ * @return str with some HTML meta characters escaped.
*/
public static String htmlEntites(String str) {
return str.replace("<", "&lt;").replace(">", "&gt;");
@@ -61,7 +61,17 @@ public static String wrapText(String line, int columnWidth) {
StringBuilder b = new StringBuilder(line);
for (int count = 0; length > widthIndex; count++) {
- b.insert(widthIndex + delimiter * count, "<br/>");
+ int breakPoint = widthIndex + delimiter * count;
+ if (Character.isHighSurrogate(b.charAt(breakPoint - 1)) &&
+ Character.isLowSurrogate(b.charAt(breakPoint))) {
+ // Shift a breakpoint that would split a supplemental code-point.
+ breakPoint += 1;
+ if (breakPoint == b.length()) {
+ // Break before instead of after if this is the last code-point.
+ breakPoint -= 2;
+ }
+ }
+ b.insert(breakPoint, "<br/>");
widthIndex += columnWidth;
}
diff --git a/java-diff-utils/src/test/java/com/github/difflib/text/StringUtilsTest.java b/java-diff-utils/src/test/java/com/github/difflib/text/StringUtilsTest.java
index c4b2acc..6867072 100644
--- a/java-diff-utils/src/test/java/com/github/difflib/text/StringUtilsTest.java
+++ b/java-diff-utils/src/test/java/com/github/difflib/text/StringUtilsTest.java
@@ -49,6 +49,8 @@ public void testWrapText_String_int() {
assertEquals("te<br/>st", StringUtils.wrapText("test", 2));
assertEquals("tes<br/>t", StringUtils.wrapText("test", 3));
assertEquals("test", StringUtils.wrapText("test", 10));
+ assertEquals(".\uD800\uDC01<br/>.", StringUtils.wrapText(".\uD800\uDC01.", 2));
+ assertEquals("..<br/>\uD800\uDC01", StringUtils.wrapText("..\uD800\uDC01", 3));
}
@Test

View File

@@ -0,0 +1,291 @@
From 9b73f79a2436760b8278377014bf78a144a427ae Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Thu, 1 Feb 2018 14:26:22 +0100
Subject: [PATCH 15/23] make Samba data tool configurable
Allow to specify an alternative path to Samba's net utility at configure
time and at run time.
https://bugs.freedesktop.org/show_bug.cgi?id=100118
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
---
configure.ac | 13 ++++++++++++
doc/adcli.xml | 21 ++++++++++++++++++-
doc/samba_data_tool_path.xml.in | 1 +
library/adenroll.c | 46 ++++++++++++++++++++++++++++++++++-------
library/adenroll.h | 5 +++++
tools/computer.c | 16 ++++++++++++++
7 files changed, 95 insertions(+), 8 deletions(-)
create mode 100644 doc/samba_data_tool_path.xml.in
diff --git a/configure.ac b/configure.ac
index fe86638..68877c7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -291,6 +291,18 @@ else
AC_DEFINE_UNQUOTED(BIN_ECHO, "$BIN_ECHO", [path to echo, used in unit test])
fi
+AC_MSG_CHECKING([where is Samba's net utility])
+AC_ARG_WITH([samba_data_tool],
+ AC_HELP_STRING([--with-samba-data-tool=/path],
+ [Path to Samba's net utility]),
+ [],
+ [with_samba_data_tool=/usr/bin/net])
+AC_MSG_RESULT([$with_samba_data_tool])
+
+AC_DEFINE_UNQUOTED(SAMBA_DATA_TOOL, "$with_samba_data_tool",
+ [Path to Samba's net utility])
+
+AC_SUBST(SAMBA_DATA_TOOL, [$with_samba_data_tool])
# ---------------------------------------------------------------------
ADCLI_LT_RELEASE=$ADCLI_CURRENT:$ADCLI_REVISION:$ADCLI_AGE
@@ -300,6 +312,7 @@ AC_CONFIG_FILES([Makefile
build/Makefile
doc/Makefile
doc/version.xml
+ doc/samba_data_tool_path.xml
library/Makefile
tools/Makefile
])
diff --git a/doc/adcli.xml b/doc/adcli.xml
index fbc6c63..c2b7760 100644
--- a/doc/adcli.xml
+++ b/doc/adcli.xml
@@ -1,6 +1,9 @@
<?xml version='1.0'?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
- "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
+ "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd"
+[
+ <!ENTITY samba_data_tool SYSTEM "samba_data_tool_path.xml">
+]>
<refentry id="adcli">
@@ -307,6 +310,14 @@ Password for Administrator:
<citerefentry><refentrytitle>smb.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>
for details.</para></listitem>
</varlistentry>
+ <varlistentry>
+ <term><option>--samba-data-tool=<parameter>/path/to/net</parameter></option></term>
+ <listitem><para>If Samba's <command>net</command>
+ cannot be found at
+ <filename>&samba_data_tool;</filename> this option can
+ be used to specific an alternative location with the
+ help of an absolute path.</para></listitem>
+ </varlistentry>
</variablelist>
</refsect1>
@@ -412,6 +423,14 @@ $ adcli update --login-ccache=/tmp/krbcc_123
<citerefentry><refentrytitle>smb.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>
for details.</para></listitem>
</varlistentry>
+ <varlistentry>
+ <term><option>--samba-data-tool=<parameter>/path/to/net</parameter></option></term>
+ <listitem><para>If Samba's <command>net</command>
+ cannot be found at
+ <filename>&samba_data_tool;</filename> this option can
+ be used to specific an alternative location with the
+ help of an absolute path.</para></listitem>
+ </varlistentry>
</variablelist>
</refsect1>
diff --git a/doc/samba_data_tool_path.xml.in b/doc/samba_data_tool_path.xml.in
new file mode 100644
index 0000000..a667c57
--- /dev/null
+++ b/doc/samba_data_tool_path.xml.in
@@ -0,0 +1 @@
+@SAMBA_DATA_TOOL@
diff --git a/library/adenroll.c b/library/adenroll.c
index 20731cd..a693049 100644
--- a/library/adenroll.c
+++ b/library/adenroll.c
@@ -42,6 +42,10 @@
#include <stdio.h>
#include <unistd.h>
+#ifndef SAMBA_DATA_TOOL
+#define SAMBA_DATA_TOOL "/usr/bin/net"
+#endif
+
static krb5_enctype v60_later_enctypes[] = {
ENCTYPE_AES256_CTS_HMAC_SHA1_96,
ENCTYPE_AES128_CTS_HMAC_SHA1_96,
@@ -100,6 +104,7 @@ struct _adcli_enroll {
int keytab_enctypes_explicit;
unsigned int computer_password_lifetime;
int computer_password_lifetime_explicit;
+ char *samba_data_tool;
};
static adcli_result
@@ -1537,26 +1542,33 @@ static adcli_result
update_samba_data (adcli_enroll *enroll)
{
int ret;
- char *argv_pw[] = { "/usr/bin/net", "changesecretpw", "-i", "-f", NULL };
- char *argv_sid[] = { "/usr/bin/net", "setdomainsid", NULL, NULL };
+ char *argv_pw[] = { NULL, "changesecretpw", "-i", "-f", NULL };
+ char *argv_sid[] = { NULL, "setdomainsid", NULL, NULL };
+
+ argv_pw[0] = (char *) adcli_enroll_get_samba_data_tool (enroll);
+ if (argv_pw[0] ==NULL) {
+ _adcli_err ("Samba data tool not available.");
+ return ADCLI_ERR_FAIL;
+ }
+ argv_sid[0] = argv_pw[0];
- _adcli_info ("Trying to set Samba secret.\n");
+ _adcli_info ("Trying to set Samba secret.");
ret = _adcli_call_external_program (argv_pw[0], argv_pw,
enroll->computer_password, NULL, NULL);
if (ret != ADCLI_SUCCESS) {
- _adcli_err ("Failed to set Samba computer account password.\n");
+ _adcli_err ("Failed to set Samba computer account password.");
}
argv_sid[2] = (char *) adcli_conn_get_domain_sid (enroll->conn);
if (argv_sid[2] == NULL) {
- _adcli_err ("Domain SID not available.\n");
+ _adcli_err ("Domain SID not available.");
} else {
- _adcli_info ("Trying to set domain SID %s for Samba.\n",
+ _adcli_info ("Trying to set domain SID %s for Samba.",
argv_sid[2]);
ret = _adcli_call_external_program (argv_sid[0], argv_sid,
NULL, NULL, NULL);
if (ret != ADCLI_SUCCESS) {
- _adcli_err ("Failed to set Samba domain SID.\n");
+ _adcli_err ("Failed to set Samba domain SID.");
}
}
@@ -1951,6 +1963,9 @@ adcli_enroll_new (adcli_conn *conn)
enroll->os_name = strdup (value);
return_val_if_fail (enroll->os_name != NULL, NULL);
+ enroll->samba_data_tool = strdup (SAMBA_DATA_TOOL);
+ return_val_if_fail (enroll->samba_data_tool != NULL, NULL);
+
return enroll;
}
@@ -1978,6 +1993,7 @@ enroll_free (adcli_enroll *enroll)
free (enroll->os_name);
free (enroll->os_version);
free (enroll->os_service_pack);
+ free (enroll->samba_data_tool);
free (enroll->user_principal);
_adcli_strv_free (enroll->service_names);
@@ -2343,3 +2359,19 @@ adcli_enroll_set_computer_password_lifetime (adcli_enroll *enroll,
enroll->computer_password_lifetime_explicit = 1;
}
+
+void
+adcli_enroll_set_samba_data_tool (adcli_enroll *enroll, const char *value)
+{
+ return_if_fail (enroll != NULL);
+ if (value != NULL && value[0] != '\0') {
+ _adcli_str_set (&enroll->samba_data_tool, value);
+ }
+}
+
+const char *
+adcli_enroll_get_samba_data_tool (adcli_enroll *enroll)
+{
+ return_val_if_fail (enroll != NULL, NULL);
+ return enroll->samba_data_tool;
+}
diff --git a/library/adenroll.h b/library/adenroll.h
index 32c9764..31ca0bc 100644
--- a/library/adenroll.h
+++ b/library/adenroll.h
@@ -141,4 +141,9 @@ const char * adcli_enroll_get_os_service_pack (adcli_enroll *enroll);
void adcli_enroll_set_os_service_pack (adcli_enroll *enroll,
const char *value);
+void adcli_enroll_set_samba_data_tool (adcli_enroll *enroll,
+ const char *value);
+
+const char * adcli_enroll_get_samba_data_tool (adcli_enroll *enroll);
+
#endif /* ADENROLL_H_ */
diff --git a/tools/computer.c b/tools/computer.c
index fc646f2..f86548b 100644
--- a/tools/computer.c
+++ b/tools/computer.c
@@ -30,6 +30,7 @@
#include <err.h>
#include <stdio.h>
#include <errno.h>
+#include <unistd.h>
static void
dump_details (adcli_conn *conn,
@@ -107,6 +108,7 @@ typedef enum {
opt_user_principal,
opt_computer_password_lifetime,
opt_add_samba_data,
+ opt_samba_data_tool,
} Option;
static adcli_tool_desc common_usages[] = {
@@ -145,6 +147,7 @@ static adcli_tool_desc common_usages[] = {
"successful join" },
{ opt_add_samba_data, "add domain SID and computer account password\n"
"to the Samba specific configuration database" },
+ { opt_samba_data_tool, "Absolute path to the tool used for add-samba-data" },
{ opt_verbose, "show verbose progress and failure messages", },
{ 0 },
};
@@ -160,6 +163,7 @@ parse_option (Option opt,
static int stdin_password = 0;
char *endptr;
unsigned int lifetime;
+ int ret;
switch (opt) {
case opt_login_ccache:
@@ -265,6 +269,16 @@ parse_option (Option opt,
adcli_enroll_set_computer_password_lifetime (enroll, lifetime);
return;
+ case opt_samba_data_tool:
+ errno = 0;
+ ret = access (optarg, X_OK);
+ if (ret != 0) {
+ ret = errno;
+ errx (EUSAGE, "Failed to access tool to add Samba data: %s", strerror (ret));
+ } else {
+ adcli_enroll_set_samba_data_tool (enroll, optarg);
+ }
+ return;
case opt_verbose:
return;
@@ -331,6 +345,7 @@ adcli_tool_computer_join (adcli_conn *conn,
{ "show-details", no_argument, NULL, opt_show_details },
{ "show-password", no_argument, NULL, opt_show_password },
{ "add-samba-data", no_argument, NULL, opt_add_samba_data },
+ { "samba-data-tool", no_argument, NULL, opt_samba_data_tool },
{ "verbose", no_argument, NULL, opt_verbose },
{ "help", no_argument, NULL, 'h' },
{ 0 },
@@ -434,6 +449,7 @@ adcli_tool_computer_update (adcli_conn *conn,
{ "show-details", no_argument, NULL, opt_show_details },
{ "show-password", no_argument, NULL, opt_show_password },
{ "add-samba-data", no_argument, NULL, opt_add_samba_data },
+ { "samba-data-tool", no_argument, NULL, opt_samba_data_tool },
{ "verbose", no_argument, NULL, opt_verbose },
{ "help", no_argument, NULL, 'h' },
{ 0 },
--
2.14.4

View File

@@ -1,174 +1,164 @@
/*
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
This file is part of Java Diff Utills Library.
* Copyright 2009-2017 java-diff-utils.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.difflib;
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.*;
import com.github.difflib.algorithm.DiffAlgorithmI;
import com.github.difflib.algorithm.DiffAlgorithmListener;
import com.github.difflib.algorithm.myers.MyersDiff;
import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.BiPredicate;
/**
* 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;
}
public final class DiffUtils {
/**
* Computes the difference between the original and revised list of elements with default diff
* algorithm
*
* @param <T> types to be diffed
* @param original The original text. Must not be {@code null}.
* @param revised The revised text. Must not be {@code null}.
* @param progress progress listener
* @return The patch describing the difference between the original and revised sequences. Never
* {@code null}.
*/
public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmListener progress) {
return DiffUtils.diff(original, revised, new MyersDiff<>(), progress);
}
public static <T> Patch<T> diff(List<T> original, List<T> revised) {
return DiffUtils.diff(original, revised, new MyersDiff<>(), null);
}
public static <T> Patch<T> diff(List<T> original, List<T> revised, boolean includeEqualParts) {
return DiffUtils.diff(original, revised, new MyersDiff<>(), null, includeEqualParts);
}
/**
* Computes the difference between the original and revised text.
*/
public static Patch<String> diff(String sourceText, String targetText,
DiffAlgorithmListener progress) {
return DiffUtils.diff(
Arrays.asList(sourceText.split("\n")),
Arrays.asList(targetText.split("\n")), progress);
}
/**
* Computes the difference between the original and revised list of elements with default diff
* algorithm
*
* @param source The original text. Must not be {@code null}.
* @param target The revised text. Must not be {@code null}.
*
* @param equalizer the equalizer object to replace the default compare algorithm
* (Object.equals). If {@code null} the default equalizer of the default algorithm is used..
* @return The patch describing the difference between the original and revised sequences. Never
* {@code null}.
*/
public static <T> Patch<T> diff(List<T> source, List<T> target,
BiPredicate<T, T> equalizer) {
if (equalizer != null) {
return DiffUtils.diff(source, target,
new MyersDiff<>(equalizer));
}
return DiffUtils.diff(source, target, new MyersDiff<>());
}
public static <T> Patch<T> diff(List<T> original, List<T> revised,
DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress) {
return diff(original, revised, algorithm, progress, false);
}
/**
* Computes the difference between the original and revised list of elements with default diff
* algorithm
*
* @param original The original text. Must not be {@code null}.
* @param revised The revised text. Must not be {@code null}.
* @param algorithm The diff algorithm. Must not be {@code null}.
* @param progress The diff algorithm listener.
* @param includeEqualParts Include equal data parts into the patch.
* @return The patch describing the difference between the original and revised sequences. Never
* {@code null}.
*/
public static <T> Patch<T> diff(List<T> original, List<T> revised,
DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress,
boolean includeEqualParts) {
Objects.requireNonNull(original, "original must not be null");
Objects.requireNonNull(revised, "revised must not be null");
Objects.requireNonNull(algorithm, "algorithm must not be null");
return Patch.generate(original, revised, algorithm.computeDiff(original, revised, progress), includeEqualParts);
}
/**
* Computes the difference between the original and revised list of elements with default diff
* algorithm
*
* @param original The original text. Must not be {@code null}.
* @param revised The revised text. Must not be {@code null}.
* @param algorithm The diff algorithm. Must not be {@code null}.
* @return The patch describing the difference between the original and revised sequences. Never
* {@code null}.
*/
public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmI<T> algorithm) {
return diff(original, revised, algorithm, null);
}
private static List<String> compressLines(List<String> lines, String delimiter) {
if (lines.isEmpty()) {
return Collections.emptyList();
}
return Collections.singletonList(String.join(delimiter, lines));
}
/**
* 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 <T> List<T> patch(List<T> original, Patch<T> 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 <T> List<T> unpatch(List<T> revised, Patch<T> patch) {
return patch.restore(revised);
}
private DiffUtils() {
}
}

View File

@@ -1,308 +1,190 @@
/*
Copyright 2009 Dmitry Naumenko (dm.naumenko@gmail.com)
This file is part of Java Diff Utils Library.
* Copyright 2009-2017 java-diff-utills.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.difflib;
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.*;
import com.github.difflib.algorithm.DiffAlgorithmI;
import com.github.difflib.algorithm.DiffAlgorithmListener;
import com.github.difflib.algorithm.myers.MyersDiff;
import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.BiPredicate;
/**
* 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;
}
public final class DiffUtills {
/**
* 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 );
/**
* Computes the difference between the original and revised list of elements with default diff
* algorithm
*
* @param <T> types to be diffed
* @param original The original text. Must not be {@code null}.
* @param revised The revised text. Must not be {@code null}.
* @param progress progress listener
* @return The patch describing the difference between the original and revised sequences. Never
* {@code null}.
*/
public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmListener progress) {
return DiffUtils.diff(original, revised, new MyersDiff<>(), progress);
}
public static <T> Patch<T> diff(List<T> original, List<T> revised) {
return DiffUtils.diff(original, revised, new MyersDiff<>(), null);
}
public static <T> Patch<T> diff(List<T> original, List<T> revised, boolean includeEqualParts) {
return DiffUtils.diff(original, revised, new MyersDiff<>(), null, includeEqualParts);
}
/**
* Computes the difference between the original and revised text.
*/
public static Patch<String> diff(String sourceText, String targetText,
DiffAlgorithmListener progress) {
return DiffUtils.diff(
Arrays.asList(sourceText.split("\n")),
Arrays.asList(targetText.split("\n")), progress);
}
/**
* Computes the difference between the original and revised list of elements with default diff
* algorithm
*
* @param source The original text. Must not be {@code null}.
* @param target The revised text. Must not be {@code null}.
*
* @param equalizer the equalizer object to replace the default compare algorithm
* (Object.equals). If {@code null} the default equalizer of the default algorithm is used..
* @return The patch describing the difference between the original and revised sequences. Never
* {@code null}.
*/
public static <T> Patch<T> diff(List<T> source, List<T> target,
BiPredicate<T, T> equalizer) {
if (equalizer != null) {
return DiffUtils.diff(source, target,
new MyersDiff<>(equalizer));
}
return DiffUtils.diff(source, target, new MyersDiff<>());
}
return ret;
}
public static <T> Patch<T> diff(List<T> original, List<T> revised,
DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress) {
return diff(original, revised, algorithm, progress, false);
}
/**
* Computes the difference between the original and revised list of elements with default diff
* algorithm
*
* @param original The original text. Must not be {@code null}.
* @param revised The revised text. Must not be {@code null}.
* @param algorithm The diff algorithm. Must not be {@code null}.
* @param progress The diff algorithm listener.
* @param includeEqualParts Include equal data parts into the patch.
* @return The patch describing the difference between the original and revised sequences. Never
* {@code null}.
*/
public static <T> Patch<T> diff(List<T> original, List<T> revised,
DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress,
boolean includeEqualParts) {
Objects.requireNonNull(original, "original must not be null");
Objects.requireNonNull(revised, "revised must not be null");
Objects.requireNonNull(algorithm, "algorithm must not be null"); /* BLA BLA BLA */
/**
* 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 Patch.generate(original, revised, algorithm.computeDiff(original, revised, progress), includeEqualParts);
}
/**
* Computes the difference between the original and revised list of elements with default diff
* algorithm
*
* @param original The original text. Must not be {@code null}.
* @param revised The revised text. Must not be {@code null}.
* @param algorithm The diff algorithm. Must not be {@code null}.
* @return The patch describing the difference between the original and revised sequences. Never
* {@code null}.
*/
public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmI<T> algorithm) {
return diff(original, revised, algorithm, null);
}
/**
* Computes the difference between the given texts inline. This one uses the "trick" to make out
* of texts lists of characters, like DiffRowGenerator does and merges those changes at the end
* together again.
*
* @param original
* @param revised
* @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++;
public static Patch<String> diffInline(String original, String revised) {
List<String> origList = new ArrayList<>();
List<String> revList = new ArrayList<>();
for (Character character : original.toCharArray()) {
origList.add(character.toString());
}
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
for (Character character : revised.toCharArray()) {
revList.add(character.toString());
}
// 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++;
Patch<String> patch = DiffUtils.diff(origList, revList);
for (AbstractDelta<String> delta : patch.getDeltas()) {
delta.getSource().setLines(compressLines(delta.getSource().getLines(), ""));
delta.getTarget().setLines(compressLines(delta.getTarget().getLines(), ""));
}
return patch;
}
// 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 );
private static List<String> compressLines(List<String> lines, String delimiter) {
if (lines.isEmpty()) {
return Collections.emptyList();
}
for ( Object line: delta.getRevised().getLines() ) {
buffer.add( "+" + line );
}
return buffer;
}
return Collections.singletonList(String.join(delimiter, lines));
}
/**
* 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 <T> List<T> patch(List<T> original, Patch<T> 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 <T> List<T> unpatch(List<T> revised, Patch<T> patch) {
return patch.restore(revised);
}
private DiffUtils() {
}
}

10
pom.xml
View File

@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.java-diff-utils</groupId>
<artifactId>java-diff-utils-parent</artifactId>
<version>4.8</version>
<version>4.10</version>
<name>java-diff-utils-parent</name>
<packaging>pom</packaging>
<modules>
@@ -12,7 +12,7 @@
</modules>
<description>The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java.</description>
<url>https://github.com/java-diff-utils/java-diff-utils</url>
<inceptionYear>2009</inceptionYear>
<inceptionYear>2009</inceptionYear>
<distributionManagement>
<repository>
@@ -29,7 +29,7 @@
<connection>scm:git:https://github.com/java-diff-utils/java-diff-utils.git</connection>
<developerConnection>scm:git:ssh://git@github.com:java-diff-utils/java-diff-utils.git</developerConnection>
<url>https://github.com/java-diff-utils/java-diff-utils.git</url>
<tag>java-diff-utils-parent-4.8</tag>
<tag>java-diff-utils-parent-4.10</tag>
</scm>
<issueManagement>
<system>GitHub Issues</system>
@@ -65,13 +65,13 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.2</version>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.15.0</version>
<version>3.19.0</version>
<scope>test</scope>
</dependency>
</dependencies>