use of com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier in project intellij-community by JetBrains.
the class ApplyPatchAction method applyOnly.
@NotNull
public static ApplyPatchStatus applyOnly(@Nullable final Project project, @NotNull final ApplyFilePatchBase patch, @Nullable final ApplyPatchContext context, @NotNull final VirtualFile file, @Nullable final CommitContext commitContext, boolean reverse, @Nullable String leftPanelTitle, @Nullable String rightPanelTitle) {
final ApplyFilePatch.Result result = tryApplyPatch(project, patch, context, file, commitContext);
final ApplyPatchStatus status = result.getStatus();
if (ApplyPatchStatus.ALREADY_APPLIED.equals(status) || ApplyPatchStatus.SUCCESS.equals(status)) {
return status;
}
final ApplyPatchForBaseRevisionTexts mergeData = result.getMergeData();
if (mergeData == null)
return status;
final Document document = FileDocumentManager.getInstance().getDocument(file);
if (document == null)
return ApplyPatchStatus.FAILURE;
String baseContent = toString(mergeData.getBase());
String localContent = toString(mergeData.getLocal());
String patchedContent = mergeData.getPatched();
if (localContent == null)
return ApplyPatchStatus.FAILURE;
final Ref<ApplyPatchStatus> applyPatchStatusReference = new Ref<>();
Consumer<MergeResult> callback = new Consumer<MergeResult>() {
@Override
public void consume(MergeResult result) {
FileDocumentManager.getInstance().saveDocument(document);
applyPatchStatusReference.setIfNull(result != MergeResult.CANCEL ? ApplyPatchStatus.SUCCESS : ApplyPatchStatus.FAILURE);
}
};
try {
MergeRequest request;
if (baseContent != null) {
if (reverse) {
if (leftPanelTitle == null)
leftPanelTitle = VcsBundle.message("patch.apply.conflict.patched.version");
if (rightPanelTitle == null)
rightPanelTitle = VcsBundle.message("patch.apply.conflict.local.version");
List<String> contents = ContainerUtil.list(patchedContent, baseContent, localContent);
List<String> titles = ContainerUtil.list(leftPanelTitle, null, rightPanelTitle);
request = PatchDiffRequestFactory.createMergeRequest(project, document, file, contents, null, titles, callback);
} else {
request = PatchDiffRequestFactory.createMergeRequest(project, document, file, baseContent, localContent, patchedContent, callback);
}
} else {
TextFilePatch textPatch = (TextFilePatch) patch.getPatch();
final GenericPatchApplier applier = new GenericPatchApplier(localContent, textPatch.getHunks());
applier.execute();
final AppliedTextPatch appliedTextPatch = AppliedTextPatch.create(applier.getAppliedInfo());
request = PatchDiffRequestFactory.createBadMergeRequest(project, document, file, localContent, appliedTextPatch, callback);
}
request.putUserData(DiffUserDataKeysEx.MERGE_ACTION_CAPTIONS, new Function<MergeResult, String>() {
@Override
public String fun(MergeResult result) {
return result.equals(MergeResult.CANCEL) ? "Abort..." : null;
}
});
request.putUserData(DiffUserDataKeysEx.MERGE_CANCEL_HANDLER, new Condition<MergeTool.MergeViewer>() {
@Override
public boolean value(MergeTool.MergeViewer viewer) {
int result = Messages.showYesNoCancelDialog(viewer.getComponent().getRootPane(), XmlStringUtil.wrapInHtml("Would you like to <u>A</u>bort&Rollback applying patch action or <u>S</u>kip this file?"), "Close Merge", "_Abort", "_Skip", "Cancel", Messages.getQuestionIcon());
if (result == Messages.YES) {
applyPatchStatusReference.set(ApplyPatchStatus.ABORT);
} else if (result == Messages.NO) {
applyPatchStatusReference.set(ApplyPatchStatus.SKIP);
}
return result != Messages.CANCEL;
}
});
DiffManager.getInstance().showMerge(project, request);
return applyPatchStatusReference.get();
} catch (InvalidDiffRequestException e) {
LOG.warn(e);
return ApplyPatchStatus.FAILURE;
}
}
use of com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier in project intellij-community by JetBrains.
the class PatchDiffRequestFactory method createConflictDiffRequest.
@NotNull
public static DiffRequest createConflictDiffRequest(@Nullable Project project, @Nullable VirtualFile file, @NotNull TextFilePatch patch, @NotNull String afterTitle, @NotNull final Getter<ApplyPatchForBaseRevisionTexts> textsGetter, @NotNull String name, @NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException {
if (file == null)
throw new DiffRequestProducerException("Can't show diff for '" + name + "'");
if (file.getFileType().isBinary())
throw new DiffRequestProducerException("Can't show diff for binary file '" + name + "'");
final Ref<ApplyPatchForBaseRevisionTexts> textsRef = new Ref<>();
ApplicationManager.getApplication().invokeAndWait(new Runnable() {
@Override
public void run() {
textsRef.set(textsGetter.get());
}
}, indicator.getModalityState());
ApplyPatchForBaseRevisionTexts texts = textsRef.get();
if (texts.getLocal() == null)
throw new DiffRequestProducerException("Can't show diff for '" + file.getPresentableUrl() + "'");
if (texts.getBase() == null) {
String localContent = texts.getLocal().toString();
final GenericPatchApplier applier = new GenericPatchApplier(localContent, patch.getHunks());
applier.execute();
final AppliedTextPatch appliedTextPatch = AppliedTextPatch.create(applier.getAppliedInfo());
return createBadDiffRequest(project, file, localContent, appliedTextPatch, null, null, null, null);
} else {
String localContent = texts.getLocal().toString();
String baseContent = texts.getBase().toString();
String patchedContent = texts.getPatched();
return createDiffRequest(project, file, ContainerUtil.list(localContent, baseContent, patchedContent), null, ContainerUtil.list("Current Version", "Base Version", afterTitle));
}
}
use of com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier in project intellij-community by JetBrains.
the class LazyPatchContentRevision method getContent.
public String getContent() {
if (myContent == null) {
final String localContext = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
final Document doc = FileDocumentManager.getInstance().getDocument(myVf);
return doc == null ? null : doc.getText();
}
});
if (localContext == null) {
myPatchApplyFailed = true;
return null;
}
final GenericPatchApplier applier = new GenericPatchApplier(localContext, myPatch.getHunks());
if (applier.execute()) {
myContent = applier.getAfter();
} else {
myPatchApplyFailed = true;
}
}
return myContent;
}
use of com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier in project intellij-community by JetBrains.
the class MatchPatchPaths method getMatchingLines.
private static int getMatchingLines(final AbstractFilePatchInProgress<TextFilePatch> patch) {
final VirtualFile base = patch.getCurrentBase();
if (base == null)
return -1;
String text;
try {
if (base.getLength() > BIG_FILE_BOUND) {
// partially
text = VfsUtilCore.loadText(base, BIG_FILE_BOUND);
} else {
text = VfsUtilCore.loadText(base);
}
} catch (IOException e) {
return 0;
}
return new GenericPatchApplier(text, patch.getPatch().getHunks()).weightContextMatch(100, 5);
}
Aggregations