From 42003b5fbeb57a122d123c6f048cbec703ea1c86 Mon Sep 17 00:00:00 2001 From: Nadya Zabrodina Date: Tue, 30 Aug 2016 20:00:56 +0300 Subject: [PATCH] [patch]: use appropriate lineSeparator for specific cases * use LS from VF for diff hunks; * use external (CODE STYLE) LS for headers (to support svn default case); * use LF for git style header and binary encoded data; * remove 'include base' option; * test added; --- .../diff/impl/patch/AirContentRevision.java | 10 ++++++- .../diff/impl/patch/TextFilePatch.java | 12 +++++++++ .../diff/impl/patch/UnifiedDiffWriter.java | 3 ++- .../diff/impl/patch/IdeaTextPatchBuilder.java | 26 +++++++------------ .../diff/impl/patch/TextPatchBuilder.java | 5 ++-- .../vcs/changes/patch/BinaryPatchWriter.java | 4 +-- .../vcs/changes/patch/PatchWriter.java | 2 +- 7 files changed, 38 insertions(+), 24 deletions(-) diff --git a/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/AirContentRevision.java b/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/AirContentRevision.java index 745d23eba6d0..85b5b26e9271 100644 --- a/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/AirContentRevision.java +++ b/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/AirContentRevision.java @@ -34,5 +34,13 @@ public interface AirContentRevision { @NotNull PathDescription getPath(); - Charset getCharset(); + @Nullable + default Charset getCharset() { + return null; + } + + @Nullable + default String getLineSeparator() { + return null; + } } diff --git a/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/TextFilePatch.java b/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/TextFilePatch.java index c40814233ed6..d06e555d59b8 100644 --- a/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/TextFilePatch.java +++ b/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/TextFilePatch.java @@ -27,10 +27,16 @@ import java.util.List; */ public class TextFilePatch extends FilePatch { private Charset myCharset; + @Nullable private String myLineSeparator; private final List myHunks; public TextFilePatch(@Nullable Charset charset) { + this(charset, null); + } + + public TextFilePatch(@Nullable Charset charset, @Nullable String lineSeparator) { myCharset = charset; + myLineSeparator = lineSeparator; myHunks = new ArrayList<>(); } @@ -45,6 +51,7 @@ public class TextFilePatch extends FilePatch { setBeforeName(patch.getBeforeName()); setAfterName(patch.getAfterName()); myHunks = patch.myHunks; + myLineSeparator = patch.getLineSeparator(); } public void addHunk(final PatchHunk hunk) { @@ -73,4 +80,9 @@ public class TextFilePatch extends FilePatch { public Charset getCharset() { return myCharset; } + + @Nullable + public String getLineSeparator() { + return myLineSeparator; + } } diff --git a/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/UnifiedDiffWriter.java b/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/UnifiedDiffWriter.java index c1a8755470bf..b7a196f1059c 100644 --- a/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/UnifiedDiffWriter.java +++ b/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/diff/impl/patch/UnifiedDiffWriter.java @@ -83,6 +83,7 @@ public class UnifiedDiffWriter { additionalMap.put(extension.getName(), charSequence); } } + String fileContentLineSeparator = ObjectUtils.coalesce(patch.getLineSeparator(), lineSeparator, "\n"); writeFileHeading(patch, writer, lineSeparator, additionalMap); for(PatchHunk hunk: patch.getHunks()) { writeHunkStart(writer, hunk.getStartLineBefore(), hunk.getEndLineBefore(), hunk.getStartLineAfter(), hunk.getEndLineAfter(), @@ -107,7 +108,7 @@ public class UnifiedDiffWriter { writer.write(lineSeparator + NO_NEWLINE_SIGNATURE + lineSeparator); } else { - writer.write(lineSeparator); + writer.write(fileContentLineSeparator); } } } diff --git a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/IdeaTextPatchBuilder.java b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/IdeaTextPatchBuilder.java index 4bb1bb5c6bdd..066b3c3e1fb9 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/IdeaTextPatchBuilder.java +++ b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/IdeaTextPatchBuilder.java @@ -23,6 +23,7 @@ import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vcs.VcsOutgoingChangesProvider; import com.intellij.openapi.vcs.VcsRoot; import com.intellij.openapi.vcs.changes.*; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.BeforeAfter; import com.intellij.util.containers.Convertor; import com.intellij.util.containers.MultiMap; @@ -78,12 +79,6 @@ public class IdeaTextPatchBuilder { @NotNull public static List buildPatch(final Project project, final Collection changes, final String basePath, final boolean reversePatch) throws VcsException { - return buildPatch(project, changes, basePath, reversePatch, false); - } - - @NotNull - public static List buildPatch(final Project project, final Collection changes, final String basePath, - final boolean reversePatch, final boolean includeBaseText) throws VcsException { final Collection> revisions; if (project != null) { revisions = revisionsConvertor(project, new ArrayList<>(changes)); @@ -93,11 +88,8 @@ public class IdeaTextPatchBuilder { revisions.add(new BeforeAfter<>(convertRevisionToAir(change.getBeforeRevision()), convertRevisionToAir(change.getAfterRevision()))); } } - return TextPatchBuilder.buildPatch(revisions, basePath, reversePatch, SystemInfo.isFileSystemCaseSensitive, new Runnable() { - public void run() { - ProgressManager.checkCanceled(); - } - }, includeBaseText); + return TextPatchBuilder.buildPatch(revisions, basePath, reversePatch, SystemInfo.isFileSystemCaseSensitive, + () -> ProgressManager.checkCanceled()); } @Nullable @@ -159,11 +151,6 @@ public class IdeaTextPatchBuilder { public PathDescription getPath() { return myDescription; } - - @Override - public Charset getCharset() { - return null; - } } private static class TextAirContentRevision implements AirContentRevision { @@ -210,5 +197,12 @@ public class IdeaTextPatchBuilder { public Charset getCharset() { return myRevision.getFile().getCharset(); } + + @Nullable + @Override + public String getLineSeparator() { + VirtualFile virtualFile = myRevision.getFile().getVirtualFile(); + return virtualFile != null ? virtualFile.getDetectedLineSeparator() : null; + } } } diff --git a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/TextPatchBuilder.java b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/TextPatchBuilder.java index f1bb512a2636..0331e693d7bf 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/TextPatchBuilder.java +++ b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/TextPatchBuilder.java @@ -63,8 +63,7 @@ public class TextPatchBuilder { @NotNull String basePath, boolean reversePatch, boolean isCaseSensitive, - @Nullable Runnable cancelChecker, - boolean includeBaseText) throws VcsException { + @Nullable Runnable cancelChecker) throws VcsException { TextPatchBuilder builder = new TextPatchBuilder(basePath, reversePatch, isCaseSensitive, cancelChecker); return builder.build(changes); } @@ -345,7 +344,7 @@ public class TextPatchBuilder { @NotNull private TextFilePatch buildPatchHeading(@NotNull AirContentRevision beforeRevision, @NotNull AirContentRevision afterRevision) { - TextFilePatch result = new TextFilePatch(afterRevision.getCharset()); + TextFilePatch result = new TextFilePatch(afterRevision.getCharset(), afterRevision.getLineSeparator()); setPatchHeading(result, beforeRevision, afterRevision); return result; } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/BinaryPatchWriter.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/BinaryPatchWriter.java index b4bcbe4686d1..9a8f14e7c717 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/BinaryPatchWriter.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/BinaryPatchWriter.java @@ -47,8 +47,8 @@ public class BinaryPatchWriter { public static void writeBinaries(@Nullable String basePath, @NotNull List patches, - @NotNull Writer writer, - @NotNull final String lineSeparator) throws IOException { + @NotNull Writer writer) throws IOException { + String lineSeparator = "\n"; //use it for git headers&binary content, otherwise git won't parse&apply it properly for (FilePatch patch : patches) { BinaryFilePatch filePatch = (BinaryFilePatch)patch; writer.write(String.format(GIT_DIFF_HEADER, filePatch.getBeforeName(), filePatch.getAfterName())); diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/PatchWriter.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/PatchWriter.java index 73c8ce36b56c..b4791bf1c4c2 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/PatchWriter.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/PatchWriter.java @@ -59,7 +59,7 @@ public class PatchWriter { UnifiedDiffWriter .write(project, basePath, patches, writer, lineSeparator, Extensions.getExtensions(PatchEP.EP_NAME, project), commitContext); if (includeBinaries) { - BinaryPatchWriter.writeBinaries(basePath, ContainerUtil.findAll(patches, BinaryFilePatch.class), writer, lineSeparator); + BinaryPatchWriter.writeBinaries(basePath, ContainerUtil.findAll(patches, BinaryFilePatch.class), writer); } } finally {