[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;
This commit is contained in:
Nadya Zabrodina
2016-09-08 14:17:38 +03:00
committed by Nadya Zabrodina
parent 2bb7643d26
commit 42003b5fbe
7 changed files with 38 additions and 24 deletions
@@ -34,5 +34,13 @@ public interface AirContentRevision {
@NotNull
PathDescription getPath();
Charset getCharset();
@Nullable
default Charset getCharset() {
return null;
}
@Nullable
default String getLineSeparator() {
return null;
}
}
@@ -27,10 +27,16 @@ import java.util.List;
*/
public class TextFilePatch extends FilePatch {
private Charset myCharset;
@Nullable private String myLineSeparator;
private final List<PatchHunk> 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;
}
}
@@ -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);
}
}
}
@@ -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<FilePatch> buildPatch(final Project project, final Collection<Change> changes, final String basePath, final boolean reversePatch) throws VcsException {
return buildPatch(project, changes, basePath, reversePatch, false);
}
@NotNull
public static List<FilePatch> buildPatch(final Project project, final Collection<Change> changes, final String basePath,
final boolean reversePatch, final boolean includeBaseText) throws VcsException {
final Collection<BeforeAfter<AirContentRevision>> 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;
}
}
}
@@ -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;
}
@@ -47,8 +47,8 @@ public class BinaryPatchWriter {
public static void writeBinaries(@Nullable String basePath,
@NotNull List<BinaryFilePatch> 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()));
@@ -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 {