/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.vcs.changes.patch;

import com.intellij.openapi.diff.impl.patch.TextFilePatch;
import com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import com.intellij.openapi.vfs.VirtualFile;
import javax.annotation.Nonnull;

public class LazyPatchContentRevision implements ContentRevision {
  private volatile String myContent;
  private final VirtualFile myVf;
  private final FilePath myNewFilePath;
  private final String myRevision;
  private final TextFilePatch myPatch;
  private volatile boolean myPatchApplyFailed;

  public LazyPatchContentRevision(final VirtualFile vf, final FilePath newFilePath, final String revision, final TextFilePatch patch) {
    myVf = vf;
    myNewFilePath = newFilePath;
    myRevision = revision;
    myPatch = patch;
  }

  public String getContent() {
    if (myContent == null) {
      final Document doc = FileDocumentManager.getInstance().getDocument(myVf);
      if (doc == null) {
        myPatchApplyFailed = true;
        return null;
      }
      final String localContext = doc.getText();

      final GenericPatchApplier applier = new GenericPatchApplier(localContext, myPatch.getHunks());
      if (applier.execute()) {
        myContent = applier.getAfter();
      } else {
        myPatchApplyFailed = true;
      }
    }
    return myContent;
  }

  public boolean isPatchApplyFailed() {
    return myPatchApplyFailed;
  }

  @Nonnull
  public FilePath getFile() {
    return myNewFilePath;
  }

  @Nonnull
  public VcsRevisionNumber getRevisionNumber() {
    return new VcsRevisionNumber() {
      public String asString() {
        return myRevision;
      }

      public int compareTo(final VcsRevisionNumber o) {
        return 0;
      }
    };
  }
}

@@ -1081,16 +1081,56 @@
 alFile;%0A
+import consulo.application.AccessRule;%0A%0A
 import j
@@ -1688,24 +1688,36 @@
 patch;%0A  %7D%0A%0A
+  @Override%0A
   public Str
@@ -1760,24 +1760,78 @@
  == null) %7B%0A
+      String localContext = AccessRule.read(() -%3E %7B%0A  
       final 
@@ -1904,19 +1904,20 @@
 ;%0A      
+  
 if
- 
 (doc == 
@@ -1936,34 +1936,33 @@
     
-myPatchApplyFailed = true;
+  return null;%0A        %7D%0A
 %0A   
@@ -1965,36 +1965,45 @@
 %0A        return 
-null
+doc.getText()
 ;%0A      %7D%0A      
@@ -1999,28 +1999,22 @@
    %7D
+);%0A
 %0A      
-final String 
+if (
 loca
@@ -2023,31 +2023,89 @@
 ontext =
- doc.getText();
+= null) %7B%0A        myPatchApplyFailed = true;%0A        return null;%0A      %7D
 %0A%0A      

fix read access error