From 1dbf65ba9ef38cef379669fb4356c6c0b72c50f9 Mon Sep 17 00:00:00 2001 From: pcloudy Date: Mon, 22 Jun 2020 04:20:35 -0700 Subject: [PATCH] Migrate java-diff-utils version to 4.0 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 --- .../devtools/build/lib/bazel/repository/BUILD | 2 +- .../build/lib/bazel/repository/PatchUtil.java | 65 ++++++++++++------- .../build/lib/bazel/repository/starlark/BUILD | 2 +- .../starlark/StarlarkRepositoryContext.java | 2 +- .../devtools/build/lib/bazel/repository/BUILD | 2 +- .../lib/bazel/repository/PatchUtilTest.java | 2 +- 6 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/BUILD b/src/main/java/com/google/devtools/build/lib/bazel/repository/BUILD index 6f67d9c15b1fea..c05b5685f2ee2a 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/BUILD +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/BUILD @@ -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", ], diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/PatchUtil.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/PatchUtil.java index 36a114c126acd9..502c3230bd3e36 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/PatchUtil.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/PatchUtil.java @@ -14,6 +14,14 @@ 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; @@ -21,12 +29,6 @@ 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; @@ -83,11 +85,10 @@ private static class OffsetPatch { public static List applyTo(Patch patch, List target) throws PatchFailedException { - List> deltas = patch.getDeltas(); + List> deltas = patch.getDeltas(); List result = new ArrayList<>(target); - deltas.sort(DeltaComparator.INSTANCE); - for (Delta item : Lists.reverse(deltas)) { - Delta delta = item; + for (AbstractDelta item : Lists.reverse(deltas)) { + AbstractDelta delta = item; applyTo(delta, result); } @@ -99,15 +100,15 @@ public static List applyTo(Patch patch, List 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 delta, List result) + private static void applyTo(AbstractDelta delta, List result) throws PatchFailedException { PatchFailedException e = applyDelta(delta, result); if (e == null) { return; } - Chunk original = delta.getOriginal(); - Chunk revised = delta.getRevised(); + Chunk original = delta.getSource(); + Chunk revised = delta.getTarget(); int[] direction = {1, -1}; int maxOffset = result.size(); for (int i = 1; i < maxOffset; i++) { @@ -116,9 +117,25 @@ private static void applyTo(Delta delta, List 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 source = new Chunk<>(original.getPosition() + offset, original.getLines()); + Chunk target = new Chunk<>(revised.getPosition() + offset, revised.getLines()); + AbstractDelta 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; } @@ -128,7 +145,8 @@ private static void applyTo(Delta delta, List result) throw e; } - private static PatchFailedException applyDelta(Delta delta, List result) { + private static PatchFailedException applyDelta( + AbstractDelta delta, List result) { try { delta.applyTo(result); return null; @@ -136,11 +154,11 @@ private static PatchFailedException applyDelta(Delta delta, List 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); } } @@ -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, @@ -620,7 +637,7 @@ public static void apply(Path patchFile, int strip, Path outputDirectory) oldFile, newFile, oldFileStr, newFileStr, patchStartLocation); } - Patch patch = DiffUtils.parseUnifiedDiff(patchContent); + Patch patch = UnifiedDiffUtils.parseUnifiedDiff(patchContent); checkFilesStatusForPatching( patch, oldFile, newFile, oldFileStr, newFileStr, patchStartLocation); @@ -660,4 +677,6 @@ public static void apply(Path patchFile, int strip, Path outputDirectory) } } } + + private PatchUtil() {} } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/BUILD b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/BUILD index 7fff85cd5d5955..86fdc744804684 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/BUILD +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/BUILD @@ -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", ], ) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryContext.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryContext.java index e4f68dbdc02ba8..71d269b27d2a5e 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryContext.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryContext.java @@ -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; @@ -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; diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/BUILD b/src/test/java/com/google/devtools/build/lib/bazel/repository/BUILD index bd4926b6f514b1..f7ccb93101608e 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/BUILD +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/BUILD @@ -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", diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/PatchUtilTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/PatchUtilTest.java index ba52aa25e6cae6..973712fbe28a2d 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/PatchUtilTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/PatchUtilTest.java @@ -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;