IDEA-189367 vcs: "Local History" for directory - make current content editable

* mirror logic from `FileDifferenceModel`
This commit is contained in:
Aleksey Pivovarov
2018-06-28 18:40:21 +03:00
parent f0251c5c77
commit 61111cf320
6 changed files with 45 additions and 29 deletions
@@ -22,7 +22,9 @@ import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.ByteBackedContentRevision;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.changes.CurrentContentRevision;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcsUtil.VcsUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -33,11 +35,17 @@ public class Difference {
private final boolean myIsFile;
private final Entry myLeft;
private final Entry myRight;
private final boolean myRightContentCurrent;
public Difference(boolean isFile, Entry left, Entry right) {
this(isFile, left, right, false);
}
public Difference(boolean isFile, Entry left, Entry right, boolean isRightContentCurrent) {
myIsFile = isFile;
myLeft = left;
myRight = right;
myRightContentCurrent = isRightContentCurrent;
}
public boolean isFile() {
@@ -57,10 +65,14 @@ public class Difference {
}
public ContentRevision getRightContentRevision(IdeaGateway gw) {
if (myRightContentCurrent) {
VirtualFile file = gw.findVirtualFile(getRight().getPath());
if (file != null) return new CurrentContentRevision(VcsUtil.getFilePath(file));
}
return createContentRevision(getRight(), gw);
}
private ContentRevision createContentRevision(final Entry e, final IdeaGateway gw) {
private static ContentRevision createContentRevision(final Entry e, final IdeaGateway gw) {
if (e == null) return null;
return new ByteBackedContentRevision() {
@@ -56,6 +56,6 @@ public abstract class Revision {
public abstract Entry findEntry();
public List<Difference> getDifferencesWith(Revision right) {
return Entry.getDifferencesBetween(findEntry(), right.findEntry());
return Entry.getDifferencesBetween(findEntry(), right.findEntry(), right instanceof CurrentRevision);
}
}
@@ -136,11 +136,11 @@ public class DirectoryEntry extends Entry {
}
@Override
public void collectDifferencesWith(@NotNull Entry right, @NotNull List<Difference> result) {
public void collectDifferencesWith(@NotNull Entry right, @NotNull List<Difference> result, boolean isRightContentCurrent) {
DirectoryEntry e = (DirectoryEntry)right;
if (!getPath().equals(e.getPath())) {
result.add(new Difference(false, this, e));
result.add(new Difference(false, this, e, isRightContentCurrent));
}
// most often we have the same children, so try processing it directly
@@ -154,7 +154,7 @@ public class DirectoryEntry extends Entry {
Entry rightChildEntry = e.myChildren.get(commonIndex);
if (childEntry.getNameId() == rightChildEntry.getNameId() && childEntry.isDirectory() == rightChildEntry.isDirectory()) {
childEntry.collectDifferencesWith(rightChildEntry, result);
childEntry.collectDifferencesWith(rightChildEntry, result, isRightContentCurrent);
} else {
break;
}
@@ -206,16 +206,16 @@ public class DirectoryEntry extends Entry {
for (Entry child : e.myChildren) {
if (uniqueNameIdToRightChildEntries.containsKey(child.getNameId())) {
child.collectCreatedDifferences(result);
child.collectCreatedDifferences(result, isRightContentCurrent);
}
}
for (Entry child : myChildren) {
if (uniqueNameIdToMyChildEntries.containsKey(child.getNameId())) {
child.collectDeletedDifferences(result);
child.collectDeletedDifferences(result, isRightContentCurrent);
} else {
Entry itsChild = myNameIdToRightChildEntries.get(child.getNameId());
if (itsChild != null) child.collectDifferencesWith(itsChild, result);
if (itsChild != null) child.collectDifferencesWith(itsChild, result, isRightContentCurrent);
}
}
}
@@ -228,20 +228,20 @@ public class DirectoryEntry extends Entry {
}
@Override
protected void collectCreatedDifferences(@NotNull List<Difference> result) {
result.add(new Difference(false, null, this));
protected void collectCreatedDifferences(@NotNull List<Difference> result, boolean isRightContentCurrent) {
result.add(new Difference(false, null, this, isRightContentCurrent));
for (Entry child : myChildren) {
child.collectCreatedDifferences(result);
child.collectCreatedDifferences(result, isRightContentCurrent);
}
}
@Override
protected void collectDeletedDifferences(@NotNull List<Difference> result) {
result.add(new Difference(false, this, null));
protected void collectDeletedDifferences(@NotNull List<Difference> result, boolean isRightContentCurrent) {
result.add(new Difference(false, this, null, isRightContentCurrent));
for (Entry child : myChildren) {
child.collectDeletedDifferences(result);
child.collectDeletedDifferences(result, isRightContentCurrent);
}
}
}
@@ -207,19 +207,23 @@ public abstract class Entry {
}
public static List<Difference> getDifferencesBetween(Entry left, Entry right) {
return getDifferencesBetween(left, right, false);
}
public static List<Difference> getDifferencesBetween(Entry left, Entry right, boolean isRightContentCurrent) {
List<Difference> result = new SmartList<>();
if (left == null) right.collectCreatedDifferences(result);
else if (right == null) left.collectDeletedDifferences(result);
else left.collectDifferencesWith(right, result);
if (left == null) right.collectCreatedDifferences(result, isRightContentCurrent);
else if (right == null) left.collectDeletedDifferences(result, isRightContentCurrent);
else left.collectDifferencesWith(right, result, isRightContentCurrent);
return result;
}
protected abstract void collectDifferencesWith(@NotNull Entry e, @NotNull List<Difference> result);
protected abstract void collectDifferencesWith(@NotNull Entry e, @NotNull List<Difference> result, boolean isRightContentCurrent);
protected abstract void collectCreatedDifferences(@NotNull List<Difference> result);
protected abstract void collectCreatedDifferences(@NotNull List<Difference> result, boolean isRightContentCurrent);
protected abstract void collectDeletedDifferences(@NotNull List<Difference> result);
protected abstract void collectDeletedDifferences(@NotNull List<Difference> result, boolean isRightContentCurrent);
@Override
public String toString() {
@@ -97,21 +97,21 @@ public class FileEntry extends Entry {
}
@Override
public void collectDifferencesWith(@NotNull Entry e, @NotNull List<Difference> result) {
public void collectDifferencesWith(@NotNull Entry e, @NotNull List<Difference> result, boolean isRightContentCurrent) {
if (getPath().equals(e.getPath())
&& myContent.equals(e.getContent())
&& isReadOnly == e.isReadOnly()) return;
result.add(new Difference(true, this, e));
result.add(new Difference(true, this, e, isRightContentCurrent));
}
@Override
protected void collectCreatedDifferences(@NotNull List<Difference> result) {
result.add(new Difference(true, null, this));
protected void collectCreatedDifferences(@NotNull List<Difference> result, boolean isRightContentCurrent) {
result.add(new Difference(true, null, this, isRightContentCurrent));
}
@Override
protected void collectDeletedDifferences(@NotNull List<Difference> result) {
result.add(new Difference(true, this, null));
protected void collectDeletedDifferences(@NotNull List<Difference> result, boolean isRightContentCurrent) {
result.add(new Difference(true, this, null, isRightContentCurrent));
}
}
@@ -60,17 +60,17 @@ public class EntryTest extends LocalHistoryTestCase {
}
@Override
public void collectDifferencesWith(@NotNull Entry e, @NotNull List<Difference> result) {
public void collectDifferencesWith(@NotNull Entry e, @NotNull List<Difference> result, boolean isRightContentCurrent) {
throw new UnsupportedOperationException();
}
@Override
protected void collectCreatedDifferences(@NotNull List<Difference> result) {
protected void collectCreatedDifferences(@NotNull List<Difference> result, boolean isRightContentCurrent) {
throw new UnsupportedOperationException();
}
@Override
protected void collectDeletedDifferences(@NotNull List<Difference> result) {
protected void collectDeletedDifferences(@NotNull List<Difference> result, boolean isRightContentCurrent) {
throw new UnsupportedOperationException();
}
}