Skip to content

Commit

Permalink
Make it work!
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorcloudy committed May 7, 2020
1 parent 673074c commit d7fdde5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

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

import com.github.difflib.UnifiedDiffUtils;
import com.github.difflib.patch.ChangeDelta;
import com.github.difflib.patch.DeleteDelta;
import com.github.difflib.patch.InsertDelta;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
Expand All @@ -23,7 +27,6 @@
import com.google.devtools.build.lib.vfs.Path;
import com.github.difflib.patch.Chunk;
import com.github.difflib.patch.AbstractDelta;
import com.github.difflib.DiffUtils;
import com.github.difflib.patch.Patch;
import com.github.difflib.patch.PatchFailedException;
import java.io.IOException;
Expand Down Expand Up @@ -84,7 +87,6 @@ public static List<String> applyTo(Patch<String> patch, List<String> target)
throws PatchFailedException {
List<AbstractDelta<String>> deltas = patch.getDeltas();
List<String> result = new ArrayList<>(target);
deltas.sort(DeltaComparator.INSTANCE);
for (AbstractDelta<String> item : Lists.reverse(deltas)) {
AbstractDelta<String> delta = item;
applyTo(delta, result);
Expand All @@ -105,8 +107,8 @@ private static void applyTo(AbstractDelta<String> delta, List<String> result)
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 @@ -115,9 +117,27 @@ private static void applyTo(AbstractDelta<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:
default:
break;
}
PatchFailedException exception = null;
if (newDelta != null) {
exception = applyDelta(newDelta, result);
}
if (exception == null) {
return;
}
Expand All @@ -135,11 +155,11 @@ private static PatchFailedException applyDelta(AbstractDelta<String> 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);
}
}
Expand Down Expand Up @@ -435,7 +455,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().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 @@ -619,7 +639,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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
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 com.github.difflib.patch.PatchFailedException;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
Expand Down

3 comments on commit d7fdde5

@olekw
Copy link

@olekw olekw commented on d7fdde5 May 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice, thanks for writing this! I'll cherry-pick this right now for Debian so we can use the newer local library.

@meteorcloudy
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, there are some other changes before this. The squashed commit is this one 1d6fc7c

@olekw
Copy link

@olekw olekw commented on d7fdde5 May 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok. I'll use the squashed on. Thanks! :)

Please sign in to comment.