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

The documentation is lacking examples #49

Open
zimmski opened this issue Nov 2, 2016 · 7 comments
Open

The documentation is lacking examples #49

zimmski opened this issue Nov 2, 2016 · 7 comments

Comments

@zimmski
Copy link
Collaborator

zimmski commented Nov 2, 2016

go-diff is currently lacking examples for each exported function and method. This would not only help other users but make the whole project a lot better and complete.

@tirithen
Copy link

tirithen commented Nov 19, 2016

Agreed, I'm rtying to create a gnu patch string and then beeing able to apply it at a later stage but I can't seem to understand how to do this:


var diff = diffmatchpatch.New()

// ApplyPatch takes a GNU patch string and applys it to a string
func ApplyPatch(patch string, text string) string {
    patches, err := diff.PatchFromText(patch)
    if err != nil {
        log.Fatal(err)
        return
    }
    return diff.PatchApply(patches, text)
}

// CreatePatch compares two strings and creates a patch string
func CreatePatch(textA string, textB string) string {
    diff := diff.DiffMain(textA, textB, true)
    // TODO: somehow convert diff to patch string
} 

@zimmski
Copy link
Collaborator Author

zimmski commented Dec 1, 2016

@tirithen: Maybe we should open a new issue for your question, but do you mean PatchMake?

@tirithen
Copy link

tirithen commented Dec 2, 2016

I wanted a way to create a diff/patch string and then I wanted to be able to apply that string. I could not understand how to do this full cycle of diffing and patching

@zimmski
Copy link
Collaborator Author

zimmski commented Dec 2, 2016

You can for example do that like this:

diffs := dmp.DiffMain(text1, text2, true)
patches := dmp.PatchMake(text1, diffs)
patchText := dmp.PatchToText(patches)
patchesFromText, _ := dmp.PatchFromText(patchText)
text2FromPatches, _ := dmp.PatchApply(patchesFromText, text1)

@tirithen
Copy link

tirithen commented Dec 2, 2016

Super! That seem to be it! ;D Got a bit lost in all the methods.

@zimmski
Copy link
Collaborator Author

zimmski commented Dec 2, 2016

Yeah, I will partly fix this with #53. Would you be interested in helping me out with the examples? I only have so much time to work on OSS so I would highly appreciate some help.

@idc77
Copy link

idc77 commented Dec 10, 2023

Complicated.

Imagine a wiki.

Data types

History data type

type History struct {
ID string
EntryID string
Date time.Time
PatchText string
PreviousHistoryID string
}

Entry data type

type Entry struct {
ID string
Content string
}

When I have
newText, oldText

New Entry:

  • insert new Entry
  • diff not needed

Update Entry:

  • diff needed
	dmp := diffmatchpatch.New()
	diffs := dmp.DiffMain(new, old, true)
	patches := dmp.PatchMake(new, diffs)
	patchText := dmp.PatchToText(patches)
  • get current History ID
  • insert new History
  • update PreviousHistoryID

Revert to History point

  • get histories up to point
  • apply all patches in sequencial order
  • set Entry content to result

for each of the History entries

	dmp := diffmatchpatch.New()
	patchesFromText, _ := dmp.PatchFromText(patchText)
	text2FromPatches, _ := dmp.PatchApply(patchesFromText, new)

How does Mediawiki do it?

I think I'll just store the old version in the history database without any diffing, only use diff to display differences between revisions.
Storage space is not a big issue, but complexity is.

  • no difftext stored in database
  • no complex operations necessary

Sometimes you need "a rubberducky" to talk to, maybe others will have the same problem and read it.

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

No branches or pull requests

3 participants