go-diff icon indicating copy to clipboard operation
go-diff copied to clipboard

The documentation is lacking examples

Open zimmski opened this issue 9 years ago • 7 comments

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.

zimmski avatar Nov 02 '16 16:11 zimmski

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
} 

tirithen avatar Nov 19 '16 00:11 tirithen

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

zimmski avatar Dec 01 '16 22:12 zimmski

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

tirithen avatar Dec 02 '16 04:12 tirithen

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)

zimmski avatar Dec 02 '16 07:12 zimmski

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

tirithen avatar Dec 02 '16 09:12 tirithen

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.

zimmski avatar Dec 02 '16 10:12 zimmski

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.

idc77 avatar Dec 10 '23 18:12 idc77