IDEA-CR-65029 Use Path in TextPatchBuilder

GitOrigin-RevId: fe48f06e359209923887d9dcd1b1b3621f5d7395
This commit is contained in:
Vladimir Krivosheev
2020-07-19 18:56:50 +00:00
committed by intellij-monorepo-bot
parent 56bf94cd4c
commit ba28406f5d
4 changed files with 30 additions and 36 deletions
@@ -1,25 +1,14 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.diff.impl.patch;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Path;
public interface PathDescription {
@NotNull
String getPath();
@NotNull Path getPath();
boolean isDirectory();
long lastModified();
}
@@ -98,10 +98,12 @@ public final class IdeaTextPatchBuilder {
@Nullable
private static AirContentRevision convertRevision(@Nullable ContentRevision cr, @Nullable String actualTextContent) {
if (cr == null) return null;
final FilePath fp = cr.getFile();
final StaticPathDescription description = new StaticPathDescription(fp.isDirectory(), fp.getIOFile().lastModified(), fp.getPath());
if (cr == null) {
return null;
}
FilePath filePath = cr.getFile();
StaticPathDescription description = new StaticPathDescription(filePath.isDirectory(), filePath.getIOFile().toPath());
if (actualTextContent != null) {
return new PartialTextAirContentRevision(actualTextContent, cr, description, null);
}
@@ -3,20 +3,32 @@ package com.intellij.openapi.diff.impl.patch;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
final class StaticPathDescription implements PathDescription {
private final String myPath;
private final Path myPath;
private final boolean myIsDirectory;
private final long myLastModified;
StaticPathDescription(boolean isDirectory, long lastModified, String path) {
StaticPathDescription(boolean isDirectory, @NotNull Path path) {
myIsDirectory = isDirectory;
long lastModified;
try {
lastModified = Files.getLastModifiedTime(path).toMillis();
}
catch (IOException e) {
lastModified = 0;
}
myLastModified = lastModified;
myPath = path;
}
@Override
@NotNull
public String getPath() {
public @NotNull Path getPath() {
return myPath;
}
@@ -7,8 +7,6 @@ import com.intellij.diff.comparison.DiffTooBigException;
import com.intellij.diff.comparison.iterables.FairDiffIterable;
import com.intellij.diff.util.Range;
import com.intellij.openapi.progress.DumbProgressIndicator;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.LineTokenizer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.FileStatus;
@@ -19,6 +17,7 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
@@ -331,14 +330,6 @@ public final class TextPatchBuilder {
return hunk;
}
public static @NotNull String getRelativePath(@NotNull String basePath, @NotNull String secondPath) {
String baseModified = FileUtil.toSystemIndependentName(basePath);
String secondModified = FileUtil.toSystemIndependentName(secondPath);
String relPath = FileUtil.getRelativePath(baseModified, secondModified, '/', SystemInfo.isFileSystemCaseSensitive);
return relPath == null ? secondModified : relPath;
}
@NotNull
private TextFilePatch buildPatchHeading(@NotNull AirContentRevision beforeRevision,
@NotNull AirContentRevision afterRevision) {
@@ -350,10 +341,10 @@ public final class TextPatchBuilder {
private void setPatchHeading(@NotNull FilePatch result,
@NotNull AirContentRevision beforeRevision,
@NotNull AirContentRevision afterRevision) {
result.setBeforeName(getRelativePath(myBasePath.toString(), beforeRevision.getPath().getPath()));
result.setBeforeName(myBasePath.relativize(beforeRevision.getPath().getPath()).toString().replace(File.separatorChar, '/'));
result.setBeforeVersionId(getRevisionName(beforeRevision));
result.setAfterName(getRelativePath(myBasePath.toString(), afterRevision.getPath().getPath()));
result.setAfterName(myBasePath.relativize(afterRevision.getPath().getPath()).toString().replace(File.separatorChar, '/'));
result.setAfterVersionId(getRevisionName(afterRevision));
}