[lvcs]: api for rollback changed; refactoring

This commit is contained in:
Nadya Zabrodina
2016-01-25 17:05:20 +03:00
parent 9bee91b9e6
commit d4623b58cb
6 changed files with 34 additions and 39 deletions
@@ -16,12 +16,15 @@
package com.intellij.history;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
public interface Label {
Label NULL_INSTANCE = new Label() {
@Override
public long getLabelChangeId() {
return -1;
public void revert(@NotNull Project project, @NotNull VirtualFile file) {
}
public ByteContent getByteContent(String path) {
@@ -29,7 +32,7 @@ public interface Label {
}
};
long getLabelChangeId();
void revert(@NotNull Project project, @NotNull VirtualFile file);
ByteContent getByteContent(String path);
}
@@ -44,7 +44,4 @@ public abstract class LocalHistory {
public abstract boolean isUnderControl(VirtualFile f);
public void revertToLabel(@NotNull Project project, @NotNull VirtualFile f, @NotNull Label label) {
}
}
@@ -193,8 +193,8 @@ public class LocalHistoryImpl extends LocalHistory implements ApplicationCompone
private Label label(final LabelImpl impl) {
return new Label() {
@Override
public long getLabelChangeId() {
return impl.getLabelChangeId();
public void revert(@NotNull Project project, @NotNull VirtualFile file) {
revertToLabel(project, file, impl);
}
@Override
@@ -249,8 +249,7 @@ public class LocalHistoryImpl extends LocalHistory implements ApplicationCompone
return myGateway;
}
@Override
public void revertToLabel(@NotNull Project project, @NotNull VirtualFile f, @NotNull Label label) {
private void revertToLabel(@NotNull Project project, @NotNull VirtualFile f, LabelImpl impl) {
if (!f.exists() || !f.isValid()) {
notifyUser(project, String.format("File %s is not valid or doesn't exist", f.getName()));
return;
@@ -258,7 +257,7 @@ public class LocalHistoryImpl extends LocalHistory implements ApplicationCompone
HistoryDialogModel dirHistoryModel = f.isDirectory()
? new DirectoryHistoryDialogModel(project, myGateway, myVcs, f)
: new EntireFileHistoryDialogModel(project, myGateway, myVcs, f);
int leftRev = findRevisionIndexToRevert(dirHistoryModel, label);
int leftRev = findRevisionIndexToRevert(dirHistoryModel, impl);
if (leftRev < 0) {
notifyUser(project,
String.format("Couldn't find label revision. Try to use local history dialog for %s and perform revert manually.",
@@ -15,7 +15,7 @@
*/
package com.intellij.history.integration;
import com.intellij.history.Label;
import com.intellij.history.core.LabelImpl;
import com.intellij.history.core.revisions.ChangeRevision;
import com.intellij.history.core.revisions.Revision;
import com.intellij.history.integration.ui.models.HistoryDialogModel;
@@ -28,7 +28,7 @@ import java.util.List;
public class LocalHistoryUtil {
static int findRevisionIndexToRevert(@NotNull HistoryDialogModel dirHistoryModel, @NotNull Label label) {
static int findRevisionIndexToRevert(@NotNull HistoryDialogModel dirHistoryModel, @NotNull LabelImpl label) {
List<RevisionItem> revs = dirHistoryModel.getRevisions();
for (int i = 0; i < revs.size(); i++) {
final RevisionItem rev = revs.get(i);
@@ -39,7 +39,7 @@ public class LocalHistoryUtil {
return -1;
}
static boolean isLabelRevision(@NotNull RevisionItem rev, @NotNull Label label) {
static boolean isLabelRevision(@NotNull RevisionItem rev, @NotNull LabelImpl label) {
final long targetChangeId = label.getLabelChangeId();
return ContainerUtil.exists(rev.labels, new Condition<Revision>() {
@Override
@@ -26,28 +26,29 @@ public class HistoryReverterToLabelTest extends IntegrationTestCase {
public void testFileCreation() throws Exception {
createChildData(myRoot, "first.txt");
final LocalHistory localHistory = LocalHistory.getInstance();
final Label testLabel = localHistory.putSystemLabel(myProject, "testLabel");
final Label testLabel = LocalHistory.getInstance().putSystemLabel(myProject, "testLabel");
createChildData(myRoot, "foo.txt");
localHistory.revertToLabel(myProject, myRoot, testLabel);
revertToLabel(testLabel, myRoot);
assertNull(myRoot.findChild("foo.txt"));
assertNotNull(myRoot.findChild("first.txt"));
}
private void revertToLabel(Label testLabel, VirtualFile root) {
testLabel.revert(myProject, root);
}
public void testFileCreationAsFirstAction() throws Exception {
final LocalHistory localHistory = LocalHistory.getInstance();
final Label testLabel = localHistory.putSystemLabel(myProject, "testLabel");
final Label testLabel = LocalHistory.getInstance().putSystemLabel(myProject, "testLabel");
createChildData(myRoot, "foo.txt");
localHistory.revertToLabel(myProject, myRoot, testLabel);
revertToLabel(testLabel, myRoot);
assertNull(myRoot.findChild("foo.txt"));
}
public void testPutLabelAndRevertInstantly() throws IOException {
VirtualFile f = createChildData(myRoot, "foo.txt");
setBinaryContent(f, new byte[]{123}, -1, 4000, this);
final LocalHistory localHistory = LocalHistory.getInstance();
final Label testLabel = localHistory.putSystemLabel(myProject, "testLabel");
localHistory.revertToLabel(myProject, myRoot, testLabel);
final Label testLabel = LocalHistory.getInstance().putSystemLabel(myProject, "testLabel");
revertToLabel(testLabel, myRoot);
f = myRoot.findChild("foo.txt");
assertNotNull(f);
assertEquals(123, f.contentsToByteArray()[0]);
@@ -57,10 +58,9 @@ public class HistoryReverterToLabelTest extends IntegrationTestCase {
public void testFileDeletion() throws Exception {
VirtualFile f = createChildData(myRoot, "foo.txt");
setBinaryContent(f, new byte[]{123}, -1, 4000, this);
final LocalHistory localHistory = LocalHistory.getInstance();
final Label testLabel = localHistory.putSystemLabel(myProject, "testLabel");
final Label testLabel = LocalHistory.getInstance().putSystemLabel(myProject, "testLabel");
delete(f);
localHistory.revertToLabel(myProject, myRoot, testLabel);
revertToLabel(testLabel, myRoot);
f = myRoot.findChild("foo.txt");
assertNotNull(f);
assertEquals(123, f.contentsToByteArray()[0]);
@@ -69,11 +69,10 @@ public class HistoryReverterToLabelTest extends IntegrationTestCase {
public void testFileDeletionWithContent() throws Exception {
VirtualFile f = createChildData(myRoot, "foo.txt");
final LocalHistory localHistory = LocalHistory.getInstance();
final Label testLabel = localHistory.putSystemLabel(myProject, "testLabel");
final Label testLabel = LocalHistory.getInstance().putSystemLabel(myProject, "testLabel");
setBinaryContent(f, new byte[]{123}, -1, 4000, this);
delete(f);
localHistory.revertToLabel(myProject, myRoot, testLabel);
revertToLabel(testLabel, myRoot);
f = myRoot.findChild("foo.txt");
assertNotNull(f);
assertEquals(0, f.contentsToByteArray().length);
@@ -90,7 +89,7 @@ public class HistoryReverterToLabelTest extends IntegrationTestCase {
final Label testLabel2 = localHistory.putSystemLabel(myProject, "testLabel");
rename(f, "bar.txt");
localHistory.revertToLabel(myProject, f, testLabel2);
revertToLabel(testLabel2, f);
assertNotNull(myRoot.findChild("dir2"));
dir = myRoot.findChild("dir2");
@@ -102,7 +101,7 @@ public class HistoryReverterToLabelTest extends IntegrationTestCase {
assertEquals(123, f.contentsToByteArray()[0]);
assertEquals(4000, f.getTimeStamp());
localHistory.revertToLabel(myProject, myRoot, testLabel1);
revertToLabel(testLabel1, myRoot);
assertNull(myRoot.findChild("dir2"));
dir = myRoot.findChild("dir");
@@ -118,12 +117,11 @@ public class HistoryReverterToLabelTest extends IntegrationTestCase {
VirtualFile f = createChildData(myRoot, "foo.txt");
int modificationStamp1 = -1;
setBinaryContent(f, new byte[]{1}, modificationStamp1, 1000, this);
final LocalHistory localHistory = LocalHistory.getInstance();
final Label testLabel = localHistory.putSystemLabel(myProject, "testLabel");
final Label testLabel = LocalHistory.getInstance().putSystemLabel(myProject, "testLabel");
int modificationStamp = -1;
setBinaryContent(f, new byte[]{2}, modificationStamp, 2000, this);
setBinaryContent(f, new byte[]{3}, modificationStamp, 3000, this);
localHistory.revertToLabel(myProject, myRoot, testLabel);
revertToLabel(testLabel, myRoot);
f = myRoot.findChild("foo.txt");
assertNotNull(f);
assertEquals(1, f.contentsToByteArray()[0]);
@@ -136,12 +134,11 @@ public class HistoryReverterToLabelTest extends IntegrationTestCase {
setBinaryContent(f, new byte[]{1}, modificationStamp1, 1000, this);
VirtualFile f2 = createChildData(myRoot, "foo2.txt");
setBinaryContent(f, new byte[]{1}, modificationStamp1, 1000, this);
final LocalHistory localHistory = LocalHistory.getInstance();
final Label testLabel = localHistory.putSystemLabel(myProject, "testLabel");
final Label testLabel = LocalHistory.getInstance().putSystemLabel(myProject, "testLabel");
int modificationStamp = -1;
setBinaryContent(f, new byte[]{2}, modificationStamp, 2000, this);
setBinaryContent(f2, new byte[]{3}, modificationStamp, 3000, this);
localHistory.revertToLabel(myProject, f, testLabel);
revertToLabel(testLabel, f);
f = myRoot.findChild("foo.txt");
assertNotNull(f);
assertEquals(1, f.contentsToByteArray()[0]);
@@ -16,7 +16,6 @@
package com.intellij.openapi.diff.impl.patch.formove;
import com.intellij.history.Label;
import com.intellij.history.LocalHistory;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
@@ -75,7 +74,7 @@ class UndoApplyPatchDialog extends DialogWrapper {
ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
@Override
public void run() {
LocalHistory.getInstance().revertToLabel(myProject, myProject.getBaseDir(), myBeforeLabel);
myBeforeLabel.revert(myProject, myProject.getBaseDir());
}
}, "Rollback Applied Changes...", true, myProject);
}