Skip to content
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

Closed
benjycui opened this issue Apr 5, 2016 · 4 comments · Fixed by #450
Closed

feat: applyPatch reversely #105

benjycui opened this issue Apr 5, 2016 · 4 comments · Fixed by #450

Comments

@benjycui
Copy link

benjycui commented Apr 5, 2016

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 :-)

@kpdecker
Copy link
Owner

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.

@Luiz-N
Copy link

Luiz-N commented Nov 5, 2016

@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?

@ericbiewener
Copy link

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
}

@gh-andre
Copy link

gh-andre commented Feb 5, 2022

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 patch --reverse does. This allows one to avoid having to store the very first original text and instead makes it possible to apply patches in reverse against the latest version.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants