Skip to content

Commit

Permalink
Migrate java-diff-utils version to 4.0
Browse files Browse the repository at this point in the history
The source of java diff utils 4.0 was added at unknown commit.

The old java-diff-utils library is no longer maintained and with a problematic LICENSE file [1].
The new version is a fork of the original version which is under maintenance and fixed the LICENSE problem.

[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=845471#52

conga-relnote: n/a
RELNOTES: None
PiperOrigin-RevId: 317628549
  • Loading branch information
meteorcloudy authored and copybara-github committed Jun 22, 2020
1 parent 9757729 commit 1dbf65b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ java_library(
"//src/main/java/com/google/devtools/common/options",
"//third_party:apache_commons_compress",
"//third_party:auto_value",
"//third_party:diffutils",
"//third_party:flogger",
"//third_party:guava",
"//third_party:java-diff-utils",
"//third_party:jsr305",
"//third_party:xz",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@

package com.google.devtools.build.lib.bazel.repository;

import com.github.difflib.UnifiedDiffUtils;
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.InsertDelta;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import difflib.Chunk;
import difflib.Delta;
import difflib.DeltaComparator;
import difflib.DiffUtils;
import difflib.Patch;
import difflib.PatchFailedException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -83,11 +85,10 @@ private static class OffsetPatch {

public static List<String> applyTo(Patch<String> patch, List<String> target)
throws PatchFailedException {
List<Delta<String>> deltas = patch.getDeltas();
List<AbstractDelta<String>> deltas = patch.getDeltas();
List<String> result = new ArrayList<>(target);
deltas.sort(DeltaComparator.INSTANCE);
for (Delta<String> item : Lists.reverse(deltas)) {
Delta<String> delta = item;
for (AbstractDelta<String> item : Lists.reverse(deltas)) {
AbstractDelta<String> delta = item;
applyTo(delta, result);
}

Expand All @@ -99,15 +100,15 @@ public static List<String> applyTo(Patch<String> patch, List<String> target)
* to apply the Delta with an offset, starting from 1, up to the total lines in the original
* content. For every offset, we try both forwards and backwards.
*/
private static void applyTo(Delta<String> delta, List<String> result)
private static void applyTo(AbstractDelta<String> delta, List<String> result)
throws PatchFailedException {
PatchFailedException e = applyDelta(delta, result);
if (e == null) {
return;
}

Chunk<String> original = delta.getOriginal();
Chunk<String> revised = delta.getRevised();
Chunk<String> original = delta.getSource();
Chunk<String> revised = delta.getTarget();
int[] direction = {1, -1};
int maxOffset = result.size();
for (int i = 1; i < maxOffset; i++) {
Expand All @@ -116,9 +117,25 @@ private static void applyTo(Delta<String> delta, List<String> result)
if (offset + original.getPosition() < 0 || offset + revised.getPosition() < 0) {
continue;
}
delta.setOriginal(new Chunk<>(original.getPosition() + offset, original.getLines()));
delta.setRevised(new Chunk<>(revised.getPosition() + offset, revised.getLines()));
PatchFailedException exception = applyDelta(delta, result);
Chunk<String> source = new Chunk<>(original.getPosition() + offset, original.getLines());
Chunk<String> target = new Chunk<>(revised.getPosition() + offset, revised.getLines());
AbstractDelta<String> newDelta = null;
switch (delta.getType()) {
case CHANGE:
newDelta = new ChangeDelta<>(source, target);
break;
case INSERT:
newDelta = new InsertDelta<>(source, target);
break;
case DELETE:
newDelta = new DeleteDelta<>(source, target);
break;
case EQUAL:
}
PatchFailedException exception = null;
if (newDelta != null) {
exception = applyDelta(newDelta, result);
}
if (exception == null) {
return;
}
Expand All @@ -128,19 +145,20 @@ private static void applyTo(Delta<String> delta, List<String> result)
throw e;
}

private static PatchFailedException applyDelta(Delta<String> delta, List<String> result) {
private static PatchFailedException applyDelta(
AbstractDelta<String> delta, List<String> result) {
try {
delta.applyTo(result);
return null;
} catch (PatchFailedException e) {
String msg =
String.join(
"\n",
"**Original Position**: " + (delta.getOriginal().getPosition() + 1) + "\n",
"**Original Position**: " + (delta.getSource().getPosition() + 1) + "\n",
"**Original Content**:",
String.join("\n", delta.getOriginal().getLines()) + "\n",
String.join("\n", delta.getSource().getLines()) + "\n",
"**Revised Content**:",
String.join("\n", delta.getRevised().getLines()) + "\n");
String.join("\n", delta.getTarget().getLines()) + "\n");
return new PatchFailedException(e.getMessage() + "\n" + msg);
}
}
Expand Down Expand Up @@ -435,8 +453,7 @@ private static void checkFilesStatusForPatching(

// Does this patch look like adding a new file.
boolean isAddFile =
patch.getDeltas().size() == 1
&& patch.getDeltas().get(0).getOriginal().getLines().isEmpty();
patch.getDeltas().size() == 1 && patch.getDeltas().get(0).getSource().getLines().isEmpty();

// If this patch is not adding a new file,
// then either old file or new file should be specified and exists,
Expand Down Expand Up @@ -620,7 +637,7 @@ public static void apply(Path patchFile, int strip, Path outputDirectory)
oldFile, newFile, oldFileStr, newFileStr, patchStartLocation);
}

Patch<String> patch = DiffUtils.parseUnifiedDiff(patchContent);
Patch<String> patch = UnifiedDiffUtils.parseUnifiedDiff(patchContent);
checkFilesStatusForPatching(
patch, oldFile, newFile, oldFileStr, newFileStr, patchStartLocation);

Expand Down Expand Up @@ -660,4 +677,6 @@ public static void apply(Path patchFile, int strip, Path outputDirectory)
}
}
}

private PatchUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//third_party:diffutils",
"//third_party:guava",
"//third_party:java-diff-utils",
"//third_party:jsr305",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.devtools.build.lib.bazel.repository.starlark;

import com.github.difflib.patch.PatchFailedException;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
import com.google.common.base.Joiner;
Expand Down Expand Up @@ -72,7 +73,6 @@
import com.google.devtools.build.skyframe.SkyFunction.Environment;
import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
import com.google.devtools.build.skyframe.SkyKey;
import difflib.PatchFailedException;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ java_library(
"//src/test/java/com/google/devtools/build/lib/testutil",
"//src/test/java/com/google/devtools/build/lib/testutil:TestConstants",
"//src/test/java/com/google/devtools/build/lib/testutil:TestUtils",
"//third_party:diffutils",
"//third_party:guava",
"//third_party:java-diff-utils",
"//third_party:junit4",
"//third_party:mockito",
"//third_party:truth",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.github.difflib.patch.PatchFailedException;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.testutil.Scratch;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import difflib.PatchFailedException;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
Expand Down

0 comments on commit 1dbf65b

Please sign in to comment.