From 9fc0a99db66d6a924361b04310c417fc0d4a98e5 Mon Sep 17 00:00:00 2001 From: Nadya Zabrodina Date: Tue, 28 May 2019 15:29:53 +0300 Subject: [PATCH] shelf: refactoring - make ShelvedChange immutable, deprecate old method GitOrigin-RevId: 68cf3dbf6cc8748d68c111a91d124e2293d37869 --- .../DiffShelvedChangesActionProvider.java | 6 +- .../vcs/changes/shelf/ShelvedChange.java | 67 +++++++++++-------- .../vcs/changes/shelf/ShelvedChangeList.java | 2 +- .../vcs/changes/shelf/ShelvedWrapper.java | 2 +- 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesActionProvider.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesActionProvider.java index 58b3142dce45..17823949a17d 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesActionProvider.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesActionProvider.java @@ -392,7 +392,7 @@ public class DiffShelvedChangesActionProvider implements AnActionExtensionProvid } } else { - return createDiffRequest(myProject, myChange.getChange(myProject), getName(), context, indicator); + return createDiffRequest(myProject, myChange.getChange(), getName(), context, indicator); } } } @@ -500,9 +500,9 @@ public class DiffShelvedChangesActionProvider implements AnActionExtensionProvid @NotNull TextFilePatch patch, @NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException { - DiffRequest diffRequest = myChange.isConflictingChange(myProject) + DiffRequest diffRequest = myChange.isConflictingChange() ? createConflictDiffRequest(myProject, myFile, patch, SHELVED_VERSION, texts, getName()) - : createDiffRequest(myProject, myChange.getChange(myProject), getName(), context, indicator); + : createDiffRequest(myProject, myChange.getChange(), getName(), context, indicator); if (!myWithLocal) { DiffUtil.addNotification(createNotification(DIFF_WITH_BASE_ERROR + " Showing difference with local version"), diffRequest); } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChange.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChange.java index f4aecec8773d..1e859f9676fe 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChange.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChange.java @@ -49,18 +49,23 @@ public class ShelvedChange { private final String myBeforePath; private final String myAfterPath; private final FileStatus myFileStatus; - private Change myChange; + @NotNull private final Change myChange; - public ShelvedChange(final String patchPath, final String beforePath, final String afterPath, final FileStatus fileStatus) { + public ShelvedChange(@NotNull Project project, + final String patchPath, + final String beforePath, + final String afterPath, + final FileStatus fileStatus) { myPatchPath = patchPath; myBeforePath = beforePath; // optimisation: memory myAfterPath = Comparing.equal(beforePath, afterPath) ? beforePath : afterPath; myFileStatus = fileStatus; + myChange = createChange(project); } - public boolean isConflictingChange(final Project project) { - ContentRevision afterRevision = getChange(project).getAfterRevision(); + public boolean isConflictingChange() { + ContentRevision afterRevision = getChange().getAfterRevision(); if (afterRevision == null) return false; try { afterRevision.getContent(); @@ -86,33 +91,39 @@ public class ShelvedChange { } @NotNull - public Change getChange(@NotNull Project project) { - // todo unify with - if (myChange == null) { - File baseDir = new File(Objects.requireNonNull(project.getBasePath())); - - File file = getAbsolutePath(baseDir, myBeforePath); - FilePath beforePath = VcsUtil.getFilePath(file, false); - ContentRevision beforeRevision = null; - if (myFileStatus != FileStatus.ADDED) { - beforeRevision = new CurrentContentRevision(beforePath) { - @Override - @NotNull - public VcsRevisionNumber getRevisionNumber() { - return new TextRevisionNumber(VcsBundle.message("local.version.title")); - } - }; - } - ContentRevision afterRevision = null; - if (myFileStatus != FileStatus.DELETED) { - FilePath afterPath = VcsUtil.getFilePath(getAbsolutePath(baseDir, myAfterPath), false); - afterRevision = new PatchedContentRevision(project, beforePath, afterPath); - } - myChange = new Change(beforeRevision, afterRevision, myFileStatus); - } + public Change getChange() { return myChange; } + @NotNull + @Deprecated + public Change getChange(@NotNull Project project) { + return myChange; + } + + private Change createChange(@NotNull Project project) { + File baseDir = new File(Objects.requireNonNull(project.getBasePath())); + + File file = getAbsolutePath(baseDir, myBeforePath); + FilePath beforePath = VcsUtil.getFilePath(file, false); + ContentRevision beforeRevision = null; + if (myFileStatus != FileStatus.ADDED) { + beforeRevision = new CurrentContentRevision(beforePath) { + @Override + @NotNull + public VcsRevisionNumber getRevisionNumber() { + return new TextRevisionNumber(VcsBundle.message("local.version.title")); + } + }; + } + ContentRevision afterRevision = null; + if (myFileStatus != FileStatus.DELETED) { + FilePath afterPath = VcsUtil.getFilePath(getAbsolutePath(baseDir, myAfterPath), false); + afterRevision = new PatchedContentRevision(project, beforePath, afterPath); + } + return new Change(beforeRevision, afterRevision, myFileStatus); + } + private static File getAbsolutePath(final File baseDir, final String relativePath) { File file; try { diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChangeList.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChangeList.java index 5a19173f1cc7..603b8c9a51ec 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChangeList.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChangeList.java @@ -156,7 +156,7 @@ public class ShelvedChangeList implements JDOMExternalizable, ExternalizableSche else { status = FileStatus.MODIFIED; } - myChanges.add(new ShelvedChange(PATH, patch.getBeforeName(), patch.getAfterName(), status)); + myChanges.add(new ShelvedChange(project, PATH, patch.getBeforeName(), patch.getAfterName(), status)); } } catch (Exception e) { diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedWrapper.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedWrapper.java index 5a5602075beb..bc507fc9b1d6 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedWrapper.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedWrapper.java @@ -70,7 +70,7 @@ class ShelvedWrapper { } Change getChange(@NotNull Project project) { - return myShelvedChange != null ? myShelvedChange.getChange(project) : assertNotNull(myBinaryFile).createChange(project); + return myShelvedChange != null ? myShelvedChange.getChange() : assertNotNull(myBinaryFile).createChange(project); } @Nullable