The documentation is lacking examples
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.
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: Maybe we should open a new issue for your question, but do you mean PatchMake?
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
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)
Super! That seem to be it! ;D Got a bit lost in all the methods.
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.
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.