refactoring: ContentRevisionCache removed for HgContentRevision, DRY fixed

This commit is contained in:
Nadya Zabrodina
2015-05-27 22:50:26 +04:00
parent f81d50346c
commit 60ead2d987
3 changed files with 13 additions and 49 deletions
@@ -14,20 +14,15 @@ package org.zmlx.hg4idea;
import com.google.common.base.Objects;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Throwable2Computable;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.impl.ContentRevisionCache;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.vcsUtil.VcsUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.zmlx.hg4idea.command.HgCatCommand;
import org.zmlx.hg4idea.execution.HgCommandResult;
import org.zmlx.hg4idea.util.HgUtil;
import java.io.IOException;
public class HgContentRevision implements ContentRevision {
private final Project myProject;
@@ -53,48 +48,13 @@ public class HgContentRevision implements ContentRevision {
@Override
public String getContent() throws VcsException {
if (myRevisionNumber.isWorkingVersion()) return VcsUtil.getFileContent(myHgFile.getFile().getPath());
final HgFile fileToCat = HgUtil.getFileNameInTargetRevision(myProject, myRevisionNumber, myHgFile);
FilePath filePath = fileToCat.toFilePath();
try {
return ContentRevisionCache
.getOrLoadAsString(myProject, filePath, myRevisionNumber, HgVcs.getKey(), ContentRevisionCache.UniqueType.REPOSITORY_CONTENT,
new Throwable2Computable<byte[], VcsException, IOException>() {
@Override
public byte[] compute() throws VcsException, IOException {
return loadContent(fileToCat);
}
}, filePath.getCharset());
}
catch (IOException e) {
throw new VcsException(e);
}
return CharsetToolkit.bytesToString(HgUtil.loadContent(myProject, myRevisionNumber, fileToCat), getFile().getCharset());
}
@NotNull
private byte[] loadContent(@NotNull HgFile fileToCat) {
HgCommandResult result = new HgCatCommand(myProject).execute(fileToCat, myRevisionNumber, getFile().getCharset());
return result != null && result.getExitValue() == 0 ? result.getBytesOutput() : new byte[0];
}
@Nullable
public byte[] getContentAsBytes() throws VcsException {
public byte[] getContentAsBytes() {
final HgFile fileToCat = HgUtil.getFileNameInTargetRevision(myProject, myRevisionNumber, myHgFile);
try {
return ContentRevisionCache
.getOrLoadAsBytes(myProject, VcsUtil.getFilePath(fileToCat.getFile()), myRevisionNumber, HgVcs.getKey(),
ContentRevisionCache.UniqueType.REPOSITORY_CONTENT,
new Throwable2Computable<byte[], VcsException, IOException>() {
@Override
public byte[] compute() throws VcsException, IOException {
return loadContent(fileToCat);
}
});
}
catch (IOException e) {
throw new VcsException(e);
}
return HgUtil.loadContent(myProject, myRevisionNumber, fileToCat);
}
@NotNull
@@ -19,8 +19,6 @@ import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.history.VcsFileRevision;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.zmlx.hg4idea.command.HgCatCommand;
import org.zmlx.hg4idea.execution.HgCommandResult;
import org.zmlx.hg4idea.util.HgUtil;
import java.io.IOException;
@@ -103,9 +101,8 @@ public class HgFileRevision implements VcsFileRevision {
@NotNull
public byte[] loadContent() throws IOException, VcsException {
HgFile fileToCat = HgUtil.getFileNameInTargetRevision(myProject, myRevisionNumber, myFile);
HgCommandResult result = new HgCatCommand(myProject).execute(fileToCat, myRevisionNumber, myFile.toFilePath().getCharset());
return result != null && result.getExitValue() == 0 ? result.getBytesOutput() : new byte[0];
final HgFile fileToCat = HgUtil.getFileNameInTargetRevision(myProject, myRevisionNumber, myFile);
return HgUtil.loadContent(myProject, myRevisionNumber, fileToCat);
}
public byte[] getContent() throws IOException, VcsException {
@@ -42,6 +42,7 @@ import com.intellij.vcsUtil.VcsUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.zmlx.hg4idea.*;
import org.zmlx.hg4idea.command.HgCatCommand;
import org.zmlx.hg4idea.command.HgRemoveCommand;
import org.zmlx.hg4idea.command.HgStatusCommand;
import org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand;
@@ -511,6 +512,12 @@ public abstract class HgUtil {
}
}
@NotNull
public static byte[] loadContent(@NotNull Project project, @Nullable HgRevisionNumber revisionNumber, @NotNull HgFile fileToCat) {
HgCommandResult result = new HgCatCommand(project).execute(fileToCat, revisionNumber, fileToCat.toFilePath().getCharset());
return result != null && result.getExitValue() == 0 ? result.getBytesOutput() : new byte[0];
}
public static String removePasswordIfNeeded(@NotNull String path) {
Matcher matcher = URL_WITH_PASSWORD.matcher(path);
if (matcher.matches()) {