if (virtualFile == null) {
Flog.warn("VirtualFile is null, no idea what do do. Aborting everything %s", this);
getBuf();
return;
}
IDoc d = context.iFactory.getDocument(virtualFile);
if (d == null) {
Flog.warn("Document not found for %s", virtualFile);
getBuf();
return;
}
String viewText;
if (!virtualFile.exists()) {
viewText = oldText;
} else {
viewText = d.getText();
if (viewText.equals(oldText)) {
b.forced_patch = false;
} else if (!b.forced_patch) {
b.forced_patch = true;
oldText = viewText;
b.send_patch(viewText);
Flog.warn("Sending force patch for %s. this is dangerous!", b.path);
}
}
b.cancelTimeout();
String md5Before = DigestUtils.md5Hex(viewText);
if (!md5Before.equals(res.md5_before)) {
Flog.warn("starting md5s don't match for %s. this is dangerous!", b.path);
}
List<diff_match_patch.Patch> patches = dmp.patch_fromText(res.patch);
final Object[] results = dmp.patch_apply((LinkedList<diff_match_patch.Patch>) patches, oldText);
final String patchedContents = (String) results[0];
final boolean[] patchesClean = (boolean[]) results[1];
final FlooPatchPosition[] positions = (FlooPatchPosition[]) results[2];
for (boolean clean : patchesClean) {
if (!clean) {
Flog.log("Patch not clean for %s. Sending get_buf and setting readonly.", d);
getBuf();
return;
}
}
// XXX: If patchedContents have carriage returns this will be a problem:
String md5After = DigestUtils.md5Hex(patchedContents);
if (!md5After.equals(res.md5_after)) {
Flog.info("MD5 after mismatch (ours %s remote %s)", md5After, res.md5_after);
}
if (!d.makeWritable()) {
Flog.info("Document: %s is not writable.", d);
return;
}
String text = d.patch(positions);
if (text == null) {
getBuf();
return;
}