-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: applyPatch
reversely
#105
Comments
I'm not sure I fully understand the feature, but if you want to write some tests to demonstrate it, go for it. Generally I'm fine with adding contributed features as long as they don't add a lot of overhead relative to their general usefulness and have proper tests. |
@kpdecker I think @benjycui might be talking about the ability to "reverse" or "revert" a patch. I have a similar use case where if I apply a series of patches to a document how can I then "revert" them in the order they were applied. Is this possible or do I need to manually create a "reversed patch" for every one applied? |
Here's my method for reversing a patch. Happy to open a PR if this is the right approach, but I'm unsure if it would cover all use cases. const reversePatch = (patch: string) => {
const parsedDiff = diff.parsePatch(patch)[0]
const { oldFileName, newFileName, oldHeader, newHeader, hunks } = parsedDiff
parsedDiff.oldFileName = newFileName
parsedDiff.oldHeader = newHeader
parsedDiff.newFileName = oldFileName
parsedDiff.newHeader = oldHeader
for (const hunk of hunks) {
const { oldLines, oldStart, newLines, newStart, lines } = hunk
hunk.oldLines = newLines
hunk.oldStart = newStart
hunk.newLines = oldLines
hunk.newStart = oldStart
hunk.lines = lines.map(l => {
if (l.startsWith('-')) return `+${l.slice(1)}`
if (l.startsWith('+')) return `-${l.slice(1)}`
return l
})
}
return parsedDiff
} |
I was looking for the same feature and found this issue. It would be very useful to be able to apply a patch in reverse, like For example, for a Wiki post, blog post, etc, I would store just the latest text version and all patches from edits and I could show limited history of patches going back from the latest version and when people want to see some older version, I would apply patches in reverse to get the full text for that version. |
It seems that we cannot apply patch on newStr to calculate oldStr now. I read source code https://github.com/kpdecker/jsdiff/blob/master/src/patch/apply.js#L85 , and thought that we can add an option to do so.
If you accept this feature, I will create a pull request :-)
The text was updated successfully, but these errors were encountered: