[lvcs]: notify user if can't revert to label; tests added

This commit is contained in:
Nadya Zabrodina
2016-01-25 17:05:19 +03:00
parent 2e4fb0305d
commit 9bee91b9e6
2 changed files with 53 additions and 3 deletions
@@ -23,6 +23,9 @@ import com.intellij.history.integration.ui.models.DirectoryHistoryDialogModel;
import com.intellij.history.integration.ui.models.EntireFileHistoryDialogModel;
import com.intellij.history.integration.ui.models.HistoryDialogModel;
import com.intellij.history.utils.LocalHistoryLog;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.command.CommandProcessor;
@@ -248,15 +251,21 @@ public class LocalHistoryImpl extends LocalHistory implements ApplicationCompone
@Override
public void revertToLabel(@NotNull Project project, @NotNull VirtualFile f, @NotNull Label label) {
if (!f.exists() || !f.isValid()) {
notifyUser(project, String.format("File %s is not valid or doesn't exist", f.getName()));
return;
}
HistoryDialogModel dirHistoryModel = f.isDirectory()
? new DirectoryHistoryDialogModel(project, myGateway, myVcs, f)
: new EntireFileHistoryDialogModel(project, myGateway, myVcs, f);
int leftRev = findRevisionIndexToRevert(dirHistoryModel, label);
if (leftRev < 0) {
LocalHistoryLog.LOG.error(
String.format("Couldn't find label revision. try to Use local history dialog for %s and perform revert manually.", f.getName()));
notifyUser(project,
String.format("Couldn't find label revision. Try to use local history dialog for %s and perform revert manually.",
f.getName()));
return;
}
if (leftRev == 0) return; // we shouldn't revert because not changes found to revert;
try {
dirHistoryModel.selectRevisions(-1, leftRev - 1); //-1 because we should revert all changes up to previous one, but not label-related.
dirHistoryModel.createReverter().revert();
@@ -265,4 +274,9 @@ public class LocalHistoryImpl extends LocalHistory implements ApplicationCompone
LocalHistoryLog.LOG.error(String.format("Couldn't revert %s to local history label.", f.getName()), e);
}
}
private static void notifyUser(@NotNull Project project, @NotNull String message) {
new Notification(Notifications.SYSTEM_MESSAGES_GROUP_ID, "Can't rollback some changes", message,
NotificationType.ERROR).notify(project);
}
}
@@ -20,6 +20,8 @@ import com.intellij.history.LocalHistory;
import com.intellij.history.integration.IntegrationTestCase;
import com.intellij.openapi.vfs.VirtualFile;
import java.io.IOException;
public class HistoryReverterToLabelTest extends IntegrationTestCase {
public void testFileCreation() throws Exception {
@@ -40,6 +42,18 @@ public class HistoryReverterToLabelTest extends IntegrationTestCase {
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);
f = myRoot.findChild("foo.txt");
assertNotNull(f);
assertEquals(123, f.contentsToByteArray()[0]);
assertEquals(4000, f.getTimeStamp());
}
public void testFileDeletion() throws Exception {
VirtualFile f = createChildData(myRoot, "foo.txt");
setBinaryContent(f, new byte[]{123}, -1, 4000, this);
@@ -76,7 +90,7 @@ public class HistoryReverterToLabelTest extends IntegrationTestCase {
final Label testLabel2 = localHistory.putSystemLabel(myProject, "testLabel");
rename(f, "bar.txt");
localHistory.revertToLabel(myProject, myRoot, testLabel2);
localHistory.revertToLabel(myProject, f, testLabel2);
assertNotNull(myRoot.findChild("dir2"));
dir = myRoot.findChild("dir2");
@@ -115,4 +129,26 @@ public class HistoryReverterToLabelTest extends IntegrationTestCase {
assertEquals(1, f.contentsToByteArray()[0]);
assertEquals(1000, f.getTimeStamp());
}
public void testRevertContentChangeOnlyForFile() throws Exception {
VirtualFile f = createChildData(myRoot, "foo.txt");
int modificationStamp1 = -1;
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");
int modificationStamp = -1;
setBinaryContent(f, new byte[]{2}, modificationStamp, 2000, this);
setBinaryContent(f2, new byte[]{3}, modificationStamp, 3000, this);
localHistory.revertToLabel(myProject, f, testLabel);
f = myRoot.findChild("foo.txt");
assertNotNull(f);
assertEquals(1, f.contentsToByteArray()[0]);
assertEquals(1000, f.getTimeStamp());
f2 = myRoot.findChild("foo2.txt");
assertNotNull(f2);
assertEquals(3, f2.contentsToByteArray()[0]);
assertEquals(3000, f2.getTimeStamp());
}
}